api.c 2.4 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415354163541735418354193542035421354223542335424354253542635427354283542935430354313543235433354343543535436354373543835439354403544135442354433544435445354463544735448354493545035451354523545335454354553545635457354583545935460354613546235463354643546535466354673546835469354703547135472354733547435475354763547735478354793548035481354823548335484354853548635487354883548935490354913549235493354943549535496354973549835499355003550135502355033550435505355063550735508355093551035511355123551335514355153551635517355183551935520355213552235523355243552535526355273552835529355303553135532355333553435535355363553735538355393554035541355423554335544355453554635547355483554935550355513555235553355543555535556355573555835559355603556135562355633556435565355663556735568355693557035571355723557335574355753557635577355783557935580355813558235583355843558535586355873558835589355903559135592355933559435595355963559735598355993560035601356023560335604356053560635607356083560935610356113561235613356143561535616356173561835619356203562135622356233562435625356263562735628356293563035631356323563335634356353563635637356383563935640356413564235643356443564535646356473564835649356503565135652356533565435655356563565735658356593566035661356623566335664356653566635667356683566935670356713567235673356743567535676356773567835679356803568135682356833568435685356863568735688356893569035691356923569335694356953569635697356983569935700357013570235703357043570535706357073570835709357103571135712357133571435715357163571735718357193572035721357223572335724357253572635727357283572935730357313573235733357343573535736357373573835739357403574135742357433574435745357463574735748357493575035751357523575335754357553575635757357583575935760357613576235763357643576535766357673576835769357703577135772357733577435775357763577735778357793578035781357823578335784357853578635787357883578935790357913579235793357943579535796357973579835799358003580135802358033580435805358063580735808358093581035811358123581335814358153581635817358183581935820358213582235823358243582535826358273582835829358303583135832358333583435835358363583735838358393584035841358423584335844358453584635847358483584935850358513585235853358543585535856358573585835859358603586135862358633586435865358663586735868358693587035871358723587335874358753587635877358783587935880358813588235883358843588535886358873588835889358903589135892358933589435895358963589735898358993590035901359023590335904359053590635907359083590935910359113591235913359143591535916359173591835919359203592135922359233592435925359263592735928359293593035931359323593335934359353593635937359383593935940359413594235943359443594535946359473594835949359503595135952359533595435955359563595735958359593596035961359623596335964359653596635967359683596935970359713597235973359743597535976359773597835979359803598135982359833598435985359863598735988359893599035991359923599335994359953599635997359983599936000360013600236003360043600536006360073600836009360103601136012360133601436015360163601736018360193602036021360223602336024360253602636027360283602936030360313603236033360343603536036360373603836039360403604136042360433604436045360463604736048360493605036051360523605336054360553605636057360583605936060360613606236063360643606536066360673606836069360703607136072360733607436075360763607736078360793608036081360823608336084360853608636087360883608936090360913609236093360943609536096360973609836099361003610136102361033610436105361063610736108361093611036111361123611336114361153611636117361183611936120361213612236123361243612536126361273612836129361303613136132361333613436135361363613736138361393614036141361423614336144361453614636147361483614936150361513615236153361543615536156361573615836159361603616136162361633616436165361663616736168361693617036171361723617336174361753617636177361783617936180361813618236183361843618536186361873618836189361903619136192361933619436195361963619736198361993620036201362023620336204362053620636207362083620936210362113621236213362143621536216362173621836219362203622136222362233622436225362263622736228362293623036231362323623336234362353623636237362383623936240362413624236243362443624536246362473624836249362503625136252362533625436255362563625736258362593626036261362623626336264362653626636267362683626936270362713627236273362743627536276362773627836279362803628136282362833628436285362863628736288362893629036291362923629336294362953629636297362983629936300363013630236303363043630536306363073630836309363103631136312363133631436315363163631736318363193632036321363223632336324363253632636327363283632936330363313633236333363343633536336363373633836339363403634136342363433634436345363463634736348363493635036351363523635336354363553635636357363583635936360363613636236363363643636536366363673636836369363703637136372363733637436375363763637736378363793638036381363823638336384363853638636387363883638936390363913639236393363943639536396363973639836399364003640136402364033640436405364063640736408364093641036411364123641336414364153641636417364183641936420364213642236423364243642536426364273642836429364303643136432364333643436435364363643736438364393644036441364423644336444364453644636447364483644936450364513645236453364543645536456364573645836459364603646136462364633646436465364663646736468364693647036471364723647336474364753647636477364783647936480364813648236483364843648536486364873648836489364903649136492364933649436495364963649736498364993650036501365023650336504365053650636507365083650936510365113651236513365143651536516365173651836519365203652136522365233652436525365263652736528365293653036531365323653336534365353653636537365383653936540365413654236543365443654536546365473654836549365503655136552365533655436555365563655736558365593656036561365623656336564365653656636567365683656936570365713657236573365743657536576365773657836579365803658136582365833658436585365863658736588365893659036591365923659336594365953659636597365983659936600366013660236603366043660536606366073660836609366103661136612366133661436615366163661736618366193662036621366223662336624366253662636627366283662936630366313663236633366343663536636366373663836639366403664136642366433664436645366463664736648366493665036651366523665336654366553665636657366583665936660366613666236663366643666536666366673666836669366703667136672366733667436675366763667736678366793668036681366823668336684366853668636687366883668936690366913669236693366943669536696366973669836699367003670136702367033670436705367063670736708367093671036711367123671336714367153671636717367183671936720367213672236723367243672536726367273672836729367303673136732367333673436735367363673736738367393674036741367423674336744367453674636747367483674936750367513675236753367543675536756367573675836759367603676136762367633676436765367663676736768367693677036771367723677336774367753677636777367783677936780367813678236783367843678536786367873678836789367903679136792367933679436795367963679736798367993680036801368023680336804368053680636807368083680936810368113681236813368143681536816368173681836819368203682136822368233682436825368263682736828368293683036831368323683336834368353683636837368383683936840368413684236843368443684536846368473684836849368503685136852368533685436855368563685736858368593686036861368623686336864368653686636867368683686936870368713687236873368743687536876368773687836879368803688136882368833688436885368863688736888368893689036891368923689336894368953689636897368983689936900369013690236903369043690536906369073690836909369103691136912369133691436915369163691736918369193692036921369223692336924369253692636927369283692936930369313693236933369343693536936369373693836939369403694136942369433694436945369463694736948369493695036951369523695336954369553695636957369583695936960369613696236963369643696536966369673696836969369703697136972369733697436975369763697736978369793698036981369823698336984369853698636987369883698936990369913699236993369943699536996369973699836999370003700137002370033700437005370063700737008370093701037011370123701337014370153701637017370183701937020370213702237023370243702537026370273702837029370303703137032370333703437035370363703737038370393704037041370423704337044370453704637047370483704937050370513705237053370543705537056370573705837059370603706137062370633706437065370663706737068370693707037071370723707337074370753707637077370783707937080370813708237083370843708537086370873708837089370903709137092370933709437095370963709737098370993710037101371023710337104371053710637107371083710937110371113711237113371143711537116371173711837119371203712137122371233712437125371263712737128371293713037131371323713337134371353713637137371383713937140371413714237143371443714537146371473714837149371503715137152371533715437155371563715737158371593716037161371623716337164371653716637167371683716937170371713717237173371743717537176371773717837179371803718137182371833718437185371863718737188371893719037191371923719337194371953719637197371983719937200372013720237203372043720537206372073720837209372103721137212372133721437215372163721737218372193722037221372223722337224372253722637227372283722937230372313723237233372343723537236372373723837239372403724137242372433724437245372463724737248372493725037251372523725337254372553725637257372583725937260372613726237263372643726537266372673726837269372703727137272372733727437275372763727737278372793728037281372823728337284372853728637287372883728937290372913729237293372943729537296372973729837299373003730137302373033730437305373063730737308373093731037311373123731337314373153731637317373183731937320373213732237323373243732537326373273732837329373303733137332373333733437335373363733737338373393734037341373423734337344373453734637347373483734937350373513735237353373543735537356373573735837359373603736137362373633736437365373663736737368373693737037371373723737337374373753737637377373783737937380373813738237383373843738537386373873738837389373903739137392373933739437395373963739737398373993740037401374023740337404374053740637407374083740937410374113741237413374143741537416374173741837419374203742137422374233742437425374263742737428374293743037431374323743337434374353743637437374383743937440374413744237443374443744537446374473744837449374503745137452374533745437455374563745737458374593746037461374623746337464374653746637467374683746937470374713747237473374743747537476374773747837479374803748137482374833748437485374863748737488374893749037491374923749337494374953749637497374983749937500375013750237503375043750537506375073750837509375103751137512375133751437515375163751737518375193752037521375223752337524375253752637527375283752937530375313753237533375343753537536375373753837539375403754137542375433754437545375463754737548375493755037551375523755337554375553755637557375583755937560375613756237563375643756537566375673756837569375703757137572375733757437575375763757737578375793758037581375823758337584375853758637587375883758937590375913759237593375943759537596375973759837599376003760137602376033760437605376063760737608376093761037611376123761337614376153761637617376183761937620376213762237623376243762537626376273762837629376303763137632376333763437635376363763737638376393764037641376423764337644376453764637647376483764937650376513765237653376543765537656376573765837659376603766137662376633766437665376663766737668376693767037671376723767337674376753767637677376783767937680376813768237683376843768537686376873768837689376903769137692376933769437695376963769737698376993770037701377023770337704377053770637707377083770937710377113771237713377143771537716377173771837719377203772137722377233772437725377263772737728377293773037731377323773337734377353773637737377383773937740377413774237743377443774537746377473774837749377503775137752377533775437755377563775737758377593776037761377623776337764377653776637767377683776937770377713777237773377743777537776377773777837779377803778137782377833778437785377863778737788377893779037791377923779337794377953779637797377983779937800378013780237803378043780537806378073780837809378103781137812378133781437815378163781737818378193782037821378223782337824378253782637827378283782937830378313783237833378343783537836378373783837839378403784137842378433784437845378463784737848378493785037851378523785337854378553785637857378583785937860378613786237863378643786537866378673786837869378703787137872378733787437875378763787737878378793788037881378823788337884378853788637887378883788937890378913789237893378943789537896378973789837899379003790137902379033790437905379063790737908379093791037911379123791337914379153791637917379183791937920379213792237923379243792537926379273792837929379303793137932379333793437935379363793737938379393794037941379423794337944379453794637947379483794937950379513795237953379543795537956379573795837959379603796137962379633796437965379663796737968379693797037971379723797337974379753797637977379783797937980379813798237983379843798537986379873798837989379903799137992379933799437995379963799737998379993800038001380023800338004380053800638007380083800938010380113801238013380143801538016380173801838019380203802138022380233802438025380263802738028380293803038031380323803338034380353803638037380383803938040380413804238043380443804538046380473804838049380503805138052380533805438055380563805738058380593806038061380623806338064380653806638067380683806938070380713807238073380743807538076380773807838079380803808138082380833808438085380863808738088380893809038091380923809338094380953809638097380983809938100381013810238103381043810538106381073810838109381103811138112381133811438115381163811738118381193812038121381223812338124381253812638127381283812938130381313813238133381343813538136381373813838139381403814138142381433814438145381463814738148381493815038151381523815338154381553815638157381583815938160381613816238163381643816538166381673816838169381703817138172381733817438175381763817738178381793818038181381823818338184381853818638187381883818938190381913819238193381943819538196381973819838199382003820138202382033820438205382063820738208382093821038211382123821338214382153821638217382183821938220382213822238223382243822538226382273822838229382303823138232382333823438235382363823738238382393824038241382423824338244382453824638247382483824938250382513825238253382543825538256382573825838259382603826138262382633826438265382663826738268382693827038271382723827338274382753827638277382783827938280382813828238283382843828538286382873828838289382903829138292382933829438295382963829738298382993830038301383023830338304383053830638307383083830938310383113831238313383143831538316383173831838319383203832138322383233832438325383263832738328383293833038331383323833338334383353833638337383383833938340383413834238343383443834538346383473834838349383503835138352383533835438355383563835738358383593836038361383623836338364383653836638367383683836938370383713837238373383743837538376383773837838379383803838138382383833838438385383863838738388383893839038391383923839338394383953839638397383983839938400384013840238403384043840538406384073840838409384103841138412384133841438415384163841738418384193842038421384223842338424384253842638427384283842938430384313843238433384343843538436384373843838439384403844138442384433844438445384463844738448384493845038451384523845338454384553845638457384583845938460384613846238463384643846538466384673846838469384703847138472384733847438475384763847738478384793848038481384823848338484384853848638487384883848938490384913849238493384943849538496384973849838499385003850138502385033850438505385063850738508385093851038511385123851338514385153851638517385183851938520385213852238523385243852538526385273852838529385303853138532385333853438535385363853738538385393854038541385423854338544385453854638547385483854938550385513855238553385543855538556385573855838559385603856138562385633856438565385663856738568385693857038571385723857338574385753857638577385783857938580385813858238583385843858538586385873858838589385903859138592385933859438595385963859738598385993860038601386023860338604386053860638607386083860938610386113861238613386143861538616386173861838619386203862138622386233862438625386263862738628386293863038631386323863338634386353863638637386383863938640386413864238643386443864538646386473864838649386503865138652386533865438655386563865738658386593866038661386623866338664386653866638667386683866938670386713867238673386743867538676386773867838679386803868138682386833868438685386863868738688386893869038691386923869338694386953869638697386983869938700387013870238703387043870538706387073870838709387103871138712387133871438715387163871738718387193872038721387223872338724387253872638727387283872938730387313873238733387343873538736387373873838739387403874138742387433874438745387463874738748387493875038751387523875338754387553875638757387583875938760387613876238763387643876538766387673876838769387703877138772387733877438775387763877738778387793878038781387823878338784387853878638787387883878938790387913879238793387943879538796387973879838799388003880138802388033880438805388063880738808388093881038811388123881338814388153881638817388183881938820388213882238823388243882538826388273882838829388303883138832388333883438835388363883738838388393884038841388423884338844388453884638847388483884938850388513885238853388543885538856388573885838859388603886138862388633886438865388663886738868388693887038871388723887338874388753887638877388783887938880388813888238883388843888538886388873888838889388903889138892388933889438895388963889738898388993890038901389023890338904389053890638907389083890938910389113891238913389143891538916389173891838919389203892138922389233892438925389263892738928389293893038931389323893338934389353893638937389383893938940389413894238943389443894538946389473894838949389503895138952389533895438955389563895738958389593896038961389623896338964389653896638967389683896938970389713897238973389743897538976389773897838979389803898138982389833898438985389863898738988389893899038991389923899338994389953899638997389983899939000390013900239003390043900539006390073900839009390103901139012390133901439015390163901739018390193902039021390223902339024390253902639027390283902939030390313903239033390343903539036390373903839039390403904139042390433904439045390463904739048390493905039051390523905339054390553905639057390583905939060390613906239063390643906539066390673906839069390703907139072390733907439075390763907739078390793908039081390823908339084390853908639087390883908939090390913909239093390943909539096390973909839099391003910139102391033910439105391063910739108391093911039111391123911339114391153911639117391183911939120391213912239123391243912539126391273912839129391303913139132391333913439135391363913739138391393914039141391423914339144391453914639147391483914939150391513915239153391543915539156391573915839159391603916139162391633916439165391663916739168391693917039171391723917339174391753917639177391783917939180391813918239183391843918539186391873918839189391903919139192391933919439195391963919739198391993920039201392023920339204392053920639207392083920939210392113921239213392143921539216392173921839219392203922139222392233922439225392263922739228392293923039231392323923339234392353923639237392383923939240392413924239243392443924539246392473924839249392503925139252392533925439255392563925739258392593926039261392623926339264392653926639267392683926939270392713927239273392743927539276392773927839279392803928139282392833928439285392863928739288392893929039291392923929339294392953929639297392983929939300393013930239303393043930539306393073930839309393103931139312393133931439315393163931739318393193932039321393223932339324393253932639327393283932939330393313933239333393343933539336393373933839339393403934139342393433934439345393463934739348393493935039351393523935339354393553935639357393583935939360393613936239363393643936539366393673936839369393703937139372393733937439375393763937739378393793938039381393823938339384393853938639387393883938939390393913939239393393943939539396393973939839399394003940139402394033940439405394063940739408394093941039411394123941339414394153941639417394183941939420394213942239423394243942539426394273942839429394303943139432394333943439435394363943739438394393944039441394423944339444394453944639447394483944939450394513945239453394543945539456394573945839459394603946139462394633946439465394663946739468394693947039471394723947339474394753947639477394783947939480394813948239483394843948539486394873948839489394903949139492394933949439495394963949739498394993950039501395023950339504395053950639507395083950939510395113951239513395143951539516395173951839519395203952139522395233952439525395263952739528395293953039531395323953339534395353953639537395383953939540395413954239543395443954539546395473954839549395503955139552395533955439555395563955739558395593956039561395623956339564395653956639567395683956939570395713957239573395743957539576395773957839579395803958139582395833958439585395863958739588395893959039591395923959339594395953959639597395983959939600396013960239603396043960539606396073960839609396103961139612396133961439615396163961739618396193962039621396223962339624396253962639627396283962939630396313963239633396343963539636396373963839639396403964139642396433964439645396463964739648396493965039651396523965339654396553965639657396583965939660396613966239663396643966539666396673966839669396703967139672396733967439675396763967739678396793968039681396823968339684396853968639687396883968939690396913969239693396943969539696396973969839699397003970139702397033970439705397063970739708397093971039711397123971339714397153971639717397183971939720397213972239723397243972539726397273972839729397303973139732397333973439735397363973739738397393974039741397423974339744397453974639747397483974939750397513975239753397543975539756397573975839759397603976139762397633976439765397663976739768397693977039771397723977339774397753977639777397783977939780397813978239783397843978539786397873978839789397903979139792397933979439795397963979739798397993980039801398023980339804398053980639807398083980939810398113981239813398143981539816398173981839819398203982139822398233982439825398263982739828398293983039831398323983339834398353983639837398383983939840398413984239843398443984539846398473984839849398503985139852398533985439855398563985739858398593986039861398623986339864398653986639867398683986939870398713987239873398743987539876398773987839879398803988139882398833988439885398863988739888398893989039891398923989339894398953989639897398983989939900399013990239903399043990539906399073990839909399103991139912399133991439915399163991739918399193992039921399223992339924399253992639927399283992939930399313993239933399343993539936399373993839939399403994139942399433994439945399463994739948399493995039951399523995339954399553995639957399583995939960399613996239963399643996539966399673996839969399703997139972399733997439975399763997739978399793998039981399823998339984399853998639987399883998939990399913999239993399943999539996399973999839999400004000140002400034000440005400064000740008400094001040011400124001340014400154001640017400184001940020400214002240023400244002540026400274002840029400304003140032400334003440035400364003740038400394004040041400424004340044400454004640047400484004940050400514005240053400544005540056400574005840059400604006140062400634006440065400664006740068400694007040071400724007340074400754007640077400784007940080400814008240083400844008540086400874008840089400904009140092400934009440095400964009740098400994010040101401024010340104401054010640107401084010940110401114011240113401144011540116401174011840119401204012140122401234012440125401264012740128401294013040131401324013340134401354013640137401384013940140401414014240143401444014540146401474014840149401504015140152401534015440155401564015740158401594016040161401624016340164401654016640167401684016940170401714017240173401744017540176401774017840179401804018140182401834018440185401864018740188401894019040191401924019340194401954019640197401984019940200402014020240203402044020540206402074020840209402104021140212402134021440215402164021740218402194022040221402224022340224402254022640227402284022940230402314023240233402344023540236402374023840239402404024140242402434024440245402464024740248402494025040251402524025340254402554025640257402584025940260402614026240263402644026540266402674026840269402704027140272402734027440275402764027740278402794028040281402824028340284402854028640287402884028940290402914029240293402944029540296402974029840299403004030140302403034030440305403064030740308403094031040311403124031340314403154031640317403184031940320403214032240323403244032540326403274032840329403304033140332403334033440335403364033740338403394034040341403424034340344403454034640347403484034940350403514035240353403544035540356403574035840359403604036140362403634036440365403664036740368403694037040371403724037340374403754037640377403784037940380403814038240383403844038540386403874038840389403904039140392403934039440395403964039740398403994040040401404024040340404404054040640407404084040940410404114041240413404144041540416404174041840419404204042140422404234042440425404264042740428404294043040431404324043340434404354043640437404384043940440404414044240443404444044540446404474044840449404504045140452404534045440455404564045740458404594046040461404624046340464404654046640467404684046940470404714047240473404744047540476404774047840479404804048140482404834048440485404864048740488404894049040491404924049340494404954049640497404984049940500405014050240503405044050540506405074050840509405104051140512405134051440515405164051740518405194052040521405224052340524405254052640527405284052940530405314053240533405344053540536405374053840539405404054140542405434054440545405464054740548405494055040551405524055340554405554055640557405584055940560405614056240563405644056540566405674056840569405704057140572405734057440575405764057740578405794058040581405824058340584405854058640587405884058940590405914059240593405944059540596405974059840599406004060140602406034060440605406064060740608406094061040611406124061340614406154061640617406184061940620406214062240623406244062540626406274062840629406304063140632406334063440635406364063740638406394064040641406424064340644406454064640647406484064940650406514065240653406544065540656406574065840659406604066140662406634066440665406664066740668406694067040671406724067340674406754067640677406784067940680406814068240683406844068540686406874068840689406904069140692406934069440695406964069740698406994070040701407024070340704407054070640707407084070940710407114071240713407144071540716407174071840719407204072140722407234072440725407264072740728407294073040731407324073340734407354073640737407384073940740407414074240743407444074540746407474074840749407504075140752407534075440755407564075740758407594076040761407624076340764407654076640767407684076940770407714077240773407744077540776407774077840779407804078140782407834078440785407864078740788407894079040791407924079340794407954079640797407984079940800408014080240803408044080540806408074080840809408104081140812408134081440815408164081740818408194082040821408224082340824408254082640827408284082940830408314083240833408344083540836408374083840839408404084140842408434084440845408464084740848408494085040851408524085340854408554085640857408584085940860408614086240863408644086540866408674086840869408704087140872408734087440875408764087740878408794088040881408824088340884408854088640887408884088940890408914089240893408944089540896408974089840899409004090140902409034090440905409064090740908409094091040911409124091340914409154091640917409184091940920409214092240923409244092540926409274092840929409304093140932409334093440935409364093740938409394094040941409424094340944409454094640947409484094940950409514095240953409544095540956409574095840959409604096140962409634096440965409664096740968409694097040971409724097340974409754097640977409784097940980409814098240983409844098540986409874098840989409904099140992409934099440995409964099740998409994100041001410024100341004410054100641007410084100941010410114101241013410144101541016410174101841019410204102141022410234102441025410264102741028410294103041031410324103341034410354103641037410384103941040410414104241043410444104541046410474104841049410504105141052410534105441055410564105741058410594106041061410624106341064410654106641067410684106941070410714107241073410744107541076410774107841079410804108141082410834108441085410864108741088410894109041091410924109341094410954109641097410984109941100411014110241103411044110541106411074110841109411104111141112411134111441115411164111741118411194112041121411224112341124411254112641127411284112941130411314113241133411344113541136411374113841139411404114141142411434114441145411464114741148411494115041151411524115341154411554115641157411584115941160411614116241163411644116541166411674116841169411704117141172411734117441175411764117741178411794118041181411824118341184411854118641187411884118941190411914119241193411944119541196411974119841199412004120141202412034120441205412064120741208412094121041211412124121341214412154121641217412184121941220412214122241223412244122541226412274122841229412304123141232412334123441235412364123741238412394124041241412424124341244412454124641247412484124941250412514125241253412544125541256412574125841259412604126141262412634126441265412664126741268412694127041271412724127341274412754127641277412784127941280412814128241283412844128541286412874128841289412904129141292412934129441295412964129741298412994130041301413024130341304413054130641307413084130941310413114131241313413144131541316413174131841319413204132141322413234132441325413264132741328413294133041331413324133341334413354133641337413384133941340413414134241343413444134541346413474134841349413504135141352413534135441355413564135741358413594136041361413624136341364413654136641367413684136941370413714137241373413744137541376413774137841379413804138141382413834138441385413864138741388413894139041391413924139341394413954139641397413984139941400414014140241403414044140541406414074140841409414104141141412414134141441415414164141741418414194142041421414224142341424414254142641427414284142941430414314143241433414344143541436414374143841439414404144141442414434144441445414464144741448414494145041451414524145341454414554145641457414584145941460414614146241463414644146541466414674146841469414704147141472414734147441475414764147741478414794148041481414824148341484414854148641487414884148941490414914149241493414944149541496414974149841499415004150141502415034150441505415064150741508415094151041511415124151341514415154151641517415184151941520415214152241523415244152541526415274152841529415304153141532415334153441535415364153741538415394154041541415424154341544415454154641547415484154941550415514155241553415544155541556415574155841559415604156141562415634156441565415664156741568415694157041571415724157341574415754157641577415784157941580415814158241583415844158541586415874158841589415904159141592415934159441595415964159741598415994160041601416024160341604416054160641607416084160941610416114161241613416144161541616416174161841619416204162141622416234162441625416264162741628416294163041631416324163341634416354163641637416384163941640416414164241643416444164541646416474164841649416504165141652416534165441655416564165741658416594166041661416624166341664416654166641667416684166941670416714167241673416744167541676416774167841679416804168141682416834168441685416864168741688416894169041691416924169341694416954169641697416984169941700417014170241703417044170541706417074170841709417104171141712417134171441715417164171741718417194172041721417224172341724417254172641727417284172941730417314173241733417344173541736417374173841739417404174141742417434174441745417464174741748417494175041751417524175341754417554175641757417584175941760417614176241763417644176541766417674176841769417704177141772417734177441775417764177741778417794178041781417824178341784417854178641787417884178941790417914179241793417944179541796417974179841799418004180141802418034180441805418064180741808418094181041811418124181341814418154181641817418184181941820418214182241823418244182541826418274182841829418304183141832418334183441835418364183741838418394184041841418424184341844418454184641847418484184941850418514185241853418544185541856418574185841859418604186141862418634186441865418664186741868418694187041871418724187341874418754187641877418784187941880418814188241883418844188541886418874188841889418904189141892418934189441895418964189741898418994190041901419024190341904419054190641907419084190941910419114191241913419144191541916419174191841919419204192141922419234192441925419264192741928419294193041931419324193341934419354193641937419384193941940419414194241943419444194541946419474194841949419504195141952419534195441955419564195741958419594196041961419624196341964419654196641967419684196941970419714197241973419744197541976419774197841979419804198141982419834198441985419864198741988419894199041991419924199341994419954199641997419984199942000420014200242003420044200542006420074200842009420104201142012420134201442015420164201742018420194202042021420224202342024420254202642027420284202942030420314203242033420344203542036420374203842039420404204142042420434204442045420464204742048420494205042051420524205342054420554205642057420584205942060420614206242063420644206542066420674206842069420704207142072420734207442075420764207742078420794208042081420824208342084420854208642087420884208942090420914209242093420944209542096420974209842099421004210142102421034210442105421064210742108421094211042111421124211342114421154211642117421184211942120421214212242123421244212542126421274212842129421304213142132421334213442135421364213742138421394214042141421424214342144421454214642147421484214942150421514215242153421544215542156421574215842159421604216142162421634216442165421664216742168421694217042171421724217342174421754217642177421784217942180421814218242183421844218542186421874218842189421904219142192421934219442195421964219742198421994220042201422024220342204422054220642207422084220942210422114221242213422144221542216422174221842219422204222142222422234222442225422264222742228422294223042231422324223342234422354223642237422384223942240422414224242243422444224542246422474224842249422504225142252422534225442255422564225742258422594226042261422624226342264422654226642267422684226942270422714227242273422744227542276422774227842279422804228142282422834228442285422864228742288422894229042291422924229342294422954229642297422984229942300423014230242303423044230542306423074230842309423104231142312423134231442315423164231742318423194232042321423224232342324423254232642327423284232942330423314233242333423344233542336423374233842339423404234142342423434234442345423464234742348423494235042351423524235342354423554235642357423584235942360423614236242363423644236542366423674236842369423704237142372423734237442375423764237742378423794238042381423824238342384423854238642387423884238942390423914239242393423944239542396423974239842399424004240142402424034240442405424064240742408424094241042411424124241342414424154241642417424184241942420424214242242423424244242542426424274242842429424304243142432424334243442435424364243742438424394244042441424424244342444424454244642447424484244942450424514245242453424544245542456424574245842459424604246142462424634246442465424664246742468424694247042471424724247342474424754247642477424784247942480424814248242483424844248542486424874248842489424904249142492424934249442495424964249742498424994250042501425024250342504425054250642507425084250942510425114251242513425144251542516425174251842519425204252142522425234252442525425264252742528425294253042531425324253342534425354253642537425384253942540425414254242543425444254542546425474254842549425504255142552425534255442555425564255742558425594256042561425624256342564425654256642567425684256942570425714257242573425744257542576425774257842579425804258142582425834258442585425864258742588425894259042591425924259342594425954259642597425984259942600426014260242603426044260542606426074260842609426104261142612426134261442615426164261742618426194262042621426224262342624426254262642627426284262942630426314263242633426344263542636426374263842639426404264142642426434264442645426464264742648426494265042651426524265342654426554265642657426584265942660426614266242663426644266542666426674266842669426704267142672426734267442675426764267742678426794268042681426824268342684426854268642687426884268942690426914269242693426944269542696426974269842699427004270142702427034270442705427064270742708427094271042711427124271342714427154271642717427184271942720427214272242723427244272542726427274272842729427304273142732427334273442735427364273742738427394274042741427424274342744427454274642747427484274942750427514275242753427544275542756427574275842759427604276142762427634276442765427664276742768427694277042771427724277342774427754277642777427784277942780427814278242783427844278542786427874278842789427904279142792427934279442795427964279742798427994280042801428024280342804428054280642807428084280942810428114281242813428144281542816428174281842819428204282142822428234282442825428264282742828428294283042831428324283342834428354283642837428384283942840428414284242843428444284542846428474284842849428504285142852428534285442855428564285742858428594286042861428624286342864428654286642867428684286942870428714287242873428744287542876428774287842879428804288142882428834288442885428864288742888428894289042891428924289342894428954289642897428984289942900429014290242903429044290542906429074290842909429104291142912429134291442915429164291742918429194292042921429224292342924429254292642927429284292942930429314293242933429344293542936429374293842939429404294142942429434294442945429464294742948429494295042951429524295342954429554295642957429584295942960429614296242963429644296542966429674296842969429704297142972429734297442975429764297742978429794298042981429824298342984429854298642987429884298942990429914299242993429944299542996429974299842999430004300143002430034300443005430064300743008430094301043011430124301343014430154301643017430184301943020430214302243023430244302543026430274302843029430304303143032430334303443035430364303743038430394304043041430424304343044430454304643047430484304943050430514305243053430544305543056430574305843059430604306143062430634306443065430664306743068430694307043071430724307343074430754307643077430784307943080430814308243083430844308543086430874308843089430904309143092430934309443095430964309743098430994310043101431024310343104431054310643107431084310943110431114311243113431144311543116431174311843119431204312143122431234312443125431264312743128431294313043131431324313343134431354313643137431384313943140431414314243143431444314543146431474314843149431504315143152431534315443155431564315743158431594316043161431624316343164431654316643167431684316943170431714317243173431744317543176431774317843179431804318143182431834318443185431864318743188431894319043191431924319343194431954319643197431984319943200432014320243203432044320543206432074320843209432104321143212432134321443215432164321743218432194322043221432224322343224432254322643227432284322943230432314323243233432344323543236432374323843239432404324143242432434324443245432464324743248432494325043251432524325343254432554325643257432584325943260432614326243263432644326543266432674326843269432704327143272432734327443275432764327743278432794328043281432824328343284432854328643287432884328943290432914329243293432944329543296432974329843299433004330143302433034330443305433064330743308433094331043311433124331343314433154331643317433184331943320433214332243323433244332543326433274332843329433304333143332433334333443335433364333743338433394334043341433424334343344433454334643347433484334943350433514335243353433544335543356433574335843359433604336143362433634336443365433664336743368433694337043371433724337343374433754337643377433784337943380433814338243383433844338543386433874338843389433904339143392433934339443395433964339743398433994340043401434024340343404434054340643407434084340943410434114341243413434144341543416434174341843419434204342143422434234342443425434264342743428434294343043431434324343343434434354343643437434384343943440434414344243443434444344543446434474344843449434504345143452434534345443455434564345743458434594346043461434624346343464434654346643467434684346943470434714347243473434744347543476434774347843479434804348143482434834348443485434864348743488434894349043491434924349343494434954349643497434984349943500435014350243503435044350543506435074350843509435104351143512435134351443515435164351743518435194352043521435224352343524435254352643527435284352943530435314353243533435344353543536435374353843539435404354143542435434354443545435464354743548435494355043551435524355343554435554355643557435584355943560435614356243563435644356543566435674356843569435704357143572435734357443575435764357743578435794358043581435824358343584435854358643587435884358943590435914359243593435944359543596435974359843599436004360143602436034360443605436064360743608436094361043611436124361343614436154361643617436184361943620436214362243623436244362543626436274362843629436304363143632436334363443635436364363743638436394364043641436424364343644436454364643647436484364943650436514365243653436544365543656436574365843659436604366143662436634366443665436664366743668436694367043671436724367343674436754367643677436784367943680436814368243683436844368543686436874368843689436904369143692436934369443695436964369743698436994370043701437024370343704437054370643707437084370943710437114371243713437144371543716437174371843719437204372143722437234372443725437264372743728437294373043731437324373343734437354373643737437384373943740437414374243743437444374543746437474374843749437504375143752437534375443755437564375743758437594376043761437624376343764437654376643767437684376943770437714377243773437744377543776437774377843779437804378143782437834378443785437864378743788437894379043791437924379343794437954379643797437984379943800438014380243803438044380543806438074380843809438104381143812438134381443815438164381743818438194382043821438224382343824438254382643827438284382943830438314383243833438344383543836438374383843839438404384143842438434384443845438464384743848438494385043851438524385343854438554385643857438584385943860438614386243863438644386543866438674386843869438704387143872438734387443875438764387743878438794388043881438824388343884438854388643887438884388943890438914389243893438944389543896438974389843899439004390143902439034390443905439064390743908439094391043911439124391343914439154391643917439184391943920439214392243923439244392543926439274392843929439304393143932439334393443935439364393743938439394394043941439424394343944439454394643947439484394943950439514395243953439544395543956439574395843959439604396143962439634396443965439664396743968439694397043971439724397343974439754397643977439784397943980439814398243983439844398543986439874398843989439904399143992439934399443995439964399743998439994400044001440024400344004440054400644007440084400944010440114401244013440144401544016440174401844019440204402144022440234402444025440264402744028440294403044031440324403344034440354403644037440384403944040440414404244043440444404544046440474404844049440504405144052440534405444055440564405744058440594406044061440624406344064440654406644067440684406944070440714407244073440744407544076440774407844079440804408144082440834408444085440864408744088440894409044091440924409344094440954409644097440984409944100441014410244103441044410544106441074410844109441104411144112441134411444115441164411744118441194412044121441224412344124441254412644127441284412944130441314413244133441344413544136441374413844139441404414144142441434414444145441464414744148441494415044151441524415344154441554415644157441584415944160441614416244163441644416544166441674416844169441704417144172441734417444175441764417744178441794418044181441824418344184441854418644187441884418944190441914419244193441944419544196441974419844199442004420144202442034420444205442064420744208442094421044211442124421344214442154421644217442184421944220442214422244223442244422544226442274422844229442304423144232442334423444235442364423744238442394424044241442424424344244442454424644247442484424944250442514425244253442544425544256442574425844259442604426144262442634426444265442664426744268442694427044271442724427344274442754427644277442784427944280442814428244283442844428544286442874428844289442904429144292442934429444295442964429744298442994430044301443024430344304443054430644307443084430944310443114431244313443144431544316443174431844319443204432144322443234432444325443264432744328443294433044331443324433344334443354433644337443384433944340443414434244343443444434544346443474434844349443504435144352443534435444355443564435744358443594436044361443624436344364443654436644367443684436944370443714437244373443744437544376443774437844379443804438144382443834438444385443864438744388443894439044391443924439344394443954439644397443984439944400444014440244403444044440544406444074440844409444104441144412444134441444415444164441744418444194442044421444224442344424444254442644427444284442944430444314443244433444344443544436444374443844439444404444144442444434444444445444464444744448444494445044451444524445344454444554445644457444584445944460444614446244463444644446544466444674446844469444704447144472444734447444475444764447744478444794448044481444824448344484444854448644487444884448944490444914449244493444944449544496444974449844499445004450144502445034450444505445064450744508445094451044511445124451344514445154451644517445184451944520445214452244523445244452544526445274452844529445304453144532445334453444535445364453744538445394454044541445424454344544445454454644547445484454944550445514455244553445544455544556445574455844559445604456144562445634456444565445664456744568445694457044571445724457344574445754457644577445784457944580445814458244583445844458544586445874458844589445904459144592445934459444595445964459744598445994460044601446024460344604446054460644607446084460944610446114461244613446144461544616446174461844619446204462144622446234462444625446264462744628446294463044631446324463344634446354463644637446384463944640446414464244643446444464544646446474464844649446504465144652446534465444655446564465744658446594466044661446624466344664446654466644667446684466944670446714467244673446744467544676446774467844679446804468144682446834468444685446864468744688446894469044691446924469344694446954469644697446984469944700447014470244703447044470544706447074470844709447104471144712447134471444715447164471744718447194472044721447224472344724447254472644727447284472944730447314473244733447344473544736447374473844739447404474144742447434474444745447464474744748447494475044751447524475344754447554475644757447584475944760447614476244763447644476544766447674476844769447704477144772447734477444775447764477744778447794478044781447824478344784447854478644787447884478944790447914479244793447944479544796447974479844799448004480144802448034480444805448064480744808448094481044811448124481344814448154481644817448184481944820448214482244823448244482544826448274482844829448304483144832448334483444835448364483744838448394484044841448424484344844448454484644847448484484944850448514485244853448544485544856448574485844859448604486144862448634486444865448664486744868448694487044871448724487344874448754487644877448784487944880448814488244883448844488544886448874488844889448904489144892448934489444895448964489744898448994490044901449024490344904449054490644907449084490944910449114491244913449144491544916449174491844919449204492144922449234492444925449264492744928449294493044931449324493344934449354493644937449384493944940449414494244943449444494544946449474494844949449504495144952449534495444955449564495744958449594496044961449624496344964449654496644967449684496944970449714497244973449744497544976449774497844979449804498144982449834498444985449864498744988449894499044991449924499344994449954499644997449984499945000450014500245003450044500545006450074500845009450104501145012450134501445015450164501745018450194502045021450224502345024450254502645027450284502945030450314503245033450344503545036450374503845039450404504145042450434504445045450464504745048450494505045051450524505345054450554505645057450584505945060450614506245063450644506545066450674506845069450704507145072450734507445075450764507745078450794508045081450824508345084450854508645087450884508945090450914509245093450944509545096450974509845099451004510145102451034510445105451064510745108451094511045111451124511345114451154511645117451184511945120451214512245123451244512545126451274512845129451304513145132451334513445135451364513745138451394514045141451424514345144451454514645147451484514945150451514515245153451544515545156451574515845159451604516145162451634516445165451664516745168451694517045171451724517345174451754517645177451784517945180451814518245183451844518545186451874518845189451904519145192451934519445195451964519745198451994520045201452024520345204452054520645207452084520945210452114521245213452144521545216452174521845219452204522145222452234522445225452264522745228452294523045231452324523345234452354523645237452384523945240452414524245243452444524545246452474524845249452504525145252452534525445255452564525745258452594526045261452624526345264452654526645267452684526945270452714527245273452744527545276452774527845279452804528145282452834528445285452864528745288452894529045291452924529345294452954529645297452984529945300453014530245303453044530545306453074530845309453104531145312453134531445315453164531745318453194532045321453224532345324453254532645327453284532945330453314533245333453344533545336453374533845339453404534145342453434534445345453464534745348453494535045351453524535345354453554535645357453584535945360453614536245363453644536545366453674536845369453704537145372453734537445375453764537745378453794538045381453824538345384453854538645387453884538945390453914539245393453944539545396453974539845399454004540145402454034540445405454064540745408454094541045411454124541345414454154541645417454184541945420454214542245423454244542545426454274542845429454304543145432454334543445435454364543745438454394544045441454424544345444454454544645447454484544945450454514545245453454544545545456454574545845459454604546145462454634546445465454664546745468454694547045471454724547345474454754547645477454784547945480454814548245483454844548545486454874548845489454904549145492454934549445495454964549745498454994550045501455024550345504455054550645507455084550945510455114551245513455144551545516455174551845519455204552145522455234552445525455264552745528455294553045531455324553345534455354553645537455384553945540455414554245543455444554545546455474554845549455504555145552455534555445555455564555745558455594556045561455624556345564455654556645567455684556945570455714557245573455744557545576455774557845579455804558145582455834558445585455864558745588455894559045591455924559345594455954559645597455984559945600456014560245603456044560545606456074560845609456104561145612456134561445615456164561745618456194562045621456224562345624456254562645627456284562945630456314563245633456344563545636456374563845639456404564145642456434564445645456464564745648456494565045651456524565345654456554565645657456584565945660456614566245663456644566545666456674566845669456704567145672456734567445675456764567745678456794568045681456824568345684456854568645687456884568945690456914569245693456944569545696456974569845699457004570145702457034570445705457064570745708457094571045711457124571345714457154571645717457184571945720457214572245723457244572545726457274572845729457304573145732457334573445735457364573745738457394574045741457424574345744457454574645747457484574945750457514575245753457544575545756457574575845759457604576145762457634576445765457664576745768457694577045771457724577345774457754577645777457784577945780457814578245783457844578545786457874578845789457904579145792457934579445795457964579745798457994580045801458024580345804458054580645807458084580945810458114581245813458144581545816458174581845819458204582145822458234582445825458264582745828458294583045831458324583345834458354583645837458384583945840458414584245843458444584545846458474584845849458504585145852458534585445855458564585745858458594586045861458624586345864458654586645867458684586945870458714587245873458744587545876458774587845879458804588145882458834588445885458864588745888458894589045891458924589345894458954589645897458984589945900459014590245903459044590545906459074590845909459104591145912459134591445915459164591745918459194592045921459224592345924459254592645927459284592945930459314593245933459344593545936459374593845939459404594145942459434594445945459464594745948459494595045951459524595345954459554595645957459584595945960459614596245963459644596545966459674596845969459704597145972459734597445975459764597745978459794598045981459824598345984459854598645987459884598945990459914599245993459944599545996459974599845999460004600146002460034600446005460064600746008460094601046011460124601346014460154601646017460184601946020460214602246023460244602546026460274602846029460304603146032460334603446035460364603746038460394604046041460424604346044460454604646047460484604946050460514605246053460544605546056460574605846059460604606146062460634606446065460664606746068460694607046071460724607346074460754607646077460784607946080460814608246083460844608546086460874608846089460904609146092460934609446095460964609746098460994610046101461024610346104461054610646107461084610946110461114611246113461144611546116461174611846119461204612146122461234612446125461264612746128461294613046131461324613346134461354613646137461384613946140461414614246143461444614546146461474614846149461504615146152461534615446155461564615746158461594616046161461624616346164461654616646167461684616946170461714617246173461744617546176461774617846179461804618146182461834618446185461864618746188461894619046191461924619346194461954619646197461984619946200462014620246203462044620546206462074620846209462104621146212462134621446215462164621746218462194622046221462224622346224462254622646227462284622946230462314623246233462344623546236462374623846239462404624146242462434624446245462464624746248462494625046251462524625346254462554625646257462584625946260462614626246263462644626546266462674626846269462704627146272462734627446275462764627746278462794628046281462824628346284462854628646287462884628946290462914629246293462944629546296462974629846299463004630146302463034630446305463064630746308463094631046311463124631346314463154631646317463184631946320463214632246323463244632546326463274632846329463304633146332463334633446335463364633746338463394634046341463424634346344463454634646347463484634946350463514635246353463544635546356463574635846359463604636146362463634636446365463664636746368463694637046371463724637346374463754637646377463784637946380463814638246383463844638546386463874638846389463904639146392463934639446395463964639746398463994640046401464024640346404464054640646407464084640946410464114641246413464144641546416464174641846419464204642146422464234642446425464264642746428464294643046431464324643346434464354643646437464384643946440464414644246443464444644546446464474644846449464504645146452464534645446455464564645746458464594646046461464624646346464464654646646467464684646946470464714647246473464744647546476464774647846479464804648146482464834648446485464864648746488464894649046491464924649346494464954649646497464984649946500465014650246503465044650546506465074650846509465104651146512465134651446515465164651746518465194652046521465224652346524465254652646527465284652946530465314653246533465344653546536465374653846539465404654146542465434654446545465464654746548465494655046551465524655346554465554655646557465584655946560465614656246563465644656546566465674656846569465704657146572465734657446575465764657746578465794658046581465824658346584465854658646587465884658946590465914659246593465944659546596465974659846599466004660146602466034660446605466064660746608466094661046611466124661346614466154661646617466184661946620466214662246623466244662546626466274662846629466304663146632466334663446635466364663746638466394664046641466424664346644466454664646647466484664946650466514665246653466544665546656466574665846659466604666146662466634666446665466664666746668466694667046671466724667346674466754667646677466784667946680466814668246683466844668546686466874668846689466904669146692466934669446695466964669746698466994670046701467024670346704467054670646707467084670946710467114671246713467144671546716467174671846719467204672146722467234672446725467264672746728467294673046731467324673346734467354673646737467384673946740467414674246743467444674546746467474674846749467504675146752467534675446755467564675746758467594676046761467624676346764467654676646767467684676946770467714677246773467744677546776467774677846779467804678146782467834678446785467864678746788467894679046791467924679346794467954679646797467984679946800468014680246803468044680546806468074680846809468104681146812468134681446815468164681746818468194682046821468224682346824468254682646827468284682946830468314683246833468344683546836468374683846839468404684146842468434684446845468464684746848468494685046851468524685346854468554685646857468584685946860468614686246863468644686546866468674686846869468704687146872468734687446875468764687746878468794688046881468824688346884468854688646887468884688946890468914689246893468944689546896468974689846899469004690146902469034690446905469064690746908469094691046911469124691346914469154691646917469184691946920469214692246923469244692546926469274692846929469304693146932469334693446935469364693746938469394694046941469424694346944469454694646947469484694946950469514695246953469544695546956469574695846959469604696146962469634696446965469664696746968469694697046971469724697346974469754697646977469784697946980469814698246983469844698546986469874698846989469904699146992469934699446995469964699746998469994700047001470024700347004470054700647007470084700947010470114701247013470144701547016470174701847019470204702147022470234702447025470264702747028470294703047031470324703347034470354703647037470384703947040470414704247043470444704547046470474704847049470504705147052470534705447055470564705747058470594706047061470624706347064470654706647067470684706947070470714707247073470744707547076470774707847079470804708147082470834708447085470864708747088470894709047091470924709347094470954709647097470984709947100471014710247103471044710547106471074710847109471104711147112471134711447115471164711747118471194712047121471224712347124471254712647127471284712947130471314713247133471344713547136471374713847139471404714147142471434714447145471464714747148471494715047151471524715347154471554715647157471584715947160471614716247163471644716547166471674716847169471704717147172471734717447175471764717747178471794718047181471824718347184471854718647187471884718947190471914719247193471944719547196471974719847199472004720147202472034720447205472064720747208472094721047211472124721347214472154721647217472184721947220472214722247223472244722547226472274722847229472304723147232472334723447235472364723747238472394724047241472424724347244472454724647247472484724947250472514725247253472544725547256472574725847259472604726147262472634726447265472664726747268472694727047271472724727347274472754727647277472784727947280472814728247283472844728547286472874728847289472904729147292472934729447295472964729747298472994730047301473024730347304473054730647307473084730947310473114731247313473144731547316473174731847319473204732147322473234732447325473264732747328473294733047331473324733347334473354733647337473384733947340473414734247343473444734547346473474734847349473504735147352473534735447355473564735747358473594736047361473624736347364473654736647367473684736947370473714737247373473744737547376473774737847379473804738147382473834738447385473864738747388473894739047391473924739347394473954739647397473984739947400474014740247403474044740547406474074740847409474104741147412474134741447415474164741747418474194742047421474224742347424474254742647427474284742947430474314743247433474344743547436474374743847439474404744147442474434744447445474464744747448474494745047451474524745347454474554745647457474584745947460474614746247463474644746547466474674746847469474704747147472474734747447475474764747747478474794748047481474824748347484474854748647487474884748947490474914749247493474944749547496474974749847499475004750147502475034750447505475064750747508475094751047511475124751347514475154751647517475184751947520475214752247523475244752547526475274752847529475304753147532475334753447535475364753747538475394754047541475424754347544475454754647547475484754947550475514755247553475544755547556475574755847559475604756147562475634756447565475664756747568475694757047571475724757347574475754757647577475784757947580475814758247583475844758547586475874758847589475904759147592475934759447595475964759747598475994760047601476024760347604476054760647607476084760947610476114761247613476144761547616476174761847619476204762147622476234762447625476264762747628476294763047631476324763347634476354763647637476384763947640476414764247643476444764547646476474764847649476504765147652476534765447655476564765747658476594766047661476624766347664476654766647667476684766947670476714767247673476744767547676476774767847679476804768147682476834768447685476864768747688476894769047691476924769347694476954769647697476984769947700477014770247703477044770547706477074770847709477104771147712477134771447715477164771747718477194772047721477224772347724477254772647727477284772947730477314773247733477344773547736477374773847739477404774147742477434774447745477464774747748477494775047751477524775347754477554775647757477584775947760477614776247763477644776547766477674776847769477704777147772477734777447775477764777747778477794778047781477824778347784477854778647787477884778947790477914779247793477944779547796477974779847799478004780147802478034780447805478064780747808478094781047811478124781347814478154781647817478184781947820478214782247823478244782547826478274782847829478304783147832478334783447835478364783747838478394784047841478424784347844478454784647847478484784947850478514785247853478544785547856478574785847859478604786147862478634786447865478664786747868478694787047871478724787347874478754787647877478784787947880478814788247883478844788547886478874788847889478904789147892478934789447895478964789747898478994790047901479024790347904479054790647907479084790947910479114791247913479144791547916479174791847919479204792147922479234792447925479264792747928479294793047931479324793347934479354793647937479384793947940479414794247943479444794547946479474794847949479504795147952479534795447955479564795747958479594796047961479624796347964479654796647967479684796947970479714797247973479744797547976479774797847979479804798147982479834798447985479864798747988479894799047991479924799347994479954799647997479984799948000480014800248003480044800548006480074800848009480104801148012480134801448015480164801748018480194802048021480224802348024480254802648027480284802948030480314803248033480344803548036480374803848039480404804148042480434804448045480464804748048480494805048051480524805348054480554805648057480584805948060480614806248063480644806548066480674806848069480704807148072480734807448075480764807748078480794808048081480824808348084480854808648087480884808948090480914809248093480944809548096480974809848099481004810148102481034810448105481064810748108481094811048111481124811348114481154811648117481184811948120481214812248123481244812548126481274812848129481304813148132481334813448135481364813748138481394814048141481424814348144481454814648147481484814948150481514815248153481544815548156481574815848159481604816148162481634816448165481664816748168481694817048171481724817348174481754817648177481784817948180481814818248183481844818548186481874818848189481904819148192481934819448195481964819748198481994820048201482024820348204482054820648207482084820948210482114821248213482144821548216482174821848219482204822148222482234822448225482264822748228482294823048231482324823348234482354823648237482384823948240482414824248243482444824548246482474824848249482504825148252482534825448255482564825748258482594826048261482624826348264482654826648267482684826948270482714827248273482744827548276482774827848279482804828148282482834828448285482864828748288482894829048291482924829348294482954829648297482984829948300483014830248303483044830548306483074830848309483104831148312483134831448315483164831748318483194832048321483224832348324483254832648327483284832948330483314833248333483344833548336483374833848339483404834148342483434834448345483464834748348483494835048351483524835348354483554835648357483584835948360483614836248363483644836548366483674836848369483704837148372483734837448375483764837748378483794838048381483824838348384483854838648387483884838948390483914839248393483944839548396483974839848399484004840148402484034840448405484064840748408484094841048411484124841348414484154841648417484184841948420484214842248423484244842548426484274842848429484304843148432484334843448435484364843748438484394844048441484424844348444484454844648447484484844948450484514845248453484544845548456484574845848459484604846148462484634846448465484664846748468484694847048471484724847348474484754847648477484784847948480484814848248483484844848548486484874848848489484904849148492484934849448495484964849748498484994850048501485024850348504485054850648507485084850948510485114851248513485144851548516485174851848519485204852148522485234852448525485264852748528485294853048531485324853348534485354853648537485384853948540485414854248543485444854548546485474854848549485504855148552485534855448555485564855748558485594856048561485624856348564485654856648567485684856948570485714857248573485744857548576485774857848579485804858148582485834858448585485864858748588485894859048591485924859348594485954859648597485984859948600486014860248603486044860548606486074860848609486104861148612486134861448615486164861748618486194862048621486224862348624486254862648627486284862948630486314863248633486344863548636486374863848639486404864148642486434864448645486464864748648486494865048651486524865348654486554865648657486584865948660486614866248663486644866548666486674866848669486704867148672486734867448675486764867748678486794868048681486824868348684486854868648687486884868948690486914869248693486944869548696486974869848699487004870148702487034870448705487064870748708487094871048711487124871348714487154871648717487184871948720487214872248723487244872548726487274872848729487304873148732487334873448735487364873748738487394874048741487424874348744487454874648747487484874948750487514875248753487544875548756487574875848759487604876148762487634876448765487664876748768487694877048771487724877348774487754877648777487784877948780487814878248783487844878548786487874878848789487904879148792487934879448795487964879748798487994880048801488024880348804488054880648807488084880948810488114881248813488144881548816488174881848819488204882148822488234882448825488264882748828488294883048831488324883348834488354883648837488384883948840488414884248843488444884548846488474884848849488504885148852488534885448855488564885748858488594886048861488624886348864488654886648867488684886948870488714887248873488744887548876488774887848879488804888148882488834888448885488864888748888488894889048891488924889348894488954889648897488984889948900489014890248903489044890548906489074890848909489104891148912489134891448915489164891748918489194892048921489224892348924489254892648927489284892948930489314893248933489344893548936489374893848939489404894148942489434894448945489464894748948489494895048951489524895348954489554895648957489584895948960489614896248963489644896548966489674896848969489704897148972489734897448975489764897748978489794898048981489824898348984489854898648987489884898948990489914899248993489944899548996489974899848999490004900149002490034900449005490064900749008490094901049011490124901349014490154901649017490184901949020490214902249023490244902549026490274902849029490304903149032490334903449035490364903749038490394904049041490424904349044490454904649047490484904949050490514905249053490544905549056490574905849059490604906149062490634906449065490664906749068490694907049071490724907349074490754907649077490784907949080490814908249083490844908549086490874908849089490904909149092490934909449095490964909749098490994910049101491024910349104491054910649107491084910949110491114911249113491144911549116491174911849119491204912149122491234912449125491264912749128491294913049131491324913349134491354913649137491384913949140491414914249143491444914549146491474914849149491504915149152491534915449155491564915749158491594916049161491624916349164491654916649167491684916949170491714917249173491744917549176491774917849179491804918149182491834918449185491864918749188491894919049191491924919349194491954919649197491984919949200492014920249203492044920549206492074920849209492104921149212492134921449215492164921749218492194922049221492224922349224492254922649227492284922949230492314923249233492344923549236492374923849239492404924149242492434924449245492464924749248492494925049251492524925349254492554925649257492584925949260492614926249263492644926549266492674926849269492704927149272492734927449275492764927749278492794928049281492824928349284492854928649287492884928949290492914929249293492944929549296492974929849299493004930149302493034930449305493064930749308493094931049311493124931349314493154931649317493184931949320493214932249323493244932549326493274932849329493304933149332493334933449335493364933749338493394934049341493424934349344493454934649347493484934949350493514935249353493544935549356493574935849359493604936149362493634936449365493664936749368493694937049371493724937349374493754937649377493784937949380493814938249383493844938549386493874938849389493904939149392493934939449395493964939749398493994940049401494024940349404494054940649407494084940949410494114941249413494144941549416494174941849419494204942149422494234942449425494264942749428494294943049431494324943349434494354943649437494384943949440494414944249443494444944549446494474944849449494504945149452494534945449455494564945749458494594946049461494624946349464494654946649467494684946949470494714947249473494744947549476494774947849479494804948149482494834948449485494864948749488494894949049491494924949349494494954949649497494984949949500495014950249503495044950549506495074950849509495104951149512495134951449515495164951749518495194952049521495224952349524495254952649527495284952949530495314953249533495344953549536495374953849539495404954149542495434954449545495464954749548495494955049551495524955349554495554955649557495584955949560495614956249563495644956549566495674956849569495704957149572495734957449575495764957749578495794958049581495824958349584495854958649587495884958949590495914959249593495944959549596495974959849599496004960149602496034960449605496064960749608496094961049611496124961349614496154961649617496184961949620496214962249623496244962549626496274962849629496304963149632496334963449635496364963749638496394964049641496424964349644496454964649647496484964949650496514965249653496544965549656496574965849659496604966149662496634966449665496664966749668496694967049671496724967349674496754967649677496784967949680496814968249683496844968549686496874968849689496904969149692496934969449695496964969749698496994970049701497024970349704497054970649707497084970949710497114971249713497144971549716497174971849719497204972149722497234972449725497264972749728497294973049731497324973349734497354973649737497384973949740497414974249743497444974549746497474974849749497504975149752497534975449755497564975749758497594976049761497624976349764497654976649767497684976949770497714977249773497744977549776497774977849779497804978149782497834978449785497864978749788497894979049791497924979349794497954979649797497984979949800498014980249803498044980549806498074980849809498104981149812498134981449815498164981749818498194982049821498224982349824498254982649827498284982949830498314983249833498344983549836498374983849839498404984149842498434984449845498464984749848498494985049851498524985349854498554985649857498584985949860498614986249863498644986549866498674986849869498704987149872498734987449875498764987749878498794988049881498824988349884498854988649887498884988949890498914989249893498944989549896498974989849899499004990149902499034990449905499064990749908499094991049911499124991349914499154991649917499184991949920499214992249923499244992549926499274992849929499304993149932499334993449935499364993749938499394994049941499424994349944499454994649947499484994949950499514995249953499544995549956499574995849959499604996149962499634996449965499664996749968499694997049971499724997349974499754997649977499784997949980499814998249983499844998549986499874998849989499904999149992499934999449995499964999749998499995000050001500025000350004500055000650007500085000950010500115001250013500145001550016500175001850019500205002150022500235002450025500265002750028500295003050031500325003350034500355003650037500385003950040500415004250043500445004550046500475004850049500505005150052500535005450055500565005750058500595006050061500625006350064500655006650067500685006950070500715007250073500745007550076500775007850079500805008150082500835008450085500865008750088500895009050091500925009350094500955009650097500985009950100501015010250103501045010550106501075010850109501105011150112501135011450115501165011750118501195012050121501225012350124501255012650127501285012950130501315013250133501345013550136501375013850139501405014150142501435014450145501465014750148501495015050151501525015350154501555015650157501585015950160501615016250163501645016550166501675016850169501705017150172501735017450175501765017750178501795018050181501825018350184501855018650187501885018950190501915019250193501945019550196501975019850199502005020150202502035020450205502065020750208502095021050211502125021350214502155021650217502185021950220502215022250223502245022550226502275022850229502305023150232502335023450235502365023750238502395024050241502425024350244502455024650247502485024950250502515025250253502545025550256502575025850259502605026150262502635026450265502665026750268502695027050271502725027350274502755027650277502785027950280502815028250283502845028550286502875028850289502905029150292502935029450295502965029750298502995030050301503025030350304503055030650307503085030950310503115031250313503145031550316503175031850319503205032150322503235032450325503265032750328503295033050331503325033350334503355033650337503385033950340503415034250343503445034550346503475034850349503505035150352503535035450355503565035750358503595036050361503625036350364503655036650367503685036950370503715037250373503745037550376503775037850379503805038150382503835038450385503865038750388503895039050391503925039350394503955039650397503985039950400504015040250403504045040550406504075040850409504105041150412504135041450415504165041750418504195042050421504225042350424504255042650427504285042950430504315043250433504345043550436504375043850439504405044150442504435044450445504465044750448504495045050451504525045350454504555045650457504585045950460504615046250463504645046550466504675046850469504705047150472504735047450475504765047750478504795048050481504825048350484504855048650487504885048950490504915049250493504945049550496504975049850499505005050150502505035050450505505065050750508505095051050511505125051350514505155051650517505185051950520505215052250523505245052550526505275052850529505305053150532505335053450535505365053750538505395054050541505425054350544505455054650547505485054950550505515055250553505545055550556505575055850559505605056150562505635056450565505665056750568505695057050571505725057350574505755057650577505785057950580505815058250583505845058550586505875058850589505905059150592505935059450595505965059750598505995060050601506025060350604506055060650607506085060950610506115061250613506145061550616506175061850619506205062150622506235062450625506265062750628506295063050631506325063350634506355063650637506385063950640506415064250643506445064550646506475064850649506505065150652506535065450655506565065750658506595066050661506625066350664506655066650667506685066950670506715067250673506745067550676506775067850679506805068150682506835068450685506865068750688506895069050691506925069350694506955069650697506985069950700507015070250703507045070550706507075070850709507105071150712507135071450715507165071750718507195072050721507225072350724507255072650727507285072950730507315073250733507345073550736507375073850739507405074150742507435074450745507465074750748507495075050751507525075350754507555075650757507585075950760507615076250763507645076550766507675076850769507705077150772507735077450775507765077750778507795078050781507825078350784507855078650787507885078950790507915079250793507945079550796507975079850799508005080150802508035080450805508065080750808508095081050811508125081350814508155081650817508185081950820508215082250823508245082550826508275082850829508305083150832508335083450835508365083750838508395084050841508425084350844508455084650847508485084950850508515085250853508545085550856508575085850859508605086150862508635086450865508665086750868508695087050871508725087350874508755087650877508785087950880508815088250883508845088550886508875088850889508905089150892508935089450895508965089750898508995090050901509025090350904509055090650907509085090950910509115091250913509145091550916509175091850919509205092150922509235092450925509265092750928509295093050931509325093350934509355093650937509385093950940509415094250943509445094550946509475094850949509505095150952509535095450955509565095750958509595096050961509625096350964509655096650967509685096950970509715097250973509745097550976509775097850979509805098150982509835098450985509865098750988509895099050991509925099350994509955099650997509985099951000510015100251003510045100551006510075100851009510105101151012510135101451015510165101751018510195102051021510225102351024510255102651027510285102951030510315103251033510345103551036510375103851039510405104151042510435104451045510465104751048510495105051051510525105351054510555105651057510585105951060510615106251063510645106551066510675106851069510705107151072510735107451075510765107751078510795108051081510825108351084510855108651087510885108951090510915109251093510945109551096510975109851099511005110151102511035110451105511065110751108511095111051111511125111351114511155111651117511185111951120511215112251123511245112551126511275112851129511305113151132511335113451135511365113751138511395114051141511425114351144511455114651147511485114951150511515115251153511545115551156511575115851159511605116151162511635116451165511665116751168511695117051171511725117351174511755117651177511785117951180511815118251183511845118551186511875118851189511905119151192511935119451195511965119751198511995120051201512025120351204512055120651207512085120951210512115121251213512145121551216512175121851219512205122151222512235122451225512265122751228512295123051231512325123351234512355123651237512385123951240512415124251243512445124551246512475124851249512505125151252512535125451255512565125751258512595126051261512625126351264512655126651267512685126951270512715127251273512745127551276512775127851279512805128151282512835128451285512865128751288512895129051291512925129351294512955129651297512985129951300513015130251303513045130551306513075130851309513105131151312513135131451315513165131751318513195132051321513225132351324513255132651327513285132951330513315133251333513345133551336513375133851339513405134151342513435134451345513465134751348513495135051351513525135351354513555135651357513585135951360513615136251363513645136551366513675136851369513705137151372513735137451375513765137751378513795138051381513825138351384513855138651387513885138951390513915139251393513945139551396513975139851399514005140151402514035140451405514065140751408514095141051411514125141351414514155141651417514185141951420514215142251423514245142551426514275142851429514305143151432514335143451435514365143751438514395144051441514425144351444514455144651447514485144951450514515145251453514545145551456514575145851459514605146151462514635146451465514665146751468514695147051471514725147351474514755147651477514785147951480514815148251483514845148551486514875148851489514905149151492514935149451495514965149751498514995150051501515025150351504515055150651507515085150951510515115151251513515145151551516515175151851519515205152151522515235152451525515265152751528515295153051531515325153351534515355153651537515385153951540515415154251543515445154551546515475154851549515505155151552515535155451555515565155751558515595156051561515625156351564515655156651567515685156951570515715157251573515745157551576515775157851579515805158151582515835158451585515865158751588515895159051591515925159351594515955159651597515985159951600516015160251603516045160551606516075160851609516105161151612516135161451615516165161751618516195162051621516225162351624516255162651627516285162951630516315163251633516345163551636516375163851639516405164151642516435164451645516465164751648516495165051651516525165351654516555165651657516585165951660516615166251663516645166551666516675166851669516705167151672516735167451675516765167751678516795168051681516825168351684516855168651687516885168951690516915169251693516945169551696516975169851699517005170151702517035170451705517065170751708517095171051711517125171351714517155171651717517185171951720517215172251723517245172551726517275172851729517305173151732517335173451735517365173751738517395174051741517425174351744517455174651747517485174951750517515175251753517545175551756517575175851759517605176151762517635176451765517665176751768517695177051771517725177351774517755177651777517785177951780517815178251783517845178551786517875178851789517905179151792517935179451795517965179751798517995180051801518025180351804518055180651807518085180951810518115181251813518145181551816518175181851819518205182151822518235182451825518265182751828518295183051831518325183351834518355183651837518385183951840518415184251843518445184551846518475184851849518505185151852518535185451855518565185751858518595186051861518625186351864518655186651867518685186951870518715187251873518745187551876518775187851879518805188151882518835188451885518865188751888518895189051891518925189351894518955189651897518985189951900519015190251903519045190551906519075190851909519105191151912519135191451915519165191751918519195192051921519225192351924519255192651927519285192951930519315193251933519345193551936519375193851939519405194151942519435194451945519465194751948519495195051951519525195351954519555195651957519585195951960519615196251963519645196551966519675196851969519705197151972519735197451975519765197751978519795198051981519825198351984519855198651987519885198951990519915199251993519945199551996519975199851999520005200152002520035200452005520065200752008520095201052011520125201352014520155201652017520185201952020520215202252023520245202552026520275202852029520305203152032520335203452035520365203752038520395204052041520425204352044520455204652047520485204952050520515205252053520545205552056520575205852059520605206152062520635206452065520665206752068520695207052071520725207352074520755207652077520785207952080520815208252083520845208552086520875208852089520905209152092520935209452095520965209752098520995210052101521025210352104521055210652107521085210952110521115211252113521145211552116521175211852119521205212152122521235212452125521265212752128521295213052131521325213352134521355213652137521385213952140521415214252143521445214552146521475214852149521505215152152521535215452155521565215752158521595216052161521625216352164521655216652167521685216952170521715217252173521745217552176521775217852179521805218152182521835218452185521865218752188521895219052191521925219352194521955219652197521985219952200522015220252203522045220552206522075220852209522105221152212522135221452215522165221752218522195222052221522225222352224522255222652227522285222952230522315223252233522345223552236522375223852239522405224152242522435224452245522465224752248522495225052251522525225352254522555225652257522585225952260522615226252263522645226552266522675226852269522705227152272522735227452275522765227752278522795228052281522825228352284522855228652287522885228952290522915229252293522945229552296522975229852299523005230152302523035230452305523065230752308523095231052311523125231352314523155231652317523185231952320523215232252323523245232552326523275232852329523305233152332523335233452335523365233752338523395234052341523425234352344523455234652347523485234952350523515235252353523545235552356523575235852359523605236152362523635236452365523665236752368523695237052371523725237352374523755237652377523785237952380523815238252383523845238552386523875238852389523905239152392523935239452395523965239752398523995240052401524025240352404524055240652407524085240952410524115241252413524145241552416524175241852419524205242152422524235242452425524265242752428524295243052431524325243352434524355243652437524385243952440524415244252443524445244552446524475244852449524505245152452524535245452455524565245752458524595246052461524625246352464524655246652467524685246952470524715247252473524745247552476524775247852479524805248152482524835248452485524865248752488524895249052491524925249352494524955249652497524985249952500525015250252503525045250552506525075250852509525105251152512525135251452515525165251752518525195252052521525225252352524525255252652527525285252952530525315253252533525345253552536525375253852539525405254152542525435254452545525465254752548525495255052551525525255352554525555255652557525585255952560525615256252563525645256552566525675256852569525705257152572525735257452575525765257752578525795258052581525825258352584525855258652587525885258952590525915259252593525945259552596525975259852599526005260152602526035260452605526065260752608526095261052611526125261352614526155261652617526185261952620526215262252623526245262552626526275262852629526305263152632526335263452635526365263752638526395264052641526425264352644526455264652647526485264952650526515265252653526545265552656526575265852659526605266152662526635266452665526665266752668526695267052671526725267352674526755267652677526785267952680526815268252683526845268552686526875268852689526905269152692526935269452695526965269752698526995270052701527025270352704527055270652707527085270952710527115271252713527145271552716527175271852719527205272152722527235272452725527265272752728527295273052731527325273352734527355273652737527385273952740527415274252743527445274552746527475274852749527505275152752527535275452755527565275752758527595276052761527625276352764527655276652767527685276952770527715277252773527745277552776527775277852779527805278152782527835278452785527865278752788527895279052791527925279352794527955279652797527985279952800528015280252803528045280552806528075280852809528105281152812528135281452815528165281752818528195282052821528225282352824528255282652827528285282952830528315283252833528345283552836528375283852839528405284152842528435284452845528465284752848528495285052851528525285352854528555285652857528585285952860528615286252863528645286552866528675286852869528705287152872528735287452875528765287752878528795288052881528825288352884528855288652887528885288952890528915289252893528945289552896528975289852899529005290152902529035290452905529065290752908529095291052911529125291352914529155291652917529185291952920529215292252923529245292552926529275292852929529305293152932529335293452935529365293752938529395294052941529425294352944529455294652947529485294952950529515295252953529545295552956529575295852959529605296152962529635296452965529665296752968529695297052971529725297352974529755297652977529785297952980529815298252983529845298552986529875298852989529905299152992529935299452995529965299752998529995300053001530025300353004530055300653007530085300953010530115301253013530145301553016530175301853019530205302153022530235302453025530265302753028530295303053031530325303353034530355303653037530385303953040530415304253043530445304553046530475304853049530505305153052530535305453055530565305753058530595306053061530625306353064530655306653067530685306953070530715307253073530745307553076530775307853079530805308153082530835308453085530865308753088530895309053091530925309353094530955309653097530985309953100531015310253103531045310553106531075310853109531105311153112531135311453115531165311753118531195312053121531225312353124531255312653127531285312953130531315313253133531345313553136531375313853139531405314153142531435314453145531465314753148531495315053151531525315353154531555315653157531585315953160531615316253163531645316553166531675316853169531705317153172531735317453175531765317753178531795318053181531825318353184531855318653187531885318953190531915319253193531945319553196531975319853199532005320153202532035320453205532065320753208532095321053211532125321353214532155321653217532185321953220532215322253223532245322553226532275322853229532305323153232532335323453235532365323753238532395324053241532425324353244532455324653247532485324953250532515325253253532545325553256532575325853259532605326153262532635326453265532665326753268532695327053271532725327353274532755327653277532785327953280532815328253283532845328553286532875328853289532905329153292532935329453295532965329753298532995330053301533025330353304533055330653307533085330953310533115331253313533145331553316533175331853319533205332153322533235332453325533265332753328533295333053331533325333353334533355333653337533385333953340533415334253343533445334553346533475334853349533505335153352533535335453355533565335753358533595336053361533625336353364533655336653367533685336953370533715337253373533745337553376533775337853379533805338153382533835338453385533865338753388533895339053391533925339353394533955339653397533985339953400534015340253403534045340553406534075340853409534105341153412534135341453415534165341753418534195342053421534225342353424534255342653427534285342953430534315343253433534345343553436534375343853439534405344153442534435344453445534465344753448534495345053451534525345353454534555345653457534585345953460534615346253463534645346553466534675346853469534705347153472534735347453475534765347753478534795348053481534825348353484534855348653487534885348953490534915349253493534945349553496534975349853499535005350153502535035350453505535065350753508535095351053511535125351353514535155351653517535185351953520535215352253523535245352553526535275352853529535305353153532535335353453535535365353753538535395354053541535425354353544535455354653547535485354953550535515355253553535545355553556535575355853559535605356153562535635356453565535665356753568535695357053571535725357353574535755357653577535785357953580535815358253583535845358553586535875358853589535905359153592535935359453595535965359753598535995360053601536025360353604536055360653607536085360953610536115361253613536145361553616536175361853619536205362153622536235362453625536265362753628536295363053631536325363353634536355363653637536385363953640536415364253643536445364553646536475364853649536505365153652536535365453655536565365753658536595366053661536625366353664536655366653667536685366953670536715367253673536745367553676536775367853679536805368153682536835368453685536865368753688536895369053691536925369353694536955369653697536985369953700537015370253703537045370553706537075370853709537105371153712537135371453715537165371753718537195372053721537225372353724537255372653727537285372953730537315373253733537345373553736537375373853739537405374153742537435374453745537465374753748537495375053751537525375353754537555375653757537585375953760537615376253763537645376553766537675376853769537705377153772537735377453775537765377753778537795378053781537825378353784537855378653787537885378953790537915379253793537945379553796537975379853799538005380153802538035380453805538065380753808538095381053811538125381353814538155381653817538185381953820538215382253823538245382553826538275382853829538305383153832538335383453835538365383753838538395384053841538425384353844538455384653847538485384953850538515385253853538545385553856538575385853859538605386153862538635386453865538665386753868538695387053871538725387353874538755387653877538785387953880538815388253883538845388553886538875388853889538905389153892538935389453895538965389753898538995390053901539025390353904539055390653907539085390953910539115391253913539145391553916539175391853919539205392153922539235392453925539265392753928539295393053931539325393353934539355393653937539385393953940539415394253943539445394553946539475394853949539505395153952539535395453955539565395753958539595396053961539625396353964539655396653967539685396953970539715397253973539745397553976539775397853979539805398153982539835398453985539865398753988539895399053991539925399353994539955399653997539985399954000540015400254003540045400554006540075400854009540105401154012540135401454015540165401754018540195402054021540225402354024540255402654027540285402954030540315403254033540345403554036540375403854039540405404154042540435404454045540465404754048540495405054051540525405354054540555405654057540585405954060540615406254063540645406554066540675406854069540705407154072540735407454075540765407754078540795408054081540825408354084540855408654087540885408954090540915409254093540945409554096540975409854099541005410154102541035410454105541065410754108541095411054111541125411354114541155411654117541185411954120541215412254123541245412554126541275412854129541305413154132541335413454135541365413754138541395414054141541425414354144541455414654147541485414954150541515415254153541545415554156541575415854159541605416154162541635416454165541665416754168541695417054171541725417354174541755417654177541785417954180541815418254183541845418554186541875418854189541905419154192541935419454195541965419754198541995420054201542025420354204542055420654207542085420954210542115421254213542145421554216542175421854219542205422154222542235422454225542265422754228542295423054231542325423354234542355423654237542385423954240542415424254243542445424554246542475424854249542505425154252542535425454255542565425754258542595426054261542625426354264542655426654267542685426954270542715427254273542745427554276542775427854279542805428154282542835428454285542865428754288542895429054291542925429354294542955429654297542985429954300543015430254303543045430554306543075430854309543105431154312543135431454315543165431754318543195432054321543225432354324543255432654327543285432954330543315433254333543345433554336543375433854339543405434154342543435434454345543465434754348543495435054351543525435354354543555435654357543585435954360543615436254363543645436554366543675436854369543705437154372543735437454375543765437754378543795438054381543825438354384543855438654387543885438954390543915439254393543945439554396543975439854399544005440154402544035440454405544065440754408544095441054411544125441354414544155441654417544185441954420544215442254423544245442554426544275442854429544305443154432544335443454435544365443754438544395444054441544425444354444544455444654447544485444954450544515445254453544545445554456544575445854459544605446154462544635446454465544665446754468544695447054471544725447354474544755447654477544785447954480544815448254483544845448554486544875448854489544905449154492544935449454495544965449754498544995450054501545025450354504545055450654507545085450954510545115451254513545145451554516545175451854519545205452154522545235452454525545265452754528545295453054531545325453354534545355453654537545385453954540545415454254543545445454554546545475454854549545505455154552545535455454555545565455754558545595456054561545625456354564545655456654567545685456954570545715457254573545745457554576545775457854579545805458154582545835458454585545865458754588545895459054591545925459354594545955459654597545985459954600546015460254603546045460554606546075460854609546105461154612546135461454615546165461754618546195462054621546225462354624546255462654627546285462954630546315463254633546345463554636546375463854639546405464154642546435464454645546465464754648546495465054651546525465354654546555465654657546585465954660546615466254663546645466554666546675466854669546705467154672546735467454675546765467754678546795468054681546825468354684546855468654687546885468954690546915469254693546945469554696546975469854699547005470154702547035470454705547065470754708547095471054711547125471354714547155471654717547185471954720547215472254723547245472554726547275472854729547305473154732547335473454735547365473754738547395474054741547425474354744547455474654747547485474954750547515475254753547545475554756547575475854759547605476154762547635476454765547665476754768547695477054771547725477354774547755477654777547785477954780547815478254783547845478554786547875478854789547905479154792547935479454795547965479754798547995480054801548025480354804548055480654807548085480954810548115481254813548145481554816548175481854819548205482154822548235482454825548265482754828548295483054831548325483354834548355483654837548385483954840548415484254843548445484554846548475484854849548505485154852548535485454855548565485754858548595486054861548625486354864548655486654867548685486954870548715487254873548745487554876548775487854879548805488154882548835488454885548865488754888548895489054891548925489354894548955489654897548985489954900549015490254903549045490554906549075490854909549105491154912549135491454915549165491754918549195492054921549225492354924549255492654927549285492954930549315493254933549345493554936549375493854939549405494154942549435494454945549465494754948549495495054951549525495354954549555495654957549585495954960549615496254963549645496554966549675496854969549705497154972549735497454975549765497754978549795498054981549825498354984549855498654987549885498954990549915499254993549945499554996549975499854999550005500155002550035500455005550065500755008550095501055011550125501355014550155501655017550185501955020550215502255023550245502555026550275502855029550305503155032550335503455035550365503755038550395504055041550425504355044550455504655047550485504955050550515505255053550545505555056550575505855059550605506155062550635506455065550665506755068550695507055071550725507355074550755507655077550785507955080550815508255083550845508555086550875508855089550905509155092550935509455095550965509755098550995510055101551025510355104551055510655107551085510955110551115511255113551145511555116551175511855119551205512155122551235512455125551265512755128551295513055131551325513355134551355513655137551385513955140551415514255143551445514555146551475514855149551505515155152551535515455155551565515755158551595516055161551625516355164551655516655167551685516955170551715517255173551745517555176551775517855179551805518155182551835518455185551865518755188551895519055191551925519355194551955519655197551985519955200552015520255203552045520555206552075520855209552105521155212552135521455215552165521755218552195522055221552225522355224552255522655227552285522955230552315523255233552345523555236552375523855239552405524155242552435524455245552465524755248552495525055251552525525355254552555525655257552585525955260552615526255263552645526555266552675526855269552705527155272552735527455275552765527755278552795528055281552825528355284552855528655287552885528955290552915529255293552945529555296552975529855299553005530155302553035530455305553065530755308553095531055311553125531355314553155531655317553185531955320553215532255323553245532555326553275532855329553305533155332553335533455335553365533755338553395534055341553425534355344553455534655347553485534955350553515535255353553545535555356553575535855359553605536155362553635536455365553665536755368553695537055371553725537355374553755537655377553785537955380553815538255383553845538555386553875538855389553905539155392553935539455395553965539755398553995540055401554025540355404554055540655407554085540955410554115541255413554145541555416554175541855419554205542155422554235542455425554265542755428554295543055431554325543355434554355543655437554385543955440554415544255443554445544555446554475544855449554505545155452554535545455455554565545755458554595546055461554625546355464554655546655467554685546955470554715547255473554745547555476554775547855479554805548155482554835548455485554865548755488554895549055491554925549355494554955549655497554985549955500555015550255503555045550555506555075550855509555105551155512555135551455515555165551755518555195552055521555225552355524555255552655527555285552955530555315553255533555345553555536555375553855539555405554155542555435554455545555465554755548555495555055551555525555355554555555555655557555585555955560555615556255563555645556555566555675556855569555705557155572555735557455575555765557755578555795558055581555825558355584555855558655587555885558955590555915559255593555945559555596555975559855599556005560155602556035560455605556065560755608556095561055611556125561355614556155561655617556185561955620556215562255623556245562555626556275562855629556305563155632556335563455635556365563755638556395564055641556425564355644556455564655647556485564955650556515565255653556545565555656556575565855659556605566155662556635566455665556665566755668556695567055671556725567355674556755567655677556785567955680556815568255683556845568555686556875568855689556905569155692556935569455695556965569755698556995570055701557025570355704557055570655707557085570955710557115571255713557145571555716557175571855719557205572155722557235572455725557265572755728557295573055731557325573355734557355573655737557385573955740557415574255743557445574555746557475574855749557505575155752557535575455755557565575755758557595576055761557625576355764557655576655767557685576955770557715577255773557745577555776557775577855779557805578155782557835578455785557865578755788557895579055791557925579355794557955579655797557985579955800558015580255803558045580555806558075580855809558105581155812558135581455815558165581755818558195582055821558225582355824558255582655827558285582955830558315583255833558345583555836558375583855839558405584155842558435584455845558465584755848558495585055851558525585355854558555585655857558585585955860558615586255863558645586555866558675586855869558705587155872558735587455875558765587755878558795588055881558825588355884558855588655887558885588955890558915589255893558945589555896558975589855899559005590155902559035590455905559065590755908559095591055911559125591355914559155591655917559185591955920559215592255923559245592555926559275592855929559305593155932559335593455935559365593755938559395594055941559425594355944559455594655947559485594955950559515595255953559545595555956559575595855959559605596155962559635596455965559665596755968559695597055971559725597355974559755597655977559785597955980559815598255983559845598555986559875598855989559905599155992559935599455995559965599755998559995600056001560025600356004560055600656007560085600956010560115601256013560145601556016560175601856019560205602156022560235602456025560265602756028560295603056031560325603356034560355603656037560385603956040560415604256043560445604556046560475604856049560505605156052560535605456055560565605756058560595606056061560625606356064560655606656067560685606956070560715607256073560745607556076560775607856079560805608156082560835608456085560865608756088560895609056091560925609356094560955609656097560985609956100561015610256103561045610556106561075610856109561105611156112561135611456115561165611756118561195612056121561225612356124561255612656127561285612956130561315613256133561345613556136561375613856139561405614156142561435614456145561465614756148561495615056151561525615356154561555615656157561585615956160561615616256163561645616556166561675616856169561705617156172561735617456175561765617756178561795618056181561825618356184561855618656187561885618956190561915619256193561945619556196561975619856199562005620156202562035620456205562065620756208562095621056211562125621356214562155621656217562185621956220562215622256223562245622556226562275622856229562305623156232562335623456235562365623756238562395624056241562425624356244562455624656247562485624956250562515625256253562545625556256562575625856259562605626156262562635626456265562665626756268562695627056271562725627356274562755627656277562785627956280562815628256283562845628556286562875628856289562905629156292562935629456295562965629756298562995630056301563025630356304563055630656307563085630956310563115631256313563145631556316563175631856319563205632156322563235632456325563265632756328563295633056331563325633356334563355633656337563385633956340563415634256343563445634556346563475634856349563505635156352563535635456355563565635756358563595636056361563625636356364563655636656367563685636956370563715637256373563745637556376563775637856379563805638156382563835638456385563865638756388563895639056391563925639356394563955639656397563985639956400564015640256403564045640556406564075640856409564105641156412564135641456415564165641756418564195642056421564225642356424564255642656427564285642956430564315643256433564345643556436564375643856439564405644156442564435644456445564465644756448564495645056451564525645356454564555645656457564585645956460564615646256463564645646556466564675646856469564705647156472564735647456475564765647756478564795648056481564825648356484564855648656487564885648956490564915649256493564945649556496564975649856499565005650156502565035650456505565065650756508565095651056511565125651356514565155651656517565185651956520565215652256523565245652556526565275652856529565305653156532565335653456535565365653756538565395654056541565425654356544565455654656547565485654956550565515655256553565545655556556565575655856559565605656156562565635656456565565665656756568565695657056571565725657356574565755657656577565785657956580565815658256583565845658556586565875658856589565905659156592565935659456595565965659756598565995660056601566025660356604566055660656607566085660956610566115661256613566145661556616566175661856619566205662156622566235662456625566265662756628566295663056631566325663356634566355663656637566385663956640566415664256643566445664556646566475664856649566505665156652566535665456655566565665756658566595666056661566625666356664566655666656667566685666956670566715667256673566745667556676566775667856679566805668156682566835668456685566865668756688566895669056691566925669356694566955669656697566985669956700567015670256703567045670556706567075670856709567105671156712567135671456715567165671756718567195672056721567225672356724567255672656727567285672956730567315673256733567345673556736567375673856739567405674156742567435674456745567465674756748567495675056751567525675356754567555675656757567585675956760567615676256763567645676556766567675676856769567705677156772567735677456775567765677756778567795678056781567825678356784567855678656787567885678956790567915679256793567945679556796567975679856799568005680156802568035680456805568065680756808568095681056811568125681356814568155681656817568185681956820568215682256823568245682556826568275682856829568305683156832568335683456835568365683756838568395684056841568425684356844568455684656847568485684956850568515685256853568545685556856568575685856859568605686156862568635686456865568665686756868568695687056871568725687356874568755687656877568785687956880568815688256883568845688556886568875688856889568905689156892568935689456895568965689756898568995690056901569025690356904569055690656907569085690956910569115691256913569145691556916569175691856919569205692156922569235692456925569265692756928569295693056931569325693356934569355693656937569385693956940569415694256943569445694556946569475694856949569505695156952569535695456955569565695756958569595696056961569625696356964569655696656967569685696956970569715697256973569745697556976569775697856979569805698156982569835698456985569865698756988569895699056991569925699356994569955699656997569985699957000570015700257003570045700557006570075700857009570105701157012570135701457015570165701757018570195702057021570225702357024570255702657027570285702957030570315703257033570345703557036570375703857039570405704157042570435704457045570465704757048570495705057051570525705357054570555705657057570585705957060570615706257063570645706557066570675706857069570705707157072570735707457075570765707757078570795708057081570825708357084570855708657087570885708957090570915709257093570945709557096570975709857099571005710157102571035710457105571065710757108571095711057111571125711357114571155711657117571185711957120571215712257123571245712557126571275712857129571305713157132571335713457135571365713757138571395714057141571425714357144571455714657147571485714957150571515715257153571545715557156571575715857159571605716157162571635716457165571665716757168571695717057171571725717357174571755717657177571785717957180571815718257183571845718557186571875718857189571905719157192571935719457195571965719757198571995720057201572025720357204572055720657207572085720957210572115721257213572145721557216572175721857219572205722157222572235722457225572265722757228572295723057231572325723357234572355723657237572385723957240572415724257243572445724557246572475724857249572505725157252572535725457255572565725757258572595726057261572625726357264572655726657267572685726957270572715727257273572745727557276572775727857279572805728157282572835728457285572865728757288572895729057291572925729357294572955729657297572985729957300573015730257303573045730557306573075730857309573105731157312573135731457315573165731757318573195732057321573225732357324573255732657327573285732957330573315733257333573345733557336573375733857339573405734157342573435734457345573465734757348573495735057351573525735357354573555735657357573585735957360573615736257363573645736557366573675736857369573705737157372573735737457375573765737757378573795738057381573825738357384573855738657387573885738957390573915739257393573945739557396573975739857399574005740157402574035740457405574065740757408574095741057411574125741357414574155741657417574185741957420574215742257423574245742557426574275742857429574305743157432574335743457435574365743757438574395744057441574425744357444574455744657447574485744957450574515745257453574545745557456574575745857459574605746157462574635746457465574665746757468574695747057471574725747357474574755747657477574785747957480574815748257483574845748557486574875748857489574905749157492574935749457495574965749757498574995750057501575025750357504575055750657507575085750957510575115751257513575145751557516575175751857519575205752157522575235752457525575265752757528575295753057531575325753357534575355753657537575385753957540575415754257543575445754557546575475754857549575505755157552575535755457555575565755757558575595756057561575625756357564575655756657567575685756957570575715757257573575745757557576575775757857579575805758157582575835758457585575865758757588575895759057591575925759357594575955759657597575985759957600576015760257603576045760557606576075760857609576105761157612576135761457615576165761757618576195762057621576225762357624576255762657627576285762957630576315763257633576345763557636576375763857639576405764157642576435764457645576465764757648576495765057651576525765357654576555765657657576585765957660576615766257663576645766557666576675766857669576705767157672576735767457675576765767757678576795768057681576825768357684576855768657687576885768957690576915769257693576945769557696576975769857699577005770157702577035770457705577065770757708577095771057711577125771357714577155771657717577185771957720577215772257723577245772557726577275772857729577305773157732577335773457735577365773757738577395774057741577425774357744577455774657747577485774957750577515775257753577545775557756577575775857759577605776157762577635776457765577665776757768577695777057771577725777357774577755777657777577785777957780577815778257783577845778557786577875778857789577905779157792577935779457795577965779757798577995780057801578025780357804578055780657807578085780957810578115781257813578145781557816578175781857819578205782157822578235782457825578265782757828578295783057831578325783357834578355783657837578385783957840578415784257843578445784557846578475784857849578505785157852578535785457855578565785757858578595786057861578625786357864578655786657867578685786957870578715787257873578745787557876578775787857879578805788157882578835788457885578865788757888578895789057891578925789357894578955789657897578985789957900579015790257903579045790557906579075790857909579105791157912579135791457915579165791757918579195792057921579225792357924579255792657927579285792957930579315793257933579345793557936579375793857939579405794157942579435794457945579465794757948579495795057951579525795357954579555795657957579585795957960579615796257963579645796557966579675796857969579705797157972579735797457975579765797757978579795798057981579825798357984579855798657987579885798957990579915799257993579945799557996579975799857999580005800158002580035800458005580065800758008580095801058011580125801358014580155801658017580185801958020580215802258023580245802558026580275802858029580305803158032580335803458035580365803758038580395804058041580425804358044580455804658047580485804958050580515805258053580545805558056580575805858059580605806158062580635806458065580665806758068580695807058071580725807358074580755807658077580785807958080580815808258083580845808558086580875808858089580905809158092580935809458095580965809758098580995810058101581025810358104581055810658107581085810958110581115811258113581145811558116581175811858119581205812158122581235812458125581265812758128581295813058131581325813358134581355813658137581385813958140581415814258143581445814558146581475814858149581505815158152581535815458155581565815758158581595816058161581625816358164581655816658167581685816958170581715817258173581745817558176581775817858179581805818158182581835818458185581865818758188581895819058191581925819358194581955819658197581985819958200582015820258203582045820558206582075820858209582105821158212582135821458215582165821758218582195822058221582225822358224582255822658227582285822958230582315823258233582345823558236582375823858239582405824158242582435824458245582465824758248582495825058251582525825358254582555825658257582585825958260582615826258263582645826558266582675826858269582705827158272582735827458275582765827758278582795828058281582825828358284582855828658287582885828958290582915829258293582945829558296582975829858299583005830158302583035830458305583065830758308583095831058311583125831358314583155831658317583185831958320583215832258323583245832558326583275832858329583305833158332583335833458335583365833758338583395834058341583425834358344583455834658347583485834958350583515835258353583545835558356583575835858359583605836158362583635836458365583665836758368583695837058371583725837358374583755837658377583785837958380583815838258383583845838558386583875838858389583905839158392583935839458395583965839758398583995840058401584025840358404584055840658407584085840958410584115841258413584145841558416584175841858419584205842158422584235842458425584265842758428584295843058431584325843358434584355843658437584385843958440584415844258443584445844558446584475844858449584505845158452584535845458455584565845758458584595846058461584625846358464584655846658467584685846958470584715847258473584745847558476584775847858479584805848158482584835848458485584865848758488584895849058491584925849358494584955849658497584985849958500585015850258503585045850558506585075850858509585105851158512585135851458515585165851758518585195852058521585225852358524585255852658527585285852958530585315853258533585345853558536585375853858539585405854158542585435854458545585465854758548585495855058551585525855358554585555855658557585585855958560585615856258563585645856558566585675856858569585705857158572585735857458575585765857758578585795858058581585825858358584585855858658587585885858958590585915859258593585945859558596585975859858599586005860158602586035860458605586065860758608586095861058611586125861358614586155861658617586185861958620586215862258623586245862558626586275862858629586305863158632586335863458635586365863758638586395864058641586425864358644586455864658647586485864958650586515865258653586545865558656586575865858659586605866158662586635866458665586665866758668586695867058671586725867358674586755867658677586785867958680586815868258683586845868558686586875868858689586905869158692586935869458695586965869758698586995870058701587025870358704587055870658707587085870958710587115871258713587145871558716587175871858719587205872158722587235872458725587265872758728587295873058731587325873358734587355873658737587385873958740587415874258743587445874558746587475874858749587505875158752587535875458755587565875758758587595876058761587625876358764587655876658767587685876958770587715877258773587745877558776587775877858779587805878158782587835878458785587865878758788587895879058791587925879358794587955879658797587985879958800588015880258803588045880558806588075880858809588105881158812588135881458815588165881758818588195882058821588225882358824588255882658827588285882958830588315883258833588345883558836588375883858839588405884158842588435884458845588465884758848588495885058851588525885358854588555885658857588585885958860588615886258863588645886558866588675886858869588705887158872588735887458875588765887758878588795888058881588825888358884588855888658887588885888958890588915889258893588945889558896588975889858899589005890158902589035890458905589065890758908589095891058911589125891358914589155891658917589185891958920589215892258923589245892558926589275892858929589305893158932589335893458935589365893758938589395894058941589425894358944589455894658947589485894958950589515895258953589545895558956589575895858959589605896158962589635896458965589665896758968589695897058971589725897358974589755897658977589785897958980589815898258983589845898558986589875898858989589905899158992589935899458995589965899758998589995900059001590025900359004590055900659007590085900959010590115901259013590145901559016590175901859019590205902159022590235902459025590265902759028590295903059031590325903359034590355903659037590385903959040590415904259043590445904559046590475904859049590505905159052590535905459055590565905759058590595906059061590625906359064590655906659067590685906959070590715907259073590745907559076590775907859079590805908159082590835908459085590865908759088590895909059091590925909359094590955909659097590985909959100591015910259103591045910559106591075910859109591105911159112591135911459115591165911759118591195912059121591225912359124591255912659127591285912959130591315913259133591345913559136591375913859139591405914159142591435914459145591465914759148591495915059151591525915359154591555915659157591585915959160591615916259163591645916559166591675916859169591705917159172591735917459175591765917759178591795918059181591825918359184591855918659187591885918959190591915919259193591945919559196591975919859199592005920159202592035920459205592065920759208592095921059211592125921359214592155921659217592185921959220592215922259223592245922559226592275922859229592305923159232592335923459235592365923759238592395924059241592425924359244592455924659247592485924959250592515925259253592545925559256592575925859259592605926159262592635926459265592665926759268592695927059271592725927359274592755927659277592785927959280592815928259283592845928559286592875928859289592905929159292592935929459295592965929759298592995930059301593025930359304593055930659307593085930959310593115931259313593145931559316593175931859319593205932159322593235932459325593265932759328593295933059331593325933359334593355933659337593385933959340593415934259343593445934559346593475934859349593505935159352593535935459355593565935759358593595936059361593625936359364593655936659367593685936959370593715937259373593745937559376593775937859379593805938159382593835938459385593865938759388593895939059391593925939359394593955939659397593985939959400594015940259403594045940559406594075940859409594105941159412594135941459415594165941759418594195942059421594225942359424594255942659427594285942959430594315943259433594345943559436594375943859439594405944159442594435944459445594465944759448594495945059451594525945359454594555945659457594585945959460594615946259463594645946559466594675946859469594705947159472594735947459475594765947759478594795948059481594825948359484594855948659487594885948959490594915949259493594945949559496594975949859499595005950159502595035950459505595065950759508595095951059511595125951359514595155951659517595185951959520595215952259523595245952559526595275952859529595305953159532595335953459535595365953759538595395954059541595425954359544595455954659547595485954959550595515955259553595545955559556595575955859559595605956159562595635956459565595665956759568595695957059571595725957359574595755957659577595785957959580595815958259583595845958559586595875958859589595905959159592595935959459595595965959759598595995960059601596025960359604596055960659607596085960959610596115961259613596145961559616596175961859619596205962159622596235962459625596265962759628596295963059631596325963359634596355963659637596385963959640596415964259643596445964559646596475964859649596505965159652596535965459655596565965759658596595966059661596625966359664596655966659667596685966959670596715967259673596745967559676596775967859679596805968159682596835968459685596865968759688596895969059691596925969359694596955969659697596985969959700597015970259703597045970559706597075970859709597105971159712597135971459715597165971759718597195972059721597225972359724597255972659727597285972959730597315973259733597345973559736597375973859739597405974159742597435974459745597465974759748597495975059751597525975359754597555975659757597585975959760597615976259763597645976559766597675976859769597705977159772597735977459775597765977759778597795978059781597825978359784597855978659787597885978959790597915979259793597945979559796597975979859799598005980159802598035980459805598065980759808598095981059811598125981359814598155981659817598185981959820598215982259823598245982559826598275982859829598305983159832598335983459835598365983759838598395984059841598425984359844598455984659847598485984959850598515985259853598545985559856598575985859859598605986159862598635986459865598665986759868598695987059871598725987359874598755987659877598785987959880598815988259883598845988559886598875988859889598905989159892598935989459895598965989759898598995990059901599025990359904599055990659907599085990959910599115991259913599145991559916599175991859919599205992159922599235992459925599265992759928599295993059931599325993359934599355993659937599385993959940599415994259943599445994559946599475994859949599505995159952599535995459955599565995759958599595996059961599625996359964599655996659967599685996959970599715997259973599745997559976599775997859979599805998159982599835998459985599865998759988599895999059991599925999359994599955999659997599985999960000600016000260003600046000560006600076000860009600106001160012600136001460015600166001760018600196002060021600226002360024600256002660027600286002960030600316003260033600346003560036600376003860039600406004160042600436004460045600466004760048600496005060051600526005360054600556005660057600586005960060600616006260063600646006560066600676006860069600706007160072600736007460075600766007760078600796008060081600826008360084600856008660087600886008960090600916009260093600946009560096600976009860099601006010160102601036010460105601066010760108601096011060111601126011360114601156011660117601186011960120601216012260123601246012560126601276012860129601306013160132601336013460135601366013760138601396014060141601426014360144601456014660147601486014960150601516015260153601546015560156601576015860159601606016160162601636016460165601666016760168601696017060171601726017360174601756017660177601786017960180601816018260183601846018560186601876018860189601906019160192601936019460195601966019760198601996020060201602026020360204602056020660207602086020960210602116021260213602146021560216602176021860219602206022160222602236022460225602266022760228602296023060231602326023360234602356023660237602386023960240602416024260243602446024560246602476024860249602506025160252602536025460255602566025760258602596026060261602626026360264602656026660267602686026960270602716027260273602746027560276602776027860279602806028160282602836028460285602866028760288602896029060291602926029360294602956029660297602986029960300603016030260303603046030560306603076030860309603106031160312603136031460315603166031760318603196032060321603226032360324603256032660327603286032960330603316033260333603346033560336603376033860339603406034160342603436034460345603466034760348603496035060351603526035360354603556035660357603586035960360603616036260363603646036560366603676036860369603706037160372603736037460375603766037760378603796038060381603826038360384603856038660387603886038960390603916039260393603946039560396603976039860399604006040160402604036040460405604066040760408604096041060411604126041360414604156041660417604186041960420604216042260423604246042560426604276042860429604306043160432604336043460435604366043760438604396044060441604426044360444604456044660447604486044960450604516045260453604546045560456604576045860459604606046160462604636046460465604666046760468604696047060471604726047360474604756047660477604786047960480604816048260483604846048560486604876048860489604906049160492604936049460495604966049760498604996050060501605026050360504605056050660507605086050960510605116051260513605146051560516605176051860519605206052160522605236052460525605266052760528605296053060531605326053360534605356053660537605386053960540605416054260543605446054560546605476054860549605506055160552605536055460555605566055760558605596056060561605626056360564605656056660567605686056960570605716057260573605746057560576605776057860579605806058160582605836058460585605866058760588605896059060591605926059360594605956059660597605986059960600606016060260603606046060560606606076060860609606106061160612606136061460615606166061760618606196062060621606226062360624606256062660627606286062960630606316063260633606346063560636606376063860639606406064160642606436064460645606466064760648606496065060651606526065360654606556065660657606586065960660606616066260663606646066560666606676066860669606706067160672606736067460675606766067760678606796068060681606826068360684606856068660687606886068960690606916069260693606946069560696606976069860699607006070160702607036070460705607066070760708607096071060711607126071360714607156071660717607186071960720607216072260723607246072560726607276072860729607306073160732607336073460735607366073760738607396074060741607426074360744607456074660747607486074960750607516075260753607546075560756607576075860759607606076160762607636076460765607666076760768607696077060771607726077360774607756077660777607786077960780607816078260783607846078560786607876078860789607906079160792607936079460795607966079760798607996080060801608026080360804608056080660807608086080960810608116081260813608146081560816608176081860819608206082160822608236082460825608266082760828608296083060831608326083360834608356083660837608386083960840608416084260843608446084560846608476084860849608506085160852608536085460855608566085760858608596086060861608626086360864608656086660867608686086960870608716087260873608746087560876608776087860879608806088160882608836088460885608866088760888608896089060891608926089360894608956089660897608986089960900609016090260903609046090560906609076090860909609106091160912609136091460915609166091760918609196092060921609226092360924609256092660927609286092960930609316093260933609346093560936609376093860939609406094160942609436094460945609466094760948609496095060951609526095360954609556095660957609586095960960609616096260963609646096560966609676096860969609706097160972609736097460975609766097760978609796098060981609826098360984609856098660987609886098960990609916099260993609946099560996609976099860999610006100161002610036100461005610066100761008610096101061011610126101361014610156101661017610186101961020610216102261023610246102561026610276102861029610306103161032610336103461035610366103761038610396104061041610426104361044610456104661047610486104961050610516105261053610546105561056610576105861059610606106161062610636106461065610666106761068610696107061071610726107361074610756107661077610786107961080610816108261083610846108561086610876108861089610906109161092610936109461095610966109761098610996110061101611026110361104611056110661107611086110961110611116111261113611146111561116611176111861119611206112161122611236112461125611266112761128611296113061131611326113361134611356113661137611386113961140611416114261143611446114561146611476114861149611506115161152611536115461155611566115761158611596116061161611626116361164611656116661167611686116961170611716117261173611746117561176611776117861179611806118161182611836118461185611866118761188611896119061191611926119361194611956119661197611986119961200612016120261203612046120561206612076120861209612106121161212612136121461215612166121761218612196122061221612226122361224612256122661227612286122961230612316123261233612346123561236612376123861239612406124161242612436124461245612466124761248612496125061251612526125361254612556125661257612586125961260612616126261263612646126561266612676126861269612706127161272612736127461275612766127761278612796128061281612826128361284612856128661287612886128961290612916129261293612946129561296612976129861299613006130161302613036130461305613066130761308613096131061311613126131361314613156131661317613186131961320613216132261323613246132561326613276132861329613306133161332613336133461335613366133761338613396134061341613426134361344613456134661347613486134961350613516135261353613546135561356613576135861359613606136161362613636136461365613666136761368613696137061371613726137361374613756137661377613786137961380613816138261383613846138561386613876138861389613906139161392613936139461395613966139761398613996140061401614026140361404614056140661407614086140961410614116141261413614146141561416614176141861419614206142161422614236142461425614266142761428614296143061431614326143361434614356143661437614386143961440614416144261443614446144561446614476144861449614506145161452614536145461455614566145761458614596146061461614626146361464614656146661467614686146961470614716147261473614746147561476614776147861479614806148161482614836148461485614866148761488614896149061491614926149361494614956149661497614986149961500615016150261503615046150561506615076150861509615106151161512615136151461515615166151761518615196152061521615226152361524615256152661527615286152961530615316153261533615346153561536615376153861539615406154161542615436154461545615466154761548615496155061551615526155361554615556155661557615586155961560615616156261563615646156561566615676156861569615706157161572615736157461575615766157761578615796158061581615826158361584615856158661587615886158961590615916159261593615946159561596615976159861599616006160161602616036160461605616066160761608616096161061611616126161361614616156161661617616186161961620616216162261623616246162561626616276162861629616306163161632616336163461635616366163761638616396164061641616426164361644616456164661647616486164961650616516165261653616546165561656616576165861659616606166161662616636166461665616666166761668616696167061671616726167361674616756167661677616786167961680616816168261683616846168561686616876168861689616906169161692616936169461695616966169761698616996170061701617026170361704617056170661707617086170961710617116171261713617146171561716617176171861719617206172161722617236172461725617266172761728617296173061731617326173361734617356173661737617386173961740617416174261743617446174561746617476174861749617506175161752617536175461755617566175761758617596176061761617626176361764617656176661767617686176961770617716177261773617746177561776617776177861779617806178161782617836178461785617866178761788617896179061791617926179361794617956179661797617986179961800618016180261803618046180561806618076180861809618106181161812618136181461815618166181761818618196182061821618226182361824618256182661827618286182961830618316183261833618346183561836618376183861839618406184161842618436184461845618466184761848618496185061851618526185361854618556185661857618586185961860618616186261863618646186561866618676186861869618706187161872618736187461875618766187761878618796188061881618826188361884618856188661887618886188961890618916189261893618946189561896618976189861899619006190161902619036190461905619066190761908619096191061911619126191361914619156191661917619186191961920619216192261923619246192561926619276192861929619306193161932619336193461935619366193761938619396194061941619426194361944619456194661947619486194961950619516195261953619546195561956619576195861959619606196161962619636196461965619666196761968619696197061971619726197361974619756197661977619786197961980619816198261983619846198561986619876198861989619906199161992619936199461995619966199761998619996200062001620026200362004620056200662007620086200962010620116201262013620146201562016620176201862019620206202162022620236202462025620266202762028620296203062031620326203362034620356203662037620386203962040620416204262043620446204562046620476204862049620506205162052620536205462055620566205762058620596206062061620626206362064620656206662067620686206962070620716207262073620746207562076620776207862079620806208162082620836208462085620866208762088620896209062091620926209362094620956209662097620986209962100621016210262103621046210562106621076210862109621106211162112621136211462115621166211762118621196212062121621226212362124621256212662127621286212962130621316213262133621346213562136621376213862139621406214162142621436214462145621466214762148621496215062151621526215362154621556215662157621586215962160621616216262163621646216562166621676216862169621706217162172621736217462175621766217762178621796218062181621826218362184621856218662187621886218962190621916219262193621946219562196621976219862199622006220162202622036220462205622066220762208622096221062211622126221362214622156221662217622186221962220622216222262223622246222562226622276222862229622306223162232622336223462235622366223762238622396224062241622426224362244622456224662247622486224962250622516225262253622546225562256622576225862259622606226162262622636226462265622666226762268622696227062271622726227362274622756227662277622786227962280622816228262283622846228562286622876228862289622906229162292622936229462295622966229762298622996230062301623026230362304623056230662307623086230962310623116231262313623146231562316623176231862319623206232162322623236232462325623266232762328623296233062331623326233362334623356233662337623386233962340623416234262343623446234562346623476234862349623506235162352623536235462355623566235762358623596236062361623626236362364623656236662367623686236962370623716237262373623746237562376623776237862379623806238162382623836238462385623866238762388623896239062391623926239362394623956239662397623986239962400624016240262403624046240562406624076240862409624106241162412624136241462415624166241762418624196242062421624226242362424624256242662427624286242962430624316243262433624346243562436624376243862439624406244162442624436244462445624466244762448624496245062451624526245362454624556245662457624586245962460624616246262463624646246562466624676246862469624706247162472624736247462475624766247762478624796248062481624826248362484624856248662487624886248962490624916249262493624946249562496624976249862499625006250162502625036250462505625066250762508625096251062511625126251362514625156251662517625186251962520625216252262523625246252562526625276252862529625306253162532625336253462535625366253762538625396254062541625426254362544625456254662547625486254962550625516255262553625546255562556625576255862559625606256162562625636256462565625666256762568625696257062571625726257362574625756257662577625786257962580625816258262583625846258562586625876258862589625906259162592625936259462595625966259762598625996260062601626026260362604626056260662607626086260962610626116261262613626146261562616626176261862619626206262162622626236262462625626266262762628626296263062631626326263362634626356263662637626386263962640626416264262643626446264562646626476264862649626506265162652626536265462655626566265762658626596266062661626626266362664626656266662667626686266962670626716267262673626746267562676626776267862679626806268162682626836268462685626866268762688626896269062691626926269362694626956269662697626986269962700627016270262703627046270562706627076270862709627106271162712627136271462715627166271762718627196272062721627226272362724627256272662727627286272962730627316273262733627346273562736627376273862739627406274162742627436274462745627466274762748627496275062751627526275362754627556275662757627586275962760627616276262763627646276562766627676276862769627706277162772627736277462775627766277762778627796278062781627826278362784627856278662787627886278962790627916279262793627946279562796627976279862799628006280162802628036280462805628066280762808628096281062811628126281362814628156281662817628186281962820628216282262823628246282562826628276282862829628306283162832628336283462835628366283762838628396284062841628426284362844628456284662847628486284962850628516285262853628546285562856628576285862859628606286162862628636286462865628666286762868628696287062871628726287362874628756287662877628786287962880628816288262883628846288562886628876288862889628906289162892628936289462895628966289762898628996290062901629026290362904629056290662907629086290962910629116291262913629146291562916629176291862919629206292162922629236292462925629266292762928629296293062931629326293362934629356293662937629386293962940629416294262943629446294562946629476294862949629506295162952629536295462955629566295762958629596296062961629626296362964629656296662967629686296962970629716297262973629746297562976629776297862979629806298162982629836298462985629866298762988629896299062991629926299362994629956299662997629986299963000630016300263003630046300563006630076300863009630106301163012630136301463015630166301763018630196302063021630226302363024630256302663027630286302963030630316303263033630346303563036630376303863039630406304163042630436304463045630466304763048630496305063051630526305363054630556305663057630586305963060630616306263063630646306563066630676306863069630706307163072630736307463075630766307763078630796308063081630826308363084630856308663087630886308963090630916309263093630946309563096630976309863099631006310163102631036310463105631066310763108631096311063111631126311363114631156311663117631186311963120631216312263123631246312563126631276312863129631306313163132631336313463135631366313763138631396314063141631426314363144631456314663147631486314963150631516315263153631546315563156631576315863159631606316163162631636316463165631666316763168631696317063171631726317363174631756317663177631786317963180631816318263183631846318563186631876318863189631906319163192631936319463195631966319763198631996320063201632026320363204632056320663207632086320963210632116321263213632146321563216632176321863219632206322163222632236322463225632266322763228632296323063231632326323363234632356323663237632386323963240632416324263243632446324563246632476324863249632506325163252632536325463255632566325763258632596326063261632626326363264632656326663267632686326963270632716327263273632746327563276632776327863279632806328163282632836328463285632866328763288632896329063291632926329363294632956329663297632986329963300633016330263303633046330563306633076330863309633106331163312633136331463315633166331763318633196332063321633226332363324633256332663327633286332963330633316333263333633346333563336633376333863339633406334163342633436334463345633466334763348633496335063351633526335363354633556335663357633586335963360633616336263363633646336563366633676336863369633706337163372633736337463375633766337763378633796338063381633826338363384633856338663387633886338963390633916339263393633946339563396633976339863399634006340163402634036340463405634066340763408634096341063411634126341363414634156341663417634186341963420634216342263423634246342563426634276342863429634306343163432634336343463435634366343763438634396344063441634426344363444634456344663447634486344963450634516345263453634546345563456634576345863459634606346163462634636346463465634666346763468634696347063471634726347363474634756347663477634786347963480634816348263483634846348563486634876348863489634906349163492634936349463495634966349763498634996350063501635026350363504635056350663507635086350963510635116351263513635146351563516635176351863519635206352163522635236352463525635266352763528635296353063531635326353363534635356353663537635386353963540635416354263543635446354563546635476354863549635506355163552635536355463555635566355763558635596356063561635626356363564635656356663567635686356963570635716357263573635746357563576635776357863579635806358163582635836358463585635866358763588635896359063591635926359363594635956359663597635986359963600636016360263603636046360563606636076360863609636106361163612636136361463615636166361763618636196362063621636226362363624636256362663627636286362963630636316363263633636346363563636636376363863639636406364163642636436364463645636466364763648636496365063651636526365363654636556365663657636586365963660636616366263663636646366563666636676366863669636706367163672636736367463675636766367763678636796368063681636826368363684636856368663687636886368963690636916369263693636946369563696636976369863699637006370163702637036370463705637066370763708637096371063711637126371363714637156371663717637186371963720637216372263723637246372563726637276372863729637306373163732637336373463735637366373763738637396374063741637426374363744637456374663747637486374963750637516375263753637546375563756637576375863759637606376163762637636376463765637666376763768637696377063771637726377363774637756377663777637786377963780637816378263783637846378563786637876378863789637906379163792637936379463795637966379763798637996380063801638026380363804638056380663807638086380963810638116381263813638146381563816638176381863819638206382163822638236382463825638266382763828638296383063831638326383363834638356383663837638386383963840638416384263843638446384563846638476384863849638506385163852638536385463855638566385763858638596386063861638626386363864638656386663867638686386963870638716387263873638746387563876638776387863879638806388163882638836388463885638866388763888638896389063891638926389363894638956389663897638986389963900639016390263903639046390563906639076390863909639106391163912639136391463915639166391763918639196392063921639226392363924639256392663927639286392963930639316393263933639346393563936639376393863939639406394163942639436394463945639466394763948639496395063951639526395363954639556395663957639586395963960639616396263963639646396563966639676396863969639706397163972639736397463975639766397763978639796398063981639826398363984639856398663987639886398963990639916399263993639946399563996639976399863999640006400164002640036400464005640066400764008640096401064011640126401364014640156401664017640186401964020640216402264023640246402564026640276402864029640306403164032640336403464035640366403764038640396404064041640426404364044640456404664047640486404964050640516405264053640546405564056640576405864059640606406164062640636406464065640666406764068640696407064071640726407364074640756407664077640786407964080640816408264083640846408564086640876408864089640906409164092640936409464095640966409764098640996410064101641026410364104641056410664107641086410964110641116411264113641146411564116641176411864119641206412164122641236412464125641266412764128641296413064131641326413364134641356413664137641386413964140641416414264143641446414564146641476414864149641506415164152641536415464155641566415764158641596416064161641626416364164641656416664167641686416964170641716417264173641746417564176641776417864179641806418164182641836418464185641866418764188641896419064191641926419364194641956419664197641986419964200642016420264203642046420564206642076420864209642106421164212642136421464215642166421764218642196422064221642226422364224642256422664227642286422964230642316423264233642346423564236642376423864239642406424164242642436424464245642466424764248642496425064251642526425364254642556425664257642586425964260642616426264263642646426564266642676426864269642706427164272642736427464275642766427764278642796428064281642826428364284642856428664287642886428964290642916429264293642946429564296642976429864299643006430164302643036430464305643066430764308643096431064311643126431364314643156431664317643186431964320643216432264323643246432564326643276432864329643306433164332643336433464335643366433764338643396434064341643426434364344643456434664347643486434964350643516435264353643546435564356643576435864359643606436164362643636436464365643666436764368643696437064371643726437364374643756437664377643786437964380643816438264383643846438564386643876438864389643906439164392643936439464395643966439764398643996440064401644026440364404644056440664407644086440964410644116441264413644146441564416644176441864419644206442164422644236442464425644266442764428644296443064431644326443364434644356443664437644386443964440644416444264443644446444564446644476444864449644506445164452644536445464455644566445764458644596446064461644626446364464644656446664467644686446964470644716447264473644746447564476644776447864479644806448164482644836448464485644866448764488644896449064491644926449364494644956449664497644986449964500645016450264503645046450564506645076450864509645106451164512645136451464515645166451764518645196452064521645226452364524645256452664527645286452964530645316453264533645346453564536645376453864539645406454164542645436454464545645466454764548645496455064551645526455364554645556455664557645586455964560645616456264563645646456564566645676456864569645706457164572645736457464575645766457764578645796458064581645826458364584645856458664587645886458964590645916459264593645946459564596645976459864599646006460164602646036460464605646066460764608646096461064611646126461364614646156461664617646186461964620646216462264623646246462564626646276462864629646306463164632646336463464635646366463764638646396464064641646426464364644646456464664647646486464964650646516465264653646546465564656646576465864659646606466164662646636466464665646666466764668646696467064671646726467364674646756467664677646786467964680646816468264683646846468564686646876468864689646906469164692646936469464695646966469764698646996470064701647026470364704647056470664707647086470964710647116471264713647146471564716647176471864719647206472164722647236472464725647266472764728647296473064731647326473364734647356473664737647386473964740647416474264743647446474564746647476474864749647506475164752647536475464755647566475764758647596476064761647626476364764647656476664767647686476964770647716477264773647746477564776647776477864779647806478164782647836478464785647866478764788647896479064791647926479364794647956479664797647986479964800648016480264803648046480564806648076480864809648106481164812648136481464815648166481764818648196482064821648226482364824648256482664827648286482964830648316483264833648346483564836648376483864839648406484164842648436484464845648466484764848648496485064851648526485364854648556485664857648586485964860648616486264863648646486564866648676486864869648706487164872648736487464875648766487764878648796488064881648826488364884648856488664887648886488964890648916489264893648946489564896648976489864899649006490164902649036490464905649066490764908649096491064911649126491364914649156491664917649186491964920649216492264923649246492564926649276492864929649306493164932649336493464935649366493764938649396494064941649426494364944649456494664947649486494964950649516495264953649546495564956649576495864959649606496164962649636496464965649666496764968649696497064971649726497364974649756497664977649786497964980649816498264983649846498564986649876498864989649906499164992649936499464995649966499764998649996500065001650026500365004650056500665007650086500965010650116501265013650146501565016650176501865019650206502165022650236502465025650266502765028650296503065031650326503365034650356503665037650386503965040650416504265043650446504565046650476504865049650506505165052650536505465055650566505765058650596506065061650626506365064650656506665067650686506965070650716507265073650746507565076650776507865079650806508165082650836508465085650866508765088650896509065091650926509365094650956509665097650986509965100651016510265103651046510565106651076510865109651106511165112651136511465115651166511765118651196512065121651226512365124651256512665127651286512965130651316513265133651346513565136651376513865139651406514165142651436514465145651466514765148651496515065151651526515365154651556515665157651586515965160651616516265163651646516565166651676516865169651706517165172651736517465175651766517765178651796518065181651826518365184651856518665187651886518965190651916519265193651946519565196651976519865199652006520165202652036520465205652066520765208652096521065211652126521365214652156521665217652186521965220652216522265223652246522565226652276522865229652306523165232652336523465235652366523765238652396524065241652426524365244652456524665247652486524965250652516525265253652546525565256652576525865259652606526165262652636526465265652666526765268652696527065271652726527365274652756527665277652786527965280652816528265283652846528565286652876528865289652906529165292652936529465295652966529765298652996530065301653026530365304653056530665307653086530965310653116531265313653146531565316653176531865319653206532165322653236532465325653266532765328653296533065331653326533365334653356533665337653386533965340653416534265343653446534565346653476534865349653506535165352653536535465355653566535765358653596536065361653626536365364653656536665367653686536965370653716537265373653746537565376653776537865379653806538165382653836538465385653866538765388653896539065391653926539365394653956539665397653986539965400654016540265403654046540565406654076540865409654106541165412654136541465415654166541765418654196542065421654226542365424654256542665427654286542965430654316543265433654346543565436654376543865439654406544165442654436544465445654466544765448654496545065451654526545365454654556545665457654586545965460654616546265463654646546565466654676546865469654706547165472654736547465475654766547765478654796548065481654826548365484654856548665487654886548965490654916549265493654946549565496654976549865499655006550165502655036550465505655066550765508655096551065511655126551365514655156551665517655186551965520655216552265523655246552565526655276552865529655306553165532655336553465535655366553765538655396554065541655426554365544655456554665547655486554965550655516555265553655546555565556655576555865559655606556165562655636556465565655666556765568655696557065571655726557365574655756557665577655786557965580655816558265583655846558565586655876558865589655906559165592655936559465595655966559765598655996560065601656026560365604656056560665607656086560965610656116561265613656146561565616656176561865619656206562165622656236562465625656266562765628656296563065631656326563365634656356563665637656386563965640656416564265643656446564565646656476564865649656506565165652656536565465655656566565765658656596566065661656626566365664656656566665667656686566965670656716567265673656746567565676656776567865679656806568165682656836568465685656866568765688656896569065691656926569365694656956569665697656986569965700657016570265703657046570565706657076570865709657106571165712657136571465715657166571765718657196572065721657226572365724657256572665727657286572965730657316573265733657346573565736657376573865739657406574165742657436574465745657466574765748657496575065751657526575365754657556575665757657586575965760657616576265763657646576565766657676576865769657706577165772657736577465775657766577765778657796578065781657826578365784657856578665787657886578965790657916579265793657946579565796657976579865799658006580165802658036580465805658066580765808658096581065811658126581365814658156581665817658186581965820658216582265823658246582565826658276582865829658306583165832658336583465835658366583765838658396584065841658426584365844658456584665847658486584965850658516585265853658546585565856658576585865859658606586165862658636586465865658666586765868658696587065871658726587365874658756587665877658786587965880658816588265883658846588565886658876588865889658906589165892658936589465895658966589765898658996590065901659026590365904659056590665907659086590965910659116591265913659146591565916659176591865919659206592165922659236592465925659266592765928659296593065931659326593365934659356593665937659386593965940659416594265943659446594565946659476594865949659506595165952659536595465955659566595765958659596596065961659626596365964659656596665967659686596965970659716597265973659746597565976659776597865979659806598165982659836598465985659866598765988659896599065991659926599365994659956599665997659986599966000660016600266003660046600566006660076600866009660106601166012660136601466015660166601766018660196602066021660226602366024660256602666027660286602966030660316603266033660346603566036660376603866039660406604166042660436604466045660466604766048660496605066051660526605366054660556605666057660586605966060660616606266063660646606566066660676606866069660706607166072660736607466075660766607766078660796608066081660826608366084660856608666087660886608966090660916609266093660946609566096660976609866099661006610166102661036610466105661066610766108661096611066111661126611366114661156611666117661186611966120661216612266123661246612566126661276612866129661306613166132661336613466135661366613766138661396614066141661426614366144661456614666147661486614966150661516615266153661546615566156661576615866159661606616166162661636616466165661666616766168661696617066171661726617366174661756617666177661786617966180661816618266183661846618566186661876618866189661906619166192661936619466195661966619766198661996620066201662026620366204662056620666207662086620966210662116621266213662146621566216662176621866219662206622166222662236622466225662266622766228662296623066231662326623366234662356623666237662386623966240662416624266243662446624566246662476624866249662506625166252662536625466255662566625766258662596626066261662626626366264662656626666267662686626966270662716627266273662746627566276662776627866279662806628166282662836628466285662866628766288662896629066291662926629366294662956629666297662986629966300663016630266303663046630566306663076630866309663106631166312663136631466315663166631766318663196632066321663226632366324663256632666327663286632966330663316633266333663346633566336663376633866339663406634166342663436634466345663466634766348663496635066351663526635366354663556635666357663586635966360663616636266363663646636566366663676636866369663706637166372663736637466375663766637766378663796638066381663826638366384663856638666387663886638966390663916639266393663946639566396663976639866399664006640166402664036640466405664066640766408664096641066411664126641366414664156641666417664186641966420664216642266423664246642566426664276642866429664306643166432664336643466435664366643766438664396644066441664426644366444664456644666447664486644966450664516645266453664546645566456664576645866459664606646166462664636646466465664666646766468664696647066471664726647366474664756647666477664786647966480664816648266483664846648566486664876648866489664906649166492664936649466495664966649766498664996650066501665026650366504665056650666507665086650966510665116651266513665146651566516665176651866519665206652166522665236652466525665266652766528665296653066531665326653366534665356653666537665386653966540665416654266543665446654566546665476654866549665506655166552665536655466555665566655766558665596656066561665626656366564665656656666567665686656966570665716657266573665746657566576665776657866579665806658166582665836658466585665866658766588665896659066591665926659366594665956659666597665986659966600666016660266603666046660566606666076660866609666106661166612666136661466615666166661766618666196662066621666226662366624666256662666627666286662966630666316663266633666346663566636666376663866639666406664166642666436664466645666466664766648666496665066651666526665366654666556665666657666586665966660666616666266663666646666566666666676666866669666706667166672666736667466675666766667766678666796668066681666826668366684666856668666687666886668966690666916669266693666946669566696666976669866699667006670166702667036670466705667066670766708667096671066711667126671366714667156671666717667186671966720667216672266723667246672566726667276672866729667306673166732667336673466735667366673766738667396674066741667426674366744667456674666747667486674966750667516675266753667546675566756667576675866759667606676166762667636676466765667666676766768667696677066771667726677366774667756677666777667786677966780667816678266783667846678566786667876678866789667906679166792667936679466795667966679766798667996680066801668026680366804668056680666807668086680966810668116681266813668146681566816668176681866819668206682166822668236682466825668266682766828668296683066831668326683366834668356683666837668386683966840668416684266843668446684566846668476684866849668506685166852668536685466855668566685766858668596686066861668626686366864668656686666867668686686966870668716687266873668746687566876668776687866879668806688166882668836688466885668866688766888668896689066891668926689366894668956689666897668986689966900669016690266903669046690566906669076690866909669106691166912669136691466915669166691766918669196692066921669226692366924669256692666927669286692966930669316693266933669346693566936669376693866939669406694166942669436694466945669466694766948669496695066951669526695366954669556695666957669586695966960669616696266963669646696566966669676696866969669706697166972669736697466975669766697766978669796698066981669826698366984669856698666987669886698966990669916699266993669946699566996669976699866999670006700167002670036700467005670066700767008670096701067011670126701367014670156701667017670186701967020670216702267023670246702567026670276702867029670306703167032670336703467035670366703767038670396704067041670426704367044670456704667047670486704967050670516705267053670546705567056670576705867059670606706167062670636706467065670666706767068670696707067071670726707367074670756707667077670786707967080670816708267083670846708567086670876708867089670906709167092670936709467095670966709767098670996710067101671026710367104671056710667107671086710967110671116711267113671146711567116671176711867119671206712167122671236712467125671266712767128671296713067131671326713367134671356713667137671386713967140671416714267143671446714567146671476714867149671506715167152671536715467155671566715767158671596716067161671626716367164671656716667167671686716967170671716717267173671746717567176671776717867179671806718167182671836718467185671866718767188671896719067191671926719367194671956719667197671986719967200672016720267203672046720567206672076720867209672106721167212672136721467215672166721767218672196722067221672226722367224672256722667227672286722967230672316723267233672346723567236672376723867239672406724167242672436724467245672466724767248672496725067251672526725367254672556725667257672586725967260672616726267263672646726567266672676726867269672706727167272672736727467275672766727767278672796728067281672826728367284672856728667287672886728967290672916729267293672946729567296672976729867299673006730167302673036730467305673066730767308673096731067311673126731367314673156731667317673186731967320673216732267323673246732567326673276732867329673306733167332673336733467335673366733767338673396734067341673426734367344673456734667347673486734967350673516735267353673546735567356673576735867359673606736167362673636736467365673666736767368673696737067371673726737367374673756737667377673786737967380673816738267383673846738567386673876738867389673906739167392673936739467395673966739767398673996740067401674026740367404674056740667407674086740967410674116741267413674146741567416674176741867419674206742167422674236742467425674266742767428674296743067431674326743367434674356743667437674386743967440674416744267443674446744567446674476744867449674506745167452674536745467455674566745767458674596746067461674626746367464674656746667467674686746967470674716747267473674746747567476674776747867479674806748167482674836748467485674866748767488674896749067491674926749367494674956749667497674986749967500675016750267503675046750567506675076750867509675106751167512675136751467515675166751767518675196752067521675226752367524675256752667527675286752967530675316753267533675346753567536675376753867539675406754167542675436754467545675466754767548675496755067551675526755367554675556755667557675586755967560675616756267563675646756567566675676756867569675706757167572675736757467575675766757767578675796758067581675826758367584675856758667587675886758967590675916759267593675946759567596675976759867599676006760167602676036760467605676066760767608676096761067611676126761367614676156761667617676186761967620676216762267623676246762567626676276762867629676306763167632676336763467635676366763767638676396764067641676426764367644676456764667647676486764967650676516765267653676546765567656676576765867659676606766167662676636766467665676666766767668676696767067671676726767367674676756767667677676786767967680676816768267683676846768567686676876768867689676906769167692676936769467695676966769767698676996770067701677026770367704677056770667707677086770967710677116771267713677146771567716677176771867719677206772167722677236772467725677266772767728677296773067731677326773367734677356773667737677386773967740677416774267743677446774567746677476774867749677506775167752677536775467755677566775767758677596776067761677626776367764677656776667767677686776967770677716777267773677746777567776677776777867779677806778167782677836778467785677866778767788677896779067791677926779367794677956779667797677986779967800678016780267803678046780567806678076780867809678106781167812678136781467815678166781767818678196782067821678226782367824678256782667827678286782967830678316783267833678346783567836678376783867839678406784167842678436784467845678466784767848678496785067851678526785367854678556785667857678586785967860678616786267863678646786567866678676786867869678706787167872678736787467875678766787767878678796788067881678826788367884678856788667887678886788967890678916789267893678946789567896678976789867899679006790167902679036790467905679066790767908679096791067911679126791367914679156791667917679186791967920679216792267923679246792567926679276792867929679306793167932679336793467935679366793767938679396794067941679426794367944679456794667947679486794967950679516795267953679546795567956679576795867959679606796167962679636796467965679666796767968679696797067971679726797367974679756797667977679786797967980679816798267983679846798567986679876798867989679906799167992679936799467995679966799767998679996800068001680026800368004680056800668007680086800968010680116801268013680146801568016680176801868019680206802168022680236802468025680266802768028680296803068031680326803368034680356803668037680386803968040680416804268043680446804568046680476804868049680506805168052680536805468055680566805768058680596806068061680626806368064680656806668067680686806968070680716807268073680746807568076680776807868079680806808168082680836808468085680866808768088680896809068091680926809368094680956809668097680986809968100681016810268103681046810568106681076810868109681106811168112681136811468115681166811768118681196812068121681226812368124681256812668127681286812968130681316813268133681346813568136681376813868139681406814168142681436814468145681466814768148681496815068151681526815368154681556815668157681586815968160681616816268163681646816568166681676816868169681706817168172681736817468175681766817768178681796818068181681826818368184681856818668187681886818968190681916819268193681946819568196681976819868199682006820168202682036820468205682066820768208682096821068211682126821368214682156821668217682186821968220682216822268223682246822568226682276822868229682306823168232682336823468235682366823768238682396824068241682426824368244682456824668247682486824968250682516825268253682546825568256682576825868259682606826168262682636826468265682666826768268682696827068271682726827368274682756827668277682786827968280682816828268283682846828568286682876828868289682906829168292682936829468295682966829768298682996830068301683026830368304683056830668307683086830968310683116831268313683146831568316683176831868319683206832168322683236832468325683266832768328683296833068331683326833368334683356833668337683386833968340683416834268343683446834568346683476834868349683506835168352683536835468355683566835768358683596836068361683626836368364683656836668367683686836968370683716837268373683746837568376683776837868379683806838168382683836838468385683866838768388683896839068391683926839368394683956839668397683986839968400684016840268403684046840568406684076840868409684106841168412684136841468415684166841768418684196842068421684226842368424684256842668427684286842968430684316843268433684346843568436684376843868439684406844168442684436844468445684466844768448684496845068451684526845368454684556845668457684586845968460684616846268463684646846568466684676846868469684706847168472684736847468475684766847768478684796848068481684826848368484684856848668487684886848968490684916849268493684946849568496684976849868499685006850168502685036850468505685066850768508685096851068511685126851368514685156851668517685186851968520685216852268523685246852568526685276852868529685306853168532685336853468535685366853768538685396854068541685426854368544685456854668547685486854968550685516855268553685546855568556685576855868559685606856168562685636856468565685666856768568685696857068571685726857368574685756857668577685786857968580685816858268583685846858568586685876858868589685906859168592685936859468595685966859768598685996860068601686026860368604686056860668607686086860968610686116861268613686146861568616686176861868619686206862168622686236862468625686266862768628686296863068631686326863368634686356863668637686386863968640686416864268643686446864568646686476864868649686506865168652686536865468655686566865768658686596866068661686626866368664686656866668667686686866968670686716867268673686746867568676686776867868679686806868168682686836868468685686866868768688686896869068691686926869368694686956869668697686986869968700687016870268703687046870568706687076870868709687106871168712687136871468715687166871768718687196872068721687226872368724687256872668727687286872968730687316873268733687346873568736687376873868739687406874168742687436874468745687466874768748687496875068751687526875368754687556875668757687586875968760687616876268763687646876568766687676876868769687706877168772687736877468775687766877768778687796878068781687826878368784687856878668787687886878968790687916879268793687946879568796687976879868799688006880168802688036880468805688066880768808688096881068811688126881368814688156881668817688186881968820688216882268823688246882568826688276882868829688306883168832688336883468835688366883768838688396884068841688426884368844688456884668847688486884968850688516885268853688546885568856688576885868859688606886168862688636886468865688666886768868688696887068871688726887368874688756887668877688786887968880688816888268883688846888568886688876888868889688906889168892688936889468895688966889768898688996890068901689026890368904689056890668907689086890968910689116891268913689146891568916689176891868919689206892168922689236892468925689266892768928689296893068931689326893368934689356893668937689386893968940689416894268943689446894568946689476894868949689506895168952689536895468955689566895768958689596896068961689626896368964689656896668967689686896968970689716897268973689746897568976689776897868979689806898168982689836898468985689866898768988689896899068991689926899368994689956899668997689986899969000690016900269003690046900569006690076900869009690106901169012690136901469015690166901769018690196902069021690226902369024690256902669027690286902969030690316903269033690346903569036690376903869039690406904169042690436904469045690466904769048690496905069051690526905369054690556905669057690586905969060690616906269063690646906569066690676906869069690706907169072690736907469075690766907769078690796908069081690826908369084690856908669087690886908969090690916909269093690946909569096690976909869099691006910169102691036910469105691066910769108691096911069111691126911369114691156911669117691186911969120691216912269123691246912569126691276912869129691306913169132691336913469135691366913769138691396914069141691426914369144691456914669147691486914969150691516915269153691546915569156691576915869159691606916169162691636916469165691666916769168691696917069171691726917369174691756917669177691786917969180691816918269183691846918569186691876918869189691906919169192691936919469195691966919769198691996920069201692026920369204692056920669207692086920969210692116921269213692146921569216692176921869219692206922169222692236922469225692266922769228692296923069231692326923369234692356923669237692386923969240692416924269243692446924569246692476924869249692506925169252692536925469255692566925769258692596926069261692626926369264692656926669267692686926969270692716927269273692746927569276692776927869279692806928169282692836928469285692866928769288692896929069291692926929369294692956929669297692986929969300693016930269303693046930569306693076930869309693106931169312693136931469315693166931769318693196932069321693226932369324693256932669327693286932969330693316933269333693346933569336693376933869339693406934169342693436934469345693466934769348693496935069351693526935369354693556935669357693586935969360693616936269363693646936569366693676936869369693706937169372693736937469375693766937769378693796938069381693826938369384693856938669387693886938969390693916939269393693946939569396693976939869399694006940169402694036940469405694066940769408694096941069411694126941369414694156941669417694186941969420694216942269423694246942569426694276942869429694306943169432694336943469435694366943769438694396944069441694426944369444694456944669447694486944969450694516945269453694546945569456694576945869459694606946169462694636946469465694666946769468694696947069471694726947369474694756947669477694786947969480694816948269483694846948569486694876948869489694906949169492694936949469495694966949769498694996950069501695026950369504695056950669507695086950969510695116951269513695146951569516695176951869519695206952169522695236952469525695266952769528695296953069531695326953369534695356953669537695386953969540695416954269543695446954569546695476954869549695506955169552695536955469555695566955769558695596956069561695626956369564695656956669567695686956969570695716957269573695746957569576695776957869579695806958169582695836958469585695866958769588695896959069591695926959369594695956959669597695986959969600696016960269603696046960569606696076960869609696106961169612696136961469615696166961769618696196962069621696226962369624696256962669627696286962969630696316963269633696346963569636696376963869639696406964169642696436964469645696466964769648696496965069651696526965369654696556965669657696586965969660696616966269663696646966569666696676966869669696706967169672696736967469675696766967769678696796968069681696826968369684696856968669687696886968969690696916969269693696946969569696696976969869699697006970169702697036970469705697066970769708697096971069711697126971369714697156971669717697186971969720697216972269723697246972569726697276972869729697306973169732697336973469735697366973769738697396974069741697426974369744697456974669747697486974969750697516975269753697546975569756697576975869759697606976169762697636976469765697666976769768697696977069771697726977369774697756977669777697786977969780697816978269783697846978569786697876978869789697906979169792697936979469795697966979769798697996980069801698026980369804698056980669807698086980969810698116981269813698146981569816698176981869819698206982169822698236982469825698266982769828698296983069831698326983369834698356983669837698386983969840698416984269843698446984569846698476984869849698506985169852698536985469855698566985769858698596986069861698626986369864698656986669867698686986969870698716987269873698746987569876698776987869879698806988169882698836988469885698866988769888698896989069891698926989369894698956989669897698986989969900699016990269903699046990569906699076990869909699106991169912699136991469915699166991769918699196992069921699226992369924699256992669927699286992969930699316993269933699346993569936699376993869939699406994169942699436994469945699466994769948699496995069951699526995369954699556995669957699586995969960699616996269963699646996569966699676996869969699706997169972699736997469975699766997769978699796998069981699826998369984699856998669987699886998969990699916999269993699946999569996699976999869999700007000170002700037000470005700067000770008700097001070011700127001370014700157001670017700187001970020700217002270023700247002570026700277002870029700307003170032700337003470035700367003770038700397004070041700427004370044700457004670047700487004970050700517005270053700547005570056700577005870059700607006170062700637006470065700667006770068700697007070071700727007370074700757007670077700787007970080700817008270083700847008570086700877008870089700907009170092700937009470095700967009770098700997010070101701027010370104701057010670107701087010970110701117011270113701147011570116701177011870119701207012170122701237012470125701267012770128701297013070131701327013370134701357013670137701387013970140701417014270143701447014570146701477014870149701507015170152701537015470155701567015770158701597016070161701627016370164701657016670167701687016970170701717017270173701747017570176701777017870179701807018170182701837018470185701867018770188701897019070191701927019370194701957019670197701987019970200702017020270203702047020570206702077020870209702107021170212702137021470215702167021770218702197022070221702227022370224702257022670227702287022970230702317023270233702347023570236702377023870239702407024170242702437024470245702467024770248702497025070251702527025370254702557025670257702587025970260702617026270263702647026570266702677026870269702707027170272702737027470275702767027770278702797028070281702827028370284702857028670287702887028970290702917029270293702947029570296702977029870299703007030170302703037030470305703067030770308703097031070311703127031370314703157031670317703187031970320703217032270323703247032570326703277032870329703307033170332703337033470335703367033770338703397034070341703427034370344703457034670347703487034970350703517035270353703547035570356703577035870359703607036170362703637036470365703667036770368703697037070371703727037370374703757037670377703787037970380703817038270383703847038570386703877038870389703907039170392703937039470395703967039770398703997040070401704027040370404704057040670407704087040970410704117041270413704147041570416704177041870419704207042170422704237042470425704267042770428704297043070431704327043370434704357043670437704387043970440704417044270443704447044570446704477044870449704507045170452704537045470455704567045770458704597046070461704627046370464704657046670467704687046970470704717047270473704747047570476704777047870479704807048170482704837048470485704867048770488704897049070491704927049370494704957049670497704987049970500705017050270503705047050570506705077050870509705107051170512705137051470515705167051770518705197052070521705227052370524705257052670527705287052970530705317053270533705347053570536705377053870539705407054170542705437054470545705467054770548705497055070551705527055370554705557055670557705587055970560705617056270563705647056570566705677056870569705707057170572705737057470575705767057770578705797058070581705827058370584705857058670587705887058970590705917059270593705947059570596705977059870599706007060170602706037060470605706067060770608706097061070611706127061370614706157061670617706187061970620706217062270623706247062570626706277062870629706307063170632706337063470635706367063770638706397064070641706427064370644706457064670647706487064970650706517065270653706547065570656706577065870659706607066170662706637066470665706667066770668706697067070671706727067370674706757067670677706787067970680706817068270683706847068570686706877068870689706907069170692706937069470695706967069770698706997070070701707027070370704707057070670707707087070970710707117071270713707147071570716707177071870719707207072170722707237072470725707267072770728707297073070731707327073370734707357073670737707387073970740707417074270743707447074570746707477074870749707507075170752707537075470755707567075770758707597076070761707627076370764707657076670767707687076970770707717077270773707747077570776707777077870779707807078170782707837078470785707867078770788707897079070791707927079370794707957079670797707987079970800708017080270803708047080570806708077080870809708107081170812708137081470815708167081770818708197082070821708227082370824708257082670827708287082970830708317083270833708347083570836708377083870839708407084170842708437084470845708467084770848708497085070851708527085370854708557085670857708587085970860708617086270863708647086570866708677086870869708707087170872708737087470875708767087770878708797088070881708827088370884708857088670887708887088970890708917089270893708947089570896708977089870899709007090170902709037090470905709067090770908709097091070911709127091370914709157091670917709187091970920709217092270923709247092570926709277092870929709307093170932709337093470935709367093770938709397094070941709427094370944709457094670947709487094970950709517095270953709547095570956709577095870959709607096170962709637096470965709667096770968709697097070971709727097370974709757097670977709787097970980709817098270983709847098570986709877098870989709907099170992709937099470995709967099770998709997100071001710027100371004710057100671007710087100971010710117101271013710147101571016710177101871019710207102171022710237102471025710267102771028710297103071031710327103371034710357103671037710387103971040710417104271043710447104571046710477104871049710507105171052710537105471055710567105771058710597106071061710627106371064710657106671067710687106971070710717107271073710747107571076710777107871079710807108171082710837108471085710867108771088710897109071091710927109371094710957109671097710987109971100711017110271103711047110571106711077110871109711107111171112711137111471115711167111771118711197112071121711227112371124711257112671127711287112971130711317113271133711347113571136711377113871139711407114171142711437114471145711467114771148711497115071151711527115371154711557115671157711587115971160711617116271163711647116571166711677116871169711707117171172711737117471175711767117771178711797118071181711827118371184711857118671187711887118971190711917119271193711947119571196711977119871199712007120171202712037120471205712067120771208712097121071211712127121371214712157121671217712187121971220712217122271223712247122571226712277122871229712307123171232712337123471235712367123771238712397124071241712427124371244712457124671247712487124971250712517125271253712547125571256712577125871259712607126171262712637126471265712667126771268712697127071271712727127371274712757127671277712787127971280712817128271283712847128571286712877128871289712907129171292712937129471295712967129771298712997130071301713027130371304713057130671307713087130971310713117131271313713147131571316713177131871319713207132171322713237132471325713267132771328713297133071331713327133371334713357133671337713387133971340713417134271343713447134571346713477134871349713507135171352713537135471355713567135771358713597136071361713627136371364713657136671367713687136971370713717137271373713747137571376713777137871379713807138171382713837138471385713867138771388713897139071391713927139371394713957139671397713987139971400714017140271403714047140571406714077140871409714107141171412714137141471415714167141771418714197142071421714227142371424714257142671427714287142971430714317143271433714347143571436714377143871439714407144171442714437144471445714467144771448714497145071451714527145371454714557145671457714587145971460714617146271463714647146571466714677146871469714707147171472714737147471475714767147771478714797148071481714827148371484714857148671487714887148971490714917149271493714947149571496714977149871499715007150171502715037150471505715067150771508715097151071511715127151371514715157151671517715187151971520715217152271523715247152571526715277152871529715307153171532715337153471535715367153771538715397154071541715427154371544715457154671547715487154971550715517155271553715547155571556715577155871559715607156171562715637156471565715667156771568715697157071571715727157371574715757157671577715787157971580715817158271583715847158571586715877158871589715907159171592715937159471595715967159771598715997160071601716027160371604716057160671607716087160971610716117161271613716147161571616716177161871619716207162171622716237162471625716267162771628716297163071631716327163371634716357163671637716387163971640716417164271643716447164571646716477164871649716507165171652716537165471655716567165771658716597166071661716627166371664716657166671667716687166971670716717167271673716747167571676716777167871679716807168171682716837168471685716867168771688716897169071691716927169371694716957169671697716987169971700717017170271703717047170571706717077170871709717107171171712717137171471715717167171771718717197172071721717227172371724717257172671727717287172971730717317173271733717347173571736717377173871739717407174171742717437174471745717467174771748717497175071751717527175371754717557175671757717587175971760717617176271763717647176571766717677176871769717707177171772717737177471775717767177771778717797178071781717827178371784717857178671787717887178971790717917179271793717947179571796717977179871799718007180171802718037180471805718067180771808718097181071811718127181371814718157181671817718187181971820718217182271823718247182571826718277182871829718307183171832718337183471835718367183771838718397184071841718427184371844718457184671847718487184971850718517185271853718547185571856718577185871859718607186171862718637186471865718667186771868718697187071871718727187371874718757187671877718787187971880718817188271883718847188571886718877188871889718907189171892718937189471895718967189771898718997190071901719027190371904719057190671907719087190971910719117191271913719147191571916719177191871919719207192171922719237192471925719267192771928719297193071931719327193371934719357193671937719387193971940719417194271943719447194571946719477194871949719507195171952719537195471955719567195771958719597196071961719627196371964719657196671967719687196971970719717197271973719747197571976719777197871979719807198171982719837198471985719867198771988719897199071991719927199371994719957199671997719987199972000720017200272003720047200572006720077200872009720107201172012720137201472015720167201772018720197202072021720227202372024720257202672027720287202972030720317203272033720347203572036720377203872039720407204172042720437204472045720467204772048720497205072051720527205372054720557205672057720587205972060720617206272063720647206572066720677206872069720707207172072720737207472075720767207772078720797208072081720827208372084720857208672087720887208972090720917209272093720947209572096720977209872099721007210172102721037210472105721067210772108721097211072111721127211372114721157211672117721187211972120721217212272123721247212572126721277212872129721307213172132721337213472135721367213772138721397214072141721427214372144721457214672147721487214972150721517215272153721547215572156721577215872159721607216172162721637216472165721667216772168721697217072171721727217372174721757217672177721787217972180721817218272183721847218572186721877218872189721907219172192721937219472195721967219772198721997220072201722027220372204722057220672207722087220972210722117221272213722147221572216722177221872219722207222172222722237222472225722267222772228722297223072231722327223372234722357223672237722387223972240722417224272243722447224572246722477224872249722507225172252722537225472255722567225772258722597226072261722627226372264722657226672267722687226972270722717227272273722747227572276722777227872279722807228172282722837228472285722867228772288722897229072291722927229372294722957229672297722987229972300723017230272303723047230572306723077230872309723107231172312723137231472315723167231772318723197232072321723227232372324723257232672327723287232972330723317233272333723347233572336723377233872339723407234172342723437234472345723467234772348723497235072351723527235372354723557235672357723587235972360723617236272363723647236572366723677236872369723707237172372723737237472375723767237772378723797238072381723827238372384723857238672387723887238972390723917239272393723947239572396723977239872399724007240172402724037240472405724067240772408724097241072411724127241372414724157241672417724187241972420724217242272423724247242572426724277242872429724307243172432724337243472435724367243772438724397244072441724427244372444724457244672447724487244972450724517245272453724547245572456724577245872459724607246172462724637246472465724667246772468724697247072471724727247372474724757247672477724787247972480724817248272483724847248572486724877248872489724907249172492724937249472495724967249772498724997250072501725027250372504725057250672507725087250972510725117251272513725147251572516725177251872519725207252172522725237252472525725267252772528725297253072531725327253372534725357253672537725387253972540725417254272543725447254572546725477254872549725507255172552725537255472555725567255772558725597256072561725627256372564725657256672567725687256972570725717257272573725747257572576725777257872579725807258172582725837258472585725867258772588725897259072591725927259372594725957259672597725987259972600726017260272603726047260572606726077260872609726107261172612726137261472615726167261772618726197262072621726227262372624726257262672627726287262972630726317263272633726347263572636726377263872639726407264172642726437264472645726467264772648726497265072651726527265372654726557265672657726587265972660726617266272663726647266572666726677266872669726707267172672726737267472675726767267772678726797268072681726827268372684726857268672687726887268972690726917269272693726947269572696726977269872699727007270172702727037270472705727067270772708727097271072711727127271372714727157271672717727187271972720727217272272723727247272572726727277272872729727307273172732727337273472735727367273772738727397274072741727427274372744727457274672747727487274972750727517275272753727547275572756727577275872759727607276172762727637276472765727667276772768727697277072771727727277372774727757277672777727787277972780727817278272783727847278572786727877278872789727907279172792727937279472795727967279772798727997280072801728027280372804728057280672807728087280972810728117281272813728147281572816728177281872819728207282172822728237282472825728267282772828728297283072831728327283372834728357283672837728387283972840728417284272843728447284572846728477284872849728507285172852728537285472855728567285772858728597286072861728627286372864728657286672867728687286972870728717287272873728747287572876728777287872879728807288172882728837288472885728867288772888728897289072891728927289372894728957289672897728987289972900729017290272903729047290572906729077290872909729107291172912729137291472915729167291772918729197292072921729227292372924729257292672927729287292972930729317293272933729347293572936729377293872939729407294172942729437294472945729467294772948729497295072951729527295372954729557295672957729587295972960729617296272963729647296572966729677296872969729707297172972729737297472975729767297772978729797298072981729827298372984729857298672987729887298972990729917299272993729947299572996729977299872999730007300173002730037300473005730067300773008730097301073011730127301373014730157301673017730187301973020730217302273023730247302573026730277302873029730307303173032730337303473035730367303773038730397304073041730427304373044730457304673047730487304973050730517305273053730547305573056730577305873059730607306173062730637306473065730667306773068730697307073071730727307373074730757307673077730787307973080730817308273083730847308573086730877308873089730907309173092730937309473095730967309773098730997310073101731027310373104731057310673107
  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. /*----------------------------------------------------------------------------*
  593. | Platform dependent function test
  594. *----------------------------------------------------------------------------*/
  595. static int test_fileAccess(void)
  596. {
  597. EXPECT_DECLS;
  598. #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
  599. const char *fname[] = {
  600. svrCertFile, svrKeyFile, caCertFile,
  601. eccCertFile, eccKeyFile, eccRsaCertFile,
  602. cliCertFile, cliCertDerFile, cliKeyFile,
  603. dhParamFile,
  604. cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
  605. cliEdCertFile, cliEdKeyFile, caEdCertFile,
  606. NULL
  607. };
  608. const char derfile[] = "./certs/server-cert.der";
  609. XFILE f = XBADFILE;
  610. size_t sz;
  611. byte *buff = NULL;
  612. int i;
  613. ExpectTrue(XFOPEN("badfilename", "rb") == XBADFILE);
  614. for (i=0; EXPECT_SUCCESS() && fname[i] != NULL ; i++) {
  615. ExpectTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
  616. XFCLOSE(f);
  617. }
  618. ExpectTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
  619. ExpectTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  620. ExpectIntGE(sz = (size_t) XFTELL(f), sizeof_server_cert_der_2048);
  621. ExpectTrue(XFSEEK(f, 0, XSEEK_SET) == 0);
  622. ExpectTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL);
  623. ExpectTrue(XFREAD(buff, 1, sz, f) == sz);
  624. ExpectIntEQ(XMEMCMP(server_cert_der_2048, buff, sz), 0);
  625. XFREE(buff, NULL, DYNAMIC_TYPE_FILE);
  626. XFCLOSE(f);
  627. #endif
  628. return EXPECT_RESULT();
  629. }
  630. /*----------------------------------------------------------------------------*
  631. | Method Allocators
  632. *----------------------------------------------------------------------------*/
  633. static int test_wolfSSL_Method_Allocators(void)
  634. {
  635. EXPECT_DECLS;
  636. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  637. do { \
  638. WOLFSSL_METHOD *method = NULL; \
  639. condition(method = allocator()); \
  640. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  641. } while (0)
  642. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  643. TEST_METHOD_ALLOCATOR(a, ExpectNotNull)
  644. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  645. TEST_METHOD_ALLOCATOR(a, ExpectNull)
  646. #ifndef NO_OLD_TLS
  647. #ifdef WOLFSSL_ALLOW_SSLV3
  648. #ifndef NO_WOLFSSL_SERVER
  649. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  650. #endif
  651. #ifndef NO_WOLFSSL_CLIENT
  652. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  653. #endif
  654. #endif
  655. #ifdef WOLFSSL_ALLOW_TLSV10
  656. #ifndef NO_WOLFSSL_SERVER
  657. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  658. #endif
  659. #ifndef NO_WOLFSSL_CLIENT
  660. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  661. #endif
  662. #endif
  663. #ifndef NO_WOLFSSL_SERVER
  664. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  665. #endif
  666. #ifndef NO_WOLFSSL_CLIENT
  667. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  668. #endif
  669. #endif /* !NO_OLD_TLS */
  670. #ifndef WOLFSSL_NO_TLS12
  671. #ifndef NO_WOLFSSL_SERVER
  672. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  673. #endif
  674. #ifndef NO_WOLFSSL_CLIENT
  675. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  676. #endif
  677. #endif /* !WOLFSSL_NO_TLS12 */
  678. #ifdef WOLFSSL_TLS13
  679. #ifndef NO_WOLFSSL_SERVER
  680. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  681. #endif
  682. #ifndef NO_WOLFSSL_CLIENT
  683. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  684. #endif
  685. #endif /* WOLFSSL_TLS13 */
  686. #ifndef NO_WOLFSSL_SERVER
  687. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  688. #endif
  689. #ifndef NO_WOLFSSL_CLIENT
  690. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  691. #endif
  692. #ifdef WOLFSSL_DTLS
  693. #ifndef NO_OLD_TLS
  694. #ifndef NO_WOLFSSL_SERVER
  695. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  696. #endif
  697. #ifndef NO_WOLFSSL_CLIENT
  698. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  699. #endif
  700. #endif
  701. #ifndef WOLFSSL_NO_TLS12
  702. #ifndef NO_WOLFSSL_SERVER
  703. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  704. #endif
  705. #ifndef NO_WOLFSSL_CLIENT
  706. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  707. #endif
  708. #endif
  709. #endif /* WOLFSSL_DTLS */
  710. #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
  711. /* Stubs */
  712. #ifndef NO_WOLFSSL_SERVER
  713. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  714. #endif
  715. #ifndef NO_WOLFSSL_CLIENT
  716. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  717. #endif
  718. #endif
  719. /* Test Either Method (client or server) */
  720. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  721. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
  722. #ifndef NO_OLD_TLS
  723. #ifdef WOLFSSL_ALLOW_TLSV10
  724. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
  725. #endif
  726. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
  727. #endif /* !NO_OLD_TLS */
  728. #ifndef WOLFSSL_NO_TLS12
  729. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
  730. #endif /* !WOLFSSL_NO_TLS12 */
  731. #ifdef WOLFSSL_TLS13
  732. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
  733. #endif /* WOLFSSL_TLS13 */
  734. #ifdef WOLFSSL_DTLS
  735. TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
  736. #ifndef NO_OLD_TLS
  737. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
  738. #endif /* !NO_OLD_TLS */
  739. #ifndef WOLFSSL_NO_TLS12
  740. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
  741. #endif /* !WOLFSSL_NO_TLS12 */
  742. #endif /* WOLFSSL_DTLS */
  743. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  744. return EXPECT_RESULT();
  745. }
  746. #if defined(WOLFSSL_DUAL_ALG_CERTS) && !defined(NO_FILESYSTEM)
  747. /*----------------------------------------------------------------------------*
  748. | Dual algorithm Certificate Tests
  749. *----------------------------------------------------------------------------*/
  750. #define LARGE_TEMP_SZ 4096
  751. /* To better understand this, please see the X9.146 example in wolfssl-examples
  752. * repo. */
  753. static int do_dual_alg_root_certgen(byte **out, char *caKeyFile,
  754. char *sapkiFile, char *altPrivFile)
  755. {
  756. EXPECT_DECLS;
  757. FILE* file = NULL;
  758. Cert newCert;
  759. DecodedCert preTBS;
  760. byte caKeyBuf[LARGE_TEMP_SZ];
  761. word32 caKeySz = LARGE_TEMP_SZ;
  762. byte sapkiBuf[LARGE_TEMP_SZ];
  763. word32 sapkiSz = LARGE_TEMP_SZ;
  764. byte altPrivBuf[LARGE_TEMP_SZ];
  765. word32 altPrivSz = LARGE_TEMP_SZ;
  766. byte altSigAlgBuf[LARGE_TEMP_SZ];
  767. word32 altSigAlgSz = LARGE_TEMP_SZ;
  768. byte scratchBuf[LARGE_TEMP_SZ];
  769. word32 scratchSz = LARGE_TEMP_SZ;
  770. byte preTbsBuf[LARGE_TEMP_SZ];
  771. word32 preTbsSz = LARGE_TEMP_SZ;
  772. byte altSigValBuf[LARGE_TEMP_SZ];
  773. word32 altSigValSz = LARGE_TEMP_SZ;
  774. byte *outBuf = NULL;
  775. word32 outSz = LARGE_TEMP_SZ;
  776. WC_RNG rng;
  777. RsaKey caKey;
  778. ecc_key altCaKey;
  779. word32 idx = 0;
  780. ExpectNotNull(outBuf = (byte*)XMALLOC(outSz, NULL,
  781. DYNAMIC_TYPE_TMP_BUFFER));
  782. ExpectIntEQ(wc_InitRng(&rng), 0);
  783. XMEMSET(caKeyBuf, 0, caKeySz);
  784. ExpectNotNull(file = fopen(caKeyFile, "rb"));
  785. ExpectIntGT(caKeySz = (word32)fread(caKeyBuf, 1, caKeySz, file), 0);
  786. if (file) {
  787. fclose(file);
  788. file = NULL;
  789. }
  790. ExpectIntEQ(wc_InitRsaKey_ex(&caKey, NULL, INVALID_DEVID), 0);
  791. idx = 0;
  792. ExpectIntEQ(wc_RsaPrivateKeyDecode(caKeyBuf, &idx, &caKey, caKeySz),
  793. 0);
  794. XMEMSET(sapkiBuf, 0, sapkiSz);
  795. ExpectNotNull(file = fopen(sapkiFile, "rb"));
  796. ExpectIntGT(sapkiSz = (word32)fread(sapkiBuf, 1, sapkiSz, file), 0);
  797. if (file) {
  798. fclose(file);
  799. file = NULL;
  800. }
  801. XMEMSET(altPrivBuf, 0, altPrivSz);
  802. ExpectNotNull(file = fopen(altPrivFile, "rb"));
  803. ExpectIntGT(altPrivSz = (word32)fread(altPrivBuf, 1, altPrivSz, file), 0);
  804. if (file) {
  805. fclose(file);
  806. file = NULL;
  807. }
  808. wc_ecc_init(&altCaKey);
  809. idx = 0;
  810. ExpectIntEQ(wc_EccPrivateKeyDecode(altPrivBuf, &idx, &altCaKey,
  811. (word32)altPrivSz), 0);
  812. XMEMSET(altSigAlgBuf, 0, altSigAlgSz);
  813. ExpectIntGT(altSigAlgSz = SetAlgoID(CTC_SHA256wECDSA, altSigAlgBuf,
  814. oidSigType, 0), 0);
  815. wc_InitCert(&newCert);
  816. strncpy(newCert.subject.country, "US", CTC_NAME_SIZE);
  817. strncpy(newCert.subject.state, "MT", CTC_NAME_SIZE);
  818. strncpy(newCert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  819. strncpy(newCert.subject.org, "wolfSSL", CTC_NAME_SIZE);
  820. strncpy(newCert.subject.unit, "Engineering", CTC_NAME_SIZE);
  821. strncpy(newCert.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
  822. strncpy(newCert.subject.email, "root@wolfssl.com", CTC_NAME_SIZE);
  823. newCert.sigType = CTC_SHA256wRSA;
  824. newCert.isCA = 1;
  825. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "1.2.3.4.5",
  826. (const byte *)"This is NOT a critical extension", 32), 0);
  827. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.72", sapkiBuf,
  828. sapkiSz), 0);
  829. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.73", altSigAlgBuf,
  830. altSigAlgSz), 0);
  831. XMEMSET(scratchBuf, 0, scratchSz);
  832. ExpectIntGT(scratchSz = wc_MakeSelfCert(&newCert, scratchBuf, scratchSz,
  833. &caKey, &rng), 0);
  834. wc_InitDecodedCert(&preTBS, scratchBuf, scratchSz, 0);
  835. ExpectIntEQ(wc_ParseCert(&preTBS, CERT_TYPE, NO_VERIFY, NULL), 0);
  836. XMEMSET(preTbsBuf, 0, preTbsSz);
  837. ExpectIntGT(preTbsSz = wc_GeneratePreTBS(&preTBS, preTbsBuf, preTbsSz), 0);
  838. XMEMSET(altSigValBuf, 0, altSigValSz);
  839. ExpectIntGT(altSigValSz = wc_MakeSigWithBitStr(altSigValBuf, altSigValSz,
  840. CTC_SHA256wECDSA, preTbsBuf, preTbsSz, ECC_TYPE, &altCaKey,
  841. &rng), 0);
  842. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.74", altSigValBuf,
  843. altSigValSz), 0);
  844. /* Finally, generate the new certificate. */
  845. XMEMSET(outBuf, 0, outSz);
  846. ExpectIntGT(outSz = wc_MakeSelfCert(&newCert, outBuf, outSz, &caKey, &rng),
  847. 0);
  848. *out = outBuf;
  849. wc_FreeRsaKey(&caKey);
  850. wc_FreeRng(&rng);
  851. wc_FreeDecodedCert(&preTBS);
  852. return outSz;
  853. }
  854. static int do_dual_alg_server_certgen(byte **out, char *caKeyFile,
  855. char *sapkiFile, char *altPrivFile,
  856. char *serverKeyFile,
  857. byte *caCertBuf, int caCertSz)
  858. {
  859. EXPECT_DECLS;
  860. FILE* file = NULL;
  861. Cert newCert;
  862. DecodedCert preTBS;
  863. byte serverKeyBuf[LARGE_TEMP_SZ];
  864. word32 serverKeySz = LARGE_TEMP_SZ;
  865. byte caKeyBuf[LARGE_TEMP_SZ];
  866. word32 caKeySz = LARGE_TEMP_SZ;
  867. byte sapkiBuf[LARGE_TEMP_SZ];
  868. word32 sapkiSz = LARGE_TEMP_SZ;
  869. byte altPrivBuf[LARGE_TEMP_SZ];
  870. word32 altPrivSz = LARGE_TEMP_SZ;
  871. byte altSigAlgBuf[LARGE_TEMP_SZ];
  872. word32 altSigAlgSz = LARGE_TEMP_SZ;
  873. byte scratchBuf[LARGE_TEMP_SZ];
  874. word32 scratchSz = LARGE_TEMP_SZ;
  875. byte preTbsBuf[LARGE_TEMP_SZ];
  876. word32 preTbsSz = LARGE_TEMP_SZ;
  877. byte altSigValBuf[LARGE_TEMP_SZ];
  878. word32 altSigValSz = LARGE_TEMP_SZ;
  879. byte *outBuf = NULL;
  880. word32 outSz = LARGE_TEMP_SZ;
  881. WC_RNG rng;
  882. RsaKey caKey;
  883. RsaKey serverKey;
  884. ecc_key altCaKey;
  885. word32 idx = 0;
  886. ExpectNotNull(outBuf = (byte*)XMALLOC(outSz, NULL,
  887. DYNAMIC_TYPE_TMP_BUFFER));
  888. ExpectIntEQ(wc_InitRng(&rng), 0);
  889. XMEMSET(serverKeyBuf, 0, serverKeySz);
  890. ExpectNotNull(file = fopen(serverKeyFile, "rb"));
  891. ExpectIntGT(serverKeySz = (word32)fread(serverKeyBuf, 1, serverKeySz, file),
  892. 0);
  893. if (file) {
  894. fclose(file);
  895. file = NULL;
  896. }
  897. ExpectIntEQ(wc_InitRsaKey_ex(&serverKey, NULL, INVALID_DEVID), 0);
  898. idx = 0;
  899. ExpectIntEQ(wc_RsaPrivateKeyDecode(serverKeyBuf, &idx, &serverKey,
  900. (word32)serverKeySz), 0);
  901. XMEMSET(caKeyBuf, 0, caKeySz);
  902. ExpectNotNull(file = fopen(caKeyFile, "rb"));
  903. ExpectIntGT(caKeySz = (word32)fread(caKeyBuf, 1, caKeySz, file), 0);
  904. if (file) {
  905. fclose(file);
  906. file = NULL;
  907. }
  908. ExpectIntEQ(wc_InitRsaKey_ex(&caKey, NULL, INVALID_DEVID), 0);
  909. idx = 0;
  910. ExpectIntEQ(wc_RsaPrivateKeyDecode(caKeyBuf, &idx, &caKey,
  911. (word32)caKeySz), 0);
  912. XMEMSET(sapkiBuf, 0, sapkiSz);
  913. ExpectNotNull(file = fopen(sapkiFile, "rb"));
  914. ExpectIntGT(sapkiSz = (word32)fread(sapkiBuf, 1, sapkiSz, file), 0);
  915. if (file) {
  916. fclose(file);
  917. file = NULL;
  918. }
  919. XMEMSET(altPrivBuf, 0, altPrivSz);
  920. ExpectNotNull(file = fopen(altPrivFile, "rb"));
  921. ExpectIntGT(altPrivSz = (word32)fread(altPrivBuf, 1, altPrivSz, file), 0);
  922. if (file) {
  923. fclose(file);
  924. file = NULL;
  925. }
  926. wc_ecc_init(&altCaKey);
  927. idx = 0;
  928. ExpectIntEQ(wc_EccPrivateKeyDecode(altPrivBuf, &idx, &altCaKey,
  929. (word32)altPrivSz), 0);
  930. XMEMSET(altSigAlgBuf, 0, altSigAlgSz);
  931. ExpectIntGT(altSigAlgSz = SetAlgoID(CTC_SHA256wECDSA, altSigAlgBuf,
  932. oidSigType, 0), 0);
  933. wc_InitCert(&newCert);
  934. strncpy(newCert.subject.country, "US", CTC_NAME_SIZE);
  935. strncpy(newCert.subject.state, "MT", CTC_NAME_SIZE);
  936. strncpy(newCert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  937. strncpy(newCert.subject.org, "wolfSSL", CTC_NAME_SIZE);
  938. strncpy(newCert.subject.unit, "Engineering", CTC_NAME_SIZE);
  939. strncpy(newCert.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
  940. strncpy(newCert.subject.email, "server@wolfssl.com", CTC_NAME_SIZE);
  941. newCert.sigType = CTC_SHA256wRSA;
  942. newCert.isCA = 0;
  943. ExpectIntEQ(wc_SetIssuerBuffer(&newCert, caCertBuf, caCertSz), 0);
  944. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "1.2.3.4.5",
  945. (const byte *)"This is NOT a critical extension", 32), 0);
  946. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.72", sapkiBuf,
  947. sapkiSz), 0);
  948. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.73", altSigAlgBuf,
  949. altSigAlgSz), 0);
  950. XMEMSET(scratchBuf, 0, scratchSz);
  951. ExpectIntGT(wc_MakeCert(&newCert, scratchBuf, scratchSz, &serverKey, NULL,
  952. &rng), 0);
  953. ExpectIntGT(scratchSz = wc_SignCert(newCert.bodySz, newCert.sigType,
  954. scratchBuf, scratchSz, &caKey, NULL, &rng), 0);
  955. wc_InitDecodedCert(&preTBS, scratchBuf, scratchSz, 0);
  956. ExpectIntEQ(wc_ParseCert(&preTBS, CERT_TYPE, NO_VERIFY, NULL), 0);
  957. XMEMSET(preTbsBuf, 0, preTbsSz);
  958. ExpectIntGT(preTbsSz = wc_GeneratePreTBS(&preTBS, preTbsBuf, preTbsSz), 0);
  959. XMEMSET(altSigValBuf, 0, altSigValSz);
  960. ExpectIntGT(altSigValSz = wc_MakeSigWithBitStr(altSigValBuf, altSigValSz,
  961. CTC_SHA256wECDSA, preTbsBuf, preTbsSz, ECC_TYPE, &altCaKey,
  962. &rng), 0);
  963. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.74",
  964. altSigValBuf, altSigValSz), 0);
  965. /* Finally, generate the new certificate. */
  966. XMEMSET(outBuf, 0, outSz);
  967. ExpectIntGT(wc_MakeCert(&newCert, outBuf, outSz, &serverKey, NULL, &rng),
  968. 0);
  969. ExpectIntGT(outSz = wc_SignCert(newCert.bodySz, newCert.sigType, outBuf,
  970. outSz, &caKey, NULL, &rng), 0);
  971. *out = outBuf;
  972. wc_FreeRsaKey(&caKey);
  973. wc_FreeRsaKey(&serverKey);
  974. wc_FreeRng(&rng);
  975. wc_FreeDecodedCert(&preTBS);
  976. return outSz;
  977. }
  978. static int do_dual_alg_tls13_connection(byte *caCert, word32 caCertSz,
  979. byte *serverCert, word32 serverCertSz,
  980. byte *serverKey, word32 serverKeySz,
  981. int negative_test)
  982. {
  983. EXPECT_DECLS;
  984. WOLFSSL_CTX *ctx_c = NULL;
  985. WOLFSSL_CTX *ctx_s = NULL;
  986. WOLFSSL *ssl_c = NULL;
  987. WOLFSSL *ssl_s = NULL;
  988. struct test_memio_ctx test_ctx;
  989. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  990. ExpectIntEQ(test_memio_setup_ex(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  991. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  992. caCert, caCertSz, serverCert, serverCertSz,
  993. serverKey, serverKeySz), 0);
  994. if (negative_test) {
  995. ExpectTrue(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0);
  996. }
  997. else {
  998. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  999. }
  1000. wolfSSL_free(ssl_c);
  1001. wolfSSL_free(ssl_s);
  1002. wolfSSL_CTX_free(ctx_c);
  1003. wolfSSL_CTX_free(ctx_s);
  1004. return EXPECT_RESULT();
  1005. }
  1006. static int extCount = 0;
  1007. static int myUnknownExtCallback(const word16* oid, word32 oidSz, int crit,
  1008. const unsigned char* der, word32 derSz)
  1009. {
  1010. (void) oid;
  1011. (void) oidSz;
  1012. (void) crit;
  1013. (void) der;
  1014. (void) derSz;
  1015. extCount ++;
  1016. /* Accept all extensions. This is only a test. Normally we would be much more
  1017. * careful about critical extensions. */
  1018. return 1;
  1019. }
  1020. static int test_dual_alg_support(void)
  1021. {
  1022. EXPECT_DECLS;
  1023. /* Root CA and server keys will be the same. This is only appropriate for
  1024. * testing. */
  1025. char keyFile[] = "./certs/ca-key.der";
  1026. char sapkiFile[] = "./certs/ecc-keyPub.der";
  1027. char altPrivFile[] = "./certs/ecc-key.der";
  1028. char wrongPrivFile[] = "./certs/ecc-client-key.der";
  1029. byte *serverKey = NULL;
  1030. size_t serverKeySz = 0;
  1031. byte *root = NULL;
  1032. int rootSz = 0;
  1033. byte *server = NULL;
  1034. int serverSz = 0;
  1035. WOLFSSL_CERT_MANAGER* cm = NULL;
  1036. ExpectIntEQ(load_file(keyFile, &serverKey, &serverKeySz), 0);
  1037. /* Base normal case. */
  1038. rootSz = do_dual_alg_root_certgen(&root, keyFile, sapkiFile, altPrivFile);
  1039. ExpectNotNull(root);
  1040. ExpectIntGT(rootSz, 0);
  1041. serverSz = do_dual_alg_server_certgen(&server, keyFile, sapkiFile,
  1042. altPrivFile, keyFile, root, rootSz);
  1043. ExpectNotNull(server);
  1044. ExpectIntGT(serverSz, 0);
  1045. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1046. server, serverSz, serverKey, (word32)serverKeySz, 0),
  1047. TEST_SUCCESS);
  1048. XFREE(root, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1049. XFREE(server, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1050. /* Now we try a negative case. Note that we use wrongPrivFile to generate
  1051. * the alternative signature and then set negative_test to true for the
  1052. * call to do_dual_alg_tls13_connection(). Its expecting a failed connection
  1053. * because the signature won't verify. The exception is if
  1054. * WOLFSSL_TRUST_PEER_CERT is defined. In that case, no verfication happens
  1055. * and this is no longer a negative test. */
  1056. rootSz = do_dual_alg_root_certgen(&root, keyFile, sapkiFile, wrongPrivFile);
  1057. ExpectNotNull(root);
  1058. ExpectIntGT(rootSz, 0);
  1059. serverSz = do_dual_alg_server_certgen(&server, keyFile, sapkiFile,
  1060. wrongPrivFile, keyFile, root, rootSz);
  1061. ExpectNotNull(server);
  1062. ExpectIntGT(serverSz, 0);
  1063. #ifdef WOLFSSL_TRUST_PEER_CERT
  1064. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1065. server, serverSz, serverKey, (word32)serverKeySz, 0),
  1066. TEST_SUCCESS);
  1067. #else
  1068. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1069. server, serverSz, serverKey, (word32)serverKeySz, 1),
  1070. TEST_SUCCESS);
  1071. #endif
  1072. /* Lets see if CertManager can find the new extensions */
  1073. extCount = 0;
  1074. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  1075. wolfSSL_CertManagerSetUnknownExtCallback(cm, myUnknownExtCallback);
  1076. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, root, rootSz,
  1077. SSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1078. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, server, serverSz,
  1079. SSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1080. /* There is only 1 unknown extension (1.2.3.4.5). The other ones are known
  1081. * because they are for the dual alg extensions. */
  1082. ExpectIntEQ(extCount, 1);
  1083. wolfSSL_CertManagerFree(cm);
  1084. XFREE(root, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1085. XFREE(server, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1086. free(serverKey);
  1087. return EXPECT_RESULT();
  1088. }
  1089. #else
  1090. static int test_dual_alg_support(void)
  1091. {
  1092. return TEST_SKIPPED;
  1093. }
  1094. #endif /* WOLFSSL_DUAL_ALG_CERTS && !NO_FILESYSTEM */
  1095. /*----------------------------------------------------------------------------*
  1096. | Context
  1097. *----------------------------------------------------------------------------*/
  1098. #ifndef NO_WOLFSSL_SERVER
  1099. static int test_wolfSSL_CTX_new(void)
  1100. {
  1101. EXPECT_DECLS;
  1102. WOLFSSL_CTX *ctx;
  1103. WOLFSSL_METHOD* method;
  1104. ExpectNull(ctx = wolfSSL_CTX_new(NULL));
  1105. ExpectNotNull(method = wolfSSLv23_server_method());
  1106. ExpectNotNull(ctx = wolfSSL_CTX_new(method));
  1107. wolfSSL_CTX_free(ctx);
  1108. return EXPECT_RESULT();
  1109. }
  1110. #endif
  1111. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  1112. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  1113. static int test_for_double_Free(void)
  1114. {
  1115. EXPECT_DECLS;
  1116. WOLFSSL_CTX* ctx = NULL;
  1117. WOLFSSL* ssl = NULL;
  1118. int skipTest = 0;
  1119. const char* testCertFile;
  1120. const char* testKeyFile;
  1121. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  1122. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  1123. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  1124. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  1125. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  1126. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  1127. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  1128. "NULL-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  1129. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  1130. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  1131. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  1132. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  1133. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  1134. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  1135. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  1136. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  1137. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  1138. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  1139. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  1140. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  1141. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  1142. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  1143. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  1144. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  1145. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  1146. "LY1305-OLD:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  1147. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  1148. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  1149. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  1150. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  1151. /* OpenVPN uses a "blacklist" method to specify which ciphers NOT to use */
  1152. #ifdef OPENSSL_EXTRA
  1153. char openvpnCiphers[] = "DEFAULT:!EXP:!LOW:!MEDIUM:!kDH:!kECDH:!DSS:!PSK:"
  1154. "!SRP:!kRSA:!aNULL:!eNULL";
  1155. #endif
  1156. #ifndef NO_RSA
  1157. testCertFile = svrCertFile;
  1158. testKeyFile = svrKeyFile;
  1159. #elif defined(HAVE_ECC)
  1160. testCertFile = eccCertFile;
  1161. testKeyFile = eccKeyFile;
  1162. #else
  1163. skipTest = 1;
  1164. #endif
  1165. if (skipTest != 1) {
  1166. #ifndef NO_WOLFSSL_SERVER
  1167. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1168. #else
  1169. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1170. #endif
  1171. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1172. WOLFSSL_FILETYPE_PEM));
  1173. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1174. WOLFSSL_FILETYPE_PEM));
  1175. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1176. /* First test freeing SSL, then CTX */
  1177. wolfSSL_free(ssl);
  1178. ssl = NULL;
  1179. wolfSSL_CTX_free(ctx);
  1180. ctx = NULL;
  1181. #ifndef NO_WOLFSSL_CLIENT
  1182. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1183. #else
  1184. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1185. #endif
  1186. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1187. WOLFSSL_FILETYPE_PEM));
  1188. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1189. WOLFSSL_FILETYPE_PEM));
  1190. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1191. /* Next test freeing CTX then SSL */
  1192. wolfSSL_CTX_free(ctx);
  1193. ctx = NULL;
  1194. wolfSSL_free(ssl);
  1195. ssl = NULL;
  1196. #ifndef NO_WOLFSSL_SERVER
  1197. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1198. #else
  1199. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1200. #endif
  1201. /* Test setting ciphers at ctx level */
  1202. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1203. WOLFSSL_FILETYPE_PEM));
  1204. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1205. WOLFSSL_FILETYPE_PEM));
  1206. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  1207. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  1208. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  1209. /* only update TLSv13 suites */
  1210. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
  1211. #endif
  1212. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  1213. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  1214. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  1215. /* only update pre-TLSv13 suites */
  1216. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx,
  1217. "ECDHE-RSA-AES128-GCM-SHA256"));
  1218. #endif
  1219. #ifdef OPENSSL_EXTRA
  1220. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, openvpnCiphers));
  1221. #endif
  1222. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1223. wolfSSL_CTX_free(ctx);
  1224. ctx = NULL;
  1225. wolfSSL_free(ssl);
  1226. ssl = NULL;
  1227. #ifndef NO_WOLFSSL_CLIENT
  1228. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1229. #else
  1230. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1231. #endif
  1232. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1233. WOLFSSL_FILETYPE_PEM));
  1234. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1235. WOLFSSL_FILETYPE_PEM));
  1236. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1237. /* test setting ciphers at SSL level */
  1238. ExpectTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  1239. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  1240. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  1241. /* only update TLSv13 suites */
  1242. ExpectTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
  1243. #endif
  1244. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  1245. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  1246. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  1247. /* only update pre-TLSv13 suites */
  1248. ExpectTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
  1249. #endif
  1250. wolfSSL_CTX_free(ctx);
  1251. ctx = NULL;
  1252. wolfSSL_free(ssl);
  1253. ssl = NULL;
  1254. }
  1255. return EXPECT_RESULT();
  1256. }
  1257. #endif
  1258. static int test_wolfSSL_CTX_set_cipher_list_bytes(void)
  1259. {
  1260. EXPECT_DECLS;
  1261. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)) && \
  1262. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  1263. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  1264. const char* testCertFile;
  1265. const char* testKeyFile;
  1266. WOLFSSL_CTX* ctx = NULL;
  1267. WOLFSSL* ssl = NULL;
  1268. const byte cipherList[] =
  1269. {
  1270. /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x16,
  1271. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x39,
  1272. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x33,
  1273. /* TLS_DH_anon_WITH_AES_128_CBC_SHA */ 0xC0, 0x34,
  1274. /* TLS_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x35,
  1275. /* TLS_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x2F,
  1276. /* TLS_RSA_WITH_NULL_MD5 */ 0xC0, 0x01,
  1277. /* TLS_RSA_WITH_NULL_SHA */ 0xC0, 0x02,
  1278. /* TLS_PSK_WITH_AES_256_CBC_SHA */ 0xC0, 0x8d,
  1279. /* TLS_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0xae,
  1280. /* TLS_PSK_WITH_AES_256_CBC_SHA384 */ 0xC0, 0xaf,
  1281. /* TLS_PSK_WITH_AES_128_CBC_SHA */ 0xC0, 0x8c,
  1282. /* TLS_PSK_WITH_NULL_SHA256 */ 0xC0, 0xb0,
  1283. /* TLS_PSK_WITH_NULL_SHA384 */ 0xC0, 0xb1,
  1284. /* TLS_PSK_WITH_NULL_SHA */ 0xC0, 0x2c,
  1285. /* SSL_RSA_WITH_RC4_128_SHA */ 0xC0, 0x05,
  1286. /* SSL_RSA_WITH_RC4_128_MD5 */ 0xC0, 0x04,
  1287. /* SSL_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0A,
  1288. /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
  1289. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x14,
  1290. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x13,
  1291. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0A,
  1292. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x09,
  1293. /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */ 0xC0, 0x11,
  1294. /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x07,
  1295. /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x12,
  1296. /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x08,
  1297. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x27,
  1298. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256*/ 0xC0, 0x23,
  1299. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x28,
  1300. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384*/ 0xC0, 0x24,
  1301. /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */ 0xC0, 0x06,
  1302. /* TLS_ECDHE_PSK_WITH_NULL_SHA256 */ 0xC0, 0x3a,
  1303. /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x37,
  1304. /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
  1305. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0F,
  1306. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x0E,
  1307. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x05,
  1308. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x04,
  1309. /* TLS_ECDH_RSA_WITH_RC4_128_SHA */ 0xC0, 0x0C,
  1310. /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x02,
  1311. /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0D,
  1312. /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x03,
  1313. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x29,
  1314. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x25,
  1315. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x2A,
  1316. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x26,
  1317. /* WDM_WITH_NULL_SHA256 */ 0x00, 0xFE, /* wolfSSL DTLS Multicast */
  1318. /* SHA256 */
  1319. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x6b,
  1320. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x67,
  1321. /* TLS_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x3d,
  1322. /* TLS_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x3c,
  1323. /* TLS_RSA_WITH_NULL_SHA256 */ 0x00, 0x3b,
  1324. /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 */ 0x00, 0xb2,
  1325. /* TLS_DHE_PSK_WITH_NULL_SHA256 */ 0x00, 0xb4,
  1326. /* SHA384 */
  1327. /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 */ 0x00, 0xb3,
  1328. /* TLS_DHE_PSK_WITH_NULL_SHA384 */ 0x00, 0xb5,
  1329. /* AES-GCM */
  1330. /* TLS_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9c,
  1331. /* TLS_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9d,
  1332. /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9e,
  1333. /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9f,
  1334. /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa7,
  1335. /* TLS_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xa8,
  1336. /* TLS_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa9,
  1337. /* TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xaa,
  1338. /* TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xab,
  1339. /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
  1340. /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2b,
  1341. /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2c,
  1342. /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2d,
  1343. /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2e,
  1344. /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2f,
  1345. /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x30,
  1346. /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x31,
  1347. /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x32,
  1348. /* AES-CCM, first byte is 0xC0 but isn't ECC,
  1349. * also, in some of the other AES-CCM suites
  1350. * there will be second byte number conflicts
  1351. * with non-ECC AES-GCM */
  1352. /* TLS_RSA_WITH_AES_128_CCM_8 */ 0xC0, 0xa0,
  1353. /* TLS_RSA_WITH_AES_256_CCM_8 */ 0xC0, 0xa1,
  1354. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM */ 0xC0, 0xac,
  1355. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */ 0xC0, 0xae,
  1356. /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 */ 0xC0, 0xaf,
  1357. /* TLS_PSK_WITH_AES_128_CCM */ 0xC0, 0xa4,
  1358. /* TLS_PSK_WITH_AES_256_CCM */ 0xC0, 0xa5,
  1359. /* TLS_PSK_WITH_AES_128_CCM_8 */ 0xC0, 0xa8,
  1360. /* TLS_PSK_WITH_AES_256_CCM_8 */ 0xC0, 0xa9,
  1361. /* TLS_DHE_PSK_WITH_AES_128_CCM */ 0xC0, 0xa6,
  1362. /* TLS_DHE_PSK_WITH_AES_256_CCM */ 0xC0, 0xa7,
  1363. /* Camellia */
  1364. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x41,
  1365. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x84,
  1366. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xba,
  1367. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc0,
  1368. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x45,
  1369. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x88,
  1370. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xbe,
  1371. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc4,
  1372. /* chacha20-poly1305 suites first byte is 0xCC (CHACHA_BYTE) */
  1373. /* TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa8,
  1374. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa9,
  1375. /* TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xaa,
  1376. /* TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xac,
  1377. /* TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xab,
  1378. /* TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xad,
  1379. /* chacha20-poly1305 earlier version of nonce and padding (CHACHA_BYTE) */
  1380. /* TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x13,
  1381. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x14,
  1382. /* TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x15,
  1383. /* ECDHE_PSK RFC8442, first byte is 0xD0 (ECDHE_PSK_BYTE) */
  1384. /* TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 */ 0xD0, 0x01,
  1385. /* TLS v1.3 cipher suites */
  1386. /* TLS_AES_128_GCM_SHA256 */ 0x13, 0x01,
  1387. /* TLS_AES_256_GCM_SHA384 */ 0x13, 0x02,
  1388. /* TLS_CHACHA20_POLY1305_SHA256 */ 0x13, 0x03,
  1389. /* TLS_AES_128_CCM_SHA256 */ 0x13, 0x04,
  1390. /* TLS_AES_128_CCM_8_SHA256 */ 0x13, 0x05,
  1391. /* TLS v1.3 Integrity only cipher suites - 0xC0 (ECC) first byte */
  1392. /* TLS_SHA256_SHA256 */ 0xC0, 0xB4,
  1393. /* TLS_SHA384_SHA384 */ 0xC0, 0xB5
  1394. };
  1395. #ifndef NO_RSA
  1396. testCertFile = svrCertFile;
  1397. testKeyFile = svrKeyFile;
  1398. #elif defined(HAVE_ECC)
  1399. testCertFile = eccCertFile;
  1400. testKeyFile = eccKeyFile;
  1401. #endif
  1402. #ifndef NO_WOLFSSL_SERVER
  1403. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1404. #else
  1405. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1406. #endif
  1407. ExpectTrue(wolfSSL_CTX_set_cipher_list_bytes(ctx, &cipherList[0U],
  1408. sizeof(cipherList)));
  1409. wolfSSL_CTX_free(ctx);
  1410. ctx = NULL;
  1411. #ifndef NO_WOLFSSL_SERVER
  1412. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1413. #else
  1414. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1415. #endif
  1416. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1417. WOLFSSL_FILETYPE_PEM));
  1418. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1419. WOLFSSL_FILETYPE_PEM));
  1420. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1421. ExpectTrue(wolfSSL_set_cipher_list_bytes(ssl, &cipherList[0U],
  1422. sizeof(cipherList)));
  1423. wolfSSL_free(ssl);
  1424. wolfSSL_CTX_free(ctx);
  1425. #endif /* (OPENSSL_EXTRA || WOLFSSL_SET_CIPHER_BYTES) &&
  1426. (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && (!NO_RSA || HAVE_ECC) */
  1427. return EXPECT_RESULT();
  1428. }
  1429. static int test_wolfSSL_CTX_use_certificate_file(void)
  1430. {
  1431. EXPECT_DECLS;
  1432. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1433. WOLFSSL_CTX *ctx = NULL;
  1434. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1435. /* invalid context */
  1436. ExpectFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  1437. WOLFSSL_FILETYPE_PEM));
  1438. /* invalid cert file */
  1439. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  1440. WOLFSSL_FILETYPE_PEM));
  1441. /* invalid cert type */
  1442. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  1443. #ifdef NO_RSA
  1444. /* rsa needed */
  1445. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1446. WOLFSSL_FILETYPE_PEM));
  1447. #else
  1448. /* success */
  1449. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1450. WOLFSSL_FILETYPE_PEM));
  1451. #endif
  1452. wolfSSL_CTX_free(ctx);
  1453. #endif
  1454. return EXPECT_RESULT();
  1455. }
  1456. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  1457. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  1458. {
  1459. EXPECT_DECLS;
  1460. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  1461. WOLFSSL_CTX* ctx = NULL;
  1462. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1463. ExpectIntEQ(SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  1464. server_cert_der_2048), WOLFSSL_SUCCESS);
  1465. wolfSSL_CTX_free(ctx);
  1466. #endif
  1467. return EXPECT_RESULT();
  1468. }
  1469. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  1470. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  1471. * context using buffer.
  1472. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  1473. * --enable-testcert flag.
  1474. */
  1475. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  1476. {
  1477. EXPECT_DECLS;
  1478. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  1479. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  1480. WOLFSSL_CTX* ctx = NULL;
  1481. int ret;
  1482. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1483. ExpectIntEQ(ret = wolfSSL_CTX_use_certificate_buffer(ctx,
  1484. server_cert_der_2048, sizeof_server_cert_der_2048,
  1485. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1486. wolfSSL_CTX_free(ctx);
  1487. #endif
  1488. return EXPECT_RESULT();
  1489. } /* END test_wolfSSL_CTX_use_certificate_buffer */
  1490. static int test_wolfSSL_CTX_use_PrivateKey_file(void)
  1491. {
  1492. EXPECT_DECLS;
  1493. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1494. WOLFSSL_CTX *ctx = NULL;
  1495. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1496. /* invalid context */
  1497. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  1498. WOLFSSL_FILETYPE_PEM));
  1499. /* invalid key file */
  1500. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  1501. WOLFSSL_FILETYPE_PEM));
  1502. /* invalid key type */
  1503. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  1504. /* success */
  1505. #ifdef NO_RSA
  1506. /* rsa needed */
  1507. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1508. WOLFSSL_FILETYPE_PEM));
  1509. #else
  1510. /* success */
  1511. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1512. WOLFSSL_FILETYPE_PEM));
  1513. #endif
  1514. wolfSSL_CTX_free(ctx);
  1515. #endif
  1516. return EXPECT_RESULT();
  1517. }
  1518. /* test both file and buffer versions along with unloading trusted peer certs */
  1519. static int test_wolfSSL_CTX_trust_peer_cert(void)
  1520. {
  1521. EXPECT_DECLS;
  1522. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1523. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_RSA)
  1524. WOLFSSL_CTX *ctx = NULL;
  1525. WOLFSSL* ssl = NULL;
  1526. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1527. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1528. #if !defined(NO_FILESYSTEM)
  1529. /* invalid file */
  1530. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  1531. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1532. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  1533. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1534. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1535. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1536. /* success */
  1537. ExpectIntEQ(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1538. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1539. /* unload cert */
  1540. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1541. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1542. /* invalid file */
  1543. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, NULL,
  1544. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1545. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, bogusFile,
  1546. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1547. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1548. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1549. /* success */
  1550. ExpectIntEQ(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1551. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1552. #ifdef WOLFSSL_LOCAL_X509_STORE
  1553. /* unload cert */
  1554. ExpectIntNE(wolfSSL_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1555. ExpectIntEQ(wolfSSL_Unload_trust_peers(ssl), WOLFSSL_SUCCESS);
  1556. #endif
  1557. #endif
  1558. /* Test of loading certs from buffers */
  1559. /* invalid buffer */
  1560. ExpectIntNE(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  1561. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1562. /* success */
  1563. #ifdef USE_CERT_BUFFERS_1024
  1564. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  1565. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1566. #endif
  1567. #ifdef USE_CERT_BUFFERS_2048
  1568. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  1569. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1570. #endif
  1571. /* unload cert */
  1572. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1573. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1574. wolfSSL_free(ssl);
  1575. wolfSSL_CTX_free(ctx);
  1576. #endif
  1577. return EXPECT_RESULT();
  1578. }
  1579. static int test_wolfSSL_CTX_load_verify_locations(void)
  1580. {
  1581. EXPECT_DECLS;
  1582. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  1583. WOLFSSL_CTX *ctx = NULL;
  1584. #ifndef NO_RSA
  1585. WOLFSSL_CERT_MANAGER* cm = NULL;
  1586. #ifdef PERSIST_CERT_CACHE
  1587. int cacheSz = 0;
  1588. unsigned char* cache = NULL;
  1589. int used = 0;
  1590. #ifndef NO_FILESYSTEM
  1591. const char* cacheFile = "./tests/cert_cache.tmp";
  1592. #endif
  1593. int i;
  1594. int t;
  1595. int* p;
  1596. #endif
  1597. #endif
  1598. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1599. const char* load_certs_path = "./certs/external";
  1600. const char* load_no_certs_path = "./examples";
  1601. const char* load_expired_path = "./certs/test/expired";
  1602. #endif
  1603. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1604. /* invalid arguments */
  1605. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL),
  1606. WOLFSSL_FAILURE);
  1607. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL),
  1608. WOLFSSL_FAILURE);
  1609. /* invalid ca file */
  1610. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  1611. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  1612. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  1613. ((defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)) && \
  1614. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  1615. /* invalid path */
  1616. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  1617. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  1618. #endif
  1619. #if defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)
  1620. /* test ignoring the invalid path */
  1621. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, bogusFile,
  1622. WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR), WOLFSSL_SUCCESS);
  1623. #endif
  1624. /* load ca cert */
  1625. #ifdef NO_RSA
  1626. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1627. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  1628. #else /* Skip the following test without RSA certs. */
  1629. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1630. WOLFSSL_SUCCESS);
  1631. #ifdef PERSIST_CERT_CACHE
  1632. /* Get cert cache size */
  1633. ExpectIntGT(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), 0);
  1634. ExpectNotNull(cache = (byte*)XMALLOC(cacheSz, NULL,
  1635. DYNAMIC_TYPE_TMP_BUFFER));
  1636. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, NULL),
  1637. BAD_FUNC_ARG);
  1638. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, -1, NULL),
  1639. BAD_FUNC_ARG);
  1640. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, -1, NULL),
  1641. BAD_FUNC_ARG);
  1642. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, cacheSz, NULL),
  1643. BAD_FUNC_ARG);
  1644. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, &used),
  1645. BAD_FUNC_ARG);
  1646. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, cacheSz, &used),
  1647. BAD_FUNC_ARG);
  1648. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, cacheSz, &used),
  1649. BAD_FUNC_ARG);
  1650. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, -1, &used),
  1651. BAD_FUNC_ARG);
  1652. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, NULL),
  1653. BAD_FUNC_ARG);
  1654. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz - 10, &used),
  1655. BUFFER_E);
  1656. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, &used), 1);
  1657. ExpectIntEQ(cacheSz, used);
  1658. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, -1),
  1659. BAD_FUNC_ARG);
  1660. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, -1),
  1661. BAD_FUNC_ARG);
  1662. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, -1),
  1663. BAD_FUNC_ARG);
  1664. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, cacheSz),
  1665. BAD_FUNC_ARG);
  1666. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, cacheSz),
  1667. BAD_FUNC_ARG);
  1668. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, cacheSz),
  1669. BAD_FUNC_ARG);
  1670. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, -1),
  1671. BAD_FUNC_ARG);
  1672. /* Smaller than header. */
  1673. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, 1), BUFFER_E);
  1674. for (i = 1; i < cacheSz; i++) {
  1675. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz - i),
  1676. BUFFER_E);
  1677. }
  1678. if (EXPECT_SUCCESS()) {
  1679. /* Modify header for bad results! */
  1680. p = (int*)cache;
  1681. /* version */
  1682. t = p[0]; p[0] = 0xff;
  1683. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1684. CACHE_MATCH_ERROR);
  1685. p[0] = t; p++;
  1686. /* rows */
  1687. t = p[0]; p[0] = 0xff;
  1688. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1689. CACHE_MATCH_ERROR);
  1690. p[0] = t; p++;
  1691. /* columns[0] */
  1692. t = p[0]; p[0] = -1;
  1693. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1694. PARSE_ERROR);
  1695. p[0] = t; p += CA_TABLE_SIZE;
  1696. /* signerSz*/
  1697. t = p[0]; p[0] = 0xff;
  1698. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1699. CACHE_MATCH_ERROR);
  1700. p[0] = t;
  1701. }
  1702. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz), 1);
  1703. ExpectIntEQ(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), used);
  1704. #ifndef NO_FILESYSTEM
  1705. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1706. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1707. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1708. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, cacheFile), 1);
  1709. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1710. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1711. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1712. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "no-file"),
  1713. WOLFSSL_BAD_FILE);
  1714. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, cacheFile), 1);
  1715. /* File contents is not a cache. */
  1716. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "./certs/ca-cert.pem"),
  1717. CACHE_MATCH_ERROR);
  1718. #endif
  1719. XFREE(cache, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1720. #endif
  1721. /* Test unloading CA's */
  1722. ExpectIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  1723. #ifdef PERSIST_CERT_CACHE
  1724. /* Verify no certs (result is less than cacheSz) */
  1725. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1726. #endif
  1727. /* load ca cert again */
  1728. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1729. WOLFSSL_SUCCESS);
  1730. /* Test getting CERT_MANAGER */
  1731. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  1732. /* Test unloading CA's using CM */
  1733. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  1734. #ifdef PERSIST_CERT_CACHE
  1735. /* Verify no certs (result is less than cacheSz) */
  1736. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1737. #endif
  1738. #endif
  1739. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1740. /* Test loading CA certificates using a path */
  1741. #ifdef NO_RSA
  1742. /* failure here okay since certs in external directory are RSA */
  1743. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1744. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1745. #else
  1746. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1747. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1748. #endif
  1749. /* Test loading path with no files */
  1750. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1751. load_no_certs_path, WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  1752. /* Test loading expired CA certificates */
  1753. #ifdef NO_RSA
  1754. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1755. load_expired_path,
  1756. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1757. WOLFSSL_SUCCESS);
  1758. #else
  1759. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1760. load_expired_path,
  1761. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1762. WOLFSSL_SUCCESS);
  1763. #endif
  1764. /* Test loading CA certificates and ignoring all errors */
  1765. #ifdef NO_RSA
  1766. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1767. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  1768. #else
  1769. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1770. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  1771. #endif
  1772. #endif
  1773. wolfSSL_CTX_free(ctx);
  1774. #endif
  1775. return EXPECT_RESULT();
  1776. }
  1777. static int test_wolfSSL_CTX_load_system_CA_certs(void)
  1778. {
  1779. int res = TEST_SKIPPED;
  1780. #if defined(WOLFSSL_SYS_CA_CERTS) && !defined(NO_WOLFSSL_CLIENT) && \
  1781. (!defined(NO_RSA) || defined(HAVE_ECC))
  1782. WOLFSSL_CTX* ctx;
  1783. byte dirValid = 0;
  1784. int ret = 0;
  1785. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1786. if (ctx == NULL) {
  1787. fprintf(stderr, "wolfSSL_CTX_new failed.\n");
  1788. ret = -1;
  1789. }
  1790. if (ret == 0) {
  1791. #if defined(USE_WINDOWS_API) || defined(__APPLE__)
  1792. dirValid = 1;
  1793. #else
  1794. word32 numDirs;
  1795. const char** caDirs = wolfSSL_get_system_CA_dirs(&numDirs);
  1796. if (caDirs == NULL || numDirs == 0) {
  1797. fprintf(stderr, "wolfSSL_get_system_CA_dirs failed.\n");
  1798. ret = -1;
  1799. }
  1800. else {
  1801. ReadDirCtx dirCtx;
  1802. word32 i;
  1803. for (i = 0; i < numDirs; ++i) {
  1804. if (wc_ReadDirFirst(&dirCtx, caDirs[i], NULL) == 0) {
  1805. /* Directory isn't empty. */
  1806. dirValid = 1;
  1807. wc_ReadDirClose(&dirCtx);
  1808. break;
  1809. }
  1810. }
  1811. }
  1812. #endif
  1813. }
  1814. /*
  1815. * If the directory isn't empty, we should be able to load CA
  1816. * certs from it. On Windows/Mac, we assume the CA cert stores are
  1817. * usable.
  1818. */
  1819. if (ret == 0 && dirValid && wolfSSL_CTX_load_system_CA_certs(ctx) !=
  1820. WOLFSSL_SUCCESS) {
  1821. fprintf(stderr, "wolfSSL_CTX_load_system_CA_certs failed.\n");
  1822. ret = -1;
  1823. }
  1824. #ifdef OPENSSL_EXTRA
  1825. if (ret == 0 &&
  1826. wolfSSL_CTX_set_default_verify_paths(ctx) != WOLFSSL_SUCCESS) {
  1827. fprintf(stderr, "wolfSSL_CTX_set_default_verify_paths failed.\n");
  1828. ret = -1;
  1829. }
  1830. #endif /* OPENSSL_EXTRA */
  1831. wolfSSL_CTX_free(ctx);
  1832. res = TEST_RES_CHECK(ret == 0);
  1833. #endif /* WOLFSSL_SYS_CA_CERTS && !NO_WOLFSSL_CLIENT */
  1834. return res;
  1835. }
  1836. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1837. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz,
  1838. int file_type)
  1839. {
  1840. int ret;
  1841. WOLFSSL_CERT_MANAGER* cm;
  1842. cm = wolfSSL_CertManagerNew();
  1843. if (cm == NULL) {
  1844. fprintf(stderr, "test_cm_load_ca failed\n");
  1845. return -1;
  1846. }
  1847. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  1848. wolfSSL_CertManagerFree(cm);
  1849. return ret;
  1850. }
  1851. static int test_cm_load_ca_file(const char* ca_cert_file)
  1852. {
  1853. int ret = 0;
  1854. byte* cert_buf = NULL;
  1855. size_t cert_sz = 0;
  1856. #if defined(WOLFSSL_PEM_TO_DER)
  1857. DerBuffer* pDer = NULL;
  1858. #endif
  1859. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1860. if (ret == 0) {
  1861. /* normal test */
  1862. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  1863. if (ret == WOLFSSL_SUCCESS) {
  1864. /* test including null terminator in length */
  1865. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1866. if (tmp == NULL) {
  1867. ret = MEMORY_E;
  1868. }
  1869. else {
  1870. cert_buf = tmp;
  1871. cert_buf[cert_sz] = '\0';
  1872. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  1873. WOLFSSL_FILETYPE_PEM);
  1874. }
  1875. }
  1876. #if defined(WOLFSSL_PEM_TO_DER)
  1877. if (ret == WOLFSSL_SUCCESS) {
  1878. /* test loading DER */
  1879. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1880. if (ret == 0 && pDer != NULL) {
  1881. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  1882. WOLFSSL_FILETYPE_ASN1);
  1883. wc_FreeDer(&pDer);
  1884. }
  1885. }
  1886. #endif
  1887. }
  1888. free(cert_buf);
  1889. return ret;
  1890. }
  1891. static int test_cm_load_ca_buffer_ex(const byte* cert_buf, size_t cert_sz,
  1892. int file_type, word32 flags)
  1893. {
  1894. int ret;
  1895. WOLFSSL_CERT_MANAGER* cm;
  1896. cm = wolfSSL_CertManagerNew();
  1897. if (cm == NULL) {
  1898. fprintf(stderr, "test_cm_load_ca failed\n");
  1899. return -1;
  1900. }
  1901. ret = wolfSSL_CertManagerLoadCABuffer_ex(cm, cert_buf, cert_sz, file_type,
  1902. 0, flags);
  1903. wolfSSL_CertManagerFree(cm);
  1904. return ret;
  1905. }
  1906. static int test_cm_load_ca_file_ex(const char* ca_cert_file, word32 flags)
  1907. {
  1908. int ret = 0;
  1909. byte* cert_buf = NULL;
  1910. size_t cert_sz = 0;
  1911. #if defined(WOLFSSL_PEM_TO_DER)
  1912. DerBuffer* pDer = NULL;
  1913. #endif
  1914. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1915. if (ret == 0) {
  1916. /* normal test */
  1917. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz,
  1918. WOLFSSL_FILETYPE_PEM, flags);
  1919. if (ret == WOLFSSL_SUCCESS) {
  1920. /* test including null terminator in length */
  1921. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1922. if (tmp == NULL) {
  1923. ret = MEMORY_E;
  1924. }
  1925. else {
  1926. cert_buf = tmp;
  1927. cert_buf[cert_sz] = '\0';
  1928. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz+1,
  1929. WOLFSSL_FILETYPE_PEM, flags);
  1930. }
  1931. }
  1932. #if defined(WOLFSSL_PEM_TO_DER)
  1933. if (ret == WOLFSSL_SUCCESS) {
  1934. /* test loading DER */
  1935. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1936. if (ret == 0 && pDer != NULL) {
  1937. ret = test_cm_load_ca_buffer_ex(pDer->buffer, pDer->length,
  1938. WOLFSSL_FILETYPE_ASN1, flags);
  1939. wc_FreeDer(&pDer);
  1940. }
  1941. }
  1942. #endif
  1943. }
  1944. free(cert_buf);
  1945. return ret;
  1946. }
  1947. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  1948. static int test_wolfSSL_CertManagerAPI(void)
  1949. {
  1950. EXPECT_DECLS;
  1951. #ifndef NO_CERTS
  1952. WOLFSSL_CERT_MANAGER* cm = NULL;
  1953. unsigned char c;
  1954. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  1955. wolfSSL_CertManagerFree(NULL);
  1956. ExpectIntEQ(wolfSSL_CertManager_up_ref(NULL), 0);
  1957. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(NULL), BAD_FUNC_ARG);
  1958. #ifdef WOLFSSL_TRUST_PEER_CERT
  1959. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(NULL), BAD_FUNC_ARG);
  1960. #endif
  1961. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer_ex(NULL, &c, 1,
  1962. WOLFSSL_FILETYPE_ASN1, 0, 0), WOLFSSL_FATAL_ERROR);
  1963. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  1964. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, -1,
  1965. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1966. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, -1,
  1967. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1968. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, -1,
  1969. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1970. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, 1,
  1971. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1972. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, 1,
  1973. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1974. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, 1,
  1975. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1976. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, -1,
  1977. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  1978. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, 1, -1),
  1979. WOLFSSL_BAD_FILETYPE);
  1980. #endif
  1981. #if !defined(NO_FILESYSTEM)
  1982. {
  1983. const char* ca_cert = "./certs/ca-cert.pem";
  1984. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  1985. const char* ca_cert_der = "./certs/ca-cert.der";
  1986. #endif
  1987. const char* ca_path = "./certs";
  1988. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  1989. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, NULL, -1),
  1990. BAD_FUNC_ARG);
  1991. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  1992. BAD_FUNC_ARG);
  1993. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, ca_cert,
  1994. WOLFSSL_FILETYPE_PEM), BAD_FUNC_ARG);
  1995. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert, -1),
  1996. WOLFSSL_BAD_FILETYPE);
  1997. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, "no-file",
  1998. WOLFSSL_FILETYPE_ASN1), WOLFSSL_BAD_FILE);
  1999. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert_der,
  2000. WOLFSSL_FILETYPE_PEM), ASN_NO_PEM_HEADER);
  2001. #endif
  2002. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, NULL),
  2003. WOLFSSL_FATAL_ERROR);
  2004. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, NULL),
  2005. WOLFSSL_FATAL_ERROR);
  2006. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, ca_path),
  2007. WOLFSSL_FATAL_ERROR);
  2008. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, ca_path),
  2009. WOLFSSL_FATAL_ERROR);
  2010. }
  2011. #endif
  2012. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  2013. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), 1);
  2014. #elif !defined(HAVE_CRL)
  2015. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), NOT_COMPILED_IN);
  2016. #endif
  2017. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(NULL), BAD_FUNC_ARG);
  2018. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(cm), 1);
  2019. #ifdef HAVE_CRL
  2020. /* Test APIs when CRL is disabled. */
  2021. #ifdef HAVE_CRL_IO
  2022. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  2023. #endif
  2024. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  2025. sizeof_server_cert_der_2048), 1);
  2026. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  2027. #endif
  2028. /* OCSP */
  2029. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(NULL, 0), BAD_FUNC_ARG);
  2030. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(NULL), BAD_FUNC_ARG);
  2031. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(NULL), BAD_FUNC_ARG);
  2032. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(NULL), BAD_FUNC_ARG);
  2033. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  2034. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  2035. #if !defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  2036. !defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2037. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), NOT_COMPILED_IN);
  2038. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), NOT_COMPILED_IN);
  2039. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), NOT_COMPILED_IN);
  2040. #endif
  2041. #ifdef HAVE_OCSP
  2042. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, -1), BAD_FUNC_ARG);
  2043. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, -1), BAD_FUNC_ARG);
  2044. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, -1), BAD_FUNC_ARG);
  2045. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, 1), BAD_FUNC_ARG);
  2046. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, 1), BAD_FUNC_ARG);
  2047. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, 1), BAD_FUNC_ARG);
  2048. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, -1), BAD_FUNC_ARG);
  2049. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, NULL, 0,
  2050. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2051. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, NULL, 1,
  2052. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2053. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, &c, 1,
  2054. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2055. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, NULL),
  2056. BAD_FUNC_ARG);
  2057. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, ""),
  2058. BAD_FUNC_ARG);
  2059. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, NULL), 1);
  2060. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(NULL, NULL, NULL, NULL),
  2061. BAD_FUNC_ARG);
  2062. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(cm, NULL, NULL, NULL), 1);
  2063. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(cm), 1);
  2064. /* Test APIs when OCSP is disabled. */
  2065. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, &c, 1,
  2066. NULL, NULL, NULL, NULL), 1);
  2067. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, 1), 1);
  2068. #endif
  2069. ExpectIntEQ(wolfSSL_CertManager_up_ref(cm), 1);
  2070. if (EXPECT_SUCCESS()) {
  2071. wolfSSL_CertManagerFree(cm);
  2072. }
  2073. wolfSSL_CertManagerFree(cm);
  2074. cm = NULL;
  2075. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2076. #ifdef HAVE_OCSP
  2077. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, WOLFSSL_OCSP_URL_OVERRIDE |
  2078. WOLFSSL_OCSP_CHECKALL), 1);
  2079. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2080. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2081. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2082. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2083. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), 1);
  2084. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2085. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), 1);
  2086. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), 1);
  2087. #endif
  2088. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  2089. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  2090. #endif
  2091. #ifdef WOLFSSL_TRUST_PEER_CERT
  2092. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(cm), 1);
  2093. #endif
  2094. wolfSSL_CertManagerFree(cm);
  2095. #endif
  2096. return EXPECT_RESULT();
  2097. }
  2098. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  2099. {
  2100. EXPECT_DECLS;
  2101. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2102. const char* ca_cert = "./certs/ca-cert.pem";
  2103. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2104. int ret;
  2105. ExpectIntLE(ret = test_cm_load_ca_file(ca_cert), 1);
  2106. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2107. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2108. #elif defined(NO_RSA)
  2109. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2110. #else
  2111. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2112. #endif
  2113. ExpectIntLE(ret = test_cm_load_ca_file(ca_expired_cert), 1);
  2114. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2115. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2116. #elif defined(NO_RSA)
  2117. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2118. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  2119. !defined(NO_ASN_TIME)
  2120. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  2121. #else
  2122. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2123. #endif
  2124. #endif
  2125. return EXPECT_RESULT();
  2126. }
  2127. static int test_wolfSSL_CertManagerLoadCABuffer_ex(void)
  2128. {
  2129. EXPECT_DECLS;
  2130. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2131. const char* ca_cert = "./certs/ca-cert.pem";
  2132. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2133. int ret;
  2134. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_cert, WOLFSSL_LOAD_FLAG_NONE),
  2135. 1);
  2136. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2137. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2138. #elif defined(NO_RSA)
  2139. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2140. #else
  2141. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2142. #endif
  2143. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_expired_cert,
  2144. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), 1);
  2145. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2146. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2147. #elif defined(NO_RSA)
  2148. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2149. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  2150. !defined(NO_ASN_TIME) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  2151. defined(OPENSSL_COMPATIBLE_DEFAULTS)
  2152. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  2153. #else
  2154. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2155. #endif
  2156. #endif
  2157. return EXPECT_RESULT();
  2158. }
  2159. static int test_wolfSSL_CertManagerGetCerts(void)
  2160. {
  2161. EXPECT_DECLS;
  2162. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  2163. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  2164. defined(WOLFSSL_SIGNER_DER_CERT)
  2165. WOLFSSL_CERT_MANAGER* cm = NULL;
  2166. WOLFSSL_STACK* sk = NULL;
  2167. X509* x509 = NULL;
  2168. X509* cert1 = NULL;
  2169. FILE* file1 = NULL;
  2170. #ifdef DEBUG_WOLFSSL_VERBOSE
  2171. WOLFSSL_BIO* bio = NULL;
  2172. #endif
  2173. int i = 0;
  2174. int ret = 0;
  2175. const byte* der = NULL;
  2176. int derSz = 0;
  2177. ExpectNotNull(file1 = fopen("./certs/ca-cert.pem", "rb"));
  2178. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  2179. if (file1 != NULL) {
  2180. fclose(file1);
  2181. }
  2182. ExpectNull(sk = wolfSSL_CertManagerGetCerts(NULL));
  2183. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2184. ExpectNull(sk = wolfSSL_CertManagerGetCerts(cm));
  2185. ExpectNotNull(der = wolfSSL_X509_get_der(cert1, &derSz));
  2186. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  2187. /* Check that ASN_SELF_SIGNED_E is returned for a self-signed cert for QT
  2188. * and full OpenSSL compatibility */
  2189. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2190. WOLFSSL_FILETYPE_ASN1), ASN_SELF_SIGNED_E);
  2191. #else
  2192. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2193. WOLFSSL_FILETYPE_ASN1), ASN_NO_SIGNER_E);
  2194. #endif
  2195. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  2196. "./certs/ca-cert.pem", NULL));
  2197. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  2198. for (i = 0; EXPECT_SUCCESS() && i < sk_X509_num(sk); i++) {
  2199. ExpectNotNull(x509 = sk_X509_value(sk, i));
  2200. ExpectIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  2201. #ifdef DEBUG_WOLFSSL_VERBOSE
  2202. bio = BIO_new(wolfSSL_BIO_s_file());
  2203. if (bio != NULL) {
  2204. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  2205. X509_print(bio, x509);
  2206. BIO_free(bio);
  2207. }
  2208. #endif /* DEBUG_WOLFSSL_VERBOSE */
  2209. }
  2210. wolfSSL_X509_free(cert1);
  2211. sk_X509_pop_free(sk, NULL);
  2212. wolfSSL_CertManagerFree(cm);
  2213. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  2214. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  2215. defined(WOLFSSL_SIGNER_DER_CERT) */
  2216. return EXPECT_RESULT();
  2217. }
  2218. static int test_wolfSSL_CertManagerSetVerify(void)
  2219. {
  2220. EXPECT_DECLS;
  2221. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2222. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2223. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  2224. WOLFSSL_CERT_MANAGER* cm = NULL;
  2225. int tmp = myVerifyAction;
  2226. const char* ca_cert = "./certs/ca-cert.pem";
  2227. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  2228. wolfSSL_CertManagerSetVerify(NULL, NULL);
  2229. wolfSSL_CertManagerSetVerify(NULL, myVerify);
  2230. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2231. wolfSSL_CertManagerSetVerify(cm, myVerify);
  2232. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2233. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL), -1);
  2234. #else
  2235. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL),
  2236. WOLFSSL_SUCCESS);
  2237. #endif
  2238. /* Use the test CB that always accepts certs */
  2239. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  2240. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, expiredCert,
  2241. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  2242. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  2243. {
  2244. const char* verifyCert = "./certs/server-cert.der";
  2245. /* Use the test CB that always fails certs */
  2246. myVerifyAction = VERIFY_FORCE_FAIL;
  2247. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, verifyCert,
  2248. WOLFSSL_FILETYPE_ASN1), VERIFY_CERT_ERROR);
  2249. }
  2250. #endif
  2251. wolfSSL_CertManagerFree(cm);
  2252. myVerifyAction = tmp;
  2253. #endif
  2254. return EXPECT_RESULT();
  2255. }
  2256. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  2257. defined(DEBUG_UNIT_TEST_CERTS)
  2258. /* Used when debugging name constraint tests. Not static to allow use in
  2259. * multiple locations with complex define guards. */
  2260. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  2261. {
  2262. BIO* out = BIO_new_file(fileName, "wb");
  2263. if (out != NULL) {
  2264. PEM_write_bio_X509(out, x509);
  2265. BIO_free(out);
  2266. }
  2267. }
  2268. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
  2269. {
  2270. BIO* out = BIO_new_file(fileName, "wb");
  2271. if (out != NULL) {
  2272. BIO_write(out, der, derSz);
  2273. BIO_free(out);
  2274. }
  2275. }
  2276. #else
  2277. #define DEBUG_WRITE_CERT_X509(x509, fileName) WC_DO_NOTHING
  2278. #define DEBUG_WRITE_DER(der, derSz, fileName) WC_DO_NOTHING
  2279. #endif
  2280. static int test_wolfSSL_CertManagerNameConstraint(void)
  2281. {
  2282. EXPECT_DECLS;
  2283. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2284. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2285. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2286. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2287. !defined(NO_SHA256)
  2288. WOLFSSL_CERT_MANAGER* cm = NULL;
  2289. WOLFSSL_EVP_PKEY *priv = NULL;
  2290. WOLFSSL_X509_NAME* name = NULL;
  2291. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  2292. const char* server_cert = "./certs/test/server-goodcn.pem";
  2293. int i = 0;
  2294. static const byte extNameConsOid[] = {85, 29, 30};
  2295. RsaKey key;
  2296. WC_RNG rng;
  2297. byte *der = NULL;
  2298. int derSz = 0;
  2299. word32 idx = 0;
  2300. byte *pt;
  2301. WOLFSSL_X509 *x509 = NULL;
  2302. WOLFSSL_X509 *ca = NULL;
  2303. wc_InitRng(&rng);
  2304. /* load in CA private key for signing */
  2305. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, testDevId), 0);
  2306. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  2307. sizeof_server_key_der_2048), 0);
  2308. /* get ca certificate then alter it */
  2309. ExpectNotNull(der =
  2310. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  2311. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  2312. WOLFSSL_FILETYPE_ASN1));
  2313. ExpectNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  2314. if (EXPECT_SUCCESS() && (der != NULL)) {
  2315. XMEMCPY(der, pt, derSz);
  2316. /* find the name constraint extension and alter it */
  2317. pt = der;
  2318. for (i = 0; i < derSz - 3; i++) {
  2319. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  2320. pt += 3;
  2321. break;
  2322. }
  2323. pt++;
  2324. }
  2325. ExpectIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  2326. /* go to the length value and set it to 0 */
  2327. while (i < derSz && *pt != 0x81) {
  2328. pt++;
  2329. i++;
  2330. }
  2331. ExpectIntNE(i, derSz); /* did not place to alter */
  2332. pt++;
  2333. *pt = 0x00;
  2334. }
  2335. /* resign the altered certificate */
  2336. ExpectIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  2337. FOURK_BUF, &key, NULL, &rng)), 0);
  2338. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2339. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2340. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  2341. wolfSSL_CertManagerFree(cm);
  2342. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2343. wolfSSL_X509_free(x509);
  2344. wc_FreeRsaKey(&key);
  2345. wc_FreeRng(&rng);
  2346. /* add email alt name to satisfy constraint */
  2347. pt = (byte*)server_key_der_2048;
  2348. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2349. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2350. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2351. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2352. WOLFSSL_FILETYPE_ASN1));
  2353. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2354. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2355. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2356. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2357. /* Good cert test with proper alt email name */
  2358. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2359. WOLFSSL_FILETYPE_PEM));
  2360. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2361. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2362. name = NULL;
  2363. ExpectNotNull(name = X509_NAME_new());
  2364. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2365. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2366. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2367. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2368. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2369. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2370. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2371. X509_NAME_free(name);
  2372. name = NULL;
  2373. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2374. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2375. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2376. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2377. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2378. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2379. wolfSSL_X509_free(x509);
  2380. x509 = NULL;
  2381. /* Cert with bad alt name list */
  2382. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2383. WOLFSSL_FILETYPE_PEM));
  2384. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2385. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2386. name = NULL;
  2387. ExpectNotNull(name = X509_NAME_new());
  2388. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2389. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2390. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2391. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2392. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2393. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2394. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2395. X509_NAME_free(name);
  2396. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2397. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2398. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2399. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2400. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2401. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2402. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2403. wolfSSL_CertManagerFree(cm);
  2404. wolfSSL_X509_free(x509);
  2405. wolfSSL_X509_free(ca);
  2406. wolfSSL_EVP_PKEY_free(priv);
  2407. #endif
  2408. return EXPECT_RESULT();
  2409. }
  2410. static int test_wolfSSL_CertManagerNameConstraint2(void)
  2411. {
  2412. EXPECT_DECLS;
  2413. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2414. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2415. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2416. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  2417. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  2418. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  2419. const char* server_cert = "./certs/server-cert.pem";
  2420. WOLFSSL_CERT_MANAGER* cm = NULL;
  2421. WOLFSSL_X509 *x509 = NULL;
  2422. WOLFSSL_X509 *ca = NULL;
  2423. const unsigned char *der = NULL;
  2424. const unsigned char *pt;
  2425. WOLFSSL_EVP_PKEY *priv = NULL;
  2426. WOLFSSL_X509_NAME* name = NULL;
  2427. int derSz = 0;
  2428. /* C=US*/
  2429. char altName[] = {
  2430. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2431. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  2432. };
  2433. /* C=ID */
  2434. char altNameFail[] = {
  2435. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2436. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  2437. };
  2438. /* C=US ST=California*/
  2439. char altNameExc[] = {
  2440. 0x30, 0x22,
  2441. 0x31, 0x0B,
  2442. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  2443. 0x31, 0x13,
  2444. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  2445. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  2446. };
  2447. /* load in CA private key for signing */
  2448. pt = ca_key_der_2048;
  2449. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  2450. sizeof_ca_key_der_2048));
  2451. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2452. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2453. WOLFSSL_FILETYPE_ASN1));
  2454. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2455. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2456. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2457. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2458. WOLFSSL_FILETYPE_PEM));
  2459. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2460. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2461. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2462. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2463. #else
  2464. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2465. #endif
  2466. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2467. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2468. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2469. /* add in matching DIR alt name and resign */
  2470. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2471. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2472. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2473. #else
  2474. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2475. #endif
  2476. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2477. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2478. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2479. wolfSSL_X509_free(x509);
  2480. x509 = NULL;
  2481. /* check verify fail */
  2482. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2483. WOLFSSL_FILETYPE_PEM));
  2484. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2485. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2486. /* add in miss matching DIR alt name and resign */
  2487. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2488. ASN_DIR_TYPE);
  2489. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2490. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2491. #else
  2492. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2493. #endif
  2494. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2495. #ifndef WOLFSSL_NO_ASN_STRICT
  2496. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2497. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2498. #else
  2499. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2500. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2501. #endif
  2502. /* check that it still fails if one bad altname and one good altname is in
  2503. * the certificate */
  2504. wolfSSL_X509_free(x509);
  2505. x509 = NULL;
  2506. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2507. WOLFSSL_FILETYPE_PEM));
  2508. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2509. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2510. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2511. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2512. ASN_DIR_TYPE);
  2513. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2514. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2515. #else
  2516. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2517. #endif
  2518. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2519. #ifndef WOLFSSL_NO_ASN_STRICT
  2520. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2521. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2522. #else
  2523. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2524. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2525. #endif
  2526. /* check it fails with switching position of bad altname */
  2527. wolfSSL_X509_free(x509);
  2528. x509 = NULL;
  2529. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2530. WOLFSSL_FILETYPE_PEM));
  2531. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2532. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2533. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2534. ASN_DIR_TYPE);
  2535. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2536. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2537. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2538. #else
  2539. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2540. #endif
  2541. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2542. #ifndef WOLFSSL_NO_ASN_STRICT
  2543. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2544. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2545. #else
  2546. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2547. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2548. #endif
  2549. wolfSSL_CertManagerFree(cm);
  2550. wolfSSL_X509_free(x509);
  2551. x509 = NULL;
  2552. wolfSSL_X509_free(ca);
  2553. ca = NULL;
  2554. /* now test with excluded name constraint */
  2555. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2556. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  2557. WOLFSSL_FILETYPE_ASN1));
  2558. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2559. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2560. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2561. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2562. WOLFSSL_FILETYPE_PEM));
  2563. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  2564. ASN_DIR_TYPE);
  2565. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2566. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  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. wolfSSL_CertManagerFree(cm);
  2581. wolfSSL_X509_free(x509);
  2582. wolfSSL_X509_free(ca);
  2583. wolfSSL_EVP_PKEY_free(priv);
  2584. #endif
  2585. return EXPECT_RESULT();
  2586. }
  2587. static int test_wolfSSL_CertManagerNameConstraint3(void)
  2588. {
  2589. EXPECT_DECLS;
  2590. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2591. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2592. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2593. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2594. !defined(NO_SHA256)
  2595. WOLFSSL_CERT_MANAGER* cm = NULL;
  2596. WOLFSSL_EVP_PKEY *priv = NULL;
  2597. WOLFSSL_X509_NAME* name = NULL;
  2598. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  2599. const char* server_cert = "./certs/test/server-goodcn.pem";
  2600. byte *der = NULL;
  2601. int derSz = 0;
  2602. byte *pt;
  2603. WOLFSSL_X509 *x509 = NULL;
  2604. WOLFSSL_X509 *ca = NULL;
  2605. pt = (byte*)server_key_der_2048;
  2606. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2607. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2608. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2609. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2610. WOLFSSL_FILETYPE_ASN1));
  2611. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2612. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2613. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2614. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2615. /* check satisfying .wolfssl.com constraint passes */
  2616. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2617. WOLFSSL_FILETYPE_PEM));
  2618. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2619. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2620. name = NULL;
  2621. ExpectNotNull(name = X509_NAME_new());
  2622. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2623. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2624. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2625. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2626. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2627. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2628. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2629. X509_NAME_free(name);
  2630. name = NULL;
  2631. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2632. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2633. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2634. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2635. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2636. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2637. wolfSSL_X509_free(x509);
  2638. x509 = NULL;
  2639. /* check satisfying .random.com constraint passes */
  2640. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2641. WOLFSSL_FILETYPE_PEM));
  2642. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2643. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2644. name = NULL;
  2645. ExpectNotNull(name = X509_NAME_new());
  2646. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2647. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2648. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2649. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2650. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2651. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  2652. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2653. X509_NAME_free(name);
  2654. name = NULL;
  2655. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  2656. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2657. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2658. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2659. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2660. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2661. wolfSSL_X509_free(x509);
  2662. x509 = NULL;
  2663. /* check fail case when neither constraint is matched */
  2664. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2665. WOLFSSL_FILETYPE_PEM));
  2666. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2667. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2668. name = NULL;
  2669. ExpectNotNull(name = X509_NAME_new());
  2670. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2671. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2672. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2673. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2674. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2675. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  2676. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2677. X509_NAME_free(name);
  2678. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2679. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2680. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2681. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2682. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2683. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2684. wolfSSL_CertManagerFree(cm);
  2685. wolfSSL_X509_free(x509);
  2686. wolfSSL_X509_free(ca);
  2687. wolfSSL_EVP_PKEY_free(priv);
  2688. #endif
  2689. return EXPECT_RESULT();
  2690. }
  2691. static int test_wolfSSL_CertManagerNameConstraint4(void)
  2692. {
  2693. EXPECT_DECLS;
  2694. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2695. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2696. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2697. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2698. !defined(NO_SHA256)
  2699. WOLFSSL_CERT_MANAGER* cm = NULL;
  2700. WOLFSSL_EVP_PKEY *priv = NULL;
  2701. WOLFSSL_X509_NAME* name = NULL;
  2702. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  2703. const char* server_cert = "./certs/test/server-goodcn.pem";
  2704. byte *der = NULL;
  2705. int derSz;
  2706. byte *pt;
  2707. WOLFSSL_X509 *x509 = NULL;
  2708. WOLFSSL_X509 *ca = NULL;
  2709. pt = (byte*)server_key_der_2048;
  2710. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2711. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2712. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2713. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2714. WOLFSSL_FILETYPE_ASN1));
  2715. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2716. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2717. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2718. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2719. /* check satisfying wolfssl.com constraint passes */
  2720. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2721. WOLFSSL_FILETYPE_PEM));
  2722. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2723. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2724. name = NULL;
  2725. ExpectNotNull(name = X509_NAME_new());
  2726. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2727. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2728. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2729. (byte*)"wolfssl.com", 11, -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, "www.wolfssl.com", ASN_DNS_TYPE);
  2734. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2735. DEBUG_WRITE_CERT_X509(x509, "good-1st-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 satisfying example.com constraint passes */
  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*)"example.com", 11, -1, 0), SSL_SUCCESS);
  2752. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2753. X509_NAME_free(name);
  2754. name = NULL;
  2755. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  2756. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2757. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2758. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2759. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2760. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2761. wolfSSL_X509_free(x509);
  2762. x509 = NULL;
  2763. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  2764. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2765. WOLFSSL_FILETYPE_PEM));
  2766. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2767. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2768. name = NULL;
  2769. ExpectNotNull(name = X509_NAME_new());
  2770. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2771. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2772. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2773. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2774. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2775. X509_NAME_free(name);
  2776. name = NULL;
  2777. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2778. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2779. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  2780. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2781. DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
  2782. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2783. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2784. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2785. wolfSSL_X509_free(x509);
  2786. x509 = NULL;
  2787. /* check fail when one DNS in the list is bad */
  2788. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2789. WOLFSSL_FILETYPE_PEM));
  2790. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2791. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2792. name = NULL;
  2793. ExpectNotNull(name = X509_NAME_new());
  2794. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2795. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2796. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2797. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2798. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2799. X509_NAME_free(name);
  2800. name = NULL;
  2801. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2802. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  2803. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2804. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2805. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  2806. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2807. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2808. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2809. wolfSSL_X509_free(x509);
  2810. x509 = NULL;
  2811. /* check fail case when neither constraint is matched */
  2812. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2813. WOLFSSL_FILETYPE_PEM));
  2814. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2815. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2816. name = NULL;
  2817. ExpectNotNull(name = X509_NAME_new());
  2818. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2819. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2820. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2821. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  2822. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2823. X509_NAME_free(name);
  2824. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  2825. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2826. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2827. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2828. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2829. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2830. wolfSSL_CertManagerFree(cm);
  2831. wolfSSL_X509_free(x509);
  2832. wolfSSL_X509_free(ca);
  2833. wolfSSL_EVP_PKEY_free(priv);
  2834. #endif
  2835. return EXPECT_RESULT();
  2836. }
  2837. static int test_wolfSSL_CertManagerNameConstraint5(void)
  2838. {
  2839. EXPECT_DECLS;
  2840. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2841. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2842. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2843. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2844. !defined(NO_SHA256)
  2845. WOLFSSL_CERT_MANAGER* cm = NULL;
  2846. WOLFSSL_EVP_PKEY *priv = NULL;
  2847. WOLFSSL_X509_NAME* name = NULL;
  2848. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  2849. const char* server_cert = "./certs/test/server-goodcn.pem";
  2850. byte *der = NULL;
  2851. int derSz;
  2852. byte *pt;
  2853. WOLFSSL_X509 *x509 = NULL;
  2854. WOLFSSL_X509 *ca = NULL;
  2855. pt = (byte*)server_key_der_2048;
  2856. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2857. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2858. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2859. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2860. WOLFSSL_FILETYPE_ASN1));
  2861. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2862. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2863. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2864. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2865. /* check satisfying wolfssl.com constraint passes */
  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*)"example", 7, -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, "good.example", ASN_DNS_TYPE);
  2880. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  2881. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2882. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2883. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2884. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2885. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2886. wolfSSL_X509_free(x509);
  2887. x509 = NULL;
  2888. /* fail with DNS check because of common name */
  2889. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2890. WOLFSSL_FILETYPE_PEM));
  2891. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2892. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2893. name = NULL;
  2894. ExpectNotNull(name = X509_NAME_new());
  2895. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2896. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2897. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2898. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2899. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2900. X509_NAME_free(name);
  2901. name = NULL;
  2902. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2903. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  2904. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2905. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  2906. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2907. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2908. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2909. wolfSSL_X509_free(x509);
  2910. x509 = NULL;
  2911. /* fail on permitted DNS name constraint */
  2912. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2913. WOLFSSL_FILETYPE_PEM));
  2914. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2915. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2916. name = NULL;
  2917. ExpectNotNull(name = X509_NAME_new());
  2918. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2919. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2920. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2921. X509_NAME_free(name);
  2922. name = NULL;
  2923. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  2924. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  2925. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2926. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2927. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  2928. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2929. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2930. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2931. wolfSSL_X509_free(x509);
  2932. x509 = NULL;
  2933. /* fail on permitted email name constraint */
  2934. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2935. WOLFSSL_FILETYPE_PEM));
  2936. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2937. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2938. name = NULL;
  2939. ExpectNotNull(name = X509_NAME_new());
  2940. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2941. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2942. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2943. X509_NAME_free(name);
  2944. name = NULL;
  2945. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2946. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  2947. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  2948. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2949. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  2950. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2951. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2952. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2953. wolfSSL_X509_free(x509);
  2954. x509 = NULL;
  2955. /* success with empty email name */
  2956. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2957. WOLFSSL_FILETYPE_PEM));
  2958. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2959. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2960. name = NULL;
  2961. ExpectNotNull(name = X509_NAME_new());
  2962. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2963. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2964. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2965. X509_NAME_free(name);
  2966. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2967. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2968. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  2969. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2970. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2971. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2972. wolfSSL_X509_free(x509);
  2973. wolfSSL_CertManagerFree(cm);
  2974. wolfSSL_X509_free(ca);
  2975. wolfSSL_EVP_PKEY_free(priv);
  2976. #endif
  2977. return EXPECT_RESULT();
  2978. }
  2979. static int test_wolfSSL_CertManagerCRL(void)
  2980. {
  2981. EXPECT_DECLS;
  2982. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  2983. !defined(NO_RSA)
  2984. const char* ca_cert = "./certs/ca-cert.pem";
  2985. const char* crl1 = "./certs/crl/crl.pem";
  2986. const char* crl2 = "./certs/crl/crl2.pem";
  2987. #ifdef WC_RSA_PSS
  2988. const char* crl_rsapss = "./certs/crl/crl_rsapss.pem";
  2989. const char* ca_rsapss = "./certs/rsapss/ca-rsapss.pem";
  2990. #endif
  2991. const unsigned char crl_buff[] = {
  2992. 0x30, 0x82, 0x02, 0x04, 0x30, 0x81, 0xed, 0x02,
  2993. 0x01, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  2994. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  2995. 0x00, 0x30, 0x81, 0x94, 0x31, 0x0b, 0x30, 0x09,
  2996. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  2997. 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  2998. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74,
  2999. 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06,
  3000. 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  3001. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x11, 0x30,
  3002. 0x0f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x08,
  3003. 0x53, 0x61, 0x77, 0x74, 0x6f, 0x6f, 0x74, 0x68,
  3004. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04,
  3005. 0x0b, 0x0c, 0x0a, 0x43, 0x6f, 0x6e, 0x73, 0x75,
  3006. 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  3007. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  3008. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66,
  3009. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x31,
  3010. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  3011. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10,
  3012. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  3013. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  3014. 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, 0x36,
  3015. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x17,
  3016. 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, 0x32,
  3017. 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, 0x14,
  3018. 0x30, 0x12, 0x02, 0x01, 0x02, 0x17, 0x0d, 0x32,
  3019. 0x32, 0x31, 0x32, 0x31, 0x36, 0x32, 0x31, 0x31,
  3020. 0x37, 0x35, 0x30, 0x5a, 0xa0, 0x0e, 0x30, 0x0c,
  3021. 0x30, 0x0a, 0x06, 0x03, 0x55, 0x1d, 0x14, 0x04,
  3022. 0x03, 0x02, 0x01, 0x02, 0x30, 0x0d, 0x06, 0x09,
  3023. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  3024. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00,
  3025. 0x39, 0x44, 0xff, 0x39, 0xf4, 0x04, 0x45, 0x79,
  3026. 0x7e, 0x73, 0xe2, 0x42, 0x48, 0xdb, 0x85, 0x66,
  3027. 0xfd, 0x99, 0x76, 0x94, 0x7c, 0xb5, 0x79, 0x5d,
  3028. 0x15, 0x71, 0x36, 0xa9, 0x87, 0xf0, 0x73, 0x05,
  3029. 0x50, 0x08, 0x6b, 0x1c, 0x6e, 0xde, 0x96, 0x45,
  3030. 0x31, 0xc3, 0xc0, 0xba, 0xba, 0xf5, 0x08, 0x1d,
  3031. 0x05, 0x4a, 0x52, 0x39, 0xe9, 0x03, 0xef, 0x59,
  3032. 0xc8, 0x1d, 0x4a, 0xf2, 0x86, 0x05, 0x99, 0x7b,
  3033. 0x4b, 0x74, 0xf6, 0xd3, 0x75, 0x8d, 0xb2, 0x57,
  3034. 0xba, 0xac, 0xa7, 0x11, 0x14, 0xd6, 0x6c, 0x71,
  3035. 0xc4, 0x4c, 0x1c, 0x68, 0xbc, 0x49, 0x78, 0xf0,
  3036. 0xc9, 0x52, 0x8a, 0xe7, 0x8b, 0x54, 0xe6, 0x20,
  3037. 0x58, 0x20, 0x60, 0x66, 0xf5, 0x14, 0xd8, 0xcb,
  3038. 0xff, 0xe0, 0xa0, 0x45, 0xbc, 0xb4, 0x81, 0xad,
  3039. 0x1d, 0xbc, 0xcf, 0xf8, 0x8e, 0xa8, 0x87, 0x24,
  3040. 0x55, 0x99, 0xd9, 0xce, 0x47, 0xf7, 0x5b, 0x4a,
  3041. 0x33, 0x6d, 0xdb, 0xbf, 0x93, 0x64, 0x1a, 0xa6,
  3042. 0x46, 0x5f, 0x27, 0xdc, 0xd8, 0xd4, 0xf9, 0xc2,
  3043. 0x42, 0x2a, 0x7e, 0xb2, 0x7c, 0xdd, 0x98, 0x77,
  3044. 0xf5, 0x88, 0x7d, 0x15, 0x25, 0x08, 0xbc, 0xe0,
  3045. 0xd0, 0x8d, 0xf4, 0xc3, 0xc3, 0x04, 0x41, 0xa4,
  3046. 0xd1, 0xb1, 0x39, 0x4a, 0x6b, 0x2c, 0xb5, 0x2e,
  3047. 0x9a, 0x65, 0x43, 0x0d, 0x0e, 0x73, 0xf4, 0x06,
  3048. 0xe1, 0xb3, 0x49, 0x34, 0x94, 0xb0, 0xb7, 0xff,
  3049. 0xc0, 0x27, 0xc1, 0xb5, 0xea, 0x06, 0xf7, 0x71,
  3050. 0x71, 0x97, 0xbb, 0xbc, 0xc7, 0x1a, 0x9f, 0xeb,
  3051. 0xf6, 0x3d, 0xa5, 0x7b, 0x55, 0xa7, 0xbf, 0xdd,
  3052. 0xd7, 0xee, 0x97, 0xb8, 0x9d, 0xdc, 0xcd, 0xe3,
  3053. 0x06, 0xdb, 0x9a, 0x2c, 0x60, 0xbf, 0x70, 0x84,
  3054. 0xfa, 0x6b, 0x8d, 0x70, 0x7d, 0xde, 0xe8, 0xb7,
  3055. 0xab, 0xb0, 0x38, 0x68, 0x6c, 0xc0, 0xb1, 0xe1,
  3056. 0xba, 0x45, 0xe0, 0xd7, 0x12, 0x3d, 0x71, 0x5b
  3057. };
  3058. WOLFSSL_CERT_MANAGER* cm = NULL;
  3059. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3060. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(NULL, 0), BAD_FUNC_ARG);
  3061. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  3062. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECK), 1);
  3063. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm,
  3064. WOLFSSL_CRL_CHECK | WOLFSSL_CRL_CHECKALL), 1);
  3065. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 16), 1);
  3066. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  3067. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, -1), BAD_FUNC_ARG);
  3068. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, -1), BAD_FUNC_ARG);
  3069. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, -1),
  3070. BAD_FUNC_ARG);
  3071. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, 1), BAD_FUNC_ARG);
  3072. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, 1),
  3073. BAD_FUNC_ARG);
  3074. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, 1), BAD_FUNC_ARG);
  3075. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048, -1),
  3076. BAD_FUNC_ARG);
  3077. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  3078. sizeof_server_cert_der_2048), ASN_NO_SIGNER_E);
  3079. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(NULL, NULL), BAD_FUNC_ARG);
  3080. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(cm, NULL), 1);
  3081. #ifdef HAVE_CRL_IO
  3082. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(NULL, NULL), BAD_FUNC_ARG);
  3083. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  3084. #endif
  3085. #ifndef NO_FILESYSTEM
  3086. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(NULL, NULL, WOLFSSL_FILETYPE_ASN1,
  3087. 0), BAD_FUNC_ARG);
  3088. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, NULL, WOLFSSL_FILETYPE_ASN1,
  3089. 0), BAD_FUNC_ARG);
  3090. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  3091. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, "./certs/crl", -1, 0), 1);
  3092. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(NULL, NULL,
  3093. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3094. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  3095. BAD_FUNC_ARG);
  3096. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  3097. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, "./certs/crl/crl.pem", -1),
  3098. ASN_PARSE_E);
  3099. #endif
  3100. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, -1,
  3101. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3102. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, -1,
  3103. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3104. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, -1,
  3105. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3106. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, 1,
  3107. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3108. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, 1,
  3109. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3110. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, 1,
  3111. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3112. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, -1,
  3113. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3114. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(NULL), BAD_FUNC_ARG);
  3115. DoExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  3116. ExpectIntEQ(WOLFSSL_SUCCESS,
  3117. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3118. ExpectIntEQ(WOLFSSL_SUCCESS,
  3119. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  3120. ExpectIntEQ(WOLFSSL_SUCCESS,
  3121. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  3122. wolfSSL_CertManagerFreeCRL(cm);
  3123. ExpectIntEQ(WOLFSSL_SUCCESS,
  3124. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  3125. ExpectIntEQ(WOLFSSL_SUCCESS,
  3126. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3127. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  3128. sizeof_server_cert_der_2048), CRL_MISSING);
  3129. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, server_cert_der_2048,
  3130. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1), CRL_MISSING);
  3131. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, sizeof(crl_buff),
  3132. WOLFSSL_FILETYPE_ASN1), 1);
  3133. #if !defined(NO_FILESYSTEM) && defined(WC_RSA_PSS)
  3134. /* loading should fail without the CA set */
  3135. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, crl_rsapss,
  3136. WOLFSSL_FILETYPE_PEM), ASN_CRL_NO_SIGNER_E);
  3137. /* now successfully load the RSA-PSS crl once loading in it's CA */
  3138. ExpectIntEQ(WOLFSSL_SUCCESS,
  3139. wolfSSL_CertManagerLoadCA(cm, ca_rsapss, NULL));
  3140. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, crl_rsapss,
  3141. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3142. #endif
  3143. wolfSSL_CertManagerFree(cm);
  3144. #endif
  3145. return EXPECT_RESULT();
  3146. }
  3147. static int test_wolfSSL_CertManagerCheckOCSPResponse(void)
  3148. {
  3149. EXPECT_DECLS;
  3150. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA)
  3151. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  3152. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  3153. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  3154. defined(HAVE_LIGHTY)
  3155. WOLFSSL_CERT_MANAGER* cm = NULL;
  3156. /* Raw OCSP response bytes captured using the following setup:
  3157. * - Run responder with
  3158. * openssl ocsp -port 9999 -ndays 9999
  3159. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  3160. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  3161. * -rkey certs/ocsp/ocsp-responder-key.pem
  3162. * -CA certs/ocsp/intermediate1-ca-cert.pem
  3163. * - Run client with
  3164. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  3165. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  3166. * -cert certs/ocsp/server1-cert.pem
  3167. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  3168. * - Select the response packet in Wireshark, and export it using
  3169. * "File->Export Packet Dissection->As "C" Arrays". Select "Selected
  3170. * packets only". After importing into the editor, remove the initial
  3171. * ~148 bytes of header, ending with the Content-Length and the \r\n\r\n.
  3172. */
  3173. static const byte response[] = {
  3174. 0x30, 0x82, 0x07, 0x40, /* ....0..@ */
  3175. 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, /* ......90 */
  3176. 0x82, 0x07, 0x35, 0x06, 0x09, 0x2b, 0x06, 0x01, /* ..5..+.. */
  3177. 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, /* ...0.... */
  3178. 0x07, 0x26, 0x30, 0x82, 0x07, 0x22, 0x30, 0x82, /* .&0.."0. */
  3179. 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, /* .@...0.. */
  3180. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3181. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  3182. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  3183. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  3184. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  3185. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  3186. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  3187. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  3188. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  3189. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  3190. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  3191. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, /* ring1.0. */
  3192. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, /* ..U....w */
  3193. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, /* olfSSL O */
  3194. 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, /* CSP Resp */
  3195. 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, /* onder1.0 */
  3196. 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, /* ...*.H.. */
  3197. 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, /* ......in */
  3198. 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, /* fo@wolfs */
  3199. 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, /* sl.com.. */
  3200. 0x32, 0x30, 0x32, 0x33, 0x31, 0x31, 0x30, 0x38, /* 20231108 */
  3201. 0x30, 0x30, 0x32, 0x36, 0x33, 0x37, 0x5a, 0x30, /* 002637Z0 */
  3202. 0x64, 0x30, 0x62, 0x30, 0x3a, 0x30, 0x09, 0x06, /* d0b0:0.. */
  3203. 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, /* .+...... */
  3204. 0x04, 0x14, 0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, /* ..qM.#@Y */
  3205. 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, /* ...7C.1. */
  3206. 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04, 0x04, 0x14, /* ..C..... */
  3207. 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, /* ..:.,... */
  3208. 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, /* ..L.*.q. */
  3209. 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, /* dD...... */
  3210. 0x00, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x33, 0x31, /* ...20231 */
  3211. 0x31, 0x30, 0x38, 0x30, 0x30, 0x32, 0x36, 0x33, /* 10800263 */
  3212. 0x37, 0x5a, 0xa0, 0x11, 0x18, 0x0f, 0x32, 0x30, /* 7Z....20 */
  3213. 0x35, 0x31, 0x30, 0x33, 0x32, 0x35, 0x30, 0x30, /* 51032500 */
  3214. 0x32, 0x36, 0x33, 0x37, 0x5a, 0xa1, 0x23, 0x30, /* 2637Z.#0 */
  3215. 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, /* !0...+.. */
  3216. 0x05, 0x05, 0x07, 0x30, 0x01, 0x02, 0x04, 0x12, /* ...0.... */
  3217. 0x04, 0x10, 0xdb, 0xbc, 0x2a, 0x76, 0xa0, 0xb4, /* ....*v.. */
  3218. 0x1e, 0x5d, 0xf6, 0x2b, 0x8e, 0x38, 0x62, 0xdb, /* .].+.8b. */
  3219. 0x90, 0xed, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, /* ..0...*. */
  3220. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, /* H....... */
  3221. 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x87, 0xde, /* ........ */
  3222. 0xfb, 0xf9, 0x3a, 0x90, 0x1f, 0x90, 0xde, 0xcf, /* ..:..... */
  3223. 0xfe, 0xad, 0x64, 0x19, 0x34, 0x17, 0xf8, 0x15, /* ..d.4... */
  3224. 0x01, 0x22, 0x5f, 0x67, 0x41, 0xa4, 0x18, 0xf7, /* ."_gA... */
  3225. 0x16, 0xb7, 0xc9, 0xf3, 0xe1, 0x9f, 0xcd, 0x40, /* .......@ */
  3226. 0x56, 0x77, 0x6e, 0x6a, 0xfb, 0x92, 0x6a, 0x6f, /* Vwnj..jo */
  3227. 0x28, 0x3e, 0x22, 0x48, 0xa1, 0xc2, 0xd8, 0x1d, /* (>"H.... */
  3228. 0xc7, 0xe6, 0x78, 0x7f, 0xb6, 0x09, 0xfe, 0x2c, /* ..x...., */
  3229. 0xb5, 0xef, 0x29, 0x7c, 0xc5, 0x51, 0x16, 0x7b, /* ..)|.Q.{ */
  3230. 0x8f, 0xfb, 0x44, 0xa8, 0xcd, 0xf5, 0x5c, 0x0f, /* ..D...\. */
  3231. 0x46, 0x0e, 0xb1, 0xa4, 0xeb, 0x5b, 0xf5, 0x86, /* F....[.. */
  3232. 0x11, 0x0f, 0xcd, 0xe2, 0xe5, 0x3c, 0x91, 0x72, /* .....<.r */
  3233. 0x0d, 0x6a, 0xcb, 0x95, 0x99, 0x39, 0x91, 0x48, /* .j...9.H */
  3234. 0x65, 0x97, 0xb9, 0x78, 0xb5, 0x88, 0x7f, 0x76, /* e..x...v */
  3235. 0xa1, 0x43, 0x2f, 0xf6, 0x1f, 0x49, 0xb7, 0x08, /* .C/..I.. */
  3236. 0x36, 0xe4, 0x2e, 0x34, 0x25, 0xda, 0x16, 0x74, /* 6..4%..t */
  3237. 0x47, 0x62, 0x56, 0xff, 0x2f, 0x02, 0x03, 0x44, /* GbV./..D */
  3238. 0x89, 0x04, 0xe7, 0xb8, 0xde, 0x0a, 0x35, 0x43, /* ......5C */
  3239. 0xae, 0xd7, 0x54, 0xbe, 0xc3, 0x7c, 0x95, 0xa5, /* ..T..|.. */
  3240. 0xc8, 0xe0, 0x2e, 0x52, 0xb6, 0xea, 0x99, 0x45, /* ...R...E */
  3241. 0xfd, 0xda, 0x4b, 0xd5, 0x79, 0x07, 0x64, 0xca, /* ..K.y.d. */
  3242. 0x64, 0xba, 0x52, 0x12, 0x62, 0x8c, 0x08, 0x9a, /* d.R.b... */
  3243. 0x32, 0xeb, 0x85, 0x65, 0x05, 0x39, 0x07, 0x5d, /* 2..e.9.] */
  3244. 0x39, 0x4a, 0xcf, 0xa5, 0x30, 0xf6, 0xd1, 0xf7, /* 9J..0... */
  3245. 0x29, 0xaa, 0x23, 0x42, 0xc6, 0x85, 0x16, 0x7f, /* ).#B.... */
  3246. 0x64, 0x16, 0xb1, 0xb0, 0x5d, 0xcd, 0x88, 0x2d, /* d...]..- */
  3247. 0x06, 0xb0, 0xa9, 0xdf, 0xa3, 0x9f, 0x25, 0x41, /* ......%A */
  3248. 0x89, 0x9a, 0x19, 0xe1, 0xaa, 0xcd, 0xdf, 0x51, /* .......Q */
  3249. 0xcb, 0xa9, 0xc3, 0x7e, 0x27, 0xbc, 0x7d, 0x9b, /* ...~'.}. */
  3250. 0x6f, 0x4d, 0x79, 0x87, 0x09, 0x3f, 0xac, 0xd2, /* oMy..?.. */
  3251. 0x4a, 0x3b, 0xbe, 0xf8, 0x7a, 0xa4, 0x93, 0x45, /* J;..z..E */
  3252. 0x11, 0x64, 0x40, 0xc5, 0x03, 0xc9, 0x24, 0x5b, /* .d@...$[ */
  3253. 0xe9, 0x6d, 0xfc, 0x94, 0x08, 0xbe, 0xa0, 0x82, /* .m...... */
  3254. 0x04, 0xc6, 0x30, 0x82, 0x04, 0xc2, 0x30, 0x82, /* ..0...0. */
  3255. 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, /* ..0..... */
  3256. 0x02, 0x01, 0x02, 0x02, 0x01, 0x04, 0x30, 0x0d, /* ......0. */
  3257. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, /* ..*.H... */
  3258. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x81, 0x97, /* .....0.. */
  3259. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3260. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  3261. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  3262. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  3263. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  3264. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  3265. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  3266. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  3267. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  3268. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  3269. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  3270. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30, 0x16, /* ring1.0. */
  3271. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, /* ..U....w */
  3272. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x72, /* olfSSL r */
  3273. 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, /* oot CA1. */
  3274. 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, /* 0...*.H. */
  3275. 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, /* .......i */
  3276. 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, /* nfo@wolf */
  3277. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, /* ssl.com0 */
  3278. 0x1e, 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, /* ...22121 */
  3279. 0x36, 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, /* 6211750Z */
  3280. 0x17, 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, /* ..250911 */
  3281. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, /* 211750Z0 */
  3282. 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, /* ..1.0... */
  3283. 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, /* U....US1 */
  3284. 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, /* .0...U.. */
  3285. 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, /* ..Washin */
  3286. 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, /* gton1.0. */
  3287. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, /* ..U....S */
  3288. 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, /* eattle1. */
  3289. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, /* 0...U... */
  3290. 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  3291. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3292. 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, /* ...Engin */
  3293. 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, /* eering1. */
  3294. 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, /* 0...U... */
  3295. 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  3296. 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, /* OCSP Re */
  3297. 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, /* sponder1 */
  3298. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, /* .0...*.H */
  3299. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, /* ........ */
  3300. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, /* info@wol */
  3301. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, /* fssl.com */
  3302. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, /* 0.."0... */
  3303. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  3304. 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, /* ........ */
  3305. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, /* 0....... */
  3306. 0x00, 0xb8, 0xba, 0x23, 0xb4, 0xf6, 0xc3, 0x7b, /* ...#...{ */
  3307. 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, /* .....a.. */
  3308. 0x1e, 0x63, 0xb9, 0x85, 0x23, 0x34, 0x50, 0x6d, /* .c..#4Pm */
  3309. 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, /* .|.....u */
  3310. 0x5c, 0x2d, 0xf7, 0x63, 0x88, 0xd1, 0x07, 0x7a, /* \-.c...z */
  3311. 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, /* ..E5+... */
  3312. 0x22, 0xb4, 0x94, 0x41, 0x38, 0xe2, 0x9d, 0x74, /* "..A8..t */
  3313. 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, /* ..0".Q.. */
  3314. 0xca, 0x3f, 0x46, 0x2b, 0xfe, 0xe5, 0x5a, 0x3f, /* .?F+..Z? */
  3315. 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, /* Atgu.... */
  3316. 0xc3, 0xee, 0x42, 0xf8, 0x8d, 0xeb, 0x92, 0x95, /* ..B..... */
  3317. 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, /* ..e.C... */
  3318. 0x16, 0x80, 0x90, 0xce, 0x24, 0x35, 0x21, 0xc4, /* ....$5!. */
  3319. 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, /* U.ZQ..-. */
  3320. 0x0a, 0x5a, 0x4f, 0x4a, 0x73, 0x31, 0x50, 0xee, /* .ZOJs1P. */
  3321. 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, /* J..9...H */
  3322. 0x87, 0xb1, 0x99, 0xe2, 0x10, 0xa7, 0x06, 0x72, /* .......r */
  3323. 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, /* g.\..... */
  3324. 0x76, 0xf8, 0xe0, 0x4a, 0xec, 0xbc, 0x93, 0xf4, /* v..J.... */
  3325. 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, /* fL(q..f. */
  3326. 0xb4, 0x90, 0x30, 0xbb, 0x17, 0xb0, 0xfe, 0x97, /* ..0..... */
  3327. 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, /* ....]... */
  3328. 0x19, 0x12, 0x3c, 0xab, 0x82, 0x71, 0x78, 0xff, /* ..<..qx. */
  3329. 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, /* .?2..q.. */
  3330. 0x8c, 0x27, 0xac, 0x11, 0xb8, 0xd8, 0x43, 0x49, /* .'....CI */
  3331. 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, /* ..p..... */
  3332. 0x24, 0x87, 0x17, 0x3b, 0xd8, 0x04, 0x65, 0x6c, /* $..;..el */
  3333. 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, /* .vP..... */
  3334. 0x73, 0x68, 0x26, 0x14, 0x87, 0x95, 0xc3, 0x5f, /* sh&...._ */
  3335. 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, /* na...... */
  3336. 0x0a, 0x8b, 0x98, 0xf3, 0xe3, 0xff, 0x4e, 0x44, /* ......ND */
  3337. 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, /* .et|qTe. */
  3338. 0x39, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x82, /* 9....... */
  3339. 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, /* ..0...0. */
  3340. 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, /* ..U....0 */
  3341. 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, /* .0...U.. */
  3342. 0x04, 0x16, 0x04, 0x14, 0x32, 0x67, 0xe1, 0xb1, /* ....2g.. */
  3343. 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, /* y....#.p */
  3344. 0x40, 0x50, 0xb5, 0x46, 0x56, 0xb8, 0x30, 0x36, /* @P.FV.06 */
  3345. 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, /* 0....U.# */
  3346. 0x04, 0x81, 0xbc, 0x30, 0x81, 0xb9, 0x80, 0x14, /* ...0.... */
  3347. 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, /* s.../... */
  3348. 0x47, 0xa5, 0x38, 0xd7, 0xb0, 0x04, 0x82, 0x3a, /* G.8....: */
  3349. 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, /* ~r.!.... */
  3350. 0x81, 0x9a, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, /* ..0..1.0 */
  3351. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, /* ...U.... */
  3352. 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, /* US1.0... */
  3353. 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, /* U....Was */
  3354. 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, /* hington1 */
  3355. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, /* .0...U.. */
  3356. 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, /* ..Seattl */
  3357. 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, /* e1.0...U */
  3358. 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  3359. 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, /* SSL1.0.. */
  3360. 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, /* .U....En */
  3361. 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, /* gineerin */
  3362. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, /* g1.0...U */
  3363. 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  3364. 0x53, 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, /* SSL root */
  3365. 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, /* CA1.0.. */
  3366. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, /* .*.H.... */
  3367. 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, /* ....info */
  3368. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, /* @wolfssl */
  3369. 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, /* .com..c0 */
  3370. 0x13, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x0c, /* ...U.%.. */
  3371. 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, /* 0...+... */
  3372. 0x05, 0x07, 0x03, 0x09, 0x30, 0x0d, 0x06, 0x09, /* ....0... */
  3373. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  3374. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, /* ........ */
  3375. 0x2f, 0xb7, 0x6b, 0xec, 0xb7, 0x12, 0x63, 0xb9, /* /.k...c. */
  3376. 0x57, 0xdc, 0x04, 0x4d, 0x9c, 0x67, 0x74, 0x98, /* W..M.gt. */
  3377. 0x06, 0x28, 0x68, 0x37, 0x34, 0xc2, 0x50, 0xe9, /* .(h74.P. */
  3378. 0x2a, 0xd4, 0x1a, 0xb2, 0x32, 0x1a, 0x9d, 0x2b, /* *...2..+ */
  3379. 0x4f, 0x23, 0x50, 0xea, 0xb4, 0x95, 0x86, 0xc3, /* O#P..... */
  3380. 0xb9, 0x5f, 0x34, 0x3e, 0x99, 0x91, 0xa7, 0x80, /* ._4>.... */
  3381. 0x5f, 0x6e, 0x1b, 0x6e, 0xdb, 0xe9, 0x02, 0x38, /* _n.n...8 */
  3382. 0x6f, 0xdf, 0xc5, 0x9b, 0x0d, 0xa3, 0x1c, 0xa9, /* o....... */
  3383. 0x15, 0x76, 0x16, 0x66, 0xa8, 0x4e, 0xfb, 0xd3, /* .v.f.N.. */
  3384. 0x43, 0x76, 0xf1, 0x72, 0xb7, 0xd1, 0xfa, 0xee, /* Cv.r.... */
  3385. 0x39, 0xa6, 0x96, 0xc1, 0xa2, 0x93, 0xa4, 0x9b, /* 9....... */
  3386. 0x1e, 0x9f, 0xba, 0x71, 0x8f, 0xba, 0xbd, 0x67, /* ...q...g */
  3387. 0x6a, 0xf2, 0x15, 0x5f, 0xf1, 0x64, 0xe7, 0xcf, /* j.._.d.. */
  3388. 0x26, 0xb8, 0x4c, 0xc0, 0xeb, 0x85, 0x04, 0x58, /* &.L....X */
  3389. 0xd9, 0x4a, 0x6b, 0xd9, 0x86, 0xf5, 0x80, 0x21, /* .Jk....! */
  3390. 0xbf, 0x91, 0xc8, 0x4b, 0x9f, 0x04, 0xed, 0x57, /* ...K...W */
  3391. 0x7a, 0xd2, 0x58, 0xac, 0x5b, 0x47, 0xaf, 0x4d, /* z.X.[G.M */
  3392. 0x7f, 0x5b, 0x1d, 0x6d, 0x68, 0x9b, 0x84, 0x98, /* .[.mh... */
  3393. 0x2a, 0x31, 0x02, 0x2c, 0xe9, 0x1b, 0xaf, 0x11, /* *1.,.... */
  3394. 0x0b, 0x78, 0x49, 0xbe, 0x68, 0x68, 0xcb, 0x9c, /* .xI.hh.. */
  3395. 0x41, 0x56, 0xe8, 0xb5, 0x59, 0xda, 0xff, 0xca, /* AV..Y... */
  3396. 0x59, 0x99, 0x17, 0x3e, 0x11, 0x0a, 0x8f, 0x49, /* Y..>...I */
  3397. 0x24, 0x0b, 0x81, 0x42, 0x63, 0xcd, 0x4f, 0xf6, /* $..Bc.O. */
  3398. 0x2b, 0x9d, 0xd1, 0x79, 0x75, 0xd7, 0x4a, 0xcc, /* +..yu.J. */
  3399. 0x4c, 0xb7, 0x2b, 0xd7, 0xe8, 0xe7, 0xd4, 0x48, /* L.+....H */
  3400. 0x3c, 0x14, 0x3b, 0x1c, 0x28, 0xe8, 0x46, 0x7a, /* <.;.(.Fz */
  3401. 0xdc, 0x11, 0x9d, 0x7f, 0x1c, 0xab, 0x10, 0x95, /* ........ */
  3402. 0x17, 0xb2, 0xc7, 0x7a, 0xbb, 0x17, 0x44, 0x59, /* ...z..DY */
  3403. 0x69, 0x8e, 0x16, 0x05, 0x94, 0x8c, 0x88, 0xd9, /* i....... */
  3404. 0xdc, 0x9a, 0xfd, 0xf2, 0x93, 0xbe, 0x68, 0xba, /* ......h. */
  3405. 0x3c, 0xd6, 0x2b, 0x61, 0x3a, 0x8b, 0xf7, 0x66, /* <.+a:..f */
  3406. 0xcb, 0x54, 0xe8, 0xe4, 0xdb, 0x9f, 0xcc, 0x9e /* .T...... */
  3407. };
  3408. OcspEntry entry[1];
  3409. CertStatus status[1];
  3410. OcspRequest* request = NULL;
  3411. #ifndef NO_FILESYSTEM
  3412. const char* ca_cert = "./certs/ca-cert.pem";
  3413. #endif
  3414. byte serial[] = {0x05};
  3415. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  3416. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  3417. XMEMSET(entry, 0, sizeof(OcspEntry));
  3418. XMEMSET(status, 0, sizeof(CertStatus));
  3419. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3420. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3421. DYNAMIC_TYPE_OCSP_REQUEST));
  3422. if ((request != NULL) && (request->serial != NULL)) {
  3423. request->serialSz = sizeof(serial);
  3424. XMEMCPY(request->serial, serial, sizeof(serial));
  3425. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3426. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  3427. }
  3428. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3429. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3430. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm,
  3431. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  3432. /* Response should be valid. */
  3433. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3434. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3435. /* Flip a byte in the request serial number, response should be invalid
  3436. * now. */
  3437. if ((request != NULL) && (request->serial != NULL))
  3438. request->serial[0] ^= request->serial[0];
  3439. ExpectIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3440. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3441. #ifndef NO_FILESYSTEM
  3442. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3443. sizeof(server_cert_der_2048)), ASN_NO_SIGNER_E);
  3444. ExpectIntEQ(WOLFSSL_SUCCESS,
  3445. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3446. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3447. sizeof(server_cert_der_2048)), 1);
  3448. #endif
  3449. wolfSSL_OCSP_REQUEST_free(request);
  3450. wolfSSL_CertManagerFree(cm);
  3451. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  3452. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  3453. #endif /* HAVE_OCSP */
  3454. return EXPECT_RESULT();
  3455. }
  3456. static int test_wolfSSL_CheckOCSPResponse(void)
  3457. {
  3458. EXPECT_DECLS;
  3459. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA) && \
  3460. defined(OPENSSL_ALL)
  3461. const char* responseFile = "./certs/ocsp/test-response.der";
  3462. const char* responseMultiFile = "./certs/ocsp/test-multi-response.der";
  3463. const char* responseNoInternFile =
  3464. "./certs/ocsp/test-response-nointern.der";
  3465. const char* caFile = "./certs/ocsp/root-ca-cert.pem";
  3466. OcspResponse* res = NULL;
  3467. byte data[4096];
  3468. const unsigned char* pt;
  3469. int dataSz = 0; /* initialize to mitigate spurious maybe-uninitialized from
  3470. * gcc sanitizer with --enable-heapmath.
  3471. */
  3472. XFILE f = XBADFILE;
  3473. WOLFSSL_OCSP_BASICRESP* bs = NULL;
  3474. WOLFSSL_X509_STORE* st = NULL;
  3475. WOLFSSL_X509* issuer = NULL;
  3476. ExpectTrue((f = XFOPEN(responseFile, "rb")) != XBADFILE);
  3477. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3478. if (f != XBADFILE) {
  3479. XFCLOSE(f);
  3480. f = XBADFILE;
  3481. }
  3482. pt = data;
  3483. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3484. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3485. SSL_FILETYPE_PEM));
  3486. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3487. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3488. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3489. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  3490. wolfSSL_OCSP_BASICRESP_free(bs);
  3491. bs = NULL;
  3492. wolfSSL_OCSP_RESPONSE_free(res);
  3493. res = NULL;
  3494. wolfSSL_X509_STORE_free(st);
  3495. st = NULL;
  3496. wolfSSL_X509_free(issuer);
  3497. issuer = NULL;
  3498. /* check loading a response with optional certs */
  3499. ExpectTrue((f = XFOPEN(responseNoInternFile, "rb")) != XBADFILE);
  3500. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3501. if (f != XBADFILE)
  3502. XFCLOSE(f);
  3503. f = XBADFILE;
  3504. pt = data;
  3505. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3506. wolfSSL_OCSP_RESPONSE_free(res);
  3507. res = NULL;
  3508. /* check loading a response with multiple certs */
  3509. {
  3510. WOLFSSL_CERT_MANAGER* cm = NULL;
  3511. OcspEntry *entry = NULL;
  3512. CertStatus* status = NULL;
  3513. OcspRequest* request = NULL;
  3514. byte serial1[] = {0x01};
  3515. byte serial[] = {0x02};
  3516. byte issuerHash[] = {
  3517. 0x44, 0xA8, 0xDB, 0xD1, 0xBC, 0x97, 0x0A, 0x83,
  3518. 0x3B, 0x5B, 0x31, 0x9A, 0x4C, 0xB8, 0xD2, 0x52,
  3519. 0x37, 0x15, 0x8A, 0x88
  3520. };
  3521. byte issuerKeyHash[] = {
  3522. 0x73, 0xB0, 0x1C, 0xA4, 0x2F, 0x82, 0xCB, 0xCF,
  3523. 0x47, 0xA5, 0x38, 0xD7, 0xB0, 0x04, 0x82, 0x3A,
  3524. 0x7E, 0x72, 0x15, 0x21
  3525. };
  3526. ExpectNotNull(entry = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  3527. DYNAMIC_TYPE_OPENSSL));
  3528. ExpectNotNull(status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  3529. DYNAMIC_TYPE_OPENSSL));
  3530. if (entry != NULL)
  3531. XMEMSET(entry, 0, sizeof(OcspEntry));
  3532. if (status != NULL)
  3533. XMEMSET(status, 0, sizeof(CertStatus));
  3534. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3535. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3536. DYNAMIC_TYPE_OCSP_REQUEST));
  3537. if (request != NULL && request->serial != NULL) {
  3538. request->serialSz = sizeof(serial);
  3539. XMEMCPY(request->serial, serial, sizeof(serial));
  3540. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3541. XMEMCPY(request->issuerKeyHash, issuerKeyHash,
  3542. sizeof(issuerKeyHash));
  3543. }
  3544. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3545. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3546. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, caFile, NULL),
  3547. WOLFSSL_SUCCESS);
  3548. ExpectTrue((f = XFOPEN(responseMultiFile, "rb")) != XBADFILE);
  3549. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3550. if (f != XBADFILE)
  3551. XFCLOSE(f);
  3552. f = XBADFILE;
  3553. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3554. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3555. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3556. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3557. ExpectNotNull(entry->status);
  3558. if (request != NULL && request->serial != NULL)
  3559. XMEMCPY(request->serial, serial1, sizeof(serial1));
  3560. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3561. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3562. /* store both status's in the entry to check that "next" is not
  3563. * overwritten */
  3564. if (EXPECT_SUCCESS() && status != NULL && entry != NULL) {
  3565. status->next = entry->status;
  3566. entry->status = status;
  3567. }
  3568. if (request != NULL && request->serial != NULL)
  3569. XMEMCPY(request->serial, serial, sizeof(serial));
  3570. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3571. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3572. ExpectNotNull(entry->status->next);
  3573. /* compare the status found */
  3574. ExpectIntEQ(status->serialSz, entry->status->serialSz);
  3575. ExpectIntEQ(XMEMCMP(status->serial, entry->status->serial,
  3576. status->serialSz), 0);
  3577. if (status != NULL && entry != NULL && entry->status != status) {
  3578. XFREE(status, NULL, DYNAMIC_TYPE_OPENSSL);
  3579. }
  3580. wolfSSL_OCSP_CERTID_free(entry);
  3581. wolfSSL_OCSP_REQUEST_free(request);
  3582. wolfSSL_CertManagerFree(cm);
  3583. }
  3584. #if defined(WC_RSA_PSS)
  3585. {
  3586. const char* responsePssFile = "./certs/ocsp/test-response-rsapss.der";
  3587. /* check loading a response with RSA-PSS signature */
  3588. ExpectTrue((f = XFOPEN(responsePssFile, "rb")) != XBADFILE);
  3589. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3590. if (f != XBADFILE)
  3591. XFCLOSE(f);
  3592. pt = data;
  3593. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3594. /* try to verify the response */
  3595. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3596. SSL_FILETYPE_PEM));
  3597. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3598. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3599. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3600. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0),
  3601. WOLFSSL_SUCCESS);
  3602. wolfSSL_OCSP_BASICRESP_free(bs);
  3603. wolfSSL_OCSP_RESPONSE_free(res);
  3604. wolfSSL_X509_STORE_free(st);
  3605. wolfSSL_X509_free(issuer);
  3606. }
  3607. #endif
  3608. #endif /* HAVE_OCSP */
  3609. return EXPECT_RESULT();
  3610. }
  3611. static int test_wolfSSL_FPKI(void)
  3612. {
  3613. EXPECT_DECLS;
  3614. #if defined(WOLFSSL_FPKI) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3615. XFILE f = XBADFILE;
  3616. const char* fpkiCert = "./certs/fpki-cert.der";
  3617. DecodedCert cert;
  3618. byte buf[4096];
  3619. byte* uuid = NULL;
  3620. byte* fascn = NULL;
  3621. word32 fascnSz;
  3622. word32 uuidSz;
  3623. int bytes = 0;
  3624. ExpectTrue((f = XFOPEN(fpkiCert, "rb")) != XBADFILE);
  3625. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3626. if (f != XBADFILE)
  3627. XFCLOSE(f);
  3628. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3629. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3630. ExpectIntEQ(wc_GetFASCNFromCert(&cert, NULL, &fascnSz), LENGTH_ONLY_E) ;
  3631. ExpectNotNull(fascn = (byte*)XMALLOC(fascnSz, NULL,
  3632. DYNAMIC_TYPE_TMP_BUFFER));
  3633. ExpectIntEQ(wc_GetFASCNFromCert(&cert, fascn, &fascnSz), 0);
  3634. XFREE(fascn, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3635. ExpectIntEQ(wc_GetUUIDFromCert(&cert, NULL, &uuidSz), LENGTH_ONLY_E);
  3636. ExpectNotNull(uuid = (byte*)XMALLOC(uuidSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  3637. ExpectIntEQ(wc_GetUUIDFromCert(&cert, uuid, &uuidSz), 0);
  3638. XFREE(uuid, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3639. wc_FreeDecodedCert(&cert);
  3640. #endif
  3641. return EXPECT_RESULT();
  3642. }
  3643. /* use RID in confuncture with other names to test parsing of unknown other
  3644. * names */
  3645. static int test_wolfSSL_OtherName(void)
  3646. {
  3647. EXPECT_DECLS;
  3648. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3649. XFILE f = XBADFILE;
  3650. const char* ridCert = "./certs/rid-cert.der";
  3651. DecodedCert cert;
  3652. byte buf[4096];
  3653. int bytes = 0;
  3654. ExpectTrue((f = XFOPEN(ridCert, "rb")) != XBADFILE);
  3655. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3656. if (f != XBADFILE)
  3657. XFCLOSE(f);
  3658. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3659. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3660. wc_FreeDecodedCert(&cert);
  3661. #endif
  3662. return EXPECT_RESULT();
  3663. }
  3664. static int test_wolfSSL_CertRsaPss(void)
  3665. {
  3666. EXPECT_DECLS;
  3667. /* FIPS v2 and below don't support long salts. */
  3668. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM) && \
  3669. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  3670. (HAVE_FIPS_VERSION > 2))) && (!defined(HAVE_SELFTEST) || \
  3671. (defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION > 2)))
  3672. XFILE f = XBADFILE;
  3673. const char* rsaPssSha256Cert = "./certs/rsapss/ca-rsapss.der";
  3674. const char* rsaPssRootSha256Cert = "./certs/rsapss/root-rsapss.pem";
  3675. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3676. RSA_MAX_SIZE >= 3072
  3677. const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
  3678. #endif
  3679. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3680. const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
  3681. #endif
  3682. DecodedCert cert;
  3683. byte buf[4096];
  3684. int bytes = 0;
  3685. WOLFSSL_CERT_MANAGER* cm = NULL;
  3686. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3687. ExpectIntEQ(WOLFSSL_SUCCESS,
  3688. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha256Cert, NULL));
  3689. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3690. ExpectIntEQ(WOLFSSL_SUCCESS,
  3691. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha384Cert, NULL));
  3692. #endif
  3693. ExpectTrue((f = XFOPEN(rsaPssSha256Cert, "rb")) != XBADFILE);
  3694. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3695. if (f != XBADFILE) {
  3696. XFCLOSE(f);
  3697. f = XBADFILE;
  3698. }
  3699. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3700. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3701. wc_FreeDecodedCert(&cert);
  3702. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3703. RSA_MAX_SIZE >= 3072
  3704. ExpectTrue((f = XFOPEN(rsaPssSha384Cert, "rb")) != XBADFILE);
  3705. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3706. if (f != XBADFILE)
  3707. XFCLOSE(f);
  3708. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3709. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3710. wc_FreeDecodedCert(&cert);
  3711. #endif
  3712. wolfSSL_CertManagerFree(cm);
  3713. #endif
  3714. return EXPECT_RESULT();
  3715. }
  3716. static int test_wolfSSL_CTX_load_verify_locations_ex(void)
  3717. {
  3718. EXPECT_DECLS;
  3719. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3720. !defined(NO_WOLFSSL_CLIENT)
  3721. WOLFSSL_CTX* ctx = NULL;
  3722. const char* ca_cert = "./certs/ca-cert.pem";
  3723. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  3724. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3725. /* test good CA */
  3726. ExpectTrue(WOLFSSL_SUCCESS ==
  3727. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  3728. WOLFSSL_LOAD_FLAG_NONE));
  3729. /* test expired CA */
  3730. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3731. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3732. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3733. #else
  3734. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3735. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3736. #endif
  3737. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3738. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3739. wolfSSL_CTX_free(ctx);
  3740. #endif
  3741. return EXPECT_RESULT();
  3742. }
  3743. static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
  3744. {
  3745. EXPECT_DECLS;
  3746. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3747. defined(USE_CERT_BUFFERS_2048)
  3748. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  3749. WOLFSSL_CTX* ctx;
  3750. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  3751. byte ca_expired_cert[TWOK_BUF];
  3752. word32 sizeof_ca_expired_cert = 0;
  3753. XFILE fp = XBADFILE;
  3754. #ifndef NO_WOLFSSL_CLIENT
  3755. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3756. #else
  3757. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3758. #endif
  3759. ExpectNotNull(ctx);
  3760. /* test good CA */
  3761. ExpectTrue(WOLFSSL_SUCCESS ==
  3762. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  3763. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  3764. WOLFSSL_LOAD_FLAG_NONE));
  3765. /* load expired CA */
  3766. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  3767. ExpectTrue((fp = XFOPEN(ca_expired_cert_file, "rb")) != XBADFILE);
  3768. ExpectIntGT(sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  3769. sizeof(ca_expired_cert), fp), 0);
  3770. if (fp != XBADFILE)
  3771. XFCLOSE(fp);
  3772. /* test expired CA failure */
  3773. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3774. ExpectIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3775. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3776. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3777. #else
  3778. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3779. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3780. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3781. #endif
  3782. /* test expired CA success */
  3783. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3784. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3785. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3786. wolfSSL_CTX_free(ctx);
  3787. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  3788. #endif
  3789. return EXPECT_RESULT();
  3790. }
  3791. static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  3792. {
  3793. EXPECT_DECLS;
  3794. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  3795. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048) && \
  3796. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3797. WOLFSSL_CTX* ctx = NULL;
  3798. #ifndef NO_WOLFSSL_CLIENT
  3799. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3800. #else
  3801. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3802. #endif
  3803. ExpectTrue(WOLFSSL_SUCCESS == wolfSSL_CTX_load_verify_chain_buffer_format(
  3804. ctx, ca_cert_chain_der, sizeof_ca_cert_chain_der,
  3805. WOLFSSL_FILETYPE_ASN1));
  3806. wolfSSL_CTX_free(ctx);
  3807. #endif
  3808. return EXPECT_RESULT();
  3809. }
  3810. static int test_wolfSSL_CTX_add1_chain_cert(void)
  3811. {
  3812. EXPECT_DECLS;
  3813. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  3814. defined(KEEP_OUR_CERT) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  3815. WOLFSSL_CTX* ctx;
  3816. WOLFSSL* ssl = NULL;
  3817. const char *certChain[] = {
  3818. "./certs/intermediate/client-int-cert.pem",
  3819. "./certs/intermediate/ca-int2-cert.pem",
  3820. "./certs/intermediate/ca-int-cert.pem",
  3821. "./certs/ca-cert.pem",
  3822. NULL
  3823. };
  3824. const char** cert;
  3825. WOLFSSL_X509* x509 = NULL;
  3826. WOLF_STACK_OF(X509)* chain = NULL;
  3827. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3828. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3829. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3830. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3831. WOLFSSL_FILETYPE_PEM));
  3832. ExpectIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  3833. X509_free(x509);
  3834. x509 = NULL;
  3835. }
  3836. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3837. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3838. WOLFSSL_FILETYPE_PEM));
  3839. ExpectIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  3840. X509_free(x509);
  3841. x509 = NULL;
  3842. }
  3843. ExpectIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  3844. ExpectIntEQ(sk_X509_num(chain), 3);
  3845. ExpectIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  3846. ExpectIntEQ(sk_X509_num(chain), 3);
  3847. SSL_free(ssl);
  3848. SSL_CTX_free(ctx);
  3849. #endif
  3850. return EXPECT_RESULT();
  3851. }
  3852. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  3853. {
  3854. EXPECT_DECLS;
  3855. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3856. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3857. const char* server_chain_der = "./certs/server-cert-chain.der";
  3858. const char* client_single_pem = "./certs/client-cert.pem";
  3859. WOLFSSL_CTX* ctx;
  3860. (void)server_chain_der;
  3861. (void)client_single_pem;
  3862. (void)ctx;
  3863. #ifndef NO_WOLFSSL_CLIENT
  3864. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3865. #else
  3866. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3867. #endif
  3868. ExpectNotNull(ctx);
  3869. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3870. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3871. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3872. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3873. wolfSSL_CTX_free(ctx);
  3874. #endif
  3875. return EXPECT_RESULT();
  3876. }
  3877. static int test_wolfSSL_CTX_SetTmpDH_file(void)
  3878. {
  3879. EXPECT_DECLS;
  3880. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  3881. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3882. WOLFSSL_CTX *ctx = NULL;
  3883. (void)ctx;
  3884. #ifndef NO_WOLFSSL_CLIENT
  3885. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3886. #else
  3887. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3888. #endif
  3889. /* invalid context */
  3890. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  3891. dhParamFile, WOLFSSL_FILETYPE_PEM));
  3892. /* invalid dhParamFile file */
  3893. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3894. NULL, WOLFSSL_FILETYPE_PEM));
  3895. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3896. bogusFile, WOLFSSL_FILETYPE_PEM));
  3897. /* success */
  3898. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  3899. WOLFSSL_FILETYPE_PEM));
  3900. wolfSSL_CTX_free(ctx);
  3901. #endif
  3902. return EXPECT_RESULT();
  3903. }
  3904. static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
  3905. {
  3906. EXPECT_DECLS;
  3907. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  3908. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3909. WOLFSSL_CTX *ctx = NULL;
  3910. #ifndef NO_WOLFSSL_CLIENT
  3911. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3912. #else
  3913. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3914. #endif
  3915. /* invalid context */
  3916. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL,
  3917. dh_key_der_2048, sizeof_dh_key_der_2048,
  3918. WOLFSSL_FILETYPE_ASN1));
  3919. /* invalid dhParamFile file */
  3920. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  3921. 0, WOLFSSL_FILETYPE_ASN1));
  3922. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3923. dsa_key_der_2048, sizeof_dsa_key_der_2048,
  3924. WOLFSSL_FILETYPE_ASN1));
  3925. /* success */
  3926. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3927. dh_key_der_2048, sizeof_dh_key_der_2048,
  3928. WOLFSSL_FILETYPE_ASN1));
  3929. wolfSSL_CTX_free(ctx);
  3930. #endif
  3931. return EXPECT_RESULT();
  3932. }
  3933. static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  3934. {
  3935. EXPECT_DECLS;
  3936. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  3937. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3938. WOLFSSL_CTX *ctx;
  3939. (void)ctx;
  3940. #ifndef NO_WOLFSSL_CLIENT
  3941. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3942. #else
  3943. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3944. #endif
  3945. ExpectNotNull(ctx);
  3946. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  3947. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  3948. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  3949. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  3950. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3951. dh_key_der_2048, sizeof_dh_key_der_2048,
  3952. WOLFSSL_FILETYPE_ASN1));
  3953. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  3954. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3955. dh_key_der_2048, sizeof_dh_key_der_2048,
  3956. WOLFSSL_FILETYPE_ASN1));
  3957. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  3958. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  3959. dh_key_der_2048, sizeof_dh_key_der_2048,
  3960. WOLFSSL_FILETYPE_ASN1));
  3961. wolfSSL_CTX_free(ctx);
  3962. #endif
  3963. return EXPECT_RESULT();
  3964. }
  3965. static int test_wolfSSL_CTX_der_load_verify_locations(void)
  3966. {
  3967. EXPECT_DECLS;
  3968. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_DER_LOAD) && \
  3969. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3970. WOLFSSL_CTX* ctx = NULL;
  3971. const char* derCert = "./certs/server-cert.der";
  3972. const char* nullPath = NULL;
  3973. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  3974. const char* emptyPath = "";
  3975. /* der load Case 1 ctx NULL */
  3976. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3977. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3978. #ifndef NO_WOLFSSL_CLIENT
  3979. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3980. #else
  3981. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3982. #endif
  3983. /* Case 2 filePath NULL */
  3984. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  3985. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3986. /* Case 3 invalid format */
  3987. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3988. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  3989. /* Case 4 filePath not valid */
  3990. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  3991. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3992. /* Case 5 filePath empty */
  3993. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  3994. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  3995. #ifndef NO_RSA
  3996. /* Case 6 success case */
  3997. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  3998. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3999. #endif
  4000. wolfSSL_CTX_free(ctx);
  4001. #endif
  4002. return EXPECT_RESULT();
  4003. }
  4004. static int test_wolfSSL_CTX_enable_disable(void)
  4005. {
  4006. EXPECT_DECLS;
  4007. #ifndef NO_CERTS
  4008. WOLFSSL_CTX* ctx = NULL;
  4009. #ifdef HAVE_CRL
  4010. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  4011. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  4012. #endif
  4013. #ifdef HAVE_OCSP
  4014. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  4015. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  4016. #endif
  4017. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  4018. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  4019. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  4020. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  4021. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  4022. ExpectIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  4023. #endif
  4024. #ifndef NO_WOLFSSL_CLIENT
  4025. #ifdef HAVE_EXTENDED_MASTER
  4026. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  4027. #endif
  4028. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4029. #ifdef HAVE_EXTENDED_MASTER
  4030. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  4031. #endif
  4032. #elif !defined(NO_WOLFSSL_SERVER)
  4033. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4034. #endif
  4035. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  4036. #ifdef HAVE_CRL
  4037. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  4038. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  4039. #endif
  4040. #ifdef HAVE_OCSP
  4041. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  4042. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  4043. WOLFSSL_SUCCESS);
  4044. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  4045. WOLFSSL_SUCCESS);
  4046. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  4047. WOLFSSL_SUCCESS);
  4048. #endif
  4049. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  4050. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  4051. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  4052. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  4053. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  4054. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  4055. #endif
  4056. wolfSSL_CTX_free(ctx);
  4057. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  4058. #endif /* NO_CERTS */
  4059. return EXPECT_RESULT();
  4060. }
  4061. static int test_wolfSSL_CTX_ticket_API(void)
  4062. {
  4063. EXPECT_DECLS;
  4064. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  4065. WOLFSSL_CTX* ctx = NULL;
  4066. void *userCtx = (void*)"this is my ctx";
  4067. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4068. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  4069. ExpectTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  4070. wolfSSL_CTX_free(ctx);
  4071. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  4072. ExpectNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  4073. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  4074. return EXPECT_RESULT();
  4075. }
  4076. static int test_wolfSSL_set_minmax_proto_version(void)
  4077. {
  4078. EXPECT_DECLS;
  4079. #ifdef OPENSSL_EXTRA
  4080. WOLFSSL_CTX *ctx = NULL;
  4081. WOLFSSL *ssl = NULL;
  4082. (void)ssl;
  4083. #ifndef NO_WOLFSSL_CLIENT
  4084. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4085. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4086. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4087. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4088. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  4089. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  4090. ExpectIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4091. ExpectIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  4092. ExpectIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4093. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  4094. wolfSSL_free(ssl);
  4095. wolfSSL_CTX_free(ctx);
  4096. ctx = NULL;
  4097. #endif
  4098. #ifndef NO_WOLFSSL_SERVER
  4099. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4100. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4101. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4102. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  4103. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  4104. wolfSSL_CTX_free(ctx);
  4105. #endif
  4106. #endif
  4107. return EXPECT_RESULT();
  4108. }
  4109. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
  4110. defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  4111. static int test_wolfSSL_CTX_set_max_proto_version_on_result(WOLFSSL* ssl)
  4112. {
  4113. EXPECT_DECLS;
  4114. ExpectStrEQ(wolfSSL_get_version(ssl), "TLSv1.2");
  4115. return EXPECT_RESULT();
  4116. }
  4117. static int test_wolfSSL_CTX_set_max_proto_version_ctx_ready(WOLFSSL_CTX* ctx)
  4118. {
  4119. EXPECT_DECLS;
  4120. /* Set TLS 1.2 */
  4121. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  4122. WOLFSSL_SUCCESS);
  4123. return EXPECT_RESULT();
  4124. }
  4125. /* Test using wolfSSL_CTX_set_max_proto_version to limit the version below
  4126. * what was set at ctx creation. */
  4127. static int test_wolfSSL_CTX_set_max_proto_version(void)
  4128. {
  4129. EXPECT_DECLS;
  4130. test_ssl_cbf client_cbs;
  4131. test_ssl_cbf server_cbs;
  4132. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  4133. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  4134. client_cbs.method = wolfTLS_client_method;
  4135. server_cbs.method = wolfTLS_server_method;
  4136. server_cbs.ctx_ready = test_wolfSSL_CTX_set_max_proto_version_ctx_ready;
  4137. client_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  4138. server_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  4139. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  4140. &server_cbs, NULL), TEST_SUCCESS);
  4141. return EXPECT_RESULT();
  4142. }
  4143. #else
  4144. static int test_wolfSSL_CTX_set_max_proto_version(void)
  4145. {
  4146. return TEST_SKIPPED;
  4147. }
  4148. #endif
  4149. /*----------------------------------------------------------------------------*
  4150. | SSL
  4151. *----------------------------------------------------------------------------*/
  4152. static int test_server_wolfSSL_new(void)
  4153. {
  4154. EXPECT_DECLS;
  4155. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  4156. !defined(NO_WOLFSSL_SERVER)
  4157. WOLFSSL_CTX *ctx = NULL;
  4158. WOLFSSL_CTX *ctx_nocert = NULL;
  4159. WOLFSSL *ssl = NULL;
  4160. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4161. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4162. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4163. WOLFSSL_FILETYPE_PEM));
  4164. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4165. WOLFSSL_FILETYPE_PEM));
  4166. /* invalid context */
  4167. ExpectNull(ssl = wolfSSL_new(NULL));
  4168. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && \
  4169. !defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_INIT_CTX_KEY)
  4170. ExpectNull(ssl = wolfSSL_new(ctx_nocert));
  4171. #endif
  4172. /* success */
  4173. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4174. wolfSSL_free(ssl);
  4175. wolfSSL_CTX_free(ctx);
  4176. wolfSSL_CTX_free(ctx_nocert);
  4177. #endif
  4178. return EXPECT_RESULT();
  4179. }
  4180. static int test_client_wolfSSL_new(void)
  4181. {
  4182. EXPECT_DECLS;
  4183. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  4184. !defined(NO_WOLFSSL_CLIENT)
  4185. WOLFSSL_CTX *ctx = NULL;
  4186. WOLFSSL_CTX *ctx_nocert = NULL;
  4187. WOLFSSL *ssl = NULL;
  4188. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4189. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4190. ExpectTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  4191. /* invalid context */
  4192. ExpectNull(ssl = wolfSSL_new(NULL));
  4193. /* success */
  4194. ExpectNotNull(ssl = wolfSSL_new(ctx_nocert));
  4195. wolfSSL_free(ssl);
  4196. ssl = NULL;
  4197. /* success */
  4198. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4199. wolfSSL_free(ssl);
  4200. wolfSSL_CTX_free(ctx);
  4201. wolfSSL_CTX_free(ctx_nocert);
  4202. #endif
  4203. return EXPECT_RESULT();
  4204. }
  4205. static int test_wolfSSL_SetTmpDH_file(void)
  4206. {
  4207. EXPECT_DECLS;
  4208. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  4209. !defined(NO_WOLFSSL_SERVER)
  4210. WOLFSSL_CTX *ctx = NULL;
  4211. WOLFSSL *ssl = NULL;
  4212. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4213. #ifndef NO_RSA
  4214. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4215. WOLFSSL_FILETYPE_PEM));
  4216. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4217. WOLFSSL_FILETYPE_PEM));
  4218. #elif defined(HAVE_ECC)
  4219. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  4220. WOLFSSL_FILETYPE_PEM));
  4221. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  4222. WOLFSSL_FILETYPE_PEM));
  4223. #elif defined(HAVE_ED25519)
  4224. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  4225. WOLFSSL_FILETYPE_PEM));
  4226. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  4227. WOLFSSL_FILETYPE_PEM));
  4228. #elif defined(HAVE_ED448)
  4229. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  4230. WOLFSSL_FILETYPE_PEM));
  4231. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  4232. WOLFSSL_FILETYPE_PEM));
  4233. #endif
  4234. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4235. /* invalid ssl */
  4236. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  4237. dhParamFile, WOLFSSL_FILETYPE_PEM));
  4238. /* invalid dhParamFile file */
  4239. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  4240. NULL, WOLFSSL_FILETYPE_PEM));
  4241. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  4242. bogusFile, WOLFSSL_FILETYPE_PEM));
  4243. /* success */
  4244. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  4245. WOLFSSL_FILETYPE_PEM));
  4246. wolfSSL_free(ssl);
  4247. wolfSSL_CTX_free(ctx);
  4248. #endif
  4249. return EXPECT_RESULT();
  4250. }
  4251. static int test_wolfSSL_SetTmpDH_buffer(void)
  4252. {
  4253. EXPECT_DECLS;
  4254. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  4255. WOLFSSL_CTX *ctx = NULL;
  4256. WOLFSSL *ssl = NULL;
  4257. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4258. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  4259. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4260. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  4261. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4262. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4263. /* invalid ssl */
  4264. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  4265. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4266. /* invalid dhParamFile file */
  4267. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  4268. 0, WOLFSSL_FILETYPE_ASN1));
  4269. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  4270. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4271. /* success */
  4272. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4273. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4274. wolfSSL_free(ssl);
  4275. wolfSSL_CTX_free(ctx);
  4276. #endif
  4277. return EXPECT_RESULT();
  4278. }
  4279. static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
  4280. {
  4281. EXPECT_DECLS;
  4282. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  4283. WOLFSSL_CTX *ctx = NULL;
  4284. WOLFSSL_CTX *ctx2 = NULL;
  4285. WOLFSSL *ssl = NULL;
  4286. WOLFSSL *ssl2 = NULL;
  4287. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4288. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  4289. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4290. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  4291. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4292. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  4293. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4294. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4295. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  4296. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4297. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  4298. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4299. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  4300. ExpectNotNull(ssl2 = wolfSSL_new(ctx2));
  4301. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4302. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4303. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  4304. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4305. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4306. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  4307. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4308. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4309. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  4310. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4311. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  4312. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  4313. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4314. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  4315. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4316. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4317. wolfSSL_free(ssl2);
  4318. wolfSSL_CTX_free(ctx2);
  4319. wolfSSL_free(ssl);
  4320. wolfSSL_CTX_free(ctx);
  4321. #endif
  4322. return EXPECT_RESULT();
  4323. }
  4324. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  4325. * allowed.
  4326. * POST: return 1 on success.
  4327. */
  4328. static int test_wolfSSL_SetMinVersion(void)
  4329. {
  4330. int res = TEST_SKIPPED;
  4331. #ifndef NO_WOLFSSL_CLIENT
  4332. int failFlag = WOLFSSL_SUCCESS;
  4333. WOLFSSL_CTX* ctx = NULL;
  4334. WOLFSSL* ssl = NULL;
  4335. int itr;
  4336. #ifndef NO_OLD_TLS
  4337. const int versions[] = {
  4338. #ifdef WOLFSSL_ALLOW_TLSV10
  4339. WOLFSSL_TLSV1,
  4340. #endif
  4341. WOLFSSL_TLSV1_1,
  4342. WOLFSSL_TLSV1_2};
  4343. #elif !defined(WOLFSSL_NO_TLS12)
  4344. const int versions[] = { WOLFSSL_TLSV1_2 };
  4345. #else
  4346. const int versions[] = { WOLFSSL_TLSV1_3 };
  4347. #endif
  4348. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  4349. ssl = wolfSSL_new(ctx);
  4350. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  4351. if (wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS) {
  4352. failFlag = WOLFSSL_FAILURE;
  4353. }
  4354. }
  4355. wolfSSL_free(ssl);
  4356. wolfSSL_CTX_free(ctx);
  4357. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  4358. #endif
  4359. return res;
  4360. } /* END test_wolfSSL_SetMinVersion */
  4361. #ifdef OPENSSL_EXTRA
  4362. static int test_EC25519(void)
  4363. {
  4364. EXPECT_DECLS;
  4365. #if defined(HAVE_CURVE25519) && defined(WOLFSSL_KEY_GEN)
  4366. byte priv[CURVE25519_KEYSIZE];
  4367. unsigned int privSz = CURVE25519_KEYSIZE;
  4368. byte pub[CURVE25519_KEYSIZE];
  4369. unsigned int pubSz = CURVE25519_KEYSIZE;
  4370. byte priv2[CURVE25519_KEYSIZE];
  4371. unsigned int priv2Sz = CURVE25519_KEYSIZE;
  4372. byte pub2[CURVE25519_KEYSIZE];
  4373. unsigned int pub2Sz = CURVE25519_KEYSIZE;
  4374. byte shared[CURVE25519_KEYSIZE];
  4375. unsigned int sharedSz = CURVE25519_KEYSIZE;
  4376. byte shared2[CURVE25519_KEYSIZE];
  4377. unsigned int shared2Sz = CURVE25519_KEYSIZE;
  4378. /* Bad parameter testing of key generation. */
  4379. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, NULL, NULL, NULL), 0);
  4380. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, &privSz, NULL, &pubSz), 0);
  4381. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4382. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, NULL, pub, &pubSz), 0);
  4383. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4384. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, NULL), 0);
  4385. /* Bad length */
  4386. privSz = 1;
  4387. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4388. privSz = CURVE25519_KEYSIZE;
  4389. pubSz = 1;
  4390. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4391. pubSz = CURVE25519_KEYSIZE;
  4392. /* Good case of generating key. */
  4393. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 1);
  4394. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv2, &priv2Sz, pub2, &pub2Sz),
  4395. 1);
  4396. ExpectIntEQ(privSz, CURVE25519_KEYSIZE);
  4397. ExpectIntEQ(pubSz, CURVE25519_KEYSIZE);
  4398. /* Bad parameter testing of shared key. */
  4399. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, NULL, NULL, privSz,
  4400. NULL, pubSz), 0);
  4401. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, NULL, privSz,
  4402. NULL, pubSz), 0);
  4403. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, priv, privSz,
  4404. pub, pubSz), 0);
  4405. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, NULL, privSz,
  4406. pub, pubSz), 0);
  4407. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4408. NULL, pubSz), 0);
  4409. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, priv, privSz,
  4410. pub, pubSz), 0);
  4411. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, NULL, priv, privSz,
  4412. pub, pubSz), 0);
  4413. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, NULL, privSz,
  4414. pub, pubSz), 0);
  4415. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4416. NULL, pubSz), 0);
  4417. /* Bad length. */
  4418. sharedSz = 1;
  4419. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4420. pub, pubSz), 0);
  4421. sharedSz = CURVE25519_KEYSIZE;
  4422. privSz = 1;
  4423. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4424. pub, pubSz), 0);
  4425. privSz = CURVE25519_KEYSIZE;
  4426. pubSz = 1;
  4427. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4428. pub, pubSz), 0);
  4429. pubSz = CURVE25519_KEYSIZE;
  4430. /* Good case of shared key. */
  4431. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4432. pub2, pub2Sz), 1);
  4433. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared2, &shared2Sz, priv2, priv2Sz,
  4434. pub, pubSz), 1);
  4435. ExpectIntEQ(sharedSz, CURVE25519_KEYSIZE);
  4436. ExpectIntEQ(shared2Sz, CURVE25519_KEYSIZE);
  4437. ExpectIntEQ(XMEMCMP(shared, shared2, sharedSz), 0);
  4438. #endif /* HAVE_CURVE25519 && WOLFSSL_KEY_GEN */
  4439. return EXPECT_RESULT();
  4440. }
  4441. static int test_ED25519(void)
  4442. {
  4443. EXPECT_DECLS;
  4444. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  4445. defined(WOLFSSL_KEY_GEN)
  4446. byte priv[ED25519_PRV_KEY_SIZE];
  4447. unsigned int privSz = (unsigned int)sizeof(priv);
  4448. byte pub[ED25519_PUB_KEY_SIZE];
  4449. unsigned int pubSz = (unsigned int)sizeof(pub);
  4450. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4451. const char* msg = TEST_STRING;
  4452. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4453. byte sig[ED25519_SIG_SIZE];
  4454. unsigned int sigSz = (unsigned int)sizeof(sig);
  4455. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4456. /* Bad parameter testing of key generation. */
  4457. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, NULL, NULL), 0);
  4458. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, NULL, NULL, NULL), 0);
  4459. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, &privSz, NULL, NULL), 0);
  4460. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, pub, NULL), 0);
  4461. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, NULL, &pubSz), 0);
  4462. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4463. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, NULL, pub, &pubSz), 0);
  4464. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4465. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, NULL), 0);
  4466. /* Bad length. */
  4467. privSz = 1;
  4468. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4469. privSz = ED25519_PRV_KEY_SIZE;
  4470. pubSz = 1;
  4471. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4472. pubSz = ED25519_PUB_KEY_SIZE;
  4473. /* Good case of generating key. */
  4474. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  4475. 1);
  4476. ExpectIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  4477. ExpectIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  4478. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4479. /* Bad parameter testing of signing. */
  4480. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, NULL,
  4481. NULL), 0);
  4482. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, NULL, privSz, NULL,
  4483. NULL), 0);
  4484. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, priv, privSz, NULL,
  4485. NULL), 0);
  4486. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, sig,
  4487. NULL), 0);
  4488. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, NULL,
  4489. &sigSz), 0);
  4490. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, priv, privSz, sig,
  4491. &sigSz), 0);
  4492. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, NULL, privSz, sig,
  4493. &sigSz), 0);
  4494. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, NULL,
  4495. &sigSz), 0);
  4496. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4497. NULL), 0);
  4498. /* Bad length. */
  4499. privSz = 1;
  4500. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4501. &sigSz), 0);
  4502. privSz = ED25519_PRV_KEY_SIZE;
  4503. sigSz = 1;
  4504. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4505. &sigSz), 0);
  4506. sigSz = ED25519_SIG_SIZE;
  4507. /* Good case of signing. */
  4508. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4509. &sigSz), 1);
  4510. ExpectIntEQ(sigSz, ED25519_SIG_SIZE);
  4511. #ifdef HAVE_ED25519_VERIFY
  4512. /* Bad parameter testing of verification. */
  4513. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, NULL, pubSz, NULL,
  4514. sigSz), 0);
  4515. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, NULL, pubSz, NULL,
  4516. sigSz), 0);
  4517. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, pub, pubSz, NULL,
  4518. sigSz), 0);
  4519. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, NULL, pubSz, sig,
  4520. sigSz), 0);
  4521. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, pub, pubSz, sig,
  4522. sigSz), 0);
  4523. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, NULL, pubSz, sig,
  4524. sigSz), 0);
  4525. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, NULL,
  4526. sigSz), 0);
  4527. /* Bad length. */
  4528. pubSz = 1;
  4529. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4530. sigSz), 0);
  4531. pubSz = ED25519_PUB_KEY_SIZE;
  4532. sigSz = 1;
  4533. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4534. sigSz), 0);
  4535. sigSz = ED25519_SIG_SIZE;
  4536. /* Good case of verification. */
  4537. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4538. sigSz), 1);
  4539. /* Bad signature. */
  4540. if (EXPECT_SUCCESS()) {
  4541. sig[1] ^= 0x80;
  4542. }
  4543. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4544. sigSz), 0);
  4545. #endif /* HAVE_ED25519_VERIFY */
  4546. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4547. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4548. return EXPECT_RESULT();
  4549. }
  4550. static int test_EC448(void)
  4551. {
  4552. EXPECT_DECLS;
  4553. #if defined(HAVE_CURVE448) && defined(WOLFSSL_KEY_GEN)
  4554. byte priv[CURVE448_KEY_SIZE];
  4555. unsigned int privSz = CURVE448_KEY_SIZE;
  4556. byte pub[CURVE448_KEY_SIZE];
  4557. unsigned int pubSz = CURVE448_KEY_SIZE;
  4558. byte priv2[CURVE448_KEY_SIZE];
  4559. unsigned int priv2Sz = CURVE448_KEY_SIZE;
  4560. byte pub2[CURVE448_KEY_SIZE];
  4561. unsigned int pub2Sz = CURVE448_KEY_SIZE;
  4562. byte shared[CURVE448_KEY_SIZE];
  4563. unsigned int sharedSz = CURVE448_KEY_SIZE;
  4564. byte shared2[CURVE448_KEY_SIZE];
  4565. unsigned int shared2Sz = CURVE448_KEY_SIZE;
  4566. /* Bad parameter testing of key generation. */
  4567. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, NULL, NULL, NULL), 0);
  4568. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, &privSz, NULL, &pubSz), 0);
  4569. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4570. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, NULL, pub, &pubSz), 0);
  4571. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4572. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, NULL), 0);
  4573. /* Bad length. */
  4574. privSz = 1;
  4575. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4576. privSz = CURVE448_KEY_SIZE;
  4577. pubSz = 1;
  4578. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4579. pubSz = CURVE448_KEY_SIZE;
  4580. /* Good case of generating key. */
  4581. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 1);
  4582. ExpectIntEQ(wolfSSL_EC448_generate_key(priv2, &priv2Sz, pub2, &pub2Sz), 1);
  4583. ExpectIntEQ(privSz, CURVE448_KEY_SIZE);
  4584. ExpectIntEQ(pubSz, CURVE448_KEY_SIZE);
  4585. /* Bad parameter testing of shared key. */
  4586. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, NULL, NULL, privSz,
  4587. NULL, pubSz), 0);
  4588. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, NULL, privSz,
  4589. NULL, pubSz), 0);
  4590. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, priv, privSz,
  4591. pub, pubSz), 0);
  4592. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, NULL, privSz,
  4593. pub, pubSz), 0);
  4594. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4595. NULL, pubSz), 0);
  4596. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, priv, privSz,
  4597. pub, pubSz), 0);
  4598. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, NULL, priv, privSz,
  4599. pub, pubSz), 0);
  4600. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, NULL, privSz,
  4601. pub, pubSz), 0);
  4602. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4603. NULL, pubSz), 0);
  4604. /* Bad length. */
  4605. sharedSz = 1;
  4606. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4607. pub, pubSz), 0);
  4608. sharedSz = CURVE448_KEY_SIZE;
  4609. privSz = 1;
  4610. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4611. pub, pubSz), 0);
  4612. privSz = CURVE448_KEY_SIZE;
  4613. pubSz = 1;
  4614. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4615. pub, pubSz), 0);
  4616. pubSz = CURVE448_KEY_SIZE;
  4617. /* Good case of shared key. */
  4618. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4619. pub2, pub2Sz), 1);
  4620. ExpectIntEQ(wolfSSL_EC448_shared_key(shared2, &shared2Sz, priv2, priv2Sz,
  4621. pub, pubSz), 1);
  4622. ExpectIntEQ(sharedSz, CURVE448_KEY_SIZE);
  4623. ExpectIntEQ(shared2Sz, CURVE448_KEY_SIZE);
  4624. ExpectIntEQ(XMEMCMP(shared, shared2, sharedSz), 0);
  4625. #endif /* HAVE_CURVE448 && WOLFSSL_KEY_GEN */
  4626. return EXPECT_RESULT();
  4627. }
  4628. static int test_ED448(void)
  4629. {
  4630. EXPECT_DECLS;
  4631. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  4632. defined(WOLFSSL_KEY_GEN)
  4633. byte priv[ED448_PRV_KEY_SIZE];
  4634. unsigned int privSz = (unsigned int)sizeof(priv);
  4635. byte pub[ED448_PUB_KEY_SIZE];
  4636. unsigned int pubSz = (unsigned int)sizeof(pub);
  4637. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4638. const char* msg = TEST_STRING;
  4639. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4640. byte sig[ED448_SIG_SIZE];
  4641. unsigned int sigSz = (unsigned int)sizeof(sig);
  4642. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4643. /* Bad parameter testing of key generation. */
  4644. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, NULL, NULL), 0);
  4645. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, NULL, NULL, NULL), 0);
  4646. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, &privSz, NULL, NULL), 0);
  4647. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, pub, NULL), 0);
  4648. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, NULL, &pubSz), 0);
  4649. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4650. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, NULL, pub, &pubSz), 0);
  4651. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4652. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, NULL), 0);
  4653. /* Bad length. */
  4654. privSz = 1;
  4655. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4656. privSz = ED448_PRV_KEY_SIZE;
  4657. pubSz = 1;
  4658. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4659. pubSz = ED448_PUB_KEY_SIZE;
  4660. /* Good case of generating key. */
  4661. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 1);
  4662. ExpectIntEQ(privSz, ED448_PRV_KEY_SIZE);
  4663. ExpectIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  4664. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4665. /* Bad parameter testing of signing. */
  4666. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, NULL,
  4667. NULL), 0);
  4668. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, NULL, privSz, NULL,
  4669. NULL), 0);
  4670. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, priv, privSz, NULL,
  4671. NULL), 0);
  4672. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, sig,
  4673. NULL), 0);
  4674. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, NULL,
  4675. &sigSz), 0);
  4676. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, priv, privSz, sig,
  4677. &sigSz), 0);
  4678. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, NULL, privSz, sig,
  4679. &sigSz), 0);
  4680. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, NULL,
  4681. &sigSz), 0);
  4682. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4683. NULL), 0);
  4684. /* Bad length. */
  4685. privSz = 1;
  4686. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4687. &sigSz), 0);
  4688. privSz = ED448_PRV_KEY_SIZE;
  4689. sigSz = 1;
  4690. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4691. &sigSz), 0);
  4692. sigSz = ED448_SIG_SIZE;
  4693. /* Good case of signing. */
  4694. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4695. &sigSz), 1);
  4696. ExpectIntEQ(sigSz, ED448_SIG_SIZE);
  4697. #ifdef HAVE_ED448_VERIFY
  4698. /* Bad parameter testing of verification. */
  4699. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, NULL, pubSz, NULL,
  4700. sigSz), 0);
  4701. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, NULL, pubSz, NULL,
  4702. sigSz), 0);
  4703. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, pub, pubSz, NULL,
  4704. sigSz), 0);
  4705. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, NULL, pubSz, sig,
  4706. sigSz), 0);
  4707. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, pub, pubSz, sig,
  4708. sigSz), 0);
  4709. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, NULL, pubSz, sig,
  4710. sigSz), 0);
  4711. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, NULL,
  4712. sigSz), 0);
  4713. /* Bad length. */
  4714. pubSz = 1;
  4715. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4716. sigSz), 0);
  4717. pubSz = ED448_PUB_KEY_SIZE;
  4718. sigSz = 1;
  4719. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4720. sigSz), 0);
  4721. sigSz = ED448_SIG_SIZE;
  4722. /* Good case of verification. */
  4723. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4724. sigSz), 1);
  4725. /* Bad signature. */
  4726. if (EXPECT_SUCCESS()) {
  4727. sig[1] ^= 0x80;
  4728. }
  4729. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4730. sigSz), 0);
  4731. #endif /* HAVE_ED448_VERIFY */
  4732. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4733. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4734. return EXPECT_RESULT();
  4735. }
  4736. #endif /* OPENSSL_EXTRA */
  4737. #include <wolfssl/openssl/pem.h>
  4738. /*----------------------------------------------------------------------------*
  4739. | EVP
  4740. *----------------------------------------------------------------------------*/
  4741. static int test_wolfSSL_EVP_PKEY_print_public(void)
  4742. {
  4743. EXPECT_DECLS;
  4744. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  4745. WOLFSSL_BIO* rbio = NULL;
  4746. WOLFSSL_BIO* wbio = NULL;
  4747. WOLFSSL_EVP_PKEY* pkey = NULL;
  4748. char line[256] = { 0 };
  4749. char line1[256] = { 0 };
  4750. int i = 0;
  4751. /* test error cases */
  4752. ExpectIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  4753. /*
  4754. * test RSA public key print
  4755. * in this test, pass '3' for indent
  4756. */
  4757. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  4758. ExpectNotNull(rbio = BIO_new_mem_buf( client_keypub_der_1024,
  4759. sizeof_client_keypub_der_1024));
  4760. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4761. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4762. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  4763. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4764. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  4765. ExpectIntEQ(XSTRNCMP(line, line1, XSTRLEN(line1)), 0);
  4766. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4767. strcpy(line1, " Modulus:\n");
  4768. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4769. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4770. strcpy(line1, " 00:bc:73:0e:a8:49:f3:74:a2:a9:ef:18:a5:da:55:\n");
  4771. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4772. /* skip to the end of modulus element*/
  4773. for (i = 0; i < 8 ;i++) {
  4774. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4775. }
  4776. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4777. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  4778. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4779. /* should reach EOF */
  4780. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4781. EVP_PKEY_free(pkey);
  4782. pkey = NULL;
  4783. BIO_free(rbio);
  4784. BIO_free(wbio);
  4785. rbio = NULL;
  4786. wbio = NULL;
  4787. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  4788. /*
  4789. * test DSA public key print
  4790. */
  4791. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  4792. ExpectNotNull(rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  4793. sizeof_dsa_pub_key_der_2048));
  4794. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4795. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4796. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4797. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4798. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  4799. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4800. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4801. strcpy(line1, "pub:\n");
  4802. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4803. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4804. strcpy(line1,
  4805. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  4806. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4807. /* skip to the end of pub element*/
  4808. for (i = 0; i < 17 ;i++) {
  4809. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4810. }
  4811. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4812. strcpy(line1, "P:\n");
  4813. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4814. /* skip to the end of P element*/
  4815. for (i = 0; i < 18 ;i++) {
  4816. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4817. }
  4818. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4819. strcpy(line1, "Q:\n");
  4820. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4821. /* skip to the end of Q element*/
  4822. for (i = 0; i < 3 ;i++) {
  4823. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4824. }
  4825. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4826. strcpy(line1, "G:\n");
  4827. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4828. /* skip to the end of G element*/
  4829. for (i = 0; i < 18 ;i++) {
  4830. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4831. }
  4832. /* should reach EOF */
  4833. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4834. EVP_PKEY_free(pkey);
  4835. pkey = NULL;
  4836. BIO_free(rbio);
  4837. BIO_free(wbio);
  4838. rbio = NULL;
  4839. wbio = NULL;
  4840. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  4841. /*
  4842. * test ECC public key print
  4843. */
  4844. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  4845. ExpectNotNull(rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  4846. sizeof_ecc_clikeypub_der_256));
  4847. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4848. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4849. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4850. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4851. strcpy(line1, "Public-Key: (256 bit)\n");
  4852. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4853. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4854. strcpy(line1, "pub:\n");
  4855. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4856. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4857. strcpy(line1,
  4858. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  4859. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4860. /* skip to the end of pub element*/
  4861. for (i = 0; i < 4 ;i++) {
  4862. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4863. }
  4864. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4865. strcpy(line1, "ASN1 OID: prime256v1\n");
  4866. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4867. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4868. strcpy(line1, "NIST CURVE: P-256\n");
  4869. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4870. /* should reach EOF */
  4871. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4872. EVP_PKEY_free(pkey);
  4873. pkey = NULL;
  4874. BIO_free(rbio);
  4875. BIO_free(wbio);
  4876. rbio = NULL;
  4877. wbio = NULL;
  4878. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  4879. /*
  4880. * test DH public key print
  4881. */
  4882. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  4883. ExpectNotNull(rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  4884. sizeof_dh_pub_key_der_2048));
  4885. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4886. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4887. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL), 1);
  4888. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4889. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  4890. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4891. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4892. strcpy(line1, "public-key:\n");
  4893. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4894. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4895. strcpy(line1,
  4896. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  4897. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4898. /* skip to the end of public-key element*/
  4899. for (i = 0; i < 17 ;i++) {
  4900. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4901. }
  4902. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4903. strcpy(line1, "prime:\n");
  4904. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4905. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4906. strcpy(line1,
  4907. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  4908. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4909. /* skip to the end of prime element*/
  4910. for (i = 0; i < 17 ;i++) {
  4911. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4912. }
  4913. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4914. strcpy(line1, "generator: 2 (0x02)\n");
  4915. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4916. /* should reach EOF */
  4917. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4918. EVP_PKEY_free(pkey);
  4919. pkey = NULL;
  4920. BIO_free(rbio);
  4921. BIO_free(wbio);
  4922. rbio = NULL;
  4923. wbio = NULL;
  4924. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  4925. /* to prevent "unused variable" warning */
  4926. (void)pkey;
  4927. (void)wbio;
  4928. (void)rbio;
  4929. (void)line;
  4930. (void)line1;
  4931. (void)i;
  4932. #endif /* OPENSSL_EXTRA */
  4933. return EXPECT_RESULT();
  4934. }
  4935. /* Test functions for base64 encode/decode */
  4936. static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
  4937. {
  4938. EXPECT_DECLS;
  4939. #if defined(OPENSSL_EXTRA) && \
  4940. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  4941. EVP_ENCODE_CTX* ctx = NULL;
  4942. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4943. ExpectIntEQ(ctx->remaining,0);
  4944. ExpectIntEQ(ctx->data[0],0);
  4945. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1],0);
  4946. EVP_ENCODE_CTX_free(ctx);
  4947. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  4948. return EXPECT_RESULT();
  4949. }
  4950. static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
  4951. {
  4952. EXPECT_DECLS;
  4953. #if defined(OPENSSL_EXTRA) && \
  4954. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  4955. EVP_ENCODE_CTX* ctx = NULL;
  4956. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4957. EVP_ENCODE_CTX_free(ctx);
  4958. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  4959. return EXPECT_RESULT();
  4960. }
  4961. static int test_wolfSSL_EVP_EncodeInit(void)
  4962. {
  4963. EXPECT_DECLS;
  4964. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  4965. EVP_ENCODE_CTX* ctx = NULL;
  4966. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  4967. ExpectIntEQ(ctx->remaining, 0);
  4968. ExpectIntEQ(ctx->data[0], 0);
  4969. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  4970. if (ctx != NULL) {
  4971. /* make ctx dirty */
  4972. ctx->remaining = 10;
  4973. XMEMSET(ctx->data, 0x77, sizeof(ctx->data));
  4974. }
  4975. EVP_EncodeInit(ctx);
  4976. ExpectIntEQ(ctx->remaining, 0);
  4977. ExpectIntEQ(ctx->data[0], 0);
  4978. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  4979. EVP_ENCODE_CTX_free(ctx);
  4980. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  4981. return EXPECT_RESULT();
  4982. }
  4983. static int test_wolfSSL_EVP_EncodeUpdate(void)
  4984. {
  4985. EXPECT_DECLS;
  4986. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  4987. int outl;
  4988. int total;
  4989. const unsigned char plain0[] = {"Th"};
  4990. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  4991. const unsigned char plain2[] = {"This is additional data."};
  4992. const unsigned char encBlock0[] = {"VGg="};
  4993. const unsigned char enc0[] = {"VGg=\n"};
  4994. /* expected encoded result for the first output 64 chars plus trailing LF*/
  4995. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  4996. const unsigned char enc2[] =
  4997. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  4998. unsigned char encOutBuff[300];
  4999. EVP_ENCODE_CTX* ctx = NULL;
  5000. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5001. EVP_EncodeInit(ctx);
  5002. /* illegal parameter test */
  5003. ExpectIntEQ(
  5004. EVP_EncodeUpdate(
  5005. NULL, /* pass NULL as ctx */
  5006. encOutBuff,
  5007. &outl,
  5008. plain1,
  5009. sizeof(plain1)-1),
  5010. 0 /* expected result code 0: fail */
  5011. );
  5012. ExpectIntEQ(
  5013. EVP_EncodeUpdate(
  5014. ctx,
  5015. NULL, /* pass NULL as out buff */
  5016. &outl,
  5017. plain1,
  5018. sizeof(plain1)-1),
  5019. 0 /* expected result code 0: fail */
  5020. );
  5021. ExpectIntEQ(
  5022. EVP_EncodeUpdate(
  5023. ctx,
  5024. encOutBuff,
  5025. NULL, /* pass NULL as outl */
  5026. plain1,
  5027. sizeof(plain1)-1),
  5028. 0 /* expected result code 0: fail */
  5029. );
  5030. ExpectIntEQ(
  5031. EVP_EncodeUpdate(
  5032. ctx,
  5033. encOutBuff,
  5034. &outl,
  5035. NULL, /* pass NULL as in */
  5036. sizeof(plain1)-1),
  5037. 0 /* expected result code 0: fail */
  5038. );
  5039. ExpectIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
  5040. /* meaningless parameter test */
  5041. ExpectIntEQ(
  5042. EVP_EncodeUpdate(
  5043. ctx,
  5044. encOutBuff,
  5045. &outl,
  5046. plain1,
  5047. 0), /* pass zero input */
  5048. 1 /* expected result code 1: success */
  5049. );
  5050. /* very small data encoding test */
  5051. EVP_EncodeInit(ctx);
  5052. ExpectIntEQ(
  5053. EVP_EncodeUpdate(
  5054. ctx,
  5055. encOutBuff,
  5056. &outl,
  5057. plain0,
  5058. sizeof(plain0)-1),
  5059. 1 /* expected result code 1: success */
  5060. );
  5061. ExpectIntEQ(outl,0);
  5062. if (EXPECT_SUCCESS()) {
  5063. EVP_EncodeFinal(
  5064. ctx,
  5065. encOutBuff + outl,
  5066. &outl);
  5067. }
  5068. ExpectIntEQ( outl, sizeof(enc0)-1);
  5069. ExpectIntEQ(
  5070. XSTRNCMP(
  5071. (const char*)encOutBuff,
  5072. (const char*)enc0,sizeof(enc0) ),
  5073. 0);
  5074. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  5075. ExpectIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
  5076. sizeof(encBlock0)-1);
  5077. ExpectStrEQ(encOutBuff, encBlock0);
  5078. /* pass small size( < 48bytes ) input, then make sure they are not
  5079. * encoded and just stored in ctx
  5080. */
  5081. EVP_EncodeInit(ctx);
  5082. total = 0;
  5083. outl = 0;
  5084. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  5085. ExpectIntEQ(
  5086. EVP_EncodeUpdate(
  5087. ctx,
  5088. encOutBuff, /* buffer for output */
  5089. &outl, /* size of output */
  5090. plain1, /* input */
  5091. sizeof(plain1)-1), /* size of input */
  5092. 1); /* expected result code 1:success */
  5093. total += outl;
  5094. ExpectIntEQ(outl, 0); /* no output expected */
  5095. ExpectIntEQ(ctx->remaining, sizeof(plain1) -1);
  5096. ExpectTrue(
  5097. XSTRNCMP((const char*)(ctx->data),
  5098. (const char*)plain1,
  5099. ctx->remaining) ==0 );
  5100. ExpectTrue(encOutBuff[0] == 0);
  5101. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  5102. * the stored data and the new input together
  5103. */
  5104. ExpectIntEQ(
  5105. EVP_EncodeUpdate(
  5106. ctx,
  5107. encOutBuff + outl, /* buffer for output */
  5108. &outl, /* size of output */
  5109. plain2, /* additional input */
  5110. sizeof(plain2) -1), /* size of additional input */
  5111. 1); /* expected result code 1:success */
  5112. total += outl;
  5113. ExpectIntNE(outl, 0); /* some output is expected this time*/
  5114. ExpectIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  5115. ExpectIntEQ(
  5116. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  5117. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  5118. EVP_EncodeFinal(
  5119. ctx,
  5120. encOutBuff + outl,
  5121. &outl);
  5122. total += outl;
  5123. ExpectIntNE(total,0);
  5124. ExpectIntNE(outl,0);
  5125. ExpectIntEQ(XSTRNCMP(
  5126. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  5127. /* test with illeagal parameters */
  5128. outl = 1;
  5129. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  5130. ExpectIntEQ(outl, 0);
  5131. outl = 1;
  5132. EVP_EncodeFinal(ctx, NULL, &outl);
  5133. ExpectIntEQ(outl, 0);
  5134. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  5135. EVP_EncodeFinal(NULL, NULL, NULL);
  5136. EVP_ENCODE_CTX_free(ctx);
  5137. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  5138. return EXPECT_RESULT();
  5139. }
  5140. static int test_wolfSSL_EVP_EncodeFinal(void)
  5141. {
  5142. int res = TEST_SKIPPED;
  5143. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  5144. /* tests for wolfSSL_EVP_EncodeFinal are included in
  5145. * test_wolfSSL_EVP_EncodeUpdate
  5146. */
  5147. res = TEST_SUCCESS;
  5148. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  5149. return res;
  5150. }
  5151. static int test_wolfSSL_EVP_DecodeInit(void)
  5152. {
  5153. EXPECT_DECLS;
  5154. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5155. EVP_ENCODE_CTX* ctx = NULL;
  5156. ExpectNotNull( ctx = EVP_ENCODE_CTX_new());
  5157. ExpectIntEQ( ctx->remaining,0);
  5158. ExpectIntEQ( ctx->data[0],0);
  5159. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  5160. if (ctx != NULL) {
  5161. /* make ctx dirty */
  5162. ctx->remaining = 10;
  5163. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  5164. }
  5165. EVP_DecodeInit(ctx);
  5166. ExpectIntEQ( ctx->remaining,0);
  5167. ExpectIntEQ( ctx->data[0],0);
  5168. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  5169. EVP_ENCODE_CTX_free(ctx);
  5170. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5171. return EXPECT_RESULT();
  5172. }
  5173. static int test_wolfSSL_EVP_DecodeUpdate(void)
  5174. {
  5175. EXPECT_DECLS;
  5176. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5177. int outl;
  5178. unsigned char decOutBuff[300];
  5179. EVP_ENCODE_CTX* ctx = NULL;
  5180. static const unsigned char enc1[] =
  5181. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  5182. /* const unsigned char plain1[] =
  5183. {"This is a base64 decoding test."} */
  5184. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5185. EVP_DecodeInit(ctx);
  5186. /* illegal parameter tests */
  5187. /* pass NULL as ctx */
  5188. ExpectIntEQ(
  5189. EVP_DecodeUpdate(
  5190. NULL, /* pass NULL as ctx */
  5191. decOutBuff,
  5192. &outl,
  5193. enc1,
  5194. sizeof(enc1)-1),
  5195. -1 /* expected result code -1: fail */
  5196. );
  5197. ExpectIntEQ( outl, 0);
  5198. /* pass NULL as output */
  5199. ExpectIntEQ(
  5200. EVP_DecodeUpdate(
  5201. ctx,
  5202. NULL, /* pass NULL as out buff */
  5203. &outl,
  5204. enc1,
  5205. sizeof(enc1)-1),
  5206. -1 /* expected result code -1: fail */
  5207. );
  5208. ExpectIntEQ( outl, 0);
  5209. /* pass NULL as outl */
  5210. ExpectIntEQ(
  5211. EVP_DecodeUpdate(
  5212. ctx,
  5213. decOutBuff,
  5214. NULL, /* pass NULL as outl */
  5215. enc1,
  5216. sizeof(enc1)-1),
  5217. -1 /* expected result code -1: fail */
  5218. );
  5219. /* pass NULL as input */
  5220. ExpectIntEQ(
  5221. EVP_DecodeUpdate(
  5222. ctx,
  5223. decOutBuff,
  5224. &outl,
  5225. NULL, /* pass NULL as in */
  5226. sizeof(enc1)-1),
  5227. -1 /* expected result code -1: fail */
  5228. );
  5229. ExpectIntEQ( outl, 0);
  5230. ExpectIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
  5231. /* pass zero length input */
  5232. ExpectIntEQ(
  5233. EVP_DecodeUpdate(
  5234. ctx,
  5235. decOutBuff,
  5236. &outl,
  5237. enc1,
  5238. 0), /* pass zero as input len */
  5239. 1 /* expected result code 1: success */
  5240. );
  5241. /* decode correct base64 string */
  5242. {
  5243. static const unsigned char enc2[] =
  5244. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  5245. static const unsigned char plain2[] =
  5246. {"This is a base64 decoding test."};
  5247. EVP_EncodeInit(ctx);
  5248. ExpectIntEQ(
  5249. EVP_DecodeUpdate(
  5250. ctx,
  5251. decOutBuff,
  5252. &outl,
  5253. enc2,
  5254. sizeof(enc2)-1),
  5255. 0 /* expected result code 0: success */
  5256. );
  5257. ExpectIntEQ(outl,sizeof(plain2) -1);
  5258. ExpectIntEQ(
  5259. EVP_DecodeFinal(
  5260. ctx,
  5261. decOutBuff + outl,
  5262. &outl),
  5263. 1 /* expected result code 1: success */
  5264. );
  5265. ExpectIntEQ(outl, 0); /* expected DecodeFinal output no data */
  5266. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  5267. sizeof(plain2) -1 ),0);
  5268. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
  5269. sizeof(plain2)-1);
  5270. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  5271. sizeof(plain2) -1 ),0);
  5272. }
  5273. /* decode correct base64 string which does not have '\n' in its last*/
  5274. {
  5275. static const unsigned char enc3[] =
  5276. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  5277. static const unsigned char plain3[] =
  5278. {"This is a base64 decoding test."}; /* 31 chars */
  5279. EVP_EncodeInit(ctx);
  5280. ExpectIntEQ(
  5281. EVP_DecodeUpdate(
  5282. ctx,
  5283. decOutBuff,
  5284. &outl,
  5285. enc3,
  5286. sizeof(enc3)-1),
  5287. 0 /* expected result code 0: success */
  5288. );
  5289. ExpectIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  5290. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  5291. sizeof(plain3) -1 ),0);
  5292. ExpectIntEQ(
  5293. EVP_DecodeFinal(
  5294. ctx,
  5295. decOutBuff + outl,
  5296. &outl),
  5297. 1 /* expected result code 1: success */
  5298. );
  5299. ExpectIntEQ(outl,0 );
  5300. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
  5301. sizeof(plain3)-1);
  5302. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  5303. sizeof(plain3) -1 ),0);
  5304. }
  5305. /* decode string which has a padding char ('=') in the illegal position*/
  5306. {
  5307. static const unsigned char enc4[] =
  5308. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  5309. EVP_EncodeInit(ctx);
  5310. ExpectIntEQ(
  5311. EVP_DecodeUpdate(
  5312. ctx,
  5313. decOutBuff,
  5314. &outl,
  5315. enc4,
  5316. sizeof(enc4)-1),
  5317. -1 /* expected result code -1: error */
  5318. );
  5319. ExpectIntEQ(outl,0);
  5320. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
  5321. }
  5322. /* small data decode test */
  5323. {
  5324. static const unsigned char enc00[] = {"VG"};
  5325. static const unsigned char enc01[] = {"g=\n"};
  5326. static const unsigned char plain4[] = {"Th"};
  5327. EVP_EncodeInit(ctx);
  5328. ExpectIntEQ(
  5329. EVP_DecodeUpdate(
  5330. ctx,
  5331. decOutBuff,
  5332. &outl,
  5333. enc00,
  5334. sizeof(enc00)-1),
  5335. 1 /* expected result code 1: success */
  5336. );
  5337. ExpectIntEQ(outl,0);
  5338. ExpectIntEQ(
  5339. EVP_DecodeUpdate(
  5340. ctx,
  5341. decOutBuff + outl,
  5342. &outl,
  5343. enc01,
  5344. sizeof(enc01)-1),
  5345. 0 /* expected result code 0: success */
  5346. );
  5347. ExpectIntEQ(outl,sizeof(plain4)-1);
  5348. /* test with illegal parameters */
  5349. ExpectIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  5350. ExpectIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  5351. ExpectIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  5352. ExpectIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  5353. if (EXPECT_SUCCESS()) {
  5354. EVP_DecodeFinal(
  5355. ctx,
  5356. decOutBuff + outl,
  5357. &outl);
  5358. }
  5359. ExpectIntEQ( outl, 0);
  5360. ExpectIntEQ(
  5361. XSTRNCMP(
  5362. (const char*)decOutBuff,
  5363. (const char*)plain4,sizeof(plain4)-1 ),
  5364. 0);
  5365. }
  5366. EVP_ENCODE_CTX_free(ctx);
  5367. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5368. return EXPECT_RESULT();
  5369. }
  5370. static int test_wolfSSL_EVP_DecodeFinal(void)
  5371. {
  5372. int res = TEST_SKIPPED;
  5373. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5374. /* tests for wolfSSL_EVP_DecodeFinal are included in
  5375. * test_wolfSSL_EVP_DecodeUpdate
  5376. */
  5377. res = TEST_SUCCESS;
  5378. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5379. return res;
  5380. }
  5381. /* Test function for wolfSSL_EVP_get_cipherbynid.
  5382. */
  5383. #ifdef OPENSSL_EXTRA
  5384. static int test_wolfSSL_EVP_get_cipherbynid(void)
  5385. {
  5386. EXPECT_DECLS;
  5387. #ifndef NO_AES
  5388. const WOLFSSL_EVP_CIPHER* c;
  5389. c = wolfSSL_EVP_get_cipherbynid(419);
  5390. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5391. defined(WOLFSSL_AES_128)
  5392. ExpectNotNull(c);
  5393. ExpectNotNull(XSTRCMP("EVP_AES_128_CBC", c));
  5394. #else
  5395. ExpectNull(c);
  5396. #endif
  5397. c = wolfSSL_EVP_get_cipherbynid(423);
  5398. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5399. defined(WOLFSSL_AES_192)
  5400. ExpectNotNull(c);
  5401. ExpectNotNull(XSTRCMP("EVP_AES_192_CBC", c));
  5402. #else
  5403. ExpectNull(c);
  5404. #endif
  5405. c = wolfSSL_EVP_get_cipherbynid(427);
  5406. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5407. defined(WOLFSSL_AES_256)
  5408. ExpectNotNull(c);
  5409. ExpectNotNull(XSTRCMP("EVP_AES_256_CBC", c));
  5410. #else
  5411. ExpectNull(c);
  5412. #endif
  5413. c = wolfSSL_EVP_get_cipherbynid(904);
  5414. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  5415. ExpectNotNull(c);
  5416. ExpectNotNull(XSTRCMP("EVP_AES_128_CTR", c));
  5417. #else
  5418. ExpectNull(c);
  5419. #endif
  5420. c = wolfSSL_EVP_get_cipherbynid(905);
  5421. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  5422. ExpectNotNull(c);
  5423. ExpectNotNull(XSTRCMP("EVP_AES_192_CTR", c));
  5424. #else
  5425. ExpectNull(c);
  5426. #endif
  5427. c = wolfSSL_EVP_get_cipherbynid(906);
  5428. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  5429. ExpectNotNull(c);
  5430. ExpectNotNull(XSTRCMP("EVP_AES_256_CTR", c));
  5431. #else
  5432. ExpectNull(c);
  5433. #endif
  5434. c = wolfSSL_EVP_get_cipherbynid(418);
  5435. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  5436. ExpectNotNull(c);
  5437. ExpectNotNull(XSTRCMP("EVP_AES_128_ECB", c));
  5438. #else
  5439. ExpectNull(c);
  5440. #endif
  5441. c = wolfSSL_EVP_get_cipherbynid(422);
  5442. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  5443. ExpectNotNull(c);
  5444. ExpectNotNull(XSTRCMP("EVP_AES_192_ECB", c));
  5445. #else
  5446. ExpectNull(c);
  5447. #endif
  5448. c = wolfSSL_EVP_get_cipherbynid(426);
  5449. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  5450. ExpectNotNull(c);
  5451. ExpectNotNull(XSTRCMP("EVP_AES_256_ECB", c));
  5452. #else
  5453. ExpectNull(c);
  5454. #endif
  5455. #endif /* !NO_AES */
  5456. #ifndef NO_DES3
  5457. ExpectNotNull(XSTRCMP("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  5458. #ifdef WOLFSSL_DES_ECB
  5459. ExpectNotNull(XSTRCMP("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  5460. #endif
  5461. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  5462. #ifdef WOLFSSL_DES_ECB
  5463. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  5464. #endif
  5465. #endif /* !NO_DES3 */
  5466. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  5467. ExpectNotNull(XSTRCMP("EVP_CHACHA20_POLY13O5", EVP_get_cipherbynid(1018)));
  5468. #endif
  5469. /* test for nid is out of range */
  5470. ExpectNull(wolfSSL_EVP_get_cipherbynid(1));
  5471. return EXPECT_RESULT();
  5472. }
  5473. static int test_wolfSSL_EVP_CIPHER_CTX(void)
  5474. {
  5475. EXPECT_DECLS;
  5476. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  5477. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  5478. const EVP_CIPHER *init = EVP_aes_128_cbc();
  5479. const EVP_CIPHER *test;
  5480. byte key[AES_BLOCK_SIZE] = {0};
  5481. byte iv[AES_BLOCK_SIZE] = {0};
  5482. ExpectNotNull(ctx);
  5483. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  5484. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  5485. test = EVP_CIPHER_CTX_cipher(ctx);
  5486. ExpectTrue(init == test);
  5487. ExpectIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  5488. ExpectIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  5489. ExpectIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  5490. EVP_CIPHER_CTX_free(ctx);
  5491. /* test EVP_CIPHER_CTX_cleanup with NULL */
  5492. ExpectIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  5493. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  5494. return EXPECT_RESULT();
  5495. }
  5496. #endif /* OPENSSL_EXTRA */
  5497. /*----------------------------------------------------------------------------*
  5498. | IO
  5499. *----------------------------------------------------------------------------*/
  5500. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) || \
  5501. defined(HAVE_IO_TESTS_DEPENDENCIES)
  5502. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5503. #ifdef WC_SHA512_DIGEST_SIZE
  5504. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  5505. #else
  5506. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  5507. #endif
  5508. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  5509. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  5510. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  5511. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  5512. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5513. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  5514. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  5515. defined(HAVE_AES_CBC)
  5516. typedef struct openssl_key_ctx {
  5517. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  5518. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  5519. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  5520. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  5521. } openssl_key_ctx;
  5522. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  5523. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  5524. static WC_INLINE int OpenSSLTicketInit(void)
  5525. {
  5526. int ret = wc_InitRng(&myOpenSSLKey_rng);
  5527. if (ret != 0) return ret;
  5528. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  5529. sizeof(myOpenSSLKey_ctx.name));
  5530. if (ret != 0) return ret;
  5531. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  5532. sizeof(myOpenSSLKey_ctx.key));
  5533. if (ret != 0) return ret;
  5534. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  5535. sizeof(myOpenSSLKey_ctx.hmacKey));
  5536. if (ret != 0) return ret;
  5537. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  5538. sizeof(myOpenSSLKey_ctx.iv));
  5539. if (ret != 0) return ret;
  5540. return 0;
  5541. }
  5542. static int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  5543. byte name[WOLFSSL_TICKET_NAME_SZ],
  5544. byte iv[WOLFSSL_TICKET_IV_SZ],
  5545. WOLFSSL_EVP_CIPHER_CTX *ectx,
  5546. WOLFSSL_HMAC_CTX *hctx, int enc) {
  5547. (void)ssl;
  5548. if (enc) {
  5549. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  5550. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  5551. }
  5552. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  5553. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  5554. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  5555. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  5556. return 0;
  5557. }
  5558. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  5559. if (enc)
  5560. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  5561. else
  5562. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  5563. return 1;
  5564. }
  5565. static WC_INLINE void OpenSSLTicketCleanup(void)
  5566. {
  5567. wc_FreeRng(&myOpenSSLKey_rng);
  5568. }
  5569. #endif
  5570. #endif
  5571. /* helper functions */
  5572. #ifdef HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  5573. static WC_INLINE int test_ssl_memio_write_cb(WOLFSSL *ssl, char *data, int sz,
  5574. void *ctx)
  5575. {
  5576. struct test_ssl_memio_ctx *test_ctx;
  5577. byte *buf;
  5578. int *len;
  5579. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  5580. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  5581. buf = test_ctx->c_buff;
  5582. len = &test_ctx->c_len;
  5583. }
  5584. else {
  5585. buf = test_ctx->s_buff;
  5586. len = &test_ctx->s_len;
  5587. }
  5588. if ((unsigned)(*len + sz) > TEST_SSL_MEMIO_BUF_SZ)
  5589. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  5590. XMEMCPY(buf + *len, data, sz);
  5591. *len += sz;
  5592. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  5593. {
  5594. /* This can be imported into Wireshark by transforming the file with
  5595. * od -Ax -tx1 -v test_output.dump > test_output.dump.hex
  5596. * And then loading test_output.dump.hex into Wireshark using the
  5597. * "Import from Hex Dump..." option ion and selecting the TCP
  5598. * encapsulation option. */
  5599. char dump_file_name[64];
  5600. WOLFSSL_BIO *dump_file;
  5601. sprintf(dump_file_name, "%s/%s.dump", tmpDirName, currentTestName);
  5602. dump_file = wolfSSL_BIO_new_file(dump_file_name, "a");
  5603. if (dump_file != NULL) {
  5604. (void)wolfSSL_BIO_write(dump_file, data, sz);
  5605. wolfSSL_BIO_free(dump_file);
  5606. }
  5607. }
  5608. #endif
  5609. return sz;
  5610. }
  5611. static WC_INLINE int test_ssl_memio_read_cb(WOLFSSL *ssl, char *data, int sz,
  5612. void *ctx)
  5613. {
  5614. struct test_ssl_memio_ctx *test_ctx;
  5615. int read_sz;
  5616. byte *buf;
  5617. int *len;
  5618. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  5619. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  5620. buf = test_ctx->s_buff;
  5621. len = &test_ctx->s_len;
  5622. }
  5623. else {
  5624. buf = test_ctx->c_buff;
  5625. len = &test_ctx->c_len;
  5626. }
  5627. if (*len == 0)
  5628. return WOLFSSL_CBIO_ERR_WANT_READ;
  5629. read_sz = sz < *len ? sz : *len;
  5630. XMEMCPY(data, buf, read_sz);
  5631. XMEMMOVE(buf, buf + read_sz, *len - read_sz);
  5632. *len -= read_sz;
  5633. return read_sz;
  5634. }
  5635. static WC_INLINE int test_ssl_memio_setup(test_ssl_memio_ctx *ctx)
  5636. {
  5637. EXPECT_DECLS;
  5638. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5639. int c_sharedCtx = 0;
  5640. int s_sharedCtx = 0;
  5641. #endif
  5642. const char* clientCertFile = cliCertFile;
  5643. const char* clientKeyFile = cliKeyFile;
  5644. const char* serverCertFile = svrCertFile;
  5645. const char* serverKeyFile = svrKeyFile;
  5646. /********************************
  5647. * Create WOLFSSL_CTX for client.
  5648. ********************************/
  5649. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5650. if (ctx->c_ctx != NULL) {
  5651. c_sharedCtx = ctx->c_cb.isSharedCtx;
  5652. }
  5653. else
  5654. #endif
  5655. {
  5656. WOLFSSL_METHOD* method = NULL;
  5657. if (ctx->c_cb.method != NULL) {
  5658. method = ctx->c_cb.method();
  5659. }
  5660. else {
  5661. method = wolfSSLv23_client_method();
  5662. }
  5663. ExpectNotNull(ctx->c_ctx = wolfSSL_CTX_new(method));
  5664. }
  5665. wolfSSL_SetIORecv(ctx->c_ctx, test_ssl_memio_read_cb);
  5666. wolfSSL_SetIOSend(ctx->c_ctx, test_ssl_memio_write_cb);
  5667. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5668. wolfSSL_CTX_set_default_passwd_cb(ctx->c_ctx, PasswordCallBack);
  5669. #endif
  5670. if (ctx->c_cb.caPemFile != NULL)
  5671. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx,
  5672. ctx->c_cb.caPemFile, 0), WOLFSSL_SUCCESS);
  5673. else
  5674. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx,
  5675. caCertFile, 0), WOLFSSL_SUCCESS);
  5676. if (ctx->c_cb.certPemFile != NULL) {
  5677. clientCertFile = ctx->c_cb.certPemFile;
  5678. }
  5679. if (ctx->c_cb.keyPemFile != NULL) {
  5680. clientKeyFile = ctx->c_cb.keyPemFile;
  5681. }
  5682. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5683. if (!c_sharedCtx)
  5684. #endif
  5685. {
  5686. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->c_ctx,
  5687. clientCertFile), WOLFSSL_SUCCESS);
  5688. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->c_ctx, clientKeyFile,
  5689. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5690. }
  5691. #ifdef HAVE_CRL
  5692. if (ctx->c_cb.crlPemFile != NULL) {
  5693. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx->c_ctx, WOLFSSL_CRL_CHECKALL),
  5694. WOLFSSL_SUCCESS);
  5695. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx->c_ctx, ctx->c_cb.crlPemFile,
  5696. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5697. }
  5698. #endif
  5699. if (ctx->c_ciphers != NULL) {
  5700. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->c_ctx, ctx->c_ciphers),
  5701. WOLFSSL_SUCCESS);
  5702. }
  5703. if (ctx->c_cb.ctx_ready != NULL) {
  5704. ExpectIntEQ(ctx->c_cb.ctx_ready(ctx->c_ctx), TEST_SUCCESS);
  5705. }
  5706. /********************************
  5707. * Create WOLFSSL_CTX for server.
  5708. ********************************/
  5709. if (ctx->s_ctx != NULL) {
  5710. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5711. s_sharedCtx = 1;
  5712. #endif
  5713. ctx->s_cb.isSharedCtx = 1;
  5714. }
  5715. else
  5716. {
  5717. WOLFSSL_METHOD* method = NULL;
  5718. if (ctx->s_cb.method != NULL) {
  5719. method = ctx->s_cb.method();
  5720. }
  5721. else {
  5722. method = wolfSSLv23_server_method();
  5723. }
  5724. ExpectNotNull(ctx->s_ctx = wolfSSL_CTX_new(method));
  5725. ctx->s_cb.isSharedCtx = 0;
  5726. }
  5727. if (!ctx->s_cb.ticNoInit && (ctx->s_ctx != NULL)) {
  5728. #if defined(HAVE_SESSION_TICKET) && \
  5729. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5730. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5731. OpenSSLTicketInit();
  5732. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx->s_ctx, myTicketEncCbOpenSSL);
  5733. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5734. TicketInit();
  5735. wolfSSL_CTX_set_TicketEncCb(ctx->s_ctx, myTicketEncCb);
  5736. #endif
  5737. #endif
  5738. }
  5739. wolfSSL_SetIORecv(ctx->s_ctx, test_ssl_memio_read_cb);
  5740. wolfSSL_SetIOSend(ctx->s_ctx, test_ssl_memio_write_cb);
  5741. wolfSSL_CTX_set_verify(ctx->s_ctx, WOLFSSL_VERIFY_PEER |
  5742. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5743. if (ctx->s_cb.caPemFile != NULL)
  5744. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx,
  5745. ctx->s_cb.caPemFile, 0), WOLFSSL_SUCCESS);
  5746. else
  5747. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx,
  5748. cliCertFile, 0), WOLFSSL_SUCCESS);
  5749. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5750. wolfSSL_CTX_set_default_passwd_cb(ctx->s_ctx, PasswordCallBack);
  5751. #endif
  5752. if (ctx->s_cb.certPemFile != NULL) {
  5753. serverCertFile = ctx->s_cb.certPemFile;
  5754. }
  5755. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5756. if (!s_sharedCtx)
  5757. #endif
  5758. {
  5759. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->s_ctx,
  5760. serverCertFile), WOLFSSL_SUCCESS);
  5761. }
  5762. if (ctx->s_cb.keyPemFile != NULL) {
  5763. serverKeyFile = ctx->s_cb.keyPemFile;
  5764. }
  5765. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5766. if (!s_sharedCtx)
  5767. #endif
  5768. {
  5769. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->s_ctx, serverKeyFile,
  5770. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5771. }
  5772. if (ctx->s_ciphers != NULL) {
  5773. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->s_ctx, ctx->s_ciphers),
  5774. WOLFSSL_SUCCESS);
  5775. }
  5776. if (ctx->s_cb.ctx_ready != NULL) {
  5777. ExpectIntEQ(ctx->s_cb.ctx_ready(ctx->s_ctx), TEST_SUCCESS);
  5778. }
  5779. /****************************
  5780. * Create WOLFSSL for client.
  5781. ****************************/
  5782. ExpectNotNull(ctx->c_ssl = wolfSSL_new(ctx->c_ctx));
  5783. wolfSSL_SetIOWriteCtx(ctx->c_ssl, ctx);
  5784. wolfSSL_SetIOReadCtx(ctx->c_ssl, ctx);
  5785. if (0
  5786. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5787. || c_sharedCtx
  5788. #endif
  5789. )
  5790. {
  5791. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->c_ssl,
  5792. clientCertFile), WOLFSSL_SUCCESS);
  5793. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->c_ssl, clientKeyFile,
  5794. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5795. }
  5796. if (ctx->c_cb.ssl_ready != NULL) {
  5797. ExpectIntEQ(ctx->c_cb.ssl_ready(ctx->c_ssl), TEST_SUCCESS);
  5798. }
  5799. /****************************
  5800. * Create WOLFSSL for server.
  5801. ****************************/
  5802. ExpectNotNull(ctx->s_ssl = wolfSSL_new(ctx->s_ctx));
  5803. wolfSSL_SetIOWriteCtx(ctx->s_ssl, ctx);
  5804. wolfSSL_SetIOReadCtx(ctx->s_ssl, ctx);
  5805. if (0
  5806. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5807. || s_sharedCtx
  5808. #endif
  5809. )
  5810. {
  5811. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->s_ssl,
  5812. serverCertFile), WOLFSSL_SUCCESS);
  5813. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->s_ssl, serverKeyFile,
  5814. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5815. }
  5816. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5817. wolfSSL_SetTmpDH_file(ctx->s_ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5818. #elif !defined(NO_DH)
  5819. /* will repick suites with DHE, higher priority than PSK */
  5820. SetDH(ctx->s_ssl);
  5821. #endif
  5822. if (ctx->s_cb.ssl_ready != NULL) {
  5823. ExpectIntEQ(ctx->s_cb.ssl_ready(ctx->s_ssl), TEST_SUCCESS);
  5824. }
  5825. return EXPECT_RESULT();
  5826. }
  5827. static int test_ssl_memio_do_handshake(test_ssl_memio_ctx* ctx, int max_rounds,
  5828. int* rounds)
  5829. {
  5830. int handshake_complete = 0;
  5831. int hs_c = 0;
  5832. int hs_s = 0;
  5833. int failing_s = 0;
  5834. int failing_c = 0;
  5835. int ret;
  5836. int err;
  5837. if (rounds != NULL) {
  5838. *rounds = 0;
  5839. }
  5840. while ((!handshake_complete) && (max_rounds > 0)) {
  5841. if (!hs_c) {
  5842. wolfSSL_SetLoggingPrefix("client");
  5843. ret = wolfSSL_connect(ctx->c_ssl);
  5844. wolfSSL_SetLoggingPrefix(NULL);
  5845. if (ret == WOLFSSL_SUCCESS) {
  5846. hs_c = 1;
  5847. }
  5848. else {
  5849. err = wolfSSL_get_error(ctx->c_ssl, ret);
  5850. if (err != WOLFSSL_ERROR_WANT_READ &&
  5851. err != WOLFSSL_ERROR_WANT_WRITE) {
  5852. char buff[WOLFSSL_MAX_ERROR_SZ];
  5853. fprintf(stderr, "error = %d, %s\n", err,
  5854. wolfSSL_ERR_error_string(err, buff));
  5855. failing_c = 1;
  5856. hs_c = 1;
  5857. if (failing_c && failing_s) {
  5858. break;
  5859. }
  5860. }
  5861. }
  5862. }
  5863. if (!hs_s) {
  5864. wolfSSL_SetLoggingPrefix("server");
  5865. ret = wolfSSL_accept(ctx->s_ssl);
  5866. wolfSSL_SetLoggingPrefix(NULL);
  5867. if (ret == WOLFSSL_SUCCESS) {
  5868. hs_s = 1;
  5869. }
  5870. else {
  5871. err = wolfSSL_get_error(ctx->s_ssl, ret);
  5872. if (err != WOLFSSL_ERROR_WANT_READ &&
  5873. err != WOLFSSL_ERROR_WANT_WRITE) {
  5874. char buff[WOLFSSL_MAX_ERROR_SZ];
  5875. fprintf(stderr, "error = %d, %s\n", err,
  5876. wolfSSL_ERR_error_string(err, buff));
  5877. failing_s = 1;
  5878. hs_s = 1;
  5879. if (failing_c && failing_s) {
  5880. break;
  5881. }
  5882. }
  5883. }
  5884. }
  5885. handshake_complete = hs_c && hs_s;
  5886. max_rounds--;
  5887. if (rounds != NULL) {
  5888. *rounds += 1;
  5889. }
  5890. }
  5891. if (!handshake_complete || failing_c || failing_s) {
  5892. return TEST_FAIL;
  5893. }
  5894. return TEST_SUCCESS;
  5895. }
  5896. static int test_ssl_memio_read_write(test_ssl_memio_ctx* ctx)
  5897. {
  5898. EXPECT_DECLS;
  5899. char input[1024];
  5900. int idx = 0;
  5901. const char* msg_c = "hello wolfssl!";
  5902. int msglen_c = (int)XSTRLEN(msg_c);
  5903. const char* msg_s = "I hear you fa shizzle!";
  5904. int msglen_s = (int)XSTRLEN(msg_s);
  5905. if (ctx->c_msg != NULL) {
  5906. msg_c = ctx->c_msg;
  5907. msglen_c = ctx->c_msglen;
  5908. }
  5909. if (ctx->s_msg != NULL) {
  5910. msg_s = ctx->s_msg;
  5911. msglen_s = ctx->s_msglen;
  5912. }
  5913. wolfSSL_SetLoggingPrefix("client");
  5914. ExpectIntEQ(wolfSSL_write(ctx->c_ssl, msg_c, msglen_c), msglen_c);
  5915. wolfSSL_SetLoggingPrefix("server");
  5916. ExpectIntGT(idx = wolfSSL_read(ctx->s_ssl, input, sizeof(input) - 1), 0);
  5917. if (idx >= 0) {
  5918. input[idx] = '\0';
  5919. }
  5920. ExpectIntGT(fprintf(stderr, "Client message: %s\n", input), 0);
  5921. ExpectIntEQ(wolfSSL_write(ctx->s_ssl, msg_s, msglen_s), msglen_s);
  5922. ctx->s_cb.return_code = EXPECT_RESULT();
  5923. wolfSSL_SetLoggingPrefix("client");
  5924. ExpectIntGT(idx = wolfSSL_read(ctx->c_ssl, input, sizeof(input) - 1), 0);
  5925. wolfSSL_SetLoggingPrefix(NULL);
  5926. if (idx >= 0) {
  5927. input[idx] = '\0';
  5928. }
  5929. ExpectIntGT(fprintf(stderr, "Server response: %s\n", input), 0);
  5930. ctx->c_cb.return_code = EXPECT_RESULT();
  5931. if (ctx->c_cb.on_result != NULL) {
  5932. ExpectIntEQ(ctx->c_cb.on_result(ctx->c_ssl), TEST_SUCCESS);
  5933. }
  5934. if (ctx->s_cb.on_result != NULL) {
  5935. ExpectIntEQ(ctx->s_cb.on_result(ctx->s_ssl), TEST_SUCCESS);
  5936. }
  5937. return EXPECT_RESULT();
  5938. }
  5939. static void test_ssl_memio_cleanup(test_ssl_memio_ctx* ctx)
  5940. {
  5941. ctx->c_cb.last_err = wolfSSL_get_error(ctx->c_ssl, 0);
  5942. ctx->s_cb.last_err = wolfSSL_get_error(ctx->s_ssl, 0);
  5943. if (ctx->c_cb.on_cleanup != NULL) {
  5944. ctx->c_cb.on_cleanup(ctx->c_ssl);
  5945. }
  5946. if (ctx->s_cb.on_cleanup != NULL) {
  5947. ctx->s_cb.on_cleanup(ctx->s_ssl);
  5948. }
  5949. wolfSSL_shutdown(ctx->s_ssl);
  5950. wolfSSL_shutdown(ctx->c_ssl);
  5951. wolfSSL_free(ctx->s_ssl);
  5952. wolfSSL_free(ctx->c_ssl);
  5953. if (ctx->c_cb.on_ctx_cleanup != NULL) {
  5954. ctx->c_cb.on_ctx_cleanup(ctx->c_ctx);
  5955. }
  5956. if (!ctx->c_cb.isSharedCtx) {
  5957. wolfSSL_CTX_free(ctx->c_ctx);
  5958. ctx->c_ctx = NULL;
  5959. }
  5960. if (ctx->s_cb.on_ctx_cleanup != NULL) {
  5961. ctx->s_cb.on_ctx_cleanup(ctx->s_ctx);
  5962. }
  5963. if (!ctx->s_cb.isSharedCtx) {
  5964. wolfSSL_CTX_free(ctx->s_ctx);
  5965. ctx->s_ctx = NULL;
  5966. }
  5967. if (!ctx->s_cb.ticNoInit) {
  5968. #if defined(HAVE_SESSION_TICKET) && \
  5969. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5970. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5971. OpenSSLTicketCleanup();
  5972. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5973. TicketCleanup();
  5974. #endif
  5975. #endif
  5976. }
  5977. }
  5978. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  5979. test_ssl_cbf* server_cb, cbType client_on_handshake)
  5980. {
  5981. EXPECT_DECLS;
  5982. struct test_ssl_memio_ctx test_ctx;
  5983. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5984. size_t msg_len;
  5985. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5986. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  5987. XMEMCPY(&test_ctx.c_cb, client_cb, sizeof(test_ssl_cbf));
  5988. XMEMCPY(&test_ctx.s_cb, server_cb, sizeof(test_ssl_cbf));
  5989. test_ctx.c_ctx = client_cb->ctx;
  5990. test_ctx.s_ctx = server_cb->ctx;
  5991. test_ctx.c_cb.return_code = TEST_FAIL;
  5992. test_ctx.s_cb.return_code = TEST_FAIL;
  5993. ExpectIntEQ(test_ssl_memio_setup(&test_ctx), TEST_SUCCESS);
  5994. ExpectIntEQ(test_ssl_memio_do_handshake(&test_ctx, 10, NULL), TEST_SUCCESS);
  5995. if (client_on_handshake != NULL) {
  5996. ExpectIntEQ(client_on_handshake(test_ctx.c_ctx, test_ctx.c_ssl),
  5997. TEST_SUCCESS);
  5998. }
  5999. if (client_cb->on_handshake != NULL) {
  6000. ExpectIntEQ(client_cb->on_handshake(&test_ctx.c_ctx, &test_ctx.c_ssl),
  6001. TEST_SUCCESS);
  6002. }
  6003. if (server_cb->on_handshake != NULL) {
  6004. ExpectIntEQ(server_cb->on_handshake(&test_ctx.s_ctx, &test_ctx.s_ssl),
  6005. TEST_SUCCESS);
  6006. }
  6007. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6008. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  6009. msg_len = wolfSSL_get_peer_finished(test_ctx.s_ssl, server_side_msg2,
  6010. MD_MAX_SIZE);
  6011. ExpectIntGE(msg_len, 0);
  6012. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  6013. msg_len = wolfSSL_get_finished(test_ctx.s_ssl, server_side_msg1,
  6014. MD_MAX_SIZE);
  6015. ExpectIntGE(msg_len, 0);
  6016. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  6017. ExpectIntEQ(test_ssl_memio_read_write(&test_ctx), TEST_SUCCESS);
  6018. test_ssl_memio_cleanup(&test_ctx);
  6019. client_cb->return_code = test_ctx.c_cb.return_code;
  6020. client_cb->last_err = test_ctx.c_cb.last_err;
  6021. server_cb->return_code = test_ctx.s_cb.return_code;
  6022. server_cb->last_err = test_ctx.s_cb.last_err;
  6023. return EXPECT_RESULT();
  6024. }
  6025. #endif
  6026. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  6027. #ifdef WOLFSSL_SESSION_EXPORT
  6028. #ifdef WOLFSSL_DTLS
  6029. /* set up function for sending session information */
  6030. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  6031. {
  6032. WOLFSSL_CTX* ctx = NULL;
  6033. WOLFSSL* ssl = NULL;
  6034. AssertNotNull(inSsl);
  6035. AssertNotNull(buf);
  6036. AssertIntNE(0, sz);
  6037. /* Set ctx to DTLS 1.2 */
  6038. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  6039. AssertNotNull(ctx);
  6040. ssl = wolfSSL_new(ctx);
  6041. AssertNotNull(ssl);
  6042. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  6043. wolfSSL_free(ssl);
  6044. wolfSSL_CTX_free(ctx);
  6045. (void)userCtx;
  6046. return 0;
  6047. }
  6048. #endif
  6049. /* returns negative value on fail and positive (including 0) on success */
  6050. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  6051. {
  6052. int ret, err, loop_count, count, timeout = 10;
  6053. char msg[] = "I hear you fa shizzle!";
  6054. char input[1024];
  6055. loop_count = ((func_args*)args)->argc;
  6056. #ifdef WOLFSSL_ASYNC_CRYPT
  6057. err = 0; /* Reset error */
  6058. #endif
  6059. do {
  6060. #ifdef WOLFSSL_ASYNC_CRYPT
  6061. if (err == WC_PENDING_E) {
  6062. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6063. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6064. }
  6065. #endif
  6066. ret = wolfSSL_accept(ssl);
  6067. err = wolfSSL_get_error(ssl, 0);
  6068. if (err == WOLFSSL_ERROR_WANT_READ ||
  6069. err == WOLFSSL_ERROR_WANT_WRITE) {
  6070. int select_ret;
  6071. err = WC_PENDING_E;
  6072. select_ret = tcp_select(*sockfd, timeout);
  6073. if (select_ret == TEST_TIMEOUT) {
  6074. return WOLFSSL_FATAL_ERROR;
  6075. }
  6076. }
  6077. } while (err == WC_PENDING_E);
  6078. if (ret != WOLFSSL_SUCCESS) {
  6079. char buff[WOLFSSL_MAX_ERROR_SZ];
  6080. fprintf(stderr, "error = %d, %s\n", err,
  6081. wolfSSL_ERR_error_string(err, buff));
  6082. return ret;
  6083. }
  6084. for (count = 0; count < loop_count; count++) {
  6085. int select_ret;
  6086. select_ret = tcp_select(*sockfd, timeout);
  6087. if (select_ret == TEST_TIMEOUT) {
  6088. ret = WOLFSSL_FATAL_ERROR;
  6089. break;
  6090. }
  6091. do {
  6092. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  6093. if (ret > 0) {
  6094. input[ret] = '\0';
  6095. fprintf(stderr, "Client message: %s\n", input);
  6096. }
  6097. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  6098. do {
  6099. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  6100. return WOLFSSL_FATAL_ERROR;
  6101. }
  6102. err = wolfSSL_get_error(ssl, ret);
  6103. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  6104. }
  6105. return ret;
  6106. }
  6107. #endif /* WOLFSSL_SESSION_EXPORT */
  6108. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  6109. {
  6110. SOCKET_T sockfd = 0;
  6111. SOCKET_T clientfd = 0;
  6112. word16 port;
  6113. callback_functions* cbf;
  6114. WOLFSSL_CTX* ctx = NULL;
  6115. WOLFSSL* ssl = NULL;
  6116. func_args* opts = (func_args*)args;
  6117. char msg[] = "I hear you fa shizzle!";
  6118. char input[1024];
  6119. int idx;
  6120. int ret, err = 0;
  6121. int sharedCtx = 0;
  6122. int doUdp = 0;
  6123. SOCKADDR_IN_T cliAddr;
  6124. socklen_t cliLen;
  6125. const char* certFile = svrCertFile;
  6126. const char* keyFile = svrKeyFile;
  6127. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6128. size_t msg_len = 0;
  6129. #endif
  6130. wolfSSL_SetLoggingPrefix("server");
  6131. #ifdef WOLFSSL_TIRTOS
  6132. fdOpenSession(Task_self());
  6133. #endif
  6134. opts->return_code = TEST_FAIL;
  6135. cbf = opts->callbacks;
  6136. if (cbf != NULL && cbf->ctx) {
  6137. ctx = cbf->ctx;
  6138. sharedCtx = 1;
  6139. }
  6140. else
  6141. {
  6142. WOLFSSL_METHOD* method = NULL;
  6143. if (cbf != NULL && cbf->method != NULL) {
  6144. method = cbf->method();
  6145. }
  6146. else {
  6147. method = wolfSSLv23_server_method();
  6148. }
  6149. ctx = wolfSSL_CTX_new(method);
  6150. }
  6151. if (ctx == NULL) {
  6152. /* Release the wait for TCP ready. */
  6153. signal_ready(opts->signal);
  6154. goto done;
  6155. }
  6156. if (cbf == NULL || !cbf->ticNoInit) {
  6157. #if defined(HAVE_SESSION_TICKET) && \
  6158. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6159. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6160. OpenSSLTicketInit();
  6161. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  6162. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6163. TicketInit();
  6164. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  6165. #endif
  6166. #endif
  6167. }
  6168. #if defined(USE_WINDOWS_API)
  6169. port = opts->signal->port;
  6170. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6171. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6172. /* Let tcp_listen assign port */
  6173. port = 0;
  6174. #else
  6175. /* Use default port */
  6176. port = wolfSSLPort;
  6177. #endif
  6178. if (cbf != NULL)
  6179. doUdp = cbf->doUdp;
  6180. /* do it here to detect failure */
  6181. tcp_accept(
  6182. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  6183. if (doUdp) {
  6184. cliLen = sizeof(cliAddr);
  6185. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  6186. (struct sockaddr*)&cliAddr, &cliLen);
  6187. AssertIntGT(idx, 0);
  6188. }
  6189. else {
  6190. CloseSocket(sockfd);
  6191. }
  6192. wolfSSL_CTX_set_verify(ctx,
  6193. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6194. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6195. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6196. #endif
  6197. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  6198. != WOLFSSL_SUCCESS) {
  6199. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6200. goto done;
  6201. }
  6202. if (cbf != NULL && cbf->certPemFile != NULL)
  6203. certFile = cbf->certPemFile;
  6204. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6205. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, certFile,
  6206. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6207. #else
  6208. if (wolfSSL_CTX_use_certificate_file(ctx, certFile,
  6209. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6210. #endif
  6211. /*err_sys("can't load server cert chain file, "
  6212. "Please run from wolfSSL home dir");*/
  6213. goto done;
  6214. }
  6215. if (cbf != NULL && cbf->keyPemFile != NULL)
  6216. keyFile = cbf->keyPemFile;
  6217. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6218. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  6219. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6220. #else
  6221. if (wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  6222. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6223. #endif
  6224. /*err_sys("can't load server key file, "
  6225. "Please run from wolfSSL home dir");*/
  6226. goto done;
  6227. }
  6228. #ifdef HAVE_CRL
  6229. if (cbf != NULL && cbf->crlPemFile != NULL) {
  6230. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  6231. goto done;
  6232. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  6233. != WOLFSSL_SUCCESS)
  6234. goto done;
  6235. }
  6236. #endif
  6237. /* call ctx setup callback */
  6238. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6239. cbf->ctx_ready(ctx);
  6240. }
  6241. ssl = wolfSSL_new(ctx);
  6242. if (ssl == NULL) {
  6243. goto done;
  6244. }
  6245. if (doUdp) {
  6246. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  6247. if (err != WOLFSSL_SUCCESS)
  6248. goto done;
  6249. }
  6250. #ifdef WOLFSSL_SESSION_EXPORT
  6251. /* only add in more complex nonblocking case with session export tests */
  6252. if (args && opts->argc > 0) {
  6253. /* set as nonblock and time out for waiting on read/write */
  6254. tcp_set_nonblocking(&clientfd);
  6255. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  6256. }
  6257. #endif
  6258. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6259. if (sharedCtx && wolfSSL_use_certificate_file(ssl, certFile,
  6260. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6261. #else
  6262. if (wolfSSL_use_certificate_file(ssl, certFile,
  6263. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6264. #endif
  6265. /*err_sys("can't load server cert chain file, "
  6266. "Please run from wolfSSL home dir");*/
  6267. goto done;
  6268. }
  6269. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6270. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, keyFile,
  6271. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6272. #else
  6273. if (wolfSSL_use_PrivateKey_file(ssl, keyFile,
  6274. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6275. #endif
  6276. /*err_sys("can't load server key file, "
  6277. "Please run from wolfSSL home dir");*/
  6278. goto done;
  6279. }
  6280. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  6281. /*err_sys("SSL_set_fd failed");*/
  6282. goto done;
  6283. }
  6284. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6285. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6286. #elif !defined(NO_DH)
  6287. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6288. #endif
  6289. /* call ssl setup callback */
  6290. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6291. cbf->ssl_ready(ssl);
  6292. }
  6293. #ifdef WOLFSSL_SESSION_EXPORT
  6294. /* only add in more complex nonblocking case with session export tests */
  6295. if (opts->argc > 0) {
  6296. ret = nonblocking_accept_read(args, ssl, &clientfd);
  6297. if (ret >= 0) {
  6298. opts->return_code = TEST_SUCCESS;
  6299. }
  6300. #ifdef WOLFSSL_TIRTOS
  6301. Task_yield();
  6302. #endif
  6303. goto done;
  6304. }
  6305. #endif
  6306. #ifdef WOLFSSL_ASYNC_CRYPT
  6307. err = 0; /* Reset error */
  6308. #endif
  6309. do {
  6310. #ifdef WOLFSSL_ASYNC_CRYPT
  6311. if (err == WC_PENDING_E) {
  6312. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6313. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6314. }
  6315. #endif
  6316. ret = wolfSSL_negotiate(ssl);
  6317. err = wolfSSL_get_error(ssl, 0);
  6318. } while (err == WC_PENDING_E);
  6319. if (ret != WOLFSSL_SUCCESS) {
  6320. char buff[WOLFSSL_MAX_ERROR_SZ];
  6321. fprintf(stderr, "error = %d, %s\n", err,
  6322. wolfSSL_ERR_error_string(err, buff));
  6323. /*err_sys("SSL_accept failed");*/
  6324. goto done;
  6325. }
  6326. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6327. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  6328. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  6329. AssertIntGE(msg_len, 0);
  6330. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  6331. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  6332. AssertIntGE(msg_len, 0);
  6333. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  6334. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6335. if (idx > 0) {
  6336. input[idx] = '\0';
  6337. fprintf(stderr, "Client message: %s\n", input);
  6338. }
  6339. else if (idx < 0) {
  6340. goto done;
  6341. }
  6342. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6343. /*err_sys("SSL_write failed");*/
  6344. goto done;
  6345. }
  6346. if (cbf != NULL && cbf->on_result != NULL)
  6347. cbf->on_result(ssl);
  6348. #ifdef WOLFSSL_TIRTOS
  6349. Task_yield();
  6350. #endif
  6351. opts->return_code = TEST_SUCCESS;
  6352. done:
  6353. if (cbf != NULL)
  6354. cbf->last_err = err;
  6355. if (cbf != NULL && cbf->on_cleanup != NULL)
  6356. cbf->on_cleanup(ssl);
  6357. wolfSSL_shutdown(ssl);
  6358. wolfSSL_free(ssl);
  6359. if (!sharedCtx)
  6360. wolfSSL_CTX_free(ctx);
  6361. CloseSocket(clientfd);
  6362. #ifdef WOLFSSL_TIRTOS
  6363. fdCloseSession(Task_self());
  6364. #endif
  6365. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6366. && defined(HAVE_THREAD_LS)
  6367. wc_ecc_fp_free(); /* free per thread cache */
  6368. #endif
  6369. if (cbf == NULL || !cbf->ticNoInit) {
  6370. #if defined(HAVE_SESSION_TICKET) && \
  6371. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6372. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6373. OpenSSLTicketCleanup();
  6374. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6375. TicketCleanup();
  6376. #endif
  6377. #endif
  6378. }
  6379. wolfSSL_SetLoggingPrefix(NULL);
  6380. WOLFSSL_RETURN_FROM_THREAD(0);
  6381. }
  6382. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6383. !defined(WOLFSSL_NO_TLS12)
  6384. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  6385. {
  6386. SOCKET_T sockfd = 0;
  6387. SOCKET_T clientfd = 0;
  6388. word16 port;
  6389. callback_functions* cbf;
  6390. WOLFSSL_CTX* ctx = 0;
  6391. WOLFSSL* ssl = 0;
  6392. char msg[] = "I hear you fa shizzle!";
  6393. char input[1024];
  6394. int idx;
  6395. int ret, err = 0;
  6396. int sharedCtx = 0;
  6397. func_args* opts = (func_args*)args;
  6398. int loop_count = opts->argc;
  6399. int count = 0;
  6400. #ifdef WOLFSSL_TIRTOS
  6401. fdOpenSession(Task_self());
  6402. #endif
  6403. opts->return_code = TEST_FAIL;
  6404. cbf = opts->callbacks;
  6405. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6406. if (cbf != NULL && cbf->ctx) {
  6407. ctx = cbf->ctx;
  6408. sharedCtx = 1;
  6409. }
  6410. else
  6411. #endif
  6412. {
  6413. WOLFSSL_METHOD* method = NULL;
  6414. if (cbf != NULL && cbf->method != NULL) {
  6415. method = cbf->method();
  6416. }
  6417. else {
  6418. method = wolfSSLv23_server_method();
  6419. }
  6420. ctx = wolfSSL_CTX_new(method);
  6421. }
  6422. #if defined(USE_WINDOWS_API)
  6423. port = opts->signal->port;
  6424. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6425. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6426. /* Let tcp_listen assign port */
  6427. port = 0;
  6428. #else
  6429. /* Use default port */
  6430. port = wolfSSLPort;
  6431. #endif
  6432. wolfSSL_CTX_set_verify(ctx,
  6433. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6434. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6435. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6436. #endif
  6437. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  6438. != WOLFSSL_SUCCESS) {
  6439. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6440. /* Release the wait for TCP ready. */
  6441. signal_ready(opts->signal);
  6442. goto done;
  6443. }
  6444. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  6445. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6446. /*err_sys("can't load server cert chain file, "
  6447. "Please run from wolfSSL home dir");*/
  6448. /* Release the wait for TCP ready. */
  6449. signal_ready(opts->signal);
  6450. goto done;
  6451. }
  6452. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  6453. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6454. /*err_sys("can't load server key file, "
  6455. "Please run from wolfSSL home dir");*/
  6456. /* Release the wait for TCP ready. */
  6457. signal_ready(opts->signal);
  6458. goto done;
  6459. }
  6460. /* call ctx setup callback */
  6461. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6462. cbf->ctx_ready(ctx);
  6463. }
  6464. while (count != loop_count) {
  6465. ssl = wolfSSL_new(ctx);
  6466. if (ssl == NULL) {
  6467. signal_ready(opts->signal);
  6468. goto done;
  6469. }
  6470. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  6471. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6472. /*err_sys("can't load server cert chain file, "
  6473. "Please run from wolfSSL home dir");*/
  6474. /* Release the wait for TCP ready. */
  6475. signal_ready(opts->signal);
  6476. goto done;
  6477. }
  6478. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  6479. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6480. /*err_sys("can't load server key file, "
  6481. "Please run from wolfSSL home dir");*/
  6482. /* Release the wait for TCP ready. */
  6483. signal_ready(opts->signal);
  6484. goto done;
  6485. }
  6486. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6487. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6488. #elif !defined(NO_DH)
  6489. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6490. #endif
  6491. /* call ssl setup callback */
  6492. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6493. cbf->ssl_ready(ssl);
  6494. }
  6495. /* do it here to detect failure */
  6496. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0,
  6497. 0);
  6498. CloseSocket(sockfd);
  6499. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  6500. /*err_sys("SSL_set_fd failed");*/
  6501. goto done;
  6502. }
  6503. #ifdef WOLFSSL_ASYNC_CRYPT
  6504. err = 0; /* Reset error */
  6505. #endif
  6506. do {
  6507. #ifdef WOLFSSL_ASYNC_CRYPT
  6508. if (err == WC_PENDING_E) {
  6509. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6510. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6511. }
  6512. #endif
  6513. ret = wolfSSL_accept(ssl);
  6514. err = wolfSSL_get_error(ssl, 0);
  6515. } while (err == WC_PENDING_E);
  6516. if (ret != WOLFSSL_SUCCESS) {
  6517. char buff[WOLFSSL_MAX_ERROR_SZ];
  6518. fprintf(stderr, "error = %d, %s\n", err,
  6519. wolfSSL_ERR_error_string(err, buff));
  6520. /*err_sys("SSL_accept failed");*/
  6521. goto done;
  6522. }
  6523. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6524. if (idx > 0) {
  6525. input[idx] = '\0';
  6526. fprintf(stderr, "Client message: %s\n", input);
  6527. }
  6528. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6529. /*err_sys("SSL_write failed");*/
  6530. goto done;
  6531. }
  6532. /* free ssl for this connection */
  6533. wolfSSL_shutdown(ssl);
  6534. wolfSSL_free(ssl); ssl = NULL;
  6535. CloseSocket(clientfd);
  6536. count++;
  6537. }
  6538. #ifdef WOLFSSL_TIRTOS
  6539. Task_yield();
  6540. #endif
  6541. opts->return_code = TEST_SUCCESS;
  6542. done:
  6543. if (ssl != NULL) {
  6544. wolfSSL_shutdown(ssl);
  6545. wolfSSL_free(ssl);
  6546. }
  6547. if (!sharedCtx)
  6548. wolfSSL_CTX_free(ctx);
  6549. CloseSocket(clientfd);
  6550. #ifdef WOLFSSL_TIRTOS
  6551. fdCloseSession(Task_self());
  6552. #endif
  6553. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6554. && defined(HAVE_THREAD_LS)
  6555. wc_ecc_fp_free(); /* free per thread cache */
  6556. #endif
  6557. WOLFSSL_RETURN_FROM_THREAD(0);
  6558. }
  6559. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  6560. static int test_client_nofail(void* args, cbType cb)
  6561. {
  6562. #if !defined(NO_WOLFSSL_CLIENT)
  6563. SOCKET_T sockfd = 0;
  6564. callback_functions* cbf;
  6565. WOLFSSL_CTX* ctx = 0;
  6566. WOLFSSL* ssl = 0;
  6567. WOLFSSL_CIPHER* cipher;
  6568. char msg[64] = "hello wolfssl!";
  6569. char reply[1024];
  6570. int input;
  6571. int msgSz = (int)XSTRLEN(msg);
  6572. int ret, err = 0;
  6573. int cipherSuite;
  6574. int sharedCtx = 0;
  6575. int doUdp = 0;
  6576. const char* cipherName1, *cipherName2;
  6577. wolfSSL_SetLoggingPrefix("client");
  6578. #ifdef WOLFSSL_TIRTOS
  6579. fdOpenSession(Task_self());
  6580. #endif
  6581. ((func_args*)args)->return_code = TEST_FAIL;
  6582. cbf = ((func_args*)args)->callbacks;
  6583. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6584. if (cbf != NULL && cbf->ctx) {
  6585. ctx = cbf->ctx;
  6586. sharedCtx = cbf->isSharedCtx;
  6587. }
  6588. else
  6589. #endif
  6590. {
  6591. WOLFSSL_METHOD* method = NULL;
  6592. if (cbf != NULL && cbf->method != NULL) {
  6593. method = cbf->method();
  6594. }
  6595. else {
  6596. method = wolfSSLv23_client_method();
  6597. }
  6598. ctx = wolfSSL_CTX_new(method);
  6599. }
  6600. if (cbf != NULL)
  6601. doUdp = cbf->doUdp;
  6602. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6603. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6604. #endif
  6605. /* Do connect here so server detects failures */
  6606. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6607. doUdp, 0, NULL);
  6608. /* Connect the socket so that we don't have to set the peer later on */
  6609. if (doUdp)
  6610. udp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port);
  6611. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  6612. {
  6613. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6614. goto done;
  6615. }
  6616. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6617. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6618. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6619. #else
  6620. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6621. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6622. #endif
  6623. /*err_sys("can't load client cert file, "
  6624. "Please run from wolfSSL home dir");*/
  6625. goto done;
  6626. }
  6627. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6628. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6629. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6630. #else
  6631. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6632. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6633. #endif
  6634. /*err_sys("can't load client key file, "
  6635. "Please run from wolfSSL home dir");*/
  6636. goto done;
  6637. }
  6638. #ifdef HAVE_CRL
  6639. if (cbf != NULL && cbf->crlPemFile != NULL) {
  6640. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  6641. goto done;
  6642. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  6643. != WOLFSSL_SUCCESS)
  6644. goto done;
  6645. }
  6646. #endif
  6647. /* call ctx setup callback */
  6648. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6649. cbf->ctx_ready(ctx);
  6650. }
  6651. ssl = wolfSSL_new(ctx);
  6652. if (ssl == NULL) {
  6653. goto done;
  6654. }
  6655. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6656. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  6657. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6658. #else
  6659. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  6660. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6661. #endif
  6662. /*err_sys("can't load client cert file, "
  6663. "Please run from wolfSSL home dir");*/
  6664. goto done;
  6665. }
  6666. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6667. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6668. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6669. #else
  6670. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6671. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6672. #endif
  6673. /*err_sys("can't load client key file, "
  6674. "Please run from wolfSSL home dir");*/
  6675. goto done;
  6676. }
  6677. if (!doUdp) {
  6678. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6679. /*err_sys("SSL_set_fd failed");*/
  6680. goto done;
  6681. }
  6682. }
  6683. else {
  6684. #ifdef WOLFSSL_DTLS
  6685. if (wolfSSL_set_dtls_fd_connected(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6686. /*err_sys("SSL_set_fd failed");*/
  6687. goto done;
  6688. }
  6689. #else
  6690. goto done;
  6691. #endif
  6692. }
  6693. /* call ssl setup callback */
  6694. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6695. cbf->ssl_ready(ssl);
  6696. }
  6697. #ifdef WOLFSSL_ASYNC_CRYPT
  6698. err = 0; /* Reset error */
  6699. #endif
  6700. do {
  6701. #ifdef WOLFSSL_ASYNC_CRYPT
  6702. if (err == WC_PENDING_E) {
  6703. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6704. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6705. }
  6706. #endif
  6707. ret = wolfSSL_negotiate(ssl);
  6708. err = wolfSSL_get_error(ssl, 0);
  6709. } while (err == WC_PENDING_E);
  6710. if (ret != WOLFSSL_SUCCESS) {
  6711. char buff[WOLFSSL_MAX_ERROR_SZ];
  6712. fprintf(stderr, "error = %d, %s\n", err,
  6713. wolfSSL_ERR_error_string(err, buff));
  6714. /*err_sys("SSL_connect failed");*/
  6715. goto done;
  6716. }
  6717. /* test the various get cipher methods */
  6718. /* Internal cipher suite names */
  6719. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  6720. cipherName1 = wolfSSL_get_cipher_name(ssl);
  6721. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  6722. (cipherSuite >> 8), cipherSuite & 0xFF);
  6723. AssertStrEQ(cipherName1, cipherName2);
  6724. /* IANA Cipher Suites Names */
  6725. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  6726. then it's the internal cipher suite name */
  6727. cipher = wolfSSL_get_current_cipher(ssl);
  6728. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  6729. cipherName2 = wolfSSL_get_cipher(ssl);
  6730. AssertStrEQ(cipherName1, cipherName2);
  6731. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  6732. !defined(WOLFSSL_QT)
  6733. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  6734. (cipherSuite >> 8), cipherSuite & 0xFF);
  6735. AssertStrEQ(cipherName1, cipherName2);
  6736. #endif
  6737. if (cb != NULL)
  6738. (cb)(ctx, ssl);
  6739. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6740. /*err_sys("SSL_write failed");*/
  6741. goto done;
  6742. }
  6743. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6744. if (input > 0) {
  6745. reply[input] = '\0';
  6746. fprintf(stderr, "Server response: %s\n", reply);
  6747. }
  6748. if (cbf != NULL && cbf->on_result != NULL)
  6749. cbf->on_result(ssl);
  6750. ((func_args*)args)->return_code = TEST_SUCCESS;
  6751. done:
  6752. if (cbf != NULL)
  6753. cbf->last_err = err;
  6754. if (cbf != NULL && cbf->on_cleanup != NULL)
  6755. cbf->on_cleanup(ssl);
  6756. wolfSSL_free(ssl);
  6757. if (!sharedCtx)
  6758. wolfSSL_CTX_free(ctx);
  6759. CloseSocket(sockfd);
  6760. #ifdef WOLFSSL_TIRTOS
  6761. fdCloseSession(Task_self());
  6762. #endif
  6763. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6764. && defined(HAVE_THREAD_LS)
  6765. wc_ecc_fp_free(); /* free per thread cache */
  6766. #endif
  6767. #else
  6768. (void)args;
  6769. (void)cb;
  6770. #endif /* !NO_WOLFSSL_CLIENT */
  6771. wolfSSL_SetLoggingPrefix(NULL);
  6772. return 0;
  6773. }
  6774. void test_wolfSSL_client_server_nofail_ex(callback_functions* client_cb,
  6775. callback_functions* server_cb, cbType client_on_handshake)
  6776. {
  6777. func_args client_args;
  6778. func_args server_args;
  6779. tcp_ready ready;
  6780. THREAD_TYPE serverThread;
  6781. XMEMSET(&client_args, 0, sizeof(func_args));
  6782. XMEMSET(&server_args, 0, sizeof(func_args));
  6783. #ifdef WOLFSSL_TIRTOS
  6784. fdOpenSession(Task_self());
  6785. #endif
  6786. StartTCP();
  6787. InitTcpReady(&ready);
  6788. #if defined(USE_WINDOWS_API)
  6789. /* use RNG to get random port if using windows */
  6790. ready.port = GetRandomPort();
  6791. #endif
  6792. server_args.signal = &ready;
  6793. server_args.callbacks = server_cb;
  6794. client_args.signal = &ready;
  6795. client_args.callbacks = client_cb;
  6796. start_thread(test_server_nofail, &server_args, &serverThread);
  6797. wait_tcp_ready(&server_args);
  6798. test_client_nofail(&client_args, client_on_handshake);
  6799. join_thread(serverThread);
  6800. client_cb->return_code = client_args.return_code;
  6801. server_cb->return_code = server_args.return_code;
  6802. FreeTcpReady(&ready);
  6803. #ifdef WOLFSSL_TIRTOS
  6804. fdOpenSession(Task_self());
  6805. #endif
  6806. }
  6807. void test_wolfSSL_client_server_nofail(callback_functions* client_cb,
  6808. callback_functions* server_cb)
  6809. {
  6810. test_wolfSSL_client_server_nofail_ex(client_cb, server_cb, NULL);
  6811. }
  6812. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6813. !defined(WOLFSSL_NO_TLS12) && !defined(NO_WOLFSSL_CLIENT)
  6814. static void test_client_reuse_WOLFSSLobj(void* args, cbType cb,
  6815. void* server_args)
  6816. {
  6817. SOCKET_T sockfd = 0;
  6818. callback_functions* cbf;
  6819. WOLFSSL_CTX* ctx = 0;
  6820. WOLFSSL* ssl = 0;
  6821. WOLFSSL_SESSION* session = NULL;
  6822. char msg[64] = "hello wolfssl!";
  6823. char reply[1024];
  6824. int input;
  6825. int msgSz = (int)XSTRLEN(msg);
  6826. int ret, err = 0;
  6827. int sharedCtx = 0;
  6828. #ifdef WOLFSSL_TIRTOS
  6829. fdOpenSession(Task_self());
  6830. #endif
  6831. ((func_args*)args)->return_code = TEST_FAIL;
  6832. cbf = ((func_args*)args)->callbacks;
  6833. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6834. if (cbf != NULL && cbf->ctx) {
  6835. ctx = cbf->ctx;
  6836. sharedCtx = 1;
  6837. }
  6838. else
  6839. #endif
  6840. {
  6841. WOLFSSL_METHOD* method = NULL;
  6842. if (cbf != NULL && cbf->method != NULL) {
  6843. method = cbf->method();
  6844. }
  6845. else {
  6846. method = wolfSSLv23_client_method();
  6847. }
  6848. ctx = wolfSSL_CTX_new(method);
  6849. }
  6850. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6851. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6852. #endif
  6853. /* Do connect here so server detects failures */
  6854. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6855. 0, 0, NULL);
  6856. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) !=
  6857. WOLFSSL_SUCCESS) {
  6858. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6859. goto done;
  6860. }
  6861. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6862. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6863. /*err_sys("can't load client cert file, "
  6864. "Please run from wolfSSL home dir");*/
  6865. goto done;
  6866. }
  6867. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6868. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6869. /*err_sys("can't load client key file, "
  6870. "Please run from wolfSSL home dir");*/
  6871. goto done;
  6872. }
  6873. /* call ctx setup callback */
  6874. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6875. cbf->ctx_ready(ctx);
  6876. }
  6877. ssl = wolfSSL_new(ctx);
  6878. if (ssl == NULL) {
  6879. goto done;
  6880. }
  6881. /* keep handshake resources for re-using WOLFSSL obj */
  6882. wolfSSL_KeepArrays(ssl);
  6883. if (wolfSSL_KeepHandshakeResources(ssl)) {
  6884. /* err_sys("SSL_KeepHandshakeResources failed"); */
  6885. goto done;
  6886. }
  6887. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  6888. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6889. /*err_sys("can't load client cert file, "
  6890. "Please run from wolfSSL home dir");*/
  6891. goto done;
  6892. }
  6893. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6894. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6895. /*err_sys("can't load client key file, "
  6896. "Please run from wolfSSL home dir");*/
  6897. goto done;
  6898. }
  6899. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6900. /*err_sys("SSL_set_fd failed");*/
  6901. goto done;
  6902. }
  6903. /* call ssl setup callback */
  6904. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6905. cbf->ssl_ready(ssl);
  6906. }
  6907. #ifdef WOLFSSL_ASYNC_CRYPT
  6908. err = 0; /* Reset error */
  6909. #endif
  6910. do {
  6911. #ifdef WOLFSSL_ASYNC_CRYPT
  6912. if (err == WC_PENDING_E) {
  6913. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6914. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6915. }
  6916. #endif
  6917. ret = wolfSSL_connect(ssl);
  6918. err = wolfSSL_get_error(ssl, 0);
  6919. } while (err == WC_PENDING_E);
  6920. if (ret != WOLFSSL_SUCCESS) {
  6921. char buff[WOLFSSL_MAX_ERROR_SZ];
  6922. fprintf(stderr, "error = %d, %s\n", err,
  6923. wolfSSL_ERR_error_string(err, buff));
  6924. /*err_sys("SSL_connect failed");*/
  6925. goto done;
  6926. }
  6927. /* Build first session */
  6928. if (cb != NULL)
  6929. cb(ctx, ssl);
  6930. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6931. /*err_sys("SSL_write failed");*/
  6932. goto done;
  6933. }
  6934. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6935. if (input > 0) {
  6936. reply[input] = '\0';
  6937. fprintf(stderr, "Server response: %s\n", reply);
  6938. }
  6939. /* Session Resumption by re-using WOLFSSL object */
  6940. wolfSSL_set_quiet_shutdown(ssl, 1);
  6941. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  6942. /* err_sys ("SSL shutdown failed"); */
  6943. goto done;
  6944. }
  6945. session = wolfSSL_get1_session(ssl);
  6946. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  6947. wolfSSL_SESSION_free(session);
  6948. /* err_sys ("SSL_clear failed"); */
  6949. goto done;
  6950. }
  6951. wolfSSL_set_session(ssl, session);
  6952. wolfSSL_SESSION_free(session);
  6953. session = NULL;
  6954. /* close socket once */
  6955. CloseSocket(sockfd);
  6956. sockfd = 0;
  6957. /* wait until server ready */
  6958. wait_tcp_ready((func_args*)server_args);
  6959. fprintf(stderr, "session resumption\n");
  6960. /* Do re-connect */
  6961. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6962. 0, 0, NULL);
  6963. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6964. /*err_sys("SSL_set_fd failed");*/
  6965. goto done;
  6966. }
  6967. #ifdef WOLFSSL_ASYNC_CRYPT
  6968. err = 0; /* Reset error */
  6969. #endif
  6970. do {
  6971. #ifdef WOLFSSL_ASYNC_CRYPT
  6972. if (err == WC_PENDING_E) {
  6973. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6974. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6975. }
  6976. #endif
  6977. ret = wolfSSL_connect(ssl);
  6978. err = wolfSSL_get_error(ssl, 0);
  6979. } while (err == WC_PENDING_E);
  6980. if (ret != WOLFSSL_SUCCESS) {
  6981. char buff[WOLFSSL_MAX_ERROR_SZ];
  6982. fprintf(stderr, "error = %d, %s\n", err,
  6983. wolfSSL_ERR_error_string(err, buff));
  6984. /*err_sys("SSL_connect failed");*/
  6985. goto done;
  6986. }
  6987. /* Build first session */
  6988. if (cb != NULL)
  6989. cb(ctx, ssl);
  6990. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6991. /*err_sys("SSL_write failed");*/
  6992. goto done;
  6993. }
  6994. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6995. if (input > 0) {
  6996. reply[input] = '\0';
  6997. fprintf(stderr, "Server response: %s\n", reply);
  6998. }
  6999. ((func_args*)args)->return_code = TEST_SUCCESS;
  7000. done:
  7001. wolfSSL_free(ssl);
  7002. if (!sharedCtx)
  7003. wolfSSL_CTX_free(ctx);
  7004. CloseSocket(sockfd);
  7005. #ifdef WOLFSSL_TIRTOS
  7006. fdCloseSession(Task_self());
  7007. #endif
  7008. return;
  7009. }
  7010. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  7011. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
  7012. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  7013. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  7014. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  7015. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  7016. #define HAVE_ALPN_PROTOS_SUPPORT
  7017. #endif
  7018. /* Generic TLS client / server with callbacks for API unit tests
  7019. * Used by SNI / ALPN / crypto callback helper functions */
  7020. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  7021. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  7022. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  7023. #define ENABLE_TLS_CALLBACK_TEST
  7024. #endif
  7025. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  7026. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  7027. /* TLS server for API unit testing - generic */
  7028. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  7029. {
  7030. callback_functions* callbacks = ((func_args*)args)->callbacks;
  7031. WOLFSSL_CTX* ctx = NULL;
  7032. WOLFSSL* ssl = NULL;
  7033. SOCKET_T sfd = 0;
  7034. SOCKET_T cfd = 0;
  7035. word16 port;
  7036. char msg[] = "I hear you fa shizzle!";
  7037. int len = (int) XSTRLEN(msg);
  7038. char input[1024];
  7039. int idx;
  7040. int ret, err = 0;
  7041. ((func_args*)args)->return_code = TEST_FAIL;
  7042. #if defined(USE_WINDOWS_API)
  7043. port = ((func_args*)args)->signal->port;
  7044. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  7045. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  7046. /* Let tcp_listen assign port */
  7047. port = 0;
  7048. #else
  7049. /* Use default port */
  7050. port = wolfSSLPort;
  7051. #endif
  7052. #ifdef WOLFSSL_DTLS
  7053. if (callbacks->method == wolfDTLS_server_method
  7054. #ifdef WOLFSSL_STATIC_MEMORY
  7055. || callbacks->method_ex == wolfDTLS_server_method_ex
  7056. #endif
  7057. #ifndef NO_OLD_TLS
  7058. || callbacks->method == wolfDTLSv1_server_method
  7059. #ifdef WOLFSSL_STATIC_MEMORY
  7060. || callbacks->method_ex == wolfDTLSv1_server_method_ex
  7061. #endif
  7062. #endif
  7063. #ifndef WOLFSSL_NO_TLS12
  7064. || callbacks->method == wolfDTLSv1_2_server_method
  7065. #ifdef WOLFSSL_STATIC_MEMORY
  7066. || callbacks->method_ex == wolfDTLSv1_2_server_method_ex
  7067. #endif
  7068. #endif
  7069. #ifdef WOLFSSL_DTLS13
  7070. || callbacks->method == wolfDTLSv1_3_server_method
  7071. #ifdef WOLFSSL_STATIC_MEMORY
  7072. || callbacks->method_ex == wolfDTLSv1_3_server_method_ex
  7073. #endif
  7074. #endif
  7075. ) {
  7076. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  7077. }
  7078. else
  7079. #endif
  7080. {
  7081. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  7082. }
  7083. #ifdef WOLFSSL_STATIC_MEMORY
  7084. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  7085. callbacks->memSz > 0) {
  7086. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  7087. callbacks->mem, callbacks->memSz, 0, 1);
  7088. if (ret != WOLFSSL_SUCCESS) {
  7089. fprintf(stderr, "CTX static new failed %d\n", ret);
  7090. goto cleanup;
  7091. }
  7092. }
  7093. #else
  7094. ctx = wolfSSL_CTX_new(callbacks->method());
  7095. #endif
  7096. if (ctx == NULL) {
  7097. fprintf(stderr, "CTX new failed\n");
  7098. goto cleanup;
  7099. }
  7100. /* set defaults */
  7101. if (callbacks->caPemFile == NULL)
  7102. callbacks->caPemFile = cliCertFile;
  7103. if (callbacks->certPemFile == NULL)
  7104. callbacks->certPemFile = svrCertFile;
  7105. if (callbacks->keyPemFile == NULL)
  7106. callbacks->keyPemFile = svrKeyFile;
  7107. #ifdef WOLFSSL_TIRTOS
  7108. fdOpenSession(Task_self());
  7109. #endif
  7110. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  7111. wolfSSL_CTX_set_verify(ctx,
  7112. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  7113. #ifdef WOLFSSL_ENCRYPTED_KEYS
  7114. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  7115. #endif
  7116. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  7117. if (callbacks->method == wolfDTLSv1_2_server_method) {
  7118. if (wolfSSL_CTX_dtls_set_export(ctx, test_export) != WOLFSSL_SUCCESS)
  7119. goto cleanup;
  7120. }
  7121. #endif
  7122. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  7123. WOLFSSL_SUCCESS) {
  7124. goto cleanup;
  7125. }
  7126. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  7127. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7128. goto cleanup;
  7129. }
  7130. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  7131. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7132. goto cleanup;
  7133. }
  7134. #ifdef HAVE_CRL
  7135. if (callbacks->crlPemFile != NULL) {
  7136. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  7137. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7138. goto cleanup;
  7139. }
  7140. }
  7141. #endif
  7142. if (callbacks->ctx_ready)
  7143. callbacks->ctx_ready(ctx);
  7144. ssl = wolfSSL_new(ctx);
  7145. if (ssl == NULL) {
  7146. fprintf(stderr, "SSL new failed\n");
  7147. goto cleanup;
  7148. }
  7149. if (wolfSSL_dtls(ssl)) {
  7150. SOCKADDR_IN_T cliAddr;
  7151. socklen_t cliLen;
  7152. cliLen = sizeof(cliAddr);
  7153. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  7154. (struct sockaddr*)&cliAddr, &cliLen);
  7155. if (idx <= 0) {
  7156. goto cleanup;
  7157. }
  7158. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  7159. }
  7160. else {
  7161. CloseSocket(sfd);
  7162. }
  7163. if (wolfSSL_set_fd(ssl, cfd) != WOLFSSL_SUCCESS) {
  7164. goto cleanup;
  7165. }
  7166. if (callbacks->loadToSSL) {
  7167. wolfSSL_SetDevId(ssl, callbacks->devId);
  7168. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  7169. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7170. goto cleanup;
  7171. }
  7172. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  7173. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7174. goto cleanup;
  7175. }
  7176. }
  7177. #ifdef NO_PSK
  7178. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  7179. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  7180. #elif !defined(NO_DH)
  7181. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  7182. #endif
  7183. #endif
  7184. if (callbacks->ssl_ready)
  7185. callbacks->ssl_ready(ssl);
  7186. #ifdef WOLFSSL_ASYNC_CRYPT
  7187. err = 0; /* Reset error */
  7188. #endif
  7189. do {
  7190. #ifdef WOLFSSL_ASYNC_CRYPT
  7191. if (err == WC_PENDING_E) {
  7192. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7193. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7194. }
  7195. #endif
  7196. ret = wolfSSL_accept(ssl);
  7197. err = wolfSSL_get_error(ssl, ret);
  7198. } while (err == WC_PENDING_E);
  7199. if (ret != WOLFSSL_SUCCESS) {
  7200. char buff[WOLFSSL_MAX_ERROR_SZ];
  7201. fprintf(stderr, "accept error = %d, %s\n", err,
  7202. wolfSSL_ERR_error_string(err, buff));
  7203. /*err_sys("SSL_accept failed");*/
  7204. }
  7205. else {
  7206. #ifdef WOLFSSL_ASYNC_CRYPT
  7207. err = 0; /* Reset error */
  7208. #endif
  7209. do {
  7210. #ifdef WOLFSSL_ASYNC_CRYPT
  7211. if (err == WC_PENDING_E) {
  7212. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7213. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7214. }
  7215. #endif
  7216. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  7217. err = wolfSSL_get_error(ssl, idx);
  7218. } while (err == WC_PENDING_E);
  7219. if (idx > 0) {
  7220. input[idx] = 0;
  7221. fprintf(stderr, "Client message: %s\n", input);
  7222. }
  7223. #ifdef WOLFSSL_ASYNC_CRYPT
  7224. err = 0; /* Reset error */
  7225. #endif
  7226. do {
  7227. #ifdef WOLFSSL_ASYNC_CRYPT
  7228. if (err == WC_PENDING_E) {
  7229. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7230. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7231. }
  7232. #endif
  7233. ret = wolfSSL_write(ssl, msg, len);
  7234. err = wolfSSL_get_error(ssl, ret);
  7235. } while (err == WC_PENDING_E);
  7236. if (len != ret) {
  7237. goto cleanup;
  7238. }
  7239. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  7240. defined(WOLFSSL_DTLS)
  7241. if (wolfSSL_dtls(ssl)) {
  7242. byte* import;
  7243. word32 sz;
  7244. wolfSSL_dtls_export(ssl, NULL, &sz);
  7245. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7246. if (import == NULL) {
  7247. goto cleanup;
  7248. }
  7249. idx = wolfSSL_dtls_export(ssl, import, &sz);
  7250. if (idx < 0) {
  7251. goto cleanup;
  7252. }
  7253. if (wolfSSL_dtls_import(ssl, import, idx) < 0) {
  7254. goto cleanup;
  7255. }
  7256. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7257. }
  7258. #endif
  7259. #ifdef WOLFSSL_TIRTOS
  7260. Task_yield();
  7261. #endif
  7262. ((func_args*)args)->return_code = TEST_SUCCESS;
  7263. }
  7264. if (callbacks->on_result)
  7265. callbacks->on_result(ssl);
  7266. wolfSSL_shutdown(ssl);
  7267. cleanup:
  7268. wolfSSL_free(ssl);
  7269. wolfSSL_CTX_free(ctx);
  7270. CloseSocket(cfd);
  7271. #ifdef WOLFSSL_TIRTOS
  7272. fdCloseSession(Task_self());
  7273. #endif
  7274. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  7275. && defined(HAVE_THREAD_LS)
  7276. wc_ecc_fp_free(); /* free per thread cache */
  7277. #endif
  7278. WOLFSSL_RETURN_FROM_THREAD(0);
  7279. }
  7280. /* TLS Client for API unit testing - generic */
  7281. static void run_wolfssl_client(void* args)
  7282. {
  7283. callback_functions* callbacks = ((func_args*)args)->callbacks;
  7284. WOLFSSL_CTX* ctx = NULL;
  7285. WOLFSSL* ssl = NULL;
  7286. SOCKET_T sfd = 0;
  7287. char msg[] = "hello wolfssl server!";
  7288. int len = (int) XSTRLEN(msg);
  7289. char input[1024];
  7290. int ret, err = 0;
  7291. ((func_args*)args)->return_code = TEST_FAIL;
  7292. /* set defaults */
  7293. if (callbacks->caPemFile == NULL)
  7294. callbacks->caPemFile = caCertFile;
  7295. if (callbacks->certPemFile == NULL)
  7296. callbacks->certPemFile = cliCertFile;
  7297. if (callbacks->keyPemFile == NULL)
  7298. callbacks->keyPemFile = cliKeyFile;
  7299. #ifdef WOLFSSL_STATIC_MEMORY
  7300. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  7301. callbacks->memSz > 0) {
  7302. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  7303. callbacks->mem, callbacks->memSz, 0, 1);
  7304. if (ret != WOLFSSL_SUCCESS) {
  7305. fprintf(stderr, "CTX static new failed %d\n", ret);
  7306. goto cleanup;
  7307. }
  7308. }
  7309. #else
  7310. ctx = wolfSSL_CTX_new(callbacks->method());
  7311. #endif
  7312. if (ctx == NULL) {
  7313. fprintf(stderr, "CTX new failed\n");
  7314. goto cleanup;
  7315. }
  7316. #ifdef WOLFSSL_TIRTOS
  7317. fdOpenSession(Task_self());
  7318. #endif
  7319. if (!callbacks->loadToSSL) {
  7320. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  7321. }
  7322. #ifdef WOLFSSL_ENCRYPTED_KEYS
  7323. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  7324. #endif
  7325. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  7326. WOLFSSL_SUCCESS) {
  7327. goto cleanup;
  7328. }
  7329. if (!callbacks->loadToSSL) {
  7330. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  7331. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7332. goto cleanup;
  7333. }
  7334. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  7335. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7336. goto cleanup;
  7337. }
  7338. }
  7339. #ifdef HAVE_CRL
  7340. if (callbacks->crlPemFile != NULL) {
  7341. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  7342. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7343. goto cleanup;
  7344. }
  7345. }
  7346. #endif
  7347. if (callbacks->ctx_ready)
  7348. callbacks->ctx_ready(ctx);
  7349. ssl = wolfSSL_new(ctx);
  7350. if (wolfSSL_dtls(ssl)) {
  7351. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7352. 1, 0, ssl);
  7353. }
  7354. else {
  7355. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7356. 0, 0, ssl);
  7357. }
  7358. if (wolfSSL_set_fd(ssl, sfd) != WOLFSSL_SUCCESS) {
  7359. goto cleanup;
  7360. }
  7361. if (callbacks->loadToSSL) {
  7362. wolfSSL_SetDevId(ssl, callbacks->devId);
  7363. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  7364. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7365. goto cleanup;
  7366. }
  7367. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  7368. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7369. goto cleanup;
  7370. }
  7371. }
  7372. if (callbacks->ssl_ready)
  7373. callbacks->ssl_ready(ssl);
  7374. #ifdef WOLFSSL_ASYNC_CRYPT
  7375. err = 0; /* Reset error */
  7376. #endif
  7377. do {
  7378. #ifdef WOLFSSL_ASYNC_CRYPT
  7379. if (err == WC_PENDING_E) {
  7380. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7381. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7382. }
  7383. #endif
  7384. ret = wolfSSL_connect(ssl);
  7385. err = wolfSSL_get_error(ssl, ret);
  7386. } while (err == WC_PENDING_E);
  7387. if (ret != WOLFSSL_SUCCESS) {
  7388. char buff[WOLFSSL_MAX_ERROR_SZ];
  7389. fprintf(stderr, "error = %d, %s\n", err,
  7390. wolfSSL_ERR_error_string(err, buff));
  7391. /*err_sys("SSL_connect failed");*/
  7392. }
  7393. else {
  7394. #ifdef WOLFSSL_ASYNC_CRYPT
  7395. err = 0; /* Reset error */
  7396. #endif
  7397. do {
  7398. #ifdef WOLFSSL_ASYNC_CRYPT
  7399. if (err == WC_PENDING_E) {
  7400. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7401. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7402. }
  7403. #endif
  7404. ret = wolfSSL_write(ssl, msg, len);
  7405. err = wolfSSL_get_error(ssl, ret);
  7406. } while (err == WC_PENDING_E);
  7407. if (len != ret)
  7408. goto cleanup;
  7409. #ifdef WOLFSSL_ASYNC_CRYPT
  7410. err = 0; /* Reset error */
  7411. #endif
  7412. do {
  7413. #ifdef WOLFSSL_ASYNC_CRYPT
  7414. if (err == WC_PENDING_E) {
  7415. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7416. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7417. }
  7418. #endif
  7419. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  7420. err = wolfSSL_get_error(ssl, ret);
  7421. } while (err == WC_PENDING_E);
  7422. if (ret > 0) {
  7423. input[ret] = '\0'; /* null term */
  7424. fprintf(stderr, "Server response: %s\n", input);
  7425. }
  7426. ((func_args*)args)->return_code = TEST_SUCCESS;
  7427. }
  7428. if (callbacks->on_result)
  7429. callbacks->on_result(ssl);
  7430. cleanup:
  7431. wolfSSL_free(ssl);
  7432. wolfSSL_CTX_free(ctx);
  7433. CloseSocket(sfd);
  7434. #ifdef WOLFSSL_TIRTOS
  7435. fdCloseSession(Task_self());
  7436. #endif
  7437. }
  7438. #endif /* ENABLE_TLS_CALLBACK_TEST */
  7439. static int test_wolfSSL_read_write(void)
  7440. {
  7441. /* The unit testing for read and write shall happen simultaneously, since
  7442. * one can't do anything with one without the other. (Except for a failure
  7443. * test case.) This function will call all the others that will set up,
  7444. * execute, and report their test findings.
  7445. *
  7446. * Set up the success case first. This function will become the template
  7447. * for the other tests. This should eventually be renamed
  7448. *
  7449. * The success case isn't interesting, how can this fail?
  7450. * - Do not give the client context a CA certificate. The connect should
  7451. * fail. Do not need server for this?
  7452. * - Using NULL for the ssl object on server. Do not need client for this.
  7453. * - Using NULL for the ssl object on client. Do not need server for this.
  7454. * - Good ssl objects for client and server. Client write() without server
  7455. * read().
  7456. * - Good ssl objects for client and server. Server write() without client
  7457. * read().
  7458. * - Forgetting the password callback?
  7459. */
  7460. tcp_ready ready;
  7461. func_args client_args;
  7462. func_args server_args;
  7463. THREAD_TYPE serverThread;
  7464. EXPECT_DECLS;
  7465. XMEMSET(&client_args, 0, sizeof(func_args));
  7466. XMEMSET(&server_args, 0, sizeof(func_args));
  7467. #ifdef WOLFSSL_TIRTOS
  7468. fdOpenSession(Task_self());
  7469. #endif
  7470. StartTCP();
  7471. InitTcpReady(&ready);
  7472. #if defined(USE_WINDOWS_API)
  7473. /* use RNG to get random port if using windows */
  7474. ready.port = GetRandomPort();
  7475. #endif
  7476. server_args.signal = &ready;
  7477. client_args.signal = &ready;
  7478. start_thread(test_server_nofail, &server_args, &serverThread);
  7479. wait_tcp_ready(&server_args);
  7480. test_client_nofail(&client_args, NULL);
  7481. join_thread(serverThread);
  7482. ExpectTrue(client_args.return_code);
  7483. ExpectTrue(server_args.return_code);
  7484. FreeTcpReady(&ready);
  7485. #ifdef WOLFSSL_TIRTOS
  7486. fdOpenSession(Task_self());
  7487. #endif
  7488. return EXPECT_RESULT();
  7489. }
  7490. static int test_wolfSSL_reuse_WOLFSSLobj(void)
  7491. {
  7492. EXPECT_DECLS;
  7493. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  7494. !defined(WOLFSSL_NO_TLS12)
  7495. /* The unit test for session resumption by re-using WOLFSSL object.
  7496. * WOLFSSL object is not cleared after first session. It reuse the object
  7497. * for second connection.
  7498. */
  7499. tcp_ready ready;
  7500. func_args client_args;
  7501. func_args server_args;
  7502. THREAD_TYPE serverThread;
  7503. callback_functions client_cbf;
  7504. callback_functions server_cbf;
  7505. XMEMSET(&client_args, 0, sizeof(func_args));
  7506. XMEMSET(&server_args, 0, sizeof(func_args));
  7507. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  7508. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7509. #ifdef WOLFSSL_TIRTOS
  7510. fdOpenSession(Task_self());
  7511. #endif
  7512. StartTCP();
  7513. InitTcpReady(&ready);
  7514. #if defined(USE_WINDOWS_API)
  7515. /* use RNG to get random port if using windows */
  7516. ready.port = GetRandomPort();
  7517. #endif
  7518. client_cbf.method = wolfTLSv1_2_client_method;
  7519. server_cbf.method = wolfTLSv1_2_server_method;
  7520. client_args.callbacks = &client_cbf;
  7521. server_args.callbacks = &server_cbf;
  7522. server_args.signal = &ready;
  7523. client_args.signal = &ready;
  7524. /* the var is used for loop number */
  7525. server_args.argc = 2;
  7526. start_thread(test_server_loop, &server_args, &serverThread);
  7527. wait_tcp_ready(&server_args);
  7528. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  7529. join_thread(serverThread);
  7530. ExpectTrue(client_args.return_code);
  7531. ExpectTrue(server_args.return_code);
  7532. FreeTcpReady(&ready);
  7533. #ifdef WOLFSSL_TIRTOS
  7534. fdOpenSession(Task_self());
  7535. #endif
  7536. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  7537. * !defined(WOLFSSL_TLS13) */
  7538. return EXPECT_RESULT();
  7539. }
  7540. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7541. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7542. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready(
  7543. WOLFSSL_CTX* ctx)
  7544. {
  7545. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7546. myVerifyAction = VERIFY_USE_PREVERFIY;
  7547. wolfSSL_CTX_set_verify_depth(ctx, 2);
  7548. return TEST_SUCCESS;
  7549. }
  7550. #endif
  7551. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1(void)
  7552. {
  7553. EXPECT_DECLS;
  7554. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7555. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7556. test_ssl_cbf client_cbf;
  7557. test_ssl_cbf server_cbf;
  7558. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7559. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7560. #ifdef WOLFSSL_TLS13
  7561. client_cbf.method = wolfTLSv1_3_client_method;
  7562. #endif /* WOLFSSL_TLS13 */
  7563. client_cbf.ctx_ready =
  7564. test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready;
  7565. /* test case 1 verify depth is equal to peer chain */
  7566. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7567. &server_cbf, NULL), TEST_SUCCESS);
  7568. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7569. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7570. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7571. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7572. return EXPECT_RESULT();
  7573. }
  7574. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7575. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7576. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready(
  7577. WOLFSSL_CTX* ctx)
  7578. {
  7579. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7580. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  7581. wolfSSL_CTX_set_verify_depth(ctx, 0);
  7582. return TEST_SUCCESS;
  7583. }
  7584. #endif
  7585. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2(void)
  7586. {
  7587. EXPECT_DECLS;
  7588. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7589. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7590. test_ssl_cbf client_cbf;
  7591. test_ssl_cbf server_cbf;
  7592. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7593. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7594. #ifdef WOLFSSL_TLS13
  7595. client_cbf.method = wolfTLSv1_3_client_method;
  7596. #endif /* WOLFSSL_TLS13 */
  7597. client_cbf.ctx_ready =
  7598. test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready;
  7599. /* test case 2
  7600. * verify depth is zero, number of peer's chain is 2.
  7601. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  7602. * callback.
  7603. */
  7604. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7605. &server_cbf, NULL), TEST_SUCCESS);
  7606. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7607. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7608. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7609. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7610. return EXPECT_RESULT();
  7611. }
  7612. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7613. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7614. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready(
  7615. WOLFSSL_CTX* ctx)
  7616. {
  7617. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7618. myVerifyAction = VERIFY_USE_PREVERFIY;
  7619. wolfSSL_CTX_set_verify_depth(ctx, 0);
  7620. return TEST_SUCCESS;
  7621. }
  7622. #endif
  7623. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3(void)
  7624. {
  7625. EXPECT_DECLS;
  7626. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7627. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7628. test_ssl_cbf client_cbf;
  7629. test_ssl_cbf server_cbf;
  7630. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7631. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7632. #ifdef WOLFSSL_TLS13
  7633. client_cbf.method = wolfTLSv1_3_client_method;
  7634. #endif /* WOLFSSL_TLS13 */
  7635. client_cbf.ctx_ready =
  7636. test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready;
  7637. /* test case 3
  7638. * verify depth is zero, number of peer's chain is 2
  7639. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  7640. * therefore, handshake becomes failure.
  7641. */
  7642. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7643. &server_cbf, NULL), TEST_FAIL);
  7644. ExpectIntEQ(client_cbf.return_code, TEST_FAIL);
  7645. ExpectIntEQ(server_cbf.return_code, TEST_FAIL);
  7646. ExpectIntEQ(client_cbf.last_err, MAX_CHAIN_ERROR);
  7647. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  7648. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7649. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7650. return EXPECT_RESULT();
  7651. }
  7652. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7653. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  7654. && !defined(NO_SHA256) && defined(HAVE_ECC)
  7655. static int test_wolfSSL_CTX_set_cipher_list_server_ctx_ready(WOLFSSL_CTX* ctx)
  7656. {
  7657. EXPECT_DECLS;
  7658. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "DEFAULT:!NULL"));
  7659. return EXPECT_RESULT();
  7660. }
  7661. static int test_wolfSSL_CTX_set_cipher_list_client_ctx_ready(WOLFSSL_CTX* ctx)
  7662. {
  7663. EXPECT_DECLS;
  7664. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256"));
  7665. return EXPECT_RESULT();
  7666. }
  7667. #endif
  7668. static int test_wolfSSL_CTX_set_cipher_list(void)
  7669. {
  7670. EXPECT_DECLS;
  7671. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7672. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  7673. && !defined(NO_SHA256) && defined(HAVE_ECC)
  7674. WOLFSSL_CTX* ctxClient = NULL;
  7675. WOLFSSL* sslClient = NULL;
  7676. test_ssl_cbf client_cbf;
  7677. test_ssl_cbf server_cbf;
  7678. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7679. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7680. server_cbf.method = wolfTLSv1_2_server_method;
  7681. server_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_server_ctx_ready;
  7682. client_cbf.method = wolfTLSv1_2_client_method;
  7683. client_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_client_ctx_ready;
  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. /* check with cipher string that has '+' */
  7689. ExpectNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
  7690. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE+AESGCM"));
  7691. ExpectNotNull((sslClient = wolfSSL_new(ctxClient)));
  7692. /* check for the existence of an ECDHE ECDSA cipher suite */
  7693. if (EXPECT_SUCCESS()) {
  7694. int i = 0;
  7695. int found = 0;
  7696. const char* suite;
  7697. WOLF_STACK_OF(WOLFSSL_CIPHER)* sk = NULL;
  7698. WOLFSSL_CIPHER* current;
  7699. ExpectNotNull((sk = wolfSSL_get_ciphers_compat(sslClient)));
  7700. do {
  7701. current = wolfSSL_sk_SSL_CIPHER_value(sk, i++);
  7702. if (current) {
  7703. suite = wolfSSL_CIPHER_get_name(current);
  7704. if (suite && XSTRSTR(suite, "ECDSA")) {
  7705. found = 1;
  7706. break;
  7707. }
  7708. }
  7709. } while (current);
  7710. ExpectIntEQ(found, 1);
  7711. }
  7712. wolfSSL_free(sslClient);
  7713. wolfSSL_CTX_free(ctxClient);
  7714. #endif
  7715. return EXPECT_RESULT();
  7716. }
  7717. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7718. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7719. static int test_wolfSSL_get_finished_client_on_handshake(WOLFSSL_CTX* ctx,
  7720. WOLFSSL* ssl)
  7721. {
  7722. EXPECT_DECLS;
  7723. size_t msg_len;
  7724. (void)ctx;
  7725. /* get_finished test */
  7726. /* 1. get own sent message */
  7727. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  7728. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  7729. ExpectIntGE(msg_len, 0);
  7730. /* 2. get peer message */
  7731. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  7732. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  7733. ExpectIntGE(msg_len, 0);
  7734. return EXPECT_RESULT();
  7735. }
  7736. #endif
  7737. static int test_wolfSSL_get_finished(void)
  7738. {
  7739. EXPECT_DECLS;
  7740. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7741. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7742. test_ssl_cbf client_cbf;
  7743. test_ssl_cbf server_cbf;
  7744. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7745. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7746. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7747. &server_cbf, test_wolfSSL_get_finished_client_on_handshake),
  7748. TEST_SUCCESS);
  7749. /* test received msg vs sent msg */
  7750. ExpectIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  7751. ExpectIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  7752. #endif /* HAVE_SSL_MEMIO_TESTS_DEPENDENCIES && WOLFSSL_HAVE_TLS_UNIQUE */
  7753. return EXPECT_RESULT();
  7754. }
  7755. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7756. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7757. !defined(NO_SESSION_CACHE)
  7758. /* Sessions to restore/store */
  7759. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  7760. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  7761. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  7762. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  7763. {
  7764. /* Don't store sessions. Lookup is still enabled. */
  7765. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7766. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7767. #ifdef OPENSSL_EXTRA
  7768. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7769. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7770. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7771. #endif
  7772. /* Require both peers to provide certs */
  7773. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7774. }
  7775. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  7776. {
  7777. WOLFSSL_SESSION** sess;
  7778. if (wolfSSL_is_server(ssl))
  7779. sess = &test_wolfSSL_CTX_add_session_server_sess;
  7780. else
  7781. sess = &test_wolfSSL_CTX_add_session_client_sess;
  7782. if (*sess == NULL) {
  7783. #ifdef NO_SESSION_CACHE_REF
  7784. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7785. #else
  7786. /* Test for backwards compatibility */
  7787. if (wolfSSL_is_server(ssl)) {
  7788. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7789. }
  7790. else {
  7791. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  7792. }
  7793. #endif
  7794. /* Now save the session in the internal store to make it available
  7795. * for lookup. For TLS 1.3, we can't save the session without
  7796. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  7797. * session from cache. */
  7798. if (wolfSSL_is_server(ssl)
  7799. #ifndef WOLFSSL_TICKET_HAVE_ID
  7800. && wolfSSL_version(ssl) != TLS1_3_VERSION
  7801. #endif
  7802. )
  7803. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  7804. *sess), WOLFSSL_SUCCESS);
  7805. }
  7806. else {
  7807. /* If we have a session retrieved then remaining connections should be
  7808. * resuming on that session */
  7809. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  7810. }
  7811. /* Save CTX to be able to decrypt tickets */
  7812. if (wolfSSL_is_server(ssl) &&
  7813. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  7814. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  7815. = wolfSSL_get_SSL_CTX(ssl));
  7816. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  7817. WOLFSSL_SUCCESS);
  7818. }
  7819. #ifdef SESSION_CERTS
  7820. #ifndef WOLFSSL_TICKET_HAVE_ID
  7821. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7822. wolfSSL_session_reused(ssl))
  7823. #endif
  7824. {
  7825. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  7826. * for all connections. TLS 1.3 only has tickets so if we don't
  7827. * include the session id in the ticket then the certificates
  7828. * will not be available on resumption. */
  7829. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  7830. AssertNotNull(peer);
  7831. wolfSSL_X509_free(peer);
  7832. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7833. #ifdef OPENSSL_EXTRA
  7834. AssertNotNull(SSL_SESSION_get0_peer(*sess));
  7835. #endif
  7836. }
  7837. #endif /* SESSION_CERTS */
  7838. }
  7839. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  7840. {
  7841. /* Set the session to reuse for the client */
  7842. AssertIntEQ(wolfSSL_set_session(ssl,
  7843. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  7844. }
  7845. #endif
  7846. static int test_wolfSSL_CTX_add_session(void)
  7847. {
  7848. EXPECT_DECLS;
  7849. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7850. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7851. !defined(NO_SESSION_CACHE)
  7852. tcp_ready ready;
  7853. func_args client_args;
  7854. func_args server_args;
  7855. THREAD_TYPE serverThread;
  7856. callback_functions client_cb;
  7857. callback_functions server_cb;
  7858. method_provider methods[][2] = {
  7859. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7860. !defined(NO_DES3))
  7861. /* Without AES there are almost no ciphersuites available. This leads
  7862. * to no ciphersuites being available and an error. */
  7863. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  7864. #endif
  7865. #ifndef WOLFSSL_NO_TLS12
  7866. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  7867. #endif
  7868. /* Needs the default ticket callback since it is tied to the
  7869. * connection context and this makes it easy to carry over the ticket
  7870. * crypto context between connections */
  7871. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7872. defined(HAVE_SESSION_TICKET)
  7873. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  7874. #endif
  7875. };
  7876. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  7877. size_t i, j;
  7878. for (i = 0; i < methodsLen; i++) {
  7879. /* First run creates a connection while the second+ run will attempt
  7880. * to resume the connection. The trick is that the internal cache
  7881. * is turned off. wolfSSL_CTX_add_session should put the session in
  7882. * the cache anyway. */
  7883. test_wolfSSL_CTX_add_session_client_sess = NULL;
  7884. test_wolfSSL_CTX_add_session_server_sess = NULL;
  7885. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  7886. #ifdef NO_SESSION_CACHE_REF
  7887. for (j = 0; j < 4; j++) {
  7888. #else
  7889. /* The session may be overwritten in this case. Do only one resumption
  7890. * to stop this test from failing intermittently. */
  7891. for (j = 0; j < 2; j++) {
  7892. #endif
  7893. #ifdef WOLFSSL_TIRTOS
  7894. fdOpenSession(Task_self());
  7895. #endif
  7896. StartTCP();
  7897. InitTcpReady(&ready);
  7898. XMEMSET(&client_args, 0, sizeof(func_args));
  7899. XMEMSET(&server_args, 0, sizeof(func_args));
  7900. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7901. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7902. client_cb.method = methods[i][0];
  7903. server_cb.method = methods[i][1];
  7904. server_args.signal = &ready;
  7905. server_args.callbacks = &server_cb;
  7906. client_args.signal = &ready;
  7907. client_args.callbacks = &client_cb;
  7908. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  7909. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  7910. server_cb.isSharedCtx = 1;
  7911. }
  7912. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  7913. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  7914. if (j != 0)
  7915. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  7916. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  7917. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  7918. server_cb.ticNoInit = 1; /* Use default builtin */
  7919. start_thread(test_server_nofail, &server_args, &serverThread);
  7920. wait_tcp_ready(&server_args);
  7921. test_client_nofail(&client_args, NULL);
  7922. join_thread(serverThread);
  7923. ExpectTrue(client_args.return_code);
  7924. ExpectTrue(server_args.return_code);
  7925. FreeTcpReady(&ready);
  7926. if (EXPECT_FAIL())
  7927. break;
  7928. }
  7929. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  7930. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  7931. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  7932. if (EXPECT_FAIL())
  7933. break;
  7934. }
  7935. #endif
  7936. return EXPECT_RESULT();
  7937. }
  7938. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7939. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  7940. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  7941. defined(HAVE_SESSION_TICKET) && \
  7942. !defined(TITAN_SESSION_CACHE) && \
  7943. !defined(HUGE_SESSION_CACHE) && \
  7944. !defined(BIG_SESSION_CACHE) && \
  7945. !defined(MEDIUM_SESSION_CACHE)
  7946. /* twcase - prefix for test_wolfSSL_CTX_add_session_ext */
  7947. /* Sessions to restore/store */
  7948. static WOLFSSL_SESSION* twcase_server_first_session_ptr;
  7949. static WOLFSSL_SESSION* twcase_client_first_session_ptr;
  7950. static WOLFSSL_CTX* twcase_server_current_ctx_ptr;
  7951. static int twcase_new_session_called = 0;
  7952. static int twcase_remove_session_called = 0;
  7953. static int twcase_get_session_called = 0;
  7954. /* Test default, SESSIONS_PER_ROW*SESSION_ROWS = 3*11, see ssl.c */
  7955. #define SESSION_CACHE_SIZE 33
  7956. typedef struct {
  7957. const byte* key; /* key, altSessionID, session ID, NULL if empty */
  7958. WOLFSSL_SESSION* value;
  7959. } hashTable_entry;
  7960. typedef struct {
  7961. hashTable_entry entries[SESSION_CACHE_SIZE]; /* hash slots */
  7962. size_t capacity; /* size of entries */
  7963. size_t length; /* number of items in the hash table */
  7964. wolfSSL_Mutex htLock; /* lock */
  7965. }hashTable;
  7966. static hashTable server_sessionCache;
  7967. static int twcase_new_sessionCb(WOLFSSL *ssl, WOLFSSL_SESSION *sess)
  7968. {
  7969. int i;
  7970. unsigned int len;
  7971. (void)ssl;
  7972. /*
  7973. * This example uses a hash table.
  7974. * Steps you should take for a non-demo code:
  7975. * - acquire a lock for the file named according to the session id
  7976. * - open the file
  7977. * - encrypt and write the SSL_SESSION object to the file
  7978. * - release the lock
  7979. *
  7980. * Return:
  7981. * 0: The callback does not wish to hold a reference of the sess
  7982. * 1: The callback wants to hold a reference of the sess. The callback is
  7983. * now also responsible for calling wolfSSL_SESSION_free() on sess.
  7984. */
  7985. if (sess == NULL)
  7986. return 0;
  7987. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  7988. return 0;
  7989. }
  7990. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  7991. if (server_sessionCache.entries[i].value == NULL) {
  7992. server_sessionCache.entries[i].key = SSL_SESSION_get_id(sess, &len);
  7993. server_sessionCache.entries[i].value = sess;
  7994. server_sessionCache.length++;
  7995. break;
  7996. }
  7997. }
  7998. ++twcase_new_session_called;
  7999. wc_UnLockMutex(&server_sessionCache.htLock);
  8000. fprintf(stderr, "\t\ttwcase_new_session_called %d\n",
  8001. twcase_new_session_called);
  8002. return 1;
  8003. }
  8004. static void twcase_remove_sessionCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  8005. {
  8006. int i;
  8007. (void)ctx;
  8008. (void)sess;
  8009. if (sess == NULL)
  8010. return;
  8011. /*
  8012. * This example uses a hash table.
  8013. * Steps you should take for a non-demo code:
  8014. * - acquire a lock for the file named according to the session id
  8015. * - remove the file
  8016. * - release the lock
  8017. */
  8018. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  8019. return;
  8020. }
  8021. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8022. if (server_sessionCache.entries[i].key != NULL &&
  8023. XMEMCMP(server_sessionCache.entries[i].key,
  8024. sess->sessionID, SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  8025. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  8026. server_sessionCache.entries[i].value = NULL;
  8027. server_sessionCache.entries[i].key = NULL;
  8028. server_sessionCache.length--;
  8029. break;
  8030. }
  8031. }
  8032. ++twcase_remove_session_called;
  8033. wc_UnLockMutex(&server_sessionCache.htLock);
  8034. fprintf(stderr, "\t\ttwcase_remove_session_called %d\n",
  8035. twcase_remove_session_called);
  8036. }
  8037. static WOLFSSL_SESSION *twcase_get_sessionCb(WOLFSSL *ssl,
  8038. const unsigned char *id, int len, int *ref)
  8039. {
  8040. int i;
  8041. (void)ssl;
  8042. (void)id;
  8043. (void)len;
  8044. /*
  8045. * This example uses a hash table.
  8046. * Steps you should take for a non-demo code:
  8047. * - acquire a lock for the file named according to the session id in the
  8048. * 2nd arg
  8049. * - read and decrypt contents of file and create a new SSL_SESSION
  8050. * - object release the lock
  8051. * - return the new session object
  8052. */
  8053. fprintf(stderr, "\t\ttwcase_get_session_called %d\n",
  8054. ++twcase_get_session_called);
  8055. /* This callback want to retain a copy of the object. If we want wolfSSL to
  8056. * be responsible for the pointer then set to 0. */
  8057. *ref = 1;
  8058. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8059. if (server_sessionCache.entries[i].key != NULL &&
  8060. XMEMCMP(server_sessionCache.entries[i].key, id,
  8061. SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  8062. return server_sessionCache.entries[i].value;
  8063. }
  8064. }
  8065. return NULL;
  8066. }
  8067. static int twcase_get_sessionCb_cleanup(void)
  8068. {
  8069. int i;
  8070. int cnt = 0;
  8071. /* If twcase_get_sessionCb sets *ref = 1, the application is responsible
  8072. * for freeing sessions */
  8073. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8074. if (server_sessionCache.entries[i].value != NULL) {
  8075. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  8076. cnt++;
  8077. }
  8078. }
  8079. fprintf(stderr, "\t\ttwcase_get_sessionCb_cleanup freed %d sessions\n",
  8080. cnt);
  8081. return TEST_SUCCESS;
  8082. }
  8083. static int twcase_cache_intOff_extOff(WOLFSSL_CTX* ctx)
  8084. {
  8085. EXPECT_DECLS;
  8086. /* off - Disable internal cache */
  8087. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  8088. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  8089. #ifdef OPENSSL_EXTRA
  8090. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  8091. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  8092. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  8093. #endif
  8094. /* off - Do not setup external cache */
  8095. /* Require both peers to provide certs */
  8096. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8097. return EXPECT_RESULT();
  8098. }
  8099. static int twcase_cache_intOn_extOff(WOLFSSL_CTX* ctx)
  8100. {
  8101. /* on - internal cache is on by default */
  8102. /* off - Do not setup external cache */
  8103. /* Require both peers to provide certs */
  8104. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8105. return TEST_SUCCESS;
  8106. }
  8107. static int twcase_cache_intOff_extOn(WOLFSSL_CTX* ctx)
  8108. {
  8109. EXPECT_DECLS;
  8110. /* off - Disable internal cache */
  8111. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  8112. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  8113. #ifdef OPENSSL_EXTRA
  8114. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  8115. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  8116. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  8117. #endif
  8118. /* on - Enable external cache */
  8119. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8120. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8121. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8122. /* Require both peers to provide certs */
  8123. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8124. return EXPECT_RESULT();
  8125. }
  8126. static int twcase_cache_intOn_extOn(WOLFSSL_CTX* ctx)
  8127. {
  8128. /* on - internal cache is on by default */
  8129. /* on - Enable external cache */
  8130. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8131. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8132. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8133. /* Require both peers to provide certs */
  8134. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8135. return TEST_SUCCESS;
  8136. }
  8137. static int twcase_cache_intOn_extOn_noTicket(WOLFSSL_CTX* ctx)
  8138. {
  8139. /* on - internal cache is on by default */
  8140. /* on - Enable external cache */
  8141. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8142. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8143. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8144. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TICKET);
  8145. /* Require both peers to provide certs */
  8146. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8147. return TEST_SUCCESS;
  8148. }
  8149. static int twcase_server_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  8150. {
  8151. EXPECT_DECLS;
  8152. WOLFSSL_SESSION** sess;
  8153. if (wolfSSL_is_server(ssl))
  8154. sess = &twcase_server_first_session_ptr;
  8155. else
  8156. return TEST_SUCCESS;
  8157. if (*sess == NULL) {
  8158. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  8159. /* Now save the session in the internal store to make it available
  8160. * for lookup. For TLS 1.3, we can't save the session without
  8161. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  8162. * session from cache. */
  8163. if (wolfSSL_is_server(ssl)
  8164. #ifndef WOLFSSL_TICKET_HAVE_ID
  8165. && wolfSSL_version(ssl) != TLS1_3_VERSION
  8166. && wolfSSL_version(ssl) != DTLS1_3_VERSION
  8167. #endif
  8168. ) {
  8169. ExpectIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  8170. *sess), WOLFSSL_SUCCESS);
  8171. }
  8172. }
  8173. /* Save CTX to be able to decrypt tickets */
  8174. if (twcase_server_current_ctx_ptr == NULL) {
  8175. ExpectNotNull(twcase_server_current_ctx_ptr = wolfSSL_get_SSL_CTX(ssl));
  8176. ExpectIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  8177. WOLFSSL_SUCCESS);
  8178. }
  8179. #ifdef SESSION_CERTS
  8180. #ifndef WOLFSSL_TICKET_HAVE_ID
  8181. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  8182. wolfSSL_session_reused(ssl))
  8183. #endif
  8184. {
  8185. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  8186. * for all connections. TLS 1.3 only has tickets so if we don't
  8187. * include the session id in the ticket then the certificates
  8188. * will not be available on resumption. */
  8189. WOLFSSL_X509* peer = NULL;
  8190. ExpectNotNull(peer = wolfSSL_get_peer_certificate(ssl));
  8191. wolfSSL_X509_free(peer);
  8192. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  8193. }
  8194. #endif
  8195. return EXPECT_RESULT();
  8196. }
  8197. static int twcase_client_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  8198. {
  8199. EXPECT_DECLS;
  8200. WOLFSSL_SESSION** sess;
  8201. sess = &twcase_client_first_session_ptr;
  8202. if (*sess == NULL) {
  8203. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  8204. }
  8205. else {
  8206. /* If we have a session retrieved then remaining connections should be
  8207. * resuming on that session */
  8208. ExpectIntEQ(wolfSSL_session_reused(ssl), 1);
  8209. }
  8210. #ifdef SESSION_CERTS
  8211. #ifndef WOLFSSL_TICKET_HAVE_ID
  8212. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  8213. wolfSSL_session_reused(ssl))
  8214. #endif
  8215. {
  8216. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  8217. ExpectNotNull(peer);
  8218. wolfSSL_X509_free(peer);
  8219. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  8220. #ifdef OPENSSL_EXTRA
  8221. ExpectNotNull(wolfSSL_SESSION_get0_peer(*sess));
  8222. #endif
  8223. }
  8224. #endif
  8225. return EXPECT_RESULT();
  8226. }
  8227. static int twcase_client_set_sess_ssl_ready(WOLFSSL* ssl)
  8228. {
  8229. EXPECT_DECLS;
  8230. /* Set the session to reuse for the client */
  8231. ExpectNotNull(ssl);
  8232. ExpectNotNull(twcase_client_first_session_ptr);
  8233. ExpectIntEQ(wolfSSL_set_session(ssl,twcase_client_first_session_ptr),
  8234. WOLFSSL_SUCCESS);
  8235. return EXPECT_RESULT();
  8236. }
  8237. struct test_add_session_ext_params {
  8238. method_provider client_meth;
  8239. method_provider server_meth;
  8240. const char* tls_version;
  8241. };
  8242. static int test_wolfSSL_CTX_add_session_ext(
  8243. struct test_add_session_ext_params* param)
  8244. {
  8245. EXPECT_DECLS;
  8246. /* Test the default 33 sessions */
  8247. int j;
  8248. /* Clear cache before starting */
  8249. wolfSSL_CTX_flush_sessions(NULL, -1);
  8250. XMEMSET(&server_sessionCache, 0, sizeof(hashTable));
  8251. if (wc_InitMutex(&server_sessionCache.htLock) != 0)
  8252. return BAD_MUTEX_E;
  8253. server_sessionCache.capacity = SESSION_CACHE_SIZE;
  8254. fprintf(stderr, "\tBegin %s\n", param->tls_version);
  8255. for (j = 0; j < 5; j++) {
  8256. int tls13 = XSTRSTR(param->tls_version, "TLSv1_3") != NULL;
  8257. int dtls = XSTRSTR(param->tls_version, "DTLS") != NULL;
  8258. test_ssl_cbf client_cb;
  8259. test_ssl_cbf server_cb;
  8260. (void)dtls;
  8261. /* Test five cache configurations */
  8262. twcase_client_first_session_ptr = NULL;
  8263. twcase_server_first_session_ptr = NULL;
  8264. twcase_server_current_ctx_ptr = NULL;
  8265. twcase_new_session_called = 0;
  8266. twcase_remove_session_called = 0;
  8267. twcase_get_session_called = 0;
  8268. /* connection 1 - first connection */
  8269. fprintf(stderr, "\tconnect: %s: j=%d\n", param->tls_version, j);
  8270. XMEMSET(&client_cb, 0, sizeof(client_cb));
  8271. XMEMSET(&server_cb, 0, sizeof(server_cb));
  8272. client_cb.method = param->client_meth;
  8273. server_cb.method = param->server_meth;
  8274. if (dtls)
  8275. client_cb.doUdp = server_cb.doUdp = 1;
  8276. /* Setup internal and external cache */
  8277. switch (j) {
  8278. case 0:
  8279. /* SSL_OP_NO_TICKET stateful ticket case */
  8280. server_cb.ctx_ready = twcase_cache_intOn_extOn_noTicket;
  8281. break;
  8282. case 1:
  8283. server_cb.ctx_ready = twcase_cache_intOn_extOn;
  8284. break;
  8285. case 2:
  8286. server_cb.ctx_ready = twcase_cache_intOff_extOn;
  8287. break;
  8288. case 3:
  8289. server_cb.ctx_ready = twcase_cache_intOn_extOff;
  8290. break;
  8291. case 4:
  8292. server_cb.ctx_ready = twcase_cache_intOff_extOff;
  8293. break;
  8294. }
  8295. client_cb.ctx_ready = twcase_cache_intOff_extOff;
  8296. /* Add session to internal cache and save SSL session for testing */
  8297. server_cb.on_result = twcase_server_sess_ctx_pre_shutdown;
  8298. /* Save client SSL session for testing */
  8299. client_cb.on_result = twcase_client_sess_ctx_pre_shutdown;
  8300. server_cb.ticNoInit = 1; /* Use default builtin */
  8301. /* Don't free/release ctx */
  8302. server_cb.ctx = twcase_server_current_ctx_ptr;
  8303. server_cb.isSharedCtx = 1;
  8304. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  8305. &server_cb, NULL), TEST_SUCCESS);
  8306. ExpectIntEQ(twcase_get_session_called, 0);
  8307. if (EXPECT_FAIL()) {
  8308. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  8309. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  8310. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  8311. break;
  8312. }
  8313. switch (j) {
  8314. case 0:
  8315. case 1:
  8316. case 2:
  8317. /* cache cannot be searched with out a connection */
  8318. /* Add a new session */
  8319. ExpectIntEQ(twcase_new_session_called, 1);
  8320. /* In twcase_server_sess_ctx_pre_shutdown
  8321. * wolfSSL_CTX_add_session which evicts the existing session
  8322. * in cache and adds it back in */
  8323. ExpectIntLE(twcase_remove_session_called, 1);
  8324. break;
  8325. case 3:
  8326. case 4:
  8327. /* no external cache */
  8328. ExpectIntEQ(twcase_new_session_called, 0);
  8329. ExpectIntEQ(twcase_remove_session_called, 0);
  8330. break;
  8331. }
  8332. /* connection 2 - session resume */
  8333. fprintf(stderr, "\tresume: %s: j=%d\n", param->tls_version, j);
  8334. twcase_new_session_called = 0;
  8335. twcase_remove_session_called = 0;
  8336. twcase_get_session_called = 0;
  8337. server_cb.on_result = 0;
  8338. client_cb.on_result = 0;
  8339. server_cb.ticNoInit = 1; /* Use default builtin */
  8340. server_cb.ctx = twcase_server_current_ctx_ptr;
  8341. /* try session resumption */
  8342. client_cb.ssl_ready = twcase_client_set_sess_ssl_ready;
  8343. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  8344. &server_cb, NULL), TEST_SUCCESS);
  8345. /* Clear cache before checking */
  8346. wolfSSL_CTX_flush_sessions(NULL, -1);
  8347. switch (j) {
  8348. case 0:
  8349. if (tls13) {
  8350. /* (D)TLSv1.3 stateful case */
  8351. /* cache hit */
  8352. /* DTLS accesses cache once for stateless parsing and
  8353. * once for stateful parsing */
  8354. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8355. /* (D)TLSv1.3 creates a new ticket,
  8356. * updates both internal and external cache */
  8357. ExpectIntEQ(twcase_new_session_called, 1);
  8358. /* A new session ID is created for a new ticket */
  8359. ExpectIntEQ(twcase_remove_session_called, 2);
  8360. }
  8361. else {
  8362. /* non (D)TLSv1.3 case, no update */
  8363. /* DTLS accesses cache once for stateless parsing and
  8364. * once for stateful parsing */
  8365. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8366. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8367. #else
  8368. ExpectIntEQ(twcase_get_session_called, 1);
  8369. #endif
  8370. ExpectIntEQ(twcase_new_session_called, 0);
  8371. /* Called on session added in
  8372. * twcase_server_sess_ctx_pre_shutdown */
  8373. ExpectIntEQ(twcase_remove_session_called, 1);
  8374. }
  8375. break;
  8376. case 1:
  8377. if (tls13) {
  8378. /* (D)TLSv1.3 case */
  8379. /* cache hit */
  8380. ExpectIntEQ(twcase_get_session_called, 1);
  8381. /* (D)TLSv1.3 creates a new ticket,
  8382. * updates both internal and external cache */
  8383. ExpectIntEQ(twcase_new_session_called, 1);
  8384. /* Called on session added in
  8385. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  8386. ExpectIntEQ(twcase_remove_session_called, 1);
  8387. }
  8388. else {
  8389. /* non (D)TLSv1.3 case */
  8390. /* cache hit */
  8391. /* DTLS accesses cache once for stateless parsing and
  8392. * once for stateful parsing */
  8393. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8394. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8395. #else
  8396. ExpectIntEQ(twcase_get_session_called, 1);
  8397. #endif
  8398. ExpectIntEQ(twcase_new_session_called, 0);
  8399. /* Called on session added in
  8400. * twcase_server_sess_ctx_pre_shutdown */
  8401. ExpectIntEQ(twcase_remove_session_called, 1);
  8402. }
  8403. break;
  8404. case 2:
  8405. if (tls13) {
  8406. /* (D)TLSv1.3 case */
  8407. /* cache hit */
  8408. ExpectIntEQ(twcase_get_session_called, 1);
  8409. /* (D)TLSv1.3 creates a new ticket,
  8410. * updates both internal and external cache */
  8411. ExpectIntEQ(twcase_new_session_called, 1);
  8412. /* Called on session added in
  8413. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  8414. ExpectIntEQ(twcase_remove_session_called, 1);
  8415. }
  8416. else {
  8417. /* non (D)TLSv1.3 case */
  8418. /* cache hit */
  8419. /* DTLS accesses cache once for stateless parsing and
  8420. * once for stateful parsing */
  8421. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8422. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8423. #else
  8424. ExpectIntEQ(twcase_get_session_called, 1);
  8425. #endif
  8426. ExpectIntEQ(twcase_new_session_called, 0);
  8427. /* Called on session added in
  8428. * twcase_server_sess_ctx_pre_shutdown */
  8429. ExpectIntEQ(twcase_remove_session_called, 1);
  8430. }
  8431. break;
  8432. case 3:
  8433. case 4:
  8434. /* no external cache */
  8435. ExpectIntEQ(twcase_get_session_called, 0);
  8436. ExpectIntEQ(twcase_new_session_called, 0);
  8437. ExpectIntEQ(twcase_remove_session_called, 0);
  8438. break;
  8439. }
  8440. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  8441. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  8442. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  8443. if (EXPECT_FAIL())
  8444. break;
  8445. }
  8446. twcase_get_sessionCb_cleanup();
  8447. XMEMSET(&server_sessionCache.entries, 0,
  8448. sizeof(server_sessionCache.entries));
  8449. fprintf(stderr, "\tEnd %s\n", param->tls_version);
  8450. wc_FreeMutex(&server_sessionCache.htLock);
  8451. return EXPECT_RESULT();
  8452. }
  8453. #endif
  8454. static int test_wolfSSL_CTX_add_session_ext_tls13(void)
  8455. {
  8456. EXPECT_DECLS;
  8457. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8458. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8459. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8460. defined(HAVE_SESSION_TICKET) && \
  8461. !defined(TITAN_SESSION_CACHE) && \
  8462. !defined(HUGE_SESSION_CACHE) && \
  8463. !defined(BIG_SESSION_CACHE) && \
  8464. !defined(MEDIUM_SESSION_CACHE)
  8465. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  8466. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  8467. struct test_add_session_ext_params param[1] = {
  8468. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" }
  8469. };
  8470. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8471. #endif
  8472. #endif
  8473. return EXPECT_RESULT();
  8474. }
  8475. static int test_wolfSSL_CTX_add_session_ext_dtls13(void)
  8476. {
  8477. EXPECT_DECLS;
  8478. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8479. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8480. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8481. defined(HAVE_SESSION_TICKET) && \
  8482. !defined(TITAN_SESSION_CACHE) && \
  8483. !defined(HUGE_SESSION_CACHE) && \
  8484. !defined(BIG_SESSION_CACHE) && \
  8485. !defined(MEDIUM_SESSION_CACHE)
  8486. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  8487. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  8488. #ifdef WOLFSSL_DTLS13
  8489. struct test_add_session_ext_params param[1] = {
  8490. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" }
  8491. };
  8492. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8493. #endif
  8494. #endif
  8495. #endif
  8496. return EXPECT_RESULT();
  8497. }
  8498. static int test_wolfSSL_CTX_add_session_ext_tls12(void)
  8499. {
  8500. EXPECT_DECLS;
  8501. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8502. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8503. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8504. defined(HAVE_SESSION_TICKET) && \
  8505. !defined(TITAN_SESSION_CACHE) && \
  8506. !defined(HUGE_SESSION_CACHE) && \
  8507. !defined(BIG_SESSION_CACHE) && \
  8508. !defined(MEDIUM_SESSION_CACHE)
  8509. #ifndef WOLFSSL_NO_TLS12
  8510. struct test_add_session_ext_params param[1] = {
  8511. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" }
  8512. };
  8513. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8514. #endif
  8515. #endif
  8516. return EXPECT_RESULT();
  8517. }
  8518. static int test_wolfSSL_CTX_add_session_ext_dtls12(void)
  8519. {
  8520. EXPECT_DECLS;
  8521. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8522. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8523. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8524. defined(HAVE_SESSION_TICKET) && \
  8525. !defined(TITAN_SESSION_CACHE) && \
  8526. !defined(HUGE_SESSION_CACHE) && \
  8527. !defined(BIG_SESSION_CACHE) && \
  8528. !defined(MEDIUM_SESSION_CACHE)
  8529. #ifndef WOLFSSL_NO_TLS12
  8530. #ifdef WOLFSSL_DTLS
  8531. struct test_add_session_ext_params param[1] = {
  8532. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" }
  8533. };
  8534. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8535. #endif
  8536. #endif
  8537. #endif
  8538. return EXPECT_RESULT();
  8539. }
  8540. static int test_wolfSSL_CTX_add_session_ext_tls11(void)
  8541. {
  8542. EXPECT_DECLS;
  8543. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8544. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8545. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8546. defined(HAVE_SESSION_TICKET) && \
  8547. !defined(TITAN_SESSION_CACHE) && \
  8548. !defined(HUGE_SESSION_CACHE) && \
  8549. !defined(BIG_SESSION_CACHE) && \
  8550. !defined(MEDIUM_SESSION_CACHE)
  8551. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  8552. !defined(NO_DES3))
  8553. struct test_add_session_ext_params param[1] = {
  8554. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" }
  8555. };
  8556. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8557. #endif
  8558. #endif
  8559. return EXPECT_RESULT();
  8560. }
  8561. static int test_wolfSSL_CTX_add_session_ext_dtls1(void)
  8562. {
  8563. EXPECT_DECLS;
  8564. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8565. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8566. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8567. defined(HAVE_SESSION_TICKET) && \
  8568. !defined(TITAN_SESSION_CACHE) && \
  8569. !defined(HUGE_SESSION_CACHE) && \
  8570. !defined(BIG_SESSION_CACHE) && \
  8571. !defined(MEDIUM_SESSION_CACHE)
  8572. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  8573. !defined(NO_DES3))
  8574. #ifdef WOLFSSL_DTLS
  8575. struct test_add_session_ext_params param[1] = {
  8576. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" }
  8577. };
  8578. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8579. #endif
  8580. #endif
  8581. #endif
  8582. return EXPECT_RESULT();
  8583. }
  8584. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  8585. /* canned export of a session using older version 3 */
  8586. static unsigned char version_3[] = {
  8587. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  8588. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  8589. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  8590. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  8591. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  8592. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8593. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  8594. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  8595. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  8596. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8597. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  8598. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  8599. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8600. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  8601. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  8602. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  8603. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  8604. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  8605. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  8606. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  8607. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8608. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8609. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8610. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8611. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8612. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8613. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8614. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8615. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8616. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8617. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8618. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  8619. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  8620. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  8621. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  8622. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  8623. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  8624. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  8625. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  8626. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  8627. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  8628. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8629. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  8630. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  8631. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8632. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  8633. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  8634. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  8635. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  8636. 0x2E, 0x31, 0xED, 0x4F
  8637. };
  8638. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  8639. static int test_wolfSSL_dtls_export(void)
  8640. {
  8641. EXPECT_DECLS;
  8642. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  8643. tcp_ready ready;
  8644. func_args client_args;
  8645. func_args server_args;
  8646. THREAD_TYPE serverThread;
  8647. callback_functions server_cbf;
  8648. callback_functions client_cbf;
  8649. #ifdef WOLFSSL_TIRTOS
  8650. fdOpenSession(Task_self());
  8651. #endif
  8652. InitTcpReady(&ready);
  8653. #if defined(USE_WINDOWS_API)
  8654. /* use RNG to get random port if using windows */
  8655. ready.port = GetRandomPort();
  8656. #endif
  8657. /* set using dtls */
  8658. XMEMSET(&client_args, 0, sizeof(func_args));
  8659. XMEMSET(&server_args, 0, sizeof(func_args));
  8660. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8661. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  8662. server_cbf.method = wolfDTLSv1_2_server_method;
  8663. client_cbf.method = wolfDTLSv1_2_client_method;
  8664. server_args.callbacks = &server_cbf;
  8665. client_args.callbacks = &client_cbf;
  8666. server_args.signal = &ready;
  8667. client_args.signal = &ready;
  8668. start_thread(run_wolfssl_server, &server_args, &serverThread);
  8669. wait_tcp_ready(&server_args);
  8670. run_wolfssl_client(&client_args);
  8671. join_thread(serverThread);
  8672. ExpectTrue(client_args.return_code);
  8673. ExpectTrue(server_args.return_code);
  8674. FreeTcpReady(&ready);
  8675. #ifdef WOLFSSL_TIRTOS
  8676. fdOpenSession(Task_self());
  8677. #endif
  8678. if (EXPECT_SUCCESS()) {
  8679. SOCKET_T sockfd = 0;
  8680. WOLFSSL_CTX* ctx = NULL;
  8681. WOLFSSL* ssl = NULL;
  8682. char msg[64] = "hello wolfssl!";
  8683. char reply[1024];
  8684. int msgSz = (int)XSTRLEN(msg);
  8685. byte *session, *window;
  8686. unsigned int sessionSz = 0;
  8687. unsigned int windowSz = 0;
  8688. #ifndef TEST_IPV6
  8689. struct sockaddr_in peerAddr;
  8690. #else
  8691. struct sockaddr_in6 peerAddr;
  8692. #endif /* TEST_IPV6 */
  8693. int i;
  8694. /* Set ctx to DTLS 1.2 */
  8695. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  8696. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8697. /* test importing version 3 */
  8698. ExpectIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8699. /* test importing bad length and bad version */
  8700. version_3[2] += 1;
  8701. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8702. version_3[2] -= 1; version_3[1] = 0XA0;
  8703. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8704. wolfSSL_free(ssl);
  8705. wolfSSL_CTX_free(ctx);
  8706. /* check storing client state after connection and storing window only */
  8707. #ifdef WOLFSSL_TIRTOS
  8708. fdOpenSession(Task_self());
  8709. #endif
  8710. InitTcpReady(&ready);
  8711. #if defined(USE_WINDOWS_API)
  8712. /* use RNG to get random port if using windows */
  8713. ready.port = GetRandomPort();
  8714. #endif
  8715. /* set using dtls */
  8716. XMEMSET(&server_args, 0, sizeof(func_args));
  8717. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8718. server_cbf.method = wolfDTLSv1_2_server_method;
  8719. server_cbf.doUdp = 1;
  8720. server_args.callbacks = &server_cbf;
  8721. server_args.argc = 3; /* set loop_count to 3 */
  8722. server_args.signal = &ready;
  8723. start_thread(test_server_nofail, &server_args, &serverThread);
  8724. wait_tcp_ready(&server_args);
  8725. /* create and connect with client */
  8726. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  8727. ExpectIntEQ(WOLFSSL_SUCCESS,
  8728. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  8729. ExpectIntEQ(WOLFSSL_SUCCESS,
  8730. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  8731. ExpectIntEQ(WOLFSSL_SUCCESS,
  8732. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  8733. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  8734. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8735. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8736. /* store server information connected too */
  8737. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  8738. #ifndef TEST_IPV6
  8739. peerAddr.sin_family = AF_INET;
  8740. ExpectIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  8741. peerAddr.sin_port = XHTONS(server_args.signal->port);
  8742. #else
  8743. peerAddr.sin6_family = AF_INET6;
  8744. ExpectIntEQ(
  8745. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  8746. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  8747. #endif
  8748. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8749. WOLFSSL_SUCCESS);
  8750. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  8751. ExpectIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  8752. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8753. ExpectIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  8754. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8755. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8756. ExpectIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  8757. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8758. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8759. wolfSSL_free(ssl);
  8760. for (i = 1; EXPECT_SUCCESS() && i < server_args.argc; i++) {
  8761. /* restore state */
  8762. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8763. ExpectIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  8764. ExpectIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  8765. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8766. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8767. WOLFSSL_SUCCESS);
  8768. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8769. ExpectIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8770. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8771. wolfSSL_free(ssl);
  8772. }
  8773. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8774. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8775. wolfSSL_CTX_free(ctx);
  8776. fprintf(stderr, "done and waiting for server\n");
  8777. join_thread(serverThread);
  8778. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  8779. FreeTcpReady(&ready);
  8780. #ifdef WOLFSSL_TIRTOS
  8781. fdOpenSession(Task_self());
  8782. #endif
  8783. }
  8784. #endif
  8785. return EXPECT_RESULT();
  8786. }
  8787. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  8788. #ifdef WOLFSSL_TLS13
  8789. static const byte canned_client_tls13_session[] = {
  8790. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  8791. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8792. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  8793. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8794. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8795. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8796. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8797. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  8798. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8799. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8800. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8801. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  8802. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8803. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8804. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8805. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8806. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8807. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8808. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8809. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8810. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8811. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8812. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8813. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8814. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8815. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8816. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8817. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8818. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8819. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  8820. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  8821. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  8822. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8823. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8824. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8825. 0x00, 0x03
  8826. };
  8827. static const byte canned_server_tls13_session[] = {
  8828. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  8829. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8830. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  8831. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8832. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8833. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8834. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8835. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  8836. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8837. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8838. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8839. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  8840. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8841. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8842. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8843. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8844. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8845. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8846. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8847. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8848. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8849. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8850. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8851. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8852. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8853. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8854. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8855. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8856. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8857. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  8858. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  8859. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  8860. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8861. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8862. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8863. 0x00, 0x04
  8864. };
  8865. #endif /* WOLFSSL_TLS13 */
  8866. static const byte canned_client_session[] = {
  8867. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8868. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8869. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  8870. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8871. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8872. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8873. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8874. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  8875. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8876. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8877. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8878. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  8879. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  8880. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  8881. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  8882. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  8883. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  8884. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  8885. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  8886. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  8887. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  8888. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  8889. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  8890. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  8891. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  8892. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  8893. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  8894. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  8895. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  8896. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8897. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8898. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8899. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8900. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  8901. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  8902. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  8903. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  8904. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  8905. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  8906. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8907. 0x00, 0x03
  8908. };
  8909. static const byte canned_server_session[] = {
  8910. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8911. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8912. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  8913. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8914. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  8915. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8916. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8917. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  8918. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8919. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8920. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8921. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  8922. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  8923. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  8924. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  8925. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  8926. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  8927. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  8928. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  8929. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  8930. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  8931. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  8932. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  8933. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  8934. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  8935. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  8936. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  8937. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  8938. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  8939. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8940. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8941. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8942. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8943. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  8944. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  8945. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  8946. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  8947. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  8948. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  8949. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8950. 0x00, 0x04
  8951. };
  8952. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  8953. {
  8954. SOCKET_T sockfd = 0;
  8955. SOCKET_T clientfd = 0;
  8956. word16 port;
  8957. callback_functions* cbf;
  8958. WOLFSSL_CTX* ctx = 0;
  8959. WOLFSSL* ssl = 0;
  8960. char msg[] = "I hear you fa shizzle!";
  8961. char input[1024];
  8962. int idx;
  8963. #ifdef WOLFSSL_TIRTOS
  8964. fdOpenSession(Task_self());
  8965. #endif
  8966. ((func_args*)args)->return_code = TEST_FAIL;
  8967. cbf = ((func_args*)args)->callbacks;
  8968. #if defined(USE_WINDOWS_API)
  8969. port = ((func_args*)args)->signal->port;
  8970. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  8971. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  8972. /* Let tcp_listen assign port */
  8973. port = 0;
  8974. #else
  8975. /* Use default port */
  8976. port = wolfSSLPort;
  8977. #endif
  8978. /* do it here to detect failure */
  8979. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  8980. CloseSocket(sockfd);
  8981. {
  8982. WOLFSSL_METHOD* method = NULL;
  8983. if (cbf != NULL && cbf->method != NULL) {
  8984. method = cbf->method();
  8985. }
  8986. else {
  8987. method = wolfTLSv1_2_server_method();
  8988. }
  8989. ctx = wolfSSL_CTX_new(method);
  8990. }
  8991. if (ctx == NULL) {
  8992. goto done;
  8993. }
  8994. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  8995. /* call ctx setup callback */
  8996. if (cbf != NULL && cbf->ctx_ready != NULL) {
  8997. cbf->ctx_ready(ctx);
  8998. }
  8999. ssl = wolfSSL_new(ctx);
  9000. if (ssl == NULL) {
  9001. goto done;
  9002. }
  9003. wolfSSL_set_fd(ssl, clientfd);
  9004. /* call ssl setup callback */
  9005. if (cbf != NULL && cbf->ssl_ready != NULL) {
  9006. cbf->ssl_ready(ssl);
  9007. }
  9008. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  9009. if (idx > 0) {
  9010. input[idx] = '\0';
  9011. fprintf(stderr, "Client message export/import: %s\n", input);
  9012. }
  9013. else {
  9014. fprintf(stderr, "ret = %d error = %d\n", idx,
  9015. wolfSSL_get_error(ssl, idx));
  9016. goto done;
  9017. }
  9018. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  9019. /*err_sys("SSL_write failed");*/
  9020. WOLFSSL_RETURN_FROM_THREAD(0);
  9021. }
  9022. #ifdef WOLFSSL_TIRTOS
  9023. Task_yield();
  9024. #endif
  9025. ((func_args*)args)->return_code = TEST_SUCCESS;
  9026. done:
  9027. wolfSSL_shutdown(ssl);
  9028. wolfSSL_free(ssl);
  9029. wolfSSL_CTX_free(ctx);
  9030. CloseSocket(clientfd);
  9031. #ifdef WOLFSSL_TIRTOS
  9032. fdCloseSession(Task_self());
  9033. #endif
  9034. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  9035. && defined(HAVE_THREAD_LS)
  9036. wc_ecc_fp_free(); /* free per thread cache */
  9037. #endif
  9038. #if defined(HAVE_SESSION_TICKET) && \
  9039. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  9040. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  9041. OpenSSLTicketCleanup();
  9042. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  9043. TicketCleanup();
  9044. #endif
  9045. #endif
  9046. WOLFSSL_RETURN_FROM_THREAD(0);
  9047. }
  9048. static void load_tls12_canned_server(WOLFSSL* ssl)
  9049. {
  9050. int clientfd = wolfSSL_get_fd(ssl);
  9051. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  9052. sizeof(canned_server_session)), sizeof(canned_server_session));
  9053. wolfSSL_set_fd(ssl, clientfd);
  9054. }
  9055. #ifdef WOLFSSL_TLS13
  9056. static void load_tls13_canned_server(WOLFSSL* ssl)
  9057. {
  9058. int clientfd = wolfSSL_get_fd(ssl);
  9059. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  9060. sizeof(canned_server_tls13_session)),
  9061. sizeof(canned_server_tls13_session));
  9062. wolfSSL_set_fd(ssl, clientfd);
  9063. }
  9064. #endif
  9065. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  9066. static int test_wolfSSL_tls_export_run(int v)
  9067. {
  9068. EXPECT_DECLS;
  9069. SOCKET_T sockfd = 0;
  9070. WOLFSSL_CTX* ctx = 0;
  9071. WOLFSSL* ssl = 0;
  9072. char msg[64] = "hello wolfssl!";
  9073. char reply[1024];
  9074. word32 replySz;
  9075. int msgSz = (int)XSTRLEN(msg);
  9076. const byte* clientSession = NULL;
  9077. int clientSessionSz = 0;
  9078. tcp_ready ready;
  9079. func_args server_args;
  9080. THREAD_TYPE serverThread;
  9081. callback_functions server_cbf;
  9082. #ifdef WOLFSSL_TIRTOS
  9083. fdOpenSession(Task_self());
  9084. #endif
  9085. InitTcpReady(&ready);
  9086. #if defined(USE_WINDOWS_API)
  9087. /* use RNG to get random port if using windows */
  9088. ready.port = GetRandomPort();
  9089. #endif
  9090. XMEMSET(&server_args, 0, sizeof(func_args));
  9091. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  9092. switch (v) {
  9093. case WOLFSSL_TLSV1_2:
  9094. server_cbf.method = wolfTLSv1_2_server_method;
  9095. server_cbf.ssl_ready = load_tls12_canned_server;
  9096. /* setup the client side */
  9097. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  9098. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  9099. clientSession = canned_client_session;
  9100. clientSessionSz = sizeof(canned_client_session);
  9101. break;
  9102. #ifdef WOLFSSL_TLS13
  9103. case WOLFSSL_TLSV1_3:
  9104. server_cbf.method = wolfTLSv1_3_server_method;
  9105. server_cbf.ssl_ready = load_tls13_canned_server;
  9106. /* setup the client side */
  9107. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  9108. clientSession = canned_client_tls13_session;
  9109. clientSessionSz = sizeof(canned_client_tls13_session);
  9110. break;
  9111. #endif
  9112. }
  9113. server_args.callbacks = &server_cbf;
  9114. server_args.signal = &ready;
  9115. start_thread(tls_export_server, &server_args, &serverThread);
  9116. wait_tcp_ready(&server_args);
  9117. #ifdef WOLFSSL_TIRTOS
  9118. fdOpenSession(Task_self());
  9119. #endif
  9120. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9121. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  9122. ExpectIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  9123. clientSessionSz);
  9124. replySz = sizeof(reply);
  9125. ExpectIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  9126. #if !defined(NO_PSK) && defined(HAVE_ANON)
  9127. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  9128. ExpectIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  9129. #endif
  9130. wolfSSL_set_fd(ssl, sockfd);
  9131. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  9132. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  9133. wolfSSL_free(ssl);
  9134. wolfSSL_CTX_free(ctx);
  9135. CloseSocket(sockfd);
  9136. #ifdef WOLFSSL_TIRTOS
  9137. fdCloseSession(Task_self());
  9138. #endif
  9139. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  9140. && defined(HAVE_THREAD_LS)
  9141. wc_ecc_fp_free(); /* free per thread cache */
  9142. #endif
  9143. join_thread(serverThread);
  9144. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  9145. FreeTcpReady(&ready);
  9146. #ifdef WOLFSSL_TIRTOS
  9147. fdOpenSession(Task_self());
  9148. #endif
  9149. return EXPECT_RESULT();
  9150. }
  9151. #endif
  9152. static int test_wolfSSL_tls_export(void)
  9153. {
  9154. int res = TEST_SKIPPED;
  9155. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  9156. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  9157. #ifdef WOLFSSL_TLS13
  9158. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  9159. #endif
  9160. res = TEST_RES_CHECK(1);
  9161. #endif
  9162. return res;
  9163. }
  9164. /*----------------------------------------------------------------------------*
  9165. | TLS extensions tests
  9166. *----------------------------------------------------------------------------*/
  9167. #ifdef ENABLE_TLS_CALLBACK_TEST
  9168. /* Connection test runner - generic */
  9169. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  9170. callback_functions* server_callbacks)
  9171. {
  9172. tcp_ready ready;
  9173. func_args client_args;
  9174. func_args server_args;
  9175. THREAD_TYPE serverThread;
  9176. XMEMSET(&client_args, 0, sizeof(func_args));
  9177. XMEMSET(&server_args, 0, sizeof(func_args));
  9178. StartTCP();
  9179. client_args.callbacks = client_callbacks;
  9180. server_args.callbacks = server_callbacks;
  9181. #ifdef WOLFSSL_TIRTOS
  9182. fdOpenSession(Task_self());
  9183. #endif
  9184. /* RUN Server side */
  9185. InitTcpReady(&ready);
  9186. #if defined(USE_WINDOWS_API)
  9187. /* use RNG to get random port if using windows */
  9188. ready.port = GetRandomPort();
  9189. #endif
  9190. server_args.signal = &ready;
  9191. client_args.signal = &ready;
  9192. start_thread(run_wolfssl_server, &server_args, &serverThread);
  9193. wait_tcp_ready(&server_args);
  9194. /* RUN Client side */
  9195. run_wolfssl_client(&client_args);
  9196. join_thread(serverThread);
  9197. FreeTcpReady(&ready);
  9198. #ifdef WOLFSSL_TIRTOS
  9199. fdCloseSession(Task_self());
  9200. #endif
  9201. client_callbacks->return_code = client_args.return_code;
  9202. server_callbacks->return_code = server_args.return_code;
  9203. }
  9204. #endif /* ENABLE_TLS_CALLBACK_TEST */
  9205. #ifdef HAVE_SNI
  9206. static int test_wolfSSL_UseSNI_params(void)
  9207. {
  9208. EXPECT_DECLS;
  9209. #if !defined(NO_WOLFSSL_CLIENT)
  9210. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9211. WOLFSSL *ssl = wolfSSL_new(ctx);
  9212. ExpectNotNull(ctx);
  9213. ExpectNotNull(ssl);
  9214. /* invalid [ctx|ssl] */
  9215. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  9216. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  9217. /* invalid type */
  9218. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  9219. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  9220. /* invalid data */
  9221. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  9222. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  9223. /* success case */
  9224. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  9225. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  9226. wolfSSL_free(ssl);
  9227. wolfSSL_CTX_free(ctx);
  9228. #endif /* !NO_WOLFSSL_CLIENT */
  9229. return EXPECT_RESULT();
  9230. }
  9231. /* BEGIN of connection tests callbacks */
  9232. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9233. {
  9234. AssertIntEQ(WOLFSSL_SUCCESS,
  9235. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  9236. }
  9237. static void use_SNI_at_ssl(WOLFSSL* ssl)
  9238. {
  9239. AssertIntEQ(WOLFSSL_SUCCESS,
  9240. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  9241. }
  9242. static void different_SNI_at_ssl(WOLFSSL* ssl)
  9243. {
  9244. AssertIntEQ(WOLFSSL_SUCCESS,
  9245. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  9246. }
  9247. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  9248. {
  9249. use_SNI_at_ssl(ssl);
  9250. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9251. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  9252. }
  9253. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  9254. {
  9255. use_SNI_at_ssl(ssl);
  9256. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9257. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  9258. }
  9259. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9260. {
  9261. use_SNI_at_ctx(ctx);
  9262. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  9263. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9264. }
  9265. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  9266. {
  9267. use_SNI_at_ssl(ssl);
  9268. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9269. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9270. }
  9271. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9272. {
  9273. use_SNI_at_ctx(ctx);
  9274. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  9275. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9276. }
  9277. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  9278. {
  9279. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  9280. }
  9281. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  9282. {
  9283. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  9284. }
  9285. static void verify_SNI_no_matching(WOLFSSL* ssl)
  9286. {
  9287. byte type = WOLFSSL_SNI_HOST_NAME;
  9288. void* request = (void*) &type; /* to be overwritten */
  9289. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  9290. AssertNotNull(request);
  9291. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9292. AssertNull(request);
  9293. }
  9294. static void verify_SNI_real_matching(WOLFSSL* ssl)
  9295. {
  9296. byte type = WOLFSSL_SNI_HOST_NAME;
  9297. void* request = NULL;
  9298. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  9299. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9300. AssertNotNull(request);
  9301. AssertStrEQ("www.wolfssl.com", (char*)request);
  9302. }
  9303. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  9304. {
  9305. byte type = WOLFSSL_SNI_HOST_NAME;
  9306. void* request = NULL;
  9307. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  9308. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9309. AssertNotNull(request);
  9310. AssertStrEQ("ww2.wolfssl.com", (char*)request);
  9311. }
  9312. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  9313. {
  9314. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  9315. }
  9316. /* END of connection tests callbacks */
  9317. static int test_wolfSSL_UseSNI_connection(void)
  9318. {
  9319. int res = TEST_SKIPPED;
  9320. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  9321. callback_functions client_cb;
  9322. callback_functions server_cb;
  9323. size_t i;
  9324. #ifdef WOLFSSL_STATIC_MEMORY
  9325. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  9326. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  9327. #endif
  9328. struct {
  9329. method_provider client_meth;
  9330. method_provider server_meth;
  9331. #ifdef WOLFSSL_STATIC_MEMORY
  9332. wolfSSL_method_func client_meth_ex;
  9333. wolfSSL_method_func server_meth_ex;
  9334. #endif
  9335. } methods[] = {
  9336. #if defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_TLS13)
  9337. {wolfSSLv23_client_method, wolfSSLv23_server_method
  9338. #ifdef WOLFSSL_STATIC_MEMORY
  9339. ,wolfSSLv23_client_method_ex, wolfSSLv23_server_method_ex
  9340. #endif
  9341. },
  9342. #endif
  9343. #ifndef WOLFSSL_NO_TLS12
  9344. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method
  9345. #ifdef WOLFSSL_STATIC_MEMORY
  9346. ,wolfTLSv1_2_client_method_ex, wolfTLSv1_2_server_method_ex
  9347. #endif
  9348. },
  9349. #endif
  9350. #ifdef WOLFSSL_TLS13
  9351. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method
  9352. #ifdef WOLFSSL_STATIC_MEMORY
  9353. ,wolfTLSv1_3_client_method_ex, wolfTLSv1_3_server_method_ex
  9354. #endif
  9355. },
  9356. #endif
  9357. };
  9358. size_t methodsSz = sizeof(methods) / sizeof(*methods);
  9359. for (i = 0; i < methodsSz; i++) {
  9360. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9361. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9362. client_cb.method = methods[i].client_meth;
  9363. server_cb.method = methods[i].server_meth;
  9364. client_cb.devId = testDevId;
  9365. server_cb.devId = testDevId;
  9366. #ifdef WOLFSSL_STATIC_MEMORY
  9367. client_cb.method_ex = methods[i].client_meth_ex;
  9368. server_cb.method_ex = methods[i].server_meth_ex;
  9369. client_cb.mem = cliMem;
  9370. client_cb.memSz = (word32)sizeof(cliMem);
  9371. server_cb.mem = svrMem;
  9372. server_cb.memSz = (word32)sizeof(svrMem);;
  9373. #endif
  9374. /* success case at ctx */
  9375. fprintf(stderr, "\n\tsuccess case at ctx\n");
  9376. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9377. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  9378. test_wolfSSL_client_server(&client_cb, &server_cb);
  9379. /* success case at ssl */
  9380. fprintf(stderr, "\tsuccess case at ssl\n");
  9381. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  9382. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  9383. test_wolfSSL_client_server(&client_cb, &server_cb);
  9384. /* default mismatch behavior */
  9385. fprintf(stderr, "\tdefault mismatch behavior\n");
  9386. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9387. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  9388. test_wolfSSL_client_server(&client_cb, &server_cb);
  9389. /* continue on mismatch */
  9390. fprintf(stderr, "\tcontinue on mismatch\n");
  9391. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9392. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  9393. test_wolfSSL_client_server(&client_cb, &server_cb);
  9394. /* fake answer on mismatch */
  9395. fprintf(stderr, "\tfake answer on mismatch\n");
  9396. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9397. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  9398. test_wolfSSL_client_server(&client_cb, &server_cb);
  9399. /* sni abort - success */
  9400. fprintf(stderr, "\tsni abort - success\n");
  9401. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9402. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  9403. test_wolfSSL_client_server(&client_cb, &server_cb);
  9404. /* sni abort - abort when absent (ctx) */
  9405. fprintf(stderr, "\tsni abort - abort when absent (ctx)\n");
  9406. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9407. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  9408. test_wolfSSL_client_server(&client_cb, &server_cb);
  9409. /* sni abort - abort when absent (ssl) */
  9410. fprintf(stderr, "\tsni abort - abort when absent (ssl)\n");
  9411. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9412. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  9413. test_wolfSSL_client_server(&client_cb, &server_cb);
  9414. /* sni abort - success when overwritten */
  9415. fprintf(stderr, "\tsni abort - success when overwritten\n");
  9416. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9417. 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;
  9418. test_wolfSSL_client_server(&client_cb, &server_cb);
  9419. /* sni abort - success when allowing mismatches */
  9420. fprintf(stderr, "\tsni abort - success when allowing mismatches\n");
  9421. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9422. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  9423. test_wolfSSL_client_server(&client_cb, &server_cb);
  9424. }
  9425. res = TEST_RES_CHECK(1);
  9426. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9427. return res;
  9428. }
  9429. static int test_wolfSSL_SNI_GetFromBuffer(void)
  9430. {
  9431. EXPECT_DECLS;
  9432. byte buff[] = { /* www.paypal.com */
  9433. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  9434. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  9435. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  9436. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  9437. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  9438. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  9439. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  9440. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  9441. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  9442. };
  9443. byte buff2[] = { /* api.textmate.org */
  9444. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  9445. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  9446. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  9447. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  9448. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  9449. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  9450. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  9451. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  9452. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  9453. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  9454. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  9455. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  9456. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  9457. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  9458. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  9459. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  9460. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  9461. };
  9462. byte buff3[] = { /* no sni extension */
  9463. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  9464. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  9465. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  9466. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  9467. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  9468. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  9469. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  9470. };
  9471. byte buff4[] = { /* last extension has zero size */
  9472. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  9473. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  9474. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  9475. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  9476. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  9477. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  9478. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  9479. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  9480. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  9481. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  9482. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  9483. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  9484. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  9485. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  9486. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  9487. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  9488. 0x12, 0x00, 0x00
  9489. };
  9490. byte buff5[] = { /* SSL v2.0 client hello */
  9491. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  9492. /* dummy bytes below, just to pass size check */
  9493. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  9494. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  9495. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  9496. };
  9497. byte result[32] = {0};
  9498. word32 length = 32;
  9499. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  9500. 0, result, &length));
  9501. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  9502. 0, result, &length));
  9503. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  9504. 1, result, &length));
  9505. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9506. 0, result, &length));
  9507. buff[0] = 0x16;
  9508. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9509. 0, result, &length));
  9510. buff[1] = 0x03;
  9511. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  9512. sizeof(buff), 0, result, &length));
  9513. buff[2] = 0x03;
  9514. ExpectIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  9515. sizeof(buff), 0, result, &length));
  9516. buff[4] = 0x64;
  9517. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9518. 0, result, &length));
  9519. if (EXPECT_SUCCESS())
  9520. result[length] = 0;
  9521. ExpectStrEQ("www.paypal.com", (const char*) result);
  9522. length = 32;
  9523. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  9524. 0, result, &length));
  9525. if (EXPECT_SUCCESS())
  9526. result[length] = 0;
  9527. ExpectStrEQ("api.textmate.org", (const char*) result);
  9528. /* SSL v2.0 tests */
  9529. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  9530. sizeof(buff5), 0, result, &length));
  9531. buff5[2] = 0x02;
  9532. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9533. sizeof(buff5), 0, result, &length));
  9534. buff5[2] = 0x01; buff5[6] = 0x08;
  9535. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9536. sizeof(buff5), 0, result, &length));
  9537. buff5[6] = 0x09; buff5[8] = 0x01;
  9538. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9539. sizeof(buff5), 0, result, &length));
  9540. return EXPECT_RESULT();
  9541. }
  9542. #endif /* HAVE_SNI */
  9543. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  9544. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) && \
  9545. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  9546. /* Dummy peer functions to satisfy the exporter/importer */
  9547. static int test_wolfSSL_dtls_export_peers_get_peer(WOLFSSL* ssl, char* ip,
  9548. int* ipSz, unsigned short* port, int* fam)
  9549. {
  9550. (void)ssl;
  9551. ip[0] = -1;
  9552. *ipSz = 1;
  9553. *port = 1;
  9554. *fam = 2;
  9555. return 1;
  9556. }
  9557. static int test_wolfSSL_dtls_export_peers_set_peer(WOLFSSL* ssl, char* ip,
  9558. int ipSz, unsigned short port, int fam)
  9559. {
  9560. (void)ssl;
  9561. if (ip[0] != -1 || ipSz != 1 || port != 1 || fam != 2)
  9562. return 0;
  9563. return 1;
  9564. }
  9565. static int test_wolfSSL_dtls_export_peers_on_handshake(WOLFSSL_CTX **ctx,
  9566. WOLFSSL **ssl)
  9567. {
  9568. EXPECT_DECLS;
  9569. unsigned char* sessionBuf = NULL;
  9570. unsigned int sessionSz = 0;
  9571. void* ioWriteCtx = wolfSSL_GetIOWriteCtx(*ssl);
  9572. void* ioReadCtx = wolfSSL_GetIOReadCtx(*ssl);
  9573. wolfSSL_CTX_SetIOGetPeer(*ctx, test_wolfSSL_dtls_export_peers_get_peer);
  9574. wolfSSL_CTX_SetIOSetPeer(*ctx, test_wolfSSL_dtls_export_peers_set_peer);
  9575. ExpectIntGE(wolfSSL_dtls_export(*ssl, NULL, &sessionSz), 0);
  9576. ExpectNotNull(sessionBuf =
  9577. (unsigned char*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  9578. ExpectIntGE(wolfSSL_dtls_export(*ssl, sessionBuf, &sessionSz), 0);
  9579. wolfSSL_free(*ssl);
  9580. *ssl = NULL;
  9581. ExpectNotNull(*ssl = wolfSSL_new(*ctx));
  9582. ExpectIntGE(wolfSSL_dtls_import(*ssl, sessionBuf, sessionSz), 0);
  9583. wolfSSL_SetIOWriteCtx(*ssl, ioWriteCtx);
  9584. wolfSSL_SetIOReadCtx(*ssl, ioReadCtx);
  9585. XFREE(sessionBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9586. return EXPECT_RESULT();
  9587. }
  9588. #endif
  9589. static int test_wolfSSL_dtls_export_peers(void)
  9590. {
  9591. EXPECT_DECLS;
  9592. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) && \
  9593. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  9594. test_ssl_cbf client_cbf;
  9595. test_ssl_cbf server_cbf;
  9596. size_t i, j;
  9597. struct test_params {
  9598. method_provider client_meth;
  9599. method_provider server_meth;
  9600. const char* dtls_version;
  9601. } params[] = {
  9602. #ifndef NO_OLD_TLS
  9603. {wolfDTLSv1_client_method, wolfDTLSv1_server_method, "1.0"},
  9604. #endif
  9605. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "1.2"},
  9606. /* TODO DTLS 1.3 exporting not supported
  9607. #ifdef WOLFSSL_DTLS13
  9608. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "1.3"},
  9609. #endif
  9610. */
  9611. };
  9612. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  9613. for (j = 0; j <= 0b11; j++) {
  9614. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  9615. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  9616. printf("\n\tTesting DTLS %s connection;", params[i].dtls_version);
  9617. client_cbf.method = params[i].client_meth;
  9618. server_cbf.method = params[i].server_meth;
  9619. if (j & 0b01) {
  9620. client_cbf.on_handshake =
  9621. test_wolfSSL_dtls_export_peers_on_handshake;
  9622. printf(" With client export;");
  9623. }
  9624. if (j & 0b10) {
  9625. server_cbf.on_handshake =
  9626. test_wolfSSL_dtls_export_peers_on_handshake;
  9627. printf(" With server export;");
  9628. }
  9629. printf("\n");
  9630. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  9631. &server_cbf, NULL), TEST_SUCCESS);
  9632. if (!EXPECT_SUCCESS())
  9633. break;
  9634. }
  9635. }
  9636. #endif
  9637. return EXPECT_RESULT();
  9638. }
  9639. static int test_wolfSSL_UseTrustedCA(void)
  9640. {
  9641. EXPECT_DECLS;
  9642. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  9643. && !defined(NO_RSA)
  9644. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9645. WOLFSSL_CTX *ctx = NULL;
  9646. WOLFSSL *ssl = NULL;
  9647. byte id[20];
  9648. #ifndef NO_WOLFSSL_SERVER
  9649. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  9650. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9651. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9652. #else
  9653. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  9654. #endif
  9655. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  9656. XMEMSET(id, 0, sizeof(id));
  9657. /* error cases */
  9658. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  9659. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9660. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  9661. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9662. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  9663. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9664. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  9665. #ifdef NO_SHA
  9666. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9667. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  9668. #endif
  9669. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9670. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  9671. /* success cases */
  9672. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9673. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  9674. #ifndef NO_SHA
  9675. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9676. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  9677. #endif
  9678. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9679. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  9680. wolfSSL_free(ssl);
  9681. wolfSSL_CTX_free(ctx);
  9682. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9683. #endif /* HAVE_TRUSTED_CA */
  9684. return EXPECT_RESULT();
  9685. }
  9686. static int test_wolfSSL_UseMaxFragment(void)
  9687. {
  9688. EXPECT_DECLS;
  9689. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  9690. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  9691. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9692. #ifndef NO_WOLFSSL_SERVER
  9693. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  9694. #else
  9695. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9696. #endif
  9697. WOLFSSL *ssl = NULL;
  9698. #ifdef OPENSSL_EXTRA
  9699. int (*UseMaxFragment)(SSL *s, uint8_t mode);
  9700. int (*CTX_UseMaxFragment)(SSL_CTX *c, uint8_t mode);
  9701. #else
  9702. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  9703. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  9704. #endif
  9705. #ifndef NO_WOLFSSL_SERVER
  9706. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9707. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9708. #endif
  9709. ExpectNotNull(ctx);
  9710. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9711. #ifdef OPENSSL_EXTRA
  9712. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  9713. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  9714. #else
  9715. UseMaxFragment = wolfSSL_UseMaxFragment;
  9716. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  9717. #endif
  9718. /* error cases */
  9719. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  9720. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  9721. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  9722. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  9723. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  9724. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  9725. /* success case */
  9726. #ifdef OPENSSL_EXTRA
  9727. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  9728. #else
  9729. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  9730. #endif
  9731. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  9732. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  9733. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  9734. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  9735. #ifdef OPENSSL_EXTRA
  9736. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  9737. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  9738. #else
  9739. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  9740. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  9741. #endif
  9742. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  9743. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  9744. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  9745. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  9746. #ifdef OPENSSL_EXTRA
  9747. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  9748. #else
  9749. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  9750. #endif
  9751. wolfSSL_free(ssl);
  9752. wolfSSL_CTX_free(ctx);
  9753. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9754. #endif
  9755. return EXPECT_RESULT();
  9756. }
  9757. static int test_wolfSSL_UseTruncatedHMAC(void)
  9758. {
  9759. EXPECT_DECLS;
  9760. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  9761. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  9762. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9763. #ifndef NO_WOLFSSL_SERVER
  9764. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  9765. #else
  9766. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9767. #endif
  9768. WOLFSSL *ssl = NULL;
  9769. ExpectNotNull(ctx);
  9770. #ifndef NO_WOLFSSL_SERVER
  9771. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9772. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9773. #endif
  9774. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9775. /* error cases */
  9776. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  9777. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  9778. /* success case */
  9779. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  9780. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  9781. wolfSSL_free(ssl);
  9782. wolfSSL_CTX_free(ctx);
  9783. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9784. #endif
  9785. return EXPECT_RESULT();
  9786. }
  9787. static int test_wolfSSL_UseSupportedCurve(void)
  9788. {
  9789. EXPECT_DECLS;
  9790. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && \
  9791. !defined(NO_TLS)
  9792. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9793. WOLFSSL *ssl = wolfSSL_new(ctx);
  9794. ExpectNotNull(ctx);
  9795. ExpectNotNull(ssl);
  9796. /* error cases */
  9797. ExpectIntNE(WOLFSSL_SUCCESS,
  9798. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  9799. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  9800. ExpectIntNE(WOLFSSL_SUCCESS,
  9801. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  9802. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  9803. /* success case */
  9804. ExpectIntEQ(WOLFSSL_SUCCESS,
  9805. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  9806. ExpectIntEQ(WOLFSSL_SUCCESS,
  9807. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  9808. wolfSSL_free(ssl);
  9809. wolfSSL_CTX_free(ctx);
  9810. #endif
  9811. return EXPECT_RESULT();
  9812. }
  9813. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  9814. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  9815. {
  9816. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  9817. }
  9818. static void use_ALPN_all(WOLFSSL* ssl)
  9819. {
  9820. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9821. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9822. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9823. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9824. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9825. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9826. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9827. }
  9828. static void use_ALPN_all_continue(WOLFSSL* ssl)
  9829. {
  9830. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9831. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9832. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9833. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9834. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9835. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9836. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9837. }
  9838. static void use_ALPN_one(WOLFSSL* ssl)
  9839. {
  9840. /* spdy/2 */
  9841. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9842. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9843. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9844. }
  9845. static void use_ALPN_unknown(WOLFSSL* ssl)
  9846. {
  9847. /* http/2.0 */
  9848. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9849. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9850. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9851. }
  9852. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  9853. {
  9854. /* http/2.0 */
  9855. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9856. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9857. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9858. }
  9859. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  9860. {
  9861. /* spdy/3 */
  9862. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9863. char *proto = NULL;
  9864. word16 protoSz = 0;
  9865. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9866. /* check value */
  9867. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  9868. if (proto) {
  9869. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  9870. }
  9871. }
  9872. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  9873. {
  9874. char *proto = NULL;
  9875. word16 protoSz = 0;
  9876. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  9877. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9878. /* check value */
  9879. AssertIntEQ(1, (0 == protoSz));
  9880. AssertIntEQ(1, (NULL == proto));
  9881. }
  9882. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  9883. {
  9884. /* http/1.1 */
  9885. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9886. char *proto;
  9887. word16 protoSz = 0;
  9888. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9889. /* check value */
  9890. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9891. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9892. }
  9893. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  9894. {
  9895. /* spdy/2 */
  9896. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9897. char *proto;
  9898. word16 protoSz = 0;
  9899. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9900. /* check value */
  9901. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9902. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9903. }
  9904. static void verify_ALPN_client_list(WOLFSSL* ssl)
  9905. {
  9906. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9907. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9908. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9909. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9910. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9911. char *clist = NULL;
  9912. word16 clistSz = 0;
  9913. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  9914. &clistSz));
  9915. /* check value */
  9916. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  9917. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  9918. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  9919. }
  9920. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  9921. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)
  9922. /* ALPN select callback, success with spdy/2 */
  9923. static int select_ALPN_spdy2(WOLFSSL *ssl, const unsigned char **out,
  9924. unsigned char *outlen, const unsigned char *in,
  9925. unsigned int inlen, void *arg)
  9926. {
  9927. /* spdy/2 */
  9928. const char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9929. (void)ssl;
  9930. (void)arg;
  9931. /* adding +1 since LEN byte comes first */
  9932. if (inlen < sizeof(proto) + 1) {
  9933. return SSL_TLSEXT_ERR_ALERT_FATAL;
  9934. }
  9935. if (XMEMCMP(in + 1, proto, sizeof(proto)) == 0) {
  9936. *out = in + 1;
  9937. *outlen = (unsigned char)sizeof(proto);
  9938. return SSL_TLSEXT_ERR_OK;
  9939. }
  9940. return SSL_TLSEXT_ERR_ALERT_FATAL;
  9941. }
  9942. /* ALPN select callback, force failure */
  9943. static int select_ALPN_failure(WOLFSSL *ssl, const unsigned char **out,
  9944. unsigned char *outlen, const unsigned char *in,
  9945. unsigned int inlen, void *arg)
  9946. {
  9947. (void)ssl;
  9948. (void)out;
  9949. (void)outlen;
  9950. (void)in;
  9951. (void)inlen;
  9952. (void)arg;
  9953. return SSL_TLSEXT_ERR_ALERT_FATAL;
  9954. }
  9955. static void use_ALPN_spdy2_callback(WOLFSSL* ssl)
  9956. {
  9957. wolfSSL_set_alpn_select_cb(ssl, select_ALPN_spdy2, NULL);
  9958. }
  9959. static void use_ALPN_failure_callback(WOLFSSL* ssl)
  9960. {
  9961. wolfSSL_set_alpn_select_cb(ssl, select_ALPN_failure, NULL);
  9962. }
  9963. #endif /* OPENSSL_ALL | NGINX | HAPROXY | LIGHTY | QUIC */
  9964. static int test_wolfSSL_UseALPN_connection(void)
  9965. {
  9966. int res = TEST_SKIPPED;
  9967. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  9968. callback_functions client_cb;
  9969. callback_functions server_cb;
  9970. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9971. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9972. client_cb.method = wolfSSLv23_client_method;
  9973. server_cb.method = wolfSSLv23_server_method;
  9974. client_cb.devId = testDevId;
  9975. server_cb.devId = testDevId;
  9976. /* success case same list */
  9977. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9978. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  9979. test_wolfSSL_client_server(&client_cb, &server_cb);
  9980. /* success case only one for server */
  9981. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9982. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  9983. test_wolfSSL_client_server(&client_cb, &server_cb);
  9984. /* success case only one for client */
  9985. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  9986. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  9987. test_wolfSSL_client_server(&client_cb, &server_cb);
  9988. /* success case none for client */
  9989. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9990. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  9991. test_wolfSSL_client_server(&client_cb, &server_cb);
  9992. /* success case mismatch behavior but option 'continue' set */
  9993. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  9994. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  9995. test_wolfSSL_client_server(&client_cb, &server_cb);
  9996. /* success case read protocol send by client */
  9997. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  9998. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  9999. test_wolfSSL_client_server(&client_cb, &server_cb);
  10000. /* mismatch behavior with same list
  10001. * the first and only this one must be taken */
  10002. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10003. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  10004. test_wolfSSL_client_server(&client_cb, &server_cb);
  10005. /* default mismatch behavior */
  10006. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10007. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  10008. test_wolfSSL_client_server(&client_cb, &server_cb);
  10009. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  10010. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)
  10011. /* WOLFSSL-level ALPN select callback tests */
  10012. /* Callback: success (one protocol, spdy/2) */
  10013. client_cb.ctx_ready = NULL;
  10014. client_cb.ssl_ready = use_ALPN_one;
  10015. client_cb.on_result = verify_ALPN_matching_spdy2;
  10016. server_cb.ctx_ready = NULL;
  10017. server_cb.ssl_ready = use_ALPN_spdy2_callback;
  10018. server_cb.on_result = verify_ALPN_matching_spdy2;
  10019. test_wolfSSL_client_server(&client_cb, &server_cb);
  10020. /* Callback: failure (one client protocol, spdy/2) */
  10021. client_cb.ctx_ready = NULL;
  10022. client_cb.ssl_ready = use_ALPN_one;
  10023. client_cb.on_result = NULL;
  10024. server_cb.ctx_ready = NULL;
  10025. server_cb.ssl_ready = use_ALPN_failure_callback;
  10026. server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  10027. test_wolfSSL_client_server(&client_cb, &server_cb);
  10028. #endif /* OPENSSL_ALL | NGINX | HAPROXY | LIGHTY */
  10029. res = TEST_RES_CHECK(1);
  10030. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  10031. return res;
  10032. }
  10033. static int test_wolfSSL_UseALPN_params(void)
  10034. {
  10035. EXPECT_DECLS;
  10036. #ifndef NO_WOLFSSL_CLIENT
  10037. /* "http/1.1" */
  10038. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  10039. /* "spdy/1" */
  10040. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  10041. /* "spdy/2" */
  10042. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  10043. /* "spdy/3" */
  10044. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10045. char buff[256];
  10046. word32 idx;
  10047. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10048. WOLFSSL *ssl = wolfSSL_new(ctx);
  10049. ExpectNotNull(ctx);
  10050. ExpectNotNull(ssl);
  10051. /* error cases */
  10052. ExpectIntNE(WOLFSSL_SUCCESS,
  10053. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  10054. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10055. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  10056. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10057. /* success case */
  10058. /* http1 only */
  10059. ExpectIntEQ(WOLFSSL_SUCCESS,
  10060. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  10061. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10062. /* http1, spdy1 */
  10063. XMEMCPY(buff, http1, sizeof(http1));
  10064. idx = sizeof(http1);
  10065. buff[idx++] = ',';
  10066. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10067. idx += sizeof(spdy1);
  10068. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10069. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10070. /* http1, spdy2, spdy1 */
  10071. XMEMCPY(buff, http1, sizeof(http1));
  10072. idx = sizeof(http1);
  10073. buff[idx++] = ',';
  10074. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  10075. idx += sizeof(spdy2);
  10076. buff[idx++] = ',';
  10077. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10078. idx += sizeof(spdy1);
  10079. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10080. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10081. /* spdy3, http1, spdy2, spdy1 */
  10082. XMEMCPY(buff, spdy3, sizeof(spdy3));
  10083. idx = sizeof(spdy3);
  10084. buff[idx++] = ',';
  10085. XMEMCPY(buff+idx, http1, sizeof(http1));
  10086. idx += sizeof(http1);
  10087. buff[idx++] = ',';
  10088. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  10089. idx += sizeof(spdy2);
  10090. buff[idx++] = ',';
  10091. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10092. idx += sizeof(spdy1);
  10093. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10094. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  10095. wolfSSL_free(ssl);
  10096. wolfSSL_CTX_free(ctx);
  10097. #endif
  10098. return EXPECT_RESULT();
  10099. }
  10100. #endif /* HAVE_ALPN */
  10101. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  10102. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  10103. {
  10104. unsigned char p[] = {
  10105. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  10106. 6, 's', 'p', 'd', 'y', '/', '2',
  10107. 6, 's', 'p', 'd', 'y', '/', '1',
  10108. };
  10109. unsigned char p_len = sizeof(p);
  10110. int ret;
  10111. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  10112. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  10113. AssertIntEQ(ret, 0);
  10114. #else
  10115. AssertIntEQ(ret, SSL_SUCCESS);
  10116. #endif
  10117. }
  10118. static void set_alpn_protos(SSL* ssl)
  10119. {
  10120. unsigned char p[] = {
  10121. 6, 's', 'p', 'd', 'y', '/', '3',
  10122. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  10123. 6, 's', 'p', 'd', 'y', '/', '2',
  10124. 6, 's', 'p', 'd', 'y', '/', '1',
  10125. };
  10126. unsigned char p_len = sizeof(p);
  10127. int ret;
  10128. ret = SSL_set_alpn_protos(ssl, p, p_len);
  10129. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  10130. AssertIntEQ(ret, 0);
  10131. #else
  10132. AssertIntEQ(ret, SSL_SUCCESS);
  10133. #endif
  10134. }
  10135. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  10136. {
  10137. /* "spdy/3" */
  10138. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10139. const unsigned char *proto;
  10140. unsigned int protoSz = 0;
  10141. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  10142. /* check value */
  10143. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  10144. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  10145. }
  10146. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  10147. {
  10148. /* "http/1.1" */
  10149. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  10150. const unsigned char *proto;
  10151. unsigned int protoSz = 0;
  10152. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  10153. /* check value */
  10154. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  10155. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  10156. }
  10157. static int test_wolfSSL_set_alpn_protos(void)
  10158. {
  10159. int res = TEST_SKIPPED;
  10160. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  10161. callback_functions client_cb;
  10162. callback_functions server_cb;
  10163. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  10164. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  10165. client_cb.method = wolfSSLv23_client_method;
  10166. server_cb.method = wolfSSLv23_server_method;
  10167. client_cb.devId = testDevId;
  10168. server_cb.devId = testDevId;
  10169. /* use CTX_alpn_protos */
  10170. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  10171. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  10172. test_wolfSSL_client_server(&client_cb, &server_cb);
  10173. /* use set_alpn_protos */
  10174. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  10175. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  10176. test_wolfSSL_client_server(&client_cb, &server_cb);
  10177. res = TEST_SUCCESS;
  10178. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  10179. return res;
  10180. }
  10181. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  10182. static int test_wolfSSL_DisableExtendedMasterSecret(void)
  10183. {
  10184. EXPECT_DECLS;
  10185. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  10186. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10187. WOLFSSL *ssl = wolfSSL_new(ctx);
  10188. ExpectNotNull(ctx);
  10189. ExpectNotNull(ssl);
  10190. /* error cases */
  10191. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  10192. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  10193. /* success cases */
  10194. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  10195. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  10196. wolfSSL_free(ssl);
  10197. wolfSSL_CTX_free(ctx);
  10198. #endif
  10199. return EXPECT_RESULT();
  10200. }
  10201. static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  10202. {
  10203. EXPECT_DECLS;
  10204. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  10205. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10206. WOLFSSL *ssl = wolfSSL_new(ctx);
  10207. ExpectNotNull(ctx);
  10208. ExpectNotNull(ssl);
  10209. /* error cases */
  10210. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  10211. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  10212. /* success cases */
  10213. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  10214. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  10215. wolfSSL_free(ssl);
  10216. wolfSSL_CTX_free(ctx);
  10217. #endif
  10218. return EXPECT_RESULT();
  10219. }
  10220. /* Test reconnecting with a different ciphersuite after a renegotiation. */
  10221. static int test_wolfSSL_SCR_Reconnect(void)
  10222. {
  10223. EXPECT_DECLS;
  10224. #if defined(HAVE_SECURE_RENEGOTIATION) && \
  10225. defined(BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) && \
  10226. defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) && \
  10227. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  10228. struct test_memio_ctx test_ctx;
  10229. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  10230. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  10231. byte data;
  10232. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  10233. test_ctx.c_ciphers = "ECDHE-RSA-AES256-GCM-SHA384";
  10234. test_ctx.s_ciphers =
  10235. "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305";
  10236. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  10237. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  10238. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_c));
  10239. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_s));
  10240. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_c));
  10241. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_s));
  10242. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10243. /* WOLFSSL_FATAL_ERROR since it will block */
  10244. ExpectIntEQ(wolfSSL_Rehandshake(ssl_s), WOLFSSL_FATAL_ERROR);
  10245. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  10246. WOLFSSL_ERROR_WANT_READ);
  10247. ExpectIntEQ(wolfSSL_read(ssl_c, &data, 1), WOLFSSL_FATAL_ERROR);
  10248. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  10249. WOLFSSL_ERROR_WANT_READ);
  10250. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10251. wolfSSL_free(ssl_c);
  10252. ssl_c = NULL;
  10253. wolfSSL_free(ssl_s);
  10254. ssl_s = NULL;
  10255. wolfSSL_CTX_free(ctx_c);
  10256. ctx_c = NULL;
  10257. test_ctx.c_ciphers = "ECDHE-RSA-CHACHA20-POLY1305";
  10258. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  10259. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  10260. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10261. wolfSSL_free(ssl_s);
  10262. wolfSSL_free(ssl_c);
  10263. wolfSSL_CTX_free(ctx_s);
  10264. wolfSSL_CTX_free(ctx_c);
  10265. #endif
  10266. return EXPECT_RESULT();
  10267. }
  10268. #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_SERVER) && \
  10269. (!defined(NO_RSA) || defined(HAVE_ECC))
  10270. /* Called when writing. */
  10271. static int DummySend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  10272. {
  10273. (void)ssl;
  10274. (void)buf;
  10275. (void)sz;
  10276. (void)ctx;
  10277. /* Force error return from wolfSSL_accept_TLSv13(). */
  10278. return WANT_WRITE;
  10279. }
  10280. /* Called when reading. */
  10281. static int BufferInfoRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  10282. {
  10283. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  10284. int len = (int)msg->length;
  10285. (void)ssl;
  10286. (void)sz;
  10287. /* Pass back as much of message as will fit in buffer. */
  10288. if (len > sz)
  10289. len = sz;
  10290. XMEMCPY(buf, msg->buffer, len);
  10291. /* Move over returned data. */
  10292. msg->buffer += len;
  10293. msg->length -= len;
  10294. /* Amount actually copied. */
  10295. return len;
  10296. }
  10297. #endif
  10298. /* Test the detection of duplicate known TLS extensions.
  10299. * Specifically in a ClientHello.
  10300. */
  10301. static int test_tls_ext_duplicate(void)
  10302. {
  10303. EXPECT_DECLS;
  10304. #if !defined(NO_WOLFSSL_SERVER) && (!defined(NO_RSA) || defined(HAVE_ECC)) && \
  10305. !defined(NO_FILESYSTEM)
  10306. const unsigned char clientHelloDupTlsExt[] = {
  10307. 0x16, 0x03, 0x03, 0x00, 0x6a, 0x01, 0x00, 0x00,
  10308. 0x66, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  10309. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  10310. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  10311. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  10312. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  10313. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  10314. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  10315. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  10316. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x13, 0x01,
  10317. 0x00, 0x9e, 0x01, 0x00,
  10318. /* Extensions - duplicate signature algorithms. */
  10319. 0x00, 0x19, 0x00, 0x0d,
  10320. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01, 0x00, 0x0d,
  10321. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01,
  10322. /* Supported Versions extension for TLS 1.3. */
  10323. 0x00, 0x2b,
  10324. 0x00, 0x05, 0x04, 0x03, 0x04, 0x03, 0x03
  10325. };
  10326. WOLFSSL_BUFFER_INFO msg;
  10327. const char* testCertFile;
  10328. const char* testKeyFile;
  10329. WOLFSSL_CTX *ctx = NULL;
  10330. WOLFSSL *ssl = NULL;
  10331. #ifndef NO_RSA
  10332. testCertFile = svrCertFile;
  10333. testKeyFile = svrKeyFile;
  10334. #elif defined(HAVE_ECC)
  10335. testCertFile = eccCertFile;
  10336. testKeyFile = eccKeyFile;
  10337. #endif
  10338. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10339. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  10340. WOLFSSL_FILETYPE_PEM));
  10341. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  10342. WOLFSSL_FILETYPE_PEM));
  10343. /* Read from 'msg'. */
  10344. wolfSSL_SetIORecv(ctx, BufferInfoRecv);
  10345. /* No where to send to - dummy sender. */
  10346. wolfSSL_SetIOSend(ctx, DummySend);
  10347. ssl = wolfSSL_new(ctx);
  10348. ExpectNotNull(ssl);
  10349. msg.buffer = (unsigned char*)clientHelloDupTlsExt;
  10350. msg.length = (unsigned int)sizeof(clientHelloDupTlsExt);
  10351. wolfSSL_SetIOReadCtx(ssl, &msg);
  10352. ExpectIntNE(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  10353. /* can return duplicate ext error or socket error if the peer closed down
  10354. * while sending alert */
  10355. if (wolfSSL_get_error(ssl, 0) != SOCKET_ERROR_E) {
  10356. ExpectIntEQ(wolfSSL_get_error(ssl, 0), DUPLICATE_TLS_EXT_E);
  10357. }
  10358. wolfSSL_free(ssl);
  10359. wolfSSL_CTX_free(ctx);
  10360. #endif
  10361. return EXPECT_RESULT();
  10362. }
  10363. /*----------------------------------------------------------------------------*
  10364. | X509 Tests
  10365. *----------------------------------------------------------------------------*/
  10366. static int test_wolfSSL_X509_NAME_get_entry(void)
  10367. {
  10368. EXPECT_DECLS;
  10369. #if !defined(NO_CERTS) && !defined(NO_RSA)
  10370. #if defined(OPENSSL_ALL) || \
  10371. (defined(OPENSSL_EXTRA) && \
  10372. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  10373. /* use openssl like name to test mapping */
  10374. X509_NAME_ENTRY* ne = NULL;
  10375. X509_NAME* name = NULL;
  10376. X509* x509 = NULL;
  10377. #ifndef NO_FILESYSTEM
  10378. ASN1_STRING* asn = NULL;
  10379. char* subCN = NULL;
  10380. #endif
  10381. int idx = 0;
  10382. ASN1_OBJECT *object = NULL;
  10383. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  10384. defined(WOLFSSL_NGINX)
  10385. #ifndef NO_BIO
  10386. BIO* bio = NULL;
  10387. #endif
  10388. #endif
  10389. #ifndef NO_FILESYSTEM
  10390. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10391. WOLFSSL_FILETYPE_PEM));
  10392. ExpectNotNull(name = X509_get_subject_name(x509));
  10393. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  10394. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  10395. ExpectNotNull(asn = X509_NAME_ENTRY_get_data(ne));
  10396. ExpectNotNull(subCN = (char*)ASN1_STRING_data(asn));
  10397. wolfSSL_FreeX509(x509);
  10398. x509 = NULL;
  10399. #endif
  10400. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10401. WOLFSSL_FILETYPE_PEM));
  10402. ExpectNotNull(name = X509_get_subject_name(x509));
  10403. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  10404. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  10405. defined(WOLFSSL_NGINX)
  10406. #ifndef NO_BIO
  10407. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10408. ExpectIntEQ(X509_NAME_print_ex(bio, name, 4,
  10409. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  10410. ExpectIntEQ(X509_NAME_print_ex_fp(stderr, name, 4,
  10411. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  10412. BIO_free(bio);
  10413. #endif
  10414. #endif
  10415. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  10416. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  10417. wolfSSL_FreeX509(x509);
  10418. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  10419. #endif /* !NO_CERTS && !NO_RSA */
  10420. return EXPECT_RESULT();
  10421. }
  10422. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  10423. static int test_wolfSSL_PKCS12(void)
  10424. {
  10425. EXPECT_DECLS;
  10426. /* .p12 file is encrypted with DES3 */
  10427. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  10428. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  10429. * Password Key
  10430. */
  10431. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  10432. !defined(NO_STDIO_FILESYSTEM) && \
  10433. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  10434. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  10435. byte buf[6000];
  10436. char file[] = "./certs/test-servercert.p12";
  10437. char order[] = "./certs/ecc-rsa-server.p12";
  10438. #ifdef WC_RC2
  10439. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  10440. #endif
  10441. char pass[] = "a password";
  10442. const char goodPsw[] = "wolfSSL test";
  10443. const char badPsw[] = "bad";
  10444. #ifdef HAVE_ECC
  10445. WOLFSSL_X509_NAME *subject = NULL;
  10446. WOLFSSL_X509 *x509 = NULL;
  10447. #endif
  10448. XFILE f = XBADFILE;
  10449. int bytes = 0, ret = 0, goodPswLen = 0, badPswLen = 0;
  10450. WOLFSSL_BIO *bio = NULL;
  10451. WOLFSSL_EVP_PKEY *pkey = NULL;
  10452. WC_PKCS12 *pkcs12 = NULL;
  10453. WC_PKCS12 *pkcs12_2 = NULL;
  10454. WOLFSSL_X509 *cert = NULL;
  10455. WOLFSSL_X509 *tmp = NULL;
  10456. WOLF_STACK_OF(WOLFSSL_X509) *ca = NULL;
  10457. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10458. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10459. WOLFSSL_CTX *ctx = NULL;
  10460. WOLFSSL *ssl = NULL;
  10461. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  10462. #endif
  10463. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10464. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10465. if (f != XBADFILE) {
  10466. XFCLOSE(f);
  10467. f = XBADFILE;
  10468. }
  10469. goodPswLen = (int)XSTRLEN(goodPsw);
  10470. badPswLen = (int)XSTRLEN(badPsw);
  10471. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  10472. ExpectIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  10473. ExpectNotNull(d2i_PKCS12_bio(bio, &pkcs12));
  10474. ExpectNotNull(pkcs12);
  10475. BIO_free(bio);
  10476. bio = NULL;
  10477. /* check verify MAC directly */
  10478. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen), 1);
  10479. /* check verify MAC fail case directly */
  10480. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen), 0);
  10481. /* check verify MAC fail case */
  10482. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10483. ExpectNull(pkey);
  10484. ExpectNull(cert);
  10485. /* check parse with no extra certs kept */
  10486. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10487. 1);
  10488. ExpectNotNull(pkey);
  10489. ExpectNotNull(cert);
  10490. wolfSSL_EVP_PKEY_free(pkey);
  10491. pkey = NULL;
  10492. wolfSSL_X509_free(cert);
  10493. cert = NULL;
  10494. /* check parse with extra certs kept */
  10495. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10496. 1);
  10497. ExpectNotNull(pkey);
  10498. ExpectNotNull(cert);
  10499. ExpectNotNull(ca);
  10500. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10501. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10502. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  10503. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10504. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  10505. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10506. #else
  10507. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10508. #endif
  10509. /* Copy stack structure */
  10510. ExpectNotNull(tmp_ca = X509_chain_up_ref(ca));
  10511. ExpectIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  10512. /* CTX now owns the tmp_ca stack structure */
  10513. tmp_ca = NULL;
  10514. ExpectIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  10515. ExpectNotNull(tmp_ca);
  10516. ExpectIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  10517. /* Check that the main cert is also set */
  10518. ExpectNotNull(SSL_CTX_get0_certificate(ctx));
  10519. ExpectNotNull(ssl = SSL_new(ctx));
  10520. ExpectNotNull(SSL_get_certificate(ssl));
  10521. SSL_free(ssl);
  10522. SSL_CTX_free(ctx);
  10523. ctx = NULL;
  10524. #endif
  10525. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10526. /* should be 2 other certs on stack */
  10527. ExpectNotNull(tmp = sk_X509_pop(ca));
  10528. X509_free(tmp);
  10529. ExpectNotNull(tmp = sk_X509_pop(ca));
  10530. X509_free(tmp);
  10531. ExpectNull(sk_X509_pop(ca));
  10532. EVP_PKEY_free(pkey);
  10533. pkey = NULL;
  10534. X509_free(cert);
  10535. cert = NULL;
  10536. sk_X509_pop_free(ca, X509_free);
  10537. ca = NULL;
  10538. /* check PKCS12_create */
  10539. ExpectNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  10540. ExpectIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10541. SSL_SUCCESS);
  10542. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  10543. -1, -1, 100, -1, 0)));
  10544. EVP_PKEY_free(pkey);
  10545. pkey = NULL;
  10546. X509_free(cert);
  10547. cert = NULL;
  10548. sk_X509_pop_free(ca, NULL);
  10549. ca = NULL;
  10550. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10551. SSL_SUCCESS);
  10552. PKCS12_free(pkcs12_2);
  10553. pkcs12_2 = NULL;
  10554. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  10555. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  10556. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  10557. 2000, 1, 0)));
  10558. EVP_PKEY_free(pkey);
  10559. pkey = NULL;
  10560. X509_free(cert);
  10561. cert = NULL;
  10562. sk_X509_pop_free(ca, NULL);
  10563. ca = NULL;
  10564. /* convert to DER then back and parse */
  10565. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10566. ExpectIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  10567. PKCS12_free(pkcs12_2);
  10568. pkcs12_2 = NULL;
  10569. ExpectNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  10570. BIO_free(bio);
  10571. bio = NULL;
  10572. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10573. SSL_SUCCESS);
  10574. /* should be 2 other certs on stack */
  10575. ExpectNotNull(tmp = sk_X509_pop(ca));
  10576. X509_free(tmp);
  10577. ExpectNotNull(tmp = sk_X509_pop(ca));
  10578. X509_free(tmp);
  10579. ExpectNull(sk_X509_pop(ca));
  10580. #ifndef NO_RC4
  10581. PKCS12_free(pkcs12_2);
  10582. pkcs12_2 = NULL;
  10583. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  10584. NID_pbe_WithSHA1And128BitRC4,
  10585. NID_pbe_WithSHA1And128BitRC4,
  10586. 2000, 1, 0)));
  10587. EVP_PKEY_free(pkey);
  10588. pkey = NULL;
  10589. X509_free(cert);
  10590. cert = NULL;
  10591. sk_X509_pop_free(ca, NULL);
  10592. ca = NULL;
  10593. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10594. SSL_SUCCESS);
  10595. #endif /* NO_RC4 */
  10596. EVP_PKEY_free(pkey);
  10597. pkey = NULL;
  10598. X509_free(cert);
  10599. cert = NULL;
  10600. PKCS12_free(pkcs12);
  10601. pkcs12 = NULL;
  10602. PKCS12_free(pkcs12_2);
  10603. pkcs12_2 = NULL;
  10604. sk_X509_pop_free(ca, NULL);
  10605. ca = NULL;
  10606. #ifdef HAVE_ECC
  10607. /* test order of parsing */
  10608. ExpectTrue((f = XFOPEN(order, "rb")) != XBADFILE);
  10609. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10610. if (f != XBADFILE) {
  10611. XFCLOSE(f);
  10612. f = XBADFILE;
  10613. }
  10614. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  10615. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  10616. ExpectIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  10617. WOLFSSL_SUCCESS);
  10618. /* check use of pkey after parse */
  10619. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10620. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10621. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10622. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  10623. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10624. #else
  10625. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10626. #endif
  10627. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  10628. SSL_CTX_free(ctx);
  10629. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10630. #endif
  10631. ExpectNotNull(pkey);
  10632. ExpectNotNull(cert);
  10633. ExpectNotNull(ca);
  10634. /* compare subject lines of certificates */
  10635. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  10636. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  10637. SSL_FILETYPE_PEM));
  10638. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10639. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10640. X509_free(x509);
  10641. x509 = NULL;
  10642. /* test expected fail case */
  10643. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  10644. SSL_FILETYPE_PEM));
  10645. ExpectIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10646. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10647. X509_free(x509);
  10648. x509 = NULL;
  10649. X509_free(cert);
  10650. cert = NULL;
  10651. /* get subject line from ca stack */
  10652. ExpectNotNull(cert = sk_X509_pop(ca));
  10653. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  10654. /* compare subject from certificate in ca to expected */
  10655. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  10656. SSL_FILETYPE_PEM));
  10657. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10658. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10659. EVP_PKEY_free(pkey);
  10660. pkey = NULL;
  10661. X509_free(x509);
  10662. x509 = NULL;
  10663. X509_free(cert);
  10664. cert = NULL;
  10665. BIO_free(bio);
  10666. bio = NULL;
  10667. PKCS12_free(pkcs12);
  10668. pkcs12 = NULL;
  10669. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  10670. ca = NULL;
  10671. /* test order of parsing */
  10672. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10673. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  10674. if (f != XBADFILE) {
  10675. XFCLOSE(f);
  10676. f = XBADFILE;
  10677. }
  10678. /* check verify MAC fail case */
  10679. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10680. ExpectNull(pkey);
  10681. ExpectNull(cert);
  10682. /* check parse with no extra certs kept */
  10683. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10684. 1);
  10685. ExpectNotNull(pkey);
  10686. ExpectNotNull(cert);
  10687. wolfSSL_EVP_PKEY_free(pkey);
  10688. pkey = NULL;
  10689. wolfSSL_X509_free(cert);
  10690. cert = NULL;
  10691. /* check parse with extra certs kept */
  10692. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10693. 1);
  10694. ExpectNotNull(pkey);
  10695. ExpectNotNull(cert);
  10696. ExpectNotNull(ca);
  10697. wolfSSL_EVP_PKEY_free(pkey);
  10698. pkey = NULL;
  10699. wolfSSL_X509_free(cert);
  10700. cert = NULL;
  10701. sk_X509_pop_free(ca, NULL);
  10702. ca = NULL;
  10703. PKCS12_free(pkcs12);
  10704. pkcs12 = NULL;
  10705. #endif /* HAVE_ECC */
  10706. #ifdef WC_RC2
  10707. /* test PKCS#12 with RC2 encryption */
  10708. ExpectTrue((f = XFOPEN(rc2p12, "rb")) != XBADFILE);
  10709. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10710. if (f != XBADFILE) {
  10711. XFCLOSE(f);
  10712. f = XBADFILE;
  10713. }
  10714. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  10715. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  10716. /* check verify MAC fail case */
  10717. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10718. ExpectNull(pkey);
  10719. ExpectNull(cert);
  10720. /* check parse with not extra certs kept */
  10721. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10722. WOLFSSL_SUCCESS);
  10723. ExpectNotNull(pkey);
  10724. ExpectNotNull(cert);
  10725. wolfSSL_EVP_PKEY_free(pkey);
  10726. pkey = NULL;
  10727. wolfSSL_X509_free(cert);
  10728. cert = NULL;
  10729. /* check parse with extra certs kept */
  10730. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10731. WOLFSSL_SUCCESS);
  10732. ExpectNotNull(pkey);
  10733. ExpectNotNull(cert);
  10734. ExpectNotNull(ca);
  10735. wolfSSL_EVP_PKEY_free(pkey);
  10736. wolfSSL_X509_free(cert);
  10737. sk_X509_pop_free(ca, NULL);
  10738. BIO_free(bio);
  10739. bio = NULL;
  10740. PKCS12_free(pkcs12);
  10741. pkcs12 = NULL;
  10742. #endif /* WC_RC2 */
  10743. /* Test i2d_PKCS12_bio */
  10744. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10745. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  10746. if (f != XBADFILE)
  10747. XFCLOSE(f);
  10748. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10749. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, pkcs12), 1);
  10750. ExpectIntEQ(ret = i2d_PKCS12_bio(NULL, pkcs12), 0);
  10751. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, NULL), 0);
  10752. PKCS12_free(pkcs12);
  10753. BIO_free(bio);
  10754. (void)order;
  10755. #endif /* OPENSSL_EXTRA */
  10756. #endif /* HAVE_FIPS */
  10757. return EXPECT_RESULT();
  10758. }
  10759. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  10760. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  10761. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  10762. #define TEST_PKCS8_ENC
  10763. #endif
  10764. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  10765. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  10766. /* used to keep track if FailTestCallback was called */
  10767. static int failTestCallbackCalled = 0;
  10768. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  10769. {
  10770. (void)passwd;
  10771. (void)sz;
  10772. (void)rw;
  10773. (void)userdata;
  10774. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  10775. failTestCallbackCalled = 1;
  10776. return -1;
  10777. }
  10778. #endif
  10779. static int test_wolfSSL_no_password_cb(void)
  10780. {
  10781. EXPECT_DECLS;
  10782. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  10783. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  10784. WOLFSSL_CTX* ctx = NULL;
  10785. byte buff[FOURK_BUF];
  10786. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  10787. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  10788. XFILE f = XBADFILE;
  10789. int bytes = 0;
  10790. #ifndef NO_WOLFSSL_CLIENT
  10791. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  10792. #else
  10793. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  10794. #endif
  10795. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  10796. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  10797. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10798. if (f != XBADFILE) {
  10799. XFCLOSE(f);
  10800. f = XBADFILE;
  10801. }
  10802. ExpectIntLE(bytes, sizeof(buff));
  10803. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10804. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10805. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  10806. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10807. if (f != XBADFILE)
  10808. XFCLOSE(f);
  10809. ExpectIntLE(bytes, sizeof(buff));
  10810. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10811. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10812. wolfSSL_CTX_free(ctx);
  10813. /* Password callback should not be called by default */
  10814. ExpectIntEQ(failTestCallbackCalled, 0);
  10815. #endif
  10816. return EXPECT_RESULT();
  10817. }
  10818. #ifdef TEST_PKCS8_ENC
  10819. /* for PKCS8 test case */
  10820. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  10821. {
  10822. int flag = 0;
  10823. (void)rw;
  10824. if (userdata != NULL) {
  10825. flag = *((int*)userdata); /* user set data */
  10826. }
  10827. switch (flag) {
  10828. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  10829. * can be anything the user wishes to be passed to the callback
  10830. * associated with the WOLFSSL_CTX */
  10831. XSTRNCPY(passwd, "yassl123", sz);
  10832. return 8;
  10833. default:
  10834. return BAD_FUNC_ARG;
  10835. }
  10836. }
  10837. #endif /* TEST_PKCS8_ENC */
  10838. /* Testing functions dealing with PKCS8 */
  10839. static int test_wolfSSL_PKCS8(void)
  10840. {
  10841. EXPECT_DECLS;
  10842. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  10843. !defined(WOLFCRYPT_ONLY)
  10844. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10845. byte buff[FOURK_BUF];
  10846. byte der[FOURK_BUF];
  10847. #ifndef NO_RSA
  10848. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  10849. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  10850. #endif
  10851. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  10852. #ifdef HAVE_ECC
  10853. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  10854. #endif
  10855. XFILE f = XBADFILE;
  10856. int bytes = 0;
  10857. WOLFSSL_CTX* ctx = NULL;
  10858. #if defined(HAVE_ECC) && !defined(NO_CODING)
  10859. int ret;
  10860. ecc_key key;
  10861. word32 x = 0;
  10862. #endif
  10863. #ifdef TEST_PKCS8_ENC
  10864. #if !defined(NO_RSA) && !defined(NO_SHA)
  10865. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  10866. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  10867. #endif
  10868. #if defined(HAVE_ECC) && !defined(NO_SHA)
  10869. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  10870. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  10871. #endif
  10872. int flag;
  10873. #endif
  10874. (void)der;
  10875. #ifndef NO_WOLFSSL_CLIENT
  10876. #ifndef WOLFSSL_NO_TLS12
  10877. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  10878. #else
  10879. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  10880. #endif
  10881. #else
  10882. #ifndef WOLFSSL_NO_TLS12
  10883. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  10884. #else
  10885. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  10886. #endif
  10887. #endif
  10888. #ifdef TEST_PKCS8_ENC
  10889. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  10890. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  10891. flag = 1; /* used by password callback as return code */
  10892. #if !defined(NO_RSA) && !defined(NO_SHA)
  10893. /* test loading PEM PKCS8 encrypted file */
  10894. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncPemFile, "rb")) != XBADFILE);
  10895. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10896. if (f != XBADFILE) {
  10897. XFCLOSE(f);
  10898. f = XBADFILE;
  10899. }
  10900. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10901. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10902. /* this next case should fail because of password callback return code */
  10903. flag = 0; /* used by password callback as return code */
  10904. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10905. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10906. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  10907. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10908. "yassl123"), 0);
  10909. /* test that error value is returned with a bad password */
  10910. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10911. "bad"), 0);
  10912. /* test loading PEM PKCS8 encrypted file */
  10913. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncDerFile, "rb")) != XBADFILE);
  10914. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10915. if (f != XBADFILE) {
  10916. XFCLOSE(f);
  10917. f = XBADFILE;
  10918. }
  10919. flag = 1; /* used by password callback as return code */
  10920. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10921. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10922. /* this next case should fail because of password callback return code */
  10923. flag = 0; /* used by password callback as return code */
  10924. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10925. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10926. #endif /* !NO_RSA && !NO_SHA */
  10927. #if defined(HAVE_ECC) && !defined(NO_SHA)
  10928. /* test loading PEM PKCS8 encrypted ECC Key file */
  10929. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb")) != XBADFILE);
  10930. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10931. if (f != XBADFILE) {
  10932. XFCLOSE(f);
  10933. f = XBADFILE;
  10934. }
  10935. flag = 1; /* used by password callback as return code */
  10936. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10937. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10938. /* this next case should fail because of password callback return code */
  10939. flag = 0; /* used by password callback as return code */
  10940. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10941. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10942. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  10943. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10944. "yassl123"), 0);
  10945. /* test that error value is returned with a bad password */
  10946. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  10947. "bad"), 0);
  10948. /* test loading DER PKCS8 encrypted ECC Key file */
  10949. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb")) != XBADFILE);
  10950. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10951. if (f != XBADFILE) {
  10952. XFCLOSE(f);
  10953. f = XBADFILE;
  10954. }
  10955. flag = 1; /* used by password callback as return code */
  10956. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10957. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10958. /* this next case should fail because of password callback return code */
  10959. flag = 0; /* used by password callback as return code */
  10960. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10961. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10962. /* leave flag as "okay" */
  10963. flag = 1;
  10964. #endif /* HAVE_ECC && !NO_SHA */
  10965. #endif /* TEST_PKCS8_ENC */
  10966. #ifndef NO_RSA
  10967. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  10968. ExpectTrue((f = XFOPEN(serverKeyPkcs8DerFile, "rb")) != XBADFILE);
  10969. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10970. if (f != XBADFILE) {
  10971. XFCLOSE(f);
  10972. f = XBADFILE;
  10973. }
  10974. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10975. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10976. /* test loading PEM PKCS8 private key file (not encrypted) */
  10977. ExpectTrue((f = XFOPEN(serverKeyPkcs8PemFile, "rb")) != XBADFILE);
  10978. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10979. if (f != XBADFILE) {
  10980. XFCLOSE(f);
  10981. f = XBADFILE;
  10982. }
  10983. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10984. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10985. #endif /* !NO_RSA */
  10986. /* Test PKCS8 PEM ECC key no crypt */
  10987. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  10988. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10989. if (f != XBADFILE) {
  10990. XFCLOSE(f);
  10991. f = XBADFILE;
  10992. }
  10993. #ifdef HAVE_ECC
  10994. /* Test PKCS8 PEM ECC key no crypt */
  10995. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10996. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10997. #ifndef NO_CODING
  10998. /* decrypt PKCS8 PEM to key in DER format */
  10999. ExpectIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  11000. (word32)sizeof(der), NULL)), 0);
  11001. ret = wc_ecc_init(&key);
  11002. if (ret == 0) {
  11003. ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
  11004. wc_ecc_free(&key);
  11005. }
  11006. ExpectIntEQ(ret, 0);
  11007. #endif
  11008. /* Test PKCS8 DER ECC key no crypt */
  11009. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  11010. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11011. if (f != XBADFILE)
  11012. XFCLOSE(f);
  11013. /* Test using a PKCS8 ECC PEM */
  11014. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11015. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11016. #else
  11017. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  11018. ExpectIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  11019. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  11020. #endif /* HAVE_ECC */
  11021. wolfSSL_CTX_free(ctx);
  11022. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11023. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  11024. return EXPECT_RESULT();
  11025. }
  11026. static int test_wolfSSL_PKCS8_ED25519(void)
  11027. {
  11028. EXPECT_DECLS;
  11029. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  11030. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  11031. defined(HAVE_ED25519_KEY_IMPORT)
  11032. const byte encPrivKey[] = \
  11033. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  11034. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  11035. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  11036. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  11037. "2L9W4v7swXkV+X+a1ww=\n"
  11038. "-----END ENCRYPTED PRIVATE KEY-----\n";
  11039. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  11040. byte der[FOURK_BUF];
  11041. WOLFSSL_CTX* ctx = NULL;
  11042. int bytes;
  11043. XMEMSET(der, 0, sizeof(der));
  11044. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  11045. (word32)sizeof(der), password)), 0);
  11046. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  11047. #ifndef NO_WOLFSSL_SERVER
  11048. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  11049. #else
  11050. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  11051. #endif
  11052. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  11053. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11054. wolfSSL_CTX_free(ctx);
  11055. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11056. #endif
  11057. return EXPECT_RESULT();
  11058. }
  11059. static int test_wolfSSL_PKCS8_ED448(void)
  11060. {
  11061. EXPECT_DECLS;
  11062. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  11063. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  11064. defined(HAVE_ED448_KEY_IMPORT)
  11065. const byte encPrivKey[] = \
  11066. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  11067. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  11068. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  11069. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  11070. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  11071. "-----END ENCRYPTED PRIVATE KEY-----\n";
  11072. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  11073. byte der[FOURK_BUF];
  11074. WOLFSSL_CTX* ctx = NULL;
  11075. int bytes;
  11076. XMEMSET(der, 0, sizeof(der));
  11077. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  11078. (word32)sizeof(der), password)), 0);
  11079. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  11080. #ifndef NO_WOLFSSL_SERVER
  11081. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  11082. #else
  11083. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  11084. #endif
  11085. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  11086. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11087. wolfSSL_CTX_free(ctx);
  11088. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11089. #endif
  11090. return EXPECT_RESULT();
  11091. }
  11092. /* Testing functions dealing with PKCS5 */
  11093. static int test_wolfSSL_PKCS5(void)
  11094. {
  11095. EXPECT_DECLS;
  11096. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  11097. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  11098. const char* passwd = "myfipsPa$$W0rd";
  11099. #else
  11100. const char *passwd = "pass1234";
  11101. #endif
  11102. const unsigned char *salt = (unsigned char *)"salt1234";
  11103. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  11104. DYNAMIC_TYPE_TMP_BUFFER);
  11105. int ret = 0;
  11106. ExpectNotNull(out);
  11107. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  11108. (int)XSTRLEN((const char *) salt), 10, WC_SHA_DIGEST_SIZE,out),
  11109. WOLFSSL_SUCCESS);
  11110. #ifdef WOLFSSL_SHA512
  11111. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  11112. (int)XSTRLEN((const char *) salt), 10, wolfSSL_EVP_sha512(),
  11113. WC_SHA_DIGEST_SIZE, out), SSL_SUCCESS);
  11114. #endif
  11115. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11116. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  11117. return EXPECT_RESULT();
  11118. }
  11119. /* test parsing URI from certificate */
  11120. static int test_wolfSSL_URI(void)
  11121. {
  11122. EXPECT_DECLS;
  11123. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  11124. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  11125. defined(OPENSSL_EXTRA))
  11126. WOLFSSL_X509* x509 = NULL;
  11127. const char uri[] = "./certs/client-uri-cert.pem";
  11128. const char urn[] = "./certs/client-absolute-urn.pem";
  11129. const char badUri[] = "./certs/client-relative-uri.pem";
  11130. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(uri,
  11131. WOLFSSL_FILETYPE_PEM));
  11132. wolfSSL_FreeX509(x509);
  11133. x509 = NULL;
  11134. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(urn,
  11135. WOLFSSL_FILETYPE_PEM));
  11136. wolfSSL_FreeX509(x509);
  11137. x509 = NULL;
  11138. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  11139. && !defined(WOLFSSL_FPKI)
  11140. ExpectNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  11141. WOLFSSL_FILETYPE_PEM));
  11142. #else
  11143. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  11144. WOLFSSL_FILETYPE_PEM));
  11145. #endif
  11146. wolfSSL_FreeX509(x509);
  11147. #endif
  11148. return EXPECT_RESULT();
  11149. }
  11150. static int test_wolfSSL_TBS(void)
  11151. {
  11152. EXPECT_DECLS;
  11153. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  11154. && defined(OPENSSL_EXTRA)
  11155. WOLFSSL_X509* x509 = NULL;
  11156. const unsigned char* tbs;
  11157. int tbsSz;
  11158. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caCertFile,
  11159. WOLFSSL_FILETYPE_PEM));
  11160. ExpectNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  11161. ExpectNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  11162. ExpectNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  11163. ExpectIntEQ(tbsSz, 1003);
  11164. wolfSSL_FreeX509(x509);
  11165. #endif
  11166. return EXPECT_RESULT();
  11167. }
  11168. static int test_wolfSSL_X509_verify(void)
  11169. {
  11170. EXPECT_DECLS;
  11171. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  11172. defined(OPENSSL_EXTRA)
  11173. WOLFSSL_X509* ca = NULL;
  11174. WOLFSSL_X509* serv = NULL;
  11175. WOLFSSL_EVP_PKEY* pkey = NULL;
  11176. unsigned char buf[2048];
  11177. const unsigned char* pt = NULL;
  11178. int bufSz;
  11179. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(caCertFile,
  11180. WOLFSSL_FILETYPE_PEM));
  11181. ExpectIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  11182. WOLFSSL_SUCCESS);
  11183. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  11184. WOLFSSL_SUCCESS);
  11185. ExpectIntEQ(bufSz, 294);
  11186. bufSz = 2048;
  11187. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  11188. WOLFSSL_SUCCESS);
  11189. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  11190. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  11191. ExpectNotNull(serv = wolfSSL_X509_load_certificate_file(svrCertFile,
  11192. WOLFSSL_FILETYPE_PEM));
  11193. /* success case */
  11194. pt = buf;
  11195. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  11196. ExpectIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  11197. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  11198. wolfSSL_EVP_PKEY_free(pkey);
  11199. pkey = NULL;
  11200. /* fail case */
  11201. bufSz = 2048;
  11202. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  11203. WOLFSSL_SUCCESS);
  11204. pt = buf;
  11205. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  11206. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  11207. ExpectIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  11208. ExpectIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  11209. wolfSSL_EVP_PKEY_free(pkey);
  11210. wolfSSL_FreeX509(ca);
  11211. wolfSSL_FreeX509(serv);
  11212. #endif
  11213. return EXPECT_RESULT();
  11214. }
  11215. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11216. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11217. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11218. /* create certificate with version 2 */
  11219. static int test_set_x509_badversion(WOLFSSL_CTX* ctx)
  11220. {
  11221. EXPECT_DECLS;
  11222. WOLFSSL_X509 *x509 = NULL, *x509v2 = NULL;
  11223. WOLFSSL_EVP_PKEY *priv = NULL, *pub = NULL;
  11224. unsigned char *der = NULL, *key = NULL, *pt;
  11225. char *header = NULL, *name = NULL;
  11226. int derSz;
  11227. long keySz;
  11228. XFILE fp = XBADFILE;
  11229. WOLFSSL_ASN1_TIME *notBefore = NULL, *notAfter = NULL;
  11230. time_t t;
  11231. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  11232. WOLFSSL_FILETYPE_PEM));
  11233. ExpectTrue((fp = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  11234. ExpectIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  11235. WOLFSSL_SUCCESS);
  11236. if (fp != XBADFILE)
  11237. XFCLOSE(fp);
  11238. pt = key;
  11239. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  11240. (const unsigned char**)&pt, keySz));
  11241. /* create the version 2 certificate */
  11242. ExpectNotNull(x509v2 = X509_new());
  11243. ExpectIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  11244. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  11245. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  11246. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  11247. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  11248. ExpectNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  11249. ExpectIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  11250. t = time(NULL);
  11251. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  11252. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  11253. ExpectTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  11254. ExpectTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  11255. ExpectIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  11256. derSz = wolfSSL_i2d_X509(x509v2, &der);
  11257. ExpectIntGT(derSz, 0);
  11258. ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  11259. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11260. /* TODO: Replace with API call */
  11261. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  11262. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11263. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11264. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11265. wolfSSL_X509_free(x509);
  11266. wolfSSL_X509_free(x509v2);
  11267. wolfSSL_EVP_PKEY_free(priv);
  11268. wolfSSL_EVP_PKEY_free(pub);
  11269. wolfSSL_ASN1_TIME_free(notBefore);
  11270. wolfSSL_ASN1_TIME_free(notAfter);
  11271. return EXPECT_RESULT();
  11272. }
  11273. /* override certificate version error */
  11274. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  11275. {
  11276. EXPECT_DECLS;
  11277. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  11278. ExpectIntEQ(store->error, ASN_VERSION_E);
  11279. #else
  11280. ExpectIntEQ(store->error, 0);
  11281. #endif
  11282. ExpectIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  11283. (void)preverify;
  11284. return EXPECT_RESULT() == TEST_SUCCESS;
  11285. }
  11286. /* set verify callback that will override bad certificate version */
  11287. static int test_set_override_x509(WOLFSSL_CTX* ctx)
  11288. {
  11289. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  11290. return TEST_SUCCESS;
  11291. }
  11292. #endif
  11293. static int test_wolfSSL_X509_TLS_version_test_1(void)
  11294. {
  11295. EXPECT_DECLS;
  11296. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11297. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11298. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11299. test_ssl_cbf func_cb_client;
  11300. test_ssl_cbf func_cb_server;
  11301. /* test server rejects a client certificate that is not version 3 */
  11302. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  11303. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  11304. func_cb_client.ctx_ready = &test_set_x509_badversion;
  11305. #ifndef WOLFSSL_NO_TLS12
  11306. func_cb_client.method = wolfTLSv1_2_client_method;
  11307. #else
  11308. func_cb_client.method = wolfTLSv1_3_client_method;
  11309. #endif
  11310. #ifndef WOLFSSL_NO_TLS12
  11311. func_cb_server.method = wolfTLSv1_2_server_method;
  11312. #else
  11313. func_cb_server.method = wolfTLSv1_3_server_method;
  11314. #endif
  11315. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  11316. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11317. &func_cb_server, NULL), TEST_FAIL);
  11318. #else
  11319. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11320. &func_cb_server, NULL), TEST_SUCCESS);
  11321. #endif
  11322. #endif
  11323. return EXPECT_RESULT();
  11324. }
  11325. static int test_wolfSSL_X509_TLS_version_test_2(void)
  11326. {
  11327. EXPECT_DECLS;
  11328. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11329. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11330. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11331. test_ssl_cbf func_cb_client;
  11332. test_ssl_cbf func_cb_server;
  11333. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  11334. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  11335. func_cb_client.ctx_ready = &test_set_x509_badversion;
  11336. func_cb_server.ctx_ready = &test_set_override_x509;
  11337. #ifndef WOLFSSL_NO_TLS12
  11338. func_cb_client.method = wolfTLSv1_2_client_method;
  11339. #else
  11340. func_cb_client.method = wolfTLSv1_3_client_method;
  11341. #endif
  11342. #ifndef WOLFSSL_NO_TLS12
  11343. func_cb_server.method = wolfTLSv1_2_server_method;
  11344. #else
  11345. func_cb_server.method = wolfTLSv1_3_server_method;
  11346. #endif
  11347. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11348. &func_cb_server, NULL), TEST_SUCCESS);
  11349. #endif
  11350. return EXPECT_RESULT();
  11351. }
  11352. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  11353. * version allowed.
  11354. * POST: 1 on success.
  11355. */
  11356. static int test_wolfSSL_CTX_SetMinVersion(void)
  11357. {
  11358. int res = TEST_SKIPPED;
  11359. #ifndef NO_WOLFSSL_CLIENT
  11360. int failFlag = WOLFSSL_SUCCESS;
  11361. WOLFSSL_CTX* ctx;
  11362. int itr;
  11363. #ifndef NO_OLD_TLS
  11364. const int versions[] = {
  11365. #ifdef WOLFSSL_ALLOW_TLSV10
  11366. WOLFSSL_TLSV1,
  11367. #endif
  11368. WOLFSSL_TLSV1_1,
  11369. WOLFSSL_TLSV1_2 };
  11370. #elif !defined(WOLFSSL_NO_TLS12)
  11371. const int versions[] = { WOLFSSL_TLSV1_2 };
  11372. #elif defined(WOLFSSL_TLS13)
  11373. const int versions[] = { WOLFSSL_TLSV1_3 };
  11374. #else
  11375. const int versions[0];
  11376. #endif
  11377. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  11378. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  11379. if (wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr))
  11380. != WOLFSSL_SUCCESS) {
  11381. failFlag = WOLFSSL_FAILURE;
  11382. }
  11383. }
  11384. wolfSSL_CTX_free(ctx);
  11385. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  11386. #endif
  11387. return res;
  11388. } /* END test_wolfSSL_CTX_SetMinVersion */
  11389. /*----------------------------------------------------------------------------*
  11390. | OCSP Stapling
  11391. *----------------------------------------------------------------------------*/
  11392. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  11393. * need to contact the CA, lowering the cost of cert revocation checking.
  11394. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  11395. * POST: 1 returned for success.
  11396. */
  11397. static int test_wolfSSL_UseOCSPStapling(void)
  11398. {
  11399. EXPECT_DECLS;
  11400. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  11401. !defined(NO_WOLFSSL_CLIENT)
  11402. WOLFSSL_CTX* ctx = NULL;
  11403. WOLFSSL* ssl = NULL;
  11404. #ifndef NO_WOLFSSL_CLIENT
  11405. #ifndef WOLFSSL_NO_TLS12
  11406. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11407. #else
  11408. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11409. #endif
  11410. #else
  11411. #ifndef WOLFSSL_NO_TLS12
  11412. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11413. #else
  11414. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11415. #endif
  11416. #endif
  11417. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11418. ExpectIntEQ(wolfSSL_UseOCSPStapling(NULL, WOLFSSL_CSR2_OCSP,
  11419. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11420. #ifndef NO_WOLFSSL_CLIENT
  11421. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  11422. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  11423. #else
  11424. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  11425. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11426. #endif
  11427. wolfSSL_free(ssl);
  11428. wolfSSL_CTX_free(ctx);
  11429. #endif
  11430. return EXPECT_RESULT();
  11431. } /* END test_wolfSSL_UseOCSPStapling */
  11432. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  11433. * stapling eliminates the need to contact the CA and lowers cert revocation
  11434. * check.
  11435. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  11436. */
  11437. static int test_wolfSSL_UseOCSPStaplingV2(void)
  11438. {
  11439. EXPECT_DECLS;
  11440. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  11441. !defined(NO_WOLFSSL_CLIENT)
  11442. WOLFSSL_CTX* ctx = NULL;
  11443. WOLFSSL* ssl = NULL;
  11444. #ifndef NO_WOLFSSL_CLIENT
  11445. #ifndef WOLFSSL_NO_TLS12
  11446. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11447. #else
  11448. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11449. #endif
  11450. #else
  11451. #ifndef WOLFSSL_NO_TLS12
  11452. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11453. #else
  11454. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11455. #endif
  11456. #endif
  11457. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11458. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(NULL, WOLFSSL_CSR2_OCSP,
  11459. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11460. #ifndef NO_WOLFSSL_CLIENT
  11461. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  11462. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  11463. #else
  11464. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  11465. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11466. #endif
  11467. wolfSSL_free(ssl);
  11468. wolfSSL_CTX_free(ctx);
  11469. #endif
  11470. return EXPECT_RESULT();
  11471. } /* END test_wolfSSL_UseOCSPStaplingV2 */
  11472. /*----------------------------------------------------------------------------*
  11473. | Multicast Tests
  11474. *----------------------------------------------------------------------------*/
  11475. static int test_wolfSSL_mcast(void)
  11476. {
  11477. EXPECT_DECLS;
  11478. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  11479. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  11480. WOLFSSL_CTX* ctx = NULL;
  11481. WOLFSSL* ssl = NULL;
  11482. byte preMasterSecret[512];
  11483. byte clientRandom[32];
  11484. byte serverRandom[32];
  11485. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  11486. byte buf[256];
  11487. word16 newId;
  11488. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  11489. ExpectIntEQ(wolfSSL_CTX_mcast_set_member_id(ctx, 0), WOLFSSL_SUCCESS);
  11490. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11491. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  11492. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  11493. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  11494. ExpectIntEQ(wolfSSL_set_secret(ssl, 23, preMasterSecret,
  11495. sizeof(preMasterSecret), clientRandom, serverRandom, suite),
  11496. WOLFSSL_SUCCESS);
  11497. ExpectIntLE(wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf)), 0);
  11498. ExpectIntLE(newId, 100);
  11499. wolfSSL_free(ssl);
  11500. wolfSSL_CTX_free(ctx);
  11501. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 ||
  11502. * WOLFSSL_SNIFFER) */
  11503. return EXPECT_RESULT();
  11504. }
  11505. /*----------------------------------------------------------------------------*
  11506. | Wolfcrypt
  11507. *----------------------------------------------------------------------------*/
  11508. /*
  11509. * Unit test for the wc_InitBlake2b()
  11510. */
  11511. static int test_wc_InitBlake2b(void)
  11512. {
  11513. EXPECT_DECLS;
  11514. #ifdef HAVE_BLAKE2
  11515. Blake2b blake;
  11516. /* Test good arg. */
  11517. ExpectIntEQ(wc_InitBlake2b(&blake, 64), 0);
  11518. /* Test bad arg. */
  11519. ExpectIntEQ(wc_InitBlake2b(NULL, 64), BAD_FUNC_ARG);
  11520. ExpectIntEQ(wc_InitBlake2b(NULL, 128), BAD_FUNC_ARG);
  11521. ExpectIntEQ(wc_InitBlake2b(&blake, 128), BAD_FUNC_ARG);
  11522. ExpectIntEQ(wc_InitBlake2b(NULL, 0), BAD_FUNC_ARG);
  11523. ExpectIntEQ(wc_InitBlake2b(&blake, 0), BAD_FUNC_ARG);
  11524. #endif
  11525. return EXPECT_RESULT();
  11526. } /* END test_wc_InitBlake2b*/
  11527. /*
  11528. * Unit test for the wc_InitBlake2b_WithKey()
  11529. */
  11530. static int test_wc_InitBlake2b_WithKey(void)
  11531. {
  11532. EXPECT_DECLS;
  11533. #ifdef HAVE_BLAKE2
  11534. Blake2b blake;
  11535. word32 digestSz = BLAKE2B_KEYBYTES;
  11536. byte key[BLAKE2B_KEYBYTES];
  11537. word32 keylen = BLAKE2B_KEYBYTES;
  11538. XMEMSET(key, 0, sizeof(key));
  11539. /* Test good arg. */
  11540. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen), 0);
  11541. /* Test bad args. */
  11542. ExpectIntEQ(wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen),
  11543. BAD_FUNC_ARG);
  11544. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, 256),
  11545. BAD_FUNC_ARG);
  11546. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen), 0);
  11547. #endif
  11548. return EXPECT_RESULT();
  11549. } /* END wc_InitBlake2b_WithKey*/
  11550. /*
  11551. * Unit test for the wc_InitBlake2s_WithKey()
  11552. */
  11553. static int test_wc_InitBlake2s_WithKey(void)
  11554. {
  11555. EXPECT_DECLS;
  11556. #ifdef HAVE_BLAKE2S
  11557. Blake2s blake;
  11558. word32 digestSz = BLAKE2S_KEYBYTES;
  11559. byte *key = (byte*)"01234567890123456789012345678901";
  11560. word32 keylen = BLAKE2S_KEYBYTES;
  11561. /* Test good arg. */
  11562. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen), 0);
  11563. /* Test bad args. */
  11564. ExpectIntEQ(wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen),
  11565. BAD_FUNC_ARG);
  11566. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, 256),
  11567. BAD_FUNC_ARG);
  11568. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen), 0);
  11569. #endif
  11570. return EXPECT_RESULT();
  11571. } /* END wc_InitBlake2s_WithKey*/
  11572. /*
  11573. * Unit test for the wc_InitMd5()
  11574. */
  11575. static int test_wc_InitMd5(void)
  11576. {
  11577. EXPECT_DECLS;
  11578. #ifndef NO_MD5
  11579. wc_Md5 md5;
  11580. /* Test good arg. */
  11581. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11582. /* Test bad arg. */
  11583. ExpectIntEQ(wc_InitMd5(NULL), BAD_FUNC_ARG);
  11584. wc_Md5Free(&md5);
  11585. #endif
  11586. return EXPECT_RESULT();
  11587. } /* END test_wc_InitMd5 */
  11588. /*
  11589. * Testing wc_UpdateMd5()
  11590. */
  11591. static int test_wc_Md5Update(void)
  11592. {
  11593. EXPECT_DECLS;
  11594. #ifndef NO_MD5
  11595. wc_Md5 md5;
  11596. byte hash[WC_MD5_DIGEST_SIZE];
  11597. testVector a, b, c;
  11598. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11599. /* Input */
  11600. a.input = "a";
  11601. a.inLen = XSTRLEN(a.input);
  11602. ExpectIntEQ(wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen), 0);
  11603. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  11604. /* Update input. */
  11605. a.input = "abc";
  11606. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  11607. "\x72";
  11608. a.inLen = XSTRLEN(a.input);
  11609. a.outLen = XSTRLEN(a.output);
  11610. ExpectIntEQ(wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen), 0);
  11611. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  11612. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  11613. /* Pass in bad values. */
  11614. b.input = NULL;
  11615. b.inLen = 0;
  11616. ExpectIntEQ(wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen), 0);
  11617. c.input = NULL;
  11618. c.inLen = WC_MD5_DIGEST_SIZE;
  11619. ExpectIntEQ(wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen),
  11620. BAD_FUNC_ARG);
  11621. ExpectIntEQ(wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen),
  11622. BAD_FUNC_ARG);
  11623. wc_Md5Free(&md5);
  11624. #endif
  11625. return EXPECT_RESULT();
  11626. } /* END test_wc_Md5Update() */
  11627. /*
  11628. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  11629. */
  11630. static int test_wc_Md5Final(void)
  11631. {
  11632. EXPECT_DECLS;
  11633. #ifndef NO_MD5
  11634. /* Instantiate */
  11635. wc_Md5 md5;
  11636. byte* hash_test[3];
  11637. byte hash1[WC_MD5_DIGEST_SIZE];
  11638. byte hash2[2*WC_MD5_DIGEST_SIZE];
  11639. byte hash3[5*WC_MD5_DIGEST_SIZE];
  11640. int times, i;
  11641. /* Initialize */
  11642. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11643. hash_test[0] = hash1;
  11644. hash_test[1] = hash2;
  11645. hash_test[2] = hash3;
  11646. times = sizeof(hash_test)/sizeof(byte*);
  11647. for (i = 0; i < times; i++) {
  11648. ExpectIntEQ(wc_Md5Final(&md5, hash_test[i]), 0);
  11649. }
  11650. /* Test bad args. */
  11651. ExpectIntEQ(wc_Md5Final(NULL, NULL), BAD_FUNC_ARG);
  11652. ExpectIntEQ(wc_Md5Final(NULL, hash1), BAD_FUNC_ARG);
  11653. ExpectIntEQ(wc_Md5Final(&md5, NULL), BAD_FUNC_ARG);
  11654. wc_Md5Free(&md5);
  11655. #endif
  11656. return EXPECT_RESULT();
  11657. }
  11658. /*
  11659. * Unit test for the wc_InitSha()
  11660. */
  11661. static int test_wc_InitSha(void)
  11662. {
  11663. EXPECT_DECLS;
  11664. #ifndef NO_SHA
  11665. wc_Sha sha;
  11666. /* Test good arg. */
  11667. ExpectIntEQ(wc_InitSha(&sha), 0);
  11668. /* Test bad arg. */
  11669. ExpectIntEQ(wc_InitSha(NULL), BAD_FUNC_ARG);
  11670. wc_ShaFree(&sha);
  11671. #endif
  11672. return EXPECT_RESULT();
  11673. } /* END test_wc_InitSha */
  11674. /*
  11675. * Tesing wc_ShaUpdate()
  11676. */
  11677. static int test_wc_ShaUpdate(void)
  11678. {
  11679. EXPECT_DECLS;
  11680. #ifndef NO_SHA
  11681. wc_Sha sha;
  11682. byte hash[WC_SHA_DIGEST_SIZE];
  11683. testVector a, b, c;
  11684. ExpectIntEQ(wc_InitSha(&sha), 0);
  11685. /* Input. */
  11686. a.input = "a";
  11687. a.inLen = XSTRLEN(a.input);
  11688. ExpectIntEQ(wc_ShaUpdate(&sha, NULL, 0), 0);
  11689. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, 0), 0);
  11690. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  11691. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  11692. /* Update input. */
  11693. a.input = "abc";
  11694. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  11695. "\x6C\x9C\xD0\xD8\x9D";
  11696. a.inLen = XSTRLEN(a.input);
  11697. a.outLen = XSTRLEN(a.output);
  11698. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  11699. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  11700. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  11701. /* Try passing in bad values. */
  11702. b.input = NULL;
  11703. b.inLen = 0;
  11704. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen), 0);
  11705. c.input = NULL;
  11706. c.inLen = WC_SHA_DIGEST_SIZE;
  11707. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen),
  11708. BAD_FUNC_ARG);
  11709. ExpectIntEQ(wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  11710. BAD_FUNC_ARG);
  11711. wc_ShaFree(&sha);
  11712. #endif
  11713. return EXPECT_RESULT();
  11714. } /* END test_wc_ShaUpdate() */
  11715. /*
  11716. * Unit test on wc_ShaFinal
  11717. */
  11718. static int test_wc_ShaFinal(void)
  11719. {
  11720. EXPECT_DECLS;
  11721. #ifndef NO_SHA
  11722. wc_Sha sha;
  11723. byte* hash_test[3];
  11724. byte hash1[WC_SHA_DIGEST_SIZE];
  11725. byte hash2[2*WC_SHA_DIGEST_SIZE];
  11726. byte hash3[5*WC_SHA_DIGEST_SIZE];
  11727. int times, i;
  11728. /* Initialize*/
  11729. ExpectIntEQ(wc_InitSha(&sha), 0);
  11730. hash_test[0] = hash1;
  11731. hash_test[1] = hash2;
  11732. hash_test[2] = hash3;
  11733. times = sizeof(hash_test)/sizeof(byte*);
  11734. for (i = 0; i < times; i++) {
  11735. ExpectIntEQ(wc_ShaFinal(&sha, hash_test[i]), 0);
  11736. }
  11737. /* Test bad args. */
  11738. ExpectIntEQ(wc_ShaFinal(NULL, NULL), BAD_FUNC_ARG);
  11739. ExpectIntEQ(wc_ShaFinal(NULL, hash1), BAD_FUNC_ARG);
  11740. ExpectIntEQ(wc_ShaFinal(&sha, NULL), BAD_FUNC_ARG);
  11741. wc_ShaFree(&sha);
  11742. #endif
  11743. return EXPECT_RESULT();
  11744. } /* END test_wc_ShaFinal */
  11745. /*
  11746. * Unit test for wc_InitSha256()
  11747. */
  11748. static int test_wc_InitSha256(void)
  11749. {
  11750. EXPECT_DECLS;
  11751. #ifndef NO_SHA256
  11752. wc_Sha256 sha256;
  11753. /* Test good arg. */
  11754. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11755. /* Test bad arg. */
  11756. ExpectIntEQ(wc_InitSha256(NULL), BAD_FUNC_ARG);
  11757. wc_Sha256Free(&sha256);
  11758. #endif
  11759. return EXPECT_RESULT();
  11760. } /* END test_wc_InitSha256 */
  11761. /*
  11762. * Unit test for wc_Sha256Update()
  11763. */
  11764. static int test_wc_Sha256Update(void)
  11765. {
  11766. EXPECT_DECLS;
  11767. #ifndef NO_SHA256
  11768. wc_Sha256 sha256;
  11769. byte hash[WC_SHA256_DIGEST_SIZE];
  11770. byte hash_unaligned[WC_SHA256_DIGEST_SIZE+1];
  11771. testVector a, b, c;
  11772. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11773. /* Input. */
  11774. a.input = "a";
  11775. a.inLen = XSTRLEN(a.input);
  11776. ExpectIntEQ(wc_Sha256Update(&sha256, NULL, 0), 0);
  11777. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, 0), 0);
  11778. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  11779. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  11780. /* Update input. */
  11781. a.input = "abc";
  11782. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  11783. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  11784. "\x15\xAD";
  11785. a.inLen = XSTRLEN(a.input);
  11786. a.outLen = XSTRLEN(a.output);
  11787. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  11788. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  11789. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  11790. /* Unaligned check. */
  11791. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input+1, (word32)a.inLen-1),
  11792. 0);
  11793. ExpectIntEQ(wc_Sha256Final(&sha256, hash_unaligned + 1), 0);
  11794. /* Try passing in bad values */
  11795. b.input = NULL;
  11796. b.inLen = 0;
  11797. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen), 0);
  11798. c.input = NULL;
  11799. c.inLen = WC_SHA256_DIGEST_SIZE;
  11800. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen),
  11801. BAD_FUNC_ARG);
  11802. ExpectIntEQ(wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen),
  11803. BAD_FUNC_ARG);
  11804. wc_Sha256Free(&sha256);
  11805. #endif
  11806. return EXPECT_RESULT();
  11807. } /* END test_wc_Sha256Update */
  11808. /*
  11809. * Unit test function for wc_Sha256Final()
  11810. */
  11811. static int test_wc_Sha256Final(void)
  11812. {
  11813. EXPECT_DECLS;
  11814. #ifndef NO_SHA256
  11815. wc_Sha256 sha256;
  11816. byte* hash_test[3];
  11817. byte hash1[WC_SHA256_DIGEST_SIZE];
  11818. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  11819. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  11820. int times, i;
  11821. /* Initialize */
  11822. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11823. hash_test[0] = hash1;
  11824. hash_test[1] = hash2;
  11825. hash_test[2] = hash3;
  11826. times = sizeof(hash_test) / sizeof(byte*);
  11827. for (i = 0; i < times; i++) {
  11828. ExpectIntEQ(wc_Sha256Final(&sha256, hash_test[i]), 0);
  11829. }
  11830. /* Test bad args. */
  11831. ExpectIntEQ(wc_Sha256Final(NULL, NULL), BAD_FUNC_ARG);
  11832. ExpectIntEQ(wc_Sha256Final(NULL, hash1), BAD_FUNC_ARG);
  11833. ExpectIntEQ(wc_Sha256Final(&sha256, NULL), BAD_FUNC_ARG);
  11834. wc_Sha256Free(&sha256);
  11835. #endif
  11836. return EXPECT_RESULT();
  11837. } /* END test_wc_Sha256Final */
  11838. /*
  11839. * Unit test function for wc_Sha256FinalRaw()
  11840. */
  11841. static int test_wc_Sha256FinalRaw(void)
  11842. {
  11843. EXPECT_DECLS;
  11844. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  11845. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  11846. !defined(WOLFSSL_NO_HASH_RAW)
  11847. wc_Sha256 sha256;
  11848. byte* hash_test[3];
  11849. byte hash1[WC_SHA256_DIGEST_SIZE];
  11850. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  11851. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  11852. int times, i;
  11853. /* Initialize */
  11854. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11855. hash_test[0] = hash1;
  11856. hash_test[1] = hash2;
  11857. hash_test[2] = hash3;
  11858. times = sizeof(hash_test) / sizeof(byte*);
  11859. for (i = 0; i < times; i++) {
  11860. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, hash_test[i]), 0);
  11861. }
  11862. /* Test bad args. */
  11863. ExpectIntEQ(wc_Sha256FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  11864. ExpectIntEQ(wc_Sha256FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  11865. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, NULL), BAD_FUNC_ARG);
  11866. wc_Sha256Free(&sha256);
  11867. #endif
  11868. return EXPECT_RESULT();
  11869. } /* END test_wc_Sha256FinalRaw */
  11870. /*
  11871. * Unit test function for wc_Sha256GetFlags()
  11872. */
  11873. static int test_wc_Sha256GetFlags(void)
  11874. {
  11875. EXPECT_DECLS;
  11876. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  11877. wc_Sha256 sha256;
  11878. word32 flags = 0;
  11879. /* Initialize */
  11880. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11881. ExpectIntEQ(wc_Sha256GetFlags(&sha256, &flags), 0);
  11882. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11883. wc_Sha256Free(&sha256);
  11884. #endif
  11885. return EXPECT_RESULT();
  11886. } /* END test_wc_Sha256GetFlags */
  11887. /*
  11888. * Unit test function for wc_Sha256Free()
  11889. */
  11890. static int test_wc_Sha256Free(void)
  11891. {
  11892. EXPECT_DECLS;
  11893. #ifndef NO_SHA256
  11894. wc_Sha256Free(NULL);
  11895. /* Set result to SUCCESS. */
  11896. ExpectTrue(1);
  11897. #endif
  11898. return EXPECT_RESULT();
  11899. } /* END test_wc_Sha256Free */
  11900. /*
  11901. * Unit test function for wc_Sha256GetHash()
  11902. */
  11903. static int test_wc_Sha256GetHash(void)
  11904. {
  11905. EXPECT_DECLS;
  11906. #ifndef NO_SHA256
  11907. wc_Sha256 sha256;
  11908. byte hash1[WC_SHA256_DIGEST_SIZE];
  11909. /* Initialize */
  11910. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11911. ExpectIntEQ(wc_Sha256GetHash(&sha256, hash1), 0);
  11912. /* test bad arguments*/
  11913. ExpectIntEQ(wc_Sha256GetHash(NULL, NULL), BAD_FUNC_ARG);
  11914. ExpectIntEQ(wc_Sha256GetHash(NULL, hash1), BAD_FUNC_ARG);
  11915. ExpectIntEQ(wc_Sha256GetHash(&sha256, NULL), BAD_FUNC_ARG);
  11916. wc_Sha256Free(&sha256);
  11917. #endif
  11918. return EXPECT_RESULT();
  11919. } /* END test_wc_Sha256GetHash */
  11920. /*
  11921. * Unit test function for wc_Sha256Copy()
  11922. */
  11923. static int test_wc_Sha256Copy(void)
  11924. {
  11925. EXPECT_DECLS;
  11926. #ifndef NO_SHA256
  11927. wc_Sha256 sha256;
  11928. wc_Sha256 temp;
  11929. XMEMSET(&sha256, 0, sizeof(sha256));
  11930. XMEMSET(&temp, 0, sizeof(temp));
  11931. /* Initialize */
  11932. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11933. ExpectIntEQ(wc_InitSha256(&temp), 0);
  11934. ExpectIntEQ(wc_Sha256Copy(&sha256, &temp), 0);
  11935. /* test bad arguments*/
  11936. ExpectIntEQ(wc_Sha256Copy(NULL, NULL), BAD_FUNC_ARG);
  11937. ExpectIntEQ(wc_Sha256Copy(NULL, &temp), BAD_FUNC_ARG);
  11938. ExpectIntEQ(wc_Sha256Copy(&sha256, NULL), BAD_FUNC_ARG);
  11939. wc_Sha256Free(&sha256);
  11940. wc_Sha256Free(&temp);
  11941. #endif
  11942. return EXPECT_RESULT();
  11943. } /* END test_wc_Sha256Copy */
  11944. /*
  11945. * Testing wc_InitSha512()
  11946. */
  11947. static int test_wc_InitSha512(void)
  11948. {
  11949. EXPECT_DECLS;
  11950. #ifdef WOLFSSL_SHA512
  11951. wc_Sha512 sha512;
  11952. /* Test good arg. */
  11953. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11954. /* Test bad arg. */
  11955. ExpectIntEQ(wc_InitSha512(NULL), BAD_FUNC_ARG);
  11956. wc_Sha512Free(&sha512);
  11957. #endif
  11958. return EXPECT_RESULT();
  11959. } /* END test_wc_InitSha512 */
  11960. /*
  11961. * wc_Sha512Update() test.
  11962. */
  11963. static int test_wc_Sha512Update(void)
  11964. {
  11965. EXPECT_DECLS;
  11966. #ifdef WOLFSSL_SHA512
  11967. wc_Sha512 sha512;
  11968. byte hash[WC_SHA512_DIGEST_SIZE];
  11969. byte hash_unaligned[WC_SHA512_DIGEST_SIZE + 1];
  11970. testVector a, b, c;
  11971. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  11972. /* Input. */
  11973. a.input = "a";
  11974. a.inLen = XSTRLEN(a.input);
  11975. ExpectIntEQ(wc_Sha512Update(&sha512, NULL, 0), 0);
  11976. ExpectIntEQ(wc_Sha512Update(&sha512,(byte*)a.input, 0), 0);
  11977. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen), 0);
  11978. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  11979. /* Update input. */
  11980. a.input = "abc";
  11981. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  11982. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  11983. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  11984. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  11985. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  11986. a.inLen = XSTRLEN(a.input);
  11987. a.outLen = XSTRLEN(a.output);
  11988. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen), 0);
  11989. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  11990. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE), 0);
  11991. /* Unaligned check. */
  11992. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input+1, (word32)a.inLen-1),
  11993. 0);
  11994. ExpectIntEQ(wc_Sha512Final(&sha512, hash_unaligned+1), 0);
  11995. /* Try passing in bad values */
  11996. b.input = NULL;
  11997. b.inLen = 0;
  11998. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen), 0);
  11999. c.input = NULL;
  12000. c.inLen = WC_SHA512_DIGEST_SIZE;
  12001. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12002. BAD_FUNC_ARG);
  12003. ExpectIntEQ(wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen),
  12004. BAD_FUNC_ARG);
  12005. wc_Sha512Free(&sha512);
  12006. #endif
  12007. return EXPECT_RESULT();
  12008. } /* END test_wc_Sha512Update */
  12009. #ifdef WOLFSSL_SHA512
  12010. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12011. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  12012. /* Performs test for
  12013. * - wc_Sha512Final/wc_Sha512FinalRaw
  12014. * - wc_Sha512_224Final/wc_Sha512_224Final
  12015. * - wc_Sha512_256Final/wc_Sha512_256Final
  12016. * parameter:
  12017. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  12018. * WC_HASH_TYPE_SHA512_256
  12019. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  12020. *return 0 on success
  12021. */
  12022. static int test_Sha512_Family_Final(int type, int isRaw)
  12023. {
  12024. EXPECT_DECLS;
  12025. wc_Sha512 sha512;
  12026. byte* hash_test[3];
  12027. byte hash1[WC_SHA512_DIGEST_SIZE];
  12028. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12029. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12030. int times, i;
  12031. int(*initFp)(wc_Sha512*);
  12032. int(*finalFp)(wc_Sha512*, byte*);
  12033. void(*freeFp)(wc_Sha512*);
  12034. if (type == WC_HASH_TYPE_SHA512) {
  12035. initFp = wc_InitSha512;
  12036. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12037. !defined(WOLFSSL_NO_HASH_RAW)
  12038. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  12039. #else
  12040. finalFp = (isRaw)? NULL : wc_Sha512Final;
  12041. #endif
  12042. freeFp = wc_Sha512Free;
  12043. }
  12044. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12045. #if !defined(WOLFSSL_NOSHA512_224)
  12046. else if (type == WC_HASH_TYPE_SHA512_224) {
  12047. initFp = wc_InitSha512_224;
  12048. #if !defined(WOLFSSL_NO_HASH_RAW)
  12049. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  12050. #else
  12051. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  12052. #endif
  12053. freeFp = wc_Sha512_224Free;
  12054. }
  12055. #endif
  12056. #if !defined(WOLFSSL_NOSHA512_256)
  12057. else if (type == WC_HASH_TYPE_SHA512_256) {
  12058. initFp = wc_InitSha512_256;
  12059. #if !defined(WOLFSSL_NO_HASH_RAW)
  12060. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  12061. #else
  12062. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  12063. #endif
  12064. freeFp = wc_Sha512_256Free;
  12065. }
  12066. #endif
  12067. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12068. else
  12069. return TEST_FAIL;
  12070. /* Initialize */
  12071. ExpectIntEQ(initFp(&sha512), 0);
  12072. hash_test[0] = hash1;
  12073. hash_test[1] = hash2;
  12074. hash_test[2] = hash3;
  12075. times = sizeof(hash_test) / sizeof(byte *);
  12076. /* Good test args. */
  12077. for (i = 0; i < times; i++) {
  12078. ExpectIntEQ(finalFp(&sha512, hash_test[i]), 0);
  12079. }
  12080. /* Test bad args. */
  12081. ExpectIntEQ(finalFp(NULL, NULL), BAD_FUNC_ARG);
  12082. ExpectIntEQ(finalFp(NULL, hash1), BAD_FUNC_ARG);
  12083. ExpectIntEQ(finalFp(&sha512, NULL), BAD_FUNC_ARG);
  12084. freeFp(&sha512);
  12085. return EXPECT_RESULT();
  12086. }
  12087. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  12088. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  12089. #endif /* WOLFSSL_SHA512 */
  12090. /*
  12091. * Unit test function for wc_Sha512Final()
  12092. */
  12093. static int test_wc_Sha512Final(void)
  12094. {
  12095. EXPECT_DECLS;
  12096. #ifdef WOLFSSL_SHA512
  12097. wc_Sha512 sha512;
  12098. byte* hash_test[3];
  12099. byte hash1[WC_SHA512_DIGEST_SIZE];
  12100. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12101. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12102. int times, i;
  12103. /* Initialize */
  12104. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12105. hash_test[0] = hash1;
  12106. hash_test[1] = hash2;
  12107. hash_test[2] = hash3;
  12108. times = sizeof(hash_test) / sizeof(byte *);
  12109. for (i = 0; i < times; i++) {
  12110. ExpectIntEQ(wc_Sha512Final(&sha512, hash_test[i]), 0);
  12111. }
  12112. /* Test bad args. */
  12113. ExpectIntEQ(wc_Sha512Final(NULL, NULL), BAD_FUNC_ARG);
  12114. ExpectIntEQ(wc_Sha512Final(NULL, hash1), BAD_FUNC_ARG);
  12115. ExpectIntEQ(wc_Sha512Final(&sha512, NULL), BAD_FUNC_ARG);
  12116. wc_Sha512Free(&sha512);
  12117. #endif
  12118. return EXPECT_RESULT();
  12119. } /* END test_wc_Sha512Final */
  12120. /*
  12121. * Unit test function for wc_Sha512GetFlags()
  12122. */
  12123. static int test_wc_Sha512GetFlags(void)
  12124. {
  12125. EXPECT_DECLS;
  12126. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  12127. wc_Sha512 sha512;
  12128. word32 flags = 0;
  12129. /* Initialize */
  12130. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12131. ExpectIntEQ(wc_Sha512GetFlags(&sha512, &flags), 0);
  12132. ExpectIntEQ((flags & WC_HASH_FLAG_ISCOPY), 0);
  12133. wc_Sha512Free(&sha512);
  12134. #endif
  12135. return EXPECT_RESULT();
  12136. } /* END test_wc_Sha512GetFlags */
  12137. /*
  12138. * Unit test function for wc_Sha512FinalRaw()
  12139. */
  12140. static int test_wc_Sha512FinalRaw(void)
  12141. {
  12142. EXPECT_DECLS;
  12143. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  12144. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  12145. !defined(WOLFSSL_NO_HASH_RAW)
  12146. wc_Sha512 sha512;
  12147. byte* hash_test[3];
  12148. byte hash1[WC_SHA512_DIGEST_SIZE];
  12149. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12150. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12151. int times, i;
  12152. /* Initialize */
  12153. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12154. hash_test[0] = hash1;
  12155. hash_test[1] = hash2;
  12156. hash_test[2] = hash3;
  12157. times = sizeof(hash_test) / sizeof(byte*);
  12158. /* Good test args. */
  12159. for (i = 0; i < times; i++) {
  12160. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, hash_test[i]), 0);
  12161. }
  12162. /* Test bad args. */
  12163. ExpectIntEQ(wc_Sha512FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12164. ExpectIntEQ(wc_Sha512FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12165. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, NULL), BAD_FUNC_ARG);
  12166. wc_Sha512Free(&sha512);
  12167. #endif
  12168. return EXPECT_RESULT();
  12169. } /* END test_wc_Sha512FinalRaw */
  12170. /*
  12171. * Unit test function for wc_Sha512Free()
  12172. */
  12173. static int test_wc_Sha512Free(void)
  12174. {
  12175. EXPECT_DECLS;
  12176. #ifdef WOLFSSL_SHA512
  12177. wc_Sha512Free(NULL);
  12178. /* Set result to SUCCESS. */
  12179. ExpectTrue(1);
  12180. #endif
  12181. return EXPECT_RESULT();
  12182. } /* END test_wc_Sha512Free */
  12183. #ifdef WOLFSSL_SHA512
  12184. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12185. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  12186. static int test_Sha512_Family_GetHash(int type )
  12187. {
  12188. EXPECT_DECLS;
  12189. int(*initFp)(wc_Sha512*);
  12190. int(*ghashFp)(wc_Sha512*, byte*);
  12191. wc_Sha512 sha512;
  12192. byte hash1[WC_SHA512_DIGEST_SIZE];
  12193. if (type == WC_HASH_TYPE_SHA512) {
  12194. initFp = wc_InitSha512;
  12195. ghashFp = wc_Sha512GetHash;
  12196. }
  12197. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12198. #if !defined(WOLFSSL_NOSHA512_224)
  12199. else if (type == WC_HASH_TYPE_SHA512_224) {
  12200. initFp = wc_InitSha512_224;
  12201. ghashFp = wc_Sha512_224GetHash;
  12202. }
  12203. #endif
  12204. #if !defined(WOLFSSL_NOSHA512_256)
  12205. else if (type == WC_HASH_TYPE_SHA512_256) {
  12206. initFp = wc_InitSha512_256;
  12207. ghashFp = wc_Sha512_256GetHash;
  12208. }
  12209. #endif
  12210. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12211. else {
  12212. initFp = NULL;
  12213. ghashFp = NULL;
  12214. }
  12215. if (initFp == NULL || ghashFp == NULL)
  12216. return TEST_FAIL;
  12217. ExpectIntEQ(initFp(&sha512), 0);
  12218. ExpectIntEQ(ghashFp(&sha512, hash1), 0);
  12219. /* test bad arguments*/
  12220. ExpectIntEQ(ghashFp(NULL, NULL), BAD_FUNC_ARG);
  12221. ExpectIntEQ(ghashFp(NULL, hash1), BAD_FUNC_ARG);
  12222. ExpectIntEQ(ghashFp(&sha512, NULL), BAD_FUNC_ARG);
  12223. wc_Sha512Free(&sha512);
  12224. return EXPECT_RESULT();
  12225. }
  12226. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  12227. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  12228. #endif /* WOLFSSL_SHA512 */
  12229. /*
  12230. * Unit test function for wc_Sha512GetHash()
  12231. */
  12232. static int test_wc_Sha512GetHash(void)
  12233. {
  12234. EXPECT_DECLS;
  12235. #ifdef WOLFSSL_SHA512
  12236. wc_Sha512 sha512;
  12237. byte hash1[WC_SHA512_DIGEST_SIZE];
  12238. /* Initialize */
  12239. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12240. ExpectIntEQ(wc_Sha512GetHash(&sha512, hash1), 0);
  12241. /* test bad arguments*/
  12242. ExpectIntEQ(wc_Sha512GetHash(NULL, NULL), BAD_FUNC_ARG);
  12243. ExpectIntEQ(wc_Sha512GetHash(NULL, hash1), BAD_FUNC_ARG);
  12244. ExpectIntEQ(wc_Sha512GetHash(&sha512, NULL), BAD_FUNC_ARG);
  12245. wc_Sha512Free(&sha512);
  12246. #endif
  12247. return EXPECT_RESULT();
  12248. } /* END test_wc_Sha512GetHash */
  12249. /*
  12250. * Unit test function for wc_Sha512Copy()
  12251. */
  12252. static int test_wc_Sha512Copy(void)
  12253. {
  12254. EXPECT_DECLS;
  12255. #ifdef WOLFSSL_SHA512
  12256. wc_Sha512 sha512;
  12257. wc_Sha512 temp;
  12258. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12259. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12260. /* Initialize */
  12261. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12262. ExpectIntEQ(wc_InitSha512(&temp), 0);
  12263. ExpectIntEQ(wc_Sha512Copy(&sha512, &temp), 0);
  12264. /* test bad arguments*/
  12265. ExpectIntEQ(wc_Sha512Copy(NULL, NULL), BAD_FUNC_ARG);
  12266. ExpectIntEQ(wc_Sha512Copy(NULL, &temp), BAD_FUNC_ARG);
  12267. ExpectIntEQ(wc_Sha512Copy(&sha512, NULL), BAD_FUNC_ARG);
  12268. wc_Sha512Free(&sha512);
  12269. wc_Sha512Free(&temp);
  12270. #endif
  12271. return EXPECT_RESULT();
  12272. } /* END test_wc_Sha512Copy */
  12273. static int test_wc_InitSha512_224(void)
  12274. {
  12275. EXPECT_DECLS;
  12276. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12277. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12278. wc_Sha512 sha512;
  12279. /* Test good arg. */
  12280. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12281. /* Test bad arg. */
  12282. ExpectIntEQ(wc_InitSha512_224(NULL), BAD_FUNC_ARG);
  12283. wc_Sha512_224Free(&sha512);
  12284. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12285. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12286. return EXPECT_RESULT();
  12287. }
  12288. static int test_wc_Sha512_224Update(void)
  12289. {
  12290. EXPECT_DECLS;
  12291. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12292. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12293. wc_Sha512 sha512;
  12294. byte hash[WC_SHA512_DIGEST_SIZE];
  12295. testVector a, c;
  12296. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12297. /* Input. */
  12298. a.input = "a";
  12299. a.inLen = XSTRLEN(a.input);
  12300. ExpectIntEQ(wc_Sha512_224Update(&sha512, NULL, 0), 0);
  12301. ExpectIntEQ(wc_Sha512_224Update(&sha512,(byte*)a.input, 0), 0);
  12302. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen),
  12303. 0);
  12304. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  12305. /* Update input. */
  12306. a.input = "abc";
  12307. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  12308. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  12309. a.inLen = XSTRLEN(a.input);
  12310. a.outLen = XSTRLEN(a.output);
  12311. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen),
  12312. 0);
  12313. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  12314. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE), 0);
  12315. c.input = NULL;
  12316. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  12317. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12318. BAD_FUNC_ARG);
  12319. ExpectIntEQ(wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen),
  12320. BAD_FUNC_ARG);
  12321. wc_Sha512_224Free(&sha512);
  12322. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12323. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12324. return EXPECT_RESULT();
  12325. }
  12326. static int test_wc_Sha512_224Final(void)
  12327. {
  12328. EXPECT_DECLS;
  12329. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12330. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12331. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0),
  12332. TEST_SUCCESS);
  12333. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12334. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12335. return EXPECT_RESULT();
  12336. }
  12337. static int test_wc_Sha512_224GetFlags(void)
  12338. {
  12339. EXPECT_DECLS;
  12340. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12341. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  12342. wc_Sha512 sha512;
  12343. wc_Sha512 copy;
  12344. word32 flags = 0;
  12345. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12346. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  12347. /* Initialize */
  12348. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12349. ExpectIntEQ(wc_InitSha512_224(&copy), 0);
  12350. ExpectIntEQ(wc_Sha512_224GetFlags(&sha512, &flags), 0);
  12351. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12352. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &copy), 0);
  12353. ExpectIntEQ(wc_Sha512_224GetFlags(&copy, &flags), 0);
  12354. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  12355. wc_Sha512_224Free(&copy);
  12356. wc_Sha512_224Free(&sha512);
  12357. #endif
  12358. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12359. return EXPECT_RESULT();
  12360. }
  12361. static int test_wc_Sha512_224FinalRaw(void)
  12362. {
  12363. EXPECT_DECLS;
  12364. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12365. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  12366. !defined(WOLFSSL_NO_HASH_RAW)
  12367. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1),
  12368. TEST_SUCCESS);
  12369. #endif
  12370. return EXPECT_RESULT();
  12371. }
  12372. static int test_wc_Sha512_224Free(void)
  12373. {
  12374. EXPECT_DECLS;
  12375. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12376. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12377. wc_Sha512_224Free(NULL);
  12378. /* Set result to SUCCESS. */
  12379. ExpectTrue(1);
  12380. #endif
  12381. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12382. return EXPECT_RESULT();
  12383. }
  12384. static int test_wc_Sha512_224GetHash(void)
  12385. {
  12386. EXPECT_DECLS;
  12387. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12388. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12389. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224),
  12390. TEST_SUCCESS);
  12391. #endif
  12392. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12393. return EXPECT_RESULT();
  12394. }
  12395. static int test_wc_Sha512_224Copy(void)
  12396. {
  12397. EXPECT_DECLS;
  12398. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12399. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12400. wc_Sha512 sha512;
  12401. wc_Sha512 temp;
  12402. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12403. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12404. /* Initialize */
  12405. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12406. ExpectIntEQ(wc_InitSha512_224(&temp), 0);
  12407. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &temp), 0);
  12408. /* test bad arguments*/
  12409. ExpectIntEQ(wc_Sha512_224Copy(NULL, NULL), BAD_FUNC_ARG);
  12410. ExpectIntEQ(wc_Sha512_224Copy(NULL, &temp), BAD_FUNC_ARG);
  12411. ExpectIntEQ(wc_Sha512_224Copy(&sha512, NULL), BAD_FUNC_ARG);
  12412. wc_Sha512_224Free(&sha512);
  12413. wc_Sha512_224Free(&temp);
  12414. #endif
  12415. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12416. return EXPECT_RESULT();
  12417. }
  12418. static int test_wc_InitSha512_256(void)
  12419. {
  12420. EXPECT_DECLS;
  12421. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12422. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12423. wc_Sha512 sha512;
  12424. /* Test good arg. */
  12425. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12426. /* Test bad arg. */
  12427. ExpectIntEQ(wc_InitSha512_256(NULL), BAD_FUNC_ARG);
  12428. wc_Sha512_256Free(&sha512);
  12429. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12430. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12431. return EXPECT_RESULT();
  12432. }
  12433. static int test_wc_Sha512_256Update(void)
  12434. {
  12435. EXPECT_DECLS;
  12436. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12437. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12438. wc_Sha512 sha512;
  12439. byte hash[WC_SHA512_DIGEST_SIZE];
  12440. testVector a, c;
  12441. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12442. /* Input. */
  12443. a.input = "a";
  12444. a.inLen = XSTRLEN(a.input);
  12445. ExpectIntEQ(wc_Sha512_256Update(&sha512, NULL, 0), 0);
  12446. ExpectIntEQ(wc_Sha512_256Update(&sha512,(byte*)a.input, 0), 0);
  12447. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen),
  12448. 0);
  12449. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  12450. /* Update input. */
  12451. a.input = "abc";
  12452. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  12453. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  12454. "\x07\xe7\xaf\x23";
  12455. a.inLen = XSTRLEN(a.input);
  12456. a.outLen = XSTRLEN(a.output);
  12457. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen),
  12458. 0);
  12459. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  12460. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE), 0);
  12461. c.input = NULL;
  12462. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  12463. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12464. BAD_FUNC_ARG);
  12465. ExpectIntEQ(wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen),
  12466. BAD_FUNC_ARG);
  12467. wc_Sha512_256Free(&sha512);
  12468. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12469. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12470. return EXPECT_RESULT();
  12471. }
  12472. static int test_wc_Sha512_256Final(void)
  12473. {
  12474. EXPECT_DECLS;
  12475. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12476. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12477. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0),
  12478. TEST_SUCCESS);
  12479. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12480. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12481. return EXPECT_RESULT();
  12482. }
  12483. static int test_wc_Sha512_256GetFlags(void)
  12484. {
  12485. EXPECT_DECLS;
  12486. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12487. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  12488. wc_Sha512 sha512, copy;
  12489. word32 flags = 0;
  12490. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12491. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  12492. /* Initialize */
  12493. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12494. ExpectIntEQ(wc_InitSha512_256(&copy), 0);
  12495. ExpectIntEQ(wc_Sha512_256GetFlags(&sha512, &flags), 0);
  12496. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12497. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &copy), 0);
  12498. ExpectIntEQ(wc_Sha512_256GetFlags(&copy, &flags), 0);
  12499. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  12500. wc_Sha512_256Free(&sha512);
  12501. #endif
  12502. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12503. return EXPECT_RESULT();
  12504. }
  12505. static int test_wc_Sha512_256FinalRaw(void)
  12506. {
  12507. EXPECT_DECLS;
  12508. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12509. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  12510. !defined(WOLFSSL_NO_HASH_RAW)
  12511. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1),
  12512. TEST_SUCCESS);
  12513. #endif
  12514. return EXPECT_RESULT();
  12515. }
  12516. static int test_wc_Sha512_256Free(void)
  12517. {
  12518. EXPECT_DECLS;
  12519. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12520. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12521. wc_Sha512_256Free(NULL);
  12522. /* Set result to SUCCESS. */
  12523. ExpectTrue(1);
  12524. #endif
  12525. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12526. return EXPECT_RESULT();
  12527. }
  12528. static int test_wc_Sha512_256GetHash(void)
  12529. {
  12530. EXPECT_DECLS;
  12531. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12532. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12533. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256),
  12534. TEST_SUCCESS);
  12535. #endif
  12536. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12537. return EXPECT_RESULT();
  12538. }
  12539. static int test_wc_Sha512_256Copy(void)
  12540. {
  12541. EXPECT_DECLS;
  12542. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12543. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12544. wc_Sha512 sha512;
  12545. wc_Sha512 temp;
  12546. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12547. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12548. /* Initialize */
  12549. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12550. ExpectIntEQ(wc_InitSha512_256(&temp), 0);
  12551. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &temp), 0);
  12552. /* test bad arguments*/
  12553. ExpectIntEQ(wc_Sha512_256Copy(NULL, NULL), BAD_FUNC_ARG);
  12554. ExpectIntEQ(wc_Sha512_256Copy(NULL, &temp), BAD_FUNC_ARG);
  12555. ExpectIntEQ(wc_Sha512_256Copy(&sha512, NULL), BAD_FUNC_ARG);
  12556. wc_Sha512_256Free(&sha512);
  12557. wc_Sha512_256Free(&temp);
  12558. #endif
  12559. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12560. return EXPECT_RESULT();
  12561. }
  12562. /*
  12563. * Testing wc_InitSha384()
  12564. */
  12565. static int test_wc_InitSha384(void)
  12566. {
  12567. EXPECT_DECLS;
  12568. #ifdef WOLFSSL_SHA384
  12569. wc_Sha384 sha384;
  12570. /* Test good arg. */
  12571. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12572. /* Test bad arg. */
  12573. ExpectIntEQ(wc_InitSha384(NULL), BAD_FUNC_ARG);
  12574. wc_Sha384Free(&sha384);
  12575. #endif
  12576. return EXPECT_RESULT();
  12577. } /* END test_wc_InitSha384 */
  12578. /*
  12579. * test wc_Sha384Update()
  12580. */
  12581. static int test_wc_Sha384Update(void)
  12582. {
  12583. EXPECT_DECLS;
  12584. #ifdef WOLFSSL_SHA384
  12585. wc_Sha384 sha384;
  12586. byte hash[WC_SHA384_DIGEST_SIZE];
  12587. testVector a, b, c;
  12588. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12589. /* Input */
  12590. a.input = "a";
  12591. a.inLen = XSTRLEN(a.input);
  12592. ExpectIntEQ(wc_Sha384Update(&sha384, NULL, 0), 0);
  12593. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, 0), 0);
  12594. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  12595. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  12596. /* Update input. */
  12597. a.input = "abc";
  12598. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  12599. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  12600. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  12601. "\xc8\x25\xa7";
  12602. a.inLen = XSTRLEN(a.input);
  12603. a.outLen = XSTRLEN(a.output);
  12604. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  12605. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  12606. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  12607. /* Pass in bad values. */
  12608. b.input = NULL;
  12609. b.inLen = 0;
  12610. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen), 0);
  12611. c.input = NULL;
  12612. c.inLen = WC_SHA384_DIGEST_SIZE;
  12613. ExpectIntEQ( wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen),
  12614. BAD_FUNC_ARG);
  12615. ExpectIntEQ(wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen),
  12616. BAD_FUNC_ARG);
  12617. wc_Sha384Free(&sha384);
  12618. #endif
  12619. return EXPECT_RESULT();
  12620. } /* END test_wc_Sha384Update */
  12621. /*
  12622. * Unit test function for wc_Sha384Final();
  12623. */
  12624. static int test_wc_Sha384Final(void)
  12625. {
  12626. EXPECT_DECLS;
  12627. #ifdef WOLFSSL_SHA384
  12628. wc_Sha384 sha384;
  12629. byte* hash_test[3];
  12630. byte hash1[WC_SHA384_DIGEST_SIZE];
  12631. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  12632. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  12633. int times, i;
  12634. /* Initialize */
  12635. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12636. hash_test[0] = hash1;
  12637. hash_test[1] = hash2;
  12638. hash_test[2] = hash3;
  12639. times = sizeof(hash_test) / sizeof(byte*);
  12640. /* Good test args. */
  12641. for (i = 0; i < times; i++) {
  12642. ExpectIntEQ(wc_Sha384Final(&sha384, hash_test[i]), 0);
  12643. }
  12644. /* Test bad args. */
  12645. ExpectIntEQ(wc_Sha384Final(NULL, NULL), BAD_FUNC_ARG);
  12646. ExpectIntEQ(wc_Sha384Final(NULL, hash1), BAD_FUNC_ARG);
  12647. ExpectIntEQ(wc_Sha384Final(&sha384, NULL), BAD_FUNC_ARG);
  12648. wc_Sha384Free(&sha384);
  12649. #endif
  12650. return EXPECT_RESULT();
  12651. } /* END test_wc_Sha384Final */
  12652. /*
  12653. * Unit test function for wc_Sha384GetFlags()
  12654. */
  12655. static int test_wc_Sha384GetFlags(void)
  12656. {
  12657. EXPECT_DECLS;
  12658. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  12659. wc_Sha384 sha384;
  12660. word32 flags = 0;
  12661. /* Initialize */
  12662. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12663. ExpectIntEQ(wc_Sha384GetFlags(&sha384, &flags), 0);
  12664. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12665. wc_Sha384Free(&sha384);
  12666. #endif
  12667. return EXPECT_RESULT();
  12668. } /* END test_wc_Sha384GetFlags */
  12669. /*
  12670. * Unit test function for wc_Sha384FinalRaw()
  12671. */
  12672. static int test_wc_Sha384FinalRaw(void)
  12673. {
  12674. EXPECT_DECLS;
  12675. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  12676. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  12677. !defined(WOLFSSL_NO_HASH_RAW)
  12678. wc_Sha384 sha384;
  12679. byte* hash_test[3];
  12680. byte hash1[WC_SHA384_DIGEST_SIZE];
  12681. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  12682. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  12683. int times, i;
  12684. /* Initialize */
  12685. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12686. hash_test[0] = hash1;
  12687. hash_test[1] = hash2;
  12688. hash_test[2] = hash3;
  12689. times = sizeof(hash_test) / sizeof(byte*);
  12690. /* Good test args. */
  12691. for (i = 0; i < times; i++) {
  12692. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, hash_test[i]), 0);
  12693. }
  12694. /* Test bad args. */
  12695. ExpectIntEQ(wc_Sha384FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12696. ExpectIntEQ(wc_Sha384FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12697. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, NULL), BAD_FUNC_ARG);
  12698. wc_Sha384Free(&sha384);
  12699. #endif
  12700. return EXPECT_RESULT();
  12701. } /* END test_wc_Sha384FinalRaw */
  12702. /*
  12703. * Unit test function for wc_Sha384Free()
  12704. */
  12705. static int test_wc_Sha384Free(void)
  12706. {
  12707. EXPECT_DECLS;
  12708. #ifdef WOLFSSL_SHA384
  12709. wc_Sha384Free(NULL);
  12710. /* Set result to SUCCESS. */
  12711. ExpectTrue(1);
  12712. #endif
  12713. return EXPECT_RESULT();
  12714. } /* END test_wc_Sha384Free */
  12715. /*
  12716. * Unit test function for wc_Sha384GetHash()
  12717. */
  12718. static int test_wc_Sha384GetHash(void)
  12719. {
  12720. EXPECT_DECLS;
  12721. #ifdef WOLFSSL_SHA384
  12722. wc_Sha384 sha384;
  12723. byte hash1[WC_SHA384_DIGEST_SIZE];
  12724. /* Initialize */
  12725. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12726. ExpectIntEQ(wc_Sha384GetHash(&sha384, hash1), 0);
  12727. /* test bad arguments*/
  12728. ExpectIntEQ(wc_Sha384GetHash(NULL, NULL), BAD_FUNC_ARG);
  12729. ExpectIntEQ(wc_Sha384GetHash(NULL, hash1), BAD_FUNC_ARG);
  12730. ExpectIntEQ(wc_Sha384GetHash(&sha384, NULL), BAD_FUNC_ARG);
  12731. wc_Sha384Free(&sha384);
  12732. #endif
  12733. return EXPECT_RESULT();
  12734. } /* END test_wc_Sha384GetHash */
  12735. /*
  12736. * Unit test function for wc_Sha384Copy()
  12737. */
  12738. static int test_wc_Sha384Copy(void)
  12739. {
  12740. EXPECT_DECLS;
  12741. #ifdef WOLFSSL_SHA384
  12742. wc_Sha384 sha384;
  12743. wc_Sha384 temp;
  12744. XMEMSET(&sha384, 0, sizeof(wc_Sha384));
  12745. XMEMSET(&temp, 0, sizeof(wc_Sha384));
  12746. /* Initialize */
  12747. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12748. ExpectIntEQ(wc_InitSha384(&temp), 0);
  12749. ExpectIntEQ(wc_Sha384Copy(&sha384, &temp), 0);
  12750. /* test bad arguments*/
  12751. ExpectIntEQ(wc_Sha384Copy(NULL, NULL), BAD_FUNC_ARG);
  12752. ExpectIntEQ(wc_Sha384Copy(NULL, &temp), BAD_FUNC_ARG);
  12753. ExpectIntEQ(wc_Sha384Copy(&sha384, NULL), BAD_FUNC_ARG);
  12754. wc_Sha384Free(&sha384);
  12755. wc_Sha384Free(&temp);
  12756. #endif
  12757. return EXPECT_RESULT();
  12758. } /* END test_wc_Sha384Copy */
  12759. /*
  12760. * Testing wc_InitSha224();
  12761. */
  12762. static int test_wc_InitSha224(void)
  12763. {
  12764. EXPECT_DECLS;
  12765. #ifdef WOLFSSL_SHA224
  12766. wc_Sha224 sha224;
  12767. /* Test good arg. */
  12768. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12769. /* Test bad arg. */
  12770. ExpectIntEQ(wc_InitSha224(NULL), BAD_FUNC_ARG);
  12771. wc_Sha224Free(&sha224);
  12772. #endif
  12773. return EXPECT_RESULT();
  12774. } /* END test_wc_InitSha224 */
  12775. /*
  12776. * Unit test on wc_Sha224Update
  12777. */
  12778. static int test_wc_Sha224Update(void)
  12779. {
  12780. EXPECT_DECLS;
  12781. #ifdef WOLFSSL_SHA224
  12782. wc_Sha224 sha224;
  12783. byte hash[WC_SHA224_DIGEST_SIZE];
  12784. testVector a, b, c;
  12785. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12786. /* Input. */
  12787. a.input = "a";
  12788. a.inLen = XSTRLEN(a.input);
  12789. ExpectIntEQ(wc_Sha224Update(&sha224, NULL, 0), 0);
  12790. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, 0), 0);
  12791. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  12792. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  12793. /* Update input. */
  12794. a.input = "abc";
  12795. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  12796. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  12797. a.inLen = XSTRLEN(a.input);
  12798. a.outLen = XSTRLEN(a.output);
  12799. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  12800. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  12801. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  12802. /* Pass in bad values. */
  12803. b.input = NULL;
  12804. b.inLen = 0;
  12805. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen), 0);
  12806. c.input = NULL;
  12807. c.inLen = WC_SHA224_DIGEST_SIZE;
  12808. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen),
  12809. BAD_FUNC_ARG);
  12810. ExpectIntEQ(wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen),
  12811. BAD_FUNC_ARG);
  12812. wc_Sha224Free(&sha224);
  12813. #endif
  12814. return EXPECT_RESULT();
  12815. } /* END test_wc_Sha224Update */
  12816. /*
  12817. * Unit test for wc_Sha224Final();
  12818. */
  12819. static int test_wc_Sha224Final(void)
  12820. {
  12821. EXPECT_DECLS;
  12822. #ifdef WOLFSSL_SHA224
  12823. wc_Sha224 sha224;
  12824. byte* hash_test[3];
  12825. byte hash1[WC_SHA224_DIGEST_SIZE];
  12826. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  12827. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  12828. int times, i;
  12829. /* Initialize */
  12830. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12831. hash_test[0] = hash1;
  12832. hash_test[1] = hash2;
  12833. hash_test[2] = hash3;
  12834. times = sizeof(hash_test) / sizeof(byte*);
  12835. /* Good test args. */
  12836. /* Testing oversized buffers. */
  12837. for (i = 0; i < times; i++) {
  12838. ExpectIntEQ(wc_Sha224Final(&sha224, hash_test[i]), 0);
  12839. }
  12840. /* Test bad args. */
  12841. ExpectIntEQ(wc_Sha224Final(NULL, NULL), BAD_FUNC_ARG);
  12842. ExpectIntEQ(wc_Sha224Final(NULL, hash1), BAD_FUNC_ARG);
  12843. ExpectIntEQ(wc_Sha224Final(&sha224, NULL), BAD_FUNC_ARG);
  12844. wc_Sha224Free(&sha224);
  12845. #endif
  12846. return EXPECT_RESULT();
  12847. } /* END test_wc_Sha224Final */
  12848. /*
  12849. * Unit test function for wc_Sha224SetFlags()
  12850. */
  12851. static int test_wc_Sha224SetFlags(void)
  12852. {
  12853. EXPECT_DECLS;
  12854. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  12855. wc_Sha224 sha224;
  12856. word32 flags = WC_HASH_FLAG_WILLCOPY;
  12857. /* Initialize */
  12858. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12859. ExpectIntEQ(wc_Sha224SetFlags(&sha224, flags), 0);
  12860. flags = 0;
  12861. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  12862. ExpectTrue(flags == WC_HASH_FLAG_WILLCOPY);
  12863. wc_Sha224Free(&sha224);
  12864. #endif
  12865. return EXPECT_RESULT();
  12866. } /* END test_wc_Sha224SetFlags */
  12867. /*
  12868. * Unit test function for wc_Sha224GetFlags()
  12869. */
  12870. static int test_wc_Sha224GetFlags(void)
  12871. {
  12872. EXPECT_DECLS;
  12873. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  12874. wc_Sha224 sha224;
  12875. word32 flags = 0;
  12876. /* Initialize */
  12877. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12878. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  12879. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12880. wc_Sha224Free(&sha224);
  12881. #endif
  12882. return EXPECT_RESULT();
  12883. } /* END test_wc_Sha224GetFlags */
  12884. /*
  12885. * Unit test function for wc_Sha224Free()
  12886. */
  12887. static int test_wc_Sha224Free(void)
  12888. {
  12889. EXPECT_DECLS;
  12890. #ifdef WOLFSSL_SHA224
  12891. wc_Sha224Free(NULL);
  12892. /* Set result to SUCCESS. */
  12893. ExpectTrue(1);
  12894. #endif
  12895. return EXPECT_RESULT();
  12896. } /* END test_wc_Sha224Free */
  12897. /*
  12898. * Unit test function for wc_Sha224GetHash()
  12899. */
  12900. static int test_wc_Sha224GetHash(void)
  12901. {
  12902. EXPECT_DECLS;
  12903. #ifdef WOLFSSL_SHA224
  12904. wc_Sha224 sha224;
  12905. byte hash1[WC_SHA224_DIGEST_SIZE];
  12906. /* Initialize */
  12907. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12908. ExpectIntEQ(wc_Sha224GetHash(&sha224, hash1), 0);
  12909. /* test bad arguments*/
  12910. ExpectIntEQ(wc_Sha224GetHash(NULL, NULL), BAD_FUNC_ARG);
  12911. ExpectIntEQ(wc_Sha224GetHash(NULL, hash1), BAD_FUNC_ARG);
  12912. ExpectIntEQ(wc_Sha224GetHash(&sha224, NULL), BAD_FUNC_ARG);
  12913. wc_Sha224Free(&sha224);
  12914. #endif
  12915. return EXPECT_RESULT();
  12916. } /* END test_wc_Sha224GetHash */
  12917. /*
  12918. * Unit test function for wc_Sha224Copy()
  12919. */
  12920. static int test_wc_Sha224Copy(void)
  12921. {
  12922. EXPECT_DECLS;
  12923. #ifdef WOLFSSL_SHA224
  12924. wc_Sha224 sha224;
  12925. wc_Sha224 temp;
  12926. XMEMSET(&sha224, 0, sizeof(wc_Sha224));
  12927. XMEMSET(&temp, 0, sizeof(wc_Sha224));
  12928. /* Initialize */
  12929. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12930. ExpectIntEQ(wc_InitSha224(&temp), 0);
  12931. ExpectIntEQ(wc_Sha224Copy(&sha224, &temp), 0);
  12932. /* test bad arguments*/
  12933. ExpectIntEQ(wc_Sha224Copy(NULL, NULL), BAD_FUNC_ARG);
  12934. ExpectIntEQ(wc_Sha224Copy(NULL, &temp), BAD_FUNC_ARG);
  12935. ExpectIntEQ(wc_Sha224Copy(&sha224, NULL), BAD_FUNC_ARG);
  12936. wc_Sha224Free(&sha224);
  12937. wc_Sha224Free(&temp);
  12938. #endif
  12939. return EXPECT_RESULT();
  12940. } /* END test_wc_Sha224Copy */
  12941. /*
  12942. * Testing wc_InitRipeMd()
  12943. */
  12944. static int test_wc_InitRipeMd(void)
  12945. {
  12946. EXPECT_DECLS;
  12947. #ifdef WOLFSSL_RIPEMD
  12948. RipeMd ripemd;
  12949. /* Test good arg. */
  12950. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  12951. /* Test bad arg. */
  12952. ExpectIntEQ(wc_InitRipeMd(NULL), BAD_FUNC_ARG);
  12953. #endif
  12954. return EXPECT_RESULT();
  12955. } /* END test_wc_InitRipeMd */
  12956. /*
  12957. * Testing wc_RipeMdUpdate()
  12958. */
  12959. static int test_wc_RipeMdUpdate(void)
  12960. {
  12961. EXPECT_DECLS;
  12962. #ifdef WOLFSSL_RIPEMD
  12963. RipeMd ripemd;
  12964. byte hash[RIPEMD_DIGEST_SIZE];
  12965. testVector a, b, c;
  12966. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  12967. /* Input */
  12968. a.input = "a";
  12969. a.inLen = XSTRLEN(a.input);
  12970. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  12971. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  12972. /* Update input. */
  12973. a.input = "abc";
  12974. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  12975. "\xb0\x87\xf1\x5a\x0b\xfc";
  12976. a.inLen = XSTRLEN(a.input);
  12977. a.outLen = XSTRLEN(a.output);
  12978. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  12979. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  12980. ExpectIntEQ(XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE), 0);
  12981. /* Pass in bad values. */
  12982. b.input = NULL;
  12983. b.inLen = 0;
  12984. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen), 0);
  12985. c.input = NULL;
  12986. c.inLen = RIPEMD_DIGEST_SIZE;
  12987. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen),
  12988. BAD_FUNC_ARG);
  12989. ExpectIntEQ(wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  12990. BAD_FUNC_ARG);
  12991. #endif
  12992. return EXPECT_RESULT();
  12993. } /* END test_wc_RipeMdUdpate */
  12994. /*
  12995. * Unit test function for wc_RipeMdFinal()
  12996. */
  12997. static int test_wc_RipeMdFinal(void)
  12998. {
  12999. EXPECT_DECLS;
  13000. #ifdef WOLFSSL_RIPEMD
  13001. RipeMd ripemd;
  13002. byte* hash_test[3];
  13003. byte hash1[RIPEMD_DIGEST_SIZE];
  13004. byte hash2[2*RIPEMD_DIGEST_SIZE];
  13005. byte hash3[5*RIPEMD_DIGEST_SIZE];
  13006. int times, i;
  13007. /* Initialize */
  13008. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  13009. hash_test[0] = hash1;
  13010. hash_test[1] = hash2;
  13011. hash_test[2] = hash3;
  13012. times = sizeof(hash_test) / sizeof(byte*);
  13013. /* Testing oversized buffers. */
  13014. for (i = 0; i < times; i++) {
  13015. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash_test[i]), 0);
  13016. }
  13017. /* Test bad args. */
  13018. ExpectIntEQ(wc_RipeMdFinal(NULL, NULL), BAD_FUNC_ARG);
  13019. ExpectIntEQ(wc_RipeMdFinal(NULL, hash1), BAD_FUNC_ARG);
  13020. ExpectIntEQ(wc_RipeMdFinal(&ripemd, NULL), BAD_FUNC_ARG);
  13021. #endif
  13022. return EXPECT_RESULT();
  13023. } /* END test_wc_RipeMdFinal */
  13024. /*
  13025. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  13026. * wc_InitSha3_512
  13027. */
  13028. static int test_wc_InitSha3(void)
  13029. {
  13030. EXPECT_DECLS;
  13031. #if defined(WOLFSSL_SHA3)
  13032. wc_Sha3 sha3;
  13033. (void)sha3;
  13034. #if !defined(WOLFSSL_NOSHA3_224)
  13035. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13036. /* Test bad args. */
  13037. ExpectIntEQ(wc_InitSha3_224(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13038. wc_Sha3_224_Free(&sha3);
  13039. #endif /* NOSHA3_224 */
  13040. #if !defined(WOLFSSL_NOSHA3_256)
  13041. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13042. /* Test bad args. */
  13043. ExpectIntEQ(wc_InitSha3_256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13044. wc_Sha3_256_Free(&sha3);
  13045. #endif /* NOSHA3_256 */
  13046. #if !defined(WOLFSSL_NOSHA3_384)
  13047. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13048. /* Test bad args. */
  13049. ExpectIntEQ(wc_InitSha3_384(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13050. wc_Sha3_384_Free(&sha3);
  13051. #endif /* NOSHA3_384 */
  13052. #if !defined(WOLFSSL_NOSHA3_512)
  13053. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13054. /* Test bad args. */
  13055. ExpectIntEQ(wc_InitSha3_512(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13056. wc_Sha3_512_Free(&sha3);
  13057. #endif /* NOSHA3_512 */
  13058. #endif
  13059. return EXPECT_RESULT();
  13060. } /* END test_wc_InitSha3 */
  13061. /*
  13062. * Testing wc_Sha3_Update()
  13063. */
  13064. static int testing_wc_Sha3_Update(void)
  13065. {
  13066. EXPECT_DECLS;
  13067. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  13068. !defined(WOLFSSL_AFALG_XILINX)
  13069. wc_Sha3 sha3;
  13070. byte msg[] = "Everybody's working for the weekend.";
  13071. byte msg2[] = "Everybody gets Friday off.";
  13072. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  13073. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  13074. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  13075. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  13076. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  13077. word32 msglen = sizeof(msg) - 1;
  13078. word32 msg2len = sizeof(msg2);
  13079. word32 msgCmplen = sizeof(msgCmp);
  13080. #if !defined(WOLFSSL_NOSHA3_224)
  13081. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13082. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg, msglen), 0);
  13083. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13084. ExpectTrue(sha3.i == msglen);
  13085. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  13086. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13087. /* Pass bad args. */
  13088. ExpectIntEQ(wc_Sha3_224_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13089. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13090. wc_Sha3_224_Free(&sha3);
  13091. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13092. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 0), 0);
  13093. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  13094. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13095. wc_Sha3_224_Free(&sha3);
  13096. #endif /* SHA3_224 */
  13097. #if !defined(WOLFSSL_NOSHA3_256)
  13098. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13099. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg, msglen), 0);
  13100. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13101. ExpectTrue(sha3.i == msglen);
  13102. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  13103. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13104. /* Pass bad args. */
  13105. ExpectIntEQ(wc_Sha3_256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13106. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13107. wc_Sha3_256_Free(&sha3);
  13108. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13109. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 0), 0);
  13110. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  13111. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13112. wc_Sha3_256_Free(&sha3);
  13113. #endif /* SHA3_256 */
  13114. #if !defined(WOLFSSL_NOSHA3_384)
  13115. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13116. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg, msglen), 0);
  13117. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13118. ExpectTrue(sha3.i == msglen);
  13119. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  13120. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13121. /* Pass bad args. */
  13122. ExpectIntEQ(wc_Sha3_384_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13123. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13124. wc_Sha3_384_Free(&sha3);
  13125. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13126. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 0), 0);
  13127. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  13128. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13129. wc_Sha3_384_Free(&sha3);
  13130. #endif /* SHA3_384 */
  13131. #if !defined(WOLFSSL_NOSHA3_512)
  13132. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13133. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg, msglen), 0);
  13134. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13135. ExpectTrue(sha3.i == msglen);
  13136. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  13137. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13138. /* Pass bad args. */
  13139. ExpectIntEQ(wc_Sha3_512_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13140. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13141. wc_Sha3_512_Free(&sha3);
  13142. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13143. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 0), 0);
  13144. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  13145. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13146. wc_Sha3_512_Free(&sha3);
  13147. #endif /* SHA3_512 */
  13148. #endif /* WOLFSSL_SHA3 */
  13149. return EXPECT_RESULT();
  13150. } /* END testing_wc_Sha3_Update */
  13151. /*
  13152. * Testing wc_Sha3_224_Final()
  13153. */
  13154. static int test_wc_Sha3_224_Final(void)
  13155. {
  13156. EXPECT_DECLS;
  13157. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  13158. wc_Sha3 sha3;
  13159. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13160. "nopnopq";
  13161. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  13162. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  13163. "\x64\xea\xd0\xfc\xce\x33";
  13164. byte hash[WC_SHA3_224_DIGEST_SIZE];
  13165. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  13166. /* Init stack variables. */
  13167. XMEMSET(hash, 0, sizeof(hash));
  13168. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13169. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13170. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13171. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE), 0);
  13172. /* Test bad args. */
  13173. ExpectIntEQ(wc_Sha3_224_Final(NULL, hash), BAD_FUNC_ARG);
  13174. ExpectIntEQ(wc_Sha3_224_Final(&sha3, NULL), BAD_FUNC_ARG);
  13175. wc_Sha3_224_Free(&sha3);
  13176. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13177. /* Init stack variables. */
  13178. XMEMSET(hash, 0, sizeof(hash));
  13179. XMEMSET(hashRet, 0, sizeof(hashRet));
  13180. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13181. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, hashRet), 0);
  13182. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13183. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE), 0);
  13184. /* Test bad args. */
  13185. ExpectIntEQ(wc_Sha3_224_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13186. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13187. wc_Sha3_224_Free(&sha3);
  13188. #endif
  13189. return EXPECT_RESULT();
  13190. } /* END test_wc_Sha3_224_Final */
  13191. /*
  13192. * Testing wc_Sha3_256_Final()
  13193. */
  13194. static int test_wc_Sha3_256_Final(void)
  13195. {
  13196. EXPECT_DECLS;
  13197. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  13198. wc_Sha3 sha3;
  13199. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13200. "nopnopq";
  13201. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  13202. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  13203. "\xdd\x97\x49\x6d\x33\x76";
  13204. byte hash[WC_SHA3_256_DIGEST_SIZE];
  13205. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  13206. /* Init stack variables. */
  13207. XMEMSET(hash, 0, sizeof(hash));
  13208. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13209. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13210. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13211. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE), 0);
  13212. /* Test bad args. */
  13213. ExpectIntEQ(wc_Sha3_256_Final(NULL, hash), BAD_FUNC_ARG);
  13214. ExpectIntEQ(wc_Sha3_256_Final(&sha3, NULL), BAD_FUNC_ARG);
  13215. wc_Sha3_256_Free(&sha3);
  13216. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13217. /* Init stack variables. */
  13218. XMEMSET(hash, 0, sizeof(hash));
  13219. XMEMSET(hashRet, 0, sizeof(hashRet));
  13220. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13221. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, hashRet), 0);
  13222. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13223. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE), 0);
  13224. /* Test bad args. */
  13225. ExpectIntEQ(wc_Sha3_256_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13226. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13227. wc_Sha3_256_Free(&sha3);
  13228. #endif
  13229. return EXPECT_RESULT();
  13230. } /* END test_wc_Sha3_256_Final */
  13231. /*
  13232. * Testing wc_Sha3_384_Final()
  13233. */
  13234. static int test_wc_Sha3_384_Final(void)
  13235. {
  13236. EXPECT_DECLS;
  13237. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  13238. wc_Sha3 sha3;
  13239. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13240. "nopnopq";
  13241. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  13242. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  13243. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  13244. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  13245. byte hash[WC_SHA3_384_DIGEST_SIZE];
  13246. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  13247. /* Init stack variables. */
  13248. XMEMSET(hash, 0, sizeof(hash));
  13249. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13250. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13251. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13252. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE), 0);
  13253. /* Test bad args. */
  13254. ExpectIntEQ(wc_Sha3_384_Final(NULL, hash), BAD_FUNC_ARG);
  13255. ExpectIntEQ(wc_Sha3_384_Final(&sha3, NULL), BAD_FUNC_ARG);
  13256. wc_Sha3_384_Free(&sha3);
  13257. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13258. /* Init stack variables. */
  13259. XMEMSET(hash, 0, sizeof(hash));
  13260. XMEMSET(hashRet, 0, sizeof(hashRet));
  13261. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13262. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, hashRet), 0);
  13263. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13264. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE), 0);
  13265. /* Test bad args. */
  13266. ExpectIntEQ(wc_Sha3_384_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13267. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13268. wc_Sha3_384_Free(&sha3);
  13269. #endif
  13270. return EXPECT_RESULT();
  13271. } /* END test_wc_Sha3_384_Final */
  13272. /*
  13273. * Testing wc_Sha3_512_Final()
  13274. */
  13275. static int test_wc_Sha3_512_Final(void)
  13276. {
  13277. EXPECT_DECLS;
  13278. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  13279. !defined(WOLFSSL_NOSHA3_384)
  13280. wc_Sha3 sha3;
  13281. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13282. "nopnopq";
  13283. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  13284. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  13285. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  13286. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  13287. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  13288. byte hash[WC_SHA3_512_DIGEST_SIZE];
  13289. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  13290. /* Init stack variables. */
  13291. XMEMSET(hash, 0, sizeof(hash));
  13292. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13293. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13294. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13295. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE), 0);
  13296. /* Test bad args. */
  13297. ExpectIntEQ(wc_Sha3_512_Final(NULL, hash), BAD_FUNC_ARG);
  13298. ExpectIntEQ(wc_Sha3_512_Final(&sha3, NULL), BAD_FUNC_ARG);
  13299. wc_Sha3_512_Free(&sha3);
  13300. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13301. /* Init stack variables. */
  13302. XMEMSET(hash, 0, sizeof(hash));
  13303. XMEMSET(hashRet, 0, sizeof(hashRet));
  13304. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13305. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, hashRet), 0);
  13306. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13307. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE), 0);
  13308. /* Test bad args. */
  13309. ExpectIntEQ(wc_Sha3_512_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13310. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13311. wc_Sha3_512_Free(&sha3);
  13312. #endif
  13313. return EXPECT_RESULT();
  13314. } /* END test_wc_Sha3_512_Final */
  13315. /*
  13316. * Testing wc_Sha3_224_Copy()
  13317. */
  13318. static int test_wc_Sha3_224_Copy(void)
  13319. {
  13320. EXPECT_DECLS;
  13321. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  13322. wc_Sha3 sha3, sha3Cpy;
  13323. const char* msg = TEST_STRING;
  13324. word32 msglen = (word32)TEST_STRING_SZ;
  13325. byte hash[WC_SHA3_224_DIGEST_SIZE];
  13326. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  13327. XMEMSET(hash, 0, sizeof(hash));
  13328. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13329. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13330. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13331. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13332. ExpectIntEQ(wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13333. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, msglen), 0);
  13334. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, &sha3), 0);
  13335. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13336. ExpectIntEQ(wc_Sha3_224_Final(&sha3Cpy, hashCpy), 0);
  13337. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13338. /* Test bad args. */
  13339. ExpectIntEQ(wc_Sha3_224_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13340. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13341. wc_Sha3_224_Free(&sha3);
  13342. wc_Sha3_224_Free(&sha3Cpy);
  13343. #endif
  13344. return EXPECT_RESULT();
  13345. } /* END test_wc_Sha3_224_Copy */
  13346. /*
  13347. * Testing wc_Sha3_256_Copy()
  13348. */
  13349. static int test_wc_Sha3_256_Copy(void)
  13350. {
  13351. EXPECT_DECLS;
  13352. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  13353. wc_Sha3 sha3, sha3Cpy;
  13354. const char* msg = TEST_STRING;
  13355. word32 msglen = (word32)TEST_STRING_SZ;
  13356. byte hash[WC_SHA3_256_DIGEST_SIZE];
  13357. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  13358. XMEMSET(hash, 0, sizeof(hash));
  13359. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13360. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13361. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13362. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13363. ExpectIntEQ(wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13364. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, msglen), 0);
  13365. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, &sha3), 0);
  13366. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13367. ExpectIntEQ(wc_Sha3_256_Final(&sha3Cpy, hashCpy), 0);
  13368. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13369. /* Test bad args. */
  13370. ExpectIntEQ(wc_Sha3_256_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13371. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13372. wc_Sha3_256_Free(&sha3);
  13373. wc_Sha3_256_Free(&sha3Cpy);
  13374. #endif
  13375. return EXPECT_RESULT();
  13376. } /* END test_wc_Sha3_256_Copy */
  13377. /*
  13378. * Testing wc_Sha3_384_Copy()
  13379. */
  13380. static int test_wc_Sha3_384_Copy(void)
  13381. {
  13382. EXPECT_DECLS;
  13383. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  13384. wc_Sha3 sha3, sha3Cpy;
  13385. const char* msg = TEST_STRING;
  13386. word32 msglen = (word32)TEST_STRING_SZ;
  13387. byte hash[WC_SHA3_384_DIGEST_SIZE];
  13388. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  13389. XMEMSET(hash, 0, sizeof(hash));
  13390. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13391. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13392. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13393. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13394. ExpectIntEQ(wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13395. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, msglen), 0);
  13396. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, &sha3), 0);
  13397. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13398. ExpectIntEQ(wc_Sha3_384_Final(&sha3Cpy, hashCpy), 0);
  13399. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13400. /* Test bad args. */
  13401. ExpectIntEQ(wc_Sha3_384_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13402. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13403. wc_Sha3_384_Free(&sha3);
  13404. wc_Sha3_384_Free(&sha3Cpy);
  13405. #endif
  13406. return EXPECT_RESULT();
  13407. } /* END test_wc_Sha3_384_Copy */
  13408. /*
  13409. * Testing wc_Sha3_512_Copy()
  13410. */
  13411. static int test_wc_Sha3_512_Copy(void)
  13412. {
  13413. EXPECT_DECLS;
  13414. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  13415. wc_Sha3 sha3, sha3Cpy;
  13416. const char* msg = TEST_STRING;
  13417. word32 msglen = (word32)TEST_STRING_SZ;
  13418. byte hash[WC_SHA3_512_DIGEST_SIZE];
  13419. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  13420. XMEMSET(hash, 0, sizeof(hash));
  13421. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13422. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13423. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13424. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13425. ExpectIntEQ(wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13426. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, msglen), 0);
  13427. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, &sha3), 0);
  13428. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13429. ExpectIntEQ(wc_Sha3_512_Final(&sha3Cpy, hashCpy), 0);
  13430. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13431. /* Test bad args. */
  13432. ExpectIntEQ(wc_Sha3_512_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13433. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13434. wc_Sha3_512_Free(&sha3);
  13435. wc_Sha3_512_Free(&sha3Cpy);
  13436. #endif
  13437. return EXPECT_RESULT();
  13438. } /* END test_wc_Sha3_512_Copy */
  13439. /*
  13440. * Unit test function for wc_Sha3_GetFlags()
  13441. */
  13442. static int test_wc_Sha3_GetFlags(void)
  13443. {
  13444. EXPECT_DECLS;
  13445. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  13446. wc_Sha3 sha3;
  13447. word32 flags = 0;
  13448. /* Initialize */
  13449. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13450. ExpectIntEQ(wc_Sha3_GetFlags(&sha3, &flags), 0);
  13451. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  13452. wc_Sha3_224_Free(&sha3);
  13453. #endif
  13454. return EXPECT_RESULT();
  13455. } /* END test_wc_Sha3_GetFlags */
  13456. static int test_wc_InitShake256(void)
  13457. {
  13458. EXPECT_DECLS;
  13459. #ifdef WOLFSSL_SHAKE256
  13460. wc_Shake shake;
  13461. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13462. /* Test bad args. */
  13463. ExpectIntEQ(wc_InitShake256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13464. wc_Shake256_Free(&shake);
  13465. #endif
  13466. return EXPECT_RESULT();
  13467. }
  13468. static int testing_wc_Shake256_Update(void)
  13469. {
  13470. EXPECT_DECLS;
  13471. #ifdef WOLFSSL_SHAKE256
  13472. wc_Shake shake;
  13473. byte msg[] = "Everybody's working for the weekend.";
  13474. byte msg2[] = "Everybody gets Friday off.";
  13475. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  13476. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  13477. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  13478. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  13479. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  13480. word32 msglen = sizeof(msg) - 1;
  13481. word32 msg2len = sizeof(msg2);
  13482. word32 msgCmplen = sizeof(msgCmp);
  13483. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13484. ExpectIntEQ(wc_Shake256_Update(&shake, msg, msglen), 0);
  13485. ExpectIntEQ(XMEMCMP(msg, shake.t, msglen), 0);
  13486. ExpectTrue(shake.i == msglen);
  13487. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  13488. ExpectIntEQ(XMEMCMP(shake.t, msgCmp, msgCmplen), 0);
  13489. /* Pass bad args. */
  13490. ExpectIntEQ(wc_Shake256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13491. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 5), BAD_FUNC_ARG);
  13492. wc_Shake256_Free(&shake);
  13493. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13494. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 0), 0);
  13495. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  13496. ExpectIntEQ(XMEMCMP(msg2, shake.t, msg2len), 0);
  13497. wc_Shake256_Free(&shake);
  13498. #endif /* WOLFSSL_SHAKE256 */
  13499. return EXPECT_RESULT();
  13500. }
  13501. static int test_wc_Shake256_Final(void)
  13502. {
  13503. EXPECT_DECLS;
  13504. #ifdef WOLFSSL_SHAKE256
  13505. wc_Shake shake;
  13506. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13507. "nopnopq";
  13508. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  13509. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  13510. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  13511. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  13512. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  13513. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  13514. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  13515. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  13516. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  13517. byte hash[114];
  13518. /* Init stack variables. */
  13519. XMEMSET(hash, 0, sizeof(hash));
  13520. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13521. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg)),
  13522. 0);
  13523. ExpectIntEQ(wc_Shake256_Final(&shake, hash, (word32)sizeof(hash)), 0);
  13524. ExpectIntEQ(XMEMCMP(expOut, hash, (word32)sizeof(hash)), 0);
  13525. /* Test bad args. */
  13526. ExpectIntEQ(wc_Shake256_Final(NULL, hash, (word32)sizeof(hash)),
  13527. BAD_FUNC_ARG);
  13528. ExpectIntEQ(wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash)),
  13529. BAD_FUNC_ARG);
  13530. wc_Shake256_Free(&shake);
  13531. #endif
  13532. return EXPECT_RESULT();
  13533. }
  13534. /*
  13535. * Testing wc_Shake256_Copy()
  13536. */
  13537. static int test_wc_Shake256_Copy(void)
  13538. {
  13539. EXPECT_DECLS;
  13540. #ifdef WOLFSSL_SHAKE256
  13541. wc_Shake shake, shakeCpy;
  13542. const char* msg = TEST_STRING;
  13543. word32 msglen = (word32)TEST_STRING_SZ;
  13544. byte hash[144];
  13545. byte hashCpy[144];
  13546. word32 hashLen = sizeof(hash);
  13547. word32 hashLenCpy = sizeof(hashCpy);
  13548. XMEMSET(hash, 0, sizeof(hash));
  13549. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13550. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13551. ExpectIntEQ(wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId), 0);
  13552. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, msglen), 0);
  13553. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, &shake), 0);
  13554. ExpectIntEQ(wc_Shake256_Final(&shake, hash, hashLen), 0);
  13555. ExpectIntEQ(wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy), 0);
  13556. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13557. /* Test bad args. */
  13558. ExpectIntEQ(wc_Shake256_Copy(NULL, &shake), BAD_FUNC_ARG);
  13559. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, NULL), BAD_FUNC_ARG);
  13560. wc_Shake256_Free(&shake);
  13561. wc_Shake256_Free(&shakeCpy);
  13562. #endif
  13563. return EXPECT_RESULT();
  13564. } /* END test_wc_Shake256_Copy */
  13565. /*
  13566. * Unit test function for wc_Shake256Hash()
  13567. */
  13568. static int test_wc_Shake256Hash(void)
  13569. {
  13570. EXPECT_DECLS;
  13571. #ifdef WOLFSSL_SHAKE256
  13572. const byte data[] = { /* Hello World */
  13573. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  13574. 0x72,0x6c,0x64
  13575. };
  13576. word32 len = sizeof(data);
  13577. byte hash[144];
  13578. word32 hashLen = sizeof(hash);
  13579. ExpectIntEQ(wc_Shake256Hash(data, len, hash, hashLen), 0);
  13580. #endif
  13581. return EXPECT_RESULT();
  13582. } /* END test_wc_Shake256Hash */
  13583. /*
  13584. * Testing wc_InitSm3(), wc_Sm3Free()
  13585. */
  13586. static int test_wc_InitSm3Free(void)
  13587. {
  13588. EXPECT_DECLS;
  13589. #ifdef WOLFSSL_SM3
  13590. wc_Sm3 sm3;
  13591. /* Invalid Parameters */
  13592. ExpectIntEQ(wc_InitSm3(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  13593. /* Valid Parameters */
  13594. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13595. wc_Sm3Free(NULL);
  13596. wc_Sm3Free(&sm3);
  13597. #endif
  13598. return EXPECT_RESULT();
  13599. } /* END test_wc_InitSm3 */
  13600. /*
  13601. * Testing wc_Sm3Update(), wc_Sm3Final()
  13602. */
  13603. static int test_wc_Sm3UpdateFinal(void)
  13604. {
  13605. EXPECT_DECLS;
  13606. #ifdef WOLFSSL_SM3
  13607. wc_Sm3 sm3;
  13608. byte data[WC_SM3_BLOCK_SIZE * 4];
  13609. byte hash[WC_SM3_DIGEST_SIZE];
  13610. byte calcHash[WC_SM3_DIGEST_SIZE];
  13611. byte expHash[WC_SM3_DIGEST_SIZE] = {
  13612. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  13613. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  13614. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  13615. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  13616. };
  13617. word32 chunk;
  13618. word32 i;
  13619. XMEMSET(data, 0, sizeof(data));
  13620. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13621. /* Invalid Parameters */
  13622. ExpectIntEQ(wc_Sm3Update(NULL, NULL, 1), BAD_FUNC_ARG);
  13623. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 1), BAD_FUNC_ARG);
  13624. ExpectIntEQ(wc_Sm3Update(NULL, data, 1), BAD_FUNC_ARG);
  13625. /* Valid Parameters */
  13626. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 0), 0);
  13627. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13628. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13629. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE), 0);
  13630. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE - 2), 0);
  13631. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE * 2), 0);
  13632. /* Ensure too many bytes for lengths. */
  13633. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_PAD_SIZE), 0);
  13634. /* Invalid Parameters */
  13635. ExpectIntEQ(wc_Sm3Final(NULL, NULL), BAD_FUNC_ARG);
  13636. ExpectIntEQ(wc_Sm3Final(&sm3, NULL), BAD_FUNC_ARG);
  13637. ExpectIntEQ(wc_Sm3Final(NULL, hash), BAD_FUNC_ARG);
  13638. /* Valid Parameters */
  13639. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13640. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  13641. /* Chunk tests. */
  13642. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  13643. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13644. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  13645. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  13646. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, chunk), 0);
  13647. }
  13648. if (i < (word32)sizeof(data)) {
  13649. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, (word32)sizeof(data) - i),
  13650. 0);
  13651. }
  13652. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13653. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13654. }
  13655. /* Not testing when the low 32-bit length overflows. */
  13656. wc_Sm3Free(&sm3);
  13657. #endif
  13658. return EXPECT_RESULT();
  13659. } /* END test_wc_Sm3Update */
  13660. /*
  13661. * Testing wc_Sm3GetHash()
  13662. */
  13663. static int test_wc_Sm3GetHash(void)
  13664. {
  13665. EXPECT_DECLS;
  13666. #ifdef WOLFSSL_SM3
  13667. wc_Sm3 sm3;
  13668. byte hash[WC_SM3_DIGEST_SIZE];
  13669. byte calcHash[WC_SM3_DIGEST_SIZE];
  13670. byte data[WC_SM3_BLOCK_SIZE];
  13671. XMEMSET(data, 0, sizeof(data));
  13672. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13673. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13674. /* Invalid Parameters */
  13675. ExpectIntEQ(wc_Sm3GetHash(NULL, NULL), BAD_FUNC_ARG);
  13676. ExpectIntEQ(wc_Sm3GetHash(&sm3, NULL), BAD_FUNC_ARG);
  13677. ExpectIntEQ(wc_Sm3GetHash(NULL, hash), BAD_FUNC_ARG);
  13678. /* Valid Parameters */
  13679. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  13680. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13681. /* With update. */
  13682. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  13683. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  13684. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13685. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13686. wc_Sm3Free(&sm3);
  13687. #endif
  13688. return EXPECT_RESULT();
  13689. } /* END test_wc_Sm3Update */
  13690. /*
  13691. * Testing wc_Sm3Copy()
  13692. */
  13693. static int test_wc_Sm3Copy(void)
  13694. {
  13695. EXPECT_DECLS;
  13696. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13697. wc_Sm3 sm3;
  13698. wc_Sm3 sm3Copy;
  13699. byte hash[WC_SM3_DIGEST_SIZE];
  13700. byte hashCopy[WC_SM3_DIGEST_SIZE];
  13701. byte data[WC_SM3_BLOCK_SIZE + 1];
  13702. int i;
  13703. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13704. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  13705. /* Invalid Parameters */
  13706. ExpectIntEQ(wc_Sm3Copy(NULL, NULL), BAD_FUNC_ARG);
  13707. ExpectIntEQ(wc_Sm3Copy(&sm3, NULL), BAD_FUNC_ARG);
  13708. ExpectIntEQ(wc_Sm3Copy(NULL, &sm3Copy), BAD_FUNC_ARG);
  13709. /* Valid Parameters */
  13710. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13711. /* Ensure all parts of data updated during hashing are copied. */
  13712. for (i = 0; i < WC_SM3_BLOCK_SIZE + 1; i++) {
  13713. ExpectIntEQ(wc_Sm3Update(&sm3, data, i), 0);
  13714. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13715. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13716. ExpectIntEQ(wc_Sm3Update(&sm3Copy, data, 1), 0);
  13717. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13718. ExpectIntEQ(wc_Sm3Final(&sm3Copy, hashCopy), 0);
  13719. ExpectBufEQ(hash, hashCopy, WC_SM3_DIGEST_SIZE);
  13720. }
  13721. wc_Sm3Free(&sm3Copy);
  13722. wc_Sm3Free(&sm3);
  13723. #endif
  13724. return EXPECT_RESULT();
  13725. } /* END test_wc_Sm3Copy */
  13726. /*
  13727. * Testing wc_Sm3FinalRaw()
  13728. */
  13729. static int test_wc_Sm3FinalRaw(void)
  13730. {
  13731. EXPECT_DECLS;
  13732. #if defined(WOLFSSL_SM3) && !defined(HAVE_SELFTEST) && \
  13733. !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  13734. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  13735. !defined(WOLFSSL_NO_HASH_RAW)
  13736. wc_Sm3 sm3;
  13737. byte hash1[WC_SM3_DIGEST_SIZE];
  13738. byte hash2[WC_SM3_DIGEST_SIZE];
  13739. byte hash3[WC_SM3_DIGEST_SIZE];
  13740. byte* hash_test[3] = { hash1, hash2, hash3 };
  13741. int times;
  13742. int i;
  13743. XMEMSET(&sm3, 0, sizeof(sm3));
  13744. /* Initialize */
  13745. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13746. /* Invalid Parameters */
  13747. ExpectIntEQ(wc_Sm3FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  13748. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, NULL), BAD_FUNC_ARG);
  13749. ExpectIntEQ(wc_Sm3FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  13750. times = sizeof(hash_test) / sizeof(byte*);
  13751. for (i = 0; i < times; i++) {
  13752. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, hash_test[i]), 0);
  13753. }
  13754. wc_Sm3Free(&sm3);
  13755. #endif
  13756. return EXPECT_RESULT();
  13757. } /* END test_wc_Sm3FinalRaw */
  13758. /*
  13759. * Testing wc_Sm3GetFlags, wc_Sm3SetFlags()
  13760. */
  13761. static int test_wc_Sm3GetSetFlags(void)
  13762. {
  13763. EXPECT_DECLS;
  13764. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13765. wc_Sm3 sm3;
  13766. wc_Sm3 sm3Copy;
  13767. word32 flags = 0;
  13768. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13769. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  13770. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  13771. ExpectIntEQ(flags, 0);
  13772. ExpectIntEQ(wc_Sm3SetFlags(NULL, WC_HASH_FLAG_WILLCOPY), 0);
  13773. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  13774. ExpectIntEQ(flags, 0);
  13775. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  13776. ExpectIntEQ(flags, 0);
  13777. ExpectIntEQ(wc_Sm3SetFlags(&sm3, WC_HASH_FLAG_WILLCOPY), 0);
  13778. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  13779. ExpectIntEQ(flags, WC_HASH_FLAG_WILLCOPY);
  13780. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13781. ExpectIntEQ(wc_Sm3GetFlags(&sm3Copy, &flags), 0);
  13782. ExpectIntEQ(flags, WC_HASH_FLAG_ISCOPY | WC_HASH_FLAG_WILLCOPY);
  13783. wc_Sm3Free(&sm3Copy);
  13784. wc_Sm3Free(&sm3);
  13785. #endif
  13786. return EXPECT_RESULT();
  13787. } /* END test_wc_Sm3Update */
  13788. /*
  13789. * Testing wc_Sm3Hash()
  13790. */
  13791. static int test_wc_Sm3Hash(void)
  13792. {
  13793. EXPECT_DECLS;
  13794. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13795. byte data[WC_SM3_BLOCK_SIZE];
  13796. byte hash[WC_SM3_DIGEST_SIZE];
  13797. /* Invalid parameters. */
  13798. ExpectIntEQ(wc_Sm3Hash(NULL, sizeof(data), hash), BAD_FUNC_ARG);
  13799. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), NULL), BAD_FUNC_ARG);
  13800. /* Valid parameters. */
  13801. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), hash), 0);
  13802. #endif
  13803. return EXPECT_RESULT();
  13804. } /* END test_wc_Sm3Hash */
  13805. /*
  13806. * Test function for wc_HmacSetKey
  13807. */
  13808. static int test_wc_Md5HmacSetKey(void)
  13809. {
  13810. EXPECT_DECLS;
  13811. #if !defined(NO_HMAC) && !defined(NO_MD5)
  13812. Hmac hmac;
  13813. int ret, times, itr;
  13814. const char* keys[]=
  13815. {
  13816. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  13817. #ifndef HAVE_FIPS
  13818. "Jefe", /* smaller than minimum FIPS key size */
  13819. #endif
  13820. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13821. };
  13822. times = sizeof(keys) / sizeof(char*);
  13823. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13824. for (itr = 0; itr < times; itr++) {
  13825. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  13826. (word32)XSTRLEN(keys[itr]));
  13827. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13828. wc_HmacFree(&hmac);
  13829. ExpectIntEQ(ret, BAD_FUNC_ARG);
  13830. #else
  13831. ExpectIntEQ(ret, 0);
  13832. #endif
  13833. }
  13834. /* Bad args. */
  13835. ExpectIntEQ(wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  13836. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13837. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0])),
  13838. BAD_FUNC_ARG);
  13839. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13840. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13841. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  13842. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13843. ExpectIntEQ(ret, BAD_FUNC_ARG);
  13844. #elif defined(HAVE_FIPS)
  13845. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13846. #else
  13847. ExpectIntEQ(ret, 0);
  13848. #endif
  13849. wc_HmacFree(&hmac);
  13850. #endif
  13851. return EXPECT_RESULT();
  13852. } /* END test_wc_Md5HmacSetKey */
  13853. /*
  13854. * testing wc_HmacSetKey() on wc_Sha hash.
  13855. */
  13856. static int test_wc_ShaHmacSetKey(void)
  13857. {
  13858. EXPECT_DECLS;
  13859. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13860. Hmac hmac;
  13861. int ret, times, itr;
  13862. const char* keys[]=
  13863. {
  13864. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13865. "\x0b\x0b\x0b",
  13866. #ifndef HAVE_FIPS
  13867. "Jefe", /* smaller than minimum FIPS key size */
  13868. #endif
  13869. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13870. "\xAA\xAA\xAA"
  13871. };
  13872. times = sizeof(keys) / sizeof(char*);
  13873. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13874. for (itr = 0; itr < times; itr++) {
  13875. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  13876. (word32)XSTRLEN(keys[itr])), 0);
  13877. }
  13878. /* Bad args. */
  13879. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  13880. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13881. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0])),
  13882. BAD_FUNC_ARG);
  13883. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13884. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13885. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  13886. #ifdef HAVE_FIPS
  13887. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13888. #else
  13889. ExpectIntEQ(ret, 0);
  13890. #endif
  13891. wc_HmacFree(&hmac);
  13892. #endif
  13893. return EXPECT_RESULT();
  13894. } /* END test_wc_ShaHmacSetKey() */
  13895. /*
  13896. * testing wc_HmacSetKey() on Sha224 hash.
  13897. */
  13898. static int test_wc_Sha224HmacSetKey(void)
  13899. {
  13900. EXPECT_DECLS;
  13901. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13902. Hmac hmac;
  13903. int ret, times, itr;
  13904. const char* keys[]=
  13905. {
  13906. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13907. "\x0b\x0b\x0b",
  13908. #ifndef HAVE_FIPS
  13909. "Jefe", /* smaller than minimum FIPS key size */
  13910. #endif
  13911. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13912. "\xAA\xAA\xAA"
  13913. };
  13914. times = sizeof(keys) / sizeof(char*);
  13915. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13916. for (itr = 0; itr < times; itr++) {
  13917. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  13918. (word32)XSTRLEN(keys[itr])), 0);
  13919. }
  13920. /* Bad args. */
  13921. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  13922. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13923. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0])),
  13924. BAD_FUNC_ARG);
  13925. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13926. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13927. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  13928. #ifdef HAVE_FIPS
  13929. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13930. #else
  13931. ExpectIntEQ(ret, 0);
  13932. #endif
  13933. wc_HmacFree(&hmac);
  13934. #endif
  13935. return EXPECT_RESULT();
  13936. } /* END test_wc_Sha224HmacSetKey() */
  13937. /*
  13938. * testing wc_HmacSetKey() on Sha256 hash
  13939. */
  13940. static int test_wc_Sha256HmacSetKey(void)
  13941. {
  13942. EXPECT_DECLS;
  13943. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  13944. Hmac hmac;
  13945. int ret, times, itr;
  13946. const char* keys[]=
  13947. {
  13948. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13949. "\x0b\x0b\x0b",
  13950. #ifndef HAVE_FIPS
  13951. "Jefe", /* smaller than minimum FIPS key size */
  13952. #endif
  13953. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13954. "\xAA\xAA\xAA"
  13955. };
  13956. times = sizeof(keys) / sizeof(char*);
  13957. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13958. for (itr = 0; itr < times; itr++) {
  13959. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  13960. (word32)XSTRLEN(keys[itr])), 0);
  13961. }
  13962. /* Bad args. */
  13963. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  13964. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13965. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0])),
  13966. BAD_FUNC_ARG);
  13967. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13968. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13969. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  13970. #ifdef HAVE_FIPS
  13971. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13972. #else
  13973. ExpectIntEQ(ret, 0);
  13974. #endif
  13975. wc_HmacFree(&hmac);
  13976. #endif
  13977. return EXPECT_RESULT();
  13978. } /* END test_wc_Sha256HmacSetKey() */
  13979. /*
  13980. * testing wc_HmacSetKey on Sha384 hash.
  13981. */
  13982. static int test_wc_Sha384HmacSetKey(void)
  13983. {
  13984. EXPECT_DECLS;
  13985. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  13986. Hmac hmac;
  13987. int ret, times, itr;
  13988. const char* keys[]=
  13989. {
  13990. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13991. "\x0b\x0b\x0b",
  13992. #ifndef HAVE_FIPS
  13993. "Jefe", /* smaller than minimum FIPS key size */
  13994. #endif
  13995. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13996. "\xAA\xAA\xAA"
  13997. };
  13998. times = sizeof(keys) / sizeof(char*);
  13999. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14000. for (itr = 0; itr < times; itr++) {
  14001. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  14002. (word32)XSTRLEN(keys[itr])), 0);
  14003. }
  14004. /* Bad args. */
  14005. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  14006. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14007. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0])),
  14008. BAD_FUNC_ARG);
  14009. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14010. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14011. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  14012. #ifdef HAVE_FIPS
  14013. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14014. #else
  14015. ExpectIntEQ(ret, 0);
  14016. #endif
  14017. wc_HmacFree(&hmac);
  14018. #endif
  14019. return EXPECT_RESULT();
  14020. } /* END test_wc_Sha384HmacSetKey() */
  14021. /*
  14022. * testing wc_HmacUpdate on wc_Md5 hash.
  14023. */
  14024. static int test_wc_Md5HmacUpdate(void)
  14025. {
  14026. EXPECT_DECLS;
  14027. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  14028. Hmac hmac;
  14029. testVector a, b;
  14030. #ifdef HAVE_FIPS
  14031. const char* keys =
  14032. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14033. #else
  14034. const char* keys = "Jefe";
  14035. #endif
  14036. a.input = "what do ya want for nothing?";
  14037. a.inLen = XSTRLEN(a.input);
  14038. b.input = "Hi There";
  14039. b.inLen = XSTRLEN(b.input);
  14040. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14041. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys,
  14042. (word32)XSTRLEN(keys)), 0);
  14043. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14044. /* Update Hmac. */
  14045. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14046. /* Test bad args. */
  14047. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14048. BAD_FUNC_ARG);
  14049. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14050. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14051. wc_HmacFree(&hmac);
  14052. #endif
  14053. return EXPECT_RESULT();
  14054. } /* END test_wc_Md5HmacUpdate */
  14055. /*
  14056. * testing wc_HmacUpdate on SHA hash.
  14057. */
  14058. static int test_wc_ShaHmacUpdate(void)
  14059. {
  14060. EXPECT_DECLS;
  14061. #if !defined(NO_HMAC) && !defined(NO_SHA)
  14062. Hmac hmac;
  14063. testVector a, b;
  14064. #ifdef HAVE_FIPS
  14065. const char* keys =
  14066. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14067. #else
  14068. const char* keys = "Jefe";
  14069. #endif
  14070. a.input = "what do ya want for nothing?";
  14071. a.inLen = XSTRLEN(a.input);
  14072. b.input = "Hi There";
  14073. b.inLen = XSTRLEN(b.input);
  14074. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14075. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys,
  14076. (word32)XSTRLEN(keys)), 0);
  14077. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14078. /* Update Hmac. */
  14079. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14080. /* Test bad args. */
  14081. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14082. BAD_FUNC_ARG);
  14083. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14084. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14085. wc_HmacFree(&hmac);
  14086. #endif
  14087. return EXPECT_RESULT();
  14088. } /* END test_wc_ShaHmacUpdate */
  14089. /*
  14090. * testing wc_HmacUpdate on SHA224 hash.
  14091. */
  14092. static int test_wc_Sha224HmacUpdate(void)
  14093. {
  14094. EXPECT_DECLS;
  14095. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14096. Hmac hmac;
  14097. testVector a, b;
  14098. #ifdef HAVE_FIPS
  14099. const char* keys =
  14100. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14101. #else
  14102. const char* keys = "Jefe";
  14103. #endif
  14104. a.input = "what do ya want for nothing?";
  14105. a.inLen = XSTRLEN(a.input);
  14106. b.input = "Hi There";
  14107. b.inLen = XSTRLEN(b.input);
  14108. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14109. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys,
  14110. (word32)XSTRLEN(keys)), 0);
  14111. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14112. /* Update Hmac. */
  14113. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14114. /* Test bad args. */
  14115. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14116. BAD_FUNC_ARG);
  14117. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14118. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14119. wc_HmacFree(&hmac);
  14120. #endif
  14121. return EXPECT_RESULT();
  14122. } /* END test_wc_Sha224HmacUpdate */
  14123. /*
  14124. * testing wc_HmacUpdate on SHA256 hash.
  14125. */
  14126. static int test_wc_Sha256HmacUpdate(void)
  14127. {
  14128. EXPECT_DECLS;
  14129. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14130. Hmac hmac;
  14131. testVector a, b;
  14132. #ifdef HAVE_FIPS
  14133. const char* keys =
  14134. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14135. #else
  14136. const char* keys = "Jefe";
  14137. #endif
  14138. a.input = "what do ya want for nothing?";
  14139. a.inLen = XSTRLEN(a.input);
  14140. b.input = "Hi There";
  14141. b.inLen = XSTRLEN(b.input);
  14142. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14143. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys,
  14144. (word32)XSTRLEN(keys)), 0);
  14145. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14146. /* Update Hmac. */
  14147. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14148. /* Test bad args. */
  14149. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14150. BAD_FUNC_ARG);
  14151. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14152. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14153. wc_HmacFree(&hmac);
  14154. #endif
  14155. return EXPECT_RESULT();
  14156. } /* END test_wc_Sha256HmacUpdate */
  14157. /*
  14158. * testing wc_HmacUpdate on SHA384 hash.
  14159. */
  14160. static int test_wc_Sha384HmacUpdate(void)
  14161. {
  14162. EXPECT_DECLS;
  14163. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14164. Hmac hmac;
  14165. testVector a, b;
  14166. #ifdef HAVE_FIPS
  14167. const char* keys =
  14168. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14169. #else
  14170. const char* keys = "Jefe";
  14171. #endif
  14172. a.input = "what do ya want for nothing?";
  14173. a.inLen = XSTRLEN(a.input);
  14174. b.input = "Hi There";
  14175. b.inLen = XSTRLEN(b.input);
  14176. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14177. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys,
  14178. (word32)XSTRLEN(keys)), 0);
  14179. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14180. /* Update Hmac. */
  14181. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14182. /* Test bad args. */
  14183. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14184. BAD_FUNC_ARG);
  14185. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14186. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14187. wc_HmacFree(&hmac);
  14188. #endif
  14189. return EXPECT_RESULT();
  14190. } /* END test_wc_Sha384HmacUpdate */
  14191. /*
  14192. * Testing wc_HmacFinal() with MD5
  14193. */
  14194. static int test_wc_Md5HmacFinal(void)
  14195. {
  14196. EXPECT_DECLS;
  14197. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  14198. Hmac hmac;
  14199. byte hash[WC_MD5_DIGEST_SIZE];
  14200. testVector a;
  14201. const char* key;
  14202. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14203. a.input = "Hi There";
  14204. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  14205. "\x9d";
  14206. a.inLen = XSTRLEN(a.input);
  14207. a.outLen = XSTRLEN(a.output);
  14208. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14209. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key)),
  14210. 0);
  14211. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14212. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14213. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  14214. /* Try bad parameters. */
  14215. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14216. #ifndef HAVE_FIPS
  14217. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14218. #endif
  14219. wc_HmacFree(&hmac);
  14220. #endif
  14221. return EXPECT_RESULT();
  14222. } /* END test_wc_Md5HmacFinal */
  14223. /*
  14224. * Testing wc_HmacFinal() with SHA
  14225. */
  14226. static int test_wc_ShaHmacFinal(void)
  14227. {
  14228. EXPECT_DECLS;
  14229. #if !defined(NO_HMAC) && !defined(NO_SHA)
  14230. Hmac hmac;
  14231. byte hash[WC_SHA_DIGEST_SIZE];
  14232. testVector a;
  14233. const char* key;
  14234. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14235. "\x0b\x0b\x0b";
  14236. a.input = "Hi There";
  14237. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  14238. "\x8e\xf1\x46\xbe\x00";
  14239. a.inLen = XSTRLEN(a.input);
  14240. a.outLen = XSTRLEN(a.output);
  14241. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14242. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key)),
  14243. 0);
  14244. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14245. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14246. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  14247. /* Try bad parameters. */
  14248. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14249. #ifndef HAVE_FIPS
  14250. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14251. #endif
  14252. wc_HmacFree(&hmac);
  14253. #endif
  14254. return EXPECT_RESULT();
  14255. } /* END test_wc_ShaHmacFinal */
  14256. /*
  14257. * Testing wc_HmacFinal() with SHA224
  14258. */
  14259. static int test_wc_Sha224HmacFinal(void)
  14260. {
  14261. EXPECT_DECLS;
  14262. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14263. Hmac hmac;
  14264. byte hash[WC_SHA224_DIGEST_SIZE];
  14265. testVector a;
  14266. const char* key;
  14267. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14268. "\x0b\x0b\x0b";
  14269. a.input = "Hi There";
  14270. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  14271. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  14272. a.inLen = XSTRLEN(a.input);
  14273. a.outLen = XSTRLEN(a.output);
  14274. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14275. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key,
  14276. (word32)XSTRLEN(key)), 0);
  14277. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14278. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14279. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  14280. /* Try bad parameters. */
  14281. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14282. #ifndef HAVE_FIPS
  14283. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14284. #endif
  14285. wc_HmacFree(&hmac);
  14286. #endif
  14287. return EXPECT_RESULT();
  14288. } /* END test_wc_Sha224HmacFinal */
  14289. /*
  14290. * Testing wc_HmacFinal() with SHA256
  14291. */
  14292. static int test_wc_Sha256HmacFinal(void)
  14293. {
  14294. EXPECT_DECLS;
  14295. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14296. Hmac hmac;
  14297. byte hash[WC_SHA256_DIGEST_SIZE];
  14298. testVector a;
  14299. const char* key;
  14300. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14301. "\x0b\x0b\x0b";
  14302. a.input = "Hi There";
  14303. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  14304. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  14305. "\xcf\xf7";
  14306. a.inLen = XSTRLEN(a.input);
  14307. a.outLen = XSTRLEN(a.output);
  14308. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14309. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key,
  14310. (word32)XSTRLEN(key)), 0);
  14311. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14312. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14313. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  14314. /* Try bad parameters. */
  14315. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14316. #ifndef HAVE_FIPS
  14317. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14318. #endif
  14319. wc_HmacFree(&hmac);
  14320. #endif
  14321. return EXPECT_RESULT();
  14322. } /* END test_wc_Sha256HmacFinal */
  14323. /*
  14324. * Testing wc_HmacFinal() with SHA384
  14325. */
  14326. static int test_wc_Sha384HmacFinal(void)
  14327. {
  14328. EXPECT_DECLS;
  14329. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14330. Hmac hmac;
  14331. byte hash[WC_SHA384_DIGEST_SIZE];
  14332. testVector a;
  14333. const char* key;
  14334. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14335. "\x0b\x0b\x0b";
  14336. a.input = "Hi There";
  14337. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  14338. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  14339. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  14340. "\xfa\x9c\xb6";
  14341. a.inLen = XSTRLEN(a.input);
  14342. a.outLen = XSTRLEN(a.output);
  14343. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14344. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key,
  14345. (word32)XSTRLEN(key)), 0);
  14346. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14347. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14348. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  14349. /* Try bad parameters. */
  14350. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14351. #ifndef HAVE_FIPS
  14352. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14353. #endif
  14354. wc_HmacFree(&hmac);
  14355. #endif
  14356. return EXPECT_RESULT();
  14357. } /* END test_wc_Sha384HmacFinal */
  14358. /*
  14359. * Testing wc_InitCmac()
  14360. */
  14361. static int test_wc_InitCmac(void)
  14362. {
  14363. EXPECT_DECLS;
  14364. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  14365. Cmac cmac1;
  14366. Cmac cmac2;
  14367. Cmac cmac3;
  14368. /* AES 128 key. */
  14369. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14370. "\x09\x10\x11\x12\x13\x14\x15\x16";
  14371. /* AES 192 key. */
  14372. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14373. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14374. "\x01\x02\x03\x04\x05\x06\x07\x08";
  14375. /* AES 256 key. */
  14376. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14377. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14378. "\x01\x02\x03\x04\x05\x06\x07\x08"
  14379. "\x09\x01\x11\x12\x13\x14\x15\x16";
  14380. word32 key1Sz = (word32)sizeof(key1) - 1;
  14381. word32 key2Sz = (word32)sizeof(key2) - 1;
  14382. word32 key3Sz = (word32)sizeof(key3) - 1;
  14383. int type = WC_CMAC_AES;
  14384. (void)key1;
  14385. (void)key1Sz;
  14386. (void)key2;
  14387. (void)key2Sz;
  14388. XMEMSET(&cmac1, 0, sizeof(Cmac));
  14389. XMEMSET(&cmac2, 0, sizeof(Cmac));
  14390. XMEMSET(&cmac3, 0, sizeof(Cmac));
  14391. #ifdef WOLFSSL_AES_128
  14392. ExpectIntEQ(wc_InitCmac(&cmac1, key1, key1Sz, type, NULL), 0);
  14393. #endif
  14394. #ifdef WOLFSSL_AES_192
  14395. wc_AesFree(&cmac1.aes);
  14396. ExpectIntEQ(wc_InitCmac(&cmac2, key2, key2Sz, type, NULL), 0);
  14397. #endif
  14398. #ifdef WOLFSSL_AES_256
  14399. wc_AesFree(&cmac2.aes);
  14400. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, type, NULL), 0);
  14401. #endif
  14402. wc_AesFree(&cmac3.aes);
  14403. /* Test bad args. */
  14404. ExpectIntEQ(wc_InitCmac(NULL, key3, key3Sz, type, NULL), BAD_FUNC_ARG);
  14405. ExpectIntEQ(wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL), BAD_FUNC_ARG);
  14406. ExpectIntEQ(wc_InitCmac(&cmac3, key3, 0, type, NULL), BAD_FUNC_ARG);
  14407. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL), BAD_FUNC_ARG);
  14408. #endif
  14409. return EXPECT_RESULT();
  14410. } /* END test_wc_InitCmac */
  14411. /*
  14412. * Testing wc_CmacUpdate()
  14413. */
  14414. static int test_wc_CmacUpdate(void)
  14415. {
  14416. EXPECT_DECLS;
  14417. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14418. Cmac cmac;
  14419. byte key[] = {
  14420. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14421. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14422. };
  14423. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  14424. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  14425. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  14426. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  14427. "\xf4";
  14428. word32 inSz = (word32)sizeof(in) - 1;
  14429. word32 keySz = (word32)sizeof(key);
  14430. int type = WC_CMAC_AES;
  14431. XMEMSET(&cmac, 0, sizeof(Cmac));
  14432. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  14433. ExpectIntEQ(wc_CmacUpdate(&cmac, in, inSz), 0);
  14434. /* Test bad args. */
  14435. ExpectIntEQ(wc_CmacUpdate(NULL, in, inSz), BAD_FUNC_ARG);
  14436. ExpectIntEQ(wc_CmacUpdate(&cmac, NULL, 30), BAD_FUNC_ARG);
  14437. wc_AesFree(&cmac.aes);
  14438. #endif
  14439. return EXPECT_RESULT();
  14440. } /* END test_wc_CmacUpdate */
  14441. /*
  14442. * Testing wc_CmacFinal()
  14443. */
  14444. static int test_wc_CmacFinal(void)
  14445. {
  14446. EXPECT_DECLS;
  14447. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14448. Cmac cmac;
  14449. byte key[] = {
  14450. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14451. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14452. };
  14453. byte msg[] = {
  14454. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  14455. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  14456. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  14457. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  14458. 0xf4
  14459. };
  14460. /* Test vectors from CMACGenAES128.rsp from
  14461. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  14462. * Per RFC4493 truncation of lsb is possible.
  14463. */
  14464. byte expMac[] = {
  14465. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  14466. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  14467. };
  14468. byte mac[AES_BLOCK_SIZE];
  14469. word32 msgSz = (word32)sizeof(msg);
  14470. word32 keySz = (word32)sizeof(key);
  14471. word32 macSz = sizeof(mac);
  14472. word32 badMacSz = 17;
  14473. int expMacSz = sizeof(expMac);
  14474. int type = WC_CMAC_AES;
  14475. XMEMSET(&cmac, 0, sizeof(Cmac));
  14476. XMEMSET(mac, 0, macSz);
  14477. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  14478. ExpectIntEQ(wc_CmacUpdate(&cmac, msg, msgSz), 0);
  14479. #if (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  14480. /* Pass in bad args. */
  14481. ExpectIntEQ(wc_CmacFinalNoFree(NULL, mac, &macSz), BAD_FUNC_ARG);
  14482. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  14483. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, mac, &badMacSz), BUFFER_E);
  14484. /* For the last call, use the API with implicit wc_CmacFree(). */
  14485. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  14486. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14487. #else /* !HAVE_FIPS || FIPS>=5.3 */
  14488. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  14489. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14490. /* Pass in bad args. */
  14491. ExpectIntEQ(wc_CmacFinal(NULL, mac, &macSz), BAD_FUNC_ARG);
  14492. ExpectIntEQ(wc_CmacFinal(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  14493. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &badMacSz), BUFFER_E);
  14494. #endif /* !HAVE_FIPS || FIPS>=5.3 */
  14495. #endif
  14496. return EXPECT_RESULT();
  14497. } /* END test_wc_CmacFinal */
  14498. /*
  14499. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  14500. */
  14501. static int test_wc_AesCmacGenerate(void)
  14502. {
  14503. EXPECT_DECLS;
  14504. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14505. byte key[] = {
  14506. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  14507. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  14508. };
  14509. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  14510. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  14511. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  14512. "\x3d\x32\x65\x4c\x66\x23\xc5";
  14513. byte mac[AES_BLOCK_SIZE];
  14514. word32 keySz = sizeof(key);
  14515. word32 macSz = sizeof(mac);
  14516. word32 msgSz = sizeof(msg) - 1;
  14517. word32 expMacSz = sizeof(expMac) - 1;
  14518. XMEMSET(mac, 0, macSz);
  14519. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz), 0);
  14520. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14521. /* Pass in bad args. */
  14522. ExpectIntEQ(wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz),
  14523. BAD_FUNC_ARG);
  14524. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz),
  14525. BAD_FUNC_ARG);
  14526. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0),
  14527. BAD_FUNC_ARG);
  14528. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz),
  14529. BAD_FUNC_ARG);
  14530. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz), 0);
  14531. /* Test bad args. */
  14532. ExpectIntEQ(wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz),
  14533. BAD_FUNC_ARG);
  14534. ExpectIntEQ(wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz),
  14535. BAD_FUNC_ARG);
  14536. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz),
  14537. BAD_FUNC_ARG);
  14538. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0),
  14539. BAD_FUNC_ARG);
  14540. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz),
  14541. BAD_FUNC_ARG);
  14542. #endif
  14543. return EXPECT_RESULT();
  14544. } /* END test_wc_AesCmacGenerate */
  14545. /*
  14546. * Testing streaming AES-GCM API.
  14547. */
  14548. static int test_wc_AesGcmStream(void)
  14549. {
  14550. EXPECT_DECLS;
  14551. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  14552. defined(WOLFSSL_AESGCM_STREAM)
  14553. int i;
  14554. WC_RNG rng[1];
  14555. Aes aesEnc[1];
  14556. Aes aesDec[1];
  14557. byte tag[AES_BLOCK_SIZE];
  14558. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  14559. byte out[AES_BLOCK_SIZE * 3 + 2];
  14560. byte plain[AES_BLOCK_SIZE * 3 + 2];
  14561. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  14562. byte key[AES_128_KEY_SIZE] = { 0, };
  14563. byte iv[AES_IV_SIZE] = { 1, };
  14564. byte ivOut[AES_IV_SIZE];
  14565. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  14566. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  14567. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  14568. };
  14569. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  14570. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  14571. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  14572. };
  14573. static const byte expTag[AES_BLOCK_SIZE] = {
  14574. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  14575. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  14576. };
  14577. XMEMSET(&rng, 0, sizeof(WC_RNG));
  14578. XMEMSET(&aesEnc, 0, sizeof(Aes));
  14579. XMEMSET(&aesDec, 0, sizeof(Aes));
  14580. /* Create a random for generating IV/nonce. */
  14581. ExpectIntEQ(wc_InitRng(rng), 0);
  14582. /* Initialize data structures. */
  14583. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14584. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14585. /* BadParameters to streaming init. */
  14586. ExpectIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14587. ExpectIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14588. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  14589. BAD_FUNC_ARG);
  14590. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  14591. BAD_FUNC_ARG);
  14592. /* Bad parameters to encrypt update. */
  14593. ExpectIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14594. BAD_FUNC_ARG);
  14595. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  14596. BAD_FUNC_ARG);
  14597. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  14598. BAD_FUNC_ARG);
  14599. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  14600. BAD_FUNC_ARG);
  14601. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  14602. BAD_FUNC_ARG);
  14603. /* Bad parameters to decrypt update. */
  14604. ExpectIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14605. BAD_FUNC_ARG);
  14606. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  14607. BAD_FUNC_ARG);
  14608. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  14609. BAD_FUNC_ARG);
  14610. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  14611. BAD_FUNC_ARG);
  14612. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  14613. BAD_FUNC_ARG);
  14614. /* Bad parameters to encrypt final. */
  14615. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14616. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14617. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14618. BAD_FUNC_ARG);
  14619. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  14620. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  14621. BAD_FUNC_ARG);
  14622. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  14623. BAD_FUNC_ARG);
  14624. /* Bad parameters to decrypt final. */
  14625. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14626. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14627. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14628. BAD_FUNC_ARG);
  14629. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  14630. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  14631. BAD_FUNC_ARG);
  14632. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  14633. BAD_FUNC_ARG);
  14634. /* Check calling final before setting key fails. */
  14635. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  14636. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  14637. /* Check calling update before setting key else fails. */
  14638. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14639. MISSING_KEY);
  14640. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14641. MISSING_KEY);
  14642. /* Set key but not IV. */
  14643. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  14644. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  14645. /* Check calling final before setting IV fails. */
  14646. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  14647. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  14648. /* Check calling update before setting IV else fails. */
  14649. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14650. MISSING_IV);
  14651. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14652. MISSING_IV);
  14653. /* Set IV using fixed part IV and external IV APIs. */
  14654. ExpectIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  14655. rng), 0);
  14656. ExpectIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  14657. GCM_NONCE_MID_SZ), 0);
  14658. ExpectIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  14659. ExpectIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  14660. /* Encrypt and decrypt data. */
  14661. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  14662. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  14663. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  14664. /* Finalize and check tag matches. */
  14665. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14666. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14667. /* Set key and IV through streaming init API. */
  14668. wc_AesFree(aesEnc);
  14669. wc_AesFree(aesDec);
  14670. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14671. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14672. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14673. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14674. /* Encrypt/decrypt one block and AAD of one block. */
  14675. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  14676. AES_BLOCK_SIZE), 0);
  14677. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  14678. AES_BLOCK_SIZE), 0);
  14679. ExpectIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  14680. /* Finalize and check tag matches. */
  14681. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14682. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14683. /* Set key and IV through streaming init API. */
  14684. wc_AesFree(aesEnc);
  14685. wc_AesFree(aesDec);
  14686. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14687. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14688. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14689. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14690. /* No data to encrypt/decrypt one byte of AAD. */
  14691. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  14692. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  14693. /* Finalize and check tag matches. */
  14694. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14695. ExpectIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  14696. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14697. /* Set key and IV through streaming init API. */
  14698. wc_AesFree(aesEnc);
  14699. wc_AesFree(aesDec);
  14700. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14701. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14702. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14703. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14704. /* Encrypt/decrypt one byte and no AAD. */
  14705. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  14706. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  14707. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  14708. /* Finalize and check tag matches. */
  14709. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14710. ExpectIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  14711. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14712. /* Set key and IV through streaming init API. */
  14713. wc_AesFree(aesEnc);
  14714. wc_AesFree(aesDec);
  14715. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14716. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14717. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14718. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14719. /* Encryption AES is one byte at a time */
  14720. for (i = 0; i < (int)sizeof(aad); i++) {
  14721. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  14722. 0);
  14723. }
  14724. for (i = 0; i < (int)sizeof(in); i++) {
  14725. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  14726. 0);
  14727. }
  14728. /* Decryption AES is two bytes at a time */
  14729. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14730. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  14731. 0);
  14732. }
  14733. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14734. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  14735. 0), 0);
  14736. }
  14737. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14738. /* Finalize and check tag matches. */
  14739. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14740. ExpectIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  14741. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14742. /* Check streaming encryption can be decrypted with one shot. */
  14743. wc_AesFree(aesDec);
  14744. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14745. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14746. ExpectIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  14747. ExpectIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  14748. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14749. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14750. wc_AesFree(aesEnc);
  14751. wc_AesFree(aesDec);
  14752. wc_FreeRng(rng);
  14753. #endif
  14754. return EXPECT_RESULT();
  14755. } /* END test_wc_AesGcmStream */
  14756. /*
  14757. * Testing streaming SM4 API.
  14758. */
  14759. static int test_wc_Sm4(void)
  14760. {
  14761. int res = TEST_SKIPPED;
  14762. #ifdef WOLFSSL_SM4
  14763. EXPECT_DECLS;
  14764. wc_Sm4 sm4;
  14765. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  14766. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  14767. unsigned char key[SM4_KEY_SIZE];
  14768. #endif
  14769. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  14770. unsigned char iv[SM4_IV_SIZE];
  14771. #endif
  14772. /* Invalid parameters - wc_Sm4Init */
  14773. ExpectIntEQ(wc_Sm4Init(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  14774. /* Valid cases - wc_Sm4Init */
  14775. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14776. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  14777. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  14778. XMEMSET(key, 0, sizeof(key));
  14779. /* Invalid parameters - wc_Sm4SetKey. */
  14780. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  14781. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  14782. ExpectIntEQ(wc_Sm4SetKey(NULL, key, 0), BAD_FUNC_ARG);
  14783. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14784. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, 0), BAD_FUNC_ARG);
  14785. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14786. ExpectIntEQ(wc_Sm4SetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14787. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE-1), BAD_FUNC_ARG);
  14788. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE+1), BAD_FUNC_ARG);
  14789. /* Valid cases - wc_Sm4SetKey. */
  14790. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14791. #endif
  14792. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  14793. XMEMSET(iv, 0, sizeof(iv));
  14794. /* Invalid parameters - wc_Sm4SetIV. */
  14795. ExpectIntEQ(wc_Sm4SetIV(NULL, NULL), BAD_FUNC_ARG);
  14796. ExpectIntEQ(wc_Sm4SetIV(&sm4, NULL), BAD_FUNC_ARG);
  14797. ExpectIntEQ(wc_Sm4SetIV(NULL, iv), BAD_FUNC_ARG);
  14798. /* Valid cases - wc_Sm4SetIV. */
  14799. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14800. #endif
  14801. /* Valid cases - wc_Sm4Free */
  14802. wc_Sm4Free(NULL);
  14803. wc_Sm4Free(&sm4);
  14804. res = EXPECT_RESULT();
  14805. #endif
  14806. return res;
  14807. } /* END test_wc_Sm4 */
  14808. /*
  14809. * Testing block based SM4-ECB API.
  14810. */
  14811. static int test_wc_Sm4Ecb(void)
  14812. {
  14813. int res = TEST_SKIPPED;
  14814. #ifdef WOLFSSL_SM4_ECB
  14815. EXPECT_DECLS;
  14816. wc_Sm4 sm4;
  14817. unsigned char key[SM4_KEY_SIZE];
  14818. unsigned char in[SM4_BLOCK_SIZE * 2];
  14819. unsigned char out[SM4_BLOCK_SIZE * 2];
  14820. unsigned char out2[SM4_BLOCK_SIZE];
  14821. XMEMSET(key, 0, sizeof(key));
  14822. XMEMSET(in, 0, sizeof(in));
  14823. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14824. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14825. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), MISSING_KEY);
  14826. /* Tested in test_wc_Sm4. */
  14827. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14828. /* Invalid parameters - wc_Sm4EcbEncrypt. */
  14829. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14830. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14831. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14832. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14833. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14834. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14835. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14836. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14837. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14838. /* Valid cases - wc_Sm4EcbEncrypt. */
  14839. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), 0);
  14840. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14841. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14842. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14843. /* In and out are same pointer. */
  14844. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14845. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14846. /* Invalid parameters - wc_Sm4EcbDecrypt. */
  14847. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14848. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14849. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14850. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14851. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14852. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14853. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14854. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14855. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14856. /* Valid cases - wc_Sm4EcbDecrypt. */
  14857. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), 0);
  14858. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14859. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14860. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14861. /* In and out are same pointer. */
  14862. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14863. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14864. wc_Sm4Free(&sm4);
  14865. res = EXPECT_RESULT();
  14866. #endif
  14867. return res;
  14868. } /* END test_wc_Sm4Ecb */
  14869. /*
  14870. * Testing block based SM4-CBC API.
  14871. */
  14872. static int test_wc_Sm4Cbc(void)
  14873. {
  14874. int res = TEST_SKIPPED;
  14875. #ifdef WOLFSSL_SM4_CBC
  14876. EXPECT_DECLS;
  14877. wc_Sm4 sm4;
  14878. unsigned char key[SM4_KEY_SIZE];
  14879. unsigned char iv[SM4_IV_SIZE];
  14880. unsigned char in[SM4_BLOCK_SIZE * 2];
  14881. unsigned char out[SM4_BLOCK_SIZE * 2];
  14882. unsigned char out2[SM4_BLOCK_SIZE];
  14883. XMEMSET(key, 0, sizeof(key));
  14884. XMEMSET(iv, 0, sizeof(iv));
  14885. XMEMSET(in, 0, sizeof(in));
  14886. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14887. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14888. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_KEY);
  14889. /* Tested in test_wc_Sm4. */
  14890. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14891. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_IV);
  14892. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_IV);
  14893. /* Tested in test_wc_Sm4. */
  14894. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14895. /* Invalid parameters - wc_Sm4CbcEncrypt. */
  14896. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14897. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14898. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14899. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14900. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14901. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14902. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14903. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14904. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14905. /* Valid cases - wc_Sm4CbcEncrypt. */
  14906. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), 0);
  14907. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14908. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14909. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14910. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14911. /* In and out are same pointer. */
  14912. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14913. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14914. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14915. /* Invalid parameters - wc_Sm4CbcDecrypt. */
  14916. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14917. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14918. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14919. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14920. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14921. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14922. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14923. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14924. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14925. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14926. /* Valid cases - wc_Sm4CbcDecrypt. */
  14927. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), 0);
  14928. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14929. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14930. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14931. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14932. /* In and out are same pointer. */
  14933. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14934. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14935. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14936. wc_Sm4Free(&sm4);
  14937. res = EXPECT_RESULT();
  14938. #endif
  14939. return res;
  14940. } /* END test_wc_Sm4Cbc */
  14941. /*
  14942. * Testing streaming SM4-CTR API.
  14943. */
  14944. static int test_wc_Sm4Ctr(void)
  14945. {
  14946. int res = TEST_SKIPPED;
  14947. #ifdef WOLFSSL_SM4_CTR
  14948. EXPECT_DECLS;
  14949. wc_Sm4 sm4;
  14950. unsigned char key[SM4_KEY_SIZE];
  14951. unsigned char iv[SM4_IV_SIZE];
  14952. unsigned char in[SM4_BLOCK_SIZE * 4];
  14953. unsigned char out[SM4_BLOCK_SIZE * 4];
  14954. unsigned char out2[SM4_BLOCK_SIZE * 4];
  14955. word32 chunk;
  14956. word32 i;
  14957. XMEMSET(key, 0, sizeof(key));
  14958. XMEMSET(iv, 0, sizeof(iv));
  14959. XMEMSET(in, 0, sizeof(in));
  14960. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14961. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14962. /* Tested in test_wc_Sm4. */
  14963. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14964. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_IV);
  14965. /* Tested in test_wc_Sm4. */
  14966. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14967. /* Invalid parameters - wc_Sm4CtrEncrypt. */
  14968. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14969. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, NULL, 0), BAD_FUNC_ARG);
  14970. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, NULL, 0), BAD_FUNC_ARG);
  14971. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, in, 0), BAD_FUNC_ARG);
  14972. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14973. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14974. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14975. /* Valid cases - wc_Sm4CtrEncrypt. */
  14976. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), 0);
  14977. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, 1), 0);
  14978. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14979. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 2), 0);
  14980. ExpectIntEQ(XMEMCMP(out, out2, 1), 0);
  14981. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14982. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14983. ExpectIntEQ(XMEMCMP(out2, out, 2), 0);
  14984. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14985. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14986. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14987. /* In and out are same pointer. Also check encrypt of cipher text produces
  14988. * plaintext.
  14989. */
  14990. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14991. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, out, SM4_BLOCK_SIZE * 2), 0);
  14992. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14993. /* Chunking tests. */
  14994. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14995. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, (word32)sizeof(in)), 0);
  14996. for (chunk = 1; chunk <= SM4_BLOCK_SIZE + 1; chunk++) {
  14997. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14998. for (i = 0; i + chunk <= (word32)sizeof(in); i += chunk) {
  14999. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i, chunk), 0);
  15000. }
  15001. if (i < (word32)sizeof(in)) {
  15002. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i,
  15003. (word32)sizeof(in) - i), 0);
  15004. }
  15005. ExpectIntEQ(XMEMCMP(out, out2, (word32)sizeof(out)), 0);
  15006. }
  15007. for (i = 0; i < (word32)sizeof(iv); i++) {
  15008. iv[i] = 0xff;
  15009. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15010. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15011. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15012. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, out, SM4_BLOCK_SIZE * 2), 0);
  15013. ExpectIntEQ(XMEMCMP(out2, in, SM4_BLOCK_SIZE * 2), 0);
  15014. }
  15015. wc_Sm4Free(&sm4);
  15016. res = EXPECT_RESULT();
  15017. #endif
  15018. return res;
  15019. } /* END test_wc_Sm4Ctr */
  15020. /*
  15021. * Testing stream SM4-GCM API.
  15022. */
  15023. static int test_wc_Sm4Gcm(void)
  15024. {
  15025. int res = TEST_SKIPPED;
  15026. #ifdef WOLFSSL_SM4_GCM
  15027. EXPECT_DECLS;
  15028. wc_Sm4 sm4;
  15029. unsigned char key[SM4_KEY_SIZE];
  15030. unsigned char nonce[GCM_NONCE_MAX_SZ];
  15031. unsigned char in[SM4_BLOCK_SIZE * 2];
  15032. unsigned char in2[SM4_BLOCK_SIZE * 2];
  15033. unsigned char out[SM4_BLOCK_SIZE * 2];
  15034. unsigned char out2[SM4_BLOCK_SIZE * 2];
  15035. unsigned char dec[SM4_BLOCK_SIZE * 2];
  15036. unsigned char tag[SM4_BLOCK_SIZE];
  15037. unsigned char aad[SM4_BLOCK_SIZE * 2];
  15038. word32 i;
  15039. XMEMSET(key, 0, sizeof(key));
  15040. XMEMSET(nonce, 0, sizeof(nonce));
  15041. XMEMSET(in, 0, sizeof(in));
  15042. XMEMSET(in2, 0, sizeof(in2));
  15043. XMEMSET(aad, 0, sizeof(aad));
  15044. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15045. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  15046. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15047. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  15048. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15049. /* Invalid parameters - wc_Sm4GcmSetKey. */
  15050. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  15051. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  15052. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, 0), BAD_FUNC_ARG);
  15053. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15054. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, 0), BAD_FUNC_ARG);
  15055. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15056. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15057. /* Valid parameters - wc_Sm4GcmSetKey. */
  15058. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15059. /* Invalid parameters - wc_Sm4GcmEncrypt. */
  15060. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15061. 0), BAD_FUNC_ARG);
  15062. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15063. 0), BAD_FUNC_ARG);
  15064. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15065. 0), BAD_FUNC_ARG);
  15066. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15067. 0), BAD_FUNC_ARG);
  15068. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15069. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15070. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15071. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15072. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15073. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15074. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  15075. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15076. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15077. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15078. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  15079. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15080. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  15081. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15082. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  15083. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15084. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15085. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15086. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15087. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15088. /* Invalid parameters - wc_Sm4GcmDecrypt. */
  15089. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15090. 0), BAD_FUNC_ARG);
  15091. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15092. 0), BAD_FUNC_ARG);
  15093. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15094. 0), BAD_FUNC_ARG);
  15095. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15096. 0), BAD_FUNC_ARG);
  15097. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15098. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15099. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15100. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15101. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15102. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15103. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  15104. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15105. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15106. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15107. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  15108. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15109. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  15110. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15111. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  15112. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15113. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15114. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15115. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15116. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15117. /* Valid cases - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15118. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15119. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15120. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15121. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15122. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15123. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15124. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15125. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15126. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15127. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15128. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15129. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15130. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15131. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15132. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15133. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15134. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15135. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15136. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15137. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15138. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15139. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15140. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  15141. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15142. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15143. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  15144. /* Check vald values of nonce - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15145. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15146. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15147. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15148. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15149. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15150. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15151. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15152. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15153. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15154. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  15155. SM4_GCM_AUTH_E);
  15156. /* Check valid values of tag size - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15157. for (i = WOLFSSL_MIN_AUTH_TAG_SZ; i < SM4_BLOCK_SIZE; i++) {
  15158. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15159. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  15160. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15161. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  15162. }
  15163. /* Check different in/out sizes. */
  15164. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce,
  15165. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15166. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce,
  15167. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15168. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce,
  15169. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15170. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  15171. XMEMCPY(out2, out, i - 1);
  15172. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, i, nonce, GCM_NONCE_MID_SZ,
  15173. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15174. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  15175. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, dec, out, i, nonce, GCM_NONCE_MID_SZ,
  15176. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15177. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  15178. }
  15179. /* Force the counter to roll over in first byte. */
  15180. {
  15181. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  15182. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  15183. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  15184. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15185. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  15186. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15187. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  15188. }
  15189. wc_Sm4Free(&sm4);
  15190. res = EXPECT_RESULT();
  15191. #endif
  15192. return res;
  15193. } /* END test_wc_Sm4Gcm */
  15194. /*
  15195. * Testing stream SM4-CCM API.
  15196. */
  15197. static int test_wc_Sm4Ccm(void)
  15198. {
  15199. int res = TEST_SKIPPED;
  15200. #ifdef WOLFSSL_SM4_CCM
  15201. EXPECT_DECLS;
  15202. wc_Sm4 sm4;
  15203. unsigned char key[SM4_KEY_SIZE];
  15204. unsigned char nonce[CCM_NONCE_MAX_SZ];
  15205. unsigned char in[SM4_BLOCK_SIZE * 2];
  15206. unsigned char in2[SM4_BLOCK_SIZE * 2];
  15207. unsigned char out[SM4_BLOCK_SIZE * 2];
  15208. unsigned char out2[SM4_BLOCK_SIZE * 2];
  15209. unsigned char dec[SM4_BLOCK_SIZE * 2];
  15210. unsigned char tag[SM4_BLOCK_SIZE];
  15211. unsigned char aad[SM4_BLOCK_SIZE * 2];
  15212. word32 i;
  15213. XMEMSET(key, 0, sizeof(key));
  15214. XMEMSET(nonce, 0, sizeof(nonce));
  15215. XMEMSET(in, 0, sizeof(in));
  15216. XMEMSET(in2, 0, sizeof(in2));
  15217. XMEMSET(aad, 0, sizeof(aad));
  15218. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15219. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  15220. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15221. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  15222. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15223. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15224. /* Invalid parameters - wc_Sm4CcmEncrypt. */
  15225. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15226. 0), BAD_FUNC_ARG);
  15227. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15228. 0), BAD_FUNC_ARG);
  15229. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15230. 0), BAD_FUNC_ARG);
  15231. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15232. 0), BAD_FUNC_ARG);
  15233. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15234. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15235. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15236. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15237. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15238. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15239. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15240. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15241. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15242. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15243. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  15244. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15245. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  15246. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15247. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15248. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15249. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15250. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15251. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15252. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15253. /* Invalid parameters - wc_Sm4CcmDecrypt. */
  15254. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15255. 0), BAD_FUNC_ARG);
  15256. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15257. 0), BAD_FUNC_ARG);
  15258. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15259. 0), BAD_FUNC_ARG);
  15260. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15261. 0), BAD_FUNC_ARG);
  15262. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15263. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15264. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15265. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15266. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15267. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15268. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15269. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15270. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15271. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15272. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  15273. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15274. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  15275. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15276. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15277. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15278. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15279. WOLFSSL_MIN_AUTH_TAG_SZ - 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15280. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15281. SM4_BLOCK_SIZE + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15282. /* Valid cases - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15283. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15284. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15285. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15286. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15287. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15288. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15289. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15290. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15291. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15292. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15293. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15294. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15295. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15296. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15297. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15298. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15299. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15300. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15301. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15302. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15303. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15304. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15305. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  15306. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15307. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15308. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  15309. /* Check vald values of nonce - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15310. for (i = CCM_NONCE_MIN_SZ; i <= CCM_NONCE_MAX_SZ; i++) {
  15311. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15312. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15313. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15314. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15315. }
  15316. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15317. CCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  15318. SM4_CCM_AUTH_E);
  15319. /* Check invalid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15320. for (i = 0; i < 4; i++) {
  15321. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15322. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15323. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15324. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15325. }
  15326. /* Odd values in range 4..SM4_BLOCK_SIZE. */
  15327. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  15328. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15329. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15330. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15331. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15332. }
  15333. /* Check valid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt.
  15334. * Even values in range 4..SM4_BLOCK_SIZE.
  15335. */
  15336. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  15337. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15338. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  15339. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15340. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  15341. }
  15342. /* Check different in/out sizes. */
  15343. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce,
  15344. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15345. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce,
  15346. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15347. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce,
  15348. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15349. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  15350. XMEMCPY(out2, out, i - 1);
  15351. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, i, nonce, CCM_NONCE_MAX_SZ,
  15352. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15353. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  15354. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, dec, out, i, nonce, CCM_NONCE_MAX_SZ,
  15355. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15356. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  15357. }
  15358. /* Force the counter to roll over in first byte. */
  15359. {
  15360. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  15361. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  15362. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  15363. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15364. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  15365. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15366. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  15367. }
  15368. wc_Sm4Free(&sm4);
  15369. res = EXPECT_RESULT();
  15370. #endif
  15371. return res;
  15372. } /* END test_wc_Sm4Ccm */
  15373. /*
  15374. * unit test for wc_Des3_SetIV()
  15375. */
  15376. static int test_wc_Des3_SetIV(void)
  15377. {
  15378. EXPECT_DECLS;
  15379. #ifndef NO_DES3
  15380. Des3 des;
  15381. const byte key[] = {
  15382. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15383. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15384. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15385. };
  15386. const byte iv[] = {
  15387. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15388. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15389. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15390. };
  15391. XMEMSET(&des, 0, sizeof(Des3));
  15392. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15393. /* DES_ENCRYPTION or DES_DECRYPTION */
  15394. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15395. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  15396. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  15397. /* Test explicitly wc_Des3_SetIV() */
  15398. ExpectIntEQ(wc_Des3_SetIV(NULL, iv), BAD_FUNC_ARG);
  15399. ExpectIntEQ(wc_Des3_SetIV(&des, NULL), 0);
  15400. #endif
  15401. wc_Des3Free(&des);
  15402. #endif
  15403. return EXPECT_RESULT();
  15404. } /* END test_wc_Des3_SetIV */
  15405. /*
  15406. * unit test for wc_Des3_SetKey()
  15407. */
  15408. static int test_wc_Des3_SetKey(void)
  15409. {
  15410. EXPECT_DECLS;
  15411. #ifndef NO_DES3
  15412. Des3 des;
  15413. const byte key[] = {
  15414. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15415. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15416. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15417. };
  15418. const byte iv[] = {
  15419. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15420. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15421. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15422. };
  15423. XMEMSET(&des, 0, sizeof(Des3));
  15424. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15425. /* DES_ENCRYPTION or DES_DECRYPTION */
  15426. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15427. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  15428. /* Test bad args. */
  15429. ExpectIntEQ(wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  15430. ExpectIntEQ(wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  15431. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, -1), BAD_FUNC_ARG);
  15432. /* Default case. Should return 0. */
  15433. ExpectIntEQ(wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION), 0);
  15434. wc_Des3Free(&des);
  15435. #endif
  15436. return EXPECT_RESULT();
  15437. } /* END test_wc_Des3_SetKey */
  15438. /*
  15439. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  15440. */
  15441. static int test_wc_Des3_CbcEncryptDecrypt(void)
  15442. {
  15443. EXPECT_DECLS;
  15444. #ifndef NO_DES3
  15445. Des3 des;
  15446. byte cipher[24];
  15447. byte plain[24];
  15448. const byte key[] = {
  15449. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15450. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15451. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15452. };
  15453. const byte iv[] = {
  15454. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15455. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15456. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15457. };
  15458. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  15459. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15460. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15461. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15462. };
  15463. XMEMSET(&des, 0, sizeof(Des3));
  15464. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15465. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15466. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, vector, 24), 0);
  15467. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION), 0);
  15468. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, cipher, 24), 0);
  15469. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  15470. /* Pass in bad args. */
  15471. ExpectIntEQ(wc_Des3_CbcEncrypt(NULL, cipher, vector, 24), BAD_FUNC_ARG);
  15472. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, NULL, vector, 24), BAD_FUNC_ARG);
  15473. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector)),
  15474. BAD_FUNC_ARG);
  15475. ExpectIntEQ(wc_Des3_CbcDecrypt(NULL, plain, cipher, 24), BAD_FUNC_ARG);
  15476. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, NULL, cipher, 24), BAD_FUNC_ARG);
  15477. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, NULL, 24), BAD_FUNC_ARG);
  15478. wc_Des3Free(&des);
  15479. #endif
  15480. return EXPECT_RESULT();
  15481. } /* END wc_Des3_CbcEncrypt */
  15482. /*
  15483. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  15484. */
  15485. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  15486. {
  15487. EXPECT_DECLS;
  15488. #ifndef NO_DES3
  15489. word32 vectorSz, cipherSz;
  15490. byte cipher[24];
  15491. byte plain[24];
  15492. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15493. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15494. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15495. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15496. };
  15497. byte key[] = {
  15498. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15499. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15500. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15501. };
  15502. byte iv[] = {
  15503. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15504. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15505. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15506. };
  15507. vectorSz = sizeof(byte) * 24;
  15508. cipherSz = sizeof(byte) * 24;
  15509. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv),
  15510. 0);
  15511. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv), 0);
  15512. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  15513. /* pass in bad args. */
  15514. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv),
  15515. BAD_FUNC_ARG);
  15516. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv),
  15517. BAD_FUNC_ARG);
  15518. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv),
  15519. BAD_FUNC_ARG);
  15520. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, NULL),
  15521. 0);
  15522. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv),
  15523. BAD_FUNC_ARG);
  15524. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv),
  15525. BAD_FUNC_ARG);
  15526. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv),
  15527. BAD_FUNC_ARG);
  15528. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL),
  15529. 0);
  15530. #endif
  15531. return EXPECT_RESULT();
  15532. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  15533. /*
  15534. * Unit test for wc_Des3_EcbEncrypt
  15535. */
  15536. static int test_wc_Des3_EcbEncrypt(void)
  15537. {
  15538. EXPECT_DECLS;
  15539. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  15540. Des3 des;
  15541. byte cipher[24];
  15542. word32 cipherSz = sizeof(cipher);
  15543. const byte key[] = {
  15544. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15545. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15546. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15547. };
  15548. const byte iv[] = {
  15549. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15550. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15551. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15552. };
  15553. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  15554. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15555. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15556. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15557. };
  15558. XMEMSET(&des, 0, sizeof(Des3));
  15559. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15560. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15561. /* Bad Cases */
  15562. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15563. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz),
  15564. BAD_FUNC_ARG);
  15565. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz), BAD_FUNC_ARG);
  15566. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz), BAD_FUNC_ARG);
  15567. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, 0), 0);
  15568. /* Good Cases */
  15569. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz), 0);
  15570. wc_Des3Free(&des);
  15571. #endif
  15572. return EXPECT_RESULT();
  15573. } /* END test_wc_Des3_EcbEncrypt */
  15574. /*
  15575. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  15576. */
  15577. static int test_wc_Chacha_SetKey(void)
  15578. {
  15579. EXPECT_DECLS;
  15580. #ifdef HAVE_CHACHA
  15581. ChaCha ctx;
  15582. const byte key[] = {
  15583. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15584. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15585. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15586. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15587. };
  15588. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  15589. byte cipher[128];
  15590. XMEMSET(cipher, 0, sizeof(cipher));
  15591. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, keySz), 0);
  15592. /* Test bad args. */
  15593. ExpectIntEQ(wc_Chacha_SetKey(NULL, key, keySz), BAD_FUNC_ARG);
  15594. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  15595. ExpectIntEQ(wc_Chacha_SetIV(&ctx, cipher, 0), 0);
  15596. /* Test bad args. */
  15597. ExpectIntEQ(wc_Chacha_SetIV(NULL, cipher, 0), BAD_FUNC_ARG);
  15598. #endif
  15599. return EXPECT_RESULT();
  15600. } /* END test_wc_Chacha_SetKey */
  15601. /*
  15602. * unit test for wc_Poly1305SetKey()
  15603. */
  15604. static int test_wc_Poly1305SetKey(void)
  15605. {
  15606. EXPECT_DECLS;
  15607. #ifdef HAVE_POLY1305
  15608. Poly1305 ctx;
  15609. const byte key[] =
  15610. {
  15611. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15612. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15613. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15614. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15615. };
  15616. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  15617. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, keySz), 0);
  15618. /* Test bad args. */
  15619. ExpectIntEQ(wc_Poly1305SetKey(NULL, key,keySz), BAD_FUNC_ARG);
  15620. ExpectIntEQ(wc_Poly1305SetKey(&ctx, NULL, keySz), BAD_FUNC_ARG);
  15621. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  15622. #endif
  15623. return EXPECT_RESULT();
  15624. } /* END test_wc_Poly1305_SetKey() */
  15625. /*
  15626. * Testing wc_Chacha_Process()
  15627. */
  15628. static int test_wc_Chacha_Process(void)
  15629. {
  15630. EXPECT_DECLS;
  15631. #ifdef HAVE_CHACHA
  15632. ChaCha enc, dec;
  15633. byte cipher[128];
  15634. byte plain[128];
  15635. const byte key[] =
  15636. {
  15637. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15638. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15639. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15640. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15641. };
  15642. const char* input = "Everybody gets Friday off.";
  15643. word32 keySz = sizeof(key)/sizeof(byte);
  15644. unsigned long int inlen = XSTRLEN(input);
  15645. /* Initialize stack variables. */
  15646. XMEMSET(cipher, 0, 128);
  15647. XMEMSET(plain, 0, 128);
  15648. ExpectIntEQ(wc_Chacha_SetKey(&enc, key, keySz), 0);
  15649. ExpectIntEQ(wc_Chacha_SetKey(&dec, key, keySz), 0);
  15650. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  15651. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  15652. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen),
  15653. 0);
  15654. ExpectIntEQ(wc_Chacha_Process(&dec, plain, cipher, (word32)inlen), 0);
  15655. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  15656. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  15657. /* test checking and using leftovers, currently just in C code */
  15658. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  15659. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  15660. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input,
  15661. (word32)inlen - 2), 0);
  15662. ExpectIntEQ(wc_Chacha_Process(&enc, cipher + (inlen - 2),
  15663. (byte*)input + (inlen - 2), 2), 0);
  15664. ExpectIntEQ(wc_Chacha_Process(&dec, plain, (byte*)cipher,
  15665. (word32)inlen - 2), 0);
  15666. ExpectIntEQ(wc_Chacha_Process(&dec, cipher + (inlen - 2),
  15667. (byte*)input + (inlen - 2), 2), 0);
  15668. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  15669. /* check edge cases with counter increment */
  15670. {
  15671. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  15672. const byte expected[] = {
  15673. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  15674. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  15675. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  15676. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  15677. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  15678. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  15679. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  15680. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  15681. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  15682. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  15683. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  15684. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  15685. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  15686. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  15687. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  15688. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  15689. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  15690. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  15691. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  15692. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  15693. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  15694. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  15695. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  15696. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  15697. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  15698. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  15699. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  15700. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  15701. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  15702. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  15703. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  15704. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  15705. };
  15706. const byte iv2[] = {
  15707. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  15708. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  15709. };
  15710. byte input2[256];
  15711. int i;
  15712. for (i = 0; i < 256; i++)
  15713. input2[i] = i;
  15714. ExpectIntEQ(wc_Chacha_SetIV(&enc, iv2, 0), 0);
  15715. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2, 64), 0);
  15716. ExpectIntEQ(XMEMCMP(expected, cipher, 64), 0);
  15717. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 64, 128), 0);
  15718. ExpectIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  15719. /* partial */
  15720. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 192, 32), 0);
  15721. ExpectIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  15722. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 224, 32), 0);
  15723. ExpectIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  15724. }
  15725. #endif
  15726. /* Test bad args. */
  15727. ExpectIntEQ(wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen),
  15728. BAD_FUNC_ARG);
  15729. #endif
  15730. return EXPECT_RESULT();
  15731. } /* END test_wc_Chacha_Process */
  15732. /*
  15733. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  15734. */
  15735. static int test_wc_ChaCha20Poly1305_aead(void)
  15736. {
  15737. EXPECT_DECLS;
  15738. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  15739. const byte key[] = {
  15740. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  15741. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  15742. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  15743. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  15744. };
  15745. const byte plaintext[] = {
  15746. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  15747. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  15748. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  15749. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  15750. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  15751. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  15752. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  15753. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  15754. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  15755. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  15756. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  15757. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  15758. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  15759. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  15760. 0x74, 0x2e
  15761. };
  15762. const byte iv[] = {
  15763. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  15764. 0x44, 0x45, 0x46, 0x47
  15765. };
  15766. const byte aad[] = { /* additional data */
  15767. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  15768. 0xc4, 0xc5, 0xc6, 0xc7
  15769. };
  15770. const byte cipher[] = { /* expected output from operation */
  15771. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  15772. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  15773. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  15774. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  15775. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  15776. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  15777. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  15778. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  15779. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  15780. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  15781. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  15782. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  15783. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  15784. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  15785. 0x61, 0x16
  15786. };
  15787. const byte authTag[] = { /* expected output from operation */
  15788. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  15789. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  15790. };
  15791. byte generatedCiphertext[272];
  15792. byte generatedPlaintext[272];
  15793. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  15794. /* Initialize stack variables. */
  15795. XMEMSET(generatedCiphertext, 0, 272);
  15796. XMEMSET(generatedPlaintext, 0, 272);
  15797. /* Test Encrypt */
  15798. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15799. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15800. 0);
  15801. ExpectIntEQ(XMEMCMP(generatedCiphertext, cipher,
  15802. sizeof(cipher)/sizeof(byte)), 0);
  15803. /* Test bad args. */
  15804. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad),
  15805. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15806. BAD_FUNC_ARG);
  15807. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  15808. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15809. BAD_FUNC_ARG);
  15810. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  15811. sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15812. BAD_FUNC_ARG);
  15813. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15814. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15815. BAD_FUNC_ARG);
  15816. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15817. plaintext, sizeof(plaintext), NULL, generatedAuthTag), BAD_FUNC_ARG);
  15818. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15819. plaintext, sizeof(plaintext), generatedCiphertext, NULL), BAD_FUNC_ARG);
  15820. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15821. sizeof(cipher), authTag, generatedPlaintext), 0);
  15822. ExpectIntEQ(XMEMCMP(generatedPlaintext, plaintext,
  15823. sizeof(plaintext)/sizeof(byte)), 0);
  15824. /* Test bad args. */
  15825. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  15826. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15827. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  15828. cipher, sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15829. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15830. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15831. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15832. sizeof(cipher), NULL, generatedPlaintext), BAD_FUNC_ARG);
  15833. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15834. sizeof(cipher), authTag, NULL), BAD_FUNC_ARG);
  15835. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15836. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15837. #endif
  15838. return EXPECT_RESULT();
  15839. } /* END test_wc_ChaCha20Poly1305_aead */
  15840. /*
  15841. * Testing function for wc_Rc2SetKey().
  15842. */
  15843. static int test_wc_Rc2SetKey(void)
  15844. {
  15845. EXPECT_DECLS;
  15846. #ifdef WC_RC2
  15847. Rc2 rc2;
  15848. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  15849. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15850. /* valid key and IV */
  15851. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15852. iv, 40), 0);
  15853. /* valid key, no IV */
  15854. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15855. NULL, 40), 0);
  15856. /* bad arguments */
  15857. /* null Rc2 struct */
  15858. ExpectIntEQ(wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  15859. iv, 40), BAD_FUNC_ARG);
  15860. /* null key */
  15861. ExpectIntEQ(wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  15862. iv, 40), BAD_FUNC_ARG);
  15863. /* key size == 0 */
  15864. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 0, iv, 40), WC_KEY_SIZE_E);
  15865. /* key size > 128 */
  15866. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 129, iv, 40), WC_KEY_SIZE_E);
  15867. /* effective bits == 0 */
  15868. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15869. iv, 0), WC_KEY_SIZE_E);
  15870. /* effective bits > 1024 */
  15871. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15872. iv, 1025), WC_KEY_SIZE_E);
  15873. #endif
  15874. return EXPECT_RESULT();
  15875. } /* END test_wc_Rc2SetKey */
  15876. /*
  15877. * Testing function for wc_Rc2SetIV().
  15878. */
  15879. static int test_wc_Rc2SetIV(void)
  15880. {
  15881. EXPECT_DECLS;
  15882. #ifdef WC_RC2
  15883. Rc2 rc2;
  15884. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15885. /* valid IV */
  15886. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  15887. /* valid NULL IV */
  15888. ExpectIntEQ(wc_Rc2SetIV(&rc2, NULL), 0);
  15889. /* bad arguments */
  15890. ExpectIntEQ(wc_Rc2SetIV(NULL, iv), BAD_FUNC_ARG);
  15891. ExpectIntEQ(wc_Rc2SetIV(NULL, NULL), BAD_FUNC_ARG);
  15892. #endif
  15893. return EXPECT_RESULT();
  15894. } /* END test_wc_Rc2SetIV */
  15895. /*
  15896. * Testing function for wc_Rc2EcbEncrypt() and wc_Rc2EcbDecrypt().
  15897. */
  15898. static int test_wc_Rc2EcbEncryptDecrypt(void)
  15899. {
  15900. EXPECT_DECLS;
  15901. #ifdef WC_RC2
  15902. Rc2 rc2;
  15903. int effectiveKeyBits = 63;
  15904. byte cipher[RC2_BLOCK_SIZE];
  15905. byte plain[RC2_BLOCK_SIZE];
  15906. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  15907. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  15908. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  15909. XMEMSET(cipher, 0, sizeof(cipher));
  15910. XMEMSET(plain, 0, sizeof(plain));
  15911. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  15912. NULL, effectiveKeyBits), 0);
  15913. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE), 0);
  15914. ExpectIntEQ(XMEMCMP(cipher, output, RC2_BLOCK_SIZE), 0);
  15915. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE), 0);
  15916. ExpectIntEQ(XMEMCMP(plain, input, RC2_BLOCK_SIZE), 0);
  15917. /* Rc2EcbEncrypt bad arguments */
  15918. /* null Rc2 struct */
  15919. ExpectIntEQ(wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE),
  15920. BAD_FUNC_ARG);
  15921. /* null out buffer */
  15922. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE),
  15923. BAD_FUNC_ARG);
  15924. /* null input buffer */
  15925. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE),
  15926. BAD_FUNC_ARG);
  15927. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  15928. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, 7), BUFFER_E);
  15929. /* Rc2EcbDecrypt bad arguments */
  15930. /* null Rc2 struct */
  15931. ExpectIntEQ(wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE),
  15932. BAD_FUNC_ARG);
  15933. /* null out buffer */
  15934. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE),
  15935. BAD_FUNC_ARG);
  15936. /* null input buffer */
  15937. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE),
  15938. BAD_FUNC_ARG);
  15939. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  15940. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, output, 7), BUFFER_E);
  15941. #endif
  15942. return EXPECT_RESULT();
  15943. } /* END test_wc_Rc2EcbEncryptDecrypt */
  15944. /*
  15945. * Testing function for wc_Rc2CbcEncrypt() and wc_Rc2CbcDecrypt().
  15946. */
  15947. static int test_wc_Rc2CbcEncryptDecrypt(void)
  15948. {
  15949. EXPECT_DECLS;
  15950. #ifdef WC_RC2
  15951. Rc2 rc2;
  15952. int effectiveKeyBits = 63;
  15953. byte cipher[RC2_BLOCK_SIZE*2];
  15954. byte plain[RC2_BLOCK_SIZE*2];
  15955. /* vector taken from test.c */
  15956. byte key[] = {
  15957. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15958. };
  15959. byte iv[] = {
  15960. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15961. };
  15962. byte input[] = {
  15963. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  15964. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  15965. };
  15966. byte output[] = {
  15967. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  15968. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  15969. };
  15970. XMEMSET(cipher, 0, sizeof(cipher));
  15971. XMEMSET(plain, 0, sizeof(plain));
  15972. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  15973. iv, effectiveKeyBits), 0);
  15974. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input)), 0);
  15975. ExpectIntEQ(XMEMCMP(cipher, output, sizeof(output)), 0);
  15976. /* reset IV for decrypt */
  15977. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  15978. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher)), 0);
  15979. ExpectIntEQ(XMEMCMP(plain, input, sizeof(input)), 0);
  15980. /* Rc2CbcEncrypt bad arguments */
  15981. /* null Rc2 struct */
  15982. ExpectIntEQ(wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input)),
  15983. BAD_FUNC_ARG);
  15984. /* null out buffer */
  15985. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input)),
  15986. BAD_FUNC_ARG);
  15987. /* null input buffer */
  15988. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input)),
  15989. BAD_FUNC_ARG);
  15990. /* Rc2CbcDecrypt bad arguments */
  15991. /* in size is 0 */
  15992. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, output, 0), 0);
  15993. /* null Rc2 struct */
  15994. ExpectIntEQ(wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output)),
  15995. BAD_FUNC_ARG);
  15996. /* null out buffer */
  15997. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output)),
  15998. BAD_FUNC_ARG);
  15999. /* null input buffer */
  16000. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output)),
  16001. BAD_FUNC_ARG);
  16002. #endif
  16003. return EXPECT_RESULT();
  16004. } /* END test_wc_Rc2CbcEncryptDecrypt */
  16005. /*
  16006. * Testing function for wc_AesSetIV
  16007. */
  16008. static int test_wc_AesSetIV(void)
  16009. {
  16010. int res = TEST_SKIPPED;
  16011. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  16012. Aes aes;
  16013. int ret = 0;
  16014. byte key16[] =
  16015. {
  16016. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16017. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16018. };
  16019. byte iv1[] = "1234567890abcdef";
  16020. byte iv2[] = "0987654321fedcba";
  16021. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  16022. if (ret != 0)
  16023. return ret;
  16024. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  16025. iv1, AES_ENCRYPTION);
  16026. if (ret == 0) {
  16027. ret = wc_AesSetIV(&aes, iv2);
  16028. }
  16029. /* Test bad args. */
  16030. if (ret == 0) {
  16031. ret = wc_AesSetIV(NULL, iv1);
  16032. if (ret == BAD_FUNC_ARG) {
  16033. /* NULL iv should return 0. */
  16034. ret = wc_AesSetIV(&aes, NULL);
  16035. }
  16036. else {
  16037. ret = WOLFSSL_FATAL_ERROR;
  16038. }
  16039. }
  16040. wc_AesFree(&aes);
  16041. res = TEST_RES_CHECK(ret == 0);
  16042. #endif
  16043. return res;
  16044. } /* test_wc_AesSetIV */
  16045. /*
  16046. * Testing function for wc_AesSetKey().
  16047. */
  16048. static int test_wc_AesSetKey(void)
  16049. {
  16050. EXPECT_DECLS;
  16051. #ifndef NO_AES
  16052. Aes aes;
  16053. byte key16[] = {
  16054. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16055. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16056. };
  16057. #ifdef WOLFSSL_AES_192
  16058. byte key24[] = {
  16059. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16060. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16061. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16062. };
  16063. #endif
  16064. #ifdef WOLFSSL_AES_256
  16065. byte key32[] = {
  16066. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16067. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16068. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16069. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16070. };
  16071. #endif
  16072. byte badKey16[] = {
  16073. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16074. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16075. };
  16076. byte iv[] = "1234567890abcdef";
  16077. XMEMSET(&aes, 0, sizeof(Aes));
  16078. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16079. #ifdef WOLFSSL_AES_128
  16080. ExpectIntEQ(wc_AesSetKey(&aes, key16, (word32)sizeof(key16) / sizeof(byte),
  16081. iv, AES_ENCRYPTION), 0);
  16082. #endif
  16083. #ifdef WOLFSSL_AES_192
  16084. ExpectIntEQ(wc_AesSetKey(&aes, key24, (word32)sizeof(key24) / sizeof(byte),
  16085. iv, AES_ENCRYPTION), 0);
  16086. #endif
  16087. #ifdef WOLFSSL_AES_256
  16088. ExpectIntEQ(wc_AesSetKey(&aes, key32, (word32)sizeof(key32) / sizeof(byte),
  16089. iv, AES_ENCRYPTION), 0);
  16090. #endif
  16091. /* Pass in bad args. */
  16092. ExpectIntEQ(wc_AesSetKey(NULL, key16, (word32)sizeof(key16) / sizeof(byte),
  16093. iv, AES_ENCRYPTION), BAD_FUNC_ARG);
  16094. ExpectIntEQ(wc_AesSetKey(&aes, badKey16,
  16095. (word32)sizeof(badKey16) / sizeof(byte), iv, AES_ENCRYPTION),
  16096. BAD_FUNC_ARG);
  16097. wc_AesFree(&aes);
  16098. #endif
  16099. return EXPECT_RESULT();
  16100. } /* END test_wc_AesSetKey */
  16101. /*
  16102. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  16103. * and wc_AesCbcDecryptWithKey()
  16104. */
  16105. static int test_wc_AesCbcEncryptDecrypt(void)
  16106. {
  16107. EXPECT_DECLS;
  16108. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  16109. defined(WOLFSSL_AES_256)
  16110. Aes aes;
  16111. byte key32[] = {
  16112. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16113. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16114. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16115. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16116. };
  16117. byte vector[] = { /* Now is the time for all good men w/o trailing 0 */
  16118. 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
  16119. 0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20,
  16120. 0x66, 0x6f, 0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20,
  16121. 0x67, 0x6f, 0x6f, 0x64, 0x20, 0x6d, 0x65, 0x6e
  16122. };
  16123. byte iv[] = "1234567890abcdef";
  16124. byte enc[sizeof(vector)];
  16125. byte dec[sizeof(vector)];
  16126. byte dec2[sizeof(vector)];
  16127. /* Init stack variables. */
  16128. XMEMSET(&aes, 0, sizeof(Aes));
  16129. XMEMSET(enc, 0, sizeof(enc));
  16130. XMEMSET(dec, 0, sizeof(vector));
  16131. XMEMSET(dec2, 0, sizeof(vector));
  16132. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16133. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  16134. AES_ENCRYPTION), 0);
  16135. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector)), 0);
  16136. /* Re init for decrypt and set flag. */
  16137. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  16138. AES_DECRYPTION), 0);
  16139. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector)), 0);
  16140. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16141. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE, key32,
  16142. sizeof(key32)/sizeof(byte), iv), 0);
  16143. ExpectIntEQ(XMEMCMP(vector, dec2, AES_BLOCK_SIZE), 0);
  16144. /* Pass in bad args */
  16145. ExpectIntEQ(wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector)),
  16146. BAD_FUNC_ARG);
  16147. ExpectIntEQ(wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector)),
  16148. BAD_FUNC_ARG);
  16149. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector)),
  16150. BAD_FUNC_ARG);
  16151. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  16152. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1),
  16153. BAD_LENGTH_E);
  16154. #endif
  16155. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16156. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  16157. fprintf(stderr, "Zero length inputs not supported with AESNI in FIPS "
  16158. "mode (v2), skip test");
  16159. #else
  16160. /* Test passing in size of 0 */
  16161. XMEMSET(enc, 0, sizeof(enc));
  16162. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, 0), 0);
  16163. /* Check enc was not modified */
  16164. {
  16165. int i;
  16166. for (i = 0; i < (int)sizeof(enc); i++)
  16167. ExpectIntEQ(enc[i], 0);
  16168. }
  16169. #endif
  16170. ExpectIntEQ(wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE), BAD_FUNC_ARG);
  16171. ExpectIntEQ(wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE),
  16172. BAD_FUNC_ARG);
  16173. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE),
  16174. BAD_FUNC_ARG);
  16175. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  16176. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  16177. BAD_LENGTH_E);
  16178. #else
  16179. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  16180. BAD_FUNC_ARG);
  16181. #endif
  16182. /* Test passing in size of 0 */
  16183. XMEMSET(dec, 0, sizeof(dec));
  16184. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, 0), 0);
  16185. /* Check dec was not modified */
  16186. {
  16187. int i;
  16188. for (i = 0; i < (int)sizeof(dec); i++)
  16189. ExpectIntEQ(dec[i], 0);
  16190. }
  16191. ExpectIntEQ(wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  16192. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16193. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  16194. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16195. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  16196. NULL, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16197. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  16198. key32, sizeof(key32)/sizeof(byte), NULL), BAD_FUNC_ARG);
  16199. wc_AesFree(&aes);
  16200. #endif
  16201. return EXPECT_RESULT();
  16202. } /* END test_wc_AesCbcEncryptDecrypt */
  16203. /*
  16204. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  16205. */
  16206. static int test_wc_AesCtrEncryptDecrypt(void)
  16207. {
  16208. EXPECT_DECLS;
  16209. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  16210. Aes aesEnc;
  16211. Aes aesDec;
  16212. byte key32[] = {
  16213. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16214. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16215. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16216. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16217. };
  16218. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  16219. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16220. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16221. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16222. };
  16223. byte iv[] = "1234567890abcdef";
  16224. byte enc[AES_BLOCK_SIZE * 2];
  16225. byte dec[AES_BLOCK_SIZE * 2];
  16226. /* Init stack variables. */
  16227. XMEMSET(&aesEnc, 0, sizeof(Aes));
  16228. XMEMSET(&aesDec, 0, sizeof(Aes));
  16229. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  16230. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  16231. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  16232. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  16233. ExpectIntEQ(wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2, iv,
  16234. AES_ENCRYPTION), 0);
  16235. ExpectIntEQ(wc_AesCtrEncrypt(&aesEnc, enc, vector,
  16236. sizeof(vector)/sizeof(byte)), 0);
  16237. /* Decrypt with wc_AesCtrEncrypt() */
  16238. ExpectIntEQ(wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2, iv,
  16239. AES_ENCRYPTION), 0);
  16240. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte)),
  16241. 0);
  16242. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16243. /* Test bad args. */
  16244. ExpectIntEQ(wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte)),
  16245. BAD_FUNC_ARG);
  16246. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte)),
  16247. BAD_FUNC_ARG);
  16248. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte)),
  16249. BAD_FUNC_ARG);
  16250. wc_AesFree(&aesEnc);
  16251. wc_AesFree(&aesDec);
  16252. #endif
  16253. return EXPECT_RESULT();
  16254. } /* END test_wc_AesCtrEncryptDecrypt */
  16255. /*
  16256. * test function for wc_AesGcmSetKey()
  16257. */
  16258. static int test_wc_AesGcmSetKey(void)
  16259. {
  16260. EXPECT_DECLS;
  16261. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16262. Aes aes;
  16263. #ifdef WOLFSSL_AES_128
  16264. byte key16[] = {
  16265. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16266. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16267. };
  16268. #endif
  16269. #ifdef WOLFSSL_AES_192
  16270. byte key24[] = {
  16271. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16272. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16273. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16274. };
  16275. #endif
  16276. #ifdef WOLFSSL_AES_256
  16277. byte key32[] = {
  16278. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16279. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16280. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16281. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16282. };
  16283. #endif
  16284. byte badKey16[] = {
  16285. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16286. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16287. };
  16288. byte badKey24[] = {
  16289. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16290. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16291. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  16292. };
  16293. byte badKey32[] = {
  16294. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  16295. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16296. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16297. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16298. };
  16299. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16300. #ifdef WOLFSSL_AES_128
  16301. ExpectIntEQ(wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte)), 0);
  16302. #endif
  16303. #ifdef WOLFSSL_AES_192
  16304. ExpectIntEQ(wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte)), 0);
  16305. #endif
  16306. #ifdef WOLFSSL_AES_256
  16307. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  16308. #endif
  16309. /* Pass in bad args. */
  16310. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte)),
  16311. BAD_FUNC_ARG);
  16312. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte)),
  16313. BAD_FUNC_ARG);
  16314. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte)),
  16315. BAD_FUNC_ARG);
  16316. wc_AesFree(&aes);
  16317. #endif
  16318. return EXPECT_RESULT();
  16319. } /* END test_wc_AesGcmSetKey */
  16320. /*
  16321. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  16322. */
  16323. static int test_wc_AesGcmEncryptDecrypt(void)
  16324. {
  16325. EXPECT_DECLS;
  16326. /* WOLFSSL_AFALG requires 12 byte IV */
  16327. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  16328. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  16329. Aes aes;
  16330. byte key32[] = {
  16331. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16332. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16333. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16334. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16335. };
  16336. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  16337. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16338. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16339. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16340. };
  16341. const byte a[] = {
  16342. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16343. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16344. 0xab, 0xad, 0xda, 0xd2
  16345. };
  16346. byte iv[] = "1234567890a";
  16347. byte longIV[] = "1234567890abcdefghij";
  16348. byte enc[sizeof(vector)];
  16349. byte resultT[AES_BLOCK_SIZE];
  16350. byte dec[sizeof(vector)];
  16351. /* Init stack variables. */
  16352. XMEMSET(&aes, 0, sizeof(Aes));
  16353. XMEMSET(enc, 0, sizeof(vector));
  16354. XMEMSET(dec, 0, sizeof(vector));
  16355. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  16356. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16357. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  16358. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16359. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  16360. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector), iv,
  16361. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  16362. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16363. /* Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  16364. ExpectIntEQ(wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector), iv,
  16365. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16366. BAD_FUNC_ARG);
  16367. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16368. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16369. BAD_FUNC_ARG);
  16370. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16371. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) - 5, a, sizeof(a)),
  16372. BAD_FUNC_ARG);
  16373. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16374. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  16375. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16376. /* FIPS does not check the lower bound of ivSz */
  16377. #else
  16378. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv, 0,
  16379. resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  16380. #endif
  16381. /* This case is now considered good. Long IVs are now allowed.
  16382. * Except for the original FIPS release, it still has an upper
  16383. * bound on the IV length. */
  16384. #if (!defined(HAVE_FIPS) || \
  16385. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16386. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16387. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  16388. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16389. 0);
  16390. #else
  16391. (void)longIV;
  16392. #endif /* Old FIPS */
  16393. /* END wc_AesGcmEncrypt */
  16394. #ifdef HAVE_AES_DECRYPT
  16395. ExpectIntEQ(wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16396. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16397. BAD_FUNC_ARG);
  16398. ExpectIntEQ(wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte), iv,
  16399. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16400. BAD_FUNC_ARG);
  16401. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte), iv,
  16402. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16403. BAD_FUNC_ARG);
  16404. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), NULL,
  16405. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16406. BAD_FUNC_ARG);
  16407. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16408. sizeof(iv)/sizeof(byte), NULL, sizeof(resultT), a, sizeof(a)),
  16409. BAD_FUNC_ARG);
  16410. #if (defined(HAVE_FIPS) && FIPS_VERSION_LE(2,0) && defined(WOLFSSL_ARMASM))
  16411. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16412. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16413. AES_GCM_AUTH_E);
  16414. #else
  16415. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16416. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16417. BAD_FUNC_ARG);
  16418. #endif
  16419. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16420. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  16421. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16422. /* FIPS does not check the lower bound of ivSz */
  16423. #else
  16424. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  16425. iv, 0, resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  16426. #endif
  16427. #endif /* HAVE_AES_DECRYPT */
  16428. wc_AesFree(&aes);
  16429. #endif
  16430. return EXPECT_RESULT();
  16431. } /* END test_wc_AesGcmEncryptDecrypt */
  16432. /*
  16433. * test function for mixed (one-shot encrpytion + stream decryption) AES GCM
  16434. * using a long IV (older FIPS does NOT support long IVs). Relates to zd15423
  16435. */
  16436. static int test_wc_AesGcmMixedEncDecLongIV(void)
  16437. {
  16438. EXPECT_DECLS;
  16439. #if (!defined(HAVE_FIPS) || \
  16440. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16441. !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AESGCM_STREAM)
  16442. const byte key[] = {
  16443. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16444. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16445. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16446. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16447. };
  16448. const byte in[] = {
  16449. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16450. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16451. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16452. };
  16453. const byte aad[] = {
  16454. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16455. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16456. 0xab, 0xad, 0xda, 0xd2
  16457. };
  16458. Aes aesEnc;
  16459. Aes aesDec;
  16460. byte iv[] = "1234567890abcdefghij";
  16461. byte out[sizeof(in)];
  16462. byte plain[sizeof(in)];
  16463. byte tag[AES_BLOCK_SIZE];
  16464. XMEMSET(&aesEnc, 0, sizeof(Aes));
  16465. XMEMSET(&aesDec, 0, sizeof(Aes));
  16466. XMEMSET(out, 0, sizeof(out));
  16467. XMEMSET(plain, 0, sizeof(plain));
  16468. XMEMSET(tag, 0, sizeof(tag));
  16469. /* Perform one-shot encryption using long IV */
  16470. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  16471. ExpectIntEQ(wc_AesGcmSetKey(&aesEnc, key, sizeof(key)), 0);
  16472. ExpectIntEQ(wc_AesGcmEncrypt(&aesEnc, out, in, sizeof(in), iv, sizeof(iv),
  16473. tag, sizeof(tag), aad, sizeof(aad)), 0);
  16474. /* Perform streaming decryption using long IV */
  16475. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  16476. ExpectIntEQ(wc_AesGcmInit(&aesDec, key, sizeof(key), iv, sizeof(iv)), 0);
  16477. ExpectIntEQ(wc_AesGcmDecryptUpdate(&aesDec, plain, out, sizeof(out), aad,
  16478. sizeof(aad)), 0);
  16479. ExpectIntEQ(wc_AesGcmDecryptFinal(&aesDec, tag, sizeof(tag)), 0);
  16480. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  16481. /* Free resources */
  16482. wc_AesFree(&aesEnc);
  16483. wc_AesFree(&aesDec);
  16484. #endif
  16485. return EXPECT_RESULT();
  16486. } /* END wc_AesGcmMixedEncDecLongIV */
  16487. /*
  16488. * unit test for wc_GmacSetKey()
  16489. */
  16490. static int test_wc_GmacSetKey(void)
  16491. {
  16492. EXPECT_DECLS;
  16493. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16494. Gmac gmac;
  16495. byte key16[] = {
  16496. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16497. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16498. };
  16499. #ifdef WOLFSSL_AES_192
  16500. byte key24[] = {
  16501. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16502. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16503. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16504. };
  16505. #endif
  16506. #ifdef WOLFSSL_AES_256
  16507. byte key32[] = {
  16508. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16509. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16510. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16511. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16512. };
  16513. #endif
  16514. byte badKey16[] = {
  16515. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16516. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  16517. };
  16518. byte badKey24[] = {
  16519. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  16520. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16521. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16522. };
  16523. byte badKey32[] = {
  16524. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16525. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  16526. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16527. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16528. };
  16529. XMEMSET(&gmac, 0, sizeof(Gmac));
  16530. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16531. #ifdef WOLFSSL_AES_128
  16532. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte)), 0);
  16533. #endif
  16534. #ifdef WOLFSSL_AES_192
  16535. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  16536. #endif
  16537. #ifdef WOLFSSL_AES_256
  16538. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  16539. #endif
  16540. /* Pass in bad args. */
  16541. ExpectIntEQ(wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte)),
  16542. BAD_FUNC_ARG);
  16543. ExpectIntEQ(wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte)),
  16544. BAD_FUNC_ARG);
  16545. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte)),
  16546. BAD_FUNC_ARG);
  16547. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte)),
  16548. BAD_FUNC_ARG);
  16549. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte)),
  16550. BAD_FUNC_ARG);
  16551. wc_AesFree(&gmac.aes);
  16552. #endif
  16553. return EXPECT_RESULT();
  16554. } /* END test_wc_GmacSetKey */
  16555. /*
  16556. * unit test for wc_GmacUpdate
  16557. */
  16558. static int test_wc_GmacUpdate(void)
  16559. {
  16560. EXPECT_DECLS;
  16561. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16562. Gmac gmac;
  16563. #ifdef WOLFSSL_AES_128
  16564. const byte key16[] = {
  16565. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  16566. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  16567. };
  16568. #endif
  16569. #ifdef WOLFSSL_AES_192
  16570. byte key24[] = {
  16571. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  16572. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  16573. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  16574. };
  16575. #endif
  16576. #ifdef WOLFSSL_AES_256
  16577. byte key32[] = {
  16578. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  16579. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  16580. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  16581. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  16582. };
  16583. #endif
  16584. #ifdef WOLFSSL_AES_128
  16585. const byte authIn[] = {
  16586. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  16587. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  16588. };
  16589. #endif
  16590. #ifdef WOLFSSL_AES_192
  16591. const byte authIn2[] = {
  16592. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  16593. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  16594. };
  16595. #endif
  16596. const byte authIn3[] = {
  16597. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  16598. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  16599. };
  16600. #ifdef WOLFSSL_AES_128
  16601. const byte tag1[] = { /* Known. */
  16602. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  16603. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  16604. };
  16605. #endif
  16606. #ifdef WOLFSSL_AES_192
  16607. const byte tag2[] = { /* Known */
  16608. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  16609. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  16610. };
  16611. #endif
  16612. const byte tag3[] = { /* Known */
  16613. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  16614. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  16615. };
  16616. #ifdef WOLFSSL_AES_128
  16617. const byte iv[] = {
  16618. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  16619. 0xe2, 0x8c, 0x8f, 0x16
  16620. };
  16621. #endif
  16622. #ifdef WOLFSSL_AES_192
  16623. const byte iv2[] = {
  16624. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  16625. 0x7e, 0x1a, 0x6f, 0xbc
  16626. };
  16627. #endif
  16628. const byte iv3[] = {
  16629. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  16630. 0xc3, 0xfb, 0x6c, 0x8a
  16631. };
  16632. byte tagOut[16];
  16633. byte tagOut2[24];
  16634. byte tagOut3[32];
  16635. /* Init stack variables. */
  16636. XMEMSET(&gmac, 0, sizeof(Gmac));
  16637. XMEMSET(tagOut, 0, sizeof(tagOut));
  16638. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  16639. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  16640. #ifdef WOLFSSL_AES_128
  16641. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16642. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)), 0);
  16643. ExpectIntEQ(wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  16644. tagOut, sizeof(tag1)), 0);
  16645. ExpectIntEQ(XMEMCMP(tag1, tagOut, sizeof(tag1)), 0);
  16646. wc_AesFree(&gmac.aes);
  16647. #endif
  16648. #ifdef WOLFSSL_AES_192
  16649. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  16650. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  16651. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  16652. ExpectIntEQ(wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2, sizeof(authIn2),
  16653. tagOut2, sizeof(tag2)), 0);
  16654. ExpectIntEQ(XMEMCMP(tagOut2, tag2, sizeof(tag2)), 0);
  16655. wc_AesFree(&gmac.aes);
  16656. #endif
  16657. #ifdef WOLFSSL_AES_256
  16658. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  16659. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  16660. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  16661. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16662. tagOut3, sizeof(tag3)), 0);
  16663. ExpectIntEQ(XMEMCMP(tag3, tagOut3, sizeof(tag3)), 0);
  16664. wc_AesFree(&gmac.aes);
  16665. #endif
  16666. /* Pass bad args. */
  16667. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16668. ExpectIntEQ(wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16669. tagOut3, sizeof(tag3)), BAD_FUNC_ARG);
  16670. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16671. tagOut3, sizeof(tag3) - 5), BAD_FUNC_ARG);
  16672. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16673. tagOut3, sizeof(tag3) + 1), BAD_FUNC_ARG);
  16674. wc_AesFree(&gmac.aes);
  16675. #endif
  16676. return EXPECT_RESULT();
  16677. } /* END test_wc_GmacUpdate */
  16678. /*
  16679. * testing wc_CamelliaSetKey
  16680. */
  16681. static int test_wc_CamelliaSetKey(void)
  16682. {
  16683. EXPECT_DECLS;
  16684. #ifdef HAVE_CAMELLIA
  16685. Camellia camellia;
  16686. /*128-bit key*/
  16687. static const byte key16[] = {
  16688. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16689. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  16690. };
  16691. /* 192-bit key */
  16692. static const byte key24[] = {
  16693. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16694. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16695. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16696. };
  16697. /* 256-bit key */
  16698. static const byte key32[] = {
  16699. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16700. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16701. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  16702. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  16703. };
  16704. static const byte iv[] = {
  16705. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16706. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16707. };
  16708. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv),
  16709. 0);
  16710. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16),
  16711. NULL), 0);
  16712. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  16713. 0);
  16714. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16715. NULL), 0);
  16716. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32), iv),
  16717. 0);
  16718. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32),
  16719. NULL), 0);
  16720. /* Bad args. */
  16721. ExpectIntEQ(wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv),
  16722. BAD_FUNC_ARG);
  16723. #endif
  16724. return EXPECT_RESULT();
  16725. } /* END test_wc_CammeliaSetKey */
  16726. /*
  16727. * Testing wc_CamelliaSetIV()
  16728. */
  16729. static int test_wc_CamelliaSetIV(void)
  16730. {
  16731. EXPECT_DECLS;
  16732. #ifdef HAVE_CAMELLIA
  16733. Camellia camellia;
  16734. static const byte iv[] = {
  16735. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16736. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16737. };
  16738. ExpectIntEQ(wc_CamelliaSetIV(&camellia, iv), 0);
  16739. ExpectIntEQ(wc_CamelliaSetIV(&camellia, NULL), 0);
  16740. /* Bad args. */
  16741. ExpectIntEQ(wc_CamelliaSetIV(NULL, NULL), BAD_FUNC_ARG);
  16742. ExpectIntEQ(wc_CamelliaSetIV(NULL, iv), BAD_FUNC_ARG);
  16743. #endif
  16744. return EXPECT_RESULT();
  16745. } /* END test_wc_CamelliaSetIV*/
  16746. /*
  16747. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  16748. */
  16749. static int test_wc_CamelliaEncryptDecryptDirect(void)
  16750. {
  16751. EXPECT_DECLS;
  16752. #ifdef HAVE_CAMELLIA
  16753. Camellia camellia;
  16754. static const byte key24[] = {
  16755. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16756. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16757. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16758. };
  16759. static const byte iv[] = {
  16760. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16761. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16762. };
  16763. static const byte plainT[] = {
  16764. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16765. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16766. };
  16767. byte enc[sizeof(plainT)];
  16768. byte dec[sizeof(enc)];
  16769. /* Init stack variables.*/
  16770. XMEMSET(enc, 0, 16);
  16771. XMEMSET(enc, 0, 16);
  16772. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  16773. 0);
  16774. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, plainT), 0);
  16775. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, enc), 0);
  16776. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  16777. /* Pass bad args. */
  16778. ExpectIntEQ(wc_CamelliaEncryptDirect(NULL, enc, plainT), BAD_FUNC_ARG);
  16779. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, NULL, plainT),
  16780. BAD_FUNC_ARG);
  16781. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, NULL), BAD_FUNC_ARG);
  16782. ExpectIntEQ(wc_CamelliaDecryptDirect(NULL, dec, enc), BAD_FUNC_ARG);
  16783. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, NULL, enc), BAD_FUNC_ARG);
  16784. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, NULL), BAD_FUNC_ARG);
  16785. #endif
  16786. return EXPECT_RESULT();
  16787. } /* END test-wc_CamelliaEncryptDecryptDirect */
  16788. /*
  16789. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  16790. */
  16791. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  16792. {
  16793. EXPECT_DECLS;
  16794. #ifdef HAVE_CAMELLIA
  16795. Camellia camellia;
  16796. static const byte key24[] = {
  16797. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16798. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16799. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16800. };
  16801. static const byte plainT[] = {
  16802. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16803. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16804. };
  16805. byte enc[CAMELLIA_BLOCK_SIZE];
  16806. byte dec[CAMELLIA_BLOCK_SIZE];
  16807. /* Init stack variables. */
  16808. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16809. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16810. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16811. NULL), 0);
  16812. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, plainT,
  16813. CAMELLIA_BLOCK_SIZE), 0);
  16814. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16815. NULL), 0);
  16816. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE),
  16817. 0);
  16818. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  16819. /* Pass in bad args. */
  16820. ExpectIntEQ(wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE),
  16821. BAD_FUNC_ARG);
  16822. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  16823. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16824. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  16825. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16826. ExpectIntEQ(wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE),
  16827. BAD_FUNC_ARG);
  16828. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  16829. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16830. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  16831. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16832. #endif
  16833. return EXPECT_RESULT();
  16834. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  16835. /*
  16836. * Testing wc_Arc4SetKey()
  16837. */
  16838. static int test_wc_Arc4SetKey(void)
  16839. {
  16840. EXPECT_DECLS;
  16841. #ifndef NO_RC4
  16842. Arc4 arc;
  16843. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16844. int keyLen = 8;
  16845. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, keyLen), 0);
  16846. /* Test bad args. */
  16847. ExpectIntEQ(wc_Arc4SetKey(NULL, (byte*)key, keyLen), BAD_FUNC_ARG);
  16848. ExpectIntEQ(wc_Arc4SetKey(&arc, NULL , keyLen), BAD_FUNC_ARG);
  16849. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, 0 ), BAD_FUNC_ARG);
  16850. #endif
  16851. return EXPECT_RESULT();
  16852. } /* END test_wc_Arc4SetKey */
  16853. /*
  16854. * Testing wc_Arc4Process for ENC/DEC.
  16855. */
  16856. static int test_wc_Arc4Process(void)
  16857. {
  16858. EXPECT_DECLS;
  16859. #ifndef NO_RC4
  16860. Arc4 enc;
  16861. Arc4 dec;
  16862. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16863. int keyLen = 8;
  16864. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16865. byte cipher[8];
  16866. byte plain[8];
  16867. /* Init stack variables */
  16868. XMEMSET(&enc, 0, sizeof(Arc4));
  16869. XMEMSET(&dec, 0, sizeof(Arc4));
  16870. XMEMSET(cipher, 0, sizeof(cipher));
  16871. XMEMSET(plain, 0, sizeof(plain));
  16872. /* Use for async. */
  16873. ExpectIntEQ(wc_Arc4Init(&enc, NULL, INVALID_DEVID), 0);
  16874. ExpectIntEQ(wc_Arc4Init(&dec, NULL, INVALID_DEVID), 0);
  16875. ExpectIntEQ(wc_Arc4SetKey(&enc, (byte*)key, keyLen), 0);
  16876. ExpectIntEQ(wc_Arc4SetKey(&dec, (byte*)key, keyLen), 0);
  16877. ExpectIntEQ(wc_Arc4Process(&enc, cipher, (byte*)input, keyLen), 0);
  16878. ExpectIntEQ(wc_Arc4Process(&dec, plain, cipher, keyLen), 0);
  16879. ExpectIntEQ(XMEMCMP(plain, input, keyLen), 0);
  16880. /* Bad args. */
  16881. ExpectIntEQ(wc_Arc4Process(NULL, plain, cipher, keyLen), BAD_FUNC_ARG);
  16882. ExpectIntEQ(wc_Arc4Process(&dec, NULL, cipher, keyLen), BAD_FUNC_ARG);
  16883. ExpectIntEQ(wc_Arc4Process(&dec, plain, NULL, keyLen), BAD_FUNC_ARG);
  16884. wc_Arc4Free(&enc);
  16885. wc_Arc4Free(&dec);
  16886. #endif
  16887. return EXPECT_RESULT();
  16888. } /* END test_wc_Arc4Process */
  16889. /*
  16890. * Testing wc_Init RsaKey()
  16891. */
  16892. static int test_wc_InitRsaKey(void)
  16893. {
  16894. EXPECT_DECLS;
  16895. #ifndef NO_RSA
  16896. RsaKey key;
  16897. XMEMSET(&key, 0, sizeof(RsaKey));
  16898. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16899. /* Test bad args. */
  16900. ExpectIntEQ(wc_InitRsaKey(NULL, HEAP_HINT), BAD_FUNC_ARG);
  16901. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16902. #endif
  16903. return EXPECT_RESULT();
  16904. } /* END test_wc_InitRsaKey */
  16905. /*
  16906. * Testing wc_RsaPrivateKeyDecode()
  16907. */
  16908. static int test_wc_RsaPrivateKeyDecode(void)
  16909. {
  16910. EXPECT_DECLS;
  16911. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  16912. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  16913. RsaKey key;
  16914. byte* tmp = NULL;
  16915. word32 idx = 0;
  16916. int bytes = 0;
  16917. XMEMSET(&key, 0, sizeof(RsaKey));
  16918. ExpectNotNull(tmp = (byte*)XMALLOC(FOURK_BUF, NULL,
  16919. DYNAMIC_TYPE_TMP_BUFFER));
  16920. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16921. if (tmp != NULL) {
  16922. #ifdef USE_CERT_BUFFERS_1024
  16923. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  16924. bytes = sizeof_client_key_der_1024;
  16925. #else
  16926. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  16927. bytes = sizeof_client_key_der_2048;
  16928. #endif /* Use cert buffers. */
  16929. }
  16930. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes), 0);
  16931. /* Test bad args. */
  16932. ExpectIntEQ(wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes),
  16933. BAD_FUNC_ARG);
  16934. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes),
  16935. BAD_FUNC_ARG);
  16936. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes),
  16937. BAD_FUNC_ARG);
  16938. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  16939. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16940. #endif
  16941. return EXPECT_RESULT();
  16942. } /* END test_wc_RsaPrivateKeyDecode */
  16943. /*
  16944. * Testing wc_RsaPublicKeyDecode()
  16945. */
  16946. static int test_wc_RsaPublicKeyDecode(void)
  16947. {
  16948. EXPECT_DECLS;
  16949. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  16950. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  16951. RsaKey keyPub;
  16952. byte* tmp = NULL;
  16953. word32 idx = 0;
  16954. int bytes = 0;
  16955. word32 keySz = 0;
  16956. word32 tstKeySz = 0;
  16957. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  16958. XFILE f = XBADFILE;
  16959. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  16960. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  16961. byte buf[4096];
  16962. #endif
  16963. XMEMSET(&keyPub, 0, sizeof(RsaKey));
  16964. ExpectNotNull(tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  16965. ExpectIntEQ(wc_InitRsaKey(&keyPub, HEAP_HINT), 0);
  16966. if (tmp != NULL) {
  16967. #ifdef USE_CERT_BUFFERS_1024
  16968. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  16969. bytes = sizeof_client_keypub_der_1024;
  16970. keySz = 1024;
  16971. #else
  16972. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  16973. bytes = sizeof_client_keypub_der_2048;
  16974. keySz = 2048;
  16975. #endif
  16976. }
  16977. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes), 0);
  16978. /* Pass in bad args. */
  16979. ExpectIntEQ(wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes),
  16980. BAD_FUNC_ARG);
  16981. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes),
  16982. BAD_FUNC_ARG);
  16983. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes),
  16984. BAD_FUNC_ARG);
  16985. DoExpectIntEQ(wc_FreeRsaKey(&keyPub), 0);
  16986. /* Test for getting modulus key size */
  16987. idx = 0;
  16988. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  16989. &tstKeySz, NULL, NULL), 0);
  16990. ExpectIntEQ(tstKeySz, keySz/8);
  16991. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  16992. ExpectTrue((f = XFOPEN(rsaPssPubKey, "rb")) != XBADFILE);
  16993. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  16994. if (f != XBADFILE) {
  16995. XFCLOSE(f);
  16996. f = XBADFILE;
  16997. }
  16998. idx = 0;
  16999. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  17000. NULL), 0);
  17001. ExpectTrue((f = XFOPEN(rsaPssPubKeyNoParams, "rb")) != XBADFILE);
  17002. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  17003. if (f != XBADFILE)
  17004. XFCLOSE(f);
  17005. idx = 0;
  17006. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  17007. NULL), 0);
  17008. #endif
  17009. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17010. #endif
  17011. return EXPECT_RESULT();
  17012. } /* END test_wc_RsaPublicKeyDecode */
  17013. /*
  17014. * Testing wc_RsaPublicKeyDecodeRaw()
  17015. */
  17016. static int test_wc_RsaPublicKeyDecodeRaw(void)
  17017. {
  17018. EXPECT_DECLS;
  17019. #if !defined(NO_RSA)
  17020. RsaKey key;
  17021. const byte n = 0x23;
  17022. const byte e = 0x03;
  17023. int nSz = sizeof(n);
  17024. int eSz = sizeof(e);
  17025. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17026. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key), 0);
  17027. /* Pass in bad args. */
  17028. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key),
  17029. BAD_FUNC_ARG);
  17030. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key),
  17031. BAD_FUNC_ARG);
  17032. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL),
  17033. BAD_FUNC_ARG);
  17034. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17035. #endif
  17036. return EXPECT_RESULT();
  17037. } /* END test_wc_RsaPublicKeyDecodeRaw */
  17038. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17039. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  17040. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  17041. * trying until it gets a probable prime. */
  17042. #ifdef HAVE_FIPS
  17043. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  17044. {
  17045. int ret;
  17046. for (;;) {
  17047. ret = wc_MakeRsaKey(key, size, e, rng);
  17048. if (ret != PRIME_GEN_E) break;
  17049. fprintf(stderr, "MakeRsaKey couldn't find prime; "
  17050. "trying again.\n");
  17051. }
  17052. return ret;
  17053. }
  17054. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  17055. #else
  17056. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  17057. #endif
  17058. #endif
  17059. /*
  17060. * Testing wc_MakeRsaKey()
  17061. */
  17062. static int test_wc_MakeRsaKey(void)
  17063. {
  17064. EXPECT_DECLS;
  17065. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17066. RsaKey genKey;
  17067. WC_RNG rng;
  17068. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17069. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17070. int bits = 1024;
  17071. #else
  17072. int bits = 2048;
  17073. #endif
  17074. XMEMSET(&genKey, 0, sizeof(RsaKey));
  17075. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17076. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  17077. ExpectIntEQ(wc_InitRng(&rng), 0);
  17078. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  17079. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  17080. /* Test bad args. */
  17081. ExpectIntEQ(MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng), BAD_FUNC_ARG);
  17082. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL),
  17083. BAD_FUNC_ARG);
  17084. /* e < 3 */
  17085. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 2, &rng), BAD_FUNC_ARG);
  17086. /* e & 1 == 0 */
  17087. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 6, &rng), BAD_FUNC_ARG);
  17088. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17089. #endif
  17090. return EXPECT_RESULT();
  17091. } /* END test_wc_MakeRsaKey */
  17092. /*
  17093. * Test the bounds checking on the cipher text versus the key modulus.
  17094. * 1. Make a new RSA key.
  17095. * 2. Set c to 1.
  17096. * 3. Decrypt c into k. (error)
  17097. * 4. Copy the key modulus to c and sub 1 from the copy.
  17098. * 5. Decrypt c into k. (error)
  17099. * Valid bounds test cases are covered by all the other RSA tests.
  17100. */
  17101. static int test_RsaDecryptBoundsCheck(void)
  17102. {
  17103. EXPECT_DECLS;
  17104. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  17105. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  17106. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  17107. WC_RNG rng;
  17108. RsaKey key;
  17109. byte flatC[256];
  17110. word32 flatCSz;
  17111. byte out[256];
  17112. word32 outSz = sizeof(out);
  17113. XMEMSET(&key, 0, sizeof(RsaKey));
  17114. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17115. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17116. ExpectIntEQ(wc_InitRng(&rng), 0);
  17117. if (EXPECT_SUCCESS()) {
  17118. const byte* derKey;
  17119. word32 derKeySz;
  17120. word32 idx = 0;
  17121. #ifdef USE_CERT_BUFFERS_1024
  17122. derKey = server_key_der_1024;
  17123. derKeySz = (word32)sizeof_server_key_der_1024;
  17124. flatCSz = 128;
  17125. #else
  17126. derKey = server_key_der_2048;
  17127. derKeySz = (word32)sizeof_server_key_der_2048;
  17128. flatCSz = 256;
  17129. #endif
  17130. ExpectIntEQ(wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz), 0);
  17131. }
  17132. if (EXPECT_SUCCESS()) {
  17133. XMEMSET(flatC, 0, flatCSz);
  17134. flatC[flatCSz-1] = 1;
  17135. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  17136. RSA_PRIVATE_DECRYPT, &rng), RSA_OUT_OF_RANGE_E);
  17137. if (EXPECT_SUCCESS()) {
  17138. mp_int c;
  17139. ExpectIntEQ(mp_init_copy(&c, &key.n), 0);
  17140. ExpectIntEQ(mp_sub_d(&c, 1, &c), 0);
  17141. ExpectIntEQ(mp_to_unsigned_bin(&c, flatC), 0);
  17142. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  17143. RSA_PRIVATE_DECRYPT, NULL), RSA_OUT_OF_RANGE_E);
  17144. mp_clear(&c);
  17145. }
  17146. }
  17147. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17148. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17149. #endif
  17150. return EXPECT_RESULT();
  17151. } /* END test_wc_RsaDecryptBoundsCheck */
  17152. /*
  17153. * Testing wc_SetKeyUsage()
  17154. */
  17155. static int test_wc_SetKeyUsage(void)
  17156. {
  17157. EXPECT_DECLS;
  17158. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  17159. Cert myCert;
  17160. ExpectIntEQ(wc_InitCert(&myCert), 0);
  17161. ExpectIntEQ(wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement"), 0);
  17162. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation"), 0);
  17163. ExpectIntEQ(wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly"), 0);
  17164. ExpectIntEQ(wc_SetKeyUsage(&myCert, "decipherOnly"), 0);
  17165. ExpectIntEQ(wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign"), 0);
  17166. /* Test bad args. */
  17167. ExpectIntEQ(wc_SetKeyUsage(NULL, "decipherOnly"), BAD_FUNC_ARG);
  17168. ExpectIntEQ(wc_SetKeyUsage(&myCert, NULL), BAD_FUNC_ARG);
  17169. ExpectIntEQ(wc_SetKeyUsage(&myCert, ""), KEYUSAGE_E);
  17170. ExpectIntEQ(wc_SetKeyUsage(&myCert, ","), KEYUSAGE_E);
  17171. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign"),
  17172. KEYUSAGE_E);
  17173. #endif
  17174. return EXPECT_RESULT();
  17175. } /* END test_wc_SetKeyUsage */
  17176. /*
  17177. * Testing wc_CheckProbablePrime()
  17178. */
  17179. static int test_wc_CheckProbablePrime(void)
  17180. {
  17181. EXPECT_DECLS;
  17182. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17183. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  17184. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  17185. RsaKey key;
  17186. WC_RNG rng;
  17187. byte e[3];
  17188. word32 eSz = (word32)sizeof(e);
  17189. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  17190. word32 nSz = (word32)sizeof(n);
  17191. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  17192. word32 dSz = (word32)sizeof(d);
  17193. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  17194. word32 pSz = (word32)sizeof(p);
  17195. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  17196. word32 qSz = (word32)sizeof(q);
  17197. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  17198. int* isPrime;
  17199. int test[5];
  17200. isPrime = test;
  17201. XMEMSET(&key, 0, sizeof(RsaKey));
  17202. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17203. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17204. ExpectIntEQ(wc_InitRng(&rng), 0);
  17205. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17206. ExpectIntEQ(wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS,
  17207. WC_RSA_EXPONENT, &rng), 0);
  17208. PRIVATE_KEY_UNLOCK();
  17209. ExpectIntEQ(wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q,
  17210. &qSz), 0);
  17211. PRIVATE_KEY_LOCK();
  17212. /* Bad cases */
  17213. ExpectIntEQ(wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz, nlen, isPrime),
  17214. BAD_FUNC_ARG);
  17215. ExpectIntEQ(wc_CheckProbablePrime(p, 0, q, qSz, e, eSz, nlen, isPrime),
  17216. BAD_FUNC_ARG);
  17217. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz, nlen, isPrime),
  17218. BAD_FUNC_ARG);
  17219. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, 0, e, eSz, nlen, isPrime),
  17220. BAD_FUNC_ARG);
  17221. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz, nlen, isPrime),
  17222. BAD_FUNC_ARG);
  17223. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, 0, nlen, isPrime),
  17224. BAD_FUNC_ARG);
  17225. ExpectIntEQ(wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0, nlen, isPrime),
  17226. BAD_FUNC_ARG);
  17227. /* Good case */
  17228. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz, nlen, isPrime),
  17229. 0);
  17230. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17231. wc_FreeRng(&rng);
  17232. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  17233. #endif
  17234. return EXPECT_RESULT();
  17235. } /* END test_wc_CheckProbablePrime */
  17236. /*
  17237. * Testing wc_RsaPSS_Verify()
  17238. */
  17239. static int test_wc_RsaPSS_Verify(void)
  17240. {
  17241. EXPECT_DECLS;
  17242. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17243. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17244. RsaKey key;
  17245. WC_RNG rng;
  17246. int sz = 256;
  17247. const char* szMessage = "This is the string to be signed";
  17248. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17249. unsigned char pDecrypted[2048/8];
  17250. byte* pt = pDecrypted;
  17251. word32 outLen = sizeof(pDecrypted);
  17252. XMEMSET(&key, 0, sizeof(RsaKey));
  17253. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17254. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17255. ExpectIntEQ(wc_InitRng(&rng), 0);
  17256. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17257. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17258. ExpectIntGT(sz = wc_RsaPSS_Sign((byte*)szMessage,
  17259. (word32)XSTRLEN(szMessage)+1, pSignature, sizeof(pSignature),
  17260. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17261. /* Bad cases */
  17262. ExpectIntEQ(wc_RsaPSS_Verify(NULL, sz, pt, outLen,
  17263. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17264. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  17265. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17266. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, sz, NULL, outLen,
  17267. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17268. ExpectIntEQ(wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  17269. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17270. /* Good case */
  17271. ExpectIntGT(wc_RsaPSS_Verify(pSignature, sz, pt, outLen,
  17272. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17273. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17274. wc_FreeRng(&rng);
  17275. #endif
  17276. return EXPECT_RESULT();
  17277. } /* END test_wc_RsaPSS_Verify */
  17278. /*
  17279. * Testing wc_RsaPSS_VerifyCheck()
  17280. */
  17281. static int test_wc_RsaPSS_VerifyCheck(void)
  17282. {
  17283. EXPECT_DECLS;
  17284. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17285. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17286. RsaKey key;
  17287. WC_RNG rng;
  17288. int sz = 256; /* 2048/8 */
  17289. byte digest[32];
  17290. word32 digestSz = sizeof(digest);
  17291. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17292. word32 pSignatureSz = sizeof(pSignature);
  17293. unsigned char pDecrypted[2048/8];
  17294. byte* pt = pDecrypted;
  17295. word32 outLen = sizeof(pDecrypted);
  17296. XMEMSET(&key, 0, sizeof(RsaKey));
  17297. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17298. XMEMSET(digest, 0, sizeof(digest));
  17299. XMEMSET(pSignature, 0, sizeof(pSignature));
  17300. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17301. ExpectIntEQ(wc_InitRng(&rng), 0);
  17302. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17303. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17304. ExpectTrue((digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  17305. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz),
  17306. 0);
  17307. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  17308. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17309. /* Bad cases */
  17310. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, sz, pt, outLen, digest,
  17311. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17312. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen, digest,
  17313. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17314. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, sz, NULL, outLen, digest,
  17315. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17316. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen, digest,
  17317. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17318. /* Good case */
  17319. ExpectIntGT(wc_RsaPSS_VerifyCheck(pSignature, sz, pt, outLen, digest,
  17320. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17321. ExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17322. wc_FreeRng(&rng);
  17323. #endif
  17324. return EXPECT_RESULT();
  17325. } /* END test_wc_RsaPSS_VerifyCheck */
  17326. /*
  17327. * Testing wc_RsaPSS_VerifyCheckInline()
  17328. */
  17329. static int test_wc_RsaPSS_VerifyCheckInline(void)
  17330. {
  17331. EXPECT_DECLS;
  17332. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17333. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17334. RsaKey key;
  17335. WC_RNG rng;
  17336. int sz = 256;
  17337. byte digest[32];
  17338. word32 digestSz = sizeof(digest);
  17339. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17340. unsigned char pDecrypted[2048/8];
  17341. byte* pt = pDecrypted;
  17342. XMEMSET(&key, 0, sizeof(RsaKey));
  17343. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17344. XMEMSET(digest, 0, sizeof(digest));
  17345. XMEMSET(pSignature, 0, sizeof(pSignature));
  17346. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17347. ExpectIntEQ(wc_InitRng(&rng), 0);
  17348. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17349. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17350. ExpectTrue((digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  17351. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz),
  17352. 0);
  17353. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature,
  17354. sizeof(pSignature), WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17355. /* Bad Cases */
  17356. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, sz, &pt, digest,
  17357. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17358. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL, digest,
  17359. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17360. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt, digest,
  17361. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17362. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt, digest,
  17363. digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17364. /* Good case */
  17365. ExpectIntGT(wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt, digest,
  17366. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17367. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17368. wc_FreeRng(&rng);
  17369. #endif
  17370. return EXPECT_RESULT();
  17371. } /* END test_wc_RsaPSS_VerifyCheckInline */
  17372. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17373. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  17374. {
  17375. (void)flag;
  17376. (void)type;
  17377. (void)file;
  17378. (void)line;
  17379. }
  17380. #endif
  17381. /*
  17382. * Testing wc_LockMutex_ex
  17383. */
  17384. static int test_wc_LockMutex_ex(void)
  17385. {
  17386. EXPECT_DECLS;
  17387. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17388. int flag = CRYPTO_LOCK;
  17389. int type = 0;
  17390. const char* file = "./test-LockMutex_ex.txt";
  17391. int line = 0;
  17392. /* without SetMutexCb */
  17393. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), BAD_STATE_E);
  17394. /* with SetMutexCb */
  17395. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  17396. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), 0);
  17397. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  17398. #endif
  17399. return EXPECT_RESULT();
  17400. } /* End test_wc_LockMutex_ex*/
  17401. /*
  17402. * Testing wc_SetMutexCb
  17403. */
  17404. static int test_wc_SetMutexCb(void)
  17405. {
  17406. EXPECT_DECLS;
  17407. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17408. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  17409. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  17410. #endif
  17411. return EXPECT_RESULT();
  17412. } /* End test_wc_SetMutexCb*/
  17413. /*
  17414. * Testing wc_RsaKeyToDer()
  17415. */
  17416. static int test_wc_RsaKeyToDer(void)
  17417. {
  17418. EXPECT_DECLS;
  17419. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17420. RsaKey genKey;
  17421. WC_RNG rng;
  17422. byte* der = NULL;
  17423. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17424. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17425. int bits = 1024;
  17426. word32 derSz = 611;
  17427. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  17428. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  17429. #else
  17430. int bits = 2048;
  17431. word32 derSz = 1196;
  17432. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  17433. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  17434. #endif
  17435. XMEMSET(&rng, 0, sizeof(rng));
  17436. XMEMSET(&genKey, 0, sizeof(genKey));
  17437. ExpectNotNull(der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17438. /* Init structures. */
  17439. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  17440. ExpectIntEQ(wc_InitRng(&rng), 0);
  17441. /* Make key. */
  17442. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  17443. ExpectIntGT(wc_RsaKeyToDer(&genKey, der, derSz), 0);
  17444. /* Pass good/bad args. */
  17445. ExpectIntEQ(wc_RsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  17446. /* Get just the output length */
  17447. ExpectIntGT(wc_RsaKeyToDer(&genKey, NULL, 0), 0);
  17448. /* Try Public Key. */
  17449. genKey.type = 0;
  17450. ExpectIntEQ(wc_RsaKeyToDer(&genKey, der, FOURK_BUF), BAD_FUNC_ARG);
  17451. #ifdef WOLFSSL_CHECK_MEM_ZERO
  17452. /* Put back to Private Key */
  17453. genKey.type = 1;
  17454. #endif
  17455. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17456. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  17457. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17458. #endif
  17459. return EXPECT_RESULT();
  17460. } /* END test_wc_RsaKeyToDer */
  17461. /*
  17462. * Testing wc_RsaKeyToPublicDer()
  17463. */
  17464. static int test_wc_RsaKeyToPublicDer(void)
  17465. {
  17466. EXPECT_DECLS;
  17467. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17468. RsaKey key;
  17469. WC_RNG rng;
  17470. byte* der = NULL;
  17471. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17472. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17473. int bits = 1024;
  17474. word32 derLen = 162;
  17475. #else
  17476. int bits = 2048;
  17477. word32 derLen = 294;
  17478. #endif
  17479. int ret;
  17480. XMEMSET(&rng, 0, sizeof(rng));
  17481. XMEMSET(&key, 0, sizeof(key));
  17482. ExpectNotNull(der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17483. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17484. ExpectIntEQ(wc_InitRng(&rng), 0);
  17485. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17486. /* test getting size only */
  17487. ExpectIntGT(wc_RsaKeyToPublicDer(&key, NULL, derLen), 0);
  17488. ExpectIntGT(wc_RsaKeyToPublicDer(&key, der, derLen), 0);
  17489. /* test getting size only */
  17490. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0), 0);
  17491. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0), 0);
  17492. /* Pass in bad args. */
  17493. ExpectIntEQ(wc_RsaKeyToPublicDer(NULL, der, derLen), BAD_FUNC_ARG);
  17494. ExpectIntLT(ret = wc_RsaKeyToPublicDer(&key, der, -1), 0);
  17495. ExpectTrue((ret == BUFFER_E) || (ret == BAD_FUNC_ARG));
  17496. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17497. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17498. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17499. #endif
  17500. return EXPECT_RESULT();
  17501. } /* END test_wc_RsaKeyToPublicDer */
  17502. /*
  17503. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  17504. */
  17505. static int test_wc_RsaPublicEncryptDecrypt(void)
  17506. {
  17507. EXPECT_DECLS;
  17508. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17509. RsaKey key;
  17510. WC_RNG rng;
  17511. const char inStr[] = TEST_STRING;
  17512. const word32 plainLen = (word32)TEST_STRING_SZ;
  17513. const word32 inLen = (word32)TEST_STRING_SZ;
  17514. int bits = TEST_RSA_BITS;
  17515. const word32 cipherLen = TEST_RSA_BYTES;
  17516. word32 cipherLenResult = cipherLen;
  17517. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17518. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17519. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17520. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17521. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17522. WC_ALLOC_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17523. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17524. ExpectNotNull(in);
  17525. ExpectNotNull(plain);
  17526. ExpectNotNull(cipher);
  17527. #endif
  17528. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17529. /* Initialize stack structures. */
  17530. XMEMSET(&key, 0, sizeof(RsaKey));
  17531. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17532. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17533. ExpectIntEQ(wc_InitRng(&rng), 0);
  17534. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17535. /* Encrypt. */
  17536. ExpectIntGT(cipherLenResult = wc_RsaPublicEncrypt(in, inLen, cipher,
  17537. cipherLen, &key, &rng), 0);
  17538. /* Pass bad args - tested in another testing function.*/
  17539. /* Decrypt */
  17540. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17541. /* Bind rng */
  17542. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17543. #endif
  17544. ExpectIntGE(wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen,
  17545. &key), 0);
  17546. ExpectIntEQ(XMEMCMP(plain, inStr, plainLen), 0);
  17547. /* Pass bad args - tested in another testing function.*/
  17548. WC_FREE_VAR(in, NULL);
  17549. WC_FREE_VAR(plain, NULL);
  17550. WC_FREE_VAR(cipher, NULL);
  17551. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17552. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17553. #endif
  17554. return EXPECT_RESULT();
  17555. } /* END test_wc_RsaPublicEncryptDecrypt */
  17556. /*
  17557. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  17558. */
  17559. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  17560. {
  17561. EXPECT_DECLS;
  17562. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  17563. && !defined(WC_NO_RSA_OAEP) && !defined(NO_SHA256)
  17564. RsaKey key;
  17565. WC_RNG rng;
  17566. const char inStr[] = TEST_STRING;
  17567. const word32 inLen = (word32)TEST_STRING_SZ;
  17568. const word32 plainSz = (word32)TEST_STRING_SZ;
  17569. byte* res = NULL;
  17570. int idx = 0;
  17571. int bits = TEST_RSA_BITS;
  17572. const word32 cipherSz = TEST_RSA_BYTES;
  17573. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17574. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17575. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17576. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17577. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17578. WC_ALLOC_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17579. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17580. ExpectNotNull(in);
  17581. ExpectNotNull(plain);
  17582. ExpectNotNull(cipher);
  17583. #endif
  17584. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17585. /* Initialize stack structures. */
  17586. XMEMSET(&key, 0, sizeof(RsaKey));
  17587. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17588. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID), 0);
  17589. ExpectIntEQ(wc_InitRng(&rng), 0);
  17590. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17591. /* Encrypt */
  17592. ExpectIntGE(idx = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key,
  17593. &rng, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17594. /* Pass bad args - tested in another testing function.*/
  17595. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17596. /* Decrypt */
  17597. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17598. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17599. #endif
  17600. ExpectIntGE(wc_RsaPrivateDecrypt_ex(cipher, (word32)idx, plain, plainSz,
  17601. &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17602. ExpectIntEQ(XMEMCMP(plain, inStr, plainSz), 0);
  17603. /* Pass bad args - tested in another testing function.*/
  17604. ExpectIntGE(wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx, &res, &key,
  17605. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17606. ExpectIntEQ(XMEMCMP(inStr, res, plainSz), 0);
  17607. #endif
  17608. WC_FREE_VAR(in, NULL);
  17609. WC_FREE_VAR(plain, NULL);
  17610. WC_FREE_VAR(cipher, NULL);
  17611. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17612. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17613. #endif
  17614. return EXPECT_RESULT();
  17615. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  17616. /*
  17617. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  17618. */
  17619. static int test_wc_RsaSSL_SignVerify(void)
  17620. {
  17621. EXPECT_DECLS;
  17622. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17623. RsaKey key;
  17624. WC_RNG rng;
  17625. const char inStr[] = TEST_STRING;
  17626. const word32 plainSz = (word32)TEST_STRING_SZ;
  17627. const word32 inLen = (word32)TEST_STRING_SZ;
  17628. word32 idx = 0;
  17629. int bits = TEST_RSA_BITS;
  17630. const word32 outSz = TEST_RSA_BYTES;
  17631. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17632. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  17633. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17634. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17635. WC_ALLOC_VAR(out, byte, TEST_RSA_BYTES, NULL);
  17636. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17637. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17638. ExpectNotNull(in);
  17639. ExpectNotNull(out);
  17640. ExpectNotNull(plain);
  17641. #endif
  17642. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17643. XMEMSET(&key, 0, sizeof(RsaKey));
  17644. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17645. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17646. ExpectIntEQ(wc_InitRng(&rng), 0);
  17647. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17648. /* Sign. */
  17649. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng), (int)outSz);
  17650. idx = (int)outSz;
  17651. /* Test bad args. */
  17652. ExpectIntEQ(wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng),
  17653. BAD_FUNC_ARG);
  17654. ExpectIntEQ(wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng),
  17655. BAD_FUNC_ARG);
  17656. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng),
  17657. BAD_FUNC_ARG);
  17658. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng),
  17659. BAD_FUNC_ARG);
  17660. /* Verify. */
  17661. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, &key), (int)inLen);
  17662. /* Pass bad args. */
  17663. ExpectIntEQ(wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key),
  17664. BAD_FUNC_ARG);
  17665. ExpectIntEQ(wc_RsaSSL_Verify(out, 0, plain, plainSz, &key),
  17666. BAD_FUNC_ARG);
  17667. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key),
  17668. BAD_FUNC_ARG);
  17669. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL),
  17670. BAD_FUNC_ARG);
  17671. WC_FREE_VAR(in, NULL);
  17672. WC_FREE_VAR(out, NULL);
  17673. WC_FREE_VAR(plain, NULL);
  17674. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17675. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17676. #endif
  17677. return EXPECT_RESULT();
  17678. } /* END test_wc_RsaSSL_SignVerify */
  17679. /*
  17680. * Testing wc_RsaEncryptSize()
  17681. */
  17682. static int test_wc_RsaEncryptSize(void)
  17683. {
  17684. EXPECT_DECLS;
  17685. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17686. RsaKey key;
  17687. WC_RNG rng;
  17688. XMEMSET(&key, 0, sizeof(RsaKey));
  17689. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17690. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17691. ExpectIntEQ(wc_InitRng(&rng), 0);
  17692. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17693. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17694. ExpectIntEQ(MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng), 0);
  17695. ExpectIntEQ(wc_RsaEncryptSize(&key), 128);
  17696. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17697. #endif
  17698. ExpectIntEQ(MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17699. ExpectIntEQ(wc_RsaEncryptSize(&key), 256);
  17700. /* Pass in bad arg. */
  17701. ExpectIntEQ(wc_RsaEncryptSize(NULL), BAD_FUNC_ARG);
  17702. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17703. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17704. #endif
  17705. return EXPECT_RESULT();
  17706. } /* END test_wc_RsaEncryptSize*/
  17707. /*
  17708. * Testing wc_RsaFlattenPublicKey()
  17709. */
  17710. static int test_wc_RsaFlattenPublicKey(void)
  17711. {
  17712. EXPECT_DECLS;
  17713. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17714. RsaKey key;
  17715. WC_RNG rng;
  17716. byte e[256];
  17717. byte n[256];
  17718. word32 eSz = sizeof(e);
  17719. word32 nSz = sizeof(n);
  17720. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17721. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17722. int bits = 1024;
  17723. #else
  17724. int bits = 2048;
  17725. #endif
  17726. XMEMSET(&key, 0, sizeof(RsaKey));
  17727. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17728. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17729. ExpectIntEQ(wc_InitRng(&rng), 0);
  17730. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17731. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz), 0);
  17732. /* Pass bad args. */
  17733. ExpectIntEQ(wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz),
  17734. BAD_FUNC_ARG);
  17735. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz),
  17736. BAD_FUNC_ARG);
  17737. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz),
  17738. BAD_FUNC_ARG);
  17739. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz),
  17740. BAD_FUNC_ARG);
  17741. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL),
  17742. BAD_FUNC_ARG);
  17743. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17744. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17745. #endif
  17746. return EXPECT_RESULT();
  17747. } /* END test_wc_RsaFlattenPublicKey */
  17748. /*
  17749. * unit test for wc_AesCcmSetKey
  17750. */
  17751. static int test_wc_AesCcmSetKey(void)
  17752. {
  17753. EXPECT_DECLS;
  17754. #ifdef HAVE_AESCCM
  17755. Aes aes;
  17756. const byte key16[] = {
  17757. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17758. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17759. };
  17760. const byte key24[] = {
  17761. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17762. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17763. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  17764. };
  17765. const byte key32[] = {
  17766. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17767. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17768. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17769. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  17770. };
  17771. XMEMSET(&aes, 0, sizeof(Aes));
  17772. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  17773. #ifdef WOLFSSL_AES_128
  17774. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  17775. #endif
  17776. #ifdef WOLFSSL_AES_192
  17777. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24)), 0);
  17778. #endif
  17779. #ifdef WOLFSSL_AES_256
  17780. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32)), 0);
  17781. #endif
  17782. /* Test bad args. */
  17783. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1), BAD_FUNC_ARG);
  17784. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1), BAD_FUNC_ARG);
  17785. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1), BAD_FUNC_ARG);
  17786. wc_AesFree(&aes);
  17787. #endif
  17788. return EXPECT_RESULT();
  17789. } /* END test_wc_AesCcmSetKey */
  17790. /*
  17791. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  17792. */
  17793. static int test_wc_AesCcmEncryptDecrypt(void)
  17794. {
  17795. EXPECT_DECLS;
  17796. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  17797. Aes aes;
  17798. const byte key16[] = {
  17799. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17800. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17801. };
  17802. /* plaintext */
  17803. const byte plainT[] = {
  17804. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  17805. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  17806. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  17807. };
  17808. /* nonce */
  17809. const byte iv[] = {
  17810. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  17811. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  17812. };
  17813. const byte c[] = { /* cipher text. */
  17814. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  17815. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  17816. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  17817. };
  17818. const byte t[] = { /* Auth tag */
  17819. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  17820. };
  17821. const byte authIn[] = {
  17822. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  17823. };
  17824. byte cipherOut[sizeof(plainT)];
  17825. byte authTag[sizeof(t)];
  17826. #ifdef HAVE_AES_DECRYPT
  17827. byte plainOut[sizeof(cipherOut)];
  17828. #endif
  17829. XMEMSET(&aes, 0, sizeof(Aes));
  17830. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  17831. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  17832. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17833. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)), 0);
  17834. ExpectIntEQ(XMEMCMP(cipherOut, c, sizeof(c)), 0);
  17835. ExpectIntEQ(XMEMCMP(t, authTag, sizeof(t)), 0);
  17836. #ifdef HAVE_AES_DECRYPT
  17837. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17838. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)), 0);
  17839. ExpectIntEQ(XMEMCMP(plainOut, plainT, sizeof(plainT)), 0);
  17840. #endif
  17841. /* Pass in bad args. Encrypt*/
  17842. ExpectIntEQ(wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  17843. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17844. BAD_FUNC_ARG);
  17845. ExpectIntEQ(wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  17846. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17847. BAD_FUNC_ARG);
  17848. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  17849. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17850. BAD_FUNC_ARG);
  17851. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17852. NULL, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17853. BAD_FUNC_ARG);
  17854. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17855. iv, sizeof(iv), NULL, sizeof(authTag), authIn , sizeof(authIn)),
  17856. BAD_FUNC_ARG);
  17857. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17858. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17859. BAD_FUNC_ARG);
  17860. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17861. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17862. BAD_FUNC_ARG);
  17863. #ifdef HAVE_AES_DECRYPT
  17864. /* Pass in bad args. Decrypt*/
  17865. ExpectIntEQ(wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  17866. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17867. BAD_FUNC_ARG);
  17868. ExpectIntEQ(wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  17869. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17870. BAD_FUNC_ARG);
  17871. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  17872. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17873. BAD_FUNC_ARG);
  17874. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17875. NULL, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17876. BAD_FUNC_ARG);
  17877. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17878. iv, sizeof(iv), NULL, sizeof(authTag), authIn, sizeof(authIn)),
  17879. BAD_FUNC_ARG);
  17880. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17881. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17882. BAD_FUNC_ARG);
  17883. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17884. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17885. BAD_FUNC_ARG);
  17886. #endif
  17887. wc_AesFree(&aes);
  17888. #endif /* HAVE_AESCCM */
  17889. return EXPECT_RESULT();
  17890. } /* END test_wc_AesCcmEncryptDecrypt */
  17891. #if defined(WOLFSSL_AES_EAX) && \
  17892. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  17893. /*
  17894. * Testing test_wc_AesEaxVectors()
  17895. */
  17896. static int test_wc_AesEaxVectors(void)
  17897. {
  17898. EXPECT_DECLS;
  17899. typedef struct {
  17900. byte key[AES_256_KEY_SIZE];
  17901. int key_length;
  17902. byte iv[AES_BLOCK_SIZE];
  17903. int iv_length;
  17904. byte aad[AES_BLOCK_SIZE * 2];
  17905. int aad_length;
  17906. byte msg[AES_BLOCK_SIZE * 5];
  17907. int msg_length;
  17908. byte ct[AES_BLOCK_SIZE * 5];
  17909. int ct_length;
  17910. byte tag[AES_BLOCK_SIZE];
  17911. int tag_length;
  17912. int valid;
  17913. } AadVector;
  17914. /* Test vectors obtained from Google wycheproof project
  17915. * https://github.com/google/wycheproof
  17916. * from testvectors/aes_eax_test.json
  17917. */
  17918. const AadVector vectors[] = {
  17919. {
  17920. /* key, key length */
  17921. {0x23, 0x39, 0x52, 0xde, 0xe4, 0xd5, 0xed, 0x5f,
  17922. 0x9b, 0x9c, 0x6d, 0x6f, 0xf8, 0x0f, 0xf4, 0x78}, 16,
  17923. /* iv, iv length */
  17924. {0x62, 0xec, 0x67, 0xf9, 0xc3, 0xa4, 0xa4, 0x07,
  17925. 0xfc, 0xb2, 0xa8, 0xc4, 0x90, 0x31, 0xa8, 0xb3}, 16,
  17926. /* aad, aad length */
  17927. {0x6b, 0xfb, 0x91, 0x4f, 0xd0, 0x7e, 0xae, 0x6b}, 8,
  17928. /* msg, msg length */
  17929. {0x00}, 0,
  17930. /* ct, ct length */
  17931. {0x00}, 0,
  17932. /* tag, tag length */
  17933. {0xe0, 0x37, 0x83, 0x0e, 0x83, 0x89, 0xf2, 0x7b,
  17934. 0x02, 0x5a, 0x2d, 0x65, 0x27, 0xe7, 0x9d, 0x01}, 16,
  17935. /* valid */
  17936. 1,
  17937. },
  17938. {
  17939. /* key, key length */
  17940. {0x91, 0x94, 0x5d, 0x3f, 0x4d, 0xcb, 0xee, 0x0b,
  17941. 0xf4, 0x5e, 0xf5, 0x22, 0x55, 0xf0, 0x95, 0xa4}, 16,
  17942. /* iv, iv length */
  17943. {0xbe, 0xca, 0xf0, 0x43, 0xb0, 0xa2, 0x3d, 0x84,
  17944. 0x31, 0x94, 0xba, 0x97, 0x2c, 0x66, 0xde, 0xbd}, 16,
  17945. /* aad, aad length */
  17946. {0xfa, 0x3b, 0xfd, 0x48, 0x06, 0xeb, 0x53, 0xfa}, 8,
  17947. /* msg, msg length */
  17948. {0xf7, 0xfb}, 2,
  17949. /* ct, ct length */
  17950. {0x19, 0xdd}, 2,
  17951. /* tag, tag length */
  17952. {0x5c, 0x4c, 0x93, 0x31, 0x04, 0x9d, 0x0b, 0xda,
  17953. 0xb0, 0x27, 0x74, 0x08, 0xf6, 0x79, 0x67, 0xe5}, 16,
  17954. /* valid */
  17955. 1,
  17956. },
  17957. {
  17958. /* key, key length */
  17959. {0x01, 0xf7, 0x4a, 0xd6, 0x40, 0x77, 0xf2, 0xe7,
  17960. 0x04, 0xc0, 0xf6, 0x0a, 0xda, 0x3d, 0xd5, 0x23}, 16,
  17961. /* iv, iv length */
  17962. {0x70, 0xc3, 0xdb, 0x4f, 0x0d, 0x26, 0x36, 0x84,
  17963. 0x00, 0xa1, 0x0e, 0xd0, 0x5d, 0x2b, 0xff, 0x5e}, 16,
  17964. /* aad, aad length */
  17965. {0x23, 0x4a, 0x34, 0x63, 0xc1, 0x26, 0x4a, 0xc6}, 8,
  17966. /* msg, msg length */
  17967. {0x1a, 0x47, 0xcb, 0x49, 0x33}, 5,
  17968. /* ct, ct length */
  17969. {0xd8, 0x51, 0xd5, 0xba, 0xe0}, 5,
  17970. /* tag, tag length */
  17971. {0x3a, 0x59, 0xf2, 0x38, 0xa2, 0x3e, 0x39, 0x19,
  17972. 0x9d, 0xc9, 0x26, 0x66, 0x26, 0xc4, 0x0f, 0x80}, 16,
  17973. /* valid */
  17974. 1,
  17975. },
  17976. {
  17977. /* key, key length */
  17978. {0xd0, 0x7c, 0xf6, 0xcb, 0xb7, 0xf3, 0x13, 0xbd,
  17979. 0xde, 0x66, 0xb7, 0x27, 0xaf, 0xd3, 0xc5, 0xe8}, 16,
  17980. /* iv, iv length */
  17981. {0x84, 0x08, 0xdf, 0xff, 0x3c, 0x1a, 0x2b, 0x12,
  17982. 0x92, 0xdc, 0x19, 0x9e, 0x46, 0xb7, 0xd6, 0x17}, 16,
  17983. /* aad, aad length */
  17984. {0x33, 0xcc, 0xe2, 0xea, 0xbf, 0xf5, 0xa7, 0x9d}, 8,
  17985. /* msg, msg length */
  17986. {0x48, 0x1c, 0x9e, 0x39, 0xb1}, 5,
  17987. /* ct, ct length */
  17988. {0x63, 0x2a, 0x9d, 0x13, 0x1a}, 5,
  17989. /* tag, tag length */
  17990. {0xd4, 0xc1, 0x68, 0xa4, 0x22, 0x5d, 0x8e, 0x1f,
  17991. 0xf7, 0x55, 0x93, 0x99, 0x74, 0xa7, 0xbe, 0xde}, 16,
  17992. /* valid */
  17993. 1,
  17994. },
  17995. {
  17996. /* key, key length */
  17997. {0x35, 0xb6, 0xd0, 0x58, 0x00, 0x05, 0xbb, 0xc1,
  17998. 0x2b, 0x05, 0x87, 0x12, 0x45, 0x57, 0xd2, 0xc2}, 16,
  17999. /* iv, iv length */
  18000. {0xfd, 0xb6, 0xb0, 0x66, 0x76, 0xee, 0xdc, 0x5c,
  18001. 0x61, 0xd7, 0x42, 0x76, 0xe1, 0xf8, 0xe8, 0x16}, 16,
  18002. /* aad, aad length */
  18003. {0xae, 0xb9, 0x6e, 0xae, 0xbe, 0x29, 0x70, 0xe9}, 8,
  18004. /* msg, msg length */
  18005. {0x40, 0xd0, 0xc0, 0x7d, 0xa5, 0xe4}, 6,
  18006. /* ct, ct length */
  18007. {0x07, 0x1d, 0xfe, 0x16, 0xc6, 0x75}, 6,
  18008. /* tag, tag length */
  18009. {0xcb, 0x06, 0x77, 0xe5, 0x36, 0xf7, 0x3a, 0xfe,
  18010. 0x6a, 0x14, 0xb7, 0x4e, 0xe4, 0x98, 0x44, 0xdd}, 16,
  18011. /* valid */
  18012. 1,
  18013. },
  18014. {
  18015. /* key, key length */
  18016. {0xbd, 0x8e, 0x6e, 0x11, 0x47, 0x5e, 0x60, 0xb2,
  18017. 0x68, 0x78, 0x4c, 0x38, 0xc6, 0x2f, 0xeb, 0x22}, 16,
  18018. /* iv, iv length */
  18019. {0x6e, 0xac, 0x5c, 0x93, 0x07, 0x2d, 0x8e, 0x85,
  18020. 0x13, 0xf7, 0x50, 0x93, 0x5e, 0x46, 0xda, 0x1b}, 16,
  18021. /* aad, aad length */
  18022. {0xd4, 0x48, 0x2d, 0x1c, 0xa7, 0x8d, 0xce, 0x0f}, 8,
  18023. /* msg, msg length */
  18024. {0x4d, 0xe3, 0xb3, 0x5c, 0x3f, 0xc0, 0x39, 0x24,
  18025. 0x5b, 0xd1, 0xfb, 0x7d}, 12,
  18026. /* ct, ct length */
  18027. {0x83, 0x5b, 0xb4, 0xf1, 0x5d, 0x74, 0x3e, 0x35,
  18028. 0x0e, 0x72, 0x84, 0x14}, 12,
  18029. /* tag, tag length */
  18030. {0xab, 0xb8, 0x64, 0x4f, 0xd6, 0xcc, 0xb8, 0x69,
  18031. 0x47, 0xc5, 0xe1, 0x05, 0x90, 0x21, 0x0a, 0x4f}, 16,
  18032. /* valid */
  18033. 1,
  18034. },
  18035. {
  18036. /* key, key length */
  18037. {0x7c, 0x77, 0xd6, 0xe8, 0x13, 0xbe, 0xd5, 0xac,
  18038. 0x98, 0xba, 0xa4, 0x17, 0x47, 0x7a, 0x2e, 0x7d}, 16,
  18039. /* iv, iv length */
  18040. {0x1a, 0x8c, 0x98, 0xdc, 0xd7, 0x3d, 0x38, 0x39,
  18041. 0x3b, 0x2b, 0xf1, 0x56, 0x9d, 0xee, 0xfc, 0x19}, 16,
  18042. /* aad, aad length */
  18043. {0x65, 0xd2, 0x01, 0x79, 0x90, 0xd6, 0x25, 0x28}, 8,
  18044. /* msg, msg length */
  18045. {0x8b, 0x0a, 0x79, 0x30, 0x6c, 0x9c, 0xe7, 0xed,
  18046. 0x99, 0xda, 0xe4, 0xf8, 0x7f, 0x8d, 0xd6, 0x16,
  18047. 0x36}, 17,
  18048. /* ct, ct length */
  18049. {0x02, 0x08, 0x3e, 0x39, 0x79, 0xda, 0x01, 0x48,
  18050. 0x12, 0xf5, 0x9f, 0x11, 0xd5, 0x26, 0x30, 0xda,
  18051. 0x30}, 17,
  18052. /* tag, tag length */
  18053. {0x13, 0x73, 0x27, 0xd1, 0x06, 0x49, 0xb0, 0xaa,
  18054. 0x6e, 0x1c, 0x18, 0x1d, 0xb6, 0x17, 0xd7, 0xf2}, 16,
  18055. /* valid */
  18056. 1,
  18057. },
  18058. {
  18059. /* key, key length */
  18060. {0x5f, 0xff, 0x20, 0xca, 0xfa, 0xb1, 0x19, 0xca,
  18061. 0x2f, 0xc7, 0x35, 0x49, 0xe2, 0x0f, 0x5b, 0x0d}, 16,
  18062. /* iv, iv length */
  18063. {0xdd, 0xe5, 0x9b, 0x97, 0xd7, 0x22, 0x15, 0x6d,
  18064. 0x4d, 0x9a, 0xff, 0x2b, 0xc7, 0x55, 0x98, 0x26}, 16,
  18065. /* aad, aad length */
  18066. {0x54, 0xb9, 0xf0, 0x4e, 0x6a, 0x09, 0x18, 0x9a}, 8,
  18067. /* msg, msg length */
  18068. {0x1b, 0xda, 0x12, 0x2b, 0xce, 0x8a, 0x8d, 0xba,
  18069. 0xf1, 0x87, 0x7d, 0x96, 0x2b, 0x85, 0x92, 0xdd,
  18070. 0x2d, 0x56}, 18,
  18071. /* ct, ct length */
  18072. {0x2e, 0xc4, 0x7b, 0x2c, 0x49, 0x54, 0xa4, 0x89,
  18073. 0xaf, 0xc7, 0xba, 0x48, 0x97, 0xed, 0xcd, 0xae,
  18074. 0x8c, 0xc3}, 18,
  18075. /* tag, tag length */
  18076. {0x3b, 0x60, 0x45, 0x05, 0x99, 0xbd, 0x02, 0xc9,
  18077. 0x63, 0x82, 0x90, 0x2a, 0xef, 0x7f, 0x83, 0x2a}, 16,
  18078. /* valid */
  18079. 1,
  18080. },
  18081. {
  18082. /* key, key length */
  18083. {0xa4, 0xa4, 0x78, 0x2b, 0xcf, 0xfd, 0x3e, 0xc5,
  18084. 0xe7, 0xef, 0x6d, 0x8c, 0x34, 0xa5, 0x61, 0x23}, 16,
  18085. /* iv, iv length */
  18086. {0xb7, 0x81, 0xfc, 0xf2, 0xf7, 0x5f, 0xa5, 0xa8,
  18087. 0xde, 0x97, 0xa9, 0xca, 0x48, 0xe5, 0x22, 0xec}, 16,
  18088. /* aad, aad length */
  18089. {0x89, 0x9a, 0x17, 0x58, 0x97, 0x56, 0x1d, 0x7e}, 8,
  18090. /* msg, msg length */
  18091. {0x6c, 0xf3, 0x67, 0x20, 0x87, 0x2b, 0x85, 0x13,
  18092. 0xf6, 0xea, 0xb1, 0xa8, 0xa4, 0x44, 0x38, 0xd5,
  18093. 0xef, 0x11}, 18,
  18094. /* ct, ct length */
  18095. {0x0d, 0xe1, 0x8f, 0xd0, 0xfd, 0xd9, 0x1e, 0x7a,
  18096. 0xf1, 0x9f, 0x1d, 0x8e, 0xe8, 0x73, 0x39, 0x38,
  18097. 0xb1, 0xe8}, 18,
  18098. /* tag, tag length */
  18099. {0xe7, 0xf6, 0xd2, 0x23, 0x16, 0x18, 0x10, 0x2f,
  18100. 0xdb, 0x7f, 0xe5, 0x5f, 0xf1, 0x99, 0x17, 0x00}, 16,
  18101. /* valid */
  18102. 1,
  18103. },
  18104. {
  18105. /* key, key length */
  18106. {0x83, 0x95, 0xfc, 0xf1, 0xe9, 0x5b, 0xeb, 0xd6,
  18107. 0x97, 0xbd, 0x01, 0x0b, 0xc7, 0x66, 0xaa, 0xc3}, 16,
  18108. /* iv, iv length */
  18109. {0x22, 0xe7, 0xad, 0xd9, 0x3c, 0xfc, 0x63, 0x93,
  18110. 0xc5, 0x7e, 0xc0, 0xb3, 0xc1, 0x7d, 0x6b, 0x44}, 16,
  18111. /* aad, aad length */
  18112. {0x12, 0x67, 0x35, 0xfc, 0xc3, 0x20, 0xd2, 0x5a}, 8,
  18113. /* msg, msg length */
  18114. {0xca, 0x40, 0xd7, 0x44, 0x6e, 0x54, 0x5f, 0xfa,
  18115. 0xed, 0x3b, 0xd1, 0x2a, 0x74, 0x0a, 0x65, 0x9f,
  18116. 0xfb, 0xbb, 0x3c, 0xea, 0xb7}, 21,
  18117. /* ct, ct length */
  18118. {0xcb, 0x89, 0x20, 0xf8, 0x7a, 0x6c, 0x75, 0xcf,
  18119. 0xf3, 0x96, 0x27, 0xb5, 0x6e, 0x3e, 0xd1, 0x97,
  18120. 0xc5, 0x52, 0xd2, 0x95, 0xa7}, 21,
  18121. /* tag, tag length */
  18122. {0xcf, 0xc4, 0x6a, 0xfc, 0x25, 0x3b, 0x46, 0x52,
  18123. 0xb1, 0xaf, 0x37, 0x95, 0xb1, 0x24, 0xab, 0x6e}, 16,
  18124. /* valid */
  18125. 1,
  18126. },
  18127. {
  18128. /* key, key length */
  18129. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18130. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18131. /* iv, iv length */
  18132. {0x3c, 0x8c, 0xc2, 0x97, 0x0a, 0x00, 0x8f, 0x75,
  18133. 0xcc, 0x5b, 0xea, 0xe2, 0x84, 0x72, 0x58, 0xc2}, 16,
  18134. /* aad, aad length */
  18135. {0x00}, 0,
  18136. /* msg, msg length */
  18137. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18138. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18139. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18140. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18141. /* ct, ct length */
  18142. {0x3c, 0x44, 0x1f, 0x32, 0xce, 0x07, 0x82, 0x23,
  18143. 0x64, 0xd7, 0xa2, 0x99, 0x0e, 0x50, 0xbb, 0x13,
  18144. 0xd7, 0xb0, 0x2a, 0x26, 0x96, 0x9e, 0x4a, 0x93,
  18145. 0x7e, 0x5e, 0x90, 0x73, 0xb0, 0xd9, 0xc9, 0x68}, 32,
  18146. /* tag, tag length */
  18147. {0xdb, 0x90, 0xbd, 0xb3, 0xda, 0x3d, 0x00, 0xaf,
  18148. 0xd0, 0xfc, 0x6a, 0x83, 0x55, 0x1d, 0xa9, 0x5e}, 16,
  18149. /* valid */
  18150. 1,
  18151. },
  18152. {
  18153. /* key, key length */
  18154. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18155. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18156. /* iv, iv length */
  18157. {0xae, 0xf0, 0x3d, 0x00, 0x59, 0x84, 0x94, 0xe9,
  18158. 0xfb, 0x03, 0xcd, 0x7d, 0x8b, 0x59, 0x08, 0x66}, 16,
  18159. /* aad, aad length */
  18160. {0x00}, 0,
  18161. /* msg, msg length */
  18162. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18163. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18164. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18165. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18166. /* ct, ct length */
  18167. {0xd1, 0x9a, 0xc5, 0x98, 0x49, 0x02, 0x6a, 0x91,
  18168. 0xaa, 0x1b, 0x9a, 0xec, 0x29, 0xb1, 0x1a, 0x20,
  18169. 0x2a, 0x4d, 0x73, 0x9f, 0xd8, 0x6c, 0x28, 0xe3,
  18170. 0xae, 0x3d, 0x58, 0x8e, 0xa2, 0x1d, 0x70, 0xc6}, 32,
  18171. /* tag, tag length */
  18172. {0xc3, 0x0f, 0x6c, 0xd9, 0x20, 0x20, 0x74, 0xed,
  18173. 0x6e, 0x2a, 0x2a, 0x36, 0x0e, 0xac, 0x8c, 0x47}, 16,
  18174. /* valid */
  18175. 1,
  18176. },
  18177. {
  18178. /* key, key length */
  18179. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18180. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18181. /* iv, iv length */
  18182. {0x55, 0xd1, 0x25, 0x11, 0xc6, 0x96, 0xa8, 0x0d,
  18183. 0x05, 0x14, 0xd1, 0xff, 0xba, 0x49, 0xca, 0xda}, 16,
  18184. /* aad, aad length */
  18185. {0x00}, 0,
  18186. /* msg, msg length */
  18187. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18188. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18189. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18190. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18191. /* ct, ct length */
  18192. {0x21, 0x08, 0x55, 0x8a, 0xc4, 0xb2, 0xc2, 0xd5,
  18193. 0xcc, 0x66, 0xce, 0xa5, 0x1d, 0x62, 0x10, 0xe0,
  18194. 0x46, 0x17, 0x7a, 0x67, 0x63, 0x1c, 0xd2, 0xdd,
  18195. 0x8f, 0x09, 0x46, 0x97, 0x33, 0xac, 0xb5, 0x17}, 32,
  18196. /* tag, tag length */
  18197. {0xfc, 0x35, 0x5e, 0x87, 0xa2, 0x67, 0xbe, 0x3a,
  18198. 0xe3, 0xe4, 0x4c, 0x0b, 0xf3, 0xf9, 0x9b, 0x2b}, 16,
  18199. /* valid */
  18200. 1,
  18201. },
  18202. {
  18203. /* key, key length */
  18204. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18205. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18206. /* iv, iv length */
  18207. {0x79, 0x42, 0x2d, 0xdd, 0x91, 0xc4, 0xee, 0xe2,
  18208. 0xde, 0xae, 0xf1, 0xf9, 0x68, 0x30, 0x53, 0x04}, 16,
  18209. /* aad, aad length */
  18210. {0x00}, 0,
  18211. /* msg, msg length */
  18212. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18213. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18214. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18215. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18216. /* ct, ct length */
  18217. {0x4d, 0x2c, 0x15, 0x24, 0xca, 0x4b, 0xaa, 0x4e,
  18218. 0xef, 0xcc, 0xe6, 0xb9, 0x1b, 0x22, 0x7e, 0xe8,
  18219. 0x3a, 0xba, 0xff, 0x81, 0x05, 0xdc, 0xaf, 0xa2,
  18220. 0xab, 0x19, 0x1f, 0x5d, 0xf2, 0x57, 0x50, 0x35}, 32,
  18221. /* tag, tag length */
  18222. {0xe2, 0xc8, 0x65, 0xce, 0x2d, 0x7a, 0xbd, 0xac,
  18223. 0x02, 0x4c, 0x6f, 0x99, 0x1a, 0x84, 0x83, 0x90}, 16,
  18224. /* valid */
  18225. 1,
  18226. },
  18227. {
  18228. /* key, key length */
  18229. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18230. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18231. /* iv, iv length */
  18232. {0x0a, 0xf5, 0xaa, 0x7a, 0x76, 0x76, 0xe2, 0x83,
  18233. 0x06, 0x30, 0x6b, 0xcd, 0x9b, 0xf2, 0x00, 0x3a}, 16,
  18234. /* aad, aad length */
  18235. {0x00}, 0,
  18236. /* msg, msg length */
  18237. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18238. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18239. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18240. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18241. /* ct, ct length */
  18242. {0x8e, 0xb0, 0x1e, 0x62, 0x18, 0x5d, 0x78, 0x2e,
  18243. 0xb9, 0x28, 0x7a, 0x34, 0x1a, 0x68, 0x62, 0xac,
  18244. 0x52, 0x57, 0xd6, 0xf9, 0xad, 0xc9, 0x9e, 0xe0,
  18245. 0xa2, 0x4d, 0x9c, 0x22, 0xb3, 0xe9, 0xb3, 0x8a}, 32,
  18246. /* tag, tag length */
  18247. {0x39, 0xc3, 0x39, 0xbc, 0x8a, 0x74, 0xc7, 0x5e,
  18248. 0x2c, 0x65, 0xc6, 0x11, 0x95, 0x44, 0xd6, 0x1e}, 16,
  18249. /* valid */
  18250. 1,
  18251. },
  18252. {
  18253. /* key, key length */
  18254. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18255. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18256. /* iv, iv length */
  18257. {0xaf, 0x5a, 0x03, 0xae, 0x7e, 0xdd, 0x73, 0x47,
  18258. 0x1b, 0xdc, 0xdf, 0xac, 0x5e, 0x19, 0x4a, 0x60}, 16,
  18259. /* aad, aad length */
  18260. {0x00}, 0,
  18261. /* msg, msg length */
  18262. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18263. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18264. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18265. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18266. /* ct, ct length */
  18267. {0x94, 0xc5, 0xd2, 0xac, 0xa6, 0xdb, 0xbc, 0xe8,
  18268. 0xc2, 0x45, 0x13, 0xa2, 0x5e, 0x09, 0x5c, 0x0e,
  18269. 0x54, 0xa9, 0x42, 0x86, 0x0d, 0x32, 0x7a, 0x22,
  18270. 0x2a, 0x81, 0x5c, 0xc7, 0x13, 0xb1, 0x63, 0xb4}, 32,
  18271. /* tag, tag length */
  18272. {0xf5, 0x0b, 0x30, 0x30, 0x4e, 0x45, 0xc9, 0xd4,
  18273. 0x11, 0xe8, 0xdf, 0x45, 0x08, 0xa9, 0x86, 0x12}, 16,
  18274. /* valid */
  18275. 1,
  18276. },
  18277. {
  18278. /* key, key length */
  18279. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18280. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18281. /* iv, iv length */
  18282. {0xb3, 0x70, 0x87, 0x68, 0x0f, 0x0e, 0xdd, 0x5a,
  18283. 0x52, 0x22, 0x8b, 0x8c, 0x7a, 0xae, 0xa6, 0x64}, 16,
  18284. /* aad, aad length */
  18285. {0x00}, 0,
  18286. /* msg, msg length */
  18287. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18288. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18289. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18290. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18291. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18292. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18293. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18294. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33}, 64,
  18295. /* ct, ct length */
  18296. {0x3b, 0xb6, 0x17, 0x3e, 0x37, 0x72, 0xd4, 0xb6,
  18297. 0x2e, 0xef, 0x37, 0xf9, 0xef, 0x07, 0x81, 0xf3,
  18298. 0x60, 0xb6, 0xc7, 0x4b, 0xe3, 0xbf, 0x6b, 0x37,
  18299. 0x10, 0x67, 0xbc, 0x1b, 0x09, 0x0d, 0x9d, 0x66,
  18300. 0x22, 0xa1, 0xfb, 0xec, 0x6a, 0xc4, 0x71, 0xb3,
  18301. 0x34, 0x9c, 0xd4, 0x27, 0x7a, 0x10, 0x1d, 0x40,
  18302. 0x89, 0x0f, 0xbf, 0x27, 0xdf, 0xdc, 0xd0, 0xb4,
  18303. 0xe3, 0x78, 0x1f, 0x98, 0x06, 0xda, 0xab, 0xb6}, 64,
  18304. /* tag, tag length */
  18305. {0xa0, 0x49, 0x87, 0x45, 0xe5, 0x99, 0x99, 0xdd,
  18306. 0xc3, 0x2d, 0x5b, 0x14, 0x02, 0x41, 0x12, 0x4e}, 16,
  18307. /* valid */
  18308. 1,
  18309. },
  18310. {
  18311. /* key, key length */
  18312. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18313. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18314. /* iv, iv length */
  18315. {0x4f, 0x80, 0x2d, 0xa6, 0x2a, 0x38, 0x45, 0x55,
  18316. 0xa1, 0x9b, 0xc2, 0xb3, 0x82, 0xeb, 0x25, 0xaf}, 16,
  18317. /* aad, aad length */
  18318. {0x00}, 0,
  18319. /* msg, msg length */
  18320. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18321. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18322. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18323. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18324. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18325. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18326. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18327. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18328. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  18329. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44}, 80,
  18330. /* ct, ct length */
  18331. {0xe9, 0xb0, 0xbb, 0x88, 0x57, 0x81, 0x8c, 0xe3,
  18332. 0x20, 0x1c, 0x36, 0x90, 0xd2, 0x1d, 0xaa, 0x7f,
  18333. 0x26, 0x4f, 0xb8, 0xee, 0x93, 0xcc, 0x7a, 0x46,
  18334. 0x74, 0xea, 0x2f, 0xc3, 0x2b, 0xf1, 0x82, 0xfb,
  18335. 0x2a, 0x7e, 0x8a, 0xd5, 0x15, 0x07, 0xad, 0x4f,
  18336. 0x31, 0xce, 0xfc, 0x23, 0x56, 0xfe, 0x79, 0x36,
  18337. 0xa7, 0xf6, 0xe1, 0x9f, 0x95, 0xe8, 0x8f, 0xdb,
  18338. 0xf1, 0x76, 0x20, 0x91, 0x6d, 0x3a, 0x6f, 0x3d,
  18339. 0x01, 0xfc, 0x17, 0xd3, 0x58, 0x67, 0x2f, 0x77,
  18340. 0x7f, 0xd4, 0x09, 0x92, 0x46, 0xe4, 0x36, 0xe1}, 80,
  18341. /* tag, tag length */
  18342. {0x67, 0x91, 0x0b, 0xe7, 0x44, 0xb8, 0x31, 0x5a,
  18343. 0xe0, 0xeb, 0x61, 0x24, 0x59, 0x0c, 0x5d, 0x8b}, 16,
  18344. /* valid */
  18345. 1,
  18346. },
  18347. {
  18348. /* key, key length */
  18349. {0xb6, 0x7b, 0x1a, 0x6e, 0xfd, 0xd4, 0x0d, 0x37,
  18350. 0x08, 0x0f, 0xbe, 0x8f, 0x80, 0x47, 0xae, 0xb9}, 16,
  18351. /* iv, iv length */
  18352. {0xfa, 0x29, 0x4b, 0x12, 0x99, 0x72, 0xf7, 0xfc,
  18353. 0x5b, 0xbd, 0x5b, 0x96, 0xbb, 0xa8, 0x37, 0xc9}, 16,
  18354. /* aad, aad length */
  18355. {0x00}, 0,
  18356. /* msg, msg length */
  18357. {0x00}, 0,
  18358. /* ct, ct length */
  18359. {0x00}, 0,
  18360. /* tag, tag length */
  18361. {0xb1, 0x4b, 0x64, 0xfb, 0x58, 0x98, 0x99, 0x69,
  18362. 0x95, 0x70, 0xcc, 0x91, 0x60, 0xe3, 0x98, 0x96}, 16,
  18363. /* valid */
  18364. 1,
  18365. },
  18366. {
  18367. /* key, key length */
  18368. {0x20, 0x9e, 0x6d, 0xbf, 0x2a, 0xd2, 0x6a, 0x10,
  18369. 0x54, 0x45, 0xfc, 0x02, 0x07, 0xcd, 0x9e, 0x9a}, 16,
  18370. /* iv, iv length */
  18371. {0x94, 0x77, 0x84, 0x9d, 0x6c, 0xcd, 0xfc, 0xa1,
  18372. 0x12, 0xd9, 0x2e, 0x53, 0xfa, 0xe4, 0xa7, 0xca}, 16,
  18373. /* aad, aad length */
  18374. {0x00}, 0,
  18375. /* msg, msg length */
  18376. {0x01}, 1,
  18377. /* ct, ct length */
  18378. {0x1d}, 1,
  18379. /* tag, tag length */
  18380. {0x52, 0xa5, 0xf6, 0x00, 0xfe, 0x53, 0x38, 0x02,
  18381. 0x6a, 0x7c, 0xb0, 0x9c, 0x11, 0x64, 0x00, 0x82}, 16,
  18382. /* valid */
  18383. 1,
  18384. },
  18385. {
  18386. /* key, key length */
  18387. {0xa5, 0x49, 0x44, 0x2e, 0x35, 0x15, 0x40, 0x32,
  18388. 0xd0, 0x7c, 0x86, 0x66, 0x00, 0x6a, 0xa6, 0xa2}, 16,
  18389. /* iv, iv length */
  18390. {0x51, 0x71, 0x52, 0x45, 0x68, 0xe8, 0x1d, 0x97,
  18391. 0xe8, 0xc4, 0xde, 0x4b, 0xa5, 0x6c, 0x10, 0xa0}, 16,
  18392. /* aad, aad length */
  18393. {0x00}, 0,
  18394. /* msg, msg length */
  18395. {0x11, 0x82, 0xe9, 0x35, 0x96, 0xca, 0xc5, 0x60,
  18396. 0x89, 0x46, 0x40, 0x0b, 0xc7, 0x3f, 0x3a}, 15,
  18397. /* ct, ct length */
  18398. {0xd7, 0xb8, 0xa6, 0xb4, 0x3d, 0x2e, 0x9f, 0x98,
  18399. 0xc2, 0xb4, 0x4c, 0xe5, 0xe3, 0xcf, 0xdb}, 15,
  18400. /* tag, tag length */
  18401. {0x1b, 0xdd, 0x52, 0xfc, 0x98, 0x7d, 0xaf, 0x0e,
  18402. 0xe1, 0x92, 0x34, 0xc9, 0x05, 0xea, 0x64, 0x5f}, 16,
  18403. /* valid */
  18404. 1,
  18405. },
  18406. {
  18407. /* key, key length */
  18408. {0x95, 0x8b, 0xcd, 0xb6, 0x6a, 0x39, 0x52, 0xb5,
  18409. 0x37, 0x01, 0x58, 0x2a, 0x68, 0xa0, 0xe4, 0x74}, 16,
  18410. /* iv, iv length */
  18411. {0x0e, 0x6e, 0xc8, 0x79, 0xb0, 0x2c, 0x6f, 0x51,
  18412. 0x69, 0x76, 0xe3, 0x58, 0x98, 0x42, 0x8d, 0xa7}, 16,
  18413. /* aad, aad length */
  18414. {0x00}, 0,
  18415. /* msg, msg length */
  18416. {0x14, 0x04, 0x15, 0x82, 0x3e, 0xcc, 0x89, 0x32,
  18417. 0xa0, 0x58, 0x38, 0x4b, 0x73, 0x8e, 0xa6, 0xea,
  18418. 0x6d, 0x4d, 0xfe, 0x3b, 0xbe, 0xee}, 22,
  18419. /* ct, ct length */
  18420. {0x73, 0xe5, 0xc6, 0xf0, 0xe7, 0x03, 0xa5, 0x2d,
  18421. 0x02, 0xf7, 0xf7, 0xfa, 0xeb, 0x1b, 0x77, 0xfd,
  18422. 0x4f, 0xd0, 0xcb, 0x42, 0x1e, 0xaf}, 22,
  18423. /* tag, tag length */
  18424. {0x6c, 0x15, 0x4a, 0x85, 0x96, 0x8e, 0xdd, 0x74,
  18425. 0x77, 0x65, 0x75, 0xa4, 0x45, 0x0b, 0xd8, 0x97}, 16,
  18426. /* valid */
  18427. 1,
  18428. },
  18429. {
  18430. /* key, key length */
  18431. {0x96, 0x5b, 0x75, 0x7b, 0xa5, 0x01, 0x8a, 0x8d,
  18432. 0x66, 0xed, 0xc7, 0x8e, 0x0c, 0xee, 0xe8, 0x6b}, 16,
  18433. /* iv, iv length */
  18434. {0x2e, 0x35, 0x90, 0x1a, 0xe7, 0xd4, 0x91, 0xee,
  18435. 0xcc, 0x88, 0x38, 0xfe, 0xdd, 0x63, 0x14, 0x05}, 16,
  18436. /* aad, aad length */
  18437. {0xdf, 0x10, 0xd0, 0xd2, 0x12, 0x24, 0x24, 0x50}, 8,
  18438. /* msg, msg length */
  18439. {0x36, 0xe5, 0x7a, 0x76, 0x39, 0x58, 0xb0, 0x2c,
  18440. 0xea, 0x9d, 0x6a, 0x67, 0x6e, 0xbc, 0xe8, 0x1f}, 16,
  18441. /* ct, ct length */
  18442. {0x93, 0x6b, 0x69, 0xb6, 0xc9, 0x55, 0xad, 0xfd,
  18443. 0x15, 0x53, 0x9b, 0x9b, 0xe4, 0x98, 0x9c, 0xb6}, 16,
  18444. /* tag, tag length */
  18445. {0xee, 0x15, 0xa1, 0x45, 0x4e, 0x88, 0xfa, 0xad,
  18446. 0x8e, 0x48, 0xa8, 0xdf, 0x29, 0x83, 0xb4, 0x25}, 16,
  18447. /* valid */
  18448. 1,
  18449. },
  18450. {
  18451. /* key, key length */
  18452. {0x88, 0xd0, 0x20, 0x33, 0x78, 0x1c, 0x7b, 0x41,
  18453. 0x64, 0x71, 0x1a, 0x05, 0x42, 0x0f, 0x25, 0x6e}, 16,
  18454. /* iv, iv length */
  18455. {0x7f, 0x29, 0x85, 0x29, 0x63, 0x15, 0x50, 0x7a,
  18456. 0xa4, 0xc0, 0xa9, 0x3d, 0x5c, 0x12, 0xbd, 0x77}, 16,
  18457. /* aad, aad length */
  18458. {0x7c, 0x57, 0x1d, 0x2f, 0xbb, 0x5f, 0x62, 0x52,
  18459. 0x3c, 0x0e, 0xb3, 0x38, 0xbe, 0xf9, 0xa9}, 15,
  18460. /* msg, msg length */
  18461. {0xd9, 0x8a, 0xdc, 0x03, 0xd9, 0xd5, 0x82, 0x73,
  18462. 0x2e, 0xb0, 0x7d, 0xf2, 0x3d, 0x7b, 0x9f, 0x74}, 16,
  18463. /* ct, ct length */
  18464. {0x67, 0xca, 0xac, 0x35, 0x44, 0x3a, 0x31, 0x38,
  18465. 0xd2, 0xcb, 0x81, 0x1f, 0x0c, 0xe0, 0x4d, 0xd2}, 16,
  18466. /* tag, tag length */
  18467. {0xb7, 0x96, 0x8e, 0x0b, 0x56, 0x40, 0xe3, 0xb2,
  18468. 0x36, 0x56, 0x96, 0x53, 0x20, 0x8b, 0x9d, 0xeb}, 16,
  18469. /* valid */
  18470. 1,
  18471. },
  18472. {
  18473. /* key, key length */
  18474. {0x51, 0x58, 0x40, 0xcf, 0x67, 0xd2, 0xe4, 0x0e,
  18475. 0xb6, 0x5e, 0x54, 0xa2, 0x4c, 0x72, 0xcb, 0xf2}, 16,
  18476. /* iv, iv length */
  18477. {0xbf, 0x47, 0xaf, 0xdf, 0xd4, 0x92, 0x13, 0x7a,
  18478. 0x24, 0x23, 0x6b, 0xc3, 0x67, 0x97, 0xa8, 0x8e}, 16,
  18479. /* aad, aad length */
  18480. {0x16, 0x84, 0x3c, 0x09, 0x1d, 0x43, 0xb0, 0xa1,
  18481. 0x91, 0xd0, 0xc7, 0x3d, 0x15, 0x60, 0x1b, 0xe9}, 16,
  18482. /* msg, msg length */
  18483. {0xc8, 0x34, 0x58, 0x8c, 0xb6, 0xda, 0xf9, 0xf0,
  18484. 0x6d, 0xd2, 0x35, 0x19, 0xf4, 0xbe, 0x9f, 0x56}, 16,
  18485. /* ct, ct length */
  18486. {0x20, 0x0a, 0xc4, 0x51, 0xfb, 0xeb, 0x0f, 0x61,
  18487. 0x51, 0xd6, 0x15, 0x83, 0xa4, 0x3b, 0x73, 0x43}, 16,
  18488. /* tag, tag length */
  18489. {0x2a, 0xd4, 0x3e, 0x4c, 0xaa, 0x51, 0x98, 0x3a,
  18490. 0x9d, 0x4d, 0x24, 0x48, 0x1b, 0xf4, 0xc8, 0x39}, 16,
  18491. /* valid */
  18492. 1,
  18493. },
  18494. {
  18495. /* key, key length */
  18496. {0x2e, 0x44, 0x92, 0xd4, 0x44, 0xe5, 0xb6, 0xf4,
  18497. 0xce, 0xc8, 0xc2, 0xd3, 0x61, 0x5a, 0xc8, 0x58}, 16,
  18498. /* iv, iv length */
  18499. {0xd0, 0x2b, 0xf0, 0x76, 0x3a, 0x9f, 0xef, 0xbf,
  18500. 0x70, 0xc3, 0x3a, 0xee, 0x1e, 0x9d, 0xa1, 0xd6}, 16,
  18501. /* aad, aad length */
  18502. {0x90, 0x4d, 0x86, 0xf1, 0x33, 0xce, 0xc1, 0x5a,
  18503. 0x0c, 0x3c, 0xaf, 0x14, 0xd7, 0xe0, 0x29, 0xc8,
  18504. 0x2a, 0x07, 0x70, 0x5a, 0x23, 0xf0, 0xd0, 0x80}, 24,
  18505. /* msg, msg length */
  18506. {0x9e, 0x62, 0xd6, 0x51, 0x1b, 0x0b, 0xda, 0x7d,
  18507. 0xd7, 0x74, 0x0b, 0x61, 0x4d, 0x97, 0xba, 0xe0}, 16,
  18508. /* ct, ct length */
  18509. {0x27, 0xc6, 0xe9, 0xa6, 0x53, 0xc5, 0x25, 0x3c,
  18510. 0xa1, 0xc5, 0x67, 0x3f, 0x97, 0xb9, 0xb3, 0x3e}, 16,
  18511. /* tag, tag length */
  18512. {0x2d, 0x58, 0x12, 0x71, 0xe1, 0xfa, 0x9e, 0x36,
  18513. 0x86, 0x13, 0x6c, 0xaa, 0x8f, 0x4d, 0x6c, 0x8e}, 16,
  18514. /* valid */
  18515. 1,
  18516. },
  18517. {
  18518. /* key, key length */
  18519. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18520. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18521. /* iv, iv length */
  18522. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18523. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18524. /* aad, aad length */
  18525. {0x00}, 0,
  18526. /* msg, msg length */
  18527. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18528. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18529. /* ct, ct length */
  18530. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18531. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18532. /* tag, tag length */
  18533. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18534. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18535. /* valid */
  18536. 0,
  18537. },
  18538. {
  18539. /* key, key length */
  18540. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18541. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18542. /* iv, iv length */
  18543. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18544. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18545. /* aad, aad length */
  18546. {0x00}, 0,
  18547. /* msg, msg length */
  18548. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18549. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18550. /* ct, ct length */
  18551. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18552. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18553. /* tag, tag length */
  18554. {0xe4, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18555. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18556. /* valid */
  18557. 0,
  18558. },
  18559. {
  18560. /* key, key length */
  18561. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18562. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18563. /* iv, iv length */
  18564. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18565. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18566. /* aad, aad length */
  18567. {0x00}, 0,
  18568. /* msg, msg length */
  18569. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18570. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18571. /* ct, ct length */
  18572. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18573. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18574. /* tag, tag length */
  18575. {0x66, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18576. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18577. /* valid */
  18578. 0,
  18579. },
  18580. {
  18581. /* key, key length */
  18582. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18583. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18584. /* iv, iv length */
  18585. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18586. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18587. /* aad, aad length */
  18588. {0x00}, 0,
  18589. /* msg, msg length */
  18590. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18591. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18592. /* ct, ct length */
  18593. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18594. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18595. /* tag, tag length */
  18596. {0xe6, 0x0f, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18597. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18598. /* valid */
  18599. 0,
  18600. },
  18601. {
  18602. /* key, key length */
  18603. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18604. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18605. /* iv, iv length */
  18606. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18607. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18608. /* aad, aad length */
  18609. {0x00}, 0,
  18610. /* msg, msg length */
  18611. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18612. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18613. /* ct, ct length */
  18614. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18615. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18616. /* tag, tag length */
  18617. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0xf2,
  18618. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18619. /* valid */
  18620. 0,
  18621. },
  18622. {
  18623. /* key, key length */
  18624. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18625. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18626. /* iv, iv length */
  18627. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18628. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18629. /* aad, aad length */
  18630. {0x00}, 0,
  18631. /* msg, msg length */
  18632. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18633. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18634. /* ct, ct length */
  18635. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18636. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18637. /* tag, tag length */
  18638. {0xe6, 0x0e, 0x7c, 0x50, 0x12, 0xa6, 0xdb, 0xf2,
  18639. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18640. /* valid */
  18641. 0,
  18642. },
  18643. {
  18644. /* key, key length */
  18645. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18646. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18647. /* iv, iv length */
  18648. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18649. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18650. /* aad, aad length */
  18651. {0x00}, 0,
  18652. /* msg, msg length */
  18653. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18654. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18655. /* ct, ct length */
  18656. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18657. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18658. /* tag, tag length */
  18659. {0xe6, 0x0e, 0x7c, 0x50, 0x11, 0xa6, 0xdb, 0xf2,
  18660. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18661. /* valid */
  18662. 0,
  18663. },
  18664. {
  18665. /* key, key length */
  18666. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18667. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18668. /* iv, iv length */
  18669. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18670. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18671. /* aad, aad length */
  18672. {0x00}, 0,
  18673. /* msg, msg length */
  18674. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18675. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18676. /* ct, ct length */
  18677. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18678. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18679. /* tag, tag length */
  18680. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  18681. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18682. /* valid */
  18683. 0,
  18684. },
  18685. {
  18686. /* key, key length */
  18687. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18688. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18689. /* iv, iv length */
  18690. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18691. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18692. /* aad, aad length */
  18693. {0x00}, 0,
  18694. /* msg, msg length */
  18695. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18696. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18697. /* ct, ct length */
  18698. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18699. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18700. /* tag, tag length */
  18701. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18702. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18703. /* valid */
  18704. 0,
  18705. },
  18706. {
  18707. /* key, key length */
  18708. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18709. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18710. /* iv, iv length */
  18711. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18712. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18713. /* aad, aad length */
  18714. {0x00}, 0,
  18715. /* msg, msg length */
  18716. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18717. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18718. /* ct, ct length */
  18719. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18720. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18721. /* tag, tag length */
  18722. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18723. 0xd2, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18724. /* valid */
  18725. 0,
  18726. },
  18727. {
  18728. /* key, key length */
  18729. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18730. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18731. /* iv, iv length */
  18732. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18733. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18734. /* aad, aad length */
  18735. {0x00}, 0,
  18736. /* msg, msg length */
  18737. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18738. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18739. /* ct, ct length */
  18740. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18741. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18742. /* tag, tag length */
  18743. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18744. 0x52, 0xb8, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18745. /* valid */
  18746. 0,
  18747. },
  18748. {
  18749. /* key, key length */
  18750. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18751. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18752. /* iv, iv length */
  18753. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18754. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18755. /* aad, aad length */
  18756. {0x00}, 0,
  18757. /* msg, msg length */
  18758. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18759. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18760. /* ct, ct length */
  18761. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18762. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18763. /* tag, tag length */
  18764. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18765. 0x52, 0x98, 0xb0, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18766. /* valid */
  18767. 0,
  18768. },
  18769. {
  18770. /* key, key length */
  18771. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18772. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18773. /* iv, iv length */
  18774. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18775. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18776. /* aad, aad length */
  18777. {0x00}, 0,
  18778. /* msg, msg length */
  18779. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18780. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18781. /* ct, ct length */
  18782. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18783. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18784. /* tag, tag length */
  18785. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18786. 0x52, 0x98, 0xb1, 0x92, 0x9a, 0xc3, 0x56, 0xa7}, 16,
  18787. /* valid */
  18788. 0,
  18789. },
  18790. {
  18791. /* key, key length */
  18792. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18793. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18794. /* iv, iv length */
  18795. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18796. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18797. /* aad, aad length */
  18798. {0x00}, 0,
  18799. /* msg, msg length */
  18800. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18801. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18802. /* ct, ct length */
  18803. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18804. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18805. /* tag, tag length */
  18806. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18807. 0x52, 0x98, 0xb1, 0x92, 0x99, 0xc3, 0x56, 0xa7}, 16,
  18808. /* valid */
  18809. 0,
  18810. },
  18811. {
  18812. /* key, key length */
  18813. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18814. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18815. /* iv, iv length */
  18816. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18817. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18818. /* aad, aad length */
  18819. {0x00}, 0,
  18820. /* msg, msg length */
  18821. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18822. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18823. /* ct, ct length */
  18824. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18825. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18826. /* tag, tag length */
  18827. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18828. 0x52, 0x98, 0xb1, 0x92, 0x1b, 0xc3, 0x56, 0xa7}, 16,
  18829. /* valid */
  18830. 0,
  18831. },
  18832. {
  18833. /* key, key length */
  18834. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18835. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18836. /* iv, iv length */
  18837. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18838. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18839. /* aad, aad length */
  18840. {0x00}, 0,
  18841. /* msg, msg length */
  18842. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18843. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18844. /* ct, ct length */
  18845. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18846. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18847. /* tag, tag length */
  18848. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18849. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa6}, 16,
  18850. /* valid */
  18851. 0,
  18852. },
  18853. {
  18854. /* key, key length */
  18855. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18856. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18857. /* iv, iv length */
  18858. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18859. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18860. /* aad, aad length */
  18861. {0x00}, 0,
  18862. /* msg, msg length */
  18863. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18864. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18865. /* ct, ct length */
  18866. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18867. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18868. /* tag, tag length */
  18869. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18870. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa5}, 16,
  18871. /* valid */
  18872. 0,
  18873. },
  18874. {
  18875. /* key, key length */
  18876. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18877. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18878. /* iv, iv length */
  18879. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18880. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18881. /* aad, aad length */
  18882. {0x00}, 0,
  18883. /* msg, msg length */
  18884. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18885. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18886. /* ct, ct length */
  18887. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18888. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18889. /* tag, tag length */
  18890. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18891. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xe7}, 16,
  18892. /* valid */
  18893. 0,
  18894. },
  18895. {
  18896. /* key, key length */
  18897. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18898. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18899. /* iv, iv length */
  18900. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18901. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18902. /* aad, aad length */
  18903. {0x00}, 0,
  18904. /* msg, msg length */
  18905. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18906. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18907. /* ct, ct length */
  18908. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18909. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18910. /* tag, tag length */
  18911. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18912. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  18913. /* valid */
  18914. 0,
  18915. },
  18916. {
  18917. /* key, key length */
  18918. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18919. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18920. /* iv, iv length */
  18921. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18922. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18923. /* aad, aad length */
  18924. {0x00}, 0,
  18925. /* msg, msg length */
  18926. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18927. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18928. /* ct, ct length */
  18929. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18930. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18931. /* tag, tag length */
  18932. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18933. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18934. /* valid */
  18935. 0,
  18936. },
  18937. {
  18938. /* key, key length */
  18939. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18940. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18941. /* iv, iv length */
  18942. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18943. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18944. /* aad, aad length */
  18945. {0x00}, 0,
  18946. /* msg, msg length */
  18947. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18948. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18949. /* ct, ct length */
  18950. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18951. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18952. /* tag, tag length */
  18953. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0x72,
  18954. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18955. /* valid */
  18956. 0,
  18957. },
  18958. {
  18959. /* key, key length */
  18960. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18961. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18962. /* iv, iv length */
  18963. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18964. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18965. /* aad, aad length */
  18966. {0x00}, 0,
  18967. /* msg, msg length */
  18968. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18969. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18970. /* ct, ct length */
  18971. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18972. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18973. /* tag, tag length */
  18974. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  18975. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  18976. /* valid */
  18977. 0,
  18978. },
  18979. {
  18980. /* key, key length */
  18981. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18982. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18983. /* iv, iv length */
  18984. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18985. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18986. /* aad, aad length */
  18987. {0x00}, 0,
  18988. /* msg, msg length */
  18989. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18990. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18991. /* ct, ct length */
  18992. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18993. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18994. /* tag, tag length */
  18995. {0x19, 0xf1, 0x83, 0xaf, 0xec, 0x59, 0x24, 0x0d,
  18996. 0xad, 0x67, 0x4e, 0x6d, 0x64, 0x3c, 0xa9, 0x58}, 16,
  18997. /* valid */
  18998. 0,
  18999. },
  19000. {
  19001. /* key, key length */
  19002. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19003. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19004. /* iv, iv length */
  19005. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19006. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19007. /* aad, aad length */
  19008. {0x00}, 0,
  19009. /* msg, msg length */
  19010. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19011. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19012. /* ct, ct length */
  19013. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19014. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19015. /* tag, tag length */
  19016. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  19017. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 16,
  19018. /* valid */
  19019. 0,
  19020. },
  19021. {
  19022. /* key, key length */
  19023. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19024. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19025. /* iv, iv length */
  19026. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19027. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19028. /* aad, aad length */
  19029. {0x00}, 0,
  19030. /* msg, msg length */
  19031. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19032. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19033. /* ct, ct length */
  19034. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19035. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19036. /* tag, tag length */
  19037. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  19038. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 16,
  19039. /* valid */
  19040. 0,
  19041. },
  19042. {
  19043. /* key, key length */
  19044. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19045. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19046. /* iv, iv length */
  19047. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19048. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19049. /* aad, aad length */
  19050. {0x00}, 0,
  19051. /* msg, msg length */
  19052. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19053. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19054. /* ct, ct length */
  19055. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19056. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19057. /* tag, tag length */
  19058. {0x66, 0x8e, 0xfc, 0xd0, 0x93, 0x26, 0x5b, 0x72,
  19059. 0xd2, 0x18, 0x31, 0x12, 0x1b, 0x43, 0xd6, 0x27}, 16,
  19060. /* valid */
  19061. 0,
  19062. },
  19063. {
  19064. /* key, key length */
  19065. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19066. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19067. /* iv, iv length */
  19068. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19069. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19070. /* aad, aad length */
  19071. {0x00}, 0,
  19072. /* msg, msg length */
  19073. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19074. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19075. /* ct, ct length */
  19076. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19077. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19078. /* tag, tag length */
  19079. {0xe7, 0x0f, 0x7d, 0x51, 0x12, 0xa7, 0xda, 0xf3,
  19080. 0x53, 0x99, 0xb0, 0x93, 0x9a, 0xc2, 0x57, 0xa6}, 16,
  19081. /* valid */
  19082. 0,
  19083. },
  19084. };
  19085. byte ciphertext[sizeof(vectors[0].ct)];
  19086. byte authtag[sizeof(vectors[0].tag)];
  19087. int i;
  19088. int len;
  19089. int ret;
  19090. for (i = 0; i < (int)(sizeof(vectors)/sizeof(vectors[0])); i++) {
  19091. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  19092. len = sizeof(authtag);
  19093. ExpectIntEQ(wc_AesEaxEncryptAuth(vectors[i].key, vectors[i].key_length,
  19094. ciphertext,
  19095. vectors[i].msg, vectors[i].msg_length,
  19096. vectors[i].iv, vectors[i].iv_length,
  19097. authtag, len,
  19098. vectors[i].aad, vectors[i].aad_length),
  19099. 0);
  19100. /* check ciphertext matches vector */
  19101. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].ct, vectors[i].ct_length),
  19102. 0);
  19103. /* check that computed tag matches vector only for vectors marked as valid */
  19104. ret = XMEMCMP(authtag, vectors[i].tag, len);
  19105. if (vectors[i].valid) {
  19106. ExpectIntEQ(ret, 0);
  19107. }
  19108. else {
  19109. ExpectIntNE(ret, 0);
  19110. }
  19111. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  19112. /* Decrypt, checking that the computed auth tags match */
  19113. ExpectIntEQ(wc_AesEaxDecryptAuth(vectors[i].key, vectors[i].key_length,
  19114. ciphertext,
  19115. vectors[i].ct, vectors[i].ct_length,
  19116. vectors[i].iv, vectors[i].iv_length,
  19117. authtag, len,
  19118. vectors[i].aad, vectors[i].aad_length),
  19119. 0);
  19120. /* check decrypted ciphertext matches vector plaintext */
  19121. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].msg, vectors[i].msg_length),
  19122. 0);
  19123. }
  19124. return EXPECT_RESULT();
  19125. } /* END test_wc_AesEaxVectors */
  19126. /*
  19127. * Testing test_wc_AesEaxEncryptAuth()
  19128. */
  19129. static int test_wc_AesEaxEncryptAuth(void)
  19130. {
  19131. EXPECT_DECLS;
  19132. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19133. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19134. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19135. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19136. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19137. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19138. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19139. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19140. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  19141. const byte msg[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  19142. byte ciphertext[sizeof(msg)];
  19143. byte authtag[AES_BLOCK_SIZE];
  19144. int i;
  19145. int len;
  19146. len = sizeof(authtag);
  19147. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19148. ciphertext,
  19149. msg, sizeof(msg),
  19150. iv, sizeof(iv),
  19151. authtag, len,
  19152. aad, sizeof(aad)),
  19153. 0);
  19154. /* Test null checking */
  19155. ExpectIntEQ(wc_AesEaxEncryptAuth(NULL, sizeof(key),
  19156. ciphertext,
  19157. msg, sizeof(msg),
  19158. iv, sizeof(iv),
  19159. authtag, len,
  19160. aad, sizeof(aad)),
  19161. BAD_FUNC_ARG);
  19162. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19163. NULL,
  19164. msg, sizeof(msg),
  19165. iv, sizeof(iv),
  19166. authtag, len,
  19167. aad, sizeof(aad)),
  19168. BAD_FUNC_ARG);
  19169. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19170. ciphertext,
  19171. NULL, sizeof(msg),
  19172. iv, sizeof(iv),
  19173. authtag, len,
  19174. aad, sizeof(aad)),
  19175. BAD_FUNC_ARG);
  19176. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19177. ciphertext,
  19178. msg, sizeof(msg),
  19179. NULL, sizeof(iv),
  19180. authtag, len,
  19181. aad, sizeof(aad)),
  19182. BAD_FUNC_ARG);
  19183. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19184. ciphertext,
  19185. msg, sizeof(msg),
  19186. iv, sizeof(iv),
  19187. NULL, len,
  19188. aad, sizeof(aad)),
  19189. BAD_FUNC_ARG);
  19190. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19191. ciphertext,
  19192. msg, sizeof(msg),
  19193. iv, sizeof(iv),
  19194. authtag, len,
  19195. NULL, sizeof(aad)),
  19196. BAD_FUNC_ARG);
  19197. /* Test bad key lengths */
  19198. for (i = 0; i <= 32; i++) {
  19199. int exp_ret;
  19200. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  19201. || i == AES_256_KEY_SIZE) {
  19202. exp_ret = 0;
  19203. }
  19204. else {
  19205. exp_ret = BAD_FUNC_ARG;
  19206. }
  19207. ExpectIntEQ(wc_AesEaxEncryptAuth(key, i,
  19208. ciphertext,
  19209. msg, sizeof(msg),
  19210. iv, sizeof(iv),
  19211. authtag, len,
  19212. aad, sizeof(aad)),
  19213. exp_ret);
  19214. }
  19215. /* Test auth tag size out of range */
  19216. len = AES_BLOCK_SIZE + 1;
  19217. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19218. ciphertext,
  19219. msg, sizeof(msg),
  19220. iv, sizeof(iv),
  19221. authtag, len,
  19222. aad, sizeof(aad)),
  19223. BAD_FUNC_ARG);
  19224. return EXPECT_RESULT();
  19225. } /* END test_wc_AesEaxEncryptAuth() */
  19226. /*
  19227. * Testing test_wc_AesEaxDecryptAuth()
  19228. */
  19229. static int test_wc_AesEaxDecryptAuth(void)
  19230. {
  19231. EXPECT_DECLS;
  19232. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19233. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19234. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19235. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19236. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19237. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19238. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19239. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19240. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  19241. const byte ct[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  19242. /* Garbage tag that should always fail for above aad */
  19243. const byte tag[] = {0xFE, 0xED, 0xBE, 0xEF, 0xDE, 0xAD, 0xC0, 0xDE,
  19244. 0xCA, 0xFE, 0xBE, 0xEF, 0xDE, 0xAF, 0xBE, 0xEF};
  19245. byte plaintext[sizeof(ct)];
  19246. int i;
  19247. int len;
  19248. len = sizeof(tag);
  19249. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19250. plaintext,
  19251. ct, sizeof(ct),
  19252. iv, sizeof(iv),
  19253. tag, len,
  19254. aad, sizeof(aad)),
  19255. AES_EAX_AUTH_E);
  19256. /* Test null checking */
  19257. ExpectIntEQ(wc_AesEaxDecryptAuth(NULL, sizeof(key),
  19258. plaintext,
  19259. ct, sizeof(ct),
  19260. iv, sizeof(iv),
  19261. tag, len,
  19262. aad, sizeof(aad)),
  19263. BAD_FUNC_ARG);
  19264. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19265. NULL,
  19266. ct, sizeof(ct),
  19267. iv, sizeof(iv),
  19268. tag, len,
  19269. aad, sizeof(aad)),
  19270. BAD_FUNC_ARG);
  19271. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19272. plaintext,
  19273. NULL, sizeof(ct),
  19274. iv, sizeof(iv),
  19275. tag, len,
  19276. aad, sizeof(aad)),
  19277. BAD_FUNC_ARG);
  19278. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19279. plaintext,
  19280. ct, sizeof(ct),
  19281. NULL, sizeof(iv),
  19282. tag, len,
  19283. aad, sizeof(aad)),
  19284. BAD_FUNC_ARG);
  19285. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19286. plaintext,
  19287. ct, sizeof(ct),
  19288. iv, sizeof(iv),
  19289. NULL, len,
  19290. aad, sizeof(aad)),
  19291. BAD_FUNC_ARG);
  19292. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19293. plaintext,
  19294. ct, sizeof(ct),
  19295. iv, sizeof(iv),
  19296. tag, len,
  19297. NULL, sizeof(aad)),
  19298. BAD_FUNC_ARG);
  19299. /* Test bad key lengths */
  19300. for (i = 0; i <= 32; i++) {
  19301. int exp_ret;
  19302. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  19303. || i == AES_256_KEY_SIZE) {
  19304. exp_ret = AES_EAX_AUTH_E;
  19305. }
  19306. else {
  19307. exp_ret = BAD_FUNC_ARG;
  19308. }
  19309. ExpectIntEQ(wc_AesEaxDecryptAuth(key, i,
  19310. plaintext,
  19311. ct, sizeof(ct),
  19312. iv, sizeof(iv),
  19313. tag, len,
  19314. aad, sizeof(aad)),
  19315. exp_ret);
  19316. }
  19317. /* Test auth tag size out of range */
  19318. len = AES_BLOCK_SIZE + 1;
  19319. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19320. plaintext,
  19321. ct, sizeof(ct),
  19322. iv, sizeof(iv),
  19323. tag, len,
  19324. aad, sizeof(aad)),
  19325. BAD_FUNC_ARG);
  19326. return EXPECT_RESULT();
  19327. } /* END test_wc_AesEaxDecryptAuth() */
  19328. #endif /* WOLFSSL_AES_EAX &&
  19329. * (!HAVE_FIPS || FIPS_VERSION_GE(5, 3)) && !HAVE_SELFTEST
  19330. */
  19331. /*
  19332. * Testing wc_InitDsaKey()
  19333. */
  19334. static int test_wc_InitDsaKey(void)
  19335. {
  19336. EXPECT_DECLS;
  19337. #ifndef NO_DSA
  19338. DsaKey key;
  19339. XMEMSET(&key, 0, sizeof(DsaKey));
  19340. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19341. /* Pass in bad args. */
  19342. ExpectIntEQ(wc_InitDsaKey(NULL), BAD_FUNC_ARG);
  19343. wc_FreeDsaKey(&key);
  19344. #endif
  19345. return EXPECT_RESULT();
  19346. } /* END test_wc_InitDsaKey */
  19347. /*
  19348. * Testing wc_DsaSign() and wc_DsaVerify()
  19349. */
  19350. static int test_wc_DsaSignVerify(void)
  19351. {
  19352. EXPECT_DECLS;
  19353. #if !defined(NO_DSA)
  19354. DsaKey key;
  19355. WC_RNG rng;
  19356. wc_Sha sha;
  19357. byte signature[DSA_SIG_SIZE];
  19358. byte hash[WC_SHA_DIGEST_SIZE];
  19359. word32 idx = 0;
  19360. word32 bytes;
  19361. int answer;
  19362. #ifdef USE_CERT_BUFFERS_1024
  19363. byte tmp[ONEK_BUF];
  19364. XMEMSET(tmp, 0, sizeof(tmp));
  19365. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19366. bytes = sizeof_dsa_key_der_1024;
  19367. #elif defined(USE_CERT_BUFFERS_2048)
  19368. byte tmp[TWOK_BUF];
  19369. XMEMSET(tmp, 0, sizeof(tmp));
  19370. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19371. bytes = sizeof_dsa_key_der_2048;
  19372. #else
  19373. byte tmp[TWOK_BUF];
  19374. XFILE fp = XBADFILE;
  19375. XMEMSET(tmp, 0, sizeof(tmp));
  19376. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19377. ExpectTrue((bytes = (word32)XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19378. if (fp != XBADFILE)
  19379. XFCLOSE(fp);
  19380. #endif /* END USE_CERT_BUFFERS_1024 */
  19381. ExpectIntEQ(wc_InitSha(&sha), 0);
  19382. ExpectIntEQ(wc_ShaUpdate(&sha, tmp, bytes), 0);
  19383. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  19384. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19385. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19386. ExpectIntEQ(wc_InitRng(&rng), 0);
  19387. /* Sign. */
  19388. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), 0);
  19389. /* Test bad args. */
  19390. ExpectIntEQ(wc_DsaSign(NULL, signature, &key, &rng), BAD_FUNC_ARG);
  19391. ExpectIntEQ(wc_DsaSign(hash, NULL, &key, &rng), BAD_FUNC_ARG);
  19392. ExpectIntEQ(wc_DsaSign(hash, signature, NULL, &rng), BAD_FUNC_ARG);
  19393. ExpectIntEQ(wc_DsaSign(hash, signature, &key, NULL), BAD_FUNC_ARG);
  19394. /* Verify. */
  19395. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, &answer), 0);
  19396. ExpectIntEQ(answer, 1);
  19397. /* Pass in bad args. */
  19398. ExpectIntEQ(wc_DsaVerify(NULL, signature, &key, &answer), BAD_FUNC_ARG);
  19399. ExpectIntEQ(wc_DsaVerify(hash, NULL, &key, &answer), BAD_FUNC_ARG);
  19400. ExpectIntEQ(wc_DsaVerify(hash, signature, NULL, &answer), BAD_FUNC_ARG);
  19401. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, NULL), BAD_FUNC_ARG);
  19402. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  19403. /* hard set q to 0 and test fail case */
  19404. mp_free(&key.q);
  19405. mp_init(&key.q);
  19406. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  19407. mp_set(&key.q, 1);
  19408. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  19409. #endif
  19410. DoExpectIntEQ(wc_FreeRng(&rng),0);
  19411. wc_FreeDsaKey(&key);
  19412. wc_ShaFree(&sha);
  19413. #endif
  19414. return EXPECT_RESULT();
  19415. } /* END test_wc_DsaSign */
  19416. /*
  19417. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  19418. */
  19419. static int test_wc_DsaPublicPrivateKeyDecode(void)
  19420. {
  19421. EXPECT_DECLS;
  19422. #if !defined(NO_DSA)
  19423. DsaKey key;
  19424. word32 bytes = 0;
  19425. word32 idx = 0;
  19426. int ret = 0;
  19427. #ifdef USE_CERT_BUFFERS_1024
  19428. byte tmp[ONEK_BUF];
  19429. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19430. bytes = sizeof_dsa_key_der_1024;
  19431. #elif defined(USE_CERT_BUFFERS_2048)
  19432. byte tmp[TWOK_BUF];
  19433. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19434. bytes = sizeof_dsa_key_der_2048;
  19435. #else
  19436. byte tmp[TWOK_BUF];
  19437. XFILE fp = XBADFILE;
  19438. XMEMSET(tmp, 0, sizeof(tmp));
  19439. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19440. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19441. if (fp != XBADFILE)
  19442. XFCLOSE(fp);
  19443. #endif /* END USE_CERT_BUFFERS_1024 */
  19444. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19445. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19446. /* Test bad args. */
  19447. ExpectIntEQ(wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  19448. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  19449. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  19450. ExpectIntLT(ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19451. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  19452. wc_FreeDsaKey(&key);
  19453. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19454. idx = 0; /* Reset */
  19455. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  19456. /* Test bad args. */
  19457. ExpectIntEQ(wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  19458. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  19459. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  19460. ExpectIntLT(ret = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  19461. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  19462. wc_FreeDsaKey(&key);
  19463. #endif /* !NO_DSA */
  19464. return EXPECT_RESULT();
  19465. } /* END test_wc_DsaPublicPrivateKeyDecode */
  19466. /*
  19467. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  19468. */
  19469. static int test_wc_MakeDsaKey(void)
  19470. {
  19471. EXPECT_DECLS;
  19472. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19473. DsaKey genKey;
  19474. WC_RNG rng;
  19475. XMEMSET(&genKey, 0, sizeof(genKey));
  19476. XMEMSET(&rng, 0, sizeof(rng));
  19477. ExpectIntEQ(wc_InitDsaKey(&genKey), 0);
  19478. ExpectIntEQ(wc_InitRng(&rng), 0);
  19479. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey), 0);
  19480. /* Test bad args. */
  19481. ExpectIntEQ(wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey), BAD_FUNC_ARG);
  19482. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, NULL), BAD_FUNC_ARG);
  19483. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey),
  19484. BAD_FUNC_ARG);
  19485. ExpectIntEQ(wc_MakeDsaKey(&rng, &genKey), 0);
  19486. /* Test bad args. */
  19487. ExpectIntEQ(wc_MakeDsaKey(NULL, &genKey), BAD_FUNC_ARG);
  19488. ExpectIntEQ(wc_MakeDsaKey(&rng, NULL), BAD_FUNC_ARG);
  19489. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19490. wc_FreeDsaKey(&genKey);
  19491. #endif
  19492. return EXPECT_RESULT();
  19493. } /* END test_wc_MakeDsaKey */
  19494. /*
  19495. * Testing wc_DsaKeyToDer()
  19496. */
  19497. static int test_wc_DsaKeyToDer(void)
  19498. {
  19499. EXPECT_DECLS;
  19500. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19501. DsaKey key;
  19502. word32 bytes;
  19503. word32 idx = 0;
  19504. #ifdef USE_CERT_BUFFERS_1024
  19505. byte tmp[ONEK_BUF];
  19506. byte der[ONEK_BUF];
  19507. XMEMSET(tmp, 0, sizeof(tmp));
  19508. XMEMSET(der, 0, sizeof(der));
  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. byte der[TWOK_BUF];
  19514. XMEMSET(tmp, 0, sizeof(tmp));
  19515. XMEMSET(der, 0, sizeof(der));
  19516. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19517. bytes = sizeof_dsa_key_der_2048;
  19518. #else
  19519. byte tmp[TWOK_BUF];
  19520. byte der[TWOK_BUF];
  19521. XFILE fp = XBADFILE;
  19522. XMEMSET(tmp, 0, sizeof(tmp));
  19523. XMEMSET(der, 0, sizeof(der));
  19524. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19525. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19526. if (fp != XBADFILE)
  19527. XFCLOSE(fp);
  19528. #endif /* END USE_CERT_BUFFERS_1024 */
  19529. XMEMSET(&key, 0, sizeof(DsaKey));
  19530. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19531. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19532. ExpectIntGE(wc_DsaKeyToDer(&key, der, bytes), 0);
  19533. ExpectIntEQ(XMEMCMP(der, tmp, bytes), 0);
  19534. /* Test bad args. */
  19535. ExpectIntEQ(wc_DsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  19536. ExpectIntEQ(wc_DsaKeyToDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  19537. wc_FreeDsaKey(&key);
  19538. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19539. return EXPECT_RESULT();
  19540. } /* END test_wc_DsaKeyToDer */
  19541. /*
  19542. * Testing wc_DsaKeyToPublicDer()
  19543. * (indirectly testing setDsaPublicKey())
  19544. */
  19545. static int test_wc_DsaKeyToPublicDer(void)
  19546. {
  19547. EXPECT_DECLS;
  19548. #ifndef HAVE_SELFTEST
  19549. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19550. DsaKey key;
  19551. WC_RNG rng;
  19552. byte* der = NULL;
  19553. word32 sz = 0;
  19554. word32 idx = 0;
  19555. XMEMSET(&key, 0, sizeof(DsaKey));
  19556. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19557. ExpectNotNull(der = (byte*)XMALLOC(ONEK_BUF, NULL,
  19558. DYNAMIC_TYPE_TMP_BUFFER));
  19559. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19560. ExpectIntEQ(wc_InitRng(&rng), 0);
  19561. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &key), 0);
  19562. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  19563. ExpectIntGE(sz = wc_DsaKeyToPublicDer(&key, der, ONEK_BUF), 0);
  19564. wc_FreeDsaKey(&key);
  19565. idx = 0;
  19566. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  19567. /* Test without the SubjectPublicKeyInfo header */
  19568. ExpectIntGE(sz = wc_SetDsaPublicKey(der, &key, ONEK_BUF, 0), 0);
  19569. wc_FreeDsaKey(&key);
  19570. idx = 0;
  19571. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  19572. /* Test bad args. */
  19573. ExpectIntEQ(wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  19574. ExpectIntEQ(wc_DsaKeyToPublicDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  19575. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19576. wc_FreeDsaKey(&key);
  19577. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  19578. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19579. #endif /* !HAVE_SELFTEST */
  19580. return EXPECT_RESULT();
  19581. } /* END test_wc_DsaKeyToPublicDer */
  19582. /*
  19583. * Testing wc_DsaImportParamsRaw()
  19584. */
  19585. static int test_wc_DsaImportParamsRaw(void)
  19586. {
  19587. EXPECT_DECLS;
  19588. #if !defined(NO_DSA)
  19589. DsaKey key;
  19590. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19591. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19592. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19593. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19594. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19595. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19596. "80a0093113a8bd73";
  19597. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19598. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19599. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19600. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19601. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19602. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19603. "76341a7e7d9";
  19604. /* invalid p and q parameters */
  19605. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19606. const char* invalidQ = "96c5390a";
  19607. XMEMSET(&key, 0, sizeof(DsaKey));
  19608. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19609. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  19610. /* test bad args */
  19611. /* null key struct */
  19612. ExpectIntEQ(wc_DsaImportParamsRaw(NULL, p, q, g), BAD_FUNC_ARG);
  19613. /* null param pointers */
  19614. ExpectIntEQ(wc_DsaImportParamsRaw(&key, NULL, NULL, NULL), BAD_FUNC_ARG);
  19615. /* illegal p length */
  19616. ExpectIntEQ(wc_DsaImportParamsRaw(&key, invalidP, q, g), BAD_FUNC_ARG);
  19617. /* illegal q length */
  19618. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, invalidQ, g), BAD_FUNC_ARG);
  19619. wc_FreeDsaKey(&key);
  19620. #endif
  19621. return EXPECT_RESULT();
  19622. } /* END test_wc_DsaImportParamsRaw */
  19623. /*
  19624. * Testing wc_DsaImportParamsRawCheck()
  19625. */
  19626. static int test_wc_DsaImportParamsRawCheck(void)
  19627. {
  19628. EXPECT_DECLS;
  19629. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  19630. DsaKey key;
  19631. int trusted = 0;
  19632. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19633. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19634. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19635. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19636. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19637. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19638. "80a0093113a8bd73";
  19639. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19640. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19641. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19642. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19643. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19644. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19645. "76341a7e7d9";
  19646. /* invalid p and q parameters */
  19647. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19648. const char* invalidQ = "96c5390a";
  19649. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19650. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL), 0);
  19651. /* test bad args */
  19652. /* null key struct */
  19653. ExpectIntEQ(wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL),
  19654. BAD_FUNC_ARG);
  19655. /* null param pointers */
  19656. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted,
  19657. NULL), BAD_FUNC_ARG);
  19658. /* illegal p length */
  19659. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL),
  19660. BAD_FUNC_ARG);
  19661. /* illegal q length */
  19662. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL),
  19663. BAD_FUNC_ARG);
  19664. wc_FreeDsaKey(&key);
  19665. #endif
  19666. return EXPECT_RESULT();
  19667. } /* END test_wc_DsaImportParamsRawCheck */
  19668. /*
  19669. * Testing wc_DsaExportParamsRaw()
  19670. */
  19671. static int test_wc_DsaExportParamsRaw(void)
  19672. {
  19673. EXPECT_DECLS;
  19674. #if !defined(NO_DSA)
  19675. DsaKey key;
  19676. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19677. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19678. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19679. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19680. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19681. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19682. "80a0093113a8bd73";
  19683. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19684. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19685. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19686. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19687. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19688. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19689. "76341a7e7d9";
  19690. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  19691. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  19692. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  19693. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  19694. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  19695. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  19696. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  19697. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  19698. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  19699. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  19700. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  19701. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  19702. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  19703. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  19704. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  19705. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  19706. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  19707. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  19708. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  19709. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  19710. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  19711. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  19712. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  19713. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  19714. byte pOut[MAX_DSA_PARAM_SIZE];
  19715. byte qOut[MAX_DSA_PARAM_SIZE];
  19716. byte gOut[MAX_DSA_PARAM_SIZE];
  19717. word32 pOutSz;
  19718. word32 qOutSz;
  19719. word32 gOutSz;
  19720. XMEMSET(&key, 0, sizeof(DsaKey));
  19721. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19722. /* first test using imported raw parameters, for expected */
  19723. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  19724. pOutSz = sizeof(pOut);
  19725. qOutSz = sizeof(qOut);
  19726. gOutSz = sizeof(gOut);
  19727. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19728. &gOutSz), 0);
  19729. /* validate exported parameters are correct */
  19730. ExpectIntEQ(XMEMCMP(pOut, pCompare, pOutSz), 0);
  19731. ExpectIntEQ(XMEMCMP(qOut, qCompare, qOutSz), 0);
  19732. ExpectIntEQ(XMEMCMP(gOut, gCompare, gOutSz), 0);
  19733. /* test bad args */
  19734. /* null key struct */
  19735. ExpectIntEQ(wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19736. &gOutSz), BAD_FUNC_ARG);
  19737. /* null output pointers */
  19738. ExpectIntEQ(wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz, NULL,
  19739. &gOutSz), LENGTH_ONLY_E);
  19740. /* null output size pointers */
  19741. ExpectIntEQ( wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL, gOut,
  19742. NULL), BAD_FUNC_ARG);
  19743. /* p output buffer size too small */
  19744. pOutSz = 1;
  19745. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19746. &gOutSz), BUFFER_E);
  19747. pOutSz = sizeof(pOut);
  19748. /* q output buffer size too small */
  19749. qOutSz = 1;
  19750. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19751. &gOutSz), BUFFER_E);
  19752. qOutSz = sizeof(qOut);
  19753. /* g output buffer size too small */
  19754. gOutSz = 1;
  19755. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19756. &gOutSz), BUFFER_E);
  19757. wc_FreeDsaKey(&key);
  19758. #endif
  19759. return EXPECT_RESULT();
  19760. } /* END test_wc_DsaExportParamsRaw */
  19761. /*
  19762. * Testing wc_DsaExportKeyRaw()
  19763. */
  19764. static int test_wc_DsaExportKeyRaw(void)
  19765. {
  19766. EXPECT_DECLS;
  19767. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19768. DsaKey key;
  19769. WC_RNG rng;
  19770. byte xOut[MAX_DSA_PARAM_SIZE];
  19771. byte yOut[MAX_DSA_PARAM_SIZE];
  19772. word32 xOutSz, yOutSz;
  19773. XMEMSET(&key, 0, sizeof(key));
  19774. XMEMSET(&rng, 0, sizeof(rng));
  19775. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19776. ExpectIntEQ(wc_InitRng(&rng), 0);
  19777. ExpectIntEQ(wc_MakeDsaParameters(&rng, 1024, &key), 0);
  19778. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  19779. /* try successful export */
  19780. xOutSz = sizeof(xOut);
  19781. yOutSz = sizeof(yOut);
  19782. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz), 0);
  19783. /* test bad args */
  19784. /* null key struct */
  19785. ExpectIntEQ(wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz),
  19786. BAD_FUNC_ARG);
  19787. /* null output pointers */
  19788. ExpectIntEQ(wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz),
  19789. LENGTH_ONLY_E);
  19790. /* null output size pointers */
  19791. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL),
  19792. BAD_FUNC_ARG);
  19793. /* x output buffer size too small */
  19794. xOutSz = 1;
  19795. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  19796. BUFFER_E);
  19797. xOutSz = sizeof(xOut);
  19798. /* y output buffer size too small */
  19799. yOutSz = 1;
  19800. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  19801. BUFFER_E);
  19802. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19803. wc_FreeDsaKey(&key);
  19804. #endif
  19805. return EXPECT_RESULT();
  19806. } /* END test_wc_DsaExportParamsRaw */
  19807. /*
  19808. * Testing wc_ed25519_make_key().
  19809. */
  19810. static int test_wc_ed25519_make_key(void)
  19811. {
  19812. EXPECT_DECLS;
  19813. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_MAKE_KEY)
  19814. ed25519_key key;
  19815. WC_RNG rng;
  19816. unsigned char pubkey[ED25519_PUB_KEY_SIZE+1];
  19817. int pubkey_sz = ED25519_PUB_KEY_SIZE;
  19818. XMEMSET(&key, 0, sizeof(ed25519_key));
  19819. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19820. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19821. ExpectIntEQ(wc_InitRng(&rng), 0);
  19822. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey, pubkey_sz),
  19823. ECC_PRIV_KEY_E);
  19824. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey+1, pubkey_sz),
  19825. ECC_PRIV_KEY_E);
  19826. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19827. /* Test bad args. */
  19828. ExpectIntEQ(wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key),
  19829. BAD_FUNC_ARG);
  19830. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL),
  19831. BAD_FUNC_ARG);
  19832. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key),
  19833. BAD_FUNC_ARG);
  19834. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key),
  19835. BAD_FUNC_ARG);
  19836. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19837. wc_ed25519_free(&key);
  19838. #endif
  19839. return EXPECT_RESULT();
  19840. } /* END test_wc_ed25519_make_key */
  19841. /*
  19842. * Testing wc_ed25519_init()
  19843. */
  19844. static int test_wc_ed25519_init(void)
  19845. {
  19846. EXPECT_DECLS;
  19847. #if defined(HAVE_ED25519)
  19848. ed25519_key key;
  19849. XMEMSET(&key, 0, sizeof(ed25519_key));
  19850. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19851. /* Test bad args. */
  19852. ExpectIntEQ(wc_ed25519_init(NULL), BAD_FUNC_ARG);
  19853. wc_ed25519_free(&key);
  19854. #endif
  19855. return EXPECT_RESULT();
  19856. } /* END test_wc_ed25519_init */
  19857. /*
  19858. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  19859. */
  19860. static int test_wc_ed25519_sign_msg(void)
  19861. {
  19862. EXPECT_DECLS;
  19863. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  19864. WC_RNG rng;
  19865. ed25519_key key;
  19866. byte msg[] = "Everybody gets Friday off.\n";
  19867. byte sig[ED25519_SIG_SIZE+1];
  19868. word32 msglen = sizeof(msg);
  19869. word32 siglen = ED25519_SIG_SIZE;
  19870. word32 badSigLen = ED25519_SIG_SIZE - 1;
  19871. #ifdef HAVE_ED25519_VERIFY
  19872. int verify_ok = 0; /*1 = Verify success.*/
  19873. #endif
  19874. /* Initialize stack variables. */
  19875. XMEMSET(&key, 0, sizeof(ed25519_key));
  19876. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19877. XMEMSET(sig, 0, sizeof(sig));
  19878. /* Initialize key. */
  19879. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19880. ExpectIntEQ(wc_InitRng(&rng), 0);
  19881. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19882. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key), 0);
  19883. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  19884. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig+1, &siglen, &key), 0);
  19885. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  19886. /* Test bad args. */
  19887. ExpectIntEQ(wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key),
  19888. BAD_FUNC_ARG);
  19889. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key),
  19890. BAD_FUNC_ARG);
  19891. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key),
  19892. BAD_FUNC_ARG);
  19893. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL),
  19894. BAD_FUNC_ARG);
  19895. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key),
  19896. BUFFER_E);
  19897. ExpectIntEQ(badSigLen, ED25519_SIG_SIZE);
  19898. badSigLen -= 1;
  19899. #ifdef HAVE_ED25519_VERIFY
  19900. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  19901. &key), 0);
  19902. ExpectIntEQ(verify_ok, 1);
  19903. /* Test bad args. */
  19904. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen - 1, msg, msglen,
  19905. &verify_ok, &key), BAD_FUNC_ARG);
  19906. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen + 1, msg, msglen,
  19907. &verify_ok, &key), BAD_FUNC_ARG);
  19908. ExpectIntEQ(wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  19909. &key), BAD_FUNC_ARG);
  19910. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, NULL, msglen, &verify_ok,
  19911. &key), BAD_FUNC_ARG);
  19912. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, NULL, &key),
  19913. BAD_FUNC_ARG);
  19914. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  19915. NULL), BAD_FUNC_ARG);
  19916. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, badSigLen, msg, msglen, &verify_ok,
  19917. &key), BAD_FUNC_ARG);
  19918. #endif /* Verify. */
  19919. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19920. wc_ed25519_free(&key);
  19921. #endif
  19922. return EXPECT_RESULT();
  19923. } /* END test_wc_ed25519_sign_msg */
  19924. /*
  19925. * Testing wc_ed25519_import_public()
  19926. */
  19927. static int test_wc_ed25519_import_public(void)
  19928. {
  19929. EXPECT_DECLS;
  19930. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  19931. ed25519_key pubKey;
  19932. WC_RNG rng;
  19933. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  19934. word32 inlen = sizeof(in);
  19935. XMEMSET(&pubKey, 0, sizeof(ed25519_key));
  19936. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19937. ExpectIntEQ(wc_ed25519_init(&pubKey), 0);
  19938. ExpectIntEQ(wc_InitRng(&rng), 0);
  19939. #ifdef HAVE_ED25519_MAKE_KEY
  19940. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey), 0);
  19941. #endif
  19942. ExpectIntEQ(wc_ed25519_import_public_ex(in, inlen, &pubKey, 1), 0);
  19943. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  19944. /* Test bad args. */
  19945. ExpectIntEQ(wc_ed25519_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  19946. ExpectIntEQ(wc_ed25519_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  19947. ExpectIntEQ(wc_ed25519_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  19948. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19949. wc_ed25519_free(&pubKey);
  19950. #endif
  19951. return EXPECT_RESULT();
  19952. } /* END wc_ed25519_import_public */
  19953. /*
  19954. * Testing wc_ed25519_import_private_key()
  19955. */
  19956. static int test_wc_ed25519_import_private_key(void)
  19957. {
  19958. EXPECT_DECLS;
  19959. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  19960. ed25519_key key;
  19961. WC_RNG rng;
  19962. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  19963. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  19964. word32 privKeySz = sizeof(privKey);
  19965. word32 pubKeySz = sizeof(pubKey);
  19966. #ifdef HAVE_ED25519_KEY_EXPORT
  19967. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  19968. word32 bothKeysSz = sizeof(bothKeys);
  19969. #endif
  19970. XMEMSET(&key, 0, sizeof(ed25519_key));
  19971. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19972. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19973. ExpectIntEQ(wc_InitRng(&rng), 0);
  19974. #ifdef HAVE_ED25519_MAKE_KEY
  19975. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19976. #endif
  19977. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  19978. pubKeySz, &key, 1), 0);
  19979. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  19980. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  19981. #ifdef HAVE_ED25519_KEY_EXPORT
  19982. PRIVATE_KEY_UNLOCK();
  19983. ExpectIntEQ(wc_ed25519_export_private(&key, bothKeys, &bothKeysSz), 0);
  19984. PRIVATE_KEY_LOCK();
  19985. ExpectIntEQ(wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  19986. &key, 1), 0);
  19987. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  19988. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  19989. #endif
  19990. /* Test bad args. */
  19991. ExpectIntEQ(wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  19992. &key), BAD_FUNC_ARG);
  19993. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  19994. pubKeySz, &key), BAD_FUNC_ARG);
  19995. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  19996. pubKeySz, NULL), BAD_FUNC_ARG);
  19997. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  19998. pubKeySz, &key), BAD_FUNC_ARG);
  19999. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  20000. pubKeySz - 1, &key), BAD_FUNC_ARG);
  20001. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL, 0,
  20002. &key), BAD_FUNC_ARG);
  20003. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20004. wc_ed25519_free(&key);
  20005. #endif
  20006. return EXPECT_RESULT();
  20007. } /* END test_wc_ed25519_import_private_key */
  20008. /*
  20009. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  20010. */
  20011. static int test_wc_ed25519_export(void)
  20012. {
  20013. EXPECT_DECLS;
  20014. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20015. ed25519_key key;
  20016. WC_RNG rng;
  20017. byte priv[ED25519_PRV_KEY_SIZE];
  20018. byte pub[ED25519_PUB_KEY_SIZE];
  20019. word32 privSz = sizeof(priv);
  20020. word32 pubSz = sizeof(pub);
  20021. #ifndef HAVE_ED25519_MAKE_KEY
  20022. const byte privKey[] = {
  20023. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20024. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20025. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20026. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20027. };
  20028. const byte pubKey[] = {
  20029. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20030. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20031. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20032. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20033. };
  20034. #endif
  20035. XMEMSET(&key, 0, sizeof(ed25519_key));
  20036. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20037. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20038. ExpectIntEQ(wc_InitRng(&rng), 0);
  20039. #ifdef HAVE_ED25519_MAKE_KEY
  20040. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20041. #else
  20042. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20043. pubKey, sizeof(pubKey), &key, 1), 0);
  20044. #endif
  20045. PRIVATE_KEY_UNLOCK();
  20046. ExpectIntEQ(wc_ed25519_export_public(&key, pub, &pubSz), 0);
  20047. ExpectIntEQ(pubSz, ED25519_KEY_SIZE);
  20048. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  20049. /* Test bad args. */
  20050. ExpectIntEQ(wc_ed25519_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  20051. ExpectIntEQ(wc_ed25519_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  20052. ExpectIntEQ(wc_ed25519_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  20053. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, &privSz), 0);
  20054. ExpectIntEQ(privSz, ED25519_KEY_SIZE);
  20055. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  20056. /* Test bad args. */
  20057. ExpectIntEQ(wc_ed25519_export_private_only(NULL, priv, &privSz),
  20058. BAD_FUNC_ARG);
  20059. ExpectIntEQ(wc_ed25519_export_private_only(&key, NULL, &privSz),
  20060. BAD_FUNC_ARG);
  20061. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, NULL),
  20062. BAD_FUNC_ARG);
  20063. PRIVATE_KEY_LOCK();
  20064. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20065. wc_ed25519_free(&key);
  20066. #endif
  20067. return EXPECT_RESULT();
  20068. } /* END test_wc_ed25519_export */
  20069. /*
  20070. * Testing wc_ed25519_size()
  20071. */
  20072. static int test_wc_ed25519_size(void)
  20073. {
  20074. EXPECT_DECLS;
  20075. #if defined(HAVE_ED25519)
  20076. ed25519_key key;
  20077. WC_RNG rng;
  20078. #ifndef HAVE_ED25519_MAKE_KEY
  20079. const byte privKey[] = {
  20080. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20081. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20082. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20083. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20084. };
  20085. const byte pubKey[] = {
  20086. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20087. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20088. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20089. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20090. };
  20091. #endif
  20092. XMEMSET(&key, 0, sizeof(ed25519_key));
  20093. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20094. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20095. ExpectIntEQ(wc_InitRng(&rng), 0);
  20096. #ifdef HAVE_ED25519_MAKE_KEY
  20097. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20098. #else
  20099. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20100. pubKey, sizeof(pubKey), &key, 1), 0);
  20101. #endif
  20102. ExpectIntEQ(wc_ed25519_size(&key), ED25519_KEY_SIZE);
  20103. /* Test bad args. */
  20104. ExpectIntEQ(wc_ed25519_size(NULL), BAD_FUNC_ARG);
  20105. ExpectIntEQ(wc_ed25519_sig_size(&key), ED25519_SIG_SIZE);
  20106. /* Test bad args. */
  20107. ExpectIntEQ(wc_ed25519_sig_size(NULL), BAD_FUNC_ARG);
  20108. ExpectIntEQ(wc_ed25519_pub_size(&key), ED25519_PUB_KEY_SIZE);
  20109. /* Test bad args. */
  20110. ExpectIntEQ(wc_ed25519_pub_size(NULL), BAD_FUNC_ARG);
  20111. ExpectIntEQ(wc_ed25519_priv_size(&key), ED25519_PRV_KEY_SIZE);
  20112. /* Test bad args. */
  20113. ExpectIntEQ(wc_ed25519_priv_size(NULL), BAD_FUNC_ARG);
  20114. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20115. wc_ed25519_free(&key);
  20116. #endif
  20117. return EXPECT_RESULT();
  20118. } /* END test_wc_ed25519_size */
  20119. /*
  20120. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  20121. */
  20122. static int test_wc_ed25519_exportKey(void)
  20123. {
  20124. EXPECT_DECLS;
  20125. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20126. WC_RNG rng;
  20127. ed25519_key key;
  20128. byte priv[ED25519_PRV_KEY_SIZE];
  20129. byte pub[ED25519_PUB_KEY_SIZE];
  20130. byte privOnly[ED25519_PRV_KEY_SIZE];
  20131. word32 privSz = sizeof(priv);
  20132. word32 pubSz = sizeof(pub);
  20133. word32 privOnlySz = sizeof(privOnly);
  20134. #ifndef HAVE_ED25519_MAKE_KEY
  20135. const byte privKey[] = {
  20136. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20137. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20138. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20139. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20140. };
  20141. const byte pubKey[] = {
  20142. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20143. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20144. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20145. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20146. };
  20147. #endif
  20148. XMEMSET(&key, 0, sizeof(ed25519_key));
  20149. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20150. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20151. ExpectIntEQ(wc_InitRng(&rng), 0);
  20152. #ifdef HAVE_ED25519_MAKE_KEY
  20153. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20154. #else
  20155. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20156. pubKey, sizeof(pubKey), &key, 1), 0);
  20157. #endif
  20158. PRIVATE_KEY_UNLOCK();
  20159. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, &privOnlySz), 0);
  20160. /* Test bad args. */
  20161. ExpectIntEQ(wc_ed25519_export_private(NULL, privOnly, &privOnlySz),
  20162. BAD_FUNC_ARG);
  20163. ExpectIntEQ(wc_ed25519_export_private(&key, NULL, &privOnlySz),
  20164. BAD_FUNC_ARG);
  20165. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  20166. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  20167. /* Test bad args. */
  20168. ExpectIntEQ(wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz),
  20169. BAD_FUNC_ARG);
  20170. ExpectIntEQ(wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz),
  20171. BAD_FUNC_ARG);
  20172. ExpectIntEQ(wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz),
  20173. BAD_FUNC_ARG);
  20174. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz),
  20175. BAD_FUNC_ARG);
  20176. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, NULL),
  20177. BAD_FUNC_ARG);
  20178. PRIVATE_KEY_LOCK();
  20179. /* Cross check output. */
  20180. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  20181. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20182. wc_ed25519_free(&key);
  20183. #endif
  20184. return EXPECT_RESULT();
  20185. } /* END test_wc_ed25519_exportKey */
  20186. /*
  20187. * Testing wc_Ed25519PublicKeyToDer
  20188. */
  20189. static int test_wc_Ed25519PublicKeyToDer(void)
  20190. {
  20191. EXPECT_DECLS;
  20192. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  20193. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20194. ed25519_key key;
  20195. byte derBuf[1024];
  20196. XMEMSET(&key, 0, sizeof(ed25519_key));
  20197. /* Test bad args */
  20198. ExpectIntEQ(wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  20199. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20200. ExpectIntEQ(wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  20201. wc_ed25519_free(&key);
  20202. /* Test good args */
  20203. if (EXPECT_SUCCESS()) {
  20204. WC_RNG rng;
  20205. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20206. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20207. ExpectIntEQ(wc_InitRng(&rng), 0);
  20208. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20209. ExpectIntGT(wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  20210. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20211. wc_ed25519_free(&key);
  20212. }
  20213. #endif
  20214. return EXPECT_RESULT();
  20215. } /* END testing wc_Ed25519PublicKeyToDer */
  20216. /*
  20217. * Testing wc_curve25519_init and wc_curve25519_free.
  20218. */
  20219. static int test_wc_curve25519_init(void)
  20220. {
  20221. EXPECT_DECLS;
  20222. #if defined(HAVE_CURVE25519)
  20223. curve25519_key key;
  20224. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20225. /* Test bad args for wc_curve25519_init */
  20226. ExpectIntEQ(wc_curve25519_init(NULL), BAD_FUNC_ARG);
  20227. /* Test good args for wc_curve_25519_free */
  20228. wc_curve25519_free(&key);
  20229. /* Test bad args for wc_curve25519 free. */
  20230. wc_curve25519_free(NULL);
  20231. #endif
  20232. return EXPECT_RESULT();
  20233. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  20234. /*
  20235. * Testing test_wc_curve25519_size.
  20236. */
  20237. static int test_wc_curve25519_size(void)
  20238. {
  20239. EXPECT_DECLS;
  20240. #if defined(HAVE_CURVE25519)
  20241. curve25519_key key;
  20242. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20243. /* Test good args for wc_curve25519_size */
  20244. ExpectIntEQ(wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  20245. /* Test bad args for wc_curve25519_size */
  20246. ExpectIntEQ(wc_curve25519_size(NULL), 0);
  20247. wc_curve25519_free(&key);
  20248. #endif
  20249. return EXPECT_RESULT();
  20250. } /* END test_wc_curve25519_size*/
  20251. /*
  20252. * Testing test_wc_curve25519_export_key_raw().
  20253. */
  20254. static int test_wc_curve25519_export_key_raw(void)
  20255. {
  20256. EXPECT_DECLS;
  20257. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  20258. curve25519_key key;
  20259. WC_RNG rng;
  20260. byte privateKey[CURVE25519_KEYSIZE];
  20261. byte publicKey[CURVE25519_KEYSIZE];
  20262. word32 prvkSz;
  20263. word32 pubkSz;
  20264. byte prik[CURVE25519_KEYSIZE];
  20265. byte pubk[CURVE25519_KEYSIZE];
  20266. word32 prksz;
  20267. word32 pbksz;
  20268. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20269. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20270. ExpectIntEQ(wc_InitRng(&rng), 0);
  20271. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20272. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  20273. prvkSz = CURVE25519_KEYSIZE;
  20274. pubkSz = CURVE25519_KEYSIZE;
  20275. ExpectIntEQ(wc_curve25519_export_key_raw(NULL, privateKey, &prvkSz,
  20276. publicKey, &pubkSz), BAD_FUNC_ARG);
  20277. prvkSz = CURVE25519_KEYSIZE;
  20278. pubkSz = CURVE25519_KEYSIZE;
  20279. ExpectIntEQ(wc_curve25519_export_key_raw(&key, NULL, &prvkSz, publicKey,
  20280. &pubkSz), BAD_FUNC_ARG);
  20281. prvkSz = CURVE25519_KEYSIZE;
  20282. pubkSz = CURVE25519_KEYSIZE;
  20283. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, NULL,
  20284. publicKey, &pubkSz), BAD_FUNC_ARG);
  20285. /* prvkSz = CURVE25519_KEYSIZE; */
  20286. pubkSz = CURVE25519_KEYSIZE;
  20287. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20288. NULL, &pubkSz), BAD_FUNC_ARG);
  20289. prvkSz = CURVE25519_KEYSIZE;
  20290. pubkSz = CURVE25519_KEYSIZE;
  20291. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20292. publicKey, NULL), BAD_FUNC_ARG);
  20293. /* cross-testing */
  20294. prksz = CURVE25519_KEYSIZE;
  20295. ExpectIntEQ(wc_curve25519_export_private_raw(&key, prik, &prksz), 0);
  20296. pbksz = CURVE25519_KEYSIZE;
  20297. ExpectIntEQ(wc_curve25519_export_public(&key, pubk, &pbksz), 0);
  20298. prvkSz = CURVE25519_KEYSIZE;
  20299. /* pubkSz = CURVE25519_KEYSIZE; */
  20300. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20301. publicKey, &pubkSz), 0);
  20302. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  20303. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  20304. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20305. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20306. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  20307. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  20308. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20309. wc_curve25519_free(&key);
  20310. #endif
  20311. return EXPECT_RESULT();
  20312. } /* end of test_wc_curve25519_export_key_raw */
  20313. /*
  20314. * Testing test_wc_curve25519_export_key_raw_ex().
  20315. */
  20316. static int test_wc_curve25519_export_key_raw_ex(void)
  20317. {
  20318. EXPECT_DECLS;
  20319. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  20320. curve25519_key key;
  20321. WC_RNG rng;
  20322. byte privateKey[CURVE25519_KEYSIZE];
  20323. byte publicKey[CURVE25519_KEYSIZE];
  20324. word32 prvkSz;
  20325. word32 pubkSz;
  20326. byte prik[CURVE25519_KEYSIZE];
  20327. byte pubk[CURVE25519_KEYSIZE];
  20328. word32 prksz;
  20329. word32 pbksz;
  20330. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20331. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20332. ExpectIntEQ(wc_InitRng(&rng), 0);
  20333. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20334. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  20335. prvkSz = CURVE25519_KEYSIZE;
  20336. pubkSz = CURVE25519_KEYSIZE;
  20337. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  20338. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20339. prvkSz = CURVE25519_KEYSIZE;
  20340. pubkSz = CURVE25519_KEYSIZE;
  20341. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  20342. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20343. prvkSz = CURVE25519_KEYSIZE;
  20344. pubkSz = CURVE25519_KEYSIZE;
  20345. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20346. NULL, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20347. /* prvkSz = CURVE25519_KEYSIZE; */
  20348. pubkSz = CURVE25519_KEYSIZE;
  20349. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20350. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20351. prvkSz = CURVE25519_KEYSIZE;
  20352. pubkSz = CURVE25519_KEYSIZE;
  20353. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20354. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20355. prvkSz = CURVE25519_KEYSIZE;
  20356. /* pubkSz = CURVE25519_KEYSIZE; */
  20357. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  20358. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20359. prvkSz = CURVE25519_KEYSIZE;
  20360. pubkSz = CURVE25519_KEYSIZE;
  20361. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  20362. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20363. prvkSz = CURVE25519_KEYSIZE;
  20364. pubkSz = CURVE25519_KEYSIZE;
  20365. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20366. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20367. /* prvkSz = CURVE25519_KEYSIZE; */
  20368. pubkSz = CURVE25519_KEYSIZE;
  20369. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20370. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20371. prvkSz = CURVE25519_KEYSIZE;
  20372. pubkSz = CURVE25519_KEYSIZE;
  20373. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20374. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20375. /* illegal value for endian */
  20376. prvkSz = CURVE25519_KEYSIZE;
  20377. /* pubkSz = CURVE25519_KEYSIZE; */
  20378. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20379. publicKey, NULL, EC25519_BIG_ENDIAN + 10), BAD_FUNC_ARG);
  20380. /* cross-testing */
  20381. prksz = CURVE25519_KEYSIZE;
  20382. ExpectIntEQ(wc_curve25519_export_private_raw( &key, prik, &prksz), 0);
  20383. pbksz = CURVE25519_KEYSIZE;
  20384. ExpectIntEQ(wc_curve25519_export_public( &key, pubk, &pbksz), 0);
  20385. prvkSz = CURVE25519_KEYSIZE;
  20386. /* pubkSz = CURVE25519_KEYSIZE; */
  20387. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20388. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  20389. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  20390. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  20391. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20392. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20393. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  20394. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  20395. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20396. publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), 0);
  20397. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20398. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20399. /* try once with another endian */
  20400. prvkSz = CURVE25519_KEYSIZE;
  20401. pubkSz = CURVE25519_KEYSIZE;
  20402. ExpectIntEQ(wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  20403. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  20404. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20405. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20406. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20407. wc_curve25519_free(&key);
  20408. #endif
  20409. return EXPECT_RESULT();
  20410. } /* end of test_wc_curve25519_export_key_raw_ex */
  20411. /*
  20412. * Testing wc_curve25519_make_key
  20413. */
  20414. static int test_wc_curve25519_make_key(void)
  20415. {
  20416. EXPECT_DECLS;
  20417. #if defined(HAVE_CURVE25519)
  20418. curve25519_key key;
  20419. WC_RNG rng;
  20420. int keysize;
  20421. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20422. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20423. ExpectIntEQ(wc_InitRng(&rng), 0);
  20424. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20425. ExpectIntEQ(keysize = wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  20426. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, &key), 0);
  20427. /* test bad cases*/
  20428. ExpectIntEQ(wc_curve25519_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  20429. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  20430. ExpectIntEQ(wc_curve25519_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  20431. ExpectIntEQ(wc_curve25519_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  20432. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20433. wc_curve25519_free(&key);
  20434. #endif
  20435. return EXPECT_RESULT();
  20436. } /* END test_wc_curve25519_make_key*/
  20437. /*
  20438. * Testing wc_curve25519_shared_secret_ex
  20439. */
  20440. static int test_wc_curve25519_shared_secret_ex(void)
  20441. {
  20442. EXPECT_DECLS;
  20443. #if defined(HAVE_CURVE25519)
  20444. curve25519_key private_key;
  20445. curve25519_key public_key;
  20446. WC_RNG rng;
  20447. byte out[CURVE25519_KEYSIZE];
  20448. word32 outLen = sizeof(out);
  20449. int endian = EC25519_BIG_ENDIAN;
  20450. ExpectIntEQ(wc_curve25519_init(&private_key), 0);
  20451. ExpectIntEQ(wc_curve25519_init(&public_key), 0);
  20452. ExpectIntEQ(wc_InitRng(&rng), 0);
  20453. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key),
  20454. 0);
  20455. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key),
  20456. 0);
  20457. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20458. &outLen, endian), 0);
  20459. /* test bad cases*/
  20460. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  20461. BAD_FUNC_ARG);
  20462. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, &public_key, out, &outLen,
  20463. endian), BAD_FUNC_ARG);
  20464. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, NULL, out, &outLen,
  20465. endian), BAD_FUNC_ARG);
  20466. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  20467. &outLen, endian), BAD_FUNC_ARG);
  20468. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20469. NULL, endian), BAD_FUNC_ARG);
  20470. /* curve25519.c is checking for public_key size less than or equal to 0x7f,
  20471. * increasing to 0x8f checks for error being returned*/
  20472. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  20473. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20474. &outLen, endian), ECC_BAD_ARG_E);
  20475. outLen = outLen - 2;
  20476. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20477. &outLen, endian), BAD_FUNC_ARG);
  20478. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20479. wc_curve25519_free(&private_key);
  20480. wc_curve25519_free(&public_key);
  20481. #endif
  20482. return EXPECT_RESULT();
  20483. } /* END test_wc_curve25519_shared_secret_ex*/
  20484. /*
  20485. * Testing wc_curve25519_make_pub
  20486. */
  20487. static int test_wc_curve25519_make_pub(void)
  20488. {
  20489. EXPECT_DECLS;
  20490. #ifdef HAVE_CURVE25519
  20491. curve25519_key key;
  20492. WC_RNG rng;
  20493. byte out[CURVE25519_KEYSIZE];
  20494. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20495. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20496. ExpectIntEQ(wc_InitRng(&rng), 0);
  20497. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20498. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(out), out,
  20499. (int)sizeof(key.k), key.k), 0);
  20500. /* test bad cases*/
  20501. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k,
  20502. (int)sizeof out, out), ECC_BAD_ARG_E);
  20503. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20504. NULL), ECC_BAD_ARG_E);
  20505. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out - 1, out,
  20506. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  20507. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, NULL,
  20508. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  20509. /* verify clamping test */
  20510. key.k[0] |= ~248;
  20511. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20512. key.k), ECC_BAD_ARG_E);
  20513. key.k[0] &= 248;
  20514. /* repeat the expected-to-succeed test. */
  20515. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20516. key.k), 0);
  20517. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20518. wc_curve25519_free(&key);
  20519. #endif
  20520. return EXPECT_RESULT();
  20521. } /* END test_wc_curve25519_make_pub */
  20522. /*
  20523. * Testing test_wc_curve25519_export_public_ex
  20524. */
  20525. static int test_wc_curve25519_export_public_ex(void)
  20526. {
  20527. EXPECT_DECLS;
  20528. #if defined(HAVE_CURVE25519)
  20529. curve25519_key key;
  20530. WC_RNG rng;
  20531. byte out[CURVE25519_KEYSIZE];
  20532. word32 outLen = sizeof(out);
  20533. int endian = EC25519_BIG_ENDIAN;
  20534. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20535. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20536. ExpectIntEQ(wc_InitRng(&rng), 0);
  20537. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20538. ExpectIntEQ(wc_curve25519_export_public(&key, out, &outLen), 0);
  20539. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian), 0);
  20540. /* test bad cases*/
  20541. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, NULL, NULL, endian),
  20542. BAD_FUNC_ARG);
  20543. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, out, &outLen, endian),
  20544. BAD_FUNC_ARG);
  20545. ExpectIntEQ(wc_curve25519_export_public_ex(&key, NULL, &outLen, endian),
  20546. BAD_FUNC_ARG);
  20547. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, NULL, endian),
  20548. BAD_FUNC_ARG);
  20549. outLen = outLen - 2;
  20550. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian),
  20551. ECC_BAD_ARG_E);
  20552. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20553. wc_curve25519_free(&key);
  20554. #endif
  20555. return EXPECT_RESULT();
  20556. } /* END test_wc_curve25519_export_public_ex*/
  20557. /*
  20558. * Testing test_wc_curve25519_import_private_raw_ex
  20559. */
  20560. static int test_wc_curve25519_import_private_raw_ex(void)
  20561. {
  20562. EXPECT_DECLS;
  20563. #if defined(HAVE_CURVE25519)
  20564. curve25519_key key;
  20565. WC_RNG rng;
  20566. byte priv[CURVE25519_KEYSIZE];
  20567. byte pub[CURVE25519_KEYSIZE];
  20568. word32 privSz = sizeof(priv);
  20569. word32 pubSz = sizeof(pub);
  20570. int endian = EC25519_BIG_ENDIAN;
  20571. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20572. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20573. ExpectIntEQ(wc_InitRng(&rng), 0);
  20574. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20575. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, priv, &privSz,
  20576. endian), 0);
  20577. ExpectIntEQ(wc_curve25519_export_public(&key, pub, &pubSz), 0);
  20578. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20579. &key, endian), 0);
  20580. /* test bad cases*/
  20581. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  20582. endian), BAD_FUNC_ARG);
  20583. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20584. &key, endian), BAD_FUNC_ARG);
  20585. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20586. &key, endian), BAD_FUNC_ARG);
  20587. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20588. NULL, endian), BAD_FUNC_ARG);
  20589. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  20590. &key, endian), ECC_BAD_ARG_E);
  20591. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  20592. &key, endian), ECC_BAD_ARG_E);
  20593. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20594. &key, EC25519_LITTLE_ENDIAN), 0);
  20595. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20596. wc_curve25519_free(&key);
  20597. #endif
  20598. return EXPECT_RESULT();
  20599. } /* END test_wc_curve25519_import_private_raw_ex*/
  20600. /*
  20601. * Testing test_wc_curve25519_import_private
  20602. */
  20603. static int test_wc_curve25519_import_private(void)
  20604. {
  20605. EXPECT_DECLS;
  20606. #if defined(HAVE_CURVE25519)
  20607. curve25519_key key;
  20608. WC_RNG rng;
  20609. byte priv[CURVE25519_KEYSIZE];
  20610. word32 privSz = sizeof(priv);
  20611. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20612. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20613. ExpectIntEQ(wc_InitRng(&rng), 0);
  20614. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20615. ExpectIntEQ(wc_curve25519_export_private_raw(&key, priv, &privSz), 0);
  20616. ExpectIntEQ(wc_curve25519_import_private(priv, privSz, &key), 0);
  20617. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20618. wc_curve25519_free(&key);
  20619. #endif
  20620. return EXPECT_RESULT();
  20621. } /* END test_wc_curve25519_import*/
  20622. /*
  20623. * Testing test_wc_curve25519_export_private_raw_ex
  20624. */
  20625. static int test_wc_curve25519_export_private_raw_ex(void)
  20626. {
  20627. EXPECT_DECLS;
  20628. #if defined(HAVE_CURVE25519)
  20629. curve25519_key key;
  20630. byte out[CURVE25519_KEYSIZE];
  20631. word32 outLen = sizeof(out);
  20632. int endian = EC25519_BIG_ENDIAN;
  20633. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20634. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  20635. 0);
  20636. /* test bad cases*/
  20637. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian),
  20638. BAD_FUNC_ARG);
  20639. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian),
  20640. BAD_FUNC_ARG);
  20641. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, NULL, &outLen,
  20642. endian), BAD_FUNC_ARG);
  20643. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, NULL, endian),
  20644. BAD_FUNC_ARG);
  20645. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  20646. EC25519_LITTLE_ENDIAN), 0);
  20647. outLen = outLen - 2;
  20648. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  20649. ECC_BAD_ARG_E);
  20650. wc_curve25519_free(&key);
  20651. #endif
  20652. return EXPECT_RESULT();
  20653. } /* END test_wc_curve25519_export_private_raw_ex*/
  20654. /*
  20655. * Testing wc_ed448_make_key().
  20656. */
  20657. static int test_wc_ed448_make_key(void)
  20658. {
  20659. EXPECT_DECLS;
  20660. #if defined(HAVE_ED448)
  20661. ed448_key key;
  20662. WC_RNG rng;
  20663. unsigned char pubkey[ED448_PUB_KEY_SIZE];
  20664. XMEMSET(&key, 0, sizeof(ed448_key));
  20665. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20666. ExpectIntEQ(wc_ed448_init(&key), 0);
  20667. ExpectIntEQ(wc_InitRng(&rng), 0);
  20668. ExpectIntEQ(wc_ed448_make_public(&key, pubkey, sizeof(pubkey)),
  20669. ECC_PRIV_KEY_E);
  20670. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20671. /* Test bad args. */
  20672. ExpectIntEQ(wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key), BAD_FUNC_ARG);
  20673. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL), BAD_FUNC_ARG);
  20674. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key),
  20675. BAD_FUNC_ARG);
  20676. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key),
  20677. BAD_FUNC_ARG);
  20678. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20679. wc_ed448_free(&key);
  20680. #endif
  20681. return EXPECT_RESULT();
  20682. } /* END test_wc_ed448_make_key */
  20683. /*
  20684. * Testing wc_ed448_init()
  20685. */
  20686. static int test_wc_ed448_init(void)
  20687. {
  20688. EXPECT_DECLS;
  20689. #if defined(HAVE_ED448)
  20690. ed448_key key;
  20691. XMEMSET(&key, 0, sizeof(ed448_key));
  20692. ExpectIntEQ(wc_ed448_init(&key), 0);
  20693. /* Test bad args. */
  20694. ExpectIntEQ(wc_ed448_init(NULL), BAD_FUNC_ARG);
  20695. wc_ed448_free(&key);
  20696. #endif
  20697. return EXPECT_RESULT();
  20698. } /* END test_wc_ed448_init */
  20699. /*
  20700. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  20701. */
  20702. static int test_wc_ed448_sign_msg(void)
  20703. {
  20704. EXPECT_DECLS;
  20705. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  20706. ed448_key key;
  20707. WC_RNG rng;
  20708. byte msg[] = "Everybody gets Friday off.\n";
  20709. byte sig[ED448_SIG_SIZE];
  20710. word32 msglen = sizeof(msg);
  20711. word32 siglen = sizeof(sig);
  20712. word32 badSigLen = sizeof(sig) - 1;
  20713. #ifdef HAVE_ED448_VERIFY
  20714. int verify_ok = 0; /*1 = Verify success.*/
  20715. #endif
  20716. /* Initialize stack variables. */
  20717. XMEMSET(&key, 0, sizeof(ed448_key));
  20718. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20719. XMEMSET(sig, 0, siglen);
  20720. /* Initialize key. */
  20721. ExpectIntEQ(wc_ed448_init(&key), 0);
  20722. ExpectIntEQ(wc_InitRng(&rng), 0);
  20723. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20724. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0), 0);
  20725. ExpectIntEQ(siglen, ED448_SIG_SIZE);
  20726. /* Test bad args. */
  20727. ExpectIntEQ(wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0),
  20728. BAD_FUNC_ARG);
  20729. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0),
  20730. BAD_FUNC_ARG);
  20731. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0),
  20732. BAD_FUNC_ARG);
  20733. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0),
  20734. BAD_FUNC_ARG);
  20735. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key, NULL, 0),
  20736. BUFFER_E);
  20737. ExpectIntEQ(badSigLen, ED448_SIG_SIZE);
  20738. badSigLen -= 1;
  20739. #ifdef HAVE_ED448_VERIFY
  20740. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key,
  20741. NULL, 0), 0);
  20742. ExpectIntEQ(verify_ok, 1);
  20743. /* Test bad args. */
  20744. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg, msglen, &verify_ok,
  20745. &key, NULL, 0), BAD_FUNC_ARG);
  20746. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg, msglen, &verify_ok,
  20747. &key, NULL, 0), BAD_FUNC_ARG);
  20748. ExpectIntEQ(wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  20749. &key, NULL, 0), BAD_FUNC_ARG);
  20750. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, NULL, msglen, &verify_ok,
  20751. &key, NULL, 0), BAD_FUNC_ARG);
  20752. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, NULL,
  20753. &key, NULL, 0), BAD_FUNC_ARG);
  20754. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  20755. NULL, NULL, 0), BAD_FUNC_ARG);
  20756. ExpectIntEQ(wc_ed448_verify_msg(sig, badSigLen, msg, msglen, &verify_ok,
  20757. &key, NULL, 0), BAD_FUNC_ARG);
  20758. #endif /* Verify. */
  20759. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20760. wc_ed448_free(&key);
  20761. #endif
  20762. return EXPECT_RESULT();
  20763. } /* END test_wc_ed448_sign_msg */
  20764. /*
  20765. * Testing wc_ed448_import_public()
  20766. */
  20767. static int test_wc_ed448_import_public(void)
  20768. {
  20769. EXPECT_DECLS;
  20770. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20771. ed448_key pubKey;
  20772. WC_RNG rng;
  20773. const byte in[] =
  20774. "Ed448PublicKeyUnitTest.................................\n";
  20775. word32 inlen = sizeof(in);
  20776. XMEMSET(&pubKey, 0, sizeof(ed448_key));
  20777. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20778. ExpectIntEQ(wc_ed448_init(&pubKey), 0);
  20779. ExpectIntEQ(wc_InitRng(&rng), 0);
  20780. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey), 0);
  20781. ExpectIntEQ(wc_ed448_import_public_ex(in, inlen, &pubKey, 1), 0);
  20782. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  20783. /* Test bad args. */
  20784. ExpectIntEQ(wc_ed448_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  20785. ExpectIntEQ(wc_ed448_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  20786. ExpectIntEQ(wc_ed448_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  20787. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20788. wc_ed448_free(&pubKey);
  20789. #endif
  20790. return EXPECT_RESULT();
  20791. } /* END wc_ed448_import_public */
  20792. /*
  20793. * Testing wc_ed448_import_private_key()
  20794. */
  20795. static int test_wc_ed448_import_private_key(void)
  20796. {
  20797. EXPECT_DECLS;
  20798. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20799. ed448_key key;
  20800. WC_RNG rng;
  20801. const byte privKey[] =
  20802. "Ed448PrivateKeyUnitTest................................\n";
  20803. const byte pubKey[] =
  20804. "Ed448PublicKeyUnitTest.................................\n";
  20805. word32 privKeySz = sizeof(privKey);
  20806. word32 pubKeySz = sizeof(pubKey);
  20807. #ifdef HAVE_ED448_KEY_EXPORT
  20808. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  20809. word32 bothKeysSz = sizeof(bothKeys);
  20810. #endif
  20811. XMEMSET(&key, 0, sizeof(ed448_key));
  20812. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20813. ExpectIntEQ(wc_ed448_init(&key), 0);
  20814. ExpectIntEQ(wc_InitRng(&rng), 0);
  20815. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20816. ExpectIntEQ(wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  20817. pubKeySz, &key, 1), 0);
  20818. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20819. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20820. #ifdef HAVE_ED448_KEY_EXPORT
  20821. PRIVATE_KEY_UNLOCK();
  20822. ExpectIntEQ(wc_ed448_export_private(&key, bothKeys, &bothKeysSz), 0);
  20823. PRIVATE_KEY_LOCK();
  20824. ExpectIntEQ(wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  20825. &key, 1), 0);
  20826. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20827. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20828. #endif
  20829. /* Test bad args. */
  20830. ExpectIntEQ(wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  20831. &key), BAD_FUNC_ARG);
  20832. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, pubKeySz,
  20833. &key), BAD_FUNC_ARG);
  20834. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20835. pubKeySz, NULL), BAD_FUNC_ARG);
  20836. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  20837. pubKeySz, &key), BAD_FUNC_ARG);
  20838. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20839. pubKeySz - 1, &key), BAD_FUNC_ARG);
  20840. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, 0, &key),
  20841. BAD_FUNC_ARG);
  20842. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20843. wc_ed448_free(&key);
  20844. #endif
  20845. return EXPECT_RESULT();
  20846. } /* END test_wc_ed448_import_private_key */
  20847. /*
  20848. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  20849. */
  20850. static int test_wc_ed448_export(void)
  20851. {
  20852. EXPECT_DECLS;
  20853. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20854. ed448_key key;
  20855. WC_RNG rng;
  20856. byte priv[ED448_PRV_KEY_SIZE];
  20857. byte pub[ED448_PUB_KEY_SIZE];
  20858. word32 privSz = sizeof(priv);
  20859. word32 pubSz = sizeof(pub);
  20860. XMEMSET(&key, 0, sizeof(ed448_key));
  20861. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20862. ExpectIntEQ(wc_ed448_init(&key), 0);
  20863. ExpectIntEQ(wc_InitRng(&rng), 0);
  20864. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20865. ExpectIntEQ(wc_ed448_export_public(&key, pub, &pubSz), 0);
  20866. ExpectIntEQ(pubSz, ED448_KEY_SIZE);
  20867. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  20868. /* Test bad args. */
  20869. ExpectIntEQ(wc_ed448_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  20870. ExpectIntEQ(wc_ed448_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  20871. ExpectIntEQ(wc_ed448_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  20872. PRIVATE_KEY_UNLOCK();
  20873. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, &privSz), 0);
  20874. ExpectIntEQ(privSz, ED448_KEY_SIZE);
  20875. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  20876. /* Test bad args. */
  20877. ExpectIntEQ(wc_ed448_export_private_only(NULL, priv, &privSz),
  20878. BAD_FUNC_ARG);
  20879. ExpectIntEQ(wc_ed448_export_private_only(&key, NULL, &privSz),
  20880. BAD_FUNC_ARG);
  20881. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, NULL), BAD_FUNC_ARG);
  20882. PRIVATE_KEY_LOCK();
  20883. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20884. wc_ed448_free(&key);
  20885. #endif
  20886. return EXPECT_RESULT();
  20887. } /* END test_wc_ed448_export */
  20888. /*
  20889. * Testing wc_ed448_size()
  20890. */
  20891. static int test_wc_ed448_size(void)
  20892. {
  20893. EXPECT_DECLS;
  20894. #if defined(HAVE_ED448)
  20895. ed448_key key;
  20896. WC_RNG rng;
  20897. XMEMSET(&key, 0, sizeof(ed448_key));
  20898. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20899. ExpectIntEQ(wc_ed448_init(&key), 0);
  20900. ExpectIntEQ(wc_InitRng(&rng), 0);
  20901. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20902. ExpectIntEQ(wc_ed448_size(&key), ED448_KEY_SIZE);
  20903. /* Test bad args. */
  20904. ExpectIntEQ(wc_ed448_size(NULL), BAD_FUNC_ARG);
  20905. ExpectIntEQ(wc_ed448_sig_size(&key), ED448_SIG_SIZE);
  20906. /* Test bad args. */
  20907. ExpectIntEQ(wc_ed448_sig_size(NULL), BAD_FUNC_ARG);
  20908. ExpectIntEQ(wc_ed448_pub_size(&key), ED448_PUB_KEY_SIZE);
  20909. /* Test bad args. */
  20910. ExpectIntEQ(wc_ed448_pub_size(NULL), BAD_FUNC_ARG);
  20911. ExpectIntEQ(wc_ed448_priv_size(&key), ED448_PRV_KEY_SIZE);
  20912. /* Test bad args. */
  20913. ExpectIntEQ(wc_ed448_priv_size(NULL), BAD_FUNC_ARG);
  20914. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20915. wc_ed448_free(&key);
  20916. #endif
  20917. return EXPECT_RESULT();
  20918. } /* END test_wc_ed448_size */
  20919. /*
  20920. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  20921. */
  20922. static int test_wc_ed448_exportKey(void)
  20923. {
  20924. EXPECT_DECLS;
  20925. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20926. ed448_key key;
  20927. WC_RNG rng;
  20928. byte priv[ED448_PRV_KEY_SIZE];
  20929. byte pub[ED448_PUB_KEY_SIZE];
  20930. byte privOnly[ED448_PRV_KEY_SIZE];
  20931. word32 privSz = sizeof(priv);
  20932. word32 pubSz = sizeof(pub);
  20933. word32 privOnlySz = sizeof(privOnly);
  20934. XMEMSET(&key, 0, sizeof(ed448_key));
  20935. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20936. ExpectIntEQ(wc_ed448_init(&key), 0);
  20937. ExpectIntEQ(wc_InitRng(&rng), 0);
  20938. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20939. PRIVATE_KEY_UNLOCK();
  20940. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, &privOnlySz), 0);
  20941. /* Test bad args. */
  20942. ExpectIntEQ(wc_ed448_export_private(NULL, privOnly, &privOnlySz),
  20943. BAD_FUNC_ARG);
  20944. ExpectIntEQ(wc_ed448_export_private(&key, NULL, &privOnlySz), BAD_FUNC_ARG);
  20945. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  20946. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  20947. /* Test bad args. */
  20948. ExpectIntEQ(wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz),
  20949. BAD_FUNC_ARG);
  20950. ExpectIntEQ(wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz),
  20951. BAD_FUNC_ARG);
  20952. ExpectIntEQ(wc_ed448_export_key(&key, priv, NULL, pub, &pubSz),
  20953. BAD_FUNC_ARG);
  20954. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz),
  20955. BAD_FUNC_ARG);
  20956. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, NULL),
  20957. BAD_FUNC_ARG);
  20958. PRIVATE_KEY_LOCK();
  20959. /* Cross check output. */
  20960. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  20961. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20962. wc_ed448_free(&key);
  20963. #endif
  20964. return EXPECT_RESULT();
  20965. } /* END test_wc_ed448_exportKey */
  20966. /*
  20967. * Testing wc_Ed448PublicKeyToDer
  20968. */
  20969. static int test_wc_Ed448PublicKeyToDer(void)
  20970. {
  20971. EXPECT_DECLS;
  20972. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  20973. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20974. ed448_key key;
  20975. byte derBuf[1024];
  20976. XMEMSET(&key, 0, sizeof(ed448_key));
  20977. /* Test bad args */
  20978. ExpectIntEQ(wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  20979. ExpectIntEQ(wc_ed448_init(&key), 0);
  20980. ExpectIntEQ(wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  20981. wc_ed448_free(&key);
  20982. /* Test good args */
  20983. if (EXPECT_SUCCESS()) {
  20984. WC_RNG rng;
  20985. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20986. ExpectIntEQ(wc_ed448_init(&key), 0);
  20987. ExpectIntEQ(wc_InitRng(&rng), 0);
  20988. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20989. ExpectIntGT(wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  20990. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20991. wc_ed448_free(&key);
  20992. }
  20993. #endif
  20994. return EXPECT_RESULT();
  20995. } /* END testing wc_Ed448PublicKeyToDer */
  20996. /*
  20997. * Testing wc_curve448_init and wc_curve448_free.
  20998. */
  20999. static int test_wc_curve448_init(void)
  21000. {
  21001. EXPECT_DECLS;
  21002. #if defined(HAVE_CURVE448)
  21003. curve448_key key;
  21004. /* Test bad args for wc_curve448_init */
  21005. ExpectIntEQ(wc_curve448_init(&key), 0);
  21006. /* Test bad args for wc_curve448_init */
  21007. ExpectIntEQ(wc_curve448_init(NULL), BAD_FUNC_ARG);
  21008. /* Test good args for wc_curve_448_free */
  21009. wc_curve448_free(&key);
  21010. /* Test bad args for wc_curve448_free */
  21011. wc_curve448_free(NULL);
  21012. #endif
  21013. return EXPECT_RESULT();
  21014. } /* END test_wc_curve448_init and wc_curve_448_free*/
  21015. /*
  21016. * Testing wc_curve448_make_key
  21017. */
  21018. static int test_wc_curve448_make_key(void)
  21019. {
  21020. EXPECT_DECLS;
  21021. #if defined(HAVE_CURVE448)
  21022. curve448_key key;
  21023. WC_RNG rng;
  21024. int keysize;
  21025. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21026. ExpectIntEQ(wc_curve448_init(&key), 0);
  21027. ExpectIntEQ(wc_InitRng(&rng), 0);
  21028. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21029. ExpectIntEQ(keysize = wc_curve448_size(&key), CURVE448_KEY_SIZE);
  21030. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, &key), 0);
  21031. /* test bad cases */
  21032. ExpectIntEQ(wc_curve448_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  21033. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  21034. ExpectIntEQ(wc_curve448_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  21035. ExpectIntEQ(wc_curve448_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  21036. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21037. wc_curve448_free(&key);
  21038. #endif
  21039. return EXPECT_RESULT();
  21040. } /* END test_wc_curve448_make_key*/
  21041. /*
  21042. * Testing test_wc_curve448_shared_secret_ex
  21043. */
  21044. static int test_wc_curve448_shared_secret_ex(void)
  21045. {
  21046. EXPECT_DECLS;
  21047. #if defined(HAVE_CURVE448)
  21048. curve448_key private_key;
  21049. curve448_key public_key;
  21050. WC_RNG rng;
  21051. byte out[CURVE448_KEY_SIZE];
  21052. word32 outLen = sizeof(out);
  21053. int endian = EC448_BIG_ENDIAN;
  21054. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21055. ExpectIntEQ(wc_curve448_init(&private_key), 0);
  21056. ExpectIntEQ(wc_InitRng(&rng), 0);
  21057. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key), 0);
  21058. ExpectIntEQ(wc_curve448_init(&public_key), 0);
  21059. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key), 0);
  21060. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21061. &outLen, endian), 0);
  21062. /* test bad cases */
  21063. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  21064. BAD_FUNC_ARG);
  21065. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, &public_key, out, &outLen,
  21066. endian), BAD_FUNC_ARG);
  21067. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, NULL, out, &outLen,
  21068. endian), BAD_FUNC_ARG);
  21069. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  21070. &outLen, endian), BAD_FUNC_ARG);
  21071. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21072. NULL, endian), BAD_FUNC_ARG);
  21073. outLen = outLen - 2;
  21074. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21075. &outLen, endian), BAD_FUNC_ARG);
  21076. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21077. wc_curve448_free(&private_key);
  21078. wc_curve448_free(&public_key);
  21079. #endif
  21080. return EXPECT_RESULT();
  21081. } /* END test_wc_curve448_shared_secret_ex*/
  21082. /*
  21083. * Testing test_wc_curve448_export_public_ex
  21084. */
  21085. static int test_wc_curve448_export_public_ex(void)
  21086. {
  21087. EXPECT_DECLS;
  21088. #if defined(HAVE_CURVE448)
  21089. WC_RNG rng;
  21090. curve448_key key;
  21091. byte out[CURVE448_KEY_SIZE];
  21092. word32 outLen = sizeof(out);
  21093. int endian = EC448_BIG_ENDIAN;
  21094. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21095. ExpectIntEQ(wc_curve448_init(&key), 0);
  21096. ExpectIntEQ(wc_InitRng(&rng), 0);
  21097. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21098. ExpectIntEQ(wc_curve448_export_public(&key, out, &outLen), 0);
  21099. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian), 0);
  21100. /* test bad cases*/
  21101. ExpectIntEQ(wc_curve448_export_public_ex(NULL, NULL, NULL, endian),
  21102. BAD_FUNC_ARG);
  21103. ExpectIntEQ(wc_curve448_export_public_ex(NULL, out, &outLen, endian),
  21104. BAD_FUNC_ARG);
  21105. ExpectIntEQ(wc_curve448_export_public_ex(&key, NULL, &outLen, endian),
  21106. BAD_FUNC_ARG);
  21107. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, NULL, endian),
  21108. BAD_FUNC_ARG);
  21109. outLen = outLen - 2;
  21110. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian),
  21111. ECC_BAD_ARG_E);
  21112. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21113. wc_curve448_free(&key);
  21114. #endif
  21115. return EXPECT_RESULT();
  21116. } /* END test_wc_curve448_export_public_ex*/
  21117. /*
  21118. * Testing test_wc_curve448_export_private_raw_ex
  21119. */
  21120. static int test_wc_curve448_export_private_raw_ex(void)
  21121. {
  21122. EXPECT_DECLS;
  21123. #if defined(HAVE_CURVE448)
  21124. curve448_key key;
  21125. byte out[CURVE448_KEY_SIZE];
  21126. word32 outLen = sizeof(out);
  21127. int endian = EC448_BIG_ENDIAN;
  21128. ExpectIntEQ(wc_curve448_init(&key), 0);
  21129. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  21130. 0);
  21131. /* test bad cases*/
  21132. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian),
  21133. BAD_FUNC_ARG);
  21134. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian),
  21135. BAD_FUNC_ARG);
  21136. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian),
  21137. BAD_FUNC_ARG);
  21138. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, NULL, endian),
  21139. BAD_FUNC_ARG);
  21140. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen,
  21141. EC448_LITTLE_ENDIAN), 0);
  21142. outLen = outLen - 2;
  21143. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  21144. ECC_BAD_ARG_E);
  21145. wc_curve448_free(&key);
  21146. #endif
  21147. return EXPECT_RESULT();
  21148. } /* END test_wc_curve448_export_private_raw_ex*/
  21149. /*
  21150. * Testing test_wc_curve448_import_private_raw_ex
  21151. */
  21152. static int test_wc_curve448_import_private_raw_ex(void)
  21153. {
  21154. EXPECT_DECLS;
  21155. #if defined(HAVE_CURVE448)
  21156. curve448_key key;
  21157. WC_RNG rng;
  21158. byte priv[CURVE448_KEY_SIZE];
  21159. byte pub[CURVE448_KEY_SIZE];
  21160. word32 privSz = sizeof(priv);
  21161. word32 pubSz = sizeof(pub);
  21162. int endian = EC448_BIG_ENDIAN;
  21163. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21164. ExpectIntEQ(wc_curve448_init(&key), 0);
  21165. ExpectIntEQ(wc_InitRng(&rng), 0);
  21166. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21167. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21168. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  21169. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21170. &key, endian), 0);
  21171. /* test bad cases */
  21172. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0),
  21173. BAD_FUNC_ARG);
  21174. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  21175. &key, endian), BAD_FUNC_ARG);
  21176. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  21177. &key, endian), BAD_FUNC_ARG);
  21178. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21179. NULL, endian), BAD_FUNC_ARG);
  21180. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  21181. &key, endian), ECC_BAD_ARG_E);
  21182. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  21183. &key, endian), ECC_BAD_ARG_E);
  21184. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21185. &key, EC448_LITTLE_ENDIAN), 0);
  21186. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21187. wc_curve448_free(&key);
  21188. #endif
  21189. return EXPECT_RESULT();
  21190. } /* END test_wc_curve448_import_private_raw_ex*/
  21191. /*
  21192. * Testing test_curve448_export_key_raw
  21193. */
  21194. static int test_wc_curve448_export_key_raw(void)
  21195. {
  21196. EXPECT_DECLS;
  21197. #if defined(HAVE_CURVE448)
  21198. curve448_key key;
  21199. WC_RNG rng;
  21200. byte priv[CURVE448_KEY_SIZE];
  21201. byte pub[CURVE448_KEY_SIZE];
  21202. word32 privSz = sizeof(priv);
  21203. word32 pubSz = sizeof(pub);
  21204. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21205. ExpectIntEQ(wc_curve448_init(&key), 0);
  21206. ExpectIntEQ(wc_InitRng(&rng), 0);
  21207. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21208. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21209. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  21210. ExpectIntEQ(wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz),
  21211. 0);
  21212. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21213. wc_curve448_free(&key);
  21214. #endif
  21215. return EXPECT_RESULT();
  21216. } /* END test_wc_curve448_import_private_raw_ex*/
  21217. /*
  21218. * Testing test_wc_curve448_import_private
  21219. */
  21220. static int test_wc_curve448_import_private(void)
  21221. {
  21222. EXPECT_DECLS;
  21223. #if defined(HAVE_CURVE448)
  21224. curve448_key key;
  21225. WC_RNG rng;
  21226. byte priv[CURVE448_KEY_SIZE];
  21227. word32 privSz = sizeof(priv);
  21228. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21229. ExpectIntEQ(wc_curve448_init(&key), 0);
  21230. ExpectIntEQ(wc_InitRng(&rng), 0);
  21231. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21232. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21233. ExpectIntEQ(wc_curve448_import_private(priv, privSz, &key), 0);
  21234. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21235. wc_curve448_free(&key);
  21236. #endif
  21237. return EXPECT_RESULT();
  21238. } /* END test_wc_curve448_import*/
  21239. /*
  21240. * Testing test_wc_curve448_size.
  21241. */
  21242. static int test_wc_curve448_size(void)
  21243. {
  21244. EXPECT_DECLS;
  21245. #if defined(HAVE_CURVE448)
  21246. curve448_key key;
  21247. ExpectIntEQ(wc_curve448_init(&key), 0);
  21248. /* Test good args for wc_curve448_size */
  21249. ExpectIntEQ(wc_curve448_size(&key), CURVE448_KEY_SIZE);
  21250. /* Test bad args for wc_curve448_size */
  21251. ExpectIntEQ(wc_curve448_size(NULL), 0);
  21252. wc_curve448_free(&key);
  21253. #endif
  21254. return EXPECT_RESULT();
  21255. } /* END test_wc_curve448_size*/
  21256. /*
  21257. * Testing wc_ecc_make_key.
  21258. */
  21259. static int test_wc_ecc_make_key(void)
  21260. {
  21261. EXPECT_DECLS;
  21262. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21263. ecc_key key;
  21264. WC_RNG rng;
  21265. int ret;
  21266. XMEMSET(&key, 0, sizeof(ecc_key));
  21267. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21268. ExpectIntEQ(wc_ecc_init(&key), 0);
  21269. ExpectIntEQ(wc_InitRng(&rng), 0);
  21270. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21271. #if defined(WOLFSSL_ASYNC_CRYPT)
  21272. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21273. #endif
  21274. ExpectIntEQ(ret, 0);
  21275. /* Pass in bad args. */
  21276. ExpectIntEQ(wc_ecc_make_key(NULL, KEY14, &key), BAD_FUNC_ARG);
  21277. ExpectIntEQ(wc_ecc_make_key(&rng, KEY14, NULL), BAD_FUNC_ARG);
  21278. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21279. wc_ecc_free(&key);
  21280. #ifdef FP_ECC
  21281. wc_ecc_fp_free();
  21282. #endif
  21283. #endif
  21284. return EXPECT_RESULT();
  21285. } /* END test_wc_ecc_make_key */
  21286. /*
  21287. * Testing wc_ecc_init()
  21288. */
  21289. static int test_wc_ecc_init(void)
  21290. {
  21291. EXPECT_DECLS;
  21292. #ifdef HAVE_ECC
  21293. ecc_key key;
  21294. XMEMSET(&key, 0, sizeof(ecc_key));
  21295. ExpectIntEQ(wc_ecc_init(&key), 0);
  21296. /* Pass in bad args. */
  21297. ExpectIntEQ(wc_ecc_init(NULL), BAD_FUNC_ARG);
  21298. wc_ecc_free(&key);
  21299. #endif
  21300. return EXPECT_RESULT();
  21301. } /* END test_wc_ecc_init */
  21302. /*
  21303. * Testing wc_ecc_check_key()
  21304. */
  21305. static int test_wc_ecc_check_key(void)
  21306. {
  21307. EXPECT_DECLS;
  21308. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21309. ecc_key key;
  21310. WC_RNG rng;
  21311. int ret;
  21312. XMEMSET(&rng, 0, sizeof(rng));
  21313. XMEMSET(&key, 0, sizeof(key));
  21314. ExpectIntEQ(wc_ecc_init(&key), 0);
  21315. ExpectIntEQ(wc_InitRng(&rng), 0);
  21316. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21317. #if defined(WOLFSSL_ASYNC_CRYPT)
  21318. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21319. #endif
  21320. ExpectIntEQ(ret, 0);
  21321. ExpectIntEQ(wc_ecc_check_key(&key), 0);
  21322. /* Pass in bad args. */
  21323. ExpectIntEQ(wc_ecc_check_key(NULL), BAD_FUNC_ARG);
  21324. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21325. wc_ecc_free(&key);
  21326. #ifdef FP_ECC
  21327. wc_ecc_fp_free();
  21328. #endif
  21329. #endif
  21330. return EXPECT_RESULT();
  21331. } /* END test_wc_ecc_check_key */
  21332. /*
  21333. * Testing wc_ecc_get_generator()
  21334. */
  21335. static int test_wc_ecc_get_generator(void)
  21336. {
  21337. EXPECT_DECLS;
  21338. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  21339. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  21340. ecc_point* pt = NULL;
  21341. ExpectNotNull(pt = wc_ecc_new_point());
  21342. ExpectIntEQ(wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  21343. MP_OKAY);
  21344. /* Test bad args. */
  21345. /* Returns Zero for bad arg. */
  21346. ExpectIntNE(wc_ecc_get_generator(pt, -1), MP_OKAY);
  21347. ExpectIntNE(wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  21348. MP_OKAY);
  21349. /* If we ever get to 1000 curves increase this number */
  21350. ExpectIntNE(wc_ecc_get_generator(pt, 1000), MP_OKAY);
  21351. ExpectIntNE(wc_ecc_get_generator(NULL, -1), MP_OKAY);
  21352. wc_ecc_del_point(pt);
  21353. #endif
  21354. return EXPECT_RESULT();
  21355. } /* END test_wc_ecc_get_generator */
  21356. /*
  21357. * Testing wc_ecc_size()
  21358. */
  21359. static int test_wc_ecc_size(void)
  21360. {
  21361. EXPECT_DECLS;
  21362. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21363. WC_RNG rng;
  21364. ecc_key key;
  21365. int ret;
  21366. XMEMSET(&key, 0, sizeof(ecc_key));
  21367. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21368. ExpectIntEQ(wc_ecc_init(&key), 0);
  21369. ExpectIntEQ(wc_InitRng(&rng), 0);
  21370. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21371. #if defined(WOLFSSL_ASYNC_CRYPT)
  21372. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21373. #endif
  21374. ExpectIntEQ(ret, 0);
  21375. ExpectIntEQ(wc_ecc_size(&key), KEY14);
  21376. /* Test bad args. */
  21377. /* Returns Zero for bad arg. */
  21378. ExpectIntEQ(wc_ecc_size(NULL), 0);
  21379. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21380. wc_ecc_free(&key);
  21381. #endif
  21382. return EXPECT_RESULT();
  21383. } /* END test_wc_ecc_size */
  21384. static int test_wc_ecc_params(void)
  21385. {
  21386. EXPECT_DECLS;
  21387. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  21388. It was added after certifications */
  21389. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  21390. const ecc_set_type* ecc_set;
  21391. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  21392. /* Test for SECP256R1 curve */
  21393. int curve_id = ECC_SECP256R1;
  21394. int curve_idx;
  21395. ExpectIntNE(curve_idx = wc_ecc_get_curve_idx(curve_id), ECC_CURVE_INVALID);
  21396. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(curve_idx));
  21397. ExpectIntEQ(ecc_set->id, curve_id);
  21398. #endif
  21399. /* Test case when SECP256R1 is not enabled */
  21400. /* Test that we get curve params for index 0 */
  21401. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(0));
  21402. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  21403. return EXPECT_RESULT();
  21404. }
  21405. /*
  21406. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  21407. */
  21408. static int test_wc_ecc_signVerify_hash(void)
  21409. {
  21410. EXPECT_DECLS;
  21411. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  21412. ecc_key key;
  21413. WC_RNG rng;
  21414. int ret;
  21415. #ifdef HAVE_ECC_VERIFY
  21416. int verify = 0;
  21417. #endif
  21418. word32 siglen = ECC_BUFSIZE;
  21419. byte sig[ECC_BUFSIZE];
  21420. byte adjustedSig[ECC_BUFSIZE+1];
  21421. byte digest[] = TEST_STRING;
  21422. word32 digestlen = (word32)TEST_STRING_SZ;
  21423. /* Init stack var */
  21424. XMEMSET(&key, 0, sizeof(ecc_key));
  21425. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21426. XMEMSET(sig, 0, siglen);
  21427. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  21428. /* Init structs. */
  21429. ExpectIntEQ(wc_ecc_init(&key), 0);
  21430. ExpectIntEQ(wc_InitRng(&rng), 0);
  21431. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21432. #if defined(WOLFSSL_ASYNC_CRYPT)
  21433. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21434. #endif
  21435. ExpectIntEQ(ret, 0);
  21436. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key),
  21437. 0);
  21438. /* Check bad args. */
  21439. ExpectIntEQ(wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key),
  21440. ECC_BAD_ARG_E);
  21441. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, NULL, &siglen, &rng, &key),
  21442. ECC_BAD_ARG_E);
  21443. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, NULL, &rng, &key),
  21444. ECC_BAD_ARG_E);
  21445. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, NULL, &key),
  21446. ECC_BAD_ARG_E);
  21447. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, NULL),
  21448. ECC_BAD_ARG_E);
  21449. #ifdef HAVE_ECC_VERIFY
  21450. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  21451. &key), 0);
  21452. ExpectIntEQ(verify, 1);
  21453. /* test check on length of signature passed in */
  21454. XMEMCPY(adjustedSig, sig, siglen);
  21455. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  21456. #ifndef NO_STRICT_ECDSA_LEN
  21457. ExpectIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21458. &verify, &key), 0);
  21459. #else
  21460. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  21461. * is allowed */
  21462. ExpectIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21463. &verify, &key), 0);
  21464. #endif
  21465. /* Test bad args. */
  21466. ExpectIntEQ(wc_ecc_verify_hash(NULL, siglen, digest, digestlen, &verify,
  21467. &key), ECC_BAD_ARG_E);
  21468. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, NULL, digestlen, &verify, &key),
  21469. ECC_BAD_ARG_E);
  21470. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, NULL, &key),
  21471. ECC_BAD_ARG_E);
  21472. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  21473. NULL), ECC_BAD_ARG_E);
  21474. #endif /* HAVE_ECC_VERIFY */
  21475. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21476. wc_ecc_free(&key);
  21477. #ifdef FP_ECC
  21478. wc_ecc_fp_free();
  21479. #endif
  21480. #endif
  21481. return EXPECT_RESULT();
  21482. } /* END test_wc_ecc_sign_hash */
  21483. /*
  21484. * Testing wc_ecc_shared_secret()
  21485. */
  21486. static int test_wc_ecc_shared_secret(void)
  21487. {
  21488. EXPECT_DECLS;
  21489. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  21490. ecc_key key;
  21491. ecc_key pubKey;
  21492. WC_RNG rng;
  21493. #if defined(NO_ECC256)
  21494. int ret;
  21495. #endif
  21496. byte out[KEY32];
  21497. int keySz = sizeof(out);
  21498. word32 outlen = (word32)sizeof(out);
  21499. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21500. const char* qx =
  21501. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21502. const char* qy =
  21503. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21504. const char* d =
  21505. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21506. const char* curveName = "SECP256R1";
  21507. const byte expected_shared_secret[] =
  21508. {
  21509. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  21510. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  21511. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  21512. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  21513. };
  21514. #endif
  21515. PRIVATE_KEY_UNLOCK();
  21516. /* Initialize variables. */
  21517. XMEMSET(&key, 0, sizeof(ecc_key));
  21518. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  21519. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21520. XMEMSET(out, 0, keySz);
  21521. ExpectIntEQ(wc_ecc_init(&key), 0);
  21522. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  21523. ExpectIntEQ(wc_InitRng(&rng), 0);
  21524. #if !defined(NO_ECC256)
  21525. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  21526. ExpectIntEQ(wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName), 0);
  21527. #else
  21528. ret = wc_ecc_make_key(&rng, keySz, &key);
  21529. #if defined(WOLFSSL_ASYNC_CRYPT)
  21530. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21531. #endif
  21532. ExpectIntEQ(ret, 0);
  21533. ret = wc_ecc_make_key(&rng, keySz, &key);
  21534. #if defined(WOLFSSL_ASYNC_CRYPT)
  21535. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21536. #endif
  21537. ExpectIntEQ(ret, 0);
  21538. #endif
  21539. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21540. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21541. !defined(HAVE_SELFTEST)
  21542. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  21543. #endif
  21544. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen), 0);
  21545. #if !defined(NO_ECC256)
  21546. ExpectIntEQ(XMEMCMP(out, expected_shared_secret, outlen), 0);
  21547. #endif
  21548. /* Test bad args. */
  21549. ExpectIntEQ(wc_ecc_shared_secret(NULL, &pubKey, out, &outlen),
  21550. BAD_FUNC_ARG);
  21551. ExpectIntEQ(wc_ecc_shared_secret(&key, NULL, out, &outlen),
  21552. BAD_FUNC_ARG);
  21553. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen),
  21554. BAD_FUNC_ARG);
  21555. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, NULL),
  21556. BAD_FUNC_ARG);
  21557. /* Invalid length */
  21558. outlen = 1;
  21559. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen),
  21560. BUFFER_E);
  21561. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21562. wc_ecc_free(&pubKey);
  21563. wc_ecc_free(&key);
  21564. #ifdef FP_ECC
  21565. wc_ecc_fp_free();
  21566. #endif
  21567. PRIVATE_KEY_LOCK();
  21568. #endif
  21569. return EXPECT_RESULT();
  21570. } /* END tests_wc_ecc_shared_secret */
  21571. /*
  21572. * testint wc_ecc_export_x963()
  21573. */
  21574. static int test_wc_ecc_export_x963(void)
  21575. {
  21576. EXPECT_DECLS;
  21577. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21578. ecc_key key;
  21579. WC_RNG rng;
  21580. byte out[ECC_ASN963_MAX_BUF_SZ];
  21581. word32 outlen = sizeof(out);
  21582. int ret;
  21583. PRIVATE_KEY_UNLOCK();
  21584. /* Initialize variables. */
  21585. XMEMSET(&key, 0, sizeof(ecc_key));
  21586. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21587. XMEMSET(out, 0, outlen);
  21588. ExpectIntEQ(wc_ecc_init(&key), 0);
  21589. ExpectIntEQ(wc_InitRng(&rng), 0);
  21590. ret = wc_ecc_make_key(&rng, KEY20, &key);
  21591. #if defined(WOLFSSL_ASYNC_CRYPT)
  21592. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21593. #endif
  21594. ExpectIntEQ(ret, 0);
  21595. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), 0);
  21596. /* Test bad args. */
  21597. ExpectIntEQ(wc_ecc_export_x963(NULL, out, &outlen), ECC_BAD_ARG_E);
  21598. ExpectIntEQ(wc_ecc_export_x963(&key, NULL, &outlen), LENGTH_ONLY_E);
  21599. ExpectIntEQ(wc_ecc_export_x963(&key, out, NULL), ECC_BAD_ARG_E);
  21600. key.idx = -4;
  21601. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), ECC_BAD_ARG_E);
  21602. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21603. wc_ecc_free(&key);
  21604. #ifdef FP_ECC
  21605. wc_ecc_fp_free();
  21606. #endif
  21607. PRIVATE_KEY_LOCK();
  21608. #endif
  21609. return EXPECT_RESULT();
  21610. } /* END test_wc_ecc_export_x963 */
  21611. /*
  21612. * Testing wc_ecc_export_x963_ex()
  21613. * compile with --enable-compkey will use compression.
  21614. */
  21615. static int test_wc_ecc_export_x963_ex(void)
  21616. {
  21617. EXPECT_DECLS;
  21618. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21619. ecc_key key;
  21620. WC_RNG rng;
  21621. int ret;
  21622. byte out[ECC_ASN963_MAX_BUF_SZ];
  21623. word32 outlen = sizeof(out);
  21624. #ifdef HAVE_COMP_KEY
  21625. word32 badOutLen = 5;
  21626. #endif
  21627. /* Init stack variables. */
  21628. XMEMSET(&key, 0, sizeof(ecc_key));
  21629. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21630. XMEMSET(out, 0, outlen);
  21631. ExpectIntEQ(wc_ecc_init(&key), 0);
  21632. ExpectIntEQ(wc_InitRng(&rng), 0);
  21633. ret = wc_ecc_make_key(&rng, KEY64, &key);
  21634. #if defined(WOLFSSL_ASYNC_CRYPT)
  21635. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21636. #endif
  21637. ExpectIntEQ(ret, 0);
  21638. #ifdef HAVE_COMP_KEY
  21639. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), 0);
  21640. #else
  21641. ExpectIntEQ(ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP), 0);
  21642. #endif
  21643. /* Test bad args. */
  21644. #ifdef HAVE_COMP_KEY
  21645. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, COMP), BAD_FUNC_ARG);
  21646. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP), BAD_FUNC_ARG);
  21647. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, COMP), BAD_FUNC_ARG);
  21648. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  21649. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP), BUFFER_E);
  21650. #else
  21651. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP),
  21652. LENGTH_ONLY_E);
  21653. #endif
  21654. key.idx = -4;
  21655. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), ECC_BAD_ARG_E);
  21656. #else
  21657. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP),
  21658. ECC_BAD_ARG_E);
  21659. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP),
  21660. LENGTH_ONLY_E);
  21661. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, 1), NOT_COMPILED_IN);
  21662. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP),
  21663. ECC_BAD_ARG_E);
  21664. key.idx = -4;
  21665. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP),
  21666. ECC_BAD_ARG_E);
  21667. #endif
  21668. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21669. wc_ecc_free(&key);
  21670. #ifdef FP_ECC
  21671. wc_ecc_fp_free();
  21672. #endif
  21673. #endif
  21674. return EXPECT_RESULT();
  21675. } /* END test_wc_ecc_export_x963_ex */
  21676. /*
  21677. * testing wc_ecc_import_x963()
  21678. */
  21679. static int test_wc_ecc_import_x963(void)
  21680. {
  21681. EXPECT_DECLS;
  21682. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21683. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21684. ecc_key pubKey;
  21685. ecc_key key;
  21686. WC_RNG rng;
  21687. byte x963[ECC_ASN963_MAX_BUF_SZ];
  21688. word32 x963Len = (word32)sizeof(x963);
  21689. int ret;
  21690. /* Init stack variables. */
  21691. XMEMSET(&key, 0, sizeof(ecc_key));
  21692. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  21693. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21694. XMEMSET(x963, 0, x963Len);
  21695. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  21696. ExpectIntEQ(wc_ecc_init(&key), 0);
  21697. ExpectIntEQ(wc_InitRng(&rng), 0);
  21698. #if FIPS_VERSION3_GE(6,0,0)
  21699. ret = wc_ecc_make_key(&rng, KEY32, &key);
  21700. #else
  21701. ret = wc_ecc_make_key(&rng, KEY24, &key);
  21702. #endif
  21703. #if defined(WOLFSSL_ASYNC_CRYPT)
  21704. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21705. #endif
  21706. ExpectIntEQ(ret, 0);
  21707. PRIVATE_KEY_UNLOCK();
  21708. ExpectIntEQ(wc_ecc_export_x963(&key, x963, &x963Len), 0);
  21709. PRIVATE_KEY_LOCK();
  21710. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, &pubKey), 0);
  21711. /* Test bad args. */
  21712. ExpectIntEQ(wc_ecc_import_x963(NULL, x963Len, &pubKey), BAD_FUNC_ARG);
  21713. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, NULL), BAD_FUNC_ARG);
  21714. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len + 1, &pubKey), ECC_BAD_ARG_E);
  21715. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21716. wc_ecc_free(&key);
  21717. wc_ecc_free(&pubKey);
  21718. #ifdef FP_ECC
  21719. wc_ecc_fp_free();
  21720. #endif
  21721. #endif
  21722. return EXPECT_RESULT();
  21723. } /* END wc_ecc_import_x963 */
  21724. /*
  21725. * testing wc_ecc_import_private_key()
  21726. */
  21727. static int test_wc_ecc_import_private_key(void)
  21728. {
  21729. EXPECT_DECLS;
  21730. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21731. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21732. ecc_key key;
  21733. ecc_key keyImp;
  21734. WC_RNG rng;
  21735. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  21736. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  21737. word32 privKeySz = (word32)sizeof(privKey);
  21738. word32 x963KeySz = (word32)sizeof(x963Key);
  21739. int ret;
  21740. /* Init stack variables. */
  21741. XMEMSET(&key, 0, sizeof(ecc_key));
  21742. XMEMSET(&keyImp, 0, sizeof(ecc_key));
  21743. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21744. XMEMSET(privKey, 0, privKeySz);
  21745. XMEMSET(x963Key, 0, x963KeySz);
  21746. ExpectIntEQ(wc_ecc_init(&key), 0);
  21747. ExpectIntEQ(wc_ecc_init(&keyImp), 0);
  21748. ExpectIntEQ(wc_InitRng(&rng), 0);
  21749. ret = wc_ecc_make_key(&rng, KEY48, &key);
  21750. #if defined(WOLFSSL_ASYNC_CRYPT)
  21751. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21752. #endif
  21753. ExpectIntEQ(ret, 0);
  21754. PRIVATE_KEY_UNLOCK();
  21755. ExpectIntEQ(wc_ecc_export_x963(&key, x963Key, &x963KeySz), 0);
  21756. PRIVATE_KEY_LOCK();
  21757. ExpectIntEQ(wc_ecc_export_private_only(&key, privKey, &privKeySz), 0);
  21758. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21759. x963KeySz, &keyImp), 0);
  21760. /* Pass in bad args. */
  21761. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21762. x963KeySz, NULL), BAD_FUNC_ARG);
  21763. ExpectIntEQ(wc_ecc_import_private_key(NULL, privKeySz, x963Key, x963KeySz,
  21764. &keyImp), BAD_FUNC_ARG);
  21765. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21766. wc_ecc_free(&keyImp);
  21767. wc_ecc_free(&key);
  21768. #ifdef FP_ECC
  21769. wc_ecc_fp_free();
  21770. #endif
  21771. #endif
  21772. return EXPECT_RESULT();
  21773. } /* END test_wc_ecc_import_private_key */
  21774. /*
  21775. * Testing wc_ecc_export_private_only()
  21776. */
  21777. static int test_wc_ecc_export_private_only(void)
  21778. {
  21779. EXPECT_DECLS;
  21780. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21781. ecc_key key;
  21782. WC_RNG rng;
  21783. byte out[ECC_PRIV_KEY_BUF];
  21784. word32 outlen = sizeof(out);
  21785. int ret;
  21786. /* Init stack variables. */
  21787. XMEMSET(&key, 0, sizeof(ecc_key));
  21788. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21789. XMEMSET(out, 0, outlen);
  21790. ExpectIntEQ(wc_ecc_init(&key), 0);
  21791. ExpectIntEQ(wc_InitRng(&rng), 0);
  21792. ret = wc_ecc_make_key(&rng, KEY32, &key);
  21793. #if defined(WOLFSSL_ASYNC_CRYPT)
  21794. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21795. #endif
  21796. ExpectIntEQ(ret, 0);
  21797. ExpectIntEQ(wc_ecc_export_private_only(&key, out, &outlen), 0);
  21798. /* Pass in bad args. */
  21799. ExpectIntEQ(wc_ecc_export_private_only(NULL, out, &outlen), BAD_FUNC_ARG);
  21800. ExpectIntEQ(wc_ecc_export_private_only(&key, NULL, &outlen), BAD_FUNC_ARG);
  21801. ExpectIntEQ(wc_ecc_export_private_only(&key, out, NULL), BAD_FUNC_ARG);
  21802. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21803. wc_ecc_free(&key);
  21804. #ifdef FP_ECC
  21805. wc_ecc_fp_free();
  21806. #endif
  21807. #endif
  21808. return EXPECT_RESULT();
  21809. } /* END test_wc_ecc_export_private_only */
  21810. /*
  21811. * Testing wc_ecc_rs_to_sig()
  21812. */
  21813. static int test_wc_ecc_rs_to_sig(void)
  21814. {
  21815. EXPECT_DECLS;
  21816. #if defined(HAVE_ECC) && !defined(NO_ASN)
  21817. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  21818. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  21819. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  21820. const char* zeroStr = "0";
  21821. byte sig[ECC_MAX_SIG_SIZE];
  21822. word32 siglen = (word32)sizeof(sig);
  21823. /* R and S max size is the order of curve. 2^192.*/
  21824. int keySz = KEY24;
  21825. byte r[KEY24];
  21826. byte s[KEY24];
  21827. word32 rlen = (word32)sizeof(r);
  21828. word32 slen = (word32)sizeof(s);
  21829. /* Init stack variables. */
  21830. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  21831. XMEMSET(r, 0, keySz);
  21832. XMEMSET(s, 0, keySz);
  21833. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, &siglen), 0);
  21834. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen), 0);
  21835. /* Test bad args. */
  21836. ExpectIntEQ(wc_ecc_rs_to_sig(NULL, S, sig, &siglen), ECC_BAD_ARG_E);
  21837. ExpectIntEQ(wc_ecc_rs_to_sig(R, NULL, sig, &siglen), ECC_BAD_ARG_E);
  21838. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, NULL), ECC_BAD_ARG_E);
  21839. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, NULL, &siglen), ECC_BAD_ARG_E);
  21840. ExpectIntEQ(wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen), MP_ZERO_E);
  21841. ExpectIntEQ(wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen), MP_ZERO_E);
  21842. ExpectIntEQ(wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen),
  21843. ECC_BAD_ARG_E);
  21844. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen),
  21845. ECC_BAD_ARG_E);
  21846. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen),
  21847. ECC_BAD_ARG_E);
  21848. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen),
  21849. ECC_BAD_ARG_E);
  21850. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL),
  21851. ECC_BAD_ARG_E);
  21852. #endif
  21853. return EXPECT_RESULT();
  21854. } /* END test_wc_ecc_rs_to_sig */
  21855. static int test_wc_ecc_import_raw(void)
  21856. {
  21857. EXPECT_DECLS;
  21858. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21859. ecc_key key;
  21860. const char* qx =
  21861. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21862. const char* qy =
  21863. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21864. const char* d =
  21865. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21866. const char* curveName = "SECP256R1";
  21867. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21868. const char* kNullStr = "";
  21869. int ret;
  21870. #endif
  21871. XMEMSET(&key, 0, sizeof(ecc_key));
  21872. ExpectIntEQ(wc_ecc_init(&key), 0);
  21873. /* Test good import */
  21874. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  21875. /* Test bad args. */
  21876. ExpectIntEQ(wc_ecc_import_raw(NULL, qx, qy, d, curveName), BAD_FUNC_ARG);
  21877. ExpectIntEQ(wc_ecc_import_raw(&key, NULL, qy, d, curveName), BAD_FUNC_ARG);
  21878. ExpectIntEQ(wc_ecc_import_raw(&key, qx, NULL, d, curveName), BAD_FUNC_ARG);
  21879. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, NULL), BAD_FUNC_ARG);
  21880. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21881. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21882. wc_ecc_free(&key);
  21883. #endif
  21884. ExpectIntLT(ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr,
  21885. curveName), 0);
  21886. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  21887. #endif
  21888. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  21889. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21890. wc_ecc_free(&key);
  21891. #endif
  21892. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21893. ExpectIntLT(ret = wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  21894. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  21895. #else
  21896. ExpectIntEQ(wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  21897. #endif
  21898. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21899. wc_ecc_free(&key);
  21900. #endif
  21901. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21902. ExpectIntLT(ret = wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  21903. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  21904. #else
  21905. ExpectIntEQ(wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  21906. #endif
  21907. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21908. wc_ecc_free(&key);
  21909. #endif
  21910. ExpectIntEQ(wc_ecc_import_raw(&key, "0", "0", d, curveName), ECC_INF_E);
  21911. #endif
  21912. wc_ecc_free(&key);
  21913. #endif
  21914. return EXPECT_RESULT();
  21915. } /* END test_wc_ecc_import_raw */
  21916. static int test_wc_ecc_import_unsigned(void)
  21917. {
  21918. EXPECT_DECLS;
  21919. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  21920. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  21921. HAVE_FIPS_VERSION >= 2))
  21922. ecc_key key;
  21923. const byte qx[] = {
  21924. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  21925. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  21926. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  21927. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  21928. };
  21929. const byte qy[] = {
  21930. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  21931. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  21932. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  21933. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  21934. };
  21935. const byte d[] = {
  21936. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  21937. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  21938. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  21939. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  21940. };
  21941. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21942. const byte nullBytes[32] = {0};
  21943. int ret;
  21944. #endif
  21945. int curveId = ECC_SECP256R1;
  21946. XMEMSET(&key, 0, sizeof(ecc_key));
  21947. ExpectIntEQ(wc_ecc_init(&key), 0);
  21948. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  21949. curveId), 0);
  21950. /* Test bad args. */
  21951. ExpectIntEQ(wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  21952. curveId), BAD_FUNC_ARG);
  21953. ExpectIntEQ(wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  21954. curveId), BAD_FUNC_ARG);
  21955. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  21956. curveId), BAD_FUNC_ARG);
  21957. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  21958. ECC_CURVE_INVALID), BAD_FUNC_ARG);
  21959. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21960. ExpectIntLT(ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  21961. (byte*)nullBytes, (byte*)nullBytes, curveId), 0);
  21962. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  21963. #endif
  21964. wc_ecc_free(&key);
  21965. #endif
  21966. return EXPECT_RESULT();
  21967. } /* END test_wc_ecc_import_unsigned */
  21968. /*
  21969. * Testing wc_ecc_sig_size()
  21970. */
  21971. static int test_wc_ecc_sig_size(void)
  21972. {
  21973. EXPECT_DECLS;
  21974. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21975. ecc_key key;
  21976. WC_RNG rng;
  21977. int keySz = KEY16;
  21978. int ret;
  21979. XMEMSET(&rng, 0, sizeof(rng));
  21980. XMEMSET(&key, 0, sizeof(key));
  21981. ExpectIntEQ(wc_ecc_init(&key), 0);
  21982. ExpectIntEQ(wc_InitRng(&rng), 0);
  21983. ret = wc_ecc_make_key(&rng, keySz, &key);
  21984. #if defined(WOLFSSL_ASYNC_CRYPT)
  21985. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21986. #endif
  21987. ExpectIntEQ(ret, 0);
  21988. ExpectIntLE(wc_ecc_sig_size(&key),
  21989. (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ));
  21990. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21991. wc_ecc_free(&key);
  21992. #endif
  21993. return EXPECT_RESULT();
  21994. } /* END test_wc_ecc_sig_size */
  21995. /*
  21996. * Testing wc_ecc_ctx_new()
  21997. */
  21998. static int test_wc_ecc_ctx_new(void)
  21999. {
  22000. EXPECT_DECLS;
  22001. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22002. WC_RNG rng;
  22003. ecEncCtx* cli = NULL;
  22004. ecEncCtx* srv = NULL;
  22005. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22006. ExpectIntEQ(wc_InitRng(&rng), 0);
  22007. ExpectNotNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22008. ExpectNotNull(srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  22009. wc_ecc_ctx_free(cli);
  22010. cli = NULL;
  22011. wc_ecc_ctx_free(srv);
  22012. /* Test bad args. */
  22013. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  22014. ExpectNull(cli = wc_ecc_ctx_new(0, &rng));
  22015. ExpectNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL));
  22016. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22017. wc_ecc_ctx_free(cli);
  22018. #endif
  22019. return EXPECT_RESULT();
  22020. } /* END test_wc_ecc_ctx_new */
  22021. /*
  22022. * Tesing wc_ecc_reset()
  22023. */
  22024. static int test_wc_ecc_ctx_reset(void)
  22025. {
  22026. EXPECT_DECLS;
  22027. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22028. ecEncCtx* ctx = NULL;
  22029. WC_RNG rng;
  22030. XMEMSET(&rng, 0, sizeof(rng));
  22031. ExpectIntEQ(wc_InitRng(&rng), 0);
  22032. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22033. ExpectIntEQ(wc_ecc_ctx_reset(ctx, &rng), 0);
  22034. /* Pass in bad args. */
  22035. ExpectIntEQ(wc_ecc_ctx_reset(NULL, &rng), BAD_FUNC_ARG);
  22036. ExpectIntEQ(wc_ecc_ctx_reset(ctx, NULL), BAD_FUNC_ARG);
  22037. wc_ecc_ctx_free(ctx);
  22038. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22039. #endif
  22040. return EXPECT_RESULT();
  22041. } /* END test_wc_ecc_ctx_reset */
  22042. /*
  22043. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  22044. */
  22045. static int test_wc_ecc_ctx_set_peer_salt(void)
  22046. {
  22047. EXPECT_DECLS;
  22048. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22049. WC_RNG rng;
  22050. ecEncCtx* cliCtx = NULL;
  22051. ecEncCtx* servCtx = NULL;
  22052. const byte* cliSalt = NULL;
  22053. const byte* servSalt = NULL;
  22054. XMEMSET(&rng, 0, sizeof(rng));
  22055. ExpectIntEQ(wc_InitRng(&rng), 0);
  22056. ExpectNotNull(cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22057. ExpectNotNull(servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  22058. /* Test bad args. */
  22059. ExpectNull(cliSalt = wc_ecc_ctx_get_own_salt(NULL));
  22060. ExpectNotNull(cliSalt = wc_ecc_ctx_get_own_salt(cliCtx));
  22061. ExpectNotNull(servSalt = wc_ecc_ctx_get_own_salt(servCtx));
  22062. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, servSalt), 0);
  22063. /* Test bad args. */
  22064. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(NULL, servSalt), BAD_FUNC_ARG);
  22065. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, NULL), BAD_FUNC_ARG);
  22066. wc_ecc_ctx_free(cliCtx);
  22067. wc_ecc_ctx_free(servCtx);
  22068. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22069. #endif
  22070. return EXPECT_RESULT();
  22071. } /* END test_wc_ecc_ctx_set_peer_salt */
  22072. /*
  22073. * Testing wc_ecc_ctx_set_info()
  22074. */
  22075. static int test_wc_ecc_ctx_set_info(void)
  22076. {
  22077. EXPECT_DECLS;
  22078. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22079. ecEncCtx* ctx = NULL;
  22080. WC_RNG rng;
  22081. const char* optInfo = "Optional Test Info.";
  22082. int optInfoSz = (int)XSTRLEN(optInfo);
  22083. const char* badOptInfo = NULL;
  22084. XMEMSET(&rng, 0, sizeof(rng));
  22085. ExpectIntEQ(wc_InitRng(&rng), 0);
  22086. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22087. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz), 0);
  22088. /* Test bad args. */
  22089. ExpectIntEQ(wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz),
  22090. BAD_FUNC_ARG);
  22091. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz),
  22092. BAD_FUNC_ARG);
  22093. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1), BAD_FUNC_ARG);
  22094. wc_ecc_ctx_free(ctx);
  22095. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22096. #endif
  22097. return EXPECT_RESULT();
  22098. } /* END test_wc_ecc_ctx_set_info */
  22099. /*
  22100. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  22101. */
  22102. static int test_wc_ecc_encryptDecrypt(void)
  22103. {
  22104. EXPECT_DECLS;
  22105. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  22106. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  22107. ecc_key srvKey;
  22108. ecc_key cliKey;
  22109. ecc_key tmpKey;
  22110. WC_RNG rng;
  22111. int ret;
  22112. const char* msg = "EccBlock Size 16";
  22113. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  22114. #ifdef WOLFSSL_ECIES_OLD
  22115. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22116. #elif defined(WOLFSSL_ECIES_GEN_IV)
  22117. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  22118. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22119. #else
  22120. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) +
  22121. WC_SHA256_DIGEST_SIZE];
  22122. #endif
  22123. word32 outSz = (word32)sizeof(out);
  22124. byte plain[sizeof("EccBlock Size 16")];
  22125. word32 plainSz = (word32)sizeof(plain);
  22126. int keySz = KEY20;
  22127. /* Init stack variables. */
  22128. XMEMSET(out, 0, outSz);
  22129. XMEMSET(plain, 0, plainSz);
  22130. XMEMSET(&rng, 0, sizeof(rng));
  22131. XMEMSET(&srvKey, 0, sizeof(ecc_key));
  22132. XMEMSET(&cliKey, 0, sizeof(ecc_key));
  22133. XMEMSET(&tmpKey, 0, sizeof(ecc_key));
  22134. ExpectIntEQ(wc_InitRng(&rng), 0);
  22135. ExpectIntEQ(wc_ecc_init(&cliKey), 0);
  22136. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  22137. #if defined(WOLFSSL_ASYNC_CRYPT)
  22138. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22139. #endif
  22140. ExpectIntEQ(ret, 0);
  22141. ExpectIntEQ(wc_ecc_init(&srvKey), 0);
  22142. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  22143. #if defined(WOLFSSL_ASYNC_CRYPT)
  22144. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22145. #endif
  22146. ExpectIntEQ(ret, 0);
  22147. ExpectIntEQ(wc_ecc_init(&tmpKey), 0);
  22148. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22149. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22150. !defined(HAVE_SELFTEST)
  22151. ExpectIntEQ(wc_ecc_set_rng(&srvKey, &rng), 0);
  22152. ExpectIntEQ(wc_ecc_set_rng(&cliKey, &rng), 0);
  22153. #endif
  22154. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  22155. &outSz, NULL), 0);
  22156. /* Test bad args. */
  22157. ExpectIntEQ(wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out, &outSz,
  22158. NULL), BAD_FUNC_ARG);
  22159. ExpectIntEQ(wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out, &outSz,
  22160. NULL), BAD_FUNC_ARG);
  22161. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out, &outSz,
  22162. NULL), BAD_FUNC_ARG);
  22163. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  22164. &outSz, NULL), BAD_FUNC_ARG);
  22165. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out, NULL,
  22166. NULL), BAD_FUNC_ARG);
  22167. #ifdef WOLFSSL_ECIES_OLD
  22168. tmpKey.dp = cliKey.dp;
  22169. ExpectIntEQ(wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey), 0);
  22170. #endif
  22171. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, &plainSz,
  22172. NULL), 0);
  22173. ExpectIntEQ(wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain, &plainSz,
  22174. NULL), BAD_FUNC_ARG);
  22175. #ifdef WOLFSSL_ECIES_OLD
  22176. /* NULL parameter allowed in new implementations - public key comes from
  22177. * the message. */
  22178. ExpectIntEQ(wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain, &plainSz,
  22179. NULL), BAD_FUNC_ARG);
  22180. #endif
  22181. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain, &plainSz,
  22182. NULL), BAD_FUNC_ARG);
  22183. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL, &plainSz,
  22184. NULL), BAD_FUNC_ARG);
  22185. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, NULL, NULL),
  22186. BAD_FUNC_ARG);
  22187. ExpectIntEQ(XMEMCMP(msg, plain, msgSz), 0);
  22188. wc_ecc_free(&tmpKey);
  22189. wc_ecc_free(&srvKey);
  22190. wc_ecc_free(&cliKey);
  22191. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22192. #endif
  22193. return EXPECT_RESULT();
  22194. } /* END test_wc_ecc_encryptDecrypt */
  22195. /*
  22196. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  22197. */
  22198. static int test_wc_ecc_del_point(void)
  22199. {
  22200. EXPECT_DECLS;
  22201. #if defined(HAVE_ECC)
  22202. ecc_point* pt = NULL;
  22203. ExpectNotNull(pt = wc_ecc_new_point());
  22204. wc_ecc_del_point(pt);
  22205. #endif
  22206. return EXPECT_RESULT();
  22207. } /* END test_wc_ecc_del_point */
  22208. /*
  22209. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  22210. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  22211. * and wc_ecc_cmp_point()
  22212. */
  22213. static int test_wc_ecc_pointFns(void)
  22214. {
  22215. EXPECT_DECLS;
  22216. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  22217. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22218. !defined(WOLFSSL_ATECC608A)
  22219. ecc_key key;
  22220. WC_RNG rng;
  22221. int ret;
  22222. ecc_point* point = NULL;
  22223. ecc_point* cpypt = NULL;
  22224. int idx = 0;
  22225. int keySz = KEY32;
  22226. byte der[DER_SZ(KEY32)];
  22227. word32 derlenChk = 0;
  22228. word32 derSz = DER_SZ(KEY32);
  22229. /* Init stack variables. */
  22230. XMEMSET(der, 0, derSz);
  22231. XMEMSET(&key, 0, sizeof(ecc_key));
  22232. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22233. ExpectIntEQ(wc_InitRng(&rng), 0);
  22234. ExpectIntEQ(wc_ecc_init(&key), 0);
  22235. ret = wc_ecc_make_key(&rng, keySz, &key);
  22236. #if defined(WOLFSSL_ASYNC_CRYPT)
  22237. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22238. #endif
  22239. ExpectIntEQ(ret, 0);
  22240. ExpectNotNull(point = wc_ecc_new_point());
  22241. ExpectNotNull(cpypt = wc_ecc_new_point());
  22242. /* Export */
  22243. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, NULL,
  22244. &derlenChk), LENGTH_ONLY_E);
  22245. /* Check length value. */
  22246. ExpectIntEQ(derSz, derlenChk);
  22247. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  22248. &derSz), 0);
  22249. /* Test bad args. */
  22250. ExpectIntEQ(wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz),
  22251. ECC_BAD_ARG_E);
  22252. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz),
  22253. ECC_BAD_ARG_E);
  22254. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  22255. NULL), ECC_BAD_ARG_E);
  22256. /* Import */
  22257. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, point), 0);
  22258. ExpectIntEQ(wc_ecc_cmp_point(&key.pubkey, point), 0);
  22259. /* Test bad args. */
  22260. ExpectIntEQ( wc_ecc_import_point_der(NULL, derSz, idx, point),
  22261. ECC_BAD_ARG_E);
  22262. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, NULL), ECC_BAD_ARG_E);
  22263. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, -1, point), ECC_BAD_ARG_E);
  22264. ExpectIntEQ(wc_ecc_import_point_der(der, derSz + 1, idx, point),
  22265. ECC_BAD_ARG_E);
  22266. /* Copy */
  22267. ExpectIntEQ(wc_ecc_copy_point(point, cpypt), 0);
  22268. /* Test bad args. */
  22269. ExpectIntEQ(wc_ecc_copy_point(NULL, cpypt), ECC_BAD_ARG_E);
  22270. ExpectIntEQ(wc_ecc_copy_point(point, NULL), ECC_BAD_ARG_E);
  22271. /* Compare point */
  22272. ExpectIntEQ(wc_ecc_cmp_point(point, cpypt), 0);
  22273. /* Test bad args. */
  22274. ExpectIntEQ(wc_ecc_cmp_point(NULL, cpypt), BAD_FUNC_ARG);
  22275. ExpectIntEQ(wc_ecc_cmp_point(point, NULL), BAD_FUNC_ARG);
  22276. /* At infinity if return == 1, otherwise return == 0. */
  22277. ExpectIntEQ(wc_ecc_point_is_at_infinity(point), 0);
  22278. /* Test bad args. */
  22279. ExpectIntEQ(wc_ecc_point_is_at_infinity(NULL), BAD_FUNC_ARG);
  22280. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  22281. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  22282. #ifdef USE_ECC_B_PARAM
  22283. /* On curve if ret == 0 */
  22284. ExpectIntEQ(wc_ecc_point_is_on_curve(point, idx), 0);
  22285. /* Test bad args. */
  22286. ExpectIntEQ(wc_ecc_point_is_on_curve(NULL, idx), BAD_FUNC_ARG);
  22287. ExpectIntEQ(wc_ecc_point_is_on_curve(point, 1000), ECC_BAD_ARG_E);
  22288. #endif /* USE_ECC_B_PARAM */
  22289. #endif /* !HAVE_SELFTEST && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  22290. /* Free */
  22291. wc_ecc_del_point(point);
  22292. wc_ecc_del_point(cpypt);
  22293. wc_ecc_free(&key);
  22294. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22295. #endif
  22296. return EXPECT_RESULT();
  22297. } /* END test_wc_ecc_pointFns */
  22298. /*
  22299. * Testing wc_ecc_shared_secret_ssh()
  22300. */
  22301. static int test_wc_ecc_shared_secret_ssh(void)
  22302. {
  22303. EXPECT_DECLS;
  22304. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  22305. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22306. !defined(WOLFSSL_ATECC608A) && !defined(PLUTON_CRYPTO_ECC) && \
  22307. !defined(WOLFSSL_CRYPTOCELL)
  22308. ecc_key key;
  22309. ecc_key key2;
  22310. WC_RNG rng;
  22311. int ret;
  22312. int keySz = KEY32;
  22313. #if FIPS_VERSION3_GE(6,0,0)
  22314. int key2Sz = KEY28;
  22315. #else
  22316. int key2Sz = KEY24;
  22317. #endif
  22318. byte secret[KEY32];
  22319. word32 secretLen = keySz;
  22320. /* Init stack variables. */
  22321. XMEMSET(&key, 0, sizeof(ecc_key));
  22322. XMEMSET(&key2, 0, sizeof(ecc_key));
  22323. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22324. XMEMSET(secret, 0, secretLen);
  22325. /* Make keys */
  22326. ExpectIntEQ(wc_ecc_init(&key), 0);
  22327. ExpectIntEQ(wc_InitRng(&rng), 0);
  22328. ret = wc_ecc_make_key(&rng, keySz, &key);
  22329. #if defined(WOLFSSL_ASYNC_CRYPT)
  22330. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22331. #endif
  22332. ExpectIntEQ(ret, 0);
  22333. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22334. ExpectIntEQ(wc_ecc_init(&key2), 0);
  22335. ExpectIntEQ(wc_InitRng(&rng), 0);
  22336. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  22337. #if defined(WOLFSSL_ASYNC_CRYPT)
  22338. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  22339. #endif
  22340. ExpectIntEQ(ret, 0);
  22341. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22342. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22343. !defined(HAVE_SELFTEST)
  22344. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  22345. #endif
  22346. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  22347. &secretLen), 0);
  22348. /* Pass in bad args. */
  22349. ExpectIntEQ(wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret,
  22350. &secretLen), BAD_FUNC_ARG);
  22351. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen),
  22352. BAD_FUNC_ARG);
  22353. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen),
  22354. BAD_FUNC_ARG);
  22355. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL),
  22356. BAD_FUNC_ARG);
  22357. key.type = ECC_PUBLICKEY;
  22358. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  22359. &secretLen), ECC_BAD_ARG_E);
  22360. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22361. wc_ecc_free(&key);
  22362. wc_ecc_free(&key2);
  22363. #ifdef FP_ECC
  22364. wc_ecc_fp_free();
  22365. #endif
  22366. #endif
  22367. return EXPECT_RESULT();
  22368. } /* END test_wc_ecc_shared_secret_ssh */
  22369. /*
  22370. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  22371. */
  22372. static int test_wc_ecc_verify_hash_ex(void)
  22373. {
  22374. EXPECT_DECLS;
  22375. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  22376. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22377. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  22378. ecc_key key;
  22379. WC_RNG rng;
  22380. int ret;
  22381. mp_int r;
  22382. mp_int s;
  22383. mp_int z;
  22384. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  22385. unsigned char iHash[] = "Everyone gets Friday off.......";
  22386. unsigned char shortHash[] = TEST_STRING;
  22387. word32 hashlen = sizeof(hash);
  22388. word32 iHashLen = sizeof(iHash);
  22389. word32 shortHashLen = sizeof(shortHash);
  22390. int keySz = KEY32;
  22391. int verify_ok = 0;
  22392. XMEMSET(&key, 0, sizeof(ecc_key));
  22393. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22394. XMEMSET(&r, 0, sizeof(mp_int));
  22395. XMEMSET(&s, 0, sizeof(mp_int));
  22396. XMEMSET(&z, 0, sizeof(mp_int));
  22397. /* Initialize r, s and z. */
  22398. ExpectIntEQ(mp_init_multi(&r, &s, &z, NULL, NULL, NULL), MP_OKAY);
  22399. ExpectIntEQ(wc_ecc_init(&key), 0);
  22400. ExpectIntEQ(wc_InitRng(&rng), 0);
  22401. ret = wc_ecc_make_key(&rng, keySz, &key);
  22402. #if defined(WOLFSSL_ASYNC_CRYPT)
  22403. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22404. #endif
  22405. ExpectIntEQ(ret, 0);
  22406. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s), 0);
  22407. /* verify_ok should be 1. */
  22408. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key),
  22409. 0);
  22410. ExpectIntEQ(verify_ok, 1);
  22411. /* verify_ok should be 0 */
  22412. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen, &verify_ok,
  22413. &key), 0);
  22414. ExpectIntEQ(verify_ok, 0);
  22415. /* verify_ok should be 0. */
  22416. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22417. &verify_ok, &key), 0);
  22418. ExpectIntEQ(verify_ok, 0);
  22419. /* Test bad args. */
  22420. ExpectIntEQ(wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s),
  22421. ECC_BAD_ARG_E);
  22422. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s),
  22423. ECC_BAD_ARG_E);
  22424. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s),
  22425. ECC_BAD_ARG_E);
  22426. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s),
  22427. ECC_BAD_ARG_E);
  22428. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL),
  22429. ECC_BAD_ARG_E);
  22430. /* Test bad args. */
  22431. ExpectIntEQ(wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen,
  22432. &verify_ok, &key), ECC_BAD_ARG_E);
  22433. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  22434. &verify_ok, &key), ECC_BAD_ARG_E);
  22435. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen,
  22436. &verify_ok, &key), MP_ZERO_E);
  22437. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen,
  22438. &verify_ok, &key), MP_ZERO_E);
  22439. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen,
  22440. &verify_ok, &key), MP_ZERO_E);
  22441. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  22442. &key), ECC_BAD_ARG_E);
  22443. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen, NULL,
  22444. &key), ECC_BAD_ARG_E);
  22445. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22446. &verify_ok, NULL), ECC_BAD_ARG_E);
  22447. wc_ecc_free(&key);
  22448. mp_free(&r);
  22449. mp_free(&s);
  22450. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22451. #endif
  22452. return EXPECT_RESULT();
  22453. } /* END test_wc_ecc_verify_hash_ex */
  22454. /*
  22455. * Testing wc_ecc_mulmod()
  22456. */
  22457. static int test_wc_ecc_mulmod(void)
  22458. {
  22459. EXPECT_DECLS;
  22460. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  22461. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  22462. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  22463. ecc_key key1;
  22464. ecc_key key2;
  22465. ecc_key key3;
  22466. WC_RNG rng;
  22467. int ret;
  22468. XMEMSET(&key1, 0, sizeof(ecc_key));
  22469. XMEMSET(&key2, 0, sizeof(ecc_key));
  22470. XMEMSET(&key3, 0, sizeof(ecc_key));
  22471. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22472. ExpectIntEQ(wc_ecc_init(&key1), 0);
  22473. ExpectIntEQ(wc_ecc_init(&key2), 0);
  22474. ExpectIntEQ(wc_ecc_init(&key3), 0);
  22475. ExpectIntEQ(wc_InitRng(&rng), 0);
  22476. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  22477. #if defined(WOLFSSL_ASYNC_CRYPT)
  22478. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  22479. #endif
  22480. ExpectIntEQ(ret, 0);
  22481. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22482. ExpectIntEQ(wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy,
  22483. key1.dp->Af, ECC_SECP256R1), 0);
  22484. ExpectIntEQ(wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  22485. key1.dp->prime, ECC_SECP256R1), 0);
  22486. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  22487. &key3.pubkey, wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3),
  22488. 1), 0);
  22489. /* Test bad args. */
  22490. ExpectIntEQ(ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey,
  22491. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22492. ECC_BAD_ARG_E);
  22493. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), NULL, &key3.pubkey,
  22494. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22495. ECC_BAD_ARG_E);
  22496. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey, NULL,
  22497. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22498. ECC_BAD_ARG_E);
  22499. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  22500. &key3.pubkey, wc_ecc_key_get_priv(&key2), NULL, 1), ECC_BAD_ARG_E);
  22501. wc_ecc_free(&key1);
  22502. wc_ecc_free(&key2);
  22503. wc_ecc_free(&key3);
  22504. #ifdef FP_ECC
  22505. wc_ecc_fp_free();
  22506. #endif
  22507. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  22508. return EXPECT_RESULT();
  22509. } /* END test_wc_ecc_mulmod */
  22510. /*
  22511. * Testing wc_ecc_is_valid_idx()
  22512. */
  22513. static int test_wc_ecc_is_valid_idx(void)
  22514. {
  22515. EXPECT_DECLS;
  22516. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22517. ecc_key key;
  22518. WC_RNG rng;
  22519. int ret;
  22520. int iVal = -2;
  22521. int iVal2 = 3000;
  22522. XMEMSET(&key, 0, sizeof(ecc_key));
  22523. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22524. ExpectIntEQ(wc_ecc_init(&key), 0);
  22525. ExpectIntEQ(wc_InitRng(&rng), 0);
  22526. ret = wc_ecc_make_key(&rng, 32, &key);
  22527. #if defined(WOLFSSL_ASYNC_CRYPT)
  22528. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22529. #endif
  22530. ExpectIntEQ(ret, 0);
  22531. ExpectIntEQ(wc_ecc_is_valid_idx(key.idx), 1);
  22532. /* Test bad args. */
  22533. ExpectIntEQ(wc_ecc_is_valid_idx(iVal), 0);
  22534. ExpectIntEQ(wc_ecc_is_valid_idx(iVal2), 0);
  22535. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22536. wc_ecc_free(&key);
  22537. #ifdef FP_ECC
  22538. wc_ecc_fp_free();
  22539. #endif
  22540. #endif
  22541. return EXPECT_RESULT();
  22542. } /* END test_wc_ecc_is_valid_idx */
  22543. /*
  22544. * Testing wc_ecc_get_curve_id_from_oid()
  22545. */
  22546. static int test_wc_ecc_get_curve_id_from_oid(void)
  22547. {
  22548. EXPECT_DECLS;
  22549. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22550. !defined(HAVE_FIPS)
  22551. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  22552. word32 len = sizeof(oid);
  22553. /* Bad Cases */
  22554. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(NULL, len), BAD_FUNC_ARG);
  22555. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, 0), ECC_CURVE_INVALID);
  22556. /* Good Case */
  22557. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, len), ECC_SECP256R1);
  22558. #endif
  22559. return EXPECT_RESULT();
  22560. } /* END test_wc_ecc_get_curve_id_from_oid */
  22561. /*
  22562. * Testing wc_ecc_sig_size_calc()
  22563. */
  22564. static int test_wc_ecc_sig_size_calc(void)
  22565. {
  22566. EXPECT_DECLS;
  22567. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  22568. ecc_key key;
  22569. WC_RNG rng;
  22570. int sz = 0;
  22571. int ret;
  22572. XMEMSET(&key, 0, sizeof(ecc_key));
  22573. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22574. ExpectIntEQ(wc_ecc_init(&key), 0);
  22575. ExpectIntEQ(wc_InitRng(&rng), 0);
  22576. ret = wc_ecc_make_key(&rng, 16, &key);
  22577. #if defined(WOLFSSL_ASYNC_CRYPT)
  22578. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22579. #endif
  22580. #if FIPS_VERSION3_GE(6,0,0)
  22581. ExpectIntEQ(ret, BAD_FUNC_ARG);
  22582. #else
  22583. ExpectIntEQ(ret, 0);
  22584. #endif
  22585. #if FIPS_VERSION3_LT(6,0,0)
  22586. sz = key.dp->size;
  22587. ExpectIntGT(wc_ecc_sig_size_calc(sz), 0);
  22588. #else
  22589. (void) sz;
  22590. #endif
  22591. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22592. wc_ecc_free(&key);
  22593. #endif
  22594. return EXPECT_RESULT();
  22595. } /* END test_wc_ecc_sig_size_calc */
  22596. /*
  22597. * Testing wc_ecc_sm2_make_key()
  22598. */
  22599. static int test_wc_ecc_sm2_make_key(void)
  22600. {
  22601. int res = TEST_SKIPPED;
  22602. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  22603. EXPECT_DECLS;
  22604. WC_RNG rng[1];
  22605. ecc_key key[1];
  22606. XMEMSET(rng, 0, sizeof(*rng));
  22607. XMEMSET(key, 0, sizeof(*key));
  22608. ExpectIntEQ(wc_InitRng(rng), 0);
  22609. ExpectIntEQ(wc_ecc_init(key), 0);
  22610. /* Test invalid parameters. */
  22611. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, NULL, WC_ECC_FLAG_NONE),
  22612. BAD_FUNC_ARG);
  22613. ExpectIntEQ(wc_ecc_sm2_make_key(rng, NULL, WC_ECC_FLAG_NONE),
  22614. BAD_FUNC_ARG);
  22615. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, key, WC_ECC_FLAG_NONE),
  22616. BAD_FUNC_ARG);
  22617. /* Test valid parameters. */
  22618. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  22619. ExpectIntEQ(key->dp->id, ECC_SM2P256V1);
  22620. wc_ecc_free(key);
  22621. wc_FreeRng(rng);
  22622. #ifdef FP_ECC
  22623. wc_ecc_fp_free();
  22624. #endif
  22625. res = EXPECT_RESULT();
  22626. #endif
  22627. return res;
  22628. }
  22629. /*
  22630. * Testing wc_ecc_sm2_shared_secret()
  22631. */
  22632. static int test_wc_ecc_sm2_shared_secret(void)
  22633. {
  22634. int res = TEST_SKIPPED;
  22635. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  22636. EXPECT_DECLS;
  22637. WC_RNG rng[1];
  22638. ecc_key keyA[1];
  22639. ecc_key keyB[1];
  22640. byte outA[32];
  22641. byte outB[32];
  22642. word32 outALen = 32;
  22643. word32 outBLen = 32;
  22644. XMEMSET(rng, 0, sizeof(*rng));
  22645. XMEMSET(keyA, 0, sizeof(*keyA));
  22646. XMEMSET(keyB, 0, sizeof(*keyB));
  22647. ExpectIntEQ(wc_InitRng(rng), 0);
  22648. ExpectIntEQ(wc_ecc_init(keyA), 0);
  22649. ExpectIntEQ(wc_ecc_init(keyB), 0);
  22650. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyA, WC_ECC_FLAG_NONE), 0);
  22651. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyB, WC_ECC_FLAG_NONE), 0);
  22652. #ifdef ECC_TIMING_RESISTANT
  22653. ExpectIntEQ(wc_ecc_set_rng(keyA, rng), 0);
  22654. ExpectIntEQ(wc_ecc_set_rng(keyB, rng), 0);
  22655. #endif
  22656. /* Test invalid parameters. */
  22657. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  22658. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, NULL, NULL), BAD_FUNC_ARG);
  22659. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, NULL, NULL), BAD_FUNC_ARG);
  22660. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, outA, NULL), BAD_FUNC_ARG);
  22661. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, &outALen),
  22662. BAD_FUNC_ARG);
  22663. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, outA, &outALen),
  22664. BAD_FUNC_ARG);
  22665. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, outA, &outALen),
  22666. BAD_FUNC_ARG);
  22667. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, NULL, &outALen),
  22668. BAD_FUNC_ARG);
  22669. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, NULL), BAD_FUNC_ARG);
  22670. /* Test valid parameters. */
  22671. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, &outALen), 0);
  22672. ExpectIntLE(outALen, 32);
  22673. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyB, keyA, outB, &outBLen), 0);
  22674. ExpectIntLE(outBLen, 32);
  22675. ExpectIntEQ(outALen, outBLen);
  22676. ExpectBufEQ(outA, outB, outALen);
  22677. wc_ecc_free(keyB);
  22678. wc_ecc_free(keyA);
  22679. wc_FreeRng(rng);
  22680. #ifdef FP_ECC
  22681. wc_ecc_fp_free();
  22682. #endif
  22683. res = EXPECT_RESULT();
  22684. #endif
  22685. return res;
  22686. }
  22687. /*
  22688. * Testing wc_ecc_sm2_create_digest()
  22689. */
  22690. static int test_wc_ecc_sm2_create_digest(void)
  22691. {
  22692. int res = TEST_SKIPPED;
  22693. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && !defined(NO_HASH_WRAPPER) && \
  22694. (defined(WOLFSSL_SM3) || !defined(NO_SHA256))
  22695. EXPECT_DECLS;
  22696. ecc_key key[1];
  22697. enum wc_HashType hashType;
  22698. unsigned char pub[] = {
  22699. 0x04,
  22700. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22701. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22702. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22703. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22704. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22705. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22706. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22707. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22708. };
  22709. unsigned char id[] = {
  22710. 0x01, 0x02, 0x03,
  22711. };
  22712. unsigned char msg[] = {
  22713. 0x01, 0x02, 0x03,
  22714. };
  22715. unsigned char hash[32];
  22716. #ifdef WOLFSSL_SM3
  22717. unsigned char expHash[32] = {
  22718. 0xc1, 0xdd, 0x92, 0xc5, 0x60, 0xd3, 0x94, 0x28,
  22719. 0xeb, 0x0f, 0x57, 0x79, 0x3f, 0xc9, 0x96, 0xc5,
  22720. 0xfa, 0xf5, 0x90, 0xb2, 0x64, 0x2f, 0xaf, 0x9c,
  22721. 0xc8, 0x57, 0x21, 0x6a, 0x52, 0x7e, 0xf1, 0x95
  22722. };
  22723. #else
  22724. unsigned char expHash[32] = {
  22725. 0xea, 0x41, 0x55, 0x21, 0x61, 0x00, 0x5c, 0x9a,
  22726. 0x57, 0x35, 0x6b, 0x49, 0xca, 0x8f, 0x65, 0xc2,
  22727. 0x0e, 0x29, 0x0c, 0xa0, 0x1d, 0xa7, 0xc4, 0xed,
  22728. 0xdd, 0x51, 0x12, 0xf6, 0xe7, 0x55, 0xc5, 0xf4
  22729. };
  22730. #endif
  22731. #ifdef WOLFSSL_SM3
  22732. hashType = WC_HASH_TYPE_SM3;
  22733. #else
  22734. hashType = WC_HASH_TYPE_SHA256;
  22735. #endif
  22736. XMEMSET(key, 0, sizeof(*key));
  22737. ExpectIntEQ(wc_ecc_init(key), 0);
  22738. /* Test with no curve set. */
  22739. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22740. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22741. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22742. /* Test invalid parameters. */
  22743. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22744. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22745. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  22746. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22747. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  22748. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22749. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22750. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  22751. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22752. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  22753. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  22754. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22755. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  22756. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22757. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22758. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  22759. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22760. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  22761. /* Bad hash type. */
  22762. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22763. -1, hash, 0, key), BAD_FUNC_ARG);
  22764. /* Bad hash size. */
  22765. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22766. hashType, hash, 0, key), BUFFER_E);
  22767. /* Test valid parameters. */
  22768. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22769. hashType, hash, sizeof(hash), key), 0);
  22770. ExpectBufEQ(hash, expHash, sizeof(expHash));
  22771. wc_ecc_free(key);
  22772. res = EXPECT_RESULT();
  22773. #endif
  22774. return res;
  22775. }
  22776. /*
  22777. * Testing wc_ecc_sm2_verify_hash_ex()
  22778. */
  22779. static int test_wc_ecc_sm2_verify_hash_ex(void)
  22780. {
  22781. int res = TEST_SKIPPED;
  22782. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY) && \
  22783. defined(WOLFSSL_PUBLIC_MP)
  22784. EXPECT_DECLS;
  22785. ecc_key key[1];
  22786. mp_int r[1];
  22787. mp_int s[1];
  22788. int verified;
  22789. unsigned char pub[] = {
  22790. 0x04,
  22791. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22792. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22793. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22794. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22795. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22796. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22797. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22798. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22799. };
  22800. unsigned char hash[] = {
  22801. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  22802. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  22803. 0x50, 0x69, 0x5B, 0x20
  22804. };
  22805. unsigned char rData[] = {
  22806. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  22807. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  22808. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  22809. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE7,
  22810. };
  22811. unsigned char sData[] = {
  22812. 0x1D,
  22813. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  22814. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  22815. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  22816. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  22817. };
  22818. unsigned char rBadData[] = {
  22819. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  22820. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  22821. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  22822. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE8,
  22823. };
  22824. XMEMSET(key, 0, sizeof(*key));
  22825. XMEMSET(r, 0, sizeof(*r));
  22826. XMEMSET(s, 0, sizeof(*s));
  22827. ExpectIntEQ(mp_init(r), 0);
  22828. ExpectIntEQ(mp_init(s), 0);
  22829. ExpectIntEQ(mp_read_unsigned_bin(r, rData, sizeof(rData)), 0);
  22830. ExpectIntEQ(mp_read_unsigned_bin(s, sData, sizeof(sData)), 0);
  22831. ExpectIntEQ(wc_ecc_init(key), 0);
  22832. /* Test with no curve set. */
  22833. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22834. &verified, key), BAD_FUNC_ARG);
  22835. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22836. /* Test invalid parameters. */
  22837. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22838. NULL, NULL), BAD_FUNC_ARG);
  22839. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, NULL, sizeof(hash),
  22840. NULL, NULL), BAD_FUNC_ARG);
  22841. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, NULL, sizeof(hash),
  22842. NULL, NULL), BAD_FUNC_ARG);
  22843. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, hash, sizeof(hash),
  22844. NULL, NULL), BAD_FUNC_ARG);
  22845. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22846. &verified, NULL), BAD_FUNC_ARG);
  22847. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22848. NULL, key), BAD_FUNC_ARG);
  22849. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, hash, sizeof(hash),
  22850. &verified, key), BAD_FUNC_ARG);
  22851. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, hash, sizeof(hash),
  22852. &verified, key), BAD_FUNC_ARG);
  22853. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, NULL, sizeof(hash),
  22854. &verified, key), BAD_FUNC_ARG);
  22855. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22856. NULL, key), BAD_FUNC_ARG);
  22857. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22858. &verified, NULL), BAD_FUNC_ARG);
  22859. /* Make key not on the SM2 curve. */
  22860. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22861. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22862. &verified, key), BAD_FUNC_ARG);
  22863. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22864. /* Test valid parameters. */
  22865. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22866. &verified, key), 0);
  22867. ExpectIntEQ(verified, 1);
  22868. ExpectIntEQ(mp_read_unsigned_bin(r, rBadData, sizeof(rBadData)), 0);
  22869. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22870. &verified, key), 0);
  22871. ExpectIntEQ(verified, 0);
  22872. mp_free(s);
  22873. mp_free(r);
  22874. wc_ecc_free(key);
  22875. #ifdef FP_ECC
  22876. wc_ecc_fp_free();
  22877. #endif
  22878. res = EXPECT_RESULT();
  22879. #endif
  22880. return res;
  22881. }
  22882. /*
  22883. * Testing wc_ecc_sm2_verify_hash()
  22884. */
  22885. static int test_wc_ecc_sm2_verify_hash(void)
  22886. {
  22887. int res = TEST_SKIPPED;
  22888. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY)
  22889. EXPECT_DECLS;
  22890. ecc_key key[1];
  22891. int verified;
  22892. unsigned char pub[] = {
  22893. 0x04,
  22894. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22895. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22896. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22897. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22898. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22899. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22900. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22901. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22902. };
  22903. unsigned char hash[] = {
  22904. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  22905. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  22906. 0x50, 0x69, 0x5B, 0x20
  22907. };
  22908. unsigned char sig[] = {
  22909. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  22910. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  22911. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  22912. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  22913. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  22914. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  22915. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  22916. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  22917. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  22918. };
  22919. unsigned char sigBad[] = {
  22920. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  22921. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  22922. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  22923. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  22924. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  22925. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  22926. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  22927. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  22928. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDD
  22929. };
  22930. XMEMSET(key, 0, sizeof(*key));
  22931. ExpectIntEQ(wc_ecc_init(key), 0);
  22932. /* Test with no curve set. */
  22933. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22934. &verified, key), BAD_FUNC_ARG);
  22935. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22936. /* Test invalid parameters. */
  22937. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  22938. NULL, NULL), BAD_FUNC_ARG);
  22939. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  22940. NULL, NULL), BAD_FUNC_ARG);
  22941. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  22942. NULL, NULL), BAD_FUNC_ARG);
  22943. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  22944. &verified, NULL), BAD_FUNC_ARG);
  22945. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  22946. NULL, key), BAD_FUNC_ARG);
  22947. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  22948. &verified, key), BAD_FUNC_ARG);
  22949. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  22950. &verified, key), BAD_FUNC_ARG);
  22951. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22952. NULL, key), BAD_FUNC_ARG);
  22953. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22954. &verified, NULL), BAD_FUNC_ARG);
  22955. /* Make key not on the SM2 curve. */
  22956. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22957. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22958. &verified, key), BAD_FUNC_ARG);
  22959. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22960. /* Test valid parameters. */
  22961. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  22962. &verified, key), 0);
  22963. ExpectIntEQ(verified, 1);
  22964. ExpectIntEQ(wc_ecc_sm2_verify_hash(sigBad, sizeof(sigBad), hash,
  22965. sizeof(hash), &verified, key), 0);
  22966. ExpectIntEQ(verified, 0);
  22967. wc_ecc_free(key);
  22968. #ifdef FP_ECC
  22969. wc_ecc_fp_free();
  22970. #endif
  22971. res = EXPECT_RESULT();
  22972. #endif
  22973. return res;
  22974. }
  22975. /*
  22976. * Testing wc_ecc_sm2_verify_hash_ex()
  22977. */
  22978. static int test_wc_ecc_sm2_sign_hash_ex(void)
  22979. {
  22980. int res = TEST_SKIPPED;
  22981. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN) && \
  22982. defined(WOLFSSL_PUBLIC_MP)
  22983. EXPECT_DECLS;
  22984. WC_RNG rng[1];
  22985. ecc_key key[1];
  22986. mp_int r[1];
  22987. mp_int s[1];
  22988. unsigned char hash[32];
  22989. #ifdef HAVE_ECC_VERIFY
  22990. int verified;
  22991. #endif
  22992. XMEMSET(rng, 0, sizeof(*rng));
  22993. XMEMSET(key, 0, sizeof(*key));
  22994. XMEMSET(r, 0, sizeof(*r));
  22995. XMEMSET(s, 0, sizeof(*s));
  22996. ExpectIntEQ(wc_InitRng(rng), 0);
  22997. ExpectIntEQ(mp_init(r), 0);
  22998. ExpectIntEQ(mp_init(s), 0);
  22999. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  23000. ExpectIntEQ(wc_ecc_init(key), 0);
  23001. /* Test with no curve set. */
  23002. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23003. BAD_FUNC_ARG);
  23004. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  23005. /* Test invalid parameters. */
  23006. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  23007. NULL), BAD_FUNC_ARG);
  23008. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, NULL, NULL,
  23009. NULL), BAD_FUNC_ARG);
  23010. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, NULL, NULL,
  23011. NULL), BAD_FUNC_ARG);
  23012. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, key, NULL,
  23013. NULL), BAD_FUNC_ARG);
  23014. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, r,
  23015. NULL), BAD_FUNC_ARG);
  23016. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  23017. s), BAD_FUNC_ARG);
  23018. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, key, r, s),
  23019. BAD_FUNC_ARG);
  23020. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, key, r, s),
  23021. BAD_FUNC_ARG);
  23022. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, NULL, r, s),
  23023. BAD_FUNC_ARG);
  23024. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, NULL, s),
  23025. BAD_FUNC_ARG);
  23026. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, NULL),
  23027. BAD_FUNC_ARG);
  23028. /* Make key not on the SM2 curve. */
  23029. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23030. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23031. BAD_FUNC_ARG);
  23032. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23033. #ifdef WOLFSSL_SP_MATH_ALL
  23034. {
  23035. mp_int smallR[1];
  23036. sp_init_size(smallR, 1);
  23037. /* Force failure in _ecc_sm2_calc_r_s by r being too small. */
  23038. ExpectIntLT(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key,
  23039. smallR, s), 0);
  23040. }
  23041. #endif
  23042. /* Test valid parameters. */
  23043. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23044. 0);
  23045. #ifdef HAVE_ECC_VERIFY
  23046. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash), &verified,
  23047. key), 0);
  23048. ExpectIntEQ(verified, 1);
  23049. #endif
  23050. mp_free(s);
  23051. mp_free(r);
  23052. wc_ecc_free(key);
  23053. wc_FreeRng(rng);
  23054. #ifdef FP_ECC
  23055. wc_ecc_fp_free();
  23056. #endif
  23057. res = EXPECT_RESULT();
  23058. #endif
  23059. return res;
  23060. }
  23061. /*
  23062. * Testing wc_ecc_sm2_verify_hash()
  23063. */
  23064. static int test_wc_ecc_sm2_sign_hash(void)
  23065. {
  23066. int res = TEST_SKIPPED;
  23067. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN)
  23068. EXPECT_DECLS;
  23069. WC_RNG rng[1];
  23070. ecc_key key[1];
  23071. unsigned char hash[32];
  23072. unsigned char sig[72];
  23073. word32 sigSz = sizeof(sig);
  23074. #ifdef HAVE_ECC_VERIFY
  23075. int verified;
  23076. #endif
  23077. XMEMSET(rng, 0, sizeof(*rng));
  23078. XMEMSET(key, 0, sizeof(*key));
  23079. ExpectIntEQ(wc_InitRng(rng), 0);
  23080. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  23081. ExpectIntEQ(wc_ecc_init(key), 0);
  23082. /* Test with no curve set. */
  23083. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23084. BAD_FUNC_ARG);
  23085. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  23086. /* Test invalid parameters. */
  23087. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  23088. NULL), BAD_FUNC_ARG);
  23089. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, NULL, NULL,
  23090. NULL), BAD_FUNC_ARG);
  23091. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, NULL, NULL,
  23092. NULL), BAD_FUNC_ARG);
  23093. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, &sigSz, NULL,
  23094. NULL), BAD_FUNC_ARG);
  23095. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, rng,
  23096. NULL), BAD_FUNC_ARG);
  23097. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  23098. key), BAD_FUNC_ARG);
  23099. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, &sigSz, rng,
  23100. key), BAD_FUNC_ARG);
  23101. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, &sigSz, rng,
  23102. key), BAD_FUNC_ARG);
  23103. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, NULL, rng,
  23104. key), BAD_FUNC_ARG);
  23105. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, NULL,
  23106. key), BAD_FUNC_ARG);
  23107. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng,
  23108. NULL), BAD_FUNC_ARG);
  23109. /* Make key not on the SM2 curve. */
  23110. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23111. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23112. BAD_FUNC_ARG);
  23113. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23114. /* Test valid parameters. */
  23115. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23116. 0);
  23117. #ifdef HAVE_ECC_VERIFY
  23118. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sigSz, hash, sizeof(hash),
  23119. &verified, key), 0);
  23120. ExpectIntEQ(verified, 1);
  23121. #endif
  23122. wc_ecc_free(key);
  23123. wc_FreeRng(rng);
  23124. #ifdef FP_ECC
  23125. wc_ecc_fp_free();
  23126. #endif
  23127. res = EXPECT_RESULT();
  23128. #endif
  23129. return res;
  23130. }
  23131. /*
  23132. * Testing ToTraditional
  23133. */
  23134. static int test_ToTraditional(void)
  23135. {
  23136. EXPECT_DECLS;
  23137. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  23138. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  23139. defined(OPENSSL_EXTRA_X509_SMALL)) && !defined(NO_FILESYSTEM)
  23140. XFILE f = XBADFILE;
  23141. byte input[TWOK_BUF];
  23142. word32 sz = 0;
  23143. ExpectTrue((f = XFOPEN("./certs/server-keyPkcs8.der", "rb")) != XBADFILE);
  23144. ExpectTrue((sz = (word32)XFREAD(input, 1, sizeof(input), f)) > 0);
  23145. if (f != XBADFILE)
  23146. XFCLOSE(f);
  23147. /* Good case */
  23148. ExpectIntGT(ToTraditional(input, sz), 0);
  23149. /* Bad cases */
  23150. ExpectIntEQ(ToTraditional(NULL, 0), BAD_FUNC_ARG);
  23151. ExpectIntEQ(ToTraditional(NULL, sz), BAD_FUNC_ARG);
  23152. #ifdef WOLFSSL_ASN_TEMPLATE
  23153. ExpectIntEQ(ToTraditional(input, 0), BUFFER_E);
  23154. #else
  23155. ExpectIntEQ(ToTraditional(input, 0), ASN_PARSE_E);
  23156. #endif
  23157. #endif
  23158. return EXPECT_RESULT();
  23159. } /* End test_ToTraditional*/
  23160. /*
  23161. * Testing wc_EccPrivateKeyToDer
  23162. */
  23163. static int test_wc_EccPrivateKeyToDer(void)
  23164. {
  23165. EXPECT_DECLS;
  23166. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  23167. byte output[ONEK_BUF];
  23168. ecc_key eccKey;
  23169. WC_RNG rng;
  23170. word32 inLen;
  23171. word32 outLen = 0;
  23172. int ret;
  23173. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23174. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23175. ExpectIntEQ(wc_InitRng(&rng), 0);
  23176. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23177. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23178. #if defined(WOLFSSL_ASYNC_CRYPT)
  23179. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23180. #endif
  23181. ExpectIntEQ(ret, 0);
  23182. inLen = (word32)sizeof(output);
  23183. /* Bad Cases */
  23184. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23185. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23186. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, NULL, inLen), LENGTH_ONLY_E);
  23187. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, output, 0), BAD_FUNC_ARG);
  23188. /* Good Case */
  23189. ExpectIntGT(outLen = wc_EccPrivateKeyToDer(&eccKey, output, inLen), 0);
  23190. wc_ecc_free(&eccKey);
  23191. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23192. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  23193. {
  23194. /* test importing private only into a PKEY struct */
  23195. EC_KEY* ec = NULL;
  23196. EVP_PKEY* pkey = NULL;
  23197. const unsigned char* der;
  23198. der = output;
  23199. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, outLen));
  23200. der = output;
  23201. ExpectNotNull(ec = d2i_ECPrivateKey(NULL, &der, outLen));
  23202. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  23203. if (EXPECT_FAIL()) {
  23204. EC_KEY_free(ec);
  23205. }
  23206. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  23207. }
  23208. #endif
  23209. #endif
  23210. return EXPECT_RESULT();
  23211. } /* End test_wc_EccPrivateKeyToDer*/
  23212. /*
  23213. * Testing wc_DhPublicKeyDecode
  23214. */
  23215. static int test_wc_DhPublicKeyDecode(void)
  23216. {
  23217. EXPECT_DECLS;
  23218. #ifndef NO_DH
  23219. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  23220. DhKey key;
  23221. word32 inOutIdx;
  23222. XMEMSET(&key, 0, sizeof(DhKey));
  23223. ExpectIntEQ(wc_InitDhKey(&key), 0);
  23224. ExpectIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0), BAD_FUNC_ARG);
  23225. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23226. BAD_FUNC_ARG);
  23227. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23228. BAD_FUNC_ARG);
  23229. inOutIdx = 0;
  23230. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  23231. BAD_FUNC_ARG);
  23232. inOutIdx = 0;
  23233. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  23234. BAD_FUNC_ARG);
  23235. inOutIdx = 0;
  23236. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  23237. sizeof_dh_pub_key_der_2048), 0);
  23238. ExpectIntNE(key.p.used, 0);
  23239. ExpectIntNE(key.g.used, 0);
  23240. ExpectIntEQ(key.q.used, 0);
  23241. ExpectIntNE(key.pub.used, 0);
  23242. ExpectIntEQ(key.priv.used, 0);
  23243. DoExpectIntEQ(wc_FreeDhKey(&key), 0);
  23244. #endif
  23245. #endif /* !NO_DH */
  23246. return EXPECT_RESULT();
  23247. }
  23248. /*
  23249. * Testing wc_Ed25519KeyToDer
  23250. */
  23251. static int test_wc_Ed25519KeyToDer(void)
  23252. {
  23253. EXPECT_DECLS;
  23254. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23255. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23256. byte output[ONEK_BUF];
  23257. ed25519_key ed25519Key;
  23258. WC_RNG rng;
  23259. word32 inLen;
  23260. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23261. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23262. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23263. ExpectIntEQ(wc_InitRng(&rng), 0);
  23264. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23265. inLen = (word32)sizeof(output);
  23266. /* Bad Cases */
  23267. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23268. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23269. ExpectIntEQ(wc_Ed25519KeyToDer(&ed25519Key, output, 0), BAD_FUNC_ARG);
  23270. /* Good Cases */
  23271. /* length only */
  23272. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen), 0);
  23273. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, output, inLen), 0);
  23274. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23275. wc_ed25519_free(&ed25519Key);
  23276. #endif
  23277. return EXPECT_RESULT();
  23278. } /* End test_wc_Ed25519KeyToDer*/
  23279. /*
  23280. * Testing wc_Ed25519PrivateKeyToDer
  23281. */
  23282. static int test_wc_Ed25519PrivateKeyToDer(void)
  23283. {
  23284. EXPECT_DECLS;
  23285. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23286. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23287. byte output[ONEK_BUF];
  23288. ed25519_key ed25519PrivKey;
  23289. WC_RNG rng;
  23290. word32 inLen;
  23291. XMEMSET(&ed25519PrivKey, 0, sizeof(ed25519_key));
  23292. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23293. ExpectIntEQ(wc_ed25519_init(&ed25519PrivKey), 0);
  23294. ExpectIntEQ(wc_InitRng(&rng), 0);
  23295. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey),
  23296. 0);
  23297. inLen = (word32)sizeof(output);
  23298. /* Bad Cases */
  23299. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23300. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23301. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0),
  23302. BAD_FUNC_ARG);
  23303. /* Good Cases */
  23304. /* length only */
  23305. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen), 0);
  23306. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen), 0);
  23307. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23308. wc_ed25519_free(&ed25519PrivKey);
  23309. #endif
  23310. return EXPECT_RESULT();
  23311. } /* End test_wc_Ed25519PrivateKeyToDer*/
  23312. /*
  23313. * Testing wc_Ed448KeyToDer
  23314. */
  23315. static int test_wc_Ed448KeyToDer(void)
  23316. {
  23317. EXPECT_DECLS;
  23318. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23319. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23320. byte output[ONEK_BUF];
  23321. ed448_key ed448Key;
  23322. WC_RNG rng;
  23323. word32 inLen;
  23324. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23325. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23326. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23327. ExpectIntEQ(wc_InitRng(&rng), 0);
  23328. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23329. inLen = (word32)sizeof(output);
  23330. /* Bad Cases */
  23331. ExpectIntEQ(wc_Ed448KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23332. ExpectIntEQ(wc_Ed448KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23333. ExpectIntEQ(wc_Ed448KeyToDer(&ed448Key, output, 0), BAD_FUNC_ARG);
  23334. /* Good Cases */
  23335. /* length only */
  23336. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, NULL, inLen), 0);
  23337. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, output, inLen), 0);
  23338. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23339. wc_ed448_free(&ed448Key);
  23340. #endif
  23341. return EXPECT_RESULT();
  23342. } /* End test_wc_Ed448KeyToDer*/
  23343. /*
  23344. * Testing wc_Ed448PrivateKeyToDer
  23345. */
  23346. static int test_wc_Ed448PrivateKeyToDer(void)
  23347. {
  23348. EXPECT_DECLS;
  23349. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23350. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23351. byte output[ONEK_BUF];
  23352. ed448_key ed448PrivKey;
  23353. WC_RNG rng;
  23354. word32 inLen;
  23355. XMEMSET(&ed448PrivKey, 0, sizeof(ed448_key));
  23356. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23357. ExpectIntEQ(wc_ed448_init(&ed448PrivKey), 0);
  23358. ExpectIntEQ(wc_InitRng(&rng), 0);
  23359. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey),
  23360. 0);
  23361. inLen = (word32)sizeof(output);
  23362. /* Bad Cases */
  23363. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23364. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23365. ExpectIntEQ(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0),
  23366. BAD_FUNC_ARG);
  23367. /* Good cases */
  23368. /* length only */
  23369. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen), 0);
  23370. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen), 0);
  23371. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23372. wc_ed448_free(&ed448PrivKey);
  23373. #endif
  23374. return EXPECT_RESULT();
  23375. } /* End test_wc_Ed448PrivateKeyToDer*/
  23376. /*
  23377. * Testing wc_SetSubjectBuffer
  23378. */
  23379. static int test_wc_SetSubjectBuffer(void)
  23380. {
  23381. EXPECT_DECLS;
  23382. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  23383. Cert cert;
  23384. XFILE file = XBADFILE;
  23385. byte* der = NULL;
  23386. word32 derSz;
  23387. derSz = FOURK_BUF;
  23388. ExpectNotNull(der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  23389. DYNAMIC_TYPE_TMP_BUFFER));
  23390. ExpectTrue((file = XFOPEN("./certs/ca-cert.der", "rb")) != XBADFILE);
  23391. ExpectTrue((derSz = (word32)XFREAD(der, 1, FOURK_BUF, file)) > 0);
  23392. if (file != XBADFILE)
  23393. XFCLOSE(file);
  23394. ExpectIntEQ(wc_InitCert(&cert), 0);
  23395. ExpectIntEQ(wc_SetSubjectBuffer(&cert, der, derSz), 0);
  23396. ExpectIntEQ(wc_SetSubjectBuffer(NULL, der, derSz), BAD_FUNC_ARG);
  23397. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23398. #endif
  23399. return EXPECT_RESULT();
  23400. } /* End test_wc_SetSubjectBuffer*/
  23401. /*
  23402. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  23403. */
  23404. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  23405. {
  23406. EXPECT_DECLS;
  23407. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23408. WC_RNG rng;
  23409. Cert cert;
  23410. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23411. RsaKey rsaKey;
  23412. int bits = 2048;
  23413. #endif
  23414. #if defined(HAVE_ECC)
  23415. ecc_key eccKey;
  23416. int ret;
  23417. #endif
  23418. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23419. ed25519_key ed25519Key;
  23420. #endif
  23421. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23422. ed448_key ed448Key;
  23423. #endif
  23424. #ifndef HAVE_FIPS
  23425. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  23426. #else
  23427. ExpectIntEQ(wc_InitRng(&rng), 0);
  23428. #endif
  23429. ExpectIntEQ(wc_InitCert(&cert), 0);
  23430. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23431. /* RSA */
  23432. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  23433. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  23434. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  23435. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey),
  23436. 0);
  23437. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  23438. #endif
  23439. #if defined(HAVE_ECC)
  23440. /* ECC */
  23441. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23442. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23443. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23444. #if defined(WOLFSSL_ASYNC_CRYPT)
  23445. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23446. #endif
  23447. ExpectIntEQ(ret, 0);
  23448. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey),
  23449. 0);
  23450. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  23451. #endif
  23452. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23453. /* ED25519 */
  23454. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23455. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23456. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23457. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23458. &ed25519Key), 0);
  23459. wc_ed25519_free(&ed25519Key);
  23460. #endif
  23461. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23462. /* ED448 */
  23463. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23464. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23465. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23466. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  23467. &ed448Key), 0);
  23468. wc_ed448_free(&ed448Key);
  23469. #endif
  23470. wc_FreeRng(&rng);
  23471. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23472. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  23473. return EXPECT_RESULT();
  23474. } /* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  23475. /*
  23476. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  23477. */
  23478. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  23479. {
  23480. EXPECT_DECLS;
  23481. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23482. WC_RNG rng;
  23483. Cert cert;
  23484. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23485. RsaKey rsaKey;
  23486. int bits = 2048;
  23487. #endif
  23488. #if defined(HAVE_ECC)
  23489. ecc_key eccKey;
  23490. int ret;
  23491. #endif
  23492. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23493. ed25519_key ed25519Key;
  23494. #endif
  23495. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23496. ed448_key ed448Key;
  23497. #endif
  23498. #ifndef HAVE_FIPS
  23499. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  23500. #else
  23501. ExpectIntEQ(wc_InitRng(&rng), 0);
  23502. #endif
  23503. ExpectIntEQ(wc_InitCert(&cert), 0);
  23504. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23505. /* RSA */
  23506. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  23507. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  23508. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  23509. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey), 0);
  23510. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  23511. #endif
  23512. #if defined(HAVE_ECC)
  23513. /* ECC */
  23514. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23515. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23516. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23517. #if defined(WOLFSSL_ASYNC_CRYPT)
  23518. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23519. #endif
  23520. ExpectIntEQ(ret, 0);
  23521. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey), 0);
  23522. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  23523. #endif
  23524. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23525. /* ED25519 */
  23526. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23527. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23528. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23529. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23530. &ed25519Key), 0);
  23531. wc_ed25519_free(&ed25519Key);
  23532. #endif
  23533. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23534. /* ED448 */
  23535. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23536. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23537. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23538. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, &ed448Key),
  23539. 0);
  23540. wc_ed448_free(&ed448Key);
  23541. #endif
  23542. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23543. #endif /* defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  23544. return EXPECT_RESULT();
  23545. } /* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  23546. /*
  23547. * Testing wc_PKCS7_New()
  23548. */
  23549. static int test_wc_PKCS7_New(void)
  23550. {
  23551. EXPECT_DECLS;
  23552. #if defined(HAVE_PKCS7)
  23553. PKCS7* pkcs7 = NULL;
  23554. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, testDevId));
  23555. wc_PKCS7_Free(pkcs7);
  23556. #endif
  23557. return EXPECT_RESULT();
  23558. } /* END test-wc_PKCS7_New */
  23559. /*
  23560. * Testing wc_PKCS7_Init()
  23561. */
  23562. static int test_wc_PKCS7_Init(void)
  23563. {
  23564. EXPECT_DECLS;
  23565. #if defined(HAVE_PKCS7)
  23566. PKCS7* pkcs7 = NULL;
  23567. void* heap = NULL;
  23568. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  23569. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  23570. /* Pass in bad args. */
  23571. ExpectIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
  23572. wc_PKCS7_Free(pkcs7);
  23573. #endif
  23574. return EXPECT_RESULT();
  23575. } /* END test-wc_PKCS7_Init */
  23576. /*
  23577. * Testing wc_PKCS7_InitWithCert()
  23578. */
  23579. static int test_wc_PKCS7_InitWithCert(void)
  23580. {
  23581. EXPECT_DECLS;
  23582. #if defined(HAVE_PKCS7)
  23583. PKCS7* pkcs7 = NULL;
  23584. #ifndef NO_RSA
  23585. #if defined(USE_CERT_BUFFERS_2048)
  23586. unsigned char cert[sizeof(client_cert_der_2048)];
  23587. int certSz = (int)sizeof(cert);
  23588. XMEMSET(cert, 0, certSz);
  23589. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  23590. #elif defined(USE_CERT_BUFFERS_1024)
  23591. unsigned char cert[sizeof(client_cert_der_1024)];
  23592. int certSz = (int)sizeof(cert);
  23593. XMEMSET(cert, 0, certSz);
  23594. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  23595. #else
  23596. unsigned char cert[ONEK_BUF];
  23597. XFILE fp = XBADFILE;
  23598. int certSz;
  23599. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23600. XBADFILE);
  23601. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23602. fp), 0);
  23603. if (fp != XBADFILE)
  23604. XFCLOSE(fp);
  23605. #endif
  23606. #elif defined(HAVE_ECC)
  23607. #if defined(USE_CERT_BUFFERS_256)
  23608. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23609. int certSz = (int)sizeof(cert);
  23610. XMEMSET(cert, 0, certSz);
  23611. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  23612. #else
  23613. unsigned char cert[ONEK_BUF];
  23614. XFILE fp = XBADFILE;
  23615. int certSz;
  23616. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23617. XBADFILE);
  23618. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256),
  23619. fp), 0);
  23620. if (fp != XBADFILE)
  23621. XFCLOSE(fp);
  23622. #endif
  23623. #else
  23624. #error PKCS7 requires ECC or RSA
  23625. #endif
  23626. #ifdef HAVE_ECC
  23627. {
  23628. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  23629. static unsigned char certWithInvalidEccKey[] = {
  23630. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  23631. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  23632. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  23633. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  23634. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23635. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  23636. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23637. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23638. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23639. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23640. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23641. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23642. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23643. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23644. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23645. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  23646. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  23647. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  23648. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  23649. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  23650. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  23651. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  23652. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  23653. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  23654. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  23655. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  23656. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  23657. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  23658. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  23659. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  23660. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  23661. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  23662. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  23663. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  23664. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  23665. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  23666. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  23667. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  23668. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  23669. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  23670. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23671. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  23672. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  23673. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23674. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  23675. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23676. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  23677. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23678. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23679. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23680. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23681. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23682. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23683. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23684. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23685. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23686. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  23687. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  23688. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  23689. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  23690. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  23691. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  23692. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  23693. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  23694. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  23695. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  23696. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  23697. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  23698. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  23699. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  23700. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  23701. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  23702. 0x08, 0xA8, 0xB4
  23703. };
  23704. #endif
  23705. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23706. /* If initialization is not successful, it's free'd in init func. */
  23707. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz),
  23708. 0);
  23709. wc_PKCS7_Free(pkcs7);
  23710. pkcs7 = NULL;
  23711. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23712. /* Valid initialization usage. */
  23713. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23714. /* Pass in bad args. No need free for null checks, free at end.*/
  23715. ExpectIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  23716. BAD_FUNC_ARG);
  23717. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  23718. BAD_FUNC_ARG);
  23719. #ifdef HAVE_ECC
  23720. ExpectIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  23721. sizeof(certWithInvalidEccKey)), 0);
  23722. }
  23723. #endif
  23724. wc_PKCS7_Free(pkcs7);
  23725. #endif
  23726. return EXPECT_RESULT();
  23727. } /* END test_wc_PKCS7_InitWithCert */
  23728. /*
  23729. * Testing wc_PKCS7_EncodeData()
  23730. */
  23731. static int test_wc_PKCS7_EncodeData(void)
  23732. {
  23733. EXPECT_DECLS;
  23734. #if defined(HAVE_PKCS7)
  23735. PKCS7* pkcs7 = NULL;
  23736. byte output[FOURK_BUF];
  23737. byte data[] = "My encoded DER cert.";
  23738. #ifndef NO_RSA
  23739. #if defined(USE_CERT_BUFFERS_2048)
  23740. unsigned char cert[sizeof(client_cert_der_2048)];
  23741. unsigned char key[sizeof(client_key_der_2048)];
  23742. int certSz = (int)sizeof(cert);
  23743. int keySz = (int)sizeof(key);
  23744. XMEMSET(cert, 0, certSz);
  23745. XMEMSET(key, 0, keySz);
  23746. XMEMCPY(cert, client_cert_der_2048, certSz);
  23747. XMEMCPY(key, client_key_der_2048, keySz);
  23748. #elif defined(USE_CERT_BUFFERS_1024)
  23749. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  23750. unsigned char key[sizeof_client_key_der_1024];
  23751. int certSz = (int)sizeof(cert);
  23752. int keySz = (int)sizeof(key);
  23753. XMEMSET(cert, 0, certSz);
  23754. XMEMSET(key, 0, keySz);
  23755. XMEMCPY(cert, client_cert_der_1024, certSz);
  23756. XMEMCPY(key, client_key_der_1024, keySz);
  23757. #else
  23758. unsigned char cert[ONEK_BUF];
  23759. unsigned char key[ONEK_BUF];
  23760. XFILE fp = XBADFILE;
  23761. int certSz;
  23762. int keySz;
  23763. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23764. XBADFILE);
  23765. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23766. fp), 0);
  23767. if (fp != XBADFILE) {
  23768. XFCLOSE(fp);
  23769. fp = XBADFILE;
  23770. }
  23771. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  23772. XBADFILE);
  23773. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  23774. 0);
  23775. if (fp != XBADFILE)
  23776. XFCLOSE(fp);
  23777. #endif
  23778. #elif defined(HAVE_ECC)
  23779. #if defined(USE_CERT_BUFFERS_256)
  23780. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23781. unsigned char key[sizeof(ecc_clikey_der_256)];
  23782. int certSz = (int)sizeof(cert);
  23783. int keySz = (int)sizeof(key);
  23784. XMEMSET(cert, 0, certSz);
  23785. XMEMSET(key, 0, keySz);
  23786. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23787. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  23788. #else
  23789. unsigned char cert[ONEK_BUF];
  23790. unsigned char key[ONEK_BUF];
  23791. XFILE fp = XBADFILE;
  23792. int certSz, keySz;
  23793. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23794. XBADFILE);
  23795. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  23796. fp), 0);
  23797. if (fp != XBADFILE) {
  23798. XFCLOSE(fp);
  23799. fp = XBADFILE;
  23800. }
  23801. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  23802. XBADFILE);
  23803. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  23804. 0);
  23805. if (fp != XBADFILE)
  23806. XFCLOSE(fp);
  23807. #endif
  23808. #endif
  23809. XMEMSET(output, 0, sizeof(output));
  23810. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23811. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23812. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
  23813. if (pkcs7 != NULL) {
  23814. pkcs7->content = data;
  23815. pkcs7->contentSz = sizeof(data);
  23816. pkcs7->privateKey = key;
  23817. pkcs7->privateKeySz = keySz;
  23818. }
  23819. ExpectIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  23820. /* Test bad args. */
  23821. ExpectIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  23822. BAD_FUNC_ARG);
  23823. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  23824. BAD_FUNC_ARG);
  23825. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  23826. wc_PKCS7_Free(pkcs7);
  23827. #endif
  23828. return EXPECT_RESULT();
  23829. } /* END test_wc_PKCS7_EncodeData */
  23830. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  23831. !defined(NO_RSA) && !defined(NO_SHA256)
  23832. /* RSA sign raw digest callback */
  23833. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  23834. byte* out, word32 outSz, byte* privateKey,
  23835. word32 privateKeySz, int devid, int hashOID)
  23836. {
  23837. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  23838. byte digInfoEncoding[] = {
  23839. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  23840. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  23841. 0x00, 0x04, 0x20
  23842. };
  23843. int ret;
  23844. byte digestInfo[ONEK_BUF];
  23845. byte sig[FOURK_BUF];
  23846. word32 digestInfoSz = 0;
  23847. word32 idx = 0;
  23848. RsaKey rsa;
  23849. /* SHA-256 required only for this example callback due to above
  23850. * digInfoEncoding[] */
  23851. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  23852. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  23853. (hashOID != SHA256h)) {
  23854. return -1;
  23855. }
  23856. /* build DigestInfo */
  23857. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  23858. digestInfoSz += sizeof(digInfoEncoding);
  23859. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  23860. digestInfoSz += digestSz;
  23861. /* set up RSA key */
  23862. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  23863. if (ret != 0) {
  23864. return ret;
  23865. }
  23866. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  23867. /* sign DigestInfo */
  23868. if (ret == 0) {
  23869. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  23870. &rsa, pkcs7->rng);
  23871. if (ret > 0) {
  23872. if (ret > (int)outSz) {
  23873. /* output buffer too small */
  23874. ret = -1;
  23875. }
  23876. else {
  23877. /* success, ret holds sig size */
  23878. XMEMCPY(out, sig, ret);
  23879. }
  23880. }
  23881. }
  23882. wc_FreeRsaKey(&rsa);
  23883. return ret;
  23884. }
  23885. #endif
  23886. #if defined(HAVE_PKCS7) && defined(ASN_BER_TO_DER)
  23887. typedef struct encodeSignedDataStream {
  23888. byte out[FOURK_BUF*3];
  23889. int idx;
  23890. word32 outIdx;
  23891. } encodeSignedDataStream;
  23892. /* content is 8k of partially created bundle */
  23893. static int GetContentCB(PKCS7* pkcs7, byte** content, void* ctx)
  23894. {
  23895. int ret = 0;
  23896. encodeSignedDataStream* strm = (encodeSignedDataStream*)ctx;
  23897. if (strm->outIdx < pkcs7->contentSz) {
  23898. ret = (pkcs7->contentSz > strm->outIdx + FOURK_BUF)?
  23899. FOURK_BUF : pkcs7->contentSz - strm->outIdx;
  23900. *content = strm->out + strm->outIdx;
  23901. strm->outIdx += ret;
  23902. }
  23903. (void)pkcs7;
  23904. return ret;
  23905. }
  23906. static int StreamOutputCB(PKCS7* pkcs7, const byte* output, word32 outputSz,
  23907. void* ctx)
  23908. {
  23909. encodeSignedDataStream* strm = (encodeSignedDataStream*)ctx;
  23910. XMEMCPY(strm->out + strm->idx, output, outputSz);
  23911. strm->idx += outputSz;
  23912. (void)pkcs7;
  23913. return 0;
  23914. }
  23915. #endif
  23916. /*
  23917. * Testing wc_PKCS7_EncodeSignedData()
  23918. */
  23919. static int test_wc_PKCS7_EncodeSignedData(void)
  23920. {
  23921. EXPECT_DECLS;
  23922. #if defined(HAVE_PKCS7)
  23923. PKCS7* pkcs7 = NULL;
  23924. WC_RNG rng;
  23925. byte output[FOURK_BUF];
  23926. byte badOut[1];
  23927. word32 outputSz = (word32)sizeof(output);
  23928. word32 badOutSz = 0;
  23929. byte data[] = "Test data to encode.";
  23930. #ifndef NO_RSA
  23931. #if defined(USE_CERT_BUFFERS_2048)
  23932. byte key[sizeof(client_key_der_2048)];
  23933. byte cert[sizeof(client_cert_der_2048)];
  23934. word32 keySz = (word32)sizeof(key);
  23935. word32 certSz = (word32)sizeof(cert);
  23936. XMEMSET(key, 0, keySz);
  23937. XMEMSET(cert, 0, certSz);
  23938. XMEMCPY(key, client_key_der_2048, keySz);
  23939. XMEMCPY(cert, client_cert_der_2048, certSz);
  23940. #elif defined(USE_CERT_BUFFERS_1024)
  23941. byte key[sizeof_client_key_der_1024];
  23942. byte cert[sizeof(sizeof_client_cert_der_1024)];
  23943. word32 keySz = (word32)sizeof(key);
  23944. word32 certSz = (word32)sizeof(cert);
  23945. XMEMSET(key, 0, keySz);
  23946. XMEMSET(cert, 0, certSz);
  23947. XMEMCPY(key, client_key_der_1024, keySz);
  23948. XMEMCPY(cert, client_cert_der_1024, certSz);
  23949. #else
  23950. unsigned char cert[ONEK_BUF];
  23951. unsigned char key[ONEK_BUF];
  23952. XFILE fp = XBADFILE;
  23953. int certSz;
  23954. int keySz;
  23955. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23956. XBADFILE);
  23957. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23958. fp), 0);
  23959. if (fp != XBADFILE) {
  23960. XFCLOSE(fp);
  23961. fp = XBADFILE;
  23962. }
  23963. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  23964. XBADFILE);
  23965. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  23966. 0);
  23967. if (fp != XBADFILE)
  23968. XFCLOSE(fp);
  23969. #endif
  23970. #elif defined(HAVE_ECC)
  23971. #if defined(USE_CERT_BUFFERS_256)
  23972. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23973. unsigned char key[sizeof(ecc_clikey_der_256)];
  23974. int certSz = (int)sizeof(cert);
  23975. int keySz = (int)sizeof(key);
  23976. XMEMSET(cert, 0, certSz);
  23977. XMEMSET(key, 0, keySz);
  23978. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  23979. XMEMCPY(key, ecc_clikey_der_256, keySz);
  23980. #else
  23981. unsigned char cert[ONEK_BUF];
  23982. unsigned char key[ONEK_BUF];
  23983. XFILE fp = XBADFILE;
  23984. int certSz;
  23985. int keySz;
  23986. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23987. XBADFILE);
  23988. ExpectIntGT(certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp), 0);
  23989. if (fp != XBADFILE) {
  23990. XFCLOSE(fp);
  23991. fp = XBADFILE;
  23992. }
  23993. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  23994. XBADFILE);
  23995. ExpectIntGT(keySz = (int)XFREAD(key, 1, ONEK_BUF, fp), 0);
  23996. if (fp != XBADFILE)
  23997. XFCLOSE(fp);
  23998. #endif
  23999. #endif
  24000. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24001. XMEMSET(output, 0, outputSz);
  24002. ExpectIntEQ(wc_InitRng(&rng), 0);
  24003. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24004. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24005. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24006. if (pkcs7 != NULL) {
  24007. pkcs7->content = data;
  24008. pkcs7->contentSz = (word32)sizeof(data);
  24009. pkcs7->privateKey = key;
  24010. pkcs7->privateKeySz = (word32)sizeof(key);
  24011. pkcs7->encryptOID = RSAk;
  24012. #ifdef NO_SHA
  24013. pkcs7->hashOID = SHA256h;
  24014. #else
  24015. pkcs7->hashOID = SHAh;
  24016. #endif
  24017. pkcs7->rng = &rng;
  24018. }
  24019. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  24020. wc_PKCS7_Free(pkcs7);
  24021. pkcs7 = NULL;
  24022. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24023. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24024. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24025. #ifdef ASN_BER_TO_DER
  24026. wc_PKCS7_Free(pkcs7);
  24027. /* reinitialize and test setting stream mode */
  24028. {
  24029. int signedSz = 0;
  24030. encodeSignedDataStream strm;
  24031. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24032. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24033. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24034. if (pkcs7 != NULL) {
  24035. pkcs7->content = data;
  24036. pkcs7->contentSz = (word32)sizeof(data);
  24037. pkcs7->privateKey = key;
  24038. pkcs7->privateKeySz = (word32)sizeof(key);
  24039. pkcs7->encryptOID = RSAk;
  24040. #ifdef NO_SHA
  24041. pkcs7->hashOID = SHA256h;
  24042. #else
  24043. pkcs7->hashOID = SHAh;
  24044. #endif
  24045. pkcs7->rng = &rng;
  24046. }
  24047. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
  24048. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL), 0);
  24049. ExpectIntEQ(wc_PKCS7_SetStreamMode(NULL, 1, NULL, NULL, NULL),
  24050. BAD_FUNC_ARG);
  24051. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 1);
  24052. ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, output,
  24053. outputSz), 0);
  24054. wc_PKCS7_Free(pkcs7);
  24055. pkcs7 = NULL;
  24056. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24057. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24058. /* use exact signed buffer size since BER encoded */
  24059. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, signedSz), 0);
  24060. wc_PKCS7_Free(pkcs7);
  24061. /* now try with using callbacks for IO */
  24062. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24063. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24064. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24065. if (pkcs7 != NULL) {
  24066. pkcs7->contentSz = FOURK_BUF*2;
  24067. pkcs7->privateKey = key;
  24068. pkcs7->privateKeySz = (word32)sizeof(key);
  24069. pkcs7->encryptOID = RSAk;
  24070. #ifdef NO_SHA
  24071. pkcs7->hashOID = SHA256h;
  24072. #else
  24073. pkcs7->hashOID = SHAh;
  24074. #endif
  24075. pkcs7->rng = &rng;
  24076. }
  24077. XMEMSET(&strm, 0, sizeof(strm));
  24078. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
  24079. StreamOutputCB, (void*)&strm), 0);
  24080. ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, NULL, 0), 0);
  24081. wc_PKCS7_Free(pkcs7);
  24082. pkcs7 = NULL;
  24083. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24084. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24085. /* use exact signed buffer size since BER encoded */
  24086. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, strm.out, signedSz), 0);
  24087. }
  24088. #endif
  24089. #ifndef NO_PKCS7_STREAM
  24090. wc_PKCS7_Free(pkcs7);
  24091. pkcs7 = NULL;
  24092. {
  24093. word32 z;
  24094. int ret;
  24095. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24096. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24097. /* test for streaming mode */
  24098. ret = -1;
  24099. for (z = 0; z < outputSz && ret != 0; z++) {
  24100. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24101. if (ret < 0){
  24102. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24103. }
  24104. }
  24105. ExpectIntEQ(ret, 0);
  24106. ExpectIntNE(pkcs7->contentSz, 0);
  24107. ExpectNotNull(pkcs7->contentDynamic);
  24108. }
  24109. #endif /* !NO_PKCS7_STREAM */
  24110. /* Pass in bad args. */
  24111. ExpectIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz),
  24112. BAD_FUNC_ARG);
  24113. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  24114. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  24115. badOutSz), BAD_FUNC_ARG);
  24116. if (pkcs7 != NULL) {
  24117. pkcs7->hashOID = 0; /* bad hashOID */
  24118. }
  24119. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz),
  24120. BAD_FUNC_ARG);
  24121. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  24122. !defined(NO_RSA) && !defined(NO_SHA256)
  24123. /* test RSA sign raw digest callback, if using RSA and compiled in.
  24124. * Example callback assumes SHA-256, so only run test if compiled in. */
  24125. wc_PKCS7_Free(pkcs7);
  24126. pkcs7 = NULL;
  24127. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24128. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24129. if (pkcs7 != NULL) {
  24130. pkcs7->content = data;
  24131. pkcs7->contentSz = (word32)sizeof(data);
  24132. pkcs7->privateKey = key;
  24133. pkcs7->privateKeySz = (word32)sizeof(key);
  24134. pkcs7->encryptOID = RSAk;
  24135. pkcs7->hashOID = SHA256h;
  24136. pkcs7->rng = &rng;
  24137. }
  24138. ExpectIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  24139. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  24140. #endif
  24141. wc_PKCS7_Free(pkcs7);
  24142. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  24143. #endif
  24144. return EXPECT_RESULT();
  24145. } /* END test_wc_PKCS7_EncodeSignedData */
  24146. /*
  24147. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  24148. */
  24149. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  24150. {
  24151. EXPECT_DECLS;
  24152. #if defined(HAVE_PKCS7)
  24153. int i;
  24154. PKCS7* pkcs7 = NULL;
  24155. WC_RNG rng;
  24156. byte outputHead[FOURK_BUF/2];
  24157. byte outputFoot[FOURK_BUF/2];
  24158. word32 outputHeadSz = (word32)sizeof(outputHead);
  24159. word32 outputFootSz = (word32)sizeof(outputFoot);
  24160. byte data[FOURK_BUF];
  24161. wc_HashAlg hash;
  24162. #ifdef NO_SHA
  24163. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  24164. #else
  24165. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24166. #endif
  24167. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24168. word32 hashSz = wc_HashGetDigestSize(hashType);
  24169. #ifndef NO_RSA
  24170. #if defined(USE_CERT_BUFFERS_2048)
  24171. byte key[sizeof(client_key_der_2048)];
  24172. byte cert[sizeof(client_cert_der_2048)];
  24173. word32 keySz = (word32)sizeof(key);
  24174. word32 certSz = (word32)sizeof(cert);
  24175. XMEMSET(key, 0, keySz);
  24176. XMEMSET(cert, 0, certSz);
  24177. XMEMCPY(key, client_key_der_2048, keySz);
  24178. XMEMCPY(cert, client_cert_der_2048, certSz);
  24179. #elif defined(USE_CERT_BUFFERS_1024)
  24180. byte key[sizeof_client_key_der_1024];
  24181. byte cert[sizeof(sizeof_client_cert_der_1024)];
  24182. word32 keySz = (word32)sizeof(key);
  24183. word32 certSz = (word32)sizeof(cert);
  24184. XMEMSET(key, 0, keySz);
  24185. XMEMSET(cert, 0, certSz);
  24186. XMEMCPY(key, client_key_der_1024, keySz);
  24187. XMEMCPY(cert, client_cert_der_1024, certSz);
  24188. #else
  24189. unsigned char cert[ONEK_BUF];
  24190. unsigned char key[ONEK_BUF];
  24191. XFILE fp = XBADFILE;
  24192. int certSz;
  24193. int keySz;
  24194. ExpectTure((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  24195. XBADFILE);
  24196. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  24197. fp), 0);
  24198. if (fp != XBADFILE) {
  24199. XFCLOSE(fp);
  24200. fp = XBADFILE;
  24201. }
  24202. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  24203. XBADFILE);
  24204. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  24205. 0);
  24206. if (fp != XBADFILE)
  24207. XFCLOSE(fp);
  24208. #endif
  24209. #elif defined(HAVE_ECC)
  24210. #if defined(USE_CERT_BUFFERS_256)
  24211. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24212. unsigned char key[sizeof(ecc_clikey_der_256)];
  24213. int certSz = (int)sizeof(cert);
  24214. int keySz = (int)sizeof(key);
  24215. XMEMSET(cert, 0, certSz);
  24216. XMEMSET(key, 0, keySz);
  24217. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  24218. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  24219. #else
  24220. unsigned char cert[ONEK_BUF];
  24221. unsigned char key[ONEK_BUF];
  24222. XFILE fp = XBADFILE;
  24223. int certSz;
  24224. int keySz;
  24225. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  24226. XBADFILE);
  24227. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  24228. fp), 0);
  24229. if (fp != XBADFILE) {
  24230. XFCLOSE(fp);
  24231. fp = XBADFILE;
  24232. }
  24233. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  24234. XBADFILE);
  24235. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  24236. 0);
  24237. if (fp != XBADFILE)
  24238. XFCLOSE(fp);
  24239. #endif
  24240. #endif
  24241. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24242. /* initialize large data with sequence */
  24243. for (i=0; i<(int)sizeof(data); i++)
  24244. data[i] = i & 0xff;
  24245. XMEMSET(outputHead, 0, outputHeadSz);
  24246. XMEMSET(outputFoot, 0, outputFootSz);
  24247. ExpectIntEQ(wc_InitRng(&rng), 0);
  24248. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24249. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24250. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24251. if (pkcs7 != NULL) {
  24252. pkcs7->content = NULL; /* not used for ex */
  24253. pkcs7->contentSz = (word32)sizeof(data);
  24254. pkcs7->privateKey = key;
  24255. pkcs7->privateKeySz = (word32)sizeof(key);
  24256. pkcs7->encryptOID = RSAk;
  24257. #ifdef NO_SHA
  24258. pkcs7->hashOID = SHA256h;
  24259. #else
  24260. pkcs7->hashOID = SHAh;
  24261. #endif
  24262. pkcs7->rng = &rng;
  24263. }
  24264. /* calculate hash for content */
  24265. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  24266. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  24267. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  24268. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  24269. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  24270. /* Perform PKCS7 sign using hash directly */
  24271. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24272. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  24273. ExpectIntGT(outputHeadSz, 0);
  24274. ExpectIntGT(outputFootSz, 0);
  24275. wc_PKCS7_Free(pkcs7);
  24276. pkcs7 = NULL;
  24277. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24278. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24279. /* required parameter even on verify when using _ex, if using outputHead
  24280. * and outputFoot */
  24281. if (pkcs7 != NULL) {
  24282. pkcs7->contentSz = (word32)sizeof(data);
  24283. }
  24284. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24285. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  24286. wc_PKCS7_Free(pkcs7);
  24287. pkcs7 = NULL;
  24288. /* assembly complete PKCS7 sign and use normal verify */
  24289. {
  24290. byte* output = NULL;
  24291. word32 outputSz = 0;
  24292. #ifndef NO_PKCS7_STREAM
  24293. word32 z;
  24294. int ret;
  24295. #endif /* !NO_PKCS7_STREAM */
  24296. ExpectNotNull(output = (byte*)XMALLOC(
  24297. outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT,
  24298. DYNAMIC_TYPE_TMP_BUFFER));
  24299. if (output != NULL) {
  24300. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  24301. outputSz += outputHeadSz;
  24302. XMEMCPY(&output[outputSz], data, sizeof(data));
  24303. outputSz += sizeof(data);
  24304. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  24305. outputSz += outputFootSz;
  24306. }
  24307. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24308. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24309. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24310. #ifndef NO_PKCS7_STREAM
  24311. wc_PKCS7_Free(pkcs7);
  24312. pkcs7 = NULL;
  24313. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24314. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24315. /* test for streaming mode */
  24316. ret = -1;
  24317. for (z = 0; z < outputSz && ret != 0; z++) {
  24318. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24319. if (ret < 0){
  24320. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24321. }
  24322. }
  24323. ExpectIntEQ(ret, 0);
  24324. ExpectIntNE(pkcs7->contentSz, 0);
  24325. ExpectNotNull(pkcs7->contentDynamic);
  24326. wc_PKCS7_Free(pkcs7);
  24327. pkcs7 = NULL;
  24328. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24329. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24330. #endif /* !NO_PKCS7_STREAM */
  24331. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24332. }
  24333. /* Pass in bad args. */
  24334. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24335. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24336. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24337. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24338. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24339. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24340. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24341. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24342. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24343. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24344. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24345. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  24346. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24347. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  24348. if (pkcs7 != NULL) {
  24349. pkcs7->hashOID = 0; /* bad hashOID */
  24350. }
  24351. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24352. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24353. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24354. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24355. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24356. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24357. #ifndef NO_PKCS7_STREAM
  24358. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24359. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24360. #else
  24361. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24362. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  24363. #endif
  24364. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24365. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24366. #ifndef NO_PKCS7_STREAM
  24367. /* can pass in 0 buffer length with streaming API */
  24368. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24369. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24370. #else
  24371. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24372. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24373. #endif
  24374. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24375. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  24376. #ifndef NO_PKCS7_STREAM
  24377. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24378. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  24379. #else
  24380. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24381. outputHead, outputHeadSz, outputFoot, 0), BUFFER_E);
  24382. #endif
  24383. wc_PKCS7_Free(pkcs7);
  24384. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  24385. #endif
  24386. return EXPECT_RESULT();
  24387. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  24388. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  24389. /**
  24390. * Loads certs/keys from files or buffers into the argument buffers,
  24391. * helper function called by CreatePKCS7SignedData().
  24392. *
  24393. * Returns 0 on success, negative on error.
  24394. */
  24395. static int LoadPKCS7SignedDataCerts(
  24396. int useIntermediateCertChain, int pkAlgoType,
  24397. byte* intCARoot, word32* intCARootSz,
  24398. byte* intCA1, word32* intCA1Sz,
  24399. byte* intCA2, word32* intCA2Sz,
  24400. byte* cert, word32* certSz,
  24401. byte* key, word32* keySz)
  24402. {
  24403. EXPECT_DECLS;
  24404. int ret = 0;
  24405. XFILE fp = XBADFILE;
  24406. #ifndef NO_RSA
  24407. const char* intCARootRSA = "./certs/ca-cert.der";
  24408. const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
  24409. const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
  24410. const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
  24411. const char* intServKeyRSA = "./certs/server-key.der";
  24412. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
  24413. const char* cli1024Cert = "./certs/1024/client-cert.der";
  24414. const char* cli1024Key = "./certs/1024/client-key.der";
  24415. #endif
  24416. #endif
  24417. #ifdef HAVE_ECC
  24418. const char* intCARootECC = "./certs/ca-ecc-cert.der";
  24419. const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
  24420. const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
  24421. const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
  24422. const char* intServKeyECC = "./certs/ecc-key.der";
  24423. #ifndef USE_CERT_BUFFERS_256
  24424. const char* cliEccCert = "./certs/client-ecc-cert.der";
  24425. const char* cliEccKey = "./certs/client-ecc-key.der";
  24426. #endif
  24427. #endif
  24428. if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
  24429. ((useIntermediateCertChain == 1) &&
  24430. (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
  24431. intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
  24432. return BAD_FUNC_ARG;
  24433. }
  24434. /* Read/load certs and keys to use for signing based on PK type and chain */
  24435. switch (pkAlgoType) {
  24436. #ifndef NO_RSA
  24437. case RSA_TYPE:
  24438. if (useIntermediateCertChain == 1) {
  24439. ExpectTrue((fp = XFOPEN(intCARootRSA, "rb")) != XBADFILE);
  24440. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  24441. if (fp != XBADFILE) {
  24442. XFCLOSE(fp);
  24443. fp = XBADFILE;
  24444. }
  24445. ExpectIntGT(*intCARootSz, 0);
  24446. ExpectTrue((fp = XFOPEN(intCA1RSA, "rb")) != XBADFILE);
  24447. if (fp != XBADFILE) {
  24448. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24449. XFCLOSE(fp);
  24450. fp = XBADFILE;
  24451. }
  24452. ExpectIntGT(*intCA1Sz, 0);
  24453. ExpectTrue((fp = XFOPEN(intCA2RSA, "rb")) != XBADFILE);
  24454. if (fp != XBADFILE) {
  24455. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24456. XFCLOSE(fp);
  24457. fp = XBADFILE;
  24458. }
  24459. ExpectIntGT(*intCA2Sz, 0);
  24460. ExpectTrue((fp = XFOPEN(intServCertRSA, "rb")) != XBADFILE);
  24461. if (fp != XBADFILE) {
  24462. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24463. XFCLOSE(fp);
  24464. fp = XBADFILE;
  24465. }
  24466. ExpectIntGT(*certSz, 0);
  24467. ExpectTrue((fp = XFOPEN(intServKeyRSA, "rb")) != XBADFILE);
  24468. if (fp != XBADFILE) {
  24469. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24470. XFCLOSE(fp);
  24471. fp = XBADFILE;
  24472. }
  24473. ExpectIntGT(*keySz, 0);
  24474. }
  24475. else {
  24476. #if defined(USE_CERT_BUFFERS_2048)
  24477. *keySz = sizeof_client_key_der_2048;
  24478. *certSz = sizeof_client_cert_der_2048;
  24479. XMEMCPY(key, client_key_der_2048, *keySz);
  24480. XMEMCPY(cert, client_cert_der_2048, *certSz);
  24481. #elif defined(USE_CERT_BUFFERS_1024)
  24482. *keySz = sizeof_client_key_der_1024;
  24483. *certSz = sizeof_client_cert_der_1024;
  24484. XMEMCPY(key, client_key_der_1024, *keySz);
  24485. XMEMCPY(cert, client_cert_der_1024, *certSz);
  24486. #else
  24487. ExpectTrue((fp = XFOPEN(cli1024Key, "rb")) != XBADFILE);
  24488. if (fp != XBADFILE) {
  24489. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24490. XFCLOSE(fp);
  24491. fp = XBADFILE;
  24492. }
  24493. ExpectIntGT(*keySz, 0);
  24494. ExpectTrue((fp = XFOPEN(cli1024Cert, "rb")) != XBADFILE);
  24495. if (fp != XBADFILE) {
  24496. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24497. XFCLOSE(fp);
  24498. fp = XBADFILE;
  24499. }
  24500. ExpectIntGT(*certSz, 0);
  24501. #endif /* USE_CERT_BUFFERS_2048 */
  24502. }
  24503. break;
  24504. #endif /* !NO_RSA */
  24505. #ifdef HAVE_ECC
  24506. case ECC_TYPE:
  24507. if (useIntermediateCertChain == 1) {
  24508. ExpectTrue((fp = XFOPEN(intCARootECC, "rb")) != XBADFILE);
  24509. if (fp != XBADFILE) {
  24510. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz,
  24511. fp);
  24512. XFCLOSE(fp);
  24513. fp = XBADFILE;
  24514. }
  24515. ExpectIntGT(*intCARootSz, 0);
  24516. ExpectTrue((fp = XFOPEN(intCA1ECC, "rb")) != XBADFILE);
  24517. if (fp != XBADFILE) {
  24518. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24519. XFCLOSE(fp);
  24520. fp = XBADFILE;
  24521. }
  24522. ExpectIntGT(*intCA1Sz, 0);
  24523. ExpectTrue((fp = XFOPEN(intCA2ECC, "rb")) != XBADFILE);
  24524. if (fp != XBADFILE) {
  24525. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24526. XFCLOSE(fp);
  24527. fp = XBADFILE;
  24528. }
  24529. ExpectIntGT(*intCA2Sz, 0);
  24530. ExpectTrue((fp = XFOPEN(intServCertECC, "rb")) != XBADFILE);
  24531. if (fp != XBADFILE) {
  24532. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24533. XFCLOSE(fp);
  24534. fp = XBADFILE;
  24535. }
  24536. ExpectIntGT(*certSz, 0);
  24537. ExpectTrue((fp = XFOPEN(intServKeyECC, "rb")) != XBADFILE);
  24538. if (fp != XBADFILE) {
  24539. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24540. XFCLOSE(fp);
  24541. fp = XBADFILE;
  24542. }
  24543. ExpectIntGT(*keySz, 0);
  24544. }
  24545. else {
  24546. #if defined(USE_CERT_BUFFERS_256)
  24547. *keySz = sizeof_ecc_clikey_der_256;
  24548. *certSz = sizeof_cliecc_cert_der_256;
  24549. XMEMCPY(key, ecc_clikey_der_256, *keySz);
  24550. XMEMCPY(cert, cliecc_cert_der_256, *certSz);
  24551. #else
  24552. ExpectTrue((fp = XFOPEN(cliEccKey, "rb")) != XBADFILE);
  24553. if (fp != XBADFILE) {
  24554. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24555. XFCLOSE(fp);
  24556. fp = XBADFILE;
  24557. }
  24558. ExpectIntGT(*keySz, 0);
  24559. ExpectTrue((fp = XFOPEN(cliEccCert, "rb")) != XBADFILE);
  24560. if (fp != XBADFILE) {
  24561. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24562. XFCLOSE(fp);
  24563. fp = XBADFILE;
  24564. }
  24565. ExpectIntGT(*certSz, 0);
  24566. #endif /* USE_CERT_BUFFERS_256 */
  24567. }
  24568. break;
  24569. #endif /* HAVE_ECC */
  24570. default:
  24571. WOLFSSL_MSG("Unsupported SignedData PK type");
  24572. ret = BAD_FUNC_ARG;
  24573. break;
  24574. }
  24575. if (EXPECT_FAIL() && (ret == 0)) {
  24576. ret = BAD_FUNC_ARG;
  24577. }
  24578. return ret;
  24579. }
  24580. /**
  24581. * Creates a PKCS7/CMS SignedData bundle to use for testing.
  24582. *
  24583. * output output buffer to place SignedData
  24584. * outputSz size of output buffer
  24585. * data data buffer to be signed
  24586. * dataSz size of data buffer
  24587. * withAttribs [1/0] include attributes in SignedData message
  24588. * detachedSig [1/0] create detached signature, no content
  24589. * useIntCertChain [1/0] use certificate chain and include intermediate and
  24590. * root CAs in bundle
  24591. * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
  24592. *
  24593. * Return size of bundle created on success, negative on error */
  24594. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  24595. byte* data, word32 dataSz,
  24596. int withAttribs, int detachedSig,
  24597. int useIntermediateCertChain,
  24598. int pkAlgoType)
  24599. {
  24600. EXPECT_DECLS;
  24601. int ret = 0;
  24602. WC_RNG rng;
  24603. PKCS7* pkcs7 = NULL;
  24604. static byte messageTypeOid[] =
  24605. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  24606. 0x09, 0x02 };
  24607. static byte messageType[] = { 0x13, 2, '1', '9' };
  24608. PKCS7Attrib attribs[] =
  24609. {
  24610. { messageTypeOid, sizeof(messageTypeOid), messageType,
  24611. sizeof(messageType) }
  24612. };
  24613. byte intCARoot[TWOK_BUF];
  24614. byte intCA1[TWOK_BUF];
  24615. byte intCA2[TWOK_BUF];
  24616. byte cert[TWOK_BUF];
  24617. byte key[TWOK_BUF];
  24618. word32 intCARootSz = sizeof(intCARoot);
  24619. word32 intCA1Sz = sizeof(intCA1);
  24620. word32 intCA2Sz = sizeof(intCA2);
  24621. word32 certSz = sizeof(cert);
  24622. word32 keySz = sizeof(key);
  24623. XMEMSET(intCARoot, 0, intCARootSz);
  24624. XMEMSET(intCA1, 0, intCA1Sz);
  24625. XMEMSET(intCA2, 0, intCA2Sz);
  24626. XMEMSET(cert, 0, certSz);
  24627. XMEMSET(key, 0, keySz);
  24628. ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
  24629. intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
  24630. cert, &certSz, key, &keySz);
  24631. ExpectIntEQ(ret, 0);
  24632. XMEMSET(output, 0, outputSz);
  24633. ExpectIntEQ(wc_InitRng(&rng), 0);
  24634. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24635. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24636. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24637. if (useIntermediateCertChain == 1) {
  24638. /* Add intermediate and root CA certs into SignedData Certs SET */
  24639. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
  24640. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
  24641. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
  24642. }
  24643. if (pkcs7 != NULL) {
  24644. pkcs7->content = data;
  24645. pkcs7->contentSz = dataSz;
  24646. pkcs7->privateKey = key;
  24647. pkcs7->privateKeySz = (word32)sizeof(key);
  24648. if (pkAlgoType == RSA_TYPE) {
  24649. pkcs7->encryptOID = RSAk;
  24650. }
  24651. else {
  24652. pkcs7->encryptOID = ECDSAk;
  24653. }
  24654. #ifdef NO_SHA
  24655. pkcs7->hashOID = SHA256h;
  24656. #else
  24657. pkcs7->hashOID = SHAh;
  24658. #endif
  24659. pkcs7->rng = &rng;
  24660. if (withAttribs) {
  24661. /* include a signed attribute */
  24662. pkcs7->signedAttribs = attribs;
  24663. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  24664. }
  24665. }
  24666. if (detachedSig) {
  24667. ExpectIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  24668. }
  24669. outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz);
  24670. ExpectIntGT(outputSz, 0);
  24671. wc_PKCS7_Free(pkcs7);
  24672. pkcs7 = NULL;
  24673. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24674. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24675. if (detachedSig && (pkcs7 != NULL)) {
  24676. pkcs7->content = data;
  24677. pkcs7->contentSz = dataSz;
  24678. }
  24679. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24680. wc_PKCS7_Free(pkcs7);
  24681. wc_FreeRng(&rng);
  24682. if (EXPECT_FAIL()) {
  24683. outputSz = 0;
  24684. }
  24685. return outputSz;
  24686. }
  24687. #endif
  24688. /*
  24689. * Testing wc_PKCS_VerifySignedData()
  24690. */
  24691. static int test_wc_PKCS7_VerifySignedData_RSA(void)
  24692. {
  24693. EXPECT_DECLS;
  24694. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  24695. PKCS7* pkcs7 = NULL;
  24696. byte output[6000]; /* Large size needed for bundles with int CA certs */
  24697. word32 outputSz = sizeof(output);
  24698. byte data[] = "Test data to encode.";
  24699. byte badOut[1];
  24700. word32 badOutSz = 0;
  24701. byte badContent[] = "This is different content than was signed";
  24702. wc_HashAlg hash;
  24703. #ifdef NO_SHA
  24704. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  24705. #else
  24706. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24707. #endif
  24708. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24709. word32 hashSz = wc_HashGetDigestSize(hashType);
  24710. #ifndef NO_RSA
  24711. PKCS7DecodedAttrib* decodedAttrib = NULL;
  24712. /* contentType OID (1.2.840.113549.1.9.3) */
  24713. static const byte contentTypeOid[] =
  24714. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01, 0x09, 0x03 };
  24715. /* PKCS#7 DATA content type (contentType defaults to DATA) */
  24716. static const byte dataType[] =
  24717. { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01 };
  24718. /* messageDigest OID (1.2.840.113549.1.9.4) */
  24719. static const byte messageDigestOid[] =
  24720. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04 };
  24721. #ifndef NO_ASN_TIME
  24722. /* signingTime OID () */
  24723. static const byte signingTimeOid[] =
  24724. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05};
  24725. #endif
  24726. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  24727. int dateLength = 0;
  24728. byte dateFormat;
  24729. const byte* datePart = NULL;
  24730. struct tm timearg;
  24731. time_t now;
  24732. struct tm* nowTm = NULL;
  24733. #ifdef NEED_TMP_TIME
  24734. struct tm tmpTimeStorage;
  24735. struct tm* tmpTime = &tmpTimeStorage;
  24736. #endif
  24737. #endif /* !NO_ASN && !NO_ASN_TIME */
  24738. #ifndef NO_PKCS7_STREAM
  24739. word32 z;
  24740. int ret;
  24741. #endif /* !NO_PKCS7_STREAM */
  24742. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  24743. /* Success test with RSA certs/key */
  24744. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24745. (word32)sizeof(data), 0, 0, 0, RSA_TYPE)), 0);
  24746. /* calculate hash for content, used later */
  24747. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  24748. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  24749. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  24750. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  24751. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24752. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24753. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24754. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24755. #ifndef NO_PKCS7_STREAM
  24756. wc_PKCS7_Free(pkcs7);
  24757. pkcs7 = NULL;
  24758. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24759. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24760. /* test for streaming */
  24761. ret = -1;
  24762. for (z = 0; z < outputSz && ret != 0; z++) {
  24763. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24764. if (ret < 0){
  24765. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24766. }
  24767. }
  24768. ExpectIntEQ(ret, 0);
  24769. ExpectIntNE(pkcs7->contentSz, 0);
  24770. ExpectNotNull(pkcs7->contentDynamic);
  24771. #endif /* !NO_PKCS7_STREAM */
  24772. /* Check that decoded signed attributes are correct */
  24773. /* messageDigest should be first */
  24774. if (pkcs7 != NULL) {
  24775. decodedAttrib = pkcs7->decodedAttrib;
  24776. }
  24777. ExpectNotNull(decodedAttrib);
  24778. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(messageDigestOid));
  24779. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, messageDigestOid,
  24780. decodedAttrib->oidSz), 0);
  24781. /* + 2 for OCTET STRING and length bytes */
  24782. ExpectIntEQ(decodedAttrib->valueSz, hashSz + 2);
  24783. ExpectNotNull(decodedAttrib->value);
  24784. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, hashBuf, hashSz), 0);
  24785. #ifndef NO_ASN_TIME
  24786. /* signingTime should be second */
  24787. if (decodedAttrib != NULL) {
  24788. decodedAttrib = decodedAttrib->next;
  24789. }
  24790. ExpectNotNull(decodedAttrib);
  24791. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(signingTimeOid));
  24792. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, signingTimeOid,
  24793. decodedAttrib->oidSz), 0);
  24794. ExpectIntGT(decodedAttrib->valueSz, 0);
  24795. ExpectNotNull(decodedAttrib->value);
  24796. #endif
  24797. /* Verify signingTime if ASN and time are available */
  24798. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  24799. ExpectIntEQ(wc_GetDateInfo(decodedAttrib->value, decodedAttrib->valueSz,
  24800. &datePart, &dateFormat, &dateLength), 0);
  24801. ExpectNotNull(datePart);
  24802. ExpectIntGT(dateLength, 0);
  24803. XMEMSET(&timearg, 0, sizeof(timearg));
  24804. ExpectIntEQ(wc_GetDateAsCalendarTime(datePart, dateLength, dateFormat,
  24805. &timearg), 0);
  24806. /* Get current time and compare year/month/day against attribute value */
  24807. ExpectIntEQ(wc_GetTime(&now, sizeof(now)), 0);
  24808. nowTm = (struct tm*)XGMTIME((time_t*)&now, tmpTime);
  24809. ExpectNotNull(nowTm);
  24810. ExpectIntEQ(timearg.tm_year, nowTm->tm_year);
  24811. ExpectIntEQ(timearg.tm_mon, nowTm->tm_mon);
  24812. ExpectIntEQ(timearg.tm_mday, nowTm->tm_mday);
  24813. #endif /* !NO_ASN && !NO_ASN_TIME */
  24814. /* contentType should be third */
  24815. if (decodedAttrib != NULL) {
  24816. decodedAttrib = decodedAttrib->next;
  24817. }
  24818. ExpectNotNull(decodedAttrib);
  24819. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(contentTypeOid));
  24820. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, contentTypeOid,
  24821. decodedAttrib->oidSz), 0);
  24822. ExpectIntEQ(decodedAttrib->valueSz, (int)sizeof(dataType) + 2);
  24823. ExpectNotNull(decodedAttrib->value);
  24824. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, dataType, sizeof(dataType)),
  24825. 0);
  24826. #endif /* !NO_RSA */
  24827. /* Test bad args. */
  24828. ExpectIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
  24829. BAD_FUNC_ARG);
  24830. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
  24831. BAD_FUNC_ARG);
  24832. #ifndef NO_PKCS7_STREAM
  24833. /* can pass in 0 buffer length with streaming API */
  24834. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  24835. badOutSz), WC_PKCS7_WANT_READ_E);
  24836. #else
  24837. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  24838. badOutSz), BAD_FUNC_ARG);
  24839. #endif
  24840. wc_PKCS7_Free(pkcs7);
  24841. pkcs7 = NULL;
  24842. #ifndef NO_RSA
  24843. /* Try RSA certs/key/sig first */
  24844. outputSz = sizeof(output);
  24845. XMEMSET(output, 0, outputSz);
  24846. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24847. (word32)sizeof(data),
  24848. 1, 1, 0, RSA_TYPE)), 0);
  24849. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24850. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24851. if (pkcs7 != NULL) {
  24852. pkcs7->content = badContent;
  24853. pkcs7->contentSz = sizeof(badContent);
  24854. }
  24855. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  24856. SIG_VERIFY_E);
  24857. wc_PKCS7_Free(pkcs7);
  24858. pkcs7 = NULL;
  24859. #ifndef NO_PKCS7_STREAM
  24860. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24861. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24862. if (pkcs7 != NULL) {
  24863. pkcs7->content = badContent;
  24864. pkcs7->contentSz = sizeof(badContent);
  24865. }
  24866. /* test for streaming */
  24867. ret = -1;
  24868. for (z = 0; z < outputSz && ret != 0; z++) {
  24869. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24870. if (ret == WC_PKCS7_WANT_READ_E){
  24871. continue;
  24872. }
  24873. else if (ret < 0) {
  24874. break;
  24875. }
  24876. }
  24877. ExpectIntEQ(ret, SIG_VERIFY_E);
  24878. ExpectIntNE(pkcs7->contentSz, 0);
  24879. ExpectNotNull(pkcs7->contentDynamic);
  24880. wc_PKCS7_Free(pkcs7);
  24881. pkcs7 = NULL;
  24882. #endif /* !NO_PKCS7_STREAM */
  24883. /* Test success case with detached signature and valid content */
  24884. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24885. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24886. if (pkcs7 != NULL) {
  24887. pkcs7->content = data;
  24888. pkcs7->contentSz = sizeof(data);
  24889. }
  24890. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24891. wc_PKCS7_Free(pkcs7);
  24892. pkcs7 = NULL;
  24893. #ifndef NO_PKCS7_STREAM
  24894. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24895. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24896. if (pkcs7 != NULL) {
  24897. pkcs7->content = data;
  24898. pkcs7->contentSz = sizeof(data);
  24899. }
  24900. /* test for streaming */
  24901. ret = -1;
  24902. for (z = 0; z < outputSz && ret != 0; z++) {
  24903. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24904. if (ret < 0){
  24905. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24906. }
  24907. }
  24908. ExpectIntEQ(ret, 0);
  24909. ExpectIntNE(pkcs7->contentSz, 0);
  24910. ExpectNotNull(pkcs7->contentDynamic);
  24911. wc_PKCS7_Free(pkcs7);
  24912. pkcs7 = NULL;
  24913. #endif /* !NO_PKCS7_STREAM */
  24914. /* verify using pre-computed content digest only (no content) */
  24915. {
  24916. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24917. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  24918. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24919. output, outputSz, NULL, 0), 0);
  24920. wc_PKCS7_Free(pkcs7);
  24921. pkcs7 = NULL;
  24922. }
  24923. #endif /* !NO_RSA */
  24924. /* Test verify on signedData containing intermediate/root CA certs */
  24925. #ifndef NO_RSA
  24926. outputSz = sizeof(output);
  24927. XMEMSET(output, 0, outputSz);
  24928. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24929. (word32)sizeof(data),
  24930. 0, 0, 1, RSA_TYPE)), 0);
  24931. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24932. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24933. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24934. wc_PKCS7_Free(pkcs7);
  24935. pkcs7 = NULL;
  24936. #ifndef NO_PKCS7_STREAM
  24937. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24938. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24939. /* test for streaming */
  24940. ret = -1;
  24941. for (z = 0; z < outputSz && ret != 0; z++) {
  24942. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24943. if (ret < 0){
  24944. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24945. }
  24946. }
  24947. ExpectIntEQ(ret, 0);
  24948. ExpectIntNE(pkcs7->contentSz, 0);
  24949. ExpectNotNull(pkcs7->contentDynamic);
  24950. wc_PKCS7_Free(pkcs7);
  24951. pkcs7 = NULL;
  24952. #endif /* !NO_PKCS7_STREAM */
  24953. #endif /* !NO_RSA */
  24954. #if defined(ASN_BER_TO_DER) && !defined(NO_PKCS7_STREAM) && \
  24955. !defined(NO_FILESYSTEM)
  24956. {
  24957. XFILE signedBundle = XBADFILE;
  24958. int signedBundleSz = 0;
  24959. int chunkSz = 1;
  24960. int i, rc = 0;
  24961. byte* buf = NULL;
  24962. ExpectTrue((signedBundle = XFOPEN("./certs/test-stream-sign.p7b",
  24963. "rb")) != XBADFILE);
  24964. ExpectTrue(XFSEEK(signedBundle, 0, XSEEK_END) == 0);
  24965. ExpectIntGT(signedBundleSz = (int)XFTELL(signedBundle), 0);
  24966. ExpectTrue(XFSEEK(signedBundle, 0, XSEEK_SET) == 0);
  24967. ExpectNotNull(buf = (byte*)XMALLOC(signedBundleSz, HEAP_HINT,
  24968. DYNAMIC_TYPE_FILE));
  24969. if (buf != NULL) {
  24970. ExpectIntEQ(XFREAD(buf, 1, signedBundleSz, signedBundle),
  24971. signedBundleSz);
  24972. }
  24973. if (signedBundle != XBADFILE) {
  24974. XFCLOSE(signedBundle);
  24975. signedBundle = XBADFILE;
  24976. }
  24977. if (buf != NULL) {
  24978. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24979. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24980. for (i = 0; i < signedBundleSz;) {
  24981. int sz = (i + chunkSz > signedBundleSz)? signedBundleSz - i :
  24982. chunkSz;
  24983. rc = wc_PKCS7_VerifySignedData(pkcs7, buf + i, sz);
  24984. if (rc < 0 ) {
  24985. if (rc == WC_PKCS7_WANT_READ_E) {
  24986. i += sz;
  24987. continue;
  24988. }
  24989. break;
  24990. }
  24991. else {
  24992. break;
  24993. }
  24994. }
  24995. ExpectIntEQ(rc, PKCS7_SIGNEEDS_CHECK);
  24996. wc_PKCS7_Free(pkcs7);
  24997. pkcs7 = NULL;
  24998. }
  24999. /* now try with malformed bundle */
  25000. if (buf != NULL) {
  25001. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25002. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25003. buf[signedBundleSz - 2] = buf[signedBundleSz - 2] + 1;
  25004. for (i = 0; i < signedBundleSz;) {
  25005. int sz = (i + chunkSz > signedBundleSz)? signedBundleSz - i :
  25006. chunkSz;
  25007. rc = wc_PKCS7_VerifySignedData(pkcs7, buf + i, sz);
  25008. if (rc < 0 ) {
  25009. if (rc == WC_PKCS7_WANT_READ_E) {
  25010. i += sz;
  25011. continue;
  25012. }
  25013. break;
  25014. }
  25015. else {
  25016. break;
  25017. }
  25018. }
  25019. ExpectIntEQ(rc, ASN_PARSE_E);
  25020. wc_PKCS7_Free(pkcs7);
  25021. pkcs7 = NULL;
  25022. }
  25023. if (buf != NULL)
  25024. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  25025. }
  25026. #endif /* BER and stream */
  25027. #endif
  25028. return EXPECT_RESULT();
  25029. } /* END test_wc_PKCS7_VerifySignedData()_RSA */
  25030. /*
  25031. * Testing wc_PKCS_VerifySignedData()
  25032. */
  25033. static int test_wc_PKCS7_VerifySignedData_ECC(void)
  25034. {
  25035. EXPECT_DECLS;
  25036. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  25037. PKCS7* pkcs7 = NULL;
  25038. byte output[6000]; /* Large size needed for bundles with int CA certs */
  25039. word32 outputSz = sizeof(output);
  25040. byte data[] = "Test data to encode.";
  25041. byte badContent[] = "This is different content than was signed";
  25042. wc_HashAlg hash;
  25043. #ifndef NO_PKCS7_STREAM
  25044. word32 z;
  25045. int ret;
  25046. #endif /* !NO_PKCS7_STREAM */
  25047. #ifdef NO_SHA
  25048. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  25049. #else
  25050. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  25051. #endif
  25052. byte hashBuf[WC_MAX_DIGEST_SIZE];
  25053. word32 hashSz = wc_HashGetDigestSize(hashType);
  25054. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  25055. /* Success test with ECC certs/key */
  25056. outputSz = sizeof(output);
  25057. XMEMSET(output, 0, outputSz);
  25058. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25059. (word32)sizeof(data), 0, 0, 0, ECC_TYPE)), 0);
  25060. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25061. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25062. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25063. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25064. wc_PKCS7_Free(pkcs7);
  25065. pkcs7 = NULL;
  25066. #ifndef NO_PKCS7_STREAM
  25067. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25068. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25069. /* test for streaming */
  25070. ret = -1;
  25071. for (z = 0; z < outputSz && ret != 0; z++) {
  25072. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25073. if (ret < 0){
  25074. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25075. }
  25076. }
  25077. ExpectIntEQ(ret, 0);
  25078. ExpectIntNE(pkcs7->contentSz, 0);
  25079. ExpectNotNull(pkcs7->contentDynamic);
  25080. wc_PKCS7_Free(pkcs7);
  25081. pkcs7 = NULL;
  25082. #endif /* !NO_PKCS7_STREAM */
  25083. /* Invalid content should error, use detached signature so we can
  25084. * easily change content */
  25085. outputSz = sizeof(output);
  25086. XMEMSET(output, 0, outputSz);
  25087. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25088. (word32)sizeof(data), 1, 1, 0, ECC_TYPE)), 0);
  25089. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25090. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25091. if (pkcs7 != NULL) {
  25092. pkcs7->content = badContent;
  25093. pkcs7->contentSz = sizeof(badContent);
  25094. }
  25095. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  25096. SIG_VERIFY_E);
  25097. wc_PKCS7_Free(pkcs7);
  25098. pkcs7 = NULL;
  25099. #ifndef NO_PKCS7_STREAM
  25100. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25101. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25102. if (pkcs7 != NULL) {
  25103. pkcs7->content = badContent;
  25104. pkcs7->contentSz = sizeof(badContent);
  25105. }
  25106. /* test for streaming */
  25107. ret = -1;
  25108. for (z = 0; z < outputSz && ret != 0; z++) {
  25109. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25110. if (ret == WC_PKCS7_WANT_READ_E){
  25111. continue;
  25112. }
  25113. else if (ret < 0) {
  25114. break;
  25115. }
  25116. }
  25117. ExpectIntEQ(ret, SIG_VERIFY_E);
  25118. ExpectIntNE(pkcs7->contentSz, 0);
  25119. ExpectNotNull(pkcs7->contentDynamic);
  25120. wc_PKCS7_Free(pkcs7);
  25121. pkcs7 = NULL;
  25122. #endif /* !NO_PKCS7_STREAM */
  25123. /* Test success case with detached signature and valid content */
  25124. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25125. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25126. if (pkcs7 != NULL) {
  25127. pkcs7->content = data;
  25128. pkcs7->contentSz = sizeof(data);
  25129. }
  25130. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25131. wc_PKCS7_Free(pkcs7);
  25132. pkcs7 = NULL;
  25133. #ifndef NO_PKCS7_STREAM
  25134. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25135. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25136. if (pkcs7 != NULL) {
  25137. pkcs7->content = data;
  25138. pkcs7->contentSz = sizeof(data);
  25139. }
  25140. /* test for streaming */
  25141. ret = -1;
  25142. for (z = 0; z < outputSz && ret != 0; z++) {
  25143. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25144. if (ret < 0){
  25145. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25146. }
  25147. }
  25148. ExpectIntEQ(ret, 0);
  25149. ExpectIntNE(pkcs7->contentSz, 0);
  25150. ExpectNotNull(pkcs7->contentDynamic);
  25151. wc_PKCS7_Free(pkcs7);
  25152. pkcs7 = NULL;
  25153. #endif /* !NO_PKCS7_STREAM */
  25154. /* verify using pre-computed content digest only (no content) */
  25155. {
  25156. /* calculate hash for content */
  25157. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  25158. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  25159. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  25160. ExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  25161. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25162. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  25163. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25164. output, outputSz, NULL, 0), 0);
  25165. wc_PKCS7_Free(pkcs7);
  25166. pkcs7 = NULL;
  25167. }
  25168. /* Test verify on signedData containing intermediate/root CA certs */
  25169. outputSz = sizeof(output);
  25170. XMEMSET(output, 0, outputSz);
  25171. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25172. (word32)sizeof(data), 0, 0, 1, ECC_TYPE)), 0);
  25173. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25174. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25175. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25176. wc_PKCS7_Free(pkcs7);
  25177. pkcs7 = NULL;
  25178. #ifndef NO_PKCS7_STREAM
  25179. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25180. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25181. /* test for streaming */
  25182. ret = -1;
  25183. for (z = 0; z < outputSz && ret != 0; z++) {
  25184. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25185. if (ret < 0){
  25186. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25187. }
  25188. }
  25189. ExpectIntEQ(ret, 0);
  25190. ExpectIntNE(pkcs7->contentSz, 0);
  25191. ExpectNotNull(pkcs7->contentDynamic);
  25192. wc_PKCS7_Free(pkcs7);
  25193. pkcs7 = NULL;
  25194. #endif /* !NO_PKCS7_STREAM */
  25195. #endif
  25196. return EXPECT_RESULT();
  25197. } /* END test_wc_PKCS7_VerifySignedData_ECC() */
  25198. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  25199. !defined(NO_AES_256)
  25200. static const byte defKey[] = {
  25201. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25202. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25203. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25204. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25205. };
  25206. static byte aesHandle[32]; /* simulated hardware key handle */
  25207. /* return 0 on success */
  25208. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  25209. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  25210. byte* in, int inSz, byte* out, void* usrCtx)
  25211. {
  25212. int ret;
  25213. Aes aes;
  25214. if (usrCtx == NULL) {
  25215. /* no simulated handle passed in */
  25216. return -1;
  25217. }
  25218. switch (encryptOID) {
  25219. case AES256CBCb:
  25220. if (ivSz != AES_BLOCK_SIZE)
  25221. return BAD_FUNC_ARG;
  25222. break;
  25223. default:
  25224. WOLFSSL_MSG("Unsupported content cipher type for test");
  25225. return ALGO_ID_E;
  25226. };
  25227. /* simulate using handle to get key */
  25228. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  25229. if (ret == 0) {
  25230. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  25231. if (ret == 0)
  25232. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  25233. wc_AesFree(&aes);
  25234. }
  25235. (void)aad;
  25236. (void)aadSz;
  25237. (void)authTag;
  25238. (void)authTagSz;
  25239. (void)pkcs7;
  25240. return ret;
  25241. }
  25242. /* returns key size on success */
  25243. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  25244. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  25245. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  25246. {
  25247. int ret = -1;
  25248. (void)cekSz;
  25249. (void)cek;
  25250. (void)outSz;
  25251. (void)keyIdSz;
  25252. (void)direction;
  25253. (void)orginKey; /* used with KAKRI */
  25254. (void)orginKeySz;
  25255. if (out == NULL)
  25256. return BAD_FUNC_ARG;
  25257. if (keyId[0] != 0x00) {
  25258. return -1;
  25259. }
  25260. if (type != (int)PKCS7_KEKRI) {
  25261. return -1;
  25262. }
  25263. switch (keyWrapAlgo) {
  25264. case AES256_WRAP:
  25265. /* simulate setting a handle for later decryption but use key
  25266. * as handle in the test case here */
  25267. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  25268. aesHandle, sizeof(aesHandle), NULL);
  25269. if (ret < 0)
  25270. return ret;
  25271. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  25272. if (ret < 0)
  25273. return ret;
  25274. /* return key size on success */
  25275. return sizeof(defKey);
  25276. default:
  25277. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  25278. return BAD_KEYWRAP_ALG_E;
  25279. };
  25280. }
  25281. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  25282. /*
  25283. * Testing wc_PKCS7_EncodeEnvelopedData()
  25284. */
  25285. static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
  25286. {
  25287. EXPECT_DECLS;
  25288. #if defined(HAVE_PKCS7)
  25289. PKCS7* pkcs7 = NULL;
  25290. #ifdef ASN_BER_TO_DER
  25291. int encodedSz = 0;
  25292. #endif
  25293. #ifdef ECC_TIMING_RESISTANT
  25294. WC_RNG rng;
  25295. #endif
  25296. word32 tempWrd32 = 0;
  25297. byte* tmpBytePtr = NULL;
  25298. const char input[] = "Test data to encode.";
  25299. int i;
  25300. int testSz = 0;
  25301. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  25302. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25303. byte* rsaCert = NULL;
  25304. byte* rsaPrivKey = NULL;
  25305. word32 rsaCertSz;
  25306. word32 rsaPrivKeySz;
  25307. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  25308. !defined(USE_CERT_BUFFERS_2048) )
  25309. static const char* rsaClientCert = "./certs/client-cert.der";
  25310. static const char* rsaClientKey = "./certs/client-key.der";
  25311. rsaCertSz = (word32)sizeof(rsaClientCert);
  25312. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  25313. #endif
  25314. #endif
  25315. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25316. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25317. byte* eccCert = NULL;
  25318. byte* eccPrivKey = NULL;
  25319. word32 eccCertSz;
  25320. word32 eccPrivKeySz;
  25321. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  25322. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  25323. static const char* eccClientKey = "./certs/ecc-client-key.der";
  25324. #endif
  25325. #endif
  25326. /* Generic buffer size. */
  25327. byte output[ONEK_BUF];
  25328. byte decoded[sizeof(input)/sizeof(char)];
  25329. int decodedSz = 0;
  25330. #ifndef NO_FILESYSTEM
  25331. XFILE certFile = XBADFILE;
  25332. XFILE keyFile = XBADFILE;
  25333. #endif
  25334. #ifdef ECC_TIMING_RESISTANT
  25335. XMEMSET(&rng, 0, sizeof(WC_RNG));
  25336. #endif
  25337. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25338. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25339. /* RSA certs and keys. */
  25340. #if defined(USE_CERT_BUFFERS_1024)
  25341. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  25342. /* Allocate buffer space. */
  25343. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  25344. DYNAMIC_TYPE_TMP_BUFFER));
  25345. /* Init buffer. */
  25346. if (rsaCert != NULL) {
  25347. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  25348. }
  25349. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  25350. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  25351. DYNAMIC_TYPE_TMP_BUFFER));
  25352. if (rsaPrivKey != NULL) {
  25353. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  25354. }
  25355. #elif defined(USE_CERT_BUFFERS_2048)
  25356. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  25357. /* Allocate buffer */
  25358. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  25359. DYNAMIC_TYPE_TMP_BUFFER));
  25360. /* Init buffer. */
  25361. if (rsaCert != NULL) {
  25362. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  25363. }
  25364. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  25365. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  25366. DYNAMIC_TYPE_TMP_BUFFER));
  25367. if (rsaPrivKey != NULL) {
  25368. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  25369. }
  25370. #else
  25371. /* File system. */
  25372. ExpectTrue((certFile = XFOPEN(rsaClientCert, "rb")) != XBADFILE);
  25373. rsaCertSz = (word32)FOURK_BUF;
  25374. ExpectNotNull(rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25375. DYNAMIC_TYPE_TMP_BUFFER));
  25376. ExpectTrue((rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz,
  25377. certFile)) > 0);
  25378. if (certFile != XBADFILE)
  25379. XFCLOSE(certFile);
  25380. ExpectTrue((keyFile = XFOPEN(rsaClientKey, "rb")) != XBADFILE);
  25381. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25382. DYNAMIC_TYPE_TMP_BUFFER));
  25383. rsaPrivKeySz = (word32)FOURK_BUF;
  25384. ExpectTrue((rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz,
  25385. keyFile)) > 0);
  25386. if (keyFile != XBADFILE)
  25387. XFCLOSE(keyFile);
  25388. #endif /* USE_CERT_BUFFERS */
  25389. #endif /* NO_RSA */
  25390. /* ECC */
  25391. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25392. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25393. #ifdef USE_CERT_BUFFERS_256
  25394. ExpectNotNull(eccCert = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  25395. DYNAMIC_TYPE_TMP_BUFFER));
  25396. /* Init buffer. */
  25397. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  25398. if (eccCert != NULL) {
  25399. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  25400. }
  25401. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  25402. DYNAMIC_TYPE_TMP_BUFFER));
  25403. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  25404. if (eccPrivKey != NULL) {
  25405. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  25406. }
  25407. #else /* File system. */
  25408. ExpectTrue((certFile = XFOPEN(eccClientCert, "rb")) != XBADFILE);
  25409. eccCertSz = (word32)FOURK_BUF;
  25410. ExpectNotNull(eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25411. DYNAMIC_TYPE_TMP_BUFFER));
  25412. ExpectTrue((eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz,
  25413. certFile)) > 0);
  25414. if (certFile != XBADFILE) {
  25415. XFCLOSE(certFile);
  25416. }
  25417. ExpectTrue((keyFile = XFOPEN(eccClientKey, "rb")) != XBADFILE);
  25418. eccPrivKeySz = (word32)FOURK_BUF;
  25419. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25420. DYNAMIC_TYPE_TMP_BUFFER));
  25421. ExpectTrue((eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz,
  25422. keyFile)) > 0);
  25423. if (keyFile != XBADFILE) {
  25424. XFCLOSE(keyFile);
  25425. }
  25426. #endif /* USE_CERT_BUFFERS_256 */
  25427. #endif /* END HAVE_ECC */
  25428. #ifndef NO_FILESYSTEM
  25429. /* Silence. */
  25430. (void)keyFile;
  25431. (void)certFile;
  25432. #endif
  25433. {
  25434. const pkcs7EnvelopedVector testVectors[] = {
  25435. /* DATA is a global variable defined in the makefile. */
  25436. #if !defined(NO_RSA)
  25437. #ifndef NO_DES3
  25438. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  25439. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25440. #endif /* NO_DES3 */
  25441. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25442. #ifndef NO_AES_128
  25443. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  25444. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25445. #endif
  25446. #ifndef NO_AES_192
  25447. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  25448. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25449. #endif
  25450. #ifndef NO_AES_256
  25451. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  25452. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25453. #endif
  25454. #endif /* NO_AES && HAVE_AES_CBC */
  25455. #endif /* NO_RSA */
  25456. #if defined(HAVE_ECC)
  25457. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25458. #if !defined(NO_SHA) && !defined(NO_AES_128)
  25459. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25460. AES128CBCb, AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme,
  25461. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25462. #endif
  25463. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  25464. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25465. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme,
  25466. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25467. #endif
  25468. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  25469. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25470. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme,
  25471. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25472. #endif
  25473. #endif /* NO_AES && HAVE_AES_CBC*/
  25474. #endif /* END HAVE_ECC */
  25475. }; /* END pkcs7EnvelopedVector */
  25476. #ifdef ECC_TIMING_RESISTANT
  25477. ExpectIntEQ(wc_InitRng(&rng), 0);
  25478. #endif
  25479. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25480. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25481. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  25482. for (i = 0; i < testSz; i++) {
  25483. #ifdef ASN_BER_TO_DER
  25484. encodeSignedDataStream strm;
  25485. /* test setting stream mode, the first one using IO callbacks */
  25486. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  25487. (word32)(testVectors + i)->certSz), 0);
  25488. if (pkcs7 != NULL) {
  25489. #ifdef ECC_TIMING_RESISTANT
  25490. pkcs7->rng = &rng;
  25491. #endif
  25492. if (i != 0)
  25493. pkcs7->content = (byte*)(testVectors + i)->content;
  25494. pkcs7->contentSz = (testVectors + i)->contentSz;
  25495. pkcs7->contentOID = (testVectors + i)->contentOID;
  25496. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  25497. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  25498. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  25499. pkcs7->privateKey = (testVectors + i)->privateKey;
  25500. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  25501. }
  25502. if (i == 0) {
  25503. XMEMSET(&strm, 0, sizeof(strm));
  25504. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
  25505. StreamOutputCB, (void*)&strm), 0);
  25506. encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL, 0);
  25507. }
  25508. else {
  25509. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL), 0);
  25510. encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25511. (word32)sizeof(output));
  25512. }
  25513. switch ((testVectors + i)->encryptOID) {
  25514. #ifndef NO_DES3
  25515. case DES3b:
  25516. case DESb:
  25517. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25518. break;
  25519. #endif
  25520. #ifdef HAVE_AESCCM
  25521. #ifdef WOLFSSL_AES_128
  25522. case AES128CCMb:
  25523. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25524. break;
  25525. #endif
  25526. #ifdef WOLFSSL_AES_192
  25527. case AES192CCMb:
  25528. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25529. break;
  25530. #endif
  25531. #ifdef WOLFSSL_AES_256
  25532. case AES256CCMb:
  25533. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25534. break;
  25535. #endif
  25536. #endif
  25537. default:
  25538. ExpectIntGE(encodedSz, 0);
  25539. }
  25540. if (encodedSz > 0) {
  25541. if (i == 0) {
  25542. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7,
  25543. strm.out, (word32)encodedSz, decoded,
  25544. (word32)sizeof(decoded));
  25545. }
  25546. else {
  25547. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25548. (word32)encodedSz, decoded, (word32)sizeof(decoded));
  25549. }
  25550. ExpectIntGE(decodedSz, 0);
  25551. /* Verify the size of each buffer. */
  25552. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  25553. }
  25554. wc_PKCS7_Free(pkcs7);
  25555. pkcs7 = NULL;
  25556. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25557. #endif
  25558. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  25559. (word32)(testVectors + i)->certSz), 0);
  25560. if (pkcs7 != NULL) {
  25561. #ifdef ECC_TIMING_RESISTANT
  25562. pkcs7->rng = &rng;
  25563. #endif
  25564. pkcs7->content = (byte*)(testVectors + i)->content;
  25565. pkcs7->contentSz = (testVectors + i)->contentSz;
  25566. pkcs7->contentOID = (testVectors + i)->contentOID;
  25567. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  25568. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  25569. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  25570. pkcs7->privateKey = (testVectors + i)->privateKey;
  25571. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  25572. }
  25573. #ifdef ASN_BER_TO_DER
  25574. /* test without setting stream mode */
  25575. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
  25576. #endif
  25577. ExpectIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25578. (word32)sizeof(output)), 0);
  25579. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25580. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  25581. ExpectIntGE(decodedSz, 0);
  25582. /* Verify the size of each buffer. */
  25583. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  25584. /* Don't free the last time through the loop. */
  25585. if (i < testSz - 1) {
  25586. wc_PKCS7_Free(pkcs7);
  25587. pkcs7 = NULL;
  25588. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25589. }
  25590. } /* END test loop. */
  25591. }
  25592. /* Test bad args. */
  25593. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  25594. (word32)sizeof(output)), BAD_FUNC_ARG);
  25595. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  25596. (word32)sizeof(output)), BAD_FUNC_ARG);
  25597. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  25598. /* Decode. */
  25599. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  25600. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25601. BAD_FUNC_ARG);
  25602. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25603. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25604. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25605. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  25606. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  25607. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25608. BAD_FUNC_ARG);
  25609. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  25610. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25611. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  25612. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  25613. /* only a failure for KARI test cases */
  25614. if (pkcs7 != NULL) {
  25615. tempWrd32 = pkcs7->singleCertSz;
  25616. pkcs7->singleCertSz = 0;
  25617. }
  25618. #if defined(WOLFSSL_ASN_TEMPLATE)
  25619. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25620. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25621. BUFFER_E);
  25622. #else
  25623. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25624. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25625. ASN_PARSE_E);
  25626. #endif
  25627. if (pkcs7 != NULL) {
  25628. pkcs7->singleCertSz = tempWrd32;
  25629. tmpBytePtr = pkcs7->singleCert;
  25630. pkcs7->singleCert = NULL;
  25631. }
  25632. #ifndef NO_RSA
  25633. #if defined(NO_PKCS7_STREAM)
  25634. /* when none streaming mode is used and PKCS7 is in bad state buffer error
  25635. * is returned from kari parse which gets set to bad func arg */
  25636. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25637. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25638. BAD_FUNC_ARG);
  25639. #else
  25640. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25641. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25642. ASN_PARSE_E);
  25643. #endif
  25644. #endif /* !NO_RSA */
  25645. if (pkcs7 != NULL) {
  25646. pkcs7->singleCert = tmpBytePtr;
  25647. }
  25648. #endif
  25649. if (pkcs7 != NULL) {
  25650. tempWrd32 = pkcs7->privateKeySz;
  25651. pkcs7->privateKeySz = 0;
  25652. }
  25653. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25654. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25655. BAD_FUNC_ARG);
  25656. if (pkcs7 != NULL) {
  25657. pkcs7->privateKeySz = tempWrd32;
  25658. tmpBytePtr = pkcs7->privateKey;
  25659. pkcs7->privateKey = NULL;
  25660. }
  25661. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25662. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25663. BAD_FUNC_ARG);
  25664. if (pkcs7 != NULL) {
  25665. pkcs7->privateKey = tmpBytePtr;
  25666. }
  25667. wc_PKCS7_Free(pkcs7);
  25668. pkcs7 = NULL;
  25669. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  25670. /* test of decrypt callback with KEKRI enveloped data */
  25671. {
  25672. int envelopedSz = 0;
  25673. const byte keyId[] = { 0x00 };
  25674. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25675. if (pkcs7 != NULL) {
  25676. pkcs7->content = (byte*)input;
  25677. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  25678. pkcs7->contentOID = DATA;
  25679. pkcs7->encryptOID = AES256CBCb;
  25680. }
  25681. ExpectIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  25682. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  25683. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  25684. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  25685. ExpectIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25686. (word32)sizeof(output))), 0);
  25687. wc_PKCS7_Free(pkcs7);
  25688. pkcs7 = NULL;
  25689. /* decode envelopedData */
  25690. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25691. ExpectIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  25692. ExpectIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  25693. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25694. envelopedSz, decoded, sizeof(decoded))), 0);
  25695. wc_PKCS7_Free(pkcs7);
  25696. pkcs7 = NULL;
  25697. }
  25698. #endif /* !NO_AES && !NO_AES_256 */
  25699. #ifndef NO_RSA
  25700. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25701. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25702. #endif /* NO_RSA */
  25703. #ifdef HAVE_ECC
  25704. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25705. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25706. #endif /* HAVE_ECC */
  25707. #ifdef ECC_TIMING_RESISTANT
  25708. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  25709. #endif
  25710. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && \
  25711. !defined(NO_RSA) && !defined(NO_SHA)
  25712. {
  25713. byte out[7];
  25714. byte *cms = NULL;
  25715. word32 cmsSz;
  25716. XFILE cmsFile = XBADFILE;
  25717. XMEMSET(out, 0, sizeof(out));
  25718. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25719. ExpectTrue((cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb"))
  25720. != XBADFILE);
  25721. cmsSz = (word32)FOURK_BUF;
  25722. ExpectNotNull(cms = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25723. DYNAMIC_TYPE_TMP_BUFFER));
  25724. ExpectTrue((cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile)) > 0);
  25725. if (cmsFile != XBADFILE)
  25726. XFCLOSE(cmsFile);
  25727. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  25728. sizeof_client_cert_der_2048), 0);
  25729. if (pkcs7 != NULL) {
  25730. pkcs7->privateKey = (byte*)client_key_der_2048;
  25731. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  25732. }
  25733. ExpectIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  25734. 2), 0);
  25735. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  25736. sizeof(out)), 0);
  25737. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25738. ExpectIntEQ(XMEMCMP(out, "test", 4), 0);
  25739. wc_PKCS7_Free(pkcs7);
  25740. pkcs7 = NULL;
  25741. }
  25742. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 && !NO_RSA && !NO_SHA */
  25743. #endif /* HAVE_PKCS7 */
  25744. return EXPECT_RESULT();
  25745. } /* END test_wc_PKCS7_EncodeDecodeEnvelopedData() */
  25746. /*
  25747. * Testing wc_PKCS7_EncodeEncryptedData()
  25748. */
  25749. static int test_wc_PKCS7_EncodeEncryptedData(void)
  25750. {
  25751. EXPECT_DECLS;
  25752. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  25753. PKCS7* pkcs7 = NULL;
  25754. byte* tmpBytePtr = NULL;
  25755. byte encrypted[TWOK_BUF];
  25756. byte decoded[TWOK_BUF];
  25757. word32 tmpWrd32 = 0;
  25758. int tmpInt = 0;
  25759. int decodedSz = 0;
  25760. int encryptedSz = 0;
  25761. int testSz = 0;
  25762. int i = 0;
  25763. const byte data[] = { /* Hello World */
  25764. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  25765. 0x72,0x6c,0x64
  25766. };
  25767. #ifndef NO_DES3
  25768. byte desKey[] = {
  25769. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  25770. };
  25771. byte des3Key[] = {
  25772. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  25773. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  25774. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  25775. };
  25776. #endif
  25777. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25778. #ifndef NO_AES_128
  25779. byte aes128Key[] = {
  25780. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25781. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25782. };
  25783. #endif
  25784. #ifndef NO_AES_192
  25785. byte aes192Key[] = {
  25786. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25787. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25788. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25789. };
  25790. #endif
  25791. #ifndef NO_AES_256
  25792. byte aes256Key[] = {
  25793. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25794. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25795. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25796. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25797. };
  25798. #endif
  25799. #endif /* !NO_AES && HAVE_AES_CBC */
  25800. const pkcs7EncryptedVector testVectors[] =
  25801. {
  25802. #ifndef NO_DES3
  25803. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  25804. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  25805. #endif /* !NO_DES3 */
  25806. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25807. #ifndef NO_AES_128
  25808. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  25809. sizeof(aes128Key)},
  25810. #endif
  25811. #ifndef NO_AES_192
  25812. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  25813. sizeof(aes192Key)},
  25814. #endif
  25815. #ifndef NO_AES_256
  25816. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  25817. sizeof(aes256Key)},
  25818. #endif
  25819. #endif /* !NO_AES && HAVE_AES_CBC */
  25820. };
  25821. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  25822. for (i = 0; i < testSz; i++) {
  25823. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25824. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25825. if (pkcs7 != NULL) {
  25826. pkcs7->content = (byte*)testVectors[i].content;
  25827. pkcs7->contentSz = testVectors[i].contentSz;
  25828. pkcs7->contentOID = testVectors[i].contentOID;
  25829. pkcs7->encryptOID = testVectors[i].encryptOID;
  25830. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  25831. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  25832. pkcs7->heap = HEAP_HINT;
  25833. }
  25834. /* encode encryptedData */
  25835. ExpectIntGT(encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25836. sizeof(encrypted)), 0);
  25837. /* Decode encryptedData */
  25838. ExpectIntGT(decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted,
  25839. encryptedSz, decoded, sizeof(decoded)), 0);
  25840. ExpectIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  25841. /* Keep values for last itr. */
  25842. if (i < testSz - 1) {
  25843. wc_PKCS7_Free(pkcs7);
  25844. pkcs7 = NULL;
  25845. }
  25846. }
  25847. if (pkcs7 == NULL || testSz == 0) {
  25848. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25849. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25850. }
  25851. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  25852. sizeof(encrypted)),BAD_FUNC_ARG);
  25853. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  25854. sizeof(encrypted)), BAD_FUNC_ARG);
  25855. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25856. 0), BAD_FUNC_ARG);
  25857. /* Testing the struct. */
  25858. if (pkcs7 != NULL) {
  25859. tmpBytePtr = pkcs7->content;
  25860. pkcs7->content = NULL;
  25861. }
  25862. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25863. sizeof(encrypted)), BAD_FUNC_ARG);
  25864. if (pkcs7 != NULL) {
  25865. pkcs7->content = tmpBytePtr;
  25866. tmpWrd32 = pkcs7->contentSz;
  25867. pkcs7->contentSz = 0;
  25868. }
  25869. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25870. sizeof(encrypted)), BAD_FUNC_ARG);
  25871. if (pkcs7 != NULL) {
  25872. pkcs7->contentSz = tmpWrd32;
  25873. tmpInt = pkcs7->encryptOID;
  25874. pkcs7->encryptOID = 0;
  25875. }
  25876. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25877. sizeof(encrypted)), BAD_FUNC_ARG);
  25878. if (pkcs7 != NULL) {
  25879. pkcs7->encryptOID = tmpInt;
  25880. tmpBytePtr = pkcs7->encryptionKey;
  25881. pkcs7->encryptionKey = NULL;
  25882. }
  25883. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25884. sizeof(encrypted)), BAD_FUNC_ARG);
  25885. if (pkcs7 != NULL) {
  25886. pkcs7->encryptionKey = tmpBytePtr;
  25887. tmpWrd32 = pkcs7->encryptionKeySz;
  25888. pkcs7->encryptionKeySz = 0;
  25889. }
  25890. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25891. sizeof(encrypted)), BAD_FUNC_ARG);
  25892. if (pkcs7 != NULL) {
  25893. pkcs7->encryptionKeySz = tmpWrd32;
  25894. }
  25895. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  25896. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25897. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  25898. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25899. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  25900. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25901. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25902. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  25903. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25904. decoded, 0), BAD_FUNC_ARG);
  25905. /* Test struct fields */
  25906. if (pkcs7 != NULL) {
  25907. tmpBytePtr = pkcs7->encryptionKey;
  25908. pkcs7->encryptionKey = NULL;
  25909. }
  25910. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25911. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25912. if (pkcs7 != NULL) {
  25913. pkcs7->encryptionKey = tmpBytePtr;
  25914. pkcs7->encryptionKeySz = 0;
  25915. }
  25916. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25917. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25918. wc_PKCS7_Free(pkcs7);
  25919. #endif
  25920. return EXPECT_RESULT();
  25921. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  25922. /*
  25923. * Testing wc_PKCS7_Degenerate()
  25924. */
  25925. static int test_wc_PKCS7_Degenerate(void)
  25926. {
  25927. EXPECT_DECLS;
  25928. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  25929. PKCS7* pkcs7 = NULL;
  25930. char fName[] = "./certs/test-degenerate.p7b";
  25931. XFILE f = XBADFILE;
  25932. byte der[4096];
  25933. word32 derSz = 0;
  25934. #ifndef NO_PKCS7_STREAM
  25935. word32 z;
  25936. int ret;
  25937. #endif /* !NO_PKCS7_STREAM */
  25938. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  25939. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  25940. if (f != XBADFILE)
  25941. XFCLOSE(f);
  25942. /* test degenerate success */
  25943. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25944. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25945. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25946. #ifndef NO_RSA
  25947. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25948. #ifndef NO_PKCS7_STREAM
  25949. wc_PKCS7_Free(pkcs7);
  25950. pkcs7 = NULL;
  25951. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25952. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25953. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25954. /* test for streaming */
  25955. ret = -1;
  25956. for (z = 0; z < derSz && ret != 0; z++) {
  25957. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  25958. if (ret < 0){
  25959. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25960. }
  25961. }
  25962. ExpectIntEQ(ret, 0);
  25963. #endif /* !NO_PKCS7_STREAM */
  25964. #else
  25965. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  25966. #endif /* NO_RSA */
  25967. wc_PKCS7_Free(pkcs7);
  25968. pkcs7 = NULL;
  25969. /* test with turning off degenerate cases */
  25970. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25971. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25972. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25973. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  25974. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz),
  25975. PKCS7_NO_SIGNER_E);
  25976. #ifndef NO_PKCS7_STREAM
  25977. wc_PKCS7_Free(pkcs7);
  25978. pkcs7 = NULL;
  25979. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25980. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25981. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25982. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  25983. /* test for streaming */
  25984. ret = -1;
  25985. for (z = 0; z < derSz && ret != 0; z++) {
  25986. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  25987. if (ret == WC_PKCS7_WANT_READ_E){
  25988. continue;
  25989. }
  25990. else
  25991. break;
  25992. }
  25993. ExpectIntEQ(ret, PKCS7_NO_SIGNER_E);
  25994. #endif /* !NO_PKCS7_STREAM */
  25995. wc_PKCS7_Free(pkcs7);
  25996. #endif
  25997. return EXPECT_RESULT();
  25998. } /* END test_wc_PKCS7_Degenerate() */
  25999. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  26000. defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA)
  26001. static byte berContent[] = {
  26002. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  26003. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  26004. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  26005. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  26006. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  26007. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  26008. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  26009. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  26010. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  26011. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  26012. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  26013. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  26014. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  26015. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  26016. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  26017. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  26018. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  26019. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  26020. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  26021. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  26022. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  26023. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  26024. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  26025. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  26026. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  26027. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  26028. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  26029. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  26030. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  26031. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  26032. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  26033. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  26034. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  26035. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  26036. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  26037. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  26038. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  26039. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  26040. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  26041. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  26042. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  26043. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  26044. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  26045. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  26046. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  26047. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  26048. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  26049. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  26050. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  26051. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  26052. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  26053. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  26054. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  26055. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  26056. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  26057. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  26058. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  26059. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  26060. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  26061. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  26062. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  26063. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  26064. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  26065. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  26066. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  26067. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  26068. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  26069. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  26070. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  26071. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  26072. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  26073. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  26074. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  26075. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  26076. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  26077. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  26078. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  26079. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  26080. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  26081. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  26082. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  26083. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  26084. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  26085. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  26086. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  26087. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  26088. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  26089. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  26090. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  26091. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  26092. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  26093. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  26094. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  26095. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  26096. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  26097. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  26098. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  26099. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  26100. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  26101. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  26102. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  26103. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  26104. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  26105. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  26106. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  26107. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  26108. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  26109. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  26110. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  26111. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  26112. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  26113. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  26114. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  26115. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  26116. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  26117. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  26118. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  26119. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  26120. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  26121. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  26122. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  26123. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  26124. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  26125. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  26126. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  26127. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  26128. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  26129. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  26130. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  26131. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  26132. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  26133. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  26134. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  26135. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  26136. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  26137. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  26138. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  26139. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  26140. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  26141. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  26142. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  26143. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  26144. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  26145. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  26146. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  26147. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  26148. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  26149. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  26150. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  26151. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  26152. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  26153. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  26154. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  26155. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  26156. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  26157. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  26158. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  26159. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  26160. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  26161. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  26162. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  26163. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  26164. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  26165. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  26166. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  26167. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  26168. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  26169. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  26170. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  26171. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  26172. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  26173. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  26174. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  26175. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  26176. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  26177. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  26178. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  26179. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  26180. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  26181. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  26182. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  26183. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  26184. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  26185. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  26186. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  26187. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  26188. 0x00, 0x00, 0x00, 0x00, 0x00
  26189. };
  26190. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER &&
  26191. * !NO_DES3 && !NO_SHA
  26192. */
  26193. /*
  26194. * Testing wc_PKCS7_BER()
  26195. */
  26196. static int test_wc_PKCS7_BER(void)
  26197. {
  26198. EXPECT_DECLS;
  26199. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  26200. !defined(NO_SHA) && defined(ASN_BER_TO_DER)
  26201. PKCS7* pkcs7 = NULL;
  26202. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  26203. XFILE f = XBADFILE;
  26204. byte der[4096];
  26205. #ifndef NO_DES3
  26206. byte decoded[2048];
  26207. #endif
  26208. word32 derSz = 0;
  26209. #ifndef NO_PKCS7_STREAM
  26210. word32 z;
  26211. int ret;
  26212. #endif /* !NO_PKCS7_STREAM */
  26213. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  26214. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  26215. if (f != XBADFILE) {
  26216. XFCLOSE(f);
  26217. f = XBADFILE;
  26218. }
  26219. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26220. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26221. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26222. #ifndef NO_RSA
  26223. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26224. #ifndef NO_PKCS7_STREAM
  26225. wc_PKCS7_Free(pkcs7);
  26226. pkcs7 = NULL;
  26227. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26228. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26229. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26230. /* test for streaming */
  26231. ret = -1;
  26232. for (z = 0; z < derSz && ret != 0; z++) {
  26233. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  26234. if (ret < 0){
  26235. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26236. }
  26237. }
  26238. ExpectIntEQ(ret, 0);
  26239. #endif /* !NO_PKCS7_STREAM */
  26240. #else
  26241. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26242. #endif
  26243. wc_PKCS7_Free(pkcs7);
  26244. pkcs7 = NULL;
  26245. #ifndef NO_DES3
  26246. /* decode BER content */
  26247. ExpectTrue((f = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  26248. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  26249. if (f != XBADFILE) {
  26250. XFCLOSE(f);
  26251. f = XBADFILE;
  26252. }
  26253. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26254. #ifndef NO_RSA
  26255. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  26256. #else
  26257. ExpectIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  26258. #endif
  26259. ExpectTrue((f = XFOPEN("./certs/1024/client-key.der", "rb")) != XBADFILE);
  26260. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  26261. if (f != XBADFILE) {
  26262. XFCLOSE(f);
  26263. f = XBADFILE;
  26264. }
  26265. if (pkcs7 != NULL) {
  26266. pkcs7->privateKey = der;
  26267. pkcs7->privateKeySz = derSz;
  26268. }
  26269. #ifndef NO_RSA
  26270. #ifdef WOLFSSL_SP_MATH
  26271. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26272. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  26273. #else
  26274. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26275. sizeof(berContent), decoded, sizeof(decoded)), 0);
  26276. #endif
  26277. #else
  26278. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26279. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  26280. #endif
  26281. wc_PKCS7_Free(pkcs7);
  26282. #endif /* !NO_DES3 */
  26283. #endif
  26284. return EXPECT_RESULT();
  26285. } /* END test_wc_PKCS7_BER() */
  26286. static int test_wc_PKCS7_signed_enveloped(void)
  26287. {
  26288. EXPECT_DECLS;
  26289. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  26290. !defined(NO_FILESYSTEM)
  26291. XFILE f = XBADFILE;
  26292. PKCS7* pkcs7 = NULL;
  26293. #ifdef HAVE_AES_CBC
  26294. PKCS7* inner = NULL;
  26295. #endif
  26296. WC_RNG rng;
  26297. unsigned char key[FOURK_BUF/2];
  26298. unsigned char cert[FOURK_BUF/2];
  26299. unsigned char env[FOURK_BUF];
  26300. int envSz = FOURK_BUF;
  26301. int keySz = 0;
  26302. int certSz = 0;
  26303. unsigned char sig[FOURK_BUF * 2];
  26304. int sigSz = FOURK_BUF * 2;
  26305. #ifdef HAVE_AES_CBC
  26306. unsigned char decoded[FOURK_BUF];
  26307. int decodedSz = FOURK_BUF;
  26308. #endif
  26309. #ifndef NO_PKCS7_STREAM
  26310. int z;
  26311. int ret;
  26312. #endif /* !NO_PKCS7_STREAM */
  26313. XMEMSET(&rng, 0, sizeof(WC_RNG));
  26314. /* load cert */
  26315. ExpectTrue((f = XFOPEN(cliCertDerFile, "rb")) != XBADFILE);
  26316. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  26317. if (f != XBADFILE) {
  26318. XFCLOSE(f);
  26319. f = XBADFILE;
  26320. }
  26321. /* load key */
  26322. ExpectTrue((f = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  26323. ExpectIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  26324. if (f != XBADFILE) {
  26325. XFCLOSE(f);
  26326. f = XBADFILE;
  26327. }
  26328. ExpectIntGT(keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL), 0);
  26329. /* sign cert for envelope */
  26330. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26331. ExpectIntEQ(wc_InitRng(&rng), 0);
  26332. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26333. if (pkcs7 != NULL) {
  26334. pkcs7->content = cert;
  26335. pkcs7->contentSz = certSz;
  26336. pkcs7->contentOID = DATA;
  26337. pkcs7->privateKey = key;
  26338. pkcs7->privateKeySz = keySz;
  26339. pkcs7->encryptOID = RSAk;
  26340. pkcs7->hashOID = SHA256h;
  26341. pkcs7->rng = &rng;
  26342. }
  26343. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26344. wc_PKCS7_Free(pkcs7);
  26345. pkcs7 = NULL;
  26346. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  26347. #ifdef HAVE_AES_CBC
  26348. /* create envelope */
  26349. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26350. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26351. if (pkcs7 != NULL) {
  26352. pkcs7->content = sig;
  26353. pkcs7->contentSz = sigSz;
  26354. pkcs7->contentOID = DATA;
  26355. pkcs7->encryptOID = AES256CBCb;
  26356. pkcs7->privateKey = key;
  26357. pkcs7->privateKeySz = keySz;
  26358. }
  26359. ExpectIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  26360. ExpectIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  26361. wc_PKCS7_Free(pkcs7);
  26362. pkcs7 = NULL;
  26363. #endif
  26364. /* create bad signed enveloped data */
  26365. sigSz = FOURK_BUF * 2;
  26366. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26367. ExpectIntEQ(wc_InitRng(&rng), 0);
  26368. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26369. if (pkcs7 != NULL) {
  26370. pkcs7->content = env;
  26371. pkcs7->contentSz = envSz;
  26372. pkcs7->contentOID = DATA;
  26373. pkcs7->privateKey = key;
  26374. pkcs7->privateKeySz = keySz;
  26375. pkcs7->encryptOID = RSAk;
  26376. pkcs7->hashOID = SHA256h;
  26377. pkcs7->rng = &rng;
  26378. }
  26379. /* Set no certs in bundle for this test. */
  26380. if (pkcs7 != NULL) {
  26381. ExpectIntEQ(wc_PKCS7_SetNoCerts(pkcs7, 1), 0);
  26382. ExpectIntEQ(wc_PKCS7_SetNoCerts(NULL, 1), BAD_FUNC_ARG);
  26383. ExpectIntEQ(wc_PKCS7_GetNoCerts(pkcs7), 1);
  26384. }
  26385. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26386. wc_PKCS7_Free(pkcs7);
  26387. pkcs7 = NULL;
  26388. /* check verify fails */
  26389. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26390. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26391. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  26392. PKCS7_SIGNEEDS_CHECK);
  26393. /* try verifying the signature manually */
  26394. {
  26395. RsaKey rKey;
  26396. word32 idx = 0;
  26397. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  26398. WC_MAX_DIGEST_SIZE];
  26399. int digestSz = 0;
  26400. ExpectIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  26401. ExpectIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  26402. ExpectIntGT(digestSz = wc_RsaSSL_Verify(pkcs7->signature,
  26403. pkcs7->signatureSz, digest, sizeof(digest), &rKey), 0);
  26404. ExpectIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  26405. ExpectIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  26406. ExpectIntEQ(wc_FreeRsaKey(&rKey), 0);
  26407. /* verify was success */
  26408. }
  26409. wc_PKCS7_Free(pkcs7);
  26410. pkcs7 = NULL;
  26411. /* initializing the PKCS7 struct with the signing certificate should pass */
  26412. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26413. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26414. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  26415. #ifndef NO_PKCS7_STREAM
  26416. wc_PKCS7_Free(pkcs7);
  26417. pkcs7 = NULL;
  26418. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26419. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26420. /* test for streaming */
  26421. ret = -1;
  26422. for (z = 0; z < sigSz && ret != 0; z++) {
  26423. ret = wc_PKCS7_VerifySignedData(pkcs7, sig + z, 1);
  26424. if (ret < 0){
  26425. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26426. }
  26427. }
  26428. ExpectIntEQ(ret, 0);
  26429. #endif /* !NO_PKCS7_STREAM */
  26430. wc_PKCS7_Free(pkcs7);
  26431. pkcs7 = NULL;
  26432. /* create valid degenerate bundle */
  26433. sigSz = FOURK_BUF * 2;
  26434. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26435. if (pkcs7 != NULL) {
  26436. pkcs7->content = env;
  26437. pkcs7->contentSz = envSz;
  26438. pkcs7->contentOID = DATA;
  26439. pkcs7->privateKey = key;
  26440. pkcs7->privateKeySz = keySz;
  26441. pkcs7->encryptOID = RSAk;
  26442. pkcs7->hashOID = SHA256h;
  26443. pkcs7->rng = &rng;
  26444. }
  26445. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  26446. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26447. wc_PKCS7_Free(pkcs7);
  26448. pkcs7 = NULL;
  26449. wc_FreeRng(&rng);
  26450. /* check verify */
  26451. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26452. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26453. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  26454. ExpectNotNull(pkcs7->content);
  26455. #ifndef NO_PKCS7_STREAM
  26456. wc_PKCS7_Free(pkcs7);
  26457. pkcs7 = NULL;
  26458. /* create valid degenerate bundle */
  26459. sigSz = FOURK_BUF * 2;
  26460. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26461. if (pkcs7 != NULL) {
  26462. pkcs7->content = env;
  26463. pkcs7->contentSz = envSz;
  26464. pkcs7->contentOID = DATA;
  26465. pkcs7->privateKey = key;
  26466. pkcs7->privateKeySz = keySz;
  26467. pkcs7->encryptOID = RSAk;
  26468. pkcs7->hashOID = SHA256h;
  26469. pkcs7->rng = &rng;
  26470. }
  26471. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  26472. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26473. wc_PKCS7_Free(pkcs7);
  26474. pkcs7 = NULL;
  26475. wc_FreeRng(&rng);
  26476. /* check verify */
  26477. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26478. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26479. /* test for streaming */
  26480. ret = -1;
  26481. for (z = 0; z < sigSz && ret != 0; z++) {
  26482. ret = wc_PKCS7_VerifySignedData(pkcs7, sig + z, 1);
  26483. if (ret < 0){
  26484. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26485. }
  26486. }
  26487. ExpectIntEQ(ret, 0);
  26488. #endif /* !NO_PKCS7_STREAM */
  26489. #ifdef HAVE_AES_CBC
  26490. /* check decode */
  26491. ExpectNotNull(inner = wc_PKCS7_New(NULL, 0));
  26492. ExpectIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  26493. if (inner != NULL) {
  26494. inner->privateKey = key;
  26495. inner->privateKeySz = keySz;
  26496. }
  26497. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  26498. pkcs7->contentSz, decoded, decodedSz)), 0);
  26499. wc_PKCS7_Free(inner);
  26500. inner = NULL;
  26501. #endif
  26502. wc_PKCS7_Free(pkcs7);
  26503. pkcs7 = NULL;
  26504. #ifdef HAVE_AES_CBC
  26505. /* check cert set */
  26506. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26507. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26508. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  26509. ExpectNotNull(pkcs7->singleCert);
  26510. ExpectIntNE(pkcs7->singleCertSz, 0);
  26511. wc_PKCS7_Free(pkcs7);
  26512. pkcs7 = NULL;
  26513. #ifndef NO_PKCS7_STREAM
  26514. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26515. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26516. /* test for streaming */
  26517. ret = -1;
  26518. for (z = 0; z < decodedSz && ret != 0; z++) {
  26519. ret = wc_PKCS7_VerifySignedData(pkcs7, decoded + z, 1);
  26520. if (ret < 0){
  26521. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26522. }
  26523. }
  26524. ExpectIntEQ(ret, 0);
  26525. ExpectNotNull(pkcs7->singleCert);
  26526. ExpectIntNE(pkcs7->singleCertSz, 0);
  26527. wc_PKCS7_Free(pkcs7);
  26528. pkcs7 = NULL;
  26529. #endif /* !NO_PKCS7_STREAM */
  26530. #endif
  26531. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  26532. return EXPECT_RESULT();
  26533. }
  26534. static int test_wc_PKCS7_NoDefaultSignedAttribs(void)
  26535. {
  26536. EXPECT_DECLS;
  26537. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26538. && !defined(NO_AES)
  26539. PKCS7* pkcs7 = NULL;
  26540. void* heap = NULL;
  26541. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26542. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26543. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  26544. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  26545. wc_PKCS7_Free(pkcs7);
  26546. #endif
  26547. return EXPECT_RESULT();
  26548. }
  26549. static int test_wc_PKCS7_SetOriEncryptCtx(void)
  26550. {
  26551. EXPECT_DECLS;
  26552. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26553. && !defined(NO_AES)
  26554. PKCS7* pkcs7 = NULL;
  26555. void* heap = NULL;
  26556. WOLFSSL_CTX* ctx = NULL;
  26557. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26558. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26559. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26560. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  26561. wc_PKCS7_Free(pkcs7);
  26562. #endif
  26563. return EXPECT_RESULT();
  26564. }
  26565. static int test_wc_PKCS7_SetOriDecryptCtx(void)
  26566. {
  26567. EXPECT_DECLS;
  26568. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26569. && !defined(NO_AES)
  26570. PKCS7* pkcs7 = NULL;
  26571. void* heap = NULL;
  26572. WOLFSSL_CTX* ctx = NULL;
  26573. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26574. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26575. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26576. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  26577. wc_PKCS7_Free(pkcs7);
  26578. #endif
  26579. return EXPECT_RESULT();
  26580. }
  26581. static int test_wc_PKCS7_DecodeCompressedData(void)
  26582. {
  26583. EXPECT_DECLS;
  26584. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26585. && !defined(NO_AES) && defined(HAVE_LIBZ)
  26586. PKCS7* pkcs7 = NULL;
  26587. void* heap = NULL;
  26588. byte out[4096];
  26589. byte* decompressed = NULL;
  26590. int outSz;
  26591. int decompressedSz;
  26592. const char* cert = "./certs/client-cert.pem";
  26593. byte* cert_buf = NULL;
  26594. size_t cert_sz = 0;
  26595. ExpectIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  26596. ExpectNotNull((decompressed = (byte*)XMALLOC(cert_sz, heap,
  26597. DYNAMIC_TYPE_TMP_BUFFER)));
  26598. decompressedSz = (int)cert_sz;
  26599. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26600. if (pkcs7 != NULL) {
  26601. pkcs7->content = (byte*)cert_buf;
  26602. pkcs7->contentSz = (word32)cert_sz;
  26603. pkcs7->contentOID = DATA;
  26604. }
  26605. ExpectIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  26606. sizeof(out))), 0);
  26607. wc_PKCS7_Free(pkcs7);
  26608. pkcs7 = NULL;
  26609. /* compressed key should be smaller than when started */
  26610. ExpectIntLT(outSz, cert_sz);
  26611. /* test decompression */
  26612. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26613. ExpectIntEQ(pkcs7->contentOID, 0);
  26614. /* fail case with out buffer too small */
  26615. ExpectIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26616. decompressed, outSz), 0);
  26617. /* success case */
  26618. ExpectIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26619. decompressed, decompressedSz), cert_sz);
  26620. ExpectIntEQ(pkcs7->contentOID, DATA);
  26621. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26622. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26623. decompressed = NULL;
  26624. /* test decompression function with different 'max' inputs */
  26625. outSz = sizeof(out);
  26626. ExpectIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  26627. 0);
  26628. ExpectIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  26629. out, outSz, 0, heap), 0);
  26630. ExpectNull(decompressed);
  26631. ExpectIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  26632. out, outSz, 0, heap), 0);
  26633. ExpectNotNull(decompressed);
  26634. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26635. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26636. decompressed = NULL;
  26637. ExpectIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  26638. out, outSz, 0, heap), 0);
  26639. ExpectNotNull(decompressed);
  26640. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26641. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26642. if (cert_buf != NULL)
  26643. free(cert_buf);
  26644. wc_PKCS7_Free(pkcs7);
  26645. #endif
  26646. return EXPECT_RESULT();
  26647. }
  26648. static int test_wc_i2d_PKCS12(void)
  26649. {
  26650. EXPECT_DECLS;
  26651. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  26652. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26653. && !defined(NO_AES) && !defined(NO_DES3) && !defined(NO_SHA)
  26654. WC_PKCS12* pkcs12 = NULL;
  26655. unsigned char der[FOURK_BUF * 2];
  26656. unsigned char* pt;
  26657. int derSz = 0;
  26658. unsigned char out[FOURK_BUF * 2];
  26659. int outSz = FOURK_BUF * 2;
  26660. const char p12_f[] = "./certs/test-servercert.p12";
  26661. XFILE f = XBADFILE;
  26662. ExpectTrue((f = XFOPEN(p12_f, "rb")) != XBADFILE);
  26663. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  26664. if (f != XBADFILE)
  26665. XFCLOSE(f);
  26666. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  26667. ExpectIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  26668. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26669. ExpectIntEQ(outSz, derSz);
  26670. outSz = derSz - 1;
  26671. pt = out;
  26672. ExpectIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  26673. outSz = derSz;
  26674. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  26675. ExpectIntEQ((pt == out), 0);
  26676. pt = NULL;
  26677. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  26678. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  26679. wc_PKCS12_free(pkcs12);
  26680. pkcs12 = NULL;
  26681. /* Run the same test but use wc_d2i_PKCS12_fp. */
  26682. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  26683. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26684. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26685. ExpectIntEQ(outSz, derSz);
  26686. wc_PKCS12_free(pkcs12);
  26687. pkcs12 = NULL;
  26688. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  26689. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26690. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26691. ExpectIntEQ(outSz, derSz);
  26692. wc_PKCS12_free(pkcs12);
  26693. pkcs12 = NULL;
  26694. #endif
  26695. return EXPECT_RESULT();
  26696. }
  26697. /* Testing wc_SignatureGetSize() for signature type ECC */
  26698. static int test_wc_SignatureGetSize_ecc(void)
  26699. {
  26700. EXPECT_DECLS;
  26701. #if !defined(NO_SIG_WRAPPER) && defined(HAVE_ECC) && !defined(NO_ECC256)
  26702. enum wc_SignatureType sig_type;
  26703. word32 key_len;
  26704. ecc_key ecc;
  26705. const char* qx =
  26706. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  26707. const char* qy =
  26708. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  26709. const char* d =
  26710. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  26711. XMEMSET(&ecc, 0, sizeof(ecc_key));
  26712. ExpectIntEQ(wc_ecc_init(&ecc), 0);
  26713. ExpectIntEQ(wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1"), 0);
  26714. /* Input for signature type ECC */
  26715. sig_type = WC_SIGNATURE_TYPE_ECC;
  26716. key_len = sizeof(ecc_key);
  26717. ExpectIntGT(wc_SignatureGetSize(sig_type, &ecc, key_len), 0);
  26718. /* Test bad args */
  26719. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26720. sig_type = (enum wc_SignatureType) 100;
  26721. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26722. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  26723. sig_type = WC_SIGNATURE_TYPE_ECC;
  26724. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0);
  26725. key_len = (word32)0;
  26726. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  26727. DoExpectIntEQ(wc_ecc_free(&ecc), 0);
  26728. #endif /* !NO_SIG_WRAPPER && HAVE_ECC && !NO_ECC256 */
  26729. return EXPECT_RESULT();
  26730. } /* END test_wc_SignatureGetSize_ecc() */
  26731. /* Testing wc_SignatureGetSize() for signature type rsa */
  26732. static int test_wc_SignatureGetSize_rsa(void)
  26733. {
  26734. EXPECT_DECLS;
  26735. #if !defined(NO_SIG_WRAPPER) && !defined(NO_RSA)
  26736. enum wc_SignatureType sig_type;
  26737. word32 key_len;
  26738. word32 idx = 0;
  26739. RsaKey rsa_key;
  26740. byte* tmp = NULL;
  26741. size_t bytes;
  26742. XMEMSET(&rsa_key, 0, sizeof(RsaKey));
  26743. #ifdef USE_CERT_BUFFERS_1024
  26744. bytes = (size_t)sizeof_client_key_der_1024;
  26745. if (bytes < (size_t)sizeof_client_key_der_1024)
  26746. bytes = (size_t)sizeof_client_cert_der_1024;
  26747. #elif defined(USE_CERT_BUFFERS_2048)
  26748. bytes = (size_t)sizeof_client_key_der_2048;
  26749. if (bytes < (size_t)sizeof_client_cert_der_2048)
  26750. bytes = (size_t)sizeof_client_cert_der_2048;
  26751. #else
  26752. bytes = FOURK_BUF;
  26753. #endif
  26754. ExpectNotNull(tmp = (byte*)XMALLOC(bytes, HEAP_HINT,
  26755. DYNAMIC_TYPE_TMP_BUFFER));
  26756. if (tmp != NULL) {
  26757. #ifdef USE_CERT_BUFFERS_1024
  26758. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  26759. #elif defined(USE_CERT_BUFFERS_2048)
  26760. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  26761. #elif !defined(NO_FILESYSTEM)
  26762. XFILE file = XBADFILE;
  26763. ExpectTrue((file = XFOPEN(clientKey, "rb")) != XBADFILE);
  26764. ExpectIntGT(bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file), 0);
  26765. if (file != XBADFILE)
  26766. XFCLOSE(file);
  26767. }
  26768. #else
  26769. ExpectFail();
  26770. #endif
  26771. }
  26772. ExpectIntEQ(wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId), 0);
  26773. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes), 0);
  26774. /* Input for signature type RSA */
  26775. sig_type = WC_SIGNATURE_TYPE_RSA;
  26776. key_len = sizeof(RsaKey);
  26777. ExpectIntGT(wc_SignatureGetSize(sig_type, &rsa_key, key_len), 0);
  26778. /* Test bad args */
  26779. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26780. sig_type = (enum wc_SignatureType)100;
  26781. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26782. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  26783. sig_type = WC_SIGNATURE_TYPE_RSA;
  26784. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), BAD_FUNC_ARG);
  26785. key_len = (word32)0;
  26786. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  26787. DoExpectIntEQ(wc_FreeRsaKey(&rsa_key), 0);
  26788. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26789. #endif /* !NO_SIG_WRAPPER && !NO_RSA */
  26790. return EXPECT_RESULT();
  26791. } /* END test_wc_SignatureGetSize_rsa(void) */
  26792. /*----------------------------------------------------------------------------*
  26793. | hash.h Tests
  26794. *----------------------------------------------------------------------------*/
  26795. static int test_wc_HashInit(void)
  26796. {
  26797. EXPECT_DECLS;
  26798. int i; /* 0 indicates tests passed, 1 indicates failure */
  26799. wc_HashAlg hash;
  26800. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26801. enum wc_HashType enumArray[] = {
  26802. #ifndef NO_MD5
  26803. WC_HASH_TYPE_MD5,
  26804. #endif
  26805. #ifndef NO_SHA
  26806. WC_HASH_TYPE_SHA,
  26807. #endif
  26808. #ifdef WOLFSSL_SHA224
  26809. WC_HASH_TYPE_SHA224,
  26810. #endif
  26811. #ifndef NO_SHA256
  26812. WC_HASH_TYPE_SHA256,
  26813. #endif
  26814. #ifdef WOLFSSL_SHA384
  26815. WC_HASH_TYPE_SHA384,
  26816. #endif
  26817. #ifdef WOLFSSL_SHA512
  26818. WC_HASH_TYPE_SHA512,
  26819. #endif
  26820. };
  26821. /* dynamically finds the length */
  26822. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26823. /* For loop to test various arguments... */
  26824. for (i = 0; i < enumlen; i++) {
  26825. /* check for bad args */
  26826. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  26827. wc_HashFree(&hash, enumArray[i]);
  26828. /* check for null ptr */
  26829. ExpectIntEQ(wc_HashInit(NULL, enumArray[i]), BAD_FUNC_ARG);
  26830. } /* end of for loop */
  26831. return EXPECT_RESULT();
  26832. } /* end of test_wc_HashInit */
  26833. /*
  26834. * Unit test function for wc_HashSetFlags()
  26835. */
  26836. static int test_wc_HashSetFlags(void)
  26837. {
  26838. EXPECT_DECLS;
  26839. #ifdef WOLFSSL_HASH_FLAGS
  26840. wc_HashAlg hash;
  26841. word32 flags = 0;
  26842. int i, j;
  26843. int notSupportedLen;
  26844. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26845. enum wc_HashType enumArray[] = {
  26846. #ifndef NO_MD5
  26847. WC_HASH_TYPE_MD5,
  26848. #endif
  26849. #ifndef NO_SHA
  26850. WC_HASH_TYPE_SHA,
  26851. #endif
  26852. #ifdef WOLFSSL_SHA224
  26853. WC_HASH_TYPE_SHA224,
  26854. #endif
  26855. #ifndef NO_SHA256
  26856. WC_HASH_TYPE_SHA256,
  26857. #endif
  26858. #ifdef WOLFSSL_SHA384
  26859. WC_HASH_TYPE_SHA384,
  26860. #endif
  26861. #ifdef WOLFSSL_SHA512
  26862. WC_HASH_TYPE_SHA512,
  26863. #endif
  26864. #ifdef WOLFSSL_SHA3
  26865. WC_HASH_TYPE_SHA3_224,
  26866. #endif
  26867. };
  26868. enum wc_HashType notSupported[] = {
  26869. WC_HASH_TYPE_MD5_SHA,
  26870. WC_HASH_TYPE_MD2,
  26871. WC_HASH_TYPE_MD4,
  26872. WC_HASH_TYPE_BLAKE2B,
  26873. WC_HASH_TYPE_BLAKE2S,
  26874. WC_HASH_TYPE_NONE,
  26875. };
  26876. /* dynamically finds the length */
  26877. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26878. /* For loop to test various arguments... */
  26879. for (i = 0; i < enumlen; i++) {
  26880. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  26881. ExpectIntEQ(wc_HashSetFlags(&hash, enumArray[i], flags), 0);
  26882. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  26883. ExpectIntEQ(wc_HashSetFlags(NULL, enumArray[i], flags), BAD_FUNC_ARG);
  26884. wc_HashFree(&hash, enumArray[i]);
  26885. }
  26886. /* For loop to test not supported cases */
  26887. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  26888. for (j = 0; j < notSupportedLen; j++) {
  26889. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  26890. ExpectIntEQ(wc_HashSetFlags(&hash, notSupported[j], flags),
  26891. BAD_FUNC_ARG);
  26892. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  26893. }
  26894. #endif
  26895. return EXPECT_RESULT();
  26896. } /* END test_wc_HashSetFlags */
  26897. /*
  26898. * Unit test function for wc_HashGetFlags()
  26899. */
  26900. static int test_wc_HashGetFlags(void)
  26901. {
  26902. EXPECT_DECLS;
  26903. #ifdef WOLFSSL_HASH_FLAGS
  26904. wc_HashAlg hash;
  26905. word32 flags = 0;
  26906. int i, j;
  26907. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26908. enum wc_HashType enumArray[] = {
  26909. #ifndef NO_MD5
  26910. WC_HASH_TYPE_MD5,
  26911. #endif
  26912. #ifndef NO_SHA
  26913. WC_HASH_TYPE_SHA,
  26914. #endif
  26915. #ifdef WOLFSSL_SHA224
  26916. WC_HASH_TYPE_SHA224,
  26917. #endif
  26918. #ifndef NO_SHA256
  26919. WC_HASH_TYPE_SHA256,
  26920. #endif
  26921. #ifdef WOLFSSL_SHA384
  26922. WC_HASH_TYPE_SHA384,
  26923. #endif
  26924. #ifdef WOLFSSL_SHA512
  26925. WC_HASH_TYPE_SHA512,
  26926. #endif
  26927. #ifdef WOLFSSL_SHA3
  26928. WC_HASH_TYPE_SHA3_224,
  26929. #endif
  26930. };
  26931. enum wc_HashType notSupported[] = {
  26932. WC_HASH_TYPE_MD5_SHA,
  26933. WC_HASH_TYPE_MD2,
  26934. WC_HASH_TYPE_MD4,
  26935. WC_HASH_TYPE_BLAKE2B,
  26936. WC_HASH_TYPE_BLAKE2S,
  26937. WC_HASH_TYPE_NONE,
  26938. };
  26939. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26940. int notSupportedLen;
  26941. /* For loop to test various arguments... */
  26942. for (i = 0; i < enumlen; i++) {
  26943. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  26944. ExpectIntEQ(wc_HashGetFlags(&hash, enumArray[i], &flags), 0);
  26945. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  26946. ExpectIntEQ(wc_HashGetFlags(NULL, enumArray[i], &flags), BAD_FUNC_ARG);
  26947. wc_HashFree(&hash, enumArray[i]);
  26948. }
  26949. /* For loop to test not supported cases */
  26950. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  26951. for (j = 0; j < notSupportedLen; j++) {
  26952. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  26953. ExpectIntEQ(wc_HashGetFlags(&hash, notSupported[j], &flags),
  26954. BAD_FUNC_ARG);
  26955. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  26956. }
  26957. #endif
  26958. return EXPECT_RESULT();
  26959. } /* END test_wc_HashGetFlags */
  26960. /*----------------------------------------------------------------------------*
  26961. | Compatibility Tests
  26962. *----------------------------------------------------------------------------*/
  26963. /*----------------------------------------------------------------------------*
  26964. | ASN.1 Tests
  26965. *----------------------------------------------------------------------------*/
  26966. static int test_wolfSSL_ASN1_BIT_STRING(void)
  26967. {
  26968. EXPECT_DECLS;
  26969. #if !defined(NO_CERTS) && defined(OPENSSL_ALL)
  26970. ASN1_BIT_STRING* str = NULL;
  26971. ExpectNotNull(str = ASN1_BIT_STRING_new());
  26972. /* Empty data testing. */
  26973. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 1), 0);
  26974. ASN1_BIT_STRING_free(str);
  26975. str = NULL;
  26976. ExpectNotNull(str = ASN1_BIT_STRING_new());
  26977. /* Invalid parameter testing. */
  26978. ExpectIntEQ(ASN1_BIT_STRING_set_bit(NULL, 42, 1), 0);
  26979. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, -1, 1), 0);
  26980. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 2), 0);
  26981. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, -1), 0);
  26982. /* No bit string - bit is always 0. */
  26983. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, 42), 0);
  26984. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, -1), 0);
  26985. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  26986. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 0), 0);
  26987. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  26988. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  26989. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  26990. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  26991. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  26992. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  26993. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  26994. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 91, 0), 1);
  26995. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 91), 0);
  26996. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 89, 0), 1);
  26997. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 89), 0);
  26998. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 0), 1);
  26999. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 0);
  27000. ASN1_BIT_STRING_free(str);
  27001. ASN1_BIT_STRING_free(NULL);
  27002. #endif
  27003. return EXPECT_RESULT();
  27004. }
  27005. static int test_wolfSSL_ASN1_INTEGER(void)
  27006. {
  27007. EXPECT_DECLS;
  27008. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27009. ASN1_INTEGER* a = NULL;
  27010. ASN1_INTEGER* dup = NULL;
  27011. const unsigned char invalidLenDer[] = {
  27012. 0x02, 0x20, 0x00
  27013. };
  27014. const unsigned char longDer[] = {
  27015. 0x02, 0x20,
  27016. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27017. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27018. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27019. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
  27020. };
  27021. const unsigned char* p;
  27022. /* Invalid parameter testing. */
  27023. ASN1_INTEGER_free(NULL);
  27024. ExpectNull(wolfSSL_ASN1_INTEGER_dup(NULL));
  27025. ExpectNotNull(a = ASN1_INTEGER_new());
  27026. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  27027. ASN1_INTEGER_free(dup);
  27028. dup = NULL;
  27029. ASN1_INTEGER_free(a);
  27030. a = NULL;
  27031. p = longDer;
  27032. ExpectNull(d2i_ASN1_INTEGER(NULL, &p, sizeof(invalidLenDer)));
  27033. p = longDer;
  27034. ExpectNotNull(a = d2i_ASN1_INTEGER(NULL, &p, sizeof(longDer)));
  27035. ExpectPtrNE(p, longDer);
  27036. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  27037. ASN1_INTEGER_free(dup);
  27038. ASN1_INTEGER_free(a);
  27039. #endif
  27040. return EXPECT_RESULT();
  27041. }
  27042. static int test_wolfSSL_ASN1_INTEGER_cmp(void)
  27043. {
  27044. EXPECT_DECLS;
  27045. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27046. ASN1_INTEGER* a = NULL;
  27047. ASN1_INTEGER* b = NULL;
  27048. ExpectNotNull(a = ASN1_INTEGER_new());
  27049. ExpectNotNull(b = ASN1_INTEGER_new());
  27050. ExpectIntEQ(ASN1_INTEGER_set(a, 1), 1);
  27051. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  27052. /* Invalid parameter testing. */
  27053. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, NULL), -1);
  27054. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, NULL), -1);
  27055. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, b), -1);
  27056. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27057. ExpectIntEQ(ASN1_INTEGER_set(b, -1), 1);
  27058. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27059. ExpectIntEQ(ASN1_INTEGER_set(a, -2), 1);
  27060. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27061. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  27062. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27063. ExpectIntEQ(ASN1_INTEGER_set(a, 0x01), 1);
  27064. ExpectIntEQ(ASN1_INTEGER_set(b, 0x1000), 1);
  27065. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27066. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(b, a), 0);
  27067. ASN1_INTEGER_free(b);
  27068. ASN1_INTEGER_free(a);
  27069. #endif
  27070. return EXPECT_RESULT();
  27071. }
  27072. static int test_wolfSSL_ASN1_INTEGER_BN(void)
  27073. {
  27074. EXPECT_DECLS;
  27075. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27076. ASN1_INTEGER* ai = NULL;
  27077. ASN1_INTEGER* ai2 = NULL;
  27078. BIGNUM* bn = NULL;
  27079. BIGNUM* bn2 = NULL;
  27080. ExpectNotNull(ai = ASN1_INTEGER_new());
  27081. ExpectNotNull(bn2 = BN_new());
  27082. /* Invalid parameter testing. */
  27083. ExpectNull(bn = ASN1_INTEGER_to_BN(NULL, NULL));
  27084. ExpectNull(ai2 = BN_to_ASN1_INTEGER(NULL, NULL));
  27085. /* at the moment hard setting since no set function */
  27086. if (ai != NULL) {
  27087. ai->data[0] = 0xff; /* No DER encoding. */
  27088. ai->length = 1;
  27089. }
  27090. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27091. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27092. BN_free(bn);
  27093. bn = NULL;
  27094. #else
  27095. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  27096. #endif
  27097. if (ai != NULL) {
  27098. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27099. ai->data[1] = 0x04; /* bad length of integer */
  27100. ai->data[2] = 0x03;
  27101. ai->length = 3;
  27102. }
  27103. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27104. /* Interpreted as a number 0x020403. */
  27105. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27106. BN_free(bn);
  27107. bn = NULL;
  27108. #else
  27109. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  27110. #endif
  27111. if (ai != NULL) {
  27112. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27113. ai->data[1] = 0x01; /* length of integer */
  27114. ai->data[2] = 0x03;
  27115. ai->length = 3;
  27116. }
  27117. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27118. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, NULL));
  27119. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27120. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27121. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27122. if (ai != NULL) {
  27123. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27124. ai->data[1] = 0x02; /* length of integer */
  27125. ai->data[2] = 0x00; /* padding byte to ensure positive */
  27126. ai->data[3] = 0xff;
  27127. ai->length = 4;
  27128. }
  27129. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  27130. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  27131. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27132. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27133. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27134. if (ai != NULL) {
  27135. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27136. ai->data[1] = 0x01; /* length of integer */
  27137. ai->data[2] = 0x00;
  27138. ai->length = 3;
  27139. }
  27140. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  27141. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  27142. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27143. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27144. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27145. if (ai != NULL) {
  27146. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27147. ai->data[1] = 0x01; /* length of integer */
  27148. ai->data[2] = 0x01;
  27149. ai->length = 3;
  27150. ai->negative = 1;
  27151. }
  27152. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  27153. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  27154. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27155. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27156. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27157. BN_free(bn2);
  27158. BN_free(bn);
  27159. ASN1_INTEGER_free(ai2);
  27160. ASN1_INTEGER_free(ai);
  27161. #endif
  27162. return EXPECT_RESULT();
  27163. }
  27164. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  27165. {
  27166. EXPECT_DECLS;
  27167. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27168. ASN1_INTEGER *a = NULL;
  27169. long val;
  27170. ExpectNotNull(a = ASN1_INTEGER_new());
  27171. /* Invalid parameter testing. */
  27172. ExpectIntEQ(ASN1_INTEGER_get(NULL), 0);
  27173. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27174. ExpectIntEQ(ASN1_INTEGER_get(a), 0);
  27175. #else
  27176. ExpectIntEQ(ASN1_INTEGER_get(a), -1);
  27177. #endif
  27178. ASN1_INTEGER_free(a);
  27179. a = NULL;
  27180. ExpectNotNull(a = ASN1_INTEGER_new());
  27181. val = 0;
  27182. ExpectIntEQ(ASN1_INTEGER_set(NULL, val), 0);
  27183. ASN1_INTEGER_free(a);
  27184. a = NULL;
  27185. /* 0 */
  27186. ExpectNotNull(a = ASN1_INTEGER_new());
  27187. val = 0;
  27188. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27189. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27190. ASN1_INTEGER_free(a);
  27191. a = NULL;
  27192. /* 40 */
  27193. ExpectNotNull(a = ASN1_INTEGER_new());
  27194. val = 40;
  27195. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27196. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27197. ASN1_INTEGER_free(a);
  27198. a = NULL;
  27199. /* -40 */
  27200. ExpectNotNull(a = ASN1_INTEGER_new());
  27201. val = -40;
  27202. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27203. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27204. ASN1_INTEGER_free(a);
  27205. a = NULL;
  27206. /* 128 */
  27207. ExpectNotNull(a = ASN1_INTEGER_new());
  27208. val = 128;
  27209. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27210. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27211. ASN1_INTEGER_free(a);
  27212. a = NULL;
  27213. /* -128 */
  27214. ExpectNotNull(a = ASN1_INTEGER_new());
  27215. val = -128;
  27216. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27217. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27218. ASN1_INTEGER_free(a);
  27219. a = NULL;
  27220. /* 200 */
  27221. ExpectNotNull(a = ASN1_INTEGER_new());
  27222. val = 200;
  27223. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27224. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27225. ASN1_INTEGER_free(a);
  27226. a = NULL;
  27227. /* int max (2147483647) */
  27228. ExpectNotNull(a = ASN1_INTEGER_new());
  27229. val = 2147483647;
  27230. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27231. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27232. ASN1_INTEGER_free(a);
  27233. a = NULL;
  27234. /* int min (-2147483648) */
  27235. ExpectNotNull(a = ASN1_INTEGER_new());
  27236. val = -2147483647 - 1;
  27237. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27238. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27239. ASN1_INTEGER_free(a);
  27240. a = NULL;
  27241. /* long max positive */
  27242. ExpectNotNull(a = ASN1_INTEGER_new());
  27243. val = (long)(((unsigned long)-1) >> 1);
  27244. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27245. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27246. ASN1_INTEGER_free(a);
  27247. #endif
  27248. return EXPECT_RESULT();
  27249. }
  27250. #if defined(OPENSSL_EXTRA)
  27251. typedef struct ASN1IntTestVector {
  27252. const byte* der;
  27253. const size_t derSz;
  27254. const long value;
  27255. } ASN1IntTestVector;
  27256. #endif
  27257. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  27258. {
  27259. EXPECT_DECLS;
  27260. #if defined(OPENSSL_EXTRA)
  27261. size_t i;
  27262. WOLFSSL_ASN1_INTEGER* a = NULL;
  27263. WOLFSSL_ASN1_INTEGER* b = NULL;
  27264. WOLFSSL_ASN1_INTEGER* c = NULL;
  27265. const byte* p = NULL;
  27266. byte* p2 = NULL;
  27267. byte* reEncoded = NULL;
  27268. int reEncodedSz = 0;
  27269. static const byte zeroDer[] = {
  27270. 0x02, 0x01, 0x00
  27271. };
  27272. static const byte oneDer[] = {
  27273. 0x02, 0x01, 0x01
  27274. };
  27275. static const byte negativeDer[] = {
  27276. 0x02, 0x03, 0xC1, 0x16, 0x0D
  27277. };
  27278. static const byte positiveDer[] = {
  27279. 0x02, 0x03, 0x01, 0x00, 0x01
  27280. };
  27281. static const byte primeDer[] = {
  27282. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  27283. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  27284. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  27285. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  27286. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  27287. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  27288. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  27289. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  27290. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  27291. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  27292. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  27293. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  27294. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  27295. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  27296. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  27297. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  27298. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  27299. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  27300. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  27301. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  27302. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  27303. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  27304. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  27305. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  27306. };
  27307. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  27308. static const ASN1IntTestVector testVectors[] = {
  27309. {zeroDer, sizeof(zeroDer), 0},
  27310. {oneDer, sizeof(oneDer), 1},
  27311. {negativeDer, sizeof(negativeDer), -4123123},
  27312. {positiveDer, sizeof(positiveDer), 65537},
  27313. {primeDer, sizeof(primeDer), 0}
  27314. };
  27315. static const size_t NUM_TEST_VECTORS =
  27316. sizeof(testVectors)/sizeof(testVectors[0]);
  27317. /* Check d2i error conditions */
  27318. /* NULL pointer to input. */
  27319. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  27320. ExpectNull(b);
  27321. /* NULL input. */
  27322. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  27323. ExpectNull(b);
  27324. /* 0 length. */
  27325. p = testVectors[0].der;
  27326. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  27327. ExpectNull(b);
  27328. /* Negative length. */
  27329. p = testVectors[0].der;
  27330. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  27331. ExpectNull(b);
  27332. /* Garbage DER input. */
  27333. p = garbageDer;
  27334. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  27335. ExpectNull(b);
  27336. /* Check i2d error conditions */
  27337. /* NULL input. */
  27338. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  27339. /* 0 length input data buffer (a->length == 0). */
  27340. ExpectNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  27341. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  27342. if (a != NULL)
  27343. a->data = NULL;
  27344. /* NULL input data buffer. */
  27345. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  27346. if (a != NULL) {
  27347. /* Reset a->data. */
  27348. a->data = a->intData;
  27349. }
  27350. /* Set a to valid value. */
  27351. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  27352. /* NULL output buffer. */
  27353. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  27354. wolfSSL_ASN1_INTEGER_free(a);
  27355. a = NULL;
  27356. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  27357. p = testVectors[i].der;
  27358. ExpectNotNull(a = wolfSSL_d2i_ASN1_INTEGER(&b, &p,
  27359. testVectors[i].derSz));
  27360. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27361. if (testVectors[i].derSz <= sizeof(long)) {
  27362. ExpectNotNull(c = wolfSSL_ASN1_INTEGER_new());
  27363. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value), 1);
  27364. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  27365. wolfSSL_ASN1_INTEGER_free(c);
  27366. c = NULL;
  27367. }
  27368. /* Convert to DER without a pre-allocated output buffer. */
  27369. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  27370. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  27371. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  27372. /* Convert to DER with a pre-allocated output buffer. In this case, the
  27373. * output buffer pointer should be incremented just past the end of the
  27374. * encoded data. */
  27375. p2 = reEncoded;
  27376. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &p2)), 0);
  27377. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  27378. ExpectPtrEq(reEncoded, p2 - reEncodedSz);
  27379. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  27380. XFREE(reEncoded, NULL, DYNAMIC_TYPE_ASN1);
  27381. reEncoded = NULL;
  27382. wolfSSL_ASN1_INTEGER_free(a);
  27383. a = NULL;
  27384. }
  27385. #endif /* OPENSSL_EXTRA */
  27386. return EXPECT_RESULT();
  27387. }
  27388. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  27389. {
  27390. EXPECT_DECLS;
  27391. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  27392. BIO* bio = NULL;
  27393. BIO* out = NULL;
  27394. BIO* fixed = NULL;
  27395. ASN1_INTEGER* ai = NULL;
  27396. char buf[] = "123456\n12345\n1123456789123456\\\n78901234567890 \r\n\n";
  27397. char tmp[1024];
  27398. int tmpSz;
  27399. const char expected1[] = "123456";
  27400. const char expected2[] = "112345678912345678901234567890";
  27401. char longStr[] = "123456781234567812345678123456781234567812345678\n"
  27402. "123456781234567812345678123456781234567812345678\\\n12345678\n";
  27403. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  27404. ExpectNotNull(ai = ASN1_INTEGER_new());
  27405. ExpectNotNull(bio = BIO_new_mem_buf(buf, -1));
  27406. /* Invalid parameter testing. */
  27407. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, -1), 0);
  27408. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, NULL, -1), 0);
  27409. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, NULL, -1), 0);
  27410. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, tmp, -1), 0);
  27411. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, 1024), 0);
  27412. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, tmp, 1024), 0);
  27413. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, tmp, 1024), 0);
  27414. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, NULL, 1024), 0);
  27415. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, -1), 0);
  27416. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, NULL), 0);
  27417. ExpectIntEQ(i2a_ASN1_INTEGER(bio, NULL), 0);
  27418. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, ai), 0);
  27419. /* No data to read from BIO. */
  27420. ExpectIntEQ(a2i_ASN1_INTEGER(out, ai, tmp, 1024), 0);
  27421. /* read first line */
  27422. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27423. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  27424. XMEMSET(tmp, 0, 1024);
  27425. tmpSz = BIO_read(out, tmp, 1024);
  27426. ExpectIntEQ(tmpSz, 6);
  27427. ExpectIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  27428. /* fail on second line (not % 2) */
  27429. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  27430. /* read 3rd long line */
  27431. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27432. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  27433. XMEMSET(tmp, 0, 1024);
  27434. tmpSz = BIO_read(out, tmp, 1024);
  27435. ExpectIntEQ(tmpSz, 30);
  27436. ExpectIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  27437. /* fail on empty line */
  27438. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  27439. BIO_free(bio);
  27440. bio = NULL;
  27441. /* Make long integer, requiring dynamic memory, even longer. */
  27442. ExpectNotNull(bio = BIO_new_mem_buf(longStr, -1));
  27443. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27444. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 48);
  27445. XMEMSET(tmp, 0, 1024);
  27446. tmpSz = BIO_read(out, tmp, 1024);
  27447. ExpectIntEQ(tmpSz, 48);
  27448. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27449. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 56);
  27450. XMEMSET(tmp, 0, 1024);
  27451. tmpSz = BIO_read(out, tmp, 1024);
  27452. ExpectIntEQ(tmpSz, 56);
  27453. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(ai, 1), 1);
  27454. BIO_free(bio);
  27455. BIO_free(out);
  27456. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  27457. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27458. /* Ensure there is 0 bytes available to write into. */
  27459. ExpectIntEQ(BIO_write(fixed, tmp, 1), 1);
  27460. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  27461. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27462. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  27463. BIO_free(fixed);
  27464. ASN1_INTEGER_free(ai);
  27465. #endif
  27466. return EXPECT_RESULT();
  27467. }
  27468. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  27469. {
  27470. EXPECT_DECLS;
  27471. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27472. ASN1_INTEGER *a = NULL;
  27473. unsigned char *pp = NULL,*tpp = NULL;
  27474. int ret = 0;
  27475. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  27476. /* Invalid parameter testing. */
  27477. /* Set pp to an invalid value. */
  27478. pp = NULL;
  27479. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, &pp), 0);
  27480. ExpectIntEQ(i2c_ASN1_INTEGER(a, &pp), 0);
  27481. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, NULL), 0);
  27482. /* 40 */
  27483. if (a != NULL) {
  27484. a->intData[0] = ASN_INTEGER;
  27485. a->intData[1] = 1;
  27486. a->intData[2] = 40;
  27487. }
  27488. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27489. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27490. DYNAMIC_TYPE_TMP_BUFFER));
  27491. tpp = pp;
  27492. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27493. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27494. tpp--;
  27495. ExpectIntEQ(*tpp, 40);
  27496. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27497. pp = NULL;
  27498. /* 128 */
  27499. if (a != NULL) {
  27500. a->intData[0] = ASN_INTEGER;
  27501. a->intData[1] = 1;
  27502. a->intData[2] = 128;
  27503. }
  27504. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27505. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27506. DYNAMIC_TYPE_TMP_BUFFER));
  27507. tpp = pp;
  27508. if (tpp != NULL) {
  27509. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27510. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27511. tpp--;
  27512. ExpectIntEQ(*(tpp--), 128);
  27513. ExpectIntEQ(*tpp, 0);
  27514. }
  27515. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27516. pp = NULL;
  27517. /* -40 */
  27518. if (a != NULL) {
  27519. a->intData[0] = ASN_INTEGER;
  27520. a->intData[1] = 1;
  27521. a->intData[2] = 40;
  27522. a->negative = 1;
  27523. }
  27524. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27525. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27526. DYNAMIC_TYPE_TMP_BUFFER));
  27527. tpp = pp;
  27528. if (tpp != NULL) {
  27529. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27530. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27531. tpp--;
  27532. ExpectIntEQ(*tpp, 216);
  27533. }
  27534. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27535. pp = NULL;
  27536. /* -128 */
  27537. if (a != NULL) {
  27538. a->intData[0] = ASN_INTEGER;
  27539. a->intData[1] = 1;
  27540. a->intData[2] = 128;
  27541. a->negative = 1;
  27542. }
  27543. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27544. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27545. DYNAMIC_TYPE_TMP_BUFFER));
  27546. tpp = pp;
  27547. if (tpp != NULL) {
  27548. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27549. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27550. tpp--;
  27551. ExpectIntEQ(*tpp, 128);
  27552. }
  27553. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27554. pp = NULL;
  27555. /* -200 */
  27556. if (a != NULL) {
  27557. a->intData[0] = ASN_INTEGER;
  27558. a->intData[1] = 1;
  27559. a->intData[2] = 200;
  27560. a->negative = 1;
  27561. }
  27562. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27563. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27564. DYNAMIC_TYPE_TMP_BUFFER));
  27565. tpp = pp;
  27566. if (tpp != NULL) {
  27567. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27568. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27569. tpp--;
  27570. ExpectIntEQ(*(tpp--), 56);
  27571. ExpectIntEQ(*tpp, 255);
  27572. }
  27573. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27574. pp = NULL;
  27575. /* Empty */
  27576. if (a != NULL) {
  27577. a->intData[0] = ASN_INTEGER;
  27578. a->intData[1] = 0;
  27579. a->negative = 0;
  27580. }
  27581. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27582. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27583. DYNAMIC_TYPE_TMP_BUFFER));
  27584. tpp = pp;
  27585. if (tpp != NULL) {
  27586. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27587. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27588. tpp--;
  27589. ExpectIntEQ(*tpp, 0);
  27590. }
  27591. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27592. pp = NULL;
  27593. /* 0 */
  27594. if (a != NULL) {
  27595. a->intData[0] = ASN_INTEGER;
  27596. a->intData[1] = 1;
  27597. a->intData[2] = 0;
  27598. a->negative = 1;
  27599. }
  27600. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27601. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27602. DYNAMIC_TYPE_TMP_BUFFER));
  27603. if (tpp != NULL) {
  27604. tpp = pp;
  27605. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27606. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27607. tpp--;
  27608. ExpectIntEQ(*tpp, 0);
  27609. }
  27610. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27611. pp = NULL;
  27612. /* 0x100 */
  27613. if (a != NULL) {
  27614. a->intData[0] = ASN_INTEGER;
  27615. a->intData[1] = 2;
  27616. a->intData[2] = 0x01;
  27617. a->intData[3] = 0x00;
  27618. a->negative = 0;
  27619. }
  27620. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27621. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27622. DYNAMIC_TYPE_TMP_BUFFER));
  27623. if (tpp != NULL) {
  27624. tpp = pp;
  27625. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27626. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27627. tpp -= 2;
  27628. ExpectIntEQ(tpp[0], 0x01);
  27629. ExpectIntEQ(tpp[1], 0x00);
  27630. }
  27631. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27632. pp = NULL;
  27633. /* -0x8000 => 0x8000 */
  27634. if (a != NULL) {
  27635. a->intData[0] = ASN_INTEGER;
  27636. a->intData[1] = 2;
  27637. a->intData[2] = 0x80;
  27638. a->intData[3] = 0x00;
  27639. a->negative = 1;
  27640. }
  27641. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27642. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27643. DYNAMIC_TYPE_TMP_BUFFER));
  27644. tpp = pp;
  27645. if (tpp != NULL) {
  27646. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27647. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27648. tpp -= 2;
  27649. ExpectIntEQ(tpp[0], 0x80);
  27650. ExpectIntEQ(tpp[1], 0x00);
  27651. }
  27652. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27653. pp = NULL;
  27654. /* -0x8001 => 0xFF7FFF */
  27655. if (a != NULL) {
  27656. a->intData[0] = ASN_INTEGER;
  27657. a->intData[1] = 2;
  27658. a->intData[2] = 0x80;
  27659. a->intData[3] = 0x01;
  27660. a->negative = 1;
  27661. }
  27662. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 3);
  27663. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27664. DYNAMIC_TYPE_TMP_BUFFER));
  27665. tpp = pp;
  27666. if (tpp != NULL) {
  27667. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27668. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 3);
  27669. tpp -= 3;
  27670. ExpectIntEQ(tpp[0], 0xFF);
  27671. ExpectIntEQ(tpp[1], 0x7F);
  27672. ExpectIntEQ(tpp[2], 0xFF);
  27673. }
  27674. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27675. wolfSSL_ASN1_INTEGER_free(a);
  27676. #endif /* OPENSSL_EXTRA && !NO_ASN */
  27677. return EXPECT_RESULT();
  27678. }
  27679. static int test_wolfSSL_ASN1_OBJECT(void)
  27680. {
  27681. EXPECT_DECLS;
  27682. #if defined(OPENSSL_EXTRA)
  27683. ASN1_OBJECT* a = NULL;
  27684. ASN1_OBJECT s;
  27685. const unsigned char der[] = { 0x06, 0x01, 0x00 };
  27686. /* Invalid parameter testing. */
  27687. ASN1_OBJECT_free(NULL);
  27688. ExpectNull(wolfSSL_ASN1_OBJECT_dup(NULL));
  27689. /* Test that a static ASN1_OBJECT can be freed. */
  27690. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  27691. ASN1_OBJECT_free(&s);
  27692. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  27693. ASN1_OBJECT_free(a);
  27694. a = NULL;
  27695. s.obj = der;
  27696. s.objSz = sizeof(der);
  27697. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  27698. ASN1_OBJECT_free(a);
  27699. ASN1_OBJECT_free(&s);
  27700. #endif /* OPENSSL_EXTRA */
  27701. return EXPECT_RESULT();
  27702. }
  27703. static int test_wolfSSL_ASN1_get_object(void)
  27704. {
  27705. EXPECT_DECLS;
  27706. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  27707. const unsigned char* derBuf = cliecc_cert_der_256;
  27708. const unsigned char* nullPtr = NULL;
  27709. const unsigned char objDerInvalidLen[] = { 0x30, 0x81 };
  27710. const unsigned char objDerBadLen[] = { 0x30, 0x04 };
  27711. const unsigned char objDerNotObj[] = { 0x02, 0x01, 0x00 };
  27712. const unsigned char objDerNoData[] = { 0x06, 0x00 };
  27713. const unsigned char* p;
  27714. unsigned char objDer[8];
  27715. unsigned char* der;
  27716. unsigned char* derPtr;
  27717. int len = sizeof_cliecc_cert_der_256;
  27718. long asnLen = 0;
  27719. int tag = 0;
  27720. int cls = 0;
  27721. ASN1_OBJECT* a = NULL;
  27722. ASN1_OBJECT s;
  27723. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  27724. /* Invalid encoding at length. */
  27725. p = objDerInvalidLen;
  27726. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  27727. 0x80);
  27728. p = objDerBadLen;
  27729. /* Error = 0x80, Constructed = 0x20 */
  27730. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  27731. 0x80 | 0x20);
  27732. /* Read a couple TLV triplets and make sure they match the expected values
  27733. */
  27734. /* SEQUENCE */
  27735. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  27736. ExpectIntEQ(asnLen, 861);
  27737. ExpectIntEQ(tag, 0x10);
  27738. ExpectIntEQ(cls, 0);
  27739. /* SEQUENCE */
  27740. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27741. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27742. ExpectIntEQ(asnLen, 772);
  27743. ExpectIntEQ(tag, 0x10);
  27744. ExpectIntEQ(cls, 0);
  27745. /* [0] */
  27746. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27747. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27748. ExpectIntEQ(asnLen, 3);
  27749. ExpectIntEQ(tag, 0);
  27750. ExpectIntEQ(cls, 0x80);
  27751. /* INTEGER */
  27752. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27753. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27754. ExpectIntEQ(asnLen, 1);
  27755. ExpectIntEQ(tag, 0x2);
  27756. ExpectIntEQ(cls, 0);
  27757. derBuf += asnLen;
  27758. /* INTEGER */
  27759. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27760. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27761. ExpectIntEQ(asnLen, 20);
  27762. ExpectIntEQ(tag, 0x2);
  27763. ExpectIntEQ(cls, 0);
  27764. derBuf += asnLen;
  27765. /* SEQUENCE */
  27766. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27767. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27768. ExpectIntEQ(asnLen, 10);
  27769. ExpectIntEQ(tag, 0x10);
  27770. ExpectIntEQ(cls, 0);
  27771. /* Found OBJECT_ID. */
  27772. /* Invalid parameter testing. */
  27773. ExpectIntEQ(ASN1_get_object(NULL, NULL, NULL, NULL, 0), 0x80);
  27774. ExpectIntEQ(ASN1_get_object(&nullPtr, NULL, NULL, NULL, 0), 0x80);
  27775. ExpectIntEQ(ASN1_get_object(NULL, &asnLen, &tag, &cls, len), 0x80);
  27776. ExpectIntEQ(ASN1_get_object(&nullPtr, &asnLen, &tag, &cls, len), 0x80);
  27777. ExpectIntEQ(ASN1_get_object(&derBuf, NULL, &tag, &cls, len), 0x80);
  27778. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, NULL, &cls, len), 0x80);
  27779. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, NULL, len), 0x80);
  27780. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, 0), 0x80);
  27781. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, -1), 0x80);
  27782. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, -1));
  27783. ExpectNull(d2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  27784. ExpectNull(d2i_ASN1_OBJECT(NULL, &derBuf, -1));
  27785. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, 0));
  27786. ExpectNull(d2i_ASN1_OBJECT(&a, NULL, len));
  27787. ExpectNull(d2i_ASN1_OBJECT(&a, &nullPtr, len));
  27788. ExpectNull(d2i_ASN1_OBJECT(&a, &derBuf, -1));
  27789. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, -1));
  27790. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  27791. ExpectNull(c2i_ASN1_OBJECT(NULL, &derBuf, -1));
  27792. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, 1));
  27793. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, 1));
  27794. /* Invalid encoding at length. */
  27795. p = objDerInvalidLen;
  27796. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerInvalidLen)));
  27797. p = objDerBadLen;
  27798. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerBadLen)));
  27799. p = objDerNotObj;
  27800. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNotObj)));
  27801. p = objDerNoData;
  27802. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNoData)));
  27803. /* Create an ASN OBJECT from content */
  27804. p = derBuf + 2;
  27805. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 8));
  27806. ASN1_OBJECT_free(a);
  27807. a = NULL;
  27808. /* Create an ASN OBJECT from DER */
  27809. ExpectNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  27810. /* Invalid parameter testing. */
  27811. ExpectIntEQ(i2d_ASN1_OBJECT(NULL, NULL), 0);
  27812. ExpectIntEQ(i2d_ASN1_OBJECT(&s, NULL), 0);
  27813. ExpectIntEQ(i2d_ASN1_OBJECT(a, NULL), 8);
  27814. der = NULL;
  27815. ExpectIntEQ(i2d_ASN1_OBJECT(a, &der), 8);
  27816. derPtr = objDer;
  27817. ExpectIntEQ(i2d_ASN1_OBJECT(a, &derPtr), 8);
  27818. ExpectPtrNE(derPtr, objDer);
  27819. ExpectIntEQ(XMEMCMP(der, objDer, 8), 0);
  27820. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  27821. ASN1_OBJECT_free(a);
  27822. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  27823. return EXPECT_RESULT();
  27824. }
  27825. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  27826. {
  27827. EXPECT_DECLS;
  27828. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  27829. ASN1_OBJECT* obj = NULL;
  27830. ASN1_OBJECT* a = NULL;
  27831. BIO *bio = NULL;
  27832. const unsigned char notObjDer[] = { 0x04, 0x01, 0xff };
  27833. const unsigned char badLenDer[] = { 0x06, 0x04, 0x01 };
  27834. const unsigned char goodDer[] = { 0x06, 0x01, 0x01 };
  27835. const unsigned char* p;
  27836. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  27837. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  27838. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  27839. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  27840. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  27841. /* No DER encoding in ASN1_OBJECT. */
  27842. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_new());
  27843. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  27844. ASN1_OBJECT_free(a);
  27845. a = NULL;
  27846. /* DER encoding - not OBJECT_ID */
  27847. p = notObjDer;
  27848. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  27849. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  27850. ASN1_OBJECT_free(a);
  27851. a = NULL;
  27852. /* Bad length encoding. */
  27853. p = badLenDer;
  27854. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  27855. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  27856. ASN1_OBJECT_free(a);
  27857. a = NULL;
  27858. /* Good encoding - but unknown. */
  27859. p = goodDer;
  27860. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  27861. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  27862. ASN1_OBJECT_free(a);
  27863. BIO_free(bio);
  27864. ASN1_OBJECT_free(obj);
  27865. #endif
  27866. return EXPECT_RESULT();
  27867. }
  27868. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  27869. {
  27870. EXPECT_DECLS;
  27871. #if defined(OPENSSL_EXTRA) && \
  27872. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  27873. char buf[50] = {0};
  27874. ASN1_OBJECT* obj;
  27875. const char* oid = "2.5.29.19";
  27876. const char* ln = "X509v3 Basic Constraints";
  27877. obj = NULL;
  27878. ExpectIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), 0);
  27879. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), 0);
  27880. ExpectIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), 0);
  27881. ExpectNotNull(obj = OBJ_txt2obj(oid, 0));
  27882. XMEMSET(buf, 0, sizeof(buf));
  27883. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  27884. ExpectIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  27885. ASN1_OBJECT_free(obj);
  27886. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  27887. return EXPECT_RESULT();
  27888. }
  27889. static int test_wolfSSL_sk_ASN1_OBJECT(void)
  27890. {
  27891. EXPECT_DECLS;
  27892. #if !defined(NO_ASN) && (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  27893. WOLFSSL_STACK* sk = NULL;
  27894. WOLFSSL_ASN1_OBJECT* obj;
  27895. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  27896. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  27897. wolfSSL_sk_ASN1_OBJECT_free(sk);
  27898. sk = NULL;
  27899. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  27900. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, NULL), 0);
  27901. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, NULL), 0);
  27902. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, obj), 0);
  27903. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  27904. wolfSSL_sk_ASN1_OBJECT_pop_free(sk, NULL);
  27905. sk = NULL;
  27906. /* obj freed in pop_free call. */
  27907. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  27908. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  27909. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  27910. ExpectPtrEq(obj, wolfSSL_sk_ASN1_OBJECT_pop(sk));
  27911. wolfSSL_sk_ASN1_OBJECT_free(sk);
  27912. wolfSSL_ASN1_OBJECT_free(obj);
  27913. #endif /* !NO_ASN && (OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL) */
  27914. return EXPECT_RESULT();
  27915. }
  27916. static int test_wolfSSL_ASN1_STRING(void)
  27917. {
  27918. EXPECT_DECLS;
  27919. #if defined(OPENSSL_EXTRA)
  27920. ASN1_STRING* str = NULL;
  27921. ASN1_STRING* c = NULL;
  27922. const char data[] = "hello wolfSSL";
  27923. const char data2[] = "Same len data";
  27924. const char longData[] =
  27925. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  27926. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  27927. ASN1_STRING_free(str);
  27928. str = NULL;
  27929. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  27930. ExpectIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  27931. ExpectIntEQ(ASN1_STRING_type(NULL), 0);
  27932. /* Check setting to NULL works. */
  27933. ExpectIntEQ(ASN1_STRING_set(str, NULL, 0), 1);
  27934. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  27935. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  27936. ExpectIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  27937. ExpectIntEQ(ASN1_STRING_set(NULL, NULL, 0), 0);
  27938. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, NULL), 0);
  27939. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(str, NULL), 0);
  27940. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, str), 0);
  27941. ExpectNull(wolfSSL_ASN1_STRING_dup(NULL));
  27942. ExpectNotNull(c = wolfSSL_ASN1_STRING_dup(str));
  27943. ExpectIntEQ(ASN1_STRING_cmp(NULL, NULL), -1);
  27944. ExpectIntEQ(ASN1_STRING_cmp(str, NULL), -1);
  27945. ExpectIntEQ(ASN1_STRING_cmp(NULL, c), -1);
  27946. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  27947. ExpectIntEQ(ASN1_STRING_set(c, (const void*)data2, -1), 1);
  27948. ExpectIntGT(ASN1_STRING_cmp(str, c), 0);
  27949. ExpectIntEQ(ASN1_STRING_set(str, (const void*)longData, -1), 1);
  27950. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(c, str), 1);
  27951. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  27952. /* Check setting back to smaller size frees dynamic data. */
  27953. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  27954. ExpectIntLT(ASN1_STRING_cmp(str, c), 0);
  27955. ExpectIntGT(ASN1_STRING_cmp(c, str), 0);
  27956. ExpectNull(ASN1_STRING_get0_data(NULL));
  27957. ExpectNotNull(ASN1_STRING_get0_data(str));
  27958. ExpectNull(ASN1_STRING_data(NULL));
  27959. ExpectNotNull(ASN1_STRING_data(str));
  27960. ExpectIntEQ(ASN1_STRING_length(NULL), 0);
  27961. ExpectIntGT(ASN1_STRING_length(str), 0);
  27962. ASN1_STRING_free(c);
  27963. ASN1_STRING_free(str);
  27964. ASN1_STRING_free(NULL);
  27965. #ifndef NO_WOLFSSL_STUB
  27966. ExpectNull(d2i_DISPLAYTEXT(NULL, NULL, 0));
  27967. #endif
  27968. #endif
  27969. return EXPECT_RESULT();
  27970. }
  27971. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  27972. {
  27973. EXPECT_DECLS;
  27974. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_RSA) && \
  27975. !defined(NO_FILESYSTEM)
  27976. WOLFSSL_X509* x509 = NULL;
  27977. WOLFSSL_X509_NAME* subject = NULL;
  27978. WOLFSSL_X509_NAME_ENTRY* e = NULL;
  27979. WOLFSSL_ASN1_STRING* a = NULL;
  27980. FILE* file = XBADFILE;
  27981. int idx = 0;
  27982. char targetOutput[16] = "www.wolfssl.com";
  27983. unsigned char* actual_output = NULL;
  27984. int len = 0;
  27985. ExpectNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  27986. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  27987. if (file != NULL)
  27988. fclose(file);
  27989. /* wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName */
  27990. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  27991. ExpectIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  27992. NID_commonName, -1)), 5);
  27993. ExpectNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  27994. ExpectNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  27995. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  27996. ExpectIntEQ(strncmp((const char*)actual_output, targetOutput, len), 0);
  27997. a = NULL;
  27998. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
  27999. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)), -1);
  28000. /* wolfSSL_ASN1_STRING_to_UTF8(valid, NULL) */
  28001. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)), -1);
  28002. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL) */
  28003. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)), -1);
  28004. wolfSSL_X509_free(x509);
  28005. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28006. ExpectNotNull(a = ASN1_STRING_new());
  28007. ExpectIntEQ(wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a), -1);
  28008. ASN1_STRING_free(a);
  28009. #endif
  28010. return EXPECT_RESULT();
  28011. }
  28012. static int test_wolfSSL_i2s_ASN1_STRING(void)
  28013. {
  28014. EXPECT_DECLS;
  28015. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  28016. WOLFSSL_ASN1_STRING* str = NULL;
  28017. const char* data = "test_wolfSSL_i2s_ASN1_STRING";
  28018. char* ret = NULL;
  28019. ExpectNotNull(str = ASN1_STRING_new());
  28020. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, NULL));
  28021. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28022. ret = NULL;
  28023. /* No data. */
  28024. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  28025. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28026. ret = NULL;
  28027. ExpectIntEQ(ASN1_STRING_set(str, data, 0), 1);
  28028. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  28029. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28030. ret = NULL;
  28031. ExpectIntEQ(ASN1_STRING_set(str, data, -1), 1);
  28032. /* No type. */
  28033. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  28034. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28035. ASN1_STRING_free(str);
  28036. #endif
  28037. return EXPECT_RESULT();
  28038. }
  28039. static int test_wolfSSL_ASN1_STRING_canon(void)
  28040. {
  28041. EXPECT_DECLS;
  28042. #if defined(WOLFSSL_TEST_STATIC_BUILD)
  28043. #if !defined(NO_CERTS) && (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
  28044. defined(OPENSSL_EXTRA_X509_SMALL))
  28045. WOLFSSL_ASN1_STRING* orig = NULL;
  28046. WOLFSSL_ASN1_STRING* canon = NULL;
  28047. const char* data = "test_wolfSSL_ASN1_STRING_canon";
  28048. const char* whitespaceOnly = "\t\r\n";
  28049. const char* modData = " \x01\f\t\x02\r\n\v\xff\nTt \n";
  28050. const char* canonData = "\x01 \x02 \xff tt";
  28051. const char longData[] =
  28052. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  28053. ExpectNotNull(orig = ASN1_STRING_new());
  28054. ExpectNotNull(canon = ASN1_STRING_new());
  28055. /* Invalid parameter testing. */
  28056. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, NULL), BAD_FUNC_ARG);
  28057. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, NULL), BAD_FUNC_ARG);
  28058. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, orig), BAD_FUNC_ARG);
  28059. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28060. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28061. ExpectIntEQ(ASN1_STRING_set(orig, longData, (int)XSTRLEN(data)), 1);
  28062. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28063. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28064. ExpectIntEQ(ASN1_STRING_set(orig, data, (int)XSTRLEN(data)), 1);
  28065. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28066. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28067. ASN1_STRING_free(orig);
  28068. orig = NULL;
  28069. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  28070. ExpectIntEQ(ASN1_STRING_set(orig, modData, 15), 1);
  28071. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28072. ExpectIntEQ(ASN1_STRING_set(orig, canonData, 8), 1);
  28073. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28074. ASN1_STRING_free(orig);
  28075. orig = NULL;
  28076. ExpectNotNull(orig = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  28077. ExpectIntEQ(ASN1_STRING_set(orig, whitespaceOnly, 3), 1);
  28078. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28079. ASN1_STRING_free(orig);
  28080. orig = NULL;
  28081. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  28082. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28083. ASN1_STRING_free(orig);
  28084. ASN1_STRING_free(canon);
  28085. #endif
  28086. #endif
  28087. return EXPECT_RESULT();
  28088. }
  28089. static int test_wolfSSL_ASN1_STRING_print(void)
  28090. {
  28091. EXPECT_DECLS;
  28092. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS) && \
  28093. !defined(NO_BIO)
  28094. ASN1_STRING* asnStr = NULL;
  28095. const char HELLO_DATA[]= \
  28096. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  28097. #define MAX_UNPRINTABLE_CHAR 32
  28098. #define MAX_BUF 255
  28099. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  28100. unsigned char expected[sizeof(unprintableData)+1];
  28101. unsigned char rbuf[MAX_BUF];
  28102. BIO *bio = NULL;
  28103. int p_len;
  28104. int i;
  28105. /* setup */
  28106. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  28107. unprintableData[i] = HELLO_DATA[i];
  28108. expected[i] = HELLO_DATA[i];
  28109. }
  28110. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  28111. unprintableData[sizeof(HELLO_DATA)+i] = i;
  28112. if (i == (int)'\n' || i == (int)'\r')
  28113. expected[sizeof(HELLO_DATA)+i] = i;
  28114. else
  28115. expected[sizeof(HELLO_DATA)+i] = '.';
  28116. }
  28117. unprintableData[sizeof(unprintableData)-1] = '\0';
  28118. expected[sizeof(expected)-1] = '\0';
  28119. XMEMSET(rbuf, 0, MAX_BUF);
  28120. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28121. ExpectIntEQ(BIO_set_write_buf_size(bio, MAX_BUF), 0);
  28122. ExpectNotNull(asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28123. ExpectIntEQ(ASN1_STRING_set(asnStr,(const void*)unprintableData,
  28124. (int)sizeof(unprintableData)), 1);
  28125. /* test */
  28126. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, NULL), 0);
  28127. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, NULL), 0);
  28128. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, asnStr), 0);
  28129. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print(bio, asnStr), 46);
  28130. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 46), 46);
  28131. ExpectStrEQ((char*)rbuf, (const char*)expected);
  28132. BIO_free(bio);
  28133. bio = NULL;
  28134. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28135. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28136. /* Ensure there is 0 bytes available to write into. */
  28137. ExpectIntEQ(BIO_write(bio, rbuf, 1), 1);
  28138. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28139. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28140. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28141. ExpectIntEQ(BIO_set_write_buf_size(bio, 45), 1);
  28142. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28143. BIO_free(bio);
  28144. ASN1_STRING_free(asnStr);
  28145. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS && !NO_BIO */
  28146. return EXPECT_RESULT();
  28147. }
  28148. static int test_wolfSSL_ASN1_STRING_print_ex(void)
  28149. {
  28150. EXPECT_DECLS;
  28151. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  28152. ASN1_STRING* asn_str = NULL;
  28153. const char data[] = "Hello wolfSSL!";
  28154. ASN1_STRING* esc_str = NULL;
  28155. const char esc_data[] = "a+;<>";
  28156. ASN1_STRING* neg_int = NULL;
  28157. const char neg_int_data[] = "\xff";
  28158. ASN1_STRING* neg_enum = NULL;
  28159. const char neg_enum_data[] = "\xff";
  28160. BIO *bio = NULL;
  28161. BIO *fixed = NULL;
  28162. unsigned long flags;
  28163. int p_len;
  28164. unsigned char rbuf[255];
  28165. /* setup */
  28166. XMEMSET(rbuf, 0, 255);
  28167. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28168. ExpectIntEQ(BIO_set_write_buf_size(bio, 255), 0);
  28169. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28170. ExpectNotNull(asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28171. ExpectIntEQ(ASN1_STRING_set(asn_str, (const void*)data, sizeof(data)), 1);
  28172. ExpectNotNull(esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28173. ExpectIntEQ(ASN1_STRING_set(esc_str, (const void*)esc_data,
  28174. sizeof(esc_data)), 1);
  28175. ExpectNotNull(neg_int = ASN1_STRING_type_new(V_ASN1_NEG_INTEGER));
  28176. ExpectIntEQ(ASN1_STRING_set(neg_int, (const void*)neg_int_data,
  28177. sizeof(neg_int_data) - 1), 1);
  28178. ExpectNotNull(neg_enum = ASN1_STRING_type_new(V_ASN1_NEG_ENUMERATED));
  28179. ExpectIntEQ(ASN1_STRING_set(neg_enum, (const void*)neg_enum_data,
  28180. sizeof(neg_enum_data) - 1), 1);
  28181. /* Invalid parameter testing. */
  28182. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, NULL, 0), 0);
  28183. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(bio, NULL, 0), 0);
  28184. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, asn_str, 0), 0);
  28185. /* no flags */
  28186. XMEMSET(rbuf, 0, 255);
  28187. flags = 0;
  28188. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 15);
  28189. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 15), 15);
  28190. ExpectStrEQ((char*)rbuf, "Hello wolfSSL!");
  28191. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28192. /* Ensure there is 0 bytes available to write into. */
  28193. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28194. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28195. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28196. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28197. ExpectIntEQ(BIO_set_write_buf_size(fixed, 14), 1);
  28198. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28199. /* RFC2253 Escape */
  28200. XMEMSET(rbuf, 0, 255);
  28201. flags = ASN1_STRFLGS_ESC_2253;
  28202. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags), 9);
  28203. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 9), 9);
  28204. ExpectStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  28205. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28206. /* Ensure there is 0 bytes available to write into. */
  28207. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28208. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28209. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28210. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28211. ExpectIntEQ(BIO_set_write_buf_size(fixed, 8), 1);
  28212. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28213. /* Show type */
  28214. XMEMSET(rbuf, 0, 255);
  28215. flags = ASN1_STRFLGS_SHOW_TYPE;
  28216. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 28);
  28217. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 28), 28);
  28218. ExpectStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  28219. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28220. /* Ensure there is 0 bytes available to write into. */
  28221. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28222. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28223. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28224. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28225. ExpectIntEQ(BIO_set_write_buf_size(fixed, 12), 1);
  28226. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28227. ExpectIntEQ(BIO_set_write_buf_size(fixed, 27), 1);
  28228. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28229. /* Dump All */
  28230. XMEMSET(rbuf, 0, 255);
  28231. flags = ASN1_STRFLGS_DUMP_ALL;
  28232. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 31);
  28233. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 31), 31);
  28234. ExpectStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  28235. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28236. /* Ensure there is 0 bytes available to write into. */
  28237. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28238. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28239. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28240. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28241. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  28242. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28243. /* Dump Der */
  28244. XMEMSET(rbuf, 0, 255);
  28245. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  28246. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 35);
  28247. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 35), 35);
  28248. ExpectStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  28249. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28250. /* Ensure there is 0 bytes available to write into. */
  28251. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28252. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28253. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28254. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28255. ExpectIntEQ(BIO_set_write_buf_size(fixed, 2), 1);
  28256. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28257. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  28258. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28259. /* Dump All + Show type */
  28260. XMEMSET(rbuf, 0, 255);
  28261. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28262. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 44);
  28263. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 44), 44);
  28264. ExpectStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  28265. /* Dump All + Show type - Negative Integer. */
  28266. XMEMSET(rbuf, 0, 255);
  28267. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28268. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_int, flags), 11);
  28269. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 11), 11);
  28270. ExpectStrEQ((char*)rbuf, "INTEGER:#FF");
  28271. /* Dump All + Show type - Negative Enumerated. */
  28272. XMEMSET(rbuf, 0, 255);
  28273. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28274. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_enum, flags), 14);
  28275. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 14), 14);
  28276. ExpectStrEQ((char*)rbuf, "ENUMERATED:#FF");
  28277. BIO_free(fixed);
  28278. BIO_free(bio);
  28279. ASN1_STRING_free(asn_str);
  28280. ASN1_STRING_free(esc_str);
  28281. ASN1_STRING_free(neg_int);
  28282. ASN1_STRING_free(neg_enum);
  28283. ExpectStrEQ(wolfSSL_ASN1_tag2str(-1), "(unknown)");
  28284. ExpectStrEQ(wolfSSL_ASN1_tag2str(31), "(unknown)");
  28285. #endif
  28286. return EXPECT_RESULT();
  28287. }
  28288. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  28289. {
  28290. EXPECT_DECLS;
  28291. #if defined(OPENSSL_ALL) && !defined(NO_ASN)
  28292. ASN1_STRING* asn1str_test = NULL;
  28293. ASN1_STRING* asn1str_answer = NULL;
  28294. /* Each character is encoded using 4 bytes */
  28295. char input[] = {
  28296. 0, 0, 0, 'T',
  28297. 0, 0, 0, 'e',
  28298. 0, 0, 0, 's',
  28299. 0, 0, 0, 't',
  28300. };
  28301. char output[] = "Test";
  28302. char badInput[] = {
  28303. 1, 0, 0, 'T',
  28304. 0, 1, 0, 'e',
  28305. 0, 0, 1, 's',
  28306. };
  28307. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(NULL), 0);
  28308. /* Test wrong type. */
  28309. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28310. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28311. ASN1_STRING_free(asn1str_test);
  28312. asn1str_test = NULL;
  28313. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  28314. /* Test bad length. */
  28315. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input) - 1), 1);
  28316. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28317. /* Test bad input. */
  28318. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 0, 4), 1);
  28319. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28320. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 4, 4), 1);
  28321. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28322. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 8, 4), 1);
  28323. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28324. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  28325. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  28326. ExpectNotNull(
  28327. asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  28328. ExpectIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  28329. ExpectIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  28330. ASN1_STRING_free(asn1str_test);
  28331. ASN1_STRING_free(asn1str_answer);
  28332. #endif /* OPENSSL_ALL && !NO_ASN */
  28333. return EXPECT_RESULT();
  28334. }
  28335. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  28336. {
  28337. EXPECT_DECLS;
  28338. #if defined(OPENSSL_EXTRA)
  28339. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime = NULL;
  28340. unsigned char nullstr[32];
  28341. XMEMSET(nullstr, 0, 32);
  28342. ExpectNotNull(asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  28343. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL, DYNAMIC_TYPE_TMP_BUFFER));
  28344. if (asn1_gtime != NULL) {
  28345. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  28346. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  28347. ExpectIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  28348. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28349. }
  28350. wolfSSL_ASN1_GENERALIZEDTIME_free(NULL);
  28351. #endif /* OPENSSL_EXTRA */
  28352. return EXPECT_RESULT();
  28353. }
  28354. static int test_wolfSSL_ASN1_GENERALIZEDTIME_print(void)
  28355. {
  28356. EXPECT_DECLS;
  28357. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  28358. WOLFSSL_ASN1_GENERALIZEDTIME gtime;
  28359. BIO* bio = NULL;
  28360. unsigned char buf[24];
  28361. int i;
  28362. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28363. BIO_set_write_buf_size(bio, 24);
  28364. XMEMSET(&gtime, 0, sizeof(WOLFSSL_ASN1_GENERALIZEDTIME));
  28365. XMEMCPY(gtime.data, "20180504123500Z", ASN_GENERALIZED_TIME_SIZE);
  28366. gtime.length = ASN_GENERALIZED_TIME_SIZE;
  28367. /* Type not set. */
  28368. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  28369. gtime.type = V_ASN1_GENERALIZEDTIME;
  28370. /* Invalid parameters testing. */
  28371. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, NULL), BAD_FUNC_ARG);
  28372. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, NULL), BAD_FUNC_ARG);
  28373. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, &gtime), BAD_FUNC_ARG);
  28374. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 1);
  28375. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 20);
  28376. ExpectIntEQ(XMEMCMP(buf, "May 04 12:35:00 2018", 20), 0);
  28377. BIO_free(bio);
  28378. bio = NULL;
  28379. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28380. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28381. /* Ensure there is 0 bytes available to write into. */
  28382. ExpectIntEQ(BIO_write(bio, buf, 1), 1);
  28383. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  28384. for (i = 1; i < 20; i++) {
  28385. ExpectIntEQ(BIO_set_write_buf_size(bio, i), 1);
  28386. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  28387. }
  28388. BIO_free(bio);
  28389. wolfSSL_ASN1_GENERALIZEDTIME_free(&gtime);
  28390. #endif /* OPENSSL_EXTRA */
  28391. return EXPECT_RESULT();
  28392. }
  28393. static int test_wolfSSL_ASN1_TIME(void)
  28394. {
  28395. EXPECT_DECLS;
  28396. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28397. WOLFSSL_ASN1_TIME* asn_time = NULL;
  28398. unsigned char *data;
  28399. ExpectNotNull(asn_time = ASN1_TIME_new());
  28400. #ifndef NO_WOLFSSL_STUB
  28401. ExpectNotNull(ASN1_TIME_set(asn_time, 1));
  28402. #endif
  28403. ExpectIntEQ(ASN1_TIME_set_string(NULL, NULL), 0);
  28404. ExpectIntEQ(ASN1_TIME_set_string(asn_time, NULL), 0);
  28405. ExpectIntEQ(ASN1_TIME_set_string(NULL,
  28406. "String longer than CTC_DATA_SIZE that is 32 bytes"), 0);
  28407. ExpectIntEQ(ASN1_TIME_set_string(NULL, "101219181011Z"), 1);
  28408. ExpectIntEQ(ASN1_TIME_set_string(asn_time, "101219181011Z"), 1);
  28409. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(NULL), 0);
  28410. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(asn_time), ASN_UTC_TIME_SIZE - 1);
  28411. ExpectNull(wolfSSL_ASN1_TIME_get_data(NULL));
  28412. ExpectNotNull(data = wolfSSL_ASN1_TIME_get_data(asn_time));
  28413. ExpectIntEQ(XMEMCMP(data, "101219181011Z", 14), 0);
  28414. ExpectIntEQ(ASN1_TIME_check(NULL), 0);
  28415. ExpectIntEQ(ASN1_TIME_check(asn_time), 1);
  28416. ASN1_TIME_free(asn_time);
  28417. ASN1_TIME_free(NULL);
  28418. #endif
  28419. return EXPECT_RESULT();
  28420. }
  28421. static int test_wolfSSL_ASN1_TIME_to_string(void)
  28422. {
  28423. EXPECT_DECLS;
  28424. #ifndef NO_ASN_TIME
  28425. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28426. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  28427. WOLFSSL_ASN1_TIME* t = NULL;
  28428. char buf[ASN_GENERALIZED_TIME_SIZE];
  28429. ExpectNotNull((t = ASN1_TIME_new()));
  28430. ExpectIntEQ(ASN1_TIME_set_string(t, "030222211515Z"), 1);
  28431. /* Invalid parameter testing. */
  28432. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 4));
  28433. ExpectNull(ASN1_TIME_to_string(t, NULL, 4));
  28434. ExpectNull(ASN1_TIME_to_string(NULL, buf, 4));
  28435. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 5));
  28436. ExpectNull(ASN1_TIME_to_string(NULL, buf, 5));
  28437. ExpectNull(ASN1_TIME_to_string(t, NULL, 5));
  28438. ExpectNull(ASN1_TIME_to_string(t, buf, 4));
  28439. /* Buffer needs to be longer than minimum of 5 characters. */
  28440. ExpectNull(ASN1_TIME_to_string(t, buf, 5));
  28441. ASN1_TIME_free(t);
  28442. #endif
  28443. #endif /* NO_ASN_TIME */
  28444. return EXPECT_RESULT();
  28445. }
  28446. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  28447. {
  28448. EXPECT_DECLS;
  28449. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28450. ASN1_TIME* fromTime = NULL;
  28451. ASN1_TIME* closeToTime = NULL;
  28452. ASN1_TIME* toTime = NULL;
  28453. ASN1_TIME* invalidTime = NULL;
  28454. int daysDiff = 0;
  28455. int secsDiff = 0;
  28456. ExpectNotNull((fromTime = ASN1_TIME_new()));
  28457. /* Feb 22, 2003, 21:15:15 */
  28458. ExpectIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), 1);
  28459. ExpectNotNull((closeToTime = ASN1_TIME_new()));
  28460. /* Feb 22, 2003, 21:16:15 */
  28461. ExpectIntEQ(ASN1_TIME_set_string(closeToTime, "030222211615Z"), 1);
  28462. ExpectNotNull((toTime = ASN1_TIME_new()));
  28463. /* Dec 19, 2010, 18:10:11 */
  28464. ExpectIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), 1);
  28465. ExpectNotNull((invalidTime = ASN1_TIME_new()));
  28466. /* Dec 19, 2010, 18:10:11 but 'U' instead of 'Z' which is invalid. */
  28467. ExpectIntEQ(ASN1_TIME_set_string(invalidTime, "102519181011U"), 1);
  28468. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, invalidTime), 0);
  28469. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, invalidTime, toTime), 0);
  28470. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28471. /* Error conditions. */
  28472. ExpectIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime), 0);
  28473. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime), 0);
  28474. /* If both times are NULL, difference is 0. */
  28475. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL), 1);
  28476. ExpectIntEQ(daysDiff, 0);
  28477. ExpectIntEQ(secsDiff, 0);
  28478. /* If one time is NULL, it defaults to the current time. */
  28479. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime), 1);
  28480. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL), 1);
  28481. /* Normal operation. Both times non-NULL. */
  28482. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28483. ExpectIntEQ(daysDiff, 2856);
  28484. ExpectIntEQ(secsDiff, 75296);
  28485. /* Swapping the times should return negative values. */
  28486. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime), 1);
  28487. ExpectIntEQ(daysDiff, -2856);
  28488. ExpectIntEQ(secsDiff, -75296);
  28489. /* Compare with invalid time string. */
  28490. ExpectIntEQ(ASN1_TIME_compare(fromTime, invalidTime), -2);
  28491. ExpectIntEQ(ASN1_TIME_compare(invalidTime, toTime), -2);
  28492. /* Compare with days difference of 0. */
  28493. ExpectIntEQ(ASN1_TIME_compare(fromTime, closeToTime), -1);
  28494. ExpectIntEQ(ASN1_TIME_compare(closeToTime, fromTime), 1);
  28495. /* Days and seconds differences not 0. */
  28496. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  28497. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  28498. /* Same time. */
  28499. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  28500. /* Compare regression test: No seconds difference, just difference in days.
  28501. */
  28502. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  28503. ASN1_TIME_set_string(toTime, "19800101000000Z");
  28504. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  28505. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  28506. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  28507. /* Edge case with Unix epoch. */
  28508. ExpectNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  28509. ExpectNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  28510. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28511. ExpectIntEQ(daysDiff, 3652);
  28512. ExpectIntEQ(secsDiff, 0);
  28513. /* Edge case with year > 2038 (year 2038 problem). */
  28514. ExpectNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  28515. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28516. ExpectIntEQ(daysDiff, 2932896);
  28517. ExpectIntEQ(secsDiff, 86399);
  28518. ASN1_TIME_free(fromTime);
  28519. ASN1_TIME_free(closeToTime);
  28520. ASN1_TIME_free(toTime);
  28521. ASN1_TIME_free(invalidTime);
  28522. #endif
  28523. return EXPECT_RESULT();
  28524. }
  28525. static int test_wolfSSL_ASN1_TIME_adj(void)
  28526. {
  28527. EXPECT_DECLS;
  28528. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  28529. !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  28530. const int year = 365*24*60*60;
  28531. const int day = 24*60*60;
  28532. const int hour = 60*60;
  28533. const int mini = 60;
  28534. const byte asn_utc_time = ASN_UTC_TIME;
  28535. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  28536. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  28537. #endif
  28538. WOLFSSL_ASN1_TIME* asn_time = NULL;
  28539. WOLFSSL_ASN1_TIME* s = NULL;
  28540. int offset_day;
  28541. long offset_sec;
  28542. char date_str[CTC_DATE_SIZE + 1];
  28543. time_t t;
  28544. ExpectNotNull(s = wolfSSL_ASN1_TIME_new());
  28545. /* UTC notation test */
  28546. /* 2000/2/15 20:30:00 */
  28547. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  28548. offset_day = 7;
  28549. offset_sec = 45 * mini;
  28550. /* offset_sec = -45 * min;*/
  28551. ExpectNotNull(asn_time =
  28552. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  28553. ExpectTrue(asn_time->type == asn_utc_time);
  28554. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28555. CTC_DATE_SIZE));
  28556. date_str[CTC_DATE_SIZE] = '\0';
  28557. ExpectIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  28558. /* negative offset */
  28559. offset_sec = -45 * mini;
  28560. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  28561. ExpectNotNull(asn_time);
  28562. ExpectTrue(asn_time->type == asn_utc_time);
  28563. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28564. CTC_DATE_SIZE));
  28565. date_str[CTC_DATE_SIZE] = '\0';
  28566. ExpectIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  28567. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  28568. s = NULL;
  28569. XMEMSET(date_str, 0, sizeof(date_str));
  28570. /* Generalized time will overflow time_t if not long */
  28571. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  28572. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  28573. DYNAMIC_TYPE_OPENSSL);
  28574. /* GeneralizedTime notation test */
  28575. /* 2055/03/01 09:00:00 */
  28576. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  28577. offset_day = 12;
  28578. offset_sec = 10 * mini;
  28579. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  28580. offset_sec));
  28581. ExpectTrue(asn_time->type == asn_gen_time);
  28582. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28583. CTC_DATE_SIZE));
  28584. date_str[CTC_DATE_SIZE] = '\0';
  28585. ExpectIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  28586. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  28587. s = NULL;
  28588. XMEMSET(date_str, 0, sizeof(date_str));
  28589. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  28590. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  28591. s = NULL;
  28592. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  28593. offset_day = 7;
  28594. offset_sec = 45 * mini;
  28595. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  28596. offset_sec));
  28597. ExpectTrue(asn_time->type == asn_utc_time);
  28598. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28599. CTC_DATE_SIZE));
  28600. date_str[CTC_DATE_SIZE] = '\0';
  28601. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  28602. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  28603. asn_time = NULL;
  28604. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day,
  28605. offset_sec));
  28606. ExpectTrue(asn_time->type == asn_utc_time);
  28607. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28608. CTC_DATE_SIZE));
  28609. date_str[CTC_DATE_SIZE] = '\0';
  28610. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  28611. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  28612. #endif
  28613. return EXPECT_RESULT();
  28614. }
  28615. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  28616. {
  28617. EXPECT_DECLS;
  28618. #if (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28619. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  28620. defined(OPENSSL_ALL)) && !defined(NO_ASN_TIME)
  28621. ASN1_TIME asnTime;
  28622. struct tm tm;
  28623. time_t testTime = 1683926567; /* Fri May 12 09:22:47 PM UTC 2023 */
  28624. XMEMSET(&tm, 0, sizeof(struct tm));
  28625. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  28626. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  28627. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, NULL), 1);
  28628. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28629. ExpectIntEQ(tm.tm_sec, 15);
  28630. ExpectIntEQ(tm.tm_min, 15);
  28631. ExpectIntEQ(tm.tm_hour, 21);
  28632. ExpectIntEQ(tm.tm_mday, 22);
  28633. ExpectIntEQ(tm.tm_mon, 1);
  28634. ExpectIntEQ(tm.tm_year, 100);
  28635. ExpectIntEQ(tm.tm_isdst, 0);
  28636. #ifdef XMKTIME
  28637. ExpectIntEQ(tm.tm_wday, 2);
  28638. ExpectIntEQ(tm.tm_yday, 52);
  28639. #endif
  28640. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "500222211515Z"), 1);
  28641. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28642. ExpectIntEQ(tm.tm_year, 50);
  28643. /* Get current time. */
  28644. ExpectIntEQ(ASN1_TIME_to_tm(NULL, NULL), 0);
  28645. ExpectIntEQ(ASN1_TIME_to_tm(NULL, &tm), 1);
  28646. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  28647. /* 0 length. */
  28648. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28649. /* No type. */
  28650. asnTime.length = 1;
  28651. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28652. /* Not UTCTIME length. */
  28653. asnTime.type = V_ASN1_UTCTIME;
  28654. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28655. /* Not GENERALIZEDTIME length. */
  28656. asnTime.type = V_ASN1_GENERALIZEDTIME;
  28657. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28658. /* Not Zulu timezone. */
  28659. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515U"), 1);
  28660. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28661. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "20000222211515U"), 1);
  28662. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28663. #ifdef XMKTIME
  28664. ExpectNotNull(ASN1_TIME_adj(&asnTime, testTime, 0, 0));
  28665. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28666. ExpectIntEQ(tm.tm_sec, 47);
  28667. ExpectIntEQ(tm.tm_min, 22);
  28668. ExpectIntEQ(tm.tm_hour, 21);
  28669. ExpectIntEQ(tm.tm_mday, 12);
  28670. ExpectIntEQ(tm.tm_mon, 4);
  28671. ExpectIntEQ(tm.tm_year, 123);
  28672. ExpectIntEQ(tm.tm_wday, 5);
  28673. ExpectIntEQ(tm.tm_yday, 131);
  28674. /* Confirm that when used with a tm struct from ASN1_TIME_adj, all other
  28675. fields are zeroed out as expected. */
  28676. ExpectIntEQ(tm.tm_isdst, 0);
  28677. #endif
  28678. #endif
  28679. return EXPECT_RESULT();
  28680. }
  28681. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void)
  28682. {
  28683. EXPECT_DECLS;
  28684. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28685. WOLFSSL_ASN1_TIME *t = NULL;
  28686. WOLFSSL_ASN1_TIME *out = NULL;
  28687. WOLFSSL_ASN1_TIME *gtime = NULL;
  28688. int tlen = 0;
  28689. unsigned char *data = NULL;
  28690. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  28691. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(NULL, &out));
  28692. /* type not set. */
  28693. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28694. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28695. t = NULL;
  28696. /* UTC Time test */
  28697. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  28698. if (t != NULL) {
  28699. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  28700. t->type = ASN_UTC_TIME;
  28701. t->length = ASN_UTC_TIME_SIZE;
  28702. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  28703. }
  28704. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t), ASN_UTC_TIME_SIZE);
  28705. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)), "050727123456Z");
  28706. out = NULL;
  28707. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28708. wolfSSL_ASN1_TIME_free(gtime);
  28709. gtime = NULL;
  28710. ExpectNotNull(out = wolfSSL_ASN1_TIME_new());
  28711. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28712. ExpectPtrEq(gtime, out);
  28713. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28714. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28715. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  28716. /* Generalized Time test */
  28717. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28718. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  28719. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  28720. if (t != NULL) {
  28721. t->type = ASN_GENERALIZED_TIME;
  28722. t->length = ASN_GENERALIZED_TIME_SIZE;
  28723. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  28724. }
  28725. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t),
  28726. ASN_GENERALIZED_TIME_SIZE);
  28727. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)),
  28728. "20050727123456Z");
  28729. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28730. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28731. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28732. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  28733. /* UTC Time to Generalized Time 1900's test */
  28734. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28735. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  28736. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  28737. if (t != NULL) {
  28738. t->type = ASN_UTC_TIME;
  28739. t->length = ASN_UTC_TIME_SIZE;
  28740. XMEMCPY(t->data, "500727123456Z", ASN_UTC_TIME_SIZE);
  28741. }
  28742. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28743. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28744. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28745. ExpectStrEQ((char*)gtime->data, "19500727123456Z");
  28746. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28747. /* Null parameter test */
  28748. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28749. gtime = NULL;
  28750. out = NULL;
  28751. if (t != NULL) {
  28752. t->type = ASN_UTC_TIME;
  28753. t->length = ASN_UTC_TIME_SIZE;
  28754. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  28755. }
  28756. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  28757. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28758. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28759. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  28760. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28761. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28762. #endif
  28763. return EXPECT_RESULT();
  28764. }
  28765. static int test_wolfSSL_ASN1_TIME_print(void)
  28766. {
  28767. EXPECT_DECLS;
  28768. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_BIO) && \
  28769. (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28770. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  28771. defined(OPENSSL_ALL)) && defined(USE_CERT_BUFFERS_2048) && \
  28772. !defined(NO_ASN_TIME)
  28773. BIO* bio = NULL;
  28774. BIO* fixed = NULL;
  28775. X509* x509 = NULL;
  28776. const unsigned char* der = client_cert_der_2048;
  28777. ASN1_TIME* notAfter = NULL;
  28778. ASN1_TIME* notBefore = NULL;
  28779. unsigned char buf[25];
  28780. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28781. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28782. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  28783. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  28784. ExpectNotNull(notBefore = X509_get_notBefore(x509));
  28785. ExpectIntEQ(ASN1_TIME_print(NULL, NULL), 0);
  28786. ExpectIntEQ(ASN1_TIME_print(bio, NULL), 0);
  28787. ExpectIntEQ(ASN1_TIME_print(NULL, notBefore), 0);
  28788. ExpectIntEQ(ASN1_TIME_print(bio, notBefore), 1);
  28789. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  28790. ExpectIntEQ(XMEMCMP(buf, "Dec 13 22:19:28 2023 GMT", sizeof(buf) - 1), 0);
  28791. /* Test BIO_write fails. */
  28792. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28793. /* Ensure there is 0 bytes available to write into. */
  28794. ExpectIntEQ(BIO_write(fixed, buf, 1), 1);
  28795. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  28796. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28797. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  28798. ExpectIntEQ(BIO_set_write_buf_size(fixed, 23), 1);
  28799. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  28800. /* create a bad time and test results */
  28801. ExpectNotNull(notAfter = X509_get_notAfter(x509));
  28802. ExpectIntEQ(ASN1_TIME_check(notAfter), 1);
  28803. if (EXPECT_SUCCESS()) {
  28804. notAfter->data[8] = 0;
  28805. notAfter->data[3] = 0;
  28806. }
  28807. ExpectIntNE(ASN1_TIME_print(bio, notAfter), 1);
  28808. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  28809. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  28810. ExpectIntEQ(ASN1_TIME_check(notAfter), 0);
  28811. BIO_free(bio);
  28812. BIO_free(fixed);
  28813. X509_free(x509);
  28814. #endif
  28815. return EXPECT_RESULT();
  28816. }
  28817. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  28818. {
  28819. EXPECT_DECLS;
  28820. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  28821. BIO* bio = NULL;
  28822. ASN1_UTCTIME* utc = NULL;
  28823. unsigned char buf[25];
  28824. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  28825. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  28826. const char* genDate = "20190424111501Z"; /* GEN = YYYYMMDDHHMMSSZ */
  28827. /* Valid date */
  28828. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28829. ExpectNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  28830. DYNAMIC_TYPE_ASN1));
  28831. if (utc != NULL) {
  28832. utc->type = ASN_UTC_TIME;
  28833. utc->length = ASN_UTC_TIME_SIZE;
  28834. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  28835. }
  28836. ExpectIntEQ(ASN1_UTCTIME_print(NULL, NULL), 0);
  28837. ExpectIntEQ(ASN1_UTCTIME_print(bio, NULL), 0);
  28838. ExpectIntEQ(ASN1_UTCTIME_print(NULL, utc), 0);
  28839. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  28840. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  28841. ExpectIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  28842. XMEMSET(buf, 0, sizeof(buf));
  28843. BIO_free(bio);
  28844. bio = NULL;
  28845. /* Invalid format */
  28846. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28847. if (utc != NULL) {
  28848. utc->type = ASN_UTC_TIME;
  28849. utc->length = ASN_UTC_TIME_SIZE;
  28850. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  28851. }
  28852. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  28853. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  28854. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  28855. /* Invalid type */
  28856. if (utc != NULL) {
  28857. utc->type = ASN_GENERALIZED_TIME;
  28858. utc->length = ASN_GENERALIZED_TIME_SIZE;
  28859. XMEMCPY(utc->data, (byte*)genDate, ASN_GENERALIZED_TIME_SIZE);
  28860. }
  28861. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  28862. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  28863. BIO_free(bio);
  28864. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  28865. return EXPECT_RESULT();
  28866. }
  28867. static int test_wolfSSL_ASN1_TYPE(void)
  28868. {
  28869. EXPECT_DECLS;
  28870. #if defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD) || \
  28871. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  28872. WOLFSSL_ASN1_TYPE* t = NULL;
  28873. WOLFSSL_ASN1_OBJECT* obj = NULL;
  28874. #ifndef NO_ASN_TIME
  28875. WOLFSSL_ASN1_TIME* time = NULL;
  28876. #endif
  28877. WOLFSSL_ASN1_STRING* str = NULL;
  28878. unsigned char data[] = { 0x00 };
  28879. ASN1_TYPE_set(NULL, V_ASN1_NULL, NULL);
  28880. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28881. ASN1_TYPE_set(t, V_ASN1_EOC, NULL);
  28882. wolfSSL_ASN1_TYPE_free(t);
  28883. t = NULL;
  28884. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28885. ASN1_TYPE_set(t, V_ASN1_NULL, NULL);
  28886. ASN1_TYPE_set(t, V_ASN1_NULL, data);
  28887. wolfSSL_ASN1_TYPE_free(t);
  28888. t = NULL;
  28889. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28890. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  28891. ASN1_TYPE_set(t, V_ASN1_OBJECT, obj);
  28892. wolfSSL_ASN1_TYPE_free(t);
  28893. t = NULL;
  28894. #ifndef NO_ASN_TIME
  28895. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28896. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  28897. ASN1_TYPE_set(t, V_ASN1_UTCTIME, time);
  28898. wolfSSL_ASN1_TYPE_free(t);
  28899. t = NULL;
  28900. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28901. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  28902. ASN1_TYPE_set(t, V_ASN1_GENERALIZEDTIME, time);
  28903. wolfSSL_ASN1_TYPE_free(t);
  28904. t = NULL;
  28905. #endif
  28906. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28907. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28908. ASN1_TYPE_set(t, V_ASN1_UTF8STRING, str);
  28909. wolfSSL_ASN1_TYPE_free(t);
  28910. t = NULL;
  28911. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28912. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28913. ASN1_TYPE_set(t, V_ASN1_PRINTABLESTRING, str);
  28914. wolfSSL_ASN1_TYPE_free(t);
  28915. t = NULL;
  28916. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28917. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28918. ASN1_TYPE_set(t, V_ASN1_T61STRING, str);
  28919. wolfSSL_ASN1_TYPE_free(t);
  28920. t = NULL;
  28921. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28922. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28923. ASN1_TYPE_set(t, V_ASN1_IA5STRING, str);
  28924. wolfSSL_ASN1_TYPE_free(t);
  28925. t = NULL;
  28926. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28927. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28928. ASN1_TYPE_set(t, V_ASN1_UNIVERSALSTRING, str);
  28929. wolfSSL_ASN1_TYPE_free(t);
  28930. t = NULL;
  28931. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28932. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  28933. ASN1_TYPE_set(t, V_ASN1_SEQUENCE, str);
  28934. wolfSSL_ASN1_TYPE_free(t);
  28935. t = NULL;
  28936. #endif
  28937. return EXPECT_RESULT();
  28938. }
  28939. /* Testing code used in dpp.c in hostap */
  28940. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  28941. typedef struct {
  28942. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  28943. * as an OID identifying the curve */
  28944. X509_ALGOR *alg;
  28945. /* Compressed format public key per ANSI X9.63 */
  28946. ASN1_BIT_STRING *pub_key;
  28947. } DPP_BOOTSTRAPPING_KEY;
  28948. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  28949. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  28950. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  28951. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  28952. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  28953. typedef struct {
  28954. ASN1_INTEGER *integer;
  28955. } TEST_ASN1;
  28956. ASN1_SEQUENCE(TEST_ASN1) = {
  28957. ASN1_SIMPLE(TEST_ASN1, integer, ASN1_INTEGER),
  28958. } ASN1_SEQUENCE_END(TEST_ASN1)
  28959. IMPLEMENT_ASN1_FUNCTIONS(TEST_ASN1)
  28960. typedef struct {
  28961. ASN1_OCTET_STRING *octet_string;
  28962. } TEST_FAIL_ASN1;
  28963. #define WOLFSSL_ASN1_OCTET_STRING_ASN1 4
  28964. ASN1_SEQUENCE(TEST_FAIL_ASN1) = {
  28965. ASN1_SIMPLE(TEST_FAIL_ASN1, octet_string, ASN1_OCTET_STRING),
  28966. } ASN1_SEQUENCE_END(TEST_FAIL_ASN1)
  28967. IMPLEMENT_ASN1_FUNCTIONS(TEST_FAIL_ASN1)
  28968. #endif
  28969. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  28970. {
  28971. EXPECT_DECLS;
  28972. /* Testing code used in dpp.c in hostap */
  28973. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  28974. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  28975. EC_KEY *eckey = NULL;
  28976. EVP_PKEY *key = NULL;
  28977. size_t len = 0;
  28978. unsigned char *der = NULL;
  28979. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  28980. const unsigned char *in = ecc_clikey_der_256;
  28981. WOLFSSL_ASN1_OBJECT* ec_obj = NULL;
  28982. WOLFSSL_ASN1_OBJECT* group_obj = NULL;
  28983. const EC_GROUP *group = NULL;
  28984. const EC_POINT *point = NULL;
  28985. int nid;
  28986. TEST_ASN1 *test_asn1 = NULL;
  28987. TEST_FAIL_ASN1 test_fail_asn1;
  28988. const unsigned char badObjDer[] = { 0x06, 0x00 };
  28989. const unsigned char goodObjDer[] = {
  28990. 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01
  28991. };
  28992. WOLFSSL_ASN1_ITEM emptyTemplate;
  28993. XMEMSET(&emptyTemplate, 0, sizeof(WOLFSSL_ASN1_ITEM));
  28994. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  28995. der = NULL;
  28996. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(NULL, &der), 0);
  28997. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, NULL), 0);
  28998. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  28999. ExpectNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  29000. (long)sizeof_ecc_clikey_der_256));
  29001. ExpectNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  29002. ExpectNotNull(group = EC_KEY_get0_group(eckey));
  29003. ExpectNotNull(point = EC_KEY_get0_public_key(eckey));
  29004. nid = EC_GROUP_get_curve_name(group);
  29005. ec_obj = OBJ_nid2obj(EVP_PKEY_EC);
  29006. group_obj = OBJ_nid2obj(nid);
  29007. if ((ec_obj != NULL) && (group_obj != NULL)) {
  29008. ExpectIntEQ(X509_ALGOR_set0(bootstrap->alg, ec_obj, V_ASN1_OBJECT,
  29009. group_obj), 1);
  29010. if (EXPECT_SUCCESS()) {
  29011. ec_obj = NULL;
  29012. group_obj = NULL;
  29013. }
  29014. }
  29015. wolfSSL_ASN1_OBJECT_free(group_obj);
  29016. wolfSSL_ASN1_OBJECT_free(ec_obj);
  29017. ExpectIntEQ(EC_POINT_point2oct(group, point, 0, NULL, 0, NULL), 0);
  29018. #ifdef HAVE_COMP_KEY
  29019. ExpectIntGT((len = EC_POINT_point2oct(
  29020. group, point, POINT_CONVERSION_COMPRESSED,
  29021. NULL, 0, NULL)), 0);
  29022. #else
  29023. ExpectIntGT((len = EC_POINT_point2oct(
  29024. group, point, POINT_CONVERSION_UNCOMPRESSED,
  29025. NULL, 0, NULL)), 0);
  29026. #endif
  29027. ExpectNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  29028. #ifdef HAVE_COMP_KEY
  29029. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  29030. der, len-1, NULL), 0);
  29031. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  29032. der, len, NULL), len);
  29033. #else
  29034. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  29035. der, len-1, NULL), 0);
  29036. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  29037. der, len, NULL), len);
  29038. #endif
  29039. if (EXPECT_SUCCESS()) {
  29040. bootstrap->pub_key->data = der;
  29041. bootstrap->pub_key->length = (int)len;
  29042. /* Not actually used */
  29043. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  29044. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  29045. }
  29046. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, NULL), 0);
  29047. der = NULL;
  29048. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29049. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29050. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  29051. EVP_PKEY_free(key);
  29052. EC_KEY_free(eckey);
  29053. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  29054. bootstrap = NULL;
  29055. DPP_BOOTSTRAPPING_KEY_free(NULL);
  29056. /* Create bootstrap key with bad OBJECT_ID DER data, parameter that is
  29057. * a NULL and an empty BIT_STRING. */
  29058. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  29059. ExpectNotNull(bootstrap->alg->algorithm = wolfSSL_ASN1_OBJECT_new());
  29060. if (EXPECT_SUCCESS()) {
  29061. bootstrap->alg->algorithm->obj = badObjDer;
  29062. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(badObjDer);
  29063. }
  29064. ExpectNotNull(bootstrap->alg->parameter = wolfSSL_ASN1_TYPE_new());
  29065. if (EXPECT_SUCCESS()) {
  29066. bootstrap->alg->parameter->type = V_ASN1_NULL;
  29067. bootstrap->alg->parameter->value.ptr = NULL;
  29068. bootstrap->pub_key->data = NULL;
  29069. bootstrap->pub_key->length = 0;
  29070. /* Not actually used */
  29071. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  29072. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  29073. }
  29074. /* Encode with bad OBJECT_ID. */
  29075. der = NULL;
  29076. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29077. /* Fix OBJECT_ID and encode with empty BIT_STRING. */
  29078. if (EXPECT_SUCCESS()) {
  29079. bootstrap->alg->algorithm->obj = goodObjDer;
  29080. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(goodObjDer);
  29081. bootstrap->alg->algorithm->grp = 2;
  29082. }
  29083. der = NULL;
  29084. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 16);
  29085. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, &emptyTemplate), 0);
  29086. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  29087. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  29088. /* Test integer */
  29089. ExpectNotNull(test_asn1 = TEST_ASN1_new());
  29090. der = NULL;
  29091. ExpectIntEQ(ASN1_INTEGER_set(test_asn1->integer, 100), 1);
  29092. ExpectIntEQ(i2d_TEST_ASN1(test_asn1, &der), 5);
  29093. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  29094. TEST_ASN1_free(test_asn1);
  29095. /* Test integer cases. */
  29096. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  29097. TEST_ASN1_free(NULL);
  29098. /* Test error cases. */
  29099. ExpectNull(TEST_FAIL_ASN1_new());
  29100. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  29101. TEST_FAIL_ASN1_free(NULL);
  29102. XMEMSET(&test_fail_asn1, 0, sizeof(TEST_FAIL_ASN1));
  29103. ExpectIntEQ(i2d_TEST_FAIL_ASN1(&test_fail_asn1, &der), 0);
  29104. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  29105. #endif /* OPENSSL_ALL && HAVE_ECC && USE_CERT_BUFFERS_256 */
  29106. return EXPECT_RESULT();
  29107. }
  29108. static int test_wolfSSL_lhash(void)
  29109. {
  29110. EXPECT_DECLS;
  29111. #ifdef OPENSSL_ALL
  29112. const char testStr[] = "Like a true nature's child\n"
  29113. "We were born\n"
  29114. "Born to be wild";
  29115. #ifdef NO_SHA
  29116. ExpectIntEQ(lh_strhash(testStr), 0xf9dc8a43);
  29117. #else
  29118. ExpectIntEQ(lh_strhash(testStr), 0x5b7541dc);
  29119. #endif
  29120. #endif
  29121. return EXPECT_RESULT();
  29122. }
  29123. static int test_wolfSSL_X509_NAME(void)
  29124. {
  29125. EXPECT_DECLS;
  29126. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  29127. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29128. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  29129. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  29130. defined(OPENSSL_EXTRA))
  29131. X509* x509 = NULL;
  29132. const unsigned char* c = NULL;
  29133. unsigned char buf[4096];
  29134. int bytes = 0;
  29135. XFILE f = XBADFILE;
  29136. const X509_NAME* a = NULL;
  29137. const X509_NAME* b = NULL;
  29138. X509_NAME* d2i_name = NULL;
  29139. int sz = 0;
  29140. unsigned char* tmp = NULL;
  29141. char file[] = "./certs/ca-cert.der";
  29142. #ifndef OPENSSL_EXTRA_X509_SMALL
  29143. byte empty[] = { /* CN=empty emailAddress= */
  29144. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  29145. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  29146. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  29147. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  29148. 0x01, 0x16, 0x00
  29149. };
  29150. #endif
  29151. #ifndef OPENSSL_EXTRA_X509_SMALL
  29152. /* test compile of deprecated function, returns 0 */
  29153. ExpectIntEQ(CRYPTO_thread_id(), 0);
  29154. #endif
  29155. ExpectNotNull(a = X509_NAME_new());
  29156. X509_NAME_free((X509_NAME*)a);
  29157. a = NULL;
  29158. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  29159. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  29160. if (f != XBADFILE)
  29161. XFCLOSE(f);
  29162. c = buf;
  29163. ExpectNotNull(x509 = wolfSSL_X509_d2i_ex(NULL, c, bytes, HEAP_HINT));
  29164. /* test cmp function */
  29165. ExpectNotNull(a = X509_get_issuer_name(x509));
  29166. ExpectNotNull(b = X509_get_subject_name(x509));
  29167. #ifndef OPENSSL_EXTRA_X509_SMALL
  29168. ExpectIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  29169. #endif
  29170. tmp = buf;
  29171. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  29172. if (sz > 0 && tmp == buf) {
  29173. fprintf(stderr, "\nERROR - %s line %d failed with:", __FILE__,
  29174. __LINE__);
  29175. fprintf(stderr, " Expected pointer to be incremented\n");
  29176. abort();
  29177. }
  29178. #ifndef OPENSSL_EXTRA_X509_SMALL
  29179. tmp = buf;
  29180. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  29181. #endif
  29182. /* if output parameter is NULL, should still return required size. */
  29183. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  29184. /* retry but with the function creating a buffer */
  29185. tmp = NULL;
  29186. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  29187. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  29188. tmp = NULL;
  29189. #ifdef WOLFSSL_CERT_NAME_ALL
  29190. /* test for givenName and name */
  29191. {
  29192. WOLFSSL_X509_NAME_ENTRY* entry = NULL;
  29193. const byte gName[] = "test-given-name";
  29194. const byte name[] = "test-name";
  29195. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  29196. NID_givenName, ASN_UTF8STRING, gName, sizeof(gName)));
  29197. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  29198. 1);
  29199. wolfSSL_X509_NAME_ENTRY_free(entry);
  29200. entry = NULL;
  29201. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  29202. NID_name, ASN_UTF8STRING, name, sizeof(name)));
  29203. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  29204. 1);
  29205. wolfSSL_X509_NAME_ENTRY_free(entry);
  29206. tmp = NULL;
  29207. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  29208. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  29209. }
  29210. #endif
  29211. b = NULL;
  29212. ExpectNotNull(b = X509_NAME_dup((X509_NAME*)a));
  29213. #ifndef OPENSSL_EXTRA_X509_SMALL
  29214. ExpectIntEQ(X509_NAME_cmp(a, b), 0);
  29215. #endif
  29216. X509_NAME_free((X509_NAME*)b);
  29217. X509_NAME_free(d2i_name);
  29218. d2i_name = NULL;
  29219. X509_free(x509);
  29220. #ifndef OPENSSL_EXTRA_X509_SMALL
  29221. /* test with an empty domain component */
  29222. tmp = empty;
  29223. sz = sizeof(empty);
  29224. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  29225. ExpectIntEQ(X509_NAME_entry_count(d2i_name), 2);
  29226. /* size of empty emailAddress will be 0 */
  29227. tmp = buf;
  29228. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  29229. (char*)tmp, sizeof(buf)), 0);
  29230. /* should contain no organization name */
  29231. tmp = buf;
  29232. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  29233. (char*)tmp, sizeof(buf)), -1);
  29234. X509_NAME_free(d2i_name);
  29235. #endif
  29236. #endif
  29237. return EXPECT_RESULT();
  29238. }
  29239. static int test_wolfSSL_X509_NAME_hash(void)
  29240. {
  29241. EXPECT_DECLS;
  29242. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  29243. !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  29244. BIO* bio = NULL;
  29245. X509* x509 = NULL;
  29246. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  29247. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  29248. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  29249. ExpectIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  29250. ExpectIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  29251. X509_free(x509);
  29252. BIO_free(bio);
  29253. #endif
  29254. return EXPECT_RESULT();
  29255. }
  29256. static int test_wolfSSL_X509_NAME_print_ex(void)
  29257. {
  29258. EXPECT_DECLS;
  29259. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  29260. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  29261. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  29262. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  29263. !defined(NO_BIO) && !defined(NO_RSA)
  29264. int memSz = 0;
  29265. byte* mem = NULL;
  29266. BIO* bio = NULL;
  29267. BIO* membio = NULL;
  29268. X509* x509 = NULL;
  29269. X509_NAME* name = NULL;
  29270. const char* expNormal = "C=US, CN=wolfssl.com";
  29271. const char* expReverse = "CN=wolfssl.com, C=US";
  29272. const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
  29273. const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
  29274. const char* expRFC5523 =
  29275. "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
  29276. /* Test with real cert (svrCertFile) first */
  29277. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  29278. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  29279. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  29280. ExpectNotNull(name = X509_get_subject_name(x509));
  29281. /* Test without flags */
  29282. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29283. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29284. BIO_free(membio);
  29285. membio = NULL;
  29286. /* Test flag: XN_FLAG_RFC2253 */
  29287. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29288. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29289. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29290. BIO_free(membio);
  29291. membio = NULL;
  29292. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  29293. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29294. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29295. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29296. BIO_free(membio);
  29297. membio = NULL;
  29298. X509_free(x509);
  29299. BIO_free(bio);
  29300. name = NULL;
  29301. /* Test normal case without escaped characters */
  29302. {
  29303. /* Create name: "/C=US/CN=wolfssl.com" */
  29304. ExpectNotNull(name = X509_NAME_new());
  29305. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  29306. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  29307. WOLFSSL_SUCCESS);
  29308. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  29309. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  29310. WOLFSSL_SUCCESS);
  29311. /* Test without flags */
  29312. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29313. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29314. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29315. ExpectIntEQ(memSz, XSTRLEN(expNormal));
  29316. ExpectIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  29317. BIO_free(membio);
  29318. membio = NULL;
  29319. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  29320. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29321. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29322. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29323. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29324. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  29325. BIO_free(membio);
  29326. membio = NULL;
  29327. /* Test flags: XN_FLAG_DN_REV - reversed */
  29328. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29329. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29330. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29331. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29332. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  29333. ExpectIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  29334. BIO_free(membio);
  29335. membio = NULL;
  29336. X509_NAME_free(name);
  29337. name = NULL;
  29338. }
  29339. /* Test RFC2253 characters are escaped with backslashes */
  29340. {
  29341. ExpectNotNull(name = X509_NAME_new());
  29342. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  29343. /* space at beginning and end, and: ,+"\ */
  29344. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  29345. WOLFSSL_SUCCESS);
  29346. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  29347. /* # at beginning, and: <>;*/
  29348. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  29349. WOLFSSL_SUCCESS);
  29350. /* Test without flags */
  29351. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29352. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29353. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29354. ExpectIntEQ(memSz, XSTRLEN(expNotEscaped));
  29355. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  29356. XSTRLEN(expNotEscaped)), 0);
  29357. BIO_free(membio);
  29358. membio = NULL;
  29359. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  29360. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29361. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29362. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29363. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29364. ExpectIntEQ(memSz, XSTRLEN(expRFC5523));
  29365. ExpectIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  29366. BIO_free(membio);
  29367. membio = NULL;
  29368. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  29369. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29370. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29371. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29372. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29373. ExpectIntEQ(memSz, XSTRLEN(expNotEscapedRev));
  29374. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  29375. XSTRLEN(expNotEscapedRev)), 0);
  29376. BIO_free(membio);
  29377. X509_NAME_free(name);
  29378. }
  29379. #endif
  29380. return EXPECT_RESULT();
  29381. }
  29382. #ifndef NO_BIO
  29383. static int test_wolfSSL_X509_INFO_multiple_info(void)
  29384. {
  29385. EXPECT_DECLS;
  29386. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  29387. STACK_OF(X509_INFO) *info_stack = NULL;
  29388. X509_INFO *info = NULL;
  29389. int len;
  29390. int i;
  29391. const char* files[] = {
  29392. cliCertFile,
  29393. cliKeyFile,
  29394. /* This needs to be the order as svrCertFile contains the
  29395. * intermediate cert as well. */
  29396. svrKeyFile,
  29397. svrCertFile,
  29398. NULL,
  29399. };
  29400. const char** curFile;
  29401. BIO *fileBIO = NULL;
  29402. BIO *concatBIO = NULL;
  29403. byte tmp[FOURK_BUF];
  29404. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  29405. * to group objects together. */
  29406. ExpectNotNull(concatBIO = BIO_new(BIO_s_mem()));
  29407. for (curFile = files; EXPECT_SUCCESS() && *curFile != NULL; curFile++) {
  29408. int fileLen = 0;
  29409. ExpectNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  29410. ExpectIntGT(fileLen = wolfSSL_BIO_get_len(fileBIO), 0);
  29411. if (EXPECT_SUCCESS()) {
  29412. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  29413. ExpectIntEQ(BIO_write(concatBIO, tmp, len), len);
  29414. fileLen -= len;
  29415. if (EXPECT_FAIL())
  29416. break;
  29417. }
  29418. /* Make sure we read the entire file */
  29419. ExpectIntEQ(fileLen, 0);
  29420. }
  29421. BIO_free(fileBIO);
  29422. fileBIO = NULL;
  29423. }
  29424. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  29425. NULL));
  29426. ExpectIntEQ(sk_X509_INFO_num(info_stack), 3);
  29427. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  29428. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  29429. ExpectNotNull(info->x509);
  29430. ExpectNull(info->crl);
  29431. if (i != 0) {
  29432. ExpectNotNull(info->x_pkey);
  29433. ExpectIntEQ(X509_check_private_key(info->x509,
  29434. info->x_pkey->dec_pkey), 1);
  29435. }
  29436. else {
  29437. ExpectNull(info->x_pkey);
  29438. }
  29439. }
  29440. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29441. BIO_free(concatBIO);
  29442. #endif
  29443. return EXPECT_RESULT();
  29444. }
  29445. #endif
  29446. #ifndef NO_BIO
  29447. static int test_wolfSSL_X509_INFO(void)
  29448. {
  29449. EXPECT_DECLS;
  29450. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  29451. STACK_OF(X509_INFO) *info_stack = NULL;
  29452. X509_INFO *info = NULL;
  29453. BIO *cert = NULL;
  29454. int i;
  29455. /* PEM in hex format to avoid null terminator */
  29456. byte data[] = {
  29457. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  29458. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  29459. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  29460. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  29461. 0x2d, 0x2d, 0x2d
  29462. };
  29463. /* PEM in hex format to avoid null terminator */
  29464. byte data2[] = {
  29465. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  29466. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  29467. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  29468. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  29469. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  29470. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  29471. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  29472. };
  29473. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  29474. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29475. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  29476. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  29477. ExpectNotNull(info->x509);
  29478. ExpectNull(info->crl);
  29479. ExpectNull(info->x_pkey);
  29480. }
  29481. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29482. info_stack = NULL;
  29483. BIO_free(cert);
  29484. cert = NULL;
  29485. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  29486. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29487. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29488. info_stack = NULL;
  29489. BIO_free(cert);
  29490. cert = NULL;
  29491. /* This case should fail due to invalid input. */
  29492. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  29493. ExpectIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  29494. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29495. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29496. info_stack = NULL;
  29497. BIO_free(cert);
  29498. cert = NULL;
  29499. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  29500. ExpectIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  29501. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29502. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29503. BIO_free(cert);
  29504. #endif
  29505. return EXPECT_RESULT();
  29506. }
  29507. #endif
  29508. static int test_wolfSSL_X509_subject_name_hash(void)
  29509. {
  29510. EXPECT_DECLS;
  29511. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29512. !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  29513. X509* x509 = NULL;
  29514. X509_NAME* subjectName = NULL;
  29515. unsigned long ret1 = 0;
  29516. unsigned long ret2 = 0;
  29517. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29518. SSL_FILETYPE_PEM));
  29519. ExpectNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  29520. /* These two
  29521. * - X509_subject_name_hash(x509)
  29522. * - X509_NAME_hash(X509_get_subject_name(x509))
  29523. * should give the same hash, if !defined(NO_SHA) is true. */
  29524. ret1 = X509_subject_name_hash(x509);
  29525. ExpectIntNE(ret1, 0);
  29526. #if !defined(NO_SHA)
  29527. ret2 = X509_NAME_hash(X509_get_subject_name(x509));
  29528. ExpectIntNE(ret2, 0);
  29529. ExpectIntEQ(ret1, ret2);
  29530. #else
  29531. (void) ret2;
  29532. #endif
  29533. X509_free(x509);
  29534. #endif
  29535. return EXPECT_RESULT();
  29536. }
  29537. static int test_wolfSSL_X509_issuer_name_hash(void)
  29538. {
  29539. EXPECT_DECLS;
  29540. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  29541. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  29542. X509* x509 = NULL;
  29543. X509_NAME* issuertName = NULL;
  29544. unsigned long ret1 = 0;
  29545. unsigned long ret2 = 0;
  29546. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29547. SSL_FILETYPE_PEM));
  29548. ExpectNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  29549. /* These two
  29550. * - X509_issuer_name_hash(x509)
  29551. * - X509_NAME_hash(X509_get_issuer_name(x509))
  29552. * should give the same hash, if !defined(NO_SHA) is true. */
  29553. ret1 = X509_issuer_name_hash(x509);
  29554. ExpectIntNE(ret1, 0);
  29555. #if !defined(NO_SHA)
  29556. ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
  29557. ExpectIntNE(ret2, 0);
  29558. ExpectIntEQ(ret1, ret2);
  29559. #else
  29560. (void) ret2;
  29561. #endif
  29562. X509_free(x509);
  29563. #endif
  29564. return EXPECT_RESULT();
  29565. }
  29566. static int test_wolfSSL_X509_check_host(void)
  29567. {
  29568. EXPECT_DECLS;
  29569. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  29570. && !defined(NO_SHA) && !defined(NO_RSA)
  29571. X509* x509 = NULL;
  29572. const char altName[] = "example.com";
  29573. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29574. SSL_FILETYPE_PEM));
  29575. ExpectIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  29576. WOLFSSL_SUCCESS);
  29577. ExpectIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  29578. WOLFSSL_FAILURE);
  29579. X509_free(x509);
  29580. ExpectIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  29581. WOLFSSL_FAILURE);
  29582. #endif
  29583. return EXPECT_RESULT();
  29584. }
  29585. static int test_wolfSSL_X509_check_email(void)
  29586. {
  29587. EXPECT_DECLS;
  29588. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  29589. X509* x509 = NULL;
  29590. const char goodEmail[] = "info@wolfssl.com";
  29591. const char badEmail[] = "disinfo@wolfssl.com";
  29592. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29593. SSL_FILETYPE_PEM));
  29594. /* Should fail on non-matching email address */
  29595. ExpectIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  29596. WOLFSSL_FAILURE);
  29597. /* Should succeed on matching email address */
  29598. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  29599. WOLFSSL_SUCCESS);
  29600. /* Should compute length internally when not provided */
  29601. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  29602. WOLFSSL_SUCCESS);
  29603. /* Should fail when email address is NULL */
  29604. ExpectIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  29605. WOLFSSL_FAILURE);
  29606. X509_free(x509);
  29607. /* Should fail when x509 is NULL */
  29608. ExpectIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  29609. WOLFSSL_FAILURE);
  29610. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  29611. return EXPECT_RESULT();
  29612. }
  29613. static int test_wc_PemToDer(void)
  29614. {
  29615. EXPECT_DECLS;
  29616. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  29617. int ret;
  29618. DerBuffer* pDer = NULL;
  29619. const char* ca_cert = "./certs/server-cert.pem";
  29620. byte* cert_buf = NULL;
  29621. size_t cert_sz = 0;
  29622. int eccKey = 0;
  29623. EncryptedInfo info;
  29624. XMEMSET(&info, 0, sizeof(info));
  29625. ExpectIntEQ(ret = load_file(ca_cert, &cert_buf, &cert_sz), 0);
  29626. ExpectIntEQ(ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE, &pDer, NULL,
  29627. &info, &eccKey), 0);
  29628. wc_FreeDer(&pDer);
  29629. pDer = NULL;
  29630. if (cert_buf != NULL) {
  29631. free(cert_buf);
  29632. cert_buf = NULL;
  29633. }
  29634. #ifdef HAVE_ECC
  29635. {
  29636. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  29637. byte key_buf[256] = {0};
  29638. /* Test fail of loading a key with cert type */
  29639. ExpectIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  29640. key_buf[0] = '\n';
  29641. ExpectNotNull(XMEMCPY(key_buf + 1, cert_buf, cert_sz));
  29642. ExpectIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  29643. &pDer, NULL, &info, &eccKey)), 0);
  29644. #ifdef OPENSSL_EXTRA
  29645. ExpectIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  29646. &pDer, NULL, &info, &eccKey)), 0);
  29647. #endif
  29648. wc_FreeDer(&pDer);
  29649. if (cert_buf != NULL)
  29650. free(cert_buf);
  29651. }
  29652. #endif
  29653. #endif
  29654. return EXPECT_RESULT();
  29655. }
  29656. static int test_wc_AllocDer(void)
  29657. {
  29658. EXPECT_DECLS;
  29659. #if !defined(NO_CERTS)
  29660. DerBuffer* pDer = NULL;
  29661. word32 testSize = 1024;
  29662. ExpectIntEQ(wc_AllocDer(NULL, testSize, CERT_TYPE, HEAP_HINT),
  29663. BAD_FUNC_ARG);
  29664. ExpectIntEQ(wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT), 0);
  29665. ExpectNotNull(pDer);
  29666. wc_FreeDer(&pDer);
  29667. #endif
  29668. return EXPECT_RESULT();
  29669. }
  29670. static int test_wc_CertPemToDer(void)
  29671. {
  29672. EXPECT_DECLS;
  29673. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  29674. const char* ca_cert = "./certs/ca-cert.pem";
  29675. byte* cert_buf = NULL;
  29676. size_t cert_sz = 0;
  29677. size_t cert_dersz = 0;
  29678. byte* cert_der = NULL;
  29679. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  29680. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  29681. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29682. ExpectIntGE(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  29683. (int)cert_dersz, CERT_TYPE), 0);
  29684. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, -1, CERT_TYPE),
  29685. BAD_FUNC_ARG);
  29686. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL, -1, CERT_TYPE),
  29687. BAD_FUNC_ARG);
  29688. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der, -1, CERT_TYPE),
  29689. BAD_FUNC_ARG);
  29690. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, (int)cert_dersz,
  29691. CERT_TYPE), BAD_FUNC_ARG);
  29692. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der,
  29693. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  29694. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL,
  29695. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  29696. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der, -1,
  29697. CERT_TYPE), BAD_FUNC_ARG);
  29698. if (cert_der != NULL)
  29699. free(cert_der);
  29700. if (cert_buf != NULL)
  29701. free(cert_buf);
  29702. #endif
  29703. return EXPECT_RESULT();
  29704. }
  29705. static int test_wc_KeyPemToDer(void)
  29706. {
  29707. EXPECT_DECLS;
  29708. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29709. int ret = 0;
  29710. const byte cert_buf[] = \
  29711. "-----BEGIN PRIVATE KEY-----\n"
  29712. "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDMG5KgWxP002pA\n"
  29713. "QJIdA4H5N0oM1Wf0LrHcos5RYUlrHDkC2b5p2BUpVRPmgDAFD2+8leim98x0BvcB\n"
  29714. "k48TNzrVynuwyVEY664+iQyzEBO5v27HPRydOddprbLCvRO036XINGIjauy1jHFi\n"
  29715. "HaDVx3bexSwgp9aefUGAszFXi4q1J4GacV7Cr2b/wBqUHqWv4ZXPu6R9/UYngTkD\n"
  29716. "UDJL5gLlLfcLzNyyodKPHPCIAKdWn6mSVdcHk8XVpK4y9lgz4E7YDWA6ohKZgWgG\n"
  29717. "2RDha8CMilFMDgYa0G0SiS9g3PQx0qh3AMXJJsKSVhScFCZufAE0kV6KvjP7jAqP\n"
  29718. "XBiSkRGPAgMBAAECggEAW7hmRyY2jRX2UMJThrM9VIs6fRLnYI0dQ0tsEJj536ay\n"
  29719. "nevQjArc05KWW0Yujg+WRDZPcry3RUqd9Djlmhp/F3Si6dpF1b+PMS3wJYVrf9Sd\n"
  29720. "SO5W7faArU4vnyBNe0HnY1Ta5xSVI65lg1RSIs88RTZwsooJwXYDGf0shq0/21CE\n"
  29721. "V8HOb27DDYNcEnm35lzaONjFnMqQQT2Vs9anRrPiSEXNleEvTgLVXZtGTyCGTz6v\n"
  29722. "x86Y8eSWL9YNHvPE1I+mDPuocfSR7eRNgRu7SK3mn94W5mqd7Ns072YKX/2XN1mO\n"
  29723. "66+ZFHO6v4dK1u7cSjuwrU1EhLHpUsgDz6Bna5InyQKBgQDv5l8RPy8UneKSADaf\n"
  29724. "M5L/5675I/5t4nqVjvbnQje00YveLTAEjlJBNR93Biln3sYgnvNamYDCxyEuUZ/I\n"
  29725. "S/vmBL9PoxfGZow4FcsIBOEbIn3E0SYJgCBNWthquUvGpKsYDnThJuhO+1cVmxAJ\n"
  29726. "BUOjLFnJYHM0a+Vmk9GexT2OBwKBgQDZzkUBOK7Im3eiYytFocUJyhqMH30d49X9\n"
  29727. "ujC7kGw4UWAqVe7YCSvlBa8nzWpRWK2kRpu3M0272RU0V4geyWqT+nr/SvRRPtNP\n"
  29728. "F5dY8l3yR7hjtSejqqjOfBcZT6ETJxI4tiG0+Nl5BlfM5M+0nxnkWpRcHuOR3j79\n"
  29729. "YUFERyN+OQKBgQCjlOKeUAc6d65W/+4/AFvsQ378Q57qLtSHxsR1TKHPmlNVXFqx\n"
  29730. "wJo1/JNIBduWCEHxXHF0BdfW+RGXE/FwEt/hKLuLAhrkHmjelX2sKieU6R/5ZOQa\n"
  29731. "9lMQbDHGFDOncAF6leD85hriQGBRSzrT69MDIOrYdfwYcroqCAGX0cb3YQKBgQC8\n"
  29732. "iIFQylj5SyHmjcMSNjKSA8CxFDzAV8yPIdE3Oo+CvGXqn5HsrRuy1hXE9VmXapR8\n"
  29733. "A6ackSszdHiXY0FvrNe1mfdH7wDHJwPQjdIzazCJHS3uGQxj7sDKY7226ie6pXJv\n"
  29734. "ZrCMr2/IBAaSVGm6ppHKCeIsT4ybYm7R85KEYLPHeQKBgBeJOMBinXQfWN/1jT9b\n"
  29735. "6Ywrutvp2zP8hVxQGSZJ0WG4iewZyFLsPUlbWRXOSYNPElHmdD0ZomdLVm+lSpAA\n"
  29736. "XSH5FJ/IFCwqq7Eft6Gf8NFRV+NjPMUny+PnjHe4oFP8YK/Ek22K3ttNG8Hw69Aw\n"
  29737. "AQue5o6oVfhgLiJzMdo/77gw\n"
  29738. "-----END PRIVATE KEY-----\n";
  29739. const int cert_sz = sizeof(cert_buf);
  29740. const char cert_pw[] = "password";
  29741. int cert_dersz = 0;
  29742. byte* cert_der = NULL;
  29743. /* Bad arg: Cert buffer is NULL */
  29744. ExpectIntEQ(wc_KeyPemToDer(NULL, cert_sz, cert_der, cert_dersz, ""),
  29745. BAD_FUNC_ARG);
  29746. /* Bad arg: Cert DER buffer non-NULL but size zero (or less) */
  29747. ExpectIntEQ(wc_KeyPemToDer(cert_buf, cert_sz, (byte*)&cert_der, 0, ""),
  29748. BAD_FUNC_ARG);
  29749. /* Test normal operation */
  29750. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  29751. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29752. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  29753. cert_pw), 0);
  29754. ExpectIntLE(ret, cert_sz);
  29755. if (cert_der != NULL) {
  29756. free(cert_der);
  29757. cert_der = NULL;
  29758. }
  29759. /* Test NULL for DER buffer to return needed DER buffer size */
  29760. ExpectIntGT(ret = wc_KeyPemToDer(cert_buf, cert_sz, NULL, 0, ""), 0);
  29761. ExpectIntLE(ret, cert_sz);
  29762. if (EXPECT_SUCCESS())
  29763. cert_dersz = ret;
  29764. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29765. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  29766. cert_pw), 0);
  29767. ExpectIntLE(ret, cert_sz);
  29768. if (cert_der != NULL)
  29769. free(cert_der);
  29770. #endif
  29771. return EXPECT_RESULT();
  29772. }
  29773. static int test_wc_PubKeyPemToDer(void)
  29774. {
  29775. EXPECT_DECLS;
  29776. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
  29777. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  29778. int ret = 0;
  29779. const char* key = "./certs/ecc-client-keyPub.pem";
  29780. byte* cert_buf = NULL;
  29781. size_t cert_sz = 0, cert_dersz = 0;
  29782. byte* cert_der = NULL;
  29783. ExpectIntEQ(wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  29784. cert_der, (int)cert_dersz), BAD_FUNC_ARG);
  29785. ExpectIntEQ(load_file(key, &cert_buf, &cert_sz), 0);
  29786. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  29787. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29788. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  29789. (int)cert_dersz), 0);
  29790. if (cert_der != NULL) {
  29791. free(cert_der);
  29792. cert_der = NULL;
  29793. }
  29794. /* Test NULL for DER buffer to return needed DER buffer size */
  29795. ExpectIntGT(ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz, NULL, 0), 0);
  29796. ExpectIntLE(ret, cert_sz);
  29797. cert_dersz = ret;
  29798. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29799. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  29800. (int)cert_dersz), 0);
  29801. if (cert_der != NULL) {
  29802. free(cert_der);
  29803. }
  29804. if (cert_buf != NULL) {
  29805. free(cert_buf);
  29806. }
  29807. #endif
  29808. return EXPECT_RESULT();
  29809. }
  29810. static int test_wc_PemPubKeyToDer(void)
  29811. {
  29812. EXPECT_DECLS;
  29813. #if !defined(NO_FILESYSTEM) && \
  29814. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  29815. const char* key = "./certs/ecc-client-keyPub.pem";
  29816. size_t cert_dersz = 1024;
  29817. byte* cert_der = NULL;
  29818. ExpectIntGE(wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz),
  29819. BAD_FUNC_ARG);
  29820. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29821. ExpectIntGE(wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz), 0);
  29822. if (cert_der != NULL) {
  29823. free(cert_der);
  29824. }
  29825. #endif
  29826. return EXPECT_RESULT();
  29827. }
  29828. static int test_wc_GetPubKeyDerFromCert(void)
  29829. {
  29830. EXPECT_DECLS;
  29831. #if !defined(NO_RSA) || defined(HAVE_ECC)
  29832. int ret;
  29833. word32 idx = 0;
  29834. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  29835. word32 keyDerSz = (word32)sizeof(keyDer);
  29836. DecodedCert decoded;
  29837. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  29838. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  29839. word32 certBufSz = sizeof(certBuf);
  29840. #endif
  29841. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  29842. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  29843. XFILE fp = XBADFILE;
  29844. #endif
  29845. #ifndef NO_RSA
  29846. RsaKey rsaKey;
  29847. #if defined(USE_CERT_BUFFERS_2048)
  29848. byte* rsaCertDer = (byte*)client_cert_der_2048;
  29849. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  29850. #elif defined(USE_CERT_BUFFERS_1024)
  29851. byte* rsaCertDer = (byte*)client_cert_der_1024;
  29852. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  29853. #else
  29854. unsigned char rsaCertDer[TWOK_BUF];
  29855. word32 rsaCertDerSz;
  29856. #endif
  29857. #endif
  29858. #ifdef HAVE_ECC
  29859. ecc_key eccKey;
  29860. #if defined(USE_CERT_BUFFERS_256)
  29861. byte* eccCert = (byte*)cliecc_cert_der_256;
  29862. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  29863. #else
  29864. unsigned char eccCert[ONEK_BUF];
  29865. word32 eccCertSz;
  29866. XFILE fp2 = XBADFILE;
  29867. #endif
  29868. #endif
  29869. #ifndef NO_RSA
  29870. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  29871. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  29872. ExpectIntGT(rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer),
  29873. fp), 0);
  29874. if (fp != XBADFILE) {
  29875. XFCLOSE(fp);
  29876. fp = XBADFILE;
  29877. }
  29878. #endif
  29879. /* good test case - RSA DER cert */
  29880. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  29881. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  29882. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  29883. ExpectIntGT(keyDerSz, 0);
  29884. /* sanity check, verify we can import DER public key */
  29885. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  29886. ExpectIntEQ(ret, 0);
  29887. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  29888. if (ret == 0) {
  29889. wc_FreeRsaKey(&rsaKey);
  29890. }
  29891. /* test LENGTH_ONLY_E case */
  29892. keyDerSz = 0;
  29893. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  29894. LENGTH_ONLY_E);
  29895. ExpectIntGT(keyDerSz, 0);
  29896. /* bad args: DecodedCert NULL */
  29897. ExpectIntEQ(wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz), BAD_FUNC_ARG);
  29898. /* bad args: output key buff size */
  29899. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL), BAD_FUNC_ARG);
  29900. /* bad args: zero size output key buffer */
  29901. keyDerSz = 0;
  29902. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  29903. BAD_FUNC_ARG);
  29904. wc_FreeDecodedCert(&decoded);
  29905. /* Certificate Request Tests */
  29906. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  29907. {
  29908. XMEMSET(certBuf, 0, sizeof(certBuf));
  29909. ExpectTrue((fp = XFOPEN("./certs/csr.signed.der", "rb")) != XBADFILE);
  29910. ExpectIntGT(certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp), 0);
  29911. if (fp != XBADFILE) {
  29912. XFCLOSE(fp);
  29913. }
  29914. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  29915. ExpectIntEQ(wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL), 0);
  29916. /* good test case - RSA DER certificate request */
  29917. keyDerSz = sizeof(keyDer);
  29918. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  29919. 0);
  29920. ExpectIntGT(keyDerSz, 0);
  29921. /* sanity check, verify we can import DER public key */
  29922. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  29923. ExpectIntEQ(ret, 0);
  29924. idx = 0;
  29925. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  29926. if (ret == 0) {
  29927. wc_FreeRsaKey(&rsaKey);
  29928. }
  29929. wc_FreeDecodedCert(&decoded);
  29930. }
  29931. #endif /* WOLFSSL_CERT_REQ */
  29932. #endif /* NO_RSA */
  29933. #ifdef HAVE_ECC
  29934. #ifndef USE_CERT_BUFFERS_256
  29935. ExpectTrue((fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  29936. XBADFILE);
  29937. ExpectIntGT(eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2), 0);
  29938. if (fp2 != XBADFILE) {
  29939. XFCLOSE(fp2);
  29940. }
  29941. #endif
  29942. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  29943. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  29944. /* good test case - ECC */
  29945. XMEMSET(keyDer, 0, sizeof(keyDer));
  29946. keyDerSz = sizeof(keyDer);
  29947. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  29948. ExpectIntGT(keyDerSz, 0);
  29949. /* sanity check, verify we can import DER public key */
  29950. ret = wc_ecc_init(&eccKey);
  29951. ExpectIntEQ(ret, 0);
  29952. idx = 0; /* reset idx to 0, used above in RSA case */
  29953. ExpectIntEQ(wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz), 0);
  29954. if (ret == 0) {
  29955. wc_ecc_free(&eccKey);
  29956. }
  29957. /* test LENGTH_ONLY_E case */
  29958. keyDerSz = 0;
  29959. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  29960. LENGTH_ONLY_E);
  29961. ExpectIntGT(keyDerSz, 0);
  29962. wc_FreeDecodedCert(&decoded);
  29963. #endif
  29964. #endif /* !NO_RSA || HAVE_ECC */
  29965. return EXPECT_RESULT();
  29966. }
  29967. static int test_wc_CheckCertSigPubKey(void)
  29968. {
  29969. EXPECT_DECLS;
  29970. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29971. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  29972. int ret = 0;
  29973. const char* ca_cert = "./certs/ca-cert.pem";
  29974. byte* cert_buf = NULL;
  29975. size_t cert_sz = 0;
  29976. byte* cert_der = NULL;
  29977. word32 cert_dersz = 0;
  29978. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  29979. word32 keyDerSz = (word32)sizeof(keyDer);
  29980. DecodedCert decoded;
  29981. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  29982. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  29983. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29984. ExpectIntGE(ret = wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  29985. (int)cert_dersz, CERT_TYPE), 0);
  29986. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  29987. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  29988. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  29989. ExpectIntGT(keyDerSz, 0);
  29990. /* Good test case. */
  29991. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  29992. keyDerSz, RSAk), 0);
  29993. /* No certificate. */
  29994. ExpectIntEQ(wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  29995. ECDSAk), BAD_FUNC_ARG);
  29996. /* Bad cert size. */
  29997. ExpectIntNE(ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  29998. RSAk), 0);
  29999. ExpectTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  30000. /* No public key. */
  30001. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL,
  30002. keyDerSz, RSAk), ASN_NO_SIGNER_E);
  30003. /* Bad public key size. */
  30004. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  30005. RSAk), BAD_FUNC_ARG);
  30006. /* Wrong aglo. */
  30007. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  30008. keyDerSz, ECDSAk), ASN_PARSE_E);
  30009. wc_FreeDecodedCert(&decoded);
  30010. if (cert_der != NULL)
  30011. free(cert_der);
  30012. if (cert_buf != NULL)
  30013. free(cert_buf);
  30014. #endif
  30015. return EXPECT_RESULT();
  30016. }
  30017. static int test_wolfSSL_certs(void)
  30018. {
  30019. EXPECT_DECLS;
  30020. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  30021. !defined(NO_RSA)
  30022. X509* x509ext = NULL;
  30023. #ifdef OPENSSL_ALL
  30024. X509* x509 = NULL;
  30025. WOLFSSL_X509_EXTENSION* ext = NULL;
  30026. ASN1_OBJECT* obj = NULL;
  30027. #endif
  30028. WOLFSSL* ssl = NULL;
  30029. WOLFSSL_CTX* ctx = NULL;
  30030. STACK_OF(ASN1_OBJECT)* sk = NULL;
  30031. ASN1_STRING* asn1_str = NULL;
  30032. AUTHORITY_KEYID* akey = NULL;
  30033. BASIC_CONSTRAINTS* bc = NULL;
  30034. int crit = 0;
  30035. #ifndef NO_WOLFSSL_SERVER
  30036. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  30037. #else
  30038. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  30039. #endif
  30040. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30041. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30042. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  30043. #if !defined(NO_CHECK_PRIVATE_KEY)
  30044. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  30045. #endif
  30046. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30047. #if !defined(NO_CHECK_PRIVATE_KEY)
  30048. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  30049. #endif
  30050. ExpectNotNull(ssl = SSL_new(ctx));
  30051. #if !defined(NO_CHECK_PRIVATE_KEY)
  30052. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30053. #endif
  30054. #ifdef HAVE_PK_CALLBACKS
  30055. ExpectIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  30056. #endif /* HAVE_PK_CALLBACKS */
  30057. /* create and use x509 */
  30058. #ifdef OPENSSL_ALL
  30059. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  30060. WOLFSSL_FILETYPE_PEM));
  30061. #endif
  30062. ExpectNotNull(x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  30063. WOLFSSL_FILETYPE_PEM));
  30064. ExpectIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  30065. #if !defined(NO_CHECK_PRIVATE_KEY)
  30066. /* with loading in a new cert the check on private key should now fail */
  30067. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30068. #endif
  30069. #if defined(USE_CERT_BUFFERS_2048)
  30070. ExpectIntEQ(SSL_use_certificate_ASN1(ssl,
  30071. (unsigned char*)server_cert_der_2048,
  30072. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  30073. #endif
  30074. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  30075. /************* Get Digest of Certificate ******************/
  30076. {
  30077. byte digest[64]; /* max digest size */
  30078. word32 digestSz;
  30079. XMEMSET(digest, 0, sizeof(digest));
  30080. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  30081. WOLFSSL_SUCCESS);
  30082. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  30083. WOLFSSL_SUCCESS);
  30084. ExpectIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  30085. WOLFSSL_FAILURE);
  30086. }
  30087. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  30088. /* test and checkout X509 extensions */
  30089. ExpectNotNull(bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext,
  30090. NID_basic_constraints, &crit, NULL));
  30091. ExpectIntEQ(crit, 0);
  30092. #ifdef OPENSSL_ALL
  30093. ExpectNotNull(ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc));
  30094. X509_EXTENSION_free(ext);
  30095. ext = NULL;
  30096. ExpectNotNull(ext = X509_EXTENSION_new());
  30097. X509_EXTENSION_set_critical(ext, 1);
  30098. ExpectNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  30099. ExpectIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  30100. ASN1_OBJECT_free(obj);
  30101. obj = NULL;
  30102. X509_EXTENSION_free(ext);
  30103. ext = NULL;
  30104. ExpectNotNull(ext = X509_EXTENSION_new());
  30105. X509_EXTENSION_set_critical(ext, 0);
  30106. ExpectIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  30107. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  30108. NULL);
  30109. ExpectIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  30110. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  30111. * and X509_get_ext_d2i has created new */
  30112. asn1_str = NULL;
  30113. X509_EXTENSION_free(ext);
  30114. ext = NULL;
  30115. #endif
  30116. BASIC_CONSTRAINTS_free(bc);
  30117. bc = NULL;
  30118. ExpectNotNull(asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext,
  30119. NID_key_usage, &crit, NULL));
  30120. ExpectIntEQ(crit, 1);
  30121. ExpectIntEQ(asn1_str->type, NID_key_usage);
  30122. #ifdef OPENSSL_ALL
  30123. ExpectNotNull(ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str));
  30124. X509_EXTENSION_free(ext);
  30125. ext = NULL;
  30126. #endif
  30127. ASN1_STRING_free(asn1_str);
  30128. asn1_str = NULL;
  30129. #ifdef OPENSSL_ALL
  30130. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  30131. NID_ext_key_usage, &crit, NULL));
  30132. ExpectNotNull(ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk));
  30133. X509_EXTENSION_free(ext);
  30134. ext = NULL;
  30135. EXTENDED_KEY_USAGE_free(sk);
  30136. sk = NULL;
  30137. #else
  30138. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  30139. &crit, NULL);
  30140. ExpectNull(sk);
  30141. #endif
  30142. ExpectNotNull(akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  30143. NID_authority_key_identifier, &crit, NULL));
  30144. #ifdef OPENSSL_ALL
  30145. ExpectNotNull(ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit,
  30146. akey));
  30147. X509_EXTENSION_free(ext);
  30148. ext = NULL;
  30149. #endif
  30150. wolfSSL_AUTHORITY_KEYID_free(akey);
  30151. akey = NULL;
  30152. /* NID not yet supported */
  30153. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30154. NID_private_key_usage_period, &crit, NULL));
  30155. ExpectIntEQ(crit, -1);
  30156. sk_ASN1_OBJECT_free(sk);
  30157. sk = NULL;
  30158. ExpectNotNull(sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext,
  30159. NID_subject_alt_name, &crit, NULL));
  30160. {
  30161. int i;
  30162. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  30163. GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
  30164. ExpectIntEQ(gen->type, GEN_DNS);
  30165. ExpectIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
  30166. }
  30167. }
  30168. sk_GENERAL_NAME_free(sk);
  30169. sk = NULL;
  30170. /* NID not yet supported */
  30171. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30172. NID_issuer_alt_name, &crit, NULL));
  30173. ExpectIntEQ(crit, -1);
  30174. sk_ASN1_OBJECT_free(sk);
  30175. sk = NULL;
  30176. /* NID not yet supported */
  30177. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30178. NID_info_access, &crit, NULL));
  30179. sk_ASN1_OBJECT_free(sk);
  30180. sk = NULL;
  30181. /* NID not yet supported */
  30182. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30183. NID_sinfo_access, &crit, NULL));
  30184. ExpectIntEQ(crit, -1);
  30185. sk_ASN1_OBJECT_free(sk);
  30186. sk = NULL;
  30187. /* NID not yet supported */
  30188. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30189. NID_name_constraints, &crit, NULL));
  30190. ExpectIntEQ(crit, -1);
  30191. sk_ASN1_OBJECT_free(sk);
  30192. sk = NULL;
  30193. /* no cert policy set */
  30194. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30195. NID_certificate_policies, &crit, NULL));
  30196. sk_ASN1_OBJECT_free(sk);
  30197. sk = NULL;
  30198. /* NID not yet supported */
  30199. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30200. NID_policy_mappings, &crit, NULL));
  30201. ExpectIntEQ(crit, -1);
  30202. sk_ASN1_OBJECT_free(sk);
  30203. sk = NULL;
  30204. /* NID not yet supported */
  30205. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30206. NID_policy_constraints, &crit, NULL));
  30207. ExpectIntEQ(crit, -1);
  30208. sk_ASN1_OBJECT_free(sk);
  30209. sk = NULL;
  30210. /* NID not yet supported */
  30211. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30212. NID_inhibit_any_policy, &crit, NULL));
  30213. ExpectIntEQ(crit, -1);
  30214. sk_ASN1_OBJECT_free(sk);
  30215. sk = NULL;
  30216. /* NID not yet supported */
  30217. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30218. NID_tlsfeature, &crit, NULL));
  30219. ExpectIntEQ(crit, -1);
  30220. sk_ASN1_OBJECT_free(sk);
  30221. sk = NULL;
  30222. /* test invalid cases */
  30223. crit = 0;
  30224. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit,
  30225. NULL));
  30226. ExpectIntEQ(crit, -1);
  30227. /* NULL passed for criticality. */
  30228. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL,
  30229. NID_tlsfeature, NULL, NULL));
  30230. ExpectIntEQ(SSL_get_hit(ssl), 0);
  30231. #ifdef OPENSSL_ALL
  30232. X509_free(x509);
  30233. #endif
  30234. X509_free(x509ext);
  30235. SSL_free(ssl);
  30236. SSL_CTX_free(ctx);
  30237. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  30238. return EXPECT_RESULT();
  30239. }
  30240. static int test_wolfSSL_X509_check_private_key(void)
  30241. {
  30242. EXPECT_DECLS;
  30243. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  30244. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  30245. X509* x509 = NULL;
  30246. EVP_PKEY* pkey = NULL;
  30247. const byte* key;
  30248. /* Check with correct key */
  30249. ExpectNotNull((x509 = X509_load_certificate_file(cliCertFile,
  30250. SSL_FILETYPE_PEM)));
  30251. key = client_key_der_2048;
  30252. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  30253. (long)sizeof_client_key_der_2048));
  30254. ExpectIntEQ(X509_check_private_key(x509, pkey), 1);
  30255. EVP_PKEY_free(pkey);
  30256. pkey = NULL;
  30257. /* Check with wrong key */
  30258. key = server_key_der_2048;
  30259. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  30260. (long)sizeof_server_key_der_2048));
  30261. ExpectIntEQ(X509_check_private_key(x509, pkey), 0);
  30262. /* test for incorrect parameter */
  30263. ExpectIntEQ(X509_check_private_key(NULL, pkey), 0);
  30264. ExpectIntEQ(X509_check_private_key(x509, NULL), 0);
  30265. ExpectIntEQ(X509_check_private_key(NULL, NULL), 0);
  30266. EVP_PKEY_free(pkey);
  30267. X509_free(x509);
  30268. #endif
  30269. return EXPECT_RESULT();
  30270. }
  30271. static int test_wolfSSL_private_keys(void)
  30272. {
  30273. EXPECT_DECLS;
  30274. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30275. !defined(NO_FILESYSTEM)
  30276. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30277. WOLFSSL* ssl = NULL;
  30278. WOLFSSL_CTX* ctx = NULL;
  30279. EVP_PKEY* pkey = NULL;
  30280. OpenSSL_add_all_digests();
  30281. OpenSSL_add_all_algorithms();
  30282. #ifndef NO_RSA
  30283. #ifndef NO_WOLFSSL_SERVER
  30284. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30285. #else
  30286. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30287. #endif
  30288. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  30289. /* Have to load a cert before you can check the private key against that
  30290. * certificates public key! */
  30291. #if !defined(NO_CHECK_PRIVATE_KEY)
  30292. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  30293. #endif
  30294. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  30295. #if !defined(NO_CHECK_PRIVATE_KEY)
  30296. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30297. #endif
  30298. ExpectNotNull(ssl = SSL_new(ctx));
  30299. #if !defined(NO_CHECK_PRIVATE_KEY)
  30300. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30301. #endif
  30302. #ifdef USE_CERT_BUFFERS_2048
  30303. {
  30304. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  30305. unsigned char buf[FOURK_BUF];
  30306. word32 bufSz;
  30307. ExpectIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  30308. (unsigned char*)client_key_der_2048,
  30309. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  30310. #if !defined(NO_CHECK_PRIVATE_KEY)
  30311. /* Should mismatch now that a different private key loaded */
  30312. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30313. #endif
  30314. ExpectIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  30315. (unsigned char*)server_key,
  30316. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  30317. #if !defined(NO_CHECK_PRIVATE_KEY)
  30318. /* After loading back in DER format of original key, should match */
  30319. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30320. #endif
  30321. /* test loading private key to the WOLFSSL_CTX */
  30322. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  30323. (unsigned char*)client_key_der_2048,
  30324. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  30325. #if !defined(NO_CHECK_PRIVATE_KEY)
  30326. /* Should mismatch now that a different private key loaded */
  30327. ExpectIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30328. #endif
  30329. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  30330. (unsigned char*)server_key,
  30331. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  30332. #if !defined(NO_CHECK_PRIVATE_KEY)
  30333. /* After loading back in DER format of original key, should match */
  30334. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30335. #endif
  30336. /* pkey not set yet, expecting to fail */
  30337. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  30338. /* set PKEY and test again */
  30339. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  30340. &server_key, (long)sizeof_server_key_der_2048));
  30341. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  30342. /* reuse PKEY structure and test
  30343. * this should be checked with a memory management sanity checker */
  30344. ExpectFalse(server_key == (const unsigned char*)server_key_der_2048);
  30345. server_key = (const unsigned char*)server_key_der_2048;
  30346. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  30347. &server_key, (long)sizeof_server_key_der_2048));
  30348. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  30349. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  30350. bufSz = FOURK_BUF;
  30351. ExpectIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  30352. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  30353. RSAk, NULL, 0)), 0);
  30354. server_key = (const unsigned char*)buf;
  30355. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  30356. (long)bufSz));
  30357. }
  30358. #endif
  30359. EVP_PKEY_free(pkey);
  30360. pkey = NULL;
  30361. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  30362. ssl = NULL;
  30363. SSL_CTX_free(ctx);
  30364. ctx = NULL;
  30365. #endif /* end of RSA private key match tests */
  30366. #ifdef HAVE_ECC
  30367. #ifndef NO_WOLFSSL_SERVER
  30368. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30369. #else
  30370. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30371. #endif
  30372. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  30373. WOLFSSL_FILETYPE_PEM));
  30374. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  30375. WOLFSSL_FILETYPE_PEM));
  30376. ExpectNotNull(ssl = SSL_new(ctx));
  30377. #if !defined(NO_CHECK_PRIVATE_KEY)
  30378. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30379. #endif
  30380. SSL_free(ssl);
  30381. ssl = NULL;
  30382. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  30383. WOLFSSL_FILETYPE_PEM));
  30384. ExpectNotNull(ssl = SSL_new(ctx));
  30385. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  30386. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30387. #endif
  30388. SSL_free(ssl);
  30389. ssl = NULL;
  30390. SSL_CTX_free(ctx);
  30391. ctx = NULL;
  30392. #endif /* end of ECC private key match tests */
  30393. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  30394. #ifndef NO_WOLFSSL_SERVER
  30395. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30396. #else
  30397. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30398. #endif
  30399. ExpectTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  30400. WOLFSSL_FILETYPE_PEM));
  30401. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  30402. WOLFSSL_FILETYPE_PEM));
  30403. ExpectNotNull(ssl = SSL_new(ctx));
  30404. #if !defined(NO_CHECK_PRIVATE_KEY)
  30405. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30406. #endif
  30407. SSL_free(ssl);
  30408. ssl = NULL;
  30409. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  30410. WOLFSSL_FILETYPE_PEM));
  30411. ExpectNotNull(ssl = SSL_new(ctx));
  30412. #if !defined(NO_CHECK_PRIVATE_KEY)
  30413. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30414. #endif
  30415. SSL_free(ssl);
  30416. ssl = NULL;
  30417. SSL_CTX_free(ctx);
  30418. ctx = NULL;
  30419. #endif /* end of Ed25519 private key match tests */
  30420. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  30421. #ifndef NO_WOLFSSL_SERVER
  30422. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30423. #else
  30424. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30425. #endif
  30426. ExpectTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  30427. WOLFSSL_FILETYPE_PEM));
  30428. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  30429. WOLFSSL_FILETYPE_PEM));
  30430. ExpectNotNull(ssl = SSL_new(ctx));
  30431. #if !defined(NO_CHECK_PRIVATE_KEY)
  30432. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30433. #endif
  30434. SSL_free(ssl);
  30435. ssl = NULL;
  30436. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  30437. WOLFSSL_FILETYPE_PEM));
  30438. ExpectNotNull(ssl = SSL_new(ctx));
  30439. #if !defined(NO_CHECK_PRIVATE_KEY)
  30440. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30441. #endif
  30442. SSL_free(ssl);
  30443. ssl = NULL;
  30444. SSL_CTX_free(ctx);
  30445. ctx = NULL;
  30446. #endif /* end of Ed448 private key match tests */
  30447. EVP_cleanup();
  30448. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  30449. CONF_modules_free();
  30450. ENGINE_cleanup();
  30451. CONF_modules_unload();
  30452. (void)ssl;
  30453. (void)ctx;
  30454. (void)pkey;
  30455. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30456. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  30457. return EXPECT_RESULT();
  30458. }
  30459. static int test_wolfSSL_PEM_def_callback(void)
  30460. {
  30461. EXPECT_DECLS;
  30462. #ifdef OPENSSL_EXTRA
  30463. char buf[10];
  30464. const char* defpwd = "DEF PWD";
  30465. int defpwdLen = (int)XSTRLEN(defpwd);
  30466. int smallLen = 1;
  30467. /* Bad parameters. */
  30468. ExpectIntEQ(wolfSSL_PEM_def_callback(NULL, sizeof(buf), 0, NULL), 0);
  30469. ExpectIntEQ(wolfSSL_PEM_def_callback(NULL, sizeof(buf), 0, (void*)defpwd),
  30470. 0);
  30471. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, sizeof(buf), 0, NULL), 0);
  30472. XMEMSET(buf, 0, sizeof(buf));
  30473. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, sizeof(buf), 0, (void*)defpwd),
  30474. defpwdLen);
  30475. ExpectIntEQ(XMEMCMP(buf, defpwd, defpwdLen), 0);
  30476. ExpectIntEQ(buf[defpwdLen], 0);
  30477. /* Size of buffer is smaller than default password. */
  30478. XMEMSET(buf, 0, sizeof(buf));
  30479. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, smallLen, 0, (void*)defpwd),
  30480. smallLen);
  30481. ExpectIntEQ(XMEMCMP(buf, defpwd, smallLen), 0);
  30482. ExpectIntEQ(buf[smallLen], 0);
  30483. #endif /* OPENSSL_EXTRA */
  30484. return EXPECT_RESULT();
  30485. }
  30486. static int test_wolfSSL_PEM_read_PrivateKey(void)
  30487. {
  30488. EXPECT_DECLS;
  30489. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || \
  30490. !defined(NO_DSA) || defined(HAVE_ECC) || !defined(NO_DH))
  30491. XFILE file = XBADFILE;
  30492. #if !defined(NO_RSA)
  30493. const char* fname_rsa = "./certs/server-key.pem";
  30494. RSA* rsa = NULL;
  30495. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  30496. unsigned char* sig = NULL;
  30497. size_t sigLen = 0;
  30498. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  30499. size_t tbsLen = sizeof(tbs);
  30500. #endif
  30501. #if !defined(NO_DSA)
  30502. const char* fname_dsa = "./certs/dsa2048.pem";
  30503. #endif
  30504. #if defined(HAVE_ECC)
  30505. const char* fname_ec = "./certs/ecc-key.pem";
  30506. #endif
  30507. #if !defined(NO_DH)
  30508. const char* fname_dh = "./certs/dh-priv-2048.pem";
  30509. #endif
  30510. EVP_PKEY* pkey = NULL;
  30511. /* Check error case. */
  30512. ExpectNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  30513. /* not a PEM key. */
  30514. ExpectTrue((file = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  30515. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30516. if (file != XBADFILE)
  30517. XFCLOSE(file);
  30518. file = XBADFILE;
  30519. #ifndef NO_RSA
  30520. /* Read in an RSA key. */
  30521. ExpectTrue((file = XFOPEN(fname_rsa, "rb")) != XBADFILE);
  30522. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30523. if (file != XBADFILE)
  30524. XFCLOSE(file);
  30525. file = XBADFILE;
  30526. /* Make sure the key is usable by signing some data with it. */
  30527. ExpectNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  30528. ExpectIntGT((sigLen = RSA_size(rsa)), 0);
  30529. ExpectNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  30530. DYNAMIC_TYPE_TMP_BUFFER));
  30531. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  30532. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  30533. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  30534. WOLFSSL_SUCCESS);
  30535. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30536. EVP_PKEY_CTX_free(ctx);
  30537. EVP_PKEY_free(pkey);
  30538. pkey = NULL;
  30539. #endif
  30540. #ifndef NO_DSA
  30541. /* Read in a DSA key. */
  30542. ExpectTrue((file = XFOPEN(fname_dsa, "rb")) != XBADFILE);
  30543. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH)
  30544. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30545. EVP_PKEY_free(pkey);
  30546. pkey = NULL;
  30547. #else
  30548. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30549. #endif
  30550. if (file != XBADFILE)
  30551. XFCLOSE(file);
  30552. file = XBADFILE;
  30553. #endif
  30554. #ifdef HAVE_ECC
  30555. /* Read in an EC key. */
  30556. ExpectTrue((file = XFOPEN(fname_ec, "rb")) != XBADFILE);
  30557. ExpectNotNull(pkey = EVP_PKEY_new());
  30558. ExpectPtrEq(PEM_read_PrivateKey(file, &pkey, NULL, NULL), pkey);
  30559. if (file != XBADFILE)
  30560. XFCLOSE(file);
  30561. file = XBADFILE;
  30562. EVP_PKEY_free(pkey);
  30563. pkey = NULL;
  30564. #endif
  30565. #ifndef NO_DH
  30566. /* Read in a DH key. */
  30567. ExpectTrue((file = XFOPEN(fname_dh, "rb")) != XBADFILE);
  30568. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  30569. defined(WOLFSSL_OPENSSH)) && (!defined(HAVE_FIPS) || \
  30570. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  30571. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30572. EVP_PKEY_free(pkey);
  30573. pkey = NULL;
  30574. #else
  30575. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30576. #endif
  30577. if (file != XBADFILE)
  30578. XFCLOSE(file);
  30579. file = XBADFILE;
  30580. #endif
  30581. #endif
  30582. return EXPECT_RESULT();
  30583. }
  30584. static int test_wolfSSL_PEM_read_PUBKEY(void)
  30585. {
  30586. EXPECT_DECLS;
  30587. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  30588. && !defined(NO_FILESYSTEM)
  30589. XFILE file = XBADFILE;
  30590. const char* fname = "./certs/client-keyPub.pem";
  30591. EVP_PKEY* pkey = NULL;
  30592. /* Check error case. */
  30593. ExpectNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  30594. /* Read in an RSA key. */
  30595. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30596. ExpectNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  30597. EVP_PKEY_free(pkey);
  30598. pkey = NULL;
  30599. if (file != XBADFILE)
  30600. XFCLOSE(file);
  30601. file = XBADFILE;
  30602. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30603. ExpectNotNull(pkey = EVP_PKEY_new());
  30604. ExpectPtrEq(PEM_read_PUBKEY(file, &pkey, NULL, NULL), pkey);
  30605. EVP_PKEY_free(pkey);
  30606. if (file != XBADFILE)
  30607. XFCLOSE(file);
  30608. #endif
  30609. return EXPECT_RESULT();
  30610. }
  30611. /* test loading RSA key using BIO */
  30612. static int test_wolfSSL_PEM_PrivateKey_rsa(void)
  30613. {
  30614. EXPECT_DECLS;
  30615. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  30616. defined(USE_CERT_BUFFERS_2048) && !defined(NO_FILESYSTEM) && \
  30617. !defined(NO_BIO)
  30618. BIO* bio = NULL;
  30619. XFILE file = XBADFILE;
  30620. const char* fname = "./certs/server-key.pem";
  30621. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  30622. EVP_PKEY* pkey = NULL;
  30623. size_t sz = 0;
  30624. byte* buf = NULL;
  30625. EVP_PKEY* pkey2 = NULL;
  30626. EVP_PKEY* pkey3 = NULL;
  30627. RSA* rsa_key = NULL;
  30628. #if defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)
  30629. unsigned char extra[10];
  30630. int i;
  30631. BIO* pub_bio = NULL;
  30632. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  30633. #endif
  30634. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30635. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30636. ExpectIntGT(sz = XFTELL(file), 0);
  30637. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30638. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30639. if (buf != NULL) {
  30640. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30641. }
  30642. if (file != XBADFILE) {
  30643. XFCLOSE(file);
  30644. file = XBADFILE;
  30645. }
  30646. /* Test using BIO new mem and loading PEM private key */
  30647. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30648. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30649. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30650. buf = NULL;
  30651. BIO_free(bio);
  30652. bio = NULL;
  30653. /* New empty EVP_PKEY */
  30654. ExpectNotNull(pkey2 = EVP_PKEY_new());
  30655. if (pkey2 != NULL) {
  30656. pkey2->type = EVP_PKEY_RSA;
  30657. }
  30658. /* Test parameter copy */
  30659. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  30660. EVP_PKEY_free(pkey2);
  30661. EVP_PKEY_free(pkey);
  30662. pkey = NULL;
  30663. /* Qt unit test case : rsa pkcs8 key */
  30664. ExpectTrue((file = XFOPEN(fname_rsa_p8, "rb")) != XBADFILE);
  30665. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30666. ExpectIntGT(sz = XFTELL(file), 0);
  30667. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30668. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30669. if (buf) {
  30670. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30671. }
  30672. if (file != XBADFILE) {
  30673. XFCLOSE(file);
  30674. file = XBADFILE;
  30675. }
  30676. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30677. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30678. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30679. buf = NULL;
  30680. BIO_free(bio);
  30681. bio = NULL;
  30682. ExpectNotNull(pkey3 = EVP_PKEY_new());
  30683. ExpectNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  30684. ExpectIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  30685. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30686. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  30687. #else
  30688. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  30689. #endif
  30690. RSA_free(rsa_key);
  30691. EVP_PKEY_free(pkey3);
  30692. EVP_PKEY_free(pkey);
  30693. pkey = NULL;
  30694. pkey2 = NULL;
  30695. #if defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)
  30696. #define BIO_PEM_TEST_CHAR 'a'
  30697. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  30698. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30699. ExpectIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  30700. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30701. ExpectIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  30702. ExpectNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key,
  30703. (long)sizeof_server_key_der_2048));
  30704. ExpectNull(pkey);
  30705. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  30706. (long)sizeof_server_key_der_2048));
  30707. ExpectIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL, NULL),
  30708. WOLFSSL_FAILURE);
  30709. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL, NULL),
  30710. WOLFSSL_FAILURE);
  30711. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30712. WOLFSSL_SUCCESS);
  30713. ExpectIntGT(BIO_pending(bio), 0);
  30714. ExpectIntEQ(BIO_pending(bio), 1679);
  30715. /* Check if the pubkey API writes only the public key */
  30716. #ifdef WOLFSSL_KEY_GEN
  30717. ExpectIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
  30718. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
  30719. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  30720. ExpectIntGT(BIO_pending(pub_bio), 0);
  30721. /* Previously both the private key and the pubkey calls would write
  30722. * out the private key and the PEM header was the only difference.
  30723. * The public PEM should be significantly shorter than the
  30724. * private key versison. */
  30725. ExpectIntEQ(BIO_pending(pub_bio), 451);
  30726. #else
  30727. /* Not supported. */
  30728. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), 0);
  30729. #endif
  30730. /* test creating new EVP_PKEY with good args */
  30731. ExpectNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30732. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  30733. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr,
  30734. pkey->pkey_sz), 0);
  30735. }
  30736. /* test of reuse of EVP_PKEY */
  30737. ExpectNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  30738. ExpectIntEQ(BIO_pending(bio), 0);
  30739. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30740. SSL_SUCCESS);
  30741. /* add 10 extra bytes after PEM */
  30742. ExpectIntEQ(BIO_write(bio, extra, 10), 10);
  30743. ExpectNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  30744. ExpectNotNull(pkey);
  30745. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  30746. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr,
  30747. pkey->pkey_sz), 0);
  30748. }
  30749. /* check 10 extra bytes still there */
  30750. ExpectIntEQ(BIO_pending(bio), 10);
  30751. ExpectIntEQ(BIO_read(bio, extra, 10), 10);
  30752. for (i = 0; i < 10; i++) {
  30753. ExpectIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  30754. }
  30755. BIO_free(pub_bio);
  30756. BIO_free(bio);
  30757. bio = NULL;
  30758. EVP_PKEY_free(pkey);
  30759. pkey = NULL;
  30760. EVP_PKEY_free(pkey2);
  30761. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  30762. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 &&
  30763. * !NO_FILESYSTEM && !NO_BIO */
  30764. return EXPECT_RESULT();
  30765. }
  30766. /* test loading ECC key using BIO */
  30767. static int test_wolfSSL_PEM_PrivateKey_ecc(void)
  30768. {
  30769. EXPECT_DECLS;
  30770. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_ECC) && \
  30771. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  30772. BIO* bio = NULL;
  30773. EVP_PKEY* pkey = NULL;
  30774. XFILE file = XBADFILE;
  30775. const char* fname = "./certs/ecc-key.pem";
  30776. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  30777. size_t sz = 0;
  30778. byte* buf = NULL;
  30779. EVP_PKEY* pkey2 = NULL;
  30780. EVP_PKEY* pkey3 = NULL;
  30781. EC_KEY* ec_key = NULL;
  30782. int nid = 0;
  30783. BIO* pub_bio = NULL;
  30784. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30785. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30786. ExpectIntGT(sz = XFTELL(file), 0);
  30787. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30788. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30789. if (buf) {
  30790. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30791. }
  30792. if (file != XBADFILE) {
  30793. XFCLOSE(file);
  30794. file = XBADFILE;
  30795. }
  30796. /* Test using BIO new mem and loading PEM private key */
  30797. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30798. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30799. BIO_free(bio);
  30800. bio = NULL;
  30801. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30802. buf = NULL;
  30803. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30804. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30805. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30806. WOLFSSL_SUCCESS);
  30807. ExpectIntGT(BIO_pending(bio), 0);
  30808. /* No parmeters. */
  30809. ExpectIntEQ(BIO_pending(bio), 227);
  30810. /* Check if the pubkey API writes only the public key */
  30811. #ifdef WOLFSSL_KEY_GEN
  30812. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  30813. ExpectIntGT(BIO_pending(pub_bio), 0);
  30814. /* Previously both the private key and the pubkey calls would write
  30815. * out the private key and the PEM header was the only difference.
  30816. * The public PEM should be significantly shorter than the
  30817. * private key versison. */
  30818. ExpectIntEQ(BIO_pending(pub_bio), 178);
  30819. #endif
  30820. BIO_free(pub_bio);
  30821. BIO_free(bio);
  30822. bio = NULL;
  30823. ExpectNotNull(pkey2 = EVP_PKEY_new());
  30824. ExpectNotNull(pkey3 = EVP_PKEY_new());
  30825. if (pkey2 != NULL) {
  30826. pkey2->type = EVP_PKEY_EC;
  30827. }
  30828. /* Test parameter copy */
  30829. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  30830. /* Qt unit test case 1*/
  30831. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  30832. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  30833. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30834. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  30835. #else
  30836. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  30837. #endif
  30838. /* Test default digest */
  30839. ExpectIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  30840. ExpectIntEQ(nid, NID_sha256);
  30841. EC_KEY_free(ec_key);
  30842. ec_key = NULL;
  30843. EVP_PKEY_free(pkey3);
  30844. pkey3 = NULL;
  30845. EVP_PKEY_free(pkey2);
  30846. pkey2 = NULL;
  30847. EVP_PKEY_free(pkey);
  30848. pkey = NULL;
  30849. /* Qt unit test case ec pkcs8 key */
  30850. ExpectTrue((file = XFOPEN(fname_ecc_p8, "rb")) != XBADFILE);
  30851. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30852. ExpectIntGT(sz = XFTELL(file), 0);
  30853. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30854. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30855. if (buf) {
  30856. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30857. }
  30858. if (file != XBADFILE) {
  30859. XFCLOSE(file);
  30860. file = XBADFILE;
  30861. }
  30862. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30863. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30864. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30865. buf = NULL;
  30866. BIO_free(bio);
  30867. bio = NULL;
  30868. ExpectNotNull(pkey3 = EVP_PKEY_new());
  30869. /* Qt unit test case */
  30870. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  30871. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  30872. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30873. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  30874. #else
  30875. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  30876. #endif
  30877. EC_KEY_free(ec_key);
  30878. EVP_PKEY_free(pkey3);
  30879. EVP_PKEY_free(pkey);
  30880. pkey = NULL;
  30881. #endif
  30882. return EXPECT_RESULT();
  30883. }
  30884. /* test loading DSA key using BIO */
  30885. static int test_wolfSSL_PEM_PrivateKey_dsa(void)
  30886. {
  30887. EXPECT_DECLS;
  30888. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_DSA) && \
  30889. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  30890. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
  30891. BIO* bio = NULL;
  30892. EVP_PKEY* pkey = NULL;
  30893. ExpectNotNull(bio = BIO_new_file("./certs/dsa2048.pem", "rb"));
  30894. /* Private DSA EVP_PKEY */
  30895. ExpectNotNull(pkey = wolfSSL_PEM_read_bio_PrivateKey(bio, NULL, NULL,
  30896. NULL));
  30897. BIO_free(bio);
  30898. bio = NULL;
  30899. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30900. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  30901. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  30902. NULL), 0);
  30903. #endif
  30904. #ifdef WOLFSSL_KEY_GEN
  30905. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), 1);
  30906. ExpectIntEQ(BIO_pending(bio), 1178);
  30907. BIO_reset(bio);
  30908. #endif
  30909. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30910. 1);
  30911. ExpectIntEQ(BIO_pending(bio), 1196);
  30912. BIO_free(bio);
  30913. bio = NULL;
  30914. EVP_PKEY_free(pkey);
  30915. pkey = NULL;
  30916. #endif
  30917. #endif
  30918. return EXPECT_RESULT();
  30919. }
  30920. /* test loading DH key using BIO */
  30921. static int test_wolfSSL_PEM_PrivateKey_dh(void)
  30922. {
  30923. EXPECT_DECLS;
  30924. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_DH) && \
  30925. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  30926. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  30927. defined(WOLFSSL_OPENSSH)) && (!defined(HAVE_FIPS) || \
  30928. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  30929. BIO* bio = NULL;
  30930. EVP_PKEY* pkey = NULL;
  30931. ExpectNotNull(bio = BIO_new_file("./certs/dh-priv-2048.pem", "rb"));
  30932. /* Private DH EVP_PKEY */
  30933. ExpectNotNull(pkey = wolfSSL_PEM_read_bio_PrivateKey(bio, NULL, NULL,
  30934. NULL));
  30935. BIO_free(bio);
  30936. bio = NULL;
  30937. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30938. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  30939. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  30940. NULL), 0);
  30941. #endif
  30942. #ifdef WOLFSSL_KEY_GEN
  30943. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), 0);
  30944. #endif
  30945. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30946. 1);
  30947. ExpectIntEQ(BIO_pending(bio), 806);
  30948. BIO_free(bio);
  30949. bio = NULL;
  30950. EVP_PKEY_free(pkey);
  30951. pkey = NULL;
  30952. #endif
  30953. #endif
  30954. return EXPECT_RESULT();
  30955. }
  30956. static int test_wolfSSL_PEM_PrivateKey(void)
  30957. {
  30958. EXPECT_DECLS;
  30959. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30960. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  30961. #ifndef NO_BIO
  30962. BIO* bio = NULL;
  30963. #endif
  30964. EVP_PKEY* pkey = NULL;
  30965. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  30966. #ifndef NO_BIO
  30967. /* test creating new EVP_PKEY with bad arg */
  30968. ExpectNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  30969. /* Test bad EVP_PKEY type. */
  30970. /* New HMAC EVP_PKEY */
  30971. ExpectNotNull(bio = BIO_new_mem_buf("", 1));
  30972. ExpectNotNull(pkey = EVP_PKEY_new());
  30973. if (pkey != NULL) {
  30974. pkey->type = EVP_PKEY_HMAC;
  30975. }
  30976. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30977. 0);
  30978. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  30979. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  30980. NULL), 0);
  30981. #endif
  30982. #ifdef WOLFSSL_KEY_GEN
  30983. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), WOLFSSL_FAILURE);
  30984. #endif
  30985. EVP_PKEY_free(pkey);
  30986. pkey = NULL;
  30987. BIO_free(bio);
  30988. bio = NULL;
  30989. /* key is DES encrypted */
  30990. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  30991. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  30992. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  30993. {
  30994. XFILE f = XBADFILE;
  30995. wc_pem_password_cb* passwd_cb = NULL;
  30996. void* passwd_cb_userdata;
  30997. SSL_CTX* ctx = NULL;
  30998. char passwd[] = "bad password";
  30999. #ifndef WOLFSSL_NO_TLS12
  31000. #ifndef NO_WOLFSSL_SERVER
  31001. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  31002. #else
  31003. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  31004. #endif
  31005. #else
  31006. #ifndef NO_WOLFSSL_SERVER
  31007. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  31008. #else
  31009. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  31010. #endif
  31011. #endif
  31012. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  31013. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  31014. ExpectNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  31015. ExpectNull(passwd_cb_userdata =
  31016. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  31017. /* fail case with password call back */
  31018. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  31019. (void*)passwd));
  31020. BIO_free(bio);
  31021. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  31022. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  31023. (void*)passwd));
  31024. BIO_free(bio);
  31025. ExpectTrue((f = XFOPEN("./certs/server-keyEnc.pem", "rb")) != XBADFILE);
  31026. ExpectNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  31027. if ((bio == NULL) && (f != XBADFILE)) {
  31028. XFCLOSE(f);
  31029. }
  31030. /* use callback that works */
  31031. ExpectNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  31032. (void*)"yassl123"));
  31033. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  31034. EVP_PKEY_free(pkey);
  31035. pkey = NULL;
  31036. BIO_free(bio);
  31037. bio = NULL;
  31038. SSL_CTX_free(ctx);
  31039. }
  31040. #endif /* !defined(NO_DES3) */
  31041. #endif /* !NO_BIO */
  31042. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  31043. {
  31044. unsigned char buf[2048];
  31045. size_t bytes = 0;
  31046. XFILE f = XBADFILE;
  31047. SSL_CTX* ctx = NULL;
  31048. #ifndef WOLFSSL_NO_TLS12
  31049. #ifndef NO_WOLFSSL_SERVER
  31050. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  31051. #else
  31052. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  31053. #endif
  31054. #else
  31055. #ifndef NO_WOLFSSL_SERVER
  31056. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  31057. #else
  31058. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  31059. #endif
  31060. #endif
  31061. ExpectTrue((f = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  31062. ExpectIntGT(bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f), 0);
  31063. if (f != XBADFILE)
  31064. XFCLOSE(f);
  31065. server_key = buf;
  31066. pkey = NULL;
  31067. ExpectNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  31068. ExpectNull(pkey);
  31069. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  31070. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  31071. EVP_PKEY_free(pkey);
  31072. pkey = NULL;
  31073. SSL_CTX_free(ctx);
  31074. server_key = NULL;
  31075. }
  31076. #endif
  31077. #ifndef NO_BIO
  31078. (void)bio;
  31079. #endif
  31080. (void)pkey;
  31081. (void)server_key;
  31082. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  31083. return EXPECT_RESULT();
  31084. }
  31085. static int test_wolfSSL_PEM_file_RSAKey(void)
  31086. {
  31087. EXPECT_DECLS;
  31088. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31089. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  31090. !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  31091. RSA* rsa = NULL;
  31092. XFILE fp = XBADFILE;
  31093. ExpectTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
  31094. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
  31095. if (fp != XBADFILE)
  31096. XFCLOSE(fp);
  31097. ExpectIntEQ(RSA_size(rsa), 256);
  31098. ExpectIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
  31099. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, NULL), WOLFSSL_FAILURE);
  31100. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, rsa), WOLFSSL_SUCCESS);
  31101. ExpectIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
  31102. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  31103. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, rsa), WOLFSSL_SUCCESS);
  31104. RSA_free(rsa);
  31105. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31106. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  31107. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  31108. return EXPECT_RESULT();
  31109. }
  31110. static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
  31111. {
  31112. EXPECT_DECLS;
  31113. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  31114. !defined(NO_FILESYSTEM) && \
  31115. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  31116. RSA* rsa = NULL;
  31117. XFILE f = NULL;
  31118. ExpectTrue((f = XFOPEN(svrKeyFile, "r")) != XBADFILE);
  31119. ExpectNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  31120. ExpectIntEQ(RSA_size(rsa), 256);
  31121. if (f != XBADFILE) {
  31122. XFCLOSE(f);
  31123. f = XBADFILE;
  31124. }
  31125. ExpectIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
  31126. NULL), WOLFSSL_FAILURE);
  31127. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  31128. NULL), WOLFSSL_FAILURE);
  31129. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0, NULL, NULL),
  31130. WOLFSSL_SUCCESS);
  31131. RSA_free(rsa);
  31132. #ifdef HAVE_ECC
  31133. ExpectTrue((f = XFOPEN(eccKeyFile, "r")) != XBADFILE);
  31134. ExpectNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  31135. if (f != XBADFILE)
  31136. XFCLOSE(f);
  31137. #endif /* HAVE_ECC */
  31138. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31139. return EXPECT_RESULT();
  31140. }
  31141. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  31142. {
  31143. EXPECT_DECLS;
  31144. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31145. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31146. XFILE file = XBADFILE;
  31147. const char* fname = "./certs/client-keyPub.pem";
  31148. RSA *rsa = NULL;
  31149. ExpectNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
  31150. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  31151. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  31152. ExpectIntEQ(RSA_size(rsa), 256);
  31153. RSA_free(rsa);
  31154. if (file != XBADFILE)
  31155. XFCLOSE(file);
  31156. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31157. return EXPECT_RESULT();
  31158. }
  31159. #ifndef NO_BIO
  31160. static int test_wolfSSL_PEM_bio_RSAKey(void)
  31161. {
  31162. EXPECT_DECLS;
  31163. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31164. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  31165. !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  31166. RSA* rsa = NULL;
  31167. BIO* bio = NULL;
  31168. /* PrivateKey */
  31169. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  31170. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  31171. ExpectNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  31172. ExpectNotNull(rsa);
  31173. ExpectIntEQ(RSA_size(rsa), 256);
  31174. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  31175. NULL), WOLFSSL_FAILURE);
  31176. BIO_free(bio);
  31177. bio = NULL;
  31178. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31179. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  31180. NULL), WOLFSSL_SUCCESS);
  31181. BIO_free(bio);
  31182. bio = NULL;
  31183. RSA_free(rsa);
  31184. rsa = NULL;
  31185. /* PUBKEY */
  31186. ExpectNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  31187. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  31188. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31189. ExpectIntEQ(RSA_size(rsa), 256);
  31190. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31191. BIO_free(bio);
  31192. bio = NULL;
  31193. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31194. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  31195. BIO_free(bio);
  31196. bio = NULL;
  31197. RSA_free(rsa);
  31198. rsa = NULL;
  31199. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  31200. ExpectNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  31201. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31202. BIO_free(bio);
  31203. bio = NULL;
  31204. RSA_free(rsa);
  31205. rsa = NULL;
  31206. #ifdef HAVE_ECC
  31207. /* ensure that non-rsa keys do not work */
  31208. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  31209. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31210. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31211. BIO_free(bio);
  31212. bio = NULL;
  31213. RSA_free(rsa);
  31214. rsa = NULL;
  31215. #endif /* HAVE_ECC */
  31216. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31217. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  31218. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  31219. return EXPECT_RESULT();
  31220. }
  31221. static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
  31222. {
  31223. EXPECT_DECLS;
  31224. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31225. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31226. RSA* rsa = NULL;
  31227. RSA* rsa_dup = NULL;
  31228. BIO* bio = NULL;
  31229. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  31230. ExpectNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31231. ExpectIntEQ(RSA_size(rsa), 256);
  31232. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  31233. ExpectNull(rsa_dup = RSAPublicKey_dup(NULL));
  31234. /* Test duplicating empty key. */
  31235. ExpectNotNull(rsa_dup = RSA_new());
  31236. ExpectNull(RSAPublicKey_dup(rsa_dup));
  31237. RSA_free(rsa_dup);
  31238. rsa_dup = NULL;
  31239. ExpectNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  31240. ExpectPtrNE(rsa_dup, rsa);
  31241. #endif
  31242. /* test if valgrind complains about unreleased memory */
  31243. RSA_up_ref(rsa);
  31244. RSA_free(rsa);
  31245. BIO_free(bio);
  31246. bio = NULL;
  31247. RSA_free(rsa);
  31248. rsa = NULL;
  31249. RSA_free(rsa_dup);
  31250. rsa_dup = NULL;
  31251. #ifdef HAVE_ECC
  31252. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  31253. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31254. BIO_free(bio);
  31255. #endif /* HAVE_ECC */
  31256. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31257. return EXPECT_RESULT();
  31258. }
  31259. static int test_wolfSSL_PEM_bio_DSAKey(void)
  31260. {
  31261. EXPECT_DECLS;
  31262. #ifndef HAVE_SELFTEST
  31263. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  31264. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  31265. DSA* dsa = NULL;
  31266. BIO* bio = NULL;
  31267. /* PrivateKey */
  31268. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  31269. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  31270. ExpectNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  31271. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  31272. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  31273. NULL), WOLFSSL_FAILURE);
  31274. BIO_free(bio);
  31275. bio = NULL;
  31276. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31277. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL,
  31278. NULL), WOLFSSL_SUCCESS);
  31279. BIO_free(bio);
  31280. bio = NULL;
  31281. DSA_free(dsa);
  31282. dsa = NULL;
  31283. /* PUBKEY */
  31284. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  31285. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  31286. ExpectNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  31287. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  31288. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31289. BIO_free(bio);
  31290. bio = NULL;
  31291. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31292. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  31293. BIO_free(bio);
  31294. bio = NULL;
  31295. DSA_free(dsa);
  31296. dsa = NULL;
  31297. #ifdef HAVE_ECC
  31298. /* ensure that non-dsa keys do not work */
  31299. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  31300. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  31301. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  31302. BIO_free(bio);
  31303. bio = NULL;
  31304. DSA_free(dsa);
  31305. dsa = NULL;
  31306. #endif /* HAVE_ECC */
  31307. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  31308. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  31309. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  31310. #endif /* HAVE_SELFTEST */
  31311. return EXPECT_RESULT();
  31312. }
  31313. static int test_wolfSSL_PEM_bio_ECKey(void)
  31314. {
  31315. EXPECT_DECLS;
  31316. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31317. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  31318. EC_KEY* ec = NULL;
  31319. EC_KEY* ec2;
  31320. BIO* bio = NULL;
  31321. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  31322. unsigned char* pem = NULL;
  31323. int pLen;
  31324. #endif
  31325. static char ec_key_bad_1[] = "-----BEGIN PUBLIC KEY-----\n"
  31326. "MAA=\n"
  31327. "-----END PUBLIC KEY-----";
  31328. static char ec_priv_key_bad_1[] = "-----BEGIN EC PRIVATE KEY-----\n"
  31329. "MAA=\n"
  31330. "-----END EC PRIVATE KEY-----";
  31331. /* PrivateKey */
  31332. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  31333. ExpectNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  31334. ec2 = NULL;
  31335. ExpectNotNull((ec = PEM_read_bio_ECPrivateKey(bio, &ec2, NULL, NULL)));
  31336. ExpectIntEQ(ec == ec2, 1);
  31337. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  31338. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  31339. NULL), WOLFSSL_FAILURE);
  31340. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  31341. NULL), WOLFSSL_FAILURE);
  31342. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, ec, NULL, NULL, 0, NULL, NULL),
  31343. WOLFSSL_FAILURE);
  31344. BIO_free(bio);
  31345. bio = NULL;
  31346. /* Public key data - fail. */
  31347. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  31348. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  31349. BIO_free(bio);
  31350. bio = NULL;
  31351. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31352. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  31353. NULL), WOLFSSL_SUCCESS);
  31354. BIO_free(bio);
  31355. bio = NULL;
  31356. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, NULL, NULL, NULL, 0, NULL,
  31357. NULL),WOLFSSL_FAILURE);
  31358. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, NULL, NULL, NULL, 0, NULL, NULL),
  31359. WOLFSSL_FAILURE);
  31360. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, ec, NULL, NULL, 0, NULL, NULL),
  31361. WOLFSSL_FAILURE);
  31362. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, ec, NULL, NULL, 0, NULL, NULL),
  31363. WOLFSSL_SUCCESS);
  31364. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  31365. NULL), 0);
  31366. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  31367. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  31368. NULL), 0);
  31369. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  31370. NULL), 0);
  31371. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  31372. &pLen), 0);
  31373. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  31374. &pLen), 0);
  31375. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  31376. &pLen), 0);
  31377. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  31378. NULL), 0);
  31379. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  31380. &pLen), 1);
  31381. ExpectIntGT(pLen, 0);
  31382. XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  31383. #endif
  31384. EC_KEY_free(ec);
  31385. ec = NULL;
  31386. /* PUBKEY */
  31387. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  31388. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  31389. ec2 = NULL;
  31390. ExpectNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, &ec2, NULL, NULL)));
  31391. ExpectIntEQ(ec == ec2, 1);
  31392. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  31393. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31394. BIO_free(bio);
  31395. bio = NULL;
  31396. /* Test 0x30, 0x00 fails. */
  31397. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_key_bad_1,
  31398. sizeof(ec_key_bad_1)));
  31399. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  31400. BIO_free(bio);
  31401. bio = NULL;
  31402. /* Private key data - fail. */
  31403. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  31404. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  31405. BIO_free(bio);
  31406. bio = NULL;
  31407. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31408. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  31409. BIO_free(bio);
  31410. bio = NULL;
  31411. /* Same test as above, but with a file pointer rather than a BIO. */
  31412. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31413. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  31414. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  31415. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, ec), WOLFSSL_SUCCESS);
  31416. EC_KEY_free(ec);
  31417. ec = NULL;
  31418. #ifndef NO_RSA
  31419. /* ensure that non-ec keys do not work */
  31420. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  31421. ExpectNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  31422. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  31423. BIO_free(bio);
  31424. bio = NULL;
  31425. EC_KEY_free(ec);
  31426. ec = NULL;
  31427. #endif /* !NO_RSA */
  31428. /* Test 0x30, 0x00 fails. */
  31429. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_priv_key_bad_1,
  31430. sizeof(ec_priv_key_bad_1)));
  31431. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  31432. BIO_free(bio);
  31433. bio = NULL;
  31434. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31435. return EXPECT_RESULT();
  31436. }
  31437. static int test_wolfSSL_PEM_PUBKEY(void)
  31438. {
  31439. EXPECT_DECLS;
  31440. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  31441. BIO* bio = NULL;
  31442. EVP_PKEY* pkey = NULL;
  31443. /* test creating new EVP_PKEY with bad arg */
  31444. ExpectNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  31445. /* test loading ECC key using BIO */
  31446. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  31447. {
  31448. XFILE file = XBADFILE;
  31449. const char* fname = "./certs/ecc-client-keyPub.pem";
  31450. size_t sz = 0;
  31451. byte* buf = NULL;
  31452. EVP_PKEY* pkey2 = NULL;
  31453. EC_KEY* ec_key = NULL;
  31454. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  31455. ExpectIntEQ(XFSEEK(file, 0, XSEEK_END), 0);
  31456. ExpectIntGT(sz = XFTELL(file), 0);
  31457. ExpectIntEQ(XFSEEK(file, 0, XSEEK_SET), 0);
  31458. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  31459. if (buf != NULL) {
  31460. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  31461. }
  31462. if (file != XBADFILE) {
  31463. XFCLOSE(file);
  31464. }
  31465. /* Test using BIO new mem and loading PEM private key */
  31466. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31467. ExpectNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  31468. BIO_free(bio);
  31469. bio = NULL;
  31470. EVP_PKEY_free(pkey);
  31471. pkey = NULL;
  31472. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31473. ExpectNotNull(pkey = EVP_PKEY_new());
  31474. ExpectPtrEq(PEM_read_bio_PUBKEY(bio, &pkey, NULL, NULL), pkey);
  31475. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  31476. BIO_free(bio);
  31477. bio = NULL;
  31478. /* Qt unit test case*/
  31479. ExpectNotNull(pkey2 = EVP_PKEY_new());
  31480. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  31481. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  31482. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31483. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  31484. #else
  31485. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  31486. #endif
  31487. EC_KEY_free(ec_key);
  31488. EVP_PKEY_free(pkey2);
  31489. EVP_PKEY_free(pkey);
  31490. pkey = NULL;
  31491. }
  31492. #endif
  31493. (void)bio;
  31494. (void)pkey;
  31495. #endif
  31496. return EXPECT_RESULT();
  31497. }
  31498. #endif /* !NO_BIO */
  31499. static int test_DSA_do_sign_verify(void)
  31500. {
  31501. EXPECT_DECLS;
  31502. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  31503. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  31504. !defined(NO_DSA)
  31505. unsigned char digest[WC_SHA_DIGEST_SIZE];
  31506. DSA_SIG* sig = NULL;
  31507. DSA* dsa = NULL;
  31508. word32 bytes;
  31509. byte sigBin[DSA_SIG_SIZE];
  31510. int dsacheck;
  31511. #ifdef USE_CERT_BUFFERS_1024
  31512. byte tmp[ONEK_BUF];
  31513. XMEMSET(tmp, 0, sizeof(tmp));
  31514. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  31515. bytes = sizeof_dsa_key_der_1024;
  31516. #elif defined(USE_CERT_BUFFERS_2048)
  31517. byte tmp[TWOK_BUF];
  31518. XMEMSET(tmp, 0, sizeof(tmp));
  31519. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  31520. bytes = sizeof_dsa_key_der_2048;
  31521. #else
  31522. byte tmp[TWOK_BUF];
  31523. XFILE fp = XBADFILE;
  31524. XMEMSET(tmp, 0, sizeof(tmp));
  31525. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb") != XBADFILE);
  31526. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  31527. if (fp != XBADFILE)
  31528. XFCLOSE(fp);
  31529. #endif /* END USE_CERT_BUFFERS_1024 */
  31530. XMEMSET(digest, 202, sizeof(digest));
  31531. ExpectNotNull(dsa = DSA_new());
  31532. ExpectIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  31533. ExpectIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  31534. ExpectIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  31535. ExpectNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  31536. ExpectIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  31537. DSA_SIG_free(sig);
  31538. DSA_free(dsa);
  31539. #endif
  31540. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  31541. return EXPECT_RESULT();
  31542. }
  31543. static int test_wolfSSL_tmp_dh(void)
  31544. {
  31545. EXPECT_DECLS;
  31546. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  31547. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  31548. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31549. byte buff[6000];
  31550. char file[] = "./certs/dsaparams.pem";
  31551. XFILE f = XBADFILE;
  31552. int bytes = 0;
  31553. DSA* dsa = NULL;
  31554. DH* dh = NULL;
  31555. #if defined(WOLFSSL_DH_EXTRA) && \
  31556. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31557. DH* dh2 = NULL;
  31558. #endif
  31559. BIO* bio = NULL;
  31560. SSL* ssl = NULL;
  31561. SSL_CTX* ctx = NULL;
  31562. #ifndef NO_WOLFSSL_SERVER
  31563. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31564. #else
  31565. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31566. #endif
  31567. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31568. WOLFSSL_FILETYPE_PEM));
  31569. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  31570. WOLFSSL_FILETYPE_PEM));
  31571. ExpectNotNull(ssl = SSL_new(ctx));
  31572. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  31573. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  31574. if (f != XBADFILE)
  31575. XFCLOSE(f);
  31576. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  31577. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  31578. ExpectNotNull(dsa);
  31579. dh = wolfSSL_DSA_dup_DH(dsa);
  31580. ExpectNotNull(dh);
  31581. #if defined(WOLFSSL_DH_EXTRA) && \
  31582. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31583. ExpectNotNull(dh2 = wolfSSL_DH_dup(dh));
  31584. #endif
  31585. ExpectIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  31586. #ifndef NO_WOLFSSL_SERVER
  31587. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  31588. #else
  31589. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  31590. #endif
  31591. BIO_free(bio);
  31592. DSA_free(dsa);
  31593. DH_free(dh);
  31594. dh = NULL;
  31595. #if defined(WOLFSSL_DH_EXTRA) && \
  31596. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31597. DH_free(dh2);
  31598. dh2 = NULL;
  31599. #endif
  31600. SSL_free(ssl);
  31601. SSL_CTX_free(ctx);
  31602. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31603. #endif
  31604. return EXPECT_RESULT();
  31605. }
  31606. static int test_wolfSSL_ctrl(void)
  31607. {
  31608. EXPECT_DECLS;
  31609. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  31610. byte buff[6000];
  31611. BIO* bio = NULL;
  31612. int bytes;
  31613. BUF_MEM* ptr = NULL;
  31614. XMEMSET(buff, 0, sizeof(buff));
  31615. bytes = sizeof(buff);
  31616. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  31617. ExpectNotNull(BIO_s_socket());
  31618. ExpectIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  31619. /* needs tested after stubs filled out @TODO
  31620. SSL_ctrl
  31621. SSL_CTX_ctrl
  31622. */
  31623. BIO_free(bio);
  31624. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  31625. return EXPECT_RESULT();
  31626. }
  31627. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  31628. {
  31629. EXPECT_DECLS;
  31630. #ifdef OPENSSL_EXTRA
  31631. static const unsigned char pw[] = "password";
  31632. static const int pwSz = sizeof(pw) - 1;
  31633. size_t checkPwSz = 0;
  31634. const unsigned char* checkPw = NULL;
  31635. WOLFSSL_EVP_PKEY* key = NULL;
  31636. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  31637. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  31638. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  31639. pwSz));
  31640. if (key != NULL) {
  31641. ExpectIntEQ(key->type, EVP_PKEY_HMAC);
  31642. ExpectIntEQ(key->save_type, EVP_PKEY_HMAC);
  31643. ExpectIntEQ(key->pkey_sz, pwSz);
  31644. ExpectIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  31645. }
  31646. ExpectNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  31647. ExpectIntEQ((int)checkPwSz, pwSz);
  31648. ExpectIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  31649. wolfSSL_EVP_PKEY_free(key);
  31650. key = NULL;
  31651. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  31652. 0));
  31653. ExpectIntEQ(key->pkey_sz, 0);
  31654. if (EXPECT_SUCCESS()) {
  31655. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  31656. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  31657. }
  31658. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  31659. ExpectIntEQ((int)checkPwSz, 0);
  31660. wolfSSL_EVP_PKEY_free(key);
  31661. key = NULL;
  31662. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL,
  31663. 0));
  31664. ExpectIntEQ(key->pkey_sz, 0);
  31665. if (EXPECT_SUCCESS()) {
  31666. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  31667. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  31668. }
  31669. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  31670. ExpectIntEQ((int)checkPwSz, 0);
  31671. wolfSSL_EVP_PKEY_free(key);
  31672. key = NULL;
  31673. #endif /* OPENSSL_EXTRA */
  31674. return EXPECT_RESULT();
  31675. }
  31676. static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
  31677. {
  31678. EXPECT_DECLS;
  31679. #ifdef OPENSSL_EXTRA
  31680. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  31681. const char *priv = "ABCDEFGHIJKLMNOP";
  31682. const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
  31683. WOLFSSL_EVP_PKEY* key = NULL;
  31684. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31685. NULL, NULL, AES_128_KEY_SIZE, cipher));
  31686. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31687. NULL, (const unsigned char *)priv, 0, cipher));
  31688. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31689. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
  31690. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31691. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
  31692. wolfSSL_EVP_PKEY_free(key);
  31693. #endif /* WOLFSSL_CMAC && !NO_AES && WOLFSSL_AES_DIRECT */
  31694. #endif /* OPENSSL_EXTRA */
  31695. return EXPECT_RESULT();
  31696. }
  31697. static int test_wolfSSL_EVP_Digest(void)
  31698. {
  31699. EXPECT_DECLS;
  31700. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  31701. const char* in = "abc";
  31702. int inLen = (int)XSTRLEN(in);
  31703. byte out[WC_SHA256_DIGEST_SIZE];
  31704. unsigned int outLen;
  31705. const char* expOut =
  31706. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  31707. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  31708. "\x15\xAD";
  31709. ExpectIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen,
  31710. "SHA256", NULL), 1);
  31711. ExpectIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  31712. ExpectIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  31713. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  31714. return EXPECT_RESULT();
  31715. }
  31716. static int test_wolfSSL_EVP_Digest_all(void)
  31717. {
  31718. EXPECT_DECLS;
  31719. #ifdef OPENSSL_EXTRA
  31720. const char* digests[] = {
  31721. #ifndef NO_MD5
  31722. "MD5",
  31723. #endif
  31724. #ifndef NO_SHA
  31725. "SHA",
  31726. #endif
  31727. #ifdef WOLFSSL_SHA224
  31728. "SHA224",
  31729. #endif
  31730. #ifndef NO_SHA256
  31731. "SHA256",
  31732. #endif
  31733. #ifdef WOLFSSL_SHA384
  31734. "SHA384",
  31735. #endif
  31736. #ifdef WOLFSSL_SHA512
  31737. "SHA512",
  31738. #endif
  31739. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  31740. "SHA512_224",
  31741. #endif
  31742. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  31743. "SHA512_256",
  31744. #endif
  31745. #ifdef WOLFSSL_SHA3
  31746. #ifndef WOLFSSL_NOSHA3_224
  31747. "SHA3_224",
  31748. #endif
  31749. #ifndef WOLFSSL_NOSHA3_256
  31750. "SHA3_256",
  31751. #endif
  31752. "SHA3_384",
  31753. #ifndef WOLFSSL_NOSHA3_512
  31754. "SHA3_512",
  31755. #endif
  31756. #endif /* WOLFSSL_SHA3 */
  31757. NULL
  31758. };
  31759. const char** d;
  31760. const unsigned char in[] = "abc";
  31761. int inLen = XSTR_SIZEOF(in);
  31762. byte out[WC_MAX_DIGEST_SIZE];
  31763. unsigned int outLen;
  31764. for (d = digests; *d != NULL; d++) {
  31765. ExpectIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  31766. ExpectIntGT(outLen, 0);
  31767. ExpectIntEQ(EVP_MD_size(*d), outLen);
  31768. }
  31769. #endif
  31770. return EXPECT_RESULT();
  31771. }
  31772. static int test_wolfSSL_EVP_MD_size(void)
  31773. {
  31774. EXPECT_DECLS;
  31775. #ifdef OPENSSL_EXTRA
  31776. WOLFSSL_EVP_MD_CTX mdCtx;
  31777. #ifdef WOLFSSL_SHA3
  31778. #ifndef WOLFSSL_NOSHA3_224
  31779. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31780. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  31781. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  31782. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  31783. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31784. #endif
  31785. #ifndef WOLFSSL_NOSHA3_256
  31786. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31787. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  31788. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  31789. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  31790. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31791. #endif
  31792. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31793. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  31794. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  31795. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  31796. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31797. #ifndef WOLFSSL_NOSHA3_512
  31798. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31799. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  31800. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  31801. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  31802. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31803. #endif
  31804. #endif /* WOLFSSL_SHA3 */
  31805. #ifndef NO_SHA256
  31806. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31807. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  31808. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31809. WC_SHA256_DIGEST_SIZE);
  31810. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31811. WC_SHA256_BLOCK_SIZE);
  31812. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  31813. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  31814. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31815. #endif
  31816. #ifndef NO_MD5
  31817. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31818. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  31819. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31820. WC_MD5_DIGEST_SIZE);
  31821. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31822. WC_MD5_BLOCK_SIZE);
  31823. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  31824. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  31825. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31826. #endif
  31827. #ifdef WOLFSSL_SHA224
  31828. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31829. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  31830. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31831. WC_SHA224_DIGEST_SIZE);
  31832. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31833. WC_SHA224_BLOCK_SIZE);
  31834. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  31835. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  31836. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31837. #endif
  31838. #ifdef WOLFSSL_SHA384
  31839. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31840. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  31841. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31842. WC_SHA384_DIGEST_SIZE);
  31843. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31844. WC_SHA384_BLOCK_SIZE);
  31845. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  31846. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  31847. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31848. #endif
  31849. #ifdef WOLFSSL_SHA512
  31850. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31851. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  31852. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31853. WC_SHA512_DIGEST_SIZE);
  31854. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31855. WC_SHA512_BLOCK_SIZE);
  31856. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  31857. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  31858. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31859. #endif
  31860. #ifndef NO_SHA
  31861. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31862. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  31863. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31864. WC_SHA_DIGEST_SIZE);
  31865. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31866. WC_SHA_BLOCK_SIZE);
  31867. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  31868. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  31869. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31870. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31871. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  31872. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31873. WC_SHA_DIGEST_SIZE);
  31874. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31875. WC_SHA_BLOCK_SIZE);
  31876. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  31877. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  31878. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31879. #endif
  31880. /* error case */
  31881. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31882. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  31883. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31884. BAD_FUNC_ARG);
  31885. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  31886. /* Cleanup is valid on uninit'ed struct */
  31887. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31888. #endif /* OPENSSL_EXTRA */
  31889. return EXPECT_RESULT();
  31890. }
  31891. static int test_wolfSSL_EVP_MD_pkey_type(void)
  31892. {
  31893. EXPECT_DECLS;
  31894. #ifdef OPENSSL_EXTRA
  31895. const WOLFSSL_EVP_MD* md;
  31896. #ifndef NO_MD5
  31897. ExpectNotNull(md = EVP_md5());
  31898. ExpectIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  31899. #endif
  31900. #ifndef NO_SHA
  31901. ExpectNotNull(md = EVP_sha1());
  31902. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  31903. #endif
  31904. #ifdef WOLFSSL_SHA224
  31905. ExpectNotNull(md = EVP_sha224());
  31906. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  31907. #endif
  31908. ExpectNotNull(md = EVP_sha256());
  31909. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  31910. #ifdef WOLFSSL_SHA384
  31911. ExpectNotNull(md = EVP_sha384());
  31912. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  31913. #endif
  31914. #ifdef WOLFSSL_SHA512
  31915. ExpectNotNull(md = EVP_sha512());
  31916. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  31917. #endif
  31918. #endif
  31919. return EXPECT_RESULT();
  31920. }
  31921. #ifdef OPENSSL_EXTRA
  31922. static int test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  31923. size_t testKeySz, const char* testData, size_t testDataSz,
  31924. const byte* testResult, size_t testResultSz)
  31925. {
  31926. EXPECT_DECLS;
  31927. unsigned char check[WC_MAX_DIGEST_SIZE];
  31928. size_t checkSz = -1;
  31929. WOLFSSL_EVP_PKEY* key = NULL;
  31930. WOLFSSL_EVP_MD_CTX mdCtx;
  31931. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  31932. testKey, (int)testKeySz));
  31933. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31934. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  31935. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  31936. (unsigned int)testDataSz), 1);
  31937. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31938. ExpectIntEQ((int)checkSz, (int)testResultSz);
  31939. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31940. ExpectIntEQ((int)checkSz,(int)testResultSz);
  31941. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  31942. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31943. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  31944. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  31945. (unsigned int)testDataSz), 1);
  31946. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  31947. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31948. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31949. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  31950. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  31951. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  31952. ExpectIntEQ((int)checkSz, (int)testResultSz);
  31953. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31954. ExpectIntEQ((int)checkSz,(int)testResultSz);
  31955. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  31956. (unsigned int)testDataSz - 4), 1);
  31957. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  31958. ExpectIntEQ((int)checkSz,(int)testResultSz);
  31959. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  31960. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31961. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  31962. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  31963. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  31964. (unsigned int)testDataSz - 4), 1);
  31965. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  31966. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31967. wolfSSL_EVP_PKEY_free(key);
  31968. return EXPECT_RESULT();
  31969. }
  31970. #endif
  31971. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  31972. {
  31973. EXPECT_DECLS;
  31974. #ifdef OPENSSL_EXTRA
  31975. static const unsigned char testKey[] =
  31976. {
  31977. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  31978. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  31979. 0x0b, 0x0b, 0x0b, 0x0b
  31980. };
  31981. static const char testData[] = "Hi There";
  31982. #ifdef WOLFSSL_SHA224
  31983. static const unsigned char testResultSha224[] =
  31984. {
  31985. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  31986. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  31987. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  31988. 0x53, 0x68, 0x4b, 0x22
  31989. };
  31990. #endif
  31991. #ifndef NO_SHA256
  31992. static const unsigned char testResultSha256[] =
  31993. {
  31994. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  31995. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  31996. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  31997. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  31998. };
  31999. #endif
  32000. #ifdef WOLFSSL_SHA384
  32001. static const unsigned char testResultSha384[] =
  32002. {
  32003. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  32004. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  32005. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  32006. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  32007. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  32008. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  32009. };
  32010. #endif
  32011. #ifdef WOLFSSL_SHA512
  32012. static const unsigned char testResultSha512[] =
  32013. {
  32014. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  32015. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  32016. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  32017. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  32018. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  32019. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  32020. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  32021. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  32022. };
  32023. #endif
  32024. #ifdef WOLFSSL_SHA3
  32025. #ifndef WOLFSSL_NOSHA3_224
  32026. static const unsigned char testResultSha3_224[] =
  32027. {
  32028. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  32029. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  32030. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  32031. 0xf3, 0xc8, 0x60, 0xf7
  32032. };
  32033. #endif
  32034. #ifndef WOLFSSL_NOSHA3_256
  32035. static const unsigned char testResultSha3_256[] =
  32036. {
  32037. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  32038. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  32039. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  32040. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  32041. };
  32042. #endif
  32043. #ifndef WOLFSSL_NOSHA3_384
  32044. static const unsigned char testResultSha3_384[] =
  32045. {
  32046. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  32047. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  32048. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  32049. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  32050. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  32051. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  32052. };
  32053. #endif
  32054. #ifndef WOLFSSL_NOSHA3_512
  32055. static const unsigned char testResultSha3_512[] =
  32056. {
  32057. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  32058. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  32059. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  32060. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  32061. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  32062. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  32063. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  32064. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  32065. };
  32066. #endif
  32067. #endif
  32068. #ifndef NO_SHA256
  32069. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha256(), testKey,
  32070. sizeof(testKey), testData, XSTRLEN(testData), testResultSha256,
  32071. sizeof(testResultSha256)), TEST_SUCCESS);
  32072. #endif
  32073. #ifdef WOLFSSL_SHA224
  32074. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha224(), testKey,
  32075. sizeof(testKey), testData, XSTRLEN(testData), testResultSha224,
  32076. sizeof(testResultSha224)), TEST_SUCCESS);
  32077. #endif
  32078. #ifdef WOLFSSL_SHA384
  32079. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha384(), testKey,
  32080. sizeof(testKey), testData, XSTRLEN(testData), testResultSha384,
  32081. sizeof(testResultSha384)), TEST_SUCCESS);
  32082. #endif
  32083. #ifdef WOLFSSL_SHA512
  32084. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha512(), testKey,
  32085. sizeof(testKey), testData, XSTRLEN(testData), testResultSha512,
  32086. sizeof(testResultSha512)), TEST_SUCCESS);
  32087. #endif
  32088. #ifdef WOLFSSL_SHA3
  32089. #ifndef WOLFSSL_NOSHA3_224
  32090. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey,
  32091. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_224,
  32092. sizeof(testResultSha3_224)), TEST_SUCCESS);
  32093. #endif
  32094. #ifndef WOLFSSL_NOSHA3_256
  32095. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey,
  32096. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_256,
  32097. sizeof(testResultSha3_256)), TEST_SUCCESS);
  32098. #endif
  32099. #ifndef WOLFSSL_NOSHA3_384
  32100. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey,
  32101. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_384,
  32102. sizeof(testResultSha3_384)), TEST_SUCCESS);
  32103. #endif
  32104. #ifndef WOLFSSL_NOSHA3_512
  32105. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey,
  32106. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_512,
  32107. sizeof(testResultSha3_512)), TEST_SUCCESS);
  32108. #endif
  32109. #endif
  32110. #endif /* OPENSSL_EXTRA */
  32111. return EXPECT_RESULT();
  32112. }
  32113. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  32114. {
  32115. EXPECT_DECLS;
  32116. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  32117. WOLFSSL_EVP_PKEY* privKey = NULL;
  32118. WOLFSSL_EVP_PKEY* pubKey = NULL;
  32119. WOLFSSL_EVP_PKEY_CTX* keyCtx = NULL;
  32120. const char testData[] = "Hi There";
  32121. WOLFSSL_EVP_MD_CTX mdCtx;
  32122. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  32123. int ret;
  32124. size_t checkSz = -1;
  32125. int sz = 2048 / 8;
  32126. const unsigned char* cp;
  32127. const unsigned char* p;
  32128. unsigned char check[2048/8];
  32129. size_t i;
  32130. int paddings[] = {
  32131. RSA_PKCS1_PADDING,
  32132. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  32133. RSA_PKCS1_PSS_PADDING,
  32134. #endif
  32135. };
  32136. cp = client_key_der_2048;
  32137. ExpectNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  32138. sizeof_client_key_der_2048)));
  32139. p = client_keypub_der_2048;
  32140. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  32141. sizeof_client_keypub_der_2048)));
  32142. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32143. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  32144. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32145. NULL, privKey), 1);
  32146. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32147. (unsigned int)XSTRLEN(testData)), 1);
  32148. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32149. ExpectIntEQ((int)checkSz, sz);
  32150. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32151. ExpectIntEQ((int)checkSz,sz);
  32152. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  32153. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  32154. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy);
  32155. ExpectIntEQ(ret, 1);
  32156. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32157. ExpectIntEQ(ret, 1);
  32158. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32159. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32160. NULL, pubKey), 1);
  32161. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32162. (unsigned int)XSTRLEN(testData)),
  32163. 1);
  32164. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32165. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32166. ExpectIntEQ(ret, 1);
  32167. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32168. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32169. NULL, privKey), 1);
  32170. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  32171. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32172. ExpectIntEQ((int)checkSz, sz);
  32173. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32174. ExpectIntEQ((int)checkSz, sz);
  32175. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  32176. (unsigned int)XSTRLEN(testData) - 4), 1);
  32177. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32178. ExpectIntEQ((int)checkSz, sz);
  32179. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32180. ExpectIntEQ(ret, 1);
  32181. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32182. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32183. NULL, pubKey), 1);
  32184. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  32185. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  32186. (unsigned int)XSTRLEN(testData) - 4),
  32187. 1);
  32188. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32189. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32190. ExpectIntEQ(ret, 1);
  32191. /* Check all signing padding types */
  32192. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  32193. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32194. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  32195. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  32196. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  32197. paddings[i]), 1);
  32198. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32199. (unsigned int)XSTRLEN(testData)), 1);
  32200. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32201. ExpectIntEQ((int)checkSz, sz);
  32202. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32203. ExpectIntEQ((int)checkSz,sz);
  32204. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32205. ExpectIntEQ(ret, 1);
  32206. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32207. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  32208. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  32209. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  32210. paddings[i]), 1);
  32211. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32212. (unsigned int)XSTRLEN(testData)), 1);
  32213. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32214. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32215. ExpectIntEQ(ret, 1);
  32216. }
  32217. wolfSSL_EVP_PKEY_free(pubKey);
  32218. wolfSSL_EVP_PKEY_free(privKey);
  32219. #endif
  32220. return EXPECT_RESULT();
  32221. }
  32222. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  32223. {
  32224. EXPECT_DECLS;
  32225. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  32226. WOLFSSL_EVP_PKEY* privKey = NULL;
  32227. WOLFSSL_EVP_PKEY* pubKey = NULL;
  32228. const char testData[] = "Hi There";
  32229. WOLFSSL_EVP_MD_CTX mdCtx;
  32230. int ret;
  32231. size_t checkSz = -1;
  32232. const unsigned char* cp;
  32233. const unsigned char* p;
  32234. unsigned char check[2048/8];
  32235. cp = ecc_clikey_der_256;
  32236. ExpectNotNull(privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  32237. sizeof_ecc_clikey_der_256));
  32238. p = ecc_clikeypub_der_256;
  32239. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  32240. sizeof_ecc_clikeypub_der_256)));
  32241. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32242. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32243. NULL, privKey), 1);
  32244. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32245. (unsigned int)XSTRLEN(testData)), 1);
  32246. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32247. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32248. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32249. ExpectIntEQ(ret, 1);
  32250. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32251. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32252. NULL, pubKey), 1);
  32253. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32254. (unsigned int)XSTRLEN(testData)),
  32255. 1);
  32256. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32257. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32258. ExpectIntEQ(ret, 1);
  32259. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32260. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32261. NULL, privKey), 1);
  32262. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  32263. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32264. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32265. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  32266. (unsigned int)XSTRLEN(testData) - 4), 1);
  32267. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32268. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32269. ExpectIntEQ(ret, 1);
  32270. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32271. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32272. NULL, pubKey), 1);
  32273. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  32274. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  32275. (unsigned int)XSTRLEN(testData) - 4),
  32276. 1);
  32277. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32278. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32279. ExpectIntEQ(ret, 1);
  32280. wolfSSL_EVP_PKEY_free(pubKey);
  32281. wolfSSL_EVP_PKEY_free(privKey);
  32282. #endif
  32283. return EXPECT_RESULT();
  32284. }
  32285. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  32286. {
  32287. EXPECT_DECLS;
  32288. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32289. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  32290. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32291. char caFile[] = "./certs/client-ca.pem";
  32292. char clientFile[] = "./certs/client-cert.pem";
  32293. SSL_CTX* ctx = NULL;
  32294. X509* x509 = NULL;
  32295. BIO *bio = NULL;
  32296. X509 *cert = NULL;
  32297. X509 *ca = NULL;
  32298. STACK_OF(X509) *chain = NULL;
  32299. STACK_OF(X509) *chain2 = NULL;
  32300. #ifndef NO_WOLFSSL_SERVER
  32301. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32302. #else
  32303. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32304. #endif
  32305. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  32306. WOLFSSL_FILETYPE_PEM));
  32307. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  32308. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  32309. WOLFSSL_FILETYPE_PEM));
  32310. /* additional test of getting EVP_PKEY key size from X509
  32311. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  32312. * allowed with user RSA */
  32313. {
  32314. EVP_PKEY* pkey = NULL;
  32315. #if defined(HAVE_ECC)
  32316. X509* ecX509 = NULL;
  32317. #endif /* HAVE_ECC */
  32318. ExpectNotNull(pkey = X509_get_pubkey(x509));
  32319. /* current RSA key is 2048 bit (256 bytes) */
  32320. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  32321. EVP_PKEY_free(pkey);
  32322. pkey = NULL;
  32323. #if defined(HAVE_ECC)
  32324. #if defined(USE_CERT_BUFFERS_256)
  32325. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  32326. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  32327. SSL_FILETYPE_ASN1));
  32328. #else
  32329. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  32330. cliEccCertFile, SSL_FILETYPE_PEM));
  32331. #endif
  32332. pkey = X509_get_pubkey(ecX509);
  32333. ExpectNotNull(pkey);
  32334. /* current ECC key is 256 bit (32 bytes) */
  32335. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  32336. X509_free(ecX509);
  32337. ecX509 = NULL;
  32338. EVP_PKEY_free(pkey);
  32339. pkey = NULL;
  32340. #endif /* HAVE_ECC */
  32341. }
  32342. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  32343. if (EXPECT_SUCCESS()) {
  32344. x509 = NULL;
  32345. }
  32346. #ifdef WOLFSSL_ENCRYPTED_KEYS
  32347. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  32348. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  32349. #endif
  32350. SSL_CTX_free(ctx);
  32351. ctx = NULL;
  32352. #ifndef NO_WOLFSSL_SERVER
  32353. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32354. #else
  32355. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32356. #endif
  32357. /* Test haproxy use case */
  32358. ExpectNotNull(bio = BIO_new_file(svrCertFile, "r"));
  32359. /* Read Certificate */
  32360. ExpectNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  32361. ExpectNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  32362. ExpectNotNull(chain = sk_X509_new_null());
  32363. ExpectIntEQ(sk_X509_push(chain, ca), 1);
  32364. if (EXPECT_SUCCESS()) {
  32365. ca = NULL;
  32366. }
  32367. ExpectNotNull(chain2 = X509_chain_up_ref(chain));
  32368. ExpectNotNull(ca = sk_X509_shift(chain2));
  32369. ExpectIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  32370. ExpectIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  32371. if (EXPECT_SUCCESS()) {
  32372. ca = NULL;
  32373. }
  32374. BIO_free(bio);
  32375. X509_free(cert);
  32376. X509_free(ca);
  32377. X509_free(x509);
  32378. sk_X509_pop_free(chain, X509_free);
  32379. sk_X509_pop_free(chain2, X509_free);
  32380. SSL_CTX_free(ctx);
  32381. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32382. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32383. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  32384. return EXPECT_RESULT();
  32385. }
  32386. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  32387. static int test_wolfSSL_ERR_peek_last_error_line(void)
  32388. {
  32389. EXPECT_DECLS;
  32390. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32391. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  32392. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  32393. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  32394. callback_functions client_cb;
  32395. callback_functions server_cb;
  32396. int line = 0;
  32397. int flag = ERR_TXT_STRING;
  32398. const char* file = NULL;
  32399. const char* data = NULL;
  32400. /* create a failed connection and inspect the error */
  32401. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  32402. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  32403. client_cb.method = wolfTLSv1_1_client_method;
  32404. server_cb.method = wolfTLSv1_2_server_method;
  32405. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  32406. ExpectIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  32407. ExpectNotNull(data);
  32408. /* check clearing error state */
  32409. ERR_remove_state(0);
  32410. ExpectIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  32411. ERR_peek_last_error_line(NULL, &line);
  32412. ExpectIntEQ(line, 0);
  32413. ERR_peek_last_error_line(&file, NULL);
  32414. ExpectNull(file);
  32415. /* retry connection to fill error queue */
  32416. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  32417. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  32418. client_cb.method = wolfTLSv1_1_client_method;
  32419. server_cb.method = wolfTLSv1_2_server_method;
  32420. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  32421. /* check that error code was stored */
  32422. ExpectIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  32423. ERR_peek_last_error_line(NULL, &line);
  32424. ExpectIntNE(line, 0);
  32425. ERR_peek_last_error_line(&file, NULL);
  32426. ExpectNotNull(file);
  32427. fprintf(stderr, "\nTesting error print out\n");
  32428. ERR_print_errors_fp(stderr);
  32429. fprintf(stderr, "Done testing print out\n\n");
  32430. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  32431. * !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  32432. return EXPECT_RESULT();
  32433. }
  32434. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  32435. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32436. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32437. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  32438. {
  32439. (void) ok;
  32440. (void) ctx;
  32441. fprintf(stderr, "ENTER verify_cb\n");
  32442. return SSL_SUCCESS;
  32443. }
  32444. #endif
  32445. static int test_wolfSSL_X509_Name_canon(void)
  32446. {
  32447. EXPECT_DECLS;
  32448. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32449. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  32450. defined(WOLFSSL_CERT_GEN) && \
  32451. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  32452. const long ex_hash1 = 0x0fdb2da4;
  32453. const long ex_hash2 = 0x9f3e8c9e;
  32454. X509_NAME *name = NULL;
  32455. X509 *x509 = NULL;
  32456. XFILE file = XBADFILE;
  32457. unsigned long hash = 0;
  32458. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  32459. byte *pbuf = NULL;
  32460. word32 len = 0;
  32461. (void) ex_hash2;
  32462. ExpectTrue((file = XFOPEN(caCertFile, "rb")) != XBADFILE);
  32463. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  32464. ExpectNotNull(name = X509_get_issuer_name(x509));
  32465. /* When output buffer is NULL, should return necessary output buffer
  32466. * length.*/
  32467. ExpectIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  32468. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  32469. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  32470. hash = (((unsigned long)digest[3] << 24) |
  32471. ((unsigned long)digest[2] << 16) |
  32472. ((unsigned long)digest[1] << 8) |
  32473. ((unsigned long)digest[0]));
  32474. ExpectIntEQ(hash, ex_hash1);
  32475. if (file != XBADFILE) {
  32476. XFCLOSE(file);
  32477. file = XBADFILE;
  32478. }
  32479. X509_free(x509);
  32480. x509 = NULL;
  32481. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  32482. pbuf = NULL;
  32483. ExpectTrue((file = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  32484. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  32485. ExpectNotNull(name = X509_get_issuer_name(x509));
  32486. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  32487. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  32488. hash = (((unsigned long)digest[3] << 24) |
  32489. ((unsigned long)digest[2] << 16) |
  32490. ((unsigned long)digest[1] << 8) |
  32491. ((unsigned long)digest[0]));
  32492. ExpectIntEQ(hash, ex_hash2);
  32493. if (file != XBADFILE)
  32494. XFCLOSE(file);
  32495. X509_free(x509);
  32496. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  32497. #endif
  32498. return EXPECT_RESULT();
  32499. }
  32500. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  32501. {
  32502. EXPECT_DECLS;
  32503. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  32504. const int MAX_DIR = 4;
  32505. const char paths[][32] = {
  32506. "./certs/ed25519",
  32507. "./certs/ecc",
  32508. "./certs/crl",
  32509. "./certs/",
  32510. };
  32511. char CertCrl_path[MAX_FILENAME_SZ];
  32512. char *p;
  32513. X509_STORE* str = NULL;
  32514. X509_LOOKUP* lookup = NULL;
  32515. WOLFSSL_STACK* sk = NULL;
  32516. int len, total_len, i;
  32517. (void)sk;
  32518. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  32519. /* illegal string */
  32520. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32521. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32522. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  32523. SSL_FILETYPE_PEM,NULL), 0);
  32524. /* free store */
  32525. X509_STORE_free(str);
  32526. str = NULL;
  32527. /* short folder string */
  32528. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32529. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32530. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  32531. SSL_FILETYPE_PEM,NULL), 1);
  32532. #if defined(WOLFSSL_INT_H)
  32533. /* only available when including internal.h */
  32534. ExpectNotNull(sk = lookup->dirs->dir_entry);
  32535. #endif
  32536. /* free store */
  32537. X509_STORE_free(str);
  32538. str = NULL;
  32539. /* typical function check */
  32540. p = &CertCrl_path[0];
  32541. total_len = 0;
  32542. for (i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  32543. len = (int)XSTRLEN((const char*)&paths[i]);
  32544. total_len += len;
  32545. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  32546. p += len;
  32547. if (i != 0) *(p++) = SEPARATOR_CHAR;
  32548. }
  32549. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32550. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32551. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  32552. SSL_FILETYPE_PEM,NULL), 1);
  32553. #if defined(WOLFSSL_INT_H)
  32554. /* only available when including internal.h */
  32555. ExpectNotNull(sk = lookup->dirs->dir_entry);
  32556. #endif
  32557. X509_STORE_free(str);
  32558. #endif
  32559. return EXPECT_RESULT();
  32560. }
  32561. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  32562. {
  32563. EXPECT_DECLS;
  32564. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32565. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  32566. defined(WOLFSSL_SIGNER_DER_CERT)
  32567. X509_STORE_CTX* ctx = NULL;
  32568. X509_STORE* str = NULL;
  32569. X509_LOOKUP* lookup = NULL;
  32570. X509* cert1 = NULL;
  32571. X509* x509Ca = NULL;
  32572. X509* x509Svr = NULL;
  32573. X509* issuer = NULL;
  32574. WOLFSSL_STACK* sk = NULL;
  32575. X509_NAME* caName = NULL;
  32576. X509_NAME* issuerName = NULL;
  32577. XFILE file1 = XBADFILE;
  32578. int i;
  32579. int cert_count = 0;
  32580. int cmp;
  32581. char der[] = "certs/ca-cert.der";
  32582. #ifdef HAVE_CRL
  32583. char pem[][100] = {
  32584. "./certs/crl/crl.pem",
  32585. "./certs/crl/crl2.pem",
  32586. "./certs/crl/caEccCrl.pem",
  32587. "./certs/crl/eccCliCRL.pem",
  32588. "./certs/crl/eccSrvCRL.pem",
  32589. ""
  32590. };
  32591. #endif
  32592. ExpectTrue((file1 = XFOPEN("./certs/ca-cert.pem", "rb")) != XBADFILE);
  32593. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  32594. if (file1 != XBADFILE)
  32595. XFCLOSE(file1);
  32596. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32597. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32598. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32599. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  32600. SSL_FILETYPE_PEM,NULL), 1);
  32601. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  32602. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  32603. /* check if CA cert is loaded into the store */
  32604. for (i = 0; i < cert_count; i++) {
  32605. x509Ca = sk_X509_value(sk, i);
  32606. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  32607. }
  32608. ExpectNotNull((x509Svr =
  32609. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32610. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  32611. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  32612. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  32613. ExpectNotNull(issuer);
  32614. caName = X509_get_subject_name(x509Ca);
  32615. ExpectNotNull(caName);
  32616. issuerName = X509_get_subject_name(issuer);
  32617. ExpectNotNull(issuerName);
  32618. cmp = X509_NAME_cmp(caName, issuerName);
  32619. ExpectIntEQ(cmp, 0);
  32620. /* load der format */
  32621. X509_free(issuer);
  32622. issuer = NULL;
  32623. X509_STORE_CTX_free(ctx);
  32624. ctx = NULL;
  32625. X509_STORE_free(str);
  32626. str = NULL;
  32627. sk_X509_pop_free(sk, NULL);
  32628. sk = NULL;
  32629. X509_free(x509Svr);
  32630. x509Svr = NULL;
  32631. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32632. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32633. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  32634. SSL_FILETYPE_ASN1,NULL), 1);
  32635. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  32636. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  32637. /* check if CA cert is loaded into the store */
  32638. for (i = 0; i < cert_count; i++) {
  32639. x509Ca = sk_X509_value(sk, i);
  32640. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  32641. }
  32642. X509_STORE_free(str);
  32643. str = NULL;
  32644. sk_X509_pop_free(sk, NULL);
  32645. sk = NULL;
  32646. X509_free(cert1);
  32647. cert1 = NULL;
  32648. #ifdef HAVE_CRL
  32649. ExpectNotNull(str = wolfSSL_X509_STORE_new());
  32650. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32651. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  32652. SSL_FILETYPE_PEM,NULL), 1);
  32653. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  32654. "certs/server-revoked-cert.pem",
  32655. SSL_FILETYPE_PEM,NULL), 1);
  32656. if (str) {
  32657. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  32658. WOLFSSL_FILETYPE_PEM), 1);
  32659. /* since store hasn't yet known the revoked cert*/
  32660. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  32661. "certs/server-revoked-cert.pem",
  32662. WOLFSSL_FILETYPE_PEM), 1);
  32663. }
  32664. for (i = 0; pem[i][0] != '\0'; i++)
  32665. {
  32666. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  32667. SSL_FILETYPE_PEM, NULL), 1);
  32668. }
  32669. if (str) {
  32670. /* since store knows crl list */
  32671. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  32672. "certs/server-revoked-cert.pem",
  32673. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  32674. }
  32675. ExpectIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  32676. X509_STORE_free(str);
  32677. #endif
  32678. #endif
  32679. return EXPECT_RESULT();
  32680. }
  32681. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  32682. {
  32683. int res = TEST_SKIPPED;
  32684. #if defined(OPENSSL_EXTRA)
  32685. X509_STORE_CTX_cleanup(NULL);
  32686. X509_STORE_CTX_trusted_stack(NULL, NULL);
  32687. res = TEST_SUCCESS;
  32688. #endif
  32689. return res;
  32690. }
  32691. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  32692. {
  32693. EXPECT_DECLS;
  32694. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32695. X509_STORE_CTX* ctx = NULL;
  32696. X509_STORE* str = NULL;
  32697. X509* x509Ca = NULL;
  32698. X509* x509Svr = NULL;
  32699. X509* issuer = NULL;
  32700. X509_NAME* caName = NULL;
  32701. X509_NAME* issuerName = NULL;
  32702. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32703. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32704. ExpectNotNull((x509Ca =
  32705. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  32706. ExpectIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  32707. ExpectNotNull((x509Svr =
  32708. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32709. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  32710. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  32711. ExpectNotNull(issuer = X509_STORE_CTX_get0_current_issuer(ctx));
  32712. ExpectNotNull(caName = X509_get_subject_name(x509Ca));
  32713. ExpectNotNull(issuerName = X509_get_subject_name(issuer));
  32714. #ifdef WOLFSSL_SIGNER_DER_CERT
  32715. ExpectIntEQ(X509_NAME_cmp(caName, issuerName), 0);
  32716. #endif
  32717. X509_free(issuer);
  32718. X509_STORE_CTX_free(ctx);
  32719. X509_free(x509Svr);
  32720. X509_STORE_free(str);
  32721. X509_free(x509Ca);
  32722. #endif
  32723. return EXPECT_RESULT();
  32724. }
  32725. static int test_wolfSSL_PKCS7_certs(void)
  32726. {
  32727. EXPECT_DECLS;
  32728. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  32729. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  32730. STACK_OF(X509)* sk = NULL;
  32731. STACK_OF(X509_INFO)* info_sk = NULL;
  32732. PKCS7 *p7 = NULL;
  32733. BIO* bio = NULL;
  32734. const byte* p = NULL;
  32735. int buflen = 0;
  32736. int i;
  32737. /* Test twice. Once with d2i and once without to test
  32738. * that everything is free'd correctly. */
  32739. for (i = 0; i < 2; i++) {
  32740. ExpectNotNull(p7 = PKCS7_new());
  32741. if (p7 != NULL) {
  32742. p7->version = 1;
  32743. #ifdef NO_SHA
  32744. p7->hashOID = SHA256h;
  32745. #else
  32746. p7->hashOID = SHAh;
  32747. #endif
  32748. }
  32749. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  32750. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  32751. ExpectNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  32752. ExpectIntEQ(sk_X509_INFO_num(info_sk), 2);
  32753. ExpectNotNull(sk = sk_X509_new_null());
  32754. while (EXPECT_SUCCESS() && (sk_X509_INFO_num(info_sk) > 0)) {
  32755. X509_INFO* info = NULL;
  32756. ExpectNotNull(info = sk_X509_INFO_shift(info_sk));
  32757. ExpectIntEQ(sk_X509_push(sk, info->x509), 1);
  32758. if (EXPECT_SUCCESS() && (info != NULL)) {
  32759. info->x509 = NULL;
  32760. }
  32761. X509_INFO_free(info);
  32762. }
  32763. sk_X509_INFO_pop_free(info_sk, X509_INFO_free);
  32764. info_sk = NULL;
  32765. BIO_free(bio);
  32766. bio = NULL;
  32767. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  32768. ExpectIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  32769. if ((sk != NULL) && ((p7 == NULL) || (bio == NULL))) {
  32770. sk_X509_pop_free(sk, X509_free);
  32771. }
  32772. sk = NULL;
  32773. ExpectIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  32774. if (i == 0) {
  32775. PKCS7_free(p7);
  32776. p7 = NULL;
  32777. ExpectNotNull(d2i_PKCS7(&p7, &p, buflen));
  32778. if (p7 != NULL) {
  32779. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate
  32780. * them */
  32781. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  32782. }
  32783. /* PKCS7_free free's the certs */
  32784. ExpectNotNull(wolfSSL_PKCS7_to_stack(p7));
  32785. }
  32786. BIO_free(bio);
  32787. bio = NULL;
  32788. PKCS7_free(p7);
  32789. p7 = NULL;
  32790. }
  32791. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32792. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  32793. return EXPECT_RESULT();
  32794. }
  32795. static int test_wolfSSL_X509_STORE_CTX(void)
  32796. {
  32797. EXPECT_DECLS;
  32798. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32799. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32800. X509_STORE_CTX* ctx = NULL;
  32801. X509_STORE* str = NULL;
  32802. X509* x509 = NULL;
  32803. #ifdef OPENSSL_ALL
  32804. X509* x5092 = NULL;
  32805. STACK_OF(X509) *sk = NULL;
  32806. STACK_OF(X509) *sk2 = NULL;
  32807. STACK_OF(X509) *sk3 = NULL;
  32808. #endif
  32809. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32810. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32811. ExpectNotNull((x509 =
  32812. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32813. ExpectIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  32814. #ifdef OPENSSL_ALL
  32815. /* sk_X509_new only in OPENSSL_ALL */
  32816. sk = sk_X509_new_null();
  32817. ExpectNotNull(sk);
  32818. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  32819. #else
  32820. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  32821. #endif
  32822. ExpectIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  32823. X509_STORE_CTX_set_error(ctx, -5);
  32824. X509_STORE_CTX_set_error(NULL, -5);
  32825. X509_STORE_CTX_free(ctx);
  32826. ctx = NULL;
  32827. #ifdef OPENSSL_ALL
  32828. sk_X509_pop_free(sk, NULL);
  32829. sk = NULL;
  32830. #endif
  32831. X509_STORE_free(str);
  32832. str = NULL;
  32833. X509_free(x509);
  32834. x509 = NULL;
  32835. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32836. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  32837. X509_STORE_CTX_free(ctx);
  32838. ctx = NULL;
  32839. #ifdef OPENSSL_ALL
  32840. /* test X509_STORE_CTX_get(1)_chain */
  32841. ExpectNotNull((x509 = X509_load_certificate_file(svrCertFile,
  32842. SSL_FILETYPE_PEM)));
  32843. ExpectNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  32844. SSL_FILETYPE_PEM)));
  32845. ExpectNotNull((sk = sk_X509_new_null()));
  32846. ExpectIntEQ(sk_X509_push(sk, x509), 1);
  32847. if (EXPECT_FAIL()) {
  32848. X509_free(x509);
  32849. x509 = NULL;
  32850. }
  32851. ExpectNotNull((str = X509_STORE_new()));
  32852. ExpectNotNull((ctx = X509_STORE_CTX_new()));
  32853. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  32854. ExpectNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  32855. ExpectNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  32856. ExpectIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  32857. ExpectNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  32858. ExpectNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  32859. ExpectIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  32860. X509_STORE_CTX_free(ctx);
  32861. ctx = NULL;
  32862. X509_STORE_free(str);
  32863. str = NULL;
  32864. /* CTX certs not freed yet */
  32865. X509_free(x5092);
  32866. x5092 = NULL;
  32867. sk_X509_pop_free(sk, NULL);
  32868. sk = NULL;
  32869. /* sk3 is dup so free here */
  32870. sk_X509_pop_free(sk3, NULL);
  32871. sk3 = NULL;
  32872. #endif
  32873. /* test X509_STORE_CTX_get/set_ex_data */
  32874. {
  32875. int i = 0, tmpData = 5;
  32876. void* tmpDataRet;
  32877. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32878. #ifdef HAVE_EX_DATA
  32879. for (i = 0; i < MAX_EX_DATA; i++) {
  32880. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  32881. WOLFSSL_SUCCESS);
  32882. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  32883. ExpectNotNull(tmpDataRet);
  32884. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  32885. }
  32886. #else
  32887. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  32888. WOLFSSL_FAILURE);
  32889. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  32890. ExpectNull(tmpDataRet);
  32891. #endif
  32892. X509_STORE_CTX_free(ctx);
  32893. ctx = NULL;
  32894. }
  32895. /* test X509_STORE_get/set_ex_data */
  32896. {
  32897. int i = 0, tmpData = 99;
  32898. void* tmpDataRet;
  32899. ExpectNotNull(str = X509_STORE_new());
  32900. #ifdef HAVE_EX_DATA
  32901. for (i = 0; i < MAX_EX_DATA; i++) {
  32902. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  32903. WOLFSSL_SUCCESS);
  32904. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  32905. ExpectNotNull(tmpDataRet);
  32906. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  32907. }
  32908. #else
  32909. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  32910. WOLFSSL_FAILURE);
  32911. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  32912. ExpectNull(tmpDataRet);
  32913. #endif
  32914. X509_STORE_free(str);
  32915. str = NULL;
  32916. }
  32917. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32918. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  32919. return EXPECT_RESULT();
  32920. }
  32921. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32922. static int test_X509_STORE_untrusted_load_cert_to_stack(const char* filename,
  32923. STACK_OF(X509)* chain)
  32924. {
  32925. EXPECT_DECLS;
  32926. XFILE fp = XBADFILE;
  32927. X509* cert = NULL;
  32928. ExpectTrue((fp = XFOPEN(filename, "rb"))
  32929. != XBADFILE);
  32930. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  32931. if (fp != XBADFILE) {
  32932. XFCLOSE(fp);
  32933. fp = XBADFILE;
  32934. }
  32935. ExpectIntEQ(sk_X509_push(chain, cert), 1);
  32936. if (EXPECT_FAIL())
  32937. X509_free(cert);
  32938. return EXPECT_RESULT();
  32939. }
  32940. static int test_X509_STORE_untrusted_certs(const char** filenames, int ret,
  32941. int err, int loadCA)
  32942. {
  32943. EXPECT_DECLS;
  32944. X509_STORE_CTX* ctx = NULL;
  32945. X509_STORE* str = NULL;
  32946. XFILE fp = XBADFILE;
  32947. X509* cert = NULL;
  32948. STACK_OF(X509)* untrusted = NULL;
  32949. ExpectTrue((fp = XFOPEN("./certs/intermediate/server-int-cert.pem", "rb"))
  32950. != XBADFILE);
  32951. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  32952. if (fp != XBADFILE) {
  32953. XFCLOSE(fp);
  32954. fp = XBADFILE;
  32955. }
  32956. ExpectNotNull(str = X509_STORE_new());
  32957. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32958. ExpectNotNull(untrusted = sk_X509_new_null());
  32959. ExpectIntEQ(X509_STORE_set_flags(str, 0), 1);
  32960. if (loadCA) {
  32961. ExpectIntEQ(X509_STORE_load_locations(str, "./certs/ca-cert.pem", NULL),
  32962. 1);
  32963. }
  32964. for (; *filenames; filenames++) {
  32965. ExpectIntEQ(test_X509_STORE_untrusted_load_cert_to_stack(*filenames,
  32966. untrusted), TEST_SUCCESS);
  32967. }
  32968. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, cert, untrusted), 1);
  32969. ExpectIntEQ(X509_verify_cert(ctx), ret);
  32970. ExpectIntEQ(X509_STORE_CTX_get_error(ctx), err);
  32971. X509_free(cert);
  32972. X509_STORE_free(str);
  32973. X509_STORE_CTX_free(ctx);
  32974. sk_X509_pop_free(untrusted, NULL);
  32975. return EXPECT_RESULT();
  32976. }
  32977. #endif
  32978. static int test_X509_STORE_untrusted(void)
  32979. {
  32980. EXPECT_DECLS;
  32981. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32982. const char* untrusted1[] = {
  32983. "./certs/intermediate/ca-int2-cert.pem",
  32984. NULL
  32985. };
  32986. const char* untrusted2[] = {
  32987. "./certs/intermediate/ca-int-cert.pem",
  32988. "./certs/intermediate/ca-int2-cert.pem",
  32989. NULL
  32990. };
  32991. const char* untrusted3[] = {
  32992. "./certs/intermediate/ca-int-cert.pem",
  32993. "./certs/intermediate/ca-int2-cert.pem",
  32994. "./certs/ca-cert.pem",
  32995. NULL
  32996. };
  32997. /* Adding unrelated certs that should be ignored */
  32998. const char* untrusted4[] = {
  32999. "./certs/client-ca.pem",
  33000. "./certs/intermediate/ca-int-cert.pem",
  33001. "./certs/server-cert.pem",
  33002. "./certs/intermediate/ca-int2-cert.pem",
  33003. NULL
  33004. };
  33005. /* Only immediate issuer in untrusted chain. Fails since can't build chain
  33006. * to loaded CA. */
  33007. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted1, 0,
  33008. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 1), TEST_SUCCESS);
  33009. /* Succeeds because path to loaded CA is available. */
  33010. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted2, 1, 0, 1),
  33011. TEST_SUCCESS);
  33012. /* Fails because root CA is in the untrusted stack */
  33013. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted3, 0,
  33014. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 0), TEST_SUCCESS);
  33015. /* Succeeds because path to loaded CA is available. */
  33016. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted4, 1, 0, 1),
  33017. TEST_SUCCESS);
  33018. #endif
  33019. return EXPECT_RESULT();
  33020. }
  33021. static int test_wolfSSL_X509_STORE_set_flags(void)
  33022. {
  33023. EXPECT_DECLS;
  33024. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33025. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33026. X509_STORE* store = NULL;
  33027. X509* x509 = NULL;
  33028. ExpectNotNull((store = wolfSSL_X509_STORE_new()));
  33029. ExpectNotNull((x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  33030. WOLFSSL_FILETYPE_PEM)));
  33031. ExpectIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  33032. #ifdef HAVE_CRL
  33033. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  33034. WOLFSSL_SUCCESS);
  33035. #else
  33036. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  33037. NOT_COMPILED_IN);
  33038. #endif
  33039. wolfSSL_X509_free(x509);
  33040. wolfSSL_X509_STORE_free(store);
  33041. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  33042. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33043. return EXPECT_RESULT();
  33044. }
  33045. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  33046. {
  33047. EXPECT_DECLS;
  33048. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  33049. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  33050. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  33051. WOLFSSL_X509_STORE* store = NULL;
  33052. WOLFSSL_X509_LOOKUP* lookup = NULL;
  33053. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  33054. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  33055. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  33056. X509_FILETYPE_PEM), 1);
  33057. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  33058. X509_FILETYPE_PEM), 1);
  33059. if (store != NULL) {
  33060. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  33061. WOLFSSL_FILETYPE_PEM), 1);
  33062. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  33063. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  33064. }
  33065. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  33066. X509_FILETYPE_PEM), 1);
  33067. if (store != NULL) {
  33068. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  33069. WOLFSSL_FILETYPE_PEM), 1);
  33070. }
  33071. wolfSSL_X509_STORE_free(store);
  33072. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) &&
  33073. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33074. return EXPECT_RESULT();
  33075. }
  33076. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  33077. {
  33078. EXPECT_DECLS;
  33079. #if defined(OPENSSL_EXTRA)
  33080. WOLFSSL_X509_STORE_CTX* ctx = NULL;
  33081. time_t c_time;
  33082. ExpectNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  33083. c_time = 365*24*60*60;
  33084. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  33085. ExpectTrue((ctx->param->flags & WOLFSSL_USE_CHECK_TIME) ==
  33086. WOLFSSL_USE_CHECK_TIME);
  33087. ExpectTrue(ctx->param->check_time == c_time);
  33088. wolfSSL_X509_STORE_CTX_free(ctx);
  33089. #endif /* OPENSSL_EXTRA */
  33090. return EXPECT_RESULT();
  33091. }
  33092. static int test_wolfSSL_CTX_get0_set1_param(void)
  33093. {
  33094. EXPECT_DECLS;
  33095. #if defined(OPENSSL_EXTRA)
  33096. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33097. SSL_CTX* ctx = NULL;
  33098. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  33099. WOLFSSL_X509_VERIFY_PARAM* pvpm = NULL;
  33100. char testIPv4[] = "127.0.0.1";
  33101. char testhostName[] = "foo.hoge.com";
  33102. #ifndef NO_WOLFSSL_SERVER
  33103. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33104. #else
  33105. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33106. #endif
  33107. ExpectNull(SSL_CTX_get0_param(NULL));
  33108. ExpectNotNull(pParam = SSL_CTX_get0_param(ctx));
  33109. ExpectNotNull(pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  33110. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL));
  33111. ExpectNotNull(XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM)));
  33112. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  33113. (int)XSTRLEN(testhostName)), WOLFSSL_SUCCESS);
  33114. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4),
  33115. WOLFSSL_SUCCESS);
  33116. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  33117. ExpectIntEQ(SSL_CTX_set1_param(ctx, pvpm), 1);
  33118. ExpectIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  33119. (int)XSTRLEN(testhostName)));
  33120. ExpectIntEQ(0x01, pParam->hostFlags);
  33121. ExpectIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  33122. /* test for incorrect parameter */
  33123. ExpectIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  33124. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  33125. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  33126. SSL_CTX_free(ctx);
  33127. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  33128. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33129. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  33130. return EXPECT_RESULT();
  33131. }
  33132. static int test_wolfSSL_get0_param(void)
  33133. {
  33134. EXPECT_DECLS;
  33135. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33136. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33137. SSL_CTX* ctx = NULL;
  33138. SSL* ssl = NULL;
  33139. #ifndef NO_WOLFSSL_SERVER
  33140. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33141. #else
  33142. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33143. #endif
  33144. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33145. SSL_FILETYPE_PEM));
  33146. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33147. ExpectNotNull(ssl = SSL_new(ctx));
  33148. ExpectNotNull(SSL_get0_param(ssl));
  33149. SSL_free(ssl);
  33150. SSL_CTX_free(ctx);
  33151. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33152. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  33153. return EXPECT_RESULT();
  33154. }
  33155. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  33156. {
  33157. EXPECT_DECLS;
  33158. #if defined(OPENSSL_EXTRA)
  33159. const char host[] = "www.example.com";
  33160. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  33161. ExpectNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  33162. sizeof(WOLFSSL_X509_VERIFY_PARAM), HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  33163. if (pParam != NULL) {
  33164. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  33165. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  33166. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33167. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  33168. ExpectIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33169. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  33170. }
  33171. #endif /* OPENSSL_EXTRA */
  33172. return EXPECT_RESULT();
  33173. }
  33174. static int test_wolfSSL_set1_host(void)
  33175. {
  33176. EXPECT_DECLS;
  33177. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33178. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33179. const char host[] = "www.test_wolfSSL_set1_host.com";
  33180. const char emptyStr[] = "";
  33181. SSL_CTX* ctx = NULL;
  33182. SSL* ssl = NULL;
  33183. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  33184. #ifndef NO_WOLFSSL_SERVER
  33185. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33186. #else
  33187. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33188. #endif
  33189. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33190. SSL_FILETYPE_PEM));
  33191. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33192. ExpectNotNull(ssl = SSL_new(ctx));
  33193. pParam = SSL_get0_param(ssl);
  33194. /* we should get back host string */
  33195. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  33196. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33197. /* we should get back empty string */
  33198. ExpectIntEQ(SSL_set1_host(ssl, emptyStr), WOLFSSL_SUCCESS);
  33199. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  33200. /* we should get back host string */
  33201. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  33202. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33203. /* we should get back empty string */
  33204. ExpectIntEQ(SSL_set1_host(ssl, NULL), WOLFSSL_SUCCESS);
  33205. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  33206. SSL_free(ssl);
  33207. SSL_CTX_free(ctx);
  33208. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33209. #endif /* OPENSSL_EXTRA */
  33210. return EXPECT_RESULT();
  33211. }
  33212. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  33213. {
  33214. EXPECT_DECLS;
  33215. #if defined(OPENSSL_EXTRA)
  33216. unsigned char buf[16] = {0};
  33217. WOLFSSL_X509_VERIFY_PARAM* param = NULL;
  33218. ExpectNotNull(param = X509_VERIFY_PARAM_new());
  33219. /* test 127.0.0.1 */
  33220. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  33221. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  33222. ExpectIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  33223. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  33224. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33225. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  33226. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  33227. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  33228. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33229. ExpectIntEQ(XSTRNCMP(param->ipasc,
  33230. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  33231. /* test 2001:db8:: */
  33232. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33233. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33234. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33235. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  33236. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33237. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  33238. /* test ::1234:5678 */
  33239. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  33240. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33241. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33242. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  33243. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33244. ExpectIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  33245. /* test 2001:db8::1234:5678 */
  33246. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33247. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33248. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33249. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  33250. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33251. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  33252. sizeof(param->ipasc)), 0);
  33253. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  33254. /* 2001:db8:1::ab9:c0a8:102 */
  33255. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33256. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  33257. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  33258. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  33259. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33260. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  33261. sizeof(param->ipasc)), 0);
  33262. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  33263. #endif /* OPENSSL_EXTRA */
  33264. return EXPECT_RESULT();
  33265. }
  33266. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  33267. {
  33268. EXPECT_DECLS;
  33269. #if defined(OPENSSL_EXTRA)
  33270. X509_STORE* store = NULL;
  33271. X509_STORE_CTX* ctx = NULL;
  33272. X509_STORE_CTX* ctx_no_init = NULL;
  33273. ExpectNotNull((store = X509_STORE_new()));
  33274. ExpectNotNull(ctx = X509_STORE_CTX_new());
  33275. ExpectNotNull(ctx_no_init = X509_STORE_CTX_new());
  33276. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  33277. ExpectNull(X509_STORE_CTX_get0_store(NULL));
  33278. /* should return NULL if ctx has not bee initialized */
  33279. ExpectNull(X509_STORE_CTX_get0_store(ctx_no_init));
  33280. ExpectNotNull(X509_STORE_CTX_get0_store(ctx));
  33281. wolfSSL_X509_STORE_CTX_free(ctx);
  33282. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  33283. X509_STORE_free(store);
  33284. #endif /* OPENSSL_EXTRA */
  33285. return EXPECT_RESULT();
  33286. }
  33287. static int test_wolfSSL_CTX_set_client_CA_list(void)
  33288. {
  33289. EXPECT_DECLS;
  33290. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  33291. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  33292. WOLFSSL_CTX* ctx = NULL;
  33293. WOLFSSL* ssl = NULL;
  33294. X509_NAME* name = NULL;
  33295. STACK_OF(X509_NAME)* names = NULL;
  33296. STACK_OF(X509_NAME)* ca_list = NULL;
  33297. int names_len = 0;
  33298. int i;
  33299. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  33300. /* Send two X501 names in cert request */
  33301. names = SSL_load_client_CA_file(cliCertFile);
  33302. ExpectNotNull(names);
  33303. ca_list = SSL_load_client_CA_file(caCertFile);
  33304. ExpectNotNull(ca_list);
  33305. ExpectNotNull(name = sk_X509_NAME_value(ca_list, 0));
  33306. ExpectIntEQ(sk_X509_NAME_push(names, name), 1);
  33307. if (EXPECT_FAIL()) {
  33308. wolfSSL_X509_NAME_free(name);
  33309. name = NULL;
  33310. }
  33311. SSL_CTX_set_client_CA_list(ctx, names);
  33312. /* This should only free the stack structure */
  33313. sk_X509_NAME_free(ca_list);
  33314. ca_list = NULL;
  33315. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  33316. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  33317. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  33318. for (i = 0; i < names_len; i++) {
  33319. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  33320. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  33321. }
  33322. /* Needed to be able to create ssl object */
  33323. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33324. SSL_FILETYPE_PEM));
  33325. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33326. ExpectNotNull(ssl = wolfSSL_new(ctx));
  33327. /* load again as old names are responsibility of ctx to free*/
  33328. names = SSL_load_client_CA_file(cliCertFile);
  33329. ExpectNotNull(names);
  33330. SSL_set_client_CA_list(ssl, names);
  33331. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl));
  33332. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  33333. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  33334. for (i = 0; i < names_len; i++) {
  33335. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  33336. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  33337. }
  33338. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  33339. {
  33340. tcp_ready ready;
  33341. func_args server_args;
  33342. callback_functions server_cb;
  33343. THREAD_TYPE serverThread;
  33344. WOLFSSL* ssl_client = NULL;
  33345. WOLFSSL_CTX* ctx_client = NULL;
  33346. SOCKET_T sockfd = 0;
  33347. /* wolfSSL_get_client_CA_list() with handshake */
  33348. StartTCP();
  33349. InitTcpReady(&ready);
  33350. XMEMSET(&server_args, 0, sizeof(func_args));
  33351. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  33352. server_args.signal = &ready;
  33353. server_args.callbacks = &server_cb;
  33354. /* we are responsible for free'ing WOLFSSL_CTX */
  33355. server_cb.ctx = ctx;
  33356. server_cb.isSharedCtx = 1;
  33357. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  33358. cliCertFile, 0));
  33359. start_thread(test_server_nofail, &server_args, &serverThread);
  33360. wait_tcp_ready(&server_args);
  33361. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  33362. ExpectNotNull(ctx_client =
  33363. wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  33364. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(
  33365. ctx_client, caCertFile, 0));
  33366. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(
  33367. ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  33368. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(
  33369. ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  33370. ExpectNotNull(ssl_client = wolfSSL_new(ctx_client));
  33371. ExpectIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  33372. ExpectIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  33373. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  33374. /* We are expecting two cert names to be sent */
  33375. ExpectIntEQ(sk_X509_NAME_num(ca_list), 2);
  33376. ExpectNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  33377. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  33378. ExpectNotNull(name = sk_X509_NAME_value(ca_list, i));
  33379. ExpectIntGE(sk_X509_NAME_find(names, name), 0);
  33380. }
  33381. wolfSSL_shutdown(ssl_client);
  33382. wolfSSL_free(ssl_client);
  33383. wolfSSL_CTX_free(ctx_client);
  33384. CloseSocket(sockfd);
  33385. join_thread(serverThread);
  33386. FreeTcpReady(&ready);
  33387. }
  33388. #endif
  33389. wolfSSL_free(ssl);
  33390. wolfSSL_CTX_free(ctx);
  33391. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT &&
  33392. * !NO_BIO */
  33393. return EXPECT_RESULT();
  33394. }
  33395. static int test_wolfSSL_CTX_add_client_CA(void)
  33396. {
  33397. EXPECT_DECLS;
  33398. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  33399. !defined(NO_WOLFSSL_CLIENT)
  33400. WOLFSSL_CTX* ctx = NULL;
  33401. WOLFSSL_X509* x509 = NULL;
  33402. WOLFSSL_X509* x509_a = NULL;
  33403. STACK_OF(X509_NAME)* ca_list = NULL;
  33404. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33405. /* Add client cert */
  33406. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  33407. SSL_FILETYPE_PEM));
  33408. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509), SSL_SUCCESS);
  33409. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  33410. /* Add another client cert */
  33411. ExpectNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  33412. SSL_FILETYPE_PEM));
  33413. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  33414. /* test for incorrect parameter */
  33415. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  33416. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  33417. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  33418. X509_free(x509);
  33419. X509_free(x509_a);
  33420. SSL_CTX_free(ctx);
  33421. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  33422. return EXPECT_RESULT();
  33423. }
  33424. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  33425. defined(HAVE_IO_TESTS_DEPENDENCIES)
  33426. static THREAD_RETURN WOLFSSL_THREAD server_task_ech(void* args)
  33427. {
  33428. callback_functions* callbacks = ((func_args*)args)->callbacks;
  33429. WOLFSSL_CTX* ctx = callbacks->ctx;
  33430. WOLFSSL* ssl = NULL;
  33431. SOCKET_T sfd = 0;
  33432. SOCKET_T cfd = 0;
  33433. word16 port;
  33434. char input[1024];
  33435. int idx;
  33436. int ret, err = 0;
  33437. const char* privateName = "ech-private-name.com";
  33438. int privateNameLen = (int)XSTRLEN(privateName);
  33439. ((func_args*)args)->return_code = TEST_FAIL;
  33440. port = ((func_args*)args)->signal->port;
  33441. AssertIntEQ(WOLFSSL_SUCCESS,
  33442. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  33443. AssertIntEQ(WOLFSSL_SUCCESS,
  33444. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  33445. WOLFSSL_FILETYPE_PEM));
  33446. AssertIntEQ(WOLFSSL_SUCCESS,
  33447. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  33448. WOLFSSL_FILETYPE_PEM));
  33449. if (callbacks->ctx_ready)
  33450. callbacks->ctx_ready(ctx);
  33451. ssl = wolfSSL_new(ctx);
  33452. /* set the sni for the server */
  33453. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, privateName, privateNameLen);
  33454. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  33455. CloseSocket(sfd);
  33456. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  33457. if (callbacks->ssl_ready)
  33458. callbacks->ssl_ready(ssl);
  33459. do {
  33460. err = 0; /* Reset error */
  33461. ret = wolfSSL_accept(ssl);
  33462. if (ret != WOLFSSL_SUCCESS) {
  33463. err = wolfSSL_get_error(ssl, 0);
  33464. }
  33465. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  33466. if (ret != WOLFSSL_SUCCESS) {
  33467. char buff[WOLFSSL_MAX_ERROR_SZ];
  33468. fprintf(stderr, "error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  33469. }
  33470. else {
  33471. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  33472. input[idx] = 0;
  33473. fprintf(stderr, "Client message: %s\n", input);
  33474. }
  33475. AssertIntEQ(privateNameLen, wolfSSL_write(ssl, privateName,
  33476. privateNameLen));
  33477. ((func_args*)args)->return_code = TEST_SUCCESS;
  33478. }
  33479. if (callbacks->on_result)
  33480. callbacks->on_result(ssl);
  33481. wolfSSL_shutdown(ssl);
  33482. wolfSSL_free(ssl);
  33483. wolfSSL_CTX_free(ctx);
  33484. CloseSocket(cfd);
  33485. #ifdef FP_ECC
  33486. wc_ecc_fp_free();
  33487. #endif
  33488. WOLFSSL_RETURN_FROM_THREAD(0);
  33489. }
  33490. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  33491. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  33492. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  33493. {
  33494. AssertNotNull(ssl);
  33495. AssertNotNull(line);
  33496. XFILE fp;
  33497. const byte lf = '\n';
  33498. fp = XFOPEN("./MyKeyLog.txt", "a");
  33499. XFWRITE( line, 1, strlen(line),fp);
  33500. XFWRITE( (void*)&lf,1,1,fp);
  33501. XFFLUSH(fp);
  33502. XFCLOSE(fp);
  33503. }
  33504. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  33505. static int test_wolfSSL_CTX_set_keylog_callback(void)
  33506. {
  33507. EXPECT_DECLS;
  33508. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  33509. !defined(NO_WOLFSSL_CLIENT)
  33510. SSL_CTX* ctx = NULL;
  33511. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33512. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  33513. SSL_CTX_free(ctx);
  33514. SSL_CTX_set_keylog_callback(NULL, NULL);
  33515. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  33516. return EXPECT_RESULT();
  33517. }
  33518. static int test_wolfSSL_CTX_get_keylog_callback(void)
  33519. {
  33520. EXPECT_DECLS;
  33521. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  33522. !defined(NO_WOLFSSL_CLIENT)
  33523. SSL_CTX* ctx = NULL;
  33524. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33525. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  33526. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  33527. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  33528. SSL_CTX_set_keylog_callback(ctx, NULL );
  33529. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  33530. SSL_CTX_free(ctx);
  33531. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  33532. return EXPECT_RESULT();
  33533. }
  33534. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  33535. static int test_wolfSSL_Tls12_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  33536. {
  33537. /* set keylog callback */
  33538. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  33539. return TEST_SUCCESS;
  33540. }
  33541. #endif
  33542. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  33543. {
  33544. EXPECT_DECLS;
  33545. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  33546. /* This test is intended for checking whether keylog callback is called
  33547. * in client during TLS handshake between the client and a server.
  33548. */
  33549. test_ssl_cbf server_cbf;
  33550. test_ssl_cbf client_cbf;
  33551. XFILE fp = XBADFILE;
  33552. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  33553. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  33554. server_cbf.method = wolfTLSv1_2_server_method;
  33555. client_cbf.ctx_ready = &test_wolfSSL_Tls12_Key_Logging_client_ctx_ready;
  33556. /* clean up keylog file */
  33557. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  33558. if (fp != XBADFILE) {
  33559. XFFLUSH(fp);
  33560. XFCLOSE(fp);
  33561. fp = XBADFILE;
  33562. }
  33563. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33564. &server_cbf, NULL), TEST_SUCCESS);
  33565. XSLEEP_MS(100);
  33566. /* check if the keylog file exists */
  33567. char buff[300] = {0};
  33568. int found = 0;
  33569. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  33570. XFFLUSH(fp); /* Just to make sure any buffers get flushed */
  33571. while (EXPECT_SUCCESS() && XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  33572. if (0 == strncmp(buff,"CLIENT_RANDOM ", sizeof("CLIENT_RANDOM ")-1)) {
  33573. found = 1;
  33574. break;
  33575. }
  33576. }
  33577. if (fp != XBADFILE) {
  33578. XFCLOSE(fp);
  33579. }
  33580. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  33581. ExpectIntEQ(found, 1);
  33582. /* clean up */
  33583. ExpectIntEQ(rem_file("./MyKeyLog.txt"), 0);
  33584. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  33585. return EXPECT_RESULT();
  33586. }
  33587. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  33588. defined(HAVE_SECRET_CALLBACK)
  33589. static int test_wolfSSL_Tls13_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  33590. {
  33591. /* set keylog callback */
  33592. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  33593. return TEST_SUCCESS;
  33594. }
  33595. #endif
  33596. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  33597. {
  33598. EXPECT_DECLS;
  33599. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  33600. defined(HAVE_SECRET_CALLBACK)
  33601. /* This test is intended for checking whether keylog callback is called
  33602. * in client during TLS handshake between the client and a server.
  33603. */
  33604. test_ssl_cbf server_cbf;
  33605. test_ssl_cbf client_cbf;
  33606. XFILE fp = XBADFILE;
  33607. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  33608. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  33609. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  33610. client_cbf.ctx_ready = &test_wolfSSL_Tls13_Key_Logging_client_ctx_ready;
  33611. /* clean up keylog file */
  33612. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  33613. if (fp != XBADFILE) {
  33614. XFCLOSE(fp);
  33615. fp = XBADFILE;
  33616. }
  33617. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33618. &server_cbf, NULL), TEST_SUCCESS);
  33619. /* check if the keylog file exists */
  33620. {
  33621. char buff[300] = {0};
  33622. int found[4] = {0};
  33623. int numfnd = 0;
  33624. int i;
  33625. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  33626. while (EXPECT_SUCCESS() &&
  33627. XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  33628. if (0 == strncmp(buff, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  33629. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  33630. found[0] = 1;
  33631. continue;
  33632. }
  33633. else if (0 == strncmp(buff, "SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  33634. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  33635. found[1] = 1;
  33636. continue;
  33637. }
  33638. else if (0 == strncmp(buff, "CLIENT_TRAFFIC_SECRET_0 ",
  33639. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  33640. found[2] = 1;
  33641. continue;
  33642. }
  33643. else if (0 == strncmp(buff, "SERVER_TRAFFIC_SECRET_0 ",
  33644. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  33645. found[3] = 1;
  33646. continue;
  33647. }
  33648. }
  33649. if (fp != XBADFILE)
  33650. XFCLOSE(fp);
  33651. for (i = 0; i < 4; i++) {
  33652. if (found[i] != 0)
  33653. numfnd++;
  33654. }
  33655. ExpectIntEQ(numfnd, 4);
  33656. }
  33657. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  33658. return EXPECT_RESULT();
  33659. }
  33660. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  33661. defined(HAVE_IO_TESTS_DEPENDENCIES)
  33662. static int test_wolfSSL_Tls13_ECH_params(void)
  33663. {
  33664. EXPECT_DECLS;
  33665. #if !defined(NO_WOLFSSL_CLIENT)
  33666. word32 outputLen = 0;
  33667. byte testBuf[72];
  33668. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  33669. WOLFSSL *ssl = wolfSSL_new(ctx);
  33670. ExpectNotNull(ctx);
  33671. ExpectNotNull(ssl);
  33672. /* invalid ctx */
  33673. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(NULL,
  33674. "ech-public-name.com", 0, 0, 0));
  33675. /* invalid public name */
  33676. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx, NULL, 0,
  33677. 0, 0));
  33678. /* invalid algorithms */
  33679. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx,
  33680. "ech-public-name.com", 1000, 1000, 1000));
  33681. /* invalid ctx */
  33682. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(NULL, NULL,
  33683. &outputLen));
  33684. /* invalid output len */
  33685. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(ctx, NULL, NULL));
  33686. /* invalid ssl */
  33687. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(NULL,
  33688. (char*)testBuf, sizeof(testBuf)));
  33689. /* invalid configs64 */
  33690. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl, NULL,
  33691. sizeof(testBuf)));
  33692. /* invalid size */
  33693. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl,
  33694. (char*)testBuf, 0));
  33695. /* invalid ssl */
  33696. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(NULL, testBuf,
  33697. sizeof(testBuf)));
  33698. /* invalid configs */
  33699. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, NULL,
  33700. sizeof(testBuf)));
  33701. /* invalid size */
  33702. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, testBuf, 0));
  33703. /* invalid ssl */
  33704. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(NULL, NULL, &outputLen));
  33705. /* invalid size */
  33706. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(ssl, NULL, NULL));
  33707. wolfSSL_free(ssl);
  33708. wolfSSL_CTX_free(ctx);
  33709. #endif /* !NO_WOLFSSL_CLIENT */
  33710. return EXPECT_RESULT();
  33711. }
  33712. static int test_wolfSSL_Tls13_ECH(void)
  33713. {
  33714. EXPECT_DECLS;
  33715. tcp_ready ready;
  33716. func_args client_args;
  33717. func_args server_args;
  33718. THREAD_TYPE serverThread;
  33719. callback_functions server_cbf;
  33720. callback_functions client_cbf;
  33721. SOCKET_T sockfd = 0;
  33722. WOLFSSL_CTX* ctx = NULL;
  33723. WOLFSSL* ssl = NULL;
  33724. const char* publicName = "ech-public-name.com";
  33725. const char* privateName = "ech-private-name.com";
  33726. int privateNameLen = 20;
  33727. char reply[1024];
  33728. int replyLen = 0;
  33729. byte rawEchConfig[128];
  33730. word32 rawEchConfigLen = sizeof(rawEchConfig);
  33731. InitTcpReady(&ready);
  33732. ready.port = 22222;
  33733. XMEMSET(&client_args, 0, sizeof(func_args));
  33734. XMEMSET(&server_args, 0, sizeof(func_args));
  33735. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  33736. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  33737. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  33738. /* create the server context here so we can get the ech config */
  33739. ExpectNotNull(server_cbf.ctx =
  33740. wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  33741. /* generate ech config */
  33742. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(server_cbf.ctx,
  33743. publicName, 0, 0, 0));
  33744. /* get the config for the client to use */
  33745. ExpectIntEQ(WOLFSSL_SUCCESS,
  33746. wolfSSL_CTX_GetEchConfigs(server_cbf.ctx, rawEchConfig,
  33747. &rawEchConfigLen));
  33748. server_args.callbacks = &server_cbf;
  33749. server_args.signal = &ready;
  33750. /* start server task */
  33751. start_thread(server_task_ech, &server_args, &serverThread);
  33752. wait_tcp_ready(&server_args);
  33753. /* run as a TLS1.3 client */
  33754. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  33755. ExpectIntEQ(WOLFSSL_SUCCESS,
  33756. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  33757. ExpectIntEQ(WOLFSSL_SUCCESS,
  33758. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  33759. ExpectIntEQ(WOLFSSL_SUCCESS,
  33760. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  33761. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  33762. /* get connected the server task */
  33763. ExpectNotNull(ssl = wolfSSL_new(ctx));
  33764. /* set the ech configs for the client */
  33765. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, rawEchConfig,
  33766. rawEchConfigLen));
  33767. /* set the sni for the client */
  33768. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
  33769. privateName, privateNameLen));
  33770. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  33771. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  33772. ExpectIntEQ(wolfSSL_write(ssl, privateName, privateNameLen),
  33773. privateNameLen);
  33774. ExpectIntGT((replyLen = wolfSSL_read(ssl, reply, sizeof(reply))), 0);
  33775. /* add th null terminator for string compare */
  33776. reply[replyLen] = 0;
  33777. /* check that the server replied with the private name */
  33778. ExpectStrEQ(privateName, reply);
  33779. wolfSSL_free(ssl);
  33780. wolfSSL_CTX_free(ctx);
  33781. CloseSocket(sockfd);
  33782. join_thread(serverThread);
  33783. FreeTcpReady(&ready);
  33784. return EXPECT_RESULT();
  33785. }
  33786. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  33787. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  33788. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33789. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  33790. static int post_auth_version_cb(WOLFSSL* ssl)
  33791. {
  33792. EXPECT_DECLS;
  33793. /* do handshake and then test version error */
  33794. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  33795. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  33796. return EXPECT_RESULT();
  33797. }
  33798. static int post_auth_version_client_cb(WOLFSSL* ssl)
  33799. {
  33800. EXPECT_DECLS;
  33801. /* do handshake and then test version error */
  33802. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  33803. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  33804. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  33805. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  33806. /* check was added to error queue */
  33807. ExpectIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  33808. /* check the string matches expected string */
  33809. ExpectStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  33810. "WRONG_SSL_VERSION");
  33811. #endif
  33812. return EXPECT_RESULT();
  33813. }
  33814. static int post_auth_cb(WOLFSSL* ssl)
  33815. {
  33816. EXPECT_DECLS;
  33817. WOLFSSL_X509* x509 = NULL;
  33818. /* do handshake and then test version error */
  33819. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  33820. ExpectStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  33821. ExpectNull(x509 = wolfSSL_get_peer_certificate(ssl));
  33822. wolfSSL_X509_free(x509);
  33823. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  33824. return EXPECT_RESULT();
  33825. }
  33826. static int set_post_auth_cb(WOLFSSL* ssl)
  33827. {
  33828. if (!wolfSSL_is_server(ssl)) {
  33829. EXPECT_DECLS;
  33830. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  33831. return EXPECT_RESULT();
  33832. }
  33833. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  33834. return TEST_SUCCESS;
  33835. }
  33836. #endif
  33837. static int test_wolfSSL_Tls13_postauth(void)
  33838. {
  33839. EXPECT_DECLS;
  33840. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  33841. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33842. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  33843. test_ssl_cbf server_cbf;
  33844. test_ssl_cbf client_cbf;
  33845. /* test version failure doing post auth with TLS 1.2 connection */
  33846. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  33847. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  33848. server_cbf.method = wolfTLSv1_2_server_method;
  33849. server_cbf.ssl_ready = set_post_auth_cb;
  33850. server_cbf.on_result = post_auth_version_cb;
  33851. client_cbf.ssl_ready = set_post_auth_cb;
  33852. client_cbf.on_result = post_auth_version_client_cb;
  33853. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33854. &server_cbf, NULL), TEST_SUCCESS);
  33855. /* tests on post auth with TLS 1.3 */
  33856. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  33857. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  33858. server_cbf.method = wolfTLSv1_3_server_method;
  33859. server_cbf.ssl_ready = set_post_auth_cb;
  33860. client_cbf.ssl_ready = set_post_auth_cb;
  33861. server_cbf.on_result = post_auth_cb;
  33862. client_cbf.on_result = NULL;
  33863. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33864. &server_cbf, NULL), TEST_SUCCESS);
  33865. #endif
  33866. return EXPECT_RESULT();
  33867. }
  33868. static int test_wolfSSL_X509_NID(void)
  33869. {
  33870. EXPECT_DECLS;
  33871. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  33872. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  33873. int sigType;
  33874. int nameSz;
  33875. X509* cert = NULL;
  33876. EVP_PKEY* pubKeyTmp = NULL;
  33877. X509_NAME* name = NULL;
  33878. char commonName[80];
  33879. char countryName[80];
  33880. char localityName[80];
  33881. char stateName[80];
  33882. char orgName[80];
  33883. char orgUnit[80];
  33884. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  33885. /* convert cert from DER to internal WOLFSSL_X509 struct */
  33886. ExpectNotNull(cert = wolfSSL_X509_d2i_ex(&cert, client_cert_der_2048,
  33887. sizeof_client_cert_der_2048, HEAP_HINT));
  33888. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  33889. /* extract PUBLIC KEY from cert */
  33890. ExpectNotNull(pubKeyTmp = X509_get_pubkey(cert));
  33891. /* extract signatureType */
  33892. ExpectIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  33893. /* extract subjectName info */
  33894. ExpectNotNull(name = X509_get_subject_name(cert));
  33895. ExpectIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  33896. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  33897. NULL, 0)), 0);
  33898. ExpectIntEQ(nameSz, 15);
  33899. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  33900. commonName, sizeof(commonName))), 0);
  33901. ExpectIntEQ(nameSz, 15);
  33902. ExpectIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  33903. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  33904. commonName, 9)), 0);
  33905. ExpectIntEQ(nameSz, 8);
  33906. ExpectIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  33907. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  33908. countryName, sizeof(countryName))), 0);
  33909. ExpectIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  33910. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  33911. localityName, sizeof(localityName))), 0);
  33912. ExpectIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  33913. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  33914. NID_stateOrProvinceName, stateName, sizeof(stateName))), 0);
  33915. ExpectIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  33916. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  33917. orgName, sizeof(orgName))), 0);
  33918. ExpectIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  33919. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  33920. NID_organizationalUnitName, orgUnit, sizeof(orgUnit))), 0);
  33921. ExpectIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  33922. EVP_PKEY_free(pubKeyTmp);
  33923. X509_free(cert);
  33924. #endif
  33925. return EXPECT_RESULT();
  33926. }
  33927. static int test_wolfSSL_CTX_set_srp_username(void)
  33928. {
  33929. EXPECT_DECLS;
  33930. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  33931. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  33932. WOLFSSL_CTX* ctx = NULL;
  33933. WOLFSSL* ssl = NULL;
  33934. const char *username = "TESTUSER";
  33935. const char *password = "TESTPASSWORD";
  33936. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33937. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  33938. SSL_SUCCESS);
  33939. wolfSSL_CTX_free(ctx);
  33940. ctx = NULL;
  33941. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33942. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  33943. SSL_SUCCESS);
  33944. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  33945. SSL_SUCCESS);
  33946. ExpectNotNull(ssl = SSL_new(ctx));
  33947. ExpectNotNull(SSL_get_srp_username(ssl));
  33948. ExpectStrEQ(SSL_get_srp_username(ssl), username);
  33949. wolfSSL_free(ssl);
  33950. wolfSSL_CTX_free(ctx);
  33951. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  33952. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  33953. return EXPECT_RESULT();
  33954. }
  33955. static int test_wolfSSL_CTX_set_srp_password(void)
  33956. {
  33957. EXPECT_DECLS;
  33958. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) && \
  33959. !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  33960. WOLFSSL_CTX* ctx = NULL;
  33961. const char *username = "TESTUSER";
  33962. const char *password = "TESTPASSWORD";
  33963. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33964. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  33965. SSL_SUCCESS);
  33966. wolfSSL_CTX_free(ctx);
  33967. ctx = NULL;
  33968. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  33969. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  33970. SSL_SUCCESS);
  33971. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  33972. SSL_SUCCESS);
  33973. wolfSSL_CTX_free(ctx);
  33974. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  33975. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  33976. return EXPECT_RESULT();
  33977. }
  33978. static int test_wolfSSL_X509_STORE(void)
  33979. {
  33980. EXPECT_DECLS;
  33981. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33982. X509_STORE *store = NULL;
  33983. #ifdef HAVE_CRL
  33984. X509_STORE_CTX *storeCtx = NULL;
  33985. X509_CRL *crl = NULL;
  33986. X509 *ca = NULL;
  33987. X509 *cert = NULL;
  33988. const char crlPem[] = "./certs/crl/crl.revoked";
  33989. const char srvCert[] = "./certs/server-revoked-cert.pem";
  33990. const char caCert[] = "./certs/ca-cert.pem";
  33991. XFILE fp = XBADFILE;
  33992. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  33993. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  33994. SSL_FILETYPE_PEM)));
  33995. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  33996. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  33997. SSL_FILETYPE_PEM)));
  33998. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  33999. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  34000. ExpectIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  34001. X509_STORE_free(store);
  34002. store = NULL;
  34003. X509_STORE_CTX_free(storeCtx);
  34004. storeCtx = NULL;
  34005. X509_free(cert);
  34006. cert = NULL;
  34007. X509_free(ca);
  34008. ca = NULL;
  34009. /* should fail to verify now after adding in CRL */
  34010. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34011. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  34012. SSL_FILETYPE_PEM)));
  34013. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  34014. ExpectTrue((fp = XFOPEN(crlPem, "rb")) != XBADFILE);
  34015. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  34016. NULL, NULL));
  34017. if (fp != XBADFILE)
  34018. XFCLOSE(fp);
  34019. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  34020. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  34021. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  34022. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  34023. SSL_FILETYPE_PEM)));
  34024. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  34025. ExpectIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  34026. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx),
  34027. WOLFSSL_X509_V_ERR_CERT_REVOKED);
  34028. X509_CRL_free(crl);
  34029. crl = NULL;
  34030. X509_STORE_free(store);
  34031. store = NULL;
  34032. X509_STORE_CTX_free(storeCtx);
  34033. storeCtx = NULL;
  34034. X509_free(cert);
  34035. cert = NULL;
  34036. X509_free(ca);
  34037. ca = NULL;
  34038. #endif /* HAVE_CRL */
  34039. #ifndef WOLFCRYPT_ONLY
  34040. {
  34041. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34042. SSL_CTX* ctx = NULL;
  34043. SSL* ssl = NULL;
  34044. int i;
  34045. for (i = 0; i < 2; i++) {
  34046. #ifndef NO_WOLFSSL_SERVER
  34047. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34048. #else
  34049. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34050. #endif
  34051. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34052. SSL_CTX_set_cert_store(ctx, store);
  34053. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34054. SSL_CTX_set_cert_store(ctx, store);
  34055. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34056. ExpectIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  34057. SSL_FILETYPE_PEM), SSL_SUCCESS);
  34058. ExpectIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  34059. SSL_FILETYPE_PEM), SSL_SUCCESS);
  34060. ExpectNotNull(ssl = SSL_new(ctx));
  34061. if (i == 0) {
  34062. ExpectIntEQ(SSL_set0_verify_cert_store(ssl, store),
  34063. SSL_SUCCESS);
  34064. }
  34065. else {
  34066. ExpectIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  34067. #ifdef OPENSSL_ALL
  34068. ExpectIntEQ(SSL_CTX_set1_verify_cert_store(ctx, store), SSL_SUCCESS);
  34069. #endif
  34070. }
  34071. if (EXPECT_FAIL() || (i == 1)) {
  34072. X509_STORE_free(store);
  34073. store = NULL;
  34074. }
  34075. SSL_free(ssl);
  34076. ssl = NULL;
  34077. SSL_CTX_free(ctx);
  34078. ctx = NULL;
  34079. }
  34080. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34081. }
  34082. #endif
  34083. #endif
  34084. return EXPECT_RESULT();
  34085. }
  34086. static int test_wolfSSL_X509_STORE_load_locations(void)
  34087. {
  34088. EXPECT_DECLS;
  34089. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  34090. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  34091. SSL_CTX *ctx = NULL;
  34092. X509_STORE *store = NULL;
  34093. const char ca_file[] = "./certs/ca-cert.pem";
  34094. const char client_pem_file[] = "./certs/client-cert.pem";
  34095. const char client_der_file[] = "./certs/client-cert.der";
  34096. const char ecc_file[] = "./certs/ecc-key.pem";
  34097. const char certs_path[] = "./certs/";
  34098. const char bad_path[] = "./bad-path/";
  34099. #ifdef HAVE_CRL
  34100. const char crl_path[] = "./certs/crl/";
  34101. const char crl_file[] = "./certs/crl/crl.pem";
  34102. #endif
  34103. #ifndef NO_WOLFSSL_SERVER
  34104. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  34105. #else
  34106. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  34107. #endif
  34108. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  34109. ExpectIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL),
  34110. WOLFSSL_SUCCESS);
  34111. /* Test bad arguments */
  34112. ExpectIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL),
  34113. WOLFSSL_FAILURE);
  34114. ExpectIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  34115. ExpectIntEQ(X509_STORE_load_locations(store, client_der_file, NULL),
  34116. WOLFSSL_FAILURE);
  34117. ExpectIntEQ(X509_STORE_load_locations(store, ecc_file, NULL),
  34118. WOLFSSL_FAILURE);
  34119. ExpectIntEQ(X509_STORE_load_locations(store, NULL, bad_path),
  34120. WOLFSSL_FAILURE);
  34121. #ifdef HAVE_CRL
  34122. /* Test with CRL */
  34123. ExpectIntEQ(X509_STORE_load_locations(store, crl_file, NULL),
  34124. WOLFSSL_SUCCESS);
  34125. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crl_path),
  34126. WOLFSSL_SUCCESS);
  34127. #endif
  34128. /* Test with CA */
  34129. ExpectIntEQ(X509_STORE_load_locations(store, ca_file, NULL),
  34130. WOLFSSL_SUCCESS);
  34131. /* Test with client_cert and certs path */
  34132. ExpectIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL),
  34133. WOLFSSL_SUCCESS);
  34134. ExpectIntEQ(X509_STORE_load_locations(store, NULL, certs_path),
  34135. WOLFSSL_SUCCESS);
  34136. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  34137. /* Clear nodes */
  34138. ERR_clear_error();
  34139. #endif
  34140. SSL_CTX_free(ctx);
  34141. #endif
  34142. return EXPECT_RESULT();
  34143. }
  34144. static int test_X509_STORE_get0_objects(void)
  34145. {
  34146. EXPECT_DECLS;
  34147. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  34148. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  34149. X509_STORE *store = NULL;
  34150. X509_STORE *store_cpy = NULL;
  34151. SSL_CTX *ctx = NULL;
  34152. X509_OBJECT *obj = NULL;
  34153. STACK_OF(X509_OBJECT) *objs = NULL;
  34154. int i;
  34155. /* Setup store */
  34156. #ifndef NO_WOLFSSL_SERVER
  34157. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  34158. #else
  34159. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  34160. #endif
  34161. ExpectNotNull(store_cpy = X509_STORE_new());
  34162. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  34163. ExpectIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL),
  34164. WOLFSSL_SUCCESS);
  34165. ExpectIntEQ(X509_STORE_load_locations(store, caCertFile, NULL),
  34166. WOLFSSL_SUCCESS);
  34167. ExpectIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL),
  34168. WOLFSSL_SUCCESS);
  34169. #ifdef HAVE_CRL
  34170. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir),
  34171. WOLFSSL_SUCCESS);
  34172. #endif
  34173. /* Store ready */
  34174. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  34175. ExpectNotNull(objs = X509_STORE_get0_objects(store));
  34176. #ifdef HAVE_CRL
  34177. #ifdef WOLFSSL_SIGNER_DER_CERT
  34178. ExpectIntEQ(sk_X509_OBJECT_num(objs), 4);
  34179. #else
  34180. ExpectIntEQ(sk_X509_OBJECT_num(objs), 1);
  34181. #endif
  34182. #else
  34183. #ifdef WOLFSSL_SIGNER_DER_CERT
  34184. ExpectIntEQ(sk_X509_OBJECT_num(objs), 3);
  34185. #else
  34186. ExpectIntEQ(sk_X509_OBJECT_num(objs), 0);
  34187. #endif
  34188. #endif
  34189. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  34190. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  34191. switch (X509_OBJECT_get_type(obj)) {
  34192. case X509_LU_X509:
  34193. {
  34194. WOLFSSL_X509* x509;
  34195. ExpectNotNull(x509 = X509_OBJECT_get0_X509(obj));
  34196. ExpectIntEQ(X509_STORE_add_cert(store_cpy, x509), WOLFSSL_SUCCESS);
  34197. break;
  34198. }
  34199. case X509_LU_CRL:
  34200. #ifdef HAVE_CRL
  34201. {
  34202. WOLFSSL_CRL* crl = NULL;
  34203. ExpectNotNull(crl = X509_OBJECT_get0_X509_CRL(obj));
  34204. ExpectIntEQ(X509_STORE_add_crl(store_cpy, crl), WOLFSSL_SUCCESS);
  34205. break;
  34206. }
  34207. #endif
  34208. case X509_LU_NONE:
  34209. default:
  34210. Fail(("X509_OBJECT_get_type should return x509 or crl "
  34211. "(when built with crl support)"),
  34212. ("Unrecognized X509_OBJECT type or none"));
  34213. }
  34214. }
  34215. X509_STORE_free(store_cpy);
  34216. SSL_CTX_free(ctx);
  34217. #endif
  34218. return EXPECT_RESULT();
  34219. }
  34220. static int test_wolfSSL_BN_CTX(void)
  34221. {
  34222. EXPECT_DECLS;
  34223. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34224. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34225. WOLFSSL_BN_CTX* bn_ctx = NULL;
  34226. WOLFSSL_BIGNUM* t = NULL;
  34227. ExpectNotNull(bn_ctx = wolfSSL_BN_CTX_new());
  34228. /* No implementation. */
  34229. BN_CTX_init(NULL);
  34230. ExpectNotNull(t = BN_CTX_get(NULL));
  34231. BN_free(t);
  34232. ExpectNotNull(t = BN_CTX_get(bn_ctx));
  34233. BN_free(t);
  34234. #ifndef NO_WOLFSSL_STUB
  34235. /* No implementation. */
  34236. BN_CTX_start(NULL);
  34237. BN_CTX_start(bn_ctx);
  34238. #endif
  34239. BN_CTX_free(NULL);
  34240. BN_CTX_free(bn_ctx);
  34241. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34242. return EXPECT_RESULT();
  34243. }
  34244. static int test_wolfSSL_BN(void)
  34245. {
  34246. EXPECT_DECLS;
  34247. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34248. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34249. BIGNUM* a = NULL;
  34250. BIGNUM* b = NULL;
  34251. BIGNUM* c = NULL;
  34252. BIGNUM* d = NULL;
  34253. BIGNUM emptyBN;
  34254. /* Setup */
  34255. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34256. /* internal not set emptyBN. */
  34257. ExpectNotNull(a = BN_new());
  34258. ExpectNotNull(b = BN_new());
  34259. ExpectNotNull(c = BN_dup(b));
  34260. ExpectNotNull(d = BN_new());
  34261. /* Invalid parameter testing. */
  34262. BN_free(NULL);
  34263. ExpectNull(BN_dup(NULL));
  34264. ExpectNull(BN_dup(&emptyBN));
  34265. ExpectNull(BN_copy(NULL, NULL));
  34266. ExpectNull(BN_copy(b, NULL));
  34267. ExpectNull(BN_copy(NULL, c));
  34268. ExpectNull(BN_copy(b, &emptyBN));
  34269. ExpectNull(BN_copy(&emptyBN, c));
  34270. BN_clear(NULL);
  34271. BN_clear(&emptyBN);
  34272. ExpectIntEQ(BN_num_bytes(NULL), 0);
  34273. ExpectIntEQ(BN_num_bytes(&emptyBN), 0);
  34274. ExpectIntEQ(BN_num_bits(NULL), 0);
  34275. ExpectIntEQ(BN_num_bits(&emptyBN), 0);
  34276. ExpectIntEQ(BN_is_negative(NULL), 0);
  34277. ExpectIntEQ(BN_is_negative(&emptyBN), 0);
  34278. /* END Invalid Parameters */
  34279. ExpectIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  34280. ExpectIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  34281. ExpectIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  34282. ExpectIntEQ(BN_num_bits(a), 2);
  34283. ExpectIntEQ(BN_num_bytes(a), 1);
  34284. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  34285. defined(WOLFSSL_SP_INT_NEGATIVE))
  34286. ExpectIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  34287. ExpectIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  34288. ExpectIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  34289. ExpectIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  34290. ExpectIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  34291. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34292. {
  34293. /* Do additional tests on negative BN conversions. */
  34294. char* ret = NULL;
  34295. ASN1_INTEGER* asn1 = NULL;
  34296. BIGNUM* tmp = NULL;
  34297. /* Sanity check we have a negative BN. */
  34298. ExpectIntEQ(BN_is_negative(c), 1);
  34299. ExpectNotNull(ret = BN_bn2dec(c));
  34300. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  34301. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  34302. ret = NULL;
  34303. /* Convert to ASN1_INTEGER and back to BN. */
  34304. ExpectNotNull(asn1 = BN_to_ASN1_INTEGER(c, NULL));
  34305. ExpectNotNull(tmp = ASN1_INTEGER_to_BN(asn1, NULL));
  34306. /* After converting back BN should be negative and correct. */
  34307. ExpectIntEQ(BN_is_negative(tmp), 1);
  34308. ExpectNotNull(ret = BN_bn2dec(tmp));
  34309. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  34310. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  34311. ASN1_INTEGER_free(asn1);
  34312. BN_free(tmp);
  34313. }
  34314. #endif
  34315. ExpectIntEQ(BN_get_word(c), 4);
  34316. #endif
  34317. ExpectIntEQ(BN_set_word(a, 3), 1);
  34318. ExpectIntEQ(BN_set_word(b, 3), 1);
  34319. ExpectIntEQ(BN_set_word(c, 4), 1);
  34320. /* NULL == NULL, NULL < num, num > NULL */
  34321. ExpectIntEQ(BN_cmp(NULL, NULL), 0);
  34322. ExpectIntEQ(BN_cmp(&emptyBN, &emptyBN), 0);
  34323. ExpectIntLT(BN_cmp(NULL, b), 0);
  34324. ExpectIntLT(BN_cmp(&emptyBN, b), 0);
  34325. ExpectIntGT(BN_cmp(a, NULL), 0);
  34326. ExpectIntGT(BN_cmp(a, &emptyBN), 0);
  34327. ExpectIntEQ(BN_cmp(a, b), 0);
  34328. ExpectIntLT(BN_cmp(a, c), 0);
  34329. ExpectIntGT(BN_cmp(c, b), 0);
  34330. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  34331. ExpectIntEQ(BN_print_fp(XBADFILE, NULL), 0);
  34332. ExpectIntEQ(BN_print_fp(XBADFILE, &emptyBN), 0);
  34333. ExpectIntEQ(BN_print_fp(stderr, NULL), 0);
  34334. ExpectIntEQ(BN_print_fp(stderr, &emptyBN), 0);
  34335. ExpectIntEQ(BN_print_fp(XBADFILE, a), 0);
  34336. ExpectIntEQ(BN_print_fp(stderr, a), 1);
  34337. #endif
  34338. BN_clear(a);
  34339. BN_free(a);
  34340. BN_free(b);
  34341. BN_free(c);
  34342. BN_clear_free(d);
  34343. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34344. return EXPECT_RESULT();
  34345. }
  34346. static int test_wolfSSL_BN_init(void)
  34347. {
  34348. EXPECT_DECLS;
  34349. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34350. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34351. #if !defined(USE_INTEGER_HEAP_MATH) && !defined(HAVE_WOLF_BIGINT)
  34352. BIGNUM* ap = NULL;
  34353. BIGNUM bv;
  34354. BIGNUM cv;
  34355. BIGNUM dv;
  34356. ExpectNotNull(ap = BN_new());
  34357. BN_init(NULL);
  34358. XMEMSET(&bv, 0, sizeof(bv));
  34359. ExpectNull(BN_dup(&bv));
  34360. BN_init(&bv);
  34361. BN_init(&cv);
  34362. BN_init(&dv);
  34363. ExpectIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  34364. ExpectIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  34365. ExpectIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  34366. /* a^b mod c = */
  34367. ExpectIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  34368. ExpectIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  34369. /* check result 3^2 mod 5 */
  34370. ExpectIntEQ(BN_get_word(&dv), 4);
  34371. /* a*b mod c = */
  34372. ExpectIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  34373. ExpectIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  34374. /* check result 3*2 mod 5 */
  34375. ExpectIntEQ(BN_get_word(&dv), 1);
  34376. BN_free(ap);
  34377. #endif
  34378. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34379. return EXPECT_RESULT();
  34380. }
  34381. static int test_wolfSSL_BN_enc_dec(void)
  34382. {
  34383. EXPECT_DECLS;
  34384. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  34385. BIGNUM* a = NULL;
  34386. BIGNUM* b = NULL;
  34387. BIGNUM* c = NULL;
  34388. BIGNUM emptyBN;
  34389. char* str = NULL;
  34390. const char* emptyStr = "";
  34391. const char* numberStr = "12345";
  34392. const char* badStr = "g12345";
  34393. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34394. const char* twoStr = "2";
  34395. #endif
  34396. unsigned char binNum[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  34397. unsigned char outNum[5];
  34398. /* Setup */
  34399. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34400. ExpectNotNull(a = BN_new());
  34401. ExpectNotNull(b = BN_new());
  34402. ExpectIntEQ(BN_set_word(a, 2), 1);
  34403. /* Invalid parameters */
  34404. ExpectIntEQ(BN_bn2bin(NULL, NULL), -1);
  34405. ExpectIntEQ(BN_bn2bin(&emptyBN, NULL), -1);
  34406. ExpectIntEQ(BN_bn2bin(NULL, outNum), -1);
  34407. ExpectIntEQ(BN_bn2bin(&emptyBN, outNum), -1);
  34408. ExpectNull(BN_bn2hex(NULL));
  34409. ExpectNull(BN_bn2hex(&emptyBN));
  34410. ExpectNull(BN_bn2dec(NULL));
  34411. ExpectNull(BN_bn2dec(&emptyBN));
  34412. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), NULL));
  34413. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), a));
  34414. ExpectNull(BN_bin2bn(binNum, -1, a));
  34415. ExpectNull(BN_bin2bn(binNum, -1, NULL));
  34416. ExpectNull(BN_bin2bn(binNum, sizeof(binNum), &emptyBN));
  34417. ExpectIntEQ(BN_hex2bn(NULL, NULL), 0);
  34418. ExpectIntEQ(BN_hex2bn(NULL, numberStr), 0);
  34419. ExpectIntEQ(BN_hex2bn(&a, NULL), 0);
  34420. ExpectIntEQ(BN_hex2bn(&a, emptyStr), 0);
  34421. ExpectIntEQ(BN_hex2bn(&a, badStr), 0);
  34422. ExpectIntEQ(BN_hex2bn(&c, badStr), 0);
  34423. ExpectIntEQ(BN_dec2bn(NULL, NULL), 0);
  34424. ExpectIntEQ(BN_dec2bn(NULL, numberStr), 0);
  34425. ExpectIntEQ(BN_dec2bn(&a, NULL), 0);
  34426. ExpectIntEQ(BN_dec2bn(&a, emptyStr), 0);
  34427. ExpectIntEQ(BN_dec2bn(&a, badStr), 0);
  34428. ExpectIntEQ(BN_dec2bn(&c, badStr), 0);
  34429. ExpectIntEQ(BN_set_word(a, 2), 1);
  34430. ExpectIntEQ(BN_bn2bin(a, NULL), 1);
  34431. ExpectIntEQ(BN_bn2bin(a, outNum), 1);
  34432. ExpectNotNull(BN_bin2bn(outNum, 1, b));
  34433. ExpectIntEQ(BN_cmp(a, b), 0);
  34434. ExpectNotNull(BN_bin2bn(binNum, sizeof(binNum), b));
  34435. ExpectIntEQ(BN_cmp(a, b), -1);
  34436. ExpectNotNull(str = BN_bn2hex(a));
  34437. ExpectNotNull(BN_hex2bn(&b, str));
  34438. ExpectIntEQ(BN_cmp(a, b), 0);
  34439. ExpectNotNull(BN_hex2bn(&b, numberStr));
  34440. ExpectIntEQ(BN_cmp(a, b), -1);
  34441. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34442. str = NULL;
  34443. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34444. ExpectNotNull(str = BN_bn2dec(a));
  34445. ExpectStrEQ(str, twoStr);
  34446. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34447. str = NULL;
  34448. #ifndef NO_RSA
  34449. ExpectNotNull(str = BN_bn2dec(a));
  34450. ExpectNotNull(BN_dec2bn(&b, str));
  34451. ExpectIntEQ(BN_cmp(a, b), 0);
  34452. ExpectNotNull(BN_dec2bn(&b, numberStr));
  34453. ExpectIntEQ(BN_cmp(a, b), -1);
  34454. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34455. str = NULL;
  34456. #else
  34457. /* No implementation - fail with good parameters. */
  34458. ExpectIntEQ(BN_dec2bn(&a, numberStr), 0);
  34459. #endif
  34460. #endif
  34461. BN_free(b);
  34462. BN_free(a);
  34463. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34464. return EXPECT_RESULT();
  34465. }
  34466. static int test_wolfSSL_BN_word(void)
  34467. {
  34468. EXPECT_DECLS;
  34469. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  34470. BIGNUM* a = NULL;
  34471. BIGNUM* b = NULL;
  34472. BIGNUM* c = NULL;
  34473. BIGNUM av;
  34474. ExpectNotNull(a = BN_new());
  34475. ExpectNotNull(b = BN_new());
  34476. ExpectNotNull(c = BN_new());
  34477. XMEMSET(&av, 0, sizeof(av));
  34478. /* Invalid parameter. */
  34479. ExpectIntEQ(BN_add_word(NULL, 3), 0);
  34480. ExpectIntEQ(BN_add_word(&av, 3), 0);
  34481. ExpectIntEQ(BN_sub_word(NULL, 3), 0);
  34482. ExpectIntEQ(BN_sub_word(&av, 3), 0);
  34483. ExpectIntEQ(BN_set_word(NULL, 3), 0);
  34484. ExpectIntEQ(BN_set_word(&av, 3), 0);
  34485. ExpectIntEQ(BN_get_word(NULL), 0);
  34486. ExpectIntEQ(BN_get_word(&av), 0);
  34487. ExpectIntEQ(BN_is_word(NULL, 3), 0);
  34488. ExpectIntEQ(BN_is_word(&av, 3), 0);
  34489. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  34490. !defined(NO_DSA))
  34491. ExpectIntEQ(BN_mod_word(NULL, 3), -1);
  34492. ExpectIntEQ(BN_mod_word(&av, 3), -1);
  34493. #endif
  34494. ExpectIntEQ(BN_one(NULL), 0);
  34495. ExpectIntEQ(BN_one(&av), 0);
  34496. BN_zero(NULL);
  34497. BN_zero(&av);
  34498. ExpectIntEQ(BN_is_one(NULL), 0);
  34499. ExpectIntEQ(BN_is_one(&av), 0);
  34500. ExpectIntEQ(BN_is_zero(NULL), 0);
  34501. ExpectIntEQ(BN_is_zero(&av), 0);
  34502. ExpectIntEQ(BN_set_word(a, 3), 1);
  34503. ExpectIntEQ(BN_set_word(b, 2), 1);
  34504. ExpectIntEQ(BN_set_word(c, 5), 1);
  34505. /* a + 3 = */
  34506. ExpectIntEQ(BN_add_word(a, 3), 1);
  34507. /* check result 3 + 3*/
  34508. ExpectIntEQ(BN_get_word(a), 6);
  34509. ExpectIntEQ(BN_is_word(a, 6), 1);
  34510. ExpectIntEQ(BN_is_word(a, 5), 0);
  34511. /* set a back to 3 */
  34512. ExpectIntEQ(BN_set_word(a, 3), 1);
  34513. /* a - 3 = */
  34514. ExpectIntEQ(BN_sub_word(a, 3), 1);
  34515. /* check result 3 - 3*/
  34516. ExpectIntEQ(BN_get_word(a), 0);
  34517. ExpectIntEQ(BN_one(a), 1);
  34518. ExpectIntEQ(BN_is_word(a, 1), 1);
  34519. ExpectIntEQ(BN_is_word(a, 0), 0);
  34520. ExpectIntEQ(BN_is_one(a), 1);
  34521. ExpectIntEQ(BN_is_zero(a), 0);
  34522. BN_zero(a);
  34523. ExpectIntEQ(BN_is_word(a, 0), 1);
  34524. ExpectIntEQ(BN_is_word(a, 1), 0);
  34525. ExpectIntEQ(BN_is_zero(a), 1);
  34526. ExpectIntEQ(BN_is_one(a), 0);
  34527. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  34528. !defined(NO_DSA))
  34529. ExpectIntEQ(BN_set_word(a, 5), 1);
  34530. ExpectIntEQ(BN_mod_word(a, 3), 2);
  34531. ExpectIntEQ(BN_mod_word(a, 0), -1);
  34532. #endif
  34533. BN_free(c);
  34534. BN_free(b);
  34535. BN_free(a);
  34536. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34537. return EXPECT_RESULT();
  34538. }
  34539. static int test_wolfSSL_BN_bits(void)
  34540. {
  34541. EXPECT_DECLS;
  34542. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34543. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34544. BIGNUM* a = NULL;
  34545. BIGNUM emptyBN;
  34546. /* Setup */
  34547. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34548. ExpectNotNull(a = BN_new());
  34549. /* Invalid parameters. */
  34550. ExpectIntEQ(BN_set_bit(NULL, 1), 0);
  34551. ExpectIntEQ(BN_set_bit(&emptyBN, 1), 0);
  34552. ExpectIntEQ(BN_set_bit(a, -1), 0);
  34553. ExpectIntEQ(BN_clear_bit(NULL, 1), 0);
  34554. ExpectIntEQ(BN_clear_bit(&emptyBN, 1), 0);
  34555. ExpectIntEQ(BN_clear_bit(a, -1), 0);
  34556. ExpectIntEQ(BN_is_bit_set(NULL, 1), 0);
  34557. ExpectIntEQ(BN_is_bit_set(&emptyBN, 1), 0);
  34558. ExpectIntEQ(BN_is_bit_set(a, -1), 0);
  34559. ExpectIntEQ(BN_is_odd(NULL), 0);
  34560. ExpectIntEQ(BN_is_odd(&emptyBN), 0);
  34561. ExpectIntEQ(BN_set_word(a, 0), 1);
  34562. ExpectIntEQ(BN_is_zero(a), 1);
  34563. ExpectIntEQ(BN_set_bit(a, 0x45), 1);
  34564. ExpectIntEQ(BN_is_zero(a), 0);
  34565. ExpectIntEQ(BN_is_bit_set(a, 0x45), 1);
  34566. ExpectIntEQ(BN_clear_bit(a, 0x45), 1);
  34567. ExpectIntEQ(BN_is_bit_set(a, 0x45), 0);
  34568. ExpectIntEQ(BN_is_zero(a), 1);
  34569. ExpectIntEQ(BN_set_bit(a, 0), 1);
  34570. ExpectIntEQ(BN_is_odd(a), 1);
  34571. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  34572. ExpectIntEQ(BN_is_odd(a), 0);
  34573. ExpectIntEQ(BN_set_bit(a, 1), 1);
  34574. ExpectIntEQ(BN_is_odd(a), 0);
  34575. ExpectIntEQ(BN_set_bit(a, 129), 1);
  34576. ExpectIntEQ(BN_get_word(a), WOLFSSL_BN_MAX_VAL);
  34577. #ifndef NO_WOLFSSL_STUB
  34578. ExpectIntEQ(BN_mask_bits(a, 1), 0);
  34579. #endif
  34580. BN_free(a);
  34581. #endif
  34582. return EXPECT_RESULT();
  34583. }
  34584. static int test_wolfSSL_BN_shift(void)
  34585. {
  34586. EXPECT_DECLS;
  34587. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34588. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34589. BIGNUM* a = NULL;
  34590. BIGNUM* b = NULL;
  34591. BIGNUM emptyBN;
  34592. /* Setup */
  34593. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34594. ExpectNotNull(a = BN_new());
  34595. ExpectNotNull(b = BN_new());
  34596. /* Invalid parameters. */
  34597. ExpectIntEQ(BN_lshift(NULL, NULL, 1), 0);
  34598. ExpectIntEQ(BN_lshift(&emptyBN, NULL, 1), 0);
  34599. ExpectIntEQ(BN_lshift(NULL, &emptyBN, 1), 0);
  34600. ExpectIntEQ(BN_lshift(b, NULL, 1), 0);
  34601. ExpectIntEQ(BN_lshift(b, &emptyBN, 1), 0);
  34602. ExpectIntEQ(BN_lshift(NULL, a, 1), 0);
  34603. ExpectIntEQ(BN_lshift(&emptyBN, a, 1), 0);
  34604. ExpectIntEQ(BN_lshift(b, a, -1), 0);
  34605. ExpectIntEQ(BN_rshift(NULL, NULL, 1), 0);
  34606. ExpectIntEQ(BN_rshift(&emptyBN, NULL, 1), 0);
  34607. ExpectIntEQ(BN_rshift(NULL, &emptyBN, 1), 0);
  34608. ExpectIntEQ(BN_rshift(b, NULL, 1), 0);
  34609. ExpectIntEQ(BN_rshift(b, &emptyBN, 1), 0);
  34610. ExpectIntEQ(BN_rshift(NULL, a, 1), 0);
  34611. ExpectIntEQ(BN_rshift(&emptyBN, a, 1), 0);
  34612. ExpectIntEQ(BN_rshift(b, a, -1), 0);
  34613. ExpectIntEQ(BN_set_word(a, 1), 1);
  34614. ExpectIntEQ(BN_lshift(b, a, 1), 1);
  34615. ExpectIntEQ(BN_is_word(b, 2), 1);
  34616. ExpectIntEQ(BN_lshift(a, a, 1), 1);
  34617. ExpectIntEQ(BN_is_word(a, 2), 1);
  34618. ExpectIntEQ(BN_rshift(b, a, 1), 1);
  34619. ExpectIntEQ(BN_is_word(b, 1), 1);
  34620. ExpectIntEQ(BN_rshift(a, a, 1), 1);
  34621. ExpectIntEQ(BN_is_word(a, 1), 1);
  34622. BN_free(b);
  34623. BN_free(a);
  34624. #endif
  34625. return EXPECT_RESULT();
  34626. }
  34627. static int test_wolfSSL_BN_math(void)
  34628. {
  34629. EXPECT_DECLS;
  34630. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34631. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34632. BIGNUM* a = NULL;
  34633. BIGNUM* b = NULL;
  34634. BIGNUM* r = NULL;
  34635. BIGNUM* rem = NULL;
  34636. BIGNUM emptyBN;
  34637. BN_ULONG val1;
  34638. BN_ULONG val2;
  34639. /* Setup */
  34640. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34641. ExpectNotNull(a = BN_new());
  34642. ExpectNotNull(b = BN_new());
  34643. ExpectNotNull(r = BN_new());
  34644. ExpectNotNull(rem = BN_new());
  34645. /* Invalid parameters. */
  34646. ExpectIntEQ(BN_add(NULL, NULL, NULL), 0);
  34647. ExpectIntEQ(BN_add(r, NULL, NULL), 0);
  34648. ExpectIntEQ(BN_add(NULL, a, NULL), 0);
  34649. ExpectIntEQ(BN_add(NULL, NULL, b), 0);
  34650. ExpectIntEQ(BN_add(r, a, NULL), 0);
  34651. ExpectIntEQ(BN_add(r, NULL, b), 0);
  34652. ExpectIntEQ(BN_add(NULL, a, b), 0);
  34653. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, &emptyBN), 0);
  34654. ExpectIntEQ(BN_add(r, &emptyBN, &emptyBN), 0);
  34655. ExpectIntEQ(BN_add(&emptyBN, a, &emptyBN), 0);
  34656. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, b), 0);
  34657. ExpectIntEQ(BN_add(r, a, &emptyBN), 0);
  34658. ExpectIntEQ(BN_add(r, &emptyBN, b), 0);
  34659. ExpectIntEQ(BN_add(&emptyBN, a, b), 0);
  34660. ExpectIntEQ(BN_sub(NULL, NULL, NULL), 0);
  34661. ExpectIntEQ(BN_sub(r, NULL, NULL), 0);
  34662. ExpectIntEQ(BN_sub(NULL, a, NULL), 0);
  34663. ExpectIntEQ(BN_sub(NULL, NULL, b), 0);
  34664. ExpectIntEQ(BN_sub(r, a, NULL), 0);
  34665. ExpectIntEQ(BN_sub(r, NULL, b), 0);
  34666. ExpectIntEQ(BN_sub(NULL, a, b), 0);
  34667. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, &emptyBN), 0);
  34668. ExpectIntEQ(BN_sub(r, &emptyBN, &emptyBN), 0);
  34669. ExpectIntEQ(BN_sub(&emptyBN, a, &emptyBN), 0);
  34670. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, b), 0);
  34671. ExpectIntEQ(BN_sub(r, a, &emptyBN), 0);
  34672. ExpectIntEQ(BN_sub(r, &emptyBN, b), 0);
  34673. ExpectIntEQ(BN_sub(&emptyBN, a, b), 0);
  34674. ExpectIntEQ(BN_mul(NULL, NULL, NULL, NULL), 0);
  34675. ExpectIntEQ(BN_mul(r, NULL, NULL, NULL), 0);
  34676. ExpectIntEQ(BN_mul(NULL, a, NULL, NULL), 0);
  34677. ExpectIntEQ(BN_mul(NULL, NULL, b, NULL), 0);
  34678. ExpectIntEQ(BN_mul(r, a, NULL, NULL), 0);
  34679. ExpectIntEQ(BN_mul(r, NULL, b, NULL), 0);
  34680. ExpectIntEQ(BN_mul(NULL, a, b, NULL), 0);
  34681. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34682. ExpectIntEQ(BN_mul(r, &emptyBN, &emptyBN, NULL), 0);
  34683. ExpectIntEQ(BN_mul(&emptyBN, a, &emptyBN, NULL), 0);
  34684. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, b, NULL), 0);
  34685. ExpectIntEQ(BN_mul(r, a, &emptyBN, NULL), 0);
  34686. ExpectIntEQ(BN_mul(r, &emptyBN, b, NULL), 0);
  34687. ExpectIntEQ(BN_mul(&emptyBN, a, b, NULL), 0);
  34688. ExpectIntEQ(BN_div(NULL, NULL, NULL, NULL, NULL), 0);
  34689. ExpectIntEQ(BN_div(r, NULL, NULL, NULL, NULL), 0);
  34690. ExpectIntEQ(BN_div(NULL, rem, NULL, NULL, NULL), 0);
  34691. ExpectIntEQ(BN_div(NULL, NULL, a, NULL, NULL), 0);
  34692. ExpectIntEQ(BN_div(NULL, NULL, NULL, b, NULL), 0);
  34693. ExpectIntEQ(BN_div(NULL, rem, a, b, NULL), 0);
  34694. ExpectIntEQ(BN_div(r, NULL, a, b, NULL), 0);
  34695. ExpectIntEQ(BN_div(r, rem, NULL, b, NULL), 0);
  34696. ExpectIntEQ(BN_div(r, rem, a, NULL, NULL), 0);
  34697. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34698. ExpectIntEQ(BN_div(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34699. ExpectIntEQ(BN_div(&emptyBN, rem, &emptyBN, &emptyBN, NULL), 0);
  34700. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, a, &emptyBN, NULL), 0);
  34701. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, b, NULL), 0);
  34702. ExpectIntEQ(BN_div(&emptyBN, rem, a, b, NULL), 0);
  34703. ExpectIntEQ(BN_div(r, &emptyBN, a, b, NULL), 0);
  34704. ExpectIntEQ(BN_div(r, rem, &emptyBN, b, NULL), 0);
  34705. ExpectIntEQ(BN_div(r, rem, a, &emptyBN, NULL), 0);
  34706. ExpectIntEQ(BN_mod(NULL, NULL, NULL, NULL), 0);
  34707. ExpectIntEQ(BN_mod(r, NULL, NULL, NULL), 0);
  34708. ExpectIntEQ(BN_mod(NULL, a, NULL, NULL), 0);
  34709. ExpectIntEQ(BN_mod(NULL, NULL, b, NULL), 0);
  34710. ExpectIntEQ(BN_mod(r, a, NULL, NULL), 0);
  34711. ExpectIntEQ(BN_mod(r, NULL, b, NULL), 0);
  34712. ExpectIntEQ(BN_mod(NULL, a, b, NULL), 0);
  34713. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34714. ExpectIntEQ(BN_mod(r, &emptyBN, &emptyBN, NULL), 0);
  34715. ExpectIntEQ(BN_mod(&emptyBN, a, &emptyBN, NULL), 0);
  34716. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, b, NULL), 0);
  34717. ExpectIntEQ(BN_mod(r, a, &emptyBN, NULL), 0);
  34718. ExpectIntEQ(BN_mod(r, &emptyBN, b, NULL), 0);
  34719. ExpectIntEQ(BN_mod(&emptyBN, a, b, NULL), 0);
  34720. /* END Invalid parameters. */
  34721. val1 = 8;
  34722. val2 = 3;
  34723. ExpectIntEQ(BN_set_word(a, val1), 1);
  34724. ExpectIntEQ(BN_set_word(b, val2), 1);
  34725. ExpectIntEQ(BN_add(r, a, b), 1);
  34726. ExpectIntEQ(BN_is_word(r, val1 + val2), 1);
  34727. ExpectIntEQ(BN_sub(r, a, b), 1);
  34728. ExpectIntEQ(BN_is_word(r, val1 - val2), 1);
  34729. ExpectIntEQ(BN_mul(r, a, b, NULL), 1);
  34730. ExpectIntEQ(BN_is_word(r, val1 * val2), 1);
  34731. ExpectIntEQ(BN_div(r, rem, a, b, NULL), 1);
  34732. ExpectIntEQ(BN_is_word(r, val1 / val2), 1);
  34733. ExpectIntEQ(BN_is_word(rem, val1 % val2), 1);
  34734. ExpectIntEQ(BN_mod(r, a, b, NULL), 1);
  34735. ExpectIntEQ(BN_is_word(r, val1 % val2), 1);
  34736. BN_free(rem);
  34737. BN_free(r);
  34738. BN_free(b);
  34739. BN_free(a);
  34740. #endif
  34741. return EXPECT_RESULT();
  34742. }
  34743. static int test_wolfSSL_BN_math_mod(void)
  34744. {
  34745. EXPECT_DECLS;
  34746. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34747. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34748. BIGNUM* a = NULL;
  34749. BIGNUM* b = NULL;
  34750. BIGNUM* m = NULL;
  34751. BIGNUM* r = NULL;
  34752. BIGNUM* t = NULL;
  34753. BIGNUM emptyBN;
  34754. BN_ULONG val1;
  34755. BN_ULONG val2;
  34756. BN_ULONG val3;
  34757. /* Setup */
  34758. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34759. ExpectNotNull(a = BN_new());
  34760. ExpectNotNull(b = BN_new());
  34761. ExpectNotNull(m = BN_new());
  34762. ExpectNotNull(r = BN_new());
  34763. /* Invalid parameters. */
  34764. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, NULL, NULL), 0);
  34765. ExpectIntEQ(BN_mod_add(r, NULL, NULL, NULL, NULL), 0);
  34766. ExpectIntEQ(BN_mod_add(NULL, a, NULL, NULL, NULL), 0);
  34767. ExpectIntEQ(BN_mod_add(NULL, NULL, b, NULL, NULL), 0);
  34768. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, m, NULL), 0);
  34769. ExpectIntEQ(BN_mod_add(NULL, a, b, m, NULL), 0);
  34770. ExpectIntEQ(BN_mod_add(r, NULL, b, m, NULL), 0);
  34771. ExpectIntEQ(BN_mod_add(r, a, NULL, m, NULL), 0);
  34772. ExpectIntEQ(BN_mod_add(r, a, m, NULL, NULL), 0);
  34773. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34774. ExpectIntEQ(BN_mod_add(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34775. ExpectIntEQ(BN_mod_add(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34776. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34777. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34778. ExpectIntEQ(BN_mod_add(&emptyBN, a, b, m, NULL), 0);
  34779. ExpectIntEQ(BN_mod_add(r, &emptyBN, b, m, NULL), 0);
  34780. ExpectIntEQ(BN_mod_add(r, a, &emptyBN, m, NULL), 0);
  34781. ExpectIntEQ(BN_mod_add(r, a, m, &emptyBN, NULL), 0);
  34782. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, NULL, NULL), 0);
  34783. ExpectIntEQ(BN_mod_mul(r, NULL, NULL, NULL, NULL), 0);
  34784. ExpectIntEQ(BN_mod_mul(NULL, a, NULL, NULL, NULL), 0);
  34785. ExpectIntEQ(BN_mod_mul(NULL, NULL, b, NULL, NULL), 0);
  34786. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, m, NULL), 0);
  34787. ExpectIntEQ(BN_mod_mul(NULL, a, b, m, NULL), 0);
  34788. ExpectIntEQ(BN_mod_mul(r, NULL, b, m, NULL), 0);
  34789. ExpectIntEQ(BN_mod_mul(r, a, NULL, m, NULL), 0);
  34790. ExpectIntEQ(BN_mod_mul(r, a, m, NULL, NULL), 0);
  34791. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34792. ExpectIntEQ(BN_mod_mul(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34793. ExpectIntEQ(BN_mod_mul(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34794. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34795. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34796. ExpectIntEQ(BN_mod_mul(&emptyBN, a, b, m, NULL), 0);
  34797. ExpectIntEQ(BN_mod_mul(r, &emptyBN, b, m, NULL), 0);
  34798. ExpectIntEQ(BN_mod_mul(r, a, &emptyBN, m, NULL), 0);
  34799. ExpectIntEQ(BN_mod_mul(r, a, m, &emptyBN, NULL), 0);
  34800. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, NULL, NULL), 0);
  34801. ExpectIntEQ(BN_mod_exp(r, NULL, NULL, NULL, NULL), 0);
  34802. ExpectIntEQ(BN_mod_exp(NULL, a, NULL, NULL, NULL), 0);
  34803. ExpectIntEQ(BN_mod_exp(NULL, NULL, b, NULL, NULL), 0);
  34804. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, m, NULL), 0);
  34805. ExpectIntEQ(BN_mod_exp(NULL, a, b, m, NULL), 0);
  34806. ExpectIntEQ(BN_mod_exp(r, NULL, b, m, NULL), 0);
  34807. ExpectIntEQ(BN_mod_exp(r, a, NULL, m, NULL), 0);
  34808. ExpectIntEQ(BN_mod_exp(r, a, m, NULL, NULL), 0);
  34809. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34810. ExpectIntEQ(BN_mod_exp(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34811. ExpectIntEQ(BN_mod_exp(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34812. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34813. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34814. ExpectIntEQ(BN_mod_exp(&emptyBN, a, b, m, NULL), 0);
  34815. ExpectIntEQ(BN_mod_exp(r, &emptyBN, b, m, NULL), 0);
  34816. ExpectIntEQ(BN_mod_exp(r, a, &emptyBN, m, NULL), 0);
  34817. ExpectIntEQ(BN_mod_exp(r, a, m, &emptyBN, NULL), 0);
  34818. ExpectNull(BN_mod_inverse(r, NULL, NULL, NULL));
  34819. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  34820. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  34821. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  34822. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  34823. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, &emptyBN, NULL));
  34824. ExpectNull(BN_mod_inverse(r, &emptyBN, &emptyBN, NULL));
  34825. ExpectNull(BN_mod_inverse(&emptyBN, a, &emptyBN, NULL));
  34826. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, m, NULL));
  34827. ExpectNull(BN_mod_inverse(&emptyBN, a, m, NULL));
  34828. ExpectNull(BN_mod_inverse(r, &emptyBN, m, NULL));
  34829. ExpectNull(BN_mod_inverse(r, a, &emptyBN, NULL));
  34830. /* END Invalid parameters. */
  34831. val1 = 9;
  34832. val2 = 13;
  34833. val3 = 5;
  34834. ExpectIntEQ(BN_set_word(a, val1), 1);
  34835. ExpectIntEQ(BN_set_word(b, val2), 1);
  34836. ExpectIntEQ(BN_set_word(m, val3), 1);
  34837. ExpectIntEQ(BN_mod_add(r, a, b, m, NULL), 1);
  34838. ExpectIntEQ(BN_is_word(r, (val1 + val2) % val3), 1);
  34839. ExpectIntEQ(BN_mod_mul(r, a, b, m, NULL), 1);
  34840. ExpectIntEQ(BN_is_word(r, (val1 * val2) % val3), 1);
  34841. ExpectIntEQ(BN_set_word(a, 2), 1);
  34842. ExpectIntEQ(BN_set_word(b, 3), 1);
  34843. ExpectIntEQ(BN_set_word(m, 5), 1);
  34844. /* (2 ^ 3) % 5 = 8 % 5 = 3 */
  34845. ExpectIntEQ(BN_mod_exp(r, a, b, m, NULL), 1);
  34846. ExpectIntEQ(BN_is_word(r, 3), 1);
  34847. /* (2 * 3) % 5 = 6 % 5 = 1 => inv = 3 */
  34848. ExpectNotNull(BN_mod_inverse(r, a, m, NULL));
  34849. ExpectIntEQ(BN_is_word(r, 3), 1);
  34850. ExpectNotNull(t = BN_mod_inverse(NULL, a, m, NULL));
  34851. ExpectIntEQ(BN_is_word(t, 3), 1);
  34852. BN_free(t);
  34853. /* No inverse case. No inverse when a divides b. */
  34854. ExpectIntEQ(BN_set_word(a, 3), 1);
  34855. ExpectIntEQ(BN_set_word(m, 9), 1);
  34856. ExpectNull(BN_mod_inverse(r, a, m, NULL));
  34857. BN_free(r);
  34858. BN_free(m);
  34859. BN_free(b);
  34860. BN_free(a);
  34861. #endif
  34862. return EXPECT_RESULT();
  34863. }
  34864. static int test_wolfSSL_BN_math_other(void)
  34865. {
  34866. EXPECT_DECLS;
  34867. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34868. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34869. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  34870. BIGNUM* a = NULL;
  34871. BIGNUM* b = NULL;
  34872. BIGNUM* r = NULL;
  34873. BIGNUM emptyBN;
  34874. /* Setup */
  34875. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34876. ExpectNotNull(a = BN_new());
  34877. ExpectNotNull(b = BN_new());
  34878. ExpectNotNull(r = BN_new());
  34879. /* Invalid parameters. */
  34880. ExpectIntEQ(BN_gcd(NULL, NULL, NULL, NULL), 0);
  34881. ExpectIntEQ(BN_gcd(r, NULL, NULL, NULL), 0);
  34882. ExpectIntEQ(BN_gcd(NULL, a, NULL, NULL), 0);
  34883. ExpectIntEQ(BN_gcd(NULL, NULL, b, NULL), 0);
  34884. ExpectIntEQ(BN_gcd(NULL, a, b, NULL), 0);
  34885. ExpectIntEQ(BN_gcd(r, NULL, b, NULL), 0);
  34886. ExpectIntEQ(BN_gcd(r, a, NULL, NULL), 0);
  34887. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34888. ExpectIntEQ(BN_gcd(r, &emptyBN, &emptyBN, NULL), 0);
  34889. ExpectIntEQ(BN_gcd(&emptyBN, a, &emptyBN, NULL), 0);
  34890. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, b, NULL), 0);
  34891. ExpectIntEQ(BN_gcd(&emptyBN, a, b, NULL), 0);
  34892. ExpectIntEQ(BN_gcd(r, &emptyBN, b, NULL), 0);
  34893. ExpectIntEQ(BN_gcd(r, a, &emptyBN, NULL), 0);
  34894. /* END Invalid parameters. */
  34895. /* No common factors between 2 and 3. */
  34896. ExpectIntEQ(BN_set_word(a, 2), 1);
  34897. ExpectIntEQ(BN_set_word(b, 3), 1);
  34898. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  34899. ExpectIntEQ(BN_is_word(r, 1), 1);
  34900. /* 3 is largest value that divides both 6 and 9. */
  34901. ExpectIntEQ(BN_set_word(a, 6), 1);
  34902. ExpectIntEQ(BN_set_word(b, 9), 1);
  34903. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  34904. ExpectIntEQ(BN_is_word(r, 3), 1);
  34905. /* GCD of 0 and 0 is undefined. */
  34906. ExpectIntEQ(BN_set_word(a, 0), 1);
  34907. ExpectIntEQ(BN_set_word(b, 0), 1);
  34908. ExpectIntEQ(BN_gcd(r, a, b, NULL), 0);
  34909. /* Teardown */
  34910. BN_free(r);
  34911. BN_free(b);
  34912. BN_free(a);
  34913. #endif
  34914. #endif
  34915. return EXPECT_RESULT();
  34916. }
  34917. static int test_wolfSSL_BN_rand(void)
  34918. {
  34919. EXPECT_DECLS;
  34920. #if defined(OPENSSL_EXTRA) && !defined(OPENSSL_EXTRA_NO_BN)
  34921. BIGNUM* bn = NULL;
  34922. BIGNUM* range = NULL;
  34923. BIGNUM emptyBN;
  34924. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34925. ExpectNotNull(bn = BN_new());
  34926. ExpectNotNull(range = BN_new());
  34927. /* Invalid parameters. */
  34928. ExpectIntEQ(BN_rand(NULL, -1, 0, 0), 0);
  34929. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  34930. ExpectIntEQ(BN_rand(NULL, 1, 0, 0), 0);
  34931. ExpectIntEQ(BN_rand(&emptyBN, -1, 0, 0), 0);
  34932. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  34933. ExpectIntEQ(BN_rand(&emptyBN, 1, 0, 0), 0);
  34934. ExpectIntEQ(BN_pseudo_rand(NULL, -1, 0, 0), 0);
  34935. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  34936. ExpectIntEQ(BN_pseudo_rand(NULL, 1, 0, 0), 0);
  34937. ExpectIntEQ(BN_pseudo_rand(&emptyBN, -1, 0, 0), 0);
  34938. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  34939. ExpectIntEQ(BN_pseudo_rand(&emptyBN, 1, 0, 0), 0);
  34940. ExpectIntEQ(BN_rand_range(NULL, NULL), 0);
  34941. ExpectIntEQ(BN_rand_range(bn, NULL), 0);
  34942. ExpectIntEQ(BN_rand_range(NULL, range), 0);
  34943. ExpectIntEQ(BN_rand_range(&emptyBN, &emptyBN), 0);
  34944. ExpectIntEQ(BN_rand_range(bn, &emptyBN), 0);
  34945. ExpectIntEQ(BN_rand_range(&emptyBN, range), 0);
  34946. /* 0 bit random value must be 0 and so cannot set bit in any position. */
  34947. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  34948. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34949. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  34950. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34951. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  34952. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34953. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  34954. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34955. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  34956. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34957. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  34958. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34959. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  34960. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34961. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  34962. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34963. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  34964. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34965. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  34966. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34967. /* 1 bit random value must have no more than one top bit set. */
  34968. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  34969. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34970. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  34971. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34972. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  34973. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  34974. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  34975. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  34976. /* END Invalid parameters. */
  34977. /* 0 bit random: 0. */
  34978. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  34979. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34980. ExpectIntEQ(BN_is_zero(bn), 1);
  34981. ExpectIntEQ(BN_set_word(bn, 2), 1); /* Make sure not zero. */
  34982. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  34983. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34984. ExpectIntEQ(BN_is_zero(bn), 1);
  34985. /* 1 bit random: 0 or 1. */
  34986. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  34987. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34988. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  34989. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  34990. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34991. ExpectIntEQ(BN_get_word(bn), 1);
  34992. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  34993. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  34994. ExpectIntEQ(BN_get_word(bn), 1);
  34995. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  34996. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  34997. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  34998. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  34999. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35000. ExpectIntEQ(BN_get_word(bn), 1);
  35001. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35002. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35003. ExpectIntEQ(BN_get_word(bn), 1);
  35004. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35005. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35006. ExpectIntEQ(BN_num_bits(bn), 8);
  35007. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35008. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35009. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35010. ExpectIntEQ(BN_num_bits(bn), 8);
  35011. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35012. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  35013. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35014. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35015. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  35016. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  35017. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35018. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35019. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  35020. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35021. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35022. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  35023. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35024. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35025. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  35026. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  35027. * requested number of bits up to the nearest multiple of 8. E.g. in this
  35028. * case, requesting a 13-bit random number would actually return a 16-bit
  35029. * random number. */
  35030. ExpectIntEQ(BN_rand(bn, 13, WOLFSSL_BN_RAND_TOP_ONE,
  35031. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35032. ExpectIntEQ(BN_num_bits(bn), 13);
  35033. ExpectIntEQ(BN_rand(range, 64, WOLFSSL_BN_RAND_TOP_ONE,
  35034. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35035. ExpectIntEQ(BN_rand_range(bn, range), 1);
  35036. ExpectIntEQ(BN_set_word(range, 0), 1);
  35037. ExpectIntEQ(BN_rand_range(bn, range), 1);
  35038. ExpectIntEQ(BN_set_word(range, 1), 1);
  35039. ExpectIntEQ(BN_rand_range(bn, range), 1);
  35040. BN_free(bn);
  35041. BN_free(range);
  35042. #endif
  35043. return EXPECT_RESULT();
  35044. }
  35045. static int test_wolfSSL_BN_prime(void)
  35046. {
  35047. EXPECT_DECLS;
  35048. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  35049. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  35050. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  35051. BIGNUM* a = NULL;
  35052. BIGNUM* add = NULL;
  35053. BIGNUM* rem = NULL;
  35054. BIGNUM emptyBN;
  35055. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35056. ExpectNotNull(a = BN_new());
  35057. ExpectNotNull(add = BN_new());
  35058. ExpectNotNull(rem = BN_new());
  35059. /* Invalid parameters. */
  35060. /* BN_generate_prime_ex()
  35061. * prime - must have valid BIGNUM
  35062. * bits - Greater then 0
  35063. * safe - not supported, must be 0
  35064. * add - not supported, must be NULL
  35065. * rem - not supported, must be NULL
  35066. * cb - anything
  35067. */
  35068. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, rem, NULL), 0);
  35069. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, rem, NULL), 0);
  35070. ExpectIntEQ(BN_generate_prime_ex(a, -1, 1, add, rem, NULL), 0);
  35071. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 1, add, rem, NULL), 0);
  35072. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 1, add, rem, NULL), 0);
  35073. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 0, add, rem, NULL), 0);
  35074. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 0, add, rem, NULL), 0);
  35075. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, NULL, rem, NULL), 0);
  35076. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, NULL, rem, NULL), 0);
  35077. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, NULL, NULL), 0);
  35078. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, NULL, NULL), 0);
  35079. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 0, NULL, NULL, NULL), 0);
  35080. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 0, NULL, NULL, NULL), 0);
  35081. ExpectIntEQ(BN_generate_prime_ex(a, -1, 0, NULL, NULL, NULL), 0);
  35082. ExpectIntEQ(BN_generate_prime_ex(a, 0, 0, NULL, NULL, NULL), 0);
  35083. ExpectIntEQ(BN_generate_prime_ex(a, 2, 1, NULL, NULL, NULL), 0);
  35084. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, add, NULL, NULL), 0);
  35085. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, NULL, rem, NULL), 0);
  35086. ExpectIntEQ(BN_is_prime_ex(NULL, -1, NULL, NULL), -1);
  35087. ExpectIntEQ(BN_is_prime_ex(&emptyBN, -1, NULL, NULL), -1);
  35088. ExpectIntEQ(BN_is_prime_ex(a, -1, NULL, NULL), -1);
  35089. ExpectIntEQ(BN_is_prime_ex(a, 2048, NULL, NULL), -1);
  35090. ExpectIntEQ(BN_is_prime_ex(NULL, 1, NULL, NULL), -1);
  35091. ExpectIntEQ(BN_is_prime_ex(&emptyBN, 1, NULL, NULL), -1);
  35092. /* END Invalid parameters. */
  35093. ExpectIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL), 1);
  35094. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 1);
  35095. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  35096. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 0);
  35097. BN_free(rem);
  35098. BN_free(add);
  35099. BN_free(a);
  35100. #endif
  35101. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  35102. return EXPECT_RESULT();
  35103. }
  35104. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35105. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35106. #define TEST_ARG 0x1234
  35107. static void msg_cb(int write_p, int version, int content_type,
  35108. const void *buf, size_t len, SSL *ssl, void *arg)
  35109. {
  35110. (void)write_p;
  35111. (void)version;
  35112. (void)content_type;
  35113. (void)buf;
  35114. (void)len;
  35115. (void)ssl;
  35116. AssertTrue(arg == (void*)TEST_ARG);
  35117. }
  35118. #endif
  35119. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  35120. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35121. #if defined(SESSION_CERTS)
  35122. #include "wolfssl/internal.h"
  35123. #endif
  35124. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  35125. {
  35126. EXPECT_DECLS;
  35127. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  35128. STACK_OF(X509)* sk = NULL;
  35129. X509* x509 = NULL;
  35130. int i, num;
  35131. BIO* bio = NULL;
  35132. #endif
  35133. ExpectNotNull(ctx);
  35134. ExpectNotNull(ssl);
  35135. fprintf(stderr, "\n===== msgcb called ====\n");
  35136. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  35137. ExpectTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  35138. ExpectIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  35139. ExpectNotNull(SSL_get0_verified_chain(ssl));
  35140. #endif
  35141. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  35142. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  35143. ExpectNotNull(sk = SSL_get_peer_cert_chain(ssl));
  35144. if (sk == NULL) {
  35145. BIO_free(bio);
  35146. return TEST_FAIL;
  35147. }
  35148. num = sk_X509_num(sk);
  35149. ExpectTrue(num > 0);
  35150. for (i = 0; i < num; i++) {
  35151. ExpectNotNull(x509 = sk_X509_value(sk,i));
  35152. if (x509 == NULL)
  35153. break;
  35154. fprintf(stderr, "Certificate at index [%d] = :\n",i);
  35155. X509_print(bio,x509);
  35156. fprintf(stderr, "\n\n");
  35157. }
  35158. BIO_free(bio);
  35159. #endif
  35160. return EXPECT_RESULT();
  35161. }
  35162. #endif
  35163. static int test_wolfSSL_msgCb(void)
  35164. {
  35165. EXPECT_DECLS;
  35166. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  35167. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35168. test_ssl_cbf client_cb;
  35169. test_ssl_cbf server_cb;
  35170. XMEMSET(&client_cb, 0, sizeof(client_cb));
  35171. XMEMSET(&server_cb, 0, sizeof(server_cb));
  35172. #ifndef WOLFSSL_NO_TLS12
  35173. client_cb.method = wolfTLSv1_2_client_method;
  35174. server_cb.method = wolfTLSv1_2_server_method;
  35175. #else
  35176. client_cb.method = wolfTLSv1_3_client_method;
  35177. server_cb.method = wolfTLSv1_3_server_method;
  35178. #endif
  35179. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  35180. &server_cb, msgCb), TEST_SUCCESS);
  35181. #endif
  35182. return EXPECT_RESULT();
  35183. }
  35184. static int test_wolfSSL_either_side(void)
  35185. {
  35186. EXPECT_DECLS;
  35187. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  35188. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35189. test_ssl_cbf client_cb;
  35190. test_ssl_cbf server_cb;
  35191. XMEMSET(&client_cb, 0, sizeof(client_cb));
  35192. XMEMSET(&server_cb, 0, sizeof(server_cb));
  35193. /* Use different CTX for client and server */
  35194. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  35195. ExpectNotNull(client_cb.ctx);
  35196. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  35197. ExpectNotNull(server_cb.ctx);
  35198. /* we are responsible for free'ing WOLFSSL_CTX */
  35199. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  35200. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  35201. &server_cb, NULL), TEST_SUCCESS);
  35202. wolfSSL_CTX_free(client_cb.ctx);
  35203. wolfSSL_CTX_free(server_cb.ctx);
  35204. #endif
  35205. return EXPECT_RESULT();
  35206. }
  35207. static int test_wolfSSL_DTLS_either_side(void)
  35208. {
  35209. EXPECT_DECLS;
  35210. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  35211. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  35212. test_ssl_cbf client_cb;
  35213. test_ssl_cbf server_cb;
  35214. XMEMSET(&client_cb, 0, sizeof(client_cb));
  35215. XMEMSET(&server_cb, 0, sizeof(server_cb));
  35216. /* Use different CTX for client and server */
  35217. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  35218. ExpectNotNull(client_cb.ctx);
  35219. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  35220. ExpectNotNull(server_cb.ctx);
  35221. /* we are responsible for free'ing WOLFSSL_CTX */
  35222. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  35223. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  35224. &server_cb, NULL), TEST_SUCCESS);
  35225. wolfSSL_CTX_free(client_cb.ctx);
  35226. wolfSSL_CTX_free(server_cb.ctx);
  35227. #endif
  35228. return EXPECT_RESULT();
  35229. }
  35230. static int test_generate_cookie(void)
  35231. {
  35232. EXPECT_DECLS;
  35233. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  35234. SSL_CTX* ctx = NULL;
  35235. SSL* ssl = NULL;
  35236. byte buf[FOURK_BUF] = {0};
  35237. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  35238. ExpectNotNull(ssl = SSL_new(ctx));
  35239. /* Test unconnected */
  35240. ExpectIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  35241. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  35242. wolfSSL_SetCookieCtx(ssl, ctx);
  35243. ExpectNotNull(wolfSSL_GetCookieCtx(ssl));
  35244. ExpectNull(wolfSSL_GetCookieCtx(NULL));
  35245. SSL_free(ssl);
  35246. SSL_CTX_free(ctx);
  35247. #endif
  35248. return EXPECT_RESULT();
  35249. }
  35250. static int test_wolfSSL_set_options(void)
  35251. {
  35252. EXPECT_DECLS;
  35253. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35254. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35255. WOLFSSL* ssl = NULL;
  35256. WOLFSSL_CTX* ctx = NULL;
  35257. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  35258. char appData[] = "extra msg";
  35259. #endif
  35260. #ifdef OPENSSL_EXTRA
  35261. unsigned char protos[] = {
  35262. 7, 't', 'l', 's', '/', '1', '.', '2',
  35263. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  35264. };
  35265. unsigned int len = sizeof(protos);
  35266. void *arg = (void *)TEST_ARG;
  35267. #endif
  35268. #ifndef NO_WOLFSSL_SERVER
  35269. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  35270. #else
  35271. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35272. #endif
  35273. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  35274. WOLFSSL_FILETYPE_PEM));
  35275. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  35276. WOLFSSL_FILETYPE_PEM));
  35277. ExpectTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  35278. == WOLFSSL_OP_NO_TLSv1);
  35279. ExpectTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  35280. ExpectIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  35281. WOLFSSL_OP_NO_SSLv2)), 0);
  35282. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  35283. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  35284. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  35285. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  35286. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  35287. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  35288. ExpectFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  35289. WOLFSSL_OP_NO_COMPRESSION));
  35290. wolfSSL_CTX_free(ctx);
  35291. ctx = NULL;
  35292. #ifndef NO_WOLFSSL_SERVER
  35293. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  35294. #else
  35295. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35296. #endif
  35297. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  35298. WOLFSSL_FILETYPE_PEM));
  35299. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  35300. WOLFSSL_FILETYPE_PEM));
  35301. #ifdef OPENSSL_EXTRA
  35302. ExpectTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  35303. #endif
  35304. ExpectNotNull(ssl = wolfSSL_new(ctx));
  35305. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  35306. #ifdef HAVE_EX_DATA
  35307. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  35308. ExpectNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  35309. if (ssl != NULL) {
  35310. ExpectIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  35311. appData, sizeof(appData)), 0);
  35312. }
  35313. #else
  35314. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  35315. ExpectNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  35316. #endif
  35317. #endif
  35318. ExpectTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  35319. WOLFSSL_OP_NO_TLSv1);
  35320. ExpectTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  35321. ExpectIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  35322. WOLFSSL_OP_NO_SSLv2)), 0);
  35323. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  35324. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  35325. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  35326. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  35327. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  35328. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  35329. #ifdef OPENSSL_EXTRA
  35330. ExpectFalse((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  35331. WOLFSSL_OP_NO_COMPRESSION));
  35332. #endif
  35333. #ifdef OPENSSL_EXTRA
  35334. ExpectTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  35335. wolfSSL_set_msg_callback_arg(ssl, arg);
  35336. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  35337. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  35338. #else
  35339. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  35340. #endif
  35341. #endif
  35342. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  35343. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  35344. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  35345. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  35346. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  35347. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  35348. #else
  35349. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  35350. #endif
  35351. #endif /* HAVE_ALPN && !NO_BIO */
  35352. #endif
  35353. wolfSSL_free(ssl);
  35354. wolfSSL_CTX_free(ctx);
  35355. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35356. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35357. return EXPECT_RESULT();
  35358. }
  35359. static int test_wolfSSL_sk_SSL_CIPHER(void)
  35360. {
  35361. EXPECT_DECLS;
  35362. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  35363. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35364. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35365. SSL* ssl = NULL;
  35366. SSL_CTX* ctx = NULL;
  35367. STACK_OF(SSL_CIPHER) *sk = NULL;
  35368. STACK_OF(SSL_CIPHER) *dupSk = NULL;
  35369. #ifndef NO_WOLFSSL_SERVER
  35370. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35371. #else
  35372. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35373. #endif
  35374. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  35375. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35376. ExpectNotNull(ssl = SSL_new(ctx));
  35377. ExpectNotNull(sk = SSL_get_ciphers(ssl));
  35378. ExpectNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  35379. ExpectIntGT(sk_SSL_CIPHER_num(sk), 0);
  35380. ExpectIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  35381. /* error case because connection has not been established yet */
  35382. ExpectIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  35383. sk_SSL_CIPHER_free(dupSk);
  35384. /* sk is pointer to internal struct that should be free'd in SSL_free */
  35385. SSL_free(ssl);
  35386. SSL_CTX_free(ctx);
  35387. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35388. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35389. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35390. return EXPECT_RESULT();
  35391. }
  35392. static int test_wolfSSL_set1_curves_list(void)
  35393. {
  35394. EXPECT_DECLS;
  35395. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  35396. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35397. SSL* ssl = NULL;
  35398. SSL_CTX* ctx = NULL;
  35399. #ifndef NO_WOLFSSL_SERVER
  35400. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35401. #else
  35402. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35403. #endif
  35404. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  35405. SSL_FILETYPE_PEM));
  35406. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  35407. ExpectNotNull(ssl = SSL_new(ctx));
  35408. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  35409. #ifdef HAVE_ECC
  35410. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  35411. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  35412. #endif
  35413. #ifdef HAVE_CURVE25519
  35414. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  35415. #else
  35416. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  35417. #endif
  35418. #ifdef HAVE_CURVE448
  35419. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  35420. #else
  35421. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  35422. #endif
  35423. ExpectIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  35424. #ifdef HAVE_ECC
  35425. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  35426. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  35427. #endif
  35428. #ifdef HAVE_CURVE25519
  35429. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  35430. #else
  35431. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  35432. #endif
  35433. #ifdef HAVE_CURVE448
  35434. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  35435. #else
  35436. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  35437. #endif
  35438. SSL_free(ssl);
  35439. SSL_CTX_free(ctx);
  35440. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35441. #endif
  35442. return EXPECT_RESULT();
  35443. }
  35444. static int test_wolfSSL_set1_sigalgs_list(void)
  35445. {
  35446. EXPECT_DECLS;
  35447. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  35448. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35449. SSL* ssl = NULL;
  35450. SSL_CTX* ctx = NULL;
  35451. #ifndef NO_WOLFSSL_SERVER
  35452. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35453. #else
  35454. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35455. #endif
  35456. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  35457. SSL_FILETYPE_PEM));
  35458. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35459. ExpectNotNull(ssl = SSL_new(ctx));
  35460. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  35461. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  35462. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  35463. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  35464. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  35465. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  35466. #ifndef NO_RSA
  35467. #ifndef NO_SHA256
  35468. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  35469. WOLFSSL_FAILURE);
  35470. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  35471. WOLFSSL_FAILURE);
  35472. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  35473. WOLFSSL_SUCCESS);
  35474. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  35475. WOLFSSL_SUCCESS);
  35476. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  35477. WOLFSSL_FAILURE);
  35478. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  35479. WOLFSSL_FAILURE);
  35480. #ifdef WC_RSA_PSS
  35481. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  35482. WOLFSSL_SUCCESS);
  35483. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  35484. WOLFSSL_SUCCESS);
  35485. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  35486. WOLFSSL_SUCCESS);
  35487. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  35488. WOLFSSL_SUCCESS);
  35489. #endif
  35490. #ifdef WOLFSSL_SHA512
  35491. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35492. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  35493. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35494. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  35495. #elif defined(WOLFSSL_SHA384)
  35496. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35497. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  35498. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35499. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  35500. #endif
  35501. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  35502. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  35503. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  35504. WOLFSSL_FAILURE);
  35505. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  35506. WOLFSSL_FAILURE);
  35507. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  35508. WOLFSSL_FAILURE);
  35509. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  35510. WOLFSSL_FAILURE);
  35511. #endif
  35512. #endif
  35513. #ifdef HAVE_ECC
  35514. #ifndef NO_SHA256
  35515. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  35516. WOLFSSL_SUCCESS);
  35517. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"),
  35518. WOLFSSL_SUCCESS);
  35519. #ifdef WOLFSSL_SHA512
  35520. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35521. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  35522. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35523. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  35524. #elif defined(WOLFSSL_SHA384)
  35525. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35526. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  35527. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35528. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  35529. #endif
  35530. #endif
  35531. #endif
  35532. #ifdef HAVE_ED25519
  35533. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  35534. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  35535. #endif
  35536. #ifdef HAVE_ED448
  35537. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  35538. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  35539. #endif
  35540. #ifndef NO_DSA
  35541. #ifndef NO_SHA256
  35542. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  35543. WOLFSSL_SUCCESS);
  35544. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  35545. WOLFSSL_SUCCESS);
  35546. #endif
  35547. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  35548. defined(WOLFSSL_ALLOW_TLS_SHA1))
  35549. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  35550. WOLFSSL_SUCCESS);
  35551. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  35552. WOLFSSL_SUCCESS);
  35553. #endif
  35554. #endif
  35555. SSL_free(ssl);
  35556. SSL_CTX_free(ctx);
  35557. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35558. #endif
  35559. return EXPECT_RESULT();
  35560. }
  35561. /* Testing wolfSSL_set_tlsext_status_type function.
  35562. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  35563. */
  35564. static int test_wolfSSL_set_tlsext_status_type(void)
  35565. {
  35566. EXPECT_DECLS;
  35567. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  35568. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  35569. SSL* ssl = NULL;
  35570. SSL_CTX* ctx = NULL;
  35571. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35572. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  35573. SSL_FILETYPE_PEM));
  35574. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35575. ExpectNotNull(ssl = SSL_new(ctx));
  35576. ExpectIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  35577. SSL_SUCCESS);
  35578. ExpectIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  35579. SSL_free(ssl);
  35580. SSL_CTX_free(ctx);
  35581. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  35582. return EXPECT_RESULT();
  35583. }
  35584. #ifndef NO_BIO
  35585. static int test_wolfSSL_PEM_read_bio(void)
  35586. {
  35587. EXPECT_DECLS;
  35588. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35589. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35590. byte buff[6000];
  35591. XFILE f = XBADFILE;
  35592. int bytes;
  35593. X509* x509 = NULL;
  35594. BIO* bio = NULL;
  35595. BUF_MEM* buf = NULL;
  35596. ExpectTrue((f = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  35597. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  35598. if (f != XBADFILE)
  35599. XFCLOSE(f);
  35600. ExpectNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  35601. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  35602. ExpectIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  35603. ExpectNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  35604. ExpectIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  35605. /* BIO should return the set EOF value */
  35606. ExpectIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  35607. ExpectIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  35608. ExpectIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  35609. ExpectIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  35610. BIO_free(bio);
  35611. BUF_MEM_free(buf);
  35612. X509_free(x509);
  35613. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  35614. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35615. return EXPECT_RESULT();
  35616. }
  35617. #if defined(OPENSSL_EXTRA)
  35618. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  35619. long argl, long ret)
  35620. {
  35621. (void)bio;
  35622. (void)cmd;
  35623. (void)argp;
  35624. (void)argi;
  35625. (void)argl;
  35626. return ret;
  35627. }
  35628. #endif
  35629. static int test_wolfSSL_BIO(void)
  35630. {
  35631. EXPECT_DECLS;
  35632. #if defined(OPENSSL_EXTRA)
  35633. const unsigned char* p = NULL;
  35634. byte buff[20];
  35635. BIO* bio1 = NULL;
  35636. BIO* bio2 = NULL;
  35637. BIO* bio3 = NULL;
  35638. char* bufPt = NULL;
  35639. int i;
  35640. for (i = 0; i < 20; i++) {
  35641. buff[i] = i;
  35642. }
  35643. /* test BIO_free with NULL */
  35644. ExpectIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  35645. /* Creating and testing type BIO_s_bio */
  35646. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  35647. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  35648. ExpectNotNull(bio3 = BIO_new(BIO_s_bio()));
  35649. /* read/write before set up */
  35650. ExpectIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  35651. ExpectIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  35652. ExpectIntEQ(BIO_set_nbio(bio1, 1), 1);
  35653. ExpectIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  35654. ExpectIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  35655. ExpectIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  35656. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  35657. ExpectNotNull(XMEMCPY(bufPt, buff, 10));
  35658. ExpectIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  35659. /* write buffer full */
  35660. ExpectIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  35661. ExpectIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  35662. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 0);
  35663. /* write the other direction with pair */
  35664. ExpectIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  35665. ExpectNotNull(XMEMCPY(bufPt, buff, 8));
  35666. ExpectIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  35667. /* try read */
  35668. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 8);
  35669. ExpectIntEQ((int)BIO_ctrl_pending(bio2), 20);
  35670. /* try read using ctrl function */
  35671. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  35672. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  35673. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  35674. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  35675. ExpectIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  35676. for (i = 0; i < 20; i++) {
  35677. ExpectIntEQ((int)bufPt[i], i);
  35678. }
  35679. ExpectIntEQ(BIO_nread(bio2, &bufPt, 1), 0);
  35680. ExpectIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  35681. for (i = 0; i < 8; i++) {
  35682. ExpectIntEQ((int)bufPt[i], i);
  35683. }
  35684. ExpectIntEQ(BIO_nread(bio1, &bufPt, 1), 0);
  35685. ExpectIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  35686. /* new pair */
  35687. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  35688. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  35689. bio2 = NULL;
  35690. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  35691. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  35692. ExpectIntEQ(BIO_nread(bio3, &bufPt, 10), 0);
  35693. /* test wrap around... */
  35694. ExpectIntEQ(BIO_reset(bio1), 0);
  35695. ExpectIntEQ(BIO_reset(bio3), 0);
  35696. /* fill write buffer, read only small amount then write again */
  35697. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35698. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  35699. ExpectIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  35700. for (i = 0; i < 4; i++) {
  35701. ExpectIntEQ(bufPt[i], i);
  35702. }
  35703. /* try writing over read index */
  35704. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  35705. ExpectNotNull(XMEMSET(bufPt, 0, 4));
  35706. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 20);
  35707. /* read and write 0 bytes */
  35708. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  35709. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  35710. /* should read only to end of write buffer then need to read again */
  35711. ExpectIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  35712. for (i = 0; i < 16; i++) {
  35713. ExpectIntEQ(bufPt[i], buff[4 + i]);
  35714. }
  35715. ExpectIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  35716. ExpectIntEQ(BIO_nread0(bio3, &bufPt), 4);
  35717. for (i = 0; i < 4; i++) {
  35718. ExpectIntEQ(bufPt[i], 0);
  35719. }
  35720. /* read index should not have advanced with nread0 */
  35721. ExpectIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  35722. for (i = 0; i < 4; i++) {
  35723. ExpectIntEQ(bufPt[i], 0);
  35724. }
  35725. /* write and fill up buffer checking reset of index state */
  35726. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35727. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  35728. /* test reset on data in bio1 write buffer */
  35729. ExpectIntEQ(BIO_reset(bio1), 0);
  35730. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  35731. ExpectIntEQ(BIO_nread(bio3, &bufPt, 3), 0);
  35732. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35733. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  35734. ExpectNotNull(p);
  35735. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  35736. ExpectIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  35737. for (i = 0; i < 6; i++) {
  35738. ExpectIntEQ(bufPt[i], i);
  35739. }
  35740. /* test case of writing twice with offset read index */
  35741. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  35742. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  35743. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35744. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  35745. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35746. ExpectIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  35747. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  35748. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35749. BIO_free(bio1);
  35750. bio1 = NULL;
  35751. BIO_free(bio3);
  35752. bio3 = NULL;
  35753. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  35754. {
  35755. BIO* bioA = NULL;
  35756. BIO* bioB = NULL;
  35757. ExpectIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  35758. ExpectIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  35759. BIO_free(bioA);
  35760. bioA = NULL;
  35761. BIO_free(bioB);
  35762. bioB = NULL;
  35763. }
  35764. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  35765. /* BIOs with file pointers */
  35766. #if !defined(NO_FILESYSTEM)
  35767. {
  35768. XFILE f1 = XBADFILE;
  35769. XFILE f2 = XBADFILE;
  35770. BIO* f_bio1 = NULL;
  35771. BIO* f_bio2 = NULL;
  35772. unsigned char cert[300];
  35773. char testFile[] = "tests/bio_write_test.txt";
  35774. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  35775. ExpectNotNull(f_bio1 = BIO_new(BIO_s_file()));
  35776. ExpectNotNull(f_bio2 = BIO_new(BIO_s_file()));
  35777. /* Failure due to wrong BIO type */
  35778. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  35779. ExpectIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  35780. ExpectTrue((f1 = XFOPEN(svrCertFile, "rwb")) != XBADFILE);
  35781. ExpectIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  35782. ExpectIntEQ(BIO_write_filename(f_bio2, testFile),
  35783. WOLFSSL_SUCCESS);
  35784. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  35785. ExpectIntEQ(BIO_tell(f_bio1),sizeof(cert));
  35786. ExpectIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  35787. ExpectIntEQ(BIO_tell(f_bio2),sizeof(msg));
  35788. ExpectIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  35789. ExpectIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  35790. ExpectIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  35791. ExpectIntEQ(BIO_reset(f_bio2), 0);
  35792. ExpectIntEQ(BIO_tell(NULL),-1);
  35793. ExpectIntEQ(BIO_tell(f_bio2),0);
  35794. ExpectIntEQ(BIO_seek(f_bio2, 4), 0);
  35795. ExpectIntEQ(BIO_tell(f_bio2),4);
  35796. BIO_free(f_bio1);
  35797. f_bio1 = NULL;
  35798. BIO_free(f_bio2);
  35799. f_bio2 = NULL;
  35800. ExpectNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  35801. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  35802. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  35803. BIO_free(f_bio1);
  35804. f_bio1 = NULL;
  35805. }
  35806. #endif /* !defined(NO_FILESYSTEM) */
  35807. /* BIO info callback */
  35808. {
  35809. const char* testArg = "test";
  35810. BIO* cb_bio = NULL;
  35811. ExpectNotNull(cb_bio = BIO_new(BIO_s_mem()));
  35812. BIO_set_callback(cb_bio, bioCallback);
  35813. ExpectNotNull(BIO_get_callback(cb_bio));
  35814. BIO_set_callback(cb_bio, NULL);
  35815. ExpectNull(BIO_get_callback(cb_bio));
  35816. BIO_set_callback_arg(cb_bio, (char*)testArg);
  35817. ExpectStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  35818. ExpectNull(BIO_get_callback_arg(NULL));
  35819. BIO_free(cb_bio);
  35820. cb_bio = NULL;
  35821. }
  35822. /* BIO_vfree */
  35823. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  35824. BIO_vfree(NULL);
  35825. BIO_vfree(bio1);
  35826. #endif
  35827. return EXPECT_RESULT();
  35828. }
  35829. static int test_wolfSSL_BIO_BIO_ring_read(void)
  35830. {
  35831. EXPECT_DECLS;
  35832. #if defined(OPENSSL_ALL)
  35833. BIO* bio1 = NULL;
  35834. BIO* bio2 = NULL;
  35835. byte data[50];
  35836. byte tmp[50];
  35837. XMEMSET(data, 42, sizeof(data));
  35838. ExpectIntEQ(BIO_new_bio_pair(&bio1, sizeof(data), &bio2, sizeof(data)),
  35839. SSL_SUCCESS);
  35840. ExpectIntEQ(BIO_write(bio1, data, 40), 40);
  35841. ExpectIntEQ(BIO_read(bio1, tmp, 20), -1);
  35842. ExpectIntEQ(BIO_read(bio2, tmp, 20), 20);
  35843. ExpectBufEQ(tmp, data, 20);
  35844. ExpectIntEQ(BIO_write(bio1, data, 20), 20);
  35845. ExpectIntEQ(BIO_read(bio2, tmp, 40), 40);
  35846. ExpectBufEQ(tmp, data, 40);
  35847. BIO_free(bio1);
  35848. BIO_free(bio2);
  35849. #endif
  35850. return EXPECT_RESULT();
  35851. }
  35852. #endif /* !NO_BIO */
  35853. static int test_wolfSSL_a2i_IPADDRESS(void)
  35854. {
  35855. EXPECT_DECLS;
  35856. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  35857. const unsigned char* data = NULL;
  35858. int dataSz = 0;
  35859. ASN1_OCTET_STRING *st = NULL;
  35860. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  35861. const unsigned char ipv6_exp[] = {
  35862. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  35863. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  35864. };
  35865. const unsigned char ipv6_home[] = {
  35866. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  35867. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  35868. };
  35869. ExpectNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  35870. ExpectNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  35871. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  35872. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP4_ADDR_LEN);
  35873. ExpectIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  35874. ASN1_STRING_free(st);
  35875. st = NULL;
  35876. ExpectNotNull(st = a2i_IPADDRESS("::1"));
  35877. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  35878. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  35879. ExpectIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  35880. ASN1_STRING_free(st);
  35881. st = NULL;
  35882. ExpectNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  35883. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  35884. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  35885. ExpectIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  35886. ASN1_STRING_free(st);
  35887. #endif
  35888. return EXPECT_RESULT();
  35889. }
  35890. static int test_wolfSSL_X509_cmp_time(void)
  35891. {
  35892. EXPECT_DECLS;
  35893. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  35894. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  35895. WOLFSSL_ASN1_TIME asn_time;
  35896. time_t t;
  35897. ExpectIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  35898. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  35899. ExpectIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  35900. ExpectIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  35901. ExpectIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  35902. #endif
  35903. return EXPECT_RESULT();
  35904. }
  35905. static int test_wolfSSL_X509_time_adj(void)
  35906. {
  35907. EXPECT_DECLS;
  35908. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  35909. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  35910. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  35911. !defined(NO_ASN_TIME)
  35912. X509* x509 = NULL;
  35913. time_t t;
  35914. time_t not_before;
  35915. time_t not_after;
  35916. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  35917. client_cert_der_2048, sizeof_client_cert_der_2048,
  35918. WOLFSSL_FILETYPE_ASN1));
  35919. t = 0;
  35920. not_before = wc_Time(0);
  35921. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  35922. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  35923. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  35924. /* Check X509_gmtime_adj, too. */
  35925. ExpectNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  35926. X509_free(x509);
  35927. #endif
  35928. return EXPECT_RESULT();
  35929. }
  35930. static int test_wolfSSL_X509(void)
  35931. {
  35932. EXPECT_DECLS;
  35933. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  35934. !defined(NO_RSA)
  35935. X509* x509 = NULL;
  35936. #ifndef NO_BIO
  35937. BIO* bio = NULL;
  35938. X509_STORE_CTX* ctx = NULL;
  35939. X509_STORE* store = NULL;
  35940. #endif
  35941. char der[] = "certs/ca-cert.der";
  35942. XFILE fp = XBADFILE;
  35943. ExpectNotNull(x509 = X509_new());
  35944. X509_free(x509);
  35945. x509 = NULL;
  35946. #ifndef NO_BIO
  35947. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  35948. SSL_FILETYPE_PEM));
  35949. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  35950. #ifdef WOLFSSL_CERT_GEN
  35951. ExpectIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  35952. #endif
  35953. ExpectNotNull(ctx = X509_STORE_CTX_new());
  35954. ExpectIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  35955. ExpectNotNull(store = X509_STORE_new());
  35956. ExpectIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  35957. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  35958. ExpectIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  35959. X509_STORE_CTX_free(ctx);
  35960. X509_STORE_free(store);
  35961. X509_free(x509);
  35962. x509 = NULL;
  35963. BIO_free(bio);
  35964. #endif
  35965. /** d2i_X509_fp test **/
  35966. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  35967. ExpectNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  35968. ExpectNotNull(x509);
  35969. X509_free(x509);
  35970. x509 = NULL;
  35971. if (fp != XBADFILE) {
  35972. XFCLOSE(fp);
  35973. fp = XBADFILE;
  35974. }
  35975. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  35976. ExpectNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  35977. ExpectNotNull(x509);
  35978. X509_free(x509);
  35979. if (fp != XBADFILE)
  35980. XFCLOSE(fp);
  35981. /* X509_up_ref test */
  35982. ExpectIntEQ(X509_up_ref(NULL), 0);
  35983. ExpectNotNull(x509 = X509_new()); /* refCount = 1 */
  35984. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  35985. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  35986. X509_free(x509); /* refCount = 2 */
  35987. X509_free(x509); /* refCount = 1 */
  35988. X509_free(x509); /* refCount = 0, free */
  35989. #endif
  35990. return EXPECT_RESULT();
  35991. }
  35992. static int test_wolfSSL_X509_get_ext_count(void)
  35993. {
  35994. EXPECT_DECLS;
  35995. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  35996. !defined(NO_RSA)
  35997. int ret = 0;
  35998. WOLFSSL_X509* x509 = NULL;
  35999. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  36000. XFILE f = XBADFILE;
  36001. /* NULL parameter check */
  36002. ExpectIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  36003. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  36004. SSL_FILETYPE_PEM));
  36005. ExpectIntEQ(X509_get_ext_count(x509), 5);
  36006. wolfSSL_X509_free(x509);
  36007. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  36008. SSL_FILETYPE_PEM));
  36009. ExpectIntEQ(X509_get_ext_count(x509), 5);
  36010. wolfSSL_X509_free(x509);
  36011. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  36012. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  36013. if (f != XBADFILE)
  36014. XFCLOSE(f);
  36015. /* wolfSSL_X509_get_ext_count() valid input */
  36016. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  36017. /* wolfSSL_X509_get_ext_count() NULL argument */
  36018. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  36019. wolfSSL_X509_free(x509);
  36020. #endif
  36021. return EXPECT_RESULT();
  36022. }
  36023. static int test_wolfSSL_X509_sign2(void)
  36024. {
  36025. EXPECT_DECLS;
  36026. /* test requires WOLFSSL_AKID_NAME to match expected output */
  36027. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  36028. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  36029. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_AKID_NAME) && \
  36030. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  36031. defined(WOLFSSL_IP_ALT_NAME))
  36032. WOLFSSL_X509 *x509 = NULL;
  36033. WOLFSSL_X509 *ca = NULL;
  36034. const unsigned char *der = NULL;
  36035. const unsigned char *pt = NULL;
  36036. WOLFSSL_EVP_PKEY *priv = NULL;
  36037. WOLFSSL_X509_NAME *name = NULL;
  36038. int derSz;
  36039. #ifndef NO_ASN_TIME
  36040. WOLFSSL_ASN1_TIME *notBefore = NULL;
  36041. WOLFSSL_ASN1_TIME *notAfter = NULL;
  36042. const int year = 365*24*60*60;
  36043. const int day = 24*60*60;
  36044. const int hour = 60*60;
  36045. const int mini = 60;
  36046. time_t t;
  36047. #endif
  36048. const unsigned char expected[] = {
  36049. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xFB, 0xA0, 0x03, 0x02, 0x01,
  36050. 0x02, 0x02, 0x14, 0x08, 0xB0, 0x54, 0x7A, 0x03, 0x5A, 0xEC, 0x55, 0x8A,
  36051. 0x12, 0xE8, 0xF9, 0x8E, 0x34, 0xB6, 0x13, 0xD9, 0x59, 0xB8, 0xE8, 0x30,
  36052. 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
  36053. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
  36054. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  36055. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61,
  36056. 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  36057. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03,
  36058. 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6F, 0x6F, 0x74,
  36059. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x0A,
  36060. 0x43, 0x6F, 0x6E, 0x73, 0x75, 0x6C, 0x74, 0x69, 0x6E, 0x67, 0x31, 0x18,
  36061. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77,
  36062. 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
  36063. 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  36064. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F,
  36065. 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E, 0x17,
  36066. 0x0D, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  36067. 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  36068. 0x33, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30, 0x09,
  36069. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  36070. 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74,
  36071. 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07,
  36072. 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15, 0x30,
  36073. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C, 0x77, 0x6F, 0x6C, 0x66,
  36074. 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  36075. 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50, 0x72, 0x6F, 0x67, 0x72,
  36076. 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32, 0x30, 0x34, 0x38, 0x31,
  36077. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  36078. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
  36079. 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  36080. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
  36081. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x82,
  36082. 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  36083. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82,
  36084. 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE,
  36085. 0x39, 0xA4, 0x32, 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C, 0x74,
  36086. 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47, 0xD6, 0xA6, 0x36, 0xB2, 0x07,
  36087. 0x32, 0x8E, 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4, 0x81,
  36088. 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67, 0xBB, 0xA1, 0x75, 0xC8, 0x36,
  36089. 0x2C, 0x4A, 0xD2, 0x1B, 0xF7, 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF, 0xEC,
  36090. 0xF1, 0x81, 0x1E, 0x7B, 0x9B, 0x03, 0x47, 0x9A, 0xBF, 0x65, 0xCC, 0x7F,
  36091. 0x65, 0x24, 0x69, 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7, 0xC5,
  36092. 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A, 0x7A, 0x78, 0xE1, 0x01, 0x56,
  36093. 0x56, 0x91, 0xA6, 0x13, 0x42, 0x8D, 0xD2, 0x3C, 0x40, 0x9C, 0x4C, 0xEF,
  36094. 0xD1, 0x86, 0xDF, 0x37, 0x51, 0x1B, 0x0C, 0xA1, 0x3B, 0xF5, 0xF1, 0xA3,
  36095. 0x4A, 0x35, 0xE4, 0xE1, 0xCE, 0x96, 0xDF, 0x1B, 0x7E, 0xBF, 0x4E, 0x97,
  36096. 0xD0, 0x10, 0xE8, 0xA8, 0x08, 0x30, 0x81, 0xAF, 0x20, 0x0B, 0x43, 0x14,
  36097. 0xC5, 0x74, 0x67, 0xB4, 0x32, 0x82, 0x6F, 0x8D, 0x86, 0xC2, 0x88, 0x40,
  36098. 0x99, 0x36, 0x83, 0xBA, 0x1E, 0x40, 0x72, 0x22, 0x17, 0xD7, 0x52, 0x65,
  36099. 0x24, 0x73, 0xB0, 0xCE, 0xEF, 0x19, 0xCD, 0xAE, 0xFF, 0x78, 0x6C, 0x7B,
  36100. 0xC0, 0x12, 0x03, 0xD4, 0x4E, 0x72, 0x0D, 0x50, 0x6D, 0x3B, 0xA3, 0x3B,
  36101. 0xA3, 0x99, 0x5E, 0x9D, 0xC8, 0xD9, 0x0C, 0x85, 0xB3, 0xD9, 0x8A, 0xD9,
  36102. 0x54, 0x26, 0xDB, 0x6D, 0xFA, 0xAC, 0xBB, 0xFF, 0x25, 0x4C, 0xC4, 0xD1,
  36103. 0x79, 0xF4, 0x71, 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, 0xB5, 0x72,
  36104. 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, 0x2D, 0x56, 0x2F, 0xD7, 0x15, 0xF7,
  36105. 0x7F, 0xC0, 0xAE, 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, 0xD3, 0x02,
  36106. 0x03, 0x01, 0x00, 0x01, 0xA3, 0x82, 0x01, 0x4F, 0x30, 0x82, 0x01, 0x4B,
  36107. 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  36108. 0x01, 0xFF, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30,
  36109. 0x13, 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63,
  36110. 0x6F, 0x6D, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03,
  36111. 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  36112. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  36113. 0x85, 0x65, 0xC0, 0x30, 0x81, 0xDE, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04,
  36114. 0x81, 0xD6, 0x30, 0x81, 0xD3, 0x80, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  36115. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  36116. 0x85, 0x65, 0xC0, 0xA1, 0x81, 0xA4, 0xA4, 0x81, 0xA1, 0x30, 0x81, 0x9E,
  36117. 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  36118. 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07,
  36119. 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06,
  36120. 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61,
  36121. 0x6E, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C,
  36122. 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38,
  36123. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50,
  36124. 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32,
  36125. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  36126. 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  36127. 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A,
  36128. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E,
  36129. 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  36130. 0x6F, 0x6D, 0x82, 0x14, 0x08, 0xB0, 0x54, 0x7A, 0x03, 0x5A, 0xEC, 0x55,
  36131. 0x8A, 0x12, 0xE8, 0xF9, 0x8E, 0x34, 0xB6, 0x13, 0xD9, 0x59, 0xB8, 0xE8,
  36132. 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  36133. 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2B,
  36134. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0D, 0x06, 0x09, 0x2A,
  36135. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
  36136. 0x01, 0x01, 0x00, 0x14, 0xFB, 0xD0, 0xCE, 0x31, 0x7F, 0xA5, 0x59, 0xFA,
  36137. 0x7C, 0x68, 0x26, 0xA7, 0xE8, 0x0D, 0x9F, 0x50, 0x57, 0xFA, 0x1C, 0x7C,
  36138. 0x5E, 0x43, 0xA4, 0x97, 0x47, 0xB6, 0x41, 0xAC, 0x63, 0xD3, 0x61, 0x8C,
  36139. 0x1F, 0x42, 0xEF, 0x53, 0xD0, 0xBA, 0x31, 0x4D, 0x99, 0x74, 0xA4, 0x60,
  36140. 0xDC, 0xC6, 0x6F, 0xCC, 0x1E, 0x25, 0x98, 0xE1, 0xA4, 0xA0, 0x67, 0x69,
  36141. 0x97, 0xE3, 0x97, 0x7C, 0x83, 0x28, 0xF1, 0xF4, 0x7D, 0x03, 0xA8, 0x31,
  36142. 0x77, 0xCC, 0xD1, 0x37, 0xEF, 0x7B, 0x4A, 0x71, 0x2D, 0x11, 0x7E, 0x92,
  36143. 0xF5, 0x67, 0xB7, 0x56, 0xBA, 0x28, 0xF8, 0xD6, 0xCE, 0x2A, 0x71, 0xE3,
  36144. 0x70, 0x6B, 0x09, 0x0F, 0x67, 0x6F, 0x7A, 0xE0, 0x89, 0xF6, 0x5E, 0x23,
  36145. 0x0C, 0x0A, 0x44, 0x4E, 0x65, 0x8E, 0x7B, 0x68, 0xD0, 0xAD, 0x76, 0x3E,
  36146. 0x2A, 0x0E, 0xA2, 0x05, 0x11, 0x74, 0x24, 0x08, 0x60, 0xED, 0x9F, 0x98,
  36147. 0x18, 0xE9, 0x91, 0x58, 0x36, 0xEC, 0xEC, 0x25, 0x6B, 0xBA, 0x9C, 0x87,
  36148. 0x38, 0x68, 0xDC, 0xDC, 0x15, 0x6F, 0x20, 0x68, 0xC4, 0xBF, 0x05, 0x5B,
  36149. 0x4A, 0x0C, 0x44, 0x2B, 0x92, 0x3F, 0x10, 0x99, 0xDC, 0xF6, 0x6C, 0x0E,
  36150. 0x34, 0x26, 0x6E, 0x6D, 0x4E, 0x12, 0xBC, 0x60, 0x8F, 0x27, 0x1D, 0x7A,
  36151. 0x00, 0x50, 0xBE, 0x23, 0xDE, 0x48, 0x47, 0x9F, 0xAD, 0x2F, 0x94, 0x3D,
  36152. 0x16, 0x73, 0x48, 0x6B, 0xC8, 0x97, 0xE6, 0xB4, 0xB3, 0x4B, 0xE1, 0x68,
  36153. 0x08, 0xC3, 0xE5, 0x34, 0x5F, 0x9B, 0xDA, 0xAB, 0xCA, 0x6D, 0x55, 0x32,
  36154. 0xEF, 0x6C, 0xEF, 0x9B, 0x8B, 0x5B, 0xC7, 0xF0, 0xC2, 0x0F, 0x8E, 0x93,
  36155. 0x09, 0x60, 0x3C, 0x0B, 0xDC, 0xBD, 0xDB, 0x4A, 0x2D, 0xD0, 0x98, 0xAA,
  36156. 0xAB, 0x6C, 0x6F, 0x6D, 0x6B, 0x6A, 0x5C, 0x33, 0xAC, 0xAD, 0xA8, 0x1B,
  36157. 0x38, 0x5D, 0x9F, 0xDA, 0xE7, 0x70, 0x07
  36158. };
  36159. pt = ca_key_der_2048;
  36160. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  36161. sizeof_ca_key_der_2048));
  36162. pt = client_cert_der_2048;
  36163. ExpectNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  36164. sizeof_client_cert_der_2048));
  36165. pt = ca_cert_der_2048;
  36166. ExpectNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  36167. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  36168. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  36169. #ifndef NO_ASN_TIME
  36170. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  36171. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  36172. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  36173. ExpectIntEQ(notAfter->length, 13);
  36174. ExpectTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  36175. ExpectTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  36176. #endif
  36177. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  36178. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  36179. ExpectIntEQ(derSz, sizeof(expected));
  36180. #ifndef NO_ASN_TIME
  36181. ExpectIntEQ(XMEMCMP(der, expected, derSz), 0);
  36182. #endif
  36183. wolfSSL_X509_free(ca);
  36184. wolfSSL_X509_free(x509);
  36185. wolfSSL_EVP_PKEY_free(priv);
  36186. #ifndef NO_ASN_TIME
  36187. wolfSSL_ASN1_TIME_free(notBefore);
  36188. wolfSSL_ASN1_TIME_free(notAfter);
  36189. #endif
  36190. #endif
  36191. return EXPECT_RESULT();
  36192. }
  36193. static int test_wolfSSL_X509_sign(void)
  36194. {
  36195. EXPECT_DECLS;
  36196. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_ASN_TIME) && \
  36197. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  36198. int ret = 0;
  36199. char *cn = NULL;
  36200. word32 cnSz = 0;
  36201. X509_NAME *name = NULL;
  36202. X509 *x509 = NULL;
  36203. X509 *ca = NULL;
  36204. DecodedCert dCert;
  36205. EVP_PKEY *pub = NULL;
  36206. EVP_PKEY *priv = NULL;
  36207. EVP_MD_CTX *mctx = NULL;
  36208. #if defined(USE_CERT_BUFFERS_1024)
  36209. const unsigned char* rsaPriv = client_key_der_1024;
  36210. const unsigned char* rsaPub = client_keypub_der_1024;
  36211. const unsigned char* certIssuer = client_cert_der_1024;
  36212. long clientKeySz = (long)sizeof_client_key_der_1024;
  36213. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  36214. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  36215. #elif defined(USE_CERT_BUFFERS_2048)
  36216. const unsigned char* rsaPriv = client_key_der_2048;
  36217. const unsigned char* rsaPub = client_keypub_der_2048;
  36218. const unsigned char* certIssuer = client_cert_der_2048;
  36219. long clientKeySz = (long)sizeof_client_key_der_2048;
  36220. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  36221. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  36222. #endif
  36223. byte sn[16];
  36224. int snSz = sizeof(sn);
  36225. /* Set X509_NAME fields */
  36226. ExpectNotNull(name = X509_NAME_new());
  36227. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  36228. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  36229. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  36230. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  36231. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  36232. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  36233. /* Get private and public keys */
  36234. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  36235. clientKeySz));
  36236. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  36237. ExpectNotNull(x509 = X509_new());
  36238. /* Set version 3 */
  36239. ExpectIntNE(X509_set_version(x509, 2L), 0);
  36240. /* Set subject name, add pubkey, and sign certificate */
  36241. ExpectIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  36242. X509_NAME_free(name);
  36243. name = NULL;
  36244. ExpectIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  36245. #ifdef WOLFSSL_ALT_NAMES
  36246. /* Add some subject alt names */
  36247. ExpectIntNE(wolfSSL_X509_add_altname(NULL,
  36248. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  36249. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  36250. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  36251. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  36252. "sphygmomanometer",
  36253. ASN_DNS_TYPE), SSL_SUCCESS);
  36254. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  36255. "supercalifragilisticexpialidocious",
  36256. ASN_DNS_TYPE), SSL_SUCCESS);
  36257. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  36258. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  36259. ASN_DNS_TYPE), SSL_SUCCESS);
  36260. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  36261. {
  36262. unsigned char ip4_type[] = {127,128,0,255};
  36263. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  36264. 0xff, 0xee, 0x99, 0x88,
  36265. 0x77, 0x66, 0x55, 0x44,
  36266. 0x00, 0x33, 0x22, 0x11};
  36267. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  36268. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  36269. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  36270. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  36271. }
  36272. #endif
  36273. #endif /* WOLFSSL_ALT_NAMES */
  36274. /* test valid sign case */
  36275. ExpectIntGT(ret = X509_sign(x509, priv, EVP_sha256()), 0);
  36276. /* test valid X509_sign_ctx case */
  36277. ExpectNotNull(mctx = EVP_MD_CTX_new());
  36278. ExpectIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  36279. ExpectIntGT(X509_sign_ctx(x509, mctx), 0);
  36280. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  36281. ExpectIntEQ(X509_get_ext_count(x509), 1);
  36282. #endif
  36283. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  36284. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  36285. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  36286. #endif
  36287. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  36288. WOLFSSL_SUCCESS);
  36289. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  36290. /* Variation in size depends on ASN.1 encoding when MSB is set.
  36291. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  36292. * with the MSB set. See GenerateInteger in asn.c */
  36293. #ifndef USE_CERT_BUFFERS_1024
  36294. #ifndef WOLFSSL_ALT_NAMES
  36295. /* Valid case - size should be 781-786 with 16 byte serial number */
  36296. ExpectTrue((781 + snSz <= ret) && (ret <= 781 + 5 + snSz));
  36297. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  36298. /* Valid case - size should be 955-960 with 16 byte serial number */
  36299. ExpectTrue((939 + snSz <= ret) && (ret <= 939 + 5 + snSz));
  36300. #else
  36301. /* Valid case - size should be 926-931 with 16 byte serial number */
  36302. ExpectTrue((910 + snSz <= ret) && (ret <= 910 + 5 + snSz));
  36303. #endif
  36304. #else
  36305. #ifndef WOLFSSL_ALT_NAMES
  36306. /* Valid case - size should be 537-542 with 16 byte serial number */
  36307. ExpectTrue((521 + snSz <= ret) && (ret <= 521 + 5 + snSz));
  36308. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  36309. /* Valid case - size should be 695-670 with 16 byte serial number */
  36310. ExpectTrue((679 + snSz <= ret) && (ret <= 679 + 5 + snSz));
  36311. #else
  36312. /* Valid case - size should be 666-671 with 16 byte serial number */
  36313. ExpectTrue((650 + snSz <= ret) && (ret <= 650 + 5 + snSz));
  36314. #endif
  36315. #endif
  36316. /* check that issuer name is as expected after signature */
  36317. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  36318. ExpectIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  36319. ExpectNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  36320. ExpectNotNull(name = X509_get_subject_name(ca));
  36321. cnSz = X509_NAME_get_sz(name);
  36322. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  36323. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  36324. ExpectIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  36325. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  36326. cn = NULL;
  36327. #ifdef WOLFSSL_MULTI_ATTRIB
  36328. /* test adding multiple OU's to the signer */
  36329. ExpectNotNull(name = X509_get_subject_name(ca));
  36330. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  36331. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  36332. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  36333. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  36334. ExpectIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  36335. #endif
  36336. ExpectNotNull(name = X509_get_subject_name(ca));
  36337. ExpectIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  36338. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  36339. ExpectNotNull(name = X509_get_issuer_name(x509));
  36340. cnSz = X509_NAME_get_sz(name);
  36341. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  36342. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  36343. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  36344. ExpectIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  36345. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  36346. cn = NULL;
  36347. FreeDecodedCert(&dCert);
  36348. /* Test invalid parameters */
  36349. ExpectIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  36350. ExpectIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  36351. ExpectIntEQ(X509_sign(x509, priv, NULL), 0);
  36352. ExpectIntEQ(X509_sign_ctx(NULL, mctx), 0);
  36353. EVP_MD_CTX_free(mctx);
  36354. mctx = NULL;
  36355. ExpectNotNull(mctx = EVP_MD_CTX_new());
  36356. ExpectIntEQ(X509_sign_ctx(x509, mctx), 0);
  36357. ExpectIntEQ(X509_sign_ctx(x509, NULL), 0);
  36358. /* test invalid version number */
  36359. #if defined(OPENSSL_ALL)
  36360. ExpectIntNE(X509_set_version(x509, 6L), 0);
  36361. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  36362. /* uses ParseCert which fails on bad version number */
  36363. ExpectIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  36364. #endif
  36365. EVP_MD_CTX_free(mctx);
  36366. EVP_PKEY_free(priv);
  36367. EVP_PKEY_free(pub);
  36368. X509_free(x509);
  36369. X509_free(ca);
  36370. #endif
  36371. return EXPECT_RESULT();
  36372. }
  36373. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  36374. {
  36375. EXPECT_DECLS;
  36376. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  36377. X509* x509 = NULL;
  36378. const X509_ALGOR* alg;
  36379. ExpectNotNull(x509 = X509_new());
  36380. ExpectNull(alg = X509_get0_tbs_sigalg(NULL));
  36381. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  36382. X509_free(x509);
  36383. #endif
  36384. return EXPECT_RESULT();
  36385. }
  36386. static int test_wolfSSL_X509_ALGOR_get0(void)
  36387. {
  36388. EXPECT_DECLS;
  36389. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  36390. !defined(NO_SHA256) && !defined(NO_RSA)
  36391. X509* x509 = NULL;
  36392. const ASN1_OBJECT* obj = NULL;
  36393. const X509_ALGOR* alg = NULL;
  36394. int pptype = 0;
  36395. const void *ppval = NULL;
  36396. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  36397. SSL_FILETYPE_PEM));
  36398. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  36399. /* Invalid case */
  36400. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  36401. ExpectNull(obj);
  36402. /* Valid case */
  36403. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  36404. ExpectNotNull(obj);
  36405. ExpectNull(ppval);
  36406. ExpectIntNE(pptype, 0);
  36407. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  36408. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  36409. X509_free(x509);
  36410. #endif
  36411. return EXPECT_RESULT();
  36412. }
  36413. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  36414. {
  36415. EXPECT_DECLS;
  36416. #if defined(OPENSSL_EXTRA)
  36417. X509_VERIFY_PARAM *paramTo = NULL;
  36418. X509_VERIFY_PARAM *paramFrom = NULL;
  36419. char testIPv4[] = "127.0.0.1";
  36420. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  36421. char testhostName1[] = "foo.hoge.com";
  36422. char testhostName2[] = "foobar.hoge.com";
  36423. ExpectNotNull(paramTo = X509_VERIFY_PARAM_new());
  36424. ExpectNotNull(XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM)));
  36425. ExpectNotNull(paramFrom = X509_VERIFY_PARAM_new());
  36426. ExpectNotNull(XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM)));
  36427. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  36428. (int)XSTRLEN(testhostName1)), 1);
  36429. ExpectIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  36430. (int)XSTRLEN(testhostName1)));
  36431. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  36432. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  36433. ExpectIntEQ(0x01, paramFrom->hostFlags);
  36434. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4), 0);
  36435. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4), 1);
  36436. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  36437. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL), 1);
  36438. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6), 1);
  36439. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36440. /* null pointer */
  36441. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, paramFrom), 0);
  36442. /* in the case of "from" null, returns success */
  36443. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, NULL), 1);
  36444. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, NULL), 0);
  36445. /* inherit flags test : VPARAM_DEFAULT */
  36446. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  36447. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  36448. (int)XSTRLEN(testhostName1)));
  36449. ExpectIntEQ(0x01, paramTo->hostFlags);
  36450. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36451. /* inherit flags test : VPARAM OVERWRITE */
  36452. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  36453. (int)XSTRLEN(testhostName2)), 1);
  36454. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  36455. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  36456. if (paramTo != NULL) {
  36457. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  36458. }
  36459. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  36460. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  36461. (int)XSTRLEN(testhostName1)));
  36462. ExpectIntEQ(0x01, paramTo->hostFlags);
  36463. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36464. /* inherit flags test : VPARAM_RESET_FLAGS */
  36465. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  36466. (int)XSTRLEN(testhostName2)), 1);
  36467. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  36468. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  36469. if (paramTo != NULL) {
  36470. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  36471. }
  36472. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  36473. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  36474. (int)XSTRLEN(testhostName1)));
  36475. ExpectIntEQ(0x01, paramTo->hostFlags);
  36476. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36477. /* inherit flags test : VPARAM_LOCKED */
  36478. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  36479. (int)XSTRLEN(testhostName2)), 1);
  36480. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  36481. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  36482. if (paramTo != NULL) {
  36483. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  36484. }
  36485. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  36486. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  36487. (int)XSTRLEN(testhostName2)));
  36488. ExpectIntEQ(0x00, paramTo->hostFlags);
  36489. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  36490. /* test for incorrect parameters */
  36491. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL),
  36492. 0);
  36493. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, 0), 0);
  36494. /* inherit flags test : VPARAM_ONCE, not testable yet */
  36495. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL),
  36496. 1);
  36497. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo),
  36498. X509_V_FLAG_CRL_CHECK_ALL);
  36499. ExpectIntEQ(X509_VERIFY_PARAM_clear_flags(paramTo,
  36500. X509_V_FLAG_CRL_CHECK_ALL), 1);
  36501. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo), 0);
  36502. X509_VERIFY_PARAM_free(paramTo);
  36503. X509_VERIFY_PARAM_free(paramFrom);
  36504. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  36505. #endif
  36506. return EXPECT_RESULT();
  36507. }
  36508. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  36509. static int test_wolfSSL_check_domain_verify_count = 0;
  36510. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  36511. WOLFSSL_X509_STORE_CTX* store)
  36512. {
  36513. EXPECT_DECLS;
  36514. ExpectIntEQ(X509_STORE_CTX_get_error(store), 0);
  36515. ExpectIntEQ(preverify, 1);
  36516. ExpectIntGT(++test_wolfSSL_check_domain_verify_count, 0);
  36517. return EXPECT_SUCCESS();
  36518. }
  36519. static int test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  36520. {
  36521. EXPECT_DECLS;
  36522. X509_VERIFY_PARAM *param = NULL;
  36523. ExpectNotNull(param = SSL_get0_param(ssl));
  36524. /* Domain check should only be done on the leaf cert */
  36525. X509_VERIFY_PARAM_set_hostflags(param,
  36526. X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  36527. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(param,
  36528. "wolfSSL Server Chain", 0), 1);
  36529. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  36530. test_wolfSSL_check_domain_verify_cb);
  36531. return EXPECT_RESULT();
  36532. }
  36533. static int test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  36534. {
  36535. EXPECT_DECLS;
  36536. /* Use a cert with different domains in chain */
  36537. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  36538. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  36539. return EXPECT_RESULT();
  36540. }
  36541. static int test_wolfSSL_check_domain(void)
  36542. {
  36543. EXPECT_DECLS;
  36544. test_ssl_cbf func_cb_client;
  36545. test_ssl_cbf func_cb_server;
  36546. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  36547. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  36548. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  36549. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  36550. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  36551. &func_cb_server, NULL), TEST_SUCCESS);
  36552. /* Should have been called once for each cert in sent chain */
  36553. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  36554. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  36555. #else
  36556. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  36557. #endif
  36558. return EXPECT_RESULT();
  36559. }
  36560. #endif /* OPENSSL_EXTRA && HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  36561. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  36562. {
  36563. EXPECT_DECLS;
  36564. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  36565. X509* x509 = NULL;
  36566. X509_PUBKEY* pubKey;
  36567. ExpectNotNull(x509 = X509_new());
  36568. ExpectNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  36569. ExpectNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  36570. X509_free(x509);
  36571. #endif
  36572. return EXPECT_RESULT();
  36573. }
  36574. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  36575. {
  36576. EXPECT_DECLS;
  36577. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  36578. !defined(NO_SHA256) && !defined(NO_RSA)
  36579. X509* x509 = NULL;
  36580. ASN1_OBJECT* obj = NULL;
  36581. const ASN1_OBJECT* pa_oid = NULL;
  36582. X509_PUBKEY* pubKey = NULL;
  36583. X509_PUBKEY* pubKey2 = NULL;
  36584. EVP_PKEY* evpKey = NULL;
  36585. const unsigned char *pk = NULL;
  36586. int ppklen;
  36587. int pptype;
  36588. X509_ALGOR *pa = NULL;
  36589. const void *pval;
  36590. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  36591. SSL_FILETYPE_PEM));
  36592. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  36593. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  36594. ExpectNotNull(pk);
  36595. ExpectNotNull(pa);
  36596. ExpectNotNull(pubKey);
  36597. ExpectIntGT(ppklen, 0);
  36598. ExpectIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  36599. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  36600. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  36601. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  36602. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  36603. ExpectNotNull(pk);
  36604. ExpectNotNull(pa);
  36605. ExpectIntGT(ppklen, 0);
  36606. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  36607. ExpectNotNull(pa_oid);
  36608. ExpectNull(pval);
  36609. ExpectIntEQ(pptype, V_ASN1_NULL);
  36610. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  36611. X509_PUBKEY_free(pubKey2);
  36612. X509_free(x509);
  36613. EVP_PKEY_free(evpKey);
  36614. #endif
  36615. return EXPECT_RESULT();
  36616. }
  36617. static int test_wolfSSL_X509_PUBKEY_EC(void)
  36618. {
  36619. EXPECT_DECLS;
  36620. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  36621. X509* x509 = NULL;
  36622. ASN1_OBJECT* obj = NULL;
  36623. ASN1_OBJECT* poid = NULL;
  36624. const ASN1_OBJECT* pa_oid = NULL;
  36625. X509_PUBKEY* pubKey = NULL;
  36626. X509_PUBKEY* pubKey2 = NULL;
  36627. EVP_PKEY* evpKey = NULL;
  36628. const unsigned char *pk = NULL;
  36629. int ppklen;
  36630. int pptype;
  36631. X509_ALGOR *pa = NULL;
  36632. const void *pval;
  36633. char buf[50];
  36634. ExpectNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  36635. SSL_FILETYPE_PEM));
  36636. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  36637. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  36638. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  36639. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  36640. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  36641. ExpectNotNull(pk);
  36642. ExpectNotNull(pa);
  36643. ExpectIntGT(ppklen, 0);
  36644. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  36645. ExpectNotNull(pa_oid);
  36646. ExpectNotNull(pval);
  36647. ExpectIntEQ(pptype, V_ASN1_OBJECT);
  36648. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  36649. poid = (ASN1_OBJECT *)pval;
  36650. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  36651. ExpectIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  36652. X509_PUBKEY_free(pubKey2);
  36653. X509_free(x509);
  36654. EVP_PKEY_free(evpKey);
  36655. #endif
  36656. return EXPECT_RESULT();
  36657. }
  36658. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  36659. {
  36660. EXPECT_DECLS;
  36661. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  36662. word32 bytes;
  36663. #ifdef USE_CERT_BUFFERS_1024
  36664. byte tmp[ONEK_BUF];
  36665. #elif defined(USE_CERT_BUFFERS_2048)
  36666. byte tmp[TWOK_BUF];
  36667. #else
  36668. byte tmp[TWOK_BUF];
  36669. #endif /* END USE_CERT_BUFFERS_1024 */
  36670. const unsigned char* dsaKeyDer = tmp;
  36671. ASN1_OBJECT* obj = NULL;
  36672. ASN1_STRING* str;
  36673. const ASN1_OBJECT* pa_oid = NULL;
  36674. X509_PUBKEY* pubKey = NULL;
  36675. EVP_PKEY* evpKey = NULL;
  36676. const unsigned char *pk = NULL;
  36677. int ppklen, pptype;
  36678. X509_ALGOR *pa = NULL;
  36679. const void *pval;
  36680. #ifdef USE_CERT_BUFFERS_1024
  36681. XMEMSET(tmp, 0, sizeof(tmp));
  36682. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  36683. bytes = sizeof_dsa_key_der_1024;
  36684. #elif defined(USE_CERT_BUFFERS_2048)
  36685. XMEMSET(tmp, 0, sizeof(tmp));
  36686. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  36687. bytes = sizeof_dsa_key_der_2048;
  36688. #else
  36689. {
  36690. XFILE fp = XBADFILE;
  36691. XMEMSET(tmp, 0, sizeof(tmp));
  36692. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  36693. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  36694. if (fp != XBADFILE)
  36695. XFCLOSE(fp);
  36696. }
  36697. #endif
  36698. /* Initialize pkey with der format dsa key */
  36699. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  36700. ExpectNotNull(pubKey = X509_PUBKEY_new());
  36701. ExpectIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  36702. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  36703. ExpectNotNull(pk);
  36704. ExpectNotNull(pa);
  36705. ExpectIntGT(ppklen, 0);
  36706. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  36707. ExpectNotNull(pa_oid);
  36708. ExpectNotNull(pval);
  36709. ExpectIntEQ(pptype, V_ASN1_SEQUENCE);
  36710. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  36711. str = (ASN1_STRING *)pval;
  36712. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  36713. #ifdef USE_CERT_BUFFERS_1024
  36714. ExpectIntEQ(ASN1_STRING_length(str), 291);
  36715. #else
  36716. ExpectIntEQ(ASN1_STRING_length(str), 549);
  36717. #endif /* END USE_CERT_BUFFERS_1024 */
  36718. X509_PUBKEY_free(pubKey);
  36719. EVP_PKEY_free(evpKey);
  36720. #endif
  36721. return EXPECT_RESULT();
  36722. }
  36723. static int test_wolfSSL_BUF(void)
  36724. {
  36725. EXPECT_DECLS;
  36726. #if defined(OPENSSL_EXTRA)
  36727. BUF_MEM* buf = NULL;
  36728. ExpectNotNull(buf = BUF_MEM_new());
  36729. ExpectIntEQ(BUF_MEM_grow(buf, 10), 10);
  36730. ExpectIntEQ(BUF_MEM_grow(buf, -1), 0);
  36731. BUF_MEM_free(buf);
  36732. #endif
  36733. return EXPECT_RESULT();
  36734. }
  36735. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  36736. static int stub_rand_seed(const void *buf, int num)
  36737. {
  36738. (void)buf;
  36739. (void)num;
  36740. return 123;
  36741. }
  36742. static int stub_rand_bytes(unsigned char *buf, int num)
  36743. {
  36744. (void)buf;
  36745. (void)num;
  36746. return 456;
  36747. }
  36748. static byte* was_stub_rand_cleanup_called(void)
  36749. {
  36750. static byte was_called = 0;
  36751. return &was_called;
  36752. }
  36753. static void stub_rand_cleanup(void)
  36754. {
  36755. byte* was_called = was_stub_rand_cleanup_called();
  36756. *was_called = 1;
  36757. return;
  36758. }
  36759. static byte* was_stub_rand_add_called(void)
  36760. {
  36761. static byte was_called = 0;
  36762. return &was_called;
  36763. }
  36764. static int stub_rand_add(const void *buf, int num, double entropy)
  36765. {
  36766. byte* was_called = was_stub_rand_add_called();
  36767. (void)buf;
  36768. (void)num;
  36769. (void)entropy;
  36770. *was_called = 1;
  36771. return 0;
  36772. }
  36773. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  36774. {
  36775. (void)buf;
  36776. (void)num;
  36777. return 9876;
  36778. }
  36779. static int stub_rand_status(void)
  36780. {
  36781. return 5432;
  36782. }
  36783. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  36784. static int test_wolfSSL_RAND_set_rand_method(void)
  36785. {
  36786. EXPECT_DECLS;
  36787. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  36788. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  36789. unsigned char* buf = NULL;
  36790. int num = 0;
  36791. double entropy = 0;
  36792. int ret;
  36793. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  36794. byte* was_add_called = was_stub_rand_add_called();
  36795. ExpectNotNull(buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  36796. DYNAMIC_TYPE_TMP_BUFFER));
  36797. ExpectIntNE(wolfSSL_RAND_status(), 5432);
  36798. ExpectIntEQ(*was_cleanup_called, 0);
  36799. RAND_cleanup();
  36800. ExpectIntEQ(*was_cleanup_called, 0);
  36801. rand_methods.seed = &stub_rand_seed;
  36802. rand_methods.bytes = &stub_rand_bytes;
  36803. rand_methods.cleanup = &stub_rand_cleanup;
  36804. rand_methods.add = &stub_rand_add;
  36805. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  36806. rand_methods.status = &stub_rand_status;
  36807. ExpectIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  36808. ExpectIntEQ(RAND_seed(buf, num), 123);
  36809. ExpectIntEQ(RAND_bytes(buf, num), 456);
  36810. ExpectIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  36811. ExpectIntEQ(RAND_status(), 5432);
  36812. ExpectIntEQ(*was_add_called, 0);
  36813. /* The function pointer for RAND_add returns int, but RAND_add itself
  36814. * returns void. */
  36815. RAND_add(buf, num, entropy);
  36816. ExpectIntEQ(*was_add_called, 1);
  36817. was_add_called = 0;
  36818. ExpectIntEQ(*was_cleanup_called, 0);
  36819. RAND_cleanup();
  36820. ExpectIntEQ(*was_cleanup_called, 1);
  36821. *was_cleanup_called = 0;
  36822. ret = RAND_set_rand_method(NULL);
  36823. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  36824. ExpectIntNE(RAND_status(), 5432);
  36825. ExpectIntEQ(*was_cleanup_called, 0);
  36826. RAND_cleanup();
  36827. ExpectIntEQ(*was_cleanup_called, 0);
  36828. RAND_set_rand_method(NULL);
  36829. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36830. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  36831. return EXPECT_RESULT();
  36832. }
  36833. static int test_wolfSSL_RAND_bytes(void)
  36834. {
  36835. EXPECT_DECLS;
  36836. #if defined(OPENSSL_EXTRA)
  36837. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  36838. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  36839. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  36840. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  36841. int max_bufsize;
  36842. byte *my_buf = NULL;
  36843. /* sanity check */
  36844. ExpectIntEQ(RAND_bytes(NULL, 16), 0);
  36845. ExpectIntEQ(RAND_bytes(NULL, 0), 0);
  36846. max_bufsize = size4;
  36847. ExpectNotNull(my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  36848. DYNAMIC_TYPE_TMP_BUFFER));
  36849. ExpectIntEQ(RAND_bytes(my_buf, 0), 1);
  36850. ExpectIntEQ(RAND_bytes(my_buf, -1), 0);
  36851. ExpectNotNull(XMEMSET(my_buf, 0, max_bufsize));
  36852. ExpectIntEQ(RAND_bytes(my_buf, size1), 1);
  36853. ExpectIntEQ(RAND_bytes(my_buf, size2), 1);
  36854. ExpectIntEQ(RAND_bytes(my_buf, size3), 1);
  36855. ExpectIntEQ(RAND_bytes(my_buf, size4), 1);
  36856. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36857. #endif
  36858. return EXPECT_RESULT();
  36859. }
  36860. static int test_wolfSSL_RAND(void)
  36861. {
  36862. EXPECT_DECLS;
  36863. #if defined(OPENSSL_EXTRA)
  36864. byte seed[16];
  36865. XMEMSET(seed, 0, sizeof(seed));
  36866. /* No global methods set. */
  36867. ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
  36868. ExpectIntEQ(RAND_poll(), 1);
  36869. RAND_cleanup();
  36870. ExpectIntEQ(RAND_egd(NULL), -1);
  36871. #ifndef NO_FILESYSTEM
  36872. {
  36873. char fname[100];
  36874. ExpectNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  36875. ExpectIntEQ(RAND_write_file(NULL), 0);
  36876. }
  36877. #endif
  36878. #endif
  36879. return EXPECT_RESULT();
  36880. }
  36881. static int test_wolfSSL_PKCS8_Compat(void)
  36882. {
  36883. EXPECT_DECLS;
  36884. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC) && \
  36885. !defined(NO_BIO)
  36886. PKCS8_PRIV_KEY_INFO* pt = NULL;
  36887. BIO* bio = NULL;
  36888. XFILE f = XBADFILE;
  36889. int bytes;
  36890. char pkcs8_buffer[512];
  36891. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  36892. EVP_PKEY *pkey = NULL;
  36893. #endif
  36894. /* file from wolfssl/certs/ directory */
  36895. ExpectTrue((f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb")) != XBADFILE);
  36896. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)),
  36897. 0);
  36898. if (f != XBADFILE)
  36899. XFCLOSE(f);
  36900. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  36901. ExpectNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  36902. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  36903. ExpectNotNull(pkey = EVP_PKCS82PKEY(pt));
  36904. ExpectIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  36905. /* gets PKCS8 pointer to pkey */
  36906. ExpectNotNull(EVP_PKEY2PKCS8(pkey));
  36907. EVP_PKEY_free(pkey);
  36908. #endif
  36909. BIO_free(bio);
  36910. PKCS8_PRIV_KEY_INFO_free(pt);
  36911. #endif
  36912. return EXPECT_RESULT();
  36913. }
  36914. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  36915. static int NoPasswordCallBack(char* passwd, int sz, int rw, void* userdata)
  36916. {
  36917. (void)passwd;
  36918. (void)sz;
  36919. (void)rw;
  36920. (void)userdata;
  36921. return -1;
  36922. }
  36923. #endif
  36924. static int test_wolfSSL_PKCS8_d2i(void)
  36925. {
  36926. EXPECT_DECLS;
  36927. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  36928. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  36929. * requires a 12 byte password in FIPS mode. This test ends up
  36930. * trying to use an 8 byte password. */
  36931. #ifndef NO_FILESYSTEM
  36932. unsigned char pkcs8_buffer[2048];
  36933. const unsigned char* p = NULL;
  36934. int bytes = 0;
  36935. XFILE file = XBADFILE;
  36936. WOLFSSL_EVP_PKEY* pkey = NULL;
  36937. #ifndef NO_BIO
  36938. BIO* bio = NULL;
  36939. #if defined(OPENSSL_ALL) && \
  36940. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  36941. defined(HAVE_ECC)) && \
  36942. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  36943. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  36944. #endif
  36945. #endif
  36946. #ifndef NO_RSA
  36947. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  36948. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  36949. #ifndef NO_DES3
  36950. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  36951. #endif
  36952. #endif /* NO_RSA */
  36953. #ifdef HAVE_ECC
  36954. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  36955. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  36956. #ifndef NO_DES3
  36957. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  36958. #endif
  36959. #endif /* HAVE_ECC */
  36960. #endif /* !NO_FILESYSTEM */
  36961. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  36962. #ifndef NO_RSA
  36963. #ifdef USE_CERT_BUFFERS_1024
  36964. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  36965. int rsaSz = sizeof_server_key_der_1024;
  36966. #else
  36967. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  36968. int rsaSz = sizeof_server_key_der_2048;
  36969. #endif
  36970. #endif
  36971. #ifdef HAVE_ECC
  36972. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  36973. int ecSz = sizeof_ecc_key_der_256;
  36974. #endif
  36975. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  36976. #ifndef NO_FILESYSTEM
  36977. (void)pkcs8_buffer;
  36978. (void)p;
  36979. (void)bytes;
  36980. (void)file;
  36981. #ifndef NO_BIO
  36982. (void)bio;
  36983. #endif
  36984. #endif
  36985. #ifdef OPENSSL_ALL
  36986. #ifndef NO_RSA
  36987. /* Try to auto-detect normal RSA private key */
  36988. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  36989. EVP_PKEY_free(pkey);
  36990. pkey = NULL;
  36991. #endif
  36992. #ifdef HAVE_ECC
  36993. /* Try to auto-detect normal EC private key */
  36994. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  36995. EVP_PKEY_free(pkey);
  36996. pkey = NULL;
  36997. #endif
  36998. #endif /* OPENSSL_ALL */
  36999. #ifndef NO_FILESYSTEM
  37000. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37001. ExpectIntEQ(PEM_write_PKCS8PrivateKey(XBADFILE, pkey, NULL, NULL, 0, NULL,
  37002. NULL), 0);
  37003. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  37004. NULL), 0);
  37005. #endif
  37006. #ifndef NO_RSA
  37007. /* Get DER encoded RSA PKCS#8 data. */
  37008. ExpectTrue((file = XFOPEN(rsaDerPkcs8File, "rb")) != XBADFILE);
  37009. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37010. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37011. file)), 0);
  37012. if (file != XBADFILE) {
  37013. XFCLOSE(file);
  37014. file = XBADFILE;
  37015. }
  37016. p = pkcs8_buffer;
  37017. #ifdef OPENSSL_ALL
  37018. /* Try to decode - auto-detect key type. */
  37019. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  37020. #else
  37021. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  37022. #endif
  37023. /* Get PEM encoded RSA PKCS#8 data. */
  37024. ExpectTrue((file = XFOPEN(rsaPemPkcs8File, "rb")) != XBADFILE);
  37025. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37026. file)), 0);
  37027. if (file != XBADFILE) {
  37028. XFCLOSE(file);
  37029. file = XBADFILE;
  37030. }
  37031. #if defined(OPENSSL_ALL) && \
  37032. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37033. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37034. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
  37035. NULL), 0);
  37036. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  37037. NULL), 0);
  37038. /* Write PKCS#8 PEM to BIO. */
  37039. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  37040. NULL), bytes);
  37041. /* Write PKCS#8 PEM to stderr. */
  37042. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, NULL, NULL, 0, NULL,
  37043. NULL), bytes);
  37044. /* Compare file and written data */
  37045. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  37046. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  37047. BIO_free(bio);
  37048. bio = NULL;
  37049. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  37050. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_gcm(),
  37051. NULL, 0, PasswordCallBack, (void*)"yassl123"), 0);
  37052. #endif
  37053. #if !defined(NO_DES3) && !defined(NO_SHA)
  37054. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37055. /* Write Encrypted PKCS#8 PEM to BIO. */
  37056. bytes = 1834;
  37057. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  37058. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37059. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_des_ede3_cbc(),
  37060. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37061. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  37062. (void*)"yassl123"));
  37063. EVP_PKEY_free(evpPkey);
  37064. evpPkey = NULL;
  37065. BIO_free(bio);
  37066. bio = NULL;
  37067. #endif /* !NO_DES3 && !NO_SHA */
  37068. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37069. EVP_PKEY_free(pkey);
  37070. pkey = NULL;
  37071. /* PKCS#8 encrypted RSA key */
  37072. #ifndef NO_DES3
  37073. ExpectTrue((file = XFOPEN(rsaDerPkcs8EncFile, "rb")) != XBADFILE);
  37074. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37075. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37076. file)), 0);
  37077. if (file != XBADFILE) {
  37078. XFCLOSE(file);
  37079. file = XBADFILE;
  37080. }
  37081. #if defined(OPENSSL_ALL) && \
  37082. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37083. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  37084. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  37085. (void*)"yassl123"));
  37086. EVP_PKEY_free(pkey);
  37087. pkey = NULL;
  37088. BIO_free(bio);
  37089. bio = NULL;
  37090. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37091. #endif /* !NO_DES3 */
  37092. #endif /* NO_RSA */
  37093. #ifdef HAVE_ECC
  37094. /* PKCS#8 encode EC key */
  37095. ExpectTrue((file = XFOPEN(ecDerPkcs8File, "rb")) != XBADFILE);
  37096. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37097. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37098. file)), 0);
  37099. if (file != XBADFILE) {
  37100. XFCLOSE(file);
  37101. file = XBADFILE;
  37102. }
  37103. p = pkcs8_buffer;
  37104. #ifdef OPENSSL_ALL
  37105. /* Try to decode - auto-detect key type. */
  37106. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  37107. #else
  37108. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  37109. #endif
  37110. /* Get PEM encoded RSA PKCS#8 data. */
  37111. ExpectTrue((file = XFOPEN(ecPemPkcs8File, "rb")) != XBADFILE);
  37112. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37113. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37114. file)), 0);
  37115. if (file != XBADFILE) {
  37116. XFCLOSE(file);
  37117. file = XBADFILE;
  37118. }
  37119. #if defined(OPENSSL_ALL) && \
  37120. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  37121. defined(HAVE_AES_CBC)
  37122. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37123. /* Write PKCS#8 PEM to BIO. */
  37124. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  37125. NULL), bytes);
  37126. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, NULL, NULL, 0, NULL,
  37127. NULL), bytes);
  37128. /* Compare file and written data */
  37129. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  37130. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  37131. BIO_free(bio);
  37132. bio = NULL;
  37133. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37134. /* Write Encrypted PKCS#8 PEM to BIO. */
  37135. bytes = 379;
  37136. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  37137. NULL, 0, NoPasswordCallBack, (void*)"yassl123"), 0);
  37138. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  37139. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37140. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_cbc(),
  37141. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37142. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_cbc(),
  37143. (char*)"yassl123", 8, PasswordCallBack, NULL), bytes);
  37144. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_256_cbc(),
  37145. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37146. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  37147. (void*)"yassl123"));
  37148. EVP_PKEY_free(evpPkey);
  37149. evpPkey = NULL;
  37150. BIO_free(bio);
  37151. bio = NULL;
  37152. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  37153. EVP_PKEY_free(pkey);
  37154. pkey = NULL;
  37155. /* PKCS#8 encrypted EC key */
  37156. #ifndef NO_DES3
  37157. ExpectTrue((file = XFOPEN(ecDerPkcs8EncFile, "rb")) != XBADFILE);
  37158. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37159. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37160. file)), 0);
  37161. if (file != XBADFILE) {
  37162. XFCLOSE(file);
  37163. file = XBADFILE;
  37164. }
  37165. #if defined(OPENSSL_ALL) && \
  37166. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37167. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  37168. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  37169. (void*)"yassl123"));
  37170. EVP_PKEY_free(pkey);
  37171. pkey = NULL;
  37172. BIO_free(bio);
  37173. bio = NULL;
  37174. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37175. #endif /* !NO_DES3 */
  37176. #endif /* HAVE_ECC */
  37177. #endif /* !NO_FILESYSTEM */
  37178. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  37179. return EXPECT_RESULT();
  37180. }
  37181. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  37182. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  37183. #define LOGGING_THREADS 5
  37184. #define ERROR_COUNT 10
  37185. /* copied from logging.c since this is not exposed otherwise */
  37186. #ifndef ERROR_QUEUE_MAX
  37187. #ifdef ERROR_QUEUE_PER_THREAD
  37188. #define ERROR_QUEUE_MAX 16
  37189. #else
  37190. /* this breaks from compat of unlimited error queue size */
  37191. #define ERROR_QUEUE_MAX 100
  37192. #endif
  37193. #endif
  37194. static volatile int loggingThreadsReady;
  37195. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  37196. {
  37197. const char* file;
  37198. int line;
  37199. unsigned long err;
  37200. int errorCount = 0;
  37201. int i;
  37202. (void)args;
  37203. while (!loggingThreadsReady);
  37204. for (i = 0; i < ERROR_COUNT; i++)
  37205. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  37206. while ((err = ERR_get_error_line(&file, &line))) {
  37207. AssertIntEQ(err, 990 + errorCount);
  37208. errorCount++;
  37209. }
  37210. AssertIntEQ(errorCount, ERROR_COUNT);
  37211. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  37212. ERR_clear_error(); /* ERR_get_error_line() does not remove */
  37213. errorCount = 0;
  37214. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  37215. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  37216. while ((err = ERR_get_error_line(&file, &line))) {
  37217. AssertIntEQ(err, 990 + errorCount);
  37218. errorCount++;
  37219. }
  37220. /* test that the 3 errors over the max were dropped */
  37221. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  37222. WOLFSSL_RETURN_FROM_THREAD(0);
  37223. }
  37224. #endif
  37225. static int test_error_queue_per_thread(void)
  37226. {
  37227. int res = TEST_SKIPPED;
  37228. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  37229. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  37230. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  37231. int i;
  37232. ERR_clear_error(); /* clear out any error nodes */
  37233. loggingThreadsReady = 0;
  37234. for (i = 0; i < LOGGING_THREADS; i++)
  37235. start_thread(test_logging, NULL, &loggingThreads[i]);
  37236. loggingThreadsReady = 1;
  37237. for (i = 0; i < LOGGING_THREADS; i++)
  37238. join_thread(loggingThreads[i]);
  37239. res = TEST_SUCCESS;
  37240. #endif
  37241. return res;
  37242. }
  37243. static int test_wolfSSL_ERR_put_error(void)
  37244. {
  37245. EXPECT_DECLS;
  37246. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37247. defined(DEBUG_WOLFSSL)
  37248. const char* file;
  37249. int line;
  37250. ERR_clear_error(); /* clear out any error nodes */
  37251. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  37252. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  37253. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  37254. ExpectIntEQ(ERR_get_error_line(&file, &line), 1);
  37255. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  37256. ExpectIntEQ(ERR_get_error_line(&file, &line), 2);
  37257. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  37258. ExpectIntEQ(ERR_get_error_line(&file, &line), 3);
  37259. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  37260. ExpectIntEQ(ERR_get_error_line(&file, &line), 4);
  37261. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  37262. ExpectIntEQ(ERR_get_error_line(&file, &line), 5);
  37263. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  37264. ExpectIntEQ(ERR_get_error_line(&file, &line), 6);
  37265. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  37266. ExpectIntEQ(ERR_get_error_line(&file, &line), 7);
  37267. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  37268. ExpectIntEQ(ERR_get_error_line(&file, &line), 8);
  37269. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  37270. ExpectIntEQ(ERR_get_error_line(&file, &line), 9);
  37271. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  37272. ExpectIntEQ(ERR_get_error_line(&file, &line), 10);
  37273. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  37274. ExpectIntEQ(ERR_get_error_line(&file, &line), 11);
  37275. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  37276. ExpectIntEQ(ERR_get_error_line(&file, &line), 12);
  37277. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  37278. ExpectIntEQ(ERR_get_error_line(&file, &line), 13);
  37279. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  37280. ExpectIntEQ(ERR_get_error_line(&file, &line), 14);
  37281. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  37282. ExpectIntEQ(ERR_get_error_line(&file, &line), 15);
  37283. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  37284. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  37285. "this file", 100);
  37286. ExpectIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  37287. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  37288. ExpectIntEQ(line, 100);
  37289. ExpectIntEQ(wolfSSL_ERR_peek_error(),
  37290. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  37291. ExpectIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  37292. #endif
  37293. /* try reading past end of error queue */
  37294. file = NULL;
  37295. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  37296. ExpectNull(file);
  37297. ExpectIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  37298. PEMerr(4,4);
  37299. ExpectIntEQ(ERR_get_error(), 4);
  37300. /* Empty and free up all error nodes */
  37301. ERR_clear_error();
  37302. /* Verify all nodes are cleared */
  37303. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  37304. ERR_clear_error();
  37305. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  37306. #endif
  37307. return EXPECT_RESULT();
  37308. }
  37309. /*
  37310. * This is a regression test for a bug where the peek/get error functions were
  37311. * drawing from the end of the queue rather than the front.
  37312. */
  37313. static int test_wolfSSL_ERR_get_error_order(void)
  37314. {
  37315. EXPECT_DECLS;
  37316. #if defined(WOLFSSL_HAVE_ERROR_QUEUE) && defined(OPENSSL_EXTRA)
  37317. /* Empty the queue. */
  37318. wolfSSL_ERR_clear_error();
  37319. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  37320. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  37321. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  37322. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  37323. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  37324. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  37325. #endif /* WOLFSSL_HAVE_ERROR_QUEUE && OPENSSL_EXTRA */
  37326. return EXPECT_RESULT();
  37327. }
  37328. #ifndef NO_BIO
  37329. static int test_wolfSSL_ERR_print_errors(void)
  37330. {
  37331. EXPECT_DECLS;
  37332. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37333. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  37334. BIO* bio = NULL;
  37335. char buf[1024];
  37336. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37337. ERR_clear_error(); /* clear out any error nodes */
  37338. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  37339. /* Choosing -600 as an unused errno. */
  37340. ERR_put_error(0,SYS_F_BIND, -600, "asn.c", 100);
  37341. ERR_print_errors(bio);
  37342. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  37343. ExpectIntEQ(XSTRNCMP(
  37344. "error:173:wolfSSL library:Bad function argument:ssl.c:0",
  37345. buf, 55), 0);
  37346. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  37347. ExpectIntEQ(XSTRNCMP(
  37348. "error:600:wolfSSL library:unknown error number:asn.c:100",
  37349. buf, 56), 0);
  37350. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  37351. ExpectIntEQ(buf[0], '\0');
  37352. ExpectIntEQ(ERR_get_error_line(NULL, NULL), 0);
  37353. BIO_free(bio);
  37354. #endif
  37355. return EXPECT_RESULT();
  37356. }
  37357. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37358. defined(DEBUG_WOLFSSL)
  37359. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  37360. {
  37361. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  37362. return 0;
  37363. }
  37364. #endif
  37365. static int test_wolfSSL_ERR_print_errors_cb(void)
  37366. {
  37367. EXPECT_DECLS;
  37368. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37369. defined(DEBUG_WOLFSSL)
  37370. BIO* bio = NULL;
  37371. char buf[1024];
  37372. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37373. ERR_clear_error(); /* clear out any error nodes */
  37374. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  37375. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  37376. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  37377. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  37378. ExpectIntEQ(XSTRNCMP(
  37379. "wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  37380. buf, 53), 0);
  37381. ExpectIntEQ(XSTRNCMP(
  37382. "wolfSSL error occurred, error = 275 line:100 file:asn.c",
  37383. buf + 53, 55), 0);
  37384. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  37385. BIO_free(bio);
  37386. #endif
  37387. return EXPECT_RESULT();
  37388. }
  37389. /*
  37390. * Testing WOLFSSL_ERROR_MSG
  37391. */
  37392. static int test_WOLFSSL_ERROR_MSG(void)
  37393. {
  37394. int res = TEST_SKIPPED;
  37395. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  37396. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  37397. const char* msg = TEST_STRING;
  37398. WOLFSSL_ERROR_MSG(msg);
  37399. res = TEST_SUCCESS;
  37400. #endif
  37401. return res;
  37402. } /* End test_WOLFSSL_ERROR_MSG */
  37403. /*
  37404. * Testing wc_ERR_remove_state
  37405. */
  37406. static int test_wc_ERR_remove_state(void)
  37407. {
  37408. int res = TEST_SKIPPED;
  37409. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  37410. wc_ERR_remove_state();
  37411. res = TEST_SUCCESS;
  37412. #endif
  37413. return res;
  37414. } /* End test_wc_ERR_remove_state */
  37415. /*
  37416. * Testing wc_ERR_print_errors_fp
  37417. */
  37418. static int test_wc_ERR_print_errors_fp(void)
  37419. {
  37420. EXPECT_DECLS;
  37421. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  37422. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  37423. long sz;
  37424. XFILE fp = XBADFILE;
  37425. WOLFSSL_ERROR(BAD_FUNC_ARG);
  37426. ExpectTrue((fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar")) !=
  37427. XBADFILE);
  37428. wc_ERR_print_errors_fp(fp);
  37429. #if defined(DEBUG_WOLFSSL)
  37430. ExpectTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  37431. #ifdef NO_ERROR_QUEUE
  37432. ExpectIntEQ(sz = XFTELL(fp), 0);
  37433. #else
  37434. ExpectIntNE(sz = XFTELL(fp), 0);
  37435. #endif
  37436. #endif
  37437. if (fp != XBADFILE)
  37438. XFCLOSE(fp);
  37439. (void)sz;
  37440. #endif
  37441. return EXPECT_RESULT();
  37442. } /* End test_wc_ERR_print_errors_fp */
  37443. #ifdef DEBUG_WOLFSSL
  37444. static void Logging_cb(const int logLevel, const char *const logMessage)
  37445. {
  37446. (void)logLevel;
  37447. (void)logMessage;
  37448. }
  37449. #endif
  37450. /*
  37451. * Testing wolfSSL_GetLoggingCb
  37452. */
  37453. static int test_wolfSSL_GetLoggingCb(void)
  37454. {
  37455. EXPECT_DECLS;
  37456. #ifdef DEBUG_WOLFSSL
  37457. /* Testing without wolfSSL_SetLoggingCb() */
  37458. ExpectNull(wolfSSL_GetLoggingCb());
  37459. /* Testing with wolfSSL_SetLoggingCb() */
  37460. ExpectIntEQ(wolfSSL_SetLoggingCb(Logging_cb), 0);
  37461. ExpectNotNull(wolfSSL_GetLoggingCb());
  37462. ExpectIntEQ(wolfSSL_SetLoggingCb(NULL), 0);
  37463. #endif
  37464. ExpectNull(wolfSSL_GetLoggingCb());
  37465. return EXPECT_RESULT();
  37466. } /* End test_wolfSSL_GetLoggingCb */
  37467. #endif /* !NO_BIO */
  37468. static int test_wolfSSL_MD4(void)
  37469. {
  37470. EXPECT_DECLS;
  37471. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  37472. MD4_CTX md4;
  37473. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  37474. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  37475. "789012345678901234567890";
  37476. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  37477. "\xcc\x05\x36";
  37478. int msgSz = (int)XSTRLEN(msg);
  37479. XMEMSET(out, 0, sizeof(out));
  37480. MD4_Init(&md4);
  37481. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  37482. MD4_Final(out, &md4);
  37483. ExpectIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  37484. #endif
  37485. return EXPECT_RESULT();
  37486. }
  37487. static int test_wolfSSL_MD5(void)
  37488. {
  37489. EXPECT_DECLS;
  37490. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  37491. byte input1[] = "";
  37492. byte input2[] = "message digest";
  37493. byte hash[WC_MD5_DIGEST_SIZE];
  37494. unsigned char output1[] =
  37495. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  37496. unsigned char output2[] =
  37497. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  37498. WOLFSSL_MD5_CTX md5;
  37499. XMEMSET(&md5, 0, sizeof(md5));
  37500. /* Test cases for illegal parameters */
  37501. ExpectIntEQ(MD5_Init(NULL), 0);
  37502. ExpectIntEQ(MD5_Init(&md5), 1);
  37503. ExpectIntEQ(MD5_Update(NULL, input1, 0), 0);
  37504. ExpectIntEQ(MD5_Update(NULL, NULL, 0), 0);
  37505. ExpectIntEQ(MD5_Update(&md5, NULL, 1), 0);
  37506. ExpectIntEQ(MD5_Final(NULL, &md5), 0);
  37507. ExpectIntEQ(MD5_Final(hash, NULL), 0);
  37508. ExpectIntEQ(MD5_Final(NULL, NULL), 0);
  37509. /* Init MD5 CTX */
  37510. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  37511. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input1, XSTRLEN((const char*)&input1)),
  37512. 1);
  37513. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  37514. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  37515. /* Init MD5 CTX */
  37516. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  37517. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input2,
  37518. (int)XSTRLEN((const char*)input2)), 1);
  37519. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  37520. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  37521. #if !defined(NO_OLD_NAMES) && \
  37522. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  37523. ExpectPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  37524. ExpectPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  37525. ExpectPtrNE(MD5(input1, 1, NULL), NULL);
  37526. ExpectPtrNE(MD5(NULL, 0, NULL), NULL);
  37527. ExpectPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash),
  37528. &hash);
  37529. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  37530. ExpectPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash),
  37531. &hash);
  37532. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  37533. {
  37534. byte data[] = "Data to be hashed.";
  37535. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  37536. ExpectNotNull(MD5(data, sizeof(data), NULL));
  37537. ExpectNotNull(MD5(data, sizeof(data), hash));
  37538. ExpectNotNull(MD5(NULL, 0, hash));
  37539. ExpectNull(MD5(NULL, sizeof(data), hash));
  37540. }
  37541. #endif
  37542. #endif
  37543. return EXPECT_RESULT();
  37544. }
  37545. static int test_wolfSSL_MD5_Transform(void)
  37546. {
  37547. EXPECT_DECLS;
  37548. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  37549. byte input1[] = "";
  37550. byte input2[] = "abc";
  37551. byte local[WC_MD5_BLOCK_SIZE];
  37552. word32 sLen = 0;
  37553. #ifdef BIG_ENDIAN_ORDER
  37554. unsigned char output1[] =
  37555. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  37556. unsigned char output2[] =
  37557. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  37558. #else
  37559. unsigned char output1[] =
  37560. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  37561. unsigned char output2[] =
  37562. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  37563. #endif
  37564. union {
  37565. wc_Md5 native;
  37566. MD5_CTX compat;
  37567. } md5;
  37568. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  37569. XMEMSET(&local, 0, sizeof(local));
  37570. /* sanity check */
  37571. ExpectIntEQ(MD5_Transform(NULL, NULL), 0);
  37572. ExpectIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  37573. ExpectIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  37574. ExpectIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  37575. ExpectIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37576. ExpectIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  37577. /* Init MD5 CTX */
  37578. ExpectIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  37579. /* Do Transform*/
  37580. sLen = (word32)XSTRLEN((char*)input1);
  37581. XMEMCPY(local, input1, sLen);
  37582. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  37583. ExpectIntEQ(XMEMCMP(md5.native.digest, output1, WC_MD5_DIGEST_SIZE), 0);
  37584. /* Init MD5 CTX */
  37585. ExpectIntEQ(MD5_Init(&md5.compat), 1);
  37586. sLen = (word32)XSTRLEN((char*)input2);
  37587. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  37588. XMEMCPY(local, input2, sLen);
  37589. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  37590. ExpectIntEQ(XMEMCMP(md5.native.digest, output2, WC_MD5_DIGEST_SIZE), 0);
  37591. #endif
  37592. return EXPECT_RESULT();
  37593. }
  37594. static int test_wolfSSL_SHA(void)
  37595. {
  37596. EXPECT_DECLS;
  37597. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  37598. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  37599. (!defined(HAVE_FIPS) || \
  37600. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  37601. {
  37602. const unsigned char in[] = "abc";
  37603. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  37604. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  37605. unsigned char out[WC_SHA_DIGEST_SIZE];
  37606. unsigned char* p = NULL;
  37607. WOLFSSL_SHA_CTX sha;
  37608. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  37609. ExpectNotNull(SHA1(in, XSTRLEN((char*)in), out));
  37610. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37611. /* SHA interface test */
  37612. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  37613. ExpectNull(SHA(NULL, XSTRLEN((char*)in), out));
  37614. ExpectNotNull(SHA(in, 0, out));
  37615. ExpectNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  37616. ExpectNotNull(SHA(NULL, 0, out));
  37617. ExpectNotNull(SHA(NULL, 0, NULL));
  37618. ExpectNotNull(SHA(in, XSTRLEN((char*)in), out));
  37619. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37620. ExpectNotNull(p = SHA(in, XSTRLEN((char*)in), NULL));
  37621. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA_DIGEST_SIZE), 0);
  37622. ExpectIntEQ(wolfSSL_SHA_Init(&sha), 1);
  37623. ExpectIntEQ(wolfSSL_SHA_Update(&sha, in, XSTRLEN((char*)in)), 1);
  37624. ExpectIntEQ(wolfSSL_SHA_Final(out, &sha), 1);
  37625. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37626. ExpectIntEQ(wolfSSL_SHA1_Init(&sha), 1);
  37627. ExpectIntEQ(wolfSSL_SHA1_Update(&sha, in, XSTRLEN((char*)in)), 1);
  37628. ExpectIntEQ(wolfSSL_SHA1_Final(out, &sha), 1);
  37629. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37630. }
  37631. #endif
  37632. #if !defined(NO_SHA256)
  37633. {
  37634. const unsigned char in[] = "abc";
  37635. unsigned char expected[] =
  37636. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  37637. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  37638. "\x15\xAD";
  37639. unsigned char out[WC_SHA256_DIGEST_SIZE];
  37640. unsigned char* p = NULL;
  37641. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  37642. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37643. ExpectNotNull(SHA256(in, XSTRLEN((char*)in), out));
  37644. #else
  37645. ExpectNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  37646. #endif
  37647. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  37648. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37649. ExpectNotNull(p = SHA256(in, XSTRLEN((char*)in), NULL));
  37650. #else
  37651. ExpectNotNull(p = wolfSSL_SHA256(in, XSTRLEN((char*)in), NULL));
  37652. #endif
  37653. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA256_DIGEST_SIZE), 0);
  37654. }
  37655. #endif
  37656. #if defined(WOLFSSL_SHA384)
  37657. {
  37658. const unsigned char in[] = "abc";
  37659. unsigned char expected[] =
  37660. "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  37661. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  37662. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  37663. "\xc8\x25\xa7";
  37664. unsigned char out[WC_SHA384_DIGEST_SIZE];
  37665. unsigned char* p = NULL;
  37666. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  37667. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37668. ExpectNotNull(SHA384(in, XSTRLEN((char*)in), out));
  37669. #else
  37670. ExpectNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  37671. #endif
  37672. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  37673. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37674. ExpectNotNull(p = SHA384(in, XSTRLEN((char*)in), NULL));
  37675. #else
  37676. ExpectNotNull(p = wolfSSL_SHA384(in, XSTRLEN((char*)in), NULL));
  37677. #endif
  37678. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA384_DIGEST_SIZE), 0);
  37679. }
  37680. #endif
  37681. #if defined(WOLFSSL_SHA512)
  37682. {
  37683. const unsigned char in[] = "abc";
  37684. unsigned char expected[] =
  37685. "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  37686. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  37687. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  37688. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  37689. "\xa5\x4c\xa4\x9f";
  37690. unsigned char out[WC_SHA512_DIGEST_SIZE];
  37691. unsigned char* p = NULL;
  37692. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  37693. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37694. ExpectNotNull(SHA512(in, XSTRLEN((char*)in), out));
  37695. #else
  37696. ExpectNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  37697. #endif
  37698. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  37699. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37700. ExpectNotNull(p = SHA512(in, XSTRLEN((char*)in), NULL));
  37701. #else
  37702. ExpectNotNull(p = wolfSSL_SHA512(in, XSTRLEN((char*)in), NULL));
  37703. #endif
  37704. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA512_DIGEST_SIZE), 0);
  37705. }
  37706. #endif
  37707. #endif
  37708. return EXPECT_RESULT();
  37709. }
  37710. static int test_wolfSSL_SHA_Transform(void)
  37711. {
  37712. EXPECT_DECLS;
  37713. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  37714. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37715. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  37716. byte input1[] = "";
  37717. byte input2[] = "abc";
  37718. byte local[WC_SHA_BLOCK_SIZE];
  37719. word32 sLen = 0;
  37720. #ifdef BIG_ENDIAN_ORDER
  37721. unsigned char output1[] =
  37722. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  37723. "\x09\xf7\x3a\x93";
  37724. unsigned char output2[] =
  37725. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  37726. "\x7c\x6e\x08\xb8";
  37727. #else
  37728. unsigned char output1[] =
  37729. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  37730. "\x93\x3a\xf7\x09";
  37731. unsigned char output2[] =
  37732. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  37733. "\xb8\x08\x6e\x7c";
  37734. #endif
  37735. union {
  37736. wc_Sha native;
  37737. SHA_CTX compat;
  37738. } sha;
  37739. union {
  37740. wc_Sha native;
  37741. SHA_CTX compat;
  37742. } sha1;
  37743. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  37744. XMEMSET(&local, 0, sizeof(local));
  37745. /* sanity check */
  37746. ExpectIntEQ(SHA_Transform(NULL, NULL), 0);
  37747. ExpectIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  37748. ExpectIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  37749. ExpectIntEQ(SHA1_Transform(NULL, NULL), 0);
  37750. ExpectIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  37751. ExpectIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  37752. ExpectIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  37753. ExpectIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37754. ExpectIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  37755. /* Init SHA CTX */
  37756. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  37757. /* Do Transform*/
  37758. sLen = (word32)XSTRLEN((char*)input1);
  37759. XMEMCPY(local, input1, sLen);
  37760. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  37761. ExpectIntEQ(XMEMCMP(sha.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  37762. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  37763. /* Init SHA CTX */
  37764. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  37765. sLen = (word32)XSTRLEN((char*)input2);
  37766. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37767. XMEMCPY(local, input2, sLen);
  37768. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  37769. ExpectIntEQ(XMEMCMP(sha.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  37770. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  37771. /* SHA1 */
  37772. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37773. /* Init SHA CTX */
  37774. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  37775. /* Do Transform*/
  37776. sLen = (word32)XSTRLEN((char*)input1);
  37777. XMEMCPY(local, input1, sLen);
  37778. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  37779. ExpectIntEQ(XMEMCMP(sha1.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  37780. ExpectIntEQ(SHA1_Final(local, &sha1.compat), 1); /* frees resources */
  37781. /* Init SHA CTX */
  37782. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  37783. sLen = (word32)XSTRLEN((char*)input2);
  37784. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37785. XMEMCPY(local, input2, sLen);
  37786. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  37787. ExpectIntEQ(XMEMCMP(sha1.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  37788. ExpectIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  37789. #endif
  37790. #endif
  37791. return EXPECT_RESULT();
  37792. }
  37793. static int test_wolfSSL_SHA224(void)
  37794. {
  37795. EXPECT_DECLS;
  37796. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  37797. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37798. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  37799. unsigned char input[] =
  37800. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  37801. unsigned char output[] =
  37802. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  37803. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  37804. size_t inLen;
  37805. byte hash[WC_SHA224_DIGEST_SIZE];
  37806. unsigned char* p;
  37807. inLen = XSTRLEN((char*)input);
  37808. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  37809. ExpectNull(SHA224(NULL, inLen, hash));
  37810. ExpectNotNull(SHA224(input, 0, hash));
  37811. ExpectNotNull(SHA224(input, inLen, NULL));
  37812. ExpectNotNull(SHA224(NULL, 0, hash));
  37813. ExpectNotNull(SHA224(NULL, 0, NULL));
  37814. ExpectNotNull(SHA224(input, inLen, hash));
  37815. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  37816. ExpectNotNull(p = SHA224(input, inLen, NULL));
  37817. ExpectIntEQ(XMEMCMP(p, output, WC_SHA224_DIGEST_SIZE), 0);
  37818. #endif
  37819. return EXPECT_RESULT();
  37820. }
  37821. static int test_wolfSSL_SHA256(void)
  37822. {
  37823. EXPECT_DECLS;
  37824. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  37825. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  37826. unsigned char input[] =
  37827. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  37828. unsigned char output[] =
  37829. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  37830. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  37831. "\x06\xC1";
  37832. size_t inLen;
  37833. byte hash[WC_SHA256_DIGEST_SIZE];
  37834. inLen = XSTRLEN((char*)input);
  37835. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  37836. ExpectNotNull(SHA256(input, inLen, hash));
  37837. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  37838. #endif
  37839. return EXPECT_RESULT();
  37840. }
  37841. static int test_wolfSSL_SHA256_Transform(void)
  37842. {
  37843. EXPECT_DECLS;
  37844. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  37845. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37846. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  37847. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH) && \
  37848. !defined(WOLFSSL_KCAPI_HASH)
  37849. byte input1[] = "";
  37850. byte input2[] = "abc";
  37851. byte local[WC_SHA256_BLOCK_SIZE];
  37852. word32 sLen = 0;
  37853. #ifdef BIG_ENDIAN_ORDER
  37854. unsigned char output1[] =
  37855. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  37856. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  37857. unsigned char output2[] =
  37858. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  37859. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  37860. #else
  37861. unsigned char output1[] =
  37862. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  37863. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  37864. unsigned char output2[] =
  37865. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  37866. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  37867. #endif
  37868. union {
  37869. wc_Sha256 native;
  37870. SHA256_CTX compat;
  37871. } sha256;
  37872. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  37873. XMEMSET(&local, 0, sizeof(local));
  37874. /* sanity check */
  37875. ExpectIntEQ(SHA256_Transform(NULL, NULL), 0);
  37876. ExpectIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  37877. ExpectIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  37878. ExpectIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  37879. ExpectIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37880. ExpectIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  37881. /* Init SHA256 CTX */
  37882. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  37883. /* Do Transform*/
  37884. sLen = (word32)XSTRLEN((char*)input1);
  37885. XMEMCPY(local, input1, sLen);
  37886. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  37887. ExpectIntEQ(XMEMCMP(sha256.native.digest, output1, WC_SHA256_DIGEST_SIZE),
  37888. 0);
  37889. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  37890. /* Init SHA256 CTX */
  37891. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  37892. sLen = (word32)XSTRLEN((char*)input2);
  37893. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  37894. XMEMCPY(local, input2, sLen);
  37895. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  37896. ExpectIntEQ(XMEMCMP(sha256.native.digest, output2, WC_SHA256_DIGEST_SIZE),
  37897. 0);
  37898. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  37899. #endif
  37900. #endif
  37901. return EXPECT_RESULT();
  37902. }
  37903. static int test_wolfSSL_SHA512_Transform(void)
  37904. {
  37905. EXPECT_DECLS;
  37906. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  37907. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37908. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  37909. !defined(WOLFSSL_KCAPI_HASH)
  37910. byte input1[] = "";
  37911. byte input2[] = "abc";
  37912. byte local[WC_SHA512_BLOCK_SIZE];
  37913. word32 sLen = 0;
  37914. #ifdef BIG_ENDIAN_ORDER
  37915. unsigned char output1[] =
  37916. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  37917. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  37918. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  37919. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  37920. unsigned char output2[] =
  37921. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  37922. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  37923. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  37924. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  37925. #else
  37926. unsigned char output1[] =
  37927. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  37928. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  37929. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  37930. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  37931. unsigned char output2[] =
  37932. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  37933. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  37934. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  37935. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  37936. #endif
  37937. union {
  37938. wc_Sha512 native;
  37939. SHA512_CTX compat;
  37940. } sha512;
  37941. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  37942. XMEMSET(&local, 0, sizeof(local));
  37943. /* sanity check */
  37944. ExpectIntEQ(SHA512_Transform(NULL, NULL), 0);
  37945. ExpectIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  37946. ExpectIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  37947. ExpectIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  37948. ExpectIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37949. ExpectIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  37950. /* Init SHA512 CTX */
  37951. ExpectIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  37952. /* Do Transform*/
  37953. sLen = (word32)XSTRLEN((char*)input1);
  37954. XMEMCPY(local, input1, sLen);
  37955. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  37956. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  37957. WC_SHA512_DIGEST_SIZE), 0);
  37958. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  37959. /* Init SHA512 CTX */
  37960. ExpectIntEQ(SHA512_Init(&sha512.compat), 1);
  37961. sLen = (word32)XSTRLEN((char*)input2);
  37962. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  37963. XMEMCPY(local, input2, sLen);
  37964. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  37965. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  37966. WC_SHA512_DIGEST_SIZE), 0);
  37967. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  37968. (void)input1;
  37969. #endif
  37970. #endif
  37971. return EXPECT_RESULT();
  37972. }
  37973. static int test_wolfSSL_SHA512_224_Transform(void)
  37974. {
  37975. EXPECT_DECLS;
  37976. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  37977. !defined(WOLFSSL_NOSHA512_224)
  37978. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37979. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  37980. !defined(WOLFSSL_KCAPI_HASH)
  37981. byte input1[] = "";
  37982. byte input2[] = "abc";
  37983. byte local[WC_SHA512_BLOCK_SIZE];
  37984. word32 sLen = 0;
  37985. unsigned char output1[] =
  37986. "\x94\x24\x66\xd4\x60\x3a\xeb\x23\x1d\xa8\x69\x31\x3c\xd2\xde\x11"
  37987. "\x48\x0f\x4a\x5a\xdf\x3a\x8d\x87\xcf\xcd\xbf\xa5\x03\x21\x50\xf1"
  37988. "\x8a\x0d\x0f\x0d\x3c\x07\xba\x52\xe0\xaa\x3c\xbb\xf1\xd3\x3f\xca"
  37989. "\x12\xa7\x61\xf8\x47\xda\x0d\x1b\x79\xc2\x65\x13\x92\xc1\x9c\xa5";
  37990. unsigned char output2[] =
  37991. "\x51\x28\xe7\x0b\xca\x1e\xbc\x5f\xd7\x34\x0b\x48\x30\xd7\xc2\x75"
  37992. "\x6d\x8d\x48\x2c\x1f\xc7\x9e\x2b\x20\x5e\xbb\x0f\x0e\x4d\xb7\x61"
  37993. "\x31\x76\x33\xa0\xb4\x3d\x5f\x93\xc1\x73\xac\xf7\x21\xff\x69\x17"
  37994. "\xce\x66\xe5\x1e\x31\xe7\xf3\x22\x0f\x0b\x34\xd7\x5a\x57\xeb\xbf";
  37995. union {
  37996. wc_Sha512 native;
  37997. SHA512_CTX compat;
  37998. } sha512;
  37999. #ifdef BIG_ENDIAN_ORDER
  38000. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  38001. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  38002. #endif
  38003. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  38004. XMEMSET(&local, 0, sizeof(local));
  38005. /* sanity check */
  38006. ExpectIntEQ(SHA512_224_Transform(NULL, NULL), 0);
  38007. ExpectIntEQ(SHA512_224_Transform(NULL, (const byte*)&input1), 0);
  38008. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, NULL), 0);
  38009. ExpectIntEQ(wc_Sha512_224Transform(NULL, NULL), BAD_FUNC_ARG);
  38010. ExpectIntEQ(wc_Sha512_224Transform(NULL, (const byte*)&input1),
  38011. BAD_FUNC_ARG);
  38012. ExpectIntEQ(wc_Sha512_224Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  38013. /* Init SHA512 CTX */
  38014. ExpectIntEQ(wolfSSL_SHA512_224_Init(&sha512.compat), 1);
  38015. /* Do Transform*/
  38016. sLen = (word32)XSTRLEN((char*)input1);
  38017. XMEMCPY(local, input1, sLen);
  38018. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  38019. 1);
  38020. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  38021. WC_SHA512_DIGEST_SIZE), 0);
  38022. /* frees resources */
  38023. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  38024. /* Init SHA512 CTX */
  38025. ExpectIntEQ(SHA512_224_Init(&sha512.compat), 1);
  38026. sLen = (word32)XSTRLEN((char*)input2);
  38027. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  38028. XMEMCPY(local, input2, sLen);
  38029. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  38030. 1);
  38031. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  38032. WC_SHA512_DIGEST_SIZE), 0);
  38033. /* frees resources */
  38034. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  38035. #endif
  38036. #endif
  38037. return EXPECT_RESULT();
  38038. }
  38039. static int test_wolfSSL_SHA512_256_Transform(void)
  38040. {
  38041. EXPECT_DECLS;
  38042. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  38043. !defined(WOLFSSL_NOSHA512_256)
  38044. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  38045. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  38046. !defined(WOLFSSL_KCAPI_HASH)
  38047. byte input1[] = "";
  38048. byte input2[] = "abc";
  38049. byte local[WC_SHA512_BLOCK_SIZE];
  38050. word32 sLen = 0;
  38051. unsigned char output1[] =
  38052. "\xf8\x37\x37\x5a\xd7\x2e\x56\xec\xe2\x51\xa8\x31\x3a\xa0\x63\x2b"
  38053. "\x7e\x7c\x64\xcc\xd9\xff\x2b\x6b\xeb\xc3\xd4\x4d\x7f\x8a\x3a\xb5"
  38054. "\x61\x85\x0b\x37\x30\x9f\x3b\x08\x5e\x7b\xd3\xbc\x6d\x00\x61\xc0"
  38055. "\x65\x9a\xd7\x73\xda\x40\xbe\xc1\xe5\x2f\xc6\x5d\xb7\x9f\xbe\x60";
  38056. unsigned char output2[] =
  38057. "\x22\xad\xc0\x30\xee\xd4\x6a\xef\x13\xee\x5a\x95\x8b\x1f\xb7\xb6"
  38058. "\xb6\xba\xc0\x44\xb8\x18\x3b\xf0\xf6\x4b\x70\x9f\x03\xba\x64\xa1"
  38059. "\xe1\xe3\x45\x15\x91\x7d\xcb\x0b\x9a\xf0\xd2\x8e\x47\x8b\x37\x78"
  38060. "\x91\x41\xa6\xc4\xb0\x29\x8f\x8b\xdd\x78\x5c\xf2\x73\x3f\x21\x31";
  38061. union {
  38062. wc_Sha512 native;
  38063. SHA512_CTX compat;
  38064. } sha512;
  38065. #ifdef BIG_ENDIAN_ORDER
  38066. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  38067. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  38068. #endif
  38069. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  38070. XMEMSET(&local, 0, sizeof(local));
  38071. /* sanity check */
  38072. ExpectIntEQ(SHA512_256_Transform(NULL, NULL), 0);
  38073. ExpectIntEQ(SHA512_256_Transform(NULL, (const byte*)&input1), 0);
  38074. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, NULL), 0);
  38075. ExpectIntEQ(wc_Sha512_256Transform(NULL, NULL), BAD_FUNC_ARG);
  38076. ExpectIntEQ(wc_Sha512_256Transform(NULL, (const byte*)&input1),
  38077. BAD_FUNC_ARG);
  38078. ExpectIntEQ(wc_Sha512_256Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  38079. /* Init SHA512 CTX */
  38080. ExpectIntEQ(wolfSSL_SHA512_256_Init(&sha512.compat), 1);
  38081. /* Do Transform*/
  38082. sLen = (word32)XSTRLEN((char*)input1);
  38083. XMEMCPY(local, input1, sLen);
  38084. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  38085. 1);
  38086. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  38087. WC_SHA512_DIGEST_SIZE), 0);
  38088. /* frees resources */
  38089. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  38090. /* Init SHA512 CTX */
  38091. ExpectIntEQ(SHA512_256_Init(&sha512.compat), 1);
  38092. sLen = (word32)XSTRLEN((char*)input2);
  38093. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  38094. XMEMCPY(local, input2, sLen);
  38095. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  38096. 1);
  38097. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  38098. WC_SHA512_DIGEST_SIZE), 0);
  38099. /* frees resources */
  38100. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  38101. #endif
  38102. #endif
  38103. return EXPECT_RESULT();
  38104. }
  38105. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  38106. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  38107. * buffer of 64 bytes.
  38108. *
  38109. * returns the size of the digest buffer on success and a negative value on
  38110. * failure.
  38111. */
  38112. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest,
  38113. int* sz)
  38114. {
  38115. EXPECT_DECLS;
  38116. HMAC_CTX ctx1;
  38117. HMAC_CTX ctx2;
  38118. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  38119. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  38120. unsigned char long_key[] =
  38121. "0123456789012345678901234567890123456789"
  38122. "0123456789012345678901234567890123456789"
  38123. "0123456789012345678901234567890123456789"
  38124. "0123456789012345678901234567890123456789";
  38125. unsigned char msg[] = "message to hash";
  38126. unsigned int digestSz = 64;
  38127. int keySz = sizeof(key);
  38128. int long_keySz = sizeof(long_key);
  38129. int msgSz = sizeof(msg);
  38130. unsigned char digest2[64];
  38131. unsigned int digestSz2 = 64;
  38132. HMAC_CTX_init(&ctx1);
  38133. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  38134. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38135. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38136. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38137. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38138. HMAC_CTX_cleanup(&ctx1);
  38139. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38140. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  38141. HMAC_CTX_cleanup(&ctx2);
  38142. ExpectIntEQ(digestSz, digestSz2);
  38143. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38144. /* test HMAC_Init with NULL key */
  38145. /* init after copy */
  38146. HMAC_CTX_init(&ctx1);
  38147. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  38148. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38149. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38150. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  38151. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38152. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38153. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38154. HMAC_CTX_cleanup(&ctx1);
  38155. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  38156. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38157. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38158. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  38159. HMAC_CTX_cleanup(&ctx2);
  38160. ExpectIntEQ(digestSz, digestSz2);
  38161. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38162. /* long key */
  38163. HMAC_CTX_init(&ctx1);
  38164. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type),
  38165. SSL_SUCCESS);
  38166. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38167. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38168. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  38169. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38170. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38171. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38172. HMAC_CTX_cleanup(&ctx1);
  38173. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  38174. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38175. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38176. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  38177. HMAC_CTX_cleanup(&ctx2);
  38178. ExpectIntEQ(digestSz, digestSz2);
  38179. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38180. /* init before copy */
  38181. HMAC_CTX_init(&ctx1);
  38182. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  38183. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38184. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  38185. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38186. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38187. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38188. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38189. HMAC_CTX_cleanup(&ctx1);
  38190. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38191. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38192. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  38193. HMAC_CTX_cleanup(&ctx2);
  38194. ExpectIntEQ(digestSz, digestSz2);
  38195. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38196. *sz = digestSz;
  38197. return EXPECT_RESULT();
  38198. }
  38199. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  38200. static int test_wolfSSL_HMAC_CTX(void)
  38201. {
  38202. EXPECT_DECLS;
  38203. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  38204. unsigned char digest[64];
  38205. int digestSz;
  38206. WOLFSSL_HMAC_CTX* hmac_ctx = NULL;
  38207. WOLFSSL_HMAC_CTX ctx1;
  38208. WOLFSSL_HMAC_CTX ctx2;
  38209. ExpectNotNull(hmac_ctx = wolfSSL_HMAC_CTX_new());
  38210. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(NULL), 1);
  38211. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(hmac_ctx), 1);
  38212. wolfSSL_HMAC_CTX_free(NULL);
  38213. wolfSSL_HMAC_CTX_free(hmac_ctx);
  38214. XMEMSET(&ctx2, 0, sizeof(WOLFSSL_HMAC_CTX));
  38215. ExpectIntEQ(HMAC_CTX_init(NULL), 1);
  38216. ExpectIntEQ(HMAC_CTX_init(&ctx2), 1);
  38217. ExpectIntEQ(HMAC_CTX_copy(NULL, NULL), 0);
  38218. ExpectIntEQ(HMAC_CTX_copy(NULL, &ctx2), 0);
  38219. ExpectIntEQ(HMAC_CTX_copy(&ctx2, NULL), 0);
  38220. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  38221. ((! defined(HAVE_FIPS_VERSION)) || \
  38222. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  38223. /* Copy object that hasn't had a digest set - MD5. */
  38224. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 1);
  38225. #else
  38226. /* Copy object that hasn't had a digest set. */
  38227. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 0);
  38228. #endif
  38229. HMAC_CTX_cleanup(NULL);
  38230. HMAC_CTX_cleanup(&ctx2);
  38231. ExpectNull(HMAC_CTX_get_md(NULL));
  38232. #ifndef NO_SHA
  38233. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha1(), digest, &digestSz)),
  38234. TEST_SUCCESS);
  38235. ExpectIntEQ(digestSz, 20);
  38236. ExpectIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  38237. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  38238. #endif /* !NO_SHA */
  38239. #ifdef WOLFSSL_SHA224
  38240. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha224(), digest, &digestSz)),
  38241. TEST_SUCCESS);
  38242. ExpectIntEQ(digestSz, 28);
  38243. ExpectIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  38244. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  38245. "\x02\x0E", digest, digestSz), 0);
  38246. #endif /* WOLFSSL_SHA224 */
  38247. #ifndef NO_SHA256
  38248. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha256(), digest, &digestSz)),
  38249. TEST_SUCCESS);
  38250. ExpectIntEQ(digestSz, 32);
  38251. ExpectIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  38252. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  38253. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  38254. #endif /* !NO_SHA256 */
  38255. #ifdef WOLFSSL_SHA384
  38256. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha384(), digest, &digestSz)),
  38257. TEST_SUCCESS);
  38258. ExpectIntEQ(digestSz, 48);
  38259. ExpectIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  38260. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  38261. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  38262. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  38263. digest, digestSz), 0);
  38264. #endif /* WOLFSSL_SHA384 */
  38265. #ifdef WOLFSSL_SHA512
  38266. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha512(), digest, &digestSz)),
  38267. TEST_SUCCESS);
  38268. ExpectIntEQ(digestSz, 64);
  38269. ExpectIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  38270. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  38271. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  38272. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  38273. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  38274. digest, digestSz), 0);
  38275. #endif /* WOLFSSL_SHA512 */
  38276. #ifdef WOLFSSL_SHA3
  38277. #ifndef WOLFSSL_NOSHA3_224
  38278. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_224(), digest, &digestSz)),
  38279. TEST_SUCCESS);
  38280. ExpectIntEQ(digestSz, 28);
  38281. ExpectIntEQ(XMEMCMP("\xdc\x53\x25\x3f\xc0\x9d\x2b\x0c\x7f\x59\x11\x17\x08"
  38282. "\x5c\xe8\x43\x31\x01\x5a\xb3\xe3\x08\x37\x71\x26\x0b"
  38283. "\x29\x0f", digest, digestSz), 0);
  38284. #endif
  38285. #ifndef WOLFSSL_NOSHA3_256
  38286. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_256(), digest, &digestSz)),
  38287. TEST_SUCCESS);
  38288. ExpectIntEQ(digestSz, 32);
  38289. ExpectIntEQ(XMEMCMP("\x0f\x00\x89\x82\x15\xce\xd6\x45\x01\x83\xce\xc8\x35"
  38290. "\xab\x71\x07\xc9\xfe\x61\x22\x38\xf9\x09\xad\x35\x65"
  38291. "\x43\x77\x24\xd4\x1e\xf4", digest, digestSz), 0);
  38292. #endif
  38293. #ifndef WOLFSSL_NOSHA3_384
  38294. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_384(), digest, &digestSz)),
  38295. TEST_SUCCESS);
  38296. ExpectIntEQ(digestSz, 48);
  38297. ExpectIntEQ(XMEMCMP("\x0f\x6a\xc0\xfb\xc3\xf2\x80\xb1\xb4\x04\xb6\xc8\x45"
  38298. "\x23\x3b\xb4\xbe\xc6\xea\x85\x07\xca\x8c\x71\xbb\x6e"
  38299. "\x79\xf6\xf9\x2b\x98\xf5\xef\x11\x39\xd4\x5d\xd3\xca"
  38300. "\xc0\xe6\x81\xf7\x73\xf9\x85\x5d\x4f",
  38301. digest, digestSz), 0);
  38302. #endif
  38303. #ifndef WOLFSSL_NOSHA3_512
  38304. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_512(), digest, &digestSz)),
  38305. TEST_SUCCESS);
  38306. ExpectIntEQ(digestSz, 64);
  38307. ExpectIntEQ(XMEMCMP("\x3e\x77\xe3\x59\x42\x89\xed\xc3\xa4\x26\x3d\xa4\x75"
  38308. "\xd2\x84\x8c\xb2\xf3\x25\x04\x47\x61\xce\x1c\x42\x86"
  38309. "\xcd\xf4\x56\xaa\x2f\x84\xb1\x3b\x18\xed\xe6\xd6\x48"
  38310. "\x15\xb0\x29\xc5\x9d\x32\xef\xdd\x3e\x09\xf6\xed\x9e"
  38311. "\x70\xbc\x1c\x63\xf7\x3b\x3e\xe1\xdc\x84\x9c\x1c",
  38312. digest, digestSz), 0);
  38313. #endif
  38314. #endif
  38315. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || \
  38316. HAVE_FIPS_VERSION <= 2)
  38317. ExpectIntEQ((test_HMAC_CTX_helper(EVP_md5(), digest, &digestSz)),
  38318. TEST_SUCCESS);
  38319. ExpectIntEQ(digestSz, 16);
  38320. ExpectIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  38321. "\xE4\x98\xDD", digest, digestSz), 0);
  38322. #endif /* !NO_MD5 */
  38323. #endif
  38324. return EXPECT_RESULT();
  38325. }
  38326. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  38327. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  38328. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  38329. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  38330. {
  38331. EXPECT_DECLS;
  38332. static const unsigned char key[] = "simple test key";
  38333. HMAC_CTX* hmac = NULL;
  38334. ENGINE* e = NULL;
  38335. unsigned char hash[WC_MAX_DIGEST_SIZE];
  38336. unsigned int len;
  38337. ExpectNotNull(hmac = HMAC_CTX_new());
  38338. HMAC_CTX_init(hmac);
  38339. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  38340. ((! defined(HAVE_FIPS_VERSION)) || \
  38341. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  38342. /* Get size on object that hasn't had a digest set - MD5. */
  38343. ExpectIntEQ(HMAC_size(hmac), 16);
  38344. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 1);
  38345. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 1);
  38346. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 1);
  38347. #else
  38348. ExpectIntEQ(HMAC_size(hmac), BAD_FUNC_ARG);
  38349. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 0);
  38350. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 0);
  38351. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 0);
  38352. #endif
  38353. ExpectIntEQ(HMAC_Init_ex(NULL, (void*)key, (int)sizeof(key), md, e), 0);
  38354. ExpectIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e), 1);
  38355. /* re-using test key as data to hash */
  38356. ExpectIntEQ(HMAC_Update(NULL, key, (int)sizeof(key)), 0);
  38357. ExpectIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), 1);
  38358. ExpectIntEQ(HMAC_Update(hmac, key, 0), 1);
  38359. ExpectIntEQ(HMAC_Update(hmac, NULL, 0), 1);
  38360. ExpectIntEQ(HMAC_Update(hmac, NULL, (int)sizeof(key)), 1);
  38361. ExpectIntEQ(HMAC_Final(NULL, NULL, &len), 0);
  38362. ExpectIntEQ(HMAC_Final(hmac, NULL, &len), 0);
  38363. ExpectIntEQ(HMAC_Final(NULL, hash, &len), 0);
  38364. ExpectIntEQ(HMAC_Final(hmac, hash, &len), 1);
  38365. ExpectIntEQ(HMAC_Final(hmac, hash, NULL), 1);
  38366. ExpectIntEQ(len, md_len);
  38367. ExpectIntEQ(HMAC_size(NULL), 0);
  38368. ExpectIntEQ(HMAC_size(hmac), md_len);
  38369. ExpectStrEQ(HMAC_CTX_get_md(hmac), md);
  38370. HMAC_cleanup(NULL);
  38371. HMAC_cleanup(hmac);
  38372. HMAC_CTX_free(hmac);
  38373. len = 0;
  38374. ExpectNull(HMAC(NULL, key, (int)sizeof(key), NULL, 0, hash, &len));
  38375. ExpectNull(HMAC(md, NULL, (int)sizeof(key), NULL, 0, hash, &len));
  38376. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, 0, NULL, &len));
  38377. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  38378. ExpectIntEQ(len, md_len);
  38379. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, NULL));
  38380. /* With data. */
  38381. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, (int)sizeof(key), hash,
  38382. &len));
  38383. /* With NULL data. */
  38384. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, (int)sizeof(key), hash,
  38385. &len));
  38386. /* With zero length data. */
  38387. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, 0, hash, &len));
  38388. return EXPECT_RESULT();
  38389. }
  38390. #endif
  38391. static int test_wolfSSL_HMAC(void)
  38392. {
  38393. EXPECT_DECLS;
  38394. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  38395. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  38396. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  38397. #ifndef NO_SHA256
  38398. ExpectIntEQ(test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE),
  38399. TEST_SUCCESS);
  38400. #endif
  38401. #ifdef WOLFSSL_SHA224
  38402. ExpectIntEQ(test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE),
  38403. TEST_SUCCESS);
  38404. #endif
  38405. #ifdef WOLFSSL_SHA384
  38406. ExpectIntEQ(test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE),
  38407. TEST_SUCCESS);
  38408. #endif
  38409. #ifdef WOLFSSL_SHA512
  38410. ExpectIntEQ(test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE),
  38411. TEST_SUCCESS);
  38412. #endif
  38413. #ifdef WOLFSSL_SHA3
  38414. #ifndef WOLFSSL_NOSHA3_224
  38415. ExpectIntEQ(test_openssl_hmac(EVP_sha3_224(),
  38416. (int)WC_SHA3_224_DIGEST_SIZE), TEST_SUCCESS);
  38417. #endif
  38418. #ifndef WOLFSSL_NOSHA3_256
  38419. ExpectIntEQ(test_openssl_hmac(EVP_sha3_256(),
  38420. (int)WC_SHA3_256_DIGEST_SIZE), TEST_SUCCESS);
  38421. #endif
  38422. #ifndef WOLFSSL_NOSHA3_384
  38423. ExpectIntEQ(test_openssl_hmac(EVP_sha3_384(),
  38424. (int)WC_SHA3_384_DIGEST_SIZE), TEST_SUCCESS);
  38425. #endif
  38426. #ifndef WOLFSSL_NOSHA3_512
  38427. ExpectIntEQ(test_openssl_hmac(EVP_sha3_512(),
  38428. (int)WC_SHA3_512_DIGEST_SIZE), TEST_SUCCESS);
  38429. #endif
  38430. #endif
  38431. #ifndef NO_SHA
  38432. ExpectIntEQ(test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE),
  38433. TEST_SUCCESS);
  38434. #endif
  38435. #endif
  38436. return EXPECT_RESULT();
  38437. }
  38438. static int test_wolfSSL_CMAC(void)
  38439. {
  38440. EXPECT_DECLS;
  38441. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  38442. defined(WOLFSSL_AES_DIRECT)
  38443. int i;
  38444. byte key[AES_256_KEY_SIZE];
  38445. CMAC_CTX* cmacCtx = NULL;
  38446. byte out[AES_BLOCK_SIZE];
  38447. size_t outLen = AES_BLOCK_SIZE;
  38448. for (i=0; i < AES_256_KEY_SIZE; ++i) {
  38449. key[i] = i;
  38450. }
  38451. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38452. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  38453. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  38454. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  38455. NULL), 1);
  38456. /* re-using test key as data to hash */
  38457. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  38458. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  38459. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 1);
  38460. ExpectIntEQ(outLen, AES_BLOCK_SIZE);
  38461. /* No Update works. */
  38462. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  38463. NULL), 1);
  38464. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  38465. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  38466. NULL), 1);
  38467. /* Test parameters with CMAC_Update. */
  38468. ExpectIntEQ(CMAC_Update(NULL, NULL, 0), 0);
  38469. ExpectIntEQ(CMAC_Update(NULL, key, 0), 0);
  38470. ExpectIntEQ(CMAC_Update(NULL, NULL, AES_128_KEY_SIZE), 0);
  38471. ExpectIntEQ(CMAC_Update(NULL, key, AES_128_KEY_SIZE), 0);
  38472. ExpectIntEQ(CMAC_Update(cmacCtx, key, 0), 1);
  38473. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  38474. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, AES_128_KEY_SIZE), 1);
  38475. /* Test parameters with CMAC_Final. */
  38476. ExpectIntEQ(CMAC_Final(NULL, NULL, NULL), 0);
  38477. ExpectIntEQ(CMAC_Final(NULL, out, NULL), 0);
  38478. ExpectIntEQ(CMAC_Final(NULL, NULL, &outLen), 0);
  38479. ExpectIntEQ(CMAC_Final(NULL, out, &outLen), 0);
  38480. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, NULL), 1);
  38481. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, &outLen), 1);
  38482. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  38483. CMAC_CTX_free(cmacCtx);
  38484. /* Test parameters with CMAC Init. */
  38485. cmacCtx = NULL;
  38486. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38487. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  38488. ExpectIntEQ(CMAC_Init(NULL, NULL, 0, NULL, NULL), 0);
  38489. ExpectIntEQ(CMAC_Init(NULL, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  38490. NULL), 0);
  38491. ExpectIntEQ(CMAC_Init(cmacCtx, NULL, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  38492. NULL), 0);
  38493. /* give a key too small for the cipher, verify we get failure */
  38494. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_192_cbc(),
  38495. NULL), 0);
  38496. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, NULL, NULL), 0);
  38497. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128)
  38498. /* Only AES-CBC supported. */
  38499. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_gcm(),
  38500. NULL), 0);
  38501. #endif
  38502. CMAC_CTX_free(cmacCtx);
  38503. ExpectNull(CMAC_CTX_get0_cipher_ctx(NULL));
  38504. cmacCtx = NULL;
  38505. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38506. /* No Init. */
  38507. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 0);
  38508. CMAC_CTX_free(cmacCtx);
  38509. /* Test AES-256-CBC */
  38510. cmacCtx = NULL;
  38511. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38512. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_256_KEY_SIZE, EVP_aes_256_cbc(),
  38513. NULL), 1);
  38514. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  38515. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  38516. CMAC_CTX_free(cmacCtx);
  38517. /* Test AES-192-CBC */
  38518. cmacCtx = NULL;
  38519. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38520. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  38521. NULL), 1);
  38522. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  38523. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  38524. CMAC_CTX_free(cmacCtx);
  38525. cmacCtx = NULL;
  38526. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38527. CMAC_CTX_free(cmacCtx);
  38528. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  38529. return EXPECT_RESULT();
  38530. }
  38531. static int test_wolfSSL_DES(void)
  38532. {
  38533. EXPECT_DECLS;
  38534. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  38535. const_DES_cblock myDes;
  38536. DES_cblock iv;
  38537. DES_key_schedule key;
  38538. word32 i = 0;
  38539. DES_LONG dl = 0;
  38540. unsigned char msg[] = "hello wolfssl";
  38541. unsigned char weakKey[][8] = {
  38542. { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
  38543. { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
  38544. { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 },
  38545. { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E }
  38546. };
  38547. unsigned char semiWeakKey[][8] = {
  38548. { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E },
  38549. { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 },
  38550. { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 },
  38551. { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 },
  38552. { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE },
  38553. { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 },
  38554. { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 },
  38555. { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E },
  38556. { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE },
  38557. { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E },
  38558. { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
  38559. { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
  38560. };
  38561. DES_check_key(1);
  38562. DES_set_key(&myDes, &key);
  38563. /* check, check of odd parity */
  38564. XMEMSET(myDes, 4, sizeof(const_DES_cblock));
  38565. myDes[0] = 6; /* set even parity */
  38566. XMEMSET(key, 5, sizeof(DES_key_schedule));
  38567. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -1);
  38568. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  38569. ExpectIntEQ(DES_set_key_checked(NULL, NULL), -2);
  38570. ExpectIntEQ(DES_set_key_checked(&myDes, NULL), -2);
  38571. ExpectIntEQ(DES_set_key_checked(NULL, &key), -2);
  38572. /* set odd parity for success case */
  38573. DES_set_odd_parity(&myDes);
  38574. ExpectIntEQ(DES_check_key_parity(&myDes), 1);
  38575. fprintf(stderr, "%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2],
  38576. myDes[3]);
  38577. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  38578. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  38579. ExpectIntEQ(key[i], myDes[i]);
  38580. }
  38581. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  38582. /* check weak key */
  38583. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  38584. XMEMSET(key, 5, sizeof(DES_key_schedule));
  38585. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -2);
  38586. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  38587. DES_set_key_unchecked(NULL, NULL);
  38588. DES_set_key_unchecked(&myDes, NULL);
  38589. DES_set_key_unchecked(NULL, &key);
  38590. /* compare arrays, should be the same */
  38591. /* now do unchecked copy of a weak key over */
  38592. DES_set_key_unchecked(&myDes, &key);
  38593. /* compare arrays, should be the same */
  38594. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  38595. ExpectIntEQ(key[i], myDes[i]);
  38596. }
  38597. ExpectIntEQ(DES_is_weak_key(&myDes), 1);
  38598. myDes[7] = 2;
  38599. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  38600. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  38601. ExpectIntEQ(DES_is_weak_key(NULL), 1);
  38602. /* Test all weak keys. */
  38603. for (i = 0; i < sizeof(weakKey) / sizeof(*weakKey); i++) {
  38604. ExpectIntEQ(DES_set_key_checked(&weakKey[i], &key), -2);
  38605. }
  38606. /* Test all semi-weak keys. */
  38607. for (i = 0; i < sizeof(semiWeakKey) / sizeof(*semiWeakKey); i++) {
  38608. ExpectIntEQ(DES_set_key_checked(&semiWeakKey[i], &key), -2);
  38609. }
  38610. /* check DES_key_sched API */
  38611. XMEMSET(key, 1, sizeof(DES_key_schedule));
  38612. ExpectIntEQ(DES_key_sched(&myDes, NULL), 0);
  38613. ExpectIntEQ(DES_key_sched(NULL, &key), 0);
  38614. ExpectIntEQ(DES_key_sched(&myDes, &key), 0);
  38615. /* compare arrays, should be the same */
  38616. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  38617. ExpectIntEQ(key[i], myDes[i]);
  38618. }
  38619. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, NULL)), 0);
  38620. ExpectIntEQ((DES_cbc_cksum(msg, NULL, 0, NULL, NULL)), 0);
  38621. ExpectIntEQ((DES_cbc_cksum(NULL, &key, 0, NULL, NULL)), 0);
  38622. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, &myDes, NULL)), 0);
  38623. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, &iv)), 0);
  38624. ExpectIntEQ((DES_cbc_cksum(NULL, &key, sizeof(msg), &myDes, &iv)), 0);
  38625. ExpectIntEQ((DES_cbc_cksum(msg, NULL, sizeof(msg), &myDes, &iv)), 0);
  38626. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), NULL, &iv)), 0);
  38627. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, NULL)), 0);
  38628. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  38629. * DES_cbc_encrypt on the input */
  38630. XMEMSET(iv, 0, sizeof(DES_cblock));
  38631. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  38632. ExpectIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  38633. ExpectIntEQ(dl, 480052723);
  38634. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  38635. return EXPECT_RESULT();
  38636. }
  38637. static int test_wolfSSL_DES_ncbc(void)
  38638. {
  38639. EXPECT_DECLS;
  38640. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  38641. const_DES_cblock myDes;
  38642. DES_cblock iv = {1};
  38643. DES_key_schedule key = {0};
  38644. unsigned char msg[] = "hello wolfssl";
  38645. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  38646. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  38647. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  38648. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  38649. /* partial block test */
  38650. DES_set_key(&key, &myDes);
  38651. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  38652. ExpectIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  38653. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  38654. DES_set_key(&key, &myDes);
  38655. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38656. *((byte*)&iv) = 1;
  38657. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  38658. ExpectIntEQ(XMEMCMP(msg, pln, 3), 0);
  38659. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  38660. /* full block test */
  38661. DES_set_key(&key, &myDes);
  38662. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  38663. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38664. *((byte*)&iv) = 1;
  38665. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  38666. ExpectIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  38667. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  38668. DES_set_key(&key, &myDes);
  38669. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38670. *((byte*)&iv) = 1;
  38671. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  38672. ExpectIntEQ(XMEMCMP(msg, pln, 8), 0);
  38673. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  38674. #endif
  38675. return EXPECT_RESULT();
  38676. }
  38677. static int test_wolfSSL_DES_ecb_encrypt(void)
  38678. {
  38679. EXPECT_DECLS;
  38680. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  38681. WOLFSSL_DES_cblock input1, input2, output1, output2, back1, back2;
  38682. WOLFSSL_DES_key_schedule key;
  38683. XMEMCPY(key, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  38684. XMEMCPY(input1, "Iamhuman", sizeof(WOLFSSL_DES_cblock));
  38685. XMEMCPY(input2, "Whoisit?", sizeof(WOLFSSL_DES_cblock));
  38686. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  38687. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  38688. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  38689. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  38690. wolfSSL_DES_ecb_encrypt(NULL, NULL, NULL, DES_ENCRYPT);
  38691. wolfSSL_DES_ecb_encrypt(&input1, NULL, NULL, DES_ENCRYPT);
  38692. wolfSSL_DES_ecb_encrypt(NULL, &output1, NULL, DES_ENCRYPT);
  38693. wolfSSL_DES_ecb_encrypt(NULL, NULL, &key, DES_ENCRYPT);
  38694. wolfSSL_DES_ecb_encrypt(&input1, &output1, NULL, DES_ENCRYPT);
  38695. wolfSSL_DES_ecb_encrypt(&input1, NULL, &key, DES_ENCRYPT);
  38696. wolfSSL_DES_ecb_encrypt(NULL, &output1, &key, DES_ENCRYPT);
  38697. /* Encrypt messages */
  38698. wolfSSL_DES_ecb_encrypt(&input1, &output1, &key, DES_ENCRYPT);
  38699. wolfSSL_DES_ecb_encrypt(&input2, &output2, &key, DES_ENCRYPT);
  38700. {
  38701. /* Decrypt messages */
  38702. int ret1 = 0;
  38703. int ret2 = 0;
  38704. wolfSSL_DES_ecb_encrypt(&output1, &back1, &key, DES_DECRYPT);
  38705. ExpectIntEQ(ret1 = XMEMCMP((unsigned char *)back1,
  38706. (unsigned char *)input1, sizeof(WOLFSSL_DES_cblock)), 0);
  38707. wolfSSL_DES_ecb_encrypt(&output2, &back2, &key, DES_DECRYPT);
  38708. ExpectIntEQ(ret2 = XMEMCMP((unsigned char *)back2,
  38709. (unsigned char *)input2, sizeof(WOLFSSL_DES_cblock)), 0);
  38710. }
  38711. #endif
  38712. return EXPECT_RESULT();
  38713. }
  38714. static int test_wolfSSL_DES_ede3_cbc_encrypt(void)
  38715. {
  38716. EXPECT_DECLS;
  38717. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  38718. unsigned char input1[8], input2[8];
  38719. unsigned char output1[8], output2[8];
  38720. unsigned char back1[8], back2[8];
  38721. WOLFSSL_DES_cblock iv1, iv2;
  38722. WOLFSSL_DES_key_schedule key1, key2, key3;
  38723. int i;
  38724. XMEMCPY(key1, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  38725. XMEMCPY(key2, "23456781", sizeof(WOLFSSL_DES_key_schedule));
  38726. XMEMCPY(key3, "34567823", sizeof(WOLFSSL_DES_key_schedule));
  38727. XMEMCPY(input1, "Iamhuman", sizeof(input1));
  38728. XMEMCPY(input2, "Whoisit?", sizeof(input2));
  38729. XMEMSET(output1, 0, sizeof(output1));
  38730. XMEMSET(output2, 0, sizeof(output2));
  38731. XMEMSET(back1, 0, sizeof(back1));
  38732. XMEMSET(back2, 0, sizeof(back2));
  38733. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  38734. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  38735. /* Encrypt messages */
  38736. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, 8, &key1, &key2, &key3, &iv1,
  38737. DES_ENCRYPT);
  38738. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, 8, &key1, &key2, &key3, &iv2,
  38739. DES_ENCRYPT);
  38740. {
  38741. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  38742. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  38743. /* Decrypt messages */
  38744. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, 8, &key1, &key2, &key3,
  38745. &iv1, DES_DECRYPT);
  38746. ExpectIntEQ(XMEMCMP(back1, input1, sizeof(input1)), 0);
  38747. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, 8, &key1, &key2, &key3,
  38748. &iv2, DES_DECRYPT);
  38749. ExpectIntEQ(XMEMCMP(back2, input2, sizeof(input2)), 0);
  38750. }
  38751. for (i = 0; i < 8; i++) {
  38752. XMEMSET(output1, 0, sizeof(output1));
  38753. XMEMSET(output2, 0, sizeof(output2));
  38754. XMEMSET(back1, 0, sizeof(back1));
  38755. XMEMSET(back2, 0, sizeof(back2));
  38756. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  38757. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  38758. /* Encrypt partial messages */
  38759. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, i, &key1, &key2, &key3,
  38760. &iv1, DES_ENCRYPT);
  38761. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, i, &key1, &key2, &key3,
  38762. &iv2, DES_ENCRYPT);
  38763. {
  38764. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  38765. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  38766. /* Decrypt messages */
  38767. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, i, &key1, &key2,
  38768. &key3, &iv1, DES_DECRYPT);
  38769. ExpectIntEQ(XMEMCMP(back1, input1, i), 0);
  38770. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, i, &key1, &key2,
  38771. &key3, &iv2, DES_DECRYPT);
  38772. ExpectIntEQ(XMEMCMP(back2, input2, i), 0);
  38773. }
  38774. }
  38775. #endif
  38776. return EXPECT_RESULT();
  38777. }
  38778. static int test_wolfSSL_AES_encrypt(void)
  38779. {
  38780. EXPECT_DECLS;
  38781. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  38782. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  38783. AES_KEY enc;
  38784. AES_KEY dec;
  38785. const byte msg[] = {
  38786. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  38787. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  38788. };
  38789. const byte exp[] = {
  38790. 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c,
  38791. 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8,
  38792. };
  38793. const byte key[] = {
  38794. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  38795. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  38796. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  38797. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  38798. };
  38799. byte eout[sizeof(msg)];
  38800. byte dout[sizeof(msg)];
  38801. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &enc), 0);
  38802. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &dec), 0);
  38803. wolfSSL_AES_encrypt(NULL, NULL, NULL);
  38804. wolfSSL_AES_encrypt(msg, NULL, NULL);
  38805. wolfSSL_AES_encrypt(NULL, eout, NULL);
  38806. wolfSSL_AES_encrypt(NULL, NULL, &enc);
  38807. wolfSSL_AES_encrypt(msg, eout, NULL);
  38808. wolfSSL_AES_encrypt(msg, NULL, &enc);
  38809. wolfSSL_AES_encrypt(NULL, eout, &enc);
  38810. wolfSSL_AES_decrypt(NULL, NULL, NULL);
  38811. wolfSSL_AES_decrypt(eout, NULL, NULL);
  38812. wolfSSL_AES_decrypt(NULL, dout, NULL);
  38813. wolfSSL_AES_decrypt(NULL, NULL, &dec);
  38814. wolfSSL_AES_decrypt(eout, dout, NULL);
  38815. wolfSSL_AES_decrypt(eout, NULL, &dec);
  38816. wolfSSL_AES_decrypt(NULL, dout, &dec);
  38817. wolfSSL_AES_encrypt(msg, eout, &enc);
  38818. ExpectIntEQ(XMEMCMP(eout, exp, AES_BLOCK_SIZE), 0);
  38819. wolfSSL_AES_decrypt(eout, dout, &dec);
  38820. ExpectIntEQ(XMEMCMP(dout, msg, AES_BLOCK_SIZE), 0);
  38821. #endif
  38822. return EXPECT_RESULT();
  38823. }
  38824. static int test_wolfSSL_AES_ecb_encrypt(void)
  38825. {
  38826. EXPECT_DECLS;
  38827. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  38828. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  38829. AES_KEY aes;
  38830. const byte msg[] =
  38831. {
  38832. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38833. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  38834. };
  38835. const byte verify[] =
  38836. {
  38837. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  38838. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  38839. };
  38840. const byte key[] =
  38841. {
  38842. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  38843. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  38844. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  38845. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  38846. };
  38847. byte out[AES_BLOCK_SIZE];
  38848. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  38849. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38850. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  38851. ExpectIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  38852. #ifdef HAVE_AES_DECRYPT
  38853. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  38854. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38855. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  38856. ExpectIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  38857. #endif
  38858. /* test bad arguments */
  38859. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  38860. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  38861. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  38862. #endif
  38863. return EXPECT_RESULT();
  38864. }
  38865. static int test_wolfSSL_AES_cbc_encrypt(void)
  38866. {
  38867. EXPECT_DECLS;
  38868. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  38869. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  38870. AES_KEY aes;
  38871. AES_KEY* aesN = NULL;
  38872. size_t len = 0;
  38873. size_t lenB = 0;
  38874. int keySz0 = 0;
  38875. int keySzN = -1;
  38876. byte out[AES_BLOCK_SIZE] = {0};
  38877. byte* outN = NULL;
  38878. /* Test vectors retrieved from:
  38879. * <begin URL>
  38880. * https://csrc.nist.gov/
  38881. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  38882. * documents/aes/KAT_AES.zip
  38883. * </end URL>
  38884. */
  38885. const byte* pt128N = NULL;
  38886. byte* key128N = NULL;
  38887. byte* iv128N = NULL;
  38888. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  38889. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38890. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  38891. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  38892. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  38893. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38894. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  38895. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  38896. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  38897. len = sizeof(pt128);
  38898. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  38899. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  38900. ExpectIntNE(XMEMCMP(b, g, h), i)
  38901. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  38902. /* Stressing wolfSSL_AES_cbc_encrypt() */
  38903. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  38904. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  38905. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  38906. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38907. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  38908. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38909. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  38910. /* Stressing wolfSSL_AES_set_encrypt_key */
  38911. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  38912. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  38913. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  38914. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  38915. /* Stressing wolfSSL_AES_set_decrypt_key */
  38916. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  38917. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  38918. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  38919. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  38920. #ifdef WOLFSSL_AES_128
  38921. /* wolfSSL_AES_cbc_encrypt() 128-bit */
  38922. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38923. RESET_IV(iv128tmp, iv128);
  38924. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  38925. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  38926. ExpectIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  38927. wc_AesFree((Aes*)&aes);
  38928. #ifdef HAVE_AES_DECRYPT
  38929. /* wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode */
  38930. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38931. RESET_IV(iv128tmp, iv128);
  38932. len = sizeof(ct128);
  38933. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  38934. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  38935. ExpectIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  38936. wc_AesFree((Aes*)&aes);
  38937. #endif
  38938. #endif /* WOLFSSL_AES_128 */
  38939. #ifdef WOLFSSL_AES_192
  38940. {
  38941. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  38942. * Appendix F.2.3 */
  38943. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  38944. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38945. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  38946. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  38947. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  38948. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  38949. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  38950. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  38951. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  38952. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  38953. len = sizeof(pt192);
  38954. /* wolfSSL_AES_cbc_encrypt() 192-bit */
  38955. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38956. RESET_IV(iv192tmp, iv192);
  38957. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  38958. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  38959. ExpectIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  38960. wc_AesFree((Aes*)&aes);
  38961. #ifdef HAVE_AES_DECRYPT
  38962. /* wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode */
  38963. len = sizeof(ct192);
  38964. RESET_IV(iv192tmp, iv192);
  38965. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38966. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  38967. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  38968. ExpectIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  38969. wc_AesFree((Aes*)&aes);
  38970. #endif
  38971. }
  38972. #endif /* WOLFSSL_AES_192 */
  38973. #ifdef WOLFSSL_AES_256
  38974. {
  38975. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  38976. * Appendix F.2.5 */
  38977. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  38978. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38979. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  38980. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  38981. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  38982. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  38983. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  38984. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  38985. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  38986. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  38987. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  38988. len = sizeof(pt256);
  38989. /* wolfSSL_AES_cbc_encrypt() 256-bit */
  38990. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38991. RESET_IV(iv256tmp, iv256);
  38992. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  38993. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  38994. ExpectIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  38995. wc_AesFree((Aes*)&aes);
  38996. #ifdef HAVE_AES_DECRYPT
  38997. /* wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode */
  38998. len = sizeof(ct256);
  38999. RESET_IV(iv256tmp, iv256);
  39000. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39001. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39002. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  39003. ExpectIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  39004. wc_AesFree((Aes*)&aes);
  39005. #endif
  39006. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && \
  39007. !defined(HAVE_SELFTEST)
  39008. {
  39009. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  39010. byte wrapPlain[sizeof(key256)] = { 0 };
  39011. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  39012. /* wolfSSL_AES_wrap_key() 256-bit NULL iv */
  39013. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39014. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  39015. 15), WOLFSSL_FAILURE);
  39016. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  39017. sizeof(key256)), sizeof(wrapCipher));
  39018. wc_AesFree((Aes*)&aes);
  39019. /* wolfSSL_AES_unwrap_key() 256-bit NULL iv */
  39020. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39021. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  39022. 23), WOLFSSL_FAILURE);
  39023. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  39024. sizeof(wrapCipher)), sizeof(wrapPlain));
  39025. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  39026. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  39027. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  39028. wc_AesFree((Aes*)&aes);
  39029. /* wolfSSL_AES_wrap_key() 256-bit custom iv */
  39030. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39031. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  39032. sizeof(key256)), sizeof(wrapCipher));
  39033. wc_AesFree((Aes*)&aes);
  39034. /* wolfSSL_AES_unwrap_key() 256-bit custom iv */
  39035. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39036. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  39037. sizeof(wrapCipher)), sizeof(wrapPlain));
  39038. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  39039. wc_AesFree((Aes*)&aes);
  39040. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, NULL, 0), 0);
  39041. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, NULL, NULL, 0), 0);
  39042. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  39043. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, wrapCipher, NULL, 0), 0);
  39044. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, key256, 0), 0);
  39045. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, wrapCipher, key256, 0), 0);
  39046. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256, 0), 0);
  39047. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, NULL, key256, 0), 0);
  39048. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, NULL, 0), 0);
  39049. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, NULL, 0), 0);
  39050. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, NULL, NULL, 0), 0);
  39051. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  39052. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, wrapPlain, NULL, 0), 0);
  39053. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, wrapCipher, 0), 0);
  39054. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, wrapPlain, wrapCipher, 0),
  39055. 0);
  39056. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher, 0),
  39057. 0);
  39058. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, NULL, wrapCipher, 0), 0);
  39059. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapPlain, NULL, 0), 0);
  39060. }
  39061. #endif /* HAVE_AES_KEYWRAP */
  39062. }
  39063. #endif /* WOLFSSL_AES_256 */
  39064. #endif
  39065. return EXPECT_RESULT();
  39066. }
  39067. static int test_wolfSSL_AES_cfb128_encrypt(void)
  39068. {
  39069. EXPECT_DECLS;
  39070. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(WOLFSSL_AES_CFB) && \
  39071. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  39072. AES_KEY aesEnc;
  39073. AES_KEY aesDec;
  39074. const byte msg[] = {
  39075. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  39076. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  39077. };
  39078. const byte exp[] = {
  39079. 0x16, 0xc9, 0x90, 0x6c, 0x04, 0x0c, 0xd1, 0x2f,
  39080. 0x84, 0x7b, 0x18, 0xed, 0xed, 0x6a, 0xb5, 0xfd
  39081. };
  39082. const byte key[] = {
  39083. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  39084. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  39085. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  39086. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  39087. };
  39088. const byte ivData[] = {
  39089. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  39090. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  39091. };
  39092. byte out[AES_BLOCK_SIZE];
  39093. byte iv[AES_BLOCK_SIZE];
  39094. word32 i;
  39095. int num;
  39096. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  39097. XMEMCPY(iv, ivData, sizeof(iv));
  39098. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39099. AES_cfb128_encrypt(msg, out, sizeof(msg), &aesEnc, iv, NULL, AES_ENCRYPT);
  39100. ExpectIntEQ(XMEMCMP(out, exp, sizeof(msg)), 0);
  39101. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39102. #ifdef HAVE_AES_DECRYPT
  39103. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  39104. XMEMCPY(iv, ivData, sizeof(iv));
  39105. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39106. AES_cfb128_encrypt(exp, out, sizeof(msg), &aesDec, iv, NULL, AES_DECRYPT);
  39107. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  39108. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39109. #endif
  39110. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(msg)); i++) {
  39111. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  39112. XMEMCPY(iv, ivData, sizeof(iv));
  39113. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39114. AES_cfb128_encrypt(msg, out, i, &aesEnc, iv, &num, AES_ENCRYPT);
  39115. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  39116. ExpectIntEQ(XMEMCMP(out, exp, i), 0);
  39117. if (i == 0) {
  39118. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39119. }
  39120. else {
  39121. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39122. }
  39123. #ifdef HAVE_AES_DECRYPT
  39124. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  39125. XMEMCPY(iv, ivData, sizeof(iv));
  39126. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39127. AES_cfb128_encrypt(exp, out, i, &aesDec, iv, &num, AES_DECRYPT);
  39128. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  39129. ExpectIntEQ(XMEMCMP(out, msg, i), 0);
  39130. if (i == 0) {
  39131. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39132. }
  39133. else {
  39134. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39135. }
  39136. #endif
  39137. }
  39138. if (EXPECT_SUCCESS()) {
  39139. /* test bad arguments */
  39140. AES_cfb128_encrypt(NULL, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  39141. AES_cfb128_encrypt(msg, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  39142. AES_cfb128_encrypt(NULL, out, 0, NULL, NULL, NULL, AES_DECRYPT);
  39143. AES_cfb128_encrypt(NULL, NULL, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  39144. AES_cfb128_encrypt(NULL, NULL, 0, NULL, iv, NULL, AES_DECRYPT);
  39145. AES_cfb128_encrypt(NULL, out, 0, &aesDec, iv, NULL, AES_DECRYPT);
  39146. AES_cfb128_encrypt(msg, NULL, 0, &aesDec, iv, NULL, AES_DECRYPT);
  39147. AES_cfb128_encrypt(msg, out, 0, NULL, iv, NULL, AES_DECRYPT);
  39148. AES_cfb128_encrypt(msg, out, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  39149. }
  39150. #endif
  39151. return EXPECT_RESULT();
  39152. }
  39153. static int test_wolfSSL_CRYPTO_cts128(void)
  39154. {
  39155. EXPECT_DECLS;
  39156. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  39157. defined(HAVE_CTS) && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  39158. byte tmp[64]; /* Largest vector size */
  39159. /* Test vectors taken form RFC3962 Appendix B */
  39160. const testVector vects[] = {
  39161. {
  39162. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39163. "\x20",
  39164. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  39165. "\x97",
  39166. 17, 17
  39167. },
  39168. {
  39169. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39170. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  39171. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  39172. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  39173. 31, 31
  39174. },
  39175. {
  39176. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39177. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  39178. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  39179. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  39180. 32, 32
  39181. },
  39182. {
  39183. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39184. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  39185. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  39186. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  39187. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  39188. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  39189. 47, 47
  39190. },
  39191. {
  39192. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39193. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  39194. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  39195. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  39196. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  39197. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  39198. 48, 48
  39199. },
  39200. {
  39201. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39202. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  39203. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  39204. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  39205. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  39206. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  39207. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  39208. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  39209. 64, 64
  39210. }
  39211. };
  39212. byte keyBytes[AES_128_KEY_SIZE] = {
  39213. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  39214. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  39215. };
  39216. size_t i;
  39217. AES_KEY encKey;
  39218. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  39219. XMEMSET(tmp, 0, sizeof(tmp));
  39220. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  39221. AES_KEY decKey;
  39222. ExpectIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  39223. &encKey), 0);
  39224. ExpectIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  39225. &decKey), 0);
  39226. XMEMSET(iv, 0, sizeof(iv));
  39227. ExpectIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  39228. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  39229. vects[i].outLen);
  39230. ExpectIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  39231. XMEMSET(iv, 0, sizeof(iv));
  39232. ExpectIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  39233. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  39234. vects[i].inLen);
  39235. ExpectIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  39236. }
  39237. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  39238. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  39239. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  39240. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  39241. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  39242. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL,
  39243. (cbc128_f)AES_cbc_encrypt), 0);
  39244. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, &encKey, iv,
  39245. (cbc128_f)AES_cbc_encrypt), 0);
  39246. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, &encKey, iv,
  39247. (cbc128_f)AES_cbc_encrypt), 0);
  39248. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, NULL, iv,
  39249. (cbc128_f)AES_cbc_encrypt), 0);
  39250. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, NULL,
  39251. (cbc128_f)AES_cbc_encrypt), 0);
  39252. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  39253. /* Length too small. */
  39254. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 0, &encKey, iv,
  39255. (cbc128_f)AES_cbc_encrypt), 0);
  39256. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  39257. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  39258. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  39259. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  39260. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  39261. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL,
  39262. (cbc128_f)AES_cbc_encrypt), 0);
  39263. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, &encKey, iv,
  39264. (cbc128_f)AES_cbc_encrypt), 0);
  39265. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, &encKey, iv,
  39266. (cbc128_f)AES_cbc_encrypt), 0);
  39267. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, NULL, iv,
  39268. (cbc128_f)AES_cbc_encrypt), 0);
  39269. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, NULL,
  39270. (cbc128_f)AES_cbc_encrypt), 0);
  39271. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  39272. /* Length too small. */
  39273. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 0, &encKey, iv,
  39274. (cbc128_f)AES_cbc_encrypt), 0);
  39275. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  39276. return EXPECT_RESULT();
  39277. }
  39278. static int test_wolfSSL_RC4(void)
  39279. {
  39280. EXPECT_DECLS;
  39281. #if !defined(NO_RC4) && defined(OPENSSL_EXTRA)
  39282. WOLFSSL_RC4_KEY rc4Key;
  39283. unsigned char key[] = {
  39284. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  39285. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  39286. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  39287. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  39288. };
  39289. unsigned char data[] = {
  39290. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  39291. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  39292. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  39293. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  39294. };
  39295. unsigned char enc[sizeof(data)];
  39296. unsigned char dec[sizeof(data)];
  39297. word32 i;
  39298. word32 j;
  39299. wolfSSL_RC4_set_key(NULL, -1, NULL);
  39300. wolfSSL_RC4_set_key(&rc4Key, -1, NULL);
  39301. wolfSSL_RC4_set_key(NULL, 0, NULL);
  39302. wolfSSL_RC4_set_key(NULL, -1, key);
  39303. wolfSSL_RC4_set_key(&rc4Key, 0, NULL);
  39304. wolfSSL_RC4_set_key(&rc4Key, -1, key);
  39305. wolfSSL_RC4_set_key(NULL, 0, key);
  39306. wolfSSL_RC4(NULL, 0, NULL, NULL);
  39307. wolfSSL_RC4(&rc4Key, 0, NULL, NULL);
  39308. wolfSSL_RC4(NULL, 0, data, NULL);
  39309. wolfSSL_RC4(NULL, 0, NULL, enc);
  39310. wolfSSL_RC4(&rc4Key, 0, data, NULL);
  39311. wolfSSL_RC4(&rc4Key, 0, NULL, enc);
  39312. wolfSSL_RC4(NULL, 0, data, enc);
  39313. ExpectIntEQ(1, 1);
  39314. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(key)); i++) {
  39315. for (j = 0; EXPECT_SUCCESS() && (j <= sizeof(data)); j++) {
  39316. XMEMSET(enc, 0, sizeof(enc));
  39317. XMEMSET(dec, 0, sizeof(dec));
  39318. /* Encrypt */
  39319. wolfSSL_RC4_set_key(&rc4Key, i, key);
  39320. wolfSSL_RC4(&rc4Key, j, data, enc);
  39321. /* Decrypt */
  39322. wolfSSL_RC4_set_key(&rc4Key, i, key);
  39323. wolfSSL_RC4(&rc4Key, j, enc, dec);
  39324. ExpectIntEQ(XMEMCMP(dec, data, j), 0);
  39325. }
  39326. }
  39327. #endif
  39328. return EXPECT_RESULT();
  39329. }
  39330. static int test_wolfSSL_OBJ(void)
  39331. {
  39332. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  39333. * mode
  39334. */
  39335. EXPECT_DECLS;
  39336. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  39337. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  39338. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && \
  39339. !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  39340. ASN1_OBJECT *obj = NULL;
  39341. ASN1_OBJECT *obj2 = NULL;
  39342. char buf[50];
  39343. XFILE fp = XBADFILE;
  39344. X509 *x509 = NULL;
  39345. X509_NAME *x509Name = NULL;
  39346. X509_NAME_ENTRY *x509NameEntry = NULL;
  39347. ASN1_OBJECT *asn1Name = NULL;
  39348. int numNames = 0;
  39349. BIO *bio = NULL;
  39350. int nid;
  39351. int i, j;
  39352. const char *f[] = {
  39353. #ifndef NO_RSA
  39354. "./certs/ca-cert.der",
  39355. #endif
  39356. #ifdef HAVE_ECC
  39357. "./certs/ca-ecc-cert.der",
  39358. "./certs/ca-ecc384-cert.der",
  39359. #endif
  39360. NULL};
  39361. ASN1_OBJECT *field_name_obj = NULL;
  39362. int lastpos = -1;
  39363. int tmp = -1;
  39364. ASN1_STRING *asn1 = NULL;
  39365. unsigned char *buf_dyn = NULL;
  39366. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  39367. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  39368. ExpectIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  39369. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  39370. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  39371. ASN1_OBJECT_free(obj);
  39372. obj = NULL;
  39373. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  39374. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256);
  39375. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  39376. #ifdef WOLFSSL_CERT_EXT
  39377. ExpectIntEQ(OBJ_txt2nid(buf), NID_sha256);
  39378. #endif
  39379. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  39380. ExpectNotNull(obj2 = OBJ_dup(obj));
  39381. ExpectIntEQ(OBJ_cmp(obj, obj2), 0);
  39382. ASN1_OBJECT_free(obj);
  39383. obj = NULL;
  39384. ASN1_OBJECT_free(obj2);
  39385. obj2 = NULL;
  39386. for (i = 0; f[i] != NULL; i++)
  39387. {
  39388. ExpectTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  39389. ExpectNotNull(x509 = d2i_X509_fp(fp, NULL));
  39390. if (fp != XBADFILE) {
  39391. XFCLOSE(fp);
  39392. fp = XBADFILE;
  39393. }
  39394. ExpectNotNull(x509Name = X509_get_issuer_name(x509));
  39395. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  39396. /* Get the Common Name by using OBJ_txt2obj */
  39397. ExpectNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  39398. do
  39399. {
  39400. lastpos = tmp;
  39401. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  39402. } while (tmp > -1);
  39403. ExpectIntNE(lastpos, -1);
  39404. ASN1_OBJECT_free(field_name_obj);
  39405. field_name_obj = NULL;
  39406. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  39407. ExpectNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  39408. ExpectIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  39409. /*
  39410. * All Common Names should be www.wolfssl.com
  39411. * This makes testing easier as we can test for the expected value.
  39412. */
  39413. ExpectStrEQ((char*)buf_dyn, "www.wolfssl.com");
  39414. OPENSSL_free(buf_dyn);
  39415. buf_dyn = NULL;
  39416. bio = BIO_new(BIO_s_mem());
  39417. ExpectTrue(bio != NULL);
  39418. for (j = 0; j < numNames; j++)
  39419. {
  39420. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  39421. ExpectNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  39422. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  39423. }
  39424. BIO_free(bio);
  39425. bio = NULL;
  39426. X509_free(x509);
  39427. x509 = NULL;
  39428. }
  39429. #ifdef HAVE_PKCS12
  39430. {
  39431. PKCS12 *p12 = NULL;
  39432. int boolRet;
  39433. EVP_PKEY *pkey = NULL;
  39434. const char *p12_f[] = {
  39435. #if !defined(NO_DES3) && !defined(NO_RSA)
  39436. "./certs/test-servercert.p12",
  39437. #endif
  39438. NULL};
  39439. for (i = 0; p12_f[i] != NULL; i++)
  39440. {
  39441. ExpectTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  39442. ExpectNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  39443. if (fp != XBADFILE) {
  39444. XFCLOSE(fp);
  39445. fp = XBADFILE;
  39446. }
  39447. ExpectTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  39448. &pkey, &x509, NULL)) > 0);
  39449. wc_PKCS12_free(p12);
  39450. p12 = NULL;
  39451. EVP_PKEY_free(pkey);
  39452. x509Name = X509_get_issuer_name(x509);
  39453. ExpectNotNull(x509Name);
  39454. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  39455. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  39456. for (j = 0; j < numNames; j++)
  39457. {
  39458. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  39459. ExpectNotNull(asn1Name =
  39460. X509_NAME_ENTRY_get_object(x509NameEntry));
  39461. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  39462. }
  39463. BIO_free(bio);
  39464. bio = NULL;
  39465. X509_free(x509);
  39466. x509 = NULL;
  39467. }
  39468. }
  39469. #endif /* HAVE_PKCS12 */
  39470. #endif
  39471. return EXPECT_RESULT();
  39472. }
  39473. static int test_wolfSSL_OBJ_cmp(void)
  39474. {
  39475. EXPECT_DECLS;
  39476. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  39477. ASN1_OBJECT *obj = NULL;
  39478. ASN1_OBJECT *obj2 = NULL;
  39479. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  39480. ExpectNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  39481. ExpectIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  39482. ExpectIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  39483. ExpectIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  39484. ExpectIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  39485. ExpectIntEQ(OBJ_cmp(obj, obj), 0);
  39486. ExpectIntEQ(OBJ_cmp(obj2, obj2), 0);
  39487. ASN1_OBJECT_free(obj);
  39488. ASN1_OBJECT_free(obj2);
  39489. #endif
  39490. return EXPECT_RESULT();
  39491. }
  39492. static int test_wolfSSL_OBJ_txt2nid(void)
  39493. {
  39494. EXPECT_DECLS;
  39495. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  39496. defined(WOLFSSL_APACHE_HTTPD)
  39497. int i;
  39498. static const struct {
  39499. const char* sn;
  39500. const char* ln;
  39501. const char* oid;
  39502. int nid;
  39503. } testVals[] = {
  39504. #ifdef WOLFSSL_APACHE_HTTPD
  39505. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  39506. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  39507. NID_id_on_dnsSRV },
  39508. { "msUPN", "Microsoft User Principal Name",
  39509. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  39510. #endif
  39511. { NULL, NULL, NULL, NID_undef }
  39512. };
  39513. /* Invalid cases */
  39514. ExpectIntEQ(OBJ_txt2nid(NULL), NID_undef);
  39515. ExpectIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  39516. /* Valid cases */
  39517. for (i = 0; testVals[i].sn != NULL; i++) {
  39518. ExpectIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  39519. ExpectIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  39520. ExpectIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  39521. }
  39522. #endif
  39523. return EXPECT_RESULT();
  39524. }
  39525. static int test_wolfSSL_OBJ_txt2obj(void)
  39526. {
  39527. EXPECT_DECLS;
  39528. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  39529. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  39530. int i;
  39531. char buf[50];
  39532. ASN1_OBJECT* obj = NULL;
  39533. static const struct {
  39534. const char* oidStr;
  39535. const char* sn;
  39536. const char* ln;
  39537. } objs_list[] = {
  39538. #if defined(WOLFSSL_APACHE_HTTPD)
  39539. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  39540. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  39541. #endif
  39542. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  39543. { NULL, NULL, NULL }
  39544. };
  39545. static const struct {
  39546. const char* numeric;
  39547. const char* name;
  39548. } objs_named[] = {
  39549. /* In dictionary but not in normal list. */
  39550. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  39551. /* Made up OID. */
  39552. { "1.3.5.7", "1.3.5.7" },
  39553. { NULL, NULL }
  39554. };
  39555. ExpectNull(obj = OBJ_txt2obj("Bad name", 0));
  39556. ASN1_OBJECT_free(obj);
  39557. obj = NULL;
  39558. ExpectNull(obj = OBJ_txt2obj(NULL, 0));
  39559. ASN1_OBJECT_free(obj);
  39560. obj = NULL;
  39561. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  39562. /* Test numerical value of oid (oidStr) */
  39563. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  39564. /* Convert object back to text to confirm oid is correct */
  39565. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  39566. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  39567. ASN1_OBJECT_free(obj);
  39568. obj = NULL;
  39569. XMEMSET(buf, 0, sizeof(buf));
  39570. /* Test short name (sn) */
  39571. ExpectNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  39572. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  39573. /* Convert object back to text to confirm oid is correct */
  39574. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  39575. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  39576. ASN1_OBJECT_free(obj);
  39577. obj = NULL;
  39578. XMEMSET(buf, 0, sizeof(buf));
  39579. /* Test long name (ln) - should fail when no_name = 1 */
  39580. ExpectNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  39581. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  39582. /* Convert object back to text to confirm oid is correct */
  39583. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  39584. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  39585. ASN1_OBJECT_free(obj);
  39586. obj = NULL;
  39587. XMEMSET(buf, 0, sizeof(buf));
  39588. }
  39589. for (i = 0; objs_named[i].numeric != NULL; i++) {
  39590. ExpectNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  39591. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  39592. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  39593. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  39594. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  39595. ASN1_OBJECT_free(obj);
  39596. obj = NULL;
  39597. }
  39598. #endif
  39599. return EXPECT_RESULT();
  39600. }
  39601. static int test_wolfSSL_PEM_write_bio_X509(void)
  39602. {
  39603. EXPECT_DECLS;
  39604. #if defined(OPENSSL_EXTRA) && defined(OPENSSL_ALL) && \
  39605. defined(WOLFSSL_AKID_NAME) && defined(WOLFSSL_CERT_EXT) && \
  39606. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && !defined(NO_RSA) && \
  39607. !defined(NO_FILESYSTEM)
  39608. /* This test contains the hard coded expected
  39609. * lengths. Update if necessary */
  39610. XFILE fp = XBADFILE;
  39611. WOLFSSL_EVP_PKEY *priv = NULL;
  39612. BIO* input = NULL;
  39613. BIO* output = NULL;
  39614. X509* x509a = NULL;
  39615. X509* x509b = NULL;
  39616. ASN1_TIME* notBeforeA = NULL;
  39617. ASN1_TIME* notAfterA = NULL;
  39618. #ifndef NO_ASN_TIME
  39619. ASN1_TIME* notBeforeB = NULL;
  39620. ASN1_TIME* notAfterB = NULL;
  39621. #endif
  39622. int expectedLen;
  39623. ExpectTrue((fp = XFOPEN("certs/server-key.pem", "rb")) != XBADFILE);
  39624. ExpectNotNull(priv = wolfSSL_PEM_read_PrivateKey(fp, NULL, NULL, NULL));
  39625. if (fp != XBADFILE) {
  39626. XFCLOSE(fp);
  39627. fp = XBADFILE;
  39628. }
  39629. ExpectNotNull(input = BIO_new_file("certs/test/cert-ext-multiple.pem",
  39630. "rb"));
  39631. ExpectIntEQ(wolfSSL_BIO_get_len(input), 2000);
  39632. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  39633. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  39634. ExpectNotNull(notBeforeA = X509_get_notBefore(x509a));
  39635. ExpectNotNull(notAfterA = X509_get_notAfter(x509a));
  39636. /* write X509 back to PEM BIO; no need to sign as nothing changed. */
  39637. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  39638. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  39639. /* compare length against expected */
  39640. expectedLen = 2000;
  39641. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  39642. #ifndef NO_ASN_TIME
  39643. /* read exported X509 PEM back into struct, sanity check on export,
  39644. * make sure notBefore/notAfter are the same and certs are identical. */
  39645. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  39646. ExpectNotNull(notBeforeB = X509_get_notBefore(x509b));
  39647. ExpectNotNull(notAfterB = X509_get_notAfter(x509b));
  39648. ExpectIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  39649. ExpectIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  39650. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  39651. X509_free(x509b);
  39652. x509b = NULL;
  39653. #endif
  39654. /* Reset output buffer */
  39655. BIO_free(output);
  39656. output = NULL;
  39657. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  39658. /* Test forcing the AKID to be generated just from KeyIdentifier */
  39659. if (EXPECT_SUCCESS() && x509a->authKeyIdSrc != NULL) {
  39660. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  39661. x509a->authKeyId = x509a->authKeyIdSrc;
  39662. x509a->authKeyIdSrc = NULL;
  39663. x509a->authKeyIdSrcSz = 0;
  39664. }
  39665. /* Resign to re-generate the der */
  39666. ExpectIntGT(wolfSSL_X509_sign(x509a, priv, EVP_sha256()), 0);
  39667. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  39668. /* Check that we generate a smaller output since the AKID will
  39669. * only contain the KeyIdentifier without any additional
  39670. * information */
  39671. /* Here we copy the validity struct from the original */
  39672. expectedLen = 1688;
  39673. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  39674. /* Reset buffers and x509 */
  39675. BIO_free(input);
  39676. input = NULL;
  39677. BIO_free(output);
  39678. output = NULL;
  39679. X509_free(x509a);
  39680. x509a = NULL;
  39681. /* test CA and basicConstSet values are encoded when
  39682. * the cert is a CA */
  39683. ExpectNotNull(input = BIO_new_file("certs/server-cert.pem", "rb"));
  39684. /* read PEM into X509 struct */
  39685. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  39686. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  39687. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  39688. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  39689. /* read exported X509 PEM back into struct, ensure isCa and basicConstSet
  39690. * values are maintained and certs are identical.*/
  39691. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  39692. ExpectIntEQ(x509b->isCa, 1);
  39693. ExpectIntEQ(x509b->basicConstSet, 1);
  39694. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  39695. X509_free(x509a);
  39696. x509a = NULL;
  39697. X509_free(x509b);
  39698. x509b = NULL;
  39699. BIO_free(input);
  39700. input = NULL;
  39701. BIO_free(output);
  39702. output = NULL;
  39703. /* test CA and basicConstSet values are encoded when
  39704. * the cert is not CA */
  39705. ExpectNotNull(input = BIO_new_file("certs/client-uri-cert.pem", "rb"));
  39706. /* read PEM into X509 struct */
  39707. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  39708. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  39709. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  39710. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  39711. /* read exported X509 PEM back into struct, ensure isCa and
  39712. * basicConstSet values are maintained and certs are identical */
  39713. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  39714. ExpectIntEQ(x509b->isCa, 0);
  39715. ExpectIntEQ(x509b->basicConstSet, 1);
  39716. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  39717. wolfSSL_EVP_PKEY_free(priv);
  39718. X509_free(x509a);
  39719. X509_free(x509b);
  39720. BIO_free(input);
  39721. BIO_free(output);
  39722. #endif
  39723. return EXPECT_RESULT();
  39724. }
  39725. static int test_wolfSSL_X509_NAME_ENTRY(void)
  39726. {
  39727. EXPECT_DECLS;
  39728. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  39729. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  39730. X509* x509 = NULL;
  39731. #ifndef NO_BIO
  39732. BIO* bio = NULL;
  39733. #endif
  39734. X509_NAME* nm = NULL;
  39735. X509_NAME_ENTRY* entry = NULL;
  39736. unsigned char cn[] = "another name to add";
  39737. #ifdef OPENSSL_ALL
  39738. int i;
  39739. int names_len = 0;
  39740. #endif
  39741. ExpectNotNull(x509 =
  39742. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  39743. #ifndef NO_BIO
  39744. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39745. ExpectIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  39746. #endif
  39747. #ifdef WOLFSSL_CERT_REQ
  39748. {
  39749. X509_REQ* req = NULL;
  39750. #ifndef NO_BIO
  39751. BIO* bReq = NULL;
  39752. #endif
  39753. ExpectNotNull(req =
  39754. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  39755. #ifndef NO_BIO
  39756. ExpectNotNull(bReq = BIO_new(BIO_s_mem()));
  39757. ExpectIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  39758. BIO_free(bReq);
  39759. #endif
  39760. X509_free(req);
  39761. }
  39762. #endif
  39763. ExpectNotNull(nm = X509_get_subject_name(x509));
  39764. /* Test add entry */
  39765. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  39766. 0x0c, cn, (int)sizeof(cn)));
  39767. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  39768. #ifdef WOLFSSL_CERT_EXT
  39769. ExpectIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  39770. (byte*)"support@wolfssl.com", 19, -1,
  39771. 1), WOLFSSL_SUCCESS);
  39772. #endif
  39773. X509_NAME_ENTRY_free(entry);
  39774. entry = NULL;
  39775. #ifdef WOLFSSL_CERT_REQ
  39776. {
  39777. unsigned char srv_pkcs9p[] = "Server";
  39778. unsigned char fvrtDrnk[] = "tequila";
  39779. unsigned char* der = NULL;
  39780. char* subject = NULL;
  39781. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  39782. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  39783. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_favouriteDrink,
  39784. MBSTRING_ASC, fvrtDrnk, -1, -1, 0), SSL_SUCCESS);
  39785. ExpectIntGT(wolfSSL_i2d_X509_NAME(nm, &der), 0);
  39786. ExpectNotNull(der);
  39787. ExpectNotNull(subject = X509_NAME_oneline(nm, 0, 0));
  39788. ExpectNotNull(XSTRSTR(subject, "favouriteDrink=tequila"));
  39789. ExpectNotNull(XSTRSTR(subject, "contentType=Server"));
  39790. #ifdef DEBUG_WOLFSSL
  39791. if (subject != NULL) {
  39792. fprintf(stderr, "\n\t%s\n", subject);
  39793. }
  39794. #endif
  39795. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  39796. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  39797. }
  39798. #endif
  39799. /* Test add entry by text */
  39800. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  39801. 0x0c, cn, (int)sizeof(cn)));
  39802. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  39803. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  39804. ExpectNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  39805. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  39806. #endif
  39807. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  39808. X509_NAME_ENTRY_free(entry);
  39809. entry = NULL;
  39810. /* Test add entry by NID */
  39811. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  39812. cn, -1, -1, 0), SSL_SUCCESS);
  39813. #ifdef OPENSSL_ALL
  39814. /* stack of name entry */
  39815. ExpectIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  39816. for (i = 0; i < names_len; i++) {
  39817. ExpectNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  39818. }
  39819. #endif
  39820. #ifndef NO_BIO
  39821. BIO_free(bio);
  39822. #endif
  39823. X509_free(x509); /* free's nm */
  39824. #endif
  39825. return EXPECT_RESULT();
  39826. }
  39827. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  39828. static int test_GENERAL_NAME_set0_othername(void) {
  39829. EXPECT_DECLS;
  39830. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  39831. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  39832. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  39833. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  39834. defined(WOLFSSL_FPKI)
  39835. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  39836. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  39837. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  39838. const char * cert_fname = "./certs/server-cert.der";
  39839. const char * key_fname = "./certs/server-key.der";
  39840. X509* x509 = NULL;
  39841. GENERAL_NAME* gn = NULL;
  39842. GENERAL_NAMES* gns = NULL;
  39843. ASN1_OBJECT* upn_oid = NULL;
  39844. ASN1_UTF8STRING *utf8str = NULL;
  39845. ASN1_TYPE *value = NULL;
  39846. X509_EXTENSION * ext = NULL;
  39847. byte* pt = NULL;
  39848. byte der[4096];
  39849. int derSz = 0;
  39850. EVP_PKEY* priv = NULL;
  39851. XFILE f = XBADFILE;
  39852. ExpectTrue((f = XFOPEN(cert_fname, "rb")) != XBADFILE);
  39853. ExpectNotNull(x509 = d2i_X509_fp(f, NULL));
  39854. if (f != XBADFILE) {
  39855. XFCLOSE(f);
  39856. f = XBADFILE;
  39857. }
  39858. ExpectNotNull(gn = GENERAL_NAME_new());
  39859. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  39860. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  39861. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  39862. ExpectNotNull(value = ASN1_TYPE_new());
  39863. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  39864. if ((value == NULL) || (value->value.ptr != (char*)utf8str)) {
  39865. wolfSSL_ASN1_STRING_free(utf8str);
  39866. }
  39867. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  39868. if (EXPECT_FAIL()) {
  39869. ASN1_TYPE_free(value);
  39870. }
  39871. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  39872. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  39873. if (EXPECT_FAIL()) {
  39874. GENERAL_NAME_free(gn);
  39875. gn = NULL;
  39876. }
  39877. ExpectNotNull(ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  39878. ExpectIntEQ(X509_add_ext(x509, ext, -1), 1);
  39879. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  39880. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  39881. if (f != XBADFILE) {
  39882. XFCLOSE(f);
  39883. f = XBADFILE;
  39884. }
  39885. pt = der;
  39886. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  39887. (const unsigned char**)&pt, derSz));
  39888. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  39889. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  39890. gns = NULL;
  39891. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  39892. NULL));
  39893. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 3);
  39894. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 2));
  39895. ExpectIntEQ(gn->type, 0);
  39896. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  39897. ASN1_OBJECT_free(upn_oid);
  39898. X509_EXTENSION_free(ext);
  39899. X509_free(x509);
  39900. EVP_PKEY_free(priv);
  39901. #endif
  39902. return EXPECT_RESULT();
  39903. }
  39904. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  39905. static int test_othername_and_SID_ext(void) {
  39906. EXPECT_DECLS;
  39907. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  39908. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  39909. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  39910. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  39911. defined(WOLFSSL_FPKI) && defined(WOLFSSL_ASN_TEMPLATE)
  39912. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  39913. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  39914. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  39915. const char* csr_fname = "./certs/csr.signed.der";
  39916. const char* key_fname = "./certs/server-key.der";
  39917. byte der[4096];
  39918. int derSz = 0;
  39919. X509_REQ* x509 = NULL;
  39920. STACK_OF(X509_EXTENSION) *exts = NULL;
  39921. X509_EXTENSION * san_ext = NULL;
  39922. X509_EXTENSION * ext = NULL;
  39923. GENERAL_NAME* gn = NULL;
  39924. GENERAL_NAMES* gns = NULL;
  39925. ASN1_OBJECT* upn_oid = NULL;
  39926. ASN1_UTF8STRING *utf8str = NULL;
  39927. ASN1_TYPE *value = NULL;
  39928. ASN1_STRING *extval = NULL;
  39929. /* SID extension. SID data format explained here:
  39930. * https://blog.qdsecurity.se/2022/05/27/manually-injecting-a-sid-in-a-certificate/
  39931. */
  39932. uint8_t SidExtension[] = {
  39933. 48, 64, 160, 62, 6, 10, 43, 6, 1, 4, 1, 130, 55, 25, 2, 1, 160,
  39934. 48, 4, 46, 83, 45, 49, 45, 53, 45, 50, 49, 45, 50, 56, 52, 51, 57,
  39935. 48, 55, 52, 49, 56, 45, 51, 57, 50, 54, 50, 55, 55, 52, 50, 49, 45,
  39936. 51, 56, 49, 53, 57, 57, 51, 57, 55, 50, 45, 52, 54, 48, 49};
  39937. uint8_t expectedAltName[] = {
  39938. 0x30, 0x27, 0xA0, 0x25, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82,
  39939. 0x37, 0x14, 0x02, 0x03, 0xA0, 0x17, 0x0C, 0x15, 0x6F, 0x74, 0x68, 0x65,
  39940. 0x72, 0x6E, 0x61, 0x6D, 0x65, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  39941. 0x6C, 0x2E, 0x63, 0x6F, 0x6D};
  39942. X509_EXTENSION *sid_ext = NULL;
  39943. ASN1_OBJECT* sid_oid = NULL;
  39944. ASN1_OCTET_STRING *sid_data = NULL;
  39945. EVP_PKEY* priv = NULL;
  39946. XFILE f = XBADFILE;
  39947. byte* pt = NULL;
  39948. BIO* bio = NULL;
  39949. ExpectTrue((f = XFOPEN(csr_fname, "rb")) != XBADFILE);
  39950. ExpectNotNull(x509 = d2i_X509_REQ_fp(f, NULL));
  39951. if (f != XBADFILE) {
  39952. XFCLOSE(f);
  39953. f = XBADFILE;
  39954. }
  39955. ExpectIntEQ(X509_REQ_set_version(x509, 2), 1);
  39956. ExpectNotNull(gn = GENERAL_NAME_new());
  39957. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  39958. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  39959. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  39960. ExpectNotNull(value = ASN1_TYPE_new());
  39961. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  39962. if (EXPECT_FAIL()) {
  39963. ASN1_UTF8STRING_free(utf8str);
  39964. }
  39965. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  39966. if (EXPECT_FAIL()) {
  39967. ASN1_TYPE_free(value);
  39968. GENERAL_NAME_free(gn);
  39969. gn = NULL;
  39970. }
  39971. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  39972. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  39973. if (EXPECT_FAIL()) {
  39974. GENERAL_NAME_free(gn);
  39975. }
  39976. ExpectNotNull(san_ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  39977. ExpectNotNull(sid_oid = OBJ_txt2obj("1.3.6.1.4.1.311.25.2", 1));
  39978. ExpectNotNull(sid_data = ASN1_OCTET_STRING_new());
  39979. ASN1_OCTET_STRING_set(sid_data, SidExtension, sizeof(SidExtension));
  39980. ExpectNotNull(sid_ext = X509_EXTENSION_create_by_OBJ(NULL, sid_oid, 0,
  39981. sid_data));
  39982. ExpectNotNull(exts = sk_X509_EXTENSION_new_null());
  39983. /* Ensure an empty stack doesn't raise an error. */
  39984. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  39985. ExpectIntEQ(sk_X509_EXTENSION_push(exts, san_ext), 1);
  39986. if (EXPECT_FAIL()) {
  39987. X509_EXTENSION_free(san_ext);
  39988. }
  39989. ExpectIntEQ(sk_X509_EXTENSION_push(exts, sid_ext), 2);
  39990. if (EXPECT_FAIL()) {
  39991. X509_EXTENSION_free(sid_ext);
  39992. }
  39993. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  39994. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  39995. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  39996. if (f != XBADFILE)
  39997. XFCLOSE(f);
  39998. pt = der;
  39999. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  40000. (const unsigned char**)&pt, derSz));
  40001. ExpectIntGT(X509_REQ_sign(x509, priv, EVP_sha256()), 0);
  40002. pt = der;
  40003. ExpectIntGT(derSz = i2d_X509_REQ(x509, &pt), 0);
  40004. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  40005. gns = NULL;
  40006. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  40007. exts = NULL;
  40008. ASN1_OBJECT_free(upn_oid);
  40009. ASN1_OBJECT_free(sid_oid);
  40010. ASN1_OCTET_STRING_free(sid_data);
  40011. X509_REQ_free(x509);
  40012. x509 = NULL;
  40013. EVP_PKEY_free(priv);
  40014. /* At this point everything used to generate what is in der is cleaned up.
  40015. * We now read back from der to confirm the extensions were inserted
  40016. * correctly. */
  40017. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
  40018. ExpectNotNull(bio);
  40019. ExpectIntEQ(BIO_write(bio, der, derSz), derSz); /* d2i consumes BIO */
  40020. ExpectNotNull(d2i_X509_REQ_bio(bio, &x509));
  40021. ExpectNotNull(x509);
  40022. BIO_free(bio);
  40023. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  40024. x509));
  40025. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  40026. /* Check the SID extension. */
  40027. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 0));
  40028. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  40029. ExpectIntEQ(extval->length, sizeof(SidExtension));
  40030. ExpectIntEQ(XMEMCMP(SidExtension, extval->data, sizeof(SidExtension)), 0);
  40031. /* Check the AltNames extension. */
  40032. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 1));
  40033. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  40034. ExpectIntEQ(extval->length, sizeof(expectedAltName));
  40035. ExpectIntEQ(XMEMCMP(expectedAltName, extval->data, sizeof(expectedAltName)),
  40036. 0);
  40037. /* Cleanup */
  40038. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  40039. NULL));
  40040. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  40041. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 0));
  40042. ExpectIntEQ(gn->type, 0);
  40043. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  40044. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  40045. X509_REQ_free(x509);
  40046. #endif
  40047. return EXPECT_RESULT();
  40048. }
  40049. static int test_wolfSSL_X509_set_name(void)
  40050. {
  40051. EXPECT_DECLS;
  40052. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  40053. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  40054. X509* x509 = NULL;
  40055. X509_NAME* name = NULL;
  40056. ExpectNotNull(name = X509_NAME_new());
  40057. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  40058. (byte*)"wolfssl.com", 11, 0, 1),
  40059. WOLFSSL_SUCCESS);
  40060. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  40061. (byte*)"support@wolfssl.com", 19, -1,
  40062. 1), WOLFSSL_SUCCESS);
  40063. ExpectNotNull(x509 = X509_new());
  40064. ExpectIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  40065. ExpectIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  40066. ExpectIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  40067. ExpectIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  40068. ExpectIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  40069. ExpectIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  40070. ExpectIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  40071. ExpectIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  40072. X509_free(x509);
  40073. X509_NAME_free(name);
  40074. #endif /* OPENSSL_ALL && !NO_CERTS */
  40075. return EXPECT_RESULT();
  40076. }
  40077. static int test_wolfSSL_X509_set_notAfter(void)
  40078. {
  40079. EXPECT_DECLS;
  40080. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  40081. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  40082. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  40083. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  40084. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  40085. /* Generalized time will overflow time_t if not long */
  40086. X509* x = NULL;
  40087. BIO* bio = NULL;
  40088. ASN1_TIME *asn_time = NULL;
  40089. ASN1_TIME *time_check = NULL;
  40090. const int year = 365*24*60*60;
  40091. const int day = 24*60*60;
  40092. const int hour = 60*60;
  40093. const int mini = 60;
  40094. int offset_day;
  40095. unsigned char buf[25];
  40096. time_t t;
  40097. /*
  40098. * Setup asn_time. APACHE HTTPD uses time(NULL)
  40099. */
  40100. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  40101. offset_day = 7;
  40102. /*
  40103. * Free these.
  40104. */
  40105. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  40106. ExpectNotNull(asn_time);
  40107. ExpectNotNull(x = X509_new());
  40108. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40109. /*
  40110. * Tests
  40111. */
  40112. ExpectTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  40113. /* time_check is simply (ANS1_TIME*)x->notAfter */
  40114. ExpectNotNull(time_check = X509_get_notAfter(x));
  40115. /* ANS1_TIME_check validates by checking if argument can be parsed */
  40116. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  40117. /* Convert to human readable format and compare to intended date */
  40118. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  40119. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  40120. ExpectIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  40121. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, NULL));
  40122. ExpectFalse(wolfSSL_X509_set_notAfter(x, NULL));
  40123. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, asn_time));
  40124. /*
  40125. * Cleanup
  40126. */
  40127. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  40128. X509_free(x);
  40129. BIO_free(bio);
  40130. #endif
  40131. return EXPECT_RESULT();
  40132. }
  40133. static int test_wolfSSL_X509_set_notBefore(void)
  40134. {
  40135. EXPECT_DECLS;
  40136. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  40137. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  40138. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  40139. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  40140. X509* x = NULL;
  40141. BIO* bio = NULL;
  40142. ASN1_TIME *asn_time = NULL;
  40143. ASN1_TIME *time_check = NULL;
  40144. const int year = 365*24*60*60;
  40145. const int day = 24*60*60;
  40146. const int hour = 60*60;
  40147. const int mini = 60;
  40148. int offset_day;
  40149. unsigned char buf[25];
  40150. time_t t;
  40151. /*
  40152. * Setup asn_time. APACHE HTTPD uses time(NULL)
  40153. */
  40154. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  40155. offset_day = 7;
  40156. /*
  40157. * Free these.
  40158. */
  40159. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  40160. ExpectNotNull(asn_time);
  40161. ExpectNotNull(x = X509_new());
  40162. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40163. ExpectIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  40164. /*
  40165. * Main Tests
  40166. */
  40167. ExpectTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  40168. /* time_check == (ANS1_TIME*)x->notBefore */
  40169. ExpectNotNull(time_check = X509_get_notBefore(x));
  40170. /* ANS1_TIME_check validates by checking if argument can be parsed */
  40171. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  40172. /* Convert to human readable format and compare to intended date */
  40173. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  40174. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  40175. ExpectIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  40176. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, NULL));
  40177. ExpectFalse(wolfSSL_X509_set_notBefore(x, NULL));
  40178. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, asn_time));
  40179. /*
  40180. * Cleanup
  40181. */
  40182. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  40183. X509_free(x);
  40184. BIO_free(bio);
  40185. #endif
  40186. return EXPECT_RESULT();
  40187. }
  40188. static int test_wolfSSL_X509_set_version(void)
  40189. {
  40190. EXPECT_DECLS;
  40191. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  40192. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  40193. X509* x509 = NULL;
  40194. long v = 2L;
  40195. long maxInt = INT_MAX;
  40196. ExpectNotNull(x509 = X509_new());
  40197. /* These should pass. */
  40198. ExpectTrue(wolfSSL_X509_set_version(x509, v));
  40199. ExpectIntEQ(v, wolfSSL_X509_get_version(x509));
  40200. /* Fail Case: When v(long) is greater than x509->version(int). */
  40201. v = maxInt+1;
  40202. ExpectFalse(wolfSSL_X509_set_version(x509, v));
  40203. ExpectFalse(wolfSSL_X509_set_version(NULL, 2L));
  40204. ExpectFalse(wolfSSL_X509_set_version(NULL, maxInt+1));
  40205. /* Cleanup */
  40206. X509_free(x509);
  40207. #endif
  40208. return EXPECT_RESULT();
  40209. }
  40210. #ifndef NO_BIO
  40211. static int test_wolfSSL_BIO_gets(void)
  40212. {
  40213. EXPECT_DECLS;
  40214. #if defined(OPENSSL_EXTRA)
  40215. BIO* bio = NULL;
  40216. BIO* bio2 = NULL;
  40217. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  40218. char emp[] = "";
  40219. char bio_buffer[20];
  40220. int bufferSz = 20;
  40221. #ifdef OPENSSL_ALL
  40222. BUF_MEM* emp_bm = NULL;
  40223. BUF_MEM* msg_bm = NULL;
  40224. #endif
  40225. /* try with bad args */
  40226. ExpectNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  40227. #ifdef OPENSSL_ALL
  40228. ExpectIntEQ(BIO_set_mem_buf(bio, NULL, BIO_NOCLOSE), BAD_FUNC_ARG);
  40229. #endif
  40230. /* try with real msg */
  40231. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  40232. XMEMSET(bio_buffer, 0, bufferSz);
  40233. ExpectNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  40234. ExpectNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  40235. ExpectNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  40236. ExpectFalse(bio2 != BIO_next(bio));
  40237. /* make buffer filled with no terminating characters */
  40238. XMEMSET(bio_buffer, 1, bufferSz);
  40239. /* BIO_gets reads a line of data */
  40240. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  40241. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  40242. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  40243. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  40244. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  40245. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  40246. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  40247. #ifdef OPENSSL_ALL
  40248. /* test setting the mem_buf manually */
  40249. BIO_free(bio);
  40250. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  40251. ExpectNotNull(emp_bm = BUF_MEM_new());
  40252. ExpectNotNull(msg_bm = BUF_MEM_new());
  40253. ExpectIntEQ(BUF_MEM_grow(msg_bm, sizeof(msg)), sizeof(msg));
  40254. if (EXPECT_SUCCESS()) {
  40255. XFREE(msg_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  40256. msg_bm->data = NULL;
  40257. }
  40258. /* emp size is 1 for terminator */
  40259. ExpectIntEQ(BUF_MEM_grow(emp_bm, sizeof(emp)), sizeof(emp));
  40260. if (EXPECT_SUCCESS()) {
  40261. XFREE(emp_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  40262. emp_bm->data = emp;
  40263. msg_bm->data = msg;
  40264. }
  40265. ExpectIntEQ(BIO_set_mem_buf(bio, emp_bm, BIO_CLOSE), WOLFSSL_SUCCESS);
  40266. /* check reading an empty string */
  40267. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  40268. ExpectStrEQ(emp, bio_buffer);
  40269. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  40270. /* BIO_gets reads a line of data */
  40271. ExpectIntEQ(BIO_set_mem_buf(bio, msg_bm, BIO_NOCLOSE), WOLFSSL_SUCCESS);
  40272. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  40273. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  40274. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  40275. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  40276. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  40277. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  40278. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  40279. if (EXPECT_SUCCESS())
  40280. emp_bm->data = NULL;
  40281. BUF_MEM_free(emp_bm);
  40282. if (EXPECT_SUCCESS())
  40283. msg_bm->data = NULL;
  40284. BUF_MEM_free(msg_bm);
  40285. #endif
  40286. /* check not null terminated string */
  40287. BIO_free(bio);
  40288. bio = NULL;
  40289. msg[0] = 0x33;
  40290. msg[1] = 0x33;
  40291. msg[2] = 0x33;
  40292. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  40293. ExpectIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  40294. ExpectIntEQ(bio_buffer[0], msg[0]);
  40295. ExpectIntEQ(bio_buffer[1], msg[1]);
  40296. ExpectIntNE(bio_buffer[2], msg[2]);
  40297. BIO_free(bio);
  40298. bio = NULL;
  40299. msg[3] = 0x33;
  40300. bio_buffer[3] = 0x33;
  40301. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  40302. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  40303. ExpectIntEQ(bio_buffer[0], msg[0]);
  40304. ExpectIntEQ(bio_buffer[1], msg[1]);
  40305. ExpectIntEQ(bio_buffer[2], msg[2]);
  40306. ExpectIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  40307. /* check reading an empty string */
  40308. BIO_free(bio);
  40309. bio = NULL;
  40310. ExpectNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  40311. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  40312. ExpectStrEQ(emp, bio_buffer);
  40313. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  40314. /* check error cases */
  40315. BIO_free(bio);
  40316. bio = NULL;
  40317. ExpectIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  40318. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40319. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  40320. #if !defined(NO_FILESYSTEM)
  40321. {
  40322. BIO* f_bio = NULL;
  40323. XFILE f = XBADFILE;
  40324. ExpectNotNull(f_bio = BIO_new(BIO_s_file()));
  40325. ExpectIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  40326. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  40327. ExpectIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  40328. if (EXPECT_FAIL() && (f != XBADFILE)) {
  40329. XFCLOSE(f);
  40330. }
  40331. ExpectIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  40332. BIO_free(f_bio);
  40333. f_bio = NULL;
  40334. }
  40335. #endif /* NO_FILESYSTEM */
  40336. BIO_free(bio);
  40337. bio = NULL;
  40338. BIO_free(bio2);
  40339. bio2 = NULL;
  40340. /* try with type BIO */
  40341. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  40342. sizeof(msg));
  40343. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  40344. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  40345. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  40346. ExpectIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  40347. ExpectIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  40348. ExpectIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  40349. ExpectIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  40350. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  40351. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  40352. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  40353. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  40354. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  40355. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  40356. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  40357. BIO_free(bio);
  40358. bio = NULL;
  40359. BIO_free(bio2);
  40360. bio2 = NULL;
  40361. /* check reading an empty string */
  40362. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  40363. ExpectIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  40364. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  40365. ExpectStrEQ(emp, bio_buffer);
  40366. BIO_free(bio);
  40367. bio = NULL;
  40368. #endif
  40369. return EXPECT_RESULT();
  40370. }
  40371. static int test_wolfSSL_BIO_puts(void)
  40372. {
  40373. EXPECT_DECLS;
  40374. #if defined(OPENSSL_EXTRA)
  40375. BIO* bio = NULL;
  40376. char input[] = "hello\0world\n.....ok\n\0";
  40377. char output[128];
  40378. XMEMSET(output, 0, sizeof(output));
  40379. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40380. ExpectIntEQ(BIO_puts(bio, input), 5);
  40381. ExpectIntEQ(BIO_pending(bio), 5);
  40382. ExpectIntEQ(BIO_puts(bio, input + 6), 14);
  40383. ExpectIntEQ(BIO_pending(bio), 19);
  40384. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  40385. ExpectStrEQ(output, "helloworld\n");
  40386. ExpectIntEQ(BIO_pending(bio), 8);
  40387. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  40388. ExpectStrEQ(output, ".....ok\n");
  40389. ExpectIntEQ(BIO_pending(bio), 0);
  40390. ExpectIntEQ(BIO_puts(bio, ""), -1);
  40391. BIO_free(bio);
  40392. #endif
  40393. return EXPECT_RESULT();
  40394. }
  40395. static int test_wolfSSL_BIO_dump(void)
  40396. {
  40397. EXPECT_DECLS;
  40398. #if defined(OPENSSL_EXTRA)
  40399. BIO* bio;
  40400. static const unsigned char data[] = {
  40401. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  40402. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  40403. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  40404. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  40405. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  40406. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  40407. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  40408. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  40409. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  40410. 0xB4
  40411. };
  40412. /* Generated with OpenSSL. */
  40413. static const char expected[] =
  40414. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  40415. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  40416. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  40417. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  40418. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  40419. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  40420. static const char expectedAll[] =
  40421. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  40422. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  40423. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  40424. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  40425. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  40426. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  40427. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  40428. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  40429. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  40430. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  40431. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  40432. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  40433. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  40434. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  40435. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  40436. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  40437. char output[16 * 80];
  40438. int i;
  40439. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40440. /* Example key dumped. */
  40441. ExpectIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  40442. sizeof(expected) - 1);
  40443. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  40444. ExpectIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  40445. /* Try every possible value for a character. */
  40446. for (i = 0; i < 256; i++)
  40447. output[i] = i;
  40448. ExpectIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  40449. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  40450. ExpectIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  40451. BIO_free(bio);
  40452. #endif
  40453. return EXPECT_RESULT();
  40454. }
  40455. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40456. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  40457. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  40458. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  40459. {
  40460. (void)ssl;
  40461. (void)buf;
  40462. (void)sz;
  40463. (void)ctx;
  40464. return WOLFSSL_CBIO_ERR_WANT_READ;
  40465. }
  40466. #endif
  40467. static int test_wolfSSL_BIO_should_retry(void)
  40468. {
  40469. EXPECT_DECLS;
  40470. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40471. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  40472. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  40473. tcp_ready ready;
  40474. func_args server_args;
  40475. THREAD_TYPE serverThread;
  40476. SOCKET_T sockfd = 0;
  40477. WOLFSSL_CTX* ctx = NULL;
  40478. WOLFSSL* ssl = NULL;
  40479. char msg[64] = "hello wolfssl!";
  40480. char reply[1024];
  40481. int msgSz = (int)XSTRLEN(msg);
  40482. int ret;
  40483. BIO* bio = NULL;
  40484. XMEMSET(&server_args, 0, sizeof(func_args));
  40485. #ifdef WOLFSSL_TIRTOS
  40486. fdOpenSession(Task_self());
  40487. #endif
  40488. StartTCP();
  40489. InitTcpReady(&ready);
  40490. #if defined(USE_WINDOWS_API)
  40491. /* use RNG to get random port if using windows */
  40492. ready.port = GetRandomPort();
  40493. #endif
  40494. server_args.signal = &ready;
  40495. start_thread(test_server_nofail, &server_args, &serverThread);
  40496. wait_tcp_ready(&server_args);
  40497. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40498. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  40499. ExpectIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  40500. #endif
  40501. ExpectIntEQ(WOLFSSL_SUCCESS,
  40502. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  40503. ExpectIntEQ(WOLFSSL_SUCCESS,
  40504. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  40505. ExpectIntEQ(WOLFSSL_SUCCESS,
  40506. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  40507. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  40508. /* force retry */
  40509. ExpectNotNull(bio = wolfSSL_BIO_new_ssl(ctx, 1));
  40510. ExpectIntEQ(BIO_get_ssl(bio, &ssl), 1);
  40511. ExpectNotNull(ssl);
  40512. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  40513. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  40514. if (EXPECT_FAIL()) {
  40515. wolfSSL_free(ssl);
  40516. ssl = NULL;
  40517. }
  40518. ExpectIntLE(BIO_write(bio, msg, msgSz), 0);
  40519. ExpectIntNE(BIO_should_retry(bio), 0);
  40520. ExpectIntEQ(BIO_should_read(bio), 0);
  40521. ExpectIntEQ(BIO_should_write(bio), 0);
  40522. /* now perform successful connection */
  40523. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  40524. ExpectIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  40525. ExpectIntNE(BIO_read(bio, reply, sizeof(reply)), 0);
  40526. ret = wolfSSL_get_error(ssl, -1);
  40527. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  40528. ExpectIntNE(BIO_should_retry(bio), 0);
  40529. if (ret == WOLFSSL_ERROR_WANT_READ)
  40530. ExpectIntEQ(BIO_should_read(bio), 1);
  40531. else
  40532. ExpectIntEQ(BIO_should_read(bio), 0);
  40533. if (ret == WOLFSSL_ERROR_WANT_WRITE)
  40534. ExpectIntEQ(BIO_should_write(bio), 1);
  40535. else
  40536. ExpectIntEQ(BIO_should_write(bio), 0);
  40537. }
  40538. else {
  40539. ExpectIntEQ(BIO_should_retry(bio), 0);
  40540. ExpectIntEQ(BIO_should_read(bio), 0);
  40541. ExpectIntEQ(BIO_should_write(bio), 0);
  40542. }
  40543. ExpectIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  40544. XSTRLEN("I hear you fa shizzle!")), 0);
  40545. BIO_free(bio);
  40546. wolfSSL_CTX_free(ctx);
  40547. CloseSocket(sockfd);
  40548. join_thread(serverThread);
  40549. FreeTcpReady(&ready);
  40550. #ifdef WOLFSSL_TIRTOS
  40551. fdOpenSession(Task_self());
  40552. #endif
  40553. #endif
  40554. return EXPECT_RESULT();
  40555. }
  40556. static int test_wolfSSL_BIO_connect(void)
  40557. {
  40558. EXPECT_DECLS;
  40559. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40560. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  40561. tcp_ready ready;
  40562. func_args server_args;
  40563. THREAD_TYPE serverThread;
  40564. BIO *tcpBio = NULL;
  40565. BIO *sslBio = NULL;
  40566. SSL_CTX* ctx = NULL;
  40567. SSL *ssl = NULL;
  40568. SSL *sslPtr;
  40569. char msg[] = "hello wolfssl!";
  40570. char reply[30];
  40571. char buff[10] = {0};
  40572. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40573. ExpectIntEQ(WOLFSSL_SUCCESS,
  40574. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  40575. ExpectIntEQ(WOLFSSL_SUCCESS,
  40576. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  40577. ExpectIntEQ(WOLFSSL_SUCCESS,
  40578. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  40579. /* Setup server */
  40580. XMEMSET(&server_args, 0, sizeof(func_args));
  40581. StartTCP();
  40582. InitTcpReady(&ready);
  40583. #if defined(USE_WINDOWS_API)
  40584. /* use RNG to get random port if using windows */
  40585. ready.port = GetRandomPort();
  40586. #endif
  40587. server_args.signal = &ready;
  40588. start_thread(test_server_nofail, &server_args, &serverThread);
  40589. wait_tcp_ready(&server_args);
  40590. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  40591. /* Start the test proper */
  40592. /* Setup the TCP BIO */
  40593. ExpectNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  40594. ExpectIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  40595. /* Setup the SSL object */
  40596. ExpectNotNull(ssl = SSL_new(ctx));
  40597. SSL_set_connect_state(ssl);
  40598. /* Setup the SSL BIO */
  40599. ExpectNotNull(sslBio = BIO_new(BIO_f_ssl()));
  40600. ExpectIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  40601. if (EXPECT_FAIL()) {
  40602. wolfSSL_free(ssl);
  40603. }
  40604. /* Verify that BIO_get_ssl works. */
  40605. ExpectIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  40606. ExpectPtrEq(ssl, sslPtr);
  40607. /* Link BIO's so that sslBio uses tcpBio for IO */
  40608. ExpectPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  40609. /* Do TCP connect */
  40610. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  40611. /* Do TLS handshake */
  40612. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  40613. /* Test writing */
  40614. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  40615. /* Expect length of default wolfSSL reply */
  40616. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  40617. /* Clean it all up */
  40618. BIO_free_all(sslBio);
  40619. /* Server clean up */
  40620. join_thread(serverThread);
  40621. FreeTcpReady(&ready);
  40622. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  40623. * after. */
  40624. XMEMSET(&server_args, 0, sizeof(func_args));
  40625. StartTCP();
  40626. InitTcpReady(&ready);
  40627. #if defined(USE_WINDOWS_API)
  40628. /* use RNG to get random port if using windows */
  40629. ready.port = GetRandomPort();
  40630. #endif
  40631. server_args.signal = &ready;
  40632. start_thread(test_server_nofail, &server_args, &serverThread);
  40633. wait_tcp_ready(&server_args);
  40634. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  40635. ExpectNotNull(sslBio = BIO_new_ssl_connect(ctx));
  40636. ExpectIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  40637. ExpectIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  40638. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  40639. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  40640. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  40641. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  40642. /* Attempt to close the TLS connection gracefully. */
  40643. BIO_ssl_shutdown(sslBio);
  40644. BIO_free_all(sslBio);
  40645. join_thread(serverThread);
  40646. FreeTcpReady(&ready);
  40647. SSL_CTX_free(ctx);
  40648. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  40649. wc_ecc_fp_free(); /* free per thread cache */
  40650. #endif
  40651. #endif
  40652. return EXPECT_RESULT();
  40653. }
  40654. static int test_wolfSSL_BIO_tls(void)
  40655. {
  40656. EXPECT_DECLS;
  40657. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  40658. SSL_CTX* ctx = NULL;
  40659. SSL *ssl = NULL;
  40660. BIO *readBio = NULL;
  40661. BIO *writeBio = NULL;
  40662. int ret;
  40663. int err = 0;
  40664. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  40665. ExpectNotNull(ssl = SSL_new(ctx));
  40666. ExpectNotNull(readBio = BIO_new(BIO_s_mem()));
  40667. ExpectNotNull(writeBio = BIO_new(BIO_s_mem()));
  40668. /* Qt reads data from write-bio,
  40669. * then writes the read data into plain packet.
  40670. * Qt reads data from plain packet,
  40671. * then writes the read data into read-bio.
  40672. */
  40673. SSL_set_bio(ssl, readBio, writeBio);
  40674. do {
  40675. #ifdef WOLFSSL_ASYNC_CRYPT
  40676. if (err == WC_PENDING_E) {
  40677. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  40678. if (ret < 0) { break; } else if (ret == 0) { continue; }
  40679. }
  40680. #endif
  40681. ret = SSL_connect(ssl);
  40682. err = SSL_get_error(ssl, 0);
  40683. } while (err == WC_PENDING_E);
  40684. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  40685. /* in this use case, should return WANT READ
  40686. * so that Qt will read the data from plain packet for next state.
  40687. */
  40688. ExpectIntEQ(err, SSL_ERROR_WANT_READ);
  40689. SSL_free(ssl);
  40690. SSL_CTX_free(ctx);
  40691. #endif
  40692. return EXPECT_RESULT();
  40693. }
  40694. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40695. defined(HAVE_HTTP_CLIENT)
  40696. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  40697. {
  40698. BIO* clientBio;
  40699. SSL* sslClient;
  40700. SSL_CTX* ctx;
  40701. char connectAddr[20]; /* IP + port */;
  40702. (void)args;
  40703. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  40704. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  40705. AssertIntEQ(BIO_do_connect(clientBio), 1);
  40706. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  40707. AssertNotNull(sslClient = SSL_new(ctx));
  40708. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  40709. SSL_set_bio(sslClient, clientBio, clientBio);
  40710. AssertIntEQ(SSL_connect(sslClient), 1);
  40711. SSL_free(sslClient);
  40712. SSL_CTX_free(ctx);
  40713. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  40714. wc_ecc_fp_free(); /* free per thread cache */
  40715. #endif
  40716. WOLFSSL_RETURN_FROM_THREAD(0);
  40717. }
  40718. #endif
  40719. static int test_wolfSSL_BIO_accept(void)
  40720. {
  40721. EXPECT_DECLS;
  40722. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40723. defined(HAVE_HTTP_CLIENT)
  40724. BIO* serverBindBio = NULL;
  40725. BIO* serverAcceptBio = NULL;
  40726. SSL* sslServer = NULL;
  40727. SSL_CTX* ctx = NULL;
  40728. func_args args;
  40729. THREAD_TYPE thread;
  40730. char port[10]; /* 10 bytes should be enough to store the string
  40731. * representation of the port */
  40732. ExpectIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  40733. ExpectNotNull(serverBindBio = BIO_new_accept(port));
  40734. /* First BIO_do_accept binds the port */
  40735. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  40736. XMEMSET(&args, 0, sizeof(func_args));
  40737. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  40738. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  40739. /* Let's plug it into SSL to test */
  40740. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  40741. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  40742. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  40743. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  40744. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  40745. ExpectNotNull(sslServer = SSL_new(ctx));
  40746. ExpectNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  40747. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  40748. ExpectIntEQ(SSL_accept(sslServer), 1);
  40749. join_thread(thread);
  40750. BIO_free(serverBindBio);
  40751. SSL_free(sslServer);
  40752. SSL_CTX_free(ctx);
  40753. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  40754. wc_ecc_fp_free(); /* free per thread cache */
  40755. #endif
  40756. #endif
  40757. return EXPECT_RESULT();
  40758. }
  40759. static int test_wolfSSL_BIO_write(void)
  40760. {
  40761. EXPECT_DECLS;
  40762. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  40763. BIO* bio = NULL;
  40764. BIO* bio64 = NULL;
  40765. BIO* bio_mem = NULL;
  40766. BIO* ptr = NULL;
  40767. int sz;
  40768. char msg[] = "conversion test";
  40769. char out[40];
  40770. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  40771. void* bufPtr = NULL;
  40772. BUF_MEM* buf = NULL;
  40773. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  40774. ExpectNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  40775. if (EXPECT_FAIL()) {
  40776. BIO_free(bio64);
  40777. }
  40778. /* now should convert to base64 then write to memory */
  40779. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40780. BIO_flush(bio);
  40781. /* test BIO chain */
  40782. ExpectIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  40783. ExpectNotNull(buf);
  40784. ExpectIntEQ(buf->length, 25);
  40785. ExpectIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  40786. ExpectPtrEq(buf->data, bufPtr);
  40787. ExpectNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  40788. sz = sizeof(out);
  40789. XMEMSET(out, 0, sz);
  40790. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  40791. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  40792. /* write then read should return the same message */
  40793. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40794. sz = sizeof(out);
  40795. XMEMSET(out, 0, sz);
  40796. ExpectIntEQ(BIO_read(bio, out, sz), 16);
  40797. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  40798. /* now try encoding with no line ending */
  40799. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  40800. #ifdef HAVE_EX_DATA
  40801. BIO_set_ex_data(bio64, 0, (void*) "data");
  40802. ExpectIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  40803. #endif
  40804. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40805. BIO_flush(bio);
  40806. sz = sizeof(out);
  40807. XMEMSET(out, 0, sz);
  40808. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  40809. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  40810. BIO_free_all(bio); /* frees bio64 also */
  40811. bio = NULL;
  40812. /* test with more than one bio64 in list */
  40813. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  40814. ExpectNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  40815. if (EXPECT_FAIL()) {
  40816. BIO_free(bio64);
  40817. bio64 = NULL;
  40818. }
  40819. ExpectNotNull(bio_mem = BIO_new(BIO_s_mem()));
  40820. ExpectNotNull(BIO_push(bio64, bio_mem));
  40821. if (EXPECT_FAIL()) {
  40822. BIO_free(bio_mem);
  40823. }
  40824. /* now should convert to base64 when stored and then decode with read */
  40825. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  40826. BIO_flush(bio);
  40827. sz = sizeof(out);
  40828. XMEMSET(out, 0, sz);
  40829. ExpectIntEQ((sz = BIO_read(bio, out, sz)), 16);
  40830. ExpectIntEQ(XMEMCMP(out, msg, sz), 0);
  40831. BIO_clear_flags(bio64, ~0);
  40832. BIO_set_retry_read(bio);
  40833. BIO_free_all(bio); /* frees bio64s also */
  40834. bio = NULL;
  40835. ExpectNotNull(bio = BIO_new_mem_buf(out, 0));
  40836. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40837. BIO_free(bio);
  40838. #endif
  40839. return EXPECT_RESULT();
  40840. }
  40841. static int test_wolfSSL_BIO_printf(void)
  40842. {
  40843. EXPECT_DECLS;
  40844. #if defined(OPENSSL_ALL)
  40845. BIO* bio = NULL;
  40846. int sz = 7;
  40847. char msg[] = "TLS 1.3 for the world";
  40848. char out[60];
  40849. char expected[] = "TLS 1.3 for the world : sz = 7";
  40850. XMEMSET(out, 0, sizeof(out));
  40851. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40852. ExpectIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  40853. ExpectIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  40854. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  40855. ExpectIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  40856. BIO_free(bio);
  40857. #endif
  40858. return EXPECT_RESULT();
  40859. }
  40860. static int test_wolfSSL_BIO_f_md(void)
  40861. {
  40862. EXPECT_DECLS;
  40863. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  40864. BIO* bio = NULL;
  40865. BIO* mem = NULL;
  40866. char msg[] = "message to hash";
  40867. char out[60];
  40868. EVP_MD_CTX* ctx = NULL;
  40869. const unsigned char testKey[] =
  40870. {
  40871. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  40872. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  40873. 0x0b, 0x0b, 0x0b, 0x0b
  40874. };
  40875. const char testData[] = "Hi There";
  40876. const unsigned char testResult[] =
  40877. {
  40878. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  40879. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  40880. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  40881. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  40882. };
  40883. const unsigned char expectedHash[] =
  40884. {
  40885. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  40886. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  40887. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  40888. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  40889. };
  40890. const unsigned char emptyHash[] =
  40891. {
  40892. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  40893. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  40894. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  40895. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  40896. };
  40897. unsigned char check[sizeof(testResult) + 1];
  40898. size_t checkSz = -1;
  40899. EVP_PKEY* key = NULL;
  40900. XMEMSET(out, 0, sizeof(out));
  40901. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  40902. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  40903. ExpectIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  40904. ExpectIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  40905. /* should not be able to write/read yet since just digest wrapper and no
  40906. * data is passing through the bio */
  40907. ExpectIntEQ(BIO_write(bio, msg, 0), 0);
  40908. ExpectIntEQ(BIO_pending(bio), 0);
  40909. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  40910. ExpectIntEQ(BIO_gets(bio, out, 3), 0);
  40911. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  40912. ExpectIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  40913. BIO_reset(bio);
  40914. /* append BIO mem to bio in order to read/write */
  40915. ExpectNotNull(bio = BIO_push(bio, mem));
  40916. XMEMSET(out, 0, sizeof(out));
  40917. ExpectIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  40918. ExpectIntEQ(BIO_pending(bio), 16);
  40919. /* this just reads the message and does not hash it (gets calls final) */
  40920. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  40921. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  40922. /* create a message digest using BIO */
  40923. XMEMSET(out, 0, sizeof(out));
  40924. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  40925. ExpectIntEQ(BIO_pending(mem), 16);
  40926. ExpectIntEQ(BIO_pending(bio), 16);
  40927. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  40928. ExpectIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  40929. BIO_free(bio);
  40930. bio = NULL;
  40931. BIO_free(mem);
  40932. mem = NULL;
  40933. /* test with HMAC */
  40934. XMEMSET(out, 0, sizeof(out));
  40935. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  40936. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  40937. BIO_get_md_ctx(bio, &ctx);
  40938. ExpectNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, testKey,
  40939. (int)sizeof(testKey)));
  40940. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  40941. ExpectNotNull(bio = BIO_push(bio, mem));
  40942. BIO_write(bio, testData, (int)strlen(testData));
  40943. ExpectIntEQ(EVP_DigestSignFinal(ctx, NULL, &checkSz), 1);
  40944. ExpectIntEQ(EVP_DigestSignFinal(ctx, check, &checkSz), 1);
  40945. ExpectIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  40946. EVP_PKEY_free(key);
  40947. BIO_free(bio);
  40948. BIO_free(mem);
  40949. #endif
  40950. return EXPECT_RESULT();
  40951. }
  40952. static int test_wolfSSL_BIO_up_ref(void)
  40953. {
  40954. EXPECT_DECLS;
  40955. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  40956. BIO* bio = NULL;
  40957. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  40958. ExpectIntEQ(BIO_up_ref(NULL), 0);
  40959. ExpectIntEQ(BIO_up_ref(bio), 1);
  40960. BIO_free(bio);
  40961. ExpectIntEQ(BIO_up_ref(bio), 1);
  40962. BIO_free(bio);
  40963. BIO_free(bio);
  40964. #endif
  40965. return EXPECT_RESULT();
  40966. }
  40967. static int test_wolfSSL_BIO_reset(void)
  40968. {
  40969. EXPECT_DECLS;
  40970. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  40971. BIO* bio = NULL;
  40972. byte buf[16];
  40973. ExpectNotNull(bio = BIO_new_mem_buf("secure your data",
  40974. (word32)XSTRLEN("secure your data")));
  40975. ExpectIntEQ(BIO_read(bio, buf, 6), 6);
  40976. ExpectIntEQ(XMEMCMP(buf, "secure", 6), 0);
  40977. XMEMSET(buf, 0, 16);
  40978. ExpectIntEQ(BIO_read(bio, buf, 16), 10);
  40979. ExpectIntEQ(XMEMCMP(buf, " your data", 10), 0);
  40980. /* You cannot write to MEM BIO with read-only mode. */
  40981. ExpectIntEQ(BIO_write(bio, "WriteToReadonly", 15), 0);
  40982. ExpectIntEQ(BIO_read(bio, buf, 16), -1);
  40983. XMEMSET(buf, 0, 16);
  40984. ExpectIntEQ(BIO_reset(bio), 0);
  40985. ExpectIntEQ(BIO_read(bio, buf, 16), 16);
  40986. ExpectIntEQ(XMEMCMP(buf, "secure your data", 16), 0);
  40987. BIO_free(bio);
  40988. #endif
  40989. return EXPECT_RESULT();
  40990. }
  40991. #endif /* !NO_BIO */
  40992. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  40993. /* test that the callback arg is correct */
  40994. static int certCbArg = 0;
  40995. static int certCb(WOLFSSL* ssl, void* arg)
  40996. {
  40997. if (ssl == NULL || arg != &certCbArg)
  40998. return 0;
  40999. if (wolfSSL_is_server(ssl)) {
  41000. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  41001. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41002. return 0;
  41003. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  41004. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41005. return 0;
  41006. }
  41007. else {
  41008. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  41009. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41010. return 0;
  41011. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  41012. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41013. return 0;
  41014. }
  41015. return 1;
  41016. }
  41017. static int certSetupCb(WOLFSSL_CTX* ctx)
  41018. {
  41019. SSL_CTX_set_cert_cb(ctx, certCb, &certCbArg);
  41020. return TEST_SUCCESS;
  41021. }
  41022. /**
  41023. * This is only done because test_wolfSSL_client_server_nofail_memio has no way
  41024. * to stop certificate and key loading
  41025. */
  41026. static int certClearCb(WOLFSSL* ssl)
  41027. {
  41028. /* Clear the loaded certs to force the callbacks to set them up */
  41029. SSL_certs_clear(ssl);
  41030. return TEST_SUCCESS;
  41031. }
  41032. #endif
  41033. static int test_wolfSSL_cert_cb(void)
  41034. {
  41035. EXPECT_DECLS;
  41036. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  41037. test_ssl_cbf func_cb_client;
  41038. test_ssl_cbf func_cb_server;
  41039. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  41040. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  41041. func_cb_client.ctx_ready = certSetupCb;
  41042. func_cb_client.ssl_ready = certClearCb;
  41043. func_cb_server.ctx_ready = certSetupCb;
  41044. func_cb_server.ssl_ready = certClearCb;
  41045. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  41046. &func_cb_server, NULL), TEST_SUCCESS);
  41047. #endif
  41048. return EXPECT_RESULT();
  41049. }
  41050. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  41051. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_cipher = NULL;
  41052. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs = NULL;
  41053. static int test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready(WOLFSSL_CTX* ctx)
  41054. {
  41055. EXPECT_DECLS;
  41056. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx,
  41057. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher), WOLFSSL_SUCCESS);
  41058. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  41059. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs), WOLFSSL_SUCCESS);
  41060. return EXPECT_RESULT();
  41061. }
  41062. static int test_wolfSSL_cert_cb_dyn_ciphers_certCB(WOLFSSL* ssl, void* arg)
  41063. {
  41064. const byte* suites = NULL;
  41065. word16 suiteSz = 0;
  41066. const byte* hashSigAlgo = NULL;
  41067. word16 hashSigAlgoSz = 0;
  41068. word16 idx = 0;
  41069. int haveRSA = 0;
  41070. int haveECC = 0;
  41071. (void)arg;
  41072. if (wolfSSL_get_client_suites_sigalgs(ssl, &suites, &suiteSz, &hashSigAlgo,
  41073. &hashSigAlgoSz) != WOLFSSL_SUCCESS)
  41074. return 0;
  41075. if (suites == NULL || suiteSz == 0 || hashSigAlgo == NULL ||
  41076. hashSigAlgoSz == 0)
  41077. return 0;
  41078. for (idx = 0; idx < suiteSz; idx += 2) {
  41079. WOLFSSL_CIPHERSUITE_INFO info =
  41080. wolfSSL_get_ciphersuite_info(suites[idx], suites[idx+1]);
  41081. if (info.rsaAuth)
  41082. haveRSA = 1;
  41083. else if (info.eccAuth)
  41084. haveECC = 1;
  41085. }
  41086. if (hashSigAlgoSz > 0) {
  41087. /* sigalgs extension takes precedence over ciphersuites */
  41088. haveRSA = 0;
  41089. haveECC = 0;
  41090. }
  41091. for (idx = 0; idx < hashSigAlgoSz; idx += 2) {
  41092. int hashAlgo = 0;
  41093. int sigAlgo = 0;
  41094. if (wolfSSL_get_sigalg_info(hashSigAlgo[idx+0], hashSigAlgo[idx+1],
  41095. &hashAlgo, &sigAlgo) != 0)
  41096. return 0;
  41097. if (sigAlgo == RSAk || sigAlgo == RSAPSSk)
  41098. haveRSA = 1;
  41099. else if (sigAlgo == ECDSAk)
  41100. haveECC = 1;
  41101. }
  41102. if (haveRSA) {
  41103. if (wolfSSL_use_certificate_file(ssl, svrCertFile, WOLFSSL_FILETYPE_PEM)
  41104. != WOLFSSL_SUCCESS)
  41105. return 0;
  41106. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile, WOLFSSL_FILETYPE_PEM)
  41107. != WOLFSSL_SUCCESS)
  41108. return 0;
  41109. }
  41110. else if (haveECC) {
  41111. if (wolfSSL_use_certificate_file(ssl, eccCertFile, WOLFSSL_FILETYPE_PEM)
  41112. != WOLFSSL_SUCCESS)
  41113. return 0;
  41114. if (wolfSSL_use_PrivateKey_file(ssl, eccKeyFile, WOLFSSL_FILETYPE_PEM)
  41115. != WOLFSSL_SUCCESS)
  41116. return 0;
  41117. }
  41118. return 1;
  41119. }
  41120. static int test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready(WOLFSSL_CTX* ctx)
  41121. {
  41122. SSL_CTX_set_cert_cb(ctx, test_wolfSSL_cert_cb_dyn_ciphers_certCB, NULL);
  41123. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, NULL);
  41124. return TEST_SUCCESS;
  41125. }
  41126. #endif
  41127. /* Testing dynamic ciphers offered by client */
  41128. static int test_wolfSSL_cert_cb_dyn_ciphers(void)
  41129. {
  41130. EXPECT_DECLS;
  41131. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  41132. test_ssl_cbf func_cb_client;
  41133. test_ssl_cbf func_cb_server;
  41134. struct {
  41135. method_provider client_meth;
  41136. const char* client_ciphers;
  41137. const char* client_sigalgs;
  41138. const char* client_ca;
  41139. method_provider server_meth;
  41140. } test_params[] = {
  41141. #if !defined(NO_SHA256) && defined(HAVE_AESGCM)
  41142. #ifdef WOLFSSL_TLS13
  41143. #if !defined(NO_RSA) && defined(WC_RSA_PSS)
  41144. {wolfTLSv1_3_client_method,
  41145. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  41146. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_3_server_method},
  41147. #endif
  41148. #ifdef HAVE_ECC
  41149. {wolfTLSv1_3_client_method,
  41150. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  41151. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_3_server_method},
  41152. #endif
  41153. #endif
  41154. #ifndef WOLFSSL_NO_TLS12
  41155. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_DH)
  41156. {wolfTLSv1_2_client_method,
  41157. "DHE-RSA-AES128-GCM-SHA256",
  41158. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_2_server_method},
  41159. #endif
  41160. #ifdef HAVE_ECC
  41161. {wolfTLSv1_2_client_method,
  41162. "ECDHE-ECDSA-AES128-GCM-SHA256",
  41163. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_2_server_method},
  41164. #endif
  41165. #endif
  41166. #endif
  41167. };
  41168. size_t i;
  41169. size_t testCount = sizeof(test_params)/sizeof(*test_params);
  41170. if (testCount > 0) {
  41171. for (i = 0; i < testCount; i++) {
  41172. printf("\tTesting %s ciphers with %s sigalgs\n",
  41173. test_params[i].client_ciphers,
  41174. test_params[i].client_sigalgs);
  41175. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  41176. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  41177. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher =
  41178. test_params[i].client_ciphers;
  41179. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs =
  41180. test_params[i].client_sigalgs;
  41181. func_cb_client.method = test_params[i].client_meth;
  41182. func_cb_client.caPemFile = test_params[i].client_ca;
  41183. func_cb_client.ctx_ready =
  41184. test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready;
  41185. func_cb_server.ctx_ready =
  41186. test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready;
  41187. func_cb_server.ssl_ready = certClearCb; /* Reuse from prev test */
  41188. func_cb_server.method = test_params[i].server_meth;
  41189. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  41190. &func_cb_server, NULL), TEST_SUCCESS);
  41191. }
  41192. }
  41193. #endif
  41194. return EXPECT_RESULT();
  41195. }
  41196. static int test_wolfSSL_ciphersuite_auth(void)
  41197. {
  41198. EXPECT_DECLS;
  41199. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  41200. WOLFSSL_CIPHERSUITE_INFO info;
  41201. (void)info;
  41202. #ifndef WOLFSSL_NO_TLS12
  41203. #ifdef HAVE_CHACHA
  41204. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  41205. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256);
  41206. ExpectIntEQ(info.rsaAuth, 1);
  41207. ExpectIntEQ(info.eccAuth, 0);
  41208. ExpectIntEQ(info.eccStatic, 0);
  41209. ExpectIntEQ(info.psk, 0);
  41210. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  41211. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256);
  41212. ExpectIntEQ(info.rsaAuth, 0);
  41213. ExpectIntEQ(info.eccAuth, 1);
  41214. ExpectIntEQ(info.eccStatic, 0);
  41215. ExpectIntEQ(info.psk, 0);
  41216. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  41217. TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256);
  41218. ExpectIntEQ(info.rsaAuth, 0);
  41219. ExpectIntEQ(info.eccAuth, 0);
  41220. ExpectIntEQ(info.eccStatic, 0);
  41221. ExpectIntEQ(info.psk, 1);
  41222. #endif
  41223. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  41224. #ifndef NO_RSA
  41225. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  41226. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA);
  41227. ExpectIntEQ(info.rsaAuth, 1);
  41228. ExpectIntEQ(info.eccAuth, 0);
  41229. ExpectIntEQ(info.eccStatic, 0);
  41230. ExpectIntEQ(info.psk, 0);
  41231. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  41232. TLS_ECDH_RSA_WITH_AES_128_CBC_SHA);
  41233. ExpectIntEQ(info.rsaAuth, 1);
  41234. ExpectIntEQ(info.eccAuth, 0);
  41235. ExpectIntEQ(info.eccStatic, 1);
  41236. ExpectIntEQ(info.psk, 0);
  41237. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  41238. TLS_ECDH_RSA_WITH_AES_256_CBC_SHA);
  41239. ExpectIntEQ(info.rsaAuth, 1);
  41240. ExpectIntEQ(info.eccAuth, 0);
  41241. ExpectIntEQ(info.eccStatic, 1);
  41242. ExpectIntEQ(info.psk, 0);
  41243. #endif
  41244. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  41245. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA);
  41246. ExpectIntEQ(info.rsaAuth, 0);
  41247. ExpectIntEQ(info.eccAuth, 1);
  41248. ExpectIntEQ(info.eccStatic, 0);
  41249. ExpectIntEQ(info.psk, 0);
  41250. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  41251. TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA);
  41252. ExpectIntEQ(info.rsaAuth, 0);
  41253. ExpectIntEQ(info.eccAuth, 1);
  41254. ExpectIntEQ(info.eccStatic, 1);
  41255. ExpectIntEQ(info.psk, 0);
  41256. info = wolfSSL_get_ciphersuite_info(ECDHE_PSK_BYTE,
  41257. TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256);
  41258. ExpectIntEQ(info.rsaAuth, 0);
  41259. ExpectIntEQ(info.eccAuth, 0);
  41260. ExpectIntEQ(info.eccStatic, 0);
  41261. ExpectIntEQ(info.psk, 1);
  41262. #endif
  41263. #endif
  41264. #ifdef WOLFSSL_TLS13
  41265. info = wolfSSL_get_ciphersuite_info(TLS13_BYTE,
  41266. TLS_AES_128_GCM_SHA256);
  41267. ExpectIntEQ(info.rsaAuth, 0);
  41268. ExpectIntEQ(info.eccAuth, 0);
  41269. ExpectIntEQ(info.eccStatic, 0);
  41270. ExpectIntEQ(info.psk, 0);
  41271. #endif
  41272. #endif
  41273. return EXPECT_RESULT();
  41274. }
  41275. static int test_wolfSSL_sigalg_info(void)
  41276. {
  41277. EXPECT_DECLS;
  41278. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  41279. byte hashSigAlgo[WOLFSSL_MAX_SIGALGO];
  41280. word16 len = 0;
  41281. word16 idx = 0;
  41282. int allSigAlgs = SIG_ECDSA | SIG_RSA | SIG_SM2 | SIG_FALCON | SIG_DILITHIUM;
  41283. InitSuitesHashSigAlgo_ex2(hashSigAlgo, allSigAlgs, 1, 0xFFFFFFFF, &len);
  41284. for (idx = 0; idx < len; idx += 2) {
  41285. int hashAlgo = 0;
  41286. int sigAlgo = 0;
  41287. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  41288. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  41289. ExpectIntNE(hashAlgo, 0);
  41290. ExpectIntNE(sigAlgo, 0);
  41291. }
  41292. InitSuitesHashSigAlgo_ex2(hashSigAlgo, allSigAlgs | SIG_ANON, 1,
  41293. 0xFFFFFFFF, &len);
  41294. for (idx = 0; idx < len; idx += 2) {
  41295. int hashAlgo = 0;
  41296. int sigAlgo = 0;
  41297. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  41298. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  41299. ExpectIntNE(hashAlgo, 0);
  41300. }
  41301. #endif
  41302. return EXPECT_RESULT();
  41303. }
  41304. static int test_wolfSSL_SESSION(void)
  41305. {
  41306. EXPECT_DECLS;
  41307. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41308. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  41309. !defined(NO_SESSION_CACHE)
  41310. WOLFSSL* ssl = NULL;
  41311. WOLFSSL_CTX* ctx = NULL;
  41312. WOLFSSL_SESSION* sess = NULL;
  41313. WOLFSSL_SESSION* sess_copy = NULL;
  41314. #ifdef OPENSSL_EXTRA
  41315. #ifdef HAVE_EXT_CACHE
  41316. unsigned char* sessDer = NULL;
  41317. unsigned char* ptr = NULL;
  41318. int sz = 0;
  41319. #endif
  41320. const unsigned char context[] = "user app context";
  41321. unsigned int contextSz = (unsigned int)sizeof(context);
  41322. #endif
  41323. int ret = 0, err = 0;
  41324. SOCKET_T sockfd;
  41325. tcp_ready ready;
  41326. func_args server_args;
  41327. THREAD_TYPE serverThread;
  41328. char msg[80];
  41329. const char* sendGET = "GET";
  41330. /* TLS v1.3 requires session tickets */
  41331. /* CHACHA and POLY1305 required for myTicketEncCb */
  41332. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  41333. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  41334. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  41335. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  41336. #else
  41337. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  41338. #endif
  41339. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  41340. WOLFSSL_FILETYPE_PEM));
  41341. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  41342. WOLFSSL_FILETYPE_PEM));
  41343. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  41344. WOLFSSL_SUCCESS);
  41345. #ifdef WOLFSSL_ENCRYPTED_KEYS
  41346. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  41347. #endif
  41348. #ifdef HAVE_SESSION_TICKET
  41349. /* Use session tickets, for ticket tests below */
  41350. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  41351. #endif
  41352. XMEMSET(&server_args, 0, sizeof(func_args));
  41353. #ifdef WOLFSSL_TIRTOS
  41354. fdOpenSession(Task_self());
  41355. #endif
  41356. StartTCP();
  41357. InitTcpReady(&ready);
  41358. #if defined(USE_WINDOWS_API)
  41359. /* use RNG to get random port if using windows */
  41360. ready.port = GetRandomPort();
  41361. #endif
  41362. server_args.signal = &ready;
  41363. start_thread(test_server_nofail, &server_args, &serverThread);
  41364. wait_tcp_ready(&server_args);
  41365. /* client connection */
  41366. ExpectNotNull(ssl = wolfSSL_new(ctx));
  41367. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  41368. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  41369. #ifdef WOLFSSL_ASYNC_CRYPT
  41370. err = 0; /* Reset error */
  41371. #endif
  41372. do {
  41373. #ifdef WOLFSSL_ASYNC_CRYPT
  41374. if (err == WC_PENDING_E) {
  41375. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  41376. if (ret < 0) { break; } else if (ret == 0) { continue; }
  41377. }
  41378. #endif
  41379. ret = wolfSSL_connect(ssl);
  41380. err = wolfSSL_get_error(ssl, 0);
  41381. } while (err == WC_PENDING_E);
  41382. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  41383. #ifdef WOLFSSL_ASYNC_CRYPT
  41384. err = 0; /* Reset error */
  41385. #endif
  41386. do {
  41387. #ifdef WOLFSSL_ASYNC_CRYPT
  41388. if (err == WC_PENDING_E) {
  41389. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  41390. if (ret < 0) { break; } else if (ret == 0) { continue; }
  41391. }
  41392. #endif
  41393. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  41394. err = wolfSSL_get_error(ssl, 0);
  41395. } while (err == WC_PENDING_E);
  41396. ExpectIntEQ(ret, (int)XSTRLEN(sendGET));
  41397. #ifdef WOLFSSL_ASYNC_CRYPT
  41398. err = 0; /* Reset error */
  41399. #endif
  41400. do {
  41401. #ifdef WOLFSSL_ASYNC_CRYPT
  41402. if (err == WC_PENDING_E) {
  41403. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  41404. if (ret < 0) { break; } else if (ret == 0) { continue; }
  41405. }
  41406. #endif
  41407. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  41408. err = wolfSSL_get_error(ssl, 0);
  41409. } while (err == WC_PENDING_E);
  41410. ExpectIntEQ(ret, 23);
  41411. ExpectPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  41412. ExpectPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  41413. #ifdef HAVE_EXT_CACHE
  41414. ExpectPtrEq(sess, sess_copy); /* they should be the same pointer but without
  41415. * HAVE_EXT_CACHE we get new objects each time */
  41416. #endif
  41417. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  41418. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  41419. sess = wolfSSL_get_session(ssl);
  41420. #ifdef OPENSSL_EXTRA
  41421. ExpectIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  41422. ExpectIntEQ(SSL_SESSION_is_resumable(sess), 1);
  41423. ExpectIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  41424. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  41425. #ifdef HAVE_SESSION_TICKET
  41426. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  41427. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  41428. SESSION_TICKET_HINT_DEFAULT);
  41429. #else
  41430. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  41431. #endif
  41432. #else
  41433. (void)sess;
  41434. #endif /* OPENSSL_EXTRA */
  41435. /* Retain copy of the session for later testing */
  41436. ExpectNotNull(sess = wolfSSL_get1_session(ssl));
  41437. wolfSSL_shutdown(ssl);
  41438. wolfSSL_free(ssl); ssl = NULL;
  41439. CloseSocket(sockfd);
  41440. join_thread(serverThread);
  41441. FreeTcpReady(&ready);
  41442. #ifdef WOLFSSL_TIRTOS
  41443. fdOpenSession(Task_self());
  41444. #endif
  41445. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  41446. {
  41447. X509 *x509 = NULL;
  41448. char buf[30];
  41449. int bufSz = 0;
  41450. ExpectNotNull(x509 = SSL_SESSION_get0_peer(sess));
  41451. ExpectIntGT((bufSz = X509_NAME_get_text_by_NID(
  41452. X509_get_subject_name(x509), NID_organizationalUnitName, buf,
  41453. sizeof(buf))), 0);
  41454. ExpectIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  41455. if (bufSz == 7) {
  41456. ExpectIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  41457. }
  41458. if (bufSz == 16) {
  41459. ExpectIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  41460. }
  41461. }
  41462. #endif
  41463. #ifdef HAVE_EXT_CACHE
  41464. ExpectNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  41465. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  41466. sess_copy = NULL;
  41467. #endif
  41468. #if defined(OPENSSL_EXTRA) && defined(HAVE_EXT_CACHE)
  41469. /* get session from DER and update the timeout */
  41470. ExpectIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  41471. ExpectIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  41472. wolfSSL_SESSION_free(sess); sess = NULL;
  41473. sess = NULL;
  41474. ptr = sessDer;
  41475. ExpectNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  41476. ExpectNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  41477. (const unsigned char**)&ptr, sz));
  41478. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  41479. sessDer = NULL;
  41480. ExpectIntGT(wolfSSL_SESSION_get_time(sess), 0);
  41481. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  41482. #endif
  41483. /* successful set session test */
  41484. ExpectNotNull(ssl = wolfSSL_new(ctx));
  41485. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  41486. #ifdef HAVE_SESSION_TICKET
  41487. /* Test set/get session ticket */
  41488. {
  41489. const char* ticket = "This is a session ticket";
  41490. char buf[64] = {0};
  41491. word32 bufSz = (word32)sizeof(buf);
  41492. ExpectIntEQ(SSL_SUCCESS,
  41493. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  41494. (word32)XSTRLEN(ticket)));
  41495. ExpectIntEQ(SSL_SUCCESS,
  41496. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  41497. ExpectStrEQ(ticket, buf);
  41498. }
  41499. #endif
  41500. #ifdef OPENSSL_EXTRA
  41501. /* session timeout case */
  41502. /* make the session to be expired */
  41503. ExpectIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  41504. XSLEEP_MS(1200);
  41505. /* SSL_set_session should reject specified session but return success
  41506. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  41507. */
  41508. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  41509. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  41510. #else
  41511. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  41512. #endif
  41513. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  41514. #ifdef WOLFSSL_SESSION_ID_CTX
  41515. /* fail case with miss match session context IDs (use compatibility API) */
  41516. ExpectIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  41517. SSL_SUCCESS);
  41518. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  41519. wolfSSL_free(ssl); ssl = NULL;
  41520. ExpectIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  41521. SSL_FAILURE);
  41522. ExpectIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  41523. SSL_SUCCESS);
  41524. ExpectNotNull(ssl = wolfSSL_new(ctx));
  41525. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  41526. #endif
  41527. #endif /* OPENSSL_EXTRA */
  41528. wolfSSL_free(ssl);
  41529. wolfSSL_SESSION_free(sess);
  41530. wolfSSL_CTX_free(ctx);
  41531. #endif
  41532. return EXPECT_RESULT();
  41533. }
  41534. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41535. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  41536. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  41537. !defined(WOLFSSL_NO_TLS12)
  41538. static WOLFSSL_SESSION* test_wolfSSL_SESSION_expire_sess = NULL;
  41539. static void test_wolfSSL_SESSION_expire_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  41540. {
  41541. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  41542. /* returns previous timeout value */
  41543. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), 500);
  41544. #else
  41545. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), WOLFSSL_SUCCESS);
  41546. #endif
  41547. }
  41548. /* set the session to timeout in a second */
  41549. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready(WOLFSSL* ssl)
  41550. {
  41551. AssertIntEQ(wolfSSL_set_timeout(ssl, 2), 1);
  41552. }
  41553. /* store the client side session from the first successful connection */
  41554. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result(WOLFSSL* ssl)
  41555. {
  41556. AssertPtrNE((test_wolfSSL_SESSION_expire_sess = wolfSSL_get1_session(ssl)),
  41557. NULL); /* ref count 1 */
  41558. }
  41559. /* wait till session is expired then set it in the WOLFSSL struct for use */
  41560. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait(WOLFSSL* ssl)
  41561. {
  41562. AssertIntEQ(wolfSSL_set_timeout(ssl, 1), 1);
  41563. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  41564. WOLFSSL_SUCCESS);
  41565. XSLEEP_MS(2000); /* wait 2 seconds for session to expire */
  41566. }
  41567. /* set expired session in the WOLFSSL struct for use */
  41568. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set(WOLFSSL* ssl)
  41569. {
  41570. XSLEEP_MS(1200); /* wait a second for session to expire */
  41571. /* set the expired session, call to set session fails but continuing on
  41572. after failure should be handled here */
  41573. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL)
  41574. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  41575. WOLFSSL_SUCCESS);
  41576. #else
  41577. AssertIntNE(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  41578. WOLFSSL_SUCCESS);
  41579. #endif
  41580. }
  41581. /* check that the expired session was not reused */
  41582. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse(WOLFSSL* ssl)
  41583. {
  41584. /* since the session has expired it should not have been reused */
  41585. AssertIntEQ(wolfSSL_session_reused(ssl), 0);
  41586. }
  41587. #endif
  41588. static int test_wolfSSL_SESSION_expire_downgrade(void)
  41589. {
  41590. EXPECT_DECLS;
  41591. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41592. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  41593. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  41594. !defined(WOLFSSL_NO_TLS12)
  41595. WOLFSSL_CTX* ctx = NULL;
  41596. callback_functions server_cbf, client_cbf;
  41597. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  41598. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  41599. /* force server side to use TLS 1.2 */
  41600. server_cbf.ctx = ctx;
  41601. server_cbf.method = wolfTLSv1_2_server_method;
  41602. client_cbf.method = wolfSSLv23_client_method;
  41603. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  41604. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready;
  41605. client_cbf.on_result = test_wolfSSL_SESSION_expire_downgrade_ssl_result;
  41606. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  41607. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  41608. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  41609. /* set the previously created session and wait till expired */
  41610. server_cbf.ctx = ctx;
  41611. client_cbf.method = wolfSSLv23_client_method;
  41612. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  41613. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait;
  41614. client_cbf.on_result =
  41615. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  41616. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  41617. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  41618. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  41619. /* set the previously created expired session */
  41620. server_cbf.ctx = ctx;
  41621. client_cbf.method = wolfSSLv23_client_method;
  41622. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  41623. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set;
  41624. client_cbf.on_result =
  41625. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  41626. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  41627. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  41628. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  41629. wolfSSL_SESSION_free(test_wolfSSL_SESSION_expire_sess);
  41630. wolfSSL_CTX_free(ctx);
  41631. #endif
  41632. return EXPECT_RESULT();
  41633. }
  41634. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  41635. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  41636. static int clientSessRemCountMalloc = 0;
  41637. static int serverSessRemCountMalloc = 0;
  41638. static int clientSessRemCountFree = 0;
  41639. static int serverSessRemCountFree = 0;
  41640. static WOLFSSL_CTX* serverSessCtx = NULL;
  41641. static WOLFSSL_SESSION* serverSess = NULL;
  41642. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  41643. !defined(NO_SESSION_CACHE_REF)
  41644. static WOLFSSL_CTX* clientSessCtx = NULL;
  41645. static WOLFSSL_SESSION* clientSess = NULL;
  41646. #endif
  41647. static int serverSessRemIdx = 3;
  41648. static int sessRemCtx_Server = WOLFSSL_SERVER_END;
  41649. static int sessRemCtx_Client = WOLFSSL_CLIENT_END;
  41650. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  41651. {
  41652. int* side;
  41653. (void)ctx;
  41654. side = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  41655. if (side != NULL) {
  41656. if (*side == WOLFSSL_CLIENT_END)
  41657. clientSessRemCountFree++;
  41658. else
  41659. serverSessRemCountFree++;
  41660. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  41661. }
  41662. }
  41663. static int SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  41664. {
  41665. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  41666. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  41667. !defined(NO_SESSION_CACHE_REF)
  41668. {
  41669. EXPECT_DECLS;
  41670. /* Allow downgrade, set min version, and disable TLS 1.3.
  41671. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  41672. * reference to the session cache. But with WOLFSSL_TLS13 and without
  41673. * HAVE_SESSION_TICKET we won't have a session ID to be able to place
  41674. * the session in the cache. In this case we need to downgrade to
  41675. * previous versions to just use the legacy session ID field. */
  41676. ExpectIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  41677. SSL_SUCCESS);
  41678. ExpectIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  41679. SSL_SUCCESS);
  41680. return EXPECT_RESULT();
  41681. }
  41682. #else
  41683. return TEST_SUCCESS;
  41684. #endif
  41685. }
  41686. static int SessRemSslSetupCb(WOLFSSL* ssl)
  41687. {
  41688. EXPECT_DECLS;
  41689. int* side;
  41690. if (SSL_is_server(ssl)) {
  41691. side = &sessRemCtx_Server;
  41692. serverSessRemCountMalloc++;
  41693. ExpectNotNull(serverSess = SSL_get1_session(ssl));
  41694. ExpectIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  41695. SSL_SUCCESS);
  41696. }
  41697. else {
  41698. side = &sessRemCtx_Client;
  41699. clientSessRemCountMalloc++;
  41700. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  41701. !defined(NO_SESSION_CACHE_REF)
  41702. ExpectNotNull(clientSess = SSL_get1_session(ssl));
  41703. ExpectIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  41704. SSL_SUCCESS);
  41705. #endif
  41706. }
  41707. ExpectIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl),
  41708. serverSessRemIdx, side), SSL_SUCCESS);
  41709. return EXPECT_RESULT();
  41710. }
  41711. #endif
  41712. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  41713. {
  41714. EXPECT_DECLS;
  41715. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  41716. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  41717. /* Check that the remove callback gets called for external data in a
  41718. * session object */
  41719. test_ssl_cbf func_cb;
  41720. XMEMSET(&func_cb, 0, sizeof(func_cb));
  41721. func_cb.ctx_ready = SessRemCtxSetupCb;
  41722. func_cb.on_result = SessRemSslSetupCb;
  41723. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb, &func_cb,
  41724. NULL), TEST_SUCCESS);
  41725. /* Both should have been allocated */
  41726. ExpectIntEQ(clientSessRemCountMalloc, 1);
  41727. ExpectIntEQ(serverSessRemCountMalloc, 1);
  41728. /* This should not be called yet. Session wasn't evicted from cache yet. */
  41729. ExpectIntEQ(clientSessRemCountFree, 0);
  41730. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  41731. !defined(NO_SESSION_CACHE_REF)
  41732. /* Force a cache lookup */
  41733. ExpectNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  41734. /* Force a cache update */
  41735. ExpectNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  41736. /* This should set the timeout to 0 and call the remove callback from within
  41737. * the session cache. */
  41738. ExpectIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  41739. ExpectNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  41740. ExpectIntEQ(clientSessRemCountFree, 1);
  41741. #endif
  41742. /* Server session is in the cache so ex_data isn't free'd with the SSL
  41743. * object */
  41744. ExpectIntEQ(serverSessRemCountFree, 0);
  41745. /* Force a cache lookup */
  41746. ExpectNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  41747. /* Force a cache update */
  41748. ExpectNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  41749. /* This should set the timeout to 0 and call the remove callback from within
  41750. * the session cache. */
  41751. ExpectIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  41752. ExpectNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  41753. ExpectIntEQ(serverSessRemCountFree, 1);
  41754. /* Need to free the references that we kept */
  41755. SSL_CTX_free(serverSessCtx);
  41756. SSL_SESSION_free(serverSess);
  41757. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  41758. !defined(NO_SESSION_CACHE_REF)
  41759. SSL_CTX_free(clientSessCtx);
  41760. SSL_SESSION_free(clientSess);
  41761. #endif
  41762. #endif
  41763. return EXPECT_RESULT();
  41764. }
  41765. static int test_wolfSSL_ticket_keys(void)
  41766. {
  41767. EXPECT_DECLS;
  41768. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  41769. !defined(NO_WOLFSSL_SERVER)
  41770. WOLFSSL_CTX* ctx = NULL;
  41771. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  41772. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  41773. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  41774. WOLFSSL_FAILURE);
  41775. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  41776. WOLFSSL_FAILURE);
  41777. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  41778. WOLFSSL_FAILURE);
  41779. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  41780. WOLFSSL_FAILURE);
  41781. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  41782. WOLFSSL_FAILURE);
  41783. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  41784. WOLFSSL_FAILURE);
  41785. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  41786. WOLFSSL_FAILURE);
  41787. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  41788. WOLFSSL_FAILURE);
  41789. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  41790. WOLFSSL_FAILURE);
  41791. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  41792. WOLFSSL_FAILURE);
  41793. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  41794. WOLFSSL_FAILURE);
  41795. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  41796. WOLFSSL_FAILURE);
  41797. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  41798. WOLFSSL_FAILURE);
  41799. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  41800. WOLFSSL_FAILURE);
  41801. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  41802. WOLFSSL_SUCCESS);
  41803. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  41804. WOLFSSL_SUCCESS);
  41805. wolfSSL_CTX_free(ctx);
  41806. #endif
  41807. return EXPECT_RESULT();
  41808. }
  41809. #ifndef NO_BIO
  41810. static int test_wolfSSL_d2i_PUBKEY(void)
  41811. {
  41812. EXPECT_DECLS;
  41813. #if defined(OPENSSL_EXTRA)
  41814. BIO* bio = NULL;
  41815. EVP_PKEY* pkey = NULL;
  41816. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  41817. ExpectNull(d2i_PUBKEY_bio(NULL, NULL));
  41818. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  41819. /* RSA PUBKEY test */
  41820. ExpectIntGT(BIO_write(bio, client_keypub_der_2048,
  41821. sizeof_client_keypub_der_2048), 0);
  41822. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  41823. EVP_PKEY_free(pkey);
  41824. pkey = NULL;
  41825. #endif
  41826. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  41827. /* ECC PUBKEY test */
  41828. ExpectIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  41829. sizeof_ecc_clikeypub_der_256), 0);
  41830. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  41831. EVP_PKEY_free(pkey);
  41832. pkey = NULL;
  41833. #endif
  41834. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  41835. /* DSA PUBKEY test */
  41836. ExpectIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  41837. sizeof_dsa_pub_key_der_2048), 0);
  41838. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  41839. EVP_PKEY_free(pkey);
  41840. pkey = NULL;
  41841. #endif
  41842. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  41843. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  41844. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  41845. (HAVE_FIPS_VERSION > 2))
  41846. /* DH PUBKEY test */
  41847. ExpectIntGT(BIO_write(bio, dh_pub_key_der_2048,
  41848. sizeof_dh_pub_key_der_2048), 0);
  41849. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  41850. EVP_PKEY_free(pkey);
  41851. pkey = NULL;
  41852. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  41853. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  41854. BIO_free(bio);
  41855. (void)pkey;
  41856. #endif
  41857. return EXPECT_RESULT();
  41858. }
  41859. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  41860. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  41861. {
  41862. EXPECT_DECLS;
  41863. BIO* bio = NULL;
  41864. EVP_PKEY* pkey = NULL;
  41865. WOLFSSL_CTX* ctx = NULL;
  41866. #if defined(WOLFSSL_KEY_GEN)
  41867. unsigned char buff[4096];
  41868. unsigned char* bufPtr = buff;
  41869. #endif
  41870. /* test creating new EVP_PKEY with bad arg */
  41871. ExpectNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  41872. /* test loading RSA key using BIO */
  41873. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  41874. {
  41875. XFILE file = XBADFILE;
  41876. const char* fname = "./certs/server-key.der";
  41877. size_t sz = 0;
  41878. byte* buf = NULL;
  41879. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  41880. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  41881. ExpectTrue((sz = XFTELL(file)) != 0);
  41882. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  41883. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  41884. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  41885. if (file != XBADFILE) {
  41886. XFCLOSE(file);
  41887. }
  41888. /* Test using BIO new mem and loading DER private key */
  41889. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  41890. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  41891. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  41892. BIO_free(bio);
  41893. bio = NULL;
  41894. EVP_PKEY_free(pkey);
  41895. pkey = NULL;
  41896. }
  41897. #endif
  41898. /* test loading ECC key using BIO */
  41899. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  41900. {
  41901. XFILE file = XBADFILE;
  41902. const char* fname = "./certs/ecc-key.der";
  41903. size_t sz = 0;
  41904. byte* buf = NULL;
  41905. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  41906. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  41907. ExpectTrue((sz = XFTELL(file)) != 0);
  41908. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  41909. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  41910. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  41911. if (file != XBADFILE)
  41912. XFCLOSE(file);
  41913. /* Test using BIO new mem and loading DER private key */
  41914. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  41915. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  41916. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  41917. BIO_free(bio);
  41918. bio = NULL;
  41919. EVP_PKEY_free(pkey);
  41920. pkey = NULL;
  41921. }
  41922. #endif
  41923. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  41924. #ifndef NO_WOLFSSL_SERVER
  41925. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  41926. #else
  41927. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  41928. #endif
  41929. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  41930. {
  41931. RSA* rsa = NULL;
  41932. /* Tests bad parameters */
  41933. ExpectNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  41934. /* RSA not set yet, expecting to fail*/
  41935. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  41936. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  41937. /* set RSA using bio*/
  41938. ExpectIntGT(BIO_write(bio, client_key_der_2048,
  41939. sizeof_client_key_der_2048), 0);
  41940. ExpectNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  41941. ExpectNotNull(rsa);
  41942. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  41943. /* i2d RSAprivate key tests */
  41944. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  41945. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  41946. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  41947. sizeof_client_key_der_2048);
  41948. bufPtr -= sizeof_client_key_der_2048;
  41949. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  41950. sizeof_client_key_der_2048), 0);
  41951. bufPtr = NULL;
  41952. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  41953. sizeof_client_key_der_2048);
  41954. ExpectNotNull(bufPtr);
  41955. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  41956. sizeof_client_key_der_2048), 0);
  41957. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  41958. RSA_free(rsa);
  41959. rsa = RSA_new();
  41960. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  41961. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  41962. RSA_free(rsa);
  41963. }
  41964. #endif /* WOLFSSL_KEY_GEN && !NO_RSA */
  41965. SSL_CTX_free(ctx);
  41966. ctx = NULL;
  41967. BIO_free(bio);
  41968. bio = NULL;
  41969. return EXPECT_RESULT();
  41970. }
  41971. #endif /* OPENSSL_ALL || (WOLFSSL_ASIO && !NO_RSA) */
  41972. #endif /* !NO_BIO */
  41973. static int test_wolfSSL_sk_GENERAL_NAME(void)
  41974. {
  41975. EXPECT_DECLS;
  41976. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41977. !defined(NO_RSA)
  41978. X509* x509 = NULL;
  41979. GENERAL_NAME* gn = NULL;
  41980. unsigned char buf[4096];
  41981. const unsigned char* bufPt = NULL;
  41982. int bytes = 0;
  41983. int i;
  41984. int j;
  41985. XFILE f = XBADFILE;
  41986. STACK_OF(GENERAL_NAME)* sk = NULL;
  41987. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  41988. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  41989. if (f != XBADFILE)
  41990. XFCLOSE(f);
  41991. for (j = 0; j < 2; ++j) {
  41992. bufPt = buf;
  41993. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  41994. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  41995. NID_subject_alt_name, NULL, NULL));
  41996. ExpectIntEQ(sk_GENERAL_NAME_num(sk), 1);
  41997. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  41998. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  41999. if (gn != NULL) {
  42000. switch (gn->type) {
  42001. case GEN_DNS:
  42002. fprintf(stderr, "found type GEN_DNS\n");
  42003. break;
  42004. case GEN_EMAIL:
  42005. fprintf(stderr, "found type GEN_EMAIL\n");
  42006. break;
  42007. case GEN_URI:
  42008. fprintf(stderr, "found type GEN_URI\n");
  42009. break;
  42010. }
  42011. }
  42012. }
  42013. X509_free(x509);
  42014. x509 = NULL;
  42015. if (j == 0) {
  42016. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  42017. }
  42018. else {
  42019. /*
  42020. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  42021. * it was supposed to. This is a regression test for that bug.
  42022. */
  42023. GENERAL_NAMES_free(sk);
  42024. }
  42025. sk = NULL;
  42026. }
  42027. #endif
  42028. return EXPECT_RESULT();
  42029. }
  42030. static int test_wolfSSL_GENERAL_NAME_print(void)
  42031. {
  42032. EXPECT_DECLS;
  42033. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  42034. X509* x509 = NULL;
  42035. GENERAL_NAME* gn = NULL;
  42036. unsigned char buf[4096];
  42037. const unsigned char* bufPt = NULL;
  42038. int bytes;
  42039. XFILE f = XBADFILE;
  42040. STACK_OF(GENERAL_NAME)* sk = NULL;
  42041. BIO* out = NULL;
  42042. unsigned char outbuf[128];
  42043. X509_EXTENSION* ext = NULL;
  42044. AUTHORITY_INFO_ACCESS* aia = NULL;
  42045. ACCESS_DESCRIPTION* ad = NULL;
  42046. ASN1_IA5STRING *dnsname = NULL;
  42047. const unsigned char v4Addr[] = {192,168,53,1};
  42048. const unsigned char v6Addr[] =
  42049. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  42050. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  42051. const unsigned char email[] =
  42052. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  42053. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  42054. const char* dnsStr = "DNS:example.com";
  42055. const char* uriStr = "URI:http://127.0.0.1:22220";
  42056. const char* v4addStr = "IP Address:192.168.53.1";
  42057. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  42058. const char* emailStr = "email:info@wolfssl.com";
  42059. const char* othrStr = "othername:<unsupported>";
  42060. const char* x400Str = "X400Name:<unsupported>";
  42061. const char* ediStr = "EdiPartyName:<unsupported>";
  42062. /* BIO to output */
  42063. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  42064. /* test for NULL param */
  42065. gn = NULL;
  42066. ExpectIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  42067. ExpectIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  42068. ExpectIntEQ(GENERAL_NAME_print(out, NULL), 0);
  42069. /* test for GEN_DNS */
  42070. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  42071. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  42072. if (f != XBADFILE) {
  42073. XFCLOSE(f);
  42074. f = XBADFILE;
  42075. }
  42076. bufPt = buf;
  42077. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  42078. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  42079. NID_subject_alt_name, NULL, NULL));
  42080. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  42081. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42082. XMEMSET(outbuf, 0, sizeof(outbuf));
  42083. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42084. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  42085. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  42086. gn = NULL;
  42087. sk = NULL;
  42088. X509_free(x509);
  42089. x509 = NULL;
  42090. /* Lets test for setting as well. */
  42091. ExpectNotNull(gn = GENERAL_NAME_new());
  42092. ExpectNotNull(dnsname = ASN1_IA5STRING_new());
  42093. ExpectIntEQ(ASN1_STRING_set(dnsname, "example.com", -1), 1);
  42094. GENERAL_NAME_set0_value(gn, GEN_DNS, dnsname);
  42095. dnsname = NULL;
  42096. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42097. XMEMSET(outbuf, 0, sizeof(outbuf));
  42098. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42099. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  42100. GENERAL_NAME_free(gn);
  42101. /* test for GEN_URI */
  42102. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  42103. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  42104. if (f != XBADFILE) {
  42105. XFCLOSE(f);
  42106. f = XBADFILE;
  42107. }
  42108. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  42109. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  42110. ext));
  42111. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0));
  42112. if (ad != NULL) {
  42113. gn = ad->location;
  42114. }
  42115. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42116. gn = NULL;
  42117. XMEMSET(outbuf,0,sizeof(outbuf));
  42118. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42119. ExpectIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  42120. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  42121. aia = NULL;
  42122. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  42123. ExpectNotNull(aia);
  42124. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  42125. aia = NULL;
  42126. X509_free(x509);
  42127. x509 = NULL;
  42128. /* test for GEN_IPADD */
  42129. /* ip v4 address */
  42130. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42131. if (gn != NULL) {
  42132. gn->type = GEN_IPADD;
  42133. if (gn->d.iPAddress != NULL) {
  42134. gn->d.iPAddress->length = sizeof(v4Addr);
  42135. }
  42136. }
  42137. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  42138. sizeof(v4Addr)), 1);
  42139. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42140. XMEMSET(outbuf,0,sizeof(outbuf));
  42141. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42142. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  42143. GENERAL_NAME_free(gn);
  42144. gn = NULL;
  42145. /* ip v6 address */
  42146. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42147. if (gn != NULL) {
  42148. gn->type = GEN_IPADD;
  42149. if (gn->d.iPAddress != NULL) {
  42150. gn->d.iPAddress->length = sizeof(v6Addr);
  42151. }
  42152. }
  42153. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  42154. sizeof(v6Addr)), 1);
  42155. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42156. XMEMSET(outbuf,0,sizeof(outbuf));
  42157. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42158. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  42159. GENERAL_NAME_free(gn);
  42160. gn = NULL;
  42161. /* test for GEN_EMAIL */
  42162. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42163. if (gn != NULL) {
  42164. gn->type = GEN_EMAIL;
  42165. if (gn->d.rfc822Name != NULL) {
  42166. gn->d.rfc822Name->length = sizeof(email);
  42167. }
  42168. }
  42169. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email, sizeof(email)),
  42170. 1);
  42171. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42172. XMEMSET(outbuf,0,sizeof(outbuf));
  42173. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42174. ExpectIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  42175. GENERAL_NAME_free(gn);
  42176. gn = NULL;
  42177. /* test for GEN_OTHERNAME */
  42178. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42179. if (gn != NULL) {
  42180. gn->type = GEN_OTHERNAME;
  42181. }
  42182. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42183. XMEMSET(outbuf,0,sizeof(outbuf));
  42184. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42185. ExpectIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  42186. GENERAL_NAME_free(gn);
  42187. gn = NULL;
  42188. /* test for GEN_X400 */
  42189. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42190. if (gn != NULL) {
  42191. gn->type = GEN_X400;
  42192. }
  42193. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42194. XMEMSET(outbuf,0,sizeof(outbuf));
  42195. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42196. ExpectIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  42197. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  42198. if (gn != NULL) {
  42199. gn->type = GEN_IA5;
  42200. }
  42201. GENERAL_NAME_free(gn);
  42202. gn = NULL;
  42203. /* test for GEN_EDIPARTY */
  42204. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42205. if (gn != NULL) {
  42206. gn->type = GEN_EDIPARTY;
  42207. }
  42208. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42209. XMEMSET(outbuf,0,sizeof(outbuf));
  42210. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42211. ExpectIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  42212. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  42213. if (gn != NULL) {
  42214. gn->type = GEN_IA5;
  42215. }
  42216. GENERAL_NAME_free(gn);
  42217. gn = NULL;
  42218. BIO_free(out);
  42219. #endif /* OPENSSL_ALL */
  42220. return EXPECT_RESULT();
  42221. }
  42222. static int test_wolfSSL_sk_DIST_POINT(void)
  42223. {
  42224. EXPECT_DECLS;
  42225. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  42226. !defined(NO_RSA)
  42227. X509* x509 = NULL;
  42228. unsigned char buf[4096];
  42229. const unsigned char* bufPt;
  42230. int bytes = 0;
  42231. int i = 0;
  42232. int j = 0;
  42233. XFILE f = XBADFILE;
  42234. DIST_POINT* dp = NULL;
  42235. DIST_POINT_NAME* dpn = NULL;
  42236. GENERAL_NAME* gn = NULL;
  42237. ASN1_IA5STRING* uri = NULL;
  42238. STACK_OF(DIST_POINT)* dps = NULL;
  42239. STACK_OF(GENERAL_NAME)* gns = NULL;
  42240. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  42241. ExpectTrue((f = XFOPEN(cliCertDerCrlDistPoint, "rb")) != XBADFILE);
  42242. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  42243. if (f != XBADFILE)
  42244. XFCLOSE(f);
  42245. bufPt = buf;
  42246. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  42247. ExpectNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  42248. NID_crl_distribution_points, NULL, NULL));
  42249. ExpectIntEQ(sk_DIST_POINT_num(dps), 1);
  42250. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  42251. ExpectNotNull(dp = sk_DIST_POINT_value(dps, i));
  42252. ExpectNotNull(dpn = dp->distpoint);
  42253. /* this should be type 0, fullname */
  42254. ExpectIntEQ(dpn->type, 0);
  42255. ExpectNotNull(gns = dp->distpoint->name.fullname);
  42256. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  42257. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  42258. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, j));
  42259. ExpectIntEQ(gn->type, GEN_URI);
  42260. ExpectNotNull(uri = gn->d.uniformResourceIdentifier);
  42261. ExpectNotNull(uri->data);
  42262. ExpectIntGT(uri->length, 0);
  42263. }
  42264. }
  42265. X509_free(x509);
  42266. CRL_DIST_POINTS_free(dps);
  42267. #endif
  42268. return EXPECT_RESULT();
  42269. }
  42270. static int test_wolfSSL_verify_mode(void)
  42271. {
  42272. EXPECT_DECLS;
  42273. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  42274. WOLFSSL* ssl = NULL;
  42275. WOLFSSL_CTX* ctx = NULL;
  42276. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42277. ExpectNotNull(ssl = SSL_new(ctx));
  42278. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  42279. SSL_free(ssl);
  42280. ssl = NULL;
  42281. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  42282. ExpectNotNull(ssl = SSL_new(ctx));
  42283. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  42284. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  42285. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  42286. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  42287. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  42288. SSL_free(ssl);
  42289. ssl = NULL;
  42290. wolfSSL_CTX_set_verify(ctx,
  42291. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  42292. ExpectNotNull(ssl = SSL_new(ctx));
  42293. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  42294. ExpectIntEQ(SSL_get_verify_mode(ssl),
  42295. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  42296. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  42297. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  42298. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  42299. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  42300. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  42301. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  42302. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  42303. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  42304. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  42305. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  42306. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  42307. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  42308. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  42309. #endif
  42310. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  42311. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  42312. SSL_free(ssl);
  42313. SSL_CTX_free(ctx);
  42314. #endif
  42315. return EXPECT_RESULT();
  42316. }
  42317. static int test_wolfSSL_verify_depth(void)
  42318. {
  42319. EXPECT_DECLS;
  42320. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  42321. WOLFSSL* ssl = NULL;
  42322. WOLFSSL_CTX* ctx = NULL;
  42323. long depth;
  42324. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42325. ExpectIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  42326. ExpectNotNull(ssl = SSL_new(ctx));
  42327. ExpectIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  42328. SSL_free(ssl);
  42329. ssl = NULL;
  42330. SSL_CTX_set_verify_depth(ctx, -1);
  42331. ExpectIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  42332. SSL_CTX_set_verify_depth(ctx, 2);
  42333. ExpectIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  42334. ExpectNotNull(ssl = SSL_new(ctx));
  42335. ExpectIntEQ(2, SSL_get_verify_depth(ssl));
  42336. SSL_free(ssl);
  42337. SSL_CTX_free(ctx);
  42338. #endif
  42339. return EXPECT_RESULT();
  42340. }
  42341. static int test_wolfSSL_verify_result(void)
  42342. {
  42343. EXPECT_DECLS;
  42344. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  42345. defined(OPENSSL_ALL)) && !defined(NO_WOLFSSL_CLIENT)
  42346. WOLFSSL* ssl = NULL;
  42347. WOLFSSL_CTX* ctx = NULL;
  42348. long result = 0xDEADBEEF;
  42349. ExpectIntEQ(WOLFSSL_FAILURE, wolfSSL_get_verify_result(ssl));
  42350. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42351. ExpectNotNull(ssl = SSL_new(ctx));
  42352. wolfSSL_set_verify_result(ssl, result);
  42353. ExpectIntEQ(result, wolfSSL_get_verify_result(ssl));
  42354. SSL_free(ssl);
  42355. SSL_CTX_free(ctx);
  42356. #endif
  42357. return EXPECT_RESULT();
  42358. }
  42359. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  42360. static void sslMsgCb(int w, int version, int type, const void* buf,
  42361. size_t sz, SSL* ssl, void* arg)
  42362. {
  42363. int i;
  42364. unsigned char* pt = (unsigned char*)buf;
  42365. fprintf(stderr, "%s %d bytes of version %d , type %d : ",
  42366. (w)?"Writing":"Reading", (int)sz, version, type);
  42367. for (i = 0; i < (int)sz; i++) fprintf(stderr, "%02X", pt[i]);
  42368. fprintf(stderr, "\n");
  42369. (void)ssl;
  42370. (void)arg;
  42371. }
  42372. #endif /* OPENSSL_EXTRA */
  42373. static int test_wolfSSL_msg_callback(void)
  42374. {
  42375. EXPECT_DECLS;
  42376. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  42377. WOLFSSL* ssl = NULL;
  42378. WOLFSSL_CTX* ctx = NULL;
  42379. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42380. ExpectNotNull(ssl = SSL_new(ctx));
  42381. ExpectIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  42382. ExpectIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  42383. ExpectIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  42384. SSL_free(ssl);
  42385. SSL_CTX_free(ctx);
  42386. #endif
  42387. return EXPECT_RESULT();
  42388. }
  42389. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  42390. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  42391. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  42392. static void binary_dump(void *ptr, int size)
  42393. {
  42394. #ifdef WOLFSSL_EVP_PRINT
  42395. int i = 0;
  42396. unsigned char *p = (unsigned char *) ptr;
  42397. fprintf(stderr, "{");
  42398. while ((p != NULL) && (i < size)) {
  42399. if ((i % 8) == 0) {
  42400. fprintf(stderr, "\n");
  42401. fprintf(stderr, " ");
  42402. }
  42403. fprintf(stderr, "0x%02x, ", p[i]);
  42404. i++;
  42405. }
  42406. fprintf(stderr, "\n};\n");
  42407. #else
  42408. (void) ptr;
  42409. (void) size;
  42410. #endif
  42411. }
  42412. static int last_val = 0x0f;
  42413. static int check_result(unsigned char *data, int len)
  42414. {
  42415. int i;
  42416. for ( ; len; ) {
  42417. last_val = (last_val + 1) % 16;
  42418. for (i = 0; i < 16; len--, i++, data++)
  42419. if (*data != last_val) {
  42420. return -1;
  42421. }
  42422. }
  42423. return 0;
  42424. }
  42425. static int r_offset;
  42426. static int w_offset;
  42427. static void init_offset(void)
  42428. {
  42429. r_offset = 0;
  42430. w_offset = 0;
  42431. }
  42432. static void get_record(unsigned char *data, unsigned char *buf, int len)
  42433. {
  42434. XMEMCPY(buf, data+r_offset, len);
  42435. r_offset += len;
  42436. }
  42437. static void set_record(unsigned char *data, unsigned char *buf, int len)
  42438. {
  42439. XMEMCPY(data+w_offset, buf, len);
  42440. w_offset += len;
  42441. }
  42442. static void set_plain(unsigned char *plain, int rec)
  42443. {
  42444. int i, j;
  42445. unsigned char *p = plain;
  42446. #define BLOCKSZ 16
  42447. for (i=0; i<(rec/BLOCKSZ); i++) {
  42448. for (j=0; j<BLOCKSZ; j++)
  42449. *p++ = (i % 16);
  42450. }
  42451. }
  42452. #endif
  42453. static int test_wolfSSL_EVP_Cipher_extra(void)
  42454. {
  42455. EXPECT_DECLS;
  42456. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  42457. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  42458. /* aes128-cbc, keylen=16, ivlen=16 */
  42459. byte aes128_cbc_key[] = {
  42460. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  42461. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  42462. };
  42463. byte aes128_cbc_iv[] = {
  42464. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  42465. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  42466. };
  42467. /* teset data size table */
  42468. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  42469. int test_drive2[] = {8, 3, 8, 512, 0};
  42470. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  42471. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  42472. int test_drive_len[100];
  42473. int ret = 0;
  42474. EVP_CIPHER_CTX *evp = NULL;
  42475. int ilen = 0;
  42476. int klen = 0;
  42477. int i, j;
  42478. const EVP_CIPHER *type;
  42479. byte *iv;
  42480. byte *key;
  42481. int ivlen;
  42482. int keylen;
  42483. #define RECORDS 16
  42484. #define BUFFSZ 512
  42485. byte plain [BUFFSZ * RECORDS];
  42486. byte cipher[BUFFSZ * RECORDS];
  42487. byte inb[BUFFSZ];
  42488. byte outb[BUFFSZ+16];
  42489. int outl = 0;
  42490. int inl;
  42491. iv = aes128_cbc_iv;
  42492. ivlen = sizeof(aes128_cbc_iv);
  42493. key = aes128_cbc_key;
  42494. keylen = sizeof(aes128_cbc_key);
  42495. type = EVP_aes_128_cbc();
  42496. set_plain(plain, BUFFSZ * RECORDS);
  42497. SSL_library_init();
  42498. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  42499. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  42500. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  42501. klen = EVP_CIPHER_CTX_key_length(evp);
  42502. if (klen > 0 && keylen != klen) {
  42503. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  42504. }
  42505. ilen = EVP_CIPHER_CTX_iv_length(evp);
  42506. if (ilen > 0 && ivlen != ilen) {
  42507. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  42508. }
  42509. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  42510. for (j = 0; j<RECORDS; j++)
  42511. {
  42512. inl = BUFFSZ;
  42513. get_record(plain, inb, inl);
  42514. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  42515. set_record(cipher, outb, outl);
  42516. }
  42517. for (i = 0; test_drive[i]; i++) {
  42518. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  42519. init_offset();
  42520. test_drive_len[i] = 0;
  42521. for (j = 0; test_drive[i][j]; j++)
  42522. {
  42523. inl = test_drive[i][j];
  42524. test_drive_len[i] += inl;
  42525. get_record(plain, inb, inl);
  42526. ExpectIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)),
  42527. 0);
  42528. /* output to cipher buffer, so that following Dec test can detect
  42529. if any error */
  42530. set_record(cipher, outb, outl);
  42531. }
  42532. EVP_CipherFinal(evp, outb, &outl);
  42533. if (outl > 0)
  42534. set_record(cipher, outb, outl);
  42535. }
  42536. for (i = 0; test_drive[i]; i++) {
  42537. last_val = 0x0f;
  42538. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  42539. init_offset();
  42540. for (j = 0; test_drive[i][j]; j++) {
  42541. inl = test_drive[i][j];
  42542. get_record(cipher, inb, inl);
  42543. ExpectIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)),
  42544. 0);
  42545. binary_dump(outb, outl);
  42546. ExpectIntEQ((ret = check_result(outb, outl)), 0);
  42547. ExpectFalse(outl > ((inl/16+1)*16) && outl > 16);
  42548. }
  42549. ret = EVP_CipherFinal(evp, outb, &outl);
  42550. binary_dump(outb, outl);
  42551. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  42552. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  42553. ExpectTrue(ret);
  42554. }
  42555. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(evp), WOLFSSL_SUCCESS);
  42556. EVP_CIPHER_CTX_free(evp);
  42557. evp = NULL;
  42558. /* Do an extra test to verify correct behavior with empty input. */
  42559. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  42560. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  42561. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  42562. klen = EVP_CIPHER_CTX_key_length(evp);
  42563. if (klen > 0 && keylen != klen) {
  42564. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  42565. }
  42566. ilen = EVP_CIPHER_CTX_iv_length(evp);
  42567. if (ilen > 0 && ivlen != ilen) {
  42568. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  42569. }
  42570. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  42571. /* outl should be set to 0 after passing NULL, 0 for input args. */
  42572. outl = -1;
  42573. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, NULL, 0)), 0);
  42574. ExpectIntEQ(outl, 0);
  42575. EVP_CIPHER_CTX_free(evp);
  42576. #endif /* test_EVP_Cipher */
  42577. return EXPECT_RESULT();
  42578. }
  42579. static int test_wolfSSL_PEM_read_DHparams(void)
  42580. {
  42581. EXPECT_DECLS;
  42582. #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
  42583. !defined(NO_FILESYSTEM)
  42584. DH* dh = NULL;
  42585. XFILE fp = XBADFILE;
  42586. unsigned char derOut[300];
  42587. unsigned char* derOutBuf = derOut;
  42588. int derOutSz = 0;
  42589. unsigned char derExpected[300];
  42590. int derExpectedSz = 0;
  42591. XMEMSET(derOut, 0, sizeof(derOut));
  42592. XMEMSET(derExpected, 0, sizeof(derExpected));
  42593. /* open DH param file, read into DH struct */
  42594. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  42595. /* bad args */
  42596. ExpectNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  42597. ExpectNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  42598. /* good args */
  42599. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  42600. if (fp != XBADFILE) {
  42601. XFCLOSE(fp);
  42602. fp = XBADFILE;
  42603. }
  42604. /* read in certs/dh2048.der for comparison against exported params */
  42605. ExpectTrue((fp = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  42606. ExpectIntGT(derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected),
  42607. fp), 0);
  42608. if (fp != XBADFILE) {
  42609. XFCLOSE(fp);
  42610. fp = XBADFILE;
  42611. }
  42612. /* export DH back to DER and compare */
  42613. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  42614. ExpectIntEQ(derOutSz, derExpectedSz);
  42615. ExpectIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  42616. DH_free(dh);
  42617. dh = NULL;
  42618. /* Test parsing with X9.42 header */
  42619. ExpectTrue((fp = XFOPEN("./certs/x942dh2048.pem", "rb")) != XBADFILE);
  42620. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  42621. if (fp != XBADFILE)
  42622. XFCLOSE(fp);
  42623. DH_free(dh);
  42624. dh = NULL;
  42625. #endif
  42626. return EXPECT_RESULT();
  42627. }
  42628. static int test_wolfSSL_X509_get_serialNumber(void)
  42629. {
  42630. EXPECT_DECLS;
  42631. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  42632. ASN1_INTEGER* a = NULL;
  42633. BIGNUM* bn = NULL;
  42634. X509* x509 = NULL;
  42635. char *serialHex = NULL;
  42636. byte serial[3];
  42637. int serialSz;
  42638. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42639. SSL_FILETYPE_PEM));
  42640. ExpectNotNull(a = X509_get_serialNumber(x509));
  42641. /* check on value of ASN1 Integer */
  42642. ExpectNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  42643. a = NULL;
  42644. /* test setting serial number and then retrieving it */
  42645. ExpectNotNull(a = ASN1_INTEGER_new());
  42646. ExpectIntEQ(ASN1_INTEGER_set(a, 3), 1);
  42647. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  42648. serialSz = sizeof(serial);
  42649. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  42650. WOLFSSL_SUCCESS);
  42651. ExpectIntEQ(serialSz, 1);
  42652. ExpectIntEQ(serial[0], 3);
  42653. ASN1_INTEGER_free(a);
  42654. a = NULL;
  42655. /* test setting serial number with 0's in it */
  42656. serial[0] = 0x01;
  42657. serial[1] = 0x00;
  42658. serial[2] = 0x02;
  42659. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  42660. if (a != NULL) {
  42661. a->data[0] = ASN_INTEGER;
  42662. a->data[1] = sizeof(serial);
  42663. XMEMCPY(&a->data[2], serial, sizeof(serial));
  42664. a->length = sizeof(serial) + 2;
  42665. }
  42666. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  42667. XMEMSET(serial, 0, sizeof(serial));
  42668. serialSz = sizeof(serial);
  42669. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  42670. WOLFSSL_SUCCESS);
  42671. ExpectIntEQ(serialSz, 3);
  42672. ExpectIntEQ(serial[0], 0x01);
  42673. ExpectIntEQ(serial[1], 0x00);
  42674. ExpectIntEQ(serial[2], 0x02);
  42675. ASN1_INTEGER_free(a);
  42676. a = NULL;
  42677. X509_free(x509); /* free's a */
  42678. ExpectNotNull(serialHex = BN_bn2hex(bn));
  42679. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  42680. ExpectStrEQ(serialHex, "01");
  42681. #else
  42682. ExpectStrEQ(serialHex, "1");
  42683. #endif
  42684. OPENSSL_free(serialHex);
  42685. ExpectIntEQ(BN_get_word(bn), 1);
  42686. BN_free(bn);
  42687. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  42688. ExpectNotNull(a = ASN1_INTEGER_new());
  42689. if (a != NULL) {
  42690. ExpectNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  42691. DYNAMIC_TYPE_OPENSSL));
  42692. a->isDynamic = 1;
  42693. ASN1_INTEGER_free(a);
  42694. }
  42695. #endif
  42696. return EXPECT_RESULT();
  42697. }
  42698. static int test_wolfSSL_OpenSSL_add_all_algorithms(void)
  42699. {
  42700. EXPECT_DECLS;
  42701. #if defined(OPENSSL_EXTRA)
  42702. ExpectIntEQ(wolfSSL_add_all_algorithms(), WOLFSSL_SUCCESS);
  42703. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(), WOLFSSL_SUCCESS);
  42704. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(), WOLFSSL_SUCCESS);
  42705. #endif
  42706. return EXPECT_RESULT();
  42707. }
  42708. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  42709. {
  42710. EXPECT_DECLS;
  42711. #if defined(OPENSSL_EXTRA)
  42712. #define MAX_HEXSTR_BUFSZ 9
  42713. #define NUM_CASES 5
  42714. struct Output {
  42715. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  42716. long ret;
  42717. };
  42718. int i;
  42719. int j;
  42720. const char* inputs[NUM_CASES] = {
  42721. "aabcd1357e",
  42722. "01:12:23:34:a5:b6:c7:d8:e9",
  42723. ":01:02",
  42724. "012",
  42725. ":ab:ac:d"
  42726. };
  42727. struct Output expectedOutputs[NUM_CASES] = {
  42728. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  42729. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  42730. {{0x01, 0x02}, 2},
  42731. {{0x00}, 0},
  42732. {{0x00}, 0}
  42733. };
  42734. long len = 0;
  42735. unsigned char* returnedBuf = NULL;
  42736. for (i = 0; i < NUM_CASES && !EXPECT_FAIL(); ++i) {
  42737. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  42738. if (returnedBuf == NULL) {
  42739. ExpectIntEQ(expectedOutputs[i].ret, 0);
  42740. continue;
  42741. }
  42742. ExpectIntEQ(expectedOutputs[i].ret, len);
  42743. for (j = 0; j < len; ++j) {
  42744. ExpectIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  42745. }
  42746. OPENSSL_free(returnedBuf);
  42747. returnedBuf = NULL;
  42748. }
  42749. #endif
  42750. return EXPECT_RESULT();
  42751. }
  42752. static int test_wolfSSL_X509_CA_num(void)
  42753. {
  42754. EXPECT_DECLS;
  42755. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  42756. defined(HAVE_ECC) && !defined(NO_RSA)
  42757. WOLFSSL_X509_STORE *store = NULL;
  42758. WOLFSSL_X509 *x509_1 = NULL;
  42759. WOLFSSL_X509 *x509_2 = NULL;
  42760. int ca_num = 0;
  42761. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  42762. ExpectNotNull(x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42763. WOLFSSL_FILETYPE_PEM));
  42764. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_1), 1);
  42765. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 1);
  42766. ExpectNotNull(x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile,
  42767. WOLFSSL_FILETYPE_PEM));
  42768. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_2), 1);
  42769. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 2);
  42770. wolfSSL_X509_free(x509_1);
  42771. wolfSSL_X509_free(x509_2);
  42772. wolfSSL_X509_STORE_free(store);
  42773. #endif
  42774. return EXPECT_RESULT();
  42775. }
  42776. static int test_wolfSSL_X509_check_ca(void)
  42777. {
  42778. EXPECT_DECLS;
  42779. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  42780. WOLFSSL_X509 *x509 = NULL;
  42781. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42782. WOLFSSL_FILETYPE_PEM));
  42783. ExpectIntEQ(wolfSSL_X509_check_ca(x509), 1);
  42784. wolfSSL_X509_free(x509);
  42785. #endif
  42786. return EXPECT_RESULT();
  42787. }
  42788. static int test_wolfSSL_X509_check_ip_asc(void)
  42789. {
  42790. EXPECT_DECLS;
  42791. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  42792. WOLFSSL_X509 *x509 = NULL;
  42793. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  42794. WOLFSSL_FILETYPE_PEM));
  42795. #if 0
  42796. /* TODO: add cert gen for testing positive case */
  42797. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  42798. #endif
  42799. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  42800. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  42801. wolfSSL_X509_free(x509);
  42802. #endif
  42803. return EXPECT_RESULT();
  42804. }
  42805. static int test_wolfSSL_make_cert(void)
  42806. {
  42807. EXPECT_DECLS;
  42808. #if !defined(NO_RSA) && !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && \
  42809. defined(WOLFSSL_CERT_EXT)
  42810. int ret = 0;
  42811. Cert cert;
  42812. CertName name;
  42813. RsaKey key;
  42814. WC_RNG rng;
  42815. byte der[FOURK_BUF];
  42816. word32 idx = 0;
  42817. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  42818. #ifdef OPENSSL_EXTRA
  42819. const unsigned char* pt = NULL;
  42820. int certSz = 0;
  42821. X509* x509 = NULL;
  42822. X509_NAME* x509name = NULL;
  42823. X509_NAME_ENTRY* entry = NULL;
  42824. ASN1_STRING* entryValue = NULL;
  42825. #endif
  42826. XMEMSET(&name, 0, sizeof(CertName));
  42827. /* set up cert name */
  42828. XMEMCPY(name.country, "US", sizeof("US"));
  42829. name.countryEnc = CTC_PRINTABLE;
  42830. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  42831. name.stateEnc = CTC_UTF8;
  42832. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  42833. name.localityEnc = CTC_UTF8;
  42834. XMEMCPY(name.sur, "Test", sizeof("Test"));
  42835. name.surEnc = CTC_UTF8;
  42836. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  42837. name.orgEnc = CTC_UTF8;
  42838. XMEMCPY(name.unit, "Development", sizeof("Development"));
  42839. name.unitEnc = CTC_UTF8;
  42840. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  42841. name.commonNameEnc = CTC_UTF8;
  42842. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  42843. name.serialDevEnc = CTC_PRINTABLE;
  42844. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  42845. name.userIdEnc = CTC_PRINTABLE;
  42846. #ifdef WOLFSSL_MULTI_ATTRIB
  42847. #if CTC_MAX_ATTRIB > 2
  42848. {
  42849. NameAttrib* n;
  42850. n = &name.name[0];
  42851. n->id = ASN_DOMAIN_COMPONENT;
  42852. n->type = CTC_UTF8;
  42853. n->sz = sizeof("com");
  42854. XMEMCPY(n->value, "com", sizeof("com"));
  42855. n = &name.name[1];
  42856. n->id = ASN_DOMAIN_COMPONENT;
  42857. n->type = CTC_UTF8;
  42858. n->sz = sizeof("wolfssl");
  42859. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  42860. }
  42861. #endif
  42862. #endif /* WOLFSSL_MULTI_ATTRIB */
  42863. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  42864. #ifndef HAVE_FIPS
  42865. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  42866. #else
  42867. ExpectIntEQ(wc_InitRng(&rng), 0);
  42868. #endif
  42869. /* load test RSA key */
  42870. idx = 0;
  42871. #if defined(USE_CERT_BUFFERS_1024)
  42872. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  42873. sizeof_server_key_der_1024), 0);
  42874. #elif defined(USE_CERT_BUFFERS_2048)
  42875. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  42876. sizeof_server_key_der_2048), 0);
  42877. #else
  42878. /* error case, no RSA key loaded, happens later */
  42879. (void)idx;
  42880. #endif
  42881. XMEMSET(&cert, 0 , sizeof(Cert));
  42882. ExpectIntEQ(wc_InitCert(&cert), 0);
  42883. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  42884. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  42885. cert.serialSz = (int)sizeof(mySerial);
  42886. cert.isCA = 1;
  42887. #ifndef NO_SHA256
  42888. cert.sigType = CTC_SHA256wRSA;
  42889. #else
  42890. cert.sigType = CTC_SHAwRSA;
  42891. #endif
  42892. /* add SKID from the Public Key */
  42893. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  42894. /* add AKID from the Public Key */
  42895. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  42896. ret = 0;
  42897. do {
  42898. #if defined(WOLFSSL_ASYNC_CRYPT)
  42899. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  42900. #endif
  42901. if (ret >= 0) {
  42902. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  42903. }
  42904. } while (ret == WC_PENDING_E);
  42905. ExpectIntGT(ret, 0);
  42906. #ifdef OPENSSL_EXTRA
  42907. /* der holds a certificate with DC's now check X509 parsing of it */
  42908. certSz = ret;
  42909. pt = der;
  42910. ExpectNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  42911. ExpectNotNull(x509name = X509_get_subject_name(x509));
  42912. #ifdef WOLFSSL_MULTI_ATTRIB
  42913. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42914. -1)), 5);
  42915. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42916. idx)), 6);
  42917. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42918. idx)), -1);
  42919. #endif /* WOLFSSL_MULTI_ATTRIB */
  42920. /* compare DN at index 0 */
  42921. ExpectNotNull(entry = X509_NAME_get_entry(x509name, 0));
  42922. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  42923. ExpectIntEQ(ASN1_STRING_length(entryValue), 2);
  42924. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  42925. #ifndef WOLFSSL_MULTI_ATTRIB
  42926. /* compare Serial Number */
  42927. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_serialNumber,
  42928. -1)), 7);
  42929. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  42930. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  42931. ExpectIntEQ(ASN1_STRING_length(entryValue), XSTRLEN("wolfSSL12345"));
  42932. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "wolfSSL12345");
  42933. #endif
  42934. #ifdef WOLFSSL_MULTI_ATTRIB
  42935. /* get first and second DC and compare result */
  42936. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42937. -1)), 5);
  42938. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  42939. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  42940. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  42941. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  42942. idx)), 6);
  42943. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  42944. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  42945. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  42946. #endif /* WOLFSSL_MULTI_ATTRIB */
  42947. /* try invalid index locations for regression test and sanity check */
  42948. ExpectNull(entry = X509_NAME_get_entry(x509name, 11));
  42949. ExpectNull(entry = X509_NAME_get_entry(x509name, 20));
  42950. X509_free(x509);
  42951. #endif /* OPENSSL_EXTRA */
  42952. wc_FreeRsaKey(&key);
  42953. wc_FreeRng(&rng);
  42954. #endif
  42955. return EXPECT_RESULT();
  42956. }
  42957. static int test_wolfSSL_X509_get_version(void)
  42958. {
  42959. EXPECT_DECLS;
  42960. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  42961. WOLFSSL_X509 *x509 = NULL;
  42962. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42963. WOLFSSL_FILETYPE_PEM));
  42964. ExpectIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  42965. wolfSSL_X509_free(x509);
  42966. #endif
  42967. return EXPECT_RESULT();
  42968. }
  42969. #if defined(OPENSSL_ALL)
  42970. static int test_wolfSSL_sk_CIPHER_description(void)
  42971. {
  42972. EXPECT_DECLS;
  42973. #if !defined(NO_RSA)
  42974. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  42975. int i;
  42976. int numCiphers = 0;
  42977. const SSL_METHOD *method = NULL;
  42978. const SSL_CIPHER *cipher = NULL;
  42979. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  42980. SSL_CTX *ctx = NULL;
  42981. SSL *ssl = NULL;
  42982. char buf[256];
  42983. char test_str[9] = "0000000";
  42984. const char badStr[] = "unknown";
  42985. const char certPath[] = "./certs/client-cert.pem";
  42986. XMEMSET(buf, 0, sizeof(buf));
  42987. ExpectNotNull(method = TLSv1_2_client_method());
  42988. ExpectNotNull(ctx = SSL_CTX_new(method));
  42989. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  42990. SSL_CTX_set_verify_depth(ctx, 4);
  42991. SSL_CTX_set_options(ctx, flags);
  42992. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  42993. WOLFSSL_SUCCESS);
  42994. ExpectNotNull(ssl = SSL_new(ctx));
  42995. /* SSL_get_ciphers returns a stack of all configured ciphers
  42996. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  42997. */
  42998. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  42999. /* loop through the amount of supportedCiphers */
  43000. numCiphers = sk_num(supportedCiphers);
  43001. for (i = 0; i < numCiphers; ++i) {
  43002. int j;
  43003. /* sk_value increments "sk->data.cipher->cipherOffset".
  43004. * wolfSSL_sk_CIPHER_description sets the description for
  43005. * the cipher based on the provided offset.
  43006. */
  43007. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  43008. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  43009. }
  43010. /* Search cipher description string for "unknown" descriptor */
  43011. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  43012. int k = 0;
  43013. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  43014. test_str[k] = badStr[k];
  43015. j++;
  43016. k++;
  43017. }
  43018. }
  43019. /* Fail if test_str == badStr == "unknown" */
  43020. ExpectStrNE(test_str,badStr);
  43021. }
  43022. SSL_free(ssl);
  43023. SSL_CTX_free(ctx);
  43024. #endif
  43025. return EXPECT_RESULT();
  43026. }
  43027. static int test_wolfSSL_get_ciphers_compat(void)
  43028. {
  43029. EXPECT_DECLS;
  43030. #if !defined(NO_RSA)
  43031. const SSL_METHOD *method = NULL;
  43032. const char certPath[] = "./certs/client-cert.pem";
  43033. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  43034. SSL_CTX *ctx = NULL;
  43035. WOLFSSL *ssl = NULL;
  43036. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  43037. ExpectNotNull(method = SSLv23_client_method());
  43038. ExpectNotNull(ctx = SSL_CTX_new(method));
  43039. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  43040. SSL_CTX_set_verify_depth(ctx, 4);
  43041. SSL_CTX_set_options(ctx, flags);
  43042. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  43043. WOLFSSL_SUCCESS);
  43044. ExpectNotNull(ssl = SSL_new(ctx));
  43045. /* Test Bad NULL input */
  43046. ExpectNull(supportedCiphers = SSL_get_ciphers(NULL));
  43047. /* Test for Good input */
  43048. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  43049. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  43050. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  43051. SSL_free(ssl);
  43052. SSL_CTX_free(ctx);
  43053. #endif
  43054. return EXPECT_RESULT();
  43055. }
  43056. static int test_wolfSSL_X509_PUBKEY_get(void)
  43057. {
  43058. EXPECT_DECLS;
  43059. WOLFSSL_X509_PUBKEY pubkey;
  43060. WOLFSSL_X509_PUBKEY* key;
  43061. WOLFSSL_EVP_PKEY evpkey ;
  43062. WOLFSSL_EVP_PKEY* evpPkey;
  43063. WOLFSSL_EVP_PKEY* retEvpPkey;
  43064. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  43065. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  43066. key = &pubkey;
  43067. evpPkey = &evpkey;
  43068. evpPkey->type = WOLFSSL_SUCCESS;
  43069. key->pkey = evpPkey;
  43070. ExpectNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  43071. ExpectIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  43072. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  43073. key->pkey = NULL;
  43074. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  43075. return EXPECT_RESULT();
  43076. }
  43077. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  43078. {
  43079. EXPECT_DECLS;
  43080. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  43081. DSA *dsa = NULL;
  43082. DSA *setDsa = NULL;
  43083. EVP_PKEY *pkey = NULL;
  43084. EVP_PKEY *set1Pkey = NULL;
  43085. SHA_CTX sha;
  43086. byte signature[DSA_SIG_SIZE];
  43087. byte hash[WC_SHA_DIGEST_SIZE];
  43088. word32 bytes;
  43089. int answer;
  43090. #ifdef USE_CERT_BUFFERS_1024
  43091. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  43092. int dsaKeySz = sizeof_dsa_key_der_1024;
  43093. byte tmp[ONEK_BUF];
  43094. XMEMSET(tmp, 0, sizeof(tmp));
  43095. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  43096. bytes = dsaKeySz;
  43097. #elif defined(USE_CERT_BUFFERS_2048)
  43098. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  43099. int dsaKeySz = sizeof_dsa_key_der_2048;
  43100. byte tmp[TWOK_BUF];
  43101. XMEMSET(tmp, 0, sizeof(tmp));
  43102. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  43103. bytes = dsaKeySz;
  43104. #else
  43105. byte tmp[TWOK_BUF];
  43106. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  43107. int dsaKeySz;
  43108. XFILE fp = XBADFILE;
  43109. XMEMSET(tmp, 0, sizeof(tmp));
  43110. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  43111. ExpectIntGT(dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  43112. if (fp != XBADFILE)
  43113. XFCLOSE(fp);
  43114. #endif /* END USE_CERT_BUFFERS_1024 */
  43115. /* Create hash to later Sign and Verify */
  43116. ExpectIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  43117. ExpectIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  43118. ExpectIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  43119. /* Initialize pkey with der format dsa key */
  43120. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey, &dsaKeyDer,
  43121. (long)dsaKeySz));
  43122. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  43123. /* Should Fail: NULL argument */
  43124. ExpectNull(dsa = EVP_PKEY_get0_DSA(NULL));
  43125. ExpectNull(dsa = EVP_PKEY_get1_DSA(NULL));
  43126. /* Should Pass: Initialized pkey argument */
  43127. ExpectNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  43128. ExpectNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  43129. #ifdef USE_CERT_BUFFERS_1024
  43130. ExpectIntEQ(DSA_bits(dsa), 1024);
  43131. #else
  43132. ExpectIntEQ(DSA_bits(dsa), 2048);
  43133. #endif
  43134. /* Sign */
  43135. ExpectIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  43136. /* Verify. */
  43137. ExpectIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  43138. WOLFSSL_SUCCESS);
  43139. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  43140. /* Should Fail: set1Pkey not initialized */
  43141. ExpectIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  43142. /* Initialize set1Pkey */
  43143. set1Pkey = EVP_PKEY_new();
  43144. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  43145. ExpectIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  43146. WOLFSSL_SUCCESS);
  43147. /* Should Pass: set dsa into set1Pkey */
  43148. ExpectIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  43149. DSA_free(dsa);
  43150. DSA_free(setDsa);
  43151. EVP_PKEY_free(pkey);
  43152. EVP_PKEY_free(set1Pkey);
  43153. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  43154. return EXPECT_RESULT();
  43155. } /* END test_EVP_PKEY_set1_get1_DSA */
  43156. static int test_wolfSSL_DSA_generate_parameters(void)
  43157. {
  43158. EXPECT_DECLS;
  43159. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  43160. !defined(HAVE_FIPS)
  43161. DSA *dsa = NULL;
  43162. ExpectNotNull(dsa = DSA_generate_parameters(2048, NULL, 0, NULL, NULL, NULL,
  43163. NULL));
  43164. DSA_free(dsa);
  43165. #endif
  43166. return EXPECT_RESULT();
  43167. }
  43168. static int test_wolfSSL_DSA_SIG(void)
  43169. {
  43170. EXPECT_DECLS;
  43171. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  43172. !defined(HAVE_FIPS)
  43173. DSA *dsa = NULL;
  43174. DSA *dsa2 = NULL;
  43175. DSA_SIG *sig = NULL;
  43176. const BIGNUM *p = NULL;
  43177. const BIGNUM *q = NULL;
  43178. const BIGNUM *g = NULL;
  43179. const BIGNUM *pub = NULL;
  43180. const BIGNUM *priv = NULL;
  43181. BIGNUM *dup_p = NULL;
  43182. BIGNUM *dup_q = NULL;
  43183. BIGNUM *dup_g = NULL;
  43184. BIGNUM *dup_pub = NULL;
  43185. BIGNUM *dup_priv = NULL;
  43186. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  43187. ExpectNotNull(dsa = DSA_new());
  43188. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048, NULL, 0, NULL, NULL,
  43189. NULL), 1);
  43190. ExpectIntEQ(DSA_generate_key(dsa), 1);
  43191. DSA_get0_pqg(dsa, &p, &q, &g);
  43192. DSA_get0_key(dsa, &pub, &priv);
  43193. ExpectNotNull(dup_p = BN_dup(p));
  43194. ExpectNotNull(dup_q = BN_dup(q));
  43195. ExpectNotNull(dup_g = BN_dup(g));
  43196. ExpectNotNull(dup_pub = BN_dup(pub));
  43197. ExpectNotNull(dup_priv = BN_dup(priv));
  43198. ExpectNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  43199. ExpectNotNull(dsa2 = DSA_new());
  43200. ExpectIntEQ(DSA_set0_pqg(dsa2, dup_p, dup_q, dup_g), 1);
  43201. if (EXPECT_FAIL()) {
  43202. BN_free(dup_p);
  43203. BN_free(dup_q);
  43204. BN_free(dup_g);
  43205. }
  43206. ExpectIntEQ(DSA_set0_key(dsa2, dup_pub, dup_priv), 1);
  43207. if (EXPECT_FAIL()) {
  43208. BN_free(dup_pub);
  43209. BN_free(dup_priv);
  43210. }
  43211. ExpectIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  43212. DSA_free(dsa);
  43213. DSA_free(dsa2);
  43214. DSA_SIG_free(sig);
  43215. #endif
  43216. return EXPECT_RESULT();
  43217. }
  43218. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  43219. {
  43220. EXPECT_DECLS;
  43221. #ifdef HAVE_ECC
  43222. WOLFSSL_EC_KEY* ecKey = NULL;
  43223. WOLFSSL_EC_KEY* ecGet1 = NULL;
  43224. EVP_PKEY* pkey = NULL;
  43225. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  43226. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43227. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  43228. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  43229. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  43230. /* Should fail since ecKey is empty */
  43231. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  43232. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  43233. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  43234. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  43235. ExpectNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  43236. ExpectNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  43237. wolfSSL_EC_KEY_free(ecKey);
  43238. wolfSSL_EC_KEY_free(ecGet1);
  43239. EVP_PKEY_free(pkey);
  43240. #endif /* HAVE_ECC */
  43241. return EXPECT_RESULT();
  43242. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  43243. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  43244. {
  43245. EXPECT_DECLS;
  43246. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  43247. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  43248. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  43249. DH *dh = NULL;
  43250. DH *setDh = NULL;
  43251. EVP_PKEY *pkey = NULL;
  43252. XFILE f = XBADFILE;
  43253. unsigned char buf[4096];
  43254. const unsigned char* pt = buf;
  43255. const char* dh2048 = "./certs/dh2048.der";
  43256. long len = 0;
  43257. int code = -1;
  43258. XMEMSET(buf, 0, sizeof(buf));
  43259. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  43260. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  43261. if (f != XBADFILE)
  43262. XFCLOSE(f);
  43263. /* Load dh2048.der into DH with internal format */
  43264. ExpectNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  43265. ExpectIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  43266. ExpectIntEQ(code, 0);
  43267. code = -1;
  43268. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43269. /* Set DH into PKEY */
  43270. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  43271. /* Get DH from PKEY */
  43272. ExpectNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  43273. ExpectIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  43274. ExpectIntEQ(code, 0);
  43275. EVP_PKEY_free(pkey);
  43276. DH_free(setDh);
  43277. setDh = NULL;
  43278. DH_free(dh);
  43279. dh = NULL;
  43280. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  43281. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  43282. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  43283. return EXPECT_RESULT();
  43284. } /* END test_EVP_PKEY_set1_get1_DH */
  43285. static int test_wolfSSL_CTX_ctrl(void)
  43286. {
  43287. EXPECT_DECLS;
  43288. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  43289. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  43290. char caFile[] = "./certs/client-ca.pem";
  43291. char clientFile[] = "./certs/client-cert.pem";
  43292. SSL_CTX* ctx = NULL;
  43293. X509* x509 = NULL;
  43294. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  43295. byte buf[6000];
  43296. char file[] = "./certs/dsaparams.pem";
  43297. XFILE f = XBADFILE;
  43298. int bytes = 0;
  43299. BIO* bio = NULL;
  43300. DSA* dsa = NULL;
  43301. DH* dh = NULL;
  43302. #endif
  43303. #ifdef HAVE_ECC
  43304. WOLFSSL_EC_KEY* ecKey = NULL;
  43305. #endif
  43306. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  43307. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  43308. WOLFSSL_FILETYPE_PEM));
  43309. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  43310. if (EXPECT_FAIL()) {
  43311. wolfSSL_X509_free(x509);
  43312. }
  43313. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  43314. WOLFSSL_FILETYPE_PEM));
  43315. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  43316. /* Initialize DH */
  43317. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  43318. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  43319. if (f != XBADFILE)
  43320. XFCLOSE(f);
  43321. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  43322. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  43323. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  43324. #endif
  43325. #ifdef HAVE_ECC
  43326. /* Initialize WOLFSSL_EC_KEY */
  43327. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  43328. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  43329. #endif
  43330. /* additional test of getting EVP_PKEY key size from X509
  43331. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  43332. * allowed with user RSA */
  43333. {
  43334. EVP_PKEY* pkey = NULL;
  43335. #if defined(HAVE_ECC)
  43336. X509* ecX509 = NULL;
  43337. #endif /* HAVE_ECC */
  43338. ExpectNotNull(pkey = X509_get_pubkey(x509));
  43339. /* current RSA key is 2048 bit (256 bytes) */
  43340. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  43341. EVP_PKEY_free(pkey);
  43342. pkey = NULL;
  43343. #if defined(HAVE_ECC)
  43344. #if defined(USE_CERT_BUFFERS_256)
  43345. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  43346. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  43347. SSL_FILETYPE_ASN1));
  43348. #else
  43349. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  43350. cliEccCertFile, SSL_FILETYPE_PEM));
  43351. #endif
  43352. ExpectNotNull(pkey = X509_get_pubkey(ecX509));
  43353. /* current ECC key is 256 bit (32 bytes) */
  43354. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  43355. X509_free(ecX509);
  43356. EVP_PKEY_free(pkey);
  43357. #endif /* HAVE_ECC */
  43358. }
  43359. /* Tests should fail with passed in NULL pointer */
  43360. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, NULL),
  43361. SSL_FAILURE);
  43362. #if !defined(NO_DH) && !defined(NO_DSA)
  43363. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, NULL),
  43364. SSL_FAILURE);
  43365. #endif
  43366. #ifdef HAVE_ECC
  43367. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, NULL),
  43368. SSL_FAILURE);
  43369. #endif
  43370. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  43371. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  43372. */
  43373. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, x509),
  43374. SSL_SUCCESS);
  43375. if (EXPECT_FAIL()) {
  43376. wolfSSL_X509_free(x509);
  43377. }
  43378. /* Test with SSL_CTRL_OPTIONS
  43379. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  43380. */
  43381. ExpectTrue(wolfSSL_CTX_ctrl(ctx, SSL_CTRL_OPTIONS, SSL_OP_NO_TLSv1,
  43382. NULL) == SSL_OP_NO_TLSv1);
  43383. ExpectTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  43384. /* Test with SSL_CTRL_SET_TMP_DH
  43385. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  43386. */
  43387. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  43388. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, dh),
  43389. SSL_SUCCESS);
  43390. #endif
  43391. /* Test with SSL_CTRL_SET_TMP_ECDH
  43392. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  43393. */
  43394. #ifdef HAVE_ECC
  43395. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, ecKey),
  43396. SSL_SUCCESS);
  43397. #endif
  43398. #ifdef WOLFSSL_ENCRYPTED_KEYS
  43399. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  43400. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  43401. #endif
  43402. /* Test for min/max proto */
  43403. #ifndef WOLFSSL_NO_TLS12
  43404. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  43405. 0, NULL), SSL_SUCCESS);
  43406. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  43407. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  43408. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  43409. #endif
  43410. #ifdef WOLFSSL_TLS13
  43411. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  43412. 0, NULL), SSL_SUCCESS);
  43413. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  43414. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  43415. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  43416. #ifndef WOLFSSL_NO_TLS12
  43417. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  43418. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  43419. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  43420. #endif
  43421. #endif
  43422. /* Cleanup and Pass */
  43423. #if !defined(NO_DH) && !defined(NO_DSA)
  43424. #ifndef NO_BIO
  43425. BIO_free(bio);
  43426. DSA_free(dsa);
  43427. DH_free(dh);
  43428. dh = NULL;
  43429. #endif
  43430. #endif
  43431. #ifdef HAVE_ECC
  43432. wolfSSL_EC_KEY_free(ecKey);
  43433. #endif
  43434. SSL_CTX_free(ctx);
  43435. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  43436. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  43437. return EXPECT_RESULT();
  43438. }
  43439. static int test_wolfSSL_EVP_PKEY_assign(void)
  43440. {
  43441. EXPECT_DECLS;
  43442. #if !defined(NO_RSA) || !defined(NO_DSA) || defined(HAVE_ECC)
  43443. int type;
  43444. WOLFSSL_EVP_PKEY* pkey = NULL;
  43445. #ifndef NO_RSA
  43446. WOLFSSL_RSA* rsa = NULL;
  43447. #endif
  43448. #ifndef NO_DSA
  43449. WOLFSSL_DSA* dsa = NULL;
  43450. #endif
  43451. #ifdef HAVE_ECC
  43452. WOLFSSL_EC_KEY* ecKey = NULL;
  43453. #endif
  43454. #ifndef NO_RSA
  43455. type = EVP_PKEY_RSA;
  43456. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43457. ExpectNotNull(rsa = wolfSSL_RSA_new());
  43458. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, rsa), WOLFSSL_FAILURE);
  43459. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  43460. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, rsa), WOLFSSL_FAILURE);
  43461. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, rsa), WOLFSSL_SUCCESS);
  43462. if (EXPECT_FAIL()) {
  43463. wolfSSL_RSA_free(rsa);
  43464. }
  43465. wolfSSL_EVP_PKEY_free(pkey);
  43466. pkey = NULL;
  43467. #endif /* NO_RSA */
  43468. #ifndef NO_DSA
  43469. type = EVP_PKEY_DSA;
  43470. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43471. ExpectNotNull(dsa = wolfSSL_DSA_new());
  43472. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, dsa), WOLFSSL_FAILURE);
  43473. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  43474. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, dsa), WOLFSSL_FAILURE);
  43475. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, dsa), WOLFSSL_SUCCESS);
  43476. if (EXPECT_FAIL()) {
  43477. wolfSSL_DSA_free(dsa);
  43478. }
  43479. wolfSSL_EVP_PKEY_free(pkey);
  43480. pkey = NULL;
  43481. #endif /* NO_DSA */
  43482. #ifdef HAVE_ECC
  43483. type = EVP_PKEY_EC;
  43484. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43485. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  43486. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, ecKey), WOLFSSL_FAILURE);
  43487. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  43488. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, ecKey), WOLFSSL_FAILURE);
  43489. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_FAILURE);
  43490. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  43491. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_SUCCESS);
  43492. if (EXPECT_FAIL()) {
  43493. wolfSSL_EC_KEY_free(ecKey);
  43494. }
  43495. wolfSSL_EVP_PKEY_free(pkey);
  43496. pkey = NULL;
  43497. #endif /* HAVE_ECC */
  43498. #endif /* !NO_RSA || !NO_DSA || HAVE_ECC */
  43499. return EXPECT_RESULT();
  43500. }
  43501. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  43502. {
  43503. EXPECT_DECLS;
  43504. #if !defined(NO_DH) && \
  43505. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43506. XFILE f = XBADFILE;
  43507. unsigned char buf[4096];
  43508. const unsigned char* pt = buf;
  43509. const char* params1 = "./certs/dh2048.der";
  43510. long len = 0;
  43511. WOLFSSL_DH* dh = NULL;
  43512. WOLFSSL_EVP_PKEY* pkey = NULL;
  43513. XMEMSET(buf, 0, sizeof(buf));
  43514. /* Load DH parameters DER. */
  43515. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  43516. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  43517. if (f != XBADFILE)
  43518. XFCLOSE(f);
  43519. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  43520. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  43521. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43522. /* Bad cases */
  43523. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  43524. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  43525. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  43526. /* Good case */
  43527. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  43528. if (EXPECT_FAIL()) {
  43529. wolfSSL_DH_free(dh);
  43530. }
  43531. EVP_PKEY_free(pkey);
  43532. #endif
  43533. return EXPECT_RESULT();
  43534. }
  43535. static int test_wolfSSL_EVP_PKEY_base_id(void)
  43536. {
  43537. EXPECT_DECLS;
  43538. WOLFSSL_EVP_PKEY* pkey = NULL;
  43539. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43540. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  43541. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  43542. EVP_PKEY_free(pkey);
  43543. return EXPECT_RESULT();
  43544. }
  43545. static int test_wolfSSL_EVP_PKEY_id(void)
  43546. {
  43547. EXPECT_DECLS;
  43548. WOLFSSL_EVP_PKEY* pkey = NULL;
  43549. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43550. ExpectIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  43551. ExpectIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  43552. EVP_PKEY_free(pkey);
  43553. return EXPECT_RESULT();
  43554. }
  43555. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  43556. {
  43557. EXPECT_DECLS;
  43558. /* ECC check taken from ecc.c. It is the condition that defines ECC256 */
  43559. #if defined(OPENSSL_ALL) && !defined(NO_ECC_SECP) && \
  43560. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  43561. ECC_MIN_KEY_SZ <= 256)
  43562. EVP_PKEY_CTX* ctx = NULL;
  43563. EVP_PKEY* pkey = NULL;
  43564. /* Test error conditions. */
  43565. ExpectIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  43566. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  43567. ExpectIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  43568. #ifndef NO_RSA
  43569. EVP_PKEY_CTX_free(ctx);
  43570. /* Parameter generation for RSA not supported yet. */
  43571. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  43572. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  43573. #endif
  43574. #ifdef HAVE_ECC
  43575. EVP_PKEY_CTX_free(ctx);
  43576. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  43577. ExpectIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  43578. ExpectIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  43579. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  43580. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  43581. ExpectIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  43582. WOLFSSL_SUCCESS);
  43583. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  43584. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  43585. #endif
  43586. EVP_PKEY_CTX_free(ctx);
  43587. EVP_PKEY_free(pkey);
  43588. #endif
  43589. return EXPECT_RESULT();
  43590. }
  43591. static int test_wolfSSL_EVP_PKEY_keygen(void)
  43592. {
  43593. EXPECT_DECLS;
  43594. WOLFSSL_EVP_PKEY* pkey = NULL;
  43595. EVP_PKEY_CTX* ctx = NULL;
  43596. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  43597. WOLFSSL_EVP_PKEY* params = NULL;
  43598. DH* dh = NULL;
  43599. const BIGNUM* pubkey = NULL;
  43600. const BIGNUM* privkey = NULL;
  43601. ASN1_INTEGER* asn1int = NULL;
  43602. unsigned int length = 0;
  43603. byte* derBuffer = NULL;
  43604. #endif
  43605. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43606. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43607. /* Bad cases */
  43608. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  43609. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  43610. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  43611. /* Good case */
  43612. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  43613. EVP_PKEY_CTX_free(ctx);
  43614. ctx = NULL;
  43615. EVP_PKEY_free(pkey);
  43616. pkey = NULL;
  43617. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  43618. /* Test DH keygen */
  43619. {
  43620. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  43621. ExpectNotNull(dh = DH_get_2048_256());
  43622. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  43623. ExpectNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  43624. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  43625. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  43626. DH_free(dh);
  43627. dh = NULL;
  43628. EVP_PKEY_CTX_free(ctx);
  43629. EVP_PKEY_free(params);
  43630. /* try exporting generated key to DER, to verify */
  43631. ExpectNotNull(dh = EVP_PKEY_get1_DH(pkey));
  43632. DH_get0_key(dh, &pubkey, &privkey);
  43633. ExpectNotNull(pubkey);
  43634. ExpectNotNull(privkey);
  43635. ExpectNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  43636. ExpectIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  43637. ASN1_INTEGER_free(asn1int);
  43638. DH_free(dh);
  43639. dh = NULL;
  43640. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43641. EVP_PKEY_free(pkey);
  43642. }
  43643. #endif
  43644. return EXPECT_RESULT();
  43645. }
  43646. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  43647. {
  43648. EXPECT_DECLS;
  43649. WOLFSSL_EVP_PKEY* pkey = NULL;
  43650. EVP_PKEY_CTX *ctx = NULL;
  43651. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43652. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43653. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  43654. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(NULL), WOLFSSL_SUCCESS);
  43655. EVP_PKEY_CTX_free(ctx);
  43656. EVP_PKEY_free(pkey);
  43657. return EXPECT_RESULT();
  43658. }
  43659. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  43660. {
  43661. EXPECT_DECLS;
  43662. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  43663. WOLFSSL_EVP_PKEY* pkey = NULL;
  43664. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43665. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  43666. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(NULL), 0);
  43667. EVP_PKEY_free(pkey);
  43668. #endif
  43669. return EXPECT_RESULT();
  43670. }
  43671. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  43672. {
  43673. EXPECT_DECLS;
  43674. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  43675. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  43676. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  43677. !defined(NO_FILESYSTEM)
  43678. WOLFSSL_EVP_PKEY* params = NULL;
  43679. WOLFSSL_EVP_PKEY* copy = NULL;
  43680. DH* dh = NULL;
  43681. BIGNUM* p1;
  43682. BIGNUM* g1;
  43683. BIGNUM* q1;
  43684. BIGNUM* p2;
  43685. BIGNUM* g2;
  43686. BIGNUM* q2;
  43687. /* create DH with DH_get_2048_256 params */
  43688. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  43689. ExpectNotNull(dh = DH_get_2048_256());
  43690. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  43691. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  43692. (const BIGNUM**)&q1,
  43693. (const BIGNUM**)&g1);
  43694. DH_free(dh);
  43695. dh = NULL;
  43696. /* create DH with random generated DH params */
  43697. ExpectNotNull(copy = wolfSSL_EVP_PKEY_new());
  43698. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  43699. ExpectIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  43700. DH_free(dh);
  43701. dh = NULL;
  43702. ExpectIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  43703. ExpectNotNull(dh = EVP_PKEY_get1_DH(copy));
  43704. ExpectNotNull(dh->p);
  43705. ExpectNotNull(dh->g);
  43706. ExpectNotNull(dh->q);
  43707. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  43708. (const BIGNUM**)&q2,
  43709. (const BIGNUM**)&g2);
  43710. ExpectIntEQ(BN_cmp(p1, p2), 0);
  43711. ExpectIntEQ(BN_cmp(q1, q2), 0);
  43712. ExpectIntEQ(BN_cmp(g1, g2), 0);
  43713. DH_free(dh);
  43714. dh = NULL;
  43715. EVP_PKEY_free(copy);
  43716. EVP_PKEY_free(params);
  43717. #endif
  43718. return EXPECT_RESULT();
  43719. }
  43720. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  43721. {
  43722. EXPECT_DECLS;
  43723. WOLFSSL_EVP_PKEY* pkey = NULL;
  43724. EVP_PKEY_CTX* ctx = NULL;
  43725. int bits = 2048;
  43726. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43727. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43728. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  43729. WOLFSSL_SUCCESS);
  43730. EVP_PKEY_CTX_free(ctx);
  43731. EVP_PKEY_free(pkey);
  43732. return EXPECT_RESULT();
  43733. }
  43734. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  43735. {
  43736. EXPECT_DECLS;
  43737. /* This is large enough to be used for all key sizes */
  43738. byte key[AES_256_KEY_SIZE] = {0};
  43739. byte iv[AES_BLOCK_SIZE] = {0};
  43740. int i;
  43741. int nids[] = {
  43742. #ifdef HAVE_AES_CBC
  43743. NID_aes_128_cbc,
  43744. #endif
  43745. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43746. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43747. #ifdef HAVE_AESGCM
  43748. NID_aes_128_gcm,
  43749. #endif
  43750. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43751. #ifdef WOLFSSL_AES_COUNTER
  43752. NID_aes_128_ctr,
  43753. #endif
  43754. #ifndef NO_DES3
  43755. NID_des_cbc,
  43756. NID_des_ede3_cbc,
  43757. #endif
  43758. };
  43759. int iv_lengths[] = {
  43760. #ifdef HAVE_AES_CBC
  43761. AES_BLOCK_SIZE,
  43762. #endif
  43763. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43764. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43765. #ifdef HAVE_AESGCM
  43766. GCM_NONCE_MID_SZ,
  43767. #endif
  43768. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43769. #ifdef WOLFSSL_AES_COUNTER
  43770. AES_BLOCK_SIZE,
  43771. #endif
  43772. #ifndef NO_DES3
  43773. DES_BLOCK_SIZE,
  43774. DES_BLOCK_SIZE,
  43775. #endif
  43776. };
  43777. int nidsLen = (sizeof(nids)/sizeof(int));
  43778. for (i = 0; i < nidsLen; i++) {
  43779. const EVP_CIPHER* init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  43780. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  43781. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43782. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43783. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  43784. EVP_CIPHER_CTX_free(ctx);
  43785. }
  43786. return EXPECT_RESULT();
  43787. }
  43788. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  43789. {
  43790. EXPECT_DECLS;
  43791. byte key[AES_256_KEY_SIZE] = {0};
  43792. byte iv[AES_BLOCK_SIZE] = {0};
  43793. int i;
  43794. int nids[] = {
  43795. #ifdef HAVE_AES_CBC
  43796. NID_aes_128_cbc,
  43797. NID_aes_256_cbc,
  43798. #endif
  43799. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43800. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43801. #ifdef HAVE_AESGCM
  43802. NID_aes_128_gcm,
  43803. NID_aes_256_gcm,
  43804. #endif
  43805. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43806. #ifdef WOLFSSL_AES_COUNTER
  43807. NID_aes_128_ctr,
  43808. NID_aes_256_ctr,
  43809. #endif
  43810. #ifndef NO_DES3
  43811. NID_des_cbc,
  43812. NID_des_ede3_cbc,
  43813. #endif
  43814. };
  43815. int key_lengths[] = {
  43816. #ifdef HAVE_AES_CBC
  43817. AES_128_KEY_SIZE,
  43818. AES_256_KEY_SIZE,
  43819. #endif
  43820. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43821. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43822. #ifdef HAVE_AESGCM
  43823. AES_128_KEY_SIZE,
  43824. AES_256_KEY_SIZE,
  43825. #endif
  43826. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43827. #ifdef WOLFSSL_AES_COUNTER
  43828. AES_128_KEY_SIZE,
  43829. AES_256_KEY_SIZE,
  43830. #endif
  43831. #ifndef NO_DES3
  43832. DES_KEY_SIZE,
  43833. DES3_KEY_SIZE,
  43834. #endif
  43835. };
  43836. int nidsLen = (sizeof(nids)/sizeof(int));
  43837. for (i = 0; i < nidsLen; i++) {
  43838. const EVP_CIPHER *init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  43839. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  43840. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43841. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43842. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), key_lengths[i]);
  43843. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, key_lengths[i]),
  43844. WOLFSSL_SUCCESS);
  43845. EVP_CIPHER_CTX_free(ctx);
  43846. }
  43847. return EXPECT_RESULT();
  43848. }
  43849. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  43850. {
  43851. EXPECT_DECLS;
  43852. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  43853. int ivLen, keyLen;
  43854. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  43855. #ifdef HAVE_AESGCM
  43856. byte key[AES_128_KEY_SIZE] = {0};
  43857. byte iv[AES_BLOCK_SIZE] = {0};
  43858. const EVP_CIPHER *init = EVP_aes_128_gcm();
  43859. #else
  43860. byte key[DES3_KEY_SIZE] = {0};
  43861. byte iv[DES_BLOCK_SIZE] = {0};
  43862. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  43863. #endif
  43864. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43865. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43866. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  43867. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  43868. /* Bad cases */
  43869. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen),
  43870. WOLFSSL_FAILURE);
  43871. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen),
  43872. WOLFSSL_FAILURE);
  43873. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  43874. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  43875. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen),
  43876. WOLFSSL_FAILURE);
  43877. /* Good case */
  43878. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  43879. EVP_CIPHER_CTX_free(ctx);
  43880. #endif
  43881. return EXPECT_RESULT();
  43882. }
  43883. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  43884. {
  43885. EXPECT_DECLS;
  43886. WOLFSSL_ENGINE* e = NULL;
  43887. int id = 0;
  43888. EVP_PKEY_CTX *ctx = NULL;
  43889. ExpectNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  43890. EVP_PKEY_CTX_free(ctx);
  43891. return EXPECT_RESULT();
  43892. }
  43893. static int test_wolfSSL_EVP_rc4(void)
  43894. {
  43895. EXPECT_DECLS;
  43896. #if !defined(NO_RC4)
  43897. ExpectNotNull(wolfSSL_EVP_rc4());
  43898. #endif
  43899. return EXPECT_RESULT();
  43900. }
  43901. static int test_wolfSSL_EVP_enc_null(void)
  43902. {
  43903. EXPECT_DECLS;
  43904. ExpectNotNull(wolfSSL_EVP_enc_null());
  43905. return EXPECT_RESULT();
  43906. }
  43907. static int test_wolfSSL_EVP_rc2_cbc(void)
  43908. {
  43909. EXPECT_DECLS;
  43910. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  43911. ExpectNull(wolfSSL_EVP_rc2_cbc());
  43912. #endif
  43913. return EXPECT_RESULT();
  43914. }
  43915. static int test_wolfSSL_EVP_mdc2(void)
  43916. {
  43917. EXPECT_DECLS;
  43918. #if !defined(NO_WOLFSSL_STUB)
  43919. ExpectNull(wolfSSL_EVP_mdc2());
  43920. #endif
  43921. return EXPECT_RESULT();
  43922. }
  43923. static int test_wolfSSL_EVP_md4(void)
  43924. {
  43925. EXPECT_DECLS;
  43926. #if !defined(NO_MD4)
  43927. ExpectNotNull(wolfSSL_EVP_md4());
  43928. #endif
  43929. return EXPECT_RESULT();
  43930. }
  43931. static int test_wolfSSL_EVP_aes_256_gcm(void)
  43932. {
  43933. EXPECT_DECLS;
  43934. #ifdef HAVE_AESGCM
  43935. ExpectNotNull(wolfSSL_EVP_aes_256_gcm());
  43936. #endif
  43937. return EXPECT_RESULT();
  43938. }
  43939. static int test_wolfSSL_EVP_aes_192_gcm(void)
  43940. {
  43941. EXPECT_DECLS;
  43942. #ifdef HAVE_AESGCM
  43943. ExpectNotNull(wolfSSL_EVP_aes_192_gcm());
  43944. #endif
  43945. return EXPECT_RESULT();
  43946. }
  43947. static int test_wolfSSL_EVP_aes_256_ccm(void)
  43948. {
  43949. EXPECT_DECLS;
  43950. #ifdef HAVE_AESCCM
  43951. ExpectNotNull(wolfSSL_EVP_aes_256_ccm());
  43952. #endif
  43953. return EXPECT_RESULT();
  43954. }
  43955. static int test_wolfSSL_EVP_aes_192_ccm(void)
  43956. {
  43957. EXPECT_DECLS;
  43958. #ifdef HAVE_AESCCM
  43959. ExpectNotNull(wolfSSL_EVP_aes_192_ccm());
  43960. #endif
  43961. return EXPECT_RESULT();
  43962. }
  43963. static int test_wolfSSL_EVP_aes_128_ccm(void)
  43964. {
  43965. EXPECT_DECLS;
  43966. #ifdef HAVE_AESCCM
  43967. ExpectNotNull(wolfSSL_EVP_aes_128_ccm());
  43968. #endif
  43969. return EXPECT_RESULT();
  43970. }
  43971. static int test_wolfSSL_EVP_ripemd160(void)
  43972. {
  43973. EXPECT_DECLS;
  43974. #if !defined(NO_WOLFSSL_STUB)
  43975. ExpectNull(wolfSSL_EVP_ripemd160());
  43976. #endif
  43977. return EXPECT_RESULT();
  43978. }
  43979. static int test_wolfSSL_EVP_get_digestbynid(void)
  43980. {
  43981. EXPECT_DECLS;
  43982. #ifndef NO_MD5
  43983. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  43984. #endif
  43985. #ifndef NO_SHA
  43986. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  43987. #endif
  43988. #ifndef NO_SHA256
  43989. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha256));
  43990. #endif
  43991. ExpectNull(wolfSSL_EVP_get_digestbynid(0));
  43992. return EXPECT_RESULT();
  43993. }
  43994. static int test_wolfSSL_EVP_MD_nid(void)
  43995. {
  43996. EXPECT_DECLS;
  43997. #ifndef NO_MD5
  43998. ExpectIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  43999. #endif
  44000. #ifndef NO_SHA
  44001. ExpectIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  44002. #endif
  44003. #ifndef NO_SHA256
  44004. ExpectIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  44005. #endif
  44006. ExpectIntEQ(EVP_MD_nid(NULL), NID_undef);
  44007. return EXPECT_RESULT();
  44008. }
  44009. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  44010. {
  44011. EXPECT_DECLS;
  44012. #if defined(HAVE_ECC)
  44013. WOLFSSL_EVP_PKEY* pkey = NULL;
  44014. ExpectNull(EVP_PKEY_get0_EC_KEY(NULL));
  44015. ExpectNotNull(pkey = EVP_PKEY_new());
  44016. ExpectNull(EVP_PKEY_get0_EC_KEY(pkey));
  44017. EVP_PKEY_free(pkey);
  44018. #endif
  44019. return EXPECT_RESULT();
  44020. }
  44021. static int test_wolfSSL_EVP_X_STATE(void)
  44022. {
  44023. EXPECT_DECLS;
  44024. #if !defined(NO_DES3) && !defined(NO_RC4)
  44025. byte key[DES3_KEY_SIZE] = {0};
  44026. byte iv[DES_IV_SIZE] = {0};
  44027. EVP_CIPHER_CTX *ctx = NULL;
  44028. const EVP_CIPHER *init = NULL;
  44029. /* Bad test cases */
  44030. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44031. ExpectNotNull(init = EVP_des_ede3_cbc());
  44032. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44033. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44034. ExpectNull(wolfSSL_EVP_X_STATE(NULL));
  44035. ExpectNull(wolfSSL_EVP_X_STATE(ctx));
  44036. EVP_CIPHER_CTX_free(ctx);
  44037. ctx = NULL;
  44038. /* Good test case */
  44039. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44040. ExpectNotNull(init = wolfSSL_EVP_rc4());
  44041. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44042. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44043. ExpectNotNull(wolfSSL_EVP_X_STATE(ctx));
  44044. EVP_CIPHER_CTX_free(ctx);
  44045. #endif
  44046. return EXPECT_RESULT();
  44047. }
  44048. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  44049. {
  44050. EXPECT_DECLS;
  44051. #if !defined(NO_DES3) && !defined(NO_RC4)
  44052. byte key[DES3_KEY_SIZE] = {0};
  44053. byte iv[DES_IV_SIZE] = {0};
  44054. EVP_CIPHER_CTX *ctx = NULL;
  44055. const EVP_CIPHER *init = NULL;
  44056. /* Bad test cases */
  44057. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44058. ExpectNotNull(init = EVP_des_ede3_cbc());
  44059. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44060. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44061. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  44062. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  44063. EVP_CIPHER_CTX_free(ctx);
  44064. ctx = NULL;
  44065. /* Good test case */
  44066. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44067. ExpectNotNull(init = wolfSSL_EVP_rc4());
  44068. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44069. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44070. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  44071. EVP_CIPHER_CTX_free(ctx);
  44072. #endif
  44073. return EXPECT_RESULT();
  44074. }
  44075. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  44076. {
  44077. EXPECT_DECLS;
  44078. #if defined(HAVE_AES_CBC) || defined(HAVE_AESGCM) || \
  44079. defined(WOLFSSL_AES_COUNTER) || defined(HAVE_AES_ECB) || \
  44080. defined(WOLFSSL_AES_OFB) || !defined(NO_RC4) || \
  44081. (defined(HAVE_CHACHA) && defined(HAVE_POLY1305))
  44082. #ifdef HAVE_AES_CBC
  44083. #ifdef WOLFSSL_AES_128
  44084. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  44085. #endif
  44086. #ifdef WOLFSSL_AES_192
  44087. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  44088. #endif
  44089. #ifdef WOLFSSL_AES_256
  44090. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  44091. #endif
  44092. #endif
  44093. #ifdef HAVE_AESGCM
  44094. #ifdef WOLFSSL_AES_128
  44095. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  44096. #endif
  44097. #ifdef WOLFSSL_AES_192
  44098. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  44099. #endif
  44100. #ifdef WOLFSSL_AES_256
  44101. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  44102. #endif
  44103. #endif
  44104. #ifdef HAVE_AESCCM
  44105. #ifdef WOLFSSL_AES_128
  44106. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ccm()), 1);
  44107. #endif
  44108. #ifdef WOLFSSL_AES_192
  44109. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ccm()), 1);
  44110. #endif
  44111. #ifdef WOLFSSL_AES_256
  44112. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ccm()), 1);
  44113. #endif
  44114. #endif
  44115. #ifdef WOLFSSL_AES_COUNTER
  44116. #ifdef WOLFSSL_AES_128
  44117. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  44118. #endif
  44119. #ifdef WOLFSSL_AES_192
  44120. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  44121. #endif
  44122. #ifdef WOLFSSL_AES_256
  44123. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  44124. #endif
  44125. #endif
  44126. #ifdef HAVE_AES_ECB
  44127. #ifdef WOLFSSL_AES_128
  44128. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  44129. #endif
  44130. #ifdef WOLFSSL_AES_192
  44131. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  44132. #endif
  44133. #ifdef WOLFSSL_AES_256
  44134. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  44135. #endif
  44136. #endif
  44137. #ifdef WOLFSSL_AES_OFB
  44138. #ifdef WOLFSSL_AES_128
  44139. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  44140. #endif
  44141. #ifdef WOLFSSL_AES_192
  44142. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  44143. #endif
  44144. #ifdef WOLFSSL_AES_256
  44145. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  44146. #endif
  44147. #endif
  44148. #ifndef NO_RC4
  44149. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  44150. #endif
  44151. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  44152. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  44153. #endif
  44154. #endif
  44155. #ifdef WOLFSSL_SM4_ECB
  44156. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ecb()), SM4_BLOCK_SIZE);
  44157. #endif
  44158. #ifdef WOLFSSL_SM4_CBC
  44159. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_cbc()), SM4_BLOCK_SIZE);
  44160. #endif
  44161. #ifdef WOLFSSL_SM4_CTR
  44162. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ctr()), 1);
  44163. #endif
  44164. #ifdef WOLFSSL_SM4_GCM
  44165. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_gcm()), 1);
  44166. #endif
  44167. #ifdef WOLFSSL_SM4_CCM
  44168. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ccm()), 1);
  44169. #endif
  44170. return EXPECT_RESULT();
  44171. }
  44172. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  44173. {
  44174. EXPECT_DECLS;
  44175. int nids[] = {
  44176. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  44177. #ifdef WOLFSSL_AES_128
  44178. NID_aes_128_cbc,
  44179. #endif
  44180. #ifdef WOLFSSL_AES_192
  44181. NID_aes_192_cbc,
  44182. #endif
  44183. #ifdef WOLFSSL_AES_256
  44184. NID_aes_256_cbc,
  44185. #endif
  44186. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  44187. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  44188. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  44189. #ifdef HAVE_AESGCM
  44190. #ifdef WOLFSSL_AES_128
  44191. NID_aes_128_gcm,
  44192. #endif
  44193. #ifdef WOLFSSL_AES_192
  44194. NID_aes_192_gcm,
  44195. #endif
  44196. #ifdef WOLFSSL_AES_256
  44197. NID_aes_256_gcm,
  44198. #endif
  44199. #endif /* HAVE_AESGCM */
  44200. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  44201. #ifdef WOLFSSL_AES_COUNTER
  44202. #ifdef WOLFSSL_AES_128
  44203. NID_aes_128_ctr,
  44204. #endif
  44205. #ifdef WOLFSSL_AES_192
  44206. NID_aes_192_ctr,
  44207. #endif
  44208. #ifdef WOLFSSL_AES_256
  44209. NID_aes_256_ctr,
  44210. #endif
  44211. #endif
  44212. #ifndef NO_DES3
  44213. NID_des_cbc,
  44214. NID_des_ede3_cbc,
  44215. #endif
  44216. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  44217. NID_chacha20_poly1305,
  44218. #endif
  44219. };
  44220. int iv_lengths[] = {
  44221. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  44222. #ifdef WOLFSSL_AES_128
  44223. AES_BLOCK_SIZE,
  44224. #endif
  44225. #ifdef WOLFSSL_AES_192
  44226. AES_BLOCK_SIZE,
  44227. #endif
  44228. #ifdef WOLFSSL_AES_256
  44229. AES_BLOCK_SIZE,
  44230. #endif
  44231. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  44232. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  44233. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  44234. #ifdef HAVE_AESGCM
  44235. #ifdef WOLFSSL_AES_128
  44236. GCM_NONCE_MID_SZ,
  44237. #endif
  44238. #ifdef WOLFSSL_AES_192
  44239. GCM_NONCE_MID_SZ,
  44240. #endif
  44241. #ifdef WOLFSSL_AES_256
  44242. GCM_NONCE_MID_SZ,
  44243. #endif
  44244. #endif /* HAVE_AESGCM */
  44245. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  44246. #ifdef WOLFSSL_AES_COUNTER
  44247. #ifdef WOLFSSL_AES_128
  44248. AES_BLOCK_SIZE,
  44249. #endif
  44250. #ifdef WOLFSSL_AES_192
  44251. AES_BLOCK_SIZE,
  44252. #endif
  44253. #ifdef WOLFSSL_AES_256
  44254. AES_BLOCK_SIZE,
  44255. #endif
  44256. #endif
  44257. #ifndef NO_DES3
  44258. DES_BLOCK_SIZE,
  44259. DES_BLOCK_SIZE,
  44260. #endif
  44261. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  44262. CHACHA20_POLY1305_AEAD_IV_SIZE,
  44263. #endif
  44264. };
  44265. int i;
  44266. int nidsLen = (sizeof(nids)/sizeof(int));
  44267. for (i = 0; i < nidsLen; i++) {
  44268. const EVP_CIPHER *c = EVP_get_cipherbynid(nids[i]);
  44269. ExpectIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  44270. }
  44271. return EXPECT_RESULT();
  44272. }
  44273. static int test_wolfSSL_EVP_SignInit_ex(void)
  44274. {
  44275. EXPECT_DECLS;
  44276. WOLFSSL_EVP_MD_CTX mdCtx;
  44277. WOLFSSL_ENGINE* e = 0;
  44278. const EVP_MD* md = EVP_sha256();
  44279. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  44280. ExpectIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  44281. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  44282. return EXPECT_RESULT();
  44283. }
  44284. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  44285. {
  44286. EXPECT_DECLS;
  44287. #if !defined(NO_SHA256)
  44288. WOLFSSL_EVP_MD_CTX mdCtx;
  44289. unsigned int s = 0;
  44290. unsigned char md[WC_SHA256_DIGEST_SIZE];
  44291. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  44292. /* Bad Case */
  44293. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  44294. (HAVE_FIPS_VERSION > 2))
  44295. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  44296. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  44297. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  44298. #else
  44299. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  44300. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  44301. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  44302. #endif
  44303. /* Good Case */
  44304. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  44305. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, EVP_sha256()), WOLFSSL_SUCCESS);
  44306. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  44307. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  44308. #endif
  44309. return EXPECT_RESULT();
  44310. }
  44311. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  44312. {
  44313. EXPECT_DECLS;
  44314. #if defined(OPENSSL_EXTRA)
  44315. EVP_PKEY* pkey = NULL;
  44316. EVP_PKEY_CTX* ctx = NULL;
  44317. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44318. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44319. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  44320. /* void */
  44321. EVP_PKEY_CTX_free(ctx);
  44322. #else
  44323. /* int */
  44324. ExpectIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  44325. #endif
  44326. EVP_PKEY_free(pkey);
  44327. #endif
  44328. return EXPECT_RESULT();
  44329. }
  44330. static int test_wolfSSL_EVP_PKEY_param_check(void)
  44331. {
  44332. EXPECT_DECLS;
  44333. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  44334. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  44335. DH *dh = NULL;
  44336. DH *setDh = NULL;
  44337. EVP_PKEY *pkey = NULL;
  44338. EVP_PKEY_CTX* ctx = NULL;
  44339. FILE* f = NULL;
  44340. unsigned char buf[512];
  44341. const unsigned char* pt = buf;
  44342. const char* dh2048 = "./certs/dh2048.der";
  44343. long len = 0;
  44344. int code = -1;
  44345. XMEMSET(buf, 0, sizeof(buf));
  44346. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  44347. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  44348. if (f != XBADFILE)
  44349. XFCLOSE(f);
  44350. /* Load dh2048.der into DH with internal format */
  44351. ExpectNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  44352. ExpectIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  44353. ExpectIntEQ(code, 0);
  44354. code = -1;
  44355. pkey = wolfSSL_EVP_PKEY_new();
  44356. /* Set DH into PKEY */
  44357. ExpectIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  44358. /* create ctx from pkey */
  44359. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44360. ExpectIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  44361. /* TODO: more invalid cases */
  44362. ExpectIntEQ(EVP_PKEY_param_check(NULL), 0);
  44363. EVP_PKEY_CTX_free(ctx);
  44364. EVP_PKEY_free(pkey);
  44365. DH_free(setDh);
  44366. setDh = NULL;
  44367. DH_free(dh);
  44368. dh = NULL;
  44369. #endif
  44370. #endif
  44371. return EXPECT_RESULT();
  44372. }
  44373. static int test_wolfSSL_EVP_BytesToKey(void)
  44374. {
  44375. EXPECT_DECLS;
  44376. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  44377. byte key[AES_BLOCK_SIZE] = {0};
  44378. byte iv[AES_BLOCK_SIZE] = {0};
  44379. int count = 0;
  44380. const EVP_MD* md = EVP_sha256();
  44381. const EVP_CIPHER *type;
  44382. const unsigned char *salt = (unsigned char *)"salt1234";
  44383. int sz = 5;
  44384. const byte data[] = {
  44385. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  44386. 0x72,0x6c,0x64
  44387. };
  44388. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  44389. /* Bad cases */
  44390. ExpectIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  44391. 0);
  44392. ExpectIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  44393. 16);
  44394. md = "2";
  44395. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  44396. WOLFSSL_FAILURE);
  44397. /* Good case */
  44398. md = EVP_sha256();
  44399. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  44400. 16);
  44401. #endif
  44402. return EXPECT_RESULT();
  44403. }
  44404. static int test_evp_cipher_aes_gcm(void)
  44405. {
  44406. EXPECT_DECLS;
  44407. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  44408. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  44409. (HAVE_FIPS_VERSION >= 2)))
  44410. /*
  44411. * This test checks data at various points in the encrypt/decrypt process
  44412. * against known values produced using the same test with OpenSSL. This
  44413. * interop testing is critical for verifying the correctness of our
  44414. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  44415. * a flow supported by OpenSSL that uses the control command
  44416. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  44417. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  44418. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  44419. * wolfSSL OpenSSH clients because there was a bug in this flow that
  44420. * happened to "cancel out" if both sides of the connection had the bug.
  44421. */
  44422. enum {
  44423. NUM_ENCRYPTIONS = 3,
  44424. AAD_SIZE = 4
  44425. };
  44426. byte plainText1[] = {
  44427. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  44428. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  44429. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  44430. };
  44431. byte plainText2[] = {
  44432. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  44433. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  44434. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  44435. };
  44436. byte plainText3[] = {
  44437. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  44438. 0x5b, 0x57, 0x10
  44439. };
  44440. byte* plainTexts[NUM_ENCRYPTIONS] = {
  44441. plainText1,
  44442. plainText2,
  44443. plainText3
  44444. };
  44445. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  44446. sizeof(plainText1),
  44447. sizeof(plainText2),
  44448. sizeof(plainText3)
  44449. };
  44450. byte aad1[AAD_SIZE] = {
  44451. 0x00, 0x00, 0x00, 0x01
  44452. };
  44453. byte aad2[AAD_SIZE] = {
  44454. 0x00, 0x00, 0x00, 0x10
  44455. };
  44456. byte aad3[AAD_SIZE] = {
  44457. 0x00, 0x00, 0x01, 0x00
  44458. };
  44459. byte* aads[NUM_ENCRYPTIONS] = {
  44460. aad1,
  44461. aad2,
  44462. aad3
  44463. };
  44464. const byte iv[GCM_NONCE_MID_SZ] = {
  44465. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  44466. };
  44467. byte currentIv[GCM_NONCE_MID_SZ];
  44468. const byte key[] = {
  44469. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  44470. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  44471. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  44472. };
  44473. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  44474. {
  44475. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  44476. 0xEF
  44477. },
  44478. {
  44479. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  44480. 0xF0
  44481. },
  44482. {
  44483. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  44484. 0xF1
  44485. }
  44486. };
  44487. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  44488. {
  44489. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  44490. 0x3D, 0x32, 0x18, 0x34, 0xA9
  44491. },
  44492. {
  44493. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  44494. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  44495. },
  44496. {
  44497. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  44498. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  44499. }
  44500. };
  44501. const byte expCipherText1[] = {
  44502. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  44503. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  44504. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  44505. };
  44506. const byte expCipherText2[] = {
  44507. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  44508. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  44509. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  44510. };
  44511. const byte expCipherText3[] = {
  44512. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  44513. 0x80, 0x5E, 0x6B,
  44514. };
  44515. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  44516. expCipherText1,
  44517. expCipherText2,
  44518. expCipherText3
  44519. };
  44520. byte* cipherText = NULL;
  44521. byte* calcPlainText = NULL;
  44522. byte tag[AES_BLOCK_SIZE];
  44523. EVP_CIPHER_CTX* encCtx = NULL;
  44524. EVP_CIPHER_CTX* decCtx = NULL;
  44525. int i, j, outl;
  44526. /****************************************************/
  44527. for (i = 0; i < 3; ++i) {
  44528. ExpectNotNull(encCtx = EVP_CIPHER_CTX_new());
  44529. ExpectNotNull(decCtx = EVP_CIPHER_CTX_new());
  44530. /* First iteration, set key before IV. */
  44531. if (i == 0) {
  44532. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  44533. SSL_SUCCESS);
  44534. /*
  44535. * The call to EVP_CipherInit below (with NULL key) should clear the
  44536. * authIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  44537. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  44538. * behavior.
  44539. */
  44540. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  44541. (void*)iv), SSL_SUCCESS);
  44542. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  44543. SSL_SUCCESS);
  44544. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  44545. currentIv), SSL_FAILURE);
  44546. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  44547. SSL_SUCCESS);
  44548. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  44549. SSL_SUCCESS);
  44550. }
  44551. /* Second iteration, IV before key. */
  44552. else {
  44553. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  44554. SSL_SUCCESS);
  44555. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  44556. SSL_SUCCESS);
  44557. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  44558. SSL_SUCCESS);
  44559. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  44560. SSL_SUCCESS);
  44561. }
  44562. /*
  44563. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  44564. * been issued first.
  44565. */
  44566. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  44567. currentIv), SSL_FAILURE);
  44568. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  44569. (void*)iv), SSL_SUCCESS);
  44570. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  44571. (void*)iv), SSL_SUCCESS);
  44572. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  44573. /*************** Encrypt ***************/
  44574. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  44575. currentIv), SSL_SUCCESS);
  44576. /* Check current IV against expected. */
  44577. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  44578. /* Add AAD. */
  44579. if (i == 2) {
  44580. /* Test streaming API. */
  44581. ExpectIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  44582. AAD_SIZE), SSL_SUCCESS);
  44583. }
  44584. else {
  44585. ExpectIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  44586. AAD_SIZE);
  44587. }
  44588. ExpectNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  44589. DYNAMIC_TYPE_TMP_BUFFER));
  44590. /* Encrypt plaintext. */
  44591. if (i == 2) {
  44592. ExpectIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  44593. plainTexts[j], plainTextSzs[j]),
  44594. SSL_SUCCESS);
  44595. }
  44596. else {
  44597. ExpectIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  44598. plainTextSzs[j]), plainTextSzs[j]);
  44599. }
  44600. if (i == 2) {
  44601. ExpectIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  44602. SSL_SUCCESS);
  44603. }
  44604. else {
  44605. /*
  44606. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  44607. * akin to calling EVP_CipherFinal.
  44608. */
  44609. ExpectIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  44610. }
  44611. /* Check ciphertext against expected. */
  44612. ExpectIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  44613. 0);
  44614. /* Get and check tag against expected. */
  44615. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  44616. sizeof(tag), tag), SSL_SUCCESS);
  44617. ExpectIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  44618. /*************** Decrypt ***************/
  44619. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  44620. currentIv), SSL_SUCCESS);
  44621. /* Check current IV against expected. */
  44622. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  44623. /* Add AAD. */
  44624. if (i == 2) {
  44625. /* Test streaming API. */
  44626. ExpectIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  44627. AAD_SIZE), SSL_SUCCESS);
  44628. }
  44629. else {
  44630. ExpectIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  44631. AAD_SIZE);
  44632. }
  44633. /* Set expected tag. */
  44634. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  44635. sizeof(tag), tag), SSL_SUCCESS);
  44636. /* Decrypt ciphertext. */
  44637. ExpectNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  44638. DYNAMIC_TYPE_TMP_BUFFER));
  44639. if (i == 2) {
  44640. ExpectIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  44641. cipherText, plainTextSzs[j]),
  44642. SSL_SUCCESS);
  44643. }
  44644. else {
  44645. /* This first EVP_Cipher call will check the tag, too. */
  44646. ExpectIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  44647. plainTextSzs[j]), plainTextSzs[j]);
  44648. }
  44649. if (i == 2) {
  44650. ExpectIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  44651. SSL_SUCCESS);
  44652. }
  44653. else {
  44654. ExpectIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  44655. }
  44656. /* Check plaintext against expected. */
  44657. ExpectIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  44658. 0);
  44659. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44660. cipherText = NULL;
  44661. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44662. calcPlainText = NULL;
  44663. }
  44664. EVP_CIPHER_CTX_free(encCtx);
  44665. encCtx = NULL;
  44666. EVP_CIPHER_CTX_free(decCtx);
  44667. decCtx = NULL;
  44668. }
  44669. #endif
  44670. return EXPECT_RESULT();
  44671. }
  44672. static int test_wolfSSL_OBJ_ln(void)
  44673. {
  44674. EXPECT_DECLS;
  44675. const int nid_set[] = {
  44676. NID_commonName,
  44677. NID_serialNumber,
  44678. NID_countryName,
  44679. NID_localityName,
  44680. NID_stateOrProvinceName,
  44681. NID_organizationName,
  44682. NID_organizationalUnitName,
  44683. NID_domainComponent,
  44684. NID_businessCategory,
  44685. NID_jurisdictionCountryName,
  44686. NID_jurisdictionStateOrProvinceName,
  44687. NID_emailAddress
  44688. };
  44689. const char* ln_set[] = {
  44690. "commonName",
  44691. "serialNumber",
  44692. "countryName",
  44693. "localityName",
  44694. "stateOrProvinceName",
  44695. "organizationName",
  44696. "organizationalUnitName",
  44697. "domainComponent",
  44698. "businessCategory",
  44699. "jurisdictionCountryName",
  44700. "jurisdictionStateOrProvinceName",
  44701. "emailAddress",
  44702. };
  44703. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  44704. ExpectIntEQ(OBJ_ln2nid(NULL), NID_undef);
  44705. #ifdef HAVE_ECC
  44706. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44707. {
  44708. EC_builtin_curve r[27];
  44709. size_t nCurves = sizeof(r) / sizeof(r[0]);
  44710. nCurves = EC_get_builtin_curves(r, nCurves);
  44711. for (i = 0; i < nCurves; i++) {
  44712. /* skip ECC_CURVE_INVALID */
  44713. if (r[i].nid != ECC_CURVE_INVALID) {
  44714. ExpectIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  44715. ExpectStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  44716. }
  44717. }
  44718. }
  44719. #endif
  44720. #endif
  44721. for (i = 0; i < maxIdx; i++) {
  44722. ExpectIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  44723. ExpectStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  44724. }
  44725. return EXPECT_RESULT();
  44726. }
  44727. static int test_wolfSSL_OBJ_sn(void)
  44728. {
  44729. EXPECT_DECLS;
  44730. int i = 0, maxIdx = 7;
  44731. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  44732. NID_stateOrProvinceName,NID_organizationName,
  44733. NID_organizationalUnitName,NID_emailAddress};
  44734. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  44735. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  44736. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  44737. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  44738. WOLFSSL_EMAIL_ADDR};
  44739. ExpectIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  44740. for (i = 0; i < maxIdx; i++) {
  44741. ExpectIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  44742. ExpectStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  44743. }
  44744. return EXPECT_RESULT();
  44745. }
  44746. #if !defined(NO_BIO)
  44747. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  44748. {
  44749. return lh_strhash(s[3]);
  44750. }
  44751. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  44752. {
  44753. return XSTRCMP(a[3], b[3]);
  44754. }
  44755. #endif
  44756. static int test_wolfSSL_TXT_DB(void)
  44757. {
  44758. EXPECT_DECLS;
  44759. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  44760. BIO *bio = NULL;
  44761. TXT_DB *db = NULL;
  44762. const int columns = 6;
  44763. const char *fields[6] = {
  44764. "V",
  44765. "320926161116Z",
  44766. "",
  44767. "12BD",
  44768. "unknown",
  44769. "/CN=rsa doe",
  44770. };
  44771. char** fields_copy = NULL;
  44772. /* Test read */
  44773. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  44774. ExpectIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  44775. ExpectNotNull(db = TXT_DB_read(bio, columns));
  44776. ExpectNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  44777. DYNAMIC_TYPE_OPENSSL));
  44778. if (fields_copy != NULL) {
  44779. XMEMCPY(fields_copy, fields, sizeof(fields));
  44780. }
  44781. ExpectIntEQ(TXT_DB_insert(db, fields_copy), 1);
  44782. if (EXPECT_FAIL()) {
  44783. XFREE(fields_copy, NULL, DYNAMIC_TYPE_OPENSSL);
  44784. }
  44785. BIO_free(bio);
  44786. bio = NULL;
  44787. /* Test write */
  44788. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  44789. ExpectIntEQ(TXT_DB_write(bio, db), 1484);
  44790. BIO_free(bio);
  44791. /* Test index */
  44792. ExpectIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  44793. (wolf_lh_compare_cb)TXT_DB_cmp), 1);
  44794. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44795. fields[3] = "12DA";
  44796. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44797. fields[3] = "FFFF";
  44798. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44799. fields[3] = "";
  44800. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44801. TXT_DB_free(db);
  44802. #endif
  44803. return EXPECT_RESULT();
  44804. }
  44805. static int test_wolfSSL_NCONF(void)
  44806. {
  44807. EXPECT_DECLS;
  44808. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  44809. const char* confFile = "./tests/NCONF_test.cnf";
  44810. CONF* conf = NULL;
  44811. long eline = 0;
  44812. long num = 0;
  44813. ExpectNotNull(conf = NCONF_new(NULL));
  44814. ExpectIntEQ(NCONF_load(conf, confFile, &eline), 1);
  44815. ExpectIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  44816. ExpectIntEQ(num, 1234);
  44817. ExpectIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  44818. ExpectIntEQ(num, 4321);
  44819. ExpectStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  44820. ExpectStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  44821. "./test-dir/file1");
  44822. ExpectStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  44823. "./test-dir/file1:./test-dir/file2:./section1:file2");
  44824. NCONF_free(conf);
  44825. #endif
  44826. return EXPECT_RESULT();
  44827. }
  44828. #endif /* OPENSSL_ALL */
  44829. static int test_wolfSSL_X509V3_EXT_get(void) {
  44830. EXPECT_DECLS;
  44831. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44832. XFILE f = XBADFILE;
  44833. int numOfExt =0;
  44834. int extNid = 0;
  44835. int i = 0;
  44836. WOLFSSL_X509* x509 = NULL;
  44837. WOLFSSL_X509_EXTENSION* ext = NULL;
  44838. const WOLFSSL_v3_ext_method* method = NULL;
  44839. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  44840. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  44841. if (f != XBADFILE)
  44842. XFCLOSE(f);
  44843. /* wolfSSL_X509V3_EXT_get() return struct and nid test */
  44844. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  44845. for (i = 0; i < numOfExt; i++) {
  44846. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44847. ExpectIntNE((extNid = ext->obj->nid), NID_undef);
  44848. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  44849. ExpectIntEQ(method->ext_nid, extNid);
  44850. }
  44851. /* wolfSSL_X509V3_EXT_get() NULL argument test */
  44852. ExpectNull(method = wolfSSL_X509V3_EXT_get(NULL));
  44853. wolfSSL_X509_free(x509);
  44854. #endif
  44855. return EXPECT_RESULT();
  44856. }
  44857. static int test_wolfSSL_X509V3_EXT_nconf(void)
  44858. {
  44859. EXPECT_DECLS;
  44860. #ifdef OPENSSL_ALL
  44861. const char *ext_names[] = {
  44862. "subjectKeyIdentifier",
  44863. "authorityKeyIdentifier",
  44864. "subjectAltName",
  44865. "keyUsage",
  44866. "extendedKeyUsage",
  44867. };
  44868. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  44869. int ext_nids[] = {
  44870. NID_subject_key_identifier,
  44871. NID_authority_key_identifier,
  44872. NID_subject_alt_name,
  44873. NID_key_usage,
  44874. NID_ext_key_usage,
  44875. };
  44876. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  44877. const char *ext_values[] = {
  44878. "hash",
  44879. "hash",
  44880. "DNS:example.com, IP:127.0.0.1",
  44881. "digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,"
  44882. "keyAgreement,keyCertSign,cRLSign,encipherOnly,decipherOnly",
  44883. "serverAuth,clientAuth,codeSigning,emailProtection,timeStamping,"
  44884. "OCSPSigning",
  44885. };
  44886. size_t i;
  44887. X509_EXTENSION* ext = NULL;
  44888. X509* x509 = NULL;
  44889. unsigned int keyUsageFlags;
  44890. unsigned int extKeyUsageFlags;
  44891. ExpectNotNull(x509 = X509_new());
  44892. /* keyUsage / extKeyUsage should match string above */
  44893. keyUsageFlags = KU_DIGITAL_SIGNATURE
  44894. | KU_NON_REPUDIATION
  44895. | KU_KEY_ENCIPHERMENT
  44896. | KU_DATA_ENCIPHERMENT
  44897. | KU_KEY_AGREEMENT
  44898. | KU_KEY_CERT_SIGN
  44899. | KU_CRL_SIGN
  44900. | KU_ENCIPHER_ONLY
  44901. | KU_DECIPHER_ONLY;
  44902. extKeyUsageFlags = XKU_SSL_CLIENT
  44903. | XKU_SSL_SERVER
  44904. | XKU_CODE_SIGN
  44905. | XKU_SMIME
  44906. | XKU_TIMESTAMP
  44907. | XKU_OCSP_SIGN;
  44908. for (i = 0; i < ext_names_count; i++) {
  44909. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  44910. ext_values[i]));
  44911. X509_EXTENSION_free(ext);
  44912. ext = NULL;
  44913. }
  44914. for (i = 0; i < ext_nids_count; i++) {
  44915. ExpectNotNull(ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i],
  44916. ext_values[i]));
  44917. X509_EXTENSION_free(ext);
  44918. ext = NULL;
  44919. }
  44920. /* Test adding extension to X509 */
  44921. for (i = 0; i < ext_nids_count; i++) {
  44922. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  44923. ext_values[i]));
  44924. ExpectIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  44925. if (ext_nids[i] == NID_key_usage) {
  44926. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  44927. }
  44928. else if (ext_nids[i] == NID_ext_key_usage) {
  44929. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  44930. }
  44931. X509_EXTENSION_free(ext);
  44932. ext = NULL;
  44933. }
  44934. X509_free(x509);
  44935. #endif
  44936. return EXPECT_RESULT();
  44937. }
  44938. static int test_wolfSSL_X509V3_EXT(void) {
  44939. EXPECT_DECLS;
  44940. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44941. XFILE f = XBADFILE;
  44942. int numOfExt = 0, nid = 0, i = 0, expected, actual = 0;
  44943. char* str = NULL;
  44944. unsigned char* data = NULL;
  44945. const WOLFSSL_v3_ext_method* method = NULL;
  44946. WOLFSSL_X509* x509 = NULL;
  44947. WOLFSSL_X509_EXTENSION* ext = NULL;
  44948. WOLFSSL_X509_EXTENSION* ext2 = NULL;
  44949. WOLFSSL_ASN1_OBJECT *obj = NULL;
  44950. WOLFSSL_ASN1_OBJECT *adObj = NULL;
  44951. WOLFSSL_ASN1_STRING* asn1str = NULL;
  44952. WOLFSSL_AUTHORITY_KEYID* aKeyId = NULL;
  44953. WOLFSSL_AUTHORITY_INFO_ACCESS* aia = NULL;
  44954. WOLFSSL_BASIC_CONSTRAINTS* bc = NULL;
  44955. WOLFSSL_ACCESS_DESCRIPTION* ad = NULL;
  44956. WOLFSSL_GENERAL_NAME* gn = NULL;
  44957. /* Check NULL argument */
  44958. ExpectNull(wolfSSL_X509V3_EXT_d2i(NULL));
  44959. /* Using OCSP cert with X509V3 extensions */
  44960. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  44961. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  44962. if (f != XBADFILE)
  44963. XFCLOSE(f);
  44964. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  44965. /* Basic Constraints */
  44966. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44967. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  44968. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  44969. ExpectNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  44970. ExpectIntEQ(bc->ca, 1);
  44971. ExpectNull(bc->pathlen);
  44972. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  44973. bc = NULL;
  44974. i++;
  44975. /* Subject Key Identifier */
  44976. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44977. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  44978. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  44979. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  44980. ExpectNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  44981. asn1str));
  44982. X509_EXTENSION_free(ext2);
  44983. ext2 = NULL;
  44984. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  44985. ExpectNotNull(method->i2s);
  44986. ExpectNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  44987. wolfSSL_ASN1_STRING_free(asn1str);
  44988. asn1str = NULL;
  44989. if (str != NULL) {
  44990. actual = strcmp(str,
  44991. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  44992. }
  44993. ExpectIntEQ(actual, 0);
  44994. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44995. str = NULL;
  44996. i++;
  44997. /* Authority Key Identifier */
  44998. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44999. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45000. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  45001. ExpectNotNull(aKeyId = (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(
  45002. ext));
  45003. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  45004. ExpectNotNull(asn1str = aKeyId->keyid);
  45005. ExpectNotNull(str = wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method,
  45006. asn1str));
  45007. asn1str = NULL;
  45008. if (str != NULL) {
  45009. actual = strcmp(str,
  45010. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  45011. }
  45012. ExpectIntEQ(actual, 0);
  45013. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45014. str = NULL;
  45015. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  45016. aKeyId = NULL;
  45017. i++;
  45018. /* Key Usage */
  45019. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45020. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45021. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  45022. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  45023. #if defined(WOLFSSL_QT)
  45024. ExpectNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  45025. #else
  45026. ExpectNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  45027. #endif
  45028. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  45029. if (data != NULL) {
  45030. #ifdef BIG_ENDIAN_ORDER
  45031. actual = data[1];
  45032. #else
  45033. actual = data[0];
  45034. #endif
  45035. }
  45036. ExpectIntEQ(actual, expected);
  45037. wolfSSL_ASN1_STRING_free(asn1str);
  45038. asn1str = NULL;
  45039. #if 1
  45040. i++;
  45041. /* Authority Info Access */
  45042. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45043. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45044. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  45045. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  45046. ext));
  45047. #if defined(WOLFSSL_QT)
  45048. ExpectIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  45049. #else
  45050. ExpectIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  45051. #endif
  45052. /* URI entry is an ACCESS_DESCRIPTION type */
  45053. #if defined(WOLFSSL_QT)
  45054. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  45055. #else
  45056. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  45057. #endif
  45058. ExpectNotNull(adObj = ad->method);
  45059. /* Make sure nid is OCSP */
  45060. ExpectIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  45061. /* GENERAL_NAME stores URI as an ASN1_STRING */
  45062. ExpectNotNull(gn = ad->location);
  45063. ExpectIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  45064. ExpectNotNull(asn1str = gn->d.uniformResourceIdentifier);
  45065. ExpectIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  45066. #if defined(WOLFSSL_QT)
  45067. ExpectNotNull(str = (char*)ASN1_STRING_get0_data(asn1str));
  45068. #else
  45069. ExpectNotNull(str = (char*)wolfSSL_ASN1_STRING_data(asn1str));
  45070. #endif
  45071. if (str != NULL) {
  45072. actual = strcmp(str, "http://127.0.0.1:22220");
  45073. }
  45074. ExpectIntEQ(actual, 0);
  45075. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  45076. aia = NULL;
  45077. #else
  45078. (void) aia; (void) ad; (void) adObj; (void) gn;
  45079. #endif
  45080. wolfSSL_X509_free(x509);
  45081. #endif
  45082. return EXPECT_RESULT();
  45083. }
  45084. static int test_wolfSSL_X509_get_extension_flags(void)
  45085. {
  45086. EXPECT_DECLS;
  45087. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  45088. XFILE f = XBADFILE;
  45089. X509* x509 = NULL;
  45090. unsigned int extFlags;
  45091. unsigned int keyUsageFlags;
  45092. unsigned int extKeyUsageFlags;
  45093. /* client-int-cert.pem has the following extension flags. */
  45094. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  45095. /* and the following key usage flags. */
  45096. keyUsageFlags = KU_DIGITAL_SIGNATURE
  45097. | KU_NON_REPUDIATION
  45098. | KU_KEY_ENCIPHERMENT;
  45099. /* and the following extended key usage flags. */
  45100. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  45101. ExpectTrue((f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb")) !=
  45102. XBADFILE);
  45103. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  45104. if (f != XBADFILE) {
  45105. XFCLOSE(f);
  45106. f = XBADFILE;
  45107. }
  45108. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  45109. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  45110. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  45111. X509_free(x509);
  45112. x509 = NULL;
  45113. /* client-cert-ext.pem has the following extension flags. */
  45114. extFlags = EXFLAG_KUSAGE;
  45115. /* and the following key usage flags. */
  45116. keyUsageFlags = KU_DIGITAL_SIGNATURE
  45117. | KU_KEY_CERT_SIGN
  45118. | KU_CRL_SIGN;
  45119. ExpectTrue((f = fopen("./certs/client-cert-ext.pem", "rb")) != XBADFILE);
  45120. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  45121. if (f != XBADFILE)
  45122. XFCLOSE(f);
  45123. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  45124. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  45125. X509_free(x509);
  45126. #endif /* OPENSSL_ALL */
  45127. return EXPECT_RESULT();
  45128. }
  45129. static int test_wolfSSL_X509_get_ext(void)
  45130. {
  45131. EXPECT_DECLS;
  45132. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45133. int ret = 0;
  45134. XFILE f = XBADFILE;
  45135. WOLFSSL_X509* x509 = NULL;
  45136. WOLFSSL_X509_EXTENSION* foundExtension;
  45137. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45138. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45139. if (f != XBADFILE)
  45140. XFCLOSE(f);
  45141. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  45142. /* wolfSSL_X509_get_ext() valid input */
  45143. ExpectNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  45144. /* wolfSSL_X509_get_ext() valid x509, idx out of bounds */
  45145. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  45146. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  45147. /* wolfSSL_X509_get_ext() NULL x509, idx out of bounds */
  45148. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  45149. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  45150. /* wolfSSL_X509_get_ext() NULL x509, valid idx */
  45151. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  45152. wolfSSL_X509_free(x509);
  45153. #endif
  45154. return EXPECT_RESULT();
  45155. }
  45156. static int test_wolfSSL_X509_get_ext_by_NID(void)
  45157. {
  45158. EXPECT_DECLS;
  45159. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  45160. int rc = 0;
  45161. XFILE f = XBADFILE;
  45162. WOLFSSL_X509* x509 = NULL;
  45163. ASN1_OBJECT* obj = NULL;
  45164. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45165. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45166. if (f != XBADFILE)
  45167. XFCLOSE(f);
  45168. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  45169. -1), 0);
  45170. /* Start search from last location (should fail) */
  45171. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  45172. rc), -1);
  45173. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  45174. -2), -1);
  45175. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints,
  45176. -1), -1);
  45177. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1), -1);
  45178. /* NID_ext_key_usage, check also its nid and oid */
  45179. ExpectIntGT(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1),
  45180. -1);
  45181. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(
  45182. x509, rc)));
  45183. ExpectIntEQ(obj->nid, NID_ext_key_usage);
  45184. ExpectIntEQ(obj->type, EXT_KEY_USAGE_OID);
  45185. wolfSSL_X509_free(x509);
  45186. #endif
  45187. return EXPECT_RESULT();
  45188. }
  45189. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  45190. {
  45191. EXPECT_DECLS;
  45192. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  45193. int rc = 0;
  45194. XFILE f = XBADFILE;
  45195. WOLFSSL_X509* x509 = NULL;
  45196. WOLFSSL_X509_EXTENSION* ext = NULL;
  45197. WOLFSSL_ASN1_STRING* sanString = NULL;
  45198. byte* sanDer = NULL;
  45199. const byte expectedDer[] = {
  45200. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  45201. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  45202. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45203. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  45204. if (f != XBADFILE)
  45205. XFCLOSE(f);
  45206. ExpectIntNE(rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1), -1);
  45207. ExpectNotNull(ext = X509_get_ext(x509, rc));
  45208. ExpectNotNull(sanString = X509_EXTENSION_get_data(ext));
  45209. ExpectIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  45210. ExpectNotNull(sanDer = ASN1_STRING_data(sanString));
  45211. ExpectIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  45212. X509_free(x509);
  45213. #endif
  45214. return EXPECT_RESULT();
  45215. }
  45216. static int test_wolfSSL_X509_EXTENSION_new(void)
  45217. {
  45218. EXPECT_DECLS;
  45219. #if defined (OPENSSL_ALL)
  45220. WOLFSSL_X509_EXTENSION* ext = NULL;
  45221. ExpectNotNull(ext = wolfSSL_X509_EXTENSION_new());
  45222. ExpectNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  45223. wolfSSL_X509_EXTENSION_free(ext);
  45224. #endif
  45225. return EXPECT_RESULT();
  45226. }
  45227. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  45228. {
  45229. EXPECT_DECLS;
  45230. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45231. WOLFSSL_X509* x509 = NULL;
  45232. WOLFSSL_X509_EXTENSION* ext = NULL;
  45233. WOLFSSL_ASN1_OBJECT* o = NULL;
  45234. XFILE file = XBADFILE;
  45235. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45236. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  45237. if (file != XBADFILE)
  45238. XFCLOSE(file);
  45239. /* wolfSSL_X509_EXTENSION_get_object() testing ext idx 0 */
  45240. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  45241. ExpectNull(wolfSSL_X509_EXTENSION_get_object(NULL));
  45242. ExpectNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  45243. ExpectIntEQ(o->nid, 128);
  45244. /* wolfSSL_X509_EXTENSION_get_object() NULL argument */
  45245. ExpectNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  45246. wolfSSL_X509_free(x509);
  45247. #endif
  45248. return EXPECT_RESULT();
  45249. }
  45250. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  45251. {
  45252. EXPECT_DECLS;
  45253. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45254. WOLFSSL_X509* x509 = NULL;
  45255. WOLFSSL_X509_EXTENSION* ext = NULL;
  45256. WOLFSSL_ASN1_STRING* str = NULL;
  45257. XFILE file = XBADFILE;
  45258. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45259. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  45260. if (file != XBADFILE)
  45261. XFCLOSE(file);
  45262. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  45263. ExpectNull(str = wolfSSL_X509_EXTENSION_get_data(NULL));
  45264. ExpectNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  45265. wolfSSL_X509_free(x509);
  45266. #endif
  45267. return EXPECT_RESULT();
  45268. }
  45269. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  45270. {
  45271. EXPECT_DECLS;
  45272. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45273. WOLFSSL_X509* x509 = NULL;
  45274. WOLFSSL_X509_EXTENSION* ext = NULL;
  45275. XFILE file = XBADFILE;
  45276. int crit = 0;
  45277. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45278. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  45279. if (file != XBADFILE)
  45280. XFCLOSE(file);
  45281. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  45282. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(NULL), BAD_FUNC_ARG);
  45283. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(ext), 0);
  45284. wolfSSL_X509_free(x509);
  45285. #endif
  45286. return EXPECT_RESULT();
  45287. }
  45288. static int test_wolfSSL_X509V3_EXT_print(void)
  45289. {
  45290. EXPECT_DECLS;
  45291. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  45292. !defined(NO_RSA)
  45293. {
  45294. XFILE f = XBADFILE;
  45295. WOLFSSL_X509* x509 = NULL;
  45296. X509_EXTENSION * ext = NULL;
  45297. int loc = 0;
  45298. BIO *bio = NULL;
  45299. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  45300. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45301. if (f != XBADFILE)
  45302. fclose(f);
  45303. ExpectNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  45304. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  45305. NID_basic_constraints, -1), -1);
  45306. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  45307. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  45308. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  45309. NID_subject_key_identifier, -1), -1);
  45310. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  45311. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  45312. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  45313. NID_authority_key_identifier, -1), -1);
  45314. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  45315. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  45316. wolfSSL_BIO_free(bio);
  45317. wolfSSL_X509_free(x509);
  45318. }
  45319. {
  45320. X509 *x509 = NULL;
  45321. BIO *bio = NULL;
  45322. X509_EXTENSION *ext = NULL;
  45323. unsigned int i = 0;
  45324. unsigned int idx = 0;
  45325. /* Some NIDs to test with */
  45326. int nids[] = {
  45327. /* NID_key_usage, currently X509_get_ext returns this as a bit
  45328. * string, which messes up X509V3_EXT_print */
  45329. /* NID_ext_key_usage, */
  45330. NID_subject_alt_name,
  45331. };
  45332. int* n = NULL;
  45333. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  45334. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  45335. WOLFSSL_FILETYPE_PEM));
  45336. ExpectIntGT(fprintf(stderr, "\nPrinting extension values:\n"), 0);
  45337. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  45338. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  45339. * update the index */
  45340. ExpectIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  45341. ExpectNotNull(ext = X509_get_ext(x509, idx));
  45342. ExpectIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  45343. ExpectIntGT(fprintf(stderr, "\n"), 0);
  45344. }
  45345. BIO_free(bio);
  45346. X509_free(x509);
  45347. }
  45348. #endif
  45349. return EXPECT_RESULT();
  45350. }
  45351. static int test_wolfSSL_X509_cmp(void)
  45352. {
  45353. EXPECT_DECLS;
  45354. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  45355. XFILE file1 = XBADFILE;
  45356. XFILE file2 = XBADFILE;
  45357. WOLFSSL_X509* cert1 = NULL;
  45358. WOLFSSL_X509* cert2 = NULL;
  45359. ExpectTrue((file1 = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45360. ExpectTrue((file2 = XFOPEN("./certs/3072/client-cert.pem", "rb")) !=
  45361. XBADFILE);
  45362. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  45363. ExpectNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  45364. if (file1 != XBADFILE)
  45365. fclose(file1);
  45366. if (file2 != XBADFILE)
  45367. fclose(file2);
  45368. /* wolfSSL_X509_cmp() testing matching certs */
  45369. ExpectIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  45370. /* wolfSSL_X509_cmp() testing mismatched certs */
  45371. ExpectIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  45372. /* wolfSSL_X509_cmp() testing NULL, valid args */
  45373. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  45374. /* wolfSSL_X509_cmp() testing valid, NULL args */
  45375. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  45376. /* wolfSSL_X509_cmp() testing NULL, NULL args */
  45377. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  45378. wolfSSL_X509_free(cert1);
  45379. wolfSSL_X509_free(cert2);
  45380. #endif
  45381. return EXPECT_RESULT();
  45382. }
  45383. static int test_wolfSSL_EVP_PKEY_up_ref(void)
  45384. {
  45385. EXPECT_DECLS;
  45386. #if defined(OPENSSL_ALL)
  45387. EVP_PKEY* pkey;
  45388. pkey = EVP_PKEY_new();
  45389. ExpectNotNull(pkey);
  45390. ExpectIntEQ(EVP_PKEY_up_ref(NULL), 0);
  45391. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  45392. EVP_PKEY_free(pkey);
  45393. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  45394. EVP_PKEY_free(pkey);
  45395. EVP_PKEY_free(pkey);
  45396. #endif
  45397. return EXPECT_RESULT();
  45398. }
  45399. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  45400. {
  45401. EXPECT_DECLS;
  45402. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  45403. EVP_PKEY* pkey = NULL;
  45404. const unsigned char* p;
  45405. unsigned char *der = NULL;
  45406. unsigned char *tmp = NULL;
  45407. int derLen;
  45408. p = client_keypub_der_2048;
  45409. /* Check that key can be successfully decoded. */
  45410. ExpectNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  45411. sizeof_client_keypub_der_2048));
  45412. /* Check that key can be successfully encoded. */
  45413. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  45414. /* Ensure that the encoded version matches the original. */
  45415. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  45416. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  45417. /* Do same test except with pre-allocated buffer to ensure the der pointer
  45418. * is advanced. */
  45419. tmp = der;
  45420. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  45421. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  45422. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  45423. ExpectTrue(der + derLen == tmp);
  45424. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  45425. EVP_PKEY_free(pkey);
  45426. #endif
  45427. return EXPECT_RESULT();
  45428. }
  45429. static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
  45430. {
  45431. EXPECT_DECLS;
  45432. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(NO_CERTS) && \
  45433. !defined(NO_ASN) && !defined(NO_PWDBASED)
  45434. EVP_PKEY* pkey = NULL;
  45435. const unsigned char* p;
  45436. unsigned char *der = NULL;
  45437. unsigned char *tmp = NULL;
  45438. int derLen;
  45439. unsigned char pub_buf[65];
  45440. const int pub_len = 65;
  45441. BN_CTX* ctx;
  45442. EC_GROUP* curve = NULL;
  45443. EC_KEY* ephemeral_key = NULL;
  45444. const EC_POINT* h;
  45445. /* Generate an x963 key pair and get public part into pub_buf */
  45446. ExpectNotNull(ctx = BN_CTX_new());
  45447. ExpectNotNull(curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  45448. ExpectNotNull(ephemeral_key = EC_KEY_new_by_curve_name(
  45449. NID_X9_62_prime256v1));
  45450. ExpectIntEQ(EC_KEY_generate_key(ephemeral_key), 1);
  45451. ExpectNotNull(h = EC_KEY_get0_public_key(ephemeral_key));
  45452. ExpectIntEQ(pub_len, EC_POINT_point2oct(curve, h,
  45453. POINT_CONVERSION_UNCOMPRESSED, pub_buf, pub_len, ctx));
  45454. /* Prepare the EVP_PKEY */
  45455. ExpectNotNull(pkey = EVP_PKEY_new());
  45456. p = pub_buf;
  45457. /* Check that key can be successfully decoded. */
  45458. ExpectNotNull(wolfSSL_d2i_PublicKey(EVP_PKEY_EC, &pkey, &p,
  45459. pub_len));
  45460. /* Check that key can be successfully encoded. */
  45461. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  45462. /* Ensure that the encoded version matches the original. */
  45463. ExpectIntEQ(derLen, pub_len);
  45464. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  45465. /* Do same test except with pre-allocated buffer to ensure the der pointer
  45466. * is advanced. */
  45467. tmp = der;
  45468. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  45469. ExpectIntEQ(derLen, pub_len);
  45470. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  45471. ExpectTrue(der + derLen == tmp);
  45472. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  45473. EVP_PKEY_free(pkey);
  45474. EC_KEY_free(ephemeral_key);
  45475. EC_GROUP_free(curve);
  45476. #endif
  45477. return EXPECT_RESULT();
  45478. }
  45479. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  45480. {
  45481. EXPECT_DECLS;
  45482. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  45483. DSA* dsa = NULL;
  45484. byte derIn[] = {
  45485. 0x30, 0x82, 0x01, 0x1f, 0x02, 0x81, 0x81, 0x00,
  45486. 0xcd, 0xde, 0x25, 0x68, 0x80, 0x53, 0x0d, 0xe5,
  45487. 0x77, 0xd6, 0xd2, 0x90, 0x39, 0x3f, 0x90, 0xa2,
  45488. 0x3f, 0x33, 0x94, 0x6e, 0xe8, 0x4f, 0x2b, 0x63,
  45489. 0xab, 0x30, 0xab, 0x15, 0xba, 0x11, 0xea, 0x8a,
  45490. 0x5d, 0x8d, 0xcc, 0xb8, 0xd4, 0xa1, 0xd5, 0xc1,
  45491. 0x47, 0x9d, 0x5a, 0x73, 0x6a, 0x62, 0x49, 0xd1,
  45492. 0x06, 0x07, 0x67, 0xf6, 0x2f, 0xa3, 0x39, 0xbd,
  45493. 0x4e, 0x0d, 0xb4, 0xd3, 0x22, 0x23, 0x84, 0xec,
  45494. 0x93, 0x26, 0x5a, 0x49, 0xee, 0x7c, 0x89, 0x48,
  45495. 0x66, 0x4d, 0xe8, 0xe8, 0xd8, 0x50, 0xfb, 0xa5,
  45496. 0x71, 0x9f, 0x22, 0x18, 0xe5, 0xe6, 0x0b, 0x46,
  45497. 0x87, 0x66, 0xee, 0x52, 0x8f, 0x46, 0x4f, 0xb5,
  45498. 0x03, 0xce, 0xed, 0xe3, 0xbe, 0xe5, 0xb5, 0x81,
  45499. 0xd2, 0x59, 0xe9, 0xc0, 0xad, 0x4d, 0xd0, 0x4d,
  45500. 0x26, 0xf7, 0xba, 0x50, 0xe8, 0xc9, 0x8f, 0xfe,
  45501. 0x24, 0x19, 0x3d, 0x2e, 0xa7, 0x52, 0x3c, 0x6d,
  45502. 0x02, 0x15, 0x00, 0xfb, 0x47, 0xfb, 0xec, 0x81,
  45503. 0x20, 0xc8, 0x1c, 0xe9, 0x4a, 0xba, 0x04, 0x6f,
  45504. 0x19, 0x9b, 0x94, 0xee, 0x82, 0x67, 0xd3, 0x02,
  45505. 0x81, 0x81, 0x00, 0x9b, 0x95, 0xbb, 0x85, 0xc5,
  45506. 0x58, 0x4a, 0x32, 0x9c, 0xaa, 0x44, 0x85, 0xd6,
  45507. 0x68, 0xdc, 0x3e, 0x14, 0xf4, 0xce, 0x6d, 0xa3,
  45508. 0x49, 0x38, 0xea, 0xd6, 0x61, 0x48, 0x92, 0x5a,
  45509. 0x40, 0x95, 0x49, 0x38, 0xaa, 0xe1, 0x39, 0x29,
  45510. 0x68, 0x58, 0x47, 0x8a, 0x4b, 0x01, 0xe1, 0x2e,
  45511. 0x8e, 0x6c, 0x63, 0x6f, 0x40, 0xca, 0x50, 0x3f,
  45512. 0x8c, 0x0b, 0x99, 0xe4, 0x72, 0x42, 0xb8, 0xb1,
  45513. 0xc2, 0x26, 0x48, 0xf1, 0x9c, 0x83, 0xc6, 0x37,
  45514. 0x2e, 0x5a, 0xae, 0x11, 0x09, 0xd9, 0xf3, 0xad,
  45515. 0x1f, 0x6f, 0xad, 0xad, 0x50, 0xe3, 0x78, 0x32,
  45516. 0xe6, 0xde, 0x8e, 0xaa, 0xbf, 0xd1, 0x00, 0x9f,
  45517. 0xb3, 0x02, 0x12, 0x19, 0xa2, 0x15, 0xec, 0x14,
  45518. 0x18, 0x5c, 0x0e, 0x26, 0xce, 0xf9, 0xae, 0xcc,
  45519. 0x7b, 0xb5, 0xd1, 0x26, 0xfc, 0x85, 0xfe, 0x14,
  45520. 0x93, 0xb6, 0x9d, 0x7d, 0x76, 0xe3, 0x35, 0x97,
  45521. 0x1e, 0xde, 0xc4
  45522. };
  45523. int derInLen = sizeof(derIn);
  45524. byte* derOut = NULL;
  45525. int derOutLen;
  45526. byte* p = derIn;
  45527. /* Check that params can be successfully decoded. */
  45528. ExpectNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&p, derInLen));
  45529. /* Check that params can be successfully encoded. */
  45530. ExpectIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  45531. /* Ensure that the encoded version matches the original. */
  45532. ExpectIntEQ(derInLen, derOutLen);
  45533. ExpectIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  45534. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  45535. DSA_free(dsa);
  45536. #endif
  45537. return EXPECT_RESULT();
  45538. }
  45539. static int test_wolfSSL_i2d_PrivateKey(void)
  45540. {
  45541. EXPECT_DECLS;
  45542. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && \
  45543. !defined(NO_ASN) && !defined(NO_PWDBASED)
  45544. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  45545. {
  45546. EVP_PKEY* pkey = NULL;
  45547. const unsigned char* server_key =
  45548. (const unsigned char*)server_key_der_2048;
  45549. unsigned char buf[FOURK_BUF];
  45550. unsigned char* pt = NULL;
  45551. int bufSz = 0;
  45552. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  45553. (long)sizeof_server_key_der_2048));
  45554. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  45555. pt = buf;
  45556. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  45557. ExpectIntNE((pt - buf), 0);
  45558. ExpectIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  45559. EVP_PKEY_free(pkey);
  45560. }
  45561. #endif
  45562. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45563. {
  45564. EVP_PKEY* pkey = NULL;
  45565. const unsigned char* client_key =
  45566. (const unsigned char*)ecc_clikey_der_256;
  45567. unsigned char buf[FOURK_BUF];
  45568. unsigned char* pt = NULL;
  45569. int bufSz = 0;
  45570. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  45571. (long)sizeof_ecc_clikey_der_256)));
  45572. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  45573. pt = buf;
  45574. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  45575. ExpectIntNE((pt - buf), 0);
  45576. ExpectIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  45577. EVP_PKEY_free(pkey);
  45578. }
  45579. #endif
  45580. #endif
  45581. return EXPECT_RESULT();
  45582. }
  45583. static int test_wolfSSL_OCSP_id_get0_info(void)
  45584. {
  45585. EXPECT_DECLS;
  45586. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && \
  45587. defined(HAVE_OCSP) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45588. X509* cert = NULL;
  45589. X509* issuer = NULL;
  45590. OCSP_CERTID* id = NULL;
  45591. OCSP_CERTID* id2 = NULL;
  45592. ASN1_STRING* name = NULL;
  45593. ASN1_OBJECT* pmd = NULL;
  45594. ASN1_STRING* keyHash = NULL;
  45595. ASN1_INTEGER* serial = NULL;
  45596. ASN1_INTEGER* x509Int = NULL;
  45597. ExpectNotNull(cert = wolfSSL_X509_load_certificate_file(svrCertFile,
  45598. SSL_FILETYPE_PEM));
  45599. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caCertFile,
  45600. SSL_FILETYPE_PEM));
  45601. ExpectNotNull(id = OCSP_cert_to_id(NULL, cert, issuer));
  45602. ExpectNotNull(id2 = OCSP_cert_to_id(NULL, cert, issuer));
  45603. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  45604. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  45605. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  45606. ExpectIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  45607. ExpectIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  45608. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  45609. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  45610. ExpectNotNull(serial);
  45611. /* compare serial number to one in cert, should be equal */
  45612. ExpectNotNull(x509Int = X509_get_serialNumber(cert));
  45613. ExpectIntEQ(x509Int->length, serial->length);
  45614. ExpectIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  45615. /* test OCSP_id_cmp */
  45616. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  45617. ExpectIntNE(OCSP_id_cmp(id, NULL), 0);
  45618. ExpectIntNE(OCSP_id_cmp(NULL, id2), 0);
  45619. ExpectIntEQ(OCSP_id_cmp(id, id2), 0);
  45620. if (id != NULL) {
  45621. id->issuerHash[0] = ~id->issuerHash[0];
  45622. }
  45623. ExpectIntNE(OCSP_id_cmp(id, id2), 0);
  45624. OCSP_CERTID_free(id);
  45625. OCSP_CERTID_free(id2);
  45626. X509_free(cert); /* free's x509Int */
  45627. X509_free(issuer);
  45628. #endif
  45629. return EXPECT_RESULT();
  45630. }
  45631. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  45632. {
  45633. EXPECT_DECLS;
  45634. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  45635. WOLFSSL_OCSP_CERTID certId;
  45636. byte* targetBuffer = NULL;
  45637. byte* p;
  45638. /* OCSP CertID bytes taken from PCAP */
  45639. byte rawCertId[] = {
  45640. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  45641. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  45642. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  45643. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  45644. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  45645. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  45646. 0xcf, 0xbc, 0x91
  45647. };
  45648. int ret = 0;
  45649. int i;
  45650. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  45651. certId.rawCertId = rawCertId;
  45652. certId.rawCertIdSize = sizeof(rawCertId);
  45653. ExpectNotNull(targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL,
  45654. DYNAMIC_TYPE_TMP_BUFFER));
  45655. p = targetBuffer;
  45656. /* Function returns the size of the encoded data. */
  45657. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &p), sizeof(rawCertId));
  45658. /* If target buffer is not null, function increments targetBuffer to point
  45659. * just past the end of the encoded data. */
  45660. ExpectPtrEq(p, (targetBuffer + sizeof(rawCertId)));
  45661. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  45662. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  45663. }
  45664. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45665. targetBuffer = NULL;
  45666. /* If target buffer is null, function allocates memory for a buffer and
  45667. * copies the encoded data into it. targetBuffer then points to the start of
  45668. * this newly allocate buffer. */
  45669. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer),
  45670. sizeof(rawCertId));
  45671. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  45672. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  45673. }
  45674. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  45675. #endif
  45676. return EXPECT_RESULT();
  45677. }
  45678. static int test_wolfSSL_d2i_OCSP_CERTID(void)
  45679. {
  45680. EXPECT_DECLS;
  45681. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  45682. WOLFSSL_OCSP_CERTID* certId;
  45683. WOLFSSL_OCSP_CERTID* certIdGood;
  45684. WOLFSSL_OCSP_CERTID* certIdBad;
  45685. const unsigned char* rawCertIdPtr;
  45686. const unsigned char rawCertId[] = {
  45687. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  45688. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  45689. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  45690. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  45691. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  45692. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  45693. 0xcf, 0xbc, 0x91
  45694. };
  45695. rawCertIdPtr = &rawCertId[0];
  45696. /* If the cert ID is NULL the function should allocate it and copy the
  45697. * data to it. */
  45698. certId = NULL;
  45699. ExpectNotNull(certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  45700. sizeof(rawCertId)));
  45701. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  45702. if (certId != NULL) {
  45703. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  45704. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  45705. }
  45706. /* If the cert ID is not NULL the function will just copy the data to it. */
  45707. ExpectNotNull(certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
  45708. DYNAMIC_TYPE_TMP_BUFFER));
  45709. ExpectNotNull(certId);
  45710. ExpectNotNull(XMEMSET(certId, 0, sizeof(*certId)));
  45711. /* Reset rawCertIdPtr since it was push forward in the previous call. */
  45712. rawCertIdPtr = &rawCertId[0];
  45713. ExpectNotNull(certIdGood = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  45714. sizeof(rawCertId)));
  45715. ExpectPtrEq(certIdGood, certId);
  45716. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  45717. if (certId != NULL) {
  45718. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  45719. XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45720. certId = NULL;
  45721. }
  45722. /* The below tests should fail when passed bad parameters. NULL should
  45723. * always be returned. */
  45724. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr,
  45725. sizeof(rawCertId)));
  45726. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL,
  45727. sizeof(rawCertId)));
  45728. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0));
  45729. #endif
  45730. return EXPECT_RESULT();
  45731. }
  45732. static int test_wolfSSL_OCSP_id_cmp(void)
  45733. {
  45734. EXPECT_DECLS;
  45735. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45736. OCSP_CERTID id1;
  45737. OCSP_CERTID id2;
  45738. XMEMSET(&id1, 0, sizeof(id1));
  45739. XMEMSET(&id2, 0, sizeof(id2));
  45740. ExpectIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  45741. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  45742. ExpectIntNE(OCSP_id_cmp(&id1, NULL), 0);
  45743. ExpectIntNE(OCSP_id_cmp(NULL, &id2), 0);
  45744. #endif
  45745. return EXPECT_RESULT();
  45746. }
  45747. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  45748. {
  45749. EXPECT_DECLS;
  45750. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45751. WOLFSSL_OCSP_SINGLERESP single;
  45752. const WOLFSSL_OCSP_CERTID* certId;
  45753. XMEMSET(&single, 0, sizeof(single));
  45754. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  45755. ExpectPtrEq(&single, certId);
  45756. ExpectNull(wolfSSL_OCSP_SINGLERESP_get0_id(NULL));
  45757. #endif
  45758. return EXPECT_RESULT();
  45759. }
  45760. static int test_wolfSSL_OCSP_single_get0_status(void)
  45761. {
  45762. EXPECT_DECLS;
  45763. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45764. WOLFSSL_OCSP_SINGLERESP single;
  45765. CertStatus certStatus;
  45766. WOLFSSL_ASN1_TIME* thisDate;
  45767. WOLFSSL_ASN1_TIME* nextDate;
  45768. int ret, i;
  45769. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45770. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  45771. /* Fill the date fields with some dummy data. */
  45772. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  45773. certStatus.thisDateParsed.data[i] = i;
  45774. certStatus.nextDateParsed.data[i] = i;
  45775. }
  45776. certStatus.status = CERT_GOOD;
  45777. single.status = &certStatus;
  45778. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  45779. &nextDate);
  45780. ExpectIntEQ(ret, CERT_GOOD);
  45781. ExpectPtrEq(thisDate, &certStatus.thisDateParsed);
  45782. ExpectPtrEq(nextDate, &certStatus.nextDateParsed);
  45783. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(NULL, NULL, NULL, NULL, NULL),
  45784. CERT_GOOD);
  45785. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, NULL,
  45786. NULL), CERT_GOOD);
  45787. #endif
  45788. return EXPECT_RESULT();
  45789. }
  45790. static int test_wolfSSL_OCSP_resp_count(void)
  45791. {
  45792. EXPECT_DECLS;
  45793. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45794. WOLFSSL_OCSP_BASICRESP basicResp;
  45795. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  45796. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  45797. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  45798. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45799. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45800. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 0);
  45801. basicResp.single = &singleRespOne;
  45802. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 1);
  45803. singleRespOne.next = &singleRespTwo;
  45804. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 2);
  45805. #endif
  45806. return EXPECT_RESULT();
  45807. }
  45808. static int test_wolfSSL_OCSP_resp_get0(void)
  45809. {
  45810. EXPECT_DECLS;
  45811. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45812. WOLFSSL_OCSP_BASICRESP basicResp;
  45813. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  45814. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  45815. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  45816. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45817. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45818. basicResp.single = &singleRespOne;
  45819. singleRespOne.next = &singleRespTwo;
  45820. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 0), &singleRespOne);
  45821. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 1), &singleRespTwo);
  45822. #endif
  45823. return EXPECT_RESULT();
  45824. }
  45825. static int test_wolfSSL_EVP_PKEY_derive(void)
  45826. {
  45827. EXPECT_DECLS;
  45828. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  45829. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  45830. EVP_PKEY_CTX *ctx = NULL;
  45831. unsigned char *skey = NULL;
  45832. size_t skeylen;
  45833. EVP_PKEY *pkey = NULL;
  45834. EVP_PKEY *peerkey = NULL;
  45835. const unsigned char* key;
  45836. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  45837. /* DH */
  45838. key = dh_key_der_2048;
  45839. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  45840. sizeof_dh_key_der_2048)));
  45841. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  45842. key = dh_key_der_2048;
  45843. ExpectNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  45844. sizeof_dh_key_der_2048)));
  45845. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  45846. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45847. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  45848. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  45849. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  45850. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  45851. DYNAMIC_TYPE_OPENSSL));
  45852. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  45853. EVP_PKEY_CTX_free(ctx);
  45854. ctx = NULL;
  45855. EVP_PKEY_free(peerkey);
  45856. peerkey = NULL;
  45857. EVP_PKEY_free(pkey);
  45858. pkey = NULL;
  45859. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  45860. skey = NULL;
  45861. #endif
  45862. #ifdef HAVE_ECC
  45863. /* ECDH */
  45864. key = ecc_clikey_der_256;
  45865. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  45866. sizeof_ecc_clikey_der_256)));
  45867. key = ecc_clikeypub_der_256;
  45868. ExpectNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  45869. sizeof_ecc_clikeypub_der_256)));
  45870. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45871. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  45872. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  45873. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  45874. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  45875. DYNAMIC_TYPE_OPENSSL));
  45876. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  45877. EVP_PKEY_CTX_free(ctx);
  45878. EVP_PKEY_free(peerkey);
  45879. EVP_PKEY_free(pkey);
  45880. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  45881. #endif /* HAVE_ECC */
  45882. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  45883. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  45884. return EXPECT_RESULT();
  45885. }
  45886. static int test_wolfSSL_EVP_PBE_scrypt(void)
  45887. {
  45888. EXPECT_DECLS;
  45889. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  45890. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  45891. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  45892. int ret;
  45893. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  45894. int pwdlen = sizeof(pwd);
  45895. const byte salt[] = {'N','a','C','l'};
  45896. int saltlen = sizeof(salt);
  45897. byte key[80];
  45898. word64 numOvr32 = (word64)INT32_MAX + 1;
  45899. /* expected derived key for N:16, r:1, p:1 */
  45900. const byte expectedKey[] = {
  45901. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  45902. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  45903. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  45904. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  45905. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  45906. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  45907. 0xE7, 0xE9, 0xC0, 0x9A};
  45908. /* N r p mx key keylen */
  45909. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  45910. ExpectIntEQ(ret, 0); /* N must be greater than 1 */
  45911. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  45912. ExpectIntEQ(ret, 0); /* N must be power of 2 */
  45913. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  45914. ExpectIntEQ(ret, 0); /* r must be greater than 0 */
  45915. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  45916. ExpectIntEQ(ret, 0); /* p must be greater than 0 */
  45917. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  45918. ExpectIntEQ(ret, 0); /* keylen must be greater than 0 */
  45919. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  45920. ExpectIntEQ(ret, 0); /* r must be smaller than 9 */
  45921. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  45922. ExpectIntEQ(ret, 1); /* should succeed if key is NULL */
  45923. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  45924. ExpectIntEQ(ret, 1); /* should succeed */
  45925. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  45926. key, 64);
  45927. ExpectIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  45928. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  45929. key, 64);
  45930. ExpectIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  45931. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  45932. ExpectIntEQ(ret, 1); /* should succeed even if salt is NULL */
  45933. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  45934. ExpectIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  45935. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  45936. ExpectIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  45937. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  45938. ExpectIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  45939. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  45940. ExpectIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  45941. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  45942. ExpectIntEQ(ret, 1);
  45943. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  45944. ExpectIntEQ(ret, 0); /* derived key must be the same as expected-key */
  45945. #endif /* !NO_PWDBASED && !NO_SHA256 */
  45946. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  45947. return EXPECT_RESULT();
  45948. }
  45949. static int test_no_op_functions(void)
  45950. {
  45951. EXPECT_DECLS;
  45952. #if defined(OPENSSL_EXTRA)
  45953. /* this makes sure wolfSSL can compile and run these no-op functions */
  45954. SSL_load_error_strings();
  45955. ENGINE_load_builtin_engines();
  45956. OpenSSL_add_all_ciphers();
  45957. ExpectIntEQ(CRYPTO_malloc_init(), 0);
  45958. #endif
  45959. return EXPECT_RESULT();
  45960. }
  45961. static int test_wolfSSL_CRYPTO_memcmp(void)
  45962. {
  45963. EXPECT_DECLS;
  45964. #ifdef OPENSSL_EXTRA
  45965. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  45966. "implementation of TLS/SSL for embedded devices to the cloud.";
  45967. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  45968. "implementation of TLS/SSL for embedded devices to the cloud.";
  45969. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  45970. "implementation of TLS/SSL for embedded devices to the cloud!";
  45971. ExpectIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  45972. ExpectIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  45973. #endif
  45974. return EXPECT_RESULT();
  45975. }
  45976. /*----------------------------------------------------------------------------*
  45977. | wolfCrypt ASN
  45978. *----------------------------------------------------------------------------*/
  45979. static int test_wc_CreateEncryptedPKCS8Key(void)
  45980. {
  45981. EXPECT_DECLS;
  45982. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  45983. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  45984. WC_RNG rng;
  45985. byte* encKey = NULL;
  45986. word32 encKeySz = 0;
  45987. word32 decKeySz = 0;
  45988. const char password[] = "Lorem ipsum dolor sit amet";
  45989. word32 passwordSz = (word32)XSTRLEN(password);
  45990. word32 tradIdx = 0;
  45991. XMEMSET(&rng, 0, sizeof(WC_RNG));
  45992. ExpectIntEQ(wc_InitRng(&rng), 0);
  45993. PRIVATE_KEY_UNLOCK();
  45994. /* Call with NULL for out buffer to get necessary length. */
  45995. ExpectIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  45996. sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
  45997. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  45998. LENGTH_ONLY_E);
  45999. ExpectNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  46000. DYNAMIC_TYPE_TMP_BUFFER));
  46001. /* Call with the allocated out buffer. */
  46002. ExpectIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  46003. sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
  46004. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  46005. 0);
  46006. /* Decrypt the encrypted PKCS8 key we just made. */
  46007. ExpectIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
  46008. passwordSz)), 0);
  46009. /* encKey now holds the decrypted key (decrypted in place). */
  46010. ExpectIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  46011. /* Check that the decrypted key matches the key prior to encryption. */
  46012. ExpectIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  46013. sizeof_server_key_der_2048), 0);
  46014. PRIVATE_KEY_LOCK();
  46015. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46016. wc_FreeRng(&rng);
  46017. #endif
  46018. return EXPECT_RESULT();
  46019. }
  46020. static int test_wc_GetPkcs8TraditionalOffset(void)
  46021. {
  46022. EXPECT_DECLS;
  46023. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  46024. int length;
  46025. int derSz = 0;
  46026. word32 inOutIdx;
  46027. const char* path = "./certs/server-keyPkcs8.der";
  46028. XFILE file = XBADFILE;
  46029. byte der[2048];
  46030. ExpectTrue((file = XFOPEN(path, "rb")) != XBADFILE);
  46031. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), file), 0);
  46032. if (file != XBADFILE)
  46033. XFCLOSE(file);
  46034. /* valid case */
  46035. inOutIdx = 0;
  46036. ExpectIntGT(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  46037. 0);
  46038. /* inOutIdx > sz */
  46039. inOutIdx = 4000;
  46040. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  46041. BAD_FUNC_ARG);
  46042. /* null input */
  46043. inOutIdx = 0;
  46044. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0),
  46045. BAD_FUNC_ARG);
  46046. /* invalid input, fill buffer with 1's */
  46047. XMEMSET(der, 1, sizeof(der));
  46048. inOutIdx = 0;
  46049. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  46050. ASN_PARSE_E);
  46051. #endif /* NO_ASN */
  46052. return EXPECT_RESULT();
  46053. }
  46054. static int test_wc_SetSubjectRaw(void)
  46055. {
  46056. EXPECT_DECLS;
  46057. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46058. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  46059. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  46060. WOLFSSL_X509* x509 = NULL;
  46061. int peerCertSz;
  46062. const byte* peerCertBuf = NULL;
  46063. Cert forgedCert;
  46064. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  46065. WOLFSSL_FILETYPE_ASN1));
  46066. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  46067. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  46068. ExpectIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  46069. wolfSSL_FreeX509(x509);
  46070. #endif
  46071. return EXPECT_RESULT();
  46072. }
  46073. static int test_wc_GetSubjectRaw(void)
  46074. {
  46075. EXPECT_DECLS;
  46076. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46077. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  46078. Cert cert;
  46079. byte *subjectRaw;
  46080. ExpectIntEQ(0, wc_InitCert(&cert));
  46081. ExpectIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  46082. #endif
  46083. return EXPECT_RESULT();
  46084. }
  46085. static int test_wc_SetIssuerRaw(void)
  46086. {
  46087. EXPECT_DECLS;
  46088. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46089. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  46090. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  46091. WOLFSSL_X509* x509 = NULL;
  46092. int peerCertSz;
  46093. const byte* peerCertBuf;
  46094. Cert forgedCert;
  46095. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  46096. WOLFSSL_FILETYPE_ASN1));
  46097. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  46098. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  46099. ExpectIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  46100. wolfSSL_FreeX509(x509);
  46101. #endif
  46102. return EXPECT_RESULT();
  46103. }
  46104. static int test_wc_SetIssueBuffer(void)
  46105. {
  46106. EXPECT_DECLS;
  46107. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46108. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  46109. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  46110. WOLFSSL_X509* x509 = NULL;
  46111. int peerCertSz;
  46112. const byte* peerCertBuf;
  46113. Cert forgedCert;
  46114. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  46115. WOLFSSL_FILETYPE_ASN1));
  46116. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  46117. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  46118. ExpectIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  46119. wolfSSL_FreeX509(x509);
  46120. #endif
  46121. return EXPECT_RESULT();
  46122. }
  46123. /*
  46124. * Testing wc_SetSubjectKeyId
  46125. */
  46126. static int test_wc_SetSubjectKeyId(void)
  46127. {
  46128. EXPECT_DECLS;
  46129. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46130. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  46131. Cert cert;
  46132. const char* file = "certs/ecc-client-keyPub.pem";
  46133. ExpectIntEQ(0, wc_InitCert(&cert));
  46134. ExpectIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  46135. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  46136. ExpectIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  46137. #endif
  46138. return EXPECT_RESULT();
  46139. } /* END test_wc_SetSubjectKeyId */
  46140. /*
  46141. * Testing wc_SetSubject
  46142. */
  46143. static int test_wc_SetSubject(void)
  46144. {
  46145. EXPECT_DECLS;
  46146. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46147. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  46148. Cert cert;
  46149. const char* file = "./certs/ca-ecc-cert.pem";
  46150. ExpectIntEQ(0, wc_InitCert(&cert));
  46151. ExpectIntEQ(0, wc_SetSubject(&cert, file));
  46152. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  46153. ExpectIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  46154. #endif
  46155. return EXPECT_RESULT();
  46156. } /* END test_wc_SetSubject */
  46157. static int test_CheckCertSignature(void)
  46158. {
  46159. EXPECT_DECLS;
  46160. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  46161. WOLFSSL_CERT_MANAGER* cm = NULL;
  46162. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  46163. XFILE fp = XBADFILE;
  46164. byte cert[4096];
  46165. int certSz;
  46166. #endif
  46167. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  46168. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  46169. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  46170. #ifndef NO_RSA
  46171. #ifdef USE_CERT_BUFFERS_1024
  46172. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  46173. sizeof_server_cert_der_1024, NULL, cm));
  46174. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  46175. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  46176. WOLFSSL_FILETYPE_ASN1));
  46177. ExpectIntEQ(0, CheckCertSignature(server_cert_der_1024,
  46178. sizeof_server_cert_der_1024, NULL, cm));
  46179. #elif defined(USE_CERT_BUFFERS_2048)
  46180. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  46181. sizeof_server_cert_der_2048, NULL, cm));
  46182. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  46183. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  46184. WOLFSSL_FILETYPE_ASN1));
  46185. ExpectIntEQ(0, CheckCertSignature(server_cert_der_2048,
  46186. sizeof_server_cert_der_2048, NULL, cm));
  46187. #endif
  46188. #endif
  46189. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  46190. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  46191. sizeof_serv_ecc_der_256, NULL, cm));
  46192. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  46193. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  46194. WOLFSSL_FILETYPE_ASN1));
  46195. ExpectIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  46196. NULL, cm));
  46197. #endif
  46198. #if !defined(NO_FILESYSTEM)
  46199. wolfSSL_CertManagerFree(cm);
  46200. cm = NULL;
  46201. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  46202. #ifndef NO_RSA
  46203. ExpectTrue((fp = XFOPEN("./certs/server-cert.der", "rb")) != XBADFILE);
  46204. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  46205. if (fp != XBADFILE) {
  46206. XFCLOSE(fp);
  46207. fp = XBADFILE;
  46208. }
  46209. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  46210. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  46211. "./certs/ca-cert.pem", NULL));
  46212. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  46213. #endif
  46214. #ifdef HAVE_ECC
  46215. ExpectTrue((fp = XFOPEN("./certs/server-ecc.der", "rb")) != XBADFILE);
  46216. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  46217. if (fp != XBADFILE) {
  46218. XFCLOSE(fp);
  46219. fp = XBADFILE;
  46220. }
  46221. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  46222. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  46223. "./certs/ca-ecc-cert.pem", NULL));
  46224. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  46225. #endif
  46226. #endif
  46227. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  46228. (void)fp;
  46229. (void)cert;
  46230. (void)certSz;
  46231. #endif
  46232. wolfSSL_CertManagerFree(cm);
  46233. #endif
  46234. return EXPECT_RESULT();
  46235. }
  46236. static int test_wc_ParseCert(void)
  46237. {
  46238. EXPECT_DECLS;
  46239. #if !defined(NO_CERTS) && !defined(NO_RSA)
  46240. DecodedCert decodedCert;
  46241. const byte* rawCert = client_cert_der_2048;
  46242. const int rawCertSize = sizeof_client_cert_der_2048;
  46243. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  46244. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  46245. #ifndef IGNORE_NAME_CONSTRAINTS
  46246. /* check that the subjects emailAddress was not put in the alt name list */
  46247. ExpectNotNull(decodedCert.subjectEmail);
  46248. ExpectNull(decodedCert.altEmailNames);
  46249. #endif
  46250. wc_FreeDecodedCert(&decodedCert);
  46251. #endif
  46252. return EXPECT_RESULT();
  46253. }
  46254. /* Test wc_ParseCert decoding of various encodings and scenarios ensuring that
  46255. * the API safely errors out on badly-formed ASN input.
  46256. * NOTE: Test not compatible with released FIPS implementations!
  46257. */
  46258. static int test_wc_ParseCert_Error(void)
  46259. {
  46260. EXPECT_DECLS;
  46261. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(HAVE_SELFTEST) && \
  46262. (!defined(HAVE_FIPS) || \
  46263. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  46264. DecodedCert decodedCert;
  46265. int i;
  46266. /* Certificate data */
  46267. const byte c0[] = { 0x30, 0x04, 0x30, 0x02, 0x02, 0x80, 0x00, 0x00};
  46268. const byte c1[] = { 0x30, 0x04, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  46269. const byte c2[] = { 0x30, 0x06, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  46270. const byte c3[] = { 0x30, 0x07, 0x30, 0x05, 0x02, 0x80, 0x10, 0x00, 0x00};
  46271. const byte c4[] = { 0x02, 0x80, 0x10, 0x00, 0x00};
  46272. /* Test data */
  46273. const struct testStruct {
  46274. const byte* c;
  46275. const int cSz;
  46276. const int expRet;
  46277. } t[] = {
  46278. {c0, sizeof(c0), ASN_PARSE_E}, /* Invalid bit-string length */
  46279. {c1, sizeof(c1), ASN_PARSE_E}, /* Invalid bit-string length */
  46280. {c2, sizeof(c2), ASN_PARSE_E}, /* Invalid integer length (zero) */
  46281. {c3, sizeof(c3), ASN_PARSE_E}, /* Valid INTEGER, but buffer too short */
  46282. {c4, sizeof(c4), ASN_PARSE_E}, /* Valid INTEGER, but not in bit-string */
  46283. };
  46284. const int tSz = (int)(sizeof(t) / sizeof(struct testStruct));
  46285. for (i = 0; i < tSz; i++) {
  46286. WOLFSSL_MSG_EX("i == %d", i);
  46287. wc_InitDecodedCert(&decodedCert, t[i].c, t[i].cSz, NULL);
  46288. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), t[i].expRet);
  46289. wc_FreeDecodedCert(&decodedCert);
  46290. }
  46291. #endif
  46292. return EXPECT_RESULT();
  46293. }
  46294. static int test_MakeCertWithPathLen(void)
  46295. {
  46296. EXPECT_DECLS;
  46297. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  46298. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  46299. const byte expectedPathLen = 7;
  46300. Cert cert;
  46301. DecodedCert decodedCert;
  46302. byte der[FOURK_BUF];
  46303. int derSize = 0;
  46304. WC_RNG rng;
  46305. ecc_key key;
  46306. int ret;
  46307. XMEMSET(&rng, 0, sizeof(WC_RNG));
  46308. XMEMSET(&key, 0, sizeof(ecc_key));
  46309. XMEMSET(&cert, 0, sizeof(Cert));
  46310. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  46311. ExpectIntEQ(wc_InitRng(&rng), 0);
  46312. ExpectIntEQ(wc_ecc_init(&key), 0);
  46313. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  46314. ExpectIntEQ(wc_InitCert(&cert), 0);
  46315. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  46316. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  46317. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  46318. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  46319. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  46320. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  46321. CTC_NAME_SIZE);
  46322. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  46323. CTC_NAME_SIZE);
  46324. cert.selfSigned = 1;
  46325. cert.isCA = 1;
  46326. cert.pathLen = expectedPathLen;
  46327. cert.pathLenSet = 1;
  46328. cert.sigType = CTC_SHA256wECDSA;
  46329. #ifdef WOLFSSL_CERT_EXT
  46330. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  46331. #endif
  46332. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  46333. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  46334. FOURK_BUF, NULL, &key, &rng), 0);
  46335. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  46336. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  46337. ExpectIntEQ(decodedCert.pathLength, expectedPathLen);
  46338. wc_FreeDecodedCert(&decodedCert);
  46339. ret = wc_ecc_free(&key);
  46340. ExpectIntEQ(ret, 0);
  46341. ret = wc_FreeRng(&rng);
  46342. ExpectIntEQ(ret, 0);
  46343. #endif
  46344. return EXPECT_RESULT();
  46345. }
  46346. static int test_MakeCertWithCaFalse(void)
  46347. {
  46348. EXPECT_DECLS;
  46349. #if defined(WOLFSSL_ALLOW_ENCODING_CA_FALSE) && defined(WOLFSSL_CERT_REQ) && \
  46350. !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  46351. const byte expectedIsCa = 0;
  46352. Cert cert;
  46353. DecodedCert decodedCert;
  46354. byte der[FOURK_BUF];
  46355. int derSize = 0;
  46356. WC_RNG rng;
  46357. ecc_key key;
  46358. int ret;
  46359. XMEMSET(&rng, 0, sizeof(WC_RNG));
  46360. XMEMSET(&key, 0, sizeof(ecc_key));
  46361. XMEMSET(&cert, 0, sizeof(Cert));
  46362. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  46363. ExpectIntEQ(wc_InitRng(&rng), 0);
  46364. ExpectIntEQ(wc_ecc_init(&key), 0);
  46365. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  46366. ExpectIntEQ(wc_InitCert(&cert), 0);
  46367. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  46368. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  46369. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  46370. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  46371. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  46372. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  46373. CTC_NAME_SIZE);
  46374. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  46375. CTC_NAME_SIZE);
  46376. cert.selfSigned = 1;
  46377. cert.isCA = expectedIsCa;
  46378. cert.isCaSet = 1;
  46379. cert.sigType = CTC_SHA256wECDSA;
  46380. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  46381. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  46382. FOURK_BUF, NULL, &key, &rng), 0);
  46383. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  46384. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  46385. ExpectIntEQ(decodedCert.isCA, expectedIsCa);
  46386. wc_FreeDecodedCert(&decodedCert);
  46387. ret = wc_ecc_free(&key);
  46388. ExpectIntEQ(ret, 0);
  46389. ret = wc_FreeRng(&rng);
  46390. ExpectIntEQ(ret, 0);
  46391. #endif
  46392. return EXPECT_RESULT();
  46393. }
  46394. /*----------------------------------------------------------------------------*
  46395. | wolfCrypt ECC
  46396. *----------------------------------------------------------------------------*/
  46397. static int test_wc_ecc_get_curve_size_from_name(void)
  46398. {
  46399. EXPECT_DECLS;
  46400. #ifdef HAVE_ECC
  46401. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46402. ExpectIntEQ(wc_ecc_get_curve_size_from_name("SECP256R1"), 32);
  46403. #endif
  46404. /* invalid case */
  46405. ExpectIntEQ(wc_ecc_get_curve_size_from_name("BADCURVE"), -1);
  46406. /* NULL input */
  46407. ExpectIntEQ(wc_ecc_get_curve_size_from_name(NULL), BAD_FUNC_ARG);
  46408. #endif /* HAVE_ECC */
  46409. return EXPECT_RESULT();
  46410. }
  46411. static int test_wc_ecc_get_curve_id_from_name(void)
  46412. {
  46413. EXPECT_DECLS;
  46414. #ifdef HAVE_ECC
  46415. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46416. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"),
  46417. ECC_SECP256R1);
  46418. #endif
  46419. /* invalid case */
  46420. ExpectIntEQ(wc_ecc_get_curve_id_from_name("BADCURVE"), -1);
  46421. /* NULL input */
  46422. ExpectIntEQ(wc_ecc_get_curve_id_from_name(NULL), BAD_FUNC_ARG);
  46423. #endif /* HAVE_ECC */
  46424. return EXPECT_RESULT();
  46425. }
  46426. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  46427. !defined(HAVE_SELFTEST) && \
  46428. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  46429. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  46430. {
  46431. EXPECT_DECLS;
  46432. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46433. ecc_key* key;
  46434. const ecc_set_type* params = NULL;
  46435. int ret;
  46436. #endif
  46437. WOLFSSL_EC_KEY *ecKey = NULL;
  46438. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46439. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"), ECC_SECP256R1);
  46440. ExpectNotNull(ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  46441. if (EXPECT_SUCCESS()) {
  46442. ret = EC_KEY_generate_key(ecKey);
  46443. } else
  46444. ret = 0;
  46445. if (ret == 1) {
  46446. /* normal test */
  46447. key = (ecc_key*)ecKey->internal;
  46448. if (key != NULL) {
  46449. params = key->dp;
  46450. }
  46451. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(params),
  46452. ECC_SECP256R1);
  46453. }
  46454. #endif
  46455. /* invalid case, NULL input*/
  46456. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(NULL), BAD_FUNC_ARG);
  46457. wolfSSL_EC_KEY_free(ecKey);
  46458. return EXPECT_RESULT();
  46459. }
  46460. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  46461. static int test_wc_ecc_get_curve_id_from_params(void)
  46462. {
  46463. EXPECT_DECLS;
  46464. #ifdef HAVE_ECC
  46465. const byte prime[] =
  46466. {
  46467. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  46468. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46469. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  46470. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  46471. };
  46472. const byte primeInvalid[] =
  46473. {
  46474. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  46475. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46476. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  46477. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  46478. };
  46479. const byte Af[] =
  46480. {
  46481. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  46482. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46483. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  46484. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  46485. };
  46486. const byte Bf[] =
  46487. {
  46488. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  46489. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  46490. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  46491. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  46492. };
  46493. const byte order[] =
  46494. {
  46495. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  46496. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  46497. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  46498. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  46499. };
  46500. const byte Gx[] =
  46501. {
  46502. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  46503. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  46504. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  46505. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  46506. };
  46507. const byte Gy[] =
  46508. {
  46509. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  46510. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  46511. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  46512. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  46513. };
  46514. int cofactor = 1;
  46515. int fieldSize = 256;
  46516. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46517. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  46518. prime, sizeof(prime), Af, sizeof(Af), Bf, sizeof(Bf),
  46519. order, sizeof(order), Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor),
  46520. ECC_SECP256R1);
  46521. #endif
  46522. /* invalid case, fieldSize = 0 */
  46523. ExpectIntEQ(wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  46524. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  46525. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  46526. /* invalid case, NULL prime */
  46527. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  46528. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  46529. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), BAD_FUNC_ARG);
  46530. /* invalid case, invalid prime */
  46531. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  46532. primeInvalid, sizeof(primeInvalid),
  46533. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  46534. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  46535. #endif
  46536. return EXPECT_RESULT();
  46537. }
  46538. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  46539. {
  46540. EXPECT_DECLS;
  46541. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  46542. WOLFSSL_RSA* rsa = NULL;
  46543. WOLFSSL_EVP_PKEY* pkey = NULL;
  46544. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  46545. const char* in = "What is easy to do is easy not to do.";
  46546. size_t inlen = XSTRLEN(in);
  46547. size_t outEncLen = 0;
  46548. byte* outEnc = NULL;
  46549. byte* outDec = NULL;
  46550. size_t outDecLen = 0;
  46551. size_t rsaKeySz = 2048/8; /* Bytes */
  46552. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  46553. byte* inTmp = NULL;
  46554. byte* outEncTmp = NULL;
  46555. byte* outDecTmp = NULL;
  46556. #endif
  46557. ExpectNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  46558. DYNAMIC_TYPE_TMP_BUFFER));
  46559. if (outEnc != NULL) {
  46560. XMEMSET(outEnc, 0, rsaKeySz);
  46561. }
  46562. ExpectNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  46563. DYNAMIC_TYPE_TMP_BUFFER));
  46564. if (outDec != NULL) {
  46565. XMEMSET(outDec, 0, rsaKeySz);
  46566. }
  46567. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46568. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  46569. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  46570. if (EXPECT_FAIL()) {
  46571. RSA_free(rsa);
  46572. }
  46573. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  46574. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  46575. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  46576. WOLFSSL_SUCCESS);
  46577. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  46578. since ctx uses it.*/
  46579. ExpectIntEQ(pkey->ref.count, 2);
  46580. EVP_PKEY_free(pkey);
  46581. ExpectIntEQ(pkey->ref.count, 1);
  46582. /* Encrypt data */
  46583. /* Check that we can get the required output buffer length by passing in a
  46584. * NULL output buffer. */
  46585. ExpectIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  46586. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  46587. ExpectIntEQ(rsaKeySz, outEncLen);
  46588. /* Now do the actual encryption. */
  46589. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  46590. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  46591. /* Decrypt data */
  46592. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  46593. /* Check that we can get the required output buffer length by passing in a
  46594. * NULL output buffer. */
  46595. ExpectIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  46596. WOLFSSL_SUCCESS);
  46597. ExpectIntEQ(rsaKeySz, outDecLen);
  46598. /* Now do the actual decryption. */
  46599. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  46600. WOLFSSL_SUCCESS);
  46601. ExpectIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  46602. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  46603. /* The input length must be the same size as the RSA key.*/
  46604. ExpectNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  46605. DYNAMIC_TYPE_TMP_BUFFER));
  46606. if (inTmp != NULL) {
  46607. XMEMSET(inTmp, 9, rsaKeySz);
  46608. }
  46609. ExpectNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  46610. DYNAMIC_TYPE_TMP_BUFFER));
  46611. if (outEncTmp != NULL) {
  46612. XMEMSET(outEncTmp, 0, rsaKeySz);
  46613. }
  46614. ExpectNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  46615. DYNAMIC_TYPE_TMP_BUFFER));
  46616. if (outDecTmp != NULL) {
  46617. XMEMSET(outDecTmp, 0, rsaKeySz);
  46618. }
  46619. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  46620. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  46621. WOLFSSL_SUCCESS);
  46622. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  46623. WOLFSSL_SUCCESS);
  46624. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  46625. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp,
  46626. outEncLen), WOLFSSL_SUCCESS);
  46627. ExpectIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  46628. #endif
  46629. EVP_PKEY_CTX_free(ctx);
  46630. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46631. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46632. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  46633. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46634. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46635. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46636. #endif
  46637. #endif
  46638. return EXPECT_RESULT();
  46639. }
  46640. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46641. !defined(HAVE_SELFTEST)
  46642. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46643. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46644. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46645. #endif
  46646. #endif
  46647. #endif
  46648. #if defined(OPENSSL_EXTRA)
  46649. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46650. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46651. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46652. #endif
  46653. #endif
  46654. #endif
  46655. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46656. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46657. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46658. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46659. #endif
  46660. #endif
  46661. #endif
  46662. #ifdef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46663. static int test_wolfSSL_EVP_PKEY_sign_verify(int keyType)
  46664. {
  46665. EXPECT_DECLS;
  46666. #if defined(OPENSSL_EXTRA)
  46667. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46668. !defined(HAVE_SELFTEST)
  46669. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46670. WOLFSSL_RSA* rsa = NULL;
  46671. #endif
  46672. #endif
  46673. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46674. WOLFSSL_DSA* dsa = NULL;
  46675. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  46676. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46677. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46678. WOLFSSL_EC_KEY* ecKey = NULL;
  46679. #endif
  46680. #endif
  46681. WOLFSSL_EVP_PKEY* pkey = NULL;
  46682. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  46683. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  46684. const char* in = "What is easy to do is easy not to do.";
  46685. size_t inlen = XSTRLEN(in);
  46686. byte hash[SHA256_DIGEST_LENGTH] = {0};
  46687. byte zero[SHA256_DIGEST_LENGTH] = {0};
  46688. SHA256_CTX c;
  46689. byte* sig = NULL;
  46690. byte* sigVerify = NULL;
  46691. size_t siglen;
  46692. size_t siglenOnlyLen;
  46693. size_t keySz = 2048/8; /* Bytes */
  46694. ExpectNotNull(sig =
  46695. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  46696. ExpectNotNull(sigVerify =
  46697. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  46698. siglen = keySz;
  46699. ExpectNotNull(XMEMSET(sig, 0, keySz));
  46700. ExpectNotNull(XMEMSET(sigVerify, 0, keySz));
  46701. /* Generate hash */
  46702. SHA256_Init(&c);
  46703. SHA256_Update(&c, in, inlen);
  46704. SHA256_Final(hash, &c);
  46705. #ifdef WOLFSSL_SMALL_STACK_CACHE
  46706. /* workaround for small stack cache case */
  46707. wc_Sha256Free((wc_Sha256*)&c);
  46708. #endif
  46709. /* Generate key */
  46710. ExpectNotNull(pkey = EVP_PKEY_new());
  46711. switch (keyType) {
  46712. case EVP_PKEY_RSA:
  46713. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46714. !defined(HAVE_SELFTEST)
  46715. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46716. {
  46717. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46718. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  46719. }
  46720. #endif
  46721. #endif
  46722. break;
  46723. case EVP_PKEY_DSA:
  46724. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46725. ExpectNotNull(dsa = DSA_new());
  46726. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  46727. NULL, 0, NULL, NULL, NULL), 1);
  46728. ExpectIntEQ(DSA_generate_key(dsa), 1);
  46729. ExpectIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  46730. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  46731. break;
  46732. case EVP_PKEY_EC:
  46733. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46734. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46735. {
  46736. ExpectNotNull(ecKey = EC_KEY_new());
  46737. ExpectIntEQ(EC_KEY_generate_key(ecKey), 1);
  46738. ExpectIntEQ(
  46739. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  46740. if (EXPECT_FAIL()) {
  46741. EC_KEY_free(ecKey);
  46742. }
  46743. }
  46744. #endif
  46745. #endif
  46746. break;
  46747. }
  46748. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  46749. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46750. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46751. !defined(HAVE_SELFTEST)
  46752. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46753. if (keyType == EVP_PKEY_RSA)
  46754. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  46755. WOLFSSL_SUCCESS);
  46756. #endif
  46757. #endif
  46758. /* Check returning only length */
  46759. ExpectIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  46760. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  46761. ExpectIntGT(siglenOnlyLen, 0);
  46762. /* Sign data */
  46763. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  46764. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  46765. ExpectIntGE(siglenOnlyLen, siglen);
  46766. /* Verify signature */
  46767. ExpectNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  46768. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  46769. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46770. !defined(HAVE_SELFTEST)
  46771. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46772. if (keyType == EVP_PKEY_RSA)
  46773. ExpectIntEQ(
  46774. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  46775. WOLFSSL_SUCCESS);
  46776. #endif
  46777. #endif
  46778. ExpectIntEQ(EVP_PKEY_verify(
  46779. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  46780. WOLFSSL_SUCCESS);
  46781. ExpectIntEQ(EVP_PKEY_verify(
  46782. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  46783. WOLFSSL_FAILURE);
  46784. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46785. !defined(HAVE_SELFTEST)
  46786. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46787. if (keyType == EVP_PKEY_RSA) {
  46788. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  46789. /* Try RSA sign/verify with no padding. */
  46790. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46791. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  46792. WOLFSSL_SUCCESS);
  46793. ExpectIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  46794. siglen), WOLFSSL_SUCCESS);
  46795. ExpectIntGE(siglenOnlyLen, siglen);
  46796. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  46797. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  46798. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  46799. ExpectIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  46800. siglen), WOLFSSL_SUCCESS);
  46801. #endif
  46802. /* Wrong padding schemes. */
  46803. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46804. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  46805. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  46806. ExpectIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  46807. siglen), WOLFSSL_SUCCESS);
  46808. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  46809. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  46810. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  46811. ExpectIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  46812. siglen), WOLFSSL_SUCCESS);
  46813. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  46814. WOLFSSL_SUCCESS);
  46815. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  46816. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  46817. }
  46818. #endif
  46819. #endif
  46820. /* error cases */
  46821. siglen = keySz; /* Reset because sig size may vary slightly */
  46822. ExpectIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  46823. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46824. ExpectIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  46825. WOLFSSL_SUCCESS);
  46826. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  46827. WOLFSSL_SUCCESS);
  46828. EVP_PKEY_free(pkey);
  46829. pkey = NULL;
  46830. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46831. DSA_free(dsa);
  46832. dsa = NULL;
  46833. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  46834. EVP_PKEY_CTX_free(ctx_verify);
  46835. ctx_verify = NULL;
  46836. EVP_PKEY_CTX_free(ctx);
  46837. ctx = NULL;
  46838. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46839. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46840. #endif /* OPENSSL_EXTRA */
  46841. return EXPECT_RESULT();
  46842. }
  46843. #endif
  46844. static int test_wolfSSL_EVP_PKEY_sign_verify_rsa(void)
  46845. {
  46846. EXPECT_DECLS;
  46847. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46848. !defined(HAVE_SELFTEST)
  46849. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46850. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_RSA), TEST_SUCCESS);
  46851. #endif
  46852. #endif
  46853. return EXPECT_RESULT();
  46854. }
  46855. static int test_wolfSSL_EVP_PKEY_sign_verify_dsa(void)
  46856. {
  46857. EXPECT_DECLS;
  46858. #if defined(OPENSSL_EXTRA)
  46859. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46860. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_DSA), TEST_SUCCESS);
  46861. #endif
  46862. #endif
  46863. return EXPECT_RESULT();
  46864. }
  46865. static int test_wolfSSL_EVP_PKEY_sign_verify_ec(void)
  46866. {
  46867. EXPECT_DECLS;
  46868. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46869. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46870. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_EC), TEST_SUCCESS);
  46871. #endif
  46872. #endif
  46873. return EXPECT_RESULT();
  46874. }
  46875. static int test_EVP_PKEY_rsa(void)
  46876. {
  46877. EXPECT_DECLS;
  46878. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  46879. WOLFSSL_RSA* rsa = NULL;
  46880. WOLFSSL_EVP_PKEY* pkey = NULL;
  46881. ExpectNotNull(rsa = wolfSSL_RSA_new());
  46882. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  46883. ExpectIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  46884. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  46885. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  46886. if (EXPECT_FAIL()) {
  46887. wolfSSL_RSA_free(rsa);
  46888. }
  46889. ExpectPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  46890. wolfSSL_EVP_PKEY_free(pkey);
  46891. #endif
  46892. return EXPECT_RESULT();
  46893. }
  46894. static int test_EVP_PKEY_ec(void)
  46895. {
  46896. EXPECT_DECLS;
  46897. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46898. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46899. WOLFSSL_EC_KEY* ecKey = NULL;
  46900. WOLFSSL_EVP_PKEY* pkey = NULL;
  46901. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  46902. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  46903. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  46904. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  46905. /* Should fail since ecKey is empty */
  46906. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  46907. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  46908. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  46909. if (EXPECT_FAIL()) {
  46910. wolfSSL_EC_KEY_free(ecKey);
  46911. }
  46912. wolfSSL_EVP_PKEY_free(pkey);
  46913. #endif
  46914. #endif
  46915. return EXPECT_RESULT();
  46916. }
  46917. static int test_EVP_PKEY_cmp(void)
  46918. {
  46919. EXPECT_DECLS;
  46920. #if defined(OPENSSL_EXTRA)
  46921. EVP_PKEY *a = NULL;
  46922. EVP_PKEY *b = NULL;
  46923. const unsigned char *in;
  46924. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  46925. in = client_key_der_2048;
  46926. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  46927. &in, (long)sizeof_client_key_der_2048));
  46928. in = client_key_der_2048;
  46929. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  46930. &in, (long)sizeof_client_key_der_2048));
  46931. /* Test success case RSA */
  46932. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  46933. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  46934. #else
  46935. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  46936. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  46937. EVP_PKEY_free(b);
  46938. b = NULL;
  46939. EVP_PKEY_free(a);
  46940. a = NULL;
  46941. #endif
  46942. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  46943. in = ecc_clikey_der_256;
  46944. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  46945. &in, (long)sizeof_ecc_clikey_der_256));
  46946. in = ecc_clikey_der_256;
  46947. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  46948. &in, (long)sizeof_ecc_clikey_der_256));
  46949. /* Test success case ECC */
  46950. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  46951. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  46952. #else
  46953. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  46954. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  46955. EVP_PKEY_free(b);
  46956. b = NULL;
  46957. EVP_PKEY_free(a);
  46958. a = NULL;
  46959. #endif
  46960. /* Test failure cases */
  46961. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  46962. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  46963. in = client_key_der_2048;
  46964. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  46965. &in, (long)sizeof_client_key_der_2048));
  46966. in = ecc_clikey_der_256;
  46967. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  46968. &in, (long)sizeof_ecc_clikey_der_256));
  46969. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  46970. ExpectIntEQ(EVP_PKEY_cmp(a, b), -1);
  46971. #else
  46972. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  46973. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  46974. EVP_PKEY_free(b);
  46975. b = NULL;
  46976. EVP_PKEY_free(a);
  46977. a = NULL;
  46978. #endif
  46979. /* invalid or empty failure cases */
  46980. a = EVP_PKEY_new();
  46981. b = EVP_PKEY_new();
  46982. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  46983. ExpectIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  46984. ExpectIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  46985. ExpectIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  46986. #ifdef NO_RSA
  46987. /* Type check will fail since RSA is the default EVP key type */
  46988. ExpectIntEQ(EVP_PKEY_cmp(a, b), -2);
  46989. #else
  46990. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  46991. #endif
  46992. #else
  46993. ExpectIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  46994. ExpectIntNE(EVP_PKEY_cmp(a, NULL), 0);
  46995. ExpectIntNE(EVP_PKEY_cmp(NULL, b), 0);
  46996. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  46997. #endif
  46998. EVP_PKEY_free(b);
  46999. EVP_PKEY_free(a);
  47000. (void)in;
  47001. #endif
  47002. return EXPECT_RESULT();
  47003. }
  47004. static int test_ERR_load_crypto_strings(void)
  47005. {
  47006. #if defined(OPENSSL_ALL)
  47007. ERR_load_crypto_strings();
  47008. return TEST_SUCCESS;
  47009. #else
  47010. return TEST_SKIPPED;
  47011. #endif
  47012. }
  47013. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  47014. static void free_x509(X509* x)
  47015. {
  47016. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  47017. }
  47018. #endif
  47019. static int test_sk_X509(void)
  47020. {
  47021. EXPECT_DECLS;
  47022. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  47023. {
  47024. STACK_OF(X509)* s = NULL;
  47025. ExpectNotNull(s = sk_X509_new_null());
  47026. ExpectIntEQ(sk_X509_num(s), 0);
  47027. sk_X509_pop_free(s, NULL);
  47028. ExpectNotNull(s = sk_X509_new_null());
  47029. ExpectIntEQ(sk_X509_num(s), 0);
  47030. sk_X509_pop_free(s, NULL);
  47031. ExpectNotNull(s = sk_X509_new_null());
  47032. sk_X509_push(s, (X509*)1);
  47033. ExpectIntEQ(sk_X509_num(s), 1);
  47034. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  47035. sk_X509_push(s, (X509*)2);
  47036. ExpectIntEQ(sk_X509_num(s), 2);
  47037. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  47038. ExpectIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  47039. sk_X509_push(s, (X509*)2);
  47040. sk_X509_pop_free(s, free_x509);
  47041. }
  47042. {
  47043. /* Push a list of 10 X509s onto stack, then verify that
  47044. * value(), push(), shift(), and pop() behave as expected. */
  47045. STACK_OF(X509)* s = NULL;
  47046. X509* xList[10];
  47047. int i = 0;
  47048. const int len = (sizeof(xList) / sizeof(xList[0]));
  47049. for (i = 0; i < len; ++i) {
  47050. xList[i] = NULL;
  47051. ExpectNotNull(xList[i] = X509_new());
  47052. }
  47053. /* test push, pop, and free */
  47054. ExpectNotNull(s = sk_X509_new_null());
  47055. for (i = 0; i < len; ++i) {
  47056. sk_X509_push(s, xList[i]);
  47057. ExpectIntEQ(sk_X509_num(s), i + 1);
  47058. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  47059. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  47060. }
  47061. /* pop returns and removes last pushed on stack, which is index 0
  47062. * in sk_x509_value */
  47063. for (i = 0; i < len; ++i) {
  47064. X509 * x = sk_X509_value(s, 0);
  47065. X509 * y = sk_X509_pop(s);
  47066. X509 * z = xList[len - 1 - i];
  47067. ExpectIntEQ((x == y), 1);
  47068. ExpectIntEQ((x == z), 1);
  47069. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  47070. }
  47071. sk_free(s);
  47072. s = NULL;
  47073. /* test push, shift, and free */
  47074. ExpectNotNull(s = sk_X509_new_null());
  47075. for (i = 0; i < len; ++i) {
  47076. sk_X509_push(s, xList[i]);
  47077. ExpectIntEQ(sk_X509_num(s), i + 1);
  47078. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  47079. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  47080. }
  47081. /* shift returns and removes first pushed on stack, which is index i
  47082. * in sk_x509_value() */
  47083. for (i = 0; i < len; ++i) {
  47084. X509 * x = sk_X509_value(s, len - 1 - i);
  47085. X509 * y = sk_X509_shift(s);
  47086. X509 * z = xList[i];
  47087. ExpectIntEQ((x == y), 1);
  47088. ExpectIntEQ((x == z), 1);
  47089. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  47090. }
  47091. sk_free(s);
  47092. for (i = 0; i < len; ++i)
  47093. X509_free(xList[i]);
  47094. }
  47095. #endif
  47096. return EXPECT_RESULT();
  47097. }
  47098. static int test_sk_X509_CRL(void)
  47099. {
  47100. EXPECT_DECLS;
  47101. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  47102. X509_CRL* crl = NULL;
  47103. XFILE fp = XBADFILE;
  47104. STACK_OF(X509_CRL)* s = NULL;
  47105. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  47106. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  47107. NULL, NULL));
  47108. if (fp != XBADFILE)
  47109. XFCLOSE(fp);
  47110. ExpectNotNull(s = sk_X509_CRL_new());
  47111. ExpectIntEQ(sk_X509_CRL_num(s), 0);
  47112. ExpectIntEQ(sk_X509_CRL_push(s, crl), 1);
  47113. if (EXPECT_FAIL()) {
  47114. X509_CRL_free(crl);
  47115. }
  47116. ExpectIntEQ(sk_X509_CRL_num(s), 1);
  47117. ExpectPtrEq(sk_X509_CRL_value(s, 0), crl);
  47118. sk_X509_CRL_free(s);
  47119. #endif
  47120. return EXPECT_RESULT();
  47121. }
  47122. static int test_X509_get_signature_nid(void)
  47123. {
  47124. EXPECT_DECLS;
  47125. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  47126. X509* x509 = NULL;
  47127. ExpectIntEQ(X509_get_signature_nid(NULL), 0);
  47128. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  47129. SSL_FILETYPE_PEM));
  47130. ExpectIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  47131. X509_free(x509);
  47132. #endif
  47133. return EXPECT_RESULT();
  47134. }
  47135. static int test_X509_REQ(void)
  47136. {
  47137. EXPECT_DECLS;
  47138. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  47139. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  47140. X509_NAME* name = NULL;
  47141. #ifndef NO_RSA
  47142. X509_NAME* subject = NULL;
  47143. #endif
  47144. #if !defined(NO_RSA) || defined(HAVE_ECC)
  47145. X509_REQ* req = NULL;
  47146. EVP_PKEY* priv = NULL;
  47147. EVP_PKEY* pub = NULL;
  47148. unsigned char* der = NULL;
  47149. int len;
  47150. #endif
  47151. #ifndef NO_RSA
  47152. EVP_MD_CTX *mctx = NULL;
  47153. EVP_PKEY_CTX *pkctx = NULL;
  47154. #ifdef USE_CERT_BUFFERS_1024
  47155. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  47156. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  47157. #elif defined(USE_CERT_BUFFERS_2048)
  47158. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  47159. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  47160. #endif
  47161. #endif
  47162. #ifdef HAVE_ECC
  47163. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  47164. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  47165. #endif
  47166. ExpectNotNull(name = X509_NAME_new());
  47167. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  47168. (byte*)"wolfssl.com", 11, 0, 1), WOLFSSL_SUCCESS);
  47169. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  47170. (byte*)"support@wolfssl.com", 19, -1, 1), WOLFSSL_SUCCESS);
  47171. #ifndef NO_RSA
  47172. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  47173. (long)sizeof_client_key_der_2048));
  47174. ExpectNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  47175. (long)sizeof_client_keypub_der_2048));
  47176. ExpectNotNull(req = X509_REQ_new());
  47177. ExpectIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  47178. ExpectIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  47179. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  47180. ExpectIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  47181. ExpectIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  47182. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  47183. ExpectIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  47184. ExpectIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  47185. ExpectIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  47186. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  47187. len = i2d_X509_REQ(req, &der);
  47188. DEBUG_WRITE_DER(der, len, "req.der");
  47189. #ifdef USE_CERT_BUFFERS_1024
  47190. ExpectIntEQ(len, 381);
  47191. #else
  47192. ExpectIntEQ(len, 643);
  47193. #endif
  47194. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  47195. der = NULL;
  47196. mctx = EVP_MD_CTX_new();
  47197. ExpectIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv),
  47198. WOLFSSL_SUCCESS);
  47199. ExpectIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  47200. EVP_MD_CTX_free(mctx);
  47201. mctx = NULL;
  47202. X509_REQ_free(NULL);
  47203. X509_REQ_free(req);
  47204. req = NULL;
  47205. /* Test getting the subject from a newly created X509_REQ */
  47206. ExpectNotNull(req = X509_REQ_new());
  47207. ExpectNotNull(subject = X509_REQ_get_subject_name(req));
  47208. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  47209. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  47210. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  47211. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  47212. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  47213. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  47214. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  47215. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  47216. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  47217. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  47218. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  47219. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  47220. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  47221. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  47222. len = i2d_X509_REQ(req, &der);
  47223. DEBUG_WRITE_DER(der, len, "req2.der");
  47224. #ifdef USE_CERT_BUFFERS_1024
  47225. ExpectIntEQ(len, 435);
  47226. #else
  47227. ExpectIntEQ(len, 696);
  47228. #endif
  47229. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  47230. der = NULL;
  47231. EVP_PKEY_free(pub);
  47232. pub = NULL;
  47233. EVP_PKEY_free(priv);
  47234. priv = NULL;
  47235. X509_REQ_free(req);
  47236. req = NULL;
  47237. #endif
  47238. #ifdef HAVE_ECC
  47239. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  47240. sizeof_ecc_clikey_der_256));
  47241. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  47242. sizeof_ecc_clikeypub_der_256));
  47243. ExpectNotNull(req = X509_REQ_new());
  47244. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  47245. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  47246. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  47247. /* Signature is random and may be shorter or longer. */
  47248. ExpectIntGE((len = i2d_X509_REQ(req, &der)), 245);
  47249. ExpectIntLE(len, 253);
  47250. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  47251. X509_REQ_free(req);
  47252. EVP_PKEY_free(pub);
  47253. EVP_PKEY_free(priv);
  47254. #ifdef FP_ECC
  47255. wc_ecc_fp_free();
  47256. #endif
  47257. #endif /* HAVE_ECC */
  47258. X509_NAME_free(name);
  47259. #endif
  47260. return EXPECT_RESULT();
  47261. }
  47262. static int test_wolfssl_PKCS7(void)
  47263. {
  47264. EXPECT_DECLS;
  47265. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  47266. !defined(NO_RSA)
  47267. PKCS7* pkcs7 = NULL;
  47268. byte data[FOURK_BUF];
  47269. word32 len = sizeof(data);
  47270. const byte* p = data;
  47271. byte content[] = "Test data to encode.";
  47272. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  47273. BIO* bio = NULL;
  47274. byte key[sizeof(client_key_der_2048)];
  47275. word32 keySz = (word32)sizeof(key);
  47276. byte* out = NULL;
  47277. #endif
  47278. ExpectIntGT((len = CreatePKCS7SignedData(data, len, content,
  47279. (word32)sizeof(content), 0, 0, 0, RSA_TYPE)), 0);
  47280. ExpectNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  47281. ExpectNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  47282. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  47283. ExpectIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  47284. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  47285. PKCS7_free(pkcs7);
  47286. pkcs7 = NULL;
  47287. /* fail case, without PKCS7_NOVERIFY */
  47288. p = data;
  47289. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  47290. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  47291. 0), WOLFSSL_FAILURE);
  47292. PKCS7_free(pkcs7);
  47293. pkcs7 = NULL;
  47294. /* success case, with PKCS7_NOVERIFY */
  47295. p = data;
  47296. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  47297. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  47298. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  47299. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  47300. /* test i2d */
  47301. XMEMCPY(key, client_key_der_2048, keySz);
  47302. if (pkcs7 != NULL) {
  47303. pkcs7->privateKey = key;
  47304. pkcs7->privateKeySz = (word32)sizeof(key);
  47305. pkcs7->encryptOID = RSAk;
  47306. #ifdef NO_SHA
  47307. pkcs7->hashOID = SHA256h;
  47308. #else
  47309. pkcs7->hashOID = SHAh;
  47310. #endif
  47311. }
  47312. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  47313. ExpectIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  47314. #ifndef NO_ASN_TIME
  47315. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  47316. #else
  47317. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 625);
  47318. #endif
  47319. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47320. BIO_free(bio);
  47321. #endif
  47322. PKCS7_free(NULL);
  47323. PKCS7_free(pkcs7);
  47324. #endif
  47325. return EXPECT_RESULT();
  47326. }
  47327. static int test_wolfSSL_PKCS7_sign(void)
  47328. {
  47329. EXPECT_DECLS;
  47330. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  47331. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  47332. PKCS7* p7 = NULL;
  47333. PKCS7* p7Ver = NULL;
  47334. byte* out = NULL;
  47335. byte* tmpPtr = NULL;
  47336. int outLen = 0;
  47337. int flags = 0;
  47338. byte data[] = "Test data to encode.";
  47339. const char* cert = "./certs/server-cert.pem";
  47340. const char* key = "./certs/server-key.pem";
  47341. const char* ca = "./certs/ca-cert.pem";
  47342. WOLFSSL_BIO* certBio = NULL;
  47343. WOLFSSL_BIO* keyBio = NULL;
  47344. WOLFSSL_BIO* caBio = NULL;
  47345. WOLFSSL_BIO* inBio = NULL;
  47346. X509* signCert = NULL;
  47347. EVP_PKEY* signKey = NULL;
  47348. X509* caCert = NULL;
  47349. X509_STORE* store = NULL;
  47350. #ifndef NO_PKCS7_STREAM
  47351. int z;
  47352. int ret;
  47353. #endif /* !NO_PKCS7_STREAM */
  47354. /* read signer cert/key into BIO */
  47355. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  47356. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  47357. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  47358. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  47359. /* read CA cert into store (for verify) */
  47360. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  47361. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  47362. ExpectNotNull(store = X509_STORE_new());
  47363. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  47364. /* data to be signed into BIO */
  47365. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47366. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47367. /* PKCS7_sign, bad args: signer NULL */
  47368. ExpectNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  47369. /* PKCS7_sign, bad args: signer key NULL */
  47370. ExpectNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  47371. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  47372. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  47373. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  47374. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  47375. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  47376. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  47377. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  47378. {
  47379. flags = PKCS7_BINARY;
  47380. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47381. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  47382. /* verify with d2i_PKCS7 */
  47383. tmpPtr = out;
  47384. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  47385. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  47386. PKCS7_free(p7Ver);
  47387. p7Ver = NULL;
  47388. /* verify with wc_PKCS7_VerifySignedData */
  47389. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47390. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  47391. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  47392. #ifndef NO_PKCS7_STREAM
  47393. /* verify with wc_PKCS7_VerifySignedData streaming */
  47394. wc_PKCS7_Free(p7Ver);
  47395. p7Ver = NULL;
  47396. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47397. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  47398. /* test for streaming */
  47399. ret = -1;
  47400. for (z = 0; z < outLen && ret != 0; z++) {
  47401. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  47402. if (ret < 0){
  47403. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  47404. }
  47405. }
  47406. ExpectIntEQ(ret, 0);
  47407. #endif /* !NO_PKCS7_STREAM */
  47408. /* compare the signer found to expected signer */
  47409. ExpectIntNE(p7Ver->verifyCertSz, 0);
  47410. tmpPtr = NULL;
  47411. ExpectIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  47412. ExpectIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  47413. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  47414. tmpPtr = NULL;
  47415. wc_PKCS7_Free(p7Ver);
  47416. p7Ver = NULL;
  47417. ExpectNotNull(out);
  47418. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47419. out = NULL;
  47420. PKCS7_free(p7);
  47421. p7 = NULL;
  47422. }
  47423. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  47424. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  47425. {
  47426. /* re-populate input BIO, may have been consumed */
  47427. BIO_free(inBio);
  47428. inBio = NULL;
  47429. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47430. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47431. flags = PKCS7_BINARY | PKCS7_STREAM;
  47432. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47433. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  47434. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  47435. /* PKCS7_final, bad args: PKCS7 null */
  47436. ExpectIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  47437. /* PKCS7_final, bad args: PKCS7 null */
  47438. ExpectIntEQ(PKCS7_final(p7, NULL, 0), 0);
  47439. tmpPtr = out;
  47440. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  47441. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  47442. PKCS7_free(p7Ver);
  47443. p7Ver = NULL;
  47444. ExpectNotNull(out);
  47445. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47446. out = NULL;
  47447. PKCS7_free(p7);
  47448. p7 = NULL;
  47449. }
  47450. /* TEST SUCCESS: Detached, not streaming, not MIME */
  47451. {
  47452. /* re-populate input BIO, may have been consumed */
  47453. BIO_free(inBio);
  47454. inBio = NULL;
  47455. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47456. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47457. flags = PKCS7_BINARY | PKCS7_DETACHED;
  47458. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47459. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  47460. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  47461. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47462. if (p7Ver != NULL) {
  47463. p7Ver->content = data;
  47464. p7Ver->contentSz = sizeof(data);
  47465. }
  47466. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  47467. wc_PKCS7_Free(p7Ver);
  47468. p7Ver = NULL;
  47469. #ifndef NO_PKCS7_STREAM
  47470. /* verify with wc_PKCS7_VerifySignedData streaming */
  47471. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47472. if (p7Ver != NULL) {
  47473. p7Ver->content = data;
  47474. p7Ver->contentSz = sizeof(data);
  47475. }
  47476. /* test for streaming */
  47477. ret = -1;
  47478. for (z = 0; z < outLen && ret != 0; z++) {
  47479. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  47480. if (ret < 0){
  47481. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  47482. }
  47483. }
  47484. ExpectIntEQ(ret, 0);
  47485. wc_PKCS7_Free(p7Ver);
  47486. p7Ver = NULL;
  47487. #endif /* !NO_PKCS7_STREAM */
  47488. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  47489. * yet support detached content */
  47490. tmpPtr = out;
  47491. ExpectNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  47492. PKCS7_free(p7Ver);
  47493. p7Ver = NULL;
  47494. ExpectNotNull(out);
  47495. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47496. out = NULL;
  47497. PKCS7_free(p7);
  47498. p7 = NULL;
  47499. }
  47500. /* TEST SUCCESS: Detached, streaming, not MIME */
  47501. {
  47502. /* re-populate input BIO, may have been consumed */
  47503. BIO_free(inBio);
  47504. inBio = NULL;
  47505. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47506. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47507. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  47508. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47509. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  47510. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  47511. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  47512. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47513. if (p7Ver != NULL) {
  47514. p7Ver->content = data;
  47515. p7Ver->contentSz = sizeof(data);
  47516. }
  47517. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  47518. wc_PKCS7_Free(p7Ver);
  47519. p7Ver = NULL;
  47520. ExpectNotNull(out);
  47521. #ifndef NO_PKCS7_STREAM
  47522. /* verify with wc_PKCS7_VerifySignedData streaming */
  47523. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47524. if (p7Ver != NULL) {
  47525. p7Ver->content = data;
  47526. p7Ver->contentSz = sizeof(data);
  47527. }
  47528. /* test for streaming */
  47529. ret = -1;
  47530. for (z = 0; z < outLen && ret != 0; z++) {
  47531. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  47532. if (ret < 0){
  47533. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  47534. }
  47535. }
  47536. ExpectIntEQ(ret, 0);
  47537. ExpectNotNull(out);
  47538. wc_PKCS7_Free(p7Ver);
  47539. p7Ver = NULL;
  47540. #endif /* !NO_PKCS7_STREAM */
  47541. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47542. PKCS7_free(p7);
  47543. p7 = NULL;
  47544. }
  47545. X509_STORE_free(store);
  47546. X509_free(caCert);
  47547. X509_free(signCert);
  47548. EVP_PKEY_free(signKey);
  47549. BIO_free(inBio);
  47550. BIO_free(keyBio);
  47551. BIO_free(certBio);
  47552. BIO_free(caBio);
  47553. #endif
  47554. return EXPECT_RESULT();
  47555. }
  47556. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  47557. {
  47558. EXPECT_DECLS;
  47559. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  47560. PKCS7_SIGNED* pkcs7 = NULL;
  47561. ExpectNotNull(pkcs7 = PKCS7_SIGNED_new());
  47562. ExpectIntEQ(pkcs7->contentOID, SIGNED_DATA);
  47563. PKCS7_SIGNED_free(pkcs7);
  47564. #endif
  47565. return EXPECT_RESULT();
  47566. }
  47567. #ifndef NO_BIO
  47568. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  47569. {
  47570. EXPECT_DECLS;
  47571. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  47572. PKCS7* pkcs7 = NULL;
  47573. BIO* bio = NULL;
  47574. const byte* cert_buf = NULL;
  47575. int ret = 0;
  47576. WC_RNG rng;
  47577. const byte data[] = { /* Hello World */
  47578. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  47579. 0x72,0x6c,0x64
  47580. };
  47581. #ifndef NO_RSA
  47582. #if defined(USE_CERT_BUFFERS_2048)
  47583. byte key[sizeof(client_key_der_2048)];
  47584. byte cert[sizeof(client_cert_der_2048)];
  47585. word32 keySz = (word32)sizeof(key);
  47586. word32 certSz = (word32)sizeof(cert);
  47587. XMEMSET(key, 0, keySz);
  47588. XMEMSET(cert, 0, certSz);
  47589. XMEMCPY(key, client_key_der_2048, keySz);
  47590. XMEMCPY(cert, client_cert_der_2048, certSz);
  47591. #elif defined(USE_CERT_BUFFERS_1024)
  47592. byte key[sizeof_client_key_der_1024];
  47593. byte cert[sizeof(sizeof_client_cert_der_1024)];
  47594. word32 keySz = (word32)sizeof(key);
  47595. word32 certSz = (word32)sizeof(cert);
  47596. XMEMSET(key, 0, keySz);
  47597. XMEMSET(cert, 0, certSz);
  47598. XMEMCPY(key, client_key_der_1024, keySz);
  47599. XMEMCPY(cert, client_cert_der_1024, certSz);
  47600. #else
  47601. unsigned char cert[ONEK_BUF];
  47602. unsigned char key[ONEK_BUF];
  47603. XFILE fp = XBADFILE;
  47604. int certSz;
  47605. int keySz;
  47606. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  47607. XBADFILE);
  47608. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  47609. fp), 0);
  47610. if (fp != XBADFILE) {
  47611. XFCLOSE(fp);
  47612. fp = XBADFILE;
  47613. }
  47614. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  47615. XBADFILE);
  47616. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  47617. 0);
  47618. if (fp != XBADFILE) {
  47619. XFCLOSE(fp);
  47620. fp = XBADFILE;
  47621. }
  47622. #endif
  47623. #elif defined(HAVE_ECC)
  47624. #if defined(USE_CERT_BUFFERS_256)
  47625. unsigned char cert[sizeof(cliecc_cert_der_256)];
  47626. unsigned char key[sizeof(ecc_clikey_der_256)];
  47627. int certSz = (int)sizeof(cert);
  47628. int keySz = (int)sizeof(key);
  47629. XMEMSET(cert, 0, certSz);
  47630. XMEMSET(key, 0, keySz);
  47631. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  47632. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  47633. #else
  47634. unsigned char cert[ONEK_BUF];
  47635. unsigned char key[ONEK_BUF];
  47636. XFILE fp = XBADFILE;
  47637. int certSz, keySz;
  47638. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  47639. XBADFILE);
  47640. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  47641. fp), 0);
  47642. if (fp != XBADFILE) {
  47643. XFCLOSE(fp);
  47644. fp = XBADFILE;
  47645. }
  47646. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  47647. XBADFILE);
  47648. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  47649. 0);
  47650. if (fp != XBADFILE) {
  47651. XFCLOSE(fp);
  47652. fp = XBADFILE;
  47653. }
  47654. #endif
  47655. #else
  47656. #error PKCS7 requires ECC or RSA
  47657. #endif
  47658. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  47659. /* initialize with DER encoded cert */
  47660. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  47661. /* init rng */
  47662. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47663. ExpectIntEQ(wc_InitRng(&rng), 0);
  47664. if (pkcs7 != NULL) {
  47665. pkcs7->rng = &rng;
  47666. pkcs7->content = (byte*)data; /* not used for ex */
  47667. pkcs7->contentSz = (word32)sizeof(data);
  47668. pkcs7->contentOID = SIGNED_DATA;
  47669. pkcs7->privateKey = key;
  47670. pkcs7->privateKeySz = (word32)sizeof(key);
  47671. pkcs7->encryptOID = RSAk;
  47672. #ifdef NO_SHA
  47673. pkcs7->hashOID = SHA256h;
  47674. #else
  47675. pkcs7->hashOID = SHAh;
  47676. #endif
  47677. pkcs7->signedAttribs = NULL;
  47678. pkcs7->signedAttribsSz = 0;
  47679. }
  47680. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  47681. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  47682. ExpectIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  47683. /* Read PKCS#7 PEM from BIO */
  47684. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  47685. ExpectIntGE(ret, 0);
  47686. BIO_free(bio);
  47687. wc_PKCS7_Free(pkcs7);
  47688. wc_FreeRng(&rng);
  47689. #endif
  47690. return EXPECT_RESULT();
  47691. }
  47692. #ifdef HAVE_SMIME
  47693. static int test_wolfSSL_SMIME_read_PKCS7(void)
  47694. {
  47695. EXPECT_DECLS;
  47696. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  47697. !defined(NO_RSA)
  47698. PKCS7* pkcs7 = NULL;
  47699. BIO* bio = NULL;
  47700. BIO* bcont = NULL;
  47701. BIO* out = NULL;
  47702. const byte* outBuf = NULL;
  47703. int outBufLen = 0;
  47704. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  47705. XFILE smimeTestFile = XBADFILE;
  47706. ExpectTrue((smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r")) !=
  47707. XBADFILE);
  47708. /* smime-test.p7s */
  47709. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  47710. ExpectNotNull(bio);
  47711. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47712. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47713. ExpectNotNull(pkcs7);
  47714. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47715. PKCS7_NOVERIFY), SSL_SUCCESS);
  47716. XFCLOSE(smimeTestFile);
  47717. if (bcont) BIO_free(bcont);
  47718. bcont = NULL;
  47719. wolfSSL_PKCS7_free(pkcs7);
  47720. pkcs7 = NULL;
  47721. /* smime-test-multipart.p7s */
  47722. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  47723. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47724. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47725. ExpectNotNull(pkcs7);
  47726. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47727. PKCS7_NOVERIFY), SSL_SUCCESS);
  47728. XFCLOSE(smimeTestFile);
  47729. if (bcont) BIO_free(bcont);
  47730. bcont = NULL;
  47731. wolfSSL_PKCS7_free(pkcs7);
  47732. pkcs7 = NULL;
  47733. /* smime-test-multipart-badsig.p7s */
  47734. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  47735. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47736. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47737. ExpectNotNull(pkcs7); /* can read in the unverified smime bundle */
  47738. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47739. PKCS7_NOVERIFY), SSL_FAILURE);
  47740. XFCLOSE(smimeTestFile);
  47741. if (bcont) BIO_free(bcont);
  47742. bcont = NULL;
  47743. wolfSSL_PKCS7_free(pkcs7);
  47744. pkcs7 = NULL;
  47745. /* smime-test-canon.p7s */
  47746. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  47747. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47748. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47749. ExpectNotNull(pkcs7);
  47750. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47751. PKCS7_NOVERIFY), SSL_SUCCESS);
  47752. XFCLOSE(smimeTestFile);
  47753. if (bcont) BIO_free(bcont);
  47754. bcont = NULL;
  47755. wolfSSL_PKCS7_free(pkcs7);
  47756. pkcs7 = NULL;
  47757. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  47758. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  47759. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47760. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47761. ExpectNotNull(pkcs7);
  47762. out = wolfSSL_BIO_new(BIO_s_mem());
  47763. ExpectNotNull(out);
  47764. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  47765. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  47766. ExpectIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  47767. /* Content-Type should not show up at beginning of output buffer */
  47768. ExpectIntGT(outBufLen, XSTRLEN(contTypeText));
  47769. ExpectIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  47770. BIO_free(out);
  47771. BIO_free(bio);
  47772. if (bcont) BIO_free(bcont);
  47773. wolfSSL_PKCS7_free(pkcs7);
  47774. #endif
  47775. return EXPECT_RESULT();
  47776. }
  47777. static int test_wolfSSL_SMIME_write_PKCS7(void)
  47778. {
  47779. EXPECT_DECLS;
  47780. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  47781. PKCS7* p7 = NULL;
  47782. PKCS7* p7Ver = NULL;
  47783. int flags = 0;
  47784. byte data[] = "Test data to encode.";
  47785. const char* cert = "./certs/server-cert.pem";
  47786. const char* key = "./certs/server-key.pem";
  47787. const char* ca = "./certs/ca-cert.pem";
  47788. WOLFSSL_BIO* certBio = NULL;
  47789. WOLFSSL_BIO* keyBio = NULL;
  47790. WOLFSSL_BIO* caBio = NULL;
  47791. WOLFSSL_BIO* inBio = NULL;
  47792. WOLFSSL_BIO* outBio = NULL;
  47793. WOLFSSL_BIO* content = NULL;
  47794. X509* signCert = NULL;
  47795. EVP_PKEY* signKey = NULL;
  47796. X509* caCert = NULL;
  47797. X509_STORE* store = NULL;
  47798. /* read signer cert/key into BIO */
  47799. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  47800. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  47801. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  47802. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  47803. /* read CA cert into store (for verify) */
  47804. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  47805. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  47806. ExpectNotNull(store = X509_STORE_new());
  47807. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  47808. /* generate and verify SMIME: not detached */
  47809. {
  47810. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47811. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47812. flags = PKCS7_STREAM;
  47813. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47814. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  47815. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47816. /* bad arg: out NULL */
  47817. ExpectIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  47818. /* bad arg: pkcs7 NULL */
  47819. ExpectIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  47820. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47821. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  47822. BIO_free(content);
  47823. content = NULL;
  47824. BIO_free(inBio);
  47825. inBio = NULL;
  47826. BIO_free(outBio);
  47827. outBio = NULL;
  47828. PKCS7_free(p7Ver);
  47829. p7Ver = NULL;
  47830. PKCS7_free(p7);
  47831. p7 = NULL;
  47832. }
  47833. /* generate and verify SMIME: not detached, add Content-Type */
  47834. {
  47835. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47836. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47837. flags = PKCS7_STREAM | PKCS7_TEXT;
  47838. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47839. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  47840. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47841. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47842. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  47843. BIO_free(content);
  47844. content = NULL;
  47845. BIO_free(inBio);
  47846. inBio = NULL;
  47847. BIO_free(outBio);
  47848. outBio = NULL;
  47849. PKCS7_free(p7Ver);
  47850. p7Ver = NULL;
  47851. PKCS7_free(p7);
  47852. p7 = NULL;
  47853. }
  47854. /* generate and verify SMIME: detached */
  47855. {
  47856. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47857. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47858. flags = PKCS7_DETACHED | PKCS7_STREAM;
  47859. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47860. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  47861. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47862. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47863. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  47864. BIO_free(content);
  47865. content = NULL;
  47866. BIO_free(inBio);
  47867. inBio = NULL;
  47868. BIO_free(outBio);
  47869. outBio = NULL;
  47870. PKCS7_free(p7Ver);
  47871. p7Ver = NULL;
  47872. PKCS7_free(p7);
  47873. p7 = NULL;
  47874. }
  47875. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  47876. {
  47877. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47878. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47879. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  47880. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47881. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  47882. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47883. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47884. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  47885. BIO_free(content);
  47886. content = NULL;
  47887. BIO_free(inBio);
  47888. inBio = NULL;
  47889. BIO_free(outBio);
  47890. outBio = NULL;
  47891. PKCS7_free(p7Ver);
  47892. p7Ver = NULL;
  47893. PKCS7_free(p7);
  47894. p7 = NULL;
  47895. }
  47896. X509_STORE_free(store);
  47897. X509_free(caCert);
  47898. X509_free(signCert);
  47899. EVP_PKEY_free(signKey);
  47900. BIO_free(keyBio);
  47901. BIO_free(certBio);
  47902. BIO_free(caBio);
  47903. #endif
  47904. return EXPECT_RESULT();
  47905. }
  47906. #endif /* HAVE_SMIME */
  47907. #endif /* !NO_BIO */
  47908. /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
  47909. * returns 0) */
  47910. static int test_X509_STORE_No_SSL_CTX(void)
  47911. {
  47912. EXPECT_DECLS;
  47913. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  47914. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  47915. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  47916. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  47917. defined(HAVE_CRL) && !defined(NO_RSA)
  47918. X509_STORE * store = NULL;
  47919. X509_STORE_CTX * storeCtx = NULL;
  47920. X509_CRL * crl = NULL;
  47921. X509 * ca = NULL;
  47922. X509 * cert = NULL;
  47923. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  47924. const char srvCert[] = "./certs/server-cert.pem";
  47925. const char caCert[] = "./certs/ca-cert.pem";
  47926. const char caDir[] = "./certs/crl/hash_pem";
  47927. XFILE fp = XBADFILE;
  47928. X509_LOOKUP * lookup = NULL;
  47929. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  47930. /* Set up store with CA */
  47931. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  47932. SSL_FILETYPE_PEM)));
  47933. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  47934. /* Add CRL lookup directory to store
  47935. * NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
  47936. * of crl.pem */
  47937. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  47938. X509_LOOKUP_hash_dir())));
  47939. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
  47940. X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
  47941. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  47942. SSL_SUCCESS);
  47943. /* Add CRL to store NOT containing the verified certificate, which
  47944. * forces use of the CRL lookup directory */
  47945. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  47946. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  47947. NULL, NULL));
  47948. if (fp != XBADFILE)
  47949. XFCLOSE(fp);
  47950. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  47951. /* Create verification context outside of an SSL session */
  47952. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  47953. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  47954. SSL_FILETYPE_PEM)));
  47955. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  47956. /* Perform verification, which should NOT indicate CRL missing due to the
  47957. * store CM's X509 store pointer being NULL */
  47958. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  47959. X509_CRL_free(crl);
  47960. X509_STORE_free(store);
  47961. X509_STORE_CTX_free(storeCtx);
  47962. X509_free(cert);
  47963. X509_free(ca);
  47964. #endif
  47965. return EXPECT_RESULT();
  47966. }
  47967. /* Test of X509 store use outside of SSL context w/ CRL lookup, but
  47968. * with X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
  47969. static int test_X509_LOOKUP_add_dir(void)
  47970. {
  47971. EXPECT_DECLS;
  47972. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  47973. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  47974. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  47975. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  47976. defined(HAVE_CRL) && !defined(NO_RSA)
  47977. X509_STORE * store = NULL;
  47978. X509_STORE_CTX * storeCtx = NULL;
  47979. X509_CRL * crl = NULL;
  47980. X509 * ca = NULL;
  47981. X509 * cert = NULL;
  47982. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  47983. const char srvCert[] = "./certs/server-cert.pem";
  47984. const char caCert[] = "./certs/ca-cert.pem";
  47985. const char caDir[] = "./certs/crl/hash_der";
  47986. XFILE fp = XBADFILE;
  47987. X509_LOOKUP * lookup = NULL;
  47988. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  47989. /* Set up store with CA */
  47990. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  47991. SSL_FILETYPE_PEM)));
  47992. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  47993. /* Add CRL lookup directory to store.
  47994. * Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
  47995. * of crl.der */
  47996. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  47997. X509_LOOKUP_hash_dir())));
  47998. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
  47999. SSL_SUCCESS);
  48000. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  48001. SSL_SUCCESS);
  48002. /* Add CRL to store NOT containing the verified certificate, which
  48003. * forces use of the CRL lookup directory */
  48004. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  48005. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  48006. NULL, NULL));
  48007. if (fp != XBADFILE) {
  48008. XFCLOSE(fp);
  48009. fp = XBADFILE;
  48010. }
  48011. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  48012. /* Create verification context outside of an SSL session */
  48013. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  48014. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  48015. SSL_FILETYPE_PEM)));
  48016. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  48017. /* Perform verification, which should NOT return CRL missing */
  48018. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  48019. X509_CRL_free(crl);
  48020. crl = NULL;
  48021. X509_STORE_free(store);
  48022. store = NULL;
  48023. X509_STORE_CTX_free(storeCtx);
  48024. storeCtx = NULL;
  48025. X509_free(cert);
  48026. cert = NULL;
  48027. X509_free(ca);
  48028. ca = NULL;
  48029. /* Now repeat the same, but look for X509_FILETYPE_PEM.
  48030. * We should get CRL_MISSING at the end, because the lookup
  48031. * dir has only ASN1 CRLs. */
  48032. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  48033. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  48034. SSL_FILETYPE_PEM)));
  48035. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  48036. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  48037. X509_LOOKUP_hash_dir())));
  48038. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
  48039. SSL_SUCCESS);
  48040. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  48041. SSL_SUCCESS);
  48042. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  48043. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  48044. NULL, NULL));
  48045. if (fp != XBADFILE) {
  48046. XFCLOSE(fp);
  48047. fp = XBADFILE;
  48048. }
  48049. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  48050. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  48051. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  48052. SSL_FILETYPE_PEM)));
  48053. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  48054. /* Now we SHOULD get CRL_MISSING, because we looked for PEM
  48055. * in dir containing only ASN1/DER. */
  48056. ExpectIntEQ(X509_verify_cert(storeCtx), WOLFSSL_FAILURE);
  48057. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_MISSING);
  48058. X509_CRL_free(crl);
  48059. X509_STORE_free(store);
  48060. X509_STORE_CTX_free(storeCtx);
  48061. X509_free(cert);
  48062. X509_free(ca);
  48063. #endif
  48064. return EXPECT_RESULT();
  48065. }
  48066. /*----------------------------------------------------------------------------*
  48067. | Certificate Failure Checks
  48068. *----------------------------------------------------------------------------*/
  48069. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  48070. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  48071. #if !defined(NO_RSA) || defined(HAVE_ECC)
  48072. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  48073. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  48074. int type)
  48075. {
  48076. int ret;
  48077. WOLFSSL_CERT_MANAGER* cm = NULL;
  48078. switch (type) {
  48079. case TESTING_RSA:
  48080. #ifdef NO_RSA
  48081. fprintf(stderr, "RSA disabled, skipping test\n");
  48082. return ASN_SIG_CONFIRM_E;
  48083. #else
  48084. break;
  48085. #endif
  48086. case TESTING_ECC:
  48087. #ifndef HAVE_ECC
  48088. fprintf(stderr, "ECC disabled, skipping test\n");
  48089. return ASN_SIG_CONFIRM_E;
  48090. #else
  48091. break;
  48092. #endif
  48093. default:
  48094. fprintf(stderr, "Bad function argument\n");
  48095. return BAD_FUNC_ARG;
  48096. }
  48097. cm = wolfSSL_CertManagerNew();
  48098. if (cm == NULL) {
  48099. fprintf(stderr, "wolfSSL_CertManagerNew failed\n");
  48100. return -1;
  48101. }
  48102. #ifndef NO_FILESYSTEM
  48103. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  48104. if (ret != WOLFSSL_SUCCESS) {
  48105. fprintf(stderr, "wolfSSL_CertManagerLoadCA failed\n");
  48106. wolfSSL_CertManagerFree(cm);
  48107. return ret;
  48108. }
  48109. #else
  48110. (void)ca;
  48111. #endif
  48112. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz,
  48113. WOLFSSL_FILETYPE_ASN1);
  48114. /* Let ExpectIntEQ handle return code */
  48115. wolfSSL_CertManagerFree(cm);
  48116. return ret;
  48117. }
  48118. #endif
  48119. #if !defined(NO_FILESYSTEM)
  48120. static int test_RsaSigFailure_cm(void)
  48121. {
  48122. EXPECT_DECLS;
  48123. #ifndef NO_RSA
  48124. const char* ca_cert = "./certs/ca-cert.pem";
  48125. const char* server_cert = "./certs/server-cert.der";
  48126. byte* cert_buf = NULL;
  48127. size_t cert_sz = 0;
  48128. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  48129. if (cert_buf != NULL) {
  48130. /* corrupt DER - invert last byte, which is signature */
  48131. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  48132. /* test bad cert */
  48133. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  48134. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  48135. WOLFSSL_FATAL_ERROR);
  48136. #else
  48137. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  48138. ASN_SIG_CONFIRM_E);
  48139. #endif
  48140. }
  48141. /* load_file() uses malloc. */
  48142. if (cert_buf != NULL) {
  48143. free(cert_buf);
  48144. }
  48145. #endif /* !NO_RSA */
  48146. return EXPECT_RESULT();
  48147. }
  48148. static int test_EccSigFailure_cm(void)
  48149. {
  48150. EXPECT_DECLS;
  48151. #ifdef HAVE_ECC
  48152. /* self-signed ECC cert, so use server cert as CA */
  48153. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  48154. const char* server_cert = "./certs/server-ecc.der";
  48155. byte* cert_buf = NULL;
  48156. size_t cert_sz = 0;
  48157. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  48158. if (cert_buf != NULL) {
  48159. /* corrupt DER - invert last byte, which is signature */
  48160. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  48161. /* test bad cert */
  48162. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  48163. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  48164. WOLFSSL_FATAL_ERROR);
  48165. #else
  48166. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  48167. ASN_SIG_CONFIRM_E);
  48168. #endif
  48169. }
  48170. /* load_file() uses malloc. */
  48171. if (cert_buf != NULL) {
  48172. free(cert_buf);
  48173. }
  48174. #ifdef FP_ECC
  48175. wc_ecc_fp_free();
  48176. #endif
  48177. #endif /* HAVE_ECC */
  48178. return EXPECT_RESULT();
  48179. }
  48180. #endif /* !NO_FILESYSTEM */
  48181. #endif /* NO_CERTS */
  48182. #ifdef WOLFSSL_TLS13
  48183. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  48184. #ifdef WC_SHA384_DIGEST_SIZE
  48185. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  48186. #else
  48187. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  48188. #endif
  48189. #endif
  48190. #ifdef WOLFSSL_EARLY_DATA
  48191. static const char earlyData[] = "Early Data";
  48192. static char earlyDataBuffer[1];
  48193. #endif
  48194. static int test_tls13_apis(void)
  48195. {
  48196. EXPECT_DECLS;
  48197. int ret;
  48198. #ifndef WOLFSSL_NO_TLS12
  48199. #ifndef NO_WOLFSSL_CLIENT
  48200. WOLFSSL_CTX* clientTls12Ctx = NULL;
  48201. WOLFSSL* clientTls12Ssl = NULL;
  48202. #endif
  48203. #ifndef NO_WOLFSSL_SERVER
  48204. WOLFSSL_CTX* serverTls12Ctx = NULL;
  48205. WOLFSSL* serverTls12Ssl = NULL;
  48206. #endif
  48207. #endif
  48208. #ifndef NO_WOLFSSL_CLIENT
  48209. WOLFSSL_CTX* clientCtx = NULL;
  48210. WOLFSSL* clientSsl = NULL;
  48211. #endif
  48212. #ifndef NO_WOLFSSL_SERVER
  48213. WOLFSSL_CTX* serverCtx = NULL;
  48214. WOLFSSL* serverSsl = NULL;
  48215. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  48216. const char* ourCert = svrCertFile;
  48217. const char* ourKey = svrKeyFile;
  48218. #endif
  48219. #endif
  48220. int required;
  48221. #ifdef WOLFSSL_EARLY_DATA
  48222. int outSz;
  48223. #endif
  48224. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  48225. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  48226. #ifdef HAVE_PQC
  48227. WOLFSSL_KYBER_LEVEL1
  48228. #else
  48229. WOLFSSL_ECC_SECP256R1
  48230. #endif
  48231. };
  48232. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  48233. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  48234. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  48235. int numGroups = 2;
  48236. #endif
  48237. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  48238. char groupList[] =
  48239. #ifndef NO_ECC_SECP
  48240. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  48241. "P-521:"
  48242. #endif
  48243. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  48244. "P-384:"
  48245. #endif
  48246. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  48247. "P-256"
  48248. #if defined(HAVE_PQC) && defined(HAVE_LIBOQS)
  48249. ":P256_KYBER_LEVEL1"
  48250. #endif
  48251. #endif
  48252. #endif /* !defined(NO_ECC_SECP) */
  48253. #ifdef HAVE_PQC
  48254. ":KYBER_LEVEL1"
  48255. #endif
  48256. "";
  48257. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  48258. (void)ret;
  48259. #ifndef WOLFSSL_NO_TLS12
  48260. #ifndef NO_WOLFSSL_CLIENT
  48261. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  48262. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  48263. #endif
  48264. #ifndef NO_WOLFSSL_SERVER
  48265. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  48266. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  48267. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  48268. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey,
  48269. WOLFSSL_FILETYPE_PEM);
  48270. #endif
  48271. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  48272. #endif
  48273. #endif
  48274. #ifndef NO_WOLFSSL_CLIENT
  48275. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  48276. clientSsl = wolfSSL_new(clientCtx);
  48277. #endif
  48278. #ifndef NO_WOLFSSL_SERVER
  48279. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  48280. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  48281. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  48282. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  48283. #endif
  48284. serverSsl = wolfSSL_new(serverCtx);
  48285. ExpectNotNull(serverSsl);
  48286. #endif
  48287. #ifdef WOLFSSL_SEND_HRR_COOKIE
  48288. ExpectIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  48289. #ifndef NO_WOLFSSL_CLIENT
  48290. ExpectIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  48291. #endif
  48292. #ifndef NO_WOLFSSL_SERVER
  48293. #ifndef WOLFSSL_NO_TLS12
  48294. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0),
  48295. BAD_FUNC_ARG);
  48296. #endif
  48297. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  48298. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  48299. WOLFSSL_SUCCESS);
  48300. #endif
  48301. #endif
  48302. #ifdef HAVE_SUPPORTED_CURVES
  48303. #ifdef HAVE_ECC
  48304. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  48305. BAD_FUNC_ARG);
  48306. #ifndef NO_WOLFSSL_SERVER
  48307. do {
  48308. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  48309. #ifdef WOLFSSL_ASYNC_CRYPT
  48310. if (ret == WC_PENDING_E)
  48311. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  48312. #endif
  48313. }
  48314. while (ret == WC_PENDING_E);
  48315. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  48316. #endif
  48317. #ifndef NO_WOLFSSL_CLIENT
  48318. #ifndef WOLFSSL_NO_TLS12
  48319. do {
  48320. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  48321. #ifdef WOLFSSL_ASYNC_CRYPT
  48322. if (ret == WC_PENDING_E)
  48323. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  48324. #endif
  48325. }
  48326. while (ret == WC_PENDING_E);
  48327. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  48328. #endif
  48329. do {
  48330. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  48331. #ifdef WOLFSSL_ASYNC_CRYPT
  48332. if (ret == WC_PENDING_E)
  48333. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  48334. #endif
  48335. }
  48336. while (ret == WC_PENDING_E);
  48337. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  48338. #endif
  48339. #elif defined(HAVE_CURVE25519)
  48340. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  48341. #ifndef NO_WOLFSSL_SERVER
  48342. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  48343. WOLFSSL_SUCCESS);
  48344. #endif
  48345. #ifndef NO_WOLFSSL_CLIENT
  48346. #ifndef WOLFSSL_NO_TLS12
  48347. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  48348. WOLFSSL_SUCCESS);
  48349. #endif
  48350. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  48351. WOLFSSL_SUCCESS);
  48352. #endif
  48353. #elif defined(HAVE_CURVE448)
  48354. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  48355. #ifndef NO_WOLFSSL_SERVER
  48356. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  48357. WOLFSSL_SUCCESS);
  48358. #endif
  48359. #ifndef NO_WOLFSSL_CLIENT
  48360. #ifndef WOLFSSL_NO_TLS12
  48361. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  48362. WOLFSSL_SUCCESS);
  48363. #endif
  48364. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  48365. WOLFSSL_SUCCESS);
  48366. #endif
  48367. #else
  48368. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  48369. BAD_FUNC_ARG);
  48370. #ifndef NO_WOLFSSL_CLIENT
  48371. #ifndef WOLFSSL_NO_TLS12
  48372. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  48373. NOT_COMPILED_IN);
  48374. #endif
  48375. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  48376. NOT_COMPILED_IN);
  48377. #endif
  48378. #endif
  48379. #if defined(HAVE_PQC)
  48380. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  48381. #ifndef NO_WOLFSSL_SERVER
  48382. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  48383. WOLFSSL_SUCCESS);
  48384. #endif
  48385. #ifndef NO_WOLFSSL_CLIENT
  48386. #ifndef WOLFSSL_NO_TLS12
  48387. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  48388. BAD_FUNC_ARG);
  48389. #endif
  48390. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  48391. WOLFSSL_SUCCESS);
  48392. #endif
  48393. #endif
  48394. ExpectIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  48395. #ifndef NO_WOLFSSL_SERVER
  48396. ExpectIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  48397. #endif
  48398. #ifndef NO_WOLFSSL_CLIENT
  48399. #ifndef WOLFSSL_NO_TLS12
  48400. ExpectIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  48401. #endif
  48402. ExpectIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  48403. #endif
  48404. #endif /* HAVE_SUPPORTED_CURVES */
  48405. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  48406. #ifndef NO_WOLFSSL_CLIENT
  48407. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  48408. #endif
  48409. #ifndef NO_WOLFSSL_SERVER
  48410. #ifndef WOLFSSL_NO_TLS12
  48411. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  48412. #endif
  48413. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  48414. #endif
  48415. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  48416. #ifndef NO_WOLFSSL_CLIENT
  48417. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  48418. #endif
  48419. #ifndef NO_WOLFSSL_SERVER
  48420. #ifndef WOLFSSL_NO_TLS12
  48421. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  48422. #endif
  48423. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  48424. #endif
  48425. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  48426. #ifndef NO_WOLFSSL_CLIENT
  48427. #ifndef WOLFSSL_NO_TLS12
  48428. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  48429. #endif
  48430. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  48431. #endif
  48432. #ifndef NO_WOLFSSL_SERVER
  48433. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  48434. #endif
  48435. ExpectIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  48436. #ifndef NO_WOLFSSL_CLIENT
  48437. #ifndef WOLFSSL_NO_TLS12
  48438. ExpectIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  48439. #endif
  48440. ExpectIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  48441. #endif
  48442. #ifndef NO_WOLFSSL_SERVER
  48443. ExpectIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  48444. #endif
  48445. ExpectIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  48446. #ifndef NO_WOLFSSL_CLIENT
  48447. #ifndef WOLFSSL_NO_TLS12
  48448. ExpectIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  48449. #endif
  48450. ExpectIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  48451. #endif
  48452. #ifndef NO_WOLFSSL_SERVER
  48453. ExpectIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  48454. #endif
  48455. ExpectIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  48456. ExpectIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  48457. #ifndef NO_WOLFSSL_CLIENT
  48458. #ifndef WOLFSSL_NO_TLS12
  48459. ExpectIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  48460. BAD_FUNC_ARG);
  48461. #endif
  48462. ExpectIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  48463. #endif
  48464. #ifndef NO_WOLFSSL_SERVER
  48465. ExpectIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  48466. #endif
  48467. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  48468. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  48469. #ifndef NO_WOLFSSL_SERVER
  48470. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  48471. #endif
  48472. #ifndef NO_WOLFSSL_CLIENT
  48473. #ifndef WOLFSSL_NO_TLS12
  48474. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  48475. BAD_FUNC_ARG);
  48476. #endif
  48477. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  48478. #endif
  48479. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  48480. #ifndef NO_WOLFSSL_SERVER
  48481. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  48482. #endif
  48483. #ifndef NO_WOLFSSL_CLIENT
  48484. #ifndef WOLFSSL_NO_TLS12
  48485. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  48486. BAD_FUNC_ARG);
  48487. #endif
  48488. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  48489. #endif
  48490. ExpectIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  48491. #ifndef NO_WOLFSSL_CLIENT
  48492. ExpectIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  48493. #endif
  48494. #ifndef NO_WOLFSSL_SERVER
  48495. #ifndef WOLFSSL_NO_TLS12
  48496. ExpectIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  48497. BAD_FUNC_ARG);
  48498. #endif
  48499. ExpectIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  48500. #endif
  48501. #endif
  48502. #ifdef HAVE_ECC
  48503. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  48504. ExpectIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  48505. #ifndef NO_WOLFSSL_SERVER
  48506. ExpectIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  48507. #endif
  48508. #ifndef NO_WOLFSSL_CLIENT
  48509. #ifndef WOLFSSL_NO_TLS12
  48510. ExpectIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  48511. #endif
  48512. ExpectIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  48513. #endif
  48514. #endif
  48515. #ifdef HAVE_SUPPORTED_CURVES
  48516. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  48517. #ifndef NO_WOLFSSL_CLIENT
  48518. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  48519. #endif
  48520. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  48521. #ifndef NO_WOLFSSL_CLIENT
  48522. #ifndef WOLFSSL_NO_TLS12
  48523. ExpectIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  48524. BAD_FUNC_ARG);
  48525. #endif
  48526. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  48527. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  48528. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  48529. WOLFSSL_SUCCESS);
  48530. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  48531. BAD_FUNC_ARG);
  48532. #endif
  48533. #ifndef NO_WOLFSSL_SERVER
  48534. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  48535. WOLFSSL_SUCCESS);
  48536. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  48537. BAD_FUNC_ARG);
  48538. #endif
  48539. ExpectIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  48540. #ifndef NO_WOLFSSL_CLIENT
  48541. ExpectIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  48542. #endif
  48543. ExpectIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  48544. #ifndef NO_WOLFSSL_CLIENT
  48545. #ifndef WOLFSSL_NO_TLS12
  48546. ExpectIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  48547. BAD_FUNC_ARG);
  48548. #endif
  48549. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups,
  48550. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  48551. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  48552. WOLFSSL_SUCCESS);
  48553. ExpectIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  48554. BAD_FUNC_ARG);
  48555. #endif
  48556. #ifndef NO_WOLFSSL_SERVER
  48557. ExpectIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  48558. WOLFSSL_SUCCESS);
  48559. ExpectIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  48560. BAD_FUNC_ARG);
  48561. #endif
  48562. #ifdef OPENSSL_EXTRA
  48563. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  48564. #ifndef NO_WOLFSSL_CLIENT
  48565. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL),
  48566. WOLFSSL_FAILURE);
  48567. #endif
  48568. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList),
  48569. WOLFSSL_FAILURE);
  48570. #ifndef NO_WOLFSSL_CLIENT
  48571. #ifndef WOLFSSL_NO_TLS12
  48572. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  48573. WOLFSSL_FAILURE);
  48574. #endif
  48575. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  48576. WOLFSSL_SUCCESS);
  48577. #endif
  48578. #ifndef NO_WOLFSSL_SERVER
  48579. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  48580. WOLFSSL_SUCCESS);
  48581. #endif
  48582. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  48583. #ifndef NO_WOLFSSL_CLIENT
  48584. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  48585. #endif
  48586. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  48587. #ifndef NO_WOLFSSL_CLIENT
  48588. #ifndef WOLFSSL_NO_TLS12
  48589. ExpectIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  48590. WOLFSSL_FAILURE);
  48591. #endif
  48592. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  48593. WOLFSSL_SUCCESS);
  48594. #endif
  48595. #ifndef NO_WOLFSSL_SERVER
  48596. ExpectIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  48597. WOLFSSL_SUCCESS);
  48598. #endif
  48599. #endif /* OPENSSL_EXTRA */
  48600. #endif /* HAVE_SUPPORTED_CURVES */
  48601. #endif /* HAVE_ECC */
  48602. #ifdef WOLFSSL_EARLY_DATA
  48603. #ifndef OPENSSL_EXTRA
  48604. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  48605. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  48606. #else
  48607. ExpectIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  48608. ExpectIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  48609. #endif
  48610. #ifndef NO_WOLFSSL_CLIENT
  48611. #ifndef OPENSSL_EXTRA
  48612. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  48613. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  48614. #else
  48615. ExpectIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  48616. ExpectIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  48617. #endif
  48618. #endif
  48619. #ifndef NO_WOLFSSL_SERVER
  48620. #ifndef WOLFSSL_NO_TLS12
  48621. #ifndef OPENSSL_EXTRA
  48622. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  48623. BAD_FUNC_ARG);
  48624. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  48625. #else
  48626. ExpectIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  48627. BAD_FUNC_ARG);
  48628. ExpectIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  48629. #endif
  48630. #endif
  48631. #ifndef OPENSSL_EXTRA
  48632. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  48633. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32),
  48634. WOLFSSL_SUCCESS);
  48635. #else
  48636. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  48637. #endif
  48638. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  48639. #else
  48640. ExpectIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  48641. ExpectIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  48642. #endif
  48643. #endif
  48644. #ifndef OPENSSL_EXTRA
  48645. ExpectIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  48646. ExpectIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  48647. #else
  48648. ExpectIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  48649. ExpectIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  48650. #endif
  48651. #ifndef NO_WOLFSSL_CLIENT
  48652. #ifndef OPENSSL_EXTRA
  48653. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  48654. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  48655. #else
  48656. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
  48657. #endif
  48658. ExpectIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
  48659. #else
  48660. ExpectIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  48661. ExpectIntEQ(SSL_get_max_early_data(clientSsl), 17);
  48662. #endif
  48663. #endif
  48664. #ifndef NO_WOLFSSL_SERVER
  48665. #ifndef WOLFSSL_NO_TLS12
  48666. #ifndef OPENSSL_EXTRA
  48667. ExpectIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  48668. ExpectIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  48669. #else
  48670. ExpectIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  48671. ExpectIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  48672. #endif
  48673. #endif
  48674. #ifndef OPENSSL_EXTRA
  48675. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  48676. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), WOLFSSL_SUCCESS);
  48677. #else
  48678. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  48679. #endif
  48680. ExpectIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  48681. #else
  48682. ExpectIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  48683. ExpectIntEQ(SSL_get_max_early_data(serverSsl), 16);
  48684. #endif
  48685. #endif
  48686. ExpectIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  48687. &outSz), BAD_FUNC_ARG);
  48688. #ifndef NO_WOLFSSL_CLIENT
  48689. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  48690. &outSz), BAD_FUNC_ARG);
  48691. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  48692. BAD_FUNC_ARG);
  48693. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  48694. sizeof(earlyData), NULL), BAD_FUNC_ARG);
  48695. #endif
  48696. #ifndef NO_WOLFSSL_SERVER
  48697. ExpectIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  48698. sizeof(earlyData), &outSz), SIDE_ERROR);
  48699. #endif
  48700. #ifndef NO_WOLFSSL_CLIENT
  48701. #ifndef WOLFSSL_NO_TLS12
  48702. ExpectIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  48703. sizeof(earlyData), &outSz), BAD_FUNC_ARG);
  48704. #endif
  48705. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  48706. sizeof(earlyData), &outSz), WOLFSSL_FATAL_ERROR);
  48707. #endif
  48708. ExpectIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  48709. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  48710. #ifndef NO_WOLFSSL_SERVER
  48711. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  48712. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  48713. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1,
  48714. &outSz), BAD_FUNC_ARG);
  48715. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  48716. sizeof(earlyDataBuffer), NULL), BAD_FUNC_ARG);
  48717. #endif
  48718. #ifndef NO_WOLFSSL_CLIENT
  48719. ExpectIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  48720. sizeof(earlyDataBuffer), &outSz), SIDE_ERROR);
  48721. #endif
  48722. #ifndef NO_WOLFSSL_SERVER
  48723. #ifndef WOLFSSL_NO_TLS12
  48724. ExpectIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  48725. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  48726. #endif
  48727. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  48728. sizeof(earlyDataBuffer), &outSz), WOLFSSL_FATAL_ERROR);
  48729. #endif
  48730. #endif
  48731. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  48732. ExpectIntLT(SSL_get_early_data_status(NULL), 0);
  48733. #endif
  48734. #ifndef NO_WOLFSSL_SERVER
  48735. wolfSSL_free(serverSsl);
  48736. wolfSSL_CTX_free(serverCtx);
  48737. #endif
  48738. #ifndef NO_WOLFSSL_CLIENT
  48739. wolfSSL_free(clientSsl);
  48740. wolfSSL_CTX_free(clientCtx);
  48741. #endif
  48742. #ifndef WOLFSSL_NO_TLS12
  48743. #ifndef NO_WOLFSSL_SERVER
  48744. wolfSSL_free(serverTls12Ssl);
  48745. wolfSSL_CTX_free(serverTls12Ctx);
  48746. #endif
  48747. #ifndef NO_WOLFSSL_CLIENT
  48748. wolfSSL_free(clientTls12Ssl);
  48749. wolfSSL_CTX_free(clientTls12Ctx);
  48750. #endif
  48751. #endif
  48752. return EXPECT_RESULT();
  48753. }
  48754. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  48755. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  48756. defined(BUILD_TLS_AES_256_GCM_SHA384)
  48757. /* Called when writing. */
  48758. static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  48759. {
  48760. (void)ssl;
  48761. (void)buf;
  48762. (void)sz;
  48763. (void)ctx;
  48764. /* Force error return from wolfSSL_accept_TLSv13(). */
  48765. return WANT_WRITE;
  48766. }
  48767. /* Called when reading. */
  48768. static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  48769. {
  48770. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  48771. int len = (int)msg->length;
  48772. (void)ssl;
  48773. (void)sz;
  48774. /* Pass back as much of message as will fit in buffer. */
  48775. if (len > sz)
  48776. len = sz;
  48777. XMEMCPY(buf, msg->buffer, len);
  48778. /* Move over returned data. */
  48779. msg->buffer += len;
  48780. msg->length -= len;
  48781. /* Amount actually copied. */
  48782. return len;
  48783. }
  48784. #endif
  48785. static int test_tls13_cipher_suites(void)
  48786. {
  48787. EXPECT_DECLS;
  48788. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  48789. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  48790. defined(BUILD_TLS_AES_256_GCM_SHA384)
  48791. WOLFSSL_CTX* ctx = NULL;
  48792. WOLFSSL *ssl = NULL;
  48793. int i;
  48794. byte clientHello[] = {
  48795. 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
  48796. 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  48797. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  48798. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  48799. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  48800. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  48801. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  48802. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  48803. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48804. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  48805. /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
  48806. 0x13, 0x01,
  48807. 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
  48808. 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
  48809. 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
  48810. 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
  48811. 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
  48812. 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
  48813. 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
  48814. 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
  48815. 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
  48816. 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
  48817. 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
  48818. 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
  48819. 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
  48820. 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
  48821. 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
  48822. 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
  48823. 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
  48824. 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
  48825. 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
  48826. 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
  48827. 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
  48828. 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
  48829. 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
  48830. 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
  48831. 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
  48832. 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
  48833. 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
  48834. 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
  48835. 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
  48836. 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
  48837. 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
  48838. 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
  48839. 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
  48840. 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
  48841. 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
  48842. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48843. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48844. 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
  48845. 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
  48846. 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
  48847. 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
  48848. 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
  48849. };
  48850. WOLFSSL_BUFFER_INFO msg;
  48851. /* Offset into ClientHello message data of first cipher suite. */
  48852. const int csOff = 78;
  48853. /* Server cipher list. */
  48854. const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
  48855. /* Suite list with duplicates. */
  48856. const char* dupCs = "TLS13-AES128-GCM-SHA256:"
  48857. "TLS13-AES128-GCM-SHA256:"
  48858. "TLS13-AES256-GCM-SHA384:"
  48859. "TLS13-AES256-GCM-SHA384:"
  48860. "TLS13-AES128-GCM-SHA256";
  48861. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  48862. const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  48863. TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  48864. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  48865. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  48866. TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
  48867. #endif
  48868. /* Set up wolfSSL context. */
  48869. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  48870. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  48871. WOLFSSL_FILETYPE_PEM));
  48872. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  48873. WOLFSSL_FILETYPE_PEM));
  48874. /* Read from 'msg'. */
  48875. wolfSSL_SetIORecv(ctx, CsRecv);
  48876. /* No where to send to - dummy sender. */
  48877. wolfSSL_SetIOSend(ctx, CsSend);
  48878. /* Test cipher suite list with many copies of a cipher suite. */
  48879. ExpectNotNull(ssl = wolfSSL_new(ctx));
  48880. msg.buffer = clientHello;
  48881. msg.length = (unsigned int)sizeof(clientHello);
  48882. wolfSSL_SetIOReadCtx(ssl, &msg);
  48883. /* Force server to have as many occurrences of same cipher suite as
  48884. * possible. */
  48885. if (ssl != NULL) {
  48886. Suites* suites = (Suites*)WOLFSSL_SUITES(ssl);
  48887. suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  48888. for (i = 0; i < suites->suiteSz; i += 2) {
  48889. suites->suites[i + 0] = TLS13_BYTE;
  48890. suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
  48891. }
  48892. }
  48893. /* Test multiple occurrences of same cipher suite. */
  48894. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  48895. wolfSSL_free(ssl);
  48896. ssl = NULL;
  48897. /* Set client order opposite to server order:
  48898. * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
  48899. clientHello[csOff + 0] = TLS13_BYTE;
  48900. clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
  48901. clientHello[csOff + 2] = TLS13_BYTE;
  48902. clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
  48903. /* Test server order negotiation. */
  48904. ExpectNotNull(ssl = wolfSSL_new(ctx));
  48905. msg.buffer = clientHello;
  48906. msg.length = (unsigned int)sizeof(clientHello);
  48907. wolfSSL_SetIOReadCtx(ssl, &msg);
  48908. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  48909. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  48910. /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
  48911. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  48912. /* Check refined order - server order. */
  48913. ExpectIntEQ(ssl->suites->suiteSz, 4);
  48914. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  48915. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
  48916. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  48917. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
  48918. wolfSSL_free(ssl);
  48919. ssl = NULL;
  48920. /* Test client order negotiation. */
  48921. ExpectNotNull(ssl = wolfSSL_new(ctx));
  48922. msg.buffer = clientHello;
  48923. msg.length = (unsigned int)sizeof(clientHello);
  48924. wolfSSL_SetIOReadCtx(ssl, &msg);
  48925. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  48926. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  48927. ExpectIntEQ(wolfSSL_UseClientSuites(ssl), 0);
  48928. /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
  48929. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  48930. /* Check refined order - client order. */
  48931. ExpectIntEQ(ssl->suites->suiteSz, 4);
  48932. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  48933. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
  48934. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  48935. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
  48936. wolfSSL_free(ssl);
  48937. ssl = NULL;
  48938. /* Check duplicate detection is working. */
  48939. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
  48940. ExpectIntEQ(ctx->suites->suiteSz, 4);
  48941. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  48942. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
  48943. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  48944. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
  48945. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  48946. ExpectIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
  48947. sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
  48948. ExpectIntEQ(ctx->suites->suiteSz, 4);
  48949. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  48950. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
  48951. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  48952. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
  48953. #endif
  48954. wolfSSL_CTX_free(ctx);
  48955. #endif
  48956. return EXPECT_RESULT();
  48957. }
  48958. #endif
  48959. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  48960. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  48961. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  48962. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  48963. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  48964. const unsigned char* priv, unsigned int privSz,
  48965. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  48966. unsigned char* out, unsigned int* outlen,
  48967. void* ctx)
  48968. {
  48969. int result;
  48970. /* Test fail when context associated with WOLFSSL is NULL */
  48971. if (ctx == NULL) {
  48972. return -1;
  48973. }
  48974. (void)ssl;
  48975. /* return 0 on success */
  48976. PRIVATE_KEY_UNLOCK();
  48977. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  48978. PRIVATE_KEY_LOCK();
  48979. return result;
  48980. }
  48981. static int test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  48982. EXPECT_DECLS;
  48983. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  48984. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  48985. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  48986. WOLFSSL_SUCCESS);
  48987. #endif
  48988. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  48989. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  48990. WOLFSSL_SUCCESS);
  48991. #endif
  48992. return EXPECT_RESULT();
  48993. }
  48994. static int test_dh_ssl_setup(WOLFSSL* ssl)
  48995. {
  48996. EXPECT_DECLS;
  48997. static int dh_test_ctx = 1;
  48998. int ret;
  48999. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  49000. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  49001. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  49002. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  49003. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  49004. }
  49005. return EXPECT_RESULT();
  49006. }
  49007. static int test_dh_ssl_setup_fail(WOLFSSL* ssl)
  49008. {
  49009. EXPECT_DECLS;
  49010. int ret;
  49011. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  49012. ExpectNull(wolfSSL_GetDhAgreeCtx(ssl));
  49013. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  49014. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  49015. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  49016. }
  49017. return EXPECT_RESULT();
  49018. }
  49019. #endif
  49020. static int test_DhCallbacks(void)
  49021. {
  49022. EXPECT_DECLS;
  49023. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  49024. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  49025. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  49026. WOLFSSL_CTX *ctx = NULL;
  49027. WOLFSSL *ssl = NULL;
  49028. int test;
  49029. test_ssl_cbf func_cb_client;
  49030. test_ssl_cbf func_cb_server;
  49031. /* Test that DH callback APIs work. */
  49032. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  49033. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  49034. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  49035. /* load client ca cert */
  49036. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  49037. WOLFSSL_SUCCESS);
  49038. /* test with NULL arguments */
  49039. wolfSSL_SetDhAgreeCtx(NULL, &test);
  49040. ExpectNull(wolfSSL_GetDhAgreeCtx(NULL));
  49041. /* test success case */
  49042. test = 1;
  49043. ExpectNotNull(ssl = wolfSSL_new(ctx));
  49044. wolfSSL_SetDhAgreeCtx(ssl, &test);
  49045. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  49046. wolfSSL_free(ssl);
  49047. wolfSSL_CTX_free(ctx);
  49048. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  49049. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  49050. /* set callbacks to use DH functions */
  49051. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  49052. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  49053. func_cb_client.method = wolfTLSv1_2_client_method;
  49054. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  49055. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  49056. func_cb_server.method = wolfTLSv1_2_server_method;
  49057. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  49058. &func_cb_server, NULL), TEST_SUCCESS);
  49059. /* Test fail */
  49060. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  49061. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  49062. /* set callbacks to use DH functions */
  49063. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  49064. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  49065. func_cb_client.method = wolfTLSv1_2_client_method;
  49066. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  49067. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  49068. func_cb_server.method = wolfTLSv1_2_server_method;
  49069. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  49070. &func_cb_server, NULL), TEST_FAIL);
  49071. #endif
  49072. return EXPECT_RESULT();
  49073. }
  49074. #endif /* HAVE_PK_CALLBACKS */
  49075. #ifdef HAVE_HASHDRBG
  49076. #ifdef TEST_RESEED_INTERVAL
  49077. static int test_wc_RNG_GenerateBlock_Reseed(void)
  49078. {
  49079. EXPECT_DECLS;
  49080. int i;
  49081. WC_RNG rng;
  49082. byte key[32];
  49083. XMEMSET(&rng, 0, sizeof(WC_RNG));
  49084. ExpectIntEQ(wc_InitRng(&rng), 0);
  49085. for (i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  49086. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  49087. }
  49088. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  49089. return EXPECT_RESULT();
  49090. }
  49091. #endif /* TEST_RESEED_INTERVAL */
  49092. static int test_wc_RNG_GenerateBlock(void)
  49093. {
  49094. EXPECT_DECLS;
  49095. int i;
  49096. WC_RNG rng;
  49097. byte key[32];
  49098. XMEMSET(&rng, 0, sizeof(WC_RNG));
  49099. ExpectIntEQ(wc_InitRng(&rng), 0);
  49100. for (i = 0; i < 10; i++) {
  49101. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  49102. }
  49103. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  49104. return EXPECT_RESULT();
  49105. }
  49106. #endif /* HAVE_HASHDRBG */
  49107. /*
  49108. * Testing get_rand_digit
  49109. */
  49110. static int test_get_rand_digit(void)
  49111. {
  49112. EXPECT_DECLS;
  49113. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  49114. WC_RNG rng;
  49115. mp_digit d;
  49116. XMEMSET(&rng, 0, sizeof(WC_RNG));
  49117. ExpectIntEQ(wc_InitRng(&rng), 0);
  49118. ExpectIntEQ(get_rand_digit(&rng, &d), 0);
  49119. ExpectIntEQ(get_rand_digit(NULL, NULL), BAD_FUNC_ARG);
  49120. ExpectIntEQ(get_rand_digit(NULL, &d), BAD_FUNC_ARG);
  49121. ExpectIntEQ(get_rand_digit(&rng, NULL), BAD_FUNC_ARG);
  49122. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  49123. #endif
  49124. return EXPECT_RESULT();
  49125. } /* End test_get_rand_digit*/
  49126. /*
  49127. * Testing get_digit_count
  49128. */
  49129. static int test_get_digit_count(void)
  49130. {
  49131. EXPECT_DECLS;
  49132. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  49133. mp_int a;
  49134. XMEMSET(&a, 0, sizeof(mp_int));
  49135. ExpectIntEQ(mp_init(&a), 0);
  49136. ExpectIntEQ(get_digit_count(NULL), 0);
  49137. ExpectIntEQ(get_digit_count(&a), 0);
  49138. mp_clear(&a);
  49139. #endif
  49140. return EXPECT_RESULT();
  49141. } /* End test_get_digit_count*/
  49142. /*
  49143. * Testing mp_cond_copy
  49144. */
  49145. static int test_mp_cond_copy(void)
  49146. {
  49147. EXPECT_DECLS;
  49148. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  49149. defined(WOLFSSL_PUBLIC_MP)
  49150. mp_int a;
  49151. mp_int b;
  49152. int copy = 0;
  49153. XMEMSET(&a, 0, sizeof(mp_int));
  49154. XMEMSET(&b, 0, sizeof(mp_int));
  49155. ExpectIntEQ(mp_init(&a), MP_OKAY);
  49156. ExpectIntEQ(mp_init(&b), MP_OKAY);
  49157. ExpectIntEQ(mp_cond_copy(NULL, copy, NULL), BAD_FUNC_ARG);
  49158. ExpectIntEQ(mp_cond_copy(NULL, copy, &b), BAD_FUNC_ARG);
  49159. ExpectIntEQ(mp_cond_copy(&a, copy, NULL), BAD_FUNC_ARG);
  49160. ExpectIntEQ(mp_cond_copy(&a, copy, &b), 0);
  49161. mp_clear(&a);
  49162. mp_clear(&b);
  49163. #endif
  49164. return EXPECT_RESULT();
  49165. } /* End test_mp_cond_copy*/
  49166. /*
  49167. * Testing mp_rand
  49168. */
  49169. static int test_mp_rand(void)
  49170. {
  49171. EXPECT_DECLS;
  49172. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  49173. mp_int a;
  49174. WC_RNG rng;
  49175. int digits = 1;
  49176. XMEMSET(&a, 0, sizeof(mp_int));
  49177. XMEMSET(&rng, 0, sizeof(WC_RNG));
  49178. ExpectIntEQ(mp_init(&a), MP_OKAY);
  49179. ExpectIntEQ(wc_InitRng(&rng), 0);
  49180. ExpectIntEQ(mp_rand(&a, digits, NULL), MISSING_RNG_E);
  49181. ExpectIntEQ(mp_rand(NULL, digits, &rng), BAD_FUNC_ARG);
  49182. ExpectIntEQ(mp_rand(&a, 0, &rng), BAD_FUNC_ARG);
  49183. ExpectIntEQ(mp_rand(&a, digits, &rng), 0);
  49184. mp_clear(&a);
  49185. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  49186. #endif
  49187. return EXPECT_RESULT();
  49188. } /* End test_mp_rand*/
  49189. /*
  49190. * Testing get_digit
  49191. */
  49192. static int test_get_digit(void)
  49193. {
  49194. EXPECT_DECLS;
  49195. #if defined(WOLFSSL_PUBLIC_MP)
  49196. mp_int a;
  49197. int n = 0;
  49198. XMEMSET(&a, 0, sizeof(mp_int));
  49199. ExpectIntEQ(mp_init(&a), MP_OKAY);
  49200. ExpectIntEQ(get_digit(NULL, n), 0);
  49201. ExpectIntEQ(get_digit(&a, n), 0);
  49202. mp_clear(&a);
  49203. #endif
  49204. return EXPECT_RESULT();
  49205. } /* End test_get_digit*/
  49206. /*
  49207. * Testing wc_export_int
  49208. */
  49209. static int test_wc_export_int(void)
  49210. {
  49211. EXPECT_DECLS;
  49212. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  49213. defined(WOLFSSL_PUBLIC_MP)
  49214. mp_int mp;
  49215. byte buf[32];
  49216. word32 keySz = (word32)sizeof(buf);
  49217. word32 len = (word32)sizeof(buf);
  49218. XMEMSET(&mp, 0, sizeof(mp_int));
  49219. ExpectIntEQ(mp_init(&mp), MP_OKAY);
  49220. ExpectIntEQ(mp_set(&mp, 1234), 0);
  49221. ExpectIntEQ(wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  49222. BAD_FUNC_ARG);
  49223. len = sizeof(buf)-1;
  49224. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  49225. BUFFER_E);
  49226. len = sizeof(buf);
  49227. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN), 0);
  49228. len = 4; /* test input too small */
  49229. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), BUFFER_E);
  49230. len = sizeof(buf);
  49231. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), 0);
  49232. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  49233. ExpectIntEQ(len, 5);
  49234. mp_clear(&mp);
  49235. #endif
  49236. return EXPECT_RESULT();
  49237. } /* End test_wc_export_int*/
  49238. static int test_wc_InitRngNonce(void)
  49239. {
  49240. EXPECT_DECLS;
  49241. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  49242. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  49243. HAVE_FIPS_VERSION >= 2))
  49244. WC_RNG rng;
  49245. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  49246. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  49247. word32 nonceSz = sizeof(nonce);
  49248. ExpectIntEQ(wc_InitRngNonce(&rng, nonce, nonceSz), 0);
  49249. ExpectIntEQ(wc_FreeRng(&rng), 0);
  49250. #endif
  49251. return EXPECT_RESULT();
  49252. } /* End test_wc_InitRngNonce*/
  49253. /*
  49254. * Testing wc_InitRngNonce_ex
  49255. */
  49256. static int test_wc_InitRngNonce_ex(void)
  49257. {
  49258. EXPECT_DECLS;
  49259. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  49260. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  49261. HAVE_FIPS_VERSION >= 2))
  49262. WC_RNG rng;
  49263. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  49264. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  49265. word32 nonceSz = sizeof(nonce);
  49266. ExpectIntEQ(wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId),
  49267. 0);
  49268. ExpectIntEQ(wc_FreeRng(&rng), 0);
  49269. #endif
  49270. return EXPECT_RESULT();
  49271. } /* End test_wc_InitRngNonce_ex */
  49272. static int test_wolfSSL_X509_CRL(void)
  49273. {
  49274. EXPECT_DECLS;
  49275. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  49276. X509_CRL *crl = NULL;
  49277. char pem[][100] = {
  49278. "./certs/crl/crl.pem",
  49279. "./certs/crl/crl2.pem",
  49280. "./certs/crl/caEccCrl.pem",
  49281. "./certs/crl/eccCliCRL.pem",
  49282. "./certs/crl/eccSrvCRL.pem",
  49283. ""
  49284. };
  49285. #ifndef NO_BIO
  49286. BIO *bio = NULL;
  49287. #endif
  49288. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  49289. char der[][100] = {
  49290. "./certs/crl/crl.der",
  49291. "./certs/crl/crl2.der",
  49292. ""};
  49293. #endif
  49294. XFILE fp = XBADFILE;
  49295. int i;
  49296. for (i = 0; pem[i][0] != '\0'; i++)
  49297. {
  49298. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  49299. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  49300. NULL, NULL));
  49301. ExpectNotNull(crl);
  49302. X509_CRL_free(crl);
  49303. if (fp != XBADFILE) {
  49304. XFCLOSE(fp);
  49305. fp = XBADFILE;
  49306. }
  49307. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  49308. ExpectNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL,
  49309. NULL));
  49310. if (EXPECT_FAIL()) {
  49311. crl = NULL;
  49312. }
  49313. ExpectNotNull(crl);
  49314. X509_CRL_free(crl);
  49315. crl = NULL;
  49316. if (fp != XBADFILE) {
  49317. XFCLOSE(fp);
  49318. fp = XBADFILE;
  49319. }
  49320. }
  49321. #ifndef NO_BIO
  49322. for (i = 0; pem[i][0] != '\0'; i++)
  49323. {
  49324. ExpectNotNull(bio = BIO_new_file(pem[i], "rb"));
  49325. ExpectNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  49326. X509_CRL_free(crl);
  49327. crl = NULL;
  49328. BIO_free(bio);
  49329. bio = NULL;
  49330. }
  49331. #endif
  49332. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  49333. for (i = 0; der[i][0] != '\0'; i++) {
  49334. ExpectTrue((fp = XFOPEN(der[i], "rb")) != XBADFILE);
  49335. ExpectTrue((fp != XBADFILE));
  49336. ExpectNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  49337. ExpectNotNull(crl);
  49338. X509_CRL_free(crl);
  49339. if (fp != XBADFILE) {
  49340. XFCLOSE(fp);
  49341. fp = XBADFILE;
  49342. }
  49343. fp = XFOPEN(der[i], "rb");
  49344. ExpectTrue((fp != XBADFILE));
  49345. ExpectNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  49346. if (EXPECT_FAIL()) {
  49347. crl = NULL;
  49348. }
  49349. ExpectNotNull(crl);
  49350. X509_CRL_free(crl);
  49351. crl = NULL;
  49352. if (fp != XBADFILE) {
  49353. XFCLOSE(fp);
  49354. fp = XBADFILE;
  49355. }
  49356. }
  49357. #endif
  49358. #endif
  49359. return EXPECT_RESULT();
  49360. }
  49361. static int test_wolfSSL_X509_load_crl_file(void)
  49362. {
  49363. EXPECT_DECLS;
  49364. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  49365. !defined(NO_STDIO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  49366. int i;
  49367. char pem[][100] = {
  49368. "./certs/crl/crl.pem",
  49369. "./certs/crl/crl2.pem",
  49370. "./certs/crl/caEccCrl.pem",
  49371. "./certs/crl/eccCliCRL.pem",
  49372. "./certs/crl/eccSrvCRL.pem",
  49373. #ifdef WC_RSA_PSS
  49374. "./certs/crl/crl_rsapss.pem",
  49375. #endif
  49376. ""
  49377. };
  49378. char der[][100] = {
  49379. "./certs/crl/crl.der",
  49380. "./certs/crl/crl2.der",
  49381. ""
  49382. };
  49383. WOLFSSL_X509_STORE* store = NULL;
  49384. WOLFSSL_X509_LOOKUP* lookup = NULL;
  49385. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  49386. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  49387. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  49388. X509_FILETYPE_PEM), 1);
  49389. #ifdef WC_RSA_PSS
  49390. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/rsapss/ca-rsapss.pem",
  49391. X509_FILETYPE_PEM), 1);
  49392. #endif
  49393. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  49394. X509_FILETYPE_PEM), 1);
  49395. if (store) {
  49396. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  49397. WOLFSSL_FILETYPE_PEM), 1);
  49398. /* since store hasn't yet known the revoked cert*/
  49399. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  49400. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  49401. }
  49402. for (i = 0; pem[i][0] != '\0'; i++) {
  49403. ExpectIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM),
  49404. 1);
  49405. }
  49406. if (store) {
  49407. /* since store knows crl list */
  49408. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  49409. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  49410. CRL_CERT_REVOKED);
  49411. #ifdef WC_RSA_PSS
  49412. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  49413. "certs/rsapss/server-rsapss-cert.pem", WOLFSSL_FILETYPE_PEM),
  49414. CRL_CERT_REVOKED);
  49415. #endif
  49416. }
  49417. /* once feeing store */
  49418. X509_STORE_free(store);
  49419. store = NULL;
  49420. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  49421. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  49422. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  49423. X509_FILETYPE_PEM), 1);
  49424. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  49425. X509_FILETYPE_PEM), 1);
  49426. if (store) {
  49427. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  49428. WOLFSSL_FILETYPE_PEM), 1);
  49429. /* since store hasn't yet known the revoked cert*/
  49430. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  49431. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  49432. }
  49433. for (i = 0; der[i][0] != '\0'; i++) {
  49434. ExpectIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1),
  49435. 1);
  49436. }
  49437. if (store) {
  49438. /* since store knows crl list */
  49439. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  49440. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  49441. CRL_CERT_REVOKED);
  49442. }
  49443. /* test for incorrect parameter */
  49444. ExpectIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  49445. ExpectIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  49446. ExpectIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  49447. X509_STORE_free(store);
  49448. store = NULL;
  49449. #endif
  49450. return EXPECT_RESULT();
  49451. }
  49452. static int test_wolfSSL_i2d_X509(void)
  49453. {
  49454. EXPECT_DECLS;
  49455. #if defined(OPENSSL_EXTRA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  49456. const unsigned char* cert_buf = server_cert_der_2048;
  49457. unsigned char* out = NULL;
  49458. unsigned char* tmp = NULL;
  49459. X509* cert = NULL;
  49460. ExpectNotNull(d2i_X509(&cert, &cert_buf, sizeof_server_cert_der_2048));
  49461. /* Pointer should be advanced */
  49462. ExpectPtrGT(cert_buf, server_cert_der_2048);
  49463. ExpectIntGT(i2d_X509(cert, &out), 0);
  49464. ExpectNotNull(out);
  49465. tmp = out;
  49466. ExpectIntGT(i2d_X509(cert, &tmp), 0);
  49467. ExpectPtrGT(tmp, out);
  49468. if (out != NULL)
  49469. XFREE(out, NULL, DYNAMIC_TYPE_OPENSSL);
  49470. X509_free(cert);
  49471. #endif
  49472. return EXPECT_RESULT();
  49473. }
  49474. static int test_wolfSSL_d2i_X509_REQ(void)
  49475. {
  49476. EXPECT_DECLS;
  49477. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  49478. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  49479. !defined(WOLFSSL_SP_MATH)
  49480. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der
  49481. * were generated by libest
  49482. * ./certs/csr.attr.der contains sample attributes
  49483. * ./certs/csr.ext.der contains sample extensions */
  49484. const char* csrFile = "./certs/csr.signed.der";
  49485. const char* csrPopFile = "./certs/csr.attr.der";
  49486. const char* csrExtFile = "./certs/csr.ext.der";
  49487. /* ./certs/csr.dsa.pem is generated using
  49488. * openssl req -newkey dsa:certs/dsaparams.pem \
  49489. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  49490. * -outform PEM
  49491. * with the passphrase "wolfSSL"
  49492. */
  49493. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  49494. const char* csrDsaFile = "./certs/csr.dsa.pem";
  49495. XFILE f = XBADFILE;
  49496. #endif
  49497. BIO* bio = NULL;
  49498. X509* req = NULL;
  49499. EVP_PKEY *pub_key = NULL;
  49500. {
  49501. ExpectNotNull(bio = BIO_new_file(csrFile, "rb"));
  49502. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  49503. /*
  49504. * Extract the public key from the CSR
  49505. */
  49506. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  49507. /*
  49508. * Verify the signature in the CSR
  49509. */
  49510. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  49511. X509_free(req);
  49512. req = NULL;
  49513. BIO_free(bio);
  49514. bio = NULL;
  49515. EVP_PKEY_free(pub_key);
  49516. pub_key = NULL;
  49517. }
  49518. {
  49519. #ifdef OPENSSL_ALL
  49520. X509_ATTRIBUTE* attr = NULL;
  49521. ASN1_TYPE *at = NULL;
  49522. #endif
  49523. ExpectNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  49524. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  49525. /*
  49526. * Extract the public key from the CSR
  49527. */
  49528. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  49529. /*
  49530. * Verify the signature in the CSR
  49531. */
  49532. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  49533. #ifdef OPENSSL_ALL
  49534. /*
  49535. * Obtain the challenge password from the CSR
  49536. */
  49537. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  49538. -1), 1);
  49539. ExpectNotNull(attr = X509_REQ_get_attr(req, 1));
  49540. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  49541. ExpectNotNull(at->value.asn1_string);
  49542. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string),
  49543. "2xIE+qqp/rhyTXP+");
  49544. ExpectIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  49545. #endif
  49546. X509_free(req);
  49547. req = NULL;
  49548. BIO_free(bio);
  49549. bio = NULL;
  49550. EVP_PKEY_free(pub_key);
  49551. pub_key = NULL;
  49552. }
  49553. {
  49554. #ifdef OPENSSL_ALL
  49555. X509_ATTRIBUTE* attr = NULL;
  49556. ASN1_TYPE *at = NULL;
  49557. STACK_OF(X509_EXTENSION) *exts = NULL;
  49558. #endif
  49559. ExpectNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  49560. /* This CSR contains an Extension Request attribute so
  49561. * we test extension parsing in a CSR attribute here. */
  49562. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  49563. /*
  49564. * Extract the public key from the CSR
  49565. */
  49566. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  49567. /*
  49568. * Verify the signature in the CSR
  49569. */
  49570. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  49571. #ifdef OPENSSL_ALL
  49572. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  49573. req));
  49574. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  49575. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  49576. /*
  49577. * Obtain the challenge password from the CSR
  49578. */
  49579. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  49580. -1), 0);
  49581. ExpectNotNull(attr = X509_REQ_get_attr(req, 0));
  49582. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  49583. ExpectNotNull(at->value.asn1_string);
  49584. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  49585. ExpectIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  49586. ExpectIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  49587. #endif
  49588. X509_free(req);
  49589. req = NULL;
  49590. BIO_free(bio);
  49591. bio = NULL;
  49592. EVP_PKEY_free(pub_key);
  49593. pub_key = NULL;
  49594. }
  49595. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  49596. {
  49597. ExpectNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  49598. ExpectNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  49599. /*
  49600. * Extract the public key from the CSR
  49601. */
  49602. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  49603. /*
  49604. * Verify the signature in the CSR
  49605. */
  49606. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  49607. X509_free(req);
  49608. req = NULL;
  49609. BIO_free(bio);
  49610. /* Run the same test, but with a file pointer instead of a BIO.
  49611. * (PEM_read_X509_REQ)*/
  49612. ExpectTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  49613. ExpectNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  49614. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  49615. X509_free(req);
  49616. EVP_PKEY_free(pub_key);
  49617. }
  49618. #endif /* !NO_DSA && !HAVE_SELFTEST */
  49619. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  49620. return EXPECT_RESULT();
  49621. }
  49622. static int test_wolfSSL_PEM_read_X509(void)
  49623. {
  49624. EXPECT_DECLS;
  49625. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  49626. !defined(NO_RSA)
  49627. X509 *x509 = NULL;
  49628. XFILE fp = XBADFILE;
  49629. ExpectTrue((fp = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  49630. ExpectNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  49631. X509_free(x509);
  49632. if (fp != XBADFILE)
  49633. XFCLOSE(fp);
  49634. #endif
  49635. return EXPECT_RESULT();
  49636. }
  49637. static int test_wolfSSL_PEM_read(void)
  49638. {
  49639. EXPECT_DECLS;
  49640. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  49641. const char* filename = "./certs/server-keyEnc.pem";
  49642. XFILE fp = XBADFILE;
  49643. char* name = NULL;
  49644. char* header = NULL;
  49645. byte* data = NULL;
  49646. long len;
  49647. EVP_CIPHER_INFO cipher;
  49648. WOLFSSL_BIO* bio = NULL;
  49649. byte* fileData = NULL;
  49650. size_t fileDataSz = 0;
  49651. byte* out;
  49652. ExpectNotNull(bio = BIO_new_file(filename, "rb"));
  49653. ExpectIntEQ(PEM_read_bio(bio, NULL, &header, &data, &len), 0);
  49654. ExpectIntEQ(PEM_read_bio(bio, &name, NULL, &data, &len), 0);
  49655. ExpectIntEQ(PEM_read_bio(bio, &name, &header, NULL, &len), 0);
  49656. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, NULL), 0);
  49657. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, &len), 1);
  49658. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  49659. ExpectIntGT(XSTRLEN(header), 0);
  49660. ExpectIntGT(len, 0);
  49661. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49662. name = NULL;
  49663. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49664. header = NULL;
  49665. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49666. data = NULL;
  49667. BIO_free(bio);
  49668. bio = NULL;
  49669. ExpectTrue((fp = XFOPEN(filename, "rb")) != XBADFILE);
  49670. /* Fail cases. */
  49671. ExpectIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  49672. ExpectIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  49673. ExpectIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  49674. ExpectIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  49675. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  49676. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  49677. ExpectIntGT(XSTRLEN(header), 0);
  49678. ExpectIntGT(len, 0);
  49679. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  49680. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  49681. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  49682. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  49683. DYNAMIC_TYPE_TMP_BUFFER));
  49684. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  49685. if (fp != XBADFILE) {
  49686. XFCLOSE(fp);
  49687. fp = XBADFILE;
  49688. }
  49689. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  49690. /* Fail cases. */
  49691. ExpectIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  49692. ExpectIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  49693. ExpectIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  49694. ExpectIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  49695. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  49696. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  49697. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  49698. /* Fail cases. */
  49699. ExpectIntEQ(PEM_write(XBADFILE, name, header, data, len), 0);
  49700. ExpectIntEQ(PEM_write(stderr, NULL, header, data, len), 0);
  49701. ExpectIntEQ(PEM_write(stderr, name, NULL, data, len), 0);
  49702. ExpectIntEQ(PEM_write(stderr, name, header, NULL, len), 0);
  49703. /* Pass case */
  49704. ExpectIntEQ(PEM_write(stderr, name, header, data, len), fileDataSz);
  49705. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49706. name = NULL;
  49707. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49708. header = NULL;
  49709. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49710. data = NULL;
  49711. /* Read out of a fixed buffer BIO - forces malloc in PEM_read_bio. */
  49712. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, &len), 1);
  49713. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  49714. ExpectIntGT(XSTRLEN(header), 0);
  49715. ExpectIntGT(len, 0);
  49716. /* Fail cases. */
  49717. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  49718. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  49719. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  49720. #ifndef NO_DES3
  49721. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  49722. #endif
  49723. /* Fail cases. */
  49724. ExpectIntEQ(PEM_do_header(NULL, data, &len, PasswordCallBack,
  49725. (void*)"yassl123"), WOLFSSL_FAILURE);
  49726. ExpectIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  49727. (void*)"yassl123"), WOLFSSL_FAILURE);
  49728. ExpectIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  49729. (void*)"yassl123"), WOLFSSL_FAILURE);
  49730. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  49731. (void*)"yassl123"), WOLFSSL_FAILURE);
  49732. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NoPasswordCallBack,
  49733. (void*)"yassl123"), WOLFSSL_FAILURE);
  49734. #if !defined(NO_DES3) && !defined(NO_MD5)
  49735. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  49736. (void*)"yassl123"), WOLFSSL_SUCCESS);
  49737. #else
  49738. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  49739. (void*)"yassl123"), WOLFSSL_FAILURE);
  49740. #endif
  49741. BIO_free(bio);
  49742. bio = NULL;
  49743. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49744. fileData = NULL;
  49745. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49746. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49747. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49748. name = NULL;
  49749. header = NULL;
  49750. data = NULL;
  49751. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  49752. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  49753. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  49754. ExpectIntEQ(XSTRLEN(header), 0);
  49755. ExpectIntGT(len, 0);
  49756. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  49757. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  49758. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  49759. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  49760. DYNAMIC_TYPE_TMP_BUFFER));
  49761. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  49762. if (fp != XBADFILE)
  49763. XFCLOSE(fp);
  49764. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  49765. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  49766. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  49767. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  49768. BIO_free(bio);
  49769. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49770. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49771. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49772. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49773. #endif
  49774. return EXPECT_RESULT();
  49775. }
  49776. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  49777. {
  49778. EXPECT_DECLS;
  49779. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  49780. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49781. const byte iv[12] = { 0 };
  49782. const byte key[16] = { 0 };
  49783. const byte cleartext[16] = { 0 };
  49784. const byte aad[] = {
  49785. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  49786. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  49787. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  49788. 0x4f
  49789. };
  49790. byte out1Part[16];
  49791. byte outTag1Part[16];
  49792. byte out2Part[16];
  49793. byte outTag2Part[16];
  49794. byte decryptBuf[16];
  49795. int len = 0;
  49796. int tlen;
  49797. EVP_CIPHER_CTX* ctx = NULL;
  49798. /* ENCRYPT */
  49799. /* Send AAD and data in 1 part */
  49800. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  49801. tlen = 0;
  49802. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49803. 1);
  49804. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49805. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  49806. ExpectIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  49807. sizeof(cleartext)), 1);
  49808. tlen += len;
  49809. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  49810. tlen += len;
  49811. ExpectIntEQ(tlen, sizeof(cleartext));
  49812. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  49813. outTag1Part), 1);
  49814. EVP_CIPHER_CTX_free(ctx);
  49815. ctx = NULL;
  49816. /* DECRYPT */
  49817. /* Send AAD and data in 1 part */
  49818. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  49819. tlen = 0;
  49820. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49821. 1);
  49822. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49823. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  49824. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  49825. sizeof(cleartext)), 1);
  49826. tlen += len;
  49827. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  49828. outTag1Part), 1);
  49829. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  49830. tlen += len;
  49831. ExpectIntEQ(tlen, sizeof(cleartext));
  49832. EVP_CIPHER_CTX_free(ctx);
  49833. ctx = NULL;
  49834. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  49835. /* ENCRYPT */
  49836. /* Send AAD and data in 2 parts */
  49837. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  49838. tlen = 0;
  49839. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49840. 1);
  49841. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49842. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  49843. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  49844. 1);
  49845. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  49846. tlen += len;
  49847. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  49848. sizeof(cleartext) - 1), 1);
  49849. tlen += len;
  49850. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  49851. tlen += len;
  49852. ExpectIntEQ(tlen, sizeof(cleartext));
  49853. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  49854. outTag2Part), 1);
  49855. ExpectIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  49856. ExpectIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  49857. EVP_CIPHER_CTX_free(ctx);
  49858. ctx = NULL;
  49859. /* DECRYPT */
  49860. /* Send AAD and data in 2 parts */
  49861. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  49862. tlen = 0;
  49863. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49864. 1);
  49865. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49866. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  49867. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  49868. 1);
  49869. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  49870. tlen += len;
  49871. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  49872. sizeof(cleartext) - 1), 1);
  49873. tlen += len;
  49874. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  49875. outTag1Part), 1);
  49876. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  49877. tlen += len;
  49878. ExpectIntEQ(tlen, sizeof(cleartext));
  49879. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  49880. /* Test AAD reuse */
  49881. EVP_CIPHER_CTX_free(ctx);
  49882. #endif
  49883. return EXPECT_RESULT();
  49884. }
  49885. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  49886. {
  49887. EXPECT_DECLS;
  49888. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  49889. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49890. /* Zero length plain text */
  49891. byte key[] = {
  49892. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49893. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49894. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49895. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49896. }; /* align */
  49897. byte iv[] = {
  49898. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49899. }; /* align */
  49900. byte plaintxt[1];
  49901. int ivSz = 12;
  49902. int plaintxtSz = 0;
  49903. unsigned char tag[16];
  49904. unsigned char tag_kat[] = {
  49905. 0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  49906. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b
  49907. };
  49908. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  49909. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  49910. int ciphertxtSz = 0;
  49911. int decryptedtxtSz = 0;
  49912. int len = 0;
  49913. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  49914. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  49915. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  49916. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49917. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  49918. plaintxtSz));
  49919. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  49920. ciphertxtSz += len;
  49921. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  49922. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  49923. ExpectIntEQ(0, ciphertxtSz);
  49924. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  49925. EVP_CIPHER_CTX_init(de);
  49926. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  49927. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  49928. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  49929. decryptedtxtSz = len;
  49930. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  49931. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  49932. decryptedtxtSz += len;
  49933. ExpectIntEQ(0, decryptedtxtSz);
  49934. EVP_CIPHER_CTX_free(en);
  49935. EVP_CIPHER_CTX_free(de);
  49936. #endif
  49937. return EXPECT_RESULT();
  49938. }
  49939. static int test_wolfssl_EVP_aes_gcm(void)
  49940. {
  49941. EXPECT_DECLS;
  49942. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  49943. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49944. /* A 256 bit key, AES_128 will use the first 128 bit*/
  49945. byte *key = (byte*)"01234567890123456789012345678901";
  49946. /* A 128 bit IV */
  49947. byte *iv = (byte*)"0123456789012345";
  49948. int ivSz = AES_BLOCK_SIZE;
  49949. /* Message to be encrypted */
  49950. byte *plaintxt = (byte*)"for things to change you have to change";
  49951. /* Additional non-confidential data */
  49952. byte *aad = (byte*)"Don't spend major time on minor things.";
  49953. unsigned char tag[AES_BLOCK_SIZE] = {0};
  49954. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  49955. int aadSz = (int)XSTRLEN((char*)aad);
  49956. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  49957. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  49958. int ciphertxtSz = 0;
  49959. int decryptedtxtSz = 0;
  49960. int len = 0;
  49961. int i = 0;
  49962. EVP_CIPHER_CTX en[2];
  49963. EVP_CIPHER_CTX de[2];
  49964. for (i = 0; i < 2; i++) {
  49965. EVP_CIPHER_CTX_init(&en[i]);
  49966. if (i == 0) {
  49967. /* Default uses 96-bits IV length */
  49968. #ifdef WOLFSSL_AES_128
  49969. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  49970. key, iv));
  49971. #elif defined(WOLFSSL_AES_192)
  49972. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  49973. key, iv));
  49974. #elif defined(WOLFSSL_AES_256)
  49975. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  49976. key, iv));
  49977. #endif
  49978. }
  49979. else {
  49980. #ifdef WOLFSSL_AES_128
  49981. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  49982. NULL, NULL));
  49983. #elif defined(WOLFSSL_AES_192)
  49984. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  49985. NULL, NULL));
  49986. #elif defined(WOLFSSL_AES_256)
  49987. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  49988. NULL, NULL));
  49989. #endif
  49990. /* non-default must to set the IV length first */
  49991. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  49992. ivSz, NULL));
  49993. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  49994. }
  49995. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  49996. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  49997. plaintxtSz));
  49998. ciphertxtSz = len;
  49999. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50000. ciphertxtSz += len;
  50001. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  50002. AES_BLOCK_SIZE, tag));
  50003. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  50004. EVP_CIPHER_CTX_init(&de[i]);
  50005. if (i == 0) {
  50006. /* Default uses 96-bits IV length */
  50007. #ifdef WOLFSSL_AES_128
  50008. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50009. key, iv));
  50010. #elif defined(WOLFSSL_AES_192)
  50011. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50012. key, iv));
  50013. #elif defined(WOLFSSL_AES_256)
  50014. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50015. key, iv));
  50016. #endif
  50017. }
  50018. else {
  50019. #ifdef WOLFSSL_AES_128
  50020. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50021. NULL, NULL));
  50022. #elif defined(WOLFSSL_AES_192)
  50023. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50024. NULL, NULL));
  50025. #elif defined(WOLFSSL_AES_256)
  50026. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50027. NULL, NULL));
  50028. #endif
  50029. /* non-default must to set the IV length first */
  50030. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  50031. ivSz, NULL));
  50032. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50033. }
  50034. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50035. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50036. ciphertxtSz));
  50037. decryptedtxtSz = len;
  50038. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  50039. AES_BLOCK_SIZE, tag));
  50040. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50041. decryptedtxtSz += len;
  50042. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  50043. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50044. /* modify tag*/
  50045. if (i == 0) {
  50046. /* Default uses 96-bits IV length */
  50047. #ifdef WOLFSSL_AES_128
  50048. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50049. key, iv));
  50050. #elif defined(WOLFSSL_AES_192)
  50051. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50052. key, iv));
  50053. #elif defined(WOLFSSL_AES_256)
  50054. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50055. key, iv));
  50056. #endif
  50057. }
  50058. else {
  50059. #ifdef WOLFSSL_AES_128
  50060. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50061. NULL, NULL));
  50062. #elif defined(WOLFSSL_AES_192)
  50063. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50064. NULL, NULL));
  50065. #elif defined(WOLFSSL_AES_256)
  50066. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50067. NULL, NULL));
  50068. #endif
  50069. /* non-default must to set the IV length first */
  50070. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  50071. ivSz, NULL));
  50072. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50073. }
  50074. tag[AES_BLOCK_SIZE-1]+=0xBB;
  50075. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50076. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  50077. AES_BLOCK_SIZE, tag));
  50078. /* fail due to wrong tag */
  50079. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50080. ciphertxtSz));
  50081. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50082. ExpectIntEQ(0, len);
  50083. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  50084. }
  50085. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  50086. return EXPECT_RESULT();
  50087. }
  50088. static int test_wolfssl_EVP_aria_gcm(void)
  50089. {
  50090. int res = TEST_SKIPPED;
  50091. #if defined(OPENSSL_EXTRA) && defined(HAVE_ARIA) && \
  50092. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50093. /* A 256 bit key, AES_128 will use the first 128 bit*/
  50094. byte *key = (byte*)"01234567890123456789012345678901";
  50095. /* A 128 bit IV */
  50096. byte *iv = (byte*)"0123456789012345";
  50097. int ivSz = ARIA_BLOCK_SIZE;
  50098. /* Message to be encrypted */
  50099. const int plaintxtSz = 40;
  50100. byte plaintxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  50101. XMEMCPY(plaintxt,"for things to change you have to change",plaintxtSz);
  50102. /* Additional non-confidential data */
  50103. byte *aad = (byte*)"Don't spend major time on minor things.";
  50104. unsigned char tag[ARIA_BLOCK_SIZE] = {0};
  50105. int aadSz = (int)XSTRLEN((char*)aad);
  50106. byte ciphertxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  50107. byte decryptedtxt[plaintxtSz];
  50108. int ciphertxtSz = 0;
  50109. int decryptedtxtSz = 0;
  50110. int len = 0;
  50111. int i = 0;
  50112. #define TEST_ARIA_GCM_COUNT 6
  50113. EVP_CIPHER_CTX en[TEST_ARIA_GCM_COUNT];
  50114. EVP_CIPHER_CTX de[TEST_ARIA_GCM_COUNT];
  50115. for (i = 0; i < TEST_ARIA_GCM_COUNT; i++) {
  50116. EVP_CIPHER_CTX_init(&en[i]);
  50117. switch (i) {
  50118. case 0:
  50119. /* Default uses 96-bits IV length */
  50120. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, key, iv));
  50121. break;
  50122. case 1:
  50123. /* Default uses 96-bits IV length */
  50124. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, key, iv));
  50125. break;
  50126. case 2:
  50127. /* Default uses 96-bits IV length */
  50128. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, key, iv));
  50129. break;
  50130. case 3:
  50131. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  50132. /* non-default must to set the IV length first */
  50133. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50134. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50135. break;
  50136. case 4:
  50137. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  50138. /* non-default must to set the IV length first */
  50139. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50140. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50141. break;
  50142. case 5:
  50143. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  50144. /* non-default must to set the IV length first */
  50145. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50146. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50147. break;
  50148. }
  50149. XMEMSET(ciphertxt,0,sizeof(ciphertxt));
  50150. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  50151. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  50152. ciphertxtSz = len;
  50153. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50154. AssertIntNE(0, XMEMCMP(plaintxt, ciphertxt, plaintxtSz));
  50155. ciphertxtSz += len;
  50156. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, ARIA_BLOCK_SIZE, tag));
  50157. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  50158. EVP_CIPHER_CTX_init(&de[i]);
  50159. switch (i) {
  50160. case 0:
  50161. /* Default uses 96-bits IV length */
  50162. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, key, iv));
  50163. break;
  50164. case 1:
  50165. /* Default uses 96-bits IV length */
  50166. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, key, iv));
  50167. break;
  50168. case 2:
  50169. /* Default uses 96-bits IV length */
  50170. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, key, iv));
  50171. break;
  50172. case 3:
  50173. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  50174. /* non-default must to set the IV length first */
  50175. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50176. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50177. break;
  50178. case 4:
  50179. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  50180. /* non-default must to set the IV length first */
  50181. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50182. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50183. break;
  50184. case 5:
  50185. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  50186. /* non-default must to set the IV length first */
  50187. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50188. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50189. break;
  50190. }
  50191. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  50192. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50193. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  50194. decryptedtxtSz = len;
  50195. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  50196. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50197. decryptedtxtSz += len;
  50198. AssertIntEQ(plaintxtSz, decryptedtxtSz);
  50199. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50200. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  50201. /* modify tag*/
  50202. tag[AES_BLOCK_SIZE-1]+=0xBB;
  50203. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50204. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  50205. /* fail due to wrong tag */
  50206. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  50207. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50208. AssertIntEQ(0, len);
  50209. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  50210. }
  50211. res = TEST_RES_CHECK(1);
  50212. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  50213. return res;
  50214. }
  50215. static int test_wolfssl_EVP_aes_ccm_zeroLen(void)
  50216. {
  50217. EXPECT_DECLS;
  50218. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  50219. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50220. /* Zero length plain text */
  50221. byte key[] = {
  50222. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50223. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50224. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50225. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50226. }; /* align */
  50227. byte iv[] = {
  50228. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50229. }; /* align */
  50230. byte plaintxt[1];
  50231. int ivSz = 12;
  50232. int plaintxtSz = 0;
  50233. unsigned char tag[16];
  50234. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  50235. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  50236. int ciphertxtSz = 0;
  50237. int decryptedtxtSz = 0;
  50238. int len = 0;
  50239. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  50240. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  50241. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_ccm(), NULL, key, iv));
  50242. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  50243. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  50244. plaintxtSz));
  50245. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  50246. ciphertxtSz += len;
  50247. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  50248. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  50249. ExpectIntEQ(0, ciphertxtSz);
  50250. EVP_CIPHER_CTX_init(de);
  50251. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_ccm(), NULL, key, iv));
  50252. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  50253. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  50254. decryptedtxtSz = len;
  50255. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  50256. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  50257. decryptedtxtSz += len;
  50258. ExpectIntEQ(0, decryptedtxtSz);
  50259. EVP_CIPHER_CTX_free(en);
  50260. EVP_CIPHER_CTX_free(de);
  50261. #endif
  50262. return EXPECT_RESULT();
  50263. }
  50264. static int test_wolfssl_EVP_aes_ccm(void)
  50265. {
  50266. EXPECT_DECLS;
  50267. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  50268. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50269. /* A 256 bit key, AES_128 will use the first 128 bit*/
  50270. byte *key = (byte*)"01234567890123456789012345678901";
  50271. /* A 128 bit IV */
  50272. byte *iv = (byte*)"0123456789012";
  50273. int ivSz = (int)XSTRLEN((char*)iv);
  50274. /* Message to be encrypted */
  50275. byte *plaintxt = (byte*)"for things to change you have to change";
  50276. /* Additional non-confidential data */
  50277. byte *aad = (byte*)"Don't spend major time on minor things.";
  50278. unsigned char tag[AES_BLOCK_SIZE] = {0};
  50279. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  50280. int aadSz = (int)XSTRLEN((char*)aad);
  50281. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  50282. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  50283. int ciphertxtSz = 0;
  50284. int decryptedtxtSz = 0;
  50285. int len = 0;
  50286. int i = 0;
  50287. int ret;
  50288. EVP_CIPHER_CTX en[2];
  50289. EVP_CIPHER_CTX de[2];
  50290. for (i = 0; i < 2; i++) {
  50291. EVP_CIPHER_CTX_init(&en[i]);
  50292. if (i == 0) {
  50293. /* Default uses 96-bits IV length */
  50294. #ifdef WOLFSSL_AES_128
  50295. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  50296. key, iv));
  50297. #elif defined(WOLFSSL_AES_192)
  50298. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  50299. key, iv));
  50300. #elif defined(WOLFSSL_AES_256)
  50301. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  50302. key, iv));
  50303. #endif
  50304. }
  50305. else {
  50306. #ifdef WOLFSSL_AES_128
  50307. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  50308. NULL, NULL));
  50309. #elif defined(WOLFSSL_AES_192)
  50310. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  50311. NULL, NULL));
  50312. #elif defined(WOLFSSL_AES_256)
  50313. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  50314. NULL, NULL));
  50315. #endif
  50316. /* non-default must to set the IV length first */
  50317. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  50318. ivSz, NULL));
  50319. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50320. }
  50321. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  50322. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  50323. plaintxtSz));
  50324. ciphertxtSz = len;
  50325. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50326. ciphertxtSz += len;
  50327. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  50328. AES_BLOCK_SIZE, tag));
  50329. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]);
  50330. ExpectIntEQ(ret, 1);
  50331. EVP_CIPHER_CTX_init(&de[i]);
  50332. if (i == 0) {
  50333. /* Default uses 96-bits IV length */
  50334. #ifdef WOLFSSL_AES_128
  50335. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  50336. key, iv));
  50337. #elif defined(WOLFSSL_AES_192)
  50338. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  50339. key, iv));
  50340. #elif defined(WOLFSSL_AES_256)
  50341. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  50342. key, iv));
  50343. #endif
  50344. }
  50345. else {
  50346. #ifdef WOLFSSL_AES_128
  50347. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  50348. NULL, NULL));
  50349. #elif defined(WOLFSSL_AES_192)
  50350. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  50351. NULL, NULL));
  50352. #elif defined(WOLFSSL_AES_256)
  50353. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  50354. NULL, NULL));
  50355. #endif
  50356. /* non-default must to set the IV length first */
  50357. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  50358. ivSz, NULL));
  50359. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50360. }
  50361. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50362. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50363. ciphertxtSz));
  50364. decryptedtxtSz = len;
  50365. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  50366. AES_BLOCK_SIZE, tag));
  50367. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50368. decryptedtxtSz += len;
  50369. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  50370. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50371. /* modify tag*/
  50372. tag[AES_BLOCK_SIZE-1]+=0xBB;
  50373. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50374. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  50375. AES_BLOCK_SIZE, tag));
  50376. /* fail due to wrong tag */
  50377. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50378. ciphertxtSz));
  50379. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50380. ExpectIntEQ(0, len);
  50381. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]);
  50382. ExpectIntEQ(ret, 1);
  50383. }
  50384. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESCCM */
  50385. return EXPECT_RESULT();
  50386. }
  50387. static int test_wolfssl_EVP_chacha20_poly1305(void)
  50388. {
  50389. EXPECT_DECLS;
  50390. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  50391. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  50392. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  50393. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  50394. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  50395. byte cipherText[sizeof(plainText)];
  50396. byte decryptedText[sizeof(plainText)];
  50397. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  50398. EVP_CIPHER_CTX* ctx = NULL;
  50399. int outSz;
  50400. /* Encrypt. */
  50401. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50402. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  50403. NULL), WOLFSSL_SUCCESS);
  50404. /* Invalid IV length. */
  50405. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  50406. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  50407. /* Valid IV length. */
  50408. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  50409. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  50410. /* Invalid tag length. */
  50411. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  50412. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  50413. /* Valid tag length. */
  50414. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  50415. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  50416. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50417. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  50418. WOLFSSL_SUCCESS);
  50419. ExpectIntEQ(outSz, sizeof(aad));
  50420. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  50421. sizeof(plainText)), WOLFSSL_SUCCESS);
  50422. ExpectIntEQ(outSz, sizeof(plainText));
  50423. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  50424. ExpectIntEQ(outSz, 0);
  50425. /* Invalid tag length. */
  50426. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  50427. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  50428. /* Valid tag length. */
  50429. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  50430. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  50431. EVP_CIPHER_CTX_free(ctx);
  50432. ctx = NULL;
  50433. /* Decrypt. */
  50434. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50435. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  50436. NULL), WOLFSSL_SUCCESS);
  50437. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  50438. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  50439. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  50440. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  50441. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50442. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  50443. WOLFSSL_SUCCESS);
  50444. ExpectIntEQ(outSz, sizeof(aad));
  50445. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50446. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50447. ExpectIntEQ(outSz, sizeof(cipherText));
  50448. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50449. WOLFSSL_SUCCESS);
  50450. ExpectIntEQ(outSz, 0);
  50451. EVP_CIPHER_CTX_free(ctx);
  50452. ctx = NULL;
  50453. /* Test partial Inits. CipherInit() allow setting of key and iv
  50454. * in separate calls. */
  50455. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50456. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  50457. key, NULL, 1), WOLFSSL_SUCCESS);
  50458. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  50459. WOLFSSL_SUCCESS);
  50460. ExpectIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  50461. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  50462. ExpectIntEQ(outSz, sizeof(aad));
  50463. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50464. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50465. ExpectIntEQ(outSz, sizeof(cipherText));
  50466. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50467. WOLFSSL_SUCCESS);
  50468. ExpectIntEQ(outSz, 0);
  50469. EVP_CIPHER_CTX_free(ctx);
  50470. #endif
  50471. return EXPECT_RESULT();
  50472. }
  50473. static int test_wolfssl_EVP_chacha20(void)
  50474. {
  50475. EXPECT_DECLS;
  50476. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
  50477. byte key[CHACHA_MAX_KEY_SZ];
  50478. byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
  50479. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  50480. byte cipherText[sizeof(plainText)];
  50481. byte decryptedText[sizeof(plainText)];
  50482. EVP_CIPHER_CTX* ctx = NULL;
  50483. int outSz;
  50484. XMEMSET(key, 0, sizeof(key));
  50485. XMEMSET(iv, 0, sizeof(iv));
  50486. /* Encrypt. */
  50487. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50488. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  50489. NULL), WOLFSSL_SUCCESS);
  50490. /* Any tag length must fail - not an AEAD cipher. */
  50491. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  50492. 16, NULL), WOLFSSL_FAILURE);
  50493. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50494. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  50495. sizeof(plainText)), WOLFSSL_SUCCESS);
  50496. ExpectIntEQ(outSz, sizeof(plainText));
  50497. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  50498. ExpectIntEQ(outSz, 0);
  50499. EVP_CIPHER_CTX_free(ctx);
  50500. ctx = NULL;
  50501. /* Decrypt. */
  50502. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50503. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  50504. NULL), WOLFSSL_SUCCESS);
  50505. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50506. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50507. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50508. ExpectIntEQ(outSz, sizeof(cipherText));
  50509. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50510. WOLFSSL_SUCCESS);
  50511. ExpectIntEQ(outSz, 0);
  50512. EVP_CIPHER_CTX_free(ctx);
  50513. ctx = NULL;
  50514. /* Test partial Inits. CipherInit() allow setting of key and iv
  50515. * in separate calls. */
  50516. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50517. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
  50518. key, NULL, 1), WOLFSSL_SUCCESS);
  50519. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  50520. WOLFSSL_SUCCESS);
  50521. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50522. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50523. ExpectIntEQ(outSz, sizeof(cipherText));
  50524. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50525. WOLFSSL_SUCCESS);
  50526. ExpectIntEQ(outSz, 0);
  50527. EVP_CIPHER_CTX_free(ctx);
  50528. #endif
  50529. return EXPECT_RESULT();
  50530. }
  50531. static int test_wolfssl_EVP_sm4_ecb(void)
  50532. {
  50533. int res = TEST_SKIPPED;
  50534. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_ECB)
  50535. EXPECT_DECLS;
  50536. byte key[SM4_KEY_SIZE];
  50537. byte plainText[SM4_BLOCK_SIZE] = {
  50538. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  50539. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  50540. };
  50541. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  50542. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  50543. EVP_CIPHER_CTX* ctx;
  50544. int outSz;
  50545. XMEMSET(key, 0, sizeof(key));
  50546. /* Encrypt. */
  50547. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50548. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  50549. WOLFSSL_SUCCESS);
  50550. /* Any tag length must fail - not an AEAD cipher. */
  50551. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  50552. WOLFSSL_FAILURE);
  50553. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL),
  50554. WOLFSSL_SUCCESS);
  50555. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  50556. sizeof(plainText)), WOLFSSL_SUCCESS);
  50557. ExpectIntEQ(outSz, sizeof(plainText));
  50558. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  50559. WOLFSSL_SUCCESS);
  50560. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  50561. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  50562. EVP_CIPHER_CTX_free(ctx);
  50563. /* Decrypt. */
  50564. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50565. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  50566. WOLFSSL_SUCCESS);
  50567. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, NULL),
  50568. WOLFSSL_SUCCESS);
  50569. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50570. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50571. ExpectIntEQ(outSz, sizeof(plainText));
  50572. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  50573. WOLFSSL_SUCCESS);
  50574. ExpectIntEQ(outSz, 0);
  50575. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  50576. EVP_CIPHER_CTX_free(ctx);
  50577. res = EXPECT_RESULT();
  50578. #endif
  50579. return res;
  50580. }
  50581. static int test_wolfssl_EVP_sm4_cbc(void)
  50582. {
  50583. int res = TEST_SKIPPED;
  50584. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CBC)
  50585. EXPECT_DECLS;
  50586. byte key[SM4_KEY_SIZE];
  50587. byte iv[SM4_BLOCK_SIZE];
  50588. byte plainText[SM4_BLOCK_SIZE] = {
  50589. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  50590. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  50591. };
  50592. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  50593. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  50594. EVP_CIPHER_CTX* ctx;
  50595. int outSz;
  50596. XMEMSET(key, 0, sizeof(key));
  50597. XMEMSET(iv, 0, sizeof(iv));
  50598. /* Encrypt. */
  50599. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50600. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  50601. WOLFSSL_SUCCESS);
  50602. /* Any tag length must fail - not an AEAD cipher. */
  50603. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  50604. WOLFSSL_FAILURE);
  50605. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50606. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  50607. sizeof(plainText)), WOLFSSL_SUCCESS);
  50608. ExpectIntEQ(outSz, sizeof(plainText));
  50609. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  50610. WOLFSSL_SUCCESS);
  50611. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  50612. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  50613. EVP_CIPHER_CTX_free(ctx);
  50614. /* Decrypt. */
  50615. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50616. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  50617. WOLFSSL_SUCCESS);
  50618. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50619. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50620. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50621. ExpectIntEQ(outSz, sizeof(plainText));
  50622. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  50623. WOLFSSL_SUCCESS);
  50624. ExpectIntEQ(outSz, 0);
  50625. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  50626. EVP_CIPHER_CTX_free(ctx);
  50627. /* Test partial Inits. CipherInit() allow setting of key and iv
  50628. * in separate calls. */
  50629. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50630. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_cbc(), key, NULL, 0),
  50631. WOLFSSL_SUCCESS);
  50632. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 0),
  50633. WOLFSSL_SUCCESS);
  50634. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50635. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50636. ExpectIntEQ(outSz, sizeof(plainText));
  50637. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  50638. WOLFSSL_SUCCESS);
  50639. ExpectIntEQ(outSz, 0);
  50640. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  50641. EVP_CIPHER_CTX_free(ctx);
  50642. res = EXPECT_RESULT();
  50643. #endif
  50644. return res;
  50645. }
  50646. static int test_wolfssl_EVP_sm4_ctr(void)
  50647. {
  50648. int res = TEST_SKIPPED;
  50649. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CTR)
  50650. EXPECT_DECLS;
  50651. byte key[SM4_KEY_SIZE];
  50652. byte iv[SM4_BLOCK_SIZE];
  50653. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  50654. byte cipherText[sizeof(plainText)];
  50655. byte decryptedText[sizeof(plainText)];
  50656. EVP_CIPHER_CTX* ctx;
  50657. int outSz;
  50658. XMEMSET(key, 0, sizeof(key));
  50659. XMEMSET(iv, 0, sizeof(iv));
  50660. /* Encrypt. */
  50661. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50662. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  50663. WOLFSSL_SUCCESS);
  50664. /* Any tag length must fail - not an AEAD cipher. */
  50665. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  50666. WOLFSSL_FAILURE);
  50667. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50668. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  50669. sizeof(plainText)), WOLFSSL_SUCCESS);
  50670. ExpectIntEQ(outSz, sizeof(plainText));
  50671. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  50672. ExpectIntEQ(outSz, 0);
  50673. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  50674. EVP_CIPHER_CTX_free(ctx);
  50675. /* Decrypt. */
  50676. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50677. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  50678. WOLFSSL_SUCCESS);
  50679. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50680. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50681. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50682. ExpectIntEQ(outSz, sizeof(cipherText));
  50683. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50684. WOLFSSL_SUCCESS);
  50685. ExpectIntEQ(outSz, 0);
  50686. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  50687. EVP_CIPHER_CTX_free(ctx);
  50688. /* Test partial Inits. CipherInit() allow setting of key and iv
  50689. * in separate calls. */
  50690. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50691. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_ctr(), key, NULL, 1),
  50692. WOLFSSL_SUCCESS);
  50693. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  50694. WOLFSSL_SUCCESS);
  50695. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50696. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50697. ExpectIntEQ(outSz, sizeof(cipherText));
  50698. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50699. WOLFSSL_SUCCESS);
  50700. ExpectIntEQ(outSz, 0);
  50701. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  50702. EVP_CIPHER_CTX_free(ctx);
  50703. res = EXPECT_RESULT();
  50704. #endif
  50705. return res;
  50706. }
  50707. static int test_wolfssl_EVP_sm4_gcm_zeroLen(void)
  50708. {
  50709. int res = TEST_SKIPPED;
  50710. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  50711. /* Zero length plain text */
  50712. EXPECT_DECLS;
  50713. byte key[] = {
  50714. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50715. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50716. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50717. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50718. }; /* align */
  50719. byte iv[] = {
  50720. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50721. }; /* align */
  50722. byte plaintxt[1];
  50723. int ivSz = 12;
  50724. int plaintxtSz = 0;
  50725. unsigned char tag[16];
  50726. unsigned char tag_kat[16] = {
  50727. 0x23,0x2f,0x0c,0xfe,0x30,0x8b,0x49,0xea,
  50728. 0x6f,0xc8,0x82,0x29,0xb5,0xdc,0x85,0x8d
  50729. };
  50730. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  50731. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  50732. int ciphertxtSz = 0;
  50733. int decryptedtxtSz = 0;
  50734. int len = 0;
  50735. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  50736. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  50737. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_gcm(), NULL, key, iv));
  50738. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50739. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  50740. plaintxtSz));
  50741. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  50742. ciphertxtSz += len;
  50743. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  50744. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  50745. ExpectIntEQ(0, ciphertxtSz);
  50746. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  50747. EVP_CIPHER_CTX_init(de);
  50748. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_gcm(), NULL, key, iv));
  50749. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50750. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  50751. decryptedtxtSz = len;
  50752. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  50753. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  50754. decryptedtxtSz += len;
  50755. ExpectIntEQ(0, decryptedtxtSz);
  50756. EVP_CIPHER_CTX_free(en);
  50757. EVP_CIPHER_CTX_free(de);
  50758. res = EXPECT_RESULT();
  50759. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  50760. return res;
  50761. }
  50762. static int test_wolfssl_EVP_sm4_gcm(void)
  50763. {
  50764. int res = TEST_SKIPPED;
  50765. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  50766. EXPECT_DECLS;
  50767. byte *key = (byte*)"0123456789012345";
  50768. /* A 128 bit IV */
  50769. byte *iv = (byte*)"0123456789012345";
  50770. int ivSz = SM4_BLOCK_SIZE;
  50771. /* Message to be encrypted */
  50772. byte *plaintxt = (byte*)"for things to change you have to change";
  50773. /* Additional non-confidential data */
  50774. byte *aad = (byte*)"Don't spend major time on minor things.";
  50775. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  50776. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  50777. int aadSz = (int)XSTRLEN((char*)aad);
  50778. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  50779. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  50780. int ciphertxtSz = 0;
  50781. int decryptedtxtSz = 0;
  50782. int len = 0;
  50783. int i = 0;
  50784. EVP_CIPHER_CTX en[2];
  50785. EVP_CIPHER_CTX de[2];
  50786. for (i = 0; i < 2; i++) {
  50787. EVP_CIPHER_CTX_init(&en[i]);
  50788. if (i == 0) {
  50789. /* Default uses 96-bits IV length */
  50790. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, key,
  50791. iv));
  50792. }
  50793. else {
  50794. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, NULL,
  50795. NULL));
  50796. /* non-default must to set the IV length first */
  50797. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  50798. ivSz, NULL));
  50799. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50800. }
  50801. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  50802. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  50803. plaintxtSz));
  50804. ciphertxtSz = len;
  50805. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50806. ciphertxtSz += len;
  50807. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  50808. SM4_BLOCK_SIZE, tag));
  50809. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  50810. EVP_CIPHER_CTX_init(&de[i]);
  50811. if (i == 0) {
  50812. /* Default uses 96-bits IV length */
  50813. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, key,
  50814. iv));
  50815. }
  50816. else {
  50817. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, NULL,
  50818. NULL));
  50819. /* non-default must to set the IV length first */
  50820. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  50821. ivSz, NULL));
  50822. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50823. }
  50824. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50825. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50826. ciphertxtSz));
  50827. decryptedtxtSz = len;
  50828. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  50829. SM4_BLOCK_SIZE, tag));
  50830. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50831. decryptedtxtSz += len;
  50832. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  50833. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50834. /* modify tag*/
  50835. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  50836. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50837. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  50838. SM4_BLOCK_SIZE, tag));
  50839. /* fail due to wrong tag */
  50840. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50841. ciphertxtSz));
  50842. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50843. ExpectIntEQ(0, len);
  50844. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  50845. }
  50846. res = EXPECT_RESULT();
  50847. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  50848. return res;
  50849. }
  50850. static int test_wolfssl_EVP_sm4_ccm_zeroLen(void)
  50851. {
  50852. int res = TEST_SKIPPED;
  50853. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  50854. /* Zero length plain text */
  50855. EXPECT_DECLS;
  50856. byte key[] = {
  50857. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50858. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50859. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50860. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50861. }; /* align */
  50862. byte iv[] = {
  50863. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50864. }; /* align */
  50865. byte plaintxt[1];
  50866. int ivSz = 12;
  50867. int plaintxtSz = 0;
  50868. unsigned char tag[16];
  50869. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  50870. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  50871. int ciphertxtSz = 0;
  50872. int decryptedtxtSz = 0;
  50873. int len = 0;
  50874. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  50875. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  50876. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_ccm(), NULL, key, iv));
  50877. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  50878. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  50879. plaintxtSz));
  50880. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  50881. ciphertxtSz += len;
  50882. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  50883. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  50884. ExpectIntEQ(0, ciphertxtSz);
  50885. EVP_CIPHER_CTX_init(de);
  50886. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_ccm(), NULL, key, iv));
  50887. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  50888. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  50889. decryptedtxtSz = len;
  50890. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  50891. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  50892. decryptedtxtSz += len;
  50893. ExpectIntEQ(0, decryptedtxtSz);
  50894. EVP_CIPHER_CTX_free(en);
  50895. EVP_CIPHER_CTX_free(de);
  50896. res = EXPECT_RESULT();
  50897. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  50898. return res;
  50899. }
  50900. static int test_wolfssl_EVP_sm4_ccm(void)
  50901. {
  50902. int res = TEST_SKIPPED;
  50903. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  50904. EXPECT_DECLS;
  50905. byte *key = (byte*)"0123456789012345";
  50906. byte *iv = (byte*)"0123456789012";
  50907. int ivSz = (int)XSTRLEN((char*)iv);
  50908. /* Message to be encrypted */
  50909. byte *plaintxt = (byte*)"for things to change you have to change";
  50910. /* Additional non-confidential data */
  50911. byte *aad = (byte*)"Don't spend major time on minor things.";
  50912. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  50913. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  50914. int aadSz = (int)XSTRLEN((char*)aad);
  50915. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  50916. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  50917. int ciphertxtSz = 0;
  50918. int decryptedtxtSz = 0;
  50919. int len = 0;
  50920. int i = 0;
  50921. EVP_CIPHER_CTX en[2];
  50922. EVP_CIPHER_CTX de[2];
  50923. for (i = 0; i < 2; i++) {
  50924. EVP_CIPHER_CTX_init(&en[i]);
  50925. if (i == 0) {
  50926. /* Default uses 96-bits IV length */
  50927. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, key,
  50928. iv));
  50929. }
  50930. else {
  50931. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, NULL,
  50932. NULL));
  50933. /* non-default must to set the IV length first */
  50934. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  50935. ivSz, NULL));
  50936. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50937. }
  50938. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  50939. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  50940. plaintxtSz));
  50941. ciphertxtSz = len;
  50942. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50943. ciphertxtSz += len;
  50944. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  50945. SM4_BLOCK_SIZE, tag));
  50946. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  50947. EVP_CIPHER_CTX_init(&de[i]);
  50948. if (i == 0) {
  50949. /* Default uses 96-bits IV length */
  50950. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, key,
  50951. iv));
  50952. }
  50953. else {
  50954. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, NULL,
  50955. NULL));
  50956. /* non-default must to set the IV length first */
  50957. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  50958. ivSz, NULL));
  50959. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50960. }
  50961. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50962. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50963. ciphertxtSz));
  50964. decryptedtxtSz = len;
  50965. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  50966. SM4_BLOCK_SIZE, tag));
  50967. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50968. decryptedtxtSz += len;
  50969. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  50970. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50971. /* modify tag*/
  50972. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  50973. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50974. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  50975. SM4_BLOCK_SIZE, tag));
  50976. /* fail due to wrong tag */
  50977. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50978. ciphertxtSz));
  50979. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50980. ExpectIntEQ(0, len);
  50981. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  50982. }
  50983. res = EXPECT_RESULT();
  50984. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  50985. return res;
  50986. }
  50987. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  50988. {
  50989. EXPECT_DECLS;
  50990. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  50991. EVP_PKEY_CTX* ctx = NULL;
  50992. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  50993. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  50994. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  50995. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  50996. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  50997. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  50998. byte outKey[34];
  50999. size_t outKeySz = sizeof(outKey);
  51000. /* These expected outputs were gathered by running the same test below using
  51001. * OpenSSL. */
  51002. const byte extractAndExpand[] = {
  51003. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  51004. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  51005. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  51006. };
  51007. const byte extractOnly[] = {
  51008. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  51009. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  51010. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  51011. };
  51012. const byte expandOnly[] = {
  51013. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  51014. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  51015. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  51016. };
  51017. const byte extractAndExpandAddInfo[] = {
  51018. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  51019. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  51020. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  51021. };
  51022. ExpectNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  51023. ExpectIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  51024. /* NULL ctx. */
  51025. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  51026. /* NULL md. */
  51027. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  51028. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  51029. /* NULL ctx. */
  51030. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  51031. WOLFSSL_FAILURE);
  51032. /* NULL salt is ok. */
  51033. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  51034. WOLFSSL_SUCCESS);
  51035. /* Salt length <= 0. */
  51036. /* Length 0 salt is ok. */
  51037. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  51038. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  51039. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  51040. WOLFSSL_SUCCESS);
  51041. /* NULL ctx. */
  51042. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  51043. WOLFSSL_FAILURE);
  51044. /* NULL key. */
  51045. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  51046. WOLFSSL_FAILURE);
  51047. /* Key length <= 0 */
  51048. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  51049. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  51050. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  51051. WOLFSSL_SUCCESS);
  51052. /* NULL ctx. */
  51053. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  51054. WOLFSSL_FAILURE);
  51055. /* NULL info is ok. */
  51056. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  51057. WOLFSSL_SUCCESS);
  51058. /* Info length <= 0 */
  51059. /* Length 0 info is ok. */
  51060. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  51061. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  51062. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  51063. WOLFSSL_SUCCESS);
  51064. /* NULL ctx. */
  51065. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  51066. WOLFSSL_FAILURE);
  51067. /* Extract and expand (default). */
  51068. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51069. ExpectIntEQ(outKeySz, sizeof(extractAndExpand));
  51070. ExpectIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  51071. /* Extract only. */
  51072. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  51073. WOLFSSL_SUCCESS);
  51074. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51075. ExpectIntEQ(outKeySz, sizeof(extractOnly));
  51076. ExpectIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  51077. outKeySz = sizeof(outKey);
  51078. /* Expand only. */
  51079. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  51080. WOLFSSL_SUCCESS);
  51081. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51082. ExpectIntEQ(outKeySz, sizeof(expandOnly));
  51083. ExpectIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  51084. outKeySz = sizeof(outKey);
  51085. /* Extract and expand with appended additional info. */
  51086. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  51087. WOLFSSL_SUCCESS);
  51088. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  51089. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  51090. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51091. ExpectIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  51092. ExpectIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  51093. EVP_PKEY_CTX_free(ctx);
  51094. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  51095. return EXPECT_RESULT();
  51096. }
  51097. #ifndef NO_BIO
  51098. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  51099. {
  51100. EXPECT_DECLS;
  51101. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  51102. BIO* bio = NULL;
  51103. X509_INFO* info = NULL;
  51104. STACK_OF(X509_INFO)* sk = NULL;
  51105. char* subject = NULL;
  51106. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/"
  51107. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  51108. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/"
  51109. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  51110. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  51111. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  51112. ExpectNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  51113. ExpectIntEQ(sk_X509_INFO_num(sk), 2);
  51114. /* using dereference to maintain testing for Apache port*/
  51115. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  51116. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  51117. 0, 0));
  51118. ExpectIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  51119. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  51120. X509_INFO_free(info);
  51121. info = NULL;
  51122. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  51123. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  51124. 0, 0));
  51125. ExpectIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  51126. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  51127. X509_INFO_free(info);
  51128. ExpectNull(info = sk_X509_INFO_pop(sk));
  51129. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  51130. BIO_free(bio);
  51131. #endif
  51132. return EXPECT_RESULT();
  51133. }
  51134. #endif /* !NO_BIO */
  51135. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  51136. {
  51137. EXPECT_DECLS;
  51138. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  51139. X509 *x509 = NULL;
  51140. X509_NAME* name = NULL;
  51141. int idx = 0;
  51142. X509_NAME_ENTRY *ne = NULL;
  51143. ASN1_OBJECT *object = NULL;
  51144. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  51145. WOLFSSL_FILETYPE_PEM));
  51146. ExpectNotNull(name = X509_get_subject_name(x509));
  51147. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  51148. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  51149. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  51150. X509_free(x509);
  51151. #endif
  51152. return EXPECT_RESULT();
  51153. }
  51154. static int test_wolfSSL_X509_STORE_get1_certs(void)
  51155. {
  51156. EXPECT_DECLS;
  51157. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  51158. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  51159. X509_STORE_CTX *storeCtx = NULL;
  51160. X509_STORE *store = NULL;
  51161. X509 *caX509 = NULL;
  51162. X509 *svrX509 = NULL;
  51163. X509_NAME *subject = NULL;
  51164. WOLF_STACK_OF(WOLFSSL_X509) *certs = NULL;
  51165. ExpectNotNull(caX509 = X509_load_certificate_file(caCertFile,
  51166. SSL_FILETYPE_PEM));
  51167. ExpectNotNull((svrX509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  51168. SSL_FILETYPE_PEM)));
  51169. ExpectNotNull(storeCtx = X509_STORE_CTX_new());
  51170. ExpectNotNull(store = X509_STORE_new());
  51171. ExpectNotNull(subject = X509_get_subject_name(caX509));
  51172. /* Errors */
  51173. ExpectNull(X509_STORE_get1_certs(storeCtx, subject));
  51174. ExpectNull(X509_STORE_get1_certs(NULL, subject));
  51175. ExpectNull(X509_STORE_get1_certs(storeCtx, NULL));
  51176. ExpectIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  51177. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL),
  51178. SSL_SUCCESS);
  51179. /* Should find the cert */
  51180. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  51181. ExpectIntEQ(1, wolfSSL_sk_X509_num(certs));
  51182. sk_X509_pop_free(certs, NULL);
  51183. certs = NULL;
  51184. /* Should not find the cert */
  51185. ExpectNotNull(subject = X509_get_subject_name(svrX509));
  51186. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  51187. ExpectIntEQ(0, wolfSSL_sk_X509_num(certs));
  51188. sk_X509_pop_free(certs, NULL);
  51189. certs = NULL;
  51190. X509_STORE_free(store);
  51191. X509_STORE_CTX_free(storeCtx);
  51192. X509_free(svrX509);
  51193. X509_free(caX509);
  51194. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  51195. return EXPECT_RESULT();
  51196. }
  51197. static int test_wolfSSL_dup_CA_list(void)
  51198. {
  51199. int res = TEST_SKIPPED;
  51200. #if defined(OPENSSL_ALL)
  51201. EXPECT_DECLS;
  51202. STACK_OF(X509_NAME) *originalStack = NULL;
  51203. STACK_OF(X509_NAME) *copyStack = NULL;
  51204. int originalCount = 0;
  51205. int copyCount = 0;
  51206. X509_NAME *name = NULL;
  51207. int i;
  51208. originalStack = sk_X509_NAME_new_null();
  51209. ExpectNotNull(originalStack);
  51210. for (i = 0; i < 3; i++) {
  51211. name = X509_NAME_new();
  51212. ExpectNotNull(name);
  51213. AssertIntEQ(sk_X509_NAME_push(originalStack, name), WOLFSSL_SUCCESS);
  51214. }
  51215. copyStack = SSL_dup_CA_list(originalStack);
  51216. ExpectNotNull(copyStack);
  51217. originalCount = sk_X509_NAME_num(originalStack);
  51218. copyCount = sk_X509_NAME_num(copyStack);
  51219. AssertIntEQ(originalCount, copyCount);
  51220. sk_X509_NAME_pop_free(originalStack, X509_NAME_free);
  51221. sk_X509_NAME_pop_free(copyStack, X509_NAME_free);
  51222. originalStack = NULL;
  51223. copyStack = NULL;
  51224. res = EXPECT_RESULT();
  51225. #endif /* OPENSSL_ALL */
  51226. return res;
  51227. }
  51228. static int test_ForceZero(void)
  51229. {
  51230. EXPECT_DECLS;
  51231. unsigned char data[32];
  51232. unsigned int i, j, len;
  51233. /* Test case with 0 length */
  51234. ForceZero(data, 0);
  51235. /* Test ForceZero */
  51236. for (i = 0; i < sizeof(data); i++) {
  51237. for (len = 1; len < sizeof(data) - i; len++) {
  51238. for (j = 0; j < sizeof(data); j++)
  51239. data[j] = j + 1;
  51240. ForceZero(data + i, len);
  51241. for (j = 0; j < sizeof(data); j++) {
  51242. if (j < i || j >= i + len) {
  51243. ExpectIntNE(data[j], 0x00);
  51244. }
  51245. else {
  51246. ExpectIntEQ(data[j], 0x00);
  51247. }
  51248. }
  51249. }
  51250. }
  51251. return EXPECT_RESULT();
  51252. }
  51253. #ifndef NO_BIO
  51254. static int test_wolfSSL_X509_print(void)
  51255. {
  51256. EXPECT_DECLS;
  51257. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  51258. !defined(NO_RSA) && defined(XSNPRINTF)
  51259. X509 *x509 = NULL;
  51260. BIO *bio = NULL;
  51261. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  51262. const X509_ALGOR *cert_sig_alg = NULL;
  51263. #endif
  51264. ExpectNotNull(x509 = X509_load_certificate_file(svrCertFile,
  51265. WOLFSSL_FILETYPE_PEM));
  51266. /* print to memory */
  51267. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  51268. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  51269. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  51270. #if defined(WC_DISABLE_RADIX_ZERO_PAD)
  51271. /* Will print IP address subject alt name. */
  51272. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3349);
  51273. #elif defined(NO_ASN_TIME)
  51274. /* Will print IP address subject alt name but not Validity. */
  51275. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3235);
  51276. #else
  51277. /* Will print IP address subject alt name. */
  51278. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3350);
  51279. #endif
  51280. #elif defined(NO_ASN_TIME)
  51281. /* With NO_ASN_TIME defined, X509_print skips printing Validity. */
  51282. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3213);
  51283. #else
  51284. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3328);
  51285. #endif
  51286. BIO_free(bio);
  51287. bio = NULL;
  51288. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  51289. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  51290. /* Print signature */
  51291. ExpectNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  51292. ExpectIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  51293. #endif
  51294. /* print to stderr */
  51295. #if !defined(NO_WOLFSSL_DIR)
  51296. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  51297. #endif
  51298. /* print again */
  51299. ExpectIntEQ(X509_print_fp(stderr, x509), SSL_SUCCESS);
  51300. X509_free(x509);
  51301. BIO_free(bio);
  51302. #endif
  51303. return EXPECT_RESULT();
  51304. }
  51305. static int test_wolfSSL_X509_CRL_print(void)
  51306. {
  51307. EXPECT_DECLS;
  51308. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  51309. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  51310. X509_CRL* crl = NULL;
  51311. BIO *bio = NULL;
  51312. XFILE fp = XBADFILE;
  51313. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  51314. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  51315. NULL, NULL));
  51316. if (fp != XBADFILE)
  51317. XFCLOSE(fp);
  51318. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  51319. ExpectIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  51320. X509_CRL_free(crl);
  51321. BIO_free(bio);
  51322. #endif
  51323. return EXPECT_RESULT();
  51324. }
  51325. static int test_wolfSSL_BIO_get_len(void)
  51326. {
  51327. EXPECT_DECLS;
  51328. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  51329. BIO *bio = NULL;
  51330. const char txt[] = "Some example text to push to the BIO.";
  51331. ExpectIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  51332. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  51333. ExpectIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  51334. ExpectIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  51335. BIO_free(bio);
  51336. bio = NULL;
  51337. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  51338. ExpectIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  51339. BIO_free(bio);
  51340. #endif
  51341. return EXPECT_RESULT();
  51342. }
  51343. #endif /* !NO_BIO */
  51344. static int test_wolfSSL_RSA(void)
  51345. {
  51346. EXPECT_DECLS;
  51347. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  51348. RSA* rsa = NULL;
  51349. const BIGNUM *n = NULL;
  51350. const BIGNUM *e = NULL;
  51351. const BIGNUM *d = NULL;
  51352. const BIGNUM *p = NULL;
  51353. const BIGNUM *q = NULL;
  51354. const BIGNUM *dmp1 = NULL;
  51355. const BIGNUM *dmq1 = NULL;
  51356. const BIGNUM *iqmp = NULL;
  51357. ExpectNotNull(rsa = RSA_new());
  51358. ExpectIntEQ(RSA_size(NULL), 0);
  51359. ExpectIntEQ(RSA_size(rsa), 0);
  51360. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  51361. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  51362. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  51363. #ifdef WOLFSSL_RSA_KEY_CHECK
  51364. ExpectIntEQ(RSA_check_key(rsa), 0);
  51365. #endif
  51366. RSA_free(rsa);
  51367. rsa = NULL;
  51368. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  51369. ExpectIntEQ(RSA_size(rsa), 256);
  51370. #ifdef WOLFSSL_RSA_KEY_CHECK
  51371. ExpectIntEQ(RSA_check_key(NULL), 0);
  51372. ExpectIntEQ(RSA_check_key(rsa), 1);
  51373. #endif
  51374. /* sanity check */
  51375. ExpectIntEQ(RSA_bits(NULL), 0);
  51376. /* key */
  51377. ExpectIntEQ(RSA_bits(rsa), 2048);
  51378. RSA_get0_key(rsa, &n, &e, &d);
  51379. ExpectPtrEq(rsa->n, n);
  51380. ExpectPtrEq(rsa->e, e);
  51381. ExpectPtrEq(rsa->d, d);
  51382. n = NULL;
  51383. e = NULL;
  51384. d = NULL;
  51385. ExpectNotNull(n = BN_new());
  51386. ExpectNotNull(e = BN_new());
  51387. ExpectNotNull(d = BN_new());
  51388. ExpectIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  51389. if (EXPECT_FAIL()) {
  51390. BN_free((BIGNUM*)n);
  51391. BN_free((BIGNUM*)e);
  51392. BN_free((BIGNUM*)d);
  51393. }
  51394. ExpectPtrEq(rsa->n, n);
  51395. ExpectPtrEq(rsa->e, e);
  51396. ExpectPtrEq(rsa->d, d);
  51397. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  51398. ExpectIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  51399. /* crt_params */
  51400. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  51401. ExpectPtrEq(rsa->dmp1, dmp1);
  51402. ExpectPtrEq(rsa->dmq1, dmq1);
  51403. ExpectPtrEq(rsa->iqmp, iqmp);
  51404. dmp1 = NULL;
  51405. dmq1 = NULL;
  51406. iqmp = NULL;
  51407. ExpectNotNull(dmp1 = BN_new());
  51408. ExpectNotNull(dmq1 = BN_new());
  51409. ExpectNotNull(iqmp = BN_new());
  51410. ExpectIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  51411. (BIGNUM*)iqmp), 1);
  51412. if (EXPECT_FAIL()) {
  51413. BN_free((BIGNUM*)dmp1);
  51414. BN_free((BIGNUM*)dmq1);
  51415. BN_free((BIGNUM*)iqmp);
  51416. }
  51417. ExpectPtrEq(rsa->dmp1, dmp1);
  51418. ExpectPtrEq(rsa->dmq1, dmq1);
  51419. ExpectPtrEq(rsa->iqmp, iqmp);
  51420. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  51421. ExpectIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  51422. (BIGNUM*)iqmp), 0);
  51423. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  51424. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  51425. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  51426. ExpectNull(dmp1);
  51427. ExpectNull(dmq1);
  51428. ExpectNull(iqmp);
  51429. /* factors */
  51430. RSA_get0_factors(rsa, NULL, NULL);
  51431. RSA_get0_factors(rsa, &p, &q);
  51432. ExpectPtrEq(rsa->p, p);
  51433. ExpectPtrEq(rsa->q, q);
  51434. p = NULL;
  51435. q = NULL;
  51436. ExpectNotNull(p = BN_new());
  51437. ExpectNotNull(q = BN_new());
  51438. ExpectIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  51439. if (EXPECT_FAIL()) {
  51440. BN_free((BIGNUM*)p);
  51441. BN_free((BIGNUM*)q);
  51442. }
  51443. ExpectPtrEq(rsa->p, p);
  51444. ExpectPtrEq(rsa->q, q);
  51445. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  51446. ExpectIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  51447. RSA_get0_factors(NULL, NULL, NULL);
  51448. RSA_get0_factors(NULL, &p, &q);
  51449. ExpectNull(p);
  51450. ExpectNull(q);
  51451. ExpectIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  51452. ExpectIntEQ(RSA_bits(rsa), 21);
  51453. RSA_free(rsa);
  51454. rsa = NULL;
  51455. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  51456. ExpectNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  51457. ExpectIntEQ(RSA_size(rsa), 384);
  51458. ExpectIntEQ(RSA_bits(rsa), 3072);
  51459. RSA_free(rsa);
  51460. rsa = NULL;
  51461. #endif
  51462. /* remove for now with odd key size until adjusting rsa key size check with
  51463. wc_MakeRsaKey()
  51464. ExpectNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  51465. RSA_free(rsa);
  51466. rsa = NULL;
  51467. */
  51468. ExpectNull(RSA_generate_key(-1, 3, NULL, NULL));
  51469. ExpectNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  51470. ExpectNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  51471. ExpectNull(RSA_generate_key(2048, 0, NULL, NULL));
  51472. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  51473. {
  51474. byte buff[FOURK_BUF];
  51475. byte der[FOURK_BUF];
  51476. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  51477. XFILE f = XBADFILE;
  51478. int bytes = 0;
  51479. /* test loading encrypted RSA private pem w/o password */
  51480. ExpectTrue((f = XFOPEN(PrivKeyPemFile, "rb")) != XBADFILE);
  51481. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  51482. if (f != XBADFILE)
  51483. XFCLOSE(f);
  51484. XMEMSET(der, 0, sizeof(der));
  51485. /* test that error value is returned with no password */
  51486. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""),
  51487. 0);
  51488. }
  51489. #endif
  51490. #endif
  51491. return EXPECT_RESULT();
  51492. }
  51493. static int test_wolfSSL_RSA_DER(void)
  51494. {
  51495. EXPECT_DECLS;
  51496. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  51497. RSA *rsa = NULL;
  51498. int i;
  51499. const unsigned char *buff = NULL;
  51500. unsigned char *newBuff = NULL;
  51501. struct tbl_s
  51502. {
  51503. const unsigned char *der;
  51504. int sz;
  51505. } tbl[] = {
  51506. #ifdef USE_CERT_BUFFERS_1024
  51507. {client_key_der_1024, sizeof_client_key_der_1024},
  51508. {server_key_der_1024, sizeof_server_key_der_1024},
  51509. #endif
  51510. #ifdef USE_CERT_BUFFERS_2048
  51511. {client_key_der_2048, sizeof_client_key_der_2048},
  51512. {server_key_der_2048, sizeof_server_key_der_2048},
  51513. #endif
  51514. {NULL, 0}
  51515. };
  51516. /* Public Key DER */
  51517. struct tbl_s pub[] = {
  51518. #ifdef USE_CERT_BUFFERS_1024
  51519. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  51520. #endif
  51521. #ifdef USE_CERT_BUFFERS_2048
  51522. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  51523. #endif
  51524. {NULL, 0}
  51525. };
  51526. ExpectNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  51527. buff = pub[0].der;
  51528. ExpectNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  51529. ExpectNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  51530. buff = tbl[0].der;
  51531. ExpectNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  51532. ExpectIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  51533. rsa = RSA_new();
  51534. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  51535. RSA_free(rsa);
  51536. rsa = NULL;
  51537. for (i = 0; tbl[i].der != NULL; i++)
  51538. {
  51539. /* Passing in pointer results in pointer moving. */
  51540. buff = tbl[i].der;
  51541. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  51542. ExpectNotNull(rsa);
  51543. RSA_free(rsa);
  51544. rsa = NULL;
  51545. }
  51546. for (i = 0; tbl[i].der != NULL; i++)
  51547. {
  51548. /* Passing in pointer results in pointer moving. */
  51549. buff = tbl[i].der;
  51550. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  51551. ExpectNotNull(rsa);
  51552. RSA_free(rsa);
  51553. rsa = NULL;
  51554. }
  51555. for (i = 0; pub[i].der != NULL; i++)
  51556. {
  51557. buff = pub[i].der;
  51558. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  51559. ExpectNotNull(rsa);
  51560. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  51561. newBuff = NULL;
  51562. ExpectIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  51563. ExpectNotNull(newBuff);
  51564. ExpectIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  51565. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51566. RSA_free(rsa);
  51567. rsa = NULL;
  51568. }
  51569. #endif
  51570. return EXPECT_RESULT();
  51571. }
  51572. static int test_wolfSSL_RSA_print(void)
  51573. {
  51574. EXPECT_DECLS;
  51575. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  51576. !defined(NO_STDIO_FILESYSTEM) && \
  51577. !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  51578. !defined(NO_BIO) && defined(XFPRINTF)
  51579. BIO *bio = NULL;
  51580. WOLFSSL_RSA* rsa = NULL;
  51581. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  51582. ExpectNotNull(rsa = RSA_new());
  51583. ExpectIntEQ(RSA_print(NULL, rsa, 0), -1);
  51584. ExpectIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  51585. ExpectIntEQ(RSA_print(bio, NULL, 0), -1);
  51586. ExpectIntEQ(RSA_print_fp(stderr, NULL, 0), 0);
  51587. /* Some very large number of indent spaces. */
  51588. ExpectIntEQ(RSA_print(bio, rsa, 128), -1);
  51589. /* RSA is empty. */
  51590. ExpectIntEQ(RSA_print(bio, rsa, 0), 0);
  51591. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 0);
  51592. RSA_free(rsa);
  51593. rsa = NULL;
  51594. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  51595. ExpectIntEQ(RSA_print(bio, rsa, 0), 1);
  51596. ExpectIntEQ(RSA_print(bio, rsa, 4), 1);
  51597. ExpectIntEQ(RSA_print(bio, rsa, -1), 1);
  51598. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 1);
  51599. ExpectIntEQ(RSA_print_fp(stderr, rsa, 4), 1);
  51600. ExpectIntEQ(RSA_print_fp(stderr, rsa, -1), 1);
  51601. BIO_free(bio);
  51602. RSA_free(rsa);
  51603. #endif
  51604. return EXPECT_RESULT();
  51605. }
  51606. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  51607. {
  51608. EXPECT_DECLS;
  51609. #ifndef NO_RSA
  51610. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  51611. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  51612. RSA *rsa = NULL;
  51613. const unsigned char *derBuf = client_key_der_2048;
  51614. unsigned char em[256] = {0}; /* len = 2048/8 */
  51615. /* Random data simulating a hash */
  51616. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  51617. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  51618. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  51619. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  51620. };
  51621. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  51622. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  51623. RSA_PSS_SALTLEN_DIGEST), 0);
  51624. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  51625. RSA_PSS_SALTLEN_DIGEST), 0);
  51626. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  51627. RSA_PSS_SALTLEN_DIGEST), 0);
  51628. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  51629. RSA_PSS_SALTLEN_DIGEST), 0);
  51630. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  51631. ExpectIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  51632. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  51633. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  51634. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  51635. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  51636. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  51637. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  51638. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  51639. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  51640. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  51641. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  51642. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  51643. RSA_PSS_SALTLEN_DIGEST), 1);
  51644. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  51645. RSA_PSS_SALTLEN_DIGEST), 1);
  51646. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  51647. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  51648. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  51649. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  51650. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  51651. RSA_PSS_SALTLEN_MAX), 1);
  51652. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  51653. RSA_PSS_SALTLEN_MAX), 1);
  51654. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  51655. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  51656. RSA_free(rsa);
  51657. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  51658. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  51659. #endif
  51660. return EXPECT_RESULT();
  51661. }
  51662. static int test_wolfSSL_RSA_sign_sha3(void)
  51663. {
  51664. EXPECT_DECLS;
  51665. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  51666. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  51667. RSA* rsa = NULL;
  51668. const unsigned char *derBuf = client_key_der_2048;
  51669. unsigned char sigRet[256] = {0};
  51670. unsigned int sigLen = sizeof(sigRet);
  51671. /* Random data simulating a hash */
  51672. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  51673. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  51674. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  51675. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  51676. };
  51677. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  51678. ExpectIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet, &sigLen,
  51679. rsa), 1);
  51680. RSA_free(rsa);
  51681. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  51682. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  51683. return EXPECT_RESULT();
  51684. }
  51685. static int test_wolfSSL_RSA_get0_key(void)
  51686. {
  51687. EXPECT_DECLS;
  51688. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51689. RSA *rsa = NULL;
  51690. const BIGNUM* n = NULL;
  51691. const BIGNUM* e = NULL;
  51692. const BIGNUM* d = NULL;
  51693. const unsigned char* der;
  51694. int derSz;
  51695. #ifdef USE_CERT_BUFFERS_1024
  51696. der = client_key_der_1024;
  51697. derSz = sizeof_client_key_der_1024;
  51698. #elif defined(USE_CERT_BUFFERS_2048)
  51699. der = client_key_der_2048;
  51700. derSz = sizeof_client_key_der_2048;
  51701. #else
  51702. der = NULL;
  51703. derSz = 0;
  51704. #endif
  51705. if (der != NULL) {
  51706. RSA_get0_key(NULL, NULL, NULL, NULL);
  51707. RSA_get0_key(rsa, NULL, NULL, NULL);
  51708. RSA_get0_key(NULL, &n, &e, &d);
  51709. ExpectNull(n);
  51710. ExpectNull(e);
  51711. ExpectNull(d);
  51712. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  51713. ExpectNotNull(rsa);
  51714. RSA_get0_key(rsa, NULL, NULL, NULL);
  51715. RSA_get0_key(rsa, &n, NULL, NULL);
  51716. ExpectNotNull(n);
  51717. RSA_get0_key(rsa, NULL, &e, NULL);
  51718. ExpectNotNull(e);
  51719. RSA_get0_key(rsa, NULL, NULL, &d);
  51720. ExpectNotNull(d);
  51721. RSA_get0_key(rsa, &n, &e, &d);
  51722. ExpectNotNull(n);
  51723. ExpectNotNull(e);
  51724. ExpectNotNull(d);
  51725. RSA_free(rsa);
  51726. }
  51727. #endif
  51728. return EXPECT_RESULT();
  51729. }
  51730. static int test_wolfSSL_RSA_meth(void)
  51731. {
  51732. EXPECT_DECLS;
  51733. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51734. RSA *rsa = NULL;
  51735. RSA_METHOD *rsa_meth = NULL;
  51736. #ifdef WOLFSSL_KEY_GEN
  51737. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  51738. RSA_free(rsa);
  51739. rsa = NULL;
  51740. #else
  51741. ExpectNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  51742. #endif
  51743. ExpectNotNull(RSA_get_default_method());
  51744. wolfSSL_RSA_meth_free(NULL);
  51745. ExpectNull(wolfSSL_RSA_meth_new(NULL, 0));
  51746. ExpectNotNull(rsa_meth = RSA_meth_new("placeholder RSA method",
  51747. RSA_METHOD_FLAG_NO_CHECK));
  51748. #ifndef NO_WOLFSSL_STUB
  51749. ExpectIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  51750. ExpectIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  51751. ExpectIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  51752. ExpectIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  51753. ExpectIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  51754. ExpectIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  51755. ExpectIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  51756. #endif
  51757. ExpectIntEQ(RSA_flags(NULL), 0);
  51758. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  51759. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  51760. ExpectIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  51761. ExpectNotNull(rsa = RSA_new());
  51762. /* No method set. */
  51763. ExpectIntEQ(RSA_flags(rsa), 0);
  51764. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  51765. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  51766. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  51767. ExpectIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  51768. ExpectIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  51769. if (EXPECT_FAIL()) {
  51770. wolfSSL_RSA_meth_free(rsa_meth);
  51771. }
  51772. ExpectNull(RSA_get_method(NULL));
  51773. ExpectPtrEq(RSA_get_method(rsa), rsa_meth);
  51774. ExpectIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  51775. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  51776. ExpectIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  51777. ExpectIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  51778. RSA_METHOD_FLAG_NO_CHECK);
  51779. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  51780. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  51781. ExpectIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  51782. /* rsa_meth is freed here */
  51783. RSA_free(rsa);
  51784. #endif
  51785. return EXPECT_RESULT();
  51786. }
  51787. static int test_wolfSSL_RSA_verify(void)
  51788. {
  51789. EXPECT_DECLS;
  51790. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  51791. #ifndef NO_BIO
  51792. XFILE fp = XBADFILE;
  51793. RSA *pKey = NULL;
  51794. RSA *pubKey = NULL;
  51795. X509 *cert = NULL;
  51796. const char *text = "Hello wolfSSL !";
  51797. unsigned char hash[SHA256_DIGEST_LENGTH];
  51798. unsigned char signature[2048/8];
  51799. unsigned int signatureLength;
  51800. byte *buf = NULL;
  51801. BIO *bio = NULL;
  51802. SHA256_CTX c;
  51803. EVP_PKEY *evpPkey = NULL;
  51804. EVP_PKEY *evpPubkey = NULL;
  51805. size_t sz;
  51806. /* generate hash */
  51807. SHA256_Init(&c);
  51808. SHA256_Update(&c, text, strlen(text));
  51809. SHA256_Final(hash, &c);
  51810. #ifdef WOLFSSL_SMALL_STACK_CACHE
  51811. /* workaround for small stack cache case */
  51812. wc_Sha256Free((wc_Sha256*)&c);
  51813. #endif
  51814. /* read privete key file */
  51815. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  51816. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_END), 0);
  51817. ExpectTrue((sz = XFTELL(fp)) > 0);
  51818. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_SET), 0);
  51819. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  51820. ExpectIntEQ(XFREAD(buf, 1, sz, fp), sz);
  51821. if (fp != XBADFILE) {
  51822. XFCLOSE(fp);
  51823. fp = XBADFILE;
  51824. }
  51825. /* read private key and sign hash data */
  51826. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  51827. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  51828. ExpectNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  51829. ExpectIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  51830. signature, &signatureLength, pKey), SSL_SUCCESS);
  51831. /* read public key and verify signed data */
  51832. ExpectTrue((fp = XFOPEN(svrCertFile,"rb")) != XBADFILE);
  51833. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  51834. if (fp != XBADFILE)
  51835. XFCLOSE(fp);
  51836. ExpectNotNull(evpPubkey = X509_get_pubkey(cert));
  51837. ExpectNotNull(pubKey = EVP_PKEY_get1_RSA(evpPubkey));
  51838. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  51839. signatureLength, pubKey), SSL_SUCCESS);
  51840. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  51841. signatureLength, NULL), SSL_FAILURE);
  51842. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  51843. signatureLength, pubKey), SSL_FAILURE);
  51844. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  51845. signatureLength, pubKey), SSL_FAILURE);
  51846. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  51847. signatureLength, NULL), SSL_FAILURE);
  51848. RSA_free(pKey);
  51849. EVP_PKEY_free(evpPkey);
  51850. RSA_free(pubKey);
  51851. EVP_PKEY_free(evpPubkey);
  51852. X509_free(cert);
  51853. BIO_free(bio);
  51854. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  51855. #endif
  51856. #endif
  51857. return EXPECT_RESULT();
  51858. }
  51859. static int test_wolfSSL_RSA_sign(void)
  51860. {
  51861. EXPECT_DECLS;
  51862. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51863. RSA *rsa;
  51864. unsigned char hash[SHA256_DIGEST_LENGTH];
  51865. #ifdef USE_CERT_BUFFERS_1024
  51866. const unsigned char* privDer = client_key_der_1024;
  51867. size_t privDerSz = sizeof_client_key_der_1024;
  51868. const unsigned char* pubDer = client_keypub_der_1024;
  51869. size_t pubDerSz = sizeof_client_keypub_der_1024;
  51870. unsigned char signature[1024/8];
  51871. #else
  51872. const unsigned char* privDer = client_key_der_2048;
  51873. size_t privDerSz = sizeof_client_key_der_2048;
  51874. const unsigned char* pubDer = client_keypub_der_2048;
  51875. size_t pubDerSz = sizeof_client_keypub_der_2048;
  51876. unsigned char signature[2048/8];
  51877. #endif
  51878. unsigned int signatureLen;
  51879. const unsigned char* der;
  51880. XMEMSET(hash, 0, sizeof(hash));
  51881. der = privDer;
  51882. rsa = NULL;
  51883. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51884. /* Invalid parameters. */
  51885. ExpectIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  51886. ExpectIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  51887. &signatureLen, rsa), 0);
  51888. ExpectIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  51889. &signatureLen, rsa), 0);
  51890. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  51891. &signatureLen, rsa), 0);
  51892. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  51893. NULL, rsa), 0);
  51894. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  51895. &signatureLen, NULL), 0);
  51896. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  51897. &signatureLen, rsa), 1);
  51898. RSA_free(rsa);
  51899. der = pubDer;
  51900. rsa = NULL;
  51901. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  51902. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  51903. signatureLen, rsa), 1);
  51904. RSA_free(rsa);
  51905. #endif
  51906. return EXPECT_RESULT();
  51907. }
  51908. static int test_wolfSSL_RSA_sign_ex(void)
  51909. {
  51910. EXPECT_DECLS;
  51911. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51912. RSA *rsa = NULL;
  51913. unsigned char hash[SHA256_DIGEST_LENGTH];
  51914. #ifdef USE_CERT_BUFFERS_1024
  51915. const unsigned char* privDer = client_key_der_1024;
  51916. size_t privDerSz = sizeof_client_key_der_1024;
  51917. const unsigned char* pubDer = client_keypub_der_1024;
  51918. size_t pubDerSz = sizeof_client_keypub_der_1024;
  51919. unsigned char signature[1024/8];
  51920. #else
  51921. const unsigned char* privDer = client_key_der_2048;
  51922. size_t privDerSz = sizeof_client_key_der_2048;
  51923. const unsigned char* pubDer = client_keypub_der_2048;
  51924. size_t pubDerSz = sizeof_client_keypub_der_2048;
  51925. unsigned char signature[2048/8];
  51926. #endif
  51927. unsigned int signatureLen;
  51928. const unsigned char* der;
  51929. unsigned char encodedHash[51];
  51930. unsigned int encodedHashLen;
  51931. const unsigned char expEncHash[] = {
  51932. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  51933. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  51934. 0x00, 0x04, 0x20,
  51935. /* Hash data */
  51936. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51937. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51938. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51939. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  51940. };
  51941. XMEMSET(hash, 0, sizeof(hash));
  51942. ExpectNotNull(rsa = wolfSSL_RSA_new());
  51943. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  51944. &signatureLen, rsa, 1), 0);
  51945. wolfSSL_RSA_free(rsa);
  51946. der = privDer;
  51947. rsa = NULL;
  51948. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51949. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  51950. -1), 0);
  51951. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  51952. signature, &signatureLen, rsa, 1), 0);
  51953. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  51954. &signatureLen, rsa, 1), 0);
  51955. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  51956. &signatureLen, rsa, 1), 0);
  51957. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  51958. NULL, rsa, 1), 0);
  51959. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  51960. &signatureLen, NULL, 1), 0);
  51961. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  51962. &signatureLen, rsa, -1), 0);
  51963. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  51964. &signatureLen, rsa, 0), 0);
  51965. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  51966. &signatureLen, rsa, 0), 0);
  51967. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  51968. NULL, rsa, 0), 0);
  51969. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  51970. &signatureLen, rsa, 1), 1);
  51971. /* Test returning encoded hash. */
  51972. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  51973. &encodedHashLen, rsa, 0), 1);
  51974. ExpectIntEQ(encodedHashLen, sizeof(expEncHash));
  51975. ExpectIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  51976. RSA_free(rsa);
  51977. der = pubDer;
  51978. rsa = NULL;
  51979. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  51980. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  51981. signatureLen, rsa), 1);
  51982. RSA_free(rsa);
  51983. #endif
  51984. return EXPECT_RESULT();
  51985. }
  51986. static int test_wolfSSL_RSA_public_decrypt(void)
  51987. {
  51988. EXPECT_DECLS;
  51989. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51990. RSA *rsa;
  51991. unsigned char msg[SHA256_DIGEST_LENGTH];
  51992. #ifdef USE_CERT_BUFFERS_1024
  51993. const unsigned char* pubDer = client_keypub_der_1024;
  51994. size_t pubDerSz = sizeof_client_keypub_der_1024;
  51995. unsigned char decMsg[1024/8];
  51996. const unsigned char encMsg[] = {
  51997. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  51998. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  51999. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  52000. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  52001. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  52002. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  52003. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  52004. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  52005. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  52006. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  52007. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  52008. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  52009. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  52010. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  52011. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  52012. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  52013. };
  52014. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  52015. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  52016. defined(WC_RSA_NO_PADDING)
  52017. const unsigned char encMsgNoPad[] = {
  52018. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  52019. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  52020. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  52021. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  52022. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  52023. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  52024. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  52025. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  52026. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  52027. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  52028. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  52029. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  52030. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  52031. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  52032. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  52033. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  52034. };
  52035. #endif
  52036. #else
  52037. const unsigned char* pubDer = client_keypub_der_2048;
  52038. size_t pubDerSz = sizeof_client_keypub_der_2048;
  52039. unsigned char decMsg[2048/8];
  52040. const unsigned char encMsg[] = {
  52041. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  52042. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  52043. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  52044. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  52045. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  52046. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  52047. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  52048. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  52049. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  52050. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  52051. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  52052. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  52053. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  52054. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  52055. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  52056. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  52057. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  52058. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  52059. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  52060. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  52061. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  52062. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  52063. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  52064. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  52065. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  52066. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  52067. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  52068. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  52069. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  52070. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  52071. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  52072. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  52073. };
  52074. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  52075. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  52076. defined(WC_RSA_NO_PADDING)
  52077. const unsigned char encMsgNoPad[] = {
  52078. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  52079. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  52080. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  52081. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  52082. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  52083. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  52084. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  52085. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  52086. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  52087. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  52088. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  52089. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  52090. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  52091. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  52092. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  52093. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  52094. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  52095. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  52096. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  52097. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  52098. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  52099. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  52100. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  52101. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  52102. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  52103. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  52104. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  52105. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  52106. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  52107. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  52108. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  52109. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  52110. };
  52111. #endif
  52112. #endif
  52113. const unsigned char* der;
  52114. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  52115. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  52116. defined(WC_RSA_NO_PADDING)
  52117. int i;
  52118. #endif
  52119. XMEMSET(msg, 0, sizeof(msg));
  52120. der = pubDer;
  52121. rsa = NULL;
  52122. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  52123. ExpectIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  52124. ExpectIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  52125. RSA_PKCS1_PADDING), -1);
  52126. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  52127. RSA_PKCS1_PADDING), -1);
  52128. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  52129. RSA_PKCS1_PADDING), -1);
  52130. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  52131. RSA_PKCS1_PADDING), -1);
  52132. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  52133. RSA_PKCS1_PSS_PADDING), -1);
  52134. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  52135. RSA_PKCS1_PADDING), 32);
  52136. ExpectIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  52137. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  52138. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  52139. defined(WC_RSA_NO_PADDING)
  52140. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  52141. rsa, RSA_NO_PADDING), sizeof(decMsg));
  52142. /* Zeros before actual data. */
  52143. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  52144. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  52145. }
  52146. /* Check actual data. */
  52147. XMEMSET(msg, 0x01, sizeof(msg));
  52148. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  52149. #endif
  52150. RSA_free(rsa);
  52151. #endif
  52152. return EXPECT_RESULT();
  52153. }
  52154. static int test_wolfSSL_RSA_private_encrypt(void)
  52155. {
  52156. EXPECT_DECLS;
  52157. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52158. RSA *rsa;
  52159. unsigned char msg[SHA256_DIGEST_LENGTH];
  52160. #ifdef USE_CERT_BUFFERS_1024
  52161. const unsigned char* privDer = client_key_der_1024;
  52162. size_t privDerSz = sizeof_client_key_der_1024;
  52163. unsigned char encMsg[1024/8];
  52164. const unsigned char expEncMsg[] = {
  52165. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  52166. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  52167. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  52168. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  52169. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  52170. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  52171. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  52172. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  52173. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  52174. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  52175. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  52176. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  52177. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  52178. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  52179. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  52180. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  52181. };
  52182. #ifdef WC_RSA_NO_PADDING
  52183. const unsigned char expEncMsgNoPad[] = {
  52184. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  52185. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  52186. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  52187. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  52188. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  52189. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  52190. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  52191. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  52192. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  52193. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  52194. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  52195. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  52196. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  52197. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  52198. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  52199. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  52200. };
  52201. #endif
  52202. #else
  52203. const unsigned char* privDer = client_key_der_2048;
  52204. size_t privDerSz = sizeof_client_key_der_2048;
  52205. unsigned char encMsg[2048/8];
  52206. const unsigned char expEncMsg[] = {
  52207. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  52208. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  52209. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  52210. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  52211. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  52212. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  52213. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  52214. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  52215. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  52216. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  52217. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  52218. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  52219. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  52220. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  52221. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  52222. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  52223. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  52224. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  52225. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  52226. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  52227. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  52228. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  52229. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  52230. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  52231. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  52232. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  52233. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  52234. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  52235. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  52236. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  52237. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  52238. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  52239. };
  52240. #ifdef WC_RSA_NO_PADDING
  52241. const unsigned char expEncMsgNoPad[] = {
  52242. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  52243. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  52244. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  52245. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  52246. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  52247. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  52248. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  52249. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  52250. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  52251. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  52252. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  52253. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  52254. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  52255. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  52256. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  52257. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  52258. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  52259. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  52260. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  52261. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  52262. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  52263. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  52264. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  52265. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  52266. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  52267. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  52268. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  52269. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  52270. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  52271. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  52272. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  52273. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  52274. };
  52275. #endif
  52276. #endif
  52277. const unsigned char* der;
  52278. XMEMSET(msg, 0x00, sizeof(msg));
  52279. der = privDer;
  52280. rsa = NULL;
  52281. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52282. ExpectIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  52283. ExpectIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  52284. -1);
  52285. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  52286. RSA_PKCS1_PADDING), -1);
  52287. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  52288. RSA_PKCS1_PADDING), -1);
  52289. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  52290. RSA_PKCS1_PADDING), -1);
  52291. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  52292. RSA_PKCS1_PSS_PADDING), -1);
  52293. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  52294. RSA_PKCS1_PADDING), sizeof(encMsg));
  52295. ExpectIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  52296. #ifdef WC_RSA_NO_PADDING
  52297. /* Non-zero message. */
  52298. XMEMSET(msg, 0x01, sizeof(msg));
  52299. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  52300. RSA_NO_PADDING), sizeof(encMsg));
  52301. ExpectIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  52302. #endif
  52303. RSA_free(rsa);
  52304. #endif
  52305. return EXPECT_RESULT();
  52306. }
  52307. static int test_wolfSSL_RSA_public_encrypt(void)
  52308. {
  52309. EXPECT_DECLS;
  52310. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52311. RSA* rsa = NULL;
  52312. const unsigned char msg[2048/8] = { 0 };
  52313. unsigned char encMsg[2048/8];
  52314. ExpectNotNull(rsa = RSA_new());
  52315. ExpectIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  52316. RSA_PKCS1_PADDING), -1);
  52317. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  52318. RSA_PKCS1_PADDING), -1);
  52319. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  52320. RSA_PKCS1_PADDING), -1);
  52321. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  52322. RSA_PKCS1_PADDING), -1);
  52323. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  52324. RSA_PKCS1_PSS_PADDING), -1);
  52325. /* Empty RSA key. */
  52326. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  52327. RSA_PKCS1_PADDING), -1);
  52328. RSA_free(rsa);
  52329. #endif
  52330. return EXPECT_RESULT();
  52331. }
  52332. static int test_wolfSSL_RSA_private_decrypt(void)
  52333. {
  52334. EXPECT_DECLS;
  52335. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52336. RSA* rsa = NULL;
  52337. unsigned char msg[2048/8];
  52338. const unsigned char encMsg[2048/8] = { 0 };
  52339. ExpectNotNull(rsa = RSA_new());
  52340. ExpectIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  52341. RSA_PKCS1_PADDING), -1);
  52342. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  52343. RSA_PKCS1_PADDING), -1);
  52344. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  52345. RSA_PKCS1_PADDING), -1);
  52346. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  52347. RSA_PKCS1_PADDING), -1);
  52348. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  52349. RSA_PKCS1_PSS_PADDING), -1);
  52350. /* Empty RSA key. */
  52351. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  52352. RSA_PKCS1_PADDING), -1);
  52353. RSA_free(rsa);
  52354. #endif
  52355. return EXPECT_RESULT();
  52356. }
  52357. static int test_wolfSSL_RSA_GenAdd(void)
  52358. {
  52359. EXPECT_DECLS;
  52360. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52361. RSA *rsa;
  52362. #ifdef USE_CERT_BUFFERS_1024
  52363. const unsigned char* privDer = client_key_der_1024;
  52364. size_t privDerSz = sizeof_client_key_der_1024;
  52365. const unsigned char* pubDer = client_keypub_der_1024;
  52366. size_t pubDerSz = sizeof_client_keypub_der_1024;
  52367. #else
  52368. const unsigned char* privDer = client_key_der_2048;
  52369. size_t privDerSz = sizeof_client_key_der_2048;
  52370. const unsigned char* pubDer = client_keypub_der_2048;
  52371. size_t pubDerSz = sizeof_client_keypub_der_2048;
  52372. #endif
  52373. const unsigned char* der;
  52374. der = privDer;
  52375. rsa = NULL;
  52376. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52377. ExpectIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  52378. #ifndef RSA_LOW_MEM
  52379. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  52380. #else
  52381. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  52382. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  52383. #endif
  52384. RSA_free(rsa);
  52385. der = pubDer;
  52386. rsa = NULL;
  52387. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  52388. /* Need private values. */
  52389. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  52390. RSA_free(rsa);
  52391. #endif
  52392. return EXPECT_RESULT();
  52393. }
  52394. static int test_wolfSSL_RSA_blinding_on(void)
  52395. {
  52396. EXPECT_DECLS;
  52397. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  52398. RSA *rsa;
  52399. WOLFSSL_BN_CTX *bnCtx = NULL;
  52400. #ifdef USE_CERT_BUFFERS_1024
  52401. const unsigned char* privDer = client_key_der_1024;
  52402. size_t privDerSz = sizeof_client_key_der_1024;
  52403. #else
  52404. const unsigned char* privDer = client_key_der_2048;
  52405. size_t privDerSz = sizeof_client_key_der_2048;
  52406. #endif
  52407. const unsigned char* der;
  52408. der = privDer;
  52409. rsa = NULL;
  52410. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52411. ExpectNotNull(bnCtx = wolfSSL_BN_CTX_new());
  52412. /* Does nothing so all parameters are valid. */
  52413. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  52414. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  52415. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  52416. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  52417. wolfSSL_BN_CTX_free(bnCtx);
  52418. RSA_free(rsa);
  52419. #endif
  52420. return EXPECT_RESULT();
  52421. }
  52422. static int test_wolfSSL_RSA_ex_data(void)
  52423. {
  52424. EXPECT_DECLS;
  52425. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  52426. RSA* rsa = NULL;
  52427. unsigned char data[1];
  52428. ExpectNotNull(rsa = RSA_new());
  52429. ExpectNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  52430. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  52431. #ifdef MAX_EX_DATA
  52432. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  52433. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  52434. #endif
  52435. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  52436. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  52437. #ifdef HAVE_EX_DATA
  52438. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  52439. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  52440. ExpectPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  52441. #else
  52442. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  52443. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  52444. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  52445. #endif
  52446. RSA_free(rsa);
  52447. #endif /* !NO_RSA && OPENSSL_EXTRA */
  52448. return EXPECT_RESULT();
  52449. }
  52450. static int test_wolfSSL_RSA_LoadDer(void)
  52451. {
  52452. EXPECT_DECLS;
  52453. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  52454. defined(OPENSSL_EXTRA_X509_SMALL))
  52455. RSA *rsa = NULL;
  52456. #ifdef USE_CERT_BUFFERS_1024
  52457. const unsigned char* privDer = client_key_der_1024;
  52458. size_t privDerSz = sizeof_client_key_der_1024;
  52459. #else
  52460. const unsigned char* privDer = client_key_der_2048;
  52461. size_t privDerSz = sizeof_client_key_der_2048;
  52462. #endif
  52463. ExpectNotNull(rsa = RSA_new());
  52464. ExpectIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  52465. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  52466. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  52467. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  52468. RSA_free(rsa);
  52469. #endif /* !NO_RSA && OPENSSL_EXTRA */
  52470. return EXPECT_RESULT();
  52471. }
  52472. /* Local API. */
  52473. static int test_wolfSSL_RSA_To_Der(void)
  52474. {
  52475. EXPECT_DECLS;
  52476. #ifdef WOLFSSL_TEST_STATIC_BUILD
  52477. #if defined(WOLFSSL_KEY_GEN) && defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52478. RSA* rsa;
  52479. #ifdef USE_CERT_BUFFERS_1024
  52480. const unsigned char* privDer = client_key_der_1024;
  52481. size_t privDerSz = sizeof_client_key_der_1024;
  52482. const unsigned char* pubDer = client_keypub_der_1024;
  52483. size_t pubDerSz = sizeof_client_keypub_der_1024;
  52484. unsigned char out[sizeof(client_key_der_1024)];
  52485. #else
  52486. const unsigned char* privDer = client_key_der_2048;
  52487. size_t privDerSz = sizeof_client_key_der_2048;
  52488. const unsigned char* pubDer = client_keypub_der_2048;
  52489. size_t pubDerSz = sizeof_client_keypub_der_2048;
  52490. unsigned char out[sizeof(client_key_der_2048)];
  52491. #endif
  52492. const unsigned char* der;
  52493. unsigned char* outDer = NULL;
  52494. der = privDer;
  52495. rsa = NULL;
  52496. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52497. ExpectIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  52498. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  52499. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  52500. outDer = out;
  52501. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  52502. ExpectIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  52503. outDer = NULL;
  52504. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  52505. ExpectNotNull(outDer);
  52506. ExpectIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  52507. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  52508. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  52509. outDer = out;
  52510. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  52511. ExpectIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  52512. RSA_free(rsa);
  52513. ExpectNotNull(rsa = RSA_new());
  52514. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  52515. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  52516. RSA_free(rsa);
  52517. der = pubDer;
  52518. rsa = NULL;
  52519. ExpectNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  52520. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  52521. RSA_free(rsa);
  52522. #endif
  52523. #endif
  52524. return EXPECT_RESULT();
  52525. }
  52526. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  52527. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  52528. {
  52529. EXPECT_DECLS;
  52530. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  52531. XFILE file = XBADFILE;
  52532. const char* fname = "./certs/server-keyPub.pem";
  52533. RSA *rsa = NULL;
  52534. ExpectNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  52535. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  52536. ExpectNotNull(rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL));
  52537. ExpectIntEQ(RSA_size(rsa), 256);
  52538. RSA_free(rsa);
  52539. if (file != XBADFILE)
  52540. XFCLOSE(file);
  52541. #endif
  52542. return EXPECT_RESULT();
  52543. }
  52544. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  52545. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  52546. {
  52547. EXPECT_DECLS;
  52548. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  52549. defined(WOLFSSL_KEY_GEN)
  52550. RSA* rsa = NULL;
  52551. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  52552. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, NULL), 0);
  52553. /* Valid but stub so returns 0. */
  52554. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, rsa), 0);
  52555. #endif
  52556. return EXPECT_RESULT();
  52557. }
  52558. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  52559. {
  52560. EXPECT_DECLS;
  52561. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  52562. (defined(WOLFSSL_PEM_TO_DER) || \
  52563. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  52564. RSA* rsa = NULL;
  52565. #ifdef USE_CERT_BUFFERS_1024
  52566. const unsigned char* privDer = client_key_der_1024;
  52567. size_t privDerSz = sizeof_client_key_der_1024;
  52568. #else
  52569. const unsigned char* privDer = client_key_der_2048;
  52570. size_t privDerSz = sizeof_client_key_der_2048;
  52571. #endif
  52572. const unsigned char* der;
  52573. #ifndef NO_AES
  52574. unsigned char passwd[] = "password";
  52575. #endif
  52576. ExpectNotNull(rsa = RSA_new());
  52577. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  52578. NULL, NULL), 0);
  52579. RSA_free(rsa);
  52580. der = privDer;
  52581. rsa = NULL;
  52582. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52583. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  52584. NULL, NULL), 0);
  52585. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0,
  52586. NULL, NULL), 0);
  52587. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  52588. NULL, NULL), 1);
  52589. #ifndef NO_AES
  52590. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  52591. NULL, 0, NULL, NULL), 1);
  52592. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  52593. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  52594. #endif
  52595. RSA_free(rsa);
  52596. #endif
  52597. return EXPECT_RESULT();
  52598. }
  52599. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  52600. {
  52601. EXPECT_DECLS;
  52602. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  52603. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  52604. RSA* rsa = NULL;
  52605. #ifdef USE_CERT_BUFFERS_1024
  52606. const unsigned char* privDer = client_key_der_1024;
  52607. size_t privDerSz = sizeof_client_key_der_1024;
  52608. #else
  52609. const unsigned char* privDer = client_key_der_2048;
  52610. size_t privDerSz = sizeof_client_key_der_2048;
  52611. #endif
  52612. const unsigned char* der;
  52613. #ifndef NO_AES
  52614. unsigned char passwd[] = "password";
  52615. #endif
  52616. unsigned char* pem = NULL;
  52617. int plen;
  52618. ExpectNotNull(rsa = RSA_new());
  52619. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  52620. &plen), 0);
  52621. RSA_free(rsa);
  52622. der = privDer;
  52623. rsa = NULL;
  52624. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52625. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  52626. &plen), 0);
  52627. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  52628. &plen), 0);
  52629. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  52630. NULL), 0);
  52631. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  52632. &plen), 1);
  52633. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  52634. pem = NULL;
  52635. #ifndef NO_AES
  52636. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  52637. NULL, 0, &pem, &plen), 1);
  52638. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  52639. pem = NULL;
  52640. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  52641. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  52642. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  52643. #endif
  52644. RSA_free(rsa);
  52645. #endif
  52646. return EXPECT_RESULT();
  52647. }
  52648. static int test_wolfSSL_DH(void)
  52649. {
  52650. EXPECT_DECLS;
  52651. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  52652. DH *dh = NULL;
  52653. BIGNUM* p;
  52654. BIGNUM* q;
  52655. BIGNUM* g;
  52656. BIGNUM* pub = NULL;
  52657. BIGNUM* priv = NULL;
  52658. #if defined(OPENSSL_ALL)
  52659. #if !defined(HAVE_FIPS) || \
  52660. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  52661. FILE* f = NULL;
  52662. unsigned char buf[268];
  52663. const unsigned char* pt = buf;
  52664. long len = 0;
  52665. dh = NULL;
  52666. XMEMSET(buf, 0, sizeof(buf));
  52667. /* Test 2048 bit parameters */
  52668. ExpectTrue((f = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  52669. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  52670. if (f != XBADFILE)
  52671. XFCLOSE(f);
  52672. ExpectNotNull(dh = d2i_DHparams(NULL, &pt, len));
  52673. ExpectNotNull(dh->p);
  52674. ExpectNotNull(dh->g);
  52675. ExpectTrue(pt == buf);
  52676. ExpectIntEQ(DH_generate_key(dh), 1);
  52677. ExpectIntEQ(DH_generate_key(dh), 1);
  52678. ExpectIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
  52679. ExpectNotNull(pub = BN_new());
  52680. ExpectIntEQ(BN_set_word(pub, 1), 1);
  52681. ExpectIntEQ(DH_compute_key(buf, NULL, NULL), -1);
  52682. ExpectIntEQ(DH_compute_key(NULL, pub, NULL), -1);
  52683. ExpectIntEQ(DH_compute_key(NULL, NULL, dh), -1);
  52684. ExpectIntEQ(DH_compute_key(buf, pub, NULL), -1);
  52685. ExpectIntEQ(DH_compute_key(buf, NULL, dh), -1);
  52686. ExpectIntEQ(DH_compute_key(NULL, pub, dh), -1);
  52687. ExpectIntEQ(DH_compute_key(buf, pub, dh), -1);
  52688. BN_free(pub);
  52689. pub = NULL;
  52690. DH_get0_pqg(dh, (const BIGNUM**)&p,
  52691. (const BIGNUM**)&q,
  52692. (const BIGNUM**)&g);
  52693. ExpectPtrEq(p, dh->p);
  52694. ExpectPtrEq(q, dh->q);
  52695. ExpectPtrEq(g, dh->g);
  52696. DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  52697. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  52698. ExpectPtrEq(pub, dh->pub_key);
  52699. ExpectPtrEq(priv, dh->priv_key);
  52700. DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
  52701. ExpectPtrEq(pub, dh->pub_key);
  52702. DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
  52703. ExpectPtrEq(priv, dh->priv_key);
  52704. pub = NULL;
  52705. priv = NULL;
  52706. ExpectNotNull(pub = BN_new());
  52707. ExpectNotNull(priv = BN_new());
  52708. ExpectIntEQ(DH_set0_key(NULL, pub, priv), 0);
  52709. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  52710. if (EXPECT_FAIL()) {
  52711. BN_free(pub);
  52712. BN_free(priv);
  52713. }
  52714. pub = NULL;
  52715. priv = NULL;
  52716. ExpectNotNull(pub = BN_new());
  52717. ExpectIntEQ(DH_set0_key(dh, pub, NULL), 1);
  52718. if (EXPECT_FAIL()) {
  52719. BN_free(pub);
  52720. }
  52721. ExpectNotNull(priv = BN_new());
  52722. ExpectIntEQ(DH_set0_key(dh, NULL, priv), 1);
  52723. if (EXPECT_FAIL()) {
  52724. BN_free(priv);
  52725. }
  52726. ExpectPtrEq(pub, dh->pub_key);
  52727. ExpectPtrEq(priv, dh->priv_key);
  52728. pub = NULL;
  52729. priv = NULL;
  52730. DH_free(dh);
  52731. dh = NULL;
  52732. ExpectNotNull(dh = DH_new());
  52733. p = NULL;
  52734. ExpectNotNull(p = BN_new());
  52735. ExpectIntEQ(BN_set_word(p, 1), 1);
  52736. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  52737. ExpectNotNull(pub = BN_new());
  52738. ExpectNotNull(priv = BN_new());
  52739. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  52740. if (EXPECT_FAIL()) {
  52741. BN_free(pub);
  52742. BN_free(priv);
  52743. }
  52744. pub = NULL;
  52745. priv = NULL;
  52746. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  52747. BN_free(p);
  52748. p = NULL;
  52749. DH_free(dh);
  52750. dh = NULL;
  52751. #ifdef WOLFSSL_KEY_GEN
  52752. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  52753. ExpectIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
  52754. DH_free(dh);
  52755. dh = NULL;
  52756. #endif
  52757. #endif /* !HAVE_FIPS || (HAVE_FIPS_VERSION && HAVE_FIPS_VERSION > 2) */
  52758. #endif /* OPENSSL_ALL */
  52759. (void)dh;
  52760. (void)p;
  52761. (void)q;
  52762. (void)g;
  52763. (void)pub;
  52764. (void)priv;
  52765. ExpectNotNull(dh = wolfSSL_DH_new());
  52766. /* invalid parameters test */
  52767. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  52768. (const BIGNUM**)&q,
  52769. (const BIGNUM**)&g);
  52770. DH_get0_pqg(dh, NULL,
  52771. (const BIGNUM**)&q,
  52772. (const BIGNUM**)&g);
  52773. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  52774. DH_get0_pqg(dh, NULL, NULL, NULL);
  52775. DH_get0_pqg(dh, (const BIGNUM**)&p,
  52776. (const BIGNUM**)&q,
  52777. (const BIGNUM**)&g);
  52778. ExpectPtrEq(p, NULL);
  52779. ExpectPtrEq(q, NULL);
  52780. ExpectPtrEq(g, NULL);
  52781. DH_free(dh);
  52782. dh = NULL;
  52783. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  52784. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  52785. #if defined(OPENSSL_ALL) || \
  52786. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  52787. dh = wolfSSL_DH_new();
  52788. ExpectNotNull(dh);
  52789. p = wolfSSL_BN_new();
  52790. ExpectNotNull(p);
  52791. ExpectIntEQ(BN_set_word(p, 11), 1);
  52792. g = wolfSSL_BN_new();
  52793. ExpectNotNull(g);
  52794. ExpectIntEQ(BN_set_word(g, 2), 1);
  52795. q = wolfSSL_BN_new();
  52796. ExpectNotNull(q);
  52797. ExpectIntEQ(BN_set_word(q, 5), 1);
  52798. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
  52799. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
  52800. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
  52801. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
  52802. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
  52803. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
  52804. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
  52805. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
  52806. /* Don't need q. */
  52807. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  52808. if (EXPECT_FAIL()) {
  52809. BN_free(p);
  52810. BN_free(g);
  52811. }
  52812. p = NULL;
  52813. g = NULL;
  52814. /* Setting again will free the p and g. */
  52815. wolfSSL_BN_free(q);
  52816. q = NULL;
  52817. DH_free(dh);
  52818. dh = NULL;
  52819. dh = wolfSSL_DH_new();
  52820. ExpectNotNull(dh);
  52821. p = wolfSSL_BN_new();
  52822. ExpectNotNull(p);
  52823. ExpectIntEQ(BN_set_word(p, 11), 1);
  52824. g = wolfSSL_BN_new();
  52825. ExpectNotNull(g);
  52826. ExpectIntEQ(BN_set_word(g, 2), 1);
  52827. q = wolfSSL_BN_new();
  52828. ExpectNotNull(q);
  52829. ExpectIntEQ(BN_set_word(q, 5), 1);
  52830. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
  52831. /* p, q and g are now owned by dh - don't free. */
  52832. if (EXPECT_FAIL()) {
  52833. BN_free(p);
  52834. BN_free(q);
  52835. BN_free(g);
  52836. }
  52837. p = NULL;
  52838. q = NULL;
  52839. g = NULL;
  52840. p = wolfSSL_BN_new();
  52841. ExpectNotNull(p);
  52842. ExpectIntEQ(BN_set_word(p, 11), 1);
  52843. g = wolfSSL_BN_new();
  52844. ExpectNotNull(g);
  52845. ExpectIntEQ(BN_set_word(g, 2), 1);
  52846. q = wolfSSL_BN_new();
  52847. ExpectNotNull(q);
  52848. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
  52849. if (EXPECT_FAIL()) {
  52850. BN_free(p);
  52851. }
  52852. p = NULL;
  52853. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
  52854. if (EXPECT_FAIL()) {
  52855. BN_free(q);
  52856. }
  52857. q = NULL;
  52858. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
  52859. if (EXPECT_FAIL()) {
  52860. BN_free(g);
  52861. }
  52862. g = NULL;
  52863. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
  52864. /* p, q and g are now owned by dh - don't free. */
  52865. DH_free(dh);
  52866. dh = NULL;
  52867. ExpectIntEQ(DH_generate_key(NULL), 0);
  52868. ExpectNotNull(dh = DH_new());
  52869. ExpectIntEQ(DH_generate_key(dh), 0);
  52870. p = wolfSSL_BN_new();
  52871. ExpectNotNull(p);
  52872. ExpectIntEQ(BN_set_word(p, 0), 1);
  52873. g = wolfSSL_BN_new();
  52874. ExpectNotNull(g);
  52875. ExpectIntEQ(BN_set_word(g, 2), 1);
  52876. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  52877. if (EXPECT_FAIL()) {
  52878. BN_free(p);
  52879. BN_free(g);
  52880. }
  52881. p = NULL;
  52882. g = NULL;
  52883. ExpectIntEQ(DH_generate_key(dh), 0);
  52884. DH_free(dh);
  52885. dh = NULL;
  52886. #endif
  52887. #endif
  52888. /* Test DH_up_ref() */
  52889. dh = wolfSSL_DH_new();
  52890. ExpectNotNull(dh);
  52891. ExpectIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  52892. ExpectIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  52893. DH_free(dh); /* decrease ref count */
  52894. DH_free(dh); /* free WOLFSSL_DH */
  52895. dh = NULL;
  52896. q = NULL;
  52897. ExpectNull((dh = DH_new_by_nid(NID_sha1)));
  52898. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  52899. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  52900. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  52901. #ifdef HAVE_FFDHE_2048
  52902. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  52903. DH_free(dh);
  52904. dh = NULL;
  52905. q = NULL;
  52906. #endif
  52907. #ifdef HAVE_FFDHE_3072
  52908. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  52909. DH_free(dh);
  52910. dh = NULL;
  52911. q = NULL;
  52912. #endif
  52913. #ifdef HAVE_FFDHE_4096
  52914. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  52915. DH_free(dh);
  52916. dh = NULL;
  52917. q = NULL;
  52918. #endif
  52919. #else
  52920. ExpectNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  52921. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  52922. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  52923. ExpectIntEQ(wolfSSL_DH_size(NULL), -1);
  52924. #endif /* OPENSSL_EXTRA && !NO_DH */
  52925. return EXPECT_RESULT();
  52926. }
  52927. static int test_wolfSSL_DH_dup(void)
  52928. {
  52929. EXPECT_DECLS;
  52930. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  52931. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  52932. defined(OPENSSL_EXTRA)
  52933. DH *dh = NULL;
  52934. DH *dhDup = NULL;
  52935. ExpectNotNull(dh = wolfSSL_DH_new());
  52936. ExpectNull(dhDup = wolfSSL_DH_dup(NULL));
  52937. ExpectNull(dhDup = wolfSSL_DH_dup(dh));
  52938. #if defined(OPENSSL_ALL) || \
  52939. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  52940. {
  52941. WOLFSSL_BIGNUM* p = NULL;
  52942. WOLFSSL_BIGNUM* g = NULL;
  52943. ExpectNotNull(p = wolfSSL_BN_new());
  52944. ExpectNotNull(g = wolfSSL_BN_new());
  52945. ExpectIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
  52946. ExpectIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
  52947. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  52948. if (EXPECT_FAIL()) {
  52949. wolfSSL_BN_free(p);
  52950. wolfSSL_BN_free(g);
  52951. }
  52952. ExpectNotNull(dhDup = wolfSSL_DH_dup(dh));
  52953. wolfSSL_DH_free(dhDup);
  52954. }
  52955. #endif
  52956. wolfSSL_DH_free(dh);
  52957. #endif
  52958. #endif
  52959. return EXPECT_RESULT();
  52960. }
  52961. static int test_wolfSSL_DH_check(void)
  52962. {
  52963. EXPECT_DECLS;
  52964. #ifdef OPENSSL_ALL
  52965. #ifndef NO_DH
  52966. #ifndef NO_BIO
  52967. #ifndef NO_DSA
  52968. byte buf[6000];
  52969. char file[] = "./certs/dsaparams.pem";
  52970. XFILE f = XBADFILE;
  52971. int bytes;
  52972. BIO* bio = NULL;
  52973. DSA* dsa = NULL;
  52974. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  52975. static const byte dh2048[] = {
  52976. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  52977. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  52978. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  52979. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  52980. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  52981. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  52982. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  52983. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  52984. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  52985. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  52986. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  52987. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  52988. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  52989. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  52990. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  52991. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  52992. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  52993. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  52994. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  52995. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  52996. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  52997. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  52998. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  52999. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  53000. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  53001. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  53002. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  53003. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  53004. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  53005. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  53006. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  53007. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  53008. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  53009. 0x93, 0x02, 0x01, 0x02
  53010. };
  53011. const byte* params;
  53012. #endif
  53013. DH* dh = NULL;
  53014. WOLFSSL_BIGNUM* p = NULL;
  53015. WOLFSSL_BIGNUM* g = NULL;
  53016. WOLFSSL_BIGNUM* pTmp = NULL;
  53017. WOLFSSL_BIGNUM* gTmp = NULL;
  53018. int codes = -1;
  53019. #ifndef NO_DSA
  53020. /* Initialize DH */
  53021. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  53022. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  53023. if (f != XBADFILE)
  53024. XFCLOSE(f);
  53025. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  53026. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  53027. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  53028. ExpectNotNull(dh);
  53029. BIO_free(bio);
  53030. DSA_free(dsa);
  53031. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  53032. params = dh2048;
  53033. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &params,
  53034. (long)sizeof(dh2048)));
  53035. #else
  53036. ExpectNotNull(dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048));
  53037. #endif
  53038. /* Test assumed to be valid dh.
  53039. * Should return WOLFSSL_SUCCESS
  53040. * codes should be 0
  53041. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  53042. */
  53043. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  53044. ExpectIntEQ(codes, 0);
  53045. /* Test NULL dh: expected BAD_FUNC_ARG */
  53046. ExpectIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
  53047. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  53048. if (dh != NULL) {
  53049. pTmp = dh->p;
  53050. dh->p = NULL;
  53051. }
  53052. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  53053. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  53054. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  53055. /* set dh->p back to normal so it won't fail on next tests */
  53056. if (dh != NULL) {
  53057. dh->p = pTmp;
  53058. pTmp = NULL;
  53059. }
  53060. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  53061. if (dh != NULL) {
  53062. gTmp = dh->g;
  53063. dh->g = NULL;
  53064. }
  53065. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  53066. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  53067. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  53068. if (dh != NULL) {
  53069. dh->g = gTmp;
  53070. gTmp = NULL;
  53071. }
  53072. /* Cleanup */
  53073. DH_free(dh);
  53074. dh = NULL;
  53075. dh = DH_new();
  53076. ExpectNotNull(dh);
  53077. /* Check empty DH. */
  53078. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  53079. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  53080. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
  53081. /* Check non-prime valued p. */
  53082. ExpectNotNull(p = BN_new());
  53083. ExpectIntEQ(BN_set_word(p, 4), 1);
  53084. ExpectNotNull(g = BN_new());
  53085. ExpectIntEQ(BN_set_word(g, 2), 1);
  53086. ExpectIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
  53087. if (EXPECT_FAIL()) {
  53088. wolfSSL_BN_free(p);
  53089. wolfSSL_BN_free(g);
  53090. }
  53091. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  53092. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  53093. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  53094. DH_free(dh);
  53095. dh = NULL;
  53096. #endif
  53097. #endif /* !NO_DH && !NO_DSA */
  53098. #endif
  53099. return EXPECT_RESULT();
  53100. }
  53101. static int test_wolfSSL_DH_prime(void)
  53102. {
  53103. EXPECT_DECLS;
  53104. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  53105. WOLFSSL_BIGNUM* bn = NULL;
  53106. #if WOLFSSL_MAX_BN_BITS >= 768
  53107. WOLFSSL_BIGNUM* bn2 = NULL;
  53108. #endif
  53109. bn = wolfSSL_DH_768_prime(NULL);
  53110. #if WOLFSSL_MAX_BN_BITS >= 768
  53111. ExpectNotNull(bn);
  53112. bn2 = wolfSSL_DH_768_prime(bn);
  53113. ExpectNotNull(bn2);
  53114. ExpectTrue(bn == bn2);
  53115. wolfSSL_BN_free(bn);
  53116. bn = NULL;
  53117. #else
  53118. ExpectNull(bn);
  53119. #endif
  53120. bn = wolfSSL_DH_1024_prime(NULL);
  53121. #if WOLFSSL_MAX_BN_BITS >= 1024
  53122. ExpectNotNull(bn);
  53123. wolfSSL_BN_free(bn);
  53124. bn = NULL;
  53125. #else
  53126. ExpectNull(bn);
  53127. #endif
  53128. bn = wolfSSL_DH_2048_prime(NULL);
  53129. #if WOLFSSL_MAX_BN_BITS >= 2048
  53130. ExpectNotNull(bn);
  53131. wolfSSL_BN_free(bn);
  53132. bn = NULL;
  53133. #else
  53134. ExpectNull(bn);
  53135. #endif
  53136. bn = wolfSSL_DH_3072_prime(NULL);
  53137. #if WOLFSSL_MAX_BN_BITS >= 3072
  53138. ExpectNotNull(bn);
  53139. wolfSSL_BN_free(bn);
  53140. bn = NULL;
  53141. #else
  53142. ExpectNull(bn);
  53143. #endif
  53144. bn = wolfSSL_DH_4096_prime(NULL);
  53145. #if WOLFSSL_MAX_BN_BITS >= 4096
  53146. ExpectNotNull(bn);
  53147. wolfSSL_BN_free(bn);
  53148. bn = NULL;
  53149. #else
  53150. ExpectNull(bn);
  53151. #endif
  53152. bn = wolfSSL_DH_6144_prime(NULL);
  53153. #if WOLFSSL_MAX_BN_BITS >= 6144
  53154. ExpectNotNull(bn);
  53155. wolfSSL_BN_free(bn);
  53156. bn = NULL;
  53157. #else
  53158. ExpectNull(bn);
  53159. #endif
  53160. bn = wolfSSL_DH_8192_prime(NULL);
  53161. #if WOLFSSL_MAX_BN_BITS >= 8192
  53162. ExpectNotNull(bn);
  53163. wolfSSL_BN_free(bn);
  53164. bn = NULL;
  53165. #else
  53166. ExpectNull(bn);
  53167. #endif
  53168. #endif
  53169. return EXPECT_RESULT();
  53170. }
  53171. static int test_wolfSSL_DH_1536_prime(void)
  53172. {
  53173. EXPECT_DECLS;
  53174. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  53175. BIGNUM* bn = NULL;
  53176. unsigned char bits[200];
  53177. int sz = 192; /* known binary size */
  53178. const byte expected[] = {
  53179. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  53180. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  53181. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  53182. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  53183. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  53184. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  53185. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  53186. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  53187. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  53188. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  53189. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  53190. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  53191. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  53192. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  53193. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  53194. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  53195. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  53196. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  53197. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  53198. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  53199. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  53200. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  53201. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  53202. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  53203. };
  53204. ExpectNotNull(bn = get_rfc3526_prime_1536(NULL));
  53205. ExpectIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  53206. ExpectIntEQ(0, XMEMCMP(expected, bits, sz));
  53207. BN_free(bn);
  53208. #endif
  53209. return EXPECT_RESULT();
  53210. }
  53211. static int test_wolfSSL_DH_get_2048_256(void)
  53212. {
  53213. EXPECT_DECLS;
  53214. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  53215. WOLFSSL_DH* dh = NULL;
  53216. const WOLFSSL_BIGNUM* pBn;
  53217. const WOLFSSL_BIGNUM* gBn;
  53218. const WOLFSSL_BIGNUM* qBn;
  53219. const byte pExpected[] = {
  53220. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  53221. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  53222. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  53223. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  53224. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  53225. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  53226. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  53227. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  53228. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  53229. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  53230. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  53231. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  53232. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  53233. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  53234. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  53235. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  53236. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  53237. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  53238. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  53239. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  53240. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  53241. 0x1E, 0x1A, 0x15, 0x97
  53242. };
  53243. const byte gExpected[] = {
  53244. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  53245. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  53246. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  53247. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  53248. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  53249. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  53250. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  53251. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  53252. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  53253. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  53254. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  53255. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  53256. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  53257. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  53258. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  53259. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  53260. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  53261. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  53262. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  53263. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  53264. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  53265. 0x6C, 0xC4, 0x16, 0x59
  53266. };
  53267. const byte qExpected[] = {
  53268. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  53269. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  53270. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  53271. };
  53272. int pSz = 0;
  53273. int qSz = 0;
  53274. int gSz = 0;
  53275. byte* pReturned = NULL;
  53276. byte* qReturned = NULL;
  53277. byte* gReturned = NULL;
  53278. ExpectNotNull((dh = wolfSSL_DH_get_2048_256()));
  53279. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  53280. ExpectIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  53281. ExpectNotNull(pReturned = (byte*)XMALLOC(pSz, NULL,
  53282. DYNAMIC_TYPE_TMP_BUFFER));
  53283. ExpectIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  53284. ExpectIntEQ(pSz, sizeof(pExpected));
  53285. ExpectIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  53286. ExpectIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  53287. ExpectNotNull(qReturned = (byte*)XMALLOC(qSz, NULL,
  53288. DYNAMIC_TYPE_TMP_BUFFER));
  53289. ExpectIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  53290. ExpectIntEQ(qSz, sizeof(qExpected));
  53291. ExpectIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  53292. ExpectIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  53293. ExpectNotNull(gReturned = (byte*)XMALLOC(gSz, NULL,
  53294. DYNAMIC_TYPE_TMP_BUFFER));
  53295. ExpectIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  53296. ExpectIntEQ(gSz, sizeof(gExpected));
  53297. ExpectIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  53298. wolfSSL_DH_free(dh);
  53299. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53300. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53301. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53302. #endif
  53303. return EXPECT_RESULT();
  53304. }
  53305. static int test_wolfSSL_PEM_write_DHparams(void)
  53306. {
  53307. EXPECT_DECLS;
  53308. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  53309. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  53310. DH* dh = NULL;
  53311. BIO* bio = NULL;
  53312. XFILE fp = XBADFILE;
  53313. byte pem[2048];
  53314. int pemSz = 0;
  53315. const char expected[] =
  53316. "-----BEGIN DH PARAMETERS-----\n"
  53317. "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
  53318. "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
  53319. "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
  53320. "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
  53321. "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
  53322. "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
  53323. "-----END DH PARAMETERS-----\n";
  53324. const char badPem[] =
  53325. "-----BEGIN DH PARAMETERS-----\n"
  53326. "-----END DH PARAMETERS-----\n";
  53327. const char emptySeqPem[] =
  53328. "-----BEGIN DH PARAMETERS-----\n"
  53329. "MAA=\n"
  53330. "-----END DH PARAMETERS-----\n";
  53331. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  53332. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  53333. if (fp != XBADFILE) {
  53334. XFCLOSE(fp);
  53335. fp = XBADFILE;
  53336. }
  53337. ExpectNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
  53338. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  53339. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  53340. ExpectIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
  53341. (int)sizeof(badPem));
  53342. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  53343. BIO_free(bio);
  53344. bio = NULL;
  53345. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  53346. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  53347. ExpectIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
  53348. (int)sizeof(emptySeqPem));
  53349. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  53350. BIO_free(bio);
  53351. bio = NULL;
  53352. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  53353. ExpectIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  53354. ExpectNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  53355. BIO_free(bio);
  53356. bio = NULL;
  53357. ExpectNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  53358. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  53359. ExpectIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  53360. DH_free(dh);
  53361. dh = NULL;
  53362. dh = wolfSSL_DH_new();
  53363. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
  53364. if (fp != XBADFILE) {
  53365. XFCLOSE(fp);
  53366. fp = XBADFILE;
  53367. }
  53368. wolfSSL_DH_free(dh);
  53369. dh = NULL;
  53370. /* check results */
  53371. XMEMSET(pem, 0, sizeof(pem));
  53372. ExpectTrue((fp = XFOPEN("./test-write-dhparams.pem", "rb")) != XBADFILE);
  53373. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  53374. ExpectIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  53375. if (fp != XBADFILE)
  53376. XFCLOSE(fp);
  53377. #endif
  53378. return EXPECT_RESULT();
  53379. }
  53380. static int test_wolfSSL_d2i_DHparams(void)
  53381. {
  53382. EXPECT_DECLS;
  53383. #ifdef OPENSSL_ALL
  53384. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  53385. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  53386. XFILE f = XBADFILE;
  53387. unsigned char buf[4096];
  53388. const unsigned char* pt = buf;
  53389. #ifdef HAVE_FFDHE_2048
  53390. const char* params1 = "./certs/dh2048.der";
  53391. #endif
  53392. #ifdef HAVE_FFDHE_3072
  53393. const char* params2 = "./certs/dh3072.der";
  53394. #endif
  53395. long len = 0;
  53396. WOLFSSL_DH* dh = NULL;
  53397. XMEMSET(buf, 0, sizeof(buf));
  53398. /* Test 2048 bit parameters */
  53399. #ifdef HAVE_FFDHE_2048
  53400. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  53401. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  53402. if (f != XBADFILE) {
  53403. XFCLOSE(f);
  53404. f = XBADFILE;
  53405. }
  53406. /* Valid case */
  53407. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  53408. ExpectNotNull(dh->p);
  53409. ExpectNotNull(dh->g);
  53410. ExpectTrue(pt == buf);
  53411. ExpectIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
  53412. ExpectIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
  53413. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  53414. /* Invalid cases */
  53415. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  53416. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  53417. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  53418. DH_free(dh);
  53419. dh = NULL;
  53420. *buf = 0;
  53421. pt = buf;
  53422. #endif /* HAVE_FFDHE_2048 */
  53423. /* Test 3072 bit parameters */
  53424. #ifdef HAVE_FFDHE_3072
  53425. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  53426. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  53427. if (f != XBADFILE) {
  53428. XFCLOSE(f);
  53429. f = XBADFILE;
  53430. }
  53431. /* Valid case */
  53432. ExpectNotNull(dh = wolfSSL_d2i_DHparams(&dh, &pt, len));
  53433. ExpectNotNull(dh->p);
  53434. ExpectNotNull(dh->g);
  53435. ExpectTrue(pt != buf);
  53436. ExpectIntEQ(DH_generate_key(dh), 1);
  53437. /* Invalid cases */
  53438. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  53439. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  53440. DH_free(dh);
  53441. dh = NULL;
  53442. #endif /* HAVE_FFDHE_3072 */
  53443. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  53444. #endif /* !NO_DH */
  53445. #endif
  53446. return EXPECT_RESULT();
  53447. }
  53448. static int test_wolfSSL_DH_LoadDer(void)
  53449. {
  53450. EXPECT_DECLS;
  53451. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
  53452. defined(OPENSSL_EXTRA)
  53453. static const byte dh2048[] = {
  53454. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  53455. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  53456. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  53457. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  53458. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  53459. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  53460. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  53461. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  53462. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  53463. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  53464. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  53465. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  53466. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  53467. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  53468. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  53469. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  53470. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  53471. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  53472. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  53473. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  53474. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  53475. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  53476. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  53477. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  53478. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  53479. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  53480. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  53481. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  53482. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  53483. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  53484. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  53485. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  53486. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  53487. 0x93, 0x02, 0x01, 0x02
  53488. };
  53489. WOLFSSL_DH* dh = NULL;
  53490. ExpectNotNull(dh = wolfSSL_DH_new());
  53491. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
  53492. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
  53493. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
  53494. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
  53495. wolfSSL_DH_free(dh);
  53496. #endif
  53497. return EXPECT_RESULT();
  53498. }
  53499. static int test_wolfSSL_i2d_DHparams(void)
  53500. {
  53501. EXPECT_DECLS;
  53502. #ifdef OPENSSL_ALL
  53503. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  53504. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  53505. XFILE f = XBADFILE;
  53506. unsigned char buf[4096];
  53507. const unsigned char* pt;
  53508. unsigned char* pt2;
  53509. #ifdef HAVE_FFDHE_2048
  53510. const char* params1 = "./certs/dh2048.der";
  53511. #endif
  53512. #ifdef HAVE_FFDHE_3072
  53513. const char* params2 = "./certs/dh3072.der";
  53514. #endif
  53515. long len = 0;
  53516. WOLFSSL_DH* dh = NULL;
  53517. /* Test 2048 bit parameters */
  53518. #ifdef HAVE_FFDHE_2048
  53519. pt = buf;
  53520. pt2 = buf;
  53521. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  53522. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  53523. if (f != XBADFILE) {
  53524. XFCLOSE(f);
  53525. f = XBADFILE;
  53526. }
  53527. /* Valid case */
  53528. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  53529. ExpectTrue(pt == buf);
  53530. ExpectIntEQ(DH_generate_key(dh), 1);
  53531. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  53532. /* Invalid case */
  53533. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  53534. /* Return length only */
  53535. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  53536. DH_free(dh);
  53537. dh = NULL;
  53538. *buf = 0;
  53539. #endif
  53540. /* Test 3072 bit parameters */
  53541. #ifdef HAVE_FFDHE_3072
  53542. pt = buf;
  53543. pt2 = buf;
  53544. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  53545. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  53546. if (f != XBADFILE) {
  53547. XFCLOSE(f);
  53548. f = XBADFILE;
  53549. }
  53550. /* Valid case */
  53551. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  53552. ExpectTrue(pt == buf);
  53553. ExpectIntEQ(DH_generate_key(dh), 1);
  53554. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  53555. /* Invalid case */
  53556. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  53557. /* Return length only */
  53558. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  53559. DH_free(dh);
  53560. dh = NULL;
  53561. #endif
  53562. dh = DH_new();
  53563. ExpectNotNull(dh);
  53564. pt2 = buf;
  53565. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
  53566. DH_free(dh);
  53567. dh = NULL;
  53568. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  53569. #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
  53570. #endif
  53571. return EXPECT_RESULT();
  53572. }
  53573. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  53574. /*----------------------------------------------------------------------------*
  53575. | EC
  53576. *----------------------------------------------------------------------------*/
  53577. static int test_wolfSSL_EC_GROUP(void)
  53578. {
  53579. EXPECT_DECLS;
  53580. #ifdef OPENSSL_EXTRA
  53581. EC_GROUP *group = NULL;
  53582. EC_GROUP *group2 = NULL;
  53583. EC_GROUP *group3 = NULL;
  53584. #ifndef HAVE_ECC_BRAINPOOL
  53585. EC_GROUP *group4 = NULL;
  53586. #endif
  53587. WOLFSSL_BIGNUM* order = NULL;
  53588. int group_bits;
  53589. int i;
  53590. static const int knownEccNids[] = {
  53591. NID_X9_62_prime192v1,
  53592. NID_X9_62_prime192v2,
  53593. NID_X9_62_prime192v3,
  53594. NID_X9_62_prime239v1,
  53595. NID_X9_62_prime239v2,
  53596. NID_X9_62_prime239v3,
  53597. NID_X9_62_prime256v1,
  53598. NID_secp112r1,
  53599. NID_secp112r2,
  53600. NID_secp128r1,
  53601. NID_secp128r2,
  53602. NID_secp160r1,
  53603. NID_secp160r2,
  53604. NID_secp224r1,
  53605. NID_secp384r1,
  53606. NID_secp521r1,
  53607. NID_secp160k1,
  53608. NID_secp192k1,
  53609. NID_secp224k1,
  53610. NID_secp256k1,
  53611. NID_brainpoolP160r1,
  53612. NID_brainpoolP192r1,
  53613. NID_brainpoolP224r1,
  53614. NID_brainpoolP256r1,
  53615. NID_brainpoolP320r1,
  53616. NID_brainpoolP384r1,
  53617. NID_brainpoolP512r1,
  53618. };
  53619. int knowEccNidsLen = (int)(sizeof(knownEccNids) / sizeof(*knownEccNids));
  53620. static const int knownEccEnums[] = {
  53621. ECC_SECP192R1,
  53622. ECC_PRIME192V2,
  53623. ECC_PRIME192V3,
  53624. ECC_PRIME239V1,
  53625. ECC_PRIME239V2,
  53626. ECC_PRIME239V3,
  53627. ECC_SECP256R1,
  53628. ECC_SECP112R1,
  53629. ECC_SECP112R2,
  53630. ECC_SECP128R1,
  53631. ECC_SECP128R2,
  53632. ECC_SECP160R1,
  53633. ECC_SECP160R2,
  53634. ECC_SECP224R1,
  53635. ECC_SECP384R1,
  53636. ECC_SECP521R1,
  53637. ECC_SECP160K1,
  53638. ECC_SECP192K1,
  53639. ECC_SECP224K1,
  53640. ECC_SECP256K1,
  53641. ECC_BRAINPOOLP160R1,
  53642. ECC_BRAINPOOLP192R1,
  53643. ECC_BRAINPOOLP224R1,
  53644. ECC_BRAINPOOLP256R1,
  53645. ECC_BRAINPOOLP320R1,
  53646. ECC_BRAINPOOLP384R1,
  53647. ECC_BRAINPOOLP512R1,
  53648. };
  53649. int knowEccEnumsLen = (int)(sizeof(knownEccEnums) / sizeof(*knownEccEnums));
  53650. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  53651. ExpectNotNull(group2 = EC_GROUP_dup(group));
  53652. ExpectNotNull(group3 = wolfSSL_EC_GROUP_new_by_curve_name(NID_secp384r1));
  53653. #ifndef HAVE_ECC_BRAINPOOL
  53654. ExpectNotNull(group4 = wolfSSL_EC_GROUP_new_by_curve_name(
  53655. NID_brainpoolP256r1));
  53656. #endif
  53657. ExpectNull(EC_GROUP_dup(NULL));
  53658. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(NULL), 0);
  53659. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  53660. ExpectIntEQ((group_bits = EC_GROUP_order_bits(NULL)), 0);
  53661. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  53662. #ifndef HAVE_ECC_BRAINPOOL
  53663. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group4)), 0);
  53664. #endif
  53665. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(NULL), 0);
  53666. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(group), 256);
  53667. ExpectNotNull(order = BN_new());
  53668. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, NULL, NULL), 0);
  53669. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, NULL, NULL), 0);
  53670. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, order, NULL), 0);
  53671. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, order, NULL), 1);
  53672. wolfSSL_BN_free(order);
  53673. ExpectNotNull(EC_GROUP_method_of(group));
  53674. ExpectIntEQ(EC_METHOD_get_field_type(NULL), 0);
  53675. ExpectIntEQ(EC_METHOD_get_field_type(EC_GROUP_method_of(group)),
  53676. NID_X9_62_prime_field);
  53677. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, NULL, NULL), -1);
  53678. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, NULL, NULL), -1);
  53679. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, group, NULL), -1);
  53680. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, group3, NULL), 1);
  53681. #ifndef NO_WOLFSSL_STUB
  53682. wolfSSL_EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  53683. #endif
  53684. #ifndef HAVE_ECC_BRAINPOOL
  53685. EC_GROUP_free(group4);
  53686. #endif
  53687. EC_GROUP_free(group3);
  53688. EC_GROUP_free(group2);
  53689. EC_GROUP_free(group);
  53690. for (i = 0; i < knowEccNidsLen; i++) {
  53691. group = NULL;
  53692. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccNids[i]));
  53693. ExpectIntGT(wolfSSL_EC_GROUP_get_degree(group), 0);
  53694. EC_GROUP_free(group);
  53695. }
  53696. for (i = 0; i < knowEccEnumsLen; i++) {
  53697. group = NULL;
  53698. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccEnums[i]));
  53699. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), knownEccNids[i]);
  53700. EC_GROUP_free(group);
  53701. }
  53702. #endif
  53703. return EXPECT_RESULT();
  53704. }
  53705. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  53706. {
  53707. EXPECT_DECLS;
  53708. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  53709. EC_GROUP *group = NULL;
  53710. BIO* bio = NULL;
  53711. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  53712. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  53713. EC_GROUP *ret = NULL;
  53714. static char ec_nc_p384[] = "-----BEGIN EC PARAMETERS-----\n"
  53715. "BgUrgQQAIg==\n"
  53716. "-----END EC PARAMETERS-----";
  53717. #endif
  53718. static char ec_nc_bad_1[] = "-----BEGIN EC PARAMETERS-----\n"
  53719. "MAA=\n"
  53720. "-----END EC PARAMETERS-----";
  53721. static char ec_nc_bad_2[] = "-----BEGIN EC PARAMETERS-----\n"
  53722. "BgA=\n"
  53723. "-----END EC PARAMETERS-----";
  53724. static char ec_nc_bad_3[] = "-----BEGIN EC PARAMETERS-----\n"
  53725. "BgE=\n"
  53726. "-----END EC PARAMETERS-----";
  53727. static char ec_nc_bad_4[] = "-----BEGIN EC PARAMETERS-----\n"
  53728. "BgE*\n"
  53729. "-----END EC PARAMETERS-----";
  53730. /* Test that first parameter, bio, being NULL fails. */
  53731. ExpectNull(PEM_read_bio_ECPKParameters(NULL, NULL, NULL, NULL));
  53732. /* Test that reading named parameters works. */
  53733. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  53734. ExpectIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  53735. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53736. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  53737. BIO_free(bio);
  53738. bio = NULL;
  53739. EC_GROUP_free(group);
  53740. group = NULL;
  53741. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  53742. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  53743. /* Test that reusing group works. */
  53744. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  53745. sizeof(ec_nc_p384)));
  53746. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  53747. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  53748. BIO_free(bio);
  53749. bio = NULL;
  53750. EC_GROUP_free(group);
  53751. group = NULL;
  53752. /* Test that returning through group works. */
  53753. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  53754. sizeof(ec_nc_p384)));
  53755. ExpectNotNull(ret = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  53756. ExpectIntEQ(group == ret, 1);
  53757. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  53758. BIO_free(bio);
  53759. bio = NULL;
  53760. EC_GROUP_free(group);
  53761. group = NULL;
  53762. #endif
  53763. /* Test 0x30, 0x00 (not and object id) fails. */
  53764. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_1,
  53765. sizeof(ec_nc_bad_1)));
  53766. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53767. BIO_free(bio);
  53768. bio = NULL;
  53769. /* Test 0x06, 0x00 (empty object id) fails. */
  53770. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_2,
  53771. sizeof(ec_nc_bad_2)));
  53772. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53773. BIO_free(bio);
  53774. bio = NULL;
  53775. /* Test 0x06, 0x01 (badly formed object id) fails. */
  53776. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_3,
  53777. sizeof(ec_nc_bad_3)));
  53778. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53779. BIO_free(bio);
  53780. bio = NULL;
  53781. /* Test invalid PEM encoding - invalid character. */
  53782. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_4,
  53783. sizeof(ec_nc_bad_4)));
  53784. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53785. BIO_free(bio);
  53786. #endif
  53787. return EXPECT_RESULT();
  53788. }
  53789. static int test_wolfSSL_EC_POINT(void)
  53790. {
  53791. EXPECT_DECLS;
  53792. #if !defined(WOLFSSL_SP_MATH) && \
  53793. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  53794. #ifdef OPENSSL_EXTRA
  53795. BN_CTX* ctx = NULL;
  53796. EC_GROUP* group = NULL;
  53797. #ifndef HAVE_ECC_BRAINPOOL
  53798. EC_GROUP* group2 = NULL;
  53799. #endif
  53800. EC_POINT* Gxy = NULL;
  53801. EC_POINT* new_point = NULL;
  53802. EC_POINT* set_point = NULL;
  53803. EC_POINT* infinity = NULL;
  53804. BIGNUM* k = NULL;
  53805. BIGNUM* Gx = NULL;
  53806. BIGNUM* Gy = NULL;
  53807. BIGNUM* Gz = NULL;
  53808. BIGNUM* X = NULL;
  53809. BIGNUM* Y = NULL;
  53810. BIGNUM* set_point_bn = NULL;
  53811. char* hexStr = NULL;
  53812. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD"
  53813. "17AF381913FF7A96314EA47055EA0FD0";
  53814. /* NISTP256R1 Gx/Gy */
  53815. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F2"
  53816. "77037D812DEB33A0F4A13945D898C296";
  53817. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  53818. "2BCE33576B315ECECBB6406837BF51F5";
  53819. #ifndef HAVE_SELFTEST
  53820. EC_POINT *tmp = NULL;
  53821. size_t bin_len;
  53822. unsigned int blen = 0;
  53823. unsigned char* buf = NULL;
  53824. unsigned char bufInf[1] = { 0x00 };
  53825. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F2"
  53826. "77037D812DEB33A0F4A13945D898C296"
  53827. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  53828. "2BCE33576B315ECECBB6406837BF51F5";
  53829. const unsigned char binUncompG[] = {
  53830. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  53831. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  53832. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  53833. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  53834. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  53835. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  53836. };
  53837. const unsigned char binUncompGBad[] = {
  53838. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  53839. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  53840. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  53841. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  53842. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  53843. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  53844. };
  53845. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F2"
  53846. "77037D812DEB33A0F4A13945D898C296";
  53847. #ifdef HAVE_COMP_KEY
  53848. const unsigned char binCompG[] = {
  53849. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  53850. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  53851. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  53852. };
  53853. #endif
  53854. #endif
  53855. ExpectNotNull(ctx = BN_CTX_new());
  53856. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  53857. #ifndef HAVE_ECC_BRAINPOOL
  53858. /* Used to make groups curve_idx == -1. */
  53859. ExpectNotNull(group2 = EC_GROUP_new_by_curve_name(NID_brainpoolP256r1));
  53860. #endif
  53861. ExpectNull(EC_POINT_new(NULL));
  53862. ExpectNotNull(Gxy = EC_POINT_new(group));
  53863. ExpectNotNull(new_point = EC_POINT_new(group));
  53864. ExpectNotNull(set_point = EC_POINT_new(group));
  53865. ExpectNotNull(X = BN_new());
  53866. ExpectNotNull(Y = BN_new());
  53867. ExpectNotNull(set_point_bn = BN_new());
  53868. ExpectNotNull(infinity = EC_POINT_new(group));
  53869. /* load test values */
  53870. ExpectIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  53871. ExpectIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  53872. ExpectIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  53873. ExpectIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  53874. /* populate coordinates for input point */
  53875. if (Gxy != NULL) {
  53876. Gxy->X = Gx;
  53877. Gxy->Y = Gy;
  53878. Gxy->Z = Gz;
  53879. }
  53880. /* Test handling of NULL point. */
  53881. EC_POINT_clear_free(NULL);
  53882. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  53883. NULL, NULL, ctx), 0);
  53884. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  53885. NULL, NULL, ctx), 0);
  53886. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  53887. NULL, NULL, ctx), 0);
  53888. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  53889. X, NULL, ctx), 0);
  53890. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  53891. NULL, Y, ctx), 0);
  53892. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  53893. X, Y, ctx), 0);
  53894. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  53895. X, Y, ctx), 0);
  53896. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  53897. NULL, Y, ctx), 0);
  53898. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  53899. X, NULL, ctx), 0);
  53900. /* Getting point at infinity returns an error. */
  53901. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, infinity,
  53902. X, Y, ctx), 0);
  53903. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  53904. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  53905. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  53906. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, NULL, ctx), 0);
  53907. ExpectIntEQ(EC_POINT_add(group, NULL, NULL, NULL, ctx), 0);
  53908. ExpectIntEQ(EC_POINT_add(NULL, new_point, NULL, NULL, ctx), 0);
  53909. ExpectIntEQ(EC_POINT_add(NULL, NULL, new_point, NULL, ctx), 0);
  53910. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, Gxy, ctx), 0);
  53911. ExpectIntEQ(EC_POINT_add(NULL, new_point, new_point, Gxy, ctx), 0);
  53912. ExpectIntEQ(EC_POINT_add(group, NULL, new_point, Gxy, ctx), 0);
  53913. ExpectIntEQ(EC_POINT_add(group, new_point, NULL, Gxy, ctx), 0);
  53914. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, NULL, ctx), 0);
  53915. ExpectIntEQ(EC_POINT_mul(NULL, NULL, Gx, Gxy, k, ctx), 0);
  53916. ExpectIntEQ(EC_POINT_mul(NULL, new_point, Gx, Gxy, k, ctx), 0);
  53917. ExpectIntEQ(EC_POINT_mul(group, NULL, Gx, Gxy, k, ctx), 0);
  53918. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), 1);
  53919. /* perform point multiplication */
  53920. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), 1);
  53921. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  53922. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  53923. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  53924. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), 1);
  53925. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  53926. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  53927. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  53928. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), 1);
  53929. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  53930. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  53931. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  53932. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, NULL, NULL, ctx), 1);
  53933. ExpectIntEQ(BN_is_zero(new_point->X), 1);
  53934. ExpectIntEQ(BN_is_zero(new_point->Y), 1);
  53935. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  53936. /* Set point to something. */
  53937. ExpectIntEQ(EC_POINT_add(group, new_point, Gxy, Gxy, ctx), 1);
  53938. #else
  53939. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy,
  53940. ctx), 1);
  53941. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  53942. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  53943. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  53944. #endif
  53945. /* check if point X coordinate is zero */
  53946. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  53947. #if defined(USE_ECC_B_PARAM) && !defined(HAVE_SELFTEST) && \
  53948. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  53949. ExpectIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  53950. #endif
  53951. /* extract the coordinates from point */
  53952. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  53953. ctx), WOLFSSL_SUCCESS);
  53954. /* check if point X coordinate is zero */
  53955. ExpectIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  53956. /* set the same X and Y points in another object */
  53957. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y,
  53958. ctx), WOLFSSL_SUCCESS);
  53959. /* compare points as they should be the same */
  53960. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, NULL, ctx), -1);
  53961. ExpectIntEQ(EC_POINT_cmp(group, NULL, NULL, ctx), -1);
  53962. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, NULL, ctx), -1);
  53963. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, set_point, ctx), -1);
  53964. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, set_point, ctx), -1);
  53965. ExpectIntEQ(EC_POINT_cmp(group, NULL, set_point, ctx), -1);
  53966. ExpectIntEQ(EC_POINT_cmp(group, new_point, NULL, ctx), -1);
  53967. ExpectIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  53968. /* Test copying */
  53969. ExpectIntEQ(EC_POINT_copy(NULL, NULL), 0);
  53970. ExpectIntEQ(EC_POINT_copy(NULL, set_point), 0);
  53971. ExpectIntEQ(EC_POINT_copy(new_point, NULL), 0);
  53972. ExpectIntEQ(EC_POINT_copy(new_point, set_point), 1);
  53973. /* Test inverting */
  53974. ExpectIntEQ(EC_POINT_invert(NULL, NULL, ctx), 0);
  53975. ExpectIntEQ(EC_POINT_invert(NULL, new_point, ctx), 0);
  53976. ExpectIntEQ(EC_POINT_invert(group, NULL, ctx), 0);
  53977. ExpectIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  53978. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  53979. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  53980. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  53981. {
  53982. EC_POINT* orig_point = NULL;
  53983. ExpectNotNull(orig_point = EC_POINT_new(group));
  53984. ExpectIntEQ(EC_POINT_add(group, orig_point, set_point, set_point, NULL),
  53985. 1);
  53986. /* new_point should be set_point inverted so adding it will revert
  53987. * the point back to set_point */
  53988. ExpectIntEQ(EC_POINT_add(group, orig_point, orig_point, new_point,
  53989. NULL), 1);
  53990. ExpectIntEQ(EC_POINT_cmp(group, orig_point, set_point, NULL), 0);
  53991. EC_POINT_free(orig_point);
  53992. }
  53993. #endif
  53994. /* Test getting affine converts from projective. */
  53995. ExpectIntEQ(EC_POINT_copy(set_point, new_point), 1);
  53996. /* Force non-affine coordinates */
  53997. ExpectIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  53998. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  53999. if (new_point != NULL) {
  54000. new_point->inSet = 0;
  54001. }
  54002. /* extract the coordinates from point */
  54003. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  54004. ctx), WOLFSSL_SUCCESS);
  54005. /* check if point ordinates have changed. */
  54006. ExpectIntNE(BN_cmp(X, set_point->X), 0);
  54007. ExpectIntNE(BN_cmp(Y, set_point->Y), 0);
  54008. /* Test check for infinity */
  54009. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  54010. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, NULL), 0);
  54011. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, infinity), 0);
  54012. ExpectIntEQ(EC_POINT_is_at_infinity(group, NULL), 0);
  54013. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 1);
  54014. ExpectIntEQ(EC_POINT_is_at_infinity(group, Gxy), 0);
  54015. #else
  54016. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 0);
  54017. #endif
  54018. ExpectPtrEq(EC_POINT_point2bn(group, set_point,
  54019. POINT_CONVERSION_UNCOMPRESSED, set_point_bn, ctx), set_point_bn);
  54020. /* check bn2hex */
  54021. hexStr = BN_bn2hex(k);
  54022. ExpectStrEQ(hexStr, kTest);
  54023. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  54024. defined(XFPRINTF)
  54025. BN_print_fp(stderr, k);
  54026. fprintf(stderr, "\n");
  54027. #endif
  54028. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  54029. hexStr = BN_bn2hex(Gx);
  54030. ExpectStrEQ(hexStr, kGx);
  54031. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  54032. defined(XFPRINTF)
  54033. BN_print_fp(stderr, Gx);
  54034. fprintf(stderr, "\n");
  54035. #endif
  54036. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  54037. hexStr = BN_bn2hex(Gy);
  54038. ExpectStrEQ(hexStr, kGy);
  54039. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  54040. defined(XFPRINTF)
  54041. BN_print_fp(stderr, Gy);
  54042. fprintf(stderr, "\n");
  54043. #endif
  54044. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  54045. #ifndef HAVE_SELFTEST
  54046. /* Test point to hex */
  54047. ExpectNull(EC_POINT_point2hex(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  54048. ctx));
  54049. ExpectNull(EC_POINT_point2hex(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  54050. ctx));
  54051. ExpectNull(EC_POINT_point2hex(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  54052. ctx));
  54053. #ifndef HAVE_ECC_BRAINPOOL
  54054. /* Group not supported in wolfCrypt. */
  54055. ExpectNull(EC_POINT_point2hex(group2, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  54056. ctx));
  54057. #endif
  54058. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  54059. ExpectNotNull(hexStr);
  54060. ExpectStrEQ(hexStr, uncompG);
  54061. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  54062. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  54063. ExpectNotNull(hexStr);
  54064. ExpectStrEQ(hexStr, compG);
  54065. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  54066. /* Test point to oct */
  54067. ExpectIntEQ(EC_POINT_point2oct(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  54068. NULL, 0, ctx), 0);
  54069. ExpectIntEQ(EC_POINT_point2oct(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  54070. NULL, 0, ctx), 0);
  54071. ExpectIntEQ(EC_POINT_point2oct(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  54072. NULL, 0, ctx), 0);
  54073. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  54074. NULL, 0, ctx);
  54075. ExpectIntEQ(bin_len, sizeof(binUncompG));
  54076. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  54077. DYNAMIC_TYPE_ECC));
  54078. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  54079. buf, bin_len, ctx), bin_len);
  54080. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  54081. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  54082. /* Infinity (x=0, y=0) encodes as '0x00'. */
  54083. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  54084. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), 1);
  54085. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  54086. POINT_CONVERSION_UNCOMPRESSED, bufInf, 0, ctx), 0);
  54087. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  54088. POINT_CONVERSION_UNCOMPRESSED, bufInf, 1, ctx), 1);
  54089. ExpectIntEQ(bufInf[0], 0);
  54090. wolfSSL_EC_POINT_dump(NULL, NULL);
  54091. /* Test point i2d */
  54092. ExpectIntEQ(ECPoint_i2d(NULL, NULL, NULL, &blen), 0);
  54093. ExpectIntEQ(ECPoint_i2d(NULL, Gxy, NULL, &blen), 0);
  54094. ExpectIntEQ(ECPoint_i2d(group, NULL, NULL, &blen), 0);
  54095. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, NULL), 0);
  54096. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, &blen), 1);
  54097. ExpectIntEQ(blen, sizeof(binUncompG));
  54098. ExpectNotNull(buf = (unsigned char*)XMALLOC(blen, NULL, DYNAMIC_TYPE_ECC));
  54099. blen -= 1;
  54100. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 0);
  54101. blen += 1;
  54102. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 1);
  54103. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  54104. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  54105. #ifdef HAVE_COMP_KEY
  54106. /* Test point to oct compressed */
  54107. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL,
  54108. 0, ctx);
  54109. ExpectIntEQ(bin_len, sizeof(binCompG));
  54110. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  54111. DYNAMIC_TYPE_ECC));
  54112. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  54113. bin_len, ctx), bin_len);
  54114. ExpectIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  54115. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  54116. #endif
  54117. /* Test point BN */
  54118. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, NULL,
  54119. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  54120. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, Gxy,
  54121. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  54122. ExpectNull(wolfSSL_EC_POINT_point2bn(group, NULL,
  54123. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  54124. ExpectNull(wolfSSL_EC_POINT_point2bn(group, Gxy, 0, NULL, ctx));
  54125. /* Test oct to point */
  54126. ExpectNotNull(tmp = EC_POINT_new(group));
  54127. ExpectIntEQ(EC_POINT_oct2point(NULL, NULL, binUncompG, sizeof(binUncompG),
  54128. ctx), 0);
  54129. ExpectIntEQ(EC_POINT_oct2point(NULL, tmp, binUncompG, sizeof(binUncompG),
  54130. ctx), 0);
  54131. ExpectIntEQ(EC_POINT_oct2point(group, NULL, binUncompG, sizeof(binUncompG),
  54132. ctx), 0);
  54133. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompGBad,
  54134. sizeof(binUncompGBad), ctx), 0);
  54135. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG),
  54136. ctx), 1);
  54137. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  54138. EC_POINT_free(tmp);
  54139. tmp = NULL;
  54140. /* Test setting BN ordinates. */
  54141. ExpectNotNull(tmp = EC_POINT_new(group));
  54142. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  54143. NULL, ctx), 0);
  54144. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, NULL,
  54145. NULL, ctx), 0);
  54146. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, NULL,
  54147. NULL, ctx), 0);
  54148. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, Gx,
  54149. NULL, ctx), 0);
  54150. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  54151. Gy, ctx), 0);
  54152. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, Gx, Gy,
  54153. ctx), 0);
  54154. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, Gx, Gy,
  54155. ctx), 0);
  54156. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, NULL,
  54157. Gy, ctx), 0);
  54158. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx,
  54159. NULL, ctx), 0);
  54160. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx, Gy,
  54161. ctx), 1);
  54162. EC_POINT_free(tmp);
  54163. tmp = NULL;
  54164. /* Test point d2i */
  54165. ExpectNotNull(tmp = EC_POINT_new(group));
  54166. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, NULL), 0);
  54167. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, NULL), 0);
  54168. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, NULL), 0);
  54169. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, tmp), 0);
  54170. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, tmp), 0);
  54171. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, tmp), 0);
  54172. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, NULL), 0);
  54173. ExpectIntEQ(ECPoint_d2i(binUncompGBad, sizeof(binUncompG), group, tmp), 0);
  54174. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, tmp), 1);
  54175. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  54176. EC_POINT_free(tmp);
  54177. tmp = NULL;
  54178. #ifdef HAVE_COMP_KEY
  54179. /* Test oct compressed to point */
  54180. ExpectNotNull(tmp = EC_POINT_new(group));
  54181. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx),
  54182. 1);
  54183. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  54184. EC_POINT_free(tmp);
  54185. tmp = NULL;
  54186. /* Test point d2i - compressed */
  54187. ExpectNotNull(tmp = EC_POINT_new(group));
  54188. ExpectIntEQ(ECPoint_d2i(binCompG, sizeof(binCompG), group, tmp), 1);
  54189. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  54190. EC_POINT_free(tmp);
  54191. tmp = NULL;
  54192. #endif
  54193. #endif
  54194. /* test BN_mod_add */
  54195. ExpectIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  54196. (WOLFSSL_BIGNUM*)BN_value_one(), (WOLFSSL_BIGNUM*)BN_value_one(), NULL),
  54197. 1);
  54198. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  54199. /* cleanup */
  54200. BN_free(X);
  54201. BN_free(Y);
  54202. BN_free(k);
  54203. BN_free(set_point_bn);
  54204. EC_POINT_free(infinity);
  54205. EC_POINT_free(new_point);
  54206. EC_POINT_free(set_point);
  54207. EC_POINT_clear_free(Gxy);
  54208. #ifndef HAVE_ECC_BRAINPOOL
  54209. EC_GROUP_free(group2);
  54210. #endif
  54211. EC_GROUP_free(group);
  54212. BN_CTX_free(ctx);
  54213. #endif
  54214. #endif /* !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  54215. return EXPECT_RESULT();
  54216. }
  54217. static int test_wolfSSL_SPAKE(void)
  54218. {
  54219. EXPECT_DECLS;
  54220. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(WOLFSSL_ATECC508A) \
  54221. && !defined(WOLFSSL_ATECC608A) && !defined(HAVE_SELFTEST) && \
  54222. !defined(WOLFSSL_SP_MATH) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  54223. BIGNUM* x = NULL; /* kdc priv */
  54224. BIGNUM* y = NULL; /* client priv */
  54225. BIGNUM* w = NULL; /* shared value */
  54226. byte M_bytes[] = {
  54227. /* uncompressed */
  54228. 0x04,
  54229. /* x */
  54230. 0x88, 0x6e, 0x2f, 0x97, 0xac, 0xe4, 0x6e, 0x55, 0xba, 0x9d, 0xd7, 0x24,
  54231. 0x25, 0x79, 0xf2, 0x99, 0x3b, 0x64, 0xe1, 0x6e, 0xf3, 0xdc, 0xab, 0x95,
  54232. 0xaf, 0xd4, 0x97, 0x33, 0x3d, 0x8f, 0xa1, 0x2f,
  54233. /* y */
  54234. 0x5f, 0xf3, 0x55, 0x16, 0x3e, 0x43, 0xce, 0x22, 0x4e, 0x0b, 0x0e, 0x65,
  54235. 0xff, 0x02, 0xac, 0x8e, 0x5c, 0x7b, 0xe0, 0x94, 0x19, 0xc7, 0x85, 0xe0,
  54236. 0xca, 0x54, 0x7d, 0x55, 0xa1, 0x2e, 0x2d, 0x20
  54237. };
  54238. EC_POINT* M = NULL; /* shared value */
  54239. byte N_bytes[] = {
  54240. /* uncompressed */
  54241. 0x04,
  54242. /* x */
  54243. 0xd8, 0xbb, 0xd6, 0xc6, 0x39, 0xc6, 0x29, 0x37, 0xb0, 0x4d, 0x99, 0x7f,
  54244. 0x38, 0xc3, 0x77, 0x07, 0x19, 0xc6, 0x29, 0xd7, 0x01, 0x4d, 0x49, 0xa2,
  54245. 0x4b, 0x4f, 0x98, 0xba, 0xa1, 0x29, 0x2b, 0x49,
  54246. /* y */
  54247. 0x07, 0xd6, 0x0a, 0xa6, 0xbf, 0xad, 0xe4, 0x50, 0x08, 0xa6, 0x36, 0x33,
  54248. 0x7f, 0x51, 0x68, 0xc6, 0x4d, 0x9b, 0xd3, 0x60, 0x34, 0x80, 0x8c, 0xd5,
  54249. 0x64, 0x49, 0x0b, 0x1e, 0x65, 0x6e, 0xdb, 0xe7
  54250. };
  54251. EC_POINT* N = NULL; /* shared value */
  54252. EC_POINT* T = NULL; /* kdc pub */
  54253. EC_POINT* tmp1 = NULL; /* kdc pub */
  54254. EC_POINT* tmp2 = NULL; /* kdc pub */
  54255. EC_POINT* S = NULL; /* client pub */
  54256. EC_POINT* client_secret = NULL;
  54257. EC_POINT* kdc_secret = NULL;
  54258. EC_GROUP* group = NULL;
  54259. BN_CTX* bn_ctx = NULL;
  54260. /* Values taken from a test run of Kerberos 5 */
  54261. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  54262. ExpectNotNull(bn_ctx = BN_CTX_new());
  54263. ExpectNotNull(M = EC_POINT_new(group));
  54264. ExpectNotNull(N = EC_POINT_new(group));
  54265. ExpectNotNull(T = EC_POINT_new(group));
  54266. ExpectNotNull(tmp1 = EC_POINT_new(group));
  54267. ExpectNotNull(tmp2 = EC_POINT_new(group));
  54268. ExpectNotNull(S = EC_POINT_new(group));
  54269. ExpectNotNull(client_secret = EC_POINT_new(group));
  54270. ExpectNotNull(kdc_secret = EC_POINT_new(group));
  54271. ExpectIntEQ(BN_hex2bn(&x, "DAC3027CD692B4BDF0EDFE9B7D0E4E7"
  54272. "E5D8768A725EAEEA6FC68EC239A17C0"), 1);
  54273. ExpectIntEQ(BN_hex2bn(&y, "6F6A1D394E26B1655A54B26DCE30D49"
  54274. "90CC47EBE08F809EF3FF7F6AEAABBB5"), 1);
  54275. ExpectIntEQ(BN_hex2bn(&w, "1D992AB8BA851B9BA05353453D81EE9"
  54276. "506AB395478F0AAB647752CF117B36250"), 1);
  54277. ExpectIntEQ(EC_POINT_oct2point(group, M, M_bytes, sizeof(M_bytes), bn_ctx),
  54278. 1);
  54279. ExpectIntEQ(EC_POINT_oct2point(group, N, N_bytes, sizeof(N_bytes), bn_ctx),
  54280. 1);
  54281. /* Function pattern similar to ossl_keygen and ossl_result in krb5 */
  54282. /* kdc */
  54283. /* T=x*P+w*M */
  54284. /* All in one function call */
  54285. ExpectIntEQ(EC_POINT_mul(group, T, x, M, w, bn_ctx), 1);
  54286. /* Spread into separate calls */
  54287. ExpectIntEQ(EC_POINT_mul(group, tmp1, x, NULL, NULL, bn_ctx), 1);
  54288. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, M, w, bn_ctx), 1);
  54289. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  54290. 1);
  54291. ExpectIntEQ(EC_POINT_cmp(group, T, tmp1, bn_ctx), 0);
  54292. /* client */
  54293. /* S=y*P+w*N */
  54294. /* All in one function call */
  54295. ExpectIntEQ(EC_POINT_mul(group, S, y, N, w, bn_ctx), 1);
  54296. /* Spread into separate calls */
  54297. ExpectIntEQ(EC_POINT_mul(group, tmp1, y, NULL, NULL, bn_ctx), 1);
  54298. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, N, w, bn_ctx), 1);
  54299. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  54300. 1);
  54301. ExpectIntEQ(EC_POINT_cmp(group, S, tmp1, bn_ctx), 0);
  54302. /* K=y*(T-w*M) */
  54303. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, M, w, bn_ctx), 1);
  54304. ExpectIntEQ(EC_POINT_invert(group, client_secret, bn_ctx), 1);
  54305. ExpectIntEQ(EC_POINT_add(group, client_secret, T, client_secret, bn_ctx),
  54306. 1);
  54307. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, client_secret, y,
  54308. bn_ctx), 1);
  54309. /* kdc */
  54310. /* K=x*(S-w*N) */
  54311. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, N, w, bn_ctx), 1);
  54312. ExpectIntEQ(EC_POINT_invert(group, kdc_secret, bn_ctx), 1);
  54313. ExpectIntEQ(EC_POINT_add(group, kdc_secret, S, kdc_secret, bn_ctx),
  54314. 1);
  54315. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, kdc_secret, x, bn_ctx),
  54316. 1);
  54317. /* kdc_secret == client_secret */
  54318. ExpectIntEQ(EC_POINT_cmp(group, client_secret, kdc_secret, bn_ctx), 0);
  54319. BN_free(x);
  54320. BN_free(y);
  54321. BN_free(w);
  54322. EC_POINT_free(M);
  54323. EC_POINT_free(N);
  54324. EC_POINT_free(T);
  54325. EC_POINT_free(tmp1);
  54326. EC_POINT_free(tmp2);
  54327. EC_POINT_free(S);
  54328. EC_POINT_free(client_secret);
  54329. EC_POINT_free(kdc_secret);
  54330. EC_GROUP_free(group);
  54331. BN_CTX_free(bn_ctx);
  54332. #endif
  54333. return EXPECT_RESULT();
  54334. }
  54335. static int test_wolfSSL_EC_KEY_generate(void)
  54336. {
  54337. EXPECT_DECLS;
  54338. #ifdef OPENSSL_EXTRA
  54339. WOLFSSL_EC_KEY* key = NULL;
  54340. #ifndef HAVE_ECC_BRAINPOOL
  54341. WOLFSSL_EC_GROUP* group = NULL;
  54342. #endif
  54343. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54344. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(NULL), 0);
  54345. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  54346. wolfSSL_EC_KEY_free(key);
  54347. key = NULL;
  54348. #ifndef HAVE_ECC_BRAINPOOL
  54349. ExpectNotNull(group = wolfSSL_EC_GROUP_new_by_curve_name(
  54350. NID_brainpoolP256r1));
  54351. ExpectNotNull(key = wolfSSL_EC_KEY_new());
  54352. ExpectIntEQ(wolfSSL_EC_KEY_set_group(key, group), 1);
  54353. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 0);
  54354. wolfSSL_EC_KEY_free(key);
  54355. wolfSSL_EC_GROUP_free(group);
  54356. #endif
  54357. #endif
  54358. return EXPECT_RESULT();
  54359. }
  54360. static int test_EC_i2d(void)
  54361. {
  54362. EXPECT_DECLS;
  54363. #if defined(OPENSSL_EXTRA) && !defined(HAVE_FIPS)
  54364. EC_KEY *key = NULL;
  54365. EC_KEY *copy = NULL;
  54366. int len = 0;
  54367. unsigned char *buf = NULL;
  54368. unsigned char *p = NULL;
  54369. const unsigned char *tmp = NULL;
  54370. const unsigned char octBad[] = {
  54371. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  54372. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  54373. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  54374. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  54375. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  54376. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  54377. };
  54378. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54379. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  54380. ExpectIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  54381. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  54382. DYNAMIC_TYPE_TMP_BUFFER));
  54383. p = buf;
  54384. ExpectIntEQ(i2d_EC_PUBKEY(key, &p), len);
  54385. ExpectNull(o2i_ECPublicKey(NULL, NULL, -1));
  54386. ExpectNull(o2i_ECPublicKey(&copy, NULL, -1));
  54387. ExpectNull(o2i_ECPublicKey(&key, NULL, -1));
  54388. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  54389. ExpectNull(o2i_ECPublicKey(NULL, NULL, 0));
  54390. ExpectNull(o2i_ECPublicKey(&key, NULL, 0));
  54391. ExpectNull(o2i_ECPublicKey(&key, &tmp, 0));
  54392. tmp = buf;
  54393. ExpectNull(o2i_ECPublicKey(NULL, &tmp, 0));
  54394. ExpectNull(o2i_ECPublicKey(&copy, &tmp, 0));
  54395. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  54396. ExpectNull(o2i_ECPublicKey(&key, &tmp, -1));
  54397. ExpectIntEQ(i2o_ECPublicKey(NULL, NULL), 0);
  54398. ExpectIntEQ(i2o_ECPublicKey(NULL, &buf), 0);
  54399. tmp = buf;
  54400. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 0));
  54401. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 1));
  54402. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 0));
  54403. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 1));
  54404. ExpectNull(d2i_ECPrivateKey(&key, &tmp, 0));
  54405. ExpectIntEQ(i2d_ECPrivateKey(NULL, &p), 0);
  54406. ExpectIntEQ(i2d_ECPrivateKey(NULL, NULL), 0);
  54407. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer(NULL, NULL, -1), -1);
  54408. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1, 0), -1);
  54409. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, -1, 0), -1);
  54410. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, -1, 0), -1);
  54411. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, 0, 0), -1);
  54412. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1,
  54413. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  54414. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, len,
  54415. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  54416. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, len,
  54417. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  54418. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, -1,
  54419. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  54420. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len, 0), -1);
  54421. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len,
  54422. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  54423. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  54424. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  54425. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  54426. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  54427. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54428. buf = NULL;
  54429. buf = NULL;
  54430. ExpectIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  54431. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  54432. DYNAMIC_TYPE_TMP_BUFFER));
  54433. p = buf;
  54434. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  54435. p = NULL;
  54436. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  54437. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54438. p = NULL;
  54439. /* Bad point is also an invalid private key. */
  54440. tmp = octBad;
  54441. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, sizeof(octBad)));
  54442. tmp = buf;
  54443. ExpectNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  54444. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54445. buf = NULL;
  54446. buf = NULL;
  54447. ExpectIntGT((len = i2o_ECPublicKey(key, NULL)), 0);
  54448. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  54449. DYNAMIC_TYPE_TMP_BUFFER));
  54450. p = buf;
  54451. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  54452. p = NULL;
  54453. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  54454. tmp = buf;
  54455. ExpectNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  54456. tmp = octBad;
  54457. ExpectNull(o2i_ECPublicKey(&key, &tmp, sizeof(octBad)));
  54458. ExpectIntEQ(EC_KEY_check_key(NULL), 0);
  54459. ExpectIntEQ(EC_KEY_check_key(key), 1);
  54460. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  54461. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  54462. EC_KEY_free(key);
  54463. EC_KEY_free(copy);
  54464. #endif
  54465. return EXPECT_RESULT();
  54466. }
  54467. static int test_wolfSSL_EC_curve(void)
  54468. {
  54469. EXPECT_DECLS;
  54470. #if defined(OPENSSL_EXTRA)
  54471. int nid = NID_secp160k1;
  54472. const char* nid_name = NULL;
  54473. ExpectNull(EC_curve_nid2nist(NID_sha256));
  54474. ExpectNotNull(nid_name = EC_curve_nid2nist(nid));
  54475. ExpectIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  54476. ExpectIntEQ(EC_curve_nist2nid("INVALID"), 0);
  54477. ExpectIntEQ(EC_curve_nist2nid(nid_name), nid);
  54478. #endif
  54479. return EXPECT_RESULT();
  54480. }
  54481. static int test_wolfSSL_EC_KEY_dup(void)
  54482. {
  54483. EXPECT_DECLS;
  54484. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  54485. WOLFSSL_EC_KEY* ecKey = NULL;
  54486. WOLFSSL_EC_KEY* dupKey = NULL;
  54487. ecc_key* srcKey = NULL;
  54488. ecc_key* destKey = NULL;
  54489. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  54490. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  54491. /* Valid cases */
  54492. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54493. ExpectIntEQ(EC_KEY_check_key(dupKey), 1);
  54494. /* Compare pubkey */
  54495. if (ecKey != NULL) {
  54496. srcKey = (ecc_key*)ecKey->internal;
  54497. }
  54498. if (dupKey != NULL) {
  54499. destKey = (ecc_key*)dupKey->internal;
  54500. }
  54501. ExpectIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  54502. /* compare EC_GROUP */
  54503. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  54504. /* compare EC_POINT */
  54505. ExpectIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  54506. dupKey->pub_key, NULL), MP_EQ);
  54507. /* compare BIGNUM */
  54508. ExpectIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  54509. wolfSSL_EC_KEY_free(dupKey);
  54510. dupKey = NULL;
  54511. /* Invalid cases */
  54512. /* NULL key */
  54513. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  54514. /* NULL ecc_key */
  54515. if (ecKey != NULL) {
  54516. wc_ecc_free((ecc_key*)ecKey->internal);
  54517. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  54518. ecKey->internal = NULL; /* Set ecc_key to NULL */
  54519. }
  54520. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54521. wolfSSL_EC_KEY_free(ecKey);
  54522. ecKey = NULL;
  54523. wolfSSL_EC_KEY_free(dupKey);
  54524. dupKey = NULL;
  54525. /* NULL Group */
  54526. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  54527. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  54528. if (ecKey != NULL) {
  54529. wolfSSL_EC_GROUP_free(ecKey->group);
  54530. ecKey->group = NULL; /* Set group to NULL */
  54531. }
  54532. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54533. wolfSSL_EC_KEY_free(ecKey);
  54534. ecKey = NULL;
  54535. wolfSSL_EC_KEY_free(dupKey);
  54536. dupKey = NULL;
  54537. /* NULL public key */
  54538. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  54539. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  54540. if (ecKey != NULL) {
  54541. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  54542. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  54543. }
  54544. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54545. if (ecKey != NULL) {
  54546. wolfSSL_EC_POINT_free(ecKey->pub_key);
  54547. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  54548. }
  54549. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54550. wolfSSL_EC_KEY_free(ecKey);
  54551. ecKey = NULL;
  54552. wolfSSL_EC_KEY_free(dupKey);
  54553. dupKey = NULL;
  54554. /* NULL private key */
  54555. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  54556. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  54557. if (ecKey != NULL) {
  54558. wolfSSL_BN_free(ecKey->priv_key);
  54559. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  54560. }
  54561. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54562. wolfSSL_EC_KEY_free(ecKey);
  54563. ecKey = NULL;
  54564. wolfSSL_EC_KEY_free(dupKey);
  54565. dupKey = NULL;
  54566. /* Test EC_KEY_up_ref */
  54567. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  54568. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  54569. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  54570. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  54571. /* reference count doesn't follow duplicate */
  54572. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54573. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  54574. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  54575. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  54576. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  54577. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  54578. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  54579. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  54580. #endif
  54581. return EXPECT_RESULT();
  54582. }
  54583. static int test_wolfSSL_EC_KEY_set_group(void)
  54584. {
  54585. EXPECT_DECLS;
  54586. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  54587. defined(OPENSSL_EXTRA)
  54588. EC_KEY *key = NULL;
  54589. EC_GROUP *group = NULL;
  54590. const EC_GROUP *group2 = NULL;
  54591. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  54592. ExpectNotNull(key = EC_KEY_new());
  54593. ExpectNull(EC_KEY_get0_group(NULL));
  54594. ExpectIntEQ(EC_KEY_set_group(NULL, NULL), 0);
  54595. ExpectIntEQ(EC_KEY_set_group(key, NULL), 0);
  54596. ExpectIntEQ(EC_KEY_set_group(NULL, group), 0);
  54597. ExpectIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  54598. ExpectNotNull(group2 = EC_KEY_get0_group(key));
  54599. ExpectIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  54600. EC_GROUP_free(group);
  54601. EC_KEY_free(key);
  54602. #endif
  54603. return EXPECT_RESULT();
  54604. }
  54605. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  54606. {
  54607. EXPECT_DECLS;
  54608. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  54609. BIO* bio = NULL;
  54610. EC_KEY* key = NULL;
  54611. /* Error condition: NULL key. */
  54612. ExpectIntLT(EC_KEY_get_conv_form(NULL), 0);
  54613. ExpectNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  54614. ExpectNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  54615. /* Conversion form defaults to uncompressed. */
  54616. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  54617. #ifdef HAVE_COMP_KEY
  54618. /* Explicitly set to compressed. */
  54619. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  54620. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  54621. #else
  54622. /* Will still work just won't change anything. */
  54623. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  54624. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  54625. EC_KEY_set_conv_form(key, POINT_CONVERSION_UNCOMPRESSED);
  54626. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  54627. #endif
  54628. EC_KEY_set_conv_form(NULL, POINT_CONVERSION_UNCOMPRESSED);
  54629. BIO_free(bio);
  54630. EC_KEY_free(key);
  54631. #endif
  54632. return EXPECT_RESULT();
  54633. }
  54634. static int test_wolfSSL_EC_KEY_private_key(void)
  54635. {
  54636. EXPECT_DECLS;
  54637. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  54638. WOLFSSL_EC_KEY* key = NULL;
  54639. WOLFSSL_BIGNUM* priv = NULL;
  54640. WOLFSSL_BIGNUM* priv2 = NULL;
  54641. WOLFSSL_BIGNUM* bn;
  54642. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54643. ExpectNotNull(priv = wolfSSL_BN_new());
  54644. ExpectNotNull(priv2 = wolfSSL_BN_new());
  54645. ExpectIntNE(BN_set_word(priv, 2), 0);
  54646. ExpectIntNE(BN_set_word(priv2, 2), 0);
  54647. ExpectNull(wolfSSL_EC_KEY_get0_private_key(NULL));
  54648. /* No private key set. */
  54649. ExpectNull(wolfSSL_EC_KEY_get0_private_key(key));
  54650. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, NULL), 0);
  54651. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, NULL), 0);
  54652. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, priv), 0);
  54653. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv), 1);
  54654. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  54655. ExpectPtrNE(bn, priv);
  54656. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv2), 1);
  54657. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  54658. ExpectPtrNE(bn, priv2);
  54659. wolfSSL_BN_free(priv2);
  54660. wolfSSL_BN_free(priv);
  54661. wolfSSL_EC_KEY_free(key);
  54662. #endif
  54663. return EXPECT_RESULT();
  54664. }
  54665. static int test_wolfSSL_EC_KEY_public_key(void)
  54666. {
  54667. EXPECT_DECLS;
  54668. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  54669. WOLFSSL_EC_KEY* key = NULL;
  54670. WOLFSSL_EC_POINT* pub = NULL;
  54671. WOLFSSL_EC_POINT* point = NULL;
  54672. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54673. ExpectNull(wolfSSL_EC_KEY_get0_public_key(NULL));
  54674. ExpectNotNull(wolfSSL_EC_KEY_get0_public_key(key));
  54675. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  54676. ExpectNotNull(pub = wolfSSL_EC_KEY_get0_public_key(key));
  54677. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, NULL), 0);
  54678. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, NULL), 0);
  54679. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, pub), 0);
  54680. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, pub), 1);
  54681. ExpectNotNull(point = wolfSSL_EC_KEY_get0_public_key(key));
  54682. ExpectPtrEq(point, pub);
  54683. wolfSSL_EC_KEY_free(key);
  54684. #endif
  54685. return EXPECT_RESULT();
  54686. }
  54687. static int test_wolfSSL_EC_KEY_print_fp(void)
  54688. {
  54689. EXPECT_DECLS;
  54690. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  54691. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224 && \
  54692. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  54693. !defined(NO_STDIO_FILESYSTEM)
  54694. EC_KEY* key = NULL;
  54695. /* Bad file pointer. */
  54696. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  54697. /* NULL key. */
  54698. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WOLFSSL_FAILURE);
  54699. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  54700. /* Negative indent. */
  54701. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WOLFSSL_FAILURE);
  54702. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  54703. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  54704. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  54705. wolfSSL_EC_KEY_free(key);
  54706. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  54707. NID_X9_62_prime256v1)));
  54708. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  54709. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  54710. wolfSSL_EC_KEY_free(key);
  54711. #endif
  54712. return EXPECT_RESULT();
  54713. }
  54714. static int test_wolfSSL_EC_get_builtin_curves(void)
  54715. {
  54716. EXPECT_DECLS;
  54717. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  54718. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  54719. EC_builtin_curve* curves = NULL;
  54720. size_t crv_len = 0;
  54721. size_t i = 0;
  54722. ExpectIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  54723. ExpectNotNull(curves = (EC_builtin_curve*)XMALLOC(
  54724. sizeof(EC_builtin_curve) * crv_len, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  54725. ExpectIntEQ((EC_get_builtin_curves(curves, 0)), crv_len);
  54726. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  54727. for (i = 0; EXPECT_SUCCESS() && (i < crv_len); i++) {
  54728. if (curves[i].comment != NULL) {
  54729. ExpectStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  54730. }
  54731. }
  54732. if (crv_len > 1) {
  54733. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len - 1), crv_len - 1);
  54734. }
  54735. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54736. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  54737. #endif /* OPENSSL_EXTRA || OPENSSL_ALL */
  54738. return EXPECT_RESULT();
  54739. }
  54740. static int test_wolfSSL_ECDSA_SIG(void)
  54741. {
  54742. EXPECT_DECLS;
  54743. #ifdef OPENSSL_EXTRA
  54744. WOLFSSL_ECDSA_SIG* sig = NULL;
  54745. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  54746. WOLFSSL_BIGNUM* r = NULL;
  54747. WOLFSSL_BIGNUM* s = NULL;
  54748. const WOLFSSL_BIGNUM* r2 = NULL;
  54749. const WOLFSSL_BIGNUM* s2 = NULL;
  54750. const unsigned char* cp = NULL;
  54751. unsigned char* p = NULL;
  54752. unsigned char outSig[8];
  54753. unsigned char sigData[8] =
  54754. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  54755. unsigned char sigDataBad[8] =
  54756. { 0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  54757. wolfSSL_ECDSA_SIG_free(NULL);
  54758. ExpectNotNull(sig = wolfSSL_ECDSA_SIG_new());
  54759. ExpectNotNull(r = wolfSSL_BN_new());
  54760. ExpectNotNull(s = wolfSSL_BN_new());
  54761. ExpectIntEQ(wolfSSL_BN_set_word(r, 1), 1);
  54762. ExpectIntEQ(wolfSSL_BN_set_word(s, 1), 1);
  54763. wolfSSL_ECDSA_SIG_get0(NULL, NULL, NULL);
  54764. wolfSSL_ECDSA_SIG_get0(NULL, &r2, NULL);
  54765. wolfSSL_ECDSA_SIG_get0(NULL, NULL, &s2);
  54766. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  54767. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, NULL), 0);
  54768. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, NULL), 0);
  54769. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, NULL), 0);
  54770. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, s), 0);
  54771. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, s), 0);
  54772. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, s), 0);
  54773. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, NULL), 0);
  54774. r2 = NULL;
  54775. s2 = NULL;
  54776. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  54777. ExpectNull(r2);
  54778. ExpectNull(s2);
  54779. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, s), 1);
  54780. if (EXPECT_FAIL()) {
  54781. wolfSSL_BN_free(r);
  54782. wolfSSL_BN_free(s);
  54783. }
  54784. wolfSSL_ECDSA_SIG_get0(sig, &r2, &s2);
  54785. ExpectPtrEq(r2, r);
  54786. ExpectPtrEq(s2, s);
  54787. r2 = NULL;
  54788. wolfSSL_ECDSA_SIG_get0(sig, &r2, NULL);
  54789. ExpectPtrEq(r2, r);
  54790. s2 = NULL;
  54791. wolfSSL_ECDSA_SIG_get0(sig, NULL, &s2);
  54792. ExpectPtrEq(s2, s);
  54793. /* r and s are freed when sig is freed. */
  54794. wolfSSL_ECDSA_SIG_free(sig);
  54795. sig = NULL;
  54796. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData)));
  54797. cp = sigDataBad;
  54798. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigDataBad)));
  54799. cp = sigData;
  54800. ExpectNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  54801. ExpectIntEQ((cp == sigData + 8), 1);
  54802. cp = sigData;
  54803. ExpectNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  54804. ExpectNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  54805. ExpectIntEQ((sig == sig2), 1);
  54806. cp = outSig;
  54807. p = outSig;
  54808. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  54809. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  54810. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  54811. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  54812. ExpectIntEQ((p == outSig + 8), 1);
  54813. ExpectIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  54814. wolfSSL_ECDSA_SIG_free(sig);
  54815. #endif
  54816. return EXPECT_RESULT();
  54817. }
  54818. static int test_ECDSA_size_sign(void)
  54819. {
  54820. EXPECT_DECLS;
  54821. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  54822. EC_KEY* key = NULL;
  54823. ECDSA_SIG* ecdsaSig = NULL;
  54824. int id;
  54825. byte hash[WC_MAX_DIGEST_SIZE];
  54826. byte hash2[WC_MAX_DIGEST_SIZE];
  54827. byte sig[ECC_MAX_SIG_SIZE];
  54828. unsigned int sigSz = sizeof(sig);
  54829. XMEMSET(hash, 123, sizeof(hash));
  54830. XMEMSET(hash2, 234, sizeof(hash2));
  54831. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  54832. ExpectIntEQ(id, ECC_SECP256R1);
  54833. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54834. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  54835. ExpectIntGE(ECDSA_size(NULL), 0);
  54836. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, NULL), 0);
  54837. ExpectIntEQ(ECDSA_sign(0, NULL, sizeof(hash), sig, &sigSz, key), 0);
  54838. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), NULL, &sigSz, key), 0);
  54839. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, NULL), 0);
  54840. ExpectIntEQ(ECDSA_verify(0, NULL, sizeof(hash), sig, sigSz, key), 0);
  54841. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), NULL, sigSz, key), 0);
  54842. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  54843. ExpectIntGE(ECDSA_size(key), sigSz);
  54844. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, key), 1);
  54845. ExpectIntEQ(ECDSA_verify(0, hash2, sizeof(hash2), sig, sigSz, key), 0);
  54846. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), NULL));
  54847. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), key));
  54848. ExpectNull(ECDSA_do_sign(hash, sizeof(hash), NULL));
  54849. ExpectNotNull(ecdsaSig = ECDSA_do_sign(hash, sizeof(hash), key));
  54850. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, NULL), -1);
  54851. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, NULL), -1);
  54852. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, NULL), -1);
  54853. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, key), -1);
  54854. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, key), -1);
  54855. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, key), -1);
  54856. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, NULL), -1);
  54857. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, key), 1);
  54858. ExpectIntEQ(ECDSA_do_verify(hash2, sizeof(hash2), ecdsaSig, key), 0);
  54859. ECDSA_SIG_free(ecdsaSig);
  54860. EC_KEY_free(key);
  54861. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP */
  54862. return EXPECT_RESULT();
  54863. }
  54864. static int test_ECDH_compute_key(void)
  54865. {
  54866. EXPECT_DECLS;
  54867. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  54868. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  54869. EC_KEY* key1 = NULL;
  54870. EC_KEY* key2 = NULL;
  54871. EC_POINT* pub1 = NULL;
  54872. EC_POINT* pub2 = NULL;
  54873. byte secret1[32];
  54874. byte secret2[32];
  54875. int i;
  54876. ExpectNotNull(key1 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54877. ExpectIntEQ(EC_KEY_generate_key(key1), 1);
  54878. ExpectNotNull(pub1 = wolfSSL_EC_KEY_get0_public_key(key1));
  54879. ExpectNotNull(key2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54880. ExpectIntEQ(EC_KEY_generate_key(key2), 1);
  54881. ExpectNotNull(pub2 = wolfSSL_EC_KEY_get0_public_key(key2));
  54882. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, NULL, NULL), 0);
  54883. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, NULL, NULL),
  54884. 0);
  54885. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, NULL, NULL), 0);
  54886. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, key1, NULL), 0);
  54887. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, key1, NULL), 0);
  54888. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, key1, NULL),
  54889. 0);
  54890. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, NULL, NULL),
  54891. 0);
  54892. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1) - 16, pub2, key1,
  54893. NULL), 0);
  54894. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, key1, NULL),
  54895. sizeof(secret1));
  54896. ExpectIntEQ(ECDH_compute_key(secret2, sizeof(secret2), pub1, key2, NULL),
  54897. sizeof(secret2));
  54898. for (i = 0; i < (int)sizeof(secret1); i++) {
  54899. ExpectIntEQ(secret1[i], secret2[i]);
  54900. }
  54901. EC_KEY_free(key2);
  54902. EC_KEY_free(key1);
  54903. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP &&
  54904. * !WOLF_CRYPTO_CB_ONLY_ECC */
  54905. return EXPECT_RESULT();
  54906. }
  54907. #endif /* HAVE_ECC && !OPENSSL_NO_PK */
  54908. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  54909. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  54910. !defined(NO_ASN_TIME)
  54911. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey,
  54912. int expectedDerSz)
  54913. {
  54914. EXPECT_DECLS;
  54915. X509* x509 = NULL;
  54916. BIGNUM* serial_number = NULL;
  54917. X509_NAME* name = NULL;
  54918. time_t epoch_off = 0;
  54919. ASN1_INTEGER* asn1_serial_number;
  54920. long not_before, not_after;
  54921. int derSz;
  54922. ExpectNotNull(x509 = X509_new());
  54923. ExpectIntNE(X509_set_pubkey(x509, pkey), 0);
  54924. ExpectNotNull(serial_number = BN_new());
  54925. ExpectIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  54926. ExpectNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  54927. ExpectNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  54928. /* version 3 */
  54929. ExpectIntNE(X509_set_version(x509, 2L), 0);
  54930. ExpectNotNull(name = X509_NAME_new());
  54931. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  54932. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  54933. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_pkcs9_contentType,
  54934. MBSTRING_UTF8,(unsigned char*)"Server", -1, -1, 0), 0);
  54935. ExpectIntNE(X509_set_subject_name(x509, name), 0);
  54936. ExpectIntNE(X509_set_issuer_name(x509, name), 0);
  54937. not_before = (long)wc_Time(NULL);
  54938. not_after = not_before + (365 * 24 * 60 * 60);
  54939. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before,
  54940. &epoch_off));
  54941. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after,
  54942. &epoch_off));
  54943. ExpectIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  54944. ExpectNotNull(wolfSSL_X509_get_der(x509, &derSz));
  54945. ExpectIntGE(derSz, expectedDerSz);
  54946. BN_free(serial_number);
  54947. X509_NAME_free(name);
  54948. X509_free(x509);
  54949. return EXPECT_RESULT();
  54950. }
  54951. #endif
  54952. static int test_openssl_generate_key_and_cert(void)
  54953. {
  54954. EXPECT_DECLS;
  54955. #if defined(OPENSSL_EXTRA)
  54956. int expectedDerSz;
  54957. EVP_PKEY* pkey = NULL;
  54958. #ifdef HAVE_ECC
  54959. EC_KEY* ec_key = NULL;
  54960. #endif
  54961. #if !defined(NO_RSA)
  54962. int key_length = 2048;
  54963. BIGNUM* exponent = NULL;
  54964. RSA* rsa = NULL;
  54965. ExpectNotNull(pkey = EVP_PKEY_new());
  54966. ExpectNotNull(exponent = BN_new());
  54967. ExpectNotNull(rsa = RSA_new());
  54968. ExpectIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  54969. #ifndef WOLFSSL_KEY_GEN
  54970. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  54971. #if defined(USE_CERT_BUFFERS_1024)
  54972. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  54973. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  54974. key_length = 1024;
  54975. #elif defined(USE_CERT_BUFFERS_2048)
  54976. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  54977. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  54978. #else
  54979. RSA_free(rsa);
  54980. rsa = NULL;
  54981. #endif
  54982. #else
  54983. ExpectIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  54984. ExpectIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  54985. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  54986. ExpectIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  54987. #endif
  54988. if (rsa) {
  54989. ExpectIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  54990. if (EXPECT_FAIL()) {
  54991. RSA_free(rsa);
  54992. }
  54993. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  54994. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  54995. expectedDerSz = 743;
  54996. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey,
  54997. expectedDerSz), TEST_SUCCESS);
  54998. #endif
  54999. }
  55000. EVP_PKEY_free(pkey);
  55001. pkey = NULL;
  55002. BN_free(exponent);
  55003. #endif /* !NO_RSA */
  55004. #ifdef HAVE_ECC
  55005. ExpectNotNull(pkey = EVP_PKEY_new());
  55006. ExpectNotNull(ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  55007. #ifndef NO_WOLFSSL_STUB
  55008. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  55009. #endif
  55010. ExpectIntNE(EC_KEY_generate_key(ec_key), 0);
  55011. ExpectIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  55012. if (EXPECT_FAIL()) {
  55013. EC_KEY_free(ec_key);
  55014. }
  55015. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  55016. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  55017. expectedDerSz = 344;
  55018. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey, expectedDerSz),
  55019. TEST_SUCCESS);
  55020. #endif
  55021. EVP_PKEY_free(pkey);
  55022. #endif /* HAVE_ECC */
  55023. (void)pkey;
  55024. (void)expectedDerSz;
  55025. #endif /* OPENSSL_EXTRA */
  55026. return EXPECT_RESULT();
  55027. }
  55028. static int test_stubs_are_stubs(void)
  55029. {
  55030. EXPECT_DECLS;
  55031. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB) && \
  55032. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  55033. WOLFSSL_CTX* ctx = NULL;
  55034. WOLFSSL_CTX* ctxN = NULL;
  55035. #ifndef NO_WOLFSSL_CLIENT
  55036. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  55037. #elif !defined(NO_WOLFSSL_SERVER)
  55038. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  55039. #endif
  55040. #define CHECKZERO_RET(x, y, z) ExpectIntEQ((int) x(y), 0); \
  55041. ExpectIntEQ((int) x(z), 0)
  55042. /* test logic, all stubs return same result regardless of ctx being NULL
  55043. * as there are no sanity checks, it's just a stub! If at some
  55044. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  55045. * if invalid inputs are supplied. Test calling both
  55046. * with and without valid inputs, if a stub functionality remains unchanged.
  55047. */
  55048. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  55049. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  55050. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  55051. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  55052. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  55053. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  55054. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  55055. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  55056. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  55057. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  55058. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  55059. wolfSSL_CTX_free(ctx);
  55060. ctx = NULL;
  55061. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB && (!NO_WOLFSSL_CLIENT ||
  55062. * !NO_WOLFSSL_SERVER) */
  55063. return EXPECT_RESULT();
  55064. }
  55065. static int test_CONF_modules_xxx(void)
  55066. {
  55067. int res = TEST_SKIPPED;
  55068. #if defined(OPENSSL_EXTRA)
  55069. CONF_modules_free();
  55070. CONF_modules_unload(0);
  55071. CONF_modules_unload(1);
  55072. CONF_modules_unload(-1);
  55073. res = TEST_SUCCESS;
  55074. #endif /* OPENSSL_EXTRA */
  55075. return res;
  55076. }
  55077. static int test_CRYPTO_set_dynlock_xxx(void)
  55078. {
  55079. int res = TEST_SKIPPED;
  55080. #if defined(OPENSSL_EXTRA)
  55081. CRYPTO_set_dynlock_create_callback(
  55082. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  55083. CRYPTO_set_dynlock_create_callback(
  55084. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  55085. CRYPTO_set_dynlock_destroy_callback(
  55086. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  55087. CRYPTO_set_dynlock_destroy_callback(
  55088. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  55089. CRYPTO_set_dynlock_lock_callback(
  55090. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  55091. CRYPTO_set_dynlock_lock_callback(
  55092. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  55093. res = TEST_SUCCESS;
  55094. #endif /* OPENSSL_EXTRA */
  55095. return res;
  55096. }
  55097. static int test_CRYPTO_THREADID_xxx(void)
  55098. {
  55099. EXPECT_DECLS;
  55100. #if defined(OPENSSL_EXTRA)
  55101. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  55102. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  55103. ExpectIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  55104. #endif /* OPENSSL_EXTRA */
  55105. return EXPECT_RESULT();
  55106. }
  55107. static int test_ENGINE_cleanup(void)
  55108. {
  55109. int res = TEST_SKIPPED;
  55110. #if defined(OPENSSL_EXTRA)
  55111. ENGINE_cleanup();
  55112. res = TEST_SUCCESS;
  55113. #endif /* OPENSSL_EXTRA */
  55114. return res;
  55115. }
  55116. static int test_wolfSSL_CTX_LoadCRL(void)
  55117. {
  55118. EXPECT_DECLS;
  55119. #if defined(HAVE_CRL) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  55120. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  55121. WOLFSSL_CTX* ctx = NULL;
  55122. WOLFSSL* ssl = NULL;
  55123. const char* badPath = "dummypath";
  55124. const char* validPath = "./certs/crl";
  55125. const char* validFilePath = "./certs/crl/cliCrl.pem";
  55126. const char* issuerCert = "./certs/client-cert.pem";
  55127. int derType = WOLFSSL_FILETYPE_ASN1;
  55128. int pemType = WOLFSSL_FILETYPE_PEM;
  55129. #ifdef HAVE_CRL_MONITOR
  55130. int monitor = WOLFSSL_CRL_MONITOR;
  55131. #else
  55132. int monitor = 0;
  55133. #endif
  55134. WOLFSSL_CERT_MANAGER* cm = NULL;
  55135. #define FAIL_T1(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  55136. BAD_FUNC_ARG)
  55137. #define FAIL_T2(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  55138. NOT_COMPILED_IN)
  55139. #define SUCC_T(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  55140. WOLFSSL_SUCCESS)
  55141. #ifndef NO_WOLFSSL_CLIENT
  55142. #define NEW_CTX(ctx) ExpectNotNull( \
  55143. (ctx) = wolfSSL_CTX_new(wolfSSLv23_client_method()))
  55144. #elif !defined(NO_WOLFSSL_SERVER)
  55145. #define NEW_CTX(ctx) ExpectNotNull( \
  55146. (ctx) = wolfSSL_CTX_new(wolfSSLv23_server_method()))
  55147. #else
  55148. #define NEW_CTX(ctx) return
  55149. #endif
  55150. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  55151. NEW_CTX(ctx);
  55152. #ifndef HAVE_CRL_MONITOR
  55153. FAIL_T2(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, WOLFSSL_CRL_MONITOR);
  55154. wolfSSL_CTX_free(ctx);
  55155. NEW_CTX(ctx);
  55156. #endif
  55157. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  55158. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  55159. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  55160. wolfSSL_CTX_free(ctx);
  55161. ctx = NULL;
  55162. NEW_CTX(ctx);
  55163. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  55164. WOLFSSL_SUCCESS);
  55165. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  55166. wolfSSL_CTX_free(ctx);
  55167. ctx = NULL;
  55168. NEW_CTX(ctx);
  55169. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  55170. WOLFSSL_SUCCESS);
  55171. ExpectNotNull(ssl = wolfSSL_new(ctx));
  55172. ExpectIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  55173. wolfSSL_free(ssl);
  55174. ssl = NULL;
  55175. wolfSSL_CTX_free(ctx);
  55176. ctx = NULL;
  55177. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  55178. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  55179. WOLFSSL_SUCCESS);
  55180. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType),
  55181. WOLFSSL_SUCCESS);
  55182. wolfSSL_CertManagerFree(cm);
  55183. #endif
  55184. return EXPECT_RESULT();
  55185. }
  55186. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  55187. static int test_multiple_crls_same_issuer_ctx_ready(WOLFSSL_CTX* ctx)
  55188. {
  55189. EXPECT_DECLS;
  55190. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  55191. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, "./certs/crl/crl.pem",
  55192. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  55193. return EXPECT_RESULT();
  55194. }
  55195. #endif
  55196. static int test_multiple_crls_same_issuer(void)
  55197. {
  55198. EXPECT_DECLS;
  55199. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  55200. test_ssl_cbf client_cbs, server_cbs;
  55201. struct {
  55202. const char* server_cert;
  55203. const char* server_key;
  55204. } test_params[] = {
  55205. { "./certs/server-cert.pem", "./certs/server-key.pem" },
  55206. { "./certs/server-revoked-cert.pem", "./certs/server-revoked-key.pem" }
  55207. };
  55208. size_t i;
  55209. for (i = 0; i < (sizeof(test_params)/sizeof(*test_params)); i++) {
  55210. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  55211. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  55212. server_cbs.certPemFile = test_params[i].server_cert;
  55213. server_cbs.keyPemFile = test_params[i].server_key;
  55214. client_cbs.crlPemFile = "./certs/crl/extra-crls/general-server-crl.pem";
  55215. client_cbs.ctx_ready = test_multiple_crls_same_issuer_ctx_ready;
  55216. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  55217. &server_cbs, NULL), TEST_FAIL);
  55218. }
  55219. #endif
  55220. return EXPECT_RESULT();
  55221. }
  55222. static int test_SetTmpEC_DHE_Sz(void)
  55223. {
  55224. EXPECT_DECLS;
  55225. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  55226. WOLFSSL_CTX *ctx = NULL;
  55227. WOLFSSL *ssl = NULL;
  55228. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  55229. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  55230. ExpectNotNull(ssl = wolfSSL_new(ctx));
  55231. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  55232. wolfSSL_free(ssl);
  55233. wolfSSL_CTX_free(ctx);
  55234. #endif
  55235. return EXPECT_RESULT();
  55236. }
  55237. static int test_wolfSSL_CTX_get0_privatekey(void)
  55238. {
  55239. EXPECT_DECLS;
  55240. #ifdef OPENSSL_ALL
  55241. WOLFSSL_CTX* ctx = NULL;
  55242. (void)ctx;
  55243. #ifndef NO_RSA
  55244. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  55245. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  55246. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  55247. WOLFSSL_FILETYPE_PEM));
  55248. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  55249. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  55250. WOLFSSL_FILETYPE_PEM));
  55251. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  55252. wolfSSL_CTX_free(ctx);
  55253. ctx = NULL;
  55254. #endif
  55255. #ifdef HAVE_ECC
  55256. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  55257. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  55258. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  55259. WOLFSSL_FILETYPE_PEM));
  55260. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  55261. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  55262. WOLFSSL_FILETYPE_PEM));
  55263. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  55264. wolfSSL_CTX_free(ctx);
  55265. #endif
  55266. #endif
  55267. return EXPECT_RESULT();
  55268. }
  55269. static int test_wolfSSL_dtls_set_mtu(void)
  55270. {
  55271. EXPECT_DECLS;
  55272. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  55273. !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_DTLS) && \
  55274. !defined(WOLFSSL_NO_TLS12)
  55275. WOLFSSL_CTX* ctx = NULL;
  55276. WOLFSSL* ssl = NULL;
  55277. const char* testCertFile;
  55278. const char* testKeyFile;
  55279. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  55280. #ifndef NO_RSA
  55281. testCertFile = svrCertFile;
  55282. testKeyFile = svrKeyFile;
  55283. #elif defined(HAVE_ECC)
  55284. testCertFile = eccCertFile;
  55285. testKeyFile = eccKeyFile;
  55286. #endif
  55287. if (testCertFile != NULL && testKeyFile != NULL) {
  55288. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  55289. WOLFSSL_FILETYPE_PEM));
  55290. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  55291. WOLFSSL_FILETYPE_PEM));
  55292. }
  55293. ExpectNotNull(ssl = wolfSSL_new(ctx));
  55294. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  55295. ExpectIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  55296. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  55297. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  55298. ExpectIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  55299. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  55300. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  55301. wolfSSL_free(ssl);
  55302. wolfSSL_CTX_free(ctx);
  55303. #endif
  55304. return EXPECT_RESULT();
  55305. }
  55306. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  55307. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  55308. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  55309. enum HandShakeType hsType, word16 length)
  55310. {
  55311. size_t idx = 0;
  55312. byte* l;
  55313. /* record layer */
  55314. /* handshake type */
  55315. out[idx++] = handshake;
  55316. /* protocol version */
  55317. out[idx++] = 0xfe;
  55318. out[idx++] = 0xfd; /* DTLS 1.2 */
  55319. /* epoch 0 */
  55320. XMEMSET(out + idx, 0, 2);
  55321. idx += 2;
  55322. /* sequence number */
  55323. XMEMSET(out + idx, 0, 6);
  55324. c32toa(seq, out + idx + 2);
  55325. idx += 6;
  55326. /* length in BE */
  55327. if (length)
  55328. c16toa(length, out + idx);
  55329. else
  55330. c16toa(outSz - idx - 2, out + idx);
  55331. idx += 2;
  55332. /* handshake layer */
  55333. /* handshake type */
  55334. out[idx++] = (byte)hsType;
  55335. /* length */
  55336. l = out + idx;
  55337. idx += 3;
  55338. /* message seq */
  55339. c16toa(0, out + idx);
  55340. idx += 2;
  55341. /* frag offset */
  55342. c32to24(0, out + idx);
  55343. idx += 3;
  55344. /* frag length */
  55345. c32to24((word32)outSz - (word32)idx - 3, l);
  55346. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  55347. idx += 3;
  55348. XMEMSET(out + idx, 0, outSz - idx);
  55349. }
  55350. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  55351. {
  55352. byte msg[] = "This is a msg for the client";
  55353. byte reply[40];
  55354. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  55355. reply[sizeof(reply) - 1] = '\0';
  55356. fprintf(stderr, "Client message: %s\n", reply);
  55357. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  55358. }
  55359. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  55360. {
  55361. byte ch[50];
  55362. int fd = wolfSSL_get_fd(ssl);
  55363. byte msg[] = "This is a msg for the server";
  55364. byte reply[40];
  55365. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  55366. /* Server should ignore this datagram */
  55367. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  55368. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  55369. /* Server should ignore this datagram */
  55370. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  55371. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  55372. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  55373. reply[sizeof(reply) - 1] = '\0';
  55374. fprintf(stderr, "Server response: %s\n", reply);
  55375. }
  55376. static int test_wolfSSL_dtls_plaintext(void)
  55377. {
  55378. callback_functions func_cb_client;
  55379. callback_functions func_cb_server;
  55380. size_t i;
  55381. struct test_params {
  55382. method_provider client_meth;
  55383. method_provider server_meth;
  55384. ssl_callback on_result_server;
  55385. ssl_callback on_result_client;
  55386. } params[] = {
  55387. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  55388. test_wolfSSL_dtls_plaintext_server,
  55389. test_wolfSSL_dtls_plaintext_client},
  55390. };
  55391. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  55392. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  55393. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  55394. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  55395. func_cb_server.method = params[i].server_meth;
  55396. func_cb_client.method = params[i].client_meth;
  55397. func_cb_client.on_result = params[i].on_result_client;
  55398. func_cb_server.on_result = params[i].on_result_server;
  55399. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  55400. if (!func_cb_client.return_code)
  55401. return TEST_FAIL;
  55402. if (!func_cb_server.return_code)
  55403. return TEST_FAIL;
  55404. }
  55405. return TEST_RES_CHECK(1);
  55406. }
  55407. #else
  55408. static int test_wolfSSL_dtls_plaintext(void) {
  55409. return TEST_SKIPPED;
  55410. }
  55411. #endif
  55412. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  55413. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  55414. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  55415. {
  55416. byte b[1100]; /* buffer for the messages to send */
  55417. size_t idx = 0;
  55418. size_t seq_offset = 0;
  55419. size_t msg_offset = 0;
  55420. int i;
  55421. int fd = wolfSSL_get_fd(ssl);
  55422. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  55423. word32 seq_number = 100; /* start high so server definitely reads this */
  55424. word16 msg_number = 50; /* start high so server has to buffer this */
  55425. AssertIntEQ(ret, 1);
  55426. /* Now let's start spamming the peer with fragments it needs to store */
  55427. XMEMSET(b, -1, sizeof(b));
  55428. /* record layer */
  55429. /* handshake type */
  55430. b[idx++] = 22;
  55431. /* protocol version */
  55432. b[idx++] = 0xfe;
  55433. b[idx++] = 0xfd; /* DTLS 1.2 */
  55434. /* epoch 0 */
  55435. XMEMSET(b + idx, 0, 2);
  55436. idx += 2;
  55437. /* sequence number */
  55438. XMEMSET(b + idx, 0, 6);
  55439. seq_offset = idx + 2; /* increment only the low 32 bits */
  55440. idx += 6;
  55441. /* static length in BE */
  55442. c16toa(42, b + idx);
  55443. idx += 2;
  55444. /* handshake layer */
  55445. /* cert type */
  55446. b[idx++] = 11;
  55447. /* length */
  55448. c32to24(1000, b + idx);
  55449. idx += 3;
  55450. /* message seq */
  55451. c16toa(0, b + idx);
  55452. msg_offset = idx;
  55453. idx += 2;
  55454. /* frag offset */
  55455. c32to24(500, b + idx);
  55456. idx += 3;
  55457. /* frag length */
  55458. c32to24(30, b + idx);
  55459. idx += 3;
  55460. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  55461. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  55462. seq_number++, msg_number++, i++) {
  55463. struct timespec delay;
  55464. XMEMSET(&delay, 0, sizeof(delay));
  55465. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  55466. c32toa(seq_number, b + seq_offset);
  55467. c16toa(msg_number, b + msg_offset);
  55468. ret = (int)send(fd, b, 55, 0);
  55469. nanosleep(&delay, NULL);
  55470. }
  55471. }
  55472. #ifdef WOLFSSL_DTLS13
  55473. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  55474. {
  55475. const word16 sendCountMax = 100;
  55476. byte b[150]; /* buffer for the messages to send */
  55477. size_t idx = 0;
  55478. size_t msg_offset = 0;
  55479. int fd = wolfSSL_get_fd(ssl);
  55480. word16 msg_number = 10; /* start high so server has to buffer this */
  55481. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  55482. AssertIntEQ(ret, 1);
  55483. /* Now let's start spamming the peer with fragments it needs to store */
  55484. XMEMSET(b, -1, sizeof(b));
  55485. /* handshake type */
  55486. b[idx++] = 11;
  55487. /* length */
  55488. c32to24(10000, b + idx);
  55489. idx += 3;
  55490. /* message_seq */
  55491. msg_offset = idx;
  55492. idx += 2;
  55493. /* fragment_offset */
  55494. c32to24(5000, b + idx);
  55495. idx += 3;
  55496. /* fragment_length */
  55497. c32to24(100, b + idx);
  55498. idx += 3;
  55499. /* fragment contents */
  55500. idx += 100;
  55501. for (; ret > 0 && msg_number < sendCountMax; msg_number++) {
  55502. byte sendBuf[150];
  55503. int sendSz = sizeof(sendBuf);
  55504. struct timespec delay;
  55505. XMEMSET(&delay, 0, sizeof(delay));
  55506. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  55507. c16toa(msg_number, b + msg_offset);
  55508. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  55509. (int)idx, handshake, 0, 0, 0);
  55510. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  55511. nanosleep(&delay, NULL);
  55512. }
  55513. }
  55514. #endif
  55515. static int test_wolfSSL_dtls_fragments(void)
  55516. {
  55517. EXPECT_DECLS;
  55518. callback_functions func_cb_client;
  55519. callback_functions func_cb_server;
  55520. size_t i;
  55521. struct test_params {
  55522. method_provider client_meth;
  55523. method_provider server_meth;
  55524. ssl_callback spammer;
  55525. } params[] = {
  55526. #if !defined(WOLFSSL_NO_TLS12)
  55527. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  55528. test_wolfSSL_dtls12_fragments_spammer},
  55529. #endif
  55530. #ifdef WOLFSSL_DTLS13
  55531. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  55532. test_wolfSSL_dtls13_fragments_spammer},
  55533. #endif
  55534. };
  55535. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  55536. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  55537. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  55538. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  55539. func_cb_server.method = params[i].server_meth;
  55540. func_cb_client.method = params[i].client_meth;
  55541. func_cb_client.ssl_ready = params[i].spammer;
  55542. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  55543. ExpectFalse(func_cb_client.return_code);
  55544. ExpectFalse(func_cb_server.return_code);
  55545. /* The socket should be closed by the server resulting in a
  55546. * socket error, fatal error or reading a close notify alert */
  55547. if (func_cb_client.last_err != SOCKET_ERROR_E &&
  55548. func_cb_client.last_err != WOLFSSL_ERROR_ZERO_RETURN &&
  55549. func_cb_client.last_err != FATAL_ERROR) {
  55550. ExpectIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  55551. }
  55552. /* Check the server returned an error indicating the msg buffer
  55553. * was full */
  55554. ExpectIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  55555. if (EXPECT_FAIL())
  55556. break;
  55557. }
  55558. return EXPECT_RESULT();
  55559. }
  55560. static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
  55561. {
  55562. int fd, ret;
  55563. byte alert_msg[] = {
  55564. 0x15, /* alert type */
  55565. 0xfe, 0xfd, /* version */
  55566. 0x00, 0x00, /* epoch */
  55567. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
  55568. 0x00, 0x02, /* length */
  55569. 0x02, /* level: fatal */
  55570. 0x46 /* protocol version */
  55571. };
  55572. fd = wolfSSL_get_fd(ssl);
  55573. ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
  55574. AssertIntGT(ret, 0);
  55575. }
  55576. static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
  55577. {
  55578. callback_functions client_cbs, server_cbs;
  55579. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  55580. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  55581. client_cbs.doUdp = server_cbs.doUdp = 1;
  55582. if (version12) {
  55583. #if !defined(WOLFSSL_NO_TLS12)
  55584. client_cbs.method = wolfDTLSv1_2_client_method;
  55585. server_cbs.method = wolfDTLSv1_2_server_method;
  55586. #else
  55587. return TEST_SKIPPED;
  55588. #endif
  55589. }
  55590. else
  55591. {
  55592. #ifdef WOLFSSL_DTLS13
  55593. client_cbs.method = wolfDTLSv1_3_client_method;
  55594. server_cbs.method = wolfDTLSv1_3_server_method;
  55595. #else
  55596. return TEST_SKIPPED;
  55597. #endif /* WOLFSSL_DTLS13 */
  55598. }
  55599. client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
  55600. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  55601. if (!client_cbs.return_code)
  55602. return TEST_FAIL;
  55603. if (!server_cbs.return_code)
  55604. return TEST_FAIL;
  55605. return TEST_SUCCESS;
  55606. }
  55607. static int test_wolfSSL_ignore_alert_before_cookie(void)
  55608. {
  55609. int ret;
  55610. ret =_test_wolfSSL_ignore_alert_before_cookie(0);
  55611. if (ret != 0)
  55612. return ret;
  55613. ret =_test_wolfSSL_ignore_alert_before_cookie(1);
  55614. if (ret != 0)
  55615. return ret;
  55616. return 0;
  55617. }
  55618. static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
  55619. {
  55620. int ret;
  55621. int fd;
  55622. byte bad_msg[] = {
  55623. 0x17, /* app data */
  55624. 0xaa, 0xfd, /* bad version */
  55625. 0x00, 0x01, /* epoch 1 */
  55626. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
  55627. 0x00, 0x26, /* length: 38 bytes */
  55628. 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
  55629. 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
  55630. 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
  55631. 0x03, 0x18, 0x72
  55632. };
  55633. fd = wolfSSL_get_fd(ssl);
  55634. AssertIntGE(fd, 0);
  55635. ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
  55636. AssertIntEQ(ret, sizeof(bad_msg));
  55637. ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
  55638. AssertIntEQ(ret, sizeof("badrecordtest"));
  55639. }
  55640. static void test_wolfSSL_read_string(WOLFSSL* ssl)
  55641. {
  55642. byte buf[100];
  55643. int ret;
  55644. ret = wolfSSL_read(ssl, buf, sizeof(buf));
  55645. AssertIntGT(ret, 0);
  55646. AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
  55647. }
  55648. static int _test_wolfSSL_dtls_bad_record(
  55649. method_provider client_method, method_provider server_method)
  55650. {
  55651. callback_functions client_cbs, server_cbs;
  55652. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  55653. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  55654. client_cbs.doUdp = server_cbs.doUdp = 1;
  55655. client_cbs.method = client_method;
  55656. server_cbs.method = server_method;
  55657. client_cbs.on_result = test_wolfSSL_send_bad_record;
  55658. server_cbs.on_result = test_wolfSSL_read_string;
  55659. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  55660. if (!client_cbs.return_code)
  55661. return TEST_FAIL;
  55662. if (!server_cbs.return_code)
  55663. return TEST_FAIL;
  55664. return TEST_SUCCESS;
  55665. }
  55666. static int test_wolfSSL_dtls_bad_record(void)
  55667. {
  55668. int ret = TEST_SUCCESS;
  55669. #if !defined(WOLFSSL_NO_TLS12)
  55670. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
  55671. wolfDTLSv1_2_server_method);
  55672. #endif
  55673. #ifdef WOLFSSL_DTLS13
  55674. if (ret == TEST_SUCCESS) {
  55675. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
  55676. wolfDTLSv1_3_server_method);
  55677. }
  55678. #endif /* WOLFSSL_DTLS13 */
  55679. return ret;
  55680. }
  55681. #else
  55682. static int test_wolfSSL_dtls_fragments(void) {
  55683. return TEST_SKIPPED;
  55684. }
  55685. static int test_wolfSSL_ignore_alert_before_cookie(void) {
  55686. return TEST_SKIPPED;
  55687. }
  55688. static int test_wolfSSL_dtls_bad_record(void) {
  55689. return TEST_SKIPPED;
  55690. }
  55691. #endif
  55692. #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS) && \
  55693. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  55694. defined(HAVE_IO_TESTS_DEPENDENCIES)
  55695. static byte test_AEAD_fail_decryption = 0;
  55696. static byte test_AEAD_seq_num = 0;
  55697. static byte test_AEAD_done = 0;
  55698. static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  55699. {
  55700. int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
  55701. if (ret > 0) {
  55702. if (test_AEAD_fail_decryption) {
  55703. /* Modify the packet to trigger a decryption failure */
  55704. buf[ret/2] ^= 0xFF;
  55705. if (test_AEAD_fail_decryption == 1)
  55706. test_AEAD_fail_decryption = 0;
  55707. }
  55708. }
  55709. (void)ctx;
  55710. return ret;
  55711. }
  55712. static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
  55713. w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
  55714. {
  55715. if (sendLimit)
  55716. w64Zero(sendLimit);
  55717. switch (ssl->specs.bulk_cipher_algorithm) {
  55718. case wolfssl_aes_gcm:
  55719. if (sendLimit)
  55720. *sendLimit = AEAD_AES_LIMIT;
  55721. FALL_THROUGH;
  55722. case wolfssl_chacha:
  55723. if (hardLimit)
  55724. *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
  55725. if (keyUpdateLimit)
  55726. *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
  55727. break;
  55728. case wolfssl_aes_ccm:
  55729. if (sendLimit)
  55730. *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
  55731. if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
  55732. if (hardLimit)
  55733. *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
  55734. if (keyUpdateLimit)
  55735. *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
  55736. }
  55737. else {
  55738. if (hardLimit)
  55739. *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
  55740. if (keyUpdateLimit)
  55741. *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
  55742. }
  55743. break;
  55744. default:
  55745. fprintf(stderr, "Unrecognized bulk cipher");
  55746. AssertFalse(1);
  55747. break;
  55748. }
  55749. }
  55750. static void test_AEAD_limit_client(WOLFSSL* ssl)
  55751. {
  55752. int ret;
  55753. int i;
  55754. int didReKey = 0;
  55755. char msgBuf[20];
  55756. w64wrapper hardLimit;
  55757. w64wrapper keyUpdateLimit;
  55758. w64wrapper counter;
  55759. w64wrapper sendLimit;
  55760. test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
  55761. w64Zero(&counter);
  55762. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
  55763. wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
  55764. for (i = 0; i < 10; i++) {
  55765. /* Test some failed decryptions */
  55766. test_AEAD_fail_decryption = 1;
  55767. w64Increment(&counter);
  55768. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55769. /* Should succeed since decryption failures are dropped */
  55770. AssertIntGT(ret, 0);
  55771. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
  55772. }
  55773. test_AEAD_fail_decryption = 1;
  55774. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
  55775. w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  55776. /* 100 read calls should be enough to complete the key update */
  55777. w64Zero(&counter);
  55778. for (i = 0; i < 100; i++) {
  55779. /* Key update should be sent and negotiated */
  55780. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55781. AssertIntGT(ret, 0);
  55782. /* Epoch after one key update is 4 */
  55783. if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
  55784. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
  55785. didReKey = 1;
  55786. break;
  55787. }
  55788. }
  55789. AssertTrue(didReKey);
  55790. if (!w64IsZero(sendLimit)) {
  55791. /* Test the sending limit for AEAD ciphers */
  55792. Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
  55793. test_AEAD_seq_num = 1;
  55794. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  55795. AssertIntGT(ret, 0);
  55796. didReKey = 0;
  55797. w64Zero(&counter);
  55798. /* 100 read calls should be enough to complete the key update */
  55799. for (i = 0; i < 100; i++) {
  55800. /* Key update should be sent and negotiated */
  55801. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55802. AssertIntGT(ret, 0);
  55803. /* Epoch after another key update is 5 */
  55804. if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
  55805. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
  55806. didReKey = 1;
  55807. break;
  55808. }
  55809. }
  55810. AssertTrue(didReKey);
  55811. }
  55812. test_AEAD_fail_decryption = 2;
  55813. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
  55814. w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  55815. /* Connection should fail with a DECRYPT_ERROR */
  55816. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55817. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  55818. AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
  55819. test_AEAD_done = 1;
  55820. }
  55821. int counter = 0;
  55822. static void test_AEAD_limit_server(WOLFSSL* ssl)
  55823. {
  55824. char msgBuf[] = "Sending data";
  55825. int ret = WOLFSSL_SUCCESS;
  55826. w64wrapper sendLimit;
  55827. SOCKET_T fd = wolfSSL_get_fd(ssl);
  55828. struct timespec delay;
  55829. XMEMSET(&delay, 0, sizeof(delay));
  55830. delay.tv_nsec = 100000000; /* wait 0.1 seconds */
  55831. tcp_set_nonblocking(&fd); /* So that read doesn't block */
  55832. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  55833. test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
  55834. while (!test_AEAD_done && ret > 0) {
  55835. counter++;
  55836. if (test_AEAD_seq_num) {
  55837. /* We need to update the seq number so that we can understand the
  55838. * peer. Otherwise we will incorrectly interpret the seq number. */
  55839. Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
  55840. AssertNotNull(e);
  55841. e->nextPeerSeqNumber = sendLimit;
  55842. test_AEAD_seq_num = 0;
  55843. }
  55844. (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55845. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  55846. nanosleep(&delay, NULL);
  55847. }
  55848. }
  55849. static int test_wolfSSL_dtls_AEAD_limit(void)
  55850. {
  55851. callback_functions func_cb_client;
  55852. callback_functions func_cb_server;
  55853. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  55854. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  55855. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  55856. func_cb_server.method = wolfDTLSv1_3_server_method;
  55857. func_cb_client.method = wolfDTLSv1_3_client_method;
  55858. func_cb_server.on_result = test_AEAD_limit_server;
  55859. func_cb_client.on_result = test_AEAD_limit_client;
  55860. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  55861. if (!func_cb_client.return_code)
  55862. return TEST_FAIL;
  55863. if (!func_cb_server.return_code)
  55864. return TEST_FAIL;
  55865. return TEST_SUCCESS;
  55866. }
  55867. #else
  55868. static int test_wolfSSL_dtls_AEAD_limit(void)
  55869. {
  55870. return TEST_SKIPPED;
  55871. }
  55872. #endif
  55873. #if defined(WOLFSSL_DTLS) && \
  55874. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED)
  55875. static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
  55876. {
  55877. int fd, ret;
  55878. byte ch_msg[] = {
  55879. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  55880. 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  55881. 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
  55882. 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
  55883. 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
  55884. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  55885. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  55886. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  55887. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  55888. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  55889. 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  55890. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  55891. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  55892. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  55893. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  55894. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
  55895. 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
  55896. 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
  55897. 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
  55898. 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
  55899. 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
  55900. 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
  55901. 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
  55902. 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
  55903. 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
  55904. 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
  55905. 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
  55906. 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
  55907. 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
  55908. 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
  55909. 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
  55910. 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
  55911. 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
  55912. 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
  55913. 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
  55914. 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
  55915. 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
  55916. 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
  55917. 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
  55918. 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
  55919. 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
  55920. 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
  55921. 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
  55922. 0x31, 0x68, 0x4c
  55923. };
  55924. fd = wolfSSL_get_fd(ssl);
  55925. ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
  55926. AssertIntGT(ret, 0);
  55927. /* consume the HRR otherwise handshake will fail */
  55928. ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
  55929. AssertIntGT(ret, 0);
  55930. }
  55931. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  55932. static void test_wolfSSL_dtls_send_ch_with_invalid_cookie(WOLFSSL* ssl)
  55933. {
  55934. int fd, ret;
  55935. byte ch_msh_invalid_cookie[] = {
  55936. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
  55937. 0x4e, 0x01, 0x00, 0x02, 0x42, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02,
  55938. 0x42, 0xfe, 0xfd, 0x69, 0xca, 0x77, 0x60, 0x6f, 0xfc, 0xd1, 0x5b, 0x60,
  55939. 0x5d, 0xf1, 0xa6, 0x5c, 0x44, 0x71, 0xae, 0xca, 0x62, 0x19, 0x0c, 0xb6,
  55940. 0xf7, 0x2c, 0xa6, 0xd5, 0xd2, 0x99, 0x9d, 0x18, 0xae, 0xac, 0x11, 0x00,
  55941. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  55942. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  55943. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  55944. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  55945. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  55946. 0xe2, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  55947. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  55948. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  55949. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x2c, 0x00, 0x45,
  55950. 0x00, 0x43, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  55951. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  55952. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  55953. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  55954. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  55955. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x2d, 0x00,
  55956. 0x03, 0x02, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x0a, 0x00, 0x19,
  55957. 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00, 0x00, 0x16, 0x00, 0x00,
  55958. 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17, 0x00, 0x41, 0x04, 0x7c,
  55959. 0x5a, 0xc2, 0x5a, 0xfd, 0xcd, 0x2b, 0x08, 0xb2, 0xeb, 0x8e, 0xc0, 0x02,
  55960. 0x03, 0x9d, 0xb1, 0xc1, 0x0d, 0x7b, 0x7f, 0x46, 0x43, 0xdf, 0xf3, 0xee,
  55961. 0x2b, 0x78, 0x0e, 0x29, 0x8c, 0x42, 0x11, 0x2c, 0xde, 0xd7, 0x41, 0x0f,
  55962. 0x28, 0x94, 0x80, 0x41, 0x70, 0xc4, 0x17, 0xfd, 0x6d, 0xfa, 0xee, 0x9a,
  55963. 0xf2, 0xc4, 0x15, 0x4c, 0x5f, 0x54, 0xb6, 0x78, 0x6e, 0xf9, 0x63, 0x27,
  55964. 0x33, 0xb8, 0x7b, 0x01, 0x00, 0x01, 0x00, 0xd4, 0x46, 0x62, 0x9c, 0xbf,
  55965. 0x8f, 0x1b, 0x65, 0x9b, 0xf0, 0x29, 0x64, 0xd8, 0x50, 0x0e, 0x74, 0xf1,
  55966. 0x58, 0x10, 0xc9, 0xd9, 0x82, 0x5b, 0xd9, 0xbe, 0x14, 0xdf, 0xde, 0x86,
  55967. 0xb4, 0x2e, 0x15, 0xee, 0x4f, 0xf6, 0x74, 0x9e, 0x59, 0x11, 0x36, 0x2d,
  55968. 0xb9, 0x67, 0xaa, 0x5a, 0x09, 0x9b, 0x45, 0xf1, 0x01, 0x4c, 0x4e, 0xf6,
  55969. 0xda, 0x6a, 0xae, 0xa7, 0x73, 0x7b, 0x2e, 0xb6, 0x24, 0x89, 0x99, 0xb7,
  55970. 0x52, 0x16, 0x62, 0x0a, 0xab, 0x58, 0xf8, 0x3f, 0x10, 0x5b, 0x83, 0xfd,
  55971. 0x7b, 0x81, 0x77, 0x81, 0x8d, 0xef, 0x24, 0x56, 0x6d, 0xba, 0x49, 0xd4,
  55972. 0x8b, 0xb5, 0xa0, 0xb1, 0xc9, 0x8c, 0x32, 0x95, 0x1c, 0x5e, 0x0a, 0x4b,
  55973. 0xf6, 0x00, 0x50, 0x0a, 0x87, 0x99, 0x59, 0xcf, 0x6f, 0x9d, 0x02, 0xd0,
  55974. 0x1b, 0xa1, 0x96, 0x45, 0x28, 0x76, 0x40, 0x33, 0x28, 0xc9, 0xa1, 0xfd,
  55975. 0x46, 0xab, 0x2c, 0x9e, 0x5e, 0xc6, 0x74, 0x19, 0x9a, 0xf5, 0x9b, 0x51,
  55976. 0x11, 0x4f, 0xc8, 0xb9, 0x99, 0x6b, 0x4e, 0x3e, 0x31, 0x64, 0xb4, 0x92,
  55977. 0xf4, 0x0d, 0x41, 0x4b, 0x2c, 0x65, 0x23, 0xf7, 0x47, 0xe3, 0xa5, 0x2e,
  55978. 0xe4, 0x9c, 0x2b, 0xc9, 0x41, 0x22, 0x83, 0x8a, 0x23, 0xef, 0x29, 0x7e,
  55979. 0x4f, 0x3f, 0xa3, 0xbf, 0x73, 0x2b, 0xd7, 0xcc, 0xc8, 0xc6, 0xe9, 0xbc,
  55980. 0x01, 0xb7, 0x32, 0x63, 0xd4, 0x7e, 0x7f, 0x9a, 0xaf, 0x5f, 0x05, 0x31,
  55981. 0x53, 0xd6, 0x1f, 0xa2, 0xd0, 0xdf, 0x67, 0x56, 0xf1, 0x9c, 0x4a, 0x9d,
  55982. 0x83, 0xb4, 0xef, 0xb3, 0xf2, 0xcc, 0xf1, 0x91, 0x6c, 0x47, 0xc3, 0x8b,
  55983. 0xd0, 0x92, 0x79, 0x3d, 0xa0, 0xc0, 0x3a, 0x57, 0x26, 0x6d, 0x0a, 0xad,
  55984. 0x5f, 0xad, 0xb4, 0x74, 0x48, 0x4a, 0x51, 0xe1, 0xb5, 0x82, 0x0a, 0x4c,
  55985. 0x4f, 0x9d, 0xaf, 0xee, 0x5a, 0xa2, 0x4d, 0x4d, 0x5f, 0xe0, 0x17, 0x00,
  55986. 0x23, 0x00, 0x00
  55987. };
  55988. byte alert_reply[50];
  55989. byte expected_alert_reply[] = {
  55990. 0x15, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  55991. 0x02, 0x02, 0x2f
  55992. };
  55993. fd = wolfSSL_get_fd(ssl);
  55994. ret = (int)send(fd, ch_msh_invalid_cookie, sizeof(ch_msh_invalid_cookie), 0);
  55995. AssertIntGT(ret, 0);
  55996. /* should reply with an illegal_parameter reply */
  55997. ret = (int)recv(fd, alert_reply, sizeof(alert_reply), 0);
  55998. AssertIntEQ(ret, sizeof(expected_alert_reply));
  55999. AssertIntEQ(XMEMCMP(alert_reply, expected_alert_reply, sizeof(expected_alert_reply)), 0);
  56000. }
  56001. #endif
  56002. static word32 test_wolfSSL_dtls_stateless_HashWOLFSSL(const WOLFSSL* ssl)
  56003. {
  56004. #ifndef NO_MD5
  56005. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  56006. #elif !defined(NO_SHA)
  56007. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  56008. #elif !defined(NO_SHA256)
  56009. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  56010. #else
  56011. #error "We need a digest to hash the WOLFSSL object"
  56012. #endif
  56013. byte hashBuf[WC_MAX_DIGEST_SIZE];
  56014. wc_HashAlg hash;
  56015. const TLSX* exts = ssl->extensions;
  56016. WOLFSSL sslCopy; /* Use a copy to omit certain fields */
  56017. HS_Hashes* hsHashes = ssl->hsHashes; /* Is re-allocated in
  56018. * InitHandshakeHashes */
  56019. XMEMCPY(&sslCopy, ssl, sizeof(*ssl));
  56020. XMEMSET(hashBuf, 0, sizeof(hashBuf));
  56021. /* Following fields are not important to compare */
  56022. XMEMSET(sslCopy.buffers.inputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  56023. sslCopy.buffers.inputBuffer.buffer = NULL;
  56024. sslCopy.buffers.inputBuffer.bufferSize = 0;
  56025. sslCopy.buffers.inputBuffer.dynamicFlag = 0;
  56026. sslCopy.buffers.inputBuffer.offset = 0;
  56027. XMEMSET(sslCopy.buffers.outputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  56028. sslCopy.buffers.outputBuffer.buffer = NULL;
  56029. sslCopy.buffers.outputBuffer.bufferSize = 0;
  56030. sslCopy.buffers.outputBuffer.dynamicFlag = 0;
  56031. sslCopy.buffers.outputBuffer.offset = 0;
  56032. sslCopy.error = 0;
  56033. sslCopy.curSize = 0;
  56034. sslCopy.curStartIdx = 0;
  56035. sslCopy.keys.curSeq_lo = 0;
  56036. XMEMSET(&sslCopy.curRL, 0, sizeof(sslCopy.curRL));
  56037. #ifdef WOLFSSL_DTLS13
  56038. XMEMSET(&sslCopy.keys.curSeq, 0, sizeof(sslCopy.keys.curSeq));
  56039. sslCopy.dtls13FastTimeout = 0;
  56040. #endif
  56041. sslCopy.keys.dtls_peer_handshake_number = 0;
  56042. XMEMSET(&sslCopy.alert_history, 0, sizeof(sslCopy.alert_history));
  56043. sslCopy.hsHashes = NULL;
  56044. #ifdef WOLFSSL_ASYNC_IO
  56045. #ifdef WOLFSSL_ASYNC_CRYPT
  56046. sslCopy.asyncDev = NULL;
  56047. #endif
  56048. sslCopy.async = NULL;
  56049. #endif
  56050. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  56051. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)&sslCopy, sizeof(sslCopy)), 0);
  56052. /* hash extension list */
  56053. while (exts != NULL) {
  56054. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)exts, sizeof(*exts)), 0);
  56055. exts = exts->next;
  56056. }
  56057. /* Hash suites */
  56058. if (sslCopy.suites != NULL) {
  56059. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)sslCopy.suites,
  56060. sizeof(struct Suites)), 0);
  56061. }
  56062. /* Hash hsHashes */
  56063. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)hsHashes,
  56064. sizeof(*hsHashes)), 0);
  56065. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  56066. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  56067. return MakeWordFromHash(hashBuf);
  56068. }
  56069. static CallbackIORecv test_wolfSSL_dtls_compare_stateless_cb;
  56070. static int test_wolfSSL_dtls_compare_stateless_cb_call_once;
  56071. static int test_wolfSSL_dtls_compare_stateless_read_cb_once(WOLFSSL *ssl,
  56072. char *buf, int sz, void *ctx)
  56073. {
  56074. if (test_wolfSSL_dtls_compare_stateless_cb_call_once) {
  56075. test_wolfSSL_dtls_compare_stateless_cb_call_once = 0;
  56076. return test_wolfSSL_dtls_compare_stateless_cb(ssl, buf, sz, ctx);
  56077. }
  56078. else {
  56079. return WOLFSSL_CBIO_ERR_WANT_READ;
  56080. }
  56081. }
  56082. static void test_wolfSSL_dtls_compare_stateless(WOLFSSL* ssl)
  56083. {
  56084. /* Compare the ssl object before and after one ClientHello msg */
  56085. SOCKET_T fd = wolfSSL_get_fd(ssl);
  56086. int res;
  56087. int err;
  56088. word32 initHash;
  56089. test_wolfSSL_dtls_compare_stateless_cb = ssl->CBIORecv;
  56090. test_wolfSSL_dtls_compare_stateless_cb_call_once = 1;
  56091. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  56092. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_read_cb_once;
  56093. initHash = test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl);
  56094. (void)initHash;
  56095. res = tcp_select(fd, 5);
  56096. /* We are expecting a msg. A timeout indicates failure. */
  56097. AssertIntEQ(res, TEST_RECV_READY);
  56098. res = wolfSSL_accept(ssl);
  56099. err = wolfSSL_get_error(ssl, res);
  56100. AssertIntEQ(res, WOLFSSL_FATAL_ERROR);
  56101. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  56102. AssertIntEQ(initHash, test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl));
  56103. wolfSSL_dtls_set_using_nonblock(ssl, 0);
  56104. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_cb;
  56105. }
  56106. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  56107. static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
  56108. {
  56109. int ret;
  56110. ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
  56111. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  56112. test_wolfSSL_dtls_compare_stateless(ssl);
  56113. }
  56114. #endif
  56115. static int test_wolfSSL_dtls_stateless(void)
  56116. {
  56117. callback_functions client_cbs, server_cbs;
  56118. size_t i;
  56119. struct {
  56120. method_provider client_meth;
  56121. method_provider server_meth;
  56122. ssl_callback client_ssl_ready;
  56123. ssl_callback server_ssl_ready;
  56124. } test_params[] = {
  56125. #if !defined(WOLFSSL_NO_TLS12)
  56126. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  56127. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_compare_stateless},
  56128. #endif
  56129. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  56130. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  56131. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
  56132. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  56133. test_wolfSSL_dtls_send_ch_with_invalid_cookie, test_wolfSSL_dtls_enable_hrrcookie},
  56134. #endif
  56135. };
  56136. if (0 == sizeof(test_params)){
  56137. return TEST_SKIPPED;
  56138. }
  56139. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  56140. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56141. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56142. client_cbs.doUdp = server_cbs.doUdp = 1;
  56143. client_cbs.method = test_params[i].client_meth;
  56144. server_cbs.method = test_params[i].server_meth;
  56145. client_cbs.ssl_ready = test_params[i].client_ssl_ready;
  56146. server_cbs.ssl_ready = test_params[i].server_ssl_ready;
  56147. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56148. if (!client_cbs.return_code)
  56149. return TEST_FAIL;
  56150. if (!server_cbs.return_code)
  56151. return TEST_FAIL;
  56152. }
  56153. return TEST_SUCCESS;
  56154. }
  56155. #else
  56156. static int test_wolfSSL_dtls_stateless(void)
  56157. {
  56158. return TEST_SKIPPED;
  56159. }
  56160. #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
  56161. * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
  56162. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  56163. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  56164. !defined(WOLFSSL_NO_CLIENT_AUTH))
  56165. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  56166. {
  56167. int ret;
  56168. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  56169. fprintf(stderr, "loading cert %s failed\n", certA);
  56170. fprintf(stderr, "Error: (%d): %s\n", ret,
  56171. wolfSSL_ERR_reason_error_string(ret));
  56172. return -1;
  56173. }
  56174. return 0;
  56175. }
  56176. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  56177. {
  56178. int ret;
  56179. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  56180. != WOLFSSL_SUCCESS) {
  56181. fprintf(stderr, "could not verify the cert: %s\n", certA);
  56182. fprintf(stderr, "Error: (%d): %s\n", ret,
  56183. wolfSSL_ERR_reason_error_string(ret));
  56184. return -1;
  56185. }
  56186. else {
  56187. fprintf(stderr, "successfully verified: %s\n", certA);
  56188. }
  56189. return 0;
  56190. }
  56191. #define LOAD_ONE_CA(a, b, c, d) \
  56192. do { \
  56193. (a) = load_ca_into_cm(c, d); \
  56194. if ((a) != 0) \
  56195. return (b); \
  56196. else \
  56197. (b)--; \
  56198. } while(0)
  56199. #define VERIFY_ONE_CERT(a, b, c, d) \
  56200. do { \
  56201. (a) = verify_cert_with_cm(c, d);\
  56202. if ((a) != 0) \
  56203. return (b); \
  56204. else \
  56205. (b)--; \
  56206. } while(0)
  56207. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  56208. {
  56209. int ret;
  56210. int i = -1;
  56211. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  56212. char chainGArr[9][50] = {"certs/ca-cert.pem",
  56213. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  56214. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  56215. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  56216. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  56217. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  56218. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  56219. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  56220. "certs/test-pathlen/chainG-entity.pem"};
  56221. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  56222. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  56223. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  56224. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  56225. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  56226. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  56227. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  56228. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  56229. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  56230. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  56231. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  56232. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  56233. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  56234. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  56235. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  56236. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  56237. /* test validating the entity twice, should have no effect on pathLen since
  56238. * entity/leaf cert */
  56239. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  56240. return ret;
  56241. }
  56242. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  56243. {
  56244. int ret;
  56245. int i = -1;
  56246. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  56247. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  56248. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  56249. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  56250. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  56251. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  56252. */
  56253. char chainHArr[6][50] = {"certs/ca-cert.pem",
  56254. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  56255. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  56256. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  56257. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  56258. "certs/test-pathlen/chainH-entity.pem"};
  56259. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  56260. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  56261. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  56262. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  56263. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  56264. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  56265. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  56266. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  56267. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  56268. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  56269. return ret;
  56270. }
  56271. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  56272. {
  56273. int ret;
  56274. int i = -1;
  56275. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  56276. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  56277. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  56278. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  56279. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  56280. */
  56281. char chainIArr[5][50] = {"certs/ca-cert.pem",
  56282. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  56283. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  56284. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  56285. "certs/test-pathlen/chainI-entity.pem"};
  56286. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  56287. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  56288. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  56289. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  56290. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  56291. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  56292. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  56293. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  56294. return ret;
  56295. }
  56296. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  56297. {
  56298. int ret;
  56299. int i = -1;
  56300. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  56301. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  56302. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  56303. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  56304. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  56305. */
  56306. char chainJArr[6][50] = {"certs/ca-cert.pem",
  56307. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  56308. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  56309. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  56310. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  56311. "certs/test-pathlen/chainJ-entity.pem"};
  56312. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  56313. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  56314. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  56315. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  56316. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  56317. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  56318. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  56319. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  56320. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  56321. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  56322. return ret;
  56323. }
  56324. static int test_various_pathlen_chains(void)
  56325. {
  56326. EXPECT_DECLS;
  56327. WOLFSSL_CERT_MANAGER* cm = NULL;
  56328. /* Test chain G (large chain with varying pathLens) */
  56329. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56330. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  56331. ExpectIntEQ(test_chainG(cm), -1);
  56332. #else
  56333. ExpectIntEQ(test_chainG(cm), 0);
  56334. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  56335. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56336. wolfSSL_CertManagerFree(cm);
  56337. /* end test chain G */
  56338. /* Test chain H (5 chain with same pathLens) */
  56339. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56340. ExpectIntLT(test_chainH(cm), 0);
  56341. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56342. wolfSSL_CertManagerFree(cm);
  56343. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56344. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56345. wolfSSL_CertManagerFree(cm);
  56346. /* end test chain H */
  56347. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  56348. * followed by 2 ICA's, should pass) */
  56349. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56350. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  56351. ExpectIntEQ(test_chainI(cm), -1);
  56352. #else
  56353. ExpectIntEQ(test_chainI(cm), 0);
  56354. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  56355. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56356. wolfSSL_CertManagerFree(cm);
  56357. cm = NULL;
  56358. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56359. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56360. wolfSSL_CertManagerFree(cm);
  56361. cm = NULL;
  56362. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  56363. * this time followed by 3 ICA's, should fail */
  56364. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56365. ExpectIntLT(test_chainJ(cm), 0);
  56366. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56367. wolfSSL_CertManagerFree(cm);
  56368. cm = NULL;
  56369. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56370. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56371. wolfSSL_CertManagerFree(cm);
  56372. return EXPECT_RESULT();
  56373. }
  56374. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  56375. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  56376. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  56377. {
  56378. EXPECT_DECLS;
  56379. byte ekm[100] = {0};
  56380. (void)ctx;
  56381. /* Success Cases */
  56382. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56383. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  56384. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56385. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  56386. /* Use some random context */
  56387. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56388. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  56389. /* Failure cases */
  56390. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56391. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  56392. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56393. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  56394. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56395. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  56396. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56397. "extended master secret", XSTR_SIZEOF("extended master secret"),
  56398. NULL, 0, 0), 0);
  56399. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56400. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  56401. return EXPECT_RESULT();
  56402. }
  56403. static int test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  56404. {
  56405. wolfSSL_KeepArrays(ssl);
  56406. return TEST_SUCCESS;
  56407. }
  56408. static int test_export_keying_material(void)
  56409. {
  56410. EXPECT_DECLS;
  56411. test_ssl_cbf serverCb;
  56412. test_ssl_cbf clientCb;
  56413. XMEMSET(&serverCb, 0, sizeof(serverCb));
  56414. XMEMSET(&clientCb, 0, sizeof(clientCb));
  56415. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  56416. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&clientCb,
  56417. &serverCb, test_export_keying_material_cb), TEST_SUCCESS);
  56418. return EXPECT_RESULT();
  56419. }
  56420. #endif /* HAVE_KEYING_MATERIAL */
  56421. static int test_wolfSSL_THREADID_hash(void)
  56422. {
  56423. EXPECT_DECLS;
  56424. #if defined(OPENSSL_EXTRA)
  56425. CRYPTO_THREADID id;
  56426. CRYPTO_THREADID_current(NULL);
  56427. /* Hash result is unsigned long. */
  56428. ExpectTrue(CRYPTO_THREADID_hash(NULL) == 0UL);
  56429. XMEMSET(&id, 0, sizeof(id));
  56430. ExpectTrue(CRYPTO_THREADID_hash(&id) == 0UL);
  56431. #endif /* OPENSSL_EXTRA */
  56432. return EXPECT_RESULT();
  56433. }
  56434. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  56435. {
  56436. EXPECT_DECLS;
  56437. #if defined(OPENSSL_EXTRA)
  56438. WOLFSSL_CTX* ctx = NULL;
  56439. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,0), 1);
  56440. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,1), 1);
  56441. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,0), 1);
  56442. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,1), 1);
  56443. #endif /* OPENSSL_EXTRA */
  56444. return EXPECT_RESULT();
  56445. }
  56446. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  56447. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  56448. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  56449. {
  56450. EXPECT_DECLS;
  56451. callback_functions* callbacks = NULL;
  56452. WOLFSSL_CTX* ctx = NULL;
  56453. WOLFSSL* ssl = NULL;
  56454. SOCKET_T sfd = 0;
  56455. SOCKET_T cfd = 0;
  56456. word16 port;
  56457. char msg[] = "I hear you fa shizzle!";
  56458. int len = (int) XSTRLEN(msg);
  56459. char input[1024];
  56460. int ret = 0;
  56461. int err = 0;
  56462. if (!args)
  56463. WOLFSSL_RETURN_FROM_THREAD(0);
  56464. ((func_args*)args)->return_code = TEST_FAIL;
  56465. callbacks = ((func_args*)args)->callbacks;
  56466. ctx = wolfSSL_CTX_new(callbacks->method());
  56467. #if defined(USE_WINDOWS_API)
  56468. port = ((func_args*)args)->signal->port;
  56469. #else
  56470. /* Let tcp_listen assign port */
  56471. port = 0;
  56472. #endif
  56473. #ifdef WOLFSSL_TIRTOS
  56474. fdOpenSession(Task_self());
  56475. #endif
  56476. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  56477. caCertFile, 0));
  56478. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  56479. svrCertFile, WOLFSSL_FILETYPE_PEM));
  56480. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  56481. svrKeyFile, WOLFSSL_FILETYPE_PEM));
  56482. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  56483. ExpectIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  56484. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  56485. #elif !defined(NO_DH)
  56486. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  56487. #endif
  56488. if (callbacks->ctx_ready)
  56489. callbacks->ctx_ready(ctx);
  56490. ssl = wolfSSL_new(ctx);
  56491. ExpectNotNull(ssl);
  56492. /* listen and accept */
  56493. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  56494. CloseSocket(sfd);
  56495. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  56496. if (callbacks->ssl_ready)
  56497. callbacks->ssl_ready(ssl);
  56498. if (EXPECT_SUCCESS()) {
  56499. do {
  56500. err = 0; /* Reset error */
  56501. ret = wolfSSL_accept(ssl);
  56502. if (ret != WOLFSSL_SUCCESS) {
  56503. err = wolfSSL_get_error(ssl, 0);
  56504. }
  56505. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  56506. }
  56507. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  56508. /* read and write data */
  56509. XMEMSET(input, 0, sizeof(input));
  56510. while (EXPECT_SUCCESS()) {
  56511. ret = wolfSSL_read(ssl, input, sizeof(input));
  56512. if (ret > 0) {
  56513. break;
  56514. }
  56515. else {
  56516. err = wolfSSL_get_error(ssl,ret);
  56517. if (err == WOLFSSL_ERROR_WANT_READ) {
  56518. continue;
  56519. }
  56520. break;
  56521. }
  56522. }
  56523. if (EXPECT_SUCCESS() && (err == WOLFSSL_ERROR_ZERO_RETURN)) {
  56524. do {
  56525. ret = wolfSSL_write(ssl, msg, len);
  56526. if (ret > 0) {
  56527. break;
  56528. }
  56529. } while (ret < 0);
  56530. }
  56531. /* bidirectional shutdown */
  56532. while (EXPECT_SUCCESS()) {
  56533. ret = wolfSSL_shutdown(ssl);
  56534. ExpectIntNE(ret, WOLFSSL_FATAL_ERROR);
  56535. if (ret == WOLFSSL_SUCCESS) {
  56536. break;
  56537. }
  56538. }
  56539. if (EXPECT_SUCCESS()) {
  56540. /* wait for the peer to disconnect the tcp connection */
  56541. do {
  56542. ret = wolfSSL_read(ssl, input, sizeof(input));
  56543. err = wolfSSL_get_error(ssl, ret);
  56544. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  56545. }
  56546. /* detect TCP disconnect */
  56547. ExpectIntLE(ret,WOLFSSL_FAILURE);
  56548. ExpectIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  56549. ((func_args*)args)->return_code = EXPECT_RESULT();
  56550. wolfSSL_free(ssl);
  56551. wolfSSL_CTX_free(ctx);
  56552. CloseSocket(cfd);
  56553. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  56554. wc_ecc_fp_free(); /* free per thread cache */
  56555. #endif
  56556. WOLFSSL_RETURN_FROM_THREAD(0);
  56557. }
  56558. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  56559. {
  56560. EXPECT_DECLS;
  56561. callback_functions* callbacks = NULL;
  56562. WOLFSSL_CTX* ctx = NULL;
  56563. WOLFSSL* ssl = NULL;
  56564. SOCKET_T sfd = 0;
  56565. char msg[] = "hello wolfssl server!";
  56566. int len = (int) XSTRLEN(msg);
  56567. char input[1024];
  56568. int idx;
  56569. int ret, err;
  56570. if (!args)
  56571. WOLFSSL_RETURN_FROM_THREAD(0);
  56572. ((func_args*)args)->return_code = TEST_FAIL;
  56573. callbacks = ((func_args*)args)->callbacks;
  56574. ctx = wolfSSL_CTX_new(callbacks->method());
  56575. #ifdef WOLFSSL_TIRTOS
  56576. fdOpenSession(Task_self());
  56577. #endif
  56578. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  56579. caCertFile, 0));
  56580. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  56581. cliCertFile, WOLFSSL_FILETYPE_PEM));
  56582. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  56583. cliKeyFile, WOLFSSL_FILETYPE_PEM));
  56584. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  56585. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  56586. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  56587. if (EXPECT_SUCCESS()) {
  56588. do {
  56589. err = 0; /* Reset error */
  56590. ret = wolfSSL_connect(ssl);
  56591. if (ret != WOLFSSL_SUCCESS) {
  56592. err = wolfSSL_get_error(ssl, 0);
  56593. }
  56594. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  56595. }
  56596. ExpectIntGE(wolfSSL_write(ssl, msg, len), 0);
  56597. if (EXPECT_SUCCESS()) {
  56598. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  56599. input[idx] = 0;
  56600. }
  56601. }
  56602. if (EXPECT_SUCCESS()) {
  56603. ret = wolfSSL_shutdown(ssl);
  56604. if (ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  56605. ret = wolfSSL_shutdown(ssl);
  56606. }
  56607. }
  56608. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  56609. ((func_args*)args)->return_code = EXPECT_RESULT();
  56610. wolfSSL_free(ssl);
  56611. wolfSSL_CTX_free(ctx);
  56612. CloseSocket(sfd);
  56613. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  56614. wc_ecc_fp_free(); /* free per thread cache */
  56615. #endif
  56616. WOLFSSL_RETURN_FROM_THREAD(0);
  56617. }
  56618. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  56619. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  56620. /* This test is to check wolfSSL_read behaves as same as
  56621. * openSSL when it is called after SSL_shutdown completes.
  56622. */
  56623. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  56624. {
  56625. EXPECT_DECLS;
  56626. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  56627. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  56628. tcp_ready ready;
  56629. func_args client_args;
  56630. func_args server_args;
  56631. THREAD_TYPE serverThread;
  56632. THREAD_TYPE clientThread;
  56633. callback_functions server_cbf;
  56634. callback_functions client_cbf;
  56635. #ifdef WOLFSSL_TIRTOS
  56636. fdOpenSession(Task_self());
  56637. #endif
  56638. StartTCP();
  56639. InitTcpReady(&ready);
  56640. #if defined(USE_WINDOWS_API)
  56641. /* use RNG to get random port if using windows */
  56642. ready.port = GetRandomPort();
  56643. #endif
  56644. XMEMSET(&client_args, 0, sizeof(func_args));
  56645. XMEMSET(&server_args, 0, sizeof(func_args));
  56646. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  56647. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  56648. server_cbf.method = wolfTLSv1_2_server_method;
  56649. client_cbf.method = wolfTLSv1_2_client_method;
  56650. server_args.callbacks = &server_cbf;
  56651. client_args.callbacks = &client_cbf;
  56652. server_args.signal = &ready;
  56653. client_args.signal = &ready;
  56654. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  56655. wait_tcp_ready(&server_args);
  56656. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  56657. join_thread(clientThread);
  56658. join_thread(serverThread);
  56659. ExpectTrue(client_args.return_code);
  56660. ExpectTrue(server_args.return_code);
  56661. FreeTcpReady(&ready);
  56662. #endif
  56663. return EXPECT_RESULT();
  56664. }
  56665. static int test_wolfSSL_CTX_get_min_proto_version(void)
  56666. {
  56667. EXPECT_DECLS;
  56668. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  56669. WOLFSSL_CTX *ctx = NULL;
  56670. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  56671. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  56672. WOLFSSL_SUCCESS);
  56673. #ifdef WOLFSSL_ALLOW_SSLV3
  56674. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  56675. #else
  56676. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  56677. #endif
  56678. wolfSSL_CTX_free(ctx);
  56679. ctx = NULL;
  56680. #ifdef WOLFSSL_ALLOW_TLSV10
  56681. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  56682. #else
  56683. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  56684. #endif
  56685. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION),
  56686. WOLFSSL_SUCCESS);
  56687. #ifdef WOLFSSL_ALLOW_TLSV10
  56688. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  56689. #else
  56690. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  56691. #endif
  56692. wolfSSL_CTX_free(ctx);
  56693. ctx = NULL;
  56694. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  56695. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION),
  56696. WOLFSSL_SUCCESS);
  56697. #ifndef NO_OLD_TLS
  56698. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  56699. #else
  56700. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  56701. #endif
  56702. wolfSSL_CTX_free(ctx);
  56703. ctx = NULL;
  56704. #ifndef WOLFSSL_NO_TLS12
  56705. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  56706. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION),
  56707. WOLFSSL_SUCCESS);
  56708. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  56709. wolfSSL_CTX_free(ctx);
  56710. ctx = NULL;
  56711. #endif
  56712. #ifdef WOLFSSL_TLS13
  56713. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  56714. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION),
  56715. WOLFSSL_SUCCESS);
  56716. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  56717. wolfSSL_CTX_free(ctx);
  56718. ctx = NULL;
  56719. #endif
  56720. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  56721. return EXPECT_RESULT();
  56722. }
  56723. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  56724. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  56725. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  56726. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  56727. static int test_wolfSSL_set_SSL_CTX(void)
  56728. {
  56729. EXPECT_DECLS;
  56730. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) \
  56731. && !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13) && \
  56732. !defined(NO_RSA)
  56733. WOLFSSL_CTX *ctx1 = NULL;
  56734. WOLFSSL_CTX *ctx2 = NULL;
  56735. WOLFSSL *ssl = NULL;
  56736. const byte *session_id1 = (const byte *)"CTX1";
  56737. const byte *session_id2 = (const byte *)"CTX2";
  56738. ExpectNotNull(ctx1 = wolfSSL_CTX_new(wolfTLS_server_method()));
  56739. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx1, svrCertFile,
  56740. WOLFSSL_FILETYPE_PEM));
  56741. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx1, svrKeyFile,
  56742. WOLFSSL_FILETYPE_PEM));
  56743. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx1, TLS1_2_VERSION),
  56744. WOLFSSL_SUCCESS);
  56745. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx1), TLS1_2_VERSION);
  56746. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx1), TLS1_3_VERSION);
  56747. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx1, session_id1, 4),
  56748. WOLFSSL_SUCCESS);
  56749. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfTLS_server_method()));
  56750. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx2, svrCertFile,
  56751. WOLFSSL_FILETYPE_PEM));
  56752. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx2, svrKeyFile,
  56753. WOLFSSL_FILETYPE_PEM));
  56754. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx2, TLS1_2_VERSION),
  56755. WOLFSSL_SUCCESS);
  56756. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx2, TLS1_2_VERSION),
  56757. WOLFSSL_SUCCESS);
  56758. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx2), TLS1_2_VERSION);
  56759. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx2), TLS1_2_VERSION);
  56760. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx2, session_id2, 4),
  56761. WOLFSSL_SUCCESS);
  56762. #ifdef HAVE_SESSION_TICKET
  56763. ExpectIntEQ((wolfSSL_CTX_get_options(ctx1) & SSL_OP_NO_TICKET), 0);
  56764. wolfSSL_CTX_set_options(ctx2, SSL_OP_NO_TICKET);
  56765. ExpectIntNE((wolfSSL_CTX_get_options(ctx2) & SSL_OP_NO_TICKET), 0);
  56766. #endif
  56767. ExpectNotNull(ssl = wolfSSL_new(ctx2));
  56768. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  56769. #ifdef WOLFSSL_INT_H
  56770. #ifdef WOLFSSL_SESSION_ID_CTX
  56771. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id2, 4), 0);
  56772. #endif
  56773. ExpectTrue(ssl->buffers.certificate == ctx2->certificate);
  56774. ExpectTrue(ssl->buffers.certChain == ctx2->certChain);
  56775. #endif
  56776. #ifdef HAVE_SESSION_TICKET
  56777. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  56778. #endif
  56779. /* Set the ctx1 that has TLSv1.3 as max proto version */
  56780. ExpectNotNull(wolfSSL_set_SSL_CTX(ssl, ctx1));
  56781. /* MUST not change proto versions of ssl */
  56782. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  56783. #ifdef HAVE_SESSION_TICKET
  56784. /* MUST not change */
  56785. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  56786. #endif
  56787. /* MUST change */
  56788. #ifdef WOLFSSL_INT_H
  56789. ExpectTrue(ssl->buffers.certificate == ctx1->certificate);
  56790. ExpectTrue(ssl->buffers.certChain == ctx1->certChain);
  56791. #ifdef WOLFSSL_SESSION_ID_CTX
  56792. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id1, 4), 0);
  56793. #endif
  56794. #endif
  56795. wolfSSL_free(ssl);
  56796. wolfSSL_CTX_free(ctx1);
  56797. wolfSSL_CTX_free(ctx2);
  56798. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  56799. return EXPECT_RESULT();
  56800. }
  56801. #endif /* defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  56802. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  56803. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  56804. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))) */
  56805. static int test_wolfSSL_security_level(void)
  56806. {
  56807. EXPECT_DECLS;
  56808. #if defined(OPENSSL_EXTRA)
  56809. SSL_CTX *ctx = NULL;
  56810. #ifdef WOLFSSL_TLS13
  56811. #ifdef NO_WOLFSSL_SERVER
  56812. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  56813. #else
  56814. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  56815. #endif
  56816. SSL_CTX_set_security_level(NULL, 1);
  56817. SSL_CTX_set_security_level(ctx, 1);
  56818. ExpectIntEQ(SSL_CTX_get_security_level(NULL), 0);
  56819. /* Stub so nothing happens. */
  56820. ExpectIntEQ(SSL_CTX_get_security_level(ctx), 0);
  56821. SSL_CTX_free(ctx);
  56822. #else
  56823. (void)ctx;
  56824. #endif
  56825. #endif
  56826. return EXPECT_RESULT();
  56827. }
  56828. static int test_wolfSSL_SSL_in_init(void)
  56829. {
  56830. EXPECT_DECLS;
  56831. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  56832. SSL_CTX* ctx = NULL;
  56833. SSL* ssl = NULL;
  56834. const char* testCertFile;
  56835. const char* testKeyFile;
  56836. #ifdef WOLFSSL_TLS13
  56837. #ifdef NO_WOLFSSL_SERVER
  56838. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  56839. #else
  56840. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  56841. #endif
  56842. #else
  56843. #ifdef NO_WOLFSSL_SERVER
  56844. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  56845. #else
  56846. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56847. #endif
  56848. #endif
  56849. #ifndef NO_RSA
  56850. testCertFile = svrCertFile;
  56851. testKeyFile = svrKeyFile;
  56852. #elif defined(HAVE_ECC)
  56853. testCertFile = eccCertFile;
  56854. testKeyFile = eccKeyFile;
  56855. #else
  56856. testCertFile = NULL;
  56857. testKeyFile = NULL;
  56858. #endif
  56859. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  56860. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  56861. SSL_FILETYPE_PEM));
  56862. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  56863. SSL_FILETYPE_PEM));
  56864. }
  56865. ExpectNotNull(ssl = SSL_new(ctx));
  56866. ExpectIntEQ(SSL_in_init(ssl), 1);
  56867. SSL_CTX_free(ctx);
  56868. SSL_free(ssl);
  56869. #endif
  56870. return EXPECT_RESULT();
  56871. }
  56872. static int test_wolfSSL_CTX_set_timeout(void)
  56873. {
  56874. EXPECT_DECLS;
  56875. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  56876. int timeout;
  56877. WOLFSSL_CTX* ctx = NULL;
  56878. (void)timeout;
  56879. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56880. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  56881. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  56882. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  56883. */
  56884. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  56885. /* giving 0 as timeout value sets default timeout */
  56886. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  56887. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  56888. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  56889. #else
  56890. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  56891. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  56892. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  56893. #endif
  56894. wolfSSL_CTX_free(ctx);
  56895. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  56896. return EXPECT_RESULT();
  56897. }
  56898. static int test_wolfSSL_OpenSSL_version(void)
  56899. {
  56900. EXPECT_DECLS;
  56901. #if defined(OPENSSL_EXTRA)
  56902. const char* ver;
  56903. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  56904. ExpectNotNull(ver = OpenSSL_version(0));
  56905. #else
  56906. ExpectNotNull(ver = OpenSSL_version());
  56907. #endif
  56908. ExpectIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  56909. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  56910. #endif
  56911. return EXPECT_RESULT();
  56912. }
  56913. static int test_CONF_CTX_CMDLINE(void)
  56914. {
  56915. EXPECT_DECLS;
  56916. #if defined(OPENSSL_ALL)
  56917. SSL_CTX* ctx = NULL;
  56918. SSL_CONF_CTX* cctx = NULL;
  56919. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  56920. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56921. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  56922. /* set flags */
  56923. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  56924. WOLFSSL_CONF_FLAG_CMDLINE);
  56925. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  56926. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  56927. /* cmd invalid command */
  56928. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  56929. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  56930. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  56931. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  56932. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  56933. /* cmd Certificate and Private Key*/
  56934. {
  56935. #if !defined(NO_CERTS) && !defined(NO_RSA)
  56936. const char* ourCert = svrCertFile;
  56937. const char* ourKey = svrKeyFile;
  56938. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  56939. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert), WOLFSSL_SUCCESS);
  56940. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  56941. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  56942. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  56943. #endif
  56944. }
  56945. /* cmd curves */
  56946. {
  56947. #if defined(HAVE_ECC)
  56948. const char* curve = "secp256r1";
  56949. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  56950. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  56951. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  56952. #endif
  56953. }
  56954. /* cmd CipherString */
  56955. {
  56956. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  56957. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  56958. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  56959. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  56960. }
  56961. /* cmd DH parameter */
  56962. {
  56963. #if !defined(NO_DH) && !defined(NO_BIO)
  56964. const char* ourdhcert = "./certs/dh2048.pem";
  56965. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL), -3);
  56966. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert), WOLFSSL_SUCCESS);
  56967. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  56968. #endif
  56969. }
  56970. SSL_CTX_free(ctx);
  56971. SSL_CONF_CTX_free(cctx);
  56972. #endif /* OPENSSL_EXTRA */
  56973. return EXPECT_RESULT();
  56974. }
  56975. static int test_CONF_CTX_FILE(void)
  56976. {
  56977. EXPECT_DECLS;
  56978. #if defined(OPENSSL_ALL)
  56979. SSL_CTX* ctx = NULL;
  56980. SSL_CONF_CTX* cctx = NULL;
  56981. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  56982. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56983. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  56984. /* set flags */
  56985. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  56986. WOLFSSL_CONF_FLAG_FILE);
  56987. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  56988. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  56989. /* sanity check */
  56990. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  56991. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  56992. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  56993. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  56994. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  56995. /* cmd Certificate and Private Key*/
  56996. {
  56997. #if !defined(NO_CERTS) && !defined(NO_RSA)
  56998. const char* ourCert = svrCertFile;
  56999. const char* ourKey = svrKeyFile;
  57000. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  57001. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  57002. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  57003. WOLFSSL_SUCCESS);
  57004. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  57005. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57006. #endif
  57007. }
  57008. /* cmd curves */
  57009. {
  57010. #if defined(HAVE_ECC)
  57011. const char* curve = "secp256r1";
  57012. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  57013. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  57014. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57015. #endif
  57016. }
  57017. /* cmd CipherString */
  57018. {
  57019. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  57020. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  57021. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher),
  57022. WOLFSSL_SUCCESS);
  57023. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57024. }
  57025. /* cmd DH parameter */
  57026. {
  57027. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  57028. const char* ourdhcert = "./certs/dh3072.pem";
  57029. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  57030. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  57031. WOLFSSL_SUCCESS);
  57032. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57033. #endif
  57034. }
  57035. SSL_CTX_free(ctx);
  57036. SSL_CONF_CTX_free(cctx);
  57037. #endif /* OPENSSL_EXTRA */
  57038. return EXPECT_RESULT();
  57039. }
  57040. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  57041. {
  57042. EXPECT_DECLS;
  57043. #ifdef HAVE_EX_DATA
  57044. int idx1, idx2;
  57045. /* test for unsupported class index */
  57046. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  57047. 0,NULL, NULL, NULL, NULL ), -1);
  57048. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(
  57049. WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  57050. 0,NULL, NULL, NULL, NULL ), -1);
  57051. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  57052. 0,NULL, NULL, NULL, NULL ), -1);
  57053. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  57054. 0,NULL, NULL, NULL, NULL ), -1);
  57055. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  57056. 0,NULL, NULL, NULL, NULL ), -1);
  57057. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  57058. 0,NULL, NULL, NULL, NULL ), -1);
  57059. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  57060. 0,NULL, NULL, NULL, NULL ), -1);
  57061. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  57062. 0,NULL, NULL, NULL, NULL ), -1);
  57063. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  57064. 0,NULL, NULL, NULL, NULL ), -1);
  57065. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  57066. 0,NULL, NULL, NULL, NULL ), -1);
  57067. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  57068. 0,NULL, NULL, NULL, NULL ), -1);
  57069. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  57070. 0,NULL, NULL, NULL, NULL ), -1);
  57071. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20,
  57072. 0,NULL, NULL, NULL, NULL ), -1);
  57073. /* test for supported class index */
  57074. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  57075. 0,NULL, NULL, NULL, NULL );
  57076. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  57077. 0,NULL, NULL, NULL, NULL );
  57078. ExpectIntNE(idx1, -1);
  57079. ExpectIntNE(idx2, -1);
  57080. ExpectIntNE(idx1, idx2);
  57081. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  57082. 0,NULL, NULL, NULL, NULL );
  57083. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  57084. 0,NULL, NULL, NULL, NULL );
  57085. ExpectIntNE(idx1, -1);
  57086. ExpectIntNE(idx2, -1);
  57087. ExpectIntNE(idx1, idx2);
  57088. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  57089. 0,NULL, NULL, NULL, NULL );
  57090. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  57091. 0,NULL, NULL, NULL, NULL );
  57092. ExpectIntNE(idx1, -1);
  57093. ExpectIntNE(idx2, -1);
  57094. ExpectIntNE(idx1, idx2);
  57095. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  57096. 0,NULL, NULL, NULL, NULL );
  57097. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  57098. 0,NULL, NULL, NULL, NULL );
  57099. ExpectIntNE(idx1, -1);
  57100. ExpectIntNE(idx2, -1);
  57101. ExpectIntNE(idx1, idx2);
  57102. #endif /* HAVE_EX_DATA */
  57103. return EXPECT_RESULT();
  57104. }
  57105. #if defined(HAVE_EX_DATA) && defined(HAVE_EXT_CACHE) && \
  57106. (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  57107. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  57108. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  57109. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))))
  57110. #define SESSION_NEW_IDX_LONG 0xDEADBEEF
  57111. #define SESSION_NEW_IDX_VAL ((void*)0xAEADAEAD)
  57112. #define SESSION_DUP_IDX_VAL ((void*)0xDEDEDEDE)
  57113. #define SESSION_NEW_IDX_PTR "Testing"
  57114. static void test_wolfSSL_SESSION_get_ex_new_index_new_cb(void* p, void* ptr,
  57115. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  57116. {
  57117. AssertNotNull(p);
  57118. AssertNull(ptr);
  57119. AssertIntEQ(CRYPTO_set_ex_data(a, idx, SESSION_NEW_IDX_VAL), SSL_SUCCESS);
  57120. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  57121. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  57122. }
  57123. static int test_wolfSSL_SESSION_get_ex_new_index_dup_cb(CRYPTO_EX_DATA* out,
  57124. const CRYPTO_EX_DATA* in, void* inPtr, int idx, long argV,
  57125. void* arg)
  57126. {
  57127. EXPECT_DECLS;
  57128. ExpectNotNull(out);
  57129. ExpectNotNull(in);
  57130. ExpectPtrEq(*(void**)inPtr, SESSION_NEW_IDX_VAL);
  57131. ExpectPtrEq(CRYPTO_get_ex_data(in, idx), SESSION_NEW_IDX_VAL);
  57132. ExpectPtrEq(CRYPTO_get_ex_data(out, idx), SESSION_NEW_IDX_VAL);
  57133. ExpectIntEQ(argV, SESSION_NEW_IDX_LONG);
  57134. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  57135. *(void**)inPtr = SESSION_DUP_IDX_VAL;
  57136. if (EXPECT_SUCCESS()) {
  57137. return SSL_SUCCESS;
  57138. }
  57139. else {
  57140. return SSL_FAILURE;
  57141. }
  57142. }
  57143. static int test_wolfSSL_SESSION_get_ex_new_index_free_cb_called = 0;
  57144. static void test_wolfSSL_SESSION_get_ex_new_index_free_cb(void* p, void* ptr,
  57145. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  57146. {
  57147. EXPECT_DECLS;
  57148. ExpectNotNull(p);
  57149. ExpectNull(ptr);
  57150. ExpectPtrNE(CRYPTO_get_ex_data(a, idx), 0);
  57151. ExpectIntEQ(argValue, SESSION_NEW_IDX_LONG);
  57152. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  57153. if (EXPECT_SUCCESS()) {
  57154. test_wolfSSL_SESSION_get_ex_new_index_free_cb_called++;
  57155. }
  57156. }
  57157. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  57158. {
  57159. EXPECT_DECLS;
  57160. int idx = SSL_SESSION_get_ex_new_index(SESSION_NEW_IDX_LONG,
  57161. (void*)SESSION_NEW_IDX_PTR,
  57162. test_wolfSSL_SESSION_get_ex_new_index_new_cb,
  57163. test_wolfSSL_SESSION_get_ex_new_index_dup_cb,
  57164. test_wolfSSL_SESSION_get_ex_new_index_free_cb);
  57165. SSL_SESSION* s = SSL_SESSION_new();
  57166. SSL_SESSION* d = NULL;
  57167. ExpectNotNull(s);
  57168. ExpectPtrEq(SSL_SESSION_get_ex_data(s, idx), SESSION_NEW_IDX_VAL);
  57169. ExpectNotNull(d = SSL_SESSION_dup(s));
  57170. ExpectPtrEq(SSL_SESSION_get_ex_data(d, idx), SESSION_DUP_IDX_VAL);
  57171. SSL_SESSION_free(s);
  57172. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 1);
  57173. SSL_SESSION_free(d);
  57174. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 2);
  57175. crypto_ex_cb_free(crypto_ex_cb_ctx_session);
  57176. crypto_ex_cb_ctx_session = NULL;
  57177. return EXPECT_RESULT();
  57178. }
  57179. #else
  57180. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  57181. {
  57182. return TEST_SKIPPED;
  57183. }
  57184. #endif
  57185. static int test_wolfSSL_set_psk_use_session_callback(void)
  57186. {
  57187. EXPECT_DECLS;
  57188. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  57189. SSL_CTX* ctx = NULL;
  57190. SSL* ssl = NULL;
  57191. const char* testCertFile;
  57192. const char* testKeyFile;
  57193. #ifdef WOLFSSL_TLS13
  57194. #ifdef NO_WOLFSSL_SERVER
  57195. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  57196. #else
  57197. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  57198. #endif
  57199. #else
  57200. #ifdef NO_WOLFSSL_SERVER
  57201. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  57202. #else
  57203. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  57204. #endif
  57205. #endif
  57206. #ifndef NO_RSA
  57207. testCertFile = svrCertFile;
  57208. testKeyFile = svrKeyFile;
  57209. #elif defined(HAVE_ECC)
  57210. testCertFile = eccCertFile;
  57211. testKeyFile = eccKeyFile;
  57212. #else
  57213. testCertFile = NULL;
  57214. testKeyFile = NULL;
  57215. #endif
  57216. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  57217. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  57218. SSL_FILETYPE_PEM));
  57219. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  57220. SSL_FILETYPE_PEM));
  57221. }
  57222. ExpectNotNull(ssl = SSL_new(ctx));
  57223. SSL_set_psk_use_session_callback(ssl, my_psk_use_session_cb);
  57224. SSL_CTX_free(ctx);
  57225. SSL_free(ssl);
  57226. #endif
  57227. return EXPECT_RESULT();
  57228. }
  57229. static int test_wolfSSL_ERR_strings(void)
  57230. {
  57231. EXPECT_DECLS;
  57232. #if !defined(NO_ERROR_STRINGS)
  57233. const char* err1 = "unsupported cipher suite";
  57234. const char* err2 = "wolfSSL PEM routines";
  57235. const char* err = NULL;
  57236. (void)err;
  57237. (void)err1;
  57238. (void)err2;
  57239. #if defined(OPENSSL_EXTRA)
  57240. ExpectNotNull(err = ERR_reason_error_string(UNSUPPORTED_SUITE));
  57241. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  57242. ExpectNotNull(err = ERR_func_error_string(UNSUPPORTED_SUITE));
  57243. ExpectIntEQ((*err == '\0'), 1);
  57244. ExpectNotNull(err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD));
  57245. ExpectIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  57246. #else
  57247. ExpectNotNull(err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE));
  57248. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  57249. ExpectNotNull(err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE));
  57250. ExpectIntEQ((*err == '\0'), 1);
  57251. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  57252. ExpectNotNull(err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2));
  57253. ExpectIntEQ((*err == '\0'), 1);
  57254. #endif
  57255. #endif
  57256. return EXPECT_RESULT();
  57257. }
  57258. static int test_wolfSSL_EVP_shake128(void)
  57259. {
  57260. EXPECT_DECLS;
  57261. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  57262. defined(WOLFSSL_SHAKE128)
  57263. const EVP_MD* md = NULL;
  57264. ExpectNotNull(md = EVP_shake128());
  57265. ExpectIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  57266. #endif
  57267. return EXPECT_RESULT();
  57268. }
  57269. static int test_wolfSSL_EVP_shake256(void)
  57270. {
  57271. EXPECT_DECLS;
  57272. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  57273. defined(WOLFSSL_SHAKE256)
  57274. const EVP_MD* md = NULL;
  57275. ExpectNotNull(md = EVP_shake256());
  57276. ExpectIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  57277. #endif
  57278. return EXPECT_RESULT();
  57279. }
  57280. /*
  57281. * Testing EVP digest API with SM3
  57282. */
  57283. static int test_wolfSSL_EVP_sm3(void)
  57284. {
  57285. int res = TEST_SKIPPED;
  57286. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM3)
  57287. EXPECT_DECLS;
  57288. const EVP_MD* md = NULL;
  57289. EVP_MD_CTX* mdCtx = NULL;
  57290. byte data[WC_SM3_BLOCK_SIZE * 4];
  57291. byte hash[WC_SM3_DIGEST_SIZE];
  57292. byte calcHash[WC_SM3_DIGEST_SIZE];
  57293. byte expHash[WC_SM3_DIGEST_SIZE] = {
  57294. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  57295. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  57296. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  57297. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  57298. };
  57299. word32 chunk;
  57300. word32 i;
  57301. unsigned int sz;
  57302. int ret;
  57303. XMEMSET(data, 0, sizeof(data));
  57304. md = EVP_sm3();
  57305. ExpectTrue(md != NULL);
  57306. ExpectIntEQ(XSTRNCMP(md, "SM3", XSTRLEN("SM3")), 0);
  57307. mdCtx = EVP_MD_CTX_new();
  57308. ExpectTrue(mdCtx != NULL);
  57309. /* Invalid Parameters */
  57310. ExpectIntEQ(EVP_DigestInit(NULL, md), BAD_FUNC_ARG);
  57311. /* Valid Parameters */
  57312. ExpectIntEQ(EVP_DigestInit(mdCtx, md), WOLFSSL_SUCCESS);
  57313. ExpectIntEQ(EVP_DigestUpdate(NULL, NULL, 1), WOLFSSL_FAILURE);
  57314. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 1), WOLFSSL_FAILURE);
  57315. ExpectIntEQ(EVP_DigestUpdate(NULL, data, 1), WOLFSSL_FAILURE);
  57316. /* Valid Parameters */
  57317. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 0), WOLFSSL_SUCCESS);
  57318. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  57319. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  57320. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE),
  57321. WOLFSSL_SUCCESS);
  57322. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE - 2),
  57323. WOLFSSL_SUCCESS);
  57324. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE * 2),
  57325. WOLFSSL_SUCCESS);
  57326. /* Ensure too many bytes for lengths. */
  57327. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_PAD_SIZE),
  57328. WOLFSSL_SUCCESS);
  57329. /* Invalid Parameters */
  57330. ExpectIntEQ(EVP_DigestFinal(NULL, NULL, NULL), WOLFSSL_FAILURE);
  57331. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  57332. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  57333. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  57334. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  57335. /* Valid Parameters */
  57336. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  57337. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  57338. /* Chunk tests. */
  57339. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, sizeof(data)), WOLFSSL_SUCCESS);
  57340. ExpectIntEQ(EVP_DigestFinal(mdCtx, calcHash, &sz), WOLFSSL_SUCCESS);
  57341. ExpectIntEQ(sz, WC_SM3_DIGEST_SIZE);
  57342. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  57343. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  57344. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i, chunk),
  57345. WOLFSSL_SUCCESS);
  57346. }
  57347. if (i < (word32)sizeof(data)) {
  57348. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i,
  57349. (word32)sizeof(data) - i), WOLFSSL_SUCCESS);
  57350. }
  57351. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  57352. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  57353. }
  57354. /* Not testing when the low 32-bit length overflows. */
  57355. ret = EVP_MD_CTX_cleanup(mdCtx);
  57356. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  57357. wolfSSL_EVP_MD_CTX_free(mdCtx);
  57358. res = EXPECT_RESULT();
  57359. #endif
  57360. return res;
  57361. } /* END test_EVP_sm3 */
  57362. static int test_EVP_blake2(void)
  57363. {
  57364. EXPECT_DECLS;
  57365. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  57366. const EVP_MD* md = NULL;
  57367. (void)md;
  57368. #if defined(HAVE_BLAKE2)
  57369. ExpectNotNull(md = EVP_blake2b512());
  57370. ExpectIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  57371. #endif
  57372. #if defined(HAVE_BLAKE2S)
  57373. ExpectNotNull(md = EVP_blake2s256());
  57374. ExpectIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  57375. #endif
  57376. #endif
  57377. return EXPECT_RESULT();
  57378. }
  57379. #if defined(OPENSSL_EXTRA)
  57380. static void list_md_fn(const EVP_MD* m, const char* from,
  57381. const char* to, void* arg)
  57382. {
  57383. const char* mn;
  57384. BIO *bio;
  57385. (void) from;
  57386. (void) to;
  57387. (void) arg;
  57388. (void) mn;
  57389. (void) bio;
  57390. if (!m) {
  57391. /* alias */
  57392. AssertNull(m);
  57393. AssertNotNull(to);
  57394. }
  57395. else {
  57396. AssertNotNull(m);
  57397. AssertNull(to);
  57398. }
  57399. AssertNotNull(from);
  57400. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  57401. mn = EVP_get_digestbyname(from);
  57402. /* print to stderr */
  57403. AssertNotNull(arg);
  57404. bio = BIO_new(BIO_s_file());
  57405. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  57406. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  57407. BIO_free(bio);
  57408. #endif
  57409. }
  57410. #endif
  57411. static int test_EVP_MD_do_all(void)
  57412. {
  57413. int res = TEST_SKIPPED;
  57414. #if defined(OPENSSL_EXTRA)
  57415. EVP_MD_do_all(NULL, stderr);
  57416. EVP_MD_do_all(list_md_fn, stderr);
  57417. res = TEST_SUCCESS;
  57418. #endif
  57419. return res;
  57420. }
  57421. #if defined(OPENSSL_EXTRA)
  57422. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  57423. {
  57424. (void)arg;
  57425. (void)nm;
  57426. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  57427. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  57428. /* print to stderr */
  57429. AssertNotNull(arg);
  57430. BIO *bio = BIO_new(BIO_s_file());
  57431. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  57432. BIO_printf(bio, "%s\n", nm);
  57433. BIO_free(bio);
  57434. #endif
  57435. }
  57436. #endif
  57437. static int test_OBJ_NAME_do_all(void)
  57438. {
  57439. int res = TEST_SKIPPED;
  57440. #if defined(OPENSSL_EXTRA)
  57441. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  57442. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
  57443. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
  57444. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
  57445. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
  57446. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
  57447. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
  57448. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
  57449. OBJ_NAME_do_all(-1, obj_name_t, stderr);
  57450. res = TEST_SUCCESS;
  57451. #endif
  57452. return res;
  57453. }
  57454. static int test_SSL_CIPHER_get_xxx(void)
  57455. {
  57456. EXPECT_DECLS;
  57457. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  57458. !defined(NO_FILESYSTEM)
  57459. const SSL_CIPHER* cipher = NULL;
  57460. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  57461. int i, numCiphers = 0;
  57462. SSL_CTX* ctx = NULL;
  57463. SSL* ssl = NULL;
  57464. const char* testCertFile;
  57465. const char* testKeyFile;
  57466. char buf[256] = {0};
  57467. const char* cipher_id = NULL;
  57468. int expect_nid1 = NID_undef;
  57469. int expect_nid2 = NID_undef;
  57470. int expect_nid3 = NID_undef;
  57471. int expect_nid4 = NID_undef;
  57472. int expect_nid5 = 0;
  57473. const char* cipher_id2 = NULL;
  57474. int expect_nid21 = NID_undef;
  57475. int expect_nid22 = NID_undef;
  57476. int expect_nid23 = NID_undef;
  57477. int expect_nid24 = NID_undef;
  57478. int expect_nid25 = 0;
  57479. (void)cipher;
  57480. (void)supportedCiphers;
  57481. (void)i;
  57482. (void)numCiphers;
  57483. (void)ctx;
  57484. (void)ssl;
  57485. (void)testCertFile;
  57486. (void)testKeyFile;
  57487. #if defined(WOLFSSL_TLS13)
  57488. cipher_id = "TLS13-AES128-GCM-SHA256";
  57489. expect_nid1 = NID_auth_rsa;
  57490. expect_nid2 = NID_aes_128_gcm;
  57491. expect_nid3 = NID_sha256;
  57492. expect_nid4 = NID_kx_any;
  57493. expect_nid5 = 1;
  57494. #if !defined(WOLFSSL_NO_TLS12)
  57495. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  57496. expect_nid21 = NID_auth_rsa;
  57497. expect_nid22 = NID_aes_256_gcm;
  57498. expect_nid23 = NID_sha384;
  57499. expect_nid24 = NID_kx_ecdhe;
  57500. expect_nid25 = 1;
  57501. #endif
  57502. #endif
  57503. #ifdef NO_WOLFSSL_SERVER
  57504. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  57505. #else
  57506. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  57507. #endif
  57508. if (cipher_id) {
  57509. #ifndef NO_RSA
  57510. testCertFile = svrCertFile;
  57511. testKeyFile = svrKeyFile;
  57512. #elif defined(HAVE_ECC)
  57513. testCertFile = eccCertFile;
  57514. testKeyFile = eccKeyFile;
  57515. #else
  57516. testCertFile = NULL;
  57517. testKeyFile = NULL;
  57518. #endif
  57519. if (testCertFile != NULL && testKeyFile != NULL) {
  57520. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  57521. SSL_FILETYPE_PEM));
  57522. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  57523. SSL_FILETYPE_PEM));
  57524. }
  57525. ExpectNotNull(ssl = SSL_new(ctx));
  57526. ExpectIntEQ(SSL_in_init(ssl), 1);
  57527. supportedCiphers = SSL_get_ciphers(ssl);
  57528. numCiphers = sk_num(supportedCiphers);
  57529. for (i = 0; i < numCiphers; ++i) {
  57530. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  57531. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  57532. }
  57533. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  57534. break;
  57535. }
  57536. }
  57537. /* test case for */
  57538. if (i != numCiphers) {
  57539. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  57540. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  57541. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  57542. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  57543. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  57544. }
  57545. if (cipher_id2) {
  57546. for (i = 0; i < numCiphers; ++i) {
  57547. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  57548. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  57549. }
  57550. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  57551. break;
  57552. }
  57553. }
  57554. /* test case for */
  57555. if (i != numCiphers) {
  57556. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  57557. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  57558. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  57559. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  57560. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  57561. }
  57562. }
  57563. }
  57564. SSL_CTX_free(ctx);
  57565. SSL_free(ssl);
  57566. #endif
  57567. return EXPECT_RESULT();
  57568. }
  57569. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  57570. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  57571. int* keyFormat)
  57572. {
  57573. int ret;
  57574. byte* key_buf = NULL;
  57575. size_t key_sz = 0;
  57576. EncryptedInfo encInfo;
  57577. XMEMSET(&encInfo, 0, sizeof(encInfo));
  57578. ret = load_file(privKeyFile, &key_buf, &key_sz);
  57579. if (ret == 0) {
  57580. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  57581. NULL, &encInfo, keyFormat);
  57582. }
  57583. if (key_buf != NULL) {
  57584. free(key_buf); key_buf = NULL;
  57585. }
  57586. (void)encInfo; /* not used in this test */
  57587. #ifdef DEBUG_WOLFSSL
  57588. fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  57589. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  57590. (*pDer)->length);
  57591. #endif
  57592. return ret;
  57593. }
  57594. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  57595. {
  57596. int ret = CRYPTOCB_UNAVAILABLE;
  57597. const char* privKeyFile = (const char*)ctx;
  57598. DerBuffer* pDer = NULL;
  57599. int keyFormat = 0;
  57600. if (info->algo_type == WC_ALGO_TYPE_PK) {
  57601. #ifdef DEBUG_WOLFSSL
  57602. fprintf(stderr, "test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  57603. #endif
  57604. #ifndef NO_RSA
  57605. if (info->pk.type == WC_PK_TYPE_RSA) {
  57606. switch (info->pk.rsa.type) {
  57607. case RSA_PUBLIC_ENCRYPT:
  57608. case RSA_PUBLIC_DECRYPT:
  57609. /* perform software based RSA public op */
  57610. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  57611. break;
  57612. case RSA_PRIVATE_ENCRYPT:
  57613. case RSA_PRIVATE_DECRYPT:
  57614. {
  57615. RsaKey key;
  57616. /* perform software based RSA private op */
  57617. #ifdef DEBUG_WOLFSSL
  57618. fprintf(stderr, "test_CryptoCb_Func: RSA Priv\n");
  57619. #endif
  57620. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  57621. &keyFormat);
  57622. if (ret != 0) {
  57623. return ret;
  57624. }
  57625. ret = wc_InitRsaKey(&key, HEAP_HINT);
  57626. if (ret == 0) {
  57627. word32 keyIdx = 0;
  57628. /* load RSA private key and perform private transform */
  57629. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  57630. &key, pDer->length);
  57631. if (ret == 0) {
  57632. ret = wc_RsaFunction(
  57633. info->pk.rsa.in, info->pk.rsa.inLen,
  57634. info->pk.rsa.out, info->pk.rsa.outLen,
  57635. info->pk.rsa.type, &key, info->pk.rsa.rng);
  57636. }
  57637. else {
  57638. /* if decode fails, then fall-back to software based crypto */
  57639. fprintf(stderr, "test_CryptoCb_Func: RSA private "
  57640. "key decode failed %d, falling back to "
  57641. "software\n", ret);
  57642. ret = CRYPTOCB_UNAVAILABLE;
  57643. }
  57644. wc_FreeRsaKey(&key);
  57645. }
  57646. wc_FreeDer(&pDer); pDer = NULL;
  57647. break;
  57648. }
  57649. }
  57650. #ifdef DEBUG_WOLFSSL
  57651. fprintf(stderr, "test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  57652. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  57653. #endif
  57654. }
  57655. #endif /* !NO_RSA */
  57656. #ifdef HAVE_ECC
  57657. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  57658. /* mark this key as ephemeral */
  57659. if (info->pk.eckg.key != NULL) {
  57660. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  57661. sizeof(info->pk.eckg.key->label));
  57662. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  57663. }
  57664. }
  57665. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  57666. ecc_key key;
  57667. /* perform software based ECC sign */
  57668. #ifdef DEBUG_WOLFSSL
  57669. fprintf(stderr, "test_CryptoCb_Func: ECC Sign\n");
  57670. #endif
  57671. if (info->pk.eccsign.key != NULL &&
  57672. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  57673. /* this is an empheral key */
  57674. #ifdef DEBUG_WOLFSSL
  57675. fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
  57676. "ephemeral key\n");
  57677. #endif
  57678. return CRYPTOCB_UNAVAILABLE;
  57679. }
  57680. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  57681. if (ret != 0) {
  57682. return ret;
  57683. }
  57684. ret = wc_ecc_init(&key);
  57685. if (ret == 0) {
  57686. word32 keyIdx = 0;
  57687. /* load ECC private key and perform private transform */
  57688. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  57689. &key, pDer->length);
  57690. if (ret == 0) {
  57691. ret = wc_ecc_sign_hash(
  57692. info->pk.eccsign.in, info->pk.eccsign.inlen,
  57693. info->pk.eccsign.out, info->pk.eccsign.outlen,
  57694. info->pk.eccsign.rng, &key);
  57695. }
  57696. else {
  57697. /* if decode fails, then fall-back to software based crypto */
  57698. fprintf(stderr, "test_CryptoCb_Func: ECC private key "
  57699. "decode failed %d, falling back to software\n", ret);
  57700. ret = CRYPTOCB_UNAVAILABLE;
  57701. }
  57702. wc_ecc_free(&key);
  57703. }
  57704. wc_FreeDer(&pDer); pDer = NULL;
  57705. #ifdef DEBUG_WOLFSSL
  57706. fprintf(stderr, "test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  57707. ret, *info->pk.eccsign.outlen);
  57708. #endif
  57709. }
  57710. #endif /* HAVE_ECC */
  57711. #ifdef HAVE_ED25519
  57712. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  57713. ed25519_key key;
  57714. /* perform software based ED25519 sign */
  57715. #ifdef DEBUG_WOLFSSL
  57716. fprintf(stderr, "test_CryptoCb_Func: ED25519 Sign\n");
  57717. #endif
  57718. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  57719. if (ret != 0) {
  57720. return ret;
  57721. }
  57722. ret = wc_ed25519_init(&key);
  57723. if (ret == 0) {
  57724. word32 keyIdx = 0;
  57725. /* load ED25519 private key and perform private transform */
  57726. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  57727. &key, pDer->length);
  57728. if (ret == 0) {
  57729. /* calculate public key */
  57730. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  57731. if (ret == 0) {
  57732. key.pubKeySet = 1;
  57733. ret = wc_ed25519_sign_msg_ex(
  57734. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  57735. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  57736. &key, info->pk.ed25519sign.type,
  57737. info->pk.ed25519sign.context,
  57738. info->pk.ed25519sign.contextLen);
  57739. }
  57740. }
  57741. else {
  57742. /* if decode fails, then fall-back to software based crypto */
  57743. fprintf(stderr, "test_CryptoCb_Func: ED25519 private key "
  57744. "decode failed %d, falling back to software\n", ret);
  57745. ret = CRYPTOCB_UNAVAILABLE;
  57746. }
  57747. wc_ed25519_free(&key);
  57748. }
  57749. wc_FreeDer(&pDer); pDer = NULL;
  57750. #ifdef DEBUG_WOLFSSL
  57751. fprintf(stderr, "test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  57752. ret, *info->pk.ed25519sign.outLen);
  57753. #endif
  57754. }
  57755. #endif /* HAVE_ED25519 */
  57756. }
  57757. (void)thisDevId;
  57758. (void)keyFormat;
  57759. return ret;
  57760. }
  57761. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  57762. static int test_wc_CryptoCb_TLS(int tlsVer,
  57763. const char* cliCaPemFile, const char* cliCertPemFile,
  57764. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  57765. const char* svrCaPemFile, const char* svrCertPemFile,
  57766. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  57767. {
  57768. EXPECT_DECLS;
  57769. callback_functions client_cbf;
  57770. callback_functions server_cbf;
  57771. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  57772. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  57773. if (tlsVer == WOLFSSL_TLSV1_3) {
  57774. #ifdef WOLFSSL_TLS13
  57775. server_cbf.method = wolfTLSv1_3_server_method;
  57776. client_cbf.method = wolfTLSv1_3_client_method;
  57777. #endif
  57778. }
  57779. else if (tlsVer == WOLFSSL_TLSV1_2) {
  57780. #ifndef WOLFSSL_NO_TLS12
  57781. server_cbf.method = wolfTLSv1_2_server_method;
  57782. client_cbf.method = wolfTLSv1_2_client_method;
  57783. #endif
  57784. }
  57785. else if (tlsVer == WOLFSSL_TLSV1_1) {
  57786. #ifndef NO_OLD_TLS
  57787. server_cbf.method = wolfTLSv1_1_server_method;
  57788. client_cbf.method = wolfTLSv1_1_client_method;
  57789. #endif
  57790. }
  57791. else if (tlsVer == WOLFSSL_TLSV1) {
  57792. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  57793. server_cbf.method = wolfTLSv1_server_method;
  57794. client_cbf.method = wolfTLSv1_client_method;
  57795. #endif
  57796. }
  57797. else if (tlsVer == WOLFSSL_SSLV3) {
  57798. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  57799. defined(WOLFSSL_STATIC_RSA)
  57800. server_cbf.method = wolfSSLv3_server_method;
  57801. client_cbf.method = wolfSSLv3_client_method;
  57802. #endif
  57803. }
  57804. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  57805. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  57806. server_cbf.method = wolfDTLSv1_2_server_method;
  57807. client_cbf.method = wolfDTLSv1_2_client_method;
  57808. #endif
  57809. }
  57810. else if (tlsVer == WOLFSSL_DTLSV1) {
  57811. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  57812. server_cbf.method = wolfDTLSv1_server_method;
  57813. client_cbf.method = wolfDTLSv1_client_method;
  57814. #endif
  57815. }
  57816. if (server_cbf.method == NULL) {
  57817. /* not enabled */
  57818. return TEST_SUCCESS;
  57819. }
  57820. /* Setup the keys for the TLS test */
  57821. client_cbf.certPemFile = cliCertPemFile;
  57822. client_cbf.keyPemFile = cliPubKeyPemFile;
  57823. client_cbf.caPemFile = cliCaPemFile;
  57824. server_cbf.certPemFile = svrCertPemFile;
  57825. server_cbf.keyPemFile = svrPubKeyPemFile;
  57826. server_cbf.caPemFile = svrCaPemFile;
  57827. /* Setup a crypto callback with pointer to private key file for testing */
  57828. client_cbf.devId = 1;
  57829. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  57830. (void*)cliPrivKeyPemFile);
  57831. server_cbf.devId = 2;
  57832. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  57833. (void*)svrPrivKeyPemFile);
  57834. /* Perform TLS server and client test */
  57835. /* First test is at WOLFSSL_CTX level */
  57836. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  57837. /* Check for success */
  57838. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  57839. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  57840. if (EXPECT_SUCCESS()) {
  57841. /* Second test is a WOLFSSL object level */
  57842. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  57843. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  57844. }
  57845. /* Check for success */
  57846. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  57847. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  57848. /* Un register the devId's */
  57849. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  57850. client_cbf.devId = INVALID_DEVID;
  57851. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  57852. server_cbf.devId = INVALID_DEVID;
  57853. return EXPECT_RESULT();
  57854. }
  57855. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  57856. static int test_wc_CryptoCb(void)
  57857. {
  57858. EXPECT_DECLS;
  57859. #ifdef WOLF_CRYPTO_CB
  57860. /* TODO: Add crypto callback API tests */
  57861. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  57862. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  57863. int tlsVer;
  57864. #endif
  57865. #ifndef NO_RSA
  57866. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  57867. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  57868. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  57869. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile),
  57870. TEST_SUCCESS);
  57871. }
  57872. #endif
  57873. #ifdef HAVE_ECC
  57874. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  57875. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  57876. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  57877. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile),
  57878. TEST_SUCCESS);
  57879. }
  57880. #endif
  57881. #ifdef HAVE_ED25519
  57882. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  57883. if (tlsVer == WOLFSSL_DTLSV1) continue;
  57884. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  57885. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  57886. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile),
  57887. TEST_SUCCESS);
  57888. }
  57889. #endif
  57890. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  57891. #endif /* WOLF_CRYPTO_CB */
  57892. return EXPECT_RESULT();
  57893. }
  57894. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  57895. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  57896. static int test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  57897. const char* cliCaPemFile, const char* cliCertPemFile,
  57898. const char* cliPrivKeyPemFile,
  57899. const char* svrCaPemFile, const char* svrCertPemFile,
  57900. const char* svrPrivKeyPemFile,
  57901. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  57902. {
  57903. EXPECT_DECLS;
  57904. callback_functions client_cbf;
  57905. callback_functions server_cbf;
  57906. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  57907. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  57908. if (tlsVer == WOLFSSL_TLSV1_3) {
  57909. #ifdef WOLFSSL_TLS13
  57910. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  57911. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  57912. #endif
  57913. }
  57914. else if (tlsVer == WOLFSSL_TLSV1_2) {
  57915. #ifndef WOLFSSL_NO_TLS12
  57916. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  57917. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  57918. #endif
  57919. }
  57920. else if (tlsVer == WOLFSSL_TLSV1_1) {
  57921. #ifndef NO_OLD_TLS
  57922. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  57923. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  57924. #endif
  57925. }
  57926. else if (tlsVer == WOLFSSL_TLSV1) {
  57927. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  57928. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  57929. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  57930. #endif
  57931. }
  57932. else if (tlsVer == WOLFSSL_SSLV3) {
  57933. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  57934. defined(WOLFSSL_STATIC_RSA)
  57935. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  57936. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  57937. #endif
  57938. }
  57939. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  57940. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  57941. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  57942. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  57943. #endif
  57944. }
  57945. else if (tlsVer == WOLFSSL_DTLSV1) {
  57946. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  57947. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  57948. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  57949. #endif
  57950. }
  57951. if (server_cbf.method_ex == NULL) {
  57952. /* not enabled */
  57953. return TEST_SUCCESS;
  57954. }
  57955. /* Setup the keys for the TLS test */
  57956. client_cbf.certPemFile = cliCertPemFile;
  57957. client_cbf.keyPemFile = cliPrivKeyPemFile;
  57958. client_cbf.caPemFile = cliCaPemFile;
  57959. server_cbf.certPemFile = svrCertPemFile;
  57960. server_cbf.keyPemFile = svrPrivKeyPemFile;
  57961. server_cbf.caPemFile = svrCaPemFile;
  57962. client_cbf.mem = cliMem;
  57963. client_cbf.memSz = cliMemSz;
  57964. server_cbf.mem = svrMem;
  57965. server_cbf.memSz = svrMemSz;
  57966. client_cbf.devId = INVALID_DEVID;
  57967. server_cbf.devId = INVALID_DEVID;
  57968. /* Perform TLS server and client test */
  57969. /* First test is at WOLFSSL_CTX level */
  57970. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  57971. /* Check for success */
  57972. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  57973. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  57974. if (EXPECT_SUCCESS()) {
  57975. /* Second test is a WOLFSSL object level */
  57976. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  57977. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  57978. }
  57979. /* Check for success */
  57980. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  57981. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  57982. return EXPECT_RESULT();
  57983. }
  57984. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  57985. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  57986. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  57987. {
  57988. EXPECT_DECLS;
  57989. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  57990. WOLFSSL_MEM_STATS mem_stats;
  57991. WOLFSSL_MEM_CONN_STATS ssl_stats;
  57992. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  57993. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  57994. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  57995. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  57996. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  57997. #endif
  57998. ExpectNotNull((ssl1 = wolfSSL_new(ctx)));
  57999. ExpectNotNull((ssl2 = wolfSSL_new(ctx)));
  58000. /* this should fail because kMaxCtxClients == 2 */
  58001. ExpectNull((ssl3 = wolfSSL_new(ctx)));
  58002. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  58003. #ifdef DEBUG_WOLFSSL
  58004. wolfSSL_PrintStatsConn(&ssl_stats);
  58005. #endif
  58006. (void)ssl_stats;
  58007. }
  58008. /* display collected statistics */
  58009. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  58010. #ifdef DEBUG_WOLFSSL
  58011. wolfSSL_PrintStats(&mem_stats);
  58012. #endif
  58013. (void)mem_stats;
  58014. }
  58015. wolfSSL_free(ssl1);
  58016. wolfSSL_free(ssl2);
  58017. return EXPECT_RESULT();
  58018. }
  58019. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  58020. static int test_wolfSSL_CTX_StaticMemory(void)
  58021. {
  58022. EXPECT_DECLS;
  58023. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  58024. wolfSSL_method_func method_func;
  58025. WOLFSSL_CTX* ctx;
  58026. const int kMaxCtxClients = 2;
  58027. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  58028. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  58029. int tlsVer;
  58030. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  58031. #endif
  58032. #endif
  58033. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  58034. #ifndef NO_WOLFSSL_SERVER
  58035. #ifndef WOLFSSL_NO_TLS12
  58036. method_func = wolfTLSv1_2_server_method_ex;
  58037. #else
  58038. method_func = wolfTLSv1_3_server_method_ex;
  58039. #endif
  58040. #else
  58041. #ifndef WOLFSSL_NO_TLS12
  58042. method_func = wolfTLSv1_2_client_method_ex;
  58043. #else
  58044. method_func = wolfTLSv1_3_client_method_ex;
  58045. #endif
  58046. #endif
  58047. /* Test creating CTX directly from static memory pool */
  58048. ctx = NULL;
  58049. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, method_func, svrMem,
  58050. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  58051. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  58052. wolfSSL_CTX_free(ctx);
  58053. ctx = NULL;
  58054. /* Test for heap allocated CTX, then assigning static pool to it */
  58055. ExpectNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  58056. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, NULL, svrMem,
  58057. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  58058. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  58059. wolfSSL_CTX_free(ctx);
  58060. /* TLS Level Tests using static memory */
  58061. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  58062. #ifndef NO_RSA
  58063. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  58064. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  58065. svrCertFile, cliCertFile, cliKeyFile,
  58066. cliCertFile, svrCertFile, svrKeyFile,
  58067. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  58068. TEST_SUCCESS);
  58069. }
  58070. #endif
  58071. #ifdef HAVE_ECC
  58072. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  58073. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  58074. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  58075. cliEccCertFile, eccCertFile, eccKeyFile,
  58076. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  58077. TEST_SUCCESS);
  58078. }
  58079. #endif
  58080. #ifdef HAVE_ED25519
  58081. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  58082. if (tlsVer == WOLFSSL_DTLSV1) continue;
  58083. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  58084. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  58085. cliEdCertFile, edCertFile, edKeyFile,
  58086. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  58087. TEST_SUCCESS);
  58088. }
  58089. #endif
  58090. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  58091. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  58092. return EXPECT_RESULT();
  58093. }
  58094. static int test_openssl_FIPS_drbg(void)
  58095. {
  58096. EXPECT_DECLS;
  58097. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  58098. DRBG_CTX* dctx = NULL;
  58099. byte data1[32], data2[32], zeroData[32];
  58100. byte testSeed[16];
  58101. size_t dlen = sizeof(data1);
  58102. int i;
  58103. XMEMSET(data1, 0, dlen);
  58104. XMEMSET(data2, 0, dlen);
  58105. XMEMSET(zeroData, 0, sizeof(zeroData));
  58106. for (i = 0; i < (int)sizeof(testSeed); i++) {
  58107. testSeed[i] = (byte)i;
  58108. }
  58109. ExpectNotNull(dctx = FIPS_get_default_drbg());
  58110. ExpectIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  58111. ExpectIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  58112. WOLFSSL_SUCCESS);
  58113. ExpectIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  58114. ExpectIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  58115. WOLFSSL_SUCCESS);
  58116. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  58117. ExpectIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  58118. WOLFSSL_SUCCESS);
  58119. ExpectIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  58120. WOLFSSL_SUCCESS);
  58121. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  58122. ExpectIntNE(XMEMCMP(data1, data2, dlen), 0);
  58123. ExpectIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  58124. #ifndef HAVE_GLOBAL_RNG
  58125. /* gets freed by wolfSSL_Cleanup() when HAVE_GLOBAL_RNG defined */
  58126. wolfSSL_FIPS_drbg_free(dctx);
  58127. #endif
  58128. #endif
  58129. return EXPECT_RESULT();
  58130. }
  58131. static int test_wolfSSL_FIPS_mode(void)
  58132. {
  58133. EXPECT_DECLS;
  58134. #if defined(OPENSSL_ALL)
  58135. #ifdef HAVE_FIPS
  58136. ExpectIntEQ(wolfSSL_FIPS_mode(), 1);
  58137. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  58138. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  58139. #else
  58140. ExpectIntEQ(wolfSSL_FIPS_mode(), 0);
  58141. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  58142. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  58143. #endif
  58144. #endif
  58145. return EXPECT_RESULT();
  58146. }
  58147. #ifdef WOLFSSL_DTLS
  58148. /* Prints out the current window */
  58149. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  58150. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  58151. int i;
  58152. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  58153. word32 b = w[i];
  58154. int j;
  58155. /* Prints out a 32 bit binary number in big endian order */
  58156. for (j = 0; j < 32; j++, b <<= 1) {
  58157. if (b & (((word32)1) << 31))
  58158. fprintf(stderr, "1");
  58159. else
  58160. fprintf(stderr, "0");
  58161. }
  58162. fprintf(stderr, " ");
  58163. }
  58164. fprintf(stderr, "cur_hi %u cur_lo %u\n", h, l);
  58165. #else
  58166. (void)h;
  58167. (void)l;
  58168. (void)w;
  58169. #endif
  58170. }
  58171. /* a - cur_hi
  58172. * b - cur_lo
  58173. * c - next_hi
  58174. * d - next_lo
  58175. * e - window
  58176. * f - expected next_hi
  58177. * g - expected next_lo
  58178. * h - expected window[1]
  58179. * i - expected window[0]
  58180. */
  58181. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  58182. ExpectIntEQ(wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)), 1); \
  58183. DUW_TEST_print_window_binary((a), (b), (e)); \
  58184. ExpectIntEQ((c), (f)); \
  58185. ExpectIntEQ((d), (g)); \
  58186. ExpectIntEQ((e)[1], (h)); \
  58187. ExpectIntEQ((e)[0], (i)); \
  58188. } while (0)
  58189. static int test_wolfSSL_DtlsUpdateWindow(void)
  58190. {
  58191. EXPECT_DECLS;
  58192. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  58193. word32 next_lo = 0;
  58194. word16 next_hi = 0;
  58195. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  58196. fprintf(stderr, "\n");
  58197. #endif
  58198. XMEMSET(window, 0, sizeof window);
  58199. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  58200. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  58201. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  58202. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  58203. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  58204. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  58205. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  58206. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  58207. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  58208. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  58209. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  58210. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  58211. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  58212. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  58213. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  58214. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  58215. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  58216. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  58217. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  58218. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  58219. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  58220. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  58221. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  58222. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  58223. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  58224. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  58225. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  58226. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  58227. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  58228. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  58229. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  58230. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  58231. return EXPECT_RESULT();
  58232. }
  58233. #endif /* WOLFSSL_DTLS */
  58234. #ifdef WOLFSSL_DTLS
  58235. static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
  58236. word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
  58237. {
  58238. DtlsMsg* cur;
  58239. static byte msg[100];
  58240. static byte msgInit = 0;
  58241. if (!msgInit) {
  58242. int i;
  58243. for (i = 0; i < 100; i++)
  58244. msg[i] = i + 1;
  58245. msgInit = 1;
  58246. }
  58247. /* Sanitize test parameters */
  58248. if (len > sizeof(msg))
  58249. return -1;
  58250. if (f_offset + f_len > sizeof(msg))
  58251. return -1;
  58252. DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
  58253. if (ssl->dtls_rx_msg_list == NULL)
  58254. return -100;
  58255. if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
  58256. return -200;
  58257. if (cur->fragBucketListCount != f_count)
  58258. return -300;
  58259. if (cur->ready != ready)
  58260. return -400;
  58261. if (cur->bytesReceived != bytesReceived)
  58262. return -500;
  58263. if (ready) {
  58264. if (cur->fragBucketList != NULL)
  58265. return -600;
  58266. if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
  58267. return -700;
  58268. }
  58269. else {
  58270. DtlsFragBucket* fb;
  58271. if (cur->fragBucketList == NULL)
  58272. return -800;
  58273. for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
  58274. if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
  58275. return -900;
  58276. }
  58277. }
  58278. return 0;
  58279. }
  58280. static int test_wolfSSL_DTLS_fragment_buckets(void)
  58281. {
  58282. EXPECT_DECLS;
  58283. WOLFSSL ssl[1];
  58284. XMEMSET(ssl, 0, sizeof(*ssl));
  58285. ExpectIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
  58286. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  58287. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
  58288. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
  58289. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
  58290. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
  58291. /* Test all permutations of 3 regions */
  58292. /* 1 2 3 */
  58293. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  58294. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
  58295. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  58296. /* 1 3 2 */
  58297. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  58298. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
  58299. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  58300. /* 2 1 3 */
  58301. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  58302. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
  58303. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  58304. /* 2 3 1 */
  58305. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  58306. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
  58307. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  58308. /* 3 1 2 */
  58309. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  58310. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
  58311. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  58312. /* 3 2 1 */
  58313. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  58314. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
  58315. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  58316. /* Test overlapping regions */
  58317. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  58318. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
  58319. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
  58320. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
  58321. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
  58322. /* Test overlapping multiple regions */
  58323. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  58324. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
  58325. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
  58326. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
  58327. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
  58328. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
  58329. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
  58330. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
  58331. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
  58332. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
  58333. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  58334. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
  58335. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
  58336. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
  58337. DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
  58338. ssl->dtls_rx_msg_list = NULL;
  58339. ssl->dtls_rx_msg_list_sz = 0;
  58340. return EXPECT_RESULT();
  58341. }
  58342. #endif
  58343. #if !defined(NO_FILESYSTEM) && \
  58344. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58345. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  58346. static int test_wolfSSL_dtls_stateless2(void)
  58347. {
  58348. EXPECT_DECLS;
  58349. WOLFSSL *ssl_c = NULL;
  58350. WOLFSSL *ssl_c2 = NULL;
  58351. WOLFSSL *ssl_s = NULL;
  58352. struct test_memio_ctx test_ctx;
  58353. WOLFSSL_CTX *ctx_c = NULL;
  58354. WOLFSSL_CTX *ctx_s = NULL;
  58355. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58356. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58357. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  58358. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  58359. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  58360. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  58361. /* send CH */
  58362. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  58363. (ssl_c2->error == WANT_READ));
  58364. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58365. (ssl_s->error == WANT_READ));
  58366. ExpectIntNE(test_ctx.c_len, 0);
  58367. /* consume HRR */
  58368. test_ctx.c_len = 0;
  58369. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58370. wolfSSL_free(ssl_c2);
  58371. wolfSSL_free(ssl_c);
  58372. wolfSSL_free(ssl_s);
  58373. wolfSSL_CTX_free(ctx_c);
  58374. wolfSSL_CTX_free(ctx_s);
  58375. return EXPECT_RESULT();
  58376. }
  58377. #ifdef HAVE_MAX_FRAGMENT
  58378. static int test_wolfSSL_dtls_stateless_maxfrag(void)
  58379. {
  58380. EXPECT_DECLS;
  58381. WOLFSSL *ssl_c = NULL;
  58382. WOLFSSL *ssl_c2 = NULL;
  58383. WOLFSSL *ssl_s = NULL;
  58384. struct test_memio_ctx test_ctx;
  58385. WOLFSSL_CTX *ctx_c = NULL;
  58386. WOLFSSL_CTX *ctx_s = NULL;
  58387. word16 max_fragment = 0;
  58388. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58389. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58390. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  58391. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  58392. ExpectIntEQ(wolfSSL_UseMaxFragment(ssl_c2, WOLFSSL_MFL_2_8),
  58393. WOLFSSL_SUCCESS);
  58394. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  58395. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  58396. if (ssl_s != NULL) {
  58397. max_fragment = ssl_s->max_fragment;
  58398. }
  58399. /* send CH */
  58400. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  58401. (ssl_c2->error == WANT_READ));
  58402. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58403. (ssl_s->error == WANT_READ));
  58404. /* CH without cookie shouldn't change state */
  58405. ExpectIntEQ(ssl_s->max_fragment, max_fragment);
  58406. ExpectIntNE(test_ctx.c_len, 0);
  58407. /* consume HRR from buffer */
  58408. test_ctx.c_len = 0;
  58409. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58410. wolfSSL_free(ssl_c2);
  58411. wolfSSL_free(ssl_c);
  58412. wolfSSL_free(ssl_s);
  58413. wolfSSL_CTX_free(ctx_c);
  58414. wolfSSL_CTX_free(ctx_s);
  58415. return EXPECT_RESULT();
  58416. }
  58417. #endif /* HAVE_MAX_FRAGMENT */
  58418. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  58419. #define ROUNDS_WITH_HVR 4
  58420. #define ROUNDS_WITHOUT_HVR 2
  58421. #define HANDSHAKE_TYPE_OFFSET DTLS_RECORD_HEADER_SZ
  58422. static int buf_is_hvr(const byte *data, int len)
  58423. {
  58424. if (len < DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ)
  58425. return 0;
  58426. return data[HANDSHAKE_TYPE_OFFSET] == hello_verify_request;
  58427. }
  58428. static int _test_wolfSSL_dtls_stateless_resume(byte useticket, byte bad)
  58429. {
  58430. EXPECT_DECLS;
  58431. struct test_memio_ctx test_ctx;
  58432. WOLFSSL_CTX *ctx_c = NULL;
  58433. WOLFSSL_CTX *ctx_s = NULL;
  58434. WOLFSSL *ssl_c = NULL;
  58435. WOLFSSL *ssl_s = NULL;
  58436. WOLFSSL_SESSION *sess = NULL;
  58437. int round_trips;
  58438. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58439. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  58440. &ssl_s, wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  58441. #ifdef HAVE_SESSION_TICKET
  58442. if (useticket) {
  58443. ExpectIntEQ(wolfSSL_UseSessionTicket(ssl_c), WOLFSSL_SUCCESS);
  58444. }
  58445. #endif
  58446. round_trips = ROUNDS_WITH_HVR;
  58447. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, round_trips,
  58448. &round_trips), 0);
  58449. ExpectIntEQ(round_trips, ROUNDS_WITH_HVR);
  58450. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  58451. wolfSSL_shutdown(ssl_c);
  58452. wolfSSL_shutdown(ssl_s);
  58453. wolfSSL_free(ssl_c);
  58454. ssl_c = NULL;
  58455. wolfSSL_free(ssl_s);
  58456. ssl_s = NULL;
  58457. test_ctx.c_len = test_ctx.s_len = 0;
  58458. /* make resumption invalid */
  58459. if (bad && (sess != NULL)) {
  58460. if (useticket) {
  58461. #ifdef HAVE_SESSION_TICKET
  58462. if (sess->ticket != NULL) {
  58463. sess->ticket[0] = !sess->ticket[0];
  58464. }
  58465. #endif /* HAVE_SESSION_TICKET */
  58466. }
  58467. else {
  58468. sess->sessionID[0] = !sess->sessionID[0];
  58469. }
  58470. }
  58471. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  58472. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  58473. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  58474. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  58475. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  58476. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  58477. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  58478. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  58479. (ssl_c->error == WANT_READ));
  58480. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58481. (ssl_s->error == WANT_READ));
  58482. ExpectFalse(bad && !buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  58483. ExpectFalse(!bad && buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  58484. if (!useticket) {
  58485. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, &round_trips), 0);
  58486. ExpectFalse(bad && round_trips != ROUNDS_WITH_HVR - 1);
  58487. ExpectFalse(!bad && round_trips != ROUNDS_WITHOUT_HVR - 1);
  58488. }
  58489. wolfSSL_SESSION_free(sess);
  58490. wolfSSL_free(ssl_c);
  58491. wolfSSL_free(ssl_s);
  58492. wolfSSL_CTX_free(ctx_c);
  58493. wolfSSL_CTX_free(ctx_s);
  58494. return EXPECT_RESULT();
  58495. }
  58496. static int test_wolfSSL_dtls_stateless_resume(void)
  58497. {
  58498. EXPECT_DECLS;
  58499. #ifdef HAVE_SESSION_TICKET
  58500. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 0), TEST_SUCCESS);
  58501. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 1), TEST_SUCCESS);
  58502. #endif /* HAVE_SESION_TICKET */
  58503. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 0), TEST_SUCCESS);
  58504. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 1), TEST_SUCCESS);
  58505. return EXPECT_RESULT();
  58506. }
  58507. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  58508. #if !defined(NO_OLD_TLS)
  58509. static int test_wolfSSL_dtls_stateless_downgrade(void)
  58510. {
  58511. EXPECT_DECLS;
  58512. WOLFSSL_CTX *ctx_c = NULL;
  58513. WOLFSSL_CTX *ctx_c2 = NULL;
  58514. WOLFSSL_CTX *ctx_s = NULL;
  58515. WOLFSSL *ssl_c = NULL;
  58516. WOLFSSL *ssl_c2 = NULL;
  58517. WOLFSSL *ssl_s = NULL;
  58518. struct test_memio_ctx test_ctx;
  58519. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58520. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58521. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  58522. ExpectIntEQ(wolfSSL_CTX_SetMinVersion(ctx_s, WOLFSSL_DTLSV1),
  58523. WOLFSSL_SUCCESS);
  58524. ExpectNotNull(ctx_c2 = wolfSSL_CTX_new(wolfDTLSv1_client_method()));
  58525. wolfSSL_SetIORecv(ctx_c2, test_memio_read_cb);
  58526. wolfSSL_SetIOSend(ctx_c2, test_memio_write_cb);
  58527. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c2));
  58528. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  58529. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  58530. /* send CH */
  58531. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  58532. (ssl_c2->error == WANT_READ));
  58533. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58534. (ssl_s->error == WANT_READ));
  58535. ExpectIntNE(test_ctx.c_len, 0);
  58536. /* consume HRR */
  58537. test_ctx.c_len = 0;
  58538. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58539. wolfSSL_free(ssl_c2);
  58540. wolfSSL_free(ssl_c);
  58541. wolfSSL_free(ssl_s);
  58542. wolfSSL_CTX_free(ctx_c);
  58543. wolfSSL_CTX_free(ctx_c2);
  58544. wolfSSL_CTX_free(ctx_s);
  58545. return EXPECT_RESULT();
  58546. }
  58547. #endif /* !defined(NO_OLD_TLS) */
  58548. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58549. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
  58550. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58551. !defined(NO_OLD_TLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  58552. static int test_WOLFSSL_dtls_version_alert(void)
  58553. {
  58554. EXPECT_DECLS;
  58555. struct test_memio_ctx test_ctx;
  58556. WOLFSSL_CTX *ctx_c = NULL;
  58557. WOLFSSL_CTX *ctx_s = NULL;
  58558. WOLFSSL *ssl_c = NULL;
  58559. WOLFSSL *ssl_s = NULL;
  58560. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58561. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58562. wolfDTLSv1_2_client_method, wolfDTLSv1_server_method), 0);
  58563. /* client hello */
  58564. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  58565. (ssl_c->error == WANT_READ));
  58566. /* hrr */
  58567. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58568. (ssl_s->error == WANT_READ));
  58569. /* client hello 1 */
  58570. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  58571. (ssl_c->error == WANT_READ));
  58572. /* server hello */
  58573. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58574. (ssl_s->error == WANT_READ));
  58575. /* should fail */
  58576. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  58577. (ssl_c->error == VERSION_ERROR));
  58578. /* shuould fail */
  58579. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58580. (ssl_s->error == VERSION_ERROR || ssl_s->error == FATAL_ERROR));
  58581. wolfSSL_free(ssl_c);
  58582. wolfSSL_free(ssl_s);
  58583. wolfSSL_CTX_free(ctx_c);
  58584. wolfSSL_CTX_free(ctx_s);
  58585. return EXPECT_RESULT();
  58586. }
  58587. #else
  58588. static int test_WOLFSSL_dtls_version_alert(void)
  58589. {
  58590. return TEST_SKIPPED;
  58591. }
  58592. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
  58593. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
  58594. * !defined(NO_OLD_TLS) && !defined(NO_RSA)
  58595. */
  58596. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  58597. && defined(WOLFSSL_TLS13) && \
  58598. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))\
  58599. && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  58600. static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
  58601. {
  58602. struct test_memio_ctx *test_ctx;
  58603. byte buf[2048];
  58604. int idx, sz;
  58605. word32 tmp;
  58606. int ret;
  58607. idx = 5; /* space for record header */
  58608. buf[idx] = session_ticket; /* type */
  58609. idx++;
  58610. tmp = OPAQUE32_LEN +
  58611. OPAQUE32_LEN +
  58612. OPAQUE8_LEN + nonceLength +
  58613. OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
  58614. c32to24(tmp, buf + idx);
  58615. idx += OPAQUE24_LEN;
  58616. c32toa((word32)12345, buf+idx); /* lifetime */
  58617. idx += OPAQUE32_LEN;
  58618. c32toa((word32)12345, buf+idx); /* add */
  58619. idx += OPAQUE32_LEN;
  58620. buf[idx] = nonceLength; /* nonce length */
  58621. idx++;
  58622. XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
  58623. idx += nonceLength;
  58624. tmp = 1; /* ticket len */
  58625. c16toa((word16)tmp, buf+idx);
  58626. idx += 2;
  58627. buf[idx] = 0xFF; /* ticket */
  58628. idx++;
  58629. tmp = 0; /* ext len */
  58630. c16toa((word16)tmp, buf+idx);
  58631. idx += 2;
  58632. sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
  58633. handshake, 0, 0, 0);
  58634. test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
  58635. ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
  58636. return !(ret == sz);
  58637. }
  58638. static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
  58639. {
  58640. int ret = 0;
  58641. if ((sess == NULL) || (sess->ticketNonce.len != len)) {
  58642. ret = -1;
  58643. }
  58644. else {
  58645. int i;
  58646. for (i = 0; i < len; i++) {
  58647. if (sess->ticketNonce.data[i] != len) {
  58648. ret = -1;
  58649. break;
  58650. }
  58651. }
  58652. }
  58653. return ret;
  58654. }
  58655. static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  58656. {
  58657. EXPECT_DECLS;
  58658. char *buf[1024];
  58659. ExpectIntEQ(send_new_session_ticket(ssl_s, len, len), 0);
  58660. ExpectTrue((wolfSSL_recv(ssl_c, buf, 1024, 0) == WOLFSSL_FATAL_ERROR) &&
  58661. (ssl_c->error == WANT_READ));
  58662. ExpectIntEQ(test_ticket_nonce_check(ssl_c->session, len), 0);
  58663. return EXPECT_RESULT();
  58664. }
  58665. static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  58666. {
  58667. EXPECT_DECLS;
  58668. WOLFSSL_SESSION *sess = NULL;
  58669. WOLFSSL_SESSION *cached = NULL;
  58670. WOLFSSL_CTX *ctx = ssl_c->ctx;
  58671. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, len), TEST_SUCCESS);
  58672. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  58673. ExpectIntEQ(AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
  58674. NULL, ssl_c->options.side, 1,NULL), 0);
  58675. ExpectNotNull(cached = wolfSSL_SESSION_new());
  58676. ExpectIntEQ(wolfSSL_GetSessionFromCache(ssl_c, cached), WOLFSSL_SUCCESS);
  58677. ExpectIntEQ(test_ticket_nonce_check(cached, len), 0);
  58678. wolfSSL_SESSION_free(cached);
  58679. wolfSSL_SESSION_free(sess);
  58680. return EXPECT_RESULT();
  58681. }
  58682. static int test_ticket_nonce_malloc(void)
  58683. {
  58684. EXPECT_DECLS;
  58685. struct test_memio_ctx test_ctx;
  58686. WOLFSSL_CTX *ctx_c = NULL;
  58687. WOLFSSL_CTX *ctx_s = NULL;
  58688. WOLFSSL *ssl_c = NULL;
  58689. WOLFSSL *ssl_s = NULL;
  58690. byte small;
  58691. byte medium;
  58692. byte big;
  58693. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58694. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58695. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  58696. /* will send ticket manually */
  58697. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(ssl_s), 0);
  58698. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  58699. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  58700. while (EXPECT_SUCCESS() && (ssl_c->options.handShakeDone == 0) &&
  58701. (ssl_s->options.handShakeDone == 0)) {
  58702. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_SUCCESS) ||
  58703. (ssl_c->error == WANT_READ));
  58704. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_SUCCESS) ||
  58705. (ssl_s->error == WANT_READ));
  58706. }
  58707. small = TLS13_TICKET_NONCE_STATIC_SZ;
  58708. medium = small + 20 <= 255 ? small + 20 : 255;
  58709. big = medium + 20 <= 255 ? small + 20 : 255;
  58710. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  58711. ExpectPtrEq(ssl_c->session->ticketNonce.data,
  58712. ssl_c->session->ticketNonce.dataStatic);
  58713. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  58714. TEST_SUCCESS);
  58715. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, big), TEST_SUCCESS);
  58716. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  58717. TEST_SUCCESS);
  58718. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  58719. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  58720. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  58721. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, big), TEST_SUCCESS);
  58722. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  58723. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  58724. wolfSSL_free(ssl_c);
  58725. wolfSSL_free(ssl_s);
  58726. wolfSSL_CTX_free(ctx_c);
  58727. wolfSSL_CTX_free(ctx_s);
  58728. return EXPECT_RESULT();
  58729. }
  58730. #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
  58731. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_TLS12) && \
  58732. !defined(WOLFSSL_TICKET_DECRYPT_NO_CREATE) && \
  58733. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  58734. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_RSA) && \
  58735. defined(HAVE_ECC) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  58736. static int test_ticket_ret_create(void)
  58737. {
  58738. EXPECT_DECLS;
  58739. WOLFSSL_CTX *ctx_c = NULL;
  58740. WOLFSSL_CTX *ctx_s = NULL;
  58741. WOLFSSL *ssl_c = NULL;
  58742. WOLFSSL *ssl_s = NULL;
  58743. byte ticket[SESSION_TICKET_LEN];
  58744. struct test_memio_ctx test_ctx;
  58745. WOLFSSL_SESSION *sess = NULL;
  58746. word16 ticketLen = 0;
  58747. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58748. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58749. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  58750. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  58751. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  58752. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx_c), WOLFSSL_SUCCESS);
  58753. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58754. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  58755. ExpectIntLE(sess->ticketLen, SESSION_TICKET_LEN);
  58756. if (sess != NULL) {
  58757. ticketLen = sess->ticketLen;
  58758. XMEMCPY(ticket, sess->ticket, sess->ticketLen);
  58759. }
  58760. wolfSSL_free(ssl_c);
  58761. ssl_c = NULL;
  58762. wolfSSL_free(ssl_s);
  58763. ssl_s = NULL;
  58764. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  58765. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  58766. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  58767. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  58768. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  58769. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  58770. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  58771. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58772. ExpectIntLE(ssl_c->session->ticketLen, SESSION_TICKET_LEN);
  58773. ExpectIntEQ(ssl_c->session->ticketLen, ticketLen);
  58774. ExpectTrue(XMEMCMP(ssl_c->session->ticket, ticket, ticketLen) != 0);
  58775. wolfSSL_SESSION_free(sess);
  58776. wolfSSL_free(ssl_c);
  58777. wolfSSL_free(ssl_s);
  58778. wolfSSL_CTX_free(ctx_c);
  58779. wolfSSL_CTX_free(ctx_s);
  58780. return EXPECT_RESULT();
  58781. }
  58782. #else
  58783. static int test_ticket_ret_create(void)
  58784. {
  58785. return TEST_SKIPPED;
  58786. }
  58787. #endif
  58788. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && \
  58789. defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  58790. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  58791. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  58792. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  58793. static void test_ticket_and_psk_mixing_on_result(WOLFSSL* ssl)
  58794. {
  58795. int ret;
  58796. WOLFSSL_SESSION* session = NULL;
  58797. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  58798. if (!wolfSSL_is_server(ssl)) {
  58799. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  58800. AssertNotNull(session);
  58801. }
  58802. do {
  58803. ret = wolfSSL_shutdown(ssl);
  58804. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  58805. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  58806. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  58807. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  58808. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  58809. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  58810. #endif
  58811. if (!wolfSSL_is_server(ssl)) {
  58812. /* client */
  58813. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  58814. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  58815. wolfSSL_set_session(ssl, session);
  58816. wolfSSL_SESSION_free(session);
  58817. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  58818. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  58819. }
  58820. else {
  58821. /* server */
  58822. /* Different ciphersuite so that the ticket will be invalidated based on
  58823. * the ciphersuite */
  58824. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"),
  58825. WOLFSSL_SUCCESS);
  58826. wolfSSL_set_psk_server_tls13_callback(ssl, my_psk_server_tls13_cb);
  58827. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  58828. }
  58829. }
  58830. static void test_ticket_and_psk_mixing_ssl_ready(WOLFSSL* ssl)
  58831. {
  58832. AssertIntEQ(wolfSSL_UseSessionTicket(ssl), WOLFSSL_SUCCESS);
  58833. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  58834. WOLFSSL_SUCCESS);
  58835. }
  58836. static int test_ticket_and_psk_mixing(void)
  58837. {
  58838. EXPECT_DECLS;
  58839. /* Test mixing tickets and regular PSK */
  58840. callback_functions client_cbs, server_cbs;
  58841. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58842. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58843. client_cbs.method = wolfTLSv1_3_client_method;
  58844. server_cbs.method = wolfTLSv1_3_server_method;
  58845. client_cbs.ssl_ready = test_ticket_and_psk_mixing_ssl_ready;
  58846. client_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  58847. server_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  58848. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  58849. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  58850. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  58851. return EXPECT_RESULT();
  58852. }
  58853. #else
  58854. static int test_ticket_and_psk_mixing(void)
  58855. {
  58856. return TEST_SKIPPED;
  58857. }
  58858. #endif
  58859. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  58860. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  58861. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  58862. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  58863. static int test_prioritize_psk_cb_called = FALSE;
  58864. static unsigned int test_prioritize_psk_cb(WOLFSSL* ssl,
  58865. const char* identity, unsigned char* key, unsigned int key_max_len,
  58866. const char** ciphersuite)
  58867. {
  58868. test_prioritize_psk_cb_called = TRUE;
  58869. return my_psk_server_tls13_cb(ssl, identity, key, key_max_len, ciphersuite);
  58870. }
  58871. static void test_prioritize_psk_on_result(WOLFSSL* ssl)
  58872. {
  58873. int ret;
  58874. WOLFSSL_SESSION* session = NULL;
  58875. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  58876. if (!wolfSSL_is_server(ssl)) {
  58877. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  58878. AssertNotNull(session);
  58879. }
  58880. do {
  58881. ret = wolfSSL_shutdown(ssl);
  58882. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  58883. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  58884. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  58885. /* Previous connection was made with TLS13-AES128-GCM-SHA256. Order is
  58886. * important. */
  58887. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  58888. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  58889. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  58890. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  58891. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  58892. #endif
  58893. if (!wolfSSL_is_server(ssl)) {
  58894. /* client */
  58895. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  58896. wolfSSL_set_session(ssl, session);
  58897. wolfSSL_SESSION_free(session);
  58898. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  58899. }
  58900. else {
  58901. /* server */
  58902. wolfSSL_set_psk_server_tls13_callback(ssl, test_prioritize_psk_cb);
  58903. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  58904. #ifdef WOLFSSL_PRIORITIZE_PSK
  58905. /* The ticket should be first tried with all ciphersuites and chosen */
  58906. AssertFalse(test_prioritize_psk_cb_called);
  58907. #else
  58908. /* Ciphersuites should be tried with each PSK. This triggers the PSK
  58909. * callback that sets this var. */
  58910. AssertTrue(test_prioritize_psk_cb_called);
  58911. #endif
  58912. }
  58913. }
  58914. static void test_prioritize_psk_ssl_ready(WOLFSSL* ssl)
  58915. {
  58916. if (!wolfSSL_is_server(ssl))
  58917. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  58918. WOLFSSL_SUCCESS);
  58919. else
  58920. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  58921. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  58922. }
  58923. static int test_prioritize_psk(void)
  58924. {
  58925. EXPECT_DECLS;
  58926. /* We always send the ticket first. With WOLFSSL_PRIORITIZE_PSK the order
  58927. * of the PSK's will be followed instead of the ciphersuite. */
  58928. callback_functions client_cbs, server_cbs;
  58929. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58930. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58931. client_cbs.method = wolfTLSv1_3_client_method;
  58932. server_cbs.method = wolfTLSv1_3_server_method;
  58933. client_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  58934. server_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  58935. client_cbs.on_result = test_prioritize_psk_on_result;
  58936. server_cbs.on_result = test_prioritize_psk_on_result;
  58937. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  58938. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  58939. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  58940. return EXPECT_RESULT();
  58941. }
  58942. #else
  58943. static int test_prioritize_psk(void)
  58944. {
  58945. return TEST_SKIPPED;
  58946. }
  58947. #endif
  58948. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  58949. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  58950. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  58951. !defined(WOLFSSL_NO_TLS12)
  58952. static int test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server(WOLFSSL_CTX* ctx)
  58953. {
  58954. EXPECT_DECLS;
  58955. ExpectTrue(SSL_CTX_set_cipher_list(ctx, "DEFAULT"));
  58956. /* Set TLS 1.3 specific suite */
  58957. ExpectTrue(SSL_CTX_set_ciphersuites(ctx, "TLS13-AES128-GCM-SHA256"));
  58958. return EXPECT_RESULT();
  58959. }
  58960. static int test_wolfSSL_CTX_set_ciphersuites(void)
  58961. {
  58962. EXPECT_DECLS;
  58963. /* Test using SSL_CTX_set_cipher_list and SSL_CTX_set_ciphersuites and then
  58964. * do a 1.2 connection. */
  58965. test_ssl_cbf client_cbs, server_cbs;
  58966. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58967. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58968. client_cbs.method = wolfTLSv1_2_client_method;
  58969. server_cbs.method = wolfTLS_server_method; /* Allow downgrade */
  58970. server_cbs.ctx_ready = test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server;
  58971. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  58972. &server_cbs, NULL), TEST_SUCCESS);
  58973. return EXPECT_RESULT();
  58974. }
  58975. #else
  58976. static int test_wolfSSL_CTX_set_ciphersuites(void)
  58977. {
  58978. return TEST_SKIPPED;
  58979. }
  58980. #endif
  58981. #if defined(HAVE_CRL) && defined(WOLFSSL_CHECK_ALERT_ON_ERR) && \
  58982. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  58983. static int test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready(WOLFSSL_CTX* ctx)
  58984. {
  58985. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  58986. return TEST_SUCCESS;
  58987. }
  58988. static int test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup(WOLFSSL* ssl)
  58989. {
  58990. EXPECT_DECLS;
  58991. WOLFSSL_ALERT_HISTORY h;
  58992. ExpectIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  58993. ExpectIntEQ(h.last_rx.level, alert_fatal);
  58994. ExpectIntEQ(h.last_rx.code, certificate_revoked);
  58995. return EXPECT_RESULT();
  58996. }
  58997. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  58998. {
  58999. EXPECT_DECLS;
  59000. test_ssl_cbf client_cbs, server_cbs;
  59001. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59002. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59003. server_cbs.certPemFile = "./certs/server-revoked-cert.pem";
  59004. server_cbs.keyPemFile = "./certs/server-revoked-key.pem";
  59005. client_cbs.crlPemFile = "./certs/crl/crl.revoked";
  59006. client_cbs.ctx_ready = test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready;
  59007. server_cbs.on_cleanup = test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup;
  59008. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  59009. &server_cbs, NULL), TEST_FAIL);
  59010. return EXPECT_RESULT();
  59011. }
  59012. #else
  59013. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  59014. {
  59015. return TEST_SKIPPED;
  59016. }
  59017. #endif
  59018. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET) \
  59019. && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  59020. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  59021. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  59022. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  59023. static WOLFSSL_CTX* test_TLS_13_ticket_different_ciphers_ctx = NULL;
  59024. static WOLFSSL_SESSION* test_TLS_13_ticket_different_ciphers_session = NULL;
  59025. static int test_TLS_13_ticket_different_ciphers_run = 0;
  59026. static int test_TLS_13_ticket_different_ciphers_ssl_ready(WOLFSSL* ssl)
  59027. {
  59028. EXPECT_DECLS;
  59029. switch (test_TLS_13_ticket_different_ciphers_run) {
  59030. case 0:
  59031. /* First run */
  59032. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  59033. WOLFSSL_SUCCESS);
  59034. if (wolfSSL_is_server(ssl)) {
  59035. ExpectNotNull(test_TLS_13_ticket_different_ciphers_ctx =
  59036. wolfSSL_get_SSL_CTX(ssl));
  59037. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_up_ref(
  59038. test_TLS_13_ticket_different_ciphers_ctx));
  59039. }
  59040. break;
  59041. case 1:
  59042. /* Second run */
  59043. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  59044. "TLS13-AES128-GCM-SHA256"),
  59045. WOLFSSL_SUCCESS);
  59046. if (!wolfSSL_is_server(ssl)) {
  59047. ExpectIntEQ(wolfSSL_set_session(ssl,
  59048. test_TLS_13_ticket_different_ciphers_session),
  59049. WOLFSSL_SUCCESS);
  59050. }
  59051. break;
  59052. default:
  59053. /* Bad state? */
  59054. Fail(("Should not enter here"), ("Should not enter here"));
  59055. }
  59056. return EXPECT_RESULT();
  59057. }
  59058. static int test_TLS_13_ticket_different_ciphers_on_result(WOLFSSL* ssl)
  59059. {
  59060. EXPECT_DECLS;
  59061. switch (test_TLS_13_ticket_different_ciphers_run) {
  59062. case 0:
  59063. /* First run */
  59064. ExpectNotNull(test_TLS_13_ticket_different_ciphers_session =
  59065. wolfSSL_get1_session(ssl));
  59066. break;
  59067. case 1:
  59068. /* Second run */
  59069. ExpectTrue(wolfSSL_session_reused(ssl));
  59070. break;
  59071. default:
  59072. /* Bad state? */
  59073. Fail(("Should not enter here"), ("Should not enter here"));
  59074. }
  59075. return EXPECT_RESULT();
  59076. }
  59077. static int test_TLS_13_ticket_different_ciphers(void)
  59078. {
  59079. EXPECT_DECLS;
  59080. /* Check that we handle the connection when the ticket doesn't match
  59081. * the first ciphersuite. */
  59082. test_ssl_cbf client_cbs, server_cbs;
  59083. struct test_params {
  59084. method_provider client_meth;
  59085. method_provider server_meth;
  59086. int doUdp;
  59087. } params[] = {
  59088. #ifdef WOLFSSL_DTLS13
  59089. /* Test that the stateless code handles sessions correctly */
  59090. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, 1},
  59091. #endif
  59092. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, 0},
  59093. };
  59094. size_t i;
  59095. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  59096. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59097. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59098. test_TLS_13_ticket_different_ciphers_run = 0;
  59099. client_cbs.doUdp = server_cbs.doUdp = params[i].doUdp;
  59100. client_cbs.method = params[i].client_meth;
  59101. server_cbs.method = params[i].server_meth;
  59102. client_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  59103. server_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  59104. client_cbs.on_result = test_TLS_13_ticket_different_ciphers_on_result;
  59105. server_cbs.ticNoInit = 1;
  59106. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  59107. &server_cbs, NULL), TEST_SUCCESS);
  59108. test_TLS_13_ticket_different_ciphers_run++;
  59109. server_cbs.ctx = test_TLS_13_ticket_different_ciphers_ctx;
  59110. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  59111. &server_cbs, NULL), TEST_SUCCESS);
  59112. wolfSSL_SESSION_free(test_TLS_13_ticket_different_ciphers_session);
  59113. test_TLS_13_ticket_different_ciphers_session = NULL;
  59114. wolfSSL_CTX_free(test_TLS_13_ticket_different_ciphers_ctx);
  59115. test_TLS_13_ticket_different_ciphers_ctx = NULL;
  59116. }
  59117. return EXPECT_RESULT();
  59118. }
  59119. #else
  59120. static int test_TLS_13_ticket_different_ciphers(void)
  59121. {
  59122. return TEST_SKIPPED;
  59123. }
  59124. #endif
  59125. #if defined(WOLFSSL_EXTRA_ALERTS) && !defined(WOLFSSL_NO_TLS12) && \
  59126. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  59127. #define TEST_WRONG_CS_CLIENT "DHE-RSA-AES128-SHA"
  59128. /* AKA TLS_DHE_RSA_WITH_AES_128_CBC_SHA */
  59129. byte test_extra_alerts_wrong_cs_sh[] = {
  59130. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0xef,
  59131. 0x0c, 0x30, 0x98, 0xa2, 0xac, 0xfa, 0x68, 0xe9, 0x3e, 0xaa, 0x5c, 0xcf,
  59132. 0xa7, 0x42, 0x72, 0xaf, 0xa0, 0xe8, 0x39, 0x2b, 0x3e, 0x81, 0xa7, 0x7a,
  59133. 0xa5, 0x62, 0x8a, 0x0e, 0x41, 0xba, 0xda, 0x20, 0x18, 0x9f, 0xe1, 0x8c,
  59134. 0x1d, 0xc0, 0x37, 0x9c, 0xf4, 0x90, 0x5d, 0x8d, 0xa0, 0x79, 0xa7, 0x4b,
  59135. 0xa8, 0x79, 0xdf, 0xcd, 0x8d, 0xf5, 0xb5, 0x50, 0x5f, 0xf1, 0xdb, 0x4d,
  59136. 0xbb, 0x07, 0x54, 0x1c,
  59137. 0x00, 0x02, /* TLS_RSA_WITH_NULL_SHA */
  59138. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  59139. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  59140. };
  59141. static int test_extra_alerts_wrong_cs(void)
  59142. {
  59143. EXPECT_DECLS;
  59144. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
  59145. struct test_memio_ctx test_ctx;
  59146. WOLFSSL_CTX *ctx_c = NULL;
  59147. WOLFSSL_ALERT_HISTORY h;
  59148. WOLFSSL *ssl_c = NULL;
  59149. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59150. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  59151. wolfTLSv1_2_client_method, NULL), 0);
  59152. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, TEST_WRONG_CS_CLIENT),
  59153. WOLFSSL_SUCCESS);
  59154. /* CH */
  59155. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59156. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59157. WOLFSSL_ERROR_WANT_READ);
  59158. /* consume CH */
  59159. test_ctx.s_len = 0;
  59160. /* inject SH */
  59161. XMEMCPY(test_ctx.c_buff, test_extra_alerts_wrong_cs_sh,
  59162. sizeof(test_extra_alerts_wrong_cs_sh));
  59163. test_ctx.c_len = sizeof(test_extra_alerts_wrong_cs_sh);
  59164. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59165. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59166. WOLFSSL_ERROR_WANT_READ);
  59167. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  59168. ExpectIntEQ(h.last_tx.code, handshake_failure);
  59169. ExpectIntEQ(h.last_tx.level, alert_fatal);
  59170. wolfSSL_free(ssl_c);
  59171. wolfSSL_CTX_free(ctx_c);
  59172. #endif
  59173. return EXPECT_RESULT();
  59174. }
  59175. #else
  59176. static int test_extra_alerts_wrong_cs(void)
  59177. {
  59178. return TEST_SKIPPED;
  59179. }
  59180. #endif
  59181. #if !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_EXTRA_ALERTS) && \
  59182. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_SP_MATH)
  59183. static void test_remove_msg(byte *msg, int tail_len, int *len, int msg_length)
  59184. {
  59185. tail_len -= msg_length;
  59186. XMEMMOVE(msg, msg + msg_length, tail_len);
  59187. *len = *len - msg_length;
  59188. }
  59189. static int test_remove_hs_msg_from_buffer(byte *buf, int *len, byte type,
  59190. byte *found)
  59191. {
  59192. const unsigned int _HANDSHAKE_HEADER_SZ = 4;
  59193. const unsigned int _RECORD_HEADER_SZ = 5;
  59194. const int _change_cipher_hs = 55;
  59195. const int _change_cipher = 20;
  59196. const int _handshake = 22;
  59197. unsigned int tail_len;
  59198. byte *idx, *curr;
  59199. word8 currType;
  59200. word16 rLength;
  59201. word32 hLength;
  59202. idx = buf;
  59203. tail_len = *len;
  59204. *found = 0;
  59205. while (tail_len > _RECORD_HEADER_SZ) {
  59206. curr = idx;
  59207. currType = *idx;
  59208. ato16(idx + 3, &rLength);
  59209. idx += _RECORD_HEADER_SZ;
  59210. tail_len -= _RECORD_HEADER_SZ;
  59211. if (tail_len < rLength)
  59212. return -1;
  59213. if (type == _change_cipher_hs && currType == _change_cipher) {
  59214. if (rLength != 1)
  59215. return -1;
  59216. /* match */
  59217. test_remove_msg(curr, *len - (int)(curr - buf),
  59218. len, _RECORD_HEADER_SZ + 1);
  59219. *found = 1;
  59220. return 0;
  59221. }
  59222. if (currType != _handshake) {
  59223. idx += rLength;
  59224. tail_len -= rLength;
  59225. continue;
  59226. }
  59227. if (rLength < _HANDSHAKE_HEADER_SZ)
  59228. return -1;
  59229. currType = *idx;
  59230. ato24(idx+1, &hLength);
  59231. hLength += _HANDSHAKE_HEADER_SZ;
  59232. if (tail_len < hLength)
  59233. return -1;
  59234. if (currType != type) {
  59235. idx += hLength;
  59236. tail_len -= hLength;
  59237. continue;
  59238. }
  59239. /* match */
  59240. test_remove_msg(curr, *len - (int)(curr - buf), len,
  59241. hLength + _RECORD_HEADER_SZ);
  59242. *found = 1;
  59243. return 0;
  59244. }
  59245. /* not found */
  59246. return 0;
  59247. }
  59248. static int test_remove_hs_message(byte hs_message_type,
  59249. int extra_round, byte alert_type)
  59250. {
  59251. EXPECT_DECLS;
  59252. WOLFSSL_CTX *ctx_c = NULL;
  59253. WOLFSSL_CTX *ctx_s = NULL;
  59254. WOLFSSL *ssl_c = NULL;
  59255. WOLFSSL *ssl_s = NULL;
  59256. struct test_memio_ctx test_ctx;
  59257. WOLFSSL_ALERT_HISTORY h;
  59258. byte found = 0;
  59259. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59260. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59261. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  59262. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59263. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59264. WOLFSSL_ERROR_WANT_READ);
  59265. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  59266. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59267. WOLFSSL_ERROR_WANT_READ);
  59268. if (extra_round) {
  59269. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59270. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59271. WOLFSSL_ERROR_WANT_READ);
  59272. /* this will complete handshake from server side */
  59273. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  59274. }
  59275. ExpectIntEQ(test_remove_hs_msg_from_buffer(test_ctx.c_buff,
  59276. &test_ctx.c_len, hs_message_type, &found), 0);
  59277. if (!found) {
  59278. wolfSSL_free(ssl_c);
  59279. wolfSSL_CTX_free(ctx_c);
  59280. wolfSSL_free(ssl_s);
  59281. wolfSSL_CTX_free(ctx_s);
  59282. return TEST_SKIPPED;
  59283. }
  59284. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59285. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59286. WOLFSSL_ERROR_WANT_READ);
  59287. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  59288. ExpectTrue(alert_type == 0xff || h.last_tx.code == alert_type);
  59289. ExpectIntEQ(h.last_tx.level, alert_fatal);
  59290. wolfSSL_free(ssl_c);
  59291. wolfSSL_CTX_free(ctx_c);
  59292. wolfSSL_free(ssl_s);
  59293. wolfSSL_CTX_free(ctx_s);
  59294. return EXPECT_RESULT();
  59295. }
  59296. static int test_extra_alerts_skip_hs(void)
  59297. {
  59298. EXPECT_DECLS;
  59299. const byte _server_key_exchange = 12;
  59300. const byte _server_hello = 2;
  59301. const byte _certificate = 11;
  59302. /* server_hello */
  59303. ExpectIntNE(test_remove_hs_message(_server_hello, 0,
  59304. unexpected_message), TEST_FAIL);
  59305. ExpectIntNE(test_remove_hs_message(_certificate, 0,
  59306. 0xff), TEST_FAIL);
  59307. ExpectIntNE(test_remove_hs_message(_server_key_exchange, 0,
  59308. unexpected_message), TEST_FAIL);
  59309. return EXPECT_RESULT();
  59310. }
  59311. #else
  59312. static int test_extra_alerts_skip_hs(void)
  59313. {
  59314. return TEST_SKIPPED;
  59315. }
  59316. #endif
  59317. #if !defined(WOLFSSL_NO_TLS12) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  59318. && defined(WOLFSSL_EXTRA_ALERTS) && !defined(NO_PSK) && !defined(NO_DH)
  59319. static unsigned int test_server_psk_cb(WOLFSSL* ssl, const char* id,
  59320. unsigned char* key, unsigned int key_max_len)
  59321. {
  59322. (void)ssl;
  59323. (void)id;
  59324. (void)key_max_len;
  59325. /* zero means error */
  59326. key[0] = 0x10;
  59327. return 1;
  59328. }
  59329. static int test_extra_alerts_bad_psk(void)
  59330. {
  59331. EXPECT_DECLS;
  59332. WOLFSSL_CTX *ctx_c = NULL;
  59333. WOLFSSL_CTX *ctx_s = NULL;
  59334. WOLFSSL *ssl_c = NULL;
  59335. WOLFSSL *ssl_s = NULL;
  59336. struct test_memio_ctx test_ctx;
  59337. WOLFSSL_ALERT_HISTORY h;
  59338. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59339. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59340. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  59341. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, "DHE-PSK-AES128-GCM-SHA256"),
  59342. WOLFSSL_SUCCESS);
  59343. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_s, "DHE-PSK-AES128-GCM-SHA256"),
  59344. WOLFSSL_SUCCESS);
  59345. wolfSSL_set_psk_server_callback(ssl_s, test_server_psk_cb);
  59346. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59347. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59348. WOLFSSL_ERROR_WANT_READ);
  59349. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  59350. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  59351. WOLFSSL_ERROR_WANT_READ);
  59352. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59353. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59354. WOLFSSL_ERROR_WANT_READ);
  59355. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  59356. ExpectIntEQ(h.last_tx.code, handshake_failure);
  59357. ExpectIntEQ(h.last_tx.level, alert_fatal);
  59358. wolfSSL_free(ssl_c);
  59359. wolfSSL_CTX_free(ctx_c);
  59360. wolfSSL_free(ssl_s);
  59361. wolfSSL_CTX_free(ctx_s);
  59362. return EXPECT_RESULT();
  59363. }
  59364. #else
  59365. static int test_extra_alerts_bad_psk(void)
  59366. {
  59367. return TEST_SKIPPED;
  59368. }
  59369. #endif
  59370. #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  59371. && !defined(NO_PSK)
  59372. static unsigned int test_tls13_bad_psk_binder_client_cb(WOLFSSL* ssl,
  59373. const char* hint, char* identity, unsigned int id_max_len,
  59374. unsigned char* key, unsigned int key_max_len)
  59375. {
  59376. (void)ssl;
  59377. (void)hint;
  59378. (void)key_max_len;
  59379. /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */
  59380. XSTRNCPY(identity, "Client_identity", id_max_len);
  59381. key[0] = 0x20;
  59382. return 1;
  59383. }
  59384. static unsigned int test_tls13_bad_psk_binder_server_cb(WOLFSSL* ssl,
  59385. const char* id, unsigned char* key, unsigned int key_max_len)
  59386. {
  59387. (void)ssl;
  59388. (void)id;
  59389. (void)key_max_len;
  59390. /* zero means error */
  59391. key[0] = 0x10;
  59392. return 1;
  59393. }
  59394. #endif
  59395. static int test_tls13_bad_psk_binder(void)
  59396. {
  59397. EXPECT_DECLS;
  59398. #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  59399. && !defined(NO_PSK)
  59400. WOLFSSL_CTX *ctx_c = NULL;
  59401. WOLFSSL_CTX *ctx_s = NULL;
  59402. WOLFSSL *ssl_c = NULL;
  59403. WOLFSSL *ssl_s = NULL;
  59404. struct test_memio_ctx test_ctx;
  59405. WOLFSSL_ALERT_HISTORY h;
  59406. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59407. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59408. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  59409. wolfSSL_set_psk_client_callback(ssl_c, test_tls13_bad_psk_binder_client_cb);
  59410. wolfSSL_set_psk_server_callback(ssl_s, test_tls13_bad_psk_binder_server_cb);
  59411. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59412. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59413. WOLFSSL_ERROR_WANT_READ);
  59414. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  59415. ExpectIntEQ( wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  59416. BAD_BINDER);
  59417. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59418. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59419. FATAL_ERROR);
  59420. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  59421. ExpectIntEQ(h.last_rx.code, illegal_parameter);
  59422. ExpectIntEQ(h.last_rx.level, alert_fatal);
  59423. wolfSSL_free(ssl_c);
  59424. wolfSSL_CTX_free(ctx_c);
  59425. wolfSSL_free(ssl_s);
  59426. wolfSSL_CTX_free(ctx_s);
  59427. #endif
  59428. return EXPECT_RESULT();
  59429. }
  59430. #if defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  59431. defined(HAVE_IO_TESTS_DEPENDENCIES)
  59432. static int test_harden_no_secure_renegotiation_io_cb(WOLFSSL *ssl, char *buf,
  59433. int sz, void *ctx)
  59434. {
  59435. static int sentServerHello = FALSE;
  59436. if (!sentServerHello) {
  59437. byte renegExt[] = { 0xFF, 0x01, 0x00, 0x01, 0x00 };
  59438. size_t i;
  59439. if (sz < (int)sizeof(renegExt))
  59440. return WOLFSSL_CBIO_ERR_GENERAL;
  59441. /* Remove SCR from ServerHello */
  59442. for (i = 0; i < sz - sizeof(renegExt); i++) {
  59443. if (XMEMCMP(buf + i, renegExt, sizeof(renegExt)) == 0) {
  59444. /* Found the extension. Change it to something unrecognized. */
  59445. buf[i+1] = 0x11;
  59446. break;
  59447. }
  59448. }
  59449. sentServerHello = TRUE;
  59450. }
  59451. return EmbedSend(ssl, buf, sz, ctx);
  59452. }
  59453. static void test_harden_no_secure_renegotiation_ssl_ready(WOLFSSL* ssl)
  59454. {
  59455. wolfSSL_SSLSetIOSend(ssl, test_harden_no_secure_renegotiation_io_cb);
  59456. }
  59457. static void test_harden_no_secure_renegotiation_on_cleanup(WOLFSSL* ssl)
  59458. {
  59459. WOLFSSL_ALERT_HISTORY h;
  59460. AssertIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  59461. AssertIntEQ(h.last_rx.code, handshake_failure);
  59462. AssertIntEQ(h.last_rx.level, alert_fatal);
  59463. }
  59464. static int test_harden_no_secure_renegotiation(void)
  59465. {
  59466. EXPECT_DECLS;
  59467. callback_functions client_cbs, server_cbs;
  59468. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59469. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59470. client_cbs.method = wolfTLSv1_2_client_method;
  59471. server_cbs.method = wolfTLSv1_2_server_method;
  59472. server_cbs.ssl_ready = test_harden_no_secure_renegotiation_ssl_ready;
  59473. server_cbs.on_cleanup = test_harden_no_secure_renegotiation_on_cleanup;
  59474. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  59475. ExpectIntEQ(client_cbs.return_code, TEST_FAIL);
  59476. ExpectIntEQ(client_cbs.last_err, SECURE_RENEGOTIATION_E);
  59477. ExpectIntEQ(server_cbs.return_code, TEST_FAIL);
  59478. ExpectTrue(server_cbs.last_err == SOCKET_ERROR_E ||
  59479. server_cbs.last_err == FATAL_ERROR);
  59480. return EXPECT_RESULT();
  59481. }
  59482. #else
  59483. static int test_harden_no_secure_renegotiation(void)
  59484. {
  59485. return TEST_SKIPPED;
  59486. }
  59487. #endif
  59488. #if defined(HAVE_OCSP) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  59489. static int test_override_alt_cert_chain_cert_cb(int preverify,
  59490. WOLFSSL_X509_STORE_CTX* store)
  59491. {
  59492. fprintf(stderr, "preverify: %d\n", preverify);
  59493. fprintf(stderr, "store->error: %d\n", store->error);
  59494. fprintf(stderr, "error reason: %s\n", wolfSSL_ERR_reason_error_string(store->error));
  59495. if (store->error == OCSP_INVALID_STATUS) {
  59496. fprintf(stderr, "Overriding OCSP error\n");
  59497. return 1;
  59498. }
  59499. #ifndef WOLFSSL_ALT_CERT_CHAINS
  59500. else if ((store->error == ASN_NO_SIGNER_E ||
  59501. store->error == ASN_SELF_SIGNED_E
  59502. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  59503. defined(HAVE_WEBSERVER)
  59504. || store->error == WOLFSSL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
  59505. #endif
  59506. ) && store->error_depth == store->totalCerts - 1) {
  59507. fprintf(stderr, "Overriding no signer error only for root cert\n");
  59508. return 1;
  59509. }
  59510. #endif
  59511. else
  59512. return preverify;
  59513. }
  59514. static int test_override_alt_cert_chain_ocsp_cb(void* ioCtx, const char* url,
  59515. int urlSz, unsigned char* request, int requestSz,
  59516. unsigned char** response)
  59517. {
  59518. (void)ioCtx;
  59519. (void)url;
  59520. (void)urlSz;
  59521. (void)request;
  59522. (void)requestSz;
  59523. (void)response;
  59524. return -1;
  59525. }
  59526. static int test_override_alt_cert_chain_client_ctx_ready(WOLFSSL_CTX* ctx)
  59527. {
  59528. EXPECT_DECLS;
  59529. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER,
  59530. test_override_alt_cert_chain_cert_cb);
  59531. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  59532. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  59533. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  59534. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  59535. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  59536. WOLFSSL_SUCCESS);
  59537. return EXPECT_RESULT();
  59538. }
  59539. static int test_override_alt_cert_chain_client_ctx_ready2(WOLFSSL_CTX* ctx)
  59540. {
  59541. EXPECT_DECLS;
  59542. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  59543. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  59544. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  59545. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  59546. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  59547. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  59548. WOLFSSL_SUCCESS);
  59549. return EXPECT_RESULT();
  59550. }
  59551. static int test_override_alt_cert_chain_server_ctx_ready(WOLFSSL_CTX* ctx)
  59552. {
  59553. EXPECT_DECLS;
  59554. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  59555. "./certs/intermediate/server-chain-alt.pem"), WOLFSSL_SUCCESS);
  59556. return EXPECT_RESULT();
  59557. }
  59558. static int test_override_alt_cert_chain(void)
  59559. {
  59560. EXPECT_DECLS;
  59561. size_t i;
  59562. struct test_params {
  59563. ctx_cb client_ctx_cb;
  59564. ctx_cb server_ctx_cb;
  59565. int result;
  59566. } params[] = {
  59567. {test_override_alt_cert_chain_client_ctx_ready,
  59568. test_override_alt_cert_chain_server_ctx_ready, TEST_SUCCESS},
  59569. {test_override_alt_cert_chain_client_ctx_ready2,
  59570. test_override_alt_cert_chain_server_ctx_ready, TEST_FAIL},
  59571. };
  59572. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  59573. test_ssl_cbf client_cbs, server_cbs;
  59574. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59575. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59576. fprintf(stderr, "test config: %d\n", (int)i);
  59577. client_cbs.ctx_ready = params[i].client_ctx_cb;
  59578. server_cbs.ctx_ready = params[i].server_ctx_cb;
  59579. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  59580. &server_cbs, NULL), params[i].result);
  59581. ExpectIntEQ(client_cbs.return_code, params[i].result);
  59582. ExpectIntEQ(server_cbs.return_code, params[i].result);
  59583. }
  59584. return EXPECT_RESULT();
  59585. }
  59586. #else
  59587. static int test_override_alt_cert_chain(void)
  59588. {
  59589. return TEST_SKIPPED;
  59590. }
  59591. #endif
  59592. #if defined(HAVE_RPK)
  59593. #define svrRpkCertFile "./certs/rpk/server-cert-rpk.der"
  59594. #define clntRpkCertFile "./certs/rpk/client-cert-rpk.der"
  59595. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  59596. static int MyRpkVerifyCb(int mode, WOLFSSL_X509_STORE_CTX* strctx)
  59597. {
  59598. int ret = WOLFSSL_SUCCESS;
  59599. (void)mode;
  59600. (void)strctx;
  59601. WOLFSSL_ENTER("MyRpkVerifyCb");
  59602. return ret;
  59603. }
  59604. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  59605. static WC_INLINE int test_rpk_memio_setup(
  59606. struct test_memio_ctx *ctx,
  59607. WOLFSSL_CTX **ctx_c,
  59608. WOLFSSL_CTX **ctx_s,
  59609. WOLFSSL **ssl_c,
  59610. WOLFSSL **ssl_s,
  59611. method_provider method_c,
  59612. method_provider method_s,
  59613. const char* certfile_c, int fmt_cc, /* client cert file path and format */
  59614. const char* certfile_s, int fmt_cs, /* server cert file path and format */
  59615. const char* pkey_c, int fmt_kc, /* client private key and format */
  59616. const char* pkey_s, int fmt_ks /* server private key and format */
  59617. )
  59618. {
  59619. int ret;
  59620. if (ctx_c != NULL && *ctx_c == NULL) {
  59621. *ctx_c = wolfSSL_CTX_new(method_c());
  59622. if (*ctx_c == NULL) {
  59623. return -1;
  59624. }
  59625. wolfSSL_CTX_set_verify(*ctx_c, WOLFSSL_VERIFY_PEER, NULL);
  59626. ret = wolfSSL_CTX_load_verify_locations(*ctx_c, caCertFile, 0);
  59627. if (ret != WOLFSSL_SUCCESS) {
  59628. return -1;
  59629. }
  59630. wolfSSL_SetIORecv(*ctx_c, test_memio_read_cb);
  59631. wolfSSL_SetIOSend(*ctx_c, test_memio_write_cb);
  59632. ret = wolfSSL_CTX_use_certificate_file(*ctx_c, certfile_c, fmt_cc);
  59633. if (ret != WOLFSSL_SUCCESS) {
  59634. return -1;
  59635. }
  59636. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_c, pkey_c, fmt_kc);
  59637. if (ret != WOLFSSL_SUCCESS) {
  59638. return -1;
  59639. }
  59640. }
  59641. if (ctx_s != NULL && *ctx_s == NULL) {
  59642. *ctx_s = wolfSSL_CTX_new(method_s());
  59643. if (*ctx_s == NULL) {
  59644. return -1;
  59645. }
  59646. wolfSSL_CTX_set_verify(*ctx_s, WOLFSSL_VERIFY_PEER, NULL);
  59647. ret = wolfSSL_CTX_load_verify_locations(*ctx_s, cliCertFile, 0);
  59648. if (ret != WOLFSSL_SUCCESS) {
  59649. return -1;
  59650. }
  59651. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_s, pkey_s, fmt_ks);
  59652. if (ret != WOLFSSL_SUCCESS) {
  59653. return -1;
  59654. }
  59655. ret = wolfSSL_CTX_use_certificate_file(*ctx_s, certfile_s, fmt_cs);
  59656. if (ret != WOLFSSL_SUCCESS) {
  59657. return -1;
  59658. }
  59659. wolfSSL_SetIORecv(*ctx_s, test_memio_read_cb);
  59660. wolfSSL_SetIOSend(*ctx_s, test_memio_write_cb);
  59661. if (ctx->s_ciphers != NULL) {
  59662. ret = wolfSSL_CTX_set_cipher_list(*ctx_s, ctx->s_ciphers);
  59663. if (ret != WOLFSSL_SUCCESS) {
  59664. return -1;
  59665. }
  59666. }
  59667. }
  59668. if (ctx_c != NULL && ssl_c != NULL) {
  59669. *ssl_c = wolfSSL_new(*ctx_c);
  59670. if (*ssl_c == NULL) {
  59671. return -1;
  59672. }
  59673. wolfSSL_SetIOWriteCtx(*ssl_c, ctx);
  59674. wolfSSL_SetIOReadCtx(*ssl_c, ctx);
  59675. }
  59676. if (ctx_s != NULL && ssl_s != NULL) {
  59677. *ssl_s = wolfSSL_new(*ctx_s);
  59678. if (*ssl_s == NULL) {
  59679. return -1;
  59680. }
  59681. wolfSSL_SetIOWriteCtx(*ssl_s, ctx);
  59682. wolfSSL_SetIOReadCtx(*ssl_s, ctx);
  59683. #if !defined(NO_DH)
  59684. SetDH(*ssl_s);
  59685. #endif
  59686. }
  59687. return 0;
  59688. }
  59689. #endif /* HAVE_RPK */
  59690. static int test_rpk_set_xxx_cert_type(void)
  59691. {
  59692. EXPECT_DECLS;
  59693. #if defined(HAVE_RPK)
  59694. char ctype[MAX_CLIENT_CERT_TYPE_CNT + 1]; /* prepare bigger buffer */
  59695. WOLFSSL_CTX* ctx = NULL;
  59696. WOLFSSL* ssl = NULL;
  59697. int tp;
  59698. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  59699. ExpectNotNull(ctx);
  59700. ssl = wolfSSL_new(ctx);
  59701. ExpectNotNull(ssl);
  59702. /*--------------------------------------------*/
  59703. /* tests for wolfSSL_CTX_set_client_cert_type */
  59704. /*--------------------------------------------*/
  59705. /* illegal parameter test caces */
  59706. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(NULL, ctype,
  59707. MAX_CLIENT_CERT_TYPE_CNT),
  59708. BAD_FUNC_ARG);
  59709. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59710. sizeof(ctype)),
  59711. BAD_FUNC_ARG);
  59712. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  59713. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  59714. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59715. MAX_CLIENT_CERT_TYPE_CNT),
  59716. BAD_FUNC_ARG);
  59717. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  59718. ctype[1] = 10; /* set unknown cert type */
  59719. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59720. MAX_CLIENT_CERT_TYPE_CNT),
  59721. BAD_FUNC_ARG);
  59722. /* pass larger type count */
  59723. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  59724. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  59725. ctype[2] = 1; /* pass unacceptable type count */
  59726. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59727. MAX_CLIENT_CERT_TYPE_CNT + 1),
  59728. BAD_FUNC_ARG);
  59729. /* should accept NULL for type buffer */
  59730. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, NULL,
  59731. MAX_CLIENT_CERT_TYPE_CNT),
  59732. WOLFSSL_SUCCESS);
  59733. /* should accept zero for type count */
  59734. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59735. 0),
  59736. WOLFSSL_SUCCESS);
  59737. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59738. MAX_CLIENT_CERT_TYPE_CNT),
  59739. WOLFSSL_SUCCESS);
  59740. /*--------------------------------------------*/
  59741. /* tests for wolfSSL_CTX_set_server_cert_type */
  59742. /*--------------------------------------------*/
  59743. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(NULL, ctype,
  59744. MAX_SERVER_CERT_TYPE_CNT),
  59745. BAD_FUNC_ARG);
  59746. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59747. sizeof(ctype)),
  59748. BAD_FUNC_ARG);
  59749. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  59750. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  59751. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59752. MAX_SERVER_CERT_TYPE_CNT),
  59753. BAD_FUNC_ARG);
  59754. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  59755. ctype[1] = 10; /* set unknown cert type */
  59756. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59757. MAX_SERVER_CERT_TYPE_CNT),
  59758. BAD_FUNC_ARG);
  59759. /* pass larger type count */
  59760. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  59761. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  59762. ctype[2] = 1; /* pass unacceptable type count */
  59763. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59764. MAX_SERVER_CERT_TYPE_CNT + 1),
  59765. BAD_FUNC_ARG);
  59766. /* should accept NULL for type buffer */
  59767. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, NULL,
  59768. MAX_SERVER_CERT_TYPE_CNT),
  59769. WOLFSSL_SUCCESS);
  59770. /* should accept zero for type count */
  59771. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59772. 0),
  59773. WOLFSSL_SUCCESS);
  59774. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59775. MAX_CLIENT_CERT_TYPE_CNT),
  59776. WOLFSSL_SUCCESS);
  59777. /*--------------------------------------------*/
  59778. /* tests for wolfSSL_set_client_cert_type */
  59779. /*--------------------------------------------*/
  59780. ExpectIntEQ(wolfSSL_set_client_cert_type(NULL, ctype,
  59781. MAX_CLIENT_CERT_TYPE_CNT),
  59782. BAD_FUNC_ARG);
  59783. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59784. sizeof(ctype)),
  59785. BAD_FUNC_ARG);
  59786. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  59787. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  59788. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59789. MAX_CLIENT_CERT_TYPE_CNT),
  59790. BAD_FUNC_ARG);
  59791. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  59792. ctype[1] = 10; /* set unknown cert type */
  59793. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59794. MAX_CLIENT_CERT_TYPE_CNT),
  59795. BAD_FUNC_ARG);
  59796. /* pass larger type count */
  59797. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  59798. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  59799. ctype[2] = 1; /* pass unacceptable type count */
  59800. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59801. MAX_CLIENT_CERT_TYPE_CNT + 1),
  59802. BAD_FUNC_ARG);
  59803. /* should accept NULL for type buffer */
  59804. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, NULL,
  59805. MAX_CLIENT_CERT_TYPE_CNT),
  59806. WOLFSSL_SUCCESS);
  59807. /* should accept zero for type count */
  59808. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59809. 0),
  59810. WOLFSSL_SUCCESS);
  59811. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59812. MAX_CLIENT_CERT_TYPE_CNT),
  59813. WOLFSSL_SUCCESS);
  59814. /*--------------------------------------------*/
  59815. /* tests for wolfSSL_CTX_set_server_cert_type */
  59816. /*--------------------------------------------*/
  59817. ExpectIntEQ(wolfSSL_set_server_cert_type(NULL, ctype,
  59818. MAX_SERVER_CERT_TYPE_CNT),
  59819. BAD_FUNC_ARG);
  59820. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59821. sizeof(ctype)),
  59822. BAD_FUNC_ARG);
  59823. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  59824. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  59825. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59826. MAX_SERVER_CERT_TYPE_CNT),
  59827. BAD_FUNC_ARG);
  59828. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  59829. ctype[1] = 10; /* set unknown cert type */
  59830. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59831. MAX_SERVER_CERT_TYPE_CNT),
  59832. BAD_FUNC_ARG);
  59833. /* pass larger type count */
  59834. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  59835. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  59836. ctype[2] = 1; /* pass unacceptable type count */
  59837. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59838. MAX_SERVER_CERT_TYPE_CNT + 1),
  59839. BAD_FUNC_ARG);
  59840. /* should accept NULL for type buffer */
  59841. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, NULL,
  59842. MAX_SERVER_CERT_TYPE_CNT),
  59843. WOLFSSL_SUCCESS);
  59844. /* should accept zero for type count */
  59845. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59846. 0),
  59847. WOLFSSL_SUCCESS);
  59848. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59849. MAX_SERVER_CERT_TYPE_CNT),
  59850. WOLFSSL_SUCCESS);
  59851. /*------------------------------------------------*/
  59852. /* tests for wolfSSL_get_negotiated_xxx_cert_type */
  59853. /*------------------------------------------------*/
  59854. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(NULL, &tp),
  59855. BAD_FUNC_ARG);
  59856. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl, NULL),
  59857. BAD_FUNC_ARG);
  59858. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(NULL, &tp),
  59859. BAD_FUNC_ARG);
  59860. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl, NULL),
  59861. BAD_FUNC_ARG);
  59862. /* clean up */
  59863. wolfSSL_free(ssl);
  59864. wolfSSL_CTX_free(ctx);
  59865. #endif
  59866. return EXPECT_RESULT();
  59867. }
  59868. static int test_tls13_rpk_handshake(void)
  59869. {
  59870. EXPECT_DECLS;
  59871. #if defined(HAVE_RPK)
  59872. int ret = 0;
  59873. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  59874. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  59875. struct test_memio_ctx test_ctx;
  59876. int err;
  59877. char certType_c[MAX_CLIENT_CERT_TYPE_CNT];
  59878. char certType_s[MAX_CLIENT_CERT_TYPE_CNT];
  59879. int typeCnt_c;
  59880. int typeCnt_s;
  59881. int tp;
  59882. (void)err;
  59883. (void)typeCnt_c;
  59884. (void)typeCnt_s;
  59885. (void)certType_c;
  59886. (void)certType_s;
  59887. /* TLS1.2
  59888. * Both client and server load x509 cert and start handshaking.
  59889. * Check no negotiation occurred.
  59890. */
  59891. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59892. ExpectIntEQ(
  59893. test_rpk_memio_setup(
  59894. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59895. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  59896. cliCertFile, WOLFSSL_FILETYPE_PEM,
  59897. svrCertFile, WOLFSSL_FILETYPE_PEM,
  59898. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  59899. svrKeyFile, WOLFSSL_FILETYPE_PEM)
  59900. , 0);
  59901. /* set client certificate type in client end */
  59902. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59903. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  59904. typeCnt_c = 2;
  59905. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  59906. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  59907. typeCnt_s = 2;
  59908. /* both client and server do not call client/server_cert_type APIs,
  59909. * expecting default settings works and no negotiation performed.
  59910. */
  59911. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  59912. return TEST_FAIL;
  59913. /* confirm no negotiation occurred */
  59914. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  59915. WOLFSSL_SUCCESS);
  59916. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59917. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  59918. WOLFSSL_SUCCESS);
  59919. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59920. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  59921. WOLFSSL_SUCCESS);
  59922. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59923. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  59924. WOLFSSL_SUCCESS);
  59925. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59926. (void)typeCnt_c;
  59927. (void)typeCnt_s;
  59928. wolfSSL_free(ssl_c);
  59929. wolfSSL_CTX_free(ctx_c);
  59930. wolfSSL_free(ssl_s);
  59931. wolfSSL_CTX_free(ctx_s);
  59932. ssl_c = ssl_s = NULL;
  59933. ctx_c = ctx_s = NULL;
  59934. /* Both client and server load x509 cert and start handshaking.
  59935. * Check no negotiation occurred.
  59936. */
  59937. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59938. ExpectIntEQ(
  59939. test_rpk_memio_setup(
  59940. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59941. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  59942. cliCertFile, WOLFSSL_FILETYPE_PEM,
  59943. svrCertFile, WOLFSSL_FILETYPE_PEM,
  59944. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  59945. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  59946. , 0);
  59947. /* set client certificate type in client end */
  59948. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59949. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  59950. typeCnt_c = 2;
  59951. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  59952. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  59953. typeCnt_s = 2;
  59954. /* both client and server do not call client/server_cert_type APIs,
  59955. * expecting default settings works and no negotiation performed.
  59956. */
  59957. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  59958. return TEST_FAIL;
  59959. /* confirm no negotiation occurred */
  59960. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  59961. WOLFSSL_SUCCESS);
  59962. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59963. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  59964. WOLFSSL_SUCCESS);
  59965. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59966. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  59967. WOLFSSL_SUCCESS);
  59968. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59969. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  59970. WOLFSSL_SUCCESS);
  59971. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  59972. (void)typeCnt_c;
  59973. (void)typeCnt_s;
  59974. wolfSSL_free(ssl_c);
  59975. wolfSSL_CTX_free(ctx_c);
  59976. wolfSSL_free(ssl_s);
  59977. wolfSSL_CTX_free(ctx_s);
  59978. ssl_c = ssl_s = NULL;
  59979. ctx_c = ctx_s = NULL;
  59980. /* Both client and server load RPK cert and start handshaking.
  59981. * Confirm negotiated cert types match as expected.
  59982. */
  59983. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59984. ExpectIntEQ(
  59985. test_rpk_memio_setup(
  59986. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59987. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  59988. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  59989. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  59990. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  59991. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  59992. , 0);
  59993. /* set client certificate type in client end */
  59994. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  59995. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  59996. typeCnt_c = 2;
  59997. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  59998. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  59999. typeCnt_s = 2;
  60000. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60001. WOLFSSL_SUCCESS);
  60002. /* set server certificate type in client end */
  60003. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60004. WOLFSSL_SUCCESS);
  60005. /* set client certificate type in server end */
  60006. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60007. WOLFSSL_SUCCESS);
  60008. /* set server certificate type in server end */
  60009. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60010. WOLFSSL_SUCCESS);
  60011. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  60012. return TEST_FAIL;
  60013. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60014. WOLFSSL_SUCCESS);
  60015. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60016. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60017. WOLFSSL_SUCCESS);
  60018. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60019. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60020. WOLFSSL_SUCCESS);
  60021. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60022. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60023. WOLFSSL_SUCCESS);
  60024. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60025. wolfSSL_free(ssl_c);
  60026. wolfSSL_CTX_free(ctx_c);
  60027. wolfSSL_free(ssl_s);
  60028. wolfSSL_CTX_free(ctx_s);
  60029. ssl_c = ssl_s = NULL;
  60030. ctx_c = ctx_s = NULL;
  60031. /* TLS1.2
  60032. * Both client and server load RPK cert and start handshaking.
  60033. * Confirm negotiated cert types match as expected.
  60034. */
  60035. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60036. ExpectIntEQ(
  60037. test_rpk_memio_setup(
  60038. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60039. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  60040. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60041. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60042. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60043. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60044. , 0);
  60045. /* set client certificate type in client end */
  60046. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60047. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60048. typeCnt_c = 2;
  60049. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  60050. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  60051. typeCnt_s = 2;
  60052. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60053. WOLFSSL_SUCCESS);
  60054. /* set server certificate type in client end */
  60055. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60056. WOLFSSL_SUCCESS);
  60057. /* set client certificate type in server end */
  60058. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60059. WOLFSSL_SUCCESS);
  60060. /* set server certificate type in server end */
  60061. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60062. WOLFSSL_SUCCESS);
  60063. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  60064. return TEST_FAIL;
  60065. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60066. WOLFSSL_SUCCESS);
  60067. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60068. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60069. WOLFSSL_SUCCESS);
  60070. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60071. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60072. WOLFSSL_SUCCESS);
  60073. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60074. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60075. WOLFSSL_SUCCESS);
  60076. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60077. wolfSSL_free(ssl_c);
  60078. wolfSSL_CTX_free(ctx_c);
  60079. wolfSSL_free(ssl_s);
  60080. wolfSSL_CTX_free(ctx_s);
  60081. ssl_c = ssl_s = NULL;
  60082. ctx_c = ctx_s = NULL;
  60083. /* Both client and server load x509 cert.
  60084. * Have client call set_client_cert_type with both RPK and x509.
  60085. * This doesn't makes client add client cert type extension to ClientHello,
  60086. * since it does not load RPK cert actually.
  60087. */
  60088. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60089. ExpectIntEQ(
  60090. test_rpk_memio_setup(
  60091. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60092. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60093. cliCertFile, WOLFSSL_FILETYPE_PEM,
  60094. svrCertFile, WOLFSSL_FILETYPE_PEM,
  60095. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60096. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60097. , 0);
  60098. /* set client certificate type in client end
  60099. *
  60100. * client indicates both RPK and x509 certs are available but loaded RPK
  60101. * cert only. It does not have client add client-cert-type extension in CH.
  60102. */
  60103. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60104. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60105. typeCnt_c = 2;
  60106. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60107. WOLFSSL_SUCCESS);
  60108. /* client indicates both RPK and x509 certs are acceptable */
  60109. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  60110. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  60111. typeCnt_s = 2;
  60112. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60113. WOLFSSL_SUCCESS);
  60114. /* server indicates both RPK and x509 certs are acceptable */
  60115. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60116. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60117. typeCnt_c = 2;
  60118. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60119. WOLFSSL_SUCCESS);
  60120. /* server should indicate only RPK cert is available */
  60121. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60122. certType_s[1] = -1;
  60123. typeCnt_s = 1;
  60124. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60125. WOLFSSL_SUCCESS);
  60126. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  60127. return TEST_FAIL;
  60128. /* Negotiation for client-cert-type should NOT happen. Therefore -1 should
  60129. * be returned as cert type.
  60130. */
  60131. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60132. WOLFSSL_SUCCESS);
  60133. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60134. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60135. WOLFSSL_SUCCESS);
  60136. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60137. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60138. WOLFSSL_SUCCESS);
  60139. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60140. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60141. WOLFSSL_SUCCESS);
  60142. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60143. wolfSSL_free(ssl_c);
  60144. wolfSSL_CTX_free(ctx_c);
  60145. wolfSSL_free(ssl_s);
  60146. wolfSSL_CTX_free(ctx_s);
  60147. ssl_c = ssl_s = NULL;
  60148. ctx_c = ctx_s = NULL;
  60149. /* Have client load RPK cert and have server load x509 cert.
  60150. * Check the negotiation result from both ends.
  60151. */
  60152. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60153. ExpectIntEQ(
  60154. test_rpk_memio_setup(
  60155. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60156. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60157. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60158. svrCertFile, WOLFSSL_FILETYPE_PEM,
  60159. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60160. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60161. , 0);
  60162. /* have client tell to use RPK cert */
  60163. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60164. certType_c[1] = -1;
  60165. typeCnt_c = 1;
  60166. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60167. WOLFSSL_SUCCESS);
  60168. /* have client tell to accept both RPK and x509 cert */
  60169. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60170. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  60171. typeCnt_s = 2;
  60172. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60173. WOLFSSL_SUCCESS);
  60174. /* have server accept to both RPK and x509 cert */
  60175. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  60176. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  60177. typeCnt_c = 2;
  60178. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60179. WOLFSSL_SUCCESS);
  60180. /* does not call wolfSSL_set_server_cert_type intentionally in sesrver
  60181. * end, expecting the default setting works.
  60182. */
  60183. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  60184. return TEST_FAIL;
  60185. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60186. WOLFSSL_SUCCESS);
  60187. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60188. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60189. WOLFSSL_SUCCESS);
  60190. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60191. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60192. WOLFSSL_SUCCESS);
  60193. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60194. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60195. WOLFSSL_SUCCESS);
  60196. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60197. wolfSSL_free(ssl_c);
  60198. wolfSSL_CTX_free(ctx_c);
  60199. wolfSSL_free(ssl_s);
  60200. wolfSSL_CTX_free(ctx_s);
  60201. ssl_c = ssl_s = NULL;
  60202. ctx_c = ctx_s = NULL;
  60203. /* Have both client and server load RPK cert, however, have server
  60204. * indicate its cert type x509.
  60205. * Client is expected to detect the cert type mismatch then to send alert
  60206. * with "unsupported_certificate".
  60207. */
  60208. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60209. ExpectIntEQ(
  60210. test_rpk_memio_setup(
  60211. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60212. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60213. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60214. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1, /* server sends RPK cert */
  60215. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60216. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60217. , 0);
  60218. /* have client tell to use RPK cert */
  60219. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60220. certType_c[1] = -1;
  60221. typeCnt_c = 1;
  60222. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60223. WOLFSSL_SUCCESS);
  60224. /* have client tell to accept both RPK and x509 cert */
  60225. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60226. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  60227. typeCnt_s = 2;
  60228. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60229. WOLFSSL_SUCCESS);
  60230. /* have server accept to both RPK and x509 cert */
  60231. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  60232. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  60233. typeCnt_c = 2;
  60234. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60235. WOLFSSL_SUCCESS);
  60236. /* have server tell to use x509 cert intentionally. This will bring
  60237. * certificate type mismatch in client side.
  60238. */
  60239. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60240. certType_s[1] = -1;
  60241. typeCnt_s = 1;
  60242. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60243. WOLFSSL_SUCCESS);
  60244. /* expect client detect cert type mismatch then send Alert */
  60245. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  60246. if (ret != -1)
  60247. return TEST_FAIL;
  60248. ExpectIntEQ(wolfSSL_get_error(ssl_c, ret), UNSUPPORTED_CERTIFICATE);
  60249. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60250. WOLFSSL_SUCCESS);
  60251. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60252. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60253. WOLFSSL_SUCCESS);
  60254. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60255. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60256. WOLFSSL_SUCCESS);
  60257. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60258. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60259. WOLFSSL_SUCCESS);
  60260. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60261. wolfSSL_free(ssl_c);
  60262. wolfSSL_CTX_free(ctx_c);
  60263. wolfSSL_free(ssl_s);
  60264. wolfSSL_CTX_free(ctx_s);
  60265. ssl_c = ssl_s = NULL;
  60266. ctx_c = ctx_s = NULL;
  60267. /* Have client load x509 cert and server load RPK cert,
  60268. * however, have client indicate its cert type RPK.
  60269. * Server is expected to detect the cert type mismatch then to send alert
  60270. * with "unsupported_certificate".
  60271. */
  60272. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60273. ExpectIntEQ(
  60274. test_rpk_memio_setup(
  60275. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60276. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60277. cliCertFile, WOLFSSL_FILETYPE_PEM,
  60278. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60279. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60280. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60281. , 0);
  60282. /* have client tell to use RPK cert intentionally */
  60283. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60284. certType_c[1] = -1;
  60285. typeCnt_c = 1;
  60286. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60287. WOLFSSL_SUCCESS);
  60288. /* have client tell to accept both RPK and x509 cert */
  60289. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60290. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  60291. typeCnt_s = 2;
  60292. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60293. WOLFSSL_SUCCESS);
  60294. /* have server accept to both RPK and x509 cert */
  60295. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  60296. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  60297. typeCnt_c = 2;
  60298. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60299. WOLFSSL_SUCCESS);
  60300. /* have server tell to use x509 cert intentionally. This will bring
  60301. * certificate type mismatch in client side.
  60302. */
  60303. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60304. certType_s[1] = -1;
  60305. typeCnt_s = 1;
  60306. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60307. WOLFSSL_SUCCESS);
  60308. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  60309. /* expect server detect cert type mismatch then send Alert */
  60310. ExpectIntNE(ret, 0);
  60311. err = wolfSSL_get_error(ssl_c, ret);
  60312. ExpectIntEQ(err, UNSUPPORTED_CERTIFICATE);
  60313. /* client did not load RPK cert actually, so negotiation did not happen */
  60314. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60315. WOLFSSL_SUCCESS);
  60316. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60317. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60318. WOLFSSL_SUCCESS);
  60319. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60320. /* client did not load RPK cert actually, so negotiation did not happen */
  60321. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60322. WOLFSSL_SUCCESS);
  60323. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60324. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60325. WOLFSSL_SUCCESS);
  60326. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60327. wolfSSL_free(ssl_c);
  60328. wolfSSL_CTX_free(ctx_c);
  60329. wolfSSL_free(ssl_s);
  60330. wolfSSL_CTX_free(ctx_s);
  60331. ssl_c = ssl_s = NULL;
  60332. ctx_c = ctx_s = NULL;
  60333. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  60334. /* Both client and server load RPK cert and set certificate verify
  60335. * callbacks then start handshaking.
  60336. * Confirm both side can refer the peer's cert.
  60337. */
  60338. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60339. ExpectIntEQ(
  60340. test_rpk_memio_setup(
  60341. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60342. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60343. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60344. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60345. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60346. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60347. , 0);
  60348. /* set client certificate type in client end */
  60349. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60350. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60351. typeCnt_c = 2;
  60352. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  60353. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  60354. typeCnt_s = 2;
  60355. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60356. WOLFSSL_SUCCESS);
  60357. /* set server certificate type in client end */
  60358. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60359. WOLFSSL_SUCCESS);
  60360. /* set client certificate type in server end */
  60361. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60362. WOLFSSL_SUCCESS);
  60363. /* set server certificate type in server end */
  60364. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60365. WOLFSSL_SUCCESS);
  60366. /* set certificate verify callback to both client and server */
  60367. int isServer = 0;
  60368. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  60369. wolfSSL_set_verify(ssl_c, SSL_VERIFY_PEER, MyRpkVerifyCb);
  60370. isServer = 1;
  60371. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  60372. wolfSSL_set_verify(ssl_s, SSL_VERIFY_PEER, MyRpkVerifyCb);
  60373. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  60374. if (ret != 0)
  60375. return TEST_FAIL;
  60376. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60377. WOLFSSL_SUCCESS);
  60378. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60379. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60380. WOLFSSL_SUCCESS);
  60381. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60382. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60383. WOLFSSL_SUCCESS);
  60384. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60385. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60386. WOLFSSL_SUCCESS);
  60387. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60388. wolfSSL_free(ssl_c);
  60389. wolfSSL_CTX_free(ctx_c);
  60390. wolfSSL_free(ssl_s);
  60391. wolfSSL_CTX_free(ctx_s);
  60392. ssl_c = ssl_s = NULL;
  60393. ctx_c = ctx_s = NULL;
  60394. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  60395. #endif /* HAVE_RPK */
  60396. return EXPECT_RESULT();
  60397. }
  60398. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  60399. static int test_dtls13_bad_epoch_ch(void)
  60400. {
  60401. EXPECT_DECLS;
  60402. WOLFSSL_CTX *ctx_c = NULL;
  60403. WOLFSSL_CTX *ctx_s = NULL;
  60404. WOLFSSL *ssl_c = NULL;
  60405. WOLFSSL *ssl_s = NULL;
  60406. struct test_memio_ctx test_ctx;
  60407. const int EPOCH_OFF = 3;
  60408. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60409. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60410. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  60411. /* disable hrr cookie so we can later check msgsReceived.got_client_hello
  60412. * with just one message */
  60413. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  60414. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60415. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60416. WOLFSSL_ERROR_WANT_READ);
  60417. ExpectIntGE(test_ctx.s_len, EPOCH_OFF + 2);
  60418. /* first CH should use epoch 0x0 */
  60419. ExpectTrue((test_ctx.s_buff[EPOCH_OFF] == 0x0) &&
  60420. (test_ctx.s_buff[EPOCH_OFF + 1] == 0x0));
  60421. /* change epoch to 2 */
  60422. test_ctx.s_buff[EPOCH_OFF + 1] = 0x2;
  60423. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  60424. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  60425. WOLFSSL_ERROR_WANT_READ);
  60426. ExpectIntNE(ssl_s->msgsReceived.got_client_hello, 1);
  60427. /* resend the CH */
  60428. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  60429. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60430. wolfSSL_free(ssl_c);
  60431. wolfSSL_CTX_free(ctx_c);
  60432. wolfSSL_free(ssl_s);
  60433. wolfSSL_CTX_free(ctx_s);
  60434. return EXPECT_RESULT();
  60435. }
  60436. #else
  60437. static int test_dtls13_bad_epoch_ch(void)
  60438. {
  60439. return TEST_SKIPPED;
  60440. }
  60441. #endif
  60442. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_SESSION_CACHE)
  60443. static int test_short_session_id_ssl_ready(WOLFSSL* ssl)
  60444. {
  60445. EXPECT_DECLS;
  60446. WOLFSSL_SESSION *sess = NULL;
  60447. /* Setup the session to avoid errors */
  60448. ssl->session->timeout = -1;
  60449. ssl->session->side = WOLFSSL_CLIENT_END;
  60450. #if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
  60451. defined(HAVE_SESSION_TICKET))
  60452. ssl->session->version = ssl->version;
  60453. #endif
  60454. /* Force a short session ID to be sent */
  60455. ssl->session->sessionIDSz = 4;
  60456. #ifndef NO_SESSION_CACHE_REF
  60457. /* Allow the client cache to be used */
  60458. ssl->session->idLen = 4;
  60459. #endif
  60460. ssl->session->isSetup = 1;
  60461. ExpectNotNull(sess = wolfSSL_get_session(ssl));
  60462. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  60463. return EXPECT_RESULT();
  60464. }
  60465. static int test_short_session_id(void)
  60466. {
  60467. EXPECT_DECLS;
  60468. test_ssl_cbf client_cbf;
  60469. test_ssl_cbf server_cbf;
  60470. size_t i;
  60471. struct {
  60472. method_provider client_meth;
  60473. method_provider server_meth;
  60474. const char* tls_version;
  60475. } params[] = {
  60476. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  60477. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID) && \
  60478. !defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
  60479. /* With WOLFSSL_TLS13_MIDDLEBOX_COMPAT a short ID will result in an error */
  60480. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  60481. #ifdef WOLFSSL_DTLS13
  60482. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  60483. #endif
  60484. #endif
  60485. #ifndef WOLFSSL_NO_TLS12
  60486. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  60487. #ifdef WOLFSSL_DTLS
  60488. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  60489. #endif
  60490. #endif
  60491. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  60492. !defined(NO_DES3))
  60493. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  60494. #ifdef WOLFSSL_DTLS
  60495. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  60496. #endif
  60497. #endif
  60498. };
  60499. fprintf(stderr, "\n");
  60500. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  60501. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  60502. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  60503. fprintf(stderr, "\tTesting short ID with %s\n", params[i].tls_version);
  60504. client_cbf.ssl_ready = test_short_session_id_ssl_ready;
  60505. client_cbf.method = params[i].client_meth;
  60506. server_cbf.method = params[i].server_meth;
  60507. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  60508. &server_cbf, NULL), TEST_SUCCESS);
  60509. }
  60510. return EXPECT_RESULT();
  60511. }
  60512. #else
  60513. static int test_short_session_id(void)
  60514. {
  60515. return TEST_SKIPPED;
  60516. }
  60517. #endif
  60518. #if defined(HAVE_NULL_CIPHER) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) \
  60519. && defined(WOLFSSL_DTLS13)
  60520. static byte* test_find_string(const char *string,
  60521. byte *buf, int buf_size)
  60522. {
  60523. int string_size, i;
  60524. string_size = (int)XSTRLEN(string);
  60525. for (i = 0; i < buf_size - string_size - 1; i++) {
  60526. if (XSTRCMP((char*)&buf[i], string) == 0)
  60527. return &buf[i];
  60528. }
  60529. return NULL;
  60530. }
  60531. static int test_wolfSSL_dtls13_null_cipher(void)
  60532. {
  60533. EXPECT_DECLS;
  60534. WOLFSSL_CTX *ctx_c = NULL;
  60535. WOLFSSL_CTX *ctx_s = NULL;
  60536. WOLFSSL *ssl_c = NULL;
  60537. WOLFSSL *ssl_s = NULL;
  60538. struct test_memio_ctx test_ctx;
  60539. const char *test_str = "test";
  60540. int test_str_size;
  60541. byte buf[255], *ptr = NULL;
  60542. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60543. test_ctx.c_ciphers = test_ctx.s_ciphers = "TLS13-SHA256-SHA256";
  60544. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60545. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  60546. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60547. test_str_size = XSTRLEN("test") + 1;
  60548. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  60549. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  60550. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  60551. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  60552. /* check that the packet was sent cleartext */
  60553. ExpectNotNull(ptr = test_find_string(test_str, test_ctx.s_buff,
  60554. test_ctx.s_len));
  60555. if (ptr != NULL) {
  60556. /* modify the message */
  60557. *ptr = 'H';
  60558. /* bad messages should be ignored in DTLS */
  60559. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), -1);
  60560. ExpectIntEQ(ssl_s->error, WANT_READ);
  60561. }
  60562. wolfSSL_free(ssl_c);
  60563. wolfSSL_free(ssl_s);
  60564. wolfSSL_CTX_free(ctx_c);
  60565. wolfSSL_CTX_free(ctx_s);
  60566. return TEST_SUCCESS;
  60567. }
  60568. #else
  60569. static int test_wolfSSL_dtls13_null_cipher(void)
  60570. {
  60571. return TEST_SKIPPED;
  60572. }
  60573. #endif
  60574. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  60575. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  60576. !defined(SINGLE_THREADED) && !defined(NO_RSA)
  60577. static int test_dtls_msg_get_connected_port(int fd, word16 *port)
  60578. {
  60579. SOCKADDR_S peer;
  60580. XSOCKLENT len;
  60581. int ret;
  60582. XMEMSET((byte*)&peer, 0, sizeof(peer));
  60583. len = sizeof(peer);
  60584. ret = getpeername(fd, (SOCKADDR*)&peer, &len);
  60585. if (ret != 0 || len > (XSOCKLENT)sizeof(peer))
  60586. return -1;
  60587. switch (peer.ss_family) {
  60588. #ifdef WOLFSSL_IPV6
  60589. case WOLFSSL_IP6: {
  60590. *port = ntohs(((SOCKADDR_IN6*)&peer)->sin6_port);
  60591. break;
  60592. }
  60593. #endif /* WOLFSSL_IPV6 */
  60594. case WOLFSSL_IP4:
  60595. *port = ntohs(((SOCKADDR_IN*)&peer)->sin_port);
  60596. break;
  60597. default:
  60598. return -1;
  60599. }
  60600. return 0;
  60601. }
  60602. static int test_dtls_msg_from_other_peer_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  60603. {
  60604. char buf[1] = {'t'};
  60605. SOCKADDR_IN_T addr;
  60606. int sock_fd;
  60607. word16 port;
  60608. int err;
  60609. (void)ssl;
  60610. (void)ctx;
  60611. if (ssl == NULL)
  60612. return -1;
  60613. err = test_dtls_msg_get_connected_port(wolfSSL_get_fd(ssl), &port);
  60614. if (err != 0)
  60615. return -1;
  60616. sock_fd = socket(AF_INET_V, SOCK_DGRAM, 0);
  60617. if (sock_fd == -1)
  60618. return -1;
  60619. build_addr(&addr, wolfSSLIP, port, 1, 0);
  60620. /* send a packet to the server. Being another socket, the kernel will ensure
  60621. * the source port will be different. */
  60622. err = (int)sendto(sock_fd, buf, sizeof(buf), 0, (SOCKADDR*)&addr,
  60623. sizeof(addr));
  60624. close(sock_fd);
  60625. if (err == -1)
  60626. return -1;
  60627. return 0;
  60628. }
  60629. /* setup a SSL session but just after the handshake send a packet to the server
  60630. * with a source address different than the one of the connected client. The I/O
  60631. * callback EmbedRecvFrom should just ignore the packet. Sending of the packet
  60632. * is done in test_dtls_msg_from_other_peer_cb */
  60633. static int test_dtls_msg_from_other_peer(void)
  60634. {
  60635. EXPECT_DECLS;
  60636. callback_functions client_cbs;
  60637. callback_functions server_cbs;
  60638. XMEMSET((byte*)&client_cbs, 0, sizeof(client_cbs));
  60639. XMEMSET((byte*)&server_cbs, 0, sizeof(server_cbs));
  60640. client_cbs.method = wolfDTLSv1_2_client_method;
  60641. server_cbs.method = wolfDTLSv1_2_server_method;
  60642. client_cbs.doUdp = 1;
  60643. server_cbs.doUdp = 1;
  60644. test_wolfSSL_client_server_nofail_ex(&client_cbs, &server_cbs,
  60645. test_dtls_msg_from_other_peer_cb);
  60646. ExpectIntEQ(client_cbs.return_code, WOLFSSL_SUCCESS);
  60647. ExpectIntEQ(server_cbs.return_code, WOLFSSL_SUCCESS);
  60648. return EXPECT_RESULT();
  60649. }
  60650. #else
  60651. static int test_dtls_msg_from_other_peer(void)
  60652. {
  60653. return TEST_SKIPPED;
  60654. }
  60655. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  60656. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  60657. * !defined(SINGLE_THREADED) && !defined(NO_RSA) */
  60658. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_IPV6) && \
  60659. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  60660. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  60661. static int test_dtls_ipv6_check(void)
  60662. {
  60663. EXPECT_DECLS;
  60664. WOLFSSL_CTX *ctx_c = NULL;
  60665. WOLFSSL_CTX *ctx_s = NULL;
  60666. WOLFSSL *ssl_c = NULL;
  60667. WOLFSSL *ssl_s = NULL;
  60668. SOCKADDR_IN fake_addr6;
  60669. int sockfd = -1;
  60670. ExpectNotNull(ctx_c = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  60671. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  60672. ExpectNotNull(ctx_s = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  60673. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile,
  60674. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60675. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile,
  60676. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60677. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  60678. XMEMSET((byte*)&fake_addr6, 0, sizeof(fake_addr6));
  60679. /* mimic a sockaddr_in6 struct, this way we can't test without
  60680. * WOLFSSL_IPV6 */
  60681. fake_addr6.sin_family = WOLFSSL_IP6;
  60682. ExpectIntNE(sockfd = socket(AF_INET, SOCK_DGRAM, 0), -1);
  60683. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  60684. /* can't return error here, as the peer is opaque for wolfssl library at
  60685. * this point */
  60686. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_c, &fake_addr6, sizeof(fake_addr6)),
  60687. WOLFSSL_SUCCESS);
  60688. ExpectIntNE(fcntl(sockfd, F_SETFL, O_NONBLOCK), -1);
  60689. wolfSSL_dtls_set_using_nonblock(ssl_c, 1);
  60690. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60691. ExpectIntEQ(ssl_c->error, SOCKET_ERROR_E);
  60692. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_s, &fake_addr6, sizeof(fake_addr6)),
  60693. WOLFSSL_SUCCESS);
  60694. /* reuse the socket */
  60695. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  60696. wolfSSL_dtls_set_using_nonblock(ssl_s, 1);
  60697. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  60698. ExpectIntEQ(ssl_s->error, SOCKET_ERROR_E);
  60699. if (sockfd != -1)
  60700. close(sockfd);
  60701. wolfSSL_free(ssl_c);
  60702. wolfSSL_CTX_free(ctx_c);
  60703. wolfSSL_free(ssl_s);
  60704. wolfSSL_CTX_free(ctx_s);
  60705. return EXPECT_RESULT();
  60706. }
  60707. #else
  60708. static int test_dtls_ipv6_check(void)
  60709. {
  60710. return TEST_SKIPPED;
  60711. }
  60712. #endif
  60713. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  60714. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  60715. static WOLFSSL_SESSION* test_wolfSSL_SCR_after_resumption_session = NULL;
  60716. static void test_wolfSSL_SCR_after_resumption_ctx_ready(WOLFSSL_CTX* ctx)
  60717. {
  60718. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  60719. }
  60720. static void test_wolfSSL_SCR_after_resumption_on_result(WOLFSSL* ssl)
  60721. {
  60722. if (test_wolfSSL_SCR_after_resumption_session == NULL) {
  60723. test_wolfSSL_SCR_after_resumption_session = wolfSSL_get1_session(ssl);
  60724. AssertNotNull(test_wolfSSL_SCR_after_resumption_session);
  60725. }
  60726. else {
  60727. char testMsg[] = "Message after SCR";
  60728. char msgBuf[sizeof(testMsg)];
  60729. int ret;
  60730. if (!wolfSSL_is_server(ssl)) {
  60731. AssertIntEQ(WOLFSSL_SUCCESS,
  60732. wolfSSL_set_session(ssl,
  60733. test_wolfSSL_SCR_after_resumption_session));
  60734. }
  60735. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  60736. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  60737. sizeof(testMsg));
  60738. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  60739. if (ret != sizeof(msgBuf)) /* Possibly APP_DATA_READY error. Retry. */
  60740. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  60741. AssertIntEQ(ret, sizeof(msgBuf));
  60742. }
  60743. }
  60744. static void test_wolfSSL_SCR_after_resumption_ssl_ready(WOLFSSL* ssl)
  60745. {
  60746. AssertIntEQ(WOLFSSL_SUCCESS,
  60747. wolfSSL_set_session(ssl, test_wolfSSL_SCR_after_resumption_session));
  60748. }
  60749. static int test_wolfSSL_SCR_after_resumption(void)
  60750. {
  60751. EXPECT_DECLS;
  60752. callback_functions func_cb_client;
  60753. callback_functions func_cb_server;
  60754. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  60755. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  60756. func_cb_client.method = wolfTLSv1_2_client_method;
  60757. func_cb_client.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  60758. func_cb_client.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  60759. func_cb_server.method = wolfTLSv1_2_server_method;
  60760. func_cb_server.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  60761. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  60762. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  60763. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  60764. func_cb_client.ssl_ready = test_wolfSSL_SCR_after_resumption_ssl_ready;
  60765. func_cb_server.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  60766. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  60767. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  60768. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  60769. wolfSSL_SESSION_free(test_wolfSSL_SCR_after_resumption_session);
  60770. return EXPECT_RESULT();
  60771. }
  60772. #else
  60773. static int test_wolfSSL_SCR_after_resumption(void)
  60774. {
  60775. return TEST_SKIPPED;
  60776. }
  60777. #endif
  60778. static int test_wolfSSL_configure_args(void)
  60779. {
  60780. EXPECT_DECLS;
  60781. #if defined(LIBWOLFSSL_CONFIGURE_ARGS) && defined(HAVE_WC_INTROSPECTION)
  60782. ExpectNotNull(wolfSSL_configure_args());
  60783. #endif
  60784. return EXPECT_RESULT();
  60785. }
  60786. static int test_dtls_no_extensions(void)
  60787. {
  60788. EXPECT_DECLS;
  60789. #if defined(WOLFSSL_DTLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  60790. !defined(WOLFSSL_NO_TLS12)
  60791. WOLFSSL *ssl_s = NULL;
  60792. WOLFSSL_CTX *ctx_s = NULL;
  60793. struct test_memio_ctx test_ctx;
  60794. const byte chNoExtensions[] = {
  60795. /* Handshake type */
  60796. 0x16,
  60797. /* Version */
  60798. 0xfe, 0xff,
  60799. /* Epoch */
  60800. 0x00, 0x00,
  60801. /* Seq number */
  60802. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60803. /* Length */
  60804. 0x00, 0x40,
  60805. /* CH type */
  60806. 0x01,
  60807. /* Length */
  60808. 0x00, 0x00, 0x34,
  60809. /* Msg Seq */
  60810. 0x00, 0x00,
  60811. /* Frag offset */
  60812. 0x00, 0x00, 0x00,
  60813. /* Frag length */
  60814. 0x00, 0x00, 0x34,
  60815. /* Version */
  60816. 0xfe, 0xff,
  60817. /* Random */
  60818. 0x62, 0xfe, 0xbc, 0xfe, 0x2b, 0xfe, 0x3f, 0xeb, 0x03, 0xc4, 0xea, 0x37,
  60819. 0xe7, 0x47, 0x7e, 0x8a, 0xd9, 0xbf, 0x77, 0x0f, 0x6c, 0xb6, 0x77, 0x0b,
  60820. 0x03, 0x3f, 0x82, 0x2b, 0x21, 0x64, 0x57, 0x1d,
  60821. /* Session Length */
  60822. 0x00,
  60823. /* Cookie Length */
  60824. 0x00,
  60825. /* CS Length */
  60826. 0x00, 0x0c,
  60827. /* CS */
  60828. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x39, 0x00, 0x33,
  60829. /* Comp Meths Length */
  60830. 0x01,
  60831. /* Comp Meths */
  60832. 0x00
  60833. /* And finally... no extensions */
  60834. };
  60835. int i;
  60836. #ifdef OPENSSL_EXTRA
  60837. int repeats = 2;
  60838. #else
  60839. int repeats = 1;
  60840. #endif
  60841. for (i = 0; i < repeats; i++) {
  60842. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60843. ssl_s = NULL;
  60844. ctx_s = NULL;
  60845. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  60846. NULL, wolfDTLS_server_method), 0);
  60847. XMEMCPY(test_ctx.s_buff, chNoExtensions, sizeof(chNoExtensions));
  60848. test_ctx.s_len = sizeof(chNoExtensions);
  60849. #ifdef OPENSSL_EXTRA
  60850. if (i > 0) {
  60851. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl_s, DTLS1_2_VERSION),
  60852. WOLFSSL_SUCCESS);
  60853. }
  60854. #endif
  60855. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  60856. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60857. /* Expecting a handshake msg. Either HVR or SH. */
  60858. ExpectIntGT(test_ctx.c_len, 0);
  60859. ExpectIntEQ(test_ctx.c_buff[0], 0x16);
  60860. wolfSSL_free(ssl_s);
  60861. wolfSSL_CTX_free(ctx_s);
  60862. }
  60863. #endif
  60864. return EXPECT_RESULT();
  60865. }
  60866. static int test_TLSX_CA_NAMES_bad_extension(void)
  60867. {
  60868. EXPECT_DECLS;
  60869. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  60870. !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES) && \
  60871. defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA384) && \
  60872. defined(HAVE_NULL_CIPHER)
  60873. /* This test should only fail (with BUFFER_ERROR) when we actually try to
  60874. * parse the CA Names extension. Otherwise it will return other non-related
  60875. * errors. If CA Names will be parsed in more configurations, that should
  60876. * be reflected in the macro guard above. */
  60877. WOLFSSL *ssl_c = NULL;
  60878. WOLFSSL_CTX *ctx_c = NULL;
  60879. struct test_memio_ctx test_ctx;
  60880. /* HRR + SH using TLS_DHE_PSK_WITH_NULL_SHA384 */
  60881. const byte shBadCaNamesExt[] = {
  60882. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  60883. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  60884. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  60885. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  60886. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  60887. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  60888. 0x5c, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x03, 0xcf, 0x21, 0xad, 0x74,
  60889. 0x00, 0x00, 0x83, 0x3f, 0x3b, 0x80, 0x01, 0xac, 0x65, 0x8c, 0x19, 0x2a,
  60890. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x02, 0x00, 0x9e, 0x09, 0x1c, 0xe8,
  60891. 0xa8, 0x09, 0x9c, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  60892. 0x03, 0x3f, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x13, 0x05,
  60893. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x00, 0x09, 0x00, 0x00,
  60894. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x01, 0xff,
  60895. 0xff, 0xff, 0xff, 0xfa, 0x0d, 0x00, 0x00, 0x00, 0xad, 0x02
  60896. };
  60897. const byte shBadCaNamesExt2[] = {
  60898. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  60899. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  60900. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  60901. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  60902. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  60903. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  60904. 0x5e, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x7f, 0xd0, 0x2d, 0xea, 0x6e,
  60905. 0x53, 0xa1, 0x6a, 0xc9, 0xc8, 0x54, 0xef, 0x75, 0xe4, 0xd9, 0xc6, 0x3e,
  60906. 0x74, 0xcb, 0x30, 0x80, 0xcc, 0x83, 0x3a, 0x00, 0x00, 0x00, 0x00, 0x00,
  60907. 0x00, 0xc0, 0x5a, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  60908. 0x03, 0x03, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x53, 0x25,
  60909. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x02, 0x05, 0x00, 0x00,
  60910. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x06, 0x00,
  60911. 0x04, 0x00, 0x03, 0x30, 0x00, 0x13, 0x94, 0x00, 0x06, 0x00, 0x04, 0x02
  60912. };
  60913. int i = 0;
  60914. for (i = 0; i < 2; i++) {
  60915. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60916. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  60917. wolfTLSv1_3_client_method, NULL), 0);
  60918. switch (i) {
  60919. case 0:
  60920. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt,
  60921. sizeof(shBadCaNamesExt));
  60922. test_ctx.c_len = sizeof(shBadCaNamesExt);
  60923. break;
  60924. case 1:
  60925. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt2,
  60926. sizeof(shBadCaNamesExt2));
  60927. test_ctx.c_len = sizeof(shBadCaNamesExt2);
  60928. break;
  60929. }
  60930. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  60931. #ifndef WOLFSSL_DISABLE_EARLY_SANITY_CHECKS
  60932. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), OUT_OF_ORDER_E);
  60933. #else
  60934. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), BUFFER_ERROR);
  60935. #endif
  60936. wolfSSL_free(ssl_c);
  60937. ssl_c = NULL;
  60938. wolfSSL_CTX_free(ctx_c);
  60939. ctx_c = NULL;
  60940. }
  60941. #endif
  60942. return EXPECT_RESULT();
  60943. }
  60944. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  60945. defined(HAVE_IO_TESTS_DEPENDENCIES)
  60946. static void test_dtls_1_0_hvr_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  60947. {
  60948. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  60949. WOLFSSL_SUCCESS);
  60950. }
  60951. static int test_dtls_1_0_hvr_downgrade(void)
  60952. {
  60953. EXPECT_DECLS;
  60954. callback_functions func_cb_client;
  60955. callback_functions func_cb_server;
  60956. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  60957. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  60958. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  60959. func_cb_client.method = wolfDTLS_client_method;
  60960. func_cb_server.method = wolfDTLSv1_2_server_method;
  60961. func_cb_client.ctx_ready = test_dtls_1_0_hvr_downgrade_ctx_ready;
  60962. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  60963. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  60964. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  60965. return EXPECT_RESULT();
  60966. }
  60967. #else
  60968. static int test_dtls_1_0_hvr_downgrade(void)
  60969. {
  60970. EXPECT_DECLS;
  60971. return EXPECT_RESULT();
  60972. }
  60973. #endif
  60974. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12) && \
  60975. defined(HAVE_SESSION_TICKET)
  60976. static WOLFSSL_SESSION* test_session_ticket_no_id_session = NULL;
  60977. static void test_session_ticket_no_id_on_result(WOLFSSL* ssl)
  60978. {
  60979. test_session_ticket_no_id_session = wolfSSL_get1_session(ssl);
  60980. AssertNotNull(test_session_ticket_no_id_session);
  60981. }
  60982. static void test_session_ticket_no_id_ctx_ready(WOLFSSL_CTX* ctx)
  60983. {
  60984. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  60985. }
  60986. static void test_session_ticket_no_id_ssl_ready(WOLFSSL* ssl)
  60987. {
  60988. test_session_ticket_no_id_session->sessionIDSz = 0;
  60989. AssertIntEQ(WOLFSSL_SUCCESS,
  60990. wolfSSL_set_session(ssl, test_session_ticket_no_id_session));
  60991. }
  60992. static int test_session_ticket_no_id(void)
  60993. {
  60994. /* We are testing an expired (invalid crypto context in out case since the
  60995. * ctx changes) session ticket being sent with the session ID being 0
  60996. * length. */
  60997. EXPECT_DECLS;
  60998. callback_functions func_cb_client;
  60999. callback_functions func_cb_server;
  61000. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  61001. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  61002. func_cb_client.method = wolfTLSv1_2_client_method;
  61003. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  61004. func_cb_client.on_result = test_session_ticket_no_id_on_result;
  61005. func_cb_server.method = wolfTLSv1_2_server_method;
  61006. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  61007. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61008. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61009. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61010. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  61011. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  61012. func_cb_client.method = wolfTLSv1_2_client_method;
  61013. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  61014. func_cb_client.ssl_ready = test_session_ticket_no_id_ssl_ready;
  61015. func_cb_server.method = wolfTLSv1_2_server_method;
  61016. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  61017. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61018. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61019. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61020. wolfSSL_SESSION_free(test_session_ticket_no_id_session);
  61021. return EXPECT_RESULT();
  61022. }
  61023. #else
  61024. static int test_session_ticket_no_id(void)
  61025. {
  61026. EXPECT_DECLS;
  61027. return EXPECT_RESULT();
  61028. }
  61029. #endif
  61030. static int test_session_ticket_hs_update(void)
  61031. {
  61032. EXPECT_DECLS;
  61033. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  61034. defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  61035. struct test_memio_ctx test_ctx;
  61036. struct test_memio_ctx test_ctx2;
  61037. struct test_memio_ctx test_ctx3;
  61038. WOLFSSL_CTX *ctx_c = NULL;
  61039. WOLFSSL_CTX *ctx_s = NULL;
  61040. WOLFSSL *ssl_c = NULL;
  61041. WOLFSSL *ssl_c2 = NULL;
  61042. WOLFSSL *ssl_c3 = NULL;
  61043. WOLFSSL *ssl_s = NULL;
  61044. WOLFSSL *ssl_s2 = NULL;
  61045. WOLFSSL *ssl_s3 = NULL;
  61046. WOLFSSL_SESSION *sess = NULL;
  61047. byte read_data[1];
  61048. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61049. XMEMSET(&test_ctx2, 0, sizeof(test_ctx2));
  61050. XMEMSET(&test_ctx3, 0, sizeof(test_ctx3));
  61051. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61052. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  61053. /* Generate tickets */
  61054. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61055. wolfSSL_SetLoggingPrefix("client");
  61056. /* Read the ticket msg */
  61057. ExpectIntEQ(wolfSSL_read(ssl_c, read_data, sizeof(read_data)),
  61058. WOLFSSL_FATAL_ERROR);
  61059. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61060. WOLFSSL_ERROR_WANT_READ);
  61061. wolfSSL_SetLoggingPrefix(NULL);
  61062. ExpectIntEQ(test_memio_setup(&test_ctx2, &ctx_c, &ctx_s, &ssl_c2, &ssl_s2,
  61063. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  61064. ExpectIntEQ(test_memio_setup(&test_ctx3, &ctx_c, &ctx_s, &ssl_c3, &ssl_s3,
  61065. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  61066. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  61067. ExpectIntEQ(wolfSSL_set_session(ssl_c2, sess), WOLFSSL_SUCCESS);
  61068. ExpectIntEQ(wolfSSL_set_session(ssl_c3, sess), WOLFSSL_SUCCESS);
  61069. wolfSSL_SetLoggingPrefix("client");
  61070. /* Exchange initial flights for the second connection */
  61071. ExpectIntEQ(wolfSSL_connect(ssl_c2), WOLFSSL_FATAL_ERROR);
  61072. ExpectIntEQ(wolfSSL_get_error(ssl_c2, WOLFSSL_FATAL_ERROR),
  61073. WOLFSSL_ERROR_WANT_READ);
  61074. wolfSSL_SetLoggingPrefix(NULL);
  61075. wolfSSL_SetLoggingPrefix("server");
  61076. ExpectIntEQ(wolfSSL_accept(ssl_s2), WOLFSSL_FATAL_ERROR);
  61077. ExpectIntEQ(wolfSSL_get_error(ssl_s2, WOLFSSL_FATAL_ERROR),
  61078. WOLFSSL_ERROR_WANT_READ);
  61079. wolfSSL_SetLoggingPrefix(NULL);
  61080. /* Complete third connection so that new tickets are exchanged */
  61081. ExpectIntEQ(test_memio_do_handshake(ssl_c3, ssl_s3, 10, NULL), 0);
  61082. /* Read the ticket msg */
  61083. wolfSSL_SetLoggingPrefix("client");
  61084. ExpectIntEQ(wolfSSL_read(ssl_c3, read_data, sizeof(read_data)),
  61085. WOLFSSL_FATAL_ERROR);
  61086. ExpectIntEQ(wolfSSL_get_error(ssl_c3, WOLFSSL_FATAL_ERROR),
  61087. WOLFSSL_ERROR_WANT_READ);
  61088. wolfSSL_SetLoggingPrefix(NULL);
  61089. /* Complete second connection */
  61090. ExpectIntEQ(test_memio_do_handshake(ssl_c2, ssl_s2, 10, NULL), 0);
  61091. ExpectIntEQ(wolfSSL_session_reused(ssl_c2), 1);
  61092. ExpectIntEQ(wolfSSL_session_reused(ssl_c3), 1);
  61093. wolfSSL_free(ssl_c);
  61094. wolfSSL_free(ssl_c2);
  61095. wolfSSL_free(ssl_c3);
  61096. wolfSSL_free(ssl_s);
  61097. wolfSSL_free(ssl_s2);
  61098. wolfSSL_free(ssl_s3);
  61099. wolfSSL_CTX_free(ctx_c);
  61100. wolfSSL_CTX_free(ctx_s);
  61101. wolfSSL_SESSION_free(sess);
  61102. #endif
  61103. return EXPECT_RESULT();
  61104. }
  61105. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  61106. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  61107. static void test_dtls_downgrade_scr_server_ctx_ready_server(WOLFSSL_CTX* ctx)
  61108. {
  61109. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  61110. WOLFSSL_SUCCESS);
  61111. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  61112. }
  61113. static void test_dtls_downgrade_scr_server_ctx_ready(WOLFSSL_CTX* ctx)
  61114. {
  61115. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  61116. }
  61117. static void test_dtls_downgrade_scr_server_on_result(WOLFSSL* ssl)
  61118. {
  61119. char testMsg[] = "Message after SCR";
  61120. char msgBuf[sizeof(testMsg)];
  61121. if (wolfSSL_is_server(ssl)) {
  61122. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  61123. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  61124. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  61125. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  61126. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  61127. sizeof(testMsg));
  61128. }
  61129. else {
  61130. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  61131. sizeof(testMsg));
  61132. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  61133. }
  61134. }
  61135. static int test_dtls_downgrade_scr_server(void)
  61136. {
  61137. EXPECT_DECLS;
  61138. callback_functions func_cb_client;
  61139. callback_functions func_cb_server;
  61140. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  61141. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  61142. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  61143. func_cb_client.method = wolfDTLSv1_2_client_method;
  61144. func_cb_server.method = wolfDTLS_server_method;
  61145. func_cb_client.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready;
  61146. func_cb_server.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready_server;
  61147. func_cb_client.on_result = test_dtls_downgrade_scr_server_on_result;
  61148. func_cb_server.on_result = test_dtls_downgrade_scr_server_on_result;
  61149. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61150. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61151. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61152. return EXPECT_RESULT();
  61153. }
  61154. #else
  61155. static int test_dtls_downgrade_scr_server(void)
  61156. {
  61157. EXPECT_DECLS;
  61158. return EXPECT_RESULT();
  61159. }
  61160. #endif
  61161. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  61162. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  61163. static void test_dtls_downgrade_scr_ctx_ready(WOLFSSL_CTX* ctx)
  61164. {
  61165. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  61166. WOLFSSL_SUCCESS);
  61167. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  61168. }
  61169. static void test_dtls_downgrade_scr_on_result(WOLFSSL* ssl)
  61170. {
  61171. char testMsg[] = "Message after SCR";
  61172. char msgBuf[sizeof(testMsg)];
  61173. if (wolfSSL_is_server(ssl)) {
  61174. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  61175. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  61176. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  61177. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  61178. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  61179. sizeof(testMsg));
  61180. }
  61181. else {
  61182. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  61183. sizeof(testMsg));
  61184. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  61185. }
  61186. }
  61187. static int test_dtls_downgrade_scr(void)
  61188. {
  61189. EXPECT_DECLS;
  61190. callback_functions func_cb_client;
  61191. callback_functions func_cb_server;
  61192. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  61193. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  61194. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  61195. func_cb_client.method = wolfDTLS_client_method;
  61196. func_cb_server.method = wolfDTLSv1_2_server_method;
  61197. func_cb_client.ctx_ready = test_dtls_downgrade_scr_ctx_ready;
  61198. func_cb_client.on_result = test_dtls_downgrade_scr_on_result;
  61199. func_cb_server.on_result = test_dtls_downgrade_scr_on_result;
  61200. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61201. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61202. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61203. return EXPECT_RESULT();
  61204. }
  61205. #else
  61206. static int test_dtls_downgrade_scr(void)
  61207. {
  61208. EXPECT_DECLS;
  61209. return EXPECT_RESULT();
  61210. }
  61211. #endif
  61212. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  61213. && !defined(WOLFSSL_NO_TLS12)
  61214. static int test_dtls_client_hello_timeout_downgrade_read_cb(WOLFSSL *ssl,
  61215. char *data, int sz, void *ctx)
  61216. {
  61217. static int call_counter = 0;
  61218. call_counter++;
  61219. (void)ssl;
  61220. (void)data;
  61221. (void)sz;
  61222. (void)ctx;
  61223. switch (call_counter) {
  61224. case 1:
  61225. case 2:
  61226. return WOLFSSL_CBIO_ERR_TIMEOUT;
  61227. case 3:
  61228. return WOLFSSL_CBIO_ERR_WANT_READ;
  61229. default:
  61230. AssertIntLE(call_counter, 3);
  61231. return -1;
  61232. }
  61233. }
  61234. #endif
  61235. /* Make sure we don't send acks before getting a server hello */
  61236. static int test_dtls_client_hello_timeout_downgrade(void)
  61237. {
  61238. EXPECT_DECLS;
  61239. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  61240. && !defined(WOLFSSL_NO_TLS12)
  61241. WOLFSSL_CTX *ctx_c = NULL;
  61242. WOLFSSL_CTX *ctx_s = NULL;
  61243. WOLFSSL *ssl_c = NULL;
  61244. WOLFSSL *ssl_s = NULL;
  61245. struct test_memio_ctx test_ctx;
  61246. DtlsRecordLayerHeader* dtlsRH;
  61247. size_t len;
  61248. byte sequence_number[8];
  61249. int i;
  61250. for (i = 0; i < 2; i++) {
  61251. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61252. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61253. wolfDTLS_client_method, wolfDTLSv1_2_server_method), 0);
  61254. if (i == 0) {
  61255. /* First time simulate timeout in IO layer */
  61256. /* CH1 */
  61257. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61258. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61259. /* HVR */
  61260. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61261. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61262. /* CH2 */
  61263. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61264. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61265. /* SH flight */
  61266. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61267. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61268. /* Drop the SH */
  61269. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  61270. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61271. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  61272. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  61273. (sizeof(DtlsRecordLayerHeader) + len));
  61274. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  61275. /* Read the remainder of the flight */
  61276. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61277. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61278. wolfSSL_SSLSetIORecv(ssl_c,
  61279. test_dtls_client_hello_timeout_downgrade_read_cb);
  61280. /* CH3 */
  61281. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61282. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61283. wolfSSL_SSLSetIORecv(ssl_c, test_memio_read_cb);
  61284. }
  61285. else {
  61286. /* Second time call wolfSSL_dtls_got_timeout */
  61287. /* CH1 */
  61288. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61289. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61290. /* HVR */
  61291. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61292. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61293. /* CH2 */
  61294. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61295. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61296. /* SH flight */
  61297. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61298. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61299. /* Drop the SH */
  61300. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  61301. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61302. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  61303. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  61304. (sizeof(DtlsRecordLayerHeader) + len));
  61305. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  61306. /* Read the remainder of the flight */
  61307. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61308. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61309. /* Quick timeout should be set as we received at least one msg */
  61310. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 1);
  61311. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  61312. /* Quick timeout should be cleared after a quick timeout */
  61313. /* CH3 */
  61314. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 0);
  61315. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  61316. }
  61317. /* Parse out to make sure we got exactly one ClientHello message */
  61318. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  61319. /* Second ClientHello after HVR */
  61320. sequence_number[7] = 2;
  61321. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.s_buff;
  61322. ExpectIntEQ(dtlsRH->type, handshake);
  61323. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  61324. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  61325. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  61326. sizeof(sequence_number)), 0);
  61327. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61328. ExpectIntEQ(sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  61329. /* Connection should be able to continue */
  61330. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61331. wolfSSL_free(ssl_c);
  61332. wolfSSL_free(ssl_s);
  61333. wolfSSL_CTX_free(ctx_c);
  61334. wolfSSL_CTX_free(ctx_s);
  61335. ssl_c = NULL;
  61336. ssl_s = NULL;
  61337. ctx_c = NULL;
  61338. ctx_s = NULL;
  61339. if (!EXPECT_SUCCESS())
  61340. break;
  61341. }
  61342. #endif
  61343. return EXPECT_RESULT();
  61344. }
  61345. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  61346. static int test_dtls_client_hello_timeout_read_cb(WOLFSSL *ssl, char *data,
  61347. int sz, void *ctx)
  61348. {
  61349. static int call_counter = 0;
  61350. call_counter++;
  61351. (void)ssl;
  61352. (void)data;
  61353. (void)sz;
  61354. (void)ctx;
  61355. switch (call_counter) {
  61356. case 1:
  61357. return WOLFSSL_CBIO_ERR_TIMEOUT;
  61358. case 2:
  61359. return WOLFSSL_CBIO_ERR_WANT_READ;
  61360. default:
  61361. AssertIntLE(call_counter, 2);
  61362. return -1;
  61363. }
  61364. }
  61365. #endif
  61366. /* Make sure we don't send acks before getting a server hello */
  61367. static int test_dtls_client_hello_timeout(void)
  61368. {
  61369. EXPECT_DECLS;
  61370. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  61371. WOLFSSL *ssl_c = NULL;
  61372. WOLFSSL_CTX *ctx_c = NULL;
  61373. struct test_memio_ctx test_ctx;
  61374. DtlsRecordLayerHeader* dtlsRH;
  61375. size_t idx;
  61376. size_t len;
  61377. byte sequence_number[8];
  61378. int i;
  61379. for (i = 0; i < 2; i++) {
  61380. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61381. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  61382. wolfDTLSv1_3_client_method, NULL), 0);
  61383. if (i == 0) {
  61384. /* First time simulate timeout in IO layer */
  61385. wolfSSL_SSLSetIORecv(ssl_c, test_dtls_client_hello_timeout_read_cb);
  61386. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  61387. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61388. }
  61389. else {
  61390. /* Second time call wolfSSL_dtls_got_timeout */
  61391. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  61392. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61393. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  61394. }
  61395. /* Parse out to make sure we got exactly two ClientHello messages */
  61396. idx = 0;
  61397. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  61398. /* First ClientHello */
  61399. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  61400. ExpectIntEQ(dtlsRH->type, handshake);
  61401. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  61402. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  61403. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  61404. sizeof(sequence_number)), 0);
  61405. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61406. ExpectIntLT(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  61407. idx += sizeof(DtlsRecordLayerHeader) + len;
  61408. /* Second ClientHello */
  61409. sequence_number[7] = 1;
  61410. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  61411. ExpectIntEQ(dtlsRH->type, handshake);
  61412. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  61413. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  61414. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  61415. sizeof(sequence_number)), 0);
  61416. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61417. ExpectIntEQ(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  61418. wolfSSL_free(ssl_c);
  61419. wolfSSL_CTX_free(ctx_c);
  61420. ssl_c = NULL;
  61421. ctx_c = NULL;
  61422. if (!EXPECT_SUCCESS())
  61423. break;
  61424. }
  61425. #endif
  61426. return EXPECT_RESULT();
  61427. }
  61428. /* DTLS test when dropping the changed cipher spec message */
  61429. static int test_dtls_dropped_ccs(void)
  61430. {
  61431. EXPECT_DECLS;
  61432. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  61433. && !defined(WOLFSSL_NO_TLS12)
  61434. WOLFSSL_CTX *ctx_c = NULL;
  61435. WOLFSSL_CTX *ctx_s = NULL;
  61436. WOLFSSL *ssl_c = NULL;
  61437. WOLFSSL *ssl_s = NULL;
  61438. struct test_memio_ctx test_ctx;
  61439. DtlsRecordLayerHeader* dtlsRH;
  61440. size_t len;
  61441. byte data[1];
  61442. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61443. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61444. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  61445. /* CH1 */
  61446. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61447. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61448. /* HVR */
  61449. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61450. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61451. /* CH2 */
  61452. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61453. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61454. /* Server first flight */
  61455. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61456. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61457. /* Client flight */
  61458. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61459. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61460. /* Server ccs + finished */
  61461. ExpectIntEQ(wolfSSL_negotiate(ssl_s), 1);
  61462. /* Drop the ccs */
  61463. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  61464. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61465. ExpectIntEQ(len, 1);
  61466. ExpectIntEQ(dtlsRH->type, change_cipher_spec);
  61467. if (EXPECT_SUCCESS()) {
  61468. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  61469. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  61470. (sizeof(DtlsRecordLayerHeader) + len));
  61471. }
  61472. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  61473. /* Client rtx flight */
  61474. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61475. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61476. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  61477. /* Server ccs + finished rtx */
  61478. ExpectIntEQ(wolfSSL_read(ssl_s, data, sizeof(data)), -1);
  61479. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61480. /* Client processes finished */
  61481. ExpectIntEQ(wolfSSL_negotiate(ssl_c), 1);
  61482. wolfSSL_free(ssl_c);
  61483. wolfSSL_free(ssl_s);
  61484. wolfSSL_CTX_free(ctx_c);
  61485. wolfSSL_CTX_free(ctx_s);
  61486. #endif
  61487. return EXPECT_RESULT();
  61488. }
  61489. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  61490. && !defined(WOLFSSL_NO_TLS12)
  61491. static int test_dtls_seq_num_downgrade_check_num(byte* ioBuf, int ioBufLen,
  61492. byte seq_num)
  61493. {
  61494. EXPECT_DECLS;
  61495. DtlsRecordLayerHeader* dtlsRH;
  61496. byte sequence_number[8];
  61497. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  61498. ExpectIntGE(ioBufLen, sizeof(*dtlsRH));
  61499. dtlsRH = (DtlsRecordLayerHeader*)ioBuf;
  61500. ExpectIntEQ(dtlsRH->type, handshake);
  61501. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  61502. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  61503. sequence_number[7] = seq_num;
  61504. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  61505. sizeof(sequence_number)), 0);
  61506. return EXPECT_RESULT();
  61507. }
  61508. #endif
  61509. /*
  61510. * Make sure that we send the correct sequence number after a HelloVerifyRequest
  61511. * and after a HelloRetryRequest. This is testing the server side as it is
  61512. * operating statelessly and should copy the sequence number of the ClientHello.
  61513. */
  61514. static int test_dtls_seq_num_downgrade(void)
  61515. {
  61516. EXPECT_DECLS;
  61517. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  61518. && !defined(WOLFSSL_NO_TLS12)
  61519. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  61520. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  61521. struct test_memio_ctx test_ctx;
  61522. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61523. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61524. wolfDTLSv1_2_client_method, wolfDTLS_server_method), 0);
  61525. /* CH1 */
  61526. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61527. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61528. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.s_buff,
  61529. test_ctx.s_len, 0), TEST_SUCCESS);
  61530. /* HVR */
  61531. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61532. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61533. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.c_buff,
  61534. test_ctx.c_len, 0), TEST_SUCCESS);
  61535. /* CH2 */
  61536. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61537. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61538. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.s_buff,
  61539. test_ctx.s_len, 1), TEST_SUCCESS);
  61540. /* Server first flight */
  61541. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61542. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61543. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.c_buff,
  61544. test_ctx.c_len, 1), TEST_SUCCESS);
  61545. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61546. wolfSSL_free(ssl_c);
  61547. wolfSSL_CTX_free(ctx_c);
  61548. wolfSSL_free(ssl_s);
  61549. wolfSSL_CTX_free(ctx_s);
  61550. #endif
  61551. return EXPECT_RESULT();
  61552. }
  61553. /**
  61554. * Make sure we don't send RSA Signature Hash Algorithms in the
  61555. * CertificateRequest when we don't have any such ciphers set.
  61556. * @return EXPECT_RESULT()
  61557. */
  61558. static int test_certreq_sighash_algos(void)
  61559. {
  61560. EXPECT_DECLS;
  61561. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  61562. !defined(WOLFSSL_MAX_STRENGTH) && defined(HAVE_ECC) && \
  61563. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  61564. defined(HAVE_AES_CBC) && !defined(WOLFSSL_NO_TLS12)
  61565. WOLFSSL_CTX *ctx_c = NULL;
  61566. WOLFSSL_CTX *ctx_s = NULL;
  61567. WOLFSSL *ssl_c = NULL;
  61568. WOLFSSL *ssl_s = NULL;
  61569. struct test_memio_ctx test_ctx;
  61570. int idx = 0;
  61571. int maxIdx = 0;
  61572. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61573. test_ctx.c_ciphers = test_ctx.s_ciphers =
  61574. "ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA384";
  61575. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61576. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  61577. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx_c,
  61578. "./certs/ca-ecc-cert.pem", NULL), WOLFSSL_SUCCESS);
  61579. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_PEER, NULL);
  61580. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ssl_s, "./certs/ecc-key.pem",
  61581. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61582. ExpectIntEQ(wolfSSL_use_certificate_file(ssl_s, "./certs/server-ecc.pem",
  61583. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61584. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_FATAL_ERROR);
  61585. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61586. WOLFSSL_ERROR_WANT_READ);
  61587. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_FATAL_ERROR);
  61588. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  61589. WOLFSSL_ERROR_WANT_READ);
  61590. /* Find the CertificateRequest message */
  61591. for (idx = 0; idx < test_ctx.c_len && EXPECT_SUCCESS();) {
  61592. word16 len;
  61593. ExpectIntEQ(test_ctx.c_buff[idx++], handshake);
  61594. ExpectIntEQ(test_ctx.c_buff[idx++], SSLv3_MAJOR);
  61595. ExpectIntEQ(test_ctx.c_buff[idx++], TLSv1_2_MINOR);
  61596. ato16(test_ctx.c_buff + idx, &len);
  61597. idx += OPAQUE16_LEN;
  61598. if (test_ctx.c_buff[idx] == certificate_request) {
  61599. idx++;
  61600. /* length */
  61601. idx += OPAQUE24_LEN;
  61602. /* cert types */
  61603. idx += 1 + test_ctx.c_buff[idx];
  61604. /* Sig algos */
  61605. ato16(test_ctx.c_buff + idx, &len);
  61606. idx += OPAQUE16_LEN;
  61607. maxIdx = idx + (int)len;
  61608. for (; idx < maxIdx && EXPECT_SUCCESS(); idx += OPAQUE16_LEN) {
  61609. if (test_ctx.c_buff[idx+1] == ED25519_SA_MINOR ||
  61610. test_ctx.c_buff[idx+1] == ED448_SA_MINOR)
  61611. ExpectIntEQ(test_ctx.c_buff[idx], NEW_SA_MAJOR);
  61612. else
  61613. ExpectIntEQ(test_ctx.c_buff[idx+1], ecc_dsa_sa_algo);
  61614. }
  61615. break;
  61616. }
  61617. else {
  61618. idx += (int)len;
  61619. }
  61620. }
  61621. ExpectIntLT(idx, test_ctx.c_len);
  61622. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61623. wolfSSL_free(ssl_c);
  61624. wolfSSL_free(ssl_s);
  61625. wolfSSL_CTX_free(ctx_c);
  61626. wolfSSL_CTX_free(ctx_s);
  61627. #endif
  61628. return EXPECT_RESULT();
  61629. }
  61630. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  61631. static int test_revoked_loaded_int_cert_ctx_ready1(WOLFSSL_CTX* ctx)
  61632. {
  61633. EXPECT_DECLS;
  61634. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  61635. myVerifyAction = VERIFY_USE_PREVERFIY;
  61636. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  61637. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  61638. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  61639. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  61640. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  61641. WOLFSSL_SUCCESS);
  61642. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  61643. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  61644. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61645. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  61646. "./certs/crl/ca-int.pem",
  61647. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61648. return EXPECT_RESULT();
  61649. }
  61650. static int test_revoked_loaded_int_cert_ctx_ready2(WOLFSSL_CTX* ctx)
  61651. {
  61652. EXPECT_DECLS;
  61653. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  61654. myVerifyAction = VERIFY_USE_PREVERFIY;
  61655. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  61656. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  61657. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  61658. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  61659. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  61660. "./certs/intermediate/ca-int2-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  61661. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  61662. WOLFSSL_SUCCESS);
  61663. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  61664. "./certs/crl/ca-int2.pem",
  61665. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61666. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  61667. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  61668. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61669. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  61670. "./certs/crl/ca-int.pem",
  61671. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61672. return EXPECT_RESULT();
  61673. }
  61674. #endif
  61675. static int test_revoked_loaded_int_cert(void)
  61676. {
  61677. EXPECT_DECLS;
  61678. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  61679. test_ssl_cbf client_cbf;
  61680. test_ssl_cbf server_cbf;
  61681. struct {
  61682. const char* certPemFile;
  61683. const char* keyPemFile;
  61684. ctx_cb client_ctx_ready;
  61685. } test_params[] = {
  61686. {"./certs/intermediate/ca-int2-cert.pem",
  61687. "./certs/intermediate/ca-int2-key.pem",
  61688. test_revoked_loaded_int_cert_ctx_ready1},
  61689. {"./certs/intermediate/server-chain.pem",
  61690. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  61691. {"./certs/intermediate/server-chain-short.pem",
  61692. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  61693. };
  61694. size_t i;
  61695. printf("\n");
  61696. for (i = 0; i < XELEM_CNT(test_params); i++) {
  61697. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  61698. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  61699. printf("\tTesting with %s...\n", test_params[i].certPemFile);
  61700. server_cbf.certPemFile = test_params[i].certPemFile;
  61701. server_cbf.keyPemFile = test_params[i].keyPemFile;
  61702. client_cbf.ctx_ready = test_params[i].client_ctx_ready;
  61703. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  61704. &server_cbf, NULL), TEST_FAIL);
  61705. #ifndef WOLFSSL_HAPROXY
  61706. ExpectIntEQ(client_cbf.last_err, CRL_CERT_REVOKED);
  61707. #else
  61708. ExpectIntEQ(client_cbf.last_err, WOLFSSL_X509_V_ERR_CERT_REVOKED);
  61709. #endif
  61710. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  61711. if (!EXPECT_SUCCESS())
  61712. break;
  61713. printf("\t%s passed\n", test_params[i].certPemFile);
  61714. }
  61715. #endif
  61716. return EXPECT_RESULT();
  61717. }
  61718. static int test_dtls13_frag_ch_pq(void)
  61719. {
  61720. EXPECT_DECLS;
  61721. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  61722. && defined(WOLFSSL_DTLS_CH_FRAG) && defined(HAVE_LIBOQS)
  61723. WOLFSSL_CTX *ctx_c = NULL;
  61724. WOLFSSL_CTX *ctx_s = NULL;
  61725. WOLFSSL *ssl_c = NULL;
  61726. WOLFSSL *ssl_s = NULL;
  61727. struct test_memio_ctx test_ctx;
  61728. const char *test_str = "test";
  61729. int test_str_size;
  61730. byte buf[255];
  61731. int group = WOLFSSL_KYBER_LEVEL5;
  61732. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61733. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61734. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  61735. /* Add in a large post-quantum key share to make the CH long. */
  61736. ExpectIntEQ(wolfSSL_set_groups(ssl_c, &group, 1), WOLFSSL_SUCCESS);
  61737. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, group), WOLFSSL_SUCCESS);
  61738. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  61739. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61740. ExpectStrEQ(wolfSSL_get_curve_name(ssl_c), "KYBER_LEVEL5");
  61741. ExpectStrEQ(wolfSSL_get_curve_name(ssl_s), "KYBER_LEVEL5");
  61742. test_str_size = XSTRLEN("test") + 1;
  61743. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  61744. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  61745. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  61746. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  61747. wolfSSL_free(ssl_c);
  61748. wolfSSL_free(ssl_s);
  61749. wolfSSL_CTX_free(ctx_c);
  61750. wolfSSL_CTX_free(ctx_s);
  61751. #endif
  61752. return EXPECT_RESULT();
  61753. }
  61754. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  61755. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  61756. static int test_dtls_frag_ch_count_records(byte* b, int len)
  61757. {
  61758. DtlsRecordLayerHeader* dtlsRH;
  61759. int records = 0;
  61760. size_t recordLen;
  61761. while (len > 0) {
  61762. records++;
  61763. dtlsRH = (DtlsRecordLayerHeader*)b;
  61764. recordLen = (dtlsRH->length[0] << 8) | dtlsRH->length[1];
  61765. b += sizeof(DtlsRecordLayerHeader) + recordLen;
  61766. len -= sizeof(DtlsRecordLayerHeader) + recordLen;
  61767. }
  61768. return records;
  61769. }
  61770. #endif
  61771. static int test_dtls_frag_ch(void)
  61772. {
  61773. EXPECT_DECLS;
  61774. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  61775. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  61776. WOLFSSL_CTX *ctx_c = NULL;
  61777. WOLFSSL_CTX *ctx_s = NULL;
  61778. WOLFSSL *ssl_c = NULL;
  61779. WOLFSSL *ssl_s = NULL;
  61780. struct test_memio_ctx test_ctx;
  61781. static unsigned int DUMMY_MTU = 256;
  61782. unsigned char four_frag_CH[] = {
  61783. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61784. 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61785. 0xce, 0xfe, 0xfd, 0xf3, 0x94, 0x01, 0x33, 0x2c, 0xcf, 0x2c, 0x47, 0xb1,
  61786. 0xe5, 0xa1, 0x7b, 0x19, 0x3e, 0xac, 0x68, 0xdd, 0xe6, 0x17, 0x6b, 0x85,
  61787. 0xad, 0x5f, 0xfc, 0x7f, 0x6e, 0xf0, 0xb9, 0xe0, 0x2e, 0xca, 0x47, 0x00,
  61788. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  61789. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  61790. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  61791. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  61792. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x02,
  61793. 0x7c, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  61794. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  61795. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  61796. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  61797. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  61798. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x02, 0x39, 0x02, 0x37, 0x00, 0x17,
  61799. 0x00, 0x41, 0x04, 0x94, 0xdf, 0x36, 0xd7, 0xb3, 0x90, 0x6d, 0x01, 0xa1,
  61800. 0xe6, 0xed, 0x67, 0xf4, 0xd9, 0x9d, 0x2c, 0xac, 0x57, 0x74, 0xff, 0x19,
  61801. 0xbe, 0x5a, 0xc9, 0x30, 0x11, 0xb7, 0x2b, 0x59, 0x47, 0x80, 0x7c, 0xa9,
  61802. 0xb7, 0x31, 0x8c, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61803. 0x00, 0x01, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00,
  61804. 0xce, 0x00, 0x00, 0xce, 0x9e, 0x13, 0x74, 0x3b, 0x86, 0xba, 0x69, 0x1f,
  61805. 0x12, 0xf7, 0xcd, 0x78, 0x53, 0xe8, 0x50, 0x4d, 0x71, 0x3f, 0x4b, 0x4e,
  61806. 0xeb, 0x3e, 0xe5, 0x43, 0x54, 0x78, 0x17, 0x6d, 0x00, 0x18, 0x00, 0x61,
  61807. 0x04, 0xd1, 0x99, 0x66, 0x4f, 0xda, 0xc7, 0x12, 0x3b, 0xff, 0xb2, 0xd6,
  61808. 0x2f, 0x35, 0xb6, 0x17, 0x1f, 0xb3, 0xd0, 0xb6, 0x52, 0xff, 0x97, 0x8b,
  61809. 0x01, 0xe8, 0xd9, 0x68, 0x71, 0x40, 0x02, 0xd5, 0x68, 0x3a, 0x58, 0xb2,
  61810. 0x5d, 0xee, 0xa4, 0xe9, 0x5f, 0xf4, 0xaf, 0x3e, 0x30, 0x9c, 0x3e, 0x2b,
  61811. 0xda, 0x61, 0x43, 0x99, 0x02, 0x35, 0x33, 0x9f, 0xcf, 0xb5, 0xd3, 0x28,
  61812. 0x19, 0x9d, 0x1c, 0xbe, 0x69, 0x07, 0x9e, 0xfc, 0xe4, 0x8e, 0xcd, 0x86,
  61813. 0x4a, 0x1b, 0xf0, 0xfc, 0x17, 0x94, 0x66, 0x53, 0xda, 0x24, 0x5e, 0xaf,
  61814. 0xce, 0xec, 0x62, 0x4c, 0x06, 0xb4, 0x52, 0x94, 0xb1, 0x4a, 0x7a, 0x8c,
  61815. 0x4f, 0x00, 0x19, 0x00, 0x85, 0x04, 0x00, 0x27, 0xeb, 0x99, 0x49, 0x7f,
  61816. 0xcb, 0x2c, 0x46, 0x54, 0x2d, 0x93, 0x5d, 0x25, 0x92, 0x58, 0x5e, 0x06,
  61817. 0xc3, 0x7c, 0xfb, 0x9a, 0xa7, 0xec, 0xcd, 0x9f, 0xe1, 0x6b, 0x2d, 0x78,
  61818. 0xf5, 0x16, 0xa9, 0x20, 0x52, 0x48, 0x19, 0x0f, 0x1a, 0xd0, 0xce, 0xd8,
  61819. 0x68, 0xb1, 0x4e, 0x7f, 0x33, 0x03, 0x7d, 0x0c, 0x39, 0xdb, 0x9c, 0x4b,
  61820. 0xf4, 0xe7, 0xc2, 0xf5, 0xdd, 0x51, 0x9b, 0x03, 0xa8, 0x53, 0x2b, 0xe6,
  61821. 0x00, 0x15, 0x4b, 0xff, 0xd2, 0xa0, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00,
  61822. 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00,
  61823. 0x00, 0x00, 0x01, 0x9c, 0x00, 0x00, 0xce, 0x58, 0x30, 0x10, 0x3d, 0x46,
  61824. 0xcc, 0xca, 0x1a, 0x44, 0xc8, 0x58, 0x9b, 0x27, 0x17, 0x67, 0x31, 0x96,
  61825. 0x8a, 0x66, 0x39, 0xf4, 0xcc, 0xc1, 0x9f, 0x12, 0x1f, 0x01, 0x30, 0x50,
  61826. 0x16, 0xd6, 0x89, 0x97, 0xa3, 0x66, 0xd7, 0x99, 0x50, 0x09, 0x6e, 0x80,
  61827. 0x87, 0xe4, 0xa2, 0x88, 0xae, 0xb4, 0x23, 0x57, 0x2f, 0x12, 0x60, 0xe7,
  61828. 0x7d, 0x44, 0x2d, 0xad, 0xbe, 0xe9, 0x0d, 0x01, 0x00, 0x01, 0x00, 0xd5,
  61829. 0xdd, 0x62, 0xee, 0xf3, 0x0e, 0xd9, 0x30, 0x0e, 0x38, 0xf3, 0x48, 0xf4,
  61830. 0xc9, 0x8f, 0x8c, 0x20, 0xf7, 0xd3, 0xa8, 0xb3, 0x87, 0x3c, 0x98, 0x5d,
  61831. 0x70, 0xc5, 0x03, 0x76, 0xb7, 0xd5, 0x0b, 0x7b, 0x23, 0x97, 0x6b, 0xe3,
  61832. 0xb5, 0x18, 0xeb, 0x64, 0x55, 0x18, 0xb2, 0x8a, 0x90, 0x1a, 0x8f, 0x0e,
  61833. 0x15, 0xda, 0xb1, 0x8e, 0x7f, 0xee, 0x1f, 0xe0, 0x3b, 0xb9, 0xed, 0xfc,
  61834. 0x4e, 0x3f, 0x78, 0x16, 0x39, 0x95, 0x5f, 0xb7, 0xcb, 0x65, 0x55, 0x72,
  61835. 0x7b, 0x7d, 0x86, 0x2f, 0x8a, 0xe5, 0xee, 0xf7, 0x57, 0x40, 0xf3, 0xc4,
  61836. 0x96, 0x4f, 0x11, 0x4d, 0x85, 0xf9, 0x56, 0xfa, 0x3d, 0xf0, 0xc9, 0xa4,
  61837. 0xec, 0x1e, 0xaa, 0x47, 0x90, 0x53, 0xdf, 0xe1, 0xb7, 0x78, 0x18, 0xeb,
  61838. 0xdd, 0x0d, 0x89, 0xb7, 0xf6, 0x15, 0x0e, 0x55, 0x12, 0xb3, 0x23, 0x17,
  61839. 0x0b, 0x59, 0x6f, 0x83, 0x05, 0x6b, 0xa6, 0xf8, 0x6c, 0x3a, 0x9b, 0x1b,
  61840. 0x50, 0x93, 0x51, 0xea, 0x95, 0x2d, 0x99, 0x96, 0x38, 0x16, 0xfe, 0xfd,
  61841. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x7e, 0x01, 0x00,
  61842. 0x02, 0xdc, 0x00, 0x00, 0x00, 0x02, 0x6a, 0x00, 0x00, 0x72, 0x2d, 0x66,
  61843. 0x3e, 0xf2, 0x36, 0x5a, 0xf2, 0x23, 0x8f, 0x28, 0x09, 0xa9, 0x55, 0x8c,
  61844. 0x8f, 0xc0, 0x0d, 0x61, 0x98, 0x33, 0x56, 0x87, 0x7a, 0xfd, 0xa7, 0x50,
  61845. 0x71, 0x84, 0x2e, 0x41, 0x58, 0x00, 0x87, 0xd9, 0x27, 0xe5, 0x7b, 0xf4,
  61846. 0x6d, 0x84, 0x4e, 0x2e, 0x0c, 0x80, 0x0c, 0xf3, 0x8a, 0x02, 0x4b, 0x99,
  61847. 0x3a, 0x1f, 0x9f, 0x18, 0x7d, 0x1c, 0xec, 0xad, 0x60, 0x54, 0xa6, 0xa3,
  61848. 0x2c, 0x82, 0x5e, 0xf8, 0x8f, 0xae, 0xe1, 0xc4, 0x82, 0x7e, 0x43, 0x43,
  61849. 0xc5, 0x99, 0x49, 0x05, 0xd3, 0xf6, 0xdf, 0xa1, 0xb5, 0x2d, 0x0c, 0x13,
  61850. 0x2f, 0x1e, 0xb6, 0x28, 0x7c, 0x5c, 0xa1, 0x02, 0x6b, 0x8d, 0xa3, 0xeb,
  61851. 0xd4, 0x58, 0xe6, 0xa0, 0x7e, 0x6b, 0xaa, 0x09, 0x43, 0x67, 0x71, 0x87,
  61852. 0xa5, 0xcb, 0x68, 0xf3
  61853. };
  61854. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61855. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61856. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  61857. /* Fragment msgs */
  61858. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_c, DUMMY_MTU), WOLFSSL_SUCCESS);
  61859. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_s, DUMMY_MTU), WOLFSSL_SUCCESS);
  61860. /* Add in some key shares to make the CH long */
  61861. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP256R1),
  61862. WOLFSSL_SUCCESS);
  61863. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP384R1),
  61864. WOLFSSL_SUCCESS);
  61865. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP521R1),
  61866. WOLFSSL_SUCCESS);
  61867. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_FFDHE_2048),
  61868. WOLFSSL_SUCCESS);
  61869. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  61870. /* Reject fragmented first CH */
  61871. ExpectIntEQ(test_dtls_frag_ch_count_records(four_frag_CH,
  61872. sizeof(four_frag_CH)), 4);
  61873. XMEMCPY(test_ctx.s_buff, four_frag_CH, sizeof(four_frag_CH));
  61874. test_ctx.s_len = sizeof(four_frag_CH);
  61875. while (test_ctx.s_len > 0 && EXPECT_SUCCESS()) {
  61876. int s_len = test_ctx.s_len;
  61877. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61878. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61879. /* Fail if we didn't advance the buffer to avoid infinite loops */
  61880. ExpectIntLT(test_ctx.s_len, s_len);
  61881. }
  61882. /* Expect all fragments read */
  61883. ExpectIntEQ(test_ctx.s_len, 0);
  61884. /* Expect quietly dropping fragmented first CH */
  61885. ExpectIntEQ(test_ctx.c_len, 0);
  61886. /* CH1 */
  61887. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61888. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61889. /* Count records. Expect 1 unfragmented CH */
  61890. ExpectIntEQ(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  61891. test_ctx.s_len), 1);
  61892. /* HRR */
  61893. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61894. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61895. /* CH2 */
  61896. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61897. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61898. /* Count records. Expect fragmented CH */
  61899. ExpectIntGT(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  61900. test_ctx.s_len), 1);
  61901. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61902. wolfSSL_free(ssl_c);
  61903. wolfSSL_free(ssl_s);
  61904. wolfSSL_CTX_free(ctx_c);
  61905. wolfSSL_CTX_free(ctx_s);
  61906. ssl_c = ssl_s = NULL;
  61907. ctx_c = ctx_s = NULL;
  61908. #endif
  61909. return EXPECT_RESULT();
  61910. }
  61911. static int test_dtls_empty_keyshare_with_cookie(void)
  61912. {
  61913. EXPECT_DECLS;
  61914. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  61915. WOLFSSL_CTX *ctx_s = NULL;
  61916. WOLFSSL *ssl_s = NULL;
  61917. struct test_memio_ctx test_ctx;
  61918. unsigned char ch_empty_keyshare_with_cookie[] = {
  61919. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  61920. 0x12, 0x01, 0x00, 0x01, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
  61921. 0x06, 0xfe, 0xfd, 0xfb, 0x8c, 0x9b, 0x28, 0xae, 0x50, 0x1c, 0x4d, 0xf3,
  61922. 0xb8, 0xcf, 0x4d, 0xd8, 0x7e, 0x93, 0x13, 0x7b, 0x9e, 0xd9, 0xeb, 0xe9,
  61923. 0x13, 0x4b, 0x0d, 0x7f, 0x2e, 0x43, 0x62, 0x8c, 0xe4, 0x57, 0x79, 0x00,
  61924. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  61925. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  61926. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  61927. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  61928. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x00,
  61929. 0xa6, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x2c, 0x00, 0x47,
  61930. 0x00, 0x45, 0x20, 0xee, 0x4b, 0x17, 0x70, 0x63, 0xa0, 0x4c, 0x82, 0xbf,
  61931. 0x43, 0x01, 0x7d, 0x8d, 0xc1, 0x1b, 0x4e, 0x9b, 0xa0, 0x3c, 0x53, 0x1f,
  61932. 0xb7, 0xd1, 0x10, 0x81, 0xa8, 0xdf, 0xdf, 0x8c, 0x7f, 0xf3, 0x11, 0x13,
  61933. 0x01, 0x02, 0x3d, 0x3b, 0x7d, 0x14, 0x2c, 0x31, 0xb3, 0x60, 0x72, 0x4d,
  61934. 0xe5, 0x1a, 0xb2, 0xa3, 0x61, 0x77, 0x73, 0x03, 0x40, 0x0e, 0x5f, 0xc5,
  61935. 0x61, 0x38, 0x43, 0x56, 0x21, 0x4a, 0x95, 0xd5, 0x35, 0xa8, 0x0d, 0x00,
  61936. 0x0d, 0x00, 0x2a, 0x00, 0x28, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02,
  61937. 0x03, 0xfe, 0x0b, 0xfe, 0x0e, 0xfe, 0xa0, 0xfe, 0xa3, 0xfe, 0xa5, 0x08,
  61938. 0x06, 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06,
  61939. 0x01, 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00,
  61940. 0x18, 0x00, 0x16, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01,
  61941. 0x00, 0x02, 0x3a, 0x02, 0x3c, 0x02, 0x3d, 0x2f, 0x3a, 0x2f, 0x3c, 0x2f,
  61942. 0x3d, 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x00, 0x02, 0x00, 0x00
  61943. };
  61944. DtlsRecordLayerHeader* dtlsRH;
  61945. byte sequence_number[8];
  61946. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  61947. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61948. XMEMCPY(test_ctx.s_buff, ch_empty_keyshare_with_cookie,
  61949. sizeof(ch_empty_keyshare_with_cookie));
  61950. test_ctx.s_len = sizeof(ch_empty_keyshare_with_cookie);
  61951. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  61952. NULL, wolfDTLSv1_3_server_method), 0);
  61953. /* CH1 */
  61954. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61955. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61956. /* Expect an alert. A plaintext alert should be exactly 15 bytes. */
  61957. ExpectIntEQ(test_ctx.c_len, 15);
  61958. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  61959. ExpectIntEQ(dtlsRH->type, alert);
  61960. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  61961. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  61962. sequence_number[7] = 1;
  61963. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  61964. sizeof(sequence_number)), 0);
  61965. ExpectIntEQ(dtlsRH->length[0], 0);
  61966. ExpectIntEQ(dtlsRH->length[1], 2);
  61967. ExpectIntEQ(test_ctx.c_buff[13], alert_fatal);
  61968. ExpectIntEQ(test_ctx.c_buff[14], illegal_parameter);
  61969. wolfSSL_free(ssl_s);
  61970. wolfSSL_CTX_free(ctx_s);
  61971. #endif
  61972. return EXPECT_RESULT();
  61973. }
  61974. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  61975. defined(HAVE_LIBOQS)
  61976. static void test_tls13_pq_groups_ctx_ready(WOLFSSL_CTX* ctx)
  61977. {
  61978. int group = WOLFSSL_KYBER_LEVEL5;
  61979. AssertIntEQ(wolfSSL_CTX_set_groups(ctx, &group, 1), WOLFSSL_SUCCESS);
  61980. }
  61981. static void test_tls13_pq_groups_on_result(WOLFSSL* ssl)
  61982. {
  61983. AssertStrEQ(wolfSSL_get_curve_name(ssl), "KYBER_LEVEL5");
  61984. }
  61985. #endif
  61986. static int test_tls13_pq_groups(void)
  61987. {
  61988. EXPECT_DECLS;
  61989. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  61990. defined(HAVE_LIBOQS)
  61991. callback_functions func_cb_client;
  61992. callback_functions func_cb_server;
  61993. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  61994. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  61995. func_cb_client.method = wolfTLSv1_3_client_method;
  61996. func_cb_server.method = wolfTLSv1_3_server_method;
  61997. func_cb_client.ctx_ready = test_tls13_pq_groups_ctx_ready;
  61998. func_cb_client.on_result = test_tls13_pq_groups_on_result;
  61999. func_cb_server.on_result = test_tls13_pq_groups_on_result;
  62000. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  62001. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  62002. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  62003. #endif
  62004. return EXPECT_RESULT();
  62005. }
  62006. static int test_tls13_early_data(void)
  62007. {
  62008. EXPECT_DECLS;
  62009. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  62010. defined(WOLFSSL_EARLY_DATA) && defined(HAVE_SESSION_TICKET)
  62011. int written = 0;
  62012. int read = 0;
  62013. size_t i;
  62014. int splitEarlyData;
  62015. char msg[] = "This is early data";
  62016. char msg2[] = "This is client data";
  62017. char msg3[] = "This is server data";
  62018. char msg4[] = "This is server immediate data";
  62019. char msgBuf[50];
  62020. struct {
  62021. method_provider client_meth;
  62022. method_provider server_meth;
  62023. const char* tls_version;
  62024. int isUdp;
  62025. } params[] = {
  62026. #ifdef WOLFSSL_TLS13
  62027. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  62028. "TLS 1.3", 0 },
  62029. #endif
  62030. #ifdef WOLFSSL_DTLS13
  62031. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  62032. "DTLS 1.3", 1 },
  62033. #endif
  62034. };
  62035. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  62036. for (splitEarlyData = 0; splitEarlyData < 2; splitEarlyData++) {
  62037. struct test_memio_ctx test_ctx;
  62038. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  62039. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  62040. WOLFSSL_SESSION *sess = NULL;
  62041. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62042. fprintf(stderr, "\tEarly data with %s\n", params[i].tls_version);
  62043. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  62044. &ssl_s, params[i].client_meth, params[i].server_meth), 0);
  62045. /* Get a ticket so that we can do 0-RTT on the next connection */
  62046. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62047. /* Make sure we read the ticket */
  62048. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), -1);
  62049. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62050. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  62051. wolfSSL_free(ssl_c);
  62052. ssl_c = NULL;
  62053. wolfSSL_free(ssl_s);
  62054. ssl_s = NULL;
  62055. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62056. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62057. params[i].client_meth, params[i].server_meth), 0);
  62058. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  62059. #ifdef WOLFSSL_DTLS13
  62060. if (params[i].isUdp) {
  62061. #ifdef WOLFSSL_DTLS13_NO_HRR_ON_RESUME
  62062. ExpectIntEQ(wolfSSL_dtls13_no_hrr_on_resume(ssl_s, 1), WOLFSSL_SUCCESS);
  62063. #else
  62064. /* Let's test this but we generally don't recommend turning off the
  62065. * cookie exchange */
  62066. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  62067. #endif
  62068. }
  62069. #endif
  62070. /* Test 0-RTT data */
  62071. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  62072. &written), sizeof(msg));
  62073. ExpectIntEQ(written, sizeof(msg));
  62074. if (splitEarlyData) {
  62075. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  62076. &written), sizeof(msg));
  62077. ExpectIntEQ(written, sizeof(msg));
  62078. }
  62079. /* Read first 0-RTT data (if split otherwise entire data) */
  62080. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  62081. &read), sizeof(msg));
  62082. ExpectIntEQ(read, sizeof(msg));
  62083. ExpectStrEQ(msg, msgBuf);
  62084. /* Test 0.5-RTT data */
  62085. ExpectIntEQ(wolfSSL_write(ssl_s, msg4, sizeof(msg4)), sizeof(msg4));
  62086. if (splitEarlyData) {
  62087. /* Read second 0-RTT data */
  62088. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  62089. &read), sizeof(msg));
  62090. ExpectIntEQ(read, sizeof(msg));
  62091. ExpectStrEQ(msg, msgBuf);
  62092. }
  62093. if (params[i].isUdp) {
  62094. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  62095. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), APP_DATA_READY);
  62096. /* Read server 0.5-RTT data */
  62097. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  62098. ExpectStrEQ(msg4, msgBuf);
  62099. /* Complete handshake */
  62100. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  62101. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62102. /* Use wolfSSL_is_init_finished to check if handshake is complete. Normally
  62103. * a user would loop until it is true but here we control both sides so we
  62104. * just assert the expected value. wolfSSL_read_early_data does not provide
  62105. * handshake status to us with non-blocking IO and we can't use
  62106. * wolfSSL_accept as TLS layer may return ZERO_RETURN due to early data
  62107. * parsing logic. */
  62108. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  62109. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  62110. &read), 0);
  62111. ExpectIntEQ(read, 0);
  62112. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  62113. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  62114. }
  62115. else {
  62116. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  62117. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  62118. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  62119. &read), 0);
  62120. ExpectIntEQ(read, 0);
  62121. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  62122. /* Read server 0.5-RTT data */
  62123. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  62124. ExpectStrEQ(msg4, msgBuf);
  62125. }
  62126. /* Test bi-directional write */
  62127. ExpectIntEQ(wolfSSL_write(ssl_c, msg2, sizeof(msg2)), sizeof(msg2));
  62128. ExpectIntEQ(wolfSSL_read(ssl_s, msgBuf, sizeof(msgBuf)), sizeof(msg2));
  62129. ExpectStrEQ(msg2, msgBuf);
  62130. ExpectIntEQ(wolfSSL_write(ssl_s, msg3, sizeof(msg3)), sizeof(msg3));
  62131. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg3));
  62132. ExpectStrEQ(msg3, msgBuf);
  62133. ExpectTrue(wolfSSL_session_reused(ssl_c));
  62134. ExpectTrue(wolfSSL_session_reused(ssl_s));
  62135. wolfSSL_SESSION_free(sess);
  62136. wolfSSL_free(ssl_c);
  62137. wolfSSL_free(ssl_s);
  62138. wolfSSL_CTX_free(ctx_c);
  62139. wolfSSL_CTX_free(ctx_s);
  62140. }
  62141. }
  62142. #endif
  62143. return EXPECT_RESULT();
  62144. }
  62145. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  62146. static int test_self_signed_stapling_client_v1_ctx_ready(WOLFSSL_CTX* ctx)
  62147. {
  62148. EXPECT_DECLS;
  62149. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  62150. ExpectIntEQ(wolfSSL_CTX_UseOCSPStapling(ctx, WOLFSSL_CSR_OCSP,
  62151. WOLFSSL_CSR_OCSP_USE_NONCE), 1);
  62152. return EXPECT_RESULT();
  62153. }
  62154. #endif
  62155. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  62156. static int test_self_signed_stapling_client_v2_ctx_ready(WOLFSSL_CTX* ctx)
  62157. {
  62158. EXPECT_DECLS;
  62159. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  62160. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP,
  62161. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  62162. return EXPECT_RESULT();
  62163. }
  62164. static int test_self_signed_stapling_client_v2_multi_ctx_ready(WOLFSSL_CTX* ctx)
  62165. {
  62166. EXPECT_DECLS;
  62167. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  62168. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP_MULTI,
  62169. 0), 1);
  62170. return EXPECT_RESULT();
  62171. }
  62172. #endif
  62173. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  62174. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  62175. static int test_self_signed_stapling_server_ctx_ready(WOLFSSL_CTX* ctx)
  62176. {
  62177. EXPECT_DECLS;
  62178. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  62179. return EXPECT_RESULT();
  62180. }
  62181. #endif
  62182. static int test_self_signed_stapling(void)
  62183. {
  62184. EXPECT_DECLS;
  62185. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  62186. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  62187. test_ssl_cbf client_cbf;
  62188. test_ssl_cbf server_cbf;
  62189. size_t i;
  62190. struct {
  62191. method_provider client_meth;
  62192. method_provider server_meth;
  62193. ctx_cb client_ctx;
  62194. const char* tls_version;
  62195. } params[] = {
  62196. #if defined(WOLFSSL_TLS13) && defined(HAVE_CERTIFICATE_STATUS_REQUEST)
  62197. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  62198. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_3 v1" },
  62199. #endif
  62200. #ifndef WOLFSSL_NO_TLS12
  62201. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  62202. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  62203. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_2 v1" },
  62204. #endif
  62205. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  62206. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  62207. test_self_signed_stapling_client_v2_ctx_ready, "TLSv1_2 v2" },
  62208. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  62209. test_self_signed_stapling_client_v2_multi_ctx_ready,
  62210. "TLSv1_2 v2 multi" },
  62211. #endif
  62212. #endif
  62213. };
  62214. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  62215. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  62216. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  62217. printf("\nTesting self-signed cert with status request: %s\n",
  62218. params[i].tls_version);
  62219. client_cbf.method = params[i].client_meth;
  62220. client_cbf.ctx_ready = params[i].client_ctx;
  62221. server_cbf.method = params[i].server_meth;
  62222. server_cbf.certPemFile = "certs/ca-cert.pem";
  62223. server_cbf.keyPemFile = "certs/ca-key.pem";
  62224. server_cbf.ctx_ready = test_self_signed_stapling_server_ctx_ready;
  62225. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  62226. &server_cbf, NULL), TEST_SUCCESS);
  62227. }
  62228. #endif
  62229. return EXPECT_RESULT();
  62230. }
  62231. static int test_tls_multi_handshakes_one_record(void)
  62232. {
  62233. EXPECT_DECLS;
  62234. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  62235. struct test_memio_ctx test_ctx;
  62236. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  62237. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  62238. RecordLayerHeader* rh = NULL;
  62239. byte *len ;
  62240. int newRecIdx = RECORD_HEADER_SZ;
  62241. int idx = 0;
  62242. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62243. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62244. wolfTLS_client_method, wolfTLSv1_2_server_method), 0);
  62245. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  62246. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62247. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  62248. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62249. /* Combine server handshake msgs into one record */
  62250. while (idx < test_ctx.c_len) {
  62251. word16 recLen;
  62252. rh = (RecordLayerHeader*)(test_ctx.c_buff + idx);
  62253. len = &rh->length[0];
  62254. ato16((const byte*)len, &recLen);
  62255. idx += RECORD_HEADER_SZ;
  62256. XMEMMOVE(test_ctx.c_buff + newRecIdx, test_ctx.c_buff + idx,
  62257. (size_t)recLen);
  62258. newRecIdx += recLen;
  62259. idx += recLen;
  62260. }
  62261. rh = (RecordLayerHeader*)(test_ctx.c_buff);
  62262. len = &rh->length[0];
  62263. c16toa(newRecIdx - RECORD_HEADER_SZ, len);
  62264. test_ctx.c_len = newRecIdx;
  62265. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  62266. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62267. wolfSSL_free(ssl_c);
  62268. wolfSSL_free(ssl_s);
  62269. wolfSSL_CTX_free(ctx_c);
  62270. wolfSSL_CTX_free(ctx_s);
  62271. #endif
  62272. return EXPECT_RESULT();
  62273. }
  62274. static int test_write_dup(void)
  62275. {
  62276. EXPECT_DECLS;
  62277. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_WRITE_DUP)
  62278. size_t i, j;
  62279. char hiWorld[] = "dup message";
  62280. char readData[sizeof(hiWorld) + 5];
  62281. struct {
  62282. method_provider client_meth;
  62283. method_provider server_meth;
  62284. const char* version_name;
  62285. int version;
  62286. } methods[] = {
  62287. #ifndef WOLFSSL_NO_TLS12
  62288. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLS 1.2", WOLFSSL_TLSV1_2},
  62289. #endif
  62290. #ifdef WOLFSSL_TLS13
  62291. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLS 1.3", WOLFSSL_TLSV1_3},
  62292. #endif
  62293. };
  62294. struct {
  62295. const char* cipher;
  62296. int version;
  62297. } ciphers[] = {
  62298. /* For simplicity the macros are copied from internal.h */
  62299. /* TLS 1.2 */
  62300. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && !defined(NO_SHA256)
  62301. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  62302. #ifndef NO_RSA
  62303. {"ECDHE-RSA-CHACHA20-POLY1305", WOLFSSL_TLSV1_2},
  62304. #endif
  62305. #endif
  62306. #if !defined(NO_DH) && !defined(NO_RSA) && !defined(NO_TLS_DH)
  62307. {"DHE-RSA-CHACHA20-POLY1305", WOLFSSL_TLSV1_2},
  62308. #endif
  62309. #endif
  62310. #if !defined(NO_DH) && !defined(NO_AES) && !defined(NO_TLS) && \
  62311. !defined(NO_RSA) && defined(HAVE_AESGCM) && !defined(NO_TLS_DH)
  62312. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  62313. {"DHE-RSA-AES128-GCM-SHA256", WOLFSSL_TLSV1_2},
  62314. #endif
  62315. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  62316. {"DHE-RSA-AES256-GCM-SHA384", WOLFSSL_TLSV1_2},
  62317. #endif
  62318. #endif
  62319. #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)) \
  62320. && !defined(NO_TLS) && !defined(NO_AES)
  62321. #ifdef HAVE_AESGCM
  62322. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  62323. #ifndef NO_RSA
  62324. {"ECDHE-RSA-AES128-GCM-SHA256", WOLFSSL_TLSV1_2},
  62325. #endif
  62326. #endif
  62327. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  62328. #ifndef NO_RSA
  62329. {"ECDHE-RSA-AES256-GCM-SHA384", WOLFSSL_TLSV1_2},
  62330. #endif
  62331. #endif
  62332. #endif
  62333. #endif
  62334. /* TLS 1.3 */
  62335. #ifdef WOLFSSL_TLS13
  62336. #ifdef HAVE_AESGCM
  62337. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  62338. {"TLS13-AES128-GCM-SHA256", WOLFSSL_TLSV1_3},
  62339. #endif
  62340. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  62341. {"TLS13-AES256-GCM-SHA384", WOLFSSL_TLSV1_3},
  62342. #endif
  62343. #endif
  62344. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  62345. #ifndef NO_SHA256
  62346. {"TLS13-CHACHA20-POLY1305-SHA256", WOLFSSL_TLSV1_3},
  62347. #endif
  62348. #endif
  62349. #ifdef HAVE_AESCCM
  62350. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  62351. {"TLS13-AES128-CCM-SHA256", WOLFSSL_TLSV1_3},
  62352. #endif
  62353. #endif
  62354. #endif
  62355. };
  62356. for (i = 0; i < XELEM_CNT(methods); i++) {
  62357. for (j = 0; j < XELEM_CNT(ciphers) && !EXPECT_FAIL(); j++) {
  62358. struct test_memio_ctx test_ctx;
  62359. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  62360. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  62361. WOLFSSL *ssl_c2 = NULL;
  62362. if (methods[i].version != ciphers[j].version)
  62363. continue;
  62364. if (i == 0 && j == 0)
  62365. printf("\n");
  62366. printf("Testing %s with %s... ", methods[i].version_name,
  62367. ciphers[j].cipher);
  62368. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62369. test_ctx.c_ciphers = test_ctx.s_ciphers = ciphers[j].cipher;
  62370. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62371. methods[i].client_meth, methods[i].server_meth), 0);
  62372. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62373. ExpectNotNull(ssl_c2 = wolfSSL_write_dup(ssl_c));
  62374. ExpectIntEQ(wolfSSL_write(ssl_c, hiWorld, sizeof(hiWorld)),
  62375. WRITE_DUP_WRITE_E);
  62376. ExpectIntEQ(wolfSSL_write(ssl_c2, hiWorld, sizeof(hiWorld)),
  62377. sizeof(hiWorld));
  62378. ExpectIntEQ(wolfSSL_read(ssl_s, readData, sizeof(readData)),
  62379. sizeof(hiWorld));
  62380. ExpectIntEQ(wolfSSL_write(ssl_s, hiWorld, sizeof(hiWorld)),
  62381. sizeof(hiWorld));
  62382. ExpectIntEQ(wolfSSL_read(ssl_c2, readData, sizeof(readData)),
  62383. WRITE_DUP_READ_E);
  62384. ExpectIntEQ(wolfSSL_read(ssl_c, readData, sizeof(readData)),
  62385. sizeof(hiWorld));
  62386. if (EXPECT_SUCCESS())
  62387. printf("ok\n");
  62388. else
  62389. printf("failed\n");
  62390. wolfSSL_free(ssl_c);
  62391. wolfSSL_free(ssl_c2);
  62392. wolfSSL_free(ssl_s);
  62393. wolfSSL_CTX_free(ctx_c);
  62394. wolfSSL_CTX_free(ctx_s);
  62395. }
  62396. }
  62397. #endif
  62398. return EXPECT_RESULT();
  62399. }
  62400. static int test_read_write_hs(void)
  62401. {
  62402. EXPECT_DECLS;
  62403. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  62404. WOLFSSL_CTX *ctx_s = NULL, *ctx_c = NULL;
  62405. WOLFSSL *ssl_s = NULL, *ssl_c = NULL;
  62406. struct test_memio_ctx test_ctx;
  62407. uint8_t test_buffer[16];
  62408. unsigned int test;
  62409. /* test == 0 : client writes, server reads */
  62410. /* test == 1 : server writes, client reads */
  62411. for (test = 0; test < 2; test++) {
  62412. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62413. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62414. wolfTLSv1_2_client_method,
  62415. wolfTLSv1_2_server_method), 0);
  62416. ExpectIntEQ(wolfSSL_set_group_messages(ssl_s), WOLFSSL_SUCCESS);
  62417. /* CH -> */
  62418. if (test == 0) {
  62419. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  62420. } else {
  62421. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  62422. sizeof(test_buffer)), -1);
  62423. }
  62424. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62425. /* <- SH + SKE + SHD */
  62426. if (test == 0) {
  62427. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  62428. sizeof(test_buffer)), -1);
  62429. } else {
  62430. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), -1);
  62431. }
  62432. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62433. /* -> CKE + CLIENT FINISHED */
  62434. if (test == 0) {
  62435. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  62436. } else {
  62437. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  62438. sizeof(test_buffer)), -1);
  62439. }
  62440. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62441. /* abide clang static analyzer */
  62442. if (ssl_s != NULL) {
  62443. /* disable group message to separate sending of ChangeCipherspec
  62444. * from Finished */
  62445. ssl_s->options.groupMessages = 0;
  62446. }
  62447. /* allow writing of CS, but not FINISHED */
  62448. test_ctx.c_len = TEST_MEMIO_BUF_SZ - 6;
  62449. /* <- CS */
  62450. if (test == 0) {
  62451. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  62452. sizeof(test_buffer)), -1);
  62453. } else {
  62454. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), -1);
  62455. }
  62456. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_WRITE);
  62457. /* move CS message where the client can read it */
  62458. memmove(test_ctx.c_buff,
  62459. (test_ctx.c_buff + TEST_MEMIO_BUF_SZ - 6), 6);
  62460. test_ctx.c_len = 6;
  62461. /* read CS */
  62462. if (test == 0) {
  62463. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  62464. } else {
  62465. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  62466. sizeof(test_buffer)), -1);
  62467. }
  62468. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62469. ExpectIntEQ(test_ctx.c_len, 0);
  62470. if (test == 0) {
  62471. /* send SERVER FINISHED */
  62472. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  62473. sizeof(test_buffer)), -1);
  62474. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1),
  62475. WOLFSSL_ERROR_WANT_READ);
  62476. } else {
  62477. /* send SERVER FINISHED + App Data */
  62478. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), 5);
  62479. }
  62480. ExpectIntGT(test_ctx.c_len, 0);
  62481. /* Send and receive the data */
  62482. if (test == 0) {
  62483. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), 5);
  62484. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  62485. sizeof(test_buffer)), 5);
  62486. } else {
  62487. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  62488. sizeof(test_buffer)), 5);
  62489. }
  62490. ExpectBufEQ(test_buffer, "hello", 5);
  62491. wolfSSL_free(ssl_c);
  62492. wolfSSL_free(ssl_s);
  62493. wolfSSL_CTX_free(ctx_c);
  62494. wolfSSL_CTX_free(ctx_s);
  62495. ssl_c = ssl_s = NULL;
  62496. ctx_c = ctx_s = NULL;
  62497. }
  62498. #endif
  62499. return EXPECT_RESULT();
  62500. }
  62501. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA)
  62502. static const char* test_get_signature_nid_siglag;
  62503. static int test_get_signature_nid_sig;
  62504. static int test_get_signature_nid_hash;
  62505. static int test_get_signature_nid_ssl_ready(WOLFSSL* ssl)
  62506. {
  62507. EXPECT_DECLS;
  62508. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "ALL"), WOLFSSL_SUCCESS);
  62509. if (!wolfSSL_is_server(ssl)) {
  62510. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  62511. test_get_signature_nid_siglag), WOLFSSL_SUCCESS);
  62512. }
  62513. return EXPECT_RESULT();
  62514. }
  62515. static int test_get_signature_nid_on_hs_client(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  62516. {
  62517. EXPECT_DECLS;
  62518. int nid = 0;
  62519. (void)ctx;
  62520. if (XSTRSTR(wolfSSL_get_cipher(*ssl), "TLS_RSA_") == NULL) {
  62521. ExpectIntEQ(SSL_get_peer_signature_type_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  62522. ExpectIntEQ(nid, test_get_signature_nid_sig);
  62523. ExpectIntEQ(SSL_get_peer_signature_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  62524. ExpectIntEQ(nid, test_get_signature_nid_hash);
  62525. }
  62526. else /* No sigalg info on static ciphersuite */
  62527. return TEST_SUCCESS;
  62528. return EXPECT_RESULT();
  62529. }
  62530. static int test_get_signature_nid_on_hs_server(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  62531. {
  62532. EXPECT_DECLS;
  62533. int nid = 0;
  62534. (void)ctx;
  62535. ExpectIntEQ(SSL_get_signature_type_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  62536. ExpectIntEQ(nid, test_get_signature_nid_sig);
  62537. ExpectIntEQ(SSL_get_signature_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  62538. ExpectIntEQ(nid, test_get_signature_nid_hash);
  62539. return EXPECT_RESULT();
  62540. }
  62541. #endif
  62542. static int test_get_signature_nid(void)
  62543. {
  62544. EXPECT_DECLS;
  62545. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA)
  62546. test_ssl_cbf client_cbf;
  62547. test_ssl_cbf server_cbf;
  62548. size_t i;
  62549. #define TGSN_TLS12_RSA(sigalg, sig_nid, hash_nid) \
  62550. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_2, svrCertFile, svrKeyFile, \
  62551. caCertFile }
  62552. #define TGSN_TLS12_ECDSA(sigalg, sig_nid, hash_nid) \
  62553. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_2, eccCertFile, eccKeyFile, \
  62554. caEccCertFile }
  62555. #define TGSN_TLS13_RSA(sigalg, sig_nid, hash_nid) \
  62556. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, svrCertFile, svrKeyFile, \
  62557. caCertFile }
  62558. #define TGSN_TLS13_ECDSA(sigalg, sig_nid, hash_nid) \
  62559. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, eccCertFile, eccKeyFile, \
  62560. caEccCertFile }
  62561. #define TGSN_TLS13_ED25519(sigalg, sig_nid, hash_nid) \
  62562. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, edCertFile, edKeyFile, \
  62563. caEdCertFile }
  62564. #define TGSN_TLS13_ED448(sigalg, sig_nid, hash_nid) \
  62565. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, ed448CertFile, ed448KeyFile, \
  62566. caEd448CertFile }
  62567. struct {
  62568. const char* siglag;
  62569. int sig_nid;
  62570. int hash_nid;
  62571. int tls_ver;
  62572. const char* server_cert;
  62573. const char* server_key;
  62574. const char* client_ca;
  62575. } params[] = {
  62576. #ifndef NO_RSA
  62577. #ifndef NO_SHA256
  62578. TGSN_TLS12_RSA("RSA+SHA256", NID_rsaEncryption, NID_sha256),
  62579. #ifdef WC_RSA_PSS
  62580. TGSN_TLS12_RSA("RSA-PSS+SHA256", NID_rsassaPss, NID_sha256),
  62581. TGSN_TLS13_RSA("RSA-PSS+SHA256", NID_rsassaPss, NID_sha256),
  62582. #endif
  62583. #endif
  62584. #ifdef WOLFSSL_SHA512
  62585. TGSN_TLS12_RSA("RSA+SHA512", NID_rsaEncryption, NID_sha512),
  62586. #ifdef WC_RSA_PSS
  62587. TGSN_TLS12_RSA("RSA-PSS+SHA512", NID_rsassaPss, NID_sha512),
  62588. TGSN_TLS13_RSA("RSA-PSS+SHA512", NID_rsassaPss, NID_sha512),
  62589. #endif
  62590. #endif
  62591. #endif
  62592. #ifdef HAVE_ECC
  62593. #ifndef NO_SHA256
  62594. TGSN_TLS12_ECDSA("ECDSA+SHA256", NID_X9_62_id_ecPublicKey, NID_sha256),
  62595. TGSN_TLS13_ECDSA("ECDSA+SHA256", NID_X9_62_id_ecPublicKey, NID_sha256),
  62596. #endif
  62597. #endif
  62598. #ifdef HAVE_ED25519
  62599. TGSN_TLS13_ED25519("ED25519", NID_ED25519, NID_sha512),
  62600. #endif
  62601. #ifdef HAVE_ED448
  62602. TGSN_TLS13_ED448("ED448", NID_ED448, NID_sha512),
  62603. #endif
  62604. };
  62605. printf("\n");
  62606. for (i = 0; i < XELEM_CNT(params) && !EXPECT_FAIL(); i++) {
  62607. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  62608. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  62609. printf("Testing %s with %s...", tls_desc[params[i].tls_ver],
  62610. params[i].siglag);
  62611. switch (params[i].tls_ver) {
  62612. #ifndef WOLFSSL_NO_TLS12
  62613. case WOLFSSL_TLSV1_2:
  62614. client_cbf.method = wolfTLSv1_2_client_method;
  62615. server_cbf.method = wolfTLSv1_2_server_method;
  62616. break;
  62617. #endif
  62618. #ifdef WOLFSSL_TLS13
  62619. case WOLFSSL_TLSV1_3:
  62620. client_cbf.method = wolfTLSv1_3_client_method;
  62621. server_cbf.method = wolfTLSv1_3_server_method;
  62622. break;
  62623. #endif
  62624. default:
  62625. printf("skipping\n");
  62626. continue;
  62627. }
  62628. test_get_signature_nid_siglag = params[i].siglag;
  62629. test_get_signature_nid_sig = params[i].sig_nid;
  62630. test_get_signature_nid_hash = params[i].hash_nid;
  62631. client_cbf.ssl_ready = test_get_signature_nid_ssl_ready;
  62632. server_cbf.ssl_ready = test_get_signature_nid_ssl_ready;
  62633. client_cbf.on_handshake = test_get_signature_nid_on_hs_client;
  62634. server_cbf.on_handshake = test_get_signature_nid_on_hs_server;
  62635. server_cbf.certPemFile = params[i].server_cert;
  62636. server_cbf.keyPemFile = params[i].server_key;
  62637. client_cbf.caPemFile = params[i].client_ca;
  62638. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  62639. &server_cbf, NULL), TEST_SUCCESS);
  62640. if (EXPECT_SUCCESS())
  62641. printf("passed\n");
  62642. }
  62643. #endif
  62644. return EXPECT_RESULT();
  62645. }
  62646. #if !defined(NO_CERTS) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  62647. static word32 test_tls_cert_store_unchanged_HashCaTable(Signer** caTable)
  62648. {
  62649. #ifndef NO_MD5
  62650. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  62651. #elif !defined(NO_SHA)
  62652. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  62653. #elif !defined(NO_SHA256)
  62654. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  62655. #else
  62656. #error "We need a digest to hash the Signer object"
  62657. #endif
  62658. byte hashBuf[WC_MAX_DIGEST_SIZE];
  62659. wc_HashAlg hash;
  62660. size_t i;
  62661. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  62662. for (i = 0; i < CA_TABLE_SIZE; i++) {
  62663. Signer* cur;
  62664. for (cur = caTable[i]; cur != NULL; cur = cur->next)
  62665. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)cur,
  62666. sizeof(*cur)), 0);
  62667. }
  62668. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  62669. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  62670. return MakeWordFromHash(hashBuf);
  62671. }
  62672. static word32 test_tls_cert_store_unchanged_before_hashes[2];
  62673. static size_t test_tls_cert_store_unchanged_before_hashes_idx;
  62674. static word32 test_tls_cert_store_unchanged_after_hashes[2];
  62675. static size_t test_tls_cert_store_unchanged_after_hashes_idx;
  62676. static int test_tls_cert_store_unchanged_ctx_ready(WOLFSSL_CTX* ctx)
  62677. {
  62678. EXPECT_DECLS;
  62679. ExpectIntNE(test_tls_cert_store_unchanged_before_hashes
  62680. [test_tls_cert_store_unchanged_before_hashes_idx++] =
  62681. test_tls_cert_store_unchanged_HashCaTable(ctx->cm->caTable), 0);
  62682. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER |
  62683. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  62684. return EXPECT_RESULT();
  62685. }
  62686. static int test_tls_cert_store_unchanged_ctx_cleanup(WOLFSSL_CTX* ctx)
  62687. {
  62688. EXPECT_DECLS;
  62689. ExpectIntEQ(wolfSSL_CTX_UnloadIntermediateCerts(ctx), WOLFSSL_SUCCESS);
  62690. ExpectIntNE(test_tls_cert_store_unchanged_after_hashes
  62691. [test_tls_cert_store_unchanged_after_hashes_idx++] =
  62692. test_tls_cert_store_unchanged_HashCaTable(ctx->cm->caTable), 0);
  62693. return EXPECT_RESULT();
  62694. }
  62695. static int test_tls_cert_store_unchanged_on_hs(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  62696. {
  62697. EXPECT_DECLS;
  62698. WOLFSSL_CERT_MANAGER* cm;
  62699. (void)ssl;
  62700. /* WARNING: this approach bypasses the reference counter check in
  62701. * wolfSSL_CTX_UnloadIntermediateCerts. It is not recommended as it may
  62702. * cause unexpected behaviour when other active connections try accessing
  62703. * the caTable. */
  62704. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(*ctx));
  62705. ExpectIntEQ(wolfSSL_CertManagerUnloadIntermediateCerts(cm),
  62706. WOLFSSL_SUCCESS);
  62707. ExpectIntNE(test_tls_cert_store_unchanged_after_hashes
  62708. [test_tls_cert_store_unchanged_after_hashes_idx++] =
  62709. test_tls_cert_store_unchanged_HashCaTable((*ctx)->cm->caTable), 0);
  62710. return EXPECT_RESULT();
  62711. }
  62712. static int test_tls_cert_store_unchanged_ssl_ready(WOLFSSL* ssl)
  62713. {
  62714. EXPECT_DECLS;
  62715. WOLFSSL_CTX* ctx;
  62716. ExpectNotNull(ctx = wolfSSL_get_SSL_CTX(ssl));
  62717. return EXPECT_RESULT();
  62718. }
  62719. #endif
  62720. static int test_tls_cert_store_unchanged(void)
  62721. {
  62722. EXPECT_DECLS;
  62723. #if !defined(NO_CERTS) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  62724. test_ssl_cbf client_cbf;
  62725. test_ssl_cbf server_cbf;
  62726. int i;
  62727. for (i = 0; i < 2; i++) {
  62728. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  62729. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  62730. test_tls_cert_store_unchanged_before_hashes_idx = 0;
  62731. XMEMSET(test_tls_cert_store_unchanged_before_hashes, 0,
  62732. sizeof(test_tls_cert_store_unchanged_before_hashes));
  62733. test_tls_cert_store_unchanged_after_hashes_idx = 0;
  62734. XMEMSET(test_tls_cert_store_unchanged_after_hashes, 0,
  62735. sizeof(test_tls_cert_store_unchanged_after_hashes));
  62736. client_cbf.ctx_ready = test_tls_cert_store_unchanged_ctx_ready;
  62737. server_cbf.ctx_ready = test_tls_cert_store_unchanged_ctx_ready;
  62738. client_cbf.ssl_ready = test_tls_cert_store_unchanged_ssl_ready;
  62739. server_cbf.ssl_ready = test_tls_cert_store_unchanged_ssl_ready;
  62740. switch (i) {
  62741. case 0:
  62742. client_cbf.on_ctx_cleanup =
  62743. test_tls_cert_store_unchanged_ctx_cleanup;
  62744. server_cbf.on_ctx_cleanup =
  62745. test_tls_cert_store_unchanged_ctx_cleanup;
  62746. break;
  62747. case 1:
  62748. client_cbf.on_handshake = test_tls_cert_store_unchanged_on_hs;
  62749. server_cbf.on_handshake = test_tls_cert_store_unchanged_on_hs;
  62750. break;
  62751. default:
  62752. Fail(("Should not enter here"), ("Entered here"));
  62753. }
  62754. client_cbf.certPemFile = "certs/intermediate/client-chain.pem";
  62755. server_cbf.certPemFile = "certs/intermediate/server-chain.pem";
  62756. server_cbf.caPemFile = caCertFile;
  62757. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  62758. &server_cbf, NULL), TEST_SUCCESS);
  62759. ExpectBufEQ(test_tls_cert_store_unchanged_before_hashes,
  62760. test_tls_cert_store_unchanged_after_hashes,
  62761. sizeof(test_tls_cert_store_unchanged_after_hashes));
  62762. }
  62763. #endif
  62764. return EXPECT_RESULT();
  62765. }
  62766. /*----------------------------------------------------------------------------*
  62767. | Main
  62768. *----------------------------------------------------------------------------*/
  62769. typedef int (*TEST_FUNC)(void);
  62770. typedef struct {
  62771. const char *name;
  62772. TEST_FUNC func;
  62773. byte run:1;
  62774. byte fail:1;
  62775. } TEST_CASE;
  62776. #define TEST_DECL(func) { #func, func, 0, 0 }
  62777. int testAll = 1;
  62778. TEST_CASE testCases[] = {
  62779. TEST_DECL(test_fileAccess),
  62780. /*********************************
  62781. * wolfcrypt
  62782. *********************************/
  62783. TEST_DECL(test_ForceZero),
  62784. TEST_DECL(test_wolfCrypt_Init),
  62785. /* Locking with Compat Mutex */
  62786. TEST_DECL(test_wc_SetMutexCb),
  62787. TEST_DECL(test_wc_LockMutex_ex),
  62788. /* Digests */
  62789. TEST_DECL(test_wc_InitMd5),
  62790. TEST_DECL(test_wc_Md5Update),
  62791. TEST_DECL(test_wc_Md5Final),
  62792. TEST_DECL(test_wc_InitSha),
  62793. TEST_DECL(test_wc_ShaUpdate),
  62794. TEST_DECL(test_wc_ShaFinal),
  62795. TEST_DECL(test_wc_InitSha256),
  62796. TEST_DECL(test_wc_Sha256Update),
  62797. TEST_DECL(test_wc_Sha256Final),
  62798. TEST_DECL(test_wc_Sha256FinalRaw),
  62799. TEST_DECL(test_wc_Sha256GetFlags),
  62800. TEST_DECL(test_wc_Sha256Free),
  62801. TEST_DECL(test_wc_Sha256GetHash),
  62802. TEST_DECL(test_wc_Sha256Copy),
  62803. TEST_DECL(test_wc_InitSha224),
  62804. TEST_DECL(test_wc_Sha224Update),
  62805. TEST_DECL(test_wc_Sha224Final),
  62806. TEST_DECL(test_wc_Sha224SetFlags),
  62807. TEST_DECL(test_wc_Sha224GetFlags),
  62808. TEST_DECL(test_wc_Sha224Free),
  62809. TEST_DECL(test_wc_Sha224GetHash),
  62810. TEST_DECL(test_wc_Sha224Copy),
  62811. TEST_DECL(test_wc_InitSha512),
  62812. TEST_DECL(test_wc_Sha512Update),
  62813. TEST_DECL(test_wc_Sha512Final),
  62814. TEST_DECL(test_wc_Sha512GetFlags),
  62815. TEST_DECL(test_wc_Sha512FinalRaw),
  62816. TEST_DECL(test_wc_Sha512Free),
  62817. TEST_DECL(test_wc_Sha512GetHash),
  62818. TEST_DECL(test_wc_Sha512Copy),
  62819. TEST_DECL(test_wc_InitSha512_224),
  62820. TEST_DECL(test_wc_Sha512_224Update),
  62821. TEST_DECL(test_wc_Sha512_224Final),
  62822. TEST_DECL(test_wc_Sha512_224GetFlags),
  62823. TEST_DECL(test_wc_Sha512_224FinalRaw),
  62824. TEST_DECL(test_wc_Sha512_224Free),
  62825. TEST_DECL(test_wc_Sha512_224GetHash),
  62826. TEST_DECL(test_wc_Sha512_224Copy),
  62827. TEST_DECL(test_wc_InitSha512_256),
  62828. TEST_DECL(test_wc_Sha512_256Update),
  62829. TEST_DECL(test_wc_Sha512_256Final),
  62830. TEST_DECL(test_wc_Sha512_256GetFlags),
  62831. TEST_DECL(test_wc_Sha512_256FinalRaw),
  62832. TEST_DECL(test_wc_Sha512_256Free),
  62833. TEST_DECL(test_wc_Sha512_256GetHash),
  62834. TEST_DECL(test_wc_Sha512_256Copy),
  62835. TEST_DECL(test_wc_InitSha384),
  62836. TEST_DECL(test_wc_Sha384Update),
  62837. TEST_DECL(test_wc_Sha384Final),
  62838. TEST_DECL(test_wc_Sha384GetFlags),
  62839. TEST_DECL(test_wc_Sha384FinalRaw),
  62840. TEST_DECL(test_wc_Sha384Free),
  62841. TEST_DECL(test_wc_Sha384GetHash),
  62842. TEST_DECL(test_wc_Sha384Copy),
  62843. TEST_DECL(test_wc_InitBlake2b),
  62844. TEST_DECL(test_wc_InitBlake2b_WithKey),
  62845. TEST_DECL(test_wc_InitBlake2s_WithKey),
  62846. TEST_DECL(test_wc_InitRipeMd),
  62847. TEST_DECL(test_wc_RipeMdUpdate),
  62848. TEST_DECL(test_wc_RipeMdFinal),
  62849. TEST_DECL(test_wc_InitSha3),
  62850. TEST_DECL(testing_wc_Sha3_Update),
  62851. TEST_DECL(test_wc_Sha3_224_Final),
  62852. TEST_DECL(test_wc_Sha3_256_Final),
  62853. TEST_DECL(test_wc_Sha3_384_Final),
  62854. TEST_DECL(test_wc_Sha3_512_Final),
  62855. TEST_DECL(test_wc_Sha3_224_Copy),
  62856. TEST_DECL(test_wc_Sha3_256_Copy),
  62857. TEST_DECL(test_wc_Sha3_384_Copy),
  62858. TEST_DECL(test_wc_Sha3_512_Copy),
  62859. TEST_DECL(test_wc_Sha3_GetFlags),
  62860. TEST_DECL(test_wc_InitShake256),
  62861. TEST_DECL(testing_wc_Shake256_Update),
  62862. TEST_DECL(test_wc_Shake256_Final),
  62863. TEST_DECL(test_wc_Shake256_Copy),
  62864. TEST_DECL(test_wc_Shake256Hash),
  62865. /* SM3 Digest */
  62866. TEST_DECL(test_wc_InitSm3Free),
  62867. TEST_DECL(test_wc_Sm3UpdateFinal),
  62868. TEST_DECL(test_wc_Sm3GetHash),
  62869. TEST_DECL(test_wc_Sm3Copy),
  62870. TEST_DECL(test_wc_Sm3FinalRaw),
  62871. TEST_DECL(test_wc_Sm3GetSetFlags),
  62872. TEST_DECL(test_wc_Sm3Hash),
  62873. TEST_DECL(test_wc_HashInit),
  62874. TEST_DECL(test_wc_HashSetFlags),
  62875. TEST_DECL(test_wc_HashGetFlags),
  62876. /* HMAC */
  62877. TEST_DECL(test_wc_Md5HmacSetKey),
  62878. TEST_DECL(test_wc_Md5HmacUpdate),
  62879. TEST_DECL(test_wc_Md5HmacFinal),
  62880. TEST_DECL(test_wc_ShaHmacSetKey),
  62881. TEST_DECL(test_wc_ShaHmacUpdate),
  62882. TEST_DECL(test_wc_ShaHmacFinal),
  62883. TEST_DECL(test_wc_Sha224HmacSetKey),
  62884. TEST_DECL(test_wc_Sha224HmacUpdate),
  62885. TEST_DECL(test_wc_Sha224HmacFinal),
  62886. TEST_DECL(test_wc_Sha256HmacSetKey),
  62887. TEST_DECL(test_wc_Sha256HmacUpdate),
  62888. TEST_DECL(test_wc_Sha256HmacFinal),
  62889. TEST_DECL(test_wc_Sha384HmacSetKey),
  62890. TEST_DECL(test_wc_Sha384HmacUpdate),
  62891. TEST_DECL(test_wc_Sha384HmacFinal),
  62892. /* CMAC */
  62893. TEST_DECL(test_wc_InitCmac),
  62894. TEST_DECL(test_wc_CmacUpdate),
  62895. TEST_DECL(test_wc_CmacFinal),
  62896. TEST_DECL(test_wc_AesCmacGenerate),
  62897. /* Cipher */
  62898. TEST_DECL(test_wc_AesGcmStream),
  62899. TEST_DECL(test_wc_Des3_SetIV),
  62900. TEST_DECL(test_wc_Des3_SetKey),
  62901. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  62902. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  62903. TEST_DECL(test_wc_Des3_EcbEncrypt),
  62904. TEST_DECL(test_wc_Chacha_SetKey),
  62905. TEST_DECL(test_wc_Chacha_Process),
  62906. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  62907. TEST_DECL(test_wc_Poly1305SetKey),
  62908. TEST_DECL(test_wc_CamelliaSetKey),
  62909. TEST_DECL(test_wc_CamelliaSetIV),
  62910. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  62911. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  62912. TEST_DECL(test_wc_Arc4SetKey),
  62913. TEST_DECL(test_wc_Arc4Process),
  62914. TEST_DECL(test_wc_Rc2SetKey),
  62915. TEST_DECL(test_wc_Rc2SetIV),
  62916. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  62917. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  62918. /* AES cipher and GMAC. */
  62919. TEST_DECL(test_wc_AesSetKey),
  62920. TEST_DECL(test_wc_AesSetIV),
  62921. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  62922. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  62923. TEST_DECL(test_wc_AesGcmSetKey),
  62924. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  62925. TEST_DECL(test_wc_AesGcmMixedEncDecLongIV),
  62926. TEST_DECL(test_wc_GmacSetKey),
  62927. TEST_DECL(test_wc_GmacUpdate),
  62928. TEST_DECL(test_wc_AesCcmSetKey),
  62929. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  62930. #if defined(WOLFSSL_AES_EAX) && \
  62931. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  62932. TEST_DECL(test_wc_AesEaxVectors),
  62933. TEST_DECL(test_wc_AesEaxEncryptAuth),
  62934. TEST_DECL(test_wc_AesEaxDecryptAuth),
  62935. #endif /* WOLFSSL_AES_EAX */
  62936. /* SM4 cipher */
  62937. TEST_DECL(test_wc_Sm4),
  62938. TEST_DECL(test_wc_Sm4Ecb),
  62939. TEST_DECL(test_wc_Sm4Cbc),
  62940. TEST_DECL(test_wc_Sm4Ctr),
  62941. TEST_DECL(test_wc_Sm4Gcm),
  62942. TEST_DECL(test_wc_Sm4Ccm),
  62943. /* RNG tests */
  62944. #ifdef HAVE_HASHDRBG
  62945. #ifdef TEST_RESEED_INTERVAL
  62946. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  62947. #endif
  62948. TEST_DECL(test_wc_RNG_GenerateBlock),
  62949. #endif
  62950. TEST_DECL(test_get_rand_digit),
  62951. TEST_DECL(test_wc_InitRngNonce),
  62952. TEST_DECL(test_wc_InitRngNonce_ex),
  62953. /* MP API tests */
  62954. TEST_DECL(test_get_digit_count),
  62955. TEST_DECL(test_mp_cond_copy),
  62956. TEST_DECL(test_mp_rand),
  62957. TEST_DECL(test_get_digit),
  62958. TEST_DECL(test_wc_export_int),
  62959. /* RSA */
  62960. TEST_DECL(test_wc_InitRsaKey),
  62961. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  62962. TEST_DECL(test_wc_RsaPublicKeyDecode),
  62963. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  62964. TEST_DECL(test_wc_MakeRsaKey),
  62965. TEST_DECL(test_wc_CheckProbablePrime),
  62966. TEST_DECL(test_wc_RsaPSS_Verify),
  62967. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  62968. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  62969. TEST_DECL(test_wc_RsaKeyToDer),
  62970. TEST_DECL(test_wc_RsaKeyToPublicDer),
  62971. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  62972. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  62973. TEST_DECL(test_wc_RsaEncryptSize),
  62974. TEST_DECL(test_wc_RsaSSL_SignVerify),
  62975. TEST_DECL(test_wc_RsaFlattenPublicKey),
  62976. TEST_DECL(test_RsaDecryptBoundsCheck),
  62977. /* DSA */
  62978. TEST_DECL(test_wc_InitDsaKey),
  62979. TEST_DECL(test_wc_DsaSignVerify),
  62980. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  62981. TEST_DECL(test_wc_MakeDsaKey),
  62982. TEST_DECL(test_wc_DsaKeyToDer),
  62983. TEST_DECL(test_wc_DsaKeyToPublicDer),
  62984. TEST_DECL(test_wc_DsaImportParamsRaw),
  62985. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  62986. TEST_DECL(test_wc_DsaExportParamsRaw),
  62987. TEST_DECL(test_wc_DsaExportKeyRaw),
  62988. /* DH */
  62989. TEST_DECL(test_wc_DhPublicKeyDecode),
  62990. /* wolfCrypt ECC tests */
  62991. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  62992. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  62993. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  62994. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  62995. !defined(HAVE_SELFTEST) && \
  62996. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  62997. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  62998. #endif
  62999. TEST_DECL(test_wc_ecc_make_key),
  63000. TEST_DECL(test_wc_ecc_init),
  63001. TEST_DECL(test_wc_ecc_check_key),
  63002. TEST_DECL(test_wc_ecc_get_generator),
  63003. TEST_DECL(test_wc_ecc_size),
  63004. TEST_DECL(test_wc_ecc_params),
  63005. TEST_DECL(test_wc_ecc_signVerify_hash),
  63006. TEST_DECL(test_wc_ecc_shared_secret),
  63007. TEST_DECL(test_wc_ecc_export_x963),
  63008. TEST_DECL(test_wc_ecc_export_x963_ex),
  63009. TEST_DECL(test_wc_ecc_import_x963),
  63010. TEST_DECL(test_wc_ecc_import_private_key),
  63011. TEST_DECL(test_wc_ecc_export_private_only),
  63012. TEST_DECL(test_wc_ecc_rs_to_sig),
  63013. TEST_DECL(test_wc_ecc_import_raw),
  63014. TEST_DECL(test_wc_ecc_import_unsigned),
  63015. TEST_DECL(test_wc_ecc_sig_size),
  63016. TEST_DECL(test_wc_ecc_ctx_new),
  63017. TEST_DECL(test_wc_ecc_ctx_reset),
  63018. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  63019. TEST_DECL(test_wc_ecc_ctx_set_info),
  63020. TEST_DECL(test_wc_ecc_encryptDecrypt),
  63021. TEST_DECL(test_wc_ecc_del_point),
  63022. TEST_DECL(test_wc_ecc_pointFns),
  63023. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  63024. TEST_DECL(test_wc_ecc_verify_hash_ex),
  63025. TEST_DECL(test_wc_ecc_mulmod),
  63026. TEST_DECL(test_wc_ecc_is_valid_idx),
  63027. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  63028. TEST_DECL(test_wc_ecc_sig_size_calc),
  63029. TEST_DECL(test_wc_EccPrivateKeyToDer),
  63030. /* SM2 elliptic curve */
  63031. TEST_DECL(test_wc_ecc_sm2_make_key),
  63032. TEST_DECL(test_wc_ecc_sm2_shared_secret),
  63033. TEST_DECL(test_wc_ecc_sm2_create_digest),
  63034. TEST_DECL(test_wc_ecc_sm2_verify_hash_ex),
  63035. TEST_DECL(test_wc_ecc_sm2_verify_hash),
  63036. TEST_DECL(test_wc_ecc_sm2_sign_hash_ex),
  63037. TEST_DECL(test_wc_ecc_sm2_sign_hash),
  63038. /* Curve25519 */
  63039. TEST_DECL(test_wc_curve25519_init),
  63040. TEST_DECL(test_wc_curve25519_size),
  63041. TEST_DECL(test_wc_curve25519_export_key_raw),
  63042. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  63043. TEST_DECL(test_wc_curve25519_make_key),
  63044. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  63045. TEST_DECL(test_wc_curve25519_make_pub),
  63046. TEST_DECL(test_wc_curve25519_export_public_ex),
  63047. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  63048. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  63049. TEST_DECL(test_wc_curve25519_import_private),
  63050. /* ED25519 */
  63051. TEST_DECL(test_wc_ed25519_make_key),
  63052. TEST_DECL(test_wc_ed25519_init),
  63053. TEST_DECL(test_wc_ed25519_sign_msg),
  63054. TEST_DECL(test_wc_ed25519_import_public),
  63055. TEST_DECL(test_wc_ed25519_import_private_key),
  63056. TEST_DECL(test_wc_ed25519_export),
  63057. TEST_DECL(test_wc_ed25519_size),
  63058. TEST_DECL(test_wc_ed25519_exportKey),
  63059. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  63060. TEST_DECL(test_wc_Ed25519KeyToDer),
  63061. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  63062. /* Curve448 */
  63063. TEST_DECL(test_wc_curve448_make_key),
  63064. TEST_DECL(test_wc_curve448_shared_secret_ex),
  63065. TEST_DECL(test_wc_curve448_export_public_ex),
  63066. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  63067. TEST_DECL(test_wc_curve448_export_key_raw),
  63068. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  63069. TEST_DECL(test_wc_curve448_import_private),
  63070. TEST_DECL(test_wc_curve448_init),
  63071. TEST_DECL(test_wc_curve448_size),
  63072. /* Ed448 */
  63073. TEST_DECL(test_wc_ed448_make_key),
  63074. TEST_DECL(test_wc_ed448_init),
  63075. TEST_DECL(test_wc_ed448_sign_msg),
  63076. TEST_DECL(test_wc_ed448_import_public),
  63077. TEST_DECL(test_wc_ed448_import_private_key),
  63078. TEST_DECL(test_wc_ed448_export),
  63079. TEST_DECL(test_wc_ed448_size),
  63080. TEST_DECL(test_wc_ed448_exportKey),
  63081. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  63082. TEST_DECL(test_wc_Ed448KeyToDer),
  63083. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  63084. /* Signature API */
  63085. TEST_DECL(test_wc_SignatureGetSize_ecc),
  63086. TEST_DECL(test_wc_SignatureGetSize_rsa),
  63087. /* PEM and DER APIs. */
  63088. TEST_DECL(test_wc_PemToDer),
  63089. TEST_DECL(test_wc_AllocDer),
  63090. TEST_DECL(test_wc_CertPemToDer),
  63091. TEST_DECL(test_wc_KeyPemToDer),
  63092. TEST_DECL(test_wc_PubKeyPemToDer),
  63093. TEST_DECL(test_wc_PemPubKeyToDer),
  63094. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  63095. TEST_DECL(test_wc_CheckCertSigPubKey),
  63096. /* wolfCrypt ASN tests */
  63097. TEST_DECL(test_ToTraditional),
  63098. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  63099. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  63100. /* Certificate */
  63101. TEST_DECL(test_wc_SetSubjectRaw),
  63102. TEST_DECL(test_wc_GetSubjectRaw),
  63103. TEST_DECL(test_wc_SetIssuerRaw),
  63104. TEST_DECL(test_wc_SetIssueBuffer),
  63105. TEST_DECL(test_wc_SetSubjectKeyId),
  63106. TEST_DECL(test_wc_SetSubject),
  63107. TEST_DECL(test_CheckCertSignature),
  63108. TEST_DECL(test_wc_ParseCert),
  63109. TEST_DECL(test_wc_ParseCert_Error),
  63110. TEST_DECL(test_MakeCertWithPathLen),
  63111. TEST_DECL(test_MakeCertWithCaFalse),
  63112. TEST_DECL(test_wc_SetKeyUsage),
  63113. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  63114. TEST_DECL(test_wc_SetSubjectBuffer),
  63115. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  63116. /* wolfcrypt PKCS#7 */
  63117. TEST_DECL(test_wc_PKCS7_New),
  63118. TEST_DECL(test_wc_PKCS7_Init),
  63119. TEST_DECL(test_wc_PKCS7_InitWithCert),
  63120. TEST_DECL(test_wc_PKCS7_EncodeData),
  63121. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  63122. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  63123. TEST_DECL(test_wc_PKCS7_VerifySignedData_RSA),
  63124. TEST_DECL(test_wc_PKCS7_VerifySignedData_ECC),
  63125. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  63126. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  63127. TEST_DECL(test_wc_PKCS7_Degenerate),
  63128. TEST_DECL(test_wc_PKCS7_BER),
  63129. TEST_DECL(test_wc_PKCS7_signed_enveloped),
  63130. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  63131. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  63132. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  63133. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  63134. /* wolfCrypt PKCS#12 */
  63135. TEST_DECL(test_wc_i2d_PKCS12),
  63136. /*
  63137. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  63138. * avoid memory leaks.
  63139. */
  63140. TEST_DECL(test_wolfCrypt_Cleanup),
  63141. TEST_DECL(test_wolfSSL_Init),
  63142. TEST_DECL(test_dual_alg_support),
  63143. /*********************************
  63144. * OpenSSL compatibility API tests
  63145. *********************************/
  63146. /* If at some point a stub get implemented this test should fail indicating
  63147. * a need to implement a new test case
  63148. */
  63149. TEST_DECL(test_stubs_are_stubs),
  63150. /* ASN.1 compatibility API tests */
  63151. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  63152. TEST_DECL(test_wolfSSL_ASN1_INTEGER),
  63153. TEST_DECL(test_wolfSSL_ASN1_INTEGER_cmp),
  63154. TEST_DECL(test_wolfSSL_ASN1_INTEGER_BN),
  63155. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  63156. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  63157. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  63158. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  63159. TEST_DECL(test_wolfSSL_ASN1_OBJECT),
  63160. TEST_DECL(test_wolfSSL_ASN1_get_object),
  63161. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  63162. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  63163. TEST_DECL(test_wolfSSL_sk_ASN1_OBJECT),
  63164. TEST_DECL(test_wolfSSL_ASN1_STRING),
  63165. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  63166. TEST_DECL(test_wolfSSL_i2s_ASN1_STRING),
  63167. TEST_DECL(test_wolfSSL_ASN1_STRING_canon),
  63168. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  63169. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  63170. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  63171. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  63172. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_print),
  63173. TEST_DECL(test_wolfSSL_ASN1_TIME),
  63174. TEST_DECL(test_wolfSSL_ASN1_TIME_to_string),
  63175. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  63176. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  63177. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  63178. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  63179. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  63180. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  63181. TEST_DECL(test_wolfSSL_ASN1_TYPE),
  63182. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  63183. TEST_DECL(test_wolfSSL_lhash),
  63184. TEST_DECL(test_wolfSSL_certs),
  63185. TEST_DECL(test_wolfSSL_private_keys),
  63186. TEST_DECL(test_wolfSSL_PEM_def_callback),
  63187. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  63188. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  63189. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  63190. TEST_DECL(test_wolfSSL_PEM_PrivateKey_rsa),
  63191. TEST_DECL(test_wolfSSL_PEM_PrivateKey_ecc),
  63192. TEST_DECL(test_wolfSSL_PEM_PrivateKey_dsa),
  63193. TEST_DECL(test_wolfSSL_PEM_PrivateKey_dh),
  63194. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  63195. TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
  63196. TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
  63197. #ifndef NO_BIO
  63198. TEST_DECL(test_wolfSSL_BIO),
  63199. TEST_DECL(test_wolfSSL_BIO_BIO_ring_read),
  63200. TEST_DECL(test_wolfSSL_PEM_read_bio),
  63201. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  63202. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  63203. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  63204. TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
  63205. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  63206. #endif
  63207. /* EVP API testing */
  63208. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  63209. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  63210. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  63211. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  63212. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  63213. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  63214. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  63215. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  63216. TEST_DECL(test_wolfSSL_EVP_shake128),
  63217. TEST_DECL(test_wolfSSL_EVP_shake256),
  63218. TEST_DECL(test_wolfSSL_EVP_sm3),
  63219. TEST_DECL(test_EVP_blake2),
  63220. #ifdef OPENSSL_ALL
  63221. TEST_DECL(test_wolfSSL_EVP_md4),
  63222. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  63223. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  63224. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  63225. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  63226. #endif
  63227. TEST_DECL(test_EVP_MD_do_all),
  63228. TEST_DECL(test_wolfSSL_EVP_MD_size),
  63229. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  63230. TEST_DECL(test_wolfSSL_EVP_Digest),
  63231. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  63232. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  63233. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  63234. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  63235. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  63236. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  63237. TEST_DECL(test_wolfssl_EVP_aes_gcm_zeroLen),
  63238. TEST_DECL(test_wolfssl_EVP_aes_ccm),
  63239. TEST_DECL(test_wolfssl_EVP_aes_ccm_zeroLen),
  63240. TEST_DECL(test_wolfssl_EVP_chacha20),
  63241. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  63242. TEST_DECL(test_wolfssl_EVP_sm4_ecb),
  63243. TEST_DECL(test_wolfssl_EVP_sm4_cbc),
  63244. TEST_DECL(test_wolfssl_EVP_sm4_ctr),
  63245. TEST_DECL(test_wolfssl_EVP_sm4_gcm_zeroLen),
  63246. TEST_DECL(test_wolfssl_EVP_sm4_gcm),
  63247. TEST_DECL(test_wolfssl_EVP_sm4_ccm_zeroLen),
  63248. TEST_DECL(test_wolfssl_EVP_sm4_ccm),
  63249. #ifdef OPENSSL_ALL
  63250. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  63251. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  63252. TEST_DECL(test_wolfSSL_EVP_aes_256_ccm),
  63253. TEST_DECL(test_wolfSSL_EVP_aes_192_ccm),
  63254. TEST_DECL(test_wolfSSL_EVP_aes_128_ccm),
  63255. TEST_DECL(test_wolfSSL_EVP_rc4),
  63256. TEST_DECL(test_wolfSSL_EVP_enc_null),
  63257. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  63258. TEST_DECL(test_wolfSSL_EVP_mdc2),
  63259. TEST_DECL(test_evp_cipher_aes_gcm),
  63260. #endif
  63261. TEST_DECL(test_wolfssl_EVP_aria_gcm),
  63262. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  63263. #ifdef OPENSSL_EXTRA
  63264. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  63265. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  63266. #endif
  63267. #ifdef OPENSSL_ALL
  63268. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  63269. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  63270. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  63271. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  63272. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  63273. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  63274. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  63275. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  63276. #endif
  63277. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  63278. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  63279. TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
  63280. TEST_DECL(test_wolfSSL_EVP_PKEY_up_ref),
  63281. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  63282. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  63283. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  63284. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey_ecc),
  63285. #ifndef NO_BIO
  63286. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  63287. #endif
  63288. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  63289. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  63290. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  63291. #ifndef NO_BIO
  63292. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  63293. #endif /* !NO_BIO */
  63294. #endif
  63295. #ifdef OPENSSL_ALL
  63296. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  63297. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  63298. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  63299. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  63300. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  63301. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  63302. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  63303. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  63304. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  63305. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  63306. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  63307. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  63308. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  63309. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  63310. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  63311. #endif
  63312. TEST_DECL(test_EVP_PKEY_rsa),
  63313. TEST_DECL(test_EVP_PKEY_ec),
  63314. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  63315. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_rsa),
  63316. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_dsa),
  63317. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_ec),
  63318. TEST_DECL(test_EVP_PKEY_cmp),
  63319. #ifdef OPENSSL_ALL
  63320. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  63321. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  63322. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  63323. #endif
  63324. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  63325. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  63326. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  63327. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  63328. #endif
  63329. #ifndef NO_BIO
  63330. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  63331. TEST_DECL(test_wolfSSL_GetLoggingCb),
  63332. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  63333. TEST_DECL(test_wc_ERR_remove_state),
  63334. TEST_DECL(test_wc_ERR_print_errors_fp),
  63335. #endif
  63336. TEST_DECL(test_wolfSSL_configure_args),
  63337. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  63338. TEST_DECL(test_wolfSSL_set1_curves_list),
  63339. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  63340. TEST_DECL(test_wolfSSL_OtherName),
  63341. TEST_DECL(test_wolfSSL_FPKI),
  63342. TEST_DECL(test_wolfSSL_URI),
  63343. TEST_DECL(test_wolfSSL_TBS),
  63344. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  63345. TEST_DECL(test_X509_STORE_untrusted),
  63346. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  63347. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  63348. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  63349. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  63350. TEST_DECL(test_wolfSSL_X509_Name_canon),
  63351. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  63352. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  63353. TEST_DECL(test_wolfSSL_X509_NID),
  63354. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  63355. TEST_DECL(test_wolfSSL_get0_param),
  63356. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  63357. TEST_DECL(test_wolfSSL_set1_host),
  63358. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  63359. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  63360. TEST_DECL(test_wolfSSL_X509_STORE),
  63361. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  63362. TEST_DECL(test_X509_STORE_get0_objects),
  63363. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  63364. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  63365. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  63366. TEST_DECL(test_wolfSSL_X509_cmp_time),
  63367. TEST_DECL(test_wolfSSL_X509_time_adj),
  63368. /* X509 tests */
  63369. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  63370. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  63371. TEST_DECL(test_wolfSSL_X509_check_host),
  63372. TEST_DECL(test_wolfSSL_X509_check_email),
  63373. TEST_DECL(test_wolfSSL_X509_check_private_key),
  63374. TEST_DECL(test_wolfSSL_X509),
  63375. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  63376. TEST_DECL(test_wolfSSL_X509_sign),
  63377. TEST_DECL(test_wolfSSL_X509_sign2),
  63378. TEST_DECL(test_wolfSSL_X509_verify),
  63379. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  63380. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  63381. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  63382. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  63383. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  63384. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  63385. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  63386. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  63387. TEST_DECL(test_wolfSSL_X509_NAME),
  63388. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  63389. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  63390. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  63391. TEST_DECL(test_wolfSSL_X509_set_name),
  63392. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  63393. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  63394. TEST_DECL(test_wolfSSL_X509_set_version),
  63395. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  63396. TEST_DECL(test_wolfSSL_X509_CRL),
  63397. TEST_DECL(test_wolfSSL_i2d_X509),
  63398. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  63399. TEST_DECL(test_wolfSSL_PEM_read_X509),
  63400. TEST_DECL(test_wolfSSL_X509_check_ca),
  63401. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  63402. TEST_DECL(test_wolfSSL_make_cert),
  63403. #ifndef NO_BIO
  63404. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  63405. TEST_DECL(test_wolfSSL_X509_INFO),
  63406. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  63407. #endif
  63408. #ifdef OPENSSL_ALL
  63409. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  63410. #endif
  63411. TEST_DECL(test_wolfSSL_X509_CA_num),
  63412. TEST_DECL(test_wolfSSL_X509_get_version),
  63413. #ifndef NO_BIO
  63414. TEST_DECL(test_wolfSSL_X509_print),
  63415. TEST_DECL(test_wolfSSL_X509_CRL_print),
  63416. #endif
  63417. TEST_DECL(test_X509_get_signature_nid),
  63418. /* X509 extension testing. */
  63419. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  63420. TEST_DECL(test_wolfSSL_X509_get_ext),
  63421. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  63422. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  63423. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  63424. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  63425. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  63426. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  63427. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  63428. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  63429. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  63430. TEST_DECL(test_wolfSSL_X509V3_EXT),
  63431. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  63432. TEST_DECL(test_wolfSSL_X509_cmp),
  63433. TEST_DECL(test_GENERAL_NAME_set0_othername),
  63434. TEST_DECL(test_othername_and_SID_ext),
  63435. TEST_DECL(test_wolfSSL_dup_CA_list),
  63436. /* OpenSSL sk_X509 API test */
  63437. TEST_DECL(test_sk_X509),
  63438. /* OpenSSL sk_X509_CRL API test */
  63439. TEST_DECL(test_sk_X509_CRL),
  63440. /* OpenSSL X509 REQ API test */
  63441. TEST_DECL(test_X509_REQ),
  63442. /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
  63443. TEST_DECL(test_X509_STORE_No_SSL_CTX),
  63444. TEST_DECL(test_X509_LOOKUP_add_dir),
  63445. /* RAND compatibility API */
  63446. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  63447. TEST_DECL(test_wolfSSL_RAND_bytes),
  63448. TEST_DECL(test_wolfSSL_RAND),
  63449. /* BN compatibility API */
  63450. TEST_DECL(test_wolfSSL_BN_CTX),
  63451. TEST_DECL(test_wolfSSL_BN),
  63452. TEST_DECL(test_wolfSSL_BN_init),
  63453. TEST_DECL(test_wolfSSL_BN_enc_dec),
  63454. TEST_DECL(test_wolfSSL_BN_word),
  63455. TEST_DECL(test_wolfSSL_BN_bits),
  63456. TEST_DECL(test_wolfSSL_BN_shift),
  63457. TEST_DECL(test_wolfSSL_BN_math),
  63458. TEST_DECL(test_wolfSSL_BN_math_mod),
  63459. TEST_DECL(test_wolfSSL_BN_math_other),
  63460. TEST_DECL(test_wolfSSL_BN_rand),
  63461. TEST_DECL(test_wolfSSL_BN_prime),
  63462. /* OpenSSL PKCS5 API test */
  63463. TEST_DECL(test_wolfSSL_PKCS5),
  63464. /* OpenSSL PKCS8 API test */
  63465. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  63466. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  63467. /* OpenSSL PKCS7 API test */
  63468. TEST_DECL(test_wolfssl_PKCS7),
  63469. TEST_DECL(test_wolfSSL_PKCS7_certs),
  63470. TEST_DECL(test_wolfSSL_PKCS7_sign),
  63471. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  63472. #ifndef NO_BIO
  63473. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  63474. #ifdef HAVE_SMIME
  63475. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  63476. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  63477. #endif /* HAVE_SMIME */
  63478. #endif /* !NO_BIO */
  63479. /* OpenSSL PKCS12 API test */
  63480. TEST_DECL(test_wolfSSL_PKCS12),
  63481. /* Can't memory test as callbacks use Assert. */
  63482. TEST_DECL(test_error_queue_per_thread),
  63483. TEST_DECL(test_wolfSSL_ERR_put_error),
  63484. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  63485. #ifndef NO_BIO
  63486. TEST_DECL(test_wolfSSL_ERR_print_errors),
  63487. #endif
  63488. TEST_DECL(test_OBJ_NAME_do_all),
  63489. TEST_DECL(test_wolfSSL_OBJ),
  63490. TEST_DECL(test_wolfSSL_OBJ_cmp),
  63491. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  63492. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  63493. #ifdef OPENSSL_ALL
  63494. TEST_DECL(test_wolfSSL_OBJ_ln),
  63495. TEST_DECL(test_wolfSSL_OBJ_sn),
  63496. #endif
  63497. #ifndef NO_BIO
  63498. TEST_DECL(test_wolfSSL_BIO_gets),
  63499. TEST_DECL(test_wolfSSL_BIO_puts),
  63500. TEST_DECL(test_wolfSSL_BIO_dump),
  63501. /* Can't memory test as server hangs. */
  63502. TEST_DECL(test_wolfSSL_BIO_should_retry),
  63503. TEST_DECL(test_wolfSSL_BIO_write),
  63504. TEST_DECL(test_wolfSSL_BIO_printf),
  63505. TEST_DECL(test_wolfSSL_BIO_f_md),
  63506. TEST_DECL(test_wolfSSL_BIO_up_ref),
  63507. TEST_DECL(test_wolfSSL_BIO_reset),
  63508. TEST_DECL(test_wolfSSL_BIO_get_len),
  63509. #endif
  63510. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  63511. TEST_DECL(test_wolfSSL_check_domain),
  63512. #endif
  63513. TEST_DECL(test_wolfSSL_cert_cb),
  63514. TEST_DECL(test_wolfSSL_cert_cb_dyn_ciphers),
  63515. TEST_DECL(test_wolfSSL_ciphersuite_auth),
  63516. TEST_DECL(test_wolfSSL_sigalg_info),
  63517. /* Can't memory test as tcp_connect aborts. */
  63518. TEST_DECL(test_wolfSSL_SESSION),
  63519. TEST_DECL(test_wolfSSL_SESSION_expire_downgrade),
  63520. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  63521. TEST_DECL(test_wolfSSL_ticket_keys),
  63522. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  63523. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  63524. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  63525. TEST_DECL(test_wolfSSL_verify_mode),
  63526. TEST_DECL(test_wolfSSL_verify_depth),
  63527. TEST_DECL(test_wolfSSL_verify_result),
  63528. TEST_DECL(test_wolfSSL_msg_callback),
  63529. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  63530. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  63531. TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
  63532. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  63533. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  63534. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  63535. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  63536. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  63537. TEST_DECL(test_wolfSSL_PEM_read),
  63538. TEST_DECL(test_wolfSSL_OpenSSL_version),
  63539. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  63540. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  63541. TEST_DECL(test_CONF_modules_xxx),
  63542. #ifdef OPENSSL_ALL
  63543. TEST_DECL(test_wolfSSL_TXT_DB),
  63544. TEST_DECL(test_wolfSSL_NCONF),
  63545. #endif
  63546. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  63547. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  63548. TEST_DECL(test_wolfSSL_SESSION_get_ex_new_index),
  63549. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  63550. TEST_DECL(test_CRYPTO_THREADID_xxx),
  63551. TEST_DECL(test_ENGINE_cleanup),
  63552. /* test the no op functions for compatibility */
  63553. TEST_DECL(test_no_op_functions),
  63554. /* OpenSSL error API tests */
  63555. TEST_DECL(test_ERR_load_crypto_strings),
  63556. #ifdef OPENSSL_ALL
  63557. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  63558. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  63559. TEST_DECL(test_wolfSSL_CTX_ctrl),
  63560. #endif /* OPENSSL_ALL */
  63561. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  63562. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  63563. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  63564. /*********************************
  63565. * Crypto API tests
  63566. *********************************/
  63567. TEST_DECL(test_wolfSSL_MD4),
  63568. TEST_DECL(test_wolfSSL_MD5),
  63569. TEST_DECL(test_wolfSSL_MD5_Transform),
  63570. TEST_DECL(test_wolfSSL_SHA),
  63571. TEST_DECL(test_wolfSSL_SHA_Transform),
  63572. TEST_DECL(test_wolfSSL_SHA224),
  63573. TEST_DECL(test_wolfSSL_SHA256),
  63574. TEST_DECL(test_wolfSSL_SHA256_Transform),
  63575. TEST_DECL(test_wolfSSL_SHA512_Transform),
  63576. TEST_DECL(test_wolfSSL_SHA512_224_Transform),
  63577. TEST_DECL(test_wolfSSL_SHA512_256_Transform),
  63578. TEST_DECL(test_wolfSSL_HMAC_CTX),
  63579. TEST_DECL(test_wolfSSL_HMAC),
  63580. TEST_DECL(test_wolfSSL_CMAC),
  63581. TEST_DECL(test_wolfSSL_DES),
  63582. TEST_DECL(test_wolfSSL_DES_ncbc),
  63583. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  63584. TEST_DECL(test_wolfSSL_DES_ede3_cbc_encrypt),
  63585. TEST_DECL(test_wolfSSL_AES_encrypt),
  63586. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  63587. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  63588. TEST_DECL(test_wolfSSL_AES_cfb128_encrypt),
  63589. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  63590. TEST_DECL(test_wolfSSL_RC4),
  63591. TEST_DECL(test_wolfSSL_RSA),
  63592. TEST_DECL(test_wolfSSL_RSA_DER),
  63593. TEST_DECL(test_wolfSSL_RSA_print),
  63594. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  63595. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  63596. TEST_DECL(test_wolfSSL_RSA_get0_key),
  63597. TEST_DECL(test_wolfSSL_RSA_meth),
  63598. TEST_DECL(test_wolfSSL_RSA_verify),
  63599. TEST_DECL(test_wolfSSL_RSA_sign),
  63600. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  63601. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  63602. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  63603. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  63604. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  63605. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  63606. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  63607. TEST_DECL(test_wolfSSL_RSA_ex_data),
  63608. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  63609. TEST_DECL(test_wolfSSL_RSA_To_Der),
  63610. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  63611. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  63612. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  63613. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  63614. TEST_DECL(test_wolfSSL_DH),
  63615. TEST_DECL(test_wolfSSL_DH_dup),
  63616. TEST_DECL(test_wolfSSL_DH_check),
  63617. TEST_DECL(test_wolfSSL_DH_prime),
  63618. TEST_DECL(test_wolfSSL_DH_1536_prime),
  63619. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  63620. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  63621. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  63622. TEST_DECL(test_wolfSSL_d2i_DHparams),
  63623. TEST_DECL(test_wolfSSL_DH_LoadDer),
  63624. TEST_DECL(test_wolfSSL_i2d_DHparams),
  63625. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  63626. TEST_DECL(test_wolfSSL_EC_GROUP),
  63627. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  63628. TEST_DECL(test_wolfSSL_EC_POINT),
  63629. TEST_DECL(test_wolfSSL_SPAKE),
  63630. TEST_DECL(test_wolfSSL_EC_KEY_generate),
  63631. TEST_DECL(test_EC_i2d),
  63632. TEST_DECL(test_wolfSSL_EC_curve),
  63633. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  63634. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  63635. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  63636. TEST_DECL(test_wolfSSL_EC_KEY_private_key),
  63637. TEST_DECL(test_wolfSSL_EC_KEY_public_key),
  63638. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  63639. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  63640. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  63641. TEST_DECL(test_ECDSA_size_sign),
  63642. TEST_DECL(test_ECDH_compute_key),
  63643. #endif
  63644. #ifdef OPENSSL_EXTRA
  63645. TEST_DECL(test_EC25519),
  63646. TEST_DECL(test_ED25519),
  63647. TEST_DECL(test_EC448),
  63648. TEST_DECL(test_ED448),
  63649. #endif
  63650. TEST_DECL(test_DSA_do_sign_verify),
  63651. #ifdef OPENSSL_ALL
  63652. TEST_DECL(test_wolfSSL_DSA_generate_parameters),
  63653. TEST_DECL(test_wolfSSL_DSA_SIG),
  63654. #endif
  63655. TEST_DECL(test_openssl_generate_key_and_cert),
  63656. TEST_DECL(test_wolfSSL_FIPS_mode),
  63657. TEST_DECL(test_openssl_FIPS_drbg),
  63658. /*********************************
  63659. * CertManager API tests
  63660. *********************************/
  63661. TEST_DECL(test_wolfSSL_CertManagerAPI),
  63662. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  63663. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer_ex),
  63664. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  63665. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  63666. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  63667. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  63668. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  63669. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  63670. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  63671. TEST_DECL(test_wolfSSL_CertManagerCRL),
  63672. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  63673. TEST_DECL(test_wolfSSL_CheckOCSPResponse),
  63674. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  63675. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  63676. !defined(WOLFSSL_NO_CLIENT_AUTH))
  63677. TEST_DECL(test_various_pathlen_chains),
  63678. #endif
  63679. /*********************************
  63680. * SSL/TLS API tests
  63681. *********************************/
  63682. TEST_DECL(test_wolfSSL_Method_Allocators),
  63683. #ifndef NO_WOLFSSL_SERVER
  63684. TEST_DECL(test_wolfSSL_CTX_new),
  63685. #endif
  63686. TEST_DECL(test_server_wolfSSL_new),
  63687. TEST_DECL(test_client_wolfSSL_new),
  63688. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  63689. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  63690. TEST_DECL(test_for_double_Free),
  63691. #endif
  63692. TEST_DECL(test_wolfSSL_set_options),
  63693. #ifdef WOLFSSL_TLS13
  63694. /* TLS v1.3 API tests */
  63695. TEST_DECL(test_tls13_apis),
  63696. TEST_DECL(test_tls13_cipher_suites),
  63697. #endif
  63698. TEST_DECL(test_wolfSSL_tmp_dh),
  63699. TEST_DECL(test_wolfSSL_ctrl),
  63700. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  63701. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  63702. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  63703. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  63704. TEST_DECL(test_wolfSSL_set_SSL_CTX),
  63705. #endif
  63706. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  63707. TEST_DECL(test_wolfSSL_security_level),
  63708. TEST_DECL(test_wolfSSL_SSL_in_init),
  63709. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  63710. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  63711. TEST_DECL(test_CONF_CTX_FILE),
  63712. TEST_DECL(test_CONF_CTX_CMDLINE),
  63713. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  63714. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  63715. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  63716. /* Bad certificate signature tests */
  63717. TEST_DECL(test_EccSigFailure_cm),
  63718. TEST_DECL(test_RsaSigFailure_cm),
  63719. #endif /* NO_CERTS */
  63720. /* PKCS8 testing */
  63721. TEST_DECL(test_wolfSSL_no_password_cb),
  63722. TEST_DECL(test_wolfSSL_PKCS8),
  63723. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  63724. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  63725. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  63726. TEST_DECL(test_wolfSSL_get_finished),
  63727. /* Uses Assert in handshake callback. */
  63728. TEST_DECL(test_wolfSSL_CTX_add_session),
  63729. /* Large number of memory allocations. */
  63730. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls13),
  63731. /* Large number of memory allocations. */
  63732. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls13),
  63733. /* Large number of memory allocations. */
  63734. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls12),
  63735. /* Large number of memory allocations. */
  63736. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls12),
  63737. /* Large number of memory allocations. */
  63738. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls11),
  63739. /* Large number of memory allocations. */
  63740. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls1),
  63741. #endif
  63742. TEST_DECL(test_SSL_CIPHER_get_xxx),
  63743. TEST_DECL(test_wolfSSL_ERR_strings),
  63744. TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
  63745. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  63746. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  63747. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  63748. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  63749. /* Large number of memory allocations. */
  63750. TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
  63751. TEST_DECL(test_wolfSSL_CertRsaPss),
  63752. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  63753. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  63754. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  63755. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  63756. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  63757. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  63758. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  63759. TEST_DECL(test_multiple_crls_same_issuer),
  63760. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  63761. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  63762. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  63763. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  63764. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  63765. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  63766. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  63767. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  63768. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  63769. TEST_DECL(test_SetTmpEC_DHE_Sz),
  63770. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  63771. #ifdef WOLFSSL_DTLS
  63772. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  63773. TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
  63774. #endif
  63775. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  63776. /* Uses Assert in handshake callback. */
  63777. TEST_DECL(test_wolfSSL_dtls_plaintext),
  63778. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  63779. defined(HAVE_IO_TESTS_DEPENDENCIES)
  63780. TEST_DECL(test_wolfSSL_read_write),
  63781. /* Can't memory test as server hangs if client fails before second connect.
  63782. */
  63783. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  63784. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_1),
  63785. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_2),
  63786. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_3),
  63787. TEST_DECL(test_wolfSSL_CTX_set_cipher_list),
  63788. /* Can't memory test as server hangs. */
  63789. TEST_DECL(test_wolfSSL_dtls_export),
  63790. /* Uses Assert in handshake callback. */
  63791. TEST_DECL(test_wolfSSL_tls_export),
  63792. #endif
  63793. TEST_DECL(test_wolfSSL_dtls_export_peers),
  63794. TEST_DECL(test_wolfSSL_SetMinVersion),
  63795. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  63796. /* wolfSSL handshake APIs. */
  63797. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  63798. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  63799. TEST_DECL(test_wolfSSL_BUF),
  63800. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  63801. /* Can't memory test as server hangs. */
  63802. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  63803. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  63804. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  63805. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  63806. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  63807. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  63808. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  63809. /* Can't memory test as server hangs. */
  63810. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  63811. TEST_DECL(test_wolfSSL_Tls13_postauth),
  63812. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  63813. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  63814. TEST_DECL(test_wolfSSL_CTX_set_max_proto_version),
  63815. TEST_DECL(test_wolfSSL_THREADID_hash),
  63816. /* TLS extensions tests */
  63817. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  63818. #ifdef HAVE_SNI
  63819. TEST_DECL(test_wolfSSL_UseSNI_params),
  63820. /* Uses Assert in handshake callback. */
  63821. TEST_DECL(test_wolfSSL_UseSNI_connection),
  63822. TEST_DECL(test_wolfSSL_SNI_GetFromBuffer),
  63823. #endif /* HAVE_SNI */
  63824. #endif
  63825. TEST_DECL(test_wolfSSL_UseTrustedCA),
  63826. TEST_DECL(test_wolfSSL_UseMaxFragment),
  63827. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  63828. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  63829. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  63830. /* Uses Assert in handshake callback. */
  63831. TEST_DECL(test_wolfSSL_UseALPN_connection),
  63832. TEST_DECL(test_wolfSSL_UseALPN_params),
  63833. #endif
  63834. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  63835. /* Uses Assert in handshake callback. */
  63836. TEST_DECL(test_wolfSSL_set_alpn_protos),
  63837. #endif
  63838. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  63839. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  63840. TEST_DECL(test_wolfSSL_SCR_Reconnect),
  63841. TEST_DECL(test_tls_ext_duplicate),
  63842. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  63843. defined(HAVE_IO_TESTS_DEPENDENCIES)
  63844. TEST_DECL(test_wolfSSL_Tls13_ECH_params),
  63845. /* Uses Assert in handshake callback. */
  63846. TEST_DECL(test_wolfSSL_Tls13_ECH),
  63847. #endif
  63848. TEST_DECL(test_wolfSSL_X509_TLS_version_test_1),
  63849. TEST_DECL(test_wolfSSL_X509_TLS_version_test_2),
  63850. /* OCSP Stapling */
  63851. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  63852. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  63853. TEST_DECL(test_self_signed_stapling),
  63854. /* Multicast */
  63855. TEST_DECL(test_wolfSSL_mcast),
  63856. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  63857. TEST_DECL(test_wolfSSL_msgCb),
  63858. TEST_DECL(test_wolfSSL_either_side),
  63859. TEST_DECL(test_wolfSSL_DTLS_either_side),
  63860. /* Uses Assert in handshake callback. */
  63861. TEST_DECL(test_wolfSSL_dtls_fragments),
  63862. /* Uses Assert in handshake callback. */
  63863. TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
  63864. /* Uses Assert in handshake callback. */
  63865. TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
  63866. /* Uses Assert in handshake callback. */
  63867. TEST_DECL(test_wolfSSL_dtls_bad_record),
  63868. /* Uses Assert in handshake callback. */
  63869. TEST_DECL(test_wolfSSL_dtls_stateless),
  63870. TEST_DECL(test_generate_cookie),
  63871. #ifndef NO_BIO
  63872. /* Can't memory test as server hangs. */
  63873. TEST_DECL(test_wolfSSL_BIO_connect),
  63874. /* Can't memory test as server Asserts in thread. */
  63875. TEST_DECL(test_wolfSSL_BIO_accept),
  63876. TEST_DECL(test_wolfSSL_BIO_tls),
  63877. #endif
  63878. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  63879. TEST_DECL(test_DhCallbacks),
  63880. #endif
  63881. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  63882. TEST_DECL(test_export_keying_material),
  63883. #endif
  63884. /* Can't memory test as client/server Asserts in thread. */
  63885. TEST_DECL(test_ticket_and_psk_mixing),
  63886. /* Can't memory test as client/server Asserts in thread. */
  63887. TEST_DECL(test_prioritize_psk),
  63888. /* Can't memory test as client/server hangs. */
  63889. TEST_DECL(test_wc_CryptoCb),
  63890. /* Can't memory test as client/server hangs. */
  63891. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  63892. #if !defined(NO_FILESYSTEM) && \
  63893. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  63894. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  63895. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  63896. TEST_DECL(test_wolfSSL_dtls_stateless_resume),
  63897. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  63898. #ifdef HAVE_MAX_FRAGMENT
  63899. TEST_DECL(test_wolfSSL_dtls_stateless_maxfrag),
  63900. #endif /* HAVE_MAX_FRAGMENT */
  63901. #ifndef NO_RSA
  63902. TEST_DECL(test_wolfSSL_dtls_stateless2),
  63903. #if !defined(NO_OLD_TLS)
  63904. TEST_DECL(test_wolfSSL_dtls_stateless_downgrade),
  63905. #endif /* !defined(NO_OLD_TLS) */
  63906. #endif /* ! NO_RSA */
  63907. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  63908. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) */
  63909. TEST_DECL(test_wolfSSL_CTX_set_ciphersuites),
  63910. TEST_DECL(test_wolfSSL_CRL_CERT_REVOKED_alert),
  63911. TEST_DECL(test_TLS_13_ticket_different_ciphers),
  63912. TEST_DECL(test_WOLFSSL_dtls_version_alert),
  63913. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  63914. && defined(WOLFSSL_TLS13) && \
  63915. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  63916. TEST_DECL(test_ticket_nonce_malloc),
  63917. #endif
  63918. TEST_DECL(test_ticket_ret_create),
  63919. TEST_DECL(test_extra_alerts_wrong_cs),
  63920. TEST_DECL(test_extra_alerts_skip_hs),
  63921. TEST_DECL(test_extra_alerts_bad_psk),
  63922. TEST_DECL(test_tls13_bad_psk_binder),
  63923. /* Can't memory test as client/server Asserts. */
  63924. TEST_DECL(test_harden_no_secure_renegotiation),
  63925. TEST_DECL(test_override_alt_cert_chain),
  63926. TEST_DECL(test_rpk_set_xxx_cert_type),
  63927. TEST_DECL(test_tls13_rpk_handshake),
  63928. TEST_DECL(test_dtls13_bad_epoch_ch),
  63929. TEST_DECL(test_short_session_id),
  63930. TEST_DECL(test_wolfSSL_dtls13_null_cipher),
  63931. /* Can't memory test as client/server hangs. */
  63932. TEST_DECL(test_dtls_msg_from_other_peer),
  63933. TEST_DECL(test_dtls_ipv6_check),
  63934. TEST_DECL(test_wolfSSL_SCR_after_resumption),
  63935. TEST_DECL(test_dtls_no_extensions),
  63936. TEST_DECL(test_TLSX_CA_NAMES_bad_extension),
  63937. TEST_DECL(test_dtls_1_0_hvr_downgrade),
  63938. TEST_DECL(test_session_ticket_no_id),
  63939. TEST_DECL(test_session_ticket_hs_update),
  63940. TEST_DECL(test_dtls_downgrade_scr_server),
  63941. TEST_DECL(test_dtls_downgrade_scr),
  63942. TEST_DECL(test_dtls_client_hello_timeout_downgrade),
  63943. TEST_DECL(test_dtls_client_hello_timeout),
  63944. TEST_DECL(test_dtls_dropped_ccs),
  63945. TEST_DECL(test_dtls_seq_num_downgrade),
  63946. TEST_DECL(test_certreq_sighash_algos),
  63947. TEST_DECL(test_revoked_loaded_int_cert),
  63948. TEST_DECL(test_dtls_frag_ch),
  63949. TEST_DECL(test_dtls13_frag_ch_pq),
  63950. TEST_DECL(test_dtls_empty_keyshare_with_cookie),
  63951. TEST_DECL(test_tls13_pq_groups),
  63952. TEST_DECL(test_tls13_early_data),
  63953. TEST_DECL(test_tls_multi_handshakes_one_record),
  63954. TEST_DECL(test_write_dup),
  63955. TEST_DECL(test_read_write_hs),
  63956. TEST_DECL(test_get_signature_nid),
  63957. TEST_DECL(test_tls_cert_store_unchanged),
  63958. /* This test needs to stay at the end to clean up any caches allocated. */
  63959. TEST_DECL(test_wolfSSL_Cleanup)
  63960. };
  63961. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  63962. static void TestSetup(void)
  63963. {
  63964. /* Stub, for now. Add common test setup code here. */
  63965. }
  63966. static void TestCleanup(void)
  63967. {
  63968. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  63969. /* Clear any errors added to the error queue during the test run. */
  63970. wolfSSL_ERR_clear_error();
  63971. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  63972. }
  63973. /* Print out all API test cases with numeric identifier.
  63974. */
  63975. void ApiTest_PrintTestCases(void)
  63976. {
  63977. int i;
  63978. printf("All Test Cases:\n");
  63979. for (i = 0; i < TEST_CASE_CNT; i++) {
  63980. printf("%3d: %s\n", i + 1, testCases[i].name);
  63981. }
  63982. }
  63983. /* Add test case with index to the list to run.
  63984. *
  63985. * @param [in] idx Index of test case to run starting at 1.
  63986. * @return 0 on success.
  63987. * @return BAD_FUNC_ARG when index is out of range of test case identifiers.
  63988. */
  63989. int ApiTest_RunIdx(int idx)
  63990. {
  63991. if (idx < 1 || idx > TEST_CASE_CNT) {
  63992. printf("Index out of range (1 - %d): %d\n", TEST_CASE_CNT, idx);
  63993. return BAD_FUNC_ARG;
  63994. }
  63995. testAll = 0;
  63996. testCases[idx-1].run = 1;
  63997. return 0;
  63998. }
  63999. /* Add test case with name to the list to run.
  64000. *
  64001. * @param [in] name Name of test case to run.
  64002. * @return 0 on success.
  64003. * @return BAD_FUNC_ARG when name is not a known test case name.
  64004. */
  64005. int ApiTest_RunName(char* name)
  64006. {
  64007. int i;
  64008. for (i = 0; i < TEST_CASE_CNT; i++) {
  64009. if (XSTRCMP(testCases[i].name, name) == 0) {
  64010. testAll = 0;
  64011. testCases[i].run = 1;
  64012. return 0;
  64013. }
  64014. }
  64015. printf("Test case name not found: %s\n", name);
  64016. printf("Use --list to see all test case names.\n");
  64017. return BAD_FUNC_ARG;
  64018. }
  64019. /* Converts the result code to a string.
  64020. *
  64021. * @param [in] res Test result code.
  64022. * @return String describing test result.
  64023. */
  64024. static const char* apitest_res_string(int res)
  64025. {
  64026. const char* str = "invalid result";
  64027. switch (res) {
  64028. case TEST_SUCCESS:
  64029. str = "passed";
  64030. break;
  64031. case TEST_FAIL:
  64032. str = "failed";
  64033. break;
  64034. case TEST_SKIPPED:
  64035. str = "skipped";
  64036. break;
  64037. }
  64038. return str;
  64039. }
  64040. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  64041. static double gettime_secs(void)
  64042. #if defined(_MSC_VER) && defined(_WIN32)
  64043. {
  64044. /* there's no gettimeofday for Windows, so we'll use system time */
  64045. #define EPOCH_DIFF 11644473600LL
  64046. FILETIME currentFileTime;
  64047. GetSystemTimePreciseAsFileTime(&currentFileTime);
  64048. ULARGE_INTEGER uli = { 0, 0 };
  64049. uli.LowPart = currentFileTime.dwLowDateTime;
  64050. uli.HighPart = currentFileTime.dwHighDateTime;
  64051. /* Convert to seconds since Unix epoch */
  64052. return (double)((uli.QuadPart - (EPOCH_DIFF * 10000000)) / 10000000.0);
  64053. }
  64054. #else
  64055. {
  64056. struct timeval tv;
  64057. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  64058. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
  64059. }
  64060. #endif
  64061. #endif
  64062. int ApiTest(void)
  64063. {
  64064. int i;
  64065. int ret;
  64066. int res = 0;
  64067. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  64068. double timeDiff;
  64069. #endif
  64070. printf(" Begin API Tests\n");
  64071. fflush(stdout);
  64072. /* we must perform init and cleanup if not all tests are running */
  64073. if (!testAll) {
  64074. #ifdef WOLFCRYPT_ONLY
  64075. if (wolfCrypt_Init() != 0) {
  64076. printf("wolfCrypt Initialization failed\n");
  64077. res = 1;
  64078. }
  64079. #else
  64080. if (wolfSSL_Init() != WOLFSSL_SUCCESS) {
  64081. printf("wolfSSL Initialization failed\n");
  64082. res = 1;
  64083. }
  64084. #endif
  64085. }
  64086. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  64087. if (res == 0) {
  64088. if (create_tmp_dir(tmpDirName, sizeof(tmpDirName) - 1) == NULL) {
  64089. printf("failed to create tmp dir\n");
  64090. res = 1;
  64091. }
  64092. else {
  64093. tmpDirNameSet = 1;
  64094. }
  64095. }
  64096. #endif
  64097. if (res == 0) {
  64098. for (i = 0; i < TEST_CASE_CNT; ++i) {
  64099. EXPECT_DECLS;
  64100. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  64101. currentTestName = testCases[i].name;
  64102. #endif
  64103. /* When not testing all cases then skip if not marked for running.
  64104. */
  64105. if (!testAll && !testCases[i].run) {
  64106. continue;
  64107. }
  64108. TestSetup();
  64109. printf(" %3d: %-52s:", i + 1, testCases[i].name);
  64110. fflush(stdout);
  64111. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  64112. timeDiff = gettime_secs();
  64113. #endif
  64114. ret = testCases[i].func();
  64115. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  64116. timeDiff = gettime_secs() - timeDiff;
  64117. #endif
  64118. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  64119. if (ret != TEST_SKIPPED) {
  64120. printf(" %s (%9.5lf)\n", apitest_res_string(ret), timeDiff);
  64121. }
  64122. else
  64123. #endif
  64124. {
  64125. printf(" %s\n", apitest_res_string(ret));
  64126. }
  64127. fflush(stdout);
  64128. /* if return code is < 0 and not skipped then assert error */
  64129. Expect((ret > 0 || ret == TEST_SKIPPED),
  64130. ("Test failed\n"),
  64131. ("ret %d", ret));
  64132. testCases[i].fail = ((ret <= 0) && (ret != TEST_SKIPPED));
  64133. res |= ((ret <= 0) && (ret != TEST_SKIPPED));
  64134. TestCleanup();
  64135. }
  64136. }
  64137. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  64138. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  64139. wc_ecc_fp_free(); /* free per thread cache */
  64140. #endif
  64141. if (!testAll) {
  64142. #ifdef WOLFCRYPT_ONLY
  64143. wolfCrypt_Cleanup();
  64144. #else
  64145. wolfSSL_Cleanup();
  64146. #endif
  64147. }
  64148. (void)testDevId;
  64149. if (res != 0) {
  64150. printf("\nFAILURES:\n");
  64151. for (i = 0; i < TEST_CASE_CNT; ++i) {
  64152. if (testCases[i].fail) {
  64153. printf(" %3d: %s\n", i + 1, testCases[i].name);
  64154. }
  64155. }
  64156. printf("\n");
  64157. fflush(stdout);
  64158. }
  64159. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  64160. if (tmpDirNameSet) {
  64161. printf("\nBinary dumps of the memio streams can be found in the\n"
  64162. "%s directory. This can be imported into\n"
  64163. "Wireshark by transforming the file with\n"
  64164. "\tod -Ax -tx1 -v stream.dump > stream.dump.hex\n"
  64165. "And then loading test_output.dump.hex into Wireshark using\n"
  64166. "the \"Import from Hex Dump...\" option and selecting the\n"
  64167. "TCP encapsulation option.\n", tmpDirName);
  64168. }
  64169. #endif
  64170. printf(" End API Tests\n");
  64171. fflush(stdout);
  64172. return res;
  64173. }