api.c 2.5 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415354163541735418354193542035421354223542335424354253542635427354283542935430354313543235433354343543535436354373543835439354403544135442354433544435445354463544735448354493545035451354523545335454354553545635457354583545935460354613546235463354643546535466354673546835469354703547135472354733547435475354763547735478354793548035481354823548335484354853548635487354883548935490354913549235493354943549535496354973549835499355003550135502355033550435505355063550735508355093551035511355123551335514355153551635517355183551935520355213552235523355243552535526355273552835529355303553135532355333553435535355363553735538355393554035541355423554335544355453554635547355483554935550355513555235553355543555535556355573555835559355603556135562355633556435565355663556735568355693557035571355723557335574355753557635577355783557935580355813558235583355843558535586355873558835589355903559135592355933559435595355963559735598355993560035601356023560335604356053560635607356083560935610356113561235613356143561535616356173561835619356203562135622356233562435625356263562735628356293563035631356323563335634356353563635637356383563935640356413564235643356443564535646356473564835649356503565135652356533565435655356563565735658356593566035661356623566335664356653566635667356683566935670356713567235673356743567535676356773567835679356803568135682356833568435685356863568735688356893569035691356923569335694356953569635697356983569935700357013570235703357043570535706357073570835709357103571135712357133571435715357163571735718357193572035721357223572335724357253572635727357283572935730357313573235733357343573535736357373573835739357403574135742357433574435745357463574735748357493575035751357523575335754357553575635757357583575935760357613576235763357643576535766357673576835769357703577135772357733577435775357763577735778357793578035781357823578335784357853578635787357883578935790357913579235793357943579535796357973579835799358003580135802358033580435805358063580735808358093581035811358123581335814358153581635817358183581935820358213582235823358243582535826358273582835829358303583135832358333583435835358363583735838358393584035841358423584335844358453584635847358483584935850358513585235853358543585535856358573585835859358603586135862358633586435865358663586735868358693587035871358723587335874358753587635877358783587935880358813588235883358843588535886358873588835889358903589135892358933589435895358963589735898358993590035901359023590335904359053590635907359083590935910359113591235913359143591535916359173591835919359203592135922359233592435925359263592735928359293593035931359323593335934359353593635937359383593935940359413594235943359443594535946359473594835949359503595135952359533595435955359563595735958359593596035961359623596335964359653596635967359683596935970359713597235973359743597535976359773597835979359803598135982359833598435985359863598735988359893599035991359923599335994359953599635997359983599936000360013600236003360043600536006360073600836009360103601136012360133601436015360163601736018360193602036021360223602336024360253602636027360283602936030360313603236033360343603536036360373603836039360403604136042360433604436045360463604736048360493605036051360523605336054360553605636057360583605936060360613606236063360643606536066360673606836069360703607136072360733607436075360763607736078360793608036081360823608336084360853608636087360883608936090360913609236093360943609536096360973609836099361003610136102361033610436105361063610736108361093611036111361123611336114361153611636117361183611936120361213612236123361243612536126361273612836129361303613136132361333613436135361363613736138361393614036141361423614336144361453614636147361483614936150361513615236153361543615536156361573615836159361603616136162361633616436165361663616736168361693617036171361723617336174361753617636177361783617936180361813618236183361843618536186361873618836189361903619136192361933619436195361963619736198361993620036201362023620336204362053620636207362083620936210362113621236213362143621536216362173621836219362203622136222362233622436225362263622736228362293623036231362323623336234362353623636237362383623936240362413624236243362443624536246362473624836249362503625136252362533625436255362563625736258362593626036261362623626336264362653626636267362683626936270362713627236273362743627536276362773627836279362803628136282362833628436285362863628736288362893629036291362923629336294362953629636297362983629936300363013630236303363043630536306363073630836309363103631136312363133631436315363163631736318363193632036321363223632336324363253632636327363283632936330363313633236333363343633536336363373633836339363403634136342363433634436345363463634736348363493635036351363523635336354363553635636357363583635936360363613636236363363643636536366363673636836369363703637136372363733637436375363763637736378363793638036381363823638336384363853638636387363883638936390363913639236393363943639536396363973639836399364003640136402364033640436405364063640736408364093641036411364123641336414364153641636417364183641936420364213642236423364243642536426364273642836429364303643136432364333643436435364363643736438364393644036441364423644336444364453644636447364483644936450364513645236453364543645536456364573645836459364603646136462364633646436465364663646736468364693647036471364723647336474364753647636477364783647936480364813648236483364843648536486364873648836489364903649136492364933649436495364963649736498364993650036501365023650336504365053650636507365083650936510365113651236513365143651536516365173651836519365203652136522365233652436525365263652736528365293653036531365323653336534365353653636537365383653936540365413654236543365443654536546365473654836549365503655136552365533655436555365563655736558365593656036561365623656336564365653656636567365683656936570365713657236573365743657536576365773657836579365803658136582365833658436585365863658736588365893659036591365923659336594365953659636597365983659936600366013660236603366043660536606366073660836609366103661136612366133661436615366163661736618366193662036621366223662336624366253662636627366283662936630366313663236633366343663536636366373663836639366403664136642366433664436645366463664736648366493665036651366523665336654366553665636657366583665936660366613666236663366643666536666366673666836669366703667136672366733667436675366763667736678366793668036681366823668336684366853668636687366883668936690366913669236693366943669536696366973669836699367003670136702367033670436705367063670736708367093671036711367123671336714367153671636717367183671936720367213672236723367243672536726367273672836729367303673136732367333673436735367363673736738367393674036741367423674336744367453674636747367483674936750367513675236753367543675536756367573675836759367603676136762367633676436765367663676736768367693677036771367723677336774367753677636777367783677936780367813678236783367843678536786367873678836789367903679136792367933679436795367963679736798367993680036801368023680336804368053680636807368083680936810368113681236813368143681536816368173681836819368203682136822368233682436825368263682736828368293683036831368323683336834368353683636837368383683936840368413684236843368443684536846368473684836849368503685136852368533685436855368563685736858368593686036861368623686336864368653686636867368683686936870368713687236873368743687536876368773687836879368803688136882368833688436885368863688736888368893689036891368923689336894368953689636897368983689936900369013690236903369043690536906369073690836909369103691136912369133691436915369163691736918369193692036921369223692336924369253692636927369283692936930369313693236933369343693536936369373693836939369403694136942369433694436945369463694736948369493695036951369523695336954369553695636957369583695936960369613696236963369643696536966369673696836969369703697136972369733697436975369763697736978369793698036981369823698336984369853698636987369883698936990369913699236993369943699536996369973699836999370003700137002370033700437005370063700737008370093701037011370123701337014370153701637017370183701937020370213702237023370243702537026370273702837029370303703137032370333703437035370363703737038370393704037041370423704337044370453704637047370483704937050370513705237053370543705537056370573705837059370603706137062370633706437065370663706737068370693707037071370723707337074370753707637077370783707937080370813708237083370843708537086370873708837089370903709137092370933709437095370963709737098370993710037101371023710337104371053710637107371083710937110371113711237113371143711537116371173711837119371203712137122371233712437125371263712737128371293713037131371323713337134371353713637137371383713937140371413714237143371443714537146371473714837149371503715137152371533715437155371563715737158371593716037161371623716337164371653716637167371683716937170371713717237173371743717537176371773717837179371803718137182371833718437185371863718737188371893719037191371923719337194371953719637197371983719937200372013720237203372043720537206372073720837209372103721137212372133721437215372163721737218372193722037221372223722337224372253722637227372283722937230372313723237233372343723537236372373723837239372403724137242372433724437245372463724737248372493725037251372523725337254372553725637257372583725937260372613726237263372643726537266372673726837269372703727137272372733727437275372763727737278372793728037281372823728337284372853728637287372883728937290372913729237293372943729537296372973729837299373003730137302373033730437305373063730737308373093731037311373123731337314373153731637317373183731937320373213732237323373243732537326373273732837329373303733137332373333733437335373363733737338373393734037341373423734337344373453734637347373483734937350373513735237353373543735537356373573735837359373603736137362373633736437365373663736737368373693737037371373723737337374373753737637377373783737937380373813738237383373843738537386373873738837389373903739137392373933739437395373963739737398373993740037401374023740337404374053740637407374083740937410374113741237413374143741537416374173741837419374203742137422374233742437425374263742737428374293743037431374323743337434374353743637437374383743937440374413744237443374443744537446374473744837449374503745137452374533745437455374563745737458374593746037461374623746337464374653746637467374683746937470374713747237473374743747537476374773747837479374803748137482374833748437485374863748737488374893749037491374923749337494374953749637497374983749937500375013750237503375043750537506375073750837509375103751137512375133751437515375163751737518375193752037521375223752337524375253752637527375283752937530375313753237533375343753537536375373753837539375403754137542375433754437545375463754737548375493755037551375523755337554375553755637557375583755937560375613756237563375643756537566375673756837569375703757137572375733757437575375763757737578375793758037581375823758337584375853758637587375883758937590375913759237593375943759537596375973759837599376003760137602376033760437605376063760737608376093761037611376123761337614376153761637617376183761937620376213762237623376243762537626376273762837629376303763137632376333763437635376363763737638376393764037641376423764337644376453764637647376483764937650376513765237653376543765537656376573765837659376603766137662376633766437665376663766737668376693767037671376723767337674376753767637677376783767937680376813768237683376843768537686376873768837689376903769137692376933769437695376963769737698376993770037701377023770337704377053770637707377083770937710377113771237713377143771537716377173771837719377203772137722377233772437725377263772737728377293773037731377323773337734377353773637737377383773937740377413774237743377443774537746377473774837749377503775137752377533775437755377563775737758377593776037761377623776337764377653776637767377683776937770377713777237773377743777537776377773777837779377803778137782377833778437785377863778737788377893779037791377923779337794377953779637797377983779937800378013780237803378043780537806378073780837809378103781137812378133781437815378163781737818378193782037821378223782337824378253782637827378283782937830378313783237833378343783537836378373783837839378403784137842378433784437845378463784737848378493785037851378523785337854378553785637857378583785937860378613786237863378643786537866378673786837869378703787137872378733787437875378763787737878378793788037881378823788337884378853788637887378883788937890378913789237893378943789537896378973789837899379003790137902379033790437905379063790737908379093791037911379123791337914379153791637917379183791937920379213792237923379243792537926379273792837929379303793137932379333793437935379363793737938379393794037941379423794337944379453794637947379483794937950379513795237953379543795537956379573795837959379603796137962379633796437965379663796737968379693797037971379723797337974379753797637977379783797937980379813798237983379843798537986379873798837989379903799137992379933799437995379963799737998379993800038001380023800338004380053800638007380083800938010380113801238013380143801538016380173801838019380203802138022380233802438025380263802738028380293803038031380323803338034380353803638037380383803938040380413804238043380443804538046380473804838049380503805138052380533805438055380563805738058380593806038061380623806338064380653806638067380683806938070380713807238073380743807538076380773807838079380803808138082380833808438085380863808738088380893809038091380923809338094380953809638097380983809938100381013810238103381043810538106381073810838109381103811138112381133811438115381163811738118381193812038121381223812338124381253812638127381283812938130381313813238133381343813538136381373813838139381403814138142381433814438145381463814738148381493815038151381523815338154381553815638157381583815938160381613816238163381643816538166381673816838169381703817138172381733817438175381763817738178381793818038181381823818338184381853818638187381883818938190381913819238193381943819538196381973819838199382003820138202382033820438205382063820738208382093821038211382123821338214382153821638217382183821938220382213822238223382243822538226382273822838229382303823138232382333823438235382363823738238382393824038241382423824338244382453824638247382483824938250382513825238253382543825538256382573825838259382603826138262382633826438265382663826738268382693827038271382723827338274382753827638277382783827938280382813828238283382843828538286382873828838289382903829138292382933829438295382963829738298382993830038301383023830338304383053830638307383083830938310383113831238313383143831538316383173831838319383203832138322383233832438325383263832738328383293833038331383323833338334383353833638337383383833938340383413834238343383443834538346383473834838349383503835138352383533835438355383563835738358383593836038361383623836338364383653836638367383683836938370383713837238373383743837538376383773837838379383803838138382383833838438385383863838738388383893839038391383923839338394383953839638397383983839938400384013840238403384043840538406384073840838409384103841138412384133841438415384163841738418384193842038421384223842338424384253842638427384283842938430384313843238433384343843538436384373843838439384403844138442384433844438445384463844738448384493845038451384523845338454384553845638457384583845938460384613846238463384643846538466384673846838469384703847138472384733847438475384763847738478384793848038481384823848338484384853848638487384883848938490384913849238493384943849538496384973849838499385003850138502385033850438505385063850738508385093851038511385123851338514385153851638517385183851938520385213852238523385243852538526385273852838529385303853138532385333853438535385363853738538385393854038541385423854338544385453854638547385483854938550385513855238553385543855538556385573855838559385603856138562385633856438565385663856738568385693857038571385723857338574385753857638577385783857938580385813858238583385843858538586385873858838589385903859138592385933859438595385963859738598385993860038601386023860338604386053860638607386083860938610386113861238613386143861538616386173861838619386203862138622386233862438625386263862738628386293863038631386323863338634386353863638637386383863938640386413864238643386443864538646386473864838649386503865138652386533865438655386563865738658386593866038661386623866338664386653866638667386683866938670386713867238673386743867538676386773867838679386803868138682386833868438685386863868738688386893869038691386923869338694386953869638697386983869938700387013870238703387043870538706387073870838709387103871138712387133871438715387163871738718387193872038721387223872338724387253872638727387283872938730387313873238733387343873538736387373873838739387403874138742387433874438745387463874738748387493875038751387523875338754387553875638757387583875938760387613876238763387643876538766387673876838769387703877138772387733877438775387763877738778387793878038781387823878338784387853878638787387883878938790387913879238793387943879538796387973879838799388003880138802388033880438805388063880738808388093881038811388123881338814388153881638817388183881938820388213882238823388243882538826388273882838829388303883138832388333883438835388363883738838388393884038841388423884338844388453884638847388483884938850388513885238853388543885538856388573885838859388603886138862388633886438865388663886738868388693887038871388723887338874388753887638877388783887938880388813888238883388843888538886388873888838889388903889138892388933889438895388963889738898388993890038901389023890338904389053890638907389083890938910389113891238913389143891538916389173891838919389203892138922389233892438925389263892738928389293893038931389323893338934389353893638937389383893938940389413894238943389443894538946389473894838949389503895138952389533895438955389563895738958389593896038961389623896338964389653896638967389683896938970389713897238973389743897538976389773897838979389803898138982389833898438985389863898738988389893899038991389923899338994389953899638997389983899939000390013900239003390043900539006390073900839009390103901139012390133901439015390163901739018390193902039021390223902339024390253902639027390283902939030390313903239033390343903539036390373903839039390403904139042390433904439045390463904739048390493905039051390523905339054390553905639057390583905939060390613906239063390643906539066390673906839069390703907139072390733907439075390763907739078390793908039081390823908339084390853908639087390883908939090390913909239093390943909539096390973909839099391003910139102391033910439105391063910739108391093911039111391123911339114391153911639117391183911939120391213912239123391243912539126391273912839129391303913139132391333913439135391363913739138391393914039141391423914339144391453914639147391483914939150391513915239153391543915539156391573915839159391603916139162391633916439165391663916739168391693917039171391723917339174391753917639177391783917939180391813918239183391843918539186391873918839189391903919139192391933919439195391963919739198391993920039201392023920339204392053920639207392083920939210392113921239213392143921539216392173921839219392203922139222392233922439225392263922739228392293923039231392323923339234392353923639237392383923939240392413924239243392443924539246392473924839249392503925139252392533925439255392563925739258392593926039261392623926339264392653926639267392683926939270392713927239273392743927539276392773927839279392803928139282392833928439285392863928739288392893929039291392923929339294392953929639297392983929939300393013930239303393043930539306393073930839309393103931139312393133931439315393163931739318393193932039321393223932339324393253932639327393283932939330393313933239333393343933539336393373933839339393403934139342393433934439345393463934739348393493935039351393523935339354393553935639357393583935939360393613936239363393643936539366393673936839369393703937139372393733937439375393763937739378393793938039381393823938339384393853938639387393883938939390393913939239393393943939539396393973939839399394003940139402394033940439405394063940739408394093941039411394123941339414394153941639417394183941939420394213942239423394243942539426394273942839429394303943139432394333943439435394363943739438394393944039441394423944339444394453944639447394483944939450394513945239453394543945539456394573945839459394603946139462394633946439465394663946739468394693947039471394723947339474394753947639477394783947939480394813948239483394843948539486394873948839489394903949139492394933949439495394963949739498394993950039501395023950339504395053950639507395083950939510395113951239513395143951539516395173951839519395203952139522395233952439525395263952739528395293953039531395323953339534395353953639537395383953939540395413954239543395443954539546395473954839549395503955139552395533955439555395563955739558395593956039561395623956339564395653956639567395683956939570395713957239573395743957539576395773957839579395803958139582395833958439585395863958739588395893959039591395923959339594395953959639597395983959939600396013960239603396043960539606396073960839609396103961139612396133961439615396163961739618396193962039621396223962339624396253962639627396283962939630396313963239633396343963539636396373963839639396403964139642396433964439645396463964739648396493965039651396523965339654396553965639657396583965939660396613966239663396643966539666396673966839669396703967139672396733967439675396763967739678396793968039681396823968339684396853968639687396883968939690396913969239693396943969539696396973969839699397003970139702397033970439705397063970739708397093971039711397123971339714397153971639717397183971939720397213972239723397243972539726397273972839729397303973139732397333973439735397363973739738397393974039741397423974339744397453974639747397483974939750397513975239753397543975539756397573975839759397603976139762397633976439765397663976739768397693977039771397723977339774397753977639777397783977939780397813978239783397843978539786397873978839789397903979139792397933979439795397963979739798397993980039801398023980339804398053980639807398083980939810398113981239813398143981539816398173981839819398203982139822398233982439825398263982739828398293983039831398323983339834398353983639837398383983939840398413984239843398443984539846398473984839849398503985139852398533985439855398563985739858398593986039861398623986339864398653986639867398683986939870398713987239873398743987539876398773987839879398803988139882398833988439885398863988739888398893989039891398923989339894398953989639897398983989939900399013990239903399043990539906399073990839909399103991139912399133991439915399163991739918399193992039921399223992339924399253992639927399283992939930399313993239933399343993539936399373993839939399403994139942399433994439945399463994739948399493995039951399523995339954399553995639957399583995939960399613996239963399643996539966399673996839969399703997139972399733997439975399763997739978399793998039981399823998339984399853998639987399883998939990399913999239993399943999539996399973999839999400004000140002400034000440005400064000740008400094001040011400124001340014400154001640017400184001940020400214002240023400244002540026400274002840029400304003140032400334003440035400364003740038400394004040041400424004340044400454004640047400484004940050400514005240053400544005540056400574005840059400604006140062400634006440065400664006740068400694007040071400724007340074400754007640077400784007940080400814008240083400844008540086400874008840089400904009140092400934009440095400964009740098400994010040101401024010340104401054010640107401084010940110401114011240113401144011540116401174011840119401204012140122401234012440125401264012740128401294013040131401324013340134401354013640137401384013940140401414014240143401444014540146401474014840149401504015140152401534015440155401564015740158401594016040161401624016340164401654016640167401684016940170401714017240173401744017540176401774017840179401804018140182401834018440185401864018740188401894019040191401924019340194401954019640197401984019940200402014020240203402044020540206402074020840209402104021140212402134021440215402164021740218402194022040221402224022340224402254022640227402284022940230402314023240233402344023540236402374023840239402404024140242402434024440245402464024740248402494025040251402524025340254402554025640257402584025940260402614026240263402644026540266402674026840269402704027140272402734027440275402764027740278402794028040281402824028340284402854028640287402884028940290402914029240293402944029540296402974029840299403004030140302403034030440305403064030740308403094031040311403124031340314403154031640317403184031940320403214032240323403244032540326403274032840329403304033140332403334033440335403364033740338403394034040341403424034340344403454034640347403484034940350403514035240353403544035540356403574035840359403604036140362403634036440365403664036740368403694037040371403724037340374403754037640377403784037940380403814038240383403844038540386403874038840389403904039140392403934039440395403964039740398403994040040401404024040340404404054040640407404084040940410404114041240413404144041540416404174041840419404204042140422404234042440425404264042740428404294043040431404324043340434404354043640437404384043940440404414044240443404444044540446404474044840449404504045140452404534045440455404564045740458404594046040461404624046340464404654046640467404684046940470404714047240473404744047540476404774047840479404804048140482404834048440485404864048740488404894049040491404924049340494404954049640497404984049940500405014050240503405044050540506405074050840509405104051140512405134051440515405164051740518405194052040521405224052340524405254052640527405284052940530405314053240533405344053540536405374053840539405404054140542405434054440545405464054740548405494055040551405524055340554405554055640557405584055940560405614056240563405644056540566405674056840569405704057140572405734057440575405764057740578405794058040581405824058340584405854058640587405884058940590405914059240593405944059540596405974059840599406004060140602406034060440605406064060740608406094061040611406124061340614406154061640617406184061940620406214062240623406244062540626406274062840629406304063140632406334063440635406364063740638406394064040641406424064340644406454064640647406484064940650406514065240653406544065540656406574065840659406604066140662406634066440665406664066740668406694067040671406724067340674406754067640677406784067940680406814068240683406844068540686406874068840689406904069140692406934069440695406964069740698406994070040701407024070340704407054070640707407084070940710407114071240713407144071540716407174071840719407204072140722407234072440725407264072740728407294073040731407324073340734407354073640737407384073940740407414074240743407444074540746407474074840749407504075140752407534075440755407564075740758407594076040761407624076340764407654076640767407684076940770407714077240773407744077540776407774077840779407804078140782407834078440785407864078740788407894079040791407924079340794407954079640797407984079940800408014080240803408044080540806408074080840809408104081140812408134081440815408164081740818408194082040821408224082340824408254082640827408284082940830408314083240833408344083540836408374083840839408404084140842408434084440845408464084740848408494085040851408524085340854408554085640857408584085940860408614086240863408644086540866408674086840869408704087140872408734087440875408764087740878408794088040881408824088340884408854088640887408884088940890408914089240893408944089540896408974089840899409004090140902409034090440905409064090740908409094091040911409124091340914409154091640917409184091940920409214092240923409244092540926409274092840929409304093140932409334093440935409364093740938409394094040941409424094340944409454094640947409484094940950409514095240953409544095540956409574095840959409604096140962409634096440965409664096740968409694097040971409724097340974409754097640977409784097940980409814098240983409844098540986409874098840989409904099140992409934099440995409964099740998409994100041001410024100341004410054100641007410084100941010410114101241013410144101541016410174101841019410204102141022410234102441025410264102741028410294103041031410324103341034410354103641037410384103941040410414104241043410444104541046410474104841049410504105141052410534105441055410564105741058410594106041061410624106341064410654106641067410684106941070410714107241073410744107541076410774107841079410804108141082410834108441085410864108741088410894109041091410924109341094410954109641097410984109941100411014110241103411044110541106411074110841109411104111141112411134111441115411164111741118411194112041121411224112341124411254112641127411284112941130411314113241133411344113541136411374113841139411404114141142411434114441145411464114741148411494115041151411524115341154411554115641157411584115941160411614116241163411644116541166411674116841169411704117141172411734117441175411764117741178411794118041181411824118341184411854118641187411884118941190411914119241193411944119541196411974119841199412004120141202412034120441205412064120741208412094121041211412124121341214412154121641217412184121941220412214122241223412244122541226412274122841229412304123141232412334123441235412364123741238412394124041241412424124341244412454124641247412484124941250412514125241253412544125541256412574125841259412604126141262412634126441265412664126741268412694127041271412724127341274412754127641277412784127941280412814128241283412844128541286412874128841289412904129141292412934129441295412964129741298412994130041301413024130341304413054130641307413084130941310413114131241313413144131541316413174131841319413204132141322413234132441325413264132741328413294133041331413324133341334413354133641337413384133941340413414134241343413444134541346413474134841349413504135141352413534135441355413564135741358413594136041361413624136341364413654136641367413684136941370413714137241373413744137541376413774137841379413804138141382413834138441385413864138741388413894139041391413924139341394413954139641397413984139941400414014140241403414044140541406414074140841409414104141141412414134141441415414164141741418414194142041421414224142341424414254142641427414284142941430414314143241433414344143541436414374143841439414404144141442414434144441445414464144741448414494145041451414524145341454414554145641457414584145941460414614146241463414644146541466414674146841469414704147141472414734147441475414764147741478414794148041481414824148341484414854148641487414884148941490414914149241493414944149541496414974149841499415004150141502415034150441505415064150741508415094151041511415124151341514415154151641517415184151941520415214152241523415244152541526415274152841529415304153141532415334153441535415364153741538415394154041541415424154341544415454154641547415484154941550415514155241553415544155541556415574155841559415604156141562415634156441565415664156741568415694157041571415724157341574415754157641577415784157941580415814158241583415844158541586415874158841589415904159141592415934159441595415964159741598415994160041601416024160341604416054160641607416084160941610416114161241613416144161541616416174161841619416204162141622416234162441625416264162741628416294163041631416324163341634416354163641637416384163941640416414164241643416444164541646416474164841649416504165141652416534165441655416564165741658416594166041661416624166341664416654166641667416684166941670416714167241673416744167541676416774167841679416804168141682416834168441685416864168741688416894169041691416924169341694416954169641697416984169941700417014170241703417044170541706417074170841709417104171141712417134171441715417164171741718417194172041721417224172341724417254172641727417284172941730417314173241733417344173541736417374173841739417404174141742417434174441745417464174741748417494175041751417524175341754417554175641757417584175941760417614176241763417644176541766417674176841769417704177141772417734177441775417764177741778417794178041781417824178341784417854178641787417884178941790417914179241793417944179541796417974179841799418004180141802418034180441805418064180741808418094181041811418124181341814418154181641817418184181941820418214182241823418244182541826418274182841829418304183141832418334183441835418364183741838418394184041841418424184341844418454184641847418484184941850418514185241853418544185541856418574185841859418604186141862418634186441865418664186741868418694187041871418724187341874418754187641877418784187941880418814188241883418844188541886418874188841889418904189141892418934189441895418964189741898418994190041901419024190341904419054190641907419084190941910419114191241913419144191541916419174191841919419204192141922419234192441925419264192741928419294193041931419324193341934419354193641937419384193941940419414194241943419444194541946419474194841949419504195141952419534195441955419564195741958419594196041961419624196341964419654196641967419684196941970419714197241973419744197541976419774197841979419804198141982419834198441985419864198741988419894199041991419924199341994419954199641997419984199942000420014200242003420044200542006420074200842009420104201142012420134201442015420164201742018420194202042021420224202342024420254202642027420284202942030420314203242033420344203542036420374203842039420404204142042420434204442045420464204742048420494205042051420524205342054420554205642057420584205942060420614206242063420644206542066420674206842069420704207142072420734207442075420764207742078420794208042081420824208342084420854208642087420884208942090420914209242093420944209542096420974209842099421004210142102421034210442105421064210742108421094211042111421124211342114421154211642117421184211942120421214212242123421244212542126421274212842129421304213142132421334213442135421364213742138421394214042141421424214342144421454214642147421484214942150421514215242153421544215542156421574215842159421604216142162421634216442165421664216742168421694217042171421724217342174421754217642177421784217942180421814218242183421844218542186421874218842189421904219142192421934219442195421964219742198421994220042201422024220342204422054220642207422084220942210422114221242213422144221542216422174221842219422204222142222422234222442225422264222742228422294223042231422324223342234422354223642237422384223942240422414224242243422444224542246422474224842249422504225142252422534225442255422564225742258422594226042261422624226342264422654226642267422684226942270422714227242273422744227542276422774227842279422804228142282422834228442285422864228742288422894229042291422924229342294422954229642297422984229942300423014230242303423044230542306423074230842309423104231142312423134231442315423164231742318423194232042321423224232342324423254232642327423284232942330423314233242333423344233542336423374233842339423404234142342423434234442345423464234742348423494235042351423524235342354423554235642357423584235942360423614236242363423644236542366423674236842369423704237142372423734237442375423764237742378423794238042381423824238342384423854238642387423884238942390423914239242393423944239542396423974239842399424004240142402424034240442405424064240742408424094241042411424124241342414424154241642417424184241942420424214242242423424244242542426424274242842429424304243142432424334243442435424364243742438424394244042441424424244342444424454244642447424484244942450424514245242453424544245542456424574245842459424604246142462424634246442465424664246742468424694247042471424724247342474424754247642477424784247942480424814248242483424844248542486424874248842489424904249142492424934249442495424964249742498424994250042501425024250342504425054250642507425084250942510425114251242513425144251542516425174251842519425204252142522425234252442525425264252742528425294253042531425324253342534425354253642537425384253942540425414254242543425444254542546425474254842549425504255142552425534255442555425564255742558425594256042561425624256342564425654256642567425684256942570425714257242573425744257542576425774257842579425804258142582425834258442585425864258742588425894259042591425924259342594425954259642597425984259942600426014260242603426044260542606426074260842609426104261142612426134261442615426164261742618426194262042621426224262342624426254262642627426284262942630426314263242633426344263542636426374263842639426404264142642426434264442645426464264742648426494265042651426524265342654426554265642657426584265942660426614266242663426644266542666426674266842669426704267142672426734267442675426764267742678426794268042681426824268342684426854268642687426884268942690426914269242693426944269542696426974269842699427004270142702427034270442705427064270742708427094271042711427124271342714427154271642717427184271942720427214272242723427244272542726427274272842729427304273142732427334273442735427364273742738427394274042741427424274342744427454274642747427484274942750427514275242753427544275542756427574275842759427604276142762427634276442765427664276742768427694277042771427724277342774427754277642777427784277942780427814278242783427844278542786427874278842789427904279142792427934279442795427964279742798427994280042801428024280342804428054280642807428084280942810428114281242813428144281542816428174281842819428204282142822428234282442825428264282742828428294283042831428324283342834428354283642837428384283942840428414284242843428444284542846428474284842849428504285142852428534285442855428564285742858428594286042861428624286342864428654286642867428684286942870428714287242873428744287542876428774287842879428804288142882428834288442885428864288742888428894289042891428924289342894428954289642897428984289942900429014290242903429044290542906429074290842909429104291142912429134291442915429164291742918429194292042921429224292342924429254292642927429284292942930429314293242933429344293542936429374293842939429404294142942429434294442945429464294742948429494295042951429524295342954429554295642957429584295942960429614296242963429644296542966429674296842969429704297142972429734297442975429764297742978429794298042981429824298342984429854298642987429884298942990429914299242993429944299542996429974299842999430004300143002430034300443005430064300743008430094301043011430124301343014430154301643017430184301943020430214302243023430244302543026430274302843029430304303143032430334303443035430364303743038430394304043041430424304343044430454304643047430484304943050430514305243053430544305543056430574305843059430604306143062430634306443065430664306743068430694307043071430724307343074430754307643077430784307943080430814308243083430844308543086430874308843089430904309143092430934309443095430964309743098430994310043101431024310343104431054310643107431084310943110431114311243113431144311543116431174311843119431204312143122431234312443125431264312743128431294313043131431324313343134431354313643137431384313943140431414314243143431444314543146431474314843149431504315143152431534315443155431564315743158431594316043161431624316343164431654316643167431684316943170431714317243173431744317543176431774317843179431804318143182431834318443185431864318743188431894319043191431924319343194431954319643197431984319943200432014320243203432044320543206432074320843209432104321143212432134321443215432164321743218432194322043221432224322343224432254322643227432284322943230432314323243233432344323543236432374323843239432404324143242432434324443245432464324743248432494325043251432524325343254432554325643257432584325943260432614326243263432644326543266432674326843269432704327143272432734327443275432764327743278432794328043281432824328343284432854328643287432884328943290432914329243293432944329543296432974329843299433004330143302433034330443305433064330743308433094331043311433124331343314433154331643317433184331943320433214332243323433244332543326433274332843329433304333143332433334333443335433364333743338433394334043341433424334343344433454334643347433484334943350433514335243353433544335543356433574335843359433604336143362433634336443365433664336743368433694337043371433724337343374433754337643377433784337943380433814338243383433844338543386433874338843389433904339143392433934339443395433964339743398433994340043401434024340343404434054340643407434084340943410434114341243413434144341543416434174341843419434204342143422434234342443425434264342743428434294343043431434324343343434434354343643437434384343943440434414344243443434444344543446434474344843449434504345143452434534345443455434564345743458434594346043461434624346343464434654346643467434684346943470434714347243473434744347543476434774347843479434804348143482434834348443485434864348743488434894349043491434924349343494434954349643497434984349943500435014350243503435044350543506435074350843509435104351143512435134351443515435164351743518435194352043521435224352343524435254352643527435284352943530435314353243533435344353543536435374353843539435404354143542435434354443545435464354743548435494355043551435524355343554435554355643557435584355943560435614356243563435644356543566435674356843569435704357143572435734357443575435764357743578435794358043581435824358343584435854358643587435884358943590435914359243593435944359543596435974359843599436004360143602436034360443605436064360743608436094361043611436124361343614436154361643617436184361943620436214362243623436244362543626436274362843629436304363143632436334363443635436364363743638436394364043641436424364343644436454364643647436484364943650436514365243653436544365543656436574365843659436604366143662436634366443665436664366743668436694367043671436724367343674436754367643677436784367943680436814368243683436844368543686436874368843689436904369143692436934369443695436964369743698436994370043701437024370343704437054370643707437084370943710437114371243713437144371543716437174371843719437204372143722437234372443725437264372743728437294373043731437324373343734437354373643737437384373943740437414374243743437444374543746437474374843749437504375143752437534375443755437564375743758437594376043761437624376343764437654376643767437684376943770437714377243773437744377543776437774377843779437804378143782437834378443785437864378743788437894379043791437924379343794437954379643797437984379943800438014380243803438044380543806438074380843809438104381143812438134381443815438164381743818438194382043821438224382343824438254382643827438284382943830438314383243833438344383543836438374383843839438404384143842438434384443845438464384743848438494385043851438524385343854438554385643857438584385943860438614386243863438644386543866438674386843869438704387143872438734387443875438764387743878438794388043881438824388343884438854388643887438884388943890438914389243893438944389543896438974389843899439004390143902439034390443905439064390743908439094391043911439124391343914439154391643917439184391943920439214392243923439244392543926439274392843929439304393143932439334393443935439364393743938439394394043941439424394343944439454394643947439484394943950439514395243953439544395543956439574395843959439604396143962439634396443965439664396743968439694397043971439724397343974439754397643977439784397943980439814398243983439844398543986439874398843989439904399143992439934399443995439964399743998439994400044001440024400344004440054400644007440084400944010440114401244013440144401544016440174401844019440204402144022440234402444025440264402744028440294403044031440324403344034440354403644037440384403944040440414404244043440444404544046440474404844049440504405144052440534405444055440564405744058440594406044061440624406344064440654406644067440684406944070440714407244073440744407544076440774407844079440804408144082440834408444085440864408744088440894409044091440924409344094440954409644097440984409944100441014410244103441044410544106441074410844109441104411144112441134411444115441164411744118441194412044121441224412344124441254412644127441284412944130441314413244133441344413544136441374413844139441404414144142441434414444145441464414744148441494415044151441524415344154441554415644157441584415944160441614416244163441644416544166441674416844169441704417144172441734417444175441764417744178441794418044181441824418344184441854418644187441884418944190441914419244193441944419544196441974419844199442004420144202442034420444205442064420744208442094421044211442124421344214442154421644217442184421944220442214422244223442244422544226442274422844229442304423144232442334423444235442364423744238442394424044241442424424344244442454424644247442484424944250442514425244253442544425544256442574425844259442604426144262442634426444265442664426744268442694427044271442724427344274442754427644277442784427944280442814428244283442844428544286442874428844289442904429144292442934429444295442964429744298442994430044301443024430344304443054430644307443084430944310443114431244313443144431544316443174431844319443204432144322443234432444325443264432744328443294433044331443324433344334443354433644337443384433944340443414434244343443444434544346443474434844349443504435144352443534435444355443564435744358443594436044361443624436344364443654436644367443684436944370443714437244373443744437544376443774437844379443804438144382443834438444385443864438744388443894439044391443924439344394443954439644397443984439944400444014440244403444044440544406444074440844409444104441144412444134441444415444164441744418444194442044421444224442344424444254442644427444284442944430444314443244433444344443544436444374443844439444404444144442444434444444445444464444744448444494445044451444524445344454444554445644457444584445944460444614446244463444644446544466444674446844469444704447144472444734447444475444764447744478444794448044481444824448344484444854448644487444884448944490444914449244493444944449544496444974449844499445004450144502445034450444505445064450744508445094451044511445124451344514445154451644517445184451944520445214452244523445244452544526445274452844529445304453144532445334453444535445364453744538445394454044541445424454344544445454454644547445484454944550445514455244553445544455544556445574455844559445604456144562445634456444565445664456744568445694457044571445724457344574445754457644577445784457944580445814458244583445844458544586445874458844589445904459144592445934459444595445964459744598445994460044601446024460344604446054460644607446084460944610446114461244613446144461544616446174461844619446204462144622446234462444625446264462744628446294463044631446324463344634446354463644637446384463944640446414464244643446444464544646446474464844649446504465144652446534465444655446564465744658446594466044661446624466344664446654466644667446684466944670446714467244673446744467544676446774467844679446804468144682446834468444685446864468744688446894469044691446924469344694446954469644697446984469944700447014470244703447044470544706447074470844709447104471144712447134471444715447164471744718447194472044721447224472344724447254472644727447284472944730447314473244733447344473544736447374473844739447404474144742447434474444745447464474744748447494475044751447524475344754447554475644757447584475944760447614476244763447644476544766447674476844769447704477144772447734477444775447764477744778447794478044781447824478344784447854478644787447884478944790447914479244793447944479544796447974479844799448004480144802448034480444805448064480744808448094481044811448124481344814448154481644817448184481944820448214482244823448244482544826448274482844829448304483144832448334483444835448364483744838448394484044841448424484344844448454484644847448484484944850448514485244853448544485544856448574485844859448604486144862448634486444865448664486744868448694487044871448724487344874448754487644877448784487944880448814488244883448844488544886448874488844889448904489144892448934489444895448964489744898448994490044901449024490344904449054490644907449084490944910449114491244913449144491544916449174491844919449204492144922449234492444925449264492744928449294493044931449324493344934449354493644937449384493944940449414494244943449444494544946449474494844949449504495144952449534495444955449564495744958449594496044961449624496344964449654496644967449684496944970449714497244973449744497544976449774497844979449804498144982449834498444985449864498744988449894499044991449924499344994449954499644997449984499945000450014500245003450044500545006450074500845009450104501145012450134501445015450164501745018450194502045021450224502345024450254502645027450284502945030450314503245033450344503545036450374503845039450404504145042450434504445045450464504745048450494505045051450524505345054450554505645057450584505945060450614506245063450644506545066450674506845069450704507145072450734507445075450764507745078450794508045081450824508345084450854508645087450884508945090450914509245093450944509545096450974509845099451004510145102451034510445105451064510745108451094511045111451124511345114451154511645117451184511945120451214512245123451244512545126451274512845129451304513145132451334513445135451364513745138451394514045141451424514345144451454514645147451484514945150451514515245153451544515545156451574515845159451604516145162451634516445165451664516745168451694517045171451724517345174451754517645177451784517945180451814518245183451844518545186451874518845189451904519145192451934519445195451964519745198451994520045201452024520345204452054520645207452084520945210452114521245213452144521545216452174521845219452204522145222452234522445225452264522745228452294523045231452324523345234452354523645237452384523945240452414524245243452444524545246452474524845249452504525145252452534525445255452564525745258452594526045261452624526345264452654526645267452684526945270452714527245273452744527545276452774527845279452804528145282452834528445285452864528745288452894529045291452924529345294452954529645297452984529945300453014530245303453044530545306453074530845309453104531145312453134531445315453164531745318453194532045321453224532345324453254532645327453284532945330453314533245333453344533545336453374533845339453404534145342453434534445345453464534745348453494535045351453524535345354453554535645357453584535945360453614536245363453644536545366453674536845369453704537145372453734537445375453764537745378453794538045381453824538345384453854538645387453884538945390453914539245393453944539545396453974539845399454004540145402454034540445405454064540745408454094541045411454124541345414454154541645417454184541945420454214542245423454244542545426454274542845429454304543145432454334543445435454364543745438454394544045441454424544345444454454544645447454484544945450454514545245453454544545545456454574545845459454604546145462454634546445465454664546745468454694547045471454724547345474454754547645477454784547945480454814548245483454844548545486454874548845489454904549145492454934549445495454964549745498454994550045501455024550345504455054550645507455084550945510455114551245513455144551545516455174551845519455204552145522455234552445525455264552745528455294553045531455324553345534455354553645537455384553945540455414554245543455444554545546455474554845549455504555145552455534555445555455564555745558455594556045561455624556345564455654556645567455684556945570455714557245573455744557545576455774557845579455804558145582455834558445585455864558745588455894559045591455924559345594455954559645597455984559945600456014560245603456044560545606456074560845609456104561145612456134561445615456164561745618456194562045621456224562345624456254562645627456284562945630456314563245633456344563545636456374563845639456404564145642456434564445645456464564745648456494565045651456524565345654456554565645657456584565945660456614566245663456644566545666456674566845669456704567145672456734567445675456764567745678456794568045681456824568345684456854568645687456884568945690456914569245693456944569545696456974569845699457004570145702457034570445705457064570745708457094571045711457124571345714457154571645717457184571945720457214572245723457244572545726457274572845729457304573145732457334573445735457364573745738457394574045741457424574345744457454574645747457484574945750457514575245753457544575545756457574575845759457604576145762457634576445765457664576745768457694577045771457724577345774457754577645777457784577945780457814578245783457844578545786457874578845789457904579145792457934579445795457964579745798457994580045801458024580345804458054580645807458084580945810458114581245813458144581545816458174581845819458204582145822458234582445825458264582745828458294583045831458324583345834458354583645837458384583945840458414584245843458444584545846458474584845849458504585145852458534585445855458564585745858458594586045861458624586345864458654586645867458684586945870458714587245873458744587545876458774587845879458804588145882458834588445885458864588745888458894589045891458924589345894458954589645897458984589945900459014590245903459044590545906459074590845909459104591145912459134591445915459164591745918459194592045921459224592345924459254592645927459284592945930459314593245933459344593545936459374593845939459404594145942459434594445945459464594745948459494595045951459524595345954459554595645957459584595945960459614596245963459644596545966459674596845969459704597145972459734597445975459764597745978459794598045981459824598345984459854598645987459884598945990459914599245993459944599545996459974599845999460004600146002460034600446005460064600746008460094601046011460124601346014460154601646017460184601946020460214602246023460244602546026460274602846029460304603146032460334603446035460364603746038460394604046041460424604346044460454604646047460484604946050460514605246053460544605546056460574605846059460604606146062460634606446065460664606746068460694607046071460724607346074460754607646077460784607946080460814608246083460844608546086460874608846089460904609146092460934609446095460964609746098460994610046101461024610346104461054610646107461084610946110461114611246113461144611546116461174611846119461204612146122461234612446125461264612746128461294613046131461324613346134461354613646137461384613946140461414614246143461444614546146461474614846149461504615146152461534615446155461564615746158461594616046161461624616346164461654616646167461684616946170461714617246173461744617546176461774617846179461804618146182461834618446185461864618746188461894619046191461924619346194461954619646197461984619946200462014620246203462044620546206462074620846209462104621146212462134621446215462164621746218462194622046221462224622346224462254622646227462284622946230462314623246233462344623546236462374623846239462404624146242462434624446245462464624746248462494625046251462524625346254462554625646257462584625946260462614626246263462644626546266462674626846269462704627146272462734627446275462764627746278462794628046281462824628346284462854628646287462884628946290462914629246293462944629546296462974629846299463004630146302463034630446305463064630746308463094631046311463124631346314463154631646317463184631946320463214632246323463244632546326463274632846329463304633146332463334633446335463364633746338463394634046341463424634346344463454634646347463484634946350463514635246353463544635546356463574635846359463604636146362463634636446365463664636746368463694637046371463724637346374463754637646377463784637946380463814638246383463844638546386463874638846389463904639146392463934639446395463964639746398463994640046401464024640346404464054640646407464084640946410464114641246413464144641546416464174641846419464204642146422464234642446425464264642746428464294643046431464324643346434464354643646437464384643946440464414644246443464444644546446464474644846449464504645146452464534645446455464564645746458464594646046461464624646346464464654646646467464684646946470464714647246473464744647546476464774647846479464804648146482464834648446485464864648746488464894649046491464924649346494464954649646497464984649946500465014650246503465044650546506465074650846509465104651146512465134651446515465164651746518465194652046521465224652346524465254652646527465284652946530465314653246533465344653546536465374653846539465404654146542465434654446545465464654746548465494655046551465524655346554465554655646557465584655946560465614656246563465644656546566465674656846569465704657146572465734657446575465764657746578465794658046581465824658346584465854658646587465884658946590465914659246593465944659546596465974659846599466004660146602466034660446605466064660746608466094661046611466124661346614466154661646617466184661946620466214662246623466244662546626466274662846629466304663146632466334663446635466364663746638466394664046641466424664346644466454664646647466484664946650466514665246653466544665546656466574665846659466604666146662466634666446665466664666746668466694667046671466724667346674466754667646677466784667946680466814668246683466844668546686466874668846689466904669146692466934669446695466964669746698466994670046701467024670346704467054670646707467084670946710467114671246713467144671546716467174671846719467204672146722467234672446725467264672746728467294673046731467324673346734467354673646737467384673946740467414674246743467444674546746467474674846749467504675146752467534675446755467564675746758467594676046761467624676346764467654676646767467684676946770467714677246773467744677546776467774677846779467804678146782467834678446785467864678746788467894679046791467924679346794467954679646797467984679946800468014680246803468044680546806468074680846809468104681146812468134681446815468164681746818468194682046821468224682346824468254682646827468284682946830468314683246833468344683546836468374683846839468404684146842468434684446845468464684746848468494685046851468524685346854468554685646857468584685946860468614686246863468644686546866468674686846869468704687146872468734687446875468764687746878468794688046881468824688346884468854688646887468884688946890468914689246893468944689546896468974689846899469004690146902469034690446905469064690746908469094691046911469124691346914469154691646917469184691946920469214692246923469244692546926469274692846929469304693146932469334693446935469364693746938469394694046941469424694346944469454694646947469484694946950469514695246953469544695546956469574695846959469604696146962469634696446965469664696746968469694697046971469724697346974469754697646977469784697946980469814698246983469844698546986469874698846989469904699146992469934699446995469964699746998469994700047001470024700347004470054700647007470084700947010470114701247013470144701547016470174701847019470204702147022470234702447025470264702747028470294703047031470324703347034470354703647037470384703947040470414704247043470444704547046470474704847049470504705147052470534705447055470564705747058470594706047061470624706347064470654706647067470684706947070470714707247073470744707547076470774707847079470804708147082470834708447085470864708747088470894709047091470924709347094470954709647097470984709947100471014710247103471044710547106471074710847109471104711147112471134711447115471164711747118471194712047121471224712347124471254712647127471284712947130471314713247133471344713547136471374713847139471404714147142471434714447145471464714747148471494715047151471524715347154471554715647157471584715947160471614716247163471644716547166471674716847169471704717147172471734717447175471764717747178471794718047181471824718347184471854718647187471884718947190471914719247193471944719547196471974719847199472004720147202472034720447205472064720747208472094721047211472124721347214472154721647217472184721947220472214722247223472244722547226472274722847229472304723147232472334723447235472364723747238472394724047241472424724347244472454724647247472484724947250472514725247253472544725547256472574725847259472604726147262472634726447265472664726747268472694727047271472724727347274472754727647277472784727947280472814728247283472844728547286472874728847289472904729147292472934729447295472964729747298472994730047301473024730347304473054730647307473084730947310473114731247313473144731547316473174731847319473204732147322473234732447325473264732747328473294733047331473324733347334473354733647337473384733947340473414734247343473444734547346473474734847349473504735147352473534735447355473564735747358473594736047361473624736347364473654736647367473684736947370473714737247373473744737547376473774737847379473804738147382473834738447385473864738747388473894739047391473924739347394473954739647397473984739947400474014740247403474044740547406474074740847409474104741147412474134741447415474164741747418474194742047421474224742347424474254742647427474284742947430474314743247433474344743547436474374743847439474404744147442474434744447445474464744747448474494745047451474524745347454474554745647457474584745947460474614746247463474644746547466474674746847469474704747147472474734747447475474764747747478474794748047481474824748347484474854748647487474884748947490474914749247493474944749547496474974749847499475004750147502475034750447505475064750747508475094751047511475124751347514475154751647517475184751947520475214752247523475244752547526475274752847529475304753147532475334753447535475364753747538475394754047541475424754347544475454754647547475484754947550475514755247553475544755547556475574755847559475604756147562475634756447565475664756747568475694757047571475724757347574475754757647577475784757947580475814758247583475844758547586475874758847589475904759147592475934759447595475964759747598475994760047601476024760347604476054760647607476084760947610476114761247613476144761547616476174761847619476204762147622476234762447625476264762747628476294763047631476324763347634476354763647637476384763947640476414764247643476444764547646476474764847649476504765147652476534765447655476564765747658476594766047661476624766347664476654766647667476684766947670476714767247673476744767547676476774767847679476804768147682476834768447685476864768747688476894769047691476924769347694476954769647697476984769947700477014770247703477044770547706477074770847709477104771147712477134771447715477164771747718477194772047721477224772347724477254772647727477284772947730477314773247733477344773547736477374773847739477404774147742477434774447745477464774747748477494775047751477524775347754477554775647757477584775947760477614776247763477644776547766477674776847769477704777147772477734777447775477764777747778477794778047781477824778347784477854778647787477884778947790477914779247793477944779547796477974779847799478004780147802478034780447805478064780747808478094781047811478124781347814478154781647817478184781947820478214782247823478244782547826478274782847829478304783147832478334783447835478364783747838478394784047841478424784347844478454784647847478484784947850478514785247853478544785547856478574785847859478604786147862478634786447865478664786747868478694787047871478724787347874478754787647877478784787947880478814788247883478844788547886478874788847889478904789147892478934789447895478964789747898478994790047901479024790347904479054790647907479084790947910479114791247913479144791547916479174791847919479204792147922479234792447925479264792747928479294793047931479324793347934479354793647937479384793947940479414794247943479444794547946479474794847949479504795147952479534795447955479564795747958479594796047961479624796347964479654796647967479684796947970479714797247973479744797547976479774797847979479804798147982479834798447985479864798747988479894799047991479924799347994479954799647997479984799948000480014800248003480044800548006480074800848009480104801148012480134801448015480164801748018480194802048021480224802348024480254802648027480284802948030480314803248033480344803548036480374803848039480404804148042480434804448045480464804748048480494805048051480524805348054480554805648057480584805948060480614806248063480644806548066480674806848069480704807148072480734807448075480764807748078480794808048081480824808348084480854808648087480884808948090480914809248093480944809548096480974809848099481004810148102481034810448105481064810748108481094811048111481124811348114481154811648117481184811948120481214812248123481244812548126481274812848129481304813148132481334813448135481364813748138481394814048141481424814348144481454814648147481484814948150481514815248153481544815548156481574815848159481604816148162481634816448165481664816748168481694817048171481724817348174481754817648177481784817948180481814818248183481844818548186481874818848189481904819148192481934819448195481964819748198481994820048201482024820348204482054820648207482084820948210482114821248213482144821548216482174821848219482204822148222482234822448225482264822748228482294823048231482324823348234482354823648237482384823948240482414824248243482444824548246482474824848249482504825148252482534825448255482564825748258482594826048261482624826348264482654826648267482684826948270482714827248273482744827548276482774827848279482804828148282482834828448285482864828748288482894829048291482924829348294482954829648297482984829948300483014830248303483044830548306483074830848309483104831148312483134831448315483164831748318483194832048321483224832348324483254832648327483284832948330483314833248333483344833548336483374833848339483404834148342483434834448345483464834748348483494835048351483524835348354483554835648357483584835948360483614836248363483644836548366483674836848369483704837148372483734837448375483764837748378483794838048381483824838348384483854838648387483884838948390483914839248393483944839548396483974839848399484004840148402484034840448405484064840748408484094841048411484124841348414484154841648417484184841948420484214842248423484244842548426484274842848429484304843148432484334843448435484364843748438484394844048441484424844348444484454844648447484484844948450484514845248453484544845548456484574845848459484604846148462484634846448465484664846748468484694847048471484724847348474484754847648477484784847948480484814848248483484844848548486484874848848489484904849148492484934849448495484964849748498484994850048501485024850348504485054850648507485084850948510485114851248513485144851548516485174851848519485204852148522485234852448525485264852748528485294853048531485324853348534485354853648537485384853948540485414854248543485444854548546485474854848549485504855148552485534855448555485564855748558485594856048561485624856348564485654856648567485684856948570485714857248573485744857548576485774857848579485804858148582485834858448585485864858748588485894859048591485924859348594485954859648597485984859948600486014860248603486044860548606486074860848609486104861148612486134861448615486164861748618486194862048621486224862348624486254862648627486284862948630486314863248633486344863548636486374863848639486404864148642486434864448645486464864748648486494865048651486524865348654486554865648657486584865948660486614866248663486644866548666486674866848669486704867148672486734867448675486764867748678486794868048681486824868348684486854868648687486884868948690486914869248693486944869548696486974869848699487004870148702487034870448705487064870748708487094871048711487124871348714487154871648717487184871948720487214872248723487244872548726487274872848729487304873148732487334873448735487364873748738487394874048741487424874348744487454874648747487484874948750487514875248753487544875548756487574875848759487604876148762487634876448765487664876748768487694877048771487724877348774487754877648777487784877948780487814878248783487844878548786487874878848789487904879148792487934879448795487964879748798487994880048801488024880348804488054880648807488084880948810488114881248813488144881548816488174881848819488204882148822488234882448825488264882748828488294883048831488324883348834488354883648837488384883948840488414884248843488444884548846488474884848849488504885148852488534885448855488564885748858488594886048861488624886348864488654886648867488684886948870488714887248873488744887548876488774887848879488804888148882488834888448885488864888748888488894889048891488924889348894488954889648897488984889948900489014890248903489044890548906489074890848909489104891148912489134891448915489164891748918489194892048921489224892348924489254892648927489284892948930489314893248933489344893548936489374893848939489404894148942489434894448945489464894748948489494895048951489524895348954489554895648957489584895948960489614896248963489644896548966489674896848969489704897148972489734897448975489764897748978489794898048981489824898348984489854898648987489884898948990489914899248993489944899548996489974899848999490004900149002490034900449005490064900749008490094901049011490124901349014490154901649017490184901949020490214902249023490244902549026490274902849029490304903149032490334903449035490364903749038490394904049041490424904349044490454904649047490484904949050490514905249053490544905549056490574905849059490604906149062490634906449065490664906749068490694907049071490724907349074490754907649077490784907949080490814908249083490844908549086490874908849089490904909149092490934909449095490964909749098490994910049101491024910349104491054910649107491084910949110491114911249113491144911549116491174911849119491204912149122491234912449125491264912749128491294913049131491324913349134491354913649137491384913949140491414914249143491444914549146491474914849149491504915149152491534915449155491564915749158491594916049161491624916349164491654916649167491684916949170491714917249173491744917549176491774917849179491804918149182491834918449185491864918749188491894919049191491924919349194491954919649197491984919949200492014920249203492044920549206492074920849209492104921149212492134921449215492164921749218492194922049221492224922349224492254922649227492284922949230492314923249233492344923549236492374923849239492404924149242492434924449245492464924749248492494925049251492524925349254492554925649257492584925949260492614926249263492644926549266492674926849269492704927149272492734927449275492764927749278492794928049281492824928349284492854928649287492884928949290492914929249293492944929549296492974929849299493004930149302493034930449305493064930749308493094931049311493124931349314493154931649317493184931949320493214932249323493244932549326493274932849329493304933149332493334933449335493364933749338493394934049341493424934349344493454934649347493484934949350493514935249353493544935549356493574935849359493604936149362493634936449365493664936749368493694937049371493724937349374493754937649377493784937949380493814938249383493844938549386493874938849389493904939149392493934939449395493964939749398493994940049401494024940349404494054940649407494084940949410494114941249413494144941549416494174941849419494204942149422494234942449425494264942749428494294943049431494324943349434494354943649437494384943949440494414944249443494444944549446494474944849449494504945149452494534945449455494564945749458494594946049461494624946349464494654946649467494684946949470494714947249473494744947549476494774947849479494804948149482494834948449485494864948749488494894949049491494924949349494494954949649497494984949949500495014950249503495044950549506495074950849509495104951149512495134951449515495164951749518495194952049521495224952349524495254952649527495284952949530495314953249533495344953549536495374953849539495404954149542495434954449545495464954749548495494955049551495524955349554495554955649557495584955949560495614956249563495644956549566495674956849569495704957149572495734957449575495764957749578495794958049581495824958349584495854958649587495884958949590495914959249593495944959549596495974959849599496004960149602496034960449605496064960749608496094961049611496124961349614496154961649617496184961949620496214962249623496244962549626496274962849629496304963149632496334963449635496364963749638496394964049641496424964349644496454964649647496484964949650496514965249653496544965549656496574965849659496604966149662496634966449665496664966749668496694967049671496724967349674496754967649677496784967949680496814968249683496844968549686496874968849689496904969149692496934969449695496964969749698496994970049701497024970349704497054970649707497084970949710497114971249713497144971549716497174971849719497204972149722497234972449725497264972749728497294973049731497324973349734497354973649737497384973949740497414974249743497444974549746497474974849749497504975149752497534975449755497564975749758497594976049761497624976349764497654976649767497684976949770497714977249773497744977549776497774977849779497804978149782497834978449785497864978749788497894979049791497924979349794497954979649797497984979949800498014980249803498044980549806498074980849809498104981149812498134981449815498164981749818498194982049821498224982349824498254982649827498284982949830498314983249833498344983549836498374983849839498404984149842498434984449845498464984749848498494985049851498524985349854498554985649857498584985949860498614986249863498644986549866498674986849869498704987149872498734987449875498764987749878498794988049881498824988349884498854988649887498884988949890498914989249893498944989549896498974989849899499004990149902499034990449905499064990749908499094991049911499124991349914499154991649917499184991949920499214992249923499244992549926499274992849929499304993149932499334993449935499364993749938499394994049941499424994349944499454994649947499484994949950499514995249953499544995549956499574995849959499604996149962499634996449965499664996749968499694997049971499724997349974499754997649977499784997949980499814998249983499844998549986499874998849989499904999149992499934999449995499964999749998499995000050001500025000350004500055000650007500085000950010500115001250013500145001550016500175001850019500205002150022500235002450025500265002750028500295003050031500325003350034500355003650037500385003950040500415004250043500445004550046500475004850049500505005150052500535005450055500565005750058500595006050061500625006350064500655006650067500685006950070500715007250073500745007550076500775007850079500805008150082500835008450085500865008750088500895009050091500925009350094500955009650097500985009950100501015010250103501045010550106501075010850109501105011150112501135011450115501165011750118501195012050121501225012350124501255012650127501285012950130501315013250133501345013550136501375013850139501405014150142501435014450145501465014750148501495015050151501525015350154501555015650157501585015950160501615016250163501645016550166501675016850169501705017150172501735017450175501765017750178501795018050181501825018350184501855018650187501885018950190501915019250193501945019550196501975019850199502005020150202502035020450205502065020750208502095021050211502125021350214502155021650217502185021950220502215022250223502245022550226502275022850229502305023150232502335023450235502365023750238502395024050241502425024350244502455024650247502485024950250502515025250253502545025550256502575025850259502605026150262502635026450265502665026750268502695027050271502725027350274502755027650277502785027950280502815028250283502845028550286502875028850289502905029150292502935029450295502965029750298502995030050301503025030350304503055030650307503085030950310503115031250313503145031550316503175031850319503205032150322503235032450325503265032750328503295033050331503325033350334503355033650337503385033950340503415034250343503445034550346503475034850349503505035150352503535035450355503565035750358503595036050361503625036350364503655036650367503685036950370503715037250373503745037550376503775037850379503805038150382503835038450385503865038750388503895039050391503925039350394503955039650397503985039950400504015040250403504045040550406504075040850409504105041150412504135041450415504165041750418504195042050421504225042350424504255042650427504285042950430504315043250433504345043550436504375043850439504405044150442504435044450445504465044750448504495045050451504525045350454504555045650457504585045950460504615046250463504645046550466504675046850469504705047150472504735047450475504765047750478504795048050481504825048350484504855048650487504885048950490504915049250493504945049550496504975049850499505005050150502505035050450505505065050750508505095051050511505125051350514505155051650517505185051950520505215052250523505245052550526505275052850529505305053150532505335053450535505365053750538505395054050541505425054350544505455054650547505485054950550505515055250553505545055550556505575055850559505605056150562505635056450565505665056750568505695057050571505725057350574505755057650577505785057950580505815058250583505845058550586505875058850589505905059150592505935059450595505965059750598505995060050601506025060350604506055060650607506085060950610506115061250613506145061550616506175061850619506205062150622506235062450625506265062750628506295063050631506325063350634506355063650637506385063950640506415064250643506445064550646506475064850649506505065150652506535065450655506565065750658506595066050661506625066350664506655066650667506685066950670506715067250673506745067550676506775067850679506805068150682506835068450685506865068750688506895069050691506925069350694506955069650697506985069950700507015070250703507045070550706507075070850709507105071150712507135071450715507165071750718507195072050721507225072350724507255072650727507285072950730507315073250733507345073550736507375073850739507405074150742507435074450745507465074750748507495075050751507525075350754507555075650757507585075950760507615076250763507645076550766507675076850769507705077150772507735077450775507765077750778507795078050781507825078350784507855078650787507885078950790507915079250793507945079550796507975079850799508005080150802508035080450805508065080750808508095081050811508125081350814508155081650817508185081950820508215082250823508245082550826508275082850829508305083150832508335083450835508365083750838508395084050841508425084350844508455084650847508485084950850508515085250853508545085550856508575085850859508605086150862508635086450865508665086750868508695087050871508725087350874508755087650877508785087950880508815088250883508845088550886508875088850889508905089150892508935089450895508965089750898508995090050901509025090350904509055090650907509085090950910509115091250913509145091550916509175091850919509205092150922509235092450925509265092750928509295093050931509325093350934509355093650937509385093950940509415094250943509445094550946509475094850949509505095150952509535095450955509565095750958509595096050961509625096350964509655096650967509685096950970509715097250973509745097550976509775097850979509805098150982509835098450985509865098750988509895099050991509925099350994509955099650997509985099951000510015100251003510045100551006510075100851009510105101151012510135101451015510165101751018510195102051021510225102351024510255102651027510285102951030510315103251033510345103551036510375103851039510405104151042510435104451045510465104751048510495105051051510525105351054510555105651057510585105951060510615106251063510645106551066510675106851069510705107151072510735107451075510765107751078510795108051081510825108351084510855108651087510885108951090510915109251093510945109551096510975109851099511005110151102511035110451105511065110751108511095111051111511125111351114511155111651117511185111951120511215112251123511245112551126511275112851129511305113151132511335113451135511365113751138511395114051141511425114351144511455114651147511485114951150511515115251153511545115551156511575115851159511605116151162511635116451165511665116751168511695117051171511725117351174511755117651177511785117951180511815118251183511845118551186511875118851189511905119151192511935119451195511965119751198511995120051201512025120351204512055120651207512085120951210512115121251213512145121551216512175121851219512205122151222512235122451225512265122751228512295123051231512325123351234512355123651237512385123951240512415124251243512445124551246512475124851249512505125151252512535125451255512565125751258512595126051261512625126351264512655126651267512685126951270512715127251273512745127551276512775127851279512805128151282512835128451285512865128751288512895129051291512925129351294512955129651297512985129951300513015130251303513045130551306513075130851309513105131151312513135131451315513165131751318513195132051321513225132351324513255132651327513285132951330513315133251333513345133551336513375133851339513405134151342513435134451345513465134751348513495135051351513525135351354513555135651357513585135951360513615136251363513645136551366513675136851369513705137151372513735137451375513765137751378513795138051381513825138351384513855138651387513885138951390513915139251393513945139551396513975139851399514005140151402514035140451405514065140751408514095141051411514125141351414514155141651417514185141951420514215142251423514245142551426514275142851429514305143151432514335143451435514365143751438514395144051441514425144351444514455144651447514485144951450514515145251453514545145551456514575145851459514605146151462514635146451465514665146751468514695147051471514725147351474514755147651477514785147951480514815148251483514845148551486514875148851489514905149151492514935149451495514965149751498514995150051501515025150351504515055150651507515085150951510515115151251513515145151551516515175151851519515205152151522515235152451525515265152751528515295153051531515325153351534515355153651537515385153951540515415154251543515445154551546515475154851549515505155151552515535155451555515565155751558515595156051561515625156351564515655156651567515685156951570515715157251573515745157551576515775157851579515805158151582515835158451585515865158751588515895159051591515925159351594515955159651597515985159951600516015160251603516045160551606516075160851609516105161151612516135161451615516165161751618516195162051621516225162351624516255162651627516285162951630516315163251633516345163551636516375163851639516405164151642516435164451645516465164751648516495165051651516525165351654516555165651657516585165951660516615166251663516645166551666516675166851669516705167151672516735167451675516765167751678516795168051681516825168351684516855168651687516885168951690516915169251693516945169551696516975169851699517005170151702517035170451705517065170751708517095171051711517125171351714517155171651717517185171951720517215172251723517245172551726517275172851729517305173151732517335173451735517365173751738517395174051741517425174351744517455174651747517485174951750517515175251753517545175551756517575175851759517605176151762517635176451765517665176751768517695177051771517725177351774517755177651777517785177951780517815178251783517845178551786517875178851789517905179151792517935179451795517965179751798517995180051801518025180351804518055180651807518085180951810518115181251813518145181551816518175181851819518205182151822518235182451825518265182751828518295183051831518325183351834518355183651837518385183951840518415184251843518445184551846518475184851849518505185151852518535185451855518565185751858518595186051861518625186351864518655186651867518685186951870518715187251873518745187551876518775187851879518805188151882518835188451885518865188751888518895189051891518925189351894518955189651897518985189951900519015190251903519045190551906519075190851909519105191151912519135191451915519165191751918519195192051921519225192351924519255192651927519285192951930519315193251933519345193551936519375193851939519405194151942519435194451945519465194751948519495195051951519525195351954519555195651957519585195951960519615196251963519645196551966519675196851969519705197151972519735197451975519765197751978519795198051981519825198351984519855198651987519885198951990519915199251993519945199551996519975199851999520005200152002520035200452005520065200752008520095201052011520125201352014520155201652017520185201952020520215202252023520245202552026520275202852029520305203152032520335203452035520365203752038520395204052041520425204352044520455204652047520485204952050520515205252053520545205552056520575205852059520605206152062520635206452065520665206752068520695207052071520725207352074520755207652077520785207952080520815208252083520845208552086520875208852089520905209152092520935209452095520965209752098520995210052101521025210352104521055210652107521085210952110521115211252113521145211552116521175211852119521205212152122521235212452125521265212752128521295213052131521325213352134521355213652137521385213952140521415214252143521445214552146521475214852149521505215152152521535215452155521565215752158521595216052161521625216352164521655216652167521685216952170521715217252173521745217552176521775217852179521805218152182521835218452185521865218752188521895219052191521925219352194521955219652197521985219952200522015220252203522045220552206522075220852209522105221152212522135221452215522165221752218522195222052221522225222352224522255222652227522285222952230522315223252233522345223552236522375223852239522405224152242522435224452245522465224752248522495225052251522525225352254522555225652257522585225952260522615226252263522645226552266522675226852269522705227152272522735227452275522765227752278522795228052281522825228352284522855228652287522885228952290522915229252293522945229552296522975229852299523005230152302523035230452305523065230752308523095231052311523125231352314523155231652317523185231952320523215232252323523245232552326523275232852329523305233152332523335233452335523365233752338523395234052341523425234352344523455234652347523485234952350523515235252353523545235552356523575235852359523605236152362523635236452365523665236752368523695237052371523725237352374523755237652377523785237952380523815238252383523845238552386523875238852389523905239152392523935239452395523965239752398523995240052401524025240352404524055240652407524085240952410524115241252413524145241552416524175241852419524205242152422524235242452425524265242752428524295243052431524325243352434524355243652437524385243952440524415244252443524445244552446524475244852449524505245152452524535245452455524565245752458524595246052461524625246352464524655246652467524685246952470524715247252473524745247552476524775247852479524805248152482524835248452485524865248752488524895249052491524925249352494524955249652497524985249952500525015250252503525045250552506525075250852509525105251152512525135251452515525165251752518525195252052521525225252352524525255252652527525285252952530525315253252533525345253552536525375253852539525405254152542525435254452545525465254752548525495255052551525525255352554525555255652557525585255952560525615256252563525645256552566525675256852569525705257152572525735257452575525765257752578525795258052581525825258352584525855258652587525885258952590525915259252593525945259552596525975259852599526005260152602526035260452605526065260752608526095261052611526125261352614526155261652617526185261952620526215262252623526245262552626526275262852629526305263152632526335263452635526365263752638526395264052641526425264352644526455264652647526485264952650526515265252653526545265552656526575265852659526605266152662526635266452665526665266752668526695267052671526725267352674526755267652677526785267952680526815268252683526845268552686526875268852689526905269152692526935269452695526965269752698526995270052701527025270352704527055270652707527085270952710527115271252713527145271552716527175271852719527205272152722527235272452725527265272752728527295273052731527325273352734527355273652737527385273952740527415274252743527445274552746527475274852749527505275152752527535275452755527565275752758527595276052761527625276352764527655276652767527685276952770527715277252773527745277552776527775277852779527805278152782527835278452785527865278752788527895279052791527925279352794527955279652797527985279952800528015280252803528045280552806528075280852809528105281152812528135281452815528165281752818528195282052821528225282352824528255282652827528285282952830528315283252833528345283552836528375283852839528405284152842528435284452845528465284752848528495285052851528525285352854528555285652857528585285952860528615286252863528645286552866528675286852869528705287152872528735287452875528765287752878528795288052881528825288352884528855288652887528885288952890528915289252893528945289552896528975289852899529005290152902529035290452905529065290752908529095291052911529125291352914529155291652917529185291952920529215292252923529245292552926529275292852929529305293152932529335293452935529365293752938529395294052941529425294352944529455294652947529485294952950529515295252953529545295552956529575295852959529605296152962529635296452965529665296752968529695297052971529725297352974529755297652977529785297952980529815298252983529845298552986529875298852989529905299152992529935299452995529965299752998529995300053001530025300353004530055300653007530085300953010530115301253013530145301553016530175301853019530205302153022530235302453025530265302753028530295303053031530325303353034530355303653037530385303953040530415304253043530445304553046530475304853049530505305153052530535305453055530565305753058530595306053061530625306353064530655306653067530685306953070530715307253073530745307553076530775307853079530805308153082530835308453085530865308753088530895309053091530925309353094530955309653097530985309953100531015310253103531045310553106531075310853109531105311153112531135311453115531165311753118531195312053121531225312353124531255312653127531285312953130531315313253133531345313553136531375313853139531405314153142531435314453145531465314753148531495315053151531525315353154531555315653157531585315953160531615316253163531645316553166531675316853169531705317153172531735317453175531765317753178531795318053181531825318353184531855318653187531885318953190531915319253193531945319553196531975319853199532005320153202532035320453205532065320753208532095321053211532125321353214532155321653217532185321953220532215322253223532245322553226532275322853229532305323153232532335323453235532365323753238532395324053241532425324353244532455324653247532485324953250532515325253253532545325553256532575325853259532605326153262532635326453265532665326753268532695327053271532725327353274532755327653277532785327953280532815328253283532845328553286532875328853289532905329153292532935329453295532965329753298532995330053301533025330353304533055330653307533085330953310533115331253313533145331553316533175331853319533205332153322533235332453325533265332753328533295333053331533325333353334533355333653337533385333953340533415334253343533445334553346533475334853349533505335153352533535335453355533565335753358533595336053361533625336353364533655336653367533685336953370533715337253373533745337553376533775337853379533805338153382533835338453385533865338753388533895339053391533925339353394533955339653397533985339953400534015340253403534045340553406534075340853409534105341153412534135341453415534165341753418534195342053421534225342353424534255342653427534285342953430534315343253433534345343553436534375343853439534405344153442534435344453445534465344753448534495345053451534525345353454534555345653457534585345953460534615346253463534645346553466534675346853469534705347153472534735347453475534765347753478534795348053481534825348353484534855348653487534885348953490534915349253493534945349553496534975349853499535005350153502535035350453505535065350753508535095351053511535125351353514535155351653517535185351953520535215352253523535245352553526535275352853529535305353153532535335353453535535365353753538535395354053541535425354353544535455354653547535485354953550535515355253553535545355553556535575355853559535605356153562535635356453565535665356753568535695357053571535725357353574535755357653577535785357953580535815358253583535845358553586535875358853589535905359153592535935359453595535965359753598535995360053601536025360353604536055360653607536085360953610536115361253613536145361553616536175361853619536205362153622536235362453625536265362753628536295363053631536325363353634536355363653637536385363953640536415364253643536445364553646536475364853649536505365153652536535365453655536565365753658536595366053661536625366353664536655366653667536685366953670536715367253673536745367553676536775367853679536805368153682536835368453685536865368753688536895369053691536925369353694536955369653697536985369953700537015370253703537045370553706537075370853709537105371153712537135371453715537165371753718537195372053721537225372353724537255372653727537285372953730537315373253733537345373553736537375373853739537405374153742537435374453745537465374753748537495375053751537525375353754537555375653757537585375953760537615376253763537645376553766537675376853769537705377153772537735377453775537765377753778537795378053781537825378353784537855378653787537885378953790537915379253793537945379553796537975379853799538005380153802538035380453805538065380753808538095381053811538125381353814538155381653817538185381953820538215382253823538245382553826538275382853829538305383153832538335383453835538365383753838538395384053841538425384353844538455384653847538485384953850538515385253853538545385553856538575385853859538605386153862538635386453865538665386753868538695387053871538725387353874538755387653877538785387953880538815388253883538845388553886538875388853889538905389153892538935389453895538965389753898538995390053901539025390353904539055390653907539085390953910539115391253913539145391553916539175391853919539205392153922539235392453925539265392753928539295393053931539325393353934539355393653937539385393953940539415394253943539445394553946539475394853949539505395153952539535395453955539565395753958539595396053961539625396353964539655396653967539685396953970539715397253973539745397553976539775397853979539805398153982539835398453985539865398753988539895399053991539925399353994539955399653997539985399954000540015400254003540045400554006540075400854009540105401154012540135401454015540165401754018540195402054021540225402354024540255402654027540285402954030540315403254033540345403554036540375403854039540405404154042540435404454045540465404754048540495405054051540525405354054540555405654057540585405954060540615406254063540645406554066540675406854069540705407154072540735407454075540765407754078540795408054081540825408354084540855408654087540885408954090540915409254093540945409554096540975409854099541005410154102541035410454105541065410754108541095411054111541125411354114541155411654117541185411954120541215412254123541245412554126541275412854129541305413154132541335413454135541365413754138541395414054141541425414354144541455414654147541485414954150541515415254153541545415554156541575415854159541605416154162541635416454165541665416754168541695417054171541725417354174541755417654177541785417954180541815418254183541845418554186541875418854189541905419154192541935419454195541965419754198541995420054201542025420354204542055420654207542085420954210542115421254213542145421554216542175421854219542205422154222542235422454225542265422754228542295423054231542325423354234542355423654237542385423954240542415424254243542445424554246542475424854249542505425154252542535425454255542565425754258542595426054261542625426354264542655426654267542685426954270542715427254273542745427554276542775427854279542805428154282542835428454285542865428754288542895429054291542925429354294542955429654297542985429954300543015430254303543045430554306543075430854309543105431154312543135431454315543165431754318543195432054321543225432354324543255432654327543285432954330543315433254333543345433554336543375433854339543405434154342543435434454345543465434754348543495435054351543525435354354543555435654357543585435954360543615436254363543645436554366543675436854369543705437154372543735437454375543765437754378543795438054381543825438354384543855438654387543885438954390543915439254393543945439554396543975439854399544005440154402544035440454405544065440754408544095441054411544125441354414544155441654417544185441954420544215442254423544245442554426544275442854429544305443154432544335443454435544365443754438544395444054441544425444354444544455444654447544485444954450544515445254453544545445554456544575445854459544605446154462544635446454465544665446754468544695447054471544725447354474544755447654477544785447954480544815448254483544845448554486544875448854489544905449154492544935449454495544965449754498544995450054501545025450354504545055450654507545085450954510545115451254513545145451554516545175451854519545205452154522545235452454525545265452754528545295453054531545325453354534545355453654537545385453954540545415454254543545445454554546545475454854549545505455154552545535455454555545565455754558545595456054561545625456354564545655456654567545685456954570545715457254573545745457554576545775457854579545805458154582545835458454585545865458754588545895459054591545925459354594545955459654597545985459954600546015460254603546045460554606546075460854609546105461154612546135461454615546165461754618546195462054621546225462354624546255462654627546285462954630546315463254633546345463554636546375463854639546405464154642546435464454645546465464754648546495465054651546525465354654546555465654657546585465954660546615466254663546645466554666546675466854669546705467154672546735467454675546765467754678546795468054681546825468354684546855468654687546885468954690546915469254693546945469554696546975469854699547005470154702547035470454705547065470754708547095471054711547125471354714547155471654717547185471954720547215472254723547245472554726547275472854729547305473154732547335473454735547365473754738547395474054741547425474354744547455474654747547485474954750547515475254753547545475554756547575475854759547605476154762547635476454765547665476754768547695477054771547725477354774547755477654777547785477954780547815478254783547845478554786547875478854789547905479154792547935479454795547965479754798547995480054801548025480354804548055480654807548085480954810548115481254813548145481554816548175481854819548205482154822548235482454825548265482754828548295483054831548325483354834548355483654837548385483954840548415484254843548445484554846548475484854849548505485154852548535485454855548565485754858548595486054861548625486354864548655486654867548685486954870548715487254873548745487554876548775487854879548805488154882548835488454885548865488754888548895489054891548925489354894548955489654897548985489954900549015490254903549045490554906549075490854909549105491154912549135491454915549165491754918549195492054921549225492354924549255492654927549285492954930549315493254933549345493554936549375493854939549405494154942549435494454945549465494754948549495495054951549525495354954549555495654957549585495954960549615496254963549645496554966549675496854969549705497154972549735497454975549765497754978549795498054981549825498354984549855498654987549885498954990549915499254993549945499554996549975499854999550005500155002550035500455005550065500755008550095501055011550125501355014550155501655017550185501955020550215502255023550245502555026550275502855029550305503155032550335503455035550365503755038550395504055041550425504355044550455504655047550485504955050550515505255053550545505555056550575505855059550605506155062550635506455065550665506755068550695507055071550725507355074550755507655077550785507955080550815508255083550845508555086550875508855089550905509155092550935509455095550965509755098550995510055101551025510355104551055510655107551085510955110551115511255113551145511555116551175511855119551205512155122551235512455125551265512755128551295513055131551325513355134551355513655137551385513955140551415514255143551445514555146551475514855149551505515155152551535515455155551565515755158551595516055161551625516355164551655516655167551685516955170551715517255173551745517555176551775517855179551805518155182551835518455185551865518755188551895519055191551925519355194551955519655197551985519955200552015520255203552045520555206552075520855209552105521155212552135521455215552165521755218552195522055221552225522355224552255522655227552285522955230552315523255233552345523555236552375523855239552405524155242552435524455245552465524755248552495525055251552525525355254552555525655257552585525955260552615526255263552645526555266552675526855269552705527155272552735527455275552765527755278552795528055281552825528355284552855528655287552885528955290552915529255293552945529555296552975529855299553005530155302553035530455305553065530755308553095531055311553125531355314553155531655317553185531955320553215532255323553245532555326553275532855329553305533155332553335533455335553365533755338553395534055341553425534355344553455534655347553485534955350553515535255353553545535555356553575535855359553605536155362553635536455365553665536755368553695537055371553725537355374553755537655377553785537955380553815538255383553845538555386553875538855389553905539155392553935539455395553965539755398553995540055401554025540355404554055540655407554085540955410554115541255413554145541555416554175541855419554205542155422554235542455425554265542755428554295543055431554325543355434554355543655437554385543955440554415544255443554445544555446554475544855449554505545155452554535545455455554565545755458554595546055461554625546355464554655546655467554685546955470554715547255473554745547555476554775547855479554805548155482554835548455485554865548755488554895549055491554925549355494554955549655497554985549955500555015550255503555045550555506555075550855509555105551155512555135551455515555165551755518555195552055521555225552355524555255552655527555285552955530555315553255533555345553555536555375553855539555405554155542555435554455545555465554755548555495555055551555525555355554555555555655557555585555955560555615556255563555645556555566555675556855569555705557155572555735557455575555765557755578555795558055581555825558355584555855558655587555885558955590555915559255593555945559555596555975559855599556005560155602556035560455605556065560755608556095561055611556125561355614556155561655617556185561955620556215562255623556245562555626556275562855629556305563155632556335563455635556365563755638556395564055641556425564355644556455564655647556485564955650556515565255653556545565555656556575565855659556605566155662556635566455665556665566755668556695567055671556725567355674556755567655677556785567955680556815568255683556845568555686556875568855689556905569155692556935569455695556965569755698556995570055701557025570355704557055570655707557085570955710557115571255713557145571555716557175571855719557205572155722557235572455725557265572755728557295573055731557325573355734557355573655737557385573955740557415574255743557445574555746557475574855749557505575155752557535575455755557565575755758557595576055761557625576355764557655576655767557685576955770557715577255773557745577555776557775577855779557805578155782557835578455785557865578755788557895579055791557925579355794557955579655797557985579955800558015580255803558045580555806558075580855809558105581155812558135581455815558165581755818558195582055821558225582355824558255582655827558285582955830558315583255833558345583555836558375583855839558405584155842558435584455845558465584755848558495585055851558525585355854558555585655857558585585955860558615586255863558645586555866558675586855869558705587155872558735587455875558765587755878558795588055881558825588355884558855588655887558885588955890558915589255893558945589555896558975589855899559005590155902559035590455905559065590755908559095591055911559125591355914559155591655917559185591955920559215592255923559245592555926559275592855929559305593155932559335593455935559365593755938559395594055941559425594355944559455594655947559485594955950559515595255953559545595555956559575595855959559605596155962559635596455965559665596755968559695597055971559725597355974559755597655977559785597955980559815598255983559845598555986559875598855989559905599155992559935599455995559965599755998559995600056001560025600356004560055600656007560085600956010560115601256013560145601556016560175601856019560205602156022560235602456025560265602756028560295603056031560325603356034560355603656037560385603956040560415604256043560445604556046560475604856049560505605156052560535605456055560565605756058560595606056061560625606356064560655606656067560685606956070560715607256073560745607556076560775607856079560805608156082560835608456085560865608756088560895609056091560925609356094560955609656097560985609956100561015610256103561045610556106561075610856109561105611156112561135611456115561165611756118561195612056121561225612356124561255612656127561285612956130561315613256133561345613556136561375613856139561405614156142561435614456145561465614756148561495615056151561525615356154561555615656157561585615956160561615616256163561645616556166561675616856169561705617156172561735617456175561765617756178561795618056181561825618356184561855618656187561885618956190561915619256193561945619556196561975619856199562005620156202562035620456205562065620756208562095621056211562125621356214562155621656217562185621956220562215622256223562245622556226562275622856229562305623156232562335623456235562365623756238562395624056241562425624356244562455624656247562485624956250562515625256253562545625556256562575625856259562605626156262562635626456265562665626756268562695627056271562725627356274562755627656277562785627956280562815628256283562845628556286562875628856289562905629156292562935629456295562965629756298562995630056301563025630356304563055630656307563085630956310563115631256313563145631556316563175631856319563205632156322563235632456325563265632756328563295633056331563325633356334563355633656337563385633956340563415634256343563445634556346563475634856349563505635156352563535635456355563565635756358563595636056361563625636356364563655636656367563685636956370563715637256373563745637556376563775637856379563805638156382563835638456385563865638756388563895639056391563925639356394563955639656397563985639956400564015640256403564045640556406564075640856409564105641156412564135641456415564165641756418564195642056421564225642356424564255642656427564285642956430564315643256433564345643556436564375643856439564405644156442564435644456445564465644756448564495645056451564525645356454564555645656457564585645956460564615646256463564645646556466564675646856469564705647156472564735647456475564765647756478564795648056481564825648356484564855648656487564885648956490564915649256493564945649556496564975649856499565005650156502565035650456505565065650756508565095651056511565125651356514565155651656517565185651956520565215652256523565245652556526565275652856529565305653156532565335653456535565365653756538565395654056541565425654356544565455654656547565485654956550565515655256553565545655556556565575655856559565605656156562565635656456565565665656756568565695657056571565725657356574565755657656577565785657956580565815658256583565845658556586565875658856589565905659156592565935659456595565965659756598565995660056601566025660356604566055660656607566085660956610566115661256613566145661556616566175661856619566205662156622566235662456625566265662756628566295663056631566325663356634566355663656637566385663956640566415664256643566445664556646566475664856649566505665156652566535665456655566565665756658566595666056661566625666356664566655666656667566685666956670566715667256673566745667556676566775667856679566805668156682566835668456685566865668756688566895669056691566925669356694566955669656697566985669956700567015670256703567045670556706567075670856709567105671156712567135671456715567165671756718567195672056721567225672356724567255672656727567285672956730567315673256733567345673556736567375673856739567405674156742567435674456745567465674756748567495675056751567525675356754567555675656757567585675956760567615676256763567645676556766567675676856769567705677156772567735677456775567765677756778567795678056781567825678356784567855678656787567885678956790567915679256793567945679556796567975679856799568005680156802568035680456805568065680756808568095681056811568125681356814568155681656817568185681956820568215682256823568245682556826568275682856829568305683156832568335683456835568365683756838568395684056841568425684356844568455684656847568485684956850568515685256853568545685556856568575685856859568605686156862568635686456865568665686756868568695687056871568725687356874568755687656877568785687956880568815688256883568845688556886568875688856889568905689156892568935689456895568965689756898568995690056901569025690356904569055690656907569085690956910569115691256913569145691556916569175691856919569205692156922569235692456925569265692756928569295693056931569325693356934569355693656937569385693956940569415694256943569445694556946569475694856949569505695156952569535695456955569565695756958569595696056961569625696356964569655696656967569685696956970569715697256973569745697556976569775697856979569805698156982569835698456985569865698756988569895699056991569925699356994569955699656997569985699957000570015700257003570045700557006570075700857009570105701157012570135701457015570165701757018570195702057021570225702357024570255702657027570285702957030570315703257033570345703557036570375703857039570405704157042570435704457045570465704757048570495705057051570525705357054570555705657057570585705957060570615706257063570645706557066570675706857069570705707157072570735707457075570765707757078570795708057081570825708357084570855708657087570885708957090570915709257093570945709557096570975709857099571005710157102571035710457105571065710757108571095711057111571125711357114571155711657117571185711957120571215712257123571245712557126571275712857129571305713157132571335713457135571365713757138571395714057141571425714357144571455714657147571485714957150571515715257153571545715557156571575715857159571605716157162571635716457165571665716757168571695717057171571725717357174571755717657177571785717957180571815718257183571845718557186571875718857189571905719157192571935719457195571965719757198571995720057201572025720357204572055720657207572085720957210572115721257213572145721557216572175721857219572205722157222572235722457225572265722757228572295723057231572325723357234572355723657237572385723957240572415724257243572445724557246572475724857249572505725157252572535725457255572565725757258572595726057261572625726357264572655726657267572685726957270572715727257273572745727557276572775727857279572805728157282572835728457285572865728757288572895729057291572925729357294572955729657297572985729957300573015730257303573045730557306573075730857309573105731157312573135731457315573165731757318573195732057321573225732357324573255732657327573285732957330573315733257333573345733557336573375733857339573405734157342573435734457345573465734757348573495735057351573525735357354573555735657357573585735957360573615736257363573645736557366573675736857369573705737157372573735737457375573765737757378573795738057381573825738357384573855738657387573885738957390573915739257393573945739557396573975739857399574005740157402574035740457405574065740757408574095741057411574125741357414574155741657417574185741957420574215742257423574245742557426574275742857429574305743157432574335743457435574365743757438574395744057441574425744357444574455744657447574485744957450574515745257453574545745557456574575745857459574605746157462574635746457465574665746757468574695747057471574725747357474574755747657477574785747957480574815748257483574845748557486574875748857489574905749157492574935749457495574965749757498574995750057501575025750357504575055750657507575085750957510575115751257513575145751557516575175751857519575205752157522575235752457525575265752757528575295753057531575325753357534575355753657537575385753957540575415754257543575445754557546575475754857549575505755157552575535755457555575565755757558575595756057561575625756357564575655756657567575685756957570575715757257573575745757557576575775757857579575805758157582575835758457585575865758757588575895759057591575925759357594575955759657597575985759957600576015760257603576045760557606576075760857609576105761157612576135761457615576165761757618576195762057621576225762357624576255762657627576285762957630576315763257633576345763557636576375763857639576405764157642576435764457645576465764757648576495765057651576525765357654576555765657657576585765957660576615766257663576645766557666576675766857669576705767157672576735767457675576765767757678576795768057681576825768357684576855768657687576885768957690576915769257693576945769557696576975769857699577005770157702577035770457705577065770757708577095771057711577125771357714577155771657717577185771957720577215772257723577245772557726577275772857729577305773157732577335773457735577365773757738577395774057741577425774357744577455774657747577485774957750577515775257753577545775557756577575775857759577605776157762577635776457765577665776757768577695777057771577725777357774577755777657777577785777957780577815778257783577845778557786577875778857789577905779157792577935779457795577965779757798577995780057801578025780357804578055780657807578085780957810578115781257813578145781557816578175781857819578205782157822578235782457825578265782757828578295783057831578325783357834578355783657837578385783957840578415784257843578445784557846578475784857849578505785157852578535785457855578565785757858578595786057861578625786357864578655786657867578685786957870578715787257873578745787557876578775787857879578805788157882578835788457885578865788757888578895789057891578925789357894578955789657897578985789957900579015790257903579045790557906579075790857909579105791157912579135791457915579165791757918579195792057921579225792357924579255792657927579285792957930579315793257933579345793557936579375793857939579405794157942579435794457945579465794757948579495795057951579525795357954579555795657957579585795957960579615796257963579645796557966579675796857969579705797157972579735797457975579765797757978579795798057981579825798357984579855798657987579885798957990579915799257993579945799557996579975799857999580005800158002580035800458005580065800758008580095801058011580125801358014580155801658017580185801958020580215802258023580245802558026580275802858029580305803158032580335803458035580365803758038580395804058041580425804358044580455804658047580485804958050580515805258053580545805558056580575805858059580605806158062580635806458065580665806758068580695807058071580725807358074580755807658077580785807958080580815808258083580845808558086580875808858089580905809158092580935809458095580965809758098580995810058101581025810358104581055810658107581085810958110581115811258113581145811558116581175811858119581205812158122581235812458125581265812758128581295813058131581325813358134581355813658137581385813958140581415814258143581445814558146581475814858149581505815158152581535815458155581565815758158581595816058161581625816358164581655816658167581685816958170581715817258173581745817558176581775817858179581805818158182581835818458185581865818758188581895819058191581925819358194581955819658197581985819958200582015820258203582045820558206582075820858209582105821158212582135821458215582165821758218582195822058221582225822358224582255822658227582285822958230582315823258233582345823558236582375823858239582405824158242582435824458245582465824758248582495825058251582525825358254582555825658257582585825958260582615826258263582645826558266582675826858269582705827158272582735827458275582765827758278582795828058281582825828358284582855828658287582885828958290582915829258293582945829558296582975829858299583005830158302583035830458305583065830758308583095831058311583125831358314583155831658317583185831958320583215832258323583245832558326583275832858329583305833158332583335833458335583365833758338583395834058341583425834358344583455834658347583485834958350583515835258353583545835558356583575835858359583605836158362583635836458365583665836758368583695837058371583725837358374583755837658377583785837958380583815838258383583845838558386583875838858389583905839158392583935839458395583965839758398583995840058401584025840358404584055840658407584085840958410584115841258413584145841558416584175841858419584205842158422584235842458425584265842758428584295843058431584325843358434584355843658437584385843958440584415844258443584445844558446584475844858449584505845158452584535845458455584565845758458584595846058461584625846358464584655846658467584685846958470584715847258473584745847558476584775847858479584805848158482584835848458485584865848758488584895849058491584925849358494584955849658497584985849958500585015850258503585045850558506585075850858509585105851158512585135851458515585165851758518585195852058521585225852358524585255852658527585285852958530585315853258533585345853558536585375853858539585405854158542585435854458545585465854758548585495855058551585525855358554585555855658557585585855958560585615856258563585645856558566585675856858569585705857158572585735857458575585765857758578585795858058581585825858358584585855858658587585885858958590585915859258593585945859558596585975859858599586005860158602586035860458605586065860758608586095861058611586125861358614586155861658617586185861958620586215862258623586245862558626586275862858629586305863158632586335863458635586365863758638586395864058641586425864358644586455864658647586485864958650586515865258653586545865558656586575865858659586605866158662586635866458665586665866758668586695867058671586725867358674586755867658677586785867958680586815868258683586845868558686586875868858689586905869158692586935869458695586965869758698586995870058701587025870358704587055870658707587085870958710587115871258713587145871558716587175871858719587205872158722587235872458725587265872758728587295873058731587325873358734587355873658737587385873958740587415874258743587445874558746587475874858749587505875158752587535875458755587565875758758587595876058761587625876358764587655876658767587685876958770587715877258773587745877558776587775877858779587805878158782587835878458785587865878758788587895879058791587925879358794587955879658797587985879958800588015880258803588045880558806588075880858809588105881158812588135881458815588165881758818588195882058821588225882358824588255882658827588285882958830588315883258833588345883558836588375883858839588405884158842588435884458845588465884758848588495885058851588525885358854588555885658857588585885958860588615886258863588645886558866588675886858869588705887158872588735887458875588765887758878588795888058881588825888358884588855888658887588885888958890588915889258893588945889558896588975889858899589005890158902589035890458905589065890758908589095891058911589125891358914589155891658917589185891958920589215892258923589245892558926589275892858929589305893158932589335893458935589365893758938589395894058941589425894358944589455894658947589485894958950589515895258953589545895558956589575895858959589605896158962589635896458965589665896758968589695897058971589725897358974589755897658977589785897958980589815898258983589845898558986589875898858989589905899158992589935899458995589965899758998589995900059001590025900359004590055900659007590085900959010590115901259013590145901559016590175901859019590205902159022590235902459025590265902759028590295903059031590325903359034590355903659037590385903959040590415904259043590445904559046590475904859049590505905159052590535905459055590565905759058590595906059061590625906359064590655906659067590685906959070590715907259073590745907559076590775907859079590805908159082590835908459085590865908759088590895909059091590925909359094590955909659097590985909959100591015910259103591045910559106591075910859109591105911159112591135911459115591165911759118591195912059121591225912359124591255912659127591285912959130591315913259133591345913559136591375913859139591405914159142591435914459145591465914759148591495915059151591525915359154591555915659157591585915959160591615916259163591645916559166591675916859169591705917159172591735917459175591765917759178591795918059181591825918359184591855918659187591885918959190591915919259193591945919559196591975919859199592005920159202592035920459205592065920759208592095921059211592125921359214592155921659217592185921959220592215922259223592245922559226592275922859229592305923159232592335923459235592365923759238592395924059241592425924359244592455924659247592485924959250592515925259253592545925559256592575925859259592605926159262592635926459265592665926759268592695927059271592725927359274592755927659277592785927959280592815928259283592845928559286592875928859289592905929159292592935929459295592965929759298592995930059301593025930359304593055930659307593085930959310593115931259313593145931559316593175931859319593205932159322593235932459325593265932759328593295933059331593325933359334593355933659337593385933959340593415934259343593445934559346593475934859349593505935159352593535935459355593565935759358593595936059361593625936359364593655936659367593685936959370593715937259373593745937559376593775937859379593805938159382593835938459385593865938759388593895939059391593925939359394593955939659397593985939959400594015940259403594045940559406594075940859409594105941159412594135941459415594165941759418594195942059421594225942359424594255942659427594285942959430594315943259433594345943559436594375943859439594405944159442594435944459445594465944759448594495945059451594525945359454594555945659457594585945959460594615946259463594645946559466594675946859469594705947159472594735947459475594765947759478594795948059481594825948359484594855948659487594885948959490594915949259493594945949559496594975949859499595005950159502595035950459505595065950759508595095951059511595125951359514595155951659517595185951959520595215952259523595245952559526595275952859529595305953159532595335953459535595365953759538595395954059541595425954359544595455954659547595485954959550595515955259553595545955559556595575955859559595605956159562595635956459565595665956759568595695957059571595725957359574595755957659577595785957959580595815958259583595845958559586595875958859589595905959159592595935959459595595965959759598595995960059601596025960359604596055960659607596085960959610596115961259613596145961559616596175961859619596205962159622596235962459625596265962759628596295963059631596325963359634596355963659637596385963959640596415964259643596445964559646596475964859649596505965159652596535965459655596565965759658596595966059661596625966359664596655966659667596685966959670596715967259673596745967559676596775967859679596805968159682596835968459685596865968759688596895969059691596925969359694596955969659697596985969959700597015970259703597045970559706597075970859709597105971159712597135971459715597165971759718597195972059721597225972359724597255972659727597285972959730597315973259733597345973559736597375973859739597405974159742597435974459745597465974759748597495975059751597525975359754597555975659757597585975959760597615976259763597645976559766597675976859769597705977159772597735977459775597765977759778597795978059781597825978359784597855978659787597885978959790597915979259793597945979559796597975979859799598005980159802598035980459805598065980759808598095981059811598125981359814598155981659817598185981959820598215982259823598245982559826598275982859829598305983159832598335983459835598365983759838598395984059841598425984359844598455984659847598485984959850598515985259853598545985559856598575985859859598605986159862598635986459865598665986759868598695987059871598725987359874598755987659877598785987959880598815988259883598845988559886598875988859889598905989159892598935989459895598965989759898598995990059901599025990359904599055990659907599085990959910599115991259913599145991559916599175991859919599205992159922599235992459925599265992759928599295993059931599325993359934599355993659937599385993959940599415994259943599445994559946599475994859949599505995159952599535995459955599565995759958599595996059961599625996359964599655996659967599685996959970599715997259973599745997559976599775997859979599805998159982599835998459985599865998759988599895999059991599925999359994599955999659997599985999960000600016000260003600046000560006600076000860009600106001160012600136001460015600166001760018600196002060021600226002360024600256002660027600286002960030600316003260033600346003560036600376003860039600406004160042600436004460045600466004760048600496005060051600526005360054600556005660057600586005960060600616006260063600646006560066600676006860069600706007160072600736007460075600766007760078600796008060081600826008360084600856008660087600886008960090600916009260093600946009560096600976009860099601006010160102601036010460105601066010760108601096011060111601126011360114601156011660117601186011960120601216012260123601246012560126601276012860129601306013160132601336013460135601366013760138601396014060141601426014360144601456014660147601486014960150601516015260153601546015560156601576015860159601606016160162601636016460165601666016760168601696017060171601726017360174601756017660177601786017960180601816018260183601846018560186601876018860189601906019160192601936019460195601966019760198601996020060201602026020360204602056020660207602086020960210602116021260213602146021560216602176021860219602206022160222602236022460225602266022760228602296023060231602326023360234602356023660237602386023960240602416024260243602446024560246602476024860249602506025160252602536025460255602566025760258602596026060261602626026360264602656026660267602686026960270602716027260273602746027560276602776027860279602806028160282602836028460285602866028760288602896029060291602926029360294602956029660297602986029960300603016030260303603046030560306603076030860309603106031160312603136031460315603166031760318603196032060321603226032360324603256032660327603286032960330603316033260333603346033560336603376033860339603406034160342603436034460345603466034760348603496035060351603526035360354603556035660357603586035960360603616036260363603646036560366603676036860369603706037160372603736037460375603766037760378603796038060381603826038360384603856038660387603886038960390603916039260393603946039560396603976039860399604006040160402604036040460405604066040760408604096041060411604126041360414604156041660417604186041960420604216042260423604246042560426604276042860429604306043160432604336043460435604366043760438604396044060441604426044360444604456044660447604486044960450604516045260453604546045560456604576045860459604606046160462604636046460465604666046760468604696047060471604726047360474604756047660477604786047960480604816048260483604846048560486604876048860489604906049160492604936049460495604966049760498604996050060501605026050360504605056050660507605086050960510605116051260513605146051560516605176051860519605206052160522605236052460525605266052760528605296053060531605326053360534605356053660537605386053960540605416054260543605446054560546605476054860549605506055160552605536055460555605566055760558605596056060561605626056360564605656056660567605686056960570605716057260573605746057560576605776057860579605806058160582605836058460585605866058760588605896059060591605926059360594605956059660597605986059960600606016060260603606046060560606606076060860609606106061160612606136061460615606166061760618606196062060621606226062360624606256062660627606286062960630606316063260633606346063560636606376063860639606406064160642606436064460645606466064760648606496065060651606526065360654606556065660657606586065960660606616066260663606646066560666606676066860669606706067160672606736067460675606766067760678606796068060681606826068360684606856068660687606886068960690606916069260693606946069560696606976069860699607006070160702607036070460705607066070760708607096071060711607126071360714607156071660717607186071960720607216072260723607246072560726607276072860729607306073160732607336073460735607366073760738607396074060741607426074360744607456074660747607486074960750607516075260753607546075560756607576075860759607606076160762607636076460765607666076760768607696077060771607726077360774607756077660777607786077960780607816078260783607846078560786607876078860789607906079160792607936079460795607966079760798607996080060801608026080360804608056080660807608086080960810608116081260813608146081560816608176081860819608206082160822608236082460825608266082760828608296083060831608326083360834608356083660837608386083960840608416084260843608446084560846608476084860849608506085160852608536085460855608566085760858608596086060861608626086360864608656086660867608686086960870608716087260873608746087560876608776087860879608806088160882608836088460885608866088760888608896089060891608926089360894608956089660897608986089960900609016090260903609046090560906609076090860909609106091160912609136091460915609166091760918609196092060921609226092360924609256092660927609286092960930609316093260933609346093560936609376093860939609406094160942609436094460945609466094760948609496095060951609526095360954609556095660957609586095960960609616096260963609646096560966609676096860969609706097160972609736097460975609766097760978609796098060981609826098360984609856098660987609886098960990609916099260993609946099560996609976099860999610006100161002610036100461005610066100761008610096101061011610126101361014610156101661017610186101961020610216102261023610246102561026610276102861029610306103161032610336103461035610366103761038610396104061041610426104361044610456104661047610486104961050610516105261053610546105561056610576105861059610606106161062610636106461065610666106761068610696107061071610726107361074610756107661077610786107961080610816108261083610846108561086610876108861089610906109161092610936109461095610966109761098610996110061101611026110361104611056110661107611086110961110611116111261113611146111561116611176111861119611206112161122611236112461125611266112761128611296113061131611326113361134611356113661137611386113961140611416114261143611446114561146611476114861149611506115161152611536115461155611566115761158611596116061161611626116361164611656116661167611686116961170611716117261173611746117561176611776117861179611806118161182611836118461185611866118761188611896119061191611926119361194611956119661197611986119961200612016120261203612046120561206612076120861209612106121161212612136121461215612166121761218612196122061221612226122361224612256122661227612286122961230612316123261233612346123561236612376123861239612406124161242612436124461245612466124761248612496125061251612526125361254612556125661257612586125961260612616126261263612646126561266612676126861269612706127161272612736127461275612766127761278612796128061281612826128361284612856128661287612886128961290612916129261293612946129561296612976129861299613006130161302613036130461305613066130761308613096131061311613126131361314613156131661317613186131961320613216132261323613246132561326613276132861329613306133161332613336133461335613366133761338613396134061341613426134361344613456134661347613486134961350613516135261353613546135561356613576135861359613606136161362613636136461365613666136761368613696137061371613726137361374613756137661377613786137961380613816138261383613846138561386613876138861389613906139161392613936139461395613966139761398613996140061401614026140361404614056140661407614086140961410614116141261413614146141561416614176141861419614206142161422614236142461425614266142761428614296143061431614326143361434614356143661437614386143961440614416144261443614446144561446614476144861449614506145161452614536145461455614566145761458614596146061461614626146361464614656146661467614686146961470614716147261473614746147561476614776147861479614806148161482614836148461485614866148761488614896149061491614926149361494614956149661497614986149961500615016150261503615046150561506615076150861509615106151161512615136151461515615166151761518615196152061521615226152361524615256152661527615286152961530615316153261533615346153561536615376153861539615406154161542615436154461545615466154761548615496155061551615526155361554615556155661557615586155961560615616156261563615646156561566615676156861569615706157161572615736157461575615766157761578615796158061581615826158361584615856158661587615886158961590615916159261593615946159561596615976159861599616006160161602616036160461605616066160761608616096161061611616126161361614616156161661617616186161961620616216162261623616246162561626616276162861629616306163161632616336163461635616366163761638616396164061641616426164361644616456164661647616486164961650616516165261653616546165561656616576165861659616606166161662616636166461665616666166761668616696167061671616726167361674616756167661677616786167961680616816168261683616846168561686616876168861689616906169161692616936169461695616966169761698616996170061701617026170361704617056170661707617086170961710617116171261713617146171561716617176171861719617206172161722617236172461725617266172761728617296173061731617326173361734617356173661737617386173961740617416174261743617446174561746617476174861749617506175161752617536175461755617566175761758617596176061761617626176361764617656176661767617686176961770617716177261773617746177561776617776177861779617806178161782617836178461785617866178761788617896179061791617926179361794617956179661797617986179961800618016180261803618046180561806618076180861809618106181161812618136181461815618166181761818618196182061821618226182361824618256182661827618286182961830618316183261833618346183561836618376183861839618406184161842618436184461845618466184761848618496185061851618526185361854618556185661857618586185961860618616186261863618646186561866618676186861869618706187161872618736187461875618766187761878618796188061881618826188361884618856188661887618886188961890618916189261893618946189561896618976189861899619006190161902619036190461905619066190761908619096191061911619126191361914619156191661917619186191961920619216192261923619246192561926619276192861929619306193161932619336193461935619366193761938619396194061941619426194361944619456194661947619486194961950619516195261953619546195561956619576195861959619606196161962619636196461965619666196761968619696197061971619726197361974619756197661977619786197961980619816198261983619846198561986619876198861989619906199161992619936199461995619966199761998619996200062001620026200362004620056200662007620086200962010620116201262013620146201562016620176201862019620206202162022620236202462025620266202762028620296203062031620326203362034620356203662037620386203962040620416204262043620446204562046620476204862049620506205162052620536205462055620566205762058620596206062061620626206362064620656206662067620686206962070620716207262073620746207562076620776207862079620806208162082620836208462085620866208762088620896209062091620926209362094620956209662097620986209962100621016210262103621046210562106621076210862109621106211162112621136211462115621166211762118621196212062121621226212362124621256212662127621286212962130621316213262133621346213562136621376213862139621406214162142621436214462145621466214762148621496215062151621526215362154621556215662157621586215962160621616216262163621646216562166621676216862169621706217162172621736217462175621766217762178621796218062181621826218362184621856218662187621886218962190621916219262193621946219562196621976219862199622006220162202622036220462205622066220762208622096221062211622126221362214622156221662217622186221962220622216222262223622246222562226622276222862229622306223162232622336223462235622366223762238622396224062241622426224362244622456224662247622486224962250622516225262253622546225562256622576225862259622606226162262622636226462265622666226762268622696227062271622726227362274622756227662277622786227962280622816228262283622846228562286622876228862289622906229162292622936229462295622966229762298622996230062301623026230362304623056230662307623086230962310623116231262313623146231562316623176231862319623206232162322623236232462325623266232762328623296233062331623326233362334623356233662337623386233962340623416234262343623446234562346623476234862349623506235162352623536235462355623566235762358623596236062361623626236362364623656236662367623686236962370623716237262373623746237562376623776237862379623806238162382623836238462385623866238762388623896239062391623926239362394623956239662397623986239962400624016240262403624046240562406624076240862409624106241162412624136241462415624166241762418624196242062421624226242362424624256242662427624286242962430624316243262433624346243562436624376243862439624406244162442624436244462445624466244762448624496245062451624526245362454624556245662457624586245962460624616246262463624646246562466624676246862469624706247162472624736247462475624766247762478624796248062481624826248362484624856248662487624886248962490624916249262493624946249562496624976249862499625006250162502625036250462505625066250762508625096251062511625126251362514625156251662517625186251962520625216252262523625246252562526625276252862529625306253162532625336253462535625366253762538625396254062541625426254362544625456254662547625486254962550625516255262553625546255562556625576255862559625606256162562625636256462565625666256762568625696257062571625726257362574625756257662577625786257962580625816258262583625846258562586625876258862589625906259162592625936259462595625966259762598625996260062601626026260362604626056260662607626086260962610626116261262613626146261562616626176261862619626206262162622626236262462625626266262762628626296263062631626326263362634626356263662637626386263962640626416264262643626446264562646626476264862649626506265162652626536265462655626566265762658626596266062661626626266362664626656266662667626686266962670626716267262673626746267562676626776267862679626806268162682626836268462685626866268762688626896269062691626926269362694626956269662697626986269962700627016270262703627046270562706627076270862709627106271162712627136271462715627166271762718627196272062721627226272362724627256272662727627286272962730627316273262733627346273562736627376273862739627406274162742627436274462745627466274762748627496275062751627526275362754627556275662757627586275962760627616276262763627646276562766627676276862769627706277162772627736277462775627766277762778627796278062781627826278362784627856278662787627886278962790627916279262793627946279562796627976279862799628006280162802628036280462805628066280762808628096281062811628126281362814628156281662817628186281962820628216282262823628246282562826628276282862829628306283162832628336283462835628366283762838628396284062841628426284362844628456284662847628486284962850628516285262853628546285562856628576285862859628606286162862628636286462865628666286762868628696287062871628726287362874628756287662877628786287962880628816288262883628846288562886628876288862889628906289162892628936289462895628966289762898628996290062901629026290362904629056290662907629086290962910629116291262913629146291562916629176291862919629206292162922629236292462925629266292762928629296293062931629326293362934629356293662937629386293962940629416294262943629446294562946629476294862949629506295162952629536295462955629566295762958629596296062961629626296362964629656296662967629686296962970629716297262973629746297562976629776297862979629806298162982629836298462985629866298762988629896299062991629926299362994629956299662997629986299963000630016300263003630046300563006630076300863009630106301163012630136301463015630166301763018630196302063021630226302363024630256302663027630286302963030630316303263033630346303563036630376303863039630406304163042630436304463045630466304763048630496305063051630526305363054630556305663057630586305963060630616306263063630646306563066630676306863069630706307163072630736307463075630766307763078630796308063081630826308363084630856308663087630886308963090630916309263093630946309563096630976309863099631006310163102631036310463105631066310763108631096311063111631126311363114631156311663117631186311963120631216312263123631246312563126631276312863129631306313163132631336313463135631366313763138631396314063141631426314363144631456314663147631486314963150631516315263153631546315563156631576315863159631606316163162631636316463165631666316763168631696317063171631726317363174631756317663177631786317963180631816318263183631846318563186631876318863189631906319163192631936319463195631966319763198631996320063201632026320363204632056320663207632086320963210632116321263213632146321563216632176321863219632206322163222632236322463225632266322763228632296323063231632326323363234632356323663237632386323963240632416324263243632446324563246632476324863249632506325163252632536325463255632566325763258632596326063261632626326363264632656326663267632686326963270632716327263273632746327563276632776327863279632806328163282632836328463285632866328763288632896329063291632926329363294632956329663297632986329963300633016330263303633046330563306633076330863309633106331163312633136331463315633166331763318633196332063321633226332363324633256332663327633286332963330633316333263333633346333563336633376333863339633406334163342633436334463345633466334763348633496335063351633526335363354633556335663357633586335963360633616336263363633646336563366633676336863369633706337163372633736337463375633766337763378633796338063381633826338363384633856338663387633886338963390633916339263393633946339563396633976339863399634006340163402634036340463405634066340763408634096341063411634126341363414634156341663417634186341963420634216342263423634246342563426634276342863429634306343163432634336343463435634366343763438634396344063441634426344363444634456344663447634486344963450634516345263453634546345563456634576345863459634606346163462634636346463465634666346763468634696347063471634726347363474634756347663477634786347963480634816348263483634846348563486634876348863489634906349163492634936349463495634966349763498634996350063501635026350363504635056350663507635086350963510635116351263513635146351563516635176351863519635206352163522635236352463525635266352763528635296353063531635326353363534635356353663537635386353963540635416354263543635446354563546635476354863549635506355163552635536355463555635566355763558635596356063561635626356363564635656356663567635686356963570635716357263573635746357563576635776357863579635806358163582635836358463585635866358763588635896359063591635926359363594635956359663597635986359963600636016360263603636046360563606636076360863609636106361163612636136361463615636166361763618636196362063621636226362363624636256362663627636286362963630636316363263633636346363563636636376363863639636406364163642636436364463645636466364763648636496365063651636526365363654636556365663657636586365963660636616366263663636646366563666636676366863669636706367163672636736367463675636766367763678636796368063681636826368363684636856368663687636886368963690636916369263693636946369563696636976369863699637006370163702637036370463705637066370763708637096371063711637126371363714637156371663717637186371963720637216372263723637246372563726637276372863729637306373163732637336373463735637366373763738637396374063741637426374363744637456374663747637486374963750637516375263753637546375563756637576375863759637606376163762637636376463765637666376763768637696377063771637726377363774637756377663777637786377963780637816378263783637846378563786637876378863789637906379163792637936379463795637966379763798637996380063801638026380363804638056380663807638086380963810638116381263813638146381563816638176381863819638206382163822638236382463825638266382763828638296383063831638326383363834638356383663837638386383963840638416384263843638446384563846638476384863849638506385163852638536385463855638566385763858638596386063861638626386363864638656386663867638686386963870638716387263873638746387563876638776387863879638806388163882638836388463885638866388763888638896389063891638926389363894638956389663897638986389963900639016390263903639046390563906639076390863909639106391163912639136391463915639166391763918639196392063921639226392363924639256392663927639286392963930639316393263933639346393563936639376393863939639406394163942639436394463945639466394763948639496395063951639526395363954639556395663957639586395963960639616396263963639646396563966639676396863969639706397163972639736397463975639766397763978639796398063981639826398363984639856398663987639886398963990639916399263993639946399563996639976399863999640006400164002640036400464005640066400764008640096401064011640126401364014640156401664017640186401964020640216402264023640246402564026640276402864029640306403164032640336403464035640366403764038640396404064041640426404364044640456404664047640486404964050640516405264053640546405564056640576405864059640606406164062640636406464065640666406764068640696407064071640726407364074640756407664077640786407964080640816408264083640846408564086640876408864089640906409164092640936409464095640966409764098640996410064101641026410364104641056410664107641086410964110641116411264113641146411564116641176411864119641206412164122641236412464125641266412764128641296413064131641326413364134641356413664137641386413964140641416414264143641446414564146641476414864149641506415164152641536415464155641566415764158641596416064161641626416364164641656416664167641686416964170641716417264173641746417564176641776417864179641806418164182641836418464185641866418764188641896419064191641926419364194641956419664197641986419964200642016420264203642046420564206642076420864209642106421164212642136421464215642166421764218642196422064221642226422364224642256422664227642286422964230642316423264233642346423564236642376423864239642406424164242642436424464245642466424764248642496425064251642526425364254642556425664257642586425964260642616426264263642646426564266642676426864269642706427164272642736427464275642766427764278642796428064281642826428364284642856428664287642886428964290642916429264293642946429564296642976429864299643006430164302643036430464305643066430764308643096431064311643126431364314643156431664317643186431964320643216432264323643246432564326643276432864329643306433164332643336433464335643366433764338643396434064341643426434364344643456434664347643486434964350643516435264353643546435564356643576435864359643606436164362643636436464365643666436764368643696437064371643726437364374643756437664377643786437964380643816438264383643846438564386643876438864389643906439164392643936439464395643966439764398643996440064401644026440364404644056440664407644086440964410644116441264413644146441564416644176441864419644206442164422644236442464425644266442764428644296443064431644326443364434644356443664437644386443964440644416444264443644446444564446644476444864449644506445164452644536445464455644566445764458644596446064461644626446364464644656446664467644686446964470644716447264473644746447564476644776447864479644806448164482644836448464485644866448764488644896449064491644926449364494644956449664497644986449964500645016450264503645046450564506645076450864509645106451164512645136451464515645166451764518645196452064521645226452364524645256452664527645286452964530645316453264533645346453564536645376453864539645406454164542645436454464545645466454764548645496455064551645526455364554645556455664557645586455964560645616456264563645646456564566645676456864569645706457164572645736457464575645766457764578645796458064581645826458364584645856458664587645886458964590645916459264593645946459564596645976459864599646006460164602646036460464605646066460764608646096461064611646126461364614646156461664617646186461964620646216462264623646246462564626646276462864629646306463164632646336463464635646366463764638646396464064641646426464364644646456464664647646486464964650646516465264653646546465564656646576465864659646606466164662646636466464665646666466764668646696467064671646726467364674646756467664677646786467964680646816468264683646846468564686646876468864689646906469164692646936469464695646966469764698646996470064701647026470364704647056470664707647086470964710647116471264713647146471564716647176471864719647206472164722647236472464725647266472764728647296473064731647326473364734647356473664737647386473964740647416474264743647446474564746647476474864749647506475164752647536475464755647566475764758647596476064761647626476364764647656476664767647686476964770647716477264773647746477564776647776477864779647806478164782647836478464785647866478764788647896479064791647926479364794647956479664797647986479964800648016480264803648046480564806648076480864809648106481164812648136481464815648166481764818648196482064821648226482364824648256482664827648286482964830648316483264833648346483564836648376483864839648406484164842648436484464845648466484764848648496485064851648526485364854648556485664857648586485964860648616486264863648646486564866648676486864869648706487164872648736487464875648766487764878648796488064881648826488364884648856488664887648886488964890648916489264893648946489564896648976489864899649006490164902649036490464905649066490764908649096491064911649126491364914649156491664917649186491964920649216492264923649246492564926649276492864929649306493164932649336493464935649366493764938649396494064941649426494364944649456494664947649486494964950649516495264953649546495564956649576495864959649606496164962649636496464965649666496764968649696497064971649726497364974649756497664977649786497964980649816498264983649846498564986649876498864989649906499164992649936499464995649966499764998649996500065001650026500365004650056500665007650086500965010650116501265013650146501565016650176501865019650206502165022650236502465025650266502765028650296503065031650326503365034650356503665037650386503965040650416504265043650446504565046650476504865049650506505165052650536505465055650566505765058650596506065061650626506365064650656506665067650686506965070650716507265073650746507565076650776507865079650806508165082650836508465085650866508765088650896509065091650926509365094650956509665097650986509965100651016510265103651046510565106651076510865109651106511165112651136511465115651166511765118651196512065121651226512365124651256512665127651286512965130651316513265133651346513565136651376513865139651406514165142651436514465145651466514765148651496515065151651526515365154651556515665157651586515965160651616516265163651646516565166651676516865169651706517165172651736517465175651766517765178651796518065181651826518365184651856518665187651886518965190651916519265193651946519565196651976519865199652006520165202652036520465205652066520765208652096521065211652126521365214652156521665217652186521965220652216522265223652246522565226652276522865229652306523165232652336523465235652366523765238652396524065241652426524365244652456524665247652486524965250652516525265253652546525565256652576525865259652606526165262652636526465265652666526765268652696527065271652726527365274652756527665277652786527965280652816528265283652846528565286652876528865289652906529165292652936529465295652966529765298652996530065301653026530365304653056530665307653086530965310653116531265313653146531565316653176531865319653206532165322653236532465325653266532765328653296533065331653326533365334653356533665337653386533965340653416534265343653446534565346653476534865349653506535165352653536535465355653566535765358653596536065361653626536365364653656536665367653686536965370653716537265373653746537565376653776537865379653806538165382653836538465385653866538765388653896539065391653926539365394653956539665397653986539965400654016540265403654046540565406654076540865409654106541165412654136541465415654166541765418654196542065421654226542365424654256542665427654286542965430654316543265433654346543565436654376543865439654406544165442654436544465445654466544765448654496545065451654526545365454654556545665457654586545965460654616546265463654646546565466654676546865469654706547165472654736547465475654766547765478654796548065481654826548365484654856548665487654886548965490654916549265493654946549565496654976549865499655006550165502655036550465505655066550765508655096551065511655126551365514655156551665517655186551965520655216552265523655246552565526655276552865529655306553165532655336553465535655366553765538655396554065541655426554365544655456554665547655486554965550655516555265553655546555565556655576555865559655606556165562655636556465565655666556765568655696557065571655726557365574655756557665577655786557965580655816558265583655846558565586655876558865589655906559165592655936559465595655966559765598655996560065601656026560365604656056560665607656086560965610656116561265613656146561565616656176561865619656206562165622656236562465625656266562765628656296563065631656326563365634656356563665637656386563965640656416564265643656446564565646656476564865649656506565165652656536565465655656566565765658656596566065661656626566365664656656566665667656686566965670656716567265673656746567565676656776567865679656806568165682656836568465685656866568765688656896569065691656926569365694656956569665697656986569965700657016570265703657046570565706657076570865709657106571165712657136571465715657166571765718657196572065721657226572365724657256572665727657286572965730657316573265733657346573565736657376573865739657406574165742657436574465745657466574765748657496575065751657526575365754657556575665757657586575965760657616576265763657646576565766657676576865769657706577165772657736577465775657766577765778657796578065781657826578365784657856578665787657886578965790657916579265793657946579565796657976579865799658006580165802658036580465805658066580765808658096581065811658126581365814658156581665817658186581965820658216582265823658246582565826658276582865829658306583165832658336583465835658366583765838658396584065841658426584365844658456584665847658486584965850658516585265853658546585565856658576585865859658606586165862658636586465865658666586765868658696587065871658726587365874658756587665877658786587965880658816588265883658846588565886658876588865889658906589165892658936589465895658966589765898658996590065901659026590365904659056590665907659086590965910659116591265913659146591565916659176591865919659206592165922659236592465925659266592765928659296593065931659326593365934659356593665937659386593965940659416594265943659446594565946659476594865949659506595165952659536595465955659566595765958659596596065961659626596365964659656596665967659686596965970659716597265973659746597565976659776597865979659806598165982659836598465985659866598765988659896599065991659926599365994659956599665997659986599966000660016600266003660046600566006660076600866009660106601166012660136601466015660166601766018660196602066021660226602366024660256602666027660286602966030660316603266033660346603566036660376603866039660406604166042660436604466045660466604766048660496605066051660526605366054660556605666057660586605966060660616606266063660646606566066660676606866069660706607166072660736607466075660766607766078660796608066081660826608366084660856608666087660886608966090660916609266093660946609566096660976609866099661006610166102661036610466105661066610766108661096611066111661126611366114661156611666117661186611966120661216612266123661246612566126661276612866129661306613166132661336613466135661366613766138661396614066141661426614366144661456614666147661486614966150661516615266153661546615566156661576615866159661606616166162661636616466165661666616766168661696617066171661726617366174661756617666177661786617966180661816618266183661846618566186661876618866189661906619166192661936619466195661966619766198661996620066201662026620366204662056620666207662086620966210662116621266213662146621566216662176621866219662206622166222662236622466225662266622766228662296623066231662326623366234662356623666237662386623966240662416624266243662446624566246662476624866249662506625166252662536625466255662566625766258662596626066261662626626366264662656626666267662686626966270662716627266273662746627566276662776627866279662806628166282662836628466285662866628766288662896629066291662926629366294662956629666297662986629966300663016630266303663046630566306663076630866309663106631166312663136631466315663166631766318663196632066321663226632366324663256632666327663286632966330663316633266333663346633566336663376633866339663406634166342663436634466345663466634766348663496635066351663526635366354663556635666357663586635966360663616636266363663646636566366663676636866369663706637166372663736637466375663766637766378663796638066381663826638366384663856638666387663886638966390663916639266393663946639566396663976639866399664006640166402664036640466405664066640766408664096641066411664126641366414664156641666417664186641966420664216642266423664246642566426664276642866429664306643166432664336643466435664366643766438664396644066441664426644366444664456644666447664486644966450664516645266453664546645566456664576645866459664606646166462664636646466465664666646766468664696647066471664726647366474664756647666477664786647966480664816648266483664846648566486664876648866489664906649166492664936649466495664966649766498664996650066501665026650366504665056650666507665086650966510665116651266513665146651566516665176651866519665206652166522665236652466525665266652766528665296653066531665326653366534665356653666537665386653966540665416654266543665446654566546665476654866549665506655166552665536655466555665566655766558665596656066561665626656366564665656656666567665686656966570665716657266573665746657566576665776657866579665806658166582665836658466585665866658766588665896659066591665926659366594665956659666597665986659966600666016660266603666046660566606666076660866609666106661166612666136661466615666166661766618666196662066621666226662366624666256662666627666286662966630666316663266633666346663566636666376663866639666406664166642666436664466645666466664766648666496665066651666526665366654666556665666657666586665966660666616666266663666646666566666666676666866669666706667166672666736667466675666766667766678666796668066681666826668366684666856668666687666886668966690666916669266693666946669566696666976669866699667006670166702667036670466705667066670766708667096671066711667126671366714667156671666717667186671966720667216672266723667246672566726667276672866729667306673166732667336673466735667366673766738667396674066741667426674366744667456674666747667486674966750667516675266753667546675566756667576675866759667606676166762667636676466765667666676766768667696677066771667726677366774667756677666777667786677966780667816678266783667846678566786667876678866789667906679166792667936679466795667966679766798667996680066801668026680366804668056680666807668086680966810668116681266813668146681566816668176681866819668206682166822668236682466825668266682766828668296683066831668326683366834668356683666837668386683966840668416684266843668446684566846668476684866849668506685166852668536685466855668566685766858668596686066861668626686366864668656686666867668686686966870668716687266873668746687566876668776687866879668806688166882668836688466885668866688766888668896689066891668926689366894668956689666897668986689966900669016690266903669046690566906669076690866909669106691166912669136691466915669166691766918669196692066921669226692366924669256692666927669286692966930669316693266933669346693566936669376693866939669406694166942669436694466945669466694766948669496695066951669526695366954669556695666957669586695966960669616696266963669646696566966669676696866969669706697166972669736697466975669766697766978669796698066981669826698366984669856698666987669886698966990669916699266993669946699566996669976699866999670006700167002670036700467005670066700767008670096701067011670126701367014670156701667017670186701967020670216702267023670246702567026670276702867029670306703167032670336703467035670366703767038670396704067041670426704367044670456704667047670486704967050670516705267053670546705567056670576705867059670606706167062670636706467065670666706767068670696707067071670726707367074670756707667077670786707967080670816708267083670846708567086670876708867089670906709167092670936709467095670966709767098670996710067101671026710367104671056710667107671086710967110671116711267113671146711567116671176711867119671206712167122671236712467125671266712767128671296713067131671326713367134671356713667137671386713967140671416714267143671446714567146671476714867149671506715167152671536715467155671566715767158671596716067161671626716367164671656716667167671686716967170671716717267173671746717567176671776717867179671806718167182671836718467185671866718767188671896719067191671926719367194671956719667197671986719967200672016720267203672046720567206672076720867209672106721167212672136721467215672166721767218672196722067221672226722367224672256722667227672286722967230672316723267233672346723567236672376723867239672406724167242672436724467245672466724767248672496725067251672526725367254672556725667257672586725967260672616726267263672646726567266672676726867269672706727167272672736727467275672766727767278672796728067281672826728367284672856728667287672886728967290672916729267293672946729567296672976729867299673006730167302673036730467305673066730767308673096731067311673126731367314673156731667317673186731967320673216732267323673246732567326673276732867329673306733167332673336733467335673366733767338673396734067341673426734367344673456734667347673486734967350673516735267353673546735567356673576735867359673606736167362673636736467365673666736767368673696737067371673726737367374673756737667377673786737967380673816738267383673846738567386673876738867389673906739167392673936739467395673966739767398673996740067401674026740367404674056740667407674086740967410674116741267413674146741567416674176741867419674206742167422674236742467425674266742767428674296743067431674326743367434674356743667437674386743967440674416744267443674446744567446674476744867449674506745167452674536745467455674566745767458674596746067461674626746367464674656746667467674686746967470674716747267473674746747567476674776747867479674806748167482674836748467485674866748767488674896749067491674926749367494674956749667497674986749967500675016750267503675046750567506675076750867509675106751167512675136751467515675166751767518675196752067521675226752367524675256752667527675286752967530675316753267533675346753567536675376753867539675406754167542675436754467545675466754767548675496755067551675526755367554675556755667557675586755967560675616756267563675646756567566675676756867569675706757167572675736757467575675766757767578675796758067581675826758367584675856758667587675886758967590675916759267593675946759567596675976759867599676006760167602676036760467605676066760767608676096761067611676126761367614676156761667617676186761967620676216762267623676246762567626676276762867629676306763167632676336763467635676366763767638676396764067641676426764367644676456764667647676486764967650676516765267653676546765567656676576765867659676606766167662676636766467665676666766767668676696767067671676726767367674676756767667677676786767967680676816768267683676846768567686676876768867689676906769167692676936769467695676966769767698676996770067701677026770367704677056770667707677086770967710677116771267713677146771567716677176771867719677206772167722677236772467725677266772767728677296773067731677326773367734677356773667737677386773967740677416774267743677446774567746677476774867749677506775167752677536775467755677566775767758677596776067761677626776367764677656776667767677686776967770677716777267773677746777567776677776777867779677806778167782677836778467785677866778767788677896779067791677926779367794677956779667797677986779967800678016780267803678046780567806678076780867809678106781167812678136781467815678166781767818678196782067821678226782367824678256782667827678286782967830678316783267833678346783567836678376783867839678406784167842678436784467845678466784767848678496785067851678526785367854678556785667857678586785967860678616786267863678646786567866678676786867869678706787167872678736787467875678766787767878678796788067881678826788367884678856788667887678886788967890678916789267893678946789567896678976789867899679006790167902679036790467905679066790767908679096791067911679126791367914679156791667917679186791967920679216792267923679246792567926679276792867929679306793167932679336793467935679366793767938679396794067941679426794367944679456794667947679486794967950679516795267953679546795567956679576795867959679606796167962679636796467965679666796767968679696797067971679726797367974679756797667977679786797967980679816798267983679846798567986679876798867989679906799167992679936799467995679966799767998679996800068001680026800368004680056800668007680086800968010680116801268013680146801568016680176801868019680206802168022680236802468025680266802768028680296803068031680326803368034680356803668037680386803968040680416804268043680446804568046680476804868049680506805168052680536805468055680566805768058680596806068061680626806368064680656806668067680686806968070680716807268073680746807568076680776807868079680806808168082680836808468085680866808768088680896809068091680926809368094680956809668097680986809968100681016810268103681046810568106681076810868109681106811168112681136811468115681166811768118681196812068121681226812368124681256812668127681286812968130681316813268133681346813568136681376813868139681406814168142681436814468145681466814768148681496815068151681526815368154681556815668157681586815968160681616816268163681646816568166681676816868169681706817168172681736817468175681766817768178681796818068181681826818368184681856818668187681886818968190681916819268193681946819568196681976819868199682006820168202682036820468205682066820768208682096821068211682126821368214682156821668217682186821968220682216822268223682246822568226682276822868229682306823168232682336823468235682366823768238682396824068241682426824368244682456824668247682486824968250682516825268253682546825568256682576825868259682606826168262682636826468265682666826768268682696827068271682726827368274682756827668277682786827968280682816828268283682846828568286682876828868289682906829168292682936829468295682966829768298682996830068301683026830368304683056830668307683086830968310683116831268313683146831568316683176831868319683206832168322683236832468325683266832768328683296833068331683326833368334683356833668337683386833968340683416834268343683446834568346683476834868349683506835168352683536835468355683566835768358683596836068361683626836368364683656836668367683686836968370683716837268373683746837568376683776837868379683806838168382683836838468385683866838768388683896839068391683926839368394683956839668397683986839968400684016840268403684046840568406684076840868409684106841168412684136841468415684166841768418684196842068421684226842368424684256842668427684286842968430684316843268433684346843568436684376843868439684406844168442684436844468445684466844768448684496845068451684526845368454684556845668457684586845968460684616846268463684646846568466684676846868469684706847168472684736847468475684766847768478684796848068481684826848368484684856848668487684886848968490684916849268493684946849568496684976849868499685006850168502685036850468505685066850768508685096851068511685126851368514685156851668517685186851968520685216852268523685246852568526685276852868529685306853168532685336853468535685366853768538685396854068541685426854368544685456854668547685486854968550685516855268553685546855568556685576855868559685606856168562685636856468565685666856768568685696857068571685726857368574685756857668577685786857968580685816858268583685846858568586685876858868589685906859168592685936859468595685966859768598685996860068601686026860368604686056860668607686086860968610686116861268613686146861568616686176861868619686206862168622686236862468625686266862768628686296863068631686326863368634686356863668637686386863968640686416864268643686446864568646686476864868649686506865168652686536865468655686566865768658686596866068661686626866368664686656866668667686686866968670686716867268673686746867568676686776867868679686806868168682686836868468685686866868768688686896869068691686926869368694686956869668697686986869968700687016870268703687046870568706687076870868709687106871168712687136871468715687166871768718687196872068721687226872368724687256872668727687286872968730687316873268733687346873568736687376873868739687406874168742687436874468745687466874768748687496875068751687526875368754687556875668757687586875968760687616876268763687646876568766687676876868769687706877168772687736877468775687766877768778687796878068781687826878368784687856878668787687886878968790687916879268793687946879568796687976879868799688006880168802688036880468805688066880768808688096881068811688126881368814688156881668817688186881968820688216882268823688246882568826688276882868829688306883168832688336883468835688366883768838688396884068841688426884368844688456884668847688486884968850688516885268853688546885568856688576885868859688606886168862688636886468865688666886768868688696887068871688726887368874688756887668877688786887968880688816888268883688846888568886688876888868889688906889168892688936889468895688966889768898688996890068901689026890368904689056890668907689086890968910689116891268913689146891568916689176891868919689206892168922689236892468925689266892768928689296893068931689326893368934689356893668937689386893968940689416894268943689446894568946689476894868949689506895168952689536895468955689566895768958689596896068961689626896368964689656896668967689686896968970689716897268973689746897568976689776897868979689806898168982689836898468985689866898768988689896899068991689926899368994689956899668997689986899969000690016900269003690046900569006690076900869009690106901169012690136901469015690166901769018690196902069021690226902369024690256902669027690286902969030690316903269033690346903569036690376903869039690406904169042690436904469045690466904769048690496905069051690526905369054690556905669057690586905969060690616906269063690646906569066690676906869069690706907169072690736907469075690766907769078690796908069081690826908369084690856908669087690886908969090690916909269093690946909569096690976909869099691006910169102691036910469105691066910769108691096911069111691126911369114691156911669117691186911969120691216912269123691246912569126691276912869129691306913169132691336913469135691366913769138691396914069141691426914369144691456914669147691486914969150691516915269153691546915569156691576915869159691606916169162691636916469165691666916769168691696917069171691726917369174691756917669177691786917969180691816918269183691846918569186691876918869189691906919169192691936919469195691966919769198691996920069201692026920369204692056920669207692086920969210692116921269213692146921569216692176921869219692206922169222692236922469225692266922769228692296923069231692326923369234692356923669237692386923969240692416924269243692446924569246692476924869249692506925169252692536925469255692566925769258692596926069261692626926369264692656926669267692686926969270692716927269273692746927569276692776927869279692806928169282692836928469285692866928769288692896929069291692926929369294692956929669297692986929969300693016930269303693046930569306693076930869309693106931169312693136931469315693166931769318693196932069321693226932369324693256932669327693286932969330693316933269333693346933569336693376933869339693406934169342693436934469345693466934769348693496935069351693526935369354693556935669357693586935969360693616936269363693646936569366693676936869369693706937169372693736937469375693766937769378693796938069381693826938369384693856938669387693886938969390693916939269393693946939569396693976939869399694006940169402694036940469405694066940769408694096941069411694126941369414694156941669417694186941969420694216942269423694246942569426694276942869429694306943169432694336943469435694366943769438694396944069441694426944369444694456944669447694486944969450694516945269453694546945569456694576945869459694606946169462694636946469465694666946769468694696947069471694726947369474694756947669477694786947969480694816948269483694846948569486694876948869489694906949169492694936949469495694966949769498694996950069501695026950369504695056950669507695086950969510695116951269513695146951569516695176951869519695206952169522695236952469525695266952769528695296953069531695326953369534695356953669537695386953969540695416954269543695446954569546695476954869549695506955169552695536955469555695566955769558695596956069561695626956369564695656956669567695686956969570695716957269573695746957569576695776957869579695806958169582695836958469585695866958769588695896959069591695926959369594695956959669597695986959969600696016960269603696046960569606696076960869609696106961169612696136961469615696166961769618696196962069621696226962369624696256962669627696286962969630696316963269633696346963569636696376963869639696406964169642696436964469645696466964769648696496965069651696526965369654696556965669657696586965969660696616966269663696646966569666696676966869669696706967169672696736967469675696766967769678696796968069681696826968369684696856968669687696886968969690696916969269693696946969569696696976969869699697006970169702697036970469705697066970769708697096971069711697126971369714697156971669717697186971969720697216972269723697246972569726697276972869729697306973169732697336973469735697366973769738697396974069741697426974369744697456974669747697486974969750697516975269753697546975569756697576975869759697606976169762697636976469765697666976769768697696977069771697726977369774697756977669777697786977969780697816978269783697846978569786697876978869789697906979169792697936979469795697966979769798697996980069801698026980369804698056980669807698086980969810698116981269813698146981569816698176981869819698206982169822698236982469825698266982769828698296983069831698326983369834698356983669837698386983969840698416984269843698446984569846698476984869849698506985169852698536985469855698566985769858698596986069861698626986369864698656986669867698686986969870698716987269873698746987569876698776987869879698806988169882698836988469885698866988769888698896989069891698926989369894698956989669897698986989969900699016990269903699046990569906699076990869909699106991169912699136991469915699166991769918699196992069921699226992369924699256992669927699286992969930699316993269933699346993569936699376993869939699406994169942699436994469945699466994769948699496995069951699526995369954699556995669957699586995969960699616996269963699646996569966699676996869969699706997169972699736997469975699766997769978699796998069981699826998369984699856998669987699886998969990699916999269993699946999569996699976999869999700007000170002700037000470005700067000770008700097001070011700127001370014700157001670017700187001970020700217002270023700247002570026700277002870029700307003170032700337003470035700367003770038700397004070041700427004370044700457004670047700487004970050700517005270053700547005570056700577005870059700607006170062700637006470065700667006770068700697007070071700727007370074700757007670077700787007970080700817008270083700847008570086700877008870089700907009170092700937009470095700967009770098700997010070101701027010370104701057010670107701087010970110701117011270113701147011570116701177011870119701207012170122701237012470125701267012770128701297013070131701327013370134701357013670137701387013970140701417014270143701447014570146701477014870149701507015170152701537015470155701567015770158701597016070161701627016370164701657016670167701687016970170701717017270173701747017570176701777017870179701807018170182701837018470185701867018770188701897019070191701927019370194701957019670197701987019970200702017020270203702047020570206702077020870209702107021170212702137021470215702167021770218702197022070221702227022370224702257022670227702287022970230702317023270233702347023570236702377023870239702407024170242702437024470245702467024770248702497025070251702527025370254702557025670257702587025970260702617026270263702647026570266702677026870269702707027170272702737027470275702767027770278702797028070281702827028370284702857028670287702887028970290702917029270293702947029570296702977029870299703007030170302703037030470305703067030770308703097031070311703127031370314703157031670317703187031970320703217032270323703247032570326703277032870329703307033170332703337033470335703367033770338703397034070341703427034370344703457034670347703487034970350703517035270353703547035570356703577035870359703607036170362703637036470365703667036770368703697037070371703727037370374703757037670377703787037970380703817038270383703847038570386703877038870389703907039170392703937039470395703967039770398703997040070401704027040370404704057040670407704087040970410704117041270413704147041570416704177041870419704207042170422704237042470425704267042770428704297043070431704327043370434704357043670437704387043970440704417044270443704447044570446704477044870449704507045170452704537045470455704567045770458704597046070461704627046370464704657046670467704687046970470704717047270473704747047570476704777047870479704807048170482704837048470485704867048770488704897049070491704927049370494704957049670497704987049970500705017050270503705047050570506705077050870509705107051170512705137051470515705167051770518705197052070521705227052370524705257052670527705287052970530705317053270533705347053570536705377053870539705407054170542705437054470545705467054770548705497055070551705527055370554705557055670557705587055970560705617056270563705647056570566705677056870569705707057170572705737057470575705767057770578705797058070581705827058370584705857058670587705887058970590705917059270593705947059570596705977059870599706007060170602706037060470605706067060770608706097061070611706127061370614706157061670617706187061970620706217062270623706247062570626706277062870629706307063170632706337063470635706367063770638706397064070641706427064370644706457064670647706487064970650706517065270653706547065570656706577065870659706607066170662706637066470665706667066770668706697067070671706727067370674706757067670677706787067970680706817068270683706847068570686706877068870689706907069170692706937069470695706967069770698706997070070701707027070370704707057070670707707087070970710707117071270713707147071570716707177071870719707207072170722707237072470725707267072770728707297073070731707327073370734707357073670737707387073970740707417074270743707447074570746707477074870749707507075170752707537075470755707567075770758707597076070761707627076370764707657076670767707687076970770707717077270773707747077570776707777077870779707807078170782707837078470785707867078770788707897079070791707927079370794707957079670797707987079970800708017080270803708047080570806708077080870809708107081170812708137081470815708167081770818708197082070821708227082370824708257082670827708287082970830708317083270833708347083570836708377083870839708407084170842708437084470845708467084770848708497085070851708527085370854708557085670857708587085970860708617086270863708647086570866708677086870869708707087170872708737087470875708767087770878708797088070881708827088370884708857088670887708887088970890708917089270893708947089570896708977089870899709007090170902709037090470905709067090770908709097091070911709127091370914709157091670917709187091970920709217092270923709247092570926709277092870929709307093170932709337093470935709367093770938709397094070941709427094370944709457094670947709487094970950709517095270953709547095570956709577095870959709607096170962709637096470965709667096770968709697097070971709727097370974709757097670977709787097970980709817098270983709847098570986709877098870989709907099170992709937099470995709967099770998709997100071001710027100371004710057100671007710087100971010710117101271013710147101571016710177101871019710207102171022710237102471025710267102771028710297103071031710327103371034710357103671037710387103971040710417104271043710447104571046710477104871049710507105171052710537105471055710567105771058710597106071061710627106371064710657106671067710687106971070710717107271073710747107571076710777107871079710807108171082710837108471085710867108771088710897109071091710927109371094710957109671097710987109971100711017110271103711047110571106711077110871109711107111171112711137111471115711167111771118711197112071121711227112371124711257112671127711287112971130711317113271133711347113571136711377113871139711407114171142711437114471145711467114771148711497115071151711527115371154711557115671157711587115971160711617116271163711647116571166711677116871169711707117171172711737117471175711767117771178711797118071181711827118371184711857118671187711887118971190711917119271193711947119571196711977119871199712007120171202712037120471205712067120771208712097121071211712127121371214712157121671217712187121971220712217122271223712247122571226712277122871229712307123171232712337123471235712367123771238712397124071241712427124371244712457124671247712487124971250712517125271253712547125571256712577125871259712607126171262712637126471265712667126771268712697127071271712727127371274712757127671277712787127971280712817128271283712847128571286712877128871289712907129171292712937129471295712967129771298712997130071301713027130371304713057130671307713087130971310713117131271313713147131571316713177131871319713207132171322713237132471325713267132771328713297133071331713327133371334713357133671337713387133971340713417134271343713447134571346713477134871349713507135171352713537135471355713567135771358713597136071361713627136371364713657136671367713687136971370713717137271373713747137571376713777137871379713807138171382713837138471385713867138771388713897139071391713927139371394713957139671397713987139971400714017140271403714047140571406714077140871409714107141171412714137141471415714167141771418714197142071421714227142371424714257142671427714287142971430714317143271433714347143571436714377143871439714407144171442714437144471445714467144771448714497145071451714527145371454714557145671457714587145971460714617146271463714647146571466714677146871469714707147171472714737147471475714767147771478714797148071481714827148371484714857148671487714887148971490714917149271493714947149571496714977149871499715007150171502715037150471505715067150771508715097151071511715127151371514715157151671517715187151971520715217152271523715247152571526715277152871529715307153171532715337153471535715367153771538715397154071541715427154371544715457154671547715487154971550715517155271553715547155571556715577155871559715607156171562715637156471565715667156771568715697157071571715727157371574715757157671577715787157971580715817158271583715847158571586715877158871589715907159171592715937159471595715967159771598715997160071601716027160371604716057160671607716087160971610716117161271613716147161571616716177161871619716207162171622716237162471625716267162771628716297163071631716327163371634716357163671637716387163971640716417164271643716447164571646716477164871649716507165171652716537165471655716567165771658716597166071661716627166371664716657166671667716687166971670716717167271673716747167571676716777167871679716807168171682716837168471685716867168771688716897169071691716927169371694716957169671697716987169971700717017170271703717047170571706717077170871709717107171171712717137171471715717167171771718717197172071721717227172371724717257172671727717287172971730717317173271733717347173571736717377173871739717407174171742717437174471745717467174771748717497175071751717527175371754717557175671757717587175971760717617176271763717647176571766717677176871769717707177171772717737177471775717767177771778717797178071781717827178371784717857178671787717887178971790717917179271793717947179571796717977179871799718007180171802718037180471805718067180771808718097181071811718127181371814718157181671817718187181971820718217182271823718247182571826718277182871829718307183171832718337183471835718367183771838718397184071841718427184371844718457184671847718487184971850718517185271853718547185571856718577185871859718607186171862718637186471865718667186771868718697187071871718727187371874718757187671877718787187971880718817188271883718847188571886718877188871889718907189171892718937189471895718967189771898718997190071901719027190371904719057190671907719087190971910719117191271913719147191571916719177191871919719207192171922719237192471925719267192771928719297193071931719327193371934719357193671937719387193971940719417194271943719447194571946719477194871949719507195171952719537195471955719567195771958719597196071961719627196371964719657196671967719687196971970719717197271973719747197571976719777197871979719807198171982719837198471985719867198771988719897199071991719927199371994719957199671997719987199972000720017200272003720047200572006720077200872009720107201172012720137201472015720167201772018720197202072021720227202372024720257202672027720287202972030720317203272033720347203572036720377203872039720407204172042720437204472045720467204772048720497205072051720527205372054720557205672057720587205972060720617206272063720647206572066720677206872069720707207172072720737207472075720767207772078720797208072081720827208372084720857208672087720887208972090720917209272093720947209572096720977209872099721007210172102721037210472105721067210772108721097211072111721127211372114721157211672117721187211972120721217212272123721247212572126721277212872129721307213172132721337213472135721367213772138721397214072141721427214372144721457214672147721487214972150721517215272153721547215572156721577215872159721607216172162721637216472165721667216772168721697217072171721727217372174721757217672177721787217972180721817218272183721847218572186721877218872189721907219172192721937219472195721967219772198721997220072201722027220372204722057220672207722087220972210722117221272213722147221572216722177221872219722207222172222722237222472225722267222772228722297223072231722327223372234722357223672237722387223972240722417224272243722447224572246722477224872249722507225172252722537225472255722567225772258722597226072261722627226372264722657226672267722687226972270722717227272273722747227572276722777227872279722807228172282722837228472285722867228772288722897229072291722927229372294722957229672297722987229972300723017230272303723047230572306723077230872309723107231172312723137231472315723167231772318723197232072321723227232372324723257232672327723287232972330723317233272333723347233572336723377233872339723407234172342723437234472345723467234772348723497235072351723527235372354723557235672357723587235972360723617236272363723647236572366723677236872369723707237172372723737237472375723767237772378723797238072381723827238372384723857238672387723887238972390723917239272393723947239572396723977239872399724007240172402724037240472405724067240772408724097241072411724127241372414724157241672417724187241972420724217242272423724247242572426724277242872429724307243172432724337243472435724367243772438724397244072441724427244372444724457244672447724487244972450724517245272453724547245572456724577245872459724607246172462724637246472465724667246772468724697247072471724727247372474724757247672477724787247972480724817248272483724847248572486724877248872489724907249172492724937249472495724967249772498724997250072501725027250372504725057250672507725087250972510725117251272513725147251572516725177251872519725207252172522725237252472525725267252772528725297253072531725327253372534725357253672537725387253972540725417254272543725447254572546725477254872549725507255172552725537255472555725567255772558725597256072561725627256372564725657256672567725687256972570725717257272573725747257572576725777257872579725807258172582725837258472585725867258772588725897259072591725927259372594725957259672597725987259972600726017260272603726047260572606726077260872609726107261172612726137261472615726167261772618726197262072621726227262372624726257262672627726287262972630726317263272633726347263572636726377263872639726407264172642726437264472645726467264772648726497265072651726527265372654726557265672657726587265972660726617266272663726647266572666726677266872669726707267172672726737267472675726767267772678726797268072681726827268372684726857268672687726887268972690726917269272693726947269572696726977269872699727007270172702727037270472705727067270772708727097271072711727127271372714727157271672717727187271972720727217272272723727247272572726727277272872729727307273172732727337273472735727367273772738727397274072741727427274372744727457274672747727487274972750727517275272753727547275572756727577275872759727607276172762727637276472765727667276772768727697277072771727727277372774727757277672777727787277972780727817278272783727847278572786727877278872789727907279172792727937279472795727967279772798727997280072801728027280372804728057280672807728087280972810728117281272813728147281572816728177281872819728207282172822728237282472825728267282772828728297283072831728327283372834728357283672837728387283972840728417284272843728447284572846728477284872849728507285172852728537285472855728567285772858728597286072861728627286372864728657286672867728687286972870728717287272873728747287572876728777287872879728807288172882728837288472885728867288772888728897289072891728927289372894728957289672897728987289972900729017290272903729047290572906729077290872909729107291172912729137291472915729167291772918729197292072921729227292372924729257292672927729287292972930729317293272933729347293572936729377293872939729407294172942729437294472945729467294772948729497295072951729527295372954729557295672957729587295972960729617296272963729647296572966729677296872969729707297172972729737297472975729767297772978729797298072981729827298372984729857298672987729887298972990729917299272993729947299572996729977299872999730007300173002730037300473005730067300773008730097301073011730127301373014730157301673017730187301973020730217302273023730247302573026730277302873029730307303173032730337303473035730367303773038730397304073041730427304373044730457304673047730487304973050730517305273053730547305573056730577305873059730607306173062730637306473065730667306773068730697307073071730727307373074730757307673077730787307973080730817308273083730847308573086730877308873089730907309173092730937309473095730967309773098730997310073101731027310373104731057310673107731087310973110731117311273113731147311573116731177311873119731207312173122731237312473125731267312773128731297313073131731327313373134731357313673137731387313973140731417314273143731447314573146731477314873149731507315173152731537315473155731567315773158731597316073161731627316373164731657316673167731687316973170731717317273173731747317573176731777317873179731807318173182731837318473185731867318773188731897319073191731927319373194731957319673197731987319973200732017320273203732047320573206732077320873209732107321173212732137321473215732167321773218732197322073221732227322373224
  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. #ifdef WOLFSSL_STATIC_MEMORY
  593. #define TEST_LSM_STATIC_SIZE 440000
  594. /* Create new bucket list, using the default list, adding
  595. * one dang large buffer size. */
  596. #define TEST_LSM_DEF_BUCKETS (WOLFMEM_DEF_BUCKETS+1)
  597. #define TEST_LSM_BUCKETS WOLFMEM_BUCKETS,(LARGEST_MEM_BUCKET*2)
  598. #define TEST_LSM_DIST WOLFMEM_DIST,1
  599. #endif
  600. static int test_wc_LoadStaticMemory_ex(void)
  601. {
  602. EXPECT_DECLS;
  603. #ifdef WOLFSSL_STATIC_MEMORY
  604. byte staticMemory[TEST_LSM_STATIC_SIZE];
  605. word32 sizeList[TEST_LSM_DEF_BUCKETS] = { TEST_LSM_BUCKETS };
  606. word32 distList[TEST_LSM_DEF_BUCKETS] = { TEST_LSM_DIST };
  607. WOLFSSL_HEAP_HINT* heap;
  608. /* For this test, the size and dist lists will be the ones configured
  609. * for the build, or default. The value of WOLFMEM_DEF_BUCKETS is 9,
  610. * so these lists are 10 long. For most tests, the value of
  611. * WOLFMEM_DEF_BUCKETS is used. There's a test case where one is added
  612. * to that, to make sure the list size is larger than
  613. * WOLFMEM_MAX_BUCKETS. */
  614. /* Pass in zero everything. */
  615. ExpectIntEQ(wc_LoadStaticMemory_ex(NULL, 0, NULL, NULL, NULL, 0, 0, 0),
  616. BAD_FUNC_ARG);
  617. /* Set the heap pointer to NULL. */
  618. ExpectIntEQ(wc_LoadStaticMemory_ex(NULL,
  619. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  620. staticMemory, (word32)sizeof(staticMemory),
  621. 0, 1),
  622. BAD_FUNC_ARG);
  623. /* Set other pointer values to NULL one at a time. */
  624. heap = NULL;
  625. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  626. WOLFMEM_DEF_BUCKETS, NULL, distList,
  627. staticMemory, (word32)sizeof(staticMemory),
  628. 0, 1),
  629. BAD_FUNC_ARG);
  630. heap = NULL;
  631. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  632. WOLFMEM_DEF_BUCKETS, sizeList, NULL,
  633. staticMemory, (word32)sizeof(staticMemory),
  634. 0, 1),
  635. BAD_FUNC_ARG);
  636. heap = NULL;
  637. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  638. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  639. NULL, (word32)sizeof(staticMemory),
  640. 0, 1),
  641. BAD_FUNC_ARG);
  642. /* Set the size of the static buffer to 0. */
  643. heap = NULL;
  644. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  645. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  646. staticMemory, 0,
  647. 0, 1),
  648. BUFFER_E);
  649. /* Set the size of the static buffer to one less than minimum allowed. */
  650. heap = NULL;
  651. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  652. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  653. staticMemory,
  654. (word32)(sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT)) - 1,
  655. 0, 1),
  656. BUFFER_E);
  657. /* Set the size of the static buffer to exactly the minimum size. */
  658. heap = NULL;
  659. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  660. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  661. staticMemory,
  662. (word32)(sizeof(WOLFSSL_HEAP) + sizeof(WOLFSSL_HEAP_HINT)),
  663. 0, 1),
  664. 0);
  665. wc_UnloadStaticMemory(heap);
  666. /* Use more buckets than able. Success case. */
  667. heap = NULL;
  668. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  669. WOLFMEM_DEF_BUCKETS*2, sizeList, distList,
  670. staticMemory, (word32)sizeof(staticMemory),
  671. 0, 1),
  672. 0);
  673. wc_UnloadStaticMemory(heap);
  674. /* Success case. */
  675. heap = NULL;
  676. ExpectIntEQ(wc_LoadStaticMemory_ex(&heap,
  677. WOLFMEM_DEF_BUCKETS, sizeList, distList,
  678. staticMemory, (word32)sizeof(staticMemory),
  679. 0, 1),
  680. 0);
  681. wc_UnloadStaticMemory(heap);
  682. #endif /* WOLFSSL_STATIC_MEMORY */
  683. return EXPECT_RESULT();
  684. }
  685. /*----------------------------------------------------------------------------*
  686. | Platform dependent function test
  687. *----------------------------------------------------------------------------*/
  688. static int test_fileAccess(void)
  689. {
  690. EXPECT_DECLS;
  691. #if defined(WOLFSSL_TEST_PLATFORMDEPEND) && !defined(NO_FILESYSTEM)
  692. const char *fname[] = {
  693. svrCertFile, svrKeyFile, caCertFile,
  694. eccCertFile, eccKeyFile, eccRsaCertFile,
  695. cliCertFile, cliCertDerFile, cliKeyFile,
  696. dhParamFile,
  697. cliEccKeyFile, cliEccCertFile, caEccCertFile, edCertFile, edKeyFile,
  698. cliEdCertFile, cliEdKeyFile, caEdCertFile,
  699. NULL
  700. };
  701. const char derfile[] = "./certs/server-cert.der";
  702. XFILE f = XBADFILE;
  703. size_t sz;
  704. byte *buff = NULL;
  705. int i;
  706. ExpectTrue(XFOPEN("badfilename", "rb") == XBADFILE);
  707. for (i=0; EXPECT_SUCCESS() && fname[i] != NULL ; i++) {
  708. ExpectTrue((f = XFOPEN(fname[i], "rb")) != XBADFILE);
  709. XFCLOSE(f);
  710. }
  711. ExpectTrue((f = XFOPEN(derfile, "rb")) != XBADFILE);
  712. ExpectTrue(XFSEEK(f, 0, XSEEK_END) == 0);
  713. ExpectIntGE(sz = (size_t) XFTELL(f), sizeof_server_cert_der_2048);
  714. ExpectTrue(XFSEEK(f, 0, XSEEK_SET) == 0);
  715. ExpectTrue((buff = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE)) != NULL);
  716. ExpectTrue(XFREAD(buff, 1, sz, f) == sz);
  717. ExpectIntEQ(XMEMCMP(server_cert_der_2048, buff, sz), 0);
  718. XFREE(buff, NULL, DYNAMIC_TYPE_FILE);
  719. XFCLOSE(f);
  720. #endif
  721. return EXPECT_RESULT();
  722. }
  723. /*----------------------------------------------------------------------------*
  724. | Method Allocators
  725. *----------------------------------------------------------------------------*/
  726. static int test_wolfSSL_Method_Allocators(void)
  727. {
  728. EXPECT_DECLS;
  729. #define TEST_METHOD_ALLOCATOR(allocator, condition) \
  730. do { \
  731. WOLFSSL_METHOD *method = NULL; \
  732. condition(method = allocator()); \
  733. XFREE(method, 0, DYNAMIC_TYPE_METHOD); \
  734. } while (0)
  735. #define TEST_VALID_METHOD_ALLOCATOR(a) \
  736. TEST_METHOD_ALLOCATOR(a, ExpectNotNull)
  737. #define TEST_INVALID_METHOD_ALLOCATOR(a) \
  738. TEST_METHOD_ALLOCATOR(a, ExpectNull)
  739. #ifndef NO_OLD_TLS
  740. #ifdef WOLFSSL_ALLOW_SSLV3
  741. #ifndef NO_WOLFSSL_SERVER
  742. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_server_method);
  743. #endif
  744. #ifndef NO_WOLFSSL_CLIENT
  745. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv3_client_method);
  746. #endif
  747. #endif
  748. #ifdef WOLFSSL_ALLOW_TLSV10
  749. #ifndef NO_WOLFSSL_SERVER
  750. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_server_method);
  751. #endif
  752. #ifndef NO_WOLFSSL_CLIENT
  753. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_client_method);
  754. #endif
  755. #endif
  756. #ifndef NO_WOLFSSL_SERVER
  757. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_server_method);
  758. #endif
  759. #ifndef NO_WOLFSSL_CLIENT
  760. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_client_method);
  761. #endif
  762. #endif /* !NO_OLD_TLS */
  763. #ifndef WOLFSSL_NO_TLS12
  764. #ifndef NO_WOLFSSL_SERVER
  765. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_server_method);
  766. #endif
  767. #ifndef NO_WOLFSSL_CLIENT
  768. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_client_method);
  769. #endif
  770. #endif /* !WOLFSSL_NO_TLS12 */
  771. #ifdef WOLFSSL_TLS13
  772. #ifndef NO_WOLFSSL_SERVER
  773. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_server_method);
  774. #endif
  775. #ifndef NO_WOLFSSL_CLIENT
  776. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_client_method);
  777. #endif
  778. #endif /* WOLFSSL_TLS13 */
  779. #ifndef NO_WOLFSSL_SERVER
  780. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_server_method);
  781. #endif
  782. #ifndef NO_WOLFSSL_CLIENT
  783. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_client_method);
  784. #endif
  785. #ifdef WOLFSSL_DTLS
  786. #ifndef NO_OLD_TLS
  787. #ifndef NO_WOLFSSL_SERVER
  788. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_server_method);
  789. #endif
  790. #ifndef NO_WOLFSSL_CLIENT
  791. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_client_method);
  792. #endif
  793. #endif
  794. #ifndef WOLFSSL_NO_TLS12
  795. #ifndef NO_WOLFSSL_SERVER
  796. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_server_method);
  797. #endif
  798. #ifndef NO_WOLFSSL_CLIENT
  799. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_client_method);
  800. #endif
  801. #endif
  802. #endif /* WOLFSSL_DTLS */
  803. #if !defined(NO_OLD_TLS) && defined(OPENSSL_EXTRA)
  804. /* Stubs */
  805. #ifndef NO_WOLFSSL_SERVER
  806. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_server_method);
  807. #endif
  808. #ifndef NO_WOLFSSL_CLIENT
  809. TEST_INVALID_METHOD_ALLOCATOR(wolfSSLv2_client_method);
  810. #endif
  811. #endif
  812. /* Test Either Method (client or server) */
  813. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  814. TEST_VALID_METHOD_ALLOCATOR(wolfSSLv23_method);
  815. #ifndef NO_OLD_TLS
  816. #ifdef WOLFSSL_ALLOW_TLSV10
  817. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_method);
  818. #endif
  819. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_1_method);
  820. #endif /* !NO_OLD_TLS */
  821. #ifndef WOLFSSL_NO_TLS12
  822. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_2_method);
  823. #endif /* !WOLFSSL_NO_TLS12 */
  824. #ifdef WOLFSSL_TLS13
  825. TEST_VALID_METHOD_ALLOCATOR(wolfTLSv1_3_method);
  826. #endif /* WOLFSSL_TLS13 */
  827. #ifdef WOLFSSL_DTLS
  828. TEST_VALID_METHOD_ALLOCATOR(wolfDTLS_method);
  829. #ifndef NO_OLD_TLS
  830. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_method);
  831. #endif /* !NO_OLD_TLS */
  832. #ifndef WOLFSSL_NO_TLS12
  833. TEST_VALID_METHOD_ALLOCATOR(wolfDTLSv1_2_method);
  834. #endif /* !WOLFSSL_NO_TLS12 */
  835. #endif /* WOLFSSL_DTLS */
  836. #endif /* OPENSSL_EXTRA || WOLFSSL_EITHER_SIDE */
  837. return EXPECT_RESULT();
  838. }
  839. #if defined(WOLFSSL_DUAL_ALG_CERTS) && !defined(NO_FILESYSTEM)
  840. /*----------------------------------------------------------------------------*
  841. | Dual algorithm Certificate Tests
  842. *----------------------------------------------------------------------------*/
  843. #define LARGE_TEMP_SZ 4096
  844. /* To better understand this, please see the X9.146 example in wolfssl-examples
  845. * repo. */
  846. static int do_dual_alg_root_certgen(byte **out, char *caKeyFile,
  847. char *sapkiFile, char *altPrivFile)
  848. {
  849. EXPECT_DECLS;
  850. FILE* file = NULL;
  851. Cert newCert;
  852. DecodedCert preTBS;
  853. byte caKeyBuf[LARGE_TEMP_SZ];
  854. word32 caKeySz = LARGE_TEMP_SZ;
  855. byte sapkiBuf[LARGE_TEMP_SZ];
  856. word32 sapkiSz = LARGE_TEMP_SZ;
  857. byte altPrivBuf[LARGE_TEMP_SZ];
  858. word32 altPrivSz = LARGE_TEMP_SZ;
  859. byte altSigAlgBuf[LARGE_TEMP_SZ];
  860. word32 altSigAlgSz = LARGE_TEMP_SZ;
  861. byte scratchBuf[LARGE_TEMP_SZ];
  862. word32 scratchSz = LARGE_TEMP_SZ;
  863. byte preTbsBuf[LARGE_TEMP_SZ];
  864. word32 preTbsSz = LARGE_TEMP_SZ;
  865. byte altSigValBuf[LARGE_TEMP_SZ];
  866. word32 altSigValSz = LARGE_TEMP_SZ;
  867. byte *outBuf = NULL;
  868. word32 outSz = LARGE_TEMP_SZ;
  869. WC_RNG rng;
  870. RsaKey caKey;
  871. ecc_key altCaKey;
  872. word32 idx = 0;
  873. ExpectNotNull(outBuf = (byte*)XMALLOC(outSz, NULL,
  874. DYNAMIC_TYPE_TMP_BUFFER));
  875. ExpectIntEQ(wc_InitRng(&rng), 0);
  876. XMEMSET(caKeyBuf, 0, caKeySz);
  877. ExpectNotNull(file = fopen(caKeyFile, "rb"));
  878. ExpectIntGT(caKeySz = (word32)fread(caKeyBuf, 1, caKeySz, file), 0);
  879. if (file) {
  880. fclose(file);
  881. file = NULL;
  882. }
  883. ExpectIntEQ(wc_InitRsaKey_ex(&caKey, NULL, INVALID_DEVID), 0);
  884. idx = 0;
  885. ExpectIntEQ(wc_RsaPrivateKeyDecode(caKeyBuf, &idx, &caKey, caKeySz),
  886. 0);
  887. XMEMSET(sapkiBuf, 0, sapkiSz);
  888. ExpectNotNull(file = fopen(sapkiFile, "rb"));
  889. ExpectIntGT(sapkiSz = (word32)fread(sapkiBuf, 1, sapkiSz, file), 0);
  890. if (file) {
  891. fclose(file);
  892. file = NULL;
  893. }
  894. XMEMSET(altPrivBuf, 0, altPrivSz);
  895. ExpectNotNull(file = fopen(altPrivFile, "rb"));
  896. ExpectIntGT(altPrivSz = (word32)fread(altPrivBuf, 1, altPrivSz, file), 0);
  897. if (file) {
  898. fclose(file);
  899. file = NULL;
  900. }
  901. wc_ecc_init(&altCaKey);
  902. idx = 0;
  903. ExpectIntEQ(wc_EccPrivateKeyDecode(altPrivBuf, &idx, &altCaKey,
  904. (word32)altPrivSz), 0);
  905. XMEMSET(altSigAlgBuf, 0, altSigAlgSz);
  906. ExpectIntGT(altSigAlgSz = SetAlgoID(CTC_SHA256wECDSA, altSigAlgBuf,
  907. oidSigType, 0), 0);
  908. wc_InitCert(&newCert);
  909. strncpy(newCert.subject.country, "US", CTC_NAME_SIZE);
  910. strncpy(newCert.subject.state, "MT", CTC_NAME_SIZE);
  911. strncpy(newCert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  912. strncpy(newCert.subject.org, "wolfSSL", CTC_NAME_SIZE);
  913. strncpy(newCert.subject.unit, "Engineering", CTC_NAME_SIZE);
  914. strncpy(newCert.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
  915. strncpy(newCert.subject.email, "root@wolfssl.com", CTC_NAME_SIZE);
  916. newCert.sigType = CTC_SHA256wRSA;
  917. newCert.isCA = 1;
  918. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "1.2.3.4.5",
  919. (const byte *)"This is NOT a critical extension", 32), 0);
  920. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.72", sapkiBuf,
  921. sapkiSz), 0);
  922. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.73", altSigAlgBuf,
  923. altSigAlgSz), 0);
  924. XMEMSET(scratchBuf, 0, scratchSz);
  925. ExpectIntGT(scratchSz = wc_MakeSelfCert(&newCert, scratchBuf, scratchSz,
  926. &caKey, &rng), 0);
  927. wc_InitDecodedCert(&preTBS, scratchBuf, scratchSz, 0);
  928. ExpectIntEQ(wc_ParseCert(&preTBS, CERT_TYPE, NO_VERIFY, NULL), 0);
  929. XMEMSET(preTbsBuf, 0, preTbsSz);
  930. ExpectIntGT(preTbsSz = wc_GeneratePreTBS(&preTBS, preTbsBuf, preTbsSz), 0);
  931. XMEMSET(altSigValBuf, 0, altSigValSz);
  932. ExpectIntGT(altSigValSz = wc_MakeSigWithBitStr(altSigValBuf, altSigValSz,
  933. CTC_SHA256wECDSA, preTbsBuf, preTbsSz, ECC_TYPE, &altCaKey,
  934. &rng), 0);
  935. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.74", altSigValBuf,
  936. altSigValSz), 0);
  937. /* Finally, generate the new certificate. */
  938. XMEMSET(outBuf, 0, outSz);
  939. ExpectIntGT(outSz = wc_MakeSelfCert(&newCert, outBuf, outSz, &caKey, &rng),
  940. 0);
  941. *out = outBuf;
  942. wc_FreeRsaKey(&caKey);
  943. wc_FreeRng(&rng);
  944. wc_FreeDecodedCert(&preTBS);
  945. return outSz;
  946. }
  947. static int do_dual_alg_server_certgen(byte **out, char *caKeyFile,
  948. char *sapkiFile, char *altPrivFile,
  949. char *serverKeyFile,
  950. byte *caCertBuf, int caCertSz)
  951. {
  952. EXPECT_DECLS;
  953. FILE* file = NULL;
  954. Cert newCert;
  955. DecodedCert preTBS;
  956. byte serverKeyBuf[LARGE_TEMP_SZ];
  957. word32 serverKeySz = LARGE_TEMP_SZ;
  958. byte caKeyBuf[LARGE_TEMP_SZ];
  959. word32 caKeySz = LARGE_TEMP_SZ;
  960. byte sapkiBuf[LARGE_TEMP_SZ];
  961. word32 sapkiSz = LARGE_TEMP_SZ;
  962. byte altPrivBuf[LARGE_TEMP_SZ];
  963. word32 altPrivSz = LARGE_TEMP_SZ;
  964. byte altSigAlgBuf[LARGE_TEMP_SZ];
  965. word32 altSigAlgSz = LARGE_TEMP_SZ;
  966. byte scratchBuf[LARGE_TEMP_SZ];
  967. word32 scratchSz = LARGE_TEMP_SZ;
  968. byte preTbsBuf[LARGE_TEMP_SZ];
  969. word32 preTbsSz = LARGE_TEMP_SZ;
  970. byte altSigValBuf[LARGE_TEMP_SZ];
  971. word32 altSigValSz = LARGE_TEMP_SZ;
  972. byte *outBuf = NULL;
  973. word32 outSz = LARGE_TEMP_SZ;
  974. WC_RNG rng;
  975. RsaKey caKey;
  976. RsaKey serverKey;
  977. ecc_key altCaKey;
  978. word32 idx = 0;
  979. ExpectNotNull(outBuf = (byte*)XMALLOC(outSz, NULL,
  980. DYNAMIC_TYPE_TMP_BUFFER));
  981. ExpectIntEQ(wc_InitRng(&rng), 0);
  982. XMEMSET(serverKeyBuf, 0, serverKeySz);
  983. ExpectNotNull(file = fopen(serverKeyFile, "rb"));
  984. ExpectIntGT(serverKeySz = (word32)fread(serverKeyBuf, 1, serverKeySz, file),
  985. 0);
  986. if (file) {
  987. fclose(file);
  988. file = NULL;
  989. }
  990. ExpectIntEQ(wc_InitRsaKey_ex(&serverKey, NULL, INVALID_DEVID), 0);
  991. idx = 0;
  992. ExpectIntEQ(wc_RsaPrivateKeyDecode(serverKeyBuf, &idx, &serverKey,
  993. (word32)serverKeySz), 0);
  994. XMEMSET(caKeyBuf, 0, caKeySz);
  995. ExpectNotNull(file = fopen(caKeyFile, "rb"));
  996. ExpectIntGT(caKeySz = (word32)fread(caKeyBuf, 1, caKeySz, file), 0);
  997. if (file) {
  998. fclose(file);
  999. file = NULL;
  1000. }
  1001. ExpectIntEQ(wc_InitRsaKey_ex(&caKey, NULL, INVALID_DEVID), 0);
  1002. idx = 0;
  1003. ExpectIntEQ(wc_RsaPrivateKeyDecode(caKeyBuf, &idx, &caKey,
  1004. (word32)caKeySz), 0);
  1005. XMEMSET(sapkiBuf, 0, sapkiSz);
  1006. ExpectNotNull(file = fopen(sapkiFile, "rb"));
  1007. ExpectIntGT(sapkiSz = (word32)fread(sapkiBuf, 1, sapkiSz, file), 0);
  1008. if (file) {
  1009. fclose(file);
  1010. file = NULL;
  1011. }
  1012. XMEMSET(altPrivBuf, 0, altPrivSz);
  1013. ExpectNotNull(file = fopen(altPrivFile, "rb"));
  1014. ExpectIntGT(altPrivSz = (word32)fread(altPrivBuf, 1, altPrivSz, file), 0);
  1015. if (file) {
  1016. fclose(file);
  1017. file = NULL;
  1018. }
  1019. wc_ecc_init(&altCaKey);
  1020. idx = 0;
  1021. ExpectIntEQ(wc_EccPrivateKeyDecode(altPrivBuf, &idx, &altCaKey,
  1022. (word32)altPrivSz), 0);
  1023. XMEMSET(altSigAlgBuf, 0, altSigAlgSz);
  1024. ExpectIntGT(altSigAlgSz = SetAlgoID(CTC_SHA256wECDSA, altSigAlgBuf,
  1025. oidSigType, 0), 0);
  1026. wc_InitCert(&newCert);
  1027. strncpy(newCert.subject.country, "US", CTC_NAME_SIZE);
  1028. strncpy(newCert.subject.state, "MT", CTC_NAME_SIZE);
  1029. strncpy(newCert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  1030. strncpy(newCert.subject.org, "wolfSSL", CTC_NAME_SIZE);
  1031. strncpy(newCert.subject.unit, "Engineering", CTC_NAME_SIZE);
  1032. strncpy(newCert.subject.commonName, "www.wolfssl.com", CTC_NAME_SIZE);
  1033. strncpy(newCert.subject.email, "server@wolfssl.com", CTC_NAME_SIZE);
  1034. newCert.sigType = CTC_SHA256wRSA;
  1035. newCert.isCA = 0;
  1036. ExpectIntEQ(wc_SetIssuerBuffer(&newCert, caCertBuf, caCertSz), 0);
  1037. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "1.2.3.4.5",
  1038. (const byte *)"This is NOT a critical extension", 32), 0);
  1039. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.72", sapkiBuf,
  1040. sapkiSz), 0);
  1041. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.73", altSigAlgBuf,
  1042. altSigAlgSz), 0);
  1043. XMEMSET(scratchBuf, 0, scratchSz);
  1044. ExpectIntGT(wc_MakeCert(&newCert, scratchBuf, scratchSz, &serverKey, NULL,
  1045. &rng), 0);
  1046. ExpectIntGT(scratchSz = wc_SignCert(newCert.bodySz, newCert.sigType,
  1047. scratchBuf, scratchSz, &caKey, NULL, &rng), 0);
  1048. wc_InitDecodedCert(&preTBS, scratchBuf, scratchSz, 0);
  1049. ExpectIntEQ(wc_ParseCert(&preTBS, CERT_TYPE, NO_VERIFY, NULL), 0);
  1050. XMEMSET(preTbsBuf, 0, preTbsSz);
  1051. ExpectIntGT(preTbsSz = wc_GeneratePreTBS(&preTBS, preTbsBuf, preTbsSz), 0);
  1052. XMEMSET(altSigValBuf, 0, altSigValSz);
  1053. ExpectIntGT(altSigValSz = wc_MakeSigWithBitStr(altSigValBuf, altSigValSz,
  1054. CTC_SHA256wECDSA, preTbsBuf, preTbsSz, ECC_TYPE, &altCaKey,
  1055. &rng), 0);
  1056. ExpectIntEQ(wc_SetCustomExtension(&newCert, 0, "2.5.29.74",
  1057. altSigValBuf, altSigValSz), 0);
  1058. /* Finally, generate the new certificate. */
  1059. XMEMSET(outBuf, 0, outSz);
  1060. ExpectIntGT(wc_MakeCert(&newCert, outBuf, outSz, &serverKey, NULL, &rng),
  1061. 0);
  1062. ExpectIntGT(outSz = wc_SignCert(newCert.bodySz, newCert.sigType, outBuf,
  1063. outSz, &caKey, NULL, &rng), 0);
  1064. *out = outBuf;
  1065. wc_FreeRsaKey(&caKey);
  1066. wc_FreeRsaKey(&serverKey);
  1067. wc_FreeRng(&rng);
  1068. wc_FreeDecodedCert(&preTBS);
  1069. return outSz;
  1070. }
  1071. static int do_dual_alg_tls13_connection(byte *caCert, word32 caCertSz,
  1072. byte *serverCert, word32 serverCertSz,
  1073. byte *serverKey, word32 serverKeySz,
  1074. int negative_test)
  1075. {
  1076. EXPECT_DECLS;
  1077. WOLFSSL_CTX *ctx_c = NULL;
  1078. WOLFSSL_CTX *ctx_s = NULL;
  1079. WOLFSSL *ssl_c = NULL;
  1080. WOLFSSL *ssl_s = NULL;
  1081. struct test_memio_ctx test_ctx;
  1082. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  1083. ExpectIntEQ(test_memio_setup_ex(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  1084. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  1085. caCert, caCertSz, serverCert, serverCertSz,
  1086. serverKey, serverKeySz), 0);
  1087. if (negative_test) {
  1088. ExpectTrue(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0);
  1089. }
  1090. else {
  1091. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  1092. }
  1093. wolfSSL_free(ssl_c);
  1094. wolfSSL_free(ssl_s);
  1095. wolfSSL_CTX_free(ctx_c);
  1096. wolfSSL_CTX_free(ctx_s);
  1097. return EXPECT_RESULT();
  1098. }
  1099. static int extCount = 0;
  1100. static int myUnknownExtCallback(const word16* oid, word32 oidSz, int crit,
  1101. const unsigned char* der, word32 derSz)
  1102. {
  1103. (void) oid;
  1104. (void) oidSz;
  1105. (void) crit;
  1106. (void) der;
  1107. (void) derSz;
  1108. extCount ++;
  1109. /* Accept all extensions. This is only a test. Normally we would be much more
  1110. * careful about critical extensions. */
  1111. return 1;
  1112. }
  1113. static int test_dual_alg_support(void)
  1114. {
  1115. EXPECT_DECLS;
  1116. /* Root CA and server keys will be the same. This is only appropriate for
  1117. * testing. */
  1118. char keyFile[] = "./certs/ca-key.der";
  1119. char sapkiFile[] = "./certs/ecc-keyPub.der";
  1120. char altPrivFile[] = "./certs/ecc-key.der";
  1121. char wrongPrivFile[] = "./certs/ecc-client-key.der";
  1122. byte *serverKey = NULL;
  1123. size_t serverKeySz = 0;
  1124. byte *root = NULL;
  1125. int rootSz = 0;
  1126. byte *server = NULL;
  1127. int serverSz = 0;
  1128. WOLFSSL_CERT_MANAGER* cm = NULL;
  1129. ExpectIntEQ(load_file(keyFile, &serverKey, &serverKeySz), 0);
  1130. /* Base normal case. */
  1131. rootSz = do_dual_alg_root_certgen(&root, keyFile, sapkiFile, altPrivFile);
  1132. ExpectNotNull(root);
  1133. ExpectIntGT(rootSz, 0);
  1134. serverSz = do_dual_alg_server_certgen(&server, keyFile, sapkiFile,
  1135. altPrivFile, keyFile, root, rootSz);
  1136. ExpectNotNull(server);
  1137. ExpectIntGT(serverSz, 0);
  1138. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1139. server, serverSz, serverKey, (word32)serverKeySz, 0),
  1140. TEST_SUCCESS);
  1141. XFREE(root, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1142. XFREE(server, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1143. /* Now we try a negative case. Note that we use wrongPrivFile to generate
  1144. * the alternative signature and then set negative_test to true for the
  1145. * call to do_dual_alg_tls13_connection(). Its expecting a failed connection
  1146. * because the signature won't verify. The exception is if
  1147. * WOLFSSL_TRUST_PEER_CERT is defined. In that case, no verfication happens
  1148. * and this is no longer a negative test. */
  1149. rootSz = do_dual_alg_root_certgen(&root, keyFile, sapkiFile, wrongPrivFile);
  1150. ExpectNotNull(root);
  1151. ExpectIntGT(rootSz, 0);
  1152. serverSz = do_dual_alg_server_certgen(&server, keyFile, sapkiFile,
  1153. wrongPrivFile, keyFile, root, rootSz);
  1154. ExpectNotNull(server);
  1155. ExpectIntGT(serverSz, 0);
  1156. #ifdef WOLFSSL_TRUST_PEER_CERT
  1157. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1158. server, serverSz, serverKey, (word32)serverKeySz, 0),
  1159. TEST_SUCCESS);
  1160. #else
  1161. ExpectIntEQ(do_dual_alg_tls13_connection(root, rootSz,
  1162. server, serverSz, serverKey, (word32)serverKeySz, 1),
  1163. TEST_SUCCESS);
  1164. #endif
  1165. /* Lets see if CertManager can find the new extensions */
  1166. extCount = 0;
  1167. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  1168. wolfSSL_CertManagerSetUnknownExtCallback(cm, myUnknownExtCallback);
  1169. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, root, rootSz,
  1170. SSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1171. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, server, serverSz,
  1172. SSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1173. /* There is only 1 unknown extension (1.2.3.4.5). The other ones are known
  1174. * because they are for the dual alg extensions. */
  1175. ExpectIntEQ(extCount, 1);
  1176. wolfSSL_CertManagerFree(cm);
  1177. XFREE(root, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1178. XFREE(server, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1179. free(serverKey);
  1180. return EXPECT_RESULT();
  1181. }
  1182. #else
  1183. static int test_dual_alg_support(void)
  1184. {
  1185. return TEST_SKIPPED;
  1186. }
  1187. #endif /* WOLFSSL_DUAL_ALG_CERTS && !NO_FILESYSTEM */
  1188. /*----------------------------------------------------------------------------*
  1189. | Context
  1190. *----------------------------------------------------------------------------*/
  1191. #ifndef NO_WOLFSSL_SERVER
  1192. static int test_wolfSSL_CTX_new(void)
  1193. {
  1194. EXPECT_DECLS;
  1195. WOLFSSL_CTX *ctx;
  1196. WOLFSSL_METHOD* method;
  1197. ExpectNull(ctx = wolfSSL_CTX_new(NULL));
  1198. ExpectNotNull(method = wolfSSLv23_server_method());
  1199. ExpectNotNull(ctx = wolfSSL_CTX_new(method));
  1200. wolfSSL_CTX_free(ctx);
  1201. return EXPECT_RESULT();
  1202. }
  1203. #endif
  1204. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  1205. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  1206. static int test_for_double_Free(void)
  1207. {
  1208. EXPECT_DECLS;
  1209. WOLFSSL_CTX* ctx = NULL;
  1210. WOLFSSL* ssl = NULL;
  1211. int skipTest = 0;
  1212. const char* testCertFile;
  1213. const char* testKeyFile;
  1214. char optionsCiphers[] = "RC4-SHA:RC4-MD5:DES-CBC3-SHA:AES128-SHA:AES256-SHA"
  1215. ":NULL-SHA:NULL-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA:DHE-PSK-AES256-GCM"
  1216. "-SHA384:DHE-PSK-AES128-GCM-SHA256:PSK-AES256-GCM-SHA384:PSK-AES128-GCM-SHA256:"
  1217. "DHE-PSK-AES256-CBC-SHA384:DHE-PSK-AES128-CBC-SHA256:PSK-AES256-CBC-SHA384:PSK-"
  1218. "AES128-CBC-SHA256:PSK-AES128-CBC-SHA:PSK-AES256-CBC-SHA:DHE-PSK-AES128-CCM:DHE"
  1219. "-PSK-AES256-CCM:PSK-AES128-CCM:PSK-AES256-CCM:PSK-AES128-CCM-8:PSK-AES256-CCM-"
  1220. "8:DHE-PSK-NULL-SHA384:DHE-PSK-NULL-SHA256:PSK-NULL-SHA384:PSK-NULL-SHA256:PSK-"
  1221. "NULL-SHA:AES128-CCM-8:AES256-CCM-8:ECDHE-ECDSA-"
  1222. "AES128-CCM:ECDHE-ECDSA-AES128-CCM-8:ECDHE-ECDSA-AES256-CCM-8:ECDHE-RSA-AES128-"
  1223. "SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA:ECDHE-R"
  1224. "SA-RC4-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-RC4-SHA:ECDHE-ECDSA-DES-CBC3-SHA"
  1225. ":AES128-SHA256:AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA256:ECDH-"
  1226. "RSA-AES128-SHA:ECDH-RSA-AES256-SHA:ECDH-ECDSA-AES128-SHA:ECDH-ECDSA-AES256-SHA"
  1227. ":ECDH-RSA-RC4-SHA:ECDH-RSA-DES-CBC3-SHA:ECDH-ECDSA-RC4-SHA:ECDH-ECDSA-DES-CBC3"
  1228. "-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES"
  1229. "256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-E"
  1230. "CDSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDH-RSA-AES128-GCM-SHA25"
  1231. "6:ECDH-RSA-AES256-GCM-SHA384:ECDH-ECDSA-AES128-GCM-SHA256:ECDH-ECDSA-AES256-GC"
  1232. "M-SHA384:CAMELLIA128-SHA:DHE-RSA-CAMELLIA128-SHA:CAMELLIA256-SHA:DHE-RSA-CAMEL"
  1233. "LIA256-SHA:CAMELLIA128-SHA256:DHE-RSA-CAMELLIA128-SHA256:CAMELLIA256-SHA256:DH"
  1234. "E-RSA-CAMELLIA256-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECD"
  1235. "H-RSA-AES128-SHA256:ECDH-ECDSA-AES128-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-ECD"
  1236. "SA-AES256-SHA384:ECDH-RSA-AES256-SHA384:ECDH-ECDSA-AES256-SHA384:ECDHE-RSA-CHA"
  1237. "CHA20-POLY1305:ECDHE-ECDSA-CHACHA20-POLY1305:DHE-RSA-CHACHA20-POLY1305:ECDHE-R"
  1238. "SA-CHACHA20-POLY1305-OLD:ECDHE-ECDSA-CHACHA20-POLY1305-OLD:DHE-RSA-CHACHA20-PO"
  1239. "LY1305-OLD:ECDHE-ECDSA-NULL-SHA:ECDHE-PSK-NULL-SHA256:ECDHE-PSK-A"
  1240. "ES128-CBC-SHA256:PSK-CHACHA20-POLY1305:ECDHE-PSK-CHACHA20-POLY1305:DHE-PSK-CHA"
  1241. "CHA20-POLY1305:EDH-RSA-DES-CBC3-SHA:TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-S"
  1242. "HA384:TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-CCM-SHA256:TLS13-AES128-CCM-"
  1243. "8-SHA256:TLS13-SHA256-SHA256:TLS13-SHA384-SHA384";
  1244. /* OpenVPN uses a "blacklist" method to specify which ciphers NOT to use */
  1245. #ifdef OPENSSL_EXTRA
  1246. char openvpnCiphers[] = "DEFAULT:!EXP:!LOW:!MEDIUM:!kDH:!kECDH:!DSS:!PSK:"
  1247. "!SRP:!kRSA:!aNULL:!eNULL";
  1248. #endif
  1249. #ifndef NO_RSA
  1250. testCertFile = svrCertFile;
  1251. testKeyFile = svrKeyFile;
  1252. #elif defined(HAVE_ECC)
  1253. testCertFile = eccCertFile;
  1254. testKeyFile = eccKeyFile;
  1255. #else
  1256. skipTest = 1;
  1257. #endif
  1258. if (skipTest != 1) {
  1259. #ifndef NO_WOLFSSL_SERVER
  1260. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1261. #else
  1262. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1263. #endif
  1264. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1265. WOLFSSL_FILETYPE_PEM));
  1266. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1267. WOLFSSL_FILETYPE_PEM));
  1268. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1269. /* First test freeing SSL, then CTX */
  1270. wolfSSL_free(ssl);
  1271. ssl = NULL;
  1272. wolfSSL_CTX_free(ctx);
  1273. ctx = NULL;
  1274. #ifndef NO_WOLFSSL_CLIENT
  1275. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1276. #else
  1277. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1278. #endif
  1279. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1280. WOLFSSL_FILETYPE_PEM));
  1281. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1282. WOLFSSL_FILETYPE_PEM));
  1283. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1284. /* Next test freeing CTX then SSL */
  1285. wolfSSL_CTX_free(ctx);
  1286. ctx = NULL;
  1287. wolfSSL_free(ssl);
  1288. ssl = NULL;
  1289. #ifndef NO_WOLFSSL_SERVER
  1290. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1291. #else
  1292. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1293. #endif
  1294. /* Test setting ciphers at ctx level */
  1295. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1296. WOLFSSL_FILETYPE_PEM));
  1297. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1298. WOLFSSL_FILETYPE_PEM));
  1299. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, optionsCiphers));
  1300. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  1301. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  1302. /* only update TLSv13 suites */
  1303. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384"));
  1304. #endif
  1305. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  1306. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  1307. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  1308. /* only update pre-TLSv13 suites */
  1309. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx,
  1310. "ECDHE-RSA-AES128-GCM-SHA256"));
  1311. #endif
  1312. #ifdef OPENSSL_EXTRA
  1313. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, openvpnCiphers));
  1314. #endif
  1315. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1316. wolfSSL_CTX_free(ctx);
  1317. ctx = NULL;
  1318. wolfSSL_free(ssl);
  1319. ssl = NULL;
  1320. #ifndef NO_WOLFSSL_CLIENT
  1321. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1322. #else
  1323. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1324. #endif
  1325. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1326. WOLFSSL_FILETYPE_PEM));
  1327. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1328. WOLFSSL_FILETYPE_PEM));
  1329. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1330. /* test setting ciphers at SSL level */
  1331. ExpectTrue(wolfSSL_set_cipher_list(ssl, optionsCiphers));
  1332. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_TLS13) && defined(HAVE_AESGCM) && \
  1333. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  1334. /* only update TLSv13 suites */
  1335. ExpectTrue(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"));
  1336. #endif
  1337. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(HAVE_AESGCM) && \
  1338. !defined(NO_SHA256) && !defined(WOLFSSL_NO_TLS12) && \
  1339. defined(WOLFSSL_AES_128) && !defined(NO_RSA)
  1340. /* only update pre-TLSv13 suites */
  1341. ExpectTrue(wolfSSL_set_cipher_list(ssl, "ECDHE-RSA-AES128-GCM-SHA256"));
  1342. #endif
  1343. wolfSSL_CTX_free(ctx);
  1344. ctx = NULL;
  1345. wolfSSL_free(ssl);
  1346. ssl = NULL;
  1347. }
  1348. return EXPECT_RESULT();
  1349. }
  1350. #endif
  1351. static int test_wolfSSL_CTX_set_cipher_list_bytes(void)
  1352. {
  1353. EXPECT_DECLS;
  1354. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)) && \
  1355. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  1356. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  1357. const char* testCertFile;
  1358. const char* testKeyFile;
  1359. WOLFSSL_CTX* ctx = NULL;
  1360. WOLFSSL* ssl = NULL;
  1361. const byte cipherList[] =
  1362. {
  1363. /* TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x16,
  1364. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x39,
  1365. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x33,
  1366. /* TLS_DH_anon_WITH_AES_128_CBC_SHA */ 0xC0, 0x34,
  1367. /* TLS_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x35,
  1368. /* TLS_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x2F,
  1369. /* TLS_RSA_WITH_NULL_MD5 */ 0xC0, 0x01,
  1370. /* TLS_RSA_WITH_NULL_SHA */ 0xC0, 0x02,
  1371. /* TLS_PSK_WITH_AES_256_CBC_SHA */ 0xC0, 0x8d,
  1372. /* TLS_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0xae,
  1373. /* TLS_PSK_WITH_AES_256_CBC_SHA384 */ 0xC0, 0xaf,
  1374. /* TLS_PSK_WITH_AES_128_CBC_SHA */ 0xC0, 0x8c,
  1375. /* TLS_PSK_WITH_NULL_SHA256 */ 0xC0, 0xb0,
  1376. /* TLS_PSK_WITH_NULL_SHA384 */ 0xC0, 0xb1,
  1377. /* TLS_PSK_WITH_NULL_SHA */ 0xC0, 0x2c,
  1378. /* SSL_RSA_WITH_RC4_128_SHA */ 0xC0, 0x05,
  1379. /* SSL_RSA_WITH_RC4_128_MD5 */ 0xC0, 0x04,
  1380. /* SSL_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0A,
  1381. /* ECC suites, first byte is 0xC0 (ECC_BYTE) */
  1382. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x14,
  1383. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x13,
  1384. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0A,
  1385. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x09,
  1386. /* TLS_ECDHE_RSA_WITH_RC4_128_SHA */ 0xC0, 0x11,
  1387. /* TLS_ECDHE_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x07,
  1388. /* TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x12,
  1389. /* TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x08,
  1390. /* TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x27,
  1391. /* TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256*/ 0xC0, 0x23,
  1392. /* TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x28,
  1393. /* TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384*/ 0xC0, 0x24,
  1394. /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */ 0xC0, 0x06,
  1395. /* TLS_ECDHE_PSK_WITH_NULL_SHA256 */ 0xC0, 0x3a,
  1396. /* TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x37,
  1397. /* static ECDH, first byte is 0xC0 (ECC_BYTE) */
  1398. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x0F,
  1399. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x0E,
  1400. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA */ 0xC0, 0x05,
  1401. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA */ 0xC0, 0x04,
  1402. /* TLS_ECDH_RSA_WITH_RC4_128_SHA */ 0xC0, 0x0C,
  1403. /* TLS_ECDH_ECDSA_WITH_RC4_128_SHA */ 0xC0, 0x02,
  1404. /* TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x0D,
  1405. /* TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA */ 0xC0, 0x03,
  1406. /* TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x29,
  1407. /* TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256 */ 0xC0, 0x25,
  1408. /* TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x2A,
  1409. /* TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384 */ 0xC0, 0x26,
  1410. /* WDM_WITH_NULL_SHA256 */ 0x00, 0xFE, /* wolfSSL DTLS Multicast */
  1411. /* SHA256 */
  1412. /* TLS_DHE_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x6b,
  1413. /* TLS_DHE_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x67,
  1414. /* TLS_RSA_WITH_AES_256_CBC_SHA256 */ 0x00, 0x3d,
  1415. /* TLS_RSA_WITH_AES_128_CBC_SHA256 */ 0x00, 0x3c,
  1416. /* TLS_RSA_WITH_NULL_SHA256 */ 0x00, 0x3b,
  1417. /* TLS_DHE_PSK_WITH_AES_128_CBC_SHA256 */ 0x00, 0xb2,
  1418. /* TLS_DHE_PSK_WITH_NULL_SHA256 */ 0x00, 0xb4,
  1419. /* SHA384 */
  1420. /* TLS_DHE_PSK_WITH_AES_256_CBC_SHA384 */ 0x00, 0xb3,
  1421. /* TLS_DHE_PSK_WITH_NULL_SHA384 */ 0x00, 0xb5,
  1422. /* AES-GCM */
  1423. /* TLS_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9c,
  1424. /* TLS_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9d,
  1425. /* TLS_DHE_RSA_WITH_AES_128_GCM_SHA256 */ 0x00, 0x9e,
  1426. /* TLS_DHE_RSA_WITH_AES_256_GCM_SHA384 */ 0x00, 0x9f,
  1427. /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa7,
  1428. /* TLS_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xa8,
  1429. /* TLS_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xa9,
  1430. /* TLS_DHE_PSK_WITH_AES_128_GCM_SHA256 */ 0x00, 0xaa,
  1431. /* TLS_DHE_PSK_WITH_AES_256_GCM_SHA384 */ 0x00, 0xab,
  1432. /* ECC AES-GCM, first byte is 0xC0 (ECC_BYTE) */
  1433. /* TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2b,
  1434. /* TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2c,
  1435. /* TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2d,
  1436. /* TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x2e,
  1437. /* TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x2f,
  1438. /* TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x30,
  1439. /* TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256 */ 0xC0, 0x31,
  1440. /* TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384 */ 0xC0, 0x32,
  1441. /* AES-CCM, first byte is 0xC0 but isn't ECC,
  1442. * also, in some of the other AES-CCM suites
  1443. * there will be second byte number conflicts
  1444. * with non-ECC AES-GCM */
  1445. /* TLS_RSA_WITH_AES_128_CCM_8 */ 0xC0, 0xa0,
  1446. /* TLS_RSA_WITH_AES_256_CCM_8 */ 0xC0, 0xa1,
  1447. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM */ 0xC0, 0xac,
  1448. /* TLS_ECDHE_ECDSA_WITH_AES_128_CCM_8 */ 0xC0, 0xae,
  1449. /* TLS_ECDHE_ECDSA_WITH_AES_256_CCM_8 */ 0xC0, 0xaf,
  1450. /* TLS_PSK_WITH_AES_128_CCM */ 0xC0, 0xa4,
  1451. /* TLS_PSK_WITH_AES_256_CCM */ 0xC0, 0xa5,
  1452. /* TLS_PSK_WITH_AES_128_CCM_8 */ 0xC0, 0xa8,
  1453. /* TLS_PSK_WITH_AES_256_CCM_8 */ 0xC0, 0xa9,
  1454. /* TLS_DHE_PSK_WITH_AES_128_CCM */ 0xC0, 0xa6,
  1455. /* TLS_DHE_PSK_WITH_AES_256_CCM */ 0xC0, 0xa7,
  1456. /* Camellia */
  1457. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x41,
  1458. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x84,
  1459. /* TLS_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xba,
  1460. /* TLS_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc0,
  1461. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA */ 0x00, 0x45,
  1462. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA */ 0x00, 0x88,
  1463. /* TLS_DHE_RSA_WITH_CAMELLIA_128_CBC_SHA256 */ 0x00, 0xbe,
  1464. /* TLS_DHE_RSA_WITH_CAMELLIA_256_CBC_SHA256 */ 0x00, 0xc4,
  1465. /* chacha20-poly1305 suites first byte is 0xCC (CHACHA_BYTE) */
  1466. /* TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa8,
  1467. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xa9,
  1468. /* TLS_DHE_RSA_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xaa,
  1469. /* TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xac,
  1470. /* TLS_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xab,
  1471. /* TLS_DHE_PSK_WITH_CHACHA20_POLY1305_SHA256 */ 0xCC, 0xad,
  1472. /* chacha20-poly1305 earlier version of nonce and padding (CHACHA_BYTE) */
  1473. /* TLS_ECDHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x13,
  1474. /* TLS_ECDHE_ECDSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x14,
  1475. /* TLS_DHE_RSA_WITH_CHACHA20_OLD_POLY1305_SHA256 */ 0xCC, 0x15,
  1476. /* ECDHE_PSK RFC8442, first byte is 0xD0 (ECDHE_PSK_BYTE) */
  1477. /* TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256 */ 0xD0, 0x01,
  1478. /* TLS v1.3 cipher suites */
  1479. /* TLS_AES_128_GCM_SHA256 */ 0x13, 0x01,
  1480. /* TLS_AES_256_GCM_SHA384 */ 0x13, 0x02,
  1481. /* TLS_CHACHA20_POLY1305_SHA256 */ 0x13, 0x03,
  1482. /* TLS_AES_128_CCM_SHA256 */ 0x13, 0x04,
  1483. /* TLS_AES_128_CCM_8_SHA256 */ 0x13, 0x05,
  1484. /* TLS v1.3 Integrity only cipher suites - 0xC0 (ECC) first byte */
  1485. /* TLS_SHA256_SHA256 */ 0xC0, 0xB4,
  1486. /* TLS_SHA384_SHA384 */ 0xC0, 0xB5
  1487. };
  1488. #ifndef NO_RSA
  1489. testCertFile = svrCertFile;
  1490. testKeyFile = svrKeyFile;
  1491. #elif defined(HAVE_ECC)
  1492. testCertFile = eccCertFile;
  1493. testKeyFile = eccKeyFile;
  1494. #endif
  1495. #ifndef NO_WOLFSSL_SERVER
  1496. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1497. #else
  1498. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1499. #endif
  1500. ExpectTrue(wolfSSL_CTX_set_cipher_list_bytes(ctx, &cipherList[0U],
  1501. sizeof(cipherList)));
  1502. wolfSSL_CTX_free(ctx);
  1503. ctx = NULL;
  1504. #ifndef NO_WOLFSSL_SERVER
  1505. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1506. #else
  1507. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1508. #endif
  1509. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  1510. WOLFSSL_FILETYPE_PEM));
  1511. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  1512. WOLFSSL_FILETYPE_PEM));
  1513. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1514. ExpectTrue(wolfSSL_set_cipher_list_bytes(ssl, &cipherList[0U],
  1515. sizeof(cipherList)));
  1516. wolfSSL_free(ssl);
  1517. wolfSSL_CTX_free(ctx);
  1518. #endif /* (OPENSSL_EXTRA || WOLFSSL_SET_CIPHER_BYTES) &&
  1519. (!NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER) && (!NO_RSA || HAVE_ECC) */
  1520. return EXPECT_RESULT();
  1521. }
  1522. static int test_wolfSSL_CTX_use_certificate_file(void)
  1523. {
  1524. EXPECT_DECLS;
  1525. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1526. WOLFSSL_CTX *ctx = NULL;
  1527. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1528. /* invalid context */
  1529. ExpectFalse(wolfSSL_CTX_use_certificate_file(NULL, svrCertFile,
  1530. WOLFSSL_FILETYPE_PEM));
  1531. /* invalid cert file */
  1532. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, bogusFile,
  1533. WOLFSSL_FILETYPE_PEM));
  1534. /* invalid cert type */
  1535. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, 9999));
  1536. #ifdef NO_RSA
  1537. /* rsa needed */
  1538. ExpectFalse(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1539. WOLFSSL_FILETYPE_PEM));
  1540. #else
  1541. /* success */
  1542. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  1543. WOLFSSL_FILETYPE_PEM));
  1544. #endif
  1545. wolfSSL_CTX_free(ctx);
  1546. #endif
  1547. return EXPECT_RESULT();
  1548. }
  1549. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  1550. static int test_wolfSSL_CTX_use_certificate_ASN1(void)
  1551. {
  1552. EXPECT_DECLS;
  1553. #if !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER) && !defined(NO_ASN)
  1554. WOLFSSL_CTX* ctx = NULL;
  1555. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1556. ExpectIntEQ(SSL_CTX_use_certificate_ASN1(ctx, sizeof_server_cert_der_2048,
  1557. server_cert_der_2048), WOLFSSL_SUCCESS);
  1558. wolfSSL_CTX_free(ctx);
  1559. #endif
  1560. return EXPECT_RESULT();
  1561. }
  1562. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  1563. /* Test function for wolfSSL_CTX_use_certificate_buffer. Load cert into
  1564. * context using buffer.
  1565. * PRE: NO_CERTS not defined; USE_CERT_BUFFERS_2048 defined; compile with
  1566. * --enable-testcert flag.
  1567. */
  1568. static int test_wolfSSL_CTX_use_certificate_buffer(void)
  1569. {
  1570. EXPECT_DECLS;
  1571. #if !defined(NO_CERTS) && defined(USE_CERT_BUFFERS_2048) && \
  1572. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  1573. WOLFSSL_CTX* ctx = NULL;
  1574. int ret;
  1575. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1576. ExpectIntEQ(ret = wolfSSL_CTX_use_certificate_buffer(ctx,
  1577. server_cert_der_2048, sizeof_server_cert_der_2048,
  1578. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1579. wolfSSL_CTX_free(ctx);
  1580. #endif
  1581. return EXPECT_RESULT();
  1582. } /* END test_wolfSSL_CTX_use_certificate_buffer */
  1583. static int test_wolfSSL_CTX_use_PrivateKey_file(void)
  1584. {
  1585. EXPECT_DECLS;
  1586. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_SERVER)
  1587. WOLFSSL_CTX *ctx = NULL;
  1588. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  1589. /* invalid context */
  1590. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(NULL, svrKeyFile,
  1591. WOLFSSL_FILETYPE_PEM));
  1592. /* invalid key file */
  1593. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, bogusFile,
  1594. WOLFSSL_FILETYPE_PEM));
  1595. /* invalid key type */
  1596. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, 9999));
  1597. /* success */
  1598. #ifdef NO_RSA
  1599. /* rsa needed */
  1600. ExpectFalse(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1601. WOLFSSL_FILETYPE_PEM));
  1602. #else
  1603. /* success */
  1604. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  1605. WOLFSSL_FILETYPE_PEM));
  1606. #endif
  1607. wolfSSL_CTX_free(ctx);
  1608. #endif
  1609. return EXPECT_RESULT();
  1610. }
  1611. /* test both file and buffer versions along with unloading trusted peer certs */
  1612. static int test_wolfSSL_CTX_trust_peer_cert(void)
  1613. {
  1614. EXPECT_DECLS;
  1615. #if !defined(NO_CERTS) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  1616. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_RSA)
  1617. WOLFSSL_CTX *ctx = NULL;
  1618. WOLFSSL* ssl = NULL;
  1619. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1620. ExpectNotNull(ssl = wolfSSL_new(ctx));
  1621. #if !defined(NO_FILESYSTEM)
  1622. /* invalid file */
  1623. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, NULL,
  1624. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1625. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, bogusFile,
  1626. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1627. ExpectIntNE(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1628. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1629. /* success */
  1630. ExpectIntEQ(wolfSSL_CTX_trust_peer_cert(ctx, cliCertFile,
  1631. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1632. /* unload cert */
  1633. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1634. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1635. /* invalid file */
  1636. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, NULL,
  1637. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1638. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, bogusFile,
  1639. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1640. ExpectIntNE(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1641. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1642. /* success */
  1643. ExpectIntEQ(wolfSSL_trust_peer_cert(ssl, cliCertFile,
  1644. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  1645. #ifdef WOLFSSL_LOCAL_X509_STORE
  1646. /* unload cert */
  1647. ExpectIntNE(wolfSSL_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1648. ExpectIntEQ(wolfSSL_Unload_trust_peers(ssl), WOLFSSL_SUCCESS);
  1649. #endif
  1650. #endif
  1651. /* Test of loading certs from buffers */
  1652. /* invalid buffer */
  1653. ExpectIntNE(wolfSSL_CTX_trust_peer_buffer(ctx, NULL, -1,
  1654. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1655. /* success */
  1656. #ifdef USE_CERT_BUFFERS_1024
  1657. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_1024,
  1658. sizeof_client_cert_der_1024, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1659. #endif
  1660. #ifdef USE_CERT_BUFFERS_2048
  1661. ExpectIntEQ(wolfSSL_CTX_trust_peer_buffer(ctx, client_cert_der_2048,
  1662. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  1663. #endif
  1664. /* unload cert */
  1665. ExpectIntNE(wolfSSL_CTX_Unload_trust_peers(NULL), WOLFSSL_SUCCESS);
  1666. ExpectIntEQ(wolfSSL_CTX_Unload_trust_peers(ctx), WOLFSSL_SUCCESS);
  1667. wolfSSL_free(ssl);
  1668. wolfSSL_CTX_free(ctx);
  1669. #endif
  1670. return EXPECT_RESULT();
  1671. }
  1672. static int test_wolfSSL_CTX_load_verify_locations(void)
  1673. {
  1674. EXPECT_DECLS;
  1675. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_WOLFSSL_CLIENT)
  1676. WOLFSSL_CTX *ctx = NULL;
  1677. #ifndef NO_RSA
  1678. WOLFSSL_CERT_MANAGER* cm = NULL;
  1679. #ifdef PERSIST_CERT_CACHE
  1680. int cacheSz = 0;
  1681. unsigned char* cache = NULL;
  1682. int used = 0;
  1683. #ifndef NO_FILESYSTEM
  1684. const char* cacheFile = "./tests/cert_cache.tmp";
  1685. #endif
  1686. int i;
  1687. int t;
  1688. int* p;
  1689. #endif
  1690. #endif
  1691. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1692. const char* load_certs_path = "./certs/external";
  1693. const char* load_no_certs_path = "./examples";
  1694. const char* load_expired_path = "./certs/test/expired";
  1695. #endif
  1696. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  1697. /* invalid arguments */
  1698. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(NULL, caCertFile, NULL),
  1699. WOLFSSL_FAILURE);
  1700. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, NULL),
  1701. WOLFSSL_FAILURE);
  1702. /* invalid ca file */
  1703. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, bogusFile, NULL),
  1704. WS_RETURN_CODE(WOLFSSL_BAD_FILE,WOLFSSL_FAILURE));
  1705. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS) && \
  1706. ((defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)) && \
  1707. !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR))
  1708. /* invalid path */
  1709. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, NULL, bogusFile),
  1710. WS_RETURN_CODE(BAD_PATH_ERROR,WOLFSSL_FAILURE));
  1711. #endif
  1712. #if defined(WOLFSSL_QT) || defined(WOLFSSL_IGNORE_BAD_CERT_PATH)
  1713. /* test ignoring the invalid path */
  1714. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, bogusFile,
  1715. WOLFSSL_LOAD_FLAG_IGNORE_BAD_PATH_ERR), WOLFSSL_SUCCESS);
  1716. #endif
  1717. /* load ca cert */
  1718. #ifdef NO_RSA
  1719. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1720. WS_RETURN_CODE(ASN_UNKNOWN_OID_E,WOLFSSL_FAILURE));
  1721. #else /* Skip the following test without RSA certs. */
  1722. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1723. WOLFSSL_SUCCESS);
  1724. #ifdef PERSIST_CERT_CACHE
  1725. /* Get cert cache size */
  1726. ExpectIntGT(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), 0);
  1727. ExpectNotNull(cache = (byte*)XMALLOC(cacheSz, NULL,
  1728. DYNAMIC_TYPE_TMP_BUFFER));
  1729. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, NULL),
  1730. BAD_FUNC_ARG);
  1731. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, -1, NULL),
  1732. BAD_FUNC_ARG);
  1733. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, -1, NULL),
  1734. BAD_FUNC_ARG);
  1735. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, cacheSz, NULL),
  1736. BAD_FUNC_ARG);
  1737. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, NULL, -1, &used),
  1738. BAD_FUNC_ARG);
  1739. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(NULL, cache, cacheSz, &used),
  1740. BAD_FUNC_ARG);
  1741. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, NULL, cacheSz, &used),
  1742. BAD_FUNC_ARG);
  1743. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, -1, &used),
  1744. BAD_FUNC_ARG);
  1745. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, NULL),
  1746. BAD_FUNC_ARG);
  1747. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz - 10, &used),
  1748. BUFFER_E);
  1749. ExpectIntEQ(wolfSSL_CTX_memsave_cert_cache(ctx, cache, cacheSz, &used), 1);
  1750. ExpectIntEQ(cacheSz, used);
  1751. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, -1),
  1752. BAD_FUNC_ARG);
  1753. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, -1),
  1754. BAD_FUNC_ARG);
  1755. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, -1),
  1756. BAD_FUNC_ARG);
  1757. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, NULL, cacheSz),
  1758. BAD_FUNC_ARG);
  1759. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(NULL, cache, cacheSz),
  1760. BAD_FUNC_ARG);
  1761. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, NULL, cacheSz),
  1762. BAD_FUNC_ARG);
  1763. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, -1),
  1764. BAD_FUNC_ARG);
  1765. /* Smaller than header. */
  1766. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, 1), BUFFER_E);
  1767. for (i = 1; i < cacheSz; i++) {
  1768. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz - i),
  1769. BUFFER_E);
  1770. }
  1771. if (EXPECT_SUCCESS()) {
  1772. /* Modify header for bad results! */
  1773. p = (int*)cache;
  1774. /* version */
  1775. t = p[0]; p[0] = 0xff;
  1776. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1777. CACHE_MATCH_ERROR);
  1778. p[0] = t; p++;
  1779. /* rows */
  1780. t = p[0]; p[0] = 0xff;
  1781. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1782. CACHE_MATCH_ERROR);
  1783. p[0] = t; p++;
  1784. /* columns[0] */
  1785. t = p[0]; p[0] = -1;
  1786. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1787. PARSE_ERROR);
  1788. p[0] = t; p += CA_TABLE_SIZE;
  1789. /* signerSz*/
  1790. t = p[0]; p[0] = 0xff;
  1791. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz),
  1792. CACHE_MATCH_ERROR);
  1793. p[0] = t;
  1794. }
  1795. ExpectIntEQ(wolfSSL_CTX_memrestore_cert_cache(ctx, cache, cacheSz), 1);
  1796. ExpectIntEQ(cacheSz = wolfSSL_CTX_get_cert_cache_memsize(ctx), used);
  1797. #ifndef NO_FILESYSTEM
  1798. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1799. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1800. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1801. ExpectIntEQ(wolfSSL_CTX_save_cert_cache(ctx, cacheFile), 1);
  1802. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, NULL), BAD_FUNC_ARG);
  1803. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, NULL), BAD_FUNC_ARG);
  1804. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(NULL, cacheFile), BAD_FUNC_ARG);
  1805. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "no-file"),
  1806. WOLFSSL_BAD_FILE);
  1807. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, cacheFile), 1);
  1808. /* File contents is not a cache. */
  1809. ExpectIntEQ(wolfSSL_CTX_restore_cert_cache(ctx, "./certs/ca-cert.pem"),
  1810. CACHE_MATCH_ERROR);
  1811. #endif
  1812. XFREE(cache, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  1813. #endif
  1814. /* Test unloading CA's */
  1815. ExpectIntEQ(wolfSSL_CTX_UnloadCAs(ctx), WOLFSSL_SUCCESS);
  1816. #ifdef PERSIST_CERT_CACHE
  1817. /* Verify no certs (result is less than cacheSz) */
  1818. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1819. #endif
  1820. /* load ca cert again */
  1821. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, NULL),
  1822. WOLFSSL_SUCCESS);
  1823. /* Test getting CERT_MANAGER */
  1824. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(ctx));
  1825. /* Test unloading CA's using CM */
  1826. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  1827. #ifdef PERSIST_CERT_CACHE
  1828. /* Verify no certs (result is less than cacheSz) */
  1829. ExpectIntGT(cacheSz, wolfSSL_CTX_get_cert_cache_memsize(ctx));
  1830. #endif
  1831. #endif
  1832. #if !defined(NO_WOLFSSL_DIR) && !defined(WOLFSSL_TIRTOS)
  1833. /* Test loading CA certificates using a path */
  1834. #ifdef NO_RSA
  1835. /* failure here okay since certs in external directory are RSA */
  1836. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1837. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1838. #else
  1839. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1840. WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_SUCCESS);
  1841. #endif
  1842. /* Test loading path with no files */
  1843. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1844. load_no_certs_path, WOLFSSL_LOAD_FLAG_PEM_CA_ONLY), WOLFSSL_FAILURE);
  1845. /* Test loading expired CA certificates */
  1846. #ifdef NO_RSA
  1847. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1848. load_expired_path,
  1849. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1850. WOLFSSL_SUCCESS);
  1851. #else
  1852. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL,
  1853. load_expired_path,
  1854. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY | WOLFSSL_LOAD_FLAG_PEM_CA_ONLY),
  1855. WOLFSSL_SUCCESS);
  1856. #endif
  1857. /* Test loading CA certificates and ignoring all errors */
  1858. #ifdef NO_RSA
  1859. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1860. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_FAILURE);
  1861. #else
  1862. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, NULL, load_certs_path,
  1863. WOLFSSL_LOAD_FLAG_IGNORE_ERR), WOLFSSL_SUCCESS);
  1864. #endif
  1865. #endif
  1866. wolfSSL_CTX_free(ctx);
  1867. #endif
  1868. return EXPECT_RESULT();
  1869. }
  1870. static int test_wolfSSL_CTX_load_system_CA_certs(void)
  1871. {
  1872. int res = TEST_SKIPPED;
  1873. #if defined(WOLFSSL_SYS_CA_CERTS) && !defined(NO_WOLFSSL_CLIENT) && \
  1874. (!defined(NO_RSA) || defined(HAVE_ECC))
  1875. WOLFSSL_CTX* ctx;
  1876. byte dirValid = 0;
  1877. int ret = 0;
  1878. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  1879. if (ctx == NULL) {
  1880. fprintf(stderr, "wolfSSL_CTX_new failed.\n");
  1881. ret = -1;
  1882. }
  1883. if (ret == 0) {
  1884. #if defined(USE_WINDOWS_API) || defined(__APPLE__)
  1885. dirValid = 1;
  1886. #else
  1887. word32 numDirs;
  1888. const char** caDirs = wolfSSL_get_system_CA_dirs(&numDirs);
  1889. if (caDirs == NULL || numDirs == 0) {
  1890. fprintf(stderr, "wolfSSL_get_system_CA_dirs failed.\n");
  1891. ret = -1;
  1892. }
  1893. else {
  1894. ReadDirCtx dirCtx;
  1895. word32 i;
  1896. for (i = 0; i < numDirs; ++i) {
  1897. if (wc_ReadDirFirst(&dirCtx, caDirs[i], NULL) == 0) {
  1898. /* Directory isn't empty. */
  1899. dirValid = 1;
  1900. wc_ReadDirClose(&dirCtx);
  1901. break;
  1902. }
  1903. }
  1904. }
  1905. #endif
  1906. }
  1907. /*
  1908. * If the directory isn't empty, we should be able to load CA
  1909. * certs from it. On Windows/Mac, we assume the CA cert stores are
  1910. * usable.
  1911. */
  1912. if (ret == 0 && dirValid && wolfSSL_CTX_load_system_CA_certs(ctx) !=
  1913. WOLFSSL_SUCCESS) {
  1914. fprintf(stderr, "wolfSSL_CTX_load_system_CA_certs failed.\n");
  1915. ret = -1;
  1916. }
  1917. #ifdef OPENSSL_EXTRA
  1918. if (ret == 0 &&
  1919. wolfSSL_CTX_set_default_verify_paths(ctx) != WOLFSSL_SUCCESS) {
  1920. fprintf(stderr, "wolfSSL_CTX_set_default_verify_paths failed.\n");
  1921. ret = -1;
  1922. }
  1923. #endif /* OPENSSL_EXTRA */
  1924. wolfSSL_CTX_free(ctx);
  1925. res = TEST_RES_CHECK(ret == 0);
  1926. #endif /* WOLFSSL_SYS_CA_CERTS && !NO_WOLFSSL_CLIENT */
  1927. return res;
  1928. }
  1929. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  1930. static int test_cm_load_ca_buffer(const byte* cert_buf, size_t cert_sz,
  1931. int file_type)
  1932. {
  1933. int ret;
  1934. WOLFSSL_CERT_MANAGER* cm;
  1935. cm = wolfSSL_CertManagerNew();
  1936. if (cm == NULL) {
  1937. fprintf(stderr, "test_cm_load_ca failed\n");
  1938. return -1;
  1939. }
  1940. ret = wolfSSL_CertManagerLoadCABuffer(cm, cert_buf, cert_sz, file_type);
  1941. wolfSSL_CertManagerFree(cm);
  1942. return ret;
  1943. }
  1944. static int test_cm_load_ca_file(const char* ca_cert_file)
  1945. {
  1946. int ret = 0;
  1947. byte* cert_buf = NULL;
  1948. size_t cert_sz = 0;
  1949. #if defined(WOLFSSL_PEM_TO_DER)
  1950. DerBuffer* pDer = NULL;
  1951. #endif
  1952. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  1953. if (ret == 0) {
  1954. /* normal test */
  1955. ret = test_cm_load_ca_buffer(cert_buf, cert_sz, WOLFSSL_FILETYPE_PEM);
  1956. if (ret == WOLFSSL_SUCCESS) {
  1957. /* test including null terminator in length */
  1958. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  1959. if (tmp == NULL) {
  1960. ret = MEMORY_E;
  1961. }
  1962. else {
  1963. cert_buf = tmp;
  1964. cert_buf[cert_sz] = '\0';
  1965. ret = test_cm_load_ca_buffer(cert_buf, cert_sz+1,
  1966. WOLFSSL_FILETYPE_PEM);
  1967. }
  1968. }
  1969. #if defined(WOLFSSL_PEM_TO_DER)
  1970. if (ret == WOLFSSL_SUCCESS) {
  1971. /* test loading DER */
  1972. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  1973. if (ret == 0 && pDer != NULL) {
  1974. ret = test_cm_load_ca_buffer(pDer->buffer, pDer->length,
  1975. WOLFSSL_FILETYPE_ASN1);
  1976. wc_FreeDer(&pDer);
  1977. }
  1978. }
  1979. #endif
  1980. }
  1981. free(cert_buf);
  1982. return ret;
  1983. }
  1984. static int test_cm_load_ca_buffer_ex(const byte* cert_buf, size_t cert_sz,
  1985. int file_type, word32 flags)
  1986. {
  1987. int ret;
  1988. WOLFSSL_CERT_MANAGER* cm;
  1989. cm = wolfSSL_CertManagerNew();
  1990. if (cm == NULL) {
  1991. fprintf(stderr, "test_cm_load_ca failed\n");
  1992. return -1;
  1993. }
  1994. ret = wolfSSL_CertManagerLoadCABuffer_ex(cm, cert_buf, cert_sz, file_type,
  1995. 0, flags);
  1996. wolfSSL_CertManagerFree(cm);
  1997. return ret;
  1998. }
  1999. static int test_cm_load_ca_file_ex(const char* ca_cert_file, word32 flags)
  2000. {
  2001. int ret = 0;
  2002. byte* cert_buf = NULL;
  2003. size_t cert_sz = 0;
  2004. #if defined(WOLFSSL_PEM_TO_DER)
  2005. DerBuffer* pDer = NULL;
  2006. #endif
  2007. ret = load_file(ca_cert_file, &cert_buf, &cert_sz);
  2008. if (ret == 0) {
  2009. /* normal test */
  2010. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz,
  2011. WOLFSSL_FILETYPE_PEM, flags);
  2012. if (ret == WOLFSSL_SUCCESS) {
  2013. /* test including null terminator in length */
  2014. byte* tmp = (byte*)realloc(cert_buf, cert_sz+1);
  2015. if (tmp == NULL) {
  2016. ret = MEMORY_E;
  2017. }
  2018. else {
  2019. cert_buf = tmp;
  2020. cert_buf[cert_sz] = '\0';
  2021. ret = test_cm_load_ca_buffer_ex(cert_buf, cert_sz+1,
  2022. WOLFSSL_FILETYPE_PEM, flags);
  2023. }
  2024. }
  2025. #if defined(WOLFSSL_PEM_TO_DER)
  2026. if (ret == WOLFSSL_SUCCESS) {
  2027. /* test loading DER */
  2028. ret = wc_PemToDer(cert_buf, cert_sz, CA_TYPE, &pDer, NULL, NULL, NULL);
  2029. if (ret == 0 && pDer != NULL) {
  2030. ret = test_cm_load_ca_buffer_ex(pDer->buffer, pDer->length,
  2031. WOLFSSL_FILETYPE_ASN1, flags);
  2032. wc_FreeDer(&pDer);
  2033. }
  2034. }
  2035. #endif
  2036. }
  2037. free(cert_buf);
  2038. return ret;
  2039. }
  2040. #endif /* !NO_FILESYSTEM && !NO_CERTS */
  2041. static int test_wolfSSL_CertManagerAPI(void)
  2042. {
  2043. EXPECT_DECLS;
  2044. #ifndef NO_CERTS
  2045. WOLFSSL_CERT_MANAGER* cm = NULL;
  2046. unsigned char c;
  2047. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2048. wolfSSL_CertManagerFree(NULL);
  2049. ExpectIntEQ(wolfSSL_CertManager_up_ref(NULL), 0);
  2050. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(NULL), BAD_FUNC_ARG);
  2051. #ifdef WOLFSSL_TRUST_PEER_CERT
  2052. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(NULL), BAD_FUNC_ARG);
  2053. #endif
  2054. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer_ex(NULL, &c, 1,
  2055. WOLFSSL_FILETYPE_ASN1, 0, 0), WOLFSSL_FATAL_ERROR);
  2056. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  2057. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, -1,
  2058. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2059. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, -1,
  2060. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2061. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, -1,
  2062. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2063. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, NULL, 1,
  2064. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2065. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(NULL, &c, 1,
  2066. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2067. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, NULL, 1,
  2068. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2069. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, -1,
  2070. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  2071. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, &c, 1, -1),
  2072. WOLFSSL_BAD_FILETYPE);
  2073. #endif
  2074. #if !defined(NO_FILESYSTEM)
  2075. {
  2076. const char* ca_cert = "./certs/ca-cert.pem";
  2077. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  2078. const char* ca_cert_der = "./certs/ca-cert.der";
  2079. #endif
  2080. const char* ca_path = "./certs";
  2081. #if !defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH)
  2082. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, NULL, -1),
  2083. BAD_FUNC_ARG);
  2084. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  2085. BAD_FUNC_ARG);
  2086. ExpectIntEQ(wolfSSL_CertManagerVerify(NULL, ca_cert,
  2087. WOLFSSL_FILETYPE_PEM), BAD_FUNC_ARG);
  2088. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert, -1),
  2089. WOLFSSL_BAD_FILETYPE);
  2090. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, "no-file",
  2091. WOLFSSL_FILETYPE_ASN1), WOLFSSL_BAD_FILE);
  2092. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, ca_cert_der,
  2093. WOLFSSL_FILETYPE_PEM), ASN_NO_PEM_HEADER);
  2094. #endif
  2095. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, NULL),
  2096. WOLFSSL_FATAL_ERROR);
  2097. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, NULL),
  2098. WOLFSSL_FATAL_ERROR);
  2099. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, NULL, ca_path),
  2100. WOLFSSL_FATAL_ERROR);
  2101. ExpectIntEQ(wolfSSL_CertManagerLoadCA(NULL, ca_cert, ca_path),
  2102. WOLFSSL_FATAL_ERROR);
  2103. }
  2104. #endif
  2105. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  2106. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), 1);
  2107. #elif !defined(HAVE_CRL)
  2108. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 0), NOT_COMPILED_IN);
  2109. #endif
  2110. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(NULL), BAD_FUNC_ARG);
  2111. ExpectIntEQ(wolfSSL_CertManagerDisableCRL(cm), 1);
  2112. #ifdef HAVE_CRL
  2113. /* Test APIs when CRL is disabled. */
  2114. #ifdef HAVE_CRL_IO
  2115. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  2116. #endif
  2117. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  2118. sizeof_server_cert_der_2048), 1);
  2119. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  2120. #endif
  2121. /* OCSP */
  2122. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(NULL, 0), BAD_FUNC_ARG);
  2123. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(NULL), BAD_FUNC_ARG);
  2124. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(NULL), BAD_FUNC_ARG);
  2125. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(NULL), BAD_FUNC_ARG);
  2126. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  2127. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(NULL), BAD_FUNC_ARG);
  2128. #if !defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  2129. !defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2130. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), NOT_COMPILED_IN);
  2131. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), NOT_COMPILED_IN);
  2132. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), NOT_COMPILED_IN);
  2133. #endif
  2134. #ifdef HAVE_OCSP
  2135. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, -1), BAD_FUNC_ARG);
  2136. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, -1), BAD_FUNC_ARG);
  2137. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, -1), BAD_FUNC_ARG);
  2138. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, NULL, 1), BAD_FUNC_ARG);
  2139. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(NULL, &c, 1), BAD_FUNC_ARG);
  2140. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, NULL, 1), BAD_FUNC_ARG);
  2141. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, -1), BAD_FUNC_ARG);
  2142. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, NULL, 0,
  2143. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2144. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, NULL, 1,
  2145. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2146. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(NULL, &c, 1,
  2147. NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  2148. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, NULL),
  2149. BAD_FUNC_ARG);
  2150. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(NULL, ""),
  2151. BAD_FUNC_ARG);
  2152. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, NULL), 1);
  2153. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(NULL, NULL, NULL, NULL),
  2154. BAD_FUNC_ARG);
  2155. ExpectIntEQ(wolfSSL_CertManagerSetOCSP_Cb(cm, NULL, NULL, NULL), 1);
  2156. ExpectIntEQ(wolfSSL_CertManagerDisableOCSP(cm), 1);
  2157. /* Test APIs when OCSP is disabled. */
  2158. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, &c, 1,
  2159. NULL, NULL, NULL, NULL), 1);
  2160. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, &c, 1), 1);
  2161. #endif
  2162. ExpectIntEQ(wolfSSL_CertManager_up_ref(cm), 1);
  2163. if (EXPECT_SUCCESS()) {
  2164. wolfSSL_CertManagerFree(cm);
  2165. }
  2166. wolfSSL_CertManagerFree(cm);
  2167. cm = NULL;
  2168. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2169. #ifdef HAVE_OCSP
  2170. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, WOLFSSL_OCSP_URL_OVERRIDE |
  2171. WOLFSSL_OCSP_CHECKALL), 1);
  2172. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  2173. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  2174. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2175. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2176. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPStapling(cm), 1);
  2177. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPStapling(cm), 1);
  2178. ExpectIntEQ(wolfSSL_CertManagerEnableOCSPMustStaple(cm), 1);
  2179. ExpectIntEQ(wolfSSL_CertManagerDisableOCSPMustStaple(cm), 1);
  2180. #endif
  2181. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  2182. ExpectIntEQ(wolfSSL_CertManagerSetOCSPOverrideURL(cm, ""), 1);
  2183. #endif
  2184. #ifdef WOLFSSL_TRUST_PEER_CERT
  2185. ExpectIntEQ(wolfSSL_CertManagerUnload_trust_peers(cm), 1);
  2186. #endif
  2187. wolfSSL_CertManagerFree(cm);
  2188. #endif
  2189. return EXPECT_RESULT();
  2190. }
  2191. static int test_wolfSSL_CertManagerLoadCABuffer(void)
  2192. {
  2193. EXPECT_DECLS;
  2194. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2195. const char* ca_cert = "./certs/ca-cert.pem";
  2196. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2197. int ret;
  2198. ExpectIntLE(ret = test_cm_load_ca_file(ca_cert), 1);
  2199. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2200. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2201. #elif defined(NO_RSA)
  2202. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2203. #else
  2204. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2205. #endif
  2206. ExpectIntLE(ret = test_cm_load_ca_file(ca_expired_cert), 1);
  2207. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2208. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2209. #elif defined(NO_RSA)
  2210. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2211. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  2212. !defined(NO_ASN_TIME)
  2213. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  2214. #else
  2215. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2216. #endif
  2217. #endif
  2218. return EXPECT_RESULT();
  2219. }
  2220. static int test_wolfSSL_CertManagerLoadCABuffer_ex(void)
  2221. {
  2222. EXPECT_DECLS;
  2223. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  2224. const char* ca_cert = "./certs/ca-cert.pem";
  2225. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  2226. int ret;
  2227. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_cert, WOLFSSL_LOAD_FLAG_NONE),
  2228. 1);
  2229. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2230. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2231. #elif defined(NO_RSA)
  2232. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2233. #else
  2234. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2235. #endif
  2236. ExpectIntLE(ret = test_cm_load_ca_file_ex(ca_expired_cert,
  2237. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), 1);
  2238. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2239. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  2240. #elif defined(NO_RSA)
  2241. ExpectIntEQ(ret, ASN_UNKNOWN_OID_E);
  2242. #elif !(WOLFSSL_LOAD_VERIFY_DEFAULT_FLAGS & WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY) && \
  2243. !defined(NO_ASN_TIME) && defined(WOLFSSL_TRUST_PEER_CERT) && \
  2244. defined(OPENSSL_COMPATIBLE_DEFAULTS)
  2245. ExpectIntEQ(ret, ASN_AFTER_DATE_E);
  2246. #else
  2247. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  2248. #endif
  2249. #endif
  2250. return EXPECT_RESULT();
  2251. }
  2252. static int test_wolfSSL_CertManagerGetCerts(void)
  2253. {
  2254. EXPECT_DECLS;
  2255. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  2256. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  2257. defined(WOLFSSL_SIGNER_DER_CERT)
  2258. WOLFSSL_CERT_MANAGER* cm = NULL;
  2259. WOLFSSL_STACK* sk = NULL;
  2260. X509* x509 = NULL;
  2261. X509* cert1 = NULL;
  2262. FILE* file1 = NULL;
  2263. #ifdef DEBUG_WOLFSSL_VERBOSE
  2264. WOLFSSL_BIO* bio = NULL;
  2265. #endif
  2266. int i = 0;
  2267. int ret = 0;
  2268. const byte* der = NULL;
  2269. int derSz = 0;
  2270. ExpectNotNull(file1 = fopen("./certs/ca-cert.pem", "rb"));
  2271. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  2272. if (file1 != NULL) {
  2273. fclose(file1);
  2274. }
  2275. ExpectNull(sk = wolfSSL_CertManagerGetCerts(NULL));
  2276. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  2277. ExpectNull(sk = wolfSSL_CertManagerGetCerts(cm));
  2278. ExpectNotNull(der = wolfSSL_X509_get_der(cert1, &derSz));
  2279. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  2280. /* Check that ASN_SELF_SIGNED_E is returned for a self-signed cert for QT
  2281. * and full OpenSSL compatibility */
  2282. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2283. WOLFSSL_FILETYPE_ASN1), ASN_SELF_SIGNED_E);
  2284. #else
  2285. ExpectIntEQ(ret = wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2286. WOLFSSL_FILETYPE_ASN1), ASN_NO_SIGNER_E);
  2287. #endif
  2288. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  2289. "./certs/ca-cert.pem", NULL));
  2290. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(cm));
  2291. for (i = 0; EXPECT_SUCCESS() && i < sk_X509_num(sk); i++) {
  2292. ExpectNotNull(x509 = sk_X509_value(sk, i));
  2293. ExpectIntEQ(0, wolfSSL_X509_cmp(x509, cert1));
  2294. #ifdef DEBUG_WOLFSSL_VERBOSE
  2295. bio = BIO_new(wolfSSL_BIO_s_file());
  2296. if (bio != NULL) {
  2297. BIO_set_fp(bio, stderr, BIO_NOCLOSE);
  2298. X509_print(bio, x509);
  2299. BIO_free(bio);
  2300. }
  2301. #endif /* DEBUG_WOLFSSL_VERBOSE */
  2302. }
  2303. wolfSSL_X509_free(cert1);
  2304. sk_X509_pop_free(sk, NULL);
  2305. wolfSSL_CertManagerFree(cm);
  2306. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  2307. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  2308. defined(WOLFSSL_SIGNER_DER_CERT) */
  2309. return EXPECT_RESULT();
  2310. }
  2311. static int test_wolfSSL_CertManagerSetVerify(void)
  2312. {
  2313. EXPECT_DECLS;
  2314. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2315. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2316. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  2317. WOLFSSL_CERT_MANAGER* cm = NULL;
  2318. int tmp = myVerifyAction;
  2319. const char* ca_cert = "./certs/ca-cert.pem";
  2320. const char* expiredCert = "./certs/test/expired/expired-cert.pem";
  2321. wolfSSL_CertManagerSetVerify(NULL, NULL);
  2322. wolfSSL_CertManagerSetVerify(NULL, myVerify);
  2323. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2324. wolfSSL_CertManagerSetVerify(cm, myVerify);
  2325. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  2326. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL), -1);
  2327. #else
  2328. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL),
  2329. WOLFSSL_SUCCESS);
  2330. #endif
  2331. /* Use the test CB that always accepts certs */
  2332. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  2333. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, expiredCert,
  2334. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  2335. #ifdef WOLFSSL_ALWAYS_VERIFY_CB
  2336. {
  2337. const char* verifyCert = "./certs/server-cert.der";
  2338. /* Use the test CB that always fails certs */
  2339. myVerifyAction = VERIFY_FORCE_FAIL;
  2340. ExpectIntEQ(wolfSSL_CertManagerVerify(cm, verifyCert,
  2341. WOLFSSL_FILETYPE_ASN1), VERIFY_CERT_ERROR);
  2342. }
  2343. #endif
  2344. wolfSSL_CertManagerFree(cm);
  2345. myVerifyAction = tmp;
  2346. #endif
  2347. return EXPECT_RESULT();
  2348. }
  2349. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  2350. defined(DEBUG_UNIT_TEST_CERTS)
  2351. /* Used when debugging name constraint tests. Not static to allow use in
  2352. * multiple locations with complex define guards. */
  2353. void DEBUG_WRITE_CERT_X509(WOLFSSL_X509* x509, const char* fileName)
  2354. {
  2355. BIO* out = BIO_new_file(fileName, "wb");
  2356. if (out != NULL) {
  2357. PEM_write_bio_X509(out, x509);
  2358. BIO_free(out);
  2359. }
  2360. }
  2361. void DEBUG_WRITE_DER(const byte* der, int derSz, const char* fileName)
  2362. {
  2363. BIO* out = BIO_new_file(fileName, "wb");
  2364. if (out != NULL) {
  2365. BIO_write(out, der, derSz);
  2366. BIO_free(out);
  2367. }
  2368. }
  2369. #else
  2370. #define DEBUG_WRITE_CERT_X509(x509, fileName) WC_DO_NOTHING
  2371. #define DEBUG_WRITE_DER(der, derSz, fileName) WC_DO_NOTHING
  2372. #endif
  2373. static int test_wolfSSL_CertManagerNameConstraint(void)
  2374. {
  2375. EXPECT_DECLS;
  2376. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2377. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2378. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2379. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2380. !defined(NO_SHA256)
  2381. WOLFSSL_CERT_MANAGER* cm = NULL;
  2382. WOLFSSL_EVP_PKEY *priv = NULL;
  2383. WOLFSSL_X509_NAME* name = NULL;
  2384. const char* ca_cert = "./certs/test/cert-ext-nc.der";
  2385. const char* server_cert = "./certs/test/server-goodcn.pem";
  2386. int i = 0;
  2387. static const byte extNameConsOid[] = {85, 29, 30};
  2388. RsaKey key;
  2389. WC_RNG rng;
  2390. byte *der = NULL;
  2391. int derSz = 0;
  2392. word32 idx = 0;
  2393. byte *pt;
  2394. WOLFSSL_X509 *x509 = NULL;
  2395. WOLFSSL_X509 *ca = NULL;
  2396. wc_InitRng(&rng);
  2397. /* load in CA private key for signing */
  2398. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, testDevId), 0);
  2399. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  2400. sizeof_server_key_der_2048), 0);
  2401. /* get ca certificate then alter it */
  2402. ExpectNotNull(der =
  2403. (byte*)XMALLOC(FOURK_BUF, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  2404. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ca_cert,
  2405. WOLFSSL_FILETYPE_ASN1));
  2406. ExpectNotNull(pt = (byte*)wolfSSL_X509_get_tbs(x509, &derSz));
  2407. if (EXPECT_SUCCESS() && (der != NULL)) {
  2408. XMEMCPY(der, pt, derSz);
  2409. /* find the name constraint extension and alter it */
  2410. pt = der;
  2411. for (i = 0; i < derSz - 3; i++) {
  2412. if (XMEMCMP(pt, extNameConsOid, 3) == 0) {
  2413. pt += 3;
  2414. break;
  2415. }
  2416. pt++;
  2417. }
  2418. ExpectIntNE(i, derSz - 3); /* did not find OID if this case is hit */
  2419. /* go to the length value and set it to 0 */
  2420. while (i < derSz && *pt != 0x81) {
  2421. pt++;
  2422. i++;
  2423. }
  2424. ExpectIntNE(i, derSz); /* did not place to alter */
  2425. pt++;
  2426. *pt = 0x00;
  2427. }
  2428. /* resign the altered certificate */
  2429. ExpectIntGT((derSz = wc_SignCert(derSz, CTC_SHA256wRSA, der,
  2430. FOURK_BUF, &key, NULL, &rng)), 0);
  2431. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2432. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2433. WOLFSSL_FILETYPE_ASN1), ASN_PARSE_E);
  2434. wolfSSL_CertManagerFree(cm);
  2435. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  2436. wolfSSL_X509_free(x509);
  2437. wc_FreeRsaKey(&key);
  2438. wc_FreeRng(&rng);
  2439. /* add email alt name to satisfy constraint */
  2440. pt = (byte*)server_key_der_2048;
  2441. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2442. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2443. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2444. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2445. WOLFSSL_FILETYPE_ASN1));
  2446. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2447. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2448. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2449. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2450. /* Good cert test with proper alt email name */
  2451. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2452. WOLFSSL_FILETYPE_PEM));
  2453. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2454. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2455. name = NULL;
  2456. ExpectNotNull(name = X509_NAME_new());
  2457. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2458. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2459. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2460. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2461. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2462. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2463. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2464. X509_NAME_free(name);
  2465. name = NULL;
  2466. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2467. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2468. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2469. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2470. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2471. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2472. wolfSSL_X509_free(x509);
  2473. x509 = NULL;
  2474. /* Cert with bad alt name list */
  2475. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2476. WOLFSSL_FILETYPE_PEM));
  2477. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2478. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2479. name = NULL;
  2480. ExpectNotNull(name = X509_NAME_new());
  2481. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2482. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2483. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2484. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2485. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2486. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2487. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2488. X509_NAME_free(name);
  2489. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2490. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2491. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2492. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2493. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2494. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2495. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2496. wolfSSL_CertManagerFree(cm);
  2497. wolfSSL_X509_free(x509);
  2498. wolfSSL_X509_free(ca);
  2499. wolfSSL_EVP_PKEY_free(priv);
  2500. #endif
  2501. return EXPECT_RESULT();
  2502. }
  2503. static int test_wolfSSL_CertManagerNameConstraint2(void)
  2504. {
  2505. EXPECT_DECLS;
  2506. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2507. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2508. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2509. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES)
  2510. const char* ca_cert = "./certs/test/cert-ext-ndir.der";
  2511. const char* ca_cert2 = "./certs/test/cert-ext-ndir-exc.der";
  2512. const char* server_cert = "./certs/server-cert.pem";
  2513. WOLFSSL_CERT_MANAGER* cm = NULL;
  2514. WOLFSSL_X509 *x509 = NULL;
  2515. WOLFSSL_X509 *ca = NULL;
  2516. const unsigned char *der = NULL;
  2517. const unsigned char *pt;
  2518. WOLFSSL_EVP_PKEY *priv = NULL;
  2519. WOLFSSL_X509_NAME* name = NULL;
  2520. int derSz = 0;
  2521. /* C=US*/
  2522. char altName[] = {
  2523. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2524. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53
  2525. };
  2526. /* C=ID */
  2527. char altNameFail[] = {
  2528. 0x30, 0x0D, 0x31, 0x0B, 0x30, 0x09,
  2529. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x49, 0x44
  2530. };
  2531. /* C=US ST=California*/
  2532. char altNameExc[] = {
  2533. 0x30, 0x22,
  2534. 0x31, 0x0B,
  2535. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53,
  2536. 0x31, 0x13,
  2537. 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x0A,
  2538. 0x43, 0x61, 0x6c, 0x69, 0x66, 0x6f, 0x72, 0x6e, 0x69, 0x61
  2539. };
  2540. /* load in CA private key for signing */
  2541. pt = ca_key_der_2048;
  2542. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  2543. sizeof_ca_key_der_2048));
  2544. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2545. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2546. WOLFSSL_FILETYPE_ASN1));
  2547. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2548. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2549. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2550. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2551. WOLFSSL_FILETYPE_PEM));
  2552. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2553. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2554. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2555. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2556. #else
  2557. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2558. #endif
  2559. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2560. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2561. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2562. /* add in matching DIR alt name and resign */
  2563. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2564. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2565. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2566. #else
  2567. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2568. #endif
  2569. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2570. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2571. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2572. wolfSSL_X509_free(x509);
  2573. x509 = NULL;
  2574. /* check verify fail */
  2575. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2576. WOLFSSL_FILETYPE_PEM));
  2577. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2578. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2579. /* add in miss matching DIR alt name and resign */
  2580. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2581. ASN_DIR_TYPE);
  2582. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2583. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2584. #else
  2585. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2586. #endif
  2587. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2588. #ifndef WOLFSSL_NO_ASN_STRICT
  2589. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2590. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2591. #else
  2592. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2593. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2594. #endif
  2595. /* check that it still fails if one bad altname and one good altname is in
  2596. * the certificate */
  2597. wolfSSL_X509_free(x509);
  2598. x509 = NULL;
  2599. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2600. WOLFSSL_FILETYPE_PEM));
  2601. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2602. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2603. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2604. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2605. ASN_DIR_TYPE);
  2606. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2607. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2608. #else
  2609. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2610. #endif
  2611. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2612. #ifndef WOLFSSL_NO_ASN_STRICT
  2613. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2614. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2615. #else
  2616. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2617. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2618. #endif
  2619. /* check it fails with switching position of bad altname */
  2620. wolfSSL_X509_free(x509);
  2621. x509 = NULL;
  2622. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2623. WOLFSSL_FILETYPE_PEM));
  2624. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2625. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2626. wolfSSL_X509_add_altname_ex(x509, altNameFail, sizeof(altNameFail),
  2627. ASN_DIR_TYPE);
  2628. wolfSSL_X509_add_altname_ex(x509, altName, sizeof(altName), ASN_DIR_TYPE);
  2629. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2630. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2631. #else
  2632. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2633. #endif
  2634. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2635. #ifndef WOLFSSL_NO_ASN_STRICT
  2636. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2637. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2638. #else
  2639. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2640. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2641. #endif
  2642. wolfSSL_CertManagerFree(cm);
  2643. wolfSSL_X509_free(x509);
  2644. x509 = NULL;
  2645. wolfSSL_X509_free(ca);
  2646. ca = NULL;
  2647. /* now test with excluded name constraint */
  2648. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2649. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert2,
  2650. WOLFSSL_FILETYPE_ASN1));
  2651. ExpectNotNull((der = wolfSSL_X509_get_der(ca, &derSz)));
  2652. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2653. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2654. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2655. WOLFSSL_FILETYPE_PEM));
  2656. wolfSSL_X509_add_altname_ex(x509, altNameExc, sizeof(altNameExc),
  2657. ASN_DIR_TYPE);
  2658. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2659. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2660. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  2661. wolfSSL_X509_sign(x509, priv, EVP_sha3_256());
  2662. #else
  2663. wolfSSL_X509_sign(x509, priv, EVP_sha256());
  2664. #endif
  2665. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  2666. #ifndef WOLFSSL_NO_ASN_STRICT
  2667. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2668. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2669. #else
  2670. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2671. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2672. #endif
  2673. wolfSSL_CertManagerFree(cm);
  2674. wolfSSL_X509_free(x509);
  2675. wolfSSL_X509_free(ca);
  2676. wolfSSL_EVP_PKEY_free(priv);
  2677. #endif
  2678. return EXPECT_RESULT();
  2679. }
  2680. static int test_wolfSSL_CertManagerNameConstraint3(void)
  2681. {
  2682. EXPECT_DECLS;
  2683. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2684. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2685. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2686. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2687. !defined(NO_SHA256)
  2688. WOLFSSL_CERT_MANAGER* cm = NULL;
  2689. WOLFSSL_EVP_PKEY *priv = NULL;
  2690. WOLFSSL_X509_NAME* name = NULL;
  2691. const char* ca_cert = "./certs/test/cert-ext-mnc.der";
  2692. const char* server_cert = "./certs/test/server-goodcn.pem";
  2693. byte *der = NULL;
  2694. int derSz = 0;
  2695. byte *pt;
  2696. WOLFSSL_X509 *x509 = NULL;
  2697. WOLFSSL_X509 *ca = NULL;
  2698. pt = (byte*)server_key_der_2048;
  2699. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2700. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2701. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2702. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2703. WOLFSSL_FILETYPE_ASN1));
  2704. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2705. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2706. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2707. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2708. /* check satisfying .wolfssl.com constraint passes */
  2709. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2710. WOLFSSL_FILETYPE_PEM));
  2711. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2712. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2713. name = NULL;
  2714. ExpectNotNull(name = X509_NAME_new());
  2715. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2716. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2717. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2718. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2719. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2720. (byte*)"support@info.wolfssl.com", 24, -1, 0), SSL_SUCCESS);
  2721. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2722. X509_NAME_free(name);
  2723. name = NULL;
  2724. wolfSSL_X509_add_altname(x509, "wolfssl@info.wolfssl.com", ASN_RFC822_TYPE);
  2725. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2726. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2727. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2728. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2729. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2730. wolfSSL_X509_free(x509);
  2731. x509 = NULL;
  2732. /* check satisfying .random.com constraint passes */
  2733. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2734. WOLFSSL_FILETYPE_PEM));
  2735. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2736. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2737. name = NULL;
  2738. ExpectNotNull(name = X509_NAME_new());
  2739. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2740. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2741. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2742. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2743. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2744. (byte*)"support@info.example.com", 24, -1, 0), SSL_SUCCESS);
  2745. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2746. X509_NAME_free(name);
  2747. name = NULL;
  2748. wolfSSL_X509_add_altname(x509, "wolfssl@info.example.com", ASN_RFC822_TYPE);
  2749. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2750. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2751. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2752. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2753. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2754. wolfSSL_X509_free(x509);
  2755. x509 = NULL;
  2756. /* check fail case when neither constraint is matched */
  2757. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2758. WOLFSSL_FILETYPE_PEM));
  2759. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2760. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2761. name = NULL;
  2762. ExpectNotNull(name = X509_NAME_new());
  2763. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2764. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2765. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2766. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2767. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  2768. (byte*)"support@info.com", 16, -1, 0), SSL_SUCCESS);
  2769. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2770. X509_NAME_free(name);
  2771. wolfSSL_X509_add_altname(x509, "wolfssl@info.com", ASN_RFC822_TYPE);
  2772. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2773. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2774. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2775. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2776. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2777. wolfSSL_CertManagerFree(cm);
  2778. wolfSSL_X509_free(x509);
  2779. wolfSSL_X509_free(ca);
  2780. wolfSSL_EVP_PKEY_free(priv);
  2781. #endif
  2782. return EXPECT_RESULT();
  2783. }
  2784. static int test_wolfSSL_CertManagerNameConstraint4(void)
  2785. {
  2786. EXPECT_DECLS;
  2787. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2788. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2789. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2790. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2791. !defined(NO_SHA256)
  2792. WOLFSSL_CERT_MANAGER* cm = NULL;
  2793. WOLFSSL_EVP_PKEY *priv = NULL;
  2794. WOLFSSL_X509_NAME* name = NULL;
  2795. const char* ca_cert = "./certs/test/cert-ext-ncdns.der";
  2796. const char* server_cert = "./certs/test/server-goodcn.pem";
  2797. byte *der = NULL;
  2798. int derSz;
  2799. byte *pt;
  2800. WOLFSSL_X509 *x509 = NULL;
  2801. WOLFSSL_X509 *ca = NULL;
  2802. pt = (byte*)server_key_der_2048;
  2803. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2804. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2805. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2806. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2807. WOLFSSL_FILETYPE_ASN1));
  2808. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2809. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2810. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2811. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2812. /* check satisfying wolfssl.com constraint passes */
  2813. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2814. WOLFSSL_FILETYPE_PEM));
  2815. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2816. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2817. name = NULL;
  2818. ExpectNotNull(name = X509_NAME_new());
  2819. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2820. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2821. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2822. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2823. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2824. X509_NAME_free(name);
  2825. name = NULL;
  2826. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2827. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2828. DEBUG_WRITE_CERT_X509(x509, "good-1st-constraint-cert.pem");
  2829. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2830. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2831. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2832. wolfSSL_X509_free(x509);
  2833. x509 = NULL;
  2834. /* check satisfying example.com constraint passes */
  2835. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2836. WOLFSSL_FILETYPE_PEM));
  2837. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2838. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2839. name = NULL;
  2840. ExpectNotNull(name = X509_NAME_new());
  2841. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2842. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2843. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2844. (byte*)"example.com", 11, -1, 0), SSL_SUCCESS);
  2845. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2846. X509_NAME_free(name);
  2847. name = NULL;
  2848. wolfSSL_X509_add_altname(x509, "www.example.com", ASN_DNS_TYPE);
  2849. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2850. DEBUG_WRITE_CERT_X509(x509, "good-2nd-constraint-cert.pem");
  2851. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2852. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2853. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2854. wolfSSL_X509_free(x509);
  2855. x509 = NULL;
  2856. /* check satisfying wolfssl.com constraint passes with list of DNS's */
  2857. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2858. WOLFSSL_FILETYPE_PEM));
  2859. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2860. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2861. name = NULL;
  2862. ExpectNotNull(name = X509_NAME_new());
  2863. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2864. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2865. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2866. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2867. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2868. X509_NAME_free(name);
  2869. name = NULL;
  2870. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2871. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2872. wolfSSL_X509_add_altname(x509, "extra.wolfssl.com", ASN_DNS_TYPE);
  2873. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2874. DEBUG_WRITE_CERT_X509(x509, "good-multiple-constraint-cert.pem");
  2875. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2876. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2877. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2878. wolfSSL_X509_free(x509);
  2879. x509 = NULL;
  2880. /* check fail when one DNS in the list is bad */
  2881. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2882. WOLFSSL_FILETYPE_PEM));
  2883. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2884. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2885. name = NULL;
  2886. ExpectNotNull(name = X509_NAME_new());
  2887. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2888. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2889. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2890. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2891. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2892. X509_NAME_free(name);
  2893. name = NULL;
  2894. wolfSSL_X509_add_altname(x509, "www.wolfssl.com", ASN_DNS_TYPE);
  2895. wolfSSL_X509_add_altname(x509, "www.nomatch.com", ASN_DNS_TYPE);
  2896. wolfSSL_X509_add_altname(x509, "www.info.wolfssl.com", ASN_DNS_TYPE);
  2897. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2898. DEBUG_WRITE_CERT_X509(x509, "bad-multiple-constraint-cert.pem");
  2899. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2900. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2901. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2902. wolfSSL_X509_free(x509);
  2903. x509 = NULL;
  2904. /* check fail case when neither constraint is matched */
  2905. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2906. WOLFSSL_FILETYPE_PEM));
  2907. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2908. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2909. name = NULL;
  2910. ExpectNotNull(name = X509_NAME_new());
  2911. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2912. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2913. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2914. (byte*)"common", 6, -1, 0), SSL_SUCCESS);
  2915. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2916. X509_NAME_free(name);
  2917. wolfSSL_X509_add_altname(x509, "www.random.com", ASN_DNS_TYPE);
  2918. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2919. DEBUG_WRITE_CERT_X509(x509, "bad-cert.pem");
  2920. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2921. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2922. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  2923. wolfSSL_CertManagerFree(cm);
  2924. wolfSSL_X509_free(x509);
  2925. wolfSSL_X509_free(ca);
  2926. wolfSSL_EVP_PKEY_free(priv);
  2927. #endif
  2928. return EXPECT_RESULT();
  2929. }
  2930. static int test_wolfSSL_CertManagerNameConstraint5(void)
  2931. {
  2932. EXPECT_DECLS;
  2933. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  2934. !defined(NO_WOLFSSL_CM_VERIFY) && !defined(NO_RSA) && \
  2935. defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && \
  2936. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_ALT_NAMES) && \
  2937. !defined(NO_SHA256)
  2938. WOLFSSL_CERT_MANAGER* cm = NULL;
  2939. WOLFSSL_EVP_PKEY *priv = NULL;
  2940. WOLFSSL_X509_NAME* name = NULL;
  2941. const char* ca_cert = "./certs/test/cert-ext-ncmixed.der";
  2942. const char* server_cert = "./certs/test/server-goodcn.pem";
  2943. byte *der = NULL;
  2944. int derSz;
  2945. byte *pt;
  2946. WOLFSSL_X509 *x509 = NULL;
  2947. WOLFSSL_X509 *ca = NULL;
  2948. pt = (byte*)server_key_der_2048;
  2949. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  2950. (const unsigned char**)&pt, sizeof_server_key_der_2048));
  2951. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  2952. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(ca_cert,
  2953. WOLFSSL_FILETYPE_ASN1));
  2954. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(ca, &derSz)));
  2955. DEBUG_WRITE_DER(der, derSz, "ca.der");
  2956. ExpectIntEQ(wolfSSL_CertManagerLoadCABuffer(cm, der, derSz,
  2957. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2958. /* check satisfying wolfssl.com constraint passes */
  2959. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2960. WOLFSSL_FILETYPE_PEM));
  2961. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2962. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2963. name = NULL;
  2964. ExpectNotNull(name = X509_NAME_new());
  2965. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2966. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2967. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2968. (byte*)"example", 7, -1, 0), SSL_SUCCESS);
  2969. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2970. X509_NAME_free(name);
  2971. name = NULL;
  2972. wolfSSL_X509_add_altname(x509, "good.example", ASN_DNS_TYPE);
  2973. wolfSSL_X509_add_altname(x509, "facts@into.wolfssl.com", ASN_RFC822_TYPE);
  2974. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2975. DEBUG_WRITE_CERT_X509(x509, "good-cert.pem");
  2976. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  2977. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  2978. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  2979. wolfSSL_X509_free(x509);
  2980. x509 = NULL;
  2981. /* fail with DNS check because of common name */
  2982. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  2983. WOLFSSL_FILETYPE_PEM));
  2984. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  2985. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  2986. name = NULL;
  2987. ExpectNotNull(name = X509_NAME_new());
  2988. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  2989. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  2990. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  2991. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  2992. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  2993. X509_NAME_free(name);
  2994. name = NULL;
  2995. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  2996. wolfSSL_X509_add_altname(x509, "facts@wolfssl.com", ASN_RFC822_TYPE);
  2997. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  2998. DEBUG_WRITE_CERT_X509(x509, "bad-cn-cert.pem");
  2999. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3000. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3001. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  3002. wolfSSL_X509_free(x509);
  3003. x509 = NULL;
  3004. /* fail on permitted DNS name constraint */
  3005. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3006. WOLFSSL_FILETYPE_PEM));
  3007. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3008. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3009. name = NULL;
  3010. ExpectNotNull(name = X509_NAME_new());
  3011. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3012. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3013. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3014. X509_NAME_free(name);
  3015. name = NULL;
  3016. wolfSSL_X509_add_altname(x509, "www.example", ASN_DNS_TYPE);
  3017. wolfSSL_X509_add_altname(x509, "www.wolfssl", ASN_DNS_TYPE);
  3018. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  3019. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3020. DEBUG_WRITE_CERT_X509(x509, "bad-1st-constraint-cert.pem");
  3021. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3022. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3023. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  3024. wolfSSL_X509_free(x509);
  3025. x509 = NULL;
  3026. /* fail on permitted email name constraint */
  3027. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3028. WOLFSSL_FILETYPE_PEM));
  3029. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3030. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3031. name = NULL;
  3032. ExpectNotNull(name = X509_NAME_new());
  3033. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3034. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3035. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3036. X509_NAME_free(name);
  3037. name = NULL;
  3038. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  3039. wolfSSL_X509_add_altname(x509, "info@wolfssl.com", ASN_RFC822_TYPE);
  3040. wolfSSL_X509_add_altname(x509, "info@example.com", ASN_RFC822_TYPE);
  3041. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3042. DEBUG_WRITE_CERT_X509(x509, "bad-2nd-constraint-cert.pem");
  3043. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3044. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3045. WOLFSSL_FILETYPE_ASN1), ASN_NAME_INVALID_E);
  3046. wolfSSL_X509_free(x509);
  3047. x509 = NULL;
  3048. /* success with empty email name */
  3049. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(server_cert,
  3050. WOLFSSL_FILETYPE_PEM));
  3051. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  3052. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  3053. name = NULL;
  3054. ExpectNotNull(name = X509_NAME_new());
  3055. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  3056. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  3057. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  3058. X509_NAME_free(name);
  3059. wolfSSL_X509_add_altname(x509, "example", ASN_DNS_TYPE);
  3060. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  3061. DEBUG_WRITE_CERT_X509(x509, "good-missing-constraint-cert.pem");
  3062. ExpectNotNull((der = (byte*)wolfSSL_X509_get_der(x509, &derSz)));
  3063. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, der, derSz,
  3064. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3065. wolfSSL_X509_free(x509);
  3066. wolfSSL_CertManagerFree(cm);
  3067. wolfSSL_X509_free(ca);
  3068. wolfSSL_EVP_PKEY_free(priv);
  3069. #endif
  3070. return EXPECT_RESULT();
  3071. }
  3072. static int test_wolfSSL_CertManagerCRL(void)
  3073. {
  3074. EXPECT_DECLS;
  3075. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(HAVE_CRL) && \
  3076. !defined(NO_RSA)
  3077. const char* ca_cert = "./certs/ca-cert.pem";
  3078. const char* crl1 = "./certs/crl/crl.pem";
  3079. const char* crl2 = "./certs/crl/crl2.pem";
  3080. #ifdef WC_RSA_PSS
  3081. const char* crl_rsapss = "./certs/crl/crl_rsapss.pem";
  3082. const char* ca_rsapss = "./certs/rsapss/ca-rsapss.pem";
  3083. #endif
  3084. const unsigned char crl_buff[] = {
  3085. 0x30, 0x82, 0x02, 0x04, 0x30, 0x81, 0xed, 0x02,
  3086. 0x01, 0x01, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  3087. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05,
  3088. 0x00, 0x30, 0x81, 0x94, 0x31, 0x0b, 0x30, 0x09,
  3089. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  3090. 0x53, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55,
  3091. 0x04, 0x08, 0x0c, 0x07, 0x4d, 0x6f, 0x6e, 0x74,
  3092. 0x61, 0x6e, 0x61, 0x31, 0x10, 0x30, 0x0e, 0x06,
  3093. 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x42, 0x6f,
  3094. 0x7a, 0x65, 0x6d, 0x61, 0x6e, 0x31, 0x11, 0x30,
  3095. 0x0f, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x08,
  3096. 0x53, 0x61, 0x77, 0x74, 0x6f, 0x6f, 0x74, 0x68,
  3097. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04,
  3098. 0x0b, 0x0c, 0x0a, 0x43, 0x6f, 0x6e, 0x73, 0x75,
  3099. 0x6c, 0x74, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30,
  3100. 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f,
  3101. 0x77, 0x77, 0x77, 0x2e, 0x77, 0x6f, 0x6c, 0x66,
  3102. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x31,
  3103. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  3104. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10,
  3105. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c,
  3106. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d,
  3107. 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, 0x36,
  3108. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x17,
  3109. 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, 0x32,
  3110. 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, 0x14,
  3111. 0x30, 0x12, 0x02, 0x01, 0x02, 0x17, 0x0d, 0x32,
  3112. 0x32, 0x31, 0x32, 0x31, 0x36, 0x32, 0x31, 0x31,
  3113. 0x37, 0x35, 0x30, 0x5a, 0xa0, 0x0e, 0x30, 0x0c,
  3114. 0x30, 0x0a, 0x06, 0x03, 0x55, 0x1d, 0x14, 0x04,
  3115. 0x03, 0x02, 0x01, 0x02, 0x30, 0x0d, 0x06, 0x09,
  3116. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01,
  3117. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00,
  3118. 0x39, 0x44, 0xff, 0x39, 0xf4, 0x04, 0x45, 0x79,
  3119. 0x7e, 0x73, 0xe2, 0x42, 0x48, 0xdb, 0x85, 0x66,
  3120. 0xfd, 0x99, 0x76, 0x94, 0x7c, 0xb5, 0x79, 0x5d,
  3121. 0x15, 0x71, 0x36, 0xa9, 0x87, 0xf0, 0x73, 0x05,
  3122. 0x50, 0x08, 0x6b, 0x1c, 0x6e, 0xde, 0x96, 0x45,
  3123. 0x31, 0xc3, 0xc0, 0xba, 0xba, 0xf5, 0x08, 0x1d,
  3124. 0x05, 0x4a, 0x52, 0x39, 0xe9, 0x03, 0xef, 0x59,
  3125. 0xc8, 0x1d, 0x4a, 0xf2, 0x86, 0x05, 0x99, 0x7b,
  3126. 0x4b, 0x74, 0xf6, 0xd3, 0x75, 0x8d, 0xb2, 0x57,
  3127. 0xba, 0xac, 0xa7, 0x11, 0x14, 0xd6, 0x6c, 0x71,
  3128. 0xc4, 0x4c, 0x1c, 0x68, 0xbc, 0x49, 0x78, 0xf0,
  3129. 0xc9, 0x52, 0x8a, 0xe7, 0x8b, 0x54, 0xe6, 0x20,
  3130. 0x58, 0x20, 0x60, 0x66, 0xf5, 0x14, 0xd8, 0xcb,
  3131. 0xff, 0xe0, 0xa0, 0x45, 0xbc, 0xb4, 0x81, 0xad,
  3132. 0x1d, 0xbc, 0xcf, 0xf8, 0x8e, 0xa8, 0x87, 0x24,
  3133. 0x55, 0x99, 0xd9, 0xce, 0x47, 0xf7, 0x5b, 0x4a,
  3134. 0x33, 0x6d, 0xdb, 0xbf, 0x93, 0x64, 0x1a, 0xa6,
  3135. 0x46, 0x5f, 0x27, 0xdc, 0xd8, 0xd4, 0xf9, 0xc2,
  3136. 0x42, 0x2a, 0x7e, 0xb2, 0x7c, 0xdd, 0x98, 0x77,
  3137. 0xf5, 0x88, 0x7d, 0x15, 0x25, 0x08, 0xbc, 0xe0,
  3138. 0xd0, 0x8d, 0xf4, 0xc3, 0xc3, 0x04, 0x41, 0xa4,
  3139. 0xd1, 0xb1, 0x39, 0x4a, 0x6b, 0x2c, 0xb5, 0x2e,
  3140. 0x9a, 0x65, 0x43, 0x0d, 0x0e, 0x73, 0xf4, 0x06,
  3141. 0xe1, 0xb3, 0x49, 0x34, 0x94, 0xb0, 0xb7, 0xff,
  3142. 0xc0, 0x27, 0xc1, 0xb5, 0xea, 0x06, 0xf7, 0x71,
  3143. 0x71, 0x97, 0xbb, 0xbc, 0xc7, 0x1a, 0x9f, 0xeb,
  3144. 0xf6, 0x3d, 0xa5, 0x7b, 0x55, 0xa7, 0xbf, 0xdd,
  3145. 0xd7, 0xee, 0x97, 0xb8, 0x9d, 0xdc, 0xcd, 0xe3,
  3146. 0x06, 0xdb, 0x9a, 0x2c, 0x60, 0xbf, 0x70, 0x84,
  3147. 0xfa, 0x6b, 0x8d, 0x70, 0x7d, 0xde, 0xe8, 0xb7,
  3148. 0xab, 0xb0, 0x38, 0x68, 0x6c, 0xc0, 0xb1, 0xe1,
  3149. 0xba, 0x45, 0xe0, 0xd7, 0x12, 0x3d, 0x71, 0x5b
  3150. };
  3151. WOLFSSL_CERT_MANAGER* cm = NULL;
  3152. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3153. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(NULL, 0), BAD_FUNC_ARG);
  3154. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  3155. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECK), 1);
  3156. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm,
  3157. WOLFSSL_CRL_CHECK | WOLFSSL_CRL_CHECKALL), 1);
  3158. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, 16), 1);
  3159. ExpectIntEQ(wolfSSL_CertManagerEnableCRL(cm, WOLFSSL_CRL_CHECKALL), 1);
  3160. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, -1), BAD_FUNC_ARG);
  3161. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, -1), BAD_FUNC_ARG);
  3162. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, -1),
  3163. BAD_FUNC_ARG);
  3164. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, NULL, 1), BAD_FUNC_ARG);
  3165. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(NULL, server_cert_der_2048, 1),
  3166. BAD_FUNC_ARG);
  3167. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, NULL, 1), BAD_FUNC_ARG);
  3168. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048, -1),
  3169. BAD_FUNC_ARG);
  3170. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  3171. sizeof_server_cert_der_2048), ASN_NO_SIGNER_E);
  3172. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(NULL, NULL), BAD_FUNC_ARG);
  3173. ExpectIntEQ(wolfSSL_CertManagerSetCRL_Cb(cm, NULL), 1);
  3174. #ifdef HAVE_CRL_IO
  3175. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(NULL, NULL), BAD_FUNC_ARG);
  3176. ExpectIntEQ(wolfSSL_CertManagerSetCRL_IOCb(cm, NULL), 1);
  3177. #endif
  3178. #ifndef NO_FILESYSTEM
  3179. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(NULL, NULL, WOLFSSL_FILETYPE_ASN1,
  3180. 0), BAD_FUNC_ARG);
  3181. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, NULL, WOLFSSL_FILETYPE_ASN1,
  3182. 0), BAD_FUNC_ARG);
  3183. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  3184. ExpectIntEQ(wolfSSL_CertManagerLoadCRL(cm, "./certs/crl", -1, 0), 1);
  3185. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(NULL, NULL,
  3186. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3187. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, NULL, WOLFSSL_FILETYPE_ASN1),
  3188. BAD_FUNC_ARG);
  3189. /* -1 seen as !WOLFSSL_FILETYPE_PEM */
  3190. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, "./certs/crl/crl.pem", -1),
  3191. ASN_PARSE_E);
  3192. #endif
  3193. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, -1,
  3194. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3195. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, -1,
  3196. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3197. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, -1,
  3198. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3199. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, NULL, 1,
  3200. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3201. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(NULL, crl_buff, 1,
  3202. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3203. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, NULL, 1,
  3204. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3205. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, -1,
  3206. WOLFSSL_FILETYPE_ASN1), BAD_FUNC_ARG);
  3207. ExpectIntEQ(wolfSSL_CertManagerFreeCRL(NULL), BAD_FUNC_ARG);
  3208. DoExpectIntEQ(wolfSSL_CertManagerFreeCRL(cm), 1);
  3209. ExpectIntEQ(WOLFSSL_SUCCESS,
  3210. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3211. ExpectIntEQ(WOLFSSL_SUCCESS,
  3212. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  3213. ExpectIntEQ(WOLFSSL_SUCCESS,
  3214. wolfSSL_CertManagerLoadCRL(cm, crl2, WOLFSSL_FILETYPE_PEM, 0));
  3215. wolfSSL_CertManagerFreeCRL(cm);
  3216. ExpectIntEQ(WOLFSSL_SUCCESS,
  3217. wolfSSL_CertManagerLoadCRL(cm, crl1, WOLFSSL_FILETYPE_PEM, 0));
  3218. ExpectIntEQ(WOLFSSL_SUCCESS,
  3219. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3220. ExpectIntEQ(wolfSSL_CertManagerCheckCRL(cm, server_cert_der_2048,
  3221. sizeof_server_cert_der_2048), CRL_MISSING);
  3222. ExpectIntEQ(wolfSSL_CertManagerVerifyBuffer(cm, server_cert_der_2048,
  3223. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1), CRL_MISSING);
  3224. ExpectIntEQ(wolfSSL_CertManagerLoadCRLBuffer(cm, crl_buff, sizeof(crl_buff),
  3225. WOLFSSL_FILETYPE_ASN1), 1);
  3226. #if !defined(NO_FILESYSTEM) && defined(WC_RSA_PSS)
  3227. /* loading should fail without the CA set */
  3228. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, crl_rsapss,
  3229. WOLFSSL_FILETYPE_PEM), ASN_CRL_NO_SIGNER_E);
  3230. /* now successfully load the RSA-PSS crl once loading in it's CA */
  3231. ExpectIntEQ(WOLFSSL_SUCCESS,
  3232. wolfSSL_CertManagerLoadCA(cm, ca_rsapss, NULL));
  3233. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, crl_rsapss,
  3234. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3235. #endif
  3236. wolfSSL_CertManagerFree(cm);
  3237. #endif
  3238. return EXPECT_RESULT();
  3239. }
  3240. static int test_wolfSSL_CertManagerCheckOCSPResponse(void)
  3241. {
  3242. EXPECT_DECLS;
  3243. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA)
  3244. /* Need one of these for wolfSSL_OCSP_REQUEST_new. */
  3245. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  3246. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_APACHE_HTTPD) || \
  3247. defined(HAVE_LIGHTY)
  3248. WOLFSSL_CERT_MANAGER* cm = NULL;
  3249. /* Raw OCSP response bytes captured using the following setup:
  3250. * - Run responder with
  3251. * openssl ocsp -port 9999 -ndays 9999
  3252. * -index certs/ocsp/index-intermediate1-ca-issued-certs.txt
  3253. * -rsigner certs/ocsp/ocsp-responder-cert.pem
  3254. * -rkey certs/ocsp/ocsp-responder-key.pem
  3255. * -CA certs/ocsp/intermediate1-ca-cert.pem
  3256. * - Run client with
  3257. * openssl ocsp -host 127.0.0.1:9999 -respout resp.out
  3258. * -issuer certs/ocsp/intermediate1-ca-cert.pem
  3259. * -cert certs/ocsp/server1-cert.pem
  3260. * -CAfile certs/ocsp/root-ca-cert.pem -noverify
  3261. * - Select the response packet in Wireshark, and export it using
  3262. * "File->Export Packet Dissection->As "C" Arrays". Select "Selected
  3263. * packets only". After importing into the editor, remove the initial
  3264. * ~148 bytes of header, ending with the Content-Length and the \r\n\r\n.
  3265. */
  3266. static const byte response[] = {
  3267. 0x30, 0x82, 0x07, 0x40, /* ....0..@ */
  3268. 0x0a, 0x01, 0x00, 0xa0, 0x82, 0x07, 0x39, 0x30, /* ......90 */
  3269. 0x82, 0x07, 0x35, 0x06, 0x09, 0x2b, 0x06, 0x01, /* ..5..+.. */
  3270. 0x05, 0x05, 0x07, 0x30, 0x01, 0x01, 0x04, 0x82, /* ...0.... */
  3271. 0x07, 0x26, 0x30, 0x82, 0x07, 0x22, 0x30, 0x82, /* .&0.."0. */
  3272. 0x01, 0x40, 0xa1, 0x81, 0xa1, 0x30, 0x81, 0x9e, /* .@...0.. */
  3273. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3274. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  3275. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  3276. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  3277. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  3278. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  3279. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  3280. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  3281. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  3282. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  3283. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  3284. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, 0x30, 0x1d, /* ring1.0. */
  3285. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x16, 0x77, /* ..U....w */
  3286. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x4f, /* olfSSL O */
  3287. 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, 0x73, 0x70, /* CSP Resp */
  3288. 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, 0x1f, 0x30, /* onder1.0 */
  3289. 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, /* ...*.H.. */
  3290. 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, /* ......in */
  3291. 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, /* fo@wolfs */
  3292. 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x18, 0x0f, /* sl.com.. */
  3293. 0x32, 0x30, 0x32, 0x33, 0x31, 0x31, 0x30, 0x38, /* 20231108 */
  3294. 0x30, 0x30, 0x32, 0x36, 0x33, 0x37, 0x5a, 0x30, /* 002637Z0 */
  3295. 0x64, 0x30, 0x62, 0x30, 0x3a, 0x30, 0x09, 0x06, /* d0b0:0.. */
  3296. 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, /* .+...... */
  3297. 0x04, 0x14, 0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, /* ..qM.#@Y */
  3298. 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, /* ...7C.1. */
  3299. 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04, 0x04, 0x14, /* ..C..... */
  3300. 0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, /* ..:.,... */
  3301. 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, /* ..L.*.q. */
  3302. 0x64, 0x44, 0xda, 0x0e, 0x02, 0x01, 0x05, 0x80, /* dD...... */
  3303. 0x00, 0x18, 0x0f, 0x32, 0x30, 0x32, 0x33, 0x31, /* ...20231 */
  3304. 0x31, 0x30, 0x38, 0x30, 0x30, 0x32, 0x36, 0x33, /* 10800263 */
  3305. 0x37, 0x5a, 0xa0, 0x11, 0x18, 0x0f, 0x32, 0x30, /* 7Z....20 */
  3306. 0x35, 0x31, 0x30, 0x33, 0x32, 0x35, 0x30, 0x30, /* 51032500 */
  3307. 0x32, 0x36, 0x33, 0x37, 0x5a, 0xa1, 0x23, 0x30, /* 2637Z.#0 */
  3308. 0x21, 0x30, 0x1f, 0x06, 0x09, 0x2b, 0x06, 0x01, /* !0...+.. */
  3309. 0x05, 0x05, 0x07, 0x30, 0x01, 0x02, 0x04, 0x12, /* ...0.... */
  3310. 0x04, 0x10, 0xdb, 0xbc, 0x2a, 0x76, 0xa0, 0xb4, /* ....*v.. */
  3311. 0x1e, 0x5d, 0xf6, 0x2b, 0x8e, 0x38, 0x62, 0xdb, /* .].+.8b. */
  3312. 0x90, 0xed, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, /* ..0...*. */
  3313. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, /* H....... */
  3314. 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, 0x87, 0xde, /* ........ */
  3315. 0xfb, 0xf9, 0x3a, 0x90, 0x1f, 0x90, 0xde, 0xcf, /* ..:..... */
  3316. 0xfe, 0xad, 0x64, 0x19, 0x34, 0x17, 0xf8, 0x15, /* ..d.4... */
  3317. 0x01, 0x22, 0x5f, 0x67, 0x41, 0xa4, 0x18, 0xf7, /* ."_gA... */
  3318. 0x16, 0xb7, 0xc9, 0xf3, 0xe1, 0x9f, 0xcd, 0x40, /* .......@ */
  3319. 0x56, 0x77, 0x6e, 0x6a, 0xfb, 0x92, 0x6a, 0x6f, /* Vwnj..jo */
  3320. 0x28, 0x3e, 0x22, 0x48, 0xa1, 0xc2, 0xd8, 0x1d, /* (>"H.... */
  3321. 0xc7, 0xe6, 0x78, 0x7f, 0xb6, 0x09, 0xfe, 0x2c, /* ..x...., */
  3322. 0xb5, 0xef, 0x29, 0x7c, 0xc5, 0x51, 0x16, 0x7b, /* ..)|.Q.{ */
  3323. 0x8f, 0xfb, 0x44, 0xa8, 0xcd, 0xf5, 0x5c, 0x0f, /* ..D...\. */
  3324. 0x46, 0x0e, 0xb1, 0xa4, 0xeb, 0x5b, 0xf5, 0x86, /* F....[.. */
  3325. 0x11, 0x0f, 0xcd, 0xe2, 0xe5, 0x3c, 0x91, 0x72, /* .....<.r */
  3326. 0x0d, 0x6a, 0xcb, 0x95, 0x99, 0x39, 0x91, 0x48, /* .j...9.H */
  3327. 0x65, 0x97, 0xb9, 0x78, 0xb5, 0x88, 0x7f, 0x76, /* e..x...v */
  3328. 0xa1, 0x43, 0x2f, 0xf6, 0x1f, 0x49, 0xb7, 0x08, /* .C/..I.. */
  3329. 0x36, 0xe4, 0x2e, 0x34, 0x25, 0xda, 0x16, 0x74, /* 6..4%..t */
  3330. 0x47, 0x62, 0x56, 0xff, 0x2f, 0x02, 0x03, 0x44, /* GbV./..D */
  3331. 0x89, 0x04, 0xe7, 0xb8, 0xde, 0x0a, 0x35, 0x43, /* ......5C */
  3332. 0xae, 0xd7, 0x54, 0xbe, 0xc3, 0x7c, 0x95, 0xa5, /* ..T..|.. */
  3333. 0xc8, 0xe0, 0x2e, 0x52, 0xb6, 0xea, 0x99, 0x45, /* ...R...E */
  3334. 0xfd, 0xda, 0x4b, 0xd5, 0x79, 0x07, 0x64, 0xca, /* ..K.y.d. */
  3335. 0x64, 0xba, 0x52, 0x12, 0x62, 0x8c, 0x08, 0x9a, /* d.R.b... */
  3336. 0x32, 0xeb, 0x85, 0x65, 0x05, 0x39, 0x07, 0x5d, /* 2..e.9.] */
  3337. 0x39, 0x4a, 0xcf, 0xa5, 0x30, 0xf6, 0xd1, 0xf7, /* 9J..0... */
  3338. 0x29, 0xaa, 0x23, 0x42, 0xc6, 0x85, 0x16, 0x7f, /* ).#B.... */
  3339. 0x64, 0x16, 0xb1, 0xb0, 0x5d, 0xcd, 0x88, 0x2d, /* d...]..- */
  3340. 0x06, 0xb0, 0xa9, 0xdf, 0xa3, 0x9f, 0x25, 0x41, /* ......%A */
  3341. 0x89, 0x9a, 0x19, 0xe1, 0xaa, 0xcd, 0xdf, 0x51, /* .......Q */
  3342. 0xcb, 0xa9, 0xc3, 0x7e, 0x27, 0xbc, 0x7d, 0x9b, /* ...~'.}. */
  3343. 0x6f, 0x4d, 0x79, 0x87, 0x09, 0x3f, 0xac, 0xd2, /* oMy..?.. */
  3344. 0x4a, 0x3b, 0xbe, 0xf8, 0x7a, 0xa4, 0x93, 0x45, /* J;..z..E */
  3345. 0x11, 0x64, 0x40, 0xc5, 0x03, 0xc9, 0x24, 0x5b, /* .d@...$[ */
  3346. 0xe9, 0x6d, 0xfc, 0x94, 0x08, 0xbe, 0xa0, 0x82, /* .m...... */
  3347. 0x04, 0xc6, 0x30, 0x82, 0x04, 0xc2, 0x30, 0x82, /* ..0...0. */
  3348. 0x04, 0xbe, 0x30, 0x82, 0x03, 0xa6, 0xa0, 0x03, /* ..0..... */
  3349. 0x02, 0x01, 0x02, 0x02, 0x01, 0x04, 0x30, 0x0d, /* ......0. */
  3350. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, /* ..*.H... */
  3351. 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x81, 0x97, /* .....0.. */
  3352. 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3353. 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x13, 0x30, /* ...US1.0 */
  3354. 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0c, 0x0a, /* ...U.... */
  3355. 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, 0x67, 0x74, /* Washingt */
  3356. 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, /* on1.0... */
  3357. 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, 0x65, 0x61, /* U....Sea */
  3358. 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, 0x30, 0x0e, /* ttle1.0. */
  3359. 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, 0x07, 0x77, /* ..U....w */
  3360. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x31, 0x14, /* olfSSL1. */
  3361. 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, 0x0b, 0x0c, /* 0...U... */
  3362. 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, 0x65, 0x65, /* .Enginee */
  3363. 0x72, 0x69, 0x6e, 0x67, 0x31, 0x18, 0x30, 0x16, /* ring1.0. */
  3364. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, 0x0f, 0x77, /* ..U....w */
  3365. 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, 0x20, 0x72, /* olfSSL r */
  3366. 0x6f, 0x6f, 0x74, 0x20, 0x43, 0x41, 0x31, 0x1f, /* oot CA1. */
  3367. 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, /* 0...*.H. */
  3368. 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, /* .......i */
  3369. 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, 0x66, /* nfo@wolf */
  3370. 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x30, /* ssl.com0 */
  3371. 0x1e, 0x17, 0x0d, 0x32, 0x32, 0x31, 0x32, 0x31, /* ...22121 */
  3372. 0x36, 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, /* 6211750Z */
  3373. 0x17, 0x0d, 0x32, 0x35, 0x30, 0x39, 0x31, 0x31, /* ..250911 */
  3374. 0x32, 0x31, 0x31, 0x37, 0x35, 0x30, 0x5a, 0x30, /* 211750Z0 */
  3375. 0x81, 0x9e, 0x31, 0x0b, 0x30, 0x09, 0x06, 0x03, /* ..1.0... */
  3376. 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, /* U....US1 */
  3377. 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x08, /* .0...U.. */
  3378. 0x0c, 0x0a, 0x57, 0x61, 0x73, 0x68, 0x69, 0x6e, /* ..Washin */
  3379. 0x67, 0x74, 0x6f, 0x6e, 0x31, 0x10, 0x30, 0x0e, /* gton1.0. */
  3380. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0c, 0x07, 0x53, /* ..U....S */
  3381. 0x65, 0x61, 0x74, 0x74, 0x6c, 0x65, 0x31, 0x10, /* eattle1. */
  3382. 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x0a, 0x0c, /* 0...U... */
  3383. 0x07, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  3384. 0x31, 0x14, 0x30, 0x12, 0x06, 0x03, 0x55, 0x04, /* 1.0...U. */
  3385. 0x0b, 0x0c, 0x0b, 0x45, 0x6e, 0x67, 0x69, 0x6e, /* ...Engin */
  3386. 0x65, 0x65, 0x72, 0x69, 0x6e, 0x67, 0x31, 0x1f, /* eering1. */
  3387. 0x30, 0x1d, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0c, /* 0...U... */
  3388. 0x16, 0x77, 0x6f, 0x6c, 0x66, 0x53, 0x53, 0x4c, /* .wolfSSL */
  3389. 0x20, 0x4f, 0x43, 0x53, 0x50, 0x20, 0x52, 0x65, /* OCSP Re */
  3390. 0x73, 0x70, 0x6f, 0x6e, 0x64, 0x65, 0x72, 0x31, /* sponder1 */
  3391. 0x1f, 0x30, 0x1d, 0x06, 0x09, 0x2a, 0x86, 0x48, /* .0...*.H */
  3392. 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x01, 0x16, 0x10, /* ........ */
  3393. 0x69, 0x6e, 0x66, 0x6f, 0x40, 0x77, 0x6f, 0x6c, /* info@wol */
  3394. 0x66, 0x73, 0x73, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, /* fssl.com */
  3395. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d, 0x06, 0x09, /* 0.."0... */
  3396. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  3397. 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00, /* ........ */
  3398. 0x30, 0x82, 0x01, 0x0a, 0x02, 0x82, 0x01, 0x01, /* 0....... */
  3399. 0x00, 0xb8, 0xba, 0x23, 0xb4, 0xf6, 0xc3, 0x7b, /* ...#...{ */
  3400. 0x14, 0xc3, 0xa4, 0xf5, 0x1d, 0x61, 0xa1, 0xf5, /* .....a.. */
  3401. 0x1e, 0x63, 0xb9, 0x85, 0x23, 0x34, 0x50, 0x6d, /* .c..#4Pm */
  3402. 0xf8, 0x7c, 0xa2, 0x8a, 0x04, 0x8b, 0xd5, 0x75, /* .|.....u */
  3403. 0x5c, 0x2d, 0xf7, 0x63, 0x88, 0xd1, 0x07, 0x7a, /* \-.c...z */
  3404. 0xea, 0x0b, 0x45, 0x35, 0x2b, 0xeb, 0x1f, 0xb1, /* ..E5+... */
  3405. 0x22, 0xb4, 0x94, 0x41, 0x38, 0xe2, 0x9d, 0x74, /* "..A8..t */
  3406. 0xd6, 0x8b, 0x30, 0x22, 0x10, 0x51, 0xc5, 0xdb, /* ..0".Q.. */
  3407. 0xca, 0x3f, 0x46, 0x2b, 0xfe, 0xe5, 0x5a, 0x3f, /* .?F+..Z? */
  3408. 0x41, 0x74, 0x67, 0x75, 0x95, 0xa9, 0x94, 0xd5, /* Atgu.... */
  3409. 0xc3, 0xee, 0x42, 0xf8, 0x8d, 0xeb, 0x92, 0x95, /* ..B..... */
  3410. 0xe1, 0xd9, 0x65, 0xb7, 0x43, 0xc4, 0x18, 0xde, /* ..e.C... */
  3411. 0x16, 0x80, 0x90, 0xce, 0x24, 0x35, 0x21, 0xc4, /* ....$5!. */
  3412. 0x55, 0xac, 0x5a, 0x51, 0xe0, 0x2e, 0x2d, 0xb3, /* U.ZQ..-. */
  3413. 0x0a, 0x5a, 0x4f, 0x4a, 0x73, 0x31, 0x50, 0xee, /* .ZOJs1P. */
  3414. 0x4a, 0x16, 0xbd, 0x39, 0x8b, 0xad, 0x05, 0x48, /* J..9...H */
  3415. 0x87, 0xb1, 0x99, 0xe2, 0x10, 0xa7, 0x06, 0x72, /* .......r */
  3416. 0x67, 0xca, 0x5c, 0xd1, 0x97, 0xbd, 0xc8, 0xf1, /* g.\..... */
  3417. 0x76, 0xf8, 0xe0, 0x4a, 0xec, 0xbc, 0x93, 0xf4, /* v..J.... */
  3418. 0x66, 0x4c, 0x28, 0x71, 0xd1, 0xd8, 0x66, 0x03, /* fL(q..f. */
  3419. 0xb4, 0x90, 0x30, 0xbb, 0x17, 0xb0, 0xfe, 0x97, /* ..0..... */
  3420. 0xf5, 0x1e, 0xe8, 0xc7, 0x5d, 0x9b, 0x8b, 0x11, /* ....]... */
  3421. 0x19, 0x12, 0x3c, 0xab, 0x82, 0x71, 0x78, 0xff, /* ..<..qx. */
  3422. 0xae, 0x3f, 0x32, 0xb2, 0x08, 0x71, 0xb2, 0x1b, /* .?2..q.. */
  3423. 0x8c, 0x27, 0xac, 0x11, 0xb8, 0xd8, 0x43, 0x49, /* .'....CI */
  3424. 0xcf, 0xb0, 0x70, 0xb1, 0xf0, 0x8c, 0xae, 0xda, /* ..p..... */
  3425. 0x24, 0x87, 0x17, 0x3b, 0xd8, 0x04, 0x65, 0x6c, /* $..;..el */
  3426. 0x00, 0x76, 0x50, 0xef, 0x15, 0x08, 0xd7, 0xb4, /* .vP..... */
  3427. 0x73, 0x68, 0x26, 0x14, 0x87, 0x95, 0xc3, 0x5f, /* sh&...._ */
  3428. 0x6e, 0x61, 0xb8, 0x87, 0x84, 0xfa, 0x80, 0x1a, /* na...... */
  3429. 0x0a, 0x8b, 0x98, 0xf3, 0xe3, 0xff, 0x4e, 0x44, /* ......ND */
  3430. 0x1c, 0x65, 0x74, 0x7c, 0x71, 0x54, 0x65, 0xe5, /* .et|qTe. */
  3431. 0x39, 0x02, 0x03, 0x01, 0x00, 0x01, 0xa3, 0x82, /* 9....... */
  3432. 0x01, 0x0a, 0x30, 0x82, 0x01, 0x06, 0x30, 0x09, /* ..0...0. */
  3433. 0x06, 0x03, 0x55, 0x1d, 0x13, 0x04, 0x02, 0x30, /* ..U....0 */
  3434. 0x00, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e, /* .0...U.. */
  3435. 0x04, 0x16, 0x04, 0x14, 0x32, 0x67, 0xe1, 0xb1, /* ....2g.. */
  3436. 0x79, 0xd2, 0x81, 0xfc, 0x9f, 0x23, 0x0c, 0x70, /* y....#.p */
  3437. 0x40, 0x50, 0xb5, 0x46, 0x56, 0xb8, 0x30, 0x36, /* @P.FV.06 */
  3438. 0x30, 0x81, 0xc4, 0x06, 0x03, 0x55, 0x1d, 0x23, /* 0....U.# */
  3439. 0x04, 0x81, 0xbc, 0x30, 0x81, 0xb9, 0x80, 0x14, /* ...0.... */
  3440. 0x73, 0xb0, 0x1c, 0xa4, 0x2f, 0x82, 0xcb, 0xcf, /* s.../... */
  3441. 0x47, 0xa5, 0x38, 0xd7, 0xb0, 0x04, 0x82, 0x3a, /* G.8....: */
  3442. 0x7e, 0x72, 0x15, 0x21, 0xa1, 0x81, 0x9d, 0xa4, /* ~r.!.... */
  3443. 0x81, 0x9a, 0x30, 0x81, 0x97, 0x31, 0x0b, 0x30, /* ..0..1.0 */
  3444. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, /* ...U.... */
  3445. 0x55, 0x53, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, /* US1.0... */
  3446. 0x55, 0x04, 0x08, 0x0c, 0x0a, 0x57, 0x61, 0x73, /* U....Was */
  3447. 0x68, 0x69, 0x6e, 0x67, 0x74, 0x6f, 0x6e, 0x31, /* hington1 */
  3448. 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, 0x04, 0x07, /* .0...U.. */
  3449. 0x0c, 0x07, 0x53, 0x65, 0x61, 0x74, 0x74, 0x6c, /* ..Seattl */
  3450. 0x65, 0x31, 0x10, 0x30, 0x0e, 0x06, 0x03, 0x55, /* e1.0...U */
  3451. 0x04, 0x0a, 0x0c, 0x07, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  3452. 0x53, 0x53, 0x4c, 0x31, 0x14, 0x30, 0x12, 0x06, /* SSL1.0.. */
  3453. 0x03, 0x55, 0x04, 0x0b, 0x0c, 0x0b, 0x45, 0x6e, /* .U....En */
  3454. 0x67, 0x69, 0x6e, 0x65, 0x65, 0x72, 0x69, 0x6e, /* gineerin */
  3455. 0x67, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, /* g1.0...U */
  3456. 0x04, 0x03, 0x0c, 0x0f, 0x77, 0x6f, 0x6c, 0x66, /* ....wolf */
  3457. 0x53, 0x53, 0x4c, 0x20, 0x72, 0x6f, 0x6f, 0x74, /* SSL root */
  3458. 0x20, 0x43, 0x41, 0x31, 0x1f, 0x30, 0x1d, 0x06, /* CA1.0.. */
  3459. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, /* .*.H.... */
  3460. 0x09, 0x01, 0x16, 0x10, 0x69, 0x6e, 0x66, 0x6f, /* ....info */
  3461. 0x40, 0x77, 0x6f, 0x6c, 0x66, 0x73, 0x73, 0x6c, /* @wolfssl */
  3462. 0x2e, 0x63, 0x6f, 0x6d, 0x82, 0x01, 0x63, 0x30, /* .com..c0 */
  3463. 0x13, 0x06, 0x03, 0x55, 0x1d, 0x25, 0x04, 0x0c, /* ...U.%.. */
  3464. 0x30, 0x0a, 0x06, 0x08, 0x2b, 0x06, 0x01, 0x05, /* 0...+... */
  3465. 0x05, 0x07, 0x03, 0x09, 0x30, 0x0d, 0x06, 0x09, /* ....0... */
  3466. 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, /* *.H..... */
  3467. 0x0b, 0x05, 0x00, 0x03, 0x82, 0x01, 0x01, 0x00, /* ........ */
  3468. 0x2f, 0xb7, 0x6b, 0xec, 0xb7, 0x12, 0x63, 0xb9, /* /.k...c. */
  3469. 0x57, 0xdc, 0x04, 0x4d, 0x9c, 0x67, 0x74, 0x98, /* W..M.gt. */
  3470. 0x06, 0x28, 0x68, 0x37, 0x34, 0xc2, 0x50, 0xe9, /* .(h74.P. */
  3471. 0x2a, 0xd4, 0x1a, 0xb2, 0x32, 0x1a, 0x9d, 0x2b, /* *...2..+ */
  3472. 0x4f, 0x23, 0x50, 0xea, 0xb4, 0x95, 0x86, 0xc3, /* O#P..... */
  3473. 0xb9, 0x5f, 0x34, 0x3e, 0x99, 0x91, 0xa7, 0x80, /* ._4>.... */
  3474. 0x5f, 0x6e, 0x1b, 0x6e, 0xdb, 0xe9, 0x02, 0x38, /* _n.n...8 */
  3475. 0x6f, 0xdf, 0xc5, 0x9b, 0x0d, 0xa3, 0x1c, 0xa9, /* o....... */
  3476. 0x15, 0x76, 0x16, 0x66, 0xa8, 0x4e, 0xfb, 0xd3, /* .v.f.N.. */
  3477. 0x43, 0x76, 0xf1, 0x72, 0xb7, 0xd1, 0xfa, 0xee, /* Cv.r.... */
  3478. 0x39, 0xa6, 0x96, 0xc1, 0xa2, 0x93, 0xa4, 0x9b, /* 9....... */
  3479. 0x1e, 0x9f, 0xba, 0x71, 0x8f, 0xba, 0xbd, 0x67, /* ...q...g */
  3480. 0x6a, 0xf2, 0x15, 0x5f, 0xf1, 0x64, 0xe7, 0xcf, /* j.._.d.. */
  3481. 0x26, 0xb8, 0x4c, 0xc0, 0xeb, 0x85, 0x04, 0x58, /* &.L....X */
  3482. 0xd9, 0x4a, 0x6b, 0xd9, 0x86, 0xf5, 0x80, 0x21, /* .Jk....! */
  3483. 0xbf, 0x91, 0xc8, 0x4b, 0x9f, 0x04, 0xed, 0x57, /* ...K...W */
  3484. 0x7a, 0xd2, 0x58, 0xac, 0x5b, 0x47, 0xaf, 0x4d, /* z.X.[G.M */
  3485. 0x7f, 0x5b, 0x1d, 0x6d, 0x68, 0x9b, 0x84, 0x98, /* .[.mh... */
  3486. 0x2a, 0x31, 0x02, 0x2c, 0xe9, 0x1b, 0xaf, 0x11, /* *1.,.... */
  3487. 0x0b, 0x78, 0x49, 0xbe, 0x68, 0x68, 0xcb, 0x9c, /* .xI.hh.. */
  3488. 0x41, 0x56, 0xe8, 0xb5, 0x59, 0xda, 0xff, 0xca, /* AV..Y... */
  3489. 0x59, 0x99, 0x17, 0x3e, 0x11, 0x0a, 0x8f, 0x49, /* Y..>...I */
  3490. 0x24, 0x0b, 0x81, 0x42, 0x63, 0xcd, 0x4f, 0xf6, /* $..Bc.O. */
  3491. 0x2b, 0x9d, 0xd1, 0x79, 0x75, 0xd7, 0x4a, 0xcc, /* +..yu.J. */
  3492. 0x4c, 0xb7, 0x2b, 0xd7, 0xe8, 0xe7, 0xd4, 0x48, /* L.+....H */
  3493. 0x3c, 0x14, 0x3b, 0x1c, 0x28, 0xe8, 0x46, 0x7a, /* <.;.(.Fz */
  3494. 0xdc, 0x11, 0x9d, 0x7f, 0x1c, 0xab, 0x10, 0x95, /* ........ */
  3495. 0x17, 0xb2, 0xc7, 0x7a, 0xbb, 0x17, 0x44, 0x59, /* ...z..DY */
  3496. 0x69, 0x8e, 0x16, 0x05, 0x94, 0x8c, 0x88, 0xd9, /* i....... */
  3497. 0xdc, 0x9a, 0xfd, 0xf2, 0x93, 0xbe, 0x68, 0xba, /* ......h. */
  3498. 0x3c, 0xd6, 0x2b, 0x61, 0x3a, 0x8b, 0xf7, 0x66, /* <.+a:..f */
  3499. 0xcb, 0x54, 0xe8, 0xe4, 0xdb, 0x9f, 0xcc, 0x9e /* .T...... */
  3500. };
  3501. OcspEntry entry[1];
  3502. CertStatus status[1];
  3503. OcspRequest* request = NULL;
  3504. #ifndef NO_FILESYSTEM
  3505. const char* ca_cert = "./certs/ca-cert.pem";
  3506. #endif
  3507. byte serial[] = {0x05};
  3508. byte issuerHash[] = {0x71, 0x4d, 0x82, 0x23, 0x40, 0x59, 0xc0, 0x96, 0xa1, 0x37, 0x43, 0xfa, 0x31, 0xdb, 0xba, 0xb1, 0x43, 0x18, 0xda, 0x04};
  3509. byte issuerKeyHash[] = {0x83, 0xc6, 0x3a, 0x89, 0x2c, 0x81, 0xf4, 0x02, 0xd7, 0x9d, 0x4c, 0xe2, 0x2a, 0xc0, 0x71, 0x82, 0x64, 0x44, 0xda, 0x0e};
  3510. XMEMSET(entry, 0, sizeof(OcspEntry));
  3511. XMEMSET(status, 0, sizeof(CertStatus));
  3512. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3513. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3514. DYNAMIC_TYPE_OCSP_REQUEST));
  3515. if ((request != NULL) && (request->serial != NULL)) {
  3516. request->serialSz = sizeof(serial);
  3517. XMEMCPY(request->serial, serial, sizeof(serial));
  3518. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3519. XMEMCPY(request->issuerKeyHash, issuerKeyHash, sizeof(issuerKeyHash));
  3520. }
  3521. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3522. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3523. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm,
  3524. "./certs/ocsp/intermediate1-ca-cert.pem", NULL), WOLFSSL_SUCCESS);
  3525. /* Response should be valid. */
  3526. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3527. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3528. /* Flip a byte in the request serial number, response should be invalid
  3529. * now. */
  3530. if ((request != NULL) && (request->serial != NULL))
  3531. request->serial[0] ^= request->serial[0];
  3532. ExpectIntNE(wolfSSL_CertManagerCheckOCSPResponse(cm, (byte *)response,
  3533. sizeof(response), NULL, status, entry, request), WOLFSSL_SUCCESS);
  3534. #ifndef NO_FILESYSTEM
  3535. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3536. sizeof(server_cert_der_2048)), ASN_NO_SIGNER_E);
  3537. ExpectIntEQ(WOLFSSL_SUCCESS,
  3538. wolfSSL_CertManagerLoadCA(cm, ca_cert, NULL));
  3539. ExpectIntEQ(wolfSSL_CertManagerCheckOCSP(cm, server_cert_der_2048,
  3540. sizeof(server_cert_der_2048)), 1);
  3541. #endif
  3542. wolfSSL_OCSP_REQUEST_free(request);
  3543. wolfSSL_CertManagerFree(cm);
  3544. #endif /* OPENSSL_ALL || WOLFSSL_NGINX || WOLFSSL_HAPROXY ||
  3545. * WOLFSSL_APACHE_HTTPD || HAVE_LIGHTY */
  3546. #endif /* HAVE_OCSP */
  3547. return EXPECT_RESULT();
  3548. }
  3549. static int test_wolfSSL_CheckOCSPResponse(void)
  3550. {
  3551. EXPECT_DECLS;
  3552. #if defined(HAVE_OCSP) && !defined(NO_RSA) && !defined(NO_SHA) && \
  3553. defined(OPENSSL_ALL)
  3554. const char* responseFile = "./certs/ocsp/test-response.der";
  3555. const char* responseMultiFile = "./certs/ocsp/test-multi-response.der";
  3556. const char* responseNoInternFile =
  3557. "./certs/ocsp/test-response-nointern.der";
  3558. const char* caFile = "./certs/ocsp/root-ca-cert.pem";
  3559. OcspResponse* res = NULL;
  3560. byte data[4096];
  3561. const unsigned char* pt;
  3562. int dataSz = 0; /* initialize to mitigate spurious maybe-uninitialized from
  3563. * gcc sanitizer with --enable-heapmath.
  3564. */
  3565. XFILE f = XBADFILE;
  3566. WOLFSSL_OCSP_BASICRESP* bs = NULL;
  3567. WOLFSSL_X509_STORE* st = NULL;
  3568. WOLFSSL_X509* issuer = NULL;
  3569. ExpectTrue((f = XFOPEN(responseFile, "rb")) != XBADFILE);
  3570. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3571. if (f != XBADFILE) {
  3572. XFCLOSE(f);
  3573. f = XBADFILE;
  3574. }
  3575. pt = data;
  3576. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3577. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3578. SSL_FILETYPE_PEM));
  3579. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3580. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3581. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3582. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0), WOLFSSL_SUCCESS);
  3583. wolfSSL_OCSP_BASICRESP_free(bs);
  3584. bs = NULL;
  3585. wolfSSL_OCSP_RESPONSE_free(res);
  3586. res = NULL;
  3587. wolfSSL_X509_STORE_free(st);
  3588. st = NULL;
  3589. wolfSSL_X509_free(issuer);
  3590. issuer = NULL;
  3591. /* check loading a response with optional certs */
  3592. ExpectTrue((f = XFOPEN(responseNoInternFile, "rb")) != XBADFILE);
  3593. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3594. if (f != XBADFILE)
  3595. XFCLOSE(f);
  3596. f = XBADFILE;
  3597. pt = data;
  3598. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3599. wolfSSL_OCSP_RESPONSE_free(res);
  3600. res = NULL;
  3601. /* check loading a response with multiple certs */
  3602. {
  3603. WOLFSSL_CERT_MANAGER* cm = NULL;
  3604. OcspEntry *entry = NULL;
  3605. CertStatus* status = NULL;
  3606. OcspRequest* request = NULL;
  3607. byte serial1[] = {0x01};
  3608. byte serial[] = {0x02};
  3609. byte issuerHash[] = {
  3610. 0x44, 0xA8, 0xDB, 0xD1, 0xBC, 0x97, 0x0A, 0x83,
  3611. 0x3B, 0x5B, 0x31, 0x9A, 0x4C, 0xB8, 0xD2, 0x52,
  3612. 0x37, 0x15, 0x8A, 0x88
  3613. };
  3614. byte issuerKeyHash[] = {
  3615. 0x73, 0xB0, 0x1C, 0xA4, 0x2F, 0x82, 0xCB, 0xCF,
  3616. 0x47, 0xA5, 0x38, 0xD7, 0xB0, 0x04, 0x82, 0x3A,
  3617. 0x7E, 0x72, 0x15, 0x21
  3618. };
  3619. ExpectNotNull(entry = (OcspEntry*)XMALLOC(sizeof(OcspEntry), NULL,
  3620. DYNAMIC_TYPE_OPENSSL));
  3621. ExpectNotNull(status = (CertStatus*)XMALLOC(sizeof(CertStatus), NULL,
  3622. DYNAMIC_TYPE_OPENSSL));
  3623. if (entry != NULL)
  3624. XMEMSET(entry, 0, sizeof(OcspEntry));
  3625. if (status != NULL)
  3626. XMEMSET(status, 0, sizeof(CertStatus));
  3627. ExpectNotNull(request = wolfSSL_OCSP_REQUEST_new());
  3628. ExpectNotNull(request->serial = (byte*)XMALLOC(sizeof(serial), NULL,
  3629. DYNAMIC_TYPE_OCSP_REQUEST));
  3630. if (request != NULL && request->serial != NULL) {
  3631. request->serialSz = sizeof(serial);
  3632. XMEMCPY(request->serial, serial, sizeof(serial));
  3633. XMEMCPY(request->issuerHash, issuerHash, sizeof(issuerHash));
  3634. XMEMCPY(request->issuerKeyHash, issuerKeyHash,
  3635. sizeof(issuerKeyHash));
  3636. }
  3637. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  3638. ExpectIntEQ(wolfSSL_CertManagerEnableOCSP(cm, 0), WOLFSSL_SUCCESS);
  3639. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, caFile, NULL),
  3640. WOLFSSL_SUCCESS);
  3641. ExpectTrue((f = XFOPEN(responseMultiFile, "rb")) != XBADFILE);
  3642. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3643. if (f != XBADFILE)
  3644. XFCLOSE(f);
  3645. f = XBADFILE;
  3646. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3647. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3648. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3649. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3650. ExpectNotNull(entry->status);
  3651. if (request != NULL && request->serial != NULL)
  3652. XMEMCPY(request->serial, serial1, sizeof(serial1));
  3653. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3654. dataSz, NULL, status, entry, request), WOLFSSL_SUCCESS);
  3655. /* store both status's in the entry to check that "next" is not
  3656. * overwritten */
  3657. if (EXPECT_SUCCESS() && status != NULL && entry != NULL) {
  3658. status->next = entry->status;
  3659. entry->status = status;
  3660. }
  3661. if (request != NULL && request->serial != NULL)
  3662. XMEMCPY(request->serial, serial, sizeof(serial));
  3663. ExpectIntEQ(wolfSSL_CertManagerCheckOCSPResponse(cm, data,
  3664. dataSz, NULL, entry->status, entry, request), WOLFSSL_SUCCESS);
  3665. ExpectNotNull(entry->status->next);
  3666. /* compare the status found */
  3667. ExpectIntEQ(status->serialSz, entry->status->serialSz);
  3668. ExpectIntEQ(XMEMCMP(status->serial, entry->status->serial,
  3669. status->serialSz), 0);
  3670. if (status != NULL && entry != NULL && entry->status != status) {
  3671. XFREE(status, NULL, DYNAMIC_TYPE_OPENSSL);
  3672. }
  3673. wolfSSL_OCSP_CERTID_free(entry);
  3674. wolfSSL_OCSP_REQUEST_free(request);
  3675. wolfSSL_CertManagerFree(cm);
  3676. }
  3677. #if defined(WC_RSA_PSS)
  3678. {
  3679. const char* responsePssFile = "./certs/ocsp/test-response-rsapss.der";
  3680. /* check loading a response with RSA-PSS signature */
  3681. ExpectTrue((f = XFOPEN(responsePssFile, "rb")) != XBADFILE);
  3682. ExpectIntGT(dataSz = (word32)XFREAD(data, 1, sizeof(data), f), 0);
  3683. if (f != XBADFILE)
  3684. XFCLOSE(f);
  3685. pt = data;
  3686. ExpectNotNull(res = wolfSSL_d2i_OCSP_RESPONSE(NULL, &pt, dataSz));
  3687. /* try to verify the response */
  3688. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caFile,
  3689. SSL_FILETYPE_PEM));
  3690. ExpectNotNull(st = wolfSSL_X509_STORE_new());
  3691. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(st, issuer), WOLFSSL_SUCCESS);
  3692. ExpectNotNull(bs = wolfSSL_OCSP_response_get1_basic(res));
  3693. ExpectIntEQ(wolfSSL_OCSP_basic_verify(bs, NULL, st, 0),
  3694. WOLFSSL_SUCCESS);
  3695. wolfSSL_OCSP_BASICRESP_free(bs);
  3696. wolfSSL_OCSP_RESPONSE_free(res);
  3697. wolfSSL_X509_STORE_free(st);
  3698. wolfSSL_X509_free(issuer);
  3699. }
  3700. #endif
  3701. #endif /* HAVE_OCSP */
  3702. return EXPECT_RESULT();
  3703. }
  3704. static int test_wolfSSL_FPKI(void)
  3705. {
  3706. EXPECT_DECLS;
  3707. #if defined(WOLFSSL_FPKI) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3708. XFILE f = XBADFILE;
  3709. const char* fpkiCert = "./certs/fpki-cert.der";
  3710. DecodedCert cert;
  3711. byte buf[4096];
  3712. byte* uuid = NULL;
  3713. byte* fascn = NULL;
  3714. word32 fascnSz;
  3715. word32 uuidSz;
  3716. int bytes = 0;
  3717. ExpectTrue((f = XFOPEN(fpkiCert, "rb")) != XBADFILE);
  3718. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3719. if (f != XBADFILE)
  3720. XFCLOSE(f);
  3721. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3722. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3723. ExpectIntEQ(wc_GetFASCNFromCert(&cert, NULL, &fascnSz), LENGTH_ONLY_E) ;
  3724. ExpectNotNull(fascn = (byte*)XMALLOC(fascnSz, NULL,
  3725. DYNAMIC_TYPE_TMP_BUFFER));
  3726. ExpectIntEQ(wc_GetFASCNFromCert(&cert, fascn, &fascnSz), 0);
  3727. XFREE(fascn, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3728. ExpectIntEQ(wc_GetUUIDFromCert(&cert, NULL, &uuidSz), LENGTH_ONLY_E);
  3729. ExpectNotNull(uuid = (byte*)XMALLOC(uuidSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  3730. ExpectIntEQ(wc_GetUUIDFromCert(&cert, uuid, &uuidSz), 0);
  3731. XFREE(uuid, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  3732. wc_FreeDecodedCert(&cert);
  3733. #endif
  3734. return EXPECT_RESULT();
  3735. }
  3736. /* use RID in confuncture with other names to test parsing of unknown other
  3737. * names */
  3738. static int test_wolfSSL_OtherName(void)
  3739. {
  3740. EXPECT_DECLS;
  3741. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  3742. XFILE f = XBADFILE;
  3743. const char* ridCert = "./certs/rid-cert.der";
  3744. DecodedCert cert;
  3745. byte buf[4096];
  3746. int bytes = 0;
  3747. ExpectTrue((f = XFOPEN(ridCert, "rb")) != XBADFILE);
  3748. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3749. if (f != XBADFILE)
  3750. XFCLOSE(f);
  3751. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3752. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, 0, NULL), 0);
  3753. wc_FreeDecodedCert(&cert);
  3754. #endif
  3755. return EXPECT_RESULT();
  3756. }
  3757. static int test_wolfSSL_CertRsaPss(void)
  3758. {
  3759. EXPECT_DECLS;
  3760. /* FIPS v2 and below don't support long salts. */
  3761. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM) && \
  3762. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  3763. (HAVE_FIPS_VERSION > 2))) && (!defined(HAVE_SELFTEST) || \
  3764. (defined(HAVE_SELFTEST_VERSION) && (HAVE_SELFTEST_VERSION > 2)))
  3765. XFILE f = XBADFILE;
  3766. const char* rsaPssSha256Cert = "./certs/rsapss/ca-rsapss.der";
  3767. const char* rsaPssRootSha256Cert = "./certs/rsapss/root-rsapss.pem";
  3768. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3769. RSA_MAX_SIZE >= 3072
  3770. const char* rsaPssSha384Cert = "./certs/rsapss/ca-3072-rsapss.der";
  3771. #endif
  3772. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3773. const char* rsaPssRootSha384Cert = "./certs/rsapss/root-3072-rsapss.pem";
  3774. #endif
  3775. DecodedCert cert;
  3776. byte buf[4096];
  3777. int bytes = 0;
  3778. WOLFSSL_CERT_MANAGER* cm = NULL;
  3779. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  3780. ExpectIntEQ(WOLFSSL_SUCCESS,
  3781. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha256Cert, NULL));
  3782. #if defined(WOLFSSL_SHA384) && RSA_MAX_SIZE >= 3072
  3783. ExpectIntEQ(WOLFSSL_SUCCESS,
  3784. wolfSSL_CertManagerLoadCA(cm, rsaPssRootSha384Cert, NULL));
  3785. #endif
  3786. ExpectTrue((f = XFOPEN(rsaPssSha256Cert, "rb")) != XBADFILE);
  3787. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3788. if (f != XBADFILE) {
  3789. XFCLOSE(f);
  3790. f = XBADFILE;
  3791. }
  3792. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3793. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3794. wc_FreeDecodedCert(&cert);
  3795. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_PSS_LONG_SALT) && \
  3796. RSA_MAX_SIZE >= 3072
  3797. ExpectTrue((f = XFOPEN(rsaPssSha384Cert, "rb")) != XBADFILE);
  3798. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  3799. if (f != XBADFILE)
  3800. XFCLOSE(f);
  3801. wc_InitDecodedCert(&cert, buf, bytes, NULL);
  3802. ExpectIntEQ(wc_ParseCert(&cert, CERT_TYPE, VERIFY, cm), 0);
  3803. wc_FreeDecodedCert(&cert);
  3804. #endif
  3805. wolfSSL_CertManagerFree(cm);
  3806. #endif
  3807. return EXPECT_RESULT();
  3808. }
  3809. static int test_wolfSSL_CTX_load_verify_locations_ex(void)
  3810. {
  3811. EXPECT_DECLS;
  3812. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3813. !defined(NO_WOLFSSL_CLIENT)
  3814. WOLFSSL_CTX* ctx = NULL;
  3815. const char* ca_cert = "./certs/ca-cert.pem";
  3816. const char* ca_expired_cert = "./certs/test/expired/expired-ca.pem";
  3817. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3818. /* test good CA */
  3819. ExpectTrue(WOLFSSL_SUCCESS ==
  3820. wolfSSL_CTX_load_verify_locations_ex(ctx, ca_cert, NULL,
  3821. WOLFSSL_LOAD_FLAG_NONE));
  3822. /* test expired CA */
  3823. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3824. ExpectIntNE(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3825. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3826. #else
  3827. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3828. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3829. #endif
  3830. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx, ca_expired_cert, NULL,
  3831. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3832. wolfSSL_CTX_free(ctx);
  3833. #endif
  3834. return EXPECT_RESULT();
  3835. }
  3836. static int test_wolfSSL_CTX_load_verify_buffer_ex(void)
  3837. {
  3838. EXPECT_DECLS;
  3839. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3840. defined(USE_CERT_BUFFERS_2048)
  3841. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  3842. WOLFSSL_CTX* ctx;
  3843. const char* ca_expired_cert_file = "./certs/test/expired/expired-ca.der";
  3844. byte ca_expired_cert[TWOK_BUF];
  3845. word32 sizeof_ca_expired_cert = 0;
  3846. XFILE fp = XBADFILE;
  3847. #ifndef NO_WOLFSSL_CLIENT
  3848. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3849. #else
  3850. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3851. #endif
  3852. ExpectNotNull(ctx);
  3853. /* test good CA */
  3854. ExpectTrue(WOLFSSL_SUCCESS ==
  3855. wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_cert_der_2048,
  3856. sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1, 0,
  3857. WOLFSSL_LOAD_FLAG_NONE));
  3858. /* load expired CA */
  3859. XMEMSET(ca_expired_cert, 0, sizeof(ca_expired_cert));
  3860. ExpectTrue((fp = XFOPEN(ca_expired_cert_file, "rb")) != XBADFILE);
  3861. ExpectIntGT(sizeof_ca_expired_cert = (word32)XFREAD(ca_expired_cert, 1,
  3862. sizeof(ca_expired_cert), fp), 0);
  3863. if (fp != XBADFILE)
  3864. XFCLOSE(fp);
  3865. /* test expired CA failure */
  3866. #if !defined(OPENSSL_COMPATIBLE_DEFAULTS) && !defined(NO_ASN_TIME)
  3867. ExpectIntNE(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3868. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3869. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3870. #else
  3871. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3872. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3873. WOLFSSL_LOAD_FLAG_NONE), WOLFSSL_SUCCESS);
  3874. #endif
  3875. /* test expired CA success */
  3876. ExpectIntEQ(wolfSSL_CTX_load_verify_buffer_ex(ctx, ca_expired_cert,
  3877. sizeof_ca_expired_cert, WOLFSSL_FILETYPE_ASN1, 0,
  3878. WOLFSSL_LOAD_FLAG_DATE_ERR_OKAY), WOLFSSL_SUCCESS);
  3879. wolfSSL_CTX_free(ctx);
  3880. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  3881. #endif
  3882. return EXPECT_RESULT();
  3883. }
  3884. static int test_wolfSSL_CTX_load_verify_chain_buffer_format(void)
  3885. {
  3886. EXPECT_DECLS;
  3887. #if !defined(NO_CERTS) && !defined(NO_RSA) && defined(OPENSSL_EXTRA) && \
  3888. defined(WOLFSSL_CERT_GEN) && defined(USE_CERT_BUFFERS_2048) && \
  3889. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3890. WOLFSSL_CTX* ctx = NULL;
  3891. #ifndef NO_WOLFSSL_CLIENT
  3892. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3893. #else
  3894. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3895. #endif
  3896. ExpectTrue(WOLFSSL_SUCCESS == wolfSSL_CTX_load_verify_chain_buffer_format(
  3897. ctx, ca_cert_chain_der, sizeof_ca_cert_chain_der,
  3898. WOLFSSL_FILETYPE_ASN1));
  3899. wolfSSL_CTX_free(ctx);
  3900. #endif
  3901. return EXPECT_RESULT();
  3902. }
  3903. static int test_wolfSSL_CTX_add1_chain_cert(void)
  3904. {
  3905. EXPECT_DECLS;
  3906. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && defined(OPENSSL_EXTRA) && \
  3907. defined(KEEP_OUR_CERT) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  3908. WOLFSSL_CTX* ctx;
  3909. WOLFSSL* ssl = NULL;
  3910. const char *certChain[] = {
  3911. "./certs/intermediate/client-int-cert.pem",
  3912. "./certs/intermediate/ca-int2-cert.pem",
  3913. "./certs/intermediate/ca-int-cert.pem",
  3914. "./certs/ca-cert.pem",
  3915. NULL
  3916. };
  3917. const char** cert;
  3918. WOLFSSL_X509* x509 = NULL;
  3919. WOLF_STACK_OF(X509)* chain = NULL;
  3920. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3921. ExpectNotNull(ssl = wolfSSL_new(ctx));
  3922. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3923. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3924. WOLFSSL_FILETYPE_PEM));
  3925. ExpectIntEQ(SSL_CTX_add1_chain_cert(ctx, x509), 1);
  3926. X509_free(x509);
  3927. x509 = NULL;
  3928. }
  3929. for (cert = certChain; EXPECT_SUCCESS() && *cert != NULL; cert++) {
  3930. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(*cert,
  3931. WOLFSSL_FILETYPE_PEM));
  3932. ExpectIntEQ(SSL_add1_chain_cert(ssl, x509), 1);
  3933. X509_free(x509);
  3934. x509 = NULL;
  3935. }
  3936. ExpectIntEQ(SSL_CTX_get0_chain_certs(ctx, &chain), 1);
  3937. ExpectIntEQ(sk_X509_num(chain), 3);
  3938. ExpectIntEQ(SSL_get0_chain_certs(ssl, &chain), 1);
  3939. ExpectIntEQ(sk_X509_num(chain), 3);
  3940. SSL_free(ssl);
  3941. SSL_CTX_free(ctx);
  3942. #endif
  3943. return EXPECT_RESULT();
  3944. }
  3945. static int test_wolfSSL_CTX_use_certificate_chain_file_format(void)
  3946. {
  3947. EXPECT_DECLS;
  3948. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  3949. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3950. const char* server_chain_der = "./certs/server-cert-chain.der";
  3951. const char* client_single_pem = "./certs/client-cert.pem";
  3952. WOLFSSL_CTX* ctx;
  3953. (void)server_chain_der;
  3954. (void)client_single_pem;
  3955. (void)ctx;
  3956. #ifndef NO_WOLFSSL_CLIENT
  3957. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  3958. #else
  3959. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  3960. #endif
  3961. ExpectNotNull(ctx);
  3962. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3963. server_chain_der, WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  3964. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file_format(ctx,
  3965. client_single_pem, WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  3966. wolfSSL_CTX_free(ctx);
  3967. #endif
  3968. return EXPECT_RESULT();
  3969. }
  3970. static int test_wolfSSL_CTX_SetTmpDH_file(void)
  3971. {
  3972. EXPECT_DECLS;
  3973. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  3974. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  3975. WOLFSSL_CTX *ctx = NULL;
  3976. (void)ctx;
  3977. #ifndef NO_WOLFSSL_CLIENT
  3978. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  3979. #else
  3980. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  3981. #endif
  3982. /* invalid context */
  3983. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(NULL,
  3984. dhParamFile, WOLFSSL_FILETYPE_PEM));
  3985. /* invalid dhParamFile file */
  3986. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3987. NULL, WOLFSSL_FILETYPE_PEM));
  3988. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx,
  3989. bogusFile, WOLFSSL_FILETYPE_PEM));
  3990. /* success */
  3991. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  3992. WOLFSSL_FILETYPE_PEM));
  3993. wolfSSL_CTX_free(ctx);
  3994. #endif
  3995. return EXPECT_RESULT();
  3996. }
  3997. static int test_wolfSSL_CTX_SetTmpDH_buffer(void)
  3998. {
  3999. EXPECT_DECLS;
  4000. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  4001. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  4002. WOLFSSL_CTX *ctx = NULL;
  4003. #ifndef NO_WOLFSSL_CLIENT
  4004. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4005. #else
  4006. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4007. #endif
  4008. /* invalid context */
  4009. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL,
  4010. dh_key_der_2048, sizeof_dh_key_der_2048,
  4011. WOLFSSL_FILETYPE_ASN1));
  4012. /* invalid dhParamFile file */
  4013. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(NULL, NULL,
  4014. 0, WOLFSSL_FILETYPE_ASN1));
  4015. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4016. dsa_key_der_2048, sizeof_dsa_key_der_2048,
  4017. WOLFSSL_FILETYPE_ASN1));
  4018. /* success */
  4019. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4020. dh_key_der_2048, sizeof_dh_key_der_2048,
  4021. WOLFSSL_FILETYPE_ASN1));
  4022. wolfSSL_CTX_free(ctx);
  4023. #endif
  4024. return EXPECT_RESULT();
  4025. }
  4026. static int test_wolfSSL_CTX_SetMinMaxDhKey_Sz(void)
  4027. {
  4028. EXPECT_DECLS;
  4029. #if !defined(NO_CERTS) && !defined(NO_DH) && \
  4030. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  4031. WOLFSSL_CTX *ctx;
  4032. (void)ctx;
  4033. #ifndef NO_WOLFSSL_CLIENT
  4034. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  4035. #else
  4036. ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  4037. #endif
  4038. ExpectNotNull(ctx);
  4039. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  4040. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx, dh_key_der_2048,
  4041. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4042. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 2048));
  4043. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4044. dh_key_der_2048, sizeof_dh_key_der_2048,
  4045. WOLFSSL_FILETYPE_ASN1));
  4046. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  4047. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4048. dh_key_der_2048, sizeof_dh_key_der_2048,
  4049. WOLFSSL_FILETYPE_ASN1));
  4050. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 2048));
  4051. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpDH_buffer(ctx,
  4052. dh_key_der_2048, sizeof_dh_key_der_2048,
  4053. WOLFSSL_FILETYPE_ASN1));
  4054. wolfSSL_CTX_free(ctx);
  4055. #endif
  4056. return EXPECT_RESULT();
  4057. }
  4058. static int test_wolfSSL_CTX_der_load_verify_locations(void)
  4059. {
  4060. EXPECT_DECLS;
  4061. #if !defined(NO_FILESYSTEM) && defined(WOLFSSL_DER_LOAD) && \
  4062. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  4063. WOLFSSL_CTX* ctx = NULL;
  4064. const char* derCert = "./certs/server-cert.der";
  4065. const char* nullPath = NULL;
  4066. const char* invalidPath = "./certs/this-cert-does-not-exist.der";
  4067. const char* emptyPath = "";
  4068. /* der load Case 1 ctx NULL */
  4069. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  4070. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4071. #ifndef NO_WOLFSSL_CLIENT
  4072. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4073. #else
  4074. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4075. #endif
  4076. /* Case 2 filePath NULL */
  4077. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, nullPath,
  4078. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4079. /* Case 3 invalid format */
  4080. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  4081. WOLFSSL_FILETYPE_PEM), WOLFSSL_FAILURE);
  4082. /* Case 4 filePath not valid */
  4083. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, invalidPath,
  4084. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4085. /* Case 5 filePath empty */
  4086. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, emptyPath,
  4087. WOLFSSL_FILETYPE_ASN1), WOLFSSL_FAILURE);
  4088. #ifndef NO_RSA
  4089. /* Case 6 success case */
  4090. ExpectIntEQ(wolfSSL_CTX_der_load_verify_locations(ctx, derCert,
  4091. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  4092. #endif
  4093. wolfSSL_CTX_free(ctx);
  4094. #endif
  4095. return EXPECT_RESULT();
  4096. }
  4097. static int test_wolfSSL_CTX_enable_disable(void)
  4098. {
  4099. EXPECT_DECLS;
  4100. #ifndef NO_CERTS
  4101. WOLFSSL_CTX* ctx = NULL;
  4102. #ifdef HAVE_CRL
  4103. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), BAD_FUNC_ARG);
  4104. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), BAD_FUNC_ARG);
  4105. #endif
  4106. #ifdef HAVE_OCSP
  4107. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), BAD_FUNC_ARG);
  4108. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, 0), BAD_FUNC_ARG);
  4109. #endif
  4110. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  4111. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  4112. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), BAD_FUNC_ARG);
  4113. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), BAD_FUNC_ARG);
  4114. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  4115. ExpectIntEQ(wolfSSL_CTX_EnableOCSPMustStaple(ctx), BAD_FUNC_ARG);
  4116. #endif
  4117. #ifndef NO_WOLFSSL_CLIENT
  4118. #ifdef HAVE_EXTENDED_MASTER
  4119. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), BAD_FUNC_ARG);
  4120. #endif
  4121. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4122. #ifdef HAVE_EXTENDED_MASTER
  4123. ExpectIntEQ(wolfSSL_CTX_DisableExtendedMasterSecret(ctx), WOLFSSL_SUCCESS);
  4124. #endif
  4125. #elif !defined(NO_WOLFSSL_SERVER)
  4126. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4127. #endif
  4128. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  4129. #ifdef HAVE_CRL
  4130. ExpectIntEQ(wolfSSL_CTX_DisableCRL(ctx), WOLFSSL_SUCCESS);
  4131. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, 0), WOLFSSL_SUCCESS);
  4132. #endif
  4133. #ifdef HAVE_OCSP
  4134. ExpectIntEQ(wolfSSL_CTX_DisableOCSP(ctx), WOLFSSL_SUCCESS);
  4135. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_URL_OVERRIDE),
  4136. WOLFSSL_SUCCESS);
  4137. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_NO_NONCE),
  4138. WOLFSSL_SUCCESS);
  4139. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL),
  4140. WOLFSSL_SUCCESS);
  4141. #endif
  4142. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) || \
  4143. defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  4144. ExpectIntEQ(wolfSSL_CTX_DisableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  4145. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), WOLFSSL_SUCCESS);
  4146. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  4147. ExpectIntEQ(wolfSSL_CTX_DisableOCSPMustStaple(ctx), WOLFSSL_SUCCESS);
  4148. #endif
  4149. wolfSSL_CTX_free(ctx);
  4150. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  4151. #endif /* NO_CERTS */
  4152. return EXPECT_RESULT();
  4153. }
  4154. static int test_wolfSSL_CTX_ticket_API(void)
  4155. {
  4156. EXPECT_DECLS;
  4157. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER)
  4158. WOLFSSL_CTX* ctx = NULL;
  4159. void *userCtx = (void*)"this is my ctx";
  4160. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4161. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(ctx, userCtx));
  4162. ExpectTrue(userCtx == wolfSSL_CTX_get_TicketEncCtx(ctx));
  4163. wolfSSL_CTX_free(ctx);
  4164. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_set_TicketEncCtx(NULL, userCtx));
  4165. ExpectNull(wolfSSL_CTX_get_TicketEncCtx(NULL));
  4166. #endif /* HAVE_SESSION_TICKET && !NO_WOLFSSL_SERVER */
  4167. return EXPECT_RESULT();
  4168. }
  4169. static int test_wolfSSL_set_minmax_proto_version(void)
  4170. {
  4171. EXPECT_DECLS;
  4172. #ifdef OPENSSL_EXTRA
  4173. WOLFSSL_CTX *ctx = NULL;
  4174. WOLFSSL *ssl = NULL;
  4175. (void)ssl;
  4176. #ifndef NO_WOLFSSL_CLIENT
  4177. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4178. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4179. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4180. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4181. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  4182. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  4183. ExpectIntEQ(wolfSSL_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4184. ExpectIntEQ(wolfSSL_set_min_proto_version(ssl, 0), SSL_SUCCESS);
  4185. ExpectIntEQ(wolfSSL_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4186. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl, 0), SSL_SUCCESS);
  4187. wolfSSL_free(ssl);
  4188. wolfSSL_CTX_free(ctx);
  4189. ctx = NULL;
  4190. #endif
  4191. #ifndef NO_WOLFSSL_SERVER
  4192. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4193. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(NULL, 0), SSL_FAILURE);
  4194. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(NULL, 0), SSL_FAILURE);
  4195. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, 0), SSL_SUCCESS);
  4196. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, 0), SSL_SUCCESS);
  4197. wolfSSL_CTX_free(ctx);
  4198. #endif
  4199. #endif
  4200. return EXPECT_RESULT();
  4201. }
  4202. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_TLS12) && \
  4203. defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  4204. static int test_wolfSSL_CTX_set_max_proto_version_on_result(WOLFSSL* ssl)
  4205. {
  4206. EXPECT_DECLS;
  4207. ExpectStrEQ(wolfSSL_get_version(ssl), "TLSv1.2");
  4208. return EXPECT_RESULT();
  4209. }
  4210. static int test_wolfSSL_CTX_set_max_proto_version_ctx_ready(WOLFSSL_CTX* ctx)
  4211. {
  4212. EXPECT_DECLS;
  4213. /* Set TLS 1.2 */
  4214. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  4215. WOLFSSL_SUCCESS);
  4216. return EXPECT_RESULT();
  4217. }
  4218. /* Test using wolfSSL_CTX_set_max_proto_version to limit the version below
  4219. * what was set at ctx creation. */
  4220. static int test_wolfSSL_CTX_set_max_proto_version(void)
  4221. {
  4222. EXPECT_DECLS;
  4223. test_ssl_cbf client_cbs;
  4224. test_ssl_cbf server_cbs;
  4225. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  4226. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  4227. client_cbs.method = wolfTLS_client_method;
  4228. server_cbs.method = wolfTLS_server_method;
  4229. server_cbs.ctx_ready = test_wolfSSL_CTX_set_max_proto_version_ctx_ready;
  4230. client_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  4231. server_cbs.on_result = test_wolfSSL_CTX_set_max_proto_version_on_result;
  4232. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  4233. &server_cbs, NULL), TEST_SUCCESS);
  4234. return EXPECT_RESULT();
  4235. }
  4236. #else
  4237. static int test_wolfSSL_CTX_set_max_proto_version(void)
  4238. {
  4239. return TEST_SKIPPED;
  4240. }
  4241. #endif
  4242. /*----------------------------------------------------------------------------*
  4243. | SSL
  4244. *----------------------------------------------------------------------------*/
  4245. static int test_server_wolfSSL_new(void)
  4246. {
  4247. EXPECT_DECLS;
  4248. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  4249. !defined(NO_WOLFSSL_SERVER)
  4250. WOLFSSL_CTX *ctx = NULL;
  4251. WOLFSSL_CTX *ctx_nocert = NULL;
  4252. WOLFSSL *ssl = NULL;
  4253. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4254. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4255. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4256. WOLFSSL_FILETYPE_PEM));
  4257. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4258. WOLFSSL_FILETYPE_PEM));
  4259. /* invalid context */
  4260. ExpectNull(ssl = wolfSSL_new(NULL));
  4261. #if !defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_QT) && \
  4262. !defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_INIT_CTX_KEY)
  4263. ExpectNull(ssl = wolfSSL_new(ctx_nocert));
  4264. #endif
  4265. /* success */
  4266. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4267. wolfSSL_free(ssl);
  4268. wolfSSL_CTX_free(ctx);
  4269. wolfSSL_CTX_free(ctx_nocert);
  4270. #endif
  4271. return EXPECT_RESULT();
  4272. }
  4273. static int test_client_wolfSSL_new(void)
  4274. {
  4275. EXPECT_DECLS;
  4276. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  4277. !defined(NO_WOLFSSL_CLIENT)
  4278. WOLFSSL_CTX *ctx = NULL;
  4279. WOLFSSL_CTX *ctx_nocert = NULL;
  4280. WOLFSSL *ssl = NULL;
  4281. ExpectNotNull(ctx_nocert = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4282. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  4283. ExpectTrue(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  4284. /* invalid context */
  4285. ExpectNull(ssl = wolfSSL_new(NULL));
  4286. /* success */
  4287. ExpectNotNull(ssl = wolfSSL_new(ctx_nocert));
  4288. wolfSSL_free(ssl);
  4289. ssl = NULL;
  4290. /* success */
  4291. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4292. wolfSSL_free(ssl);
  4293. wolfSSL_CTX_free(ctx);
  4294. wolfSSL_CTX_free(ctx_nocert);
  4295. #endif
  4296. return EXPECT_RESULT();
  4297. }
  4298. static int test_wolfSSL_SetTmpDH_file(void)
  4299. {
  4300. EXPECT_DECLS;
  4301. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_DH) && \
  4302. !defined(NO_WOLFSSL_SERVER)
  4303. WOLFSSL_CTX *ctx = NULL;
  4304. WOLFSSL *ssl = NULL;
  4305. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4306. #ifndef NO_RSA
  4307. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  4308. WOLFSSL_FILETYPE_PEM));
  4309. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  4310. WOLFSSL_FILETYPE_PEM));
  4311. #elif defined(HAVE_ECC)
  4312. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  4313. WOLFSSL_FILETYPE_PEM));
  4314. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  4315. WOLFSSL_FILETYPE_PEM));
  4316. #elif defined(HAVE_ED25519)
  4317. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, edCertFile,
  4318. WOLFSSL_FILETYPE_PEM));
  4319. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  4320. WOLFSSL_FILETYPE_PEM));
  4321. #elif defined(HAVE_ED448)
  4322. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, ed448CertFile,
  4323. WOLFSSL_FILETYPE_PEM));
  4324. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  4325. WOLFSSL_FILETYPE_PEM));
  4326. #endif
  4327. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4328. /* invalid ssl */
  4329. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(NULL,
  4330. dhParamFile, WOLFSSL_FILETYPE_PEM));
  4331. /* invalid dhParamFile file */
  4332. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  4333. NULL, WOLFSSL_FILETYPE_PEM));
  4334. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl,
  4335. bogusFile, WOLFSSL_FILETYPE_PEM));
  4336. /* success */
  4337. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_file(ssl, dhParamFile,
  4338. WOLFSSL_FILETYPE_PEM));
  4339. wolfSSL_free(ssl);
  4340. wolfSSL_CTX_free(ctx);
  4341. #endif
  4342. return EXPECT_RESULT();
  4343. }
  4344. static int test_wolfSSL_SetTmpDH_buffer(void)
  4345. {
  4346. EXPECT_DECLS;
  4347. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  4348. WOLFSSL_CTX *ctx = NULL;
  4349. WOLFSSL *ssl = NULL;
  4350. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4351. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  4352. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4353. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  4354. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4355. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4356. /* invalid ssl */
  4357. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, dh_key_der_2048,
  4358. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4359. /* invalid dhParamFile file */
  4360. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(NULL, NULL,
  4361. 0, WOLFSSL_FILETYPE_ASN1));
  4362. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dsa_key_der_2048,
  4363. sizeof_dsa_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4364. /* success */
  4365. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4366. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4367. wolfSSL_free(ssl);
  4368. wolfSSL_CTX_free(ctx);
  4369. #endif
  4370. return EXPECT_RESULT();
  4371. }
  4372. static int test_wolfSSL_SetMinMaxDhKey_Sz(void)
  4373. {
  4374. EXPECT_DECLS;
  4375. #if !defined(NO_CERTS) && !defined(NO_DH) && !defined(NO_WOLFSSL_SERVER)
  4376. WOLFSSL_CTX *ctx = NULL;
  4377. WOLFSSL_CTX *ctx2 = NULL;
  4378. WOLFSSL *ssl = NULL;
  4379. WOLFSSL *ssl2 = NULL;
  4380. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4381. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx, server_cert_der_2048,
  4382. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4383. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx, server_key_der_2048,
  4384. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4385. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMinDhKey_Sz(ctx, 3072));
  4386. ExpectNotNull(ssl = wolfSSL_new(ctx));
  4387. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  4388. ExpectTrue(wolfSSL_CTX_use_certificate_buffer(ctx2, server_cert_der_2048,
  4389. sizeof_server_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  4390. ExpectTrue(wolfSSL_CTX_use_PrivateKey_buffer(ctx2, server_key_der_2048,
  4391. sizeof_server_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4392. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetMaxDhKey_Sz(ctx, 1024));
  4393. ExpectNotNull(ssl2 = wolfSSL_new(ctx2));
  4394. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4395. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4396. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 2048));
  4397. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4398. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4399. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMinDhKey_Sz(ssl, 3072));
  4400. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4401. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4402. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  4403. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4404. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 2048));
  4405. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpDH_buffer(ssl2, dh_key_der_2048,
  4406. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4407. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetMaxDhKey_Sz(ssl2, 1024));
  4408. ExpectIntEQ(DH_KEY_SIZE_E, wolfSSL_SetTmpDH_buffer(ssl, dh_key_der_2048,
  4409. sizeof_dh_key_der_2048, WOLFSSL_FILETYPE_ASN1));
  4410. wolfSSL_free(ssl2);
  4411. wolfSSL_CTX_free(ctx2);
  4412. wolfSSL_free(ssl);
  4413. wolfSSL_CTX_free(ctx);
  4414. #endif
  4415. return EXPECT_RESULT();
  4416. }
  4417. /* Test function for wolfSSL_SetMinVersion. Sets the minimum downgrade version
  4418. * allowed.
  4419. * POST: return 1 on success.
  4420. */
  4421. static int test_wolfSSL_SetMinVersion(void)
  4422. {
  4423. int res = TEST_SKIPPED;
  4424. #ifndef NO_WOLFSSL_CLIENT
  4425. int failFlag = WOLFSSL_SUCCESS;
  4426. WOLFSSL_CTX* ctx = NULL;
  4427. WOLFSSL* ssl = NULL;
  4428. int itr;
  4429. #ifndef NO_OLD_TLS
  4430. const int versions[] = {
  4431. #ifdef WOLFSSL_ALLOW_TLSV10
  4432. WOLFSSL_TLSV1,
  4433. #endif
  4434. WOLFSSL_TLSV1_1,
  4435. WOLFSSL_TLSV1_2};
  4436. #elif !defined(WOLFSSL_NO_TLS12)
  4437. const int versions[] = { WOLFSSL_TLSV1_2 };
  4438. #else
  4439. const int versions[] = { WOLFSSL_TLSV1_3 };
  4440. #endif
  4441. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  4442. ssl = wolfSSL_new(ctx);
  4443. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  4444. if (wolfSSL_SetMinVersion(ssl, *(versions + itr)) != WOLFSSL_SUCCESS) {
  4445. failFlag = WOLFSSL_FAILURE;
  4446. }
  4447. }
  4448. wolfSSL_free(ssl);
  4449. wolfSSL_CTX_free(ctx);
  4450. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  4451. #endif
  4452. return res;
  4453. } /* END test_wolfSSL_SetMinVersion */
  4454. #ifdef OPENSSL_EXTRA
  4455. static int test_EC25519(void)
  4456. {
  4457. EXPECT_DECLS;
  4458. #if defined(HAVE_CURVE25519) && defined(WOLFSSL_KEY_GEN)
  4459. byte priv[CURVE25519_KEYSIZE];
  4460. unsigned int privSz = CURVE25519_KEYSIZE;
  4461. byte pub[CURVE25519_KEYSIZE];
  4462. unsigned int pubSz = CURVE25519_KEYSIZE;
  4463. byte priv2[CURVE25519_KEYSIZE];
  4464. unsigned int priv2Sz = CURVE25519_KEYSIZE;
  4465. byte pub2[CURVE25519_KEYSIZE];
  4466. unsigned int pub2Sz = CURVE25519_KEYSIZE;
  4467. byte shared[CURVE25519_KEYSIZE];
  4468. unsigned int sharedSz = CURVE25519_KEYSIZE;
  4469. byte shared2[CURVE25519_KEYSIZE];
  4470. unsigned int shared2Sz = CURVE25519_KEYSIZE;
  4471. /* Bad parameter testing of key generation. */
  4472. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, NULL, NULL, NULL), 0);
  4473. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, &privSz, NULL, &pubSz), 0);
  4474. ExpectIntEQ(wolfSSL_EC25519_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4475. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, NULL, pub, &pubSz), 0);
  4476. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4477. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, NULL), 0);
  4478. /* Bad length */
  4479. privSz = 1;
  4480. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4481. privSz = CURVE25519_KEYSIZE;
  4482. pubSz = 1;
  4483. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4484. pubSz = CURVE25519_KEYSIZE;
  4485. /* Good case of generating key. */
  4486. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv, &privSz, pub, &pubSz), 1);
  4487. ExpectIntEQ(wolfSSL_EC25519_generate_key(priv2, &priv2Sz, pub2, &pub2Sz),
  4488. 1);
  4489. ExpectIntEQ(privSz, CURVE25519_KEYSIZE);
  4490. ExpectIntEQ(pubSz, CURVE25519_KEYSIZE);
  4491. /* Bad parameter testing of shared key. */
  4492. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, NULL, NULL, privSz,
  4493. NULL, pubSz), 0);
  4494. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, NULL, privSz,
  4495. NULL, pubSz), 0);
  4496. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, priv, privSz,
  4497. pub, pubSz), 0);
  4498. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, NULL, privSz,
  4499. pub, pubSz), 0);
  4500. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4501. NULL, pubSz), 0);
  4502. ExpectIntEQ(wolfSSL_EC25519_shared_key( NULL, &sharedSz, priv, privSz,
  4503. pub, pubSz), 0);
  4504. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, NULL, priv, privSz,
  4505. pub, pubSz), 0);
  4506. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, NULL, privSz,
  4507. pub, pubSz), 0);
  4508. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4509. NULL, pubSz), 0);
  4510. /* Bad length. */
  4511. sharedSz = 1;
  4512. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4513. pub, pubSz), 0);
  4514. sharedSz = CURVE25519_KEYSIZE;
  4515. privSz = 1;
  4516. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4517. pub, pubSz), 0);
  4518. privSz = CURVE25519_KEYSIZE;
  4519. pubSz = 1;
  4520. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4521. pub, pubSz), 0);
  4522. pubSz = CURVE25519_KEYSIZE;
  4523. /* Good case of shared key. */
  4524. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared, &sharedSz, priv, privSz,
  4525. pub2, pub2Sz), 1);
  4526. ExpectIntEQ(wolfSSL_EC25519_shared_key(shared2, &shared2Sz, priv2, priv2Sz,
  4527. pub, pubSz), 1);
  4528. ExpectIntEQ(sharedSz, CURVE25519_KEYSIZE);
  4529. ExpectIntEQ(shared2Sz, CURVE25519_KEYSIZE);
  4530. ExpectIntEQ(XMEMCMP(shared, shared2, sharedSz), 0);
  4531. #endif /* HAVE_CURVE25519 && WOLFSSL_KEY_GEN */
  4532. return EXPECT_RESULT();
  4533. }
  4534. static int test_ED25519(void)
  4535. {
  4536. EXPECT_DECLS;
  4537. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  4538. defined(WOLFSSL_KEY_GEN)
  4539. byte priv[ED25519_PRV_KEY_SIZE];
  4540. unsigned int privSz = (unsigned int)sizeof(priv);
  4541. byte pub[ED25519_PUB_KEY_SIZE];
  4542. unsigned int pubSz = (unsigned int)sizeof(pub);
  4543. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4544. const char* msg = TEST_STRING;
  4545. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4546. byte sig[ED25519_SIG_SIZE];
  4547. unsigned int sigSz = (unsigned int)sizeof(sig);
  4548. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4549. /* Bad parameter testing of key generation. */
  4550. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, NULL, NULL), 0);
  4551. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, NULL, NULL, NULL), 0);
  4552. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, &privSz, NULL, NULL), 0);
  4553. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, pub, NULL), 0);
  4554. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, NULL, NULL, &pubSz), 0);
  4555. ExpectIntEQ(wolfSSL_ED25519_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4556. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, NULL, pub, &pubSz), 0);
  4557. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4558. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, NULL), 0);
  4559. /* Bad length. */
  4560. privSz = 1;
  4561. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4562. privSz = ED25519_PRV_KEY_SIZE;
  4563. pubSz = 1;
  4564. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz), 0);
  4565. pubSz = ED25519_PUB_KEY_SIZE;
  4566. /* Good case of generating key. */
  4567. ExpectIntEQ(wolfSSL_ED25519_generate_key(priv, &privSz, pub, &pubSz),
  4568. 1);
  4569. ExpectIntEQ(privSz, ED25519_PRV_KEY_SIZE);
  4570. ExpectIntEQ(pubSz, ED25519_PUB_KEY_SIZE);
  4571. #if defined(HAVE_ED25519_SIGN) && defined(HAVE_ED25519_KEY_IMPORT)
  4572. /* Bad parameter testing of signing. */
  4573. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, NULL,
  4574. NULL), 0);
  4575. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, NULL, privSz, NULL,
  4576. NULL), 0);
  4577. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, priv, privSz, NULL,
  4578. NULL), 0);
  4579. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, sig,
  4580. NULL), 0);
  4581. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, NULL, privSz, NULL,
  4582. &sigSz), 0);
  4583. ExpectIntEQ(wolfSSL_ED25519_sign( NULL, msglen, priv, privSz, sig,
  4584. &sigSz), 0);
  4585. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, NULL, privSz, sig,
  4586. &sigSz), 0);
  4587. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, NULL,
  4588. &sigSz), 0);
  4589. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4590. NULL), 0);
  4591. /* Bad length. */
  4592. privSz = 1;
  4593. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4594. &sigSz), 0);
  4595. privSz = ED25519_PRV_KEY_SIZE;
  4596. sigSz = 1;
  4597. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4598. &sigSz), 0);
  4599. sigSz = ED25519_SIG_SIZE;
  4600. /* Good case of signing. */
  4601. ExpectIntEQ(wolfSSL_ED25519_sign((byte*)msg, msglen, priv, privSz, sig,
  4602. &sigSz), 1);
  4603. ExpectIntEQ(sigSz, ED25519_SIG_SIZE);
  4604. #ifdef HAVE_ED25519_VERIFY
  4605. /* Bad parameter testing of verification. */
  4606. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, NULL, pubSz, NULL,
  4607. sigSz), 0);
  4608. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, NULL, pubSz, NULL,
  4609. sigSz), 0);
  4610. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, pub, pubSz, NULL,
  4611. sigSz), 0);
  4612. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, NULL, pubSz, sig,
  4613. sigSz), 0);
  4614. ExpectIntEQ(wolfSSL_ED25519_verify( NULL, msglen, pub, pubSz, sig,
  4615. sigSz), 0);
  4616. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, NULL, pubSz, sig,
  4617. sigSz), 0);
  4618. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, NULL,
  4619. sigSz), 0);
  4620. /* Bad length. */
  4621. pubSz = 1;
  4622. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4623. sigSz), 0);
  4624. pubSz = ED25519_PUB_KEY_SIZE;
  4625. sigSz = 1;
  4626. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4627. sigSz), 0);
  4628. sigSz = ED25519_SIG_SIZE;
  4629. /* Good case of verification. */
  4630. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4631. sigSz), 1);
  4632. /* Bad signature. */
  4633. if (EXPECT_SUCCESS()) {
  4634. sig[1] ^= 0x80;
  4635. }
  4636. ExpectIntEQ(wolfSSL_ED25519_verify((byte*)msg, msglen, pub, pubSz, sig,
  4637. sigSz), 0);
  4638. #endif /* HAVE_ED25519_VERIFY */
  4639. #endif /* HAVE_ED25519_SIGN && HAVE_ED25519_KEY_IMPORT */
  4640. #endif /* HAVE_ED25519 && HAVE_ED25519_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4641. return EXPECT_RESULT();
  4642. }
  4643. static int test_EC448(void)
  4644. {
  4645. EXPECT_DECLS;
  4646. #if defined(HAVE_CURVE448) && defined(WOLFSSL_KEY_GEN)
  4647. byte priv[CURVE448_KEY_SIZE];
  4648. unsigned int privSz = CURVE448_KEY_SIZE;
  4649. byte pub[CURVE448_KEY_SIZE];
  4650. unsigned int pubSz = CURVE448_KEY_SIZE;
  4651. byte priv2[CURVE448_KEY_SIZE];
  4652. unsigned int priv2Sz = CURVE448_KEY_SIZE;
  4653. byte pub2[CURVE448_KEY_SIZE];
  4654. unsigned int pub2Sz = CURVE448_KEY_SIZE;
  4655. byte shared[CURVE448_KEY_SIZE];
  4656. unsigned int sharedSz = CURVE448_KEY_SIZE;
  4657. byte shared2[CURVE448_KEY_SIZE];
  4658. unsigned int shared2Sz = CURVE448_KEY_SIZE;
  4659. /* Bad parameter testing of key generation. */
  4660. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, NULL, NULL, NULL), 0);
  4661. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, &privSz, NULL, &pubSz), 0);
  4662. ExpectIntEQ(wolfSSL_EC448_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4663. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, NULL, pub, &pubSz), 0);
  4664. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4665. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, NULL), 0);
  4666. /* Bad length. */
  4667. privSz = 1;
  4668. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4669. privSz = CURVE448_KEY_SIZE;
  4670. pubSz = 1;
  4671. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4672. pubSz = CURVE448_KEY_SIZE;
  4673. /* Good case of generating key. */
  4674. ExpectIntEQ(wolfSSL_EC448_generate_key(priv, &privSz, pub, &pubSz), 1);
  4675. ExpectIntEQ(wolfSSL_EC448_generate_key(priv2, &priv2Sz, pub2, &pub2Sz), 1);
  4676. ExpectIntEQ(privSz, CURVE448_KEY_SIZE);
  4677. ExpectIntEQ(pubSz, CURVE448_KEY_SIZE);
  4678. /* Bad parameter testing of shared key. */
  4679. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, NULL, NULL, privSz,
  4680. NULL, pubSz), 0);
  4681. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, NULL, privSz,
  4682. NULL, pubSz), 0);
  4683. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, priv, privSz,
  4684. pub, pubSz), 0);
  4685. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, NULL, privSz,
  4686. pub, pubSz), 0);
  4687. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4688. NULL, pubSz), 0);
  4689. ExpectIntEQ(wolfSSL_EC448_shared_key( NULL, &sharedSz, priv, privSz,
  4690. pub, pubSz), 0);
  4691. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, NULL, priv, privSz,
  4692. pub, pubSz), 0);
  4693. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, NULL, privSz,
  4694. pub, pubSz), 0);
  4695. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4696. NULL, pubSz), 0);
  4697. /* Bad length. */
  4698. sharedSz = 1;
  4699. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4700. pub, pubSz), 0);
  4701. sharedSz = CURVE448_KEY_SIZE;
  4702. privSz = 1;
  4703. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4704. pub, pubSz), 0);
  4705. privSz = CURVE448_KEY_SIZE;
  4706. pubSz = 1;
  4707. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4708. pub, pubSz), 0);
  4709. pubSz = CURVE448_KEY_SIZE;
  4710. /* Good case of shared key. */
  4711. ExpectIntEQ(wolfSSL_EC448_shared_key(shared, &sharedSz, priv, privSz,
  4712. pub2, pub2Sz), 1);
  4713. ExpectIntEQ(wolfSSL_EC448_shared_key(shared2, &shared2Sz, priv2, priv2Sz,
  4714. pub, pubSz), 1);
  4715. ExpectIntEQ(sharedSz, CURVE448_KEY_SIZE);
  4716. ExpectIntEQ(shared2Sz, CURVE448_KEY_SIZE);
  4717. ExpectIntEQ(XMEMCMP(shared, shared2, sharedSz), 0);
  4718. #endif /* HAVE_CURVE448 && WOLFSSL_KEY_GEN */
  4719. return EXPECT_RESULT();
  4720. }
  4721. static int test_ED448(void)
  4722. {
  4723. EXPECT_DECLS;
  4724. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  4725. defined(WOLFSSL_KEY_GEN)
  4726. byte priv[ED448_PRV_KEY_SIZE];
  4727. unsigned int privSz = (unsigned int)sizeof(priv);
  4728. byte pub[ED448_PUB_KEY_SIZE];
  4729. unsigned int pubSz = (unsigned int)sizeof(pub);
  4730. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4731. const char* msg = TEST_STRING;
  4732. unsigned int msglen = (unsigned int)TEST_STRING_SZ;
  4733. byte sig[ED448_SIG_SIZE];
  4734. unsigned int sigSz = (unsigned int)sizeof(sig);
  4735. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4736. /* Bad parameter testing of key generation. */
  4737. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, NULL, NULL), 0);
  4738. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, NULL, NULL, NULL), 0);
  4739. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, &privSz, NULL, NULL), 0);
  4740. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, pub, NULL), 0);
  4741. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, NULL, NULL, &pubSz), 0);
  4742. ExpectIntEQ(wolfSSL_ED448_generate_key(NULL, &privSz, pub, &pubSz), 0);
  4743. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, NULL, pub, &pubSz), 0);
  4744. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, NULL, &pubSz), 0);
  4745. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, NULL), 0);
  4746. /* Bad length. */
  4747. privSz = 1;
  4748. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4749. privSz = ED448_PRV_KEY_SIZE;
  4750. pubSz = 1;
  4751. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 0);
  4752. pubSz = ED448_PUB_KEY_SIZE;
  4753. /* Good case of generating key. */
  4754. ExpectIntEQ(wolfSSL_ED448_generate_key(priv, &privSz, pub, &pubSz), 1);
  4755. ExpectIntEQ(privSz, ED448_PRV_KEY_SIZE);
  4756. ExpectIntEQ(pubSz, ED448_PUB_KEY_SIZE);
  4757. #if defined(HAVE_ED448_SIGN) && defined(HAVE_ED448_KEY_IMPORT)
  4758. /* Bad parameter testing of signing. */
  4759. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, NULL,
  4760. NULL), 0);
  4761. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, NULL, privSz, NULL,
  4762. NULL), 0);
  4763. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, priv, privSz, NULL,
  4764. NULL), 0);
  4765. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, sig,
  4766. NULL), 0);
  4767. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, NULL, privSz, NULL,
  4768. &sigSz), 0);
  4769. ExpectIntEQ(wolfSSL_ED448_sign( NULL, msglen, priv, privSz, sig,
  4770. &sigSz), 0);
  4771. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, NULL, privSz, sig,
  4772. &sigSz), 0);
  4773. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, NULL,
  4774. &sigSz), 0);
  4775. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4776. NULL), 0);
  4777. /* Bad length. */
  4778. privSz = 1;
  4779. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4780. &sigSz), 0);
  4781. privSz = ED448_PRV_KEY_SIZE;
  4782. sigSz = 1;
  4783. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4784. &sigSz), 0);
  4785. sigSz = ED448_SIG_SIZE;
  4786. /* Good case of signing. */
  4787. ExpectIntEQ(wolfSSL_ED448_sign((byte*)msg, msglen, priv, privSz, sig,
  4788. &sigSz), 1);
  4789. ExpectIntEQ(sigSz, ED448_SIG_SIZE);
  4790. #ifdef HAVE_ED448_VERIFY
  4791. /* Bad parameter testing of verification. */
  4792. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, NULL, pubSz, NULL,
  4793. sigSz), 0);
  4794. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, NULL, pubSz, NULL,
  4795. sigSz), 0);
  4796. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, pub, pubSz, NULL,
  4797. sigSz), 0);
  4798. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, NULL, pubSz, sig,
  4799. sigSz), 0);
  4800. ExpectIntEQ(wolfSSL_ED448_verify( NULL, msglen, pub, pubSz, sig,
  4801. sigSz), 0);
  4802. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, NULL, pubSz, sig,
  4803. sigSz), 0);
  4804. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, NULL,
  4805. sigSz), 0);
  4806. /* Bad length. */
  4807. pubSz = 1;
  4808. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4809. sigSz), 0);
  4810. pubSz = ED448_PUB_KEY_SIZE;
  4811. sigSz = 1;
  4812. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4813. sigSz), 0);
  4814. sigSz = ED448_SIG_SIZE;
  4815. /* Good case of verification. */
  4816. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4817. sigSz), 1);
  4818. /* Bad signature. */
  4819. if (EXPECT_SUCCESS()) {
  4820. sig[1] ^= 0x80;
  4821. }
  4822. ExpectIntEQ(wolfSSL_ED448_verify((byte*)msg, msglen, pub, pubSz, sig,
  4823. sigSz), 0);
  4824. #endif /* HAVE_ED448_VERIFY */
  4825. #endif /* HAVE_ED448_SIGN && HAVE_ED448_KEY_IMPORT */
  4826. #endif /* HAVE_ED448 && HAVE_ED448_KEY_EXPORT && WOLFSSL_KEY_GEN */
  4827. return EXPECT_RESULT();
  4828. }
  4829. #endif /* OPENSSL_EXTRA */
  4830. #include <wolfssl/openssl/pem.h>
  4831. /*----------------------------------------------------------------------------*
  4832. | EVP
  4833. *----------------------------------------------------------------------------*/
  4834. static int test_wolfSSL_EVP_PKEY_print_public(void)
  4835. {
  4836. EXPECT_DECLS;
  4837. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  4838. WOLFSSL_BIO* rbio = NULL;
  4839. WOLFSSL_BIO* wbio = NULL;
  4840. WOLFSSL_EVP_PKEY* pkey = NULL;
  4841. char line[256] = { 0 };
  4842. char line1[256] = { 0 };
  4843. int i = 0;
  4844. /* test error cases */
  4845. ExpectIntEQ( EVP_PKEY_print_public(NULL,NULL,0,NULL),0L);
  4846. /*
  4847. * test RSA public key print
  4848. * in this test, pass '3' for indent
  4849. */
  4850. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_1024)
  4851. ExpectNotNull(rbio = BIO_new_mem_buf( client_keypub_der_1024,
  4852. sizeof_client_keypub_der_1024));
  4853. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4854. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4855. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,3,NULL),1);
  4856. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4857. strcpy(line1, " RSA Public-Key: (1024 bit)\n");
  4858. ExpectIntEQ(XSTRNCMP(line, line1, XSTRLEN(line1)), 0);
  4859. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4860. strcpy(line1, " Modulus:\n");
  4861. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4862. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4863. strcpy(line1, " 00:bc:73:0e:a8:49:f3:74:a2:a9:ef:18:a5:da:55:\n");
  4864. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4865. /* skip to the end of modulus element*/
  4866. for (i = 0; i < 8 ;i++) {
  4867. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4868. }
  4869. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4870. strcpy(line1, " Exponent: 65537 (0x010001)\n");
  4871. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4872. /* should reach EOF */
  4873. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4874. EVP_PKEY_free(pkey);
  4875. pkey = NULL;
  4876. BIO_free(rbio);
  4877. BIO_free(wbio);
  4878. rbio = NULL;
  4879. wbio = NULL;
  4880. #endif /* !NO_RSA && USE_CERT_BUFFERS_1024*/
  4881. /*
  4882. * test DSA public key print
  4883. */
  4884. #if !defined(NO_DSA) && defined(USE_CERT_BUFFERS_2048)
  4885. ExpectNotNull(rbio = BIO_new_mem_buf( dsa_pub_key_der_2048,
  4886. sizeof_dsa_pub_key_der_2048));
  4887. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4888. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4889. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4890. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4891. strcpy(line1, "DSA Public-Key: (2048 bit)\n");
  4892. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4893. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4894. strcpy(line1, "pub:\n");
  4895. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4896. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4897. strcpy(line1,
  4898. " 00:C2:35:2D:EC:83:83:6C:73:13:9E:52:7C:74:C8:\n");
  4899. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4900. /* skip to the end of pub element*/
  4901. for (i = 0; i < 17 ;i++) {
  4902. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4903. }
  4904. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4905. strcpy(line1, "P:\n");
  4906. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4907. /* skip to the end of P element*/
  4908. for (i = 0; i < 18 ;i++) {
  4909. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4910. }
  4911. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4912. strcpy(line1, "Q:\n");
  4913. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4914. /* skip to the end of Q element*/
  4915. for (i = 0; i < 3 ;i++) {
  4916. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4917. }
  4918. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4919. strcpy(line1, "G:\n");
  4920. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4921. /* skip to the end of G element*/
  4922. for (i = 0; i < 18 ;i++) {
  4923. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4924. }
  4925. /* should reach EOF */
  4926. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4927. EVP_PKEY_free(pkey);
  4928. pkey = NULL;
  4929. BIO_free(rbio);
  4930. BIO_free(wbio);
  4931. rbio = NULL;
  4932. wbio = NULL;
  4933. #endif /* !NO_DSA && USE_CERT_BUFFERS_2048 */
  4934. /*
  4935. * test ECC public key print
  4936. */
  4937. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  4938. ExpectNotNull(rbio = BIO_new_mem_buf( ecc_clikeypub_der_256,
  4939. sizeof_ecc_clikeypub_der_256));
  4940. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4941. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4942. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL),1);
  4943. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4944. strcpy(line1, "Public-Key: (256 bit)\n");
  4945. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4946. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4947. strcpy(line1, "pub:\n");
  4948. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4949. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4950. strcpy(line1,
  4951. " 04:55:BF:F4:0F:44:50:9A:3D:CE:9B:B7:F0:C5:4D:\n");
  4952. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4953. /* skip to the end of pub element*/
  4954. for (i = 0; i < 4 ;i++) {
  4955. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4956. }
  4957. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4958. strcpy(line1, "ASN1 OID: prime256v1\n");
  4959. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4960. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4961. strcpy(line1, "NIST CURVE: P-256\n");
  4962. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4963. /* should reach EOF */
  4964. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  4965. EVP_PKEY_free(pkey);
  4966. pkey = NULL;
  4967. BIO_free(rbio);
  4968. BIO_free(wbio);
  4969. rbio = NULL;
  4970. wbio = NULL;
  4971. #endif /* HAVE_ECC && USE_CERT_BUFFERS_256 */
  4972. /*
  4973. * test DH public key print
  4974. */
  4975. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  4976. ExpectNotNull(rbio = BIO_new_mem_buf( dh_pub_key_der_2048,
  4977. sizeof_dh_pub_key_der_2048));
  4978. ExpectNotNull(wolfSSL_d2i_PUBKEY_bio(rbio, &pkey));
  4979. ExpectNotNull(wbio = BIO_new(BIO_s_mem()));
  4980. ExpectIntEQ(EVP_PKEY_print_public(wbio, pkey,0,NULL), 1);
  4981. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4982. strcpy(line1, "DH Public-Key: (2048 bit)\n");
  4983. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4984. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4985. strcpy(line1, "public-key:\n");
  4986. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4987. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4988. strcpy(line1,
  4989. " 34:41:BF:E9:F2:11:BF:05:DB:B2:72:A8:29:CC:BD:\n");
  4990. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4991. /* skip to the end of public-key element*/
  4992. for (i = 0; i < 17 ;i++) {
  4993. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4994. }
  4995. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4996. strcpy(line1, "prime:\n");
  4997. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  4998. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  4999. strcpy(line1,
  5000. " 00:D3:B2:99:84:5C:0A:4C:E7:37:CC:FC:18:37:01:\n");
  5001. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5002. /* skip to the end of prime element*/
  5003. for (i = 0; i < 17 ;i++) {
  5004. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5005. }
  5006. ExpectIntGT(BIO_gets(wbio, line, sizeof(line)), 0);
  5007. strcpy(line1, "generator: 2 (0x02)\n");
  5008. ExpectIntEQ(XSTRNCMP( line, line1, XSTRLEN(line1)), 0);
  5009. /* should reach EOF */
  5010. ExpectIntLE(BIO_gets(wbio, line, sizeof(line)), 0);
  5011. EVP_PKEY_free(pkey);
  5012. pkey = NULL;
  5013. BIO_free(rbio);
  5014. BIO_free(wbio);
  5015. rbio = NULL;
  5016. wbio = NULL;
  5017. #endif /* WOLFSSL_DH_EXTRA && USE_CERT_BUFFERS_2048 */
  5018. /* to prevent "unused variable" warning */
  5019. (void)pkey;
  5020. (void)wbio;
  5021. (void)rbio;
  5022. (void)line;
  5023. (void)line1;
  5024. (void)i;
  5025. #endif /* OPENSSL_EXTRA */
  5026. return EXPECT_RESULT();
  5027. }
  5028. /* Test functions for base64 encode/decode */
  5029. static int test_wolfSSL_EVP_ENCODE_CTX_new(void)
  5030. {
  5031. EXPECT_DECLS;
  5032. #if defined(OPENSSL_EXTRA) && \
  5033. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  5034. EVP_ENCODE_CTX* ctx = NULL;
  5035. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5036. ExpectIntEQ(ctx->remaining,0);
  5037. ExpectIntEQ(ctx->data[0],0);
  5038. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1],0);
  5039. EVP_ENCODE_CTX_free(ctx);
  5040. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  5041. return EXPECT_RESULT();
  5042. }
  5043. static int test_wolfSSL_EVP_ENCODE_CTX_free(void)
  5044. {
  5045. EXPECT_DECLS;
  5046. #if defined(OPENSSL_EXTRA) && \
  5047. ( defined(WOLFSSL_BASE64_ENCODE) || defined(WOLFSSL_BASE64_DECODE))
  5048. EVP_ENCODE_CTX* ctx = NULL;
  5049. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5050. EVP_ENCODE_CTX_free(ctx);
  5051. #endif /* OPENSSL_EXTRA && (WOLFSSL_BASE64_ENCODE || WOLFSSL_BASE64_DECODE) */
  5052. return EXPECT_RESULT();
  5053. }
  5054. static int test_wolfSSL_EVP_EncodeInit(void)
  5055. {
  5056. EXPECT_DECLS;
  5057. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  5058. EVP_ENCODE_CTX* ctx = NULL;
  5059. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5060. ExpectIntEQ(ctx->remaining, 0);
  5061. ExpectIntEQ(ctx->data[0], 0);
  5062. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  5063. if (ctx != NULL) {
  5064. /* make ctx dirty */
  5065. ctx->remaining = 10;
  5066. XMEMSET(ctx->data, 0x77, sizeof(ctx->data));
  5067. }
  5068. EVP_EncodeInit(ctx);
  5069. ExpectIntEQ(ctx->remaining, 0);
  5070. ExpectIntEQ(ctx->data[0], 0);
  5071. ExpectIntEQ(ctx->data[sizeof(ctx->data) -1], 0);
  5072. EVP_ENCODE_CTX_free(ctx);
  5073. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  5074. return EXPECT_RESULT();
  5075. }
  5076. static int test_wolfSSL_EVP_EncodeUpdate(void)
  5077. {
  5078. EXPECT_DECLS;
  5079. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  5080. int outl;
  5081. int total;
  5082. const unsigned char plain0[] = {"Th"};
  5083. const unsigned char plain1[] = {"This is a base64 encodeing test."};
  5084. const unsigned char plain2[] = {"This is additional data."};
  5085. const unsigned char encBlock0[] = {"VGg="};
  5086. const unsigned char enc0[] = {"VGg=\n"};
  5087. /* expected encoded result for the first output 64 chars plus trailing LF*/
  5088. const unsigned char enc1[] = {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\n"};
  5089. const unsigned char enc2[] =
  5090. {"VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVpbmcgdGVzdC5UaGlzIGlzIGFkZGl0aW9u\nYWwgZGF0YS4=\n"};
  5091. unsigned char encOutBuff[300];
  5092. EVP_ENCODE_CTX* ctx = NULL;
  5093. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5094. EVP_EncodeInit(ctx);
  5095. /* illegal parameter test */
  5096. ExpectIntEQ(
  5097. EVP_EncodeUpdate(
  5098. NULL, /* pass NULL as ctx */
  5099. encOutBuff,
  5100. &outl,
  5101. plain1,
  5102. sizeof(plain1)-1),
  5103. 0 /* expected result code 0: fail */
  5104. );
  5105. ExpectIntEQ(
  5106. EVP_EncodeUpdate(
  5107. ctx,
  5108. NULL, /* pass NULL as out buff */
  5109. &outl,
  5110. plain1,
  5111. sizeof(plain1)-1),
  5112. 0 /* expected result code 0: fail */
  5113. );
  5114. ExpectIntEQ(
  5115. EVP_EncodeUpdate(
  5116. ctx,
  5117. encOutBuff,
  5118. NULL, /* pass NULL as outl */
  5119. plain1,
  5120. sizeof(plain1)-1),
  5121. 0 /* expected result code 0: fail */
  5122. );
  5123. ExpectIntEQ(
  5124. EVP_EncodeUpdate(
  5125. ctx,
  5126. encOutBuff,
  5127. &outl,
  5128. NULL, /* pass NULL as in */
  5129. sizeof(plain1)-1),
  5130. 0 /* expected result code 0: fail */
  5131. );
  5132. ExpectIntEQ(EVP_EncodeBlock(NULL, NULL, 0), -1);
  5133. /* meaningless parameter test */
  5134. ExpectIntEQ(
  5135. EVP_EncodeUpdate(
  5136. ctx,
  5137. encOutBuff,
  5138. &outl,
  5139. plain1,
  5140. 0), /* pass zero input */
  5141. 1 /* expected result code 1: success */
  5142. );
  5143. /* very small data encoding test */
  5144. EVP_EncodeInit(ctx);
  5145. ExpectIntEQ(
  5146. EVP_EncodeUpdate(
  5147. ctx,
  5148. encOutBuff,
  5149. &outl,
  5150. plain0,
  5151. sizeof(plain0)-1),
  5152. 1 /* expected result code 1: success */
  5153. );
  5154. ExpectIntEQ(outl,0);
  5155. if (EXPECT_SUCCESS()) {
  5156. EVP_EncodeFinal(
  5157. ctx,
  5158. encOutBuff + outl,
  5159. &outl);
  5160. }
  5161. ExpectIntEQ( outl, sizeof(enc0)-1);
  5162. ExpectIntEQ(
  5163. XSTRNCMP(
  5164. (const char*)encOutBuff,
  5165. (const char*)enc0,sizeof(enc0) ),
  5166. 0);
  5167. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  5168. ExpectIntEQ(EVP_EncodeBlock(encOutBuff, plain0, sizeof(plain0)-1),
  5169. sizeof(encBlock0)-1);
  5170. ExpectStrEQ(encOutBuff, encBlock0);
  5171. /* pass small size( < 48bytes ) input, then make sure they are not
  5172. * encoded and just stored in ctx
  5173. */
  5174. EVP_EncodeInit(ctx);
  5175. total = 0;
  5176. outl = 0;
  5177. XMEMSET( encOutBuff,0, sizeof(encOutBuff));
  5178. ExpectIntEQ(
  5179. EVP_EncodeUpdate(
  5180. ctx,
  5181. encOutBuff, /* buffer for output */
  5182. &outl, /* size of output */
  5183. plain1, /* input */
  5184. sizeof(plain1)-1), /* size of input */
  5185. 1); /* expected result code 1:success */
  5186. total += outl;
  5187. ExpectIntEQ(outl, 0); /* no output expected */
  5188. ExpectIntEQ(ctx->remaining, sizeof(plain1) -1);
  5189. ExpectTrue(
  5190. XSTRNCMP((const char*)(ctx->data),
  5191. (const char*)plain1,
  5192. ctx->remaining) ==0 );
  5193. ExpectTrue(encOutBuff[0] == 0);
  5194. /* call wolfSSL_EVP_EncodeUpdate again to make it encode
  5195. * the stored data and the new input together
  5196. */
  5197. ExpectIntEQ(
  5198. EVP_EncodeUpdate(
  5199. ctx,
  5200. encOutBuff + outl, /* buffer for output */
  5201. &outl, /* size of output */
  5202. plain2, /* additional input */
  5203. sizeof(plain2) -1), /* size of additional input */
  5204. 1); /* expected result code 1:success */
  5205. total += outl;
  5206. ExpectIntNE(outl, 0); /* some output is expected this time*/
  5207. ExpectIntEQ(outl, BASE64_ENCODE_RESULT_BLOCK_SIZE +1); /* 64 bytes and LF */
  5208. ExpectIntEQ(
  5209. XSTRNCMP((const char*)encOutBuff,(const char*)enc1,sizeof(enc1) ),0);
  5210. /* call wolfSSL_EVP_EncodeFinal to flush all the unprocessed input */
  5211. EVP_EncodeFinal(
  5212. ctx,
  5213. encOutBuff + outl,
  5214. &outl);
  5215. total += outl;
  5216. ExpectIntNE(total,0);
  5217. ExpectIntNE(outl,0);
  5218. ExpectIntEQ(XSTRNCMP(
  5219. (const char*)encOutBuff,(const char*)enc2,sizeof(enc2) ),0);
  5220. /* test with illeagal parameters */
  5221. outl = 1;
  5222. EVP_EncodeFinal(NULL, encOutBuff + outl, &outl);
  5223. ExpectIntEQ(outl, 0);
  5224. outl = 1;
  5225. EVP_EncodeFinal(ctx, NULL, &outl);
  5226. ExpectIntEQ(outl, 0);
  5227. EVP_EncodeFinal(ctx, encOutBuff + outl, NULL);
  5228. EVP_EncodeFinal(NULL, NULL, NULL);
  5229. EVP_ENCODE_CTX_free(ctx);
  5230. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  5231. return EXPECT_RESULT();
  5232. }
  5233. static int test_wolfSSL_EVP_EncodeFinal(void)
  5234. {
  5235. int res = TEST_SKIPPED;
  5236. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  5237. /* tests for wolfSSL_EVP_EncodeFinal are included in
  5238. * test_wolfSSL_EVP_EncodeUpdate
  5239. */
  5240. res = TEST_SUCCESS;
  5241. #endif /* OPENSSL_EXTRA && WOLFSSL_BASE64_ENCODE*/
  5242. return res;
  5243. }
  5244. static int test_wolfSSL_EVP_DecodeInit(void)
  5245. {
  5246. EXPECT_DECLS;
  5247. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5248. EVP_ENCODE_CTX* ctx = NULL;
  5249. ExpectNotNull( ctx = EVP_ENCODE_CTX_new());
  5250. ExpectIntEQ( ctx->remaining,0);
  5251. ExpectIntEQ( ctx->data[0],0);
  5252. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  5253. if (ctx != NULL) {
  5254. /* make ctx dirty */
  5255. ctx->remaining = 10;
  5256. XMEMSET( ctx->data, 0x77, sizeof(ctx->data));
  5257. }
  5258. EVP_DecodeInit(ctx);
  5259. ExpectIntEQ( ctx->remaining,0);
  5260. ExpectIntEQ( ctx->data[0],0);
  5261. ExpectIntEQ( ctx->data[sizeof(ctx->data) -1],0);
  5262. EVP_ENCODE_CTX_free(ctx);
  5263. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5264. return EXPECT_RESULT();
  5265. }
  5266. static int test_wolfSSL_EVP_DecodeUpdate(void)
  5267. {
  5268. EXPECT_DECLS;
  5269. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5270. int outl;
  5271. unsigned char decOutBuff[300];
  5272. EVP_ENCODE_CTX* ctx = NULL;
  5273. static const unsigned char enc1[] =
  5274. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  5275. /* const unsigned char plain1[] =
  5276. {"This is a base64 decoding test."} */
  5277. ExpectNotNull(ctx = EVP_ENCODE_CTX_new());
  5278. EVP_DecodeInit(ctx);
  5279. /* illegal parameter tests */
  5280. /* pass NULL as ctx */
  5281. ExpectIntEQ(
  5282. EVP_DecodeUpdate(
  5283. NULL, /* pass NULL as ctx */
  5284. decOutBuff,
  5285. &outl,
  5286. enc1,
  5287. sizeof(enc1)-1),
  5288. -1 /* expected result code -1: fail */
  5289. );
  5290. ExpectIntEQ( outl, 0);
  5291. /* pass NULL as output */
  5292. ExpectIntEQ(
  5293. EVP_DecodeUpdate(
  5294. ctx,
  5295. NULL, /* pass NULL as out buff */
  5296. &outl,
  5297. enc1,
  5298. sizeof(enc1)-1),
  5299. -1 /* expected result code -1: fail */
  5300. );
  5301. ExpectIntEQ( outl, 0);
  5302. /* pass NULL as outl */
  5303. ExpectIntEQ(
  5304. EVP_DecodeUpdate(
  5305. ctx,
  5306. decOutBuff,
  5307. NULL, /* pass NULL as outl */
  5308. enc1,
  5309. sizeof(enc1)-1),
  5310. -1 /* expected result code -1: fail */
  5311. );
  5312. /* pass NULL as input */
  5313. ExpectIntEQ(
  5314. EVP_DecodeUpdate(
  5315. ctx,
  5316. decOutBuff,
  5317. &outl,
  5318. NULL, /* pass NULL as in */
  5319. sizeof(enc1)-1),
  5320. -1 /* expected result code -1: fail */
  5321. );
  5322. ExpectIntEQ( outl, 0);
  5323. ExpectIntEQ(EVP_DecodeBlock(NULL, NULL, 0), -1);
  5324. /* pass zero length input */
  5325. ExpectIntEQ(
  5326. EVP_DecodeUpdate(
  5327. ctx,
  5328. decOutBuff,
  5329. &outl,
  5330. enc1,
  5331. 0), /* pass zero as input len */
  5332. 1 /* expected result code 1: success */
  5333. );
  5334. /* decode correct base64 string */
  5335. {
  5336. static const unsigned char enc2[] =
  5337. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg==\n"};
  5338. static const unsigned char plain2[] =
  5339. {"This is a base64 decoding test."};
  5340. EVP_EncodeInit(ctx);
  5341. ExpectIntEQ(
  5342. EVP_DecodeUpdate(
  5343. ctx,
  5344. decOutBuff,
  5345. &outl,
  5346. enc2,
  5347. sizeof(enc2)-1),
  5348. 0 /* expected result code 0: success */
  5349. );
  5350. ExpectIntEQ(outl,sizeof(plain2) -1);
  5351. ExpectIntEQ(
  5352. EVP_DecodeFinal(
  5353. ctx,
  5354. decOutBuff + outl,
  5355. &outl),
  5356. 1 /* expected result code 1: success */
  5357. );
  5358. ExpectIntEQ(outl, 0); /* expected DecodeFinal output no data */
  5359. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  5360. sizeof(plain2) -1 ),0);
  5361. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc2, sizeof(enc2)),
  5362. sizeof(plain2)-1);
  5363. ExpectIntEQ(XSTRNCMP( (const char*)plain2,(const char*)decOutBuff,
  5364. sizeof(plain2) -1 ),0);
  5365. }
  5366. /* decode correct base64 string which does not have '\n' in its last*/
  5367. {
  5368. static const unsigned char enc3[] =
  5369. {"VGhpcyBpcyBhIGJhc2U2NCBkZWNvZGluZyB0ZXN0Lg=="}; /* 44 chars */
  5370. static const unsigned char plain3[] =
  5371. {"This is a base64 decoding test."}; /* 31 chars */
  5372. EVP_EncodeInit(ctx);
  5373. ExpectIntEQ(
  5374. EVP_DecodeUpdate(
  5375. ctx,
  5376. decOutBuff,
  5377. &outl,
  5378. enc3,
  5379. sizeof(enc3)-1),
  5380. 0 /* expected result code 0: success */
  5381. );
  5382. ExpectIntEQ(outl,sizeof(plain3)-1); /* 31 chars should be output */
  5383. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  5384. sizeof(plain3) -1 ),0);
  5385. ExpectIntEQ(
  5386. EVP_DecodeFinal(
  5387. ctx,
  5388. decOutBuff + outl,
  5389. &outl),
  5390. 1 /* expected result code 1: success */
  5391. );
  5392. ExpectIntEQ(outl,0 );
  5393. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc3, sizeof(enc3)-1),
  5394. sizeof(plain3)-1);
  5395. ExpectIntEQ(XSTRNCMP( (const char*)plain3,(const char*)decOutBuff,
  5396. sizeof(plain3) -1 ),0);
  5397. }
  5398. /* decode string which has a padding char ('=') in the illegal position*/
  5399. {
  5400. static const unsigned char enc4[] =
  5401. {"VGhpcyBpcyBhIGJhc2U2N=CBkZWNvZGluZyB0ZXN0Lg==\n"};
  5402. EVP_EncodeInit(ctx);
  5403. ExpectIntEQ(
  5404. EVP_DecodeUpdate(
  5405. ctx,
  5406. decOutBuff,
  5407. &outl,
  5408. enc4,
  5409. sizeof(enc4)-1),
  5410. -1 /* expected result code -1: error */
  5411. );
  5412. ExpectIntEQ(outl,0);
  5413. ExpectIntEQ(EVP_DecodeBlock(decOutBuff, enc4, sizeof(enc4)-1), -1);
  5414. }
  5415. /* small data decode test */
  5416. {
  5417. static const unsigned char enc00[] = {"VG"};
  5418. static const unsigned char enc01[] = {"g=\n"};
  5419. static const unsigned char plain4[] = {"Th"};
  5420. EVP_EncodeInit(ctx);
  5421. ExpectIntEQ(
  5422. EVP_DecodeUpdate(
  5423. ctx,
  5424. decOutBuff,
  5425. &outl,
  5426. enc00,
  5427. sizeof(enc00)-1),
  5428. 1 /* expected result code 1: success */
  5429. );
  5430. ExpectIntEQ(outl,0);
  5431. ExpectIntEQ(
  5432. EVP_DecodeUpdate(
  5433. ctx,
  5434. decOutBuff + outl,
  5435. &outl,
  5436. enc01,
  5437. sizeof(enc01)-1),
  5438. 0 /* expected result code 0: success */
  5439. );
  5440. ExpectIntEQ(outl,sizeof(plain4)-1);
  5441. /* test with illegal parameters */
  5442. ExpectIntEQ(EVP_DecodeFinal(NULL,decOutBuff + outl,&outl), -1);
  5443. ExpectIntEQ(EVP_DecodeFinal(ctx,NULL,&outl), -1);
  5444. ExpectIntEQ(EVP_DecodeFinal(ctx,decOutBuff + outl, NULL), -1);
  5445. ExpectIntEQ(EVP_DecodeFinal(NULL,NULL, NULL), -1);
  5446. if (EXPECT_SUCCESS()) {
  5447. EVP_DecodeFinal(
  5448. ctx,
  5449. decOutBuff + outl,
  5450. &outl);
  5451. }
  5452. ExpectIntEQ( outl, 0);
  5453. ExpectIntEQ(
  5454. XSTRNCMP(
  5455. (const char*)decOutBuff,
  5456. (const char*)plain4,sizeof(plain4)-1 ),
  5457. 0);
  5458. }
  5459. EVP_ENCODE_CTX_free(ctx);
  5460. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5461. return EXPECT_RESULT();
  5462. }
  5463. static int test_wolfSSL_EVP_DecodeFinal(void)
  5464. {
  5465. int res = TEST_SKIPPED;
  5466. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_DECODE)
  5467. /* tests for wolfSSL_EVP_DecodeFinal are included in
  5468. * test_wolfSSL_EVP_DecodeUpdate
  5469. */
  5470. res = TEST_SUCCESS;
  5471. #endif /* OPENSSL && WOLFSSL_BASE_DECODE */
  5472. return res;
  5473. }
  5474. /* Test function for wolfSSL_EVP_get_cipherbynid.
  5475. */
  5476. #ifdef OPENSSL_EXTRA
  5477. static int test_wolfSSL_EVP_get_cipherbynid(void)
  5478. {
  5479. EXPECT_DECLS;
  5480. #ifndef NO_AES
  5481. const WOLFSSL_EVP_CIPHER* c;
  5482. c = wolfSSL_EVP_get_cipherbynid(419);
  5483. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5484. defined(WOLFSSL_AES_128)
  5485. ExpectNotNull(c);
  5486. ExpectNotNull(XSTRCMP("EVP_AES_128_CBC", c));
  5487. #else
  5488. ExpectNull(c);
  5489. #endif
  5490. c = wolfSSL_EVP_get_cipherbynid(423);
  5491. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5492. defined(WOLFSSL_AES_192)
  5493. ExpectNotNull(c);
  5494. ExpectNotNull(XSTRCMP("EVP_AES_192_CBC", c));
  5495. #else
  5496. ExpectNull(c);
  5497. #endif
  5498. c = wolfSSL_EVP_get_cipherbynid(427);
  5499. #if (defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)) && \
  5500. defined(WOLFSSL_AES_256)
  5501. ExpectNotNull(c);
  5502. ExpectNotNull(XSTRCMP("EVP_AES_256_CBC", c));
  5503. #else
  5504. ExpectNull(c);
  5505. #endif
  5506. c = wolfSSL_EVP_get_cipherbynid(904);
  5507. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_128)
  5508. ExpectNotNull(c);
  5509. ExpectNotNull(XSTRCMP("EVP_AES_128_CTR", c));
  5510. #else
  5511. ExpectNull(c);
  5512. #endif
  5513. c = wolfSSL_EVP_get_cipherbynid(905);
  5514. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_192)
  5515. ExpectNotNull(c);
  5516. ExpectNotNull(XSTRCMP("EVP_AES_192_CTR", c));
  5517. #else
  5518. ExpectNull(c);
  5519. #endif
  5520. c = wolfSSL_EVP_get_cipherbynid(906);
  5521. #if defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  5522. ExpectNotNull(c);
  5523. ExpectNotNull(XSTRCMP("EVP_AES_256_CTR", c));
  5524. #else
  5525. ExpectNull(c);
  5526. #endif
  5527. c = wolfSSL_EVP_get_cipherbynid(418);
  5528. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_128)
  5529. ExpectNotNull(c);
  5530. ExpectNotNull(XSTRCMP("EVP_AES_128_ECB", c));
  5531. #else
  5532. ExpectNull(c);
  5533. #endif
  5534. c = wolfSSL_EVP_get_cipherbynid(422);
  5535. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_192)
  5536. ExpectNotNull(c);
  5537. ExpectNotNull(XSTRCMP("EVP_AES_192_ECB", c));
  5538. #else
  5539. ExpectNull(c);
  5540. #endif
  5541. c = wolfSSL_EVP_get_cipherbynid(426);
  5542. #if defined(HAVE_AES_ECB) && defined(WOLFSSL_AES_256)
  5543. ExpectNotNull(c);
  5544. ExpectNotNull(XSTRCMP("EVP_AES_256_ECB", c));
  5545. #else
  5546. ExpectNull(c);
  5547. #endif
  5548. #endif /* !NO_AES */
  5549. #ifndef NO_DES3
  5550. ExpectNotNull(XSTRCMP("EVP_DES_CBC", wolfSSL_EVP_get_cipherbynid(31)));
  5551. #ifdef WOLFSSL_DES_ECB
  5552. ExpectNotNull(XSTRCMP("EVP_DES_ECB", wolfSSL_EVP_get_cipherbynid(29)));
  5553. #endif
  5554. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_CBC", wolfSSL_EVP_get_cipherbynid(44)));
  5555. #ifdef WOLFSSL_DES_ECB
  5556. ExpectNotNull(XSTRCMP("EVP_DES_EDE3_ECB", wolfSSL_EVP_get_cipherbynid(33)));
  5557. #endif
  5558. #endif /* !NO_DES3 */
  5559. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  5560. ExpectNotNull(XSTRCMP("EVP_CHACHA20_POLY13O5", EVP_get_cipherbynid(1018)));
  5561. #endif
  5562. /* test for nid is out of range */
  5563. ExpectNull(wolfSSL_EVP_get_cipherbynid(1));
  5564. return EXPECT_RESULT();
  5565. }
  5566. static int test_wolfSSL_EVP_CIPHER_CTX(void)
  5567. {
  5568. EXPECT_DECLS;
  5569. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  5570. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  5571. const EVP_CIPHER *init = EVP_aes_128_cbc();
  5572. const EVP_CIPHER *test;
  5573. byte key[AES_BLOCK_SIZE] = {0};
  5574. byte iv[AES_BLOCK_SIZE] = {0};
  5575. ExpectNotNull(ctx);
  5576. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  5577. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  5578. test = EVP_CIPHER_CTX_cipher(ctx);
  5579. ExpectTrue(init == test);
  5580. ExpectIntEQ(EVP_CIPHER_nid(test), NID_aes_128_cbc);
  5581. ExpectIntEQ(EVP_CIPHER_CTX_reset(ctx), WOLFSSL_SUCCESS);
  5582. ExpectIntEQ(EVP_CIPHER_CTX_reset(NULL), WOLFSSL_FAILURE);
  5583. EVP_CIPHER_CTX_free(ctx);
  5584. /* test EVP_CIPHER_CTX_cleanup with NULL */
  5585. ExpectIntEQ(EVP_CIPHER_CTX_cleanup(NULL), WOLFSSL_SUCCESS);
  5586. #endif /* !NO_AES && HAVE_AES_CBC && WOLFSSL_AES_128 */
  5587. return EXPECT_RESULT();
  5588. }
  5589. #endif /* OPENSSL_EXTRA */
  5590. /*----------------------------------------------------------------------------*
  5591. | IO
  5592. *----------------------------------------------------------------------------*/
  5593. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) || \
  5594. defined(HAVE_IO_TESTS_DEPENDENCIES)
  5595. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  5596. #ifdef WC_SHA512_DIGEST_SIZE
  5597. #define MD_MAX_SIZE WC_SHA512_DIGEST_SIZE
  5598. #else
  5599. #define MD_MAX_SIZE WC_SHA256_DIGEST_SIZE
  5600. #endif
  5601. byte server_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by server */
  5602. byte server_side_msg2[MD_MAX_SIZE] = {0};/* msg received from client */
  5603. byte client_side_msg1[MD_MAX_SIZE] = {0};/* msg sent by client */
  5604. byte client_side_msg2[MD_MAX_SIZE] = {0};/* msg received from server */
  5605. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  5606. /* TODO: Expand and enable this when EVP_chacha20_poly1305 is supported */
  5607. #if defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  5608. defined(HAVE_AES_CBC)
  5609. typedef struct openssl_key_ctx {
  5610. byte name[WOLFSSL_TICKET_NAME_SZ]; /* server name */
  5611. byte key[WOLFSSL_TICKET_KEY_SZ]; /* cipher key */
  5612. byte hmacKey[WOLFSSL_TICKET_NAME_SZ]; /* hmac key */
  5613. byte iv[WOLFSSL_TICKET_IV_SZ]; /* cipher iv */
  5614. } openssl_key_ctx;
  5615. static THREAD_LS_T openssl_key_ctx myOpenSSLKey_ctx;
  5616. static THREAD_LS_T WC_RNG myOpenSSLKey_rng;
  5617. static WC_INLINE int OpenSSLTicketInit(void)
  5618. {
  5619. int ret = wc_InitRng(&myOpenSSLKey_rng);
  5620. if (ret != 0) return ret;
  5621. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.name,
  5622. sizeof(myOpenSSLKey_ctx.name));
  5623. if (ret != 0) return ret;
  5624. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.key,
  5625. sizeof(myOpenSSLKey_ctx.key));
  5626. if (ret != 0) return ret;
  5627. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.hmacKey,
  5628. sizeof(myOpenSSLKey_ctx.hmacKey));
  5629. if (ret != 0) return ret;
  5630. ret = wc_RNG_GenerateBlock(&myOpenSSLKey_rng, myOpenSSLKey_ctx.iv,
  5631. sizeof(myOpenSSLKey_ctx.iv));
  5632. if (ret != 0) return ret;
  5633. return 0;
  5634. }
  5635. static int myTicketEncCbOpenSSL(WOLFSSL* ssl,
  5636. byte name[WOLFSSL_TICKET_NAME_SZ],
  5637. byte iv[WOLFSSL_TICKET_IV_SZ],
  5638. WOLFSSL_EVP_CIPHER_CTX *ectx,
  5639. WOLFSSL_HMAC_CTX *hctx, int enc) {
  5640. (void)ssl;
  5641. if (enc) {
  5642. XMEMCPY(name, myOpenSSLKey_ctx.name, sizeof(myOpenSSLKey_ctx.name));
  5643. XMEMCPY(iv, myOpenSSLKey_ctx.iv, sizeof(myOpenSSLKey_ctx.iv));
  5644. }
  5645. else if (XMEMCMP(name, myOpenSSLKey_ctx.name,
  5646. sizeof(myOpenSSLKey_ctx.name)) != 0 ||
  5647. XMEMCMP(iv, myOpenSSLKey_ctx.iv,
  5648. sizeof(myOpenSSLKey_ctx.iv)) != 0) {
  5649. return 0;
  5650. }
  5651. HMAC_Init_ex(hctx, myOpenSSLKey_ctx.hmacKey, WOLFSSL_TICKET_NAME_SZ, EVP_sha256(), NULL);
  5652. if (enc)
  5653. EVP_EncryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  5654. else
  5655. EVP_DecryptInit_ex(ectx, EVP_aes_256_cbc(), NULL, myOpenSSLKey_ctx.key, iv);
  5656. return 1;
  5657. }
  5658. static WC_INLINE void OpenSSLTicketCleanup(void)
  5659. {
  5660. wc_FreeRng(&myOpenSSLKey_rng);
  5661. }
  5662. #endif
  5663. #endif
  5664. /* helper functions */
  5665. #ifdef HAVE_SSL_MEMIO_TESTS_DEPENDENCIES
  5666. static WC_INLINE int test_ssl_memio_write_cb(WOLFSSL *ssl, char *data, int sz,
  5667. void *ctx)
  5668. {
  5669. struct test_ssl_memio_ctx *test_ctx;
  5670. byte *buf;
  5671. int *len;
  5672. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  5673. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  5674. buf = test_ctx->c_buff;
  5675. len = &test_ctx->c_len;
  5676. }
  5677. else {
  5678. buf = test_ctx->s_buff;
  5679. len = &test_ctx->s_len;
  5680. }
  5681. if ((unsigned)(*len + sz) > TEST_SSL_MEMIO_BUF_SZ)
  5682. return WOLFSSL_CBIO_ERR_WANT_WRITE;
  5683. XMEMCPY(buf + *len, data, sz);
  5684. *len += sz;
  5685. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  5686. {
  5687. /* This can be imported into Wireshark by transforming the file with
  5688. * od -Ax -tx1 -v test_output.dump > test_output.dump.hex
  5689. * And then loading test_output.dump.hex into Wireshark using the
  5690. * "Import from Hex Dump..." option ion and selecting the TCP
  5691. * encapsulation option. */
  5692. char dump_file_name[64];
  5693. WOLFSSL_BIO *dump_file;
  5694. sprintf(dump_file_name, "%s/%s.dump", tmpDirName, currentTestName);
  5695. dump_file = wolfSSL_BIO_new_file(dump_file_name, "a");
  5696. if (dump_file != NULL) {
  5697. (void)wolfSSL_BIO_write(dump_file, data, sz);
  5698. wolfSSL_BIO_free(dump_file);
  5699. }
  5700. }
  5701. #endif
  5702. return sz;
  5703. }
  5704. static WC_INLINE int test_ssl_memio_read_cb(WOLFSSL *ssl, char *data, int sz,
  5705. void *ctx)
  5706. {
  5707. struct test_ssl_memio_ctx *test_ctx;
  5708. int read_sz;
  5709. byte *buf;
  5710. int *len;
  5711. test_ctx = (struct test_ssl_memio_ctx*)ctx;
  5712. if (wolfSSL_GetSide(ssl) == WOLFSSL_SERVER_END) {
  5713. buf = test_ctx->s_buff;
  5714. len = &test_ctx->s_len;
  5715. }
  5716. else {
  5717. buf = test_ctx->c_buff;
  5718. len = &test_ctx->c_len;
  5719. }
  5720. if (*len == 0)
  5721. return WOLFSSL_CBIO_ERR_WANT_READ;
  5722. read_sz = sz < *len ? sz : *len;
  5723. XMEMCPY(data, buf, read_sz);
  5724. XMEMMOVE(buf, buf + read_sz, *len - read_sz);
  5725. *len -= read_sz;
  5726. return read_sz;
  5727. }
  5728. static WC_INLINE int test_ssl_memio_setup(test_ssl_memio_ctx *ctx)
  5729. {
  5730. EXPECT_DECLS;
  5731. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5732. int c_sharedCtx = 0;
  5733. int s_sharedCtx = 0;
  5734. #endif
  5735. const char* clientCertFile = cliCertFile;
  5736. const char* clientKeyFile = cliKeyFile;
  5737. const char* serverCertFile = svrCertFile;
  5738. const char* serverKeyFile = svrKeyFile;
  5739. /********************************
  5740. * Create WOLFSSL_CTX for client.
  5741. ********************************/
  5742. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5743. if (ctx->c_ctx != NULL) {
  5744. c_sharedCtx = ctx->c_cb.isSharedCtx;
  5745. }
  5746. else
  5747. #endif
  5748. {
  5749. WOLFSSL_METHOD* method = NULL;
  5750. if (ctx->c_cb.method != NULL) {
  5751. method = ctx->c_cb.method();
  5752. }
  5753. else {
  5754. method = wolfSSLv23_client_method();
  5755. }
  5756. ExpectNotNull(ctx->c_ctx = wolfSSL_CTX_new(method));
  5757. }
  5758. wolfSSL_SetIORecv(ctx->c_ctx, test_ssl_memio_read_cb);
  5759. wolfSSL_SetIOSend(ctx->c_ctx, test_ssl_memio_write_cb);
  5760. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5761. wolfSSL_CTX_set_default_passwd_cb(ctx->c_ctx, PasswordCallBack);
  5762. #endif
  5763. if (ctx->c_cb.caPemFile != NULL)
  5764. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx,
  5765. ctx->c_cb.caPemFile, 0), WOLFSSL_SUCCESS);
  5766. else
  5767. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->c_ctx,
  5768. caCertFile, 0), WOLFSSL_SUCCESS);
  5769. if (ctx->c_cb.certPemFile != NULL) {
  5770. clientCertFile = ctx->c_cb.certPemFile;
  5771. }
  5772. if (ctx->c_cb.keyPemFile != NULL) {
  5773. clientKeyFile = ctx->c_cb.keyPemFile;
  5774. }
  5775. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5776. if (!c_sharedCtx)
  5777. #endif
  5778. {
  5779. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->c_ctx,
  5780. clientCertFile), WOLFSSL_SUCCESS);
  5781. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->c_ctx, clientKeyFile,
  5782. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5783. }
  5784. #ifdef HAVE_CRL
  5785. if (ctx->c_cb.crlPemFile != NULL) {
  5786. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx->c_ctx, WOLFSSL_CRL_CHECKALL),
  5787. WOLFSSL_SUCCESS);
  5788. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx->c_ctx, ctx->c_cb.crlPemFile,
  5789. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5790. }
  5791. #endif
  5792. if (ctx->c_ciphers != NULL) {
  5793. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->c_ctx, ctx->c_ciphers),
  5794. WOLFSSL_SUCCESS);
  5795. }
  5796. if (ctx->c_cb.ctx_ready != NULL) {
  5797. ExpectIntEQ(ctx->c_cb.ctx_ready(ctx->c_ctx), TEST_SUCCESS);
  5798. }
  5799. /********************************
  5800. * Create WOLFSSL_CTX for server.
  5801. ********************************/
  5802. if (ctx->s_ctx != NULL) {
  5803. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5804. s_sharedCtx = 1;
  5805. #endif
  5806. ctx->s_cb.isSharedCtx = 1;
  5807. }
  5808. else
  5809. {
  5810. WOLFSSL_METHOD* method = NULL;
  5811. if (ctx->s_cb.method != NULL) {
  5812. method = ctx->s_cb.method();
  5813. }
  5814. else {
  5815. method = wolfSSLv23_server_method();
  5816. }
  5817. ExpectNotNull(ctx->s_ctx = wolfSSL_CTX_new(method));
  5818. ctx->s_cb.isSharedCtx = 0;
  5819. }
  5820. if (!ctx->s_cb.ticNoInit && (ctx->s_ctx != NULL)) {
  5821. #if defined(HAVE_SESSION_TICKET) && \
  5822. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  5823. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  5824. OpenSSLTicketInit();
  5825. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx->s_ctx, myTicketEncCbOpenSSL);
  5826. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  5827. TicketInit();
  5828. wolfSSL_CTX_set_TicketEncCb(ctx->s_ctx, myTicketEncCb);
  5829. #endif
  5830. #endif
  5831. }
  5832. wolfSSL_SetIORecv(ctx->s_ctx, test_ssl_memio_read_cb);
  5833. wolfSSL_SetIOSend(ctx->s_ctx, test_ssl_memio_write_cb);
  5834. wolfSSL_CTX_set_verify(ctx->s_ctx, WOLFSSL_VERIFY_PEER |
  5835. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  5836. if (ctx->s_cb.caPemFile != NULL)
  5837. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx,
  5838. ctx->s_cb.caPemFile, 0), WOLFSSL_SUCCESS);
  5839. else
  5840. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx->s_ctx,
  5841. cliCertFile, 0), WOLFSSL_SUCCESS);
  5842. #ifdef WOLFSSL_ENCRYPTED_KEYS
  5843. wolfSSL_CTX_set_default_passwd_cb(ctx->s_ctx, PasswordCallBack);
  5844. #endif
  5845. if (ctx->s_cb.certPemFile != NULL) {
  5846. serverCertFile = ctx->s_cb.certPemFile;
  5847. }
  5848. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5849. if (!s_sharedCtx)
  5850. #endif
  5851. {
  5852. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx->s_ctx,
  5853. serverCertFile), WOLFSSL_SUCCESS);
  5854. }
  5855. if (ctx->s_cb.keyPemFile != NULL) {
  5856. serverKeyFile = ctx->s_cb.keyPemFile;
  5857. }
  5858. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5859. if (!s_sharedCtx)
  5860. #endif
  5861. {
  5862. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx->s_ctx, serverKeyFile,
  5863. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5864. }
  5865. if (ctx->s_ciphers != NULL) {
  5866. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx->s_ctx, ctx->s_ciphers),
  5867. WOLFSSL_SUCCESS);
  5868. }
  5869. if (ctx->s_cb.ctx_ready != NULL) {
  5870. ExpectIntEQ(ctx->s_cb.ctx_ready(ctx->s_ctx), TEST_SUCCESS);
  5871. }
  5872. /****************************
  5873. * Create WOLFSSL for client.
  5874. ****************************/
  5875. ExpectNotNull(ctx->c_ssl = wolfSSL_new(ctx->c_ctx));
  5876. wolfSSL_SetIOWriteCtx(ctx->c_ssl, ctx);
  5877. wolfSSL_SetIOReadCtx(ctx->c_ssl, ctx);
  5878. if (0
  5879. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5880. || c_sharedCtx
  5881. #endif
  5882. )
  5883. {
  5884. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->c_ssl,
  5885. clientCertFile), WOLFSSL_SUCCESS);
  5886. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->c_ssl, clientKeyFile,
  5887. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5888. }
  5889. if (ctx->c_cb.ssl_ready != NULL) {
  5890. ExpectIntEQ(ctx->c_cb.ssl_ready(ctx->c_ssl), TEST_SUCCESS);
  5891. }
  5892. /****************************
  5893. * Create WOLFSSL for server.
  5894. ****************************/
  5895. ExpectNotNull(ctx->s_ssl = wolfSSL_new(ctx->s_ctx));
  5896. wolfSSL_SetIOWriteCtx(ctx->s_ssl, ctx);
  5897. wolfSSL_SetIOReadCtx(ctx->s_ssl, ctx);
  5898. if (0
  5899. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  5900. || s_sharedCtx
  5901. #endif
  5902. )
  5903. {
  5904. ExpectIntEQ(wolfSSL_use_certificate_chain_file(ctx->s_ssl,
  5905. serverCertFile), WOLFSSL_SUCCESS);
  5906. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ctx->s_ssl, serverKeyFile,
  5907. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  5908. }
  5909. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  5910. wolfSSL_SetTmpDH_file(ctx->s_ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  5911. #elif !defined(NO_DH)
  5912. /* will repick suites with DHE, higher priority than PSK */
  5913. SetDH(ctx->s_ssl);
  5914. #endif
  5915. if (ctx->s_cb.ssl_ready != NULL) {
  5916. ExpectIntEQ(ctx->s_cb.ssl_ready(ctx->s_ssl), TEST_SUCCESS);
  5917. }
  5918. return EXPECT_RESULT();
  5919. }
  5920. static int test_ssl_memio_do_handshake(test_ssl_memio_ctx* ctx, int max_rounds,
  5921. int* rounds)
  5922. {
  5923. int handshake_complete = 0;
  5924. int hs_c = 0;
  5925. int hs_s = 0;
  5926. int failing_s = 0;
  5927. int failing_c = 0;
  5928. int ret;
  5929. int err;
  5930. if (rounds != NULL) {
  5931. *rounds = 0;
  5932. }
  5933. while ((!handshake_complete) && (max_rounds > 0)) {
  5934. if (!hs_c) {
  5935. wolfSSL_SetLoggingPrefix("client");
  5936. ret = wolfSSL_connect(ctx->c_ssl);
  5937. wolfSSL_SetLoggingPrefix(NULL);
  5938. if (ret == WOLFSSL_SUCCESS) {
  5939. hs_c = 1;
  5940. }
  5941. else {
  5942. err = wolfSSL_get_error(ctx->c_ssl, ret);
  5943. if (err != WOLFSSL_ERROR_WANT_READ &&
  5944. err != WOLFSSL_ERROR_WANT_WRITE) {
  5945. char buff[WOLFSSL_MAX_ERROR_SZ];
  5946. fprintf(stderr, "error = %d, %s\n", err,
  5947. wolfSSL_ERR_error_string(err, buff));
  5948. failing_c = 1;
  5949. hs_c = 1;
  5950. if (failing_c && failing_s) {
  5951. break;
  5952. }
  5953. }
  5954. }
  5955. }
  5956. if (!hs_s) {
  5957. wolfSSL_SetLoggingPrefix("server");
  5958. ret = wolfSSL_accept(ctx->s_ssl);
  5959. wolfSSL_SetLoggingPrefix(NULL);
  5960. if (ret == WOLFSSL_SUCCESS) {
  5961. hs_s = 1;
  5962. }
  5963. else {
  5964. err = wolfSSL_get_error(ctx->s_ssl, ret);
  5965. if (err != WOLFSSL_ERROR_WANT_READ &&
  5966. err != WOLFSSL_ERROR_WANT_WRITE) {
  5967. char buff[WOLFSSL_MAX_ERROR_SZ];
  5968. fprintf(stderr, "error = %d, %s\n", err,
  5969. wolfSSL_ERR_error_string(err, buff));
  5970. failing_s = 1;
  5971. hs_s = 1;
  5972. if (failing_c && failing_s) {
  5973. break;
  5974. }
  5975. }
  5976. }
  5977. }
  5978. handshake_complete = hs_c && hs_s;
  5979. max_rounds--;
  5980. if (rounds != NULL) {
  5981. *rounds += 1;
  5982. }
  5983. }
  5984. if (!handshake_complete || failing_c || failing_s) {
  5985. return TEST_FAIL;
  5986. }
  5987. return TEST_SUCCESS;
  5988. }
  5989. static int test_ssl_memio_read_write(test_ssl_memio_ctx* ctx)
  5990. {
  5991. EXPECT_DECLS;
  5992. char input[1024];
  5993. int idx = 0;
  5994. const char* msg_c = "hello wolfssl!";
  5995. int msglen_c = (int)XSTRLEN(msg_c);
  5996. const char* msg_s = "I hear you fa shizzle!";
  5997. int msglen_s = (int)XSTRLEN(msg_s);
  5998. if (ctx->c_msg != NULL) {
  5999. msg_c = ctx->c_msg;
  6000. msglen_c = ctx->c_msglen;
  6001. }
  6002. if (ctx->s_msg != NULL) {
  6003. msg_s = ctx->s_msg;
  6004. msglen_s = ctx->s_msglen;
  6005. }
  6006. wolfSSL_SetLoggingPrefix("client");
  6007. ExpectIntEQ(wolfSSL_write(ctx->c_ssl, msg_c, msglen_c), msglen_c);
  6008. wolfSSL_SetLoggingPrefix("server");
  6009. ExpectIntGT(idx = wolfSSL_read(ctx->s_ssl, input, sizeof(input) - 1), 0);
  6010. if (idx >= 0) {
  6011. input[idx] = '\0';
  6012. }
  6013. ExpectIntGT(fprintf(stderr, "Client message: %s\n", input), 0);
  6014. ExpectIntEQ(wolfSSL_write(ctx->s_ssl, msg_s, msglen_s), msglen_s);
  6015. ctx->s_cb.return_code = EXPECT_RESULT();
  6016. wolfSSL_SetLoggingPrefix("client");
  6017. ExpectIntGT(idx = wolfSSL_read(ctx->c_ssl, input, sizeof(input) - 1), 0);
  6018. wolfSSL_SetLoggingPrefix(NULL);
  6019. if (idx >= 0) {
  6020. input[idx] = '\0';
  6021. }
  6022. ExpectIntGT(fprintf(stderr, "Server response: %s\n", input), 0);
  6023. ctx->c_cb.return_code = EXPECT_RESULT();
  6024. if (ctx->c_cb.on_result != NULL) {
  6025. ExpectIntEQ(ctx->c_cb.on_result(ctx->c_ssl), TEST_SUCCESS);
  6026. }
  6027. if (ctx->s_cb.on_result != NULL) {
  6028. ExpectIntEQ(ctx->s_cb.on_result(ctx->s_ssl), TEST_SUCCESS);
  6029. }
  6030. return EXPECT_RESULT();
  6031. }
  6032. static void test_ssl_memio_cleanup(test_ssl_memio_ctx* ctx)
  6033. {
  6034. ctx->c_cb.last_err = wolfSSL_get_error(ctx->c_ssl, 0);
  6035. ctx->s_cb.last_err = wolfSSL_get_error(ctx->s_ssl, 0);
  6036. if (ctx->c_cb.on_cleanup != NULL) {
  6037. ctx->c_cb.on_cleanup(ctx->c_ssl);
  6038. }
  6039. if (ctx->s_cb.on_cleanup != NULL) {
  6040. ctx->s_cb.on_cleanup(ctx->s_ssl);
  6041. }
  6042. wolfSSL_shutdown(ctx->s_ssl);
  6043. wolfSSL_shutdown(ctx->c_ssl);
  6044. wolfSSL_free(ctx->s_ssl);
  6045. wolfSSL_free(ctx->c_ssl);
  6046. if (ctx->c_cb.on_ctx_cleanup != NULL) {
  6047. ctx->c_cb.on_ctx_cleanup(ctx->c_ctx);
  6048. }
  6049. if (!ctx->c_cb.isSharedCtx) {
  6050. wolfSSL_CTX_free(ctx->c_ctx);
  6051. ctx->c_ctx = NULL;
  6052. }
  6053. if (ctx->s_cb.on_ctx_cleanup != NULL) {
  6054. ctx->s_cb.on_ctx_cleanup(ctx->s_ctx);
  6055. }
  6056. if (!ctx->s_cb.isSharedCtx) {
  6057. wolfSSL_CTX_free(ctx->s_ctx);
  6058. ctx->s_ctx = NULL;
  6059. }
  6060. if (!ctx->s_cb.ticNoInit) {
  6061. #if defined(HAVE_SESSION_TICKET) && \
  6062. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6063. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6064. OpenSSLTicketCleanup();
  6065. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6066. TicketCleanup();
  6067. #endif
  6068. #endif
  6069. }
  6070. }
  6071. int test_wolfSSL_client_server_nofail_memio(test_ssl_cbf* client_cb,
  6072. test_ssl_cbf* server_cb, cbType client_on_handshake)
  6073. {
  6074. EXPECT_DECLS;
  6075. struct test_ssl_memio_ctx test_ctx;
  6076. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6077. size_t msg_len;
  6078. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  6079. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  6080. XMEMCPY(&test_ctx.c_cb, client_cb, sizeof(test_ssl_cbf));
  6081. XMEMCPY(&test_ctx.s_cb, server_cb, sizeof(test_ssl_cbf));
  6082. test_ctx.c_ctx = client_cb->ctx;
  6083. test_ctx.s_ctx = server_cb->ctx;
  6084. test_ctx.c_cb.return_code = TEST_FAIL;
  6085. test_ctx.s_cb.return_code = TEST_FAIL;
  6086. ExpectIntEQ(test_ssl_memio_setup(&test_ctx), TEST_SUCCESS);
  6087. ExpectIntEQ(test_ssl_memio_do_handshake(&test_ctx, 10, NULL), TEST_SUCCESS);
  6088. if (client_on_handshake != NULL) {
  6089. ExpectIntEQ(client_on_handshake(test_ctx.c_ctx, test_ctx.c_ssl),
  6090. TEST_SUCCESS);
  6091. }
  6092. if (client_cb->on_handshake != NULL) {
  6093. ExpectIntEQ(client_cb->on_handshake(&test_ctx.c_ctx, &test_ctx.c_ssl),
  6094. TEST_SUCCESS);
  6095. }
  6096. if (server_cb->on_handshake != NULL) {
  6097. ExpectIntEQ(server_cb->on_handshake(&test_ctx.s_ctx, &test_ctx.s_ssl),
  6098. TEST_SUCCESS);
  6099. }
  6100. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6101. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  6102. msg_len = wolfSSL_get_peer_finished(test_ctx.s_ssl, server_side_msg2,
  6103. MD_MAX_SIZE);
  6104. ExpectIntGE(msg_len, 0);
  6105. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  6106. msg_len = wolfSSL_get_finished(test_ctx.s_ssl, server_side_msg1,
  6107. MD_MAX_SIZE);
  6108. ExpectIntGE(msg_len, 0);
  6109. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  6110. ExpectIntEQ(test_ssl_memio_read_write(&test_ctx), TEST_SUCCESS);
  6111. test_ssl_memio_cleanup(&test_ctx);
  6112. client_cb->return_code = test_ctx.c_cb.return_code;
  6113. client_cb->last_err = test_ctx.c_cb.last_err;
  6114. server_cb->return_code = test_ctx.s_cb.return_code;
  6115. server_cb->last_err = test_ctx.s_cb.last_err;
  6116. return EXPECT_RESULT();
  6117. }
  6118. #endif
  6119. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  6120. #ifdef WOLFSSL_SESSION_EXPORT
  6121. #ifdef WOLFSSL_DTLS
  6122. /* set up function for sending session information */
  6123. static int test_export(WOLFSSL* inSsl, byte* buf, word32 sz, void* userCtx)
  6124. {
  6125. WOLFSSL_CTX* ctx = NULL;
  6126. WOLFSSL* ssl = NULL;
  6127. AssertNotNull(inSsl);
  6128. AssertNotNull(buf);
  6129. AssertIntNE(0, sz);
  6130. /* Set ctx to DTLS 1.2 */
  6131. ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method());
  6132. AssertNotNull(ctx);
  6133. ssl = wolfSSL_new(ctx);
  6134. AssertNotNull(ssl);
  6135. AssertIntGE(wolfSSL_dtls_import(ssl, buf, sz), 0);
  6136. wolfSSL_free(ssl);
  6137. wolfSSL_CTX_free(ctx);
  6138. (void)userCtx;
  6139. return 0;
  6140. }
  6141. #endif
  6142. /* returns negative value on fail and positive (including 0) on success */
  6143. static int nonblocking_accept_read(void* args, WOLFSSL* ssl, SOCKET_T* sockfd)
  6144. {
  6145. int ret, err, loop_count, count, timeout = 10;
  6146. char msg[] = "I hear you fa shizzle!";
  6147. char input[1024];
  6148. loop_count = ((func_args*)args)->argc;
  6149. #ifdef WOLFSSL_ASYNC_CRYPT
  6150. err = 0; /* Reset error */
  6151. #endif
  6152. do {
  6153. #ifdef WOLFSSL_ASYNC_CRYPT
  6154. if (err == WC_PENDING_E) {
  6155. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6156. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6157. }
  6158. #endif
  6159. ret = wolfSSL_accept(ssl);
  6160. err = wolfSSL_get_error(ssl, 0);
  6161. if (err == WOLFSSL_ERROR_WANT_READ ||
  6162. err == WOLFSSL_ERROR_WANT_WRITE) {
  6163. int select_ret;
  6164. err = WC_PENDING_E;
  6165. select_ret = tcp_select(*sockfd, timeout);
  6166. if (select_ret == TEST_TIMEOUT) {
  6167. return WOLFSSL_FATAL_ERROR;
  6168. }
  6169. }
  6170. } while (err == WC_PENDING_E);
  6171. if (ret != WOLFSSL_SUCCESS) {
  6172. char buff[WOLFSSL_MAX_ERROR_SZ];
  6173. fprintf(stderr, "error = %d, %s\n", err,
  6174. wolfSSL_ERR_error_string(err, buff));
  6175. return ret;
  6176. }
  6177. for (count = 0; count < loop_count; count++) {
  6178. int select_ret;
  6179. select_ret = tcp_select(*sockfd, timeout);
  6180. if (select_ret == TEST_TIMEOUT) {
  6181. ret = WOLFSSL_FATAL_ERROR;
  6182. break;
  6183. }
  6184. do {
  6185. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  6186. if (ret > 0) {
  6187. input[ret] = '\0';
  6188. fprintf(stderr, "Client message: %s\n", input);
  6189. }
  6190. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  6191. do {
  6192. if ((ret = wolfSSL_write(ssl, msg, sizeof(msg))) != sizeof(msg)) {
  6193. return WOLFSSL_FATAL_ERROR;
  6194. }
  6195. err = wolfSSL_get_error(ssl, ret);
  6196. } while (err == WOLFSSL_ERROR_WANT_READ && ret != WOLFSSL_SUCCESS);
  6197. }
  6198. return ret;
  6199. }
  6200. #endif /* WOLFSSL_SESSION_EXPORT */
  6201. static THREAD_RETURN WOLFSSL_THREAD test_server_nofail(void* args)
  6202. {
  6203. SOCKET_T sockfd = 0;
  6204. SOCKET_T clientfd = 0;
  6205. word16 port;
  6206. callback_functions* cbf;
  6207. WOLFSSL_CTX* ctx = NULL;
  6208. WOLFSSL* ssl = NULL;
  6209. func_args* opts = (func_args*)args;
  6210. char msg[] = "I hear you fa shizzle!";
  6211. char input[1024];
  6212. int idx;
  6213. int ret, err = 0;
  6214. int sharedCtx = 0;
  6215. int doUdp = 0;
  6216. SOCKADDR_IN_T cliAddr;
  6217. socklen_t cliLen;
  6218. const char* certFile = svrCertFile;
  6219. const char* keyFile = svrKeyFile;
  6220. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6221. size_t msg_len = 0;
  6222. #endif
  6223. wolfSSL_SetLoggingPrefix("server");
  6224. #ifdef WOLFSSL_TIRTOS
  6225. fdOpenSession(Task_self());
  6226. #endif
  6227. opts->return_code = TEST_FAIL;
  6228. cbf = opts->callbacks;
  6229. if (cbf != NULL && cbf->ctx) {
  6230. ctx = cbf->ctx;
  6231. sharedCtx = 1;
  6232. }
  6233. else
  6234. {
  6235. WOLFSSL_METHOD* method = NULL;
  6236. if (cbf != NULL && cbf->method != NULL) {
  6237. method = cbf->method();
  6238. }
  6239. else {
  6240. method = wolfSSLv23_server_method();
  6241. }
  6242. ctx = wolfSSL_CTX_new(method);
  6243. }
  6244. if (ctx == NULL) {
  6245. /* Release the wait for TCP ready. */
  6246. signal_ready(opts->signal);
  6247. goto done;
  6248. }
  6249. if (cbf == NULL || !cbf->ticNoInit) {
  6250. #if defined(HAVE_SESSION_TICKET) && \
  6251. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6252. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6253. OpenSSLTicketInit();
  6254. wolfSSL_CTX_set_tlsext_ticket_key_cb(ctx, myTicketEncCbOpenSSL);
  6255. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6256. TicketInit();
  6257. wolfSSL_CTX_set_TicketEncCb(ctx, myTicketEncCb);
  6258. #endif
  6259. #endif
  6260. }
  6261. #if defined(USE_WINDOWS_API)
  6262. port = opts->signal->port;
  6263. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6264. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6265. /* Let tcp_listen assign port */
  6266. port = 0;
  6267. #else
  6268. /* Use default port */
  6269. port = wolfSSLPort;
  6270. #endif
  6271. if (cbf != NULL)
  6272. doUdp = cbf->doUdp;
  6273. /* do it here to detect failure */
  6274. tcp_accept(
  6275. &sockfd, &clientfd, opts, port, 0, doUdp, 0, 0, 1, 0, 0);
  6276. if (doUdp) {
  6277. cliLen = sizeof(cliAddr);
  6278. idx = (int)recvfrom(sockfd, input, sizeof(input), MSG_PEEK,
  6279. (struct sockaddr*)&cliAddr, &cliLen);
  6280. AssertIntGT(idx, 0);
  6281. }
  6282. else {
  6283. CloseSocket(sockfd);
  6284. }
  6285. wolfSSL_CTX_set_verify(ctx,
  6286. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6287. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6288. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6289. #endif
  6290. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  6291. != WOLFSSL_SUCCESS) {
  6292. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6293. goto done;
  6294. }
  6295. if (cbf != NULL && cbf->certPemFile != NULL)
  6296. certFile = cbf->certPemFile;
  6297. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6298. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, certFile,
  6299. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6300. #else
  6301. if (wolfSSL_CTX_use_certificate_file(ctx, certFile,
  6302. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6303. #endif
  6304. /*err_sys("can't load server cert chain file, "
  6305. "Please run from wolfSSL home dir");*/
  6306. goto done;
  6307. }
  6308. if (cbf != NULL && cbf->keyPemFile != NULL)
  6309. keyFile = cbf->keyPemFile;
  6310. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6311. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  6312. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6313. #else
  6314. if (wolfSSL_CTX_use_PrivateKey_file(ctx, keyFile,
  6315. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6316. #endif
  6317. /*err_sys("can't load server key file, "
  6318. "Please run from wolfSSL home dir");*/
  6319. goto done;
  6320. }
  6321. #ifdef HAVE_CRL
  6322. if (cbf != NULL && cbf->crlPemFile != NULL) {
  6323. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  6324. goto done;
  6325. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  6326. != WOLFSSL_SUCCESS)
  6327. goto done;
  6328. }
  6329. #endif
  6330. /* call ctx setup callback */
  6331. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6332. cbf->ctx_ready(ctx);
  6333. }
  6334. ssl = wolfSSL_new(ctx);
  6335. if (ssl == NULL) {
  6336. goto done;
  6337. }
  6338. if (doUdp) {
  6339. err = wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  6340. if (err != WOLFSSL_SUCCESS)
  6341. goto done;
  6342. }
  6343. #ifdef WOLFSSL_SESSION_EXPORT
  6344. /* only add in more complex nonblocking case with session export tests */
  6345. if (args && opts->argc > 0) {
  6346. /* set as nonblock and time out for waiting on read/write */
  6347. tcp_set_nonblocking(&clientfd);
  6348. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  6349. }
  6350. #endif
  6351. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6352. if (sharedCtx && wolfSSL_use_certificate_file(ssl, certFile,
  6353. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6354. #else
  6355. if (wolfSSL_use_certificate_file(ssl, certFile,
  6356. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6357. #endif
  6358. /*err_sys("can't load server cert chain file, "
  6359. "Please run from wolfSSL home dir");*/
  6360. goto done;
  6361. }
  6362. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6363. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, keyFile,
  6364. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6365. #else
  6366. if (wolfSSL_use_PrivateKey_file(ssl, keyFile,
  6367. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6368. #endif
  6369. /*err_sys("can't load server key file, "
  6370. "Please run from wolfSSL home dir");*/
  6371. goto done;
  6372. }
  6373. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  6374. /*err_sys("SSL_set_fd failed");*/
  6375. goto done;
  6376. }
  6377. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6378. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6379. #elif !defined(NO_DH)
  6380. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6381. #endif
  6382. /* call ssl setup callback */
  6383. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6384. cbf->ssl_ready(ssl);
  6385. }
  6386. #ifdef WOLFSSL_SESSION_EXPORT
  6387. /* only add in more complex nonblocking case with session export tests */
  6388. if (opts->argc > 0) {
  6389. ret = nonblocking_accept_read(args, ssl, &clientfd);
  6390. if (ret >= 0) {
  6391. opts->return_code = TEST_SUCCESS;
  6392. }
  6393. #ifdef WOLFSSL_TIRTOS
  6394. Task_yield();
  6395. #endif
  6396. goto done;
  6397. }
  6398. #endif
  6399. #ifdef WOLFSSL_ASYNC_CRYPT
  6400. err = 0; /* Reset error */
  6401. #endif
  6402. do {
  6403. #ifdef WOLFSSL_ASYNC_CRYPT
  6404. if (err == WC_PENDING_E) {
  6405. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6406. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6407. }
  6408. #endif
  6409. ret = wolfSSL_negotiate(ssl);
  6410. err = wolfSSL_get_error(ssl, 0);
  6411. } while (err == WC_PENDING_E);
  6412. if (ret != WOLFSSL_SUCCESS) {
  6413. char buff[WOLFSSL_MAX_ERROR_SZ];
  6414. fprintf(stderr, "error = %d, %s\n", err,
  6415. wolfSSL_ERR_error_string(err, buff));
  6416. /*err_sys("SSL_accept failed");*/
  6417. goto done;
  6418. }
  6419. #ifdef WOLFSSL_HAVE_TLS_UNIQUE
  6420. XMEMSET(server_side_msg2, 0, MD_MAX_SIZE);
  6421. msg_len = wolfSSL_get_peer_finished(ssl, server_side_msg2, MD_MAX_SIZE);
  6422. AssertIntGE(msg_len, 0);
  6423. XMEMSET(server_side_msg1, 0, MD_MAX_SIZE);
  6424. msg_len = wolfSSL_get_finished(ssl, server_side_msg1, MD_MAX_SIZE);
  6425. AssertIntGE(msg_len, 0);
  6426. #endif /* WOLFSSL_HAVE_TLS_UNIQUE */
  6427. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6428. if (idx > 0) {
  6429. input[idx] = '\0';
  6430. fprintf(stderr, "Client message: %s\n", input);
  6431. }
  6432. else if (idx < 0) {
  6433. goto done;
  6434. }
  6435. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6436. /*err_sys("SSL_write failed");*/
  6437. goto done;
  6438. }
  6439. if (cbf != NULL && cbf->on_result != NULL)
  6440. cbf->on_result(ssl);
  6441. #ifdef WOLFSSL_TIRTOS
  6442. Task_yield();
  6443. #endif
  6444. opts->return_code = TEST_SUCCESS;
  6445. done:
  6446. if (cbf != NULL)
  6447. cbf->last_err = err;
  6448. if (cbf != NULL && cbf->on_cleanup != NULL)
  6449. cbf->on_cleanup(ssl);
  6450. wolfSSL_shutdown(ssl);
  6451. wolfSSL_free(ssl);
  6452. if (!sharedCtx)
  6453. wolfSSL_CTX_free(ctx);
  6454. CloseSocket(clientfd);
  6455. #ifdef WOLFSSL_TIRTOS
  6456. fdCloseSession(Task_self());
  6457. #endif
  6458. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6459. && defined(HAVE_THREAD_LS)
  6460. wc_ecc_fp_free(); /* free per thread cache */
  6461. #endif
  6462. if (cbf == NULL || !cbf->ticNoInit) {
  6463. #if defined(HAVE_SESSION_TICKET) && \
  6464. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  6465. #if defined(OPENSSL_EXTRA) && defined(HAVE_AES_CBC)
  6466. OpenSSLTicketCleanup();
  6467. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  6468. TicketCleanup();
  6469. #endif
  6470. #endif
  6471. }
  6472. wolfSSL_SetLoggingPrefix(NULL);
  6473. WOLFSSL_RETURN_FROM_THREAD(0);
  6474. }
  6475. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6476. !defined(WOLFSSL_NO_TLS12)
  6477. static THREAD_RETURN WOLFSSL_THREAD test_server_loop(void* args)
  6478. {
  6479. SOCKET_T sockfd;
  6480. SOCKET_T clientfd = -1;
  6481. word16 port;
  6482. callback_functions* cbf;
  6483. WOLFSSL_CTX* ctx = 0;
  6484. WOLFSSL* ssl = 0;
  6485. char msg[] = "I hear you fa shizzle!";
  6486. char input[1024];
  6487. int idx;
  6488. int ret, err = 0;
  6489. int sharedCtx = 0;
  6490. func_args* opts = (func_args*)args;
  6491. int loop_count = opts->argc;
  6492. int count = 0;
  6493. #ifdef WOLFSSL_TIRTOS
  6494. fdOpenSession(Task_self());
  6495. #endif
  6496. opts->return_code = TEST_FAIL;
  6497. cbf = opts->callbacks;
  6498. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6499. if (cbf != NULL && cbf->ctx) {
  6500. ctx = cbf->ctx;
  6501. sharedCtx = 1;
  6502. }
  6503. else
  6504. #endif
  6505. {
  6506. WOLFSSL_METHOD* method = NULL;
  6507. if (cbf != NULL && cbf->method != NULL) {
  6508. method = cbf->method();
  6509. }
  6510. else {
  6511. method = wolfSSLv23_server_method();
  6512. }
  6513. ctx = wolfSSL_CTX_new(method);
  6514. }
  6515. #if defined(USE_WINDOWS_API)
  6516. port = opts->signal->port;
  6517. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  6518. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  6519. /* Let tcp_listen assign port */
  6520. port = 0;
  6521. #else
  6522. /* Use default port */
  6523. port = wolfSSLPort;
  6524. #endif
  6525. wolfSSL_CTX_set_verify(ctx,
  6526. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  6527. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6528. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6529. #endif
  6530. if (wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0)
  6531. != WOLFSSL_SUCCESS) {
  6532. /*err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6533. /* Release the wait for TCP ready. */
  6534. signal_ready(opts->signal);
  6535. goto done;
  6536. }
  6537. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  6538. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6539. /*err_sys("can't load server cert chain file, "
  6540. "Please run from wolfSSL home dir");*/
  6541. /* Release the wait for TCP ready. */
  6542. signal_ready(opts->signal);
  6543. goto done;
  6544. }
  6545. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  6546. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6547. /*err_sys("can't load server key file, "
  6548. "Please run from wolfSSL home dir");*/
  6549. /* Release the wait for TCP ready. */
  6550. signal_ready(opts->signal);
  6551. goto done;
  6552. }
  6553. /* call ctx setup callback */
  6554. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6555. cbf->ctx_ready(ctx);
  6556. }
  6557. while (count != loop_count) {
  6558. ssl = wolfSSL_new(ctx);
  6559. if (ssl == NULL) {
  6560. signal_ready(opts->signal);
  6561. goto done;
  6562. }
  6563. if (sharedCtx && wolfSSL_use_certificate_file(ssl, svrCertFile,
  6564. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6565. /*err_sys("can't load server cert chain file, "
  6566. "Please run from wolfSSL home dir");*/
  6567. /* Release the wait for TCP ready. */
  6568. signal_ready(opts->signal);
  6569. goto done;
  6570. }
  6571. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  6572. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6573. /*err_sys("can't load server key file, "
  6574. "Please run from wolfSSL home dir");*/
  6575. /* Release the wait for TCP ready. */
  6576. signal_ready(opts->signal);
  6577. goto done;
  6578. }
  6579. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  6580. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  6581. #elif !defined(NO_DH)
  6582. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  6583. #endif
  6584. /* call ssl setup callback */
  6585. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6586. cbf->ssl_ready(ssl);
  6587. }
  6588. /* do it here to detect failure */
  6589. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0,
  6590. 0);
  6591. CloseSocket(sockfd);
  6592. if (wolfSSL_set_fd(ssl, clientfd) != WOLFSSL_SUCCESS) {
  6593. /*err_sys("SSL_set_fd failed");*/
  6594. goto done;
  6595. }
  6596. #ifdef WOLFSSL_ASYNC_CRYPT
  6597. err = 0; /* Reset error */
  6598. #endif
  6599. do {
  6600. #ifdef WOLFSSL_ASYNC_CRYPT
  6601. if (err == WC_PENDING_E) {
  6602. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6603. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6604. }
  6605. #endif
  6606. ret = wolfSSL_accept(ssl);
  6607. err = wolfSSL_get_error(ssl, 0);
  6608. } while (err == WC_PENDING_E);
  6609. if (ret != WOLFSSL_SUCCESS) {
  6610. char buff[WOLFSSL_MAX_ERROR_SZ];
  6611. fprintf(stderr, "error = %d, %s\n", err,
  6612. wolfSSL_ERR_error_string(err, buff));
  6613. /*err_sys("SSL_accept failed");*/
  6614. goto done;
  6615. }
  6616. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  6617. if (idx > 0) {
  6618. input[idx] = '\0';
  6619. fprintf(stderr, "Client message: %s\n", input);
  6620. }
  6621. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  6622. /*err_sys("SSL_write failed");*/
  6623. goto done;
  6624. }
  6625. /* free ssl for this connection */
  6626. wolfSSL_shutdown(ssl);
  6627. wolfSSL_free(ssl); ssl = NULL;
  6628. CloseSocket(clientfd);
  6629. clientfd = -1;
  6630. count++;
  6631. }
  6632. #ifdef WOLFSSL_TIRTOS
  6633. Task_yield();
  6634. #endif
  6635. opts->return_code = TEST_SUCCESS;
  6636. done:
  6637. if (ssl != NULL) {
  6638. wolfSSL_shutdown(ssl);
  6639. wolfSSL_free(ssl);
  6640. }
  6641. if (!sharedCtx)
  6642. wolfSSL_CTX_free(ctx);
  6643. if (clientfd >= 0)
  6644. CloseSocket(clientfd);
  6645. #ifdef WOLFSSL_TIRTOS
  6646. fdCloseSession(Task_self());
  6647. #endif
  6648. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6649. && defined(HAVE_THREAD_LS)
  6650. wc_ecc_fp_free(); /* free per thread cache */
  6651. #endif
  6652. WOLFSSL_RETURN_FROM_THREAD(0);
  6653. }
  6654. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && !defined(WOLFSSL_TLS13) */
  6655. static int test_client_nofail(void* args, cbType cb)
  6656. {
  6657. #if !defined(NO_WOLFSSL_CLIENT)
  6658. SOCKET_T sockfd = 0;
  6659. callback_functions* cbf;
  6660. WOLFSSL_CTX* ctx = 0;
  6661. WOLFSSL* ssl = 0;
  6662. WOLFSSL_CIPHER* cipher;
  6663. char msg[64] = "hello wolfssl!";
  6664. char reply[1024];
  6665. int input;
  6666. int msgSz = (int)XSTRLEN(msg);
  6667. int ret, err = 0;
  6668. int cipherSuite;
  6669. int sharedCtx = 0;
  6670. int doUdp = 0;
  6671. const char* cipherName1, *cipherName2;
  6672. wolfSSL_SetLoggingPrefix("client");
  6673. #ifdef WOLFSSL_TIRTOS
  6674. fdOpenSession(Task_self());
  6675. #endif
  6676. ((func_args*)args)->return_code = TEST_FAIL;
  6677. cbf = ((func_args*)args)->callbacks;
  6678. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6679. if (cbf != NULL && cbf->ctx) {
  6680. ctx = cbf->ctx;
  6681. sharedCtx = cbf->isSharedCtx;
  6682. }
  6683. else
  6684. #endif
  6685. {
  6686. WOLFSSL_METHOD* method = NULL;
  6687. if (cbf != NULL && cbf->method != NULL) {
  6688. method = cbf->method();
  6689. }
  6690. else {
  6691. method = wolfSSLv23_client_method();
  6692. }
  6693. ctx = wolfSSL_CTX_new(method);
  6694. }
  6695. if (cbf != NULL)
  6696. doUdp = cbf->doUdp;
  6697. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6698. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6699. #endif
  6700. /* Do connect here so server detects failures */
  6701. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6702. doUdp, 0, NULL);
  6703. /* Connect the socket so that we don't have to set the peer later on */
  6704. if (doUdp)
  6705. udp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port);
  6706. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
  6707. {
  6708. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6709. goto done;
  6710. }
  6711. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6712. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6713. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6714. #else
  6715. if (wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6716. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6717. #endif
  6718. /*err_sys("can't load client cert file, "
  6719. "Please run from wolfSSL home dir");*/
  6720. goto done;
  6721. }
  6722. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6723. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6724. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6725. #else
  6726. if (wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6727. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6728. #endif
  6729. /*err_sys("can't load client key file, "
  6730. "Please run from wolfSSL home dir");*/
  6731. goto done;
  6732. }
  6733. #ifdef HAVE_CRL
  6734. if (cbf != NULL && cbf->crlPemFile != NULL) {
  6735. if (wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL) != WOLFSSL_SUCCESS)
  6736. goto done;
  6737. if (wolfSSL_CTX_LoadCRLFile(ctx, cbf->crlPemFile, WOLFSSL_FILETYPE_PEM)
  6738. != WOLFSSL_SUCCESS)
  6739. goto done;
  6740. }
  6741. #endif
  6742. /* call ctx setup callback */
  6743. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6744. cbf->ctx_ready(ctx);
  6745. }
  6746. ssl = wolfSSL_new(ctx);
  6747. if (ssl == NULL) {
  6748. goto done;
  6749. }
  6750. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6751. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  6752. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6753. #else
  6754. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  6755. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6756. #endif
  6757. /*err_sys("can't load client cert file, "
  6758. "Please run from wolfSSL home dir");*/
  6759. goto done;
  6760. }
  6761. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6762. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6763. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6764. #else
  6765. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6766. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6767. #endif
  6768. /*err_sys("can't load client key file, "
  6769. "Please run from wolfSSL home dir");*/
  6770. goto done;
  6771. }
  6772. if (!doUdp) {
  6773. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6774. /*err_sys("SSL_set_fd failed");*/
  6775. goto done;
  6776. }
  6777. }
  6778. else {
  6779. #ifdef WOLFSSL_DTLS
  6780. if (wolfSSL_set_dtls_fd_connected(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6781. /*err_sys("SSL_set_fd failed");*/
  6782. goto done;
  6783. }
  6784. #else
  6785. goto done;
  6786. #endif
  6787. }
  6788. /* call ssl setup callback */
  6789. if (cbf != NULL && cbf->ssl_ready != NULL) {
  6790. cbf->ssl_ready(ssl);
  6791. }
  6792. #ifdef WOLFSSL_ASYNC_CRYPT
  6793. err = 0; /* Reset error */
  6794. #endif
  6795. do {
  6796. #ifdef WOLFSSL_ASYNC_CRYPT
  6797. if (err == WC_PENDING_E) {
  6798. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  6799. if (ret < 0) { break; } else if (ret == 0) { continue; }
  6800. }
  6801. #endif
  6802. ret = wolfSSL_negotiate(ssl);
  6803. err = wolfSSL_get_error(ssl, 0);
  6804. } while (err == WC_PENDING_E);
  6805. if (ret != WOLFSSL_SUCCESS) {
  6806. char buff[WOLFSSL_MAX_ERROR_SZ];
  6807. fprintf(stderr, "error = %d, %s\n", err,
  6808. wolfSSL_ERR_error_string(err, buff));
  6809. /*err_sys("SSL_connect failed");*/
  6810. goto done;
  6811. }
  6812. /* test the various get cipher methods */
  6813. /* Internal cipher suite names */
  6814. cipherSuite = wolfSSL_get_current_cipher_suite(ssl);
  6815. cipherName1 = wolfSSL_get_cipher_name(ssl);
  6816. cipherName2 = wolfSSL_get_cipher_name_from_suite(
  6817. (cipherSuite >> 8), cipherSuite & 0xFF);
  6818. AssertStrEQ(cipherName1, cipherName2);
  6819. /* IANA Cipher Suites Names */
  6820. /* Unless WOLFSSL_CIPHER_INTERNALNAME or NO_ERROR_STRINGS,
  6821. then it's the internal cipher suite name */
  6822. cipher = wolfSSL_get_current_cipher(ssl);
  6823. cipherName1 = wolfSSL_CIPHER_get_name(cipher);
  6824. cipherName2 = wolfSSL_get_cipher(ssl);
  6825. AssertStrEQ(cipherName1, cipherName2);
  6826. #if !defined(WOLFSSL_CIPHER_INTERNALNAME) && !defined(NO_ERROR_STRINGS) && \
  6827. !defined(WOLFSSL_QT)
  6828. cipherName1 = wolfSSL_get_cipher_name_iana_from_suite(
  6829. (cipherSuite >> 8), cipherSuite & 0xFF);
  6830. AssertStrEQ(cipherName1, cipherName2);
  6831. #endif
  6832. if (cb != NULL)
  6833. (cb)(ctx, ssl);
  6834. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  6835. /*err_sys("SSL_write failed");*/
  6836. goto done;
  6837. }
  6838. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  6839. if (input > 0) {
  6840. reply[input] = '\0';
  6841. fprintf(stderr, "Server response: %s\n", reply);
  6842. }
  6843. if (cbf != NULL && cbf->on_result != NULL)
  6844. cbf->on_result(ssl);
  6845. ((func_args*)args)->return_code = TEST_SUCCESS;
  6846. done:
  6847. if (cbf != NULL)
  6848. cbf->last_err = err;
  6849. if (cbf != NULL && cbf->on_cleanup != NULL)
  6850. cbf->on_cleanup(ssl);
  6851. wolfSSL_free(ssl);
  6852. if (!sharedCtx)
  6853. wolfSSL_CTX_free(ctx);
  6854. CloseSocket(sockfd);
  6855. #ifdef WOLFSSL_TIRTOS
  6856. fdCloseSession(Task_self());
  6857. #endif
  6858. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  6859. && defined(HAVE_THREAD_LS)
  6860. wc_ecc_fp_free(); /* free per thread cache */
  6861. #endif
  6862. #else
  6863. (void)args;
  6864. (void)cb;
  6865. #endif /* !NO_WOLFSSL_CLIENT */
  6866. wolfSSL_SetLoggingPrefix(NULL);
  6867. return 0;
  6868. }
  6869. void test_wolfSSL_client_server_nofail_ex(callback_functions* client_cb,
  6870. callback_functions* server_cb, cbType client_on_handshake)
  6871. {
  6872. func_args client_args;
  6873. func_args server_args;
  6874. tcp_ready ready;
  6875. THREAD_TYPE serverThread;
  6876. XMEMSET(&client_args, 0, sizeof(func_args));
  6877. XMEMSET(&server_args, 0, sizeof(func_args));
  6878. #ifdef WOLFSSL_TIRTOS
  6879. fdOpenSession(Task_self());
  6880. #endif
  6881. StartTCP();
  6882. InitTcpReady(&ready);
  6883. #if defined(USE_WINDOWS_API)
  6884. /* use RNG to get random port if using windows */
  6885. ready.port = GetRandomPort();
  6886. #endif
  6887. server_args.signal = &ready;
  6888. server_args.callbacks = server_cb;
  6889. client_args.signal = &ready;
  6890. client_args.callbacks = client_cb;
  6891. start_thread(test_server_nofail, &server_args, &serverThread);
  6892. wait_tcp_ready(&server_args);
  6893. test_client_nofail(&client_args, client_on_handshake);
  6894. join_thread(serverThread);
  6895. client_cb->return_code = client_args.return_code;
  6896. server_cb->return_code = server_args.return_code;
  6897. FreeTcpReady(&ready);
  6898. #ifdef WOLFSSL_TIRTOS
  6899. fdOpenSession(Task_self());
  6900. #endif
  6901. }
  6902. void test_wolfSSL_client_server_nofail(callback_functions* client_cb,
  6903. callback_functions* server_cb)
  6904. {
  6905. test_wolfSSL_client_server_nofail_ex(client_cb, server_cb, NULL);
  6906. }
  6907. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  6908. !defined(WOLFSSL_NO_TLS12) && !defined(NO_WOLFSSL_CLIENT)
  6909. static void test_client_reuse_WOLFSSLobj(void* args, cbType cb,
  6910. void* server_args)
  6911. {
  6912. SOCKET_T sockfd = 0;
  6913. callback_functions* cbf;
  6914. WOLFSSL_CTX* ctx = 0;
  6915. WOLFSSL* ssl = 0;
  6916. WOLFSSL_SESSION* session = NULL;
  6917. char msg[64] = "hello wolfssl!";
  6918. char reply[1024];
  6919. int input;
  6920. int msgSz = (int)XSTRLEN(msg);
  6921. int ret, err = 0;
  6922. int sharedCtx = 0;
  6923. #ifdef WOLFSSL_TIRTOS
  6924. fdOpenSession(Task_self());
  6925. #endif
  6926. ((func_args*)args)->return_code = TEST_FAIL;
  6927. cbf = ((func_args*)args)->callbacks;
  6928. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)
  6929. if (cbf != NULL && cbf->ctx) {
  6930. ctx = cbf->ctx;
  6931. sharedCtx = 1;
  6932. }
  6933. else
  6934. #endif
  6935. {
  6936. WOLFSSL_METHOD* method = NULL;
  6937. if (cbf != NULL && cbf->method != NULL) {
  6938. method = cbf->method();
  6939. }
  6940. else {
  6941. method = wolfSSLv23_client_method();
  6942. }
  6943. ctx = wolfSSL_CTX_new(method);
  6944. }
  6945. #ifdef WOLFSSL_ENCRYPTED_KEYS
  6946. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  6947. #endif
  6948. /* Do connect here so server detects failures */
  6949. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  6950. 0, 0, NULL);
  6951. if (wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0) !=
  6952. WOLFSSL_SUCCESS) {
  6953. /* err_sys("can't load ca file, Please run from wolfSSL home dir");*/
  6954. goto done;
  6955. }
  6956. if (!sharedCtx && wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  6957. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6958. /*err_sys("can't load client cert file, "
  6959. "Please run from wolfSSL home dir");*/
  6960. goto done;
  6961. }
  6962. if (!sharedCtx && wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  6963. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6964. /*err_sys("can't load client key file, "
  6965. "Please run from wolfSSL home dir");*/
  6966. goto done;
  6967. }
  6968. /* call ctx setup callback */
  6969. if (cbf != NULL && cbf->ctx_ready != NULL) {
  6970. cbf->ctx_ready(ctx);
  6971. }
  6972. ssl = wolfSSL_new(ctx);
  6973. if (ssl == NULL) {
  6974. goto done;
  6975. }
  6976. /* keep handshake resources for re-using WOLFSSL obj */
  6977. wolfSSL_KeepArrays(ssl);
  6978. if (wolfSSL_KeepHandshakeResources(ssl)) {
  6979. /* err_sys("SSL_KeepHandshakeResources failed"); */
  6980. goto done;
  6981. }
  6982. if (sharedCtx && wolfSSL_use_certificate_file(ssl, cliCertFile,
  6983. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6984. /*err_sys("can't load client cert file, "
  6985. "Please run from wolfSSL home dir");*/
  6986. goto done;
  6987. }
  6988. if (sharedCtx && wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  6989. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  6990. /*err_sys("can't load client key file, "
  6991. "Please run from wolfSSL home dir");*/
  6992. goto done;
  6993. }
  6994. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  6995. /*err_sys("SSL_set_fd failed");*/
  6996. goto done;
  6997. }
  6998. /* call ssl setup callback */
  6999. if (cbf != NULL && cbf->ssl_ready != NULL) {
  7000. cbf->ssl_ready(ssl);
  7001. }
  7002. #ifdef WOLFSSL_ASYNC_CRYPT
  7003. err = 0; /* Reset error */
  7004. #endif
  7005. do {
  7006. #ifdef WOLFSSL_ASYNC_CRYPT
  7007. if (err == WC_PENDING_E) {
  7008. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7009. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7010. }
  7011. #endif
  7012. ret = wolfSSL_connect(ssl);
  7013. err = wolfSSL_get_error(ssl, 0);
  7014. } while (err == WC_PENDING_E);
  7015. if (ret != WOLFSSL_SUCCESS) {
  7016. char buff[WOLFSSL_MAX_ERROR_SZ];
  7017. fprintf(stderr, "error = %d, %s\n", err,
  7018. wolfSSL_ERR_error_string(err, buff));
  7019. /*err_sys("SSL_connect failed");*/
  7020. goto done;
  7021. }
  7022. /* Build first session */
  7023. if (cb != NULL)
  7024. cb(ctx, ssl);
  7025. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  7026. /*err_sys("SSL_write failed");*/
  7027. goto done;
  7028. }
  7029. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  7030. if (input > 0) {
  7031. reply[input] = '\0';
  7032. fprintf(stderr, "Server response: %s\n", reply);
  7033. }
  7034. /* Session Resumption by re-using WOLFSSL object */
  7035. wolfSSL_set_quiet_shutdown(ssl, 1);
  7036. if (wolfSSL_shutdown(ssl) != WOLFSSL_SUCCESS) {
  7037. /* err_sys ("SSL shutdown failed"); */
  7038. goto done;
  7039. }
  7040. session = wolfSSL_get1_session(ssl);
  7041. if (wolfSSL_clear(ssl) != WOLFSSL_SUCCESS) {
  7042. wolfSSL_SESSION_free(session);
  7043. /* err_sys ("SSL_clear failed"); */
  7044. goto done;
  7045. }
  7046. wolfSSL_set_session(ssl, session);
  7047. wolfSSL_SESSION_free(session);
  7048. session = NULL;
  7049. /* close socket once */
  7050. CloseSocket(sockfd);
  7051. sockfd = 0;
  7052. /* wait until server ready */
  7053. wait_tcp_ready((func_args*)server_args);
  7054. fprintf(stderr, "session resumption\n");
  7055. /* Do re-connect */
  7056. tcp_connect(&sockfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7057. 0, 0, NULL);
  7058. if (wolfSSL_set_fd(ssl, sockfd) != WOLFSSL_SUCCESS) {
  7059. /*err_sys("SSL_set_fd failed");*/
  7060. goto done;
  7061. }
  7062. #ifdef WOLFSSL_ASYNC_CRYPT
  7063. err = 0; /* Reset error */
  7064. #endif
  7065. do {
  7066. #ifdef WOLFSSL_ASYNC_CRYPT
  7067. if (err == WC_PENDING_E) {
  7068. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7069. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7070. }
  7071. #endif
  7072. ret = wolfSSL_connect(ssl);
  7073. err = wolfSSL_get_error(ssl, 0);
  7074. } while (err == WC_PENDING_E);
  7075. if (ret != WOLFSSL_SUCCESS) {
  7076. char buff[WOLFSSL_MAX_ERROR_SZ];
  7077. fprintf(stderr, "error = %d, %s\n", err,
  7078. wolfSSL_ERR_error_string(err, buff));
  7079. /*err_sys("SSL_connect failed");*/
  7080. goto done;
  7081. }
  7082. /* Build first session */
  7083. if (cb != NULL)
  7084. cb(ctx, ssl);
  7085. if (wolfSSL_write(ssl, msg, msgSz) != msgSz) {
  7086. /*err_sys("SSL_write failed");*/
  7087. goto done;
  7088. }
  7089. input = wolfSSL_read(ssl, reply, sizeof(reply)-1);
  7090. if (input > 0) {
  7091. reply[input] = '\0';
  7092. fprintf(stderr, "Server response: %s\n", reply);
  7093. }
  7094. ((func_args*)args)->return_code = TEST_SUCCESS;
  7095. done:
  7096. wolfSSL_free(ssl);
  7097. if (!sharedCtx)
  7098. wolfSSL_CTX_free(ctx);
  7099. CloseSocket(sockfd);
  7100. #ifdef WOLFSSL_TIRTOS
  7101. fdCloseSession(Task_self());
  7102. #endif
  7103. return;
  7104. }
  7105. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  7106. !defined(WOLFSSL_TLS13) && !defined(NO_WOLFSSL_CLIENT) */
  7107. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  7108. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)) && \
  7109. defined(HAVE_ALPN) && defined(HAVE_SNI) && \
  7110. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_BIO)
  7111. #define HAVE_ALPN_PROTOS_SUPPORT
  7112. #endif
  7113. /* Generic TLS client / server with callbacks for API unit tests
  7114. * Used by SNI / ALPN / crypto callback helper functions */
  7115. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  7116. (defined(HAVE_SNI) || defined(HAVE_ALPN) || defined(WOLF_CRYPTO_CB) || \
  7117. defined(HAVE_ALPN_PROTOS_SUPPORT)) || defined(WOLFSSL_STATIC_MEMORY)
  7118. #define ENABLE_TLS_CALLBACK_TEST
  7119. #endif
  7120. #if defined(ENABLE_TLS_CALLBACK_TEST) || \
  7121. (defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT))
  7122. /* TLS server for API unit testing - generic */
  7123. static THREAD_RETURN WOLFSSL_THREAD run_wolfssl_server(void* args)
  7124. {
  7125. callback_functions* callbacks = ((func_args*)args)->callbacks;
  7126. WOLFSSL_CTX* ctx = NULL;
  7127. WOLFSSL* ssl = NULL;
  7128. SOCKET_T sfd = 0;
  7129. SOCKET_T cfd = 0;
  7130. word16 port;
  7131. char msg[] = "I hear you fa shizzle!";
  7132. int len = (int) XSTRLEN(msg);
  7133. char input[1024];
  7134. int idx;
  7135. int ret, err = 0;
  7136. ((func_args*)args)->return_code = TEST_FAIL;
  7137. #if defined(USE_WINDOWS_API)
  7138. port = ((func_args*)args)->signal->port;
  7139. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  7140. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  7141. /* Let tcp_listen assign port */
  7142. port = 0;
  7143. #else
  7144. /* Use default port */
  7145. port = wolfSSLPort;
  7146. #endif
  7147. #ifdef WOLFSSL_DTLS
  7148. if (callbacks->method == wolfDTLS_server_method
  7149. #ifdef WOLFSSL_STATIC_MEMORY
  7150. || callbacks->method_ex == wolfDTLS_server_method_ex
  7151. #endif
  7152. #ifndef NO_OLD_TLS
  7153. || callbacks->method == wolfDTLSv1_server_method
  7154. #ifdef WOLFSSL_STATIC_MEMORY
  7155. || callbacks->method_ex == wolfDTLSv1_server_method_ex
  7156. #endif
  7157. #endif
  7158. #ifndef WOLFSSL_NO_TLS12
  7159. || callbacks->method == wolfDTLSv1_2_server_method
  7160. #ifdef WOLFSSL_STATIC_MEMORY
  7161. || callbacks->method_ex == wolfDTLSv1_2_server_method_ex
  7162. #endif
  7163. #endif
  7164. #ifdef WOLFSSL_DTLS13
  7165. || callbacks->method == wolfDTLSv1_3_server_method
  7166. #ifdef WOLFSSL_STATIC_MEMORY
  7167. || callbacks->method_ex == wolfDTLSv1_3_server_method_ex
  7168. #endif
  7169. #endif
  7170. ) {
  7171. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 1, 0, 0, 0, 0, 0);
  7172. }
  7173. else
  7174. #endif
  7175. {
  7176. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  7177. }
  7178. #ifdef WOLFSSL_STATIC_MEMORY
  7179. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  7180. callbacks->memSz > 0) {
  7181. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  7182. callbacks->mem, callbacks->memSz, 0, 1);
  7183. if (ret != WOLFSSL_SUCCESS) {
  7184. fprintf(stderr, "CTX static new failed %d\n", ret);
  7185. goto cleanup;
  7186. }
  7187. }
  7188. #else
  7189. ctx = wolfSSL_CTX_new(callbacks->method());
  7190. #endif
  7191. if (ctx == NULL) {
  7192. fprintf(stderr, "CTX new failed\n");
  7193. goto cleanup;
  7194. }
  7195. /* set defaults */
  7196. if (callbacks->caPemFile == NULL)
  7197. callbacks->caPemFile = cliCertFile;
  7198. if (callbacks->certPemFile == NULL)
  7199. callbacks->certPemFile = svrCertFile;
  7200. if (callbacks->keyPemFile == NULL)
  7201. callbacks->keyPemFile = svrKeyFile;
  7202. #ifdef WOLFSSL_TIRTOS
  7203. fdOpenSession(Task_self());
  7204. #endif
  7205. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  7206. wolfSSL_CTX_set_verify(ctx,
  7207. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  7208. #ifdef WOLFSSL_ENCRYPTED_KEYS
  7209. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  7210. #endif
  7211. #if defined(WOLFSSL_SESSION_EXPORT) && defined(WOLFSSL_DTLS)
  7212. if (callbacks->method == wolfDTLSv1_2_server_method) {
  7213. if (wolfSSL_CTX_dtls_set_export(ctx, test_export) != WOLFSSL_SUCCESS)
  7214. goto cleanup;
  7215. }
  7216. #endif
  7217. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  7218. WOLFSSL_SUCCESS) {
  7219. goto cleanup;
  7220. }
  7221. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  7222. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7223. goto cleanup;
  7224. }
  7225. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  7226. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7227. goto cleanup;
  7228. }
  7229. #ifdef HAVE_CRL
  7230. if (callbacks->crlPemFile != NULL) {
  7231. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  7232. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7233. goto cleanup;
  7234. }
  7235. }
  7236. #endif
  7237. if (callbacks->ctx_ready)
  7238. callbacks->ctx_ready(ctx);
  7239. ssl = wolfSSL_new(ctx);
  7240. if (ssl == NULL) {
  7241. fprintf(stderr, "SSL new failed\n");
  7242. goto cleanup;
  7243. }
  7244. if (wolfSSL_dtls(ssl)) {
  7245. SOCKADDR_IN_T cliAddr;
  7246. socklen_t cliLen;
  7247. cliLen = sizeof(cliAddr);
  7248. idx = (int)recvfrom(sfd, input, sizeof(input), MSG_PEEK,
  7249. (struct sockaddr*)&cliAddr, &cliLen);
  7250. if (idx <= 0) {
  7251. goto cleanup;
  7252. }
  7253. wolfSSL_dtls_set_peer(ssl, &cliAddr, cliLen);
  7254. }
  7255. else {
  7256. CloseSocket(sfd);
  7257. }
  7258. if (wolfSSL_set_fd(ssl, cfd) != WOLFSSL_SUCCESS) {
  7259. goto cleanup;
  7260. }
  7261. if (callbacks->loadToSSL) {
  7262. wolfSSL_SetDevId(ssl, callbacks->devId);
  7263. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  7264. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7265. goto cleanup;
  7266. }
  7267. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  7268. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7269. goto cleanup;
  7270. }
  7271. }
  7272. #ifdef NO_PSK
  7273. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  7274. wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  7275. #elif !defined(NO_DH)
  7276. SetDH(ssl); /* will repick suites with DHE, higher priority than PSK */
  7277. #endif
  7278. #endif
  7279. if (callbacks->ssl_ready)
  7280. callbacks->ssl_ready(ssl);
  7281. #ifdef WOLFSSL_ASYNC_CRYPT
  7282. err = 0; /* Reset error */
  7283. #endif
  7284. do {
  7285. #ifdef WOLFSSL_ASYNC_CRYPT
  7286. if (err == WC_PENDING_E) {
  7287. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7288. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7289. }
  7290. #endif
  7291. ret = wolfSSL_accept(ssl);
  7292. err = wolfSSL_get_error(ssl, ret);
  7293. } while (err == WC_PENDING_E);
  7294. if (ret != WOLFSSL_SUCCESS) {
  7295. char buff[WOLFSSL_MAX_ERROR_SZ];
  7296. fprintf(stderr, "accept error = %d, %s\n", err,
  7297. wolfSSL_ERR_error_string(err, buff));
  7298. /*err_sys("SSL_accept failed");*/
  7299. }
  7300. else {
  7301. #ifdef WOLFSSL_ASYNC_CRYPT
  7302. err = 0; /* Reset error */
  7303. #endif
  7304. do {
  7305. #ifdef WOLFSSL_ASYNC_CRYPT
  7306. if (err == WC_PENDING_E) {
  7307. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7308. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7309. }
  7310. #endif
  7311. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  7312. err = wolfSSL_get_error(ssl, idx);
  7313. } while (err == WC_PENDING_E);
  7314. if (idx > 0) {
  7315. input[idx] = 0;
  7316. fprintf(stderr, "Client message: %s\n", input);
  7317. }
  7318. #ifdef WOLFSSL_ASYNC_CRYPT
  7319. err = 0; /* Reset error */
  7320. #endif
  7321. do {
  7322. #ifdef WOLFSSL_ASYNC_CRYPT
  7323. if (err == WC_PENDING_E) {
  7324. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7325. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7326. }
  7327. #endif
  7328. ret = wolfSSL_write(ssl, msg, len);
  7329. err = wolfSSL_get_error(ssl, ret);
  7330. } while (err == WC_PENDING_E);
  7331. if (len != ret) {
  7332. goto cleanup;
  7333. }
  7334. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(HAVE_IO_POOL) && \
  7335. defined(WOLFSSL_DTLS)
  7336. if (wolfSSL_dtls(ssl)) {
  7337. byte* import;
  7338. word32 sz;
  7339. wolfSSL_dtls_export(ssl, NULL, &sz);
  7340. import = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7341. if (import == NULL) {
  7342. goto cleanup;
  7343. }
  7344. idx = wolfSSL_dtls_export(ssl, import, &sz);
  7345. if (idx < 0) {
  7346. goto cleanup;
  7347. }
  7348. if (wolfSSL_dtls_import(ssl, import, idx) < 0) {
  7349. goto cleanup;
  7350. }
  7351. XFREE(import, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  7352. }
  7353. #endif
  7354. #ifdef WOLFSSL_TIRTOS
  7355. Task_yield();
  7356. #endif
  7357. ((func_args*)args)->return_code = TEST_SUCCESS;
  7358. }
  7359. if (callbacks->on_result)
  7360. callbacks->on_result(ssl);
  7361. wolfSSL_shutdown(ssl);
  7362. cleanup:
  7363. wolfSSL_free(ssl);
  7364. wolfSSL_CTX_free(ctx);
  7365. CloseSocket(cfd);
  7366. #ifdef WOLFSSL_TIRTOS
  7367. fdCloseSession(Task_self());
  7368. #endif
  7369. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  7370. && defined(HAVE_THREAD_LS)
  7371. wc_ecc_fp_free(); /* free per thread cache */
  7372. #endif
  7373. WOLFSSL_RETURN_FROM_THREAD(0);
  7374. }
  7375. /* TLS Client for API unit testing - generic */
  7376. static void run_wolfssl_client(void* args)
  7377. {
  7378. callback_functions* callbacks = ((func_args*)args)->callbacks;
  7379. WOLFSSL_CTX* ctx = NULL;
  7380. WOLFSSL* ssl = NULL;
  7381. SOCKET_T sfd = 0;
  7382. char msg[] = "hello wolfssl server!";
  7383. int len = (int) XSTRLEN(msg);
  7384. char input[1024];
  7385. int ret, err = 0;
  7386. ((func_args*)args)->return_code = TEST_FAIL;
  7387. /* set defaults */
  7388. if (callbacks->caPemFile == NULL)
  7389. callbacks->caPemFile = caCertFile;
  7390. if (callbacks->certPemFile == NULL)
  7391. callbacks->certPemFile = cliCertFile;
  7392. if (callbacks->keyPemFile == NULL)
  7393. callbacks->keyPemFile = cliKeyFile;
  7394. #ifdef WOLFSSL_STATIC_MEMORY
  7395. if (callbacks->method_ex != NULL && callbacks->mem != NULL &&
  7396. callbacks->memSz > 0) {
  7397. ret = wolfSSL_CTX_load_static_memory(&ctx, callbacks->method_ex,
  7398. callbacks->mem, callbacks->memSz, 0, 1);
  7399. if (ret != WOLFSSL_SUCCESS) {
  7400. fprintf(stderr, "CTX static new failed %d\n", ret);
  7401. goto cleanup;
  7402. }
  7403. }
  7404. #else
  7405. ctx = wolfSSL_CTX_new(callbacks->method());
  7406. #endif
  7407. if (ctx == NULL) {
  7408. fprintf(stderr, "CTX new failed\n");
  7409. goto cleanup;
  7410. }
  7411. #ifdef WOLFSSL_TIRTOS
  7412. fdOpenSession(Task_self());
  7413. #endif
  7414. if (!callbacks->loadToSSL) {
  7415. wolfSSL_CTX_SetDevId(ctx, callbacks->devId);
  7416. }
  7417. #ifdef WOLFSSL_ENCRYPTED_KEYS
  7418. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  7419. #endif
  7420. if (wolfSSL_CTX_load_verify_locations(ctx, callbacks->caPemFile, 0) !=
  7421. WOLFSSL_SUCCESS) {
  7422. goto cleanup;
  7423. }
  7424. if (!callbacks->loadToSSL) {
  7425. if (wolfSSL_CTX_use_certificate_file(ctx, callbacks->certPemFile,
  7426. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7427. goto cleanup;
  7428. }
  7429. if (wolfSSL_CTX_use_PrivateKey_file(ctx, callbacks->keyPemFile,
  7430. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7431. goto cleanup;
  7432. }
  7433. }
  7434. #ifdef HAVE_CRL
  7435. if (callbacks->crlPemFile != NULL) {
  7436. if (wolfSSL_CTX_LoadCRLFile(ctx, callbacks->crlPemFile,
  7437. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7438. goto cleanup;
  7439. }
  7440. }
  7441. #endif
  7442. if (callbacks->ctx_ready)
  7443. callbacks->ctx_ready(ctx);
  7444. ssl = wolfSSL_new(ctx);
  7445. if (wolfSSL_dtls(ssl)) {
  7446. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7447. 1, 0, ssl);
  7448. }
  7449. else {
  7450. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port,
  7451. 0, 0, ssl);
  7452. }
  7453. if (wolfSSL_set_fd(ssl, sfd) != WOLFSSL_SUCCESS) {
  7454. goto cleanup;
  7455. }
  7456. if (callbacks->loadToSSL) {
  7457. wolfSSL_SetDevId(ssl, callbacks->devId);
  7458. if (wolfSSL_use_certificate_file(ssl, callbacks->certPemFile,
  7459. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7460. goto cleanup;
  7461. }
  7462. if (wolfSSL_use_PrivateKey_file(ssl, callbacks->keyPemFile,
  7463. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS) {
  7464. goto cleanup;
  7465. }
  7466. }
  7467. if (callbacks->ssl_ready)
  7468. callbacks->ssl_ready(ssl);
  7469. #ifdef WOLFSSL_ASYNC_CRYPT
  7470. err = 0; /* Reset error */
  7471. #endif
  7472. do {
  7473. #ifdef WOLFSSL_ASYNC_CRYPT
  7474. if (err == WC_PENDING_E) {
  7475. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7476. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7477. }
  7478. #endif
  7479. ret = wolfSSL_connect(ssl);
  7480. err = wolfSSL_get_error(ssl, ret);
  7481. } while (err == WC_PENDING_E);
  7482. if (ret != WOLFSSL_SUCCESS) {
  7483. char buff[WOLFSSL_MAX_ERROR_SZ];
  7484. fprintf(stderr, "error = %d, %s\n", err,
  7485. wolfSSL_ERR_error_string(err, buff));
  7486. /*err_sys("SSL_connect failed");*/
  7487. }
  7488. else {
  7489. #ifdef WOLFSSL_ASYNC_CRYPT
  7490. err = 0; /* Reset error */
  7491. #endif
  7492. do {
  7493. #ifdef WOLFSSL_ASYNC_CRYPT
  7494. if (err == WC_PENDING_E) {
  7495. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7496. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7497. }
  7498. #endif
  7499. ret = wolfSSL_write(ssl, msg, len);
  7500. err = wolfSSL_get_error(ssl, ret);
  7501. } while (err == WC_PENDING_E);
  7502. if (len != ret)
  7503. goto cleanup;
  7504. #ifdef WOLFSSL_ASYNC_CRYPT
  7505. err = 0; /* Reset error */
  7506. #endif
  7507. do {
  7508. #ifdef WOLFSSL_ASYNC_CRYPT
  7509. if (err == WC_PENDING_E) {
  7510. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  7511. if (ret < 0) { break; } else if (ret == 0) { continue; }
  7512. }
  7513. #endif
  7514. ret = wolfSSL_read(ssl, input, sizeof(input)-1);
  7515. err = wolfSSL_get_error(ssl, ret);
  7516. } while (err == WC_PENDING_E);
  7517. if (ret > 0) {
  7518. input[ret] = '\0'; /* null term */
  7519. fprintf(stderr, "Server response: %s\n", input);
  7520. }
  7521. ((func_args*)args)->return_code = TEST_SUCCESS;
  7522. }
  7523. if (callbacks->on_result)
  7524. callbacks->on_result(ssl);
  7525. cleanup:
  7526. wolfSSL_free(ssl);
  7527. wolfSSL_CTX_free(ctx);
  7528. CloseSocket(sfd);
  7529. #ifdef WOLFSSL_TIRTOS
  7530. fdCloseSession(Task_self());
  7531. #endif
  7532. }
  7533. #endif /* ENABLE_TLS_CALLBACK_TEST */
  7534. static int test_wolfSSL_read_write(void)
  7535. {
  7536. /* The unit testing for read and write shall happen simultaneously, since
  7537. * one can't do anything with one without the other. (Except for a failure
  7538. * test case.) This function will call all the others that will set up,
  7539. * execute, and report their test findings.
  7540. *
  7541. * Set up the success case first. This function will become the template
  7542. * for the other tests. This should eventually be renamed
  7543. *
  7544. * The success case isn't interesting, how can this fail?
  7545. * - Do not give the client context a CA certificate. The connect should
  7546. * fail. Do not need server for this?
  7547. * - Using NULL for the ssl object on server. Do not need client for this.
  7548. * - Using NULL for the ssl object on client. Do not need server for this.
  7549. * - Good ssl objects for client and server. Client write() without server
  7550. * read().
  7551. * - Good ssl objects for client and server. Server write() without client
  7552. * read().
  7553. * - Forgetting the password callback?
  7554. */
  7555. tcp_ready ready;
  7556. func_args client_args;
  7557. func_args server_args;
  7558. THREAD_TYPE serverThread;
  7559. EXPECT_DECLS;
  7560. XMEMSET(&client_args, 0, sizeof(func_args));
  7561. XMEMSET(&server_args, 0, sizeof(func_args));
  7562. #ifdef WOLFSSL_TIRTOS
  7563. fdOpenSession(Task_self());
  7564. #endif
  7565. StartTCP();
  7566. InitTcpReady(&ready);
  7567. #if defined(USE_WINDOWS_API)
  7568. /* use RNG to get random port if using windows */
  7569. ready.port = GetRandomPort();
  7570. #endif
  7571. server_args.signal = &ready;
  7572. client_args.signal = &ready;
  7573. start_thread(test_server_nofail, &server_args, &serverThread);
  7574. wait_tcp_ready(&server_args);
  7575. test_client_nofail(&client_args, NULL);
  7576. join_thread(serverThread);
  7577. ExpectTrue(client_args.return_code);
  7578. ExpectTrue(server_args.return_code);
  7579. FreeTcpReady(&ready);
  7580. #ifdef WOLFSSL_TIRTOS
  7581. fdOpenSession(Task_self());
  7582. #endif
  7583. return EXPECT_RESULT();
  7584. }
  7585. static int test_wolfSSL_reuse_WOLFSSLobj(void)
  7586. {
  7587. EXPECT_DECLS;
  7588. #if defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) && \
  7589. !defined(WOLFSSL_NO_TLS12)
  7590. /* The unit test for session resumption by re-using WOLFSSL object.
  7591. * WOLFSSL object is not cleared after first session. It reuse the object
  7592. * for second connection.
  7593. */
  7594. tcp_ready ready;
  7595. func_args client_args;
  7596. func_args server_args;
  7597. THREAD_TYPE serverThread;
  7598. callback_functions client_cbf;
  7599. callback_functions server_cbf;
  7600. XMEMSET(&client_args, 0, sizeof(func_args));
  7601. XMEMSET(&server_args, 0, sizeof(func_args));
  7602. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  7603. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  7604. #ifdef WOLFSSL_TIRTOS
  7605. fdOpenSession(Task_self());
  7606. #endif
  7607. StartTCP();
  7608. InitTcpReady(&ready);
  7609. #if defined(USE_WINDOWS_API)
  7610. /* use RNG to get random port if using windows */
  7611. ready.port = GetRandomPort();
  7612. #endif
  7613. client_cbf.method = wolfTLSv1_2_client_method;
  7614. server_cbf.method = wolfTLSv1_2_server_method;
  7615. client_args.callbacks = &client_cbf;
  7616. server_args.callbacks = &server_cbf;
  7617. server_args.signal = &ready;
  7618. client_args.signal = &ready;
  7619. /* the var is used for loop number */
  7620. server_args.argc = 2;
  7621. start_thread(test_server_loop, &server_args, &serverThread);
  7622. wait_tcp_ready(&server_args);
  7623. test_client_reuse_WOLFSSLobj(&client_args, NULL, &server_args);
  7624. join_thread(serverThread);
  7625. ExpectTrue(client_args.return_code);
  7626. ExpectTrue(server_args.return_code);
  7627. FreeTcpReady(&ready);
  7628. #ifdef WOLFSSL_TIRTOS
  7629. fdOpenSession(Task_self());
  7630. #endif
  7631. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SESSION_CACHE) &&
  7632. * !defined(WOLFSSL_TLS13) */
  7633. return EXPECT_RESULT();
  7634. }
  7635. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7636. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7637. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready(
  7638. WOLFSSL_CTX* ctx)
  7639. {
  7640. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7641. myVerifyAction = VERIFY_USE_PREVERFIY;
  7642. wolfSSL_CTX_set_verify_depth(ctx, 2);
  7643. return TEST_SUCCESS;
  7644. }
  7645. #endif
  7646. static int test_wolfSSL_CTX_verifyDepth_ServerClient_1(void)
  7647. {
  7648. EXPECT_DECLS;
  7649. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7650. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7651. test_ssl_cbf client_cbf;
  7652. test_ssl_cbf server_cbf;
  7653. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7654. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7655. #ifdef WOLFSSL_TLS13
  7656. client_cbf.method = wolfTLSv1_3_client_method;
  7657. #endif /* WOLFSSL_TLS13 */
  7658. client_cbf.ctx_ready =
  7659. test_wolfSSL_CTX_verifyDepth_ServerClient_1_ctx_ready;
  7660. /* test case 1 verify depth is equal to peer chain */
  7661. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7662. &server_cbf, NULL), TEST_SUCCESS);
  7663. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7664. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7665. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7666. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7667. return EXPECT_RESULT();
  7668. }
  7669. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7670. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7671. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready(
  7672. WOLFSSL_CTX* ctx)
  7673. {
  7674. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7675. myVerifyAction = VERIFY_OVERRIDE_ERROR;
  7676. wolfSSL_CTX_set_verify_depth(ctx, 0);
  7677. return TEST_SUCCESS;
  7678. }
  7679. #endif
  7680. static int test_wolfSSL_CTX_verifyDepth_ServerClient_2(void)
  7681. {
  7682. EXPECT_DECLS;
  7683. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7684. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7685. test_ssl_cbf client_cbf;
  7686. test_ssl_cbf server_cbf;
  7687. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7688. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7689. #ifdef WOLFSSL_TLS13
  7690. client_cbf.method = wolfTLSv1_3_client_method;
  7691. #endif /* WOLFSSL_TLS13 */
  7692. client_cbf.ctx_ready =
  7693. test_wolfSSL_CTX_verifyDepth_ServerClient_2_ctx_ready;
  7694. /* test case 2
  7695. * verify depth is zero, number of peer's chain is 2.
  7696. * verify result becomes MAX_CHAIN_ERROR, but it is overridden in
  7697. * callback.
  7698. */
  7699. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7700. &server_cbf, NULL), TEST_SUCCESS);
  7701. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7702. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7703. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7704. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7705. return EXPECT_RESULT();
  7706. }
  7707. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7708. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7709. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready(
  7710. WOLFSSL_CTX* ctx)
  7711. {
  7712. wolfSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  7713. myVerifyAction = VERIFY_USE_PREVERFIY;
  7714. wolfSSL_CTX_set_verify_depth(ctx, 0);
  7715. return TEST_SUCCESS;
  7716. }
  7717. #endif
  7718. static int test_wolfSSL_CTX_verifyDepth_ServerClient_3(void)
  7719. {
  7720. EXPECT_DECLS;
  7721. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_TIRTOS) && \
  7722. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  7723. test_ssl_cbf client_cbf;
  7724. test_ssl_cbf server_cbf;
  7725. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7726. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7727. #ifdef WOLFSSL_TLS13
  7728. client_cbf.method = wolfTLSv1_3_client_method;
  7729. #endif /* WOLFSSL_TLS13 */
  7730. client_cbf.ctx_ready =
  7731. test_wolfSSL_CTX_verifyDepth_ServerClient_3_ctx_ready;
  7732. /* test case 3
  7733. * verify depth is zero, number of peer's chain is 2
  7734. * verify result becomes MAX_CHAIN_ERRO. call-back returns failure.
  7735. * therefore, handshake becomes failure.
  7736. */
  7737. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7738. &server_cbf, NULL), TEST_FAIL);
  7739. ExpectIntEQ(client_cbf.return_code, TEST_FAIL);
  7740. ExpectIntEQ(server_cbf.return_code, TEST_FAIL);
  7741. ExpectIntEQ(client_cbf.last_err, MAX_CHAIN_ERROR);
  7742. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  7743. #endif /* OPENSSL_EXTRA && !WOLFSSL_TIRTOS &&
  7744. * HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  7745. return EXPECT_RESULT();
  7746. }
  7747. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7748. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  7749. && !defined(NO_SHA256) && defined(HAVE_ECC)
  7750. static int test_wolfSSL_CTX_set_cipher_list_server_ctx_ready(WOLFSSL_CTX* ctx)
  7751. {
  7752. EXPECT_DECLS;
  7753. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "DEFAULT:!NULL"));
  7754. return EXPECT_RESULT();
  7755. }
  7756. static int test_wolfSSL_CTX_set_cipher_list_client_ctx_ready(WOLFSSL_CTX* ctx)
  7757. {
  7758. EXPECT_DECLS;
  7759. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256"));
  7760. return EXPECT_RESULT();
  7761. }
  7762. #endif
  7763. static int test_wolfSSL_CTX_set_cipher_list(void)
  7764. {
  7765. EXPECT_DECLS;
  7766. #if defined(OPENSSL_ALL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7767. !defined(WOLFSSL_TIRTOS) && !defined(NO_AES) && !defined(WOLFSSL_NO_TLS12) \
  7768. && !defined(NO_SHA256) && defined(HAVE_ECC)
  7769. WOLFSSL_CTX* ctxClient = NULL;
  7770. WOLFSSL* sslClient = NULL;
  7771. test_ssl_cbf client_cbf;
  7772. test_ssl_cbf server_cbf;
  7773. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7774. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7775. server_cbf.method = wolfTLSv1_2_server_method;
  7776. server_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_server_ctx_ready;
  7777. client_cbf.method = wolfTLSv1_2_client_method;
  7778. client_cbf.ctx_ready = test_wolfSSL_CTX_set_cipher_list_client_ctx_ready;
  7779. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7780. &server_cbf, NULL), TEST_SUCCESS);
  7781. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  7782. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  7783. /* check with cipher string that has '+' */
  7784. ExpectNotNull((ctxClient = wolfSSL_CTX_new(wolfTLSv1_2_client_method())));
  7785. ExpectTrue(wolfSSL_CTX_set_cipher_list(ctxClient, "ECDHE+AESGCM"));
  7786. ExpectNotNull((sslClient = wolfSSL_new(ctxClient)));
  7787. /* check for the existence of an ECDHE ECDSA cipher suite */
  7788. if (EXPECT_SUCCESS()) {
  7789. int i = 0;
  7790. int found = 0;
  7791. const char* suite;
  7792. WOLF_STACK_OF(WOLFSSL_CIPHER)* sk = NULL;
  7793. WOLFSSL_CIPHER* current;
  7794. ExpectNotNull((sk = wolfSSL_get_ciphers_compat(sslClient)));
  7795. do {
  7796. current = wolfSSL_sk_SSL_CIPHER_value(sk, i++);
  7797. if (current) {
  7798. suite = wolfSSL_CIPHER_get_name(current);
  7799. if (suite && XSTRSTR(suite, "ECDSA")) {
  7800. found = 1;
  7801. break;
  7802. }
  7803. }
  7804. } while (current);
  7805. ExpectIntEQ(found, 1);
  7806. }
  7807. wolfSSL_free(sslClient);
  7808. wolfSSL_CTX_free(ctxClient);
  7809. #endif
  7810. return EXPECT_RESULT();
  7811. }
  7812. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7813. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7814. static int test_wolfSSL_get_finished_client_on_handshake(WOLFSSL_CTX* ctx,
  7815. WOLFSSL* ssl)
  7816. {
  7817. EXPECT_DECLS;
  7818. size_t msg_len;
  7819. (void)ctx;
  7820. /* get_finished test */
  7821. /* 1. get own sent message */
  7822. XMEMSET(client_side_msg1, 0, MD_MAX_SIZE);
  7823. msg_len = wolfSSL_get_finished(ssl, client_side_msg1, MD_MAX_SIZE);
  7824. ExpectIntGE(msg_len, 0);
  7825. /* 2. get peer message */
  7826. XMEMSET(client_side_msg2, 0, MD_MAX_SIZE);
  7827. msg_len = wolfSSL_get_peer_finished(ssl, client_side_msg2, MD_MAX_SIZE);
  7828. ExpectIntGE(msg_len, 0);
  7829. return EXPECT_RESULT();
  7830. }
  7831. #endif
  7832. static int test_wolfSSL_get_finished(void)
  7833. {
  7834. EXPECT_DECLS;
  7835. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  7836. defined(WOLFSSL_HAVE_TLS_UNIQUE)
  7837. test_ssl_cbf client_cbf;
  7838. test_ssl_cbf server_cbf;
  7839. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  7840. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  7841. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  7842. &server_cbf, test_wolfSSL_get_finished_client_on_handshake),
  7843. TEST_SUCCESS);
  7844. /* test received msg vs sent msg */
  7845. ExpectIntEQ(0, XMEMCMP(client_side_msg1, server_side_msg2, MD_MAX_SIZE));
  7846. ExpectIntEQ(0, XMEMCMP(client_side_msg2, server_side_msg1, MD_MAX_SIZE));
  7847. #endif /* HAVE_SSL_MEMIO_TESTS_DEPENDENCIES && WOLFSSL_HAVE_TLS_UNIQUE */
  7848. return EXPECT_RESULT();
  7849. }
  7850. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7851. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7852. !defined(NO_SESSION_CACHE)
  7853. /* Sessions to restore/store */
  7854. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_client_sess;
  7855. static WOLFSSL_SESSION* test_wolfSSL_CTX_add_session_server_sess;
  7856. static WOLFSSL_CTX* test_wolfSSL_CTX_add_session_server_ctx;
  7857. static void test_wolfSSL_CTX_add_session_ctx_ready(WOLFSSL_CTX* ctx)
  7858. {
  7859. /* Don't store sessions. Lookup is still enabled. */
  7860. AssertIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  7861. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  7862. #ifdef OPENSSL_EXTRA
  7863. AssertIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  7864. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  7865. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  7866. #endif
  7867. /* Require both peers to provide certs */
  7868. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  7869. }
  7870. static void test_wolfSSL_CTX_add_session_on_result(WOLFSSL* ssl)
  7871. {
  7872. WOLFSSL_SESSION** sess;
  7873. if (wolfSSL_is_server(ssl))
  7874. sess = &test_wolfSSL_CTX_add_session_server_sess;
  7875. else
  7876. sess = &test_wolfSSL_CTX_add_session_client_sess;
  7877. if (*sess == NULL) {
  7878. #ifdef NO_SESSION_CACHE_REF
  7879. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7880. #else
  7881. /* Test for backwards compatibility */
  7882. if (wolfSSL_is_server(ssl)) {
  7883. AssertNotNull(*sess = wolfSSL_get1_session(ssl));
  7884. }
  7885. else {
  7886. AssertNotNull(*sess = wolfSSL_get_session(ssl));
  7887. }
  7888. #endif
  7889. /* Now save the session in the internal store to make it available
  7890. * for lookup. For TLS 1.3, we can't save the session without
  7891. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  7892. * session from cache. */
  7893. if (wolfSSL_is_server(ssl)
  7894. #ifndef WOLFSSL_TICKET_HAVE_ID
  7895. && wolfSSL_version(ssl) != TLS1_3_VERSION
  7896. #endif
  7897. )
  7898. AssertIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  7899. *sess), WOLFSSL_SUCCESS);
  7900. }
  7901. else {
  7902. /* If we have a session retrieved then remaining connections should be
  7903. * resuming on that session */
  7904. AssertIntEQ(wolfSSL_session_reused(ssl), 1);
  7905. }
  7906. /* Save CTX to be able to decrypt tickets */
  7907. if (wolfSSL_is_server(ssl) &&
  7908. test_wolfSSL_CTX_add_session_server_ctx == NULL) {
  7909. AssertNotNull(test_wolfSSL_CTX_add_session_server_ctx
  7910. = wolfSSL_get_SSL_CTX(ssl));
  7911. AssertIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  7912. WOLFSSL_SUCCESS);
  7913. }
  7914. #ifdef SESSION_CERTS
  7915. #ifndef WOLFSSL_TICKET_HAVE_ID
  7916. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  7917. wolfSSL_session_reused(ssl))
  7918. #endif
  7919. {
  7920. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  7921. * for all connections. TLS 1.3 only has tickets so if we don't
  7922. * include the session id in the ticket then the certificates
  7923. * will not be available on resumption. */
  7924. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  7925. AssertNotNull(peer);
  7926. wolfSSL_X509_free(peer);
  7927. AssertNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  7928. #ifdef OPENSSL_EXTRA
  7929. AssertNotNull(SSL_SESSION_get0_peer(*sess));
  7930. #endif
  7931. }
  7932. #endif /* SESSION_CERTS */
  7933. }
  7934. static void test_wolfSSL_CTX_add_session_ssl_ready(WOLFSSL* ssl)
  7935. {
  7936. /* Set the session to reuse for the client */
  7937. AssertIntEQ(wolfSSL_set_session(ssl,
  7938. test_wolfSSL_CTX_add_session_client_sess), WOLFSSL_SUCCESS);
  7939. }
  7940. #endif
  7941. static int test_wolfSSL_CTX_add_session(void)
  7942. {
  7943. EXPECT_DECLS;
  7944. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  7945. !defined(SINGLE_THREADED) && defined(WOLFSSL_TLS13) && \
  7946. !defined(NO_SESSION_CACHE)
  7947. tcp_ready ready;
  7948. func_args client_args;
  7949. func_args server_args;
  7950. THREAD_TYPE serverThread;
  7951. callback_functions client_cb;
  7952. callback_functions server_cb;
  7953. method_provider methods[][2] = {
  7954. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  7955. !defined(NO_DES3))
  7956. /* Without AES there are almost no ciphersuites available. This leads
  7957. * to no ciphersuites being available and an error. */
  7958. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method },
  7959. #endif
  7960. #ifndef WOLFSSL_NO_TLS12
  7961. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method },
  7962. #endif
  7963. /* Needs the default ticket callback since it is tied to the
  7964. * connection context and this makes it easy to carry over the ticket
  7965. * crypto context between connections */
  7966. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  7967. defined(HAVE_SESSION_TICKET)
  7968. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method },
  7969. #endif
  7970. };
  7971. const size_t methodsLen = sizeof(methods)/sizeof(*methods);
  7972. size_t i, j;
  7973. for (i = 0; i < methodsLen; i++) {
  7974. /* First run creates a connection while the second+ run will attempt
  7975. * to resume the connection. The trick is that the internal cache
  7976. * is turned off. wolfSSL_CTX_add_session should put the session in
  7977. * the cache anyway. */
  7978. test_wolfSSL_CTX_add_session_client_sess = NULL;
  7979. test_wolfSSL_CTX_add_session_server_sess = NULL;
  7980. test_wolfSSL_CTX_add_session_server_ctx = NULL;
  7981. #ifdef NO_SESSION_CACHE_REF
  7982. for (j = 0; j < 4; j++) {
  7983. #else
  7984. /* The session may be overwritten in this case. Do only one resumption
  7985. * to stop this test from failing intermittently. */
  7986. for (j = 0; j < 2; j++) {
  7987. #endif
  7988. #ifdef WOLFSSL_TIRTOS
  7989. fdOpenSession(Task_self());
  7990. #endif
  7991. StartTCP();
  7992. InitTcpReady(&ready);
  7993. XMEMSET(&client_args, 0, sizeof(func_args));
  7994. XMEMSET(&server_args, 0, sizeof(func_args));
  7995. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  7996. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  7997. client_cb.method = methods[i][0];
  7998. server_cb.method = methods[i][1];
  7999. server_args.signal = &ready;
  8000. server_args.callbacks = &server_cb;
  8001. client_args.signal = &ready;
  8002. client_args.callbacks = &client_cb;
  8003. if (test_wolfSSL_CTX_add_session_server_ctx != NULL) {
  8004. server_cb.ctx = test_wolfSSL_CTX_add_session_server_ctx;
  8005. server_cb.isSharedCtx = 1;
  8006. }
  8007. server_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  8008. client_cb.ctx_ready = test_wolfSSL_CTX_add_session_ctx_ready;
  8009. if (j != 0)
  8010. client_cb.ssl_ready = test_wolfSSL_CTX_add_session_ssl_ready;
  8011. server_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  8012. client_cb.on_result = test_wolfSSL_CTX_add_session_on_result;
  8013. server_cb.ticNoInit = 1; /* Use default builtin */
  8014. start_thread(test_server_nofail, &server_args, &serverThread);
  8015. wait_tcp_ready(&server_args);
  8016. test_client_nofail(&client_args, NULL);
  8017. join_thread(serverThread);
  8018. ExpectTrue(client_args.return_code);
  8019. ExpectTrue(server_args.return_code);
  8020. FreeTcpReady(&ready);
  8021. if (EXPECT_FAIL())
  8022. break;
  8023. }
  8024. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_client_sess);
  8025. wolfSSL_SESSION_free(test_wolfSSL_CTX_add_session_server_sess);
  8026. wolfSSL_CTX_free(test_wolfSSL_CTX_add_session_server_ctx);
  8027. if (EXPECT_FAIL())
  8028. break;
  8029. }
  8030. #endif
  8031. return EXPECT_RESULT();
  8032. }
  8033. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8034. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8035. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8036. defined(HAVE_SESSION_TICKET) && \
  8037. !defined(TITAN_SESSION_CACHE) && \
  8038. !defined(HUGE_SESSION_CACHE) && \
  8039. !defined(BIG_SESSION_CACHE) && \
  8040. !defined(MEDIUM_SESSION_CACHE)
  8041. /* twcase - prefix for test_wolfSSL_CTX_add_session_ext */
  8042. /* Sessions to restore/store */
  8043. static WOLFSSL_SESSION* twcase_server_first_session_ptr;
  8044. static WOLFSSL_SESSION* twcase_client_first_session_ptr;
  8045. static WOLFSSL_CTX* twcase_server_current_ctx_ptr;
  8046. static int twcase_new_session_called = 0;
  8047. static int twcase_remove_session_called = 0;
  8048. static int twcase_get_session_called = 0;
  8049. /* Test default, SESSIONS_PER_ROW*SESSION_ROWS = 3*11, see ssl.c */
  8050. #define SESSION_CACHE_SIZE 33
  8051. typedef struct {
  8052. const byte* key; /* key, altSessionID, session ID, NULL if empty */
  8053. WOLFSSL_SESSION* value;
  8054. } hashTable_entry;
  8055. typedef struct {
  8056. hashTable_entry entries[SESSION_CACHE_SIZE]; /* hash slots */
  8057. size_t capacity; /* size of entries */
  8058. size_t length; /* number of items in the hash table */
  8059. wolfSSL_Mutex htLock; /* lock */
  8060. }hashTable;
  8061. static hashTable server_sessionCache;
  8062. static int twcase_new_sessionCb(WOLFSSL *ssl, WOLFSSL_SESSION *sess)
  8063. {
  8064. int i;
  8065. unsigned int len;
  8066. (void)ssl;
  8067. /*
  8068. * This example uses a hash table.
  8069. * Steps you should take for a non-demo code:
  8070. * - acquire a lock for the file named according to the session id
  8071. * - open the file
  8072. * - encrypt and write the SSL_SESSION object to the file
  8073. * - release the lock
  8074. *
  8075. * Return:
  8076. * 0: The callback does not wish to hold a reference of the sess
  8077. * 1: The callback wants to hold a reference of the sess. The callback is
  8078. * now also responsible for calling wolfSSL_SESSION_free() on sess.
  8079. */
  8080. if (sess == NULL)
  8081. return 0;
  8082. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  8083. return 0;
  8084. }
  8085. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8086. if (server_sessionCache.entries[i].value == NULL) {
  8087. server_sessionCache.entries[i].key = SSL_SESSION_get_id(sess, &len);
  8088. server_sessionCache.entries[i].value = sess;
  8089. server_sessionCache.length++;
  8090. break;
  8091. }
  8092. }
  8093. ++twcase_new_session_called;
  8094. wc_UnLockMutex(&server_sessionCache.htLock);
  8095. fprintf(stderr, "\t\ttwcase_new_session_called %d\n",
  8096. twcase_new_session_called);
  8097. return 1;
  8098. }
  8099. static void twcase_remove_sessionCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  8100. {
  8101. int i;
  8102. (void)ctx;
  8103. (void)sess;
  8104. if (sess == NULL)
  8105. return;
  8106. /*
  8107. * This example uses a hash table.
  8108. * Steps you should take for a non-demo code:
  8109. * - acquire a lock for the file named according to the session id
  8110. * - remove the file
  8111. * - release the lock
  8112. */
  8113. if (wc_LockMutex(&server_sessionCache.htLock) != 0) {
  8114. return;
  8115. }
  8116. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8117. if (server_sessionCache.entries[i].key != NULL &&
  8118. XMEMCMP(server_sessionCache.entries[i].key,
  8119. sess->sessionID, SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  8120. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  8121. server_sessionCache.entries[i].value = NULL;
  8122. server_sessionCache.entries[i].key = NULL;
  8123. server_sessionCache.length--;
  8124. break;
  8125. }
  8126. }
  8127. ++twcase_remove_session_called;
  8128. wc_UnLockMutex(&server_sessionCache.htLock);
  8129. fprintf(stderr, "\t\ttwcase_remove_session_called %d\n",
  8130. twcase_remove_session_called);
  8131. }
  8132. static WOLFSSL_SESSION *twcase_get_sessionCb(WOLFSSL *ssl,
  8133. const unsigned char *id, int len, int *ref)
  8134. {
  8135. int i;
  8136. (void)ssl;
  8137. (void)id;
  8138. (void)len;
  8139. /*
  8140. * This example uses a hash table.
  8141. * Steps you should take for a non-demo code:
  8142. * - acquire a lock for the file named according to the session id in the
  8143. * 2nd arg
  8144. * - read and decrypt contents of file and create a new SSL_SESSION
  8145. * - object release the lock
  8146. * - return the new session object
  8147. */
  8148. fprintf(stderr, "\t\ttwcase_get_session_called %d\n",
  8149. ++twcase_get_session_called);
  8150. /* This callback want to retain a copy of the object. If we want wolfSSL to
  8151. * be responsible for the pointer then set to 0. */
  8152. *ref = 1;
  8153. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8154. if (server_sessionCache.entries[i].key != NULL &&
  8155. XMEMCMP(server_sessionCache.entries[i].key, id,
  8156. SSL_MAX_SSL_SESSION_ID_LENGTH) == 0) {
  8157. return server_sessionCache.entries[i].value;
  8158. }
  8159. }
  8160. return NULL;
  8161. }
  8162. static int twcase_get_sessionCb_cleanup(void)
  8163. {
  8164. int i;
  8165. int cnt = 0;
  8166. /* If twcase_get_sessionCb sets *ref = 1, the application is responsible
  8167. * for freeing sessions */
  8168. for (i = 0; i < SESSION_CACHE_SIZE; i++) {
  8169. if (server_sessionCache.entries[i].value != NULL) {
  8170. wolfSSL_SESSION_free(server_sessionCache.entries[i].value);
  8171. cnt++;
  8172. }
  8173. }
  8174. fprintf(stderr, "\t\ttwcase_get_sessionCb_cleanup freed %d sessions\n",
  8175. cnt);
  8176. return TEST_SUCCESS;
  8177. }
  8178. static int twcase_cache_intOff_extOff(WOLFSSL_CTX* ctx)
  8179. {
  8180. EXPECT_DECLS;
  8181. /* off - Disable internal cache */
  8182. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  8183. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  8184. #ifdef OPENSSL_EXTRA
  8185. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  8186. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  8187. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  8188. #endif
  8189. /* off - Do not setup external cache */
  8190. /* Require both peers to provide certs */
  8191. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8192. return EXPECT_RESULT();
  8193. }
  8194. static int twcase_cache_intOn_extOff(WOLFSSL_CTX* ctx)
  8195. {
  8196. /* on - internal cache is on by default */
  8197. /* off - Do not setup external cache */
  8198. /* Require both peers to provide certs */
  8199. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8200. return TEST_SUCCESS;
  8201. }
  8202. static int twcase_cache_intOff_extOn(WOLFSSL_CTX* ctx)
  8203. {
  8204. EXPECT_DECLS;
  8205. /* off - Disable internal cache */
  8206. ExpectIntEQ(wolfSSL_CTX_set_session_cache_mode(ctx,
  8207. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE), WOLFSSL_SUCCESS);
  8208. #ifdef OPENSSL_EXTRA
  8209. ExpectIntEQ(wolfSSL_CTX_get_session_cache_mode(ctx) &
  8210. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE,
  8211. WOLFSSL_SESS_CACHE_NO_INTERNAL_STORE);
  8212. #endif
  8213. /* on - Enable external cache */
  8214. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8215. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8216. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8217. /* Require both peers to provide certs */
  8218. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8219. return EXPECT_RESULT();
  8220. }
  8221. static int twcase_cache_intOn_extOn(WOLFSSL_CTX* ctx)
  8222. {
  8223. /* on - internal cache is on by default */
  8224. /* on - Enable external cache */
  8225. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8226. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8227. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8228. /* Require both peers to provide certs */
  8229. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8230. return TEST_SUCCESS;
  8231. }
  8232. static int twcase_cache_intOn_extOn_noTicket(WOLFSSL_CTX* ctx)
  8233. {
  8234. /* on - internal cache is on by default */
  8235. /* on - Enable external cache */
  8236. wolfSSL_CTX_sess_set_new_cb(ctx, twcase_new_sessionCb);
  8237. wolfSSL_CTX_sess_set_remove_cb(ctx, twcase_remove_sessionCb);
  8238. wolfSSL_CTX_sess_set_get_cb(ctx, twcase_get_sessionCb);
  8239. wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TICKET);
  8240. /* Require both peers to provide certs */
  8241. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  8242. return TEST_SUCCESS;
  8243. }
  8244. static int twcase_server_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  8245. {
  8246. EXPECT_DECLS;
  8247. WOLFSSL_SESSION** sess;
  8248. if (wolfSSL_is_server(ssl))
  8249. sess = &twcase_server_first_session_ptr;
  8250. else
  8251. return TEST_SUCCESS;
  8252. if (*sess == NULL) {
  8253. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  8254. /* Now save the session in the internal store to make it available
  8255. * for lookup. For TLS 1.3, we can't save the session without
  8256. * WOLFSSL_TICKET_HAVE_ID because there is no way to retrieve the
  8257. * session from cache. */
  8258. if (wolfSSL_is_server(ssl)
  8259. #ifndef WOLFSSL_TICKET_HAVE_ID
  8260. && wolfSSL_version(ssl) != TLS1_3_VERSION
  8261. && wolfSSL_version(ssl) != DTLS1_3_VERSION
  8262. #endif
  8263. ) {
  8264. ExpectIntEQ(wolfSSL_CTX_add_session(wolfSSL_get_SSL_CTX(ssl),
  8265. *sess), WOLFSSL_SUCCESS);
  8266. }
  8267. }
  8268. /* Save CTX to be able to decrypt tickets */
  8269. if (twcase_server_current_ctx_ptr == NULL) {
  8270. ExpectNotNull(twcase_server_current_ctx_ptr = wolfSSL_get_SSL_CTX(ssl));
  8271. ExpectIntEQ(wolfSSL_CTX_up_ref(wolfSSL_get_SSL_CTX(ssl)),
  8272. WOLFSSL_SUCCESS);
  8273. }
  8274. #ifdef SESSION_CERTS
  8275. #ifndef WOLFSSL_TICKET_HAVE_ID
  8276. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  8277. wolfSSL_session_reused(ssl))
  8278. #endif
  8279. {
  8280. /* With WOLFSSL_TICKET_HAVE_ID the peer certs should be available
  8281. * for all connections. TLS 1.3 only has tickets so if we don't
  8282. * include the session id in the ticket then the certificates
  8283. * will not be available on resumption. */
  8284. WOLFSSL_X509* peer = NULL;
  8285. ExpectNotNull(peer = wolfSSL_get_peer_certificate(ssl));
  8286. wolfSSL_X509_free(peer);
  8287. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  8288. }
  8289. #endif
  8290. return EXPECT_RESULT();
  8291. }
  8292. static int twcase_client_sess_ctx_pre_shutdown(WOLFSSL* ssl)
  8293. {
  8294. EXPECT_DECLS;
  8295. WOLFSSL_SESSION** sess;
  8296. sess = &twcase_client_first_session_ptr;
  8297. if (*sess == NULL) {
  8298. ExpectNotNull(*sess = wolfSSL_get1_session(ssl));
  8299. }
  8300. else {
  8301. /* If we have a session retrieved then remaining connections should be
  8302. * resuming on that session */
  8303. ExpectIntEQ(wolfSSL_session_reused(ssl), 1);
  8304. }
  8305. #ifdef SESSION_CERTS
  8306. #ifndef WOLFSSL_TICKET_HAVE_ID
  8307. if (wolfSSL_version(ssl) != TLS1_3_VERSION &&
  8308. wolfSSL_session_reused(ssl))
  8309. #endif
  8310. {
  8311. WOLFSSL_X509* peer = wolfSSL_get_peer_certificate(ssl);
  8312. ExpectNotNull(peer);
  8313. wolfSSL_X509_free(peer);
  8314. ExpectNotNull(wolfSSL_SESSION_get_peer_chain(*sess));
  8315. #ifdef OPENSSL_EXTRA
  8316. ExpectNotNull(wolfSSL_SESSION_get0_peer(*sess));
  8317. #endif
  8318. }
  8319. #endif
  8320. return EXPECT_RESULT();
  8321. }
  8322. static int twcase_client_set_sess_ssl_ready(WOLFSSL* ssl)
  8323. {
  8324. EXPECT_DECLS;
  8325. /* Set the session to reuse for the client */
  8326. ExpectNotNull(ssl);
  8327. ExpectNotNull(twcase_client_first_session_ptr);
  8328. ExpectIntEQ(wolfSSL_set_session(ssl,twcase_client_first_session_ptr),
  8329. WOLFSSL_SUCCESS);
  8330. return EXPECT_RESULT();
  8331. }
  8332. struct test_add_session_ext_params {
  8333. method_provider client_meth;
  8334. method_provider server_meth;
  8335. const char* tls_version;
  8336. };
  8337. static int test_wolfSSL_CTX_add_session_ext(
  8338. struct test_add_session_ext_params* param)
  8339. {
  8340. EXPECT_DECLS;
  8341. /* Test the default 33 sessions */
  8342. int j;
  8343. /* Clear cache before starting */
  8344. wolfSSL_CTX_flush_sessions(NULL, -1);
  8345. XMEMSET(&server_sessionCache, 0, sizeof(hashTable));
  8346. if (wc_InitMutex(&server_sessionCache.htLock) != 0)
  8347. return BAD_MUTEX_E;
  8348. server_sessionCache.capacity = SESSION_CACHE_SIZE;
  8349. fprintf(stderr, "\tBegin %s\n", param->tls_version);
  8350. for (j = 0; j < 5; j++) {
  8351. int tls13 = XSTRSTR(param->tls_version, "TLSv1_3") != NULL;
  8352. int dtls = XSTRSTR(param->tls_version, "DTLS") != NULL;
  8353. test_ssl_cbf client_cb;
  8354. test_ssl_cbf server_cb;
  8355. (void)dtls;
  8356. /* Test five cache configurations */
  8357. twcase_client_first_session_ptr = NULL;
  8358. twcase_server_first_session_ptr = NULL;
  8359. twcase_server_current_ctx_ptr = NULL;
  8360. twcase_new_session_called = 0;
  8361. twcase_remove_session_called = 0;
  8362. twcase_get_session_called = 0;
  8363. /* connection 1 - first connection */
  8364. fprintf(stderr, "\tconnect: %s: j=%d\n", param->tls_version, j);
  8365. XMEMSET(&client_cb, 0, sizeof(client_cb));
  8366. XMEMSET(&server_cb, 0, sizeof(server_cb));
  8367. client_cb.method = param->client_meth;
  8368. server_cb.method = param->server_meth;
  8369. if (dtls)
  8370. client_cb.doUdp = server_cb.doUdp = 1;
  8371. /* Setup internal and external cache */
  8372. switch (j) {
  8373. case 0:
  8374. /* SSL_OP_NO_TICKET stateful ticket case */
  8375. server_cb.ctx_ready = twcase_cache_intOn_extOn_noTicket;
  8376. break;
  8377. case 1:
  8378. server_cb.ctx_ready = twcase_cache_intOn_extOn;
  8379. break;
  8380. case 2:
  8381. server_cb.ctx_ready = twcase_cache_intOff_extOn;
  8382. break;
  8383. case 3:
  8384. server_cb.ctx_ready = twcase_cache_intOn_extOff;
  8385. break;
  8386. case 4:
  8387. server_cb.ctx_ready = twcase_cache_intOff_extOff;
  8388. break;
  8389. }
  8390. client_cb.ctx_ready = twcase_cache_intOff_extOff;
  8391. /* Add session to internal cache and save SSL session for testing */
  8392. server_cb.on_result = twcase_server_sess_ctx_pre_shutdown;
  8393. /* Save client SSL session for testing */
  8394. client_cb.on_result = twcase_client_sess_ctx_pre_shutdown;
  8395. server_cb.ticNoInit = 1; /* Use default builtin */
  8396. /* Don't free/release ctx */
  8397. server_cb.ctx = twcase_server_current_ctx_ptr;
  8398. server_cb.isSharedCtx = 1;
  8399. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  8400. &server_cb, NULL), TEST_SUCCESS);
  8401. ExpectIntEQ(twcase_get_session_called, 0);
  8402. if (EXPECT_FAIL()) {
  8403. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  8404. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  8405. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  8406. break;
  8407. }
  8408. switch (j) {
  8409. case 0:
  8410. case 1:
  8411. case 2:
  8412. /* cache cannot be searched with out a connection */
  8413. /* Add a new session */
  8414. ExpectIntEQ(twcase_new_session_called, 1);
  8415. /* In twcase_server_sess_ctx_pre_shutdown
  8416. * wolfSSL_CTX_add_session which evicts the existing session
  8417. * in cache and adds it back in */
  8418. ExpectIntLE(twcase_remove_session_called, 1);
  8419. break;
  8420. case 3:
  8421. case 4:
  8422. /* no external cache */
  8423. ExpectIntEQ(twcase_new_session_called, 0);
  8424. ExpectIntEQ(twcase_remove_session_called, 0);
  8425. break;
  8426. }
  8427. /* connection 2 - session resume */
  8428. fprintf(stderr, "\tresume: %s: j=%d\n", param->tls_version, j);
  8429. twcase_new_session_called = 0;
  8430. twcase_remove_session_called = 0;
  8431. twcase_get_session_called = 0;
  8432. server_cb.on_result = 0;
  8433. client_cb.on_result = 0;
  8434. server_cb.ticNoInit = 1; /* Use default builtin */
  8435. server_cb.ctx = twcase_server_current_ctx_ptr;
  8436. /* try session resumption */
  8437. client_cb.ssl_ready = twcase_client_set_sess_ssl_ready;
  8438. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  8439. &server_cb, NULL), TEST_SUCCESS);
  8440. /* Clear cache before checking */
  8441. wolfSSL_CTX_flush_sessions(NULL, -1);
  8442. switch (j) {
  8443. case 0:
  8444. if (tls13) {
  8445. /* (D)TLSv1.3 stateful case */
  8446. /* cache hit */
  8447. /* DTLS accesses cache once for stateless parsing and
  8448. * once for stateful parsing */
  8449. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8450. /* (D)TLSv1.3 creates a new ticket,
  8451. * updates both internal and external cache */
  8452. ExpectIntEQ(twcase_new_session_called, 1);
  8453. /* A new session ID is created for a new ticket */
  8454. ExpectIntEQ(twcase_remove_session_called, 2);
  8455. }
  8456. else {
  8457. /* non (D)TLSv1.3 case, no update */
  8458. /* DTLS accesses cache once for stateless parsing and
  8459. * once for stateful parsing */
  8460. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8461. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8462. #else
  8463. ExpectIntEQ(twcase_get_session_called, 1);
  8464. #endif
  8465. ExpectIntEQ(twcase_new_session_called, 0);
  8466. /* Called on session added in
  8467. * twcase_server_sess_ctx_pre_shutdown */
  8468. ExpectIntEQ(twcase_remove_session_called, 1);
  8469. }
  8470. break;
  8471. case 1:
  8472. if (tls13) {
  8473. /* (D)TLSv1.3 case */
  8474. /* cache hit */
  8475. ExpectIntEQ(twcase_get_session_called, 1);
  8476. /* (D)TLSv1.3 creates a new ticket,
  8477. * updates both internal and external cache */
  8478. ExpectIntEQ(twcase_new_session_called, 1);
  8479. /* Called on session added in
  8480. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  8481. ExpectIntEQ(twcase_remove_session_called, 1);
  8482. }
  8483. else {
  8484. /* non (D)TLSv1.3 case */
  8485. /* cache hit */
  8486. /* DTLS accesses cache once for stateless parsing and
  8487. * once for stateful parsing */
  8488. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8489. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8490. #else
  8491. ExpectIntEQ(twcase_get_session_called, 1);
  8492. #endif
  8493. ExpectIntEQ(twcase_new_session_called, 0);
  8494. /* Called on session added in
  8495. * twcase_server_sess_ctx_pre_shutdown */
  8496. ExpectIntEQ(twcase_remove_session_called, 1);
  8497. }
  8498. break;
  8499. case 2:
  8500. if (tls13) {
  8501. /* (D)TLSv1.3 case */
  8502. /* cache hit */
  8503. ExpectIntEQ(twcase_get_session_called, 1);
  8504. /* (D)TLSv1.3 creates a new ticket,
  8505. * updates both internal and external cache */
  8506. ExpectIntEQ(twcase_new_session_called, 1);
  8507. /* Called on session added in
  8508. * twcase_server_sess_ctx_pre_shutdown and by wolfSSL */
  8509. ExpectIntEQ(twcase_remove_session_called, 1);
  8510. }
  8511. else {
  8512. /* non (D)TLSv1.3 case */
  8513. /* cache hit */
  8514. /* DTLS accesses cache once for stateless parsing and
  8515. * once for stateful parsing */
  8516. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  8517. ExpectIntEQ(twcase_get_session_called, !dtls ? 1 : 2);
  8518. #else
  8519. ExpectIntEQ(twcase_get_session_called, 1);
  8520. #endif
  8521. ExpectIntEQ(twcase_new_session_called, 0);
  8522. /* Called on session added in
  8523. * twcase_server_sess_ctx_pre_shutdown */
  8524. ExpectIntEQ(twcase_remove_session_called, 1);
  8525. }
  8526. break;
  8527. case 3:
  8528. case 4:
  8529. /* no external cache */
  8530. ExpectIntEQ(twcase_get_session_called, 0);
  8531. ExpectIntEQ(twcase_new_session_called, 0);
  8532. ExpectIntEQ(twcase_remove_session_called, 0);
  8533. break;
  8534. }
  8535. wolfSSL_SESSION_free(twcase_client_first_session_ptr);
  8536. wolfSSL_SESSION_free(twcase_server_first_session_ptr);
  8537. wolfSSL_CTX_free(twcase_server_current_ctx_ptr);
  8538. if (EXPECT_FAIL())
  8539. break;
  8540. }
  8541. twcase_get_sessionCb_cleanup();
  8542. XMEMSET(&server_sessionCache.entries, 0,
  8543. sizeof(server_sessionCache.entries));
  8544. fprintf(stderr, "\tEnd %s\n", param->tls_version);
  8545. wc_FreeMutex(&server_sessionCache.htLock);
  8546. return EXPECT_RESULT();
  8547. }
  8548. #endif
  8549. static int test_wolfSSL_CTX_add_session_ext_tls13(void)
  8550. {
  8551. EXPECT_DECLS;
  8552. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8553. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8554. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8555. defined(HAVE_SESSION_TICKET) && \
  8556. !defined(TITAN_SESSION_CACHE) && \
  8557. !defined(HUGE_SESSION_CACHE) && \
  8558. !defined(BIG_SESSION_CACHE) && \
  8559. !defined(MEDIUM_SESSION_CACHE)
  8560. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  8561. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  8562. struct test_add_session_ext_params param[1] = {
  8563. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" }
  8564. };
  8565. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8566. #endif
  8567. #endif
  8568. return EXPECT_RESULT();
  8569. }
  8570. static int test_wolfSSL_CTX_add_session_ext_dtls13(void)
  8571. {
  8572. EXPECT_DECLS;
  8573. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8574. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8575. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8576. defined(HAVE_SESSION_TICKET) && \
  8577. !defined(TITAN_SESSION_CACHE) && \
  8578. !defined(HUGE_SESSION_CACHE) && \
  8579. !defined(BIG_SESSION_CACHE) && \
  8580. !defined(MEDIUM_SESSION_CACHE)
  8581. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  8582. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID)
  8583. #ifdef WOLFSSL_DTLS13
  8584. struct test_add_session_ext_params param[1] = {
  8585. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" }
  8586. };
  8587. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8588. #endif
  8589. #endif
  8590. #endif
  8591. return EXPECT_RESULT();
  8592. }
  8593. static int test_wolfSSL_CTX_add_session_ext_tls12(void)
  8594. {
  8595. EXPECT_DECLS;
  8596. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8597. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8598. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8599. defined(HAVE_SESSION_TICKET) && \
  8600. !defined(TITAN_SESSION_CACHE) && \
  8601. !defined(HUGE_SESSION_CACHE) && \
  8602. !defined(BIG_SESSION_CACHE) && \
  8603. !defined(MEDIUM_SESSION_CACHE)
  8604. #ifndef WOLFSSL_NO_TLS12
  8605. struct test_add_session_ext_params param[1] = {
  8606. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" }
  8607. };
  8608. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8609. #endif
  8610. #endif
  8611. return EXPECT_RESULT();
  8612. }
  8613. static int test_wolfSSL_CTX_add_session_ext_dtls12(void)
  8614. {
  8615. EXPECT_DECLS;
  8616. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8617. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8618. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8619. defined(HAVE_SESSION_TICKET) && \
  8620. !defined(TITAN_SESSION_CACHE) && \
  8621. !defined(HUGE_SESSION_CACHE) && \
  8622. !defined(BIG_SESSION_CACHE) && \
  8623. !defined(MEDIUM_SESSION_CACHE)
  8624. #ifndef WOLFSSL_NO_TLS12
  8625. #ifdef WOLFSSL_DTLS
  8626. struct test_add_session_ext_params param[1] = {
  8627. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" }
  8628. };
  8629. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8630. #endif
  8631. #endif
  8632. #endif
  8633. return EXPECT_RESULT();
  8634. }
  8635. static int test_wolfSSL_CTX_add_session_ext_tls11(void)
  8636. {
  8637. EXPECT_DECLS;
  8638. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8639. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8640. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8641. defined(HAVE_SESSION_TICKET) && \
  8642. !defined(TITAN_SESSION_CACHE) && \
  8643. !defined(HUGE_SESSION_CACHE) && \
  8644. !defined(BIG_SESSION_CACHE) && \
  8645. !defined(MEDIUM_SESSION_CACHE)
  8646. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  8647. !defined(NO_DES3))
  8648. struct test_add_session_ext_params param[1] = {
  8649. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" }
  8650. };
  8651. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8652. #endif
  8653. #endif
  8654. return EXPECT_RESULT();
  8655. }
  8656. static int test_wolfSSL_CTX_add_session_ext_dtls1(void)
  8657. {
  8658. EXPECT_DECLS;
  8659. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_EXT_CACHE) && \
  8660. defined(WOLFSSL_TLS13) && !defined(NO_SESSION_CACHE) && \
  8661. defined(OPENSSL_EXTRA) && defined(SESSION_CERTS) && \
  8662. defined(HAVE_SESSION_TICKET) && \
  8663. !defined(TITAN_SESSION_CACHE) && \
  8664. !defined(HUGE_SESSION_CACHE) && \
  8665. !defined(BIG_SESSION_CACHE) && \
  8666. !defined(MEDIUM_SESSION_CACHE)
  8667. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  8668. !defined(NO_DES3))
  8669. #ifdef WOLFSSL_DTLS
  8670. struct test_add_session_ext_params param[1] = {
  8671. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" }
  8672. };
  8673. ExpectIntEQ(test_wolfSSL_CTX_add_session_ext(param), TEST_SUCCESS);
  8674. #endif
  8675. #endif
  8676. #endif
  8677. return EXPECT_RESULT();
  8678. }
  8679. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  8680. /* canned export of a session using older version 3 */
  8681. static unsigned char version_3[] = {
  8682. 0xA5, 0xA3, 0x01, 0x88, 0x00, 0x3c, 0x00, 0x01,
  8683. 0x00, 0x00, 0x00, 0x80, 0x0C, 0x00, 0x00, 0x00,
  8684. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  8685. 0x00, 0x01, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00,
  8686. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  8687. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8688. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0, 0x30,
  8689. 0x05, 0x09, 0x0A, 0x01, 0x01, 0x00, 0x0D, 0x05,
  8690. 0xFE, 0xFD, 0x01, 0x25, 0x00, 0x00, 0x00, 0x00,
  8691. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8692. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  8693. 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
  8694. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8695. 0x00, 0x06, 0x00, 0x05, 0x00, 0x06, 0x00, 0x00,
  8696. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
  8697. 0x00, 0x06, 0x00, 0x01, 0x00, 0x07, 0x00, 0x00,
  8698. 0x00, 0x30, 0x00, 0x00, 0x00, 0x10, 0x01, 0x01,
  8699. 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00,
  8700. 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x3F,
  8701. 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
  8702. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8703. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8704. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8705. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8706. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8707. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8708. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8709. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8710. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8711. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8712. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8713. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x05,
  8714. 0x12, 0xCF, 0x22, 0xA1, 0x9F, 0x1C, 0x39, 0x1D,
  8715. 0x31, 0x11, 0x12, 0x1D, 0x11, 0x18, 0x0D, 0x0B,
  8716. 0xF3, 0xE1, 0x4D, 0xDC, 0xB1, 0xF1, 0x39, 0x98,
  8717. 0x91, 0x6C, 0x48, 0xE5, 0xED, 0x11, 0x12, 0xA0,
  8718. 0x00, 0xF2, 0x25, 0x4C, 0x09, 0x26, 0xD1, 0x74,
  8719. 0xDF, 0x23, 0x40, 0x15, 0x6A, 0x42, 0x2A, 0x26,
  8720. 0xA5, 0xAC, 0x56, 0xD5, 0x4A, 0x20, 0xB7, 0xE9,
  8721. 0xEF, 0xEB, 0xAF, 0xA8, 0x1E, 0x23, 0x7C, 0x04,
  8722. 0xAA, 0xA1, 0x6D, 0x92, 0x79, 0x7B, 0xFA, 0x80,
  8723. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8724. 0x0C, 0x79, 0x7B, 0xFA, 0x80, 0x00, 0x00, 0x00,
  8725. 0x00, 0x00, 0x00, 0x00, 0x00, 0xAA, 0xA1, 0x6D,
  8726. 0x92, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8727. 0x00, 0x00, 0x10, 0x00, 0x20, 0x00, 0x04, 0x00,
  8728. 0x10, 0x00, 0x10, 0x08, 0x02, 0x05, 0x08, 0x01,
  8729. 0x30, 0x28, 0x00, 0x00, 0x0F, 0x00, 0x02, 0x00,
  8730. 0x09, 0x31, 0x32, 0x37, 0x2E, 0x30, 0x2E, 0x30,
  8731. 0x2E, 0x31, 0xED, 0x4F
  8732. };
  8733. #endif /* defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) */
  8734. static int test_wolfSSL_dtls_export(void)
  8735. {
  8736. EXPECT_DECLS;
  8737. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT)
  8738. tcp_ready ready;
  8739. func_args client_args;
  8740. func_args server_args;
  8741. THREAD_TYPE serverThread;
  8742. callback_functions server_cbf;
  8743. callback_functions client_cbf;
  8744. #ifdef WOLFSSL_TIRTOS
  8745. fdOpenSession(Task_self());
  8746. #endif
  8747. InitTcpReady(&ready);
  8748. #if defined(USE_WINDOWS_API)
  8749. /* use RNG to get random port if using windows */
  8750. ready.port = GetRandomPort();
  8751. #endif
  8752. /* set using dtls */
  8753. XMEMSET(&client_args, 0, sizeof(func_args));
  8754. XMEMSET(&server_args, 0, sizeof(func_args));
  8755. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8756. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  8757. server_cbf.method = wolfDTLSv1_2_server_method;
  8758. client_cbf.method = wolfDTLSv1_2_client_method;
  8759. server_args.callbacks = &server_cbf;
  8760. client_args.callbacks = &client_cbf;
  8761. server_args.signal = &ready;
  8762. client_args.signal = &ready;
  8763. start_thread(run_wolfssl_server, &server_args, &serverThread);
  8764. wait_tcp_ready(&server_args);
  8765. run_wolfssl_client(&client_args);
  8766. join_thread(serverThread);
  8767. ExpectTrue(client_args.return_code);
  8768. ExpectTrue(server_args.return_code);
  8769. FreeTcpReady(&ready);
  8770. #ifdef WOLFSSL_TIRTOS
  8771. fdOpenSession(Task_self());
  8772. #endif
  8773. if (EXPECT_SUCCESS()) {
  8774. SOCKET_T sockfd = 0;
  8775. WOLFSSL_CTX* ctx = NULL;
  8776. WOLFSSL* ssl = NULL;
  8777. char msg[64] = "hello wolfssl!";
  8778. char reply[1024];
  8779. int msgSz = (int)XSTRLEN(msg);
  8780. byte *session, *window;
  8781. unsigned int sessionSz = 0;
  8782. unsigned int windowSz = 0;
  8783. #ifndef TEST_IPV6
  8784. struct sockaddr_in peerAddr;
  8785. #else
  8786. struct sockaddr_in6 peerAddr;
  8787. #endif /* TEST_IPV6 */
  8788. int i;
  8789. /* Set ctx to DTLS 1.2 */
  8790. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  8791. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8792. /* test importing version 3 */
  8793. ExpectIntGE(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8794. /* test importing bad length and bad version */
  8795. version_3[2] += 1;
  8796. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8797. version_3[2] -= 1; version_3[1] = 0XA0;
  8798. ExpectIntLT(wolfSSL_dtls_import(ssl, version_3, sizeof(version_3)), 0);
  8799. wolfSSL_free(ssl);
  8800. wolfSSL_CTX_free(ctx);
  8801. /* check storing client state after connection and storing window only */
  8802. #ifdef WOLFSSL_TIRTOS
  8803. fdOpenSession(Task_self());
  8804. #endif
  8805. InitTcpReady(&ready);
  8806. #if defined(USE_WINDOWS_API)
  8807. /* use RNG to get random port if using windows */
  8808. ready.port = GetRandomPort();
  8809. #endif
  8810. /* set using dtls */
  8811. XMEMSET(&server_args, 0, sizeof(func_args));
  8812. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  8813. server_cbf.method = wolfDTLSv1_2_server_method;
  8814. server_cbf.doUdp = 1;
  8815. server_args.callbacks = &server_cbf;
  8816. server_args.argc = 3; /* set loop_count to 3 */
  8817. server_args.signal = &ready;
  8818. start_thread(test_server_nofail, &server_args, &serverThread);
  8819. wait_tcp_ready(&server_args);
  8820. /* create and connect with client */
  8821. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  8822. ExpectIntEQ(WOLFSSL_SUCCESS,
  8823. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  8824. ExpectIntEQ(WOLFSSL_SUCCESS,
  8825. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  8826. ExpectIntEQ(WOLFSSL_SUCCESS,
  8827. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  8828. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 1, 0, NULL);
  8829. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8830. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8831. /* store server information connected too */
  8832. XMEMSET(&peerAddr, 0, sizeof(peerAddr));
  8833. #ifndef TEST_IPV6
  8834. peerAddr.sin_family = AF_INET;
  8835. ExpectIntEQ(XINET_PTON(AF_INET, wolfSSLIP, &peerAddr.sin_addr),1);
  8836. peerAddr.sin_port = XHTONS(server_args.signal->port);
  8837. #else
  8838. peerAddr.sin6_family = AF_INET6;
  8839. ExpectIntEQ(
  8840. XINET_PTON(AF_INET6, wolfSSLIP, &peerAddr.sin6_addr),1);
  8841. peerAddr.sin6_port = XHTONS(server_args.signal->port);
  8842. #endif
  8843. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8844. WOLFSSL_SUCCESS);
  8845. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  8846. ExpectIntEQ(wolfSSL_dtls_export(ssl, NULL, &sessionSz), 0);
  8847. session = (byte*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8848. ExpectIntGT(wolfSSL_dtls_export(ssl, session, &sessionSz), 0);
  8849. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8850. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8851. ExpectIntEQ(wolfSSL_dtls_export_state_only(ssl, NULL, &windowSz), 0);
  8852. window = (byte*)XMALLOC(windowSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8853. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8854. wolfSSL_free(ssl);
  8855. for (i = 1; EXPECT_SUCCESS() && i < server_args.argc; i++) {
  8856. /* restore state */
  8857. ExpectNotNull(ssl = wolfSSL_new(ctx));
  8858. ExpectIntGT(wolfSSL_dtls_import(ssl, session, sessionSz), 0);
  8859. ExpectIntGT(wolfSSL_dtls_import(ssl, window, windowSz), 0);
  8860. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  8861. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl, &peerAddr, sizeof(peerAddr)),
  8862. WOLFSSL_SUCCESS);
  8863. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  8864. ExpectIntGE(wolfSSL_read(ssl, reply, sizeof(reply)), 0);
  8865. ExpectIntGT(wolfSSL_dtls_export_state_only(ssl, window, &windowSz), 0);
  8866. wolfSSL_free(ssl);
  8867. }
  8868. XFREE(session, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8869. XFREE(window, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  8870. wolfSSL_CTX_free(ctx);
  8871. fprintf(stderr, "done and waiting for server\n");
  8872. join_thread(serverThread);
  8873. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  8874. FreeTcpReady(&ready);
  8875. #ifdef WOLFSSL_TIRTOS
  8876. fdOpenSession(Task_self());
  8877. #endif
  8878. }
  8879. #endif
  8880. return EXPECT_RESULT();
  8881. }
  8882. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  8883. #ifdef WOLFSSL_TLS13
  8884. static const byte canned_client_tls13_session[] = {
  8885. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x00, 0x00,
  8886. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8887. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x01,
  8888. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8889. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8890. 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8891. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8892. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x09, 0x00,
  8893. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8894. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8895. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8896. 0x01, 0x00, 0x00, 0x00, 0x27, 0x00, 0x00, 0x00,
  8897. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8898. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8899. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8900. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8901. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8902. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8903. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8904. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8905. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8906. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8907. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8908. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8909. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8910. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8911. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8912. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8913. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8914. 0x35, 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0,
  8915. 0x6F, 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A,
  8916. 0xA0, 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06,
  8917. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8918. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8919. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8920. 0x00, 0x03
  8921. };
  8922. static const byte canned_server_tls13_session[] = {
  8923. 0xA7, 0xA4, 0x01, 0x18, 0x00, 0x41, 0x01, 0x00,
  8924. 0x01, 0x00, 0x00, 0x80, 0x04, 0x00, 0x00, 0x00,
  8925. 0x00, 0x80, 0x00, 0x1C, 0x01, 0x00, 0x00, 0x00,
  8926. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8927. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8928. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  8929. 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x13,
  8930. 0x01, 0x0A, 0x0F, 0x10, 0x01, 0x02, 0x00, 0x0F,
  8931. 0x05, 0x00, 0x00, 0x00, 0x00, 0x03, 0x04, 0x00,
  8932. 0xB7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8933. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8934. 0x02, 0x00, 0x00, 0x00, 0x17, 0x00, 0x00, 0x00,
  8935. 0x11, 0x01, 0x01, 0x00, 0x20, 0x84, 0x4F, 0x18,
  8936. 0xD8, 0xC1, 0x24, 0xD8, 0xBB, 0x17, 0x9E, 0x31,
  8937. 0xA3, 0xF8, 0xA7, 0x3C, 0xBA, 0xEC, 0xFA, 0xB4,
  8938. 0x7F, 0xC5, 0x78, 0xEB, 0x6D, 0xE3, 0x2B, 0x7B,
  8939. 0x94, 0xBE, 0x20, 0x11, 0x7E, 0x17, 0x10, 0xA7,
  8940. 0x10, 0x19, 0xEC, 0x62, 0xCC, 0xBE, 0xF5, 0x01,
  8941. 0x35, 0x3C, 0xEA, 0xEF, 0x44, 0x3C, 0x40, 0xA2,
  8942. 0xBC, 0x18, 0x43, 0xA1, 0xA1, 0x65, 0x5C, 0x48,
  8943. 0xE2, 0xF9, 0x38, 0xEB, 0x11, 0x10, 0x72, 0x7C,
  8944. 0x78, 0x22, 0x13, 0x3B, 0x19, 0x40, 0xF0, 0x73,
  8945. 0xBE, 0x96, 0x14, 0x78, 0x26, 0xB9, 0x6B, 0x2E,
  8946. 0x72, 0x22, 0x0D, 0x90, 0x94, 0xDD, 0x78, 0x77,
  8947. 0xFC, 0x0C, 0x2E, 0x63, 0x6E, 0xF0, 0x0C, 0x35,
  8948. 0x41, 0xCD, 0xF3, 0x49, 0x31, 0x08, 0xD0, 0x6F,
  8949. 0x02, 0x3D, 0xC1, 0xD3, 0xB7, 0xEE, 0x3A, 0xA0,
  8950. 0x8E, 0xA1, 0x4D, 0xC3, 0x2E, 0x5E, 0x06, 0x00,
  8951. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8952. 0xD3, 0xB7, 0xEE, 0x3A, 0xA0, 0x8E, 0xA1, 0x4D,
  8953. 0xC3, 0x2E, 0x5E, 0x06, 0x35, 0x41, 0xCD, 0xF3,
  8954. 0x49, 0x31, 0x08, 0xD0, 0x6F, 0x02, 0x3D, 0xC1,
  8955. 0x00, 0x10, 0x00, 0x10, 0x00, 0x0C, 0x00, 0x10,
  8956. 0x00, 0x10, 0x07, 0x02, 0x04, 0x00, 0x00, 0x20,
  8957. 0x28, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  8958. 0x00, 0x04
  8959. };
  8960. #endif /* WOLFSSL_TLS13 */
  8961. static const byte canned_client_session[] = {
  8962. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  8963. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  8964. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x01,
  8965. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  8966. 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
  8967. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8968. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  8969. 0x27, 0x0A, 0x0D, 0x10, 0x01, 0x01, 0x0A, 0x00,
  8970. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  8971. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8972. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8973. 0x02, 0x00, 0x00, 0x00, 0x50, 0x00, 0x00, 0x00,
  8974. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  8975. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  8976. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  8977. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  8978. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  8979. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  8980. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  8981. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  8982. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  8983. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  8984. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  8985. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  8986. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  8987. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  8988. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  8989. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  8990. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  8991. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  8992. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8993. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8994. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  8995. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  8996. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  8997. 0x28, 0x00, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  8998. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  8999. 0x43, 0xF3, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  9000. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  9001. 0x14, 0x63, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  9002. 0x00, 0x03
  9003. };
  9004. static const byte canned_server_session[] = {
  9005. 0xA7, 0xA4, 0x01, 0x40, 0x00, 0x41, 0x00, 0x00,
  9006. 0x00, 0x00, 0x00, 0x80, 0x02, 0x00, 0x00, 0x00,
  9007. 0x00, 0x80, 0x00, 0x1C, 0x00, 0x00, 0x00, 0x00,
  9008. 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
  9009. 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
  9010. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9011. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xC0,
  9012. 0x27, 0x08, 0x0F, 0x10, 0x01, 0x01, 0x00, 0x11,
  9013. 0x05, 0x00, 0x01, 0x01, 0x01, 0x03, 0x03, 0x00,
  9014. 0xBF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9015. 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9016. 0x02, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00,
  9017. 0x0A, 0x01, 0x01, 0x00, 0x20, 0x69, 0x11, 0x6D,
  9018. 0x97, 0x15, 0x6E, 0x52, 0x27, 0xD6, 0x1D, 0x1D,
  9019. 0xF5, 0x0D, 0x59, 0xA5, 0xAC, 0x2E, 0x8C, 0x0E,
  9020. 0xCB, 0x26, 0x1E, 0xE2, 0xCE, 0xBB, 0xCE, 0xE1,
  9021. 0x7D, 0xD7, 0xEF, 0xA5, 0x44, 0x80, 0x2A, 0xDE,
  9022. 0xBB, 0x75, 0xB0, 0x1D, 0x75, 0x17, 0x20, 0x4C,
  9023. 0x08, 0x05, 0x1B, 0xBA, 0x60, 0x1F, 0x6C, 0x91,
  9024. 0x8C, 0xAA, 0xBB, 0xE5, 0xA3, 0x0B, 0x12, 0x3E,
  9025. 0xC0, 0x35, 0x43, 0x1D, 0xE2, 0x10, 0xE2, 0x02,
  9026. 0x92, 0x4B, 0x8F, 0x05, 0xA9, 0x4B, 0xCC, 0x90,
  9027. 0xC3, 0x0E, 0xC2, 0x0F, 0xE9, 0x33, 0x85, 0x9B,
  9028. 0x3C, 0x19, 0x21, 0xD5, 0x62, 0xE5, 0xE1, 0x17,
  9029. 0x8F, 0x8C, 0x19, 0x52, 0xD8, 0x59, 0x10, 0x2D,
  9030. 0x20, 0x6F, 0xBA, 0xC1, 0x1C, 0xD1, 0x82, 0xC7,
  9031. 0x32, 0x1B, 0xBB, 0xCC, 0x30, 0x03, 0xD7, 0x3A,
  9032. 0xC8, 0x18, 0xED, 0x58, 0xC8, 0x11, 0xFE, 0x71,
  9033. 0x9C, 0x71, 0xD8, 0x6B, 0xE0, 0x25, 0x64, 0x00,
  9034. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C,
  9035. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9036. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9037. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  9038. 0x00, 0x10, 0x00, 0x10, 0x00, 0x10, 0x00, 0x10,
  9039. 0x00, 0x00, 0x06, 0x01, 0x04, 0x08, 0x01, 0x20,
  9040. 0x28, 0x00, 0xC5, 0x8F, 0xA1, 0x59, 0x93, 0xEF,
  9041. 0x7E, 0xD3, 0xD0, 0xB5, 0x87, 0x1D, 0x81, 0x54,
  9042. 0x14, 0x63, 0x09, 0xE1, 0x50, 0x70, 0x02, 0x2F,
  9043. 0x7E, 0xDA, 0xBD, 0x40, 0xC5, 0x58, 0x87, 0xCE,
  9044. 0x43, 0xF3, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
  9045. 0x00, 0x04
  9046. };
  9047. static THREAD_RETURN WOLFSSL_THREAD tls_export_server(void* args)
  9048. {
  9049. SOCKET_T sockfd = 0;
  9050. SOCKET_T clientfd = 0;
  9051. word16 port;
  9052. callback_functions* cbf;
  9053. WOLFSSL_CTX* ctx = 0;
  9054. WOLFSSL* ssl = 0;
  9055. char msg[] = "I hear you fa shizzle!";
  9056. char input[1024];
  9057. int idx;
  9058. #ifdef WOLFSSL_TIRTOS
  9059. fdOpenSession(Task_self());
  9060. #endif
  9061. ((func_args*)args)->return_code = TEST_FAIL;
  9062. cbf = ((func_args*)args)->callbacks;
  9063. #if defined(USE_WINDOWS_API)
  9064. port = ((func_args*)args)->signal->port;
  9065. #elif defined(NO_MAIN_DRIVER) && !defined(WOLFSSL_SNIFFER) && \
  9066. !defined(WOLFSSL_MDK_SHELL) && !defined(WOLFSSL_TIRTOS)
  9067. /* Let tcp_listen assign port */
  9068. port = 0;
  9069. #else
  9070. /* Use default port */
  9071. port = wolfSSLPort;
  9072. #endif
  9073. /* do it here to detect failure */
  9074. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  9075. CloseSocket(sockfd);
  9076. {
  9077. WOLFSSL_METHOD* method = NULL;
  9078. if (cbf != NULL && cbf->method != NULL) {
  9079. method = cbf->method();
  9080. }
  9081. else {
  9082. method = wolfTLSv1_2_server_method();
  9083. }
  9084. ctx = wolfSSL_CTX_new(method);
  9085. }
  9086. if (ctx == NULL) {
  9087. goto done;
  9088. }
  9089. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  9090. /* call ctx setup callback */
  9091. if (cbf != NULL && cbf->ctx_ready != NULL) {
  9092. cbf->ctx_ready(ctx);
  9093. }
  9094. ssl = wolfSSL_new(ctx);
  9095. if (ssl == NULL) {
  9096. goto done;
  9097. }
  9098. wolfSSL_set_fd(ssl, clientfd);
  9099. /* call ssl setup callback */
  9100. if (cbf != NULL && cbf->ssl_ready != NULL) {
  9101. cbf->ssl_ready(ssl);
  9102. }
  9103. idx = wolfSSL_read(ssl, input, sizeof(input)-1);
  9104. if (idx > 0) {
  9105. input[idx] = '\0';
  9106. fprintf(stderr, "Client message export/import: %s\n", input);
  9107. }
  9108. else {
  9109. fprintf(stderr, "ret = %d error = %d\n", idx,
  9110. wolfSSL_get_error(ssl, idx));
  9111. goto done;
  9112. }
  9113. if (wolfSSL_write(ssl, msg, sizeof(msg)) != sizeof(msg)) {
  9114. /*err_sys("SSL_write failed");*/
  9115. WOLFSSL_RETURN_FROM_THREAD(0);
  9116. }
  9117. #ifdef WOLFSSL_TIRTOS
  9118. Task_yield();
  9119. #endif
  9120. ((func_args*)args)->return_code = TEST_SUCCESS;
  9121. done:
  9122. wolfSSL_shutdown(ssl);
  9123. wolfSSL_free(ssl);
  9124. wolfSSL_CTX_free(ctx);
  9125. CloseSocket(clientfd);
  9126. #ifdef WOLFSSL_TIRTOS
  9127. fdCloseSession(Task_self());
  9128. #endif
  9129. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  9130. && defined(HAVE_THREAD_LS)
  9131. wc_ecc_fp_free(); /* free per thread cache */
  9132. #endif
  9133. #if defined(HAVE_SESSION_TICKET) && \
  9134. ((defined(HAVE_CHACHA) && defined(HAVE_POLY1305)) || defined(HAVE_AESGCM))
  9135. #if defined(OPENSSL_EXTRA) && defined(HAVE_AESGCM)
  9136. OpenSSLTicketCleanup();
  9137. #elif defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  9138. TicketCleanup();
  9139. #endif
  9140. #endif
  9141. WOLFSSL_RETURN_FROM_THREAD(0);
  9142. }
  9143. static void load_tls12_canned_server(WOLFSSL* ssl)
  9144. {
  9145. int clientfd = wolfSSL_get_fd(ssl);
  9146. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_session,
  9147. sizeof(canned_server_session)), sizeof(canned_server_session));
  9148. wolfSSL_set_fd(ssl, clientfd);
  9149. }
  9150. #ifdef WOLFSSL_TLS13
  9151. static void load_tls13_canned_server(WOLFSSL* ssl)
  9152. {
  9153. int clientfd = wolfSSL_get_fd(ssl);
  9154. AssertIntEQ(wolfSSL_tls_import(ssl, canned_server_tls13_session,
  9155. sizeof(canned_server_tls13_session)),
  9156. sizeof(canned_server_tls13_session));
  9157. wolfSSL_set_fd(ssl, clientfd);
  9158. }
  9159. #endif
  9160. /* v is for version WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  9161. static int test_wolfSSL_tls_export_run(int v)
  9162. {
  9163. EXPECT_DECLS;
  9164. SOCKET_T sockfd = 0;
  9165. WOLFSSL_CTX* ctx = 0;
  9166. WOLFSSL* ssl = 0;
  9167. char msg[64] = "hello wolfssl!";
  9168. char reply[1024];
  9169. word32 replySz;
  9170. int msgSz = (int)XSTRLEN(msg);
  9171. const byte* clientSession = NULL;
  9172. int clientSessionSz = 0;
  9173. tcp_ready ready;
  9174. func_args server_args;
  9175. THREAD_TYPE serverThread;
  9176. callback_functions server_cbf;
  9177. #ifdef WOLFSSL_TIRTOS
  9178. fdOpenSession(Task_self());
  9179. #endif
  9180. InitTcpReady(&ready);
  9181. #if defined(USE_WINDOWS_API)
  9182. /* use RNG to get random port if using windows */
  9183. ready.port = GetRandomPort();
  9184. #endif
  9185. XMEMSET(&server_args, 0, sizeof(func_args));
  9186. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  9187. switch (v) {
  9188. case WOLFSSL_TLSV1_2:
  9189. server_cbf.method = wolfTLSv1_2_server_method;
  9190. server_cbf.ssl_ready = load_tls12_canned_server;
  9191. /* setup the client side */
  9192. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  9193. wolfSSL_CTX_set_cipher_list(ctx, "ECDHE-RSA-AES128-SHA256");
  9194. clientSession = canned_client_session;
  9195. clientSessionSz = sizeof(canned_client_session);
  9196. break;
  9197. #ifdef WOLFSSL_TLS13
  9198. case WOLFSSL_TLSV1_3:
  9199. server_cbf.method = wolfTLSv1_3_server_method;
  9200. server_cbf.ssl_ready = load_tls13_canned_server;
  9201. /* setup the client side */
  9202. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  9203. clientSession = canned_client_tls13_session;
  9204. clientSessionSz = sizeof(canned_client_tls13_session);
  9205. break;
  9206. #endif
  9207. }
  9208. server_args.callbacks = &server_cbf;
  9209. server_args.signal = &ready;
  9210. start_thread(tls_export_server, &server_args, &serverThread);
  9211. wait_tcp_ready(&server_args);
  9212. #ifdef WOLFSSL_TIRTOS
  9213. fdOpenSession(Task_self());
  9214. #endif
  9215. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9216. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  9217. ExpectIntEQ(wolfSSL_tls_import(ssl, clientSession, clientSessionSz),
  9218. clientSessionSz);
  9219. replySz = sizeof(reply);
  9220. ExpectIntGT(wolfSSL_tls_export(ssl, (byte*)reply, &replySz), 0);
  9221. #if !defined(NO_PSK) && defined(HAVE_ANON)
  9222. /* index 20 has is setting if PSK was on and 49 is if anon is allowed */
  9223. ExpectIntEQ(XMEMCMP(reply, clientSession, replySz), 0);
  9224. #endif
  9225. wolfSSL_set_fd(ssl, sockfd);
  9226. ExpectIntEQ(wolfSSL_write(ssl, msg, msgSz), msgSz);
  9227. ExpectIntGT(wolfSSL_read(ssl, reply, sizeof(reply)-1), 0);
  9228. wolfSSL_free(ssl);
  9229. wolfSSL_CTX_free(ctx);
  9230. CloseSocket(sockfd);
  9231. #ifdef WOLFSSL_TIRTOS
  9232. fdCloseSession(Task_self());
  9233. #endif
  9234. #if defined(NO_MAIN_DRIVER) && defined(HAVE_ECC) && defined(FP_ECC) \
  9235. && defined(HAVE_THREAD_LS)
  9236. wc_ecc_fp_free(); /* free per thread cache */
  9237. #endif
  9238. join_thread(serverThread);
  9239. ExpectIntEQ(server_args.return_code, TEST_SUCCESS);
  9240. FreeTcpReady(&ready);
  9241. #ifdef WOLFSSL_TIRTOS
  9242. fdOpenSession(Task_self());
  9243. #endif
  9244. return EXPECT_RESULT();
  9245. }
  9246. #endif
  9247. static int test_wolfSSL_tls_export(void)
  9248. {
  9249. int res = TEST_SKIPPED;
  9250. #if defined(WOLFSSL_SESSION_EXPORT) && !defined(WOLFSSL_NO_TLS12)
  9251. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_2);
  9252. #ifdef WOLFSSL_TLS13
  9253. test_wolfSSL_tls_export_run(WOLFSSL_TLSV1_3);
  9254. #endif
  9255. res = TEST_RES_CHECK(1);
  9256. #endif
  9257. return res;
  9258. }
  9259. /*----------------------------------------------------------------------------*
  9260. | TLS extensions tests
  9261. *----------------------------------------------------------------------------*/
  9262. #ifdef ENABLE_TLS_CALLBACK_TEST
  9263. /* Connection test runner - generic */
  9264. static void test_wolfSSL_client_server(callback_functions* client_callbacks,
  9265. callback_functions* server_callbacks)
  9266. {
  9267. tcp_ready ready;
  9268. func_args client_args;
  9269. func_args server_args;
  9270. THREAD_TYPE serverThread;
  9271. XMEMSET(&client_args, 0, sizeof(func_args));
  9272. XMEMSET(&server_args, 0, sizeof(func_args));
  9273. StartTCP();
  9274. client_args.callbacks = client_callbacks;
  9275. server_args.callbacks = server_callbacks;
  9276. #ifdef WOLFSSL_TIRTOS
  9277. fdOpenSession(Task_self());
  9278. #endif
  9279. /* RUN Server side */
  9280. InitTcpReady(&ready);
  9281. #if defined(USE_WINDOWS_API)
  9282. /* use RNG to get random port if using windows */
  9283. ready.port = GetRandomPort();
  9284. #endif
  9285. server_args.signal = &ready;
  9286. client_args.signal = &ready;
  9287. start_thread(run_wolfssl_server, &server_args, &serverThread);
  9288. wait_tcp_ready(&server_args);
  9289. /* RUN Client side */
  9290. run_wolfssl_client(&client_args);
  9291. join_thread(serverThread);
  9292. FreeTcpReady(&ready);
  9293. #ifdef WOLFSSL_TIRTOS
  9294. fdCloseSession(Task_self());
  9295. #endif
  9296. client_callbacks->return_code = client_args.return_code;
  9297. server_callbacks->return_code = server_args.return_code;
  9298. }
  9299. #endif /* ENABLE_TLS_CALLBACK_TEST */
  9300. #ifdef HAVE_SNI
  9301. static int test_wolfSSL_UseSNI_params(void)
  9302. {
  9303. EXPECT_DECLS;
  9304. #if !defined(NO_WOLFSSL_CLIENT)
  9305. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9306. WOLFSSL *ssl = wolfSSL_new(ctx);
  9307. ExpectNotNull(ctx);
  9308. ExpectNotNull(ssl);
  9309. /* invalid [ctx|ssl] */
  9310. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(NULL, 0, "ctx", 3));
  9311. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( NULL, 0, "ssl", 3));
  9312. /* invalid type */
  9313. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, -1, "ctx", 3));
  9314. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, -1, "ssl", 3));
  9315. /* invalid data */
  9316. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, NULL, 3));
  9317. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, NULL, 3));
  9318. /* success case */
  9319. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSNI(ctx, 0, "ctx", 3));
  9320. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI( ssl, 0, "ssl", 3));
  9321. wolfSSL_free(ssl);
  9322. wolfSSL_CTX_free(ctx);
  9323. #endif /* !NO_WOLFSSL_CLIENT */
  9324. return EXPECT_RESULT();
  9325. }
  9326. /* BEGIN of connection tests callbacks */
  9327. static void use_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9328. {
  9329. AssertIntEQ(WOLFSSL_SUCCESS,
  9330. wolfSSL_CTX_UseSNI(ctx, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  9331. }
  9332. static void use_SNI_at_ssl(WOLFSSL* ssl)
  9333. {
  9334. AssertIntEQ(WOLFSSL_SUCCESS,
  9335. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "www.wolfssl.com", 15));
  9336. }
  9337. static void different_SNI_at_ssl(WOLFSSL* ssl)
  9338. {
  9339. AssertIntEQ(WOLFSSL_SUCCESS,
  9340. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, "ww2.wolfssl.com", 15));
  9341. }
  9342. static void use_SNI_WITH_CONTINUE_at_ssl(WOLFSSL* ssl)
  9343. {
  9344. use_SNI_at_ssl(ssl);
  9345. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9346. WOLFSSL_SNI_CONTINUE_ON_MISMATCH);
  9347. }
  9348. static void use_SNI_WITH_FAKE_ANSWER_at_ssl(WOLFSSL* ssl)
  9349. {
  9350. use_SNI_at_ssl(ssl);
  9351. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9352. WOLFSSL_SNI_ANSWER_ON_MISMATCH);
  9353. }
  9354. static void use_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9355. {
  9356. use_SNI_at_ctx(ctx);
  9357. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  9358. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9359. }
  9360. static void use_MANDATORY_SNI_at_ssl(WOLFSSL* ssl)
  9361. {
  9362. use_SNI_at_ssl(ssl);
  9363. wolfSSL_SNI_SetOptions(ssl, WOLFSSL_SNI_HOST_NAME,
  9364. WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9365. }
  9366. static void use_PSEUDO_MANDATORY_SNI_at_ctx(WOLFSSL_CTX* ctx)
  9367. {
  9368. use_SNI_at_ctx(ctx);
  9369. wolfSSL_CTX_SNI_SetOptions(ctx, WOLFSSL_SNI_HOST_NAME,
  9370. WOLFSSL_SNI_ANSWER_ON_MISMATCH | WOLFSSL_SNI_ABORT_ON_ABSENCE);
  9371. }
  9372. static void verify_UNKNOWN_SNI_on_server(WOLFSSL* ssl)
  9373. {
  9374. AssertIntEQ(UNKNOWN_SNI_HOST_NAME_E, wolfSSL_get_error(ssl, 0));
  9375. }
  9376. static void verify_SNI_ABSENT_on_server(WOLFSSL* ssl)
  9377. {
  9378. AssertIntEQ(SNI_ABSENT_ERROR, wolfSSL_get_error(ssl, 0));
  9379. }
  9380. static void verify_SNI_no_matching(WOLFSSL* ssl)
  9381. {
  9382. byte type = WOLFSSL_SNI_HOST_NAME;
  9383. void* request = (void*) &type; /* to be overwritten */
  9384. AssertIntEQ(WOLFSSL_SNI_NO_MATCH, wolfSSL_SNI_Status(ssl, type));
  9385. AssertNotNull(request);
  9386. AssertIntEQ(0, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9387. AssertNull(request);
  9388. }
  9389. static void verify_SNI_real_matching(WOLFSSL* ssl)
  9390. {
  9391. byte type = WOLFSSL_SNI_HOST_NAME;
  9392. void* request = NULL;
  9393. AssertIntEQ(WOLFSSL_SNI_REAL_MATCH, wolfSSL_SNI_Status(ssl, type));
  9394. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9395. AssertNotNull(request);
  9396. AssertStrEQ("www.wolfssl.com", (char*)request);
  9397. }
  9398. static void verify_SNI_fake_matching(WOLFSSL* ssl)
  9399. {
  9400. byte type = WOLFSSL_SNI_HOST_NAME;
  9401. void* request = NULL;
  9402. AssertIntEQ(WOLFSSL_SNI_FAKE_MATCH, wolfSSL_SNI_Status(ssl, type));
  9403. AssertIntEQ(15, wolfSSL_SNI_GetRequest(ssl, type, &request));
  9404. AssertNotNull(request);
  9405. AssertStrEQ("ww2.wolfssl.com", (char*)request);
  9406. }
  9407. static void verify_FATAL_ERROR_on_client(WOLFSSL* ssl)
  9408. {
  9409. AssertIntEQ(FATAL_ERROR, wolfSSL_get_error(ssl, 0));
  9410. }
  9411. /* END of connection tests callbacks */
  9412. static int test_wolfSSL_UseSNI_connection(void)
  9413. {
  9414. int res = TEST_SKIPPED;
  9415. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  9416. callback_functions client_cb;
  9417. callback_functions server_cb;
  9418. size_t i;
  9419. #ifdef WOLFSSL_STATIC_MEMORY
  9420. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  9421. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  9422. #endif
  9423. struct {
  9424. method_provider client_meth;
  9425. method_provider server_meth;
  9426. #ifdef WOLFSSL_STATIC_MEMORY
  9427. wolfSSL_method_func client_meth_ex;
  9428. wolfSSL_method_func server_meth_ex;
  9429. #endif
  9430. } methods[] = {
  9431. #if defined(WOLFSSL_NO_TLS12) && !defined(WOLFSSL_TLS13)
  9432. {wolfSSLv23_client_method, wolfSSLv23_server_method
  9433. #ifdef WOLFSSL_STATIC_MEMORY
  9434. ,wolfSSLv23_client_method_ex, wolfSSLv23_server_method_ex
  9435. #endif
  9436. },
  9437. #endif
  9438. #ifndef WOLFSSL_NO_TLS12
  9439. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method
  9440. #ifdef WOLFSSL_STATIC_MEMORY
  9441. ,wolfTLSv1_2_client_method_ex, wolfTLSv1_2_server_method_ex
  9442. #endif
  9443. },
  9444. #endif
  9445. #ifdef WOLFSSL_TLS13
  9446. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method
  9447. #ifdef WOLFSSL_STATIC_MEMORY
  9448. ,wolfTLSv1_3_client_method_ex, wolfTLSv1_3_server_method_ex
  9449. #endif
  9450. },
  9451. #endif
  9452. };
  9453. size_t methodsSz = sizeof(methods) / sizeof(*methods);
  9454. for (i = 0; i < methodsSz; i++) {
  9455. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  9456. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  9457. client_cb.method = methods[i].client_meth;
  9458. server_cb.method = methods[i].server_meth;
  9459. client_cb.devId = testDevId;
  9460. server_cb.devId = testDevId;
  9461. #ifdef WOLFSSL_STATIC_MEMORY
  9462. client_cb.method_ex = methods[i].client_meth_ex;
  9463. server_cb.method_ex = methods[i].server_meth_ex;
  9464. client_cb.mem = cliMem;
  9465. client_cb.memSz = (word32)sizeof(cliMem);
  9466. server_cb.mem = svrMem;
  9467. server_cb.memSz = (word32)sizeof(svrMem);;
  9468. #endif
  9469. /* success case at ctx */
  9470. fprintf(stderr, "\n\tsuccess case at ctx\n");
  9471. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9472. server_cb.ctx_ready = use_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  9473. test_wolfSSL_client_server(&client_cb, &server_cb);
  9474. /* success case at ssl */
  9475. fprintf(stderr, "\tsuccess case at ssl\n");
  9476. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_SNI_at_ssl; client_cb.on_result = verify_SNI_real_matching;
  9477. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_SNI_real_matching;
  9478. test_wolfSSL_client_server(&client_cb, &server_cb);
  9479. /* default mismatch behavior */
  9480. fprintf(stderr, "\tdefault mismatch behavior\n");
  9481. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9482. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_at_ssl; server_cb.on_result = verify_UNKNOWN_SNI_on_server;
  9483. test_wolfSSL_client_server(&client_cb, &server_cb);
  9484. /* continue on mismatch */
  9485. fprintf(stderr, "\tcontinue on mismatch\n");
  9486. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9487. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_CONTINUE_at_ssl; server_cb.on_result = verify_SNI_no_matching;
  9488. test_wolfSSL_client_server(&client_cb, &server_cb);
  9489. /* fake answer on mismatch */
  9490. fprintf(stderr, "\tfake answer on mismatch\n");
  9491. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9492. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_SNI_WITH_FAKE_ANSWER_at_ssl; server_cb.on_result = verify_SNI_fake_matching;
  9493. test_wolfSSL_client_server(&client_cb, &server_cb);
  9494. /* sni abort - success */
  9495. fprintf(stderr, "\tsni abort - success\n");
  9496. client_cb.ctx_ready = use_SNI_at_ctx; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9497. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_real_matching;
  9498. test_wolfSSL_client_server(&client_cb, &server_cb);
  9499. /* sni abort - abort when absent (ctx) */
  9500. fprintf(stderr, "\tsni abort - abort when absent (ctx)\n");
  9501. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9502. server_cb.ctx_ready = use_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_ABSENT_on_server;
  9503. test_wolfSSL_client_server(&client_cb, &server_cb);
  9504. /* sni abort - abort when absent (ssl) */
  9505. fprintf(stderr, "\tsni abort - abort when absent (ssl)\n");
  9506. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = verify_FATAL_ERROR_on_client;
  9507. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_MANDATORY_SNI_at_ssl; server_cb.on_result = verify_SNI_ABSENT_on_server;
  9508. test_wolfSSL_client_server(&client_cb, &server_cb);
  9509. /* sni abort - success when overwritten */
  9510. fprintf(stderr, "\tsni abort - success when overwritten\n");
  9511. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  9512. 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;
  9513. test_wolfSSL_client_server(&client_cb, &server_cb);
  9514. /* sni abort - success when allowing mismatches */
  9515. fprintf(stderr, "\tsni abort - success when allowing mismatches\n");
  9516. client_cb.ctx_ready = NULL; client_cb.ssl_ready = different_SNI_at_ssl; client_cb.on_result = NULL;
  9517. server_cb.ctx_ready = use_PSEUDO_MANDATORY_SNI_at_ctx; server_cb.ssl_ready = NULL; server_cb.on_result = verify_SNI_fake_matching;
  9518. test_wolfSSL_client_server(&client_cb, &server_cb);
  9519. }
  9520. res = TEST_RES_CHECK(1);
  9521. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  9522. return res;
  9523. }
  9524. static int test_wolfSSL_SNI_GetFromBuffer(void)
  9525. {
  9526. EXPECT_DECLS;
  9527. byte buff[] = { /* www.paypal.com */
  9528. 0x00, 0x00, 0x00, 0x00, 0xff, 0x01, 0x00, 0x00, 0x60, 0x03, 0x03, 0x5c,
  9529. 0xc4, 0xb3, 0x8c, 0x87, 0xef, 0xa4, 0x09, 0xe0, 0x02, 0xab, 0x86, 0xca,
  9530. 0x76, 0xf0, 0x9e, 0x01, 0x65, 0xf6, 0xa6, 0x06, 0x13, 0x1d, 0x0f, 0xa5,
  9531. 0x79, 0xb0, 0xd4, 0x77, 0x22, 0xeb, 0x1a, 0x00, 0x00, 0x16, 0x00, 0x6b,
  9532. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  9533. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x21,
  9534. 0x00, 0x00, 0x00, 0x13, 0x00, 0x11, 0x00, 0x00, 0x0e, 0x77, 0x77, 0x77,
  9535. 0x2e, 0x70, 0x61, 0x79, 0x70, 0x61, 0x6c, 0x2e, 0x63, 0x6f, 0x6d, 0x00,
  9536. 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  9537. };
  9538. byte buff2[] = { /* api.textmate.org */
  9539. 0x16, 0x03, 0x01, 0x00, 0xc6, 0x01, 0x00, 0x00, 0xc2, 0x03, 0x03, 0x52,
  9540. 0x8b, 0x7b, 0xca, 0x69, 0xec, 0x97, 0xd5, 0x08, 0x03, 0x50, 0xfe, 0x3b,
  9541. 0x99, 0xc3, 0x20, 0xce, 0xa5, 0xf6, 0x99, 0xa5, 0x71, 0xf9, 0x57, 0x7f,
  9542. 0x04, 0x38, 0xf6, 0x11, 0x0b, 0xb8, 0xd3, 0x00, 0x00, 0x5e, 0x00, 0xff,
  9543. 0xc0, 0x24, 0xc0, 0x23, 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x07, 0xc0, 0x08,
  9544. 0xc0, 0x28, 0xc0, 0x27, 0xc0, 0x14, 0xc0, 0x13, 0xc0, 0x11, 0xc0, 0x12,
  9545. 0xc0, 0x26, 0xc0, 0x25, 0xc0, 0x2a, 0xc0, 0x29, 0xc0, 0x05, 0xc0, 0x04,
  9546. 0xc0, 0x02, 0xc0, 0x03, 0xc0, 0x0f, 0xc0, 0x0e, 0xc0, 0x0c, 0xc0, 0x0d,
  9547. 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x35,
  9548. 0x00, 0x0a, 0x00, 0x67, 0x00, 0x6b, 0x00, 0x33, 0x00, 0x39, 0x00, 0x16,
  9549. 0x00, 0xaf, 0x00, 0xae, 0x00, 0x8d, 0x00, 0x8c, 0x00, 0x8a, 0x00, 0x8b,
  9550. 0x00, 0xb1, 0x00, 0xb0, 0x00, 0x2c, 0x00, 0x3b, 0x01, 0x00, 0x00, 0x3b,
  9551. 0x00, 0x00, 0x00, 0x15, 0x00, 0x13, 0x00, 0x00, 0x10, 0x61, 0x70, 0x69,
  9552. 0x2e, 0x74, 0x65, 0x78, 0x74, 0x6d, 0x61, 0x74, 0x65, 0x2e, 0x6f, 0x72,
  9553. 0x67, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00, 0x18, 0x00,
  9554. 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x0d, 0x00, 0x0c, 0x00,
  9555. 0x0a, 0x05, 0x01, 0x04, 0x01, 0x02, 0x01, 0x04, 0x03, 0x02, 0x03
  9556. };
  9557. byte buff3[] = { /* no sni extension */
  9558. 0x16, 0x03, 0x03, 0x00, 0x4d, 0x01, 0x00, 0x00, 0x49, 0x03, 0x03, 0xea,
  9559. 0xa1, 0x9f, 0x60, 0xdd, 0x52, 0x12, 0x13, 0xbd, 0x84, 0x34, 0xd5, 0x1c,
  9560. 0x38, 0x25, 0xa8, 0x97, 0xd2, 0xd5, 0xc6, 0x45, 0xaf, 0x1b, 0x08, 0xe4,
  9561. 0x1e, 0xbb, 0xdf, 0x9d, 0x39, 0xf0, 0x65, 0x00, 0x00, 0x16, 0x00, 0x6b,
  9562. 0x00, 0x67, 0x00, 0x39, 0x00, 0x33, 0x00, 0x3d, 0x00, 0x3c, 0x00, 0x35,
  9563. 0x00, 0x2f, 0x00, 0x05, 0x00, 0x04, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x0a,
  9564. 0x00, 0x0d, 0x00, 0x06, 0x00, 0x04, 0x04, 0x01, 0x02, 0x01
  9565. };
  9566. byte buff4[] = { /* last extension has zero size */
  9567. 0x16, 0x03, 0x01, 0x00, 0xba, 0x01, 0x00, 0x00,
  9568. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  9569. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  9570. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  9571. 0x00, 0x28, 0xcc, 0x14, 0xcc, 0x13, 0xc0, 0x2b, 0xc0, 0x2f, 0x00, 0x9e,
  9572. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x13, 0xc0, 0x14, 0xc0, 0x07, 0xc0, 0x11,
  9573. 0x00, 0x33, 0x00, 0x32, 0x00, 0x39, 0x00, 0x9c, 0x00, 0x2f, 0x00, 0x35,
  9574. 0x00, 0x0a, 0x00, 0x05, 0x00, 0x04, 0x01, 0x00, 0x00, 0x65, 0xff, 0x01,
  9575. 0x00, 0x01, 0x00, 0x00, 0x0a, 0x00, 0x08, 0x00, 0x06, 0x00, 0x17, 0x00,
  9576. 0x18, 0x00, 0x19, 0x00, 0x0b, 0x00, 0x02, 0x01, 0x00, 0x00, 0x23, 0x00,
  9577. 0x00, 0x33, 0x74, 0x00, 0x00, 0x00, 0x10, 0x00, 0x1b, 0x00, 0x19, 0x06,
  9578. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33, 0x08, 0x73, 0x70, 0x64, 0x79, 0x2f,
  9579. 0x33, 0x2e, 0x31, 0x08, 0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31,
  9580. 0x75, 0x50, 0x00, 0x00, 0x00, 0x05, 0x00, 0x05, 0x01, 0x00, 0x00, 0x00,
  9581. 0x00, 0x00, 0x0d, 0x00, 0x12, 0x00, 0x10, 0x04, 0x01, 0x05, 0x01, 0x02,
  9582. 0x01, 0x04, 0x03, 0x05, 0x03, 0x02, 0x03, 0x04, 0x02, 0x02, 0x02, 0x00,
  9583. 0x12, 0x00, 0x00
  9584. };
  9585. byte buff5[] = { /* SSL v2.0 client hello */
  9586. 0x00, 0x2b, 0x01, 0x03, 0x01, 0x00, 0x09, 0x00, 0x00,
  9587. /* dummy bytes below, just to pass size check */
  9588. 0xb6, 0x03, 0x03, 0x83, 0xa3, 0xe6, 0xdc, 0x16, 0xa1, 0x43, 0xe9, 0x45,
  9589. 0x15, 0xbd, 0x64, 0xa9, 0xb6, 0x07, 0xb4, 0x50, 0xc6, 0xdd, 0xff, 0xc2,
  9590. 0xd3, 0x0d, 0x4f, 0x36, 0xb4, 0x41, 0x51, 0x61, 0xc1, 0xa5, 0x9e, 0x00,
  9591. };
  9592. byte result[32] = {0};
  9593. word32 length = 32;
  9594. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff4, sizeof(buff4),
  9595. 0, result, &length));
  9596. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff3, sizeof(buff3),
  9597. 0, result, &length));
  9598. ExpectIntEQ(0, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  9599. 1, result, &length));
  9600. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9601. 0, result, &length));
  9602. buff[0] = 0x16;
  9603. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9604. 0, result, &length));
  9605. buff[1] = 0x03;
  9606. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff,
  9607. sizeof(buff), 0, result, &length));
  9608. buff[2] = 0x03;
  9609. ExpectIntEQ(INCOMPLETE_DATA, wolfSSL_SNI_GetFromBuffer(buff,
  9610. sizeof(buff), 0, result, &length));
  9611. buff[4] = 0x64;
  9612. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff, sizeof(buff),
  9613. 0, result, &length));
  9614. if (EXPECT_SUCCESS())
  9615. result[length] = 0;
  9616. ExpectStrEQ("www.paypal.com", (const char*) result);
  9617. length = 32;
  9618. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SNI_GetFromBuffer(buff2, sizeof(buff2),
  9619. 0, result, &length));
  9620. if (EXPECT_SUCCESS())
  9621. result[length] = 0;
  9622. ExpectStrEQ("api.textmate.org", (const char*) result);
  9623. /* SSL v2.0 tests */
  9624. ExpectIntEQ(SNI_UNSUPPORTED, wolfSSL_SNI_GetFromBuffer(buff5,
  9625. sizeof(buff5), 0, result, &length));
  9626. buff5[2] = 0x02;
  9627. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9628. sizeof(buff5), 0, result, &length));
  9629. buff5[2] = 0x01; buff5[6] = 0x08;
  9630. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9631. sizeof(buff5), 0, result, &length));
  9632. buff5[6] = 0x09; buff5[8] = 0x01;
  9633. ExpectIntEQ(BUFFER_ERROR, wolfSSL_SNI_GetFromBuffer(buff5,
  9634. sizeof(buff5), 0, result, &length));
  9635. return EXPECT_RESULT();
  9636. }
  9637. #endif /* HAVE_SNI */
  9638. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  9639. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) && \
  9640. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  9641. /* Dummy peer functions to satisfy the exporter/importer */
  9642. static int test_wolfSSL_dtls_export_peers_get_peer(WOLFSSL* ssl, char* ip,
  9643. int* ipSz, unsigned short* port, int* fam)
  9644. {
  9645. (void)ssl;
  9646. ip[0] = -1;
  9647. *ipSz = 1;
  9648. *port = 1;
  9649. *fam = 2;
  9650. return 1;
  9651. }
  9652. static int test_wolfSSL_dtls_export_peers_set_peer(WOLFSSL* ssl, char* ip,
  9653. int ipSz, unsigned short port, int fam)
  9654. {
  9655. (void)ssl;
  9656. if (ip[0] != -1 || ipSz != 1 || port != 1 || fam != 2)
  9657. return 0;
  9658. return 1;
  9659. }
  9660. static int test_wolfSSL_dtls_export_peers_on_handshake(WOLFSSL_CTX **ctx,
  9661. WOLFSSL **ssl)
  9662. {
  9663. EXPECT_DECLS;
  9664. unsigned char* sessionBuf = NULL;
  9665. unsigned int sessionSz = 0;
  9666. void* ioWriteCtx = wolfSSL_GetIOWriteCtx(*ssl);
  9667. void* ioReadCtx = wolfSSL_GetIOReadCtx(*ssl);
  9668. wolfSSL_CTX_SetIOGetPeer(*ctx, test_wolfSSL_dtls_export_peers_get_peer);
  9669. wolfSSL_CTX_SetIOSetPeer(*ctx, test_wolfSSL_dtls_export_peers_set_peer);
  9670. ExpectIntGE(wolfSSL_dtls_export(*ssl, NULL, &sessionSz), 0);
  9671. ExpectNotNull(sessionBuf =
  9672. (unsigned char*)XMALLOC(sessionSz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  9673. ExpectIntGE(wolfSSL_dtls_export(*ssl, sessionBuf, &sessionSz), 0);
  9674. wolfSSL_free(*ssl);
  9675. *ssl = NULL;
  9676. ExpectNotNull(*ssl = wolfSSL_new(*ctx));
  9677. ExpectIntGE(wolfSSL_dtls_import(*ssl, sessionBuf, sessionSz), 0);
  9678. wolfSSL_SetIOWriteCtx(*ssl, ioWriteCtx);
  9679. wolfSSL_SetIOReadCtx(*ssl, ioReadCtx);
  9680. XFREE(sessionBuf, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  9681. return EXPECT_RESULT();
  9682. }
  9683. #endif
  9684. static int test_wolfSSL_dtls_export_peers(void)
  9685. {
  9686. EXPECT_DECLS;
  9687. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_SESSION_EXPORT) && \
  9688. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  9689. test_ssl_cbf client_cbf;
  9690. test_ssl_cbf server_cbf;
  9691. size_t i, j;
  9692. struct test_params {
  9693. method_provider client_meth;
  9694. method_provider server_meth;
  9695. const char* dtls_version;
  9696. } params[] = {
  9697. #ifndef NO_OLD_TLS
  9698. {wolfDTLSv1_client_method, wolfDTLSv1_server_method, "1.0"},
  9699. #endif
  9700. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "1.2"},
  9701. /* TODO DTLS 1.3 exporting not supported
  9702. #ifdef WOLFSSL_DTLS13
  9703. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "1.3"},
  9704. #endif
  9705. */
  9706. };
  9707. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  9708. for (j = 0; j <= 0b11; j++) {
  9709. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  9710. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  9711. printf("\n\tTesting DTLS %s connection;", params[i].dtls_version);
  9712. client_cbf.method = params[i].client_meth;
  9713. server_cbf.method = params[i].server_meth;
  9714. if (j & 0b01) {
  9715. client_cbf.on_handshake =
  9716. test_wolfSSL_dtls_export_peers_on_handshake;
  9717. printf(" With client export;");
  9718. }
  9719. if (j & 0b10) {
  9720. server_cbf.on_handshake =
  9721. test_wolfSSL_dtls_export_peers_on_handshake;
  9722. printf(" With server export;");
  9723. }
  9724. printf("\n");
  9725. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  9726. &server_cbf, NULL), TEST_SUCCESS);
  9727. if (!EXPECT_SUCCESS())
  9728. break;
  9729. }
  9730. }
  9731. #endif
  9732. return EXPECT_RESULT();
  9733. }
  9734. static int test_wolfSSL_UseTrustedCA(void)
  9735. {
  9736. EXPECT_DECLS;
  9737. #if defined(HAVE_TRUSTED_CA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  9738. && !defined(NO_RSA)
  9739. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9740. WOLFSSL_CTX *ctx = NULL;
  9741. WOLFSSL *ssl = NULL;
  9742. byte id[20];
  9743. #ifndef NO_WOLFSSL_SERVER
  9744. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_server_method())));
  9745. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9746. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9747. #else
  9748. ExpectNotNull((ctx = wolfSSL_CTX_new(wolfSSLv23_client_method())));
  9749. #endif
  9750. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  9751. XMEMSET(id, 0, sizeof(id));
  9752. /* error cases */
  9753. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(NULL, 0, NULL, 0));
  9754. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9755. WOLFSSL_TRUSTED_CA_CERT_SHA1+1, NULL, 0));
  9756. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9757. WOLFSSL_TRUSTED_CA_CERT_SHA1, NULL, 0));
  9758. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9759. WOLFSSL_TRUSTED_CA_CERT_SHA1, id, 5));
  9760. #ifdef NO_SHA
  9761. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9762. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  9763. #endif
  9764. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9765. WOLFSSL_TRUSTED_CA_X509_NAME, id, 0));
  9766. /* success cases */
  9767. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9768. WOLFSSL_TRUSTED_CA_PRE_AGREED, NULL, 0));
  9769. #ifndef NO_SHA
  9770. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9771. WOLFSSL_TRUSTED_CA_KEY_SHA1, id, sizeof(id)));
  9772. #endif
  9773. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTrustedCA(ssl,
  9774. WOLFSSL_TRUSTED_CA_X509_NAME, id, 5));
  9775. wolfSSL_free(ssl);
  9776. wolfSSL_CTX_free(ctx);
  9777. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9778. #endif /* HAVE_TRUSTED_CA */
  9779. return EXPECT_RESULT();
  9780. }
  9781. static int test_wolfSSL_UseMaxFragment(void)
  9782. {
  9783. EXPECT_DECLS;
  9784. #if defined(HAVE_MAX_FRAGMENT) && !defined(NO_CERTS) && \
  9785. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  9786. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9787. #ifndef NO_WOLFSSL_SERVER
  9788. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  9789. #else
  9790. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9791. #endif
  9792. WOLFSSL *ssl = NULL;
  9793. #ifdef OPENSSL_EXTRA
  9794. int (*UseMaxFragment)(SSL *s, uint8_t mode);
  9795. int (*CTX_UseMaxFragment)(SSL_CTX *c, uint8_t mode);
  9796. #else
  9797. int (*UseMaxFragment)(WOLFSSL *s, unsigned char mode);
  9798. int (*CTX_UseMaxFragment)(WOLFSSL_CTX *c, unsigned char mode);
  9799. #endif
  9800. #ifndef NO_WOLFSSL_SERVER
  9801. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9802. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9803. #endif
  9804. ExpectNotNull(ctx);
  9805. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9806. #ifdef OPENSSL_EXTRA
  9807. CTX_UseMaxFragment = SSL_CTX_set_tlsext_max_fragment_length;
  9808. UseMaxFragment = SSL_set_tlsext_max_fragment_length;
  9809. #else
  9810. UseMaxFragment = wolfSSL_UseMaxFragment;
  9811. CTX_UseMaxFragment = wolfSSL_CTX_UseMaxFragment;
  9812. #endif
  9813. /* error cases */
  9814. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(NULL, WOLFSSL_MFL_2_9));
  9815. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment( NULL, WOLFSSL_MFL_2_9));
  9816. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MIN-1));
  9817. ExpectIntNE(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_MAX+1));
  9818. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MIN-1));
  9819. ExpectIntNE(WOLFSSL_SUCCESS, UseMaxFragment(ssl, WOLFSSL_MFL_MAX+1));
  9820. /* success case */
  9821. #ifdef OPENSSL_EXTRA
  9822. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  9823. #else
  9824. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_8));
  9825. #endif
  9826. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_9));
  9827. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_10));
  9828. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_11));
  9829. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_12));
  9830. #ifdef OPENSSL_EXTRA
  9831. ExpectIntEQ(BAD_FUNC_ARG, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  9832. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  9833. #else
  9834. ExpectIntEQ(WOLFSSL_SUCCESS, CTX_UseMaxFragment(ctx, WOLFSSL_MFL_2_13));
  9835. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_8));
  9836. #endif
  9837. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_9));
  9838. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_10));
  9839. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_11));
  9840. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_12));
  9841. #ifdef OPENSSL_EXTRA
  9842. ExpectIntEQ(BAD_FUNC_ARG, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  9843. #else
  9844. ExpectIntEQ(WOLFSSL_SUCCESS, UseMaxFragment( ssl, WOLFSSL_MFL_2_13));
  9845. #endif
  9846. wolfSSL_free(ssl);
  9847. wolfSSL_CTX_free(ctx);
  9848. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9849. #endif
  9850. return EXPECT_RESULT();
  9851. }
  9852. static int test_wolfSSL_UseTruncatedHMAC(void)
  9853. {
  9854. EXPECT_DECLS;
  9855. #if defined(HAVE_TRUNCATED_HMAC) && !defined(NO_CERTS) && \
  9856. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  9857. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  9858. #ifndef NO_WOLFSSL_SERVER
  9859. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_server_method());
  9860. #else
  9861. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9862. #endif
  9863. WOLFSSL *ssl = NULL;
  9864. ExpectNotNull(ctx);
  9865. #ifndef NO_WOLFSSL_SERVER
  9866. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  9867. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  9868. #endif
  9869. ExpectNotNull(ssl = wolfSSL_new(ctx));
  9870. /* error cases */
  9871. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(NULL));
  9872. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(NULL));
  9873. /* success case */
  9874. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseTruncatedHMAC(ctx));
  9875. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseTruncatedHMAC(ssl));
  9876. wolfSSL_free(ssl);
  9877. wolfSSL_CTX_free(ctx);
  9878. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  9879. #endif
  9880. return EXPECT_RESULT();
  9881. }
  9882. static int test_wolfSSL_UseSupportedCurve(void)
  9883. {
  9884. EXPECT_DECLS;
  9885. #if defined(HAVE_SUPPORTED_CURVES) && !defined(NO_WOLFSSL_CLIENT) && \
  9886. !defined(NO_TLS)
  9887. WOLFSSL_CTX* ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  9888. WOLFSSL *ssl = wolfSSL_new(ctx);
  9889. ExpectNotNull(ctx);
  9890. ExpectNotNull(ssl);
  9891. /* error cases */
  9892. ExpectIntNE(WOLFSSL_SUCCESS,
  9893. wolfSSL_CTX_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  9894. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSupportedCurve(ctx, 0));
  9895. ExpectIntNE(WOLFSSL_SUCCESS,
  9896. wolfSSL_UseSupportedCurve(NULL, WOLFSSL_ECC_SECP256R1));
  9897. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSupportedCurve(ssl, 0));
  9898. /* success case */
  9899. ExpectIntEQ(WOLFSSL_SUCCESS,
  9900. wolfSSL_CTX_UseSupportedCurve(ctx, WOLFSSL_ECC_SECP256R1));
  9901. ExpectIntEQ(WOLFSSL_SUCCESS,
  9902. wolfSSL_UseSupportedCurve(ssl, WOLFSSL_ECC_SECP256R1));
  9903. wolfSSL_free(ssl);
  9904. wolfSSL_CTX_free(ctx);
  9905. #endif
  9906. return EXPECT_RESULT();
  9907. }
  9908. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  9909. static void verify_ALPN_FATAL_ERROR_on_client(WOLFSSL* ssl)
  9910. {
  9911. AssertIntEQ(UNKNOWN_ALPN_PROTOCOL_NAME_E, wolfSSL_get_error(ssl, 0));
  9912. }
  9913. static void use_ALPN_all(WOLFSSL* ssl)
  9914. {
  9915. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9916. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9917. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9918. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9919. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9920. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9921. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9922. }
  9923. static void use_ALPN_all_continue(WOLFSSL* ssl)
  9924. {
  9925. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  9926. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  9927. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  9928. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  9929. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9930. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, alpn_list, sizeof(alpn_list),
  9931. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9932. }
  9933. static void use_ALPN_one(WOLFSSL* ssl)
  9934. {
  9935. /* spdy/2 */
  9936. char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9937. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9938. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9939. }
  9940. static void use_ALPN_unknown(WOLFSSL* ssl)
  9941. {
  9942. /* http/2.0 */
  9943. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9944. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9945. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  9946. }
  9947. static void use_ALPN_unknown_continue(WOLFSSL* ssl)
  9948. {
  9949. /* http/2.0 */
  9950. char proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x32, 0x2e, 0x30};
  9951. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, proto, sizeof(proto),
  9952. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  9953. }
  9954. static void verify_ALPN_not_matching_spdy3(WOLFSSL* ssl)
  9955. {
  9956. /* spdy/3 */
  9957. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  9958. char *proto = NULL;
  9959. word16 protoSz = 0;
  9960. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9961. /* check value */
  9962. AssertIntNE(1, sizeof(nego_proto) == protoSz);
  9963. if (proto) {
  9964. AssertIntNE(0, XMEMCMP(nego_proto, proto, sizeof(nego_proto)));
  9965. }
  9966. }
  9967. static void verify_ALPN_not_matching_continue(WOLFSSL* ssl)
  9968. {
  9969. char *proto = NULL;
  9970. word16 protoSz = 0;
  9971. AssertIntEQ(WOLFSSL_ALPN_NOT_FOUND,
  9972. wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9973. /* check value */
  9974. AssertIntEQ(1, (0 == protoSz));
  9975. AssertIntEQ(1, (NULL == proto));
  9976. }
  9977. static void verify_ALPN_matching_http1(WOLFSSL* ssl)
  9978. {
  9979. /* http/1.1 */
  9980. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  9981. char *proto;
  9982. word16 protoSz = 0;
  9983. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9984. /* check value */
  9985. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9986. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9987. }
  9988. static void verify_ALPN_matching_spdy2(WOLFSSL* ssl)
  9989. {
  9990. /* spdy/2 */
  9991. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  9992. char *proto;
  9993. word16 protoSz = 0;
  9994. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetProtocol(ssl, &proto, &protoSz));
  9995. /* check value */
  9996. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  9997. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  9998. }
  9999. static void verify_ALPN_client_list(WOLFSSL* ssl)
  10000. {
  10001. /* http/1.1,spdy/1,spdy/2,spdy/3 */
  10002. char alpn_list[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31, 0x2c,
  10003. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x31, 0x2c,
  10004. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x32, 0x2c,
  10005. 0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10006. char *clist = NULL;
  10007. word16 clistSz = 0;
  10008. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_GetPeerProtocol(ssl, &clist,
  10009. &clistSz));
  10010. /* check value */
  10011. AssertIntEQ(1, sizeof(alpn_list) == clistSz);
  10012. AssertIntEQ(0, XMEMCMP(alpn_list, clist, clistSz));
  10013. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_ALPN_FreePeerProtocol(ssl, &clist));
  10014. }
  10015. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  10016. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)
  10017. /* ALPN select callback, success with spdy/2 */
  10018. static int select_ALPN_spdy2(WOLFSSL *ssl, const unsigned char **out,
  10019. unsigned char *outlen, const unsigned char *in,
  10020. unsigned int inlen, void *arg)
  10021. {
  10022. /* spdy/2 */
  10023. const char proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  10024. (void)ssl;
  10025. (void)arg;
  10026. /* adding +1 since LEN byte comes first */
  10027. if (inlen < sizeof(proto) + 1) {
  10028. return SSL_TLSEXT_ERR_ALERT_FATAL;
  10029. }
  10030. if (XMEMCMP(in + 1, proto, sizeof(proto)) == 0) {
  10031. *out = in + 1;
  10032. *outlen = (unsigned char)sizeof(proto);
  10033. return SSL_TLSEXT_ERR_OK;
  10034. }
  10035. return SSL_TLSEXT_ERR_ALERT_FATAL;
  10036. }
  10037. /* ALPN select callback, force failure */
  10038. static int select_ALPN_failure(WOLFSSL *ssl, const unsigned char **out,
  10039. unsigned char *outlen, const unsigned char *in,
  10040. unsigned int inlen, void *arg)
  10041. {
  10042. (void)ssl;
  10043. (void)out;
  10044. (void)outlen;
  10045. (void)in;
  10046. (void)inlen;
  10047. (void)arg;
  10048. return SSL_TLSEXT_ERR_ALERT_FATAL;
  10049. }
  10050. static void use_ALPN_spdy2_callback(WOLFSSL* ssl)
  10051. {
  10052. wolfSSL_set_alpn_select_cb(ssl, select_ALPN_spdy2, NULL);
  10053. }
  10054. static void use_ALPN_failure_callback(WOLFSSL* ssl)
  10055. {
  10056. wolfSSL_set_alpn_select_cb(ssl, select_ALPN_failure, NULL);
  10057. }
  10058. #endif /* OPENSSL_ALL | NGINX | HAPROXY | LIGHTY | QUIC */
  10059. static int test_wolfSSL_UseALPN_connection(void)
  10060. {
  10061. int res = TEST_SKIPPED;
  10062. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  10063. callback_functions client_cb;
  10064. callback_functions server_cb;
  10065. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  10066. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  10067. client_cb.method = wolfSSLv23_client_method;
  10068. server_cb.method = wolfSSLv23_server_method;
  10069. client_cb.devId = testDevId;
  10070. server_cb.devId = testDevId;
  10071. /* success case same list */
  10072. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10073. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_http1;
  10074. test_wolfSSL_client_server(&client_cb, &server_cb);
  10075. /* success case only one for server */
  10076. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10077. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_matching_spdy2;
  10078. test_wolfSSL_client_server(&client_cb, &server_cb);
  10079. /* success case only one for client */
  10080. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_one; client_cb.on_result = NULL;
  10081. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_matching_spdy2;
  10082. test_wolfSSL_client_server(&client_cb, &server_cb);
  10083. /* success case none for client */
  10084. client_cb.ctx_ready = NULL; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  10085. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = NULL;
  10086. test_wolfSSL_client_server(&client_cb, &server_cb);
  10087. /* success case mismatch behavior but option 'continue' set */
  10088. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all_continue; client_cb.on_result = verify_ALPN_not_matching_continue;
  10089. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown_continue; server_cb.on_result = NULL;
  10090. test_wolfSSL_client_server(&client_cb, &server_cb);
  10091. /* success case read protocol send by client */
  10092. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10093. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_one; server_cb.on_result = verify_ALPN_client_list;
  10094. test_wolfSSL_client_server(&client_cb, &server_cb);
  10095. /* mismatch behavior with same list
  10096. * the first and only this one must be taken */
  10097. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10098. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_all; server_cb.on_result = verify_ALPN_not_matching_spdy3;
  10099. test_wolfSSL_client_server(&client_cb, &server_cb);
  10100. /* default mismatch behavior */
  10101. client_cb.ctx_ready = NULL; client_cb.ssl_ready = use_ALPN_all; client_cb.on_result = NULL;
  10102. server_cb.ctx_ready = NULL; server_cb.ssl_ready = use_ALPN_unknown; server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  10103. test_wolfSSL_client_server(&client_cb, &server_cb);
  10104. #if defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) || \
  10105. defined(WOLFSSL_HAPROXY) || defined(HAVE_LIGHTY)
  10106. /* WOLFSSL-level ALPN select callback tests */
  10107. /* Callback: success (one protocol, spdy/2) */
  10108. client_cb.ctx_ready = NULL;
  10109. client_cb.ssl_ready = use_ALPN_one;
  10110. client_cb.on_result = verify_ALPN_matching_spdy2;
  10111. server_cb.ctx_ready = NULL;
  10112. server_cb.ssl_ready = use_ALPN_spdy2_callback;
  10113. server_cb.on_result = verify_ALPN_matching_spdy2;
  10114. test_wolfSSL_client_server(&client_cb, &server_cb);
  10115. /* Callback: failure (one client protocol, spdy/2) */
  10116. client_cb.ctx_ready = NULL;
  10117. client_cb.ssl_ready = use_ALPN_one;
  10118. client_cb.on_result = NULL;
  10119. server_cb.ctx_ready = NULL;
  10120. server_cb.ssl_ready = use_ALPN_failure_callback;
  10121. server_cb.on_result = verify_ALPN_FATAL_ERROR_on_client;
  10122. test_wolfSSL_client_server(&client_cb, &server_cb);
  10123. #endif /* OPENSSL_ALL | NGINX | HAPROXY | LIGHTY */
  10124. res = TEST_RES_CHECK(1);
  10125. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  10126. return res;
  10127. }
  10128. static int test_wolfSSL_UseALPN_params(void)
  10129. {
  10130. EXPECT_DECLS;
  10131. #ifndef NO_WOLFSSL_CLIENT
  10132. /* "http/1.1" */
  10133. char http1[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  10134. /* "spdy/1" */
  10135. char spdy1[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x31};
  10136. /* "spdy/2" */
  10137. char spdy2[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x32};
  10138. /* "spdy/3" */
  10139. char spdy3[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10140. char buff[256];
  10141. word32 idx;
  10142. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10143. WOLFSSL *ssl = wolfSSL_new(ctx);
  10144. ExpectNotNull(ctx);
  10145. ExpectNotNull(ssl);
  10146. /* error cases */
  10147. ExpectIntNE(WOLFSSL_SUCCESS,
  10148. wolfSSL_UseALPN(NULL, http1, sizeof(http1),
  10149. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10150. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, NULL, 0,
  10151. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10152. /* success case */
  10153. /* http1 only */
  10154. ExpectIntEQ(WOLFSSL_SUCCESS,
  10155. wolfSSL_UseALPN(ssl, http1, sizeof(http1),
  10156. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10157. /* http1, spdy1 */
  10158. XMEMCPY(buff, http1, sizeof(http1));
  10159. idx = sizeof(http1);
  10160. buff[idx++] = ',';
  10161. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10162. idx += sizeof(spdy1);
  10163. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10164. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10165. /* http1, spdy2, spdy1 */
  10166. XMEMCPY(buff, http1, sizeof(http1));
  10167. idx = sizeof(http1);
  10168. buff[idx++] = ',';
  10169. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  10170. idx += sizeof(spdy2);
  10171. buff[idx++] = ',';
  10172. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10173. idx += sizeof(spdy1);
  10174. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10175. WOLFSSL_ALPN_FAILED_ON_MISMATCH));
  10176. /* spdy3, http1, spdy2, spdy1 */
  10177. XMEMCPY(buff, spdy3, sizeof(spdy3));
  10178. idx = sizeof(spdy3);
  10179. buff[idx++] = ',';
  10180. XMEMCPY(buff+idx, http1, sizeof(http1));
  10181. idx += sizeof(http1);
  10182. buff[idx++] = ',';
  10183. XMEMCPY(buff+idx, spdy2, sizeof(spdy2));
  10184. idx += sizeof(spdy2);
  10185. buff[idx++] = ',';
  10186. XMEMCPY(buff+idx, spdy1, sizeof(spdy1));
  10187. idx += sizeof(spdy1);
  10188. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseALPN(ssl, buff, idx,
  10189. WOLFSSL_ALPN_CONTINUE_ON_MISMATCH));
  10190. wolfSSL_free(ssl);
  10191. wolfSSL_CTX_free(ctx);
  10192. #endif
  10193. return EXPECT_RESULT();
  10194. }
  10195. #endif /* HAVE_ALPN */
  10196. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  10197. static void CTX_set_alpn_protos(SSL_CTX *ctx)
  10198. {
  10199. unsigned char p[] = {
  10200. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  10201. 6, 's', 'p', 'd', 'y', '/', '2',
  10202. 6, 's', 'p', 'd', 'y', '/', '1',
  10203. };
  10204. unsigned char p_len = sizeof(p);
  10205. int ret;
  10206. ret = SSL_CTX_set_alpn_protos(ctx, p, p_len);
  10207. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  10208. AssertIntEQ(ret, 0);
  10209. #else
  10210. AssertIntEQ(ret, SSL_SUCCESS);
  10211. #endif
  10212. }
  10213. static void set_alpn_protos(SSL* ssl)
  10214. {
  10215. unsigned char p[] = {
  10216. 6, 's', 'p', 'd', 'y', '/', '3',
  10217. 8, 'h', 't', 't', 'p', '/', '1', '.', '1',
  10218. 6, 's', 'p', 'd', 'y', '/', '2',
  10219. 6, 's', 'p', 'd', 'y', '/', '1',
  10220. };
  10221. unsigned char p_len = sizeof(p);
  10222. int ret;
  10223. ret = SSL_set_alpn_protos(ssl, p, p_len);
  10224. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  10225. AssertIntEQ(ret, 0);
  10226. #else
  10227. AssertIntEQ(ret, SSL_SUCCESS);
  10228. #endif
  10229. }
  10230. static void verify_alpn_matching_spdy3(WOLFSSL* ssl)
  10231. {
  10232. /* "spdy/3" */
  10233. char nego_proto[] = {0x73, 0x70, 0x64, 0x79, 0x2f, 0x33};
  10234. const unsigned char *proto;
  10235. unsigned int protoSz = 0;
  10236. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  10237. /* check value */
  10238. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  10239. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  10240. }
  10241. static void verify_alpn_matching_http1(WOLFSSL* ssl)
  10242. {
  10243. /* "http/1.1" */
  10244. char nego_proto[] = {0x68, 0x74, 0x74, 0x70, 0x2f, 0x31, 0x2e, 0x31};
  10245. const unsigned char *proto;
  10246. unsigned int protoSz = 0;
  10247. SSL_get0_alpn_selected(ssl, &proto, &protoSz);
  10248. /* check value */
  10249. AssertIntEQ(1, sizeof(nego_proto) == protoSz);
  10250. AssertIntEQ(0, XMEMCMP(nego_proto, proto, protoSz));
  10251. }
  10252. static int test_wolfSSL_set_alpn_protos(void)
  10253. {
  10254. int res = TEST_SKIPPED;
  10255. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  10256. callback_functions client_cb;
  10257. callback_functions server_cb;
  10258. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  10259. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  10260. client_cb.method = wolfSSLv23_client_method;
  10261. server_cb.method = wolfSSLv23_server_method;
  10262. client_cb.devId = testDevId;
  10263. server_cb.devId = testDevId;
  10264. /* use CTX_alpn_protos */
  10265. client_cb.ctx_ready = CTX_set_alpn_protos; client_cb.ssl_ready = NULL; client_cb.on_result = NULL;
  10266. server_cb.ctx_ready = CTX_set_alpn_protos; server_cb.ssl_ready = NULL; server_cb.on_result = verify_alpn_matching_http1;
  10267. test_wolfSSL_client_server(&client_cb, &server_cb);
  10268. /* use set_alpn_protos */
  10269. client_cb.ctx_ready = NULL; client_cb.ssl_ready = set_alpn_protos; client_cb.on_result = NULL;
  10270. server_cb.ctx_ready = NULL; server_cb.ssl_ready = set_alpn_protos; server_cb.on_result = verify_alpn_matching_spdy3;
  10271. test_wolfSSL_client_server(&client_cb, &server_cb);
  10272. res = TEST_SUCCESS;
  10273. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  10274. return res;
  10275. }
  10276. #endif /* HAVE_ALPN_PROTOS_SUPPORT */
  10277. static int test_wolfSSL_DisableExtendedMasterSecret(void)
  10278. {
  10279. EXPECT_DECLS;
  10280. #if defined(HAVE_EXTENDED_MASTER) && !defined(NO_WOLFSSL_CLIENT)
  10281. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10282. WOLFSSL *ssl = wolfSSL_new(ctx);
  10283. ExpectNotNull(ctx);
  10284. ExpectNotNull(ssl);
  10285. /* error cases */
  10286. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(NULL));
  10287. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(NULL));
  10288. /* success cases */
  10289. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_DisableExtendedMasterSecret(ctx));
  10290. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_DisableExtendedMasterSecret(ssl));
  10291. wolfSSL_free(ssl);
  10292. wolfSSL_CTX_free(ctx);
  10293. #endif
  10294. return EXPECT_RESULT();
  10295. }
  10296. static int test_wolfSSL_wolfSSL_UseSecureRenegotiation(void)
  10297. {
  10298. EXPECT_DECLS;
  10299. #if defined(HAVE_SECURE_RENEGOTIATION) && !defined(NO_WOLFSSL_CLIENT)
  10300. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  10301. WOLFSSL *ssl = wolfSSL_new(ctx);
  10302. ExpectNotNull(ctx);
  10303. ExpectNotNull(ssl);
  10304. /* error cases */
  10305. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(NULL));
  10306. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(NULL));
  10307. /* success cases */
  10308. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx));
  10309. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl));
  10310. wolfSSL_free(ssl);
  10311. wolfSSL_CTX_free(ctx);
  10312. #endif
  10313. return EXPECT_RESULT();
  10314. }
  10315. /* Test reconnecting with a different ciphersuite after a renegotiation. */
  10316. static int test_wolfSSL_SCR_Reconnect(void)
  10317. {
  10318. EXPECT_DECLS;
  10319. #if defined(HAVE_SECURE_RENEGOTIATION) && \
  10320. defined(BUILD_TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) && \
  10321. defined(BUILD_TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256) && \
  10322. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  10323. struct test_memio_ctx test_ctx;
  10324. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  10325. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  10326. byte data;
  10327. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  10328. test_ctx.c_ciphers = "ECDHE-RSA-AES256-GCM-SHA384";
  10329. test_ctx.s_ciphers =
  10330. "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-CHACHA20-POLY1305";
  10331. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  10332. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  10333. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_c));
  10334. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_UseSecureRenegotiation(ctx_s));
  10335. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_c));
  10336. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSecureRenegotiation(ssl_s));
  10337. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10338. /* WOLFSSL_FATAL_ERROR since it will block */
  10339. ExpectIntEQ(wolfSSL_Rehandshake(ssl_s), WOLFSSL_FATAL_ERROR);
  10340. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  10341. WOLFSSL_ERROR_WANT_READ);
  10342. ExpectIntEQ(wolfSSL_read(ssl_c, &data, 1), WOLFSSL_FATAL_ERROR);
  10343. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  10344. WOLFSSL_ERROR_WANT_READ);
  10345. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10346. wolfSSL_free(ssl_c);
  10347. ssl_c = NULL;
  10348. wolfSSL_free(ssl_s);
  10349. ssl_s = NULL;
  10350. wolfSSL_CTX_free(ctx_c);
  10351. ctx_c = NULL;
  10352. test_ctx.c_ciphers = "ECDHE-RSA-CHACHA20-POLY1305";
  10353. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  10354. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  10355. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  10356. wolfSSL_free(ssl_s);
  10357. wolfSSL_free(ssl_c);
  10358. wolfSSL_CTX_free(ctx_s);
  10359. wolfSSL_CTX_free(ctx_c);
  10360. #endif
  10361. return EXPECT_RESULT();
  10362. }
  10363. #if !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_SERVER) && \
  10364. (!defined(NO_RSA) || defined(HAVE_ECC))
  10365. /* Called when writing. */
  10366. static int DummySend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  10367. {
  10368. (void)ssl;
  10369. (void)buf;
  10370. (void)sz;
  10371. (void)ctx;
  10372. /* Force error return from wolfSSL_accept_TLSv13(). */
  10373. return WANT_WRITE;
  10374. }
  10375. /* Called when reading. */
  10376. static int BufferInfoRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  10377. {
  10378. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  10379. int len = (int)msg->length;
  10380. (void)ssl;
  10381. (void)sz;
  10382. /* Pass back as much of message as will fit in buffer. */
  10383. if (len > sz)
  10384. len = sz;
  10385. XMEMCPY(buf, msg->buffer, len);
  10386. /* Move over returned data. */
  10387. msg->buffer += len;
  10388. msg->length -= len;
  10389. /* Amount actually copied. */
  10390. return len;
  10391. }
  10392. #endif
  10393. /* Test the detection of duplicate known TLS extensions.
  10394. * Specifically in a ClientHello.
  10395. */
  10396. static int test_tls_ext_duplicate(void)
  10397. {
  10398. EXPECT_DECLS;
  10399. #if !defined(NO_WOLFSSL_SERVER) && (!defined(NO_RSA) || defined(HAVE_ECC)) && \
  10400. !defined(NO_FILESYSTEM)
  10401. const unsigned char clientHelloDupTlsExt[] = {
  10402. 0x16, 0x03, 0x03, 0x00, 0x6a, 0x01, 0x00, 0x00,
  10403. 0x66, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  10404. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  10405. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  10406. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  10407. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  10408. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  10409. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  10410. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  10411. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x13, 0x01,
  10412. 0x00, 0x9e, 0x01, 0x00,
  10413. /* Extensions - duplicate signature algorithms. */
  10414. 0x00, 0x19, 0x00, 0x0d,
  10415. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01, 0x00, 0x0d,
  10416. 0x00, 0x04, 0x00, 0x02, 0x04, 0x01,
  10417. /* Supported Versions extension for TLS 1.3. */
  10418. 0x00, 0x2b,
  10419. 0x00, 0x05, 0x04, 0x03, 0x04, 0x03, 0x03
  10420. };
  10421. WOLFSSL_BUFFER_INFO msg;
  10422. const char* testCertFile;
  10423. const char* testKeyFile;
  10424. WOLFSSL_CTX *ctx = NULL;
  10425. WOLFSSL *ssl = NULL;
  10426. #ifndef NO_RSA
  10427. testCertFile = svrCertFile;
  10428. testKeyFile = svrKeyFile;
  10429. #elif defined(HAVE_ECC)
  10430. testCertFile = eccCertFile;
  10431. testKeyFile = eccKeyFile;
  10432. #endif
  10433. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10434. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  10435. WOLFSSL_FILETYPE_PEM));
  10436. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  10437. WOLFSSL_FILETYPE_PEM));
  10438. /* Read from 'msg'. */
  10439. wolfSSL_SetIORecv(ctx, BufferInfoRecv);
  10440. /* No where to send to - dummy sender. */
  10441. wolfSSL_SetIOSend(ctx, DummySend);
  10442. ssl = wolfSSL_new(ctx);
  10443. ExpectNotNull(ssl);
  10444. msg.buffer = (unsigned char*)clientHelloDupTlsExt;
  10445. msg.length = (unsigned int)sizeof(clientHelloDupTlsExt);
  10446. wolfSSL_SetIOReadCtx(ssl, &msg);
  10447. ExpectIntNE(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  10448. /* can return duplicate ext error or socket error if the peer closed down
  10449. * while sending alert */
  10450. if (wolfSSL_get_error(ssl, 0) != SOCKET_ERROR_E) {
  10451. ExpectIntEQ(wolfSSL_get_error(ssl, 0), DUPLICATE_TLS_EXT_E);
  10452. }
  10453. wolfSSL_free(ssl);
  10454. wolfSSL_CTX_free(ctx);
  10455. #endif
  10456. return EXPECT_RESULT();
  10457. }
  10458. /*----------------------------------------------------------------------------*
  10459. | X509 Tests
  10460. *----------------------------------------------------------------------------*/
  10461. static int test_wolfSSL_X509_NAME_get_entry(void)
  10462. {
  10463. EXPECT_DECLS;
  10464. #if !defined(NO_CERTS) && !defined(NO_RSA)
  10465. #if defined(OPENSSL_ALL) || \
  10466. (defined(OPENSSL_EXTRA) && \
  10467. (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS)))
  10468. /* use openssl like name to test mapping */
  10469. X509_NAME_ENTRY* ne = NULL;
  10470. X509_NAME* name = NULL;
  10471. X509* x509 = NULL;
  10472. #ifndef NO_FILESYSTEM
  10473. ASN1_STRING* asn = NULL;
  10474. char* subCN = NULL;
  10475. #endif
  10476. int idx = 0;
  10477. ASN1_OBJECT *object = NULL;
  10478. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  10479. defined(WOLFSSL_NGINX)
  10480. #ifndef NO_BIO
  10481. BIO* bio = NULL;
  10482. #endif
  10483. #endif
  10484. #ifndef NO_FILESYSTEM
  10485. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10486. WOLFSSL_FILETYPE_PEM));
  10487. ExpectNotNull(name = X509_get_subject_name(x509));
  10488. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  10489. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  10490. ExpectNotNull(asn = X509_NAME_ENTRY_get_data(ne));
  10491. ExpectNotNull(subCN = (char*)ASN1_STRING_data(asn));
  10492. wolfSSL_FreeX509(x509);
  10493. x509 = NULL;
  10494. #endif
  10495. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  10496. WOLFSSL_FILETYPE_PEM));
  10497. ExpectNotNull(name = X509_get_subject_name(x509));
  10498. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  10499. #if defined(WOLFSSL_APACHE_HTTPD) || defined(OPENSSL_ALL) || \
  10500. defined(WOLFSSL_NGINX)
  10501. #ifndef NO_BIO
  10502. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10503. ExpectIntEQ(X509_NAME_print_ex(bio, name, 4,
  10504. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  10505. ExpectIntEQ(X509_NAME_print_ex_fp(stderr, name, 4,
  10506. (XN_FLAG_RFC2253 & ~XN_FLAG_DN_REV)), WOLFSSL_SUCCESS);
  10507. BIO_free(bio);
  10508. #endif
  10509. #endif
  10510. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  10511. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  10512. wolfSSL_FreeX509(x509);
  10513. #endif /* OPENSSL_ALL || (OPENSSL_EXTRA && (KEEP_PEER_CERT || SESSION_CERTS) */
  10514. #endif /* !NO_CERTS && !NO_RSA */
  10515. return EXPECT_RESULT();
  10516. }
  10517. /* Testing functions dealing with PKCS12 parsing out X509 certs */
  10518. static int test_wolfSSL_PKCS12(void)
  10519. {
  10520. EXPECT_DECLS;
  10521. /* .p12 file is encrypted with DES3 */
  10522. #ifndef HAVE_FIPS /* Password used in cert "wolfSSL test" is only 12-bytes
  10523. * (96-bit) FIPS mode requires Minimum of 14-byte (112-bit)
  10524. * Password Key
  10525. */
  10526. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && !defined(NO_FILESYSTEM) && \
  10527. !defined(NO_STDIO_FILESYSTEM) && \
  10528. !defined(NO_ASN) && !defined(NO_PWDBASED) && !defined(NO_RSA) && \
  10529. !defined(NO_SHA) && defined(HAVE_PKCS12) && !defined(NO_BIO)
  10530. byte buf[6000];
  10531. char file[] = "./certs/test-servercert.p12";
  10532. char order[] = "./certs/ecc-rsa-server.p12";
  10533. #ifdef WC_RC2
  10534. char rc2p12[] = "./certs/test-servercert-rc2.p12";
  10535. #endif
  10536. char pass[] = "a password";
  10537. const char goodPsw[] = "wolfSSL test";
  10538. const char badPsw[] = "bad";
  10539. #ifdef HAVE_ECC
  10540. WOLFSSL_X509_NAME *subject = NULL;
  10541. WOLFSSL_X509 *x509 = NULL;
  10542. #endif
  10543. XFILE f = XBADFILE;
  10544. int bytes = 0, ret = 0, goodPswLen = 0, badPswLen = 0;
  10545. WOLFSSL_BIO *bio = NULL;
  10546. WOLFSSL_EVP_PKEY *pkey = NULL;
  10547. WC_PKCS12 *pkcs12 = NULL;
  10548. WC_PKCS12 *pkcs12_2 = NULL;
  10549. WOLFSSL_X509 *cert = NULL;
  10550. WOLFSSL_X509 *tmp = NULL;
  10551. WOLF_STACK_OF(WOLFSSL_X509) *ca = NULL;
  10552. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10553. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10554. WOLFSSL_CTX *ctx = NULL;
  10555. WOLFSSL *ssl = NULL;
  10556. WOLF_STACK_OF(WOLFSSL_X509) *tmp_ca = NULL;
  10557. #endif
  10558. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10559. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10560. if (f != XBADFILE) {
  10561. XFCLOSE(f);
  10562. f = XBADFILE;
  10563. }
  10564. goodPswLen = (int)XSTRLEN(goodPsw);
  10565. badPswLen = (int)XSTRLEN(badPsw);
  10566. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  10567. ExpectIntEQ(BIO_write(bio, buf, bytes), bytes); /* d2i consumes BIO */
  10568. ExpectNotNull(d2i_PKCS12_bio(bio, &pkcs12));
  10569. ExpectNotNull(pkcs12);
  10570. BIO_free(bio);
  10571. bio = NULL;
  10572. /* check verify MAC directly */
  10573. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, goodPsw, goodPswLen), 1);
  10574. /* check verify MAC fail case directly */
  10575. ExpectIntEQ(ret = PKCS12_verify_mac(pkcs12, badPsw, badPswLen), 0);
  10576. /* check verify MAC fail case */
  10577. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10578. ExpectNull(pkey);
  10579. ExpectNull(cert);
  10580. /* check parse with no extra certs kept */
  10581. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10582. 1);
  10583. ExpectNotNull(pkey);
  10584. ExpectNotNull(cert);
  10585. wolfSSL_EVP_PKEY_free(pkey);
  10586. pkey = NULL;
  10587. wolfSSL_X509_free(cert);
  10588. cert = NULL;
  10589. /* check parse with extra certs kept */
  10590. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10591. 1);
  10592. ExpectNotNull(pkey);
  10593. ExpectNotNull(cert);
  10594. ExpectNotNull(ca);
  10595. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10596. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10597. /* Check that SSL_CTX_set0_chain correctly sets the certChain buffer */
  10598. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10599. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  10600. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10601. #else
  10602. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10603. #endif
  10604. /* Copy stack structure */
  10605. ExpectNotNull(tmp_ca = X509_chain_up_ref(ca));
  10606. ExpectIntEQ(SSL_CTX_set0_chain(ctx, tmp_ca), 1);
  10607. /* CTX now owns the tmp_ca stack structure */
  10608. tmp_ca = NULL;
  10609. ExpectIntEQ(wolfSSL_CTX_get_extra_chain_certs(ctx, &tmp_ca), 1);
  10610. ExpectNotNull(tmp_ca);
  10611. ExpectIntEQ(sk_X509_num(tmp_ca), sk_X509_num(ca));
  10612. /* Check that the main cert is also set */
  10613. ExpectNotNull(SSL_CTX_get0_certificate(ctx));
  10614. ExpectNotNull(ssl = SSL_new(ctx));
  10615. ExpectNotNull(SSL_get_certificate(ssl));
  10616. SSL_free(ssl);
  10617. SSL_CTX_free(ctx);
  10618. ctx = NULL;
  10619. #endif
  10620. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10621. /* should be 2 other certs on stack */
  10622. ExpectNotNull(tmp = sk_X509_pop(ca));
  10623. X509_free(tmp);
  10624. ExpectNotNull(tmp = sk_X509_pop(ca));
  10625. X509_free(tmp);
  10626. ExpectNull(sk_X509_pop(ca));
  10627. EVP_PKEY_free(pkey);
  10628. pkey = NULL;
  10629. X509_free(cert);
  10630. cert = NULL;
  10631. sk_X509_pop_free(ca, X509_free);
  10632. ca = NULL;
  10633. /* check PKCS12_create */
  10634. ExpectNull(PKCS12_create(pass, NULL, NULL, NULL, NULL, -1, -1, -1, -1,0));
  10635. ExpectIntEQ(PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10636. SSL_SUCCESS);
  10637. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  10638. -1, -1, 100, -1, 0)));
  10639. EVP_PKEY_free(pkey);
  10640. pkey = NULL;
  10641. X509_free(cert);
  10642. cert = NULL;
  10643. sk_X509_pop_free(ca, NULL);
  10644. ca = NULL;
  10645. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10646. SSL_SUCCESS);
  10647. PKCS12_free(pkcs12_2);
  10648. pkcs12_2 = NULL;
  10649. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, ca,
  10650. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  10651. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  10652. 2000, 1, 0)));
  10653. EVP_PKEY_free(pkey);
  10654. pkey = NULL;
  10655. X509_free(cert);
  10656. cert = NULL;
  10657. sk_X509_pop_free(ca, NULL);
  10658. ca = NULL;
  10659. /* convert to DER then back and parse */
  10660. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10661. ExpectIntEQ(i2d_PKCS12_bio(bio, pkcs12_2), SSL_SUCCESS);
  10662. PKCS12_free(pkcs12_2);
  10663. pkcs12_2 = NULL;
  10664. ExpectNotNull(pkcs12_2 = d2i_PKCS12_bio(bio, NULL));
  10665. BIO_free(bio);
  10666. bio = NULL;
  10667. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10668. SSL_SUCCESS);
  10669. /* should be 2 other certs on stack */
  10670. ExpectNotNull(tmp = sk_X509_pop(ca));
  10671. X509_free(tmp);
  10672. ExpectNotNull(tmp = sk_X509_pop(ca));
  10673. X509_free(tmp);
  10674. ExpectNull(sk_X509_pop(ca));
  10675. #ifndef NO_RC4
  10676. PKCS12_free(pkcs12_2);
  10677. pkcs12_2 = NULL;
  10678. ExpectNotNull((pkcs12_2 = PKCS12_create(pass, NULL, pkey, cert, NULL,
  10679. NID_pbe_WithSHA1And128BitRC4,
  10680. NID_pbe_WithSHA1And128BitRC4,
  10681. 2000, 1, 0)));
  10682. EVP_PKEY_free(pkey);
  10683. pkey = NULL;
  10684. X509_free(cert);
  10685. cert = NULL;
  10686. sk_X509_pop_free(ca, NULL);
  10687. ca = NULL;
  10688. ExpectIntEQ(PKCS12_parse(pkcs12_2, "a password", &pkey, &cert, &ca),
  10689. SSL_SUCCESS);
  10690. #endif /* NO_RC4 */
  10691. EVP_PKEY_free(pkey);
  10692. pkey = NULL;
  10693. X509_free(cert);
  10694. cert = NULL;
  10695. PKCS12_free(pkcs12);
  10696. pkcs12 = NULL;
  10697. PKCS12_free(pkcs12_2);
  10698. pkcs12_2 = NULL;
  10699. sk_X509_pop_free(ca, NULL);
  10700. ca = NULL;
  10701. #ifdef HAVE_ECC
  10702. /* test order of parsing */
  10703. ExpectTrue((f = XFOPEN(order, "rb")) != XBADFILE);
  10704. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10705. if (f != XBADFILE) {
  10706. XFCLOSE(f);
  10707. f = XBADFILE;
  10708. }
  10709. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  10710. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  10711. ExpectIntEQ((ret = PKCS12_parse(pkcs12, "", &pkey, &cert, &ca)),
  10712. WOLFSSL_SUCCESS);
  10713. /* check use of pkey after parse */
  10714. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) || defined(WOLFSSL_HAPROXY) \
  10715. || defined(WOLFSSL_NGINX)) && defined(SESSION_CERTS)
  10716. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10717. #if !defined(NO_WOLFSSL_CLIENT) && defined(SESSION_CERTS)
  10718. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  10719. #else
  10720. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  10721. #endif
  10722. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), WOLFSSL_SUCCESS);
  10723. SSL_CTX_free(ctx);
  10724. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  10725. #endif
  10726. ExpectNotNull(pkey);
  10727. ExpectNotNull(cert);
  10728. ExpectNotNull(ca);
  10729. /* compare subject lines of certificates */
  10730. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  10731. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccRsaCertFile,
  10732. SSL_FILETYPE_PEM));
  10733. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10734. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10735. X509_free(x509);
  10736. x509 = NULL;
  10737. /* test expected fail case */
  10738. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  10739. SSL_FILETYPE_PEM));
  10740. ExpectIntNE(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10741. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10742. X509_free(x509);
  10743. x509 = NULL;
  10744. X509_free(cert);
  10745. cert = NULL;
  10746. /* get subject line from ca stack */
  10747. ExpectNotNull(cert = sk_X509_pop(ca));
  10748. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(cert));
  10749. /* compare subject from certificate in ca to expected */
  10750. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(eccCertFile,
  10751. SSL_FILETYPE_PEM));
  10752. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10753. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10754. EVP_PKEY_free(pkey);
  10755. pkey = NULL;
  10756. X509_free(x509);
  10757. x509 = NULL;
  10758. X509_free(cert);
  10759. cert = NULL;
  10760. BIO_free(bio);
  10761. bio = NULL;
  10762. PKCS12_free(pkcs12);
  10763. pkcs12 = NULL;
  10764. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  10765. ca = NULL;
  10766. /* test order of parsing */
  10767. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10768. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  10769. if (f != XBADFILE) {
  10770. XFCLOSE(f);
  10771. f = XBADFILE;
  10772. }
  10773. /* check verify MAC fail case */
  10774. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10775. ExpectNull(pkey);
  10776. ExpectNull(cert);
  10777. /* check parse with no extra certs kept */
  10778. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10779. 1);
  10780. ExpectNotNull(pkey);
  10781. ExpectNotNull(cert);
  10782. wolfSSL_EVP_PKEY_free(pkey);
  10783. pkey = NULL;
  10784. wolfSSL_X509_free(cert);
  10785. cert = NULL;
  10786. /* check parse with extra certs kept */
  10787. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10788. 1);
  10789. ExpectNotNull(pkey);
  10790. ExpectNotNull(cert);
  10791. ExpectNotNull(ca);
  10792. wolfSSL_EVP_PKEY_free(pkey);
  10793. pkey = NULL;
  10794. wolfSSL_X509_free(cert);
  10795. cert = NULL;
  10796. sk_X509_pop_free(ca, NULL);
  10797. ca = NULL;
  10798. PKCS12_free(pkcs12);
  10799. pkcs12 = NULL;
  10800. #endif /* HAVE_ECC */
  10801. #ifdef WC_RC2
  10802. /* test PKCS#12 with RC2 encryption */
  10803. ExpectTrue((f = XFOPEN(rc2p12, "rb")) != XBADFILE);
  10804. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10805. if (f != XBADFILE) {
  10806. XFCLOSE(f);
  10807. f = XBADFILE;
  10808. }
  10809. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  10810. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  10811. /* check verify MAC fail case */
  10812. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10813. ExpectNull(pkey);
  10814. ExpectNull(cert);
  10815. /* check parse with not extra certs kept */
  10816. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10817. WOLFSSL_SUCCESS);
  10818. ExpectNotNull(pkey);
  10819. ExpectNotNull(cert);
  10820. wolfSSL_EVP_PKEY_free(pkey);
  10821. pkey = NULL;
  10822. wolfSSL_X509_free(cert);
  10823. cert = NULL;
  10824. /* check parse with extra certs kept */
  10825. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10826. WOLFSSL_SUCCESS);
  10827. ExpectNotNull(pkey);
  10828. ExpectNotNull(cert);
  10829. ExpectNotNull(ca);
  10830. wolfSSL_EVP_PKEY_free(pkey);
  10831. wolfSSL_X509_free(cert);
  10832. sk_X509_pop_free(ca, NULL);
  10833. BIO_free(bio);
  10834. bio = NULL;
  10835. PKCS12_free(pkcs12);
  10836. pkcs12 = NULL;
  10837. #endif /* WC_RC2 */
  10838. /* Test i2d_PKCS12_bio */
  10839. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10840. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  10841. if (f != XBADFILE)
  10842. XFCLOSE(f);
  10843. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10844. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, pkcs12), 1);
  10845. ExpectIntEQ(ret = i2d_PKCS12_bio(NULL, pkcs12), 0);
  10846. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, NULL), 0);
  10847. PKCS12_free(pkcs12);
  10848. BIO_free(bio);
  10849. (void)order;
  10850. #endif /* OPENSSL_EXTRA */
  10851. #endif /* HAVE_FIPS */
  10852. return EXPECT_RESULT();
  10853. }
  10854. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  10855. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  10856. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  10857. #define TEST_PKCS8_ENC
  10858. #endif
  10859. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  10860. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  10861. /* used to keep track if FailTestCallback was called */
  10862. static int failTestCallbackCalled = 0;
  10863. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  10864. {
  10865. (void)passwd;
  10866. (void)sz;
  10867. (void)rw;
  10868. (void)userdata;
  10869. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  10870. failTestCallbackCalled = 1;
  10871. return -1;
  10872. }
  10873. #endif
  10874. static int test_wolfSSL_no_password_cb(void)
  10875. {
  10876. EXPECT_DECLS;
  10877. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  10878. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  10879. WOLFSSL_CTX* ctx = NULL;
  10880. byte buff[FOURK_BUF];
  10881. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  10882. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  10883. XFILE f = XBADFILE;
  10884. int bytes = 0;
  10885. #ifndef NO_WOLFSSL_CLIENT
  10886. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  10887. #else
  10888. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  10889. #endif
  10890. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  10891. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  10892. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10893. if (f != XBADFILE) {
  10894. XFCLOSE(f);
  10895. f = XBADFILE;
  10896. }
  10897. ExpectIntLE(bytes, sizeof(buff));
  10898. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10899. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10900. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  10901. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10902. if (f != XBADFILE)
  10903. XFCLOSE(f);
  10904. ExpectIntLE(bytes, sizeof(buff));
  10905. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10906. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10907. wolfSSL_CTX_free(ctx);
  10908. /* Password callback should not be called by default */
  10909. ExpectIntEQ(failTestCallbackCalled, 0);
  10910. #endif
  10911. return EXPECT_RESULT();
  10912. }
  10913. #ifdef TEST_PKCS8_ENC
  10914. /* for PKCS8 test case */
  10915. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  10916. {
  10917. int flag = 0;
  10918. (void)rw;
  10919. if (userdata != NULL) {
  10920. flag = *((int*)userdata); /* user set data */
  10921. }
  10922. switch (flag) {
  10923. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  10924. * can be anything the user wishes to be passed to the callback
  10925. * associated with the WOLFSSL_CTX */
  10926. XSTRNCPY(passwd, "yassl123", sz);
  10927. return 8;
  10928. default:
  10929. return BAD_FUNC_ARG;
  10930. }
  10931. }
  10932. #endif /* TEST_PKCS8_ENC */
  10933. /* Testing functions dealing with PKCS8 */
  10934. static int test_wolfSSL_PKCS8(void)
  10935. {
  10936. EXPECT_DECLS;
  10937. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  10938. !defined(WOLFCRYPT_ONLY)
  10939. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10940. byte buff[FOURK_BUF];
  10941. byte der[FOURK_BUF];
  10942. #ifndef NO_RSA
  10943. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  10944. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  10945. #endif
  10946. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  10947. #ifdef HAVE_ECC
  10948. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  10949. #endif
  10950. XFILE f = XBADFILE;
  10951. int bytes = 0;
  10952. WOLFSSL_CTX* ctx = NULL;
  10953. #if defined(HAVE_ECC) && !defined(NO_CODING) && !defined(WOLFSSL_NO_PEM)
  10954. int ret;
  10955. ecc_key key;
  10956. word32 x = 0;
  10957. #endif
  10958. #ifdef TEST_PKCS8_ENC
  10959. #if !defined(NO_RSA) && !defined(NO_SHA)
  10960. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  10961. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  10962. #endif
  10963. #if defined(HAVE_ECC) && !defined(NO_SHA)
  10964. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  10965. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  10966. #endif
  10967. int flag;
  10968. #endif
  10969. (void)der;
  10970. #ifndef NO_WOLFSSL_CLIENT
  10971. #ifndef WOLFSSL_NO_TLS12
  10972. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  10973. #else
  10974. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  10975. #endif
  10976. #else
  10977. #ifndef WOLFSSL_NO_TLS12
  10978. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  10979. #else
  10980. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  10981. #endif
  10982. #endif
  10983. #ifdef TEST_PKCS8_ENC
  10984. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  10985. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  10986. flag = 1; /* used by password callback as return code */
  10987. #if !defined(NO_RSA) && !defined(NO_SHA)
  10988. /* test loading PEM PKCS8 encrypted file */
  10989. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncPemFile, "rb")) != XBADFILE);
  10990. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10991. if (f != XBADFILE) {
  10992. XFCLOSE(f);
  10993. f = XBADFILE;
  10994. }
  10995. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10996. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10997. /* this next case should fail because of password callback return code */
  10998. flag = 0; /* used by password callback as return code */
  10999. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11000. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11001. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  11002. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11003. "yassl123"), 0);
  11004. /* test that error value is returned with a bad password */
  11005. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11006. "bad"), 0);
  11007. /* test loading PEM PKCS8 encrypted file */
  11008. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncDerFile, "rb")) != XBADFILE);
  11009. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11010. if (f != XBADFILE) {
  11011. XFCLOSE(f);
  11012. f = XBADFILE;
  11013. }
  11014. flag = 1; /* used by password callback as return code */
  11015. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11016. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11017. /* this next case should fail because of password callback return code */
  11018. flag = 0; /* used by password callback as return code */
  11019. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11020. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11021. #endif /* !NO_RSA && !NO_SHA */
  11022. #if defined(HAVE_ECC) && !defined(NO_SHA)
  11023. /* test loading PEM PKCS8 encrypted ECC Key file */
  11024. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb")) != XBADFILE);
  11025. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11026. if (f != XBADFILE) {
  11027. XFCLOSE(f);
  11028. f = XBADFILE;
  11029. }
  11030. flag = 1; /* used by password callback as return code */
  11031. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11032. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11033. /* this next case should fail because of password callback return code */
  11034. flag = 0; /* used by password callback as return code */
  11035. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11036. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11037. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  11038. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11039. "yassl123"), 0);
  11040. /* test that error value is returned with a bad password */
  11041. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11042. "bad"), 0);
  11043. /* test loading DER PKCS8 encrypted ECC Key file */
  11044. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb")) != XBADFILE);
  11045. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11046. if (f != XBADFILE) {
  11047. XFCLOSE(f);
  11048. f = XBADFILE;
  11049. }
  11050. flag = 1; /* used by password callback as return code */
  11051. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11052. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11053. /* this next case should fail because of password callback return code */
  11054. flag = 0; /* used by password callback as return code */
  11055. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11056. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11057. /* leave flag as "okay" */
  11058. flag = 1;
  11059. #endif /* HAVE_ECC && !NO_SHA */
  11060. #endif /* TEST_PKCS8_ENC */
  11061. #ifndef NO_RSA
  11062. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  11063. ExpectTrue((f = XFOPEN(serverKeyPkcs8DerFile, "rb")) != XBADFILE);
  11064. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11065. if (f != XBADFILE) {
  11066. XFCLOSE(f);
  11067. f = XBADFILE;
  11068. }
  11069. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11070. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11071. /* test loading PEM PKCS8 private key file (not encrypted) */
  11072. ExpectTrue((f = XFOPEN(serverKeyPkcs8PemFile, "rb")) != XBADFILE);
  11073. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11074. if (f != XBADFILE) {
  11075. XFCLOSE(f);
  11076. f = XBADFILE;
  11077. }
  11078. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11079. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11080. #endif /* !NO_RSA */
  11081. /* Test PKCS8 PEM ECC key no crypt */
  11082. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  11083. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11084. if (f != XBADFILE) {
  11085. XFCLOSE(f);
  11086. f = XBADFILE;
  11087. }
  11088. #ifdef HAVE_ECC
  11089. /* Test PKCS8 PEM ECC key no crypt */
  11090. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11091. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11092. #if !defined(NO_CODING) && !defined(WOLFSSL_NO_PEM)
  11093. /* decrypt PKCS8 PEM to key in DER format */
  11094. ExpectIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  11095. (word32)sizeof(der), NULL)), 0);
  11096. ret = wc_ecc_init(&key);
  11097. if (ret == 0) {
  11098. ret = wc_EccPrivateKeyDecode(der, &x, &key, bytes);
  11099. wc_ecc_free(&key);
  11100. }
  11101. ExpectIntEQ(ret, 0);
  11102. #endif
  11103. /* Test PKCS8 DER ECC key no crypt */
  11104. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  11105. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11106. if (f != XBADFILE)
  11107. XFCLOSE(f);
  11108. /* Test using a PKCS8 ECC PEM */
  11109. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11110. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11111. #else
  11112. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  11113. ExpectIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  11114. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  11115. #endif /* HAVE_ECC */
  11116. wolfSSL_CTX_free(ctx);
  11117. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11118. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  11119. return EXPECT_RESULT();
  11120. }
  11121. static int test_wolfSSL_PKCS8_ED25519(void)
  11122. {
  11123. EXPECT_DECLS;
  11124. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  11125. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  11126. defined(HAVE_ED25519_KEY_IMPORT)
  11127. const byte encPrivKey[] = \
  11128. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  11129. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  11130. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  11131. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  11132. "2L9W4v7swXkV+X+a1ww=\n"
  11133. "-----END ENCRYPTED PRIVATE KEY-----\n";
  11134. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  11135. byte der[FOURK_BUF];
  11136. WOLFSSL_CTX* ctx = NULL;
  11137. int bytes;
  11138. XMEMSET(der, 0, sizeof(der));
  11139. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  11140. (word32)sizeof(der), password)), 0);
  11141. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  11142. #ifndef NO_WOLFSSL_SERVER
  11143. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  11144. #else
  11145. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  11146. #endif
  11147. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  11148. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11149. wolfSSL_CTX_free(ctx);
  11150. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11151. #endif
  11152. return EXPECT_RESULT();
  11153. }
  11154. static int test_wolfSSL_PKCS8_ED448(void)
  11155. {
  11156. EXPECT_DECLS;
  11157. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  11158. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  11159. defined(HAVE_ED448_KEY_IMPORT)
  11160. const byte encPrivKey[] = \
  11161. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  11162. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  11163. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  11164. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  11165. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  11166. "-----END ENCRYPTED PRIVATE KEY-----\n";
  11167. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  11168. byte der[FOURK_BUF];
  11169. WOLFSSL_CTX* ctx = NULL;
  11170. int bytes;
  11171. XMEMSET(der, 0, sizeof(der));
  11172. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  11173. (word32)sizeof(der), password)), 0);
  11174. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  11175. #ifndef NO_WOLFSSL_SERVER
  11176. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  11177. #else
  11178. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  11179. #endif
  11180. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  11181. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11182. wolfSSL_CTX_free(ctx);
  11183. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11184. #endif
  11185. return EXPECT_RESULT();
  11186. }
  11187. /* Testing functions dealing with PKCS5 */
  11188. static int test_wolfSSL_PKCS5(void)
  11189. {
  11190. EXPECT_DECLS;
  11191. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  11192. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  11193. const char* passwd = "myfipsPa$$W0rd";
  11194. #else
  11195. const char *passwd = "pass1234";
  11196. #endif
  11197. const unsigned char *salt = (unsigned char *)"salt1234";
  11198. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  11199. DYNAMIC_TYPE_TMP_BUFFER);
  11200. int ret = 0;
  11201. ExpectNotNull(out);
  11202. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  11203. (int)XSTRLEN((const char *) salt), 10, WC_SHA_DIGEST_SIZE,out),
  11204. WOLFSSL_SUCCESS);
  11205. #ifdef WOLFSSL_SHA512
  11206. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  11207. (int)XSTRLEN((const char *) salt), 10, wolfSSL_EVP_sha512(),
  11208. WC_SHA_DIGEST_SIZE, out), SSL_SUCCESS);
  11209. #endif
  11210. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11211. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  11212. return EXPECT_RESULT();
  11213. }
  11214. /* test parsing URI from certificate */
  11215. static int test_wolfSSL_URI(void)
  11216. {
  11217. EXPECT_DECLS;
  11218. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  11219. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  11220. defined(OPENSSL_EXTRA))
  11221. WOLFSSL_X509* x509 = NULL;
  11222. const char uri[] = "./certs/client-uri-cert.pem";
  11223. const char urn[] = "./certs/client-absolute-urn.pem";
  11224. const char badUri[] = "./certs/client-relative-uri.pem";
  11225. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(uri,
  11226. WOLFSSL_FILETYPE_PEM));
  11227. wolfSSL_FreeX509(x509);
  11228. x509 = NULL;
  11229. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(urn,
  11230. WOLFSSL_FILETYPE_PEM));
  11231. wolfSSL_FreeX509(x509);
  11232. x509 = NULL;
  11233. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  11234. && !defined(WOLFSSL_FPKI)
  11235. ExpectNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  11236. WOLFSSL_FILETYPE_PEM));
  11237. #else
  11238. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  11239. WOLFSSL_FILETYPE_PEM));
  11240. #endif
  11241. wolfSSL_FreeX509(x509);
  11242. #endif
  11243. return EXPECT_RESULT();
  11244. }
  11245. static int test_wolfSSL_TBS(void)
  11246. {
  11247. EXPECT_DECLS;
  11248. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  11249. && defined(OPENSSL_EXTRA)
  11250. WOLFSSL_X509* x509 = NULL;
  11251. const unsigned char* tbs;
  11252. int tbsSz;
  11253. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caCertFile,
  11254. WOLFSSL_FILETYPE_PEM));
  11255. ExpectNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  11256. ExpectNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  11257. ExpectNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  11258. ExpectIntEQ(tbsSz, 1003);
  11259. wolfSSL_FreeX509(x509);
  11260. #endif
  11261. return EXPECT_RESULT();
  11262. }
  11263. static int test_wolfSSL_X509_verify(void)
  11264. {
  11265. EXPECT_DECLS;
  11266. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  11267. defined(OPENSSL_EXTRA)
  11268. WOLFSSL_X509* ca = NULL;
  11269. WOLFSSL_X509* serv = NULL;
  11270. WOLFSSL_EVP_PKEY* pkey = NULL;
  11271. unsigned char buf[2048];
  11272. const unsigned char* pt = NULL;
  11273. int bufSz;
  11274. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(caCertFile,
  11275. WOLFSSL_FILETYPE_PEM));
  11276. ExpectIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  11277. WOLFSSL_SUCCESS);
  11278. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  11279. WOLFSSL_SUCCESS);
  11280. ExpectIntEQ(bufSz, 294);
  11281. bufSz = 2048;
  11282. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  11283. WOLFSSL_SUCCESS);
  11284. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  11285. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  11286. ExpectNotNull(serv = wolfSSL_X509_load_certificate_file(svrCertFile,
  11287. WOLFSSL_FILETYPE_PEM));
  11288. /* success case */
  11289. pt = buf;
  11290. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  11291. ExpectIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  11292. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  11293. wolfSSL_EVP_PKEY_free(pkey);
  11294. pkey = NULL;
  11295. /* fail case */
  11296. bufSz = 2048;
  11297. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  11298. WOLFSSL_SUCCESS);
  11299. pt = buf;
  11300. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  11301. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  11302. ExpectIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  11303. ExpectIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  11304. wolfSSL_EVP_PKEY_free(pkey);
  11305. wolfSSL_FreeX509(ca);
  11306. wolfSSL_FreeX509(serv);
  11307. #endif
  11308. return EXPECT_RESULT();
  11309. }
  11310. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11311. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11312. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11313. /* create certificate with version 2 */
  11314. static int test_set_x509_badversion(WOLFSSL_CTX* ctx)
  11315. {
  11316. EXPECT_DECLS;
  11317. WOLFSSL_X509 *x509 = NULL, *x509v2 = NULL;
  11318. WOLFSSL_EVP_PKEY *priv = NULL, *pub = NULL;
  11319. unsigned char *der = NULL, *key = NULL, *pt;
  11320. char *header = NULL, *name = NULL;
  11321. int derSz;
  11322. long keySz;
  11323. XFILE fp = XBADFILE;
  11324. WOLFSSL_ASN1_TIME *notBefore = NULL, *notAfter = NULL;
  11325. time_t t;
  11326. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  11327. WOLFSSL_FILETYPE_PEM));
  11328. ExpectTrue((fp = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  11329. ExpectIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  11330. WOLFSSL_SUCCESS);
  11331. if (fp != XBADFILE)
  11332. XFCLOSE(fp);
  11333. pt = key;
  11334. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  11335. (const unsigned char**)&pt, keySz));
  11336. /* create the version 2 certificate */
  11337. ExpectNotNull(x509v2 = X509_new());
  11338. ExpectIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  11339. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  11340. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  11341. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  11342. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  11343. ExpectNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  11344. ExpectIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  11345. t = time(NULL);
  11346. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  11347. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  11348. ExpectTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  11349. ExpectTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  11350. ExpectIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  11351. derSz = wolfSSL_i2d_X509(x509v2, &der);
  11352. ExpectIntGT(derSz, 0);
  11353. ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  11354. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11355. /* TODO: Replace with API call */
  11356. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  11357. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11358. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11359. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11360. wolfSSL_X509_free(x509);
  11361. wolfSSL_X509_free(x509v2);
  11362. wolfSSL_EVP_PKEY_free(priv);
  11363. wolfSSL_EVP_PKEY_free(pub);
  11364. wolfSSL_ASN1_TIME_free(notBefore);
  11365. wolfSSL_ASN1_TIME_free(notAfter);
  11366. return EXPECT_RESULT();
  11367. }
  11368. /* override certificate version error */
  11369. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  11370. {
  11371. EXPECT_DECLS;
  11372. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  11373. ExpectIntEQ(store->error, ASN_VERSION_E);
  11374. #else
  11375. ExpectIntEQ(store->error, 0);
  11376. #endif
  11377. ExpectIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  11378. (void)preverify;
  11379. return EXPECT_RESULT() == TEST_SUCCESS;
  11380. }
  11381. /* set verify callback that will override bad certificate version */
  11382. static int test_set_override_x509(WOLFSSL_CTX* ctx)
  11383. {
  11384. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  11385. return TEST_SUCCESS;
  11386. }
  11387. #endif
  11388. static int test_wolfSSL_X509_TLS_version_test_1(void)
  11389. {
  11390. EXPECT_DECLS;
  11391. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11392. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11393. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11394. test_ssl_cbf func_cb_client;
  11395. test_ssl_cbf func_cb_server;
  11396. /* test server rejects a client certificate that is not version 3 */
  11397. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  11398. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  11399. func_cb_client.ctx_ready = &test_set_x509_badversion;
  11400. #ifndef WOLFSSL_NO_TLS12
  11401. func_cb_client.method = wolfTLSv1_2_client_method;
  11402. #else
  11403. func_cb_client.method = wolfTLSv1_3_client_method;
  11404. #endif
  11405. #ifndef WOLFSSL_NO_TLS12
  11406. func_cb_server.method = wolfTLSv1_2_server_method;
  11407. #else
  11408. func_cb_server.method = wolfTLSv1_3_server_method;
  11409. #endif
  11410. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  11411. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11412. &func_cb_server, NULL), TEST_FAIL);
  11413. #else
  11414. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11415. &func_cb_server, NULL), TEST_SUCCESS);
  11416. #endif
  11417. #endif
  11418. return EXPECT_RESULT();
  11419. }
  11420. static int test_wolfSSL_X509_TLS_version_test_2(void)
  11421. {
  11422. EXPECT_DECLS;
  11423. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11424. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11425. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11426. test_ssl_cbf func_cb_client;
  11427. test_ssl_cbf func_cb_server;
  11428. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  11429. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  11430. func_cb_client.ctx_ready = &test_set_x509_badversion;
  11431. func_cb_server.ctx_ready = &test_set_override_x509;
  11432. #ifndef WOLFSSL_NO_TLS12
  11433. func_cb_client.method = wolfTLSv1_2_client_method;
  11434. #else
  11435. func_cb_client.method = wolfTLSv1_3_client_method;
  11436. #endif
  11437. #ifndef WOLFSSL_NO_TLS12
  11438. func_cb_server.method = wolfTLSv1_2_server_method;
  11439. #else
  11440. func_cb_server.method = wolfTLSv1_3_server_method;
  11441. #endif
  11442. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11443. &func_cb_server, NULL), TEST_SUCCESS);
  11444. #endif
  11445. return EXPECT_RESULT();
  11446. }
  11447. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  11448. * version allowed.
  11449. * POST: 1 on success.
  11450. */
  11451. static int test_wolfSSL_CTX_SetMinVersion(void)
  11452. {
  11453. int res = TEST_SKIPPED;
  11454. #ifndef NO_WOLFSSL_CLIENT
  11455. int failFlag = WOLFSSL_SUCCESS;
  11456. WOLFSSL_CTX* ctx;
  11457. int itr;
  11458. #ifndef NO_OLD_TLS
  11459. const int versions[] = {
  11460. #ifdef WOLFSSL_ALLOW_TLSV10
  11461. WOLFSSL_TLSV1,
  11462. #endif
  11463. WOLFSSL_TLSV1_1,
  11464. WOLFSSL_TLSV1_2 };
  11465. #elif !defined(WOLFSSL_NO_TLS12)
  11466. const int versions[] = { WOLFSSL_TLSV1_2 };
  11467. #elif defined(WOLFSSL_TLS13)
  11468. const int versions[] = { WOLFSSL_TLSV1_3 };
  11469. #else
  11470. const int versions[0];
  11471. #endif
  11472. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  11473. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  11474. if (wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr))
  11475. != WOLFSSL_SUCCESS) {
  11476. failFlag = WOLFSSL_FAILURE;
  11477. }
  11478. }
  11479. wolfSSL_CTX_free(ctx);
  11480. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  11481. #endif
  11482. return res;
  11483. } /* END test_wolfSSL_CTX_SetMinVersion */
  11484. /*----------------------------------------------------------------------------*
  11485. | OCSP Stapling
  11486. *----------------------------------------------------------------------------*/
  11487. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  11488. * need to contact the CA, lowering the cost of cert revocation checking.
  11489. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  11490. * POST: 1 returned for success.
  11491. */
  11492. static int test_wolfSSL_UseOCSPStapling(void)
  11493. {
  11494. EXPECT_DECLS;
  11495. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  11496. !defined(NO_WOLFSSL_CLIENT)
  11497. WOLFSSL_CTX* ctx = NULL;
  11498. WOLFSSL* ssl = NULL;
  11499. #ifndef NO_WOLFSSL_CLIENT
  11500. #ifndef WOLFSSL_NO_TLS12
  11501. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11502. #else
  11503. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11504. #endif
  11505. #else
  11506. #ifndef WOLFSSL_NO_TLS12
  11507. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11508. #else
  11509. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11510. #endif
  11511. #endif
  11512. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11513. ExpectIntEQ(wolfSSL_UseOCSPStapling(NULL, WOLFSSL_CSR2_OCSP,
  11514. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11515. #ifndef NO_WOLFSSL_CLIENT
  11516. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  11517. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  11518. #else
  11519. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  11520. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11521. #endif
  11522. wolfSSL_free(ssl);
  11523. wolfSSL_CTX_free(ctx);
  11524. #endif
  11525. return EXPECT_RESULT();
  11526. } /* END test_wolfSSL_UseOCSPStapling */
  11527. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  11528. * stapling eliminates the need to contact the CA and lowers cert revocation
  11529. * check.
  11530. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  11531. */
  11532. static int test_wolfSSL_UseOCSPStaplingV2(void)
  11533. {
  11534. EXPECT_DECLS;
  11535. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  11536. !defined(NO_WOLFSSL_CLIENT)
  11537. WOLFSSL_CTX* ctx = NULL;
  11538. WOLFSSL* ssl = NULL;
  11539. #ifndef NO_WOLFSSL_CLIENT
  11540. #ifndef WOLFSSL_NO_TLS12
  11541. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11542. #else
  11543. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11544. #endif
  11545. #else
  11546. #ifndef WOLFSSL_NO_TLS12
  11547. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11548. #else
  11549. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11550. #endif
  11551. #endif
  11552. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11553. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(NULL, WOLFSSL_CSR2_OCSP,
  11554. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11555. #ifndef NO_WOLFSSL_CLIENT
  11556. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  11557. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  11558. #else
  11559. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  11560. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11561. #endif
  11562. wolfSSL_free(ssl);
  11563. wolfSSL_CTX_free(ctx);
  11564. #endif
  11565. return EXPECT_RESULT();
  11566. } /* END test_wolfSSL_UseOCSPStaplingV2 */
  11567. /*----------------------------------------------------------------------------*
  11568. | Multicast Tests
  11569. *----------------------------------------------------------------------------*/
  11570. static int test_wolfSSL_mcast(void)
  11571. {
  11572. EXPECT_DECLS;
  11573. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  11574. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  11575. WOLFSSL_CTX* ctx = NULL;
  11576. WOLFSSL* ssl = NULL;
  11577. byte preMasterSecret[512];
  11578. byte clientRandom[32];
  11579. byte serverRandom[32];
  11580. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  11581. byte buf[256];
  11582. word16 newId;
  11583. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  11584. ExpectIntEQ(wolfSSL_CTX_mcast_set_member_id(ctx, 0), WOLFSSL_SUCCESS);
  11585. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11586. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  11587. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  11588. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  11589. ExpectIntEQ(wolfSSL_set_secret(ssl, 23, preMasterSecret,
  11590. sizeof(preMasterSecret), clientRandom, serverRandom, suite),
  11591. WOLFSSL_SUCCESS);
  11592. ExpectIntLE(wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf)), 0);
  11593. ExpectIntLE(newId, 100);
  11594. wolfSSL_free(ssl);
  11595. wolfSSL_CTX_free(ctx);
  11596. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 ||
  11597. * WOLFSSL_SNIFFER) */
  11598. return EXPECT_RESULT();
  11599. }
  11600. /*----------------------------------------------------------------------------*
  11601. | Wolfcrypt
  11602. *----------------------------------------------------------------------------*/
  11603. /*
  11604. * Unit test for the wc_InitBlake2b()
  11605. */
  11606. static int test_wc_InitBlake2b(void)
  11607. {
  11608. EXPECT_DECLS;
  11609. #ifdef HAVE_BLAKE2
  11610. Blake2b blake;
  11611. /* Test good arg. */
  11612. ExpectIntEQ(wc_InitBlake2b(&blake, 64), 0);
  11613. /* Test bad arg. */
  11614. ExpectIntEQ(wc_InitBlake2b(NULL, 64), BAD_FUNC_ARG);
  11615. ExpectIntEQ(wc_InitBlake2b(NULL, 128), BAD_FUNC_ARG);
  11616. ExpectIntEQ(wc_InitBlake2b(&blake, 128), BAD_FUNC_ARG);
  11617. ExpectIntEQ(wc_InitBlake2b(NULL, 0), BAD_FUNC_ARG);
  11618. ExpectIntEQ(wc_InitBlake2b(&blake, 0), BAD_FUNC_ARG);
  11619. #endif
  11620. return EXPECT_RESULT();
  11621. } /* END test_wc_InitBlake2b*/
  11622. /*
  11623. * Unit test for the wc_InitBlake2b_WithKey()
  11624. */
  11625. static int test_wc_InitBlake2b_WithKey(void)
  11626. {
  11627. EXPECT_DECLS;
  11628. #ifdef HAVE_BLAKE2
  11629. Blake2b blake;
  11630. word32 digestSz = BLAKE2B_KEYBYTES;
  11631. byte key[BLAKE2B_KEYBYTES];
  11632. word32 keylen = BLAKE2B_KEYBYTES;
  11633. XMEMSET(key, 0, sizeof(key));
  11634. /* Test good arg. */
  11635. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen), 0);
  11636. /* Test bad args. */
  11637. ExpectIntEQ(wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen),
  11638. BAD_FUNC_ARG);
  11639. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, 256),
  11640. BAD_FUNC_ARG);
  11641. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen), 0);
  11642. #endif
  11643. return EXPECT_RESULT();
  11644. } /* END wc_InitBlake2b_WithKey*/
  11645. /*
  11646. * Unit test for the wc_InitBlake2s_WithKey()
  11647. */
  11648. static int test_wc_InitBlake2s_WithKey(void)
  11649. {
  11650. EXPECT_DECLS;
  11651. #ifdef HAVE_BLAKE2S
  11652. Blake2s blake;
  11653. word32 digestSz = BLAKE2S_KEYBYTES;
  11654. byte *key = (byte*)"01234567890123456789012345678901";
  11655. word32 keylen = BLAKE2S_KEYBYTES;
  11656. /* Test good arg. */
  11657. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen), 0);
  11658. /* Test bad args. */
  11659. ExpectIntEQ(wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen),
  11660. BAD_FUNC_ARG);
  11661. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, 256),
  11662. BAD_FUNC_ARG);
  11663. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen), 0);
  11664. #endif
  11665. return EXPECT_RESULT();
  11666. } /* END wc_InitBlake2s_WithKey*/
  11667. /*
  11668. * Unit test for the wc_InitMd5()
  11669. */
  11670. static int test_wc_InitMd5(void)
  11671. {
  11672. EXPECT_DECLS;
  11673. #ifndef NO_MD5
  11674. wc_Md5 md5;
  11675. /* Test good arg. */
  11676. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11677. /* Test bad arg. */
  11678. ExpectIntEQ(wc_InitMd5(NULL), BAD_FUNC_ARG);
  11679. wc_Md5Free(&md5);
  11680. #endif
  11681. return EXPECT_RESULT();
  11682. } /* END test_wc_InitMd5 */
  11683. /*
  11684. * Testing wc_UpdateMd5()
  11685. */
  11686. static int test_wc_Md5Update(void)
  11687. {
  11688. EXPECT_DECLS;
  11689. #ifndef NO_MD5
  11690. wc_Md5 md5;
  11691. byte hash[WC_MD5_DIGEST_SIZE];
  11692. testVector a, b, c;
  11693. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11694. /* Input */
  11695. a.input = "a";
  11696. a.inLen = XSTRLEN(a.input);
  11697. ExpectIntEQ(wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen), 0);
  11698. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  11699. /* Update input. */
  11700. a.input = "abc";
  11701. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  11702. "\x72";
  11703. a.inLen = XSTRLEN(a.input);
  11704. a.outLen = XSTRLEN(a.output);
  11705. ExpectIntEQ(wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen), 0);
  11706. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  11707. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  11708. /* Pass in bad values. */
  11709. b.input = NULL;
  11710. b.inLen = 0;
  11711. ExpectIntEQ(wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen), 0);
  11712. c.input = NULL;
  11713. c.inLen = WC_MD5_DIGEST_SIZE;
  11714. ExpectIntEQ(wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen),
  11715. BAD_FUNC_ARG);
  11716. ExpectIntEQ(wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen),
  11717. BAD_FUNC_ARG);
  11718. wc_Md5Free(&md5);
  11719. #endif
  11720. return EXPECT_RESULT();
  11721. } /* END test_wc_Md5Update() */
  11722. /*
  11723. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  11724. */
  11725. static int test_wc_Md5Final(void)
  11726. {
  11727. EXPECT_DECLS;
  11728. #ifndef NO_MD5
  11729. /* Instantiate */
  11730. wc_Md5 md5;
  11731. byte* hash_test[3];
  11732. byte hash1[WC_MD5_DIGEST_SIZE];
  11733. byte hash2[2*WC_MD5_DIGEST_SIZE];
  11734. byte hash3[5*WC_MD5_DIGEST_SIZE];
  11735. int times, i;
  11736. /* Initialize */
  11737. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11738. hash_test[0] = hash1;
  11739. hash_test[1] = hash2;
  11740. hash_test[2] = hash3;
  11741. times = sizeof(hash_test)/sizeof(byte*);
  11742. for (i = 0; i < times; i++) {
  11743. ExpectIntEQ(wc_Md5Final(&md5, hash_test[i]), 0);
  11744. }
  11745. /* Test bad args. */
  11746. ExpectIntEQ(wc_Md5Final(NULL, NULL), BAD_FUNC_ARG);
  11747. ExpectIntEQ(wc_Md5Final(NULL, hash1), BAD_FUNC_ARG);
  11748. ExpectIntEQ(wc_Md5Final(&md5, NULL), BAD_FUNC_ARG);
  11749. wc_Md5Free(&md5);
  11750. #endif
  11751. return EXPECT_RESULT();
  11752. }
  11753. /*
  11754. * Unit test for the wc_InitSha()
  11755. */
  11756. static int test_wc_InitSha(void)
  11757. {
  11758. EXPECT_DECLS;
  11759. #ifndef NO_SHA
  11760. wc_Sha sha;
  11761. /* Test good arg. */
  11762. ExpectIntEQ(wc_InitSha(&sha), 0);
  11763. /* Test bad arg. */
  11764. ExpectIntEQ(wc_InitSha(NULL), BAD_FUNC_ARG);
  11765. wc_ShaFree(&sha);
  11766. #endif
  11767. return EXPECT_RESULT();
  11768. } /* END test_wc_InitSha */
  11769. /*
  11770. * Tesing wc_ShaUpdate()
  11771. */
  11772. static int test_wc_ShaUpdate(void)
  11773. {
  11774. EXPECT_DECLS;
  11775. #ifndef NO_SHA
  11776. wc_Sha sha;
  11777. byte hash[WC_SHA_DIGEST_SIZE];
  11778. testVector a, b, c;
  11779. ExpectIntEQ(wc_InitSha(&sha), 0);
  11780. /* Input. */
  11781. a.input = "a";
  11782. a.inLen = XSTRLEN(a.input);
  11783. ExpectIntEQ(wc_ShaUpdate(&sha, NULL, 0), 0);
  11784. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, 0), 0);
  11785. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  11786. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  11787. /* Update input. */
  11788. a.input = "abc";
  11789. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  11790. "\x6C\x9C\xD0\xD8\x9D";
  11791. a.inLen = XSTRLEN(a.input);
  11792. a.outLen = XSTRLEN(a.output);
  11793. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  11794. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  11795. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  11796. /* Try passing in bad values. */
  11797. b.input = NULL;
  11798. b.inLen = 0;
  11799. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen), 0);
  11800. c.input = NULL;
  11801. c.inLen = WC_SHA_DIGEST_SIZE;
  11802. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen),
  11803. BAD_FUNC_ARG);
  11804. ExpectIntEQ(wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  11805. BAD_FUNC_ARG);
  11806. wc_ShaFree(&sha);
  11807. #endif
  11808. return EXPECT_RESULT();
  11809. } /* END test_wc_ShaUpdate() */
  11810. /*
  11811. * Unit test on wc_ShaFinal
  11812. */
  11813. static int test_wc_ShaFinal(void)
  11814. {
  11815. EXPECT_DECLS;
  11816. #ifndef NO_SHA
  11817. wc_Sha sha;
  11818. byte* hash_test[3];
  11819. byte hash1[WC_SHA_DIGEST_SIZE];
  11820. byte hash2[2*WC_SHA_DIGEST_SIZE];
  11821. byte hash3[5*WC_SHA_DIGEST_SIZE];
  11822. int times, i;
  11823. /* Initialize*/
  11824. ExpectIntEQ(wc_InitSha(&sha), 0);
  11825. hash_test[0] = hash1;
  11826. hash_test[1] = hash2;
  11827. hash_test[2] = hash3;
  11828. times = sizeof(hash_test)/sizeof(byte*);
  11829. for (i = 0; i < times; i++) {
  11830. ExpectIntEQ(wc_ShaFinal(&sha, hash_test[i]), 0);
  11831. }
  11832. /* Test bad args. */
  11833. ExpectIntEQ(wc_ShaFinal(NULL, NULL), BAD_FUNC_ARG);
  11834. ExpectIntEQ(wc_ShaFinal(NULL, hash1), BAD_FUNC_ARG);
  11835. ExpectIntEQ(wc_ShaFinal(&sha, NULL), BAD_FUNC_ARG);
  11836. wc_ShaFree(&sha);
  11837. #endif
  11838. return EXPECT_RESULT();
  11839. } /* END test_wc_ShaFinal */
  11840. /*
  11841. * Unit test for wc_InitSha256()
  11842. */
  11843. static int test_wc_InitSha256(void)
  11844. {
  11845. EXPECT_DECLS;
  11846. #ifndef NO_SHA256
  11847. wc_Sha256 sha256;
  11848. /* Test good arg. */
  11849. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11850. /* Test bad arg. */
  11851. ExpectIntEQ(wc_InitSha256(NULL), BAD_FUNC_ARG);
  11852. wc_Sha256Free(&sha256);
  11853. #endif
  11854. return EXPECT_RESULT();
  11855. } /* END test_wc_InitSha256 */
  11856. /*
  11857. * Unit test for wc_Sha256Update()
  11858. */
  11859. static int test_wc_Sha256Update(void)
  11860. {
  11861. EXPECT_DECLS;
  11862. #ifndef NO_SHA256
  11863. wc_Sha256 sha256;
  11864. byte hash[WC_SHA256_DIGEST_SIZE];
  11865. byte hash_unaligned[WC_SHA256_DIGEST_SIZE+1];
  11866. testVector a, b, c;
  11867. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11868. /* Input. */
  11869. a.input = "a";
  11870. a.inLen = XSTRLEN(a.input);
  11871. ExpectIntEQ(wc_Sha256Update(&sha256, NULL, 0), 0);
  11872. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, 0), 0);
  11873. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  11874. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  11875. /* Update input. */
  11876. a.input = "abc";
  11877. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  11878. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  11879. "\x15\xAD";
  11880. a.inLen = XSTRLEN(a.input);
  11881. a.outLen = XSTRLEN(a.output);
  11882. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  11883. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  11884. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  11885. /* Unaligned check. */
  11886. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input+1, (word32)a.inLen-1),
  11887. 0);
  11888. ExpectIntEQ(wc_Sha256Final(&sha256, hash_unaligned + 1), 0);
  11889. /* Try passing in bad values */
  11890. b.input = NULL;
  11891. b.inLen = 0;
  11892. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen), 0);
  11893. c.input = NULL;
  11894. c.inLen = WC_SHA256_DIGEST_SIZE;
  11895. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen),
  11896. BAD_FUNC_ARG);
  11897. ExpectIntEQ(wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen),
  11898. BAD_FUNC_ARG);
  11899. wc_Sha256Free(&sha256);
  11900. #endif
  11901. return EXPECT_RESULT();
  11902. } /* END test_wc_Sha256Update */
  11903. /*
  11904. * Unit test function for wc_Sha256Final()
  11905. */
  11906. static int test_wc_Sha256Final(void)
  11907. {
  11908. EXPECT_DECLS;
  11909. #ifndef NO_SHA256
  11910. wc_Sha256 sha256;
  11911. byte* hash_test[3];
  11912. byte hash1[WC_SHA256_DIGEST_SIZE];
  11913. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  11914. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  11915. int times, i;
  11916. /* Initialize */
  11917. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11918. hash_test[0] = hash1;
  11919. hash_test[1] = hash2;
  11920. hash_test[2] = hash3;
  11921. times = sizeof(hash_test) / sizeof(byte*);
  11922. for (i = 0; i < times; i++) {
  11923. ExpectIntEQ(wc_Sha256Final(&sha256, hash_test[i]), 0);
  11924. }
  11925. /* Test bad args. */
  11926. ExpectIntEQ(wc_Sha256Final(NULL, NULL), BAD_FUNC_ARG);
  11927. ExpectIntEQ(wc_Sha256Final(NULL, hash1), BAD_FUNC_ARG);
  11928. ExpectIntEQ(wc_Sha256Final(&sha256, NULL), BAD_FUNC_ARG);
  11929. wc_Sha256Free(&sha256);
  11930. #endif
  11931. return EXPECT_RESULT();
  11932. } /* END test_wc_Sha256Final */
  11933. /*
  11934. * Unit test function for wc_Sha256FinalRaw()
  11935. */
  11936. static int test_wc_Sha256FinalRaw(void)
  11937. {
  11938. EXPECT_DECLS;
  11939. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  11940. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  11941. !defined(WOLFSSL_NO_HASH_RAW)
  11942. wc_Sha256 sha256;
  11943. byte* hash_test[3];
  11944. byte hash1[WC_SHA256_DIGEST_SIZE];
  11945. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  11946. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  11947. int times, i;
  11948. /* Initialize */
  11949. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11950. hash_test[0] = hash1;
  11951. hash_test[1] = hash2;
  11952. hash_test[2] = hash3;
  11953. times = sizeof(hash_test) / sizeof(byte*);
  11954. for (i = 0; i < times; i++) {
  11955. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, hash_test[i]), 0);
  11956. }
  11957. /* Test bad args. */
  11958. ExpectIntEQ(wc_Sha256FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  11959. ExpectIntEQ(wc_Sha256FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  11960. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, NULL), BAD_FUNC_ARG);
  11961. wc_Sha256Free(&sha256);
  11962. #endif
  11963. return EXPECT_RESULT();
  11964. } /* END test_wc_Sha256FinalRaw */
  11965. /*
  11966. * Unit test function for wc_Sha256GetFlags()
  11967. */
  11968. static int test_wc_Sha256GetFlags(void)
  11969. {
  11970. EXPECT_DECLS;
  11971. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  11972. wc_Sha256 sha256;
  11973. word32 flags = 0;
  11974. /* Initialize */
  11975. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11976. ExpectIntEQ(wc_Sha256GetFlags(&sha256, &flags), 0);
  11977. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11978. wc_Sha256Free(&sha256);
  11979. #endif
  11980. return EXPECT_RESULT();
  11981. } /* END test_wc_Sha256GetFlags */
  11982. /*
  11983. * Unit test function for wc_Sha256Free()
  11984. */
  11985. static int test_wc_Sha256Free(void)
  11986. {
  11987. EXPECT_DECLS;
  11988. #ifndef NO_SHA256
  11989. wc_Sha256Free(NULL);
  11990. /* Set result to SUCCESS. */
  11991. ExpectTrue(1);
  11992. #endif
  11993. return EXPECT_RESULT();
  11994. } /* END test_wc_Sha256Free */
  11995. /*
  11996. * Unit test function for wc_Sha256GetHash()
  11997. */
  11998. static int test_wc_Sha256GetHash(void)
  11999. {
  12000. EXPECT_DECLS;
  12001. #ifndef NO_SHA256
  12002. wc_Sha256 sha256;
  12003. byte hash1[WC_SHA256_DIGEST_SIZE];
  12004. /* Initialize */
  12005. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  12006. ExpectIntEQ(wc_Sha256GetHash(&sha256, hash1), 0);
  12007. /* test bad arguments*/
  12008. ExpectIntEQ(wc_Sha256GetHash(NULL, NULL), BAD_FUNC_ARG);
  12009. ExpectIntEQ(wc_Sha256GetHash(NULL, hash1), BAD_FUNC_ARG);
  12010. ExpectIntEQ(wc_Sha256GetHash(&sha256, NULL), BAD_FUNC_ARG);
  12011. wc_Sha256Free(&sha256);
  12012. #endif
  12013. return EXPECT_RESULT();
  12014. } /* END test_wc_Sha256GetHash */
  12015. /*
  12016. * Unit test function for wc_Sha256Copy()
  12017. */
  12018. static int test_wc_Sha256Copy(void)
  12019. {
  12020. EXPECT_DECLS;
  12021. #ifndef NO_SHA256
  12022. wc_Sha256 sha256;
  12023. wc_Sha256 temp;
  12024. XMEMSET(&sha256, 0, sizeof(sha256));
  12025. XMEMSET(&temp, 0, sizeof(temp));
  12026. /* Initialize */
  12027. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  12028. ExpectIntEQ(wc_InitSha256(&temp), 0);
  12029. ExpectIntEQ(wc_Sha256Copy(&sha256, &temp), 0);
  12030. /* test bad arguments*/
  12031. ExpectIntEQ(wc_Sha256Copy(NULL, NULL), BAD_FUNC_ARG);
  12032. ExpectIntEQ(wc_Sha256Copy(NULL, &temp), BAD_FUNC_ARG);
  12033. ExpectIntEQ(wc_Sha256Copy(&sha256, NULL), BAD_FUNC_ARG);
  12034. wc_Sha256Free(&sha256);
  12035. wc_Sha256Free(&temp);
  12036. #endif
  12037. return EXPECT_RESULT();
  12038. } /* END test_wc_Sha256Copy */
  12039. /*
  12040. * Testing wc_InitSha512()
  12041. */
  12042. static int test_wc_InitSha512(void)
  12043. {
  12044. EXPECT_DECLS;
  12045. #ifdef WOLFSSL_SHA512
  12046. wc_Sha512 sha512;
  12047. /* Test good arg. */
  12048. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12049. /* Test bad arg. */
  12050. ExpectIntEQ(wc_InitSha512(NULL), BAD_FUNC_ARG);
  12051. wc_Sha512Free(&sha512);
  12052. #endif
  12053. return EXPECT_RESULT();
  12054. } /* END test_wc_InitSha512 */
  12055. /*
  12056. * wc_Sha512Update() test.
  12057. */
  12058. static int test_wc_Sha512Update(void)
  12059. {
  12060. EXPECT_DECLS;
  12061. #ifdef WOLFSSL_SHA512
  12062. wc_Sha512 sha512;
  12063. byte hash[WC_SHA512_DIGEST_SIZE];
  12064. byte hash_unaligned[WC_SHA512_DIGEST_SIZE + 1];
  12065. testVector a, b, c;
  12066. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12067. /* Input. */
  12068. a.input = "a";
  12069. a.inLen = XSTRLEN(a.input);
  12070. ExpectIntEQ(wc_Sha512Update(&sha512, NULL, 0), 0);
  12071. ExpectIntEQ(wc_Sha512Update(&sha512,(byte*)a.input, 0), 0);
  12072. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen), 0);
  12073. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  12074. /* Update input. */
  12075. a.input = "abc";
  12076. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  12077. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  12078. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  12079. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  12080. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  12081. a.inLen = XSTRLEN(a.input);
  12082. a.outLen = XSTRLEN(a.output);
  12083. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen), 0);
  12084. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  12085. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE), 0);
  12086. /* Unaligned check. */
  12087. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input+1, (word32)a.inLen-1),
  12088. 0);
  12089. ExpectIntEQ(wc_Sha512Final(&sha512, hash_unaligned+1), 0);
  12090. /* Try passing in bad values */
  12091. b.input = NULL;
  12092. b.inLen = 0;
  12093. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen), 0);
  12094. c.input = NULL;
  12095. c.inLen = WC_SHA512_DIGEST_SIZE;
  12096. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12097. BAD_FUNC_ARG);
  12098. ExpectIntEQ(wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen),
  12099. BAD_FUNC_ARG);
  12100. wc_Sha512Free(&sha512);
  12101. #endif
  12102. return EXPECT_RESULT();
  12103. } /* END test_wc_Sha512Update */
  12104. #ifdef WOLFSSL_SHA512
  12105. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12106. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  12107. /* Performs test for
  12108. * - wc_Sha512Final/wc_Sha512FinalRaw
  12109. * - wc_Sha512_224Final/wc_Sha512_224Final
  12110. * - wc_Sha512_256Final/wc_Sha512_256Final
  12111. * parameter:
  12112. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  12113. * WC_HASH_TYPE_SHA512_256
  12114. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  12115. *return 0 on success
  12116. */
  12117. static int test_Sha512_Family_Final(int type, int isRaw)
  12118. {
  12119. EXPECT_DECLS;
  12120. wc_Sha512 sha512;
  12121. byte* hash_test[3];
  12122. byte hash1[WC_SHA512_DIGEST_SIZE];
  12123. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12124. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12125. int times, i;
  12126. int(*initFp)(wc_Sha512*);
  12127. int(*finalFp)(wc_Sha512*, byte*);
  12128. void(*freeFp)(wc_Sha512*);
  12129. if (type == WC_HASH_TYPE_SHA512) {
  12130. initFp = wc_InitSha512;
  12131. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12132. !defined(WOLFSSL_NO_HASH_RAW)
  12133. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  12134. #else
  12135. finalFp = (isRaw)? NULL : wc_Sha512Final;
  12136. #endif
  12137. freeFp = wc_Sha512Free;
  12138. }
  12139. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12140. #if !defined(WOLFSSL_NOSHA512_224)
  12141. else if (type == WC_HASH_TYPE_SHA512_224) {
  12142. initFp = wc_InitSha512_224;
  12143. #if !defined(WOLFSSL_NO_HASH_RAW)
  12144. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  12145. #else
  12146. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  12147. #endif
  12148. freeFp = wc_Sha512_224Free;
  12149. }
  12150. #endif
  12151. #if !defined(WOLFSSL_NOSHA512_256)
  12152. else if (type == WC_HASH_TYPE_SHA512_256) {
  12153. initFp = wc_InitSha512_256;
  12154. #if !defined(WOLFSSL_NO_HASH_RAW)
  12155. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  12156. #else
  12157. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  12158. #endif
  12159. freeFp = wc_Sha512_256Free;
  12160. }
  12161. #endif
  12162. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12163. else
  12164. return TEST_FAIL;
  12165. /* Initialize */
  12166. ExpectIntEQ(initFp(&sha512), 0);
  12167. hash_test[0] = hash1;
  12168. hash_test[1] = hash2;
  12169. hash_test[2] = hash3;
  12170. times = sizeof(hash_test) / sizeof(byte *);
  12171. /* Good test args. */
  12172. for (i = 0; i < times; i++) {
  12173. ExpectIntEQ(finalFp(&sha512, hash_test[i]), 0);
  12174. }
  12175. /* Test bad args. */
  12176. ExpectIntEQ(finalFp(NULL, NULL), BAD_FUNC_ARG);
  12177. ExpectIntEQ(finalFp(NULL, hash1), BAD_FUNC_ARG);
  12178. ExpectIntEQ(finalFp(&sha512, NULL), BAD_FUNC_ARG);
  12179. freeFp(&sha512);
  12180. return EXPECT_RESULT();
  12181. }
  12182. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  12183. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  12184. #endif /* WOLFSSL_SHA512 */
  12185. /*
  12186. * Unit test function for wc_Sha512Final()
  12187. */
  12188. static int test_wc_Sha512Final(void)
  12189. {
  12190. EXPECT_DECLS;
  12191. #ifdef WOLFSSL_SHA512
  12192. wc_Sha512 sha512;
  12193. byte* hash_test[3];
  12194. byte hash1[WC_SHA512_DIGEST_SIZE];
  12195. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12196. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12197. int times, i;
  12198. /* Initialize */
  12199. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12200. hash_test[0] = hash1;
  12201. hash_test[1] = hash2;
  12202. hash_test[2] = hash3;
  12203. times = sizeof(hash_test) / sizeof(byte *);
  12204. for (i = 0; i < times; i++) {
  12205. ExpectIntEQ(wc_Sha512Final(&sha512, hash_test[i]), 0);
  12206. }
  12207. /* Test bad args. */
  12208. ExpectIntEQ(wc_Sha512Final(NULL, NULL), BAD_FUNC_ARG);
  12209. ExpectIntEQ(wc_Sha512Final(NULL, hash1), BAD_FUNC_ARG);
  12210. ExpectIntEQ(wc_Sha512Final(&sha512, NULL), BAD_FUNC_ARG);
  12211. wc_Sha512Free(&sha512);
  12212. #endif
  12213. return EXPECT_RESULT();
  12214. } /* END test_wc_Sha512Final */
  12215. /*
  12216. * Unit test function for wc_Sha512GetFlags()
  12217. */
  12218. static int test_wc_Sha512GetFlags(void)
  12219. {
  12220. EXPECT_DECLS;
  12221. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  12222. wc_Sha512 sha512;
  12223. word32 flags = 0;
  12224. /* Initialize */
  12225. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12226. ExpectIntEQ(wc_Sha512GetFlags(&sha512, &flags), 0);
  12227. ExpectIntEQ((flags & WC_HASH_FLAG_ISCOPY), 0);
  12228. wc_Sha512Free(&sha512);
  12229. #endif
  12230. return EXPECT_RESULT();
  12231. } /* END test_wc_Sha512GetFlags */
  12232. /*
  12233. * Unit test function for wc_Sha512FinalRaw()
  12234. */
  12235. static int test_wc_Sha512FinalRaw(void)
  12236. {
  12237. EXPECT_DECLS;
  12238. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  12239. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  12240. !defined(WOLFSSL_NO_HASH_RAW)
  12241. wc_Sha512 sha512;
  12242. byte* hash_test[3];
  12243. byte hash1[WC_SHA512_DIGEST_SIZE];
  12244. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12245. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12246. int times, i;
  12247. /* Initialize */
  12248. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12249. hash_test[0] = hash1;
  12250. hash_test[1] = hash2;
  12251. hash_test[2] = hash3;
  12252. times = sizeof(hash_test) / sizeof(byte*);
  12253. /* Good test args. */
  12254. for (i = 0; i < times; i++) {
  12255. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, hash_test[i]), 0);
  12256. }
  12257. /* Test bad args. */
  12258. ExpectIntEQ(wc_Sha512FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12259. ExpectIntEQ(wc_Sha512FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12260. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, NULL), BAD_FUNC_ARG);
  12261. wc_Sha512Free(&sha512);
  12262. #endif
  12263. return EXPECT_RESULT();
  12264. } /* END test_wc_Sha512FinalRaw */
  12265. /*
  12266. * Unit test function for wc_Sha512Free()
  12267. */
  12268. static int test_wc_Sha512Free(void)
  12269. {
  12270. EXPECT_DECLS;
  12271. #ifdef WOLFSSL_SHA512
  12272. wc_Sha512Free(NULL);
  12273. /* Set result to SUCCESS. */
  12274. ExpectTrue(1);
  12275. #endif
  12276. return EXPECT_RESULT();
  12277. } /* END test_wc_Sha512Free */
  12278. #ifdef WOLFSSL_SHA512
  12279. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12280. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  12281. static int test_Sha512_Family_GetHash(int type )
  12282. {
  12283. EXPECT_DECLS;
  12284. int(*initFp)(wc_Sha512*);
  12285. int(*ghashFp)(wc_Sha512*, byte*);
  12286. wc_Sha512 sha512;
  12287. byte hash1[WC_SHA512_DIGEST_SIZE];
  12288. if (type == WC_HASH_TYPE_SHA512) {
  12289. initFp = wc_InitSha512;
  12290. ghashFp = wc_Sha512GetHash;
  12291. }
  12292. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12293. #if !defined(WOLFSSL_NOSHA512_224)
  12294. else if (type == WC_HASH_TYPE_SHA512_224) {
  12295. initFp = wc_InitSha512_224;
  12296. ghashFp = wc_Sha512_224GetHash;
  12297. }
  12298. #endif
  12299. #if !defined(WOLFSSL_NOSHA512_256)
  12300. else if (type == WC_HASH_TYPE_SHA512_256) {
  12301. initFp = wc_InitSha512_256;
  12302. ghashFp = wc_Sha512_256GetHash;
  12303. }
  12304. #endif
  12305. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12306. else {
  12307. initFp = NULL;
  12308. ghashFp = NULL;
  12309. }
  12310. if (initFp == NULL || ghashFp == NULL)
  12311. return TEST_FAIL;
  12312. ExpectIntEQ(initFp(&sha512), 0);
  12313. ExpectIntEQ(ghashFp(&sha512, hash1), 0);
  12314. /* test bad arguments*/
  12315. ExpectIntEQ(ghashFp(NULL, NULL), BAD_FUNC_ARG);
  12316. ExpectIntEQ(ghashFp(NULL, hash1), BAD_FUNC_ARG);
  12317. ExpectIntEQ(ghashFp(&sha512, NULL), BAD_FUNC_ARG);
  12318. wc_Sha512Free(&sha512);
  12319. return EXPECT_RESULT();
  12320. }
  12321. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  12322. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  12323. #endif /* WOLFSSL_SHA512 */
  12324. /*
  12325. * Unit test function for wc_Sha512GetHash()
  12326. */
  12327. static int test_wc_Sha512GetHash(void)
  12328. {
  12329. EXPECT_DECLS;
  12330. #ifdef WOLFSSL_SHA512
  12331. wc_Sha512 sha512;
  12332. byte hash1[WC_SHA512_DIGEST_SIZE];
  12333. /* Initialize */
  12334. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12335. ExpectIntEQ(wc_Sha512GetHash(&sha512, hash1), 0);
  12336. /* test bad arguments*/
  12337. ExpectIntEQ(wc_Sha512GetHash(NULL, NULL), BAD_FUNC_ARG);
  12338. ExpectIntEQ(wc_Sha512GetHash(NULL, hash1), BAD_FUNC_ARG);
  12339. ExpectIntEQ(wc_Sha512GetHash(&sha512, NULL), BAD_FUNC_ARG);
  12340. wc_Sha512Free(&sha512);
  12341. #endif
  12342. return EXPECT_RESULT();
  12343. } /* END test_wc_Sha512GetHash */
  12344. /*
  12345. * Unit test function for wc_Sha512Copy()
  12346. */
  12347. static int test_wc_Sha512Copy(void)
  12348. {
  12349. EXPECT_DECLS;
  12350. #ifdef WOLFSSL_SHA512
  12351. wc_Sha512 sha512;
  12352. wc_Sha512 temp;
  12353. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12354. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12355. /* Initialize */
  12356. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12357. ExpectIntEQ(wc_InitSha512(&temp), 0);
  12358. ExpectIntEQ(wc_Sha512Copy(&sha512, &temp), 0);
  12359. /* test bad arguments*/
  12360. ExpectIntEQ(wc_Sha512Copy(NULL, NULL), BAD_FUNC_ARG);
  12361. ExpectIntEQ(wc_Sha512Copy(NULL, &temp), BAD_FUNC_ARG);
  12362. ExpectIntEQ(wc_Sha512Copy(&sha512, NULL), BAD_FUNC_ARG);
  12363. wc_Sha512Free(&sha512);
  12364. wc_Sha512Free(&temp);
  12365. #endif
  12366. return EXPECT_RESULT();
  12367. } /* END test_wc_Sha512Copy */
  12368. static int test_wc_InitSha512_224(void)
  12369. {
  12370. EXPECT_DECLS;
  12371. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12372. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12373. wc_Sha512 sha512;
  12374. /* Test good arg. */
  12375. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12376. /* Test bad arg. */
  12377. ExpectIntEQ(wc_InitSha512_224(NULL), BAD_FUNC_ARG);
  12378. wc_Sha512_224Free(&sha512);
  12379. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12380. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12381. return EXPECT_RESULT();
  12382. }
  12383. static int test_wc_Sha512_224Update(void)
  12384. {
  12385. EXPECT_DECLS;
  12386. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12387. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12388. wc_Sha512 sha512;
  12389. byte hash[WC_SHA512_DIGEST_SIZE];
  12390. testVector a, c;
  12391. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12392. /* Input. */
  12393. a.input = "a";
  12394. a.inLen = XSTRLEN(a.input);
  12395. ExpectIntEQ(wc_Sha512_224Update(&sha512, NULL, 0), 0);
  12396. ExpectIntEQ(wc_Sha512_224Update(&sha512,(byte*)a.input, 0), 0);
  12397. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen),
  12398. 0);
  12399. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  12400. /* Update input. */
  12401. a.input = "abc";
  12402. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  12403. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  12404. a.inLen = XSTRLEN(a.input);
  12405. a.outLen = XSTRLEN(a.output);
  12406. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen),
  12407. 0);
  12408. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  12409. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE), 0);
  12410. c.input = NULL;
  12411. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  12412. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12413. BAD_FUNC_ARG);
  12414. ExpectIntEQ(wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen),
  12415. BAD_FUNC_ARG);
  12416. wc_Sha512_224Free(&sha512);
  12417. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12418. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12419. return EXPECT_RESULT();
  12420. }
  12421. static int test_wc_Sha512_224Final(void)
  12422. {
  12423. EXPECT_DECLS;
  12424. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12425. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12426. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0),
  12427. TEST_SUCCESS);
  12428. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12429. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12430. return EXPECT_RESULT();
  12431. }
  12432. static int test_wc_Sha512_224GetFlags(void)
  12433. {
  12434. EXPECT_DECLS;
  12435. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12436. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  12437. wc_Sha512 sha512;
  12438. wc_Sha512 copy;
  12439. word32 flags = 0;
  12440. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12441. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  12442. /* Initialize */
  12443. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12444. ExpectIntEQ(wc_InitSha512_224(&copy), 0);
  12445. ExpectIntEQ(wc_Sha512_224GetFlags(&sha512, &flags), 0);
  12446. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12447. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &copy), 0);
  12448. ExpectIntEQ(wc_Sha512_224GetFlags(&copy, &flags), 0);
  12449. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  12450. wc_Sha512_224Free(&copy);
  12451. wc_Sha512_224Free(&sha512);
  12452. #endif
  12453. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12454. return EXPECT_RESULT();
  12455. }
  12456. static int test_wc_Sha512_224FinalRaw(void)
  12457. {
  12458. EXPECT_DECLS;
  12459. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12460. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  12461. !defined(WOLFSSL_NO_HASH_RAW)
  12462. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1),
  12463. TEST_SUCCESS);
  12464. #endif
  12465. return EXPECT_RESULT();
  12466. }
  12467. static int test_wc_Sha512_224Free(void)
  12468. {
  12469. EXPECT_DECLS;
  12470. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12471. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12472. wc_Sha512_224Free(NULL);
  12473. /* Set result to SUCCESS. */
  12474. ExpectTrue(1);
  12475. #endif
  12476. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12477. return EXPECT_RESULT();
  12478. }
  12479. static int test_wc_Sha512_224GetHash(void)
  12480. {
  12481. EXPECT_DECLS;
  12482. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12483. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12484. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224),
  12485. TEST_SUCCESS);
  12486. #endif
  12487. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12488. return EXPECT_RESULT();
  12489. }
  12490. static int test_wc_Sha512_224Copy(void)
  12491. {
  12492. EXPECT_DECLS;
  12493. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12494. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12495. wc_Sha512 sha512;
  12496. wc_Sha512 temp;
  12497. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12498. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12499. /* Initialize */
  12500. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12501. ExpectIntEQ(wc_InitSha512_224(&temp), 0);
  12502. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &temp), 0);
  12503. /* test bad arguments*/
  12504. ExpectIntEQ(wc_Sha512_224Copy(NULL, NULL), BAD_FUNC_ARG);
  12505. ExpectIntEQ(wc_Sha512_224Copy(NULL, &temp), BAD_FUNC_ARG);
  12506. ExpectIntEQ(wc_Sha512_224Copy(&sha512, NULL), BAD_FUNC_ARG);
  12507. wc_Sha512_224Free(&sha512);
  12508. wc_Sha512_224Free(&temp);
  12509. #endif
  12510. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12511. return EXPECT_RESULT();
  12512. }
  12513. static int test_wc_InitSha512_256(void)
  12514. {
  12515. EXPECT_DECLS;
  12516. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12517. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12518. wc_Sha512 sha512;
  12519. /* Test good arg. */
  12520. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12521. /* Test bad arg. */
  12522. ExpectIntEQ(wc_InitSha512_256(NULL), BAD_FUNC_ARG);
  12523. wc_Sha512_256Free(&sha512);
  12524. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12525. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12526. return EXPECT_RESULT();
  12527. }
  12528. static int test_wc_Sha512_256Update(void)
  12529. {
  12530. EXPECT_DECLS;
  12531. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12532. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12533. wc_Sha512 sha512;
  12534. byte hash[WC_SHA512_DIGEST_SIZE];
  12535. testVector a, c;
  12536. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12537. /* Input. */
  12538. a.input = "a";
  12539. a.inLen = XSTRLEN(a.input);
  12540. ExpectIntEQ(wc_Sha512_256Update(&sha512, NULL, 0), 0);
  12541. ExpectIntEQ(wc_Sha512_256Update(&sha512,(byte*)a.input, 0), 0);
  12542. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen),
  12543. 0);
  12544. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  12545. /* Update input. */
  12546. a.input = "abc";
  12547. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  12548. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  12549. "\x07\xe7\xaf\x23";
  12550. a.inLen = XSTRLEN(a.input);
  12551. a.outLen = XSTRLEN(a.output);
  12552. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen),
  12553. 0);
  12554. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  12555. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE), 0);
  12556. c.input = NULL;
  12557. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  12558. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12559. BAD_FUNC_ARG);
  12560. ExpectIntEQ(wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen),
  12561. BAD_FUNC_ARG);
  12562. wc_Sha512_256Free(&sha512);
  12563. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12564. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12565. return EXPECT_RESULT();
  12566. }
  12567. static int test_wc_Sha512_256Final(void)
  12568. {
  12569. EXPECT_DECLS;
  12570. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12571. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12572. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0),
  12573. TEST_SUCCESS);
  12574. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12575. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12576. return EXPECT_RESULT();
  12577. }
  12578. static int test_wc_Sha512_256GetFlags(void)
  12579. {
  12580. EXPECT_DECLS;
  12581. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12582. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  12583. wc_Sha512 sha512, copy;
  12584. word32 flags = 0;
  12585. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12586. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  12587. /* Initialize */
  12588. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12589. ExpectIntEQ(wc_InitSha512_256(&copy), 0);
  12590. ExpectIntEQ(wc_Sha512_256GetFlags(&sha512, &flags), 0);
  12591. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12592. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &copy), 0);
  12593. ExpectIntEQ(wc_Sha512_256GetFlags(&copy, &flags), 0);
  12594. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  12595. wc_Sha512_256Free(&sha512);
  12596. #endif
  12597. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12598. return EXPECT_RESULT();
  12599. }
  12600. static int test_wc_Sha512_256FinalRaw(void)
  12601. {
  12602. EXPECT_DECLS;
  12603. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12604. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  12605. !defined(WOLFSSL_NO_HASH_RAW)
  12606. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1),
  12607. TEST_SUCCESS);
  12608. #endif
  12609. return EXPECT_RESULT();
  12610. }
  12611. static int test_wc_Sha512_256Free(void)
  12612. {
  12613. EXPECT_DECLS;
  12614. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12615. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12616. wc_Sha512_256Free(NULL);
  12617. /* Set result to SUCCESS. */
  12618. ExpectTrue(1);
  12619. #endif
  12620. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12621. return EXPECT_RESULT();
  12622. }
  12623. static int test_wc_Sha512_256GetHash(void)
  12624. {
  12625. EXPECT_DECLS;
  12626. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12627. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12628. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256),
  12629. TEST_SUCCESS);
  12630. #endif
  12631. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12632. return EXPECT_RESULT();
  12633. }
  12634. static int test_wc_Sha512_256Copy(void)
  12635. {
  12636. EXPECT_DECLS;
  12637. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12638. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12639. wc_Sha512 sha512;
  12640. wc_Sha512 temp;
  12641. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12642. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12643. /* Initialize */
  12644. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12645. ExpectIntEQ(wc_InitSha512_256(&temp), 0);
  12646. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &temp), 0);
  12647. /* test bad arguments*/
  12648. ExpectIntEQ(wc_Sha512_256Copy(NULL, NULL), BAD_FUNC_ARG);
  12649. ExpectIntEQ(wc_Sha512_256Copy(NULL, &temp), BAD_FUNC_ARG);
  12650. ExpectIntEQ(wc_Sha512_256Copy(&sha512, NULL), BAD_FUNC_ARG);
  12651. wc_Sha512_256Free(&sha512);
  12652. wc_Sha512_256Free(&temp);
  12653. #endif
  12654. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12655. return EXPECT_RESULT();
  12656. }
  12657. /*
  12658. * Testing wc_InitSha384()
  12659. */
  12660. static int test_wc_InitSha384(void)
  12661. {
  12662. EXPECT_DECLS;
  12663. #ifdef WOLFSSL_SHA384
  12664. wc_Sha384 sha384;
  12665. /* Test good arg. */
  12666. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12667. /* Test bad arg. */
  12668. ExpectIntEQ(wc_InitSha384(NULL), BAD_FUNC_ARG);
  12669. wc_Sha384Free(&sha384);
  12670. #endif
  12671. return EXPECT_RESULT();
  12672. } /* END test_wc_InitSha384 */
  12673. /*
  12674. * test wc_Sha384Update()
  12675. */
  12676. static int test_wc_Sha384Update(void)
  12677. {
  12678. EXPECT_DECLS;
  12679. #ifdef WOLFSSL_SHA384
  12680. wc_Sha384 sha384;
  12681. byte hash[WC_SHA384_DIGEST_SIZE];
  12682. testVector a, b, c;
  12683. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12684. /* Input */
  12685. a.input = "a";
  12686. a.inLen = XSTRLEN(a.input);
  12687. ExpectIntEQ(wc_Sha384Update(&sha384, NULL, 0), 0);
  12688. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, 0), 0);
  12689. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  12690. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  12691. /* Update input. */
  12692. a.input = "abc";
  12693. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  12694. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  12695. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  12696. "\xc8\x25\xa7";
  12697. a.inLen = XSTRLEN(a.input);
  12698. a.outLen = XSTRLEN(a.output);
  12699. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  12700. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  12701. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  12702. /* Pass in bad values. */
  12703. b.input = NULL;
  12704. b.inLen = 0;
  12705. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen), 0);
  12706. c.input = NULL;
  12707. c.inLen = WC_SHA384_DIGEST_SIZE;
  12708. ExpectIntEQ( wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen),
  12709. BAD_FUNC_ARG);
  12710. ExpectIntEQ(wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen),
  12711. BAD_FUNC_ARG);
  12712. wc_Sha384Free(&sha384);
  12713. #endif
  12714. return EXPECT_RESULT();
  12715. } /* END test_wc_Sha384Update */
  12716. /*
  12717. * Unit test function for wc_Sha384Final();
  12718. */
  12719. static int test_wc_Sha384Final(void)
  12720. {
  12721. EXPECT_DECLS;
  12722. #ifdef WOLFSSL_SHA384
  12723. wc_Sha384 sha384;
  12724. byte* hash_test[3];
  12725. byte hash1[WC_SHA384_DIGEST_SIZE];
  12726. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  12727. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  12728. int times, i;
  12729. /* Initialize */
  12730. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12731. hash_test[0] = hash1;
  12732. hash_test[1] = hash2;
  12733. hash_test[2] = hash3;
  12734. times = sizeof(hash_test) / sizeof(byte*);
  12735. /* Good test args. */
  12736. for (i = 0; i < times; i++) {
  12737. ExpectIntEQ(wc_Sha384Final(&sha384, hash_test[i]), 0);
  12738. }
  12739. /* Test bad args. */
  12740. ExpectIntEQ(wc_Sha384Final(NULL, NULL), BAD_FUNC_ARG);
  12741. ExpectIntEQ(wc_Sha384Final(NULL, hash1), BAD_FUNC_ARG);
  12742. ExpectIntEQ(wc_Sha384Final(&sha384, NULL), BAD_FUNC_ARG);
  12743. wc_Sha384Free(&sha384);
  12744. #endif
  12745. return EXPECT_RESULT();
  12746. } /* END test_wc_Sha384Final */
  12747. /*
  12748. * Unit test function for wc_Sha384GetFlags()
  12749. */
  12750. static int test_wc_Sha384GetFlags(void)
  12751. {
  12752. EXPECT_DECLS;
  12753. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  12754. wc_Sha384 sha384;
  12755. word32 flags = 0;
  12756. /* Initialize */
  12757. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12758. ExpectIntEQ(wc_Sha384GetFlags(&sha384, &flags), 0);
  12759. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12760. wc_Sha384Free(&sha384);
  12761. #endif
  12762. return EXPECT_RESULT();
  12763. } /* END test_wc_Sha384GetFlags */
  12764. /*
  12765. * Unit test function for wc_Sha384FinalRaw()
  12766. */
  12767. static int test_wc_Sha384FinalRaw(void)
  12768. {
  12769. EXPECT_DECLS;
  12770. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  12771. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  12772. !defined(WOLFSSL_NO_HASH_RAW)
  12773. wc_Sha384 sha384;
  12774. byte* hash_test[3];
  12775. byte hash1[WC_SHA384_DIGEST_SIZE];
  12776. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  12777. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  12778. int times, i;
  12779. /* Initialize */
  12780. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12781. hash_test[0] = hash1;
  12782. hash_test[1] = hash2;
  12783. hash_test[2] = hash3;
  12784. times = sizeof(hash_test) / sizeof(byte*);
  12785. /* Good test args. */
  12786. for (i = 0; i < times; i++) {
  12787. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, hash_test[i]), 0);
  12788. }
  12789. /* Test bad args. */
  12790. ExpectIntEQ(wc_Sha384FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12791. ExpectIntEQ(wc_Sha384FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12792. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, NULL), BAD_FUNC_ARG);
  12793. wc_Sha384Free(&sha384);
  12794. #endif
  12795. return EXPECT_RESULT();
  12796. } /* END test_wc_Sha384FinalRaw */
  12797. /*
  12798. * Unit test function for wc_Sha384Free()
  12799. */
  12800. static int test_wc_Sha384Free(void)
  12801. {
  12802. EXPECT_DECLS;
  12803. #ifdef WOLFSSL_SHA384
  12804. wc_Sha384Free(NULL);
  12805. /* Set result to SUCCESS. */
  12806. ExpectTrue(1);
  12807. #endif
  12808. return EXPECT_RESULT();
  12809. } /* END test_wc_Sha384Free */
  12810. /*
  12811. * Unit test function for wc_Sha384GetHash()
  12812. */
  12813. static int test_wc_Sha384GetHash(void)
  12814. {
  12815. EXPECT_DECLS;
  12816. #ifdef WOLFSSL_SHA384
  12817. wc_Sha384 sha384;
  12818. byte hash1[WC_SHA384_DIGEST_SIZE];
  12819. /* Initialize */
  12820. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12821. ExpectIntEQ(wc_Sha384GetHash(&sha384, hash1), 0);
  12822. /* test bad arguments*/
  12823. ExpectIntEQ(wc_Sha384GetHash(NULL, NULL), BAD_FUNC_ARG);
  12824. ExpectIntEQ(wc_Sha384GetHash(NULL, hash1), BAD_FUNC_ARG);
  12825. ExpectIntEQ(wc_Sha384GetHash(&sha384, NULL), BAD_FUNC_ARG);
  12826. wc_Sha384Free(&sha384);
  12827. #endif
  12828. return EXPECT_RESULT();
  12829. } /* END test_wc_Sha384GetHash */
  12830. /*
  12831. * Unit test function for wc_Sha384Copy()
  12832. */
  12833. static int test_wc_Sha384Copy(void)
  12834. {
  12835. EXPECT_DECLS;
  12836. #ifdef WOLFSSL_SHA384
  12837. wc_Sha384 sha384;
  12838. wc_Sha384 temp;
  12839. XMEMSET(&sha384, 0, sizeof(wc_Sha384));
  12840. XMEMSET(&temp, 0, sizeof(wc_Sha384));
  12841. /* Initialize */
  12842. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12843. ExpectIntEQ(wc_InitSha384(&temp), 0);
  12844. ExpectIntEQ(wc_Sha384Copy(&sha384, &temp), 0);
  12845. /* test bad arguments*/
  12846. ExpectIntEQ(wc_Sha384Copy(NULL, NULL), BAD_FUNC_ARG);
  12847. ExpectIntEQ(wc_Sha384Copy(NULL, &temp), BAD_FUNC_ARG);
  12848. ExpectIntEQ(wc_Sha384Copy(&sha384, NULL), BAD_FUNC_ARG);
  12849. wc_Sha384Free(&sha384);
  12850. wc_Sha384Free(&temp);
  12851. #endif
  12852. return EXPECT_RESULT();
  12853. } /* END test_wc_Sha384Copy */
  12854. /*
  12855. * Testing wc_InitSha224();
  12856. */
  12857. static int test_wc_InitSha224(void)
  12858. {
  12859. EXPECT_DECLS;
  12860. #ifdef WOLFSSL_SHA224
  12861. wc_Sha224 sha224;
  12862. /* Test good arg. */
  12863. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12864. /* Test bad arg. */
  12865. ExpectIntEQ(wc_InitSha224(NULL), BAD_FUNC_ARG);
  12866. wc_Sha224Free(&sha224);
  12867. #endif
  12868. return EXPECT_RESULT();
  12869. } /* END test_wc_InitSha224 */
  12870. /*
  12871. * Unit test on wc_Sha224Update
  12872. */
  12873. static int test_wc_Sha224Update(void)
  12874. {
  12875. EXPECT_DECLS;
  12876. #ifdef WOLFSSL_SHA224
  12877. wc_Sha224 sha224;
  12878. byte hash[WC_SHA224_DIGEST_SIZE];
  12879. testVector a, b, c;
  12880. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12881. /* Input. */
  12882. a.input = "a";
  12883. a.inLen = XSTRLEN(a.input);
  12884. ExpectIntEQ(wc_Sha224Update(&sha224, NULL, 0), 0);
  12885. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, 0), 0);
  12886. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  12887. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  12888. /* Update input. */
  12889. a.input = "abc";
  12890. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  12891. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  12892. a.inLen = XSTRLEN(a.input);
  12893. a.outLen = XSTRLEN(a.output);
  12894. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  12895. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  12896. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  12897. /* Pass in bad values. */
  12898. b.input = NULL;
  12899. b.inLen = 0;
  12900. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen), 0);
  12901. c.input = NULL;
  12902. c.inLen = WC_SHA224_DIGEST_SIZE;
  12903. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen),
  12904. BAD_FUNC_ARG);
  12905. ExpectIntEQ(wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen),
  12906. BAD_FUNC_ARG);
  12907. wc_Sha224Free(&sha224);
  12908. #endif
  12909. return EXPECT_RESULT();
  12910. } /* END test_wc_Sha224Update */
  12911. /*
  12912. * Unit test for wc_Sha224Final();
  12913. */
  12914. static int test_wc_Sha224Final(void)
  12915. {
  12916. EXPECT_DECLS;
  12917. #ifdef WOLFSSL_SHA224
  12918. wc_Sha224 sha224;
  12919. byte* hash_test[3];
  12920. byte hash1[WC_SHA224_DIGEST_SIZE];
  12921. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  12922. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  12923. int times, i;
  12924. /* Initialize */
  12925. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12926. hash_test[0] = hash1;
  12927. hash_test[1] = hash2;
  12928. hash_test[2] = hash3;
  12929. times = sizeof(hash_test) / sizeof(byte*);
  12930. /* Good test args. */
  12931. /* Testing oversized buffers. */
  12932. for (i = 0; i < times; i++) {
  12933. ExpectIntEQ(wc_Sha224Final(&sha224, hash_test[i]), 0);
  12934. }
  12935. /* Test bad args. */
  12936. ExpectIntEQ(wc_Sha224Final(NULL, NULL), BAD_FUNC_ARG);
  12937. ExpectIntEQ(wc_Sha224Final(NULL, hash1), BAD_FUNC_ARG);
  12938. ExpectIntEQ(wc_Sha224Final(&sha224, NULL), BAD_FUNC_ARG);
  12939. wc_Sha224Free(&sha224);
  12940. #endif
  12941. return EXPECT_RESULT();
  12942. } /* END test_wc_Sha224Final */
  12943. /*
  12944. * Unit test function for wc_Sha224SetFlags()
  12945. */
  12946. static int test_wc_Sha224SetFlags(void)
  12947. {
  12948. EXPECT_DECLS;
  12949. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  12950. wc_Sha224 sha224;
  12951. word32 flags = WC_HASH_FLAG_WILLCOPY;
  12952. /* Initialize */
  12953. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12954. ExpectIntEQ(wc_Sha224SetFlags(&sha224, flags), 0);
  12955. flags = 0;
  12956. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  12957. ExpectTrue(flags == WC_HASH_FLAG_WILLCOPY);
  12958. wc_Sha224Free(&sha224);
  12959. #endif
  12960. return EXPECT_RESULT();
  12961. } /* END test_wc_Sha224SetFlags */
  12962. /*
  12963. * Unit test function for wc_Sha224GetFlags()
  12964. */
  12965. static int test_wc_Sha224GetFlags(void)
  12966. {
  12967. EXPECT_DECLS;
  12968. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  12969. wc_Sha224 sha224;
  12970. word32 flags = 0;
  12971. /* Initialize */
  12972. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12973. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  12974. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12975. wc_Sha224Free(&sha224);
  12976. #endif
  12977. return EXPECT_RESULT();
  12978. } /* END test_wc_Sha224GetFlags */
  12979. /*
  12980. * Unit test function for wc_Sha224Free()
  12981. */
  12982. static int test_wc_Sha224Free(void)
  12983. {
  12984. EXPECT_DECLS;
  12985. #ifdef WOLFSSL_SHA224
  12986. wc_Sha224Free(NULL);
  12987. /* Set result to SUCCESS. */
  12988. ExpectTrue(1);
  12989. #endif
  12990. return EXPECT_RESULT();
  12991. } /* END test_wc_Sha224Free */
  12992. /*
  12993. * Unit test function for wc_Sha224GetHash()
  12994. */
  12995. static int test_wc_Sha224GetHash(void)
  12996. {
  12997. EXPECT_DECLS;
  12998. #ifdef WOLFSSL_SHA224
  12999. wc_Sha224 sha224;
  13000. byte hash1[WC_SHA224_DIGEST_SIZE];
  13001. /* Initialize */
  13002. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  13003. ExpectIntEQ(wc_Sha224GetHash(&sha224, hash1), 0);
  13004. /* test bad arguments*/
  13005. ExpectIntEQ(wc_Sha224GetHash(NULL, NULL), BAD_FUNC_ARG);
  13006. ExpectIntEQ(wc_Sha224GetHash(NULL, hash1), BAD_FUNC_ARG);
  13007. ExpectIntEQ(wc_Sha224GetHash(&sha224, NULL), BAD_FUNC_ARG);
  13008. wc_Sha224Free(&sha224);
  13009. #endif
  13010. return EXPECT_RESULT();
  13011. } /* END test_wc_Sha224GetHash */
  13012. /*
  13013. * Unit test function for wc_Sha224Copy()
  13014. */
  13015. static int test_wc_Sha224Copy(void)
  13016. {
  13017. EXPECT_DECLS;
  13018. #ifdef WOLFSSL_SHA224
  13019. wc_Sha224 sha224;
  13020. wc_Sha224 temp;
  13021. XMEMSET(&sha224, 0, sizeof(wc_Sha224));
  13022. XMEMSET(&temp, 0, sizeof(wc_Sha224));
  13023. /* Initialize */
  13024. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  13025. ExpectIntEQ(wc_InitSha224(&temp), 0);
  13026. ExpectIntEQ(wc_Sha224Copy(&sha224, &temp), 0);
  13027. /* test bad arguments*/
  13028. ExpectIntEQ(wc_Sha224Copy(NULL, NULL), BAD_FUNC_ARG);
  13029. ExpectIntEQ(wc_Sha224Copy(NULL, &temp), BAD_FUNC_ARG);
  13030. ExpectIntEQ(wc_Sha224Copy(&sha224, NULL), BAD_FUNC_ARG);
  13031. wc_Sha224Free(&sha224);
  13032. wc_Sha224Free(&temp);
  13033. #endif
  13034. return EXPECT_RESULT();
  13035. } /* END test_wc_Sha224Copy */
  13036. /*
  13037. * Testing wc_InitRipeMd()
  13038. */
  13039. static int test_wc_InitRipeMd(void)
  13040. {
  13041. EXPECT_DECLS;
  13042. #ifdef WOLFSSL_RIPEMD
  13043. RipeMd ripemd;
  13044. /* Test good arg. */
  13045. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  13046. /* Test bad arg. */
  13047. ExpectIntEQ(wc_InitRipeMd(NULL), BAD_FUNC_ARG);
  13048. #endif
  13049. return EXPECT_RESULT();
  13050. } /* END test_wc_InitRipeMd */
  13051. /*
  13052. * Testing wc_RipeMdUpdate()
  13053. */
  13054. static int test_wc_RipeMdUpdate(void)
  13055. {
  13056. EXPECT_DECLS;
  13057. #ifdef WOLFSSL_RIPEMD
  13058. RipeMd ripemd;
  13059. byte hash[RIPEMD_DIGEST_SIZE];
  13060. testVector a, b, c;
  13061. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  13062. /* Input */
  13063. a.input = "a";
  13064. a.inLen = XSTRLEN(a.input);
  13065. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  13066. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  13067. /* Update input. */
  13068. a.input = "abc";
  13069. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  13070. "\xb0\x87\xf1\x5a\x0b\xfc";
  13071. a.inLen = XSTRLEN(a.input);
  13072. a.outLen = XSTRLEN(a.output);
  13073. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  13074. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  13075. ExpectIntEQ(XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE), 0);
  13076. /* Pass in bad values. */
  13077. b.input = NULL;
  13078. b.inLen = 0;
  13079. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen), 0);
  13080. c.input = NULL;
  13081. c.inLen = RIPEMD_DIGEST_SIZE;
  13082. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen),
  13083. BAD_FUNC_ARG);
  13084. ExpectIntEQ(wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13085. BAD_FUNC_ARG);
  13086. #endif
  13087. return EXPECT_RESULT();
  13088. } /* END test_wc_RipeMdUdpate */
  13089. /*
  13090. * Unit test function for wc_RipeMdFinal()
  13091. */
  13092. static int test_wc_RipeMdFinal(void)
  13093. {
  13094. EXPECT_DECLS;
  13095. #ifdef WOLFSSL_RIPEMD
  13096. RipeMd ripemd;
  13097. byte* hash_test[3];
  13098. byte hash1[RIPEMD_DIGEST_SIZE];
  13099. byte hash2[2*RIPEMD_DIGEST_SIZE];
  13100. byte hash3[5*RIPEMD_DIGEST_SIZE];
  13101. int times, i;
  13102. /* Initialize */
  13103. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  13104. hash_test[0] = hash1;
  13105. hash_test[1] = hash2;
  13106. hash_test[2] = hash3;
  13107. times = sizeof(hash_test) / sizeof(byte*);
  13108. /* Testing oversized buffers. */
  13109. for (i = 0; i < times; i++) {
  13110. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash_test[i]), 0);
  13111. }
  13112. /* Test bad args. */
  13113. ExpectIntEQ(wc_RipeMdFinal(NULL, NULL), BAD_FUNC_ARG);
  13114. ExpectIntEQ(wc_RipeMdFinal(NULL, hash1), BAD_FUNC_ARG);
  13115. ExpectIntEQ(wc_RipeMdFinal(&ripemd, NULL), BAD_FUNC_ARG);
  13116. #endif
  13117. return EXPECT_RESULT();
  13118. } /* END test_wc_RipeMdFinal */
  13119. /*
  13120. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  13121. * wc_InitSha3_512
  13122. */
  13123. static int test_wc_InitSha3(void)
  13124. {
  13125. EXPECT_DECLS;
  13126. #if defined(WOLFSSL_SHA3)
  13127. wc_Sha3 sha3;
  13128. (void)sha3;
  13129. #if !defined(WOLFSSL_NOSHA3_224)
  13130. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13131. /* Test bad args. */
  13132. ExpectIntEQ(wc_InitSha3_224(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13133. wc_Sha3_224_Free(&sha3);
  13134. #endif /* NOSHA3_224 */
  13135. #if !defined(WOLFSSL_NOSHA3_256)
  13136. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13137. /* Test bad args. */
  13138. ExpectIntEQ(wc_InitSha3_256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13139. wc_Sha3_256_Free(&sha3);
  13140. #endif /* NOSHA3_256 */
  13141. #if !defined(WOLFSSL_NOSHA3_384)
  13142. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13143. /* Test bad args. */
  13144. ExpectIntEQ(wc_InitSha3_384(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13145. wc_Sha3_384_Free(&sha3);
  13146. #endif /* NOSHA3_384 */
  13147. #if !defined(WOLFSSL_NOSHA3_512)
  13148. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13149. /* Test bad args. */
  13150. ExpectIntEQ(wc_InitSha3_512(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13151. wc_Sha3_512_Free(&sha3);
  13152. #endif /* NOSHA3_512 */
  13153. #endif
  13154. return EXPECT_RESULT();
  13155. } /* END test_wc_InitSha3 */
  13156. /*
  13157. * Testing wc_Sha3_Update()
  13158. */
  13159. static int testing_wc_Sha3_Update(void)
  13160. {
  13161. EXPECT_DECLS;
  13162. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  13163. !defined(WOLFSSL_AFALG_XILINX)
  13164. wc_Sha3 sha3;
  13165. byte msg[] = "Everybody's working for the weekend.";
  13166. byte msg2[] = "Everybody gets Friday off.";
  13167. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  13168. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  13169. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  13170. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  13171. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  13172. word32 msglen = sizeof(msg) - 1;
  13173. word32 msg2len = sizeof(msg2);
  13174. word32 msgCmplen = sizeof(msgCmp);
  13175. #if !defined(WOLFSSL_NOSHA3_224)
  13176. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13177. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg, msglen), 0);
  13178. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13179. ExpectTrue(sha3.i == msglen);
  13180. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  13181. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13182. /* Pass bad args. */
  13183. ExpectIntEQ(wc_Sha3_224_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13184. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13185. wc_Sha3_224_Free(&sha3);
  13186. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13187. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 0), 0);
  13188. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  13189. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13190. wc_Sha3_224_Free(&sha3);
  13191. #endif /* SHA3_224 */
  13192. #if !defined(WOLFSSL_NOSHA3_256)
  13193. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13194. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg, msglen), 0);
  13195. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13196. ExpectTrue(sha3.i == msglen);
  13197. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  13198. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13199. /* Pass bad args. */
  13200. ExpectIntEQ(wc_Sha3_256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13201. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13202. wc_Sha3_256_Free(&sha3);
  13203. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13204. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 0), 0);
  13205. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  13206. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13207. wc_Sha3_256_Free(&sha3);
  13208. #endif /* SHA3_256 */
  13209. #if !defined(WOLFSSL_NOSHA3_384)
  13210. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13211. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg, msglen), 0);
  13212. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13213. ExpectTrue(sha3.i == msglen);
  13214. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  13215. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13216. /* Pass bad args. */
  13217. ExpectIntEQ(wc_Sha3_384_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13218. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13219. wc_Sha3_384_Free(&sha3);
  13220. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13221. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 0), 0);
  13222. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  13223. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13224. wc_Sha3_384_Free(&sha3);
  13225. #endif /* SHA3_384 */
  13226. #if !defined(WOLFSSL_NOSHA3_512)
  13227. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13228. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg, msglen), 0);
  13229. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13230. ExpectTrue(sha3.i == msglen);
  13231. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  13232. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13233. /* Pass bad args. */
  13234. ExpectIntEQ(wc_Sha3_512_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13235. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13236. wc_Sha3_512_Free(&sha3);
  13237. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13238. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 0), 0);
  13239. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  13240. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13241. wc_Sha3_512_Free(&sha3);
  13242. #endif /* SHA3_512 */
  13243. #endif /* WOLFSSL_SHA3 */
  13244. return EXPECT_RESULT();
  13245. } /* END testing_wc_Sha3_Update */
  13246. /*
  13247. * Testing wc_Sha3_224_Final()
  13248. */
  13249. static int test_wc_Sha3_224_Final(void)
  13250. {
  13251. EXPECT_DECLS;
  13252. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  13253. wc_Sha3 sha3;
  13254. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13255. "nopnopq";
  13256. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  13257. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  13258. "\x64\xea\xd0\xfc\xce\x33";
  13259. byte hash[WC_SHA3_224_DIGEST_SIZE];
  13260. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  13261. /* Init stack variables. */
  13262. XMEMSET(hash, 0, sizeof(hash));
  13263. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13264. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13265. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13266. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE), 0);
  13267. /* Test bad args. */
  13268. ExpectIntEQ(wc_Sha3_224_Final(NULL, hash), BAD_FUNC_ARG);
  13269. ExpectIntEQ(wc_Sha3_224_Final(&sha3, NULL), BAD_FUNC_ARG);
  13270. wc_Sha3_224_Free(&sha3);
  13271. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13272. /* Init stack variables. */
  13273. XMEMSET(hash, 0, sizeof(hash));
  13274. XMEMSET(hashRet, 0, sizeof(hashRet));
  13275. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13276. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, hashRet), 0);
  13277. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13278. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE), 0);
  13279. /* Test bad args. */
  13280. ExpectIntEQ(wc_Sha3_224_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13281. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13282. wc_Sha3_224_Free(&sha3);
  13283. #endif
  13284. return EXPECT_RESULT();
  13285. } /* END test_wc_Sha3_224_Final */
  13286. /*
  13287. * Testing wc_Sha3_256_Final()
  13288. */
  13289. static int test_wc_Sha3_256_Final(void)
  13290. {
  13291. EXPECT_DECLS;
  13292. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  13293. wc_Sha3 sha3;
  13294. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13295. "nopnopq";
  13296. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  13297. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  13298. "\xdd\x97\x49\x6d\x33\x76";
  13299. byte hash[WC_SHA3_256_DIGEST_SIZE];
  13300. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  13301. /* Init stack variables. */
  13302. XMEMSET(hash, 0, sizeof(hash));
  13303. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13304. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13305. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13306. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE), 0);
  13307. /* Test bad args. */
  13308. ExpectIntEQ(wc_Sha3_256_Final(NULL, hash), BAD_FUNC_ARG);
  13309. ExpectIntEQ(wc_Sha3_256_Final(&sha3, NULL), BAD_FUNC_ARG);
  13310. wc_Sha3_256_Free(&sha3);
  13311. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13312. /* Init stack variables. */
  13313. XMEMSET(hash, 0, sizeof(hash));
  13314. XMEMSET(hashRet, 0, sizeof(hashRet));
  13315. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13316. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, hashRet), 0);
  13317. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13318. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE), 0);
  13319. /* Test bad args. */
  13320. ExpectIntEQ(wc_Sha3_256_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13321. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13322. wc_Sha3_256_Free(&sha3);
  13323. #endif
  13324. return EXPECT_RESULT();
  13325. } /* END test_wc_Sha3_256_Final */
  13326. /*
  13327. * Testing wc_Sha3_384_Final()
  13328. */
  13329. static int test_wc_Sha3_384_Final(void)
  13330. {
  13331. EXPECT_DECLS;
  13332. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  13333. wc_Sha3 sha3;
  13334. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13335. "nopnopq";
  13336. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  13337. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  13338. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  13339. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  13340. byte hash[WC_SHA3_384_DIGEST_SIZE];
  13341. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  13342. /* Init stack variables. */
  13343. XMEMSET(hash, 0, sizeof(hash));
  13344. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13345. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13346. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13347. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE), 0);
  13348. /* Test bad args. */
  13349. ExpectIntEQ(wc_Sha3_384_Final(NULL, hash), BAD_FUNC_ARG);
  13350. ExpectIntEQ(wc_Sha3_384_Final(&sha3, NULL), BAD_FUNC_ARG);
  13351. wc_Sha3_384_Free(&sha3);
  13352. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13353. /* Init stack variables. */
  13354. XMEMSET(hash, 0, sizeof(hash));
  13355. XMEMSET(hashRet, 0, sizeof(hashRet));
  13356. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13357. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, hashRet), 0);
  13358. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13359. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE), 0);
  13360. /* Test bad args. */
  13361. ExpectIntEQ(wc_Sha3_384_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13362. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13363. wc_Sha3_384_Free(&sha3);
  13364. #endif
  13365. return EXPECT_RESULT();
  13366. } /* END test_wc_Sha3_384_Final */
  13367. /*
  13368. * Testing wc_Sha3_512_Final()
  13369. */
  13370. static int test_wc_Sha3_512_Final(void)
  13371. {
  13372. EXPECT_DECLS;
  13373. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  13374. !defined(WOLFSSL_NOSHA3_384)
  13375. wc_Sha3 sha3;
  13376. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13377. "nopnopq";
  13378. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  13379. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  13380. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  13381. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  13382. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  13383. byte hash[WC_SHA3_512_DIGEST_SIZE];
  13384. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  13385. /* Init stack variables. */
  13386. XMEMSET(hash, 0, sizeof(hash));
  13387. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13388. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13389. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13390. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE), 0);
  13391. /* Test bad args. */
  13392. ExpectIntEQ(wc_Sha3_512_Final(NULL, hash), BAD_FUNC_ARG);
  13393. ExpectIntEQ(wc_Sha3_512_Final(&sha3, NULL), BAD_FUNC_ARG);
  13394. wc_Sha3_512_Free(&sha3);
  13395. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13396. /* Init stack variables. */
  13397. XMEMSET(hash, 0, sizeof(hash));
  13398. XMEMSET(hashRet, 0, sizeof(hashRet));
  13399. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13400. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, hashRet), 0);
  13401. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13402. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE), 0);
  13403. /* Test bad args. */
  13404. ExpectIntEQ(wc_Sha3_512_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13405. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13406. wc_Sha3_512_Free(&sha3);
  13407. #endif
  13408. return EXPECT_RESULT();
  13409. } /* END test_wc_Sha3_512_Final */
  13410. /*
  13411. * Testing wc_Sha3_224_Copy()
  13412. */
  13413. static int test_wc_Sha3_224_Copy(void)
  13414. {
  13415. EXPECT_DECLS;
  13416. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  13417. wc_Sha3 sha3, sha3Cpy;
  13418. const char* msg = TEST_STRING;
  13419. word32 msglen = (word32)TEST_STRING_SZ;
  13420. byte hash[WC_SHA3_224_DIGEST_SIZE];
  13421. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  13422. XMEMSET(hash, 0, sizeof(hash));
  13423. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13424. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13425. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13426. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13427. ExpectIntEQ(wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13428. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, msglen), 0);
  13429. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, &sha3), 0);
  13430. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13431. ExpectIntEQ(wc_Sha3_224_Final(&sha3Cpy, hashCpy), 0);
  13432. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13433. /* Test bad args. */
  13434. ExpectIntEQ(wc_Sha3_224_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13435. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13436. wc_Sha3_224_Free(&sha3);
  13437. wc_Sha3_224_Free(&sha3Cpy);
  13438. #endif
  13439. return EXPECT_RESULT();
  13440. } /* END test_wc_Sha3_224_Copy */
  13441. /*
  13442. * Testing wc_Sha3_256_Copy()
  13443. */
  13444. static int test_wc_Sha3_256_Copy(void)
  13445. {
  13446. EXPECT_DECLS;
  13447. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  13448. wc_Sha3 sha3, sha3Cpy;
  13449. const char* msg = TEST_STRING;
  13450. word32 msglen = (word32)TEST_STRING_SZ;
  13451. byte hash[WC_SHA3_256_DIGEST_SIZE];
  13452. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  13453. XMEMSET(hash, 0, sizeof(hash));
  13454. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13455. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13456. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13457. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13458. ExpectIntEQ(wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13459. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, msglen), 0);
  13460. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, &sha3), 0);
  13461. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13462. ExpectIntEQ(wc_Sha3_256_Final(&sha3Cpy, hashCpy), 0);
  13463. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13464. /* Test bad args. */
  13465. ExpectIntEQ(wc_Sha3_256_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13466. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13467. wc_Sha3_256_Free(&sha3);
  13468. wc_Sha3_256_Free(&sha3Cpy);
  13469. #endif
  13470. return EXPECT_RESULT();
  13471. } /* END test_wc_Sha3_256_Copy */
  13472. /*
  13473. * Testing wc_Sha3_384_Copy()
  13474. */
  13475. static int test_wc_Sha3_384_Copy(void)
  13476. {
  13477. EXPECT_DECLS;
  13478. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  13479. wc_Sha3 sha3, sha3Cpy;
  13480. const char* msg = TEST_STRING;
  13481. word32 msglen = (word32)TEST_STRING_SZ;
  13482. byte hash[WC_SHA3_384_DIGEST_SIZE];
  13483. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  13484. XMEMSET(hash, 0, sizeof(hash));
  13485. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13486. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13487. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13488. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13489. ExpectIntEQ(wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13490. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, msglen), 0);
  13491. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, &sha3), 0);
  13492. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13493. ExpectIntEQ(wc_Sha3_384_Final(&sha3Cpy, hashCpy), 0);
  13494. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13495. /* Test bad args. */
  13496. ExpectIntEQ(wc_Sha3_384_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13497. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13498. wc_Sha3_384_Free(&sha3);
  13499. wc_Sha3_384_Free(&sha3Cpy);
  13500. #endif
  13501. return EXPECT_RESULT();
  13502. } /* END test_wc_Sha3_384_Copy */
  13503. /*
  13504. * Testing wc_Sha3_512_Copy()
  13505. */
  13506. static int test_wc_Sha3_512_Copy(void)
  13507. {
  13508. EXPECT_DECLS;
  13509. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  13510. wc_Sha3 sha3, sha3Cpy;
  13511. const char* msg = TEST_STRING;
  13512. word32 msglen = (word32)TEST_STRING_SZ;
  13513. byte hash[WC_SHA3_512_DIGEST_SIZE];
  13514. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  13515. XMEMSET(hash, 0, sizeof(hash));
  13516. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13517. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13518. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13519. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13520. ExpectIntEQ(wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13521. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, msglen), 0);
  13522. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, &sha3), 0);
  13523. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13524. ExpectIntEQ(wc_Sha3_512_Final(&sha3Cpy, hashCpy), 0);
  13525. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13526. /* Test bad args. */
  13527. ExpectIntEQ(wc_Sha3_512_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13528. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13529. wc_Sha3_512_Free(&sha3);
  13530. wc_Sha3_512_Free(&sha3Cpy);
  13531. #endif
  13532. return EXPECT_RESULT();
  13533. } /* END test_wc_Sha3_512_Copy */
  13534. /*
  13535. * Unit test function for wc_Sha3_GetFlags()
  13536. */
  13537. static int test_wc_Sha3_GetFlags(void)
  13538. {
  13539. EXPECT_DECLS;
  13540. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  13541. wc_Sha3 sha3;
  13542. word32 flags = 0;
  13543. /* Initialize */
  13544. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13545. ExpectIntEQ(wc_Sha3_GetFlags(&sha3, &flags), 0);
  13546. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  13547. wc_Sha3_224_Free(&sha3);
  13548. #endif
  13549. return EXPECT_RESULT();
  13550. } /* END test_wc_Sha3_GetFlags */
  13551. static int test_wc_InitShake256(void)
  13552. {
  13553. EXPECT_DECLS;
  13554. #ifdef WOLFSSL_SHAKE256
  13555. wc_Shake shake;
  13556. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13557. /* Test bad args. */
  13558. ExpectIntEQ(wc_InitShake256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13559. wc_Shake256_Free(&shake);
  13560. #endif
  13561. return EXPECT_RESULT();
  13562. }
  13563. static int testing_wc_Shake256_Update(void)
  13564. {
  13565. EXPECT_DECLS;
  13566. #ifdef WOLFSSL_SHAKE256
  13567. wc_Shake shake;
  13568. byte msg[] = "Everybody's working for the weekend.";
  13569. byte msg2[] = "Everybody gets Friday off.";
  13570. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  13571. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  13572. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  13573. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  13574. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  13575. word32 msglen = sizeof(msg) - 1;
  13576. word32 msg2len = sizeof(msg2);
  13577. word32 msgCmplen = sizeof(msgCmp);
  13578. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13579. ExpectIntEQ(wc_Shake256_Update(&shake, msg, msglen), 0);
  13580. ExpectIntEQ(XMEMCMP(msg, shake.t, msglen), 0);
  13581. ExpectTrue(shake.i == msglen);
  13582. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  13583. ExpectIntEQ(XMEMCMP(shake.t, msgCmp, msgCmplen), 0);
  13584. /* Pass bad args. */
  13585. ExpectIntEQ(wc_Shake256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13586. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 5), BAD_FUNC_ARG);
  13587. wc_Shake256_Free(&shake);
  13588. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13589. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 0), 0);
  13590. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  13591. ExpectIntEQ(XMEMCMP(msg2, shake.t, msg2len), 0);
  13592. wc_Shake256_Free(&shake);
  13593. #endif /* WOLFSSL_SHAKE256 */
  13594. return EXPECT_RESULT();
  13595. }
  13596. static int test_wc_Shake256_Final(void)
  13597. {
  13598. EXPECT_DECLS;
  13599. #ifdef WOLFSSL_SHAKE256
  13600. wc_Shake shake;
  13601. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13602. "nopnopq";
  13603. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  13604. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  13605. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  13606. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  13607. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  13608. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  13609. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  13610. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  13611. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  13612. byte hash[114];
  13613. /* Init stack variables. */
  13614. XMEMSET(hash, 0, sizeof(hash));
  13615. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13616. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg)),
  13617. 0);
  13618. ExpectIntEQ(wc_Shake256_Final(&shake, hash, (word32)sizeof(hash)), 0);
  13619. ExpectIntEQ(XMEMCMP(expOut, hash, (word32)sizeof(hash)), 0);
  13620. /* Test bad args. */
  13621. ExpectIntEQ(wc_Shake256_Final(NULL, hash, (word32)sizeof(hash)),
  13622. BAD_FUNC_ARG);
  13623. ExpectIntEQ(wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash)),
  13624. BAD_FUNC_ARG);
  13625. wc_Shake256_Free(&shake);
  13626. #endif
  13627. return EXPECT_RESULT();
  13628. }
  13629. /*
  13630. * Testing wc_Shake256_Copy()
  13631. */
  13632. static int test_wc_Shake256_Copy(void)
  13633. {
  13634. EXPECT_DECLS;
  13635. #ifdef WOLFSSL_SHAKE256
  13636. wc_Shake shake, shakeCpy;
  13637. const char* msg = TEST_STRING;
  13638. word32 msglen = (word32)TEST_STRING_SZ;
  13639. byte hash[144];
  13640. byte hashCpy[144];
  13641. word32 hashLen = sizeof(hash);
  13642. word32 hashLenCpy = sizeof(hashCpy);
  13643. XMEMSET(hash, 0, sizeof(hash));
  13644. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13645. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13646. ExpectIntEQ(wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId), 0);
  13647. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, msglen), 0);
  13648. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, &shake), 0);
  13649. ExpectIntEQ(wc_Shake256_Final(&shake, hash, hashLen), 0);
  13650. ExpectIntEQ(wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy), 0);
  13651. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13652. /* Test bad args. */
  13653. ExpectIntEQ(wc_Shake256_Copy(NULL, &shake), BAD_FUNC_ARG);
  13654. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, NULL), BAD_FUNC_ARG);
  13655. wc_Shake256_Free(&shake);
  13656. wc_Shake256_Free(&shakeCpy);
  13657. #endif
  13658. return EXPECT_RESULT();
  13659. } /* END test_wc_Shake256_Copy */
  13660. /*
  13661. * Unit test function for wc_Shake256Hash()
  13662. */
  13663. static int test_wc_Shake256Hash(void)
  13664. {
  13665. EXPECT_DECLS;
  13666. #ifdef WOLFSSL_SHAKE256
  13667. const byte data[] = { /* Hello World */
  13668. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  13669. 0x72,0x6c,0x64
  13670. };
  13671. word32 len = sizeof(data);
  13672. byte hash[144];
  13673. word32 hashLen = sizeof(hash);
  13674. ExpectIntEQ(wc_Shake256Hash(data, len, hash, hashLen), 0);
  13675. #endif
  13676. return EXPECT_RESULT();
  13677. } /* END test_wc_Shake256Hash */
  13678. /*
  13679. * Testing wc_InitSm3(), wc_Sm3Free()
  13680. */
  13681. static int test_wc_InitSm3Free(void)
  13682. {
  13683. EXPECT_DECLS;
  13684. #ifdef WOLFSSL_SM3
  13685. wc_Sm3 sm3;
  13686. /* Invalid Parameters */
  13687. ExpectIntEQ(wc_InitSm3(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  13688. /* Valid Parameters */
  13689. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13690. wc_Sm3Free(NULL);
  13691. wc_Sm3Free(&sm3);
  13692. #endif
  13693. return EXPECT_RESULT();
  13694. } /* END test_wc_InitSm3 */
  13695. /*
  13696. * Testing wc_Sm3Update(), wc_Sm3Final()
  13697. */
  13698. static int test_wc_Sm3UpdateFinal(void)
  13699. {
  13700. EXPECT_DECLS;
  13701. #ifdef WOLFSSL_SM3
  13702. wc_Sm3 sm3;
  13703. byte data[WC_SM3_BLOCK_SIZE * 4];
  13704. byte hash[WC_SM3_DIGEST_SIZE];
  13705. byte calcHash[WC_SM3_DIGEST_SIZE];
  13706. byte expHash[WC_SM3_DIGEST_SIZE] = {
  13707. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  13708. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  13709. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  13710. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  13711. };
  13712. word32 chunk;
  13713. word32 i;
  13714. XMEMSET(data, 0, sizeof(data));
  13715. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13716. /* Invalid Parameters */
  13717. ExpectIntEQ(wc_Sm3Update(NULL, NULL, 1), BAD_FUNC_ARG);
  13718. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 1), BAD_FUNC_ARG);
  13719. ExpectIntEQ(wc_Sm3Update(NULL, data, 1), BAD_FUNC_ARG);
  13720. /* Valid Parameters */
  13721. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 0), 0);
  13722. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13723. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13724. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE), 0);
  13725. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE - 2), 0);
  13726. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE * 2), 0);
  13727. /* Ensure too many bytes for lengths. */
  13728. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_PAD_SIZE), 0);
  13729. /* Invalid Parameters */
  13730. ExpectIntEQ(wc_Sm3Final(NULL, NULL), BAD_FUNC_ARG);
  13731. ExpectIntEQ(wc_Sm3Final(&sm3, NULL), BAD_FUNC_ARG);
  13732. ExpectIntEQ(wc_Sm3Final(NULL, hash), BAD_FUNC_ARG);
  13733. /* Valid Parameters */
  13734. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13735. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  13736. /* Chunk tests. */
  13737. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  13738. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13739. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  13740. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  13741. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, chunk), 0);
  13742. }
  13743. if (i < (word32)sizeof(data)) {
  13744. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, (word32)sizeof(data) - i),
  13745. 0);
  13746. }
  13747. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13748. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13749. }
  13750. /* Not testing when the low 32-bit length overflows. */
  13751. wc_Sm3Free(&sm3);
  13752. #endif
  13753. return EXPECT_RESULT();
  13754. } /* END test_wc_Sm3Update */
  13755. /*
  13756. * Testing wc_Sm3GetHash()
  13757. */
  13758. static int test_wc_Sm3GetHash(void)
  13759. {
  13760. EXPECT_DECLS;
  13761. #ifdef WOLFSSL_SM3
  13762. wc_Sm3 sm3;
  13763. byte hash[WC_SM3_DIGEST_SIZE];
  13764. byte calcHash[WC_SM3_DIGEST_SIZE];
  13765. byte data[WC_SM3_BLOCK_SIZE];
  13766. XMEMSET(data, 0, sizeof(data));
  13767. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13768. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13769. /* Invalid Parameters */
  13770. ExpectIntEQ(wc_Sm3GetHash(NULL, NULL), BAD_FUNC_ARG);
  13771. ExpectIntEQ(wc_Sm3GetHash(&sm3, NULL), BAD_FUNC_ARG);
  13772. ExpectIntEQ(wc_Sm3GetHash(NULL, hash), BAD_FUNC_ARG);
  13773. /* Valid Parameters */
  13774. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  13775. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13776. /* With update. */
  13777. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  13778. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  13779. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13780. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13781. wc_Sm3Free(&sm3);
  13782. #endif
  13783. return EXPECT_RESULT();
  13784. } /* END test_wc_Sm3Update */
  13785. /*
  13786. * Testing wc_Sm3Copy()
  13787. */
  13788. static int test_wc_Sm3Copy(void)
  13789. {
  13790. EXPECT_DECLS;
  13791. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13792. wc_Sm3 sm3;
  13793. wc_Sm3 sm3Copy;
  13794. byte hash[WC_SM3_DIGEST_SIZE];
  13795. byte hashCopy[WC_SM3_DIGEST_SIZE];
  13796. byte data[WC_SM3_BLOCK_SIZE + 1];
  13797. int i;
  13798. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13799. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  13800. /* Invalid Parameters */
  13801. ExpectIntEQ(wc_Sm3Copy(NULL, NULL), BAD_FUNC_ARG);
  13802. ExpectIntEQ(wc_Sm3Copy(&sm3, NULL), BAD_FUNC_ARG);
  13803. ExpectIntEQ(wc_Sm3Copy(NULL, &sm3Copy), BAD_FUNC_ARG);
  13804. /* Valid Parameters */
  13805. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13806. /* Ensure all parts of data updated during hashing are copied. */
  13807. for (i = 0; i < WC_SM3_BLOCK_SIZE + 1; i++) {
  13808. ExpectIntEQ(wc_Sm3Update(&sm3, data, i), 0);
  13809. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13810. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13811. ExpectIntEQ(wc_Sm3Update(&sm3Copy, data, 1), 0);
  13812. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13813. ExpectIntEQ(wc_Sm3Final(&sm3Copy, hashCopy), 0);
  13814. ExpectBufEQ(hash, hashCopy, WC_SM3_DIGEST_SIZE);
  13815. }
  13816. wc_Sm3Free(&sm3Copy);
  13817. wc_Sm3Free(&sm3);
  13818. #endif
  13819. return EXPECT_RESULT();
  13820. } /* END test_wc_Sm3Copy */
  13821. /*
  13822. * Testing wc_Sm3FinalRaw()
  13823. */
  13824. static int test_wc_Sm3FinalRaw(void)
  13825. {
  13826. EXPECT_DECLS;
  13827. #if defined(WOLFSSL_SM3) && !defined(HAVE_SELFTEST) && \
  13828. !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  13829. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  13830. !defined(WOLFSSL_NO_HASH_RAW)
  13831. wc_Sm3 sm3;
  13832. byte hash1[WC_SM3_DIGEST_SIZE];
  13833. byte hash2[WC_SM3_DIGEST_SIZE];
  13834. byte hash3[WC_SM3_DIGEST_SIZE];
  13835. byte* hash_test[3] = { hash1, hash2, hash3 };
  13836. int times;
  13837. int i;
  13838. XMEMSET(&sm3, 0, sizeof(sm3));
  13839. /* Initialize */
  13840. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13841. /* Invalid Parameters */
  13842. ExpectIntEQ(wc_Sm3FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  13843. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, NULL), BAD_FUNC_ARG);
  13844. ExpectIntEQ(wc_Sm3FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  13845. times = sizeof(hash_test) / sizeof(byte*);
  13846. for (i = 0; i < times; i++) {
  13847. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, hash_test[i]), 0);
  13848. }
  13849. wc_Sm3Free(&sm3);
  13850. #endif
  13851. return EXPECT_RESULT();
  13852. } /* END test_wc_Sm3FinalRaw */
  13853. /*
  13854. * Testing wc_Sm3GetFlags, wc_Sm3SetFlags()
  13855. */
  13856. static int test_wc_Sm3GetSetFlags(void)
  13857. {
  13858. EXPECT_DECLS;
  13859. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13860. wc_Sm3 sm3;
  13861. wc_Sm3 sm3Copy;
  13862. word32 flags = 0;
  13863. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13864. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  13865. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  13866. ExpectIntEQ(flags, 0);
  13867. ExpectIntEQ(wc_Sm3SetFlags(NULL, WC_HASH_FLAG_WILLCOPY), 0);
  13868. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  13869. ExpectIntEQ(flags, 0);
  13870. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  13871. ExpectIntEQ(flags, 0);
  13872. ExpectIntEQ(wc_Sm3SetFlags(&sm3, WC_HASH_FLAG_WILLCOPY), 0);
  13873. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  13874. ExpectIntEQ(flags, WC_HASH_FLAG_WILLCOPY);
  13875. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13876. ExpectIntEQ(wc_Sm3GetFlags(&sm3Copy, &flags), 0);
  13877. ExpectIntEQ(flags, WC_HASH_FLAG_ISCOPY | WC_HASH_FLAG_WILLCOPY);
  13878. wc_Sm3Free(&sm3Copy);
  13879. wc_Sm3Free(&sm3);
  13880. #endif
  13881. return EXPECT_RESULT();
  13882. } /* END test_wc_Sm3Update */
  13883. /*
  13884. * Testing wc_Sm3Hash()
  13885. */
  13886. static int test_wc_Sm3Hash(void)
  13887. {
  13888. EXPECT_DECLS;
  13889. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13890. byte data[WC_SM3_BLOCK_SIZE];
  13891. byte hash[WC_SM3_DIGEST_SIZE];
  13892. /* Invalid parameters. */
  13893. ExpectIntEQ(wc_Sm3Hash(NULL, sizeof(data), hash), BAD_FUNC_ARG);
  13894. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), NULL), BAD_FUNC_ARG);
  13895. /* Valid parameters. */
  13896. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), hash), 0);
  13897. #endif
  13898. return EXPECT_RESULT();
  13899. } /* END test_wc_Sm3Hash */
  13900. /*
  13901. * Test function for wc_HmacSetKey
  13902. */
  13903. static int test_wc_Md5HmacSetKey(void)
  13904. {
  13905. EXPECT_DECLS;
  13906. #if !defined(NO_HMAC) && !defined(NO_MD5)
  13907. Hmac hmac;
  13908. int ret, times, itr;
  13909. const char* keys[]=
  13910. {
  13911. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  13912. #ifndef HAVE_FIPS
  13913. "Jefe", /* smaller than minimum FIPS key size */
  13914. #endif
  13915. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13916. };
  13917. times = sizeof(keys) / sizeof(char*);
  13918. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13919. for (itr = 0; itr < times; itr++) {
  13920. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  13921. (word32)XSTRLEN(keys[itr]));
  13922. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13923. wc_HmacFree(&hmac);
  13924. ExpectIntEQ(ret, BAD_FUNC_ARG);
  13925. #else
  13926. ExpectIntEQ(ret, 0);
  13927. #endif
  13928. }
  13929. /* Bad args. */
  13930. ExpectIntEQ(wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  13931. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13932. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0])),
  13933. BAD_FUNC_ARG);
  13934. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13935. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13936. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  13937. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13938. ExpectIntEQ(ret, BAD_FUNC_ARG);
  13939. #elif defined(HAVE_FIPS)
  13940. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13941. #else
  13942. ExpectIntEQ(ret, 0);
  13943. #endif
  13944. wc_HmacFree(&hmac);
  13945. #endif
  13946. return EXPECT_RESULT();
  13947. } /* END test_wc_Md5HmacSetKey */
  13948. /*
  13949. * testing wc_HmacSetKey() on wc_Sha hash.
  13950. */
  13951. static int test_wc_ShaHmacSetKey(void)
  13952. {
  13953. EXPECT_DECLS;
  13954. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13955. Hmac hmac;
  13956. int ret, times, itr;
  13957. const char* keys[]=
  13958. {
  13959. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13960. "\x0b\x0b\x0b",
  13961. #ifndef HAVE_FIPS
  13962. "Jefe", /* smaller than minimum FIPS key size */
  13963. #endif
  13964. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13965. "\xAA\xAA\xAA"
  13966. };
  13967. times = sizeof(keys) / sizeof(char*);
  13968. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13969. for (itr = 0; itr < times; itr++) {
  13970. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  13971. (word32)XSTRLEN(keys[itr])), 0);
  13972. }
  13973. /* Bad args. */
  13974. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  13975. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13976. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0])),
  13977. BAD_FUNC_ARG);
  13978. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13979. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13980. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  13981. #ifdef HAVE_FIPS
  13982. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13983. #else
  13984. ExpectIntEQ(ret, 0);
  13985. #endif
  13986. wc_HmacFree(&hmac);
  13987. #endif
  13988. return EXPECT_RESULT();
  13989. } /* END test_wc_ShaHmacSetKey() */
  13990. /*
  13991. * testing wc_HmacSetKey() on Sha224 hash.
  13992. */
  13993. static int test_wc_Sha224HmacSetKey(void)
  13994. {
  13995. EXPECT_DECLS;
  13996. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  13997. Hmac hmac;
  13998. int ret, times, itr;
  13999. const char* keys[]=
  14000. {
  14001. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14002. "\x0b\x0b\x0b",
  14003. #ifndef HAVE_FIPS
  14004. "Jefe", /* smaller than minimum FIPS key size */
  14005. #endif
  14006. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14007. "\xAA\xAA\xAA"
  14008. };
  14009. times = sizeof(keys) / sizeof(char*);
  14010. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14011. for (itr = 0; itr < times; itr++) {
  14012. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  14013. (word32)XSTRLEN(keys[itr])), 0);
  14014. }
  14015. /* Bad args. */
  14016. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  14017. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14018. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0])),
  14019. BAD_FUNC_ARG);
  14020. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14021. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14022. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  14023. #ifdef HAVE_FIPS
  14024. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14025. #else
  14026. ExpectIntEQ(ret, 0);
  14027. #endif
  14028. wc_HmacFree(&hmac);
  14029. #endif
  14030. return EXPECT_RESULT();
  14031. } /* END test_wc_Sha224HmacSetKey() */
  14032. /*
  14033. * testing wc_HmacSetKey() on Sha256 hash
  14034. */
  14035. static int test_wc_Sha256HmacSetKey(void)
  14036. {
  14037. EXPECT_DECLS;
  14038. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14039. Hmac hmac;
  14040. int ret, times, itr;
  14041. const char* keys[]=
  14042. {
  14043. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14044. "\x0b\x0b\x0b",
  14045. #ifndef HAVE_FIPS
  14046. "Jefe", /* smaller than minimum FIPS key size */
  14047. #endif
  14048. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14049. "\xAA\xAA\xAA"
  14050. };
  14051. times = sizeof(keys) / sizeof(char*);
  14052. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14053. for (itr = 0; itr < times; itr++) {
  14054. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  14055. (word32)XSTRLEN(keys[itr])), 0);
  14056. }
  14057. /* Bad args. */
  14058. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  14059. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14060. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0])),
  14061. BAD_FUNC_ARG);
  14062. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14063. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14064. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  14065. #ifdef HAVE_FIPS
  14066. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14067. #else
  14068. ExpectIntEQ(ret, 0);
  14069. #endif
  14070. wc_HmacFree(&hmac);
  14071. #endif
  14072. return EXPECT_RESULT();
  14073. } /* END test_wc_Sha256HmacSetKey() */
  14074. /*
  14075. * testing wc_HmacSetKey on Sha384 hash.
  14076. */
  14077. static int test_wc_Sha384HmacSetKey(void)
  14078. {
  14079. EXPECT_DECLS;
  14080. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14081. Hmac hmac;
  14082. int ret, times, itr;
  14083. const char* keys[]=
  14084. {
  14085. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14086. "\x0b\x0b\x0b",
  14087. #ifndef HAVE_FIPS
  14088. "Jefe", /* smaller than minimum FIPS key size */
  14089. #endif
  14090. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14091. "\xAA\xAA\xAA"
  14092. };
  14093. times = sizeof(keys) / sizeof(char*);
  14094. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14095. for (itr = 0; itr < times; itr++) {
  14096. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  14097. (word32)XSTRLEN(keys[itr])), 0);
  14098. }
  14099. /* Bad args. */
  14100. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  14101. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14102. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0])),
  14103. BAD_FUNC_ARG);
  14104. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14105. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14106. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  14107. #ifdef HAVE_FIPS
  14108. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14109. #else
  14110. ExpectIntEQ(ret, 0);
  14111. #endif
  14112. wc_HmacFree(&hmac);
  14113. #endif
  14114. return EXPECT_RESULT();
  14115. } /* END test_wc_Sha384HmacSetKey() */
  14116. /*
  14117. * testing wc_HmacUpdate on wc_Md5 hash.
  14118. */
  14119. static int test_wc_Md5HmacUpdate(void)
  14120. {
  14121. EXPECT_DECLS;
  14122. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  14123. Hmac hmac;
  14124. testVector a, b;
  14125. #ifdef HAVE_FIPS
  14126. const char* keys =
  14127. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14128. #else
  14129. const char* keys = "Jefe";
  14130. #endif
  14131. a.input = "what do ya want for nothing?";
  14132. a.inLen = XSTRLEN(a.input);
  14133. b.input = "Hi There";
  14134. b.inLen = XSTRLEN(b.input);
  14135. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14136. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys,
  14137. (word32)XSTRLEN(keys)), 0);
  14138. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14139. /* Update Hmac. */
  14140. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14141. /* Test bad args. */
  14142. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14143. BAD_FUNC_ARG);
  14144. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14145. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14146. wc_HmacFree(&hmac);
  14147. #endif
  14148. return EXPECT_RESULT();
  14149. } /* END test_wc_Md5HmacUpdate */
  14150. /*
  14151. * testing wc_HmacUpdate on SHA hash.
  14152. */
  14153. static int test_wc_ShaHmacUpdate(void)
  14154. {
  14155. EXPECT_DECLS;
  14156. #if !defined(NO_HMAC) && !defined(NO_SHA)
  14157. Hmac hmac;
  14158. testVector a, b;
  14159. #ifdef HAVE_FIPS
  14160. const char* keys =
  14161. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14162. #else
  14163. const char* keys = "Jefe";
  14164. #endif
  14165. a.input = "what do ya want for nothing?";
  14166. a.inLen = XSTRLEN(a.input);
  14167. b.input = "Hi There";
  14168. b.inLen = XSTRLEN(b.input);
  14169. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14170. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys,
  14171. (word32)XSTRLEN(keys)), 0);
  14172. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14173. /* Update Hmac. */
  14174. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14175. /* Test bad args. */
  14176. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14177. BAD_FUNC_ARG);
  14178. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14179. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14180. wc_HmacFree(&hmac);
  14181. #endif
  14182. return EXPECT_RESULT();
  14183. } /* END test_wc_ShaHmacUpdate */
  14184. /*
  14185. * testing wc_HmacUpdate on SHA224 hash.
  14186. */
  14187. static int test_wc_Sha224HmacUpdate(void)
  14188. {
  14189. EXPECT_DECLS;
  14190. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14191. Hmac hmac;
  14192. testVector a, b;
  14193. #ifdef HAVE_FIPS
  14194. const char* keys =
  14195. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14196. #else
  14197. const char* keys = "Jefe";
  14198. #endif
  14199. a.input = "what do ya want for nothing?";
  14200. a.inLen = XSTRLEN(a.input);
  14201. b.input = "Hi There";
  14202. b.inLen = XSTRLEN(b.input);
  14203. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14204. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys,
  14205. (word32)XSTRLEN(keys)), 0);
  14206. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14207. /* Update Hmac. */
  14208. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14209. /* Test bad args. */
  14210. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14211. BAD_FUNC_ARG);
  14212. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14213. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14214. wc_HmacFree(&hmac);
  14215. #endif
  14216. return EXPECT_RESULT();
  14217. } /* END test_wc_Sha224HmacUpdate */
  14218. /*
  14219. * testing wc_HmacUpdate on SHA256 hash.
  14220. */
  14221. static int test_wc_Sha256HmacUpdate(void)
  14222. {
  14223. EXPECT_DECLS;
  14224. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14225. Hmac hmac;
  14226. testVector a, b;
  14227. #ifdef HAVE_FIPS
  14228. const char* keys =
  14229. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14230. #else
  14231. const char* keys = "Jefe";
  14232. #endif
  14233. a.input = "what do ya want for nothing?";
  14234. a.inLen = XSTRLEN(a.input);
  14235. b.input = "Hi There";
  14236. b.inLen = XSTRLEN(b.input);
  14237. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14238. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys,
  14239. (word32)XSTRLEN(keys)), 0);
  14240. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14241. /* Update Hmac. */
  14242. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14243. /* Test bad args. */
  14244. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14245. BAD_FUNC_ARG);
  14246. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14247. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14248. wc_HmacFree(&hmac);
  14249. #endif
  14250. return EXPECT_RESULT();
  14251. } /* END test_wc_Sha256HmacUpdate */
  14252. /*
  14253. * testing wc_HmacUpdate on SHA384 hash.
  14254. */
  14255. static int test_wc_Sha384HmacUpdate(void)
  14256. {
  14257. EXPECT_DECLS;
  14258. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14259. Hmac hmac;
  14260. testVector a, b;
  14261. #ifdef HAVE_FIPS
  14262. const char* keys =
  14263. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14264. #else
  14265. const char* keys = "Jefe";
  14266. #endif
  14267. a.input = "what do ya want for nothing?";
  14268. a.inLen = XSTRLEN(a.input);
  14269. b.input = "Hi There";
  14270. b.inLen = XSTRLEN(b.input);
  14271. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14272. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys,
  14273. (word32)XSTRLEN(keys)), 0);
  14274. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14275. /* Update Hmac. */
  14276. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14277. /* Test bad args. */
  14278. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14279. BAD_FUNC_ARG);
  14280. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14281. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14282. wc_HmacFree(&hmac);
  14283. #endif
  14284. return EXPECT_RESULT();
  14285. } /* END test_wc_Sha384HmacUpdate */
  14286. /*
  14287. * Testing wc_HmacFinal() with MD5
  14288. */
  14289. static int test_wc_Md5HmacFinal(void)
  14290. {
  14291. EXPECT_DECLS;
  14292. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  14293. Hmac hmac;
  14294. byte hash[WC_MD5_DIGEST_SIZE];
  14295. testVector a;
  14296. const char* key;
  14297. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14298. a.input = "Hi There";
  14299. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  14300. "\x9d";
  14301. a.inLen = XSTRLEN(a.input);
  14302. a.outLen = XSTRLEN(a.output);
  14303. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14304. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key)),
  14305. 0);
  14306. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14307. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14308. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  14309. /* Try bad parameters. */
  14310. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14311. #ifndef HAVE_FIPS
  14312. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14313. #endif
  14314. wc_HmacFree(&hmac);
  14315. #endif
  14316. return EXPECT_RESULT();
  14317. } /* END test_wc_Md5HmacFinal */
  14318. /*
  14319. * Testing wc_HmacFinal() with SHA
  14320. */
  14321. static int test_wc_ShaHmacFinal(void)
  14322. {
  14323. EXPECT_DECLS;
  14324. #if !defined(NO_HMAC) && !defined(NO_SHA)
  14325. Hmac hmac;
  14326. byte hash[WC_SHA_DIGEST_SIZE];
  14327. testVector a;
  14328. const char* key;
  14329. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14330. "\x0b\x0b\x0b";
  14331. a.input = "Hi There";
  14332. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  14333. "\x8e\xf1\x46\xbe\x00";
  14334. a.inLen = XSTRLEN(a.input);
  14335. a.outLen = XSTRLEN(a.output);
  14336. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14337. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key)),
  14338. 0);
  14339. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14340. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14341. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  14342. /* Try bad parameters. */
  14343. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14344. #ifndef HAVE_FIPS
  14345. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14346. #endif
  14347. wc_HmacFree(&hmac);
  14348. #endif
  14349. return EXPECT_RESULT();
  14350. } /* END test_wc_ShaHmacFinal */
  14351. /*
  14352. * Testing wc_HmacFinal() with SHA224
  14353. */
  14354. static int test_wc_Sha224HmacFinal(void)
  14355. {
  14356. EXPECT_DECLS;
  14357. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14358. Hmac hmac;
  14359. byte hash[WC_SHA224_DIGEST_SIZE];
  14360. testVector a;
  14361. const char* key;
  14362. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14363. "\x0b\x0b\x0b";
  14364. a.input = "Hi There";
  14365. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  14366. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  14367. a.inLen = XSTRLEN(a.input);
  14368. a.outLen = XSTRLEN(a.output);
  14369. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14370. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key,
  14371. (word32)XSTRLEN(key)), 0);
  14372. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14373. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14374. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  14375. /* Try bad parameters. */
  14376. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14377. #ifndef HAVE_FIPS
  14378. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14379. #endif
  14380. wc_HmacFree(&hmac);
  14381. #endif
  14382. return EXPECT_RESULT();
  14383. } /* END test_wc_Sha224HmacFinal */
  14384. /*
  14385. * Testing wc_HmacFinal() with SHA256
  14386. */
  14387. static int test_wc_Sha256HmacFinal(void)
  14388. {
  14389. EXPECT_DECLS;
  14390. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14391. Hmac hmac;
  14392. byte hash[WC_SHA256_DIGEST_SIZE];
  14393. testVector a;
  14394. const char* key;
  14395. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14396. "\x0b\x0b\x0b";
  14397. a.input = "Hi There";
  14398. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  14399. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  14400. "\xcf\xf7";
  14401. a.inLen = XSTRLEN(a.input);
  14402. a.outLen = XSTRLEN(a.output);
  14403. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14404. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key,
  14405. (word32)XSTRLEN(key)), 0);
  14406. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14407. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14408. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  14409. /* Try bad parameters. */
  14410. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14411. #ifndef HAVE_FIPS
  14412. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14413. #endif
  14414. wc_HmacFree(&hmac);
  14415. #endif
  14416. return EXPECT_RESULT();
  14417. } /* END test_wc_Sha256HmacFinal */
  14418. /*
  14419. * Testing wc_HmacFinal() with SHA384
  14420. */
  14421. static int test_wc_Sha384HmacFinal(void)
  14422. {
  14423. EXPECT_DECLS;
  14424. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14425. Hmac hmac;
  14426. byte hash[WC_SHA384_DIGEST_SIZE];
  14427. testVector a;
  14428. const char* key;
  14429. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14430. "\x0b\x0b\x0b";
  14431. a.input = "Hi There";
  14432. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  14433. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  14434. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  14435. "\xfa\x9c\xb6";
  14436. a.inLen = XSTRLEN(a.input);
  14437. a.outLen = XSTRLEN(a.output);
  14438. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14439. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key,
  14440. (word32)XSTRLEN(key)), 0);
  14441. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14442. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14443. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  14444. /* Try bad parameters. */
  14445. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14446. #ifndef HAVE_FIPS
  14447. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14448. #endif
  14449. wc_HmacFree(&hmac);
  14450. #endif
  14451. return EXPECT_RESULT();
  14452. } /* END test_wc_Sha384HmacFinal */
  14453. /*
  14454. * Testing wc_InitCmac()
  14455. */
  14456. static int test_wc_InitCmac(void)
  14457. {
  14458. EXPECT_DECLS;
  14459. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  14460. Cmac cmac1;
  14461. Cmac cmac2;
  14462. Cmac cmac3;
  14463. /* AES 128 key. */
  14464. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14465. "\x09\x10\x11\x12\x13\x14\x15\x16";
  14466. /* AES 192 key. */
  14467. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14468. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14469. "\x01\x02\x03\x04\x05\x06\x07\x08";
  14470. /* AES 256 key. */
  14471. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14472. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14473. "\x01\x02\x03\x04\x05\x06\x07\x08"
  14474. "\x09\x01\x11\x12\x13\x14\x15\x16";
  14475. word32 key1Sz = (word32)sizeof(key1) - 1;
  14476. word32 key2Sz = (word32)sizeof(key2) - 1;
  14477. word32 key3Sz = (word32)sizeof(key3) - 1;
  14478. int type = WC_CMAC_AES;
  14479. (void)key1;
  14480. (void)key1Sz;
  14481. (void)key2;
  14482. (void)key2Sz;
  14483. XMEMSET(&cmac1, 0, sizeof(Cmac));
  14484. XMEMSET(&cmac2, 0, sizeof(Cmac));
  14485. XMEMSET(&cmac3, 0, sizeof(Cmac));
  14486. #ifdef WOLFSSL_AES_128
  14487. ExpectIntEQ(wc_InitCmac(&cmac1, key1, key1Sz, type, NULL), 0);
  14488. #endif
  14489. #ifdef WOLFSSL_AES_192
  14490. wc_AesFree(&cmac1.aes);
  14491. ExpectIntEQ(wc_InitCmac(&cmac2, key2, key2Sz, type, NULL), 0);
  14492. #endif
  14493. #ifdef WOLFSSL_AES_256
  14494. wc_AesFree(&cmac2.aes);
  14495. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, type, NULL), 0);
  14496. #endif
  14497. wc_AesFree(&cmac3.aes);
  14498. /* Test bad args. */
  14499. ExpectIntEQ(wc_InitCmac(NULL, key3, key3Sz, type, NULL), BAD_FUNC_ARG);
  14500. ExpectIntEQ(wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL), BAD_FUNC_ARG);
  14501. ExpectIntEQ(wc_InitCmac(&cmac3, key3, 0, type, NULL), BAD_FUNC_ARG);
  14502. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL), BAD_FUNC_ARG);
  14503. #endif
  14504. return EXPECT_RESULT();
  14505. } /* END test_wc_InitCmac */
  14506. /*
  14507. * Testing wc_CmacUpdate()
  14508. */
  14509. static int test_wc_CmacUpdate(void)
  14510. {
  14511. EXPECT_DECLS;
  14512. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14513. Cmac cmac;
  14514. byte key[] = {
  14515. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14516. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14517. };
  14518. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  14519. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  14520. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  14521. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  14522. "\xf4";
  14523. word32 inSz = (word32)sizeof(in) - 1;
  14524. word32 keySz = (word32)sizeof(key);
  14525. int type = WC_CMAC_AES;
  14526. XMEMSET(&cmac, 0, sizeof(Cmac));
  14527. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  14528. ExpectIntEQ(wc_CmacUpdate(&cmac, in, inSz), 0);
  14529. /* Test bad args. */
  14530. ExpectIntEQ(wc_CmacUpdate(NULL, in, inSz), BAD_FUNC_ARG);
  14531. ExpectIntEQ(wc_CmacUpdate(&cmac, NULL, 30), BAD_FUNC_ARG);
  14532. wc_AesFree(&cmac.aes);
  14533. #endif
  14534. return EXPECT_RESULT();
  14535. } /* END test_wc_CmacUpdate */
  14536. /*
  14537. * Testing wc_CmacFinal()
  14538. */
  14539. static int test_wc_CmacFinal(void)
  14540. {
  14541. EXPECT_DECLS;
  14542. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14543. Cmac cmac;
  14544. byte key[] = {
  14545. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14546. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14547. };
  14548. byte msg[] = {
  14549. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  14550. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  14551. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  14552. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  14553. 0xf4
  14554. };
  14555. /* Test vectors from CMACGenAES128.rsp from
  14556. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  14557. * Per RFC4493 truncation of lsb is possible.
  14558. */
  14559. byte expMac[] = {
  14560. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  14561. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  14562. };
  14563. byte mac[AES_BLOCK_SIZE];
  14564. word32 msgSz = (word32)sizeof(msg);
  14565. word32 keySz = (word32)sizeof(key);
  14566. word32 macSz = sizeof(mac);
  14567. word32 badMacSz = 17;
  14568. int expMacSz = sizeof(expMac);
  14569. int type = WC_CMAC_AES;
  14570. XMEMSET(&cmac, 0, sizeof(Cmac));
  14571. XMEMSET(mac, 0, macSz);
  14572. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  14573. ExpectIntEQ(wc_CmacUpdate(&cmac, msg, msgSz), 0);
  14574. #if (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  14575. /* Pass in bad args. */
  14576. ExpectIntEQ(wc_CmacFinalNoFree(NULL, mac, &macSz), BAD_FUNC_ARG);
  14577. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  14578. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, mac, &badMacSz), BUFFER_E);
  14579. /* For the last call, use the API with implicit wc_CmacFree(). */
  14580. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  14581. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14582. #else /* !HAVE_FIPS || FIPS>=5.3 */
  14583. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  14584. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14585. /* Pass in bad args. */
  14586. ExpectIntEQ(wc_CmacFinal(NULL, mac, &macSz), BAD_FUNC_ARG);
  14587. ExpectIntEQ(wc_CmacFinal(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  14588. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &badMacSz), BUFFER_E);
  14589. #endif /* !HAVE_FIPS || FIPS>=5.3 */
  14590. #endif
  14591. return EXPECT_RESULT();
  14592. } /* END test_wc_CmacFinal */
  14593. /*
  14594. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  14595. */
  14596. static int test_wc_AesCmacGenerate(void)
  14597. {
  14598. EXPECT_DECLS;
  14599. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14600. byte key[] = {
  14601. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  14602. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  14603. };
  14604. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  14605. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  14606. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  14607. "\x3d\x32\x65\x4c\x66\x23\xc5";
  14608. byte mac[AES_BLOCK_SIZE];
  14609. word32 keySz = sizeof(key);
  14610. word32 macSz = sizeof(mac);
  14611. word32 msgSz = sizeof(msg) - 1;
  14612. word32 expMacSz = sizeof(expMac) - 1;
  14613. XMEMSET(mac, 0, macSz);
  14614. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz), 0);
  14615. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14616. /* Pass in bad args. */
  14617. ExpectIntEQ(wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz),
  14618. BAD_FUNC_ARG);
  14619. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz),
  14620. BAD_FUNC_ARG);
  14621. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0),
  14622. BAD_FUNC_ARG);
  14623. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz),
  14624. BAD_FUNC_ARG);
  14625. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz), 0);
  14626. /* Test bad args. */
  14627. ExpectIntEQ(wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz),
  14628. BAD_FUNC_ARG);
  14629. ExpectIntEQ(wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz),
  14630. BAD_FUNC_ARG);
  14631. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz),
  14632. BAD_FUNC_ARG);
  14633. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0),
  14634. BAD_FUNC_ARG);
  14635. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz),
  14636. BAD_FUNC_ARG);
  14637. #endif
  14638. return EXPECT_RESULT();
  14639. } /* END test_wc_AesCmacGenerate */
  14640. /*
  14641. * Testing streaming AES-GCM API.
  14642. */
  14643. static int test_wc_AesGcmStream(void)
  14644. {
  14645. EXPECT_DECLS;
  14646. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  14647. defined(WOLFSSL_AESGCM_STREAM)
  14648. int i;
  14649. WC_RNG rng[1];
  14650. Aes aesEnc[1];
  14651. Aes aesDec[1];
  14652. byte tag[AES_BLOCK_SIZE];
  14653. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  14654. byte out[AES_BLOCK_SIZE * 3 + 2];
  14655. byte plain[AES_BLOCK_SIZE * 3 + 2];
  14656. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  14657. byte key[AES_128_KEY_SIZE] = { 0, };
  14658. byte iv[AES_IV_SIZE] = { 1, };
  14659. byte ivOut[AES_IV_SIZE];
  14660. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  14661. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  14662. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  14663. };
  14664. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  14665. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  14666. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  14667. };
  14668. static const byte expTag[AES_BLOCK_SIZE] = {
  14669. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  14670. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  14671. };
  14672. XMEMSET(&rng, 0, sizeof(WC_RNG));
  14673. XMEMSET(&aesEnc, 0, sizeof(Aes));
  14674. XMEMSET(&aesDec, 0, sizeof(Aes));
  14675. /* Create a random for generating IV/nonce. */
  14676. ExpectIntEQ(wc_InitRng(rng), 0);
  14677. /* Initialize data structures. */
  14678. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14679. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14680. /* BadParameters to streaming init. */
  14681. ExpectIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14682. ExpectIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14683. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  14684. BAD_FUNC_ARG);
  14685. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  14686. BAD_FUNC_ARG);
  14687. /* Bad parameters to encrypt update. */
  14688. ExpectIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14689. BAD_FUNC_ARG);
  14690. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  14691. BAD_FUNC_ARG);
  14692. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  14693. BAD_FUNC_ARG);
  14694. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  14695. BAD_FUNC_ARG);
  14696. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  14697. BAD_FUNC_ARG);
  14698. /* Bad parameters to decrypt update. */
  14699. ExpectIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14700. BAD_FUNC_ARG);
  14701. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  14702. BAD_FUNC_ARG);
  14703. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  14704. BAD_FUNC_ARG);
  14705. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  14706. BAD_FUNC_ARG);
  14707. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  14708. BAD_FUNC_ARG);
  14709. /* Bad parameters to encrypt final. */
  14710. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14711. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14712. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14713. BAD_FUNC_ARG);
  14714. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  14715. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  14716. BAD_FUNC_ARG);
  14717. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  14718. BAD_FUNC_ARG);
  14719. /* Bad parameters to decrypt final. */
  14720. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14721. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14722. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14723. BAD_FUNC_ARG);
  14724. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  14725. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  14726. BAD_FUNC_ARG);
  14727. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  14728. BAD_FUNC_ARG);
  14729. /* Check calling final before setting key fails. */
  14730. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  14731. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  14732. /* Check calling update before setting key else fails. */
  14733. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14734. MISSING_KEY);
  14735. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14736. MISSING_KEY);
  14737. /* Set key but not IV. */
  14738. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  14739. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  14740. /* Check calling final before setting IV fails. */
  14741. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  14742. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  14743. /* Check calling update before setting IV else fails. */
  14744. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14745. MISSING_IV);
  14746. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14747. MISSING_IV);
  14748. /* Set IV using fixed part IV and external IV APIs. */
  14749. ExpectIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  14750. rng), 0);
  14751. ExpectIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  14752. GCM_NONCE_MID_SZ), 0);
  14753. ExpectIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  14754. ExpectIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  14755. /* Encrypt and decrypt data. */
  14756. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  14757. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  14758. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  14759. /* Finalize and check tag matches. */
  14760. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14761. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14762. /* Set key and IV through streaming init API. */
  14763. wc_AesFree(aesEnc);
  14764. wc_AesFree(aesDec);
  14765. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14766. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14767. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14768. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14769. /* Encrypt/decrypt one block and AAD of one block. */
  14770. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  14771. AES_BLOCK_SIZE), 0);
  14772. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  14773. AES_BLOCK_SIZE), 0);
  14774. ExpectIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  14775. /* Finalize and check tag matches. */
  14776. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14777. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14778. /* Set key and IV through streaming init API. */
  14779. wc_AesFree(aesEnc);
  14780. wc_AesFree(aesDec);
  14781. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14782. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14783. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14784. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14785. /* No data to encrypt/decrypt one byte of AAD. */
  14786. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  14787. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  14788. /* Finalize and check tag matches. */
  14789. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14790. ExpectIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  14791. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14792. /* Set key and IV through streaming init API. */
  14793. wc_AesFree(aesEnc);
  14794. wc_AesFree(aesDec);
  14795. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14796. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14797. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14798. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14799. /* Encrypt/decrypt one byte and no AAD. */
  14800. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  14801. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  14802. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  14803. /* Finalize and check tag matches. */
  14804. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14805. ExpectIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  14806. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14807. /* Set key and IV through streaming init API. */
  14808. wc_AesFree(aesEnc);
  14809. wc_AesFree(aesDec);
  14810. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14811. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14812. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14813. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14814. /* Encryption AES is one byte at a time */
  14815. for (i = 0; i < (int)sizeof(aad); i++) {
  14816. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  14817. 0);
  14818. }
  14819. for (i = 0; i < (int)sizeof(in); i++) {
  14820. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  14821. 0);
  14822. }
  14823. /* Decryption AES is two bytes at a time */
  14824. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14825. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  14826. 0);
  14827. }
  14828. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14829. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  14830. 0), 0);
  14831. }
  14832. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14833. /* Finalize and check tag matches. */
  14834. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14835. ExpectIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  14836. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14837. /* Check streaming encryption can be decrypted with one shot. */
  14838. wc_AesFree(aesDec);
  14839. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14840. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14841. ExpectIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  14842. ExpectIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  14843. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14844. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14845. wc_AesFree(aesEnc);
  14846. wc_AesFree(aesDec);
  14847. wc_FreeRng(rng);
  14848. #endif
  14849. return EXPECT_RESULT();
  14850. } /* END test_wc_AesGcmStream */
  14851. /*
  14852. * Testing streaming SM4 API.
  14853. */
  14854. static int test_wc_Sm4(void)
  14855. {
  14856. int res = TEST_SKIPPED;
  14857. #ifdef WOLFSSL_SM4
  14858. EXPECT_DECLS;
  14859. wc_Sm4 sm4;
  14860. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  14861. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  14862. unsigned char key[SM4_KEY_SIZE];
  14863. #endif
  14864. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  14865. unsigned char iv[SM4_IV_SIZE];
  14866. #endif
  14867. /* Invalid parameters - wc_Sm4Init */
  14868. ExpectIntEQ(wc_Sm4Init(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  14869. /* Valid cases - wc_Sm4Init */
  14870. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14871. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  14872. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  14873. XMEMSET(key, 0, sizeof(key));
  14874. /* Invalid parameters - wc_Sm4SetKey. */
  14875. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  14876. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  14877. ExpectIntEQ(wc_Sm4SetKey(NULL, key, 0), BAD_FUNC_ARG);
  14878. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14879. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, 0), BAD_FUNC_ARG);
  14880. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14881. ExpectIntEQ(wc_Sm4SetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14882. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE-1), BAD_FUNC_ARG);
  14883. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE+1), BAD_FUNC_ARG);
  14884. /* Valid cases - wc_Sm4SetKey. */
  14885. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14886. #endif
  14887. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  14888. XMEMSET(iv, 0, sizeof(iv));
  14889. /* Invalid parameters - wc_Sm4SetIV. */
  14890. ExpectIntEQ(wc_Sm4SetIV(NULL, NULL), BAD_FUNC_ARG);
  14891. ExpectIntEQ(wc_Sm4SetIV(&sm4, NULL), BAD_FUNC_ARG);
  14892. ExpectIntEQ(wc_Sm4SetIV(NULL, iv), BAD_FUNC_ARG);
  14893. /* Valid cases - wc_Sm4SetIV. */
  14894. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14895. #endif
  14896. /* Valid cases - wc_Sm4Free */
  14897. wc_Sm4Free(NULL);
  14898. wc_Sm4Free(&sm4);
  14899. res = EXPECT_RESULT();
  14900. #endif
  14901. return res;
  14902. } /* END test_wc_Sm4 */
  14903. /*
  14904. * Testing block based SM4-ECB API.
  14905. */
  14906. static int test_wc_Sm4Ecb(void)
  14907. {
  14908. int res = TEST_SKIPPED;
  14909. #ifdef WOLFSSL_SM4_ECB
  14910. EXPECT_DECLS;
  14911. wc_Sm4 sm4;
  14912. unsigned char key[SM4_KEY_SIZE];
  14913. unsigned char in[SM4_BLOCK_SIZE * 2];
  14914. unsigned char out[SM4_BLOCK_SIZE * 2];
  14915. unsigned char out2[SM4_BLOCK_SIZE];
  14916. XMEMSET(key, 0, sizeof(key));
  14917. XMEMSET(in, 0, sizeof(in));
  14918. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14919. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14920. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), MISSING_KEY);
  14921. /* Tested in test_wc_Sm4. */
  14922. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14923. /* Invalid parameters - wc_Sm4EcbEncrypt. */
  14924. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14925. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14926. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14927. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14928. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14929. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14930. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14931. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14932. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14933. /* Valid cases - wc_Sm4EcbEncrypt. */
  14934. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), 0);
  14935. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14936. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14937. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14938. /* In and out are same pointer. */
  14939. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14940. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14941. /* Invalid parameters - wc_Sm4EcbDecrypt. */
  14942. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14943. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14944. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14945. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14946. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14947. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14948. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14949. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14950. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14951. /* Valid cases - wc_Sm4EcbDecrypt. */
  14952. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), 0);
  14953. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14954. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14955. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14956. /* In and out are same pointer. */
  14957. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14958. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14959. wc_Sm4Free(&sm4);
  14960. res = EXPECT_RESULT();
  14961. #endif
  14962. return res;
  14963. } /* END test_wc_Sm4Ecb */
  14964. /*
  14965. * Testing block based SM4-CBC API.
  14966. */
  14967. static int test_wc_Sm4Cbc(void)
  14968. {
  14969. int res = TEST_SKIPPED;
  14970. #ifdef WOLFSSL_SM4_CBC
  14971. EXPECT_DECLS;
  14972. wc_Sm4 sm4;
  14973. unsigned char key[SM4_KEY_SIZE];
  14974. unsigned char iv[SM4_IV_SIZE];
  14975. unsigned char in[SM4_BLOCK_SIZE * 2];
  14976. unsigned char out[SM4_BLOCK_SIZE * 2];
  14977. unsigned char out2[SM4_BLOCK_SIZE];
  14978. XMEMSET(key, 0, sizeof(key));
  14979. XMEMSET(iv, 0, sizeof(iv));
  14980. XMEMSET(in, 0, sizeof(in));
  14981. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14982. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14983. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_KEY);
  14984. /* Tested in test_wc_Sm4. */
  14985. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14986. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_IV);
  14987. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_IV);
  14988. /* Tested in test_wc_Sm4. */
  14989. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14990. /* Invalid parameters - wc_Sm4CbcEncrypt. */
  14991. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14992. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14993. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14994. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14995. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14996. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14997. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14998. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14999. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  15000. /* Valid cases - wc_Sm4CbcEncrypt. */
  15001. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), 0);
  15002. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  15003. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15004. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15005. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  15006. /* In and out are same pointer. */
  15007. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15008. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  15009. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  15010. /* Invalid parameters - wc_Sm4CbcDecrypt. */
  15011. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  15012. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  15013. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  15014. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  15015. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  15016. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  15017. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  15018. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  15019. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  15020. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15021. /* Valid cases - wc_Sm4CbcDecrypt. */
  15022. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), 0);
  15023. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  15024. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15025. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15026. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  15027. /* In and out are same pointer. */
  15028. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15029. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  15030. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  15031. wc_Sm4Free(&sm4);
  15032. res = EXPECT_RESULT();
  15033. #endif
  15034. return res;
  15035. } /* END test_wc_Sm4Cbc */
  15036. /*
  15037. * Testing streaming SM4-CTR API.
  15038. */
  15039. static int test_wc_Sm4Ctr(void)
  15040. {
  15041. int res = TEST_SKIPPED;
  15042. #ifdef WOLFSSL_SM4_CTR
  15043. EXPECT_DECLS;
  15044. wc_Sm4 sm4;
  15045. unsigned char key[SM4_KEY_SIZE];
  15046. unsigned char iv[SM4_IV_SIZE];
  15047. unsigned char in[SM4_BLOCK_SIZE * 4];
  15048. unsigned char out[SM4_BLOCK_SIZE * 4];
  15049. unsigned char out2[SM4_BLOCK_SIZE * 4];
  15050. word32 chunk;
  15051. word32 i;
  15052. XMEMSET(key, 0, sizeof(key));
  15053. XMEMSET(iv, 0, sizeof(iv));
  15054. XMEMSET(in, 0, sizeof(in));
  15055. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15056. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_KEY);
  15057. /* Tested in test_wc_Sm4. */
  15058. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15059. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_IV);
  15060. /* Tested in test_wc_Sm4. */
  15061. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15062. /* Invalid parameters - wc_Sm4CtrEncrypt. */
  15063. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  15064. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, NULL, 0), BAD_FUNC_ARG);
  15065. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, NULL, 0), BAD_FUNC_ARG);
  15066. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, in, 0), BAD_FUNC_ARG);
  15067. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  15068. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  15069. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  15070. /* Valid cases - wc_Sm4CtrEncrypt. */
  15071. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), 0);
  15072. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, 1), 0);
  15073. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15074. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 2), 0);
  15075. ExpectIntEQ(XMEMCMP(out, out2, 1), 0);
  15076. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15077. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  15078. ExpectIntEQ(XMEMCMP(out2, out, 2), 0);
  15079. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15080. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15081. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  15082. /* In and out are same pointer. Also check encrypt of cipher text produces
  15083. * plaintext.
  15084. */
  15085. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15086. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, out, SM4_BLOCK_SIZE * 2), 0);
  15087. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  15088. /* Chunking tests. */
  15089. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15090. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, (word32)sizeof(in)), 0);
  15091. for (chunk = 1; chunk <= SM4_BLOCK_SIZE + 1; chunk++) {
  15092. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15093. for (i = 0; i + chunk <= (word32)sizeof(in); i += chunk) {
  15094. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i, chunk), 0);
  15095. }
  15096. if (i < (word32)sizeof(in)) {
  15097. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i,
  15098. (word32)sizeof(in) - i), 0);
  15099. }
  15100. ExpectIntEQ(XMEMCMP(out, out2, (word32)sizeof(out)), 0);
  15101. }
  15102. for (i = 0; i < (word32)sizeof(iv); i++) {
  15103. iv[i] = 0xff;
  15104. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15105. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15106. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15107. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, out, SM4_BLOCK_SIZE * 2), 0);
  15108. ExpectIntEQ(XMEMCMP(out2, in, SM4_BLOCK_SIZE * 2), 0);
  15109. }
  15110. wc_Sm4Free(&sm4);
  15111. res = EXPECT_RESULT();
  15112. #endif
  15113. return res;
  15114. } /* END test_wc_Sm4Ctr */
  15115. /*
  15116. * Testing stream SM4-GCM API.
  15117. */
  15118. static int test_wc_Sm4Gcm(void)
  15119. {
  15120. int res = TEST_SKIPPED;
  15121. #ifdef WOLFSSL_SM4_GCM
  15122. EXPECT_DECLS;
  15123. wc_Sm4 sm4;
  15124. unsigned char key[SM4_KEY_SIZE];
  15125. unsigned char nonce[GCM_NONCE_MAX_SZ];
  15126. unsigned char in[SM4_BLOCK_SIZE * 2];
  15127. unsigned char in2[SM4_BLOCK_SIZE * 2];
  15128. unsigned char out[SM4_BLOCK_SIZE * 2];
  15129. unsigned char out2[SM4_BLOCK_SIZE * 2];
  15130. unsigned char dec[SM4_BLOCK_SIZE * 2];
  15131. unsigned char tag[SM4_BLOCK_SIZE];
  15132. unsigned char aad[SM4_BLOCK_SIZE * 2];
  15133. word32 i;
  15134. XMEMSET(key, 0, sizeof(key));
  15135. XMEMSET(nonce, 0, sizeof(nonce));
  15136. XMEMSET(in, 0, sizeof(in));
  15137. XMEMSET(in2, 0, sizeof(in2));
  15138. XMEMSET(aad, 0, sizeof(aad));
  15139. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15140. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  15141. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15142. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  15143. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15144. /* Invalid parameters - wc_Sm4GcmSetKey. */
  15145. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  15146. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  15147. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, 0), BAD_FUNC_ARG);
  15148. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15149. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, 0), BAD_FUNC_ARG);
  15150. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15151. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15152. /* Valid parameters - wc_Sm4GcmSetKey. */
  15153. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15154. /* Invalid parameters - wc_Sm4GcmEncrypt. */
  15155. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15156. 0), BAD_FUNC_ARG);
  15157. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15158. 0), BAD_FUNC_ARG);
  15159. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15160. 0), BAD_FUNC_ARG);
  15161. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15162. 0), BAD_FUNC_ARG);
  15163. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15164. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15165. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15166. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15167. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15168. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15169. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  15170. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15171. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15172. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15173. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  15174. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15175. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  15176. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15177. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  15178. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15179. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15180. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15181. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15182. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15183. /* Invalid parameters - wc_Sm4GcmDecrypt. */
  15184. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15185. 0), BAD_FUNC_ARG);
  15186. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15187. 0), BAD_FUNC_ARG);
  15188. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15189. 0), BAD_FUNC_ARG);
  15190. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15191. 0), BAD_FUNC_ARG);
  15192. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15193. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15194. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15195. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15196. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15197. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15198. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  15199. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15200. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15201. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15202. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  15203. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15204. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  15205. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15206. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  15207. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15208. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15209. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15210. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15211. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15212. /* Valid cases - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15213. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15214. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15215. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15216. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15217. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15218. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15219. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15220. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15221. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15222. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15223. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15224. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15225. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15226. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15227. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15228. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15229. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15230. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15231. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15232. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15233. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15234. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15235. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  15236. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15237. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15238. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  15239. /* Check vald values of nonce - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15240. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15241. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15242. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15243. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15244. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15245. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15246. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15247. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15248. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15249. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  15250. SM4_GCM_AUTH_E);
  15251. /* Check valid values of tag size - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15252. for (i = WOLFSSL_MIN_AUTH_TAG_SZ; i < SM4_BLOCK_SIZE; i++) {
  15253. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15254. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  15255. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15256. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  15257. }
  15258. /* Check different in/out sizes. */
  15259. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce,
  15260. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15261. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce,
  15262. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15263. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce,
  15264. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15265. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  15266. XMEMCPY(out2, out, i - 1);
  15267. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, i, nonce, GCM_NONCE_MID_SZ,
  15268. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15269. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  15270. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, dec, out, i, nonce, GCM_NONCE_MID_SZ,
  15271. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15272. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  15273. }
  15274. /* Force the counter to roll over in first byte. */
  15275. {
  15276. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  15277. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  15278. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  15279. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15280. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  15281. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15282. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  15283. }
  15284. wc_Sm4Free(&sm4);
  15285. res = EXPECT_RESULT();
  15286. #endif
  15287. return res;
  15288. } /* END test_wc_Sm4Gcm */
  15289. /*
  15290. * Testing stream SM4-CCM API.
  15291. */
  15292. static int test_wc_Sm4Ccm(void)
  15293. {
  15294. int res = TEST_SKIPPED;
  15295. #ifdef WOLFSSL_SM4_CCM
  15296. EXPECT_DECLS;
  15297. wc_Sm4 sm4;
  15298. unsigned char key[SM4_KEY_SIZE];
  15299. unsigned char nonce[CCM_NONCE_MAX_SZ];
  15300. unsigned char in[SM4_BLOCK_SIZE * 2];
  15301. unsigned char in2[SM4_BLOCK_SIZE * 2];
  15302. unsigned char out[SM4_BLOCK_SIZE * 2];
  15303. unsigned char out2[SM4_BLOCK_SIZE * 2];
  15304. unsigned char dec[SM4_BLOCK_SIZE * 2];
  15305. unsigned char tag[SM4_BLOCK_SIZE];
  15306. unsigned char aad[SM4_BLOCK_SIZE * 2];
  15307. word32 i;
  15308. XMEMSET(key, 0, sizeof(key));
  15309. XMEMSET(nonce, 0, sizeof(nonce));
  15310. XMEMSET(in, 0, sizeof(in));
  15311. XMEMSET(in2, 0, sizeof(in2));
  15312. XMEMSET(aad, 0, sizeof(aad));
  15313. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15314. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  15315. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15316. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  15317. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15318. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15319. /* Invalid parameters - wc_Sm4CcmEncrypt. */
  15320. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15321. 0), BAD_FUNC_ARG);
  15322. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15323. 0), BAD_FUNC_ARG);
  15324. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15325. 0), BAD_FUNC_ARG);
  15326. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15327. 0), BAD_FUNC_ARG);
  15328. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15329. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15330. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15331. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15332. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15333. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15334. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15335. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15336. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15337. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15338. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  15339. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15340. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  15341. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15342. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15343. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15344. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15345. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15346. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15347. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15348. /* Invalid parameters - wc_Sm4CcmDecrypt. */
  15349. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15350. 0), BAD_FUNC_ARG);
  15351. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15352. 0), BAD_FUNC_ARG);
  15353. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15354. 0), BAD_FUNC_ARG);
  15355. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15356. 0), BAD_FUNC_ARG);
  15357. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15358. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15359. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15360. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15361. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15362. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15363. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15364. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15365. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15366. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15367. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  15368. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15369. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  15370. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15371. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15372. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15373. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15374. WOLFSSL_MIN_AUTH_TAG_SZ - 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15375. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15376. SM4_BLOCK_SIZE + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15377. /* Valid cases - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15378. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15379. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15380. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15381. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15382. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15383. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15384. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15385. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15386. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15387. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15388. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15389. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15390. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15391. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15392. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15393. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15394. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15395. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15396. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15397. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15398. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15399. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15400. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  15401. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15402. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15403. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  15404. /* Check vald values of nonce - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15405. for (i = CCM_NONCE_MIN_SZ; i <= CCM_NONCE_MAX_SZ; i++) {
  15406. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15407. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15408. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15409. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15410. }
  15411. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15412. CCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  15413. SM4_CCM_AUTH_E);
  15414. /* Check invalid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15415. for (i = 0; i < 4; i++) {
  15416. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15417. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15418. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15419. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15420. }
  15421. /* Odd values in range 4..SM4_BLOCK_SIZE. */
  15422. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  15423. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15424. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15425. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15426. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15427. }
  15428. /* Check valid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt.
  15429. * Even values in range 4..SM4_BLOCK_SIZE.
  15430. */
  15431. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  15432. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15433. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  15434. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15435. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  15436. }
  15437. /* Check different in/out sizes. */
  15438. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce,
  15439. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15440. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce,
  15441. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15442. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce,
  15443. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15444. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  15445. XMEMCPY(out2, out, i - 1);
  15446. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, i, nonce, CCM_NONCE_MAX_SZ,
  15447. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15448. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  15449. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, dec, out, i, nonce, CCM_NONCE_MAX_SZ,
  15450. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15451. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  15452. }
  15453. /* Force the counter to roll over in first byte. */
  15454. {
  15455. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  15456. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  15457. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  15458. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15459. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  15460. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15461. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  15462. }
  15463. wc_Sm4Free(&sm4);
  15464. res = EXPECT_RESULT();
  15465. #endif
  15466. return res;
  15467. } /* END test_wc_Sm4Ccm */
  15468. /*
  15469. * unit test for wc_Des3_SetIV()
  15470. */
  15471. static int test_wc_Des3_SetIV(void)
  15472. {
  15473. EXPECT_DECLS;
  15474. #ifndef NO_DES3
  15475. Des3 des;
  15476. const byte key[] = {
  15477. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15478. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15479. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15480. };
  15481. const byte iv[] = {
  15482. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15483. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15484. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15485. };
  15486. XMEMSET(&des, 0, sizeof(Des3));
  15487. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15488. /* DES_ENCRYPTION or DES_DECRYPTION */
  15489. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15490. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  15491. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  15492. /* Test explicitly wc_Des3_SetIV() */
  15493. ExpectIntEQ(wc_Des3_SetIV(NULL, iv), BAD_FUNC_ARG);
  15494. ExpectIntEQ(wc_Des3_SetIV(&des, NULL), 0);
  15495. #endif
  15496. wc_Des3Free(&des);
  15497. #endif
  15498. return EXPECT_RESULT();
  15499. } /* END test_wc_Des3_SetIV */
  15500. /*
  15501. * unit test for wc_Des3_SetKey()
  15502. */
  15503. static int test_wc_Des3_SetKey(void)
  15504. {
  15505. EXPECT_DECLS;
  15506. #ifndef NO_DES3
  15507. Des3 des;
  15508. const byte key[] = {
  15509. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15510. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15511. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15512. };
  15513. const byte iv[] = {
  15514. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15515. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15516. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15517. };
  15518. XMEMSET(&des, 0, sizeof(Des3));
  15519. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15520. /* DES_ENCRYPTION or DES_DECRYPTION */
  15521. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15522. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  15523. /* Test bad args. */
  15524. ExpectIntEQ(wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  15525. ExpectIntEQ(wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  15526. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, -1), BAD_FUNC_ARG);
  15527. /* Default case. Should return 0. */
  15528. ExpectIntEQ(wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION), 0);
  15529. wc_Des3Free(&des);
  15530. #endif
  15531. return EXPECT_RESULT();
  15532. } /* END test_wc_Des3_SetKey */
  15533. /*
  15534. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  15535. */
  15536. static int test_wc_Des3_CbcEncryptDecrypt(void)
  15537. {
  15538. EXPECT_DECLS;
  15539. #ifndef NO_DES3
  15540. Des3 des;
  15541. byte cipher[24];
  15542. byte plain[24];
  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. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, vector, 24), 0);
  15562. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION), 0);
  15563. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, cipher, 24), 0);
  15564. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  15565. /* Pass in bad args. */
  15566. ExpectIntEQ(wc_Des3_CbcEncrypt(NULL, cipher, vector, 24), BAD_FUNC_ARG);
  15567. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, NULL, vector, 24), BAD_FUNC_ARG);
  15568. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector)),
  15569. BAD_FUNC_ARG);
  15570. ExpectIntEQ(wc_Des3_CbcDecrypt(NULL, plain, cipher, 24), BAD_FUNC_ARG);
  15571. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, NULL, cipher, 24), BAD_FUNC_ARG);
  15572. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, NULL, 24), BAD_FUNC_ARG);
  15573. wc_Des3Free(&des);
  15574. #endif
  15575. return EXPECT_RESULT();
  15576. } /* END wc_Des3_CbcEncrypt */
  15577. /*
  15578. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  15579. */
  15580. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  15581. {
  15582. EXPECT_DECLS;
  15583. #ifndef NO_DES3
  15584. word32 vectorSz, cipherSz;
  15585. byte cipher[24];
  15586. byte plain[24];
  15587. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15588. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15589. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15590. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15591. };
  15592. byte key[] = {
  15593. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15594. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15595. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15596. };
  15597. byte iv[] = {
  15598. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15599. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15600. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15601. };
  15602. vectorSz = sizeof(byte) * 24;
  15603. cipherSz = sizeof(byte) * 24;
  15604. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv),
  15605. 0);
  15606. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv), 0);
  15607. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  15608. /* pass in bad args. */
  15609. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv),
  15610. BAD_FUNC_ARG);
  15611. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv),
  15612. BAD_FUNC_ARG);
  15613. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv),
  15614. BAD_FUNC_ARG);
  15615. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, NULL),
  15616. 0);
  15617. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv),
  15618. BAD_FUNC_ARG);
  15619. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv),
  15620. BAD_FUNC_ARG);
  15621. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv),
  15622. BAD_FUNC_ARG);
  15623. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL),
  15624. 0);
  15625. #endif
  15626. return EXPECT_RESULT();
  15627. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  15628. /*
  15629. * Unit test for wc_Des3_EcbEncrypt
  15630. */
  15631. static int test_wc_Des3_EcbEncrypt(void)
  15632. {
  15633. EXPECT_DECLS;
  15634. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  15635. Des3 des;
  15636. byte cipher[24];
  15637. word32 cipherSz = sizeof(cipher);
  15638. const byte key[] = {
  15639. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15640. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15641. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15642. };
  15643. const byte iv[] = {
  15644. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15645. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15646. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15647. };
  15648. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  15649. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15650. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15651. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15652. };
  15653. XMEMSET(&des, 0, sizeof(Des3));
  15654. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15655. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15656. /* Bad Cases */
  15657. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15658. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz),
  15659. BAD_FUNC_ARG);
  15660. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz), BAD_FUNC_ARG);
  15661. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz), BAD_FUNC_ARG);
  15662. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, 0), 0);
  15663. /* Good Cases */
  15664. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz), 0);
  15665. wc_Des3Free(&des);
  15666. #endif
  15667. return EXPECT_RESULT();
  15668. } /* END test_wc_Des3_EcbEncrypt */
  15669. /*
  15670. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  15671. */
  15672. static int test_wc_Chacha_SetKey(void)
  15673. {
  15674. EXPECT_DECLS;
  15675. #ifdef HAVE_CHACHA
  15676. ChaCha ctx;
  15677. const byte key[] = {
  15678. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15679. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15680. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15681. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15682. };
  15683. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  15684. byte cipher[128];
  15685. XMEMSET(cipher, 0, sizeof(cipher));
  15686. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, keySz), 0);
  15687. /* Test bad args. */
  15688. ExpectIntEQ(wc_Chacha_SetKey(NULL, key, keySz), BAD_FUNC_ARG);
  15689. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  15690. ExpectIntEQ(wc_Chacha_SetIV(&ctx, cipher, 0), 0);
  15691. /* Test bad args. */
  15692. ExpectIntEQ(wc_Chacha_SetIV(NULL, cipher, 0), BAD_FUNC_ARG);
  15693. #endif
  15694. return EXPECT_RESULT();
  15695. } /* END test_wc_Chacha_SetKey */
  15696. /*
  15697. * unit test for wc_Poly1305SetKey()
  15698. */
  15699. static int test_wc_Poly1305SetKey(void)
  15700. {
  15701. EXPECT_DECLS;
  15702. #ifdef HAVE_POLY1305
  15703. Poly1305 ctx;
  15704. const byte key[] =
  15705. {
  15706. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15707. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15708. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15709. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15710. };
  15711. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  15712. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, keySz), 0);
  15713. /* Test bad args. */
  15714. ExpectIntEQ(wc_Poly1305SetKey(NULL, key,keySz), BAD_FUNC_ARG);
  15715. ExpectIntEQ(wc_Poly1305SetKey(&ctx, NULL, keySz), BAD_FUNC_ARG);
  15716. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  15717. #endif
  15718. return EXPECT_RESULT();
  15719. } /* END test_wc_Poly1305_SetKey() */
  15720. /*
  15721. * Testing wc_Chacha_Process()
  15722. */
  15723. static int test_wc_Chacha_Process(void)
  15724. {
  15725. EXPECT_DECLS;
  15726. #ifdef HAVE_CHACHA
  15727. ChaCha enc, dec;
  15728. byte cipher[128];
  15729. byte plain[128];
  15730. const byte key[] =
  15731. {
  15732. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15733. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15734. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15735. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15736. };
  15737. const char* input = "Everybody gets Friday off.";
  15738. word32 keySz = sizeof(key)/sizeof(byte);
  15739. unsigned long int inlen = XSTRLEN(input);
  15740. /* Initialize stack variables. */
  15741. XMEMSET(cipher, 0, 128);
  15742. XMEMSET(plain, 0, 128);
  15743. ExpectIntEQ(wc_Chacha_SetKey(&enc, key, keySz), 0);
  15744. ExpectIntEQ(wc_Chacha_SetKey(&dec, key, keySz), 0);
  15745. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  15746. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  15747. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen),
  15748. 0);
  15749. ExpectIntEQ(wc_Chacha_Process(&dec, plain, cipher, (word32)inlen), 0);
  15750. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  15751. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  15752. /* test checking and using leftovers, currently just in C code */
  15753. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  15754. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  15755. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input,
  15756. (word32)inlen - 2), 0);
  15757. ExpectIntEQ(wc_Chacha_Process(&enc, cipher + (inlen - 2),
  15758. (byte*)input + (inlen - 2), 2), 0);
  15759. ExpectIntEQ(wc_Chacha_Process(&dec, plain, (byte*)cipher,
  15760. (word32)inlen - 2), 0);
  15761. ExpectIntEQ(wc_Chacha_Process(&dec, cipher + (inlen - 2),
  15762. (byte*)input + (inlen - 2), 2), 0);
  15763. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  15764. /* check edge cases with counter increment */
  15765. {
  15766. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  15767. const byte expected[] = {
  15768. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  15769. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  15770. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  15771. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  15772. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  15773. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  15774. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  15775. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  15776. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  15777. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  15778. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  15779. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  15780. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  15781. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  15782. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  15783. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  15784. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  15785. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  15786. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  15787. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  15788. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  15789. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  15790. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  15791. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  15792. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  15793. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  15794. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  15795. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  15796. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  15797. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  15798. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  15799. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  15800. };
  15801. const byte iv2[] = {
  15802. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  15803. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  15804. };
  15805. byte input2[256];
  15806. int i;
  15807. for (i = 0; i < 256; i++)
  15808. input2[i] = i;
  15809. ExpectIntEQ(wc_Chacha_SetIV(&enc, iv2, 0), 0);
  15810. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2, 64), 0);
  15811. ExpectIntEQ(XMEMCMP(expected, cipher, 64), 0);
  15812. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 64, 128), 0);
  15813. ExpectIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  15814. /* partial */
  15815. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 192, 32), 0);
  15816. ExpectIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  15817. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 224, 32), 0);
  15818. ExpectIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  15819. }
  15820. #endif
  15821. /* Test bad args. */
  15822. ExpectIntEQ(wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen),
  15823. BAD_FUNC_ARG);
  15824. #endif
  15825. return EXPECT_RESULT();
  15826. } /* END test_wc_Chacha_Process */
  15827. /*
  15828. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  15829. */
  15830. static int test_wc_ChaCha20Poly1305_aead(void)
  15831. {
  15832. EXPECT_DECLS;
  15833. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  15834. const byte key[] = {
  15835. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  15836. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  15837. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  15838. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  15839. };
  15840. const byte plaintext[] = {
  15841. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  15842. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  15843. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  15844. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  15845. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  15846. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  15847. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  15848. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  15849. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  15850. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  15851. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  15852. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  15853. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  15854. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  15855. 0x74, 0x2e
  15856. };
  15857. const byte iv[] = {
  15858. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  15859. 0x44, 0x45, 0x46, 0x47
  15860. };
  15861. const byte aad[] = { /* additional data */
  15862. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  15863. 0xc4, 0xc5, 0xc6, 0xc7
  15864. };
  15865. const byte cipher[] = { /* expected output from operation */
  15866. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  15867. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  15868. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  15869. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  15870. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  15871. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  15872. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  15873. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  15874. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  15875. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  15876. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  15877. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  15878. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  15879. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  15880. 0x61, 0x16
  15881. };
  15882. const byte authTag[] = { /* expected output from operation */
  15883. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  15884. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  15885. };
  15886. byte generatedCiphertext[272];
  15887. byte generatedPlaintext[272];
  15888. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  15889. /* Initialize stack variables. */
  15890. XMEMSET(generatedCiphertext, 0, 272);
  15891. XMEMSET(generatedPlaintext, 0, 272);
  15892. /* Test Encrypt */
  15893. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15894. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15895. 0);
  15896. ExpectIntEQ(XMEMCMP(generatedCiphertext, cipher,
  15897. sizeof(cipher)/sizeof(byte)), 0);
  15898. /* Test bad args. */
  15899. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad),
  15900. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15901. BAD_FUNC_ARG);
  15902. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  15903. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15904. BAD_FUNC_ARG);
  15905. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  15906. sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15907. BAD_FUNC_ARG);
  15908. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15909. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15910. BAD_FUNC_ARG);
  15911. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15912. plaintext, sizeof(plaintext), NULL, generatedAuthTag), BAD_FUNC_ARG);
  15913. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15914. plaintext, sizeof(plaintext), generatedCiphertext, NULL), BAD_FUNC_ARG);
  15915. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15916. sizeof(cipher), authTag, generatedPlaintext), 0);
  15917. ExpectIntEQ(XMEMCMP(generatedPlaintext, plaintext,
  15918. sizeof(plaintext)/sizeof(byte)), 0);
  15919. /* Test bad args. */
  15920. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  15921. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15922. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  15923. cipher, sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15924. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15925. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15926. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15927. sizeof(cipher), NULL, generatedPlaintext), BAD_FUNC_ARG);
  15928. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15929. sizeof(cipher), authTag, NULL), BAD_FUNC_ARG);
  15930. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15931. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15932. #endif
  15933. return EXPECT_RESULT();
  15934. } /* END test_wc_ChaCha20Poly1305_aead */
  15935. /*
  15936. * Testing function for wc_Rc2SetKey().
  15937. */
  15938. static int test_wc_Rc2SetKey(void)
  15939. {
  15940. EXPECT_DECLS;
  15941. #ifdef WC_RC2
  15942. Rc2 rc2;
  15943. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  15944. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15945. /* valid key and IV */
  15946. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15947. iv, 40), 0);
  15948. /* valid key, no IV */
  15949. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15950. NULL, 40), 0);
  15951. /* bad arguments */
  15952. /* null Rc2 struct */
  15953. ExpectIntEQ(wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  15954. iv, 40), BAD_FUNC_ARG);
  15955. /* null key */
  15956. ExpectIntEQ(wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  15957. iv, 40), BAD_FUNC_ARG);
  15958. /* key size == 0 */
  15959. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 0, iv, 40), WC_KEY_SIZE_E);
  15960. /* key size > 128 */
  15961. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 129, iv, 40), WC_KEY_SIZE_E);
  15962. /* effective bits == 0 */
  15963. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15964. iv, 0), WC_KEY_SIZE_E);
  15965. /* effective bits > 1024 */
  15966. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15967. iv, 1025), WC_KEY_SIZE_E);
  15968. #endif
  15969. return EXPECT_RESULT();
  15970. } /* END test_wc_Rc2SetKey */
  15971. /*
  15972. * Testing function for wc_Rc2SetIV().
  15973. */
  15974. static int test_wc_Rc2SetIV(void)
  15975. {
  15976. EXPECT_DECLS;
  15977. #ifdef WC_RC2
  15978. Rc2 rc2;
  15979. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15980. /* valid IV */
  15981. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  15982. /* valid NULL IV */
  15983. ExpectIntEQ(wc_Rc2SetIV(&rc2, NULL), 0);
  15984. /* bad arguments */
  15985. ExpectIntEQ(wc_Rc2SetIV(NULL, iv), BAD_FUNC_ARG);
  15986. ExpectIntEQ(wc_Rc2SetIV(NULL, NULL), BAD_FUNC_ARG);
  15987. #endif
  15988. return EXPECT_RESULT();
  15989. } /* END test_wc_Rc2SetIV */
  15990. /*
  15991. * Testing function for wc_Rc2EcbEncrypt() and wc_Rc2EcbDecrypt().
  15992. */
  15993. static int test_wc_Rc2EcbEncryptDecrypt(void)
  15994. {
  15995. EXPECT_DECLS;
  15996. #ifdef WC_RC2
  15997. Rc2 rc2;
  15998. int effectiveKeyBits = 63;
  15999. byte cipher[RC2_BLOCK_SIZE];
  16000. byte plain[RC2_BLOCK_SIZE];
  16001. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  16002. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  16003. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  16004. XMEMSET(cipher, 0, sizeof(cipher));
  16005. XMEMSET(plain, 0, sizeof(plain));
  16006. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  16007. NULL, effectiveKeyBits), 0);
  16008. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE), 0);
  16009. ExpectIntEQ(XMEMCMP(cipher, output, RC2_BLOCK_SIZE), 0);
  16010. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE), 0);
  16011. ExpectIntEQ(XMEMCMP(plain, input, RC2_BLOCK_SIZE), 0);
  16012. /* Rc2EcbEncrypt bad arguments */
  16013. /* null Rc2 struct */
  16014. ExpectIntEQ(wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE),
  16015. BAD_FUNC_ARG);
  16016. /* null out buffer */
  16017. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE),
  16018. BAD_FUNC_ARG);
  16019. /* null input buffer */
  16020. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE),
  16021. BAD_FUNC_ARG);
  16022. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  16023. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, 7), BUFFER_E);
  16024. /* Rc2EcbDecrypt bad arguments */
  16025. /* null Rc2 struct */
  16026. ExpectIntEQ(wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE),
  16027. BAD_FUNC_ARG);
  16028. /* null out buffer */
  16029. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE),
  16030. BAD_FUNC_ARG);
  16031. /* null input buffer */
  16032. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE),
  16033. BAD_FUNC_ARG);
  16034. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  16035. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, output, 7), BUFFER_E);
  16036. #endif
  16037. return EXPECT_RESULT();
  16038. } /* END test_wc_Rc2EcbEncryptDecrypt */
  16039. /*
  16040. * Testing function for wc_Rc2CbcEncrypt() and wc_Rc2CbcDecrypt().
  16041. */
  16042. static int test_wc_Rc2CbcEncryptDecrypt(void)
  16043. {
  16044. EXPECT_DECLS;
  16045. #ifdef WC_RC2
  16046. Rc2 rc2;
  16047. int effectiveKeyBits = 63;
  16048. byte cipher[RC2_BLOCK_SIZE*2];
  16049. byte plain[RC2_BLOCK_SIZE*2];
  16050. /* vector taken from test.c */
  16051. byte key[] = {
  16052. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16053. };
  16054. byte iv[] = {
  16055. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16056. };
  16057. byte input[] = {
  16058. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  16059. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16060. };
  16061. byte output[] = {
  16062. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  16063. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  16064. };
  16065. XMEMSET(cipher, 0, sizeof(cipher));
  16066. XMEMSET(plain, 0, sizeof(plain));
  16067. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  16068. iv, effectiveKeyBits), 0);
  16069. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input)), 0);
  16070. ExpectIntEQ(XMEMCMP(cipher, output, sizeof(output)), 0);
  16071. /* reset IV for decrypt */
  16072. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  16073. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher)), 0);
  16074. ExpectIntEQ(XMEMCMP(plain, input, sizeof(input)), 0);
  16075. /* Rc2CbcEncrypt bad arguments */
  16076. /* null Rc2 struct */
  16077. ExpectIntEQ(wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input)),
  16078. BAD_FUNC_ARG);
  16079. /* null out buffer */
  16080. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input)),
  16081. BAD_FUNC_ARG);
  16082. /* null input buffer */
  16083. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input)),
  16084. BAD_FUNC_ARG);
  16085. /* Rc2CbcDecrypt bad arguments */
  16086. /* in size is 0 */
  16087. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, output, 0), 0);
  16088. /* null Rc2 struct */
  16089. ExpectIntEQ(wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output)),
  16090. BAD_FUNC_ARG);
  16091. /* null out buffer */
  16092. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output)),
  16093. BAD_FUNC_ARG);
  16094. /* null input buffer */
  16095. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output)),
  16096. BAD_FUNC_ARG);
  16097. #endif
  16098. return EXPECT_RESULT();
  16099. } /* END test_wc_Rc2CbcEncryptDecrypt */
  16100. /*
  16101. * Testing function for wc_AesSetIV
  16102. */
  16103. static int test_wc_AesSetIV(void)
  16104. {
  16105. int res = TEST_SKIPPED;
  16106. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  16107. Aes aes;
  16108. int ret = 0;
  16109. byte key16[] =
  16110. {
  16111. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16112. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16113. };
  16114. byte iv1[] = "1234567890abcdef";
  16115. byte iv2[] = "0987654321fedcba";
  16116. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  16117. if (ret != 0)
  16118. return ret;
  16119. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  16120. iv1, AES_ENCRYPTION);
  16121. if (ret == 0) {
  16122. ret = wc_AesSetIV(&aes, iv2);
  16123. }
  16124. /* Test bad args. */
  16125. if (ret == 0) {
  16126. ret = wc_AesSetIV(NULL, iv1);
  16127. if (ret == BAD_FUNC_ARG) {
  16128. /* NULL iv should return 0. */
  16129. ret = wc_AesSetIV(&aes, NULL);
  16130. }
  16131. else {
  16132. ret = WOLFSSL_FATAL_ERROR;
  16133. }
  16134. }
  16135. wc_AesFree(&aes);
  16136. res = TEST_RES_CHECK(ret == 0);
  16137. #endif
  16138. return res;
  16139. } /* test_wc_AesSetIV */
  16140. /*
  16141. * Testing function for wc_AesSetKey().
  16142. */
  16143. static int test_wc_AesSetKey(void)
  16144. {
  16145. EXPECT_DECLS;
  16146. #ifndef NO_AES
  16147. Aes aes;
  16148. byte key16[] = {
  16149. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16150. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16151. };
  16152. #ifdef WOLFSSL_AES_192
  16153. byte key24[] = {
  16154. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16155. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16156. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16157. };
  16158. #endif
  16159. #ifdef WOLFSSL_AES_256
  16160. byte key32[] = {
  16161. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16162. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16163. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16164. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16165. };
  16166. #endif
  16167. byte badKey16[] = {
  16168. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16169. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16170. };
  16171. byte iv[] = "1234567890abcdef";
  16172. XMEMSET(&aes, 0, sizeof(Aes));
  16173. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16174. #ifdef WOLFSSL_AES_128
  16175. ExpectIntEQ(wc_AesSetKey(&aes, key16, (word32)sizeof(key16) / sizeof(byte),
  16176. iv, AES_ENCRYPTION), 0);
  16177. #endif
  16178. #ifdef WOLFSSL_AES_192
  16179. ExpectIntEQ(wc_AesSetKey(&aes, key24, (word32)sizeof(key24) / sizeof(byte),
  16180. iv, AES_ENCRYPTION), 0);
  16181. #endif
  16182. #ifdef WOLFSSL_AES_256
  16183. ExpectIntEQ(wc_AesSetKey(&aes, key32, (word32)sizeof(key32) / sizeof(byte),
  16184. iv, AES_ENCRYPTION), 0);
  16185. #endif
  16186. /* Pass in bad args. */
  16187. ExpectIntEQ(wc_AesSetKey(NULL, key16, (word32)sizeof(key16) / sizeof(byte),
  16188. iv, AES_ENCRYPTION), BAD_FUNC_ARG);
  16189. ExpectIntEQ(wc_AesSetKey(&aes, badKey16,
  16190. (word32)sizeof(badKey16) / sizeof(byte), iv, AES_ENCRYPTION),
  16191. BAD_FUNC_ARG);
  16192. wc_AesFree(&aes);
  16193. #endif
  16194. return EXPECT_RESULT();
  16195. } /* END test_wc_AesSetKey */
  16196. /*
  16197. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  16198. * and wc_AesCbcDecryptWithKey()
  16199. */
  16200. static int test_wc_AesCbcEncryptDecrypt(void)
  16201. {
  16202. EXPECT_DECLS;
  16203. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  16204. defined(WOLFSSL_AES_256)
  16205. Aes aes;
  16206. byte key32[] = {
  16207. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16208. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16209. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16210. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16211. };
  16212. byte vector[] = { /* Now is the time for all good men w/o trailing 0 */
  16213. 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
  16214. 0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20,
  16215. 0x66, 0x6f, 0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20,
  16216. 0x67, 0x6f, 0x6f, 0x64, 0x20, 0x6d, 0x65, 0x6e
  16217. };
  16218. byte iv[] = "1234567890abcdef";
  16219. byte enc[sizeof(vector)];
  16220. byte dec[sizeof(vector)];
  16221. byte dec2[sizeof(vector)];
  16222. /* Init stack variables. */
  16223. XMEMSET(&aes, 0, sizeof(Aes));
  16224. XMEMSET(enc, 0, sizeof(enc));
  16225. XMEMSET(dec, 0, sizeof(vector));
  16226. XMEMSET(dec2, 0, sizeof(vector));
  16227. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16228. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  16229. AES_ENCRYPTION), 0);
  16230. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector)), 0);
  16231. /* Re init for decrypt and set flag. */
  16232. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  16233. AES_DECRYPTION), 0);
  16234. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector)), 0);
  16235. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16236. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE, key32,
  16237. sizeof(key32)/sizeof(byte), iv), 0);
  16238. ExpectIntEQ(XMEMCMP(vector, dec2, AES_BLOCK_SIZE), 0);
  16239. /* Pass in bad args */
  16240. ExpectIntEQ(wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector)),
  16241. BAD_FUNC_ARG);
  16242. ExpectIntEQ(wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector)),
  16243. BAD_FUNC_ARG);
  16244. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector)),
  16245. BAD_FUNC_ARG);
  16246. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  16247. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1),
  16248. BAD_LENGTH_E);
  16249. #endif
  16250. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16251. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  16252. fprintf(stderr, "Zero length inputs not supported with AESNI in FIPS "
  16253. "mode (v2), skip test");
  16254. #else
  16255. /* Test passing in size of 0 */
  16256. XMEMSET(enc, 0, sizeof(enc));
  16257. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, 0), 0);
  16258. /* Check enc was not modified */
  16259. {
  16260. int i;
  16261. for (i = 0; i < (int)sizeof(enc); i++)
  16262. ExpectIntEQ(enc[i], 0);
  16263. }
  16264. #endif
  16265. ExpectIntEQ(wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE), BAD_FUNC_ARG);
  16266. ExpectIntEQ(wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE),
  16267. BAD_FUNC_ARG);
  16268. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE),
  16269. BAD_FUNC_ARG);
  16270. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  16271. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  16272. BAD_LENGTH_E);
  16273. #else
  16274. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  16275. BAD_FUNC_ARG);
  16276. #endif
  16277. /* Test passing in size of 0 */
  16278. XMEMSET(dec, 0, sizeof(dec));
  16279. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, 0), 0);
  16280. /* Check dec was not modified */
  16281. {
  16282. int i;
  16283. for (i = 0; i < (int)sizeof(dec); i++)
  16284. ExpectIntEQ(dec[i], 0);
  16285. }
  16286. ExpectIntEQ(wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  16287. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16288. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  16289. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16290. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  16291. NULL, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16292. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  16293. key32, sizeof(key32)/sizeof(byte), NULL), BAD_FUNC_ARG);
  16294. wc_AesFree(&aes);
  16295. #endif
  16296. return EXPECT_RESULT();
  16297. } /* END test_wc_AesCbcEncryptDecrypt */
  16298. /*
  16299. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  16300. */
  16301. static int test_wc_AesCtrEncryptDecrypt(void)
  16302. {
  16303. EXPECT_DECLS;
  16304. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  16305. Aes aesEnc;
  16306. Aes aesDec;
  16307. byte key32[] = {
  16308. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16309. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16310. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16311. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16312. };
  16313. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  16314. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16315. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16316. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16317. };
  16318. byte iv[] = "1234567890abcdef";
  16319. byte enc[AES_BLOCK_SIZE * 2];
  16320. byte dec[AES_BLOCK_SIZE * 2];
  16321. /* Init stack variables. */
  16322. XMEMSET(&aesEnc, 0, sizeof(Aes));
  16323. XMEMSET(&aesDec, 0, sizeof(Aes));
  16324. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  16325. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  16326. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  16327. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  16328. ExpectIntEQ(wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2, iv,
  16329. AES_ENCRYPTION), 0);
  16330. ExpectIntEQ(wc_AesCtrEncrypt(&aesEnc, enc, vector,
  16331. sizeof(vector)/sizeof(byte)), 0);
  16332. /* Decrypt with wc_AesCtrEncrypt() */
  16333. ExpectIntEQ(wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2, iv,
  16334. AES_ENCRYPTION), 0);
  16335. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte)),
  16336. 0);
  16337. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16338. /* Test bad args. */
  16339. ExpectIntEQ(wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte)),
  16340. BAD_FUNC_ARG);
  16341. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte)),
  16342. BAD_FUNC_ARG);
  16343. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte)),
  16344. BAD_FUNC_ARG);
  16345. wc_AesFree(&aesEnc);
  16346. wc_AesFree(&aesDec);
  16347. #endif
  16348. return EXPECT_RESULT();
  16349. } /* END test_wc_AesCtrEncryptDecrypt */
  16350. /*
  16351. * test function for wc_AesGcmSetKey()
  16352. */
  16353. static int test_wc_AesGcmSetKey(void)
  16354. {
  16355. EXPECT_DECLS;
  16356. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16357. Aes aes;
  16358. #ifdef WOLFSSL_AES_128
  16359. byte key16[] = {
  16360. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16361. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16362. };
  16363. #endif
  16364. #ifdef WOLFSSL_AES_192
  16365. byte key24[] = {
  16366. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16367. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16368. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16369. };
  16370. #endif
  16371. #ifdef WOLFSSL_AES_256
  16372. byte key32[] = {
  16373. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16374. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16375. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16376. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16377. };
  16378. #endif
  16379. byte badKey16[] = {
  16380. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16381. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16382. };
  16383. byte badKey24[] = {
  16384. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16385. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16386. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  16387. };
  16388. byte badKey32[] = {
  16389. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  16390. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16391. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16392. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16393. };
  16394. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16395. #ifdef WOLFSSL_AES_128
  16396. ExpectIntEQ(wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte)), 0);
  16397. #endif
  16398. #ifdef WOLFSSL_AES_192
  16399. ExpectIntEQ(wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte)), 0);
  16400. #endif
  16401. #ifdef WOLFSSL_AES_256
  16402. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  16403. #endif
  16404. /* Pass in bad args. */
  16405. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte)),
  16406. BAD_FUNC_ARG);
  16407. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte)),
  16408. BAD_FUNC_ARG);
  16409. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte)),
  16410. BAD_FUNC_ARG);
  16411. wc_AesFree(&aes);
  16412. #endif
  16413. return EXPECT_RESULT();
  16414. } /* END test_wc_AesGcmSetKey */
  16415. /*
  16416. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  16417. */
  16418. static int test_wc_AesGcmEncryptDecrypt(void)
  16419. {
  16420. EXPECT_DECLS;
  16421. /* WOLFSSL_AFALG requires 12 byte IV */
  16422. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  16423. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  16424. Aes aes;
  16425. byte key32[] = {
  16426. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16427. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16428. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16429. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16430. };
  16431. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  16432. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16433. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16434. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16435. };
  16436. const byte a[] = {
  16437. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16438. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16439. 0xab, 0xad, 0xda, 0xd2
  16440. };
  16441. byte iv[] = "1234567890a";
  16442. byte longIV[] = "1234567890abcdefghij";
  16443. byte enc[sizeof(vector)];
  16444. byte resultT[AES_BLOCK_SIZE];
  16445. byte dec[sizeof(vector)];
  16446. /* Init stack variables. */
  16447. XMEMSET(&aes, 0, sizeof(Aes));
  16448. XMEMSET(enc, 0, sizeof(vector));
  16449. XMEMSET(dec, 0, sizeof(vector));
  16450. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  16451. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16452. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  16453. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16454. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  16455. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector), iv,
  16456. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  16457. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16458. /* Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  16459. ExpectIntEQ(wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector), iv,
  16460. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16461. BAD_FUNC_ARG);
  16462. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16463. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16464. BAD_FUNC_ARG);
  16465. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16466. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) - 5, a, sizeof(a)),
  16467. BAD_FUNC_ARG);
  16468. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16469. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  16470. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16471. /* FIPS does not check the lower bound of ivSz */
  16472. #else
  16473. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv, 0,
  16474. resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  16475. #endif
  16476. /* This case is now considered good. Long IVs are now allowed.
  16477. * Except for the original FIPS release, it still has an upper
  16478. * bound on the IV length. */
  16479. #if (!defined(HAVE_FIPS) || \
  16480. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16481. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16482. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  16483. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16484. 0);
  16485. #else
  16486. (void)longIV;
  16487. #endif /* Old FIPS */
  16488. /* END wc_AesGcmEncrypt */
  16489. #ifdef HAVE_AES_DECRYPT
  16490. ExpectIntEQ(wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16491. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16492. BAD_FUNC_ARG);
  16493. ExpectIntEQ(wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte), iv,
  16494. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16495. BAD_FUNC_ARG);
  16496. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte), iv,
  16497. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16498. BAD_FUNC_ARG);
  16499. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), NULL,
  16500. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16501. BAD_FUNC_ARG);
  16502. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16503. sizeof(iv)/sizeof(byte), NULL, sizeof(resultT), a, sizeof(a)),
  16504. BAD_FUNC_ARG);
  16505. #if (defined(HAVE_FIPS) && FIPS_VERSION_LE(2,0) && defined(WOLFSSL_ARMASM))
  16506. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16507. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16508. AES_GCM_AUTH_E);
  16509. #else
  16510. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16511. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16512. BAD_FUNC_ARG);
  16513. #endif
  16514. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16515. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  16516. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16517. /* FIPS does not check the lower bound of ivSz */
  16518. #else
  16519. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  16520. iv, 0, resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  16521. #endif
  16522. #endif /* HAVE_AES_DECRYPT */
  16523. wc_AesFree(&aes);
  16524. #endif
  16525. return EXPECT_RESULT();
  16526. } /* END test_wc_AesGcmEncryptDecrypt */
  16527. /*
  16528. * test function for mixed (one-shot encrpytion + stream decryption) AES GCM
  16529. * using a long IV (older FIPS does NOT support long IVs). Relates to zd15423
  16530. */
  16531. static int test_wc_AesGcmMixedEncDecLongIV(void)
  16532. {
  16533. EXPECT_DECLS;
  16534. #if (!defined(HAVE_FIPS) || \
  16535. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16536. !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AESGCM_STREAM)
  16537. const byte key[] = {
  16538. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16539. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16540. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16541. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16542. };
  16543. const byte in[] = {
  16544. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16545. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16546. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16547. };
  16548. const byte aad[] = {
  16549. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16550. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16551. 0xab, 0xad, 0xda, 0xd2
  16552. };
  16553. Aes aesEnc;
  16554. Aes aesDec;
  16555. byte iv[] = "1234567890abcdefghij";
  16556. byte out[sizeof(in)];
  16557. byte plain[sizeof(in)];
  16558. byte tag[AES_BLOCK_SIZE];
  16559. XMEMSET(&aesEnc, 0, sizeof(Aes));
  16560. XMEMSET(&aesDec, 0, sizeof(Aes));
  16561. XMEMSET(out, 0, sizeof(out));
  16562. XMEMSET(plain, 0, sizeof(plain));
  16563. XMEMSET(tag, 0, sizeof(tag));
  16564. /* Perform one-shot encryption using long IV */
  16565. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  16566. ExpectIntEQ(wc_AesGcmSetKey(&aesEnc, key, sizeof(key)), 0);
  16567. ExpectIntEQ(wc_AesGcmEncrypt(&aesEnc, out, in, sizeof(in), iv, sizeof(iv),
  16568. tag, sizeof(tag), aad, sizeof(aad)), 0);
  16569. /* Perform streaming decryption using long IV */
  16570. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  16571. ExpectIntEQ(wc_AesGcmInit(&aesDec, key, sizeof(key), iv, sizeof(iv)), 0);
  16572. ExpectIntEQ(wc_AesGcmDecryptUpdate(&aesDec, plain, out, sizeof(out), aad,
  16573. sizeof(aad)), 0);
  16574. ExpectIntEQ(wc_AesGcmDecryptFinal(&aesDec, tag, sizeof(tag)), 0);
  16575. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  16576. /* Free resources */
  16577. wc_AesFree(&aesEnc);
  16578. wc_AesFree(&aesDec);
  16579. #endif
  16580. return EXPECT_RESULT();
  16581. } /* END wc_AesGcmMixedEncDecLongIV */
  16582. /*
  16583. * unit test for wc_GmacSetKey()
  16584. */
  16585. static int test_wc_GmacSetKey(void)
  16586. {
  16587. EXPECT_DECLS;
  16588. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16589. Gmac gmac;
  16590. byte key16[] = {
  16591. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16592. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16593. };
  16594. #ifdef WOLFSSL_AES_192
  16595. byte key24[] = {
  16596. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16597. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16598. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16599. };
  16600. #endif
  16601. #ifdef WOLFSSL_AES_256
  16602. byte key32[] = {
  16603. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16604. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16605. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16606. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16607. };
  16608. #endif
  16609. byte badKey16[] = {
  16610. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16611. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  16612. };
  16613. byte badKey24[] = {
  16614. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  16615. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16616. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16617. };
  16618. byte badKey32[] = {
  16619. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16620. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  16621. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16622. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16623. };
  16624. XMEMSET(&gmac, 0, sizeof(Gmac));
  16625. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16626. #ifdef WOLFSSL_AES_128
  16627. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte)), 0);
  16628. #endif
  16629. #ifdef WOLFSSL_AES_192
  16630. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  16631. #endif
  16632. #ifdef WOLFSSL_AES_256
  16633. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  16634. #endif
  16635. /* Pass in bad args. */
  16636. ExpectIntEQ(wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte)),
  16637. BAD_FUNC_ARG);
  16638. ExpectIntEQ(wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte)),
  16639. BAD_FUNC_ARG);
  16640. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte)),
  16641. BAD_FUNC_ARG);
  16642. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte)),
  16643. BAD_FUNC_ARG);
  16644. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte)),
  16645. BAD_FUNC_ARG);
  16646. wc_AesFree(&gmac.aes);
  16647. #endif
  16648. return EXPECT_RESULT();
  16649. } /* END test_wc_GmacSetKey */
  16650. /*
  16651. * unit test for wc_GmacUpdate
  16652. */
  16653. static int test_wc_GmacUpdate(void)
  16654. {
  16655. EXPECT_DECLS;
  16656. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16657. Gmac gmac;
  16658. #ifdef WOLFSSL_AES_128
  16659. const byte key16[] = {
  16660. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  16661. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  16662. };
  16663. #endif
  16664. #ifdef WOLFSSL_AES_192
  16665. byte key24[] = {
  16666. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  16667. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  16668. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  16669. };
  16670. #endif
  16671. #ifdef WOLFSSL_AES_256
  16672. byte key32[] = {
  16673. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  16674. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  16675. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  16676. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  16677. };
  16678. #endif
  16679. #ifdef WOLFSSL_AES_128
  16680. const byte authIn[] = {
  16681. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  16682. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  16683. };
  16684. #endif
  16685. #ifdef WOLFSSL_AES_192
  16686. const byte authIn2[] = {
  16687. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  16688. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  16689. };
  16690. #endif
  16691. const byte authIn3[] = {
  16692. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  16693. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  16694. };
  16695. #ifdef WOLFSSL_AES_128
  16696. const byte tag1[] = { /* Known. */
  16697. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  16698. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  16699. };
  16700. #endif
  16701. #ifdef WOLFSSL_AES_192
  16702. const byte tag2[] = { /* Known */
  16703. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  16704. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  16705. };
  16706. #endif
  16707. const byte tag3[] = { /* Known */
  16708. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  16709. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  16710. };
  16711. #ifdef WOLFSSL_AES_128
  16712. const byte iv[] = {
  16713. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  16714. 0xe2, 0x8c, 0x8f, 0x16
  16715. };
  16716. #endif
  16717. #ifdef WOLFSSL_AES_192
  16718. const byte iv2[] = {
  16719. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  16720. 0x7e, 0x1a, 0x6f, 0xbc
  16721. };
  16722. #endif
  16723. const byte iv3[] = {
  16724. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  16725. 0xc3, 0xfb, 0x6c, 0x8a
  16726. };
  16727. byte tagOut[16];
  16728. byte tagOut2[24];
  16729. byte tagOut3[32];
  16730. /* Init stack variables. */
  16731. XMEMSET(&gmac, 0, sizeof(Gmac));
  16732. XMEMSET(tagOut, 0, sizeof(tagOut));
  16733. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  16734. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  16735. #ifdef WOLFSSL_AES_128
  16736. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16737. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)), 0);
  16738. ExpectIntEQ(wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  16739. tagOut, sizeof(tag1)), 0);
  16740. ExpectIntEQ(XMEMCMP(tag1, tagOut, sizeof(tag1)), 0);
  16741. wc_AesFree(&gmac.aes);
  16742. #endif
  16743. #ifdef WOLFSSL_AES_192
  16744. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  16745. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  16746. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  16747. ExpectIntEQ(wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2, sizeof(authIn2),
  16748. tagOut2, sizeof(tag2)), 0);
  16749. ExpectIntEQ(XMEMCMP(tagOut2, tag2, sizeof(tag2)), 0);
  16750. wc_AesFree(&gmac.aes);
  16751. #endif
  16752. #ifdef WOLFSSL_AES_256
  16753. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  16754. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  16755. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  16756. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16757. tagOut3, sizeof(tag3)), 0);
  16758. ExpectIntEQ(XMEMCMP(tag3, tagOut3, sizeof(tag3)), 0);
  16759. wc_AesFree(&gmac.aes);
  16760. #endif
  16761. /* Pass bad args. */
  16762. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16763. ExpectIntEQ(wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16764. tagOut3, sizeof(tag3)), BAD_FUNC_ARG);
  16765. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16766. tagOut3, sizeof(tag3) - 5), BAD_FUNC_ARG);
  16767. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16768. tagOut3, sizeof(tag3) + 1), BAD_FUNC_ARG);
  16769. wc_AesFree(&gmac.aes);
  16770. #endif
  16771. return EXPECT_RESULT();
  16772. } /* END test_wc_GmacUpdate */
  16773. /*
  16774. * testing wc_CamelliaSetKey
  16775. */
  16776. static int test_wc_CamelliaSetKey(void)
  16777. {
  16778. EXPECT_DECLS;
  16779. #ifdef HAVE_CAMELLIA
  16780. Camellia camellia;
  16781. /*128-bit key*/
  16782. static const byte key16[] = {
  16783. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16784. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  16785. };
  16786. /* 192-bit key */
  16787. static const byte key24[] = {
  16788. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16789. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16790. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16791. };
  16792. /* 256-bit key */
  16793. static const byte key32[] = {
  16794. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16795. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16796. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  16797. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  16798. };
  16799. static const byte iv[] = {
  16800. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16801. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16802. };
  16803. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv),
  16804. 0);
  16805. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16),
  16806. NULL), 0);
  16807. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  16808. 0);
  16809. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16810. NULL), 0);
  16811. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32), iv),
  16812. 0);
  16813. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32),
  16814. NULL), 0);
  16815. /* Bad args. */
  16816. ExpectIntEQ(wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv),
  16817. BAD_FUNC_ARG);
  16818. #endif
  16819. return EXPECT_RESULT();
  16820. } /* END test_wc_CammeliaSetKey */
  16821. /*
  16822. * Testing wc_CamelliaSetIV()
  16823. */
  16824. static int test_wc_CamelliaSetIV(void)
  16825. {
  16826. EXPECT_DECLS;
  16827. #ifdef HAVE_CAMELLIA
  16828. Camellia camellia;
  16829. static const byte iv[] = {
  16830. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16831. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16832. };
  16833. ExpectIntEQ(wc_CamelliaSetIV(&camellia, iv), 0);
  16834. ExpectIntEQ(wc_CamelliaSetIV(&camellia, NULL), 0);
  16835. /* Bad args. */
  16836. ExpectIntEQ(wc_CamelliaSetIV(NULL, NULL), BAD_FUNC_ARG);
  16837. ExpectIntEQ(wc_CamelliaSetIV(NULL, iv), BAD_FUNC_ARG);
  16838. #endif
  16839. return EXPECT_RESULT();
  16840. } /* END test_wc_CamelliaSetIV*/
  16841. /*
  16842. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  16843. */
  16844. static int test_wc_CamelliaEncryptDecryptDirect(void)
  16845. {
  16846. EXPECT_DECLS;
  16847. #ifdef HAVE_CAMELLIA
  16848. Camellia camellia;
  16849. static const byte key24[] = {
  16850. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16851. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16852. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16853. };
  16854. static const byte iv[] = {
  16855. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16856. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16857. };
  16858. static const byte plainT[] = {
  16859. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16860. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16861. };
  16862. byte enc[sizeof(plainT)];
  16863. byte dec[sizeof(enc)];
  16864. /* Init stack variables.*/
  16865. XMEMSET(enc, 0, 16);
  16866. XMEMSET(enc, 0, 16);
  16867. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  16868. 0);
  16869. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, plainT), 0);
  16870. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, enc), 0);
  16871. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  16872. /* Pass bad args. */
  16873. ExpectIntEQ(wc_CamelliaEncryptDirect(NULL, enc, plainT), BAD_FUNC_ARG);
  16874. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, NULL, plainT),
  16875. BAD_FUNC_ARG);
  16876. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, NULL), BAD_FUNC_ARG);
  16877. ExpectIntEQ(wc_CamelliaDecryptDirect(NULL, dec, enc), BAD_FUNC_ARG);
  16878. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, NULL, enc), BAD_FUNC_ARG);
  16879. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, NULL), BAD_FUNC_ARG);
  16880. #endif
  16881. return EXPECT_RESULT();
  16882. } /* END test-wc_CamelliaEncryptDecryptDirect */
  16883. /*
  16884. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  16885. */
  16886. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  16887. {
  16888. EXPECT_DECLS;
  16889. #ifdef HAVE_CAMELLIA
  16890. Camellia camellia;
  16891. static const byte key24[] = {
  16892. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16893. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16894. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16895. };
  16896. static const byte plainT[] = {
  16897. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16898. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16899. };
  16900. byte enc[CAMELLIA_BLOCK_SIZE];
  16901. byte dec[CAMELLIA_BLOCK_SIZE];
  16902. /* Init stack variables. */
  16903. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16904. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16905. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16906. NULL), 0);
  16907. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, plainT,
  16908. CAMELLIA_BLOCK_SIZE), 0);
  16909. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16910. NULL), 0);
  16911. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE),
  16912. 0);
  16913. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  16914. /* Pass in bad args. */
  16915. ExpectIntEQ(wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE),
  16916. BAD_FUNC_ARG);
  16917. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  16918. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16919. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  16920. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16921. ExpectIntEQ(wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE),
  16922. BAD_FUNC_ARG);
  16923. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  16924. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16925. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  16926. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16927. #endif
  16928. return EXPECT_RESULT();
  16929. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  16930. /*
  16931. * Testing wc_Arc4SetKey()
  16932. */
  16933. static int test_wc_Arc4SetKey(void)
  16934. {
  16935. EXPECT_DECLS;
  16936. #ifndef NO_RC4
  16937. Arc4 arc;
  16938. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16939. int keyLen = 8;
  16940. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, keyLen), 0);
  16941. /* Test bad args. */
  16942. ExpectIntEQ(wc_Arc4SetKey(NULL, (byte*)key, keyLen), BAD_FUNC_ARG);
  16943. ExpectIntEQ(wc_Arc4SetKey(&arc, NULL , keyLen), BAD_FUNC_ARG);
  16944. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, 0 ), BAD_FUNC_ARG);
  16945. #endif
  16946. return EXPECT_RESULT();
  16947. } /* END test_wc_Arc4SetKey */
  16948. /*
  16949. * Testing wc_Arc4Process for ENC/DEC.
  16950. */
  16951. static int test_wc_Arc4Process(void)
  16952. {
  16953. EXPECT_DECLS;
  16954. #ifndef NO_RC4
  16955. Arc4 enc;
  16956. Arc4 dec;
  16957. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16958. int keyLen = 8;
  16959. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16960. byte cipher[8];
  16961. byte plain[8];
  16962. /* Init stack variables */
  16963. XMEMSET(&enc, 0, sizeof(Arc4));
  16964. XMEMSET(&dec, 0, sizeof(Arc4));
  16965. XMEMSET(cipher, 0, sizeof(cipher));
  16966. XMEMSET(plain, 0, sizeof(plain));
  16967. /* Use for async. */
  16968. ExpectIntEQ(wc_Arc4Init(&enc, NULL, INVALID_DEVID), 0);
  16969. ExpectIntEQ(wc_Arc4Init(&dec, NULL, INVALID_DEVID), 0);
  16970. ExpectIntEQ(wc_Arc4SetKey(&enc, (byte*)key, keyLen), 0);
  16971. ExpectIntEQ(wc_Arc4SetKey(&dec, (byte*)key, keyLen), 0);
  16972. ExpectIntEQ(wc_Arc4Process(&enc, cipher, (byte*)input, keyLen), 0);
  16973. ExpectIntEQ(wc_Arc4Process(&dec, plain, cipher, keyLen), 0);
  16974. ExpectIntEQ(XMEMCMP(plain, input, keyLen), 0);
  16975. /* Bad args. */
  16976. ExpectIntEQ(wc_Arc4Process(NULL, plain, cipher, keyLen), BAD_FUNC_ARG);
  16977. ExpectIntEQ(wc_Arc4Process(&dec, NULL, cipher, keyLen), BAD_FUNC_ARG);
  16978. ExpectIntEQ(wc_Arc4Process(&dec, plain, NULL, keyLen), BAD_FUNC_ARG);
  16979. wc_Arc4Free(&enc);
  16980. wc_Arc4Free(&dec);
  16981. #endif
  16982. return EXPECT_RESULT();
  16983. } /* END test_wc_Arc4Process */
  16984. /*
  16985. * Testing wc_Init RsaKey()
  16986. */
  16987. static int test_wc_InitRsaKey(void)
  16988. {
  16989. EXPECT_DECLS;
  16990. #ifndef NO_RSA
  16991. RsaKey key;
  16992. XMEMSET(&key, 0, sizeof(RsaKey));
  16993. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  16994. /* Test bad args. */
  16995. ExpectIntEQ(wc_InitRsaKey(NULL, HEAP_HINT), BAD_FUNC_ARG);
  16996. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  16997. #endif
  16998. return EXPECT_RESULT();
  16999. } /* END test_wc_InitRsaKey */
  17000. /*
  17001. * Testing wc_RsaPrivateKeyDecode()
  17002. */
  17003. static int test_wc_RsaPrivateKeyDecode(void)
  17004. {
  17005. EXPECT_DECLS;
  17006. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  17007. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  17008. RsaKey key;
  17009. byte* tmp = NULL;
  17010. word32 idx = 0;
  17011. int bytes = 0;
  17012. XMEMSET(&key, 0, sizeof(RsaKey));
  17013. ExpectNotNull(tmp = (byte*)XMALLOC(FOURK_BUF, NULL,
  17014. DYNAMIC_TYPE_TMP_BUFFER));
  17015. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17016. if (tmp != NULL) {
  17017. #ifdef USE_CERT_BUFFERS_1024
  17018. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  17019. bytes = sizeof_client_key_der_1024;
  17020. #else
  17021. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  17022. bytes = sizeof_client_key_der_2048;
  17023. #endif /* Use cert buffers. */
  17024. }
  17025. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes), 0);
  17026. /* Test bad args. */
  17027. ExpectIntEQ(wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes),
  17028. BAD_FUNC_ARG);
  17029. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes),
  17030. BAD_FUNC_ARG);
  17031. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes),
  17032. BAD_FUNC_ARG);
  17033. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17034. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17035. #endif
  17036. return EXPECT_RESULT();
  17037. } /* END test_wc_RsaPrivateKeyDecode */
  17038. /*
  17039. * Testing wc_RsaPublicKeyDecode()
  17040. */
  17041. static int test_wc_RsaPublicKeyDecode(void)
  17042. {
  17043. EXPECT_DECLS;
  17044. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  17045. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  17046. RsaKey keyPub;
  17047. byte* tmp = NULL;
  17048. word32 idx = 0;
  17049. int bytes = 0;
  17050. word32 keySz = 0;
  17051. word32 tstKeySz = 0;
  17052. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  17053. XFILE f = XBADFILE;
  17054. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  17055. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  17056. byte buf[4096];
  17057. #endif
  17058. XMEMSET(&keyPub, 0, sizeof(RsaKey));
  17059. ExpectNotNull(tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17060. ExpectIntEQ(wc_InitRsaKey(&keyPub, HEAP_HINT), 0);
  17061. if (tmp != NULL) {
  17062. #ifdef USE_CERT_BUFFERS_1024
  17063. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  17064. bytes = sizeof_client_keypub_der_1024;
  17065. keySz = 1024;
  17066. #else
  17067. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  17068. bytes = sizeof_client_keypub_der_2048;
  17069. keySz = 2048;
  17070. #endif
  17071. }
  17072. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes), 0);
  17073. /* Pass in bad args. */
  17074. ExpectIntEQ(wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes),
  17075. BAD_FUNC_ARG);
  17076. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes),
  17077. BAD_FUNC_ARG);
  17078. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes),
  17079. BAD_FUNC_ARG);
  17080. DoExpectIntEQ(wc_FreeRsaKey(&keyPub), 0);
  17081. /* Test for getting modulus key size */
  17082. idx = 0;
  17083. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  17084. &tstKeySz, NULL, NULL), 0);
  17085. ExpectIntEQ(tstKeySz, keySz/8);
  17086. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  17087. ExpectTrue((f = XFOPEN(rsaPssPubKey, "rb")) != XBADFILE);
  17088. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  17089. if (f != XBADFILE) {
  17090. XFCLOSE(f);
  17091. f = XBADFILE;
  17092. }
  17093. idx = 0;
  17094. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  17095. NULL), 0);
  17096. ExpectTrue((f = XFOPEN(rsaPssPubKeyNoParams, "rb")) != XBADFILE);
  17097. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  17098. if (f != XBADFILE)
  17099. XFCLOSE(f);
  17100. idx = 0;
  17101. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, bytes, NULL, NULL, NULL,
  17102. NULL), 0);
  17103. #endif
  17104. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17105. #endif
  17106. return EXPECT_RESULT();
  17107. } /* END test_wc_RsaPublicKeyDecode */
  17108. /*
  17109. * Testing wc_RsaPublicKeyDecodeRaw()
  17110. */
  17111. static int test_wc_RsaPublicKeyDecodeRaw(void)
  17112. {
  17113. EXPECT_DECLS;
  17114. #if !defined(NO_RSA)
  17115. RsaKey key;
  17116. const byte n = 0x23;
  17117. const byte e = 0x03;
  17118. int nSz = sizeof(n);
  17119. int eSz = sizeof(e);
  17120. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17121. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key), 0);
  17122. /* Pass in bad args. */
  17123. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key),
  17124. BAD_FUNC_ARG);
  17125. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key),
  17126. BAD_FUNC_ARG);
  17127. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL),
  17128. BAD_FUNC_ARG);
  17129. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17130. #endif
  17131. return EXPECT_RESULT();
  17132. } /* END test_wc_RsaPublicKeyDecodeRaw */
  17133. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17134. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  17135. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  17136. * trying until it gets a probable prime. */
  17137. #ifdef HAVE_FIPS
  17138. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  17139. {
  17140. int ret;
  17141. for (;;) {
  17142. ret = wc_MakeRsaKey(key, size, e, rng);
  17143. if (ret != PRIME_GEN_E) break;
  17144. fprintf(stderr, "MakeRsaKey couldn't find prime; "
  17145. "trying again.\n");
  17146. }
  17147. return ret;
  17148. }
  17149. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  17150. #else
  17151. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  17152. #endif
  17153. #endif
  17154. /*
  17155. * Testing wc_MakeRsaKey()
  17156. */
  17157. static int test_wc_MakeRsaKey(void)
  17158. {
  17159. EXPECT_DECLS;
  17160. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17161. RsaKey genKey;
  17162. WC_RNG rng;
  17163. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17164. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17165. int bits = 1024;
  17166. #else
  17167. int bits = 2048;
  17168. #endif
  17169. XMEMSET(&genKey, 0, sizeof(RsaKey));
  17170. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17171. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  17172. ExpectIntEQ(wc_InitRng(&rng), 0);
  17173. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  17174. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  17175. /* Test bad args. */
  17176. ExpectIntEQ(MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng), BAD_FUNC_ARG);
  17177. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL),
  17178. BAD_FUNC_ARG);
  17179. /* e < 3 */
  17180. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 2, &rng), BAD_FUNC_ARG);
  17181. /* e & 1 == 0 */
  17182. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 6, &rng), BAD_FUNC_ARG);
  17183. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17184. #endif
  17185. return EXPECT_RESULT();
  17186. } /* END test_wc_MakeRsaKey */
  17187. /*
  17188. * Test the bounds checking on the cipher text versus the key modulus.
  17189. * 1. Make a new RSA key.
  17190. * 2. Set c to 1.
  17191. * 3. Decrypt c into k. (error)
  17192. * 4. Copy the key modulus to c and sub 1 from the copy.
  17193. * 5. Decrypt c into k. (error)
  17194. * Valid bounds test cases are covered by all the other RSA tests.
  17195. */
  17196. static int test_RsaDecryptBoundsCheck(void)
  17197. {
  17198. EXPECT_DECLS;
  17199. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  17200. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  17201. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  17202. WC_RNG rng;
  17203. RsaKey key;
  17204. byte flatC[256];
  17205. word32 flatCSz;
  17206. byte out[256];
  17207. word32 outSz = sizeof(out);
  17208. XMEMSET(&key, 0, sizeof(RsaKey));
  17209. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17210. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17211. ExpectIntEQ(wc_InitRng(&rng), 0);
  17212. if (EXPECT_SUCCESS()) {
  17213. const byte* derKey;
  17214. word32 derKeySz;
  17215. word32 idx = 0;
  17216. #ifdef USE_CERT_BUFFERS_1024
  17217. derKey = server_key_der_1024;
  17218. derKeySz = (word32)sizeof_server_key_der_1024;
  17219. flatCSz = 128;
  17220. #else
  17221. derKey = server_key_der_2048;
  17222. derKeySz = (word32)sizeof_server_key_der_2048;
  17223. flatCSz = 256;
  17224. #endif
  17225. ExpectIntEQ(wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz), 0);
  17226. }
  17227. if (EXPECT_SUCCESS()) {
  17228. XMEMSET(flatC, 0, flatCSz);
  17229. flatC[flatCSz-1] = 1;
  17230. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  17231. RSA_PRIVATE_DECRYPT, &rng), RSA_OUT_OF_RANGE_E);
  17232. if (EXPECT_SUCCESS()) {
  17233. mp_int c;
  17234. ExpectIntEQ(mp_init_copy(&c, &key.n), 0);
  17235. ExpectIntEQ(mp_sub_d(&c, 1, &c), 0);
  17236. ExpectIntEQ(mp_to_unsigned_bin(&c, flatC), 0);
  17237. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  17238. RSA_PRIVATE_DECRYPT, NULL), RSA_OUT_OF_RANGE_E);
  17239. mp_clear(&c);
  17240. }
  17241. }
  17242. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17243. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17244. #endif
  17245. return EXPECT_RESULT();
  17246. } /* END test_wc_RsaDecryptBoundsCheck */
  17247. /*
  17248. * Testing wc_SetKeyUsage()
  17249. */
  17250. static int test_wc_SetKeyUsage(void)
  17251. {
  17252. EXPECT_DECLS;
  17253. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  17254. Cert myCert;
  17255. ExpectIntEQ(wc_InitCert(&myCert), 0);
  17256. ExpectIntEQ(wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement"), 0);
  17257. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation"), 0);
  17258. ExpectIntEQ(wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly"), 0);
  17259. ExpectIntEQ(wc_SetKeyUsage(&myCert, "decipherOnly"), 0);
  17260. ExpectIntEQ(wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign"), 0);
  17261. /* Test bad args. */
  17262. ExpectIntEQ(wc_SetKeyUsage(NULL, "decipherOnly"), BAD_FUNC_ARG);
  17263. ExpectIntEQ(wc_SetKeyUsage(&myCert, NULL), BAD_FUNC_ARG);
  17264. ExpectIntEQ(wc_SetKeyUsage(&myCert, ""), KEYUSAGE_E);
  17265. ExpectIntEQ(wc_SetKeyUsage(&myCert, ","), KEYUSAGE_E);
  17266. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign"),
  17267. KEYUSAGE_E);
  17268. #endif
  17269. return EXPECT_RESULT();
  17270. } /* END test_wc_SetKeyUsage */
  17271. /*
  17272. * Testing wc_CheckProbablePrime()
  17273. */
  17274. static int test_wc_CheckProbablePrime(void)
  17275. {
  17276. EXPECT_DECLS;
  17277. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17278. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  17279. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  17280. RsaKey key;
  17281. WC_RNG rng;
  17282. byte e[3];
  17283. word32 eSz = (word32)sizeof(e);
  17284. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  17285. word32 nSz = (word32)sizeof(n);
  17286. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  17287. word32 dSz = (word32)sizeof(d);
  17288. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  17289. word32 pSz = (word32)sizeof(p);
  17290. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  17291. word32 qSz = (word32)sizeof(q);
  17292. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  17293. int* isPrime;
  17294. int test[5];
  17295. isPrime = test;
  17296. XMEMSET(&key, 0, sizeof(RsaKey));
  17297. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17298. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17299. ExpectIntEQ(wc_InitRng(&rng), 0);
  17300. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17301. ExpectIntEQ(wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS,
  17302. WC_RSA_EXPONENT, &rng), 0);
  17303. PRIVATE_KEY_UNLOCK();
  17304. ExpectIntEQ(wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q,
  17305. &qSz), 0);
  17306. PRIVATE_KEY_LOCK();
  17307. /* Bad cases */
  17308. ExpectIntEQ(wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz, nlen, isPrime),
  17309. BAD_FUNC_ARG);
  17310. ExpectIntEQ(wc_CheckProbablePrime(p, 0, q, qSz, e, eSz, nlen, isPrime),
  17311. BAD_FUNC_ARG);
  17312. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz, nlen, isPrime),
  17313. BAD_FUNC_ARG);
  17314. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, 0, e, eSz, nlen, isPrime),
  17315. BAD_FUNC_ARG);
  17316. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz, nlen, isPrime),
  17317. BAD_FUNC_ARG);
  17318. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, 0, nlen, isPrime),
  17319. BAD_FUNC_ARG);
  17320. ExpectIntEQ(wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0, nlen, isPrime),
  17321. BAD_FUNC_ARG);
  17322. /* Good case */
  17323. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz, nlen, isPrime),
  17324. 0);
  17325. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17326. wc_FreeRng(&rng);
  17327. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  17328. #endif
  17329. return EXPECT_RESULT();
  17330. } /* END test_wc_CheckProbablePrime */
  17331. /*
  17332. * Testing wc_RsaPSS_Verify()
  17333. */
  17334. static int test_wc_RsaPSS_Verify(void)
  17335. {
  17336. EXPECT_DECLS;
  17337. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17338. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17339. RsaKey key;
  17340. WC_RNG rng;
  17341. int sz = 256;
  17342. const char* szMessage = "This is the string to be signed";
  17343. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17344. unsigned char pDecrypted[2048/8];
  17345. byte* pt = pDecrypted;
  17346. word32 outLen = sizeof(pDecrypted);
  17347. XMEMSET(&key, 0, sizeof(RsaKey));
  17348. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17349. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17350. ExpectIntEQ(wc_InitRng(&rng), 0);
  17351. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17352. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17353. ExpectIntGT(sz = wc_RsaPSS_Sign((byte*)szMessage,
  17354. (word32)XSTRLEN(szMessage)+1, pSignature, sizeof(pSignature),
  17355. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17356. /* Bad cases */
  17357. ExpectIntEQ(wc_RsaPSS_Verify(NULL, sz, pt, outLen,
  17358. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17359. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  17360. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17361. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, sz, NULL, outLen,
  17362. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17363. ExpectIntEQ(wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  17364. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17365. /* Good case */
  17366. ExpectIntGT(wc_RsaPSS_Verify(pSignature, sz, pt, outLen,
  17367. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17368. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17369. wc_FreeRng(&rng);
  17370. #endif
  17371. return EXPECT_RESULT();
  17372. } /* END test_wc_RsaPSS_Verify */
  17373. /*
  17374. * Testing wc_RsaPSS_VerifyCheck()
  17375. */
  17376. static int test_wc_RsaPSS_VerifyCheck(void)
  17377. {
  17378. EXPECT_DECLS;
  17379. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17380. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17381. RsaKey key;
  17382. WC_RNG rng;
  17383. int sz = 256; /* 2048/8 */
  17384. byte digest[32];
  17385. word32 digestSz = sizeof(digest);
  17386. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17387. word32 pSignatureSz = sizeof(pSignature);
  17388. unsigned char pDecrypted[2048/8];
  17389. byte* pt = pDecrypted;
  17390. word32 outLen = sizeof(pDecrypted);
  17391. XMEMSET(&key, 0, sizeof(RsaKey));
  17392. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17393. XMEMSET(digest, 0, sizeof(digest));
  17394. XMEMSET(pSignature, 0, sizeof(pSignature));
  17395. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17396. ExpectIntEQ(wc_InitRng(&rng), 0);
  17397. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17398. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17399. ExpectTrue((digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  17400. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz),
  17401. 0);
  17402. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  17403. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17404. /* Bad cases */
  17405. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, sz, pt, outLen, digest,
  17406. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17407. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen, digest,
  17408. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17409. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, sz, NULL, outLen, digest,
  17410. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17411. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen, digest,
  17412. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17413. /* Good case */
  17414. ExpectIntGT(wc_RsaPSS_VerifyCheck(pSignature, sz, pt, outLen, digest,
  17415. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17416. ExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17417. wc_FreeRng(&rng);
  17418. #endif
  17419. return EXPECT_RESULT();
  17420. } /* END test_wc_RsaPSS_VerifyCheck */
  17421. /*
  17422. * Testing wc_RsaPSS_VerifyCheckInline()
  17423. */
  17424. static int test_wc_RsaPSS_VerifyCheckInline(void)
  17425. {
  17426. EXPECT_DECLS;
  17427. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17428. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17429. RsaKey key;
  17430. WC_RNG rng;
  17431. int sz = 256;
  17432. byte digest[32];
  17433. word32 digestSz = sizeof(digest);
  17434. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17435. unsigned char pDecrypted[2048/8];
  17436. byte* pt = pDecrypted;
  17437. XMEMSET(&key, 0, sizeof(RsaKey));
  17438. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17439. XMEMSET(digest, 0, sizeof(digest));
  17440. XMEMSET(pSignature, 0, sizeof(pSignature));
  17441. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17442. ExpectIntEQ(wc_InitRng(&rng), 0);
  17443. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17444. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17445. ExpectTrue((digestSz = wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  17446. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, sz, digest, digestSz),
  17447. 0);
  17448. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature,
  17449. sizeof(pSignature), WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17450. /* Bad Cases */
  17451. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, sz, &pt, digest,
  17452. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17453. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL, digest,
  17454. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17455. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt, digest,
  17456. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17457. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt, digest,
  17458. digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17459. /* Good case */
  17460. ExpectIntGT(wc_RsaPSS_VerifyCheckInline(pSignature, sz, &pt, digest,
  17461. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17462. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17463. wc_FreeRng(&rng);
  17464. #endif
  17465. return EXPECT_RESULT();
  17466. } /* END test_wc_RsaPSS_VerifyCheckInline */
  17467. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17468. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  17469. {
  17470. (void)flag;
  17471. (void)type;
  17472. (void)file;
  17473. (void)line;
  17474. }
  17475. #endif
  17476. /*
  17477. * Testing wc_LockMutex_ex
  17478. */
  17479. static int test_wc_LockMutex_ex(void)
  17480. {
  17481. EXPECT_DECLS;
  17482. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17483. int flag = CRYPTO_LOCK;
  17484. int type = 0;
  17485. const char* file = "./test-LockMutex_ex.txt";
  17486. int line = 0;
  17487. /* without SetMutexCb */
  17488. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), BAD_STATE_E);
  17489. /* with SetMutexCb */
  17490. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  17491. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), 0);
  17492. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  17493. #endif
  17494. return EXPECT_RESULT();
  17495. } /* End test_wc_LockMutex_ex*/
  17496. /*
  17497. * Testing wc_SetMutexCb
  17498. */
  17499. static int test_wc_SetMutexCb(void)
  17500. {
  17501. EXPECT_DECLS;
  17502. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17503. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  17504. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  17505. #endif
  17506. return EXPECT_RESULT();
  17507. } /* End test_wc_SetMutexCb*/
  17508. /*
  17509. * Testing wc_RsaKeyToDer()
  17510. */
  17511. static int test_wc_RsaKeyToDer(void)
  17512. {
  17513. EXPECT_DECLS;
  17514. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17515. RsaKey genKey;
  17516. WC_RNG rng;
  17517. byte* der = NULL;
  17518. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17519. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17520. int bits = 1024;
  17521. word32 derSz = 611;
  17522. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  17523. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  17524. #else
  17525. int bits = 2048;
  17526. word32 derSz = 1196;
  17527. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  17528. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  17529. #endif
  17530. XMEMSET(&rng, 0, sizeof(rng));
  17531. XMEMSET(&genKey, 0, sizeof(genKey));
  17532. ExpectNotNull(der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17533. /* Init structures. */
  17534. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  17535. ExpectIntEQ(wc_InitRng(&rng), 0);
  17536. /* Make key. */
  17537. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  17538. ExpectIntGT(wc_RsaKeyToDer(&genKey, der, derSz), 0);
  17539. /* Pass good/bad args. */
  17540. ExpectIntEQ(wc_RsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  17541. /* Get just the output length */
  17542. ExpectIntGT(wc_RsaKeyToDer(&genKey, NULL, 0), 0);
  17543. /* Try Public Key. */
  17544. genKey.type = 0;
  17545. ExpectIntEQ(wc_RsaKeyToDer(&genKey, der, FOURK_BUF), BAD_FUNC_ARG);
  17546. #ifdef WOLFSSL_CHECK_MEM_ZERO
  17547. /* Put back to Private Key */
  17548. genKey.type = 1;
  17549. #endif
  17550. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17551. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  17552. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17553. #endif
  17554. return EXPECT_RESULT();
  17555. } /* END test_wc_RsaKeyToDer */
  17556. /*
  17557. * Testing wc_RsaKeyToPublicDer()
  17558. */
  17559. static int test_wc_RsaKeyToPublicDer(void)
  17560. {
  17561. EXPECT_DECLS;
  17562. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17563. RsaKey key;
  17564. WC_RNG rng;
  17565. byte* der = NULL;
  17566. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17567. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17568. int bits = 1024;
  17569. word32 derLen = 162;
  17570. #else
  17571. int bits = 2048;
  17572. word32 derLen = 294;
  17573. #endif
  17574. int ret;
  17575. XMEMSET(&rng, 0, sizeof(rng));
  17576. XMEMSET(&key, 0, sizeof(key));
  17577. ExpectNotNull(der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17578. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17579. ExpectIntEQ(wc_InitRng(&rng), 0);
  17580. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17581. /* test getting size only */
  17582. ExpectIntGT(wc_RsaKeyToPublicDer(&key, NULL, derLen), 0);
  17583. ExpectIntGT(wc_RsaKeyToPublicDer(&key, der, derLen), 0);
  17584. /* test getting size only */
  17585. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0), 0);
  17586. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0), 0);
  17587. /* Pass in bad args. */
  17588. ExpectIntEQ(wc_RsaKeyToPublicDer(NULL, der, derLen), BAD_FUNC_ARG);
  17589. ExpectIntLT(ret = wc_RsaKeyToPublicDer(&key, der, -1), 0);
  17590. ExpectTrue((ret == BUFFER_E) || (ret == BAD_FUNC_ARG));
  17591. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17592. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17593. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17594. #endif
  17595. return EXPECT_RESULT();
  17596. } /* END test_wc_RsaKeyToPublicDer */
  17597. /*
  17598. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  17599. */
  17600. static int test_wc_RsaPublicEncryptDecrypt(void)
  17601. {
  17602. EXPECT_DECLS;
  17603. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17604. RsaKey key;
  17605. WC_RNG rng;
  17606. const char inStr[] = TEST_STRING;
  17607. const word32 plainLen = (word32)TEST_STRING_SZ;
  17608. const word32 inLen = (word32)TEST_STRING_SZ;
  17609. int bits = TEST_RSA_BITS;
  17610. const word32 cipherLen = TEST_RSA_BYTES;
  17611. word32 cipherLenResult = cipherLen;
  17612. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17613. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17614. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17615. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17616. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17617. WC_ALLOC_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17618. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17619. ExpectNotNull(in);
  17620. ExpectNotNull(plain);
  17621. ExpectNotNull(cipher);
  17622. #endif
  17623. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17624. /* Initialize stack structures. */
  17625. XMEMSET(&key, 0, sizeof(RsaKey));
  17626. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17627. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17628. ExpectIntEQ(wc_InitRng(&rng), 0);
  17629. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17630. /* Encrypt. */
  17631. ExpectIntGT(cipherLenResult = wc_RsaPublicEncrypt(in, inLen, cipher,
  17632. cipherLen, &key, &rng), 0);
  17633. /* Pass bad args - tested in another testing function.*/
  17634. /* Decrypt */
  17635. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17636. /* Bind rng */
  17637. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17638. #endif
  17639. ExpectIntGE(wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen,
  17640. &key), 0);
  17641. ExpectIntEQ(XMEMCMP(plain, inStr, plainLen), 0);
  17642. /* Pass bad args - tested in another testing function.*/
  17643. WC_FREE_VAR(in, NULL);
  17644. WC_FREE_VAR(plain, NULL);
  17645. WC_FREE_VAR(cipher, NULL);
  17646. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17647. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17648. #endif
  17649. return EXPECT_RESULT();
  17650. } /* END test_wc_RsaPublicEncryptDecrypt */
  17651. /*
  17652. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  17653. */
  17654. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  17655. {
  17656. EXPECT_DECLS;
  17657. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  17658. && !defined(WC_NO_RSA_OAEP) && !defined(NO_SHA256)
  17659. RsaKey key;
  17660. WC_RNG rng;
  17661. const char inStr[] = TEST_STRING;
  17662. const word32 inLen = (word32)TEST_STRING_SZ;
  17663. const word32 plainSz = (word32)TEST_STRING_SZ;
  17664. byte* res = NULL;
  17665. int idx = 0;
  17666. int bits = TEST_RSA_BITS;
  17667. const word32 cipherSz = TEST_RSA_BYTES;
  17668. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17669. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17670. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17671. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17672. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17673. WC_ALLOC_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17674. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17675. ExpectNotNull(in);
  17676. ExpectNotNull(plain);
  17677. ExpectNotNull(cipher);
  17678. #endif
  17679. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17680. /* Initialize stack structures. */
  17681. XMEMSET(&key, 0, sizeof(RsaKey));
  17682. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17683. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID), 0);
  17684. ExpectIntEQ(wc_InitRng(&rng), 0);
  17685. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17686. /* Encrypt */
  17687. ExpectIntGE(idx = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key,
  17688. &rng, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17689. /* Pass bad args - tested in another testing function.*/
  17690. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17691. /* Decrypt */
  17692. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17693. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17694. #endif
  17695. ExpectIntGE(wc_RsaPrivateDecrypt_ex(cipher, (word32)idx, plain, plainSz,
  17696. &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17697. ExpectIntEQ(XMEMCMP(plain, inStr, plainSz), 0);
  17698. /* Pass bad args - tested in another testing function.*/
  17699. ExpectIntGE(wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx, &res, &key,
  17700. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17701. ExpectIntEQ(XMEMCMP(inStr, res, plainSz), 0);
  17702. #endif
  17703. WC_FREE_VAR(in, NULL);
  17704. WC_FREE_VAR(plain, NULL);
  17705. WC_FREE_VAR(cipher, NULL);
  17706. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17707. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17708. #endif
  17709. return EXPECT_RESULT();
  17710. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  17711. /*
  17712. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  17713. */
  17714. static int test_wc_RsaSSL_SignVerify(void)
  17715. {
  17716. EXPECT_DECLS;
  17717. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17718. RsaKey key;
  17719. WC_RNG rng;
  17720. const char inStr[] = TEST_STRING;
  17721. const word32 plainSz = (word32)TEST_STRING_SZ;
  17722. const word32 inLen = (word32)TEST_STRING_SZ;
  17723. word32 idx = 0;
  17724. int bits = TEST_RSA_BITS;
  17725. const word32 outSz = TEST_RSA_BYTES;
  17726. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17727. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  17728. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17729. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17730. WC_ALLOC_VAR(out, byte, TEST_RSA_BYTES, NULL);
  17731. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17732. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17733. ExpectNotNull(in);
  17734. ExpectNotNull(out);
  17735. ExpectNotNull(plain);
  17736. #endif
  17737. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17738. XMEMSET(&key, 0, sizeof(RsaKey));
  17739. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17740. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17741. ExpectIntEQ(wc_InitRng(&rng), 0);
  17742. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17743. /* Sign. */
  17744. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng), (int)outSz);
  17745. idx = (int)outSz;
  17746. /* Test bad args. */
  17747. ExpectIntEQ(wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng),
  17748. BAD_FUNC_ARG);
  17749. ExpectIntEQ(wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng),
  17750. BAD_FUNC_ARG);
  17751. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng),
  17752. BAD_FUNC_ARG);
  17753. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng),
  17754. BAD_FUNC_ARG);
  17755. /* Verify. */
  17756. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, &key), (int)inLen);
  17757. /* Pass bad args. */
  17758. ExpectIntEQ(wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key),
  17759. BAD_FUNC_ARG);
  17760. ExpectIntEQ(wc_RsaSSL_Verify(out, 0, plain, plainSz, &key),
  17761. BAD_FUNC_ARG);
  17762. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key),
  17763. BAD_FUNC_ARG);
  17764. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL),
  17765. BAD_FUNC_ARG);
  17766. WC_FREE_VAR(in, NULL);
  17767. WC_FREE_VAR(out, NULL);
  17768. WC_FREE_VAR(plain, NULL);
  17769. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17770. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17771. #endif
  17772. return EXPECT_RESULT();
  17773. } /* END test_wc_RsaSSL_SignVerify */
  17774. /*
  17775. * Testing wc_RsaEncryptSize()
  17776. */
  17777. static int test_wc_RsaEncryptSize(void)
  17778. {
  17779. EXPECT_DECLS;
  17780. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17781. RsaKey key;
  17782. WC_RNG rng;
  17783. XMEMSET(&key, 0, sizeof(RsaKey));
  17784. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17785. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17786. ExpectIntEQ(wc_InitRng(&rng), 0);
  17787. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17788. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17789. ExpectIntEQ(MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng), 0);
  17790. ExpectIntEQ(wc_RsaEncryptSize(&key), 128);
  17791. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17792. #endif
  17793. ExpectIntEQ(MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17794. ExpectIntEQ(wc_RsaEncryptSize(&key), 256);
  17795. /* Pass in bad arg. */
  17796. ExpectIntEQ(wc_RsaEncryptSize(NULL), BAD_FUNC_ARG);
  17797. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17798. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17799. #endif
  17800. return EXPECT_RESULT();
  17801. } /* END test_wc_RsaEncryptSize*/
  17802. /*
  17803. * Testing wc_RsaFlattenPublicKey()
  17804. */
  17805. static int test_wc_RsaFlattenPublicKey(void)
  17806. {
  17807. EXPECT_DECLS;
  17808. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17809. RsaKey key;
  17810. WC_RNG rng;
  17811. byte e[256];
  17812. byte n[256];
  17813. word32 eSz = sizeof(e);
  17814. word32 nSz = sizeof(n);
  17815. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17816. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17817. int bits = 1024;
  17818. #else
  17819. int bits = 2048;
  17820. #endif
  17821. XMEMSET(&key, 0, sizeof(RsaKey));
  17822. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17823. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17824. ExpectIntEQ(wc_InitRng(&rng), 0);
  17825. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17826. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz), 0);
  17827. /* Pass bad args. */
  17828. ExpectIntEQ(wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz),
  17829. BAD_FUNC_ARG);
  17830. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz),
  17831. BAD_FUNC_ARG);
  17832. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz),
  17833. BAD_FUNC_ARG);
  17834. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz),
  17835. BAD_FUNC_ARG);
  17836. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL),
  17837. BAD_FUNC_ARG);
  17838. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17839. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17840. #endif
  17841. return EXPECT_RESULT();
  17842. } /* END test_wc_RsaFlattenPublicKey */
  17843. /*
  17844. * unit test for wc_AesCcmSetKey
  17845. */
  17846. static int test_wc_AesCcmSetKey(void)
  17847. {
  17848. EXPECT_DECLS;
  17849. #ifdef HAVE_AESCCM
  17850. Aes aes;
  17851. const byte key16[] = {
  17852. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17853. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17854. };
  17855. const byte key24[] = {
  17856. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17857. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17858. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  17859. };
  17860. const byte key32[] = {
  17861. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17862. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17863. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17864. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  17865. };
  17866. XMEMSET(&aes, 0, sizeof(Aes));
  17867. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  17868. #ifdef WOLFSSL_AES_128
  17869. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  17870. #endif
  17871. #ifdef WOLFSSL_AES_192
  17872. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24)), 0);
  17873. #endif
  17874. #ifdef WOLFSSL_AES_256
  17875. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32)), 0);
  17876. #endif
  17877. /* Test bad args. */
  17878. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1), BAD_FUNC_ARG);
  17879. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1), BAD_FUNC_ARG);
  17880. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1), BAD_FUNC_ARG);
  17881. wc_AesFree(&aes);
  17882. #endif
  17883. return EXPECT_RESULT();
  17884. } /* END test_wc_AesCcmSetKey */
  17885. /*
  17886. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  17887. */
  17888. static int test_wc_AesCcmEncryptDecrypt(void)
  17889. {
  17890. EXPECT_DECLS;
  17891. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  17892. Aes aes;
  17893. const byte key16[] = {
  17894. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17895. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17896. };
  17897. /* plaintext */
  17898. const byte plainT[] = {
  17899. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  17900. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  17901. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  17902. };
  17903. /* nonce */
  17904. const byte iv[] = {
  17905. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  17906. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  17907. };
  17908. const byte c[] = { /* cipher text. */
  17909. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  17910. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  17911. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  17912. };
  17913. const byte t[] = { /* Auth tag */
  17914. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  17915. };
  17916. const byte authIn[] = {
  17917. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  17918. };
  17919. byte cipherOut[sizeof(plainT)];
  17920. byte authTag[sizeof(t)];
  17921. #ifdef HAVE_AES_DECRYPT
  17922. byte plainOut[sizeof(cipherOut)];
  17923. #endif
  17924. XMEMSET(&aes, 0, sizeof(Aes));
  17925. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  17926. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  17927. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17928. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)), 0);
  17929. ExpectIntEQ(XMEMCMP(cipherOut, c, sizeof(c)), 0);
  17930. ExpectIntEQ(XMEMCMP(t, authTag, sizeof(t)), 0);
  17931. #ifdef HAVE_AES_DECRYPT
  17932. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17933. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)), 0);
  17934. ExpectIntEQ(XMEMCMP(plainOut, plainT, sizeof(plainT)), 0);
  17935. #endif
  17936. /* Pass in bad args. Encrypt*/
  17937. ExpectIntEQ(wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  17938. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17939. BAD_FUNC_ARG);
  17940. ExpectIntEQ(wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  17941. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17942. BAD_FUNC_ARG);
  17943. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  17944. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17945. BAD_FUNC_ARG);
  17946. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17947. NULL, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17948. BAD_FUNC_ARG);
  17949. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17950. iv, sizeof(iv), NULL, sizeof(authTag), authIn , sizeof(authIn)),
  17951. BAD_FUNC_ARG);
  17952. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17953. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17954. BAD_FUNC_ARG);
  17955. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17956. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17957. BAD_FUNC_ARG);
  17958. #ifdef HAVE_AES_DECRYPT
  17959. /* Pass in bad args. Decrypt*/
  17960. ExpectIntEQ(wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  17961. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17962. BAD_FUNC_ARG);
  17963. ExpectIntEQ(wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  17964. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17965. BAD_FUNC_ARG);
  17966. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  17967. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17968. BAD_FUNC_ARG);
  17969. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17970. NULL, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17971. BAD_FUNC_ARG);
  17972. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17973. iv, sizeof(iv), NULL, sizeof(authTag), authIn, sizeof(authIn)),
  17974. BAD_FUNC_ARG);
  17975. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17976. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17977. BAD_FUNC_ARG);
  17978. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17979. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17980. BAD_FUNC_ARG);
  17981. #endif
  17982. wc_AesFree(&aes);
  17983. #endif /* HAVE_AESCCM */
  17984. return EXPECT_RESULT();
  17985. } /* END test_wc_AesCcmEncryptDecrypt */
  17986. #if defined(WOLFSSL_AES_EAX) && \
  17987. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  17988. /*
  17989. * Testing test_wc_AesEaxVectors()
  17990. */
  17991. static int test_wc_AesEaxVectors(void)
  17992. {
  17993. EXPECT_DECLS;
  17994. typedef struct {
  17995. byte key[AES_256_KEY_SIZE];
  17996. int key_length;
  17997. byte iv[AES_BLOCK_SIZE];
  17998. int iv_length;
  17999. byte aad[AES_BLOCK_SIZE * 2];
  18000. int aad_length;
  18001. byte msg[AES_BLOCK_SIZE * 5];
  18002. int msg_length;
  18003. byte ct[AES_BLOCK_SIZE * 5];
  18004. int ct_length;
  18005. byte tag[AES_BLOCK_SIZE];
  18006. int tag_length;
  18007. int valid;
  18008. } AadVector;
  18009. /* Test vectors obtained from Google wycheproof project
  18010. * https://github.com/google/wycheproof
  18011. * from testvectors/aes_eax_test.json
  18012. */
  18013. const AadVector vectors[] = {
  18014. {
  18015. /* key, key length */
  18016. {0x23, 0x39, 0x52, 0xde, 0xe4, 0xd5, 0xed, 0x5f,
  18017. 0x9b, 0x9c, 0x6d, 0x6f, 0xf8, 0x0f, 0xf4, 0x78}, 16,
  18018. /* iv, iv length */
  18019. {0x62, 0xec, 0x67, 0xf9, 0xc3, 0xa4, 0xa4, 0x07,
  18020. 0xfc, 0xb2, 0xa8, 0xc4, 0x90, 0x31, 0xa8, 0xb3}, 16,
  18021. /* aad, aad length */
  18022. {0x6b, 0xfb, 0x91, 0x4f, 0xd0, 0x7e, 0xae, 0x6b}, 8,
  18023. /* msg, msg length */
  18024. {0x00}, 0,
  18025. /* ct, ct length */
  18026. {0x00}, 0,
  18027. /* tag, tag length */
  18028. {0xe0, 0x37, 0x83, 0x0e, 0x83, 0x89, 0xf2, 0x7b,
  18029. 0x02, 0x5a, 0x2d, 0x65, 0x27, 0xe7, 0x9d, 0x01}, 16,
  18030. /* valid */
  18031. 1,
  18032. },
  18033. {
  18034. /* key, key length */
  18035. {0x91, 0x94, 0x5d, 0x3f, 0x4d, 0xcb, 0xee, 0x0b,
  18036. 0xf4, 0x5e, 0xf5, 0x22, 0x55, 0xf0, 0x95, 0xa4}, 16,
  18037. /* iv, iv length */
  18038. {0xbe, 0xca, 0xf0, 0x43, 0xb0, 0xa2, 0x3d, 0x84,
  18039. 0x31, 0x94, 0xba, 0x97, 0x2c, 0x66, 0xde, 0xbd}, 16,
  18040. /* aad, aad length */
  18041. {0xfa, 0x3b, 0xfd, 0x48, 0x06, 0xeb, 0x53, 0xfa}, 8,
  18042. /* msg, msg length */
  18043. {0xf7, 0xfb}, 2,
  18044. /* ct, ct length */
  18045. {0x19, 0xdd}, 2,
  18046. /* tag, tag length */
  18047. {0x5c, 0x4c, 0x93, 0x31, 0x04, 0x9d, 0x0b, 0xda,
  18048. 0xb0, 0x27, 0x74, 0x08, 0xf6, 0x79, 0x67, 0xe5}, 16,
  18049. /* valid */
  18050. 1,
  18051. },
  18052. {
  18053. /* key, key length */
  18054. {0x01, 0xf7, 0x4a, 0xd6, 0x40, 0x77, 0xf2, 0xe7,
  18055. 0x04, 0xc0, 0xf6, 0x0a, 0xda, 0x3d, 0xd5, 0x23}, 16,
  18056. /* iv, iv length */
  18057. {0x70, 0xc3, 0xdb, 0x4f, 0x0d, 0x26, 0x36, 0x84,
  18058. 0x00, 0xa1, 0x0e, 0xd0, 0x5d, 0x2b, 0xff, 0x5e}, 16,
  18059. /* aad, aad length */
  18060. {0x23, 0x4a, 0x34, 0x63, 0xc1, 0x26, 0x4a, 0xc6}, 8,
  18061. /* msg, msg length */
  18062. {0x1a, 0x47, 0xcb, 0x49, 0x33}, 5,
  18063. /* ct, ct length */
  18064. {0xd8, 0x51, 0xd5, 0xba, 0xe0}, 5,
  18065. /* tag, tag length */
  18066. {0x3a, 0x59, 0xf2, 0x38, 0xa2, 0x3e, 0x39, 0x19,
  18067. 0x9d, 0xc9, 0x26, 0x66, 0x26, 0xc4, 0x0f, 0x80}, 16,
  18068. /* valid */
  18069. 1,
  18070. },
  18071. {
  18072. /* key, key length */
  18073. {0xd0, 0x7c, 0xf6, 0xcb, 0xb7, 0xf3, 0x13, 0xbd,
  18074. 0xde, 0x66, 0xb7, 0x27, 0xaf, 0xd3, 0xc5, 0xe8}, 16,
  18075. /* iv, iv length */
  18076. {0x84, 0x08, 0xdf, 0xff, 0x3c, 0x1a, 0x2b, 0x12,
  18077. 0x92, 0xdc, 0x19, 0x9e, 0x46, 0xb7, 0xd6, 0x17}, 16,
  18078. /* aad, aad length */
  18079. {0x33, 0xcc, 0xe2, 0xea, 0xbf, 0xf5, 0xa7, 0x9d}, 8,
  18080. /* msg, msg length */
  18081. {0x48, 0x1c, 0x9e, 0x39, 0xb1}, 5,
  18082. /* ct, ct length */
  18083. {0x63, 0x2a, 0x9d, 0x13, 0x1a}, 5,
  18084. /* tag, tag length */
  18085. {0xd4, 0xc1, 0x68, 0xa4, 0x22, 0x5d, 0x8e, 0x1f,
  18086. 0xf7, 0x55, 0x93, 0x99, 0x74, 0xa7, 0xbe, 0xde}, 16,
  18087. /* valid */
  18088. 1,
  18089. },
  18090. {
  18091. /* key, key length */
  18092. {0x35, 0xb6, 0xd0, 0x58, 0x00, 0x05, 0xbb, 0xc1,
  18093. 0x2b, 0x05, 0x87, 0x12, 0x45, 0x57, 0xd2, 0xc2}, 16,
  18094. /* iv, iv length */
  18095. {0xfd, 0xb6, 0xb0, 0x66, 0x76, 0xee, 0xdc, 0x5c,
  18096. 0x61, 0xd7, 0x42, 0x76, 0xe1, 0xf8, 0xe8, 0x16}, 16,
  18097. /* aad, aad length */
  18098. {0xae, 0xb9, 0x6e, 0xae, 0xbe, 0x29, 0x70, 0xe9}, 8,
  18099. /* msg, msg length */
  18100. {0x40, 0xd0, 0xc0, 0x7d, 0xa5, 0xe4}, 6,
  18101. /* ct, ct length */
  18102. {0x07, 0x1d, 0xfe, 0x16, 0xc6, 0x75}, 6,
  18103. /* tag, tag length */
  18104. {0xcb, 0x06, 0x77, 0xe5, 0x36, 0xf7, 0x3a, 0xfe,
  18105. 0x6a, 0x14, 0xb7, 0x4e, 0xe4, 0x98, 0x44, 0xdd}, 16,
  18106. /* valid */
  18107. 1,
  18108. },
  18109. {
  18110. /* key, key length */
  18111. {0xbd, 0x8e, 0x6e, 0x11, 0x47, 0x5e, 0x60, 0xb2,
  18112. 0x68, 0x78, 0x4c, 0x38, 0xc6, 0x2f, 0xeb, 0x22}, 16,
  18113. /* iv, iv length */
  18114. {0x6e, 0xac, 0x5c, 0x93, 0x07, 0x2d, 0x8e, 0x85,
  18115. 0x13, 0xf7, 0x50, 0x93, 0x5e, 0x46, 0xda, 0x1b}, 16,
  18116. /* aad, aad length */
  18117. {0xd4, 0x48, 0x2d, 0x1c, 0xa7, 0x8d, 0xce, 0x0f}, 8,
  18118. /* msg, msg length */
  18119. {0x4d, 0xe3, 0xb3, 0x5c, 0x3f, 0xc0, 0x39, 0x24,
  18120. 0x5b, 0xd1, 0xfb, 0x7d}, 12,
  18121. /* ct, ct length */
  18122. {0x83, 0x5b, 0xb4, 0xf1, 0x5d, 0x74, 0x3e, 0x35,
  18123. 0x0e, 0x72, 0x84, 0x14}, 12,
  18124. /* tag, tag length */
  18125. {0xab, 0xb8, 0x64, 0x4f, 0xd6, 0xcc, 0xb8, 0x69,
  18126. 0x47, 0xc5, 0xe1, 0x05, 0x90, 0x21, 0x0a, 0x4f}, 16,
  18127. /* valid */
  18128. 1,
  18129. },
  18130. {
  18131. /* key, key length */
  18132. {0x7c, 0x77, 0xd6, 0xe8, 0x13, 0xbe, 0xd5, 0xac,
  18133. 0x98, 0xba, 0xa4, 0x17, 0x47, 0x7a, 0x2e, 0x7d}, 16,
  18134. /* iv, iv length */
  18135. {0x1a, 0x8c, 0x98, 0xdc, 0xd7, 0x3d, 0x38, 0x39,
  18136. 0x3b, 0x2b, 0xf1, 0x56, 0x9d, 0xee, 0xfc, 0x19}, 16,
  18137. /* aad, aad length */
  18138. {0x65, 0xd2, 0x01, 0x79, 0x90, 0xd6, 0x25, 0x28}, 8,
  18139. /* msg, msg length */
  18140. {0x8b, 0x0a, 0x79, 0x30, 0x6c, 0x9c, 0xe7, 0xed,
  18141. 0x99, 0xda, 0xe4, 0xf8, 0x7f, 0x8d, 0xd6, 0x16,
  18142. 0x36}, 17,
  18143. /* ct, ct length */
  18144. {0x02, 0x08, 0x3e, 0x39, 0x79, 0xda, 0x01, 0x48,
  18145. 0x12, 0xf5, 0x9f, 0x11, 0xd5, 0x26, 0x30, 0xda,
  18146. 0x30}, 17,
  18147. /* tag, tag length */
  18148. {0x13, 0x73, 0x27, 0xd1, 0x06, 0x49, 0xb0, 0xaa,
  18149. 0x6e, 0x1c, 0x18, 0x1d, 0xb6, 0x17, 0xd7, 0xf2}, 16,
  18150. /* valid */
  18151. 1,
  18152. },
  18153. {
  18154. /* key, key length */
  18155. {0x5f, 0xff, 0x20, 0xca, 0xfa, 0xb1, 0x19, 0xca,
  18156. 0x2f, 0xc7, 0x35, 0x49, 0xe2, 0x0f, 0x5b, 0x0d}, 16,
  18157. /* iv, iv length */
  18158. {0xdd, 0xe5, 0x9b, 0x97, 0xd7, 0x22, 0x15, 0x6d,
  18159. 0x4d, 0x9a, 0xff, 0x2b, 0xc7, 0x55, 0x98, 0x26}, 16,
  18160. /* aad, aad length */
  18161. {0x54, 0xb9, 0xf0, 0x4e, 0x6a, 0x09, 0x18, 0x9a}, 8,
  18162. /* msg, msg length */
  18163. {0x1b, 0xda, 0x12, 0x2b, 0xce, 0x8a, 0x8d, 0xba,
  18164. 0xf1, 0x87, 0x7d, 0x96, 0x2b, 0x85, 0x92, 0xdd,
  18165. 0x2d, 0x56}, 18,
  18166. /* ct, ct length */
  18167. {0x2e, 0xc4, 0x7b, 0x2c, 0x49, 0x54, 0xa4, 0x89,
  18168. 0xaf, 0xc7, 0xba, 0x48, 0x97, 0xed, 0xcd, 0xae,
  18169. 0x8c, 0xc3}, 18,
  18170. /* tag, tag length */
  18171. {0x3b, 0x60, 0x45, 0x05, 0x99, 0xbd, 0x02, 0xc9,
  18172. 0x63, 0x82, 0x90, 0x2a, 0xef, 0x7f, 0x83, 0x2a}, 16,
  18173. /* valid */
  18174. 1,
  18175. },
  18176. {
  18177. /* key, key length */
  18178. {0xa4, 0xa4, 0x78, 0x2b, 0xcf, 0xfd, 0x3e, 0xc5,
  18179. 0xe7, 0xef, 0x6d, 0x8c, 0x34, 0xa5, 0x61, 0x23}, 16,
  18180. /* iv, iv length */
  18181. {0xb7, 0x81, 0xfc, 0xf2, 0xf7, 0x5f, 0xa5, 0xa8,
  18182. 0xde, 0x97, 0xa9, 0xca, 0x48, 0xe5, 0x22, 0xec}, 16,
  18183. /* aad, aad length */
  18184. {0x89, 0x9a, 0x17, 0x58, 0x97, 0x56, 0x1d, 0x7e}, 8,
  18185. /* msg, msg length */
  18186. {0x6c, 0xf3, 0x67, 0x20, 0x87, 0x2b, 0x85, 0x13,
  18187. 0xf6, 0xea, 0xb1, 0xa8, 0xa4, 0x44, 0x38, 0xd5,
  18188. 0xef, 0x11}, 18,
  18189. /* ct, ct length */
  18190. {0x0d, 0xe1, 0x8f, 0xd0, 0xfd, 0xd9, 0x1e, 0x7a,
  18191. 0xf1, 0x9f, 0x1d, 0x8e, 0xe8, 0x73, 0x39, 0x38,
  18192. 0xb1, 0xe8}, 18,
  18193. /* tag, tag length */
  18194. {0xe7, 0xf6, 0xd2, 0x23, 0x16, 0x18, 0x10, 0x2f,
  18195. 0xdb, 0x7f, 0xe5, 0x5f, 0xf1, 0x99, 0x17, 0x00}, 16,
  18196. /* valid */
  18197. 1,
  18198. },
  18199. {
  18200. /* key, key length */
  18201. {0x83, 0x95, 0xfc, 0xf1, 0xe9, 0x5b, 0xeb, 0xd6,
  18202. 0x97, 0xbd, 0x01, 0x0b, 0xc7, 0x66, 0xaa, 0xc3}, 16,
  18203. /* iv, iv length */
  18204. {0x22, 0xe7, 0xad, 0xd9, 0x3c, 0xfc, 0x63, 0x93,
  18205. 0xc5, 0x7e, 0xc0, 0xb3, 0xc1, 0x7d, 0x6b, 0x44}, 16,
  18206. /* aad, aad length */
  18207. {0x12, 0x67, 0x35, 0xfc, 0xc3, 0x20, 0xd2, 0x5a}, 8,
  18208. /* msg, msg length */
  18209. {0xca, 0x40, 0xd7, 0x44, 0x6e, 0x54, 0x5f, 0xfa,
  18210. 0xed, 0x3b, 0xd1, 0x2a, 0x74, 0x0a, 0x65, 0x9f,
  18211. 0xfb, 0xbb, 0x3c, 0xea, 0xb7}, 21,
  18212. /* ct, ct length */
  18213. {0xcb, 0x89, 0x20, 0xf8, 0x7a, 0x6c, 0x75, 0xcf,
  18214. 0xf3, 0x96, 0x27, 0xb5, 0x6e, 0x3e, 0xd1, 0x97,
  18215. 0xc5, 0x52, 0xd2, 0x95, 0xa7}, 21,
  18216. /* tag, tag length */
  18217. {0xcf, 0xc4, 0x6a, 0xfc, 0x25, 0x3b, 0x46, 0x52,
  18218. 0xb1, 0xaf, 0x37, 0x95, 0xb1, 0x24, 0xab, 0x6e}, 16,
  18219. /* valid */
  18220. 1,
  18221. },
  18222. {
  18223. /* key, key length */
  18224. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18225. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18226. /* iv, iv length */
  18227. {0x3c, 0x8c, 0xc2, 0x97, 0x0a, 0x00, 0x8f, 0x75,
  18228. 0xcc, 0x5b, 0xea, 0xe2, 0x84, 0x72, 0x58, 0xc2}, 16,
  18229. /* aad, aad length */
  18230. {0x00}, 0,
  18231. /* msg, msg length */
  18232. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18233. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18234. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18235. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18236. /* ct, ct length */
  18237. {0x3c, 0x44, 0x1f, 0x32, 0xce, 0x07, 0x82, 0x23,
  18238. 0x64, 0xd7, 0xa2, 0x99, 0x0e, 0x50, 0xbb, 0x13,
  18239. 0xd7, 0xb0, 0x2a, 0x26, 0x96, 0x9e, 0x4a, 0x93,
  18240. 0x7e, 0x5e, 0x90, 0x73, 0xb0, 0xd9, 0xc9, 0x68}, 32,
  18241. /* tag, tag length */
  18242. {0xdb, 0x90, 0xbd, 0xb3, 0xda, 0x3d, 0x00, 0xaf,
  18243. 0xd0, 0xfc, 0x6a, 0x83, 0x55, 0x1d, 0xa9, 0x5e}, 16,
  18244. /* valid */
  18245. 1,
  18246. },
  18247. {
  18248. /* key, key length */
  18249. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18250. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18251. /* iv, iv length */
  18252. {0xae, 0xf0, 0x3d, 0x00, 0x59, 0x84, 0x94, 0xe9,
  18253. 0xfb, 0x03, 0xcd, 0x7d, 0x8b, 0x59, 0x08, 0x66}, 16,
  18254. /* aad, aad length */
  18255. {0x00}, 0,
  18256. /* msg, msg length */
  18257. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18258. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18259. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18260. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18261. /* ct, ct length */
  18262. {0xd1, 0x9a, 0xc5, 0x98, 0x49, 0x02, 0x6a, 0x91,
  18263. 0xaa, 0x1b, 0x9a, 0xec, 0x29, 0xb1, 0x1a, 0x20,
  18264. 0x2a, 0x4d, 0x73, 0x9f, 0xd8, 0x6c, 0x28, 0xe3,
  18265. 0xae, 0x3d, 0x58, 0x8e, 0xa2, 0x1d, 0x70, 0xc6}, 32,
  18266. /* tag, tag length */
  18267. {0xc3, 0x0f, 0x6c, 0xd9, 0x20, 0x20, 0x74, 0xed,
  18268. 0x6e, 0x2a, 0x2a, 0x36, 0x0e, 0xac, 0x8c, 0x47}, 16,
  18269. /* valid */
  18270. 1,
  18271. },
  18272. {
  18273. /* key, key length */
  18274. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18275. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18276. /* iv, iv length */
  18277. {0x55, 0xd1, 0x25, 0x11, 0xc6, 0x96, 0xa8, 0x0d,
  18278. 0x05, 0x14, 0xd1, 0xff, 0xba, 0x49, 0xca, 0xda}, 16,
  18279. /* aad, aad length */
  18280. {0x00}, 0,
  18281. /* msg, msg length */
  18282. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18283. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18284. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18285. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18286. /* ct, ct length */
  18287. {0x21, 0x08, 0x55, 0x8a, 0xc4, 0xb2, 0xc2, 0xd5,
  18288. 0xcc, 0x66, 0xce, 0xa5, 0x1d, 0x62, 0x10, 0xe0,
  18289. 0x46, 0x17, 0x7a, 0x67, 0x63, 0x1c, 0xd2, 0xdd,
  18290. 0x8f, 0x09, 0x46, 0x97, 0x33, 0xac, 0xb5, 0x17}, 32,
  18291. /* tag, tag length */
  18292. {0xfc, 0x35, 0x5e, 0x87, 0xa2, 0x67, 0xbe, 0x3a,
  18293. 0xe3, 0xe4, 0x4c, 0x0b, 0xf3, 0xf9, 0x9b, 0x2b}, 16,
  18294. /* valid */
  18295. 1,
  18296. },
  18297. {
  18298. /* key, key length */
  18299. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18300. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18301. /* iv, iv length */
  18302. {0x79, 0x42, 0x2d, 0xdd, 0x91, 0xc4, 0xee, 0xe2,
  18303. 0xde, 0xae, 0xf1, 0xf9, 0x68, 0x30, 0x53, 0x04}, 16,
  18304. /* aad, aad length */
  18305. {0x00}, 0,
  18306. /* msg, msg length */
  18307. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18308. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18309. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18310. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18311. /* ct, ct length */
  18312. {0x4d, 0x2c, 0x15, 0x24, 0xca, 0x4b, 0xaa, 0x4e,
  18313. 0xef, 0xcc, 0xe6, 0xb9, 0x1b, 0x22, 0x7e, 0xe8,
  18314. 0x3a, 0xba, 0xff, 0x81, 0x05, 0xdc, 0xaf, 0xa2,
  18315. 0xab, 0x19, 0x1f, 0x5d, 0xf2, 0x57, 0x50, 0x35}, 32,
  18316. /* tag, tag length */
  18317. {0xe2, 0xc8, 0x65, 0xce, 0x2d, 0x7a, 0xbd, 0xac,
  18318. 0x02, 0x4c, 0x6f, 0x99, 0x1a, 0x84, 0x83, 0x90}, 16,
  18319. /* valid */
  18320. 1,
  18321. },
  18322. {
  18323. /* key, key length */
  18324. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18325. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18326. /* iv, iv length */
  18327. {0x0a, 0xf5, 0xaa, 0x7a, 0x76, 0x76, 0xe2, 0x83,
  18328. 0x06, 0x30, 0x6b, 0xcd, 0x9b, 0xf2, 0x00, 0x3a}, 16,
  18329. /* aad, aad length */
  18330. {0x00}, 0,
  18331. /* msg, msg length */
  18332. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18333. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18334. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18335. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18336. /* ct, ct length */
  18337. {0x8e, 0xb0, 0x1e, 0x62, 0x18, 0x5d, 0x78, 0x2e,
  18338. 0xb9, 0x28, 0x7a, 0x34, 0x1a, 0x68, 0x62, 0xac,
  18339. 0x52, 0x57, 0xd6, 0xf9, 0xad, 0xc9, 0x9e, 0xe0,
  18340. 0xa2, 0x4d, 0x9c, 0x22, 0xb3, 0xe9, 0xb3, 0x8a}, 32,
  18341. /* tag, tag length */
  18342. {0x39, 0xc3, 0x39, 0xbc, 0x8a, 0x74, 0xc7, 0x5e,
  18343. 0x2c, 0x65, 0xc6, 0x11, 0x95, 0x44, 0xd6, 0x1e}, 16,
  18344. /* valid */
  18345. 1,
  18346. },
  18347. {
  18348. /* key, key length */
  18349. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18350. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18351. /* iv, iv length */
  18352. {0xaf, 0x5a, 0x03, 0xae, 0x7e, 0xdd, 0x73, 0x47,
  18353. 0x1b, 0xdc, 0xdf, 0xac, 0x5e, 0x19, 0x4a, 0x60}, 16,
  18354. /* aad, aad length */
  18355. {0x00}, 0,
  18356. /* msg, msg length */
  18357. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18358. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18359. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18360. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18361. /* ct, ct length */
  18362. {0x94, 0xc5, 0xd2, 0xac, 0xa6, 0xdb, 0xbc, 0xe8,
  18363. 0xc2, 0x45, 0x13, 0xa2, 0x5e, 0x09, 0x5c, 0x0e,
  18364. 0x54, 0xa9, 0x42, 0x86, 0x0d, 0x32, 0x7a, 0x22,
  18365. 0x2a, 0x81, 0x5c, 0xc7, 0x13, 0xb1, 0x63, 0xb4}, 32,
  18366. /* tag, tag length */
  18367. {0xf5, 0x0b, 0x30, 0x30, 0x4e, 0x45, 0xc9, 0xd4,
  18368. 0x11, 0xe8, 0xdf, 0x45, 0x08, 0xa9, 0x86, 0x12}, 16,
  18369. /* valid */
  18370. 1,
  18371. },
  18372. {
  18373. /* key, key length */
  18374. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18375. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18376. /* iv, iv length */
  18377. {0xb3, 0x70, 0x87, 0x68, 0x0f, 0x0e, 0xdd, 0x5a,
  18378. 0x52, 0x22, 0x8b, 0x8c, 0x7a, 0xae, 0xa6, 0x64}, 16,
  18379. /* aad, aad length */
  18380. {0x00}, 0,
  18381. /* msg, msg length */
  18382. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18383. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18384. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18385. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18386. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18387. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18388. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18389. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33}, 64,
  18390. /* ct, ct length */
  18391. {0x3b, 0xb6, 0x17, 0x3e, 0x37, 0x72, 0xd4, 0xb6,
  18392. 0x2e, 0xef, 0x37, 0xf9, 0xef, 0x07, 0x81, 0xf3,
  18393. 0x60, 0xb6, 0xc7, 0x4b, 0xe3, 0xbf, 0x6b, 0x37,
  18394. 0x10, 0x67, 0xbc, 0x1b, 0x09, 0x0d, 0x9d, 0x66,
  18395. 0x22, 0xa1, 0xfb, 0xec, 0x6a, 0xc4, 0x71, 0xb3,
  18396. 0x34, 0x9c, 0xd4, 0x27, 0x7a, 0x10, 0x1d, 0x40,
  18397. 0x89, 0x0f, 0xbf, 0x27, 0xdf, 0xdc, 0xd0, 0xb4,
  18398. 0xe3, 0x78, 0x1f, 0x98, 0x06, 0xda, 0xab, 0xb6}, 64,
  18399. /* tag, tag length */
  18400. {0xa0, 0x49, 0x87, 0x45, 0xe5, 0x99, 0x99, 0xdd,
  18401. 0xc3, 0x2d, 0x5b, 0x14, 0x02, 0x41, 0x12, 0x4e}, 16,
  18402. /* valid */
  18403. 1,
  18404. },
  18405. {
  18406. /* key, key length */
  18407. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18408. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18409. /* iv, iv length */
  18410. {0x4f, 0x80, 0x2d, 0xa6, 0x2a, 0x38, 0x45, 0x55,
  18411. 0xa1, 0x9b, 0xc2, 0xb3, 0x82, 0xeb, 0x25, 0xaf}, 16,
  18412. /* aad, aad length */
  18413. {0x00}, 0,
  18414. /* msg, msg length */
  18415. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18416. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18417. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18418. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18419. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18420. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18421. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18422. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18423. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  18424. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44}, 80,
  18425. /* ct, ct length */
  18426. {0xe9, 0xb0, 0xbb, 0x88, 0x57, 0x81, 0x8c, 0xe3,
  18427. 0x20, 0x1c, 0x36, 0x90, 0xd2, 0x1d, 0xaa, 0x7f,
  18428. 0x26, 0x4f, 0xb8, 0xee, 0x93, 0xcc, 0x7a, 0x46,
  18429. 0x74, 0xea, 0x2f, 0xc3, 0x2b, 0xf1, 0x82, 0xfb,
  18430. 0x2a, 0x7e, 0x8a, 0xd5, 0x15, 0x07, 0xad, 0x4f,
  18431. 0x31, 0xce, 0xfc, 0x23, 0x56, 0xfe, 0x79, 0x36,
  18432. 0xa7, 0xf6, 0xe1, 0x9f, 0x95, 0xe8, 0x8f, 0xdb,
  18433. 0xf1, 0x76, 0x20, 0x91, 0x6d, 0x3a, 0x6f, 0x3d,
  18434. 0x01, 0xfc, 0x17, 0xd3, 0x58, 0x67, 0x2f, 0x77,
  18435. 0x7f, 0xd4, 0x09, 0x92, 0x46, 0xe4, 0x36, 0xe1}, 80,
  18436. /* tag, tag length */
  18437. {0x67, 0x91, 0x0b, 0xe7, 0x44, 0xb8, 0x31, 0x5a,
  18438. 0xe0, 0xeb, 0x61, 0x24, 0x59, 0x0c, 0x5d, 0x8b}, 16,
  18439. /* valid */
  18440. 1,
  18441. },
  18442. {
  18443. /* key, key length */
  18444. {0xb6, 0x7b, 0x1a, 0x6e, 0xfd, 0xd4, 0x0d, 0x37,
  18445. 0x08, 0x0f, 0xbe, 0x8f, 0x80, 0x47, 0xae, 0xb9}, 16,
  18446. /* iv, iv length */
  18447. {0xfa, 0x29, 0x4b, 0x12, 0x99, 0x72, 0xf7, 0xfc,
  18448. 0x5b, 0xbd, 0x5b, 0x96, 0xbb, 0xa8, 0x37, 0xc9}, 16,
  18449. /* aad, aad length */
  18450. {0x00}, 0,
  18451. /* msg, msg length */
  18452. {0x00}, 0,
  18453. /* ct, ct length */
  18454. {0x00}, 0,
  18455. /* tag, tag length */
  18456. {0xb1, 0x4b, 0x64, 0xfb, 0x58, 0x98, 0x99, 0x69,
  18457. 0x95, 0x70, 0xcc, 0x91, 0x60, 0xe3, 0x98, 0x96}, 16,
  18458. /* valid */
  18459. 1,
  18460. },
  18461. {
  18462. /* key, key length */
  18463. {0x20, 0x9e, 0x6d, 0xbf, 0x2a, 0xd2, 0x6a, 0x10,
  18464. 0x54, 0x45, 0xfc, 0x02, 0x07, 0xcd, 0x9e, 0x9a}, 16,
  18465. /* iv, iv length */
  18466. {0x94, 0x77, 0x84, 0x9d, 0x6c, 0xcd, 0xfc, 0xa1,
  18467. 0x12, 0xd9, 0x2e, 0x53, 0xfa, 0xe4, 0xa7, 0xca}, 16,
  18468. /* aad, aad length */
  18469. {0x00}, 0,
  18470. /* msg, msg length */
  18471. {0x01}, 1,
  18472. /* ct, ct length */
  18473. {0x1d}, 1,
  18474. /* tag, tag length */
  18475. {0x52, 0xa5, 0xf6, 0x00, 0xfe, 0x53, 0x38, 0x02,
  18476. 0x6a, 0x7c, 0xb0, 0x9c, 0x11, 0x64, 0x00, 0x82}, 16,
  18477. /* valid */
  18478. 1,
  18479. },
  18480. {
  18481. /* key, key length */
  18482. {0xa5, 0x49, 0x44, 0x2e, 0x35, 0x15, 0x40, 0x32,
  18483. 0xd0, 0x7c, 0x86, 0x66, 0x00, 0x6a, 0xa6, 0xa2}, 16,
  18484. /* iv, iv length */
  18485. {0x51, 0x71, 0x52, 0x45, 0x68, 0xe8, 0x1d, 0x97,
  18486. 0xe8, 0xc4, 0xde, 0x4b, 0xa5, 0x6c, 0x10, 0xa0}, 16,
  18487. /* aad, aad length */
  18488. {0x00}, 0,
  18489. /* msg, msg length */
  18490. {0x11, 0x82, 0xe9, 0x35, 0x96, 0xca, 0xc5, 0x60,
  18491. 0x89, 0x46, 0x40, 0x0b, 0xc7, 0x3f, 0x3a}, 15,
  18492. /* ct, ct length */
  18493. {0xd7, 0xb8, 0xa6, 0xb4, 0x3d, 0x2e, 0x9f, 0x98,
  18494. 0xc2, 0xb4, 0x4c, 0xe5, 0xe3, 0xcf, 0xdb}, 15,
  18495. /* tag, tag length */
  18496. {0x1b, 0xdd, 0x52, 0xfc, 0x98, 0x7d, 0xaf, 0x0e,
  18497. 0xe1, 0x92, 0x34, 0xc9, 0x05, 0xea, 0x64, 0x5f}, 16,
  18498. /* valid */
  18499. 1,
  18500. },
  18501. {
  18502. /* key, key length */
  18503. {0x95, 0x8b, 0xcd, 0xb6, 0x6a, 0x39, 0x52, 0xb5,
  18504. 0x37, 0x01, 0x58, 0x2a, 0x68, 0xa0, 0xe4, 0x74}, 16,
  18505. /* iv, iv length */
  18506. {0x0e, 0x6e, 0xc8, 0x79, 0xb0, 0x2c, 0x6f, 0x51,
  18507. 0x69, 0x76, 0xe3, 0x58, 0x98, 0x42, 0x8d, 0xa7}, 16,
  18508. /* aad, aad length */
  18509. {0x00}, 0,
  18510. /* msg, msg length */
  18511. {0x14, 0x04, 0x15, 0x82, 0x3e, 0xcc, 0x89, 0x32,
  18512. 0xa0, 0x58, 0x38, 0x4b, 0x73, 0x8e, 0xa6, 0xea,
  18513. 0x6d, 0x4d, 0xfe, 0x3b, 0xbe, 0xee}, 22,
  18514. /* ct, ct length */
  18515. {0x73, 0xe5, 0xc6, 0xf0, 0xe7, 0x03, 0xa5, 0x2d,
  18516. 0x02, 0xf7, 0xf7, 0xfa, 0xeb, 0x1b, 0x77, 0xfd,
  18517. 0x4f, 0xd0, 0xcb, 0x42, 0x1e, 0xaf}, 22,
  18518. /* tag, tag length */
  18519. {0x6c, 0x15, 0x4a, 0x85, 0x96, 0x8e, 0xdd, 0x74,
  18520. 0x77, 0x65, 0x75, 0xa4, 0x45, 0x0b, 0xd8, 0x97}, 16,
  18521. /* valid */
  18522. 1,
  18523. },
  18524. {
  18525. /* key, key length */
  18526. {0x96, 0x5b, 0x75, 0x7b, 0xa5, 0x01, 0x8a, 0x8d,
  18527. 0x66, 0xed, 0xc7, 0x8e, 0x0c, 0xee, 0xe8, 0x6b}, 16,
  18528. /* iv, iv length */
  18529. {0x2e, 0x35, 0x90, 0x1a, 0xe7, 0xd4, 0x91, 0xee,
  18530. 0xcc, 0x88, 0x38, 0xfe, 0xdd, 0x63, 0x14, 0x05}, 16,
  18531. /* aad, aad length */
  18532. {0xdf, 0x10, 0xd0, 0xd2, 0x12, 0x24, 0x24, 0x50}, 8,
  18533. /* msg, msg length */
  18534. {0x36, 0xe5, 0x7a, 0x76, 0x39, 0x58, 0xb0, 0x2c,
  18535. 0xea, 0x9d, 0x6a, 0x67, 0x6e, 0xbc, 0xe8, 0x1f}, 16,
  18536. /* ct, ct length */
  18537. {0x93, 0x6b, 0x69, 0xb6, 0xc9, 0x55, 0xad, 0xfd,
  18538. 0x15, 0x53, 0x9b, 0x9b, 0xe4, 0x98, 0x9c, 0xb6}, 16,
  18539. /* tag, tag length */
  18540. {0xee, 0x15, 0xa1, 0x45, 0x4e, 0x88, 0xfa, 0xad,
  18541. 0x8e, 0x48, 0xa8, 0xdf, 0x29, 0x83, 0xb4, 0x25}, 16,
  18542. /* valid */
  18543. 1,
  18544. },
  18545. {
  18546. /* key, key length */
  18547. {0x88, 0xd0, 0x20, 0x33, 0x78, 0x1c, 0x7b, 0x41,
  18548. 0x64, 0x71, 0x1a, 0x05, 0x42, 0x0f, 0x25, 0x6e}, 16,
  18549. /* iv, iv length */
  18550. {0x7f, 0x29, 0x85, 0x29, 0x63, 0x15, 0x50, 0x7a,
  18551. 0xa4, 0xc0, 0xa9, 0x3d, 0x5c, 0x12, 0xbd, 0x77}, 16,
  18552. /* aad, aad length */
  18553. {0x7c, 0x57, 0x1d, 0x2f, 0xbb, 0x5f, 0x62, 0x52,
  18554. 0x3c, 0x0e, 0xb3, 0x38, 0xbe, 0xf9, 0xa9}, 15,
  18555. /* msg, msg length */
  18556. {0xd9, 0x8a, 0xdc, 0x03, 0xd9, 0xd5, 0x82, 0x73,
  18557. 0x2e, 0xb0, 0x7d, 0xf2, 0x3d, 0x7b, 0x9f, 0x74}, 16,
  18558. /* ct, ct length */
  18559. {0x67, 0xca, 0xac, 0x35, 0x44, 0x3a, 0x31, 0x38,
  18560. 0xd2, 0xcb, 0x81, 0x1f, 0x0c, 0xe0, 0x4d, 0xd2}, 16,
  18561. /* tag, tag length */
  18562. {0xb7, 0x96, 0x8e, 0x0b, 0x56, 0x40, 0xe3, 0xb2,
  18563. 0x36, 0x56, 0x96, 0x53, 0x20, 0x8b, 0x9d, 0xeb}, 16,
  18564. /* valid */
  18565. 1,
  18566. },
  18567. {
  18568. /* key, key length */
  18569. {0x51, 0x58, 0x40, 0xcf, 0x67, 0xd2, 0xe4, 0x0e,
  18570. 0xb6, 0x5e, 0x54, 0xa2, 0x4c, 0x72, 0xcb, 0xf2}, 16,
  18571. /* iv, iv length */
  18572. {0xbf, 0x47, 0xaf, 0xdf, 0xd4, 0x92, 0x13, 0x7a,
  18573. 0x24, 0x23, 0x6b, 0xc3, 0x67, 0x97, 0xa8, 0x8e}, 16,
  18574. /* aad, aad length */
  18575. {0x16, 0x84, 0x3c, 0x09, 0x1d, 0x43, 0xb0, 0xa1,
  18576. 0x91, 0xd0, 0xc7, 0x3d, 0x15, 0x60, 0x1b, 0xe9}, 16,
  18577. /* msg, msg length */
  18578. {0xc8, 0x34, 0x58, 0x8c, 0xb6, 0xda, 0xf9, 0xf0,
  18579. 0x6d, 0xd2, 0x35, 0x19, 0xf4, 0xbe, 0x9f, 0x56}, 16,
  18580. /* ct, ct length */
  18581. {0x20, 0x0a, 0xc4, 0x51, 0xfb, 0xeb, 0x0f, 0x61,
  18582. 0x51, 0xd6, 0x15, 0x83, 0xa4, 0x3b, 0x73, 0x43}, 16,
  18583. /* tag, tag length */
  18584. {0x2a, 0xd4, 0x3e, 0x4c, 0xaa, 0x51, 0x98, 0x3a,
  18585. 0x9d, 0x4d, 0x24, 0x48, 0x1b, 0xf4, 0xc8, 0x39}, 16,
  18586. /* valid */
  18587. 1,
  18588. },
  18589. {
  18590. /* key, key length */
  18591. {0x2e, 0x44, 0x92, 0xd4, 0x44, 0xe5, 0xb6, 0xf4,
  18592. 0xce, 0xc8, 0xc2, 0xd3, 0x61, 0x5a, 0xc8, 0x58}, 16,
  18593. /* iv, iv length */
  18594. {0xd0, 0x2b, 0xf0, 0x76, 0x3a, 0x9f, 0xef, 0xbf,
  18595. 0x70, 0xc3, 0x3a, 0xee, 0x1e, 0x9d, 0xa1, 0xd6}, 16,
  18596. /* aad, aad length */
  18597. {0x90, 0x4d, 0x86, 0xf1, 0x33, 0xce, 0xc1, 0x5a,
  18598. 0x0c, 0x3c, 0xaf, 0x14, 0xd7, 0xe0, 0x29, 0xc8,
  18599. 0x2a, 0x07, 0x70, 0x5a, 0x23, 0xf0, 0xd0, 0x80}, 24,
  18600. /* msg, msg length */
  18601. {0x9e, 0x62, 0xd6, 0x51, 0x1b, 0x0b, 0xda, 0x7d,
  18602. 0xd7, 0x74, 0x0b, 0x61, 0x4d, 0x97, 0xba, 0xe0}, 16,
  18603. /* ct, ct length */
  18604. {0x27, 0xc6, 0xe9, 0xa6, 0x53, 0xc5, 0x25, 0x3c,
  18605. 0xa1, 0xc5, 0x67, 0x3f, 0x97, 0xb9, 0xb3, 0x3e}, 16,
  18606. /* tag, tag length */
  18607. {0x2d, 0x58, 0x12, 0x71, 0xe1, 0xfa, 0x9e, 0x36,
  18608. 0x86, 0x13, 0x6c, 0xaa, 0x8f, 0x4d, 0x6c, 0x8e}, 16,
  18609. /* valid */
  18610. 1,
  18611. },
  18612. {
  18613. /* key, key length */
  18614. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18615. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18616. /* iv, iv length */
  18617. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18618. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18619. /* aad, aad length */
  18620. {0x00}, 0,
  18621. /* msg, msg length */
  18622. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18623. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18624. /* ct, ct length */
  18625. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18626. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18627. /* tag, tag length */
  18628. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18629. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18630. /* valid */
  18631. 0,
  18632. },
  18633. {
  18634. /* key, key length */
  18635. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18636. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18637. /* iv, iv length */
  18638. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18639. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18640. /* aad, aad length */
  18641. {0x00}, 0,
  18642. /* msg, msg length */
  18643. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18644. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18645. /* ct, ct length */
  18646. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18647. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18648. /* tag, tag length */
  18649. {0xe4, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18650. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18651. /* valid */
  18652. 0,
  18653. },
  18654. {
  18655. /* key, key length */
  18656. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18657. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18658. /* iv, iv length */
  18659. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18660. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18661. /* aad, aad length */
  18662. {0x00}, 0,
  18663. /* msg, msg length */
  18664. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18665. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18666. /* ct, ct length */
  18667. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18668. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18669. /* tag, tag length */
  18670. {0x66, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18671. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18672. /* valid */
  18673. 0,
  18674. },
  18675. {
  18676. /* key, key length */
  18677. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18678. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18679. /* iv, iv length */
  18680. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18681. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18682. /* aad, aad length */
  18683. {0x00}, 0,
  18684. /* msg, msg length */
  18685. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18686. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18687. /* ct, ct length */
  18688. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18689. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18690. /* tag, tag length */
  18691. {0xe6, 0x0f, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18692. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18693. /* valid */
  18694. 0,
  18695. },
  18696. {
  18697. /* key, key length */
  18698. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18699. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18700. /* iv, iv length */
  18701. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18702. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18703. /* aad, aad length */
  18704. {0x00}, 0,
  18705. /* msg, msg length */
  18706. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18707. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18708. /* ct, ct length */
  18709. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18710. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18711. /* tag, tag length */
  18712. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0xf2,
  18713. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18714. /* valid */
  18715. 0,
  18716. },
  18717. {
  18718. /* key, key length */
  18719. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18720. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18721. /* iv, iv length */
  18722. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18723. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18724. /* aad, aad length */
  18725. {0x00}, 0,
  18726. /* msg, msg length */
  18727. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18728. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18729. /* ct, ct length */
  18730. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18731. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18732. /* tag, tag length */
  18733. {0xe6, 0x0e, 0x7c, 0x50, 0x12, 0xa6, 0xdb, 0xf2,
  18734. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18735. /* valid */
  18736. 0,
  18737. },
  18738. {
  18739. /* key, key length */
  18740. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18741. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18742. /* iv, iv length */
  18743. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18744. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18745. /* aad, aad length */
  18746. {0x00}, 0,
  18747. /* msg, msg length */
  18748. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18749. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18750. /* ct, ct length */
  18751. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18752. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18753. /* tag, tag length */
  18754. {0xe6, 0x0e, 0x7c, 0x50, 0x11, 0xa6, 0xdb, 0xf2,
  18755. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18756. /* valid */
  18757. 0,
  18758. },
  18759. {
  18760. /* key, key length */
  18761. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18762. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18763. /* iv, iv length */
  18764. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18765. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18766. /* aad, aad length */
  18767. {0x00}, 0,
  18768. /* msg, msg length */
  18769. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18770. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18771. /* ct, ct length */
  18772. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18773. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18774. /* tag, tag length */
  18775. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  18776. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18777. /* valid */
  18778. 0,
  18779. },
  18780. {
  18781. /* key, key length */
  18782. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18783. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18784. /* iv, iv length */
  18785. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18786. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18787. /* aad, aad length */
  18788. {0x00}, 0,
  18789. /* msg, msg length */
  18790. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18791. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18792. /* ct, ct length */
  18793. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18794. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18795. /* tag, tag length */
  18796. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18797. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18798. /* valid */
  18799. 0,
  18800. },
  18801. {
  18802. /* key, key length */
  18803. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18804. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18805. /* iv, iv length */
  18806. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18807. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18808. /* aad, aad length */
  18809. {0x00}, 0,
  18810. /* msg, msg length */
  18811. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18812. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18813. /* ct, ct length */
  18814. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18815. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18816. /* tag, tag length */
  18817. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18818. 0xd2, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18819. /* valid */
  18820. 0,
  18821. },
  18822. {
  18823. /* key, key length */
  18824. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18825. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18826. /* iv, iv length */
  18827. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18828. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18829. /* aad, aad length */
  18830. {0x00}, 0,
  18831. /* msg, msg length */
  18832. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18833. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18834. /* ct, ct length */
  18835. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18836. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18837. /* tag, tag length */
  18838. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18839. 0x52, 0xb8, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18840. /* valid */
  18841. 0,
  18842. },
  18843. {
  18844. /* key, key length */
  18845. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18846. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18847. /* iv, iv length */
  18848. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18849. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18850. /* aad, aad length */
  18851. {0x00}, 0,
  18852. /* msg, msg length */
  18853. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18854. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18855. /* ct, ct length */
  18856. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18857. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18858. /* tag, tag length */
  18859. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18860. 0x52, 0x98, 0xb0, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18861. /* valid */
  18862. 0,
  18863. },
  18864. {
  18865. /* key, key length */
  18866. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18867. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18868. /* iv, iv length */
  18869. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18870. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18871. /* aad, aad length */
  18872. {0x00}, 0,
  18873. /* msg, msg length */
  18874. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18875. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18876. /* ct, ct length */
  18877. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18878. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18879. /* tag, tag length */
  18880. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18881. 0x52, 0x98, 0xb1, 0x92, 0x9a, 0xc3, 0x56, 0xa7}, 16,
  18882. /* valid */
  18883. 0,
  18884. },
  18885. {
  18886. /* key, key length */
  18887. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18888. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18889. /* iv, iv length */
  18890. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18891. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18892. /* aad, aad length */
  18893. {0x00}, 0,
  18894. /* msg, msg length */
  18895. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18896. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18897. /* ct, ct length */
  18898. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18899. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18900. /* tag, tag length */
  18901. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18902. 0x52, 0x98, 0xb1, 0x92, 0x99, 0xc3, 0x56, 0xa7}, 16,
  18903. /* valid */
  18904. 0,
  18905. },
  18906. {
  18907. /* key, key length */
  18908. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18909. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18910. /* iv, iv length */
  18911. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18912. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18913. /* aad, aad length */
  18914. {0x00}, 0,
  18915. /* msg, msg length */
  18916. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18917. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18918. /* ct, ct length */
  18919. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18920. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18921. /* tag, tag length */
  18922. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18923. 0x52, 0x98, 0xb1, 0x92, 0x1b, 0xc3, 0x56, 0xa7}, 16,
  18924. /* valid */
  18925. 0,
  18926. },
  18927. {
  18928. /* key, key length */
  18929. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18930. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18931. /* iv, iv length */
  18932. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18933. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18934. /* aad, aad length */
  18935. {0x00}, 0,
  18936. /* msg, msg length */
  18937. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18938. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18939. /* ct, ct length */
  18940. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18941. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18942. /* tag, tag length */
  18943. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18944. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa6}, 16,
  18945. /* valid */
  18946. 0,
  18947. },
  18948. {
  18949. /* key, key length */
  18950. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18951. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18952. /* iv, iv length */
  18953. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18954. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18955. /* aad, aad length */
  18956. {0x00}, 0,
  18957. /* msg, msg length */
  18958. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18959. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18960. /* ct, ct length */
  18961. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18962. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18963. /* tag, tag length */
  18964. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18965. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa5}, 16,
  18966. /* valid */
  18967. 0,
  18968. },
  18969. {
  18970. /* key, key length */
  18971. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18972. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18973. /* iv, iv length */
  18974. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18975. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18976. /* aad, aad length */
  18977. {0x00}, 0,
  18978. /* msg, msg length */
  18979. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18980. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18981. /* ct, ct length */
  18982. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18983. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18984. /* tag, tag length */
  18985. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18986. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xe7}, 16,
  18987. /* valid */
  18988. 0,
  18989. },
  18990. {
  18991. /* key, key length */
  18992. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18993. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18994. /* iv, iv length */
  18995. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18996. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18997. /* aad, aad length */
  18998. {0x00}, 0,
  18999. /* msg, msg length */
  19000. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19001. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19002. /* ct, ct length */
  19003. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19004. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19005. /* tag, tag length */
  19006. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19007. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  19008. /* valid */
  19009. 0,
  19010. },
  19011. {
  19012. /* key, key length */
  19013. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19014. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19015. /* iv, iv length */
  19016. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19017. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19018. /* aad, aad length */
  19019. {0x00}, 0,
  19020. /* msg, msg length */
  19021. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19022. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19023. /* ct, ct length */
  19024. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19025. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19026. /* tag, tag length */
  19027. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19028. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19029. /* valid */
  19030. 0,
  19031. },
  19032. {
  19033. /* key, key length */
  19034. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19035. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19036. /* iv, iv length */
  19037. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19038. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19039. /* aad, aad length */
  19040. {0x00}, 0,
  19041. /* msg, msg length */
  19042. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19043. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19044. /* ct, ct length */
  19045. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19046. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19047. /* tag, tag length */
  19048. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0x72,
  19049. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19050. /* valid */
  19051. 0,
  19052. },
  19053. {
  19054. /* key, key length */
  19055. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19056. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19057. /* iv, iv length */
  19058. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19059. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19060. /* aad, aad length */
  19061. {0x00}, 0,
  19062. /* msg, msg length */
  19063. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19064. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19065. /* ct, ct length */
  19066. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19067. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19068. /* tag, tag length */
  19069. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  19070. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  19071. /* valid */
  19072. 0,
  19073. },
  19074. {
  19075. /* key, key length */
  19076. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19077. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19078. /* iv, iv length */
  19079. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19080. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19081. /* aad, aad length */
  19082. {0x00}, 0,
  19083. /* msg, msg length */
  19084. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19085. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19086. /* ct, ct length */
  19087. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19088. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19089. /* tag, tag length */
  19090. {0x19, 0xf1, 0x83, 0xaf, 0xec, 0x59, 0x24, 0x0d,
  19091. 0xad, 0x67, 0x4e, 0x6d, 0x64, 0x3c, 0xa9, 0x58}, 16,
  19092. /* valid */
  19093. 0,
  19094. },
  19095. {
  19096. /* key, key length */
  19097. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19098. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19099. /* iv, iv length */
  19100. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19101. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19102. /* aad, aad length */
  19103. {0x00}, 0,
  19104. /* msg, msg length */
  19105. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19106. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19107. /* ct, ct length */
  19108. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19109. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19110. /* tag, tag length */
  19111. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  19112. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 16,
  19113. /* valid */
  19114. 0,
  19115. },
  19116. {
  19117. /* key, key length */
  19118. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19119. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19120. /* iv, iv length */
  19121. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19122. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19123. /* aad, aad length */
  19124. {0x00}, 0,
  19125. /* msg, msg length */
  19126. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19127. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19128. /* ct, ct length */
  19129. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19130. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19131. /* tag, tag length */
  19132. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  19133. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 16,
  19134. /* valid */
  19135. 0,
  19136. },
  19137. {
  19138. /* key, key length */
  19139. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19140. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19141. /* iv, iv length */
  19142. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19143. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19144. /* aad, aad length */
  19145. {0x00}, 0,
  19146. /* msg, msg length */
  19147. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19148. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19149. /* ct, ct length */
  19150. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19151. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19152. /* tag, tag length */
  19153. {0x66, 0x8e, 0xfc, 0xd0, 0x93, 0x26, 0x5b, 0x72,
  19154. 0xd2, 0x18, 0x31, 0x12, 0x1b, 0x43, 0xd6, 0x27}, 16,
  19155. /* valid */
  19156. 0,
  19157. },
  19158. {
  19159. /* key, key length */
  19160. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19161. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19162. /* iv, iv length */
  19163. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19164. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19165. /* aad, aad length */
  19166. {0x00}, 0,
  19167. /* msg, msg length */
  19168. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19169. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19170. /* ct, ct length */
  19171. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19172. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19173. /* tag, tag length */
  19174. {0xe7, 0x0f, 0x7d, 0x51, 0x12, 0xa7, 0xda, 0xf3,
  19175. 0x53, 0x99, 0xb0, 0x93, 0x9a, 0xc2, 0x57, 0xa6}, 16,
  19176. /* valid */
  19177. 0,
  19178. },
  19179. };
  19180. byte ciphertext[sizeof(vectors[0].ct)];
  19181. byte authtag[sizeof(vectors[0].tag)];
  19182. int i;
  19183. int len;
  19184. int ret;
  19185. for (i = 0; i < (int)(sizeof(vectors)/sizeof(vectors[0])); i++) {
  19186. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  19187. len = sizeof(authtag);
  19188. ExpectIntEQ(wc_AesEaxEncryptAuth(vectors[i].key, vectors[i].key_length,
  19189. ciphertext,
  19190. vectors[i].msg, vectors[i].msg_length,
  19191. vectors[i].iv, vectors[i].iv_length,
  19192. authtag, len,
  19193. vectors[i].aad, vectors[i].aad_length),
  19194. 0);
  19195. /* check ciphertext matches vector */
  19196. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].ct, vectors[i].ct_length),
  19197. 0);
  19198. /* check that computed tag matches vector only for vectors marked as valid */
  19199. ret = XMEMCMP(authtag, vectors[i].tag, len);
  19200. if (vectors[i].valid) {
  19201. ExpectIntEQ(ret, 0);
  19202. }
  19203. else {
  19204. ExpectIntNE(ret, 0);
  19205. }
  19206. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  19207. /* Decrypt, checking that the computed auth tags match */
  19208. ExpectIntEQ(wc_AesEaxDecryptAuth(vectors[i].key, vectors[i].key_length,
  19209. ciphertext,
  19210. vectors[i].ct, vectors[i].ct_length,
  19211. vectors[i].iv, vectors[i].iv_length,
  19212. authtag, len,
  19213. vectors[i].aad, vectors[i].aad_length),
  19214. 0);
  19215. /* check decrypted ciphertext matches vector plaintext */
  19216. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].msg, vectors[i].msg_length),
  19217. 0);
  19218. }
  19219. return EXPECT_RESULT();
  19220. } /* END test_wc_AesEaxVectors */
  19221. /*
  19222. * Testing test_wc_AesEaxEncryptAuth()
  19223. */
  19224. static int test_wc_AesEaxEncryptAuth(void)
  19225. {
  19226. EXPECT_DECLS;
  19227. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19228. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19229. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19230. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19231. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19232. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19233. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19234. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19235. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  19236. const byte msg[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  19237. byte ciphertext[sizeof(msg)];
  19238. byte authtag[AES_BLOCK_SIZE];
  19239. int i;
  19240. int len;
  19241. len = sizeof(authtag);
  19242. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19243. ciphertext,
  19244. msg, sizeof(msg),
  19245. iv, sizeof(iv),
  19246. authtag, len,
  19247. aad, sizeof(aad)),
  19248. 0);
  19249. /* Test null checking */
  19250. ExpectIntEQ(wc_AesEaxEncryptAuth(NULL, sizeof(key),
  19251. ciphertext,
  19252. msg, sizeof(msg),
  19253. iv, sizeof(iv),
  19254. authtag, len,
  19255. aad, sizeof(aad)),
  19256. BAD_FUNC_ARG);
  19257. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19258. NULL,
  19259. msg, sizeof(msg),
  19260. iv, sizeof(iv),
  19261. authtag, len,
  19262. aad, sizeof(aad)),
  19263. BAD_FUNC_ARG);
  19264. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19265. ciphertext,
  19266. NULL, sizeof(msg),
  19267. iv, sizeof(iv),
  19268. authtag, len,
  19269. aad, sizeof(aad)),
  19270. BAD_FUNC_ARG);
  19271. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19272. ciphertext,
  19273. msg, sizeof(msg),
  19274. NULL, sizeof(iv),
  19275. authtag, len,
  19276. aad, sizeof(aad)),
  19277. BAD_FUNC_ARG);
  19278. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19279. ciphertext,
  19280. msg, sizeof(msg),
  19281. iv, sizeof(iv),
  19282. NULL, len,
  19283. aad, sizeof(aad)),
  19284. BAD_FUNC_ARG);
  19285. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19286. ciphertext,
  19287. msg, sizeof(msg),
  19288. iv, sizeof(iv),
  19289. authtag, len,
  19290. NULL, sizeof(aad)),
  19291. BAD_FUNC_ARG);
  19292. /* Test bad key lengths */
  19293. for (i = 0; i <= 32; i++) {
  19294. int exp_ret;
  19295. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  19296. || i == AES_256_KEY_SIZE) {
  19297. exp_ret = 0;
  19298. }
  19299. else {
  19300. exp_ret = BAD_FUNC_ARG;
  19301. }
  19302. ExpectIntEQ(wc_AesEaxEncryptAuth(key, i,
  19303. ciphertext,
  19304. msg, sizeof(msg),
  19305. iv, sizeof(iv),
  19306. authtag, len,
  19307. aad, sizeof(aad)),
  19308. exp_ret);
  19309. }
  19310. /* Test auth tag size out of range */
  19311. len = AES_BLOCK_SIZE + 1;
  19312. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19313. ciphertext,
  19314. msg, sizeof(msg),
  19315. iv, sizeof(iv),
  19316. authtag, len,
  19317. aad, sizeof(aad)),
  19318. BAD_FUNC_ARG);
  19319. return EXPECT_RESULT();
  19320. } /* END test_wc_AesEaxEncryptAuth() */
  19321. /*
  19322. * Testing test_wc_AesEaxDecryptAuth()
  19323. */
  19324. static int test_wc_AesEaxDecryptAuth(void)
  19325. {
  19326. EXPECT_DECLS;
  19327. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19328. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19329. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19330. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19331. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19332. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19333. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19334. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19335. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  19336. const byte ct[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  19337. /* Garbage tag that should always fail for above aad */
  19338. const byte tag[] = {0xFE, 0xED, 0xBE, 0xEF, 0xDE, 0xAD, 0xC0, 0xDE,
  19339. 0xCA, 0xFE, 0xBE, 0xEF, 0xDE, 0xAF, 0xBE, 0xEF};
  19340. byte plaintext[sizeof(ct)];
  19341. int i;
  19342. int len;
  19343. len = sizeof(tag);
  19344. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19345. plaintext,
  19346. ct, sizeof(ct),
  19347. iv, sizeof(iv),
  19348. tag, len,
  19349. aad, sizeof(aad)),
  19350. AES_EAX_AUTH_E);
  19351. /* Test null checking */
  19352. ExpectIntEQ(wc_AesEaxDecryptAuth(NULL, sizeof(key),
  19353. plaintext,
  19354. ct, sizeof(ct),
  19355. iv, sizeof(iv),
  19356. tag, len,
  19357. aad, sizeof(aad)),
  19358. BAD_FUNC_ARG);
  19359. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19360. NULL,
  19361. ct, sizeof(ct),
  19362. iv, sizeof(iv),
  19363. tag, len,
  19364. aad, sizeof(aad)),
  19365. BAD_FUNC_ARG);
  19366. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19367. plaintext,
  19368. NULL, sizeof(ct),
  19369. iv, sizeof(iv),
  19370. tag, len,
  19371. aad, sizeof(aad)),
  19372. BAD_FUNC_ARG);
  19373. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19374. plaintext,
  19375. ct, sizeof(ct),
  19376. NULL, sizeof(iv),
  19377. tag, len,
  19378. aad, sizeof(aad)),
  19379. BAD_FUNC_ARG);
  19380. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19381. plaintext,
  19382. ct, sizeof(ct),
  19383. iv, sizeof(iv),
  19384. NULL, len,
  19385. aad, sizeof(aad)),
  19386. BAD_FUNC_ARG);
  19387. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19388. plaintext,
  19389. ct, sizeof(ct),
  19390. iv, sizeof(iv),
  19391. tag, len,
  19392. NULL, sizeof(aad)),
  19393. BAD_FUNC_ARG);
  19394. /* Test bad key lengths */
  19395. for (i = 0; i <= 32; i++) {
  19396. int exp_ret;
  19397. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  19398. || i == AES_256_KEY_SIZE) {
  19399. exp_ret = AES_EAX_AUTH_E;
  19400. }
  19401. else {
  19402. exp_ret = BAD_FUNC_ARG;
  19403. }
  19404. ExpectIntEQ(wc_AesEaxDecryptAuth(key, i,
  19405. plaintext,
  19406. ct, sizeof(ct),
  19407. iv, sizeof(iv),
  19408. tag, len,
  19409. aad, sizeof(aad)),
  19410. exp_ret);
  19411. }
  19412. /* Test auth tag size out of range */
  19413. len = AES_BLOCK_SIZE + 1;
  19414. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19415. plaintext,
  19416. ct, sizeof(ct),
  19417. iv, sizeof(iv),
  19418. tag, len,
  19419. aad, sizeof(aad)),
  19420. BAD_FUNC_ARG);
  19421. return EXPECT_RESULT();
  19422. } /* END test_wc_AesEaxDecryptAuth() */
  19423. #endif /* WOLFSSL_AES_EAX &&
  19424. * (!HAVE_FIPS || FIPS_VERSION_GE(5, 3)) && !HAVE_SELFTEST
  19425. */
  19426. /*
  19427. * Testing wc_InitDsaKey()
  19428. */
  19429. static int test_wc_InitDsaKey(void)
  19430. {
  19431. EXPECT_DECLS;
  19432. #ifndef NO_DSA
  19433. DsaKey key;
  19434. XMEMSET(&key, 0, sizeof(DsaKey));
  19435. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19436. /* Pass in bad args. */
  19437. ExpectIntEQ(wc_InitDsaKey(NULL), BAD_FUNC_ARG);
  19438. wc_FreeDsaKey(&key);
  19439. #endif
  19440. return EXPECT_RESULT();
  19441. } /* END test_wc_InitDsaKey */
  19442. /*
  19443. * Testing wc_DsaSign() and wc_DsaVerify()
  19444. */
  19445. static int test_wc_DsaSignVerify(void)
  19446. {
  19447. EXPECT_DECLS;
  19448. #if !defined(NO_DSA)
  19449. DsaKey key;
  19450. WC_RNG rng;
  19451. wc_Sha sha;
  19452. byte signature[DSA_SIG_SIZE];
  19453. byte hash[WC_SHA_DIGEST_SIZE];
  19454. word32 idx = 0;
  19455. word32 bytes;
  19456. int answer;
  19457. #ifdef USE_CERT_BUFFERS_1024
  19458. byte tmp[ONEK_BUF];
  19459. XMEMSET(tmp, 0, sizeof(tmp));
  19460. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19461. bytes = sizeof_dsa_key_der_1024;
  19462. #elif defined(USE_CERT_BUFFERS_2048)
  19463. byte tmp[TWOK_BUF];
  19464. XMEMSET(tmp, 0, sizeof(tmp));
  19465. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19466. bytes = sizeof_dsa_key_der_2048;
  19467. #else
  19468. byte tmp[TWOK_BUF];
  19469. XFILE fp = XBADFILE;
  19470. XMEMSET(tmp, 0, sizeof(tmp));
  19471. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19472. ExpectTrue((bytes = (word32)XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19473. if (fp != XBADFILE)
  19474. XFCLOSE(fp);
  19475. #endif /* END USE_CERT_BUFFERS_1024 */
  19476. ExpectIntEQ(wc_InitSha(&sha), 0);
  19477. ExpectIntEQ(wc_ShaUpdate(&sha, tmp, bytes), 0);
  19478. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  19479. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19480. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19481. ExpectIntEQ(wc_InitRng(&rng), 0);
  19482. /* Sign. */
  19483. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), 0);
  19484. /* Test bad args. */
  19485. ExpectIntEQ(wc_DsaSign(NULL, signature, &key, &rng), BAD_FUNC_ARG);
  19486. ExpectIntEQ(wc_DsaSign(hash, NULL, &key, &rng), BAD_FUNC_ARG);
  19487. ExpectIntEQ(wc_DsaSign(hash, signature, NULL, &rng), BAD_FUNC_ARG);
  19488. ExpectIntEQ(wc_DsaSign(hash, signature, &key, NULL), BAD_FUNC_ARG);
  19489. /* Verify. */
  19490. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, &answer), 0);
  19491. ExpectIntEQ(answer, 1);
  19492. /* Pass in bad args. */
  19493. ExpectIntEQ(wc_DsaVerify(NULL, signature, &key, &answer), BAD_FUNC_ARG);
  19494. ExpectIntEQ(wc_DsaVerify(hash, NULL, &key, &answer), BAD_FUNC_ARG);
  19495. ExpectIntEQ(wc_DsaVerify(hash, signature, NULL, &answer), BAD_FUNC_ARG);
  19496. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, NULL), BAD_FUNC_ARG);
  19497. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  19498. /* hard set q to 0 and test fail case */
  19499. mp_free(&key.q);
  19500. mp_init(&key.q);
  19501. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  19502. mp_set(&key.q, 1);
  19503. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  19504. #endif
  19505. DoExpectIntEQ(wc_FreeRng(&rng),0);
  19506. wc_FreeDsaKey(&key);
  19507. wc_ShaFree(&sha);
  19508. #endif
  19509. return EXPECT_RESULT();
  19510. } /* END test_wc_DsaSign */
  19511. /*
  19512. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  19513. */
  19514. static int test_wc_DsaPublicPrivateKeyDecode(void)
  19515. {
  19516. EXPECT_DECLS;
  19517. #if !defined(NO_DSA)
  19518. DsaKey key;
  19519. word32 bytes = 0;
  19520. word32 idx = 0;
  19521. int ret = 0;
  19522. #ifdef USE_CERT_BUFFERS_1024
  19523. byte tmp[ONEK_BUF];
  19524. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19525. bytes = sizeof_dsa_key_der_1024;
  19526. #elif defined(USE_CERT_BUFFERS_2048)
  19527. byte tmp[TWOK_BUF];
  19528. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19529. bytes = sizeof_dsa_key_der_2048;
  19530. #else
  19531. byte tmp[TWOK_BUF];
  19532. XFILE fp = XBADFILE;
  19533. XMEMSET(tmp, 0, sizeof(tmp));
  19534. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19535. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19536. if (fp != XBADFILE)
  19537. XFCLOSE(fp);
  19538. #endif /* END USE_CERT_BUFFERS_1024 */
  19539. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19540. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19541. /* Test bad args. */
  19542. ExpectIntEQ(wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  19543. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  19544. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  19545. ExpectIntLT(ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19546. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  19547. wc_FreeDsaKey(&key);
  19548. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19549. idx = 0; /* Reset */
  19550. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  19551. /* Test bad args. */
  19552. ExpectIntEQ(wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  19553. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  19554. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  19555. ExpectIntLT(ret = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  19556. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  19557. wc_FreeDsaKey(&key);
  19558. #endif /* !NO_DSA */
  19559. return EXPECT_RESULT();
  19560. } /* END test_wc_DsaPublicPrivateKeyDecode */
  19561. /*
  19562. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  19563. */
  19564. static int test_wc_MakeDsaKey(void)
  19565. {
  19566. EXPECT_DECLS;
  19567. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19568. DsaKey genKey;
  19569. WC_RNG rng;
  19570. XMEMSET(&genKey, 0, sizeof(genKey));
  19571. XMEMSET(&rng, 0, sizeof(rng));
  19572. ExpectIntEQ(wc_InitDsaKey(&genKey), 0);
  19573. ExpectIntEQ(wc_InitRng(&rng), 0);
  19574. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey), 0);
  19575. /* Test bad args. */
  19576. ExpectIntEQ(wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey), BAD_FUNC_ARG);
  19577. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, NULL), BAD_FUNC_ARG);
  19578. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey),
  19579. BAD_FUNC_ARG);
  19580. ExpectIntEQ(wc_MakeDsaKey(&rng, &genKey), 0);
  19581. /* Test bad args. */
  19582. ExpectIntEQ(wc_MakeDsaKey(NULL, &genKey), BAD_FUNC_ARG);
  19583. ExpectIntEQ(wc_MakeDsaKey(&rng, NULL), BAD_FUNC_ARG);
  19584. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19585. wc_FreeDsaKey(&genKey);
  19586. #endif
  19587. return EXPECT_RESULT();
  19588. } /* END test_wc_MakeDsaKey */
  19589. /*
  19590. * Testing wc_DsaKeyToDer()
  19591. */
  19592. static int test_wc_DsaKeyToDer(void)
  19593. {
  19594. EXPECT_DECLS;
  19595. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19596. DsaKey key;
  19597. word32 bytes;
  19598. word32 idx = 0;
  19599. #ifdef USE_CERT_BUFFERS_1024
  19600. byte tmp[ONEK_BUF];
  19601. byte der[ONEK_BUF];
  19602. XMEMSET(tmp, 0, sizeof(tmp));
  19603. XMEMSET(der, 0, sizeof(der));
  19604. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19605. bytes = sizeof_dsa_key_der_1024;
  19606. #elif defined(USE_CERT_BUFFERS_2048)
  19607. byte tmp[TWOK_BUF];
  19608. byte der[TWOK_BUF];
  19609. XMEMSET(tmp, 0, sizeof(tmp));
  19610. XMEMSET(der, 0, sizeof(der));
  19611. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19612. bytes = sizeof_dsa_key_der_2048;
  19613. #else
  19614. byte tmp[TWOK_BUF];
  19615. byte der[TWOK_BUF];
  19616. XFILE fp = XBADFILE;
  19617. XMEMSET(tmp, 0, sizeof(tmp));
  19618. XMEMSET(der, 0, sizeof(der));
  19619. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19620. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19621. if (fp != XBADFILE)
  19622. XFCLOSE(fp);
  19623. #endif /* END USE_CERT_BUFFERS_1024 */
  19624. XMEMSET(&key, 0, sizeof(DsaKey));
  19625. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19626. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19627. ExpectIntGE(wc_DsaKeyToDer(&key, der, bytes), 0);
  19628. ExpectIntEQ(XMEMCMP(der, tmp, bytes), 0);
  19629. /* Test bad args. */
  19630. ExpectIntEQ(wc_DsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  19631. ExpectIntEQ(wc_DsaKeyToDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  19632. wc_FreeDsaKey(&key);
  19633. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19634. return EXPECT_RESULT();
  19635. } /* END test_wc_DsaKeyToDer */
  19636. /*
  19637. * Testing wc_DsaKeyToPublicDer()
  19638. * (indirectly testing setDsaPublicKey())
  19639. */
  19640. static int test_wc_DsaKeyToPublicDer(void)
  19641. {
  19642. EXPECT_DECLS;
  19643. #ifndef HAVE_SELFTEST
  19644. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19645. DsaKey key;
  19646. WC_RNG rng;
  19647. byte* der = NULL;
  19648. word32 sz = 0;
  19649. word32 idx = 0;
  19650. XMEMSET(&key, 0, sizeof(DsaKey));
  19651. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19652. ExpectNotNull(der = (byte*)XMALLOC(ONEK_BUF, NULL,
  19653. DYNAMIC_TYPE_TMP_BUFFER));
  19654. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19655. ExpectIntEQ(wc_InitRng(&rng), 0);
  19656. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &key), 0);
  19657. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  19658. ExpectIntGE(sz = wc_DsaKeyToPublicDer(&key, der, ONEK_BUF), 0);
  19659. wc_FreeDsaKey(&key);
  19660. idx = 0;
  19661. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  19662. /* Test without the SubjectPublicKeyInfo header */
  19663. ExpectIntGE(sz = wc_SetDsaPublicKey(der, &key, ONEK_BUF, 0), 0);
  19664. wc_FreeDsaKey(&key);
  19665. idx = 0;
  19666. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  19667. /* Test bad args. */
  19668. ExpectIntEQ(wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  19669. ExpectIntEQ(wc_DsaKeyToPublicDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  19670. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19671. wc_FreeDsaKey(&key);
  19672. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  19673. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19674. #endif /* !HAVE_SELFTEST */
  19675. return EXPECT_RESULT();
  19676. } /* END test_wc_DsaKeyToPublicDer */
  19677. /*
  19678. * Testing wc_DsaImportParamsRaw()
  19679. */
  19680. static int test_wc_DsaImportParamsRaw(void)
  19681. {
  19682. EXPECT_DECLS;
  19683. #if !defined(NO_DSA)
  19684. DsaKey key;
  19685. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19686. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19687. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19688. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19689. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19690. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19691. "80a0093113a8bd73";
  19692. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19693. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19694. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19695. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19696. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19697. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19698. "76341a7e7d9";
  19699. /* invalid p and q parameters */
  19700. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19701. const char* invalidQ = "96c5390a";
  19702. XMEMSET(&key, 0, sizeof(DsaKey));
  19703. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19704. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  19705. /* test bad args */
  19706. /* null key struct */
  19707. ExpectIntEQ(wc_DsaImportParamsRaw(NULL, p, q, g), BAD_FUNC_ARG);
  19708. /* null param pointers */
  19709. ExpectIntEQ(wc_DsaImportParamsRaw(&key, NULL, NULL, NULL), BAD_FUNC_ARG);
  19710. /* illegal p length */
  19711. ExpectIntEQ(wc_DsaImportParamsRaw(&key, invalidP, q, g), BAD_FUNC_ARG);
  19712. /* illegal q length */
  19713. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, invalidQ, g), BAD_FUNC_ARG);
  19714. wc_FreeDsaKey(&key);
  19715. #endif
  19716. return EXPECT_RESULT();
  19717. } /* END test_wc_DsaImportParamsRaw */
  19718. /*
  19719. * Testing wc_DsaImportParamsRawCheck()
  19720. */
  19721. static int test_wc_DsaImportParamsRawCheck(void)
  19722. {
  19723. EXPECT_DECLS;
  19724. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  19725. DsaKey key;
  19726. int trusted = 0;
  19727. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19728. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19729. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19730. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19731. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19732. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19733. "80a0093113a8bd73";
  19734. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19735. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19736. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19737. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19738. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19739. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19740. "76341a7e7d9";
  19741. /* invalid p and q parameters */
  19742. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19743. const char* invalidQ = "96c5390a";
  19744. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19745. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL), 0);
  19746. /* test bad args */
  19747. /* null key struct */
  19748. ExpectIntEQ(wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL),
  19749. BAD_FUNC_ARG);
  19750. /* null param pointers */
  19751. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted,
  19752. NULL), BAD_FUNC_ARG);
  19753. /* illegal p length */
  19754. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL),
  19755. BAD_FUNC_ARG);
  19756. /* illegal q length */
  19757. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL),
  19758. BAD_FUNC_ARG);
  19759. wc_FreeDsaKey(&key);
  19760. #endif
  19761. return EXPECT_RESULT();
  19762. } /* END test_wc_DsaImportParamsRawCheck */
  19763. /*
  19764. * Testing wc_DsaExportParamsRaw()
  19765. */
  19766. static int test_wc_DsaExportParamsRaw(void)
  19767. {
  19768. EXPECT_DECLS;
  19769. #if !defined(NO_DSA)
  19770. DsaKey key;
  19771. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19772. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19773. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19774. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19775. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19776. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19777. "80a0093113a8bd73";
  19778. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19779. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19780. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19781. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19782. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19783. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19784. "76341a7e7d9";
  19785. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  19786. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  19787. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  19788. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  19789. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  19790. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  19791. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  19792. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  19793. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  19794. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  19795. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  19796. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  19797. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  19798. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  19799. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  19800. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  19801. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  19802. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  19803. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  19804. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  19805. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  19806. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  19807. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  19808. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  19809. byte pOut[MAX_DSA_PARAM_SIZE];
  19810. byte qOut[MAX_DSA_PARAM_SIZE];
  19811. byte gOut[MAX_DSA_PARAM_SIZE];
  19812. word32 pOutSz;
  19813. word32 qOutSz;
  19814. word32 gOutSz;
  19815. XMEMSET(&key, 0, sizeof(DsaKey));
  19816. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19817. /* first test using imported raw parameters, for expected */
  19818. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  19819. pOutSz = sizeof(pOut);
  19820. qOutSz = sizeof(qOut);
  19821. gOutSz = sizeof(gOut);
  19822. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19823. &gOutSz), 0);
  19824. /* validate exported parameters are correct */
  19825. ExpectIntEQ(XMEMCMP(pOut, pCompare, pOutSz), 0);
  19826. ExpectIntEQ(XMEMCMP(qOut, qCompare, qOutSz), 0);
  19827. ExpectIntEQ(XMEMCMP(gOut, gCompare, gOutSz), 0);
  19828. /* test bad args */
  19829. /* null key struct */
  19830. ExpectIntEQ(wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19831. &gOutSz), BAD_FUNC_ARG);
  19832. /* null output pointers */
  19833. ExpectIntEQ(wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz, NULL,
  19834. &gOutSz), LENGTH_ONLY_E);
  19835. /* null output size pointers */
  19836. ExpectIntEQ( wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL, gOut,
  19837. NULL), BAD_FUNC_ARG);
  19838. /* p output buffer size too small */
  19839. pOutSz = 1;
  19840. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19841. &gOutSz), BUFFER_E);
  19842. pOutSz = sizeof(pOut);
  19843. /* q output buffer size too small */
  19844. qOutSz = 1;
  19845. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19846. &gOutSz), BUFFER_E);
  19847. qOutSz = sizeof(qOut);
  19848. /* g output buffer size too small */
  19849. gOutSz = 1;
  19850. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19851. &gOutSz), BUFFER_E);
  19852. wc_FreeDsaKey(&key);
  19853. #endif
  19854. return EXPECT_RESULT();
  19855. } /* END test_wc_DsaExportParamsRaw */
  19856. /*
  19857. * Testing wc_DsaExportKeyRaw()
  19858. */
  19859. static int test_wc_DsaExportKeyRaw(void)
  19860. {
  19861. EXPECT_DECLS;
  19862. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19863. DsaKey key;
  19864. WC_RNG rng;
  19865. byte xOut[MAX_DSA_PARAM_SIZE];
  19866. byte yOut[MAX_DSA_PARAM_SIZE];
  19867. word32 xOutSz, yOutSz;
  19868. XMEMSET(&key, 0, sizeof(key));
  19869. XMEMSET(&rng, 0, sizeof(rng));
  19870. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19871. ExpectIntEQ(wc_InitRng(&rng), 0);
  19872. ExpectIntEQ(wc_MakeDsaParameters(&rng, 1024, &key), 0);
  19873. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  19874. /* try successful export */
  19875. xOutSz = sizeof(xOut);
  19876. yOutSz = sizeof(yOut);
  19877. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz), 0);
  19878. /* test bad args */
  19879. /* null key struct */
  19880. ExpectIntEQ(wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz),
  19881. BAD_FUNC_ARG);
  19882. /* null output pointers */
  19883. ExpectIntEQ(wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz),
  19884. LENGTH_ONLY_E);
  19885. /* null output size pointers */
  19886. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL),
  19887. BAD_FUNC_ARG);
  19888. /* x output buffer size too small */
  19889. xOutSz = 1;
  19890. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  19891. BUFFER_E);
  19892. xOutSz = sizeof(xOut);
  19893. /* y output buffer size too small */
  19894. yOutSz = 1;
  19895. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  19896. BUFFER_E);
  19897. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19898. wc_FreeDsaKey(&key);
  19899. #endif
  19900. return EXPECT_RESULT();
  19901. } /* END test_wc_DsaExportParamsRaw */
  19902. /*
  19903. * Testing wc_ed25519_make_key().
  19904. */
  19905. static int test_wc_ed25519_make_key(void)
  19906. {
  19907. EXPECT_DECLS;
  19908. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_MAKE_KEY)
  19909. ed25519_key key;
  19910. WC_RNG rng;
  19911. unsigned char pubkey[ED25519_PUB_KEY_SIZE+1];
  19912. int pubkey_sz = ED25519_PUB_KEY_SIZE;
  19913. XMEMSET(&key, 0, sizeof(ed25519_key));
  19914. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19915. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19916. ExpectIntEQ(wc_InitRng(&rng), 0);
  19917. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey, pubkey_sz),
  19918. ECC_PRIV_KEY_E);
  19919. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey+1, pubkey_sz),
  19920. ECC_PRIV_KEY_E);
  19921. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19922. /* Test bad args. */
  19923. ExpectIntEQ(wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key),
  19924. BAD_FUNC_ARG);
  19925. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL),
  19926. BAD_FUNC_ARG);
  19927. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key),
  19928. BAD_FUNC_ARG);
  19929. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key),
  19930. BAD_FUNC_ARG);
  19931. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19932. wc_ed25519_free(&key);
  19933. #endif
  19934. return EXPECT_RESULT();
  19935. } /* END test_wc_ed25519_make_key */
  19936. /*
  19937. * Testing wc_ed25519_init()
  19938. */
  19939. static int test_wc_ed25519_init(void)
  19940. {
  19941. EXPECT_DECLS;
  19942. #if defined(HAVE_ED25519)
  19943. ed25519_key key;
  19944. XMEMSET(&key, 0, sizeof(ed25519_key));
  19945. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19946. /* Test bad args. */
  19947. ExpectIntEQ(wc_ed25519_init(NULL), BAD_FUNC_ARG);
  19948. wc_ed25519_free(&key);
  19949. #endif
  19950. return EXPECT_RESULT();
  19951. } /* END test_wc_ed25519_init */
  19952. /*
  19953. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  19954. */
  19955. static int test_wc_ed25519_sign_msg(void)
  19956. {
  19957. EXPECT_DECLS;
  19958. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  19959. WC_RNG rng;
  19960. ed25519_key key;
  19961. byte msg[] = "Everybody gets Friday off.\n";
  19962. byte sig[ED25519_SIG_SIZE+1];
  19963. word32 msglen = sizeof(msg);
  19964. word32 siglen = ED25519_SIG_SIZE;
  19965. word32 badSigLen = ED25519_SIG_SIZE - 1;
  19966. #ifdef HAVE_ED25519_VERIFY
  19967. int verify_ok = 0; /*1 = Verify success.*/
  19968. #endif
  19969. /* Initialize stack variables. */
  19970. XMEMSET(&key, 0, sizeof(ed25519_key));
  19971. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19972. XMEMSET(sig, 0, sizeof(sig));
  19973. /* Initialize key. */
  19974. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19975. ExpectIntEQ(wc_InitRng(&rng), 0);
  19976. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19977. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key), 0);
  19978. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  19979. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig+1, &siglen, &key), 0);
  19980. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  19981. /* Test bad args. */
  19982. ExpectIntEQ(wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key),
  19983. BAD_FUNC_ARG);
  19984. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key),
  19985. BAD_FUNC_ARG);
  19986. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key),
  19987. BAD_FUNC_ARG);
  19988. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL),
  19989. BAD_FUNC_ARG);
  19990. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key),
  19991. BUFFER_E);
  19992. ExpectIntEQ(badSigLen, ED25519_SIG_SIZE);
  19993. badSigLen -= 1;
  19994. #ifdef HAVE_ED25519_VERIFY
  19995. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  19996. &key), 0);
  19997. ExpectIntEQ(verify_ok, 1);
  19998. /* Test bad args. */
  19999. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen - 1, msg, msglen,
  20000. &verify_ok, &key), BAD_FUNC_ARG);
  20001. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen + 1, msg, msglen,
  20002. &verify_ok, &key), BAD_FUNC_ARG);
  20003. ExpectIntEQ(wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  20004. &key), BAD_FUNC_ARG);
  20005. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, NULL, msglen, &verify_ok,
  20006. &key), BAD_FUNC_ARG);
  20007. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, NULL, &key),
  20008. BAD_FUNC_ARG);
  20009. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  20010. NULL), BAD_FUNC_ARG);
  20011. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, badSigLen, msg, msglen, &verify_ok,
  20012. &key), BAD_FUNC_ARG);
  20013. #endif /* Verify. */
  20014. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20015. wc_ed25519_free(&key);
  20016. #endif
  20017. return EXPECT_RESULT();
  20018. } /* END test_wc_ed25519_sign_msg */
  20019. /*
  20020. * Testing wc_ed25519_import_public()
  20021. */
  20022. static int test_wc_ed25519_import_public(void)
  20023. {
  20024. EXPECT_DECLS;
  20025. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  20026. ed25519_key pubKey;
  20027. WC_RNG rng;
  20028. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  20029. word32 inlen = sizeof(in);
  20030. XMEMSET(&pubKey, 0, sizeof(ed25519_key));
  20031. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20032. ExpectIntEQ(wc_ed25519_init(&pubKey), 0);
  20033. ExpectIntEQ(wc_InitRng(&rng), 0);
  20034. #ifdef HAVE_ED25519_MAKE_KEY
  20035. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey), 0);
  20036. #endif
  20037. ExpectIntEQ(wc_ed25519_import_public_ex(in, inlen, &pubKey, 1), 0);
  20038. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  20039. /* Test bad args. */
  20040. ExpectIntEQ(wc_ed25519_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  20041. ExpectIntEQ(wc_ed25519_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  20042. ExpectIntEQ(wc_ed25519_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  20043. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20044. wc_ed25519_free(&pubKey);
  20045. #endif
  20046. return EXPECT_RESULT();
  20047. } /* END wc_ed25519_import_public */
  20048. /*
  20049. * Testing wc_ed25519_import_private_key()
  20050. */
  20051. static int test_wc_ed25519_import_private_key(void)
  20052. {
  20053. EXPECT_DECLS;
  20054. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  20055. ed25519_key key;
  20056. WC_RNG rng;
  20057. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  20058. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  20059. word32 privKeySz = sizeof(privKey);
  20060. word32 pubKeySz = sizeof(pubKey);
  20061. #ifdef HAVE_ED25519_KEY_EXPORT
  20062. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  20063. word32 bothKeysSz = sizeof(bothKeys);
  20064. #endif
  20065. XMEMSET(&key, 0, sizeof(ed25519_key));
  20066. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20067. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20068. ExpectIntEQ(wc_InitRng(&rng), 0);
  20069. #ifdef HAVE_ED25519_MAKE_KEY
  20070. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20071. #endif
  20072. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  20073. pubKeySz, &key, 1), 0);
  20074. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20075. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20076. #ifdef HAVE_ED25519_KEY_EXPORT
  20077. PRIVATE_KEY_UNLOCK();
  20078. ExpectIntEQ(wc_ed25519_export_private(&key, bothKeys, &bothKeysSz), 0);
  20079. PRIVATE_KEY_LOCK();
  20080. ExpectIntEQ(wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  20081. &key, 1), 0);
  20082. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20083. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20084. #endif
  20085. /* Test bad args. */
  20086. ExpectIntEQ(wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  20087. &key), BAD_FUNC_ARG);
  20088. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  20089. pubKeySz, &key), BAD_FUNC_ARG);
  20090. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  20091. pubKeySz, NULL), BAD_FUNC_ARG);
  20092. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  20093. pubKeySz, &key), BAD_FUNC_ARG);
  20094. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  20095. pubKeySz - 1, &key), BAD_FUNC_ARG);
  20096. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL, 0,
  20097. &key), BAD_FUNC_ARG);
  20098. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20099. wc_ed25519_free(&key);
  20100. #endif
  20101. return EXPECT_RESULT();
  20102. } /* END test_wc_ed25519_import_private_key */
  20103. /*
  20104. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  20105. */
  20106. static int test_wc_ed25519_export(void)
  20107. {
  20108. EXPECT_DECLS;
  20109. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20110. ed25519_key key;
  20111. WC_RNG rng;
  20112. byte priv[ED25519_PRV_KEY_SIZE];
  20113. byte pub[ED25519_PUB_KEY_SIZE];
  20114. word32 privSz = sizeof(priv);
  20115. word32 pubSz = sizeof(pub);
  20116. #ifndef HAVE_ED25519_MAKE_KEY
  20117. const byte privKey[] = {
  20118. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20119. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20120. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20121. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20122. };
  20123. const byte pubKey[] = {
  20124. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20125. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20126. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20127. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20128. };
  20129. #endif
  20130. XMEMSET(&key, 0, sizeof(ed25519_key));
  20131. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20132. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20133. ExpectIntEQ(wc_InitRng(&rng), 0);
  20134. #ifdef HAVE_ED25519_MAKE_KEY
  20135. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20136. #else
  20137. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20138. pubKey, sizeof(pubKey), &key, 1), 0);
  20139. #endif
  20140. PRIVATE_KEY_UNLOCK();
  20141. ExpectIntEQ(wc_ed25519_export_public(&key, pub, &pubSz), 0);
  20142. ExpectIntEQ(pubSz, ED25519_KEY_SIZE);
  20143. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  20144. /* Test bad args. */
  20145. ExpectIntEQ(wc_ed25519_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  20146. ExpectIntEQ(wc_ed25519_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  20147. ExpectIntEQ(wc_ed25519_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  20148. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, &privSz), 0);
  20149. ExpectIntEQ(privSz, ED25519_KEY_SIZE);
  20150. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  20151. /* Test bad args. */
  20152. ExpectIntEQ(wc_ed25519_export_private_only(NULL, priv, &privSz),
  20153. BAD_FUNC_ARG);
  20154. ExpectIntEQ(wc_ed25519_export_private_only(&key, NULL, &privSz),
  20155. BAD_FUNC_ARG);
  20156. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, NULL),
  20157. BAD_FUNC_ARG);
  20158. PRIVATE_KEY_LOCK();
  20159. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20160. wc_ed25519_free(&key);
  20161. #endif
  20162. return EXPECT_RESULT();
  20163. } /* END test_wc_ed25519_export */
  20164. /*
  20165. * Testing wc_ed25519_size()
  20166. */
  20167. static int test_wc_ed25519_size(void)
  20168. {
  20169. EXPECT_DECLS;
  20170. #if defined(HAVE_ED25519)
  20171. ed25519_key key;
  20172. WC_RNG rng;
  20173. #ifndef HAVE_ED25519_MAKE_KEY
  20174. const byte privKey[] = {
  20175. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20176. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20177. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20178. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20179. };
  20180. const byte pubKey[] = {
  20181. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20182. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20183. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20184. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20185. };
  20186. #endif
  20187. XMEMSET(&key, 0, sizeof(ed25519_key));
  20188. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20189. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20190. ExpectIntEQ(wc_InitRng(&rng), 0);
  20191. #ifdef HAVE_ED25519_MAKE_KEY
  20192. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20193. #else
  20194. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20195. pubKey, sizeof(pubKey), &key, 1), 0);
  20196. #endif
  20197. ExpectIntEQ(wc_ed25519_size(&key), ED25519_KEY_SIZE);
  20198. /* Test bad args. */
  20199. ExpectIntEQ(wc_ed25519_size(NULL), BAD_FUNC_ARG);
  20200. ExpectIntEQ(wc_ed25519_sig_size(&key), ED25519_SIG_SIZE);
  20201. /* Test bad args. */
  20202. ExpectIntEQ(wc_ed25519_sig_size(NULL), BAD_FUNC_ARG);
  20203. ExpectIntEQ(wc_ed25519_pub_size(&key), ED25519_PUB_KEY_SIZE);
  20204. /* Test bad args. */
  20205. ExpectIntEQ(wc_ed25519_pub_size(NULL), BAD_FUNC_ARG);
  20206. ExpectIntEQ(wc_ed25519_priv_size(&key), ED25519_PRV_KEY_SIZE);
  20207. /* Test bad args. */
  20208. ExpectIntEQ(wc_ed25519_priv_size(NULL), BAD_FUNC_ARG);
  20209. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20210. wc_ed25519_free(&key);
  20211. #endif
  20212. return EXPECT_RESULT();
  20213. } /* END test_wc_ed25519_size */
  20214. /*
  20215. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  20216. */
  20217. static int test_wc_ed25519_exportKey(void)
  20218. {
  20219. EXPECT_DECLS;
  20220. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20221. WC_RNG rng;
  20222. ed25519_key key;
  20223. byte priv[ED25519_PRV_KEY_SIZE];
  20224. byte pub[ED25519_PUB_KEY_SIZE];
  20225. byte privOnly[ED25519_PRV_KEY_SIZE];
  20226. word32 privSz = sizeof(priv);
  20227. word32 pubSz = sizeof(pub);
  20228. word32 privOnlySz = sizeof(privOnly);
  20229. #ifndef HAVE_ED25519_MAKE_KEY
  20230. const byte privKey[] = {
  20231. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20232. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20233. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20234. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20235. };
  20236. const byte pubKey[] = {
  20237. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20238. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20239. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20240. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20241. };
  20242. #endif
  20243. XMEMSET(&key, 0, sizeof(ed25519_key));
  20244. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20245. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20246. ExpectIntEQ(wc_InitRng(&rng), 0);
  20247. #ifdef HAVE_ED25519_MAKE_KEY
  20248. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20249. #else
  20250. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20251. pubKey, sizeof(pubKey), &key, 1), 0);
  20252. #endif
  20253. PRIVATE_KEY_UNLOCK();
  20254. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, &privOnlySz), 0);
  20255. /* Test bad args. */
  20256. ExpectIntEQ(wc_ed25519_export_private(NULL, privOnly, &privOnlySz),
  20257. BAD_FUNC_ARG);
  20258. ExpectIntEQ(wc_ed25519_export_private(&key, NULL, &privOnlySz),
  20259. BAD_FUNC_ARG);
  20260. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  20261. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  20262. /* Test bad args. */
  20263. ExpectIntEQ(wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz),
  20264. BAD_FUNC_ARG);
  20265. ExpectIntEQ(wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz),
  20266. BAD_FUNC_ARG);
  20267. ExpectIntEQ(wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz),
  20268. BAD_FUNC_ARG);
  20269. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz),
  20270. BAD_FUNC_ARG);
  20271. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, NULL),
  20272. BAD_FUNC_ARG);
  20273. PRIVATE_KEY_LOCK();
  20274. /* Cross check output. */
  20275. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  20276. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20277. wc_ed25519_free(&key);
  20278. #endif
  20279. return EXPECT_RESULT();
  20280. } /* END test_wc_ed25519_exportKey */
  20281. /*
  20282. * Testing wc_Ed25519PublicKeyToDer
  20283. */
  20284. static int test_wc_Ed25519PublicKeyToDer(void)
  20285. {
  20286. EXPECT_DECLS;
  20287. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  20288. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20289. ed25519_key key;
  20290. byte derBuf[1024];
  20291. XMEMSET(&key, 0, sizeof(ed25519_key));
  20292. /* Test bad args */
  20293. ExpectIntEQ(wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  20294. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20295. ExpectIntEQ(wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  20296. wc_ed25519_free(&key);
  20297. /* Test good args */
  20298. if (EXPECT_SUCCESS()) {
  20299. WC_RNG rng;
  20300. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20301. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20302. ExpectIntEQ(wc_InitRng(&rng), 0);
  20303. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20304. ExpectIntGT(wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  20305. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20306. wc_ed25519_free(&key);
  20307. }
  20308. #endif
  20309. return EXPECT_RESULT();
  20310. } /* END testing wc_Ed25519PublicKeyToDer */
  20311. /*
  20312. * Testing wc_curve25519_init and wc_curve25519_free.
  20313. */
  20314. static int test_wc_curve25519_init(void)
  20315. {
  20316. EXPECT_DECLS;
  20317. #if defined(HAVE_CURVE25519)
  20318. curve25519_key key;
  20319. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20320. /* Test bad args for wc_curve25519_init */
  20321. ExpectIntEQ(wc_curve25519_init(NULL), BAD_FUNC_ARG);
  20322. /* Test good args for wc_curve_25519_free */
  20323. wc_curve25519_free(&key);
  20324. /* Test bad args for wc_curve25519 free. */
  20325. wc_curve25519_free(NULL);
  20326. #endif
  20327. return EXPECT_RESULT();
  20328. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  20329. /*
  20330. * Testing test_wc_curve25519_size.
  20331. */
  20332. static int test_wc_curve25519_size(void)
  20333. {
  20334. EXPECT_DECLS;
  20335. #if defined(HAVE_CURVE25519)
  20336. curve25519_key key;
  20337. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20338. /* Test good args for wc_curve25519_size */
  20339. ExpectIntEQ(wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  20340. /* Test bad args for wc_curve25519_size */
  20341. ExpectIntEQ(wc_curve25519_size(NULL), 0);
  20342. wc_curve25519_free(&key);
  20343. #endif
  20344. return EXPECT_RESULT();
  20345. } /* END test_wc_curve25519_size*/
  20346. /*
  20347. * Testing test_wc_curve25519_export_key_raw().
  20348. */
  20349. static int test_wc_curve25519_export_key_raw(void)
  20350. {
  20351. EXPECT_DECLS;
  20352. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  20353. curve25519_key key;
  20354. WC_RNG rng;
  20355. byte privateKey[CURVE25519_KEYSIZE];
  20356. byte publicKey[CURVE25519_KEYSIZE];
  20357. word32 prvkSz;
  20358. word32 pubkSz;
  20359. byte prik[CURVE25519_KEYSIZE];
  20360. byte pubk[CURVE25519_KEYSIZE];
  20361. word32 prksz;
  20362. word32 pbksz;
  20363. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20364. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20365. ExpectIntEQ(wc_InitRng(&rng), 0);
  20366. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20367. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  20368. prvkSz = CURVE25519_KEYSIZE;
  20369. pubkSz = CURVE25519_KEYSIZE;
  20370. ExpectIntEQ(wc_curve25519_export_key_raw(NULL, privateKey, &prvkSz,
  20371. publicKey, &pubkSz), BAD_FUNC_ARG);
  20372. prvkSz = CURVE25519_KEYSIZE;
  20373. pubkSz = CURVE25519_KEYSIZE;
  20374. ExpectIntEQ(wc_curve25519_export_key_raw(&key, NULL, &prvkSz, publicKey,
  20375. &pubkSz), BAD_FUNC_ARG);
  20376. prvkSz = CURVE25519_KEYSIZE;
  20377. pubkSz = CURVE25519_KEYSIZE;
  20378. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, NULL,
  20379. publicKey, &pubkSz), BAD_FUNC_ARG);
  20380. /* prvkSz = CURVE25519_KEYSIZE; */
  20381. pubkSz = CURVE25519_KEYSIZE;
  20382. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20383. NULL, &pubkSz), BAD_FUNC_ARG);
  20384. prvkSz = CURVE25519_KEYSIZE;
  20385. pubkSz = CURVE25519_KEYSIZE;
  20386. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20387. publicKey, NULL), BAD_FUNC_ARG);
  20388. /* cross-testing */
  20389. prksz = CURVE25519_KEYSIZE;
  20390. ExpectIntEQ(wc_curve25519_export_private_raw(&key, prik, &prksz), 0);
  20391. pbksz = CURVE25519_KEYSIZE;
  20392. ExpectIntEQ(wc_curve25519_export_public(&key, pubk, &pbksz), 0);
  20393. prvkSz = CURVE25519_KEYSIZE;
  20394. /* pubkSz = CURVE25519_KEYSIZE; */
  20395. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20396. publicKey, &pubkSz), 0);
  20397. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  20398. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  20399. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20400. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20401. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  20402. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  20403. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20404. wc_curve25519_free(&key);
  20405. #endif
  20406. return EXPECT_RESULT();
  20407. } /* end of test_wc_curve25519_export_key_raw */
  20408. /*
  20409. * Testing test_wc_curve25519_export_key_raw_ex().
  20410. */
  20411. static int test_wc_curve25519_export_key_raw_ex(void)
  20412. {
  20413. EXPECT_DECLS;
  20414. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  20415. curve25519_key key;
  20416. WC_RNG rng;
  20417. byte privateKey[CURVE25519_KEYSIZE];
  20418. byte publicKey[CURVE25519_KEYSIZE];
  20419. word32 prvkSz;
  20420. word32 pubkSz;
  20421. byte prik[CURVE25519_KEYSIZE];
  20422. byte pubk[CURVE25519_KEYSIZE];
  20423. word32 prksz;
  20424. word32 pbksz;
  20425. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20426. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20427. ExpectIntEQ(wc_InitRng(&rng), 0);
  20428. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20429. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  20430. prvkSz = CURVE25519_KEYSIZE;
  20431. pubkSz = CURVE25519_KEYSIZE;
  20432. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  20433. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20434. prvkSz = CURVE25519_KEYSIZE;
  20435. pubkSz = CURVE25519_KEYSIZE;
  20436. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  20437. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20438. prvkSz = CURVE25519_KEYSIZE;
  20439. pubkSz = CURVE25519_KEYSIZE;
  20440. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20441. NULL, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20442. /* prvkSz = CURVE25519_KEYSIZE; */
  20443. pubkSz = CURVE25519_KEYSIZE;
  20444. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20445. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20446. prvkSz = CURVE25519_KEYSIZE;
  20447. pubkSz = CURVE25519_KEYSIZE;
  20448. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20449. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20450. prvkSz = CURVE25519_KEYSIZE;
  20451. /* pubkSz = CURVE25519_KEYSIZE; */
  20452. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  20453. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20454. prvkSz = CURVE25519_KEYSIZE;
  20455. pubkSz = CURVE25519_KEYSIZE;
  20456. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  20457. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20458. prvkSz = CURVE25519_KEYSIZE;
  20459. pubkSz = CURVE25519_KEYSIZE;
  20460. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20461. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20462. /* prvkSz = CURVE25519_KEYSIZE; */
  20463. pubkSz = CURVE25519_KEYSIZE;
  20464. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20465. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20466. prvkSz = CURVE25519_KEYSIZE;
  20467. pubkSz = CURVE25519_KEYSIZE;
  20468. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20469. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20470. /* illegal value for endian */
  20471. prvkSz = CURVE25519_KEYSIZE;
  20472. /* pubkSz = CURVE25519_KEYSIZE; */
  20473. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20474. publicKey, NULL, EC25519_BIG_ENDIAN + 10), BAD_FUNC_ARG);
  20475. /* cross-testing */
  20476. prksz = CURVE25519_KEYSIZE;
  20477. ExpectIntEQ(wc_curve25519_export_private_raw( &key, prik, &prksz), 0);
  20478. pbksz = CURVE25519_KEYSIZE;
  20479. ExpectIntEQ(wc_curve25519_export_public( &key, pubk, &pbksz), 0);
  20480. prvkSz = CURVE25519_KEYSIZE;
  20481. /* pubkSz = CURVE25519_KEYSIZE; */
  20482. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20483. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  20484. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  20485. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  20486. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20487. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20488. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  20489. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  20490. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20491. publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), 0);
  20492. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20493. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20494. /* try once with another endian */
  20495. prvkSz = CURVE25519_KEYSIZE;
  20496. pubkSz = CURVE25519_KEYSIZE;
  20497. ExpectIntEQ(wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  20498. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  20499. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20500. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20501. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20502. wc_curve25519_free(&key);
  20503. #endif
  20504. return EXPECT_RESULT();
  20505. } /* end of test_wc_curve25519_export_key_raw_ex */
  20506. /*
  20507. * Testing wc_curve25519_make_key
  20508. */
  20509. static int test_wc_curve25519_make_key(void)
  20510. {
  20511. EXPECT_DECLS;
  20512. #if defined(HAVE_CURVE25519)
  20513. curve25519_key key;
  20514. WC_RNG rng;
  20515. int keysize;
  20516. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20517. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20518. ExpectIntEQ(wc_InitRng(&rng), 0);
  20519. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20520. ExpectIntEQ(keysize = wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  20521. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, &key), 0);
  20522. /* test bad cases*/
  20523. ExpectIntEQ(wc_curve25519_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  20524. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  20525. ExpectIntEQ(wc_curve25519_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  20526. ExpectIntEQ(wc_curve25519_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  20527. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20528. wc_curve25519_free(&key);
  20529. #endif
  20530. return EXPECT_RESULT();
  20531. } /* END test_wc_curve25519_make_key*/
  20532. /*
  20533. * Testing wc_curve25519_shared_secret_ex
  20534. */
  20535. static int test_wc_curve25519_shared_secret_ex(void)
  20536. {
  20537. EXPECT_DECLS;
  20538. #if defined(HAVE_CURVE25519)
  20539. curve25519_key private_key;
  20540. curve25519_key public_key;
  20541. WC_RNG rng;
  20542. byte out[CURVE25519_KEYSIZE];
  20543. word32 outLen = sizeof(out);
  20544. int endian = EC25519_BIG_ENDIAN;
  20545. ExpectIntEQ(wc_curve25519_init(&private_key), 0);
  20546. ExpectIntEQ(wc_curve25519_init(&public_key), 0);
  20547. ExpectIntEQ(wc_InitRng(&rng), 0);
  20548. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key),
  20549. 0);
  20550. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key),
  20551. 0);
  20552. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20553. &outLen, endian), 0);
  20554. /* test bad cases*/
  20555. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  20556. BAD_FUNC_ARG);
  20557. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, &public_key, out, &outLen,
  20558. endian), BAD_FUNC_ARG);
  20559. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, NULL, out, &outLen,
  20560. endian), BAD_FUNC_ARG);
  20561. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  20562. &outLen, endian), BAD_FUNC_ARG);
  20563. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20564. NULL, endian), BAD_FUNC_ARG);
  20565. /* curve25519.c is checking for public_key size less than or equal to 0x7f,
  20566. * increasing to 0x8f checks for error being returned*/
  20567. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  20568. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20569. &outLen, endian), ECC_BAD_ARG_E);
  20570. outLen = outLen - 2;
  20571. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20572. &outLen, endian), BAD_FUNC_ARG);
  20573. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20574. wc_curve25519_free(&private_key);
  20575. wc_curve25519_free(&public_key);
  20576. #endif
  20577. return EXPECT_RESULT();
  20578. } /* END test_wc_curve25519_shared_secret_ex*/
  20579. /*
  20580. * Testing wc_curve25519_make_pub
  20581. */
  20582. static int test_wc_curve25519_make_pub(void)
  20583. {
  20584. EXPECT_DECLS;
  20585. #ifdef HAVE_CURVE25519
  20586. curve25519_key key;
  20587. WC_RNG rng;
  20588. byte out[CURVE25519_KEYSIZE];
  20589. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20590. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20591. ExpectIntEQ(wc_InitRng(&rng), 0);
  20592. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20593. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(out), out,
  20594. (int)sizeof(key.k), key.k), 0);
  20595. /* test bad cases*/
  20596. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k,
  20597. (int)sizeof out, out), ECC_BAD_ARG_E);
  20598. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20599. NULL), ECC_BAD_ARG_E);
  20600. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out - 1, out,
  20601. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  20602. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, NULL,
  20603. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  20604. /* verify clamping test */
  20605. key.k[0] |= ~248;
  20606. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20607. key.k), ECC_BAD_ARG_E);
  20608. key.k[0] &= 248;
  20609. /* repeat the expected-to-succeed test. */
  20610. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20611. key.k), 0);
  20612. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20613. wc_curve25519_free(&key);
  20614. #endif
  20615. return EXPECT_RESULT();
  20616. } /* END test_wc_curve25519_make_pub */
  20617. /*
  20618. * Testing test_wc_curve25519_export_public_ex
  20619. */
  20620. static int test_wc_curve25519_export_public_ex(void)
  20621. {
  20622. EXPECT_DECLS;
  20623. #if defined(HAVE_CURVE25519)
  20624. curve25519_key key;
  20625. WC_RNG rng;
  20626. byte out[CURVE25519_KEYSIZE];
  20627. word32 outLen = sizeof(out);
  20628. int endian = EC25519_BIG_ENDIAN;
  20629. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20630. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20631. ExpectIntEQ(wc_InitRng(&rng), 0);
  20632. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20633. ExpectIntEQ(wc_curve25519_export_public(&key, out, &outLen), 0);
  20634. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian), 0);
  20635. /* test bad cases*/
  20636. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, NULL, NULL, endian),
  20637. BAD_FUNC_ARG);
  20638. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, out, &outLen, endian),
  20639. BAD_FUNC_ARG);
  20640. ExpectIntEQ(wc_curve25519_export_public_ex(&key, NULL, &outLen, endian),
  20641. BAD_FUNC_ARG);
  20642. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, NULL, endian),
  20643. BAD_FUNC_ARG);
  20644. outLen = outLen - 2;
  20645. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian),
  20646. ECC_BAD_ARG_E);
  20647. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20648. wc_curve25519_free(&key);
  20649. #endif
  20650. return EXPECT_RESULT();
  20651. } /* END test_wc_curve25519_export_public_ex*/
  20652. /*
  20653. * Testing test_wc_curve25519_import_private_raw_ex
  20654. */
  20655. static int test_wc_curve25519_import_private_raw_ex(void)
  20656. {
  20657. EXPECT_DECLS;
  20658. #if defined(HAVE_CURVE25519)
  20659. curve25519_key key;
  20660. WC_RNG rng;
  20661. byte priv[CURVE25519_KEYSIZE];
  20662. byte pub[CURVE25519_KEYSIZE];
  20663. word32 privSz = sizeof(priv);
  20664. word32 pubSz = sizeof(pub);
  20665. int endian = EC25519_BIG_ENDIAN;
  20666. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20667. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20668. ExpectIntEQ(wc_InitRng(&rng), 0);
  20669. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20670. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, priv, &privSz,
  20671. endian), 0);
  20672. ExpectIntEQ(wc_curve25519_export_public(&key, pub, &pubSz), 0);
  20673. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20674. &key, endian), 0);
  20675. /* test bad cases*/
  20676. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  20677. endian), BAD_FUNC_ARG);
  20678. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20679. &key, endian), BAD_FUNC_ARG);
  20680. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20681. &key, endian), BAD_FUNC_ARG);
  20682. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20683. NULL, endian), BAD_FUNC_ARG);
  20684. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  20685. &key, endian), ECC_BAD_ARG_E);
  20686. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  20687. &key, endian), ECC_BAD_ARG_E);
  20688. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20689. &key, EC25519_LITTLE_ENDIAN), 0);
  20690. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20691. wc_curve25519_free(&key);
  20692. #endif
  20693. return EXPECT_RESULT();
  20694. } /* END test_wc_curve25519_import_private_raw_ex*/
  20695. /*
  20696. * Testing test_wc_curve25519_import_private
  20697. */
  20698. static int test_wc_curve25519_import_private(void)
  20699. {
  20700. EXPECT_DECLS;
  20701. #if defined(HAVE_CURVE25519)
  20702. curve25519_key key;
  20703. WC_RNG rng;
  20704. byte priv[CURVE25519_KEYSIZE];
  20705. word32 privSz = sizeof(priv);
  20706. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20707. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20708. ExpectIntEQ(wc_InitRng(&rng), 0);
  20709. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20710. ExpectIntEQ(wc_curve25519_export_private_raw(&key, priv, &privSz), 0);
  20711. ExpectIntEQ(wc_curve25519_import_private(priv, privSz, &key), 0);
  20712. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20713. wc_curve25519_free(&key);
  20714. #endif
  20715. return EXPECT_RESULT();
  20716. } /* END test_wc_curve25519_import*/
  20717. /*
  20718. * Testing test_wc_curve25519_export_private_raw_ex
  20719. */
  20720. static int test_wc_curve25519_export_private_raw_ex(void)
  20721. {
  20722. EXPECT_DECLS;
  20723. #if defined(HAVE_CURVE25519)
  20724. curve25519_key key;
  20725. byte out[CURVE25519_KEYSIZE];
  20726. word32 outLen = sizeof(out);
  20727. int endian = EC25519_BIG_ENDIAN;
  20728. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20729. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  20730. 0);
  20731. /* test bad cases*/
  20732. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian),
  20733. BAD_FUNC_ARG);
  20734. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian),
  20735. BAD_FUNC_ARG);
  20736. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, NULL, &outLen,
  20737. endian), BAD_FUNC_ARG);
  20738. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, NULL, endian),
  20739. BAD_FUNC_ARG);
  20740. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  20741. EC25519_LITTLE_ENDIAN), 0);
  20742. outLen = outLen - 2;
  20743. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  20744. ECC_BAD_ARG_E);
  20745. wc_curve25519_free(&key);
  20746. #endif
  20747. return EXPECT_RESULT();
  20748. } /* END test_wc_curve25519_export_private_raw_ex*/
  20749. /*
  20750. * Testing wc_ed448_make_key().
  20751. */
  20752. static int test_wc_ed448_make_key(void)
  20753. {
  20754. EXPECT_DECLS;
  20755. #if defined(HAVE_ED448)
  20756. ed448_key key;
  20757. WC_RNG rng;
  20758. unsigned char pubkey[ED448_PUB_KEY_SIZE];
  20759. XMEMSET(&key, 0, sizeof(ed448_key));
  20760. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20761. ExpectIntEQ(wc_ed448_init(&key), 0);
  20762. ExpectIntEQ(wc_InitRng(&rng), 0);
  20763. ExpectIntEQ(wc_ed448_make_public(&key, pubkey, sizeof(pubkey)),
  20764. ECC_PRIV_KEY_E);
  20765. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20766. /* Test bad args. */
  20767. ExpectIntEQ(wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key), BAD_FUNC_ARG);
  20768. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL), BAD_FUNC_ARG);
  20769. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key),
  20770. BAD_FUNC_ARG);
  20771. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key),
  20772. BAD_FUNC_ARG);
  20773. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20774. wc_ed448_free(&key);
  20775. #endif
  20776. return EXPECT_RESULT();
  20777. } /* END test_wc_ed448_make_key */
  20778. /*
  20779. * Testing wc_ed448_init()
  20780. */
  20781. static int test_wc_ed448_init(void)
  20782. {
  20783. EXPECT_DECLS;
  20784. #if defined(HAVE_ED448)
  20785. ed448_key key;
  20786. XMEMSET(&key, 0, sizeof(ed448_key));
  20787. ExpectIntEQ(wc_ed448_init(&key), 0);
  20788. /* Test bad args. */
  20789. ExpectIntEQ(wc_ed448_init(NULL), BAD_FUNC_ARG);
  20790. wc_ed448_free(&key);
  20791. #endif
  20792. return EXPECT_RESULT();
  20793. } /* END test_wc_ed448_init */
  20794. /*
  20795. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  20796. */
  20797. static int test_wc_ed448_sign_msg(void)
  20798. {
  20799. EXPECT_DECLS;
  20800. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  20801. ed448_key key;
  20802. WC_RNG rng;
  20803. byte msg[] = "Everybody gets Friday off.\n";
  20804. byte sig[ED448_SIG_SIZE];
  20805. word32 msglen = sizeof(msg);
  20806. word32 siglen = sizeof(sig);
  20807. word32 badSigLen = sizeof(sig) - 1;
  20808. #ifdef HAVE_ED448_VERIFY
  20809. int verify_ok = 0; /*1 = Verify success.*/
  20810. #endif
  20811. /* Initialize stack variables. */
  20812. XMEMSET(&key, 0, sizeof(ed448_key));
  20813. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20814. XMEMSET(sig, 0, siglen);
  20815. /* Initialize key. */
  20816. ExpectIntEQ(wc_ed448_init(&key), 0);
  20817. ExpectIntEQ(wc_InitRng(&rng), 0);
  20818. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20819. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0), 0);
  20820. ExpectIntEQ(siglen, ED448_SIG_SIZE);
  20821. /* Test bad args. */
  20822. ExpectIntEQ(wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0),
  20823. BAD_FUNC_ARG);
  20824. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0),
  20825. BAD_FUNC_ARG);
  20826. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0),
  20827. BAD_FUNC_ARG);
  20828. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0),
  20829. BAD_FUNC_ARG);
  20830. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key, NULL, 0),
  20831. BUFFER_E);
  20832. ExpectIntEQ(badSigLen, ED448_SIG_SIZE);
  20833. badSigLen -= 1;
  20834. #ifdef HAVE_ED448_VERIFY
  20835. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key,
  20836. NULL, 0), 0);
  20837. ExpectIntEQ(verify_ok, 1);
  20838. /* Test bad args. */
  20839. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg, msglen, &verify_ok,
  20840. &key, NULL, 0), BAD_FUNC_ARG);
  20841. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg, msglen, &verify_ok,
  20842. &key, NULL, 0), BAD_FUNC_ARG);
  20843. ExpectIntEQ(wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  20844. &key, NULL, 0), BAD_FUNC_ARG);
  20845. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, NULL, msglen, &verify_ok,
  20846. &key, NULL, 0), BAD_FUNC_ARG);
  20847. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, NULL,
  20848. &key, NULL, 0), BAD_FUNC_ARG);
  20849. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  20850. NULL, NULL, 0), BAD_FUNC_ARG);
  20851. ExpectIntEQ(wc_ed448_verify_msg(sig, badSigLen, msg, msglen, &verify_ok,
  20852. &key, NULL, 0), BAD_FUNC_ARG);
  20853. #endif /* Verify. */
  20854. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20855. wc_ed448_free(&key);
  20856. #endif
  20857. return EXPECT_RESULT();
  20858. } /* END test_wc_ed448_sign_msg */
  20859. /*
  20860. * Testing wc_ed448_import_public()
  20861. */
  20862. static int test_wc_ed448_import_public(void)
  20863. {
  20864. EXPECT_DECLS;
  20865. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20866. ed448_key pubKey;
  20867. WC_RNG rng;
  20868. const byte in[] =
  20869. "Ed448PublicKeyUnitTest.................................\n";
  20870. word32 inlen = sizeof(in);
  20871. XMEMSET(&pubKey, 0, sizeof(ed448_key));
  20872. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20873. ExpectIntEQ(wc_ed448_init(&pubKey), 0);
  20874. ExpectIntEQ(wc_InitRng(&rng), 0);
  20875. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey), 0);
  20876. ExpectIntEQ(wc_ed448_import_public_ex(in, inlen, &pubKey, 1), 0);
  20877. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  20878. /* Test bad args. */
  20879. ExpectIntEQ(wc_ed448_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  20880. ExpectIntEQ(wc_ed448_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  20881. ExpectIntEQ(wc_ed448_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  20882. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20883. wc_ed448_free(&pubKey);
  20884. #endif
  20885. return EXPECT_RESULT();
  20886. } /* END wc_ed448_import_public */
  20887. /*
  20888. * Testing wc_ed448_import_private_key()
  20889. */
  20890. static int test_wc_ed448_import_private_key(void)
  20891. {
  20892. EXPECT_DECLS;
  20893. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20894. ed448_key key;
  20895. WC_RNG rng;
  20896. const byte privKey[] =
  20897. "Ed448PrivateKeyUnitTest................................\n";
  20898. const byte pubKey[] =
  20899. "Ed448PublicKeyUnitTest.................................\n";
  20900. word32 privKeySz = sizeof(privKey);
  20901. word32 pubKeySz = sizeof(pubKey);
  20902. #ifdef HAVE_ED448_KEY_EXPORT
  20903. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  20904. word32 bothKeysSz = sizeof(bothKeys);
  20905. #endif
  20906. XMEMSET(&key, 0, sizeof(ed448_key));
  20907. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20908. ExpectIntEQ(wc_ed448_init(&key), 0);
  20909. ExpectIntEQ(wc_InitRng(&rng), 0);
  20910. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20911. ExpectIntEQ(wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  20912. pubKeySz, &key, 1), 0);
  20913. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20914. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20915. #ifdef HAVE_ED448_KEY_EXPORT
  20916. PRIVATE_KEY_UNLOCK();
  20917. ExpectIntEQ(wc_ed448_export_private(&key, bothKeys, &bothKeysSz), 0);
  20918. PRIVATE_KEY_LOCK();
  20919. ExpectIntEQ(wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  20920. &key, 1), 0);
  20921. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20922. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20923. #endif
  20924. /* Test bad args. */
  20925. ExpectIntEQ(wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  20926. &key), BAD_FUNC_ARG);
  20927. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, pubKeySz,
  20928. &key), BAD_FUNC_ARG);
  20929. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20930. pubKeySz, NULL), BAD_FUNC_ARG);
  20931. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  20932. pubKeySz, &key), BAD_FUNC_ARG);
  20933. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20934. pubKeySz - 1, &key), BAD_FUNC_ARG);
  20935. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, 0, &key),
  20936. BAD_FUNC_ARG);
  20937. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20938. wc_ed448_free(&key);
  20939. #endif
  20940. return EXPECT_RESULT();
  20941. } /* END test_wc_ed448_import_private_key */
  20942. /*
  20943. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  20944. */
  20945. static int test_wc_ed448_export(void)
  20946. {
  20947. EXPECT_DECLS;
  20948. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20949. ed448_key key;
  20950. WC_RNG rng;
  20951. byte priv[ED448_PRV_KEY_SIZE];
  20952. byte pub[ED448_PUB_KEY_SIZE];
  20953. word32 privSz = sizeof(priv);
  20954. word32 pubSz = sizeof(pub);
  20955. XMEMSET(&key, 0, sizeof(ed448_key));
  20956. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20957. ExpectIntEQ(wc_ed448_init(&key), 0);
  20958. ExpectIntEQ(wc_InitRng(&rng), 0);
  20959. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20960. ExpectIntEQ(wc_ed448_export_public(&key, pub, &pubSz), 0);
  20961. ExpectIntEQ(pubSz, ED448_KEY_SIZE);
  20962. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  20963. /* Test bad args. */
  20964. ExpectIntEQ(wc_ed448_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  20965. ExpectIntEQ(wc_ed448_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  20966. ExpectIntEQ(wc_ed448_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  20967. PRIVATE_KEY_UNLOCK();
  20968. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, &privSz), 0);
  20969. ExpectIntEQ(privSz, ED448_KEY_SIZE);
  20970. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  20971. /* Test bad args. */
  20972. ExpectIntEQ(wc_ed448_export_private_only(NULL, priv, &privSz),
  20973. BAD_FUNC_ARG);
  20974. ExpectIntEQ(wc_ed448_export_private_only(&key, NULL, &privSz),
  20975. BAD_FUNC_ARG);
  20976. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, NULL), BAD_FUNC_ARG);
  20977. PRIVATE_KEY_LOCK();
  20978. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20979. wc_ed448_free(&key);
  20980. #endif
  20981. return EXPECT_RESULT();
  20982. } /* END test_wc_ed448_export */
  20983. /*
  20984. * Testing wc_ed448_size()
  20985. */
  20986. static int test_wc_ed448_size(void)
  20987. {
  20988. EXPECT_DECLS;
  20989. #if defined(HAVE_ED448)
  20990. ed448_key key;
  20991. WC_RNG rng;
  20992. XMEMSET(&key, 0, sizeof(ed448_key));
  20993. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20994. ExpectIntEQ(wc_ed448_init(&key), 0);
  20995. ExpectIntEQ(wc_InitRng(&rng), 0);
  20996. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20997. ExpectIntEQ(wc_ed448_size(&key), ED448_KEY_SIZE);
  20998. /* Test bad args. */
  20999. ExpectIntEQ(wc_ed448_size(NULL), BAD_FUNC_ARG);
  21000. ExpectIntEQ(wc_ed448_sig_size(&key), ED448_SIG_SIZE);
  21001. /* Test bad args. */
  21002. ExpectIntEQ(wc_ed448_sig_size(NULL), BAD_FUNC_ARG);
  21003. ExpectIntEQ(wc_ed448_pub_size(&key), ED448_PUB_KEY_SIZE);
  21004. /* Test bad args. */
  21005. ExpectIntEQ(wc_ed448_pub_size(NULL), BAD_FUNC_ARG);
  21006. ExpectIntEQ(wc_ed448_priv_size(&key), ED448_PRV_KEY_SIZE);
  21007. /* Test bad args. */
  21008. ExpectIntEQ(wc_ed448_priv_size(NULL), BAD_FUNC_ARG);
  21009. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21010. wc_ed448_free(&key);
  21011. #endif
  21012. return EXPECT_RESULT();
  21013. } /* END test_wc_ed448_size */
  21014. /*
  21015. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  21016. */
  21017. static int test_wc_ed448_exportKey(void)
  21018. {
  21019. EXPECT_DECLS;
  21020. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21021. ed448_key key;
  21022. WC_RNG rng;
  21023. byte priv[ED448_PRV_KEY_SIZE];
  21024. byte pub[ED448_PUB_KEY_SIZE];
  21025. byte privOnly[ED448_PRV_KEY_SIZE];
  21026. word32 privSz = sizeof(priv);
  21027. word32 pubSz = sizeof(pub);
  21028. word32 privOnlySz = sizeof(privOnly);
  21029. XMEMSET(&key, 0, sizeof(ed448_key));
  21030. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21031. ExpectIntEQ(wc_ed448_init(&key), 0);
  21032. ExpectIntEQ(wc_InitRng(&rng), 0);
  21033. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21034. PRIVATE_KEY_UNLOCK();
  21035. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, &privOnlySz), 0);
  21036. /* Test bad args. */
  21037. ExpectIntEQ(wc_ed448_export_private(NULL, privOnly, &privOnlySz),
  21038. BAD_FUNC_ARG);
  21039. ExpectIntEQ(wc_ed448_export_private(&key, NULL, &privOnlySz), BAD_FUNC_ARG);
  21040. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  21041. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  21042. /* Test bad args. */
  21043. ExpectIntEQ(wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz),
  21044. BAD_FUNC_ARG);
  21045. ExpectIntEQ(wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz),
  21046. BAD_FUNC_ARG);
  21047. ExpectIntEQ(wc_ed448_export_key(&key, priv, NULL, pub, &pubSz),
  21048. BAD_FUNC_ARG);
  21049. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz),
  21050. BAD_FUNC_ARG);
  21051. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, NULL),
  21052. BAD_FUNC_ARG);
  21053. PRIVATE_KEY_LOCK();
  21054. /* Cross check output. */
  21055. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  21056. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21057. wc_ed448_free(&key);
  21058. #endif
  21059. return EXPECT_RESULT();
  21060. } /* END test_wc_ed448_exportKey */
  21061. /*
  21062. * Testing wc_Ed448PublicKeyToDer
  21063. */
  21064. static int test_wc_Ed448PublicKeyToDer(void)
  21065. {
  21066. EXPECT_DECLS;
  21067. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  21068. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  21069. ed448_key key;
  21070. byte derBuf[1024];
  21071. XMEMSET(&key, 0, sizeof(ed448_key));
  21072. /* Test bad args */
  21073. ExpectIntEQ(wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  21074. ExpectIntEQ(wc_ed448_init(&key), 0);
  21075. ExpectIntEQ(wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  21076. wc_ed448_free(&key);
  21077. /* Test good args */
  21078. if (EXPECT_SUCCESS()) {
  21079. WC_RNG rng;
  21080. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21081. ExpectIntEQ(wc_ed448_init(&key), 0);
  21082. ExpectIntEQ(wc_InitRng(&rng), 0);
  21083. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21084. ExpectIntGT(wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  21085. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21086. wc_ed448_free(&key);
  21087. }
  21088. #endif
  21089. return EXPECT_RESULT();
  21090. } /* END testing wc_Ed448PublicKeyToDer */
  21091. /*
  21092. * Testing wc_curve448_init and wc_curve448_free.
  21093. */
  21094. static int test_wc_curve448_init(void)
  21095. {
  21096. EXPECT_DECLS;
  21097. #if defined(HAVE_CURVE448)
  21098. curve448_key key;
  21099. /* Test bad args for wc_curve448_init */
  21100. ExpectIntEQ(wc_curve448_init(&key), 0);
  21101. /* Test bad args for wc_curve448_init */
  21102. ExpectIntEQ(wc_curve448_init(NULL), BAD_FUNC_ARG);
  21103. /* Test good args for wc_curve_448_free */
  21104. wc_curve448_free(&key);
  21105. /* Test bad args for wc_curve448_free */
  21106. wc_curve448_free(NULL);
  21107. #endif
  21108. return EXPECT_RESULT();
  21109. } /* END test_wc_curve448_init and wc_curve_448_free*/
  21110. /*
  21111. * Testing wc_curve448_make_key
  21112. */
  21113. static int test_wc_curve448_make_key(void)
  21114. {
  21115. EXPECT_DECLS;
  21116. #if defined(HAVE_CURVE448)
  21117. curve448_key key;
  21118. WC_RNG rng;
  21119. int keysize;
  21120. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21121. ExpectIntEQ(wc_curve448_init(&key), 0);
  21122. ExpectIntEQ(wc_InitRng(&rng), 0);
  21123. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21124. ExpectIntEQ(keysize = wc_curve448_size(&key), CURVE448_KEY_SIZE);
  21125. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, &key), 0);
  21126. /* test bad cases */
  21127. ExpectIntEQ(wc_curve448_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  21128. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  21129. ExpectIntEQ(wc_curve448_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  21130. ExpectIntEQ(wc_curve448_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  21131. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21132. wc_curve448_free(&key);
  21133. #endif
  21134. return EXPECT_RESULT();
  21135. } /* END test_wc_curve448_make_key*/
  21136. /*
  21137. * Testing test_wc_curve448_shared_secret_ex
  21138. */
  21139. static int test_wc_curve448_shared_secret_ex(void)
  21140. {
  21141. EXPECT_DECLS;
  21142. #if defined(HAVE_CURVE448)
  21143. curve448_key private_key;
  21144. curve448_key public_key;
  21145. WC_RNG rng;
  21146. byte out[CURVE448_KEY_SIZE];
  21147. word32 outLen = sizeof(out);
  21148. int endian = EC448_BIG_ENDIAN;
  21149. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21150. ExpectIntEQ(wc_curve448_init(&private_key), 0);
  21151. ExpectIntEQ(wc_InitRng(&rng), 0);
  21152. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key), 0);
  21153. ExpectIntEQ(wc_curve448_init(&public_key), 0);
  21154. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key), 0);
  21155. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21156. &outLen, endian), 0);
  21157. /* test bad cases */
  21158. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  21159. BAD_FUNC_ARG);
  21160. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, &public_key, out, &outLen,
  21161. endian), BAD_FUNC_ARG);
  21162. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, NULL, out, &outLen,
  21163. endian), BAD_FUNC_ARG);
  21164. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  21165. &outLen, endian), BAD_FUNC_ARG);
  21166. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21167. NULL, endian), BAD_FUNC_ARG);
  21168. outLen = outLen - 2;
  21169. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21170. &outLen, endian), BAD_FUNC_ARG);
  21171. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21172. wc_curve448_free(&private_key);
  21173. wc_curve448_free(&public_key);
  21174. #endif
  21175. return EXPECT_RESULT();
  21176. } /* END test_wc_curve448_shared_secret_ex*/
  21177. /*
  21178. * Testing test_wc_curve448_export_public_ex
  21179. */
  21180. static int test_wc_curve448_export_public_ex(void)
  21181. {
  21182. EXPECT_DECLS;
  21183. #if defined(HAVE_CURVE448)
  21184. WC_RNG rng;
  21185. curve448_key key;
  21186. byte out[CURVE448_KEY_SIZE];
  21187. word32 outLen = sizeof(out);
  21188. int endian = EC448_BIG_ENDIAN;
  21189. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21190. ExpectIntEQ(wc_curve448_init(&key), 0);
  21191. ExpectIntEQ(wc_InitRng(&rng), 0);
  21192. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21193. ExpectIntEQ(wc_curve448_export_public(&key, out, &outLen), 0);
  21194. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian), 0);
  21195. /* test bad cases*/
  21196. ExpectIntEQ(wc_curve448_export_public_ex(NULL, NULL, NULL, endian),
  21197. BAD_FUNC_ARG);
  21198. ExpectIntEQ(wc_curve448_export_public_ex(NULL, out, &outLen, endian),
  21199. BAD_FUNC_ARG);
  21200. ExpectIntEQ(wc_curve448_export_public_ex(&key, NULL, &outLen, endian),
  21201. BAD_FUNC_ARG);
  21202. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, NULL, endian),
  21203. BAD_FUNC_ARG);
  21204. outLen = outLen - 2;
  21205. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian),
  21206. ECC_BAD_ARG_E);
  21207. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21208. wc_curve448_free(&key);
  21209. #endif
  21210. return EXPECT_RESULT();
  21211. } /* END test_wc_curve448_export_public_ex*/
  21212. /*
  21213. * Testing test_wc_curve448_export_private_raw_ex
  21214. */
  21215. static int test_wc_curve448_export_private_raw_ex(void)
  21216. {
  21217. EXPECT_DECLS;
  21218. #if defined(HAVE_CURVE448)
  21219. curve448_key key;
  21220. byte out[CURVE448_KEY_SIZE];
  21221. word32 outLen = sizeof(out);
  21222. int endian = EC448_BIG_ENDIAN;
  21223. ExpectIntEQ(wc_curve448_init(&key), 0);
  21224. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  21225. 0);
  21226. /* test bad cases*/
  21227. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian),
  21228. BAD_FUNC_ARG);
  21229. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian),
  21230. BAD_FUNC_ARG);
  21231. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian),
  21232. BAD_FUNC_ARG);
  21233. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, NULL, endian),
  21234. BAD_FUNC_ARG);
  21235. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen,
  21236. EC448_LITTLE_ENDIAN), 0);
  21237. outLen = outLen - 2;
  21238. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  21239. ECC_BAD_ARG_E);
  21240. wc_curve448_free(&key);
  21241. #endif
  21242. return EXPECT_RESULT();
  21243. } /* END test_wc_curve448_export_private_raw_ex*/
  21244. /*
  21245. * Testing test_wc_curve448_import_private_raw_ex
  21246. */
  21247. static int test_wc_curve448_import_private_raw_ex(void)
  21248. {
  21249. EXPECT_DECLS;
  21250. #if defined(HAVE_CURVE448)
  21251. curve448_key key;
  21252. WC_RNG rng;
  21253. byte priv[CURVE448_KEY_SIZE];
  21254. byte pub[CURVE448_KEY_SIZE];
  21255. word32 privSz = sizeof(priv);
  21256. word32 pubSz = sizeof(pub);
  21257. int endian = EC448_BIG_ENDIAN;
  21258. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21259. ExpectIntEQ(wc_curve448_init(&key), 0);
  21260. ExpectIntEQ(wc_InitRng(&rng), 0);
  21261. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21262. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21263. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  21264. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21265. &key, endian), 0);
  21266. /* test bad cases */
  21267. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0),
  21268. BAD_FUNC_ARG);
  21269. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  21270. &key, endian), BAD_FUNC_ARG);
  21271. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  21272. &key, endian), BAD_FUNC_ARG);
  21273. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21274. NULL, endian), BAD_FUNC_ARG);
  21275. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  21276. &key, endian), ECC_BAD_ARG_E);
  21277. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  21278. &key, endian), ECC_BAD_ARG_E);
  21279. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21280. &key, EC448_LITTLE_ENDIAN), 0);
  21281. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21282. wc_curve448_free(&key);
  21283. #endif
  21284. return EXPECT_RESULT();
  21285. } /* END test_wc_curve448_import_private_raw_ex*/
  21286. /*
  21287. * Testing test_curve448_export_key_raw
  21288. */
  21289. static int test_wc_curve448_export_key_raw(void)
  21290. {
  21291. EXPECT_DECLS;
  21292. #if defined(HAVE_CURVE448)
  21293. curve448_key key;
  21294. WC_RNG rng;
  21295. byte priv[CURVE448_KEY_SIZE];
  21296. byte pub[CURVE448_KEY_SIZE];
  21297. word32 privSz = sizeof(priv);
  21298. word32 pubSz = sizeof(pub);
  21299. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21300. ExpectIntEQ(wc_curve448_init(&key), 0);
  21301. ExpectIntEQ(wc_InitRng(&rng), 0);
  21302. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21303. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21304. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  21305. ExpectIntEQ(wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz),
  21306. 0);
  21307. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21308. wc_curve448_free(&key);
  21309. #endif
  21310. return EXPECT_RESULT();
  21311. } /* END test_wc_curve448_import_private_raw_ex*/
  21312. /*
  21313. * Testing test_wc_curve448_import_private
  21314. */
  21315. static int test_wc_curve448_import_private(void)
  21316. {
  21317. EXPECT_DECLS;
  21318. #if defined(HAVE_CURVE448)
  21319. curve448_key key;
  21320. WC_RNG rng;
  21321. byte priv[CURVE448_KEY_SIZE];
  21322. word32 privSz = sizeof(priv);
  21323. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21324. ExpectIntEQ(wc_curve448_init(&key), 0);
  21325. ExpectIntEQ(wc_InitRng(&rng), 0);
  21326. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21327. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21328. ExpectIntEQ(wc_curve448_import_private(priv, privSz, &key), 0);
  21329. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21330. wc_curve448_free(&key);
  21331. #endif
  21332. return EXPECT_RESULT();
  21333. } /* END test_wc_curve448_import*/
  21334. /*
  21335. * Testing test_wc_curve448_size.
  21336. */
  21337. static int test_wc_curve448_size(void)
  21338. {
  21339. EXPECT_DECLS;
  21340. #if defined(HAVE_CURVE448)
  21341. curve448_key key;
  21342. ExpectIntEQ(wc_curve448_init(&key), 0);
  21343. /* Test good args for wc_curve448_size */
  21344. ExpectIntEQ(wc_curve448_size(&key), CURVE448_KEY_SIZE);
  21345. /* Test bad args for wc_curve448_size */
  21346. ExpectIntEQ(wc_curve448_size(NULL), 0);
  21347. wc_curve448_free(&key);
  21348. #endif
  21349. return EXPECT_RESULT();
  21350. } /* END test_wc_curve448_size*/
  21351. /*
  21352. * Testing wc_ecc_make_key.
  21353. */
  21354. static int test_wc_ecc_make_key(void)
  21355. {
  21356. EXPECT_DECLS;
  21357. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21358. ecc_key key;
  21359. WC_RNG rng;
  21360. int ret;
  21361. XMEMSET(&key, 0, sizeof(ecc_key));
  21362. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21363. ExpectIntEQ(wc_ecc_init(&key), 0);
  21364. ExpectIntEQ(wc_InitRng(&rng), 0);
  21365. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21366. #if defined(WOLFSSL_ASYNC_CRYPT)
  21367. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21368. #endif
  21369. ExpectIntEQ(ret, 0);
  21370. /* Pass in bad args. */
  21371. ExpectIntEQ(wc_ecc_make_key(NULL, KEY14, &key), BAD_FUNC_ARG);
  21372. ExpectIntEQ(wc_ecc_make_key(&rng, KEY14, NULL), BAD_FUNC_ARG);
  21373. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21374. wc_ecc_free(&key);
  21375. #ifdef FP_ECC
  21376. wc_ecc_fp_free();
  21377. #endif
  21378. #endif
  21379. return EXPECT_RESULT();
  21380. } /* END test_wc_ecc_make_key */
  21381. /*
  21382. * Testing wc_ecc_init()
  21383. */
  21384. static int test_wc_ecc_init(void)
  21385. {
  21386. EXPECT_DECLS;
  21387. #ifdef HAVE_ECC
  21388. ecc_key key;
  21389. XMEMSET(&key, 0, sizeof(ecc_key));
  21390. ExpectIntEQ(wc_ecc_init(&key), 0);
  21391. /* Pass in bad args. */
  21392. ExpectIntEQ(wc_ecc_init(NULL), BAD_FUNC_ARG);
  21393. wc_ecc_free(&key);
  21394. #endif
  21395. return EXPECT_RESULT();
  21396. } /* END test_wc_ecc_init */
  21397. /*
  21398. * Testing wc_ecc_check_key()
  21399. */
  21400. static int test_wc_ecc_check_key(void)
  21401. {
  21402. EXPECT_DECLS;
  21403. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21404. ecc_key key;
  21405. WC_RNG rng;
  21406. int ret;
  21407. XMEMSET(&rng, 0, sizeof(rng));
  21408. XMEMSET(&key, 0, sizeof(key));
  21409. ExpectIntEQ(wc_ecc_init(&key), 0);
  21410. ExpectIntEQ(wc_InitRng(&rng), 0);
  21411. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21412. #if defined(WOLFSSL_ASYNC_CRYPT)
  21413. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21414. #endif
  21415. ExpectIntEQ(ret, 0);
  21416. ExpectIntEQ(wc_ecc_check_key(&key), 0);
  21417. /* Pass in bad args. */
  21418. ExpectIntEQ(wc_ecc_check_key(NULL), BAD_FUNC_ARG);
  21419. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21420. wc_ecc_free(&key);
  21421. #ifdef FP_ECC
  21422. wc_ecc_fp_free();
  21423. #endif
  21424. #endif
  21425. return EXPECT_RESULT();
  21426. } /* END test_wc_ecc_check_key */
  21427. /*
  21428. * Testing wc_ecc_get_generator()
  21429. */
  21430. static int test_wc_ecc_get_generator(void)
  21431. {
  21432. EXPECT_DECLS;
  21433. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  21434. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  21435. ecc_point* pt = NULL;
  21436. ExpectNotNull(pt = wc_ecc_new_point());
  21437. ExpectIntEQ(wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  21438. MP_OKAY);
  21439. /* Test bad args. */
  21440. /* Returns Zero for bad arg. */
  21441. ExpectIntNE(wc_ecc_get_generator(pt, -1), MP_OKAY);
  21442. ExpectIntNE(wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  21443. MP_OKAY);
  21444. /* If we ever get to 1000 curves increase this number */
  21445. ExpectIntNE(wc_ecc_get_generator(pt, 1000), MP_OKAY);
  21446. ExpectIntNE(wc_ecc_get_generator(NULL, -1), MP_OKAY);
  21447. wc_ecc_del_point(pt);
  21448. #endif
  21449. return EXPECT_RESULT();
  21450. } /* END test_wc_ecc_get_generator */
  21451. /*
  21452. * Testing wc_ecc_size()
  21453. */
  21454. static int test_wc_ecc_size(void)
  21455. {
  21456. EXPECT_DECLS;
  21457. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21458. WC_RNG rng;
  21459. ecc_key key;
  21460. int ret;
  21461. XMEMSET(&key, 0, sizeof(ecc_key));
  21462. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21463. ExpectIntEQ(wc_ecc_init(&key), 0);
  21464. ExpectIntEQ(wc_InitRng(&rng), 0);
  21465. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21466. #if defined(WOLFSSL_ASYNC_CRYPT)
  21467. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21468. #endif
  21469. ExpectIntEQ(ret, 0);
  21470. ExpectIntEQ(wc_ecc_size(&key), KEY14);
  21471. /* Test bad args. */
  21472. /* Returns Zero for bad arg. */
  21473. ExpectIntEQ(wc_ecc_size(NULL), 0);
  21474. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21475. wc_ecc_free(&key);
  21476. #endif
  21477. return EXPECT_RESULT();
  21478. } /* END test_wc_ecc_size */
  21479. static int test_wc_ecc_params(void)
  21480. {
  21481. EXPECT_DECLS;
  21482. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  21483. It was added after certifications */
  21484. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  21485. const ecc_set_type* ecc_set;
  21486. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  21487. /* Test for SECP256R1 curve */
  21488. int curve_id = ECC_SECP256R1;
  21489. int curve_idx;
  21490. ExpectIntNE(curve_idx = wc_ecc_get_curve_idx(curve_id), ECC_CURVE_INVALID);
  21491. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(curve_idx));
  21492. ExpectIntEQ(ecc_set->id, curve_id);
  21493. #endif
  21494. /* Test case when SECP256R1 is not enabled */
  21495. /* Test that we get curve params for index 0 */
  21496. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(0));
  21497. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  21498. return EXPECT_RESULT();
  21499. }
  21500. /*
  21501. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  21502. */
  21503. static int test_wc_ecc_signVerify_hash(void)
  21504. {
  21505. EXPECT_DECLS;
  21506. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  21507. ecc_key key;
  21508. WC_RNG rng;
  21509. int ret;
  21510. #ifdef HAVE_ECC_VERIFY
  21511. int verify = 0;
  21512. #endif
  21513. word32 siglen = ECC_BUFSIZE;
  21514. byte sig[ECC_BUFSIZE];
  21515. byte adjustedSig[ECC_BUFSIZE+1];
  21516. byte digest[] = TEST_STRING;
  21517. word32 digestlen = (word32)TEST_STRING_SZ;
  21518. /* Init stack var */
  21519. XMEMSET(&key, 0, sizeof(ecc_key));
  21520. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21521. XMEMSET(sig, 0, siglen);
  21522. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  21523. /* Init structs. */
  21524. ExpectIntEQ(wc_ecc_init(&key), 0);
  21525. ExpectIntEQ(wc_InitRng(&rng), 0);
  21526. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21527. #if defined(WOLFSSL_ASYNC_CRYPT)
  21528. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21529. #endif
  21530. ExpectIntEQ(ret, 0);
  21531. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key),
  21532. 0);
  21533. /* Check bad args. */
  21534. ExpectIntEQ(wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key),
  21535. ECC_BAD_ARG_E);
  21536. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, NULL, &siglen, &rng, &key),
  21537. ECC_BAD_ARG_E);
  21538. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, NULL, &rng, &key),
  21539. ECC_BAD_ARG_E);
  21540. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, NULL, &key),
  21541. ECC_BAD_ARG_E);
  21542. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, NULL),
  21543. ECC_BAD_ARG_E);
  21544. #ifdef HAVE_ECC_VERIFY
  21545. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  21546. &key), 0);
  21547. ExpectIntEQ(verify, 1);
  21548. /* test check on length of signature passed in */
  21549. XMEMCPY(adjustedSig, sig, siglen);
  21550. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  21551. #ifndef NO_STRICT_ECDSA_LEN
  21552. ExpectIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21553. &verify, &key), 0);
  21554. #else
  21555. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  21556. * is allowed */
  21557. ExpectIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21558. &verify, &key), 0);
  21559. #endif
  21560. /* Test bad args. */
  21561. ExpectIntEQ(wc_ecc_verify_hash(NULL, siglen, digest, digestlen, &verify,
  21562. &key), ECC_BAD_ARG_E);
  21563. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, NULL, digestlen, &verify, &key),
  21564. ECC_BAD_ARG_E);
  21565. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, NULL, &key),
  21566. ECC_BAD_ARG_E);
  21567. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  21568. NULL), ECC_BAD_ARG_E);
  21569. #endif /* HAVE_ECC_VERIFY */
  21570. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21571. wc_ecc_free(&key);
  21572. #ifdef FP_ECC
  21573. wc_ecc_fp_free();
  21574. #endif
  21575. #endif
  21576. return EXPECT_RESULT();
  21577. } /* END test_wc_ecc_sign_hash */
  21578. /*
  21579. * Testing wc_ecc_shared_secret()
  21580. */
  21581. static int test_wc_ecc_shared_secret(void)
  21582. {
  21583. EXPECT_DECLS;
  21584. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  21585. ecc_key key;
  21586. ecc_key pubKey;
  21587. WC_RNG rng;
  21588. #if defined(NO_ECC256)
  21589. int ret;
  21590. #endif
  21591. byte out[KEY32];
  21592. int keySz = sizeof(out);
  21593. word32 outlen = (word32)sizeof(out);
  21594. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21595. const char* qx =
  21596. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21597. const char* qy =
  21598. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21599. const char* d =
  21600. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21601. const char* curveName = "SECP256R1";
  21602. const byte expected_shared_secret[] =
  21603. {
  21604. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  21605. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  21606. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  21607. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  21608. };
  21609. #endif
  21610. PRIVATE_KEY_UNLOCK();
  21611. /* Initialize variables. */
  21612. XMEMSET(&key, 0, sizeof(ecc_key));
  21613. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  21614. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21615. XMEMSET(out, 0, keySz);
  21616. ExpectIntEQ(wc_ecc_init(&key), 0);
  21617. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  21618. ExpectIntEQ(wc_InitRng(&rng), 0);
  21619. #if !defined(NO_ECC256)
  21620. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  21621. ExpectIntEQ(wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName), 0);
  21622. #else
  21623. ret = wc_ecc_make_key(&rng, keySz, &key);
  21624. #if defined(WOLFSSL_ASYNC_CRYPT)
  21625. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21626. #endif
  21627. ExpectIntEQ(ret, 0);
  21628. ret = wc_ecc_make_key(&rng, keySz, &key);
  21629. #if defined(WOLFSSL_ASYNC_CRYPT)
  21630. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21631. #endif
  21632. ExpectIntEQ(ret, 0);
  21633. #endif
  21634. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21635. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21636. !defined(HAVE_SELFTEST)
  21637. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  21638. #endif
  21639. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen), 0);
  21640. #if !defined(NO_ECC256)
  21641. ExpectIntEQ(XMEMCMP(out, expected_shared_secret, outlen), 0);
  21642. #endif
  21643. /* Test bad args. */
  21644. ExpectIntEQ(wc_ecc_shared_secret(NULL, &pubKey, out, &outlen),
  21645. BAD_FUNC_ARG);
  21646. ExpectIntEQ(wc_ecc_shared_secret(&key, NULL, out, &outlen),
  21647. BAD_FUNC_ARG);
  21648. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen),
  21649. BAD_FUNC_ARG);
  21650. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, NULL),
  21651. BAD_FUNC_ARG);
  21652. /* Invalid length */
  21653. outlen = 1;
  21654. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen),
  21655. BUFFER_E);
  21656. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21657. wc_ecc_free(&pubKey);
  21658. wc_ecc_free(&key);
  21659. #ifdef FP_ECC
  21660. wc_ecc_fp_free();
  21661. #endif
  21662. PRIVATE_KEY_LOCK();
  21663. #endif
  21664. return EXPECT_RESULT();
  21665. } /* END tests_wc_ecc_shared_secret */
  21666. /*
  21667. * testint wc_ecc_export_x963()
  21668. */
  21669. static int test_wc_ecc_export_x963(void)
  21670. {
  21671. EXPECT_DECLS;
  21672. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21673. ecc_key key;
  21674. WC_RNG rng;
  21675. byte out[ECC_ASN963_MAX_BUF_SZ];
  21676. word32 outlen = sizeof(out);
  21677. int ret;
  21678. PRIVATE_KEY_UNLOCK();
  21679. /* Initialize variables. */
  21680. XMEMSET(&key, 0, sizeof(ecc_key));
  21681. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21682. XMEMSET(out, 0, outlen);
  21683. ExpectIntEQ(wc_ecc_init(&key), 0);
  21684. ExpectIntEQ(wc_InitRng(&rng), 0);
  21685. ret = wc_ecc_make_key(&rng, KEY20, &key);
  21686. #if defined(WOLFSSL_ASYNC_CRYPT)
  21687. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21688. #endif
  21689. ExpectIntEQ(ret, 0);
  21690. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), 0);
  21691. /* Test bad args. */
  21692. ExpectIntEQ(wc_ecc_export_x963(NULL, out, &outlen), ECC_BAD_ARG_E);
  21693. ExpectIntEQ(wc_ecc_export_x963(&key, NULL, &outlen), LENGTH_ONLY_E);
  21694. ExpectIntEQ(wc_ecc_export_x963(&key, out, NULL), ECC_BAD_ARG_E);
  21695. key.idx = -4;
  21696. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), ECC_BAD_ARG_E);
  21697. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21698. wc_ecc_free(&key);
  21699. #ifdef FP_ECC
  21700. wc_ecc_fp_free();
  21701. #endif
  21702. PRIVATE_KEY_LOCK();
  21703. #endif
  21704. return EXPECT_RESULT();
  21705. } /* END test_wc_ecc_export_x963 */
  21706. /*
  21707. * Testing wc_ecc_export_x963_ex()
  21708. * compile with --enable-compkey will use compression.
  21709. */
  21710. static int test_wc_ecc_export_x963_ex(void)
  21711. {
  21712. EXPECT_DECLS;
  21713. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21714. ecc_key key;
  21715. WC_RNG rng;
  21716. int ret;
  21717. byte out[ECC_ASN963_MAX_BUF_SZ];
  21718. word32 outlen = sizeof(out);
  21719. #ifdef HAVE_COMP_KEY
  21720. word32 badOutLen = 5;
  21721. #endif
  21722. /* Init stack variables. */
  21723. XMEMSET(&key, 0, sizeof(ecc_key));
  21724. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21725. XMEMSET(out, 0, outlen);
  21726. ExpectIntEQ(wc_ecc_init(&key), 0);
  21727. ExpectIntEQ(wc_InitRng(&rng), 0);
  21728. ret = wc_ecc_make_key(&rng, KEY64, &key);
  21729. #if defined(WOLFSSL_ASYNC_CRYPT)
  21730. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21731. #endif
  21732. ExpectIntEQ(ret, 0);
  21733. #ifdef HAVE_COMP_KEY
  21734. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), 0);
  21735. #else
  21736. ExpectIntEQ(ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP), 0);
  21737. #endif
  21738. /* Test bad args. */
  21739. #ifdef HAVE_COMP_KEY
  21740. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, COMP), BAD_FUNC_ARG);
  21741. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP), BAD_FUNC_ARG);
  21742. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, COMP), BAD_FUNC_ARG);
  21743. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  21744. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP), BUFFER_E);
  21745. #else
  21746. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP),
  21747. LENGTH_ONLY_E);
  21748. #endif
  21749. key.idx = -4;
  21750. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), ECC_BAD_ARG_E);
  21751. #else
  21752. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP),
  21753. ECC_BAD_ARG_E);
  21754. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP),
  21755. LENGTH_ONLY_E);
  21756. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, 1), NOT_COMPILED_IN);
  21757. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP),
  21758. ECC_BAD_ARG_E);
  21759. key.idx = -4;
  21760. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP),
  21761. ECC_BAD_ARG_E);
  21762. #endif
  21763. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21764. wc_ecc_free(&key);
  21765. #ifdef FP_ECC
  21766. wc_ecc_fp_free();
  21767. #endif
  21768. #endif
  21769. return EXPECT_RESULT();
  21770. } /* END test_wc_ecc_export_x963_ex */
  21771. /*
  21772. * testing wc_ecc_import_x963()
  21773. */
  21774. static int test_wc_ecc_import_x963(void)
  21775. {
  21776. EXPECT_DECLS;
  21777. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21778. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21779. ecc_key pubKey;
  21780. ecc_key key;
  21781. WC_RNG rng;
  21782. byte x963[ECC_ASN963_MAX_BUF_SZ];
  21783. word32 x963Len = (word32)sizeof(x963);
  21784. int ret;
  21785. /* Init stack variables. */
  21786. XMEMSET(&key, 0, sizeof(ecc_key));
  21787. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  21788. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21789. XMEMSET(x963, 0, x963Len);
  21790. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  21791. ExpectIntEQ(wc_ecc_init(&key), 0);
  21792. ExpectIntEQ(wc_InitRng(&rng), 0);
  21793. #if FIPS_VERSION3_GE(6,0,0)
  21794. ret = wc_ecc_make_key(&rng, KEY32, &key);
  21795. #else
  21796. ret = wc_ecc_make_key(&rng, KEY24, &key);
  21797. #endif
  21798. #if defined(WOLFSSL_ASYNC_CRYPT)
  21799. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21800. #endif
  21801. ExpectIntEQ(ret, 0);
  21802. PRIVATE_KEY_UNLOCK();
  21803. ExpectIntEQ(wc_ecc_export_x963(&key, x963, &x963Len), 0);
  21804. PRIVATE_KEY_LOCK();
  21805. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, &pubKey), 0);
  21806. /* Test bad args. */
  21807. ExpectIntEQ(wc_ecc_import_x963(NULL, x963Len, &pubKey), BAD_FUNC_ARG);
  21808. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, NULL), BAD_FUNC_ARG);
  21809. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len + 1, &pubKey), ECC_BAD_ARG_E);
  21810. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21811. wc_ecc_free(&key);
  21812. wc_ecc_free(&pubKey);
  21813. #ifdef FP_ECC
  21814. wc_ecc_fp_free();
  21815. #endif
  21816. #endif
  21817. return EXPECT_RESULT();
  21818. } /* END wc_ecc_import_x963 */
  21819. /*
  21820. * testing wc_ecc_import_private_key()
  21821. */
  21822. static int test_wc_ecc_import_private_key(void)
  21823. {
  21824. EXPECT_DECLS;
  21825. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21826. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21827. ecc_key key;
  21828. ecc_key keyImp;
  21829. WC_RNG rng;
  21830. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  21831. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  21832. word32 privKeySz = (word32)sizeof(privKey);
  21833. word32 x963KeySz = (word32)sizeof(x963Key);
  21834. int ret;
  21835. /* Init stack variables. */
  21836. XMEMSET(&key, 0, sizeof(ecc_key));
  21837. XMEMSET(&keyImp, 0, sizeof(ecc_key));
  21838. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21839. XMEMSET(privKey, 0, privKeySz);
  21840. XMEMSET(x963Key, 0, x963KeySz);
  21841. ExpectIntEQ(wc_ecc_init(&key), 0);
  21842. ExpectIntEQ(wc_ecc_init(&keyImp), 0);
  21843. ExpectIntEQ(wc_InitRng(&rng), 0);
  21844. ret = wc_ecc_make_key(&rng, KEY48, &key);
  21845. #if defined(WOLFSSL_ASYNC_CRYPT)
  21846. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21847. #endif
  21848. ExpectIntEQ(ret, 0);
  21849. PRIVATE_KEY_UNLOCK();
  21850. ExpectIntEQ(wc_ecc_export_x963(&key, x963Key, &x963KeySz), 0);
  21851. PRIVATE_KEY_LOCK();
  21852. ExpectIntEQ(wc_ecc_export_private_only(&key, privKey, &privKeySz), 0);
  21853. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21854. x963KeySz, &keyImp), 0);
  21855. /* Pass in bad args. */
  21856. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21857. x963KeySz, NULL), BAD_FUNC_ARG);
  21858. ExpectIntEQ(wc_ecc_import_private_key(NULL, privKeySz, x963Key, x963KeySz,
  21859. &keyImp), BAD_FUNC_ARG);
  21860. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21861. wc_ecc_free(&keyImp);
  21862. wc_ecc_free(&key);
  21863. #ifdef FP_ECC
  21864. wc_ecc_fp_free();
  21865. #endif
  21866. #endif
  21867. return EXPECT_RESULT();
  21868. } /* END test_wc_ecc_import_private_key */
  21869. /*
  21870. * Testing wc_ecc_export_private_only()
  21871. */
  21872. static int test_wc_ecc_export_private_only(void)
  21873. {
  21874. EXPECT_DECLS;
  21875. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21876. ecc_key key;
  21877. WC_RNG rng;
  21878. byte out[ECC_PRIV_KEY_BUF];
  21879. word32 outlen = sizeof(out);
  21880. int ret;
  21881. /* Init stack variables. */
  21882. XMEMSET(&key, 0, sizeof(ecc_key));
  21883. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21884. XMEMSET(out, 0, outlen);
  21885. ExpectIntEQ(wc_ecc_init(&key), 0);
  21886. ExpectIntEQ(wc_InitRng(&rng), 0);
  21887. ret = wc_ecc_make_key(&rng, KEY32, &key);
  21888. #if defined(WOLFSSL_ASYNC_CRYPT)
  21889. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21890. #endif
  21891. ExpectIntEQ(ret, 0);
  21892. ExpectIntEQ(wc_ecc_export_private_only(&key, out, &outlen), 0);
  21893. /* Pass in bad args. */
  21894. ExpectIntEQ(wc_ecc_export_private_only(NULL, out, &outlen), BAD_FUNC_ARG);
  21895. ExpectIntEQ(wc_ecc_export_private_only(&key, NULL, &outlen), BAD_FUNC_ARG);
  21896. ExpectIntEQ(wc_ecc_export_private_only(&key, out, NULL), BAD_FUNC_ARG);
  21897. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21898. wc_ecc_free(&key);
  21899. #ifdef FP_ECC
  21900. wc_ecc_fp_free();
  21901. #endif
  21902. #endif
  21903. return EXPECT_RESULT();
  21904. } /* END test_wc_ecc_export_private_only */
  21905. /*
  21906. * Testing wc_ecc_rs_to_sig()
  21907. */
  21908. static int test_wc_ecc_rs_to_sig(void)
  21909. {
  21910. EXPECT_DECLS;
  21911. #if defined(HAVE_ECC) && !defined(NO_ASN)
  21912. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  21913. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  21914. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  21915. const char* zeroStr = "0";
  21916. byte sig[ECC_MAX_SIG_SIZE];
  21917. word32 siglen = (word32)sizeof(sig);
  21918. /* R and S max size is the order of curve. 2^192.*/
  21919. int keySz = KEY24;
  21920. byte r[KEY24];
  21921. byte s[KEY24];
  21922. word32 rlen = (word32)sizeof(r);
  21923. word32 slen = (word32)sizeof(s);
  21924. /* Init stack variables. */
  21925. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  21926. XMEMSET(r, 0, keySz);
  21927. XMEMSET(s, 0, keySz);
  21928. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, &siglen), 0);
  21929. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen), 0);
  21930. /* Test bad args. */
  21931. ExpectIntEQ(wc_ecc_rs_to_sig(NULL, S, sig, &siglen), ECC_BAD_ARG_E);
  21932. ExpectIntEQ(wc_ecc_rs_to_sig(R, NULL, sig, &siglen), ECC_BAD_ARG_E);
  21933. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, NULL), ECC_BAD_ARG_E);
  21934. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, NULL, &siglen), ECC_BAD_ARG_E);
  21935. ExpectIntEQ(wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen), MP_ZERO_E);
  21936. ExpectIntEQ(wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen), MP_ZERO_E);
  21937. ExpectIntEQ(wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen),
  21938. ECC_BAD_ARG_E);
  21939. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen),
  21940. ECC_BAD_ARG_E);
  21941. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen),
  21942. ECC_BAD_ARG_E);
  21943. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen),
  21944. ECC_BAD_ARG_E);
  21945. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL),
  21946. ECC_BAD_ARG_E);
  21947. #endif
  21948. return EXPECT_RESULT();
  21949. } /* END test_wc_ecc_rs_to_sig */
  21950. static int test_wc_ecc_import_raw(void)
  21951. {
  21952. EXPECT_DECLS;
  21953. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21954. ecc_key key;
  21955. const char* qx =
  21956. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21957. const char* qy =
  21958. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21959. const char* d =
  21960. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21961. const char* curveName = "SECP256R1";
  21962. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21963. const char* kNullStr = "";
  21964. int ret;
  21965. #endif
  21966. XMEMSET(&key, 0, sizeof(ecc_key));
  21967. ExpectIntEQ(wc_ecc_init(&key), 0);
  21968. /* Test good import */
  21969. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  21970. /* Test bad args. */
  21971. ExpectIntEQ(wc_ecc_import_raw(NULL, qx, qy, d, curveName), BAD_FUNC_ARG);
  21972. ExpectIntEQ(wc_ecc_import_raw(&key, NULL, qy, d, curveName), BAD_FUNC_ARG);
  21973. ExpectIntEQ(wc_ecc_import_raw(&key, qx, NULL, d, curveName), BAD_FUNC_ARG);
  21974. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, NULL), BAD_FUNC_ARG);
  21975. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21976. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21977. wc_ecc_free(&key);
  21978. #endif
  21979. ExpectIntLT(ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr,
  21980. curveName), 0);
  21981. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  21982. #endif
  21983. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  21984. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21985. wc_ecc_free(&key);
  21986. #endif
  21987. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21988. ExpectIntLT(ret = wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  21989. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  21990. #else
  21991. ExpectIntEQ(wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  21992. #endif
  21993. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21994. wc_ecc_free(&key);
  21995. #endif
  21996. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21997. ExpectIntLT(ret = wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  21998. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  21999. #else
  22000. ExpectIntEQ(wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  22001. #endif
  22002. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22003. wc_ecc_free(&key);
  22004. #endif
  22005. ExpectIntEQ(wc_ecc_import_raw(&key, "0", "0", d, curveName), ECC_INF_E);
  22006. #endif
  22007. wc_ecc_free(&key);
  22008. #endif
  22009. return EXPECT_RESULT();
  22010. } /* END test_wc_ecc_import_raw */
  22011. static int test_wc_ecc_import_unsigned(void)
  22012. {
  22013. EXPECT_DECLS;
  22014. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22015. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  22016. HAVE_FIPS_VERSION >= 2))
  22017. ecc_key key;
  22018. const byte qx[] = {
  22019. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  22020. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  22021. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  22022. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  22023. };
  22024. const byte qy[] = {
  22025. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  22026. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  22027. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  22028. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  22029. };
  22030. const byte d[] = {
  22031. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  22032. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  22033. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  22034. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  22035. };
  22036. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22037. const byte nullBytes[32] = {0};
  22038. int ret;
  22039. #endif
  22040. int curveId = ECC_SECP256R1;
  22041. XMEMSET(&key, 0, sizeof(ecc_key));
  22042. ExpectIntEQ(wc_ecc_init(&key), 0);
  22043. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  22044. curveId), 0);
  22045. /* Test bad args. */
  22046. ExpectIntEQ(wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  22047. curveId), BAD_FUNC_ARG);
  22048. ExpectIntEQ(wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  22049. curveId), BAD_FUNC_ARG);
  22050. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  22051. curveId), BAD_FUNC_ARG);
  22052. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  22053. ECC_CURVE_INVALID), BAD_FUNC_ARG);
  22054. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22055. ExpectIntLT(ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  22056. (byte*)nullBytes, (byte*)nullBytes, curveId), 0);
  22057. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  22058. #endif
  22059. wc_ecc_free(&key);
  22060. #endif
  22061. return EXPECT_RESULT();
  22062. } /* END test_wc_ecc_import_unsigned */
  22063. /*
  22064. * Testing wc_ecc_sig_size()
  22065. */
  22066. static int test_wc_ecc_sig_size(void)
  22067. {
  22068. EXPECT_DECLS;
  22069. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22070. ecc_key key;
  22071. WC_RNG rng;
  22072. int keySz = KEY16;
  22073. int ret;
  22074. XMEMSET(&rng, 0, sizeof(rng));
  22075. XMEMSET(&key, 0, sizeof(key));
  22076. ExpectIntEQ(wc_ecc_init(&key), 0);
  22077. ExpectIntEQ(wc_InitRng(&rng), 0);
  22078. ret = wc_ecc_make_key(&rng, keySz, &key);
  22079. #if defined(WOLFSSL_ASYNC_CRYPT)
  22080. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22081. #endif
  22082. ExpectIntEQ(ret, 0);
  22083. ExpectIntLE(wc_ecc_sig_size(&key),
  22084. (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ));
  22085. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22086. wc_ecc_free(&key);
  22087. #endif
  22088. return EXPECT_RESULT();
  22089. } /* END test_wc_ecc_sig_size */
  22090. /*
  22091. * Testing wc_ecc_ctx_new()
  22092. */
  22093. static int test_wc_ecc_ctx_new(void)
  22094. {
  22095. EXPECT_DECLS;
  22096. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22097. WC_RNG rng;
  22098. ecEncCtx* cli = NULL;
  22099. ecEncCtx* srv = NULL;
  22100. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22101. ExpectIntEQ(wc_InitRng(&rng), 0);
  22102. ExpectNotNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22103. ExpectNotNull(srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  22104. wc_ecc_ctx_free(cli);
  22105. cli = NULL;
  22106. wc_ecc_ctx_free(srv);
  22107. /* Test bad args. */
  22108. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  22109. ExpectNull(cli = wc_ecc_ctx_new(0, &rng));
  22110. ExpectNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL));
  22111. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22112. wc_ecc_ctx_free(cli);
  22113. #endif
  22114. return EXPECT_RESULT();
  22115. } /* END test_wc_ecc_ctx_new */
  22116. /*
  22117. * Tesing wc_ecc_reset()
  22118. */
  22119. static int test_wc_ecc_ctx_reset(void)
  22120. {
  22121. EXPECT_DECLS;
  22122. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22123. ecEncCtx* ctx = NULL;
  22124. WC_RNG rng;
  22125. XMEMSET(&rng, 0, sizeof(rng));
  22126. ExpectIntEQ(wc_InitRng(&rng), 0);
  22127. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22128. ExpectIntEQ(wc_ecc_ctx_reset(ctx, &rng), 0);
  22129. /* Pass in bad args. */
  22130. ExpectIntEQ(wc_ecc_ctx_reset(NULL, &rng), BAD_FUNC_ARG);
  22131. ExpectIntEQ(wc_ecc_ctx_reset(ctx, NULL), BAD_FUNC_ARG);
  22132. wc_ecc_ctx_free(ctx);
  22133. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22134. #endif
  22135. return EXPECT_RESULT();
  22136. } /* END test_wc_ecc_ctx_reset */
  22137. /*
  22138. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  22139. */
  22140. static int test_wc_ecc_ctx_set_peer_salt(void)
  22141. {
  22142. EXPECT_DECLS;
  22143. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22144. WC_RNG rng;
  22145. ecEncCtx* cliCtx = NULL;
  22146. ecEncCtx* servCtx = NULL;
  22147. const byte* cliSalt = NULL;
  22148. const byte* servSalt = NULL;
  22149. XMEMSET(&rng, 0, sizeof(rng));
  22150. ExpectIntEQ(wc_InitRng(&rng), 0);
  22151. ExpectNotNull(cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22152. ExpectNotNull(servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  22153. /* Test bad args. */
  22154. ExpectNull(cliSalt = wc_ecc_ctx_get_own_salt(NULL));
  22155. ExpectNotNull(cliSalt = wc_ecc_ctx_get_own_salt(cliCtx));
  22156. ExpectNotNull(servSalt = wc_ecc_ctx_get_own_salt(servCtx));
  22157. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, servSalt), 0);
  22158. /* Test bad args. */
  22159. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(NULL, servSalt), BAD_FUNC_ARG);
  22160. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, NULL), BAD_FUNC_ARG);
  22161. wc_ecc_ctx_free(cliCtx);
  22162. wc_ecc_ctx_free(servCtx);
  22163. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22164. #endif
  22165. return EXPECT_RESULT();
  22166. } /* END test_wc_ecc_ctx_set_peer_salt */
  22167. /*
  22168. * Testing wc_ecc_ctx_set_info()
  22169. */
  22170. static int test_wc_ecc_ctx_set_info(void)
  22171. {
  22172. EXPECT_DECLS;
  22173. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22174. ecEncCtx* ctx = NULL;
  22175. WC_RNG rng;
  22176. const char* optInfo = "Optional Test Info.";
  22177. int optInfoSz = (int)XSTRLEN(optInfo);
  22178. const char* badOptInfo = NULL;
  22179. XMEMSET(&rng, 0, sizeof(rng));
  22180. ExpectIntEQ(wc_InitRng(&rng), 0);
  22181. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22182. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz), 0);
  22183. /* Test bad args. */
  22184. ExpectIntEQ(wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz),
  22185. BAD_FUNC_ARG);
  22186. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz),
  22187. BAD_FUNC_ARG);
  22188. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1), BAD_FUNC_ARG);
  22189. wc_ecc_ctx_free(ctx);
  22190. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22191. #endif
  22192. return EXPECT_RESULT();
  22193. } /* END test_wc_ecc_ctx_set_info */
  22194. /*
  22195. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  22196. */
  22197. static int test_wc_ecc_encryptDecrypt(void)
  22198. {
  22199. EXPECT_DECLS;
  22200. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  22201. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  22202. ecc_key srvKey;
  22203. ecc_key cliKey;
  22204. ecc_key tmpKey;
  22205. WC_RNG rng;
  22206. int ret;
  22207. const char* msg = "EccBlock Size 16";
  22208. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  22209. #ifdef WOLFSSL_ECIES_OLD
  22210. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22211. #elif defined(WOLFSSL_ECIES_GEN_IV)
  22212. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  22213. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22214. #else
  22215. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) +
  22216. WC_SHA256_DIGEST_SIZE];
  22217. #endif
  22218. word32 outSz = (word32)sizeof(out);
  22219. byte plain[sizeof("EccBlock Size 16")];
  22220. word32 plainSz = (word32)sizeof(plain);
  22221. int keySz = KEY20;
  22222. /* Init stack variables. */
  22223. XMEMSET(out, 0, outSz);
  22224. XMEMSET(plain, 0, plainSz);
  22225. XMEMSET(&rng, 0, sizeof(rng));
  22226. XMEMSET(&srvKey, 0, sizeof(ecc_key));
  22227. XMEMSET(&cliKey, 0, sizeof(ecc_key));
  22228. XMEMSET(&tmpKey, 0, sizeof(ecc_key));
  22229. ExpectIntEQ(wc_InitRng(&rng), 0);
  22230. ExpectIntEQ(wc_ecc_init(&cliKey), 0);
  22231. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  22232. #if defined(WOLFSSL_ASYNC_CRYPT)
  22233. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22234. #endif
  22235. ExpectIntEQ(ret, 0);
  22236. ExpectIntEQ(wc_ecc_init(&srvKey), 0);
  22237. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  22238. #if defined(WOLFSSL_ASYNC_CRYPT)
  22239. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22240. #endif
  22241. ExpectIntEQ(ret, 0);
  22242. ExpectIntEQ(wc_ecc_init(&tmpKey), 0);
  22243. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22244. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22245. !defined(HAVE_SELFTEST)
  22246. ExpectIntEQ(wc_ecc_set_rng(&srvKey, &rng), 0);
  22247. ExpectIntEQ(wc_ecc_set_rng(&cliKey, &rng), 0);
  22248. #endif
  22249. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  22250. &outSz, NULL), 0);
  22251. /* Test bad args. */
  22252. ExpectIntEQ(wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out, &outSz,
  22253. NULL), BAD_FUNC_ARG);
  22254. ExpectIntEQ(wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out, &outSz,
  22255. NULL), BAD_FUNC_ARG);
  22256. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out, &outSz,
  22257. NULL), BAD_FUNC_ARG);
  22258. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  22259. &outSz, NULL), BAD_FUNC_ARG);
  22260. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out, NULL,
  22261. NULL), BAD_FUNC_ARG);
  22262. #ifdef WOLFSSL_ECIES_OLD
  22263. tmpKey.dp = cliKey.dp;
  22264. ExpectIntEQ(wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey), 0);
  22265. #endif
  22266. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, &plainSz,
  22267. NULL), 0);
  22268. ExpectIntEQ(wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain, &plainSz,
  22269. NULL), BAD_FUNC_ARG);
  22270. #ifdef WOLFSSL_ECIES_OLD
  22271. /* NULL parameter allowed in new implementations - public key comes from
  22272. * the message. */
  22273. ExpectIntEQ(wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain, &plainSz,
  22274. NULL), BAD_FUNC_ARG);
  22275. #endif
  22276. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain, &plainSz,
  22277. NULL), BAD_FUNC_ARG);
  22278. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL, &plainSz,
  22279. NULL), BAD_FUNC_ARG);
  22280. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, NULL, NULL),
  22281. BAD_FUNC_ARG);
  22282. ExpectIntEQ(XMEMCMP(msg, plain, msgSz), 0);
  22283. wc_ecc_free(&tmpKey);
  22284. wc_ecc_free(&srvKey);
  22285. wc_ecc_free(&cliKey);
  22286. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22287. #endif
  22288. return EXPECT_RESULT();
  22289. } /* END test_wc_ecc_encryptDecrypt */
  22290. /*
  22291. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  22292. */
  22293. static int test_wc_ecc_del_point(void)
  22294. {
  22295. EXPECT_DECLS;
  22296. #if defined(HAVE_ECC)
  22297. ecc_point* pt = NULL;
  22298. ExpectNotNull(pt = wc_ecc_new_point());
  22299. wc_ecc_del_point(pt);
  22300. #endif
  22301. return EXPECT_RESULT();
  22302. } /* END test_wc_ecc_del_point */
  22303. /*
  22304. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  22305. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  22306. * and wc_ecc_cmp_point()
  22307. */
  22308. static int test_wc_ecc_pointFns(void)
  22309. {
  22310. EXPECT_DECLS;
  22311. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  22312. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22313. !defined(WOLFSSL_ATECC608A)
  22314. ecc_key key;
  22315. WC_RNG rng;
  22316. int ret;
  22317. ecc_point* point = NULL;
  22318. ecc_point* cpypt = NULL;
  22319. int idx = 0;
  22320. int keySz = KEY32;
  22321. byte der[DER_SZ(KEY32)];
  22322. word32 derlenChk = 0;
  22323. word32 derSz = DER_SZ(KEY32);
  22324. /* Init stack variables. */
  22325. XMEMSET(der, 0, derSz);
  22326. XMEMSET(&key, 0, sizeof(ecc_key));
  22327. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22328. ExpectIntEQ(wc_InitRng(&rng), 0);
  22329. ExpectIntEQ(wc_ecc_init(&key), 0);
  22330. ret = wc_ecc_make_key(&rng, keySz, &key);
  22331. #if defined(WOLFSSL_ASYNC_CRYPT)
  22332. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22333. #endif
  22334. ExpectIntEQ(ret, 0);
  22335. ExpectNotNull(point = wc_ecc_new_point());
  22336. ExpectNotNull(cpypt = wc_ecc_new_point());
  22337. /* Export */
  22338. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, NULL,
  22339. &derlenChk), LENGTH_ONLY_E);
  22340. /* Check length value. */
  22341. ExpectIntEQ(derSz, derlenChk);
  22342. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  22343. &derSz), 0);
  22344. /* Test bad args. */
  22345. ExpectIntEQ(wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz),
  22346. ECC_BAD_ARG_E);
  22347. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz),
  22348. ECC_BAD_ARG_E);
  22349. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  22350. NULL), ECC_BAD_ARG_E);
  22351. /* Import */
  22352. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, point), 0);
  22353. ExpectIntEQ(wc_ecc_cmp_point(&key.pubkey, point), 0);
  22354. /* Test bad args. */
  22355. ExpectIntEQ( wc_ecc_import_point_der(NULL, derSz, idx, point),
  22356. ECC_BAD_ARG_E);
  22357. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, NULL), ECC_BAD_ARG_E);
  22358. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, -1, point), ECC_BAD_ARG_E);
  22359. ExpectIntEQ(wc_ecc_import_point_der(der, derSz + 1, idx, point),
  22360. ECC_BAD_ARG_E);
  22361. /* Copy */
  22362. ExpectIntEQ(wc_ecc_copy_point(point, cpypt), 0);
  22363. /* Test bad args. */
  22364. ExpectIntEQ(wc_ecc_copy_point(NULL, cpypt), ECC_BAD_ARG_E);
  22365. ExpectIntEQ(wc_ecc_copy_point(point, NULL), ECC_BAD_ARG_E);
  22366. /* Compare point */
  22367. ExpectIntEQ(wc_ecc_cmp_point(point, cpypt), 0);
  22368. /* Test bad args. */
  22369. ExpectIntEQ(wc_ecc_cmp_point(NULL, cpypt), BAD_FUNC_ARG);
  22370. ExpectIntEQ(wc_ecc_cmp_point(point, NULL), BAD_FUNC_ARG);
  22371. /* At infinity if return == 1, otherwise return == 0. */
  22372. ExpectIntEQ(wc_ecc_point_is_at_infinity(point), 0);
  22373. /* Test bad args. */
  22374. ExpectIntEQ(wc_ecc_point_is_at_infinity(NULL), BAD_FUNC_ARG);
  22375. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  22376. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  22377. #ifdef USE_ECC_B_PARAM
  22378. /* On curve if ret == 0 */
  22379. ExpectIntEQ(wc_ecc_point_is_on_curve(point, idx), 0);
  22380. /* Test bad args. */
  22381. ExpectIntEQ(wc_ecc_point_is_on_curve(NULL, idx), BAD_FUNC_ARG);
  22382. ExpectIntEQ(wc_ecc_point_is_on_curve(point, 1000), ECC_BAD_ARG_E);
  22383. #endif /* USE_ECC_B_PARAM */
  22384. #endif /* !HAVE_SELFTEST && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  22385. /* Free */
  22386. wc_ecc_del_point(point);
  22387. wc_ecc_del_point(cpypt);
  22388. wc_ecc_free(&key);
  22389. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22390. #endif
  22391. return EXPECT_RESULT();
  22392. } /* END test_wc_ecc_pointFns */
  22393. /*
  22394. * Testing wc_ecc_shared_secret_ssh()
  22395. */
  22396. static int test_wc_ecc_shared_secret_ssh(void)
  22397. {
  22398. EXPECT_DECLS;
  22399. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  22400. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22401. !defined(WOLFSSL_ATECC608A) && !defined(PLUTON_CRYPTO_ECC) && \
  22402. !defined(WOLFSSL_CRYPTOCELL)
  22403. ecc_key key;
  22404. ecc_key key2;
  22405. WC_RNG rng;
  22406. int ret;
  22407. int keySz = KEY32;
  22408. #if FIPS_VERSION3_GE(6,0,0)
  22409. int key2Sz = KEY28;
  22410. #else
  22411. int key2Sz = KEY24;
  22412. #endif
  22413. byte secret[KEY32];
  22414. word32 secretLen = keySz;
  22415. /* Init stack variables. */
  22416. XMEMSET(&key, 0, sizeof(ecc_key));
  22417. XMEMSET(&key2, 0, sizeof(ecc_key));
  22418. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22419. XMEMSET(secret, 0, secretLen);
  22420. /* Make keys */
  22421. ExpectIntEQ(wc_ecc_init(&key), 0);
  22422. ExpectIntEQ(wc_InitRng(&rng), 0);
  22423. ret = wc_ecc_make_key(&rng, keySz, &key);
  22424. #if defined(WOLFSSL_ASYNC_CRYPT)
  22425. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22426. #endif
  22427. ExpectIntEQ(ret, 0);
  22428. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22429. ExpectIntEQ(wc_ecc_init(&key2), 0);
  22430. ExpectIntEQ(wc_InitRng(&rng), 0);
  22431. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  22432. #if defined(WOLFSSL_ASYNC_CRYPT)
  22433. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  22434. #endif
  22435. ExpectIntEQ(ret, 0);
  22436. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22437. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22438. !defined(HAVE_SELFTEST)
  22439. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  22440. #endif
  22441. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  22442. &secretLen), 0);
  22443. /* Pass in bad args. */
  22444. ExpectIntEQ(wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret,
  22445. &secretLen), BAD_FUNC_ARG);
  22446. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen),
  22447. BAD_FUNC_ARG);
  22448. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen),
  22449. BAD_FUNC_ARG);
  22450. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL),
  22451. BAD_FUNC_ARG);
  22452. key.type = ECC_PUBLICKEY;
  22453. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  22454. &secretLen), ECC_BAD_ARG_E);
  22455. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22456. wc_ecc_free(&key);
  22457. wc_ecc_free(&key2);
  22458. #ifdef FP_ECC
  22459. wc_ecc_fp_free();
  22460. #endif
  22461. #endif
  22462. return EXPECT_RESULT();
  22463. } /* END test_wc_ecc_shared_secret_ssh */
  22464. /*
  22465. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  22466. */
  22467. static int test_wc_ecc_verify_hash_ex(void)
  22468. {
  22469. EXPECT_DECLS;
  22470. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  22471. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22472. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  22473. ecc_key key;
  22474. WC_RNG rng;
  22475. int ret;
  22476. mp_int r;
  22477. mp_int s;
  22478. mp_int z;
  22479. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  22480. unsigned char iHash[] = "Everyone gets Friday off.......";
  22481. unsigned char shortHash[] = TEST_STRING;
  22482. word32 hashlen = sizeof(hash);
  22483. word32 iHashLen = sizeof(iHash);
  22484. word32 shortHashLen = sizeof(shortHash);
  22485. int keySz = KEY32;
  22486. int verify_ok = 0;
  22487. XMEMSET(&key, 0, sizeof(ecc_key));
  22488. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22489. XMEMSET(&r, 0, sizeof(mp_int));
  22490. XMEMSET(&s, 0, sizeof(mp_int));
  22491. XMEMSET(&z, 0, sizeof(mp_int));
  22492. /* Initialize r, s and z. */
  22493. ExpectIntEQ(mp_init_multi(&r, &s, &z, NULL, NULL, NULL), MP_OKAY);
  22494. ExpectIntEQ(wc_ecc_init(&key), 0);
  22495. ExpectIntEQ(wc_InitRng(&rng), 0);
  22496. ret = wc_ecc_make_key(&rng, keySz, &key);
  22497. #if defined(WOLFSSL_ASYNC_CRYPT)
  22498. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22499. #endif
  22500. ExpectIntEQ(ret, 0);
  22501. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s), 0);
  22502. /* verify_ok should be 1. */
  22503. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key),
  22504. 0);
  22505. ExpectIntEQ(verify_ok, 1);
  22506. /* verify_ok should be 0 */
  22507. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen, &verify_ok,
  22508. &key), 0);
  22509. ExpectIntEQ(verify_ok, 0);
  22510. /* verify_ok should be 0. */
  22511. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22512. &verify_ok, &key), 0);
  22513. ExpectIntEQ(verify_ok, 0);
  22514. /* Test bad args. */
  22515. ExpectIntEQ(wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s),
  22516. ECC_BAD_ARG_E);
  22517. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s),
  22518. ECC_BAD_ARG_E);
  22519. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s),
  22520. ECC_BAD_ARG_E);
  22521. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s),
  22522. ECC_BAD_ARG_E);
  22523. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL),
  22524. ECC_BAD_ARG_E);
  22525. /* Test bad args. */
  22526. ExpectIntEQ(wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen,
  22527. &verify_ok, &key), ECC_BAD_ARG_E);
  22528. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  22529. &verify_ok, &key), ECC_BAD_ARG_E);
  22530. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen,
  22531. &verify_ok, &key), MP_ZERO_E);
  22532. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen,
  22533. &verify_ok, &key), MP_ZERO_E);
  22534. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen,
  22535. &verify_ok, &key), MP_ZERO_E);
  22536. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  22537. &key), ECC_BAD_ARG_E);
  22538. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen, NULL,
  22539. &key), ECC_BAD_ARG_E);
  22540. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22541. &verify_ok, NULL), ECC_BAD_ARG_E);
  22542. wc_ecc_free(&key);
  22543. mp_free(&r);
  22544. mp_free(&s);
  22545. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22546. #endif
  22547. return EXPECT_RESULT();
  22548. } /* END test_wc_ecc_verify_hash_ex */
  22549. /*
  22550. * Testing wc_ecc_mulmod()
  22551. */
  22552. static int test_wc_ecc_mulmod(void)
  22553. {
  22554. EXPECT_DECLS;
  22555. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  22556. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  22557. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  22558. ecc_key key1;
  22559. ecc_key key2;
  22560. ecc_key key3;
  22561. WC_RNG rng;
  22562. int ret;
  22563. XMEMSET(&key1, 0, sizeof(ecc_key));
  22564. XMEMSET(&key2, 0, sizeof(ecc_key));
  22565. XMEMSET(&key3, 0, sizeof(ecc_key));
  22566. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22567. ExpectIntEQ(wc_ecc_init(&key1), 0);
  22568. ExpectIntEQ(wc_ecc_init(&key2), 0);
  22569. ExpectIntEQ(wc_ecc_init(&key3), 0);
  22570. ExpectIntEQ(wc_InitRng(&rng), 0);
  22571. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  22572. #if defined(WOLFSSL_ASYNC_CRYPT)
  22573. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  22574. #endif
  22575. ExpectIntEQ(ret, 0);
  22576. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22577. ExpectIntEQ(wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy,
  22578. key1.dp->Af, ECC_SECP256R1), 0);
  22579. ExpectIntEQ(wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  22580. key1.dp->prime, ECC_SECP256R1), 0);
  22581. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  22582. &key3.pubkey, wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3),
  22583. 1), 0);
  22584. /* Test bad args. */
  22585. ExpectIntEQ(ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey,
  22586. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22587. ECC_BAD_ARG_E);
  22588. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), NULL, &key3.pubkey,
  22589. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22590. ECC_BAD_ARG_E);
  22591. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey, NULL,
  22592. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22593. ECC_BAD_ARG_E);
  22594. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  22595. &key3.pubkey, wc_ecc_key_get_priv(&key2), NULL, 1), ECC_BAD_ARG_E);
  22596. wc_ecc_free(&key1);
  22597. wc_ecc_free(&key2);
  22598. wc_ecc_free(&key3);
  22599. #ifdef FP_ECC
  22600. wc_ecc_fp_free();
  22601. #endif
  22602. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  22603. return EXPECT_RESULT();
  22604. } /* END test_wc_ecc_mulmod */
  22605. /*
  22606. * Testing wc_ecc_is_valid_idx()
  22607. */
  22608. static int test_wc_ecc_is_valid_idx(void)
  22609. {
  22610. EXPECT_DECLS;
  22611. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22612. ecc_key key;
  22613. WC_RNG rng;
  22614. int ret;
  22615. int iVal = -2;
  22616. int iVal2 = 3000;
  22617. XMEMSET(&key, 0, sizeof(ecc_key));
  22618. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22619. ExpectIntEQ(wc_ecc_init(&key), 0);
  22620. ExpectIntEQ(wc_InitRng(&rng), 0);
  22621. ret = wc_ecc_make_key(&rng, 32, &key);
  22622. #if defined(WOLFSSL_ASYNC_CRYPT)
  22623. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22624. #endif
  22625. ExpectIntEQ(ret, 0);
  22626. ExpectIntEQ(wc_ecc_is_valid_idx(key.idx), 1);
  22627. /* Test bad args. */
  22628. ExpectIntEQ(wc_ecc_is_valid_idx(iVal), 0);
  22629. ExpectIntEQ(wc_ecc_is_valid_idx(iVal2), 0);
  22630. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22631. wc_ecc_free(&key);
  22632. #ifdef FP_ECC
  22633. wc_ecc_fp_free();
  22634. #endif
  22635. #endif
  22636. return EXPECT_RESULT();
  22637. } /* END test_wc_ecc_is_valid_idx */
  22638. /*
  22639. * Testing wc_ecc_get_curve_id_from_oid()
  22640. */
  22641. static int test_wc_ecc_get_curve_id_from_oid(void)
  22642. {
  22643. EXPECT_DECLS;
  22644. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22645. !defined(HAVE_FIPS)
  22646. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  22647. word32 len = sizeof(oid);
  22648. /* Bad Cases */
  22649. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(NULL, len), BAD_FUNC_ARG);
  22650. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, 0), ECC_CURVE_INVALID);
  22651. /* Good Case */
  22652. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, len), ECC_SECP256R1);
  22653. #endif
  22654. return EXPECT_RESULT();
  22655. } /* END test_wc_ecc_get_curve_id_from_oid */
  22656. /*
  22657. * Testing wc_ecc_sig_size_calc()
  22658. */
  22659. static int test_wc_ecc_sig_size_calc(void)
  22660. {
  22661. EXPECT_DECLS;
  22662. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  22663. ecc_key key;
  22664. WC_RNG rng;
  22665. int sz = 0;
  22666. int ret;
  22667. XMEMSET(&key, 0, sizeof(ecc_key));
  22668. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22669. ExpectIntEQ(wc_ecc_init(&key), 0);
  22670. ExpectIntEQ(wc_InitRng(&rng), 0);
  22671. ret = wc_ecc_make_key(&rng, 16, &key);
  22672. #if defined(WOLFSSL_ASYNC_CRYPT)
  22673. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22674. #endif
  22675. #if FIPS_VERSION3_GE(6,0,0)
  22676. ExpectIntEQ(ret, BAD_FUNC_ARG);
  22677. #else
  22678. ExpectIntEQ(ret, 0);
  22679. #endif
  22680. #if FIPS_VERSION3_LT(6,0,0)
  22681. sz = key.dp->size;
  22682. ExpectIntGT(wc_ecc_sig_size_calc(sz), 0);
  22683. #else
  22684. (void) sz;
  22685. #endif
  22686. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22687. wc_ecc_free(&key);
  22688. #endif
  22689. return EXPECT_RESULT();
  22690. } /* END test_wc_ecc_sig_size_calc */
  22691. /*
  22692. * Testing wc_ecc_sm2_make_key()
  22693. */
  22694. static int test_wc_ecc_sm2_make_key(void)
  22695. {
  22696. int res = TEST_SKIPPED;
  22697. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  22698. EXPECT_DECLS;
  22699. WC_RNG rng[1];
  22700. ecc_key key[1];
  22701. XMEMSET(rng, 0, sizeof(*rng));
  22702. XMEMSET(key, 0, sizeof(*key));
  22703. ExpectIntEQ(wc_InitRng(rng), 0);
  22704. ExpectIntEQ(wc_ecc_init(key), 0);
  22705. /* Test invalid parameters. */
  22706. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, NULL, WC_ECC_FLAG_NONE),
  22707. BAD_FUNC_ARG);
  22708. ExpectIntEQ(wc_ecc_sm2_make_key(rng, NULL, WC_ECC_FLAG_NONE),
  22709. BAD_FUNC_ARG);
  22710. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, key, WC_ECC_FLAG_NONE),
  22711. BAD_FUNC_ARG);
  22712. /* Test valid parameters. */
  22713. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  22714. ExpectIntEQ(key->dp->id, ECC_SM2P256V1);
  22715. wc_ecc_free(key);
  22716. wc_FreeRng(rng);
  22717. #ifdef FP_ECC
  22718. wc_ecc_fp_free();
  22719. #endif
  22720. res = EXPECT_RESULT();
  22721. #endif
  22722. return res;
  22723. }
  22724. /*
  22725. * Testing wc_ecc_sm2_shared_secret()
  22726. */
  22727. static int test_wc_ecc_sm2_shared_secret(void)
  22728. {
  22729. int res = TEST_SKIPPED;
  22730. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  22731. EXPECT_DECLS;
  22732. WC_RNG rng[1];
  22733. ecc_key keyA[1];
  22734. ecc_key keyB[1];
  22735. byte outA[32];
  22736. byte outB[32];
  22737. word32 outALen = 32;
  22738. word32 outBLen = 32;
  22739. XMEMSET(rng, 0, sizeof(*rng));
  22740. XMEMSET(keyA, 0, sizeof(*keyA));
  22741. XMEMSET(keyB, 0, sizeof(*keyB));
  22742. ExpectIntEQ(wc_InitRng(rng), 0);
  22743. ExpectIntEQ(wc_ecc_init(keyA), 0);
  22744. ExpectIntEQ(wc_ecc_init(keyB), 0);
  22745. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyA, WC_ECC_FLAG_NONE), 0);
  22746. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyB, WC_ECC_FLAG_NONE), 0);
  22747. #ifdef ECC_TIMING_RESISTANT
  22748. ExpectIntEQ(wc_ecc_set_rng(keyA, rng), 0);
  22749. ExpectIntEQ(wc_ecc_set_rng(keyB, rng), 0);
  22750. #endif
  22751. /* Test invalid parameters. */
  22752. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  22753. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, NULL, NULL), BAD_FUNC_ARG);
  22754. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, NULL, NULL), BAD_FUNC_ARG);
  22755. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, outA, NULL), BAD_FUNC_ARG);
  22756. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, &outALen),
  22757. BAD_FUNC_ARG);
  22758. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, outA, &outALen),
  22759. BAD_FUNC_ARG);
  22760. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, outA, &outALen),
  22761. BAD_FUNC_ARG);
  22762. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, NULL, &outALen),
  22763. BAD_FUNC_ARG);
  22764. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, NULL), BAD_FUNC_ARG);
  22765. /* Test valid parameters. */
  22766. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, &outALen), 0);
  22767. ExpectIntLE(outALen, 32);
  22768. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyB, keyA, outB, &outBLen), 0);
  22769. ExpectIntLE(outBLen, 32);
  22770. ExpectIntEQ(outALen, outBLen);
  22771. ExpectBufEQ(outA, outB, outALen);
  22772. wc_ecc_free(keyB);
  22773. wc_ecc_free(keyA);
  22774. wc_FreeRng(rng);
  22775. #ifdef FP_ECC
  22776. wc_ecc_fp_free();
  22777. #endif
  22778. res = EXPECT_RESULT();
  22779. #endif
  22780. return res;
  22781. }
  22782. /*
  22783. * Testing wc_ecc_sm2_create_digest()
  22784. */
  22785. static int test_wc_ecc_sm2_create_digest(void)
  22786. {
  22787. int res = TEST_SKIPPED;
  22788. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && !defined(NO_HASH_WRAPPER) && \
  22789. (defined(WOLFSSL_SM3) || !defined(NO_SHA256))
  22790. EXPECT_DECLS;
  22791. ecc_key key[1];
  22792. enum wc_HashType hashType;
  22793. unsigned char pub[] = {
  22794. 0x04,
  22795. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22796. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22797. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22798. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22799. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22800. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22801. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22802. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22803. };
  22804. unsigned char id[] = {
  22805. 0x01, 0x02, 0x03,
  22806. };
  22807. unsigned char msg[] = {
  22808. 0x01, 0x02, 0x03,
  22809. };
  22810. unsigned char hash[32];
  22811. #ifdef WOLFSSL_SM3
  22812. unsigned char expHash[32] = {
  22813. 0xc1, 0xdd, 0x92, 0xc5, 0x60, 0xd3, 0x94, 0x28,
  22814. 0xeb, 0x0f, 0x57, 0x79, 0x3f, 0xc9, 0x96, 0xc5,
  22815. 0xfa, 0xf5, 0x90, 0xb2, 0x64, 0x2f, 0xaf, 0x9c,
  22816. 0xc8, 0x57, 0x21, 0x6a, 0x52, 0x7e, 0xf1, 0x95
  22817. };
  22818. #else
  22819. unsigned char expHash[32] = {
  22820. 0xea, 0x41, 0x55, 0x21, 0x61, 0x00, 0x5c, 0x9a,
  22821. 0x57, 0x35, 0x6b, 0x49, 0xca, 0x8f, 0x65, 0xc2,
  22822. 0x0e, 0x29, 0x0c, 0xa0, 0x1d, 0xa7, 0xc4, 0xed,
  22823. 0xdd, 0x51, 0x12, 0xf6, 0xe7, 0x55, 0xc5, 0xf4
  22824. };
  22825. #endif
  22826. #ifdef WOLFSSL_SM3
  22827. hashType = WC_HASH_TYPE_SM3;
  22828. #else
  22829. hashType = WC_HASH_TYPE_SHA256;
  22830. #endif
  22831. XMEMSET(key, 0, sizeof(*key));
  22832. ExpectIntEQ(wc_ecc_init(key), 0);
  22833. /* Test with no curve set. */
  22834. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22835. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22836. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22837. /* Test invalid parameters. */
  22838. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22839. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22840. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  22841. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22842. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  22843. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22844. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22845. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  22846. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22847. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  22848. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  22849. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22850. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  22851. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22852. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22853. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  22854. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22855. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  22856. /* Bad hash type. */
  22857. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22858. -1, hash, 0, key), BAD_FUNC_ARG);
  22859. /* Bad hash size. */
  22860. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22861. hashType, hash, 0, key), BUFFER_E);
  22862. /* Test valid parameters. */
  22863. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22864. hashType, hash, sizeof(hash), key), 0);
  22865. ExpectBufEQ(hash, expHash, sizeof(expHash));
  22866. wc_ecc_free(key);
  22867. res = EXPECT_RESULT();
  22868. #endif
  22869. return res;
  22870. }
  22871. /*
  22872. * Testing wc_ecc_sm2_verify_hash_ex()
  22873. */
  22874. static int test_wc_ecc_sm2_verify_hash_ex(void)
  22875. {
  22876. int res = TEST_SKIPPED;
  22877. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY) && \
  22878. defined(WOLFSSL_PUBLIC_MP)
  22879. EXPECT_DECLS;
  22880. ecc_key key[1];
  22881. mp_int r[1];
  22882. mp_int s[1];
  22883. int verified;
  22884. unsigned char pub[] = {
  22885. 0x04,
  22886. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22887. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22888. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22889. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22890. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22891. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22892. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22893. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22894. };
  22895. unsigned char hash[] = {
  22896. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  22897. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  22898. 0x50, 0x69, 0x5B, 0x20
  22899. };
  22900. unsigned char rData[] = {
  22901. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  22902. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  22903. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  22904. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE7,
  22905. };
  22906. unsigned char sData[] = {
  22907. 0x1D,
  22908. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  22909. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  22910. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  22911. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  22912. };
  22913. unsigned char rBadData[] = {
  22914. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  22915. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  22916. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  22917. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE8,
  22918. };
  22919. XMEMSET(key, 0, sizeof(*key));
  22920. XMEMSET(r, 0, sizeof(*r));
  22921. XMEMSET(s, 0, sizeof(*s));
  22922. ExpectIntEQ(mp_init(r), 0);
  22923. ExpectIntEQ(mp_init(s), 0);
  22924. ExpectIntEQ(mp_read_unsigned_bin(r, rData, sizeof(rData)), 0);
  22925. ExpectIntEQ(mp_read_unsigned_bin(s, sData, sizeof(sData)), 0);
  22926. ExpectIntEQ(wc_ecc_init(key), 0);
  22927. /* Test with no curve set. */
  22928. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22929. &verified, key), BAD_FUNC_ARG);
  22930. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22931. /* Test invalid parameters. */
  22932. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22933. NULL, NULL), BAD_FUNC_ARG);
  22934. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, NULL, sizeof(hash),
  22935. NULL, NULL), BAD_FUNC_ARG);
  22936. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, NULL, sizeof(hash),
  22937. NULL, NULL), BAD_FUNC_ARG);
  22938. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, hash, sizeof(hash),
  22939. NULL, NULL), BAD_FUNC_ARG);
  22940. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22941. &verified, NULL), BAD_FUNC_ARG);
  22942. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22943. NULL, key), BAD_FUNC_ARG);
  22944. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, hash, sizeof(hash),
  22945. &verified, key), BAD_FUNC_ARG);
  22946. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, hash, sizeof(hash),
  22947. &verified, key), BAD_FUNC_ARG);
  22948. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, NULL, sizeof(hash),
  22949. &verified, key), BAD_FUNC_ARG);
  22950. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22951. NULL, key), BAD_FUNC_ARG);
  22952. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22953. &verified, NULL), BAD_FUNC_ARG);
  22954. /* Make key not on the SM2 curve. */
  22955. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22956. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22957. &verified, key), BAD_FUNC_ARG);
  22958. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22959. /* Test valid parameters. */
  22960. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22961. &verified, key), 0);
  22962. ExpectIntEQ(verified, 1);
  22963. ExpectIntEQ(mp_read_unsigned_bin(r, rBadData, sizeof(rBadData)), 0);
  22964. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22965. &verified, key), 0);
  22966. ExpectIntEQ(verified, 0);
  22967. mp_free(s);
  22968. mp_free(r);
  22969. wc_ecc_free(key);
  22970. #ifdef FP_ECC
  22971. wc_ecc_fp_free();
  22972. #endif
  22973. res = EXPECT_RESULT();
  22974. #endif
  22975. return res;
  22976. }
  22977. /*
  22978. * Testing wc_ecc_sm2_verify_hash()
  22979. */
  22980. static int test_wc_ecc_sm2_verify_hash(void)
  22981. {
  22982. int res = TEST_SKIPPED;
  22983. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY)
  22984. EXPECT_DECLS;
  22985. ecc_key key[1];
  22986. int verified;
  22987. unsigned char pub[] = {
  22988. 0x04,
  22989. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22990. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22991. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22992. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22993. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22994. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22995. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22996. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22997. };
  22998. unsigned char hash[] = {
  22999. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  23000. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  23001. 0x50, 0x69, 0x5B, 0x20
  23002. };
  23003. unsigned char sig[] = {
  23004. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  23005. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  23006. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  23007. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  23008. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  23009. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  23010. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  23011. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  23012. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  23013. };
  23014. unsigned char sigBad[] = {
  23015. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  23016. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  23017. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  23018. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  23019. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  23020. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  23021. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  23022. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  23023. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDD
  23024. };
  23025. XMEMSET(key, 0, sizeof(*key));
  23026. ExpectIntEQ(wc_ecc_init(key), 0);
  23027. /* Test with no curve set. */
  23028. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23029. &verified, key), BAD_FUNC_ARG);
  23030. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  23031. /* Test invalid parameters. */
  23032. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  23033. NULL, NULL), BAD_FUNC_ARG);
  23034. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  23035. NULL, NULL), BAD_FUNC_ARG);
  23036. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  23037. NULL, NULL), BAD_FUNC_ARG);
  23038. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  23039. &verified, NULL), BAD_FUNC_ARG);
  23040. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  23041. NULL, key), BAD_FUNC_ARG);
  23042. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  23043. &verified, key), BAD_FUNC_ARG);
  23044. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  23045. &verified, key), BAD_FUNC_ARG);
  23046. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23047. NULL, key), BAD_FUNC_ARG);
  23048. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23049. &verified, NULL), BAD_FUNC_ARG);
  23050. /* Make key not on the SM2 curve. */
  23051. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23052. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23053. &verified, key), BAD_FUNC_ARG);
  23054. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23055. /* Test valid parameters. */
  23056. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23057. &verified, key), 0);
  23058. ExpectIntEQ(verified, 1);
  23059. ExpectIntEQ(wc_ecc_sm2_verify_hash(sigBad, sizeof(sigBad), hash,
  23060. sizeof(hash), &verified, key), 0);
  23061. ExpectIntEQ(verified, 0);
  23062. wc_ecc_free(key);
  23063. #ifdef FP_ECC
  23064. wc_ecc_fp_free();
  23065. #endif
  23066. res = EXPECT_RESULT();
  23067. #endif
  23068. return res;
  23069. }
  23070. /*
  23071. * Testing wc_ecc_sm2_verify_hash_ex()
  23072. */
  23073. static int test_wc_ecc_sm2_sign_hash_ex(void)
  23074. {
  23075. int res = TEST_SKIPPED;
  23076. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN) && \
  23077. defined(WOLFSSL_PUBLIC_MP)
  23078. EXPECT_DECLS;
  23079. WC_RNG rng[1];
  23080. ecc_key key[1];
  23081. mp_int r[1];
  23082. mp_int s[1];
  23083. unsigned char hash[32];
  23084. #ifdef HAVE_ECC_VERIFY
  23085. int verified;
  23086. #endif
  23087. XMEMSET(rng, 0, sizeof(*rng));
  23088. XMEMSET(key, 0, sizeof(*key));
  23089. XMEMSET(r, 0, sizeof(*r));
  23090. XMEMSET(s, 0, sizeof(*s));
  23091. ExpectIntEQ(wc_InitRng(rng), 0);
  23092. ExpectIntEQ(mp_init(r), 0);
  23093. ExpectIntEQ(mp_init(s), 0);
  23094. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  23095. ExpectIntEQ(wc_ecc_init(key), 0);
  23096. /* Test with no curve set. */
  23097. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23098. BAD_FUNC_ARG);
  23099. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  23100. /* Test invalid parameters. */
  23101. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  23102. NULL), BAD_FUNC_ARG);
  23103. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, NULL, NULL,
  23104. NULL), BAD_FUNC_ARG);
  23105. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, NULL, NULL,
  23106. NULL), BAD_FUNC_ARG);
  23107. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, key, NULL,
  23108. NULL), BAD_FUNC_ARG);
  23109. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, r,
  23110. NULL), BAD_FUNC_ARG);
  23111. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  23112. s), BAD_FUNC_ARG);
  23113. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, key, r, s),
  23114. BAD_FUNC_ARG);
  23115. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, key, r, s),
  23116. BAD_FUNC_ARG);
  23117. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, NULL, r, s),
  23118. BAD_FUNC_ARG);
  23119. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, NULL, s),
  23120. BAD_FUNC_ARG);
  23121. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, NULL),
  23122. BAD_FUNC_ARG);
  23123. /* Make key not on the SM2 curve. */
  23124. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23125. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23126. BAD_FUNC_ARG);
  23127. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23128. #ifdef WOLFSSL_SP_MATH_ALL
  23129. {
  23130. mp_int smallR[1];
  23131. sp_init_size(smallR, 1);
  23132. /* Force failure in _ecc_sm2_calc_r_s by r being too small. */
  23133. ExpectIntLT(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key,
  23134. smallR, s), 0);
  23135. }
  23136. #endif
  23137. /* Test valid parameters. */
  23138. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23139. 0);
  23140. #ifdef HAVE_ECC_VERIFY
  23141. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash), &verified,
  23142. key), 0);
  23143. ExpectIntEQ(verified, 1);
  23144. #endif
  23145. mp_free(s);
  23146. mp_free(r);
  23147. wc_ecc_free(key);
  23148. wc_FreeRng(rng);
  23149. #ifdef FP_ECC
  23150. wc_ecc_fp_free();
  23151. #endif
  23152. res = EXPECT_RESULT();
  23153. #endif
  23154. return res;
  23155. }
  23156. /*
  23157. * Testing wc_ecc_sm2_verify_hash()
  23158. */
  23159. static int test_wc_ecc_sm2_sign_hash(void)
  23160. {
  23161. int res = TEST_SKIPPED;
  23162. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN)
  23163. EXPECT_DECLS;
  23164. WC_RNG rng[1];
  23165. ecc_key key[1];
  23166. unsigned char hash[32];
  23167. unsigned char sig[72];
  23168. word32 sigSz = sizeof(sig);
  23169. #ifdef HAVE_ECC_VERIFY
  23170. int verified;
  23171. #endif
  23172. XMEMSET(rng, 0, sizeof(*rng));
  23173. XMEMSET(key, 0, sizeof(*key));
  23174. ExpectIntEQ(wc_InitRng(rng), 0);
  23175. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  23176. ExpectIntEQ(wc_ecc_init(key), 0);
  23177. /* Test with no curve set. */
  23178. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23179. BAD_FUNC_ARG);
  23180. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  23181. /* Test invalid parameters. */
  23182. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  23183. NULL), BAD_FUNC_ARG);
  23184. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, NULL, NULL,
  23185. NULL), BAD_FUNC_ARG);
  23186. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, NULL, NULL,
  23187. NULL), BAD_FUNC_ARG);
  23188. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, &sigSz, NULL,
  23189. NULL), BAD_FUNC_ARG);
  23190. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, rng,
  23191. NULL), BAD_FUNC_ARG);
  23192. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  23193. key), BAD_FUNC_ARG);
  23194. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, &sigSz, rng,
  23195. key), BAD_FUNC_ARG);
  23196. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, &sigSz, rng,
  23197. key), BAD_FUNC_ARG);
  23198. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, NULL, rng,
  23199. key), BAD_FUNC_ARG);
  23200. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, NULL,
  23201. key), BAD_FUNC_ARG);
  23202. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng,
  23203. NULL), BAD_FUNC_ARG);
  23204. /* Make key not on the SM2 curve. */
  23205. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23206. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23207. BAD_FUNC_ARG);
  23208. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23209. /* Test valid parameters. */
  23210. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23211. 0);
  23212. #ifdef HAVE_ECC_VERIFY
  23213. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sigSz, hash, sizeof(hash),
  23214. &verified, key), 0);
  23215. ExpectIntEQ(verified, 1);
  23216. #endif
  23217. wc_ecc_free(key);
  23218. wc_FreeRng(rng);
  23219. #ifdef FP_ECC
  23220. wc_ecc_fp_free();
  23221. #endif
  23222. res = EXPECT_RESULT();
  23223. #endif
  23224. return res;
  23225. }
  23226. /*
  23227. * Testing ToTraditional
  23228. */
  23229. static int test_ToTraditional(void)
  23230. {
  23231. EXPECT_DECLS;
  23232. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  23233. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  23234. defined(OPENSSL_EXTRA_X509_SMALL)) && !defined(NO_FILESYSTEM)
  23235. XFILE f = XBADFILE;
  23236. byte input[TWOK_BUF];
  23237. word32 sz = 0;
  23238. ExpectTrue((f = XFOPEN("./certs/server-keyPkcs8.der", "rb")) != XBADFILE);
  23239. ExpectTrue((sz = (word32)XFREAD(input, 1, sizeof(input), f)) > 0);
  23240. if (f != XBADFILE)
  23241. XFCLOSE(f);
  23242. /* Good case */
  23243. ExpectIntGT(ToTraditional(input, sz), 0);
  23244. /* Bad cases */
  23245. ExpectIntEQ(ToTraditional(NULL, 0), BAD_FUNC_ARG);
  23246. ExpectIntEQ(ToTraditional(NULL, sz), BAD_FUNC_ARG);
  23247. #ifdef WOLFSSL_ASN_TEMPLATE
  23248. ExpectIntEQ(ToTraditional(input, 0), BUFFER_E);
  23249. #else
  23250. ExpectIntEQ(ToTraditional(input, 0), ASN_PARSE_E);
  23251. #endif
  23252. #endif
  23253. return EXPECT_RESULT();
  23254. } /* End test_ToTraditional*/
  23255. /*
  23256. * Testing wc_EccPrivateKeyToDer
  23257. */
  23258. static int test_wc_EccPrivateKeyToDer(void)
  23259. {
  23260. EXPECT_DECLS;
  23261. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  23262. byte output[ONEK_BUF];
  23263. ecc_key eccKey;
  23264. WC_RNG rng;
  23265. word32 inLen;
  23266. word32 outLen = 0;
  23267. int ret;
  23268. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23269. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23270. ExpectIntEQ(wc_InitRng(&rng), 0);
  23271. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23272. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23273. #if defined(WOLFSSL_ASYNC_CRYPT)
  23274. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23275. #endif
  23276. ExpectIntEQ(ret, 0);
  23277. inLen = (word32)sizeof(output);
  23278. /* Bad Cases */
  23279. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23280. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23281. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, NULL, inLen), LENGTH_ONLY_E);
  23282. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, output, 0), BAD_FUNC_ARG);
  23283. /* Good Case */
  23284. ExpectIntGT(outLen = wc_EccPrivateKeyToDer(&eccKey, output, inLen), 0);
  23285. wc_ecc_free(&eccKey);
  23286. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23287. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  23288. {
  23289. /* test importing private only into a PKEY struct */
  23290. EC_KEY* ec = NULL;
  23291. EVP_PKEY* pkey = NULL;
  23292. const unsigned char* der;
  23293. der = output;
  23294. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, outLen));
  23295. der = output;
  23296. ExpectNotNull(ec = d2i_ECPrivateKey(NULL, &der, outLen));
  23297. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  23298. if (EXPECT_FAIL()) {
  23299. EC_KEY_free(ec);
  23300. }
  23301. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  23302. }
  23303. #endif
  23304. #endif
  23305. return EXPECT_RESULT();
  23306. } /* End test_wc_EccPrivateKeyToDer*/
  23307. /*
  23308. * Testing wc_DhPublicKeyDecode
  23309. */
  23310. static int test_wc_DhPublicKeyDecode(void)
  23311. {
  23312. EXPECT_DECLS;
  23313. #ifndef NO_DH
  23314. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  23315. DhKey key;
  23316. word32 inOutIdx;
  23317. XMEMSET(&key, 0, sizeof(DhKey));
  23318. ExpectIntEQ(wc_InitDhKey(&key), 0);
  23319. ExpectIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0), BAD_FUNC_ARG);
  23320. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23321. BAD_FUNC_ARG);
  23322. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23323. BAD_FUNC_ARG);
  23324. inOutIdx = 0;
  23325. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  23326. BAD_FUNC_ARG);
  23327. inOutIdx = 0;
  23328. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  23329. BAD_FUNC_ARG);
  23330. inOutIdx = 0;
  23331. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  23332. sizeof_dh_pub_key_der_2048), 0);
  23333. ExpectIntNE(key.p.used, 0);
  23334. ExpectIntNE(key.g.used, 0);
  23335. ExpectIntEQ(key.q.used, 0);
  23336. ExpectIntNE(key.pub.used, 0);
  23337. ExpectIntEQ(key.priv.used, 0);
  23338. DoExpectIntEQ(wc_FreeDhKey(&key), 0);
  23339. #endif
  23340. #endif /* !NO_DH */
  23341. return EXPECT_RESULT();
  23342. }
  23343. /*
  23344. * Testing wc_Ed25519KeyToDer
  23345. */
  23346. static int test_wc_Ed25519KeyToDer(void)
  23347. {
  23348. EXPECT_DECLS;
  23349. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23350. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23351. byte output[ONEK_BUF];
  23352. ed25519_key ed25519Key;
  23353. WC_RNG rng;
  23354. word32 inLen;
  23355. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23356. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23357. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23358. ExpectIntEQ(wc_InitRng(&rng), 0);
  23359. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23360. inLen = (word32)sizeof(output);
  23361. /* Bad Cases */
  23362. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23363. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23364. ExpectIntEQ(wc_Ed25519KeyToDer(&ed25519Key, output, 0), BAD_FUNC_ARG);
  23365. /* Good Cases */
  23366. /* length only */
  23367. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen), 0);
  23368. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, output, inLen), 0);
  23369. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23370. wc_ed25519_free(&ed25519Key);
  23371. #endif
  23372. return EXPECT_RESULT();
  23373. } /* End test_wc_Ed25519KeyToDer*/
  23374. /*
  23375. * Testing wc_Ed25519PrivateKeyToDer
  23376. */
  23377. static int test_wc_Ed25519PrivateKeyToDer(void)
  23378. {
  23379. EXPECT_DECLS;
  23380. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23381. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23382. byte output[ONEK_BUF];
  23383. ed25519_key ed25519PrivKey;
  23384. WC_RNG rng;
  23385. word32 inLen;
  23386. XMEMSET(&ed25519PrivKey, 0, sizeof(ed25519_key));
  23387. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23388. ExpectIntEQ(wc_ed25519_init(&ed25519PrivKey), 0);
  23389. ExpectIntEQ(wc_InitRng(&rng), 0);
  23390. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey),
  23391. 0);
  23392. inLen = (word32)sizeof(output);
  23393. /* Bad Cases */
  23394. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23395. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23396. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0),
  23397. BAD_FUNC_ARG);
  23398. /* Good Cases */
  23399. /* length only */
  23400. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen), 0);
  23401. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen), 0);
  23402. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23403. wc_ed25519_free(&ed25519PrivKey);
  23404. #endif
  23405. return EXPECT_RESULT();
  23406. } /* End test_wc_Ed25519PrivateKeyToDer*/
  23407. /*
  23408. * Testing wc_Ed448KeyToDer
  23409. */
  23410. static int test_wc_Ed448KeyToDer(void)
  23411. {
  23412. EXPECT_DECLS;
  23413. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23414. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23415. byte output[ONEK_BUF];
  23416. ed448_key ed448Key;
  23417. WC_RNG rng;
  23418. word32 inLen;
  23419. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23420. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23421. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23422. ExpectIntEQ(wc_InitRng(&rng), 0);
  23423. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23424. inLen = (word32)sizeof(output);
  23425. /* Bad Cases */
  23426. ExpectIntEQ(wc_Ed448KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23427. ExpectIntEQ(wc_Ed448KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23428. ExpectIntEQ(wc_Ed448KeyToDer(&ed448Key, output, 0), BAD_FUNC_ARG);
  23429. /* Good Cases */
  23430. /* length only */
  23431. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, NULL, inLen), 0);
  23432. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, output, inLen), 0);
  23433. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23434. wc_ed448_free(&ed448Key);
  23435. #endif
  23436. return EXPECT_RESULT();
  23437. } /* End test_wc_Ed448KeyToDer*/
  23438. /*
  23439. * Testing wc_Ed448PrivateKeyToDer
  23440. */
  23441. static int test_wc_Ed448PrivateKeyToDer(void)
  23442. {
  23443. EXPECT_DECLS;
  23444. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23445. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23446. byte output[ONEK_BUF];
  23447. ed448_key ed448PrivKey;
  23448. WC_RNG rng;
  23449. word32 inLen;
  23450. XMEMSET(&ed448PrivKey, 0, sizeof(ed448_key));
  23451. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23452. ExpectIntEQ(wc_ed448_init(&ed448PrivKey), 0);
  23453. ExpectIntEQ(wc_InitRng(&rng), 0);
  23454. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey),
  23455. 0);
  23456. inLen = (word32)sizeof(output);
  23457. /* Bad Cases */
  23458. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23459. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23460. ExpectIntEQ(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0),
  23461. BAD_FUNC_ARG);
  23462. /* Good cases */
  23463. /* length only */
  23464. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen), 0);
  23465. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen), 0);
  23466. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23467. wc_ed448_free(&ed448PrivKey);
  23468. #endif
  23469. return EXPECT_RESULT();
  23470. } /* End test_wc_Ed448PrivateKeyToDer*/
  23471. /*
  23472. * Testing wc_SetSubjectBuffer
  23473. */
  23474. static int test_wc_SetSubjectBuffer(void)
  23475. {
  23476. EXPECT_DECLS;
  23477. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  23478. Cert cert;
  23479. XFILE file = XBADFILE;
  23480. byte* der = NULL;
  23481. word32 derSz;
  23482. derSz = FOURK_BUF;
  23483. ExpectNotNull(der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  23484. DYNAMIC_TYPE_TMP_BUFFER));
  23485. ExpectTrue((file = XFOPEN("./certs/ca-cert.der", "rb")) != XBADFILE);
  23486. ExpectTrue((derSz = (word32)XFREAD(der, 1, FOURK_BUF, file)) > 0);
  23487. if (file != XBADFILE)
  23488. XFCLOSE(file);
  23489. ExpectIntEQ(wc_InitCert(&cert), 0);
  23490. ExpectIntEQ(wc_SetSubjectBuffer(&cert, der, derSz), 0);
  23491. ExpectIntEQ(wc_SetSubjectBuffer(NULL, der, derSz), BAD_FUNC_ARG);
  23492. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23493. #endif
  23494. return EXPECT_RESULT();
  23495. } /* End test_wc_SetSubjectBuffer*/
  23496. /*
  23497. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  23498. */
  23499. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  23500. {
  23501. EXPECT_DECLS;
  23502. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23503. WC_RNG rng;
  23504. Cert cert;
  23505. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23506. RsaKey rsaKey;
  23507. int bits = 2048;
  23508. #endif
  23509. #if defined(HAVE_ECC)
  23510. ecc_key eccKey;
  23511. int ret;
  23512. #endif
  23513. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23514. ed25519_key ed25519Key;
  23515. #endif
  23516. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23517. ed448_key ed448Key;
  23518. #endif
  23519. #ifndef HAVE_FIPS
  23520. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  23521. #else
  23522. ExpectIntEQ(wc_InitRng(&rng), 0);
  23523. #endif
  23524. ExpectIntEQ(wc_InitCert(&cert), 0);
  23525. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23526. /* RSA */
  23527. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  23528. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  23529. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  23530. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey),
  23531. 0);
  23532. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  23533. #endif
  23534. #if defined(HAVE_ECC)
  23535. /* ECC */
  23536. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23537. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23538. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23539. #if defined(WOLFSSL_ASYNC_CRYPT)
  23540. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23541. #endif
  23542. ExpectIntEQ(ret, 0);
  23543. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey),
  23544. 0);
  23545. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  23546. #endif
  23547. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23548. /* ED25519 */
  23549. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23550. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23551. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23552. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23553. &ed25519Key), 0);
  23554. wc_ed25519_free(&ed25519Key);
  23555. #endif
  23556. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23557. /* ED448 */
  23558. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23559. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23560. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23561. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  23562. &ed448Key), 0);
  23563. wc_ed448_free(&ed448Key);
  23564. #endif
  23565. wc_FreeRng(&rng);
  23566. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23567. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  23568. return EXPECT_RESULT();
  23569. } /* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  23570. /*
  23571. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  23572. */
  23573. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  23574. {
  23575. EXPECT_DECLS;
  23576. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23577. WC_RNG rng;
  23578. Cert cert;
  23579. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23580. RsaKey rsaKey;
  23581. int bits = 2048;
  23582. #endif
  23583. #if defined(HAVE_ECC)
  23584. ecc_key eccKey;
  23585. int ret;
  23586. #endif
  23587. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23588. ed25519_key ed25519Key;
  23589. #endif
  23590. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23591. ed448_key ed448Key;
  23592. #endif
  23593. #ifndef HAVE_FIPS
  23594. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  23595. #else
  23596. ExpectIntEQ(wc_InitRng(&rng), 0);
  23597. #endif
  23598. ExpectIntEQ(wc_InitCert(&cert), 0);
  23599. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23600. /* RSA */
  23601. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  23602. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  23603. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  23604. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey), 0);
  23605. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  23606. #endif
  23607. #if defined(HAVE_ECC)
  23608. /* ECC */
  23609. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23610. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23611. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23612. #if defined(WOLFSSL_ASYNC_CRYPT)
  23613. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23614. #endif
  23615. ExpectIntEQ(ret, 0);
  23616. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey), 0);
  23617. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  23618. #endif
  23619. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23620. /* ED25519 */
  23621. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23622. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23623. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23624. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23625. &ed25519Key), 0);
  23626. wc_ed25519_free(&ed25519Key);
  23627. #endif
  23628. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23629. /* ED448 */
  23630. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23631. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23632. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23633. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, &ed448Key),
  23634. 0);
  23635. wc_ed448_free(&ed448Key);
  23636. #endif
  23637. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23638. #endif /* defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  23639. return EXPECT_RESULT();
  23640. } /* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  23641. /*
  23642. * Testing wc_PKCS7_New()
  23643. */
  23644. static int test_wc_PKCS7_New(void)
  23645. {
  23646. EXPECT_DECLS;
  23647. #if defined(HAVE_PKCS7)
  23648. PKCS7* pkcs7 = NULL;
  23649. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, testDevId));
  23650. wc_PKCS7_Free(pkcs7);
  23651. #endif
  23652. return EXPECT_RESULT();
  23653. } /* END test-wc_PKCS7_New */
  23654. /*
  23655. * Testing wc_PKCS7_Init()
  23656. */
  23657. static int test_wc_PKCS7_Init(void)
  23658. {
  23659. EXPECT_DECLS;
  23660. #if defined(HAVE_PKCS7)
  23661. PKCS7* pkcs7 = NULL;
  23662. void* heap = NULL;
  23663. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  23664. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  23665. /* Pass in bad args. */
  23666. ExpectIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
  23667. wc_PKCS7_Free(pkcs7);
  23668. #endif
  23669. return EXPECT_RESULT();
  23670. } /* END test-wc_PKCS7_Init */
  23671. /*
  23672. * Testing wc_PKCS7_InitWithCert()
  23673. */
  23674. static int test_wc_PKCS7_InitWithCert(void)
  23675. {
  23676. EXPECT_DECLS;
  23677. #if defined(HAVE_PKCS7)
  23678. PKCS7* pkcs7 = NULL;
  23679. #ifndef NO_RSA
  23680. #if defined(USE_CERT_BUFFERS_2048)
  23681. unsigned char cert[sizeof(client_cert_der_2048)];
  23682. int certSz = (int)sizeof(cert);
  23683. XMEMSET(cert, 0, certSz);
  23684. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  23685. #elif defined(USE_CERT_BUFFERS_1024)
  23686. unsigned char cert[sizeof(client_cert_der_1024)];
  23687. int certSz = (int)sizeof(cert);
  23688. XMEMSET(cert, 0, certSz);
  23689. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  23690. #else
  23691. unsigned char cert[ONEK_BUF];
  23692. XFILE fp = XBADFILE;
  23693. int certSz;
  23694. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23695. XBADFILE);
  23696. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23697. fp), 0);
  23698. if (fp != XBADFILE)
  23699. XFCLOSE(fp);
  23700. #endif
  23701. #elif defined(HAVE_ECC)
  23702. #if defined(USE_CERT_BUFFERS_256)
  23703. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23704. int certSz = (int)sizeof(cert);
  23705. XMEMSET(cert, 0, certSz);
  23706. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  23707. #else
  23708. unsigned char cert[ONEK_BUF];
  23709. XFILE fp = XBADFILE;
  23710. int certSz;
  23711. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23712. XBADFILE);
  23713. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256),
  23714. fp), 0);
  23715. if (fp != XBADFILE)
  23716. XFCLOSE(fp);
  23717. #endif
  23718. #else
  23719. #error PKCS7 requires ECC or RSA
  23720. #endif
  23721. #ifdef HAVE_ECC
  23722. {
  23723. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  23724. static unsigned char certWithInvalidEccKey[] = {
  23725. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  23726. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  23727. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  23728. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  23729. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23730. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  23731. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23732. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23733. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23734. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23735. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23736. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23737. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23738. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23739. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23740. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  23741. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  23742. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  23743. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  23744. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  23745. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  23746. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  23747. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  23748. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  23749. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  23750. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  23751. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  23752. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  23753. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  23754. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  23755. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  23756. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  23757. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  23758. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  23759. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  23760. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  23761. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  23762. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  23763. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  23764. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  23765. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23766. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  23767. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  23768. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23769. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  23770. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23771. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  23772. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23773. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23774. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23775. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23776. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23777. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23778. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23779. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23780. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23781. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  23782. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  23783. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  23784. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  23785. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  23786. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  23787. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  23788. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  23789. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  23790. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  23791. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  23792. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  23793. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  23794. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  23795. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  23796. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  23797. 0x08, 0xA8, 0xB4
  23798. };
  23799. #endif
  23800. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23801. /* If initialization is not successful, it's free'd in init func. */
  23802. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz),
  23803. 0);
  23804. wc_PKCS7_Free(pkcs7);
  23805. pkcs7 = NULL;
  23806. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23807. /* Valid initialization usage. */
  23808. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23809. /* Pass in bad args. No need free for null checks, free at end.*/
  23810. ExpectIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  23811. BAD_FUNC_ARG);
  23812. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  23813. BAD_FUNC_ARG);
  23814. #ifdef HAVE_ECC
  23815. ExpectIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  23816. sizeof(certWithInvalidEccKey)), 0);
  23817. }
  23818. #endif
  23819. wc_PKCS7_Free(pkcs7);
  23820. #endif
  23821. return EXPECT_RESULT();
  23822. } /* END test_wc_PKCS7_InitWithCert */
  23823. /*
  23824. * Testing wc_PKCS7_EncodeData()
  23825. */
  23826. static int test_wc_PKCS7_EncodeData(void)
  23827. {
  23828. EXPECT_DECLS;
  23829. #if defined(HAVE_PKCS7)
  23830. PKCS7* pkcs7 = NULL;
  23831. byte output[FOURK_BUF];
  23832. byte data[] = "My encoded DER cert.";
  23833. #ifndef NO_RSA
  23834. #if defined(USE_CERT_BUFFERS_2048)
  23835. unsigned char cert[sizeof(client_cert_der_2048)];
  23836. unsigned char key[sizeof(client_key_der_2048)];
  23837. int certSz = (int)sizeof(cert);
  23838. int keySz = (int)sizeof(key);
  23839. XMEMSET(cert, 0, certSz);
  23840. XMEMSET(key, 0, keySz);
  23841. XMEMCPY(cert, client_cert_der_2048, certSz);
  23842. XMEMCPY(key, client_key_der_2048, keySz);
  23843. #elif defined(USE_CERT_BUFFERS_1024)
  23844. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  23845. unsigned char key[sizeof_client_key_der_1024];
  23846. int certSz = (int)sizeof(cert);
  23847. int keySz = (int)sizeof(key);
  23848. XMEMSET(cert, 0, certSz);
  23849. XMEMSET(key, 0, keySz);
  23850. XMEMCPY(cert, client_cert_der_1024, certSz);
  23851. XMEMCPY(key, client_key_der_1024, keySz);
  23852. #else
  23853. unsigned char cert[ONEK_BUF];
  23854. unsigned char key[ONEK_BUF];
  23855. XFILE fp = XBADFILE;
  23856. int certSz;
  23857. int keySz;
  23858. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23859. XBADFILE);
  23860. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23861. fp), 0);
  23862. if (fp != XBADFILE) {
  23863. XFCLOSE(fp);
  23864. fp = XBADFILE;
  23865. }
  23866. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  23867. XBADFILE);
  23868. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  23869. 0);
  23870. if (fp != XBADFILE)
  23871. XFCLOSE(fp);
  23872. #endif
  23873. #elif defined(HAVE_ECC)
  23874. #if defined(USE_CERT_BUFFERS_256)
  23875. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23876. unsigned char key[sizeof(ecc_clikey_der_256)];
  23877. int certSz = (int)sizeof(cert);
  23878. int keySz = (int)sizeof(key);
  23879. XMEMSET(cert, 0, certSz);
  23880. XMEMSET(key, 0, keySz);
  23881. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23882. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  23883. #else
  23884. unsigned char cert[ONEK_BUF];
  23885. unsigned char key[ONEK_BUF];
  23886. XFILE fp = XBADFILE;
  23887. int certSz, keySz;
  23888. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23889. XBADFILE);
  23890. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  23891. fp), 0);
  23892. if (fp != XBADFILE) {
  23893. XFCLOSE(fp);
  23894. fp = XBADFILE;
  23895. }
  23896. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  23897. XBADFILE);
  23898. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  23899. 0);
  23900. if (fp != XBADFILE)
  23901. XFCLOSE(fp);
  23902. #endif
  23903. #endif
  23904. XMEMSET(output, 0, sizeof(output));
  23905. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23906. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23907. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, certSz), 0);
  23908. if (pkcs7 != NULL) {
  23909. pkcs7->content = data;
  23910. pkcs7->contentSz = sizeof(data);
  23911. pkcs7->privateKey = key;
  23912. pkcs7->privateKeySz = keySz;
  23913. }
  23914. ExpectIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  23915. /* Test bad args. */
  23916. ExpectIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  23917. BAD_FUNC_ARG);
  23918. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  23919. BAD_FUNC_ARG);
  23920. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  23921. wc_PKCS7_Free(pkcs7);
  23922. #endif
  23923. return EXPECT_RESULT();
  23924. } /* END test_wc_PKCS7_EncodeData */
  23925. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  23926. !defined(NO_RSA) && !defined(NO_SHA256)
  23927. /* RSA sign raw digest callback */
  23928. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  23929. byte* out, word32 outSz, byte* privateKey,
  23930. word32 privateKeySz, int devid, int hashOID)
  23931. {
  23932. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  23933. byte digInfoEncoding[] = {
  23934. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  23935. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  23936. 0x00, 0x04, 0x20
  23937. };
  23938. int ret;
  23939. byte digestInfo[ONEK_BUF];
  23940. byte sig[FOURK_BUF];
  23941. word32 digestInfoSz = 0;
  23942. word32 idx = 0;
  23943. RsaKey rsa;
  23944. /* SHA-256 required only for this example callback due to above
  23945. * digInfoEncoding[] */
  23946. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  23947. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  23948. (hashOID != SHA256h)) {
  23949. return -1;
  23950. }
  23951. /* build DigestInfo */
  23952. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  23953. digestInfoSz += sizeof(digInfoEncoding);
  23954. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  23955. digestInfoSz += digestSz;
  23956. /* set up RSA key */
  23957. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  23958. if (ret != 0) {
  23959. return ret;
  23960. }
  23961. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  23962. /* sign DigestInfo */
  23963. if (ret == 0) {
  23964. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  23965. &rsa, pkcs7->rng);
  23966. if (ret > 0) {
  23967. if (ret > (int)outSz) {
  23968. /* output buffer too small */
  23969. ret = -1;
  23970. }
  23971. else {
  23972. /* success, ret holds sig size */
  23973. XMEMCPY(out, sig, ret);
  23974. }
  23975. }
  23976. }
  23977. wc_FreeRsaKey(&rsa);
  23978. return ret;
  23979. }
  23980. #endif
  23981. #if defined(HAVE_PKCS7) && defined(ASN_BER_TO_DER)
  23982. typedef struct encodeSignedDataStream {
  23983. byte out[FOURK_BUF*3];
  23984. int idx;
  23985. word32 outIdx;
  23986. } encodeSignedDataStream;
  23987. /* content is 8k of partially created bundle */
  23988. static int GetContentCB(PKCS7* pkcs7, byte** content, void* ctx)
  23989. {
  23990. int ret = 0;
  23991. encodeSignedDataStream* strm = (encodeSignedDataStream*)ctx;
  23992. if (strm->outIdx < pkcs7->contentSz) {
  23993. ret = (pkcs7->contentSz > strm->outIdx + FOURK_BUF)?
  23994. FOURK_BUF : pkcs7->contentSz - strm->outIdx;
  23995. *content = strm->out + strm->outIdx;
  23996. strm->outIdx += ret;
  23997. }
  23998. (void)pkcs7;
  23999. return ret;
  24000. }
  24001. static int StreamOutputCB(PKCS7* pkcs7, const byte* output, word32 outputSz,
  24002. void* ctx)
  24003. {
  24004. encodeSignedDataStream* strm = (encodeSignedDataStream*)ctx;
  24005. XMEMCPY(strm->out + strm->idx, output, outputSz);
  24006. strm->idx += outputSz;
  24007. (void)pkcs7;
  24008. return 0;
  24009. }
  24010. #endif
  24011. /*
  24012. * Testing wc_PKCS7_EncodeSignedData()
  24013. */
  24014. static int test_wc_PKCS7_EncodeSignedData(void)
  24015. {
  24016. EXPECT_DECLS;
  24017. #if defined(HAVE_PKCS7)
  24018. PKCS7* pkcs7 = NULL;
  24019. WC_RNG rng;
  24020. byte output[FOURK_BUF];
  24021. byte badOut[1];
  24022. word32 outputSz = (word32)sizeof(output);
  24023. word32 badOutSz = 0;
  24024. byte data[] = "Test data to encode.";
  24025. #ifndef NO_RSA
  24026. #if defined(USE_CERT_BUFFERS_2048)
  24027. byte key[sizeof(client_key_der_2048)];
  24028. byte cert[sizeof(client_cert_der_2048)];
  24029. word32 keySz = (word32)sizeof(key);
  24030. word32 certSz = (word32)sizeof(cert);
  24031. XMEMSET(key, 0, keySz);
  24032. XMEMSET(cert, 0, certSz);
  24033. XMEMCPY(key, client_key_der_2048, keySz);
  24034. XMEMCPY(cert, client_cert_der_2048, certSz);
  24035. #elif defined(USE_CERT_BUFFERS_1024)
  24036. byte key[sizeof_client_key_der_1024];
  24037. byte cert[sizeof(sizeof_client_cert_der_1024)];
  24038. word32 keySz = (word32)sizeof(key);
  24039. word32 certSz = (word32)sizeof(cert);
  24040. XMEMSET(key, 0, keySz);
  24041. XMEMSET(cert, 0, certSz);
  24042. XMEMCPY(key, client_key_der_1024, keySz);
  24043. XMEMCPY(cert, client_cert_der_1024, certSz);
  24044. #else
  24045. unsigned char cert[ONEK_BUF];
  24046. unsigned char key[ONEK_BUF];
  24047. XFILE fp = XBADFILE;
  24048. int certSz;
  24049. int keySz;
  24050. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  24051. XBADFILE);
  24052. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  24053. fp), 0);
  24054. if (fp != XBADFILE) {
  24055. XFCLOSE(fp);
  24056. fp = XBADFILE;
  24057. }
  24058. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  24059. XBADFILE);
  24060. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  24061. 0);
  24062. if (fp != XBADFILE)
  24063. XFCLOSE(fp);
  24064. #endif
  24065. #elif defined(HAVE_ECC)
  24066. #if defined(USE_CERT_BUFFERS_256)
  24067. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24068. unsigned char key[sizeof(ecc_clikey_der_256)];
  24069. int certSz = (int)sizeof(cert);
  24070. int keySz = (int)sizeof(key);
  24071. XMEMSET(cert, 0, certSz);
  24072. XMEMSET(key, 0, keySz);
  24073. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  24074. XMEMCPY(key, ecc_clikey_der_256, keySz);
  24075. #else
  24076. unsigned char cert[ONEK_BUF];
  24077. unsigned char key[ONEK_BUF];
  24078. XFILE fp = XBADFILE;
  24079. int certSz;
  24080. int keySz;
  24081. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  24082. XBADFILE);
  24083. ExpectIntGT(certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp), 0);
  24084. if (fp != XBADFILE) {
  24085. XFCLOSE(fp);
  24086. fp = XBADFILE;
  24087. }
  24088. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  24089. XBADFILE);
  24090. ExpectIntGT(keySz = (int)XFREAD(key, 1, ONEK_BUF, fp), 0);
  24091. if (fp != XBADFILE)
  24092. XFCLOSE(fp);
  24093. #endif
  24094. #endif
  24095. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24096. XMEMSET(output, 0, outputSz);
  24097. ExpectIntEQ(wc_InitRng(&rng), 0);
  24098. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24099. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24100. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24101. if (pkcs7 != NULL) {
  24102. pkcs7->content = data;
  24103. pkcs7->contentSz = (word32)sizeof(data);
  24104. pkcs7->privateKey = key;
  24105. pkcs7->privateKeySz = (word32)sizeof(key);
  24106. pkcs7->encryptOID = RSAk;
  24107. #ifdef NO_SHA
  24108. pkcs7->hashOID = SHA256h;
  24109. #else
  24110. pkcs7->hashOID = SHAh;
  24111. #endif
  24112. pkcs7->rng = &rng;
  24113. }
  24114. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  24115. wc_PKCS7_Free(pkcs7);
  24116. pkcs7 = NULL;
  24117. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24118. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24119. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24120. #ifdef ASN_BER_TO_DER
  24121. wc_PKCS7_Free(pkcs7);
  24122. /* reinitialize and test setting stream mode */
  24123. {
  24124. int signedSz = 0;
  24125. encodeSignedDataStream strm;
  24126. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24127. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  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. #ifdef NO_SHA
  24136. pkcs7->hashOID = SHA256h;
  24137. #else
  24138. pkcs7->hashOID = SHAh;
  24139. #endif
  24140. pkcs7->rng = &rng;
  24141. }
  24142. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
  24143. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL), 0);
  24144. ExpectIntEQ(wc_PKCS7_SetStreamMode(NULL, 1, NULL, NULL, NULL),
  24145. BAD_FUNC_ARG);
  24146. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 1);
  24147. ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, output,
  24148. outputSz), 0);
  24149. wc_PKCS7_Free(pkcs7);
  24150. pkcs7 = NULL;
  24151. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24152. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24153. /* use exact signed buffer size since BER encoded */
  24154. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, signedSz), 0);
  24155. wc_PKCS7_Free(pkcs7);
  24156. /* now try with using callbacks for IO */
  24157. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24158. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24159. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24160. if (pkcs7 != NULL) {
  24161. pkcs7->contentSz = FOURK_BUF*2;
  24162. pkcs7->privateKey = key;
  24163. pkcs7->privateKeySz = (word32)sizeof(key);
  24164. pkcs7->encryptOID = RSAk;
  24165. #ifdef NO_SHA
  24166. pkcs7->hashOID = SHA256h;
  24167. #else
  24168. pkcs7->hashOID = SHAh;
  24169. #endif
  24170. pkcs7->rng = &rng;
  24171. }
  24172. XMEMSET(&strm, 0, sizeof(strm));
  24173. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
  24174. StreamOutputCB, (void*)&strm), 0);
  24175. ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, NULL, 0), 0);
  24176. wc_PKCS7_Free(pkcs7);
  24177. pkcs7 = NULL;
  24178. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24179. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24180. /* use exact signed buffer size since BER encoded */
  24181. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, strm.out, signedSz), 0);
  24182. }
  24183. #endif
  24184. #ifndef NO_PKCS7_STREAM
  24185. wc_PKCS7_Free(pkcs7);
  24186. pkcs7 = NULL;
  24187. {
  24188. word32 z;
  24189. int ret;
  24190. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24191. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24192. /* test for streaming mode */
  24193. ret = -1;
  24194. for (z = 0; z < outputSz && ret != 0; z++) {
  24195. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24196. if (ret < 0){
  24197. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24198. }
  24199. }
  24200. ExpectIntEQ(ret, 0);
  24201. ExpectIntNE(pkcs7->contentSz, 0);
  24202. ExpectNotNull(pkcs7->contentDynamic);
  24203. }
  24204. #endif /* !NO_PKCS7_STREAM */
  24205. /* Pass in bad args. */
  24206. ExpectIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz),
  24207. BAD_FUNC_ARG);
  24208. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  24209. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  24210. badOutSz), BAD_FUNC_ARG);
  24211. if (pkcs7 != NULL) {
  24212. pkcs7->hashOID = 0; /* bad hashOID */
  24213. }
  24214. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz),
  24215. BAD_FUNC_ARG);
  24216. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  24217. !defined(NO_RSA) && !defined(NO_SHA256)
  24218. /* test RSA sign raw digest callback, if using RSA and compiled in.
  24219. * Example callback assumes SHA-256, so only run test if compiled in. */
  24220. wc_PKCS7_Free(pkcs7);
  24221. pkcs7 = NULL;
  24222. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24223. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24224. if (pkcs7 != NULL) {
  24225. pkcs7->content = data;
  24226. pkcs7->contentSz = (word32)sizeof(data);
  24227. pkcs7->privateKey = key;
  24228. pkcs7->privateKeySz = (word32)sizeof(key);
  24229. pkcs7->encryptOID = RSAk;
  24230. pkcs7->hashOID = SHA256h;
  24231. pkcs7->rng = &rng;
  24232. }
  24233. ExpectIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  24234. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  24235. #endif
  24236. wc_PKCS7_Free(pkcs7);
  24237. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  24238. #endif
  24239. return EXPECT_RESULT();
  24240. } /* END test_wc_PKCS7_EncodeSignedData */
  24241. /*
  24242. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  24243. */
  24244. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  24245. {
  24246. EXPECT_DECLS;
  24247. #if defined(HAVE_PKCS7)
  24248. int i;
  24249. PKCS7* pkcs7 = NULL;
  24250. WC_RNG rng;
  24251. byte outputHead[FOURK_BUF/2];
  24252. byte outputFoot[FOURK_BUF/2];
  24253. word32 outputHeadSz = (word32)sizeof(outputHead);
  24254. word32 outputFootSz = (word32)sizeof(outputFoot);
  24255. byte data[FOURK_BUF];
  24256. wc_HashAlg hash;
  24257. #ifdef NO_SHA
  24258. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  24259. #else
  24260. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24261. #endif
  24262. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24263. word32 hashSz = wc_HashGetDigestSize(hashType);
  24264. #ifndef NO_RSA
  24265. #if defined(USE_CERT_BUFFERS_2048)
  24266. byte key[sizeof(client_key_der_2048)];
  24267. byte cert[sizeof(client_cert_der_2048)];
  24268. word32 keySz = (word32)sizeof(key);
  24269. word32 certSz = (word32)sizeof(cert);
  24270. XMEMSET(key, 0, keySz);
  24271. XMEMSET(cert, 0, certSz);
  24272. XMEMCPY(key, client_key_der_2048, keySz);
  24273. XMEMCPY(cert, client_cert_der_2048, certSz);
  24274. #elif defined(USE_CERT_BUFFERS_1024)
  24275. byte key[sizeof_client_key_der_1024];
  24276. byte cert[sizeof(sizeof_client_cert_der_1024)];
  24277. word32 keySz = (word32)sizeof(key);
  24278. word32 certSz = (word32)sizeof(cert);
  24279. XMEMSET(key, 0, keySz);
  24280. XMEMSET(cert, 0, certSz);
  24281. XMEMCPY(key, client_key_der_1024, keySz);
  24282. XMEMCPY(cert, client_cert_der_1024, certSz);
  24283. #else
  24284. unsigned char cert[ONEK_BUF];
  24285. unsigned char key[ONEK_BUF];
  24286. XFILE fp = XBADFILE;
  24287. int certSz;
  24288. int keySz;
  24289. ExpectTure((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  24290. XBADFILE);
  24291. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  24292. fp), 0);
  24293. if (fp != XBADFILE) {
  24294. XFCLOSE(fp);
  24295. fp = XBADFILE;
  24296. }
  24297. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  24298. XBADFILE);
  24299. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  24300. 0);
  24301. if (fp != XBADFILE)
  24302. XFCLOSE(fp);
  24303. #endif
  24304. #elif defined(HAVE_ECC)
  24305. #if defined(USE_CERT_BUFFERS_256)
  24306. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24307. unsigned char key[sizeof(ecc_clikey_der_256)];
  24308. int certSz = (int)sizeof(cert);
  24309. int keySz = (int)sizeof(key);
  24310. XMEMSET(cert, 0, certSz);
  24311. XMEMSET(key, 0, keySz);
  24312. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  24313. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  24314. #else
  24315. unsigned char cert[ONEK_BUF];
  24316. unsigned char key[ONEK_BUF];
  24317. XFILE fp = XBADFILE;
  24318. int certSz;
  24319. int keySz;
  24320. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  24321. XBADFILE);
  24322. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  24323. fp), 0);
  24324. if (fp != XBADFILE) {
  24325. XFCLOSE(fp);
  24326. fp = XBADFILE;
  24327. }
  24328. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  24329. XBADFILE);
  24330. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  24331. 0);
  24332. if (fp != XBADFILE)
  24333. XFCLOSE(fp);
  24334. #endif
  24335. #endif
  24336. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24337. /* initialize large data with sequence */
  24338. for (i=0; i<(int)sizeof(data); i++)
  24339. data[i] = i & 0xff;
  24340. XMEMSET(outputHead, 0, outputHeadSz);
  24341. XMEMSET(outputFoot, 0, outputFootSz);
  24342. ExpectIntEQ(wc_InitRng(&rng), 0);
  24343. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24344. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24345. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24346. if (pkcs7 != NULL) {
  24347. pkcs7->content = NULL; /* not used for ex */
  24348. pkcs7->contentSz = (word32)sizeof(data);
  24349. pkcs7->privateKey = key;
  24350. pkcs7->privateKeySz = (word32)sizeof(key);
  24351. pkcs7->encryptOID = RSAk;
  24352. #ifdef NO_SHA
  24353. pkcs7->hashOID = SHA256h;
  24354. #else
  24355. pkcs7->hashOID = SHAh;
  24356. #endif
  24357. pkcs7->rng = &rng;
  24358. }
  24359. /* calculate hash for content */
  24360. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  24361. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  24362. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  24363. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  24364. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  24365. /* Perform PKCS7 sign using hash directly */
  24366. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24367. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  24368. ExpectIntGT(outputHeadSz, 0);
  24369. ExpectIntGT(outputFootSz, 0);
  24370. wc_PKCS7_Free(pkcs7);
  24371. pkcs7 = NULL;
  24372. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24373. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24374. /* required parameter even on verify when using _ex, if using outputHead
  24375. * and outputFoot */
  24376. if (pkcs7 != NULL) {
  24377. pkcs7->contentSz = (word32)sizeof(data);
  24378. }
  24379. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24380. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  24381. wc_PKCS7_Free(pkcs7);
  24382. pkcs7 = NULL;
  24383. /* assembly complete PKCS7 sign and use normal verify */
  24384. {
  24385. byte* output = NULL;
  24386. word32 outputSz = 0;
  24387. #ifndef NO_PKCS7_STREAM
  24388. word32 z;
  24389. int ret;
  24390. #endif /* !NO_PKCS7_STREAM */
  24391. ExpectNotNull(output = (byte*)XMALLOC(
  24392. outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT,
  24393. DYNAMIC_TYPE_TMP_BUFFER));
  24394. if (output != NULL) {
  24395. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  24396. outputSz += outputHeadSz;
  24397. XMEMCPY(&output[outputSz], data, sizeof(data));
  24398. outputSz += sizeof(data);
  24399. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  24400. outputSz += outputFootSz;
  24401. }
  24402. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24403. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24404. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24405. #ifndef NO_PKCS7_STREAM
  24406. wc_PKCS7_Free(pkcs7);
  24407. pkcs7 = NULL;
  24408. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24409. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24410. /* test for streaming mode */
  24411. ret = -1;
  24412. for (z = 0; z < outputSz && ret != 0; z++) {
  24413. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24414. if (ret < 0){
  24415. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24416. }
  24417. }
  24418. ExpectIntEQ(ret, 0);
  24419. ExpectIntNE(pkcs7->contentSz, 0);
  24420. ExpectNotNull(pkcs7->contentDynamic);
  24421. wc_PKCS7_Free(pkcs7);
  24422. pkcs7 = NULL;
  24423. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24424. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24425. #endif /* !NO_PKCS7_STREAM */
  24426. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24427. }
  24428. /* Pass in bad args. */
  24429. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24430. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24431. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24432. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24433. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24434. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24435. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24436. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24437. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24438. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24439. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24440. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  24441. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24442. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  24443. if (pkcs7 != NULL) {
  24444. pkcs7->hashOID = 0; /* bad hashOID */
  24445. }
  24446. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24447. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24448. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24449. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24450. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24451. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24452. #ifndef NO_PKCS7_STREAM
  24453. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24454. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24455. #else
  24456. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24457. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  24458. #endif
  24459. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24460. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24461. #ifndef NO_PKCS7_STREAM
  24462. /* can pass in 0 buffer length with streaming API */
  24463. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24464. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24465. #else
  24466. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24467. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24468. #endif
  24469. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24470. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  24471. #ifndef NO_PKCS7_STREAM
  24472. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24473. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  24474. #else
  24475. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24476. outputHead, outputHeadSz, outputFoot, 0), BUFFER_E);
  24477. #endif
  24478. wc_PKCS7_Free(pkcs7);
  24479. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  24480. #endif
  24481. return EXPECT_RESULT();
  24482. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  24483. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  24484. /**
  24485. * Loads certs/keys from files or buffers into the argument buffers,
  24486. * helper function called by CreatePKCS7SignedData().
  24487. *
  24488. * Returns 0 on success, negative on error.
  24489. */
  24490. static int LoadPKCS7SignedDataCerts(
  24491. int useIntermediateCertChain, int pkAlgoType,
  24492. byte* intCARoot, word32* intCARootSz,
  24493. byte* intCA1, word32* intCA1Sz,
  24494. byte* intCA2, word32* intCA2Sz,
  24495. byte* cert, word32* certSz,
  24496. byte* key, word32* keySz)
  24497. {
  24498. EXPECT_DECLS;
  24499. int ret = 0;
  24500. XFILE fp = XBADFILE;
  24501. #ifndef NO_RSA
  24502. const char* intCARootRSA = "./certs/ca-cert.der";
  24503. const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
  24504. const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
  24505. const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
  24506. const char* intServKeyRSA = "./certs/server-key.der";
  24507. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
  24508. const char* cli1024Cert = "./certs/1024/client-cert.der";
  24509. const char* cli1024Key = "./certs/1024/client-key.der";
  24510. #endif
  24511. #endif
  24512. #ifdef HAVE_ECC
  24513. const char* intCARootECC = "./certs/ca-ecc-cert.der";
  24514. const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
  24515. const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
  24516. const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
  24517. const char* intServKeyECC = "./certs/ecc-key.der";
  24518. #ifndef USE_CERT_BUFFERS_256
  24519. const char* cliEccCert = "./certs/client-ecc-cert.der";
  24520. const char* cliEccKey = "./certs/client-ecc-key.der";
  24521. #endif
  24522. #endif
  24523. if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
  24524. ((useIntermediateCertChain == 1) &&
  24525. (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
  24526. intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
  24527. return BAD_FUNC_ARG;
  24528. }
  24529. /* Read/load certs and keys to use for signing based on PK type and chain */
  24530. switch (pkAlgoType) {
  24531. #ifndef NO_RSA
  24532. case RSA_TYPE:
  24533. if (useIntermediateCertChain == 1) {
  24534. ExpectTrue((fp = XFOPEN(intCARootRSA, "rb")) != XBADFILE);
  24535. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  24536. if (fp != XBADFILE) {
  24537. XFCLOSE(fp);
  24538. fp = XBADFILE;
  24539. }
  24540. ExpectIntGT(*intCARootSz, 0);
  24541. ExpectTrue((fp = XFOPEN(intCA1RSA, "rb")) != XBADFILE);
  24542. if (fp != XBADFILE) {
  24543. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24544. XFCLOSE(fp);
  24545. fp = XBADFILE;
  24546. }
  24547. ExpectIntGT(*intCA1Sz, 0);
  24548. ExpectTrue((fp = XFOPEN(intCA2RSA, "rb")) != XBADFILE);
  24549. if (fp != XBADFILE) {
  24550. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24551. XFCLOSE(fp);
  24552. fp = XBADFILE;
  24553. }
  24554. ExpectIntGT(*intCA2Sz, 0);
  24555. ExpectTrue((fp = XFOPEN(intServCertRSA, "rb")) != XBADFILE);
  24556. if (fp != XBADFILE) {
  24557. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24558. XFCLOSE(fp);
  24559. fp = XBADFILE;
  24560. }
  24561. ExpectIntGT(*certSz, 0);
  24562. ExpectTrue((fp = XFOPEN(intServKeyRSA, "rb")) != XBADFILE);
  24563. if (fp != XBADFILE) {
  24564. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24565. XFCLOSE(fp);
  24566. fp = XBADFILE;
  24567. }
  24568. ExpectIntGT(*keySz, 0);
  24569. }
  24570. else {
  24571. #if defined(USE_CERT_BUFFERS_2048)
  24572. *keySz = sizeof_client_key_der_2048;
  24573. *certSz = sizeof_client_cert_der_2048;
  24574. XMEMCPY(key, client_key_der_2048, *keySz);
  24575. XMEMCPY(cert, client_cert_der_2048, *certSz);
  24576. #elif defined(USE_CERT_BUFFERS_1024)
  24577. *keySz = sizeof_client_key_der_1024;
  24578. *certSz = sizeof_client_cert_der_1024;
  24579. XMEMCPY(key, client_key_der_1024, *keySz);
  24580. XMEMCPY(cert, client_cert_der_1024, *certSz);
  24581. #else
  24582. ExpectTrue((fp = XFOPEN(cli1024Key, "rb")) != XBADFILE);
  24583. if (fp != XBADFILE) {
  24584. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24585. XFCLOSE(fp);
  24586. fp = XBADFILE;
  24587. }
  24588. ExpectIntGT(*keySz, 0);
  24589. ExpectTrue((fp = XFOPEN(cli1024Cert, "rb")) != XBADFILE);
  24590. if (fp != XBADFILE) {
  24591. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24592. XFCLOSE(fp);
  24593. fp = XBADFILE;
  24594. }
  24595. ExpectIntGT(*certSz, 0);
  24596. #endif /* USE_CERT_BUFFERS_2048 */
  24597. }
  24598. break;
  24599. #endif /* !NO_RSA */
  24600. #ifdef HAVE_ECC
  24601. case ECC_TYPE:
  24602. if (useIntermediateCertChain == 1) {
  24603. ExpectTrue((fp = XFOPEN(intCARootECC, "rb")) != XBADFILE);
  24604. if (fp != XBADFILE) {
  24605. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz,
  24606. fp);
  24607. XFCLOSE(fp);
  24608. fp = XBADFILE;
  24609. }
  24610. ExpectIntGT(*intCARootSz, 0);
  24611. ExpectTrue((fp = XFOPEN(intCA1ECC, "rb")) != XBADFILE);
  24612. if (fp != XBADFILE) {
  24613. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24614. XFCLOSE(fp);
  24615. fp = XBADFILE;
  24616. }
  24617. ExpectIntGT(*intCA1Sz, 0);
  24618. ExpectTrue((fp = XFOPEN(intCA2ECC, "rb")) != XBADFILE);
  24619. if (fp != XBADFILE) {
  24620. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24621. XFCLOSE(fp);
  24622. fp = XBADFILE;
  24623. }
  24624. ExpectIntGT(*intCA2Sz, 0);
  24625. ExpectTrue((fp = XFOPEN(intServCertECC, "rb")) != XBADFILE);
  24626. if (fp != XBADFILE) {
  24627. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24628. XFCLOSE(fp);
  24629. fp = XBADFILE;
  24630. }
  24631. ExpectIntGT(*certSz, 0);
  24632. ExpectTrue((fp = XFOPEN(intServKeyECC, "rb")) != XBADFILE);
  24633. if (fp != XBADFILE) {
  24634. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24635. XFCLOSE(fp);
  24636. fp = XBADFILE;
  24637. }
  24638. ExpectIntGT(*keySz, 0);
  24639. }
  24640. else {
  24641. #if defined(USE_CERT_BUFFERS_256)
  24642. *keySz = sizeof_ecc_clikey_der_256;
  24643. *certSz = sizeof_cliecc_cert_der_256;
  24644. XMEMCPY(key, ecc_clikey_der_256, *keySz);
  24645. XMEMCPY(cert, cliecc_cert_der_256, *certSz);
  24646. #else
  24647. ExpectTrue((fp = XFOPEN(cliEccKey, "rb")) != XBADFILE);
  24648. if (fp != XBADFILE) {
  24649. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24650. XFCLOSE(fp);
  24651. fp = XBADFILE;
  24652. }
  24653. ExpectIntGT(*keySz, 0);
  24654. ExpectTrue((fp = XFOPEN(cliEccCert, "rb")) != XBADFILE);
  24655. if (fp != XBADFILE) {
  24656. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24657. XFCLOSE(fp);
  24658. fp = XBADFILE;
  24659. }
  24660. ExpectIntGT(*certSz, 0);
  24661. #endif /* USE_CERT_BUFFERS_256 */
  24662. }
  24663. break;
  24664. #endif /* HAVE_ECC */
  24665. default:
  24666. WOLFSSL_MSG("Unsupported SignedData PK type");
  24667. ret = BAD_FUNC_ARG;
  24668. break;
  24669. }
  24670. if (EXPECT_FAIL() && (ret == 0)) {
  24671. ret = BAD_FUNC_ARG;
  24672. }
  24673. return ret;
  24674. }
  24675. /**
  24676. * Creates a PKCS7/CMS SignedData bundle to use for testing.
  24677. *
  24678. * output output buffer to place SignedData
  24679. * outputSz size of output buffer
  24680. * data data buffer to be signed
  24681. * dataSz size of data buffer
  24682. * withAttribs [1/0] include attributes in SignedData message
  24683. * detachedSig [1/0] create detached signature, no content
  24684. * useIntCertChain [1/0] use certificate chain and include intermediate and
  24685. * root CAs in bundle
  24686. * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
  24687. *
  24688. * Return size of bundle created on success, negative on error */
  24689. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  24690. byte* data, word32 dataSz,
  24691. int withAttribs, int detachedSig,
  24692. int useIntermediateCertChain,
  24693. int pkAlgoType)
  24694. {
  24695. EXPECT_DECLS;
  24696. int ret = 0;
  24697. WC_RNG rng;
  24698. PKCS7* pkcs7 = NULL;
  24699. static byte messageTypeOid[] =
  24700. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  24701. 0x09, 0x02 };
  24702. static byte messageType[] = { 0x13, 2, '1', '9' };
  24703. PKCS7Attrib attribs[] =
  24704. {
  24705. { messageTypeOid, sizeof(messageTypeOid), messageType,
  24706. sizeof(messageType) }
  24707. };
  24708. byte intCARoot[TWOK_BUF];
  24709. byte intCA1[TWOK_BUF];
  24710. byte intCA2[TWOK_BUF];
  24711. byte cert[TWOK_BUF];
  24712. byte key[TWOK_BUF];
  24713. word32 intCARootSz = sizeof(intCARoot);
  24714. word32 intCA1Sz = sizeof(intCA1);
  24715. word32 intCA2Sz = sizeof(intCA2);
  24716. word32 certSz = sizeof(cert);
  24717. word32 keySz = sizeof(key);
  24718. XMEMSET(intCARoot, 0, intCARootSz);
  24719. XMEMSET(intCA1, 0, intCA1Sz);
  24720. XMEMSET(intCA2, 0, intCA2Sz);
  24721. XMEMSET(cert, 0, certSz);
  24722. XMEMSET(key, 0, keySz);
  24723. ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
  24724. intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
  24725. cert, &certSz, key, &keySz);
  24726. ExpectIntEQ(ret, 0);
  24727. XMEMSET(output, 0, outputSz);
  24728. ExpectIntEQ(wc_InitRng(&rng), 0);
  24729. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24730. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24731. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24732. if (useIntermediateCertChain == 1) {
  24733. /* Add intermediate and root CA certs into SignedData Certs SET */
  24734. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
  24735. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
  24736. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
  24737. }
  24738. if (pkcs7 != NULL) {
  24739. pkcs7->content = data;
  24740. pkcs7->contentSz = dataSz;
  24741. pkcs7->privateKey = key;
  24742. pkcs7->privateKeySz = (word32)sizeof(key);
  24743. if (pkAlgoType == RSA_TYPE) {
  24744. pkcs7->encryptOID = RSAk;
  24745. }
  24746. else {
  24747. pkcs7->encryptOID = ECDSAk;
  24748. }
  24749. #ifdef NO_SHA
  24750. pkcs7->hashOID = SHA256h;
  24751. #else
  24752. pkcs7->hashOID = SHAh;
  24753. #endif
  24754. pkcs7->rng = &rng;
  24755. if (withAttribs) {
  24756. /* include a signed attribute */
  24757. pkcs7->signedAttribs = attribs;
  24758. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  24759. }
  24760. }
  24761. if (detachedSig) {
  24762. ExpectIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  24763. }
  24764. outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz);
  24765. ExpectIntGT(outputSz, 0);
  24766. wc_PKCS7_Free(pkcs7);
  24767. pkcs7 = NULL;
  24768. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24769. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24770. if (detachedSig && (pkcs7 != NULL)) {
  24771. pkcs7->content = data;
  24772. pkcs7->contentSz = dataSz;
  24773. }
  24774. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24775. wc_PKCS7_Free(pkcs7);
  24776. wc_FreeRng(&rng);
  24777. if (EXPECT_FAIL()) {
  24778. outputSz = 0;
  24779. }
  24780. return outputSz;
  24781. }
  24782. #endif
  24783. /*
  24784. * Testing wc_PKCS_VerifySignedData()
  24785. */
  24786. static int test_wc_PKCS7_VerifySignedData_RSA(void)
  24787. {
  24788. EXPECT_DECLS;
  24789. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  24790. PKCS7* pkcs7 = NULL;
  24791. byte output[6000]; /* Large size needed for bundles with int CA certs */
  24792. word32 outputSz = sizeof(output);
  24793. byte data[] = "Test data to encode.";
  24794. byte badOut[1];
  24795. word32 badOutSz = 0;
  24796. byte badContent[] = "This is different content than was signed";
  24797. wc_HashAlg hash;
  24798. #ifdef NO_SHA
  24799. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  24800. #else
  24801. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24802. #endif
  24803. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24804. word32 hashSz = wc_HashGetDigestSize(hashType);
  24805. #ifndef NO_RSA
  24806. PKCS7DecodedAttrib* decodedAttrib = NULL;
  24807. /* contentType OID (1.2.840.113549.1.9.3) */
  24808. static const byte contentTypeOid[] =
  24809. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01, 0x09, 0x03 };
  24810. /* PKCS#7 DATA content type (contentType defaults to DATA) */
  24811. static const byte dataType[] =
  24812. { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01 };
  24813. /* messageDigest OID (1.2.840.113549.1.9.4) */
  24814. static const byte messageDigestOid[] =
  24815. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04 };
  24816. #ifndef NO_ASN_TIME
  24817. /* signingTime OID () */
  24818. static const byte signingTimeOid[] =
  24819. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05};
  24820. #endif
  24821. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  24822. int dateLength = 0;
  24823. byte dateFormat;
  24824. const byte* datePart = NULL;
  24825. struct tm timearg;
  24826. time_t now;
  24827. struct tm* nowTm = NULL;
  24828. #ifdef NEED_TMP_TIME
  24829. struct tm tmpTimeStorage;
  24830. struct tm* tmpTime = &tmpTimeStorage;
  24831. #endif
  24832. #endif /* !NO_ASN && !NO_ASN_TIME */
  24833. #ifndef NO_PKCS7_STREAM
  24834. word32 z;
  24835. int ret;
  24836. #endif /* !NO_PKCS7_STREAM */
  24837. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  24838. /* Success test with RSA certs/key */
  24839. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24840. (word32)sizeof(data), 0, 0, 0, RSA_TYPE)), 0);
  24841. /* calculate hash for content, used later */
  24842. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  24843. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  24844. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  24845. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  24846. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24847. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24848. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24849. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24850. #ifndef NO_PKCS7_STREAM
  24851. wc_PKCS7_Free(pkcs7);
  24852. pkcs7 = NULL;
  24853. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24854. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24855. /* test for streaming */
  24856. ret = -1;
  24857. for (z = 0; z < outputSz && ret != 0; z++) {
  24858. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24859. if (ret < 0){
  24860. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24861. }
  24862. }
  24863. ExpectIntEQ(ret, 0);
  24864. ExpectIntNE(pkcs7->contentSz, 0);
  24865. ExpectNotNull(pkcs7->contentDynamic);
  24866. #endif /* !NO_PKCS7_STREAM */
  24867. /* Check that decoded signed attributes are correct */
  24868. /* messageDigest should be first */
  24869. if (pkcs7 != NULL) {
  24870. decodedAttrib = pkcs7->decodedAttrib;
  24871. }
  24872. ExpectNotNull(decodedAttrib);
  24873. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(messageDigestOid));
  24874. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, messageDigestOid,
  24875. decodedAttrib->oidSz), 0);
  24876. /* + 2 for OCTET STRING and length bytes */
  24877. ExpectIntEQ(decodedAttrib->valueSz, hashSz + 2);
  24878. ExpectNotNull(decodedAttrib->value);
  24879. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, hashBuf, hashSz), 0);
  24880. #ifndef NO_ASN_TIME
  24881. /* signingTime should be second */
  24882. if (decodedAttrib != NULL) {
  24883. decodedAttrib = decodedAttrib->next;
  24884. }
  24885. ExpectNotNull(decodedAttrib);
  24886. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(signingTimeOid));
  24887. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, signingTimeOid,
  24888. decodedAttrib->oidSz), 0);
  24889. ExpectIntGT(decodedAttrib->valueSz, 0);
  24890. ExpectNotNull(decodedAttrib->value);
  24891. #endif
  24892. /* Verify signingTime if ASN and time are available */
  24893. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  24894. ExpectIntEQ(wc_GetDateInfo(decodedAttrib->value, decodedAttrib->valueSz,
  24895. &datePart, &dateFormat, &dateLength), 0);
  24896. ExpectNotNull(datePart);
  24897. ExpectIntGT(dateLength, 0);
  24898. XMEMSET(&timearg, 0, sizeof(timearg));
  24899. ExpectIntEQ(wc_GetDateAsCalendarTime(datePart, dateLength, dateFormat,
  24900. &timearg), 0);
  24901. /* Get current time and compare year/month/day against attribute value */
  24902. ExpectIntEQ(wc_GetTime(&now, sizeof(now)), 0);
  24903. nowTm = (struct tm*)XGMTIME((time_t*)&now, tmpTime);
  24904. ExpectNotNull(nowTm);
  24905. ExpectIntEQ(timearg.tm_year, nowTm->tm_year);
  24906. ExpectIntEQ(timearg.tm_mon, nowTm->tm_mon);
  24907. ExpectIntEQ(timearg.tm_mday, nowTm->tm_mday);
  24908. #endif /* !NO_ASN && !NO_ASN_TIME */
  24909. /* contentType should be third */
  24910. if (decodedAttrib != NULL) {
  24911. decodedAttrib = decodedAttrib->next;
  24912. }
  24913. ExpectNotNull(decodedAttrib);
  24914. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(contentTypeOid));
  24915. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, contentTypeOid,
  24916. decodedAttrib->oidSz), 0);
  24917. ExpectIntEQ(decodedAttrib->valueSz, (int)sizeof(dataType) + 2);
  24918. ExpectNotNull(decodedAttrib->value);
  24919. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, dataType, sizeof(dataType)),
  24920. 0);
  24921. #endif /* !NO_RSA */
  24922. /* Test bad args. */
  24923. ExpectIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
  24924. BAD_FUNC_ARG);
  24925. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
  24926. BAD_FUNC_ARG);
  24927. #ifndef NO_PKCS7_STREAM
  24928. /* can pass in 0 buffer length with streaming API */
  24929. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  24930. badOutSz), WC_PKCS7_WANT_READ_E);
  24931. #else
  24932. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  24933. badOutSz), BAD_FUNC_ARG);
  24934. #endif
  24935. wc_PKCS7_Free(pkcs7);
  24936. pkcs7 = NULL;
  24937. #ifndef NO_RSA
  24938. /* Try RSA certs/key/sig first */
  24939. outputSz = sizeof(output);
  24940. XMEMSET(output, 0, outputSz);
  24941. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  24942. (word32)sizeof(data),
  24943. 1, 1, 0, RSA_TYPE)), 0);
  24944. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24945. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24946. if (pkcs7 != NULL) {
  24947. pkcs7->content = badContent;
  24948. pkcs7->contentSz = sizeof(badContent);
  24949. }
  24950. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  24951. SIG_VERIFY_E);
  24952. wc_PKCS7_Free(pkcs7);
  24953. pkcs7 = NULL;
  24954. #ifndef NO_PKCS7_STREAM
  24955. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24956. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24957. if (pkcs7 != NULL) {
  24958. pkcs7->content = badContent;
  24959. pkcs7->contentSz = sizeof(badContent);
  24960. }
  24961. /* test for streaming */
  24962. ret = -1;
  24963. for (z = 0; z < outputSz && ret != 0; z++) {
  24964. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24965. if (ret == WC_PKCS7_WANT_READ_E){
  24966. continue;
  24967. }
  24968. else if (ret < 0) {
  24969. break;
  24970. }
  24971. }
  24972. ExpectIntEQ(ret, SIG_VERIFY_E);
  24973. ExpectIntNE(pkcs7->contentSz, 0);
  24974. ExpectNotNull(pkcs7->contentDynamic);
  24975. wc_PKCS7_Free(pkcs7);
  24976. pkcs7 = NULL;
  24977. #endif /* !NO_PKCS7_STREAM */
  24978. /* Test success case with detached signature and valid content */
  24979. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24980. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24981. if (pkcs7 != NULL) {
  24982. pkcs7->content = data;
  24983. pkcs7->contentSz = sizeof(data);
  24984. }
  24985. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24986. wc_PKCS7_Free(pkcs7);
  24987. pkcs7 = NULL;
  24988. #ifndef NO_PKCS7_STREAM
  24989. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24990. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24991. if (pkcs7 != NULL) {
  24992. pkcs7->content = data;
  24993. pkcs7->contentSz = sizeof(data);
  24994. }
  24995. /* test for streaming */
  24996. ret = -1;
  24997. for (z = 0; z < outputSz && ret != 0; z++) {
  24998. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24999. if (ret < 0){
  25000. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25001. }
  25002. }
  25003. ExpectIntEQ(ret, 0);
  25004. ExpectIntNE(pkcs7->contentSz, 0);
  25005. ExpectNotNull(pkcs7->contentDynamic);
  25006. wc_PKCS7_Free(pkcs7);
  25007. pkcs7 = NULL;
  25008. #endif /* !NO_PKCS7_STREAM */
  25009. /* verify using pre-computed content digest only (no content) */
  25010. {
  25011. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25012. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  25013. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25014. output, outputSz, NULL, 0), 0);
  25015. wc_PKCS7_Free(pkcs7);
  25016. pkcs7 = NULL;
  25017. }
  25018. #endif /* !NO_RSA */
  25019. /* Test verify on signedData containing intermediate/root CA certs */
  25020. #ifndef NO_RSA
  25021. outputSz = sizeof(output);
  25022. XMEMSET(output, 0, outputSz);
  25023. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25024. (word32)sizeof(data),
  25025. 0, 0, 1, RSA_TYPE)), 0);
  25026. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25027. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25028. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25029. wc_PKCS7_Free(pkcs7);
  25030. pkcs7 = NULL;
  25031. #ifndef NO_PKCS7_STREAM
  25032. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25033. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25034. /* test for streaming */
  25035. ret = -1;
  25036. for (z = 0; z < outputSz && ret != 0; z++) {
  25037. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25038. if (ret < 0){
  25039. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25040. }
  25041. }
  25042. ExpectIntEQ(ret, 0);
  25043. ExpectIntNE(pkcs7->contentSz, 0);
  25044. ExpectNotNull(pkcs7->contentDynamic);
  25045. wc_PKCS7_Free(pkcs7);
  25046. pkcs7 = NULL;
  25047. #endif /* !NO_PKCS7_STREAM */
  25048. #endif /* !NO_RSA */
  25049. #if defined(ASN_BER_TO_DER) && !defined(NO_PKCS7_STREAM) && \
  25050. !defined(NO_FILESYSTEM)
  25051. {
  25052. XFILE signedBundle = XBADFILE;
  25053. int signedBundleSz = 0;
  25054. int chunkSz = 1;
  25055. int i, rc = 0;
  25056. byte* buf = NULL;
  25057. ExpectTrue((signedBundle = XFOPEN("./certs/test-stream-sign.p7b",
  25058. "rb")) != XBADFILE);
  25059. ExpectTrue(XFSEEK(signedBundle, 0, XSEEK_END) == 0);
  25060. ExpectIntGT(signedBundleSz = (int)XFTELL(signedBundle), 0);
  25061. ExpectTrue(XFSEEK(signedBundle, 0, XSEEK_SET) == 0);
  25062. ExpectNotNull(buf = (byte*)XMALLOC(signedBundleSz, HEAP_HINT,
  25063. DYNAMIC_TYPE_FILE));
  25064. if (buf != NULL) {
  25065. ExpectIntEQ(XFREAD(buf, 1, signedBundleSz, signedBundle),
  25066. signedBundleSz);
  25067. }
  25068. if (signedBundle != XBADFILE) {
  25069. XFCLOSE(signedBundle);
  25070. signedBundle = XBADFILE;
  25071. }
  25072. if (buf != NULL) {
  25073. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25074. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25075. for (i = 0; i < signedBundleSz;) {
  25076. int sz = (i + chunkSz > signedBundleSz)? signedBundleSz - i :
  25077. chunkSz;
  25078. rc = wc_PKCS7_VerifySignedData(pkcs7, buf + i, sz);
  25079. if (rc < 0 ) {
  25080. if (rc == WC_PKCS7_WANT_READ_E) {
  25081. i += sz;
  25082. continue;
  25083. }
  25084. break;
  25085. }
  25086. else {
  25087. break;
  25088. }
  25089. }
  25090. ExpectIntEQ(rc, PKCS7_SIGNEEDS_CHECK);
  25091. wc_PKCS7_Free(pkcs7);
  25092. pkcs7 = NULL;
  25093. }
  25094. /* now try with malformed bundle */
  25095. if (buf != NULL) {
  25096. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25097. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25098. buf[signedBundleSz - 2] = buf[signedBundleSz - 2] + 1;
  25099. for (i = 0; i < signedBundleSz;) {
  25100. int sz = (i + chunkSz > signedBundleSz)? signedBundleSz - i :
  25101. chunkSz;
  25102. rc = wc_PKCS7_VerifySignedData(pkcs7, buf + i, sz);
  25103. if (rc < 0 ) {
  25104. if (rc == WC_PKCS7_WANT_READ_E) {
  25105. i += sz;
  25106. continue;
  25107. }
  25108. break;
  25109. }
  25110. else {
  25111. break;
  25112. }
  25113. }
  25114. ExpectIntEQ(rc, ASN_PARSE_E);
  25115. wc_PKCS7_Free(pkcs7);
  25116. pkcs7 = NULL;
  25117. }
  25118. if (buf != NULL)
  25119. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  25120. }
  25121. #endif /* BER and stream */
  25122. #endif
  25123. return EXPECT_RESULT();
  25124. } /* END test_wc_PKCS7_VerifySignedData()_RSA */
  25125. /*
  25126. * Testing wc_PKCS_VerifySignedData()
  25127. */
  25128. static int test_wc_PKCS7_VerifySignedData_ECC(void)
  25129. {
  25130. EXPECT_DECLS;
  25131. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  25132. PKCS7* pkcs7 = NULL;
  25133. byte output[6000]; /* Large size needed for bundles with int CA certs */
  25134. word32 outputSz = sizeof(output);
  25135. byte data[] = "Test data to encode.";
  25136. byte badContent[] = "This is different content than was signed";
  25137. wc_HashAlg hash;
  25138. #ifndef NO_PKCS7_STREAM
  25139. word32 z;
  25140. int ret;
  25141. #endif /* !NO_PKCS7_STREAM */
  25142. #ifdef NO_SHA
  25143. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  25144. #else
  25145. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  25146. #endif
  25147. byte hashBuf[WC_MAX_DIGEST_SIZE];
  25148. word32 hashSz = wc_HashGetDigestSize(hashType);
  25149. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  25150. /* Success test with ECC certs/key */
  25151. outputSz = sizeof(output);
  25152. XMEMSET(output, 0, outputSz);
  25153. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25154. (word32)sizeof(data), 0, 0, 0, ECC_TYPE)), 0);
  25155. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25156. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25157. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25158. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25159. wc_PKCS7_Free(pkcs7);
  25160. pkcs7 = NULL;
  25161. #ifndef NO_PKCS7_STREAM
  25162. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25163. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25164. /* test for streaming */
  25165. ret = -1;
  25166. for (z = 0; z < outputSz && ret != 0; z++) {
  25167. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25168. if (ret < 0){
  25169. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25170. }
  25171. }
  25172. ExpectIntEQ(ret, 0);
  25173. ExpectIntNE(pkcs7->contentSz, 0);
  25174. ExpectNotNull(pkcs7->contentDynamic);
  25175. wc_PKCS7_Free(pkcs7);
  25176. pkcs7 = NULL;
  25177. #endif /* !NO_PKCS7_STREAM */
  25178. /* Invalid content should error, use detached signature so we can
  25179. * easily change content */
  25180. outputSz = sizeof(output);
  25181. XMEMSET(output, 0, outputSz);
  25182. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25183. (word32)sizeof(data), 1, 1, 0, ECC_TYPE)), 0);
  25184. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25185. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25186. if (pkcs7 != NULL) {
  25187. pkcs7->content = badContent;
  25188. pkcs7->contentSz = sizeof(badContent);
  25189. }
  25190. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  25191. SIG_VERIFY_E);
  25192. wc_PKCS7_Free(pkcs7);
  25193. pkcs7 = NULL;
  25194. #ifndef NO_PKCS7_STREAM
  25195. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25196. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25197. if (pkcs7 != NULL) {
  25198. pkcs7->content = badContent;
  25199. pkcs7->contentSz = sizeof(badContent);
  25200. }
  25201. /* test for streaming */
  25202. ret = -1;
  25203. for (z = 0; z < outputSz && ret != 0; z++) {
  25204. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25205. if (ret == WC_PKCS7_WANT_READ_E){
  25206. continue;
  25207. }
  25208. else if (ret < 0) {
  25209. break;
  25210. }
  25211. }
  25212. ExpectIntEQ(ret, SIG_VERIFY_E);
  25213. ExpectIntNE(pkcs7->contentSz, 0);
  25214. ExpectNotNull(pkcs7->contentDynamic);
  25215. wc_PKCS7_Free(pkcs7);
  25216. pkcs7 = NULL;
  25217. #endif /* !NO_PKCS7_STREAM */
  25218. /* Test success case with detached signature and valid content */
  25219. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25220. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25221. if (pkcs7 != NULL) {
  25222. pkcs7->content = data;
  25223. pkcs7->contentSz = sizeof(data);
  25224. }
  25225. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25226. wc_PKCS7_Free(pkcs7);
  25227. pkcs7 = NULL;
  25228. #ifndef NO_PKCS7_STREAM
  25229. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25230. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25231. if (pkcs7 != NULL) {
  25232. pkcs7->content = data;
  25233. pkcs7->contentSz = sizeof(data);
  25234. }
  25235. /* test for streaming */
  25236. ret = -1;
  25237. for (z = 0; z < outputSz && ret != 0; z++) {
  25238. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25239. if (ret < 0){
  25240. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25241. }
  25242. }
  25243. ExpectIntEQ(ret, 0);
  25244. ExpectIntNE(pkcs7->contentSz, 0);
  25245. ExpectNotNull(pkcs7->contentDynamic);
  25246. wc_PKCS7_Free(pkcs7);
  25247. pkcs7 = NULL;
  25248. #endif /* !NO_PKCS7_STREAM */
  25249. /* verify using pre-computed content digest only (no content) */
  25250. {
  25251. /* calculate hash for content */
  25252. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  25253. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  25254. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  25255. ExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  25256. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25257. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  25258. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25259. output, outputSz, NULL, 0), 0);
  25260. wc_PKCS7_Free(pkcs7);
  25261. pkcs7 = NULL;
  25262. }
  25263. /* Test verify on signedData containing intermediate/root CA certs */
  25264. outputSz = sizeof(output);
  25265. XMEMSET(output, 0, outputSz);
  25266. ExpectIntGT((outputSz = CreatePKCS7SignedData(output, outputSz, data,
  25267. (word32)sizeof(data), 0, 0, 1, ECC_TYPE)), 0);
  25268. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25269. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25270. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25271. wc_PKCS7_Free(pkcs7);
  25272. pkcs7 = NULL;
  25273. #ifndef NO_PKCS7_STREAM
  25274. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25275. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25276. /* test for streaming */
  25277. ret = -1;
  25278. for (z = 0; z < outputSz && ret != 0; z++) {
  25279. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25280. if (ret < 0){
  25281. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25282. }
  25283. }
  25284. ExpectIntEQ(ret, 0);
  25285. ExpectIntNE(pkcs7->contentSz, 0);
  25286. ExpectNotNull(pkcs7->contentDynamic);
  25287. wc_PKCS7_Free(pkcs7);
  25288. pkcs7 = NULL;
  25289. #endif /* !NO_PKCS7_STREAM */
  25290. #endif
  25291. return EXPECT_RESULT();
  25292. } /* END test_wc_PKCS7_VerifySignedData_ECC() */
  25293. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  25294. !defined(NO_AES_256)
  25295. static const byte defKey[] = {
  25296. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25297. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25298. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25299. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25300. };
  25301. static byte aesHandle[32]; /* simulated hardware key handle */
  25302. /* return 0 on success */
  25303. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  25304. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  25305. byte* in, int inSz, byte* out, void* usrCtx)
  25306. {
  25307. int ret;
  25308. Aes aes;
  25309. if (usrCtx == NULL) {
  25310. /* no simulated handle passed in */
  25311. return -1;
  25312. }
  25313. switch (encryptOID) {
  25314. case AES256CBCb:
  25315. if (ivSz != AES_BLOCK_SIZE)
  25316. return BAD_FUNC_ARG;
  25317. break;
  25318. default:
  25319. WOLFSSL_MSG("Unsupported content cipher type for test");
  25320. return ALGO_ID_E;
  25321. };
  25322. /* simulate using handle to get key */
  25323. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  25324. if (ret == 0) {
  25325. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  25326. if (ret == 0)
  25327. ret = wc_AesCbcDecrypt(&aes, out, in, inSz);
  25328. wc_AesFree(&aes);
  25329. }
  25330. (void)aad;
  25331. (void)aadSz;
  25332. (void)authTag;
  25333. (void)authTagSz;
  25334. (void)pkcs7;
  25335. return ret;
  25336. }
  25337. /* returns key size on success */
  25338. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  25339. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  25340. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  25341. {
  25342. int ret = -1;
  25343. (void)cekSz;
  25344. (void)cek;
  25345. (void)outSz;
  25346. (void)keyIdSz;
  25347. (void)direction;
  25348. (void)orginKey; /* used with KAKRI */
  25349. (void)orginKeySz;
  25350. if (out == NULL)
  25351. return BAD_FUNC_ARG;
  25352. if (keyId[0] != 0x00) {
  25353. return -1;
  25354. }
  25355. if (type != (int)PKCS7_KEKRI) {
  25356. return -1;
  25357. }
  25358. switch (keyWrapAlgo) {
  25359. case AES256_WRAP:
  25360. /* simulate setting a handle for later decryption but use key
  25361. * as handle in the test case here */
  25362. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  25363. aesHandle, sizeof(aesHandle), NULL);
  25364. if (ret < 0)
  25365. return ret;
  25366. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  25367. if (ret < 0)
  25368. return ret;
  25369. /* return key size on success */
  25370. return sizeof(defKey);
  25371. default:
  25372. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  25373. return BAD_KEYWRAP_ALG_E;
  25374. };
  25375. }
  25376. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  25377. /*
  25378. * Testing wc_PKCS7_EncodeEnvelopedData()
  25379. */
  25380. static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
  25381. {
  25382. EXPECT_DECLS;
  25383. #if defined(HAVE_PKCS7)
  25384. PKCS7* pkcs7 = NULL;
  25385. #ifdef ASN_BER_TO_DER
  25386. int encodedSz = 0;
  25387. #endif
  25388. #ifdef ECC_TIMING_RESISTANT
  25389. WC_RNG rng;
  25390. #endif
  25391. word32 tempWrd32 = 0;
  25392. byte* tmpBytePtr = NULL;
  25393. const char input[] = "Test data to encode.";
  25394. int i;
  25395. int testSz = 0;
  25396. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  25397. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25398. byte* rsaCert = NULL;
  25399. byte* rsaPrivKey = NULL;
  25400. word32 rsaCertSz;
  25401. word32 rsaPrivKeySz;
  25402. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  25403. !defined(USE_CERT_BUFFERS_2048) )
  25404. static const char* rsaClientCert = "./certs/client-cert.der";
  25405. static const char* rsaClientKey = "./certs/client-key.der";
  25406. rsaCertSz = (word32)sizeof(rsaClientCert);
  25407. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  25408. #endif
  25409. #endif
  25410. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25411. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25412. byte* eccCert = NULL;
  25413. byte* eccPrivKey = NULL;
  25414. word32 eccCertSz;
  25415. word32 eccPrivKeySz;
  25416. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  25417. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  25418. static const char* eccClientKey = "./certs/ecc-client-key.der";
  25419. #endif
  25420. #endif
  25421. /* Generic buffer size. */
  25422. byte output[ONEK_BUF];
  25423. byte decoded[sizeof(input)/sizeof(char)];
  25424. int decodedSz = 0;
  25425. #ifndef NO_FILESYSTEM
  25426. XFILE certFile = XBADFILE;
  25427. XFILE keyFile = XBADFILE;
  25428. #endif
  25429. #ifdef ECC_TIMING_RESISTANT
  25430. XMEMSET(&rng, 0, sizeof(WC_RNG));
  25431. #endif
  25432. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25433. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25434. /* RSA certs and keys. */
  25435. #if defined(USE_CERT_BUFFERS_1024)
  25436. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  25437. /* Allocate buffer space. */
  25438. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  25439. DYNAMIC_TYPE_TMP_BUFFER));
  25440. /* Init buffer. */
  25441. if (rsaCert != NULL) {
  25442. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  25443. }
  25444. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  25445. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  25446. DYNAMIC_TYPE_TMP_BUFFER));
  25447. if (rsaPrivKey != NULL) {
  25448. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  25449. }
  25450. #elif defined(USE_CERT_BUFFERS_2048)
  25451. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  25452. /* Allocate buffer */
  25453. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  25454. DYNAMIC_TYPE_TMP_BUFFER));
  25455. /* Init buffer. */
  25456. if (rsaCert != NULL) {
  25457. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  25458. }
  25459. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  25460. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  25461. DYNAMIC_TYPE_TMP_BUFFER));
  25462. if (rsaPrivKey != NULL) {
  25463. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  25464. }
  25465. #else
  25466. /* File system. */
  25467. ExpectTrue((certFile = XFOPEN(rsaClientCert, "rb")) != XBADFILE);
  25468. rsaCertSz = (word32)FOURK_BUF;
  25469. ExpectNotNull(rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25470. DYNAMIC_TYPE_TMP_BUFFER));
  25471. ExpectTrue((rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz,
  25472. certFile)) > 0);
  25473. if (certFile != XBADFILE)
  25474. XFCLOSE(certFile);
  25475. ExpectTrue((keyFile = XFOPEN(rsaClientKey, "rb")) != XBADFILE);
  25476. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25477. DYNAMIC_TYPE_TMP_BUFFER));
  25478. rsaPrivKeySz = (word32)FOURK_BUF;
  25479. ExpectTrue((rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz,
  25480. keyFile)) > 0);
  25481. if (keyFile != XBADFILE)
  25482. XFCLOSE(keyFile);
  25483. #endif /* USE_CERT_BUFFERS */
  25484. #endif /* NO_RSA */
  25485. /* ECC */
  25486. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25487. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25488. #ifdef USE_CERT_BUFFERS_256
  25489. ExpectNotNull(eccCert = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  25490. DYNAMIC_TYPE_TMP_BUFFER));
  25491. /* Init buffer. */
  25492. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  25493. if (eccCert != NULL) {
  25494. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  25495. }
  25496. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  25497. DYNAMIC_TYPE_TMP_BUFFER));
  25498. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  25499. if (eccPrivKey != NULL) {
  25500. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  25501. }
  25502. #else /* File system. */
  25503. ExpectTrue((certFile = XFOPEN(eccClientCert, "rb")) != XBADFILE);
  25504. eccCertSz = (word32)FOURK_BUF;
  25505. ExpectNotNull(eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25506. DYNAMIC_TYPE_TMP_BUFFER));
  25507. ExpectTrue((eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz,
  25508. certFile)) > 0);
  25509. if (certFile != XBADFILE) {
  25510. XFCLOSE(certFile);
  25511. }
  25512. ExpectTrue((keyFile = XFOPEN(eccClientKey, "rb")) != XBADFILE);
  25513. eccPrivKeySz = (word32)FOURK_BUF;
  25514. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25515. DYNAMIC_TYPE_TMP_BUFFER));
  25516. ExpectTrue((eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz,
  25517. keyFile)) > 0);
  25518. if (keyFile != XBADFILE) {
  25519. XFCLOSE(keyFile);
  25520. }
  25521. #endif /* USE_CERT_BUFFERS_256 */
  25522. #endif /* END HAVE_ECC */
  25523. #ifndef NO_FILESYSTEM
  25524. /* Silence. */
  25525. (void)keyFile;
  25526. (void)certFile;
  25527. #endif
  25528. {
  25529. const pkcs7EnvelopedVector testVectors[] = {
  25530. /* DATA is a global variable defined in the makefile. */
  25531. #if !defined(NO_RSA)
  25532. #ifndef NO_DES3
  25533. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  25534. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25535. #endif /* NO_DES3 */
  25536. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25537. #ifndef NO_AES_128
  25538. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  25539. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25540. #endif
  25541. #ifndef NO_AES_192
  25542. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  25543. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25544. #endif
  25545. #ifndef NO_AES_256
  25546. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  25547. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25548. #endif
  25549. #endif /* NO_AES && HAVE_AES_CBC */
  25550. #endif /* NO_RSA */
  25551. #if defined(HAVE_ECC)
  25552. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25553. #if !defined(NO_SHA) && !defined(NO_AES_128)
  25554. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25555. AES128CBCb, AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme,
  25556. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25557. #endif
  25558. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  25559. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25560. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme,
  25561. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25562. #endif
  25563. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  25564. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25565. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme,
  25566. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25567. #endif
  25568. #endif /* NO_AES && HAVE_AES_CBC*/
  25569. #endif /* END HAVE_ECC */
  25570. }; /* END pkcs7EnvelopedVector */
  25571. #ifdef ECC_TIMING_RESISTANT
  25572. ExpectIntEQ(wc_InitRng(&rng), 0);
  25573. #endif
  25574. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25575. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25576. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  25577. for (i = 0; i < testSz; i++) {
  25578. #ifdef ASN_BER_TO_DER
  25579. encodeSignedDataStream strm;
  25580. /* test setting stream mode, the first one using IO callbacks */
  25581. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  25582. (word32)(testVectors + i)->certSz), 0);
  25583. if (pkcs7 != NULL) {
  25584. #ifdef ECC_TIMING_RESISTANT
  25585. pkcs7->rng = &rng;
  25586. #endif
  25587. if (i != 0)
  25588. pkcs7->content = (byte*)(testVectors + i)->content;
  25589. pkcs7->contentSz = (testVectors + i)->contentSz;
  25590. pkcs7->contentOID = (testVectors + i)->contentOID;
  25591. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  25592. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  25593. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  25594. pkcs7->privateKey = (testVectors + i)->privateKey;
  25595. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  25596. }
  25597. if (i == 0) {
  25598. XMEMSET(&strm, 0, sizeof(strm));
  25599. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
  25600. StreamOutputCB, (void*)&strm), 0);
  25601. encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL, 0);
  25602. }
  25603. else {
  25604. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL), 0);
  25605. encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25606. (word32)sizeof(output));
  25607. }
  25608. switch ((testVectors + i)->encryptOID) {
  25609. #ifndef NO_DES3
  25610. case DES3b:
  25611. case DESb:
  25612. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25613. break;
  25614. #endif
  25615. #ifdef HAVE_AESCCM
  25616. #ifdef WOLFSSL_AES_128
  25617. case AES128CCMb:
  25618. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25619. break;
  25620. #endif
  25621. #ifdef WOLFSSL_AES_192
  25622. case AES192CCMb:
  25623. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25624. break;
  25625. #endif
  25626. #ifdef WOLFSSL_AES_256
  25627. case AES256CCMb:
  25628. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25629. break;
  25630. #endif
  25631. #endif
  25632. default:
  25633. ExpectIntGE(encodedSz, 0);
  25634. }
  25635. if (encodedSz > 0) {
  25636. if (i == 0) {
  25637. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7,
  25638. strm.out, (word32)encodedSz, decoded,
  25639. (word32)sizeof(decoded));
  25640. }
  25641. else {
  25642. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25643. (word32)encodedSz, decoded, (word32)sizeof(decoded));
  25644. }
  25645. ExpectIntGE(decodedSz, 0);
  25646. /* Verify the size of each buffer. */
  25647. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  25648. }
  25649. wc_PKCS7_Free(pkcs7);
  25650. pkcs7 = NULL;
  25651. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25652. #endif
  25653. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  25654. (word32)(testVectors + i)->certSz), 0);
  25655. if (pkcs7 != NULL) {
  25656. #ifdef ECC_TIMING_RESISTANT
  25657. pkcs7->rng = &rng;
  25658. #endif
  25659. pkcs7->content = (byte*)(testVectors + i)->content;
  25660. pkcs7->contentSz = (testVectors + i)->contentSz;
  25661. pkcs7->contentOID = (testVectors + i)->contentOID;
  25662. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  25663. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  25664. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  25665. pkcs7->privateKey = (testVectors + i)->privateKey;
  25666. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  25667. }
  25668. #ifdef ASN_BER_TO_DER
  25669. /* test without setting stream mode */
  25670. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
  25671. #endif
  25672. ExpectIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25673. (word32)sizeof(output)), 0);
  25674. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25675. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  25676. ExpectIntGE(decodedSz, 0);
  25677. /* Verify the size of each buffer. */
  25678. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  25679. /* Don't free the last time through the loop. */
  25680. if (i < testSz - 1) {
  25681. wc_PKCS7_Free(pkcs7);
  25682. pkcs7 = NULL;
  25683. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25684. }
  25685. } /* END test loop. */
  25686. }
  25687. /* Test bad args. */
  25688. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  25689. (word32)sizeof(output)), BAD_FUNC_ARG);
  25690. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  25691. (word32)sizeof(output)), BAD_FUNC_ARG);
  25692. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  25693. /* Decode. */
  25694. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  25695. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25696. BAD_FUNC_ARG);
  25697. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25698. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25699. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25700. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  25701. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  25702. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25703. BAD_FUNC_ARG);
  25704. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  25705. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25706. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  25707. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  25708. /* only a failure for KARI test cases */
  25709. if (pkcs7 != NULL) {
  25710. tempWrd32 = pkcs7->singleCertSz;
  25711. pkcs7->singleCertSz = 0;
  25712. }
  25713. #if defined(WOLFSSL_ASN_TEMPLATE)
  25714. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25715. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25716. BUFFER_E);
  25717. #else
  25718. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25719. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25720. ASN_PARSE_E);
  25721. #endif
  25722. if (pkcs7 != NULL) {
  25723. pkcs7->singleCertSz = tempWrd32;
  25724. tmpBytePtr = pkcs7->singleCert;
  25725. pkcs7->singleCert = NULL;
  25726. }
  25727. #ifndef NO_RSA
  25728. #if defined(NO_PKCS7_STREAM)
  25729. /* when none streaming mode is used and PKCS7 is in bad state buffer error
  25730. * is returned from kari parse which gets set to bad func arg */
  25731. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25732. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25733. BAD_FUNC_ARG);
  25734. #else
  25735. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25736. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25737. ASN_PARSE_E);
  25738. #endif
  25739. #endif /* !NO_RSA */
  25740. if (pkcs7 != NULL) {
  25741. pkcs7->singleCert = tmpBytePtr;
  25742. }
  25743. #endif
  25744. if (pkcs7 != NULL) {
  25745. tempWrd32 = pkcs7->privateKeySz;
  25746. pkcs7->privateKeySz = 0;
  25747. }
  25748. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25749. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25750. BAD_FUNC_ARG);
  25751. if (pkcs7 != NULL) {
  25752. pkcs7->privateKeySz = tempWrd32;
  25753. tmpBytePtr = pkcs7->privateKey;
  25754. pkcs7->privateKey = NULL;
  25755. }
  25756. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25757. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25758. BAD_FUNC_ARG);
  25759. if (pkcs7 != NULL) {
  25760. pkcs7->privateKey = tmpBytePtr;
  25761. }
  25762. wc_PKCS7_Free(pkcs7);
  25763. pkcs7 = NULL;
  25764. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  25765. /* test of decrypt callback with KEKRI enveloped data */
  25766. {
  25767. int envelopedSz = 0;
  25768. const byte keyId[] = { 0x00 };
  25769. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25770. if (pkcs7 != NULL) {
  25771. pkcs7->content = (byte*)input;
  25772. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  25773. pkcs7->contentOID = DATA;
  25774. pkcs7->encryptOID = AES256CBCb;
  25775. }
  25776. ExpectIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  25777. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  25778. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  25779. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  25780. ExpectIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25781. (word32)sizeof(output))), 0);
  25782. wc_PKCS7_Free(pkcs7);
  25783. pkcs7 = NULL;
  25784. /* decode envelopedData */
  25785. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25786. ExpectIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  25787. ExpectIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  25788. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25789. envelopedSz, decoded, sizeof(decoded))), 0);
  25790. wc_PKCS7_Free(pkcs7);
  25791. pkcs7 = NULL;
  25792. }
  25793. #endif /* !NO_AES && !NO_AES_256 */
  25794. #ifndef NO_RSA
  25795. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25796. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25797. #endif /* NO_RSA */
  25798. #ifdef HAVE_ECC
  25799. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25800. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25801. #endif /* HAVE_ECC */
  25802. #ifdef ECC_TIMING_RESISTANT
  25803. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  25804. #endif
  25805. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && \
  25806. !defined(NO_RSA) && !defined(NO_SHA)
  25807. {
  25808. byte out[7];
  25809. byte *cms = NULL;
  25810. word32 cmsSz;
  25811. XFILE cmsFile = XBADFILE;
  25812. XMEMSET(out, 0, sizeof(out));
  25813. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25814. ExpectTrue((cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb"))
  25815. != XBADFILE);
  25816. cmsSz = (word32)FOURK_BUF;
  25817. ExpectNotNull(cms = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25818. DYNAMIC_TYPE_TMP_BUFFER));
  25819. ExpectTrue((cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile)) > 0);
  25820. if (cmsFile != XBADFILE)
  25821. XFCLOSE(cmsFile);
  25822. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  25823. sizeof_client_cert_der_2048), 0);
  25824. if (pkcs7 != NULL) {
  25825. pkcs7->privateKey = (byte*)client_key_der_2048;
  25826. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  25827. }
  25828. ExpectIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  25829. 2), 0);
  25830. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  25831. sizeof(out)), 0);
  25832. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25833. ExpectIntEQ(XMEMCMP(out, "test", 4), 0);
  25834. wc_PKCS7_Free(pkcs7);
  25835. pkcs7 = NULL;
  25836. }
  25837. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 && !NO_RSA && !NO_SHA */
  25838. #endif /* HAVE_PKCS7 */
  25839. return EXPECT_RESULT();
  25840. } /* END test_wc_PKCS7_EncodeDecodeEnvelopedData() */
  25841. /*
  25842. * Testing wc_PKCS7_EncodeEncryptedData()
  25843. */
  25844. static int test_wc_PKCS7_EncodeEncryptedData(void)
  25845. {
  25846. EXPECT_DECLS;
  25847. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  25848. PKCS7* pkcs7 = NULL;
  25849. byte* tmpBytePtr = NULL;
  25850. byte encrypted[TWOK_BUF];
  25851. byte decoded[TWOK_BUF];
  25852. word32 tmpWrd32 = 0;
  25853. int tmpInt = 0;
  25854. int decodedSz = 0;
  25855. int encryptedSz = 0;
  25856. int testSz = 0;
  25857. int i = 0;
  25858. const byte data[] = { /* Hello World */
  25859. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  25860. 0x72,0x6c,0x64
  25861. };
  25862. #ifndef NO_DES3
  25863. byte desKey[] = {
  25864. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  25865. };
  25866. byte des3Key[] = {
  25867. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  25868. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  25869. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  25870. };
  25871. #endif
  25872. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25873. #ifndef NO_AES_128
  25874. byte aes128Key[] = {
  25875. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25876. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25877. };
  25878. #endif
  25879. #ifndef NO_AES_192
  25880. byte aes192Key[] = {
  25881. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25882. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25883. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25884. };
  25885. #endif
  25886. #ifndef NO_AES_256
  25887. byte aes256Key[] = {
  25888. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25889. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25890. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25891. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25892. };
  25893. #endif
  25894. #endif /* !NO_AES && HAVE_AES_CBC */
  25895. const pkcs7EncryptedVector testVectors[] =
  25896. {
  25897. #ifndef NO_DES3
  25898. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  25899. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  25900. #endif /* !NO_DES3 */
  25901. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25902. #ifndef NO_AES_128
  25903. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  25904. sizeof(aes128Key)},
  25905. #endif
  25906. #ifndef NO_AES_192
  25907. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  25908. sizeof(aes192Key)},
  25909. #endif
  25910. #ifndef NO_AES_256
  25911. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  25912. sizeof(aes256Key)},
  25913. #endif
  25914. #endif /* !NO_AES && HAVE_AES_CBC */
  25915. };
  25916. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  25917. for (i = 0; i < testSz; i++) {
  25918. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25919. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25920. if (pkcs7 != NULL) {
  25921. pkcs7->content = (byte*)testVectors[i].content;
  25922. pkcs7->contentSz = testVectors[i].contentSz;
  25923. pkcs7->contentOID = testVectors[i].contentOID;
  25924. pkcs7->encryptOID = testVectors[i].encryptOID;
  25925. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  25926. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  25927. pkcs7->heap = HEAP_HINT;
  25928. }
  25929. /* encode encryptedData */
  25930. ExpectIntGT(encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25931. sizeof(encrypted)), 0);
  25932. /* Decode encryptedData */
  25933. ExpectIntGT(decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted,
  25934. encryptedSz, decoded, sizeof(decoded)), 0);
  25935. ExpectIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  25936. /* Keep values for last itr. */
  25937. if (i < testSz - 1) {
  25938. wc_PKCS7_Free(pkcs7);
  25939. pkcs7 = NULL;
  25940. }
  25941. }
  25942. if (pkcs7 == NULL || testSz == 0) {
  25943. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25944. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25945. }
  25946. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  25947. sizeof(encrypted)),BAD_FUNC_ARG);
  25948. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  25949. sizeof(encrypted)), BAD_FUNC_ARG);
  25950. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25951. 0), BAD_FUNC_ARG);
  25952. /* Testing the struct. */
  25953. if (pkcs7 != NULL) {
  25954. tmpBytePtr = pkcs7->content;
  25955. pkcs7->content = NULL;
  25956. }
  25957. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25958. sizeof(encrypted)), BAD_FUNC_ARG);
  25959. if (pkcs7 != NULL) {
  25960. pkcs7->content = tmpBytePtr;
  25961. tmpWrd32 = pkcs7->contentSz;
  25962. pkcs7->contentSz = 0;
  25963. }
  25964. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25965. sizeof(encrypted)), BAD_FUNC_ARG);
  25966. if (pkcs7 != NULL) {
  25967. pkcs7->contentSz = tmpWrd32;
  25968. tmpInt = pkcs7->encryptOID;
  25969. pkcs7->encryptOID = 0;
  25970. }
  25971. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25972. sizeof(encrypted)), BAD_FUNC_ARG);
  25973. if (pkcs7 != NULL) {
  25974. pkcs7->encryptOID = tmpInt;
  25975. tmpBytePtr = pkcs7->encryptionKey;
  25976. pkcs7->encryptionKey = NULL;
  25977. }
  25978. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25979. sizeof(encrypted)), BAD_FUNC_ARG);
  25980. if (pkcs7 != NULL) {
  25981. pkcs7->encryptionKey = tmpBytePtr;
  25982. tmpWrd32 = pkcs7->encryptionKeySz;
  25983. pkcs7->encryptionKeySz = 0;
  25984. }
  25985. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25986. sizeof(encrypted)), BAD_FUNC_ARG);
  25987. if (pkcs7 != NULL) {
  25988. pkcs7->encryptionKeySz = tmpWrd32;
  25989. }
  25990. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, encryptedSz,
  25991. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25992. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, encryptedSz,
  25993. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25994. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  25995. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  25996. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25997. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  25998. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  25999. decoded, 0), BAD_FUNC_ARG);
  26000. /* Test struct fields */
  26001. if (pkcs7 != NULL) {
  26002. tmpBytePtr = pkcs7->encryptionKey;
  26003. pkcs7->encryptionKey = NULL;
  26004. }
  26005. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  26006. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26007. if (pkcs7 != NULL) {
  26008. pkcs7->encryptionKey = tmpBytePtr;
  26009. pkcs7->encryptionKeySz = 0;
  26010. }
  26011. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, encryptedSz,
  26012. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26013. wc_PKCS7_Free(pkcs7);
  26014. #endif
  26015. return EXPECT_RESULT();
  26016. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  26017. /*
  26018. * Testing wc_PKCS7_Degenerate()
  26019. */
  26020. static int test_wc_PKCS7_Degenerate(void)
  26021. {
  26022. EXPECT_DECLS;
  26023. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  26024. PKCS7* pkcs7 = NULL;
  26025. char fName[] = "./certs/test-degenerate.p7b";
  26026. XFILE f = XBADFILE;
  26027. byte der[4096];
  26028. word32 derSz = 0;
  26029. #ifndef NO_PKCS7_STREAM
  26030. word32 z;
  26031. int ret;
  26032. #endif /* !NO_PKCS7_STREAM */
  26033. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  26034. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  26035. if (f != XBADFILE)
  26036. XFCLOSE(f);
  26037. /* test degenerate success */
  26038. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26039. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26040. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26041. #ifndef NO_RSA
  26042. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26043. #ifndef NO_PKCS7_STREAM
  26044. wc_PKCS7_Free(pkcs7);
  26045. pkcs7 = NULL;
  26046. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26047. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26048. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26049. /* test for streaming */
  26050. ret = -1;
  26051. for (z = 0; z < derSz && ret != 0; z++) {
  26052. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  26053. if (ret < 0){
  26054. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26055. }
  26056. }
  26057. ExpectIntEQ(ret, 0);
  26058. #endif /* !NO_PKCS7_STREAM */
  26059. #else
  26060. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26061. #endif /* NO_RSA */
  26062. wc_PKCS7_Free(pkcs7);
  26063. pkcs7 = NULL;
  26064. /* test with turning off degenerate cases */
  26065. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26066. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26067. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26068. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  26069. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz),
  26070. PKCS7_NO_SIGNER_E);
  26071. #ifndef NO_PKCS7_STREAM
  26072. wc_PKCS7_Free(pkcs7);
  26073. pkcs7 = NULL;
  26074. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26075. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26076. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26077. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  26078. /* test for streaming */
  26079. ret = -1;
  26080. for (z = 0; z < derSz && ret != 0; z++) {
  26081. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  26082. if (ret == WC_PKCS7_WANT_READ_E){
  26083. continue;
  26084. }
  26085. else
  26086. break;
  26087. }
  26088. ExpectIntEQ(ret, PKCS7_NO_SIGNER_E);
  26089. #endif /* !NO_PKCS7_STREAM */
  26090. wc_PKCS7_Free(pkcs7);
  26091. #endif
  26092. return EXPECT_RESULT();
  26093. } /* END test_wc_PKCS7_Degenerate() */
  26094. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  26095. defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA)
  26096. static byte berContent[] = {
  26097. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  26098. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  26099. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  26100. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  26101. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  26102. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  26103. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  26104. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  26105. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  26106. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  26107. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  26108. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  26109. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  26110. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  26111. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  26112. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  26113. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  26114. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  26115. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  26116. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  26117. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  26118. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  26119. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  26120. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  26121. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  26122. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  26123. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  26124. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  26125. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  26126. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  26127. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  26128. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  26129. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  26130. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  26131. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  26132. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  26133. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  26134. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  26135. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  26136. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  26137. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  26138. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  26139. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  26140. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  26141. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  26142. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  26143. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  26144. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  26145. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  26146. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  26147. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  26148. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  26149. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  26150. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  26151. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  26152. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  26153. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  26154. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  26155. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  26156. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  26157. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  26158. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  26159. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  26160. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  26161. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  26162. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  26163. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  26164. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  26165. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  26166. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  26167. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  26168. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  26169. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  26170. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  26171. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  26172. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  26173. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  26174. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  26175. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  26176. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  26177. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  26178. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  26179. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  26180. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  26181. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  26182. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  26183. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  26184. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  26185. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  26186. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  26187. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  26188. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  26189. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  26190. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  26191. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  26192. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  26193. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  26194. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  26195. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  26196. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  26197. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  26198. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  26199. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  26200. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  26201. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  26202. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  26203. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  26204. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  26205. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  26206. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  26207. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  26208. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  26209. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  26210. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  26211. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  26212. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  26213. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  26214. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  26215. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  26216. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  26217. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  26218. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  26219. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  26220. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  26221. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  26222. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  26223. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  26224. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  26225. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  26226. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  26227. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  26228. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  26229. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  26230. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  26231. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  26232. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  26233. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  26234. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  26235. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  26236. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  26237. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  26238. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  26239. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  26240. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  26241. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  26242. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  26243. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  26244. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  26245. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  26246. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  26247. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  26248. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  26249. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  26250. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  26251. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  26252. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  26253. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  26254. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  26255. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  26256. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  26257. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  26258. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  26259. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  26260. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  26261. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  26262. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  26263. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  26264. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  26265. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  26266. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  26267. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  26268. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  26269. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  26270. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  26271. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  26272. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  26273. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  26274. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  26275. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  26276. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  26277. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  26278. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  26279. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  26280. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  26281. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  26282. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  26283. 0x00, 0x00, 0x00, 0x00, 0x00
  26284. };
  26285. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER &&
  26286. * !NO_DES3 && !NO_SHA
  26287. */
  26288. /*
  26289. * Testing wc_PKCS7_BER()
  26290. */
  26291. static int test_wc_PKCS7_BER(void)
  26292. {
  26293. EXPECT_DECLS;
  26294. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  26295. !defined(NO_SHA) && defined(ASN_BER_TO_DER)
  26296. PKCS7* pkcs7 = NULL;
  26297. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  26298. XFILE f = XBADFILE;
  26299. byte der[4096];
  26300. #ifndef NO_DES3
  26301. byte decoded[2048];
  26302. #endif
  26303. word32 derSz = 0;
  26304. #ifndef NO_PKCS7_STREAM
  26305. word32 z;
  26306. int ret;
  26307. #endif /* !NO_PKCS7_STREAM */
  26308. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  26309. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  26310. if (f != XBADFILE) {
  26311. XFCLOSE(f);
  26312. f = XBADFILE;
  26313. }
  26314. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26315. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26316. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26317. #ifndef NO_RSA
  26318. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26319. #ifndef NO_PKCS7_STREAM
  26320. wc_PKCS7_Free(pkcs7);
  26321. pkcs7 = NULL;
  26322. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26323. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26324. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26325. /* test for streaming */
  26326. ret = -1;
  26327. for (z = 0; z < derSz && ret != 0; z++) {
  26328. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  26329. if (ret < 0){
  26330. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26331. }
  26332. }
  26333. ExpectIntEQ(ret, 0);
  26334. #endif /* !NO_PKCS7_STREAM */
  26335. #else
  26336. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26337. #endif
  26338. wc_PKCS7_Free(pkcs7);
  26339. pkcs7 = NULL;
  26340. #ifndef NO_DES3
  26341. /* decode BER content */
  26342. ExpectTrue((f = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  26343. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  26344. if (f != XBADFILE) {
  26345. XFCLOSE(f);
  26346. f = XBADFILE;
  26347. }
  26348. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26349. #ifndef NO_RSA
  26350. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  26351. #else
  26352. ExpectIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  26353. #endif
  26354. ExpectTrue((f = XFOPEN("./certs/1024/client-key.der", "rb")) != XBADFILE);
  26355. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  26356. if (f != XBADFILE) {
  26357. XFCLOSE(f);
  26358. f = XBADFILE;
  26359. }
  26360. if (pkcs7 != NULL) {
  26361. pkcs7->privateKey = der;
  26362. pkcs7->privateKeySz = derSz;
  26363. }
  26364. #ifndef NO_RSA
  26365. #ifdef WOLFSSL_SP_MATH
  26366. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26367. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  26368. #else
  26369. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26370. sizeof(berContent), decoded, sizeof(decoded)), 0);
  26371. #endif
  26372. #else
  26373. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26374. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  26375. #endif
  26376. wc_PKCS7_Free(pkcs7);
  26377. #endif /* !NO_DES3 */
  26378. #endif
  26379. return EXPECT_RESULT();
  26380. } /* END test_wc_PKCS7_BER() */
  26381. static int test_wc_PKCS7_signed_enveloped(void)
  26382. {
  26383. EXPECT_DECLS;
  26384. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  26385. !defined(NO_FILESYSTEM)
  26386. XFILE f = XBADFILE;
  26387. PKCS7* pkcs7 = NULL;
  26388. #ifdef HAVE_AES_CBC
  26389. PKCS7* inner = NULL;
  26390. #endif
  26391. WC_RNG rng;
  26392. unsigned char key[FOURK_BUF/2];
  26393. unsigned char cert[FOURK_BUF/2];
  26394. unsigned char env[FOURK_BUF];
  26395. int envSz = FOURK_BUF;
  26396. int keySz = 0;
  26397. int certSz = 0;
  26398. unsigned char sig[FOURK_BUF * 2];
  26399. int sigSz = FOURK_BUF * 2;
  26400. #ifdef HAVE_AES_CBC
  26401. unsigned char decoded[FOURK_BUF];
  26402. int decodedSz = FOURK_BUF;
  26403. #endif
  26404. #ifndef NO_PKCS7_STREAM
  26405. int z;
  26406. int ret;
  26407. #endif /* !NO_PKCS7_STREAM */
  26408. XMEMSET(&rng, 0, sizeof(WC_RNG));
  26409. /* load cert */
  26410. ExpectTrue((f = XFOPEN(cliCertDerFile, "rb")) != XBADFILE);
  26411. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  26412. if (f != XBADFILE) {
  26413. XFCLOSE(f);
  26414. f = XBADFILE;
  26415. }
  26416. /* load key */
  26417. ExpectTrue((f = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  26418. ExpectIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  26419. if (f != XBADFILE) {
  26420. XFCLOSE(f);
  26421. f = XBADFILE;
  26422. }
  26423. ExpectIntGT(keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL), 0);
  26424. /* sign cert for envelope */
  26425. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26426. ExpectIntEQ(wc_InitRng(&rng), 0);
  26427. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26428. if (pkcs7 != NULL) {
  26429. pkcs7->content = cert;
  26430. pkcs7->contentSz = certSz;
  26431. pkcs7->contentOID = DATA;
  26432. pkcs7->privateKey = key;
  26433. pkcs7->privateKeySz = keySz;
  26434. pkcs7->encryptOID = RSAk;
  26435. pkcs7->hashOID = SHA256h;
  26436. pkcs7->rng = &rng;
  26437. }
  26438. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26439. wc_PKCS7_Free(pkcs7);
  26440. pkcs7 = NULL;
  26441. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  26442. #ifdef HAVE_AES_CBC
  26443. /* create envelope */
  26444. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26445. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26446. if (pkcs7 != NULL) {
  26447. pkcs7->content = sig;
  26448. pkcs7->contentSz = sigSz;
  26449. pkcs7->contentOID = DATA;
  26450. pkcs7->encryptOID = AES256CBCb;
  26451. pkcs7->privateKey = key;
  26452. pkcs7->privateKeySz = keySz;
  26453. }
  26454. ExpectIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, envSz)), 0);
  26455. ExpectIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  26456. wc_PKCS7_Free(pkcs7);
  26457. pkcs7 = NULL;
  26458. #endif
  26459. /* create bad signed enveloped data */
  26460. sigSz = FOURK_BUF * 2;
  26461. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26462. ExpectIntEQ(wc_InitRng(&rng), 0);
  26463. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26464. if (pkcs7 != NULL) {
  26465. pkcs7->content = env;
  26466. pkcs7->contentSz = envSz;
  26467. pkcs7->contentOID = DATA;
  26468. pkcs7->privateKey = key;
  26469. pkcs7->privateKeySz = keySz;
  26470. pkcs7->encryptOID = RSAk;
  26471. pkcs7->hashOID = SHA256h;
  26472. pkcs7->rng = &rng;
  26473. }
  26474. /* Set no certs in bundle for this test. */
  26475. if (pkcs7 != NULL) {
  26476. ExpectIntEQ(wc_PKCS7_SetNoCerts(pkcs7, 1), 0);
  26477. ExpectIntEQ(wc_PKCS7_SetNoCerts(NULL, 1), BAD_FUNC_ARG);
  26478. ExpectIntEQ(wc_PKCS7_GetNoCerts(pkcs7), 1);
  26479. }
  26480. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26481. wc_PKCS7_Free(pkcs7);
  26482. pkcs7 = NULL;
  26483. /* check verify fails */
  26484. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26485. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26486. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz),
  26487. PKCS7_SIGNEEDS_CHECK);
  26488. /* try verifying the signature manually */
  26489. {
  26490. RsaKey rKey;
  26491. word32 idx = 0;
  26492. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  26493. WC_MAX_DIGEST_SIZE];
  26494. int digestSz = 0;
  26495. ExpectIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  26496. ExpectIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, keySz), 0);
  26497. ExpectIntGT(digestSz = wc_RsaSSL_Verify(pkcs7->signature,
  26498. pkcs7->signatureSz, digest, sizeof(digest), &rKey), 0);
  26499. ExpectIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  26500. ExpectIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  26501. ExpectIntEQ(wc_FreeRsaKey(&rKey), 0);
  26502. /* verify was success */
  26503. }
  26504. wc_PKCS7_Free(pkcs7);
  26505. pkcs7 = NULL;
  26506. /* initializing the PKCS7 struct with the signing certificate should pass */
  26507. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26508. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26509. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  26510. #ifndef NO_PKCS7_STREAM
  26511. wc_PKCS7_Free(pkcs7);
  26512. pkcs7 = NULL;
  26513. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26514. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  26515. /* test for streaming */
  26516. ret = -1;
  26517. for (z = 0; z < sigSz && ret != 0; z++) {
  26518. ret = wc_PKCS7_VerifySignedData(pkcs7, sig + z, 1);
  26519. if (ret < 0){
  26520. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26521. }
  26522. }
  26523. ExpectIntEQ(ret, 0);
  26524. #endif /* !NO_PKCS7_STREAM */
  26525. wc_PKCS7_Free(pkcs7);
  26526. pkcs7 = NULL;
  26527. /* create valid degenerate bundle */
  26528. sigSz = FOURK_BUF * 2;
  26529. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26530. if (pkcs7 != NULL) {
  26531. pkcs7->content = env;
  26532. pkcs7->contentSz = envSz;
  26533. pkcs7->contentOID = DATA;
  26534. pkcs7->privateKey = key;
  26535. pkcs7->privateKeySz = keySz;
  26536. pkcs7->encryptOID = RSAk;
  26537. pkcs7->hashOID = SHA256h;
  26538. pkcs7->rng = &rng;
  26539. }
  26540. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  26541. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26542. wc_PKCS7_Free(pkcs7);
  26543. pkcs7 = NULL;
  26544. wc_FreeRng(&rng);
  26545. /* check verify */
  26546. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26547. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26548. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, sigSz), 0);
  26549. ExpectNotNull(pkcs7->content);
  26550. #ifndef NO_PKCS7_STREAM
  26551. wc_PKCS7_Free(pkcs7);
  26552. pkcs7 = NULL;
  26553. /* create valid degenerate bundle */
  26554. sigSz = FOURK_BUF * 2;
  26555. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26556. if (pkcs7 != NULL) {
  26557. pkcs7->content = env;
  26558. pkcs7->contentSz = envSz;
  26559. pkcs7->contentOID = DATA;
  26560. pkcs7->privateKey = key;
  26561. pkcs7->privateKeySz = keySz;
  26562. pkcs7->encryptOID = RSAk;
  26563. pkcs7->hashOID = SHA256h;
  26564. pkcs7->rng = &rng;
  26565. }
  26566. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  26567. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, sigSz)), 0);
  26568. wc_PKCS7_Free(pkcs7);
  26569. pkcs7 = NULL;
  26570. wc_FreeRng(&rng);
  26571. /* check verify */
  26572. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26573. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26574. /* test for streaming */
  26575. ret = -1;
  26576. for (z = 0; z < sigSz && ret != 0; z++) {
  26577. ret = wc_PKCS7_VerifySignedData(pkcs7, sig + z, 1);
  26578. if (ret < 0){
  26579. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26580. }
  26581. }
  26582. ExpectIntEQ(ret, 0);
  26583. #endif /* !NO_PKCS7_STREAM */
  26584. #ifdef HAVE_AES_CBC
  26585. /* check decode */
  26586. ExpectNotNull(inner = wc_PKCS7_New(NULL, 0));
  26587. ExpectIntEQ(wc_PKCS7_InitWithCert(inner, cert, certSz), 0);
  26588. if (inner != NULL) {
  26589. inner->privateKey = key;
  26590. inner->privateKeySz = keySz;
  26591. }
  26592. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  26593. pkcs7->contentSz, decoded, decodedSz)), 0);
  26594. wc_PKCS7_Free(inner);
  26595. inner = NULL;
  26596. #endif
  26597. wc_PKCS7_Free(pkcs7);
  26598. pkcs7 = NULL;
  26599. #ifdef HAVE_AES_CBC
  26600. /* check cert set */
  26601. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26602. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26603. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, decodedSz), 0);
  26604. ExpectNotNull(pkcs7->singleCert);
  26605. ExpectIntNE(pkcs7->singleCertSz, 0);
  26606. wc_PKCS7_Free(pkcs7);
  26607. pkcs7 = NULL;
  26608. #ifndef NO_PKCS7_STREAM
  26609. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26610. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26611. /* test for streaming */
  26612. ret = -1;
  26613. for (z = 0; z < decodedSz && ret != 0; z++) {
  26614. ret = wc_PKCS7_VerifySignedData(pkcs7, decoded + z, 1);
  26615. if (ret < 0){
  26616. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26617. }
  26618. }
  26619. ExpectIntEQ(ret, 0);
  26620. ExpectNotNull(pkcs7->singleCert);
  26621. ExpectIntNE(pkcs7->singleCertSz, 0);
  26622. wc_PKCS7_Free(pkcs7);
  26623. pkcs7 = NULL;
  26624. #endif /* !NO_PKCS7_STREAM */
  26625. #endif
  26626. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  26627. return EXPECT_RESULT();
  26628. }
  26629. static int test_wc_PKCS7_NoDefaultSignedAttribs(void)
  26630. {
  26631. EXPECT_DECLS;
  26632. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26633. && !defined(NO_AES)
  26634. PKCS7* pkcs7 = NULL;
  26635. void* heap = NULL;
  26636. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26637. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26638. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  26639. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  26640. wc_PKCS7_Free(pkcs7);
  26641. #endif
  26642. return EXPECT_RESULT();
  26643. }
  26644. static int test_wc_PKCS7_SetOriEncryptCtx(void)
  26645. {
  26646. EXPECT_DECLS;
  26647. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26648. && !defined(NO_AES)
  26649. PKCS7* pkcs7 = NULL;
  26650. void* heap = NULL;
  26651. WOLFSSL_CTX* ctx = NULL;
  26652. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26653. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26654. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26655. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  26656. wc_PKCS7_Free(pkcs7);
  26657. #endif
  26658. return EXPECT_RESULT();
  26659. }
  26660. static int test_wc_PKCS7_SetOriDecryptCtx(void)
  26661. {
  26662. EXPECT_DECLS;
  26663. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26664. && !defined(NO_AES)
  26665. PKCS7* pkcs7 = NULL;
  26666. void* heap = NULL;
  26667. WOLFSSL_CTX* ctx = NULL;
  26668. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26669. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26670. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26671. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  26672. wc_PKCS7_Free(pkcs7);
  26673. #endif
  26674. return EXPECT_RESULT();
  26675. }
  26676. static int test_wc_PKCS7_DecodeCompressedData(void)
  26677. {
  26678. EXPECT_DECLS;
  26679. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26680. && !defined(NO_AES) && defined(HAVE_LIBZ)
  26681. PKCS7* pkcs7 = NULL;
  26682. void* heap = NULL;
  26683. byte out[4096];
  26684. byte* decompressed = NULL;
  26685. int outSz;
  26686. int decompressedSz;
  26687. const char* cert = "./certs/client-cert.pem";
  26688. byte* cert_buf = NULL;
  26689. size_t cert_sz = 0;
  26690. ExpectIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  26691. ExpectNotNull((decompressed = (byte*)XMALLOC(cert_sz, heap,
  26692. DYNAMIC_TYPE_TMP_BUFFER)));
  26693. decompressedSz = (int)cert_sz;
  26694. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26695. if (pkcs7 != NULL) {
  26696. pkcs7->content = (byte*)cert_buf;
  26697. pkcs7->contentSz = (word32)cert_sz;
  26698. pkcs7->contentOID = DATA;
  26699. }
  26700. ExpectIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  26701. sizeof(out))), 0);
  26702. wc_PKCS7_Free(pkcs7);
  26703. pkcs7 = NULL;
  26704. /* compressed key should be smaller than when started */
  26705. ExpectIntLT(outSz, cert_sz);
  26706. /* test decompression */
  26707. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26708. ExpectIntEQ(pkcs7->contentOID, 0);
  26709. /* fail case with out buffer too small */
  26710. ExpectIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26711. decompressed, outSz), 0);
  26712. /* success case */
  26713. ExpectIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26714. decompressed, decompressedSz), cert_sz);
  26715. ExpectIntEQ(pkcs7->contentOID, DATA);
  26716. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26717. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26718. decompressed = NULL;
  26719. /* test decompression function with different 'max' inputs */
  26720. outSz = sizeof(out);
  26721. ExpectIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  26722. 0);
  26723. ExpectIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  26724. out, outSz, 0, heap), 0);
  26725. ExpectNull(decompressed);
  26726. ExpectIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  26727. out, outSz, 0, heap), 0);
  26728. ExpectNotNull(decompressed);
  26729. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26730. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26731. decompressed = NULL;
  26732. ExpectIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  26733. out, outSz, 0, heap), 0);
  26734. ExpectNotNull(decompressed);
  26735. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26736. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26737. if (cert_buf != NULL)
  26738. free(cert_buf);
  26739. wc_PKCS7_Free(pkcs7);
  26740. #endif
  26741. return EXPECT_RESULT();
  26742. }
  26743. static int test_wc_i2d_PKCS12(void)
  26744. {
  26745. EXPECT_DECLS;
  26746. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  26747. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26748. && !defined(NO_AES) && !defined(NO_SHA)
  26749. WC_PKCS12* pkcs12 = NULL;
  26750. unsigned char der[FOURK_BUF * 2];
  26751. unsigned char* pt;
  26752. int derSz = 0;
  26753. unsigned char out[FOURK_BUF * 2];
  26754. int outSz = FOURK_BUF * 2;
  26755. const char p12_f[] = "./certs/test-servercert.p12";
  26756. XFILE f = XBADFILE;
  26757. ExpectTrue((f = XFOPEN(p12_f, "rb")) != XBADFILE);
  26758. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  26759. if (f != XBADFILE)
  26760. XFCLOSE(f);
  26761. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  26762. ExpectIntEQ(wc_d2i_PKCS12(der, derSz, pkcs12), 0);
  26763. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26764. ExpectIntEQ(outSz, derSz);
  26765. outSz = derSz - 1;
  26766. pt = out;
  26767. ExpectIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  26768. outSz = derSz;
  26769. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  26770. ExpectIntEQ((pt == out), 0);
  26771. pt = NULL;
  26772. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  26773. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  26774. wc_PKCS12_free(pkcs12);
  26775. pkcs12 = NULL;
  26776. /* Run the same test but use wc_d2i_PKCS12_fp. */
  26777. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  26778. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26779. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26780. ExpectIntEQ(outSz, derSz);
  26781. wc_PKCS12_free(pkcs12);
  26782. pkcs12 = NULL;
  26783. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  26784. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26785. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26786. ExpectIntEQ(outSz, derSz);
  26787. wc_PKCS12_free(pkcs12);
  26788. pkcs12 = NULL;
  26789. #endif
  26790. return EXPECT_RESULT();
  26791. }
  26792. /* Testing wc_SignatureGetSize() for signature type ECC */
  26793. static int test_wc_SignatureGetSize_ecc(void)
  26794. {
  26795. EXPECT_DECLS;
  26796. #if !defined(NO_SIG_WRAPPER) && defined(HAVE_ECC) && !defined(NO_ECC256)
  26797. enum wc_SignatureType sig_type;
  26798. word32 key_len;
  26799. ecc_key ecc;
  26800. const char* qx =
  26801. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  26802. const char* qy =
  26803. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  26804. const char* d =
  26805. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  26806. XMEMSET(&ecc, 0, sizeof(ecc_key));
  26807. ExpectIntEQ(wc_ecc_init(&ecc), 0);
  26808. ExpectIntEQ(wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1"), 0);
  26809. /* Input for signature type ECC */
  26810. sig_type = WC_SIGNATURE_TYPE_ECC;
  26811. key_len = sizeof(ecc_key);
  26812. ExpectIntGT(wc_SignatureGetSize(sig_type, &ecc, key_len), 0);
  26813. /* Test bad args */
  26814. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26815. sig_type = (enum wc_SignatureType) 100;
  26816. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26817. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  26818. sig_type = WC_SIGNATURE_TYPE_ECC;
  26819. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0);
  26820. key_len = (word32)0;
  26821. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  26822. DoExpectIntEQ(wc_ecc_free(&ecc), 0);
  26823. #endif /* !NO_SIG_WRAPPER && HAVE_ECC && !NO_ECC256 */
  26824. return EXPECT_RESULT();
  26825. } /* END test_wc_SignatureGetSize_ecc() */
  26826. /* Testing wc_SignatureGetSize() for signature type rsa */
  26827. static int test_wc_SignatureGetSize_rsa(void)
  26828. {
  26829. EXPECT_DECLS;
  26830. #if !defined(NO_SIG_WRAPPER) && !defined(NO_RSA)
  26831. enum wc_SignatureType sig_type;
  26832. word32 key_len;
  26833. word32 idx = 0;
  26834. RsaKey rsa_key;
  26835. byte* tmp = NULL;
  26836. size_t bytes;
  26837. XMEMSET(&rsa_key, 0, sizeof(RsaKey));
  26838. #ifdef USE_CERT_BUFFERS_1024
  26839. bytes = (size_t)sizeof_client_key_der_1024;
  26840. if (bytes < (size_t)sizeof_client_key_der_1024)
  26841. bytes = (size_t)sizeof_client_cert_der_1024;
  26842. #elif defined(USE_CERT_BUFFERS_2048)
  26843. bytes = (size_t)sizeof_client_key_der_2048;
  26844. if (bytes < (size_t)sizeof_client_cert_der_2048)
  26845. bytes = (size_t)sizeof_client_cert_der_2048;
  26846. #else
  26847. bytes = FOURK_BUF;
  26848. #endif
  26849. ExpectNotNull(tmp = (byte*)XMALLOC(bytes, HEAP_HINT,
  26850. DYNAMIC_TYPE_TMP_BUFFER));
  26851. if (tmp != NULL) {
  26852. #ifdef USE_CERT_BUFFERS_1024
  26853. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  26854. #elif defined(USE_CERT_BUFFERS_2048)
  26855. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  26856. #elif !defined(NO_FILESYSTEM)
  26857. XFILE file = XBADFILE;
  26858. ExpectTrue((file = XFOPEN(clientKey, "rb")) != XBADFILE);
  26859. ExpectIntGT(bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file), 0);
  26860. if (file != XBADFILE)
  26861. XFCLOSE(file);
  26862. }
  26863. #else
  26864. ExpectFail();
  26865. #endif
  26866. }
  26867. ExpectIntEQ(wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId), 0);
  26868. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes), 0);
  26869. /* Input for signature type RSA */
  26870. sig_type = WC_SIGNATURE_TYPE_RSA;
  26871. key_len = sizeof(RsaKey);
  26872. ExpectIntGT(wc_SignatureGetSize(sig_type, &rsa_key, key_len), 0);
  26873. /* Test bad args */
  26874. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26875. sig_type = (enum wc_SignatureType)100;
  26876. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26877. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  26878. sig_type = WC_SIGNATURE_TYPE_RSA;
  26879. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), BAD_FUNC_ARG);
  26880. key_len = (word32)0;
  26881. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  26882. DoExpectIntEQ(wc_FreeRsaKey(&rsa_key), 0);
  26883. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26884. #endif /* !NO_SIG_WRAPPER && !NO_RSA */
  26885. return EXPECT_RESULT();
  26886. } /* END test_wc_SignatureGetSize_rsa(void) */
  26887. /*----------------------------------------------------------------------------*
  26888. | hash.h Tests
  26889. *----------------------------------------------------------------------------*/
  26890. static int test_wc_HashInit(void)
  26891. {
  26892. EXPECT_DECLS;
  26893. int i; /* 0 indicates tests passed, 1 indicates failure */
  26894. wc_HashAlg hash;
  26895. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26896. enum wc_HashType enumArray[] = {
  26897. #ifndef NO_MD5
  26898. WC_HASH_TYPE_MD5,
  26899. #endif
  26900. #ifndef NO_SHA
  26901. WC_HASH_TYPE_SHA,
  26902. #endif
  26903. #ifdef WOLFSSL_SHA224
  26904. WC_HASH_TYPE_SHA224,
  26905. #endif
  26906. #ifndef NO_SHA256
  26907. WC_HASH_TYPE_SHA256,
  26908. #endif
  26909. #ifdef WOLFSSL_SHA384
  26910. WC_HASH_TYPE_SHA384,
  26911. #endif
  26912. #ifdef WOLFSSL_SHA512
  26913. WC_HASH_TYPE_SHA512,
  26914. #endif
  26915. };
  26916. /* dynamically finds the length */
  26917. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26918. /* For loop to test various arguments... */
  26919. for (i = 0; i < enumlen; i++) {
  26920. /* check for bad args */
  26921. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  26922. wc_HashFree(&hash, enumArray[i]);
  26923. /* check for null ptr */
  26924. ExpectIntEQ(wc_HashInit(NULL, enumArray[i]), BAD_FUNC_ARG);
  26925. } /* end of for loop */
  26926. return EXPECT_RESULT();
  26927. } /* end of test_wc_HashInit */
  26928. /*
  26929. * Unit test function for wc_HashSetFlags()
  26930. */
  26931. static int test_wc_HashSetFlags(void)
  26932. {
  26933. EXPECT_DECLS;
  26934. #ifdef WOLFSSL_HASH_FLAGS
  26935. wc_HashAlg hash;
  26936. word32 flags = 0;
  26937. int i, j;
  26938. int notSupportedLen;
  26939. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26940. enum wc_HashType enumArray[] = {
  26941. #ifndef NO_MD5
  26942. WC_HASH_TYPE_MD5,
  26943. #endif
  26944. #ifndef NO_SHA
  26945. WC_HASH_TYPE_SHA,
  26946. #endif
  26947. #ifdef WOLFSSL_SHA224
  26948. WC_HASH_TYPE_SHA224,
  26949. #endif
  26950. #ifndef NO_SHA256
  26951. WC_HASH_TYPE_SHA256,
  26952. #endif
  26953. #ifdef WOLFSSL_SHA384
  26954. WC_HASH_TYPE_SHA384,
  26955. #endif
  26956. #ifdef WOLFSSL_SHA512
  26957. WC_HASH_TYPE_SHA512,
  26958. #endif
  26959. #ifdef WOLFSSL_SHA3
  26960. WC_HASH_TYPE_SHA3_224,
  26961. #endif
  26962. };
  26963. enum wc_HashType notSupported[] = {
  26964. WC_HASH_TYPE_MD5_SHA,
  26965. WC_HASH_TYPE_MD2,
  26966. WC_HASH_TYPE_MD4,
  26967. WC_HASH_TYPE_BLAKE2B,
  26968. WC_HASH_TYPE_BLAKE2S,
  26969. WC_HASH_TYPE_NONE,
  26970. };
  26971. /* dynamically finds the length */
  26972. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26973. /* For loop to test various arguments... */
  26974. for (i = 0; i < enumlen; i++) {
  26975. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  26976. ExpectIntEQ(wc_HashSetFlags(&hash, enumArray[i], flags), 0);
  26977. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  26978. ExpectIntEQ(wc_HashSetFlags(NULL, enumArray[i], flags), BAD_FUNC_ARG);
  26979. wc_HashFree(&hash, enumArray[i]);
  26980. }
  26981. /* For loop to test not supported cases */
  26982. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  26983. for (j = 0; j < notSupportedLen; j++) {
  26984. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  26985. ExpectIntEQ(wc_HashSetFlags(&hash, notSupported[j], flags),
  26986. BAD_FUNC_ARG);
  26987. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  26988. }
  26989. #endif
  26990. return EXPECT_RESULT();
  26991. } /* END test_wc_HashSetFlags */
  26992. /*
  26993. * Unit test function for wc_HashGetFlags()
  26994. */
  26995. static int test_wc_HashGetFlags(void)
  26996. {
  26997. EXPECT_DECLS;
  26998. #ifdef WOLFSSL_HASH_FLAGS
  26999. wc_HashAlg hash;
  27000. word32 flags = 0;
  27001. int i, j;
  27002. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  27003. enum wc_HashType enumArray[] = {
  27004. #ifndef NO_MD5
  27005. WC_HASH_TYPE_MD5,
  27006. #endif
  27007. #ifndef NO_SHA
  27008. WC_HASH_TYPE_SHA,
  27009. #endif
  27010. #ifdef WOLFSSL_SHA224
  27011. WC_HASH_TYPE_SHA224,
  27012. #endif
  27013. #ifndef NO_SHA256
  27014. WC_HASH_TYPE_SHA256,
  27015. #endif
  27016. #ifdef WOLFSSL_SHA384
  27017. WC_HASH_TYPE_SHA384,
  27018. #endif
  27019. #ifdef WOLFSSL_SHA512
  27020. WC_HASH_TYPE_SHA512,
  27021. #endif
  27022. #ifdef WOLFSSL_SHA3
  27023. WC_HASH_TYPE_SHA3_224,
  27024. #endif
  27025. };
  27026. enum wc_HashType notSupported[] = {
  27027. WC_HASH_TYPE_MD5_SHA,
  27028. WC_HASH_TYPE_MD2,
  27029. WC_HASH_TYPE_MD4,
  27030. WC_HASH_TYPE_BLAKE2B,
  27031. WC_HASH_TYPE_BLAKE2S,
  27032. WC_HASH_TYPE_NONE,
  27033. };
  27034. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  27035. int notSupportedLen;
  27036. /* For loop to test various arguments... */
  27037. for (i = 0; i < enumlen; i++) {
  27038. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  27039. ExpectIntEQ(wc_HashGetFlags(&hash, enumArray[i], &flags), 0);
  27040. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  27041. ExpectIntEQ(wc_HashGetFlags(NULL, enumArray[i], &flags), BAD_FUNC_ARG);
  27042. wc_HashFree(&hash, enumArray[i]);
  27043. }
  27044. /* For loop to test not supported cases */
  27045. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  27046. for (j = 0; j < notSupportedLen; j++) {
  27047. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  27048. ExpectIntEQ(wc_HashGetFlags(&hash, notSupported[j], &flags),
  27049. BAD_FUNC_ARG);
  27050. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  27051. }
  27052. #endif
  27053. return EXPECT_RESULT();
  27054. } /* END test_wc_HashGetFlags */
  27055. /*----------------------------------------------------------------------------*
  27056. | Compatibility Tests
  27057. *----------------------------------------------------------------------------*/
  27058. /*----------------------------------------------------------------------------*
  27059. | ASN.1 Tests
  27060. *----------------------------------------------------------------------------*/
  27061. static int test_wolfSSL_ASN1_BIT_STRING(void)
  27062. {
  27063. EXPECT_DECLS;
  27064. #if !defined(NO_CERTS) && defined(OPENSSL_ALL)
  27065. ASN1_BIT_STRING* str = NULL;
  27066. ExpectNotNull(str = ASN1_BIT_STRING_new());
  27067. /* Empty data testing. */
  27068. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 1), 0);
  27069. ASN1_BIT_STRING_free(str);
  27070. str = NULL;
  27071. ExpectNotNull(str = ASN1_BIT_STRING_new());
  27072. /* Invalid parameter testing. */
  27073. ExpectIntEQ(ASN1_BIT_STRING_set_bit(NULL, 42, 1), 0);
  27074. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, -1, 1), 0);
  27075. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 2), 0);
  27076. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, -1), 0);
  27077. /* No bit string - bit is always 0. */
  27078. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, 42), 0);
  27079. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, -1), 0);
  27080. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  27081. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 0), 0);
  27082. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  27083. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  27084. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  27085. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  27086. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  27087. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  27088. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  27089. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 91, 0), 1);
  27090. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 91), 0);
  27091. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 89, 0), 1);
  27092. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 89), 0);
  27093. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 0), 1);
  27094. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 0);
  27095. ASN1_BIT_STRING_free(str);
  27096. ASN1_BIT_STRING_free(NULL);
  27097. #endif
  27098. return EXPECT_RESULT();
  27099. }
  27100. static int test_wolfSSL_ASN1_INTEGER(void)
  27101. {
  27102. EXPECT_DECLS;
  27103. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27104. ASN1_INTEGER* a = NULL;
  27105. ASN1_INTEGER* dup = NULL;
  27106. const unsigned char invalidLenDer[] = {
  27107. 0x02, 0x20, 0x00
  27108. };
  27109. const unsigned char longDer[] = {
  27110. 0x02, 0x20,
  27111. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27112. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27113. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27114. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
  27115. };
  27116. const unsigned char* p;
  27117. /* Invalid parameter testing. */
  27118. ASN1_INTEGER_free(NULL);
  27119. ExpectNull(wolfSSL_ASN1_INTEGER_dup(NULL));
  27120. ExpectNotNull(a = ASN1_INTEGER_new());
  27121. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  27122. ASN1_INTEGER_free(dup);
  27123. dup = NULL;
  27124. ASN1_INTEGER_free(a);
  27125. a = NULL;
  27126. p = longDer;
  27127. ExpectNull(d2i_ASN1_INTEGER(NULL, &p, sizeof(invalidLenDer)));
  27128. p = longDer;
  27129. ExpectNotNull(a = d2i_ASN1_INTEGER(NULL, &p, sizeof(longDer)));
  27130. ExpectPtrNE(p, longDer);
  27131. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  27132. ASN1_INTEGER_free(dup);
  27133. ASN1_INTEGER_free(a);
  27134. #endif
  27135. return EXPECT_RESULT();
  27136. }
  27137. static int test_wolfSSL_ASN1_INTEGER_cmp(void)
  27138. {
  27139. EXPECT_DECLS;
  27140. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27141. ASN1_INTEGER* a = NULL;
  27142. ASN1_INTEGER* b = NULL;
  27143. ExpectNotNull(a = ASN1_INTEGER_new());
  27144. ExpectNotNull(b = ASN1_INTEGER_new());
  27145. ExpectIntEQ(ASN1_INTEGER_set(a, 1), 1);
  27146. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  27147. /* Invalid parameter testing. */
  27148. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, NULL), -1);
  27149. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, NULL), -1);
  27150. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, b), -1);
  27151. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27152. ExpectIntEQ(ASN1_INTEGER_set(b, -1), 1);
  27153. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27154. ExpectIntEQ(ASN1_INTEGER_set(a, -2), 1);
  27155. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27156. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  27157. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27158. ExpectIntEQ(ASN1_INTEGER_set(a, 0x01), 1);
  27159. ExpectIntEQ(ASN1_INTEGER_set(b, 0x1000), 1);
  27160. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27161. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(b, a), 0);
  27162. ASN1_INTEGER_free(b);
  27163. ASN1_INTEGER_free(a);
  27164. #endif
  27165. return EXPECT_RESULT();
  27166. }
  27167. static int test_wolfSSL_ASN1_INTEGER_BN(void)
  27168. {
  27169. EXPECT_DECLS;
  27170. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27171. ASN1_INTEGER* ai = NULL;
  27172. ASN1_INTEGER* ai2 = NULL;
  27173. BIGNUM* bn = NULL;
  27174. BIGNUM* bn2 = NULL;
  27175. ExpectNotNull(ai = ASN1_INTEGER_new());
  27176. ExpectNotNull(bn2 = BN_new());
  27177. /* Invalid parameter testing. */
  27178. ExpectNull(bn = ASN1_INTEGER_to_BN(NULL, NULL));
  27179. ExpectNull(ai2 = BN_to_ASN1_INTEGER(NULL, NULL));
  27180. /* at the moment hard setting since no set function */
  27181. if (ai != NULL) {
  27182. ai->data[0] = 0xff; /* No DER encoding. */
  27183. ai->length = 1;
  27184. }
  27185. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27186. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27187. BN_free(bn);
  27188. bn = NULL;
  27189. #else
  27190. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  27191. #endif
  27192. if (ai != NULL) {
  27193. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27194. ai->data[1] = 0x04; /* bad length of integer */
  27195. ai->data[2] = 0x03;
  27196. ai->length = 3;
  27197. }
  27198. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27199. /* Interpreted as a number 0x020403. */
  27200. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27201. BN_free(bn);
  27202. bn = NULL;
  27203. #else
  27204. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  27205. #endif
  27206. if (ai != NULL) {
  27207. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27208. ai->data[1] = 0x01; /* length of integer */
  27209. ai->data[2] = 0x03;
  27210. ai->length = 3;
  27211. }
  27212. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27213. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, NULL));
  27214. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27215. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27216. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27217. if (ai != NULL) {
  27218. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27219. ai->data[1] = 0x02; /* length of integer */
  27220. ai->data[2] = 0x00; /* padding byte to ensure positive */
  27221. ai->data[3] = 0xff;
  27222. ai->length = 4;
  27223. }
  27224. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  27225. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  27226. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27227. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27228. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27229. if (ai != NULL) {
  27230. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27231. ai->data[1] = 0x01; /* length of integer */
  27232. ai->data[2] = 0x00;
  27233. ai->length = 3;
  27234. }
  27235. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  27236. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  27237. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27238. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27239. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27240. if (ai != NULL) {
  27241. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27242. ai->data[1] = 0x01; /* length of integer */
  27243. ai->data[2] = 0x01;
  27244. ai->length = 3;
  27245. ai->negative = 1;
  27246. }
  27247. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  27248. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  27249. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27250. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27251. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27252. BN_free(bn2);
  27253. BN_free(bn);
  27254. ASN1_INTEGER_free(ai2);
  27255. ASN1_INTEGER_free(ai);
  27256. #endif
  27257. return EXPECT_RESULT();
  27258. }
  27259. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  27260. {
  27261. EXPECT_DECLS;
  27262. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27263. ASN1_INTEGER *a = NULL;
  27264. long val;
  27265. ExpectNotNull(a = ASN1_INTEGER_new());
  27266. /* Invalid parameter testing. */
  27267. ExpectIntEQ(ASN1_INTEGER_get(NULL), 0);
  27268. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27269. ExpectIntEQ(ASN1_INTEGER_get(a), 0);
  27270. #else
  27271. ExpectIntEQ(ASN1_INTEGER_get(a), -1);
  27272. #endif
  27273. ASN1_INTEGER_free(a);
  27274. a = NULL;
  27275. ExpectNotNull(a = ASN1_INTEGER_new());
  27276. val = 0;
  27277. ExpectIntEQ(ASN1_INTEGER_set(NULL, val), 0);
  27278. ASN1_INTEGER_free(a);
  27279. a = NULL;
  27280. /* 0 */
  27281. ExpectNotNull(a = ASN1_INTEGER_new());
  27282. val = 0;
  27283. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27284. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27285. ASN1_INTEGER_free(a);
  27286. a = NULL;
  27287. /* 40 */
  27288. ExpectNotNull(a = ASN1_INTEGER_new());
  27289. val = 40;
  27290. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27291. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27292. ASN1_INTEGER_free(a);
  27293. a = NULL;
  27294. /* -40 */
  27295. ExpectNotNull(a = ASN1_INTEGER_new());
  27296. val = -40;
  27297. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27298. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27299. ASN1_INTEGER_free(a);
  27300. a = NULL;
  27301. /* 128 */
  27302. ExpectNotNull(a = ASN1_INTEGER_new());
  27303. val = 128;
  27304. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27305. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27306. ASN1_INTEGER_free(a);
  27307. a = NULL;
  27308. /* -128 */
  27309. ExpectNotNull(a = ASN1_INTEGER_new());
  27310. val = -128;
  27311. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27312. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27313. ASN1_INTEGER_free(a);
  27314. a = NULL;
  27315. /* 200 */
  27316. ExpectNotNull(a = ASN1_INTEGER_new());
  27317. val = 200;
  27318. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27319. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27320. ASN1_INTEGER_free(a);
  27321. a = NULL;
  27322. /* int max (2147483647) */
  27323. ExpectNotNull(a = ASN1_INTEGER_new());
  27324. val = 2147483647;
  27325. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27326. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27327. ASN1_INTEGER_free(a);
  27328. a = NULL;
  27329. /* int min (-2147483648) */
  27330. ExpectNotNull(a = ASN1_INTEGER_new());
  27331. val = -2147483647 - 1;
  27332. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27333. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27334. ASN1_INTEGER_free(a);
  27335. a = NULL;
  27336. /* long max positive */
  27337. ExpectNotNull(a = ASN1_INTEGER_new());
  27338. val = (long)(((unsigned long)-1) >> 1);
  27339. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27340. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27341. ASN1_INTEGER_free(a);
  27342. #endif
  27343. return EXPECT_RESULT();
  27344. }
  27345. #if defined(OPENSSL_EXTRA)
  27346. typedef struct ASN1IntTestVector {
  27347. const byte* der;
  27348. const size_t derSz;
  27349. const long value;
  27350. } ASN1IntTestVector;
  27351. #endif
  27352. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  27353. {
  27354. EXPECT_DECLS;
  27355. #if defined(OPENSSL_EXTRA)
  27356. size_t i;
  27357. WOLFSSL_ASN1_INTEGER* a = NULL;
  27358. WOLFSSL_ASN1_INTEGER* b = NULL;
  27359. WOLFSSL_ASN1_INTEGER* c = NULL;
  27360. const byte* p = NULL;
  27361. byte* p2 = NULL;
  27362. byte* reEncoded = NULL;
  27363. int reEncodedSz = 0;
  27364. static const byte zeroDer[] = {
  27365. 0x02, 0x01, 0x00
  27366. };
  27367. static const byte oneDer[] = {
  27368. 0x02, 0x01, 0x01
  27369. };
  27370. static const byte negativeDer[] = {
  27371. 0x02, 0x03, 0xC1, 0x16, 0x0D
  27372. };
  27373. static const byte positiveDer[] = {
  27374. 0x02, 0x03, 0x01, 0x00, 0x01
  27375. };
  27376. static const byte primeDer[] = {
  27377. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  27378. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  27379. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  27380. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  27381. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  27382. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  27383. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  27384. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  27385. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  27386. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  27387. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  27388. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  27389. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  27390. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  27391. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  27392. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  27393. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  27394. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  27395. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  27396. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  27397. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  27398. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  27399. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  27400. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  27401. };
  27402. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  27403. static const ASN1IntTestVector testVectors[] = {
  27404. {zeroDer, sizeof(zeroDer), 0},
  27405. {oneDer, sizeof(oneDer), 1},
  27406. {negativeDer, sizeof(negativeDer), -4123123},
  27407. {positiveDer, sizeof(positiveDer), 65537},
  27408. {primeDer, sizeof(primeDer), 0}
  27409. };
  27410. static const size_t NUM_TEST_VECTORS =
  27411. sizeof(testVectors)/sizeof(testVectors[0]);
  27412. /* Check d2i error conditions */
  27413. /* NULL pointer to input. */
  27414. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  27415. ExpectNull(b);
  27416. /* NULL input. */
  27417. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  27418. ExpectNull(b);
  27419. /* 0 length. */
  27420. p = testVectors[0].der;
  27421. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  27422. ExpectNull(b);
  27423. /* Negative length. */
  27424. p = testVectors[0].der;
  27425. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  27426. ExpectNull(b);
  27427. /* Garbage DER input. */
  27428. p = garbageDer;
  27429. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  27430. ExpectNull(b);
  27431. /* Check i2d error conditions */
  27432. /* NULL input. */
  27433. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  27434. /* 0 length input data buffer (a->length == 0). */
  27435. ExpectNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  27436. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  27437. if (a != NULL)
  27438. a->data = NULL;
  27439. /* NULL input data buffer. */
  27440. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  27441. if (a != NULL) {
  27442. /* Reset a->data. */
  27443. a->data = a->intData;
  27444. }
  27445. /* Set a to valid value. */
  27446. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  27447. /* NULL output buffer. */
  27448. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  27449. wolfSSL_ASN1_INTEGER_free(a);
  27450. a = NULL;
  27451. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  27452. p = testVectors[i].der;
  27453. ExpectNotNull(a = wolfSSL_d2i_ASN1_INTEGER(&b, &p,
  27454. testVectors[i].derSz));
  27455. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27456. if (testVectors[i].derSz <= sizeof(long)) {
  27457. ExpectNotNull(c = wolfSSL_ASN1_INTEGER_new());
  27458. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value), 1);
  27459. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  27460. wolfSSL_ASN1_INTEGER_free(c);
  27461. c = NULL;
  27462. }
  27463. /* Convert to DER without a pre-allocated output buffer. */
  27464. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  27465. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  27466. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  27467. /* Convert to DER with a pre-allocated output buffer. In this case, the
  27468. * output buffer pointer should be incremented just past the end of the
  27469. * encoded data. */
  27470. p2 = reEncoded;
  27471. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &p2)), 0);
  27472. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  27473. ExpectPtrEq(reEncoded, p2 - reEncodedSz);
  27474. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  27475. XFREE(reEncoded, NULL, DYNAMIC_TYPE_ASN1);
  27476. reEncoded = NULL;
  27477. wolfSSL_ASN1_INTEGER_free(a);
  27478. a = NULL;
  27479. }
  27480. #endif /* OPENSSL_EXTRA */
  27481. return EXPECT_RESULT();
  27482. }
  27483. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  27484. {
  27485. EXPECT_DECLS;
  27486. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  27487. BIO* bio = NULL;
  27488. BIO* out = NULL;
  27489. BIO* fixed = NULL;
  27490. ASN1_INTEGER* ai = NULL;
  27491. char buf[] = "123456\n12345\n1123456789123456\\\n78901234567890 \r\n\n";
  27492. char tmp[1024];
  27493. int tmpSz;
  27494. const char expected1[] = "123456";
  27495. const char expected2[] = "112345678912345678901234567890";
  27496. char longStr[] = "123456781234567812345678123456781234567812345678\n"
  27497. "123456781234567812345678123456781234567812345678\\\n12345678\n";
  27498. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  27499. ExpectNotNull(ai = ASN1_INTEGER_new());
  27500. ExpectNotNull(bio = BIO_new_mem_buf(buf, -1));
  27501. /* Invalid parameter testing. */
  27502. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, -1), 0);
  27503. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, NULL, -1), 0);
  27504. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, NULL, -1), 0);
  27505. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, tmp, -1), 0);
  27506. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, 1024), 0);
  27507. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, tmp, 1024), 0);
  27508. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, tmp, 1024), 0);
  27509. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, NULL, 1024), 0);
  27510. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, -1), 0);
  27511. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, NULL), 0);
  27512. ExpectIntEQ(i2a_ASN1_INTEGER(bio, NULL), 0);
  27513. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, ai), 0);
  27514. /* No data to read from BIO. */
  27515. ExpectIntEQ(a2i_ASN1_INTEGER(out, ai, tmp, 1024), 0);
  27516. /* read first line */
  27517. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27518. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  27519. XMEMSET(tmp, 0, 1024);
  27520. tmpSz = BIO_read(out, tmp, 1024);
  27521. ExpectIntEQ(tmpSz, 6);
  27522. ExpectIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  27523. /* fail on second line (not % 2) */
  27524. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  27525. /* read 3rd long line */
  27526. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27527. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  27528. XMEMSET(tmp, 0, 1024);
  27529. tmpSz = BIO_read(out, tmp, 1024);
  27530. ExpectIntEQ(tmpSz, 30);
  27531. ExpectIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  27532. /* fail on empty line */
  27533. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  27534. BIO_free(bio);
  27535. bio = NULL;
  27536. /* Make long integer, requiring dynamic memory, even longer. */
  27537. ExpectNotNull(bio = BIO_new_mem_buf(longStr, -1));
  27538. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27539. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 48);
  27540. XMEMSET(tmp, 0, 1024);
  27541. tmpSz = BIO_read(out, tmp, 1024);
  27542. ExpectIntEQ(tmpSz, 48);
  27543. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27544. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 56);
  27545. XMEMSET(tmp, 0, 1024);
  27546. tmpSz = BIO_read(out, tmp, 1024);
  27547. ExpectIntEQ(tmpSz, 56);
  27548. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(ai, 1), 1);
  27549. BIO_free(bio);
  27550. BIO_free(out);
  27551. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  27552. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27553. /* Ensure there is 0 bytes available to write into. */
  27554. ExpectIntEQ(BIO_write(fixed, tmp, 1), 1);
  27555. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  27556. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27557. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  27558. BIO_free(fixed);
  27559. ASN1_INTEGER_free(ai);
  27560. #endif
  27561. return EXPECT_RESULT();
  27562. }
  27563. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  27564. {
  27565. EXPECT_DECLS;
  27566. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27567. ASN1_INTEGER *a = NULL;
  27568. unsigned char *pp = NULL,*tpp = NULL;
  27569. int ret = 0;
  27570. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  27571. /* Invalid parameter testing. */
  27572. /* Set pp to an invalid value. */
  27573. pp = NULL;
  27574. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, &pp), 0);
  27575. ExpectIntEQ(i2c_ASN1_INTEGER(a, &pp), 0);
  27576. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, NULL), 0);
  27577. /* 40 */
  27578. if (a != NULL) {
  27579. a->intData[0] = ASN_INTEGER;
  27580. a->intData[1] = 1;
  27581. a->intData[2] = 40;
  27582. }
  27583. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27584. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27585. DYNAMIC_TYPE_TMP_BUFFER));
  27586. tpp = pp;
  27587. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27588. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27589. tpp--;
  27590. ExpectIntEQ(*tpp, 40);
  27591. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27592. pp = NULL;
  27593. /* 128 */
  27594. if (a != NULL) {
  27595. a->intData[0] = ASN_INTEGER;
  27596. a->intData[1] = 1;
  27597. a->intData[2] = 128;
  27598. }
  27599. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27600. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27601. DYNAMIC_TYPE_TMP_BUFFER));
  27602. tpp = pp;
  27603. if (tpp != NULL) {
  27604. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27605. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27606. tpp--;
  27607. ExpectIntEQ(*(tpp--), 128);
  27608. ExpectIntEQ(*tpp, 0);
  27609. }
  27610. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27611. pp = NULL;
  27612. /* -40 */
  27613. if (a != NULL) {
  27614. a->intData[0] = ASN_INTEGER;
  27615. a->intData[1] = 1;
  27616. a->intData[2] = 40;
  27617. a->negative = 1;
  27618. }
  27619. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27620. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27621. DYNAMIC_TYPE_TMP_BUFFER));
  27622. tpp = pp;
  27623. if (tpp != NULL) {
  27624. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27625. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27626. tpp--;
  27627. ExpectIntEQ(*tpp, 216);
  27628. }
  27629. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27630. pp = NULL;
  27631. /* -128 */
  27632. if (a != NULL) {
  27633. a->intData[0] = ASN_INTEGER;
  27634. a->intData[1] = 1;
  27635. a->intData[2] = 128;
  27636. a->negative = 1;
  27637. }
  27638. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27639. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27640. DYNAMIC_TYPE_TMP_BUFFER));
  27641. tpp = pp;
  27642. if (tpp != NULL) {
  27643. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27644. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27645. tpp--;
  27646. ExpectIntEQ(*tpp, 128);
  27647. }
  27648. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27649. pp = NULL;
  27650. /* -200 */
  27651. if (a != NULL) {
  27652. a->intData[0] = ASN_INTEGER;
  27653. a->intData[1] = 1;
  27654. a->intData[2] = 200;
  27655. a->negative = 1;
  27656. }
  27657. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27658. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27659. DYNAMIC_TYPE_TMP_BUFFER));
  27660. tpp = pp;
  27661. if (tpp != NULL) {
  27662. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27663. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27664. tpp--;
  27665. ExpectIntEQ(*(tpp--), 56);
  27666. ExpectIntEQ(*tpp, 255);
  27667. }
  27668. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27669. pp = NULL;
  27670. /* Empty */
  27671. if (a != NULL) {
  27672. a->intData[0] = ASN_INTEGER;
  27673. a->intData[1] = 0;
  27674. a->negative = 0;
  27675. }
  27676. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27677. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27678. DYNAMIC_TYPE_TMP_BUFFER));
  27679. tpp = pp;
  27680. if (tpp != NULL) {
  27681. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27682. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27683. tpp--;
  27684. ExpectIntEQ(*tpp, 0);
  27685. }
  27686. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27687. pp = NULL;
  27688. /* 0 */
  27689. if (a != NULL) {
  27690. a->intData[0] = ASN_INTEGER;
  27691. a->intData[1] = 1;
  27692. a->intData[2] = 0;
  27693. a->negative = 1;
  27694. }
  27695. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27696. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27697. DYNAMIC_TYPE_TMP_BUFFER));
  27698. if (tpp != NULL) {
  27699. tpp = pp;
  27700. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27701. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27702. tpp--;
  27703. ExpectIntEQ(*tpp, 0);
  27704. }
  27705. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27706. pp = NULL;
  27707. /* 0x100 */
  27708. if (a != NULL) {
  27709. a->intData[0] = ASN_INTEGER;
  27710. a->intData[1] = 2;
  27711. a->intData[2] = 0x01;
  27712. a->intData[3] = 0x00;
  27713. a->negative = 0;
  27714. }
  27715. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27716. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27717. DYNAMIC_TYPE_TMP_BUFFER));
  27718. if (tpp != NULL) {
  27719. tpp = pp;
  27720. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27721. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27722. tpp -= 2;
  27723. ExpectIntEQ(tpp[0], 0x01);
  27724. ExpectIntEQ(tpp[1], 0x00);
  27725. }
  27726. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27727. pp = NULL;
  27728. /* -0x8000 => 0x8000 */
  27729. if (a != NULL) {
  27730. a->intData[0] = ASN_INTEGER;
  27731. a->intData[1] = 2;
  27732. a->intData[2] = 0x80;
  27733. a->intData[3] = 0x00;
  27734. a->negative = 1;
  27735. }
  27736. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27737. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27738. DYNAMIC_TYPE_TMP_BUFFER));
  27739. tpp = pp;
  27740. if (tpp != NULL) {
  27741. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27742. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27743. tpp -= 2;
  27744. ExpectIntEQ(tpp[0], 0x80);
  27745. ExpectIntEQ(tpp[1], 0x00);
  27746. }
  27747. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27748. pp = NULL;
  27749. /* -0x8001 => 0xFF7FFF */
  27750. if (a != NULL) {
  27751. a->intData[0] = ASN_INTEGER;
  27752. a->intData[1] = 2;
  27753. a->intData[2] = 0x80;
  27754. a->intData[3] = 0x01;
  27755. a->negative = 1;
  27756. }
  27757. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 3);
  27758. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27759. DYNAMIC_TYPE_TMP_BUFFER));
  27760. tpp = pp;
  27761. if (tpp != NULL) {
  27762. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27763. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 3);
  27764. tpp -= 3;
  27765. ExpectIntEQ(tpp[0], 0xFF);
  27766. ExpectIntEQ(tpp[1], 0x7F);
  27767. ExpectIntEQ(tpp[2], 0xFF);
  27768. }
  27769. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27770. wolfSSL_ASN1_INTEGER_free(a);
  27771. #endif /* OPENSSL_EXTRA && !NO_ASN */
  27772. return EXPECT_RESULT();
  27773. }
  27774. static int test_wolfSSL_ASN1_OBJECT(void)
  27775. {
  27776. EXPECT_DECLS;
  27777. #if defined(OPENSSL_EXTRA)
  27778. ASN1_OBJECT* a = NULL;
  27779. ASN1_OBJECT s;
  27780. const unsigned char der[] = { 0x06, 0x01, 0x00 };
  27781. /* Invalid parameter testing. */
  27782. ASN1_OBJECT_free(NULL);
  27783. ExpectNull(wolfSSL_ASN1_OBJECT_dup(NULL));
  27784. /* Test that a static ASN1_OBJECT can be freed. */
  27785. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  27786. ASN1_OBJECT_free(&s);
  27787. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  27788. ASN1_OBJECT_free(a);
  27789. a = NULL;
  27790. s.obj = der;
  27791. s.objSz = sizeof(der);
  27792. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  27793. ASN1_OBJECT_free(a);
  27794. ASN1_OBJECT_free(&s);
  27795. #endif /* OPENSSL_EXTRA */
  27796. return EXPECT_RESULT();
  27797. }
  27798. static int test_wolfSSL_ASN1_get_object(void)
  27799. {
  27800. EXPECT_DECLS;
  27801. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  27802. const unsigned char* derBuf = cliecc_cert_der_256;
  27803. const unsigned char* nullPtr = NULL;
  27804. const unsigned char objDerInvalidLen[] = { 0x30, 0x81 };
  27805. const unsigned char objDerBadLen[] = { 0x30, 0x04 };
  27806. const unsigned char objDerNotObj[] = { 0x02, 0x01, 0x00 };
  27807. const unsigned char objDerNoData[] = { 0x06, 0x00 };
  27808. const unsigned char* p;
  27809. unsigned char objDer[8];
  27810. unsigned char* der;
  27811. unsigned char* derPtr;
  27812. int len = sizeof_cliecc_cert_der_256;
  27813. long asnLen = 0;
  27814. int tag = 0;
  27815. int cls = 0;
  27816. ASN1_OBJECT* a = NULL;
  27817. ASN1_OBJECT s;
  27818. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  27819. /* Invalid encoding at length. */
  27820. p = objDerInvalidLen;
  27821. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  27822. 0x80);
  27823. p = objDerBadLen;
  27824. /* Error = 0x80, Constructed = 0x20 */
  27825. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  27826. 0x80 | 0x20);
  27827. /* Read a couple TLV triplets and make sure they match the expected values
  27828. */
  27829. /* SEQUENCE */
  27830. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  27831. ExpectIntEQ(asnLen, 861);
  27832. ExpectIntEQ(tag, 0x10);
  27833. ExpectIntEQ(cls, 0);
  27834. /* SEQUENCE */
  27835. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27836. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27837. ExpectIntEQ(asnLen, 772);
  27838. ExpectIntEQ(tag, 0x10);
  27839. ExpectIntEQ(cls, 0);
  27840. /* [0] */
  27841. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27842. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27843. ExpectIntEQ(asnLen, 3);
  27844. ExpectIntEQ(tag, 0);
  27845. ExpectIntEQ(cls, 0x80);
  27846. /* INTEGER */
  27847. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27848. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27849. ExpectIntEQ(asnLen, 1);
  27850. ExpectIntEQ(tag, 0x2);
  27851. ExpectIntEQ(cls, 0);
  27852. derBuf += asnLen;
  27853. /* INTEGER */
  27854. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27855. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27856. ExpectIntEQ(asnLen, 20);
  27857. ExpectIntEQ(tag, 0x2);
  27858. ExpectIntEQ(cls, 0);
  27859. derBuf += asnLen;
  27860. /* SEQUENCE */
  27861. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27862. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27863. ExpectIntEQ(asnLen, 10);
  27864. ExpectIntEQ(tag, 0x10);
  27865. ExpectIntEQ(cls, 0);
  27866. /* Found OBJECT_ID. */
  27867. /* Invalid parameter testing. */
  27868. ExpectIntEQ(ASN1_get_object(NULL, NULL, NULL, NULL, 0), 0x80);
  27869. ExpectIntEQ(ASN1_get_object(&nullPtr, NULL, NULL, NULL, 0), 0x80);
  27870. ExpectIntEQ(ASN1_get_object(NULL, &asnLen, &tag, &cls, len), 0x80);
  27871. ExpectIntEQ(ASN1_get_object(&nullPtr, &asnLen, &tag, &cls, len), 0x80);
  27872. ExpectIntEQ(ASN1_get_object(&derBuf, NULL, &tag, &cls, len), 0x80);
  27873. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, NULL, &cls, len), 0x80);
  27874. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, NULL, len), 0x80);
  27875. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, 0), 0x80);
  27876. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, -1), 0x80);
  27877. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, -1));
  27878. ExpectNull(d2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  27879. ExpectNull(d2i_ASN1_OBJECT(NULL, &derBuf, -1));
  27880. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, 0));
  27881. ExpectNull(d2i_ASN1_OBJECT(&a, NULL, len));
  27882. ExpectNull(d2i_ASN1_OBJECT(&a, &nullPtr, len));
  27883. ExpectNull(d2i_ASN1_OBJECT(&a, &derBuf, -1));
  27884. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, -1));
  27885. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  27886. ExpectNull(c2i_ASN1_OBJECT(NULL, &derBuf, -1));
  27887. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, 1));
  27888. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, 1));
  27889. /* Invalid encoding at length. */
  27890. p = objDerInvalidLen;
  27891. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerInvalidLen)));
  27892. p = objDerBadLen;
  27893. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerBadLen)));
  27894. p = objDerNotObj;
  27895. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNotObj)));
  27896. p = objDerNoData;
  27897. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNoData)));
  27898. /* Create an ASN OBJECT from content */
  27899. p = derBuf + 2;
  27900. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 8));
  27901. ASN1_OBJECT_free(a);
  27902. a = NULL;
  27903. /* Create an ASN OBJECT from DER */
  27904. ExpectNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  27905. /* Invalid parameter testing. */
  27906. ExpectIntEQ(i2d_ASN1_OBJECT(NULL, NULL), 0);
  27907. ExpectIntEQ(i2d_ASN1_OBJECT(&s, NULL), 0);
  27908. ExpectIntEQ(i2d_ASN1_OBJECT(a, NULL), 8);
  27909. der = NULL;
  27910. ExpectIntEQ(i2d_ASN1_OBJECT(a, &der), 8);
  27911. derPtr = objDer;
  27912. ExpectIntEQ(i2d_ASN1_OBJECT(a, &derPtr), 8);
  27913. ExpectPtrNE(derPtr, objDer);
  27914. ExpectIntEQ(XMEMCMP(der, objDer, 8), 0);
  27915. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  27916. ASN1_OBJECT_free(a);
  27917. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  27918. return EXPECT_RESULT();
  27919. }
  27920. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  27921. {
  27922. EXPECT_DECLS;
  27923. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  27924. ASN1_OBJECT* obj = NULL;
  27925. ASN1_OBJECT* a = NULL;
  27926. BIO *bio = NULL;
  27927. const unsigned char notObjDer[] = { 0x04, 0x01, 0xff };
  27928. const unsigned char badLenDer[] = { 0x06, 0x04, 0x01 };
  27929. const unsigned char goodDer[] = { 0x06, 0x01, 0x01 };
  27930. const unsigned char* p;
  27931. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  27932. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  27933. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  27934. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  27935. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  27936. /* No DER encoding in ASN1_OBJECT. */
  27937. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_new());
  27938. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  27939. ASN1_OBJECT_free(a);
  27940. a = NULL;
  27941. /* DER encoding - not OBJECT_ID */
  27942. p = notObjDer;
  27943. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  27944. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  27945. ASN1_OBJECT_free(a);
  27946. a = NULL;
  27947. /* Bad length encoding. */
  27948. p = badLenDer;
  27949. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  27950. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  27951. ASN1_OBJECT_free(a);
  27952. a = NULL;
  27953. /* Good encoding - but unknown. */
  27954. p = goodDer;
  27955. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  27956. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  27957. ASN1_OBJECT_free(a);
  27958. BIO_free(bio);
  27959. ASN1_OBJECT_free(obj);
  27960. #endif
  27961. return EXPECT_RESULT();
  27962. }
  27963. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  27964. {
  27965. EXPECT_DECLS;
  27966. #if defined(OPENSSL_EXTRA) && \
  27967. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  27968. char buf[50] = {0};
  27969. ASN1_OBJECT* obj;
  27970. const char* oid = "2.5.29.19";
  27971. const char* ln = "X509v3 Basic Constraints";
  27972. obj = NULL;
  27973. ExpectIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), 0);
  27974. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), 0);
  27975. ExpectIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), 0);
  27976. ExpectNotNull(obj = OBJ_txt2obj(oid, 0));
  27977. XMEMSET(buf, 0, sizeof(buf));
  27978. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  27979. ExpectIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  27980. ASN1_OBJECT_free(obj);
  27981. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  27982. return EXPECT_RESULT();
  27983. }
  27984. static int test_wolfSSL_sk_ASN1_OBJECT(void)
  27985. {
  27986. EXPECT_DECLS;
  27987. #if !defined(NO_ASN) && (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  27988. WOLFSSL_STACK* sk = NULL;
  27989. WOLFSSL_ASN1_OBJECT* obj;
  27990. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  27991. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  27992. wolfSSL_sk_ASN1_OBJECT_free(sk);
  27993. sk = NULL;
  27994. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  27995. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, NULL), 0);
  27996. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, NULL), 0);
  27997. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, obj), 0);
  27998. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  27999. wolfSSL_sk_ASN1_OBJECT_pop_free(sk, NULL);
  28000. sk = NULL;
  28001. /* obj freed in pop_free call. */
  28002. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  28003. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  28004. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  28005. ExpectPtrEq(obj, wolfSSL_sk_ASN1_OBJECT_pop(sk));
  28006. wolfSSL_sk_ASN1_OBJECT_free(sk);
  28007. wolfSSL_ASN1_OBJECT_free(obj);
  28008. #endif /* !NO_ASN && (OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL) */
  28009. return EXPECT_RESULT();
  28010. }
  28011. static int test_wolfSSL_ASN1_STRING(void)
  28012. {
  28013. EXPECT_DECLS;
  28014. #if defined(OPENSSL_EXTRA)
  28015. ASN1_STRING* str = NULL;
  28016. ASN1_STRING* c = NULL;
  28017. const char data[] = "hello wolfSSL";
  28018. const char data2[] = "Same len data";
  28019. const char longData[] =
  28020. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  28021. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28022. ASN1_STRING_free(str);
  28023. str = NULL;
  28024. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28025. ExpectIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  28026. ExpectIntEQ(ASN1_STRING_type(NULL), 0);
  28027. /* Check setting to NULL works. */
  28028. ExpectIntEQ(ASN1_STRING_set(str, NULL, 0), 1);
  28029. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  28030. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  28031. ExpectIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  28032. ExpectIntEQ(ASN1_STRING_set(NULL, NULL, 0), 0);
  28033. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, NULL), 0);
  28034. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(str, NULL), 0);
  28035. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, str), 0);
  28036. ExpectNull(wolfSSL_ASN1_STRING_dup(NULL));
  28037. ExpectNotNull(c = wolfSSL_ASN1_STRING_dup(str));
  28038. ExpectIntEQ(ASN1_STRING_cmp(NULL, NULL), -1);
  28039. ExpectIntEQ(ASN1_STRING_cmp(str, NULL), -1);
  28040. ExpectIntEQ(ASN1_STRING_cmp(NULL, c), -1);
  28041. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  28042. ExpectIntEQ(ASN1_STRING_set(c, (const void*)data2, -1), 1);
  28043. ExpectIntGT(ASN1_STRING_cmp(str, c), 0);
  28044. ExpectIntEQ(ASN1_STRING_set(str, (const void*)longData, -1), 1);
  28045. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(c, str), 1);
  28046. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  28047. /* Check setting back to smaller size frees dynamic data. */
  28048. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  28049. ExpectIntLT(ASN1_STRING_cmp(str, c), 0);
  28050. ExpectIntGT(ASN1_STRING_cmp(c, str), 0);
  28051. ExpectNull(ASN1_STRING_get0_data(NULL));
  28052. ExpectNotNull(ASN1_STRING_get0_data(str));
  28053. ExpectNull(ASN1_STRING_data(NULL));
  28054. ExpectNotNull(ASN1_STRING_data(str));
  28055. ExpectIntEQ(ASN1_STRING_length(NULL), 0);
  28056. ExpectIntGT(ASN1_STRING_length(str), 0);
  28057. ASN1_STRING_free(c);
  28058. ASN1_STRING_free(str);
  28059. ASN1_STRING_free(NULL);
  28060. #ifndef NO_WOLFSSL_STUB
  28061. ExpectNull(d2i_DISPLAYTEXT(NULL, NULL, 0));
  28062. #endif
  28063. #endif
  28064. return EXPECT_RESULT();
  28065. }
  28066. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  28067. {
  28068. EXPECT_DECLS;
  28069. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_RSA) && \
  28070. !defined(NO_FILESYSTEM)
  28071. WOLFSSL_X509* x509 = NULL;
  28072. WOLFSSL_X509_NAME* subject = NULL;
  28073. WOLFSSL_X509_NAME_ENTRY* e = NULL;
  28074. WOLFSSL_ASN1_STRING* a = NULL;
  28075. FILE* file = XBADFILE;
  28076. int idx = 0;
  28077. char targetOutput[16] = "www.wolfssl.com";
  28078. unsigned char* actual_output = NULL;
  28079. int len = 0;
  28080. ExpectNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  28081. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  28082. if (file != NULL)
  28083. fclose(file);
  28084. /* wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName */
  28085. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  28086. ExpectIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  28087. NID_commonName, -1)), 5);
  28088. ExpectNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  28089. ExpectNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  28090. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  28091. ExpectIntEQ(strncmp((const char*)actual_output, targetOutput, len), 0);
  28092. a = NULL;
  28093. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
  28094. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)), -1);
  28095. /* wolfSSL_ASN1_STRING_to_UTF8(valid, NULL) */
  28096. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)), -1);
  28097. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL) */
  28098. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)), -1);
  28099. wolfSSL_X509_free(x509);
  28100. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28101. ExpectNotNull(a = ASN1_STRING_new());
  28102. ExpectIntEQ(wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a), -1);
  28103. ASN1_STRING_free(a);
  28104. #endif
  28105. return EXPECT_RESULT();
  28106. }
  28107. static int test_wolfSSL_i2s_ASN1_STRING(void)
  28108. {
  28109. EXPECT_DECLS;
  28110. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  28111. WOLFSSL_ASN1_STRING* str = NULL;
  28112. const char* data = "test_wolfSSL_i2s_ASN1_STRING";
  28113. char* ret = NULL;
  28114. ExpectNotNull(str = ASN1_STRING_new());
  28115. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, NULL));
  28116. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28117. ret = NULL;
  28118. /* No data. */
  28119. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  28120. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28121. ret = NULL;
  28122. ExpectIntEQ(ASN1_STRING_set(str, data, 0), 1);
  28123. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  28124. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28125. ret = NULL;
  28126. ExpectIntEQ(ASN1_STRING_set(str, data, -1), 1);
  28127. /* No type. */
  28128. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  28129. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28130. ASN1_STRING_free(str);
  28131. #endif
  28132. return EXPECT_RESULT();
  28133. }
  28134. static int test_wolfSSL_ASN1_STRING_canon(void)
  28135. {
  28136. EXPECT_DECLS;
  28137. #if defined(WOLFSSL_TEST_STATIC_BUILD)
  28138. #if !defined(NO_CERTS) && (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
  28139. defined(OPENSSL_EXTRA_X509_SMALL))
  28140. WOLFSSL_ASN1_STRING* orig = NULL;
  28141. WOLFSSL_ASN1_STRING* canon = NULL;
  28142. const char* data = "test_wolfSSL_ASN1_STRING_canon";
  28143. const char* whitespaceOnly = "\t\r\n";
  28144. const char* modData = " \x01\f\t\x02\r\n\v\xff\nTt \n";
  28145. const char* canonData = "\x01 \x02 \xff tt";
  28146. const char longData[] =
  28147. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  28148. ExpectNotNull(orig = ASN1_STRING_new());
  28149. ExpectNotNull(canon = ASN1_STRING_new());
  28150. /* Invalid parameter testing. */
  28151. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, NULL), BAD_FUNC_ARG);
  28152. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, NULL), BAD_FUNC_ARG);
  28153. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, orig), BAD_FUNC_ARG);
  28154. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28155. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28156. ExpectIntEQ(ASN1_STRING_set(orig, longData, (int)XSTRLEN(data)), 1);
  28157. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28158. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28159. ExpectIntEQ(ASN1_STRING_set(orig, data, (int)XSTRLEN(data)), 1);
  28160. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28161. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28162. ASN1_STRING_free(orig);
  28163. orig = NULL;
  28164. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  28165. ExpectIntEQ(ASN1_STRING_set(orig, modData, 15), 1);
  28166. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28167. ExpectIntEQ(ASN1_STRING_set(orig, canonData, 8), 1);
  28168. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28169. ASN1_STRING_free(orig);
  28170. orig = NULL;
  28171. ExpectNotNull(orig = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  28172. ExpectIntEQ(ASN1_STRING_set(orig, whitespaceOnly, 3), 1);
  28173. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28174. ASN1_STRING_free(orig);
  28175. orig = NULL;
  28176. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  28177. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28178. ASN1_STRING_free(orig);
  28179. ASN1_STRING_free(canon);
  28180. #endif
  28181. #endif
  28182. return EXPECT_RESULT();
  28183. }
  28184. static int test_wolfSSL_ASN1_STRING_print(void)
  28185. {
  28186. EXPECT_DECLS;
  28187. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS) && \
  28188. !defined(NO_BIO)
  28189. ASN1_STRING* asnStr = NULL;
  28190. const char HELLO_DATA[]= \
  28191. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  28192. #define MAX_UNPRINTABLE_CHAR 32
  28193. #define MAX_BUF 255
  28194. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  28195. unsigned char expected[sizeof(unprintableData)+1];
  28196. unsigned char rbuf[MAX_BUF];
  28197. BIO *bio = NULL;
  28198. int p_len;
  28199. int i;
  28200. /* setup */
  28201. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  28202. unprintableData[i] = HELLO_DATA[i];
  28203. expected[i] = HELLO_DATA[i];
  28204. }
  28205. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  28206. unprintableData[sizeof(HELLO_DATA)+i] = i;
  28207. if (i == (int)'\n' || i == (int)'\r')
  28208. expected[sizeof(HELLO_DATA)+i] = i;
  28209. else
  28210. expected[sizeof(HELLO_DATA)+i] = '.';
  28211. }
  28212. unprintableData[sizeof(unprintableData)-1] = '\0';
  28213. expected[sizeof(expected)-1] = '\0';
  28214. XMEMSET(rbuf, 0, MAX_BUF);
  28215. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28216. ExpectIntEQ(BIO_set_write_buf_size(bio, MAX_BUF), 0);
  28217. ExpectNotNull(asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28218. ExpectIntEQ(ASN1_STRING_set(asnStr,(const void*)unprintableData,
  28219. (int)sizeof(unprintableData)), 1);
  28220. /* test */
  28221. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, NULL), 0);
  28222. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, NULL), 0);
  28223. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, asnStr), 0);
  28224. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print(bio, asnStr), 46);
  28225. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 46), 46);
  28226. ExpectStrEQ((char*)rbuf, (const char*)expected);
  28227. BIO_free(bio);
  28228. bio = NULL;
  28229. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28230. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28231. /* Ensure there is 0 bytes available to write into. */
  28232. ExpectIntEQ(BIO_write(bio, rbuf, 1), 1);
  28233. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28234. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28235. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28236. ExpectIntEQ(BIO_set_write_buf_size(bio, 45), 1);
  28237. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28238. BIO_free(bio);
  28239. ASN1_STRING_free(asnStr);
  28240. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS && !NO_BIO */
  28241. return EXPECT_RESULT();
  28242. }
  28243. static int test_wolfSSL_ASN1_STRING_print_ex(void)
  28244. {
  28245. EXPECT_DECLS;
  28246. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  28247. ASN1_STRING* asn_str = NULL;
  28248. const char data[] = "Hello wolfSSL!";
  28249. ASN1_STRING* esc_str = NULL;
  28250. const char esc_data[] = "a+;<>";
  28251. ASN1_STRING* neg_int = NULL;
  28252. const char neg_int_data[] = "\xff";
  28253. ASN1_STRING* neg_enum = NULL;
  28254. const char neg_enum_data[] = "\xff";
  28255. BIO *bio = NULL;
  28256. BIO *fixed = NULL;
  28257. unsigned long flags;
  28258. int p_len;
  28259. unsigned char rbuf[255];
  28260. /* setup */
  28261. XMEMSET(rbuf, 0, 255);
  28262. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28263. ExpectIntEQ(BIO_set_write_buf_size(bio, 255), 0);
  28264. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28265. ExpectNotNull(asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28266. ExpectIntEQ(ASN1_STRING_set(asn_str, (const void*)data, sizeof(data)), 1);
  28267. ExpectNotNull(esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28268. ExpectIntEQ(ASN1_STRING_set(esc_str, (const void*)esc_data,
  28269. sizeof(esc_data)), 1);
  28270. ExpectNotNull(neg_int = ASN1_STRING_type_new(V_ASN1_NEG_INTEGER));
  28271. ExpectIntEQ(ASN1_STRING_set(neg_int, (const void*)neg_int_data,
  28272. sizeof(neg_int_data) - 1), 1);
  28273. ExpectNotNull(neg_enum = ASN1_STRING_type_new(V_ASN1_NEG_ENUMERATED));
  28274. ExpectIntEQ(ASN1_STRING_set(neg_enum, (const void*)neg_enum_data,
  28275. sizeof(neg_enum_data) - 1), 1);
  28276. /* Invalid parameter testing. */
  28277. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, NULL, 0), 0);
  28278. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(bio, NULL, 0), 0);
  28279. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, asn_str, 0), 0);
  28280. /* no flags */
  28281. XMEMSET(rbuf, 0, 255);
  28282. flags = 0;
  28283. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 15);
  28284. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 15), 15);
  28285. ExpectStrEQ((char*)rbuf, "Hello wolfSSL!");
  28286. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28287. /* Ensure there is 0 bytes available to write into. */
  28288. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28289. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28290. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28291. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28292. ExpectIntEQ(BIO_set_write_buf_size(fixed, 14), 1);
  28293. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28294. /* RFC2253 Escape */
  28295. XMEMSET(rbuf, 0, 255);
  28296. flags = ASN1_STRFLGS_ESC_2253;
  28297. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags), 9);
  28298. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 9), 9);
  28299. ExpectStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  28300. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28301. /* Ensure there is 0 bytes available to write into. */
  28302. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28303. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28304. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28305. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28306. ExpectIntEQ(BIO_set_write_buf_size(fixed, 8), 1);
  28307. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28308. /* Show type */
  28309. XMEMSET(rbuf, 0, 255);
  28310. flags = ASN1_STRFLGS_SHOW_TYPE;
  28311. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 28);
  28312. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 28), 28);
  28313. ExpectStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  28314. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28315. /* Ensure there is 0 bytes available to write into. */
  28316. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28317. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28318. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28319. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28320. ExpectIntEQ(BIO_set_write_buf_size(fixed, 12), 1);
  28321. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28322. ExpectIntEQ(BIO_set_write_buf_size(fixed, 27), 1);
  28323. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28324. /* Dump All */
  28325. XMEMSET(rbuf, 0, 255);
  28326. flags = ASN1_STRFLGS_DUMP_ALL;
  28327. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 31);
  28328. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 31), 31);
  28329. ExpectStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  28330. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28331. /* Ensure there is 0 bytes available to write into. */
  28332. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28333. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28334. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28335. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28336. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  28337. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28338. /* Dump Der */
  28339. XMEMSET(rbuf, 0, 255);
  28340. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  28341. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 35);
  28342. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 35), 35);
  28343. ExpectStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  28344. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28345. /* Ensure there is 0 bytes available to write into. */
  28346. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28347. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28348. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28349. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28350. ExpectIntEQ(BIO_set_write_buf_size(fixed, 2), 1);
  28351. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28352. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  28353. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28354. /* Dump All + Show type */
  28355. XMEMSET(rbuf, 0, 255);
  28356. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28357. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 44);
  28358. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 44), 44);
  28359. ExpectStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  28360. /* Dump All + Show type - Negative Integer. */
  28361. XMEMSET(rbuf, 0, 255);
  28362. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28363. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_int, flags), 11);
  28364. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 11), 11);
  28365. ExpectStrEQ((char*)rbuf, "INTEGER:#FF");
  28366. /* Dump All + Show type - Negative Enumerated. */
  28367. XMEMSET(rbuf, 0, 255);
  28368. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28369. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_enum, flags), 14);
  28370. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 14), 14);
  28371. ExpectStrEQ((char*)rbuf, "ENUMERATED:#FF");
  28372. BIO_free(fixed);
  28373. BIO_free(bio);
  28374. ASN1_STRING_free(asn_str);
  28375. ASN1_STRING_free(esc_str);
  28376. ASN1_STRING_free(neg_int);
  28377. ASN1_STRING_free(neg_enum);
  28378. ExpectStrEQ(wolfSSL_ASN1_tag2str(-1), "(unknown)");
  28379. ExpectStrEQ(wolfSSL_ASN1_tag2str(31), "(unknown)");
  28380. #endif
  28381. return EXPECT_RESULT();
  28382. }
  28383. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  28384. {
  28385. EXPECT_DECLS;
  28386. #if defined(OPENSSL_ALL) && !defined(NO_ASN)
  28387. ASN1_STRING* asn1str_test = NULL;
  28388. ASN1_STRING* asn1str_answer = NULL;
  28389. /* Each character is encoded using 4 bytes */
  28390. char input[] = {
  28391. 0, 0, 0, 'T',
  28392. 0, 0, 0, 'e',
  28393. 0, 0, 0, 's',
  28394. 0, 0, 0, 't',
  28395. };
  28396. char output[] = "Test";
  28397. char badInput[] = {
  28398. 1, 0, 0, 'T',
  28399. 0, 1, 0, 'e',
  28400. 0, 0, 1, 's',
  28401. };
  28402. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(NULL), 0);
  28403. /* Test wrong type. */
  28404. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28405. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28406. ASN1_STRING_free(asn1str_test);
  28407. asn1str_test = NULL;
  28408. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  28409. /* Test bad length. */
  28410. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input) - 1), 1);
  28411. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28412. /* Test bad input. */
  28413. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 0, 4), 1);
  28414. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28415. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 4, 4), 1);
  28416. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28417. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 8, 4), 1);
  28418. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28419. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  28420. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  28421. ExpectNotNull(
  28422. asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  28423. ExpectIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  28424. ExpectIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  28425. ASN1_STRING_free(asn1str_test);
  28426. ASN1_STRING_free(asn1str_answer);
  28427. #endif /* OPENSSL_ALL && !NO_ASN */
  28428. return EXPECT_RESULT();
  28429. }
  28430. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  28431. {
  28432. EXPECT_DECLS;
  28433. #if defined(OPENSSL_EXTRA)
  28434. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime = NULL;
  28435. unsigned char nullstr[32];
  28436. XMEMSET(nullstr, 0, 32);
  28437. ExpectNotNull(asn1_gtime = (WOLFSSL_ASN1_GENERALIZEDTIME*)XMALLOC(
  28438. sizeof(WOLFSSL_ASN1_GENERALIZEDTIME), NULL, DYNAMIC_TYPE_TMP_BUFFER));
  28439. if (asn1_gtime != NULL) {
  28440. XMEMCPY(asn1_gtime->data,"20180504123500Z",ASN_GENERALIZED_TIME_SIZE);
  28441. wolfSSL_ASN1_GENERALIZEDTIME_free(asn1_gtime);
  28442. ExpectIntEQ(0, XMEMCMP(asn1_gtime->data, nullstr, 32));
  28443. XFREE(asn1_gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28444. }
  28445. wolfSSL_ASN1_GENERALIZEDTIME_free(NULL);
  28446. #endif /* OPENSSL_EXTRA */
  28447. return EXPECT_RESULT();
  28448. }
  28449. static int test_wolfSSL_ASN1_GENERALIZEDTIME_print(void)
  28450. {
  28451. EXPECT_DECLS;
  28452. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  28453. WOLFSSL_ASN1_GENERALIZEDTIME gtime;
  28454. BIO* bio = NULL;
  28455. unsigned char buf[24];
  28456. int i;
  28457. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28458. BIO_set_write_buf_size(bio, 24);
  28459. XMEMSET(&gtime, 0, sizeof(WOLFSSL_ASN1_GENERALIZEDTIME));
  28460. XMEMCPY(gtime.data, "20180504123500Z", ASN_GENERALIZED_TIME_SIZE);
  28461. gtime.length = ASN_GENERALIZED_TIME_SIZE;
  28462. /* Type not set. */
  28463. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  28464. gtime.type = V_ASN1_GENERALIZEDTIME;
  28465. /* Invalid parameters testing. */
  28466. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, NULL), BAD_FUNC_ARG);
  28467. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, NULL), BAD_FUNC_ARG);
  28468. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, &gtime), BAD_FUNC_ARG);
  28469. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 1);
  28470. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 20);
  28471. ExpectIntEQ(XMEMCMP(buf, "May 04 12:35:00 2018", 20), 0);
  28472. BIO_free(bio);
  28473. bio = NULL;
  28474. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28475. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28476. /* Ensure there is 0 bytes available to write into. */
  28477. ExpectIntEQ(BIO_write(bio, buf, 1), 1);
  28478. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  28479. for (i = 1; i < 20; i++) {
  28480. ExpectIntEQ(BIO_set_write_buf_size(bio, i), 1);
  28481. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, &gtime), 0);
  28482. }
  28483. BIO_free(bio);
  28484. wolfSSL_ASN1_GENERALIZEDTIME_free(&gtime);
  28485. #endif /* OPENSSL_EXTRA */
  28486. return EXPECT_RESULT();
  28487. }
  28488. static int test_wolfSSL_ASN1_TIME(void)
  28489. {
  28490. EXPECT_DECLS;
  28491. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28492. WOLFSSL_ASN1_TIME* asn_time = NULL;
  28493. unsigned char *data;
  28494. ExpectNotNull(asn_time = ASN1_TIME_new());
  28495. #ifndef NO_WOLFSSL_STUB
  28496. ExpectNotNull(ASN1_TIME_set(asn_time, 1));
  28497. #endif
  28498. ExpectIntEQ(ASN1_TIME_set_string(NULL, NULL), 0);
  28499. ExpectIntEQ(ASN1_TIME_set_string(asn_time, NULL), 0);
  28500. ExpectIntEQ(ASN1_TIME_set_string(NULL,
  28501. "String longer than CTC_DATA_SIZE that is 32 bytes"), 0);
  28502. ExpectIntEQ(ASN1_TIME_set_string(NULL, "101219181011Z"), 1);
  28503. ExpectIntEQ(ASN1_TIME_set_string(asn_time, "101219181011Z"), 1);
  28504. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(NULL), 0);
  28505. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(asn_time), ASN_UTC_TIME_SIZE - 1);
  28506. ExpectNull(wolfSSL_ASN1_TIME_get_data(NULL));
  28507. ExpectNotNull(data = wolfSSL_ASN1_TIME_get_data(asn_time));
  28508. ExpectIntEQ(XMEMCMP(data, "101219181011Z", 14), 0);
  28509. ExpectIntEQ(ASN1_TIME_check(NULL), 0);
  28510. ExpectIntEQ(ASN1_TIME_check(asn_time), 1);
  28511. ASN1_TIME_free(asn_time);
  28512. ASN1_TIME_free(NULL);
  28513. #endif
  28514. return EXPECT_RESULT();
  28515. }
  28516. static int test_wolfSSL_ASN1_TIME_to_string(void)
  28517. {
  28518. EXPECT_DECLS;
  28519. #ifndef NO_ASN_TIME
  28520. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28521. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  28522. WOLFSSL_ASN1_TIME* t = NULL;
  28523. char buf[ASN_GENERALIZED_TIME_SIZE];
  28524. ExpectNotNull((t = ASN1_TIME_new()));
  28525. ExpectIntEQ(ASN1_TIME_set_string(t, "030222211515Z"), 1);
  28526. /* Invalid parameter testing. */
  28527. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 4));
  28528. ExpectNull(ASN1_TIME_to_string(t, NULL, 4));
  28529. ExpectNull(ASN1_TIME_to_string(NULL, buf, 4));
  28530. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 5));
  28531. ExpectNull(ASN1_TIME_to_string(NULL, buf, 5));
  28532. ExpectNull(ASN1_TIME_to_string(t, NULL, 5));
  28533. ExpectNull(ASN1_TIME_to_string(t, buf, 4));
  28534. /* Buffer needs to be longer than minimum of 5 characters. */
  28535. ExpectNull(ASN1_TIME_to_string(t, buf, 5));
  28536. ASN1_TIME_free(t);
  28537. #endif
  28538. #endif /* NO_ASN_TIME */
  28539. return EXPECT_RESULT();
  28540. }
  28541. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  28542. {
  28543. EXPECT_DECLS;
  28544. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28545. ASN1_TIME* fromTime = NULL;
  28546. ASN1_TIME* closeToTime = NULL;
  28547. ASN1_TIME* toTime = NULL;
  28548. ASN1_TIME* invalidTime = NULL;
  28549. int daysDiff = 0;
  28550. int secsDiff = 0;
  28551. ExpectNotNull((fromTime = ASN1_TIME_new()));
  28552. /* Feb 22, 2003, 21:15:15 */
  28553. ExpectIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), 1);
  28554. ExpectNotNull((closeToTime = ASN1_TIME_new()));
  28555. /* Feb 22, 2003, 21:16:15 */
  28556. ExpectIntEQ(ASN1_TIME_set_string(closeToTime, "030222211615Z"), 1);
  28557. ExpectNotNull((toTime = ASN1_TIME_new()));
  28558. /* Dec 19, 2010, 18:10:11 */
  28559. ExpectIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), 1);
  28560. ExpectNotNull((invalidTime = ASN1_TIME_new()));
  28561. /* Dec 19, 2010, 18:10:11 but 'U' instead of 'Z' which is invalid. */
  28562. ExpectIntEQ(ASN1_TIME_set_string(invalidTime, "102519181011U"), 1);
  28563. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, invalidTime), 0);
  28564. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, invalidTime, toTime), 0);
  28565. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28566. /* Error conditions. */
  28567. ExpectIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime), 0);
  28568. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime), 0);
  28569. /* If both times are NULL, difference is 0. */
  28570. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL), 1);
  28571. ExpectIntEQ(daysDiff, 0);
  28572. ExpectIntEQ(secsDiff, 0);
  28573. /* If one time is NULL, it defaults to the current time. */
  28574. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime), 1);
  28575. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL), 1);
  28576. /* Normal operation. Both times non-NULL. */
  28577. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28578. ExpectIntEQ(daysDiff, 2856);
  28579. ExpectIntEQ(secsDiff, 75296);
  28580. /* Swapping the times should return negative values. */
  28581. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime), 1);
  28582. ExpectIntEQ(daysDiff, -2856);
  28583. ExpectIntEQ(secsDiff, -75296);
  28584. /* Compare with invalid time string. */
  28585. ExpectIntEQ(ASN1_TIME_compare(fromTime, invalidTime), -2);
  28586. ExpectIntEQ(ASN1_TIME_compare(invalidTime, toTime), -2);
  28587. /* Compare with days difference of 0. */
  28588. ExpectIntEQ(ASN1_TIME_compare(fromTime, closeToTime), -1);
  28589. ExpectIntEQ(ASN1_TIME_compare(closeToTime, fromTime), 1);
  28590. /* Days and seconds differences not 0. */
  28591. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  28592. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  28593. /* Same time. */
  28594. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  28595. /* Compare regression test: No seconds difference, just difference in days.
  28596. */
  28597. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  28598. ASN1_TIME_set_string(toTime, "19800101000000Z");
  28599. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  28600. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  28601. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  28602. /* Edge case with Unix epoch. */
  28603. ExpectNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  28604. ExpectNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  28605. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28606. ExpectIntEQ(daysDiff, 3652);
  28607. ExpectIntEQ(secsDiff, 0);
  28608. /* Edge case with year > 2038 (year 2038 problem). */
  28609. ExpectNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  28610. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28611. ExpectIntEQ(daysDiff, 2932896);
  28612. ExpectIntEQ(secsDiff, 86399);
  28613. ASN1_TIME_free(fromTime);
  28614. ASN1_TIME_free(closeToTime);
  28615. ASN1_TIME_free(toTime);
  28616. ASN1_TIME_free(invalidTime);
  28617. #endif
  28618. return EXPECT_RESULT();
  28619. }
  28620. static int test_wolfSSL_ASN1_TIME_adj(void)
  28621. {
  28622. EXPECT_DECLS;
  28623. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  28624. !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  28625. const int year = 365*24*60*60;
  28626. const int day = 24*60*60;
  28627. const int hour = 60*60;
  28628. const int mini = 60;
  28629. const byte asn_utc_time = ASN_UTC_TIME;
  28630. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  28631. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  28632. #endif
  28633. WOLFSSL_ASN1_TIME* asn_time = NULL;
  28634. WOLFSSL_ASN1_TIME* s = NULL;
  28635. int offset_day;
  28636. long offset_sec;
  28637. char date_str[CTC_DATE_SIZE + 1];
  28638. time_t t;
  28639. ExpectNotNull(s = wolfSSL_ASN1_TIME_new());
  28640. /* UTC notation test */
  28641. /* 2000/2/15 20:30:00 */
  28642. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  28643. offset_day = 7;
  28644. offset_sec = 45 * mini;
  28645. /* offset_sec = -45 * min;*/
  28646. ExpectNotNull(asn_time =
  28647. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  28648. ExpectTrue(asn_time->type == asn_utc_time);
  28649. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28650. CTC_DATE_SIZE));
  28651. date_str[CTC_DATE_SIZE] = '\0';
  28652. ExpectIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  28653. /* negative offset */
  28654. offset_sec = -45 * mini;
  28655. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  28656. ExpectNotNull(asn_time);
  28657. ExpectTrue(asn_time->type == asn_utc_time);
  28658. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28659. CTC_DATE_SIZE));
  28660. date_str[CTC_DATE_SIZE] = '\0';
  28661. ExpectIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  28662. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  28663. s = NULL;
  28664. XMEMSET(date_str, 0, sizeof(date_str));
  28665. /* Generalized time will overflow time_t if not long */
  28666. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  28667. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  28668. DYNAMIC_TYPE_OPENSSL);
  28669. /* GeneralizedTime notation test */
  28670. /* 2055/03/01 09:00:00 */
  28671. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  28672. offset_day = 12;
  28673. offset_sec = 10 * mini;
  28674. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  28675. offset_sec));
  28676. ExpectTrue(asn_time->type == asn_gen_time);
  28677. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28678. CTC_DATE_SIZE));
  28679. date_str[CTC_DATE_SIZE] = '\0';
  28680. ExpectIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  28681. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  28682. s = NULL;
  28683. XMEMSET(date_str, 0, sizeof(date_str));
  28684. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  28685. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  28686. s = NULL;
  28687. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  28688. offset_day = 7;
  28689. offset_sec = 45 * mini;
  28690. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  28691. offset_sec));
  28692. ExpectTrue(asn_time->type == asn_utc_time);
  28693. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28694. CTC_DATE_SIZE));
  28695. date_str[CTC_DATE_SIZE] = '\0';
  28696. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  28697. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  28698. asn_time = NULL;
  28699. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day,
  28700. offset_sec));
  28701. ExpectTrue(asn_time->type == asn_utc_time);
  28702. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28703. CTC_DATE_SIZE));
  28704. date_str[CTC_DATE_SIZE] = '\0';
  28705. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  28706. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  28707. #endif
  28708. return EXPECT_RESULT();
  28709. }
  28710. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  28711. {
  28712. EXPECT_DECLS;
  28713. #if (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28714. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  28715. defined(OPENSSL_ALL)) && !defined(NO_ASN_TIME)
  28716. ASN1_TIME asnTime;
  28717. struct tm tm;
  28718. time_t testTime = 1683926567; /* Fri May 12 09:22:47 PM UTC 2023 */
  28719. XMEMSET(&tm, 0, sizeof(struct tm));
  28720. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  28721. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  28722. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, NULL), 1);
  28723. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28724. ExpectIntEQ(tm.tm_sec, 15);
  28725. ExpectIntEQ(tm.tm_min, 15);
  28726. ExpectIntEQ(tm.tm_hour, 21);
  28727. ExpectIntEQ(tm.tm_mday, 22);
  28728. ExpectIntEQ(tm.tm_mon, 1);
  28729. ExpectIntEQ(tm.tm_year, 100);
  28730. ExpectIntEQ(tm.tm_isdst, 0);
  28731. #ifdef XMKTIME
  28732. ExpectIntEQ(tm.tm_wday, 2);
  28733. ExpectIntEQ(tm.tm_yday, 52);
  28734. #endif
  28735. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "500222211515Z"), 1);
  28736. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28737. ExpectIntEQ(tm.tm_year, 50);
  28738. /* Get current time. */
  28739. ExpectIntEQ(ASN1_TIME_to_tm(NULL, NULL), 0);
  28740. ExpectIntEQ(ASN1_TIME_to_tm(NULL, &tm), 1);
  28741. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  28742. /* 0 length. */
  28743. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28744. /* No type. */
  28745. asnTime.length = 1;
  28746. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28747. /* Not UTCTIME length. */
  28748. asnTime.type = V_ASN1_UTCTIME;
  28749. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28750. /* Not GENERALIZEDTIME length. */
  28751. asnTime.type = V_ASN1_GENERALIZEDTIME;
  28752. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28753. /* Not Zulu timezone. */
  28754. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515U"), 1);
  28755. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28756. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "20000222211515U"), 1);
  28757. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28758. #ifdef XMKTIME
  28759. ExpectNotNull(ASN1_TIME_adj(&asnTime, testTime, 0, 0));
  28760. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28761. ExpectIntEQ(tm.tm_sec, 47);
  28762. ExpectIntEQ(tm.tm_min, 22);
  28763. ExpectIntEQ(tm.tm_hour, 21);
  28764. ExpectIntEQ(tm.tm_mday, 12);
  28765. ExpectIntEQ(tm.tm_mon, 4);
  28766. ExpectIntEQ(tm.tm_year, 123);
  28767. ExpectIntEQ(tm.tm_wday, 5);
  28768. ExpectIntEQ(tm.tm_yday, 131);
  28769. /* Confirm that when used with a tm struct from ASN1_TIME_adj, all other
  28770. fields are zeroed out as expected. */
  28771. ExpectIntEQ(tm.tm_isdst, 0);
  28772. #endif
  28773. #endif
  28774. return EXPECT_RESULT();
  28775. }
  28776. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void)
  28777. {
  28778. EXPECT_DECLS;
  28779. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28780. WOLFSSL_ASN1_TIME *t = NULL;
  28781. WOLFSSL_ASN1_TIME *out = NULL;
  28782. WOLFSSL_ASN1_TIME *gtime = NULL;
  28783. int tlen = 0;
  28784. unsigned char *data = NULL;
  28785. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  28786. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(NULL, &out));
  28787. /* type not set. */
  28788. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28789. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28790. t = NULL;
  28791. /* UTC Time test */
  28792. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  28793. if (t != NULL) {
  28794. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  28795. t->type = ASN_UTC_TIME;
  28796. t->length = ASN_UTC_TIME_SIZE;
  28797. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  28798. }
  28799. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t), ASN_UTC_TIME_SIZE);
  28800. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)), "050727123456Z");
  28801. out = NULL;
  28802. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28803. wolfSSL_ASN1_TIME_free(gtime);
  28804. gtime = NULL;
  28805. ExpectNotNull(out = wolfSSL_ASN1_TIME_new());
  28806. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28807. ExpectPtrEq(gtime, out);
  28808. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28809. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28810. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  28811. /* Generalized Time test */
  28812. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28813. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  28814. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  28815. if (t != NULL) {
  28816. t->type = ASN_GENERALIZED_TIME;
  28817. t->length = ASN_GENERALIZED_TIME_SIZE;
  28818. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  28819. }
  28820. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t),
  28821. ASN_GENERALIZED_TIME_SIZE);
  28822. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)),
  28823. "20050727123456Z");
  28824. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28825. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28826. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28827. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  28828. /* UTC Time to Generalized Time 1900's test */
  28829. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28830. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  28831. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  28832. if (t != NULL) {
  28833. t->type = ASN_UTC_TIME;
  28834. t->length = ASN_UTC_TIME_SIZE;
  28835. XMEMCPY(t->data, "500727123456Z", ASN_UTC_TIME_SIZE);
  28836. }
  28837. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28838. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28839. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28840. ExpectStrEQ((char*)gtime->data, "19500727123456Z");
  28841. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28842. /* Null parameter test */
  28843. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28844. gtime = NULL;
  28845. out = NULL;
  28846. if (t != NULL) {
  28847. t->type = ASN_UTC_TIME;
  28848. t->length = ASN_UTC_TIME_SIZE;
  28849. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  28850. }
  28851. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  28852. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28853. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28854. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  28855. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28856. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28857. #endif
  28858. return EXPECT_RESULT();
  28859. }
  28860. static int test_wolfSSL_ASN1_TIME_print(void)
  28861. {
  28862. EXPECT_DECLS;
  28863. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_BIO) && \
  28864. (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28865. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  28866. defined(OPENSSL_ALL)) && defined(USE_CERT_BUFFERS_2048) && \
  28867. !defined(NO_ASN_TIME)
  28868. BIO* bio = NULL;
  28869. BIO* fixed = NULL;
  28870. X509* x509 = NULL;
  28871. const unsigned char* der = client_cert_der_2048;
  28872. ASN1_TIME* notAfter = NULL;
  28873. ASN1_TIME* notBefore = NULL;
  28874. unsigned char buf[25];
  28875. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28876. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28877. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  28878. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  28879. ExpectNotNull(notBefore = X509_get_notBefore(x509));
  28880. ExpectIntEQ(ASN1_TIME_print(NULL, NULL), 0);
  28881. ExpectIntEQ(ASN1_TIME_print(bio, NULL), 0);
  28882. ExpectIntEQ(ASN1_TIME_print(NULL, notBefore), 0);
  28883. ExpectIntEQ(ASN1_TIME_print(bio, notBefore), 1);
  28884. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  28885. ExpectIntEQ(XMEMCMP(buf, "Dec 13 22:19:28 2023 GMT", sizeof(buf) - 1), 0);
  28886. /* Test BIO_write fails. */
  28887. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28888. /* Ensure there is 0 bytes available to write into. */
  28889. ExpectIntEQ(BIO_write(fixed, buf, 1), 1);
  28890. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  28891. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28892. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  28893. ExpectIntEQ(BIO_set_write_buf_size(fixed, 23), 1);
  28894. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  28895. /* create a bad time and test results */
  28896. ExpectNotNull(notAfter = X509_get_notAfter(x509));
  28897. ExpectIntEQ(ASN1_TIME_check(notAfter), 1);
  28898. if (EXPECT_SUCCESS()) {
  28899. notAfter->data[8] = 0;
  28900. notAfter->data[3] = 0;
  28901. }
  28902. ExpectIntNE(ASN1_TIME_print(bio, notAfter), 1);
  28903. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  28904. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  28905. ExpectIntEQ(ASN1_TIME_check(notAfter), 0);
  28906. BIO_free(bio);
  28907. BIO_free(fixed);
  28908. X509_free(x509);
  28909. #endif
  28910. return EXPECT_RESULT();
  28911. }
  28912. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  28913. {
  28914. EXPECT_DECLS;
  28915. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  28916. BIO* bio = NULL;
  28917. ASN1_UTCTIME* utc = NULL;
  28918. unsigned char buf[25];
  28919. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  28920. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  28921. const char* genDate = "20190424111501Z"; /* GEN = YYYYMMDDHHMMSSZ */
  28922. /* Valid date */
  28923. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28924. ExpectNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  28925. DYNAMIC_TYPE_ASN1));
  28926. if (utc != NULL) {
  28927. utc->type = ASN_UTC_TIME;
  28928. utc->length = ASN_UTC_TIME_SIZE;
  28929. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  28930. }
  28931. ExpectIntEQ(ASN1_UTCTIME_print(NULL, NULL), 0);
  28932. ExpectIntEQ(ASN1_UTCTIME_print(bio, NULL), 0);
  28933. ExpectIntEQ(ASN1_UTCTIME_print(NULL, utc), 0);
  28934. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  28935. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  28936. ExpectIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  28937. XMEMSET(buf, 0, sizeof(buf));
  28938. BIO_free(bio);
  28939. bio = NULL;
  28940. /* Invalid format */
  28941. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28942. if (utc != NULL) {
  28943. utc->type = ASN_UTC_TIME;
  28944. utc->length = ASN_UTC_TIME_SIZE;
  28945. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  28946. }
  28947. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  28948. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  28949. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  28950. /* Invalid type */
  28951. if (utc != NULL) {
  28952. utc->type = ASN_GENERALIZED_TIME;
  28953. utc->length = ASN_GENERALIZED_TIME_SIZE;
  28954. XMEMCPY(utc->data, (byte*)genDate, ASN_GENERALIZED_TIME_SIZE);
  28955. }
  28956. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  28957. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  28958. BIO_free(bio);
  28959. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  28960. return EXPECT_RESULT();
  28961. }
  28962. static int test_wolfSSL_ASN1_TYPE(void)
  28963. {
  28964. EXPECT_DECLS;
  28965. #if defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD) || \
  28966. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  28967. WOLFSSL_ASN1_TYPE* t = NULL;
  28968. WOLFSSL_ASN1_OBJECT* obj = NULL;
  28969. #ifndef NO_ASN_TIME
  28970. WOLFSSL_ASN1_TIME* time = NULL;
  28971. #endif
  28972. WOLFSSL_ASN1_STRING* str = NULL;
  28973. unsigned char data[] = { 0x00 };
  28974. ASN1_TYPE_set(NULL, V_ASN1_NULL, NULL);
  28975. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28976. ASN1_TYPE_set(t, V_ASN1_EOC, NULL);
  28977. wolfSSL_ASN1_TYPE_free(t);
  28978. t = NULL;
  28979. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28980. ASN1_TYPE_set(t, V_ASN1_NULL, NULL);
  28981. ASN1_TYPE_set(t, V_ASN1_NULL, data);
  28982. wolfSSL_ASN1_TYPE_free(t);
  28983. t = NULL;
  28984. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28985. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  28986. ASN1_TYPE_set(t, V_ASN1_OBJECT, obj);
  28987. wolfSSL_ASN1_TYPE_free(t);
  28988. t = NULL;
  28989. #ifndef NO_ASN_TIME
  28990. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28991. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  28992. ASN1_TYPE_set(t, V_ASN1_UTCTIME, time);
  28993. wolfSSL_ASN1_TYPE_free(t);
  28994. t = NULL;
  28995. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  28996. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  28997. ASN1_TYPE_set(t, V_ASN1_GENERALIZEDTIME, time);
  28998. wolfSSL_ASN1_TYPE_free(t);
  28999. t = NULL;
  29000. #endif
  29001. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29002. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29003. ASN1_TYPE_set(t, V_ASN1_UTF8STRING, str);
  29004. wolfSSL_ASN1_TYPE_free(t);
  29005. t = NULL;
  29006. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29007. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29008. ASN1_TYPE_set(t, V_ASN1_PRINTABLESTRING, str);
  29009. wolfSSL_ASN1_TYPE_free(t);
  29010. t = NULL;
  29011. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29012. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29013. ASN1_TYPE_set(t, V_ASN1_T61STRING, str);
  29014. wolfSSL_ASN1_TYPE_free(t);
  29015. t = NULL;
  29016. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29017. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29018. ASN1_TYPE_set(t, V_ASN1_IA5STRING, str);
  29019. wolfSSL_ASN1_TYPE_free(t);
  29020. t = NULL;
  29021. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29022. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29023. ASN1_TYPE_set(t, V_ASN1_UNIVERSALSTRING, str);
  29024. wolfSSL_ASN1_TYPE_free(t);
  29025. t = NULL;
  29026. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29027. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29028. ASN1_TYPE_set(t, V_ASN1_SEQUENCE, str);
  29029. wolfSSL_ASN1_TYPE_free(t);
  29030. t = NULL;
  29031. #endif
  29032. return EXPECT_RESULT();
  29033. }
  29034. /* Testing code used in dpp.c in hostap */
  29035. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  29036. typedef struct {
  29037. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  29038. * as an OID identifying the curve */
  29039. X509_ALGOR *alg;
  29040. /* Compressed format public key per ANSI X9.63 */
  29041. ASN1_BIT_STRING *pub_key;
  29042. } DPP_BOOTSTRAPPING_KEY;
  29043. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  29044. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  29045. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  29046. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  29047. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  29048. typedef struct {
  29049. ASN1_INTEGER *integer;
  29050. } TEST_ASN1;
  29051. ASN1_SEQUENCE(TEST_ASN1) = {
  29052. ASN1_SIMPLE(TEST_ASN1, integer, ASN1_INTEGER),
  29053. } ASN1_SEQUENCE_END(TEST_ASN1)
  29054. IMPLEMENT_ASN1_FUNCTIONS(TEST_ASN1)
  29055. typedef struct {
  29056. ASN1_OCTET_STRING *octet_string;
  29057. } TEST_FAIL_ASN1;
  29058. #define WOLFSSL_ASN1_OCTET_STRING_ASN1 4
  29059. ASN1_SEQUENCE(TEST_FAIL_ASN1) = {
  29060. ASN1_SIMPLE(TEST_FAIL_ASN1, octet_string, ASN1_OCTET_STRING),
  29061. } ASN1_SEQUENCE_END(TEST_FAIL_ASN1)
  29062. IMPLEMENT_ASN1_FUNCTIONS(TEST_FAIL_ASN1)
  29063. #endif
  29064. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  29065. {
  29066. EXPECT_DECLS;
  29067. /* Testing code used in dpp.c in hostap */
  29068. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  29069. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  29070. EC_KEY *eckey = NULL;
  29071. EVP_PKEY *key = NULL;
  29072. size_t len = 0;
  29073. unsigned char *der = NULL;
  29074. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  29075. const unsigned char *in = ecc_clikey_der_256;
  29076. WOLFSSL_ASN1_OBJECT* ec_obj = NULL;
  29077. WOLFSSL_ASN1_OBJECT* group_obj = NULL;
  29078. const EC_GROUP *group = NULL;
  29079. const EC_POINT *point = NULL;
  29080. int nid;
  29081. TEST_ASN1 *test_asn1 = NULL;
  29082. TEST_FAIL_ASN1 test_fail_asn1;
  29083. const unsigned char badObjDer[] = { 0x06, 0x00 };
  29084. const unsigned char goodObjDer[] = {
  29085. 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01
  29086. };
  29087. WOLFSSL_ASN1_ITEM emptyTemplate;
  29088. XMEMSET(&emptyTemplate, 0, sizeof(WOLFSSL_ASN1_ITEM));
  29089. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  29090. der = NULL;
  29091. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(NULL, &der), 0);
  29092. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, NULL), 0);
  29093. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29094. ExpectNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  29095. (long)sizeof_ecc_clikey_der_256));
  29096. ExpectNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  29097. ExpectNotNull(group = EC_KEY_get0_group(eckey));
  29098. ExpectNotNull(point = EC_KEY_get0_public_key(eckey));
  29099. nid = EC_GROUP_get_curve_name(group);
  29100. ec_obj = OBJ_nid2obj(EVP_PKEY_EC);
  29101. group_obj = OBJ_nid2obj(nid);
  29102. if ((ec_obj != NULL) && (group_obj != NULL)) {
  29103. ExpectIntEQ(X509_ALGOR_set0(bootstrap->alg, ec_obj, V_ASN1_OBJECT,
  29104. group_obj), 1);
  29105. if (EXPECT_SUCCESS()) {
  29106. ec_obj = NULL;
  29107. group_obj = NULL;
  29108. }
  29109. }
  29110. wolfSSL_ASN1_OBJECT_free(group_obj);
  29111. wolfSSL_ASN1_OBJECT_free(ec_obj);
  29112. ExpectIntEQ(EC_POINT_point2oct(group, point, 0, NULL, 0, NULL), 0);
  29113. #ifdef HAVE_COMP_KEY
  29114. ExpectIntGT((len = EC_POINT_point2oct(
  29115. group, point, POINT_CONVERSION_COMPRESSED,
  29116. NULL, 0, NULL)), 0);
  29117. #else
  29118. ExpectIntGT((len = EC_POINT_point2oct(
  29119. group, point, POINT_CONVERSION_UNCOMPRESSED,
  29120. NULL, 0, NULL)), 0);
  29121. #endif
  29122. ExpectNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  29123. #ifdef HAVE_COMP_KEY
  29124. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  29125. der, len-1, NULL), 0);
  29126. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  29127. der, len, NULL), len);
  29128. #else
  29129. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  29130. der, len-1, NULL), 0);
  29131. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  29132. der, len, NULL), len);
  29133. #endif
  29134. if (EXPECT_SUCCESS()) {
  29135. bootstrap->pub_key->data = der;
  29136. bootstrap->pub_key->length = (int)len;
  29137. /* Not actually used */
  29138. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  29139. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  29140. }
  29141. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, NULL), 0);
  29142. der = NULL;
  29143. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29144. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29145. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  29146. EVP_PKEY_free(key);
  29147. EC_KEY_free(eckey);
  29148. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  29149. bootstrap = NULL;
  29150. DPP_BOOTSTRAPPING_KEY_free(NULL);
  29151. /* Create bootstrap key with bad OBJECT_ID DER data, parameter that is
  29152. * a NULL and an empty BIT_STRING. */
  29153. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  29154. ExpectNotNull(bootstrap->alg->algorithm = wolfSSL_ASN1_OBJECT_new());
  29155. if (EXPECT_SUCCESS()) {
  29156. bootstrap->alg->algorithm->obj = badObjDer;
  29157. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(badObjDer);
  29158. }
  29159. ExpectNotNull(bootstrap->alg->parameter = wolfSSL_ASN1_TYPE_new());
  29160. if (EXPECT_SUCCESS()) {
  29161. bootstrap->alg->parameter->type = V_ASN1_NULL;
  29162. bootstrap->alg->parameter->value.ptr = NULL;
  29163. bootstrap->pub_key->data = NULL;
  29164. bootstrap->pub_key->length = 0;
  29165. /* Not actually used */
  29166. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  29167. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  29168. }
  29169. /* Encode with bad OBJECT_ID. */
  29170. der = NULL;
  29171. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29172. /* Fix OBJECT_ID and encode with empty BIT_STRING. */
  29173. if (EXPECT_SUCCESS()) {
  29174. bootstrap->alg->algorithm->obj = goodObjDer;
  29175. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(goodObjDer);
  29176. bootstrap->alg->algorithm->grp = 2;
  29177. }
  29178. der = NULL;
  29179. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 16);
  29180. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, &emptyTemplate), 0);
  29181. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  29182. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  29183. /* Test integer */
  29184. ExpectNotNull(test_asn1 = TEST_ASN1_new());
  29185. der = NULL;
  29186. ExpectIntEQ(ASN1_INTEGER_set(test_asn1->integer, 100), 1);
  29187. ExpectIntEQ(i2d_TEST_ASN1(test_asn1, &der), 5);
  29188. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  29189. TEST_ASN1_free(test_asn1);
  29190. /* Test integer cases. */
  29191. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  29192. TEST_ASN1_free(NULL);
  29193. /* Test error cases. */
  29194. ExpectNull(TEST_FAIL_ASN1_new());
  29195. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  29196. TEST_FAIL_ASN1_free(NULL);
  29197. XMEMSET(&test_fail_asn1, 0, sizeof(TEST_FAIL_ASN1));
  29198. ExpectIntEQ(i2d_TEST_FAIL_ASN1(&test_fail_asn1, &der), 0);
  29199. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  29200. #endif /* OPENSSL_ALL && HAVE_ECC && USE_CERT_BUFFERS_256 */
  29201. return EXPECT_RESULT();
  29202. }
  29203. static int test_wolfSSL_lhash(void)
  29204. {
  29205. EXPECT_DECLS;
  29206. #ifdef OPENSSL_ALL
  29207. const char testStr[] = "Like a true nature's child\n"
  29208. "We were born\n"
  29209. "Born to be wild";
  29210. #ifdef NO_SHA
  29211. ExpectIntEQ(lh_strhash(testStr), 0xf9dc8a43);
  29212. #else
  29213. ExpectIntEQ(lh_strhash(testStr), 0x5b7541dc);
  29214. #endif
  29215. #endif
  29216. return EXPECT_RESULT();
  29217. }
  29218. static int test_wolfSSL_X509_NAME(void)
  29219. {
  29220. EXPECT_DECLS;
  29221. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  29222. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29223. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  29224. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  29225. defined(OPENSSL_EXTRA))
  29226. X509* x509 = NULL;
  29227. const unsigned char* c = NULL;
  29228. unsigned char buf[4096];
  29229. int bytes = 0;
  29230. XFILE f = XBADFILE;
  29231. const X509_NAME* a = NULL;
  29232. const X509_NAME* b = NULL;
  29233. X509_NAME* d2i_name = NULL;
  29234. int sz = 0;
  29235. unsigned char* tmp = NULL;
  29236. char file[] = "./certs/ca-cert.der";
  29237. #ifndef OPENSSL_EXTRA_X509_SMALL
  29238. byte empty[] = { /* CN=empty emailAddress= */
  29239. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  29240. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  29241. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  29242. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  29243. 0x01, 0x16, 0x00
  29244. };
  29245. #endif
  29246. #ifndef OPENSSL_EXTRA_X509_SMALL
  29247. /* test compile of deprecated function, returns 0 */
  29248. ExpectIntEQ(CRYPTO_thread_id(), 0);
  29249. #endif
  29250. ExpectNotNull(a = X509_NAME_new());
  29251. X509_NAME_free((X509_NAME*)a);
  29252. a = NULL;
  29253. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  29254. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  29255. if (f != XBADFILE)
  29256. XFCLOSE(f);
  29257. c = buf;
  29258. ExpectNotNull(x509 = wolfSSL_X509_d2i_ex(NULL, c, bytes, HEAP_HINT));
  29259. /* test cmp function */
  29260. ExpectNotNull(a = X509_get_issuer_name(x509));
  29261. ExpectNotNull(b = X509_get_subject_name(x509));
  29262. #ifndef OPENSSL_EXTRA_X509_SMALL
  29263. ExpectIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  29264. #endif
  29265. tmp = buf;
  29266. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  29267. if (sz > 0 && tmp == buf) {
  29268. fprintf(stderr, "\nERROR - %s line %d failed with:", __FILE__,
  29269. __LINE__);
  29270. fprintf(stderr, " Expected pointer to be incremented\n");
  29271. abort();
  29272. }
  29273. #ifndef OPENSSL_EXTRA_X509_SMALL
  29274. tmp = buf;
  29275. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  29276. #endif
  29277. /* if output parameter is NULL, should still return required size. */
  29278. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  29279. /* retry but with the function creating a buffer */
  29280. tmp = NULL;
  29281. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  29282. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  29283. tmp = NULL;
  29284. #ifdef WOLFSSL_CERT_NAME_ALL
  29285. /* test for givenName and name */
  29286. {
  29287. WOLFSSL_X509_NAME_ENTRY* entry = NULL;
  29288. const byte gName[] = "test-given-name";
  29289. const byte name[] = "test-name";
  29290. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  29291. NID_givenName, ASN_UTF8STRING, gName, sizeof(gName)));
  29292. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  29293. 1);
  29294. wolfSSL_X509_NAME_ENTRY_free(entry);
  29295. entry = NULL;
  29296. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  29297. NID_name, ASN_UTF8STRING, name, sizeof(name)));
  29298. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  29299. 1);
  29300. wolfSSL_X509_NAME_ENTRY_free(entry);
  29301. tmp = NULL;
  29302. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  29303. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  29304. }
  29305. #endif
  29306. b = NULL;
  29307. ExpectNotNull(b = X509_NAME_dup((X509_NAME*)a));
  29308. #ifndef OPENSSL_EXTRA_X509_SMALL
  29309. ExpectIntEQ(X509_NAME_cmp(a, b), 0);
  29310. #endif
  29311. X509_NAME_free((X509_NAME*)b);
  29312. X509_NAME_free(d2i_name);
  29313. d2i_name = NULL;
  29314. X509_free(x509);
  29315. #ifndef OPENSSL_EXTRA_X509_SMALL
  29316. /* test with an empty domain component */
  29317. tmp = empty;
  29318. sz = sizeof(empty);
  29319. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  29320. ExpectIntEQ(X509_NAME_entry_count(d2i_name), 2);
  29321. /* size of empty emailAddress will be 0 */
  29322. tmp = buf;
  29323. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  29324. (char*)tmp, sizeof(buf)), 0);
  29325. /* should contain no organization name */
  29326. tmp = buf;
  29327. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  29328. (char*)tmp, sizeof(buf)), -1);
  29329. X509_NAME_free(d2i_name);
  29330. #endif
  29331. #endif
  29332. return EXPECT_RESULT();
  29333. }
  29334. static int test_wolfSSL_X509_NAME_hash(void)
  29335. {
  29336. EXPECT_DECLS;
  29337. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  29338. !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  29339. BIO* bio = NULL;
  29340. X509* x509 = NULL;
  29341. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  29342. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  29343. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  29344. ExpectIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  29345. ExpectIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  29346. X509_free(x509);
  29347. BIO_free(bio);
  29348. #endif
  29349. return EXPECT_RESULT();
  29350. }
  29351. static int test_wolfSSL_X509_NAME_print_ex(void)
  29352. {
  29353. EXPECT_DECLS;
  29354. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  29355. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  29356. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  29357. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  29358. !defined(NO_BIO) && !defined(NO_RSA)
  29359. int memSz = 0;
  29360. byte* mem = NULL;
  29361. BIO* bio = NULL;
  29362. BIO* membio = NULL;
  29363. X509* x509 = NULL;
  29364. X509_NAME* name = NULL;
  29365. const char* expNormal = "C=US, CN=wolfssl.com";
  29366. const char* expReverse = "CN=wolfssl.com, C=US";
  29367. const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
  29368. const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
  29369. const char* expRFC5523 =
  29370. "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
  29371. /* Test with real cert (svrCertFile) first */
  29372. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  29373. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  29374. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  29375. ExpectNotNull(name = X509_get_subject_name(x509));
  29376. /* Test without flags */
  29377. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29378. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29379. BIO_free(membio);
  29380. membio = NULL;
  29381. /* Test flag: XN_FLAG_RFC2253 */
  29382. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29383. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29384. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29385. BIO_free(membio);
  29386. membio = NULL;
  29387. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  29388. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29389. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29390. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29391. BIO_free(membio);
  29392. membio = NULL;
  29393. X509_free(x509);
  29394. BIO_free(bio);
  29395. name = NULL;
  29396. /* Test normal case without escaped characters */
  29397. {
  29398. /* Create name: "/C=US/CN=wolfssl.com" */
  29399. ExpectNotNull(name = X509_NAME_new());
  29400. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  29401. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  29402. WOLFSSL_SUCCESS);
  29403. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  29404. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  29405. WOLFSSL_SUCCESS);
  29406. /* Test without flags */
  29407. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29408. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29409. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29410. ExpectIntEQ(memSz, XSTRLEN(expNormal));
  29411. ExpectIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  29412. BIO_free(membio);
  29413. membio = NULL;
  29414. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  29415. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29416. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29417. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29418. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29419. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  29420. BIO_free(membio);
  29421. membio = NULL;
  29422. /* Test flags: XN_FLAG_DN_REV - reversed */
  29423. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29424. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29425. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29426. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29427. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  29428. ExpectIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  29429. BIO_free(membio);
  29430. membio = NULL;
  29431. X509_NAME_free(name);
  29432. name = NULL;
  29433. }
  29434. /* Test RFC2253 characters are escaped with backslashes */
  29435. {
  29436. ExpectNotNull(name = X509_NAME_new());
  29437. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  29438. /* space at beginning and end, and: ,+"\ */
  29439. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  29440. WOLFSSL_SUCCESS);
  29441. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  29442. /* # at beginning, and: <>;*/
  29443. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  29444. WOLFSSL_SUCCESS);
  29445. /* Test without flags */
  29446. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29447. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29448. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29449. ExpectIntEQ(memSz, XSTRLEN(expNotEscaped));
  29450. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  29451. XSTRLEN(expNotEscaped)), 0);
  29452. BIO_free(membio);
  29453. membio = NULL;
  29454. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  29455. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29456. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29457. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29458. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29459. ExpectIntEQ(memSz, XSTRLEN(expRFC5523));
  29460. ExpectIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  29461. BIO_free(membio);
  29462. membio = NULL;
  29463. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  29464. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29465. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29466. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29467. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29468. ExpectIntEQ(memSz, XSTRLEN(expNotEscapedRev));
  29469. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  29470. XSTRLEN(expNotEscapedRev)), 0);
  29471. BIO_free(membio);
  29472. X509_NAME_free(name);
  29473. }
  29474. #endif
  29475. return EXPECT_RESULT();
  29476. }
  29477. #ifndef NO_BIO
  29478. static int test_wolfSSL_X509_INFO_multiple_info(void)
  29479. {
  29480. EXPECT_DECLS;
  29481. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  29482. STACK_OF(X509_INFO) *info_stack = NULL;
  29483. X509_INFO *info = NULL;
  29484. int len;
  29485. int i;
  29486. const char* files[] = {
  29487. cliCertFile,
  29488. cliKeyFile,
  29489. /* This needs to be the order as svrCertFile contains the
  29490. * intermediate cert as well. */
  29491. svrKeyFile,
  29492. svrCertFile,
  29493. NULL,
  29494. };
  29495. const char** curFile;
  29496. BIO *fileBIO = NULL;
  29497. BIO *concatBIO = NULL;
  29498. byte tmp[FOURK_BUF];
  29499. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  29500. * to group objects together. */
  29501. ExpectNotNull(concatBIO = BIO_new(BIO_s_mem()));
  29502. for (curFile = files; EXPECT_SUCCESS() && *curFile != NULL; curFile++) {
  29503. int fileLen = 0;
  29504. ExpectNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  29505. ExpectIntGT(fileLen = wolfSSL_BIO_get_len(fileBIO), 0);
  29506. if (EXPECT_SUCCESS()) {
  29507. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  29508. ExpectIntEQ(BIO_write(concatBIO, tmp, len), len);
  29509. fileLen -= len;
  29510. if (EXPECT_FAIL())
  29511. break;
  29512. }
  29513. /* Make sure we read the entire file */
  29514. ExpectIntEQ(fileLen, 0);
  29515. }
  29516. BIO_free(fileBIO);
  29517. fileBIO = NULL;
  29518. }
  29519. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  29520. NULL));
  29521. ExpectIntEQ(sk_X509_INFO_num(info_stack), 3);
  29522. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  29523. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  29524. ExpectNotNull(info->x509);
  29525. ExpectNull(info->crl);
  29526. if (i != 0) {
  29527. ExpectNotNull(info->x_pkey);
  29528. ExpectIntEQ(X509_check_private_key(info->x509,
  29529. info->x_pkey->dec_pkey), 1);
  29530. }
  29531. else {
  29532. ExpectNull(info->x_pkey);
  29533. }
  29534. }
  29535. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29536. BIO_free(concatBIO);
  29537. #endif
  29538. return EXPECT_RESULT();
  29539. }
  29540. #endif
  29541. #ifndef NO_BIO
  29542. static int test_wolfSSL_X509_INFO(void)
  29543. {
  29544. EXPECT_DECLS;
  29545. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  29546. STACK_OF(X509_INFO) *info_stack = NULL;
  29547. X509_INFO *info = NULL;
  29548. BIO *cert = NULL;
  29549. int i;
  29550. /* PEM in hex format to avoid null terminator */
  29551. byte data[] = {
  29552. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  29553. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  29554. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  29555. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  29556. 0x2d, 0x2d, 0x2d
  29557. };
  29558. /* PEM in hex format to avoid null terminator */
  29559. byte data2[] = {
  29560. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  29561. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  29562. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  29563. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  29564. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  29565. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  29566. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  29567. };
  29568. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  29569. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29570. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  29571. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  29572. ExpectNotNull(info->x509);
  29573. ExpectNull(info->crl);
  29574. ExpectNull(info->x_pkey);
  29575. }
  29576. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29577. info_stack = NULL;
  29578. BIO_free(cert);
  29579. cert = NULL;
  29580. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  29581. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29582. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29583. info_stack = NULL;
  29584. BIO_free(cert);
  29585. cert = NULL;
  29586. /* This case should fail due to invalid input. */
  29587. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  29588. ExpectIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  29589. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29590. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29591. info_stack = NULL;
  29592. BIO_free(cert);
  29593. cert = NULL;
  29594. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  29595. ExpectIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  29596. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29597. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29598. BIO_free(cert);
  29599. #endif
  29600. return EXPECT_RESULT();
  29601. }
  29602. #endif
  29603. static int test_wolfSSL_X509_subject_name_hash(void)
  29604. {
  29605. EXPECT_DECLS;
  29606. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29607. !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  29608. X509* x509 = NULL;
  29609. X509_NAME* subjectName = NULL;
  29610. unsigned long ret1 = 0;
  29611. unsigned long ret2 = 0;
  29612. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29613. SSL_FILETYPE_PEM));
  29614. ExpectNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  29615. /* These two
  29616. * - X509_subject_name_hash(x509)
  29617. * - X509_NAME_hash(X509_get_subject_name(x509))
  29618. * should give the same hash, if !defined(NO_SHA) is true. */
  29619. ret1 = X509_subject_name_hash(x509);
  29620. ExpectIntNE(ret1, 0);
  29621. #if !defined(NO_SHA)
  29622. ret2 = X509_NAME_hash(X509_get_subject_name(x509));
  29623. ExpectIntNE(ret2, 0);
  29624. ExpectIntEQ(ret1, ret2);
  29625. #else
  29626. (void) ret2;
  29627. #endif
  29628. X509_free(x509);
  29629. #endif
  29630. return EXPECT_RESULT();
  29631. }
  29632. static int test_wolfSSL_X509_issuer_name_hash(void)
  29633. {
  29634. EXPECT_DECLS;
  29635. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  29636. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  29637. X509* x509 = NULL;
  29638. X509_NAME* issuertName = NULL;
  29639. unsigned long ret1 = 0;
  29640. unsigned long ret2 = 0;
  29641. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29642. SSL_FILETYPE_PEM));
  29643. ExpectNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  29644. /* These two
  29645. * - X509_issuer_name_hash(x509)
  29646. * - X509_NAME_hash(X509_get_issuer_name(x509))
  29647. * should give the same hash, if !defined(NO_SHA) is true. */
  29648. ret1 = X509_issuer_name_hash(x509);
  29649. ExpectIntNE(ret1, 0);
  29650. #if !defined(NO_SHA)
  29651. ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
  29652. ExpectIntNE(ret2, 0);
  29653. ExpectIntEQ(ret1, ret2);
  29654. #else
  29655. (void) ret2;
  29656. #endif
  29657. X509_free(x509);
  29658. #endif
  29659. return EXPECT_RESULT();
  29660. }
  29661. static int test_wolfSSL_X509_check_host(void)
  29662. {
  29663. EXPECT_DECLS;
  29664. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  29665. && !defined(NO_SHA) && !defined(NO_RSA)
  29666. X509* x509 = NULL;
  29667. const char altName[] = "example.com";
  29668. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29669. SSL_FILETYPE_PEM));
  29670. ExpectIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  29671. WOLFSSL_SUCCESS);
  29672. ExpectIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  29673. WOLFSSL_FAILURE);
  29674. X509_free(x509);
  29675. ExpectIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  29676. WOLFSSL_FAILURE);
  29677. #endif
  29678. return EXPECT_RESULT();
  29679. }
  29680. static int test_wolfSSL_X509_check_email(void)
  29681. {
  29682. EXPECT_DECLS;
  29683. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  29684. X509* x509 = NULL;
  29685. const char goodEmail[] = "info@wolfssl.com";
  29686. const char badEmail[] = "disinfo@wolfssl.com";
  29687. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29688. SSL_FILETYPE_PEM));
  29689. /* Should fail on non-matching email address */
  29690. ExpectIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  29691. WOLFSSL_FAILURE);
  29692. /* Should succeed on matching email address */
  29693. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  29694. WOLFSSL_SUCCESS);
  29695. /* Should compute length internally when not provided */
  29696. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  29697. WOLFSSL_SUCCESS);
  29698. /* Should fail when email address is NULL */
  29699. ExpectIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  29700. WOLFSSL_FAILURE);
  29701. X509_free(x509);
  29702. /* Should fail when x509 is NULL */
  29703. ExpectIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  29704. WOLFSSL_FAILURE);
  29705. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  29706. return EXPECT_RESULT();
  29707. }
  29708. static int test_wc_PemToDer(void)
  29709. {
  29710. EXPECT_DECLS;
  29711. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  29712. int ret;
  29713. DerBuffer* pDer = NULL;
  29714. const char* ca_cert = "./certs/server-cert.pem";
  29715. byte* cert_buf = NULL;
  29716. size_t cert_sz = 0;
  29717. int eccKey = 0;
  29718. EncryptedInfo info;
  29719. XMEMSET(&info, 0, sizeof(info));
  29720. ExpectIntEQ(ret = load_file(ca_cert, &cert_buf, &cert_sz), 0);
  29721. ExpectIntEQ(ret = wc_PemToDer(cert_buf, cert_sz, CERT_TYPE, &pDer, NULL,
  29722. &info, &eccKey), 0);
  29723. wc_FreeDer(&pDer);
  29724. pDer = NULL;
  29725. if (cert_buf != NULL) {
  29726. free(cert_buf);
  29727. cert_buf = NULL;
  29728. }
  29729. #ifdef HAVE_ECC
  29730. {
  29731. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  29732. byte key_buf[256] = {0};
  29733. /* Test fail of loading a key with cert type */
  29734. ExpectIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  29735. key_buf[0] = '\n';
  29736. ExpectNotNull(XMEMCPY(key_buf + 1, cert_buf, cert_sz));
  29737. ExpectIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  29738. &pDer, NULL, &info, &eccKey)), 0);
  29739. #ifdef OPENSSL_EXTRA
  29740. ExpectIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  29741. &pDer, NULL, &info, &eccKey)), 0);
  29742. #endif
  29743. wc_FreeDer(&pDer);
  29744. if (cert_buf != NULL)
  29745. free(cert_buf);
  29746. }
  29747. #endif
  29748. #endif
  29749. return EXPECT_RESULT();
  29750. }
  29751. static int test_wc_AllocDer(void)
  29752. {
  29753. EXPECT_DECLS;
  29754. #if !defined(NO_CERTS)
  29755. DerBuffer* pDer = NULL;
  29756. word32 testSize = 1024;
  29757. ExpectIntEQ(wc_AllocDer(NULL, testSize, CERT_TYPE, HEAP_HINT),
  29758. BAD_FUNC_ARG);
  29759. ExpectIntEQ(wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT), 0);
  29760. ExpectNotNull(pDer);
  29761. wc_FreeDer(&pDer);
  29762. #endif
  29763. return EXPECT_RESULT();
  29764. }
  29765. static int test_wc_CertPemToDer(void)
  29766. {
  29767. EXPECT_DECLS;
  29768. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  29769. const char* ca_cert = "./certs/ca-cert.pem";
  29770. byte* cert_buf = NULL;
  29771. size_t cert_sz = 0;
  29772. size_t cert_dersz = 0;
  29773. byte* cert_der = NULL;
  29774. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  29775. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  29776. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29777. ExpectIntGE(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  29778. (int)cert_dersz, CERT_TYPE), 0);
  29779. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, -1, CERT_TYPE),
  29780. BAD_FUNC_ARG);
  29781. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL, -1, CERT_TYPE),
  29782. BAD_FUNC_ARG);
  29783. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der, -1, CERT_TYPE),
  29784. BAD_FUNC_ARG);
  29785. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, (int)cert_dersz,
  29786. CERT_TYPE), BAD_FUNC_ARG);
  29787. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der,
  29788. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  29789. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL,
  29790. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  29791. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der, -1,
  29792. CERT_TYPE), BAD_FUNC_ARG);
  29793. if (cert_der != NULL)
  29794. free(cert_der);
  29795. if (cert_buf != NULL)
  29796. free(cert_buf);
  29797. #endif
  29798. return EXPECT_RESULT();
  29799. }
  29800. static int test_wc_KeyPemToDer(void)
  29801. {
  29802. EXPECT_DECLS;
  29803. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29804. int ret = 0;
  29805. const byte cert_buf[] = \
  29806. "-----BEGIN PRIVATE KEY-----\n"
  29807. "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDMG5KgWxP002pA\n"
  29808. "QJIdA4H5N0oM1Wf0LrHcos5RYUlrHDkC2b5p2BUpVRPmgDAFD2+8leim98x0BvcB\n"
  29809. "k48TNzrVynuwyVEY664+iQyzEBO5v27HPRydOddprbLCvRO036XINGIjauy1jHFi\n"
  29810. "HaDVx3bexSwgp9aefUGAszFXi4q1J4GacV7Cr2b/wBqUHqWv4ZXPu6R9/UYngTkD\n"
  29811. "UDJL5gLlLfcLzNyyodKPHPCIAKdWn6mSVdcHk8XVpK4y9lgz4E7YDWA6ohKZgWgG\n"
  29812. "2RDha8CMilFMDgYa0G0SiS9g3PQx0qh3AMXJJsKSVhScFCZufAE0kV6KvjP7jAqP\n"
  29813. "XBiSkRGPAgMBAAECggEAW7hmRyY2jRX2UMJThrM9VIs6fRLnYI0dQ0tsEJj536ay\n"
  29814. "nevQjArc05KWW0Yujg+WRDZPcry3RUqd9Djlmhp/F3Si6dpF1b+PMS3wJYVrf9Sd\n"
  29815. "SO5W7faArU4vnyBNe0HnY1Ta5xSVI65lg1RSIs88RTZwsooJwXYDGf0shq0/21CE\n"
  29816. "V8HOb27DDYNcEnm35lzaONjFnMqQQT2Vs9anRrPiSEXNleEvTgLVXZtGTyCGTz6v\n"
  29817. "x86Y8eSWL9YNHvPE1I+mDPuocfSR7eRNgRu7SK3mn94W5mqd7Ns072YKX/2XN1mO\n"
  29818. "66+ZFHO6v4dK1u7cSjuwrU1EhLHpUsgDz6Bna5InyQKBgQDv5l8RPy8UneKSADaf\n"
  29819. "M5L/5675I/5t4nqVjvbnQje00YveLTAEjlJBNR93Biln3sYgnvNamYDCxyEuUZ/I\n"
  29820. "S/vmBL9PoxfGZow4FcsIBOEbIn3E0SYJgCBNWthquUvGpKsYDnThJuhO+1cVmxAJ\n"
  29821. "BUOjLFnJYHM0a+Vmk9GexT2OBwKBgQDZzkUBOK7Im3eiYytFocUJyhqMH30d49X9\n"
  29822. "ujC7kGw4UWAqVe7YCSvlBa8nzWpRWK2kRpu3M0272RU0V4geyWqT+nr/SvRRPtNP\n"
  29823. "F5dY8l3yR7hjtSejqqjOfBcZT6ETJxI4tiG0+Nl5BlfM5M+0nxnkWpRcHuOR3j79\n"
  29824. "YUFERyN+OQKBgQCjlOKeUAc6d65W/+4/AFvsQ378Q57qLtSHxsR1TKHPmlNVXFqx\n"
  29825. "wJo1/JNIBduWCEHxXHF0BdfW+RGXE/FwEt/hKLuLAhrkHmjelX2sKieU6R/5ZOQa\n"
  29826. "9lMQbDHGFDOncAF6leD85hriQGBRSzrT69MDIOrYdfwYcroqCAGX0cb3YQKBgQC8\n"
  29827. "iIFQylj5SyHmjcMSNjKSA8CxFDzAV8yPIdE3Oo+CvGXqn5HsrRuy1hXE9VmXapR8\n"
  29828. "A6ackSszdHiXY0FvrNe1mfdH7wDHJwPQjdIzazCJHS3uGQxj7sDKY7226ie6pXJv\n"
  29829. "ZrCMr2/IBAaSVGm6ppHKCeIsT4ybYm7R85KEYLPHeQKBgBeJOMBinXQfWN/1jT9b\n"
  29830. "6Ywrutvp2zP8hVxQGSZJ0WG4iewZyFLsPUlbWRXOSYNPElHmdD0ZomdLVm+lSpAA\n"
  29831. "XSH5FJ/IFCwqq7Eft6Gf8NFRV+NjPMUny+PnjHe4oFP8YK/Ek22K3ttNG8Hw69Aw\n"
  29832. "AQue5o6oVfhgLiJzMdo/77gw\n"
  29833. "-----END PRIVATE KEY-----\n";
  29834. const int cert_sz = sizeof(cert_buf);
  29835. const char cert_pw[] = "password";
  29836. int cert_dersz = 0;
  29837. byte* cert_der = NULL;
  29838. /* Bad arg: Cert buffer is NULL */
  29839. ExpectIntEQ(wc_KeyPemToDer(NULL, cert_sz, cert_der, cert_dersz, ""),
  29840. BAD_FUNC_ARG);
  29841. /* Bad arg: Cert DER buffer non-NULL but size zero (or less) */
  29842. ExpectIntEQ(wc_KeyPemToDer(cert_buf, cert_sz, (byte*)&cert_der, 0, ""),
  29843. BAD_FUNC_ARG);
  29844. /* Test normal operation */
  29845. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  29846. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29847. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  29848. cert_pw), 0);
  29849. ExpectIntLE(ret, cert_sz);
  29850. if (cert_der != NULL) {
  29851. free(cert_der);
  29852. cert_der = NULL;
  29853. }
  29854. /* Test NULL for DER buffer to return needed DER buffer size */
  29855. ExpectIntGT(ret = wc_KeyPemToDer(cert_buf, cert_sz, NULL, 0, ""), 0);
  29856. ExpectIntLE(ret, cert_sz);
  29857. if (EXPECT_SUCCESS())
  29858. cert_dersz = ret;
  29859. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29860. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  29861. cert_pw), 0);
  29862. ExpectIntLE(ret, cert_sz);
  29863. if (cert_der != NULL)
  29864. free(cert_der);
  29865. #endif
  29866. return EXPECT_RESULT();
  29867. }
  29868. static int test_wc_PubKeyPemToDer(void)
  29869. {
  29870. EXPECT_DECLS;
  29871. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
  29872. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  29873. int ret = 0;
  29874. const char* key = "./certs/ecc-client-keyPub.pem";
  29875. byte* cert_buf = NULL;
  29876. size_t cert_sz = 0, cert_dersz = 0;
  29877. byte* cert_der = NULL;
  29878. ExpectIntEQ(wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  29879. cert_der, (int)cert_dersz), BAD_FUNC_ARG);
  29880. ExpectIntEQ(load_file(key, &cert_buf, &cert_sz), 0);
  29881. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  29882. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29883. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  29884. (int)cert_dersz), 0);
  29885. if (cert_der != NULL) {
  29886. free(cert_der);
  29887. cert_der = NULL;
  29888. }
  29889. /* Test NULL for DER buffer to return needed DER buffer size */
  29890. ExpectIntGT(ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz, NULL, 0), 0);
  29891. ExpectIntLE(ret, cert_sz);
  29892. cert_dersz = ret;
  29893. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29894. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  29895. (int)cert_dersz), 0);
  29896. if (cert_der != NULL) {
  29897. free(cert_der);
  29898. }
  29899. if (cert_buf != NULL) {
  29900. free(cert_buf);
  29901. }
  29902. #endif
  29903. return EXPECT_RESULT();
  29904. }
  29905. static int test_wc_PemPubKeyToDer(void)
  29906. {
  29907. EXPECT_DECLS;
  29908. #if !defined(NO_FILESYSTEM) && \
  29909. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  29910. const char* key = "./certs/ecc-client-keyPub.pem";
  29911. size_t cert_dersz = 1024;
  29912. byte* cert_der = NULL;
  29913. ExpectIntGE(wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz),
  29914. BAD_FUNC_ARG);
  29915. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29916. ExpectIntGE(wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz), 0);
  29917. if (cert_der != NULL) {
  29918. free(cert_der);
  29919. }
  29920. #endif
  29921. return EXPECT_RESULT();
  29922. }
  29923. static int test_wc_GetPubKeyDerFromCert(void)
  29924. {
  29925. EXPECT_DECLS;
  29926. #if !defined(NO_RSA) || defined(HAVE_ECC)
  29927. int ret;
  29928. word32 idx = 0;
  29929. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  29930. word32 keyDerSz = (word32)sizeof(keyDer);
  29931. DecodedCert decoded;
  29932. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  29933. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  29934. word32 certBufSz = sizeof(certBuf);
  29935. #endif
  29936. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  29937. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  29938. XFILE fp = XBADFILE;
  29939. #endif
  29940. #ifndef NO_RSA
  29941. RsaKey rsaKey;
  29942. #if defined(USE_CERT_BUFFERS_2048)
  29943. byte* rsaCertDer = (byte*)client_cert_der_2048;
  29944. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  29945. #elif defined(USE_CERT_BUFFERS_1024)
  29946. byte* rsaCertDer = (byte*)client_cert_der_1024;
  29947. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  29948. #else
  29949. unsigned char rsaCertDer[TWOK_BUF];
  29950. word32 rsaCertDerSz;
  29951. #endif
  29952. #endif
  29953. #ifdef HAVE_ECC
  29954. ecc_key eccKey;
  29955. #if defined(USE_CERT_BUFFERS_256)
  29956. byte* eccCert = (byte*)cliecc_cert_der_256;
  29957. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  29958. #else
  29959. unsigned char eccCert[ONEK_BUF];
  29960. word32 eccCertSz;
  29961. XFILE fp2 = XBADFILE;
  29962. #endif
  29963. #endif
  29964. #ifndef NO_RSA
  29965. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  29966. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  29967. ExpectIntGT(rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer),
  29968. fp), 0);
  29969. if (fp != XBADFILE) {
  29970. XFCLOSE(fp);
  29971. fp = XBADFILE;
  29972. }
  29973. #endif
  29974. /* good test case - RSA DER cert */
  29975. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  29976. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  29977. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  29978. ExpectIntGT(keyDerSz, 0);
  29979. /* sanity check, verify we can import DER public key */
  29980. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  29981. ExpectIntEQ(ret, 0);
  29982. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  29983. if (ret == 0) {
  29984. wc_FreeRsaKey(&rsaKey);
  29985. }
  29986. /* test LENGTH_ONLY_E case */
  29987. keyDerSz = 0;
  29988. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  29989. LENGTH_ONLY_E);
  29990. ExpectIntGT(keyDerSz, 0);
  29991. /* bad args: DecodedCert NULL */
  29992. ExpectIntEQ(wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz), BAD_FUNC_ARG);
  29993. /* bad args: output key buff size */
  29994. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL), BAD_FUNC_ARG);
  29995. /* bad args: zero size output key buffer */
  29996. keyDerSz = 0;
  29997. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  29998. BAD_FUNC_ARG);
  29999. wc_FreeDecodedCert(&decoded);
  30000. /* Certificate Request Tests */
  30001. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  30002. {
  30003. XMEMSET(certBuf, 0, sizeof(certBuf));
  30004. ExpectTrue((fp = XFOPEN("./certs/csr.signed.der", "rb")) != XBADFILE);
  30005. ExpectIntGT(certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp), 0);
  30006. if (fp != XBADFILE) {
  30007. XFCLOSE(fp);
  30008. }
  30009. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  30010. ExpectIntEQ(wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL), 0);
  30011. /* good test case - RSA DER certificate request */
  30012. keyDerSz = sizeof(keyDer);
  30013. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  30014. 0);
  30015. ExpectIntGT(keyDerSz, 0);
  30016. /* sanity check, verify we can import DER public key */
  30017. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  30018. ExpectIntEQ(ret, 0);
  30019. idx = 0;
  30020. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  30021. if (ret == 0) {
  30022. wc_FreeRsaKey(&rsaKey);
  30023. }
  30024. wc_FreeDecodedCert(&decoded);
  30025. }
  30026. #endif /* WOLFSSL_CERT_REQ */
  30027. #endif /* NO_RSA */
  30028. #ifdef HAVE_ECC
  30029. #ifndef USE_CERT_BUFFERS_256
  30030. ExpectTrue((fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  30031. XBADFILE);
  30032. ExpectIntGT(eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2), 0);
  30033. if (fp2 != XBADFILE) {
  30034. XFCLOSE(fp2);
  30035. }
  30036. #endif
  30037. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  30038. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  30039. /* good test case - ECC */
  30040. XMEMSET(keyDer, 0, sizeof(keyDer));
  30041. keyDerSz = sizeof(keyDer);
  30042. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  30043. ExpectIntGT(keyDerSz, 0);
  30044. /* sanity check, verify we can import DER public key */
  30045. ret = wc_ecc_init(&eccKey);
  30046. ExpectIntEQ(ret, 0);
  30047. idx = 0; /* reset idx to 0, used above in RSA case */
  30048. ExpectIntEQ(wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz), 0);
  30049. if (ret == 0) {
  30050. wc_ecc_free(&eccKey);
  30051. }
  30052. /* test LENGTH_ONLY_E case */
  30053. keyDerSz = 0;
  30054. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  30055. LENGTH_ONLY_E);
  30056. ExpectIntGT(keyDerSz, 0);
  30057. wc_FreeDecodedCert(&decoded);
  30058. #endif
  30059. #endif /* !NO_RSA || HAVE_ECC */
  30060. return EXPECT_RESULT();
  30061. }
  30062. static int test_wc_CheckCertSigPubKey(void)
  30063. {
  30064. EXPECT_DECLS;
  30065. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  30066. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  30067. int ret = 0;
  30068. const char* ca_cert = "./certs/ca-cert.pem";
  30069. byte* cert_buf = NULL;
  30070. size_t cert_sz = 0;
  30071. byte* cert_der = NULL;
  30072. word32 cert_dersz = 0;
  30073. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  30074. word32 keyDerSz = (word32)sizeof(keyDer);
  30075. DecodedCert decoded;
  30076. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  30077. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  30078. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  30079. ExpectIntGE(ret = wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  30080. (int)cert_dersz, CERT_TYPE), 0);
  30081. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  30082. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  30083. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  30084. ExpectIntGT(keyDerSz, 0);
  30085. /* Good test case. */
  30086. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  30087. keyDerSz, RSAk), 0);
  30088. /* No certificate. */
  30089. ExpectIntEQ(wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  30090. ECDSAk), BAD_FUNC_ARG);
  30091. /* Bad cert size. */
  30092. ExpectIntNE(ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  30093. RSAk), 0);
  30094. ExpectTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  30095. /* No public key. */
  30096. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL,
  30097. keyDerSz, RSAk), ASN_NO_SIGNER_E);
  30098. /* Bad public key size. */
  30099. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  30100. RSAk), BAD_FUNC_ARG);
  30101. /* Wrong aglo. */
  30102. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  30103. keyDerSz, ECDSAk), ASN_PARSE_E);
  30104. wc_FreeDecodedCert(&decoded);
  30105. if (cert_der != NULL)
  30106. free(cert_der);
  30107. if (cert_buf != NULL)
  30108. free(cert_buf);
  30109. #endif
  30110. return EXPECT_RESULT();
  30111. }
  30112. static int test_wolfSSL_certs(void)
  30113. {
  30114. EXPECT_DECLS;
  30115. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  30116. !defined(NO_RSA)
  30117. X509* x509ext = NULL;
  30118. #ifdef OPENSSL_ALL
  30119. X509* x509 = NULL;
  30120. WOLFSSL_X509_EXTENSION* ext = NULL;
  30121. ASN1_OBJECT* obj = NULL;
  30122. #endif
  30123. WOLFSSL* ssl = NULL;
  30124. WOLFSSL_CTX* ctx = NULL;
  30125. STACK_OF(ASN1_OBJECT)* sk = NULL;
  30126. ASN1_STRING* asn1_str = NULL;
  30127. AUTHORITY_KEYID* akey = NULL;
  30128. BASIC_CONSTRAINTS* bc = NULL;
  30129. int crit = 0;
  30130. #ifndef NO_WOLFSSL_SERVER
  30131. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  30132. #else
  30133. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  30134. #endif
  30135. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30136. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30137. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  30138. #if !defined(NO_CHECK_PRIVATE_KEY)
  30139. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  30140. #endif
  30141. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30142. #if !defined(NO_CHECK_PRIVATE_KEY)
  30143. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  30144. #endif
  30145. ExpectNotNull(ssl = SSL_new(ctx));
  30146. #if !defined(NO_CHECK_PRIVATE_KEY)
  30147. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30148. #endif
  30149. #ifdef HAVE_PK_CALLBACKS
  30150. ExpectIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  30151. #endif /* HAVE_PK_CALLBACKS */
  30152. /* create and use x509 */
  30153. #ifdef OPENSSL_ALL
  30154. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  30155. WOLFSSL_FILETYPE_PEM));
  30156. #endif
  30157. ExpectNotNull(x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  30158. WOLFSSL_FILETYPE_PEM));
  30159. ExpectIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  30160. #if !defined(NO_CHECK_PRIVATE_KEY)
  30161. /* with loading in a new cert the check on private key should now fail */
  30162. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30163. #endif
  30164. #if defined(USE_CERT_BUFFERS_2048)
  30165. ExpectIntEQ(SSL_use_certificate_ASN1(ssl,
  30166. (unsigned char*)server_cert_der_2048,
  30167. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  30168. #endif
  30169. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  30170. /************* Get Digest of Certificate ******************/
  30171. {
  30172. byte digest[64]; /* max digest size */
  30173. word32 digestSz;
  30174. XMEMSET(digest, 0, sizeof(digest));
  30175. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  30176. WOLFSSL_SUCCESS);
  30177. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  30178. WOLFSSL_SUCCESS);
  30179. ExpectIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  30180. WOLFSSL_FAILURE);
  30181. }
  30182. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  30183. /* test and checkout X509 extensions */
  30184. ExpectNotNull(bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext,
  30185. NID_basic_constraints, &crit, NULL));
  30186. ExpectIntEQ(crit, 0);
  30187. #ifdef OPENSSL_ALL
  30188. ExpectNotNull(ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc));
  30189. X509_EXTENSION_free(ext);
  30190. ext = NULL;
  30191. ExpectNotNull(ext = X509_EXTENSION_new());
  30192. X509_EXTENSION_set_critical(ext, 1);
  30193. ExpectNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  30194. ExpectIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  30195. ASN1_OBJECT_free(obj);
  30196. obj = NULL;
  30197. X509_EXTENSION_free(ext);
  30198. ext = NULL;
  30199. ExpectNotNull(ext = X509_EXTENSION_new());
  30200. X509_EXTENSION_set_critical(ext, 0);
  30201. ExpectIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  30202. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  30203. NULL);
  30204. ExpectIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  30205. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  30206. * and X509_get_ext_d2i has created new */
  30207. asn1_str = NULL;
  30208. X509_EXTENSION_free(ext);
  30209. ext = NULL;
  30210. #endif
  30211. BASIC_CONSTRAINTS_free(bc);
  30212. bc = NULL;
  30213. ExpectNotNull(asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext,
  30214. NID_key_usage, &crit, NULL));
  30215. ExpectIntEQ(crit, 1);
  30216. ExpectIntEQ(asn1_str->type, NID_key_usage);
  30217. #ifdef OPENSSL_ALL
  30218. ExpectNotNull(ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str));
  30219. X509_EXTENSION_free(ext);
  30220. ext = NULL;
  30221. #endif
  30222. ASN1_STRING_free(asn1_str);
  30223. asn1_str = NULL;
  30224. #ifdef OPENSSL_ALL
  30225. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  30226. NID_ext_key_usage, &crit, NULL));
  30227. ExpectNotNull(ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk));
  30228. X509_EXTENSION_free(ext);
  30229. ext = NULL;
  30230. EXTENDED_KEY_USAGE_free(sk);
  30231. sk = NULL;
  30232. #else
  30233. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  30234. &crit, NULL);
  30235. ExpectNull(sk);
  30236. #endif
  30237. ExpectNotNull(akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  30238. NID_authority_key_identifier, &crit, NULL));
  30239. #ifdef OPENSSL_ALL
  30240. ExpectNotNull(ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit,
  30241. akey));
  30242. X509_EXTENSION_free(ext);
  30243. ext = NULL;
  30244. #endif
  30245. wolfSSL_AUTHORITY_KEYID_free(akey);
  30246. akey = NULL;
  30247. /* NID not yet supported */
  30248. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30249. NID_private_key_usage_period, &crit, NULL));
  30250. ExpectIntEQ(crit, -1);
  30251. sk_ASN1_OBJECT_free(sk);
  30252. sk = NULL;
  30253. ExpectNotNull(sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext,
  30254. NID_subject_alt_name, &crit, NULL));
  30255. {
  30256. int i;
  30257. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  30258. GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
  30259. ExpectIntEQ(gen->type, GEN_DNS);
  30260. ExpectIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
  30261. }
  30262. }
  30263. sk_GENERAL_NAME_free(sk);
  30264. sk = NULL;
  30265. /* NID not yet supported */
  30266. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30267. NID_issuer_alt_name, &crit, NULL));
  30268. ExpectIntEQ(crit, -1);
  30269. sk_ASN1_OBJECT_free(sk);
  30270. sk = NULL;
  30271. /* NID not yet supported */
  30272. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30273. NID_info_access, &crit, NULL));
  30274. sk_ASN1_OBJECT_free(sk);
  30275. sk = NULL;
  30276. /* NID not yet supported */
  30277. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30278. NID_sinfo_access, &crit, NULL));
  30279. ExpectIntEQ(crit, -1);
  30280. sk_ASN1_OBJECT_free(sk);
  30281. sk = NULL;
  30282. /* NID not yet supported */
  30283. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30284. NID_name_constraints, &crit, NULL));
  30285. ExpectIntEQ(crit, -1);
  30286. sk_ASN1_OBJECT_free(sk);
  30287. sk = NULL;
  30288. /* no cert policy set */
  30289. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30290. NID_certificate_policies, &crit, NULL));
  30291. sk_ASN1_OBJECT_free(sk);
  30292. sk = NULL;
  30293. /* NID not yet supported */
  30294. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30295. NID_policy_mappings, &crit, NULL));
  30296. ExpectIntEQ(crit, -1);
  30297. sk_ASN1_OBJECT_free(sk);
  30298. sk = NULL;
  30299. /* NID not yet supported */
  30300. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30301. NID_policy_constraints, &crit, NULL));
  30302. ExpectIntEQ(crit, -1);
  30303. sk_ASN1_OBJECT_free(sk);
  30304. sk = NULL;
  30305. /* NID not yet supported */
  30306. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30307. NID_inhibit_any_policy, &crit, NULL));
  30308. ExpectIntEQ(crit, -1);
  30309. sk_ASN1_OBJECT_free(sk);
  30310. sk = NULL;
  30311. /* NID not yet supported */
  30312. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30313. NID_tlsfeature, &crit, NULL));
  30314. ExpectIntEQ(crit, -1);
  30315. sk_ASN1_OBJECT_free(sk);
  30316. sk = NULL;
  30317. /* test invalid cases */
  30318. crit = 0;
  30319. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit,
  30320. NULL));
  30321. ExpectIntEQ(crit, -1);
  30322. /* NULL passed for criticality. */
  30323. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL,
  30324. NID_tlsfeature, NULL, NULL));
  30325. ExpectIntEQ(SSL_get_hit(ssl), 0);
  30326. #ifdef OPENSSL_ALL
  30327. X509_free(x509);
  30328. #endif
  30329. X509_free(x509ext);
  30330. SSL_free(ssl);
  30331. SSL_CTX_free(ctx);
  30332. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  30333. return EXPECT_RESULT();
  30334. }
  30335. static int test_wolfSSL_X509_check_private_key(void)
  30336. {
  30337. EXPECT_DECLS;
  30338. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  30339. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  30340. X509* x509 = NULL;
  30341. EVP_PKEY* pkey = NULL;
  30342. const byte* key;
  30343. /* Check with correct key */
  30344. ExpectNotNull((x509 = X509_load_certificate_file(cliCertFile,
  30345. SSL_FILETYPE_PEM)));
  30346. key = client_key_der_2048;
  30347. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  30348. (long)sizeof_client_key_der_2048));
  30349. ExpectIntEQ(X509_check_private_key(x509, pkey), 1);
  30350. EVP_PKEY_free(pkey);
  30351. pkey = NULL;
  30352. /* Check with wrong key */
  30353. key = server_key_der_2048;
  30354. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  30355. (long)sizeof_server_key_der_2048));
  30356. ExpectIntEQ(X509_check_private_key(x509, pkey), 0);
  30357. /* test for incorrect parameter */
  30358. ExpectIntEQ(X509_check_private_key(NULL, pkey), 0);
  30359. ExpectIntEQ(X509_check_private_key(x509, NULL), 0);
  30360. ExpectIntEQ(X509_check_private_key(NULL, NULL), 0);
  30361. EVP_PKEY_free(pkey);
  30362. X509_free(x509);
  30363. #endif
  30364. return EXPECT_RESULT();
  30365. }
  30366. static int test_wolfSSL_private_keys(void)
  30367. {
  30368. EXPECT_DECLS;
  30369. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30370. !defined(NO_FILESYSTEM)
  30371. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30372. WOLFSSL* ssl = NULL;
  30373. WOLFSSL_CTX* ctx = NULL;
  30374. EVP_PKEY* pkey = NULL;
  30375. OpenSSL_add_all_digests();
  30376. OpenSSL_add_all_algorithms();
  30377. #ifndef NO_RSA
  30378. #ifndef NO_WOLFSSL_SERVER
  30379. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30380. #else
  30381. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30382. #endif
  30383. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  30384. /* Have to load a cert before you can check the private key against that
  30385. * certificates public key! */
  30386. #if !defined(NO_CHECK_PRIVATE_KEY)
  30387. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  30388. #endif
  30389. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  30390. #if !defined(NO_CHECK_PRIVATE_KEY)
  30391. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30392. #endif
  30393. ExpectNotNull(ssl = SSL_new(ctx));
  30394. #if !defined(NO_CHECK_PRIVATE_KEY)
  30395. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30396. #endif
  30397. #ifdef USE_CERT_BUFFERS_2048
  30398. {
  30399. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  30400. unsigned char buf[FOURK_BUF];
  30401. word32 bufSz;
  30402. ExpectIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  30403. (unsigned char*)client_key_der_2048,
  30404. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  30405. #if !defined(NO_CHECK_PRIVATE_KEY)
  30406. /* Should mismatch now that a different private key loaded */
  30407. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30408. #endif
  30409. ExpectIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  30410. (unsigned char*)server_key,
  30411. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  30412. #if !defined(NO_CHECK_PRIVATE_KEY)
  30413. /* After loading back in DER format of original key, should match */
  30414. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30415. #endif
  30416. /* test loading private key to the WOLFSSL_CTX */
  30417. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  30418. (unsigned char*)client_key_der_2048,
  30419. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  30420. #if !defined(NO_CHECK_PRIVATE_KEY)
  30421. /* Should mismatch now that a different private key loaded */
  30422. ExpectIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30423. #endif
  30424. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  30425. (unsigned char*)server_key,
  30426. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  30427. #if !defined(NO_CHECK_PRIVATE_KEY)
  30428. /* After loading back in DER format of original key, should match */
  30429. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30430. #endif
  30431. /* pkey not set yet, expecting to fail */
  30432. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  30433. /* set PKEY and test again */
  30434. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  30435. &server_key, (long)sizeof_server_key_der_2048));
  30436. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  30437. /* reuse PKEY structure and test
  30438. * this should be checked with a memory management sanity checker */
  30439. ExpectFalse(server_key == (const unsigned char*)server_key_der_2048);
  30440. server_key = (const unsigned char*)server_key_der_2048;
  30441. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  30442. &server_key, (long)sizeof_server_key_der_2048));
  30443. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  30444. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  30445. bufSz = FOURK_BUF;
  30446. ExpectIntGT((bufSz = wc_CreatePKCS8Key(buf, &bufSz,
  30447. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  30448. RSAk, NULL, 0)), 0);
  30449. server_key = (const unsigned char*)buf;
  30450. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  30451. (long)bufSz));
  30452. }
  30453. #endif
  30454. EVP_PKEY_free(pkey);
  30455. pkey = NULL;
  30456. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  30457. ssl = NULL;
  30458. SSL_CTX_free(ctx);
  30459. ctx = NULL;
  30460. #endif /* end of RSA private key match tests */
  30461. #ifdef HAVE_ECC
  30462. #ifndef NO_WOLFSSL_SERVER
  30463. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30464. #else
  30465. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30466. #endif
  30467. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  30468. WOLFSSL_FILETYPE_PEM));
  30469. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  30470. WOLFSSL_FILETYPE_PEM));
  30471. ExpectNotNull(ssl = SSL_new(ctx));
  30472. #if !defined(NO_CHECK_PRIVATE_KEY)
  30473. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30474. #endif
  30475. SSL_free(ssl);
  30476. ssl = NULL;
  30477. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  30478. WOLFSSL_FILETYPE_PEM));
  30479. ExpectNotNull(ssl = SSL_new(ctx));
  30480. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  30481. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30482. #endif
  30483. SSL_free(ssl);
  30484. ssl = NULL;
  30485. SSL_CTX_free(ctx);
  30486. ctx = NULL;
  30487. #endif /* end of ECC private key match tests */
  30488. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  30489. #ifndef NO_WOLFSSL_SERVER
  30490. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30491. #else
  30492. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30493. #endif
  30494. ExpectTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  30495. WOLFSSL_FILETYPE_PEM));
  30496. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  30497. WOLFSSL_FILETYPE_PEM));
  30498. ExpectNotNull(ssl = SSL_new(ctx));
  30499. #if !defined(NO_CHECK_PRIVATE_KEY)
  30500. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30501. #endif
  30502. SSL_free(ssl);
  30503. ssl = NULL;
  30504. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  30505. WOLFSSL_FILETYPE_PEM));
  30506. ExpectNotNull(ssl = SSL_new(ctx));
  30507. #if !defined(NO_CHECK_PRIVATE_KEY)
  30508. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30509. #endif
  30510. SSL_free(ssl);
  30511. ssl = NULL;
  30512. SSL_CTX_free(ctx);
  30513. ctx = NULL;
  30514. #endif /* end of Ed25519 private key match tests */
  30515. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  30516. #ifndef NO_WOLFSSL_SERVER
  30517. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30518. #else
  30519. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30520. #endif
  30521. ExpectTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  30522. WOLFSSL_FILETYPE_PEM));
  30523. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  30524. WOLFSSL_FILETYPE_PEM));
  30525. ExpectNotNull(ssl = SSL_new(ctx));
  30526. #if !defined(NO_CHECK_PRIVATE_KEY)
  30527. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30528. #endif
  30529. SSL_free(ssl);
  30530. ssl = NULL;
  30531. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  30532. WOLFSSL_FILETYPE_PEM));
  30533. ExpectNotNull(ssl = SSL_new(ctx));
  30534. #if !defined(NO_CHECK_PRIVATE_KEY)
  30535. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30536. #endif
  30537. SSL_free(ssl);
  30538. ssl = NULL;
  30539. SSL_CTX_free(ctx);
  30540. ctx = NULL;
  30541. #endif /* end of Ed448 private key match tests */
  30542. EVP_cleanup();
  30543. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  30544. CONF_modules_free();
  30545. ENGINE_cleanup();
  30546. CONF_modules_unload();
  30547. (void)ssl;
  30548. (void)ctx;
  30549. (void)pkey;
  30550. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30551. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  30552. return EXPECT_RESULT();
  30553. }
  30554. static int test_wolfSSL_PEM_def_callback(void)
  30555. {
  30556. EXPECT_DECLS;
  30557. #ifdef OPENSSL_EXTRA
  30558. char buf[10];
  30559. const char* defpwd = "DEF PWD";
  30560. int defpwdLen = (int)XSTRLEN(defpwd);
  30561. int smallLen = 1;
  30562. /* Bad parameters. */
  30563. ExpectIntEQ(wolfSSL_PEM_def_callback(NULL, sizeof(buf), 0, NULL), 0);
  30564. ExpectIntEQ(wolfSSL_PEM_def_callback(NULL, sizeof(buf), 0, (void*)defpwd),
  30565. 0);
  30566. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, sizeof(buf), 0, NULL), 0);
  30567. XMEMSET(buf, 0, sizeof(buf));
  30568. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, sizeof(buf), 0, (void*)defpwd),
  30569. defpwdLen);
  30570. ExpectIntEQ(XMEMCMP(buf, defpwd, defpwdLen), 0);
  30571. ExpectIntEQ(buf[defpwdLen], 0);
  30572. /* Size of buffer is smaller than default password. */
  30573. XMEMSET(buf, 0, sizeof(buf));
  30574. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, smallLen, 0, (void*)defpwd),
  30575. smallLen);
  30576. ExpectIntEQ(XMEMCMP(buf, defpwd, smallLen), 0);
  30577. ExpectIntEQ(buf[smallLen], 0);
  30578. #endif /* OPENSSL_EXTRA */
  30579. return EXPECT_RESULT();
  30580. }
  30581. static int test_wolfSSL_PEM_read_PrivateKey(void)
  30582. {
  30583. EXPECT_DECLS;
  30584. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || \
  30585. !defined(NO_DSA) || defined(HAVE_ECC) || !defined(NO_DH))
  30586. XFILE file = XBADFILE;
  30587. #if !defined(NO_RSA)
  30588. const char* fname_rsa = "./certs/server-key.pem";
  30589. RSA* rsa = NULL;
  30590. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  30591. unsigned char* sig = NULL;
  30592. size_t sigLen = 0;
  30593. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  30594. size_t tbsLen = sizeof(tbs);
  30595. #endif
  30596. #if !defined(NO_DSA)
  30597. const char* fname_dsa = "./certs/dsa2048.pem";
  30598. #endif
  30599. #if defined(HAVE_ECC)
  30600. const char* fname_ec = "./certs/ecc-key.pem";
  30601. #endif
  30602. #if !defined(NO_DH)
  30603. const char* fname_dh = "./certs/dh-priv-2048.pem";
  30604. #endif
  30605. EVP_PKEY* pkey = NULL;
  30606. /* Check error case. */
  30607. ExpectNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  30608. /* not a PEM key. */
  30609. ExpectTrue((file = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  30610. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30611. if (file != XBADFILE)
  30612. XFCLOSE(file);
  30613. file = XBADFILE;
  30614. #ifndef NO_RSA
  30615. /* Read in an RSA key. */
  30616. ExpectTrue((file = XFOPEN(fname_rsa, "rb")) != XBADFILE);
  30617. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30618. if (file != XBADFILE)
  30619. XFCLOSE(file);
  30620. file = XBADFILE;
  30621. /* Make sure the key is usable by signing some data with it. */
  30622. ExpectNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  30623. ExpectIntGT((sigLen = RSA_size(rsa)), 0);
  30624. ExpectNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  30625. DYNAMIC_TYPE_TMP_BUFFER));
  30626. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  30627. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  30628. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  30629. WOLFSSL_SUCCESS);
  30630. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30631. EVP_PKEY_CTX_free(ctx);
  30632. EVP_PKEY_free(pkey);
  30633. pkey = NULL;
  30634. #endif
  30635. #ifndef NO_DSA
  30636. /* Read in a DSA key. */
  30637. ExpectTrue((file = XFOPEN(fname_dsa, "rb")) != XBADFILE);
  30638. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH)
  30639. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30640. EVP_PKEY_free(pkey);
  30641. pkey = NULL;
  30642. #else
  30643. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30644. #endif
  30645. if (file != XBADFILE)
  30646. XFCLOSE(file);
  30647. file = XBADFILE;
  30648. #endif
  30649. #ifdef HAVE_ECC
  30650. /* Read in an EC key. */
  30651. ExpectTrue((file = XFOPEN(fname_ec, "rb")) != XBADFILE);
  30652. ExpectNotNull(pkey = EVP_PKEY_new());
  30653. ExpectPtrEq(PEM_read_PrivateKey(file, &pkey, NULL, NULL), pkey);
  30654. if (file != XBADFILE)
  30655. XFCLOSE(file);
  30656. file = XBADFILE;
  30657. EVP_PKEY_free(pkey);
  30658. pkey = NULL;
  30659. #endif
  30660. #ifndef NO_DH
  30661. /* Read in a DH key. */
  30662. ExpectTrue((file = XFOPEN(fname_dh, "rb")) != XBADFILE);
  30663. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  30664. defined(WOLFSSL_OPENSSH)) && (!defined(HAVE_FIPS) || \
  30665. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  30666. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30667. EVP_PKEY_free(pkey);
  30668. pkey = NULL;
  30669. #else
  30670. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30671. #endif
  30672. if (file != XBADFILE)
  30673. XFCLOSE(file);
  30674. file = XBADFILE;
  30675. #endif
  30676. #endif
  30677. return EXPECT_RESULT();
  30678. }
  30679. static int test_wolfSSL_PEM_read_PUBKEY(void)
  30680. {
  30681. EXPECT_DECLS;
  30682. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  30683. && !defined(NO_FILESYSTEM)
  30684. XFILE file = XBADFILE;
  30685. const char* fname = "./certs/client-keyPub.pem";
  30686. EVP_PKEY* pkey = NULL;
  30687. /* Check error case. */
  30688. ExpectNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  30689. /* Read in an RSA key. */
  30690. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30691. ExpectNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  30692. EVP_PKEY_free(pkey);
  30693. pkey = NULL;
  30694. if (file != XBADFILE)
  30695. XFCLOSE(file);
  30696. file = XBADFILE;
  30697. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30698. ExpectNotNull(pkey = EVP_PKEY_new());
  30699. ExpectPtrEq(PEM_read_PUBKEY(file, &pkey, NULL, NULL), pkey);
  30700. EVP_PKEY_free(pkey);
  30701. if (file != XBADFILE)
  30702. XFCLOSE(file);
  30703. #endif
  30704. return EXPECT_RESULT();
  30705. }
  30706. /* test loading RSA key using BIO */
  30707. static int test_wolfSSL_PEM_PrivateKey_rsa(void)
  30708. {
  30709. EXPECT_DECLS;
  30710. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  30711. defined(USE_CERT_BUFFERS_2048) && !defined(NO_FILESYSTEM) && \
  30712. !defined(NO_BIO)
  30713. BIO* bio = NULL;
  30714. XFILE file = XBADFILE;
  30715. const char* fname = "./certs/server-key.pem";
  30716. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  30717. EVP_PKEY* pkey = NULL;
  30718. size_t sz = 0;
  30719. byte* buf = NULL;
  30720. EVP_PKEY* pkey2 = NULL;
  30721. EVP_PKEY* pkey3 = NULL;
  30722. RSA* rsa_key = NULL;
  30723. #if defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)
  30724. unsigned char extra[10];
  30725. int i;
  30726. BIO* pub_bio = NULL;
  30727. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  30728. #endif
  30729. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30730. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30731. ExpectIntGT(sz = XFTELL(file), 0);
  30732. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30733. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30734. if (buf != NULL) {
  30735. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30736. }
  30737. if (file != XBADFILE) {
  30738. XFCLOSE(file);
  30739. file = XBADFILE;
  30740. }
  30741. /* Test using BIO new mem and loading PEM private key */
  30742. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30743. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30744. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30745. buf = NULL;
  30746. BIO_free(bio);
  30747. bio = NULL;
  30748. /* New empty EVP_PKEY */
  30749. ExpectNotNull(pkey2 = EVP_PKEY_new());
  30750. if (pkey2 != NULL) {
  30751. pkey2->type = EVP_PKEY_RSA;
  30752. }
  30753. /* Test parameter copy */
  30754. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  30755. EVP_PKEY_free(pkey2);
  30756. EVP_PKEY_free(pkey);
  30757. pkey = NULL;
  30758. /* Qt unit test case : rsa pkcs8 key */
  30759. ExpectTrue((file = XFOPEN(fname_rsa_p8, "rb")) != XBADFILE);
  30760. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30761. ExpectIntGT(sz = XFTELL(file), 0);
  30762. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30763. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30764. if (buf) {
  30765. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30766. }
  30767. if (file != XBADFILE) {
  30768. XFCLOSE(file);
  30769. file = XBADFILE;
  30770. }
  30771. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30772. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30773. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30774. buf = NULL;
  30775. BIO_free(bio);
  30776. bio = NULL;
  30777. ExpectNotNull(pkey3 = EVP_PKEY_new());
  30778. ExpectNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  30779. ExpectIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  30780. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30781. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  30782. #else
  30783. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  30784. #endif
  30785. RSA_free(rsa_key);
  30786. EVP_PKEY_free(pkey3);
  30787. EVP_PKEY_free(pkey);
  30788. pkey = NULL;
  30789. pkey2 = NULL;
  30790. #if defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)
  30791. #define BIO_PEM_TEST_CHAR 'a'
  30792. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  30793. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30794. ExpectIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  30795. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30796. ExpectIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  30797. ExpectNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key,
  30798. (long)sizeof_server_key_der_2048));
  30799. ExpectNull(pkey);
  30800. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  30801. (long)sizeof_server_key_der_2048));
  30802. ExpectIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL, NULL),
  30803. WOLFSSL_FAILURE);
  30804. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL, NULL),
  30805. WOLFSSL_FAILURE);
  30806. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30807. WOLFSSL_SUCCESS);
  30808. ExpectIntGT(BIO_pending(bio), 0);
  30809. ExpectIntEQ(BIO_pending(bio), 1679);
  30810. /* Check if the pubkey API writes only the public key */
  30811. #ifdef WOLFSSL_KEY_GEN
  30812. ExpectIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
  30813. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
  30814. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  30815. ExpectIntGT(BIO_pending(pub_bio), 0);
  30816. /* Previously both the private key and the pubkey calls would write
  30817. * out the private key and the PEM header was the only difference.
  30818. * The public PEM should be significantly shorter than the
  30819. * private key versison. */
  30820. ExpectIntEQ(BIO_pending(pub_bio), 451);
  30821. #else
  30822. /* Not supported. */
  30823. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), 0);
  30824. #endif
  30825. /* test creating new EVP_PKEY with good args */
  30826. ExpectNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30827. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  30828. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr,
  30829. pkey->pkey_sz), 0);
  30830. }
  30831. /* test of reuse of EVP_PKEY */
  30832. ExpectNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  30833. ExpectIntEQ(BIO_pending(bio), 0);
  30834. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30835. SSL_SUCCESS);
  30836. /* add 10 extra bytes after PEM */
  30837. ExpectIntEQ(BIO_write(bio, extra, 10), 10);
  30838. ExpectNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  30839. ExpectNotNull(pkey);
  30840. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  30841. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr,
  30842. pkey->pkey_sz), 0);
  30843. }
  30844. /* check 10 extra bytes still there */
  30845. ExpectIntEQ(BIO_pending(bio), 10);
  30846. ExpectIntEQ(BIO_read(bio, extra, 10), 10);
  30847. for (i = 0; i < 10; i++) {
  30848. ExpectIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  30849. }
  30850. BIO_free(pub_bio);
  30851. BIO_free(bio);
  30852. bio = NULL;
  30853. EVP_PKEY_free(pkey);
  30854. pkey = NULL;
  30855. EVP_PKEY_free(pkey2);
  30856. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  30857. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 &&
  30858. * !NO_FILESYSTEM && !NO_BIO */
  30859. return EXPECT_RESULT();
  30860. }
  30861. /* test loading ECC key using BIO */
  30862. static int test_wolfSSL_PEM_PrivateKey_ecc(void)
  30863. {
  30864. EXPECT_DECLS;
  30865. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_ECC) && \
  30866. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  30867. BIO* bio = NULL;
  30868. EVP_PKEY* pkey = NULL;
  30869. XFILE file = XBADFILE;
  30870. const char* fname = "./certs/ecc-key.pem";
  30871. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  30872. size_t sz = 0;
  30873. byte* buf = NULL;
  30874. EVP_PKEY* pkey2 = NULL;
  30875. EVP_PKEY* pkey3 = NULL;
  30876. EC_KEY* ec_key = NULL;
  30877. int nid = 0;
  30878. BIO* pub_bio = NULL;
  30879. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30880. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30881. ExpectIntGT(sz = XFTELL(file), 0);
  30882. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30883. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30884. if (buf) {
  30885. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30886. }
  30887. if (file != XBADFILE) {
  30888. XFCLOSE(file);
  30889. file = XBADFILE;
  30890. }
  30891. /* Test using BIO new mem and loading PEM private key */
  30892. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30893. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30894. BIO_free(bio);
  30895. bio = NULL;
  30896. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30897. buf = NULL;
  30898. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30899. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30900. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30901. WOLFSSL_SUCCESS);
  30902. ExpectIntGT(BIO_pending(bio), 0);
  30903. /* No parmeters. */
  30904. ExpectIntEQ(BIO_pending(bio), 227);
  30905. /* Check if the pubkey API writes only the public key */
  30906. #ifdef WOLFSSL_KEY_GEN
  30907. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  30908. ExpectIntGT(BIO_pending(pub_bio), 0);
  30909. /* Previously both the private key and the pubkey calls would write
  30910. * out the private key and the PEM header was the only difference.
  30911. * The public PEM should be significantly shorter than the
  30912. * private key versison. */
  30913. ExpectIntEQ(BIO_pending(pub_bio), 178);
  30914. #endif
  30915. BIO_free(pub_bio);
  30916. BIO_free(bio);
  30917. bio = NULL;
  30918. ExpectNotNull(pkey2 = EVP_PKEY_new());
  30919. ExpectNotNull(pkey3 = EVP_PKEY_new());
  30920. if (pkey2 != NULL) {
  30921. pkey2->type = EVP_PKEY_EC;
  30922. }
  30923. /* Test parameter copy */
  30924. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  30925. /* Qt unit test case 1*/
  30926. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  30927. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  30928. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30929. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  30930. #else
  30931. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  30932. #endif
  30933. /* Test default digest */
  30934. ExpectIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  30935. ExpectIntEQ(nid, NID_sha256);
  30936. EC_KEY_free(ec_key);
  30937. ec_key = NULL;
  30938. EVP_PKEY_free(pkey3);
  30939. pkey3 = NULL;
  30940. EVP_PKEY_free(pkey2);
  30941. pkey2 = NULL;
  30942. EVP_PKEY_free(pkey);
  30943. pkey = NULL;
  30944. /* Qt unit test case ec pkcs8 key */
  30945. ExpectTrue((file = XFOPEN(fname_ecc_p8, "rb")) != XBADFILE);
  30946. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30947. ExpectIntGT(sz = XFTELL(file), 0);
  30948. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30949. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30950. if (buf) {
  30951. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30952. }
  30953. if (file != XBADFILE) {
  30954. XFCLOSE(file);
  30955. file = XBADFILE;
  30956. }
  30957. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30958. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30959. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30960. buf = NULL;
  30961. BIO_free(bio);
  30962. bio = NULL;
  30963. ExpectNotNull(pkey3 = EVP_PKEY_new());
  30964. /* Qt unit test case */
  30965. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  30966. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  30967. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30968. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  30969. #else
  30970. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  30971. #endif
  30972. EC_KEY_free(ec_key);
  30973. EVP_PKEY_free(pkey3);
  30974. EVP_PKEY_free(pkey);
  30975. pkey = NULL;
  30976. #endif
  30977. return EXPECT_RESULT();
  30978. }
  30979. /* test loading DSA key using BIO */
  30980. static int test_wolfSSL_PEM_PrivateKey_dsa(void)
  30981. {
  30982. EXPECT_DECLS;
  30983. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_DSA) && \
  30984. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  30985. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
  30986. BIO* bio = NULL;
  30987. EVP_PKEY* pkey = NULL;
  30988. ExpectNotNull(bio = BIO_new_file("./certs/dsa2048.pem", "rb"));
  30989. /* Private DSA EVP_PKEY */
  30990. ExpectNotNull(pkey = wolfSSL_PEM_read_bio_PrivateKey(bio, NULL, NULL,
  30991. NULL));
  30992. BIO_free(bio);
  30993. bio = NULL;
  30994. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30995. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  30996. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  30997. NULL), 0);
  30998. #endif
  30999. #ifdef WOLFSSL_KEY_GEN
  31000. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), 1);
  31001. ExpectIntEQ(BIO_pending(bio), 1178);
  31002. BIO_reset(bio);
  31003. #endif
  31004. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  31005. 1);
  31006. ExpectIntEQ(BIO_pending(bio), 1196);
  31007. BIO_free(bio);
  31008. bio = NULL;
  31009. EVP_PKEY_free(pkey);
  31010. pkey = NULL;
  31011. #endif
  31012. #endif
  31013. return EXPECT_RESULT();
  31014. }
  31015. /* test loading DH key using BIO */
  31016. static int test_wolfSSL_PEM_PrivateKey_dh(void)
  31017. {
  31018. EXPECT_DECLS;
  31019. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_DH) && \
  31020. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  31021. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  31022. defined(WOLFSSL_OPENSSH)) && (!defined(HAVE_FIPS) || \
  31023. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  31024. BIO* bio = NULL;
  31025. EVP_PKEY* pkey = NULL;
  31026. ExpectNotNull(bio = BIO_new_file("./certs/dh-priv-2048.pem", "rb"));
  31027. /* Private DH EVP_PKEY */
  31028. ExpectNotNull(pkey = wolfSSL_PEM_read_bio_PrivateKey(bio, NULL, NULL,
  31029. NULL));
  31030. BIO_free(bio);
  31031. bio = NULL;
  31032. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31033. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  31034. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  31035. NULL), 0);
  31036. #endif
  31037. #ifdef WOLFSSL_KEY_GEN
  31038. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), 0);
  31039. #endif
  31040. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  31041. 1);
  31042. ExpectIntEQ(BIO_pending(bio), 806);
  31043. BIO_free(bio);
  31044. bio = NULL;
  31045. EVP_PKEY_free(pkey);
  31046. pkey = NULL;
  31047. #endif
  31048. #endif
  31049. return EXPECT_RESULT();
  31050. }
  31051. static int test_wolfSSL_PEM_PrivateKey(void)
  31052. {
  31053. EXPECT_DECLS;
  31054. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31055. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  31056. #ifndef NO_BIO
  31057. BIO* bio = NULL;
  31058. #endif
  31059. EVP_PKEY* pkey = NULL;
  31060. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  31061. #ifndef NO_BIO
  31062. /* test creating new EVP_PKEY with bad arg */
  31063. ExpectNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  31064. /* Test bad EVP_PKEY type. */
  31065. /* New HMAC EVP_PKEY */
  31066. ExpectNotNull(bio = BIO_new_mem_buf("", 1));
  31067. ExpectNotNull(pkey = EVP_PKEY_new());
  31068. if (pkey != NULL) {
  31069. pkey->type = EVP_PKEY_HMAC;
  31070. }
  31071. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  31072. 0);
  31073. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  31074. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  31075. NULL), 0);
  31076. #endif
  31077. #ifdef WOLFSSL_KEY_GEN
  31078. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), WOLFSSL_FAILURE);
  31079. #endif
  31080. EVP_PKEY_free(pkey);
  31081. pkey = NULL;
  31082. BIO_free(bio);
  31083. bio = NULL;
  31084. /* key is DES encrypted */
  31085. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  31086. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  31087. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  31088. {
  31089. XFILE f = XBADFILE;
  31090. wc_pem_password_cb* passwd_cb = NULL;
  31091. void* passwd_cb_userdata;
  31092. SSL_CTX* ctx = NULL;
  31093. char passwd[] = "bad password";
  31094. #ifndef WOLFSSL_NO_TLS12
  31095. #ifndef NO_WOLFSSL_SERVER
  31096. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  31097. #else
  31098. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  31099. #endif
  31100. #else
  31101. #ifndef NO_WOLFSSL_SERVER
  31102. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  31103. #else
  31104. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  31105. #endif
  31106. #endif
  31107. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  31108. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  31109. ExpectNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  31110. ExpectNull(passwd_cb_userdata =
  31111. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  31112. /* fail case with password call back */
  31113. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  31114. (void*)passwd));
  31115. BIO_free(bio);
  31116. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  31117. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  31118. (void*)passwd));
  31119. BIO_free(bio);
  31120. ExpectTrue((f = XFOPEN("./certs/server-keyEnc.pem", "rb")) != XBADFILE);
  31121. ExpectNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  31122. if ((bio == NULL) && (f != XBADFILE)) {
  31123. XFCLOSE(f);
  31124. }
  31125. /* use callback that works */
  31126. ExpectNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  31127. (void*)"yassl123"));
  31128. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  31129. EVP_PKEY_free(pkey);
  31130. pkey = NULL;
  31131. BIO_free(bio);
  31132. bio = NULL;
  31133. SSL_CTX_free(ctx);
  31134. }
  31135. #endif /* !defined(NO_DES3) */
  31136. #endif /* !NO_BIO */
  31137. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  31138. {
  31139. unsigned char buf[2048];
  31140. size_t bytes = 0;
  31141. XFILE f = XBADFILE;
  31142. SSL_CTX* ctx = NULL;
  31143. #ifndef WOLFSSL_NO_TLS12
  31144. #ifndef NO_WOLFSSL_SERVER
  31145. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  31146. #else
  31147. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  31148. #endif
  31149. #else
  31150. #ifndef NO_WOLFSSL_SERVER
  31151. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  31152. #else
  31153. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  31154. #endif
  31155. #endif
  31156. ExpectTrue((f = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  31157. ExpectIntGT(bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f), 0);
  31158. if (f != XBADFILE)
  31159. XFCLOSE(f);
  31160. server_key = buf;
  31161. pkey = NULL;
  31162. ExpectNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, bytes));
  31163. ExpectNull(pkey);
  31164. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, bytes));
  31165. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  31166. EVP_PKEY_free(pkey);
  31167. pkey = NULL;
  31168. SSL_CTX_free(ctx);
  31169. server_key = NULL;
  31170. }
  31171. #endif
  31172. #ifndef NO_BIO
  31173. (void)bio;
  31174. #endif
  31175. (void)pkey;
  31176. (void)server_key;
  31177. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  31178. return EXPECT_RESULT();
  31179. }
  31180. static int test_wolfSSL_PEM_file_RSAKey(void)
  31181. {
  31182. EXPECT_DECLS;
  31183. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31184. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  31185. !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  31186. RSA* rsa = NULL;
  31187. XFILE fp = XBADFILE;
  31188. ExpectTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
  31189. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
  31190. if (fp != XBADFILE)
  31191. XFCLOSE(fp);
  31192. ExpectIntEQ(RSA_size(rsa), 256);
  31193. ExpectIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
  31194. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, NULL), WOLFSSL_FAILURE);
  31195. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, rsa), WOLFSSL_SUCCESS);
  31196. ExpectIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
  31197. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  31198. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, rsa), WOLFSSL_SUCCESS);
  31199. RSA_free(rsa);
  31200. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31201. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  31202. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  31203. return EXPECT_RESULT();
  31204. }
  31205. static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
  31206. {
  31207. EXPECT_DECLS;
  31208. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  31209. !defined(NO_FILESYSTEM) && \
  31210. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  31211. RSA* rsa = NULL;
  31212. XFILE f = NULL;
  31213. ExpectTrue((f = XFOPEN(svrKeyFile, "r")) != XBADFILE);
  31214. ExpectNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  31215. ExpectIntEQ(RSA_size(rsa), 256);
  31216. if (f != XBADFILE) {
  31217. XFCLOSE(f);
  31218. f = XBADFILE;
  31219. }
  31220. ExpectIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
  31221. NULL), WOLFSSL_FAILURE);
  31222. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  31223. NULL), WOLFSSL_FAILURE);
  31224. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0, NULL, NULL),
  31225. WOLFSSL_SUCCESS);
  31226. RSA_free(rsa);
  31227. #ifdef HAVE_ECC
  31228. ExpectTrue((f = XFOPEN(eccKeyFile, "r")) != XBADFILE);
  31229. ExpectNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  31230. if (f != XBADFILE)
  31231. XFCLOSE(f);
  31232. #endif /* HAVE_ECC */
  31233. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31234. return EXPECT_RESULT();
  31235. }
  31236. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  31237. {
  31238. EXPECT_DECLS;
  31239. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31240. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31241. XFILE file = XBADFILE;
  31242. const char* fname = "./certs/client-keyPub.pem";
  31243. RSA *rsa = NULL;
  31244. ExpectNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
  31245. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  31246. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  31247. ExpectIntEQ(RSA_size(rsa), 256);
  31248. RSA_free(rsa);
  31249. if (file != XBADFILE)
  31250. XFCLOSE(file);
  31251. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31252. return EXPECT_RESULT();
  31253. }
  31254. #ifndef NO_BIO
  31255. static int test_wolfSSL_PEM_bio_RSAKey(void)
  31256. {
  31257. EXPECT_DECLS;
  31258. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31259. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  31260. !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  31261. RSA* rsa = NULL;
  31262. BIO* bio = NULL;
  31263. /* PrivateKey */
  31264. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  31265. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  31266. ExpectNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  31267. ExpectNotNull(rsa);
  31268. ExpectIntEQ(RSA_size(rsa), 256);
  31269. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  31270. NULL), WOLFSSL_FAILURE);
  31271. BIO_free(bio);
  31272. bio = NULL;
  31273. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31274. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  31275. NULL), WOLFSSL_SUCCESS);
  31276. BIO_free(bio);
  31277. bio = NULL;
  31278. RSA_free(rsa);
  31279. rsa = NULL;
  31280. /* PUBKEY */
  31281. ExpectNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  31282. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  31283. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31284. ExpectIntEQ(RSA_size(rsa), 256);
  31285. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31286. BIO_free(bio);
  31287. bio = NULL;
  31288. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31289. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  31290. BIO_free(bio);
  31291. bio = NULL;
  31292. RSA_free(rsa);
  31293. rsa = NULL;
  31294. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  31295. ExpectNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  31296. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31297. BIO_free(bio);
  31298. bio = NULL;
  31299. RSA_free(rsa);
  31300. rsa = NULL;
  31301. #ifdef HAVE_ECC
  31302. /* ensure that non-rsa keys do not work */
  31303. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  31304. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31305. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31306. BIO_free(bio);
  31307. bio = NULL;
  31308. RSA_free(rsa);
  31309. rsa = NULL;
  31310. #endif /* HAVE_ECC */
  31311. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31312. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  31313. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  31314. return EXPECT_RESULT();
  31315. }
  31316. static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
  31317. {
  31318. EXPECT_DECLS;
  31319. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31320. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31321. RSA* rsa = NULL;
  31322. RSA* rsa_dup = NULL;
  31323. BIO* bio = NULL;
  31324. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  31325. ExpectNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31326. ExpectIntEQ(RSA_size(rsa), 256);
  31327. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  31328. ExpectNull(rsa_dup = RSAPublicKey_dup(NULL));
  31329. /* Test duplicating empty key. */
  31330. ExpectNotNull(rsa_dup = RSA_new());
  31331. ExpectNull(RSAPublicKey_dup(rsa_dup));
  31332. RSA_free(rsa_dup);
  31333. rsa_dup = NULL;
  31334. ExpectNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  31335. ExpectPtrNE(rsa_dup, rsa);
  31336. #endif
  31337. /* test if valgrind complains about unreleased memory */
  31338. RSA_up_ref(rsa);
  31339. RSA_free(rsa);
  31340. BIO_free(bio);
  31341. bio = NULL;
  31342. RSA_free(rsa);
  31343. rsa = NULL;
  31344. RSA_free(rsa_dup);
  31345. rsa_dup = NULL;
  31346. #ifdef HAVE_ECC
  31347. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  31348. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31349. BIO_free(bio);
  31350. #endif /* HAVE_ECC */
  31351. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31352. return EXPECT_RESULT();
  31353. }
  31354. static int test_wolfSSL_PEM_bio_DSAKey(void)
  31355. {
  31356. EXPECT_DECLS;
  31357. #ifndef HAVE_SELFTEST
  31358. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  31359. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  31360. DSA* dsa = NULL;
  31361. BIO* bio = NULL;
  31362. /* PrivateKey */
  31363. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  31364. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  31365. ExpectNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  31366. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  31367. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  31368. NULL), WOLFSSL_FAILURE);
  31369. BIO_free(bio);
  31370. bio = NULL;
  31371. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31372. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL,
  31373. NULL), WOLFSSL_SUCCESS);
  31374. BIO_free(bio);
  31375. bio = NULL;
  31376. DSA_free(dsa);
  31377. dsa = NULL;
  31378. /* PUBKEY */
  31379. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  31380. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  31381. ExpectNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  31382. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  31383. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31384. BIO_free(bio);
  31385. bio = NULL;
  31386. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31387. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  31388. BIO_free(bio);
  31389. bio = NULL;
  31390. DSA_free(dsa);
  31391. dsa = NULL;
  31392. #ifdef HAVE_ECC
  31393. /* ensure that non-dsa keys do not work */
  31394. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  31395. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  31396. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  31397. BIO_free(bio);
  31398. bio = NULL;
  31399. DSA_free(dsa);
  31400. dsa = NULL;
  31401. #endif /* HAVE_ECC */
  31402. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  31403. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  31404. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  31405. #endif /* HAVE_SELFTEST */
  31406. return EXPECT_RESULT();
  31407. }
  31408. static int test_wolfSSL_PEM_bio_ECKey(void)
  31409. {
  31410. EXPECT_DECLS;
  31411. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31412. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  31413. EC_KEY* ec = NULL;
  31414. EC_KEY* ec2;
  31415. BIO* bio = NULL;
  31416. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  31417. unsigned char* pem = NULL;
  31418. int pLen;
  31419. #endif
  31420. static char ec_key_bad_1[] = "-----BEGIN PUBLIC KEY-----\n"
  31421. "MAA=\n"
  31422. "-----END PUBLIC KEY-----";
  31423. static char ec_priv_key_bad_1[] = "-----BEGIN EC PRIVATE KEY-----\n"
  31424. "MAA=\n"
  31425. "-----END EC PRIVATE KEY-----";
  31426. /* PrivateKey */
  31427. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  31428. ExpectNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  31429. ec2 = NULL;
  31430. ExpectNotNull((ec = PEM_read_bio_ECPrivateKey(bio, &ec2, NULL, NULL)));
  31431. ExpectIntEQ(ec == ec2, 1);
  31432. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  31433. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  31434. NULL), WOLFSSL_FAILURE);
  31435. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  31436. NULL), WOLFSSL_FAILURE);
  31437. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, ec, NULL, NULL, 0, NULL, NULL),
  31438. WOLFSSL_FAILURE);
  31439. BIO_free(bio);
  31440. bio = NULL;
  31441. /* Public key data - fail. */
  31442. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  31443. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  31444. BIO_free(bio);
  31445. bio = NULL;
  31446. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31447. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  31448. NULL), WOLFSSL_SUCCESS);
  31449. BIO_free(bio);
  31450. bio = NULL;
  31451. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, NULL, NULL, NULL, 0, NULL,
  31452. NULL),WOLFSSL_FAILURE);
  31453. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, NULL, NULL, NULL, 0, NULL, NULL),
  31454. WOLFSSL_FAILURE);
  31455. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, ec, NULL, NULL, 0, NULL, NULL),
  31456. WOLFSSL_FAILURE);
  31457. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, ec, NULL, NULL, 0, NULL, NULL),
  31458. WOLFSSL_SUCCESS);
  31459. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  31460. NULL), 0);
  31461. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  31462. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  31463. NULL), 0);
  31464. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  31465. NULL), 0);
  31466. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  31467. &pLen), 0);
  31468. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  31469. &pLen), 0);
  31470. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  31471. &pLen), 0);
  31472. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  31473. NULL), 0);
  31474. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  31475. &pLen), 1);
  31476. ExpectIntGT(pLen, 0);
  31477. XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  31478. #endif
  31479. EC_KEY_free(ec);
  31480. ec = NULL;
  31481. /* PUBKEY */
  31482. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  31483. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  31484. ec2 = NULL;
  31485. ExpectNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, &ec2, NULL, NULL)));
  31486. ExpectIntEQ(ec == ec2, 1);
  31487. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  31488. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31489. BIO_free(bio);
  31490. bio = NULL;
  31491. /* Test 0x30, 0x00 fails. */
  31492. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_key_bad_1,
  31493. sizeof(ec_key_bad_1)));
  31494. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  31495. BIO_free(bio);
  31496. bio = NULL;
  31497. /* Private key data - fail. */
  31498. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  31499. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  31500. BIO_free(bio);
  31501. bio = NULL;
  31502. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31503. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  31504. BIO_free(bio);
  31505. bio = NULL;
  31506. /* Same test as above, but with a file pointer rather than a BIO. */
  31507. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31508. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  31509. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  31510. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, ec), WOLFSSL_SUCCESS);
  31511. EC_KEY_free(ec);
  31512. ec = NULL;
  31513. #ifndef NO_RSA
  31514. /* ensure that non-ec keys do not work */
  31515. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  31516. ExpectNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  31517. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  31518. BIO_free(bio);
  31519. bio = NULL;
  31520. EC_KEY_free(ec);
  31521. ec = NULL;
  31522. #endif /* !NO_RSA */
  31523. /* Test 0x30, 0x00 fails. */
  31524. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_priv_key_bad_1,
  31525. sizeof(ec_priv_key_bad_1)));
  31526. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  31527. BIO_free(bio);
  31528. bio = NULL;
  31529. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31530. return EXPECT_RESULT();
  31531. }
  31532. static int test_wolfSSL_PEM_PUBKEY(void)
  31533. {
  31534. EXPECT_DECLS;
  31535. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  31536. BIO* bio = NULL;
  31537. EVP_PKEY* pkey = NULL;
  31538. /* test creating new EVP_PKEY with bad arg */
  31539. ExpectNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  31540. /* test loading ECC key using BIO */
  31541. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  31542. {
  31543. XFILE file = XBADFILE;
  31544. const char* fname = "./certs/ecc-client-keyPub.pem";
  31545. size_t sz = 0;
  31546. byte* buf = NULL;
  31547. EVP_PKEY* pkey2 = NULL;
  31548. EC_KEY* ec_key = NULL;
  31549. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  31550. ExpectIntEQ(XFSEEK(file, 0, XSEEK_END), 0);
  31551. ExpectIntGT(sz = XFTELL(file), 0);
  31552. ExpectIntEQ(XFSEEK(file, 0, XSEEK_SET), 0);
  31553. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  31554. if (buf != NULL) {
  31555. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  31556. }
  31557. if (file != XBADFILE) {
  31558. XFCLOSE(file);
  31559. }
  31560. /* Test using BIO new mem and loading PEM private key */
  31561. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31562. ExpectNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  31563. BIO_free(bio);
  31564. bio = NULL;
  31565. EVP_PKEY_free(pkey);
  31566. pkey = NULL;
  31567. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31568. ExpectNotNull(pkey = EVP_PKEY_new());
  31569. ExpectPtrEq(PEM_read_bio_PUBKEY(bio, &pkey, NULL, NULL), pkey);
  31570. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  31571. BIO_free(bio);
  31572. bio = NULL;
  31573. /* Qt unit test case*/
  31574. ExpectNotNull(pkey2 = EVP_PKEY_new());
  31575. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  31576. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  31577. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31578. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  31579. #else
  31580. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  31581. #endif
  31582. EC_KEY_free(ec_key);
  31583. EVP_PKEY_free(pkey2);
  31584. EVP_PKEY_free(pkey);
  31585. pkey = NULL;
  31586. }
  31587. #endif
  31588. (void)bio;
  31589. (void)pkey;
  31590. #endif
  31591. return EXPECT_RESULT();
  31592. }
  31593. #endif /* !NO_BIO */
  31594. static int test_DSA_do_sign_verify(void)
  31595. {
  31596. EXPECT_DECLS;
  31597. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  31598. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  31599. !defined(NO_DSA)
  31600. unsigned char digest[WC_SHA_DIGEST_SIZE];
  31601. DSA_SIG* sig = NULL;
  31602. DSA* dsa = NULL;
  31603. word32 bytes;
  31604. byte sigBin[DSA_SIG_SIZE];
  31605. int dsacheck;
  31606. #ifdef USE_CERT_BUFFERS_1024
  31607. byte tmp[ONEK_BUF];
  31608. XMEMSET(tmp, 0, sizeof(tmp));
  31609. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  31610. bytes = sizeof_dsa_key_der_1024;
  31611. #elif defined(USE_CERT_BUFFERS_2048)
  31612. byte tmp[TWOK_BUF];
  31613. XMEMSET(tmp, 0, sizeof(tmp));
  31614. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  31615. bytes = sizeof_dsa_key_der_2048;
  31616. #else
  31617. byte tmp[TWOK_BUF];
  31618. XFILE fp = XBADFILE;
  31619. XMEMSET(tmp, 0, sizeof(tmp));
  31620. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb") != XBADFILE);
  31621. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  31622. if (fp != XBADFILE)
  31623. XFCLOSE(fp);
  31624. #endif /* END USE_CERT_BUFFERS_1024 */
  31625. XMEMSET(digest, 202, sizeof(digest));
  31626. ExpectNotNull(dsa = DSA_new());
  31627. ExpectIntEQ(DSA_LoadDer(dsa, tmp, bytes), 1);
  31628. ExpectIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  31629. ExpectIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  31630. ExpectNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  31631. ExpectIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  31632. DSA_SIG_free(sig);
  31633. DSA_free(dsa);
  31634. #endif
  31635. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  31636. return EXPECT_RESULT();
  31637. }
  31638. static int test_wolfSSL_tmp_dh(void)
  31639. {
  31640. EXPECT_DECLS;
  31641. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  31642. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  31643. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31644. byte buff[6000];
  31645. char file[] = "./certs/dsaparams.pem";
  31646. XFILE f = XBADFILE;
  31647. int bytes = 0;
  31648. DSA* dsa = NULL;
  31649. DH* dh = NULL;
  31650. #if defined(WOLFSSL_DH_EXTRA) && \
  31651. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31652. DH* dh2 = NULL;
  31653. #endif
  31654. BIO* bio = NULL;
  31655. SSL* ssl = NULL;
  31656. SSL_CTX* ctx = NULL;
  31657. #ifndef NO_WOLFSSL_SERVER
  31658. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31659. #else
  31660. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31661. #endif
  31662. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31663. WOLFSSL_FILETYPE_PEM));
  31664. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  31665. WOLFSSL_FILETYPE_PEM));
  31666. ExpectNotNull(ssl = SSL_new(ctx));
  31667. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  31668. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  31669. if (f != XBADFILE)
  31670. XFCLOSE(f);
  31671. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  31672. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  31673. ExpectNotNull(dsa);
  31674. dh = wolfSSL_DSA_dup_DH(dsa);
  31675. ExpectNotNull(dh);
  31676. #if defined(WOLFSSL_DH_EXTRA) && \
  31677. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31678. ExpectNotNull(dh2 = wolfSSL_DH_dup(dh));
  31679. #endif
  31680. ExpectIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  31681. #ifndef NO_WOLFSSL_SERVER
  31682. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  31683. #else
  31684. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  31685. #endif
  31686. BIO_free(bio);
  31687. DSA_free(dsa);
  31688. DH_free(dh);
  31689. dh = NULL;
  31690. #if defined(WOLFSSL_DH_EXTRA) && \
  31691. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31692. DH_free(dh2);
  31693. dh2 = NULL;
  31694. #endif
  31695. SSL_free(ssl);
  31696. SSL_CTX_free(ctx);
  31697. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31698. #endif
  31699. return EXPECT_RESULT();
  31700. }
  31701. static int test_wolfSSL_ctrl(void)
  31702. {
  31703. EXPECT_DECLS;
  31704. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  31705. byte buff[6000];
  31706. BIO* bio = NULL;
  31707. int bytes;
  31708. BUF_MEM* ptr = NULL;
  31709. XMEMSET(buff, 0, sizeof(buff));
  31710. bytes = sizeof(buff);
  31711. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  31712. ExpectNotNull(BIO_s_socket());
  31713. ExpectIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  31714. /* needs tested after stubs filled out @TODO
  31715. SSL_ctrl
  31716. SSL_CTX_ctrl
  31717. */
  31718. BIO_free(bio);
  31719. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  31720. return EXPECT_RESULT();
  31721. }
  31722. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  31723. {
  31724. EXPECT_DECLS;
  31725. #ifdef OPENSSL_EXTRA
  31726. static const unsigned char pw[] = "password";
  31727. static const int pwSz = sizeof(pw) - 1;
  31728. size_t checkPwSz = 0;
  31729. const unsigned char* checkPw = NULL;
  31730. WOLFSSL_EVP_PKEY* key = NULL;
  31731. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  31732. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  31733. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  31734. pwSz));
  31735. if (key != NULL) {
  31736. ExpectIntEQ(key->type, EVP_PKEY_HMAC);
  31737. ExpectIntEQ(key->save_type, EVP_PKEY_HMAC);
  31738. ExpectIntEQ(key->pkey_sz, pwSz);
  31739. ExpectIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  31740. }
  31741. ExpectNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  31742. ExpectIntEQ((int)checkPwSz, pwSz);
  31743. ExpectIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  31744. wolfSSL_EVP_PKEY_free(key);
  31745. key = NULL;
  31746. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  31747. 0));
  31748. ExpectIntEQ(key->pkey_sz, 0);
  31749. if (EXPECT_SUCCESS()) {
  31750. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  31751. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  31752. }
  31753. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  31754. ExpectIntEQ((int)checkPwSz, 0);
  31755. wolfSSL_EVP_PKEY_free(key);
  31756. key = NULL;
  31757. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL,
  31758. 0));
  31759. ExpectIntEQ(key->pkey_sz, 0);
  31760. if (EXPECT_SUCCESS()) {
  31761. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  31762. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  31763. }
  31764. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  31765. ExpectIntEQ((int)checkPwSz, 0);
  31766. wolfSSL_EVP_PKEY_free(key);
  31767. key = NULL;
  31768. #endif /* OPENSSL_EXTRA */
  31769. return EXPECT_RESULT();
  31770. }
  31771. static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
  31772. {
  31773. EXPECT_DECLS;
  31774. #ifdef OPENSSL_EXTRA
  31775. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  31776. const char *priv = "ABCDEFGHIJKLMNOP";
  31777. const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
  31778. WOLFSSL_EVP_PKEY* key = NULL;
  31779. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31780. NULL, NULL, AES_128_KEY_SIZE, cipher));
  31781. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31782. NULL, (const unsigned char *)priv, 0, cipher));
  31783. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31784. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
  31785. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31786. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
  31787. wolfSSL_EVP_PKEY_free(key);
  31788. #endif /* WOLFSSL_CMAC && !NO_AES && WOLFSSL_AES_DIRECT */
  31789. #endif /* OPENSSL_EXTRA */
  31790. return EXPECT_RESULT();
  31791. }
  31792. static int test_wolfSSL_EVP_Digest(void)
  31793. {
  31794. EXPECT_DECLS;
  31795. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  31796. const char* in = "abc";
  31797. int inLen = (int)XSTRLEN(in);
  31798. byte out[WC_SHA256_DIGEST_SIZE];
  31799. unsigned int outLen;
  31800. const char* expOut =
  31801. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  31802. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  31803. "\x15\xAD";
  31804. ExpectIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen,
  31805. "SHA256", NULL), 1);
  31806. ExpectIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  31807. ExpectIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  31808. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  31809. return EXPECT_RESULT();
  31810. }
  31811. static int test_wolfSSL_EVP_Digest_all(void)
  31812. {
  31813. EXPECT_DECLS;
  31814. #ifdef OPENSSL_EXTRA
  31815. const char* digests[] = {
  31816. #ifndef NO_MD5
  31817. "MD5",
  31818. #endif
  31819. #ifndef NO_SHA
  31820. "SHA",
  31821. #endif
  31822. #ifdef WOLFSSL_SHA224
  31823. "SHA224",
  31824. #endif
  31825. #ifndef NO_SHA256
  31826. "SHA256",
  31827. #endif
  31828. #ifdef WOLFSSL_SHA384
  31829. "SHA384",
  31830. #endif
  31831. #ifdef WOLFSSL_SHA512
  31832. "SHA512",
  31833. #endif
  31834. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  31835. "SHA512_224",
  31836. #endif
  31837. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  31838. "SHA512_256",
  31839. #endif
  31840. #ifdef WOLFSSL_SHA3
  31841. #ifndef WOLFSSL_NOSHA3_224
  31842. "SHA3_224",
  31843. #endif
  31844. #ifndef WOLFSSL_NOSHA3_256
  31845. "SHA3_256",
  31846. #endif
  31847. "SHA3_384",
  31848. #ifndef WOLFSSL_NOSHA3_512
  31849. "SHA3_512",
  31850. #endif
  31851. #endif /* WOLFSSL_SHA3 */
  31852. NULL
  31853. };
  31854. const char** d;
  31855. const unsigned char in[] = "abc";
  31856. int inLen = XSTR_SIZEOF(in);
  31857. byte out[WC_MAX_DIGEST_SIZE];
  31858. unsigned int outLen;
  31859. for (d = digests; *d != NULL; d++) {
  31860. ExpectIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  31861. ExpectIntGT(outLen, 0);
  31862. ExpectIntEQ(EVP_MD_size(*d), outLen);
  31863. }
  31864. #endif
  31865. return EXPECT_RESULT();
  31866. }
  31867. static int test_wolfSSL_EVP_MD_size(void)
  31868. {
  31869. EXPECT_DECLS;
  31870. #ifdef OPENSSL_EXTRA
  31871. WOLFSSL_EVP_MD_CTX mdCtx;
  31872. #ifdef WOLFSSL_SHA3
  31873. #ifndef WOLFSSL_NOSHA3_224
  31874. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31875. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  31876. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  31877. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  31878. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31879. #endif
  31880. #ifndef WOLFSSL_NOSHA3_256
  31881. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31882. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  31883. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  31884. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  31885. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31886. #endif
  31887. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31888. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  31889. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  31890. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  31891. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31892. #ifndef WOLFSSL_NOSHA3_512
  31893. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31894. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  31895. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  31896. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  31897. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31898. #endif
  31899. #endif /* WOLFSSL_SHA3 */
  31900. #ifndef NO_SHA256
  31901. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31902. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  31903. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31904. WC_SHA256_DIGEST_SIZE);
  31905. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31906. WC_SHA256_BLOCK_SIZE);
  31907. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  31908. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  31909. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31910. #endif
  31911. #ifndef NO_MD5
  31912. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31913. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  31914. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31915. WC_MD5_DIGEST_SIZE);
  31916. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31917. WC_MD5_BLOCK_SIZE);
  31918. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  31919. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  31920. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31921. #endif
  31922. #ifdef WOLFSSL_SHA224
  31923. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31924. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  31925. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31926. WC_SHA224_DIGEST_SIZE);
  31927. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31928. WC_SHA224_BLOCK_SIZE);
  31929. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  31930. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  31931. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31932. #endif
  31933. #ifdef WOLFSSL_SHA384
  31934. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31935. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  31936. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31937. WC_SHA384_DIGEST_SIZE);
  31938. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31939. WC_SHA384_BLOCK_SIZE);
  31940. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  31941. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  31942. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31943. #endif
  31944. #ifdef WOLFSSL_SHA512
  31945. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31946. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  31947. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31948. WC_SHA512_DIGEST_SIZE);
  31949. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31950. WC_SHA512_BLOCK_SIZE);
  31951. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  31952. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  31953. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31954. #endif
  31955. #ifndef NO_SHA
  31956. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31957. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  31958. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31959. WC_SHA_DIGEST_SIZE);
  31960. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31961. WC_SHA_BLOCK_SIZE);
  31962. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  31963. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  31964. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31965. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31966. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  31967. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31968. WC_SHA_DIGEST_SIZE);
  31969. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31970. WC_SHA_BLOCK_SIZE);
  31971. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  31972. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  31973. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31974. #endif
  31975. /* error case */
  31976. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31977. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  31978. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31979. BAD_FUNC_ARG);
  31980. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  31981. /* Cleanup is valid on uninit'ed struct */
  31982. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31983. #endif /* OPENSSL_EXTRA */
  31984. return EXPECT_RESULT();
  31985. }
  31986. static int test_wolfSSL_EVP_MD_pkey_type(void)
  31987. {
  31988. EXPECT_DECLS;
  31989. #ifdef OPENSSL_EXTRA
  31990. const WOLFSSL_EVP_MD* md;
  31991. #ifndef NO_MD5
  31992. ExpectNotNull(md = EVP_md5());
  31993. ExpectIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  31994. #endif
  31995. #ifndef NO_SHA
  31996. ExpectNotNull(md = EVP_sha1());
  31997. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  31998. #endif
  31999. #ifdef WOLFSSL_SHA224
  32000. ExpectNotNull(md = EVP_sha224());
  32001. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  32002. #endif
  32003. ExpectNotNull(md = EVP_sha256());
  32004. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  32005. #ifdef WOLFSSL_SHA384
  32006. ExpectNotNull(md = EVP_sha384());
  32007. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  32008. #endif
  32009. #ifdef WOLFSSL_SHA512
  32010. ExpectNotNull(md = EVP_sha512());
  32011. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  32012. #endif
  32013. #endif
  32014. return EXPECT_RESULT();
  32015. }
  32016. #ifdef OPENSSL_EXTRA
  32017. static int test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  32018. size_t testKeySz, const char* testData, size_t testDataSz,
  32019. const byte* testResult, size_t testResultSz)
  32020. {
  32021. EXPECT_DECLS;
  32022. unsigned char check[WC_MAX_DIGEST_SIZE];
  32023. size_t checkSz = -1;
  32024. WOLFSSL_EVP_PKEY* key = NULL;
  32025. WOLFSSL_EVP_MD_CTX mdCtx;
  32026. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  32027. testKey, (int)testKeySz));
  32028. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32029. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  32030. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32031. (unsigned int)testDataSz), 1);
  32032. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32033. ExpectIntEQ((int)checkSz, (int)testResultSz);
  32034. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32035. ExpectIntEQ((int)checkSz,(int)testResultSz);
  32036. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  32037. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32038. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  32039. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32040. (unsigned int)testDataSz), 1);
  32041. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  32042. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32043. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32044. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  32045. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  32046. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32047. ExpectIntEQ((int)checkSz, (int)testResultSz);
  32048. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32049. ExpectIntEQ((int)checkSz,(int)testResultSz);
  32050. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  32051. (unsigned int)testDataSz - 4), 1);
  32052. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32053. ExpectIntEQ((int)checkSz,(int)testResultSz);
  32054. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  32055. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32056. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  32057. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  32058. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  32059. (unsigned int)testDataSz - 4), 1);
  32060. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  32061. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32062. wolfSSL_EVP_PKEY_free(key);
  32063. return EXPECT_RESULT();
  32064. }
  32065. #endif
  32066. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  32067. {
  32068. EXPECT_DECLS;
  32069. #ifdef OPENSSL_EXTRA
  32070. static const unsigned char testKey[] =
  32071. {
  32072. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  32073. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  32074. 0x0b, 0x0b, 0x0b, 0x0b
  32075. };
  32076. static const char testData[] = "Hi There";
  32077. #ifdef WOLFSSL_SHA224
  32078. static const unsigned char testResultSha224[] =
  32079. {
  32080. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  32081. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  32082. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  32083. 0x53, 0x68, 0x4b, 0x22
  32084. };
  32085. #endif
  32086. #ifndef NO_SHA256
  32087. static const unsigned char testResultSha256[] =
  32088. {
  32089. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  32090. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  32091. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  32092. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  32093. };
  32094. #endif
  32095. #ifdef WOLFSSL_SHA384
  32096. static const unsigned char testResultSha384[] =
  32097. {
  32098. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  32099. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  32100. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  32101. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  32102. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  32103. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  32104. };
  32105. #endif
  32106. #ifdef WOLFSSL_SHA512
  32107. static const unsigned char testResultSha512[] =
  32108. {
  32109. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  32110. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  32111. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  32112. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  32113. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  32114. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  32115. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  32116. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  32117. };
  32118. #endif
  32119. #ifdef WOLFSSL_SHA3
  32120. #ifndef WOLFSSL_NOSHA3_224
  32121. static const unsigned char testResultSha3_224[] =
  32122. {
  32123. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  32124. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  32125. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  32126. 0xf3, 0xc8, 0x60, 0xf7
  32127. };
  32128. #endif
  32129. #ifndef WOLFSSL_NOSHA3_256
  32130. static const unsigned char testResultSha3_256[] =
  32131. {
  32132. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  32133. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  32134. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  32135. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  32136. };
  32137. #endif
  32138. #ifndef WOLFSSL_NOSHA3_384
  32139. static const unsigned char testResultSha3_384[] =
  32140. {
  32141. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  32142. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  32143. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  32144. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  32145. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  32146. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  32147. };
  32148. #endif
  32149. #ifndef WOLFSSL_NOSHA3_512
  32150. static const unsigned char testResultSha3_512[] =
  32151. {
  32152. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  32153. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  32154. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  32155. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  32156. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  32157. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  32158. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  32159. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  32160. };
  32161. #endif
  32162. #endif
  32163. #ifndef NO_SHA256
  32164. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha256(), testKey,
  32165. sizeof(testKey), testData, XSTRLEN(testData), testResultSha256,
  32166. sizeof(testResultSha256)), TEST_SUCCESS);
  32167. #endif
  32168. #ifdef WOLFSSL_SHA224
  32169. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha224(), testKey,
  32170. sizeof(testKey), testData, XSTRLEN(testData), testResultSha224,
  32171. sizeof(testResultSha224)), TEST_SUCCESS);
  32172. #endif
  32173. #ifdef WOLFSSL_SHA384
  32174. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha384(), testKey,
  32175. sizeof(testKey), testData, XSTRLEN(testData), testResultSha384,
  32176. sizeof(testResultSha384)), TEST_SUCCESS);
  32177. #endif
  32178. #ifdef WOLFSSL_SHA512
  32179. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha512(), testKey,
  32180. sizeof(testKey), testData, XSTRLEN(testData), testResultSha512,
  32181. sizeof(testResultSha512)), TEST_SUCCESS);
  32182. #endif
  32183. #ifdef WOLFSSL_SHA3
  32184. #ifndef WOLFSSL_NOSHA3_224
  32185. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey,
  32186. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_224,
  32187. sizeof(testResultSha3_224)), TEST_SUCCESS);
  32188. #endif
  32189. #ifndef WOLFSSL_NOSHA3_256
  32190. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey,
  32191. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_256,
  32192. sizeof(testResultSha3_256)), TEST_SUCCESS);
  32193. #endif
  32194. #ifndef WOLFSSL_NOSHA3_384
  32195. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey,
  32196. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_384,
  32197. sizeof(testResultSha3_384)), TEST_SUCCESS);
  32198. #endif
  32199. #ifndef WOLFSSL_NOSHA3_512
  32200. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey,
  32201. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_512,
  32202. sizeof(testResultSha3_512)), TEST_SUCCESS);
  32203. #endif
  32204. #endif
  32205. #endif /* OPENSSL_EXTRA */
  32206. return EXPECT_RESULT();
  32207. }
  32208. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  32209. {
  32210. EXPECT_DECLS;
  32211. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  32212. WOLFSSL_EVP_PKEY* privKey = NULL;
  32213. WOLFSSL_EVP_PKEY* pubKey = NULL;
  32214. WOLFSSL_EVP_PKEY_CTX* keyCtx = NULL;
  32215. const char testData[] = "Hi There";
  32216. WOLFSSL_EVP_MD_CTX mdCtx;
  32217. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  32218. int ret;
  32219. size_t checkSz = -1;
  32220. int sz = 2048 / 8;
  32221. const unsigned char* cp;
  32222. const unsigned char* p;
  32223. unsigned char check[2048/8];
  32224. size_t i;
  32225. int paddings[] = {
  32226. RSA_PKCS1_PADDING,
  32227. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  32228. RSA_PKCS1_PSS_PADDING,
  32229. #endif
  32230. };
  32231. cp = client_key_der_2048;
  32232. ExpectNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  32233. sizeof_client_key_der_2048)));
  32234. p = client_keypub_der_2048;
  32235. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  32236. sizeof_client_keypub_der_2048)));
  32237. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32238. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  32239. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32240. NULL, privKey), 1);
  32241. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32242. (unsigned int)XSTRLEN(testData)), 1);
  32243. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32244. ExpectIntEQ((int)checkSz, sz);
  32245. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32246. ExpectIntEQ((int)checkSz,sz);
  32247. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  32248. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  32249. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy);
  32250. ExpectIntEQ(ret, 1);
  32251. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32252. ExpectIntEQ(ret, 1);
  32253. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32254. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32255. NULL, pubKey), 1);
  32256. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32257. (unsigned int)XSTRLEN(testData)),
  32258. 1);
  32259. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32260. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32261. ExpectIntEQ(ret, 1);
  32262. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32263. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32264. NULL, privKey), 1);
  32265. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  32266. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32267. ExpectIntEQ((int)checkSz, sz);
  32268. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32269. ExpectIntEQ((int)checkSz, sz);
  32270. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  32271. (unsigned int)XSTRLEN(testData) - 4), 1);
  32272. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32273. ExpectIntEQ((int)checkSz, sz);
  32274. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32275. ExpectIntEQ(ret, 1);
  32276. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32277. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32278. NULL, pubKey), 1);
  32279. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  32280. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  32281. (unsigned int)XSTRLEN(testData) - 4),
  32282. 1);
  32283. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32284. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32285. ExpectIntEQ(ret, 1);
  32286. /* Check all signing padding types */
  32287. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  32288. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32289. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  32290. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  32291. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  32292. paddings[i]), 1);
  32293. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32294. (unsigned int)XSTRLEN(testData)), 1);
  32295. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32296. ExpectIntEQ((int)checkSz, sz);
  32297. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32298. ExpectIntEQ((int)checkSz,sz);
  32299. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32300. ExpectIntEQ(ret, 1);
  32301. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32302. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  32303. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  32304. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  32305. paddings[i]), 1);
  32306. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32307. (unsigned int)XSTRLEN(testData)), 1);
  32308. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32309. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32310. ExpectIntEQ(ret, 1);
  32311. }
  32312. wolfSSL_EVP_PKEY_free(pubKey);
  32313. wolfSSL_EVP_PKEY_free(privKey);
  32314. #endif
  32315. return EXPECT_RESULT();
  32316. }
  32317. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  32318. {
  32319. EXPECT_DECLS;
  32320. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  32321. WOLFSSL_EVP_PKEY* privKey = NULL;
  32322. WOLFSSL_EVP_PKEY* pubKey = NULL;
  32323. const char testData[] = "Hi There";
  32324. WOLFSSL_EVP_MD_CTX mdCtx;
  32325. int ret;
  32326. size_t checkSz = -1;
  32327. const unsigned char* cp;
  32328. const unsigned char* p;
  32329. unsigned char check[2048/8];
  32330. cp = ecc_clikey_der_256;
  32331. ExpectNotNull(privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  32332. sizeof_ecc_clikey_der_256));
  32333. p = ecc_clikeypub_der_256;
  32334. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  32335. sizeof_ecc_clikeypub_der_256)));
  32336. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32337. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32338. NULL, privKey), 1);
  32339. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32340. (unsigned int)XSTRLEN(testData)), 1);
  32341. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32342. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32343. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32344. ExpectIntEQ(ret, 1);
  32345. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32346. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32347. NULL, pubKey), 1);
  32348. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32349. (unsigned int)XSTRLEN(testData)),
  32350. 1);
  32351. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32352. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32353. ExpectIntEQ(ret, 1);
  32354. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32355. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32356. NULL, privKey), 1);
  32357. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  32358. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32359. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32360. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  32361. (unsigned int)XSTRLEN(testData) - 4), 1);
  32362. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32363. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32364. ExpectIntEQ(ret, 1);
  32365. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32366. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32367. NULL, pubKey), 1);
  32368. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  32369. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  32370. (unsigned int)XSTRLEN(testData) - 4),
  32371. 1);
  32372. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32373. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32374. ExpectIntEQ(ret, 1);
  32375. wolfSSL_EVP_PKEY_free(pubKey);
  32376. wolfSSL_EVP_PKEY_free(privKey);
  32377. #endif
  32378. return EXPECT_RESULT();
  32379. }
  32380. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  32381. {
  32382. EXPECT_DECLS;
  32383. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32384. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  32385. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32386. char caFile[] = "./certs/client-ca.pem";
  32387. char clientFile[] = "./certs/client-cert.pem";
  32388. SSL_CTX* ctx = NULL;
  32389. X509* x509 = NULL;
  32390. BIO *bio = NULL;
  32391. X509 *cert = NULL;
  32392. X509 *ca = NULL;
  32393. STACK_OF(X509) *chain = NULL;
  32394. STACK_OF(X509) *chain2 = NULL;
  32395. #ifndef NO_WOLFSSL_SERVER
  32396. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32397. #else
  32398. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32399. #endif
  32400. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  32401. WOLFSSL_FILETYPE_PEM));
  32402. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  32403. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  32404. WOLFSSL_FILETYPE_PEM));
  32405. /* additional test of getting EVP_PKEY key size from X509
  32406. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  32407. * allowed with user RSA */
  32408. {
  32409. EVP_PKEY* pkey = NULL;
  32410. #if defined(HAVE_ECC)
  32411. X509* ecX509 = NULL;
  32412. #endif /* HAVE_ECC */
  32413. ExpectNotNull(pkey = X509_get_pubkey(x509));
  32414. /* current RSA key is 2048 bit (256 bytes) */
  32415. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  32416. EVP_PKEY_free(pkey);
  32417. pkey = NULL;
  32418. #if defined(HAVE_ECC)
  32419. #if defined(USE_CERT_BUFFERS_256)
  32420. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  32421. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  32422. SSL_FILETYPE_ASN1));
  32423. #else
  32424. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  32425. cliEccCertFile, SSL_FILETYPE_PEM));
  32426. #endif
  32427. pkey = X509_get_pubkey(ecX509);
  32428. ExpectNotNull(pkey);
  32429. /* current ECC key is 256 bit (32 bytes) */
  32430. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  32431. X509_free(ecX509);
  32432. ecX509 = NULL;
  32433. EVP_PKEY_free(pkey);
  32434. pkey = NULL;
  32435. #endif /* HAVE_ECC */
  32436. }
  32437. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  32438. if (EXPECT_SUCCESS()) {
  32439. x509 = NULL;
  32440. }
  32441. #ifdef WOLFSSL_ENCRYPTED_KEYS
  32442. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  32443. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  32444. #endif
  32445. SSL_CTX_free(ctx);
  32446. ctx = NULL;
  32447. #ifndef NO_WOLFSSL_SERVER
  32448. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32449. #else
  32450. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32451. #endif
  32452. /* Test haproxy use case */
  32453. ExpectNotNull(bio = BIO_new_file(svrCertFile, "r"));
  32454. /* Read Certificate */
  32455. ExpectNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  32456. ExpectNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  32457. ExpectNotNull(chain = sk_X509_new_null());
  32458. ExpectIntEQ(sk_X509_push(chain, ca), 1);
  32459. if (EXPECT_SUCCESS()) {
  32460. ca = NULL;
  32461. }
  32462. ExpectNotNull(chain2 = X509_chain_up_ref(chain));
  32463. ExpectNotNull(ca = sk_X509_shift(chain2));
  32464. ExpectIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  32465. ExpectIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  32466. if (EXPECT_SUCCESS()) {
  32467. ca = NULL;
  32468. }
  32469. BIO_free(bio);
  32470. X509_free(cert);
  32471. X509_free(ca);
  32472. X509_free(x509);
  32473. sk_X509_pop_free(chain, X509_free);
  32474. sk_X509_pop_free(chain2, X509_free);
  32475. SSL_CTX_free(ctx);
  32476. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32477. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32478. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  32479. return EXPECT_RESULT();
  32480. }
  32481. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  32482. static int test_wolfSSL_ERR_peek_last_error_line(void)
  32483. {
  32484. EXPECT_DECLS;
  32485. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32486. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  32487. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  32488. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  32489. callback_functions client_cb;
  32490. callback_functions server_cb;
  32491. int line = 0;
  32492. int flag = ERR_TXT_STRING;
  32493. const char* file = NULL;
  32494. const char* data = NULL;
  32495. /* create a failed connection and inspect the error */
  32496. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  32497. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  32498. client_cb.method = wolfTLSv1_1_client_method;
  32499. server_cb.method = wolfTLSv1_2_server_method;
  32500. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  32501. ExpectIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  32502. ExpectNotNull(data);
  32503. /* check clearing error state */
  32504. ERR_remove_state(0);
  32505. ExpectIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  32506. ERR_peek_last_error_line(NULL, &line);
  32507. ExpectIntEQ(line, 0);
  32508. ERR_peek_last_error_line(&file, NULL);
  32509. ExpectNull(file);
  32510. /* retry connection to fill error queue */
  32511. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  32512. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  32513. client_cb.method = wolfTLSv1_1_client_method;
  32514. server_cb.method = wolfTLSv1_2_server_method;
  32515. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  32516. /* check that error code was stored */
  32517. ExpectIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  32518. ERR_peek_last_error_line(NULL, &line);
  32519. ExpectIntNE(line, 0);
  32520. ERR_peek_last_error_line(&file, NULL);
  32521. ExpectNotNull(file);
  32522. fprintf(stderr, "\nTesting error print out\n");
  32523. ERR_print_errors_fp(stderr);
  32524. fprintf(stderr, "Done testing print out\n\n");
  32525. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  32526. * !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  32527. return EXPECT_RESULT();
  32528. }
  32529. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  32530. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32531. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32532. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  32533. {
  32534. (void) ok;
  32535. (void) ctx;
  32536. fprintf(stderr, "ENTER verify_cb\n");
  32537. return SSL_SUCCESS;
  32538. }
  32539. #endif
  32540. static int test_wolfSSL_X509_Name_canon(void)
  32541. {
  32542. EXPECT_DECLS;
  32543. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32544. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  32545. defined(WOLFSSL_CERT_GEN) && \
  32546. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  32547. const long ex_hash1 = 0x0fdb2da4;
  32548. const long ex_hash2 = 0x9f3e8c9e;
  32549. X509_NAME *name = NULL;
  32550. X509 *x509 = NULL;
  32551. XFILE file = XBADFILE;
  32552. unsigned long hash = 0;
  32553. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  32554. byte *pbuf = NULL;
  32555. word32 len = 0;
  32556. (void) ex_hash2;
  32557. ExpectTrue((file = XFOPEN(caCertFile, "rb")) != XBADFILE);
  32558. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  32559. ExpectNotNull(name = X509_get_issuer_name(x509));
  32560. /* When output buffer is NULL, should return necessary output buffer
  32561. * length.*/
  32562. ExpectIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  32563. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  32564. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  32565. hash = (((unsigned long)digest[3] << 24) |
  32566. ((unsigned long)digest[2] << 16) |
  32567. ((unsigned long)digest[1] << 8) |
  32568. ((unsigned long)digest[0]));
  32569. ExpectIntEQ(hash, ex_hash1);
  32570. if (file != XBADFILE) {
  32571. XFCLOSE(file);
  32572. file = XBADFILE;
  32573. }
  32574. X509_free(x509);
  32575. x509 = NULL;
  32576. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  32577. pbuf = NULL;
  32578. ExpectTrue((file = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  32579. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  32580. ExpectNotNull(name = X509_get_issuer_name(x509));
  32581. ExpectIntGT((len = wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  32582. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  32583. hash = (((unsigned long)digest[3] << 24) |
  32584. ((unsigned long)digest[2] << 16) |
  32585. ((unsigned long)digest[1] << 8) |
  32586. ((unsigned long)digest[0]));
  32587. ExpectIntEQ(hash, ex_hash2);
  32588. if (file != XBADFILE)
  32589. XFCLOSE(file);
  32590. X509_free(x509);
  32591. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  32592. #endif
  32593. return EXPECT_RESULT();
  32594. }
  32595. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  32596. {
  32597. EXPECT_DECLS;
  32598. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  32599. const int MAX_DIR = 4;
  32600. const char paths[][32] = {
  32601. "./certs/ed25519",
  32602. "./certs/ecc",
  32603. "./certs/crl",
  32604. "./certs/",
  32605. };
  32606. char CertCrl_path[MAX_FILENAME_SZ];
  32607. char *p;
  32608. X509_STORE* str = NULL;
  32609. X509_LOOKUP* lookup = NULL;
  32610. WOLFSSL_STACK* sk = NULL;
  32611. int len, total_len, i;
  32612. (void)sk;
  32613. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  32614. /* illegal string */
  32615. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32616. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32617. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  32618. SSL_FILETYPE_PEM,NULL), 0);
  32619. /* free store */
  32620. X509_STORE_free(str);
  32621. str = NULL;
  32622. /* short folder string */
  32623. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32624. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32625. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  32626. SSL_FILETYPE_PEM,NULL), 1);
  32627. #if defined(WOLFSSL_INT_H)
  32628. /* only available when including internal.h */
  32629. ExpectNotNull(sk = lookup->dirs->dir_entry);
  32630. #endif
  32631. /* free store */
  32632. X509_STORE_free(str);
  32633. str = NULL;
  32634. /* typical function check */
  32635. p = &CertCrl_path[0];
  32636. total_len = 0;
  32637. for (i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  32638. len = (int)XSTRLEN((const char*)&paths[i]);
  32639. total_len += len;
  32640. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  32641. p += len;
  32642. if (i != 0) *(p++) = SEPARATOR_CHAR;
  32643. }
  32644. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32645. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32646. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, CertCrl_path,
  32647. SSL_FILETYPE_PEM,NULL), 1);
  32648. #if defined(WOLFSSL_INT_H)
  32649. /* only available when including internal.h */
  32650. ExpectNotNull(sk = lookup->dirs->dir_entry);
  32651. #endif
  32652. X509_STORE_free(str);
  32653. #endif
  32654. return EXPECT_RESULT();
  32655. }
  32656. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  32657. {
  32658. EXPECT_DECLS;
  32659. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32660. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  32661. defined(WOLFSSL_SIGNER_DER_CERT)
  32662. X509_STORE_CTX* ctx = NULL;
  32663. X509_STORE* str = NULL;
  32664. X509_LOOKUP* lookup = NULL;
  32665. X509* cert1 = NULL;
  32666. X509* x509Ca = NULL;
  32667. X509* x509Svr = NULL;
  32668. X509* issuer = NULL;
  32669. WOLFSSL_STACK* sk = NULL;
  32670. X509_NAME* caName = NULL;
  32671. X509_NAME* issuerName = NULL;
  32672. XFILE file1 = XBADFILE;
  32673. int i;
  32674. int cert_count = 0;
  32675. int cmp;
  32676. char der[] = "certs/ca-cert.der";
  32677. #ifdef HAVE_CRL
  32678. char pem[][100] = {
  32679. "./certs/crl/crl.pem",
  32680. "./certs/crl/crl2.pem",
  32681. "./certs/crl/caEccCrl.pem",
  32682. "./certs/crl/eccCliCRL.pem",
  32683. "./certs/crl/eccSrvCRL.pem",
  32684. ""
  32685. };
  32686. #endif
  32687. ExpectTrue((file1 = XFOPEN("./certs/ca-cert.pem", "rb")) != XBADFILE);
  32688. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  32689. if (file1 != XBADFILE)
  32690. XFCLOSE(file1);
  32691. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32692. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32693. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32694. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  32695. SSL_FILETYPE_PEM,NULL), 1);
  32696. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  32697. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  32698. /* check if CA cert is loaded into the store */
  32699. for (i = 0; i < cert_count; i++) {
  32700. x509Ca = sk_X509_value(sk, i);
  32701. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  32702. }
  32703. ExpectNotNull((x509Svr =
  32704. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32705. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  32706. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  32707. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  32708. ExpectNotNull(issuer);
  32709. caName = X509_get_subject_name(x509Ca);
  32710. ExpectNotNull(caName);
  32711. issuerName = X509_get_subject_name(issuer);
  32712. ExpectNotNull(issuerName);
  32713. cmp = X509_NAME_cmp(caName, issuerName);
  32714. ExpectIntEQ(cmp, 0);
  32715. /* load der format */
  32716. X509_free(issuer);
  32717. issuer = NULL;
  32718. X509_STORE_CTX_free(ctx);
  32719. ctx = NULL;
  32720. X509_STORE_free(str);
  32721. str = NULL;
  32722. sk_X509_pop_free(sk, NULL);
  32723. sk = NULL;
  32724. X509_free(x509Svr);
  32725. x509Svr = NULL;
  32726. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32727. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32728. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  32729. SSL_FILETYPE_ASN1,NULL), 1);
  32730. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  32731. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  32732. /* check if CA cert is loaded into the store */
  32733. for (i = 0; i < cert_count; i++) {
  32734. x509Ca = sk_X509_value(sk, i);
  32735. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  32736. }
  32737. X509_STORE_free(str);
  32738. str = NULL;
  32739. sk_X509_pop_free(sk, NULL);
  32740. sk = NULL;
  32741. X509_free(cert1);
  32742. cert1 = NULL;
  32743. #ifdef HAVE_CRL
  32744. ExpectNotNull(str = wolfSSL_X509_STORE_new());
  32745. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32746. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  32747. SSL_FILETYPE_PEM,NULL), 1);
  32748. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  32749. "certs/server-revoked-cert.pem",
  32750. SSL_FILETYPE_PEM,NULL), 1);
  32751. if (str) {
  32752. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  32753. WOLFSSL_FILETYPE_PEM), 1);
  32754. /* since store hasn't yet known the revoked cert*/
  32755. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  32756. "certs/server-revoked-cert.pem",
  32757. WOLFSSL_FILETYPE_PEM), 1);
  32758. }
  32759. for (i = 0; pem[i][0] != '\0'; i++)
  32760. {
  32761. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  32762. SSL_FILETYPE_PEM, NULL), 1);
  32763. }
  32764. if (str) {
  32765. /* since store knows crl list */
  32766. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  32767. "certs/server-revoked-cert.pem",
  32768. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  32769. }
  32770. ExpectIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  32771. X509_STORE_free(str);
  32772. #endif
  32773. #endif
  32774. return EXPECT_RESULT();
  32775. }
  32776. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  32777. {
  32778. int res = TEST_SKIPPED;
  32779. #if defined(OPENSSL_EXTRA)
  32780. X509_STORE_CTX_cleanup(NULL);
  32781. X509_STORE_CTX_trusted_stack(NULL, NULL);
  32782. res = TEST_SUCCESS;
  32783. #endif
  32784. return res;
  32785. }
  32786. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  32787. {
  32788. EXPECT_DECLS;
  32789. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32790. X509_STORE_CTX* ctx = NULL;
  32791. X509_STORE* str = NULL;
  32792. X509* x509Ca = NULL;
  32793. X509* x509Svr = NULL;
  32794. X509* issuer = NULL;
  32795. X509_NAME* caName = NULL;
  32796. X509_NAME* issuerName = NULL;
  32797. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32798. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32799. ExpectNotNull((x509Ca =
  32800. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  32801. ExpectIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  32802. ExpectNotNull((x509Svr =
  32803. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32804. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  32805. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  32806. ExpectNotNull(issuer = X509_STORE_CTX_get0_current_issuer(ctx));
  32807. ExpectNotNull(caName = X509_get_subject_name(x509Ca));
  32808. ExpectNotNull(issuerName = X509_get_subject_name(issuer));
  32809. #ifdef WOLFSSL_SIGNER_DER_CERT
  32810. ExpectIntEQ(X509_NAME_cmp(caName, issuerName), 0);
  32811. #endif
  32812. X509_free(issuer);
  32813. X509_STORE_CTX_free(ctx);
  32814. X509_free(x509Svr);
  32815. X509_STORE_free(str);
  32816. X509_free(x509Ca);
  32817. #endif
  32818. return EXPECT_RESULT();
  32819. }
  32820. static int test_wolfSSL_PKCS7_certs(void)
  32821. {
  32822. EXPECT_DECLS;
  32823. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  32824. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  32825. STACK_OF(X509)* sk = NULL;
  32826. STACK_OF(X509_INFO)* info_sk = NULL;
  32827. PKCS7 *p7 = NULL;
  32828. BIO* bio = NULL;
  32829. const byte* p = NULL;
  32830. int buflen = 0;
  32831. int i;
  32832. /* Test twice. Once with d2i and once without to test
  32833. * that everything is free'd correctly. */
  32834. for (i = 0; i < 2; i++) {
  32835. ExpectNotNull(p7 = PKCS7_new());
  32836. if (p7 != NULL) {
  32837. p7->version = 1;
  32838. #ifdef NO_SHA
  32839. p7->hashOID = SHA256h;
  32840. #else
  32841. p7->hashOID = SHAh;
  32842. #endif
  32843. }
  32844. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  32845. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  32846. ExpectNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  32847. ExpectIntEQ(sk_X509_INFO_num(info_sk), 2);
  32848. ExpectNotNull(sk = sk_X509_new_null());
  32849. while (EXPECT_SUCCESS() && (sk_X509_INFO_num(info_sk) > 0)) {
  32850. X509_INFO* info = NULL;
  32851. ExpectNotNull(info = sk_X509_INFO_shift(info_sk));
  32852. ExpectIntEQ(sk_X509_push(sk, info->x509), 1);
  32853. if (EXPECT_SUCCESS() && (info != NULL)) {
  32854. info->x509 = NULL;
  32855. }
  32856. X509_INFO_free(info);
  32857. }
  32858. sk_X509_INFO_pop_free(info_sk, X509_INFO_free);
  32859. info_sk = NULL;
  32860. BIO_free(bio);
  32861. bio = NULL;
  32862. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  32863. ExpectIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  32864. if ((sk != NULL) && ((p7 == NULL) || (bio == NULL))) {
  32865. sk_X509_pop_free(sk, X509_free);
  32866. }
  32867. sk = NULL;
  32868. ExpectIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  32869. if (i == 0) {
  32870. PKCS7_free(p7);
  32871. p7 = NULL;
  32872. ExpectNotNull(d2i_PKCS7(&p7, &p, buflen));
  32873. if (p7 != NULL) {
  32874. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate
  32875. * them */
  32876. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  32877. }
  32878. /* PKCS7_free free's the certs */
  32879. ExpectNotNull(wolfSSL_PKCS7_to_stack(p7));
  32880. }
  32881. BIO_free(bio);
  32882. bio = NULL;
  32883. PKCS7_free(p7);
  32884. p7 = NULL;
  32885. }
  32886. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32887. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  32888. return EXPECT_RESULT();
  32889. }
  32890. static int test_wolfSSL_X509_STORE_CTX(void)
  32891. {
  32892. EXPECT_DECLS;
  32893. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32894. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32895. X509_STORE_CTX* ctx = NULL;
  32896. X509_STORE* str = NULL;
  32897. X509* x509 = NULL;
  32898. #ifdef OPENSSL_ALL
  32899. X509* x5092 = NULL;
  32900. STACK_OF(X509) *sk = NULL;
  32901. STACK_OF(X509) *sk2 = NULL;
  32902. STACK_OF(X509) *sk3 = NULL;
  32903. #endif
  32904. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32905. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32906. ExpectNotNull((x509 =
  32907. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32908. ExpectIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  32909. #ifdef OPENSSL_ALL
  32910. /* sk_X509_new only in OPENSSL_ALL */
  32911. sk = sk_X509_new_null();
  32912. ExpectNotNull(sk);
  32913. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  32914. #else
  32915. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  32916. #endif
  32917. ExpectIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  32918. X509_STORE_CTX_set_error(ctx, -5);
  32919. X509_STORE_CTX_set_error(NULL, -5);
  32920. X509_STORE_CTX_free(ctx);
  32921. ctx = NULL;
  32922. #ifdef OPENSSL_ALL
  32923. sk_X509_pop_free(sk, NULL);
  32924. sk = NULL;
  32925. #endif
  32926. X509_STORE_free(str);
  32927. str = NULL;
  32928. X509_free(x509);
  32929. x509 = NULL;
  32930. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32931. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  32932. X509_STORE_CTX_free(ctx);
  32933. ctx = NULL;
  32934. #ifdef OPENSSL_ALL
  32935. /* test X509_STORE_CTX_get(1)_chain */
  32936. ExpectNotNull((x509 = X509_load_certificate_file(svrCertFile,
  32937. SSL_FILETYPE_PEM)));
  32938. ExpectNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  32939. SSL_FILETYPE_PEM)));
  32940. ExpectNotNull((sk = sk_X509_new_null()));
  32941. ExpectIntEQ(sk_X509_push(sk, x509), 1);
  32942. if (EXPECT_FAIL()) {
  32943. X509_free(x509);
  32944. x509 = NULL;
  32945. }
  32946. ExpectNotNull((str = X509_STORE_new()));
  32947. ExpectNotNull((ctx = X509_STORE_CTX_new()));
  32948. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  32949. ExpectNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  32950. ExpectNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  32951. ExpectIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  32952. ExpectNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  32953. ExpectNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  32954. ExpectIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  32955. X509_STORE_CTX_free(ctx);
  32956. ctx = NULL;
  32957. X509_STORE_free(str);
  32958. str = NULL;
  32959. /* CTX certs not freed yet */
  32960. X509_free(x5092);
  32961. x5092 = NULL;
  32962. sk_X509_pop_free(sk, NULL);
  32963. sk = NULL;
  32964. /* sk3 is dup so free here */
  32965. sk_X509_pop_free(sk3, NULL);
  32966. sk3 = NULL;
  32967. #endif
  32968. /* test X509_STORE_CTX_get/set_ex_data */
  32969. {
  32970. int i = 0, tmpData = 5;
  32971. void* tmpDataRet;
  32972. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32973. #ifdef HAVE_EX_DATA
  32974. for (i = 0; i < MAX_EX_DATA; i++) {
  32975. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  32976. WOLFSSL_SUCCESS);
  32977. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  32978. ExpectNotNull(tmpDataRet);
  32979. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  32980. }
  32981. #else
  32982. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  32983. WOLFSSL_FAILURE);
  32984. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  32985. ExpectNull(tmpDataRet);
  32986. #endif
  32987. X509_STORE_CTX_free(ctx);
  32988. ctx = NULL;
  32989. }
  32990. /* test X509_STORE_get/set_ex_data */
  32991. {
  32992. int i = 0, tmpData = 99;
  32993. void* tmpDataRet;
  32994. ExpectNotNull(str = X509_STORE_new());
  32995. #ifdef HAVE_EX_DATA
  32996. for (i = 0; i < MAX_EX_DATA; i++) {
  32997. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  32998. WOLFSSL_SUCCESS);
  32999. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  33000. ExpectNotNull(tmpDataRet);
  33001. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  33002. }
  33003. #else
  33004. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  33005. WOLFSSL_FAILURE);
  33006. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  33007. ExpectNull(tmpDataRet);
  33008. #endif
  33009. X509_STORE_free(str);
  33010. str = NULL;
  33011. }
  33012. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33013. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33014. return EXPECT_RESULT();
  33015. }
  33016. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33017. static int test_X509_STORE_untrusted_load_cert_to_stack(const char* filename,
  33018. STACK_OF(X509)* chain)
  33019. {
  33020. EXPECT_DECLS;
  33021. XFILE fp = XBADFILE;
  33022. X509* cert = NULL;
  33023. ExpectTrue((fp = XFOPEN(filename, "rb"))
  33024. != XBADFILE);
  33025. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  33026. if (fp != XBADFILE) {
  33027. XFCLOSE(fp);
  33028. fp = XBADFILE;
  33029. }
  33030. ExpectIntEQ(sk_X509_push(chain, cert), 1);
  33031. if (EXPECT_FAIL())
  33032. X509_free(cert);
  33033. return EXPECT_RESULT();
  33034. }
  33035. static int test_X509_STORE_untrusted_certs(const char** filenames, int ret,
  33036. int err, int loadCA)
  33037. {
  33038. EXPECT_DECLS;
  33039. X509_STORE_CTX* ctx = NULL;
  33040. X509_STORE* str = NULL;
  33041. XFILE fp = XBADFILE;
  33042. X509* cert = NULL;
  33043. STACK_OF(X509)* untrusted = NULL;
  33044. ExpectTrue((fp = XFOPEN("./certs/intermediate/server-int-cert.pem", "rb"))
  33045. != XBADFILE);
  33046. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  33047. if (fp != XBADFILE) {
  33048. XFCLOSE(fp);
  33049. fp = XBADFILE;
  33050. }
  33051. ExpectNotNull(str = X509_STORE_new());
  33052. ExpectNotNull(ctx = X509_STORE_CTX_new());
  33053. ExpectNotNull(untrusted = sk_X509_new_null());
  33054. ExpectIntEQ(X509_STORE_set_flags(str, 0), 1);
  33055. if (loadCA) {
  33056. ExpectIntEQ(X509_STORE_load_locations(str, "./certs/ca-cert.pem", NULL),
  33057. 1);
  33058. }
  33059. for (; *filenames; filenames++) {
  33060. ExpectIntEQ(test_X509_STORE_untrusted_load_cert_to_stack(*filenames,
  33061. untrusted), TEST_SUCCESS);
  33062. }
  33063. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, cert, untrusted), 1);
  33064. ExpectIntEQ(X509_verify_cert(ctx), ret);
  33065. ExpectIntEQ(X509_STORE_CTX_get_error(ctx), err);
  33066. X509_free(cert);
  33067. X509_STORE_free(str);
  33068. X509_STORE_CTX_free(ctx);
  33069. sk_X509_pop_free(untrusted, NULL);
  33070. return EXPECT_RESULT();
  33071. }
  33072. #endif
  33073. static int test_X509_STORE_untrusted(void)
  33074. {
  33075. EXPECT_DECLS;
  33076. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33077. const char* untrusted1[] = {
  33078. "./certs/intermediate/ca-int2-cert.pem",
  33079. NULL
  33080. };
  33081. const char* untrusted2[] = {
  33082. "./certs/intermediate/ca-int-cert.pem",
  33083. "./certs/intermediate/ca-int2-cert.pem",
  33084. NULL
  33085. };
  33086. const char* untrusted3[] = {
  33087. "./certs/intermediate/ca-int-cert.pem",
  33088. "./certs/intermediate/ca-int2-cert.pem",
  33089. "./certs/ca-cert.pem",
  33090. NULL
  33091. };
  33092. /* Adding unrelated certs that should be ignored */
  33093. const char* untrusted4[] = {
  33094. "./certs/client-ca.pem",
  33095. "./certs/intermediate/ca-int-cert.pem",
  33096. "./certs/server-cert.pem",
  33097. "./certs/intermediate/ca-int2-cert.pem",
  33098. NULL
  33099. };
  33100. /* Only immediate issuer in untrusted chain. Fails since can't build chain
  33101. * to loaded CA. */
  33102. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted1, 0,
  33103. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 1), TEST_SUCCESS);
  33104. /* Succeeds because path to loaded CA is available. */
  33105. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted2, 1, 0, 1),
  33106. TEST_SUCCESS);
  33107. /* Fails because root CA is in the untrusted stack */
  33108. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted3, 0,
  33109. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 0), TEST_SUCCESS);
  33110. /* Succeeds because path to loaded CA is available. */
  33111. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted4, 1, 0, 1),
  33112. TEST_SUCCESS);
  33113. #endif
  33114. return EXPECT_RESULT();
  33115. }
  33116. static int test_wolfSSL_X509_STORE_set_flags(void)
  33117. {
  33118. EXPECT_DECLS;
  33119. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33120. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33121. X509_STORE* store = NULL;
  33122. X509* x509 = NULL;
  33123. ExpectNotNull((store = wolfSSL_X509_STORE_new()));
  33124. ExpectNotNull((x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  33125. WOLFSSL_FILETYPE_PEM)));
  33126. ExpectIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  33127. #ifdef HAVE_CRL
  33128. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  33129. WOLFSSL_SUCCESS);
  33130. #else
  33131. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  33132. NOT_COMPILED_IN);
  33133. #endif
  33134. wolfSSL_X509_free(x509);
  33135. wolfSSL_X509_STORE_free(store);
  33136. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  33137. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33138. return EXPECT_RESULT();
  33139. }
  33140. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  33141. {
  33142. EXPECT_DECLS;
  33143. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  33144. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  33145. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  33146. WOLFSSL_X509_STORE* store = NULL;
  33147. WOLFSSL_X509_LOOKUP* lookup = NULL;
  33148. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  33149. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  33150. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  33151. X509_FILETYPE_PEM), 1);
  33152. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  33153. X509_FILETYPE_PEM), 1);
  33154. if (store != NULL) {
  33155. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  33156. WOLFSSL_FILETYPE_PEM), 1);
  33157. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  33158. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  33159. }
  33160. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  33161. X509_FILETYPE_PEM), 1);
  33162. if (store != NULL) {
  33163. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  33164. WOLFSSL_FILETYPE_PEM), 1);
  33165. }
  33166. wolfSSL_X509_STORE_free(store);
  33167. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) &&
  33168. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33169. return EXPECT_RESULT();
  33170. }
  33171. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  33172. {
  33173. EXPECT_DECLS;
  33174. #if defined(OPENSSL_EXTRA)
  33175. WOLFSSL_X509_STORE_CTX* ctx = NULL;
  33176. time_t c_time;
  33177. ExpectNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  33178. c_time = 365*24*60*60;
  33179. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  33180. ExpectTrue((ctx->param->flags & WOLFSSL_USE_CHECK_TIME) ==
  33181. WOLFSSL_USE_CHECK_TIME);
  33182. ExpectTrue(ctx->param->check_time == c_time);
  33183. wolfSSL_X509_STORE_CTX_free(ctx);
  33184. #endif /* OPENSSL_EXTRA */
  33185. return EXPECT_RESULT();
  33186. }
  33187. static int test_wolfSSL_CTX_get0_set1_param(void)
  33188. {
  33189. EXPECT_DECLS;
  33190. #if defined(OPENSSL_EXTRA)
  33191. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33192. SSL_CTX* ctx = NULL;
  33193. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  33194. WOLFSSL_X509_VERIFY_PARAM* pvpm = NULL;
  33195. char testIPv4[] = "127.0.0.1";
  33196. char testhostName[] = "foo.hoge.com";
  33197. #ifndef NO_WOLFSSL_SERVER
  33198. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33199. #else
  33200. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33201. #endif
  33202. ExpectNull(SSL_CTX_get0_param(NULL));
  33203. ExpectNotNull(pParam = SSL_CTX_get0_param(ctx));
  33204. ExpectNotNull(pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  33205. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL));
  33206. ExpectNotNull(XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM)));
  33207. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  33208. (int)XSTRLEN(testhostName)), WOLFSSL_SUCCESS);
  33209. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4),
  33210. WOLFSSL_SUCCESS);
  33211. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  33212. ExpectIntEQ(SSL_CTX_set1_param(ctx, pvpm), 1);
  33213. ExpectIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  33214. (int)XSTRLEN(testhostName)));
  33215. ExpectIntEQ(0x01, pParam->hostFlags);
  33216. ExpectIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  33217. /* test for incorrect parameter */
  33218. ExpectIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  33219. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  33220. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  33221. SSL_CTX_free(ctx);
  33222. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  33223. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33224. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  33225. return EXPECT_RESULT();
  33226. }
  33227. static int test_wolfSSL_get0_param(void)
  33228. {
  33229. EXPECT_DECLS;
  33230. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33231. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33232. SSL_CTX* ctx = NULL;
  33233. SSL* ssl = NULL;
  33234. #ifndef NO_WOLFSSL_SERVER
  33235. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33236. #else
  33237. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33238. #endif
  33239. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33240. SSL_FILETYPE_PEM));
  33241. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33242. ExpectNotNull(ssl = SSL_new(ctx));
  33243. ExpectNotNull(SSL_get0_param(ssl));
  33244. SSL_free(ssl);
  33245. SSL_CTX_free(ctx);
  33246. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33247. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  33248. return EXPECT_RESULT();
  33249. }
  33250. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  33251. {
  33252. EXPECT_DECLS;
  33253. #if defined(OPENSSL_EXTRA)
  33254. const char host[] = "www.example.com";
  33255. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  33256. ExpectNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  33257. sizeof(WOLFSSL_X509_VERIFY_PARAM), HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  33258. if (pParam != NULL) {
  33259. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  33260. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  33261. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33262. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  33263. ExpectIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33264. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  33265. }
  33266. #endif /* OPENSSL_EXTRA */
  33267. return EXPECT_RESULT();
  33268. }
  33269. static int test_wolfSSL_set1_host(void)
  33270. {
  33271. EXPECT_DECLS;
  33272. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33273. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33274. const char host[] = "www.test_wolfSSL_set1_host.com";
  33275. const char emptyStr[] = "";
  33276. SSL_CTX* ctx = NULL;
  33277. SSL* ssl = NULL;
  33278. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  33279. #ifndef NO_WOLFSSL_SERVER
  33280. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33281. #else
  33282. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33283. #endif
  33284. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33285. SSL_FILETYPE_PEM));
  33286. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33287. ExpectNotNull(ssl = SSL_new(ctx));
  33288. pParam = SSL_get0_param(ssl);
  33289. /* we should get back host string */
  33290. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  33291. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33292. /* we should get back empty string */
  33293. ExpectIntEQ(SSL_set1_host(ssl, emptyStr), WOLFSSL_SUCCESS);
  33294. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  33295. /* we should get back host string */
  33296. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  33297. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33298. /* we should get back empty string */
  33299. ExpectIntEQ(SSL_set1_host(ssl, NULL), WOLFSSL_SUCCESS);
  33300. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  33301. SSL_free(ssl);
  33302. SSL_CTX_free(ctx);
  33303. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33304. #endif /* OPENSSL_EXTRA */
  33305. return EXPECT_RESULT();
  33306. }
  33307. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  33308. {
  33309. EXPECT_DECLS;
  33310. #if defined(OPENSSL_EXTRA)
  33311. unsigned char buf[16] = {0};
  33312. WOLFSSL_X509_VERIFY_PARAM* param = NULL;
  33313. ExpectNotNull(param = X509_VERIFY_PARAM_new());
  33314. /* test 127.0.0.1 */
  33315. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  33316. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  33317. ExpectIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  33318. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  33319. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33320. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  33321. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  33322. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  33323. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33324. ExpectIntEQ(XSTRNCMP(param->ipasc,
  33325. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  33326. /* test 2001:db8:: */
  33327. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33328. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33329. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33330. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  33331. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33332. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  33333. /* test ::1234:5678 */
  33334. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  33335. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33336. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33337. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  33338. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33339. ExpectIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  33340. /* test 2001:db8::1234:5678 */
  33341. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33342. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33343. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33344. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  33345. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33346. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  33347. sizeof(param->ipasc)), 0);
  33348. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  33349. /* 2001:db8:1::ab9:c0a8:102 */
  33350. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33351. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  33352. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  33353. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  33354. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33355. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  33356. sizeof(param->ipasc)), 0);
  33357. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  33358. #endif /* OPENSSL_EXTRA */
  33359. return EXPECT_RESULT();
  33360. }
  33361. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  33362. {
  33363. EXPECT_DECLS;
  33364. #if defined(OPENSSL_EXTRA)
  33365. X509_STORE* store = NULL;
  33366. X509_STORE_CTX* ctx = NULL;
  33367. X509_STORE_CTX* ctx_no_init = NULL;
  33368. ExpectNotNull((store = X509_STORE_new()));
  33369. ExpectNotNull(ctx = X509_STORE_CTX_new());
  33370. ExpectNotNull(ctx_no_init = X509_STORE_CTX_new());
  33371. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  33372. ExpectNull(X509_STORE_CTX_get0_store(NULL));
  33373. /* should return NULL if ctx has not bee initialized */
  33374. ExpectNull(X509_STORE_CTX_get0_store(ctx_no_init));
  33375. ExpectNotNull(X509_STORE_CTX_get0_store(ctx));
  33376. wolfSSL_X509_STORE_CTX_free(ctx);
  33377. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  33378. X509_STORE_free(store);
  33379. #endif /* OPENSSL_EXTRA */
  33380. return EXPECT_RESULT();
  33381. }
  33382. static int test_wolfSSL_CTX_set_client_CA_list(void)
  33383. {
  33384. EXPECT_DECLS;
  33385. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  33386. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  33387. WOLFSSL_CTX* ctx = NULL;
  33388. WOLFSSL* ssl = NULL;
  33389. X509_NAME* name = NULL;
  33390. STACK_OF(X509_NAME)* names = NULL;
  33391. STACK_OF(X509_NAME)* ca_list = NULL;
  33392. int names_len = 0;
  33393. int i;
  33394. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  33395. /* Send two X501 names in cert request */
  33396. names = SSL_load_client_CA_file(cliCertFile);
  33397. ExpectNotNull(names);
  33398. ca_list = SSL_load_client_CA_file(caCertFile);
  33399. ExpectNotNull(ca_list);
  33400. ExpectNotNull(name = sk_X509_NAME_value(ca_list, 0));
  33401. ExpectIntEQ(sk_X509_NAME_push(names, name), 1);
  33402. if (EXPECT_FAIL()) {
  33403. wolfSSL_X509_NAME_free(name);
  33404. name = NULL;
  33405. }
  33406. SSL_CTX_set_client_CA_list(ctx, names);
  33407. /* This should only free the stack structure */
  33408. sk_X509_NAME_free(ca_list);
  33409. ca_list = NULL;
  33410. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  33411. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  33412. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  33413. for (i = 0; i < names_len; i++) {
  33414. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  33415. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  33416. }
  33417. /* Needed to be able to create ssl object */
  33418. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33419. SSL_FILETYPE_PEM));
  33420. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33421. ExpectNotNull(ssl = wolfSSL_new(ctx));
  33422. /* load again as old names are responsibility of ctx to free*/
  33423. names = SSL_load_client_CA_file(cliCertFile);
  33424. ExpectNotNull(names);
  33425. SSL_set_client_CA_list(ssl, names);
  33426. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl));
  33427. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  33428. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  33429. for (i = 0; i < names_len; i++) {
  33430. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  33431. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  33432. }
  33433. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  33434. {
  33435. tcp_ready ready;
  33436. func_args server_args;
  33437. callback_functions server_cb;
  33438. THREAD_TYPE serverThread;
  33439. WOLFSSL* ssl_client = NULL;
  33440. WOLFSSL_CTX* ctx_client = NULL;
  33441. SOCKET_T sockfd = 0;
  33442. /* wolfSSL_get_client_CA_list() with handshake */
  33443. StartTCP();
  33444. InitTcpReady(&ready);
  33445. XMEMSET(&server_args, 0, sizeof(func_args));
  33446. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  33447. server_args.signal = &ready;
  33448. server_args.callbacks = &server_cb;
  33449. /* we are responsible for free'ing WOLFSSL_CTX */
  33450. server_cb.ctx = ctx;
  33451. server_cb.isSharedCtx = 1;
  33452. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  33453. cliCertFile, 0));
  33454. start_thread(test_server_nofail, &server_args, &serverThread);
  33455. wait_tcp_ready(&server_args);
  33456. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  33457. ExpectNotNull(ctx_client =
  33458. wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  33459. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(
  33460. ctx_client, caCertFile, 0));
  33461. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(
  33462. ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  33463. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(
  33464. ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  33465. ExpectNotNull(ssl_client = wolfSSL_new(ctx_client));
  33466. ExpectIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  33467. ExpectIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  33468. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  33469. /* We are expecting two cert names to be sent */
  33470. ExpectIntEQ(sk_X509_NAME_num(ca_list), 2);
  33471. ExpectNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  33472. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  33473. ExpectNotNull(name = sk_X509_NAME_value(ca_list, i));
  33474. ExpectIntGE(sk_X509_NAME_find(names, name), 0);
  33475. }
  33476. wolfSSL_shutdown(ssl_client);
  33477. wolfSSL_free(ssl_client);
  33478. wolfSSL_CTX_free(ctx_client);
  33479. CloseSocket(sockfd);
  33480. join_thread(serverThread);
  33481. FreeTcpReady(&ready);
  33482. }
  33483. #endif
  33484. wolfSSL_free(ssl);
  33485. wolfSSL_CTX_free(ctx);
  33486. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT &&
  33487. * !NO_BIO */
  33488. return EXPECT_RESULT();
  33489. }
  33490. static int test_wolfSSL_CTX_add_client_CA(void)
  33491. {
  33492. EXPECT_DECLS;
  33493. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  33494. !defined(NO_WOLFSSL_CLIENT)
  33495. WOLFSSL_CTX* ctx = NULL;
  33496. WOLFSSL_X509* x509 = NULL;
  33497. WOLFSSL_X509* x509_a = NULL;
  33498. STACK_OF(X509_NAME)* ca_list = NULL;
  33499. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33500. /* Add client cert */
  33501. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  33502. SSL_FILETYPE_PEM));
  33503. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509), SSL_SUCCESS);
  33504. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  33505. /* Add another client cert */
  33506. ExpectNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  33507. SSL_FILETYPE_PEM));
  33508. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  33509. /* test for incorrect parameter */
  33510. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  33511. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  33512. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  33513. X509_free(x509);
  33514. X509_free(x509_a);
  33515. SSL_CTX_free(ctx);
  33516. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  33517. return EXPECT_RESULT();
  33518. }
  33519. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  33520. defined(HAVE_IO_TESTS_DEPENDENCIES)
  33521. static THREAD_RETURN WOLFSSL_THREAD server_task_ech(void* args)
  33522. {
  33523. callback_functions* callbacks = ((func_args*)args)->callbacks;
  33524. WOLFSSL_CTX* ctx = callbacks->ctx;
  33525. WOLFSSL* ssl = NULL;
  33526. SOCKET_T sfd = 0;
  33527. SOCKET_T cfd = 0;
  33528. word16 port;
  33529. char input[1024];
  33530. int idx;
  33531. int ret, err = 0;
  33532. const char* privateName = "ech-private-name.com";
  33533. int privateNameLen = (int)XSTRLEN(privateName);
  33534. ((func_args*)args)->return_code = TEST_FAIL;
  33535. port = ((func_args*)args)->signal->port;
  33536. AssertIntEQ(WOLFSSL_SUCCESS,
  33537. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  33538. AssertIntEQ(WOLFSSL_SUCCESS,
  33539. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  33540. WOLFSSL_FILETYPE_PEM));
  33541. AssertIntEQ(WOLFSSL_SUCCESS,
  33542. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  33543. WOLFSSL_FILETYPE_PEM));
  33544. if (callbacks->ctx_ready)
  33545. callbacks->ctx_ready(ctx);
  33546. ssl = wolfSSL_new(ctx);
  33547. /* set the sni for the server */
  33548. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, privateName, privateNameLen);
  33549. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  33550. CloseSocket(sfd);
  33551. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  33552. if (callbacks->ssl_ready)
  33553. callbacks->ssl_ready(ssl);
  33554. do {
  33555. err = 0; /* Reset error */
  33556. ret = wolfSSL_accept(ssl);
  33557. if (ret != WOLFSSL_SUCCESS) {
  33558. err = wolfSSL_get_error(ssl, 0);
  33559. }
  33560. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  33561. if (ret != WOLFSSL_SUCCESS) {
  33562. char buff[WOLFSSL_MAX_ERROR_SZ];
  33563. fprintf(stderr, "error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  33564. }
  33565. else {
  33566. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  33567. input[idx] = 0;
  33568. fprintf(stderr, "Client message: %s\n", input);
  33569. }
  33570. AssertIntEQ(privateNameLen, wolfSSL_write(ssl, privateName,
  33571. privateNameLen));
  33572. ((func_args*)args)->return_code = TEST_SUCCESS;
  33573. }
  33574. if (callbacks->on_result)
  33575. callbacks->on_result(ssl);
  33576. wolfSSL_shutdown(ssl);
  33577. wolfSSL_free(ssl);
  33578. wolfSSL_CTX_free(ctx);
  33579. CloseSocket(cfd);
  33580. #ifdef FP_ECC
  33581. wc_ecc_fp_free();
  33582. #endif
  33583. WOLFSSL_RETURN_FROM_THREAD(0);
  33584. }
  33585. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  33586. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  33587. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  33588. {
  33589. AssertNotNull(ssl);
  33590. AssertNotNull(line);
  33591. XFILE fp;
  33592. const byte lf = '\n';
  33593. fp = XFOPEN("./MyKeyLog.txt", "a");
  33594. XFWRITE( line, 1, strlen(line),fp);
  33595. XFWRITE( (void*)&lf,1,1,fp);
  33596. XFFLUSH(fp);
  33597. XFCLOSE(fp);
  33598. }
  33599. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  33600. static int test_wolfSSL_CTX_set_keylog_callback(void)
  33601. {
  33602. EXPECT_DECLS;
  33603. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  33604. !defined(NO_WOLFSSL_CLIENT)
  33605. SSL_CTX* ctx = NULL;
  33606. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33607. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  33608. SSL_CTX_free(ctx);
  33609. SSL_CTX_set_keylog_callback(NULL, NULL);
  33610. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  33611. return EXPECT_RESULT();
  33612. }
  33613. static int test_wolfSSL_CTX_get_keylog_callback(void)
  33614. {
  33615. EXPECT_DECLS;
  33616. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  33617. !defined(NO_WOLFSSL_CLIENT)
  33618. SSL_CTX* ctx = NULL;
  33619. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33620. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  33621. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  33622. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  33623. SSL_CTX_set_keylog_callback(ctx, NULL );
  33624. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  33625. SSL_CTX_free(ctx);
  33626. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  33627. return EXPECT_RESULT();
  33628. }
  33629. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  33630. static int test_wolfSSL_Tls12_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  33631. {
  33632. /* set keylog callback */
  33633. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  33634. return TEST_SUCCESS;
  33635. }
  33636. #endif
  33637. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  33638. {
  33639. EXPECT_DECLS;
  33640. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  33641. /* This test is intended for checking whether keylog callback is called
  33642. * in client during TLS handshake between the client and a server.
  33643. */
  33644. test_ssl_cbf server_cbf;
  33645. test_ssl_cbf client_cbf;
  33646. XFILE fp = XBADFILE;
  33647. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  33648. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  33649. server_cbf.method = wolfTLSv1_2_server_method;
  33650. client_cbf.ctx_ready = &test_wolfSSL_Tls12_Key_Logging_client_ctx_ready;
  33651. /* clean up keylog file */
  33652. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  33653. if (fp != XBADFILE) {
  33654. XFFLUSH(fp);
  33655. XFCLOSE(fp);
  33656. fp = XBADFILE;
  33657. }
  33658. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33659. &server_cbf, NULL), TEST_SUCCESS);
  33660. XSLEEP_MS(100);
  33661. /* check if the keylog file exists */
  33662. char buff[300] = {0};
  33663. int found = 0;
  33664. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  33665. XFFLUSH(fp); /* Just to make sure any buffers get flushed */
  33666. while (EXPECT_SUCCESS() && XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  33667. if (0 == strncmp(buff,"CLIENT_RANDOM ", sizeof("CLIENT_RANDOM ")-1)) {
  33668. found = 1;
  33669. break;
  33670. }
  33671. }
  33672. if (fp != XBADFILE) {
  33673. XFCLOSE(fp);
  33674. }
  33675. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  33676. ExpectIntEQ(found, 1);
  33677. /* clean up */
  33678. ExpectIntEQ(rem_file("./MyKeyLog.txt"), 0);
  33679. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  33680. return EXPECT_RESULT();
  33681. }
  33682. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  33683. defined(HAVE_SECRET_CALLBACK)
  33684. static int test_wolfSSL_Tls13_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  33685. {
  33686. /* set keylog callback */
  33687. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  33688. return TEST_SUCCESS;
  33689. }
  33690. #endif
  33691. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  33692. {
  33693. EXPECT_DECLS;
  33694. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  33695. defined(HAVE_SECRET_CALLBACK)
  33696. /* This test is intended for checking whether keylog callback is called
  33697. * in client during TLS handshake between the client and a server.
  33698. */
  33699. test_ssl_cbf server_cbf;
  33700. test_ssl_cbf client_cbf;
  33701. XFILE fp = XBADFILE;
  33702. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  33703. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  33704. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  33705. client_cbf.ctx_ready = &test_wolfSSL_Tls13_Key_Logging_client_ctx_ready;
  33706. /* clean up keylog file */
  33707. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  33708. if (fp != XBADFILE) {
  33709. XFCLOSE(fp);
  33710. fp = XBADFILE;
  33711. }
  33712. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33713. &server_cbf, NULL), TEST_SUCCESS);
  33714. /* check if the keylog file exists */
  33715. {
  33716. char buff[300] = {0};
  33717. int found[4] = {0};
  33718. int numfnd = 0;
  33719. int i;
  33720. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  33721. while (EXPECT_SUCCESS() &&
  33722. XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  33723. if (0 == strncmp(buff, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  33724. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  33725. found[0] = 1;
  33726. continue;
  33727. }
  33728. else if (0 == strncmp(buff, "SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  33729. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  33730. found[1] = 1;
  33731. continue;
  33732. }
  33733. else if (0 == strncmp(buff, "CLIENT_TRAFFIC_SECRET_0 ",
  33734. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  33735. found[2] = 1;
  33736. continue;
  33737. }
  33738. else if (0 == strncmp(buff, "SERVER_TRAFFIC_SECRET_0 ",
  33739. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  33740. found[3] = 1;
  33741. continue;
  33742. }
  33743. }
  33744. if (fp != XBADFILE)
  33745. XFCLOSE(fp);
  33746. for (i = 0; i < 4; i++) {
  33747. if (found[i] != 0)
  33748. numfnd++;
  33749. }
  33750. ExpectIntEQ(numfnd, 4);
  33751. }
  33752. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  33753. return EXPECT_RESULT();
  33754. }
  33755. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  33756. defined(HAVE_IO_TESTS_DEPENDENCIES)
  33757. static int test_wolfSSL_Tls13_ECH_params(void)
  33758. {
  33759. EXPECT_DECLS;
  33760. #if !defined(NO_WOLFSSL_CLIENT)
  33761. word32 outputLen = 0;
  33762. byte testBuf[72];
  33763. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  33764. WOLFSSL *ssl = wolfSSL_new(ctx);
  33765. ExpectNotNull(ctx);
  33766. ExpectNotNull(ssl);
  33767. /* invalid ctx */
  33768. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(NULL,
  33769. "ech-public-name.com", 0, 0, 0));
  33770. /* invalid public name */
  33771. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx, NULL, 0,
  33772. 0, 0));
  33773. /* invalid algorithms */
  33774. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx,
  33775. "ech-public-name.com", 1000, 1000, 1000));
  33776. /* invalid ctx */
  33777. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(NULL, NULL,
  33778. &outputLen));
  33779. /* invalid output len */
  33780. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(ctx, NULL, NULL));
  33781. /* invalid ssl */
  33782. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(NULL,
  33783. (char*)testBuf, sizeof(testBuf)));
  33784. /* invalid configs64 */
  33785. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl, NULL,
  33786. sizeof(testBuf)));
  33787. /* invalid size */
  33788. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl,
  33789. (char*)testBuf, 0));
  33790. /* invalid ssl */
  33791. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(NULL, testBuf,
  33792. sizeof(testBuf)));
  33793. /* invalid configs */
  33794. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, NULL,
  33795. sizeof(testBuf)));
  33796. /* invalid size */
  33797. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, testBuf, 0));
  33798. /* invalid ssl */
  33799. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(NULL, NULL, &outputLen));
  33800. /* invalid size */
  33801. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(ssl, NULL, NULL));
  33802. wolfSSL_free(ssl);
  33803. wolfSSL_CTX_free(ctx);
  33804. #endif /* !NO_WOLFSSL_CLIENT */
  33805. return EXPECT_RESULT();
  33806. }
  33807. static int test_wolfSSL_Tls13_ECH(void)
  33808. {
  33809. EXPECT_DECLS;
  33810. tcp_ready ready;
  33811. func_args client_args;
  33812. func_args server_args;
  33813. THREAD_TYPE serverThread;
  33814. callback_functions server_cbf;
  33815. callback_functions client_cbf;
  33816. SOCKET_T sockfd = 0;
  33817. WOLFSSL_CTX* ctx = NULL;
  33818. WOLFSSL* ssl = NULL;
  33819. const char* publicName = "ech-public-name.com";
  33820. const char* privateName = "ech-private-name.com";
  33821. int privateNameLen = 20;
  33822. char reply[1024];
  33823. int replyLen = 0;
  33824. byte rawEchConfig[128];
  33825. word32 rawEchConfigLen = sizeof(rawEchConfig);
  33826. InitTcpReady(&ready);
  33827. ready.port = 22222;
  33828. XMEMSET(&client_args, 0, sizeof(func_args));
  33829. XMEMSET(&server_args, 0, sizeof(func_args));
  33830. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  33831. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  33832. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  33833. /* create the server context here so we can get the ech config */
  33834. ExpectNotNull(server_cbf.ctx =
  33835. wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  33836. /* generate ech config */
  33837. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(server_cbf.ctx,
  33838. publicName, 0, 0, 0));
  33839. /* get the config for the client to use */
  33840. ExpectIntEQ(WOLFSSL_SUCCESS,
  33841. wolfSSL_CTX_GetEchConfigs(server_cbf.ctx, rawEchConfig,
  33842. &rawEchConfigLen));
  33843. server_args.callbacks = &server_cbf;
  33844. server_args.signal = &ready;
  33845. /* start server task */
  33846. start_thread(server_task_ech, &server_args, &serverThread);
  33847. wait_tcp_ready(&server_args);
  33848. /* run as a TLS1.3 client */
  33849. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  33850. ExpectIntEQ(WOLFSSL_SUCCESS,
  33851. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  33852. ExpectIntEQ(WOLFSSL_SUCCESS,
  33853. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  33854. ExpectIntEQ(WOLFSSL_SUCCESS,
  33855. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  33856. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  33857. /* get connected the server task */
  33858. ExpectNotNull(ssl = wolfSSL_new(ctx));
  33859. /* set the ech configs for the client */
  33860. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, rawEchConfig,
  33861. rawEchConfigLen));
  33862. /* set the sni for the client */
  33863. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
  33864. privateName, privateNameLen));
  33865. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  33866. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  33867. ExpectIntEQ(wolfSSL_write(ssl, privateName, privateNameLen),
  33868. privateNameLen);
  33869. ExpectIntGT((replyLen = wolfSSL_read(ssl, reply, sizeof(reply))), 0);
  33870. /* add th null terminator for string compare */
  33871. reply[replyLen] = 0;
  33872. /* check that the server replied with the private name */
  33873. ExpectStrEQ(privateName, reply);
  33874. wolfSSL_free(ssl);
  33875. wolfSSL_CTX_free(ctx);
  33876. CloseSocket(sockfd);
  33877. join_thread(serverThread);
  33878. FreeTcpReady(&ready);
  33879. return EXPECT_RESULT();
  33880. }
  33881. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  33882. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  33883. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33884. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  33885. static int post_auth_version_cb(WOLFSSL* ssl)
  33886. {
  33887. EXPECT_DECLS;
  33888. /* do handshake and then test version error */
  33889. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  33890. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  33891. return EXPECT_RESULT();
  33892. }
  33893. static int post_auth_version_client_cb(WOLFSSL* ssl)
  33894. {
  33895. EXPECT_DECLS;
  33896. /* do handshake and then test version error */
  33897. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  33898. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  33899. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  33900. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  33901. /* check was added to error queue */
  33902. ExpectIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  33903. /* check the string matches expected string */
  33904. ExpectStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  33905. "WRONG_SSL_VERSION");
  33906. #endif
  33907. return EXPECT_RESULT();
  33908. }
  33909. static int post_auth_cb(WOLFSSL* ssl)
  33910. {
  33911. EXPECT_DECLS;
  33912. WOLFSSL_X509* x509 = NULL;
  33913. /* do handshake and then test version error */
  33914. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  33915. ExpectStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  33916. ExpectNull(x509 = wolfSSL_get_peer_certificate(ssl));
  33917. wolfSSL_X509_free(x509);
  33918. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  33919. return EXPECT_RESULT();
  33920. }
  33921. static int set_post_auth_cb(WOLFSSL* ssl)
  33922. {
  33923. if (!wolfSSL_is_server(ssl)) {
  33924. EXPECT_DECLS;
  33925. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  33926. return EXPECT_RESULT();
  33927. }
  33928. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  33929. return TEST_SUCCESS;
  33930. }
  33931. #endif
  33932. static int test_wolfSSL_Tls13_postauth(void)
  33933. {
  33934. EXPECT_DECLS;
  33935. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  33936. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33937. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  33938. test_ssl_cbf server_cbf;
  33939. test_ssl_cbf client_cbf;
  33940. /* test version failure doing post auth with TLS 1.2 connection */
  33941. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  33942. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  33943. server_cbf.method = wolfTLSv1_2_server_method;
  33944. server_cbf.ssl_ready = set_post_auth_cb;
  33945. server_cbf.on_result = post_auth_version_cb;
  33946. client_cbf.ssl_ready = set_post_auth_cb;
  33947. client_cbf.on_result = post_auth_version_client_cb;
  33948. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33949. &server_cbf, NULL), TEST_SUCCESS);
  33950. /* tests on post auth with TLS 1.3 */
  33951. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  33952. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  33953. server_cbf.method = wolfTLSv1_3_server_method;
  33954. server_cbf.ssl_ready = set_post_auth_cb;
  33955. client_cbf.ssl_ready = set_post_auth_cb;
  33956. server_cbf.on_result = post_auth_cb;
  33957. client_cbf.on_result = NULL;
  33958. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33959. &server_cbf, NULL), TEST_SUCCESS);
  33960. #endif
  33961. return EXPECT_RESULT();
  33962. }
  33963. static int test_wolfSSL_X509_NID(void)
  33964. {
  33965. EXPECT_DECLS;
  33966. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  33967. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  33968. int sigType;
  33969. int nameSz;
  33970. X509* cert = NULL;
  33971. EVP_PKEY* pubKeyTmp = NULL;
  33972. X509_NAME* name = NULL;
  33973. char commonName[80];
  33974. char countryName[80];
  33975. char localityName[80];
  33976. char stateName[80];
  33977. char orgName[80];
  33978. char orgUnit[80];
  33979. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  33980. /* convert cert from DER to internal WOLFSSL_X509 struct */
  33981. ExpectNotNull(cert = wolfSSL_X509_d2i_ex(&cert, client_cert_der_2048,
  33982. sizeof_client_cert_der_2048, HEAP_HINT));
  33983. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  33984. /* extract PUBLIC KEY from cert */
  33985. ExpectNotNull(pubKeyTmp = X509_get_pubkey(cert));
  33986. /* extract signatureType */
  33987. ExpectIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  33988. /* extract subjectName info */
  33989. ExpectNotNull(name = X509_get_subject_name(cert));
  33990. ExpectIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  33991. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  33992. NULL, 0)), 0);
  33993. ExpectIntEQ(nameSz, 15);
  33994. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  33995. commonName, sizeof(commonName))), 0);
  33996. ExpectIntEQ(nameSz, 15);
  33997. ExpectIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  33998. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  33999. commonName, 9)), 0);
  34000. ExpectIntEQ(nameSz, 8);
  34001. ExpectIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  34002. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  34003. countryName, sizeof(countryName))), 0);
  34004. ExpectIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  34005. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  34006. localityName, sizeof(localityName))), 0);
  34007. ExpectIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  34008. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  34009. NID_stateOrProvinceName, stateName, sizeof(stateName))), 0);
  34010. ExpectIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  34011. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  34012. orgName, sizeof(orgName))), 0);
  34013. ExpectIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  34014. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  34015. NID_organizationalUnitName, orgUnit, sizeof(orgUnit))), 0);
  34016. ExpectIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  34017. EVP_PKEY_free(pubKeyTmp);
  34018. X509_free(cert);
  34019. #endif
  34020. return EXPECT_RESULT();
  34021. }
  34022. static int test_wolfSSL_CTX_set_srp_username(void)
  34023. {
  34024. EXPECT_DECLS;
  34025. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  34026. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  34027. WOLFSSL_CTX* ctx = NULL;
  34028. WOLFSSL* ssl = NULL;
  34029. const char *username = "TESTUSER";
  34030. const char *password = "TESTPASSWORD";
  34031. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34032. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  34033. SSL_SUCCESS);
  34034. wolfSSL_CTX_free(ctx);
  34035. ctx = NULL;
  34036. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34037. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  34038. SSL_SUCCESS);
  34039. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  34040. SSL_SUCCESS);
  34041. ExpectNotNull(ssl = SSL_new(ctx));
  34042. ExpectNotNull(SSL_get_srp_username(ssl));
  34043. ExpectStrEQ(SSL_get_srp_username(ssl), username);
  34044. wolfSSL_free(ssl);
  34045. wolfSSL_CTX_free(ctx);
  34046. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  34047. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  34048. return EXPECT_RESULT();
  34049. }
  34050. static int test_wolfSSL_CTX_set_srp_password(void)
  34051. {
  34052. EXPECT_DECLS;
  34053. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) && \
  34054. !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  34055. WOLFSSL_CTX* ctx = NULL;
  34056. const char *username = "TESTUSER";
  34057. const char *password = "TESTPASSWORD";
  34058. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34059. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  34060. SSL_SUCCESS);
  34061. wolfSSL_CTX_free(ctx);
  34062. ctx = NULL;
  34063. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34064. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  34065. SSL_SUCCESS);
  34066. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  34067. SSL_SUCCESS);
  34068. wolfSSL_CTX_free(ctx);
  34069. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  34070. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  34071. return EXPECT_RESULT();
  34072. }
  34073. static int test_wolfSSL_X509_STORE(void)
  34074. {
  34075. EXPECT_DECLS;
  34076. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  34077. X509_STORE *store = NULL;
  34078. #ifdef HAVE_CRL
  34079. X509_STORE_CTX *storeCtx = NULL;
  34080. X509_CRL *crl = NULL;
  34081. X509 *ca = NULL;
  34082. X509 *cert = NULL;
  34083. const char crlPem[] = "./certs/crl/crl.revoked";
  34084. const char srvCert[] = "./certs/server-revoked-cert.pem";
  34085. const char caCert[] = "./certs/ca-cert.pem";
  34086. XFILE fp = XBADFILE;
  34087. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34088. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  34089. SSL_FILETYPE_PEM)));
  34090. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  34091. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  34092. SSL_FILETYPE_PEM)));
  34093. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  34094. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  34095. ExpectIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  34096. X509_STORE_free(store);
  34097. store = NULL;
  34098. X509_STORE_CTX_free(storeCtx);
  34099. storeCtx = NULL;
  34100. X509_free(cert);
  34101. cert = NULL;
  34102. X509_free(ca);
  34103. ca = NULL;
  34104. /* should fail to verify now after adding in CRL */
  34105. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34106. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  34107. SSL_FILETYPE_PEM)));
  34108. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  34109. ExpectTrue((fp = XFOPEN(crlPem, "rb")) != XBADFILE);
  34110. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  34111. NULL, NULL));
  34112. if (fp != XBADFILE)
  34113. XFCLOSE(fp);
  34114. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  34115. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  34116. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  34117. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  34118. SSL_FILETYPE_PEM)));
  34119. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  34120. ExpectIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  34121. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx),
  34122. WOLFSSL_X509_V_ERR_CERT_REVOKED);
  34123. X509_CRL_free(crl);
  34124. crl = NULL;
  34125. X509_STORE_free(store);
  34126. store = NULL;
  34127. X509_STORE_CTX_free(storeCtx);
  34128. storeCtx = NULL;
  34129. X509_free(cert);
  34130. cert = NULL;
  34131. X509_free(ca);
  34132. ca = NULL;
  34133. #endif /* HAVE_CRL */
  34134. #ifndef WOLFCRYPT_ONLY
  34135. {
  34136. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34137. SSL_CTX* ctx = NULL;
  34138. SSL* ssl = NULL;
  34139. int i;
  34140. for (i = 0; i < 2; i++) {
  34141. #ifndef NO_WOLFSSL_SERVER
  34142. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34143. #else
  34144. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34145. #endif
  34146. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34147. SSL_CTX_set_cert_store(ctx, store);
  34148. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34149. SSL_CTX_set_cert_store(ctx, store);
  34150. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34151. ExpectIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  34152. SSL_FILETYPE_PEM), SSL_SUCCESS);
  34153. ExpectIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  34154. SSL_FILETYPE_PEM), SSL_SUCCESS);
  34155. ExpectNotNull(ssl = SSL_new(ctx));
  34156. if (i == 0) {
  34157. ExpectIntEQ(SSL_set0_verify_cert_store(ssl, store),
  34158. SSL_SUCCESS);
  34159. }
  34160. else {
  34161. ExpectIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  34162. #ifdef OPENSSL_ALL
  34163. ExpectIntEQ(SSL_CTX_set1_verify_cert_store(ctx, store), SSL_SUCCESS);
  34164. #endif
  34165. }
  34166. if (EXPECT_FAIL() || (i == 1)) {
  34167. X509_STORE_free(store);
  34168. store = NULL;
  34169. }
  34170. SSL_free(ssl);
  34171. ssl = NULL;
  34172. SSL_CTX_free(ctx);
  34173. ctx = NULL;
  34174. }
  34175. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34176. }
  34177. #endif
  34178. #endif
  34179. return EXPECT_RESULT();
  34180. }
  34181. static int test_wolfSSL_X509_STORE_load_locations(void)
  34182. {
  34183. EXPECT_DECLS;
  34184. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  34185. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  34186. SSL_CTX *ctx = NULL;
  34187. X509_STORE *store = NULL;
  34188. const char ca_file[] = "./certs/ca-cert.pem";
  34189. const char client_pem_file[] = "./certs/client-cert.pem";
  34190. const char client_der_file[] = "./certs/client-cert.der";
  34191. const char ecc_file[] = "./certs/ecc-key.pem";
  34192. const char certs_path[] = "./certs/";
  34193. const char bad_path[] = "./bad-path/";
  34194. #ifdef HAVE_CRL
  34195. const char crl_path[] = "./certs/crl/";
  34196. const char crl_file[] = "./certs/crl/crl.pem";
  34197. #endif
  34198. #ifndef NO_WOLFSSL_SERVER
  34199. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  34200. #else
  34201. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  34202. #endif
  34203. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  34204. ExpectIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL),
  34205. WOLFSSL_SUCCESS);
  34206. /* Test bad arguments */
  34207. ExpectIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL),
  34208. WOLFSSL_FAILURE);
  34209. ExpectIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  34210. ExpectIntEQ(X509_STORE_load_locations(store, client_der_file, NULL),
  34211. WOLFSSL_FAILURE);
  34212. ExpectIntEQ(X509_STORE_load_locations(store, ecc_file, NULL),
  34213. WOLFSSL_FAILURE);
  34214. ExpectIntEQ(X509_STORE_load_locations(store, NULL, bad_path),
  34215. WOLFSSL_FAILURE);
  34216. #ifdef HAVE_CRL
  34217. /* Test with CRL */
  34218. ExpectIntEQ(X509_STORE_load_locations(store, crl_file, NULL),
  34219. WOLFSSL_SUCCESS);
  34220. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crl_path),
  34221. WOLFSSL_SUCCESS);
  34222. #endif
  34223. /* Test with CA */
  34224. ExpectIntEQ(X509_STORE_load_locations(store, ca_file, NULL),
  34225. WOLFSSL_SUCCESS);
  34226. /* Test with client_cert and certs path */
  34227. ExpectIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL),
  34228. WOLFSSL_SUCCESS);
  34229. ExpectIntEQ(X509_STORE_load_locations(store, NULL, certs_path),
  34230. WOLFSSL_SUCCESS);
  34231. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  34232. /* Clear nodes */
  34233. ERR_clear_error();
  34234. #endif
  34235. SSL_CTX_free(ctx);
  34236. #endif
  34237. return EXPECT_RESULT();
  34238. }
  34239. static int test_X509_STORE_get0_objects(void)
  34240. {
  34241. EXPECT_DECLS;
  34242. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  34243. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  34244. X509_STORE *store = NULL;
  34245. X509_STORE *store_cpy = NULL;
  34246. SSL_CTX *ctx = NULL;
  34247. X509_OBJECT *obj = NULL;
  34248. STACK_OF(X509_OBJECT) *objs = NULL;
  34249. int i;
  34250. /* Setup store */
  34251. #ifndef NO_WOLFSSL_SERVER
  34252. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  34253. #else
  34254. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  34255. #endif
  34256. ExpectNotNull(store_cpy = X509_STORE_new());
  34257. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  34258. ExpectIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL),
  34259. WOLFSSL_SUCCESS);
  34260. ExpectIntEQ(X509_STORE_load_locations(store, caCertFile, NULL),
  34261. WOLFSSL_SUCCESS);
  34262. ExpectIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL),
  34263. WOLFSSL_SUCCESS);
  34264. #ifdef HAVE_CRL
  34265. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir),
  34266. WOLFSSL_SUCCESS);
  34267. #endif
  34268. /* Store ready */
  34269. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  34270. ExpectNotNull(objs = X509_STORE_get0_objects(store));
  34271. #ifdef HAVE_CRL
  34272. #ifdef WOLFSSL_SIGNER_DER_CERT
  34273. ExpectIntEQ(sk_X509_OBJECT_num(objs), 4);
  34274. #else
  34275. ExpectIntEQ(sk_X509_OBJECT_num(objs), 1);
  34276. #endif
  34277. #else
  34278. #ifdef WOLFSSL_SIGNER_DER_CERT
  34279. ExpectIntEQ(sk_X509_OBJECT_num(objs), 3);
  34280. #else
  34281. ExpectIntEQ(sk_X509_OBJECT_num(objs), 0);
  34282. #endif
  34283. #endif
  34284. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  34285. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  34286. switch (X509_OBJECT_get_type(obj)) {
  34287. case X509_LU_X509:
  34288. {
  34289. WOLFSSL_X509* x509;
  34290. ExpectNotNull(x509 = X509_OBJECT_get0_X509(obj));
  34291. ExpectIntEQ(X509_STORE_add_cert(store_cpy, x509), WOLFSSL_SUCCESS);
  34292. break;
  34293. }
  34294. case X509_LU_CRL:
  34295. #ifdef HAVE_CRL
  34296. {
  34297. WOLFSSL_CRL* crl = NULL;
  34298. ExpectNotNull(crl = X509_OBJECT_get0_X509_CRL(obj));
  34299. ExpectIntEQ(X509_STORE_add_crl(store_cpy, crl), WOLFSSL_SUCCESS);
  34300. break;
  34301. }
  34302. #endif
  34303. case X509_LU_NONE:
  34304. default:
  34305. Fail(("X509_OBJECT_get_type should return x509 or crl "
  34306. "(when built with crl support)"),
  34307. ("Unrecognized X509_OBJECT type or none"));
  34308. }
  34309. }
  34310. X509_STORE_free(store_cpy);
  34311. SSL_CTX_free(ctx);
  34312. #endif
  34313. return EXPECT_RESULT();
  34314. }
  34315. static int test_wolfSSL_BN_CTX(void)
  34316. {
  34317. EXPECT_DECLS;
  34318. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34319. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34320. WOLFSSL_BN_CTX* bn_ctx = NULL;
  34321. WOLFSSL_BIGNUM* t = NULL;
  34322. ExpectNotNull(bn_ctx = wolfSSL_BN_CTX_new());
  34323. /* No implementation. */
  34324. BN_CTX_init(NULL);
  34325. ExpectNotNull(t = BN_CTX_get(NULL));
  34326. BN_free(t);
  34327. ExpectNotNull(t = BN_CTX_get(bn_ctx));
  34328. BN_free(t);
  34329. #ifndef NO_WOLFSSL_STUB
  34330. /* No implementation. */
  34331. BN_CTX_start(NULL);
  34332. BN_CTX_start(bn_ctx);
  34333. #endif
  34334. BN_CTX_free(NULL);
  34335. BN_CTX_free(bn_ctx);
  34336. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34337. return EXPECT_RESULT();
  34338. }
  34339. static int test_wolfSSL_BN(void)
  34340. {
  34341. EXPECT_DECLS;
  34342. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34343. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34344. BIGNUM* a = NULL;
  34345. BIGNUM* b = NULL;
  34346. BIGNUM* c = NULL;
  34347. BIGNUM* d = NULL;
  34348. BIGNUM emptyBN;
  34349. /* Setup */
  34350. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34351. /* internal not set emptyBN. */
  34352. ExpectNotNull(a = BN_new());
  34353. ExpectNotNull(b = BN_new());
  34354. ExpectNotNull(c = BN_dup(b));
  34355. ExpectNotNull(d = BN_new());
  34356. /* Invalid parameter testing. */
  34357. BN_free(NULL);
  34358. ExpectNull(BN_dup(NULL));
  34359. ExpectNull(BN_dup(&emptyBN));
  34360. ExpectNull(BN_copy(NULL, NULL));
  34361. ExpectNull(BN_copy(b, NULL));
  34362. ExpectNull(BN_copy(NULL, c));
  34363. ExpectNull(BN_copy(b, &emptyBN));
  34364. ExpectNull(BN_copy(&emptyBN, c));
  34365. BN_clear(NULL);
  34366. BN_clear(&emptyBN);
  34367. ExpectIntEQ(BN_num_bytes(NULL), 0);
  34368. ExpectIntEQ(BN_num_bytes(&emptyBN), 0);
  34369. ExpectIntEQ(BN_num_bits(NULL), 0);
  34370. ExpectIntEQ(BN_num_bits(&emptyBN), 0);
  34371. ExpectIntEQ(BN_is_negative(NULL), 0);
  34372. ExpectIntEQ(BN_is_negative(&emptyBN), 0);
  34373. /* END Invalid Parameters */
  34374. ExpectIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  34375. ExpectIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  34376. ExpectIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  34377. ExpectIntEQ(BN_num_bits(a), 2);
  34378. ExpectIntEQ(BN_num_bytes(a), 1);
  34379. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  34380. defined(WOLFSSL_SP_INT_NEGATIVE))
  34381. ExpectIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  34382. ExpectIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  34383. ExpectIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  34384. ExpectIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  34385. ExpectIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  34386. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34387. {
  34388. /* Do additional tests on negative BN conversions. */
  34389. char* ret = NULL;
  34390. ASN1_INTEGER* asn1 = NULL;
  34391. BIGNUM* tmp = NULL;
  34392. /* Sanity check we have a negative BN. */
  34393. ExpectIntEQ(BN_is_negative(c), 1);
  34394. ExpectNotNull(ret = BN_bn2dec(c));
  34395. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  34396. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  34397. ret = NULL;
  34398. /* Convert to ASN1_INTEGER and back to BN. */
  34399. ExpectNotNull(asn1 = BN_to_ASN1_INTEGER(c, NULL));
  34400. ExpectNotNull(tmp = ASN1_INTEGER_to_BN(asn1, NULL));
  34401. /* After converting back BN should be negative and correct. */
  34402. ExpectIntEQ(BN_is_negative(tmp), 1);
  34403. ExpectNotNull(ret = BN_bn2dec(tmp));
  34404. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  34405. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  34406. ASN1_INTEGER_free(asn1);
  34407. BN_free(tmp);
  34408. }
  34409. #endif
  34410. ExpectIntEQ(BN_get_word(c), 4);
  34411. #endif
  34412. ExpectIntEQ(BN_set_word(a, 3), 1);
  34413. ExpectIntEQ(BN_set_word(b, 3), 1);
  34414. ExpectIntEQ(BN_set_word(c, 4), 1);
  34415. /* NULL == NULL, NULL < num, num > NULL */
  34416. ExpectIntEQ(BN_cmp(NULL, NULL), 0);
  34417. ExpectIntEQ(BN_cmp(&emptyBN, &emptyBN), 0);
  34418. ExpectIntLT(BN_cmp(NULL, b), 0);
  34419. ExpectIntLT(BN_cmp(&emptyBN, b), 0);
  34420. ExpectIntGT(BN_cmp(a, NULL), 0);
  34421. ExpectIntGT(BN_cmp(a, &emptyBN), 0);
  34422. ExpectIntEQ(BN_cmp(a, b), 0);
  34423. ExpectIntLT(BN_cmp(a, c), 0);
  34424. ExpectIntGT(BN_cmp(c, b), 0);
  34425. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  34426. ExpectIntEQ(BN_print_fp(XBADFILE, NULL), 0);
  34427. ExpectIntEQ(BN_print_fp(XBADFILE, &emptyBN), 0);
  34428. ExpectIntEQ(BN_print_fp(stderr, NULL), 0);
  34429. ExpectIntEQ(BN_print_fp(stderr, &emptyBN), 0);
  34430. ExpectIntEQ(BN_print_fp(XBADFILE, a), 0);
  34431. ExpectIntEQ(BN_print_fp(stderr, a), 1);
  34432. #endif
  34433. BN_clear(a);
  34434. BN_free(a);
  34435. BN_free(b);
  34436. BN_free(c);
  34437. BN_clear_free(d);
  34438. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34439. return EXPECT_RESULT();
  34440. }
  34441. static int test_wolfSSL_BN_init(void)
  34442. {
  34443. EXPECT_DECLS;
  34444. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34445. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34446. #if !defined(USE_INTEGER_HEAP_MATH) && !defined(HAVE_WOLF_BIGINT)
  34447. BIGNUM* ap = NULL;
  34448. BIGNUM bv;
  34449. BIGNUM cv;
  34450. BIGNUM dv;
  34451. ExpectNotNull(ap = BN_new());
  34452. BN_init(NULL);
  34453. XMEMSET(&bv, 0, sizeof(bv));
  34454. ExpectNull(BN_dup(&bv));
  34455. BN_init(&bv);
  34456. BN_init(&cv);
  34457. BN_init(&dv);
  34458. ExpectIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  34459. ExpectIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  34460. ExpectIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  34461. /* a^b mod c = */
  34462. ExpectIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  34463. ExpectIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  34464. /* check result 3^2 mod 5 */
  34465. ExpectIntEQ(BN_get_word(&dv), 4);
  34466. /* a*b mod c = */
  34467. ExpectIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  34468. ExpectIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  34469. /* check result 3*2 mod 5 */
  34470. ExpectIntEQ(BN_get_word(&dv), 1);
  34471. BN_free(ap);
  34472. #endif
  34473. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34474. return EXPECT_RESULT();
  34475. }
  34476. static int test_wolfSSL_BN_enc_dec(void)
  34477. {
  34478. EXPECT_DECLS;
  34479. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  34480. BIGNUM* a = NULL;
  34481. BIGNUM* b = NULL;
  34482. BIGNUM* c = NULL;
  34483. BIGNUM emptyBN;
  34484. char* str = NULL;
  34485. const char* emptyStr = "";
  34486. const char* numberStr = "12345";
  34487. const char* badStr = "g12345";
  34488. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34489. const char* twoStr = "2";
  34490. #endif
  34491. unsigned char binNum[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  34492. unsigned char outNum[5];
  34493. /* Setup */
  34494. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34495. ExpectNotNull(a = BN_new());
  34496. ExpectNotNull(b = BN_new());
  34497. ExpectIntEQ(BN_set_word(a, 2), 1);
  34498. /* Invalid parameters */
  34499. ExpectIntEQ(BN_bn2bin(NULL, NULL), -1);
  34500. ExpectIntEQ(BN_bn2bin(&emptyBN, NULL), -1);
  34501. ExpectIntEQ(BN_bn2bin(NULL, outNum), -1);
  34502. ExpectIntEQ(BN_bn2bin(&emptyBN, outNum), -1);
  34503. ExpectNull(BN_bn2hex(NULL));
  34504. ExpectNull(BN_bn2hex(&emptyBN));
  34505. ExpectNull(BN_bn2dec(NULL));
  34506. ExpectNull(BN_bn2dec(&emptyBN));
  34507. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), NULL));
  34508. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), a));
  34509. ExpectNull(BN_bin2bn(binNum, -1, a));
  34510. ExpectNull(BN_bin2bn(binNum, -1, NULL));
  34511. ExpectNull(BN_bin2bn(binNum, sizeof(binNum), &emptyBN));
  34512. ExpectIntEQ(BN_hex2bn(NULL, NULL), 0);
  34513. ExpectIntEQ(BN_hex2bn(NULL, numberStr), 0);
  34514. ExpectIntEQ(BN_hex2bn(&a, NULL), 0);
  34515. ExpectIntEQ(BN_hex2bn(&a, emptyStr), 0);
  34516. ExpectIntEQ(BN_hex2bn(&a, badStr), 0);
  34517. ExpectIntEQ(BN_hex2bn(&c, badStr), 0);
  34518. ExpectIntEQ(BN_dec2bn(NULL, NULL), 0);
  34519. ExpectIntEQ(BN_dec2bn(NULL, numberStr), 0);
  34520. ExpectIntEQ(BN_dec2bn(&a, NULL), 0);
  34521. ExpectIntEQ(BN_dec2bn(&a, emptyStr), 0);
  34522. ExpectIntEQ(BN_dec2bn(&a, badStr), 0);
  34523. ExpectIntEQ(BN_dec2bn(&c, badStr), 0);
  34524. ExpectIntEQ(BN_set_word(a, 2), 1);
  34525. ExpectIntEQ(BN_bn2bin(a, NULL), 1);
  34526. ExpectIntEQ(BN_bn2bin(a, outNum), 1);
  34527. ExpectNotNull(BN_bin2bn(outNum, 1, b));
  34528. ExpectIntEQ(BN_cmp(a, b), 0);
  34529. ExpectNotNull(BN_bin2bn(binNum, sizeof(binNum), b));
  34530. ExpectIntEQ(BN_cmp(a, b), -1);
  34531. ExpectNotNull(str = BN_bn2hex(a));
  34532. ExpectNotNull(BN_hex2bn(&b, str));
  34533. ExpectIntEQ(BN_cmp(a, b), 0);
  34534. ExpectNotNull(BN_hex2bn(&b, numberStr));
  34535. ExpectIntEQ(BN_cmp(a, b), -1);
  34536. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34537. str = NULL;
  34538. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34539. ExpectNotNull(str = BN_bn2dec(a));
  34540. ExpectStrEQ(str, twoStr);
  34541. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34542. str = NULL;
  34543. #ifndef NO_RSA
  34544. ExpectNotNull(str = BN_bn2dec(a));
  34545. ExpectNotNull(BN_dec2bn(&b, str));
  34546. ExpectIntEQ(BN_cmp(a, b), 0);
  34547. ExpectNotNull(BN_dec2bn(&b, numberStr));
  34548. ExpectIntEQ(BN_cmp(a, b), -1);
  34549. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34550. str = NULL;
  34551. #else
  34552. /* No implementation - fail with good parameters. */
  34553. ExpectIntEQ(BN_dec2bn(&a, numberStr), 0);
  34554. #endif
  34555. #endif
  34556. BN_free(b);
  34557. BN_free(a);
  34558. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34559. return EXPECT_RESULT();
  34560. }
  34561. static int test_wolfSSL_BN_word(void)
  34562. {
  34563. EXPECT_DECLS;
  34564. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  34565. BIGNUM* a = NULL;
  34566. BIGNUM* b = NULL;
  34567. BIGNUM* c = NULL;
  34568. BIGNUM av;
  34569. ExpectNotNull(a = BN_new());
  34570. ExpectNotNull(b = BN_new());
  34571. ExpectNotNull(c = BN_new());
  34572. XMEMSET(&av, 0, sizeof(av));
  34573. /* Invalid parameter. */
  34574. ExpectIntEQ(BN_add_word(NULL, 3), 0);
  34575. ExpectIntEQ(BN_add_word(&av, 3), 0);
  34576. ExpectIntEQ(BN_sub_word(NULL, 3), 0);
  34577. ExpectIntEQ(BN_sub_word(&av, 3), 0);
  34578. ExpectIntEQ(BN_set_word(NULL, 3), 0);
  34579. ExpectIntEQ(BN_set_word(&av, 3), 0);
  34580. ExpectIntEQ(BN_get_word(NULL), 0);
  34581. ExpectIntEQ(BN_get_word(&av), 0);
  34582. ExpectIntEQ(BN_is_word(NULL, 3), 0);
  34583. ExpectIntEQ(BN_is_word(&av, 3), 0);
  34584. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  34585. !defined(NO_DSA))
  34586. ExpectIntEQ(BN_mod_word(NULL, 3), -1);
  34587. ExpectIntEQ(BN_mod_word(&av, 3), -1);
  34588. #endif
  34589. ExpectIntEQ(BN_one(NULL), 0);
  34590. ExpectIntEQ(BN_one(&av), 0);
  34591. BN_zero(NULL);
  34592. BN_zero(&av);
  34593. ExpectIntEQ(BN_is_one(NULL), 0);
  34594. ExpectIntEQ(BN_is_one(&av), 0);
  34595. ExpectIntEQ(BN_is_zero(NULL), 0);
  34596. ExpectIntEQ(BN_is_zero(&av), 0);
  34597. ExpectIntEQ(BN_set_word(a, 3), 1);
  34598. ExpectIntEQ(BN_set_word(b, 2), 1);
  34599. ExpectIntEQ(BN_set_word(c, 5), 1);
  34600. /* a + 3 = */
  34601. ExpectIntEQ(BN_add_word(a, 3), 1);
  34602. /* check result 3 + 3*/
  34603. ExpectIntEQ(BN_get_word(a), 6);
  34604. ExpectIntEQ(BN_is_word(a, 6), 1);
  34605. ExpectIntEQ(BN_is_word(a, 5), 0);
  34606. /* set a back to 3 */
  34607. ExpectIntEQ(BN_set_word(a, 3), 1);
  34608. /* a - 3 = */
  34609. ExpectIntEQ(BN_sub_word(a, 3), 1);
  34610. /* check result 3 - 3*/
  34611. ExpectIntEQ(BN_get_word(a), 0);
  34612. ExpectIntEQ(BN_one(a), 1);
  34613. ExpectIntEQ(BN_is_word(a, 1), 1);
  34614. ExpectIntEQ(BN_is_word(a, 0), 0);
  34615. ExpectIntEQ(BN_is_one(a), 1);
  34616. ExpectIntEQ(BN_is_zero(a), 0);
  34617. BN_zero(a);
  34618. ExpectIntEQ(BN_is_word(a, 0), 1);
  34619. ExpectIntEQ(BN_is_word(a, 1), 0);
  34620. ExpectIntEQ(BN_is_zero(a), 1);
  34621. ExpectIntEQ(BN_is_one(a), 0);
  34622. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  34623. !defined(NO_DSA))
  34624. ExpectIntEQ(BN_set_word(a, 5), 1);
  34625. ExpectIntEQ(BN_mod_word(a, 3), 2);
  34626. ExpectIntEQ(BN_mod_word(a, 0), -1);
  34627. #endif
  34628. BN_free(c);
  34629. BN_free(b);
  34630. BN_free(a);
  34631. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34632. return EXPECT_RESULT();
  34633. }
  34634. static int test_wolfSSL_BN_bits(void)
  34635. {
  34636. EXPECT_DECLS;
  34637. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34638. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34639. BIGNUM* a = NULL;
  34640. BIGNUM emptyBN;
  34641. /* Setup */
  34642. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34643. ExpectNotNull(a = BN_new());
  34644. /* Invalid parameters. */
  34645. ExpectIntEQ(BN_set_bit(NULL, 1), 0);
  34646. ExpectIntEQ(BN_set_bit(&emptyBN, 1), 0);
  34647. ExpectIntEQ(BN_set_bit(a, -1), 0);
  34648. ExpectIntEQ(BN_clear_bit(NULL, 1), 0);
  34649. ExpectIntEQ(BN_clear_bit(&emptyBN, 1), 0);
  34650. ExpectIntEQ(BN_clear_bit(a, -1), 0);
  34651. ExpectIntEQ(BN_is_bit_set(NULL, 1), 0);
  34652. ExpectIntEQ(BN_is_bit_set(&emptyBN, 1), 0);
  34653. ExpectIntEQ(BN_is_bit_set(a, -1), 0);
  34654. ExpectIntEQ(BN_is_odd(NULL), 0);
  34655. ExpectIntEQ(BN_is_odd(&emptyBN), 0);
  34656. ExpectIntEQ(BN_set_word(a, 0), 1);
  34657. ExpectIntEQ(BN_is_zero(a), 1);
  34658. ExpectIntEQ(BN_set_bit(a, 0x45), 1);
  34659. ExpectIntEQ(BN_is_zero(a), 0);
  34660. ExpectIntEQ(BN_is_bit_set(a, 0x45), 1);
  34661. ExpectIntEQ(BN_clear_bit(a, 0x45), 1);
  34662. ExpectIntEQ(BN_is_bit_set(a, 0x45), 0);
  34663. ExpectIntEQ(BN_is_zero(a), 1);
  34664. ExpectIntEQ(BN_set_bit(a, 0), 1);
  34665. ExpectIntEQ(BN_is_odd(a), 1);
  34666. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  34667. ExpectIntEQ(BN_is_odd(a), 0);
  34668. ExpectIntEQ(BN_set_bit(a, 1), 1);
  34669. ExpectIntEQ(BN_is_odd(a), 0);
  34670. ExpectIntEQ(BN_set_bit(a, 129), 1);
  34671. ExpectIntEQ(BN_get_word(a), WOLFSSL_BN_MAX_VAL);
  34672. #ifndef NO_WOLFSSL_STUB
  34673. ExpectIntEQ(BN_mask_bits(a, 1), 0);
  34674. #endif
  34675. BN_free(a);
  34676. #endif
  34677. return EXPECT_RESULT();
  34678. }
  34679. static int test_wolfSSL_BN_shift(void)
  34680. {
  34681. EXPECT_DECLS;
  34682. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34683. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34684. BIGNUM* a = NULL;
  34685. BIGNUM* b = NULL;
  34686. BIGNUM emptyBN;
  34687. /* Setup */
  34688. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34689. ExpectNotNull(a = BN_new());
  34690. ExpectNotNull(b = BN_new());
  34691. /* Invalid parameters. */
  34692. ExpectIntEQ(BN_lshift(NULL, NULL, 1), 0);
  34693. ExpectIntEQ(BN_lshift(&emptyBN, NULL, 1), 0);
  34694. ExpectIntEQ(BN_lshift(NULL, &emptyBN, 1), 0);
  34695. ExpectIntEQ(BN_lshift(b, NULL, 1), 0);
  34696. ExpectIntEQ(BN_lshift(b, &emptyBN, 1), 0);
  34697. ExpectIntEQ(BN_lshift(NULL, a, 1), 0);
  34698. ExpectIntEQ(BN_lshift(&emptyBN, a, 1), 0);
  34699. ExpectIntEQ(BN_lshift(b, a, -1), 0);
  34700. ExpectIntEQ(BN_rshift(NULL, NULL, 1), 0);
  34701. ExpectIntEQ(BN_rshift(&emptyBN, NULL, 1), 0);
  34702. ExpectIntEQ(BN_rshift(NULL, &emptyBN, 1), 0);
  34703. ExpectIntEQ(BN_rshift(b, NULL, 1), 0);
  34704. ExpectIntEQ(BN_rshift(b, &emptyBN, 1), 0);
  34705. ExpectIntEQ(BN_rshift(NULL, a, 1), 0);
  34706. ExpectIntEQ(BN_rshift(&emptyBN, a, 1), 0);
  34707. ExpectIntEQ(BN_rshift(b, a, -1), 0);
  34708. ExpectIntEQ(BN_set_word(a, 1), 1);
  34709. ExpectIntEQ(BN_lshift(b, a, 1), 1);
  34710. ExpectIntEQ(BN_is_word(b, 2), 1);
  34711. ExpectIntEQ(BN_lshift(a, a, 1), 1);
  34712. ExpectIntEQ(BN_is_word(a, 2), 1);
  34713. ExpectIntEQ(BN_rshift(b, a, 1), 1);
  34714. ExpectIntEQ(BN_is_word(b, 1), 1);
  34715. ExpectIntEQ(BN_rshift(a, a, 1), 1);
  34716. ExpectIntEQ(BN_is_word(a, 1), 1);
  34717. BN_free(b);
  34718. BN_free(a);
  34719. #endif
  34720. return EXPECT_RESULT();
  34721. }
  34722. static int test_wolfSSL_BN_math(void)
  34723. {
  34724. EXPECT_DECLS;
  34725. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34726. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34727. BIGNUM* a = NULL;
  34728. BIGNUM* b = NULL;
  34729. BIGNUM* r = NULL;
  34730. BIGNUM* rem = NULL;
  34731. BIGNUM emptyBN;
  34732. BN_ULONG val1;
  34733. BN_ULONG val2;
  34734. /* Setup */
  34735. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34736. ExpectNotNull(a = BN_new());
  34737. ExpectNotNull(b = BN_new());
  34738. ExpectNotNull(r = BN_new());
  34739. ExpectNotNull(rem = BN_new());
  34740. /* Invalid parameters. */
  34741. ExpectIntEQ(BN_add(NULL, NULL, NULL), 0);
  34742. ExpectIntEQ(BN_add(r, NULL, NULL), 0);
  34743. ExpectIntEQ(BN_add(NULL, a, NULL), 0);
  34744. ExpectIntEQ(BN_add(NULL, NULL, b), 0);
  34745. ExpectIntEQ(BN_add(r, a, NULL), 0);
  34746. ExpectIntEQ(BN_add(r, NULL, b), 0);
  34747. ExpectIntEQ(BN_add(NULL, a, b), 0);
  34748. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, &emptyBN), 0);
  34749. ExpectIntEQ(BN_add(r, &emptyBN, &emptyBN), 0);
  34750. ExpectIntEQ(BN_add(&emptyBN, a, &emptyBN), 0);
  34751. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, b), 0);
  34752. ExpectIntEQ(BN_add(r, a, &emptyBN), 0);
  34753. ExpectIntEQ(BN_add(r, &emptyBN, b), 0);
  34754. ExpectIntEQ(BN_add(&emptyBN, a, b), 0);
  34755. ExpectIntEQ(BN_sub(NULL, NULL, NULL), 0);
  34756. ExpectIntEQ(BN_sub(r, NULL, NULL), 0);
  34757. ExpectIntEQ(BN_sub(NULL, a, NULL), 0);
  34758. ExpectIntEQ(BN_sub(NULL, NULL, b), 0);
  34759. ExpectIntEQ(BN_sub(r, a, NULL), 0);
  34760. ExpectIntEQ(BN_sub(r, NULL, b), 0);
  34761. ExpectIntEQ(BN_sub(NULL, a, b), 0);
  34762. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, &emptyBN), 0);
  34763. ExpectIntEQ(BN_sub(r, &emptyBN, &emptyBN), 0);
  34764. ExpectIntEQ(BN_sub(&emptyBN, a, &emptyBN), 0);
  34765. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, b), 0);
  34766. ExpectIntEQ(BN_sub(r, a, &emptyBN), 0);
  34767. ExpectIntEQ(BN_sub(r, &emptyBN, b), 0);
  34768. ExpectIntEQ(BN_sub(&emptyBN, a, b), 0);
  34769. ExpectIntEQ(BN_mul(NULL, NULL, NULL, NULL), 0);
  34770. ExpectIntEQ(BN_mul(r, NULL, NULL, NULL), 0);
  34771. ExpectIntEQ(BN_mul(NULL, a, NULL, NULL), 0);
  34772. ExpectIntEQ(BN_mul(NULL, NULL, b, NULL), 0);
  34773. ExpectIntEQ(BN_mul(r, a, NULL, NULL), 0);
  34774. ExpectIntEQ(BN_mul(r, NULL, b, NULL), 0);
  34775. ExpectIntEQ(BN_mul(NULL, a, b, NULL), 0);
  34776. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34777. ExpectIntEQ(BN_mul(r, &emptyBN, &emptyBN, NULL), 0);
  34778. ExpectIntEQ(BN_mul(&emptyBN, a, &emptyBN, NULL), 0);
  34779. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, b, NULL), 0);
  34780. ExpectIntEQ(BN_mul(r, a, &emptyBN, NULL), 0);
  34781. ExpectIntEQ(BN_mul(r, &emptyBN, b, NULL), 0);
  34782. ExpectIntEQ(BN_mul(&emptyBN, a, b, NULL), 0);
  34783. ExpectIntEQ(BN_div(NULL, NULL, NULL, NULL, NULL), 0);
  34784. ExpectIntEQ(BN_div(r, NULL, NULL, NULL, NULL), 0);
  34785. ExpectIntEQ(BN_div(NULL, rem, NULL, NULL, NULL), 0);
  34786. ExpectIntEQ(BN_div(NULL, NULL, a, NULL, NULL), 0);
  34787. ExpectIntEQ(BN_div(NULL, NULL, NULL, b, NULL), 0);
  34788. ExpectIntEQ(BN_div(NULL, rem, a, b, NULL), 0);
  34789. ExpectIntEQ(BN_div(r, NULL, a, b, NULL), 0);
  34790. ExpectIntEQ(BN_div(r, rem, NULL, b, NULL), 0);
  34791. ExpectIntEQ(BN_div(r, rem, a, NULL, NULL), 0);
  34792. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34793. ExpectIntEQ(BN_div(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34794. ExpectIntEQ(BN_div(&emptyBN, rem, &emptyBN, &emptyBN, NULL), 0);
  34795. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, a, &emptyBN, NULL), 0);
  34796. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, b, NULL), 0);
  34797. ExpectIntEQ(BN_div(&emptyBN, rem, a, b, NULL), 0);
  34798. ExpectIntEQ(BN_div(r, &emptyBN, a, b, NULL), 0);
  34799. ExpectIntEQ(BN_div(r, rem, &emptyBN, b, NULL), 0);
  34800. ExpectIntEQ(BN_div(r, rem, a, &emptyBN, NULL), 0);
  34801. ExpectIntEQ(BN_mod(NULL, NULL, NULL, NULL), 0);
  34802. ExpectIntEQ(BN_mod(r, NULL, NULL, NULL), 0);
  34803. ExpectIntEQ(BN_mod(NULL, a, NULL, NULL), 0);
  34804. ExpectIntEQ(BN_mod(NULL, NULL, b, NULL), 0);
  34805. ExpectIntEQ(BN_mod(r, a, NULL, NULL), 0);
  34806. ExpectIntEQ(BN_mod(r, NULL, b, NULL), 0);
  34807. ExpectIntEQ(BN_mod(NULL, a, b, NULL), 0);
  34808. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34809. ExpectIntEQ(BN_mod(r, &emptyBN, &emptyBN, NULL), 0);
  34810. ExpectIntEQ(BN_mod(&emptyBN, a, &emptyBN, NULL), 0);
  34811. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, b, NULL), 0);
  34812. ExpectIntEQ(BN_mod(r, a, &emptyBN, NULL), 0);
  34813. ExpectIntEQ(BN_mod(r, &emptyBN, b, NULL), 0);
  34814. ExpectIntEQ(BN_mod(&emptyBN, a, b, NULL), 0);
  34815. /* END Invalid parameters. */
  34816. val1 = 8;
  34817. val2 = 3;
  34818. ExpectIntEQ(BN_set_word(a, val1), 1);
  34819. ExpectIntEQ(BN_set_word(b, val2), 1);
  34820. ExpectIntEQ(BN_add(r, a, b), 1);
  34821. ExpectIntEQ(BN_is_word(r, val1 + val2), 1);
  34822. ExpectIntEQ(BN_sub(r, a, b), 1);
  34823. ExpectIntEQ(BN_is_word(r, val1 - val2), 1);
  34824. ExpectIntEQ(BN_mul(r, a, b, NULL), 1);
  34825. ExpectIntEQ(BN_is_word(r, val1 * val2), 1);
  34826. ExpectIntEQ(BN_div(r, rem, a, b, NULL), 1);
  34827. ExpectIntEQ(BN_is_word(r, val1 / val2), 1);
  34828. ExpectIntEQ(BN_is_word(rem, val1 % val2), 1);
  34829. ExpectIntEQ(BN_mod(r, a, b, NULL), 1);
  34830. ExpectIntEQ(BN_is_word(r, val1 % val2), 1);
  34831. BN_free(rem);
  34832. BN_free(r);
  34833. BN_free(b);
  34834. BN_free(a);
  34835. #endif
  34836. return EXPECT_RESULT();
  34837. }
  34838. static int test_wolfSSL_BN_math_mod(void)
  34839. {
  34840. EXPECT_DECLS;
  34841. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34842. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34843. BIGNUM* a = NULL;
  34844. BIGNUM* b = NULL;
  34845. BIGNUM* m = NULL;
  34846. BIGNUM* r = NULL;
  34847. BIGNUM* t = NULL;
  34848. BIGNUM emptyBN;
  34849. BN_ULONG val1;
  34850. BN_ULONG val2;
  34851. BN_ULONG val3;
  34852. /* Setup */
  34853. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34854. ExpectNotNull(a = BN_new());
  34855. ExpectNotNull(b = BN_new());
  34856. ExpectNotNull(m = BN_new());
  34857. ExpectNotNull(r = BN_new());
  34858. /* Invalid parameters. */
  34859. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, NULL, NULL), 0);
  34860. ExpectIntEQ(BN_mod_add(r, NULL, NULL, NULL, NULL), 0);
  34861. ExpectIntEQ(BN_mod_add(NULL, a, NULL, NULL, NULL), 0);
  34862. ExpectIntEQ(BN_mod_add(NULL, NULL, b, NULL, NULL), 0);
  34863. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, m, NULL), 0);
  34864. ExpectIntEQ(BN_mod_add(NULL, a, b, m, NULL), 0);
  34865. ExpectIntEQ(BN_mod_add(r, NULL, b, m, NULL), 0);
  34866. ExpectIntEQ(BN_mod_add(r, a, NULL, m, NULL), 0);
  34867. ExpectIntEQ(BN_mod_add(r, a, m, NULL, NULL), 0);
  34868. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34869. ExpectIntEQ(BN_mod_add(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34870. ExpectIntEQ(BN_mod_add(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34871. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34872. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34873. ExpectIntEQ(BN_mod_add(&emptyBN, a, b, m, NULL), 0);
  34874. ExpectIntEQ(BN_mod_add(r, &emptyBN, b, m, NULL), 0);
  34875. ExpectIntEQ(BN_mod_add(r, a, &emptyBN, m, NULL), 0);
  34876. ExpectIntEQ(BN_mod_add(r, a, m, &emptyBN, NULL), 0);
  34877. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, NULL, NULL), 0);
  34878. ExpectIntEQ(BN_mod_mul(r, NULL, NULL, NULL, NULL), 0);
  34879. ExpectIntEQ(BN_mod_mul(NULL, a, NULL, NULL, NULL), 0);
  34880. ExpectIntEQ(BN_mod_mul(NULL, NULL, b, NULL, NULL), 0);
  34881. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, m, NULL), 0);
  34882. ExpectIntEQ(BN_mod_mul(NULL, a, b, m, NULL), 0);
  34883. ExpectIntEQ(BN_mod_mul(r, NULL, b, m, NULL), 0);
  34884. ExpectIntEQ(BN_mod_mul(r, a, NULL, m, NULL), 0);
  34885. ExpectIntEQ(BN_mod_mul(r, a, m, NULL, NULL), 0);
  34886. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34887. ExpectIntEQ(BN_mod_mul(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34888. ExpectIntEQ(BN_mod_mul(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34889. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34890. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34891. ExpectIntEQ(BN_mod_mul(&emptyBN, a, b, m, NULL), 0);
  34892. ExpectIntEQ(BN_mod_mul(r, &emptyBN, b, m, NULL), 0);
  34893. ExpectIntEQ(BN_mod_mul(r, a, &emptyBN, m, NULL), 0);
  34894. ExpectIntEQ(BN_mod_mul(r, a, m, &emptyBN, NULL), 0);
  34895. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, NULL, NULL), 0);
  34896. ExpectIntEQ(BN_mod_exp(r, NULL, NULL, NULL, NULL), 0);
  34897. ExpectIntEQ(BN_mod_exp(NULL, a, NULL, NULL, NULL), 0);
  34898. ExpectIntEQ(BN_mod_exp(NULL, NULL, b, NULL, NULL), 0);
  34899. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, m, NULL), 0);
  34900. ExpectIntEQ(BN_mod_exp(NULL, a, b, m, NULL), 0);
  34901. ExpectIntEQ(BN_mod_exp(r, NULL, b, m, NULL), 0);
  34902. ExpectIntEQ(BN_mod_exp(r, a, NULL, m, NULL), 0);
  34903. ExpectIntEQ(BN_mod_exp(r, a, m, NULL, NULL), 0);
  34904. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34905. ExpectIntEQ(BN_mod_exp(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34906. ExpectIntEQ(BN_mod_exp(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34907. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34908. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34909. ExpectIntEQ(BN_mod_exp(&emptyBN, a, b, m, NULL), 0);
  34910. ExpectIntEQ(BN_mod_exp(r, &emptyBN, b, m, NULL), 0);
  34911. ExpectIntEQ(BN_mod_exp(r, a, &emptyBN, m, NULL), 0);
  34912. ExpectIntEQ(BN_mod_exp(r, a, m, &emptyBN, NULL), 0);
  34913. ExpectNull(BN_mod_inverse(r, NULL, NULL, NULL));
  34914. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  34915. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  34916. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  34917. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  34918. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, &emptyBN, NULL));
  34919. ExpectNull(BN_mod_inverse(r, &emptyBN, &emptyBN, NULL));
  34920. ExpectNull(BN_mod_inverse(&emptyBN, a, &emptyBN, NULL));
  34921. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, m, NULL));
  34922. ExpectNull(BN_mod_inverse(&emptyBN, a, m, NULL));
  34923. ExpectNull(BN_mod_inverse(r, &emptyBN, m, NULL));
  34924. ExpectNull(BN_mod_inverse(r, a, &emptyBN, NULL));
  34925. /* END Invalid parameters. */
  34926. val1 = 9;
  34927. val2 = 13;
  34928. val3 = 5;
  34929. ExpectIntEQ(BN_set_word(a, val1), 1);
  34930. ExpectIntEQ(BN_set_word(b, val2), 1);
  34931. ExpectIntEQ(BN_set_word(m, val3), 1);
  34932. ExpectIntEQ(BN_mod_add(r, a, b, m, NULL), 1);
  34933. ExpectIntEQ(BN_is_word(r, (val1 + val2) % val3), 1);
  34934. ExpectIntEQ(BN_mod_mul(r, a, b, m, NULL), 1);
  34935. ExpectIntEQ(BN_is_word(r, (val1 * val2) % val3), 1);
  34936. ExpectIntEQ(BN_set_word(a, 2), 1);
  34937. ExpectIntEQ(BN_set_word(b, 3), 1);
  34938. ExpectIntEQ(BN_set_word(m, 5), 1);
  34939. /* (2 ^ 3) % 5 = 8 % 5 = 3 */
  34940. ExpectIntEQ(BN_mod_exp(r, a, b, m, NULL), 1);
  34941. ExpectIntEQ(BN_is_word(r, 3), 1);
  34942. /* (2 * 3) % 5 = 6 % 5 = 1 => inv = 3 */
  34943. ExpectNotNull(BN_mod_inverse(r, a, m, NULL));
  34944. ExpectIntEQ(BN_is_word(r, 3), 1);
  34945. ExpectNotNull(t = BN_mod_inverse(NULL, a, m, NULL));
  34946. ExpectIntEQ(BN_is_word(t, 3), 1);
  34947. BN_free(t);
  34948. /* No inverse case. No inverse when a divides b. */
  34949. ExpectIntEQ(BN_set_word(a, 3), 1);
  34950. ExpectIntEQ(BN_set_word(m, 9), 1);
  34951. ExpectNull(BN_mod_inverse(r, a, m, NULL));
  34952. BN_free(r);
  34953. BN_free(m);
  34954. BN_free(b);
  34955. BN_free(a);
  34956. #endif
  34957. return EXPECT_RESULT();
  34958. }
  34959. static int test_wolfSSL_BN_math_other(void)
  34960. {
  34961. EXPECT_DECLS;
  34962. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34963. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34964. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  34965. BIGNUM* a = NULL;
  34966. BIGNUM* b = NULL;
  34967. BIGNUM* r = NULL;
  34968. BIGNUM emptyBN;
  34969. /* Setup */
  34970. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34971. ExpectNotNull(a = BN_new());
  34972. ExpectNotNull(b = BN_new());
  34973. ExpectNotNull(r = BN_new());
  34974. /* Invalid parameters. */
  34975. ExpectIntEQ(BN_gcd(NULL, NULL, NULL, NULL), 0);
  34976. ExpectIntEQ(BN_gcd(r, NULL, NULL, NULL), 0);
  34977. ExpectIntEQ(BN_gcd(NULL, a, NULL, NULL), 0);
  34978. ExpectIntEQ(BN_gcd(NULL, NULL, b, NULL), 0);
  34979. ExpectIntEQ(BN_gcd(NULL, a, b, NULL), 0);
  34980. ExpectIntEQ(BN_gcd(r, NULL, b, NULL), 0);
  34981. ExpectIntEQ(BN_gcd(r, a, NULL, NULL), 0);
  34982. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34983. ExpectIntEQ(BN_gcd(r, &emptyBN, &emptyBN, NULL), 0);
  34984. ExpectIntEQ(BN_gcd(&emptyBN, a, &emptyBN, NULL), 0);
  34985. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, b, NULL), 0);
  34986. ExpectIntEQ(BN_gcd(&emptyBN, a, b, NULL), 0);
  34987. ExpectIntEQ(BN_gcd(r, &emptyBN, b, NULL), 0);
  34988. ExpectIntEQ(BN_gcd(r, a, &emptyBN, NULL), 0);
  34989. /* END Invalid parameters. */
  34990. /* No common factors between 2 and 3. */
  34991. ExpectIntEQ(BN_set_word(a, 2), 1);
  34992. ExpectIntEQ(BN_set_word(b, 3), 1);
  34993. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  34994. ExpectIntEQ(BN_is_word(r, 1), 1);
  34995. /* 3 is largest value that divides both 6 and 9. */
  34996. ExpectIntEQ(BN_set_word(a, 6), 1);
  34997. ExpectIntEQ(BN_set_word(b, 9), 1);
  34998. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  34999. ExpectIntEQ(BN_is_word(r, 3), 1);
  35000. /* GCD of 0 and 0 is undefined. */
  35001. ExpectIntEQ(BN_set_word(a, 0), 1);
  35002. ExpectIntEQ(BN_set_word(b, 0), 1);
  35003. ExpectIntEQ(BN_gcd(r, a, b, NULL), 0);
  35004. /* Teardown */
  35005. BN_free(r);
  35006. BN_free(b);
  35007. BN_free(a);
  35008. #endif
  35009. #endif
  35010. return EXPECT_RESULT();
  35011. }
  35012. static int test_wolfSSL_BN_rand(void)
  35013. {
  35014. EXPECT_DECLS;
  35015. #if defined(OPENSSL_EXTRA) && !defined(OPENSSL_EXTRA_NO_BN)
  35016. BIGNUM* bn = NULL;
  35017. BIGNUM* range = NULL;
  35018. BIGNUM emptyBN;
  35019. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35020. ExpectNotNull(bn = BN_new());
  35021. ExpectNotNull(range = BN_new());
  35022. /* Invalid parameters. */
  35023. ExpectIntEQ(BN_rand(NULL, -1, 0, 0), 0);
  35024. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  35025. ExpectIntEQ(BN_rand(NULL, 1, 0, 0), 0);
  35026. ExpectIntEQ(BN_rand(&emptyBN, -1, 0, 0), 0);
  35027. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  35028. ExpectIntEQ(BN_rand(&emptyBN, 1, 0, 0), 0);
  35029. ExpectIntEQ(BN_pseudo_rand(NULL, -1, 0, 0), 0);
  35030. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  35031. ExpectIntEQ(BN_pseudo_rand(NULL, 1, 0, 0), 0);
  35032. ExpectIntEQ(BN_pseudo_rand(&emptyBN, -1, 0, 0), 0);
  35033. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  35034. ExpectIntEQ(BN_pseudo_rand(&emptyBN, 1, 0, 0), 0);
  35035. ExpectIntEQ(BN_rand_range(NULL, NULL), 0);
  35036. ExpectIntEQ(BN_rand_range(bn, NULL), 0);
  35037. ExpectIntEQ(BN_rand_range(NULL, range), 0);
  35038. ExpectIntEQ(BN_rand_range(&emptyBN, &emptyBN), 0);
  35039. ExpectIntEQ(BN_rand_range(bn, &emptyBN), 0);
  35040. ExpectIntEQ(BN_rand_range(&emptyBN, range), 0);
  35041. /* 0 bit random value must be 0 and so cannot set bit in any position. */
  35042. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  35043. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35044. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35045. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35046. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35047. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35048. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  35049. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35050. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35051. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35052. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  35053. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35054. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35055. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35056. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35057. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35058. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  35059. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35060. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35061. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35062. /* 1 bit random value must have no more than one top bit set. */
  35063. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35064. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35065. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35066. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35067. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35068. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35069. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35070. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35071. /* END Invalid parameters. */
  35072. /* 0 bit random: 0. */
  35073. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35074. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35075. ExpectIntEQ(BN_is_zero(bn), 1);
  35076. ExpectIntEQ(BN_set_word(bn, 2), 1); /* Make sure not zero. */
  35077. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35078. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35079. ExpectIntEQ(BN_is_zero(bn), 1);
  35080. /* 1 bit random: 0 or 1. */
  35081. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  35082. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35083. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  35084. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35085. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35086. ExpectIntEQ(BN_get_word(bn), 1);
  35087. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35088. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35089. ExpectIntEQ(BN_get_word(bn), 1);
  35090. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  35091. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35092. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  35093. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35094. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35095. ExpectIntEQ(BN_get_word(bn), 1);
  35096. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35097. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35098. ExpectIntEQ(BN_get_word(bn), 1);
  35099. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35100. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35101. ExpectIntEQ(BN_num_bits(bn), 8);
  35102. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35103. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35104. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35105. ExpectIntEQ(BN_num_bits(bn), 8);
  35106. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35107. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  35108. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35109. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35110. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  35111. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  35112. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35113. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35114. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  35115. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35116. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35117. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  35118. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35119. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35120. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  35121. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  35122. * requested number of bits up to the nearest multiple of 8. E.g. in this
  35123. * case, requesting a 13-bit random number would actually return a 16-bit
  35124. * random number. */
  35125. ExpectIntEQ(BN_rand(bn, 13, WOLFSSL_BN_RAND_TOP_ONE,
  35126. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35127. ExpectIntEQ(BN_num_bits(bn), 13);
  35128. ExpectIntEQ(BN_rand(range, 64, WOLFSSL_BN_RAND_TOP_ONE,
  35129. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35130. ExpectIntEQ(BN_rand_range(bn, range), 1);
  35131. ExpectIntEQ(BN_set_word(range, 0), 1);
  35132. ExpectIntEQ(BN_rand_range(bn, range), 1);
  35133. ExpectIntEQ(BN_set_word(range, 1), 1);
  35134. ExpectIntEQ(BN_rand_range(bn, range), 1);
  35135. BN_free(bn);
  35136. BN_free(range);
  35137. #endif
  35138. return EXPECT_RESULT();
  35139. }
  35140. static int test_wolfSSL_BN_prime(void)
  35141. {
  35142. EXPECT_DECLS;
  35143. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  35144. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  35145. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  35146. BIGNUM* a = NULL;
  35147. BIGNUM* add = NULL;
  35148. BIGNUM* rem = NULL;
  35149. BIGNUM emptyBN;
  35150. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35151. ExpectNotNull(a = BN_new());
  35152. ExpectNotNull(add = BN_new());
  35153. ExpectNotNull(rem = BN_new());
  35154. /* Invalid parameters. */
  35155. /* BN_generate_prime_ex()
  35156. * prime - must have valid BIGNUM
  35157. * bits - Greater then 0
  35158. * safe - not supported, must be 0
  35159. * add - not supported, must be NULL
  35160. * rem - not supported, must be NULL
  35161. * cb - anything
  35162. */
  35163. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, rem, NULL), 0);
  35164. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, rem, NULL), 0);
  35165. ExpectIntEQ(BN_generate_prime_ex(a, -1, 1, add, rem, NULL), 0);
  35166. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 1, add, rem, NULL), 0);
  35167. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 1, add, rem, NULL), 0);
  35168. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 0, add, rem, NULL), 0);
  35169. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 0, add, rem, NULL), 0);
  35170. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, NULL, rem, NULL), 0);
  35171. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, NULL, rem, NULL), 0);
  35172. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, NULL, NULL), 0);
  35173. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, NULL, NULL), 0);
  35174. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 0, NULL, NULL, NULL), 0);
  35175. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 0, NULL, NULL, NULL), 0);
  35176. ExpectIntEQ(BN_generate_prime_ex(a, -1, 0, NULL, NULL, NULL), 0);
  35177. ExpectIntEQ(BN_generate_prime_ex(a, 0, 0, NULL, NULL, NULL), 0);
  35178. ExpectIntEQ(BN_generate_prime_ex(a, 2, 1, NULL, NULL, NULL), 0);
  35179. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, add, NULL, NULL), 0);
  35180. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, NULL, rem, NULL), 0);
  35181. ExpectIntEQ(BN_is_prime_ex(NULL, -1, NULL, NULL), -1);
  35182. ExpectIntEQ(BN_is_prime_ex(&emptyBN, -1, NULL, NULL), -1);
  35183. ExpectIntEQ(BN_is_prime_ex(a, -1, NULL, NULL), -1);
  35184. ExpectIntEQ(BN_is_prime_ex(a, 2048, NULL, NULL), -1);
  35185. ExpectIntEQ(BN_is_prime_ex(NULL, 1, NULL, NULL), -1);
  35186. ExpectIntEQ(BN_is_prime_ex(&emptyBN, 1, NULL, NULL), -1);
  35187. /* END Invalid parameters. */
  35188. ExpectIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL), 1);
  35189. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 1);
  35190. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  35191. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 0);
  35192. BN_free(rem);
  35193. BN_free(add);
  35194. BN_free(a);
  35195. #endif
  35196. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  35197. return EXPECT_RESULT();
  35198. }
  35199. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35200. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35201. #define TEST_ARG 0x1234
  35202. static void msg_cb(int write_p, int version, int content_type,
  35203. const void *buf, size_t len, SSL *ssl, void *arg)
  35204. {
  35205. (void)write_p;
  35206. (void)version;
  35207. (void)content_type;
  35208. (void)buf;
  35209. (void)len;
  35210. (void)ssl;
  35211. AssertTrue(arg == (void*)TEST_ARG);
  35212. }
  35213. #endif
  35214. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  35215. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35216. #if defined(SESSION_CERTS)
  35217. #include "wolfssl/internal.h"
  35218. #endif
  35219. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  35220. {
  35221. EXPECT_DECLS;
  35222. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  35223. STACK_OF(X509)* sk = NULL;
  35224. X509* x509 = NULL;
  35225. int i, num;
  35226. BIO* bio = NULL;
  35227. #endif
  35228. ExpectNotNull(ctx);
  35229. ExpectNotNull(ssl);
  35230. fprintf(stderr, "\n===== msgcb called ====\n");
  35231. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  35232. ExpectTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  35233. ExpectIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  35234. ExpectNotNull(SSL_get0_verified_chain(ssl));
  35235. #endif
  35236. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  35237. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  35238. ExpectNotNull(sk = SSL_get_peer_cert_chain(ssl));
  35239. if (sk == NULL) {
  35240. BIO_free(bio);
  35241. return TEST_FAIL;
  35242. }
  35243. num = sk_X509_num(sk);
  35244. ExpectTrue(num > 0);
  35245. for (i = 0; i < num; i++) {
  35246. ExpectNotNull(x509 = sk_X509_value(sk,i));
  35247. if (x509 == NULL)
  35248. break;
  35249. fprintf(stderr, "Certificate at index [%d] = :\n",i);
  35250. X509_print(bio,x509);
  35251. fprintf(stderr, "\n\n");
  35252. }
  35253. BIO_free(bio);
  35254. #endif
  35255. return EXPECT_RESULT();
  35256. }
  35257. #endif
  35258. static int test_wolfSSL_msgCb(void)
  35259. {
  35260. EXPECT_DECLS;
  35261. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  35262. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35263. test_ssl_cbf client_cb;
  35264. test_ssl_cbf server_cb;
  35265. XMEMSET(&client_cb, 0, sizeof(client_cb));
  35266. XMEMSET(&server_cb, 0, sizeof(server_cb));
  35267. #ifndef WOLFSSL_NO_TLS12
  35268. client_cb.method = wolfTLSv1_2_client_method;
  35269. server_cb.method = wolfTLSv1_2_server_method;
  35270. #else
  35271. client_cb.method = wolfTLSv1_3_client_method;
  35272. server_cb.method = wolfTLSv1_3_server_method;
  35273. #endif
  35274. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  35275. &server_cb, msgCb), TEST_SUCCESS);
  35276. #endif
  35277. return EXPECT_RESULT();
  35278. }
  35279. static int test_wolfSSL_either_side(void)
  35280. {
  35281. EXPECT_DECLS;
  35282. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  35283. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35284. test_ssl_cbf client_cb;
  35285. test_ssl_cbf server_cb;
  35286. XMEMSET(&client_cb, 0, sizeof(client_cb));
  35287. XMEMSET(&server_cb, 0, sizeof(server_cb));
  35288. /* Use different CTX for client and server */
  35289. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  35290. ExpectNotNull(client_cb.ctx);
  35291. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  35292. ExpectNotNull(server_cb.ctx);
  35293. /* we are responsible for free'ing WOLFSSL_CTX */
  35294. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  35295. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  35296. &server_cb, NULL), TEST_SUCCESS);
  35297. wolfSSL_CTX_free(client_cb.ctx);
  35298. wolfSSL_CTX_free(server_cb.ctx);
  35299. #endif
  35300. return EXPECT_RESULT();
  35301. }
  35302. static int test_wolfSSL_DTLS_either_side(void)
  35303. {
  35304. EXPECT_DECLS;
  35305. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  35306. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  35307. test_ssl_cbf client_cb;
  35308. test_ssl_cbf server_cb;
  35309. XMEMSET(&client_cb, 0, sizeof(client_cb));
  35310. XMEMSET(&server_cb, 0, sizeof(server_cb));
  35311. /* Use different CTX for client and server */
  35312. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  35313. ExpectNotNull(client_cb.ctx);
  35314. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  35315. ExpectNotNull(server_cb.ctx);
  35316. /* we are responsible for free'ing WOLFSSL_CTX */
  35317. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  35318. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  35319. &server_cb, NULL), TEST_SUCCESS);
  35320. wolfSSL_CTX_free(client_cb.ctx);
  35321. wolfSSL_CTX_free(server_cb.ctx);
  35322. #endif
  35323. return EXPECT_RESULT();
  35324. }
  35325. static int test_generate_cookie(void)
  35326. {
  35327. EXPECT_DECLS;
  35328. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  35329. SSL_CTX* ctx = NULL;
  35330. SSL* ssl = NULL;
  35331. byte buf[FOURK_BUF] = {0};
  35332. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  35333. ExpectNotNull(ssl = SSL_new(ctx));
  35334. /* Test unconnected */
  35335. ExpectIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  35336. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  35337. wolfSSL_SetCookieCtx(ssl, ctx);
  35338. ExpectNotNull(wolfSSL_GetCookieCtx(ssl));
  35339. ExpectNull(wolfSSL_GetCookieCtx(NULL));
  35340. SSL_free(ssl);
  35341. SSL_CTX_free(ctx);
  35342. #endif
  35343. return EXPECT_RESULT();
  35344. }
  35345. static int test_wolfSSL_set_options(void)
  35346. {
  35347. EXPECT_DECLS;
  35348. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35349. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35350. WOLFSSL* ssl = NULL;
  35351. WOLFSSL_CTX* ctx = NULL;
  35352. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  35353. char appData[] = "extra msg";
  35354. #endif
  35355. #ifdef OPENSSL_EXTRA
  35356. unsigned char protos[] = {
  35357. 7, 't', 'l', 's', '/', '1', '.', '2',
  35358. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  35359. };
  35360. unsigned int len = sizeof(protos);
  35361. void *arg = (void *)TEST_ARG;
  35362. #endif
  35363. #ifndef NO_WOLFSSL_SERVER
  35364. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  35365. #else
  35366. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35367. #endif
  35368. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  35369. WOLFSSL_FILETYPE_PEM));
  35370. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  35371. WOLFSSL_FILETYPE_PEM));
  35372. ExpectTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  35373. == WOLFSSL_OP_NO_TLSv1);
  35374. ExpectTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  35375. ExpectIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  35376. WOLFSSL_OP_NO_SSLv2)), 0);
  35377. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  35378. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  35379. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  35380. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  35381. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  35382. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  35383. ExpectFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  35384. WOLFSSL_OP_NO_COMPRESSION));
  35385. wolfSSL_CTX_free(ctx);
  35386. ctx = NULL;
  35387. #ifndef NO_WOLFSSL_SERVER
  35388. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  35389. #else
  35390. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35391. #endif
  35392. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  35393. WOLFSSL_FILETYPE_PEM));
  35394. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  35395. WOLFSSL_FILETYPE_PEM));
  35396. #ifdef OPENSSL_EXTRA
  35397. ExpectTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  35398. #endif
  35399. ExpectNotNull(ssl = wolfSSL_new(ctx));
  35400. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  35401. #ifdef HAVE_EX_DATA
  35402. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  35403. ExpectNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  35404. if (ssl != NULL) {
  35405. ExpectIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  35406. appData, sizeof(appData)), 0);
  35407. }
  35408. #else
  35409. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  35410. ExpectNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  35411. #endif
  35412. #endif
  35413. ExpectTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  35414. WOLFSSL_OP_NO_TLSv1);
  35415. ExpectTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  35416. ExpectIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  35417. WOLFSSL_OP_NO_SSLv2)), 0);
  35418. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  35419. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  35420. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  35421. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  35422. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  35423. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  35424. #ifdef OPENSSL_EXTRA
  35425. ExpectFalse((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  35426. WOLFSSL_OP_NO_COMPRESSION));
  35427. #endif
  35428. #ifdef OPENSSL_EXTRA
  35429. ExpectTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  35430. wolfSSL_set_msg_callback_arg(ssl, arg);
  35431. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  35432. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  35433. #else
  35434. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  35435. #endif
  35436. #endif
  35437. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  35438. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  35439. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  35440. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  35441. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  35442. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  35443. #else
  35444. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  35445. #endif
  35446. #endif /* HAVE_ALPN && !NO_BIO */
  35447. #endif
  35448. wolfSSL_free(ssl);
  35449. wolfSSL_CTX_free(ctx);
  35450. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35451. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35452. return EXPECT_RESULT();
  35453. }
  35454. static int test_wolfSSL_sk_SSL_CIPHER(void)
  35455. {
  35456. EXPECT_DECLS;
  35457. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  35458. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35459. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35460. SSL* ssl = NULL;
  35461. SSL_CTX* ctx = NULL;
  35462. STACK_OF(SSL_CIPHER) *sk = NULL;
  35463. STACK_OF(SSL_CIPHER) *dupSk = NULL;
  35464. #ifndef NO_WOLFSSL_SERVER
  35465. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35466. #else
  35467. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35468. #endif
  35469. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  35470. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35471. ExpectNotNull(ssl = SSL_new(ctx));
  35472. ExpectNotNull(sk = SSL_get_ciphers(ssl));
  35473. ExpectNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  35474. ExpectIntGT(sk_SSL_CIPHER_num(sk), 0);
  35475. ExpectIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  35476. /* error case because connection has not been established yet */
  35477. ExpectIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  35478. sk_SSL_CIPHER_free(dupSk);
  35479. /* sk is pointer to internal struct that should be free'd in SSL_free */
  35480. SSL_free(ssl);
  35481. SSL_CTX_free(ctx);
  35482. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35483. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35484. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35485. return EXPECT_RESULT();
  35486. }
  35487. static int test_wolfSSL_set1_curves_list(void)
  35488. {
  35489. EXPECT_DECLS;
  35490. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  35491. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35492. SSL* ssl = NULL;
  35493. SSL_CTX* ctx = NULL;
  35494. #ifndef NO_WOLFSSL_SERVER
  35495. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35496. #else
  35497. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35498. #endif
  35499. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  35500. SSL_FILETYPE_PEM));
  35501. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  35502. ExpectNotNull(ssl = SSL_new(ctx));
  35503. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  35504. #ifdef HAVE_ECC
  35505. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  35506. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  35507. #endif
  35508. #ifdef HAVE_CURVE25519
  35509. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  35510. #else
  35511. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  35512. #endif
  35513. #ifdef HAVE_CURVE448
  35514. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  35515. #else
  35516. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  35517. #endif
  35518. ExpectIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  35519. #ifdef HAVE_ECC
  35520. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  35521. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  35522. #endif
  35523. #ifdef HAVE_CURVE25519
  35524. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  35525. #else
  35526. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  35527. #endif
  35528. #ifdef HAVE_CURVE448
  35529. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  35530. #else
  35531. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  35532. #endif
  35533. SSL_free(ssl);
  35534. SSL_CTX_free(ctx);
  35535. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35536. #endif
  35537. return EXPECT_RESULT();
  35538. }
  35539. static int test_wolfSSL_set1_sigalgs_list(void)
  35540. {
  35541. EXPECT_DECLS;
  35542. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  35543. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35544. SSL* ssl = NULL;
  35545. SSL_CTX* ctx = NULL;
  35546. #ifndef NO_WOLFSSL_SERVER
  35547. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35548. #else
  35549. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35550. #endif
  35551. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  35552. SSL_FILETYPE_PEM));
  35553. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35554. ExpectNotNull(ssl = SSL_new(ctx));
  35555. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  35556. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  35557. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  35558. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  35559. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  35560. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  35561. #ifndef NO_RSA
  35562. #ifndef NO_SHA256
  35563. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  35564. WOLFSSL_FAILURE);
  35565. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  35566. WOLFSSL_FAILURE);
  35567. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  35568. WOLFSSL_SUCCESS);
  35569. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  35570. WOLFSSL_SUCCESS);
  35571. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  35572. WOLFSSL_FAILURE);
  35573. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  35574. WOLFSSL_FAILURE);
  35575. #ifdef WC_RSA_PSS
  35576. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  35577. WOLFSSL_SUCCESS);
  35578. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  35579. WOLFSSL_SUCCESS);
  35580. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  35581. WOLFSSL_SUCCESS);
  35582. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  35583. WOLFSSL_SUCCESS);
  35584. #endif
  35585. #ifdef WOLFSSL_SHA512
  35586. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35587. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  35588. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35589. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  35590. #elif defined(WOLFSSL_SHA384)
  35591. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35592. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  35593. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35594. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  35595. #endif
  35596. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  35597. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  35598. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  35599. WOLFSSL_FAILURE);
  35600. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  35601. WOLFSSL_FAILURE);
  35602. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  35603. WOLFSSL_FAILURE);
  35604. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  35605. WOLFSSL_FAILURE);
  35606. #endif
  35607. #endif
  35608. #ifdef HAVE_ECC
  35609. #ifndef NO_SHA256
  35610. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  35611. WOLFSSL_SUCCESS);
  35612. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"),
  35613. WOLFSSL_SUCCESS);
  35614. #ifdef WOLFSSL_SHA512
  35615. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35616. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  35617. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35618. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  35619. #elif defined(WOLFSSL_SHA384)
  35620. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35621. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  35622. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35623. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  35624. #endif
  35625. #endif
  35626. #endif
  35627. #ifdef HAVE_ED25519
  35628. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  35629. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  35630. #endif
  35631. #ifdef HAVE_ED448
  35632. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  35633. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  35634. #endif
  35635. #ifndef NO_DSA
  35636. #ifndef NO_SHA256
  35637. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  35638. WOLFSSL_SUCCESS);
  35639. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  35640. WOLFSSL_SUCCESS);
  35641. #endif
  35642. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  35643. defined(WOLFSSL_ALLOW_TLS_SHA1))
  35644. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  35645. WOLFSSL_SUCCESS);
  35646. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  35647. WOLFSSL_SUCCESS);
  35648. #endif
  35649. #endif
  35650. SSL_free(ssl);
  35651. SSL_CTX_free(ctx);
  35652. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35653. #endif
  35654. return EXPECT_RESULT();
  35655. }
  35656. /* Testing wolfSSL_set_tlsext_status_type function.
  35657. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  35658. */
  35659. static int test_wolfSSL_set_tlsext_status_type(void)
  35660. {
  35661. EXPECT_DECLS;
  35662. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  35663. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  35664. SSL* ssl = NULL;
  35665. SSL_CTX* ctx = NULL;
  35666. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35667. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  35668. SSL_FILETYPE_PEM));
  35669. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35670. ExpectNotNull(ssl = SSL_new(ctx));
  35671. ExpectIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  35672. SSL_SUCCESS);
  35673. ExpectIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  35674. SSL_free(ssl);
  35675. SSL_CTX_free(ctx);
  35676. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  35677. return EXPECT_RESULT();
  35678. }
  35679. #ifndef NO_BIO
  35680. static int test_wolfSSL_PEM_read_bio(void)
  35681. {
  35682. EXPECT_DECLS;
  35683. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35684. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35685. byte buff[6000];
  35686. XFILE f = XBADFILE;
  35687. int bytes;
  35688. X509* x509 = NULL;
  35689. BIO* bio = NULL;
  35690. BUF_MEM* buf = NULL;
  35691. ExpectTrue((f = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  35692. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  35693. if (f != XBADFILE)
  35694. XFCLOSE(f);
  35695. ExpectNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  35696. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  35697. ExpectIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  35698. ExpectNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  35699. ExpectIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  35700. /* BIO should return the set EOF value */
  35701. ExpectIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  35702. ExpectIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  35703. ExpectIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  35704. ExpectIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  35705. BIO_free(bio);
  35706. BUF_MEM_free(buf);
  35707. X509_free(x509);
  35708. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  35709. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35710. return EXPECT_RESULT();
  35711. }
  35712. #if defined(OPENSSL_EXTRA)
  35713. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  35714. long argl, long ret)
  35715. {
  35716. (void)bio;
  35717. (void)cmd;
  35718. (void)argp;
  35719. (void)argi;
  35720. (void)argl;
  35721. return ret;
  35722. }
  35723. #endif
  35724. static int test_wolfSSL_BIO(void)
  35725. {
  35726. EXPECT_DECLS;
  35727. #if defined(OPENSSL_EXTRA)
  35728. const unsigned char* p = NULL;
  35729. byte buff[20];
  35730. BIO* bio1 = NULL;
  35731. BIO* bio2 = NULL;
  35732. BIO* bio3 = NULL;
  35733. char* bufPt = NULL;
  35734. int i;
  35735. for (i = 0; i < 20; i++) {
  35736. buff[i] = i;
  35737. }
  35738. /* test BIO_free with NULL */
  35739. ExpectIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  35740. /* Creating and testing type BIO_s_bio */
  35741. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  35742. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  35743. ExpectNotNull(bio3 = BIO_new(BIO_s_bio()));
  35744. /* read/write before set up */
  35745. ExpectIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  35746. ExpectIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  35747. ExpectIntEQ(BIO_set_nbio(bio1, 1), 1);
  35748. ExpectIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  35749. ExpectIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  35750. ExpectIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  35751. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  35752. ExpectNotNull(XMEMCPY(bufPt, buff, 10));
  35753. ExpectIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  35754. /* write buffer full */
  35755. ExpectIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  35756. ExpectIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  35757. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 0);
  35758. /* write the other direction with pair */
  35759. ExpectIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  35760. ExpectNotNull(XMEMCPY(bufPt, buff, 8));
  35761. ExpectIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  35762. /* try read */
  35763. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 8);
  35764. ExpectIntEQ((int)BIO_ctrl_pending(bio2), 20);
  35765. /* try read using ctrl function */
  35766. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  35767. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  35768. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  35769. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  35770. ExpectIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  35771. for (i = 0; i < 20; i++) {
  35772. ExpectIntEQ((int)bufPt[i], i);
  35773. }
  35774. ExpectIntEQ(BIO_nread(bio2, &bufPt, 1), 0);
  35775. ExpectIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  35776. for (i = 0; i < 8; i++) {
  35777. ExpectIntEQ((int)bufPt[i], i);
  35778. }
  35779. ExpectIntEQ(BIO_nread(bio1, &bufPt, 1), 0);
  35780. ExpectIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  35781. /* new pair */
  35782. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  35783. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  35784. bio2 = NULL;
  35785. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  35786. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  35787. ExpectIntEQ(BIO_nread(bio3, &bufPt, 10), 0);
  35788. /* test wrap around... */
  35789. ExpectIntEQ(BIO_reset(bio1), 0);
  35790. ExpectIntEQ(BIO_reset(bio3), 0);
  35791. /* fill write buffer, read only small amount then write again */
  35792. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35793. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  35794. ExpectIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  35795. for (i = 0; i < 4; i++) {
  35796. ExpectIntEQ(bufPt[i], i);
  35797. }
  35798. /* try writing over read index */
  35799. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  35800. ExpectNotNull(XMEMSET(bufPt, 0, 4));
  35801. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 20);
  35802. /* read and write 0 bytes */
  35803. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  35804. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  35805. /* should read only to end of write buffer then need to read again */
  35806. ExpectIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  35807. for (i = 0; i < 16; i++) {
  35808. ExpectIntEQ(bufPt[i], buff[4 + i]);
  35809. }
  35810. ExpectIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  35811. ExpectIntEQ(BIO_nread0(bio3, &bufPt), 4);
  35812. for (i = 0; i < 4; i++) {
  35813. ExpectIntEQ(bufPt[i], 0);
  35814. }
  35815. /* read index should not have advanced with nread0 */
  35816. ExpectIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  35817. for (i = 0; i < 4; i++) {
  35818. ExpectIntEQ(bufPt[i], 0);
  35819. }
  35820. /* write and fill up buffer checking reset of index state */
  35821. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35822. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  35823. /* test reset on data in bio1 write buffer */
  35824. ExpectIntEQ(BIO_reset(bio1), 0);
  35825. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  35826. ExpectIntEQ(BIO_nread(bio3, &bufPt, 3), 0);
  35827. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35828. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  35829. ExpectNotNull(p);
  35830. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  35831. ExpectIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  35832. for (i = 0; i < 6; i++) {
  35833. ExpectIntEQ(bufPt[i], i);
  35834. }
  35835. /* test case of writing twice with offset read index */
  35836. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  35837. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  35838. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35839. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  35840. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35841. ExpectIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  35842. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  35843. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35844. BIO_free(bio1);
  35845. bio1 = NULL;
  35846. BIO_free(bio3);
  35847. bio3 = NULL;
  35848. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  35849. {
  35850. BIO* bioA = NULL;
  35851. BIO* bioB = NULL;
  35852. ExpectIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  35853. ExpectIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  35854. BIO_free(bioA);
  35855. bioA = NULL;
  35856. BIO_free(bioB);
  35857. bioB = NULL;
  35858. }
  35859. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  35860. /* BIOs with file pointers */
  35861. #if !defined(NO_FILESYSTEM)
  35862. {
  35863. XFILE f1 = XBADFILE;
  35864. XFILE f2 = XBADFILE;
  35865. BIO* f_bio1 = NULL;
  35866. BIO* f_bio2 = NULL;
  35867. unsigned char cert[300];
  35868. char testFile[] = "tests/bio_write_test.txt";
  35869. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  35870. ExpectNotNull(f_bio1 = BIO_new(BIO_s_file()));
  35871. ExpectNotNull(f_bio2 = BIO_new(BIO_s_file()));
  35872. /* Failure due to wrong BIO type */
  35873. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  35874. ExpectIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  35875. ExpectTrue((f1 = XFOPEN(svrCertFile, "rwb")) != XBADFILE);
  35876. ExpectIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  35877. ExpectIntEQ(BIO_write_filename(f_bio2, testFile),
  35878. WOLFSSL_SUCCESS);
  35879. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  35880. ExpectIntEQ(BIO_tell(f_bio1),sizeof(cert));
  35881. ExpectIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  35882. ExpectIntEQ(BIO_tell(f_bio2),sizeof(msg));
  35883. ExpectIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  35884. ExpectIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  35885. ExpectIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  35886. ExpectIntEQ(BIO_reset(f_bio2), 0);
  35887. ExpectIntEQ(BIO_tell(NULL),-1);
  35888. ExpectIntEQ(BIO_tell(f_bio2),0);
  35889. ExpectIntEQ(BIO_seek(f_bio2, 4), 0);
  35890. ExpectIntEQ(BIO_tell(f_bio2),4);
  35891. BIO_free(f_bio1);
  35892. f_bio1 = NULL;
  35893. BIO_free(f_bio2);
  35894. f_bio2 = NULL;
  35895. ExpectNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  35896. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  35897. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  35898. BIO_free(f_bio1);
  35899. f_bio1 = NULL;
  35900. }
  35901. #endif /* !defined(NO_FILESYSTEM) */
  35902. /* BIO info callback */
  35903. {
  35904. const char* testArg = "test";
  35905. BIO* cb_bio = NULL;
  35906. ExpectNotNull(cb_bio = BIO_new(BIO_s_mem()));
  35907. BIO_set_callback(cb_bio, bioCallback);
  35908. ExpectNotNull(BIO_get_callback(cb_bio));
  35909. BIO_set_callback(cb_bio, NULL);
  35910. ExpectNull(BIO_get_callback(cb_bio));
  35911. BIO_set_callback_arg(cb_bio, (char*)testArg);
  35912. ExpectStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  35913. ExpectNull(BIO_get_callback_arg(NULL));
  35914. BIO_free(cb_bio);
  35915. cb_bio = NULL;
  35916. }
  35917. /* BIO_vfree */
  35918. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  35919. BIO_vfree(NULL);
  35920. BIO_vfree(bio1);
  35921. #endif
  35922. return EXPECT_RESULT();
  35923. }
  35924. static int test_wolfSSL_BIO_BIO_ring_read(void)
  35925. {
  35926. EXPECT_DECLS;
  35927. #if defined(OPENSSL_ALL)
  35928. BIO* bio1 = NULL;
  35929. BIO* bio2 = NULL;
  35930. byte data[50];
  35931. byte tmp[50];
  35932. XMEMSET(data, 42, sizeof(data));
  35933. ExpectIntEQ(BIO_new_bio_pair(&bio1, sizeof(data), &bio2, sizeof(data)),
  35934. SSL_SUCCESS);
  35935. ExpectIntEQ(BIO_write(bio1, data, 40), 40);
  35936. ExpectIntEQ(BIO_read(bio1, tmp, 20), -1);
  35937. ExpectIntEQ(BIO_read(bio2, tmp, 20), 20);
  35938. ExpectBufEQ(tmp, data, 20);
  35939. ExpectIntEQ(BIO_write(bio1, data, 20), 20);
  35940. ExpectIntEQ(BIO_read(bio2, tmp, 40), 40);
  35941. ExpectBufEQ(tmp, data, 40);
  35942. BIO_free(bio1);
  35943. BIO_free(bio2);
  35944. #endif
  35945. return EXPECT_RESULT();
  35946. }
  35947. #endif /* !NO_BIO */
  35948. static int test_wolfSSL_a2i_IPADDRESS(void)
  35949. {
  35950. EXPECT_DECLS;
  35951. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  35952. const unsigned char* data = NULL;
  35953. int dataSz = 0;
  35954. ASN1_OCTET_STRING *st = NULL;
  35955. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  35956. const unsigned char ipv6_exp[] = {
  35957. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  35958. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  35959. };
  35960. const unsigned char ipv6_home[] = {
  35961. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  35962. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  35963. };
  35964. ExpectNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  35965. ExpectNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  35966. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  35967. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP4_ADDR_LEN);
  35968. ExpectIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  35969. ASN1_STRING_free(st);
  35970. st = NULL;
  35971. ExpectNotNull(st = a2i_IPADDRESS("::1"));
  35972. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  35973. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  35974. ExpectIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  35975. ASN1_STRING_free(st);
  35976. st = NULL;
  35977. ExpectNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  35978. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  35979. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  35980. ExpectIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  35981. ASN1_STRING_free(st);
  35982. #endif
  35983. return EXPECT_RESULT();
  35984. }
  35985. static int test_wolfSSL_X509_cmp_time(void)
  35986. {
  35987. EXPECT_DECLS;
  35988. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  35989. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  35990. WOLFSSL_ASN1_TIME asn_time;
  35991. time_t t;
  35992. ExpectIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  35993. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  35994. ExpectIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  35995. ExpectIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  35996. ExpectIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  35997. #endif
  35998. return EXPECT_RESULT();
  35999. }
  36000. static int test_wolfSSL_X509_time_adj(void)
  36001. {
  36002. EXPECT_DECLS;
  36003. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  36004. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  36005. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  36006. !defined(NO_ASN_TIME)
  36007. X509* x509 = NULL;
  36008. time_t t;
  36009. time_t not_before;
  36010. time_t not_after;
  36011. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  36012. client_cert_der_2048, sizeof_client_cert_der_2048,
  36013. WOLFSSL_FILETYPE_ASN1));
  36014. t = 0;
  36015. not_before = wc_Time(0);
  36016. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  36017. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  36018. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  36019. /* Check X509_gmtime_adj, too. */
  36020. ExpectNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  36021. X509_free(x509);
  36022. #endif
  36023. return EXPECT_RESULT();
  36024. }
  36025. static int test_wolfSSL_X509(void)
  36026. {
  36027. EXPECT_DECLS;
  36028. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  36029. !defined(NO_RSA)
  36030. X509* x509 = NULL;
  36031. #ifndef NO_BIO
  36032. BIO* bio = NULL;
  36033. X509_STORE_CTX* ctx = NULL;
  36034. X509_STORE* store = NULL;
  36035. #endif
  36036. char der[] = "certs/ca-cert.der";
  36037. XFILE fp = XBADFILE;
  36038. ExpectNotNull(x509 = X509_new());
  36039. X509_free(x509);
  36040. x509 = NULL;
  36041. #ifndef NO_BIO
  36042. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  36043. SSL_FILETYPE_PEM));
  36044. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36045. #ifdef WOLFSSL_CERT_GEN
  36046. ExpectIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  36047. #endif
  36048. ExpectNotNull(ctx = X509_STORE_CTX_new());
  36049. ExpectIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  36050. ExpectNotNull(store = X509_STORE_new());
  36051. ExpectIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  36052. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  36053. ExpectIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  36054. X509_STORE_CTX_free(ctx);
  36055. X509_STORE_free(store);
  36056. X509_free(x509);
  36057. x509 = NULL;
  36058. BIO_free(bio);
  36059. #endif
  36060. /** d2i_X509_fp test **/
  36061. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  36062. ExpectNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  36063. ExpectNotNull(x509);
  36064. X509_free(x509);
  36065. x509 = NULL;
  36066. if (fp != XBADFILE) {
  36067. XFCLOSE(fp);
  36068. fp = XBADFILE;
  36069. }
  36070. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  36071. ExpectNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  36072. ExpectNotNull(x509);
  36073. X509_free(x509);
  36074. if (fp != XBADFILE)
  36075. XFCLOSE(fp);
  36076. /* X509_up_ref test */
  36077. ExpectIntEQ(X509_up_ref(NULL), 0);
  36078. ExpectNotNull(x509 = X509_new()); /* refCount = 1 */
  36079. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  36080. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  36081. X509_free(x509); /* refCount = 2 */
  36082. X509_free(x509); /* refCount = 1 */
  36083. X509_free(x509); /* refCount = 0, free */
  36084. #endif
  36085. return EXPECT_RESULT();
  36086. }
  36087. static int test_wolfSSL_X509_get_ext_count(void)
  36088. {
  36089. EXPECT_DECLS;
  36090. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  36091. !defined(NO_RSA)
  36092. int ret = 0;
  36093. WOLFSSL_X509* x509 = NULL;
  36094. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  36095. XFILE f = XBADFILE;
  36096. /* NULL parameter check */
  36097. ExpectIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  36098. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  36099. SSL_FILETYPE_PEM));
  36100. ExpectIntEQ(X509_get_ext_count(x509), 5);
  36101. wolfSSL_X509_free(x509);
  36102. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  36103. SSL_FILETYPE_PEM));
  36104. ExpectIntEQ(X509_get_ext_count(x509), 5);
  36105. wolfSSL_X509_free(x509);
  36106. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  36107. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  36108. if (f != XBADFILE)
  36109. XFCLOSE(f);
  36110. /* wolfSSL_X509_get_ext_count() valid input */
  36111. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  36112. /* wolfSSL_X509_get_ext_count() NULL argument */
  36113. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  36114. wolfSSL_X509_free(x509);
  36115. #endif
  36116. return EXPECT_RESULT();
  36117. }
  36118. static int test_wolfSSL_X509_sign2(void)
  36119. {
  36120. EXPECT_DECLS;
  36121. /* test requires WOLFSSL_AKID_NAME to match expected output */
  36122. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  36123. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  36124. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_AKID_NAME) && \
  36125. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  36126. defined(WOLFSSL_IP_ALT_NAME))
  36127. WOLFSSL_X509 *x509 = NULL;
  36128. WOLFSSL_X509 *ca = NULL;
  36129. const unsigned char *der = NULL;
  36130. const unsigned char *pt = NULL;
  36131. WOLFSSL_EVP_PKEY *priv = NULL;
  36132. WOLFSSL_X509_NAME *name = NULL;
  36133. int derSz;
  36134. #ifndef NO_ASN_TIME
  36135. WOLFSSL_ASN1_TIME *notBefore = NULL;
  36136. WOLFSSL_ASN1_TIME *notAfter = NULL;
  36137. const int year = 365*24*60*60;
  36138. const int day = 24*60*60;
  36139. const int hour = 60*60;
  36140. const int mini = 60;
  36141. time_t t;
  36142. #endif
  36143. const unsigned char expected[] = {
  36144. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xFB, 0xA0, 0x03, 0x02, 0x01,
  36145. 0x02, 0x02, 0x14, 0x08, 0xB0, 0x54, 0x7A, 0x03, 0x5A, 0xEC, 0x55, 0x8A,
  36146. 0x12, 0xE8, 0xF9, 0x8E, 0x34, 0xB6, 0x13, 0xD9, 0x59, 0xB8, 0xE8, 0x30,
  36147. 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
  36148. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
  36149. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  36150. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61,
  36151. 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  36152. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03,
  36153. 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6F, 0x6F, 0x74,
  36154. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x0A,
  36155. 0x43, 0x6F, 0x6E, 0x73, 0x75, 0x6C, 0x74, 0x69, 0x6E, 0x67, 0x31, 0x18,
  36156. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77,
  36157. 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
  36158. 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  36159. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F,
  36160. 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E, 0x17,
  36161. 0x0D, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  36162. 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  36163. 0x33, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30, 0x09,
  36164. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  36165. 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74,
  36166. 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07,
  36167. 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15, 0x30,
  36168. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C, 0x77, 0x6F, 0x6C, 0x66,
  36169. 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  36170. 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50, 0x72, 0x6F, 0x67, 0x72,
  36171. 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32, 0x30, 0x34, 0x38, 0x31,
  36172. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  36173. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
  36174. 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  36175. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
  36176. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x82,
  36177. 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  36178. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82,
  36179. 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE,
  36180. 0x39, 0xA4, 0x32, 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C, 0x74,
  36181. 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47, 0xD6, 0xA6, 0x36, 0xB2, 0x07,
  36182. 0x32, 0x8E, 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4, 0x81,
  36183. 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67, 0xBB, 0xA1, 0x75, 0xC8, 0x36,
  36184. 0x2C, 0x4A, 0xD2, 0x1B, 0xF7, 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF, 0xEC,
  36185. 0xF1, 0x81, 0x1E, 0x7B, 0x9B, 0x03, 0x47, 0x9A, 0xBF, 0x65, 0xCC, 0x7F,
  36186. 0x65, 0x24, 0x69, 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7, 0xC5,
  36187. 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A, 0x7A, 0x78, 0xE1, 0x01, 0x56,
  36188. 0x56, 0x91, 0xA6, 0x13, 0x42, 0x8D, 0xD2, 0x3C, 0x40, 0x9C, 0x4C, 0xEF,
  36189. 0xD1, 0x86, 0xDF, 0x37, 0x51, 0x1B, 0x0C, 0xA1, 0x3B, 0xF5, 0xF1, 0xA3,
  36190. 0x4A, 0x35, 0xE4, 0xE1, 0xCE, 0x96, 0xDF, 0x1B, 0x7E, 0xBF, 0x4E, 0x97,
  36191. 0xD0, 0x10, 0xE8, 0xA8, 0x08, 0x30, 0x81, 0xAF, 0x20, 0x0B, 0x43, 0x14,
  36192. 0xC5, 0x74, 0x67, 0xB4, 0x32, 0x82, 0x6F, 0x8D, 0x86, 0xC2, 0x88, 0x40,
  36193. 0x99, 0x36, 0x83, 0xBA, 0x1E, 0x40, 0x72, 0x22, 0x17, 0xD7, 0x52, 0x65,
  36194. 0x24, 0x73, 0xB0, 0xCE, 0xEF, 0x19, 0xCD, 0xAE, 0xFF, 0x78, 0x6C, 0x7B,
  36195. 0xC0, 0x12, 0x03, 0xD4, 0x4E, 0x72, 0x0D, 0x50, 0x6D, 0x3B, 0xA3, 0x3B,
  36196. 0xA3, 0x99, 0x5E, 0x9D, 0xC8, 0xD9, 0x0C, 0x85, 0xB3, 0xD9, 0x8A, 0xD9,
  36197. 0x54, 0x26, 0xDB, 0x6D, 0xFA, 0xAC, 0xBB, 0xFF, 0x25, 0x4C, 0xC4, 0xD1,
  36198. 0x79, 0xF4, 0x71, 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, 0xB5, 0x72,
  36199. 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, 0x2D, 0x56, 0x2F, 0xD7, 0x15, 0xF7,
  36200. 0x7F, 0xC0, 0xAE, 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, 0xD3, 0x02,
  36201. 0x03, 0x01, 0x00, 0x01, 0xA3, 0x82, 0x01, 0x4F, 0x30, 0x82, 0x01, 0x4B,
  36202. 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  36203. 0x01, 0xFF, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30,
  36204. 0x13, 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63,
  36205. 0x6F, 0x6D, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03,
  36206. 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  36207. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  36208. 0x85, 0x65, 0xC0, 0x30, 0x81, 0xDE, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04,
  36209. 0x81, 0xD6, 0x30, 0x81, 0xD3, 0x80, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  36210. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  36211. 0x85, 0x65, 0xC0, 0xA1, 0x81, 0xA4, 0xA4, 0x81, 0xA1, 0x30, 0x81, 0x9E,
  36212. 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  36213. 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07,
  36214. 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06,
  36215. 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61,
  36216. 0x6E, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C,
  36217. 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38,
  36218. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50,
  36219. 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32,
  36220. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  36221. 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  36222. 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A,
  36223. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E,
  36224. 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  36225. 0x6F, 0x6D, 0x82, 0x14, 0x08, 0xB0, 0x54, 0x7A, 0x03, 0x5A, 0xEC, 0x55,
  36226. 0x8A, 0x12, 0xE8, 0xF9, 0x8E, 0x34, 0xB6, 0x13, 0xD9, 0x59, 0xB8, 0xE8,
  36227. 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  36228. 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2B,
  36229. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0D, 0x06, 0x09, 0x2A,
  36230. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
  36231. 0x01, 0x01, 0x00, 0x14, 0xFB, 0xD0, 0xCE, 0x31, 0x7F, 0xA5, 0x59, 0xFA,
  36232. 0x7C, 0x68, 0x26, 0xA7, 0xE8, 0x0D, 0x9F, 0x50, 0x57, 0xFA, 0x1C, 0x7C,
  36233. 0x5E, 0x43, 0xA4, 0x97, 0x47, 0xB6, 0x41, 0xAC, 0x63, 0xD3, 0x61, 0x8C,
  36234. 0x1F, 0x42, 0xEF, 0x53, 0xD0, 0xBA, 0x31, 0x4D, 0x99, 0x74, 0xA4, 0x60,
  36235. 0xDC, 0xC6, 0x6F, 0xCC, 0x1E, 0x25, 0x98, 0xE1, 0xA4, 0xA0, 0x67, 0x69,
  36236. 0x97, 0xE3, 0x97, 0x7C, 0x83, 0x28, 0xF1, 0xF4, 0x7D, 0x03, 0xA8, 0x31,
  36237. 0x77, 0xCC, 0xD1, 0x37, 0xEF, 0x7B, 0x4A, 0x71, 0x2D, 0x11, 0x7E, 0x92,
  36238. 0xF5, 0x67, 0xB7, 0x56, 0xBA, 0x28, 0xF8, 0xD6, 0xCE, 0x2A, 0x71, 0xE3,
  36239. 0x70, 0x6B, 0x09, 0x0F, 0x67, 0x6F, 0x7A, 0xE0, 0x89, 0xF6, 0x5E, 0x23,
  36240. 0x0C, 0x0A, 0x44, 0x4E, 0x65, 0x8E, 0x7B, 0x68, 0xD0, 0xAD, 0x76, 0x3E,
  36241. 0x2A, 0x0E, 0xA2, 0x05, 0x11, 0x74, 0x24, 0x08, 0x60, 0xED, 0x9F, 0x98,
  36242. 0x18, 0xE9, 0x91, 0x58, 0x36, 0xEC, 0xEC, 0x25, 0x6B, 0xBA, 0x9C, 0x87,
  36243. 0x38, 0x68, 0xDC, 0xDC, 0x15, 0x6F, 0x20, 0x68, 0xC4, 0xBF, 0x05, 0x5B,
  36244. 0x4A, 0x0C, 0x44, 0x2B, 0x92, 0x3F, 0x10, 0x99, 0xDC, 0xF6, 0x6C, 0x0E,
  36245. 0x34, 0x26, 0x6E, 0x6D, 0x4E, 0x12, 0xBC, 0x60, 0x8F, 0x27, 0x1D, 0x7A,
  36246. 0x00, 0x50, 0xBE, 0x23, 0xDE, 0x48, 0x47, 0x9F, 0xAD, 0x2F, 0x94, 0x3D,
  36247. 0x16, 0x73, 0x48, 0x6B, 0xC8, 0x97, 0xE6, 0xB4, 0xB3, 0x4B, 0xE1, 0x68,
  36248. 0x08, 0xC3, 0xE5, 0x34, 0x5F, 0x9B, 0xDA, 0xAB, 0xCA, 0x6D, 0x55, 0x32,
  36249. 0xEF, 0x6C, 0xEF, 0x9B, 0x8B, 0x5B, 0xC7, 0xF0, 0xC2, 0x0F, 0x8E, 0x93,
  36250. 0x09, 0x60, 0x3C, 0x0B, 0xDC, 0xBD, 0xDB, 0x4A, 0x2D, 0xD0, 0x98, 0xAA,
  36251. 0xAB, 0x6C, 0x6F, 0x6D, 0x6B, 0x6A, 0x5C, 0x33, 0xAC, 0xAD, 0xA8, 0x1B,
  36252. 0x38, 0x5D, 0x9F, 0xDA, 0xE7, 0x70, 0x07
  36253. };
  36254. pt = ca_key_der_2048;
  36255. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  36256. sizeof_ca_key_der_2048));
  36257. pt = client_cert_der_2048;
  36258. ExpectNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  36259. sizeof_client_cert_der_2048));
  36260. pt = ca_cert_der_2048;
  36261. ExpectNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  36262. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  36263. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  36264. #ifndef NO_ASN_TIME
  36265. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  36266. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  36267. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  36268. ExpectIntEQ(notAfter->length, 13);
  36269. ExpectTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  36270. ExpectTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  36271. #endif
  36272. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  36273. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  36274. ExpectIntEQ(derSz, sizeof(expected));
  36275. #ifndef NO_ASN_TIME
  36276. ExpectIntEQ(XMEMCMP(der, expected, derSz), 0);
  36277. #endif
  36278. wolfSSL_X509_free(ca);
  36279. wolfSSL_X509_free(x509);
  36280. wolfSSL_EVP_PKEY_free(priv);
  36281. #ifndef NO_ASN_TIME
  36282. wolfSSL_ASN1_TIME_free(notBefore);
  36283. wolfSSL_ASN1_TIME_free(notAfter);
  36284. #endif
  36285. #endif
  36286. return EXPECT_RESULT();
  36287. }
  36288. static int test_wolfSSL_X509_sign(void)
  36289. {
  36290. EXPECT_DECLS;
  36291. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_ASN_TIME) && \
  36292. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  36293. int ret = 0;
  36294. char *cn = NULL;
  36295. word32 cnSz = 0;
  36296. X509_NAME *name = NULL;
  36297. X509 *x509 = NULL;
  36298. X509 *ca = NULL;
  36299. DecodedCert dCert;
  36300. EVP_PKEY *pub = NULL;
  36301. EVP_PKEY *priv = NULL;
  36302. EVP_MD_CTX *mctx = NULL;
  36303. #if defined(USE_CERT_BUFFERS_1024)
  36304. const unsigned char* rsaPriv = client_key_der_1024;
  36305. const unsigned char* rsaPub = client_keypub_der_1024;
  36306. const unsigned char* certIssuer = client_cert_der_1024;
  36307. long clientKeySz = (long)sizeof_client_key_der_1024;
  36308. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  36309. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  36310. #elif defined(USE_CERT_BUFFERS_2048)
  36311. const unsigned char* rsaPriv = client_key_der_2048;
  36312. const unsigned char* rsaPub = client_keypub_der_2048;
  36313. const unsigned char* certIssuer = client_cert_der_2048;
  36314. long clientKeySz = (long)sizeof_client_key_der_2048;
  36315. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  36316. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  36317. #endif
  36318. byte sn[16];
  36319. int snSz = sizeof(sn);
  36320. /* Set X509_NAME fields */
  36321. ExpectNotNull(name = X509_NAME_new());
  36322. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  36323. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  36324. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  36325. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  36326. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  36327. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  36328. /* Get private and public keys */
  36329. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  36330. clientKeySz));
  36331. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  36332. ExpectNotNull(x509 = X509_new());
  36333. /* Set version 3 */
  36334. ExpectIntNE(X509_set_version(x509, 2L), 0);
  36335. /* Set subject name, add pubkey, and sign certificate */
  36336. ExpectIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  36337. X509_NAME_free(name);
  36338. name = NULL;
  36339. ExpectIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  36340. #ifdef WOLFSSL_ALT_NAMES
  36341. /* Add some subject alt names */
  36342. ExpectIntNE(wolfSSL_X509_add_altname(NULL,
  36343. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  36344. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  36345. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  36346. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  36347. "sphygmomanometer",
  36348. ASN_DNS_TYPE), SSL_SUCCESS);
  36349. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  36350. "supercalifragilisticexpialidocious",
  36351. ASN_DNS_TYPE), SSL_SUCCESS);
  36352. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  36353. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  36354. ASN_DNS_TYPE), SSL_SUCCESS);
  36355. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  36356. {
  36357. unsigned char ip4_type[] = {127,128,0,255};
  36358. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  36359. 0xff, 0xee, 0x99, 0x88,
  36360. 0x77, 0x66, 0x55, 0x44,
  36361. 0x00, 0x33, 0x22, 0x11};
  36362. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  36363. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  36364. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  36365. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  36366. }
  36367. #endif
  36368. #endif /* WOLFSSL_ALT_NAMES */
  36369. /* test valid sign case */
  36370. ExpectIntGT(ret = X509_sign(x509, priv, EVP_sha256()), 0);
  36371. /* test valid X509_sign_ctx case */
  36372. ExpectNotNull(mctx = EVP_MD_CTX_new());
  36373. ExpectIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  36374. ExpectIntGT(X509_sign_ctx(x509, mctx), 0);
  36375. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  36376. ExpectIntEQ(X509_get_ext_count(x509), 1);
  36377. #endif
  36378. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  36379. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  36380. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  36381. #endif
  36382. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  36383. WOLFSSL_SUCCESS);
  36384. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  36385. /* Variation in size depends on ASN.1 encoding when MSB is set.
  36386. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  36387. * with the MSB set. See GenerateInteger in asn.c */
  36388. #ifndef USE_CERT_BUFFERS_1024
  36389. #ifndef WOLFSSL_ALT_NAMES
  36390. /* Valid case - size should be 781-786 with 16 byte serial number */
  36391. ExpectTrue((781 + snSz <= ret) && (ret <= 781 + 5 + snSz));
  36392. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  36393. /* Valid case - size should be 955-960 with 16 byte serial number */
  36394. ExpectTrue((939 + snSz <= ret) && (ret <= 939 + 5 + snSz));
  36395. #else
  36396. /* Valid case - size should be 926-931 with 16 byte serial number */
  36397. ExpectTrue((910 + snSz <= ret) && (ret <= 910 + 5 + snSz));
  36398. #endif
  36399. #else
  36400. #ifndef WOLFSSL_ALT_NAMES
  36401. /* Valid case - size should be 537-542 with 16 byte serial number */
  36402. ExpectTrue((521 + snSz <= ret) && (ret <= 521 + 5 + snSz));
  36403. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  36404. /* Valid case - size should be 695-670 with 16 byte serial number */
  36405. ExpectTrue((679 + snSz <= ret) && (ret <= 679 + 5 + snSz));
  36406. #else
  36407. /* Valid case - size should be 666-671 with 16 byte serial number */
  36408. ExpectTrue((650 + snSz <= ret) && (ret <= 650 + 5 + snSz));
  36409. #endif
  36410. #endif
  36411. /* check that issuer name is as expected after signature */
  36412. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  36413. ExpectIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  36414. ExpectNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  36415. ExpectNotNull(name = X509_get_subject_name(ca));
  36416. cnSz = X509_NAME_get_sz(name);
  36417. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  36418. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  36419. ExpectIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  36420. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  36421. cn = NULL;
  36422. #ifdef WOLFSSL_MULTI_ATTRIB
  36423. /* test adding multiple OU's to the signer */
  36424. ExpectNotNull(name = X509_get_subject_name(ca));
  36425. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  36426. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  36427. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  36428. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  36429. ExpectIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  36430. #endif
  36431. ExpectNotNull(name = X509_get_subject_name(ca));
  36432. ExpectIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  36433. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  36434. ExpectNotNull(name = X509_get_issuer_name(x509));
  36435. cnSz = X509_NAME_get_sz(name);
  36436. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  36437. ExpectNotNull(cn = X509_NAME_oneline(name, cn, cnSz));
  36438. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  36439. ExpectIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  36440. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  36441. cn = NULL;
  36442. FreeDecodedCert(&dCert);
  36443. /* Test invalid parameters */
  36444. ExpectIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  36445. ExpectIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  36446. ExpectIntEQ(X509_sign(x509, priv, NULL), 0);
  36447. ExpectIntEQ(X509_sign_ctx(NULL, mctx), 0);
  36448. EVP_MD_CTX_free(mctx);
  36449. mctx = NULL;
  36450. ExpectNotNull(mctx = EVP_MD_CTX_new());
  36451. ExpectIntEQ(X509_sign_ctx(x509, mctx), 0);
  36452. ExpectIntEQ(X509_sign_ctx(x509, NULL), 0);
  36453. /* test invalid version number */
  36454. #if defined(OPENSSL_ALL)
  36455. ExpectIntNE(X509_set_version(x509, 6L), 0);
  36456. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  36457. /* uses ParseCert which fails on bad version number */
  36458. ExpectIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  36459. #endif
  36460. EVP_MD_CTX_free(mctx);
  36461. EVP_PKEY_free(priv);
  36462. EVP_PKEY_free(pub);
  36463. X509_free(x509);
  36464. X509_free(ca);
  36465. #endif
  36466. return EXPECT_RESULT();
  36467. }
  36468. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  36469. {
  36470. EXPECT_DECLS;
  36471. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  36472. X509* x509 = NULL;
  36473. const X509_ALGOR* alg;
  36474. ExpectNotNull(x509 = X509_new());
  36475. ExpectNull(alg = X509_get0_tbs_sigalg(NULL));
  36476. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  36477. X509_free(x509);
  36478. #endif
  36479. return EXPECT_RESULT();
  36480. }
  36481. static int test_wolfSSL_X509_ALGOR_get0(void)
  36482. {
  36483. EXPECT_DECLS;
  36484. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  36485. !defined(NO_SHA256) && !defined(NO_RSA)
  36486. X509* x509 = NULL;
  36487. const ASN1_OBJECT* obj = NULL;
  36488. const X509_ALGOR* alg = NULL;
  36489. int pptype = 0;
  36490. const void *ppval = NULL;
  36491. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  36492. SSL_FILETYPE_PEM));
  36493. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  36494. /* Invalid case */
  36495. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  36496. ExpectNull(obj);
  36497. /* Valid case */
  36498. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  36499. ExpectNotNull(obj);
  36500. ExpectNull(ppval);
  36501. ExpectIntNE(pptype, 0);
  36502. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  36503. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  36504. X509_free(x509);
  36505. #endif
  36506. return EXPECT_RESULT();
  36507. }
  36508. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  36509. {
  36510. EXPECT_DECLS;
  36511. #if defined(OPENSSL_EXTRA)
  36512. X509_VERIFY_PARAM *paramTo = NULL;
  36513. X509_VERIFY_PARAM *paramFrom = NULL;
  36514. char testIPv4[] = "127.0.0.1";
  36515. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  36516. char testhostName1[] = "foo.hoge.com";
  36517. char testhostName2[] = "foobar.hoge.com";
  36518. ExpectNotNull(paramTo = X509_VERIFY_PARAM_new());
  36519. ExpectNotNull(XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM)));
  36520. ExpectNotNull(paramFrom = X509_VERIFY_PARAM_new());
  36521. ExpectNotNull(XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM)));
  36522. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  36523. (int)XSTRLEN(testhostName1)), 1);
  36524. ExpectIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  36525. (int)XSTRLEN(testhostName1)));
  36526. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  36527. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  36528. ExpectIntEQ(0x01, paramFrom->hostFlags);
  36529. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4), 0);
  36530. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4), 1);
  36531. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  36532. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL), 1);
  36533. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6), 1);
  36534. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36535. /* null pointer */
  36536. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, paramFrom), 0);
  36537. /* in the case of "from" null, returns success */
  36538. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, NULL), 1);
  36539. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, NULL), 0);
  36540. /* inherit flags test : VPARAM_DEFAULT */
  36541. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  36542. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  36543. (int)XSTRLEN(testhostName1)));
  36544. ExpectIntEQ(0x01, paramTo->hostFlags);
  36545. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36546. /* inherit flags test : VPARAM OVERWRITE */
  36547. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  36548. (int)XSTRLEN(testhostName2)), 1);
  36549. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  36550. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  36551. if (paramTo != NULL) {
  36552. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  36553. }
  36554. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  36555. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  36556. (int)XSTRLEN(testhostName1)));
  36557. ExpectIntEQ(0x01, paramTo->hostFlags);
  36558. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36559. /* inherit flags test : VPARAM_RESET_FLAGS */
  36560. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  36561. (int)XSTRLEN(testhostName2)), 1);
  36562. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  36563. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  36564. if (paramTo != NULL) {
  36565. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  36566. }
  36567. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  36568. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  36569. (int)XSTRLEN(testhostName1)));
  36570. ExpectIntEQ(0x01, paramTo->hostFlags);
  36571. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36572. /* inherit flags test : VPARAM_LOCKED */
  36573. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  36574. (int)XSTRLEN(testhostName2)), 1);
  36575. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  36576. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  36577. if (paramTo != NULL) {
  36578. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  36579. }
  36580. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  36581. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  36582. (int)XSTRLEN(testhostName2)));
  36583. ExpectIntEQ(0x00, paramTo->hostFlags);
  36584. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  36585. /* test for incorrect parameters */
  36586. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL),
  36587. 0);
  36588. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, 0), 0);
  36589. /* inherit flags test : VPARAM_ONCE, not testable yet */
  36590. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL),
  36591. 1);
  36592. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo),
  36593. X509_V_FLAG_CRL_CHECK_ALL);
  36594. ExpectIntEQ(X509_VERIFY_PARAM_clear_flags(paramTo,
  36595. X509_V_FLAG_CRL_CHECK_ALL), 1);
  36596. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo), 0);
  36597. X509_VERIFY_PARAM_free(paramTo);
  36598. X509_VERIFY_PARAM_free(paramFrom);
  36599. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  36600. #endif
  36601. return EXPECT_RESULT();
  36602. }
  36603. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  36604. static int test_wolfSSL_check_domain_verify_count = 0;
  36605. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  36606. WOLFSSL_X509_STORE_CTX* store)
  36607. {
  36608. EXPECT_DECLS;
  36609. ExpectIntEQ(X509_STORE_CTX_get_error(store), 0);
  36610. ExpectIntEQ(preverify, 1);
  36611. ExpectIntGT(++test_wolfSSL_check_domain_verify_count, 0);
  36612. return EXPECT_SUCCESS();
  36613. }
  36614. static int test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  36615. {
  36616. EXPECT_DECLS;
  36617. X509_VERIFY_PARAM *param = NULL;
  36618. ExpectNotNull(param = SSL_get0_param(ssl));
  36619. /* Domain check should only be done on the leaf cert */
  36620. X509_VERIFY_PARAM_set_hostflags(param,
  36621. X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  36622. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(param,
  36623. "wolfSSL Server Chain", 0), 1);
  36624. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  36625. test_wolfSSL_check_domain_verify_cb);
  36626. return EXPECT_RESULT();
  36627. }
  36628. static int test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  36629. {
  36630. EXPECT_DECLS;
  36631. /* Use a cert with different domains in chain */
  36632. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  36633. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  36634. return EXPECT_RESULT();
  36635. }
  36636. static int test_wolfSSL_check_domain(void)
  36637. {
  36638. EXPECT_DECLS;
  36639. test_ssl_cbf func_cb_client;
  36640. test_ssl_cbf func_cb_server;
  36641. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  36642. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  36643. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  36644. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  36645. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  36646. &func_cb_server, NULL), TEST_SUCCESS);
  36647. /* Should have been called once for each cert in sent chain */
  36648. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  36649. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  36650. #else
  36651. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  36652. #endif
  36653. return EXPECT_RESULT();
  36654. }
  36655. #endif /* OPENSSL_EXTRA && HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  36656. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  36657. {
  36658. EXPECT_DECLS;
  36659. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  36660. X509* x509 = NULL;
  36661. X509_PUBKEY* pubKey;
  36662. ExpectNotNull(x509 = X509_new());
  36663. ExpectNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  36664. ExpectNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  36665. X509_free(x509);
  36666. #endif
  36667. return EXPECT_RESULT();
  36668. }
  36669. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  36670. {
  36671. EXPECT_DECLS;
  36672. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  36673. !defined(NO_SHA256) && !defined(NO_RSA)
  36674. X509* x509 = NULL;
  36675. ASN1_OBJECT* obj = NULL;
  36676. const ASN1_OBJECT* pa_oid = NULL;
  36677. X509_PUBKEY* pubKey = NULL;
  36678. X509_PUBKEY* pubKey2 = NULL;
  36679. EVP_PKEY* evpKey = NULL;
  36680. const unsigned char *pk = NULL;
  36681. int ppklen;
  36682. int pptype;
  36683. X509_ALGOR *pa = NULL;
  36684. const void *pval;
  36685. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  36686. SSL_FILETYPE_PEM));
  36687. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  36688. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  36689. ExpectNotNull(pk);
  36690. ExpectNotNull(pa);
  36691. ExpectNotNull(pubKey);
  36692. ExpectIntGT(ppklen, 0);
  36693. ExpectIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  36694. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  36695. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  36696. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  36697. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  36698. ExpectNotNull(pk);
  36699. ExpectNotNull(pa);
  36700. ExpectIntGT(ppklen, 0);
  36701. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  36702. ExpectNotNull(pa_oid);
  36703. ExpectNull(pval);
  36704. ExpectIntEQ(pptype, V_ASN1_NULL);
  36705. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  36706. X509_PUBKEY_free(pubKey2);
  36707. X509_free(x509);
  36708. EVP_PKEY_free(evpKey);
  36709. #endif
  36710. return EXPECT_RESULT();
  36711. }
  36712. static int test_wolfSSL_X509_PUBKEY_EC(void)
  36713. {
  36714. EXPECT_DECLS;
  36715. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  36716. X509* x509 = NULL;
  36717. ASN1_OBJECT* obj = NULL;
  36718. ASN1_OBJECT* poid = NULL;
  36719. const ASN1_OBJECT* pa_oid = NULL;
  36720. X509_PUBKEY* pubKey = NULL;
  36721. X509_PUBKEY* pubKey2 = NULL;
  36722. EVP_PKEY* evpKey = NULL;
  36723. const unsigned char *pk = NULL;
  36724. int ppklen;
  36725. int pptype;
  36726. X509_ALGOR *pa = NULL;
  36727. const void *pval;
  36728. char buf[50];
  36729. ExpectNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  36730. SSL_FILETYPE_PEM));
  36731. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  36732. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  36733. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  36734. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  36735. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  36736. ExpectNotNull(pk);
  36737. ExpectNotNull(pa);
  36738. ExpectIntGT(ppklen, 0);
  36739. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  36740. ExpectNotNull(pa_oid);
  36741. ExpectNotNull(pval);
  36742. ExpectIntEQ(pptype, V_ASN1_OBJECT);
  36743. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  36744. poid = (ASN1_OBJECT *)pval;
  36745. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  36746. ExpectIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  36747. X509_PUBKEY_free(pubKey2);
  36748. X509_free(x509);
  36749. EVP_PKEY_free(evpKey);
  36750. #endif
  36751. return EXPECT_RESULT();
  36752. }
  36753. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  36754. {
  36755. EXPECT_DECLS;
  36756. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  36757. word32 bytes;
  36758. #ifdef USE_CERT_BUFFERS_1024
  36759. byte tmp[ONEK_BUF];
  36760. #elif defined(USE_CERT_BUFFERS_2048)
  36761. byte tmp[TWOK_BUF];
  36762. #else
  36763. byte tmp[TWOK_BUF];
  36764. #endif /* END USE_CERT_BUFFERS_1024 */
  36765. const unsigned char* dsaKeyDer = tmp;
  36766. ASN1_OBJECT* obj = NULL;
  36767. ASN1_STRING* str;
  36768. const ASN1_OBJECT* pa_oid = NULL;
  36769. X509_PUBKEY* pubKey = NULL;
  36770. EVP_PKEY* evpKey = NULL;
  36771. const unsigned char *pk = NULL;
  36772. int ppklen, pptype;
  36773. X509_ALGOR *pa = NULL;
  36774. const void *pval;
  36775. #ifdef USE_CERT_BUFFERS_1024
  36776. XMEMSET(tmp, 0, sizeof(tmp));
  36777. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  36778. bytes = sizeof_dsa_key_der_1024;
  36779. #elif defined(USE_CERT_BUFFERS_2048)
  36780. XMEMSET(tmp, 0, sizeof(tmp));
  36781. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  36782. bytes = sizeof_dsa_key_der_2048;
  36783. #else
  36784. {
  36785. XFILE fp = XBADFILE;
  36786. XMEMSET(tmp, 0, sizeof(tmp));
  36787. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  36788. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  36789. if (fp != XBADFILE)
  36790. XFCLOSE(fp);
  36791. }
  36792. #endif
  36793. /* Initialize pkey with der format dsa key */
  36794. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  36795. ExpectNotNull(pubKey = X509_PUBKEY_new());
  36796. ExpectIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  36797. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  36798. ExpectNotNull(pk);
  36799. ExpectNotNull(pa);
  36800. ExpectIntGT(ppklen, 0);
  36801. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  36802. ExpectNotNull(pa_oid);
  36803. ExpectNotNull(pval);
  36804. ExpectIntEQ(pptype, V_ASN1_SEQUENCE);
  36805. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  36806. str = (ASN1_STRING *)pval;
  36807. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  36808. #ifdef USE_CERT_BUFFERS_1024
  36809. ExpectIntEQ(ASN1_STRING_length(str), 291);
  36810. #else
  36811. ExpectIntEQ(ASN1_STRING_length(str), 549);
  36812. #endif /* END USE_CERT_BUFFERS_1024 */
  36813. X509_PUBKEY_free(pubKey);
  36814. EVP_PKEY_free(evpKey);
  36815. #endif
  36816. return EXPECT_RESULT();
  36817. }
  36818. static int test_wolfSSL_BUF(void)
  36819. {
  36820. EXPECT_DECLS;
  36821. #if defined(OPENSSL_EXTRA)
  36822. BUF_MEM* buf = NULL;
  36823. ExpectNotNull(buf = BUF_MEM_new());
  36824. ExpectIntEQ(BUF_MEM_grow(buf, 10), 10);
  36825. ExpectIntEQ(BUF_MEM_grow(buf, -1), 0);
  36826. BUF_MEM_free(buf);
  36827. #endif
  36828. return EXPECT_RESULT();
  36829. }
  36830. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  36831. static int stub_rand_seed(const void *buf, int num)
  36832. {
  36833. (void)buf;
  36834. (void)num;
  36835. return 123;
  36836. }
  36837. static int stub_rand_bytes(unsigned char *buf, int num)
  36838. {
  36839. (void)buf;
  36840. (void)num;
  36841. return 456;
  36842. }
  36843. static byte* was_stub_rand_cleanup_called(void)
  36844. {
  36845. static byte was_called = 0;
  36846. return &was_called;
  36847. }
  36848. static void stub_rand_cleanup(void)
  36849. {
  36850. byte* was_called = was_stub_rand_cleanup_called();
  36851. *was_called = 1;
  36852. return;
  36853. }
  36854. static byte* was_stub_rand_add_called(void)
  36855. {
  36856. static byte was_called = 0;
  36857. return &was_called;
  36858. }
  36859. static int stub_rand_add(const void *buf, int num, double entropy)
  36860. {
  36861. byte* was_called = was_stub_rand_add_called();
  36862. (void)buf;
  36863. (void)num;
  36864. (void)entropy;
  36865. *was_called = 1;
  36866. return 0;
  36867. }
  36868. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  36869. {
  36870. (void)buf;
  36871. (void)num;
  36872. return 9876;
  36873. }
  36874. static int stub_rand_status(void)
  36875. {
  36876. return 5432;
  36877. }
  36878. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  36879. static int test_wolfSSL_RAND_set_rand_method(void)
  36880. {
  36881. EXPECT_DECLS;
  36882. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  36883. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  36884. unsigned char* buf = NULL;
  36885. int num = 0;
  36886. double entropy = 0;
  36887. int ret;
  36888. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  36889. byte* was_add_called = was_stub_rand_add_called();
  36890. ExpectNotNull(buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  36891. DYNAMIC_TYPE_TMP_BUFFER));
  36892. ExpectIntNE(wolfSSL_RAND_status(), 5432);
  36893. ExpectIntEQ(*was_cleanup_called, 0);
  36894. RAND_cleanup();
  36895. ExpectIntEQ(*was_cleanup_called, 0);
  36896. rand_methods.seed = &stub_rand_seed;
  36897. rand_methods.bytes = &stub_rand_bytes;
  36898. rand_methods.cleanup = &stub_rand_cleanup;
  36899. rand_methods.add = &stub_rand_add;
  36900. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  36901. rand_methods.status = &stub_rand_status;
  36902. ExpectIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  36903. ExpectIntEQ(RAND_seed(buf, num), 123);
  36904. ExpectIntEQ(RAND_bytes(buf, num), 456);
  36905. ExpectIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  36906. ExpectIntEQ(RAND_status(), 5432);
  36907. ExpectIntEQ(*was_add_called, 0);
  36908. /* The function pointer for RAND_add returns int, but RAND_add itself
  36909. * returns void. */
  36910. RAND_add(buf, num, entropy);
  36911. ExpectIntEQ(*was_add_called, 1);
  36912. was_add_called = 0;
  36913. ExpectIntEQ(*was_cleanup_called, 0);
  36914. RAND_cleanup();
  36915. ExpectIntEQ(*was_cleanup_called, 1);
  36916. *was_cleanup_called = 0;
  36917. ret = RAND_set_rand_method(NULL);
  36918. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  36919. ExpectIntNE(RAND_status(), 5432);
  36920. ExpectIntEQ(*was_cleanup_called, 0);
  36921. RAND_cleanup();
  36922. ExpectIntEQ(*was_cleanup_called, 0);
  36923. RAND_set_rand_method(NULL);
  36924. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36925. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  36926. return EXPECT_RESULT();
  36927. }
  36928. static int test_wolfSSL_RAND_bytes(void)
  36929. {
  36930. EXPECT_DECLS;
  36931. #if defined(OPENSSL_EXTRA)
  36932. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  36933. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  36934. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  36935. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  36936. int max_bufsize;
  36937. byte *my_buf = NULL;
  36938. /* sanity check */
  36939. ExpectIntEQ(RAND_bytes(NULL, 16), 0);
  36940. ExpectIntEQ(RAND_bytes(NULL, 0), 0);
  36941. max_bufsize = size4;
  36942. ExpectNotNull(my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  36943. DYNAMIC_TYPE_TMP_BUFFER));
  36944. ExpectIntEQ(RAND_bytes(my_buf, 0), 1);
  36945. ExpectIntEQ(RAND_bytes(my_buf, -1), 0);
  36946. ExpectNotNull(XMEMSET(my_buf, 0, max_bufsize));
  36947. ExpectIntEQ(RAND_bytes(my_buf, size1), 1);
  36948. ExpectIntEQ(RAND_bytes(my_buf, size2), 1);
  36949. ExpectIntEQ(RAND_bytes(my_buf, size3), 1);
  36950. ExpectIntEQ(RAND_bytes(my_buf, size4), 1);
  36951. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  36952. #endif
  36953. return EXPECT_RESULT();
  36954. }
  36955. static int test_wolfSSL_RAND(void)
  36956. {
  36957. EXPECT_DECLS;
  36958. #if defined(OPENSSL_EXTRA)
  36959. byte seed[16];
  36960. XMEMSET(seed, 0, sizeof(seed));
  36961. /* No global methods set. */
  36962. ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
  36963. ExpectIntEQ(RAND_poll(), 1);
  36964. RAND_cleanup();
  36965. ExpectIntEQ(RAND_egd(NULL), -1);
  36966. #ifndef NO_FILESYSTEM
  36967. {
  36968. char fname[100];
  36969. ExpectNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  36970. ExpectIntEQ(RAND_write_file(NULL), 0);
  36971. }
  36972. #endif
  36973. #endif
  36974. return EXPECT_RESULT();
  36975. }
  36976. static int test_wolfSSL_PKCS8_Compat(void)
  36977. {
  36978. EXPECT_DECLS;
  36979. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC) && \
  36980. !defined(NO_BIO)
  36981. PKCS8_PRIV_KEY_INFO* pt = NULL;
  36982. BIO* bio = NULL;
  36983. XFILE f = XBADFILE;
  36984. int bytes;
  36985. char pkcs8_buffer[512];
  36986. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  36987. EVP_PKEY *pkey = NULL;
  36988. #endif
  36989. /* file from wolfssl/certs/ directory */
  36990. ExpectTrue((f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb")) != XBADFILE);
  36991. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)),
  36992. 0);
  36993. if (f != XBADFILE)
  36994. XFCLOSE(f);
  36995. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  36996. ExpectNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  36997. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  36998. ExpectNotNull(pkey = EVP_PKCS82PKEY(pt));
  36999. ExpectIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  37000. /* gets PKCS8 pointer to pkey */
  37001. ExpectNotNull(EVP_PKEY2PKCS8(pkey));
  37002. EVP_PKEY_free(pkey);
  37003. #endif
  37004. BIO_free(bio);
  37005. PKCS8_PRIV_KEY_INFO_free(pt);
  37006. #endif
  37007. return EXPECT_RESULT();
  37008. }
  37009. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  37010. static int NoPasswordCallBack(char* passwd, int sz, int rw, void* userdata)
  37011. {
  37012. (void)passwd;
  37013. (void)sz;
  37014. (void)rw;
  37015. (void)userdata;
  37016. return -1;
  37017. }
  37018. #endif
  37019. static int test_wolfSSL_PKCS8_d2i(void)
  37020. {
  37021. EXPECT_DECLS;
  37022. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  37023. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  37024. * requires a 12 byte password in FIPS mode. This test ends up
  37025. * trying to use an 8 byte password. */
  37026. #ifndef NO_FILESYSTEM
  37027. unsigned char pkcs8_buffer[2048];
  37028. const unsigned char* p = NULL;
  37029. int bytes = 0;
  37030. XFILE file = XBADFILE;
  37031. WOLFSSL_EVP_PKEY* pkey = NULL;
  37032. #ifndef NO_BIO
  37033. BIO* bio = NULL;
  37034. #if defined(OPENSSL_ALL) && \
  37035. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  37036. defined(HAVE_ECC)) && \
  37037. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37038. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  37039. #endif
  37040. #endif
  37041. #ifndef NO_RSA
  37042. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  37043. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  37044. #ifndef NO_DES3
  37045. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  37046. #endif
  37047. #endif /* NO_RSA */
  37048. #ifdef HAVE_ECC
  37049. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  37050. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  37051. #ifndef NO_DES3
  37052. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  37053. #endif
  37054. #endif /* HAVE_ECC */
  37055. #endif /* !NO_FILESYSTEM */
  37056. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  37057. #ifndef NO_RSA
  37058. #ifdef USE_CERT_BUFFERS_1024
  37059. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  37060. int rsaSz = sizeof_server_key_der_1024;
  37061. #else
  37062. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  37063. int rsaSz = sizeof_server_key_der_2048;
  37064. #endif
  37065. #endif
  37066. #ifdef HAVE_ECC
  37067. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  37068. int ecSz = sizeof_ecc_key_der_256;
  37069. #endif
  37070. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  37071. #ifndef NO_FILESYSTEM
  37072. (void)pkcs8_buffer;
  37073. (void)p;
  37074. (void)bytes;
  37075. (void)file;
  37076. #ifndef NO_BIO
  37077. (void)bio;
  37078. #endif
  37079. #endif
  37080. #ifdef OPENSSL_ALL
  37081. #ifndef NO_RSA
  37082. /* Try to auto-detect normal RSA private key */
  37083. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  37084. EVP_PKEY_free(pkey);
  37085. pkey = NULL;
  37086. #endif
  37087. #ifdef HAVE_ECC
  37088. /* Try to auto-detect normal EC private key */
  37089. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  37090. EVP_PKEY_free(pkey);
  37091. pkey = NULL;
  37092. #endif
  37093. #endif /* OPENSSL_ALL */
  37094. #ifndef NO_FILESYSTEM
  37095. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37096. ExpectIntEQ(PEM_write_PKCS8PrivateKey(XBADFILE, pkey, NULL, NULL, 0, NULL,
  37097. NULL), 0);
  37098. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  37099. NULL), 0);
  37100. #endif
  37101. #ifndef NO_RSA
  37102. /* Get DER encoded RSA PKCS#8 data. */
  37103. ExpectTrue((file = XFOPEN(rsaDerPkcs8File, "rb")) != XBADFILE);
  37104. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37105. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37106. file)), 0);
  37107. if (file != XBADFILE) {
  37108. XFCLOSE(file);
  37109. file = XBADFILE;
  37110. }
  37111. p = pkcs8_buffer;
  37112. #ifdef OPENSSL_ALL
  37113. /* Try to decode - auto-detect key type. */
  37114. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  37115. #else
  37116. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  37117. #endif
  37118. /* Get PEM encoded RSA PKCS#8 data. */
  37119. ExpectTrue((file = XFOPEN(rsaPemPkcs8File, "rb")) != XBADFILE);
  37120. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37121. file)), 0);
  37122. if (file != XBADFILE) {
  37123. XFCLOSE(file);
  37124. file = XBADFILE;
  37125. }
  37126. #if defined(OPENSSL_ALL) && \
  37127. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37128. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37129. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
  37130. NULL), 0);
  37131. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  37132. NULL), 0);
  37133. /* Write PKCS#8 PEM to BIO. */
  37134. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  37135. NULL), bytes);
  37136. /* Write PKCS#8 PEM to stderr. */
  37137. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, NULL, NULL, 0, NULL,
  37138. NULL), bytes);
  37139. /* Compare file and written data */
  37140. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  37141. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  37142. BIO_free(bio);
  37143. bio = NULL;
  37144. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  37145. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_gcm(),
  37146. NULL, 0, PasswordCallBack, (void*)"yassl123"), 0);
  37147. #endif
  37148. #if !defined(NO_DES3) && !defined(NO_SHA)
  37149. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37150. /* Write Encrypted PKCS#8 PEM to BIO. */
  37151. bytes = 1834;
  37152. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  37153. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37154. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_des_ede3_cbc(),
  37155. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37156. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  37157. (void*)"yassl123"));
  37158. EVP_PKEY_free(evpPkey);
  37159. evpPkey = NULL;
  37160. BIO_free(bio);
  37161. bio = NULL;
  37162. #endif /* !NO_DES3 && !NO_SHA */
  37163. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37164. EVP_PKEY_free(pkey);
  37165. pkey = NULL;
  37166. /* PKCS#8 encrypted RSA key */
  37167. #ifndef NO_DES3
  37168. ExpectTrue((file = XFOPEN(rsaDerPkcs8EncFile, "rb")) != XBADFILE);
  37169. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37170. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37171. file)), 0);
  37172. if (file != XBADFILE) {
  37173. XFCLOSE(file);
  37174. file = XBADFILE;
  37175. }
  37176. #if defined(OPENSSL_ALL) && \
  37177. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37178. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  37179. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  37180. (void*)"yassl123"));
  37181. EVP_PKEY_free(pkey);
  37182. pkey = NULL;
  37183. BIO_free(bio);
  37184. bio = NULL;
  37185. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37186. #endif /* !NO_DES3 */
  37187. #endif /* NO_RSA */
  37188. #ifdef HAVE_ECC
  37189. /* PKCS#8 encode EC key */
  37190. ExpectTrue((file = XFOPEN(ecDerPkcs8File, "rb")) != XBADFILE);
  37191. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37192. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37193. file)), 0);
  37194. if (file != XBADFILE) {
  37195. XFCLOSE(file);
  37196. file = XBADFILE;
  37197. }
  37198. p = pkcs8_buffer;
  37199. #ifdef OPENSSL_ALL
  37200. /* Try to decode - auto-detect key type. */
  37201. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  37202. #else
  37203. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  37204. #endif
  37205. /* Get PEM encoded RSA PKCS#8 data. */
  37206. ExpectTrue((file = XFOPEN(ecPemPkcs8File, "rb")) != XBADFILE);
  37207. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37208. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37209. file)), 0);
  37210. if (file != XBADFILE) {
  37211. XFCLOSE(file);
  37212. file = XBADFILE;
  37213. }
  37214. #if defined(OPENSSL_ALL) && \
  37215. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  37216. defined(HAVE_AES_CBC)
  37217. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37218. /* Write PKCS#8 PEM to BIO. */
  37219. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  37220. NULL), bytes);
  37221. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, NULL, NULL, 0, NULL,
  37222. NULL), bytes);
  37223. /* Compare file and written data */
  37224. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  37225. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  37226. BIO_free(bio);
  37227. bio = NULL;
  37228. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37229. /* Write Encrypted PKCS#8 PEM to BIO. */
  37230. bytes = 379;
  37231. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  37232. NULL, 0, NoPasswordCallBack, (void*)"yassl123"), 0);
  37233. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  37234. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37235. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_cbc(),
  37236. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37237. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_cbc(),
  37238. (char*)"yassl123", 8, PasswordCallBack, NULL), bytes);
  37239. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_256_cbc(),
  37240. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37241. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  37242. (void*)"yassl123"));
  37243. EVP_PKEY_free(evpPkey);
  37244. evpPkey = NULL;
  37245. BIO_free(bio);
  37246. bio = NULL;
  37247. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  37248. EVP_PKEY_free(pkey);
  37249. pkey = NULL;
  37250. /* PKCS#8 encrypted EC key */
  37251. #ifndef NO_DES3
  37252. ExpectTrue((file = XFOPEN(ecDerPkcs8EncFile, "rb")) != XBADFILE);
  37253. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37254. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37255. file)), 0);
  37256. if (file != XBADFILE) {
  37257. XFCLOSE(file);
  37258. file = XBADFILE;
  37259. }
  37260. #if defined(OPENSSL_ALL) && \
  37261. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37262. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  37263. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  37264. (void*)"yassl123"));
  37265. EVP_PKEY_free(pkey);
  37266. pkey = NULL;
  37267. BIO_free(bio);
  37268. bio = NULL;
  37269. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37270. #endif /* !NO_DES3 */
  37271. #endif /* HAVE_ECC */
  37272. #endif /* !NO_FILESYSTEM */
  37273. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  37274. return EXPECT_RESULT();
  37275. }
  37276. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  37277. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  37278. #define LOGGING_THREADS 5
  37279. #define ERROR_COUNT 10
  37280. /* copied from logging.c since this is not exposed otherwise */
  37281. #ifndef ERROR_QUEUE_MAX
  37282. #ifdef ERROR_QUEUE_PER_THREAD
  37283. #define ERROR_QUEUE_MAX 16
  37284. #else
  37285. /* this breaks from compat of unlimited error queue size */
  37286. #define ERROR_QUEUE_MAX 100
  37287. #endif
  37288. #endif
  37289. static volatile int loggingThreadsReady;
  37290. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  37291. {
  37292. const char* file;
  37293. int line;
  37294. unsigned long err;
  37295. int errorCount = 0;
  37296. int i;
  37297. (void)args;
  37298. while (!loggingThreadsReady);
  37299. for (i = 0; i < ERROR_COUNT; i++)
  37300. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  37301. while ((err = ERR_get_error_line(&file, &line))) {
  37302. AssertIntEQ(err, 990 + errorCount);
  37303. errorCount++;
  37304. }
  37305. AssertIntEQ(errorCount, ERROR_COUNT);
  37306. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  37307. ERR_clear_error(); /* ERR_get_error_line() does not remove */
  37308. errorCount = 0;
  37309. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  37310. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  37311. while ((err = ERR_get_error_line(&file, &line))) {
  37312. AssertIntEQ(err, 990 + errorCount);
  37313. errorCount++;
  37314. }
  37315. /* test that the 3 errors over the max were dropped */
  37316. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  37317. WOLFSSL_RETURN_FROM_THREAD(0);
  37318. }
  37319. #endif
  37320. static int test_error_queue_per_thread(void)
  37321. {
  37322. int res = TEST_SKIPPED;
  37323. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  37324. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  37325. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  37326. int i;
  37327. ERR_clear_error(); /* clear out any error nodes */
  37328. loggingThreadsReady = 0;
  37329. for (i = 0; i < LOGGING_THREADS; i++)
  37330. start_thread(test_logging, NULL, &loggingThreads[i]);
  37331. loggingThreadsReady = 1;
  37332. for (i = 0; i < LOGGING_THREADS; i++)
  37333. join_thread(loggingThreads[i]);
  37334. res = TEST_SUCCESS;
  37335. #endif
  37336. return res;
  37337. }
  37338. static int test_wolfSSL_ERR_put_error(void)
  37339. {
  37340. EXPECT_DECLS;
  37341. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37342. defined(DEBUG_WOLFSSL)
  37343. const char* file;
  37344. int line;
  37345. ERR_clear_error(); /* clear out any error nodes */
  37346. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  37347. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  37348. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  37349. ExpectIntEQ(ERR_get_error_line(&file, &line), 1);
  37350. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  37351. ExpectIntEQ(ERR_get_error_line(&file, &line), 2);
  37352. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  37353. ExpectIntEQ(ERR_get_error_line(&file, &line), 3);
  37354. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  37355. ExpectIntEQ(ERR_get_error_line(&file, &line), 4);
  37356. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  37357. ExpectIntEQ(ERR_get_error_line(&file, &line), 5);
  37358. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  37359. ExpectIntEQ(ERR_get_error_line(&file, &line), 6);
  37360. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  37361. ExpectIntEQ(ERR_get_error_line(&file, &line), 7);
  37362. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  37363. ExpectIntEQ(ERR_get_error_line(&file, &line), 8);
  37364. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  37365. ExpectIntEQ(ERR_get_error_line(&file, &line), 9);
  37366. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  37367. ExpectIntEQ(ERR_get_error_line(&file, &line), 10);
  37368. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  37369. ExpectIntEQ(ERR_get_error_line(&file, &line), 11);
  37370. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  37371. ExpectIntEQ(ERR_get_error_line(&file, &line), 12);
  37372. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  37373. ExpectIntEQ(ERR_get_error_line(&file, &line), 13);
  37374. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  37375. ExpectIntEQ(ERR_get_error_line(&file, &line), 14);
  37376. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  37377. ExpectIntEQ(ERR_get_error_line(&file, &line), 15);
  37378. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  37379. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  37380. "this file", 100);
  37381. ExpectIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  37382. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  37383. ExpectIntEQ(line, 100);
  37384. ExpectIntEQ(wolfSSL_ERR_peek_error(),
  37385. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  37386. ExpectIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  37387. #endif
  37388. /* try reading past end of error queue */
  37389. file = NULL;
  37390. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  37391. ExpectNull(file);
  37392. ExpectIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  37393. PEMerr(4,4);
  37394. ExpectIntEQ(ERR_get_error(), 4);
  37395. /* Empty and free up all error nodes */
  37396. ERR_clear_error();
  37397. /* Verify all nodes are cleared */
  37398. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  37399. ERR_clear_error();
  37400. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  37401. #endif
  37402. return EXPECT_RESULT();
  37403. }
  37404. /*
  37405. * This is a regression test for a bug where the peek/get error functions were
  37406. * drawing from the end of the queue rather than the front.
  37407. */
  37408. static int test_wolfSSL_ERR_get_error_order(void)
  37409. {
  37410. EXPECT_DECLS;
  37411. #if defined(WOLFSSL_HAVE_ERROR_QUEUE) && defined(OPENSSL_EXTRA)
  37412. /* Empty the queue. */
  37413. wolfSSL_ERR_clear_error();
  37414. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  37415. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  37416. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  37417. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  37418. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  37419. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  37420. #endif /* WOLFSSL_HAVE_ERROR_QUEUE && OPENSSL_EXTRA */
  37421. return EXPECT_RESULT();
  37422. }
  37423. #ifndef NO_BIO
  37424. static int test_wolfSSL_ERR_print_errors(void)
  37425. {
  37426. EXPECT_DECLS;
  37427. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37428. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  37429. BIO* bio = NULL;
  37430. char buf[1024];
  37431. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37432. ERR_clear_error(); /* clear out any error nodes */
  37433. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  37434. /* Choosing -600 as an unused errno. */
  37435. ERR_put_error(0,SYS_F_BIND, -600, "asn.c", 100);
  37436. ERR_print_errors(bio);
  37437. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  37438. ExpectIntEQ(XSTRNCMP(
  37439. "error:173:wolfSSL library:Bad function argument:ssl.c:0",
  37440. buf, 55), 0);
  37441. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  37442. ExpectIntEQ(XSTRNCMP(
  37443. "error:600:wolfSSL library:unknown error number:asn.c:100",
  37444. buf, 56), 0);
  37445. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  37446. ExpectIntEQ(buf[0], '\0');
  37447. ExpectIntEQ(ERR_get_error_line(NULL, NULL), 0);
  37448. BIO_free(bio);
  37449. #endif
  37450. return EXPECT_RESULT();
  37451. }
  37452. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37453. defined(DEBUG_WOLFSSL)
  37454. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  37455. {
  37456. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  37457. return 0;
  37458. }
  37459. #endif
  37460. static int test_wolfSSL_ERR_print_errors_cb(void)
  37461. {
  37462. EXPECT_DECLS;
  37463. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37464. defined(DEBUG_WOLFSSL)
  37465. BIO* bio = NULL;
  37466. char buf[1024];
  37467. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37468. ERR_clear_error(); /* clear out any error nodes */
  37469. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  37470. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  37471. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  37472. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  37473. ExpectIntEQ(XSTRNCMP(
  37474. "wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  37475. buf, 53), 0);
  37476. ExpectIntEQ(XSTRNCMP(
  37477. "wolfSSL error occurred, error = 275 line:100 file:asn.c",
  37478. buf + 53, 55), 0);
  37479. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  37480. BIO_free(bio);
  37481. #endif
  37482. return EXPECT_RESULT();
  37483. }
  37484. /*
  37485. * Testing WOLFSSL_ERROR_MSG
  37486. */
  37487. static int test_WOLFSSL_ERROR_MSG(void)
  37488. {
  37489. int res = TEST_SKIPPED;
  37490. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  37491. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  37492. const char* msg = TEST_STRING;
  37493. WOLFSSL_ERROR_MSG(msg);
  37494. res = TEST_SUCCESS;
  37495. #endif
  37496. return res;
  37497. } /* End test_WOLFSSL_ERROR_MSG */
  37498. /*
  37499. * Testing wc_ERR_remove_state
  37500. */
  37501. static int test_wc_ERR_remove_state(void)
  37502. {
  37503. int res = TEST_SKIPPED;
  37504. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  37505. wc_ERR_remove_state();
  37506. res = TEST_SUCCESS;
  37507. #endif
  37508. return res;
  37509. } /* End test_wc_ERR_remove_state */
  37510. /*
  37511. * Testing wc_ERR_print_errors_fp
  37512. */
  37513. static int test_wc_ERR_print_errors_fp(void)
  37514. {
  37515. EXPECT_DECLS;
  37516. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  37517. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  37518. long sz;
  37519. XFILE fp = XBADFILE;
  37520. WOLFSSL_ERROR(BAD_FUNC_ARG);
  37521. ExpectTrue((fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar")) !=
  37522. XBADFILE);
  37523. wc_ERR_print_errors_fp(fp);
  37524. #if defined(DEBUG_WOLFSSL)
  37525. ExpectTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  37526. #ifdef NO_ERROR_QUEUE
  37527. ExpectIntEQ(sz = XFTELL(fp), 0);
  37528. #else
  37529. ExpectIntNE(sz = XFTELL(fp), 0);
  37530. #endif
  37531. #endif
  37532. if (fp != XBADFILE)
  37533. XFCLOSE(fp);
  37534. (void)sz;
  37535. #endif
  37536. return EXPECT_RESULT();
  37537. } /* End test_wc_ERR_print_errors_fp */
  37538. #ifdef DEBUG_WOLFSSL
  37539. static void Logging_cb(const int logLevel, const char *const logMessage)
  37540. {
  37541. (void)logLevel;
  37542. (void)logMessage;
  37543. }
  37544. #endif
  37545. /*
  37546. * Testing wolfSSL_GetLoggingCb
  37547. */
  37548. static int test_wolfSSL_GetLoggingCb(void)
  37549. {
  37550. EXPECT_DECLS;
  37551. #ifdef DEBUG_WOLFSSL
  37552. /* Testing without wolfSSL_SetLoggingCb() */
  37553. ExpectNull(wolfSSL_GetLoggingCb());
  37554. /* Testing with wolfSSL_SetLoggingCb() */
  37555. ExpectIntEQ(wolfSSL_SetLoggingCb(Logging_cb), 0);
  37556. ExpectNotNull(wolfSSL_GetLoggingCb());
  37557. ExpectIntEQ(wolfSSL_SetLoggingCb(NULL), 0);
  37558. #endif
  37559. ExpectNull(wolfSSL_GetLoggingCb());
  37560. return EXPECT_RESULT();
  37561. } /* End test_wolfSSL_GetLoggingCb */
  37562. #endif /* !NO_BIO */
  37563. static int test_wolfSSL_MD4(void)
  37564. {
  37565. EXPECT_DECLS;
  37566. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  37567. MD4_CTX md4;
  37568. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  37569. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  37570. "789012345678901234567890";
  37571. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  37572. "\xcc\x05\x36";
  37573. int msgSz = (int)XSTRLEN(msg);
  37574. XMEMSET(out, 0, sizeof(out));
  37575. MD4_Init(&md4);
  37576. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  37577. MD4_Final(out, &md4);
  37578. ExpectIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  37579. #endif
  37580. return EXPECT_RESULT();
  37581. }
  37582. static int test_wolfSSL_MD5(void)
  37583. {
  37584. EXPECT_DECLS;
  37585. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  37586. byte input1[] = "";
  37587. byte input2[] = "message digest";
  37588. byte hash[WC_MD5_DIGEST_SIZE];
  37589. unsigned char output1[] =
  37590. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  37591. unsigned char output2[] =
  37592. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  37593. WOLFSSL_MD5_CTX md5;
  37594. XMEMSET(&md5, 0, sizeof(md5));
  37595. /* Test cases for illegal parameters */
  37596. ExpectIntEQ(MD5_Init(NULL), 0);
  37597. ExpectIntEQ(MD5_Init(&md5), 1);
  37598. ExpectIntEQ(MD5_Update(NULL, input1, 0), 0);
  37599. ExpectIntEQ(MD5_Update(NULL, NULL, 0), 0);
  37600. ExpectIntEQ(MD5_Update(&md5, NULL, 1), 0);
  37601. ExpectIntEQ(MD5_Final(NULL, &md5), 0);
  37602. ExpectIntEQ(MD5_Final(hash, NULL), 0);
  37603. ExpectIntEQ(MD5_Final(NULL, NULL), 0);
  37604. /* Init MD5 CTX */
  37605. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  37606. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input1, XSTRLEN((const char*)&input1)),
  37607. 1);
  37608. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  37609. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  37610. /* Init MD5 CTX */
  37611. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  37612. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input2,
  37613. (int)XSTRLEN((const char*)input2)), 1);
  37614. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  37615. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  37616. #if !defined(NO_OLD_NAMES) && \
  37617. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  37618. ExpectPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  37619. ExpectPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  37620. ExpectPtrNE(MD5(input1, 1, NULL), NULL);
  37621. ExpectPtrNE(MD5(NULL, 0, NULL), NULL);
  37622. ExpectPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash),
  37623. &hash);
  37624. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  37625. ExpectPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash),
  37626. &hash);
  37627. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  37628. {
  37629. byte data[] = "Data to be hashed.";
  37630. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  37631. ExpectNotNull(MD5(data, sizeof(data), NULL));
  37632. ExpectNotNull(MD5(data, sizeof(data), hash));
  37633. ExpectNotNull(MD5(NULL, 0, hash));
  37634. ExpectNull(MD5(NULL, sizeof(data), hash));
  37635. }
  37636. #endif
  37637. #endif
  37638. return EXPECT_RESULT();
  37639. }
  37640. static int test_wolfSSL_MD5_Transform(void)
  37641. {
  37642. EXPECT_DECLS;
  37643. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  37644. byte input1[] = "";
  37645. byte input2[] = "abc";
  37646. byte local[WC_MD5_BLOCK_SIZE];
  37647. word32 sLen = 0;
  37648. #ifdef BIG_ENDIAN_ORDER
  37649. unsigned char output1[] =
  37650. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  37651. unsigned char output2[] =
  37652. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  37653. #else
  37654. unsigned char output1[] =
  37655. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  37656. unsigned char output2[] =
  37657. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  37658. #endif
  37659. union {
  37660. wc_Md5 native;
  37661. MD5_CTX compat;
  37662. } md5;
  37663. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  37664. XMEMSET(&local, 0, sizeof(local));
  37665. /* sanity check */
  37666. ExpectIntEQ(MD5_Transform(NULL, NULL), 0);
  37667. ExpectIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  37668. ExpectIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  37669. ExpectIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  37670. ExpectIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37671. ExpectIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  37672. /* Init MD5 CTX */
  37673. ExpectIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  37674. /* Do Transform*/
  37675. sLen = (word32)XSTRLEN((char*)input1);
  37676. XMEMCPY(local, input1, sLen);
  37677. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  37678. ExpectIntEQ(XMEMCMP(md5.native.digest, output1, WC_MD5_DIGEST_SIZE), 0);
  37679. /* Init MD5 CTX */
  37680. ExpectIntEQ(MD5_Init(&md5.compat), 1);
  37681. sLen = (word32)XSTRLEN((char*)input2);
  37682. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  37683. XMEMCPY(local, input2, sLen);
  37684. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  37685. ExpectIntEQ(XMEMCMP(md5.native.digest, output2, WC_MD5_DIGEST_SIZE), 0);
  37686. #endif
  37687. return EXPECT_RESULT();
  37688. }
  37689. static int test_wolfSSL_SHA(void)
  37690. {
  37691. EXPECT_DECLS;
  37692. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  37693. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  37694. (!defined(HAVE_FIPS) || \
  37695. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  37696. {
  37697. const unsigned char in[] = "abc";
  37698. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  37699. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  37700. unsigned char out[WC_SHA_DIGEST_SIZE];
  37701. unsigned char* p = NULL;
  37702. WOLFSSL_SHA_CTX sha;
  37703. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  37704. ExpectNotNull(SHA1(in, XSTRLEN((char*)in), out));
  37705. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37706. /* SHA interface test */
  37707. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  37708. ExpectNull(SHA(NULL, XSTRLEN((char*)in), out));
  37709. ExpectNotNull(SHA(in, 0, out));
  37710. ExpectNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  37711. ExpectNotNull(SHA(NULL, 0, out));
  37712. ExpectNotNull(SHA(NULL, 0, NULL));
  37713. ExpectNotNull(SHA(in, XSTRLEN((char*)in), out));
  37714. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37715. ExpectNotNull(p = SHA(in, XSTRLEN((char*)in), NULL));
  37716. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA_DIGEST_SIZE), 0);
  37717. ExpectIntEQ(wolfSSL_SHA_Init(&sha), 1);
  37718. ExpectIntEQ(wolfSSL_SHA_Update(&sha, in, XSTRLEN((char*)in)), 1);
  37719. ExpectIntEQ(wolfSSL_SHA_Final(out, &sha), 1);
  37720. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37721. ExpectIntEQ(wolfSSL_SHA1_Init(&sha), 1);
  37722. ExpectIntEQ(wolfSSL_SHA1_Update(&sha, in, XSTRLEN((char*)in)), 1);
  37723. ExpectIntEQ(wolfSSL_SHA1_Final(out, &sha), 1);
  37724. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37725. }
  37726. #endif
  37727. #if !defined(NO_SHA256)
  37728. {
  37729. const unsigned char in[] = "abc";
  37730. unsigned char expected[] =
  37731. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  37732. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  37733. "\x15\xAD";
  37734. unsigned char out[WC_SHA256_DIGEST_SIZE];
  37735. unsigned char* p = NULL;
  37736. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  37737. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37738. ExpectNotNull(SHA256(in, XSTRLEN((char*)in), out));
  37739. #else
  37740. ExpectNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  37741. #endif
  37742. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  37743. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37744. ExpectNotNull(p = SHA256(in, XSTRLEN((char*)in), NULL));
  37745. #else
  37746. ExpectNotNull(p = wolfSSL_SHA256(in, XSTRLEN((char*)in), NULL));
  37747. #endif
  37748. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA256_DIGEST_SIZE), 0);
  37749. }
  37750. #endif
  37751. #if defined(WOLFSSL_SHA384)
  37752. {
  37753. const unsigned char in[] = "abc";
  37754. unsigned char expected[] =
  37755. "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  37756. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  37757. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  37758. "\xc8\x25\xa7";
  37759. unsigned char out[WC_SHA384_DIGEST_SIZE];
  37760. unsigned char* p = NULL;
  37761. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  37762. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37763. ExpectNotNull(SHA384(in, XSTRLEN((char*)in), out));
  37764. #else
  37765. ExpectNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  37766. #endif
  37767. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  37768. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37769. ExpectNotNull(p = SHA384(in, XSTRLEN((char*)in), NULL));
  37770. #else
  37771. ExpectNotNull(p = wolfSSL_SHA384(in, XSTRLEN((char*)in), NULL));
  37772. #endif
  37773. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA384_DIGEST_SIZE), 0);
  37774. }
  37775. #endif
  37776. #if defined(WOLFSSL_SHA512)
  37777. {
  37778. const unsigned char in[] = "abc";
  37779. unsigned char expected[] =
  37780. "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  37781. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  37782. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  37783. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  37784. "\xa5\x4c\xa4\x9f";
  37785. unsigned char out[WC_SHA512_DIGEST_SIZE];
  37786. unsigned char* p = NULL;
  37787. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  37788. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37789. ExpectNotNull(SHA512(in, XSTRLEN((char*)in), out));
  37790. #else
  37791. ExpectNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  37792. #endif
  37793. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  37794. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37795. ExpectNotNull(p = SHA512(in, XSTRLEN((char*)in), NULL));
  37796. #else
  37797. ExpectNotNull(p = wolfSSL_SHA512(in, XSTRLEN((char*)in), NULL));
  37798. #endif
  37799. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA512_DIGEST_SIZE), 0);
  37800. }
  37801. #endif
  37802. #endif
  37803. return EXPECT_RESULT();
  37804. }
  37805. static int test_wolfSSL_SHA_Transform(void)
  37806. {
  37807. EXPECT_DECLS;
  37808. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  37809. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37810. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  37811. byte input1[] = "";
  37812. byte input2[] = "abc";
  37813. byte local[WC_SHA_BLOCK_SIZE];
  37814. word32 sLen = 0;
  37815. #ifdef BIG_ENDIAN_ORDER
  37816. unsigned char output1[] =
  37817. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  37818. "\x09\xf7\x3a\x93";
  37819. unsigned char output2[] =
  37820. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  37821. "\x7c\x6e\x08\xb8";
  37822. #else
  37823. unsigned char output1[] =
  37824. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  37825. "\x93\x3a\xf7\x09";
  37826. unsigned char output2[] =
  37827. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  37828. "\xb8\x08\x6e\x7c";
  37829. #endif
  37830. union {
  37831. wc_Sha native;
  37832. SHA_CTX compat;
  37833. } sha;
  37834. union {
  37835. wc_Sha native;
  37836. SHA_CTX compat;
  37837. } sha1;
  37838. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  37839. XMEMSET(&local, 0, sizeof(local));
  37840. /* sanity check */
  37841. ExpectIntEQ(SHA_Transform(NULL, NULL), 0);
  37842. ExpectIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  37843. ExpectIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  37844. ExpectIntEQ(SHA1_Transform(NULL, NULL), 0);
  37845. ExpectIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  37846. ExpectIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  37847. ExpectIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  37848. ExpectIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37849. ExpectIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  37850. /* Init SHA CTX */
  37851. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  37852. /* Do Transform*/
  37853. sLen = (word32)XSTRLEN((char*)input1);
  37854. XMEMCPY(local, input1, sLen);
  37855. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  37856. ExpectIntEQ(XMEMCMP(sha.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  37857. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  37858. /* Init SHA CTX */
  37859. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  37860. sLen = (word32)XSTRLEN((char*)input2);
  37861. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37862. XMEMCPY(local, input2, sLen);
  37863. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  37864. ExpectIntEQ(XMEMCMP(sha.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  37865. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  37866. /* SHA1 */
  37867. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37868. /* Init SHA CTX */
  37869. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  37870. /* Do Transform*/
  37871. sLen = (word32)XSTRLEN((char*)input1);
  37872. XMEMCPY(local, input1, sLen);
  37873. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  37874. ExpectIntEQ(XMEMCMP(sha1.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  37875. ExpectIntEQ(SHA1_Final(local, &sha1.compat), 1); /* frees resources */
  37876. /* Init SHA CTX */
  37877. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  37878. sLen = (word32)XSTRLEN((char*)input2);
  37879. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  37880. XMEMCPY(local, input2, sLen);
  37881. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  37882. ExpectIntEQ(XMEMCMP(sha1.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  37883. ExpectIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  37884. #endif
  37885. #endif
  37886. return EXPECT_RESULT();
  37887. }
  37888. static int test_wolfSSL_SHA224(void)
  37889. {
  37890. EXPECT_DECLS;
  37891. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  37892. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37893. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  37894. unsigned char input[] =
  37895. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  37896. unsigned char output[] =
  37897. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  37898. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  37899. size_t inLen;
  37900. byte hash[WC_SHA224_DIGEST_SIZE];
  37901. unsigned char* p;
  37902. inLen = XSTRLEN((char*)input);
  37903. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  37904. ExpectNull(SHA224(NULL, inLen, hash));
  37905. ExpectNotNull(SHA224(input, 0, hash));
  37906. ExpectNotNull(SHA224(input, inLen, NULL));
  37907. ExpectNotNull(SHA224(NULL, 0, hash));
  37908. ExpectNotNull(SHA224(NULL, 0, NULL));
  37909. ExpectNotNull(SHA224(input, inLen, hash));
  37910. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  37911. ExpectNotNull(p = SHA224(input, inLen, NULL));
  37912. ExpectIntEQ(XMEMCMP(p, output, WC_SHA224_DIGEST_SIZE), 0);
  37913. #endif
  37914. return EXPECT_RESULT();
  37915. }
  37916. static int test_wolfSSL_SHA256(void)
  37917. {
  37918. EXPECT_DECLS;
  37919. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  37920. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  37921. unsigned char input[] =
  37922. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  37923. unsigned char output[] =
  37924. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  37925. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  37926. "\x06\xC1";
  37927. size_t inLen;
  37928. byte hash[WC_SHA256_DIGEST_SIZE];
  37929. inLen = XSTRLEN((char*)input);
  37930. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  37931. ExpectNotNull(SHA256(input, inLen, hash));
  37932. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  37933. #endif
  37934. return EXPECT_RESULT();
  37935. }
  37936. static int test_wolfSSL_SHA256_Transform(void)
  37937. {
  37938. EXPECT_DECLS;
  37939. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  37940. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  37941. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  37942. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH) && \
  37943. !defined(WOLFSSL_KCAPI_HASH)
  37944. byte input1[] = "";
  37945. byte input2[] = "abc";
  37946. byte local[WC_SHA256_BLOCK_SIZE];
  37947. word32 sLen = 0;
  37948. #ifdef BIG_ENDIAN_ORDER
  37949. unsigned char output1[] =
  37950. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  37951. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  37952. unsigned char output2[] =
  37953. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  37954. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  37955. #else
  37956. unsigned char output1[] =
  37957. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  37958. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  37959. unsigned char output2[] =
  37960. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  37961. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  37962. #endif
  37963. union {
  37964. wc_Sha256 native;
  37965. SHA256_CTX compat;
  37966. } sha256;
  37967. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  37968. XMEMSET(&local, 0, sizeof(local));
  37969. /* sanity check */
  37970. ExpectIntEQ(SHA256_Transform(NULL, NULL), 0);
  37971. ExpectIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  37972. ExpectIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  37973. ExpectIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  37974. ExpectIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37975. ExpectIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  37976. /* Init SHA256 CTX */
  37977. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  37978. /* Do Transform*/
  37979. sLen = (word32)XSTRLEN((char*)input1);
  37980. XMEMCPY(local, input1, sLen);
  37981. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  37982. ExpectIntEQ(XMEMCMP(sha256.native.digest, output1, WC_SHA256_DIGEST_SIZE),
  37983. 0);
  37984. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  37985. /* Init SHA256 CTX */
  37986. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  37987. sLen = (word32)XSTRLEN((char*)input2);
  37988. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  37989. XMEMCPY(local, input2, sLen);
  37990. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  37991. ExpectIntEQ(XMEMCMP(sha256.native.digest, output2, WC_SHA256_DIGEST_SIZE),
  37992. 0);
  37993. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  37994. #endif
  37995. #endif
  37996. return EXPECT_RESULT();
  37997. }
  37998. static int test_wolfSSL_SHA512_Transform(void)
  37999. {
  38000. EXPECT_DECLS;
  38001. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  38002. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  38003. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  38004. !defined(WOLFSSL_KCAPI_HASH)
  38005. byte input1[] = "";
  38006. byte input2[] = "abc";
  38007. byte local[WC_SHA512_BLOCK_SIZE];
  38008. word32 sLen = 0;
  38009. #ifdef BIG_ENDIAN_ORDER
  38010. unsigned char output1[] =
  38011. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  38012. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  38013. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  38014. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  38015. unsigned char output2[] =
  38016. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  38017. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  38018. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  38019. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  38020. #else
  38021. unsigned char output1[] =
  38022. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  38023. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  38024. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  38025. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  38026. unsigned char output2[] =
  38027. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  38028. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  38029. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  38030. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  38031. #endif
  38032. union {
  38033. wc_Sha512 native;
  38034. SHA512_CTX compat;
  38035. } sha512;
  38036. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  38037. XMEMSET(&local, 0, sizeof(local));
  38038. /* sanity check */
  38039. ExpectIntEQ(SHA512_Transform(NULL, NULL), 0);
  38040. ExpectIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  38041. ExpectIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  38042. ExpectIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  38043. ExpectIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  38044. ExpectIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  38045. /* Init SHA512 CTX */
  38046. ExpectIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  38047. /* Do Transform*/
  38048. sLen = (word32)XSTRLEN((char*)input1);
  38049. XMEMCPY(local, input1, sLen);
  38050. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  38051. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  38052. WC_SHA512_DIGEST_SIZE), 0);
  38053. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  38054. /* Init SHA512 CTX */
  38055. ExpectIntEQ(SHA512_Init(&sha512.compat), 1);
  38056. sLen = (word32)XSTRLEN((char*)input2);
  38057. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  38058. XMEMCPY(local, input2, sLen);
  38059. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  38060. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  38061. WC_SHA512_DIGEST_SIZE), 0);
  38062. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  38063. (void)input1;
  38064. #endif
  38065. #endif
  38066. return EXPECT_RESULT();
  38067. }
  38068. static int test_wolfSSL_SHA512_224_Transform(void)
  38069. {
  38070. EXPECT_DECLS;
  38071. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  38072. !defined(WOLFSSL_NOSHA512_224)
  38073. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  38074. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  38075. !defined(WOLFSSL_KCAPI_HASH)
  38076. byte input1[] = "";
  38077. byte input2[] = "abc";
  38078. byte local[WC_SHA512_BLOCK_SIZE];
  38079. word32 sLen = 0;
  38080. unsigned char output1[] =
  38081. "\x94\x24\x66\xd4\x60\x3a\xeb\x23\x1d\xa8\x69\x31\x3c\xd2\xde\x11"
  38082. "\x48\x0f\x4a\x5a\xdf\x3a\x8d\x87\xcf\xcd\xbf\xa5\x03\x21\x50\xf1"
  38083. "\x8a\x0d\x0f\x0d\x3c\x07\xba\x52\xe0\xaa\x3c\xbb\xf1\xd3\x3f\xca"
  38084. "\x12\xa7\x61\xf8\x47\xda\x0d\x1b\x79\xc2\x65\x13\x92\xc1\x9c\xa5";
  38085. unsigned char output2[] =
  38086. "\x51\x28\xe7\x0b\xca\x1e\xbc\x5f\xd7\x34\x0b\x48\x30\xd7\xc2\x75"
  38087. "\x6d\x8d\x48\x2c\x1f\xc7\x9e\x2b\x20\x5e\xbb\x0f\x0e\x4d\xb7\x61"
  38088. "\x31\x76\x33\xa0\xb4\x3d\x5f\x93\xc1\x73\xac\xf7\x21\xff\x69\x17"
  38089. "\xce\x66\xe5\x1e\x31\xe7\xf3\x22\x0f\x0b\x34\xd7\x5a\x57\xeb\xbf";
  38090. union {
  38091. wc_Sha512 native;
  38092. SHA512_CTX compat;
  38093. } sha512;
  38094. #ifdef BIG_ENDIAN_ORDER
  38095. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  38096. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  38097. #endif
  38098. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  38099. XMEMSET(&local, 0, sizeof(local));
  38100. /* sanity check */
  38101. ExpectIntEQ(SHA512_224_Transform(NULL, NULL), 0);
  38102. ExpectIntEQ(SHA512_224_Transform(NULL, (const byte*)&input1), 0);
  38103. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, NULL), 0);
  38104. ExpectIntEQ(wc_Sha512_224Transform(NULL, NULL), BAD_FUNC_ARG);
  38105. ExpectIntEQ(wc_Sha512_224Transform(NULL, (const byte*)&input1),
  38106. BAD_FUNC_ARG);
  38107. ExpectIntEQ(wc_Sha512_224Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  38108. /* Init SHA512 CTX */
  38109. ExpectIntEQ(wolfSSL_SHA512_224_Init(&sha512.compat), 1);
  38110. /* Do Transform*/
  38111. sLen = (word32)XSTRLEN((char*)input1);
  38112. XMEMCPY(local, input1, sLen);
  38113. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  38114. 1);
  38115. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  38116. WC_SHA512_DIGEST_SIZE), 0);
  38117. /* frees resources */
  38118. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  38119. /* Init SHA512 CTX */
  38120. ExpectIntEQ(SHA512_224_Init(&sha512.compat), 1);
  38121. sLen = (word32)XSTRLEN((char*)input2);
  38122. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  38123. XMEMCPY(local, input2, sLen);
  38124. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  38125. 1);
  38126. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  38127. WC_SHA512_DIGEST_SIZE), 0);
  38128. /* frees resources */
  38129. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  38130. #endif
  38131. #endif
  38132. return EXPECT_RESULT();
  38133. }
  38134. static int test_wolfSSL_SHA512_256_Transform(void)
  38135. {
  38136. EXPECT_DECLS;
  38137. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  38138. !defined(WOLFSSL_NOSHA512_256)
  38139. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  38140. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  38141. !defined(WOLFSSL_KCAPI_HASH)
  38142. byte input1[] = "";
  38143. byte input2[] = "abc";
  38144. byte local[WC_SHA512_BLOCK_SIZE];
  38145. word32 sLen = 0;
  38146. unsigned char output1[] =
  38147. "\xf8\x37\x37\x5a\xd7\x2e\x56\xec\xe2\x51\xa8\x31\x3a\xa0\x63\x2b"
  38148. "\x7e\x7c\x64\xcc\xd9\xff\x2b\x6b\xeb\xc3\xd4\x4d\x7f\x8a\x3a\xb5"
  38149. "\x61\x85\x0b\x37\x30\x9f\x3b\x08\x5e\x7b\xd3\xbc\x6d\x00\x61\xc0"
  38150. "\x65\x9a\xd7\x73\xda\x40\xbe\xc1\xe5\x2f\xc6\x5d\xb7\x9f\xbe\x60";
  38151. unsigned char output2[] =
  38152. "\x22\xad\xc0\x30\xee\xd4\x6a\xef\x13\xee\x5a\x95\x8b\x1f\xb7\xb6"
  38153. "\xb6\xba\xc0\x44\xb8\x18\x3b\xf0\xf6\x4b\x70\x9f\x03\xba\x64\xa1"
  38154. "\xe1\xe3\x45\x15\x91\x7d\xcb\x0b\x9a\xf0\xd2\x8e\x47\x8b\x37\x78"
  38155. "\x91\x41\xa6\xc4\xb0\x29\x8f\x8b\xdd\x78\x5c\xf2\x73\x3f\x21\x31";
  38156. union {
  38157. wc_Sha512 native;
  38158. SHA512_CTX compat;
  38159. } sha512;
  38160. #ifdef BIG_ENDIAN_ORDER
  38161. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  38162. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  38163. #endif
  38164. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  38165. XMEMSET(&local, 0, sizeof(local));
  38166. /* sanity check */
  38167. ExpectIntEQ(SHA512_256_Transform(NULL, NULL), 0);
  38168. ExpectIntEQ(SHA512_256_Transform(NULL, (const byte*)&input1), 0);
  38169. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, NULL), 0);
  38170. ExpectIntEQ(wc_Sha512_256Transform(NULL, NULL), BAD_FUNC_ARG);
  38171. ExpectIntEQ(wc_Sha512_256Transform(NULL, (const byte*)&input1),
  38172. BAD_FUNC_ARG);
  38173. ExpectIntEQ(wc_Sha512_256Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  38174. /* Init SHA512 CTX */
  38175. ExpectIntEQ(wolfSSL_SHA512_256_Init(&sha512.compat), 1);
  38176. /* Do Transform*/
  38177. sLen = (word32)XSTRLEN((char*)input1);
  38178. XMEMCPY(local, input1, sLen);
  38179. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  38180. 1);
  38181. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  38182. WC_SHA512_DIGEST_SIZE), 0);
  38183. /* frees resources */
  38184. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  38185. /* Init SHA512 CTX */
  38186. ExpectIntEQ(SHA512_256_Init(&sha512.compat), 1);
  38187. sLen = (word32)XSTRLEN((char*)input2);
  38188. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  38189. XMEMCPY(local, input2, sLen);
  38190. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  38191. 1);
  38192. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  38193. WC_SHA512_DIGEST_SIZE), 0);
  38194. /* frees resources */
  38195. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  38196. #endif
  38197. #endif
  38198. return EXPECT_RESULT();
  38199. }
  38200. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  38201. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  38202. * buffer of 64 bytes.
  38203. *
  38204. * returns the size of the digest buffer on success and a negative value on
  38205. * failure.
  38206. */
  38207. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest,
  38208. int* sz)
  38209. {
  38210. EXPECT_DECLS;
  38211. HMAC_CTX ctx1;
  38212. HMAC_CTX ctx2;
  38213. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  38214. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  38215. unsigned char long_key[] =
  38216. "0123456789012345678901234567890123456789"
  38217. "0123456789012345678901234567890123456789"
  38218. "0123456789012345678901234567890123456789"
  38219. "0123456789012345678901234567890123456789";
  38220. unsigned char msg[] = "message to hash";
  38221. unsigned int digestSz = 64;
  38222. int keySz = sizeof(key);
  38223. int long_keySz = sizeof(long_key);
  38224. int msgSz = sizeof(msg);
  38225. unsigned char digest2[64];
  38226. unsigned int digestSz2 = 64;
  38227. HMAC_CTX_init(&ctx1);
  38228. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  38229. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38230. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38231. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38232. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38233. HMAC_CTX_cleanup(&ctx1);
  38234. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38235. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  38236. HMAC_CTX_cleanup(&ctx2);
  38237. ExpectIntEQ(digestSz, digestSz2);
  38238. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38239. /* test HMAC_Init with NULL key */
  38240. /* init after copy */
  38241. HMAC_CTX_init(&ctx1);
  38242. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  38243. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38244. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38245. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  38246. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38247. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38248. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38249. HMAC_CTX_cleanup(&ctx1);
  38250. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  38251. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38252. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38253. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  38254. HMAC_CTX_cleanup(&ctx2);
  38255. ExpectIntEQ(digestSz, digestSz2);
  38256. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38257. /* long key */
  38258. HMAC_CTX_init(&ctx1);
  38259. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type),
  38260. SSL_SUCCESS);
  38261. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38262. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38263. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  38264. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38265. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38266. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38267. HMAC_CTX_cleanup(&ctx1);
  38268. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  38269. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38270. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38271. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  38272. HMAC_CTX_cleanup(&ctx2);
  38273. ExpectIntEQ(digestSz, digestSz2);
  38274. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38275. /* init before copy */
  38276. HMAC_CTX_init(&ctx1);
  38277. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  38278. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38279. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  38280. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38281. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38282. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38283. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38284. HMAC_CTX_cleanup(&ctx1);
  38285. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38286. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38287. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  38288. HMAC_CTX_cleanup(&ctx2);
  38289. ExpectIntEQ(digestSz, digestSz2);
  38290. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38291. *sz = digestSz;
  38292. return EXPECT_RESULT();
  38293. }
  38294. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  38295. static int test_wolfSSL_HMAC_CTX(void)
  38296. {
  38297. EXPECT_DECLS;
  38298. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  38299. unsigned char digest[64];
  38300. int digestSz;
  38301. WOLFSSL_HMAC_CTX* hmac_ctx = NULL;
  38302. WOLFSSL_HMAC_CTX ctx1;
  38303. WOLFSSL_HMAC_CTX ctx2;
  38304. ExpectNotNull(hmac_ctx = wolfSSL_HMAC_CTX_new());
  38305. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(NULL), 1);
  38306. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(hmac_ctx), 1);
  38307. wolfSSL_HMAC_CTX_free(NULL);
  38308. wolfSSL_HMAC_CTX_free(hmac_ctx);
  38309. XMEMSET(&ctx2, 0, sizeof(WOLFSSL_HMAC_CTX));
  38310. ExpectIntEQ(HMAC_CTX_init(NULL), 1);
  38311. ExpectIntEQ(HMAC_CTX_init(&ctx2), 1);
  38312. ExpectIntEQ(HMAC_CTX_copy(NULL, NULL), 0);
  38313. ExpectIntEQ(HMAC_CTX_copy(NULL, &ctx2), 0);
  38314. ExpectIntEQ(HMAC_CTX_copy(&ctx2, NULL), 0);
  38315. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  38316. ((! defined(HAVE_FIPS_VERSION)) || \
  38317. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  38318. /* Copy object that hasn't had a digest set - MD5. */
  38319. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 1);
  38320. #else
  38321. /* Copy object that hasn't had a digest set. */
  38322. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 0);
  38323. #endif
  38324. HMAC_CTX_cleanup(NULL);
  38325. HMAC_CTX_cleanup(&ctx2);
  38326. ExpectNull(HMAC_CTX_get_md(NULL));
  38327. #ifndef NO_SHA
  38328. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha1(), digest, &digestSz)),
  38329. TEST_SUCCESS);
  38330. ExpectIntEQ(digestSz, 20);
  38331. ExpectIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  38332. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  38333. #endif /* !NO_SHA */
  38334. #ifdef WOLFSSL_SHA224
  38335. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha224(), digest, &digestSz)),
  38336. TEST_SUCCESS);
  38337. ExpectIntEQ(digestSz, 28);
  38338. ExpectIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  38339. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  38340. "\x02\x0E", digest, digestSz), 0);
  38341. #endif /* WOLFSSL_SHA224 */
  38342. #ifndef NO_SHA256
  38343. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha256(), digest, &digestSz)),
  38344. TEST_SUCCESS);
  38345. ExpectIntEQ(digestSz, 32);
  38346. ExpectIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  38347. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  38348. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  38349. #endif /* !NO_SHA256 */
  38350. #ifdef WOLFSSL_SHA384
  38351. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha384(), digest, &digestSz)),
  38352. TEST_SUCCESS);
  38353. ExpectIntEQ(digestSz, 48);
  38354. ExpectIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  38355. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  38356. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  38357. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  38358. digest, digestSz), 0);
  38359. #endif /* WOLFSSL_SHA384 */
  38360. #ifdef WOLFSSL_SHA512
  38361. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha512(), digest, &digestSz)),
  38362. TEST_SUCCESS);
  38363. ExpectIntEQ(digestSz, 64);
  38364. ExpectIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  38365. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  38366. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  38367. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  38368. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  38369. digest, digestSz), 0);
  38370. #endif /* WOLFSSL_SHA512 */
  38371. #ifdef WOLFSSL_SHA3
  38372. #ifndef WOLFSSL_NOSHA3_224
  38373. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_224(), digest, &digestSz)),
  38374. TEST_SUCCESS);
  38375. ExpectIntEQ(digestSz, 28);
  38376. ExpectIntEQ(XMEMCMP("\xdc\x53\x25\x3f\xc0\x9d\x2b\x0c\x7f\x59\x11\x17\x08"
  38377. "\x5c\xe8\x43\x31\x01\x5a\xb3\xe3\x08\x37\x71\x26\x0b"
  38378. "\x29\x0f", digest, digestSz), 0);
  38379. #endif
  38380. #ifndef WOLFSSL_NOSHA3_256
  38381. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_256(), digest, &digestSz)),
  38382. TEST_SUCCESS);
  38383. ExpectIntEQ(digestSz, 32);
  38384. ExpectIntEQ(XMEMCMP("\x0f\x00\x89\x82\x15\xce\xd6\x45\x01\x83\xce\xc8\x35"
  38385. "\xab\x71\x07\xc9\xfe\x61\x22\x38\xf9\x09\xad\x35\x65"
  38386. "\x43\x77\x24\xd4\x1e\xf4", digest, digestSz), 0);
  38387. #endif
  38388. #ifndef WOLFSSL_NOSHA3_384
  38389. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_384(), digest, &digestSz)),
  38390. TEST_SUCCESS);
  38391. ExpectIntEQ(digestSz, 48);
  38392. ExpectIntEQ(XMEMCMP("\x0f\x6a\xc0\xfb\xc3\xf2\x80\xb1\xb4\x04\xb6\xc8\x45"
  38393. "\x23\x3b\xb4\xbe\xc6\xea\x85\x07\xca\x8c\x71\xbb\x6e"
  38394. "\x79\xf6\xf9\x2b\x98\xf5\xef\x11\x39\xd4\x5d\xd3\xca"
  38395. "\xc0\xe6\x81\xf7\x73\xf9\x85\x5d\x4f",
  38396. digest, digestSz), 0);
  38397. #endif
  38398. #ifndef WOLFSSL_NOSHA3_512
  38399. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_512(), digest, &digestSz)),
  38400. TEST_SUCCESS);
  38401. ExpectIntEQ(digestSz, 64);
  38402. ExpectIntEQ(XMEMCMP("\x3e\x77\xe3\x59\x42\x89\xed\xc3\xa4\x26\x3d\xa4\x75"
  38403. "\xd2\x84\x8c\xb2\xf3\x25\x04\x47\x61\xce\x1c\x42\x86"
  38404. "\xcd\xf4\x56\xaa\x2f\x84\xb1\x3b\x18\xed\xe6\xd6\x48"
  38405. "\x15\xb0\x29\xc5\x9d\x32\xef\xdd\x3e\x09\xf6\xed\x9e"
  38406. "\x70\xbc\x1c\x63\xf7\x3b\x3e\xe1\xdc\x84\x9c\x1c",
  38407. digest, digestSz), 0);
  38408. #endif
  38409. #endif
  38410. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || \
  38411. HAVE_FIPS_VERSION <= 2)
  38412. ExpectIntEQ((test_HMAC_CTX_helper(EVP_md5(), digest, &digestSz)),
  38413. TEST_SUCCESS);
  38414. ExpectIntEQ(digestSz, 16);
  38415. ExpectIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  38416. "\xE4\x98\xDD", digest, digestSz), 0);
  38417. #endif /* !NO_MD5 */
  38418. #endif
  38419. return EXPECT_RESULT();
  38420. }
  38421. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  38422. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  38423. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  38424. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  38425. {
  38426. EXPECT_DECLS;
  38427. static const unsigned char key[] = "simple test key";
  38428. HMAC_CTX* hmac = NULL;
  38429. ENGINE* e = NULL;
  38430. unsigned char hash[WC_MAX_DIGEST_SIZE];
  38431. unsigned int len;
  38432. ExpectNotNull(hmac = HMAC_CTX_new());
  38433. HMAC_CTX_init(hmac);
  38434. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  38435. ((! defined(HAVE_FIPS_VERSION)) || \
  38436. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  38437. /* Get size on object that hasn't had a digest set - MD5. */
  38438. ExpectIntEQ(HMAC_size(hmac), 16);
  38439. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 1);
  38440. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 1);
  38441. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 1);
  38442. #else
  38443. ExpectIntEQ(HMAC_size(hmac), BAD_FUNC_ARG);
  38444. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 0);
  38445. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 0);
  38446. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 0);
  38447. #endif
  38448. ExpectIntEQ(HMAC_Init_ex(NULL, (void*)key, (int)sizeof(key), md, e), 0);
  38449. ExpectIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e), 1);
  38450. /* re-using test key as data to hash */
  38451. ExpectIntEQ(HMAC_Update(NULL, key, (int)sizeof(key)), 0);
  38452. ExpectIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), 1);
  38453. ExpectIntEQ(HMAC_Update(hmac, key, 0), 1);
  38454. ExpectIntEQ(HMAC_Update(hmac, NULL, 0), 1);
  38455. ExpectIntEQ(HMAC_Update(hmac, NULL, (int)sizeof(key)), 1);
  38456. ExpectIntEQ(HMAC_Final(NULL, NULL, &len), 0);
  38457. ExpectIntEQ(HMAC_Final(hmac, NULL, &len), 0);
  38458. ExpectIntEQ(HMAC_Final(NULL, hash, &len), 0);
  38459. ExpectIntEQ(HMAC_Final(hmac, hash, &len), 1);
  38460. ExpectIntEQ(HMAC_Final(hmac, hash, NULL), 1);
  38461. ExpectIntEQ(len, md_len);
  38462. ExpectIntEQ(HMAC_size(NULL), 0);
  38463. ExpectIntEQ(HMAC_size(hmac), md_len);
  38464. ExpectStrEQ(HMAC_CTX_get_md(hmac), md);
  38465. HMAC_cleanup(NULL);
  38466. HMAC_cleanup(hmac);
  38467. HMAC_CTX_free(hmac);
  38468. len = 0;
  38469. ExpectNull(HMAC(NULL, key, (int)sizeof(key), NULL, 0, hash, &len));
  38470. ExpectNull(HMAC(md, NULL, (int)sizeof(key), NULL, 0, hash, &len));
  38471. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, 0, NULL, &len));
  38472. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  38473. ExpectIntEQ(len, md_len);
  38474. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, NULL));
  38475. /* With data. */
  38476. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, (int)sizeof(key), hash,
  38477. &len));
  38478. /* With NULL data. */
  38479. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, (int)sizeof(key), hash,
  38480. &len));
  38481. /* With zero length data. */
  38482. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, 0, hash, &len));
  38483. return EXPECT_RESULT();
  38484. }
  38485. #endif
  38486. static int test_wolfSSL_HMAC(void)
  38487. {
  38488. EXPECT_DECLS;
  38489. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  38490. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  38491. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  38492. #ifndef NO_SHA256
  38493. ExpectIntEQ(test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE),
  38494. TEST_SUCCESS);
  38495. #endif
  38496. #ifdef WOLFSSL_SHA224
  38497. ExpectIntEQ(test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE),
  38498. TEST_SUCCESS);
  38499. #endif
  38500. #ifdef WOLFSSL_SHA384
  38501. ExpectIntEQ(test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE),
  38502. TEST_SUCCESS);
  38503. #endif
  38504. #ifdef WOLFSSL_SHA512
  38505. ExpectIntEQ(test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE),
  38506. TEST_SUCCESS);
  38507. #endif
  38508. #ifdef WOLFSSL_SHA3
  38509. #ifndef WOLFSSL_NOSHA3_224
  38510. ExpectIntEQ(test_openssl_hmac(EVP_sha3_224(),
  38511. (int)WC_SHA3_224_DIGEST_SIZE), TEST_SUCCESS);
  38512. #endif
  38513. #ifndef WOLFSSL_NOSHA3_256
  38514. ExpectIntEQ(test_openssl_hmac(EVP_sha3_256(),
  38515. (int)WC_SHA3_256_DIGEST_SIZE), TEST_SUCCESS);
  38516. #endif
  38517. #ifndef WOLFSSL_NOSHA3_384
  38518. ExpectIntEQ(test_openssl_hmac(EVP_sha3_384(),
  38519. (int)WC_SHA3_384_DIGEST_SIZE), TEST_SUCCESS);
  38520. #endif
  38521. #ifndef WOLFSSL_NOSHA3_512
  38522. ExpectIntEQ(test_openssl_hmac(EVP_sha3_512(),
  38523. (int)WC_SHA3_512_DIGEST_SIZE), TEST_SUCCESS);
  38524. #endif
  38525. #endif
  38526. #ifndef NO_SHA
  38527. ExpectIntEQ(test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE),
  38528. TEST_SUCCESS);
  38529. #endif
  38530. #endif
  38531. return EXPECT_RESULT();
  38532. }
  38533. static int test_wolfSSL_CMAC(void)
  38534. {
  38535. EXPECT_DECLS;
  38536. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  38537. defined(WOLFSSL_AES_DIRECT)
  38538. int i;
  38539. byte key[AES_256_KEY_SIZE];
  38540. CMAC_CTX* cmacCtx = NULL;
  38541. byte out[AES_BLOCK_SIZE];
  38542. size_t outLen = AES_BLOCK_SIZE;
  38543. for (i=0; i < AES_256_KEY_SIZE; ++i) {
  38544. key[i] = i;
  38545. }
  38546. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38547. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  38548. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  38549. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  38550. NULL), 1);
  38551. /* re-using test key as data to hash */
  38552. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  38553. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  38554. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 1);
  38555. ExpectIntEQ(outLen, AES_BLOCK_SIZE);
  38556. /* No Update works. */
  38557. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  38558. NULL), 1);
  38559. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  38560. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  38561. NULL), 1);
  38562. /* Test parameters with CMAC_Update. */
  38563. ExpectIntEQ(CMAC_Update(NULL, NULL, 0), 0);
  38564. ExpectIntEQ(CMAC_Update(NULL, key, 0), 0);
  38565. ExpectIntEQ(CMAC_Update(NULL, NULL, AES_128_KEY_SIZE), 0);
  38566. ExpectIntEQ(CMAC_Update(NULL, key, AES_128_KEY_SIZE), 0);
  38567. ExpectIntEQ(CMAC_Update(cmacCtx, key, 0), 1);
  38568. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  38569. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, AES_128_KEY_SIZE), 1);
  38570. /* Test parameters with CMAC_Final. */
  38571. ExpectIntEQ(CMAC_Final(NULL, NULL, NULL), 0);
  38572. ExpectIntEQ(CMAC_Final(NULL, out, NULL), 0);
  38573. ExpectIntEQ(CMAC_Final(NULL, NULL, &outLen), 0);
  38574. ExpectIntEQ(CMAC_Final(NULL, out, &outLen), 0);
  38575. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, NULL), 1);
  38576. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, &outLen), 1);
  38577. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  38578. CMAC_CTX_free(cmacCtx);
  38579. /* Test parameters with CMAC Init. */
  38580. cmacCtx = NULL;
  38581. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38582. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  38583. ExpectIntEQ(CMAC_Init(NULL, NULL, 0, NULL, NULL), 0);
  38584. ExpectIntEQ(CMAC_Init(NULL, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  38585. NULL), 0);
  38586. ExpectIntEQ(CMAC_Init(cmacCtx, NULL, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  38587. NULL), 0);
  38588. /* give a key too small for the cipher, verify we get failure */
  38589. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_192_cbc(),
  38590. NULL), 0);
  38591. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, NULL, NULL), 0);
  38592. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128)
  38593. /* Only AES-CBC supported. */
  38594. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_gcm(),
  38595. NULL), 0);
  38596. #endif
  38597. CMAC_CTX_free(cmacCtx);
  38598. ExpectNull(CMAC_CTX_get0_cipher_ctx(NULL));
  38599. cmacCtx = NULL;
  38600. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38601. /* No Init. */
  38602. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 0);
  38603. CMAC_CTX_free(cmacCtx);
  38604. /* Test AES-256-CBC */
  38605. cmacCtx = NULL;
  38606. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38607. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_256_KEY_SIZE, EVP_aes_256_cbc(),
  38608. NULL), 1);
  38609. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  38610. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  38611. CMAC_CTX_free(cmacCtx);
  38612. /* Test AES-192-CBC */
  38613. cmacCtx = NULL;
  38614. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38615. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  38616. NULL), 1);
  38617. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  38618. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  38619. CMAC_CTX_free(cmacCtx);
  38620. cmacCtx = NULL;
  38621. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38622. CMAC_CTX_free(cmacCtx);
  38623. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  38624. return EXPECT_RESULT();
  38625. }
  38626. static int test_wolfSSL_DES(void)
  38627. {
  38628. EXPECT_DECLS;
  38629. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  38630. const_DES_cblock myDes;
  38631. DES_cblock iv;
  38632. DES_key_schedule key;
  38633. word32 i = 0;
  38634. DES_LONG dl = 0;
  38635. unsigned char msg[] = "hello wolfssl";
  38636. unsigned char weakKey[][8] = {
  38637. { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
  38638. { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
  38639. { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 },
  38640. { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E }
  38641. };
  38642. unsigned char semiWeakKey[][8] = {
  38643. { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E },
  38644. { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 },
  38645. { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 },
  38646. { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 },
  38647. { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE },
  38648. { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 },
  38649. { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 },
  38650. { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E },
  38651. { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE },
  38652. { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E },
  38653. { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
  38654. { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
  38655. };
  38656. DES_check_key(1);
  38657. DES_set_key(&myDes, &key);
  38658. /* check, check of odd parity */
  38659. XMEMSET(myDes, 4, sizeof(const_DES_cblock));
  38660. myDes[0] = 6; /* set even parity */
  38661. XMEMSET(key, 5, sizeof(DES_key_schedule));
  38662. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -1);
  38663. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  38664. ExpectIntEQ(DES_set_key_checked(NULL, NULL), -2);
  38665. ExpectIntEQ(DES_set_key_checked(&myDes, NULL), -2);
  38666. ExpectIntEQ(DES_set_key_checked(NULL, &key), -2);
  38667. /* set odd parity for success case */
  38668. DES_set_odd_parity(&myDes);
  38669. ExpectIntEQ(DES_check_key_parity(&myDes), 1);
  38670. fprintf(stderr, "%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2],
  38671. myDes[3]);
  38672. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  38673. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  38674. ExpectIntEQ(key[i], myDes[i]);
  38675. }
  38676. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  38677. /* check weak key */
  38678. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  38679. XMEMSET(key, 5, sizeof(DES_key_schedule));
  38680. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -2);
  38681. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  38682. DES_set_key_unchecked(NULL, NULL);
  38683. DES_set_key_unchecked(&myDes, NULL);
  38684. DES_set_key_unchecked(NULL, &key);
  38685. /* compare arrays, should be the same */
  38686. /* now do unchecked copy of a weak key over */
  38687. DES_set_key_unchecked(&myDes, &key);
  38688. /* compare arrays, should be the same */
  38689. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  38690. ExpectIntEQ(key[i], myDes[i]);
  38691. }
  38692. ExpectIntEQ(DES_is_weak_key(&myDes), 1);
  38693. myDes[7] = 2;
  38694. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  38695. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  38696. ExpectIntEQ(DES_is_weak_key(NULL), 1);
  38697. /* Test all weak keys. */
  38698. for (i = 0; i < sizeof(weakKey) / sizeof(*weakKey); i++) {
  38699. ExpectIntEQ(DES_set_key_checked(&weakKey[i], &key), -2);
  38700. }
  38701. /* Test all semi-weak keys. */
  38702. for (i = 0; i < sizeof(semiWeakKey) / sizeof(*semiWeakKey); i++) {
  38703. ExpectIntEQ(DES_set_key_checked(&semiWeakKey[i], &key), -2);
  38704. }
  38705. /* check DES_key_sched API */
  38706. XMEMSET(key, 1, sizeof(DES_key_schedule));
  38707. ExpectIntEQ(DES_key_sched(&myDes, NULL), 0);
  38708. ExpectIntEQ(DES_key_sched(NULL, &key), 0);
  38709. ExpectIntEQ(DES_key_sched(&myDes, &key), 0);
  38710. /* compare arrays, should be the same */
  38711. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  38712. ExpectIntEQ(key[i], myDes[i]);
  38713. }
  38714. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, NULL)), 0);
  38715. ExpectIntEQ((DES_cbc_cksum(msg, NULL, 0, NULL, NULL)), 0);
  38716. ExpectIntEQ((DES_cbc_cksum(NULL, &key, 0, NULL, NULL)), 0);
  38717. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, &myDes, NULL)), 0);
  38718. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, &iv)), 0);
  38719. ExpectIntEQ((DES_cbc_cksum(NULL, &key, sizeof(msg), &myDes, &iv)), 0);
  38720. ExpectIntEQ((DES_cbc_cksum(msg, NULL, sizeof(msg), &myDes, &iv)), 0);
  38721. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), NULL, &iv)), 0);
  38722. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, NULL)), 0);
  38723. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  38724. * DES_cbc_encrypt on the input */
  38725. XMEMSET(iv, 0, sizeof(DES_cblock));
  38726. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  38727. ExpectIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  38728. ExpectIntEQ(dl, 480052723);
  38729. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  38730. return EXPECT_RESULT();
  38731. }
  38732. static int test_wolfSSL_DES_ncbc(void)
  38733. {
  38734. EXPECT_DECLS;
  38735. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  38736. const_DES_cblock myDes;
  38737. DES_cblock iv = {1};
  38738. DES_key_schedule key = {0};
  38739. unsigned char msg[] = "hello wolfssl";
  38740. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  38741. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  38742. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  38743. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  38744. /* partial block test */
  38745. DES_set_key(&key, &myDes);
  38746. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  38747. ExpectIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  38748. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  38749. DES_set_key(&key, &myDes);
  38750. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38751. *((byte*)&iv) = 1;
  38752. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  38753. ExpectIntEQ(XMEMCMP(msg, pln, 3), 0);
  38754. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  38755. /* full block test */
  38756. DES_set_key(&key, &myDes);
  38757. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  38758. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38759. *((byte*)&iv) = 1;
  38760. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  38761. ExpectIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  38762. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  38763. DES_set_key(&key, &myDes);
  38764. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38765. *((byte*)&iv) = 1;
  38766. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  38767. ExpectIntEQ(XMEMCMP(msg, pln, 8), 0);
  38768. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  38769. #endif
  38770. return EXPECT_RESULT();
  38771. }
  38772. static int test_wolfSSL_DES_ecb_encrypt(void)
  38773. {
  38774. EXPECT_DECLS;
  38775. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  38776. WOLFSSL_DES_cblock input1, input2, output1, output2, back1, back2;
  38777. WOLFSSL_DES_key_schedule key;
  38778. XMEMCPY(key, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  38779. XMEMCPY(input1, "Iamhuman", sizeof(WOLFSSL_DES_cblock));
  38780. XMEMCPY(input2, "Whoisit?", sizeof(WOLFSSL_DES_cblock));
  38781. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  38782. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  38783. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  38784. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  38785. wolfSSL_DES_ecb_encrypt(NULL, NULL, NULL, DES_ENCRYPT);
  38786. wolfSSL_DES_ecb_encrypt(&input1, NULL, NULL, DES_ENCRYPT);
  38787. wolfSSL_DES_ecb_encrypt(NULL, &output1, NULL, DES_ENCRYPT);
  38788. wolfSSL_DES_ecb_encrypt(NULL, NULL, &key, DES_ENCRYPT);
  38789. wolfSSL_DES_ecb_encrypt(&input1, &output1, NULL, DES_ENCRYPT);
  38790. wolfSSL_DES_ecb_encrypt(&input1, NULL, &key, DES_ENCRYPT);
  38791. wolfSSL_DES_ecb_encrypt(NULL, &output1, &key, DES_ENCRYPT);
  38792. /* Encrypt messages */
  38793. wolfSSL_DES_ecb_encrypt(&input1, &output1, &key, DES_ENCRYPT);
  38794. wolfSSL_DES_ecb_encrypt(&input2, &output2, &key, DES_ENCRYPT);
  38795. {
  38796. /* Decrypt messages */
  38797. int ret1 = 0;
  38798. int ret2 = 0;
  38799. wolfSSL_DES_ecb_encrypt(&output1, &back1, &key, DES_DECRYPT);
  38800. ExpectIntEQ(ret1 = XMEMCMP((unsigned char *)back1,
  38801. (unsigned char *)input1, sizeof(WOLFSSL_DES_cblock)), 0);
  38802. wolfSSL_DES_ecb_encrypt(&output2, &back2, &key, DES_DECRYPT);
  38803. ExpectIntEQ(ret2 = XMEMCMP((unsigned char *)back2,
  38804. (unsigned char *)input2, sizeof(WOLFSSL_DES_cblock)), 0);
  38805. }
  38806. #endif
  38807. return EXPECT_RESULT();
  38808. }
  38809. static int test_wolfSSL_DES_ede3_cbc_encrypt(void)
  38810. {
  38811. EXPECT_DECLS;
  38812. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  38813. unsigned char input1[8], input2[8];
  38814. unsigned char output1[8], output2[8];
  38815. unsigned char back1[8], back2[8];
  38816. WOLFSSL_DES_cblock iv1, iv2;
  38817. WOLFSSL_DES_key_schedule key1, key2, key3;
  38818. int i;
  38819. XMEMCPY(key1, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  38820. XMEMCPY(key2, "23456781", sizeof(WOLFSSL_DES_key_schedule));
  38821. XMEMCPY(key3, "34567823", sizeof(WOLFSSL_DES_key_schedule));
  38822. XMEMCPY(input1, "Iamhuman", sizeof(input1));
  38823. XMEMCPY(input2, "Whoisit?", sizeof(input2));
  38824. XMEMSET(output1, 0, sizeof(output1));
  38825. XMEMSET(output2, 0, sizeof(output2));
  38826. XMEMSET(back1, 0, sizeof(back1));
  38827. XMEMSET(back2, 0, sizeof(back2));
  38828. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  38829. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  38830. /* Encrypt messages */
  38831. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, 8, &key1, &key2, &key3, &iv1,
  38832. DES_ENCRYPT);
  38833. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, 8, &key1, &key2, &key3, &iv2,
  38834. DES_ENCRYPT);
  38835. {
  38836. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  38837. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  38838. /* Decrypt messages */
  38839. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, 8, &key1, &key2, &key3,
  38840. &iv1, DES_DECRYPT);
  38841. ExpectIntEQ(XMEMCMP(back1, input1, sizeof(input1)), 0);
  38842. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, 8, &key1, &key2, &key3,
  38843. &iv2, DES_DECRYPT);
  38844. ExpectIntEQ(XMEMCMP(back2, input2, sizeof(input2)), 0);
  38845. }
  38846. for (i = 0; i < 8; i++) {
  38847. XMEMSET(output1, 0, sizeof(output1));
  38848. XMEMSET(output2, 0, sizeof(output2));
  38849. XMEMSET(back1, 0, sizeof(back1));
  38850. XMEMSET(back2, 0, sizeof(back2));
  38851. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  38852. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  38853. /* Encrypt partial messages */
  38854. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, i, &key1, &key2, &key3,
  38855. &iv1, DES_ENCRYPT);
  38856. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, i, &key1, &key2, &key3,
  38857. &iv2, DES_ENCRYPT);
  38858. {
  38859. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  38860. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  38861. /* Decrypt messages */
  38862. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, i, &key1, &key2,
  38863. &key3, &iv1, DES_DECRYPT);
  38864. ExpectIntEQ(XMEMCMP(back1, input1, i), 0);
  38865. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, i, &key1, &key2,
  38866. &key3, &iv2, DES_DECRYPT);
  38867. ExpectIntEQ(XMEMCMP(back2, input2, i), 0);
  38868. }
  38869. }
  38870. #endif
  38871. return EXPECT_RESULT();
  38872. }
  38873. static int test_wolfSSL_AES_encrypt(void)
  38874. {
  38875. EXPECT_DECLS;
  38876. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  38877. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  38878. AES_KEY enc;
  38879. AES_KEY dec;
  38880. const byte msg[] = {
  38881. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  38882. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  38883. };
  38884. const byte exp[] = {
  38885. 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c,
  38886. 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8,
  38887. };
  38888. const byte key[] = {
  38889. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  38890. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  38891. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  38892. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  38893. };
  38894. byte eout[sizeof(msg)];
  38895. byte dout[sizeof(msg)];
  38896. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &enc), 0);
  38897. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &dec), 0);
  38898. wolfSSL_AES_encrypt(NULL, NULL, NULL);
  38899. wolfSSL_AES_encrypt(msg, NULL, NULL);
  38900. wolfSSL_AES_encrypt(NULL, eout, NULL);
  38901. wolfSSL_AES_encrypt(NULL, NULL, &enc);
  38902. wolfSSL_AES_encrypt(msg, eout, NULL);
  38903. wolfSSL_AES_encrypt(msg, NULL, &enc);
  38904. wolfSSL_AES_encrypt(NULL, eout, &enc);
  38905. wolfSSL_AES_decrypt(NULL, NULL, NULL);
  38906. wolfSSL_AES_decrypt(eout, NULL, NULL);
  38907. wolfSSL_AES_decrypt(NULL, dout, NULL);
  38908. wolfSSL_AES_decrypt(NULL, NULL, &dec);
  38909. wolfSSL_AES_decrypt(eout, dout, NULL);
  38910. wolfSSL_AES_decrypt(eout, NULL, &dec);
  38911. wolfSSL_AES_decrypt(NULL, dout, &dec);
  38912. wolfSSL_AES_encrypt(msg, eout, &enc);
  38913. ExpectIntEQ(XMEMCMP(eout, exp, AES_BLOCK_SIZE), 0);
  38914. wolfSSL_AES_decrypt(eout, dout, &dec);
  38915. ExpectIntEQ(XMEMCMP(dout, msg, AES_BLOCK_SIZE), 0);
  38916. #endif
  38917. return EXPECT_RESULT();
  38918. }
  38919. static int test_wolfSSL_AES_ecb_encrypt(void)
  38920. {
  38921. EXPECT_DECLS;
  38922. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  38923. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  38924. AES_KEY aes;
  38925. const byte msg[] =
  38926. {
  38927. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  38928. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  38929. };
  38930. const byte verify[] =
  38931. {
  38932. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  38933. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  38934. };
  38935. const byte key[] =
  38936. {
  38937. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  38938. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  38939. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  38940. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  38941. };
  38942. byte out[AES_BLOCK_SIZE];
  38943. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  38944. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38945. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  38946. ExpectIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  38947. #ifdef HAVE_AES_DECRYPT
  38948. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  38949. XMEMSET(out, 0, AES_BLOCK_SIZE);
  38950. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  38951. ExpectIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  38952. #endif
  38953. /* test bad arguments */
  38954. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  38955. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  38956. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  38957. #endif
  38958. return EXPECT_RESULT();
  38959. }
  38960. static int test_wolfSSL_AES_cbc_encrypt(void)
  38961. {
  38962. EXPECT_DECLS;
  38963. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  38964. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  38965. AES_KEY aes;
  38966. AES_KEY* aesN = NULL;
  38967. size_t len = 0;
  38968. size_t lenB = 0;
  38969. int keySz0 = 0;
  38970. int keySzN = -1;
  38971. byte out[AES_BLOCK_SIZE] = {0};
  38972. byte* outN = NULL;
  38973. /* Test vectors retrieved from:
  38974. * <begin URL>
  38975. * https://csrc.nist.gov/
  38976. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  38977. * documents/aes/KAT_AES.zip
  38978. * </end URL>
  38979. */
  38980. const byte* pt128N = NULL;
  38981. byte* key128N = NULL;
  38982. byte* iv128N = NULL;
  38983. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  38984. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38985. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  38986. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  38987. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  38988. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  38989. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  38990. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  38991. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  38992. len = sizeof(pt128);
  38993. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  38994. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  38995. ExpectIntNE(XMEMCMP(b, g, h), i)
  38996. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  38997. /* Stressing wolfSSL_AES_cbc_encrypt() */
  38998. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  38999. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  39000. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  39001. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  39002. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  39003. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  39004. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  39005. /* Stressing wolfSSL_AES_set_encrypt_key */
  39006. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  39007. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  39008. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  39009. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  39010. /* Stressing wolfSSL_AES_set_decrypt_key */
  39011. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  39012. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  39013. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  39014. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  39015. #ifdef WOLFSSL_AES_128
  39016. /* wolfSSL_AES_cbc_encrypt() 128-bit */
  39017. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39018. RESET_IV(iv128tmp, iv128);
  39019. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  39020. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  39021. ExpectIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  39022. wc_AesFree((Aes*)&aes);
  39023. #ifdef HAVE_AES_DECRYPT
  39024. /* wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode */
  39025. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39026. RESET_IV(iv128tmp, iv128);
  39027. len = sizeof(ct128);
  39028. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  39029. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  39030. ExpectIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  39031. wc_AesFree((Aes*)&aes);
  39032. #endif
  39033. #endif /* WOLFSSL_AES_128 */
  39034. #ifdef WOLFSSL_AES_192
  39035. {
  39036. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  39037. * Appendix F.2.3 */
  39038. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  39039. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  39040. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  39041. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  39042. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  39043. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  39044. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  39045. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  39046. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  39047. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  39048. len = sizeof(pt192);
  39049. /* wolfSSL_AES_cbc_encrypt() 192-bit */
  39050. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39051. RESET_IV(iv192tmp, iv192);
  39052. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  39053. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  39054. ExpectIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  39055. wc_AesFree((Aes*)&aes);
  39056. #ifdef HAVE_AES_DECRYPT
  39057. /* wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode */
  39058. len = sizeof(ct192);
  39059. RESET_IV(iv192tmp, iv192);
  39060. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39061. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  39062. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  39063. ExpectIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  39064. wc_AesFree((Aes*)&aes);
  39065. #endif
  39066. }
  39067. #endif /* WOLFSSL_AES_192 */
  39068. #ifdef WOLFSSL_AES_256
  39069. {
  39070. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  39071. * Appendix F.2.5 */
  39072. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  39073. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  39074. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  39075. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  39076. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  39077. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  39078. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  39079. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  39080. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  39081. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  39082. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  39083. len = sizeof(pt256);
  39084. /* wolfSSL_AES_cbc_encrypt() 256-bit */
  39085. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39086. RESET_IV(iv256tmp, iv256);
  39087. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39088. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  39089. ExpectIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  39090. wc_AesFree((Aes*)&aes);
  39091. #ifdef HAVE_AES_DECRYPT
  39092. /* wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode */
  39093. len = sizeof(ct256);
  39094. RESET_IV(iv256tmp, iv256);
  39095. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39096. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39097. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  39098. ExpectIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  39099. wc_AesFree((Aes*)&aes);
  39100. #endif
  39101. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && \
  39102. !defined(HAVE_SELFTEST)
  39103. {
  39104. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  39105. byte wrapPlain[sizeof(key256)] = { 0 };
  39106. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  39107. /* wolfSSL_AES_wrap_key() 256-bit NULL iv */
  39108. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39109. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  39110. 15), WOLFSSL_FAILURE);
  39111. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  39112. sizeof(key256)), sizeof(wrapCipher));
  39113. wc_AesFree((Aes*)&aes);
  39114. /* wolfSSL_AES_unwrap_key() 256-bit NULL iv */
  39115. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39116. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  39117. 23), WOLFSSL_FAILURE);
  39118. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  39119. sizeof(wrapCipher)), sizeof(wrapPlain));
  39120. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  39121. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  39122. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  39123. wc_AesFree((Aes*)&aes);
  39124. /* wolfSSL_AES_wrap_key() 256-bit custom iv */
  39125. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39126. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  39127. sizeof(key256)), sizeof(wrapCipher));
  39128. wc_AesFree((Aes*)&aes);
  39129. /* wolfSSL_AES_unwrap_key() 256-bit custom iv */
  39130. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39131. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  39132. sizeof(wrapCipher)), sizeof(wrapPlain));
  39133. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  39134. wc_AesFree((Aes*)&aes);
  39135. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, NULL, 0), 0);
  39136. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, NULL, NULL, 0), 0);
  39137. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  39138. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, wrapCipher, NULL, 0), 0);
  39139. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, key256, 0), 0);
  39140. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, wrapCipher, key256, 0), 0);
  39141. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256, 0), 0);
  39142. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, NULL, key256, 0), 0);
  39143. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, NULL, 0), 0);
  39144. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, NULL, 0), 0);
  39145. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, NULL, NULL, 0), 0);
  39146. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  39147. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, wrapPlain, NULL, 0), 0);
  39148. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, wrapCipher, 0), 0);
  39149. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, wrapPlain, wrapCipher, 0),
  39150. 0);
  39151. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher, 0),
  39152. 0);
  39153. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, NULL, wrapCipher, 0), 0);
  39154. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapPlain, NULL, 0), 0);
  39155. }
  39156. #endif /* HAVE_AES_KEYWRAP */
  39157. }
  39158. #endif /* WOLFSSL_AES_256 */
  39159. #endif
  39160. return EXPECT_RESULT();
  39161. }
  39162. static int test_wolfSSL_AES_cfb128_encrypt(void)
  39163. {
  39164. EXPECT_DECLS;
  39165. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(WOLFSSL_AES_CFB) && \
  39166. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  39167. AES_KEY aesEnc;
  39168. AES_KEY aesDec;
  39169. const byte msg[] = {
  39170. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  39171. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  39172. };
  39173. const byte exp[] = {
  39174. 0x16, 0xc9, 0x90, 0x6c, 0x04, 0x0c, 0xd1, 0x2f,
  39175. 0x84, 0x7b, 0x18, 0xed, 0xed, 0x6a, 0xb5, 0xfd
  39176. };
  39177. const byte key[] = {
  39178. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  39179. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  39180. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  39181. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  39182. };
  39183. const byte ivData[] = {
  39184. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  39185. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  39186. };
  39187. byte out[AES_BLOCK_SIZE];
  39188. byte iv[AES_BLOCK_SIZE];
  39189. word32 i;
  39190. int num;
  39191. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  39192. XMEMCPY(iv, ivData, sizeof(iv));
  39193. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39194. AES_cfb128_encrypt(msg, out, sizeof(msg), &aesEnc, iv, NULL, AES_ENCRYPT);
  39195. ExpectIntEQ(XMEMCMP(out, exp, sizeof(msg)), 0);
  39196. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39197. #ifdef HAVE_AES_DECRYPT
  39198. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  39199. XMEMCPY(iv, ivData, sizeof(iv));
  39200. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39201. AES_cfb128_encrypt(exp, out, sizeof(msg), &aesDec, iv, NULL, AES_DECRYPT);
  39202. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  39203. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39204. #endif
  39205. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(msg)); i++) {
  39206. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  39207. XMEMCPY(iv, ivData, sizeof(iv));
  39208. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39209. AES_cfb128_encrypt(msg, out, i, &aesEnc, iv, &num, AES_ENCRYPT);
  39210. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  39211. ExpectIntEQ(XMEMCMP(out, exp, i), 0);
  39212. if (i == 0) {
  39213. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39214. }
  39215. else {
  39216. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39217. }
  39218. #ifdef HAVE_AES_DECRYPT
  39219. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  39220. XMEMCPY(iv, ivData, sizeof(iv));
  39221. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39222. AES_cfb128_encrypt(exp, out, i, &aesDec, iv, &num, AES_DECRYPT);
  39223. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  39224. ExpectIntEQ(XMEMCMP(out, msg, i), 0);
  39225. if (i == 0) {
  39226. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39227. }
  39228. else {
  39229. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39230. }
  39231. #endif
  39232. }
  39233. if (EXPECT_SUCCESS()) {
  39234. /* test bad arguments */
  39235. AES_cfb128_encrypt(NULL, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  39236. AES_cfb128_encrypt(msg, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  39237. AES_cfb128_encrypt(NULL, out, 0, NULL, NULL, NULL, AES_DECRYPT);
  39238. AES_cfb128_encrypt(NULL, NULL, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  39239. AES_cfb128_encrypt(NULL, NULL, 0, NULL, iv, NULL, AES_DECRYPT);
  39240. AES_cfb128_encrypt(NULL, out, 0, &aesDec, iv, NULL, AES_DECRYPT);
  39241. AES_cfb128_encrypt(msg, NULL, 0, &aesDec, iv, NULL, AES_DECRYPT);
  39242. AES_cfb128_encrypt(msg, out, 0, NULL, iv, NULL, AES_DECRYPT);
  39243. AES_cfb128_encrypt(msg, out, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  39244. }
  39245. #endif
  39246. return EXPECT_RESULT();
  39247. }
  39248. static int test_wolfSSL_CRYPTO_cts128(void)
  39249. {
  39250. EXPECT_DECLS;
  39251. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  39252. defined(HAVE_CTS) && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  39253. byte tmp[64]; /* Largest vector size */
  39254. /* Test vectors taken form RFC3962 Appendix B */
  39255. const testVector vects[] = {
  39256. {
  39257. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39258. "\x20",
  39259. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  39260. "\x97",
  39261. 17, 17
  39262. },
  39263. {
  39264. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39265. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  39266. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  39267. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  39268. 31, 31
  39269. },
  39270. {
  39271. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39272. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  39273. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  39274. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  39275. 32, 32
  39276. },
  39277. {
  39278. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39279. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  39280. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  39281. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  39282. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  39283. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  39284. 47, 47
  39285. },
  39286. {
  39287. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39288. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  39289. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  39290. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  39291. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  39292. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  39293. 48, 48
  39294. },
  39295. {
  39296. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39297. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  39298. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  39299. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  39300. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  39301. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  39302. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  39303. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  39304. 64, 64
  39305. }
  39306. };
  39307. byte keyBytes[AES_128_KEY_SIZE] = {
  39308. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  39309. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  39310. };
  39311. size_t i;
  39312. AES_KEY encKey;
  39313. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  39314. XMEMSET(tmp, 0, sizeof(tmp));
  39315. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  39316. AES_KEY decKey;
  39317. ExpectIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  39318. &encKey), 0);
  39319. ExpectIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  39320. &decKey), 0);
  39321. XMEMSET(iv, 0, sizeof(iv));
  39322. ExpectIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  39323. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  39324. vects[i].outLen);
  39325. ExpectIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  39326. XMEMSET(iv, 0, sizeof(iv));
  39327. ExpectIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  39328. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  39329. vects[i].inLen);
  39330. ExpectIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  39331. }
  39332. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  39333. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  39334. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  39335. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  39336. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  39337. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL,
  39338. (cbc128_f)AES_cbc_encrypt), 0);
  39339. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, &encKey, iv,
  39340. (cbc128_f)AES_cbc_encrypt), 0);
  39341. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, &encKey, iv,
  39342. (cbc128_f)AES_cbc_encrypt), 0);
  39343. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, NULL, iv,
  39344. (cbc128_f)AES_cbc_encrypt), 0);
  39345. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, NULL,
  39346. (cbc128_f)AES_cbc_encrypt), 0);
  39347. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  39348. /* Length too small. */
  39349. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 0, &encKey, iv,
  39350. (cbc128_f)AES_cbc_encrypt), 0);
  39351. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  39352. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  39353. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  39354. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  39355. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  39356. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL,
  39357. (cbc128_f)AES_cbc_encrypt), 0);
  39358. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, &encKey, iv,
  39359. (cbc128_f)AES_cbc_encrypt), 0);
  39360. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, &encKey, iv,
  39361. (cbc128_f)AES_cbc_encrypt), 0);
  39362. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, NULL, iv,
  39363. (cbc128_f)AES_cbc_encrypt), 0);
  39364. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, NULL,
  39365. (cbc128_f)AES_cbc_encrypt), 0);
  39366. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  39367. /* Length too small. */
  39368. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 0, &encKey, iv,
  39369. (cbc128_f)AES_cbc_encrypt), 0);
  39370. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  39371. return EXPECT_RESULT();
  39372. }
  39373. static int test_wolfSSL_RC4(void)
  39374. {
  39375. EXPECT_DECLS;
  39376. #if !defined(NO_RC4) && defined(OPENSSL_EXTRA)
  39377. WOLFSSL_RC4_KEY rc4Key;
  39378. unsigned char key[] = {
  39379. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  39380. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  39381. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  39382. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  39383. };
  39384. unsigned char data[] = {
  39385. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  39386. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  39387. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  39388. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  39389. };
  39390. unsigned char enc[sizeof(data)];
  39391. unsigned char dec[sizeof(data)];
  39392. word32 i;
  39393. word32 j;
  39394. wolfSSL_RC4_set_key(NULL, -1, NULL);
  39395. wolfSSL_RC4_set_key(&rc4Key, -1, NULL);
  39396. wolfSSL_RC4_set_key(NULL, 0, NULL);
  39397. wolfSSL_RC4_set_key(NULL, -1, key);
  39398. wolfSSL_RC4_set_key(&rc4Key, 0, NULL);
  39399. wolfSSL_RC4_set_key(&rc4Key, -1, key);
  39400. wolfSSL_RC4_set_key(NULL, 0, key);
  39401. wolfSSL_RC4(NULL, 0, NULL, NULL);
  39402. wolfSSL_RC4(&rc4Key, 0, NULL, NULL);
  39403. wolfSSL_RC4(NULL, 0, data, NULL);
  39404. wolfSSL_RC4(NULL, 0, NULL, enc);
  39405. wolfSSL_RC4(&rc4Key, 0, data, NULL);
  39406. wolfSSL_RC4(&rc4Key, 0, NULL, enc);
  39407. wolfSSL_RC4(NULL, 0, data, enc);
  39408. ExpectIntEQ(1, 1);
  39409. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(key)); i++) {
  39410. for (j = 0; EXPECT_SUCCESS() && (j <= sizeof(data)); j++) {
  39411. XMEMSET(enc, 0, sizeof(enc));
  39412. XMEMSET(dec, 0, sizeof(dec));
  39413. /* Encrypt */
  39414. wolfSSL_RC4_set_key(&rc4Key, i, key);
  39415. wolfSSL_RC4(&rc4Key, j, data, enc);
  39416. /* Decrypt */
  39417. wolfSSL_RC4_set_key(&rc4Key, i, key);
  39418. wolfSSL_RC4(&rc4Key, j, enc, dec);
  39419. ExpectIntEQ(XMEMCMP(dec, data, j), 0);
  39420. }
  39421. }
  39422. #endif
  39423. return EXPECT_RESULT();
  39424. }
  39425. static int test_wolfSSL_OBJ(void)
  39426. {
  39427. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  39428. * mode
  39429. */
  39430. EXPECT_DECLS;
  39431. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  39432. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  39433. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && \
  39434. !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  39435. ASN1_OBJECT *obj = NULL;
  39436. ASN1_OBJECT *obj2 = NULL;
  39437. char buf[50];
  39438. XFILE fp = XBADFILE;
  39439. X509 *x509 = NULL;
  39440. X509_NAME *x509Name = NULL;
  39441. X509_NAME_ENTRY *x509NameEntry = NULL;
  39442. ASN1_OBJECT *asn1Name = NULL;
  39443. int numNames = 0;
  39444. BIO *bio = NULL;
  39445. int nid;
  39446. int i, j;
  39447. const char *f[] = {
  39448. #ifndef NO_RSA
  39449. "./certs/ca-cert.der",
  39450. #endif
  39451. #ifdef HAVE_ECC
  39452. "./certs/ca-ecc-cert.der",
  39453. "./certs/ca-ecc384-cert.der",
  39454. #endif
  39455. NULL};
  39456. ASN1_OBJECT *field_name_obj = NULL;
  39457. int lastpos = -1;
  39458. int tmp = -1;
  39459. ASN1_STRING *asn1 = NULL;
  39460. unsigned char *buf_dyn = NULL;
  39461. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  39462. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  39463. ExpectIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  39464. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  39465. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  39466. ASN1_OBJECT_free(obj);
  39467. obj = NULL;
  39468. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  39469. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256);
  39470. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  39471. #ifdef WOLFSSL_CERT_EXT
  39472. ExpectIntEQ(OBJ_txt2nid(buf), NID_sha256);
  39473. #endif
  39474. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  39475. ExpectNotNull(obj2 = OBJ_dup(obj));
  39476. ExpectIntEQ(OBJ_cmp(obj, obj2), 0);
  39477. ASN1_OBJECT_free(obj);
  39478. obj = NULL;
  39479. ASN1_OBJECT_free(obj2);
  39480. obj2 = NULL;
  39481. for (i = 0; f[i] != NULL; i++)
  39482. {
  39483. ExpectTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  39484. ExpectNotNull(x509 = d2i_X509_fp(fp, NULL));
  39485. if (fp != XBADFILE) {
  39486. XFCLOSE(fp);
  39487. fp = XBADFILE;
  39488. }
  39489. ExpectNotNull(x509Name = X509_get_issuer_name(x509));
  39490. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  39491. /* Get the Common Name by using OBJ_txt2obj */
  39492. ExpectNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  39493. do
  39494. {
  39495. lastpos = tmp;
  39496. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  39497. } while (tmp > -1);
  39498. ExpectIntNE(lastpos, -1);
  39499. ASN1_OBJECT_free(field_name_obj);
  39500. field_name_obj = NULL;
  39501. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  39502. ExpectNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  39503. ExpectIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  39504. /*
  39505. * All Common Names should be www.wolfssl.com
  39506. * This makes testing easier as we can test for the expected value.
  39507. */
  39508. ExpectStrEQ((char*)buf_dyn, "www.wolfssl.com");
  39509. OPENSSL_free(buf_dyn);
  39510. buf_dyn = NULL;
  39511. bio = BIO_new(BIO_s_mem());
  39512. ExpectTrue(bio != NULL);
  39513. for (j = 0; j < numNames; j++)
  39514. {
  39515. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  39516. ExpectNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  39517. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  39518. }
  39519. BIO_free(bio);
  39520. bio = NULL;
  39521. X509_free(x509);
  39522. x509 = NULL;
  39523. }
  39524. #ifdef HAVE_PKCS12
  39525. {
  39526. PKCS12 *p12 = NULL;
  39527. int boolRet;
  39528. EVP_PKEY *pkey = NULL;
  39529. const char *p12_f[] = {
  39530. #if !defined(NO_DES3) && !defined(NO_RSA)
  39531. "./certs/test-servercert.p12",
  39532. #endif
  39533. NULL};
  39534. for (i = 0; p12_f[i] != NULL; i++)
  39535. {
  39536. ExpectTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  39537. ExpectNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  39538. if (fp != XBADFILE) {
  39539. XFCLOSE(fp);
  39540. fp = XBADFILE;
  39541. }
  39542. ExpectTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  39543. &pkey, &x509, NULL)) > 0);
  39544. wc_PKCS12_free(p12);
  39545. p12 = NULL;
  39546. EVP_PKEY_free(pkey);
  39547. x509Name = X509_get_issuer_name(x509);
  39548. ExpectNotNull(x509Name);
  39549. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  39550. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  39551. for (j = 0; j < numNames; j++)
  39552. {
  39553. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  39554. ExpectNotNull(asn1Name =
  39555. X509_NAME_ENTRY_get_object(x509NameEntry));
  39556. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  39557. }
  39558. BIO_free(bio);
  39559. bio = NULL;
  39560. X509_free(x509);
  39561. x509 = NULL;
  39562. }
  39563. }
  39564. #endif /* HAVE_PKCS12 */
  39565. #endif
  39566. return EXPECT_RESULT();
  39567. }
  39568. static int test_wolfSSL_OBJ_cmp(void)
  39569. {
  39570. EXPECT_DECLS;
  39571. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  39572. ASN1_OBJECT *obj = NULL;
  39573. ASN1_OBJECT *obj2 = NULL;
  39574. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  39575. ExpectNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  39576. ExpectIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  39577. ExpectIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  39578. ExpectIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  39579. ExpectIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  39580. ExpectIntEQ(OBJ_cmp(obj, obj), 0);
  39581. ExpectIntEQ(OBJ_cmp(obj2, obj2), 0);
  39582. ASN1_OBJECT_free(obj);
  39583. ASN1_OBJECT_free(obj2);
  39584. #endif
  39585. return EXPECT_RESULT();
  39586. }
  39587. static int test_wolfSSL_OBJ_txt2nid(void)
  39588. {
  39589. EXPECT_DECLS;
  39590. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  39591. defined(WOLFSSL_APACHE_HTTPD)
  39592. int i;
  39593. static const struct {
  39594. const char* sn;
  39595. const char* ln;
  39596. const char* oid;
  39597. int nid;
  39598. } testVals[] = {
  39599. #ifdef WOLFSSL_APACHE_HTTPD
  39600. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  39601. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  39602. NID_id_on_dnsSRV },
  39603. { "msUPN", "Microsoft User Principal Name",
  39604. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  39605. #endif
  39606. { NULL, NULL, NULL, NID_undef }
  39607. };
  39608. /* Invalid cases */
  39609. ExpectIntEQ(OBJ_txt2nid(NULL), NID_undef);
  39610. ExpectIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  39611. /* Valid cases */
  39612. for (i = 0; testVals[i].sn != NULL; i++) {
  39613. ExpectIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  39614. ExpectIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  39615. ExpectIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  39616. }
  39617. #endif
  39618. return EXPECT_RESULT();
  39619. }
  39620. static int test_wolfSSL_OBJ_txt2obj(void)
  39621. {
  39622. EXPECT_DECLS;
  39623. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  39624. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  39625. int i;
  39626. char buf[50];
  39627. ASN1_OBJECT* obj = NULL;
  39628. static const struct {
  39629. const char* oidStr;
  39630. const char* sn;
  39631. const char* ln;
  39632. } objs_list[] = {
  39633. #if defined(WOLFSSL_APACHE_HTTPD)
  39634. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  39635. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  39636. #endif
  39637. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  39638. { NULL, NULL, NULL }
  39639. };
  39640. static const struct {
  39641. const char* numeric;
  39642. const char* name;
  39643. } objs_named[] = {
  39644. /* In dictionary but not in normal list. */
  39645. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  39646. /* Made up OID. */
  39647. { "1.3.5.7", "1.3.5.7" },
  39648. { NULL, NULL }
  39649. };
  39650. ExpectNull(obj = OBJ_txt2obj("Bad name", 0));
  39651. ASN1_OBJECT_free(obj);
  39652. obj = NULL;
  39653. ExpectNull(obj = OBJ_txt2obj(NULL, 0));
  39654. ASN1_OBJECT_free(obj);
  39655. obj = NULL;
  39656. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  39657. /* Test numerical value of oid (oidStr) */
  39658. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  39659. /* Convert object back to text to confirm oid is correct */
  39660. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  39661. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  39662. ASN1_OBJECT_free(obj);
  39663. obj = NULL;
  39664. XMEMSET(buf, 0, sizeof(buf));
  39665. /* Test short name (sn) */
  39666. ExpectNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  39667. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  39668. /* Convert object back to text to confirm oid is correct */
  39669. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  39670. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  39671. ASN1_OBJECT_free(obj);
  39672. obj = NULL;
  39673. XMEMSET(buf, 0, sizeof(buf));
  39674. /* Test long name (ln) - should fail when no_name = 1 */
  39675. ExpectNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  39676. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  39677. /* Convert object back to text to confirm oid is correct */
  39678. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  39679. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  39680. ASN1_OBJECT_free(obj);
  39681. obj = NULL;
  39682. XMEMSET(buf, 0, sizeof(buf));
  39683. }
  39684. for (i = 0; objs_named[i].numeric != NULL; i++) {
  39685. ExpectNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  39686. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  39687. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  39688. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  39689. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  39690. ASN1_OBJECT_free(obj);
  39691. obj = NULL;
  39692. }
  39693. #endif
  39694. return EXPECT_RESULT();
  39695. }
  39696. static int test_wolfSSL_PEM_write_bio_X509(void)
  39697. {
  39698. EXPECT_DECLS;
  39699. #if defined(OPENSSL_EXTRA) && defined(OPENSSL_ALL) && \
  39700. defined(WOLFSSL_AKID_NAME) && defined(WOLFSSL_CERT_EXT) && \
  39701. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && !defined(NO_RSA) && \
  39702. !defined(NO_FILESYSTEM)
  39703. /* This test contains the hard coded expected
  39704. * lengths. Update if necessary */
  39705. XFILE fp = XBADFILE;
  39706. WOLFSSL_EVP_PKEY *priv = NULL;
  39707. BIO* input = NULL;
  39708. BIO* output = NULL;
  39709. X509* x509a = NULL;
  39710. X509* x509b = NULL;
  39711. ASN1_TIME* notBeforeA = NULL;
  39712. ASN1_TIME* notAfterA = NULL;
  39713. #ifndef NO_ASN_TIME
  39714. ASN1_TIME* notBeforeB = NULL;
  39715. ASN1_TIME* notAfterB = NULL;
  39716. #endif
  39717. int expectedLen;
  39718. ExpectTrue((fp = XFOPEN("certs/server-key.pem", "rb")) != XBADFILE);
  39719. ExpectNotNull(priv = wolfSSL_PEM_read_PrivateKey(fp, NULL, NULL, NULL));
  39720. if (fp != XBADFILE) {
  39721. XFCLOSE(fp);
  39722. fp = XBADFILE;
  39723. }
  39724. ExpectNotNull(input = BIO_new_file("certs/test/cert-ext-multiple.pem",
  39725. "rb"));
  39726. ExpectIntEQ(wolfSSL_BIO_get_len(input), 2000);
  39727. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  39728. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  39729. ExpectNotNull(notBeforeA = X509_get_notBefore(x509a));
  39730. ExpectNotNull(notAfterA = X509_get_notAfter(x509a));
  39731. /* write X509 back to PEM BIO; no need to sign as nothing changed. */
  39732. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  39733. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  39734. /* compare length against expected */
  39735. expectedLen = 2000;
  39736. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  39737. #ifndef NO_ASN_TIME
  39738. /* read exported X509 PEM back into struct, sanity check on export,
  39739. * make sure notBefore/notAfter are the same and certs are identical. */
  39740. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  39741. ExpectNotNull(notBeforeB = X509_get_notBefore(x509b));
  39742. ExpectNotNull(notAfterB = X509_get_notAfter(x509b));
  39743. ExpectIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  39744. ExpectIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  39745. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  39746. X509_free(x509b);
  39747. x509b = NULL;
  39748. #endif
  39749. /* Reset output buffer */
  39750. BIO_free(output);
  39751. output = NULL;
  39752. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  39753. /* Test forcing the AKID to be generated just from KeyIdentifier */
  39754. if (EXPECT_SUCCESS() && x509a->authKeyIdSrc != NULL) {
  39755. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  39756. x509a->authKeyId = x509a->authKeyIdSrc;
  39757. x509a->authKeyIdSrc = NULL;
  39758. x509a->authKeyIdSrcSz = 0;
  39759. }
  39760. /* Resign to re-generate the der */
  39761. ExpectIntGT(wolfSSL_X509_sign(x509a, priv, EVP_sha256()), 0);
  39762. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  39763. /* Check that we generate a smaller output since the AKID will
  39764. * only contain the KeyIdentifier without any additional
  39765. * information */
  39766. /* Here we copy the validity struct from the original */
  39767. expectedLen = 1688;
  39768. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  39769. /* Reset buffers and x509 */
  39770. BIO_free(input);
  39771. input = NULL;
  39772. BIO_free(output);
  39773. output = NULL;
  39774. X509_free(x509a);
  39775. x509a = NULL;
  39776. /* test CA and basicConstSet values are encoded when
  39777. * the cert is a CA */
  39778. ExpectNotNull(input = BIO_new_file("certs/server-cert.pem", "rb"));
  39779. /* read PEM into X509 struct */
  39780. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  39781. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  39782. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  39783. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  39784. /* read exported X509 PEM back into struct, ensure isCa and basicConstSet
  39785. * values are maintained and certs are identical.*/
  39786. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  39787. ExpectIntEQ(x509b->isCa, 1);
  39788. ExpectIntEQ(x509b->basicConstSet, 1);
  39789. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  39790. X509_free(x509a);
  39791. x509a = NULL;
  39792. X509_free(x509b);
  39793. x509b = NULL;
  39794. BIO_free(input);
  39795. input = NULL;
  39796. BIO_free(output);
  39797. output = NULL;
  39798. /* test CA and basicConstSet values are encoded when
  39799. * the cert is not CA */
  39800. ExpectNotNull(input = BIO_new_file("certs/client-uri-cert.pem", "rb"));
  39801. /* read PEM into X509 struct */
  39802. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  39803. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  39804. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  39805. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  39806. /* read exported X509 PEM back into struct, ensure isCa and
  39807. * basicConstSet values are maintained and certs are identical */
  39808. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  39809. ExpectIntEQ(x509b->isCa, 0);
  39810. ExpectIntEQ(x509b->basicConstSet, 1);
  39811. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  39812. wolfSSL_EVP_PKEY_free(priv);
  39813. X509_free(x509a);
  39814. X509_free(x509b);
  39815. BIO_free(input);
  39816. BIO_free(output);
  39817. #endif
  39818. return EXPECT_RESULT();
  39819. }
  39820. static int test_wolfSSL_X509_NAME_ENTRY(void)
  39821. {
  39822. EXPECT_DECLS;
  39823. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  39824. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  39825. X509* x509 = NULL;
  39826. #ifndef NO_BIO
  39827. BIO* bio = NULL;
  39828. #endif
  39829. X509_NAME* nm = NULL;
  39830. X509_NAME_ENTRY* entry = NULL;
  39831. unsigned char cn[] = "another name to add";
  39832. #ifdef OPENSSL_ALL
  39833. int i;
  39834. int names_len = 0;
  39835. #endif
  39836. ExpectNotNull(x509 =
  39837. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  39838. #ifndef NO_BIO
  39839. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  39840. ExpectIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  39841. #endif
  39842. #ifdef WOLFSSL_CERT_REQ
  39843. {
  39844. X509_REQ* req = NULL;
  39845. #ifndef NO_BIO
  39846. BIO* bReq = NULL;
  39847. #endif
  39848. ExpectNotNull(req =
  39849. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  39850. #ifndef NO_BIO
  39851. ExpectNotNull(bReq = BIO_new(BIO_s_mem()));
  39852. ExpectIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  39853. BIO_free(bReq);
  39854. #endif
  39855. X509_free(req);
  39856. }
  39857. #endif
  39858. ExpectNotNull(nm = X509_get_subject_name(x509));
  39859. /* Test add entry */
  39860. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  39861. 0x0c, cn, (int)sizeof(cn)));
  39862. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  39863. #ifdef WOLFSSL_CERT_EXT
  39864. ExpectIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  39865. (byte*)"support@wolfssl.com", 19, -1,
  39866. 1), WOLFSSL_SUCCESS);
  39867. #endif
  39868. X509_NAME_ENTRY_free(entry);
  39869. entry = NULL;
  39870. #ifdef WOLFSSL_CERT_REQ
  39871. {
  39872. unsigned char srv_pkcs9p[] = "Server";
  39873. unsigned char fvrtDrnk[] = "tequila";
  39874. unsigned char* der = NULL;
  39875. char* subject = NULL;
  39876. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  39877. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  39878. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_favouriteDrink,
  39879. MBSTRING_ASC, fvrtDrnk, -1, -1, 0), SSL_SUCCESS);
  39880. ExpectIntGT(wolfSSL_i2d_X509_NAME(nm, &der), 0);
  39881. ExpectNotNull(der);
  39882. ExpectNotNull(subject = X509_NAME_oneline(nm, 0, 0));
  39883. ExpectNotNull(XSTRSTR(subject, "favouriteDrink=tequila"));
  39884. ExpectNotNull(XSTRSTR(subject, "contentType=Server"));
  39885. #ifdef DEBUG_WOLFSSL
  39886. if (subject != NULL) {
  39887. fprintf(stderr, "\n\t%s\n", subject);
  39888. }
  39889. #endif
  39890. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  39891. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  39892. }
  39893. #endif
  39894. /* Test add entry by text */
  39895. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  39896. 0x0c, cn, (int)sizeof(cn)));
  39897. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  39898. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  39899. ExpectNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  39900. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  39901. #endif
  39902. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  39903. X509_NAME_ENTRY_free(entry);
  39904. entry = NULL;
  39905. /* Test add entry by NID */
  39906. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  39907. cn, -1, -1, 0), SSL_SUCCESS);
  39908. #ifdef OPENSSL_ALL
  39909. /* stack of name entry */
  39910. ExpectIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  39911. for (i = 0; i < names_len; i++) {
  39912. ExpectNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  39913. }
  39914. #endif
  39915. #ifndef NO_BIO
  39916. BIO_free(bio);
  39917. #endif
  39918. X509_free(x509); /* free's nm */
  39919. #endif
  39920. return EXPECT_RESULT();
  39921. }
  39922. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  39923. static int test_GENERAL_NAME_set0_othername(void) {
  39924. EXPECT_DECLS;
  39925. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  39926. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  39927. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  39928. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  39929. defined(WOLFSSL_FPKI)
  39930. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  39931. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  39932. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  39933. const char * cert_fname = "./certs/server-cert.der";
  39934. const char * key_fname = "./certs/server-key.der";
  39935. X509* x509 = NULL;
  39936. GENERAL_NAME* gn = NULL;
  39937. GENERAL_NAMES* gns = NULL;
  39938. ASN1_OBJECT* upn_oid = NULL;
  39939. ASN1_UTF8STRING *utf8str = NULL;
  39940. ASN1_TYPE *value = NULL;
  39941. X509_EXTENSION * ext = NULL;
  39942. byte* pt = NULL;
  39943. byte der[4096];
  39944. int derSz = 0;
  39945. EVP_PKEY* priv = NULL;
  39946. XFILE f = XBADFILE;
  39947. ExpectTrue((f = XFOPEN(cert_fname, "rb")) != XBADFILE);
  39948. ExpectNotNull(x509 = d2i_X509_fp(f, NULL));
  39949. if (f != XBADFILE) {
  39950. XFCLOSE(f);
  39951. f = XBADFILE;
  39952. }
  39953. ExpectNotNull(gn = GENERAL_NAME_new());
  39954. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  39955. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  39956. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  39957. ExpectNotNull(value = ASN1_TYPE_new());
  39958. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  39959. if ((value == NULL) || (value->value.ptr != (char*)utf8str)) {
  39960. wolfSSL_ASN1_STRING_free(utf8str);
  39961. }
  39962. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  39963. if (EXPECT_FAIL()) {
  39964. ASN1_TYPE_free(value);
  39965. }
  39966. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  39967. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  39968. if (EXPECT_FAIL()) {
  39969. GENERAL_NAME_free(gn);
  39970. gn = NULL;
  39971. }
  39972. ExpectNotNull(ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  39973. ExpectIntEQ(X509_add_ext(x509, ext, -1), 1);
  39974. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  39975. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  39976. if (f != XBADFILE) {
  39977. XFCLOSE(f);
  39978. f = XBADFILE;
  39979. }
  39980. pt = der;
  39981. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  39982. (const unsigned char**)&pt, derSz));
  39983. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  39984. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  39985. gns = NULL;
  39986. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  39987. NULL));
  39988. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 3);
  39989. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 2));
  39990. ExpectIntEQ(gn->type, 0);
  39991. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  39992. ASN1_OBJECT_free(upn_oid);
  39993. X509_EXTENSION_free(ext);
  39994. X509_free(x509);
  39995. EVP_PKEY_free(priv);
  39996. #endif
  39997. return EXPECT_RESULT();
  39998. }
  39999. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  40000. static int test_othername_and_SID_ext(void) {
  40001. EXPECT_DECLS;
  40002. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  40003. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  40004. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  40005. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  40006. defined(WOLFSSL_FPKI) && defined(WOLFSSL_ASN_TEMPLATE)
  40007. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  40008. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  40009. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  40010. const char* csr_fname = "./certs/csr.signed.der";
  40011. const char* key_fname = "./certs/server-key.der";
  40012. byte der[4096];
  40013. int derSz = 0;
  40014. X509_REQ* x509 = NULL;
  40015. STACK_OF(X509_EXTENSION) *exts = NULL;
  40016. X509_EXTENSION * san_ext = NULL;
  40017. X509_EXTENSION * ext = NULL;
  40018. GENERAL_NAME* gn = NULL;
  40019. GENERAL_NAMES* gns = NULL;
  40020. ASN1_OBJECT* upn_oid = NULL;
  40021. ASN1_UTF8STRING *utf8str = NULL;
  40022. ASN1_TYPE *value = NULL;
  40023. ASN1_STRING *extval = NULL;
  40024. /* SID extension. SID data format explained here:
  40025. * https://blog.qdsecurity.se/2022/05/27/manually-injecting-a-sid-in-a-certificate/
  40026. */
  40027. uint8_t SidExtension[] = {
  40028. 48, 64, 160, 62, 6, 10, 43, 6, 1, 4, 1, 130, 55, 25, 2, 1, 160,
  40029. 48, 4, 46, 83, 45, 49, 45, 53, 45, 50, 49, 45, 50, 56, 52, 51, 57,
  40030. 48, 55, 52, 49, 56, 45, 51, 57, 50, 54, 50, 55, 55, 52, 50, 49, 45,
  40031. 51, 56, 49, 53, 57, 57, 51, 57, 55, 50, 45, 52, 54, 48, 49};
  40032. uint8_t expectedAltName[] = {
  40033. 0x30, 0x27, 0xA0, 0x25, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82,
  40034. 0x37, 0x14, 0x02, 0x03, 0xA0, 0x17, 0x0C, 0x15, 0x6F, 0x74, 0x68, 0x65,
  40035. 0x72, 0x6E, 0x61, 0x6D, 0x65, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  40036. 0x6C, 0x2E, 0x63, 0x6F, 0x6D};
  40037. X509_EXTENSION *sid_ext = NULL;
  40038. ASN1_OBJECT* sid_oid = NULL;
  40039. ASN1_OCTET_STRING *sid_data = NULL;
  40040. EVP_PKEY* priv = NULL;
  40041. XFILE f = XBADFILE;
  40042. byte* pt = NULL;
  40043. BIO* bio = NULL;
  40044. ExpectTrue((f = XFOPEN(csr_fname, "rb")) != XBADFILE);
  40045. ExpectNotNull(x509 = d2i_X509_REQ_fp(f, NULL));
  40046. if (f != XBADFILE) {
  40047. XFCLOSE(f);
  40048. f = XBADFILE;
  40049. }
  40050. ExpectIntEQ(X509_REQ_set_version(x509, 2), 1);
  40051. ExpectNotNull(gn = GENERAL_NAME_new());
  40052. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  40053. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  40054. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  40055. ExpectNotNull(value = ASN1_TYPE_new());
  40056. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  40057. if (EXPECT_FAIL()) {
  40058. ASN1_UTF8STRING_free(utf8str);
  40059. }
  40060. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  40061. if (EXPECT_FAIL()) {
  40062. ASN1_TYPE_free(value);
  40063. GENERAL_NAME_free(gn);
  40064. gn = NULL;
  40065. }
  40066. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  40067. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  40068. if (EXPECT_FAIL()) {
  40069. GENERAL_NAME_free(gn);
  40070. }
  40071. ExpectNotNull(san_ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  40072. ExpectNotNull(sid_oid = OBJ_txt2obj("1.3.6.1.4.1.311.25.2", 1));
  40073. ExpectNotNull(sid_data = ASN1_OCTET_STRING_new());
  40074. ASN1_OCTET_STRING_set(sid_data, SidExtension, sizeof(SidExtension));
  40075. ExpectNotNull(sid_ext = X509_EXTENSION_create_by_OBJ(NULL, sid_oid, 0,
  40076. sid_data));
  40077. ExpectNotNull(exts = sk_X509_EXTENSION_new_null());
  40078. /* Ensure an empty stack doesn't raise an error. */
  40079. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  40080. ExpectIntEQ(sk_X509_EXTENSION_push(exts, san_ext), 1);
  40081. if (EXPECT_FAIL()) {
  40082. X509_EXTENSION_free(san_ext);
  40083. }
  40084. ExpectIntEQ(sk_X509_EXTENSION_push(exts, sid_ext), 2);
  40085. if (EXPECT_FAIL()) {
  40086. X509_EXTENSION_free(sid_ext);
  40087. }
  40088. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  40089. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  40090. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  40091. if (f != XBADFILE)
  40092. XFCLOSE(f);
  40093. pt = der;
  40094. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  40095. (const unsigned char**)&pt, derSz));
  40096. ExpectIntGT(X509_REQ_sign(x509, priv, EVP_sha256()), 0);
  40097. pt = der;
  40098. ExpectIntGT(derSz = i2d_X509_REQ(x509, &pt), 0);
  40099. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  40100. gns = NULL;
  40101. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  40102. exts = NULL;
  40103. ASN1_OBJECT_free(upn_oid);
  40104. ASN1_OBJECT_free(sid_oid);
  40105. ASN1_OCTET_STRING_free(sid_data);
  40106. X509_REQ_free(x509);
  40107. x509 = NULL;
  40108. EVP_PKEY_free(priv);
  40109. /* At this point everything used to generate what is in der is cleaned up.
  40110. * We now read back from der to confirm the extensions were inserted
  40111. * correctly. */
  40112. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
  40113. ExpectNotNull(bio);
  40114. ExpectIntEQ(BIO_write(bio, der, derSz), derSz); /* d2i consumes BIO */
  40115. ExpectNotNull(d2i_X509_REQ_bio(bio, &x509));
  40116. ExpectNotNull(x509);
  40117. BIO_free(bio);
  40118. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  40119. x509));
  40120. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  40121. /* Check the SID extension. */
  40122. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 0));
  40123. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  40124. ExpectIntEQ(extval->length, sizeof(SidExtension));
  40125. ExpectIntEQ(XMEMCMP(SidExtension, extval->data, sizeof(SidExtension)), 0);
  40126. /* Check the AltNames extension. */
  40127. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 1));
  40128. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  40129. ExpectIntEQ(extval->length, sizeof(expectedAltName));
  40130. ExpectIntEQ(XMEMCMP(expectedAltName, extval->data, sizeof(expectedAltName)),
  40131. 0);
  40132. /* Cleanup */
  40133. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  40134. NULL));
  40135. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  40136. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 0));
  40137. ExpectIntEQ(gn->type, 0);
  40138. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  40139. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  40140. X509_REQ_free(x509);
  40141. #endif
  40142. return EXPECT_RESULT();
  40143. }
  40144. static int test_wolfSSL_X509_set_name(void)
  40145. {
  40146. EXPECT_DECLS;
  40147. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  40148. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  40149. X509* x509 = NULL;
  40150. X509_NAME* name = NULL;
  40151. ExpectNotNull(name = X509_NAME_new());
  40152. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  40153. (byte*)"wolfssl.com", 11, 0, 1),
  40154. WOLFSSL_SUCCESS);
  40155. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  40156. (byte*)"support@wolfssl.com", 19, -1,
  40157. 1), WOLFSSL_SUCCESS);
  40158. ExpectNotNull(x509 = X509_new());
  40159. ExpectIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  40160. ExpectIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  40161. ExpectIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  40162. ExpectIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  40163. ExpectIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  40164. ExpectIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  40165. ExpectIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  40166. ExpectIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  40167. X509_free(x509);
  40168. X509_NAME_free(name);
  40169. #endif /* OPENSSL_ALL && !NO_CERTS */
  40170. return EXPECT_RESULT();
  40171. }
  40172. static int test_wolfSSL_X509_set_notAfter(void)
  40173. {
  40174. EXPECT_DECLS;
  40175. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  40176. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  40177. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  40178. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  40179. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  40180. /* Generalized time will overflow time_t if not long */
  40181. X509* x = NULL;
  40182. BIO* bio = NULL;
  40183. ASN1_TIME *asn_time = NULL;
  40184. ASN1_TIME *time_check = NULL;
  40185. const int year = 365*24*60*60;
  40186. const int day = 24*60*60;
  40187. const int hour = 60*60;
  40188. const int mini = 60;
  40189. int offset_day;
  40190. unsigned char buf[25];
  40191. time_t t;
  40192. /*
  40193. * Setup asn_time. APACHE HTTPD uses time(NULL)
  40194. */
  40195. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  40196. offset_day = 7;
  40197. /*
  40198. * Free these.
  40199. */
  40200. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  40201. ExpectNotNull(asn_time);
  40202. ExpectNotNull(x = X509_new());
  40203. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40204. /*
  40205. * Tests
  40206. */
  40207. ExpectTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  40208. /* time_check is simply (ANS1_TIME*)x->notAfter */
  40209. ExpectNotNull(time_check = X509_get_notAfter(x));
  40210. /* ANS1_TIME_check validates by checking if argument can be parsed */
  40211. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  40212. /* Convert to human readable format and compare to intended date */
  40213. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  40214. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  40215. ExpectIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  40216. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, NULL));
  40217. ExpectFalse(wolfSSL_X509_set_notAfter(x, NULL));
  40218. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, asn_time));
  40219. /*
  40220. * Cleanup
  40221. */
  40222. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  40223. X509_free(x);
  40224. BIO_free(bio);
  40225. #endif
  40226. return EXPECT_RESULT();
  40227. }
  40228. static int test_wolfSSL_X509_set_notBefore(void)
  40229. {
  40230. EXPECT_DECLS;
  40231. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  40232. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  40233. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  40234. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  40235. X509* x = NULL;
  40236. BIO* bio = NULL;
  40237. ASN1_TIME *asn_time = NULL;
  40238. ASN1_TIME *time_check = NULL;
  40239. const int year = 365*24*60*60;
  40240. const int day = 24*60*60;
  40241. const int hour = 60*60;
  40242. const int mini = 60;
  40243. int offset_day;
  40244. unsigned char buf[25];
  40245. time_t t;
  40246. /*
  40247. * Setup asn_time. APACHE HTTPD uses time(NULL)
  40248. */
  40249. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  40250. offset_day = 7;
  40251. /*
  40252. * Free these.
  40253. */
  40254. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  40255. ExpectNotNull(asn_time);
  40256. ExpectNotNull(x = X509_new());
  40257. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40258. ExpectIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  40259. /*
  40260. * Main Tests
  40261. */
  40262. ExpectTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  40263. /* time_check == (ANS1_TIME*)x->notBefore */
  40264. ExpectNotNull(time_check = X509_get_notBefore(x));
  40265. /* ANS1_TIME_check validates by checking if argument can be parsed */
  40266. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  40267. /* Convert to human readable format and compare to intended date */
  40268. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  40269. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  40270. ExpectIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  40271. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, NULL));
  40272. ExpectFalse(wolfSSL_X509_set_notBefore(x, NULL));
  40273. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, asn_time));
  40274. /*
  40275. * Cleanup
  40276. */
  40277. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  40278. X509_free(x);
  40279. BIO_free(bio);
  40280. #endif
  40281. return EXPECT_RESULT();
  40282. }
  40283. static int test_wolfSSL_X509_set_version(void)
  40284. {
  40285. EXPECT_DECLS;
  40286. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  40287. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  40288. X509* x509 = NULL;
  40289. long v = 2L;
  40290. long maxInt = INT_MAX;
  40291. ExpectNotNull(x509 = X509_new());
  40292. /* These should pass. */
  40293. ExpectTrue(wolfSSL_X509_set_version(x509, v));
  40294. ExpectIntEQ(v, wolfSSL_X509_get_version(x509));
  40295. /* Fail Case: When v(long) is greater than x509->version(int). */
  40296. v = maxInt+1;
  40297. ExpectFalse(wolfSSL_X509_set_version(x509, v));
  40298. ExpectFalse(wolfSSL_X509_set_version(NULL, 2L));
  40299. ExpectFalse(wolfSSL_X509_set_version(NULL, maxInt+1));
  40300. /* Cleanup */
  40301. X509_free(x509);
  40302. #endif
  40303. return EXPECT_RESULT();
  40304. }
  40305. #ifndef NO_BIO
  40306. static int test_wolfSSL_BIO_gets(void)
  40307. {
  40308. EXPECT_DECLS;
  40309. #if defined(OPENSSL_EXTRA)
  40310. BIO* bio = NULL;
  40311. BIO* bio2 = NULL;
  40312. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  40313. char emp[] = "";
  40314. char bio_buffer[20];
  40315. int bufferSz = 20;
  40316. #ifdef OPENSSL_ALL
  40317. BUF_MEM* emp_bm = NULL;
  40318. BUF_MEM* msg_bm = NULL;
  40319. #endif
  40320. /* try with bad args */
  40321. ExpectNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  40322. #ifdef OPENSSL_ALL
  40323. ExpectIntEQ(BIO_set_mem_buf(bio, NULL, BIO_NOCLOSE), BAD_FUNC_ARG);
  40324. #endif
  40325. /* try with real msg */
  40326. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  40327. XMEMSET(bio_buffer, 0, bufferSz);
  40328. ExpectNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  40329. ExpectNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  40330. ExpectNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  40331. ExpectFalse(bio2 != BIO_next(bio));
  40332. /* make buffer filled with no terminating characters */
  40333. XMEMSET(bio_buffer, 1, bufferSz);
  40334. /* BIO_gets reads a line of data */
  40335. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  40336. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  40337. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  40338. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  40339. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  40340. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  40341. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  40342. #ifdef OPENSSL_ALL
  40343. /* test setting the mem_buf manually */
  40344. BIO_free(bio);
  40345. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  40346. ExpectNotNull(emp_bm = BUF_MEM_new());
  40347. ExpectNotNull(msg_bm = BUF_MEM_new());
  40348. ExpectIntEQ(BUF_MEM_grow(msg_bm, sizeof(msg)), sizeof(msg));
  40349. if (EXPECT_SUCCESS()) {
  40350. XFREE(msg_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  40351. msg_bm->data = NULL;
  40352. }
  40353. /* emp size is 1 for terminator */
  40354. ExpectIntEQ(BUF_MEM_grow(emp_bm, sizeof(emp)), sizeof(emp));
  40355. if (EXPECT_SUCCESS()) {
  40356. XFREE(emp_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  40357. emp_bm->data = emp;
  40358. msg_bm->data = msg;
  40359. }
  40360. ExpectIntEQ(BIO_set_mem_buf(bio, emp_bm, BIO_CLOSE), WOLFSSL_SUCCESS);
  40361. /* check reading an empty string */
  40362. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  40363. ExpectStrEQ(emp, bio_buffer);
  40364. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  40365. /* BIO_gets reads a line of data */
  40366. ExpectIntEQ(BIO_set_mem_buf(bio, msg_bm, BIO_NOCLOSE), WOLFSSL_SUCCESS);
  40367. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  40368. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  40369. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  40370. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  40371. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  40372. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  40373. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  40374. if (EXPECT_SUCCESS())
  40375. emp_bm->data = NULL;
  40376. BUF_MEM_free(emp_bm);
  40377. if (EXPECT_SUCCESS())
  40378. msg_bm->data = NULL;
  40379. BUF_MEM_free(msg_bm);
  40380. #endif
  40381. /* check not null terminated string */
  40382. BIO_free(bio);
  40383. bio = NULL;
  40384. msg[0] = 0x33;
  40385. msg[1] = 0x33;
  40386. msg[2] = 0x33;
  40387. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  40388. ExpectIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  40389. ExpectIntEQ(bio_buffer[0], msg[0]);
  40390. ExpectIntEQ(bio_buffer[1], msg[1]);
  40391. ExpectIntNE(bio_buffer[2], msg[2]);
  40392. BIO_free(bio);
  40393. bio = NULL;
  40394. msg[3] = 0x33;
  40395. bio_buffer[3] = 0x33;
  40396. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  40397. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  40398. ExpectIntEQ(bio_buffer[0], msg[0]);
  40399. ExpectIntEQ(bio_buffer[1], msg[1]);
  40400. ExpectIntEQ(bio_buffer[2], msg[2]);
  40401. ExpectIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  40402. /* check reading an empty string */
  40403. BIO_free(bio);
  40404. bio = NULL;
  40405. ExpectNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  40406. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  40407. ExpectStrEQ(emp, bio_buffer);
  40408. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  40409. /* check error cases */
  40410. BIO_free(bio);
  40411. bio = NULL;
  40412. ExpectIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  40413. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40414. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  40415. #if !defined(NO_FILESYSTEM)
  40416. {
  40417. BIO* f_bio = NULL;
  40418. XFILE f = XBADFILE;
  40419. ExpectNotNull(f_bio = BIO_new(BIO_s_file()));
  40420. ExpectIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  40421. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  40422. ExpectIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  40423. if (EXPECT_FAIL() && (f != XBADFILE)) {
  40424. XFCLOSE(f);
  40425. }
  40426. ExpectIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  40427. BIO_free(f_bio);
  40428. f_bio = NULL;
  40429. }
  40430. #endif /* NO_FILESYSTEM */
  40431. BIO_free(bio);
  40432. bio = NULL;
  40433. BIO_free(bio2);
  40434. bio2 = NULL;
  40435. /* try with type BIO */
  40436. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  40437. sizeof(msg));
  40438. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  40439. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  40440. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  40441. ExpectIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  40442. ExpectIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  40443. ExpectIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  40444. ExpectIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  40445. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  40446. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  40447. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  40448. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  40449. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  40450. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  40451. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  40452. BIO_free(bio);
  40453. bio = NULL;
  40454. BIO_free(bio2);
  40455. bio2 = NULL;
  40456. /* check reading an empty string */
  40457. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  40458. ExpectIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  40459. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  40460. ExpectStrEQ(emp, bio_buffer);
  40461. BIO_free(bio);
  40462. bio = NULL;
  40463. #endif
  40464. return EXPECT_RESULT();
  40465. }
  40466. static int test_wolfSSL_BIO_puts(void)
  40467. {
  40468. EXPECT_DECLS;
  40469. #if defined(OPENSSL_EXTRA)
  40470. BIO* bio = NULL;
  40471. char input[] = "hello\0world\n.....ok\n\0";
  40472. char output[128];
  40473. XMEMSET(output, 0, sizeof(output));
  40474. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40475. ExpectIntEQ(BIO_puts(bio, input), 5);
  40476. ExpectIntEQ(BIO_pending(bio), 5);
  40477. ExpectIntEQ(BIO_puts(bio, input + 6), 14);
  40478. ExpectIntEQ(BIO_pending(bio), 19);
  40479. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  40480. ExpectStrEQ(output, "helloworld\n");
  40481. ExpectIntEQ(BIO_pending(bio), 8);
  40482. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  40483. ExpectStrEQ(output, ".....ok\n");
  40484. ExpectIntEQ(BIO_pending(bio), 0);
  40485. ExpectIntEQ(BIO_puts(bio, ""), -1);
  40486. BIO_free(bio);
  40487. #endif
  40488. return EXPECT_RESULT();
  40489. }
  40490. static int test_wolfSSL_BIO_dump(void)
  40491. {
  40492. EXPECT_DECLS;
  40493. #if defined(OPENSSL_EXTRA)
  40494. BIO* bio;
  40495. static const unsigned char data[] = {
  40496. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  40497. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  40498. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  40499. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  40500. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  40501. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  40502. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  40503. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  40504. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  40505. 0xB4
  40506. };
  40507. /* Generated with OpenSSL. */
  40508. static const char expected[] =
  40509. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  40510. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  40511. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  40512. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  40513. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  40514. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  40515. static const char expectedAll[] =
  40516. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  40517. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  40518. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  40519. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  40520. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  40521. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  40522. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  40523. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  40524. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  40525. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  40526. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  40527. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  40528. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  40529. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  40530. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  40531. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  40532. char output[16 * 80];
  40533. int i;
  40534. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40535. /* Example key dumped. */
  40536. ExpectIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  40537. sizeof(expected) - 1);
  40538. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  40539. ExpectIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  40540. /* Try every possible value for a character. */
  40541. for (i = 0; i < 256; i++)
  40542. output[i] = i;
  40543. ExpectIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  40544. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  40545. ExpectIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  40546. BIO_free(bio);
  40547. #endif
  40548. return EXPECT_RESULT();
  40549. }
  40550. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40551. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  40552. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  40553. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  40554. {
  40555. (void)ssl;
  40556. (void)buf;
  40557. (void)sz;
  40558. (void)ctx;
  40559. return WOLFSSL_CBIO_ERR_WANT_READ;
  40560. }
  40561. #endif
  40562. static int test_wolfSSL_BIO_should_retry(void)
  40563. {
  40564. EXPECT_DECLS;
  40565. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40566. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  40567. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  40568. tcp_ready ready;
  40569. func_args server_args;
  40570. THREAD_TYPE serverThread;
  40571. SOCKET_T sockfd = 0;
  40572. WOLFSSL_CTX* ctx = NULL;
  40573. WOLFSSL* ssl = NULL;
  40574. char msg[64] = "hello wolfssl!";
  40575. char reply[1024];
  40576. int msgSz = (int)XSTRLEN(msg);
  40577. int ret;
  40578. BIO* bio = NULL;
  40579. XMEMSET(&server_args, 0, sizeof(func_args));
  40580. #ifdef WOLFSSL_TIRTOS
  40581. fdOpenSession(Task_self());
  40582. #endif
  40583. StartTCP();
  40584. InitTcpReady(&ready);
  40585. #if defined(USE_WINDOWS_API)
  40586. /* use RNG to get random port if using windows */
  40587. ready.port = GetRandomPort();
  40588. #endif
  40589. server_args.signal = &ready;
  40590. start_thread(test_server_nofail, &server_args, &serverThread);
  40591. wait_tcp_ready(&server_args);
  40592. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40593. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  40594. ExpectIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  40595. #endif
  40596. ExpectIntEQ(WOLFSSL_SUCCESS,
  40597. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  40598. ExpectIntEQ(WOLFSSL_SUCCESS,
  40599. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  40600. ExpectIntEQ(WOLFSSL_SUCCESS,
  40601. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  40602. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  40603. /* force retry */
  40604. ExpectNotNull(bio = wolfSSL_BIO_new_ssl(ctx, 1));
  40605. ExpectIntEQ(BIO_get_ssl(bio, &ssl), 1);
  40606. ExpectNotNull(ssl);
  40607. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  40608. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  40609. if (EXPECT_FAIL()) {
  40610. wolfSSL_free(ssl);
  40611. ssl = NULL;
  40612. }
  40613. ExpectIntLE(BIO_write(bio, msg, msgSz), 0);
  40614. ExpectIntNE(BIO_should_retry(bio), 0);
  40615. ExpectIntEQ(BIO_should_read(bio), 0);
  40616. ExpectIntEQ(BIO_should_write(bio), 0);
  40617. /* now perform successful connection */
  40618. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  40619. ExpectIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  40620. ExpectIntNE(BIO_read(bio, reply, sizeof(reply)), 0);
  40621. ret = wolfSSL_get_error(ssl, -1);
  40622. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  40623. ExpectIntNE(BIO_should_retry(bio), 0);
  40624. if (ret == WOLFSSL_ERROR_WANT_READ)
  40625. ExpectIntEQ(BIO_should_read(bio), 1);
  40626. else
  40627. ExpectIntEQ(BIO_should_read(bio), 0);
  40628. if (ret == WOLFSSL_ERROR_WANT_WRITE)
  40629. ExpectIntEQ(BIO_should_write(bio), 1);
  40630. else
  40631. ExpectIntEQ(BIO_should_write(bio), 0);
  40632. }
  40633. else {
  40634. ExpectIntEQ(BIO_should_retry(bio), 0);
  40635. ExpectIntEQ(BIO_should_read(bio), 0);
  40636. ExpectIntEQ(BIO_should_write(bio), 0);
  40637. }
  40638. ExpectIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  40639. XSTRLEN("I hear you fa shizzle!")), 0);
  40640. BIO_free(bio);
  40641. wolfSSL_CTX_free(ctx);
  40642. CloseSocket(sockfd);
  40643. join_thread(serverThread);
  40644. FreeTcpReady(&ready);
  40645. #ifdef WOLFSSL_TIRTOS
  40646. fdOpenSession(Task_self());
  40647. #endif
  40648. #endif
  40649. return EXPECT_RESULT();
  40650. }
  40651. static int test_wolfSSL_BIO_connect(void)
  40652. {
  40653. EXPECT_DECLS;
  40654. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40655. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  40656. tcp_ready ready;
  40657. func_args server_args;
  40658. THREAD_TYPE serverThread;
  40659. BIO *tcpBio = NULL;
  40660. BIO *sslBio = NULL;
  40661. SSL_CTX* ctx = NULL;
  40662. SSL *ssl = NULL;
  40663. SSL *sslPtr;
  40664. char msg[] = "hello wolfssl!";
  40665. char reply[30];
  40666. char buff[10] = {0};
  40667. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40668. ExpectIntEQ(WOLFSSL_SUCCESS,
  40669. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  40670. ExpectIntEQ(WOLFSSL_SUCCESS,
  40671. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  40672. ExpectIntEQ(WOLFSSL_SUCCESS,
  40673. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  40674. /* Setup server */
  40675. XMEMSET(&server_args, 0, sizeof(func_args));
  40676. StartTCP();
  40677. InitTcpReady(&ready);
  40678. #if defined(USE_WINDOWS_API)
  40679. /* use RNG to get random port if using windows */
  40680. ready.port = GetRandomPort();
  40681. #endif
  40682. server_args.signal = &ready;
  40683. start_thread(test_server_nofail, &server_args, &serverThread);
  40684. wait_tcp_ready(&server_args);
  40685. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  40686. /* Start the test proper */
  40687. /* Setup the TCP BIO */
  40688. ExpectNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  40689. ExpectIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  40690. /* Setup the SSL object */
  40691. ExpectNotNull(ssl = SSL_new(ctx));
  40692. SSL_set_connect_state(ssl);
  40693. /* Setup the SSL BIO */
  40694. ExpectNotNull(sslBio = BIO_new(BIO_f_ssl()));
  40695. ExpectIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  40696. if (EXPECT_FAIL()) {
  40697. wolfSSL_free(ssl);
  40698. }
  40699. /* Verify that BIO_get_ssl works. */
  40700. ExpectIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  40701. ExpectPtrEq(ssl, sslPtr);
  40702. /* Link BIO's so that sslBio uses tcpBio for IO */
  40703. ExpectPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  40704. /* Do TCP connect */
  40705. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  40706. /* Do TLS handshake */
  40707. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  40708. /* Test writing */
  40709. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  40710. /* Expect length of default wolfSSL reply */
  40711. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  40712. /* Clean it all up */
  40713. BIO_free_all(sslBio);
  40714. /* Server clean up */
  40715. join_thread(serverThread);
  40716. FreeTcpReady(&ready);
  40717. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  40718. * after. */
  40719. XMEMSET(&server_args, 0, sizeof(func_args));
  40720. StartTCP();
  40721. InitTcpReady(&ready);
  40722. #if defined(USE_WINDOWS_API)
  40723. /* use RNG to get random port if using windows */
  40724. ready.port = GetRandomPort();
  40725. #endif
  40726. server_args.signal = &ready;
  40727. start_thread(test_server_nofail, &server_args, &serverThread);
  40728. wait_tcp_ready(&server_args);
  40729. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  40730. ExpectNotNull(sslBio = BIO_new_ssl_connect(ctx));
  40731. ExpectIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  40732. ExpectIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  40733. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  40734. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  40735. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  40736. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  40737. /* Attempt to close the TLS connection gracefully. */
  40738. BIO_ssl_shutdown(sslBio);
  40739. BIO_free_all(sslBio);
  40740. join_thread(serverThread);
  40741. FreeTcpReady(&ready);
  40742. SSL_CTX_free(ctx);
  40743. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  40744. wc_ecc_fp_free(); /* free per thread cache */
  40745. #endif
  40746. #endif
  40747. return EXPECT_RESULT();
  40748. }
  40749. static int test_wolfSSL_BIO_tls(void)
  40750. {
  40751. EXPECT_DECLS;
  40752. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  40753. SSL_CTX* ctx = NULL;
  40754. SSL *ssl = NULL;
  40755. BIO *readBio = NULL;
  40756. BIO *writeBio = NULL;
  40757. int ret;
  40758. int err = 0;
  40759. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  40760. ExpectNotNull(ssl = SSL_new(ctx));
  40761. ExpectNotNull(readBio = BIO_new(BIO_s_mem()));
  40762. ExpectNotNull(writeBio = BIO_new(BIO_s_mem()));
  40763. /* Qt reads data from write-bio,
  40764. * then writes the read data into plain packet.
  40765. * Qt reads data from plain packet,
  40766. * then writes the read data into read-bio.
  40767. */
  40768. SSL_set_bio(ssl, readBio, writeBio);
  40769. do {
  40770. #ifdef WOLFSSL_ASYNC_CRYPT
  40771. if (err == WC_PENDING_E) {
  40772. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  40773. if (ret < 0) { break; } else if (ret == 0) { continue; }
  40774. }
  40775. #endif
  40776. ret = SSL_connect(ssl);
  40777. err = SSL_get_error(ssl, 0);
  40778. } while (err == WC_PENDING_E);
  40779. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  40780. /* in this use case, should return WANT READ
  40781. * so that Qt will read the data from plain packet for next state.
  40782. */
  40783. ExpectIntEQ(err, SSL_ERROR_WANT_READ);
  40784. SSL_free(ssl);
  40785. SSL_CTX_free(ctx);
  40786. #endif
  40787. return EXPECT_RESULT();
  40788. }
  40789. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40790. defined(HAVE_HTTP_CLIENT)
  40791. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  40792. {
  40793. BIO* clientBio;
  40794. SSL* sslClient;
  40795. SSL_CTX* ctx;
  40796. char connectAddr[20]; /* IP + port */;
  40797. (void)args;
  40798. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  40799. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  40800. AssertIntEQ(BIO_do_connect(clientBio), 1);
  40801. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  40802. AssertNotNull(sslClient = SSL_new(ctx));
  40803. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  40804. SSL_set_bio(sslClient, clientBio, clientBio);
  40805. AssertIntEQ(SSL_connect(sslClient), 1);
  40806. SSL_free(sslClient);
  40807. SSL_CTX_free(ctx);
  40808. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  40809. wc_ecc_fp_free(); /* free per thread cache */
  40810. #endif
  40811. WOLFSSL_RETURN_FROM_THREAD(0);
  40812. }
  40813. #endif
  40814. static int test_wolfSSL_BIO_accept(void)
  40815. {
  40816. EXPECT_DECLS;
  40817. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40818. defined(HAVE_HTTP_CLIENT)
  40819. BIO* serverBindBio = NULL;
  40820. BIO* serverAcceptBio = NULL;
  40821. SSL* sslServer = NULL;
  40822. SSL_CTX* ctx = NULL;
  40823. func_args args;
  40824. THREAD_TYPE thread;
  40825. char port[10]; /* 10 bytes should be enough to store the string
  40826. * representation of the port */
  40827. ExpectIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  40828. ExpectNotNull(serverBindBio = BIO_new_accept(port));
  40829. /* First BIO_do_accept binds the port */
  40830. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  40831. XMEMSET(&args, 0, sizeof(func_args));
  40832. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  40833. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  40834. /* Let's plug it into SSL to test */
  40835. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  40836. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  40837. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  40838. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  40839. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  40840. ExpectNotNull(sslServer = SSL_new(ctx));
  40841. ExpectNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  40842. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  40843. ExpectIntEQ(SSL_accept(sslServer), 1);
  40844. join_thread(thread);
  40845. BIO_free(serverBindBio);
  40846. SSL_free(sslServer);
  40847. SSL_CTX_free(ctx);
  40848. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  40849. wc_ecc_fp_free(); /* free per thread cache */
  40850. #endif
  40851. #endif
  40852. return EXPECT_RESULT();
  40853. }
  40854. static int test_wolfSSL_BIO_write(void)
  40855. {
  40856. EXPECT_DECLS;
  40857. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  40858. BIO* bio = NULL;
  40859. BIO* bio64 = NULL;
  40860. BIO* bio_mem = NULL;
  40861. BIO* ptr = NULL;
  40862. int sz;
  40863. char msg[] = "conversion test";
  40864. char out[40];
  40865. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  40866. void* bufPtr = NULL;
  40867. BUF_MEM* buf = NULL;
  40868. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  40869. ExpectNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  40870. if (EXPECT_FAIL()) {
  40871. BIO_free(bio64);
  40872. }
  40873. /* now should convert to base64 then write to memory */
  40874. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40875. BIO_flush(bio);
  40876. /* test BIO chain */
  40877. ExpectIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  40878. ExpectNotNull(buf);
  40879. ExpectIntEQ(buf->length, 25);
  40880. ExpectIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  40881. ExpectPtrEq(buf->data, bufPtr);
  40882. ExpectNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  40883. sz = sizeof(out);
  40884. XMEMSET(out, 0, sz);
  40885. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  40886. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  40887. /* write then read should return the same message */
  40888. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40889. sz = sizeof(out);
  40890. XMEMSET(out, 0, sz);
  40891. ExpectIntEQ(BIO_read(bio, out, sz), 16);
  40892. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  40893. /* now try encoding with no line ending */
  40894. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  40895. #ifdef HAVE_EX_DATA
  40896. BIO_set_ex_data(bio64, 0, (void*) "data");
  40897. ExpectIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  40898. #endif
  40899. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40900. BIO_flush(bio);
  40901. sz = sizeof(out);
  40902. XMEMSET(out, 0, sz);
  40903. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  40904. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  40905. BIO_free_all(bio); /* frees bio64 also */
  40906. bio = NULL;
  40907. /* test with more than one bio64 in list */
  40908. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  40909. ExpectNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  40910. if (EXPECT_FAIL()) {
  40911. BIO_free(bio64);
  40912. bio64 = NULL;
  40913. }
  40914. ExpectNotNull(bio_mem = BIO_new(BIO_s_mem()));
  40915. ExpectNotNull(BIO_push(bio64, bio_mem));
  40916. if (EXPECT_FAIL()) {
  40917. BIO_free(bio_mem);
  40918. }
  40919. /* now should convert to base64 when stored and then decode with read */
  40920. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  40921. BIO_flush(bio);
  40922. sz = sizeof(out);
  40923. XMEMSET(out, 0, sz);
  40924. ExpectIntEQ((sz = BIO_read(bio, out, sz)), 16);
  40925. ExpectIntEQ(XMEMCMP(out, msg, sz), 0);
  40926. BIO_clear_flags(bio64, ~0);
  40927. BIO_set_retry_read(bio);
  40928. BIO_free_all(bio); /* frees bio64s also */
  40929. bio = NULL;
  40930. ExpectNotNull(bio = BIO_new_mem_buf(out, 0));
  40931. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  40932. BIO_free(bio);
  40933. #endif
  40934. return EXPECT_RESULT();
  40935. }
  40936. static int test_wolfSSL_BIO_printf(void)
  40937. {
  40938. EXPECT_DECLS;
  40939. #if defined(OPENSSL_ALL)
  40940. BIO* bio = NULL;
  40941. int sz = 7;
  40942. char msg[] = "TLS 1.3 for the world";
  40943. char out[60];
  40944. char expected[] = "TLS 1.3 for the world : sz = 7";
  40945. XMEMSET(out, 0, sizeof(out));
  40946. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40947. ExpectIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  40948. ExpectIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  40949. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  40950. ExpectIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  40951. BIO_free(bio);
  40952. #endif
  40953. return EXPECT_RESULT();
  40954. }
  40955. static int test_wolfSSL_BIO_f_md(void)
  40956. {
  40957. EXPECT_DECLS;
  40958. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  40959. BIO* bio = NULL;
  40960. BIO* mem = NULL;
  40961. char msg[] = "message to hash";
  40962. char out[60];
  40963. EVP_MD_CTX* ctx = NULL;
  40964. const unsigned char testKey[] =
  40965. {
  40966. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  40967. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  40968. 0x0b, 0x0b, 0x0b, 0x0b
  40969. };
  40970. const char testData[] = "Hi There";
  40971. const unsigned char testResult[] =
  40972. {
  40973. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  40974. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  40975. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  40976. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  40977. };
  40978. const unsigned char expectedHash[] =
  40979. {
  40980. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  40981. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  40982. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  40983. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  40984. };
  40985. const unsigned char emptyHash[] =
  40986. {
  40987. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  40988. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  40989. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  40990. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  40991. };
  40992. unsigned char check[sizeof(testResult) + 1];
  40993. size_t checkSz = -1;
  40994. EVP_PKEY* key = NULL;
  40995. XMEMSET(out, 0, sizeof(out));
  40996. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  40997. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  40998. ExpectIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  40999. ExpectIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  41000. /* should not be able to write/read yet since just digest wrapper and no
  41001. * data is passing through the bio */
  41002. ExpectIntEQ(BIO_write(bio, msg, 0), 0);
  41003. ExpectIntEQ(BIO_pending(bio), 0);
  41004. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  41005. ExpectIntEQ(BIO_gets(bio, out, 3), 0);
  41006. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  41007. ExpectIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  41008. BIO_reset(bio);
  41009. /* append BIO mem to bio in order to read/write */
  41010. ExpectNotNull(bio = BIO_push(bio, mem));
  41011. XMEMSET(out, 0, sizeof(out));
  41012. ExpectIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  41013. ExpectIntEQ(BIO_pending(bio), 16);
  41014. /* this just reads the message and does not hash it (gets calls final) */
  41015. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  41016. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  41017. /* create a message digest using BIO */
  41018. XMEMSET(out, 0, sizeof(out));
  41019. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  41020. ExpectIntEQ(BIO_pending(mem), 16);
  41021. ExpectIntEQ(BIO_pending(bio), 16);
  41022. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  41023. ExpectIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  41024. BIO_free(bio);
  41025. bio = NULL;
  41026. BIO_free(mem);
  41027. mem = NULL;
  41028. /* test with HMAC */
  41029. XMEMSET(out, 0, sizeof(out));
  41030. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  41031. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  41032. BIO_get_md_ctx(bio, &ctx);
  41033. ExpectNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, testKey,
  41034. (int)sizeof(testKey)));
  41035. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  41036. ExpectNotNull(bio = BIO_push(bio, mem));
  41037. BIO_write(bio, testData, (int)strlen(testData));
  41038. ExpectIntEQ(EVP_DigestSignFinal(ctx, NULL, &checkSz), 1);
  41039. ExpectIntEQ(EVP_DigestSignFinal(ctx, check, &checkSz), 1);
  41040. ExpectIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  41041. EVP_PKEY_free(key);
  41042. BIO_free(bio);
  41043. BIO_free(mem);
  41044. #endif
  41045. return EXPECT_RESULT();
  41046. }
  41047. static int test_wolfSSL_BIO_up_ref(void)
  41048. {
  41049. EXPECT_DECLS;
  41050. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  41051. BIO* bio = NULL;
  41052. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  41053. ExpectIntEQ(BIO_up_ref(NULL), 0);
  41054. ExpectIntEQ(BIO_up_ref(bio), 1);
  41055. BIO_free(bio);
  41056. ExpectIntEQ(BIO_up_ref(bio), 1);
  41057. BIO_free(bio);
  41058. BIO_free(bio);
  41059. #endif
  41060. return EXPECT_RESULT();
  41061. }
  41062. static int test_wolfSSL_BIO_reset(void)
  41063. {
  41064. EXPECT_DECLS;
  41065. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  41066. BIO* bio = NULL;
  41067. byte buf[16];
  41068. ExpectNotNull(bio = BIO_new_mem_buf("secure your data",
  41069. (word32)XSTRLEN("secure your data")));
  41070. ExpectIntEQ(BIO_read(bio, buf, 6), 6);
  41071. ExpectIntEQ(XMEMCMP(buf, "secure", 6), 0);
  41072. XMEMSET(buf, 0, 16);
  41073. ExpectIntEQ(BIO_read(bio, buf, 16), 10);
  41074. ExpectIntEQ(XMEMCMP(buf, " your data", 10), 0);
  41075. /* You cannot write to MEM BIO with read-only mode. */
  41076. ExpectIntEQ(BIO_write(bio, "WriteToReadonly", 15), 0);
  41077. ExpectIntEQ(BIO_read(bio, buf, 16), -1);
  41078. XMEMSET(buf, 0, 16);
  41079. ExpectIntEQ(BIO_reset(bio), 0);
  41080. ExpectIntEQ(BIO_read(bio, buf, 16), 16);
  41081. ExpectIntEQ(XMEMCMP(buf, "secure your data", 16), 0);
  41082. BIO_free(bio);
  41083. #endif
  41084. return EXPECT_RESULT();
  41085. }
  41086. #endif /* !NO_BIO */
  41087. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  41088. /* test that the callback arg is correct */
  41089. static int certCbArg = 0;
  41090. static int certCb(WOLFSSL* ssl, void* arg)
  41091. {
  41092. if (ssl == NULL || arg != &certCbArg)
  41093. return 0;
  41094. if (wolfSSL_is_server(ssl)) {
  41095. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  41096. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41097. return 0;
  41098. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  41099. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41100. return 0;
  41101. }
  41102. else {
  41103. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  41104. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41105. return 0;
  41106. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  41107. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41108. return 0;
  41109. }
  41110. return 1;
  41111. }
  41112. static int certSetupCb(WOLFSSL_CTX* ctx)
  41113. {
  41114. SSL_CTX_set_cert_cb(ctx, certCb, &certCbArg);
  41115. return TEST_SUCCESS;
  41116. }
  41117. /**
  41118. * This is only done because test_wolfSSL_client_server_nofail_memio has no way
  41119. * to stop certificate and key loading
  41120. */
  41121. static int certClearCb(WOLFSSL* ssl)
  41122. {
  41123. /* Clear the loaded certs to force the callbacks to set them up */
  41124. SSL_certs_clear(ssl);
  41125. return TEST_SUCCESS;
  41126. }
  41127. #endif
  41128. static int test_wolfSSL_cert_cb(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. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  41135. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  41136. func_cb_client.ctx_ready = certSetupCb;
  41137. func_cb_client.ssl_ready = certClearCb;
  41138. func_cb_server.ctx_ready = certSetupCb;
  41139. func_cb_server.ssl_ready = certClearCb;
  41140. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  41141. &func_cb_server, NULL), TEST_SUCCESS);
  41142. #endif
  41143. return EXPECT_RESULT();
  41144. }
  41145. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  41146. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_cipher = NULL;
  41147. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs = NULL;
  41148. static int test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready(WOLFSSL_CTX* ctx)
  41149. {
  41150. EXPECT_DECLS;
  41151. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx,
  41152. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher), WOLFSSL_SUCCESS);
  41153. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  41154. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs), WOLFSSL_SUCCESS);
  41155. return EXPECT_RESULT();
  41156. }
  41157. static int test_wolfSSL_cert_cb_dyn_ciphers_certCB(WOLFSSL* ssl, void* arg)
  41158. {
  41159. const byte* suites = NULL;
  41160. word16 suiteSz = 0;
  41161. const byte* hashSigAlgo = NULL;
  41162. word16 hashSigAlgoSz = 0;
  41163. word16 idx = 0;
  41164. int haveRSA = 0;
  41165. int haveECC = 0;
  41166. (void)arg;
  41167. if (wolfSSL_get_client_suites_sigalgs(ssl, &suites, &suiteSz, &hashSigAlgo,
  41168. &hashSigAlgoSz) != WOLFSSL_SUCCESS)
  41169. return 0;
  41170. if (suites == NULL || suiteSz == 0 || hashSigAlgo == NULL ||
  41171. hashSigAlgoSz == 0)
  41172. return 0;
  41173. for (idx = 0; idx < suiteSz; idx += 2) {
  41174. WOLFSSL_CIPHERSUITE_INFO info =
  41175. wolfSSL_get_ciphersuite_info(suites[idx], suites[idx+1]);
  41176. if (info.rsaAuth)
  41177. haveRSA = 1;
  41178. else if (info.eccAuth)
  41179. haveECC = 1;
  41180. }
  41181. if (hashSigAlgoSz > 0) {
  41182. /* sigalgs extension takes precedence over ciphersuites */
  41183. haveRSA = 0;
  41184. haveECC = 0;
  41185. }
  41186. for (idx = 0; idx < hashSigAlgoSz; idx += 2) {
  41187. int hashAlgo = 0;
  41188. int sigAlgo = 0;
  41189. if (wolfSSL_get_sigalg_info(hashSigAlgo[idx+0], hashSigAlgo[idx+1],
  41190. &hashAlgo, &sigAlgo) != 0)
  41191. return 0;
  41192. if (sigAlgo == RSAk || sigAlgo == RSAPSSk)
  41193. haveRSA = 1;
  41194. else if (sigAlgo == ECDSAk)
  41195. haveECC = 1;
  41196. }
  41197. if (haveRSA) {
  41198. if (wolfSSL_use_certificate_file(ssl, svrCertFile, WOLFSSL_FILETYPE_PEM)
  41199. != WOLFSSL_SUCCESS)
  41200. return 0;
  41201. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile, WOLFSSL_FILETYPE_PEM)
  41202. != WOLFSSL_SUCCESS)
  41203. return 0;
  41204. }
  41205. else if (haveECC) {
  41206. if (wolfSSL_use_certificate_file(ssl, eccCertFile, WOLFSSL_FILETYPE_PEM)
  41207. != WOLFSSL_SUCCESS)
  41208. return 0;
  41209. if (wolfSSL_use_PrivateKey_file(ssl, eccKeyFile, WOLFSSL_FILETYPE_PEM)
  41210. != WOLFSSL_SUCCESS)
  41211. return 0;
  41212. }
  41213. return 1;
  41214. }
  41215. static int test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready(WOLFSSL_CTX* ctx)
  41216. {
  41217. SSL_CTX_set_cert_cb(ctx, test_wolfSSL_cert_cb_dyn_ciphers_certCB, NULL);
  41218. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, NULL);
  41219. return TEST_SUCCESS;
  41220. }
  41221. #endif
  41222. /* Testing dynamic ciphers offered by client */
  41223. static int test_wolfSSL_cert_cb_dyn_ciphers(void)
  41224. {
  41225. EXPECT_DECLS;
  41226. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  41227. test_ssl_cbf func_cb_client;
  41228. test_ssl_cbf func_cb_server;
  41229. struct {
  41230. method_provider client_meth;
  41231. const char* client_ciphers;
  41232. const char* client_sigalgs;
  41233. const char* client_ca;
  41234. method_provider server_meth;
  41235. } test_params[] = {
  41236. #if !defined(NO_SHA256) && defined(HAVE_AESGCM)
  41237. #ifdef WOLFSSL_TLS13
  41238. #if !defined(NO_RSA) && defined(WC_RSA_PSS)
  41239. {wolfTLSv1_3_client_method,
  41240. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  41241. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_3_server_method},
  41242. #endif
  41243. #ifdef HAVE_ECC
  41244. {wolfTLSv1_3_client_method,
  41245. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  41246. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_3_server_method},
  41247. #endif
  41248. #endif
  41249. #ifndef WOLFSSL_NO_TLS12
  41250. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_DH)
  41251. {wolfTLSv1_2_client_method,
  41252. "DHE-RSA-AES128-GCM-SHA256",
  41253. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_2_server_method},
  41254. #endif
  41255. #ifdef HAVE_ECC
  41256. {wolfTLSv1_2_client_method,
  41257. "ECDHE-ECDSA-AES128-GCM-SHA256",
  41258. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_2_server_method},
  41259. #endif
  41260. #endif
  41261. #endif
  41262. };
  41263. size_t i;
  41264. size_t testCount = sizeof(test_params)/sizeof(*test_params);
  41265. if (testCount > 0) {
  41266. for (i = 0; i < testCount; i++) {
  41267. printf("\tTesting %s ciphers with %s sigalgs\n",
  41268. test_params[i].client_ciphers,
  41269. test_params[i].client_sigalgs);
  41270. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  41271. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  41272. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher =
  41273. test_params[i].client_ciphers;
  41274. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs =
  41275. test_params[i].client_sigalgs;
  41276. func_cb_client.method = test_params[i].client_meth;
  41277. func_cb_client.caPemFile = test_params[i].client_ca;
  41278. func_cb_client.ctx_ready =
  41279. test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready;
  41280. func_cb_server.ctx_ready =
  41281. test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready;
  41282. func_cb_server.ssl_ready = certClearCb; /* Reuse from prev test */
  41283. func_cb_server.method = test_params[i].server_meth;
  41284. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  41285. &func_cb_server, NULL), TEST_SUCCESS);
  41286. }
  41287. }
  41288. #endif
  41289. return EXPECT_RESULT();
  41290. }
  41291. static int test_wolfSSL_ciphersuite_auth(void)
  41292. {
  41293. EXPECT_DECLS;
  41294. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  41295. WOLFSSL_CIPHERSUITE_INFO info;
  41296. (void)info;
  41297. #ifndef WOLFSSL_NO_TLS12
  41298. #ifdef HAVE_CHACHA
  41299. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  41300. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256);
  41301. ExpectIntEQ(info.rsaAuth, 1);
  41302. ExpectIntEQ(info.eccAuth, 0);
  41303. ExpectIntEQ(info.eccStatic, 0);
  41304. ExpectIntEQ(info.psk, 0);
  41305. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  41306. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256);
  41307. ExpectIntEQ(info.rsaAuth, 0);
  41308. ExpectIntEQ(info.eccAuth, 1);
  41309. ExpectIntEQ(info.eccStatic, 0);
  41310. ExpectIntEQ(info.psk, 0);
  41311. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  41312. TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256);
  41313. ExpectIntEQ(info.rsaAuth, 0);
  41314. ExpectIntEQ(info.eccAuth, 0);
  41315. ExpectIntEQ(info.eccStatic, 0);
  41316. ExpectIntEQ(info.psk, 1);
  41317. #endif
  41318. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  41319. #ifndef NO_RSA
  41320. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  41321. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA);
  41322. ExpectIntEQ(info.rsaAuth, 1);
  41323. ExpectIntEQ(info.eccAuth, 0);
  41324. ExpectIntEQ(info.eccStatic, 0);
  41325. ExpectIntEQ(info.psk, 0);
  41326. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  41327. TLS_ECDH_RSA_WITH_AES_128_CBC_SHA);
  41328. ExpectIntEQ(info.rsaAuth, 1);
  41329. ExpectIntEQ(info.eccAuth, 0);
  41330. ExpectIntEQ(info.eccStatic, 1);
  41331. ExpectIntEQ(info.psk, 0);
  41332. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  41333. TLS_ECDH_RSA_WITH_AES_256_CBC_SHA);
  41334. ExpectIntEQ(info.rsaAuth, 1);
  41335. ExpectIntEQ(info.eccAuth, 0);
  41336. ExpectIntEQ(info.eccStatic, 1);
  41337. ExpectIntEQ(info.psk, 0);
  41338. #endif
  41339. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  41340. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA);
  41341. ExpectIntEQ(info.rsaAuth, 0);
  41342. ExpectIntEQ(info.eccAuth, 1);
  41343. ExpectIntEQ(info.eccStatic, 0);
  41344. ExpectIntEQ(info.psk, 0);
  41345. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  41346. TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA);
  41347. ExpectIntEQ(info.rsaAuth, 0);
  41348. ExpectIntEQ(info.eccAuth, 1);
  41349. ExpectIntEQ(info.eccStatic, 1);
  41350. ExpectIntEQ(info.psk, 0);
  41351. info = wolfSSL_get_ciphersuite_info(ECDHE_PSK_BYTE,
  41352. TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256);
  41353. ExpectIntEQ(info.rsaAuth, 0);
  41354. ExpectIntEQ(info.eccAuth, 0);
  41355. ExpectIntEQ(info.eccStatic, 0);
  41356. ExpectIntEQ(info.psk, 1);
  41357. #endif
  41358. #endif
  41359. #ifdef WOLFSSL_TLS13
  41360. info = wolfSSL_get_ciphersuite_info(TLS13_BYTE,
  41361. TLS_AES_128_GCM_SHA256);
  41362. ExpectIntEQ(info.rsaAuth, 0);
  41363. ExpectIntEQ(info.eccAuth, 0);
  41364. ExpectIntEQ(info.eccStatic, 0);
  41365. ExpectIntEQ(info.psk, 0);
  41366. #endif
  41367. #endif
  41368. return EXPECT_RESULT();
  41369. }
  41370. static int test_wolfSSL_sigalg_info(void)
  41371. {
  41372. EXPECT_DECLS;
  41373. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  41374. byte hashSigAlgo[WOLFSSL_MAX_SIGALGO];
  41375. word16 len = 0;
  41376. word16 idx = 0;
  41377. int allSigAlgs = SIG_ECDSA | SIG_RSA | SIG_SM2 | SIG_FALCON | SIG_DILITHIUM;
  41378. InitSuitesHashSigAlgo_ex2(hashSigAlgo, allSigAlgs, 1, 0xFFFFFFFF, &len);
  41379. for (idx = 0; idx < len; idx += 2) {
  41380. int hashAlgo = 0;
  41381. int sigAlgo = 0;
  41382. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  41383. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  41384. ExpectIntNE(hashAlgo, 0);
  41385. ExpectIntNE(sigAlgo, 0);
  41386. }
  41387. InitSuitesHashSigAlgo_ex2(hashSigAlgo, allSigAlgs | SIG_ANON, 1,
  41388. 0xFFFFFFFF, &len);
  41389. for (idx = 0; idx < len; idx += 2) {
  41390. int hashAlgo = 0;
  41391. int sigAlgo = 0;
  41392. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  41393. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  41394. ExpectIntNE(hashAlgo, 0);
  41395. }
  41396. #endif
  41397. return EXPECT_RESULT();
  41398. }
  41399. static int test_wolfSSL_SESSION(void)
  41400. {
  41401. EXPECT_DECLS;
  41402. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41403. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  41404. !defined(NO_SESSION_CACHE)
  41405. WOLFSSL* ssl = NULL;
  41406. WOLFSSL_CTX* ctx = NULL;
  41407. WOLFSSL_SESSION* sess = NULL;
  41408. WOLFSSL_SESSION* sess_copy = NULL;
  41409. #ifdef OPENSSL_EXTRA
  41410. #ifdef HAVE_EXT_CACHE
  41411. unsigned char* sessDer = NULL;
  41412. unsigned char* ptr = NULL;
  41413. int sz = 0;
  41414. #endif
  41415. const unsigned char context[] = "user app context";
  41416. unsigned int contextSz = (unsigned int)sizeof(context);
  41417. #endif
  41418. int ret = 0, err = 0;
  41419. SOCKET_T sockfd;
  41420. tcp_ready ready;
  41421. func_args server_args;
  41422. THREAD_TYPE serverThread;
  41423. char msg[80];
  41424. const char* sendGET = "GET";
  41425. /* TLS v1.3 requires session tickets */
  41426. /* CHACHA and POLY1305 required for myTicketEncCb */
  41427. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  41428. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  41429. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  41430. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  41431. #else
  41432. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  41433. #endif
  41434. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  41435. WOLFSSL_FILETYPE_PEM));
  41436. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  41437. WOLFSSL_FILETYPE_PEM));
  41438. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  41439. WOLFSSL_SUCCESS);
  41440. #ifdef WOLFSSL_ENCRYPTED_KEYS
  41441. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  41442. #endif
  41443. #ifdef HAVE_SESSION_TICKET
  41444. /* Use session tickets, for ticket tests below */
  41445. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  41446. #endif
  41447. XMEMSET(&server_args, 0, sizeof(func_args));
  41448. #ifdef WOLFSSL_TIRTOS
  41449. fdOpenSession(Task_self());
  41450. #endif
  41451. StartTCP();
  41452. InitTcpReady(&ready);
  41453. #if defined(USE_WINDOWS_API)
  41454. /* use RNG to get random port if using windows */
  41455. ready.port = GetRandomPort();
  41456. #endif
  41457. server_args.signal = &ready;
  41458. start_thread(test_server_nofail, &server_args, &serverThread);
  41459. wait_tcp_ready(&server_args);
  41460. /* client connection */
  41461. ExpectNotNull(ssl = wolfSSL_new(ctx));
  41462. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  41463. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  41464. #ifdef WOLFSSL_ASYNC_CRYPT
  41465. err = 0; /* Reset error */
  41466. #endif
  41467. do {
  41468. #ifdef WOLFSSL_ASYNC_CRYPT
  41469. if (err == WC_PENDING_E) {
  41470. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  41471. if (ret < 0) { break; } else if (ret == 0) { continue; }
  41472. }
  41473. #endif
  41474. ret = wolfSSL_connect(ssl);
  41475. err = wolfSSL_get_error(ssl, 0);
  41476. } while (err == WC_PENDING_E);
  41477. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  41478. #ifdef WOLFSSL_ASYNC_CRYPT
  41479. err = 0; /* Reset error */
  41480. #endif
  41481. do {
  41482. #ifdef WOLFSSL_ASYNC_CRYPT
  41483. if (err == WC_PENDING_E) {
  41484. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  41485. if (ret < 0) { break; } else if (ret == 0) { continue; }
  41486. }
  41487. #endif
  41488. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  41489. err = wolfSSL_get_error(ssl, 0);
  41490. } while (err == WC_PENDING_E);
  41491. ExpectIntEQ(ret, (int)XSTRLEN(sendGET));
  41492. #ifdef WOLFSSL_ASYNC_CRYPT
  41493. err = 0; /* Reset error */
  41494. #endif
  41495. do {
  41496. #ifdef WOLFSSL_ASYNC_CRYPT
  41497. if (err == WC_PENDING_E) {
  41498. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  41499. if (ret < 0) { break; } else if (ret == 0) { continue; }
  41500. }
  41501. #endif
  41502. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  41503. err = wolfSSL_get_error(ssl, 0);
  41504. } while (err == WC_PENDING_E);
  41505. ExpectIntEQ(ret, 23);
  41506. ExpectPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  41507. ExpectPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  41508. ExpectIntEQ(wolfSSL_SessionIsSetup(sess), 1);
  41509. #ifdef HAVE_EXT_CACHE
  41510. ExpectPtrEq(sess, sess_copy); /* they should be the same pointer but without
  41511. * HAVE_EXT_CACHE we get new objects each time */
  41512. #endif
  41513. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  41514. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  41515. sess = wolfSSL_get_session(ssl);
  41516. #ifdef OPENSSL_EXTRA
  41517. ExpectIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  41518. ExpectIntEQ(SSL_SESSION_is_resumable(sess), 1);
  41519. ExpectIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  41520. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  41521. #ifdef HAVE_SESSION_TICKET
  41522. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  41523. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  41524. SESSION_TICKET_HINT_DEFAULT);
  41525. #else
  41526. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  41527. #endif
  41528. #else
  41529. (void)sess;
  41530. #endif /* OPENSSL_EXTRA */
  41531. /* Retain copy of the session for later testing */
  41532. ExpectNotNull(sess = wolfSSL_get1_session(ssl));
  41533. wolfSSL_shutdown(ssl);
  41534. wolfSSL_free(ssl); ssl = NULL;
  41535. CloseSocket(sockfd);
  41536. join_thread(serverThread);
  41537. FreeTcpReady(&ready);
  41538. #ifdef WOLFSSL_TIRTOS
  41539. fdOpenSession(Task_self());
  41540. #endif
  41541. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  41542. {
  41543. X509 *x509 = NULL;
  41544. char buf[30];
  41545. int bufSz = 0;
  41546. ExpectNotNull(x509 = SSL_SESSION_get0_peer(sess));
  41547. ExpectIntGT((bufSz = X509_NAME_get_text_by_NID(
  41548. X509_get_subject_name(x509), NID_organizationalUnitName, buf,
  41549. sizeof(buf))), 0);
  41550. ExpectIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  41551. if (bufSz == 7) {
  41552. ExpectIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  41553. }
  41554. if (bufSz == 16) {
  41555. ExpectIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  41556. }
  41557. }
  41558. #endif
  41559. #ifdef HAVE_EXT_CACHE
  41560. ExpectNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  41561. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  41562. sess_copy = NULL;
  41563. #endif
  41564. #if defined(OPENSSL_EXTRA) && defined(HAVE_EXT_CACHE)
  41565. /* get session from DER and update the timeout */
  41566. ExpectIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  41567. ExpectIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  41568. wolfSSL_SESSION_free(sess); sess = NULL;
  41569. sess = NULL;
  41570. ptr = sessDer;
  41571. ExpectNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  41572. ExpectNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  41573. (const unsigned char**)&ptr, sz));
  41574. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  41575. sessDer = NULL;
  41576. ExpectIntGT(wolfSSL_SESSION_get_time(sess), 0);
  41577. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  41578. #endif
  41579. /* successful set session test */
  41580. ExpectNotNull(ssl = wolfSSL_new(ctx));
  41581. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  41582. #ifdef HAVE_SESSION_TICKET
  41583. /* Test set/get session ticket */
  41584. {
  41585. const char* ticket = "This is a session ticket";
  41586. char buf[64] = {0};
  41587. word32 bufSz = (word32)sizeof(buf);
  41588. ExpectIntEQ(SSL_SUCCESS,
  41589. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  41590. (word32)XSTRLEN(ticket)));
  41591. ExpectIntEQ(SSL_SUCCESS,
  41592. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  41593. ExpectStrEQ(ticket, buf);
  41594. }
  41595. #endif
  41596. #ifdef OPENSSL_EXTRA
  41597. /* session timeout case */
  41598. /* make the session to be expired */
  41599. ExpectIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  41600. XSLEEP_MS(1200);
  41601. /* SSL_set_session should reject specified session but return success
  41602. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  41603. */
  41604. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  41605. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  41606. #else
  41607. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  41608. #endif
  41609. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  41610. #ifdef WOLFSSL_SESSION_ID_CTX
  41611. /* fail case with miss match session context IDs (use compatibility API) */
  41612. ExpectIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  41613. SSL_SUCCESS);
  41614. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  41615. wolfSSL_free(ssl); ssl = NULL;
  41616. ExpectIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  41617. SSL_FAILURE);
  41618. ExpectIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  41619. SSL_SUCCESS);
  41620. ExpectNotNull(ssl = wolfSSL_new(ctx));
  41621. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  41622. #endif
  41623. #endif /* OPENSSL_EXTRA */
  41624. wolfSSL_free(ssl);
  41625. wolfSSL_SESSION_free(sess);
  41626. wolfSSL_CTX_free(ctx);
  41627. #endif
  41628. return EXPECT_RESULT();
  41629. }
  41630. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41631. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  41632. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  41633. !defined(WOLFSSL_NO_TLS12)
  41634. static WOLFSSL_SESSION* test_wolfSSL_SESSION_expire_sess = NULL;
  41635. static void test_wolfSSL_SESSION_expire_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  41636. {
  41637. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  41638. /* returns previous timeout value */
  41639. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), 500);
  41640. #else
  41641. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), WOLFSSL_SUCCESS);
  41642. #endif
  41643. }
  41644. /* set the session to timeout in a second */
  41645. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready(WOLFSSL* ssl)
  41646. {
  41647. AssertIntEQ(wolfSSL_set_timeout(ssl, 2), 1);
  41648. }
  41649. /* store the client side session from the first successful connection */
  41650. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result(WOLFSSL* ssl)
  41651. {
  41652. AssertPtrNE((test_wolfSSL_SESSION_expire_sess = wolfSSL_get1_session(ssl)),
  41653. NULL); /* ref count 1 */
  41654. }
  41655. /* wait till session is expired then set it in the WOLFSSL struct for use */
  41656. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait(WOLFSSL* ssl)
  41657. {
  41658. AssertIntEQ(wolfSSL_set_timeout(ssl, 1), 1);
  41659. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  41660. WOLFSSL_SUCCESS);
  41661. XSLEEP_MS(2000); /* wait 2 seconds for session to expire */
  41662. }
  41663. /* set expired session in the WOLFSSL struct for use */
  41664. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set(WOLFSSL* ssl)
  41665. {
  41666. XSLEEP_MS(1200); /* wait a second for session to expire */
  41667. /* set the expired session, call to set session fails but continuing on
  41668. after failure should be handled here */
  41669. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL)
  41670. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  41671. WOLFSSL_SUCCESS);
  41672. #else
  41673. AssertIntNE(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  41674. WOLFSSL_SUCCESS);
  41675. #endif
  41676. }
  41677. /* check that the expired session was not reused */
  41678. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse(WOLFSSL* ssl)
  41679. {
  41680. /* since the session has expired it should not have been reused */
  41681. AssertIntEQ(wolfSSL_session_reused(ssl), 0);
  41682. }
  41683. #endif
  41684. static int test_wolfSSL_SESSION_expire_downgrade(void)
  41685. {
  41686. EXPECT_DECLS;
  41687. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41688. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  41689. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  41690. !defined(WOLFSSL_NO_TLS12)
  41691. WOLFSSL_CTX* ctx = NULL;
  41692. callback_functions server_cbf, client_cbf;
  41693. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  41694. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  41695. /* force server side to use TLS 1.2 */
  41696. server_cbf.ctx = ctx;
  41697. server_cbf.method = wolfTLSv1_2_server_method;
  41698. client_cbf.method = wolfSSLv23_client_method;
  41699. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  41700. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready;
  41701. client_cbf.on_result = test_wolfSSL_SESSION_expire_downgrade_ssl_result;
  41702. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  41703. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  41704. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  41705. /* set the previously created session and wait till expired */
  41706. server_cbf.ctx = ctx;
  41707. client_cbf.method = wolfSSLv23_client_method;
  41708. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  41709. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait;
  41710. client_cbf.on_result =
  41711. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  41712. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  41713. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  41714. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  41715. /* set the previously created expired session */
  41716. server_cbf.ctx = ctx;
  41717. client_cbf.method = wolfSSLv23_client_method;
  41718. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  41719. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set;
  41720. client_cbf.on_result =
  41721. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  41722. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  41723. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  41724. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  41725. wolfSSL_SESSION_free(test_wolfSSL_SESSION_expire_sess);
  41726. wolfSSL_CTX_free(ctx);
  41727. #endif
  41728. return EXPECT_RESULT();
  41729. }
  41730. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  41731. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  41732. static int clientSessRemCountMalloc = 0;
  41733. static int serverSessRemCountMalloc = 0;
  41734. static int clientSessRemCountFree = 0;
  41735. static int serverSessRemCountFree = 0;
  41736. static WOLFSSL_CTX* serverSessCtx = NULL;
  41737. static WOLFSSL_SESSION* serverSess = NULL;
  41738. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  41739. !defined(NO_SESSION_CACHE_REF)
  41740. static WOLFSSL_CTX* clientSessCtx = NULL;
  41741. static WOLFSSL_SESSION* clientSess = NULL;
  41742. #endif
  41743. static int serverSessRemIdx = 3;
  41744. static int sessRemCtx_Server = WOLFSSL_SERVER_END;
  41745. static int sessRemCtx_Client = WOLFSSL_CLIENT_END;
  41746. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  41747. {
  41748. int* side;
  41749. (void)ctx;
  41750. side = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  41751. if (side != NULL) {
  41752. if (*side == WOLFSSL_CLIENT_END)
  41753. clientSessRemCountFree++;
  41754. else
  41755. serverSessRemCountFree++;
  41756. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  41757. }
  41758. }
  41759. static int SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  41760. {
  41761. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  41762. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  41763. !defined(NO_SESSION_CACHE_REF)
  41764. {
  41765. EXPECT_DECLS;
  41766. /* Allow downgrade, set min version, and disable TLS 1.3.
  41767. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  41768. * reference to the session cache. But with WOLFSSL_TLS13 and without
  41769. * HAVE_SESSION_TICKET we won't have a session ID to be able to place
  41770. * the session in the cache. In this case we need to downgrade to
  41771. * previous versions to just use the legacy session ID field. */
  41772. ExpectIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  41773. SSL_SUCCESS);
  41774. ExpectIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  41775. SSL_SUCCESS);
  41776. return EXPECT_RESULT();
  41777. }
  41778. #else
  41779. return TEST_SUCCESS;
  41780. #endif
  41781. }
  41782. static int SessRemSslSetupCb(WOLFSSL* ssl)
  41783. {
  41784. EXPECT_DECLS;
  41785. int* side;
  41786. if (SSL_is_server(ssl)) {
  41787. side = &sessRemCtx_Server;
  41788. serverSessRemCountMalloc++;
  41789. ExpectNotNull(serverSess = SSL_get1_session(ssl));
  41790. ExpectIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  41791. SSL_SUCCESS);
  41792. }
  41793. else {
  41794. side = &sessRemCtx_Client;
  41795. clientSessRemCountMalloc++;
  41796. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  41797. !defined(NO_SESSION_CACHE_REF)
  41798. ExpectNotNull(clientSess = SSL_get1_session(ssl));
  41799. ExpectIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  41800. SSL_SUCCESS);
  41801. #endif
  41802. }
  41803. ExpectIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl),
  41804. serverSessRemIdx, side), SSL_SUCCESS);
  41805. return EXPECT_RESULT();
  41806. }
  41807. #endif
  41808. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  41809. {
  41810. EXPECT_DECLS;
  41811. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  41812. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  41813. /* Check that the remove callback gets called for external data in a
  41814. * session object */
  41815. test_ssl_cbf func_cb;
  41816. XMEMSET(&func_cb, 0, sizeof(func_cb));
  41817. func_cb.ctx_ready = SessRemCtxSetupCb;
  41818. func_cb.on_result = SessRemSslSetupCb;
  41819. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb, &func_cb,
  41820. NULL), TEST_SUCCESS);
  41821. /* Both should have been allocated */
  41822. ExpectIntEQ(clientSessRemCountMalloc, 1);
  41823. ExpectIntEQ(serverSessRemCountMalloc, 1);
  41824. /* This should not be called yet. Session wasn't evicted from cache yet. */
  41825. ExpectIntEQ(clientSessRemCountFree, 0);
  41826. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  41827. !defined(NO_SESSION_CACHE_REF)
  41828. /* Force a cache lookup */
  41829. ExpectNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  41830. /* Force a cache update */
  41831. ExpectNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  41832. /* This should set the timeout to 0 and call the remove callback from within
  41833. * the session cache. */
  41834. ExpectIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  41835. ExpectNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  41836. ExpectIntEQ(clientSessRemCountFree, 1);
  41837. #endif
  41838. /* Server session is in the cache so ex_data isn't free'd with the SSL
  41839. * object */
  41840. ExpectIntEQ(serverSessRemCountFree, 0);
  41841. /* Force a cache lookup */
  41842. ExpectNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  41843. /* Force a cache update */
  41844. ExpectNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  41845. /* This should set the timeout to 0 and call the remove callback from within
  41846. * the session cache. */
  41847. ExpectIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  41848. ExpectNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  41849. ExpectIntEQ(serverSessRemCountFree, 1);
  41850. /* Need to free the references that we kept */
  41851. SSL_CTX_free(serverSessCtx);
  41852. SSL_SESSION_free(serverSess);
  41853. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  41854. !defined(NO_SESSION_CACHE_REF)
  41855. SSL_CTX_free(clientSessCtx);
  41856. SSL_SESSION_free(clientSess);
  41857. #endif
  41858. #endif
  41859. return EXPECT_RESULT();
  41860. }
  41861. static int test_wolfSSL_ticket_keys(void)
  41862. {
  41863. EXPECT_DECLS;
  41864. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  41865. !defined(NO_WOLFSSL_SERVER)
  41866. WOLFSSL_CTX* ctx = NULL;
  41867. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  41868. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  41869. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  41870. WOLFSSL_FAILURE);
  41871. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  41872. WOLFSSL_FAILURE);
  41873. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  41874. WOLFSSL_FAILURE);
  41875. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  41876. WOLFSSL_FAILURE);
  41877. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  41878. WOLFSSL_FAILURE);
  41879. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  41880. WOLFSSL_FAILURE);
  41881. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  41882. WOLFSSL_FAILURE);
  41883. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  41884. WOLFSSL_FAILURE);
  41885. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  41886. WOLFSSL_FAILURE);
  41887. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  41888. WOLFSSL_FAILURE);
  41889. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  41890. WOLFSSL_FAILURE);
  41891. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  41892. WOLFSSL_FAILURE);
  41893. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  41894. WOLFSSL_FAILURE);
  41895. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  41896. WOLFSSL_FAILURE);
  41897. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  41898. WOLFSSL_SUCCESS);
  41899. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  41900. WOLFSSL_SUCCESS);
  41901. wolfSSL_CTX_free(ctx);
  41902. #endif
  41903. return EXPECT_RESULT();
  41904. }
  41905. #ifndef NO_BIO
  41906. static int test_wolfSSL_d2i_PUBKEY(void)
  41907. {
  41908. EXPECT_DECLS;
  41909. #if defined(OPENSSL_EXTRA)
  41910. BIO* bio = NULL;
  41911. EVP_PKEY* pkey = NULL;
  41912. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  41913. ExpectNull(d2i_PUBKEY_bio(NULL, NULL));
  41914. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  41915. /* RSA PUBKEY test */
  41916. ExpectIntGT(BIO_write(bio, client_keypub_der_2048,
  41917. sizeof_client_keypub_der_2048), 0);
  41918. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  41919. EVP_PKEY_free(pkey);
  41920. pkey = NULL;
  41921. #endif
  41922. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  41923. /* ECC PUBKEY test */
  41924. ExpectIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  41925. sizeof_ecc_clikeypub_der_256), 0);
  41926. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  41927. EVP_PKEY_free(pkey);
  41928. pkey = NULL;
  41929. #endif
  41930. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  41931. /* DSA PUBKEY test */
  41932. ExpectIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  41933. sizeof_dsa_pub_key_der_2048), 0);
  41934. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  41935. EVP_PKEY_free(pkey);
  41936. pkey = NULL;
  41937. #endif
  41938. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  41939. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  41940. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  41941. (HAVE_FIPS_VERSION > 2))
  41942. /* DH PUBKEY test */
  41943. ExpectIntGT(BIO_write(bio, dh_pub_key_der_2048,
  41944. sizeof_dh_pub_key_der_2048), 0);
  41945. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  41946. EVP_PKEY_free(pkey);
  41947. pkey = NULL;
  41948. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  41949. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  41950. BIO_free(bio);
  41951. (void)pkey;
  41952. #endif
  41953. return EXPECT_RESULT();
  41954. }
  41955. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  41956. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  41957. {
  41958. EXPECT_DECLS;
  41959. BIO* bio = NULL;
  41960. EVP_PKEY* pkey = NULL;
  41961. WOLFSSL_CTX* ctx = NULL;
  41962. #if defined(WOLFSSL_KEY_GEN)
  41963. unsigned char buff[4096];
  41964. unsigned char* bufPtr = buff;
  41965. #endif
  41966. /* test creating new EVP_PKEY with bad arg */
  41967. ExpectNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  41968. /* test loading RSA key using BIO */
  41969. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  41970. {
  41971. XFILE file = XBADFILE;
  41972. const char* fname = "./certs/server-key.der";
  41973. size_t sz = 0;
  41974. byte* buf = NULL;
  41975. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  41976. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  41977. ExpectTrue((sz = XFTELL(file)) != 0);
  41978. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  41979. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  41980. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  41981. if (file != XBADFILE) {
  41982. XFCLOSE(file);
  41983. }
  41984. /* Test using BIO new mem and loading DER private key */
  41985. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  41986. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  41987. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  41988. BIO_free(bio);
  41989. bio = NULL;
  41990. EVP_PKEY_free(pkey);
  41991. pkey = NULL;
  41992. }
  41993. #endif
  41994. /* test loading ECC key using BIO */
  41995. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  41996. {
  41997. XFILE file = XBADFILE;
  41998. const char* fname = "./certs/ecc-key.der";
  41999. size_t sz = 0;
  42000. byte* buf = NULL;
  42001. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  42002. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  42003. ExpectTrue((sz = XFTELL(file)) != 0);
  42004. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  42005. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  42006. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  42007. if (file != XBADFILE)
  42008. XFCLOSE(file);
  42009. /* Test using BIO new mem and loading DER private key */
  42010. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  42011. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  42012. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  42013. BIO_free(bio);
  42014. bio = NULL;
  42015. EVP_PKEY_free(pkey);
  42016. pkey = NULL;
  42017. }
  42018. #endif
  42019. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  42020. #ifndef NO_WOLFSSL_SERVER
  42021. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  42022. #else
  42023. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  42024. #endif
  42025. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  42026. {
  42027. RSA* rsa = NULL;
  42028. /* Tests bad parameters */
  42029. ExpectNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  42030. /* RSA not set yet, expecting to fail*/
  42031. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  42032. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  42033. /* set RSA using bio*/
  42034. ExpectIntGT(BIO_write(bio, client_key_der_2048,
  42035. sizeof_client_key_der_2048), 0);
  42036. ExpectNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  42037. ExpectNotNull(rsa);
  42038. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  42039. /* i2d RSAprivate key tests */
  42040. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  42041. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  42042. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  42043. sizeof_client_key_der_2048);
  42044. bufPtr -= sizeof_client_key_der_2048;
  42045. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  42046. sizeof_client_key_der_2048), 0);
  42047. bufPtr = NULL;
  42048. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  42049. sizeof_client_key_der_2048);
  42050. ExpectNotNull(bufPtr);
  42051. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  42052. sizeof_client_key_der_2048), 0);
  42053. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  42054. RSA_free(rsa);
  42055. rsa = RSA_new();
  42056. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  42057. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  42058. RSA_free(rsa);
  42059. }
  42060. #endif /* WOLFSSL_KEY_GEN && !NO_RSA */
  42061. SSL_CTX_free(ctx);
  42062. ctx = NULL;
  42063. BIO_free(bio);
  42064. bio = NULL;
  42065. return EXPECT_RESULT();
  42066. }
  42067. #endif /* OPENSSL_ALL || (WOLFSSL_ASIO && !NO_RSA) */
  42068. #endif /* !NO_BIO */
  42069. static int test_wolfSSL_sk_GENERAL_NAME(void)
  42070. {
  42071. EXPECT_DECLS;
  42072. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  42073. !defined(NO_RSA)
  42074. X509* x509 = NULL;
  42075. GENERAL_NAME* gn = NULL;
  42076. unsigned char buf[4096];
  42077. const unsigned char* bufPt = NULL;
  42078. int bytes = 0;
  42079. int i;
  42080. int j;
  42081. XFILE f = XBADFILE;
  42082. STACK_OF(GENERAL_NAME)* sk = NULL;
  42083. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  42084. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  42085. if (f != XBADFILE)
  42086. XFCLOSE(f);
  42087. for (j = 0; j < 2; ++j) {
  42088. bufPt = buf;
  42089. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  42090. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  42091. NID_subject_alt_name, NULL, NULL));
  42092. ExpectIntEQ(sk_GENERAL_NAME_num(sk), 1);
  42093. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  42094. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  42095. if (gn != NULL) {
  42096. switch (gn->type) {
  42097. case GEN_DNS:
  42098. fprintf(stderr, "found type GEN_DNS\n");
  42099. break;
  42100. case GEN_EMAIL:
  42101. fprintf(stderr, "found type GEN_EMAIL\n");
  42102. break;
  42103. case GEN_URI:
  42104. fprintf(stderr, "found type GEN_URI\n");
  42105. break;
  42106. }
  42107. }
  42108. }
  42109. X509_free(x509);
  42110. x509 = NULL;
  42111. if (j == 0) {
  42112. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  42113. }
  42114. else {
  42115. /*
  42116. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  42117. * it was supposed to. This is a regression test for that bug.
  42118. */
  42119. GENERAL_NAMES_free(sk);
  42120. }
  42121. sk = NULL;
  42122. }
  42123. #endif
  42124. return EXPECT_RESULT();
  42125. }
  42126. static int test_wolfSSL_GENERAL_NAME_print(void)
  42127. {
  42128. EXPECT_DECLS;
  42129. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  42130. X509* x509 = NULL;
  42131. GENERAL_NAME* gn = NULL;
  42132. unsigned char buf[4096];
  42133. const unsigned char* bufPt = NULL;
  42134. int bytes;
  42135. XFILE f = XBADFILE;
  42136. STACK_OF(GENERAL_NAME)* sk = NULL;
  42137. BIO* out = NULL;
  42138. unsigned char outbuf[128];
  42139. X509_EXTENSION* ext = NULL;
  42140. AUTHORITY_INFO_ACCESS* aia = NULL;
  42141. ACCESS_DESCRIPTION* ad = NULL;
  42142. ASN1_IA5STRING *dnsname = NULL;
  42143. const unsigned char v4Addr[] = {192,168,53,1};
  42144. const unsigned char v6Addr[] =
  42145. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  42146. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  42147. const unsigned char email[] =
  42148. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  42149. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  42150. const char* dnsStr = "DNS:example.com";
  42151. const char* uriStr = "URI:http://127.0.0.1:22220";
  42152. const char* v4addStr = "IP Address:192.168.53.1";
  42153. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  42154. const char* emailStr = "email:info@wolfssl.com";
  42155. const char* othrStr = "othername:<unsupported>";
  42156. const char* x400Str = "X400Name:<unsupported>";
  42157. const char* ediStr = "EdiPartyName:<unsupported>";
  42158. /* BIO to output */
  42159. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  42160. /* test for NULL param */
  42161. gn = NULL;
  42162. ExpectIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  42163. ExpectIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  42164. ExpectIntEQ(GENERAL_NAME_print(out, NULL), 0);
  42165. /* test for GEN_DNS */
  42166. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  42167. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  42168. if (f != XBADFILE) {
  42169. XFCLOSE(f);
  42170. f = XBADFILE;
  42171. }
  42172. bufPt = buf;
  42173. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  42174. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  42175. NID_subject_alt_name, NULL, NULL));
  42176. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  42177. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42178. XMEMSET(outbuf, 0, sizeof(outbuf));
  42179. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42180. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  42181. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  42182. gn = NULL;
  42183. sk = NULL;
  42184. X509_free(x509);
  42185. x509 = NULL;
  42186. /* Lets test for setting as well. */
  42187. ExpectNotNull(gn = GENERAL_NAME_new());
  42188. ExpectNotNull(dnsname = ASN1_IA5STRING_new());
  42189. ExpectIntEQ(ASN1_STRING_set(dnsname, "example.com", -1), 1);
  42190. GENERAL_NAME_set0_value(gn, GEN_DNS, dnsname);
  42191. dnsname = NULL;
  42192. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42193. XMEMSET(outbuf, 0, sizeof(outbuf));
  42194. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42195. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  42196. GENERAL_NAME_free(gn);
  42197. /* test for GEN_URI */
  42198. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  42199. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  42200. if (f != XBADFILE) {
  42201. XFCLOSE(f);
  42202. f = XBADFILE;
  42203. }
  42204. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  42205. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  42206. ext));
  42207. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0));
  42208. if (ad != NULL) {
  42209. gn = ad->location;
  42210. }
  42211. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42212. gn = NULL;
  42213. XMEMSET(outbuf,0,sizeof(outbuf));
  42214. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42215. ExpectIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  42216. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  42217. aia = NULL;
  42218. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  42219. ExpectNotNull(aia);
  42220. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  42221. aia = NULL;
  42222. X509_free(x509);
  42223. x509 = NULL;
  42224. /* test for GEN_IPADD */
  42225. /* ip v4 address */
  42226. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42227. if (gn != NULL) {
  42228. gn->type = GEN_IPADD;
  42229. if (gn->d.iPAddress != NULL) {
  42230. gn->d.iPAddress->length = sizeof(v4Addr);
  42231. }
  42232. }
  42233. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  42234. sizeof(v4Addr)), 1);
  42235. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42236. XMEMSET(outbuf,0,sizeof(outbuf));
  42237. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42238. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  42239. GENERAL_NAME_free(gn);
  42240. gn = NULL;
  42241. /* ip v6 address */
  42242. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42243. if (gn != NULL) {
  42244. gn->type = GEN_IPADD;
  42245. if (gn->d.iPAddress != NULL) {
  42246. gn->d.iPAddress->length = sizeof(v6Addr);
  42247. }
  42248. }
  42249. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  42250. sizeof(v6Addr)), 1);
  42251. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42252. XMEMSET(outbuf,0,sizeof(outbuf));
  42253. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42254. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  42255. GENERAL_NAME_free(gn);
  42256. gn = NULL;
  42257. /* test for GEN_EMAIL */
  42258. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42259. if (gn != NULL) {
  42260. gn->type = GEN_EMAIL;
  42261. if (gn->d.rfc822Name != NULL) {
  42262. gn->d.rfc822Name->length = sizeof(email);
  42263. }
  42264. }
  42265. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email, sizeof(email)),
  42266. 1);
  42267. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42268. XMEMSET(outbuf,0,sizeof(outbuf));
  42269. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42270. ExpectIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  42271. GENERAL_NAME_free(gn);
  42272. gn = NULL;
  42273. /* test for GEN_OTHERNAME */
  42274. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42275. if (gn != NULL) {
  42276. gn->type = GEN_OTHERNAME;
  42277. }
  42278. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42279. XMEMSET(outbuf,0,sizeof(outbuf));
  42280. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42281. ExpectIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  42282. GENERAL_NAME_free(gn);
  42283. gn = NULL;
  42284. /* test for GEN_X400 */
  42285. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42286. if (gn != NULL) {
  42287. gn->type = GEN_X400;
  42288. }
  42289. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42290. XMEMSET(outbuf,0,sizeof(outbuf));
  42291. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42292. ExpectIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  42293. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  42294. if (gn != NULL) {
  42295. gn->type = GEN_IA5;
  42296. }
  42297. GENERAL_NAME_free(gn);
  42298. gn = NULL;
  42299. /* test for GEN_EDIPARTY */
  42300. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42301. if (gn != NULL) {
  42302. gn->type = GEN_EDIPARTY;
  42303. }
  42304. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42305. XMEMSET(outbuf,0,sizeof(outbuf));
  42306. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42307. ExpectIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  42308. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  42309. if (gn != NULL) {
  42310. gn->type = GEN_IA5;
  42311. }
  42312. GENERAL_NAME_free(gn);
  42313. gn = NULL;
  42314. BIO_free(out);
  42315. #endif /* OPENSSL_ALL */
  42316. return EXPECT_RESULT();
  42317. }
  42318. static int test_wolfSSL_sk_DIST_POINT(void)
  42319. {
  42320. EXPECT_DECLS;
  42321. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  42322. !defined(NO_RSA)
  42323. X509* x509 = NULL;
  42324. unsigned char buf[4096];
  42325. const unsigned char* bufPt;
  42326. int bytes = 0;
  42327. int i = 0;
  42328. int j = 0;
  42329. XFILE f = XBADFILE;
  42330. DIST_POINT* dp = NULL;
  42331. DIST_POINT_NAME* dpn = NULL;
  42332. GENERAL_NAME* gn = NULL;
  42333. ASN1_IA5STRING* uri = NULL;
  42334. STACK_OF(DIST_POINT)* dps = NULL;
  42335. STACK_OF(GENERAL_NAME)* gns = NULL;
  42336. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  42337. ExpectTrue((f = XFOPEN(cliCertDerCrlDistPoint, "rb")) != XBADFILE);
  42338. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  42339. if (f != XBADFILE)
  42340. XFCLOSE(f);
  42341. bufPt = buf;
  42342. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  42343. ExpectNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  42344. NID_crl_distribution_points, NULL, NULL));
  42345. ExpectIntEQ(sk_DIST_POINT_num(dps), 1);
  42346. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  42347. ExpectNotNull(dp = sk_DIST_POINT_value(dps, i));
  42348. ExpectNotNull(dpn = dp->distpoint);
  42349. /* this should be type 0, fullname */
  42350. ExpectIntEQ(dpn->type, 0);
  42351. ExpectNotNull(gns = dp->distpoint->name.fullname);
  42352. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  42353. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  42354. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, j));
  42355. ExpectIntEQ(gn->type, GEN_URI);
  42356. ExpectNotNull(uri = gn->d.uniformResourceIdentifier);
  42357. ExpectNotNull(uri->data);
  42358. ExpectIntGT(uri->length, 0);
  42359. }
  42360. }
  42361. X509_free(x509);
  42362. CRL_DIST_POINTS_free(dps);
  42363. #endif
  42364. return EXPECT_RESULT();
  42365. }
  42366. static int test_wolfSSL_verify_mode(void)
  42367. {
  42368. EXPECT_DECLS;
  42369. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  42370. WOLFSSL* ssl = NULL;
  42371. WOLFSSL_CTX* ctx = NULL;
  42372. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42373. ExpectNotNull(ssl = SSL_new(ctx));
  42374. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  42375. SSL_free(ssl);
  42376. ssl = NULL;
  42377. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  42378. ExpectNotNull(ssl = SSL_new(ctx));
  42379. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  42380. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  42381. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  42382. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  42383. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  42384. SSL_free(ssl);
  42385. ssl = NULL;
  42386. wolfSSL_CTX_set_verify(ctx,
  42387. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  42388. ExpectNotNull(ssl = SSL_new(ctx));
  42389. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  42390. ExpectIntEQ(SSL_get_verify_mode(ssl),
  42391. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  42392. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  42393. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  42394. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  42395. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  42396. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  42397. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  42398. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  42399. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  42400. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  42401. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  42402. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  42403. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  42404. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  42405. #endif
  42406. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  42407. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  42408. SSL_free(ssl);
  42409. SSL_CTX_free(ctx);
  42410. #endif
  42411. return EXPECT_RESULT();
  42412. }
  42413. static int test_wolfSSL_verify_depth(void)
  42414. {
  42415. EXPECT_DECLS;
  42416. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  42417. WOLFSSL* ssl = NULL;
  42418. WOLFSSL_CTX* ctx = NULL;
  42419. long depth;
  42420. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42421. ExpectIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  42422. ExpectNotNull(ssl = SSL_new(ctx));
  42423. ExpectIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  42424. SSL_free(ssl);
  42425. ssl = NULL;
  42426. SSL_CTX_set_verify_depth(ctx, -1);
  42427. ExpectIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  42428. SSL_CTX_set_verify_depth(ctx, 2);
  42429. ExpectIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  42430. ExpectNotNull(ssl = SSL_new(ctx));
  42431. ExpectIntEQ(2, SSL_get_verify_depth(ssl));
  42432. SSL_free(ssl);
  42433. SSL_CTX_free(ctx);
  42434. #endif
  42435. return EXPECT_RESULT();
  42436. }
  42437. static int test_wolfSSL_verify_result(void)
  42438. {
  42439. EXPECT_DECLS;
  42440. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  42441. defined(OPENSSL_ALL)) && !defined(NO_WOLFSSL_CLIENT)
  42442. WOLFSSL* ssl = NULL;
  42443. WOLFSSL_CTX* ctx = NULL;
  42444. long result = 0xDEADBEEF;
  42445. ExpectIntEQ(WOLFSSL_FAILURE, wolfSSL_get_verify_result(ssl));
  42446. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42447. ExpectNotNull(ssl = SSL_new(ctx));
  42448. wolfSSL_set_verify_result(ssl, result);
  42449. ExpectIntEQ(result, wolfSSL_get_verify_result(ssl));
  42450. SSL_free(ssl);
  42451. SSL_CTX_free(ctx);
  42452. #endif
  42453. return EXPECT_RESULT();
  42454. }
  42455. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  42456. static void sslMsgCb(int w, int version, int type, const void* buf,
  42457. size_t sz, SSL* ssl, void* arg)
  42458. {
  42459. int i;
  42460. unsigned char* pt = (unsigned char*)buf;
  42461. fprintf(stderr, "%s %d bytes of version %d , type %d : ",
  42462. (w)?"Writing":"Reading", (int)sz, version, type);
  42463. for (i = 0; i < (int)sz; i++) fprintf(stderr, "%02X", pt[i]);
  42464. fprintf(stderr, "\n");
  42465. (void)ssl;
  42466. (void)arg;
  42467. }
  42468. #endif /* OPENSSL_EXTRA */
  42469. static int test_wolfSSL_msg_callback(void)
  42470. {
  42471. EXPECT_DECLS;
  42472. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  42473. WOLFSSL* ssl = NULL;
  42474. WOLFSSL_CTX* ctx = NULL;
  42475. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42476. ExpectNotNull(ssl = SSL_new(ctx));
  42477. ExpectIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  42478. ExpectIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  42479. ExpectIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  42480. SSL_free(ssl);
  42481. SSL_CTX_free(ctx);
  42482. #endif
  42483. return EXPECT_RESULT();
  42484. }
  42485. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  42486. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  42487. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  42488. static void binary_dump(void *ptr, int size)
  42489. {
  42490. #ifdef WOLFSSL_EVP_PRINT
  42491. int i = 0;
  42492. unsigned char *p = (unsigned char *) ptr;
  42493. fprintf(stderr, "{");
  42494. while ((p != NULL) && (i < size)) {
  42495. if ((i % 8) == 0) {
  42496. fprintf(stderr, "\n");
  42497. fprintf(stderr, " ");
  42498. }
  42499. fprintf(stderr, "0x%02x, ", p[i]);
  42500. i++;
  42501. }
  42502. fprintf(stderr, "\n};\n");
  42503. #else
  42504. (void) ptr;
  42505. (void) size;
  42506. #endif
  42507. }
  42508. static int last_val = 0x0f;
  42509. static int check_result(unsigned char *data, int len)
  42510. {
  42511. int i;
  42512. for ( ; len; ) {
  42513. last_val = (last_val + 1) % 16;
  42514. for (i = 0; i < 16; len--, i++, data++)
  42515. if (*data != last_val) {
  42516. return -1;
  42517. }
  42518. }
  42519. return 0;
  42520. }
  42521. static int r_offset;
  42522. static int w_offset;
  42523. static void init_offset(void)
  42524. {
  42525. r_offset = 0;
  42526. w_offset = 0;
  42527. }
  42528. static void get_record(unsigned char *data, unsigned char *buf, int len)
  42529. {
  42530. XMEMCPY(buf, data+r_offset, len);
  42531. r_offset += len;
  42532. }
  42533. static void set_record(unsigned char *data, unsigned char *buf, int len)
  42534. {
  42535. XMEMCPY(data+w_offset, buf, len);
  42536. w_offset += len;
  42537. }
  42538. static void set_plain(unsigned char *plain, int rec)
  42539. {
  42540. int i, j;
  42541. unsigned char *p = plain;
  42542. #define BLOCKSZ 16
  42543. for (i=0; i<(rec/BLOCKSZ); i++) {
  42544. for (j=0; j<BLOCKSZ; j++)
  42545. *p++ = (i % 16);
  42546. }
  42547. }
  42548. #endif
  42549. static int test_wolfSSL_EVP_Cipher_extra(void)
  42550. {
  42551. EXPECT_DECLS;
  42552. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  42553. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  42554. /* aes128-cbc, keylen=16, ivlen=16 */
  42555. byte aes128_cbc_key[] = {
  42556. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  42557. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  42558. };
  42559. byte aes128_cbc_iv[] = {
  42560. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  42561. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  42562. };
  42563. /* teset data size table */
  42564. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  42565. int test_drive2[] = {8, 3, 8, 512, 0};
  42566. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  42567. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  42568. int test_drive_len[100];
  42569. int ret = 0;
  42570. EVP_CIPHER_CTX *evp = NULL;
  42571. int ilen = 0;
  42572. int klen = 0;
  42573. int i, j;
  42574. const EVP_CIPHER *type;
  42575. byte *iv;
  42576. byte *key;
  42577. int ivlen;
  42578. int keylen;
  42579. #define RECORDS 16
  42580. #define BUFFSZ 512
  42581. byte plain [BUFFSZ * RECORDS];
  42582. byte cipher[BUFFSZ * RECORDS];
  42583. byte inb[BUFFSZ];
  42584. byte outb[BUFFSZ+16];
  42585. int outl = 0;
  42586. int inl;
  42587. iv = aes128_cbc_iv;
  42588. ivlen = sizeof(aes128_cbc_iv);
  42589. key = aes128_cbc_key;
  42590. keylen = sizeof(aes128_cbc_key);
  42591. type = EVP_aes_128_cbc();
  42592. set_plain(plain, BUFFSZ * RECORDS);
  42593. SSL_library_init();
  42594. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  42595. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  42596. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  42597. klen = EVP_CIPHER_CTX_key_length(evp);
  42598. if (klen > 0 && keylen != klen) {
  42599. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  42600. }
  42601. ilen = EVP_CIPHER_CTX_iv_length(evp);
  42602. if (ilen > 0 && ivlen != ilen) {
  42603. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  42604. }
  42605. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  42606. for (j = 0; j<RECORDS; j++)
  42607. {
  42608. inl = BUFFSZ;
  42609. get_record(plain, inb, inl);
  42610. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  42611. set_record(cipher, outb, outl);
  42612. }
  42613. for (i = 0; test_drive[i]; i++) {
  42614. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  42615. init_offset();
  42616. test_drive_len[i] = 0;
  42617. for (j = 0; test_drive[i][j]; j++)
  42618. {
  42619. inl = test_drive[i][j];
  42620. test_drive_len[i] += inl;
  42621. get_record(plain, inb, inl);
  42622. ExpectIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)),
  42623. 0);
  42624. /* output to cipher buffer, so that following Dec test can detect
  42625. if any error */
  42626. set_record(cipher, outb, outl);
  42627. }
  42628. EVP_CipherFinal(evp, outb, &outl);
  42629. if (outl > 0)
  42630. set_record(cipher, outb, outl);
  42631. }
  42632. for (i = 0; test_drive[i]; i++) {
  42633. last_val = 0x0f;
  42634. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  42635. init_offset();
  42636. for (j = 0; test_drive[i][j]; j++) {
  42637. inl = test_drive[i][j];
  42638. get_record(cipher, inb, inl);
  42639. ExpectIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)),
  42640. 0);
  42641. binary_dump(outb, outl);
  42642. ExpectIntEQ((ret = check_result(outb, outl)), 0);
  42643. ExpectFalse(outl > ((inl/16+1)*16) && outl > 16);
  42644. }
  42645. ret = EVP_CipherFinal(evp, outb, &outl);
  42646. binary_dump(outb, outl);
  42647. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  42648. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  42649. ExpectTrue(ret);
  42650. }
  42651. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(evp), WOLFSSL_SUCCESS);
  42652. EVP_CIPHER_CTX_free(evp);
  42653. evp = NULL;
  42654. /* Do an extra test to verify correct behavior with empty input. */
  42655. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  42656. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  42657. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  42658. klen = EVP_CIPHER_CTX_key_length(evp);
  42659. if (klen > 0 && keylen != klen) {
  42660. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  42661. }
  42662. ilen = EVP_CIPHER_CTX_iv_length(evp);
  42663. if (ilen > 0 && ivlen != ilen) {
  42664. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  42665. }
  42666. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  42667. /* outl should be set to 0 after passing NULL, 0 for input args. */
  42668. outl = -1;
  42669. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, NULL, 0)), 0);
  42670. ExpectIntEQ(outl, 0);
  42671. EVP_CIPHER_CTX_free(evp);
  42672. #endif /* test_EVP_Cipher */
  42673. return EXPECT_RESULT();
  42674. }
  42675. static int test_wolfSSL_PEM_read_DHparams(void)
  42676. {
  42677. EXPECT_DECLS;
  42678. #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
  42679. !defined(NO_FILESYSTEM)
  42680. DH* dh = NULL;
  42681. XFILE fp = XBADFILE;
  42682. unsigned char derOut[300];
  42683. unsigned char* derOutBuf = derOut;
  42684. int derOutSz = 0;
  42685. unsigned char derExpected[300];
  42686. int derExpectedSz = 0;
  42687. XMEMSET(derOut, 0, sizeof(derOut));
  42688. XMEMSET(derExpected, 0, sizeof(derExpected));
  42689. /* open DH param file, read into DH struct */
  42690. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  42691. /* bad args */
  42692. ExpectNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  42693. ExpectNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  42694. /* good args */
  42695. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  42696. if (fp != XBADFILE) {
  42697. XFCLOSE(fp);
  42698. fp = XBADFILE;
  42699. }
  42700. /* read in certs/dh2048.der for comparison against exported params */
  42701. ExpectTrue((fp = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  42702. ExpectIntGT(derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected),
  42703. fp), 0);
  42704. if (fp != XBADFILE) {
  42705. XFCLOSE(fp);
  42706. fp = XBADFILE;
  42707. }
  42708. /* export DH back to DER and compare */
  42709. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  42710. ExpectIntEQ(derOutSz, derExpectedSz);
  42711. ExpectIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  42712. DH_free(dh);
  42713. dh = NULL;
  42714. /* Test parsing with X9.42 header */
  42715. ExpectTrue((fp = XFOPEN("./certs/x942dh2048.pem", "rb")) != XBADFILE);
  42716. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  42717. if (fp != XBADFILE)
  42718. XFCLOSE(fp);
  42719. DH_free(dh);
  42720. dh = NULL;
  42721. #endif
  42722. return EXPECT_RESULT();
  42723. }
  42724. static int test_wolfSSL_X509_get_serialNumber(void)
  42725. {
  42726. EXPECT_DECLS;
  42727. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  42728. ASN1_INTEGER* a = NULL;
  42729. BIGNUM* bn = NULL;
  42730. X509* x509 = NULL;
  42731. char *serialHex = NULL;
  42732. byte serial[3];
  42733. int serialSz;
  42734. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42735. SSL_FILETYPE_PEM));
  42736. ExpectNotNull(a = X509_get_serialNumber(x509));
  42737. /* check on value of ASN1 Integer */
  42738. ExpectNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  42739. a = NULL;
  42740. /* test setting serial number and then retrieving it */
  42741. ExpectNotNull(a = ASN1_INTEGER_new());
  42742. ExpectIntEQ(ASN1_INTEGER_set(a, 3), 1);
  42743. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  42744. serialSz = sizeof(serial);
  42745. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  42746. WOLFSSL_SUCCESS);
  42747. ExpectIntEQ(serialSz, 1);
  42748. ExpectIntEQ(serial[0], 3);
  42749. ASN1_INTEGER_free(a);
  42750. a = NULL;
  42751. /* test setting serial number with 0's in it */
  42752. serial[0] = 0x01;
  42753. serial[1] = 0x00;
  42754. serial[2] = 0x02;
  42755. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  42756. if (a != NULL) {
  42757. a->data[0] = ASN_INTEGER;
  42758. a->data[1] = sizeof(serial);
  42759. XMEMCPY(&a->data[2], serial, sizeof(serial));
  42760. a->length = sizeof(serial) + 2;
  42761. }
  42762. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  42763. XMEMSET(serial, 0, sizeof(serial));
  42764. serialSz = sizeof(serial);
  42765. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  42766. WOLFSSL_SUCCESS);
  42767. ExpectIntEQ(serialSz, 3);
  42768. ExpectIntEQ(serial[0], 0x01);
  42769. ExpectIntEQ(serial[1], 0x00);
  42770. ExpectIntEQ(serial[2], 0x02);
  42771. ASN1_INTEGER_free(a);
  42772. a = NULL;
  42773. X509_free(x509); /* free's a */
  42774. ExpectNotNull(serialHex = BN_bn2hex(bn));
  42775. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  42776. ExpectStrEQ(serialHex, "01");
  42777. #else
  42778. ExpectStrEQ(serialHex, "1");
  42779. #endif
  42780. OPENSSL_free(serialHex);
  42781. ExpectIntEQ(BN_get_word(bn), 1);
  42782. BN_free(bn);
  42783. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  42784. ExpectNotNull(a = ASN1_INTEGER_new());
  42785. if (a != NULL) {
  42786. ExpectNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  42787. DYNAMIC_TYPE_OPENSSL));
  42788. a->isDynamic = 1;
  42789. ASN1_INTEGER_free(a);
  42790. }
  42791. #endif
  42792. return EXPECT_RESULT();
  42793. }
  42794. static int test_wolfSSL_OpenSSL_add_all_algorithms(void)
  42795. {
  42796. EXPECT_DECLS;
  42797. #if defined(OPENSSL_EXTRA)
  42798. ExpectIntEQ(wolfSSL_add_all_algorithms(), WOLFSSL_SUCCESS);
  42799. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(), WOLFSSL_SUCCESS);
  42800. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(), WOLFSSL_SUCCESS);
  42801. #endif
  42802. return EXPECT_RESULT();
  42803. }
  42804. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  42805. {
  42806. EXPECT_DECLS;
  42807. #if defined(OPENSSL_EXTRA)
  42808. #define MAX_HEXSTR_BUFSZ 9
  42809. #define NUM_CASES 5
  42810. struct Output {
  42811. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  42812. long ret;
  42813. };
  42814. int i;
  42815. int j;
  42816. const char* inputs[NUM_CASES] = {
  42817. "aabcd1357e",
  42818. "01:12:23:34:a5:b6:c7:d8:e9",
  42819. ":01:02",
  42820. "012",
  42821. ":ab:ac:d"
  42822. };
  42823. struct Output expectedOutputs[NUM_CASES] = {
  42824. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  42825. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  42826. {{0x01, 0x02}, 2},
  42827. {{0x00}, 0},
  42828. {{0x00}, 0}
  42829. };
  42830. long len = 0;
  42831. unsigned char* returnedBuf = NULL;
  42832. for (i = 0; i < NUM_CASES && !EXPECT_FAIL(); ++i) {
  42833. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  42834. if (returnedBuf == NULL) {
  42835. ExpectIntEQ(expectedOutputs[i].ret, 0);
  42836. continue;
  42837. }
  42838. ExpectIntEQ(expectedOutputs[i].ret, len);
  42839. for (j = 0; j < len; ++j) {
  42840. ExpectIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  42841. }
  42842. OPENSSL_free(returnedBuf);
  42843. returnedBuf = NULL;
  42844. }
  42845. #endif
  42846. return EXPECT_RESULT();
  42847. }
  42848. static int test_wolfSSL_X509_CA_num(void)
  42849. {
  42850. EXPECT_DECLS;
  42851. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  42852. defined(HAVE_ECC) && !defined(NO_RSA)
  42853. WOLFSSL_X509_STORE *store = NULL;
  42854. WOLFSSL_X509 *x509_1 = NULL;
  42855. WOLFSSL_X509 *x509_2 = NULL;
  42856. int ca_num = 0;
  42857. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  42858. ExpectNotNull(x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42859. WOLFSSL_FILETYPE_PEM));
  42860. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_1), 1);
  42861. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 1);
  42862. ExpectNotNull(x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile,
  42863. WOLFSSL_FILETYPE_PEM));
  42864. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_2), 1);
  42865. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 2);
  42866. wolfSSL_X509_free(x509_1);
  42867. wolfSSL_X509_free(x509_2);
  42868. wolfSSL_X509_STORE_free(store);
  42869. #endif
  42870. return EXPECT_RESULT();
  42871. }
  42872. static int test_wolfSSL_X509_check_ca(void)
  42873. {
  42874. EXPECT_DECLS;
  42875. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  42876. WOLFSSL_X509 *x509 = NULL;
  42877. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42878. WOLFSSL_FILETYPE_PEM));
  42879. ExpectIntEQ(wolfSSL_X509_check_ca(x509), 1);
  42880. wolfSSL_X509_free(x509);
  42881. #endif
  42882. return EXPECT_RESULT();
  42883. }
  42884. static int test_wolfSSL_X509_check_ip_asc(void)
  42885. {
  42886. EXPECT_DECLS;
  42887. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  42888. WOLFSSL_X509 *x509 = NULL;
  42889. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  42890. WOLFSSL_FILETYPE_PEM));
  42891. #if 0
  42892. /* TODO: add cert gen for testing positive case */
  42893. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  42894. #endif
  42895. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  42896. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  42897. wolfSSL_X509_free(x509);
  42898. #endif
  42899. return EXPECT_RESULT();
  42900. }
  42901. static int test_wolfSSL_make_cert(void)
  42902. {
  42903. EXPECT_DECLS;
  42904. #if !defined(NO_RSA) && !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && \
  42905. defined(WOLFSSL_CERT_EXT)
  42906. int ret = 0;
  42907. Cert cert;
  42908. CertName name;
  42909. RsaKey key;
  42910. WC_RNG rng;
  42911. byte der[FOURK_BUF];
  42912. word32 idx = 0;
  42913. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  42914. #ifdef OPENSSL_EXTRA
  42915. const unsigned char* pt = NULL;
  42916. int certSz = 0;
  42917. X509* x509 = NULL;
  42918. X509_NAME* x509name = NULL;
  42919. X509_NAME_ENTRY* entry = NULL;
  42920. ASN1_STRING* entryValue = NULL;
  42921. #endif
  42922. XMEMSET(&name, 0, sizeof(CertName));
  42923. /* set up cert name */
  42924. XMEMCPY(name.country, "US", sizeof("US"));
  42925. name.countryEnc = CTC_PRINTABLE;
  42926. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  42927. name.stateEnc = CTC_UTF8;
  42928. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  42929. name.localityEnc = CTC_UTF8;
  42930. XMEMCPY(name.sur, "Test", sizeof("Test"));
  42931. name.surEnc = CTC_UTF8;
  42932. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  42933. name.orgEnc = CTC_UTF8;
  42934. XMEMCPY(name.unit, "Development", sizeof("Development"));
  42935. name.unitEnc = CTC_UTF8;
  42936. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  42937. name.commonNameEnc = CTC_UTF8;
  42938. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  42939. name.serialDevEnc = CTC_PRINTABLE;
  42940. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  42941. name.userIdEnc = CTC_PRINTABLE;
  42942. #ifdef WOLFSSL_MULTI_ATTRIB
  42943. #if CTC_MAX_ATTRIB > 2
  42944. {
  42945. NameAttrib* n;
  42946. n = &name.name[0];
  42947. n->id = ASN_DOMAIN_COMPONENT;
  42948. n->type = CTC_UTF8;
  42949. n->sz = sizeof("com");
  42950. XMEMCPY(n->value, "com", sizeof("com"));
  42951. n = &name.name[1];
  42952. n->id = ASN_DOMAIN_COMPONENT;
  42953. n->type = CTC_UTF8;
  42954. n->sz = sizeof("wolfssl");
  42955. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  42956. }
  42957. #endif
  42958. #endif /* WOLFSSL_MULTI_ATTRIB */
  42959. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  42960. #ifndef HAVE_FIPS
  42961. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  42962. #else
  42963. ExpectIntEQ(wc_InitRng(&rng), 0);
  42964. #endif
  42965. /* load test RSA key */
  42966. idx = 0;
  42967. #if defined(USE_CERT_BUFFERS_1024)
  42968. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  42969. sizeof_server_key_der_1024), 0);
  42970. #elif defined(USE_CERT_BUFFERS_2048)
  42971. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  42972. sizeof_server_key_der_2048), 0);
  42973. #else
  42974. /* error case, no RSA key loaded, happens later */
  42975. (void)idx;
  42976. #endif
  42977. XMEMSET(&cert, 0 , sizeof(Cert));
  42978. ExpectIntEQ(wc_InitCert(&cert), 0);
  42979. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  42980. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  42981. cert.serialSz = (int)sizeof(mySerial);
  42982. cert.isCA = 1;
  42983. #ifndef NO_SHA256
  42984. cert.sigType = CTC_SHA256wRSA;
  42985. #else
  42986. cert.sigType = CTC_SHAwRSA;
  42987. #endif
  42988. /* add SKID from the Public Key */
  42989. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  42990. /* add AKID from the Public Key */
  42991. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  42992. ret = 0;
  42993. do {
  42994. #if defined(WOLFSSL_ASYNC_CRYPT)
  42995. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  42996. #endif
  42997. if (ret >= 0) {
  42998. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  42999. }
  43000. } while (ret == WC_PENDING_E);
  43001. ExpectIntGT(ret, 0);
  43002. #ifdef OPENSSL_EXTRA
  43003. /* der holds a certificate with DC's now check X509 parsing of it */
  43004. certSz = ret;
  43005. pt = der;
  43006. ExpectNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  43007. ExpectNotNull(x509name = X509_get_subject_name(x509));
  43008. #ifdef WOLFSSL_MULTI_ATTRIB
  43009. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  43010. -1)), 5);
  43011. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  43012. idx)), 6);
  43013. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  43014. idx)), -1);
  43015. #endif /* WOLFSSL_MULTI_ATTRIB */
  43016. /* compare DN at index 0 */
  43017. ExpectNotNull(entry = X509_NAME_get_entry(x509name, 0));
  43018. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  43019. ExpectIntEQ(ASN1_STRING_length(entryValue), 2);
  43020. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  43021. #ifndef WOLFSSL_MULTI_ATTRIB
  43022. /* compare Serial Number */
  43023. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_serialNumber,
  43024. -1)), 7);
  43025. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  43026. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  43027. ExpectIntEQ(ASN1_STRING_length(entryValue), XSTRLEN("wolfSSL12345"));
  43028. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "wolfSSL12345");
  43029. #endif
  43030. #ifdef WOLFSSL_MULTI_ATTRIB
  43031. /* get first and second DC and compare result */
  43032. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  43033. -1)), 5);
  43034. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  43035. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  43036. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  43037. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  43038. idx)), 6);
  43039. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  43040. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  43041. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  43042. #endif /* WOLFSSL_MULTI_ATTRIB */
  43043. /* try invalid index locations for regression test and sanity check */
  43044. ExpectNull(entry = X509_NAME_get_entry(x509name, 11));
  43045. ExpectNull(entry = X509_NAME_get_entry(x509name, 20));
  43046. X509_free(x509);
  43047. #endif /* OPENSSL_EXTRA */
  43048. wc_FreeRsaKey(&key);
  43049. wc_FreeRng(&rng);
  43050. #endif
  43051. return EXPECT_RESULT();
  43052. }
  43053. static int test_wolfSSL_X509_get_version(void)
  43054. {
  43055. EXPECT_DECLS;
  43056. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  43057. WOLFSSL_X509 *x509 = NULL;
  43058. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  43059. WOLFSSL_FILETYPE_PEM));
  43060. ExpectIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  43061. wolfSSL_X509_free(x509);
  43062. #endif
  43063. return EXPECT_RESULT();
  43064. }
  43065. #if defined(OPENSSL_ALL)
  43066. static int test_wolfSSL_sk_CIPHER_description(void)
  43067. {
  43068. EXPECT_DECLS;
  43069. #if !defined(NO_RSA)
  43070. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  43071. int i;
  43072. int numCiphers = 0;
  43073. const SSL_METHOD *method = NULL;
  43074. const SSL_CIPHER *cipher = NULL;
  43075. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  43076. SSL_CTX *ctx = NULL;
  43077. SSL *ssl = NULL;
  43078. char buf[256];
  43079. char test_str[9] = "0000000";
  43080. const char badStr[] = "unknown";
  43081. const char certPath[] = "./certs/client-cert.pem";
  43082. XMEMSET(buf, 0, sizeof(buf));
  43083. ExpectNotNull(method = TLSv1_2_client_method());
  43084. ExpectNotNull(ctx = SSL_CTX_new(method));
  43085. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  43086. SSL_CTX_set_verify_depth(ctx, 4);
  43087. SSL_CTX_set_options(ctx, flags);
  43088. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  43089. WOLFSSL_SUCCESS);
  43090. ExpectNotNull(ssl = SSL_new(ctx));
  43091. /* SSL_get_ciphers returns a stack of all configured ciphers
  43092. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  43093. */
  43094. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  43095. /* loop through the amount of supportedCiphers */
  43096. numCiphers = sk_num(supportedCiphers);
  43097. for (i = 0; i < numCiphers; ++i) {
  43098. int j;
  43099. /* sk_value increments "sk->data.cipher->cipherOffset".
  43100. * wolfSSL_sk_CIPHER_description sets the description for
  43101. * the cipher based on the provided offset.
  43102. */
  43103. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  43104. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  43105. }
  43106. /* Search cipher description string for "unknown" descriptor */
  43107. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  43108. int k = 0;
  43109. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  43110. test_str[k] = badStr[k];
  43111. j++;
  43112. k++;
  43113. }
  43114. }
  43115. /* Fail if test_str == badStr == "unknown" */
  43116. ExpectStrNE(test_str,badStr);
  43117. }
  43118. SSL_free(ssl);
  43119. SSL_CTX_free(ctx);
  43120. #endif
  43121. return EXPECT_RESULT();
  43122. }
  43123. static int test_wolfSSL_get_ciphers_compat(void)
  43124. {
  43125. EXPECT_DECLS;
  43126. #if !defined(NO_RSA)
  43127. const SSL_METHOD *method = NULL;
  43128. const char certPath[] = "./certs/client-cert.pem";
  43129. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  43130. SSL_CTX *ctx = NULL;
  43131. WOLFSSL *ssl = NULL;
  43132. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  43133. ExpectNotNull(method = SSLv23_client_method());
  43134. ExpectNotNull(ctx = SSL_CTX_new(method));
  43135. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  43136. SSL_CTX_set_verify_depth(ctx, 4);
  43137. SSL_CTX_set_options(ctx, flags);
  43138. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  43139. WOLFSSL_SUCCESS);
  43140. ExpectNotNull(ssl = SSL_new(ctx));
  43141. /* Test Bad NULL input */
  43142. ExpectNull(supportedCiphers = SSL_get_ciphers(NULL));
  43143. /* Test for Good input */
  43144. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  43145. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  43146. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  43147. SSL_free(ssl);
  43148. SSL_CTX_free(ctx);
  43149. #endif
  43150. return EXPECT_RESULT();
  43151. }
  43152. static int test_wolfSSL_X509_PUBKEY_get(void)
  43153. {
  43154. EXPECT_DECLS;
  43155. WOLFSSL_X509_PUBKEY pubkey;
  43156. WOLFSSL_X509_PUBKEY* key;
  43157. WOLFSSL_EVP_PKEY evpkey ;
  43158. WOLFSSL_EVP_PKEY* evpPkey;
  43159. WOLFSSL_EVP_PKEY* retEvpPkey;
  43160. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  43161. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  43162. key = &pubkey;
  43163. evpPkey = &evpkey;
  43164. evpPkey->type = WOLFSSL_SUCCESS;
  43165. key->pkey = evpPkey;
  43166. ExpectNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  43167. ExpectIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  43168. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  43169. key->pkey = NULL;
  43170. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  43171. return EXPECT_RESULT();
  43172. }
  43173. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  43174. {
  43175. EXPECT_DECLS;
  43176. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  43177. DSA *dsa = NULL;
  43178. DSA *setDsa = NULL;
  43179. EVP_PKEY *pkey = NULL;
  43180. EVP_PKEY *set1Pkey = NULL;
  43181. SHA_CTX sha;
  43182. byte signature[DSA_SIG_SIZE];
  43183. byte hash[WC_SHA_DIGEST_SIZE];
  43184. word32 bytes;
  43185. int answer;
  43186. #ifdef USE_CERT_BUFFERS_1024
  43187. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  43188. int dsaKeySz = sizeof_dsa_key_der_1024;
  43189. byte tmp[ONEK_BUF];
  43190. XMEMSET(tmp, 0, sizeof(tmp));
  43191. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  43192. bytes = dsaKeySz;
  43193. #elif defined(USE_CERT_BUFFERS_2048)
  43194. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  43195. int dsaKeySz = sizeof_dsa_key_der_2048;
  43196. byte tmp[TWOK_BUF];
  43197. XMEMSET(tmp, 0, sizeof(tmp));
  43198. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  43199. bytes = dsaKeySz;
  43200. #else
  43201. byte tmp[TWOK_BUF];
  43202. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  43203. int dsaKeySz;
  43204. XFILE fp = XBADFILE;
  43205. XMEMSET(tmp, 0, sizeof(tmp));
  43206. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  43207. ExpectIntGT(dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  43208. if (fp != XBADFILE)
  43209. XFCLOSE(fp);
  43210. #endif /* END USE_CERT_BUFFERS_1024 */
  43211. /* Create hash to later Sign and Verify */
  43212. ExpectIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  43213. ExpectIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  43214. ExpectIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  43215. /* Initialize pkey with der format dsa key */
  43216. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey, &dsaKeyDer,
  43217. (long)dsaKeySz));
  43218. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  43219. /* Should Fail: NULL argument */
  43220. ExpectNull(dsa = EVP_PKEY_get0_DSA(NULL));
  43221. ExpectNull(dsa = EVP_PKEY_get1_DSA(NULL));
  43222. /* Should Pass: Initialized pkey argument */
  43223. ExpectNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  43224. ExpectNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  43225. #ifdef USE_CERT_BUFFERS_1024
  43226. ExpectIntEQ(DSA_bits(dsa), 1024);
  43227. #else
  43228. ExpectIntEQ(DSA_bits(dsa), 2048);
  43229. #endif
  43230. /* Sign */
  43231. ExpectIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  43232. /* Verify. */
  43233. ExpectIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  43234. WOLFSSL_SUCCESS);
  43235. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  43236. /* Should Fail: set1Pkey not initialized */
  43237. ExpectIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  43238. /* Initialize set1Pkey */
  43239. set1Pkey = EVP_PKEY_new();
  43240. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  43241. ExpectIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  43242. WOLFSSL_SUCCESS);
  43243. /* Should Pass: set dsa into set1Pkey */
  43244. ExpectIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  43245. DSA_free(dsa);
  43246. DSA_free(setDsa);
  43247. EVP_PKEY_free(pkey);
  43248. EVP_PKEY_free(set1Pkey);
  43249. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  43250. return EXPECT_RESULT();
  43251. } /* END test_EVP_PKEY_set1_get1_DSA */
  43252. static int test_wolfSSL_DSA_generate_parameters(void)
  43253. {
  43254. EXPECT_DECLS;
  43255. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  43256. !defined(HAVE_FIPS)
  43257. DSA *dsa = NULL;
  43258. ExpectNotNull(dsa = DSA_generate_parameters(2048, NULL, 0, NULL, NULL, NULL,
  43259. NULL));
  43260. DSA_free(dsa);
  43261. #endif
  43262. return EXPECT_RESULT();
  43263. }
  43264. static int test_wolfSSL_DSA_SIG(void)
  43265. {
  43266. EXPECT_DECLS;
  43267. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  43268. !defined(HAVE_FIPS)
  43269. DSA *dsa = NULL;
  43270. DSA *dsa2 = NULL;
  43271. DSA_SIG *sig = NULL;
  43272. const BIGNUM *p = NULL;
  43273. const BIGNUM *q = NULL;
  43274. const BIGNUM *g = NULL;
  43275. const BIGNUM *pub = NULL;
  43276. const BIGNUM *priv = NULL;
  43277. BIGNUM *dup_p = NULL;
  43278. BIGNUM *dup_q = NULL;
  43279. BIGNUM *dup_g = NULL;
  43280. BIGNUM *dup_pub = NULL;
  43281. BIGNUM *dup_priv = NULL;
  43282. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  43283. ExpectNotNull(dsa = DSA_new());
  43284. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048, NULL, 0, NULL, NULL,
  43285. NULL), 1);
  43286. ExpectIntEQ(DSA_generate_key(dsa), 1);
  43287. DSA_get0_pqg(dsa, &p, &q, &g);
  43288. DSA_get0_key(dsa, &pub, &priv);
  43289. ExpectNotNull(dup_p = BN_dup(p));
  43290. ExpectNotNull(dup_q = BN_dup(q));
  43291. ExpectNotNull(dup_g = BN_dup(g));
  43292. ExpectNotNull(dup_pub = BN_dup(pub));
  43293. ExpectNotNull(dup_priv = BN_dup(priv));
  43294. ExpectNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  43295. ExpectNotNull(dsa2 = DSA_new());
  43296. ExpectIntEQ(DSA_set0_pqg(dsa2, dup_p, dup_q, dup_g), 1);
  43297. if (EXPECT_FAIL()) {
  43298. BN_free(dup_p);
  43299. BN_free(dup_q);
  43300. BN_free(dup_g);
  43301. }
  43302. ExpectIntEQ(DSA_set0_key(dsa2, dup_pub, dup_priv), 1);
  43303. if (EXPECT_FAIL()) {
  43304. BN_free(dup_pub);
  43305. BN_free(dup_priv);
  43306. }
  43307. ExpectIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  43308. DSA_free(dsa);
  43309. DSA_free(dsa2);
  43310. DSA_SIG_free(sig);
  43311. #endif
  43312. return EXPECT_RESULT();
  43313. }
  43314. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  43315. {
  43316. EXPECT_DECLS;
  43317. #ifdef HAVE_ECC
  43318. WOLFSSL_EC_KEY* ecKey = NULL;
  43319. WOLFSSL_EC_KEY* ecGet1 = NULL;
  43320. EVP_PKEY* pkey = NULL;
  43321. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  43322. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43323. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  43324. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  43325. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  43326. /* Should fail since ecKey is empty */
  43327. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  43328. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  43329. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  43330. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  43331. ExpectNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  43332. ExpectNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  43333. wolfSSL_EC_KEY_free(ecKey);
  43334. wolfSSL_EC_KEY_free(ecGet1);
  43335. EVP_PKEY_free(pkey);
  43336. #endif /* HAVE_ECC */
  43337. return EXPECT_RESULT();
  43338. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  43339. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  43340. {
  43341. EXPECT_DECLS;
  43342. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  43343. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  43344. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  43345. DH *dh = NULL;
  43346. DH *setDh = NULL;
  43347. EVP_PKEY *pkey = NULL;
  43348. XFILE f = XBADFILE;
  43349. unsigned char buf[4096];
  43350. const unsigned char* pt = buf;
  43351. const char* dh2048 = "./certs/dh2048.der";
  43352. long len = 0;
  43353. int code = -1;
  43354. XMEMSET(buf, 0, sizeof(buf));
  43355. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  43356. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  43357. if (f != XBADFILE)
  43358. XFCLOSE(f);
  43359. /* Load dh2048.der into DH with internal format */
  43360. ExpectNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  43361. ExpectIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  43362. ExpectIntEQ(code, 0);
  43363. code = -1;
  43364. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43365. /* Set DH into PKEY */
  43366. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  43367. /* Get DH from PKEY */
  43368. ExpectNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  43369. ExpectIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  43370. ExpectIntEQ(code, 0);
  43371. EVP_PKEY_free(pkey);
  43372. DH_free(setDh);
  43373. setDh = NULL;
  43374. DH_free(dh);
  43375. dh = NULL;
  43376. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  43377. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  43378. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  43379. return EXPECT_RESULT();
  43380. } /* END test_EVP_PKEY_set1_get1_DH */
  43381. static int test_wolfSSL_CTX_ctrl(void)
  43382. {
  43383. EXPECT_DECLS;
  43384. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  43385. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  43386. char caFile[] = "./certs/client-ca.pem";
  43387. char clientFile[] = "./certs/client-cert.pem";
  43388. SSL_CTX* ctx = NULL;
  43389. X509* x509 = NULL;
  43390. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  43391. byte buf[6000];
  43392. char file[] = "./certs/dsaparams.pem";
  43393. XFILE f = XBADFILE;
  43394. int bytes = 0;
  43395. BIO* bio = NULL;
  43396. DSA* dsa = NULL;
  43397. DH* dh = NULL;
  43398. #endif
  43399. #ifdef HAVE_ECC
  43400. WOLFSSL_EC_KEY* ecKey = NULL;
  43401. #endif
  43402. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  43403. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  43404. WOLFSSL_FILETYPE_PEM));
  43405. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  43406. if (EXPECT_FAIL()) {
  43407. wolfSSL_X509_free(x509);
  43408. }
  43409. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  43410. WOLFSSL_FILETYPE_PEM));
  43411. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  43412. /* Initialize DH */
  43413. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  43414. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  43415. if (f != XBADFILE)
  43416. XFCLOSE(f);
  43417. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  43418. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  43419. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  43420. #endif
  43421. #ifdef HAVE_ECC
  43422. /* Initialize WOLFSSL_EC_KEY */
  43423. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  43424. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  43425. #endif
  43426. /* additional test of getting EVP_PKEY key size from X509
  43427. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  43428. * allowed with user RSA */
  43429. {
  43430. EVP_PKEY* pkey = NULL;
  43431. #if defined(HAVE_ECC)
  43432. X509* ecX509 = NULL;
  43433. #endif /* HAVE_ECC */
  43434. ExpectNotNull(pkey = X509_get_pubkey(x509));
  43435. /* current RSA key is 2048 bit (256 bytes) */
  43436. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  43437. EVP_PKEY_free(pkey);
  43438. pkey = NULL;
  43439. #if defined(HAVE_ECC)
  43440. #if defined(USE_CERT_BUFFERS_256)
  43441. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  43442. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  43443. SSL_FILETYPE_ASN1));
  43444. #else
  43445. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  43446. cliEccCertFile, SSL_FILETYPE_PEM));
  43447. #endif
  43448. ExpectNotNull(pkey = X509_get_pubkey(ecX509));
  43449. /* current ECC key is 256 bit (32 bytes) */
  43450. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  43451. X509_free(ecX509);
  43452. EVP_PKEY_free(pkey);
  43453. #endif /* HAVE_ECC */
  43454. }
  43455. /* Tests should fail with passed in NULL pointer */
  43456. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, NULL),
  43457. SSL_FAILURE);
  43458. #if !defined(NO_DH) && !defined(NO_DSA)
  43459. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, NULL),
  43460. SSL_FAILURE);
  43461. #endif
  43462. #ifdef HAVE_ECC
  43463. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, NULL),
  43464. SSL_FAILURE);
  43465. #endif
  43466. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  43467. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  43468. */
  43469. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, x509),
  43470. SSL_SUCCESS);
  43471. if (EXPECT_FAIL()) {
  43472. wolfSSL_X509_free(x509);
  43473. }
  43474. /* Test with SSL_CTRL_OPTIONS
  43475. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  43476. */
  43477. ExpectTrue(wolfSSL_CTX_ctrl(ctx, SSL_CTRL_OPTIONS, SSL_OP_NO_TLSv1,
  43478. NULL) == SSL_OP_NO_TLSv1);
  43479. ExpectTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  43480. /* Test with SSL_CTRL_SET_TMP_DH
  43481. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  43482. */
  43483. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  43484. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, dh),
  43485. SSL_SUCCESS);
  43486. #endif
  43487. /* Test with SSL_CTRL_SET_TMP_ECDH
  43488. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  43489. */
  43490. #ifdef HAVE_ECC
  43491. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, ecKey),
  43492. SSL_SUCCESS);
  43493. #endif
  43494. #ifdef WOLFSSL_ENCRYPTED_KEYS
  43495. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  43496. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  43497. #endif
  43498. /* Test for min/max proto */
  43499. #ifndef WOLFSSL_NO_TLS12
  43500. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  43501. 0, NULL), SSL_SUCCESS);
  43502. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  43503. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  43504. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  43505. #endif
  43506. #ifdef WOLFSSL_TLS13
  43507. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  43508. 0, NULL), SSL_SUCCESS);
  43509. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  43510. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  43511. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  43512. #ifndef WOLFSSL_NO_TLS12
  43513. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  43514. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  43515. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  43516. #endif
  43517. #endif
  43518. /* Cleanup and Pass */
  43519. #if !defined(NO_DH) && !defined(NO_DSA)
  43520. #ifndef NO_BIO
  43521. BIO_free(bio);
  43522. DSA_free(dsa);
  43523. DH_free(dh);
  43524. dh = NULL;
  43525. #endif
  43526. #endif
  43527. #ifdef HAVE_ECC
  43528. wolfSSL_EC_KEY_free(ecKey);
  43529. #endif
  43530. SSL_CTX_free(ctx);
  43531. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  43532. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  43533. return EXPECT_RESULT();
  43534. }
  43535. static int test_wolfSSL_EVP_PKEY_assign(void)
  43536. {
  43537. EXPECT_DECLS;
  43538. #if !defined(NO_RSA) || !defined(NO_DSA) || defined(HAVE_ECC)
  43539. int type;
  43540. WOLFSSL_EVP_PKEY* pkey = NULL;
  43541. #ifndef NO_RSA
  43542. WOLFSSL_RSA* rsa = NULL;
  43543. #endif
  43544. #ifndef NO_DSA
  43545. WOLFSSL_DSA* dsa = NULL;
  43546. #endif
  43547. #ifdef HAVE_ECC
  43548. WOLFSSL_EC_KEY* ecKey = NULL;
  43549. #endif
  43550. #ifndef NO_RSA
  43551. type = EVP_PKEY_RSA;
  43552. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43553. ExpectNotNull(rsa = wolfSSL_RSA_new());
  43554. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, rsa), WOLFSSL_FAILURE);
  43555. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  43556. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, rsa), WOLFSSL_FAILURE);
  43557. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, rsa), WOLFSSL_SUCCESS);
  43558. if (EXPECT_FAIL()) {
  43559. wolfSSL_RSA_free(rsa);
  43560. }
  43561. wolfSSL_EVP_PKEY_free(pkey);
  43562. pkey = NULL;
  43563. #endif /* NO_RSA */
  43564. #ifndef NO_DSA
  43565. type = EVP_PKEY_DSA;
  43566. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43567. ExpectNotNull(dsa = wolfSSL_DSA_new());
  43568. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, dsa), WOLFSSL_FAILURE);
  43569. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  43570. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, dsa), WOLFSSL_FAILURE);
  43571. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, dsa), WOLFSSL_SUCCESS);
  43572. if (EXPECT_FAIL()) {
  43573. wolfSSL_DSA_free(dsa);
  43574. }
  43575. wolfSSL_EVP_PKEY_free(pkey);
  43576. pkey = NULL;
  43577. #endif /* NO_DSA */
  43578. #ifdef HAVE_ECC
  43579. type = EVP_PKEY_EC;
  43580. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43581. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  43582. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, ecKey), WOLFSSL_FAILURE);
  43583. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  43584. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, ecKey), WOLFSSL_FAILURE);
  43585. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_FAILURE);
  43586. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  43587. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_SUCCESS);
  43588. if (EXPECT_FAIL()) {
  43589. wolfSSL_EC_KEY_free(ecKey);
  43590. }
  43591. wolfSSL_EVP_PKEY_free(pkey);
  43592. pkey = NULL;
  43593. #endif /* HAVE_ECC */
  43594. #endif /* !NO_RSA || !NO_DSA || HAVE_ECC */
  43595. return EXPECT_RESULT();
  43596. }
  43597. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  43598. {
  43599. EXPECT_DECLS;
  43600. #if !defined(NO_DH) && \
  43601. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43602. XFILE f = XBADFILE;
  43603. unsigned char buf[4096];
  43604. const unsigned char* pt = buf;
  43605. const char* params1 = "./certs/dh2048.der";
  43606. long len = 0;
  43607. WOLFSSL_DH* dh = NULL;
  43608. WOLFSSL_EVP_PKEY* pkey = NULL;
  43609. XMEMSET(buf, 0, sizeof(buf));
  43610. /* Load DH parameters DER. */
  43611. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  43612. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  43613. if (f != XBADFILE)
  43614. XFCLOSE(f);
  43615. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  43616. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  43617. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43618. /* Bad cases */
  43619. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  43620. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  43621. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  43622. /* Good case */
  43623. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  43624. if (EXPECT_FAIL()) {
  43625. wolfSSL_DH_free(dh);
  43626. }
  43627. EVP_PKEY_free(pkey);
  43628. #endif
  43629. return EXPECT_RESULT();
  43630. }
  43631. static int test_wolfSSL_EVP_PKEY_base_id(void)
  43632. {
  43633. EXPECT_DECLS;
  43634. WOLFSSL_EVP_PKEY* pkey = NULL;
  43635. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43636. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  43637. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  43638. EVP_PKEY_free(pkey);
  43639. return EXPECT_RESULT();
  43640. }
  43641. static int test_wolfSSL_EVP_PKEY_id(void)
  43642. {
  43643. EXPECT_DECLS;
  43644. WOLFSSL_EVP_PKEY* pkey = NULL;
  43645. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43646. ExpectIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  43647. ExpectIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  43648. EVP_PKEY_free(pkey);
  43649. return EXPECT_RESULT();
  43650. }
  43651. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  43652. {
  43653. EXPECT_DECLS;
  43654. /* ECC check taken from ecc.c. It is the condition that defines ECC256 */
  43655. #if defined(OPENSSL_ALL) && !defined(NO_ECC_SECP) && \
  43656. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  43657. ECC_MIN_KEY_SZ <= 256)
  43658. EVP_PKEY_CTX* ctx = NULL;
  43659. EVP_PKEY* pkey = NULL;
  43660. /* Test error conditions. */
  43661. ExpectIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  43662. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  43663. ExpectIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  43664. #ifndef NO_RSA
  43665. EVP_PKEY_CTX_free(ctx);
  43666. /* Parameter generation for RSA not supported yet. */
  43667. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  43668. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  43669. #endif
  43670. #ifdef HAVE_ECC
  43671. EVP_PKEY_CTX_free(ctx);
  43672. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  43673. ExpectIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  43674. ExpectIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  43675. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  43676. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  43677. ExpectIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  43678. WOLFSSL_SUCCESS);
  43679. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  43680. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  43681. #endif
  43682. EVP_PKEY_CTX_free(ctx);
  43683. EVP_PKEY_free(pkey);
  43684. #endif
  43685. return EXPECT_RESULT();
  43686. }
  43687. static int test_wolfSSL_EVP_PKEY_keygen(void)
  43688. {
  43689. EXPECT_DECLS;
  43690. WOLFSSL_EVP_PKEY* pkey = NULL;
  43691. EVP_PKEY_CTX* ctx = NULL;
  43692. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  43693. WOLFSSL_EVP_PKEY* params = NULL;
  43694. DH* dh = NULL;
  43695. const BIGNUM* pubkey = NULL;
  43696. const BIGNUM* privkey = NULL;
  43697. ASN1_INTEGER* asn1int = NULL;
  43698. unsigned int length = 0;
  43699. byte* derBuffer = NULL;
  43700. #endif
  43701. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43702. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43703. /* Bad cases */
  43704. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  43705. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  43706. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  43707. /* Good case */
  43708. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  43709. EVP_PKEY_CTX_free(ctx);
  43710. ctx = NULL;
  43711. EVP_PKEY_free(pkey);
  43712. pkey = NULL;
  43713. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  43714. /* Test DH keygen */
  43715. {
  43716. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  43717. ExpectNotNull(dh = DH_get_2048_256());
  43718. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  43719. ExpectNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  43720. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  43721. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  43722. DH_free(dh);
  43723. dh = NULL;
  43724. EVP_PKEY_CTX_free(ctx);
  43725. EVP_PKEY_free(params);
  43726. /* try exporting generated key to DER, to verify */
  43727. ExpectNotNull(dh = EVP_PKEY_get1_DH(pkey));
  43728. DH_get0_key(dh, &pubkey, &privkey);
  43729. ExpectNotNull(pubkey);
  43730. ExpectNotNull(privkey);
  43731. ExpectNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  43732. ExpectIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  43733. ASN1_INTEGER_free(asn1int);
  43734. DH_free(dh);
  43735. dh = NULL;
  43736. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43737. EVP_PKEY_free(pkey);
  43738. }
  43739. #endif
  43740. return EXPECT_RESULT();
  43741. }
  43742. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  43743. {
  43744. EXPECT_DECLS;
  43745. WOLFSSL_EVP_PKEY* pkey = NULL;
  43746. EVP_PKEY_CTX *ctx = NULL;
  43747. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43748. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43749. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  43750. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(NULL), WOLFSSL_SUCCESS);
  43751. EVP_PKEY_CTX_free(ctx);
  43752. EVP_PKEY_free(pkey);
  43753. return EXPECT_RESULT();
  43754. }
  43755. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  43756. {
  43757. EXPECT_DECLS;
  43758. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  43759. WOLFSSL_EVP_PKEY* pkey = NULL;
  43760. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43761. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  43762. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(NULL), 0);
  43763. EVP_PKEY_free(pkey);
  43764. #endif
  43765. return EXPECT_RESULT();
  43766. }
  43767. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  43768. {
  43769. EXPECT_DECLS;
  43770. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  43771. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  43772. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  43773. !defined(NO_FILESYSTEM)
  43774. WOLFSSL_EVP_PKEY* params = NULL;
  43775. WOLFSSL_EVP_PKEY* copy = NULL;
  43776. DH* dh = NULL;
  43777. BIGNUM* p1;
  43778. BIGNUM* g1;
  43779. BIGNUM* q1;
  43780. BIGNUM* p2;
  43781. BIGNUM* g2;
  43782. BIGNUM* q2;
  43783. /* create DH with DH_get_2048_256 params */
  43784. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  43785. ExpectNotNull(dh = DH_get_2048_256());
  43786. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  43787. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  43788. (const BIGNUM**)&q1,
  43789. (const BIGNUM**)&g1);
  43790. DH_free(dh);
  43791. dh = NULL;
  43792. /* create DH with random generated DH params */
  43793. ExpectNotNull(copy = wolfSSL_EVP_PKEY_new());
  43794. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  43795. ExpectIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  43796. DH_free(dh);
  43797. dh = NULL;
  43798. ExpectIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  43799. ExpectNotNull(dh = EVP_PKEY_get1_DH(copy));
  43800. ExpectNotNull(dh->p);
  43801. ExpectNotNull(dh->g);
  43802. ExpectNotNull(dh->q);
  43803. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  43804. (const BIGNUM**)&q2,
  43805. (const BIGNUM**)&g2);
  43806. ExpectIntEQ(BN_cmp(p1, p2), 0);
  43807. ExpectIntEQ(BN_cmp(q1, q2), 0);
  43808. ExpectIntEQ(BN_cmp(g1, g2), 0);
  43809. DH_free(dh);
  43810. dh = NULL;
  43811. EVP_PKEY_free(copy);
  43812. EVP_PKEY_free(params);
  43813. #endif
  43814. return EXPECT_RESULT();
  43815. }
  43816. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  43817. {
  43818. EXPECT_DECLS;
  43819. WOLFSSL_EVP_PKEY* pkey = NULL;
  43820. EVP_PKEY_CTX* ctx = NULL;
  43821. int bits = 2048;
  43822. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43823. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43824. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  43825. WOLFSSL_SUCCESS);
  43826. EVP_PKEY_CTX_free(ctx);
  43827. EVP_PKEY_free(pkey);
  43828. return EXPECT_RESULT();
  43829. }
  43830. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  43831. {
  43832. EXPECT_DECLS;
  43833. /* This is large enough to be used for all key sizes */
  43834. byte key[AES_256_KEY_SIZE] = {0};
  43835. byte iv[AES_BLOCK_SIZE] = {0};
  43836. int i;
  43837. int nids[] = {
  43838. #ifdef HAVE_AES_CBC
  43839. NID_aes_128_cbc,
  43840. #endif
  43841. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43842. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43843. #ifdef HAVE_AESGCM
  43844. NID_aes_128_gcm,
  43845. #endif
  43846. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43847. #ifdef WOLFSSL_AES_COUNTER
  43848. NID_aes_128_ctr,
  43849. #endif
  43850. #ifndef NO_DES3
  43851. NID_des_cbc,
  43852. NID_des_ede3_cbc,
  43853. #endif
  43854. };
  43855. int iv_lengths[] = {
  43856. #ifdef HAVE_AES_CBC
  43857. AES_BLOCK_SIZE,
  43858. #endif
  43859. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43860. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43861. #ifdef HAVE_AESGCM
  43862. GCM_NONCE_MID_SZ,
  43863. #endif
  43864. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43865. #ifdef WOLFSSL_AES_COUNTER
  43866. AES_BLOCK_SIZE,
  43867. #endif
  43868. #ifndef NO_DES3
  43869. DES_BLOCK_SIZE,
  43870. DES_BLOCK_SIZE,
  43871. #endif
  43872. };
  43873. int nidsLen = (sizeof(nids)/sizeof(int));
  43874. for (i = 0; i < nidsLen; i++) {
  43875. const EVP_CIPHER* init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  43876. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  43877. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43878. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43879. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  43880. EVP_CIPHER_CTX_free(ctx);
  43881. }
  43882. return EXPECT_RESULT();
  43883. }
  43884. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  43885. {
  43886. EXPECT_DECLS;
  43887. byte key[AES_256_KEY_SIZE] = {0};
  43888. byte iv[AES_BLOCK_SIZE] = {0};
  43889. int i;
  43890. int nids[] = {
  43891. #ifdef HAVE_AES_CBC
  43892. NID_aes_128_cbc,
  43893. NID_aes_256_cbc,
  43894. #endif
  43895. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43896. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43897. #ifdef HAVE_AESGCM
  43898. NID_aes_128_gcm,
  43899. NID_aes_256_gcm,
  43900. #endif
  43901. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43902. #ifdef WOLFSSL_AES_COUNTER
  43903. NID_aes_128_ctr,
  43904. NID_aes_256_ctr,
  43905. #endif
  43906. #ifndef NO_DES3
  43907. NID_des_cbc,
  43908. NID_des_ede3_cbc,
  43909. #endif
  43910. };
  43911. int key_lengths[] = {
  43912. #ifdef HAVE_AES_CBC
  43913. AES_128_KEY_SIZE,
  43914. AES_256_KEY_SIZE,
  43915. #endif
  43916. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  43917. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43918. #ifdef HAVE_AESGCM
  43919. AES_128_KEY_SIZE,
  43920. AES_256_KEY_SIZE,
  43921. #endif
  43922. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  43923. #ifdef WOLFSSL_AES_COUNTER
  43924. AES_128_KEY_SIZE,
  43925. AES_256_KEY_SIZE,
  43926. #endif
  43927. #ifndef NO_DES3
  43928. DES_KEY_SIZE,
  43929. DES3_KEY_SIZE,
  43930. #endif
  43931. };
  43932. int nidsLen = (sizeof(nids)/sizeof(int));
  43933. for (i = 0; i < nidsLen; i++) {
  43934. const EVP_CIPHER *init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  43935. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  43936. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43937. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43938. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), key_lengths[i]);
  43939. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, key_lengths[i]),
  43940. WOLFSSL_SUCCESS);
  43941. EVP_CIPHER_CTX_free(ctx);
  43942. }
  43943. return EXPECT_RESULT();
  43944. }
  43945. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  43946. {
  43947. EXPECT_DECLS;
  43948. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  43949. int ivLen, keyLen;
  43950. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  43951. #ifdef HAVE_AESGCM
  43952. byte key[AES_128_KEY_SIZE] = {0};
  43953. byte iv[AES_BLOCK_SIZE] = {0};
  43954. const EVP_CIPHER *init = EVP_aes_128_gcm();
  43955. #else
  43956. byte key[DES3_KEY_SIZE] = {0};
  43957. byte iv[DES_BLOCK_SIZE] = {0};
  43958. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  43959. #endif
  43960. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  43961. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  43962. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  43963. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  43964. /* Bad cases */
  43965. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen),
  43966. WOLFSSL_FAILURE);
  43967. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen),
  43968. WOLFSSL_FAILURE);
  43969. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  43970. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  43971. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen),
  43972. WOLFSSL_FAILURE);
  43973. /* Good case */
  43974. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  43975. EVP_CIPHER_CTX_free(ctx);
  43976. #endif
  43977. return EXPECT_RESULT();
  43978. }
  43979. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  43980. {
  43981. EXPECT_DECLS;
  43982. WOLFSSL_ENGINE* e = NULL;
  43983. int id = 0;
  43984. EVP_PKEY_CTX *ctx = NULL;
  43985. ExpectNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  43986. EVP_PKEY_CTX_free(ctx);
  43987. return EXPECT_RESULT();
  43988. }
  43989. static int test_wolfSSL_EVP_rc4(void)
  43990. {
  43991. EXPECT_DECLS;
  43992. #if !defined(NO_RC4)
  43993. ExpectNotNull(wolfSSL_EVP_rc4());
  43994. #endif
  43995. return EXPECT_RESULT();
  43996. }
  43997. static int test_wolfSSL_EVP_enc_null(void)
  43998. {
  43999. EXPECT_DECLS;
  44000. ExpectNotNull(wolfSSL_EVP_enc_null());
  44001. return EXPECT_RESULT();
  44002. }
  44003. static int test_wolfSSL_EVP_rc2_cbc(void)
  44004. {
  44005. EXPECT_DECLS;
  44006. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  44007. ExpectNull(wolfSSL_EVP_rc2_cbc());
  44008. #endif
  44009. return EXPECT_RESULT();
  44010. }
  44011. static int test_wolfSSL_EVP_mdc2(void)
  44012. {
  44013. EXPECT_DECLS;
  44014. #if !defined(NO_WOLFSSL_STUB)
  44015. ExpectNull(wolfSSL_EVP_mdc2());
  44016. #endif
  44017. return EXPECT_RESULT();
  44018. }
  44019. static int test_wolfSSL_EVP_md4(void)
  44020. {
  44021. EXPECT_DECLS;
  44022. #if !defined(NO_MD4)
  44023. ExpectNotNull(wolfSSL_EVP_md4());
  44024. #endif
  44025. return EXPECT_RESULT();
  44026. }
  44027. static int test_wolfSSL_EVP_aes_256_gcm(void)
  44028. {
  44029. EXPECT_DECLS;
  44030. #ifdef HAVE_AESGCM
  44031. ExpectNotNull(wolfSSL_EVP_aes_256_gcm());
  44032. #endif
  44033. return EXPECT_RESULT();
  44034. }
  44035. static int test_wolfSSL_EVP_aes_192_gcm(void)
  44036. {
  44037. EXPECT_DECLS;
  44038. #ifdef HAVE_AESGCM
  44039. ExpectNotNull(wolfSSL_EVP_aes_192_gcm());
  44040. #endif
  44041. return EXPECT_RESULT();
  44042. }
  44043. static int test_wolfSSL_EVP_aes_256_ccm(void)
  44044. {
  44045. EXPECT_DECLS;
  44046. #ifdef HAVE_AESCCM
  44047. ExpectNotNull(wolfSSL_EVP_aes_256_ccm());
  44048. #endif
  44049. return EXPECT_RESULT();
  44050. }
  44051. static int test_wolfSSL_EVP_aes_192_ccm(void)
  44052. {
  44053. EXPECT_DECLS;
  44054. #ifdef HAVE_AESCCM
  44055. ExpectNotNull(wolfSSL_EVP_aes_192_ccm());
  44056. #endif
  44057. return EXPECT_RESULT();
  44058. }
  44059. static int test_wolfSSL_EVP_aes_128_ccm(void)
  44060. {
  44061. EXPECT_DECLS;
  44062. #ifdef HAVE_AESCCM
  44063. ExpectNotNull(wolfSSL_EVP_aes_128_ccm());
  44064. #endif
  44065. return EXPECT_RESULT();
  44066. }
  44067. static int test_wolfSSL_EVP_ripemd160(void)
  44068. {
  44069. EXPECT_DECLS;
  44070. #if !defined(NO_WOLFSSL_STUB)
  44071. ExpectNull(wolfSSL_EVP_ripemd160());
  44072. #endif
  44073. return EXPECT_RESULT();
  44074. }
  44075. static int test_wolfSSL_EVP_get_digestbynid(void)
  44076. {
  44077. EXPECT_DECLS;
  44078. #ifndef NO_MD5
  44079. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  44080. #endif
  44081. #ifndef NO_SHA
  44082. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  44083. #endif
  44084. #ifndef NO_SHA256
  44085. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha256));
  44086. #endif
  44087. ExpectNull(wolfSSL_EVP_get_digestbynid(0));
  44088. return EXPECT_RESULT();
  44089. }
  44090. static int test_wolfSSL_EVP_MD_nid(void)
  44091. {
  44092. EXPECT_DECLS;
  44093. #ifndef NO_MD5
  44094. ExpectIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  44095. #endif
  44096. #ifndef NO_SHA
  44097. ExpectIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  44098. #endif
  44099. #ifndef NO_SHA256
  44100. ExpectIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  44101. #endif
  44102. ExpectIntEQ(EVP_MD_nid(NULL), NID_undef);
  44103. return EXPECT_RESULT();
  44104. }
  44105. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  44106. {
  44107. EXPECT_DECLS;
  44108. #if defined(HAVE_ECC)
  44109. WOLFSSL_EVP_PKEY* pkey = NULL;
  44110. ExpectNull(EVP_PKEY_get0_EC_KEY(NULL));
  44111. ExpectNotNull(pkey = EVP_PKEY_new());
  44112. ExpectNull(EVP_PKEY_get0_EC_KEY(pkey));
  44113. EVP_PKEY_free(pkey);
  44114. #endif
  44115. return EXPECT_RESULT();
  44116. }
  44117. static int test_wolfSSL_EVP_X_STATE(void)
  44118. {
  44119. EXPECT_DECLS;
  44120. #if !defined(NO_DES3) && !defined(NO_RC4)
  44121. byte key[DES3_KEY_SIZE] = {0};
  44122. byte iv[DES_IV_SIZE] = {0};
  44123. EVP_CIPHER_CTX *ctx = NULL;
  44124. const EVP_CIPHER *init = NULL;
  44125. /* Bad test cases */
  44126. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44127. ExpectNotNull(init = EVP_des_ede3_cbc());
  44128. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44129. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44130. ExpectNull(wolfSSL_EVP_X_STATE(NULL));
  44131. ExpectNull(wolfSSL_EVP_X_STATE(ctx));
  44132. EVP_CIPHER_CTX_free(ctx);
  44133. ctx = NULL;
  44134. /* Good test case */
  44135. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44136. ExpectNotNull(init = wolfSSL_EVP_rc4());
  44137. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44138. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44139. ExpectNotNull(wolfSSL_EVP_X_STATE(ctx));
  44140. EVP_CIPHER_CTX_free(ctx);
  44141. #endif
  44142. return EXPECT_RESULT();
  44143. }
  44144. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  44145. {
  44146. EXPECT_DECLS;
  44147. #if !defined(NO_DES3) && !defined(NO_RC4)
  44148. byte key[DES3_KEY_SIZE] = {0};
  44149. byte iv[DES_IV_SIZE] = {0};
  44150. EVP_CIPHER_CTX *ctx = NULL;
  44151. const EVP_CIPHER *init = NULL;
  44152. /* Bad test cases */
  44153. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44154. ExpectNotNull(init = EVP_des_ede3_cbc());
  44155. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44156. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44157. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  44158. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  44159. EVP_CIPHER_CTX_free(ctx);
  44160. ctx = NULL;
  44161. /* Good test case */
  44162. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44163. ExpectNotNull(init = wolfSSL_EVP_rc4());
  44164. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44165. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44166. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  44167. EVP_CIPHER_CTX_free(ctx);
  44168. #endif
  44169. return EXPECT_RESULT();
  44170. }
  44171. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  44172. {
  44173. EXPECT_DECLS;
  44174. #if defined(HAVE_AES_CBC) || defined(HAVE_AESGCM) || \
  44175. defined(WOLFSSL_AES_COUNTER) || defined(HAVE_AES_ECB) || \
  44176. defined(WOLFSSL_AES_OFB) || !defined(NO_RC4) || \
  44177. (defined(HAVE_CHACHA) && defined(HAVE_POLY1305))
  44178. #ifdef HAVE_AES_CBC
  44179. #ifdef WOLFSSL_AES_128
  44180. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  44181. #endif
  44182. #ifdef WOLFSSL_AES_192
  44183. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  44184. #endif
  44185. #ifdef WOLFSSL_AES_256
  44186. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  44187. #endif
  44188. #endif
  44189. #ifdef HAVE_AESGCM
  44190. #ifdef WOLFSSL_AES_128
  44191. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  44192. #endif
  44193. #ifdef WOLFSSL_AES_192
  44194. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  44195. #endif
  44196. #ifdef WOLFSSL_AES_256
  44197. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  44198. #endif
  44199. #endif
  44200. #ifdef HAVE_AESCCM
  44201. #ifdef WOLFSSL_AES_128
  44202. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ccm()), 1);
  44203. #endif
  44204. #ifdef WOLFSSL_AES_192
  44205. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ccm()), 1);
  44206. #endif
  44207. #ifdef WOLFSSL_AES_256
  44208. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ccm()), 1);
  44209. #endif
  44210. #endif
  44211. #ifdef WOLFSSL_AES_COUNTER
  44212. #ifdef WOLFSSL_AES_128
  44213. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  44214. #endif
  44215. #ifdef WOLFSSL_AES_192
  44216. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  44217. #endif
  44218. #ifdef WOLFSSL_AES_256
  44219. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  44220. #endif
  44221. #endif
  44222. #ifdef HAVE_AES_ECB
  44223. #ifdef WOLFSSL_AES_128
  44224. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  44225. #endif
  44226. #ifdef WOLFSSL_AES_192
  44227. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  44228. #endif
  44229. #ifdef WOLFSSL_AES_256
  44230. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  44231. #endif
  44232. #endif
  44233. #ifdef WOLFSSL_AES_OFB
  44234. #ifdef WOLFSSL_AES_128
  44235. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  44236. #endif
  44237. #ifdef WOLFSSL_AES_192
  44238. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  44239. #endif
  44240. #ifdef WOLFSSL_AES_256
  44241. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  44242. #endif
  44243. #endif
  44244. #ifndef NO_RC4
  44245. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  44246. #endif
  44247. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  44248. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  44249. #endif
  44250. #endif
  44251. #ifdef WOLFSSL_SM4_ECB
  44252. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ecb()), SM4_BLOCK_SIZE);
  44253. #endif
  44254. #ifdef WOLFSSL_SM4_CBC
  44255. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_cbc()), SM4_BLOCK_SIZE);
  44256. #endif
  44257. #ifdef WOLFSSL_SM4_CTR
  44258. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ctr()), 1);
  44259. #endif
  44260. #ifdef WOLFSSL_SM4_GCM
  44261. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_gcm()), 1);
  44262. #endif
  44263. #ifdef WOLFSSL_SM4_CCM
  44264. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ccm()), 1);
  44265. #endif
  44266. return EXPECT_RESULT();
  44267. }
  44268. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  44269. {
  44270. EXPECT_DECLS;
  44271. int nids[] = {
  44272. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  44273. #ifdef WOLFSSL_AES_128
  44274. NID_aes_128_cbc,
  44275. #endif
  44276. #ifdef WOLFSSL_AES_192
  44277. NID_aes_192_cbc,
  44278. #endif
  44279. #ifdef WOLFSSL_AES_256
  44280. NID_aes_256_cbc,
  44281. #endif
  44282. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  44283. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  44284. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  44285. #ifdef HAVE_AESGCM
  44286. #ifdef WOLFSSL_AES_128
  44287. NID_aes_128_gcm,
  44288. #endif
  44289. #ifdef WOLFSSL_AES_192
  44290. NID_aes_192_gcm,
  44291. #endif
  44292. #ifdef WOLFSSL_AES_256
  44293. NID_aes_256_gcm,
  44294. #endif
  44295. #endif /* HAVE_AESGCM */
  44296. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  44297. #ifdef WOLFSSL_AES_COUNTER
  44298. #ifdef WOLFSSL_AES_128
  44299. NID_aes_128_ctr,
  44300. #endif
  44301. #ifdef WOLFSSL_AES_192
  44302. NID_aes_192_ctr,
  44303. #endif
  44304. #ifdef WOLFSSL_AES_256
  44305. NID_aes_256_ctr,
  44306. #endif
  44307. #endif
  44308. #ifndef NO_DES3
  44309. NID_des_cbc,
  44310. NID_des_ede3_cbc,
  44311. #endif
  44312. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  44313. NID_chacha20_poly1305,
  44314. #endif
  44315. };
  44316. int iv_lengths[] = {
  44317. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  44318. #ifdef WOLFSSL_AES_128
  44319. AES_BLOCK_SIZE,
  44320. #endif
  44321. #ifdef WOLFSSL_AES_192
  44322. AES_BLOCK_SIZE,
  44323. #endif
  44324. #ifdef WOLFSSL_AES_256
  44325. AES_BLOCK_SIZE,
  44326. #endif
  44327. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  44328. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  44329. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  44330. #ifdef HAVE_AESGCM
  44331. #ifdef WOLFSSL_AES_128
  44332. GCM_NONCE_MID_SZ,
  44333. #endif
  44334. #ifdef WOLFSSL_AES_192
  44335. GCM_NONCE_MID_SZ,
  44336. #endif
  44337. #ifdef WOLFSSL_AES_256
  44338. GCM_NONCE_MID_SZ,
  44339. #endif
  44340. #endif /* HAVE_AESGCM */
  44341. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  44342. #ifdef WOLFSSL_AES_COUNTER
  44343. #ifdef WOLFSSL_AES_128
  44344. AES_BLOCK_SIZE,
  44345. #endif
  44346. #ifdef WOLFSSL_AES_192
  44347. AES_BLOCK_SIZE,
  44348. #endif
  44349. #ifdef WOLFSSL_AES_256
  44350. AES_BLOCK_SIZE,
  44351. #endif
  44352. #endif
  44353. #ifndef NO_DES3
  44354. DES_BLOCK_SIZE,
  44355. DES_BLOCK_SIZE,
  44356. #endif
  44357. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  44358. CHACHA20_POLY1305_AEAD_IV_SIZE,
  44359. #endif
  44360. };
  44361. int i;
  44362. int nidsLen = (sizeof(nids)/sizeof(int));
  44363. for (i = 0; i < nidsLen; i++) {
  44364. const EVP_CIPHER *c = EVP_get_cipherbynid(nids[i]);
  44365. ExpectIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  44366. }
  44367. return EXPECT_RESULT();
  44368. }
  44369. static int test_wolfSSL_EVP_SignInit_ex(void)
  44370. {
  44371. EXPECT_DECLS;
  44372. WOLFSSL_EVP_MD_CTX mdCtx;
  44373. WOLFSSL_ENGINE* e = 0;
  44374. const EVP_MD* md = EVP_sha256();
  44375. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  44376. ExpectIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  44377. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  44378. return EXPECT_RESULT();
  44379. }
  44380. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  44381. {
  44382. EXPECT_DECLS;
  44383. #if !defined(NO_SHA256)
  44384. WOLFSSL_EVP_MD_CTX mdCtx;
  44385. unsigned int s = 0;
  44386. unsigned char md[WC_SHA256_DIGEST_SIZE];
  44387. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  44388. /* Bad Case */
  44389. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  44390. (HAVE_FIPS_VERSION > 2))
  44391. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  44392. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  44393. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  44394. #else
  44395. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  44396. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  44397. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  44398. #endif
  44399. /* Good Case */
  44400. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  44401. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, EVP_sha256()), WOLFSSL_SUCCESS);
  44402. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  44403. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  44404. #endif
  44405. return EXPECT_RESULT();
  44406. }
  44407. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  44408. {
  44409. EXPECT_DECLS;
  44410. #if defined(OPENSSL_EXTRA)
  44411. EVP_PKEY* pkey = NULL;
  44412. EVP_PKEY_CTX* ctx = NULL;
  44413. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44414. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44415. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  44416. /* void */
  44417. EVP_PKEY_CTX_free(ctx);
  44418. #else
  44419. /* int */
  44420. ExpectIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  44421. #endif
  44422. EVP_PKEY_free(pkey);
  44423. #endif
  44424. return EXPECT_RESULT();
  44425. }
  44426. static int test_wolfSSL_EVP_PKEY_param_check(void)
  44427. {
  44428. EXPECT_DECLS;
  44429. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  44430. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  44431. DH *dh = NULL;
  44432. DH *setDh = NULL;
  44433. EVP_PKEY *pkey = NULL;
  44434. EVP_PKEY_CTX* ctx = NULL;
  44435. FILE* f = NULL;
  44436. unsigned char buf[512];
  44437. const unsigned char* pt = buf;
  44438. const char* dh2048 = "./certs/dh2048.der";
  44439. long len = 0;
  44440. int code = -1;
  44441. XMEMSET(buf, 0, sizeof(buf));
  44442. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  44443. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  44444. if (f != XBADFILE)
  44445. XFCLOSE(f);
  44446. /* Load dh2048.der into DH with internal format */
  44447. ExpectNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  44448. ExpectIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  44449. ExpectIntEQ(code, 0);
  44450. code = -1;
  44451. pkey = wolfSSL_EVP_PKEY_new();
  44452. /* Set DH into PKEY */
  44453. ExpectIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  44454. /* create ctx from pkey */
  44455. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44456. ExpectIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  44457. /* TODO: more invalid cases */
  44458. ExpectIntEQ(EVP_PKEY_param_check(NULL), 0);
  44459. EVP_PKEY_CTX_free(ctx);
  44460. EVP_PKEY_free(pkey);
  44461. DH_free(setDh);
  44462. setDh = NULL;
  44463. DH_free(dh);
  44464. dh = NULL;
  44465. #endif
  44466. #endif
  44467. return EXPECT_RESULT();
  44468. }
  44469. static int test_wolfSSL_EVP_BytesToKey(void)
  44470. {
  44471. EXPECT_DECLS;
  44472. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  44473. byte key[AES_BLOCK_SIZE] = {0};
  44474. byte iv[AES_BLOCK_SIZE] = {0};
  44475. int count = 0;
  44476. const EVP_MD* md = EVP_sha256();
  44477. const EVP_CIPHER *type;
  44478. const unsigned char *salt = (unsigned char *)"salt1234";
  44479. int sz = 5;
  44480. const byte data[] = {
  44481. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  44482. 0x72,0x6c,0x64
  44483. };
  44484. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  44485. /* Bad cases */
  44486. ExpectIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  44487. 0);
  44488. ExpectIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  44489. 16);
  44490. md = "2";
  44491. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  44492. WOLFSSL_FAILURE);
  44493. /* Good case */
  44494. md = EVP_sha256();
  44495. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  44496. 16);
  44497. #endif
  44498. return EXPECT_RESULT();
  44499. }
  44500. static int test_evp_cipher_aes_gcm(void)
  44501. {
  44502. EXPECT_DECLS;
  44503. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  44504. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  44505. (HAVE_FIPS_VERSION >= 2)))
  44506. /*
  44507. * This test checks data at various points in the encrypt/decrypt process
  44508. * against known values produced using the same test with OpenSSL. This
  44509. * interop testing is critical for verifying the correctness of our
  44510. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  44511. * a flow supported by OpenSSL that uses the control command
  44512. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  44513. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  44514. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  44515. * wolfSSL OpenSSH clients because there was a bug in this flow that
  44516. * happened to "cancel out" if both sides of the connection had the bug.
  44517. */
  44518. enum {
  44519. NUM_ENCRYPTIONS = 3,
  44520. AAD_SIZE = 4
  44521. };
  44522. byte plainText1[] = {
  44523. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  44524. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  44525. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  44526. };
  44527. byte plainText2[] = {
  44528. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  44529. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  44530. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  44531. };
  44532. byte plainText3[] = {
  44533. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  44534. 0x5b, 0x57, 0x10
  44535. };
  44536. byte* plainTexts[NUM_ENCRYPTIONS] = {
  44537. plainText1,
  44538. plainText2,
  44539. plainText3
  44540. };
  44541. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  44542. sizeof(plainText1),
  44543. sizeof(plainText2),
  44544. sizeof(plainText3)
  44545. };
  44546. byte aad1[AAD_SIZE] = {
  44547. 0x00, 0x00, 0x00, 0x01
  44548. };
  44549. byte aad2[AAD_SIZE] = {
  44550. 0x00, 0x00, 0x00, 0x10
  44551. };
  44552. byte aad3[AAD_SIZE] = {
  44553. 0x00, 0x00, 0x01, 0x00
  44554. };
  44555. byte* aads[NUM_ENCRYPTIONS] = {
  44556. aad1,
  44557. aad2,
  44558. aad3
  44559. };
  44560. const byte iv[GCM_NONCE_MID_SZ] = {
  44561. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  44562. };
  44563. byte currentIv[GCM_NONCE_MID_SZ];
  44564. const byte key[] = {
  44565. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  44566. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  44567. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  44568. };
  44569. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  44570. {
  44571. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  44572. 0xEF
  44573. },
  44574. {
  44575. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  44576. 0xF0
  44577. },
  44578. {
  44579. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  44580. 0xF1
  44581. }
  44582. };
  44583. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  44584. {
  44585. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  44586. 0x3D, 0x32, 0x18, 0x34, 0xA9
  44587. },
  44588. {
  44589. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  44590. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  44591. },
  44592. {
  44593. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  44594. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  44595. }
  44596. };
  44597. const byte expCipherText1[] = {
  44598. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  44599. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  44600. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  44601. };
  44602. const byte expCipherText2[] = {
  44603. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  44604. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  44605. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  44606. };
  44607. const byte expCipherText3[] = {
  44608. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  44609. 0x80, 0x5E, 0x6B,
  44610. };
  44611. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  44612. expCipherText1,
  44613. expCipherText2,
  44614. expCipherText3
  44615. };
  44616. byte* cipherText = NULL;
  44617. byte* calcPlainText = NULL;
  44618. byte tag[AES_BLOCK_SIZE];
  44619. EVP_CIPHER_CTX* encCtx = NULL;
  44620. EVP_CIPHER_CTX* decCtx = NULL;
  44621. int i, j, outl;
  44622. /****************************************************/
  44623. for (i = 0; i < 3; ++i) {
  44624. ExpectNotNull(encCtx = EVP_CIPHER_CTX_new());
  44625. ExpectNotNull(decCtx = EVP_CIPHER_CTX_new());
  44626. /* First iteration, set key before IV. */
  44627. if (i == 0) {
  44628. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  44629. SSL_SUCCESS);
  44630. /*
  44631. * The call to EVP_CipherInit below (with NULL key) should clear the
  44632. * authIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  44633. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  44634. * behavior.
  44635. */
  44636. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  44637. (void*)iv), SSL_SUCCESS);
  44638. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  44639. SSL_SUCCESS);
  44640. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  44641. currentIv), SSL_FAILURE);
  44642. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  44643. SSL_SUCCESS);
  44644. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  44645. SSL_SUCCESS);
  44646. }
  44647. /* Second iteration, IV before key. */
  44648. else {
  44649. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  44650. SSL_SUCCESS);
  44651. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  44652. SSL_SUCCESS);
  44653. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  44654. SSL_SUCCESS);
  44655. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  44656. SSL_SUCCESS);
  44657. }
  44658. /*
  44659. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  44660. * been issued first.
  44661. */
  44662. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  44663. currentIv), SSL_FAILURE);
  44664. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  44665. (void*)iv), SSL_SUCCESS);
  44666. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  44667. (void*)iv), SSL_SUCCESS);
  44668. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  44669. /*************** Encrypt ***************/
  44670. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  44671. currentIv), SSL_SUCCESS);
  44672. /* Check current IV against expected. */
  44673. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  44674. /* Add AAD. */
  44675. if (i == 2) {
  44676. /* Test streaming API. */
  44677. ExpectIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  44678. AAD_SIZE), SSL_SUCCESS);
  44679. }
  44680. else {
  44681. ExpectIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  44682. AAD_SIZE);
  44683. }
  44684. ExpectNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  44685. DYNAMIC_TYPE_TMP_BUFFER));
  44686. /* Encrypt plaintext. */
  44687. if (i == 2) {
  44688. ExpectIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  44689. plainTexts[j], plainTextSzs[j]),
  44690. SSL_SUCCESS);
  44691. }
  44692. else {
  44693. ExpectIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  44694. plainTextSzs[j]), plainTextSzs[j]);
  44695. }
  44696. if (i == 2) {
  44697. ExpectIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  44698. SSL_SUCCESS);
  44699. }
  44700. else {
  44701. /*
  44702. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  44703. * akin to calling EVP_CipherFinal.
  44704. */
  44705. ExpectIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  44706. }
  44707. /* Check ciphertext against expected. */
  44708. ExpectIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  44709. 0);
  44710. /* Get and check tag against expected. */
  44711. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  44712. sizeof(tag), tag), SSL_SUCCESS);
  44713. ExpectIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  44714. /*************** Decrypt ***************/
  44715. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  44716. currentIv), SSL_SUCCESS);
  44717. /* Check current IV against expected. */
  44718. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  44719. /* Add AAD. */
  44720. if (i == 2) {
  44721. /* Test streaming API. */
  44722. ExpectIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  44723. AAD_SIZE), SSL_SUCCESS);
  44724. }
  44725. else {
  44726. ExpectIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  44727. AAD_SIZE);
  44728. }
  44729. /* Set expected tag. */
  44730. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  44731. sizeof(tag), tag), SSL_SUCCESS);
  44732. /* Decrypt ciphertext. */
  44733. ExpectNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  44734. DYNAMIC_TYPE_TMP_BUFFER));
  44735. if (i == 2) {
  44736. ExpectIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  44737. cipherText, plainTextSzs[j]),
  44738. SSL_SUCCESS);
  44739. }
  44740. else {
  44741. /* This first EVP_Cipher call will check the tag, too. */
  44742. ExpectIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  44743. plainTextSzs[j]), plainTextSzs[j]);
  44744. }
  44745. if (i == 2) {
  44746. ExpectIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  44747. SSL_SUCCESS);
  44748. }
  44749. else {
  44750. ExpectIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  44751. }
  44752. /* Check plaintext against expected. */
  44753. ExpectIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  44754. 0);
  44755. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44756. cipherText = NULL;
  44757. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44758. calcPlainText = NULL;
  44759. }
  44760. EVP_CIPHER_CTX_free(encCtx);
  44761. encCtx = NULL;
  44762. EVP_CIPHER_CTX_free(decCtx);
  44763. decCtx = NULL;
  44764. }
  44765. #endif
  44766. return EXPECT_RESULT();
  44767. }
  44768. static int test_wolfSSL_OBJ_ln(void)
  44769. {
  44770. EXPECT_DECLS;
  44771. const int nid_set[] = {
  44772. NID_commonName,
  44773. NID_serialNumber,
  44774. NID_countryName,
  44775. NID_localityName,
  44776. NID_stateOrProvinceName,
  44777. NID_organizationName,
  44778. NID_organizationalUnitName,
  44779. NID_domainComponent,
  44780. NID_businessCategory,
  44781. NID_jurisdictionCountryName,
  44782. NID_jurisdictionStateOrProvinceName,
  44783. NID_emailAddress
  44784. };
  44785. const char* ln_set[] = {
  44786. "commonName",
  44787. "serialNumber",
  44788. "countryName",
  44789. "localityName",
  44790. "stateOrProvinceName",
  44791. "organizationName",
  44792. "organizationalUnitName",
  44793. "domainComponent",
  44794. "businessCategory",
  44795. "jurisdictionCountryName",
  44796. "jurisdictionStateOrProvinceName",
  44797. "emailAddress",
  44798. };
  44799. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  44800. ExpectIntEQ(OBJ_ln2nid(NULL), NID_undef);
  44801. #ifdef HAVE_ECC
  44802. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  44803. {
  44804. EC_builtin_curve r[27];
  44805. size_t nCurves = sizeof(r) / sizeof(r[0]);
  44806. nCurves = EC_get_builtin_curves(r, nCurves);
  44807. for (i = 0; i < nCurves; i++) {
  44808. /* skip ECC_CURVE_INVALID */
  44809. if (r[i].nid != ECC_CURVE_INVALID) {
  44810. ExpectIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  44811. ExpectStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  44812. }
  44813. }
  44814. }
  44815. #endif
  44816. #endif
  44817. for (i = 0; i < maxIdx; i++) {
  44818. ExpectIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  44819. ExpectStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  44820. }
  44821. return EXPECT_RESULT();
  44822. }
  44823. static int test_wolfSSL_OBJ_sn(void)
  44824. {
  44825. EXPECT_DECLS;
  44826. int i = 0, maxIdx = 7;
  44827. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  44828. NID_stateOrProvinceName,NID_organizationName,
  44829. NID_organizationalUnitName,NID_emailAddress};
  44830. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  44831. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  44832. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  44833. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  44834. WOLFSSL_EMAIL_ADDR};
  44835. ExpectIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  44836. for (i = 0; i < maxIdx; i++) {
  44837. ExpectIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  44838. ExpectStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  44839. }
  44840. return EXPECT_RESULT();
  44841. }
  44842. #if !defined(NO_BIO)
  44843. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  44844. {
  44845. return lh_strhash(s[3]);
  44846. }
  44847. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  44848. {
  44849. return XSTRCMP(a[3], b[3]);
  44850. }
  44851. #endif
  44852. static int test_wolfSSL_TXT_DB(void)
  44853. {
  44854. EXPECT_DECLS;
  44855. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  44856. BIO *bio = NULL;
  44857. TXT_DB *db = NULL;
  44858. const int columns = 6;
  44859. const char *fields[6] = {
  44860. "V",
  44861. "320926161116Z",
  44862. "",
  44863. "12BD",
  44864. "unknown",
  44865. "/CN=rsa doe",
  44866. };
  44867. char** fields_copy = NULL;
  44868. /* Test read */
  44869. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  44870. ExpectIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  44871. ExpectNotNull(db = TXT_DB_read(bio, columns));
  44872. ExpectNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  44873. DYNAMIC_TYPE_OPENSSL));
  44874. if (fields_copy != NULL) {
  44875. XMEMCPY(fields_copy, fields, sizeof(fields));
  44876. }
  44877. ExpectIntEQ(TXT_DB_insert(db, fields_copy), 1);
  44878. if (EXPECT_FAIL()) {
  44879. XFREE(fields_copy, NULL, DYNAMIC_TYPE_OPENSSL);
  44880. }
  44881. BIO_free(bio);
  44882. bio = NULL;
  44883. /* Test write */
  44884. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  44885. ExpectIntEQ(TXT_DB_write(bio, db), 1484);
  44886. BIO_free(bio);
  44887. /* Test index */
  44888. ExpectIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  44889. (wolf_lh_compare_cb)TXT_DB_cmp), 1);
  44890. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44891. fields[3] = "12DA";
  44892. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44893. fields[3] = "FFFF";
  44894. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44895. fields[3] = "";
  44896. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  44897. TXT_DB_free(db);
  44898. #endif
  44899. return EXPECT_RESULT();
  44900. }
  44901. static int test_wolfSSL_NCONF(void)
  44902. {
  44903. EXPECT_DECLS;
  44904. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  44905. const char* confFile = "./tests/NCONF_test.cnf";
  44906. CONF* conf = NULL;
  44907. long eline = 0;
  44908. long num = 0;
  44909. ExpectNotNull(conf = NCONF_new(NULL));
  44910. ExpectIntEQ(NCONF_load(conf, confFile, &eline), 1);
  44911. ExpectIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  44912. ExpectIntEQ(num, 1234);
  44913. ExpectIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  44914. ExpectIntEQ(num, 4321);
  44915. ExpectStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  44916. ExpectStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  44917. "./test-dir/file1");
  44918. ExpectStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  44919. "./test-dir/file1:./test-dir/file2:./section1:file2");
  44920. NCONF_free(conf);
  44921. #endif
  44922. return EXPECT_RESULT();
  44923. }
  44924. #endif /* OPENSSL_ALL */
  44925. static int test_wolfSSL_X509V3_EXT_get(void) {
  44926. EXPECT_DECLS;
  44927. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  44928. XFILE f = XBADFILE;
  44929. int numOfExt =0;
  44930. int extNid = 0;
  44931. int i = 0;
  44932. WOLFSSL_X509* x509 = NULL;
  44933. WOLFSSL_X509_EXTENSION* ext = NULL;
  44934. const WOLFSSL_v3_ext_method* method = NULL;
  44935. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  44936. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  44937. if (f != XBADFILE)
  44938. XFCLOSE(f);
  44939. /* wolfSSL_X509V3_EXT_get() return struct and nid test */
  44940. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  44941. for (i = 0; i < numOfExt; i++) {
  44942. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  44943. ExpectIntNE((extNid = ext->obj->nid), NID_undef);
  44944. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  44945. ExpectIntEQ(method->ext_nid, extNid);
  44946. }
  44947. /* wolfSSL_X509V3_EXT_get() NULL argument test */
  44948. ExpectNull(method = wolfSSL_X509V3_EXT_get(NULL));
  44949. wolfSSL_X509_free(x509);
  44950. #endif
  44951. return EXPECT_RESULT();
  44952. }
  44953. static int test_wolfSSL_X509V3_EXT_nconf(void)
  44954. {
  44955. EXPECT_DECLS;
  44956. #ifdef OPENSSL_ALL
  44957. const char *ext_names[] = {
  44958. "subjectKeyIdentifier",
  44959. "authorityKeyIdentifier",
  44960. "subjectAltName",
  44961. "keyUsage",
  44962. "extendedKeyUsage",
  44963. };
  44964. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  44965. int ext_nids[] = {
  44966. NID_subject_key_identifier,
  44967. NID_authority_key_identifier,
  44968. NID_subject_alt_name,
  44969. NID_key_usage,
  44970. NID_ext_key_usage,
  44971. };
  44972. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  44973. const char *ext_values[] = {
  44974. "hash",
  44975. "hash",
  44976. "DNS:example.com, IP:127.0.0.1",
  44977. "digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,"
  44978. "keyAgreement,keyCertSign,cRLSign,encipherOnly,decipherOnly",
  44979. "serverAuth,clientAuth,codeSigning,emailProtection,timeStamping,"
  44980. "OCSPSigning",
  44981. };
  44982. size_t i;
  44983. X509_EXTENSION* ext = NULL;
  44984. X509* x509 = NULL;
  44985. unsigned int keyUsageFlags;
  44986. unsigned int extKeyUsageFlags;
  44987. ExpectNotNull(x509 = X509_new());
  44988. /* keyUsage / extKeyUsage should match string above */
  44989. keyUsageFlags = KU_DIGITAL_SIGNATURE
  44990. | KU_NON_REPUDIATION
  44991. | KU_KEY_ENCIPHERMENT
  44992. | KU_DATA_ENCIPHERMENT
  44993. | KU_KEY_AGREEMENT
  44994. | KU_KEY_CERT_SIGN
  44995. | KU_CRL_SIGN
  44996. | KU_ENCIPHER_ONLY
  44997. | KU_DECIPHER_ONLY;
  44998. extKeyUsageFlags = XKU_SSL_CLIENT
  44999. | XKU_SSL_SERVER
  45000. | XKU_CODE_SIGN
  45001. | XKU_SMIME
  45002. | XKU_TIMESTAMP
  45003. | XKU_OCSP_SIGN;
  45004. for (i = 0; i < ext_names_count; i++) {
  45005. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  45006. ext_values[i]));
  45007. X509_EXTENSION_free(ext);
  45008. ext = NULL;
  45009. }
  45010. for (i = 0; i < ext_nids_count; i++) {
  45011. ExpectNotNull(ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i],
  45012. ext_values[i]));
  45013. X509_EXTENSION_free(ext);
  45014. ext = NULL;
  45015. }
  45016. /* Test adding extension to X509 */
  45017. for (i = 0; i < ext_nids_count; i++) {
  45018. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  45019. ext_values[i]));
  45020. ExpectIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  45021. if (ext_nids[i] == NID_key_usage) {
  45022. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  45023. }
  45024. else if (ext_nids[i] == NID_ext_key_usage) {
  45025. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  45026. }
  45027. X509_EXTENSION_free(ext);
  45028. ext = NULL;
  45029. }
  45030. X509_free(x509);
  45031. #endif
  45032. return EXPECT_RESULT();
  45033. }
  45034. static int test_wolfSSL_X509V3_EXT(void) {
  45035. EXPECT_DECLS;
  45036. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45037. XFILE f = XBADFILE;
  45038. int numOfExt = 0, nid = 0, i = 0, expected, actual = 0;
  45039. char* str = NULL;
  45040. unsigned char* data = NULL;
  45041. const WOLFSSL_v3_ext_method* method = NULL;
  45042. WOLFSSL_X509* x509 = NULL;
  45043. WOLFSSL_X509_EXTENSION* ext = NULL;
  45044. WOLFSSL_X509_EXTENSION* ext2 = NULL;
  45045. WOLFSSL_ASN1_OBJECT *obj = NULL;
  45046. WOLFSSL_ASN1_OBJECT *adObj = NULL;
  45047. WOLFSSL_ASN1_STRING* asn1str = NULL;
  45048. WOLFSSL_AUTHORITY_KEYID* aKeyId = NULL;
  45049. WOLFSSL_AUTHORITY_INFO_ACCESS* aia = NULL;
  45050. WOLFSSL_BASIC_CONSTRAINTS* bc = NULL;
  45051. WOLFSSL_ACCESS_DESCRIPTION* ad = NULL;
  45052. WOLFSSL_GENERAL_NAME* gn = NULL;
  45053. /* Check NULL argument */
  45054. ExpectNull(wolfSSL_X509V3_EXT_d2i(NULL));
  45055. /* Using OCSP cert with X509V3 extensions */
  45056. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  45057. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45058. if (f != XBADFILE)
  45059. XFCLOSE(f);
  45060. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  45061. /* Basic Constraints */
  45062. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45063. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45064. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  45065. ExpectNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  45066. ExpectIntEQ(bc->ca, 1);
  45067. ExpectNull(bc->pathlen);
  45068. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  45069. bc = NULL;
  45070. i++;
  45071. /* Subject Key Identifier */
  45072. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45073. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45074. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  45075. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  45076. ExpectNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  45077. asn1str));
  45078. X509_EXTENSION_free(ext2);
  45079. ext2 = NULL;
  45080. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  45081. ExpectNotNull(method->i2s);
  45082. ExpectNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  45083. wolfSSL_ASN1_STRING_free(asn1str);
  45084. asn1str = NULL;
  45085. if (str != NULL) {
  45086. actual = strcmp(str,
  45087. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  45088. }
  45089. ExpectIntEQ(actual, 0);
  45090. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45091. str = NULL;
  45092. i++;
  45093. /* Authority Key Identifier */
  45094. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45095. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45096. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  45097. ExpectNotNull(aKeyId = (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(
  45098. ext));
  45099. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  45100. ExpectNotNull(asn1str = aKeyId->keyid);
  45101. ExpectNotNull(str = wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method,
  45102. asn1str));
  45103. asn1str = NULL;
  45104. if (str != NULL) {
  45105. actual = strcmp(str,
  45106. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  45107. }
  45108. ExpectIntEQ(actual, 0);
  45109. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45110. str = NULL;
  45111. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  45112. aKeyId = NULL;
  45113. i++;
  45114. /* Key Usage */
  45115. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45116. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45117. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  45118. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  45119. #if defined(WOLFSSL_QT)
  45120. ExpectNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  45121. #else
  45122. ExpectNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  45123. #endif
  45124. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  45125. if (data != NULL) {
  45126. #ifdef BIG_ENDIAN_ORDER
  45127. actual = data[1];
  45128. #else
  45129. actual = data[0];
  45130. #endif
  45131. }
  45132. ExpectIntEQ(actual, expected);
  45133. wolfSSL_ASN1_STRING_free(asn1str);
  45134. asn1str = NULL;
  45135. #if 1
  45136. i++;
  45137. /* Authority Info Access */
  45138. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45139. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45140. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  45141. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  45142. ext));
  45143. #if defined(WOLFSSL_QT)
  45144. ExpectIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  45145. #else
  45146. ExpectIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  45147. #endif
  45148. /* URI entry is an ACCESS_DESCRIPTION type */
  45149. #if defined(WOLFSSL_QT)
  45150. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  45151. #else
  45152. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  45153. #endif
  45154. ExpectNotNull(adObj = ad->method);
  45155. /* Make sure nid is OCSP */
  45156. ExpectIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  45157. /* GENERAL_NAME stores URI as an ASN1_STRING */
  45158. ExpectNotNull(gn = ad->location);
  45159. ExpectIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  45160. ExpectNotNull(asn1str = gn->d.uniformResourceIdentifier);
  45161. ExpectIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  45162. #if defined(WOLFSSL_QT)
  45163. ExpectNotNull(str = (char*)ASN1_STRING_get0_data(asn1str));
  45164. #else
  45165. ExpectNotNull(str = (char*)wolfSSL_ASN1_STRING_data(asn1str));
  45166. #endif
  45167. if (str != NULL) {
  45168. actual = strcmp(str, "http://127.0.0.1:22220");
  45169. }
  45170. ExpectIntEQ(actual, 0);
  45171. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  45172. aia = NULL;
  45173. #else
  45174. (void) aia; (void) ad; (void) adObj; (void) gn;
  45175. #endif
  45176. wolfSSL_X509_free(x509);
  45177. #endif
  45178. return EXPECT_RESULT();
  45179. }
  45180. static int test_wolfSSL_X509_get_extension_flags(void)
  45181. {
  45182. EXPECT_DECLS;
  45183. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  45184. XFILE f = XBADFILE;
  45185. X509* x509 = NULL;
  45186. unsigned int extFlags;
  45187. unsigned int keyUsageFlags;
  45188. unsigned int extKeyUsageFlags;
  45189. /* client-int-cert.pem has the following extension flags. */
  45190. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  45191. /* and the following key usage flags. */
  45192. keyUsageFlags = KU_DIGITAL_SIGNATURE
  45193. | KU_NON_REPUDIATION
  45194. | KU_KEY_ENCIPHERMENT;
  45195. /* and the following extended key usage flags. */
  45196. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  45197. ExpectTrue((f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb")) !=
  45198. XBADFILE);
  45199. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  45200. if (f != XBADFILE) {
  45201. XFCLOSE(f);
  45202. f = XBADFILE;
  45203. }
  45204. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  45205. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  45206. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  45207. X509_free(x509);
  45208. x509 = NULL;
  45209. /* client-cert-ext.pem has the following extension flags. */
  45210. extFlags = EXFLAG_KUSAGE;
  45211. /* and the following key usage flags. */
  45212. keyUsageFlags = KU_DIGITAL_SIGNATURE
  45213. | KU_KEY_CERT_SIGN
  45214. | KU_CRL_SIGN;
  45215. ExpectTrue((f = fopen("./certs/client-cert-ext.pem", "rb")) != XBADFILE);
  45216. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  45217. if (f != XBADFILE)
  45218. XFCLOSE(f);
  45219. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  45220. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  45221. X509_free(x509);
  45222. #endif /* OPENSSL_ALL */
  45223. return EXPECT_RESULT();
  45224. }
  45225. static int test_wolfSSL_X509_get_ext(void)
  45226. {
  45227. EXPECT_DECLS;
  45228. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45229. int ret = 0;
  45230. XFILE f = XBADFILE;
  45231. WOLFSSL_X509* x509 = NULL;
  45232. WOLFSSL_X509_EXTENSION* foundExtension;
  45233. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45234. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45235. if (f != XBADFILE)
  45236. XFCLOSE(f);
  45237. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  45238. /* wolfSSL_X509_get_ext() valid input */
  45239. ExpectNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  45240. /* wolfSSL_X509_get_ext() valid x509, idx out of bounds */
  45241. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  45242. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  45243. /* wolfSSL_X509_get_ext() NULL x509, idx out of bounds */
  45244. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  45245. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  45246. /* wolfSSL_X509_get_ext() NULL x509, valid idx */
  45247. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  45248. wolfSSL_X509_free(x509);
  45249. #endif
  45250. return EXPECT_RESULT();
  45251. }
  45252. static int test_wolfSSL_X509_get_ext_by_NID(void)
  45253. {
  45254. EXPECT_DECLS;
  45255. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  45256. int rc = 0;
  45257. XFILE f = XBADFILE;
  45258. WOLFSSL_X509* x509 = NULL;
  45259. ASN1_OBJECT* obj = NULL;
  45260. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45261. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45262. if (f != XBADFILE)
  45263. XFCLOSE(f);
  45264. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  45265. -1), 0);
  45266. /* Start search from last location (should fail) */
  45267. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  45268. rc), -1);
  45269. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  45270. -2), -1);
  45271. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints,
  45272. -1), -1);
  45273. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1), -1);
  45274. /* NID_ext_key_usage, check also its nid and oid */
  45275. ExpectIntGT(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1),
  45276. -1);
  45277. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(
  45278. x509, rc)));
  45279. ExpectIntEQ(obj->nid, NID_ext_key_usage);
  45280. ExpectIntEQ(obj->type, EXT_KEY_USAGE_OID);
  45281. wolfSSL_X509_free(x509);
  45282. #endif
  45283. return EXPECT_RESULT();
  45284. }
  45285. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  45286. {
  45287. EXPECT_DECLS;
  45288. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  45289. int rc = 0;
  45290. XFILE f = XBADFILE;
  45291. WOLFSSL_X509* x509 = NULL;
  45292. WOLFSSL_X509_EXTENSION* ext = NULL;
  45293. WOLFSSL_ASN1_STRING* sanString = NULL;
  45294. byte* sanDer = NULL;
  45295. const byte expectedDer[] = {
  45296. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  45297. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  45298. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45299. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  45300. if (f != XBADFILE)
  45301. XFCLOSE(f);
  45302. ExpectIntNE(rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1), -1);
  45303. ExpectNotNull(ext = X509_get_ext(x509, rc));
  45304. ExpectNotNull(sanString = X509_EXTENSION_get_data(ext));
  45305. ExpectIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  45306. ExpectNotNull(sanDer = ASN1_STRING_data(sanString));
  45307. ExpectIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  45308. X509_free(x509);
  45309. #endif
  45310. return EXPECT_RESULT();
  45311. }
  45312. static int test_wolfSSL_X509_EXTENSION_new(void)
  45313. {
  45314. EXPECT_DECLS;
  45315. #if defined (OPENSSL_ALL)
  45316. WOLFSSL_X509_EXTENSION* ext = NULL;
  45317. ExpectNotNull(ext = wolfSSL_X509_EXTENSION_new());
  45318. ExpectNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  45319. wolfSSL_X509_EXTENSION_free(ext);
  45320. #endif
  45321. return EXPECT_RESULT();
  45322. }
  45323. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  45324. {
  45325. EXPECT_DECLS;
  45326. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45327. WOLFSSL_X509* x509 = NULL;
  45328. WOLFSSL_X509_EXTENSION* ext = NULL;
  45329. WOLFSSL_ASN1_OBJECT* o = NULL;
  45330. XFILE file = XBADFILE;
  45331. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45332. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  45333. if (file != XBADFILE)
  45334. XFCLOSE(file);
  45335. /* wolfSSL_X509_EXTENSION_get_object() testing ext idx 0 */
  45336. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  45337. ExpectNull(wolfSSL_X509_EXTENSION_get_object(NULL));
  45338. ExpectNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  45339. ExpectIntEQ(o->nid, 128);
  45340. /* wolfSSL_X509_EXTENSION_get_object() NULL argument */
  45341. ExpectNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  45342. wolfSSL_X509_free(x509);
  45343. #endif
  45344. return EXPECT_RESULT();
  45345. }
  45346. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  45347. {
  45348. EXPECT_DECLS;
  45349. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45350. WOLFSSL_X509* x509 = NULL;
  45351. WOLFSSL_X509_EXTENSION* ext = NULL;
  45352. WOLFSSL_ASN1_STRING* str = NULL;
  45353. XFILE file = XBADFILE;
  45354. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45355. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  45356. if (file != XBADFILE)
  45357. XFCLOSE(file);
  45358. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  45359. ExpectNull(str = wolfSSL_X509_EXTENSION_get_data(NULL));
  45360. ExpectNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  45361. wolfSSL_X509_free(x509);
  45362. #endif
  45363. return EXPECT_RESULT();
  45364. }
  45365. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  45366. {
  45367. EXPECT_DECLS;
  45368. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45369. WOLFSSL_X509* x509 = NULL;
  45370. WOLFSSL_X509_EXTENSION* ext = NULL;
  45371. XFILE file = XBADFILE;
  45372. int crit = 0;
  45373. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45374. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  45375. if (file != XBADFILE)
  45376. XFCLOSE(file);
  45377. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  45378. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(NULL), BAD_FUNC_ARG);
  45379. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(ext), 0);
  45380. wolfSSL_X509_free(x509);
  45381. #endif
  45382. return EXPECT_RESULT();
  45383. }
  45384. static int test_wolfSSL_X509V3_EXT_print(void)
  45385. {
  45386. EXPECT_DECLS;
  45387. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  45388. !defined(NO_RSA)
  45389. {
  45390. XFILE f = XBADFILE;
  45391. WOLFSSL_X509* x509 = NULL;
  45392. X509_EXTENSION * ext = NULL;
  45393. int loc = 0;
  45394. BIO *bio = NULL;
  45395. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  45396. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45397. if (f != XBADFILE)
  45398. fclose(f);
  45399. ExpectNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  45400. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  45401. NID_basic_constraints, -1), -1);
  45402. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  45403. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  45404. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  45405. NID_subject_key_identifier, -1), -1);
  45406. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  45407. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  45408. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  45409. NID_authority_key_identifier, -1), -1);
  45410. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  45411. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  45412. wolfSSL_BIO_free(bio);
  45413. wolfSSL_X509_free(x509);
  45414. }
  45415. {
  45416. X509 *x509 = NULL;
  45417. BIO *bio = NULL;
  45418. X509_EXTENSION *ext = NULL;
  45419. unsigned int i = 0;
  45420. unsigned int idx = 0;
  45421. /* Some NIDs to test with */
  45422. int nids[] = {
  45423. /* NID_key_usage, currently X509_get_ext returns this as a bit
  45424. * string, which messes up X509V3_EXT_print */
  45425. /* NID_ext_key_usage, */
  45426. NID_subject_alt_name,
  45427. };
  45428. int* n = NULL;
  45429. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  45430. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  45431. WOLFSSL_FILETYPE_PEM));
  45432. ExpectIntGT(fprintf(stderr, "\nPrinting extension values:\n"), 0);
  45433. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  45434. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  45435. * update the index */
  45436. ExpectIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  45437. ExpectNotNull(ext = X509_get_ext(x509, idx));
  45438. ExpectIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  45439. ExpectIntGT(fprintf(stderr, "\n"), 0);
  45440. }
  45441. BIO_free(bio);
  45442. X509_free(x509);
  45443. }
  45444. #endif
  45445. return EXPECT_RESULT();
  45446. }
  45447. static int test_wolfSSL_X509_cmp(void)
  45448. {
  45449. EXPECT_DECLS;
  45450. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  45451. XFILE file1 = XBADFILE;
  45452. XFILE file2 = XBADFILE;
  45453. WOLFSSL_X509* cert1 = NULL;
  45454. WOLFSSL_X509* cert2 = NULL;
  45455. ExpectTrue((file1 = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45456. ExpectTrue((file2 = XFOPEN("./certs/3072/client-cert.pem", "rb")) !=
  45457. XBADFILE);
  45458. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  45459. ExpectNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  45460. if (file1 != XBADFILE)
  45461. fclose(file1);
  45462. if (file2 != XBADFILE)
  45463. fclose(file2);
  45464. /* wolfSSL_X509_cmp() testing matching certs */
  45465. ExpectIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  45466. /* wolfSSL_X509_cmp() testing mismatched certs */
  45467. ExpectIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  45468. /* wolfSSL_X509_cmp() testing NULL, valid args */
  45469. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  45470. /* wolfSSL_X509_cmp() testing valid, NULL args */
  45471. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  45472. /* wolfSSL_X509_cmp() testing NULL, NULL args */
  45473. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  45474. wolfSSL_X509_free(cert1);
  45475. wolfSSL_X509_free(cert2);
  45476. #endif
  45477. return EXPECT_RESULT();
  45478. }
  45479. static int test_wolfSSL_EVP_PKEY_up_ref(void)
  45480. {
  45481. EXPECT_DECLS;
  45482. #if defined(OPENSSL_ALL)
  45483. EVP_PKEY* pkey;
  45484. pkey = EVP_PKEY_new();
  45485. ExpectNotNull(pkey);
  45486. ExpectIntEQ(EVP_PKEY_up_ref(NULL), 0);
  45487. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  45488. EVP_PKEY_free(pkey);
  45489. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  45490. EVP_PKEY_free(pkey);
  45491. EVP_PKEY_free(pkey);
  45492. #endif
  45493. return EXPECT_RESULT();
  45494. }
  45495. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  45496. {
  45497. EXPECT_DECLS;
  45498. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  45499. EVP_PKEY* pkey = NULL;
  45500. const unsigned char* p;
  45501. unsigned char *der = NULL;
  45502. unsigned char *tmp = NULL;
  45503. int derLen;
  45504. p = client_keypub_der_2048;
  45505. /* Check that key can be successfully decoded. */
  45506. ExpectNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  45507. sizeof_client_keypub_der_2048));
  45508. /* Check that key can be successfully encoded. */
  45509. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  45510. /* Ensure that the encoded version matches the original. */
  45511. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  45512. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  45513. /* Do same test except with pre-allocated buffer to ensure the der pointer
  45514. * is advanced. */
  45515. tmp = der;
  45516. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  45517. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  45518. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  45519. ExpectTrue(der + derLen == tmp);
  45520. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  45521. EVP_PKEY_free(pkey);
  45522. #endif
  45523. return EXPECT_RESULT();
  45524. }
  45525. static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
  45526. {
  45527. EXPECT_DECLS;
  45528. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(NO_CERTS) && \
  45529. !defined(NO_ASN) && !defined(NO_PWDBASED)
  45530. EVP_PKEY* pkey = NULL;
  45531. const unsigned char* p;
  45532. unsigned char *der = NULL;
  45533. unsigned char *tmp = NULL;
  45534. int derLen;
  45535. unsigned char pub_buf[65];
  45536. const int pub_len = 65;
  45537. BN_CTX* ctx;
  45538. EC_GROUP* curve = NULL;
  45539. EC_KEY* ephemeral_key = NULL;
  45540. const EC_POINT* h;
  45541. /* Generate an x963 key pair and get public part into pub_buf */
  45542. ExpectNotNull(ctx = BN_CTX_new());
  45543. ExpectNotNull(curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  45544. ExpectNotNull(ephemeral_key = EC_KEY_new_by_curve_name(
  45545. NID_X9_62_prime256v1));
  45546. ExpectIntEQ(EC_KEY_generate_key(ephemeral_key), 1);
  45547. ExpectNotNull(h = EC_KEY_get0_public_key(ephemeral_key));
  45548. ExpectIntEQ(pub_len, EC_POINT_point2oct(curve, h,
  45549. POINT_CONVERSION_UNCOMPRESSED, pub_buf, pub_len, ctx));
  45550. /* Prepare the EVP_PKEY */
  45551. ExpectNotNull(pkey = EVP_PKEY_new());
  45552. p = pub_buf;
  45553. /* Check that key can be successfully decoded. */
  45554. ExpectNotNull(wolfSSL_d2i_PublicKey(EVP_PKEY_EC, &pkey, &p,
  45555. pub_len));
  45556. /* Check that key can be successfully encoded. */
  45557. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  45558. /* Ensure that the encoded version matches the original. */
  45559. ExpectIntEQ(derLen, pub_len);
  45560. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  45561. /* Do same test except with pre-allocated buffer to ensure the der pointer
  45562. * is advanced. */
  45563. tmp = der;
  45564. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  45565. ExpectIntEQ(derLen, pub_len);
  45566. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  45567. ExpectTrue(der + derLen == tmp);
  45568. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  45569. EVP_PKEY_free(pkey);
  45570. EC_KEY_free(ephemeral_key);
  45571. EC_GROUP_free(curve);
  45572. #endif
  45573. return EXPECT_RESULT();
  45574. }
  45575. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  45576. {
  45577. EXPECT_DECLS;
  45578. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  45579. DSA* dsa = NULL;
  45580. byte derIn[] = {
  45581. 0x30, 0x82, 0x01, 0x1f, 0x02, 0x81, 0x81, 0x00,
  45582. 0xcd, 0xde, 0x25, 0x68, 0x80, 0x53, 0x0d, 0xe5,
  45583. 0x77, 0xd6, 0xd2, 0x90, 0x39, 0x3f, 0x90, 0xa2,
  45584. 0x3f, 0x33, 0x94, 0x6e, 0xe8, 0x4f, 0x2b, 0x63,
  45585. 0xab, 0x30, 0xab, 0x15, 0xba, 0x11, 0xea, 0x8a,
  45586. 0x5d, 0x8d, 0xcc, 0xb8, 0xd4, 0xa1, 0xd5, 0xc1,
  45587. 0x47, 0x9d, 0x5a, 0x73, 0x6a, 0x62, 0x49, 0xd1,
  45588. 0x06, 0x07, 0x67, 0xf6, 0x2f, 0xa3, 0x39, 0xbd,
  45589. 0x4e, 0x0d, 0xb4, 0xd3, 0x22, 0x23, 0x84, 0xec,
  45590. 0x93, 0x26, 0x5a, 0x49, 0xee, 0x7c, 0x89, 0x48,
  45591. 0x66, 0x4d, 0xe8, 0xe8, 0xd8, 0x50, 0xfb, 0xa5,
  45592. 0x71, 0x9f, 0x22, 0x18, 0xe5, 0xe6, 0x0b, 0x46,
  45593. 0x87, 0x66, 0xee, 0x52, 0x8f, 0x46, 0x4f, 0xb5,
  45594. 0x03, 0xce, 0xed, 0xe3, 0xbe, 0xe5, 0xb5, 0x81,
  45595. 0xd2, 0x59, 0xe9, 0xc0, 0xad, 0x4d, 0xd0, 0x4d,
  45596. 0x26, 0xf7, 0xba, 0x50, 0xe8, 0xc9, 0x8f, 0xfe,
  45597. 0x24, 0x19, 0x3d, 0x2e, 0xa7, 0x52, 0x3c, 0x6d,
  45598. 0x02, 0x15, 0x00, 0xfb, 0x47, 0xfb, 0xec, 0x81,
  45599. 0x20, 0xc8, 0x1c, 0xe9, 0x4a, 0xba, 0x04, 0x6f,
  45600. 0x19, 0x9b, 0x94, 0xee, 0x82, 0x67, 0xd3, 0x02,
  45601. 0x81, 0x81, 0x00, 0x9b, 0x95, 0xbb, 0x85, 0xc5,
  45602. 0x58, 0x4a, 0x32, 0x9c, 0xaa, 0x44, 0x85, 0xd6,
  45603. 0x68, 0xdc, 0x3e, 0x14, 0xf4, 0xce, 0x6d, 0xa3,
  45604. 0x49, 0x38, 0xea, 0xd6, 0x61, 0x48, 0x92, 0x5a,
  45605. 0x40, 0x95, 0x49, 0x38, 0xaa, 0xe1, 0x39, 0x29,
  45606. 0x68, 0x58, 0x47, 0x8a, 0x4b, 0x01, 0xe1, 0x2e,
  45607. 0x8e, 0x6c, 0x63, 0x6f, 0x40, 0xca, 0x50, 0x3f,
  45608. 0x8c, 0x0b, 0x99, 0xe4, 0x72, 0x42, 0xb8, 0xb1,
  45609. 0xc2, 0x26, 0x48, 0xf1, 0x9c, 0x83, 0xc6, 0x37,
  45610. 0x2e, 0x5a, 0xae, 0x11, 0x09, 0xd9, 0xf3, 0xad,
  45611. 0x1f, 0x6f, 0xad, 0xad, 0x50, 0xe3, 0x78, 0x32,
  45612. 0xe6, 0xde, 0x8e, 0xaa, 0xbf, 0xd1, 0x00, 0x9f,
  45613. 0xb3, 0x02, 0x12, 0x19, 0xa2, 0x15, 0xec, 0x14,
  45614. 0x18, 0x5c, 0x0e, 0x26, 0xce, 0xf9, 0xae, 0xcc,
  45615. 0x7b, 0xb5, 0xd1, 0x26, 0xfc, 0x85, 0xfe, 0x14,
  45616. 0x93, 0xb6, 0x9d, 0x7d, 0x76, 0xe3, 0x35, 0x97,
  45617. 0x1e, 0xde, 0xc4
  45618. };
  45619. int derInLen = sizeof(derIn);
  45620. byte* derOut = NULL;
  45621. int derOutLen;
  45622. byte* p = derIn;
  45623. /* Check that params can be successfully decoded. */
  45624. ExpectNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&p, derInLen));
  45625. /* Check that params can be successfully encoded. */
  45626. ExpectIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  45627. /* Ensure that the encoded version matches the original. */
  45628. ExpectIntEQ(derInLen, derOutLen);
  45629. ExpectIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  45630. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  45631. DSA_free(dsa);
  45632. #endif
  45633. return EXPECT_RESULT();
  45634. }
  45635. static int test_wolfSSL_i2d_PrivateKey(void)
  45636. {
  45637. EXPECT_DECLS;
  45638. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && \
  45639. !defined(NO_ASN) && !defined(NO_PWDBASED)
  45640. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  45641. {
  45642. EVP_PKEY* pkey = NULL;
  45643. const unsigned char* server_key =
  45644. (const unsigned char*)server_key_der_2048;
  45645. unsigned char buf[FOURK_BUF];
  45646. unsigned char* pt = NULL;
  45647. int bufSz = 0;
  45648. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  45649. (long)sizeof_server_key_der_2048));
  45650. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  45651. pt = buf;
  45652. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  45653. ExpectIntNE((pt - buf), 0);
  45654. ExpectIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  45655. EVP_PKEY_free(pkey);
  45656. }
  45657. #endif
  45658. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45659. {
  45660. EVP_PKEY* pkey = NULL;
  45661. const unsigned char* client_key =
  45662. (const unsigned char*)ecc_clikey_der_256;
  45663. unsigned char buf[FOURK_BUF];
  45664. unsigned char* pt = NULL;
  45665. int bufSz = 0;
  45666. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  45667. (long)sizeof_ecc_clikey_der_256)));
  45668. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  45669. pt = buf;
  45670. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  45671. ExpectIntNE((pt - buf), 0);
  45672. ExpectIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  45673. EVP_PKEY_free(pkey);
  45674. }
  45675. #endif
  45676. #endif
  45677. return EXPECT_RESULT();
  45678. }
  45679. static int test_wolfSSL_OCSP_id_get0_info(void)
  45680. {
  45681. EXPECT_DECLS;
  45682. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && \
  45683. defined(HAVE_OCSP) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45684. X509* cert = NULL;
  45685. X509* issuer = NULL;
  45686. OCSP_CERTID* id = NULL;
  45687. OCSP_CERTID* id2 = NULL;
  45688. ASN1_STRING* name = NULL;
  45689. ASN1_OBJECT* pmd = NULL;
  45690. ASN1_STRING* keyHash = NULL;
  45691. ASN1_INTEGER* serial = NULL;
  45692. ASN1_INTEGER* x509Int = NULL;
  45693. ExpectNotNull(cert = wolfSSL_X509_load_certificate_file(svrCertFile,
  45694. SSL_FILETYPE_PEM));
  45695. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caCertFile,
  45696. SSL_FILETYPE_PEM));
  45697. ExpectNotNull(id = OCSP_cert_to_id(NULL, cert, issuer));
  45698. ExpectNotNull(id2 = OCSP_cert_to_id(NULL, cert, issuer));
  45699. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  45700. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  45701. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  45702. ExpectIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  45703. ExpectIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  45704. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  45705. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  45706. ExpectNotNull(serial);
  45707. /* compare serial number to one in cert, should be equal */
  45708. ExpectNotNull(x509Int = X509_get_serialNumber(cert));
  45709. ExpectIntEQ(x509Int->length, serial->length);
  45710. ExpectIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  45711. /* test OCSP_id_cmp */
  45712. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  45713. ExpectIntNE(OCSP_id_cmp(id, NULL), 0);
  45714. ExpectIntNE(OCSP_id_cmp(NULL, id2), 0);
  45715. ExpectIntEQ(OCSP_id_cmp(id, id2), 0);
  45716. if (id != NULL) {
  45717. id->issuerHash[0] = ~id->issuerHash[0];
  45718. }
  45719. ExpectIntNE(OCSP_id_cmp(id, id2), 0);
  45720. OCSP_CERTID_free(id);
  45721. OCSP_CERTID_free(id2);
  45722. X509_free(cert); /* free's x509Int */
  45723. X509_free(issuer);
  45724. #endif
  45725. return EXPECT_RESULT();
  45726. }
  45727. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  45728. {
  45729. EXPECT_DECLS;
  45730. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  45731. WOLFSSL_OCSP_CERTID certId;
  45732. byte* targetBuffer = NULL;
  45733. byte* p;
  45734. /* OCSP CertID bytes taken from PCAP */
  45735. byte rawCertId[] = {
  45736. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  45737. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  45738. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  45739. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  45740. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  45741. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  45742. 0xcf, 0xbc, 0x91
  45743. };
  45744. int ret = 0;
  45745. int i;
  45746. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  45747. certId.rawCertId = rawCertId;
  45748. certId.rawCertIdSize = sizeof(rawCertId);
  45749. ExpectNotNull(targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL,
  45750. DYNAMIC_TYPE_TMP_BUFFER));
  45751. p = targetBuffer;
  45752. /* Function returns the size of the encoded data. */
  45753. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &p), sizeof(rawCertId));
  45754. /* If target buffer is not null, function increments targetBuffer to point
  45755. * just past the end of the encoded data. */
  45756. ExpectPtrEq(p, (targetBuffer + sizeof(rawCertId)));
  45757. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  45758. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  45759. }
  45760. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45761. targetBuffer = NULL;
  45762. /* If target buffer is null, function allocates memory for a buffer and
  45763. * copies the encoded data into it. targetBuffer then points to the start of
  45764. * this newly allocate buffer. */
  45765. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer),
  45766. sizeof(rawCertId));
  45767. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  45768. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  45769. }
  45770. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  45771. #endif
  45772. return EXPECT_RESULT();
  45773. }
  45774. static int test_wolfSSL_d2i_OCSP_CERTID(void)
  45775. {
  45776. EXPECT_DECLS;
  45777. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  45778. WOLFSSL_OCSP_CERTID* certId;
  45779. WOLFSSL_OCSP_CERTID* certIdGood;
  45780. WOLFSSL_OCSP_CERTID* certIdBad;
  45781. const unsigned char* rawCertIdPtr;
  45782. const unsigned char rawCertId[] = {
  45783. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  45784. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  45785. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  45786. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  45787. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  45788. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  45789. 0xcf, 0xbc, 0x91
  45790. };
  45791. rawCertIdPtr = &rawCertId[0];
  45792. /* If the cert ID is NULL the function should allocate it and copy the
  45793. * data to it. */
  45794. certId = NULL;
  45795. ExpectNotNull(certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  45796. sizeof(rawCertId)));
  45797. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  45798. if (certId != NULL) {
  45799. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  45800. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  45801. }
  45802. /* If the cert ID is not NULL the function will just copy the data to it. */
  45803. ExpectNotNull(certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
  45804. DYNAMIC_TYPE_TMP_BUFFER));
  45805. ExpectNotNull(certId);
  45806. ExpectNotNull(XMEMSET(certId, 0, sizeof(*certId)));
  45807. /* Reset rawCertIdPtr since it was push forward in the previous call. */
  45808. rawCertIdPtr = &rawCertId[0];
  45809. ExpectNotNull(certIdGood = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  45810. sizeof(rawCertId)));
  45811. ExpectPtrEq(certIdGood, certId);
  45812. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  45813. if (certId != NULL) {
  45814. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  45815. XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45816. certId = NULL;
  45817. }
  45818. /* The below tests should fail when passed bad parameters. NULL should
  45819. * always be returned. */
  45820. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr,
  45821. sizeof(rawCertId)));
  45822. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL,
  45823. sizeof(rawCertId)));
  45824. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0));
  45825. #endif
  45826. return EXPECT_RESULT();
  45827. }
  45828. static int test_wolfSSL_OCSP_id_cmp(void)
  45829. {
  45830. EXPECT_DECLS;
  45831. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45832. OCSP_CERTID id1;
  45833. OCSP_CERTID id2;
  45834. XMEMSET(&id1, 0, sizeof(id1));
  45835. XMEMSET(&id2, 0, sizeof(id2));
  45836. ExpectIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  45837. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  45838. ExpectIntNE(OCSP_id_cmp(&id1, NULL), 0);
  45839. ExpectIntNE(OCSP_id_cmp(NULL, &id2), 0);
  45840. #endif
  45841. return EXPECT_RESULT();
  45842. }
  45843. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  45844. {
  45845. EXPECT_DECLS;
  45846. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45847. WOLFSSL_OCSP_SINGLERESP single;
  45848. const WOLFSSL_OCSP_CERTID* certId;
  45849. XMEMSET(&single, 0, sizeof(single));
  45850. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  45851. ExpectPtrEq(&single, certId);
  45852. ExpectNull(wolfSSL_OCSP_SINGLERESP_get0_id(NULL));
  45853. #endif
  45854. return EXPECT_RESULT();
  45855. }
  45856. static int test_wolfSSL_OCSP_single_get0_status(void)
  45857. {
  45858. EXPECT_DECLS;
  45859. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45860. WOLFSSL_OCSP_SINGLERESP single;
  45861. CertStatus certStatus;
  45862. WOLFSSL_ASN1_TIME* thisDate;
  45863. WOLFSSL_ASN1_TIME* nextDate;
  45864. int ret, i;
  45865. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45866. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  45867. /* Fill the date fields with some dummy data. */
  45868. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  45869. certStatus.thisDateParsed.data[i] = i;
  45870. certStatus.nextDateParsed.data[i] = i;
  45871. }
  45872. certStatus.status = CERT_GOOD;
  45873. single.status = &certStatus;
  45874. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  45875. &nextDate);
  45876. ExpectIntEQ(ret, CERT_GOOD);
  45877. ExpectPtrEq(thisDate, &certStatus.thisDateParsed);
  45878. ExpectPtrEq(nextDate, &certStatus.nextDateParsed);
  45879. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(NULL, NULL, NULL, NULL, NULL),
  45880. CERT_GOOD);
  45881. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, NULL,
  45882. NULL), CERT_GOOD);
  45883. #endif
  45884. return EXPECT_RESULT();
  45885. }
  45886. static int test_wolfSSL_OCSP_resp_count(void)
  45887. {
  45888. EXPECT_DECLS;
  45889. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45890. WOLFSSL_OCSP_BASICRESP basicResp;
  45891. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  45892. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  45893. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  45894. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45895. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45896. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 0);
  45897. basicResp.single = &singleRespOne;
  45898. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 1);
  45899. singleRespOne.next = &singleRespTwo;
  45900. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 2);
  45901. #endif
  45902. return EXPECT_RESULT();
  45903. }
  45904. static int test_wolfSSL_OCSP_resp_get0(void)
  45905. {
  45906. EXPECT_DECLS;
  45907. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  45908. WOLFSSL_OCSP_BASICRESP basicResp;
  45909. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  45910. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  45911. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  45912. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45913. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  45914. basicResp.single = &singleRespOne;
  45915. singleRespOne.next = &singleRespTwo;
  45916. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 0), &singleRespOne);
  45917. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 1), &singleRespTwo);
  45918. #endif
  45919. return EXPECT_RESULT();
  45920. }
  45921. static int test_wolfSSL_EVP_PKEY_derive(void)
  45922. {
  45923. EXPECT_DECLS;
  45924. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  45925. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  45926. EVP_PKEY_CTX *ctx = NULL;
  45927. unsigned char *skey = NULL;
  45928. size_t skeylen;
  45929. EVP_PKEY *pkey = NULL;
  45930. EVP_PKEY *peerkey = NULL;
  45931. const unsigned char* key;
  45932. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  45933. /* DH */
  45934. key = dh_key_der_2048;
  45935. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  45936. sizeof_dh_key_der_2048)));
  45937. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  45938. key = dh_key_der_2048;
  45939. ExpectNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  45940. sizeof_dh_key_der_2048)));
  45941. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  45942. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45943. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  45944. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  45945. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  45946. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  45947. DYNAMIC_TYPE_OPENSSL));
  45948. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  45949. EVP_PKEY_CTX_free(ctx);
  45950. ctx = NULL;
  45951. EVP_PKEY_free(peerkey);
  45952. peerkey = NULL;
  45953. EVP_PKEY_free(pkey);
  45954. pkey = NULL;
  45955. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  45956. skey = NULL;
  45957. #endif
  45958. #ifdef HAVE_ECC
  45959. /* ECDH */
  45960. key = ecc_clikey_der_256;
  45961. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  45962. sizeof_ecc_clikey_der_256)));
  45963. key = ecc_clikeypub_der_256;
  45964. ExpectNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  45965. sizeof_ecc_clikeypub_der_256)));
  45966. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  45967. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  45968. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  45969. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  45970. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  45971. DYNAMIC_TYPE_OPENSSL));
  45972. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  45973. EVP_PKEY_CTX_free(ctx);
  45974. EVP_PKEY_free(peerkey);
  45975. EVP_PKEY_free(pkey);
  45976. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  45977. #endif /* HAVE_ECC */
  45978. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  45979. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  45980. return EXPECT_RESULT();
  45981. }
  45982. static int test_wolfSSL_EVP_PBE_scrypt(void)
  45983. {
  45984. EXPECT_DECLS;
  45985. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  45986. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  45987. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  45988. int ret;
  45989. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  45990. int pwdlen = sizeof(pwd);
  45991. const byte salt[] = {'N','a','C','l'};
  45992. int saltlen = sizeof(salt);
  45993. byte key[80];
  45994. word64 numOvr32 = (word64)INT32_MAX + 1;
  45995. /* expected derived key for N:16, r:1, p:1 */
  45996. const byte expectedKey[] = {
  45997. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  45998. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  45999. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  46000. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  46001. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  46002. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  46003. 0xE7, 0xE9, 0xC0, 0x9A};
  46004. /* N r p mx key keylen */
  46005. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  46006. ExpectIntEQ(ret, 0); /* N must be greater than 1 */
  46007. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  46008. ExpectIntEQ(ret, 0); /* N must be power of 2 */
  46009. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  46010. ExpectIntEQ(ret, 0); /* r must be greater than 0 */
  46011. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  46012. ExpectIntEQ(ret, 0); /* p must be greater than 0 */
  46013. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  46014. ExpectIntEQ(ret, 0); /* keylen must be greater than 0 */
  46015. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  46016. ExpectIntEQ(ret, 0); /* r must be smaller than 9 */
  46017. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  46018. ExpectIntEQ(ret, 1); /* should succeed if key is NULL */
  46019. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  46020. ExpectIntEQ(ret, 1); /* should succeed */
  46021. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  46022. key, 64);
  46023. ExpectIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  46024. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  46025. key, 64);
  46026. ExpectIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  46027. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  46028. ExpectIntEQ(ret, 1); /* should succeed even if salt is NULL */
  46029. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  46030. ExpectIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  46031. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  46032. ExpectIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  46033. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  46034. ExpectIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  46035. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  46036. ExpectIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  46037. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  46038. ExpectIntEQ(ret, 1);
  46039. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  46040. ExpectIntEQ(ret, 0); /* derived key must be the same as expected-key */
  46041. #endif /* !NO_PWDBASED && !NO_SHA256 */
  46042. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  46043. return EXPECT_RESULT();
  46044. }
  46045. static int test_no_op_functions(void)
  46046. {
  46047. EXPECT_DECLS;
  46048. #if defined(OPENSSL_EXTRA)
  46049. /* this makes sure wolfSSL can compile and run these no-op functions */
  46050. SSL_load_error_strings();
  46051. ENGINE_load_builtin_engines();
  46052. OpenSSL_add_all_ciphers();
  46053. ExpectIntEQ(CRYPTO_malloc_init(), 0);
  46054. #endif
  46055. return EXPECT_RESULT();
  46056. }
  46057. static int test_wolfSSL_CRYPTO_memcmp(void)
  46058. {
  46059. EXPECT_DECLS;
  46060. #ifdef OPENSSL_EXTRA
  46061. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  46062. "implementation of TLS/SSL for embedded devices to the cloud.";
  46063. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  46064. "implementation of TLS/SSL for embedded devices to the cloud.";
  46065. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  46066. "implementation of TLS/SSL for embedded devices to the cloud!";
  46067. ExpectIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  46068. ExpectIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  46069. #endif
  46070. return EXPECT_RESULT();
  46071. }
  46072. /*----------------------------------------------------------------------------*
  46073. | wolfCrypt ASN
  46074. *----------------------------------------------------------------------------*/
  46075. static int test_wc_CreateEncryptedPKCS8Key(void)
  46076. {
  46077. EXPECT_DECLS;
  46078. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  46079. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  46080. WC_RNG rng;
  46081. byte* encKey = NULL;
  46082. word32 encKeySz = 0;
  46083. word32 decKeySz = 0;
  46084. const char password[] = "Lorem ipsum dolor sit amet";
  46085. word32 passwordSz = (word32)XSTRLEN(password);
  46086. word32 tradIdx = 0;
  46087. XMEMSET(&rng, 0, sizeof(WC_RNG));
  46088. ExpectIntEQ(wc_InitRng(&rng), 0);
  46089. PRIVATE_KEY_UNLOCK();
  46090. /* Call with NULL for out buffer to get necessary length. */
  46091. ExpectIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  46092. sizeof_server_key_der_2048, NULL, &encKeySz, password, passwordSz,
  46093. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  46094. LENGTH_ONLY_E);
  46095. ExpectNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  46096. DYNAMIC_TYPE_TMP_BUFFER));
  46097. /* Call with the allocated out buffer. */
  46098. ExpectIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  46099. sizeof_server_key_der_2048, encKey, &encKeySz, password, passwordSz,
  46100. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  46101. 0);
  46102. /* Decrypt the encrypted PKCS8 key we just made. */
  46103. ExpectIntGT((decKeySz = wc_DecryptPKCS8Key(encKey, encKeySz, password,
  46104. passwordSz)), 0);
  46105. /* encKey now holds the decrypted key (decrypted in place). */
  46106. ExpectIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  46107. /* Check that the decrypted key matches the key prior to encryption. */
  46108. ExpectIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  46109. sizeof_server_key_der_2048), 0);
  46110. PRIVATE_KEY_LOCK();
  46111. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46112. wc_FreeRng(&rng);
  46113. #endif
  46114. return EXPECT_RESULT();
  46115. }
  46116. static int test_wc_GetPkcs8TraditionalOffset(void)
  46117. {
  46118. EXPECT_DECLS;
  46119. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  46120. int length;
  46121. int derSz = 0;
  46122. word32 inOutIdx;
  46123. const char* path = "./certs/server-keyPkcs8.der";
  46124. XFILE file = XBADFILE;
  46125. byte der[2048];
  46126. ExpectTrue((file = XFOPEN(path, "rb")) != XBADFILE);
  46127. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), file), 0);
  46128. if (file != XBADFILE)
  46129. XFCLOSE(file);
  46130. /* valid case */
  46131. inOutIdx = 0;
  46132. ExpectIntGT(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  46133. 0);
  46134. /* inOutIdx > sz */
  46135. inOutIdx = 4000;
  46136. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  46137. BAD_FUNC_ARG);
  46138. /* null input */
  46139. inOutIdx = 0;
  46140. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0),
  46141. BAD_FUNC_ARG);
  46142. /* invalid input, fill buffer with 1's */
  46143. XMEMSET(der, 1, sizeof(der));
  46144. inOutIdx = 0;
  46145. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, derSz),
  46146. ASN_PARSE_E);
  46147. #endif /* NO_ASN */
  46148. return EXPECT_RESULT();
  46149. }
  46150. static int test_wc_SetSubjectRaw(void)
  46151. {
  46152. EXPECT_DECLS;
  46153. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46154. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  46155. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  46156. WOLFSSL_X509* x509 = NULL;
  46157. int peerCertSz;
  46158. const byte* peerCertBuf = NULL;
  46159. Cert forgedCert;
  46160. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  46161. WOLFSSL_FILETYPE_ASN1));
  46162. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  46163. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  46164. ExpectIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  46165. wolfSSL_FreeX509(x509);
  46166. #endif
  46167. return EXPECT_RESULT();
  46168. }
  46169. static int test_wc_GetSubjectRaw(void)
  46170. {
  46171. EXPECT_DECLS;
  46172. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46173. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  46174. Cert cert;
  46175. byte *subjectRaw;
  46176. ExpectIntEQ(0, wc_InitCert(&cert));
  46177. ExpectIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  46178. #endif
  46179. return EXPECT_RESULT();
  46180. }
  46181. static int test_wc_SetIssuerRaw(void)
  46182. {
  46183. EXPECT_DECLS;
  46184. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46185. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  46186. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  46187. WOLFSSL_X509* x509 = NULL;
  46188. int peerCertSz;
  46189. const byte* peerCertBuf;
  46190. Cert forgedCert;
  46191. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  46192. WOLFSSL_FILETYPE_ASN1));
  46193. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  46194. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  46195. ExpectIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  46196. wolfSSL_FreeX509(x509);
  46197. #endif
  46198. return EXPECT_RESULT();
  46199. }
  46200. static int test_wc_SetIssueBuffer(void)
  46201. {
  46202. EXPECT_DECLS;
  46203. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46204. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  46205. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  46206. WOLFSSL_X509* x509 = NULL;
  46207. int peerCertSz;
  46208. const byte* peerCertBuf;
  46209. Cert forgedCert;
  46210. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  46211. WOLFSSL_FILETYPE_ASN1));
  46212. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  46213. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  46214. ExpectIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  46215. wolfSSL_FreeX509(x509);
  46216. #endif
  46217. return EXPECT_RESULT();
  46218. }
  46219. /*
  46220. * Testing wc_SetSubjectKeyId
  46221. */
  46222. static int test_wc_SetSubjectKeyId(void)
  46223. {
  46224. EXPECT_DECLS;
  46225. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46226. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  46227. Cert cert;
  46228. const char* file = "certs/ecc-client-keyPub.pem";
  46229. ExpectIntEQ(0, wc_InitCert(&cert));
  46230. ExpectIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  46231. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  46232. ExpectIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  46233. #endif
  46234. return EXPECT_RESULT();
  46235. } /* END test_wc_SetSubjectKeyId */
  46236. /*
  46237. * Testing wc_SetSubject
  46238. */
  46239. static int test_wc_SetSubject(void)
  46240. {
  46241. EXPECT_DECLS;
  46242. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46243. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  46244. Cert cert;
  46245. const char* file = "./certs/ca-ecc-cert.pem";
  46246. ExpectIntEQ(0, wc_InitCert(&cert));
  46247. ExpectIntEQ(0, wc_SetSubject(&cert, file));
  46248. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  46249. ExpectIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  46250. #endif
  46251. return EXPECT_RESULT();
  46252. } /* END test_wc_SetSubject */
  46253. static int test_CheckCertSignature(void)
  46254. {
  46255. EXPECT_DECLS;
  46256. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  46257. WOLFSSL_CERT_MANAGER* cm = NULL;
  46258. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  46259. XFILE fp = XBADFILE;
  46260. byte cert[4096];
  46261. int certSz;
  46262. #endif
  46263. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  46264. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  46265. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  46266. #ifndef NO_RSA
  46267. #ifdef USE_CERT_BUFFERS_1024
  46268. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  46269. sizeof_server_cert_der_1024, NULL, cm));
  46270. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  46271. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  46272. WOLFSSL_FILETYPE_ASN1));
  46273. ExpectIntEQ(0, CheckCertSignature(server_cert_der_1024,
  46274. sizeof_server_cert_der_1024, NULL, cm));
  46275. #elif defined(USE_CERT_BUFFERS_2048)
  46276. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  46277. sizeof_server_cert_der_2048, NULL, cm));
  46278. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  46279. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  46280. WOLFSSL_FILETYPE_ASN1));
  46281. ExpectIntEQ(0, CheckCertSignature(server_cert_der_2048,
  46282. sizeof_server_cert_der_2048, NULL, cm));
  46283. #endif
  46284. #endif
  46285. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  46286. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  46287. sizeof_serv_ecc_der_256, NULL, cm));
  46288. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  46289. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  46290. WOLFSSL_FILETYPE_ASN1));
  46291. ExpectIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  46292. NULL, cm));
  46293. #endif
  46294. #if !defined(NO_FILESYSTEM)
  46295. wolfSSL_CertManagerFree(cm);
  46296. cm = NULL;
  46297. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  46298. #ifndef NO_RSA
  46299. ExpectTrue((fp = XFOPEN("./certs/server-cert.der", "rb")) != XBADFILE);
  46300. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  46301. if (fp != XBADFILE) {
  46302. XFCLOSE(fp);
  46303. fp = XBADFILE;
  46304. }
  46305. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  46306. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  46307. "./certs/ca-cert.pem", NULL));
  46308. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  46309. #endif
  46310. #ifdef HAVE_ECC
  46311. ExpectTrue((fp = XFOPEN("./certs/server-ecc.der", "rb")) != XBADFILE);
  46312. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  46313. if (fp != XBADFILE) {
  46314. XFCLOSE(fp);
  46315. fp = XBADFILE;
  46316. }
  46317. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  46318. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  46319. "./certs/ca-ecc-cert.pem", NULL));
  46320. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  46321. #endif
  46322. #endif
  46323. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  46324. (void)fp;
  46325. (void)cert;
  46326. (void)certSz;
  46327. #endif
  46328. wolfSSL_CertManagerFree(cm);
  46329. #endif
  46330. return EXPECT_RESULT();
  46331. }
  46332. static int test_wc_ParseCert(void)
  46333. {
  46334. EXPECT_DECLS;
  46335. #if !defined(NO_CERTS) && !defined(NO_RSA)
  46336. DecodedCert decodedCert;
  46337. const byte* rawCert = client_cert_der_2048;
  46338. const int rawCertSize = sizeof_client_cert_der_2048;
  46339. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  46340. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  46341. #ifndef IGNORE_NAME_CONSTRAINTS
  46342. /* check that the subjects emailAddress was not put in the alt name list */
  46343. ExpectNotNull(decodedCert.subjectEmail);
  46344. ExpectNull(decodedCert.altEmailNames);
  46345. #endif
  46346. wc_FreeDecodedCert(&decodedCert);
  46347. #endif
  46348. return EXPECT_RESULT();
  46349. }
  46350. /* Test wc_ParseCert decoding of various encodings and scenarios ensuring that
  46351. * the API safely errors out on badly-formed ASN input.
  46352. * NOTE: Test not compatible with released FIPS implementations!
  46353. */
  46354. static int test_wc_ParseCert_Error(void)
  46355. {
  46356. EXPECT_DECLS;
  46357. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(HAVE_SELFTEST) && \
  46358. (!defined(HAVE_FIPS) || \
  46359. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  46360. DecodedCert decodedCert;
  46361. int i;
  46362. /* Certificate data */
  46363. const byte c0[] = { 0x30, 0x04, 0x30, 0x02, 0x02, 0x80, 0x00, 0x00};
  46364. const byte c1[] = { 0x30, 0x04, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  46365. const byte c2[] = { 0x30, 0x06, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  46366. const byte c3[] = { 0x30, 0x07, 0x30, 0x05, 0x02, 0x80, 0x10, 0x00, 0x00};
  46367. const byte c4[] = { 0x02, 0x80, 0x10, 0x00, 0x00};
  46368. /* Test data */
  46369. const struct testStruct {
  46370. const byte* c;
  46371. const int cSz;
  46372. const int expRet;
  46373. } t[] = {
  46374. {c0, sizeof(c0), ASN_PARSE_E}, /* Invalid bit-string length */
  46375. {c1, sizeof(c1), ASN_PARSE_E}, /* Invalid bit-string length */
  46376. {c2, sizeof(c2), ASN_PARSE_E}, /* Invalid integer length (zero) */
  46377. {c3, sizeof(c3), ASN_PARSE_E}, /* Valid INTEGER, but buffer too short */
  46378. {c4, sizeof(c4), ASN_PARSE_E}, /* Valid INTEGER, but not in bit-string */
  46379. };
  46380. const int tSz = (int)(sizeof(t) / sizeof(struct testStruct));
  46381. for (i = 0; i < tSz; i++) {
  46382. WOLFSSL_MSG_EX("i == %d", i);
  46383. wc_InitDecodedCert(&decodedCert, t[i].c, t[i].cSz, NULL);
  46384. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), t[i].expRet);
  46385. wc_FreeDecodedCert(&decodedCert);
  46386. }
  46387. #endif
  46388. return EXPECT_RESULT();
  46389. }
  46390. static int test_MakeCertWithPathLen(void)
  46391. {
  46392. EXPECT_DECLS;
  46393. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  46394. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  46395. const byte expectedPathLen = 7;
  46396. Cert cert;
  46397. DecodedCert decodedCert;
  46398. byte der[FOURK_BUF];
  46399. int derSize = 0;
  46400. WC_RNG rng;
  46401. ecc_key key;
  46402. int ret;
  46403. XMEMSET(&rng, 0, sizeof(WC_RNG));
  46404. XMEMSET(&key, 0, sizeof(ecc_key));
  46405. XMEMSET(&cert, 0, sizeof(Cert));
  46406. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  46407. ExpectIntEQ(wc_InitRng(&rng), 0);
  46408. ExpectIntEQ(wc_ecc_init(&key), 0);
  46409. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  46410. ExpectIntEQ(wc_InitCert(&cert), 0);
  46411. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  46412. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  46413. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  46414. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  46415. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  46416. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  46417. CTC_NAME_SIZE);
  46418. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  46419. CTC_NAME_SIZE);
  46420. cert.selfSigned = 1;
  46421. cert.isCA = 1;
  46422. cert.pathLen = expectedPathLen;
  46423. cert.pathLenSet = 1;
  46424. cert.sigType = CTC_SHA256wECDSA;
  46425. #ifdef WOLFSSL_CERT_EXT
  46426. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  46427. #endif
  46428. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  46429. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  46430. FOURK_BUF, NULL, &key, &rng), 0);
  46431. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  46432. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  46433. ExpectIntEQ(decodedCert.pathLength, expectedPathLen);
  46434. wc_FreeDecodedCert(&decodedCert);
  46435. ret = wc_ecc_free(&key);
  46436. ExpectIntEQ(ret, 0);
  46437. ret = wc_FreeRng(&rng);
  46438. ExpectIntEQ(ret, 0);
  46439. #endif
  46440. return EXPECT_RESULT();
  46441. }
  46442. static int test_MakeCertWithCaFalse(void)
  46443. {
  46444. EXPECT_DECLS;
  46445. #if defined(WOLFSSL_ALLOW_ENCODING_CA_FALSE) && defined(WOLFSSL_CERT_REQ) && \
  46446. !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  46447. const byte expectedIsCa = 0;
  46448. Cert cert;
  46449. DecodedCert decodedCert;
  46450. byte der[FOURK_BUF];
  46451. int derSize = 0;
  46452. WC_RNG rng;
  46453. ecc_key key;
  46454. int ret;
  46455. XMEMSET(&rng, 0, sizeof(WC_RNG));
  46456. XMEMSET(&key, 0, sizeof(ecc_key));
  46457. XMEMSET(&cert, 0, sizeof(Cert));
  46458. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  46459. ExpectIntEQ(wc_InitRng(&rng), 0);
  46460. ExpectIntEQ(wc_ecc_init(&key), 0);
  46461. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  46462. ExpectIntEQ(wc_InitCert(&cert), 0);
  46463. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  46464. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  46465. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  46466. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  46467. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  46468. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  46469. CTC_NAME_SIZE);
  46470. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  46471. CTC_NAME_SIZE);
  46472. cert.selfSigned = 1;
  46473. cert.isCA = expectedIsCa;
  46474. cert.isCaSet = 1;
  46475. cert.sigType = CTC_SHA256wECDSA;
  46476. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  46477. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  46478. FOURK_BUF, NULL, &key, &rng), 0);
  46479. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  46480. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  46481. ExpectIntEQ(decodedCert.isCA, expectedIsCa);
  46482. wc_FreeDecodedCert(&decodedCert);
  46483. ret = wc_ecc_free(&key);
  46484. ExpectIntEQ(ret, 0);
  46485. ret = wc_FreeRng(&rng);
  46486. ExpectIntEQ(ret, 0);
  46487. #endif
  46488. return EXPECT_RESULT();
  46489. }
  46490. /*----------------------------------------------------------------------------*
  46491. | wolfCrypt ECC
  46492. *----------------------------------------------------------------------------*/
  46493. static int test_wc_ecc_get_curve_size_from_name(void)
  46494. {
  46495. EXPECT_DECLS;
  46496. #ifdef HAVE_ECC
  46497. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46498. ExpectIntEQ(wc_ecc_get_curve_size_from_name("SECP256R1"), 32);
  46499. #endif
  46500. /* invalid case */
  46501. ExpectIntEQ(wc_ecc_get_curve_size_from_name("BADCURVE"), -1);
  46502. /* NULL input */
  46503. ExpectIntEQ(wc_ecc_get_curve_size_from_name(NULL), BAD_FUNC_ARG);
  46504. #endif /* HAVE_ECC */
  46505. return EXPECT_RESULT();
  46506. }
  46507. static int test_wc_ecc_get_curve_id_from_name(void)
  46508. {
  46509. EXPECT_DECLS;
  46510. #ifdef HAVE_ECC
  46511. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46512. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"),
  46513. ECC_SECP256R1);
  46514. #endif
  46515. /* invalid case */
  46516. ExpectIntEQ(wc_ecc_get_curve_id_from_name("BADCURVE"), -1);
  46517. /* NULL input */
  46518. ExpectIntEQ(wc_ecc_get_curve_id_from_name(NULL), BAD_FUNC_ARG);
  46519. #endif /* HAVE_ECC */
  46520. return EXPECT_RESULT();
  46521. }
  46522. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  46523. !defined(HAVE_SELFTEST) && \
  46524. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  46525. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  46526. {
  46527. EXPECT_DECLS;
  46528. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46529. ecc_key* key;
  46530. const ecc_set_type* params = NULL;
  46531. int ret;
  46532. #endif
  46533. WOLFSSL_EC_KEY *ecKey = NULL;
  46534. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46535. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"), ECC_SECP256R1);
  46536. ExpectNotNull(ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  46537. if (EXPECT_SUCCESS()) {
  46538. ret = EC_KEY_generate_key(ecKey);
  46539. } else
  46540. ret = 0;
  46541. if (ret == 1) {
  46542. /* normal test */
  46543. key = (ecc_key*)ecKey->internal;
  46544. if (key != NULL) {
  46545. params = key->dp;
  46546. }
  46547. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(params),
  46548. ECC_SECP256R1);
  46549. }
  46550. #endif
  46551. /* invalid case, NULL input*/
  46552. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(NULL), BAD_FUNC_ARG);
  46553. wolfSSL_EC_KEY_free(ecKey);
  46554. return EXPECT_RESULT();
  46555. }
  46556. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  46557. static int test_wc_ecc_get_curve_id_from_params(void)
  46558. {
  46559. EXPECT_DECLS;
  46560. #ifdef HAVE_ECC
  46561. const byte prime[] =
  46562. {
  46563. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  46564. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46565. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  46566. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  46567. };
  46568. const byte primeInvalid[] =
  46569. {
  46570. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  46571. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46572. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  46573. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  46574. };
  46575. const byte Af[] =
  46576. {
  46577. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  46578. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46579. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  46580. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  46581. };
  46582. const byte Bf[] =
  46583. {
  46584. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  46585. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  46586. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  46587. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  46588. };
  46589. const byte order[] =
  46590. {
  46591. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  46592. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  46593. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  46594. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  46595. };
  46596. const byte Gx[] =
  46597. {
  46598. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  46599. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  46600. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  46601. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  46602. };
  46603. const byte Gy[] =
  46604. {
  46605. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  46606. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  46607. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  46608. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  46609. };
  46610. int cofactor = 1;
  46611. int fieldSize = 256;
  46612. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46613. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  46614. prime, sizeof(prime), Af, sizeof(Af), Bf, sizeof(Bf),
  46615. order, sizeof(order), Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor),
  46616. ECC_SECP256R1);
  46617. #endif
  46618. /* invalid case, fieldSize = 0 */
  46619. ExpectIntEQ(wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  46620. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  46621. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  46622. /* invalid case, NULL prime */
  46623. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  46624. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  46625. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), BAD_FUNC_ARG);
  46626. /* invalid case, invalid prime */
  46627. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  46628. primeInvalid, sizeof(primeInvalid),
  46629. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  46630. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  46631. #endif
  46632. return EXPECT_RESULT();
  46633. }
  46634. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  46635. {
  46636. EXPECT_DECLS;
  46637. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  46638. WOLFSSL_RSA* rsa = NULL;
  46639. WOLFSSL_EVP_PKEY* pkey = NULL;
  46640. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  46641. const char* in = "What is easy to do is easy not to do.";
  46642. size_t inlen = XSTRLEN(in);
  46643. size_t outEncLen = 0;
  46644. byte* outEnc = NULL;
  46645. byte* outDec = NULL;
  46646. size_t outDecLen = 0;
  46647. size_t rsaKeySz = 2048/8; /* Bytes */
  46648. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  46649. byte* inTmp = NULL;
  46650. byte* outEncTmp = NULL;
  46651. byte* outDecTmp = NULL;
  46652. #endif
  46653. ExpectNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  46654. DYNAMIC_TYPE_TMP_BUFFER));
  46655. if (outEnc != NULL) {
  46656. XMEMSET(outEnc, 0, rsaKeySz);
  46657. }
  46658. ExpectNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  46659. DYNAMIC_TYPE_TMP_BUFFER));
  46660. if (outDec != NULL) {
  46661. XMEMSET(outDec, 0, rsaKeySz);
  46662. }
  46663. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46664. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  46665. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  46666. if (EXPECT_FAIL()) {
  46667. RSA_free(rsa);
  46668. }
  46669. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  46670. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  46671. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  46672. WOLFSSL_SUCCESS);
  46673. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  46674. since ctx uses it.*/
  46675. ExpectIntEQ(pkey->ref.count, 2);
  46676. EVP_PKEY_free(pkey);
  46677. ExpectIntEQ(pkey->ref.count, 1);
  46678. /* Encrypt data */
  46679. /* Check that we can get the required output buffer length by passing in a
  46680. * NULL output buffer. */
  46681. ExpectIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  46682. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  46683. ExpectIntEQ(rsaKeySz, outEncLen);
  46684. /* Now do the actual encryption. */
  46685. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  46686. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  46687. /* Decrypt data */
  46688. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  46689. /* Check that we can get the required output buffer length by passing in a
  46690. * NULL output buffer. */
  46691. ExpectIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  46692. WOLFSSL_SUCCESS);
  46693. ExpectIntEQ(rsaKeySz, outDecLen);
  46694. /* Now do the actual decryption. */
  46695. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  46696. WOLFSSL_SUCCESS);
  46697. ExpectIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  46698. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  46699. /* The input length must be the same size as the RSA key.*/
  46700. ExpectNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  46701. DYNAMIC_TYPE_TMP_BUFFER));
  46702. if (inTmp != NULL) {
  46703. XMEMSET(inTmp, 9, rsaKeySz);
  46704. }
  46705. ExpectNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  46706. DYNAMIC_TYPE_TMP_BUFFER));
  46707. if (outEncTmp != NULL) {
  46708. XMEMSET(outEncTmp, 0, rsaKeySz);
  46709. }
  46710. ExpectNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  46711. DYNAMIC_TYPE_TMP_BUFFER));
  46712. if (outDecTmp != NULL) {
  46713. XMEMSET(outDecTmp, 0, rsaKeySz);
  46714. }
  46715. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  46716. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  46717. WOLFSSL_SUCCESS);
  46718. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  46719. WOLFSSL_SUCCESS);
  46720. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  46721. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp,
  46722. outEncLen), WOLFSSL_SUCCESS);
  46723. ExpectIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  46724. #endif
  46725. EVP_PKEY_CTX_free(ctx);
  46726. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46727. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46728. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  46729. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46730. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46731. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46732. #endif
  46733. #endif
  46734. return EXPECT_RESULT();
  46735. }
  46736. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46737. !defined(HAVE_SELFTEST)
  46738. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46739. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46740. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46741. #endif
  46742. #endif
  46743. #endif
  46744. #if defined(OPENSSL_EXTRA)
  46745. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46746. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46747. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46748. #endif
  46749. #endif
  46750. #endif
  46751. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46752. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46753. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46754. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46755. #endif
  46756. #endif
  46757. #endif
  46758. #ifdef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46759. static int test_wolfSSL_EVP_PKEY_sign_verify(int keyType)
  46760. {
  46761. EXPECT_DECLS;
  46762. #if defined(OPENSSL_EXTRA)
  46763. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46764. !defined(HAVE_SELFTEST)
  46765. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46766. WOLFSSL_RSA* rsa = NULL;
  46767. #endif
  46768. #endif
  46769. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46770. WOLFSSL_DSA* dsa = NULL;
  46771. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  46772. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46773. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46774. WOLFSSL_EC_KEY* ecKey = NULL;
  46775. #endif
  46776. #endif
  46777. WOLFSSL_EVP_PKEY* pkey = NULL;
  46778. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  46779. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  46780. const char* in = "What is easy to do is easy not to do.";
  46781. size_t inlen = XSTRLEN(in);
  46782. byte hash[SHA256_DIGEST_LENGTH] = {0};
  46783. byte zero[SHA256_DIGEST_LENGTH] = {0};
  46784. SHA256_CTX c;
  46785. byte* sig = NULL;
  46786. byte* sigVerify = NULL;
  46787. size_t siglen;
  46788. size_t siglenOnlyLen;
  46789. size_t keySz = 2048/8; /* Bytes */
  46790. ExpectNotNull(sig =
  46791. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  46792. ExpectNotNull(sigVerify =
  46793. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  46794. siglen = keySz;
  46795. ExpectNotNull(XMEMSET(sig, 0, keySz));
  46796. ExpectNotNull(XMEMSET(sigVerify, 0, keySz));
  46797. /* Generate hash */
  46798. SHA256_Init(&c);
  46799. SHA256_Update(&c, in, inlen);
  46800. SHA256_Final(hash, &c);
  46801. #ifdef WOLFSSL_SMALL_STACK_CACHE
  46802. /* workaround for small stack cache case */
  46803. wc_Sha256Free((wc_Sha256*)&c);
  46804. #endif
  46805. /* Generate key */
  46806. ExpectNotNull(pkey = EVP_PKEY_new());
  46807. switch (keyType) {
  46808. case EVP_PKEY_RSA:
  46809. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46810. !defined(HAVE_SELFTEST)
  46811. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46812. {
  46813. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46814. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  46815. }
  46816. #endif
  46817. #endif
  46818. break;
  46819. case EVP_PKEY_DSA:
  46820. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46821. ExpectNotNull(dsa = DSA_new());
  46822. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  46823. NULL, 0, NULL, NULL, NULL), 1);
  46824. ExpectIntEQ(DSA_generate_key(dsa), 1);
  46825. ExpectIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  46826. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  46827. break;
  46828. case EVP_PKEY_EC:
  46829. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46830. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46831. {
  46832. ExpectNotNull(ecKey = EC_KEY_new());
  46833. ExpectIntEQ(EC_KEY_generate_key(ecKey), 1);
  46834. ExpectIntEQ(
  46835. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  46836. if (EXPECT_FAIL()) {
  46837. EC_KEY_free(ecKey);
  46838. }
  46839. }
  46840. #endif
  46841. #endif
  46842. break;
  46843. }
  46844. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  46845. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46846. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46847. !defined(HAVE_SELFTEST)
  46848. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46849. if (keyType == EVP_PKEY_RSA)
  46850. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  46851. WOLFSSL_SUCCESS);
  46852. #endif
  46853. #endif
  46854. /* Check returning only length */
  46855. ExpectIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  46856. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  46857. ExpectIntGT(siglenOnlyLen, 0);
  46858. /* Sign data */
  46859. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  46860. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  46861. ExpectIntGE(siglenOnlyLen, siglen);
  46862. /* Verify signature */
  46863. ExpectNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  46864. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  46865. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46866. !defined(HAVE_SELFTEST)
  46867. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46868. if (keyType == EVP_PKEY_RSA)
  46869. ExpectIntEQ(
  46870. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  46871. WOLFSSL_SUCCESS);
  46872. #endif
  46873. #endif
  46874. ExpectIntEQ(EVP_PKEY_verify(
  46875. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  46876. WOLFSSL_SUCCESS);
  46877. ExpectIntEQ(EVP_PKEY_verify(
  46878. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  46879. WOLFSSL_FAILURE);
  46880. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46881. !defined(HAVE_SELFTEST)
  46882. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46883. if (keyType == EVP_PKEY_RSA) {
  46884. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  46885. /* Try RSA sign/verify with no padding. */
  46886. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46887. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  46888. WOLFSSL_SUCCESS);
  46889. ExpectIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  46890. siglen), WOLFSSL_SUCCESS);
  46891. ExpectIntGE(siglenOnlyLen, siglen);
  46892. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  46893. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  46894. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  46895. ExpectIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  46896. siglen), WOLFSSL_SUCCESS);
  46897. #endif
  46898. /* Wrong padding schemes. */
  46899. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46900. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  46901. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  46902. ExpectIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  46903. siglen), WOLFSSL_SUCCESS);
  46904. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  46905. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  46906. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  46907. ExpectIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  46908. siglen), WOLFSSL_SUCCESS);
  46909. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  46910. WOLFSSL_SUCCESS);
  46911. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  46912. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  46913. }
  46914. #endif
  46915. #endif
  46916. /* error cases */
  46917. siglen = keySz; /* Reset because sig size may vary slightly */
  46918. ExpectIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  46919. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  46920. ExpectIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  46921. WOLFSSL_SUCCESS);
  46922. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  46923. WOLFSSL_SUCCESS);
  46924. EVP_PKEY_free(pkey);
  46925. pkey = NULL;
  46926. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46927. DSA_free(dsa);
  46928. dsa = NULL;
  46929. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  46930. EVP_PKEY_CTX_free(ctx_verify);
  46931. ctx_verify = NULL;
  46932. EVP_PKEY_CTX_free(ctx);
  46933. ctx = NULL;
  46934. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46935. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46936. #endif /* OPENSSL_EXTRA */
  46937. return EXPECT_RESULT();
  46938. }
  46939. #endif
  46940. static int test_wolfSSL_EVP_PKEY_sign_verify_rsa(void)
  46941. {
  46942. EXPECT_DECLS;
  46943. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46944. !defined(HAVE_SELFTEST)
  46945. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46946. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_RSA), TEST_SUCCESS);
  46947. #endif
  46948. #endif
  46949. return EXPECT_RESULT();
  46950. }
  46951. static int test_wolfSSL_EVP_PKEY_sign_verify_dsa(void)
  46952. {
  46953. EXPECT_DECLS;
  46954. #if defined(OPENSSL_EXTRA)
  46955. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46956. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_DSA), TEST_SUCCESS);
  46957. #endif
  46958. #endif
  46959. return EXPECT_RESULT();
  46960. }
  46961. static int test_wolfSSL_EVP_PKEY_sign_verify_ec(void)
  46962. {
  46963. EXPECT_DECLS;
  46964. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46965. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46966. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_EC), TEST_SUCCESS);
  46967. #endif
  46968. #endif
  46969. return EXPECT_RESULT();
  46970. }
  46971. static int test_EVP_PKEY_rsa(void)
  46972. {
  46973. EXPECT_DECLS;
  46974. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  46975. WOLFSSL_RSA* rsa = NULL;
  46976. WOLFSSL_EVP_PKEY* pkey = NULL;
  46977. ExpectNotNull(rsa = wolfSSL_RSA_new());
  46978. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  46979. ExpectIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  46980. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  46981. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  46982. if (EXPECT_FAIL()) {
  46983. wolfSSL_RSA_free(rsa);
  46984. }
  46985. ExpectPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  46986. wolfSSL_EVP_PKEY_free(pkey);
  46987. #endif
  46988. return EXPECT_RESULT();
  46989. }
  46990. static int test_EVP_PKEY_ec(void)
  46991. {
  46992. EXPECT_DECLS;
  46993. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46994. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46995. WOLFSSL_EC_KEY* ecKey = NULL;
  46996. WOLFSSL_EVP_PKEY* pkey = NULL;
  46997. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  46998. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  46999. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  47000. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  47001. /* Should fail since ecKey is empty */
  47002. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  47003. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  47004. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  47005. if (EXPECT_FAIL()) {
  47006. wolfSSL_EC_KEY_free(ecKey);
  47007. }
  47008. wolfSSL_EVP_PKEY_free(pkey);
  47009. #endif
  47010. #endif
  47011. return EXPECT_RESULT();
  47012. }
  47013. static int test_EVP_PKEY_cmp(void)
  47014. {
  47015. EXPECT_DECLS;
  47016. #if defined(OPENSSL_EXTRA)
  47017. EVP_PKEY *a = NULL;
  47018. EVP_PKEY *b = NULL;
  47019. const unsigned char *in;
  47020. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  47021. in = client_key_der_2048;
  47022. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  47023. &in, (long)sizeof_client_key_der_2048));
  47024. in = client_key_der_2048;
  47025. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  47026. &in, (long)sizeof_client_key_der_2048));
  47027. /* Test success case RSA */
  47028. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  47029. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  47030. #else
  47031. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  47032. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  47033. EVP_PKEY_free(b);
  47034. b = NULL;
  47035. EVP_PKEY_free(a);
  47036. a = NULL;
  47037. #endif
  47038. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  47039. in = ecc_clikey_der_256;
  47040. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  47041. &in, (long)sizeof_ecc_clikey_der_256));
  47042. in = ecc_clikey_der_256;
  47043. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  47044. &in, (long)sizeof_ecc_clikey_der_256));
  47045. /* Test success case ECC */
  47046. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  47047. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  47048. #else
  47049. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  47050. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  47051. EVP_PKEY_free(b);
  47052. b = NULL;
  47053. EVP_PKEY_free(a);
  47054. a = NULL;
  47055. #endif
  47056. /* Test failure cases */
  47057. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  47058. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  47059. in = client_key_der_2048;
  47060. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  47061. &in, (long)sizeof_client_key_der_2048));
  47062. in = ecc_clikey_der_256;
  47063. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  47064. &in, (long)sizeof_ecc_clikey_der_256));
  47065. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  47066. ExpectIntEQ(EVP_PKEY_cmp(a, b), -1);
  47067. #else
  47068. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  47069. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  47070. EVP_PKEY_free(b);
  47071. b = NULL;
  47072. EVP_PKEY_free(a);
  47073. a = NULL;
  47074. #endif
  47075. /* invalid or empty failure cases */
  47076. a = EVP_PKEY_new();
  47077. b = EVP_PKEY_new();
  47078. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  47079. ExpectIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  47080. ExpectIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  47081. ExpectIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  47082. #ifdef NO_RSA
  47083. /* Type check will fail since RSA is the default EVP key type */
  47084. ExpectIntEQ(EVP_PKEY_cmp(a, b), -2);
  47085. #else
  47086. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  47087. #endif
  47088. #else
  47089. ExpectIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  47090. ExpectIntNE(EVP_PKEY_cmp(a, NULL), 0);
  47091. ExpectIntNE(EVP_PKEY_cmp(NULL, b), 0);
  47092. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  47093. #endif
  47094. EVP_PKEY_free(b);
  47095. EVP_PKEY_free(a);
  47096. (void)in;
  47097. #endif
  47098. return EXPECT_RESULT();
  47099. }
  47100. static int test_ERR_load_crypto_strings(void)
  47101. {
  47102. #if defined(OPENSSL_ALL)
  47103. ERR_load_crypto_strings();
  47104. return TEST_SUCCESS;
  47105. #else
  47106. return TEST_SKIPPED;
  47107. #endif
  47108. }
  47109. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  47110. static void free_x509(X509* x)
  47111. {
  47112. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  47113. }
  47114. #endif
  47115. static int test_sk_X509(void)
  47116. {
  47117. EXPECT_DECLS;
  47118. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  47119. {
  47120. STACK_OF(X509)* s = NULL;
  47121. ExpectNotNull(s = sk_X509_new_null());
  47122. ExpectIntEQ(sk_X509_num(s), 0);
  47123. sk_X509_pop_free(s, NULL);
  47124. ExpectNotNull(s = sk_X509_new_null());
  47125. ExpectIntEQ(sk_X509_num(s), 0);
  47126. sk_X509_pop_free(s, NULL);
  47127. ExpectNotNull(s = sk_X509_new_null());
  47128. sk_X509_push(s, (X509*)1);
  47129. ExpectIntEQ(sk_X509_num(s), 1);
  47130. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  47131. sk_X509_push(s, (X509*)2);
  47132. ExpectIntEQ(sk_X509_num(s), 2);
  47133. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  47134. ExpectIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  47135. sk_X509_push(s, (X509*)2);
  47136. sk_X509_pop_free(s, free_x509);
  47137. }
  47138. {
  47139. /* Push a list of 10 X509s onto stack, then verify that
  47140. * value(), push(), shift(), and pop() behave as expected. */
  47141. STACK_OF(X509)* s = NULL;
  47142. X509* xList[10];
  47143. int i = 0;
  47144. const int len = (sizeof(xList) / sizeof(xList[0]));
  47145. for (i = 0; i < len; ++i) {
  47146. xList[i] = NULL;
  47147. ExpectNotNull(xList[i] = X509_new());
  47148. }
  47149. /* test push, pop, and free */
  47150. ExpectNotNull(s = sk_X509_new_null());
  47151. for (i = 0; i < len; ++i) {
  47152. sk_X509_push(s, xList[i]);
  47153. ExpectIntEQ(sk_X509_num(s), i + 1);
  47154. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  47155. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  47156. }
  47157. /* pop returns and removes last pushed on stack, which is index 0
  47158. * in sk_x509_value */
  47159. for (i = 0; i < len; ++i) {
  47160. X509 * x = sk_X509_value(s, 0);
  47161. X509 * y = sk_X509_pop(s);
  47162. X509 * z = xList[len - 1 - i];
  47163. ExpectIntEQ((x == y), 1);
  47164. ExpectIntEQ((x == z), 1);
  47165. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  47166. }
  47167. sk_free(s);
  47168. s = NULL;
  47169. /* test push, shift, and free */
  47170. ExpectNotNull(s = sk_X509_new_null());
  47171. for (i = 0; i < len; ++i) {
  47172. sk_X509_push(s, xList[i]);
  47173. ExpectIntEQ(sk_X509_num(s), i + 1);
  47174. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  47175. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  47176. }
  47177. /* shift returns and removes first pushed on stack, which is index i
  47178. * in sk_x509_value() */
  47179. for (i = 0; i < len; ++i) {
  47180. X509 * x = sk_X509_value(s, len - 1 - i);
  47181. X509 * y = sk_X509_shift(s);
  47182. X509 * z = xList[i];
  47183. ExpectIntEQ((x == y), 1);
  47184. ExpectIntEQ((x == z), 1);
  47185. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  47186. }
  47187. sk_free(s);
  47188. for (i = 0; i < len; ++i)
  47189. X509_free(xList[i]);
  47190. }
  47191. #endif
  47192. return EXPECT_RESULT();
  47193. }
  47194. static int test_sk_X509_CRL(void)
  47195. {
  47196. EXPECT_DECLS;
  47197. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  47198. X509_CRL* crl = NULL;
  47199. XFILE fp = XBADFILE;
  47200. STACK_OF(X509_CRL)* s = NULL;
  47201. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  47202. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  47203. NULL, NULL));
  47204. if (fp != XBADFILE)
  47205. XFCLOSE(fp);
  47206. ExpectNotNull(s = sk_X509_CRL_new());
  47207. ExpectIntEQ(sk_X509_CRL_num(s), 0);
  47208. ExpectIntEQ(sk_X509_CRL_push(s, crl), 1);
  47209. if (EXPECT_FAIL()) {
  47210. X509_CRL_free(crl);
  47211. }
  47212. ExpectIntEQ(sk_X509_CRL_num(s), 1);
  47213. ExpectPtrEq(sk_X509_CRL_value(s, 0), crl);
  47214. sk_X509_CRL_free(s);
  47215. #endif
  47216. return EXPECT_RESULT();
  47217. }
  47218. static int test_X509_get_signature_nid(void)
  47219. {
  47220. EXPECT_DECLS;
  47221. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  47222. X509* x509 = NULL;
  47223. ExpectIntEQ(X509_get_signature_nid(NULL), 0);
  47224. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  47225. SSL_FILETYPE_PEM));
  47226. ExpectIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  47227. X509_free(x509);
  47228. #endif
  47229. return EXPECT_RESULT();
  47230. }
  47231. static int test_X509_REQ(void)
  47232. {
  47233. EXPECT_DECLS;
  47234. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  47235. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  47236. X509_NAME* name = NULL;
  47237. #ifndef NO_RSA
  47238. X509_NAME* subject = NULL;
  47239. #endif
  47240. #if !defined(NO_RSA) || defined(HAVE_ECC)
  47241. X509_REQ* req = NULL;
  47242. EVP_PKEY* priv = NULL;
  47243. EVP_PKEY* pub = NULL;
  47244. unsigned char* der = NULL;
  47245. int len;
  47246. #endif
  47247. #ifndef NO_RSA
  47248. EVP_MD_CTX *mctx = NULL;
  47249. EVP_PKEY_CTX *pkctx = NULL;
  47250. #ifdef USE_CERT_BUFFERS_1024
  47251. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  47252. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  47253. #elif defined(USE_CERT_BUFFERS_2048)
  47254. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  47255. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  47256. #endif
  47257. #endif
  47258. #ifdef HAVE_ECC
  47259. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  47260. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  47261. #endif
  47262. ExpectNotNull(name = X509_NAME_new());
  47263. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  47264. (byte*)"wolfssl.com", 11, 0, 1), WOLFSSL_SUCCESS);
  47265. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  47266. (byte*)"support@wolfssl.com", 19, -1, 1), WOLFSSL_SUCCESS);
  47267. #ifndef NO_RSA
  47268. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  47269. (long)sizeof_client_key_der_2048));
  47270. ExpectNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  47271. (long)sizeof_client_keypub_der_2048));
  47272. ExpectNotNull(req = X509_REQ_new());
  47273. ExpectIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  47274. ExpectIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  47275. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  47276. ExpectIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  47277. ExpectIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  47278. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  47279. ExpectIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  47280. ExpectIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  47281. ExpectIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  47282. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  47283. len = i2d_X509_REQ(req, &der);
  47284. DEBUG_WRITE_DER(der, len, "req.der");
  47285. #ifdef USE_CERT_BUFFERS_1024
  47286. ExpectIntEQ(len, 381);
  47287. #else
  47288. ExpectIntEQ(len, 643);
  47289. #endif
  47290. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  47291. der = NULL;
  47292. mctx = EVP_MD_CTX_new();
  47293. ExpectIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv),
  47294. WOLFSSL_SUCCESS);
  47295. ExpectIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  47296. EVP_MD_CTX_free(mctx);
  47297. mctx = NULL;
  47298. X509_REQ_free(NULL);
  47299. X509_REQ_free(req);
  47300. req = NULL;
  47301. /* Test getting the subject from a newly created X509_REQ */
  47302. ExpectNotNull(req = X509_REQ_new());
  47303. ExpectNotNull(subject = X509_REQ_get_subject_name(req));
  47304. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  47305. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  47306. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  47307. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  47308. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  47309. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  47310. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  47311. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  47312. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  47313. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  47314. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  47315. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  47316. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  47317. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  47318. len = i2d_X509_REQ(req, &der);
  47319. DEBUG_WRITE_DER(der, len, "req2.der");
  47320. #ifdef USE_CERT_BUFFERS_1024
  47321. ExpectIntEQ(len, 435);
  47322. #else
  47323. ExpectIntEQ(len, 696);
  47324. #endif
  47325. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  47326. der = NULL;
  47327. EVP_PKEY_free(pub);
  47328. pub = NULL;
  47329. EVP_PKEY_free(priv);
  47330. priv = NULL;
  47331. X509_REQ_free(req);
  47332. req = NULL;
  47333. #endif
  47334. #ifdef HAVE_ECC
  47335. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  47336. sizeof_ecc_clikey_der_256));
  47337. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  47338. sizeof_ecc_clikeypub_der_256));
  47339. ExpectNotNull(req = X509_REQ_new());
  47340. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  47341. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  47342. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  47343. /* Signature is random and may be shorter or longer. */
  47344. ExpectIntGE((len = i2d_X509_REQ(req, &der)), 245);
  47345. ExpectIntLE(len, 253);
  47346. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  47347. X509_REQ_free(req);
  47348. EVP_PKEY_free(pub);
  47349. EVP_PKEY_free(priv);
  47350. #ifdef FP_ECC
  47351. wc_ecc_fp_free();
  47352. #endif
  47353. #endif /* HAVE_ECC */
  47354. X509_NAME_free(name);
  47355. #endif
  47356. return EXPECT_RESULT();
  47357. }
  47358. static int test_wolfssl_PKCS7(void)
  47359. {
  47360. EXPECT_DECLS;
  47361. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  47362. !defined(NO_RSA)
  47363. PKCS7* pkcs7 = NULL;
  47364. byte data[FOURK_BUF];
  47365. word32 len = sizeof(data);
  47366. const byte* p = data;
  47367. byte content[] = "Test data to encode.";
  47368. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  47369. BIO* bio = NULL;
  47370. byte key[sizeof(client_key_der_2048)];
  47371. word32 keySz = (word32)sizeof(key);
  47372. byte* out = NULL;
  47373. #endif
  47374. ExpectIntGT((len = CreatePKCS7SignedData(data, len, content,
  47375. (word32)sizeof(content), 0, 0, 0, RSA_TYPE)), 0);
  47376. ExpectNull(pkcs7 = d2i_PKCS7(NULL, NULL, len));
  47377. ExpectNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  47378. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  47379. ExpectIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  47380. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  47381. PKCS7_free(pkcs7);
  47382. pkcs7 = NULL;
  47383. /* fail case, without PKCS7_NOVERIFY */
  47384. p = data;
  47385. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  47386. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  47387. 0), WOLFSSL_FAILURE);
  47388. PKCS7_free(pkcs7);
  47389. pkcs7 = NULL;
  47390. /* success case, with PKCS7_NOVERIFY */
  47391. p = data;
  47392. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, len));
  47393. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  47394. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  47395. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  47396. /* test i2d */
  47397. XMEMCPY(key, client_key_der_2048, keySz);
  47398. if (pkcs7 != NULL) {
  47399. pkcs7->privateKey = key;
  47400. pkcs7->privateKeySz = (word32)sizeof(key);
  47401. pkcs7->encryptOID = RSAk;
  47402. #ifdef NO_SHA
  47403. pkcs7->hashOID = SHA256h;
  47404. #else
  47405. pkcs7->hashOID = SHAh;
  47406. #endif
  47407. }
  47408. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  47409. ExpectIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  47410. #ifndef NO_ASN_TIME
  47411. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  47412. #else
  47413. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 625);
  47414. #endif
  47415. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47416. BIO_free(bio);
  47417. #endif
  47418. PKCS7_free(NULL);
  47419. PKCS7_free(pkcs7);
  47420. #endif
  47421. return EXPECT_RESULT();
  47422. }
  47423. static int test_wolfSSL_PKCS7_sign(void)
  47424. {
  47425. EXPECT_DECLS;
  47426. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  47427. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  47428. PKCS7* p7 = NULL;
  47429. PKCS7* p7Ver = NULL;
  47430. byte* out = NULL;
  47431. byte* tmpPtr = NULL;
  47432. int outLen = 0;
  47433. int flags = 0;
  47434. byte data[] = "Test data to encode.";
  47435. const char* cert = "./certs/server-cert.pem";
  47436. const char* key = "./certs/server-key.pem";
  47437. const char* ca = "./certs/ca-cert.pem";
  47438. WOLFSSL_BIO* certBio = NULL;
  47439. WOLFSSL_BIO* keyBio = NULL;
  47440. WOLFSSL_BIO* caBio = NULL;
  47441. WOLFSSL_BIO* inBio = NULL;
  47442. X509* signCert = NULL;
  47443. EVP_PKEY* signKey = NULL;
  47444. X509* caCert = NULL;
  47445. X509_STORE* store = NULL;
  47446. #ifndef NO_PKCS7_STREAM
  47447. int z;
  47448. int ret;
  47449. #endif /* !NO_PKCS7_STREAM */
  47450. /* read signer cert/key into BIO */
  47451. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  47452. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  47453. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  47454. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  47455. /* read CA cert into store (for verify) */
  47456. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  47457. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  47458. ExpectNotNull(store = X509_STORE_new());
  47459. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  47460. /* data to be signed into BIO */
  47461. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47462. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47463. /* PKCS7_sign, bad args: signer NULL */
  47464. ExpectNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  47465. /* PKCS7_sign, bad args: signer key NULL */
  47466. ExpectNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  47467. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  47468. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  47469. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  47470. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  47471. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  47472. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  47473. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  47474. {
  47475. flags = PKCS7_BINARY;
  47476. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47477. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  47478. /* verify with d2i_PKCS7 */
  47479. tmpPtr = out;
  47480. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  47481. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  47482. PKCS7_free(p7Ver);
  47483. p7Ver = NULL;
  47484. /* verify with wc_PKCS7_VerifySignedData */
  47485. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47486. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  47487. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  47488. #ifndef NO_PKCS7_STREAM
  47489. /* verify with wc_PKCS7_VerifySignedData streaming */
  47490. wc_PKCS7_Free(p7Ver);
  47491. p7Ver = NULL;
  47492. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47493. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  47494. /* test for streaming */
  47495. ret = -1;
  47496. for (z = 0; z < outLen && ret != 0; z++) {
  47497. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  47498. if (ret < 0){
  47499. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  47500. }
  47501. }
  47502. ExpectIntEQ(ret, 0);
  47503. #endif /* !NO_PKCS7_STREAM */
  47504. /* compare the signer found to expected signer */
  47505. ExpectIntNE(p7Ver->verifyCertSz, 0);
  47506. tmpPtr = NULL;
  47507. ExpectIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  47508. ExpectIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  47509. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  47510. tmpPtr = NULL;
  47511. wc_PKCS7_Free(p7Ver);
  47512. p7Ver = NULL;
  47513. ExpectNotNull(out);
  47514. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47515. out = NULL;
  47516. PKCS7_free(p7);
  47517. p7 = NULL;
  47518. }
  47519. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  47520. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  47521. {
  47522. /* re-populate input BIO, may have been consumed */
  47523. BIO_free(inBio);
  47524. inBio = NULL;
  47525. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47526. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47527. flags = PKCS7_BINARY | PKCS7_STREAM;
  47528. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47529. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  47530. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  47531. /* PKCS7_final, bad args: PKCS7 null */
  47532. ExpectIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  47533. /* PKCS7_final, bad args: PKCS7 null */
  47534. ExpectIntEQ(PKCS7_final(p7, NULL, 0), 0);
  47535. tmpPtr = out;
  47536. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  47537. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  47538. PKCS7_free(p7Ver);
  47539. p7Ver = NULL;
  47540. ExpectNotNull(out);
  47541. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47542. out = NULL;
  47543. PKCS7_free(p7);
  47544. p7 = NULL;
  47545. }
  47546. /* TEST SUCCESS: Detached, not streaming, not MIME */
  47547. {
  47548. /* re-populate input BIO, may have been consumed */
  47549. BIO_free(inBio);
  47550. inBio = NULL;
  47551. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47552. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47553. flags = PKCS7_BINARY | PKCS7_DETACHED;
  47554. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47555. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  47556. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  47557. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47558. if (p7Ver != NULL) {
  47559. p7Ver->content = data;
  47560. p7Ver->contentSz = sizeof(data);
  47561. }
  47562. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  47563. wc_PKCS7_Free(p7Ver);
  47564. p7Ver = NULL;
  47565. #ifndef NO_PKCS7_STREAM
  47566. /* verify with wc_PKCS7_VerifySignedData streaming */
  47567. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47568. if (p7Ver != NULL) {
  47569. p7Ver->content = data;
  47570. p7Ver->contentSz = sizeof(data);
  47571. }
  47572. /* test for streaming */
  47573. ret = -1;
  47574. for (z = 0; z < outLen && ret != 0; z++) {
  47575. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  47576. if (ret < 0){
  47577. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  47578. }
  47579. }
  47580. ExpectIntEQ(ret, 0);
  47581. wc_PKCS7_Free(p7Ver);
  47582. p7Ver = NULL;
  47583. #endif /* !NO_PKCS7_STREAM */
  47584. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  47585. * yet support detached content */
  47586. tmpPtr = out;
  47587. ExpectNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  47588. PKCS7_free(p7Ver);
  47589. p7Ver = NULL;
  47590. ExpectNotNull(out);
  47591. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47592. out = NULL;
  47593. PKCS7_free(p7);
  47594. p7 = NULL;
  47595. }
  47596. /* TEST SUCCESS: Detached, streaming, not MIME */
  47597. {
  47598. /* re-populate input BIO, may have been consumed */
  47599. BIO_free(inBio);
  47600. inBio = NULL;
  47601. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47602. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47603. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  47604. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47605. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  47606. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  47607. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  47608. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47609. if (p7Ver != NULL) {
  47610. p7Ver->content = data;
  47611. p7Ver->contentSz = sizeof(data);
  47612. }
  47613. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, outLen), 0);
  47614. wc_PKCS7_Free(p7Ver);
  47615. p7Ver = NULL;
  47616. ExpectNotNull(out);
  47617. #ifndef NO_PKCS7_STREAM
  47618. /* verify with wc_PKCS7_VerifySignedData streaming */
  47619. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47620. if (p7Ver != NULL) {
  47621. p7Ver->content = data;
  47622. p7Ver->contentSz = sizeof(data);
  47623. }
  47624. /* test for streaming */
  47625. ret = -1;
  47626. for (z = 0; z < outLen && ret != 0; z++) {
  47627. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  47628. if (ret < 0){
  47629. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  47630. }
  47631. }
  47632. ExpectIntEQ(ret, 0);
  47633. ExpectNotNull(out);
  47634. wc_PKCS7_Free(p7Ver);
  47635. p7Ver = NULL;
  47636. #endif /* !NO_PKCS7_STREAM */
  47637. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47638. PKCS7_free(p7);
  47639. p7 = NULL;
  47640. }
  47641. X509_STORE_free(store);
  47642. X509_free(caCert);
  47643. X509_free(signCert);
  47644. EVP_PKEY_free(signKey);
  47645. BIO_free(inBio);
  47646. BIO_free(keyBio);
  47647. BIO_free(certBio);
  47648. BIO_free(caBio);
  47649. #endif
  47650. return EXPECT_RESULT();
  47651. }
  47652. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  47653. {
  47654. EXPECT_DECLS;
  47655. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  47656. PKCS7_SIGNED* pkcs7 = NULL;
  47657. ExpectNotNull(pkcs7 = PKCS7_SIGNED_new());
  47658. ExpectIntEQ(pkcs7->contentOID, SIGNED_DATA);
  47659. PKCS7_SIGNED_free(pkcs7);
  47660. #endif
  47661. return EXPECT_RESULT();
  47662. }
  47663. #ifndef NO_BIO
  47664. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  47665. {
  47666. EXPECT_DECLS;
  47667. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  47668. PKCS7* pkcs7 = NULL;
  47669. BIO* bio = NULL;
  47670. const byte* cert_buf = NULL;
  47671. int ret = 0;
  47672. WC_RNG rng;
  47673. const byte data[] = { /* Hello World */
  47674. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  47675. 0x72,0x6c,0x64
  47676. };
  47677. #ifndef NO_RSA
  47678. #if defined(USE_CERT_BUFFERS_2048)
  47679. byte key[sizeof(client_key_der_2048)];
  47680. byte cert[sizeof(client_cert_der_2048)];
  47681. word32 keySz = (word32)sizeof(key);
  47682. word32 certSz = (word32)sizeof(cert);
  47683. XMEMSET(key, 0, keySz);
  47684. XMEMSET(cert, 0, certSz);
  47685. XMEMCPY(key, client_key_der_2048, keySz);
  47686. XMEMCPY(cert, client_cert_der_2048, certSz);
  47687. #elif defined(USE_CERT_BUFFERS_1024)
  47688. byte key[sizeof_client_key_der_1024];
  47689. byte cert[sizeof(sizeof_client_cert_der_1024)];
  47690. word32 keySz = (word32)sizeof(key);
  47691. word32 certSz = (word32)sizeof(cert);
  47692. XMEMSET(key, 0, keySz);
  47693. XMEMSET(cert, 0, certSz);
  47694. XMEMCPY(key, client_key_der_1024, keySz);
  47695. XMEMCPY(cert, client_cert_der_1024, certSz);
  47696. #else
  47697. unsigned char cert[ONEK_BUF];
  47698. unsigned char key[ONEK_BUF];
  47699. XFILE fp = XBADFILE;
  47700. int certSz;
  47701. int keySz;
  47702. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  47703. XBADFILE);
  47704. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  47705. fp), 0);
  47706. if (fp != XBADFILE) {
  47707. XFCLOSE(fp);
  47708. fp = XBADFILE;
  47709. }
  47710. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  47711. XBADFILE);
  47712. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  47713. 0);
  47714. if (fp != XBADFILE) {
  47715. XFCLOSE(fp);
  47716. fp = XBADFILE;
  47717. }
  47718. #endif
  47719. #elif defined(HAVE_ECC)
  47720. #if defined(USE_CERT_BUFFERS_256)
  47721. unsigned char cert[sizeof(cliecc_cert_der_256)];
  47722. unsigned char key[sizeof(ecc_clikey_der_256)];
  47723. int certSz = (int)sizeof(cert);
  47724. int keySz = (int)sizeof(key);
  47725. XMEMSET(cert, 0, certSz);
  47726. XMEMSET(key, 0, keySz);
  47727. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  47728. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  47729. #else
  47730. unsigned char cert[ONEK_BUF];
  47731. unsigned char key[ONEK_BUF];
  47732. XFILE fp = XBADFILE;
  47733. int certSz, keySz;
  47734. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  47735. XBADFILE);
  47736. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  47737. fp), 0);
  47738. if (fp != XBADFILE) {
  47739. XFCLOSE(fp);
  47740. fp = XBADFILE;
  47741. }
  47742. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  47743. XBADFILE);
  47744. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  47745. 0);
  47746. if (fp != XBADFILE) {
  47747. XFCLOSE(fp);
  47748. fp = XBADFILE;
  47749. }
  47750. #endif
  47751. #else
  47752. #error PKCS7 requires ECC or RSA
  47753. #endif
  47754. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  47755. /* initialize with DER encoded cert */
  47756. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  47757. /* init rng */
  47758. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47759. ExpectIntEQ(wc_InitRng(&rng), 0);
  47760. if (pkcs7 != NULL) {
  47761. pkcs7->rng = &rng;
  47762. pkcs7->content = (byte*)data; /* not used for ex */
  47763. pkcs7->contentSz = (word32)sizeof(data);
  47764. pkcs7->contentOID = SIGNED_DATA;
  47765. pkcs7->privateKey = key;
  47766. pkcs7->privateKeySz = (word32)sizeof(key);
  47767. pkcs7->encryptOID = RSAk;
  47768. #ifdef NO_SHA
  47769. pkcs7->hashOID = SHA256h;
  47770. #else
  47771. pkcs7->hashOID = SHAh;
  47772. #endif
  47773. pkcs7->signedAttribs = NULL;
  47774. pkcs7->signedAttribsSz = 0;
  47775. }
  47776. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  47777. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  47778. ExpectIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  47779. /* Read PKCS#7 PEM from BIO */
  47780. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  47781. ExpectIntGE(ret, 0);
  47782. BIO_free(bio);
  47783. wc_PKCS7_Free(pkcs7);
  47784. wc_FreeRng(&rng);
  47785. #endif
  47786. return EXPECT_RESULT();
  47787. }
  47788. #ifdef HAVE_SMIME
  47789. static int test_wolfSSL_SMIME_read_PKCS7(void)
  47790. {
  47791. EXPECT_DECLS;
  47792. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  47793. !defined(NO_RSA)
  47794. PKCS7* pkcs7 = NULL;
  47795. BIO* bio = NULL;
  47796. BIO* bcont = NULL;
  47797. BIO* out = NULL;
  47798. const byte* outBuf = NULL;
  47799. int outBufLen = 0;
  47800. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  47801. XFILE smimeTestFile = XBADFILE;
  47802. ExpectTrue((smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r")) !=
  47803. XBADFILE);
  47804. /* smime-test.p7s */
  47805. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  47806. ExpectNotNull(bio);
  47807. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47808. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47809. ExpectNotNull(pkcs7);
  47810. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47811. PKCS7_NOVERIFY), SSL_SUCCESS);
  47812. XFCLOSE(smimeTestFile);
  47813. if (bcont) BIO_free(bcont);
  47814. bcont = NULL;
  47815. wolfSSL_PKCS7_free(pkcs7);
  47816. pkcs7 = NULL;
  47817. /* smime-test-multipart.p7s */
  47818. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  47819. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47820. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47821. ExpectNotNull(pkcs7);
  47822. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47823. PKCS7_NOVERIFY), SSL_SUCCESS);
  47824. XFCLOSE(smimeTestFile);
  47825. if (bcont) BIO_free(bcont);
  47826. bcont = NULL;
  47827. wolfSSL_PKCS7_free(pkcs7);
  47828. pkcs7 = NULL;
  47829. /* smime-test-multipart-badsig.p7s */
  47830. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  47831. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47832. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47833. ExpectNotNull(pkcs7); /* can read in the unverified smime bundle */
  47834. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47835. PKCS7_NOVERIFY), SSL_FAILURE);
  47836. XFCLOSE(smimeTestFile);
  47837. if (bcont) BIO_free(bcont);
  47838. bcont = NULL;
  47839. wolfSSL_PKCS7_free(pkcs7);
  47840. pkcs7 = NULL;
  47841. /* smime-test-canon.p7s */
  47842. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  47843. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47844. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47845. ExpectNotNull(pkcs7);
  47846. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  47847. PKCS7_NOVERIFY), SSL_SUCCESS);
  47848. XFCLOSE(smimeTestFile);
  47849. if (bcont) BIO_free(bcont);
  47850. bcont = NULL;
  47851. wolfSSL_PKCS7_free(pkcs7);
  47852. pkcs7 = NULL;
  47853. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  47854. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  47855. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  47856. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  47857. ExpectNotNull(pkcs7);
  47858. out = wolfSSL_BIO_new(BIO_s_mem());
  47859. ExpectNotNull(out);
  47860. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  47861. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  47862. ExpectIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  47863. /* Content-Type should not show up at beginning of output buffer */
  47864. ExpectIntGT(outBufLen, XSTRLEN(contTypeText));
  47865. ExpectIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  47866. BIO_free(out);
  47867. BIO_free(bio);
  47868. if (bcont) BIO_free(bcont);
  47869. wolfSSL_PKCS7_free(pkcs7);
  47870. #endif
  47871. return EXPECT_RESULT();
  47872. }
  47873. static int test_wolfSSL_SMIME_write_PKCS7(void)
  47874. {
  47875. EXPECT_DECLS;
  47876. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  47877. PKCS7* p7 = NULL;
  47878. PKCS7* p7Ver = NULL;
  47879. int flags = 0;
  47880. byte data[] = "Test data to encode.";
  47881. const char* cert = "./certs/server-cert.pem";
  47882. const char* key = "./certs/server-key.pem";
  47883. const char* ca = "./certs/ca-cert.pem";
  47884. WOLFSSL_BIO* certBio = NULL;
  47885. WOLFSSL_BIO* keyBio = NULL;
  47886. WOLFSSL_BIO* caBio = NULL;
  47887. WOLFSSL_BIO* inBio = NULL;
  47888. WOLFSSL_BIO* outBio = NULL;
  47889. WOLFSSL_BIO* content = NULL;
  47890. X509* signCert = NULL;
  47891. EVP_PKEY* signKey = NULL;
  47892. X509* caCert = NULL;
  47893. X509_STORE* store = NULL;
  47894. /* read signer cert/key into BIO */
  47895. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  47896. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  47897. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  47898. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  47899. /* read CA cert into store (for verify) */
  47900. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  47901. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  47902. ExpectNotNull(store = X509_STORE_new());
  47903. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  47904. /* generate and verify SMIME: not detached */
  47905. {
  47906. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47907. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47908. flags = PKCS7_STREAM;
  47909. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47910. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  47911. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47912. /* bad arg: out NULL */
  47913. ExpectIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  47914. /* bad arg: pkcs7 NULL */
  47915. ExpectIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  47916. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47917. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  47918. BIO_free(content);
  47919. content = NULL;
  47920. BIO_free(inBio);
  47921. inBio = NULL;
  47922. BIO_free(outBio);
  47923. outBio = NULL;
  47924. PKCS7_free(p7Ver);
  47925. p7Ver = NULL;
  47926. PKCS7_free(p7);
  47927. p7 = NULL;
  47928. }
  47929. /* generate and verify SMIME: not detached, add Content-Type */
  47930. {
  47931. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47932. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47933. flags = PKCS7_STREAM | PKCS7_TEXT;
  47934. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47935. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  47936. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47937. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47938. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  47939. BIO_free(content);
  47940. content = NULL;
  47941. BIO_free(inBio);
  47942. inBio = NULL;
  47943. BIO_free(outBio);
  47944. outBio = NULL;
  47945. PKCS7_free(p7Ver);
  47946. p7Ver = NULL;
  47947. PKCS7_free(p7);
  47948. p7 = NULL;
  47949. }
  47950. /* generate and verify SMIME: detached */
  47951. {
  47952. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47953. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47954. flags = PKCS7_DETACHED | PKCS7_STREAM;
  47955. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47956. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  47957. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47958. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47959. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  47960. BIO_free(content);
  47961. content = NULL;
  47962. BIO_free(inBio);
  47963. inBio = NULL;
  47964. BIO_free(outBio);
  47965. outBio = NULL;
  47966. PKCS7_free(p7Ver);
  47967. p7Ver = NULL;
  47968. PKCS7_free(p7);
  47969. p7 = NULL;
  47970. }
  47971. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  47972. {
  47973. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47974. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47975. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  47976. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47977. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  47978. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  47979. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  47980. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  47981. BIO_free(content);
  47982. content = NULL;
  47983. BIO_free(inBio);
  47984. inBio = NULL;
  47985. BIO_free(outBio);
  47986. outBio = NULL;
  47987. PKCS7_free(p7Ver);
  47988. p7Ver = NULL;
  47989. PKCS7_free(p7);
  47990. p7 = NULL;
  47991. }
  47992. X509_STORE_free(store);
  47993. X509_free(caCert);
  47994. X509_free(signCert);
  47995. EVP_PKEY_free(signKey);
  47996. BIO_free(keyBio);
  47997. BIO_free(certBio);
  47998. BIO_free(caBio);
  47999. #endif
  48000. return EXPECT_RESULT();
  48001. }
  48002. #endif /* HAVE_SMIME */
  48003. #endif /* !NO_BIO */
  48004. /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
  48005. * returns 0) */
  48006. static int test_X509_STORE_No_SSL_CTX(void)
  48007. {
  48008. EXPECT_DECLS;
  48009. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  48010. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  48011. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  48012. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  48013. defined(HAVE_CRL) && !defined(NO_RSA)
  48014. X509_STORE * store = NULL;
  48015. X509_STORE_CTX * storeCtx = NULL;
  48016. X509_CRL * crl = NULL;
  48017. X509 * ca = NULL;
  48018. X509 * cert = NULL;
  48019. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  48020. const char srvCert[] = "./certs/server-cert.pem";
  48021. const char caCert[] = "./certs/ca-cert.pem";
  48022. const char caDir[] = "./certs/crl/hash_pem";
  48023. XFILE fp = XBADFILE;
  48024. X509_LOOKUP * lookup = NULL;
  48025. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  48026. /* Set up store with CA */
  48027. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  48028. SSL_FILETYPE_PEM)));
  48029. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  48030. /* Add CRL lookup directory to store
  48031. * NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
  48032. * of crl.pem */
  48033. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  48034. X509_LOOKUP_hash_dir())));
  48035. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
  48036. X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
  48037. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  48038. SSL_SUCCESS);
  48039. /* Add CRL to store NOT containing the verified certificate, which
  48040. * forces use of the CRL lookup directory */
  48041. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  48042. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  48043. NULL, NULL));
  48044. if (fp != XBADFILE)
  48045. XFCLOSE(fp);
  48046. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  48047. /* Create verification context outside of an SSL session */
  48048. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  48049. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  48050. SSL_FILETYPE_PEM)));
  48051. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  48052. /* Perform verification, which should NOT indicate CRL missing due to the
  48053. * store CM's X509 store pointer being NULL */
  48054. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  48055. X509_CRL_free(crl);
  48056. X509_STORE_free(store);
  48057. X509_STORE_CTX_free(storeCtx);
  48058. X509_free(cert);
  48059. X509_free(ca);
  48060. #endif
  48061. return EXPECT_RESULT();
  48062. }
  48063. /* Test of X509 store use outside of SSL context w/ CRL lookup, but
  48064. * with X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
  48065. static int test_X509_LOOKUP_add_dir(void)
  48066. {
  48067. EXPECT_DECLS;
  48068. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  48069. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  48070. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  48071. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  48072. defined(HAVE_CRL) && !defined(NO_RSA)
  48073. X509_STORE * store = NULL;
  48074. X509_STORE_CTX * storeCtx = NULL;
  48075. X509_CRL * crl = NULL;
  48076. X509 * ca = NULL;
  48077. X509 * cert = NULL;
  48078. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  48079. const char srvCert[] = "./certs/server-cert.pem";
  48080. const char caCert[] = "./certs/ca-cert.pem";
  48081. const char caDir[] = "./certs/crl/hash_der";
  48082. XFILE fp = XBADFILE;
  48083. X509_LOOKUP * lookup = NULL;
  48084. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  48085. /* Set up store with CA */
  48086. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  48087. SSL_FILETYPE_PEM)));
  48088. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  48089. /* Add CRL lookup directory to store.
  48090. * Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
  48091. * of crl.der */
  48092. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  48093. X509_LOOKUP_hash_dir())));
  48094. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
  48095. SSL_SUCCESS);
  48096. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  48097. SSL_SUCCESS);
  48098. /* Add CRL to store NOT containing the verified certificate, which
  48099. * forces use of the CRL lookup directory */
  48100. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  48101. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  48102. NULL, NULL));
  48103. if (fp != XBADFILE) {
  48104. XFCLOSE(fp);
  48105. fp = XBADFILE;
  48106. }
  48107. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  48108. /* Create verification context outside of an SSL session */
  48109. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  48110. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  48111. SSL_FILETYPE_PEM)));
  48112. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  48113. /* Perform verification, which should NOT return CRL missing */
  48114. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  48115. X509_CRL_free(crl);
  48116. crl = NULL;
  48117. X509_STORE_free(store);
  48118. store = NULL;
  48119. X509_STORE_CTX_free(storeCtx);
  48120. storeCtx = NULL;
  48121. X509_free(cert);
  48122. cert = NULL;
  48123. X509_free(ca);
  48124. ca = NULL;
  48125. /* Now repeat the same, but look for X509_FILETYPE_PEM.
  48126. * We should get CRL_MISSING at the end, because the lookup
  48127. * dir has only ASN1 CRLs. */
  48128. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  48129. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  48130. SSL_FILETYPE_PEM)));
  48131. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  48132. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  48133. X509_LOOKUP_hash_dir())));
  48134. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
  48135. SSL_SUCCESS);
  48136. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  48137. SSL_SUCCESS);
  48138. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  48139. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  48140. NULL, NULL));
  48141. if (fp != XBADFILE) {
  48142. XFCLOSE(fp);
  48143. fp = XBADFILE;
  48144. }
  48145. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  48146. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  48147. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  48148. SSL_FILETYPE_PEM)));
  48149. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  48150. /* Now we SHOULD get CRL_MISSING, because we looked for PEM
  48151. * in dir containing only ASN1/DER. */
  48152. ExpectIntEQ(X509_verify_cert(storeCtx), WOLFSSL_FAILURE);
  48153. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx), CRL_MISSING);
  48154. X509_CRL_free(crl);
  48155. X509_STORE_free(store);
  48156. X509_STORE_CTX_free(storeCtx);
  48157. X509_free(cert);
  48158. X509_free(ca);
  48159. #endif
  48160. return EXPECT_RESULT();
  48161. }
  48162. /*----------------------------------------------------------------------------*
  48163. | Certificate Failure Checks
  48164. *----------------------------------------------------------------------------*/
  48165. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  48166. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  48167. #if !defined(NO_RSA) || defined(HAVE_ECC)
  48168. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  48169. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  48170. int type)
  48171. {
  48172. int ret;
  48173. WOLFSSL_CERT_MANAGER* cm = NULL;
  48174. switch (type) {
  48175. case TESTING_RSA:
  48176. #ifdef NO_RSA
  48177. fprintf(stderr, "RSA disabled, skipping test\n");
  48178. return ASN_SIG_CONFIRM_E;
  48179. #else
  48180. break;
  48181. #endif
  48182. case TESTING_ECC:
  48183. #ifndef HAVE_ECC
  48184. fprintf(stderr, "ECC disabled, skipping test\n");
  48185. return ASN_SIG_CONFIRM_E;
  48186. #else
  48187. break;
  48188. #endif
  48189. default:
  48190. fprintf(stderr, "Bad function argument\n");
  48191. return BAD_FUNC_ARG;
  48192. }
  48193. cm = wolfSSL_CertManagerNew();
  48194. if (cm == NULL) {
  48195. fprintf(stderr, "wolfSSL_CertManagerNew failed\n");
  48196. return -1;
  48197. }
  48198. #ifndef NO_FILESYSTEM
  48199. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  48200. if (ret != WOLFSSL_SUCCESS) {
  48201. fprintf(stderr, "wolfSSL_CertManagerLoadCA failed\n");
  48202. wolfSSL_CertManagerFree(cm);
  48203. return ret;
  48204. }
  48205. #else
  48206. (void)ca;
  48207. #endif
  48208. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, cert_sz,
  48209. WOLFSSL_FILETYPE_ASN1);
  48210. /* Let ExpectIntEQ handle return code */
  48211. wolfSSL_CertManagerFree(cm);
  48212. return ret;
  48213. }
  48214. #endif
  48215. #if !defined(NO_FILESYSTEM)
  48216. static int test_RsaSigFailure_cm(void)
  48217. {
  48218. EXPECT_DECLS;
  48219. #ifndef NO_RSA
  48220. const char* ca_cert = "./certs/ca-cert.pem";
  48221. const char* server_cert = "./certs/server-cert.der";
  48222. byte* cert_buf = NULL;
  48223. size_t cert_sz = 0;
  48224. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  48225. if (cert_buf != NULL) {
  48226. /* corrupt DER - invert last byte, which is signature */
  48227. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  48228. /* test bad cert */
  48229. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  48230. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  48231. WOLFSSL_FATAL_ERROR);
  48232. #else
  48233. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  48234. ASN_SIG_CONFIRM_E);
  48235. #endif
  48236. }
  48237. /* load_file() uses malloc. */
  48238. if (cert_buf != NULL) {
  48239. free(cert_buf);
  48240. }
  48241. #endif /* !NO_RSA */
  48242. return EXPECT_RESULT();
  48243. }
  48244. static int test_EccSigFailure_cm(void)
  48245. {
  48246. EXPECT_DECLS;
  48247. #ifdef HAVE_ECC
  48248. /* self-signed ECC cert, so use server cert as CA */
  48249. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  48250. const char* server_cert = "./certs/server-ecc.der";
  48251. byte* cert_buf = NULL;
  48252. size_t cert_sz = 0;
  48253. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  48254. if (cert_buf != NULL) {
  48255. /* corrupt DER - invert last byte, which is signature */
  48256. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  48257. /* test bad cert */
  48258. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  48259. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  48260. WOLFSSL_FATAL_ERROR);
  48261. #else
  48262. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  48263. ASN_SIG_CONFIRM_E);
  48264. #endif
  48265. }
  48266. /* load_file() uses malloc. */
  48267. if (cert_buf != NULL) {
  48268. free(cert_buf);
  48269. }
  48270. #ifdef FP_ECC
  48271. wc_ecc_fp_free();
  48272. #endif
  48273. #endif /* HAVE_ECC */
  48274. return EXPECT_RESULT();
  48275. }
  48276. #endif /* !NO_FILESYSTEM */
  48277. #endif /* NO_CERTS */
  48278. #ifdef WOLFSSL_TLS13
  48279. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  48280. #ifdef WC_SHA384_DIGEST_SIZE
  48281. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  48282. #else
  48283. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  48284. #endif
  48285. #endif
  48286. #ifdef WOLFSSL_EARLY_DATA
  48287. static const char earlyData[] = "Early Data";
  48288. static char earlyDataBuffer[1];
  48289. #endif
  48290. static int test_tls13_apis(void)
  48291. {
  48292. EXPECT_DECLS;
  48293. int ret;
  48294. #ifndef WOLFSSL_NO_TLS12
  48295. #ifndef NO_WOLFSSL_CLIENT
  48296. WOLFSSL_CTX* clientTls12Ctx = NULL;
  48297. WOLFSSL* clientTls12Ssl = NULL;
  48298. #endif
  48299. #ifndef NO_WOLFSSL_SERVER
  48300. WOLFSSL_CTX* serverTls12Ctx = NULL;
  48301. WOLFSSL* serverTls12Ssl = NULL;
  48302. #endif
  48303. #endif
  48304. #ifndef NO_WOLFSSL_CLIENT
  48305. WOLFSSL_CTX* clientCtx = NULL;
  48306. WOLFSSL* clientSsl = NULL;
  48307. #endif
  48308. #ifndef NO_WOLFSSL_SERVER
  48309. WOLFSSL_CTX* serverCtx = NULL;
  48310. WOLFSSL* serverSsl = NULL;
  48311. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  48312. const char* ourCert = svrCertFile;
  48313. const char* ourKey = svrKeyFile;
  48314. #endif
  48315. #endif
  48316. int required;
  48317. #ifdef WOLFSSL_EARLY_DATA
  48318. int outSz;
  48319. #endif
  48320. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  48321. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  48322. #ifdef HAVE_PQC
  48323. WOLFSSL_KYBER_LEVEL1
  48324. #else
  48325. WOLFSSL_ECC_SECP256R1
  48326. #endif
  48327. };
  48328. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  48329. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  48330. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  48331. int numGroups = 2;
  48332. #endif
  48333. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  48334. char groupList[] =
  48335. #ifndef NO_ECC_SECP
  48336. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  48337. "P-521:"
  48338. #endif
  48339. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  48340. "P-384:"
  48341. #endif
  48342. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  48343. "P-256"
  48344. #if defined(HAVE_PQC) && defined(HAVE_LIBOQS)
  48345. ":P256_KYBER_LEVEL1"
  48346. #endif
  48347. #endif
  48348. #endif /* !defined(NO_ECC_SECP) */
  48349. #ifdef HAVE_PQC
  48350. ":KYBER_LEVEL1"
  48351. #endif
  48352. "";
  48353. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  48354. (void)ret;
  48355. #ifndef WOLFSSL_NO_TLS12
  48356. #ifndef NO_WOLFSSL_CLIENT
  48357. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  48358. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  48359. #endif
  48360. #ifndef NO_WOLFSSL_SERVER
  48361. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  48362. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  48363. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  48364. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey,
  48365. WOLFSSL_FILETYPE_PEM);
  48366. #endif
  48367. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  48368. #endif
  48369. #endif
  48370. #ifndef NO_WOLFSSL_CLIENT
  48371. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  48372. clientSsl = wolfSSL_new(clientCtx);
  48373. #endif
  48374. #ifndef NO_WOLFSSL_SERVER
  48375. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  48376. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  48377. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  48378. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  48379. #endif
  48380. serverSsl = wolfSSL_new(serverCtx);
  48381. ExpectNotNull(serverSsl);
  48382. #endif
  48383. #ifdef WOLFSSL_SEND_HRR_COOKIE
  48384. ExpectIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  48385. #ifndef NO_WOLFSSL_CLIENT
  48386. ExpectIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  48387. #endif
  48388. #ifndef NO_WOLFSSL_SERVER
  48389. #ifndef WOLFSSL_NO_TLS12
  48390. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0),
  48391. BAD_FUNC_ARG);
  48392. #endif
  48393. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  48394. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  48395. WOLFSSL_SUCCESS);
  48396. #endif
  48397. #endif
  48398. #ifdef HAVE_SUPPORTED_CURVES
  48399. #ifdef HAVE_ECC
  48400. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  48401. BAD_FUNC_ARG);
  48402. #ifndef NO_WOLFSSL_SERVER
  48403. do {
  48404. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  48405. #ifdef WOLFSSL_ASYNC_CRYPT
  48406. if (ret == WC_PENDING_E)
  48407. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  48408. #endif
  48409. }
  48410. while (ret == WC_PENDING_E);
  48411. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  48412. #endif
  48413. #ifndef NO_WOLFSSL_CLIENT
  48414. #ifndef WOLFSSL_NO_TLS12
  48415. do {
  48416. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  48417. #ifdef WOLFSSL_ASYNC_CRYPT
  48418. if (ret == WC_PENDING_E)
  48419. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  48420. #endif
  48421. }
  48422. while (ret == WC_PENDING_E);
  48423. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  48424. #endif
  48425. do {
  48426. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  48427. #ifdef WOLFSSL_ASYNC_CRYPT
  48428. if (ret == WC_PENDING_E)
  48429. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  48430. #endif
  48431. }
  48432. while (ret == WC_PENDING_E);
  48433. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  48434. #endif
  48435. #elif defined(HAVE_CURVE25519)
  48436. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  48437. #ifndef NO_WOLFSSL_SERVER
  48438. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  48439. WOLFSSL_SUCCESS);
  48440. #endif
  48441. #ifndef NO_WOLFSSL_CLIENT
  48442. #ifndef WOLFSSL_NO_TLS12
  48443. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  48444. WOLFSSL_SUCCESS);
  48445. #endif
  48446. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  48447. WOLFSSL_SUCCESS);
  48448. #endif
  48449. #elif defined(HAVE_CURVE448)
  48450. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  48451. #ifndef NO_WOLFSSL_SERVER
  48452. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  48453. WOLFSSL_SUCCESS);
  48454. #endif
  48455. #ifndef NO_WOLFSSL_CLIENT
  48456. #ifndef WOLFSSL_NO_TLS12
  48457. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  48458. WOLFSSL_SUCCESS);
  48459. #endif
  48460. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  48461. WOLFSSL_SUCCESS);
  48462. #endif
  48463. #else
  48464. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  48465. BAD_FUNC_ARG);
  48466. #ifndef NO_WOLFSSL_CLIENT
  48467. #ifndef WOLFSSL_NO_TLS12
  48468. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  48469. NOT_COMPILED_IN);
  48470. #endif
  48471. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  48472. NOT_COMPILED_IN);
  48473. #endif
  48474. #endif
  48475. #if defined(HAVE_PQC)
  48476. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  48477. #ifndef NO_WOLFSSL_SERVER
  48478. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  48479. WOLFSSL_SUCCESS);
  48480. #endif
  48481. #ifndef NO_WOLFSSL_CLIENT
  48482. #ifndef WOLFSSL_NO_TLS12
  48483. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  48484. BAD_FUNC_ARG);
  48485. #endif
  48486. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  48487. WOLFSSL_SUCCESS);
  48488. #endif
  48489. #endif
  48490. ExpectIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  48491. #ifndef NO_WOLFSSL_SERVER
  48492. ExpectIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  48493. #endif
  48494. #ifndef NO_WOLFSSL_CLIENT
  48495. #ifndef WOLFSSL_NO_TLS12
  48496. ExpectIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  48497. #endif
  48498. ExpectIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  48499. #endif
  48500. #endif /* HAVE_SUPPORTED_CURVES */
  48501. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  48502. #ifndef NO_WOLFSSL_CLIENT
  48503. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  48504. #endif
  48505. #ifndef NO_WOLFSSL_SERVER
  48506. #ifndef WOLFSSL_NO_TLS12
  48507. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  48508. #endif
  48509. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  48510. #endif
  48511. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  48512. #ifndef NO_WOLFSSL_CLIENT
  48513. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  48514. #endif
  48515. #ifndef NO_WOLFSSL_SERVER
  48516. #ifndef WOLFSSL_NO_TLS12
  48517. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  48518. #endif
  48519. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  48520. #endif
  48521. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  48522. #ifndef NO_WOLFSSL_CLIENT
  48523. #ifndef WOLFSSL_NO_TLS12
  48524. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  48525. #endif
  48526. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  48527. #endif
  48528. #ifndef NO_WOLFSSL_SERVER
  48529. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  48530. #endif
  48531. ExpectIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  48532. #ifndef NO_WOLFSSL_CLIENT
  48533. #ifndef WOLFSSL_NO_TLS12
  48534. ExpectIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  48535. #endif
  48536. ExpectIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  48537. #endif
  48538. #ifndef NO_WOLFSSL_SERVER
  48539. ExpectIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  48540. #endif
  48541. ExpectIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  48542. #ifndef NO_WOLFSSL_CLIENT
  48543. #ifndef WOLFSSL_NO_TLS12
  48544. ExpectIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  48545. #endif
  48546. ExpectIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  48547. #endif
  48548. #ifndef NO_WOLFSSL_SERVER
  48549. ExpectIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  48550. #endif
  48551. ExpectIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  48552. ExpectIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  48553. #ifndef NO_WOLFSSL_CLIENT
  48554. #ifndef WOLFSSL_NO_TLS12
  48555. ExpectIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  48556. BAD_FUNC_ARG);
  48557. #endif
  48558. ExpectIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  48559. #endif
  48560. #ifndef NO_WOLFSSL_SERVER
  48561. ExpectIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  48562. #endif
  48563. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  48564. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  48565. #ifndef NO_WOLFSSL_SERVER
  48566. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  48567. #endif
  48568. #ifndef NO_WOLFSSL_CLIENT
  48569. #ifndef WOLFSSL_NO_TLS12
  48570. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  48571. BAD_FUNC_ARG);
  48572. #endif
  48573. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  48574. #endif
  48575. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  48576. #ifndef NO_WOLFSSL_SERVER
  48577. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  48578. #endif
  48579. #ifndef NO_WOLFSSL_CLIENT
  48580. #ifndef WOLFSSL_NO_TLS12
  48581. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  48582. BAD_FUNC_ARG);
  48583. #endif
  48584. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  48585. #endif
  48586. ExpectIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  48587. #ifndef NO_WOLFSSL_CLIENT
  48588. ExpectIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  48589. #endif
  48590. #ifndef NO_WOLFSSL_SERVER
  48591. #ifndef WOLFSSL_NO_TLS12
  48592. ExpectIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  48593. BAD_FUNC_ARG);
  48594. #endif
  48595. ExpectIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  48596. #endif
  48597. #endif
  48598. #ifdef HAVE_ECC
  48599. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  48600. ExpectIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  48601. #ifndef NO_WOLFSSL_SERVER
  48602. ExpectIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  48603. #endif
  48604. #ifndef NO_WOLFSSL_CLIENT
  48605. #ifndef WOLFSSL_NO_TLS12
  48606. ExpectIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  48607. #endif
  48608. ExpectIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  48609. #endif
  48610. #endif
  48611. #ifdef HAVE_SUPPORTED_CURVES
  48612. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  48613. #ifndef NO_WOLFSSL_CLIENT
  48614. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  48615. #endif
  48616. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  48617. #ifndef NO_WOLFSSL_CLIENT
  48618. #ifndef WOLFSSL_NO_TLS12
  48619. ExpectIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  48620. BAD_FUNC_ARG);
  48621. #endif
  48622. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  48623. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  48624. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  48625. WOLFSSL_SUCCESS);
  48626. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  48627. BAD_FUNC_ARG);
  48628. #endif
  48629. #ifndef NO_WOLFSSL_SERVER
  48630. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  48631. WOLFSSL_SUCCESS);
  48632. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  48633. BAD_FUNC_ARG);
  48634. #endif
  48635. ExpectIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  48636. #ifndef NO_WOLFSSL_CLIENT
  48637. ExpectIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  48638. #endif
  48639. ExpectIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  48640. #ifndef NO_WOLFSSL_CLIENT
  48641. #ifndef WOLFSSL_NO_TLS12
  48642. ExpectIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  48643. BAD_FUNC_ARG);
  48644. #endif
  48645. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups,
  48646. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  48647. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  48648. WOLFSSL_SUCCESS);
  48649. ExpectIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  48650. BAD_FUNC_ARG);
  48651. #endif
  48652. #ifndef NO_WOLFSSL_SERVER
  48653. ExpectIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  48654. WOLFSSL_SUCCESS);
  48655. ExpectIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  48656. BAD_FUNC_ARG);
  48657. #endif
  48658. #ifdef OPENSSL_EXTRA
  48659. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  48660. #ifndef NO_WOLFSSL_CLIENT
  48661. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL),
  48662. WOLFSSL_FAILURE);
  48663. #endif
  48664. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList),
  48665. WOLFSSL_FAILURE);
  48666. #ifndef NO_WOLFSSL_CLIENT
  48667. #ifndef WOLFSSL_NO_TLS12
  48668. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  48669. WOLFSSL_FAILURE);
  48670. #endif
  48671. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  48672. WOLFSSL_SUCCESS);
  48673. #endif
  48674. #ifndef NO_WOLFSSL_SERVER
  48675. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  48676. WOLFSSL_SUCCESS);
  48677. #endif
  48678. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  48679. #ifndef NO_WOLFSSL_CLIENT
  48680. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  48681. #endif
  48682. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  48683. #ifndef NO_WOLFSSL_CLIENT
  48684. #ifndef WOLFSSL_NO_TLS12
  48685. ExpectIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  48686. WOLFSSL_FAILURE);
  48687. #endif
  48688. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  48689. WOLFSSL_SUCCESS);
  48690. #endif
  48691. #ifndef NO_WOLFSSL_SERVER
  48692. ExpectIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  48693. WOLFSSL_SUCCESS);
  48694. #endif
  48695. #endif /* OPENSSL_EXTRA */
  48696. #endif /* HAVE_SUPPORTED_CURVES */
  48697. #endif /* HAVE_ECC */
  48698. #ifdef WOLFSSL_EARLY_DATA
  48699. #ifndef OPENSSL_EXTRA
  48700. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  48701. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  48702. #else
  48703. ExpectIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  48704. ExpectIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  48705. #endif
  48706. #ifndef NO_WOLFSSL_CLIENT
  48707. #ifndef OPENSSL_EXTRA
  48708. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  48709. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  48710. #else
  48711. ExpectIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  48712. ExpectIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  48713. #endif
  48714. #endif
  48715. #ifndef NO_WOLFSSL_SERVER
  48716. #ifndef WOLFSSL_NO_TLS12
  48717. #ifndef OPENSSL_EXTRA
  48718. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  48719. BAD_FUNC_ARG);
  48720. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  48721. #else
  48722. ExpectIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  48723. BAD_FUNC_ARG);
  48724. ExpectIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  48725. #endif
  48726. #endif
  48727. #ifndef OPENSSL_EXTRA
  48728. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  48729. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32),
  48730. WOLFSSL_SUCCESS);
  48731. #else
  48732. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  48733. #endif
  48734. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  48735. #else
  48736. ExpectIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  48737. ExpectIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  48738. #endif
  48739. #endif
  48740. #ifndef OPENSSL_EXTRA
  48741. ExpectIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  48742. ExpectIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  48743. #else
  48744. ExpectIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  48745. ExpectIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  48746. #endif
  48747. #ifndef NO_WOLFSSL_CLIENT
  48748. #ifndef OPENSSL_EXTRA
  48749. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  48750. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  48751. #else
  48752. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
  48753. #endif
  48754. ExpectIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
  48755. #else
  48756. ExpectIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  48757. ExpectIntEQ(SSL_get_max_early_data(clientSsl), 17);
  48758. #endif
  48759. #endif
  48760. #ifndef NO_WOLFSSL_SERVER
  48761. #ifndef WOLFSSL_NO_TLS12
  48762. #ifndef OPENSSL_EXTRA
  48763. ExpectIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  48764. ExpectIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  48765. #else
  48766. ExpectIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  48767. ExpectIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  48768. #endif
  48769. #endif
  48770. #ifndef OPENSSL_EXTRA
  48771. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  48772. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), WOLFSSL_SUCCESS);
  48773. #else
  48774. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  48775. #endif
  48776. ExpectIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  48777. #else
  48778. ExpectIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  48779. ExpectIntEQ(SSL_get_max_early_data(serverSsl), 16);
  48780. #endif
  48781. #endif
  48782. ExpectIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  48783. &outSz), BAD_FUNC_ARG);
  48784. #ifndef NO_WOLFSSL_CLIENT
  48785. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  48786. &outSz), BAD_FUNC_ARG);
  48787. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  48788. BAD_FUNC_ARG);
  48789. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  48790. sizeof(earlyData), NULL), BAD_FUNC_ARG);
  48791. #endif
  48792. #ifndef NO_WOLFSSL_SERVER
  48793. ExpectIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  48794. sizeof(earlyData), &outSz), SIDE_ERROR);
  48795. #endif
  48796. #ifndef NO_WOLFSSL_CLIENT
  48797. #ifndef WOLFSSL_NO_TLS12
  48798. ExpectIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  48799. sizeof(earlyData), &outSz), BAD_FUNC_ARG);
  48800. #endif
  48801. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  48802. sizeof(earlyData), &outSz), WOLFSSL_FATAL_ERROR);
  48803. #endif
  48804. ExpectIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  48805. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  48806. #ifndef NO_WOLFSSL_SERVER
  48807. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  48808. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  48809. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1,
  48810. &outSz), BAD_FUNC_ARG);
  48811. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  48812. sizeof(earlyDataBuffer), NULL), BAD_FUNC_ARG);
  48813. #endif
  48814. #ifndef NO_WOLFSSL_CLIENT
  48815. ExpectIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  48816. sizeof(earlyDataBuffer), &outSz), SIDE_ERROR);
  48817. #endif
  48818. #ifndef NO_WOLFSSL_SERVER
  48819. #ifndef WOLFSSL_NO_TLS12
  48820. ExpectIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  48821. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  48822. #endif
  48823. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  48824. sizeof(earlyDataBuffer), &outSz), WOLFSSL_FATAL_ERROR);
  48825. #endif
  48826. #endif
  48827. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  48828. ExpectIntLT(SSL_get_early_data_status(NULL), 0);
  48829. #endif
  48830. #ifndef NO_WOLFSSL_SERVER
  48831. wolfSSL_free(serverSsl);
  48832. wolfSSL_CTX_free(serverCtx);
  48833. #endif
  48834. #ifndef NO_WOLFSSL_CLIENT
  48835. wolfSSL_free(clientSsl);
  48836. wolfSSL_CTX_free(clientCtx);
  48837. #endif
  48838. #ifndef WOLFSSL_NO_TLS12
  48839. #ifndef NO_WOLFSSL_SERVER
  48840. wolfSSL_free(serverTls12Ssl);
  48841. wolfSSL_CTX_free(serverTls12Ctx);
  48842. #endif
  48843. #ifndef NO_WOLFSSL_CLIENT
  48844. wolfSSL_free(clientTls12Ssl);
  48845. wolfSSL_CTX_free(clientTls12Ctx);
  48846. #endif
  48847. #endif
  48848. return EXPECT_RESULT();
  48849. }
  48850. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  48851. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  48852. defined(BUILD_TLS_AES_256_GCM_SHA384)
  48853. /* Called when writing. */
  48854. static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  48855. {
  48856. (void)ssl;
  48857. (void)buf;
  48858. (void)sz;
  48859. (void)ctx;
  48860. /* Force error return from wolfSSL_accept_TLSv13(). */
  48861. return WANT_WRITE;
  48862. }
  48863. /* Called when reading. */
  48864. static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  48865. {
  48866. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  48867. int len = (int)msg->length;
  48868. (void)ssl;
  48869. (void)sz;
  48870. /* Pass back as much of message as will fit in buffer. */
  48871. if (len > sz)
  48872. len = sz;
  48873. XMEMCPY(buf, msg->buffer, len);
  48874. /* Move over returned data. */
  48875. msg->buffer += len;
  48876. msg->length -= len;
  48877. /* Amount actually copied. */
  48878. return len;
  48879. }
  48880. #endif
  48881. static int test_tls13_cipher_suites(void)
  48882. {
  48883. EXPECT_DECLS;
  48884. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  48885. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  48886. defined(BUILD_TLS_AES_256_GCM_SHA384)
  48887. WOLFSSL_CTX* ctx = NULL;
  48888. WOLFSSL *ssl = NULL;
  48889. int i;
  48890. byte clientHello[] = {
  48891. 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
  48892. 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  48893. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  48894. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  48895. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  48896. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  48897. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  48898. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  48899. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48900. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  48901. /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
  48902. 0x13, 0x01,
  48903. 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
  48904. 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
  48905. 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
  48906. 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
  48907. 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
  48908. 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
  48909. 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
  48910. 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
  48911. 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
  48912. 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
  48913. 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
  48914. 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
  48915. 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
  48916. 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
  48917. 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
  48918. 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
  48919. 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
  48920. 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
  48921. 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
  48922. 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
  48923. 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
  48924. 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
  48925. 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
  48926. 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
  48927. 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
  48928. 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
  48929. 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
  48930. 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
  48931. 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
  48932. 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
  48933. 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
  48934. 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
  48935. 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
  48936. 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
  48937. 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
  48938. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48939. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  48940. 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
  48941. 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
  48942. 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
  48943. 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
  48944. 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
  48945. };
  48946. WOLFSSL_BUFFER_INFO msg;
  48947. /* Offset into ClientHello message data of first cipher suite. */
  48948. const int csOff = 78;
  48949. /* Server cipher list. */
  48950. const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
  48951. /* Suite list with duplicates. */
  48952. const char* dupCs = "TLS13-AES128-GCM-SHA256:"
  48953. "TLS13-AES128-GCM-SHA256:"
  48954. "TLS13-AES256-GCM-SHA384:"
  48955. "TLS13-AES256-GCM-SHA384:"
  48956. "TLS13-AES128-GCM-SHA256";
  48957. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  48958. const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  48959. TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  48960. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  48961. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  48962. TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
  48963. #endif
  48964. /* Set up wolfSSL context. */
  48965. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  48966. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  48967. WOLFSSL_FILETYPE_PEM));
  48968. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  48969. WOLFSSL_FILETYPE_PEM));
  48970. /* Read from 'msg'. */
  48971. wolfSSL_SetIORecv(ctx, CsRecv);
  48972. /* No where to send to - dummy sender. */
  48973. wolfSSL_SetIOSend(ctx, CsSend);
  48974. /* Test cipher suite list with many copies of a cipher suite. */
  48975. ExpectNotNull(ssl = wolfSSL_new(ctx));
  48976. msg.buffer = clientHello;
  48977. msg.length = (unsigned int)sizeof(clientHello);
  48978. wolfSSL_SetIOReadCtx(ssl, &msg);
  48979. /* Force server to have as many occurrences of same cipher suite as
  48980. * possible. */
  48981. if (ssl != NULL) {
  48982. Suites* suites = (Suites*)WOLFSSL_SUITES(ssl);
  48983. suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  48984. for (i = 0; i < suites->suiteSz; i += 2) {
  48985. suites->suites[i + 0] = TLS13_BYTE;
  48986. suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
  48987. }
  48988. }
  48989. /* Test multiple occurrences of same cipher suite. */
  48990. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  48991. wolfSSL_free(ssl);
  48992. ssl = NULL;
  48993. /* Set client order opposite to server order:
  48994. * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
  48995. clientHello[csOff + 0] = TLS13_BYTE;
  48996. clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
  48997. clientHello[csOff + 2] = TLS13_BYTE;
  48998. clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
  48999. /* Test server order negotiation. */
  49000. ExpectNotNull(ssl = wolfSSL_new(ctx));
  49001. msg.buffer = clientHello;
  49002. msg.length = (unsigned int)sizeof(clientHello);
  49003. wolfSSL_SetIOReadCtx(ssl, &msg);
  49004. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  49005. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  49006. /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
  49007. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  49008. /* Check refined order - server order. */
  49009. ExpectIntEQ(ssl->suites->suiteSz, 4);
  49010. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  49011. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
  49012. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  49013. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
  49014. wolfSSL_free(ssl);
  49015. ssl = NULL;
  49016. /* Test client order negotiation. */
  49017. ExpectNotNull(ssl = wolfSSL_new(ctx));
  49018. msg.buffer = clientHello;
  49019. msg.length = (unsigned int)sizeof(clientHello);
  49020. wolfSSL_SetIOReadCtx(ssl, &msg);
  49021. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  49022. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  49023. ExpectIntEQ(wolfSSL_UseClientSuites(ssl), 0);
  49024. /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
  49025. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  49026. /* Check refined order - client order. */
  49027. ExpectIntEQ(ssl->suites->suiteSz, 4);
  49028. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  49029. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
  49030. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  49031. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
  49032. wolfSSL_free(ssl);
  49033. ssl = NULL;
  49034. /* Check duplicate detection is working. */
  49035. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
  49036. ExpectIntEQ(ctx->suites->suiteSz, 4);
  49037. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  49038. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
  49039. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  49040. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
  49041. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  49042. ExpectIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
  49043. sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
  49044. ExpectIntEQ(ctx->suites->suiteSz, 4);
  49045. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  49046. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
  49047. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  49048. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
  49049. #endif
  49050. wolfSSL_CTX_free(ctx);
  49051. #endif
  49052. return EXPECT_RESULT();
  49053. }
  49054. #endif
  49055. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  49056. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  49057. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  49058. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  49059. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  49060. const unsigned char* priv, unsigned int privSz,
  49061. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  49062. unsigned char* out, unsigned int* outlen,
  49063. void* ctx)
  49064. {
  49065. int result;
  49066. /* Test fail when context associated with WOLFSSL is NULL */
  49067. if (ctx == NULL) {
  49068. return -1;
  49069. }
  49070. (void)ssl;
  49071. /* return 0 on success */
  49072. PRIVATE_KEY_UNLOCK();
  49073. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  49074. PRIVATE_KEY_LOCK();
  49075. return result;
  49076. }
  49077. static int test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  49078. EXPECT_DECLS;
  49079. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  49080. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  49081. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  49082. WOLFSSL_SUCCESS);
  49083. #endif
  49084. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  49085. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  49086. WOLFSSL_SUCCESS);
  49087. #endif
  49088. return EXPECT_RESULT();
  49089. }
  49090. static int test_dh_ssl_setup(WOLFSSL* ssl)
  49091. {
  49092. EXPECT_DECLS;
  49093. static int dh_test_ctx = 1;
  49094. int ret;
  49095. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  49096. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  49097. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  49098. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  49099. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  49100. }
  49101. return EXPECT_RESULT();
  49102. }
  49103. static int test_dh_ssl_setup_fail(WOLFSSL* ssl)
  49104. {
  49105. EXPECT_DECLS;
  49106. int ret;
  49107. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  49108. ExpectNull(wolfSSL_GetDhAgreeCtx(ssl));
  49109. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  49110. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  49111. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  49112. }
  49113. return EXPECT_RESULT();
  49114. }
  49115. #endif
  49116. static int test_DhCallbacks(void)
  49117. {
  49118. EXPECT_DECLS;
  49119. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  49120. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  49121. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  49122. WOLFSSL_CTX *ctx = NULL;
  49123. WOLFSSL *ssl = NULL;
  49124. int test;
  49125. test_ssl_cbf func_cb_client;
  49126. test_ssl_cbf func_cb_server;
  49127. /* Test that DH callback APIs work. */
  49128. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  49129. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  49130. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  49131. /* load client ca cert */
  49132. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  49133. WOLFSSL_SUCCESS);
  49134. /* test with NULL arguments */
  49135. wolfSSL_SetDhAgreeCtx(NULL, &test);
  49136. ExpectNull(wolfSSL_GetDhAgreeCtx(NULL));
  49137. /* test success case */
  49138. test = 1;
  49139. ExpectNotNull(ssl = wolfSSL_new(ctx));
  49140. wolfSSL_SetDhAgreeCtx(ssl, &test);
  49141. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  49142. wolfSSL_free(ssl);
  49143. wolfSSL_CTX_free(ctx);
  49144. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  49145. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  49146. /* set callbacks to use DH functions */
  49147. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  49148. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  49149. func_cb_client.method = wolfTLSv1_2_client_method;
  49150. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  49151. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  49152. func_cb_server.method = wolfTLSv1_2_server_method;
  49153. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  49154. &func_cb_server, NULL), TEST_SUCCESS);
  49155. /* Test fail */
  49156. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  49157. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  49158. /* set callbacks to use DH functions */
  49159. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  49160. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  49161. func_cb_client.method = wolfTLSv1_2_client_method;
  49162. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  49163. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  49164. func_cb_server.method = wolfTLSv1_2_server_method;
  49165. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  49166. &func_cb_server, NULL), TEST_FAIL);
  49167. #endif
  49168. return EXPECT_RESULT();
  49169. }
  49170. #endif /* HAVE_PK_CALLBACKS */
  49171. #ifdef HAVE_HASHDRBG
  49172. #ifdef TEST_RESEED_INTERVAL
  49173. static int test_wc_RNG_GenerateBlock_Reseed(void)
  49174. {
  49175. EXPECT_DECLS;
  49176. int i;
  49177. WC_RNG rng;
  49178. byte key[32];
  49179. XMEMSET(&rng, 0, sizeof(WC_RNG));
  49180. ExpectIntEQ(wc_InitRng(&rng), 0);
  49181. for (i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  49182. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  49183. }
  49184. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  49185. return EXPECT_RESULT();
  49186. }
  49187. #endif /* TEST_RESEED_INTERVAL */
  49188. static int test_wc_RNG_GenerateBlock(void)
  49189. {
  49190. EXPECT_DECLS;
  49191. int i;
  49192. WC_RNG rng;
  49193. byte key[32];
  49194. XMEMSET(&rng, 0, sizeof(WC_RNG));
  49195. ExpectIntEQ(wc_InitRng(&rng), 0);
  49196. for (i = 0; i < 10; i++) {
  49197. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  49198. }
  49199. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  49200. return EXPECT_RESULT();
  49201. }
  49202. #endif /* HAVE_HASHDRBG */
  49203. /*
  49204. * Testing get_rand_digit
  49205. */
  49206. static int test_get_rand_digit(void)
  49207. {
  49208. EXPECT_DECLS;
  49209. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  49210. WC_RNG rng;
  49211. mp_digit d;
  49212. XMEMSET(&rng, 0, sizeof(WC_RNG));
  49213. ExpectIntEQ(wc_InitRng(&rng), 0);
  49214. ExpectIntEQ(get_rand_digit(&rng, &d), 0);
  49215. ExpectIntEQ(get_rand_digit(NULL, NULL), BAD_FUNC_ARG);
  49216. ExpectIntEQ(get_rand_digit(NULL, &d), BAD_FUNC_ARG);
  49217. ExpectIntEQ(get_rand_digit(&rng, NULL), BAD_FUNC_ARG);
  49218. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  49219. #endif
  49220. return EXPECT_RESULT();
  49221. } /* End test_get_rand_digit*/
  49222. /*
  49223. * Testing get_digit_count
  49224. */
  49225. static int test_get_digit_count(void)
  49226. {
  49227. EXPECT_DECLS;
  49228. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  49229. mp_int a;
  49230. XMEMSET(&a, 0, sizeof(mp_int));
  49231. ExpectIntEQ(mp_init(&a), 0);
  49232. ExpectIntEQ(get_digit_count(NULL), 0);
  49233. ExpectIntEQ(get_digit_count(&a), 0);
  49234. mp_clear(&a);
  49235. #endif
  49236. return EXPECT_RESULT();
  49237. } /* End test_get_digit_count*/
  49238. /*
  49239. * Testing mp_cond_copy
  49240. */
  49241. static int test_mp_cond_copy(void)
  49242. {
  49243. EXPECT_DECLS;
  49244. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  49245. defined(WOLFSSL_PUBLIC_MP)
  49246. mp_int a;
  49247. mp_int b;
  49248. int copy = 0;
  49249. XMEMSET(&a, 0, sizeof(mp_int));
  49250. XMEMSET(&b, 0, sizeof(mp_int));
  49251. ExpectIntEQ(mp_init(&a), MP_OKAY);
  49252. ExpectIntEQ(mp_init(&b), MP_OKAY);
  49253. ExpectIntEQ(mp_cond_copy(NULL, copy, NULL), BAD_FUNC_ARG);
  49254. ExpectIntEQ(mp_cond_copy(NULL, copy, &b), BAD_FUNC_ARG);
  49255. ExpectIntEQ(mp_cond_copy(&a, copy, NULL), BAD_FUNC_ARG);
  49256. ExpectIntEQ(mp_cond_copy(&a, copy, &b), 0);
  49257. mp_clear(&a);
  49258. mp_clear(&b);
  49259. #endif
  49260. return EXPECT_RESULT();
  49261. } /* End test_mp_cond_copy*/
  49262. /*
  49263. * Testing mp_rand
  49264. */
  49265. static int test_mp_rand(void)
  49266. {
  49267. EXPECT_DECLS;
  49268. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  49269. mp_int a;
  49270. WC_RNG rng;
  49271. int digits = 1;
  49272. XMEMSET(&a, 0, sizeof(mp_int));
  49273. XMEMSET(&rng, 0, sizeof(WC_RNG));
  49274. ExpectIntEQ(mp_init(&a), MP_OKAY);
  49275. ExpectIntEQ(wc_InitRng(&rng), 0);
  49276. ExpectIntEQ(mp_rand(&a, digits, NULL), MISSING_RNG_E);
  49277. ExpectIntEQ(mp_rand(NULL, digits, &rng), BAD_FUNC_ARG);
  49278. ExpectIntEQ(mp_rand(&a, 0, &rng), BAD_FUNC_ARG);
  49279. ExpectIntEQ(mp_rand(&a, digits, &rng), 0);
  49280. mp_clear(&a);
  49281. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  49282. #endif
  49283. return EXPECT_RESULT();
  49284. } /* End test_mp_rand*/
  49285. /*
  49286. * Testing get_digit
  49287. */
  49288. static int test_get_digit(void)
  49289. {
  49290. EXPECT_DECLS;
  49291. #if defined(WOLFSSL_PUBLIC_MP)
  49292. mp_int a;
  49293. int n = 0;
  49294. XMEMSET(&a, 0, sizeof(mp_int));
  49295. ExpectIntEQ(mp_init(&a), MP_OKAY);
  49296. ExpectIntEQ(get_digit(NULL, n), 0);
  49297. ExpectIntEQ(get_digit(&a, n), 0);
  49298. mp_clear(&a);
  49299. #endif
  49300. return EXPECT_RESULT();
  49301. } /* End test_get_digit*/
  49302. /*
  49303. * Testing wc_export_int
  49304. */
  49305. static int test_wc_export_int(void)
  49306. {
  49307. EXPECT_DECLS;
  49308. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  49309. defined(WOLFSSL_PUBLIC_MP)
  49310. mp_int mp;
  49311. byte buf[32];
  49312. word32 keySz = (word32)sizeof(buf);
  49313. word32 len = (word32)sizeof(buf);
  49314. XMEMSET(&mp, 0, sizeof(mp_int));
  49315. ExpectIntEQ(mp_init(&mp), MP_OKAY);
  49316. ExpectIntEQ(mp_set(&mp, 1234), 0);
  49317. ExpectIntEQ(wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  49318. BAD_FUNC_ARG);
  49319. len = sizeof(buf)-1;
  49320. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  49321. BUFFER_E);
  49322. len = sizeof(buf);
  49323. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN), 0);
  49324. len = 4; /* test input too small */
  49325. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), BUFFER_E);
  49326. len = sizeof(buf);
  49327. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), 0);
  49328. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  49329. ExpectIntEQ(len, 5);
  49330. mp_clear(&mp);
  49331. #endif
  49332. return EXPECT_RESULT();
  49333. } /* End test_wc_export_int*/
  49334. static int test_wc_InitRngNonce(void)
  49335. {
  49336. EXPECT_DECLS;
  49337. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  49338. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  49339. HAVE_FIPS_VERSION >= 2))
  49340. WC_RNG rng;
  49341. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  49342. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  49343. word32 nonceSz = sizeof(nonce);
  49344. ExpectIntEQ(wc_InitRngNonce(&rng, nonce, nonceSz), 0);
  49345. ExpectIntEQ(wc_FreeRng(&rng), 0);
  49346. #endif
  49347. return EXPECT_RESULT();
  49348. } /* End test_wc_InitRngNonce*/
  49349. /*
  49350. * Testing wc_InitRngNonce_ex
  49351. */
  49352. static int test_wc_InitRngNonce_ex(void)
  49353. {
  49354. EXPECT_DECLS;
  49355. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  49356. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  49357. HAVE_FIPS_VERSION >= 2))
  49358. WC_RNG rng;
  49359. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  49360. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  49361. word32 nonceSz = sizeof(nonce);
  49362. ExpectIntEQ(wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId),
  49363. 0);
  49364. ExpectIntEQ(wc_FreeRng(&rng), 0);
  49365. #endif
  49366. return EXPECT_RESULT();
  49367. } /* End test_wc_InitRngNonce_ex */
  49368. static int test_wolfSSL_X509_CRL(void)
  49369. {
  49370. EXPECT_DECLS;
  49371. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  49372. X509_CRL *crl = NULL;
  49373. char pem[][100] = {
  49374. "./certs/crl/crl.pem",
  49375. "./certs/crl/crl2.pem",
  49376. "./certs/crl/caEccCrl.pem",
  49377. "./certs/crl/eccCliCRL.pem",
  49378. "./certs/crl/eccSrvCRL.pem",
  49379. ""
  49380. };
  49381. #ifndef NO_BIO
  49382. BIO *bio = NULL;
  49383. #endif
  49384. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  49385. char der[][100] = {
  49386. "./certs/crl/crl.der",
  49387. "./certs/crl/crl2.der",
  49388. ""};
  49389. #endif
  49390. XFILE fp = XBADFILE;
  49391. int i;
  49392. for (i = 0; pem[i][0] != '\0'; i++)
  49393. {
  49394. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  49395. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  49396. NULL, NULL));
  49397. ExpectNotNull(crl);
  49398. X509_CRL_free(crl);
  49399. if (fp != XBADFILE) {
  49400. XFCLOSE(fp);
  49401. fp = XBADFILE;
  49402. }
  49403. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  49404. ExpectNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL,
  49405. NULL));
  49406. if (EXPECT_FAIL()) {
  49407. crl = NULL;
  49408. }
  49409. ExpectNotNull(crl);
  49410. X509_CRL_free(crl);
  49411. crl = NULL;
  49412. if (fp != XBADFILE) {
  49413. XFCLOSE(fp);
  49414. fp = XBADFILE;
  49415. }
  49416. }
  49417. #ifndef NO_BIO
  49418. for (i = 0; pem[i][0] != '\0'; i++)
  49419. {
  49420. ExpectNotNull(bio = BIO_new_file(pem[i], "rb"));
  49421. ExpectNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  49422. X509_CRL_free(crl);
  49423. crl = NULL;
  49424. BIO_free(bio);
  49425. bio = NULL;
  49426. }
  49427. #endif
  49428. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  49429. for (i = 0; der[i][0] != '\0'; i++) {
  49430. ExpectTrue((fp = XFOPEN(der[i], "rb")) != XBADFILE);
  49431. ExpectTrue((fp != XBADFILE));
  49432. ExpectNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  49433. ExpectNotNull(crl);
  49434. X509_CRL_free(crl);
  49435. if (fp != XBADFILE) {
  49436. XFCLOSE(fp);
  49437. fp = XBADFILE;
  49438. }
  49439. fp = XFOPEN(der[i], "rb");
  49440. ExpectTrue((fp != XBADFILE));
  49441. ExpectNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  49442. if (EXPECT_FAIL()) {
  49443. crl = NULL;
  49444. }
  49445. ExpectNotNull(crl);
  49446. X509_CRL_free(crl);
  49447. crl = NULL;
  49448. if (fp != XBADFILE) {
  49449. XFCLOSE(fp);
  49450. fp = XBADFILE;
  49451. }
  49452. }
  49453. #endif
  49454. #endif
  49455. return EXPECT_RESULT();
  49456. }
  49457. static int test_wolfSSL_X509_load_crl_file(void)
  49458. {
  49459. EXPECT_DECLS;
  49460. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  49461. !defined(NO_STDIO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  49462. int i;
  49463. char pem[][100] = {
  49464. "./certs/crl/crl.pem",
  49465. "./certs/crl/crl2.pem",
  49466. "./certs/crl/caEccCrl.pem",
  49467. "./certs/crl/eccCliCRL.pem",
  49468. "./certs/crl/eccSrvCRL.pem",
  49469. #ifdef WC_RSA_PSS
  49470. "./certs/crl/crl_rsapss.pem",
  49471. #endif
  49472. ""
  49473. };
  49474. char der[][100] = {
  49475. "./certs/crl/crl.der",
  49476. "./certs/crl/crl2.der",
  49477. ""
  49478. };
  49479. WOLFSSL_X509_STORE* store = NULL;
  49480. WOLFSSL_X509_LOOKUP* lookup = NULL;
  49481. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  49482. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  49483. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  49484. X509_FILETYPE_PEM), 1);
  49485. #ifdef WC_RSA_PSS
  49486. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/rsapss/ca-rsapss.pem",
  49487. X509_FILETYPE_PEM), 1);
  49488. #endif
  49489. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  49490. X509_FILETYPE_PEM), 1);
  49491. if (store) {
  49492. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  49493. WOLFSSL_FILETYPE_PEM), 1);
  49494. /* since store hasn't yet known the revoked cert*/
  49495. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  49496. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  49497. }
  49498. for (i = 0; pem[i][0] != '\0'; i++) {
  49499. ExpectIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM),
  49500. 1);
  49501. }
  49502. if (store) {
  49503. /* since store knows crl list */
  49504. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  49505. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  49506. CRL_CERT_REVOKED);
  49507. #ifdef WC_RSA_PSS
  49508. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  49509. "certs/rsapss/server-rsapss-cert.pem", WOLFSSL_FILETYPE_PEM),
  49510. CRL_CERT_REVOKED);
  49511. #endif
  49512. }
  49513. /* once feeing store */
  49514. X509_STORE_free(store);
  49515. store = NULL;
  49516. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  49517. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  49518. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  49519. X509_FILETYPE_PEM), 1);
  49520. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  49521. X509_FILETYPE_PEM), 1);
  49522. if (store) {
  49523. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  49524. WOLFSSL_FILETYPE_PEM), 1);
  49525. /* since store hasn't yet known the revoked cert*/
  49526. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  49527. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  49528. }
  49529. for (i = 0; der[i][0] != '\0'; i++) {
  49530. ExpectIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1),
  49531. 1);
  49532. }
  49533. if (store) {
  49534. /* since store knows crl list */
  49535. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  49536. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  49537. CRL_CERT_REVOKED);
  49538. }
  49539. /* test for incorrect parameter */
  49540. ExpectIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  49541. ExpectIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  49542. ExpectIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  49543. X509_STORE_free(store);
  49544. store = NULL;
  49545. #endif
  49546. return EXPECT_RESULT();
  49547. }
  49548. static int test_wolfSSL_i2d_X509(void)
  49549. {
  49550. EXPECT_DECLS;
  49551. #if defined(OPENSSL_EXTRA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  49552. const unsigned char* cert_buf = server_cert_der_2048;
  49553. unsigned char* out = NULL;
  49554. unsigned char* tmp = NULL;
  49555. X509* cert = NULL;
  49556. ExpectNotNull(d2i_X509(&cert, &cert_buf, sizeof_server_cert_der_2048));
  49557. /* Pointer should be advanced */
  49558. ExpectPtrGT(cert_buf, server_cert_der_2048);
  49559. ExpectIntGT(i2d_X509(cert, &out), 0);
  49560. ExpectNotNull(out);
  49561. tmp = out;
  49562. ExpectIntGT(i2d_X509(cert, &tmp), 0);
  49563. ExpectPtrGT(tmp, out);
  49564. if (out != NULL)
  49565. XFREE(out, NULL, DYNAMIC_TYPE_OPENSSL);
  49566. X509_free(cert);
  49567. #endif
  49568. return EXPECT_RESULT();
  49569. }
  49570. static int test_wolfSSL_d2i_X509_REQ(void)
  49571. {
  49572. EXPECT_DECLS;
  49573. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  49574. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  49575. !defined(WOLFSSL_SP_MATH)
  49576. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der
  49577. * were generated by libest
  49578. * ./certs/csr.attr.der contains sample attributes
  49579. * ./certs/csr.ext.der contains sample extensions */
  49580. const char* csrFile = "./certs/csr.signed.der";
  49581. const char* csrPopFile = "./certs/csr.attr.der";
  49582. const char* csrExtFile = "./certs/csr.ext.der";
  49583. /* ./certs/csr.dsa.pem is generated using
  49584. * openssl req -newkey dsa:certs/dsaparams.pem \
  49585. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  49586. * -outform PEM
  49587. * with the passphrase "wolfSSL"
  49588. */
  49589. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  49590. const char* csrDsaFile = "./certs/csr.dsa.pem";
  49591. XFILE f = XBADFILE;
  49592. #endif
  49593. BIO* bio = NULL;
  49594. X509* req = NULL;
  49595. EVP_PKEY *pub_key = NULL;
  49596. {
  49597. ExpectNotNull(bio = BIO_new_file(csrFile, "rb"));
  49598. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  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. bio = NULL;
  49611. EVP_PKEY_free(pub_key);
  49612. pub_key = NULL;
  49613. }
  49614. {
  49615. #ifdef OPENSSL_ALL
  49616. X509_ATTRIBUTE* attr = NULL;
  49617. ASN1_TYPE *at = NULL;
  49618. #endif
  49619. ExpectNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  49620. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  49621. /*
  49622. * Extract the public key from the CSR
  49623. */
  49624. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  49625. /*
  49626. * Verify the signature in the CSR
  49627. */
  49628. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  49629. #ifdef OPENSSL_ALL
  49630. /*
  49631. * Obtain the challenge password from the CSR
  49632. */
  49633. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  49634. -1), 1);
  49635. ExpectNotNull(attr = X509_REQ_get_attr(req, 1));
  49636. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  49637. ExpectNotNull(at->value.asn1_string);
  49638. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string),
  49639. "2xIE+qqp/rhyTXP+");
  49640. ExpectIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  49641. #endif
  49642. X509_free(req);
  49643. req = NULL;
  49644. BIO_free(bio);
  49645. bio = NULL;
  49646. EVP_PKEY_free(pub_key);
  49647. pub_key = NULL;
  49648. }
  49649. {
  49650. #ifdef OPENSSL_ALL
  49651. X509_ATTRIBUTE* attr = NULL;
  49652. ASN1_TYPE *at = NULL;
  49653. STACK_OF(X509_EXTENSION) *exts = NULL;
  49654. #endif
  49655. ExpectNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  49656. /* This CSR contains an Extension Request attribute so
  49657. * we test extension parsing in a CSR attribute here. */
  49658. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  49659. /*
  49660. * Extract the public key from the CSR
  49661. */
  49662. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  49663. /*
  49664. * Verify the signature in the CSR
  49665. */
  49666. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  49667. #ifdef OPENSSL_ALL
  49668. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  49669. req));
  49670. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  49671. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  49672. /*
  49673. * Obtain the challenge password from the CSR
  49674. */
  49675. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  49676. -1), 0);
  49677. ExpectNotNull(attr = X509_REQ_get_attr(req, 0));
  49678. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  49679. ExpectNotNull(at->value.asn1_string);
  49680. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  49681. ExpectIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  49682. ExpectIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  49683. #endif
  49684. X509_free(req);
  49685. req = NULL;
  49686. BIO_free(bio);
  49687. bio = NULL;
  49688. EVP_PKEY_free(pub_key);
  49689. pub_key = NULL;
  49690. }
  49691. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  49692. {
  49693. ExpectNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  49694. ExpectNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  49695. /*
  49696. * Extract the public key from the CSR
  49697. */
  49698. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  49699. /*
  49700. * Verify the signature in the CSR
  49701. */
  49702. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  49703. X509_free(req);
  49704. req = NULL;
  49705. BIO_free(bio);
  49706. /* Run the same test, but with a file pointer instead of a BIO.
  49707. * (PEM_read_X509_REQ)*/
  49708. ExpectTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  49709. ExpectNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  49710. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  49711. X509_free(req);
  49712. EVP_PKEY_free(pub_key);
  49713. }
  49714. #endif /* !NO_DSA && !HAVE_SELFTEST */
  49715. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  49716. return EXPECT_RESULT();
  49717. }
  49718. static int test_wolfSSL_PEM_read_X509(void)
  49719. {
  49720. EXPECT_DECLS;
  49721. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  49722. !defined(NO_RSA)
  49723. X509 *x509 = NULL;
  49724. XFILE fp = XBADFILE;
  49725. ExpectTrue((fp = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  49726. ExpectNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  49727. X509_free(x509);
  49728. if (fp != XBADFILE)
  49729. XFCLOSE(fp);
  49730. #endif
  49731. return EXPECT_RESULT();
  49732. }
  49733. static int test_wolfSSL_PEM_read(void)
  49734. {
  49735. EXPECT_DECLS;
  49736. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  49737. const char* filename = "./certs/server-keyEnc.pem";
  49738. XFILE fp = XBADFILE;
  49739. char* name = NULL;
  49740. char* header = NULL;
  49741. byte* data = NULL;
  49742. long len;
  49743. EVP_CIPHER_INFO cipher;
  49744. WOLFSSL_BIO* bio = NULL;
  49745. byte* fileData = NULL;
  49746. size_t fileDataSz = 0;
  49747. byte* out;
  49748. ExpectNotNull(bio = BIO_new_file(filename, "rb"));
  49749. ExpectIntEQ(PEM_read_bio(bio, NULL, &header, &data, &len), 0);
  49750. ExpectIntEQ(PEM_read_bio(bio, &name, NULL, &data, &len), 0);
  49751. ExpectIntEQ(PEM_read_bio(bio, &name, &header, NULL, &len), 0);
  49752. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, NULL), 0);
  49753. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, &len), 1);
  49754. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  49755. ExpectIntGT(XSTRLEN(header), 0);
  49756. ExpectIntGT(len, 0);
  49757. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49758. name = NULL;
  49759. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49760. header = NULL;
  49761. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49762. data = NULL;
  49763. BIO_free(bio);
  49764. bio = NULL;
  49765. ExpectTrue((fp = XFOPEN(filename, "rb")) != XBADFILE);
  49766. /* Fail cases. */
  49767. ExpectIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  49768. ExpectIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  49769. ExpectIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  49770. ExpectIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  49771. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  49772. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  49773. ExpectIntGT(XSTRLEN(header), 0);
  49774. ExpectIntGT(len, 0);
  49775. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  49776. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  49777. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  49778. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  49779. DYNAMIC_TYPE_TMP_BUFFER));
  49780. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  49781. if (fp != XBADFILE) {
  49782. XFCLOSE(fp);
  49783. fp = XBADFILE;
  49784. }
  49785. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  49786. /* Fail cases. */
  49787. ExpectIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  49788. ExpectIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  49789. ExpectIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  49790. ExpectIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  49791. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  49792. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  49793. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  49794. /* Fail cases. */
  49795. ExpectIntEQ(PEM_write(XBADFILE, name, header, data, len), 0);
  49796. ExpectIntEQ(PEM_write(stderr, NULL, header, data, len), 0);
  49797. ExpectIntEQ(PEM_write(stderr, name, NULL, data, len), 0);
  49798. ExpectIntEQ(PEM_write(stderr, name, header, NULL, len), 0);
  49799. /* Pass case */
  49800. ExpectIntEQ(PEM_write(stderr, name, header, data, len), fileDataSz);
  49801. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49802. name = NULL;
  49803. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49804. header = NULL;
  49805. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49806. data = NULL;
  49807. /* Read out of a fixed buffer BIO - forces malloc in PEM_read_bio. */
  49808. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, &len), 1);
  49809. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  49810. ExpectIntGT(XSTRLEN(header), 0);
  49811. ExpectIntGT(len, 0);
  49812. /* Fail cases. */
  49813. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  49814. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  49815. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  49816. #ifndef NO_DES3
  49817. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  49818. #endif
  49819. /* Fail cases. */
  49820. ExpectIntEQ(PEM_do_header(NULL, data, &len, PasswordCallBack,
  49821. (void*)"yassl123"), WOLFSSL_FAILURE);
  49822. ExpectIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  49823. (void*)"yassl123"), WOLFSSL_FAILURE);
  49824. ExpectIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  49825. (void*)"yassl123"), WOLFSSL_FAILURE);
  49826. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  49827. (void*)"yassl123"), WOLFSSL_FAILURE);
  49828. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NoPasswordCallBack,
  49829. (void*)"yassl123"), WOLFSSL_FAILURE);
  49830. #if !defined(NO_DES3) && !defined(NO_MD5)
  49831. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  49832. (void*)"yassl123"), WOLFSSL_SUCCESS);
  49833. #else
  49834. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  49835. (void*)"yassl123"), WOLFSSL_FAILURE);
  49836. #endif
  49837. BIO_free(bio);
  49838. bio = NULL;
  49839. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49840. fileData = NULL;
  49841. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49842. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49843. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49844. name = NULL;
  49845. header = NULL;
  49846. data = NULL;
  49847. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  49848. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  49849. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  49850. ExpectIntEQ(XSTRLEN(header), 0);
  49851. ExpectIntGT(len, 0);
  49852. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  49853. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  49854. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  49855. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  49856. DYNAMIC_TYPE_TMP_BUFFER));
  49857. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  49858. if (fp != XBADFILE)
  49859. XFCLOSE(fp);
  49860. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  49861. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  49862. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  49863. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  49864. BIO_free(bio);
  49865. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49866. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49867. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49868. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  49869. #endif
  49870. return EXPECT_RESULT();
  49871. }
  49872. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  49873. {
  49874. EXPECT_DECLS;
  49875. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  49876. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49877. const byte iv[12] = { 0 };
  49878. const byte key[16] = { 0 };
  49879. const byte cleartext[16] = { 0 };
  49880. const byte aad[] = {
  49881. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  49882. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  49883. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  49884. 0x4f
  49885. };
  49886. byte out1Part[16];
  49887. byte outTag1Part[16];
  49888. byte out2Part[16];
  49889. byte outTag2Part[16];
  49890. byte decryptBuf[16];
  49891. int len = 0;
  49892. int tlen;
  49893. EVP_CIPHER_CTX* ctx = NULL;
  49894. /* ENCRYPT */
  49895. /* Send AAD and data in 1 part */
  49896. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  49897. tlen = 0;
  49898. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49899. 1);
  49900. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49901. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  49902. ExpectIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  49903. sizeof(cleartext)), 1);
  49904. tlen += len;
  49905. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  49906. tlen += len;
  49907. ExpectIntEQ(tlen, sizeof(cleartext));
  49908. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  49909. outTag1Part), 1);
  49910. EVP_CIPHER_CTX_free(ctx);
  49911. ctx = NULL;
  49912. /* DECRYPT */
  49913. /* Send AAD and data in 1 part */
  49914. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  49915. tlen = 0;
  49916. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49917. 1);
  49918. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49919. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  49920. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  49921. sizeof(cleartext)), 1);
  49922. tlen += len;
  49923. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  49924. outTag1Part), 1);
  49925. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  49926. tlen += len;
  49927. ExpectIntEQ(tlen, sizeof(cleartext));
  49928. EVP_CIPHER_CTX_free(ctx);
  49929. ctx = NULL;
  49930. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  49931. /* ENCRYPT */
  49932. /* Send AAD and data in 2 parts */
  49933. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  49934. tlen = 0;
  49935. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49936. 1);
  49937. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49938. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  49939. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  49940. 1);
  49941. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  49942. tlen += len;
  49943. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  49944. sizeof(cleartext) - 1), 1);
  49945. tlen += len;
  49946. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  49947. tlen += len;
  49948. ExpectIntEQ(tlen, sizeof(cleartext));
  49949. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  49950. outTag2Part), 1);
  49951. ExpectIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  49952. ExpectIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  49953. EVP_CIPHER_CTX_free(ctx);
  49954. ctx = NULL;
  49955. /* DECRYPT */
  49956. /* Send AAD and data in 2 parts */
  49957. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  49958. tlen = 0;
  49959. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  49960. 1);
  49961. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  49962. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  49963. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  49964. 1);
  49965. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  49966. tlen += len;
  49967. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  49968. sizeof(cleartext) - 1), 1);
  49969. tlen += len;
  49970. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  49971. outTag1Part), 1);
  49972. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  49973. tlen += len;
  49974. ExpectIntEQ(tlen, sizeof(cleartext));
  49975. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  49976. /* Test AAD reuse */
  49977. EVP_CIPHER_CTX_free(ctx);
  49978. #endif
  49979. return EXPECT_RESULT();
  49980. }
  49981. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  49982. {
  49983. EXPECT_DECLS;
  49984. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  49985. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  49986. /* Zero length plain text */
  49987. byte key[] = {
  49988. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49989. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49990. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  49991. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49992. }; /* align */
  49993. byte iv[] = {
  49994. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  49995. }; /* align */
  49996. byte plaintxt[1];
  49997. int ivSz = 12;
  49998. int plaintxtSz = 0;
  49999. unsigned char tag[16];
  50000. unsigned char tag_kat[] = {
  50001. 0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  50002. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b
  50003. };
  50004. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  50005. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  50006. int ciphertxtSz = 0;
  50007. int decryptedtxtSz = 0;
  50008. int len = 0;
  50009. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  50010. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  50011. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  50012. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50013. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  50014. plaintxtSz));
  50015. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  50016. ciphertxtSz += len;
  50017. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  50018. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  50019. ExpectIntEQ(0, ciphertxtSz);
  50020. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  50021. EVP_CIPHER_CTX_init(de);
  50022. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  50023. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50024. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  50025. decryptedtxtSz = len;
  50026. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  50027. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  50028. decryptedtxtSz += len;
  50029. ExpectIntEQ(0, decryptedtxtSz);
  50030. EVP_CIPHER_CTX_free(en);
  50031. EVP_CIPHER_CTX_free(de);
  50032. #endif
  50033. return EXPECT_RESULT();
  50034. }
  50035. static int test_wolfssl_EVP_aes_gcm(void)
  50036. {
  50037. EXPECT_DECLS;
  50038. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  50039. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50040. /* A 256 bit key, AES_128 will use the first 128 bit*/
  50041. byte *key = (byte*)"01234567890123456789012345678901";
  50042. /* A 128 bit IV */
  50043. byte *iv = (byte*)"0123456789012345";
  50044. int ivSz = AES_BLOCK_SIZE;
  50045. /* Message to be encrypted */
  50046. byte *plaintxt = (byte*)"for things to change you have to change";
  50047. /* Additional non-confidential data */
  50048. byte *aad = (byte*)"Don't spend major time on minor things.";
  50049. unsigned char tag[AES_BLOCK_SIZE] = {0};
  50050. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  50051. int aadSz = (int)XSTRLEN((char*)aad);
  50052. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  50053. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  50054. int ciphertxtSz = 0;
  50055. int decryptedtxtSz = 0;
  50056. int len = 0;
  50057. int i = 0;
  50058. EVP_CIPHER_CTX en[2];
  50059. EVP_CIPHER_CTX de[2];
  50060. for (i = 0; i < 2; i++) {
  50061. EVP_CIPHER_CTX_init(&en[i]);
  50062. if (i == 0) {
  50063. /* Default uses 96-bits IV length */
  50064. #ifdef WOLFSSL_AES_128
  50065. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  50066. key, iv));
  50067. #elif defined(WOLFSSL_AES_192)
  50068. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  50069. key, iv));
  50070. #elif defined(WOLFSSL_AES_256)
  50071. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  50072. key, iv));
  50073. #endif
  50074. }
  50075. else {
  50076. #ifdef WOLFSSL_AES_128
  50077. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  50078. NULL, NULL));
  50079. #elif defined(WOLFSSL_AES_192)
  50080. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  50081. NULL, NULL));
  50082. #elif defined(WOLFSSL_AES_256)
  50083. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  50084. NULL, NULL));
  50085. #endif
  50086. /* non-default must to set the IV length first */
  50087. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  50088. ivSz, NULL));
  50089. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50090. }
  50091. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  50092. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  50093. plaintxtSz));
  50094. ciphertxtSz = len;
  50095. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50096. ciphertxtSz += len;
  50097. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  50098. AES_BLOCK_SIZE, tag));
  50099. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  50100. EVP_CIPHER_CTX_init(&de[i]);
  50101. if (i == 0) {
  50102. /* Default uses 96-bits IV length */
  50103. #ifdef WOLFSSL_AES_128
  50104. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50105. key, iv));
  50106. #elif defined(WOLFSSL_AES_192)
  50107. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50108. key, iv));
  50109. #elif defined(WOLFSSL_AES_256)
  50110. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50111. key, iv));
  50112. #endif
  50113. }
  50114. else {
  50115. #ifdef WOLFSSL_AES_128
  50116. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50117. NULL, NULL));
  50118. #elif defined(WOLFSSL_AES_192)
  50119. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50120. NULL, NULL));
  50121. #elif defined(WOLFSSL_AES_256)
  50122. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50123. NULL, NULL));
  50124. #endif
  50125. /* non-default must to set the IV length first */
  50126. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  50127. ivSz, NULL));
  50128. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50129. }
  50130. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50131. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50132. ciphertxtSz));
  50133. decryptedtxtSz = len;
  50134. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  50135. AES_BLOCK_SIZE, tag));
  50136. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50137. decryptedtxtSz += len;
  50138. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  50139. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50140. /* modify tag*/
  50141. if (i == 0) {
  50142. /* Default uses 96-bits IV length */
  50143. #ifdef WOLFSSL_AES_128
  50144. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50145. key, iv));
  50146. #elif defined(WOLFSSL_AES_192)
  50147. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50148. key, iv));
  50149. #elif defined(WOLFSSL_AES_256)
  50150. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50151. key, iv));
  50152. #endif
  50153. }
  50154. else {
  50155. #ifdef WOLFSSL_AES_128
  50156. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50157. NULL, NULL));
  50158. #elif defined(WOLFSSL_AES_192)
  50159. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50160. NULL, NULL));
  50161. #elif defined(WOLFSSL_AES_256)
  50162. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50163. NULL, NULL));
  50164. #endif
  50165. /* non-default must to set the IV length first */
  50166. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  50167. ivSz, NULL));
  50168. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50169. }
  50170. tag[AES_BLOCK_SIZE-1]+=0xBB;
  50171. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50172. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  50173. AES_BLOCK_SIZE, tag));
  50174. /* fail due to wrong tag */
  50175. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50176. ciphertxtSz));
  50177. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50178. ExpectIntEQ(0, len);
  50179. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  50180. }
  50181. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  50182. return EXPECT_RESULT();
  50183. }
  50184. static int test_wolfssl_EVP_aria_gcm(void)
  50185. {
  50186. int res = TEST_SKIPPED;
  50187. #if defined(OPENSSL_EXTRA) && defined(HAVE_ARIA) && \
  50188. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50189. /* A 256 bit key, AES_128 will use the first 128 bit*/
  50190. byte *key = (byte*)"01234567890123456789012345678901";
  50191. /* A 128 bit IV */
  50192. byte *iv = (byte*)"0123456789012345";
  50193. int ivSz = ARIA_BLOCK_SIZE;
  50194. /* Message to be encrypted */
  50195. const int plaintxtSz = 40;
  50196. byte plaintxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  50197. XMEMCPY(plaintxt,"for things to change you have to change",plaintxtSz);
  50198. /* Additional non-confidential data */
  50199. byte *aad = (byte*)"Don't spend major time on minor things.";
  50200. unsigned char tag[ARIA_BLOCK_SIZE] = {0};
  50201. int aadSz = (int)XSTRLEN((char*)aad);
  50202. byte ciphertxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  50203. byte decryptedtxt[plaintxtSz];
  50204. int ciphertxtSz = 0;
  50205. int decryptedtxtSz = 0;
  50206. int len = 0;
  50207. int i = 0;
  50208. #define TEST_ARIA_GCM_COUNT 6
  50209. EVP_CIPHER_CTX en[TEST_ARIA_GCM_COUNT];
  50210. EVP_CIPHER_CTX de[TEST_ARIA_GCM_COUNT];
  50211. for (i = 0; i < TEST_ARIA_GCM_COUNT; i++) {
  50212. EVP_CIPHER_CTX_init(&en[i]);
  50213. switch (i) {
  50214. case 0:
  50215. /* Default uses 96-bits IV length */
  50216. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, key, iv));
  50217. break;
  50218. case 1:
  50219. /* Default uses 96-bits IV length */
  50220. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, key, iv));
  50221. break;
  50222. case 2:
  50223. /* Default uses 96-bits IV length */
  50224. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, key, iv));
  50225. break;
  50226. case 3:
  50227. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  50228. /* non-default must to set the IV length first */
  50229. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50230. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50231. break;
  50232. case 4:
  50233. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  50234. /* non-default must to set the IV length first */
  50235. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50236. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50237. break;
  50238. case 5:
  50239. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  50240. /* non-default must to set the IV length first */
  50241. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50242. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50243. break;
  50244. }
  50245. XMEMSET(ciphertxt,0,sizeof(ciphertxt));
  50246. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  50247. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  50248. ciphertxtSz = len;
  50249. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50250. AssertIntNE(0, XMEMCMP(plaintxt, ciphertxt, plaintxtSz));
  50251. ciphertxtSz += len;
  50252. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, ARIA_BLOCK_SIZE, tag));
  50253. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  50254. EVP_CIPHER_CTX_init(&de[i]);
  50255. switch (i) {
  50256. case 0:
  50257. /* Default uses 96-bits IV length */
  50258. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, key, iv));
  50259. break;
  50260. case 1:
  50261. /* Default uses 96-bits IV length */
  50262. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, key, iv));
  50263. break;
  50264. case 2:
  50265. /* Default uses 96-bits IV length */
  50266. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, key, iv));
  50267. break;
  50268. case 3:
  50269. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  50270. /* non-default must to set the IV length first */
  50271. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50272. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50273. break;
  50274. case 4:
  50275. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  50276. /* non-default must to set the IV length first */
  50277. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50278. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50279. break;
  50280. case 5:
  50281. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  50282. /* non-default must to set the IV length first */
  50283. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50284. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50285. break;
  50286. }
  50287. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  50288. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50289. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  50290. decryptedtxtSz = len;
  50291. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  50292. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50293. decryptedtxtSz += len;
  50294. AssertIntEQ(plaintxtSz, decryptedtxtSz);
  50295. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50296. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  50297. /* modify tag*/
  50298. tag[AES_BLOCK_SIZE-1]+=0xBB;
  50299. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50300. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  50301. /* fail due to wrong tag */
  50302. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  50303. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50304. AssertIntEQ(0, len);
  50305. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  50306. }
  50307. res = TEST_RES_CHECK(1);
  50308. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  50309. return res;
  50310. }
  50311. static int test_wolfssl_EVP_aes_ccm_zeroLen(void)
  50312. {
  50313. EXPECT_DECLS;
  50314. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  50315. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50316. /* Zero length plain text */
  50317. byte key[] = {
  50318. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50319. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50320. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50321. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50322. }; /* align */
  50323. byte iv[] = {
  50324. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50325. }; /* align */
  50326. byte plaintxt[1];
  50327. int ivSz = 12;
  50328. int plaintxtSz = 0;
  50329. unsigned char tag[16];
  50330. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  50331. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  50332. int ciphertxtSz = 0;
  50333. int decryptedtxtSz = 0;
  50334. int len = 0;
  50335. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  50336. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  50337. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_ccm(), NULL, key, iv));
  50338. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  50339. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  50340. plaintxtSz));
  50341. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  50342. ciphertxtSz += len;
  50343. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  50344. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  50345. ExpectIntEQ(0, ciphertxtSz);
  50346. EVP_CIPHER_CTX_init(de);
  50347. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_ccm(), NULL, key, iv));
  50348. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  50349. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  50350. decryptedtxtSz = len;
  50351. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  50352. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  50353. decryptedtxtSz += len;
  50354. ExpectIntEQ(0, decryptedtxtSz);
  50355. EVP_CIPHER_CTX_free(en);
  50356. EVP_CIPHER_CTX_free(de);
  50357. #endif
  50358. return EXPECT_RESULT();
  50359. }
  50360. static int test_wolfssl_EVP_aes_ccm(void)
  50361. {
  50362. EXPECT_DECLS;
  50363. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  50364. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50365. /* A 256 bit key, AES_128 will use the first 128 bit*/
  50366. byte *key = (byte*)"01234567890123456789012345678901";
  50367. /* A 128 bit IV */
  50368. byte *iv = (byte*)"0123456789012";
  50369. int ivSz = (int)XSTRLEN((char*)iv);
  50370. /* Message to be encrypted */
  50371. byte *plaintxt = (byte*)"for things to change you have to change";
  50372. /* Additional non-confidential data */
  50373. byte *aad = (byte*)"Don't spend major time on minor things.";
  50374. unsigned char tag[AES_BLOCK_SIZE] = {0};
  50375. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  50376. int aadSz = (int)XSTRLEN((char*)aad);
  50377. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  50378. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  50379. int ciphertxtSz = 0;
  50380. int decryptedtxtSz = 0;
  50381. int len = 0;
  50382. int i = 0;
  50383. int ret;
  50384. EVP_CIPHER_CTX en[2];
  50385. EVP_CIPHER_CTX de[2];
  50386. for (i = 0; i < 2; i++) {
  50387. EVP_CIPHER_CTX_init(&en[i]);
  50388. if (i == 0) {
  50389. /* Default uses 96-bits IV length */
  50390. #ifdef WOLFSSL_AES_128
  50391. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  50392. key, iv));
  50393. #elif defined(WOLFSSL_AES_192)
  50394. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  50395. key, iv));
  50396. #elif defined(WOLFSSL_AES_256)
  50397. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  50398. key, iv));
  50399. #endif
  50400. }
  50401. else {
  50402. #ifdef WOLFSSL_AES_128
  50403. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  50404. NULL, NULL));
  50405. #elif defined(WOLFSSL_AES_192)
  50406. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  50407. NULL, NULL));
  50408. #elif defined(WOLFSSL_AES_256)
  50409. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  50410. NULL, NULL));
  50411. #endif
  50412. /* non-default must to set the IV length first */
  50413. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  50414. ivSz, NULL));
  50415. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50416. }
  50417. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  50418. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  50419. plaintxtSz));
  50420. ciphertxtSz = len;
  50421. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50422. ciphertxtSz += len;
  50423. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  50424. AES_BLOCK_SIZE, tag));
  50425. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]);
  50426. ExpectIntEQ(ret, 1);
  50427. EVP_CIPHER_CTX_init(&de[i]);
  50428. if (i == 0) {
  50429. /* Default uses 96-bits IV length */
  50430. #ifdef WOLFSSL_AES_128
  50431. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  50432. key, iv));
  50433. #elif defined(WOLFSSL_AES_192)
  50434. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  50435. key, iv));
  50436. #elif defined(WOLFSSL_AES_256)
  50437. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  50438. key, iv));
  50439. #endif
  50440. }
  50441. else {
  50442. #ifdef WOLFSSL_AES_128
  50443. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  50444. NULL, NULL));
  50445. #elif defined(WOLFSSL_AES_192)
  50446. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  50447. NULL, NULL));
  50448. #elif defined(WOLFSSL_AES_256)
  50449. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  50450. NULL, NULL));
  50451. #endif
  50452. /* non-default must to set the IV length first */
  50453. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  50454. ivSz, NULL));
  50455. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50456. }
  50457. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50458. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50459. ciphertxtSz));
  50460. decryptedtxtSz = len;
  50461. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  50462. AES_BLOCK_SIZE, tag));
  50463. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50464. decryptedtxtSz += len;
  50465. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  50466. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50467. /* modify tag*/
  50468. tag[AES_BLOCK_SIZE-1]+=0xBB;
  50469. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50470. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  50471. AES_BLOCK_SIZE, tag));
  50472. /* fail due to wrong tag */
  50473. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50474. ciphertxtSz));
  50475. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50476. ExpectIntEQ(0, len);
  50477. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]);
  50478. ExpectIntEQ(ret, 1);
  50479. }
  50480. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESCCM */
  50481. return EXPECT_RESULT();
  50482. }
  50483. static int test_wolfssl_EVP_chacha20_poly1305(void)
  50484. {
  50485. EXPECT_DECLS;
  50486. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  50487. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  50488. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  50489. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  50490. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  50491. byte cipherText[sizeof(plainText)];
  50492. byte decryptedText[sizeof(plainText)];
  50493. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  50494. EVP_CIPHER_CTX* ctx = NULL;
  50495. int outSz;
  50496. /* Encrypt. */
  50497. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50498. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  50499. NULL), WOLFSSL_SUCCESS);
  50500. /* Invalid IV length. */
  50501. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  50502. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  50503. /* Valid IV length. */
  50504. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  50505. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  50506. /* Invalid tag length. */
  50507. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  50508. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  50509. /* Valid tag length. */
  50510. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  50511. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  50512. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50513. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  50514. WOLFSSL_SUCCESS);
  50515. ExpectIntEQ(outSz, sizeof(aad));
  50516. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  50517. sizeof(plainText)), WOLFSSL_SUCCESS);
  50518. ExpectIntEQ(outSz, sizeof(plainText));
  50519. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  50520. ExpectIntEQ(outSz, 0);
  50521. /* Invalid tag length. */
  50522. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  50523. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  50524. /* Valid tag length. */
  50525. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  50526. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  50527. EVP_CIPHER_CTX_free(ctx);
  50528. ctx = NULL;
  50529. /* Decrypt. */
  50530. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50531. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  50532. NULL), WOLFSSL_SUCCESS);
  50533. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  50534. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  50535. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  50536. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  50537. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50538. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  50539. WOLFSSL_SUCCESS);
  50540. ExpectIntEQ(outSz, sizeof(aad));
  50541. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50542. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50543. ExpectIntEQ(outSz, sizeof(cipherText));
  50544. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50545. WOLFSSL_SUCCESS);
  50546. ExpectIntEQ(outSz, 0);
  50547. EVP_CIPHER_CTX_free(ctx);
  50548. ctx = NULL;
  50549. /* Test partial Inits. CipherInit() allow setting of key and iv
  50550. * in separate calls. */
  50551. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50552. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  50553. key, NULL, 1), WOLFSSL_SUCCESS);
  50554. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  50555. WOLFSSL_SUCCESS);
  50556. ExpectIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  50557. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  50558. ExpectIntEQ(outSz, sizeof(aad));
  50559. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50560. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50561. ExpectIntEQ(outSz, sizeof(cipherText));
  50562. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50563. WOLFSSL_SUCCESS);
  50564. ExpectIntEQ(outSz, 0);
  50565. EVP_CIPHER_CTX_free(ctx);
  50566. #endif
  50567. return EXPECT_RESULT();
  50568. }
  50569. static int test_wolfssl_EVP_chacha20(void)
  50570. {
  50571. EXPECT_DECLS;
  50572. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
  50573. byte key[CHACHA_MAX_KEY_SZ];
  50574. byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
  50575. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  50576. byte cipherText[sizeof(plainText)];
  50577. byte decryptedText[sizeof(plainText)];
  50578. EVP_CIPHER_CTX* ctx = NULL;
  50579. int outSz;
  50580. XMEMSET(key, 0, sizeof(key));
  50581. XMEMSET(iv, 0, sizeof(iv));
  50582. /* Encrypt. */
  50583. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50584. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  50585. NULL), WOLFSSL_SUCCESS);
  50586. /* Any tag length must fail - not an AEAD cipher. */
  50587. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  50588. 16, NULL), WOLFSSL_FAILURE);
  50589. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50590. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  50591. sizeof(plainText)), WOLFSSL_SUCCESS);
  50592. ExpectIntEQ(outSz, sizeof(plainText));
  50593. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  50594. ExpectIntEQ(outSz, 0);
  50595. EVP_CIPHER_CTX_free(ctx);
  50596. ctx = NULL;
  50597. /* Decrypt. */
  50598. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50599. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  50600. NULL), WOLFSSL_SUCCESS);
  50601. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50602. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50603. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50604. ExpectIntEQ(outSz, sizeof(cipherText));
  50605. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50606. WOLFSSL_SUCCESS);
  50607. ExpectIntEQ(outSz, 0);
  50608. EVP_CIPHER_CTX_free(ctx);
  50609. ctx = NULL;
  50610. /* Test partial Inits. CipherInit() allow setting of key and iv
  50611. * in separate calls. */
  50612. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50613. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
  50614. key, NULL, 1), WOLFSSL_SUCCESS);
  50615. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  50616. WOLFSSL_SUCCESS);
  50617. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50618. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50619. ExpectIntEQ(outSz, sizeof(cipherText));
  50620. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50621. WOLFSSL_SUCCESS);
  50622. ExpectIntEQ(outSz, 0);
  50623. EVP_CIPHER_CTX_free(ctx);
  50624. #endif
  50625. return EXPECT_RESULT();
  50626. }
  50627. static int test_wolfssl_EVP_sm4_ecb(void)
  50628. {
  50629. int res = TEST_SKIPPED;
  50630. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_ECB)
  50631. EXPECT_DECLS;
  50632. byte key[SM4_KEY_SIZE];
  50633. byte plainText[SM4_BLOCK_SIZE] = {
  50634. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  50635. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  50636. };
  50637. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  50638. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  50639. EVP_CIPHER_CTX* ctx;
  50640. int outSz;
  50641. XMEMSET(key, 0, sizeof(key));
  50642. /* Encrypt. */
  50643. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50644. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  50645. WOLFSSL_SUCCESS);
  50646. /* Any tag length must fail - not an AEAD cipher. */
  50647. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  50648. WOLFSSL_FAILURE);
  50649. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL),
  50650. WOLFSSL_SUCCESS);
  50651. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  50652. sizeof(plainText)), WOLFSSL_SUCCESS);
  50653. ExpectIntEQ(outSz, sizeof(plainText));
  50654. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  50655. WOLFSSL_SUCCESS);
  50656. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  50657. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  50658. EVP_CIPHER_CTX_free(ctx);
  50659. /* Decrypt. */
  50660. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50661. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  50662. WOLFSSL_SUCCESS);
  50663. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, NULL),
  50664. WOLFSSL_SUCCESS);
  50665. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50666. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50667. ExpectIntEQ(outSz, sizeof(plainText));
  50668. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  50669. WOLFSSL_SUCCESS);
  50670. ExpectIntEQ(outSz, 0);
  50671. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  50672. EVP_CIPHER_CTX_free(ctx);
  50673. res = EXPECT_RESULT();
  50674. #endif
  50675. return res;
  50676. }
  50677. static int test_wolfssl_EVP_sm4_cbc(void)
  50678. {
  50679. int res = TEST_SKIPPED;
  50680. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CBC)
  50681. EXPECT_DECLS;
  50682. byte key[SM4_KEY_SIZE];
  50683. byte iv[SM4_BLOCK_SIZE];
  50684. byte plainText[SM4_BLOCK_SIZE] = {
  50685. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  50686. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  50687. };
  50688. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  50689. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  50690. EVP_CIPHER_CTX* ctx;
  50691. int outSz;
  50692. XMEMSET(key, 0, sizeof(key));
  50693. XMEMSET(iv, 0, sizeof(iv));
  50694. /* Encrypt. */
  50695. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50696. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  50697. WOLFSSL_SUCCESS);
  50698. /* Any tag length must fail - not an AEAD cipher. */
  50699. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  50700. WOLFSSL_FAILURE);
  50701. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50702. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  50703. sizeof(plainText)), WOLFSSL_SUCCESS);
  50704. ExpectIntEQ(outSz, sizeof(plainText));
  50705. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  50706. WOLFSSL_SUCCESS);
  50707. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  50708. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  50709. EVP_CIPHER_CTX_free(ctx);
  50710. /* Decrypt. */
  50711. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50712. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  50713. WOLFSSL_SUCCESS);
  50714. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50715. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50716. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50717. ExpectIntEQ(outSz, sizeof(plainText));
  50718. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  50719. WOLFSSL_SUCCESS);
  50720. ExpectIntEQ(outSz, 0);
  50721. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  50722. EVP_CIPHER_CTX_free(ctx);
  50723. /* Test partial Inits. CipherInit() allow setting of key and iv
  50724. * in separate calls. */
  50725. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50726. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_cbc(), key, NULL, 0),
  50727. WOLFSSL_SUCCESS);
  50728. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 0),
  50729. WOLFSSL_SUCCESS);
  50730. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50731. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50732. ExpectIntEQ(outSz, sizeof(plainText));
  50733. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  50734. WOLFSSL_SUCCESS);
  50735. ExpectIntEQ(outSz, 0);
  50736. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  50737. EVP_CIPHER_CTX_free(ctx);
  50738. res = EXPECT_RESULT();
  50739. #endif
  50740. return res;
  50741. }
  50742. static int test_wolfssl_EVP_sm4_ctr(void)
  50743. {
  50744. int res = TEST_SKIPPED;
  50745. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CTR)
  50746. EXPECT_DECLS;
  50747. byte key[SM4_KEY_SIZE];
  50748. byte iv[SM4_BLOCK_SIZE];
  50749. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  50750. byte cipherText[sizeof(plainText)];
  50751. byte decryptedText[sizeof(plainText)];
  50752. EVP_CIPHER_CTX* ctx;
  50753. int outSz;
  50754. XMEMSET(key, 0, sizeof(key));
  50755. XMEMSET(iv, 0, sizeof(iv));
  50756. /* Encrypt. */
  50757. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50758. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  50759. WOLFSSL_SUCCESS);
  50760. /* Any tag length must fail - not an AEAD cipher. */
  50761. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  50762. WOLFSSL_FAILURE);
  50763. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50764. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  50765. sizeof(plainText)), WOLFSSL_SUCCESS);
  50766. ExpectIntEQ(outSz, sizeof(plainText));
  50767. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  50768. ExpectIntEQ(outSz, 0);
  50769. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  50770. EVP_CIPHER_CTX_free(ctx);
  50771. /* Decrypt. */
  50772. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50773. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  50774. WOLFSSL_SUCCESS);
  50775. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50776. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50777. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50778. ExpectIntEQ(outSz, sizeof(cipherText));
  50779. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50780. WOLFSSL_SUCCESS);
  50781. ExpectIntEQ(outSz, 0);
  50782. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  50783. EVP_CIPHER_CTX_free(ctx);
  50784. /* Test partial Inits. CipherInit() allow setting of key and iv
  50785. * in separate calls. */
  50786. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50787. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_ctr(), key, NULL, 1),
  50788. WOLFSSL_SUCCESS);
  50789. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  50790. WOLFSSL_SUCCESS);
  50791. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50792. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50793. ExpectIntEQ(outSz, sizeof(cipherText));
  50794. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50795. WOLFSSL_SUCCESS);
  50796. ExpectIntEQ(outSz, 0);
  50797. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  50798. EVP_CIPHER_CTX_free(ctx);
  50799. res = EXPECT_RESULT();
  50800. #endif
  50801. return res;
  50802. }
  50803. static int test_wolfssl_EVP_sm4_gcm_zeroLen(void)
  50804. {
  50805. int res = TEST_SKIPPED;
  50806. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  50807. /* Zero length plain text */
  50808. EXPECT_DECLS;
  50809. byte key[] = {
  50810. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50811. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50812. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50813. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50814. }; /* align */
  50815. byte iv[] = {
  50816. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50817. }; /* align */
  50818. byte plaintxt[1];
  50819. int ivSz = 12;
  50820. int plaintxtSz = 0;
  50821. unsigned char tag[16];
  50822. unsigned char tag_kat[16] = {
  50823. 0x23,0x2f,0x0c,0xfe,0x30,0x8b,0x49,0xea,
  50824. 0x6f,0xc8,0x82,0x29,0xb5,0xdc,0x85,0x8d
  50825. };
  50826. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  50827. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  50828. int ciphertxtSz = 0;
  50829. int decryptedtxtSz = 0;
  50830. int len = 0;
  50831. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  50832. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  50833. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_gcm(), NULL, key, iv));
  50834. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50835. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  50836. plaintxtSz));
  50837. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  50838. ciphertxtSz += len;
  50839. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  50840. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  50841. ExpectIntEQ(0, ciphertxtSz);
  50842. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  50843. EVP_CIPHER_CTX_init(de);
  50844. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_gcm(), NULL, key, iv));
  50845. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50846. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  50847. decryptedtxtSz = len;
  50848. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  50849. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  50850. decryptedtxtSz += len;
  50851. ExpectIntEQ(0, decryptedtxtSz);
  50852. EVP_CIPHER_CTX_free(en);
  50853. EVP_CIPHER_CTX_free(de);
  50854. res = EXPECT_RESULT();
  50855. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  50856. return res;
  50857. }
  50858. static int test_wolfssl_EVP_sm4_gcm(void)
  50859. {
  50860. int res = TEST_SKIPPED;
  50861. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  50862. EXPECT_DECLS;
  50863. byte *key = (byte*)"0123456789012345";
  50864. /* A 128 bit IV */
  50865. byte *iv = (byte*)"0123456789012345";
  50866. int ivSz = SM4_BLOCK_SIZE;
  50867. /* Message to be encrypted */
  50868. byte *plaintxt = (byte*)"for things to change you have to change";
  50869. /* Additional non-confidential data */
  50870. byte *aad = (byte*)"Don't spend major time on minor things.";
  50871. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  50872. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  50873. int aadSz = (int)XSTRLEN((char*)aad);
  50874. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  50875. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  50876. int ciphertxtSz = 0;
  50877. int decryptedtxtSz = 0;
  50878. int len = 0;
  50879. int i = 0;
  50880. EVP_CIPHER_CTX en[2];
  50881. EVP_CIPHER_CTX de[2];
  50882. for (i = 0; i < 2; i++) {
  50883. EVP_CIPHER_CTX_init(&en[i]);
  50884. if (i == 0) {
  50885. /* Default uses 96-bits IV length */
  50886. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, key,
  50887. iv));
  50888. }
  50889. else {
  50890. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, NULL,
  50891. NULL));
  50892. /* non-default must to set the IV length first */
  50893. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  50894. ivSz, NULL));
  50895. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50896. }
  50897. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  50898. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  50899. plaintxtSz));
  50900. ciphertxtSz = len;
  50901. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50902. ciphertxtSz += len;
  50903. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  50904. SM4_BLOCK_SIZE, tag));
  50905. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  50906. EVP_CIPHER_CTX_init(&de[i]);
  50907. if (i == 0) {
  50908. /* Default uses 96-bits IV length */
  50909. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, key,
  50910. iv));
  50911. }
  50912. else {
  50913. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, NULL,
  50914. NULL));
  50915. /* non-default must to set the IV length first */
  50916. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  50917. ivSz, NULL));
  50918. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50919. }
  50920. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50921. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50922. ciphertxtSz));
  50923. decryptedtxtSz = len;
  50924. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  50925. SM4_BLOCK_SIZE, tag));
  50926. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50927. decryptedtxtSz += len;
  50928. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  50929. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50930. /* modify tag*/
  50931. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  50932. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50933. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  50934. SM4_BLOCK_SIZE, tag));
  50935. /* fail due to wrong tag */
  50936. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50937. ciphertxtSz));
  50938. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50939. ExpectIntEQ(0, len);
  50940. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  50941. }
  50942. res = EXPECT_RESULT();
  50943. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  50944. return res;
  50945. }
  50946. static int test_wolfssl_EVP_sm4_ccm_zeroLen(void)
  50947. {
  50948. int res = TEST_SKIPPED;
  50949. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  50950. /* Zero length plain text */
  50951. EXPECT_DECLS;
  50952. byte key[] = {
  50953. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50954. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50955. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50956. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50957. }; /* align */
  50958. byte iv[] = {
  50959. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50960. }; /* align */
  50961. byte plaintxt[1];
  50962. int ivSz = 12;
  50963. int plaintxtSz = 0;
  50964. unsigned char tag[16];
  50965. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  50966. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  50967. int ciphertxtSz = 0;
  50968. int decryptedtxtSz = 0;
  50969. int len = 0;
  50970. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  50971. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  50972. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_ccm(), NULL, key, iv));
  50973. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  50974. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  50975. plaintxtSz));
  50976. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  50977. ciphertxtSz += len;
  50978. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  50979. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  50980. ExpectIntEQ(0, ciphertxtSz);
  50981. EVP_CIPHER_CTX_init(de);
  50982. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_ccm(), NULL, key, iv));
  50983. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  50984. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  50985. decryptedtxtSz = len;
  50986. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  50987. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  50988. decryptedtxtSz += len;
  50989. ExpectIntEQ(0, decryptedtxtSz);
  50990. EVP_CIPHER_CTX_free(en);
  50991. EVP_CIPHER_CTX_free(de);
  50992. res = EXPECT_RESULT();
  50993. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  50994. return res;
  50995. }
  50996. static int test_wolfssl_EVP_sm4_ccm(void)
  50997. {
  50998. int res = TEST_SKIPPED;
  50999. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  51000. EXPECT_DECLS;
  51001. byte *key = (byte*)"0123456789012345";
  51002. byte *iv = (byte*)"0123456789012";
  51003. int ivSz = (int)XSTRLEN((char*)iv);
  51004. /* Message to be encrypted */
  51005. byte *plaintxt = (byte*)"for things to change you have to change";
  51006. /* Additional non-confidential data */
  51007. byte *aad = (byte*)"Don't spend major time on minor things.";
  51008. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  51009. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  51010. int aadSz = (int)XSTRLEN((char*)aad);
  51011. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  51012. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  51013. int ciphertxtSz = 0;
  51014. int decryptedtxtSz = 0;
  51015. int len = 0;
  51016. int i = 0;
  51017. EVP_CIPHER_CTX en[2];
  51018. EVP_CIPHER_CTX de[2];
  51019. for (i = 0; i < 2; i++) {
  51020. EVP_CIPHER_CTX_init(&en[i]);
  51021. if (i == 0) {
  51022. /* Default uses 96-bits IV length */
  51023. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, key,
  51024. iv));
  51025. }
  51026. else {
  51027. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, NULL,
  51028. NULL));
  51029. /* non-default must to set the IV length first */
  51030. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  51031. ivSz, NULL));
  51032. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  51033. }
  51034. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  51035. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  51036. plaintxtSz));
  51037. ciphertxtSz = len;
  51038. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  51039. ciphertxtSz += len;
  51040. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  51041. SM4_BLOCK_SIZE, tag));
  51042. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  51043. EVP_CIPHER_CTX_init(&de[i]);
  51044. if (i == 0) {
  51045. /* Default uses 96-bits IV length */
  51046. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, key,
  51047. iv));
  51048. }
  51049. else {
  51050. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, NULL,
  51051. NULL));
  51052. /* non-default must to set the IV length first */
  51053. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  51054. ivSz, NULL));
  51055. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  51056. }
  51057. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  51058. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  51059. ciphertxtSz));
  51060. decryptedtxtSz = len;
  51061. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  51062. SM4_BLOCK_SIZE, tag));
  51063. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  51064. decryptedtxtSz += len;
  51065. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  51066. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  51067. /* modify tag*/
  51068. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  51069. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  51070. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  51071. SM4_BLOCK_SIZE, tag));
  51072. /* fail due to wrong tag */
  51073. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  51074. ciphertxtSz));
  51075. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  51076. ExpectIntEQ(0, len);
  51077. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  51078. }
  51079. res = EXPECT_RESULT();
  51080. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  51081. return res;
  51082. }
  51083. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  51084. {
  51085. EXPECT_DECLS;
  51086. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  51087. EVP_PKEY_CTX* ctx = NULL;
  51088. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  51089. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  51090. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  51091. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  51092. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  51093. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  51094. byte outKey[34];
  51095. size_t outKeySz = sizeof(outKey);
  51096. /* These expected outputs were gathered by running the same test below using
  51097. * OpenSSL. */
  51098. const byte extractAndExpand[] = {
  51099. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  51100. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  51101. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  51102. };
  51103. const byte extractOnly[] = {
  51104. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  51105. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  51106. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  51107. };
  51108. const byte expandOnly[] = {
  51109. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  51110. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  51111. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  51112. };
  51113. const byte extractAndExpandAddInfo[] = {
  51114. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  51115. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  51116. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  51117. };
  51118. ExpectNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  51119. ExpectIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  51120. /* NULL ctx. */
  51121. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  51122. /* NULL md. */
  51123. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  51124. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  51125. /* NULL ctx. */
  51126. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  51127. WOLFSSL_FAILURE);
  51128. /* NULL salt is ok. */
  51129. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  51130. WOLFSSL_SUCCESS);
  51131. /* Salt length <= 0. */
  51132. /* Length 0 salt is ok. */
  51133. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  51134. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  51135. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  51136. WOLFSSL_SUCCESS);
  51137. /* NULL ctx. */
  51138. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  51139. WOLFSSL_FAILURE);
  51140. /* NULL key. */
  51141. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  51142. WOLFSSL_FAILURE);
  51143. /* Key length <= 0 */
  51144. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  51145. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  51146. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  51147. WOLFSSL_SUCCESS);
  51148. /* NULL ctx. */
  51149. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  51150. WOLFSSL_FAILURE);
  51151. /* NULL info is ok. */
  51152. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  51153. WOLFSSL_SUCCESS);
  51154. /* Info length <= 0 */
  51155. /* Length 0 info is ok. */
  51156. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  51157. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  51158. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  51159. WOLFSSL_SUCCESS);
  51160. /* NULL ctx. */
  51161. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  51162. WOLFSSL_FAILURE);
  51163. /* Extract and expand (default). */
  51164. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51165. ExpectIntEQ(outKeySz, sizeof(extractAndExpand));
  51166. ExpectIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  51167. /* Extract only. */
  51168. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  51169. WOLFSSL_SUCCESS);
  51170. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51171. ExpectIntEQ(outKeySz, sizeof(extractOnly));
  51172. ExpectIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  51173. outKeySz = sizeof(outKey);
  51174. /* Expand only. */
  51175. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  51176. WOLFSSL_SUCCESS);
  51177. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51178. ExpectIntEQ(outKeySz, sizeof(expandOnly));
  51179. ExpectIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  51180. outKeySz = sizeof(outKey);
  51181. /* Extract and expand with appended additional info. */
  51182. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  51183. WOLFSSL_SUCCESS);
  51184. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  51185. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  51186. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51187. ExpectIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  51188. ExpectIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  51189. EVP_PKEY_CTX_free(ctx);
  51190. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  51191. return EXPECT_RESULT();
  51192. }
  51193. #ifndef NO_BIO
  51194. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  51195. {
  51196. EXPECT_DECLS;
  51197. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  51198. BIO* bio = NULL;
  51199. X509_INFO* info = NULL;
  51200. STACK_OF(X509_INFO)* sk = NULL;
  51201. char* subject = NULL;
  51202. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/"
  51203. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  51204. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/"
  51205. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  51206. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  51207. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  51208. ExpectNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  51209. ExpectIntEQ(sk_X509_INFO_num(sk), 2);
  51210. /* using dereference to maintain testing for Apache port*/
  51211. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  51212. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  51213. 0, 0));
  51214. ExpectIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  51215. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  51216. X509_INFO_free(info);
  51217. info = NULL;
  51218. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  51219. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  51220. 0, 0));
  51221. ExpectIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  51222. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  51223. X509_INFO_free(info);
  51224. ExpectNull(info = sk_X509_INFO_pop(sk));
  51225. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  51226. BIO_free(bio);
  51227. #endif
  51228. return EXPECT_RESULT();
  51229. }
  51230. #endif /* !NO_BIO */
  51231. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  51232. {
  51233. EXPECT_DECLS;
  51234. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  51235. X509 *x509 = NULL;
  51236. X509_NAME* name = NULL;
  51237. int idx = 0;
  51238. X509_NAME_ENTRY *ne = NULL;
  51239. ASN1_OBJECT *object = NULL;
  51240. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  51241. WOLFSSL_FILETYPE_PEM));
  51242. ExpectNotNull(name = X509_get_subject_name(x509));
  51243. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  51244. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  51245. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  51246. X509_free(x509);
  51247. #endif
  51248. return EXPECT_RESULT();
  51249. }
  51250. static int test_wolfSSL_X509_STORE_get1_certs(void)
  51251. {
  51252. EXPECT_DECLS;
  51253. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  51254. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  51255. X509_STORE_CTX *storeCtx = NULL;
  51256. X509_STORE *store = NULL;
  51257. X509 *caX509 = NULL;
  51258. X509 *svrX509 = NULL;
  51259. X509_NAME *subject = NULL;
  51260. WOLF_STACK_OF(WOLFSSL_X509) *certs = NULL;
  51261. ExpectNotNull(caX509 = X509_load_certificate_file(caCertFile,
  51262. SSL_FILETYPE_PEM));
  51263. ExpectNotNull((svrX509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  51264. SSL_FILETYPE_PEM)));
  51265. ExpectNotNull(storeCtx = X509_STORE_CTX_new());
  51266. ExpectNotNull(store = X509_STORE_new());
  51267. ExpectNotNull(subject = X509_get_subject_name(caX509));
  51268. /* Errors */
  51269. ExpectNull(X509_STORE_get1_certs(storeCtx, subject));
  51270. ExpectNull(X509_STORE_get1_certs(NULL, subject));
  51271. ExpectNull(X509_STORE_get1_certs(storeCtx, NULL));
  51272. ExpectIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  51273. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL),
  51274. SSL_SUCCESS);
  51275. /* Should find the cert */
  51276. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  51277. ExpectIntEQ(1, wolfSSL_sk_X509_num(certs));
  51278. sk_X509_pop_free(certs, NULL);
  51279. certs = NULL;
  51280. /* Should not find the cert */
  51281. ExpectNotNull(subject = X509_get_subject_name(svrX509));
  51282. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  51283. ExpectIntEQ(0, wolfSSL_sk_X509_num(certs));
  51284. sk_X509_pop_free(certs, NULL);
  51285. certs = NULL;
  51286. X509_STORE_free(store);
  51287. X509_STORE_CTX_free(storeCtx);
  51288. X509_free(svrX509);
  51289. X509_free(caX509);
  51290. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  51291. return EXPECT_RESULT();
  51292. }
  51293. static int test_wolfSSL_dup_CA_list(void)
  51294. {
  51295. int res = TEST_SKIPPED;
  51296. #if defined(OPENSSL_ALL)
  51297. EXPECT_DECLS;
  51298. STACK_OF(X509_NAME) *originalStack = NULL;
  51299. STACK_OF(X509_NAME) *copyStack = NULL;
  51300. int originalCount = 0;
  51301. int copyCount = 0;
  51302. X509_NAME *name = NULL;
  51303. int i;
  51304. originalStack = sk_X509_NAME_new_null();
  51305. ExpectNotNull(originalStack);
  51306. for (i = 0; i < 3; i++) {
  51307. name = X509_NAME_new();
  51308. ExpectNotNull(name);
  51309. AssertIntEQ(sk_X509_NAME_push(originalStack, name), WOLFSSL_SUCCESS);
  51310. }
  51311. copyStack = SSL_dup_CA_list(originalStack);
  51312. ExpectNotNull(copyStack);
  51313. originalCount = sk_X509_NAME_num(originalStack);
  51314. copyCount = sk_X509_NAME_num(copyStack);
  51315. AssertIntEQ(originalCount, copyCount);
  51316. sk_X509_NAME_pop_free(originalStack, X509_NAME_free);
  51317. sk_X509_NAME_pop_free(copyStack, X509_NAME_free);
  51318. originalStack = NULL;
  51319. copyStack = NULL;
  51320. res = EXPECT_RESULT();
  51321. #endif /* OPENSSL_ALL */
  51322. return res;
  51323. }
  51324. static int test_ForceZero(void)
  51325. {
  51326. EXPECT_DECLS;
  51327. unsigned char data[32];
  51328. unsigned int i, j, len;
  51329. /* Test case with 0 length */
  51330. ForceZero(data, 0);
  51331. /* Test ForceZero */
  51332. for (i = 0; i < sizeof(data); i++) {
  51333. for (len = 1; len < sizeof(data) - i; len++) {
  51334. for (j = 0; j < sizeof(data); j++)
  51335. data[j] = j + 1;
  51336. ForceZero(data + i, len);
  51337. for (j = 0; j < sizeof(data); j++) {
  51338. if (j < i || j >= i + len) {
  51339. ExpectIntNE(data[j], 0x00);
  51340. }
  51341. else {
  51342. ExpectIntEQ(data[j], 0x00);
  51343. }
  51344. }
  51345. }
  51346. }
  51347. return EXPECT_RESULT();
  51348. }
  51349. #ifndef NO_BIO
  51350. static int test_wolfSSL_X509_print(void)
  51351. {
  51352. EXPECT_DECLS;
  51353. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  51354. !defined(NO_RSA) && defined(XSNPRINTF)
  51355. X509 *x509 = NULL;
  51356. BIO *bio = NULL;
  51357. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  51358. const X509_ALGOR *cert_sig_alg = NULL;
  51359. #endif
  51360. ExpectNotNull(x509 = X509_load_certificate_file(svrCertFile,
  51361. WOLFSSL_FILETYPE_PEM));
  51362. /* print to memory */
  51363. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  51364. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  51365. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  51366. #if defined(WC_DISABLE_RADIX_ZERO_PAD)
  51367. /* Will print IP address subject alt name. */
  51368. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3349);
  51369. #elif defined(NO_ASN_TIME)
  51370. /* Will print IP address subject alt name but not Validity. */
  51371. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3235);
  51372. #else
  51373. /* Will print IP address subject alt name. */
  51374. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3350);
  51375. #endif
  51376. #elif defined(NO_ASN_TIME)
  51377. /* With NO_ASN_TIME defined, X509_print skips printing Validity. */
  51378. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3213);
  51379. #else
  51380. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3328);
  51381. #endif
  51382. BIO_free(bio);
  51383. bio = NULL;
  51384. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  51385. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  51386. /* Print signature */
  51387. ExpectNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  51388. ExpectIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  51389. #endif
  51390. /* print to stderr */
  51391. #if !defined(NO_WOLFSSL_DIR)
  51392. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  51393. #endif
  51394. /* print again */
  51395. ExpectIntEQ(X509_print_fp(stderr, x509), SSL_SUCCESS);
  51396. X509_free(x509);
  51397. BIO_free(bio);
  51398. #endif
  51399. return EXPECT_RESULT();
  51400. }
  51401. static int test_wolfSSL_X509_CRL_print(void)
  51402. {
  51403. EXPECT_DECLS;
  51404. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  51405. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  51406. X509_CRL* crl = NULL;
  51407. BIO *bio = NULL;
  51408. XFILE fp = XBADFILE;
  51409. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  51410. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  51411. NULL, NULL));
  51412. if (fp != XBADFILE)
  51413. XFCLOSE(fp);
  51414. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  51415. ExpectIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  51416. X509_CRL_free(crl);
  51417. BIO_free(bio);
  51418. #endif
  51419. return EXPECT_RESULT();
  51420. }
  51421. static int test_wolfSSL_BIO_get_len(void)
  51422. {
  51423. EXPECT_DECLS;
  51424. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  51425. BIO *bio = NULL;
  51426. const char txt[] = "Some example text to push to the BIO.";
  51427. ExpectIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  51428. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  51429. ExpectIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  51430. ExpectIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  51431. BIO_free(bio);
  51432. bio = NULL;
  51433. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  51434. ExpectIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  51435. BIO_free(bio);
  51436. #endif
  51437. return EXPECT_RESULT();
  51438. }
  51439. #endif /* !NO_BIO */
  51440. static int test_wolfSSL_RSA(void)
  51441. {
  51442. EXPECT_DECLS;
  51443. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  51444. RSA* rsa = NULL;
  51445. const BIGNUM *n = NULL;
  51446. const BIGNUM *e = NULL;
  51447. const BIGNUM *d = NULL;
  51448. const BIGNUM *p = NULL;
  51449. const BIGNUM *q = NULL;
  51450. const BIGNUM *dmp1 = NULL;
  51451. const BIGNUM *dmq1 = NULL;
  51452. const BIGNUM *iqmp = NULL;
  51453. ExpectNotNull(rsa = RSA_new());
  51454. ExpectIntEQ(RSA_size(NULL), 0);
  51455. ExpectIntEQ(RSA_size(rsa), 0);
  51456. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  51457. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  51458. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  51459. #ifdef WOLFSSL_RSA_KEY_CHECK
  51460. ExpectIntEQ(RSA_check_key(rsa), 0);
  51461. #endif
  51462. RSA_free(rsa);
  51463. rsa = NULL;
  51464. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  51465. ExpectIntEQ(RSA_size(rsa), 256);
  51466. #ifdef WOLFSSL_RSA_KEY_CHECK
  51467. ExpectIntEQ(RSA_check_key(NULL), 0);
  51468. ExpectIntEQ(RSA_check_key(rsa), 1);
  51469. #endif
  51470. /* sanity check */
  51471. ExpectIntEQ(RSA_bits(NULL), 0);
  51472. /* key */
  51473. ExpectIntEQ(RSA_bits(rsa), 2048);
  51474. RSA_get0_key(rsa, &n, &e, &d);
  51475. ExpectPtrEq(rsa->n, n);
  51476. ExpectPtrEq(rsa->e, e);
  51477. ExpectPtrEq(rsa->d, d);
  51478. n = NULL;
  51479. e = NULL;
  51480. d = NULL;
  51481. ExpectNotNull(n = BN_new());
  51482. ExpectNotNull(e = BN_new());
  51483. ExpectNotNull(d = BN_new());
  51484. ExpectIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  51485. if (EXPECT_FAIL()) {
  51486. BN_free((BIGNUM*)n);
  51487. BN_free((BIGNUM*)e);
  51488. BN_free((BIGNUM*)d);
  51489. }
  51490. ExpectPtrEq(rsa->n, n);
  51491. ExpectPtrEq(rsa->e, e);
  51492. ExpectPtrEq(rsa->d, d);
  51493. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  51494. ExpectIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  51495. /* crt_params */
  51496. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  51497. ExpectPtrEq(rsa->dmp1, dmp1);
  51498. ExpectPtrEq(rsa->dmq1, dmq1);
  51499. ExpectPtrEq(rsa->iqmp, iqmp);
  51500. dmp1 = NULL;
  51501. dmq1 = NULL;
  51502. iqmp = NULL;
  51503. ExpectNotNull(dmp1 = BN_new());
  51504. ExpectNotNull(dmq1 = BN_new());
  51505. ExpectNotNull(iqmp = BN_new());
  51506. ExpectIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  51507. (BIGNUM*)iqmp), 1);
  51508. if (EXPECT_FAIL()) {
  51509. BN_free((BIGNUM*)dmp1);
  51510. BN_free((BIGNUM*)dmq1);
  51511. BN_free((BIGNUM*)iqmp);
  51512. }
  51513. ExpectPtrEq(rsa->dmp1, dmp1);
  51514. ExpectPtrEq(rsa->dmq1, dmq1);
  51515. ExpectPtrEq(rsa->iqmp, iqmp);
  51516. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  51517. ExpectIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  51518. (BIGNUM*)iqmp), 0);
  51519. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  51520. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  51521. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  51522. ExpectNull(dmp1);
  51523. ExpectNull(dmq1);
  51524. ExpectNull(iqmp);
  51525. /* factors */
  51526. RSA_get0_factors(rsa, NULL, NULL);
  51527. RSA_get0_factors(rsa, &p, &q);
  51528. ExpectPtrEq(rsa->p, p);
  51529. ExpectPtrEq(rsa->q, q);
  51530. p = NULL;
  51531. q = NULL;
  51532. ExpectNotNull(p = BN_new());
  51533. ExpectNotNull(q = BN_new());
  51534. ExpectIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  51535. if (EXPECT_FAIL()) {
  51536. BN_free((BIGNUM*)p);
  51537. BN_free((BIGNUM*)q);
  51538. }
  51539. ExpectPtrEq(rsa->p, p);
  51540. ExpectPtrEq(rsa->q, q);
  51541. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  51542. ExpectIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  51543. RSA_get0_factors(NULL, NULL, NULL);
  51544. RSA_get0_factors(NULL, &p, &q);
  51545. ExpectNull(p);
  51546. ExpectNull(q);
  51547. ExpectIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  51548. ExpectIntEQ(RSA_bits(rsa), 21);
  51549. RSA_free(rsa);
  51550. rsa = NULL;
  51551. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  51552. ExpectNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  51553. ExpectIntEQ(RSA_size(rsa), 384);
  51554. ExpectIntEQ(RSA_bits(rsa), 3072);
  51555. RSA_free(rsa);
  51556. rsa = NULL;
  51557. #endif
  51558. /* remove for now with odd key size until adjusting rsa key size check with
  51559. wc_MakeRsaKey()
  51560. ExpectNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  51561. RSA_free(rsa);
  51562. rsa = NULL;
  51563. */
  51564. ExpectNull(RSA_generate_key(-1, 3, NULL, NULL));
  51565. ExpectNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  51566. ExpectNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  51567. ExpectNull(RSA_generate_key(2048, 0, NULL, NULL));
  51568. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  51569. {
  51570. byte buff[FOURK_BUF];
  51571. byte der[FOURK_BUF];
  51572. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  51573. XFILE f = XBADFILE;
  51574. int bytes = 0;
  51575. /* test loading encrypted RSA private pem w/o password */
  51576. ExpectTrue((f = XFOPEN(PrivKeyPemFile, "rb")) != XBADFILE);
  51577. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  51578. if (f != XBADFILE)
  51579. XFCLOSE(f);
  51580. XMEMSET(der, 0, sizeof(der));
  51581. /* test that error value is returned with no password */
  51582. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""),
  51583. 0);
  51584. }
  51585. #endif
  51586. #endif
  51587. return EXPECT_RESULT();
  51588. }
  51589. static int test_wolfSSL_RSA_DER(void)
  51590. {
  51591. EXPECT_DECLS;
  51592. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  51593. RSA *rsa = NULL;
  51594. int i;
  51595. const unsigned char *buff = NULL;
  51596. unsigned char *newBuff = NULL;
  51597. struct tbl_s
  51598. {
  51599. const unsigned char *der;
  51600. int sz;
  51601. } tbl[] = {
  51602. #ifdef USE_CERT_BUFFERS_1024
  51603. {client_key_der_1024, sizeof_client_key_der_1024},
  51604. {server_key_der_1024, sizeof_server_key_der_1024},
  51605. #endif
  51606. #ifdef USE_CERT_BUFFERS_2048
  51607. {client_key_der_2048, sizeof_client_key_der_2048},
  51608. {server_key_der_2048, sizeof_server_key_der_2048},
  51609. #endif
  51610. {NULL, 0}
  51611. };
  51612. /* Public Key DER */
  51613. struct tbl_s pub[] = {
  51614. #ifdef USE_CERT_BUFFERS_1024
  51615. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  51616. #endif
  51617. #ifdef USE_CERT_BUFFERS_2048
  51618. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  51619. #endif
  51620. {NULL, 0}
  51621. };
  51622. ExpectNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  51623. buff = pub[0].der;
  51624. ExpectNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  51625. ExpectNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  51626. buff = tbl[0].der;
  51627. ExpectNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  51628. ExpectIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  51629. rsa = RSA_new();
  51630. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  51631. RSA_free(rsa);
  51632. rsa = NULL;
  51633. for (i = 0; tbl[i].der != NULL; i++)
  51634. {
  51635. /* Passing in pointer results in pointer moving. */
  51636. buff = tbl[i].der;
  51637. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  51638. ExpectNotNull(rsa);
  51639. RSA_free(rsa);
  51640. rsa = NULL;
  51641. }
  51642. for (i = 0; tbl[i].der != NULL; i++)
  51643. {
  51644. /* Passing in pointer results in pointer moving. */
  51645. buff = tbl[i].der;
  51646. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  51647. ExpectNotNull(rsa);
  51648. RSA_free(rsa);
  51649. rsa = NULL;
  51650. }
  51651. for (i = 0; pub[i].der != NULL; i++)
  51652. {
  51653. buff = pub[i].der;
  51654. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  51655. ExpectNotNull(rsa);
  51656. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  51657. newBuff = NULL;
  51658. ExpectIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  51659. ExpectNotNull(newBuff);
  51660. ExpectIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  51661. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  51662. RSA_free(rsa);
  51663. rsa = NULL;
  51664. }
  51665. #endif
  51666. return EXPECT_RESULT();
  51667. }
  51668. static int test_wolfSSL_RSA_print(void)
  51669. {
  51670. EXPECT_DECLS;
  51671. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  51672. !defined(NO_STDIO_FILESYSTEM) && \
  51673. !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  51674. !defined(NO_BIO) && defined(XFPRINTF)
  51675. BIO *bio = NULL;
  51676. WOLFSSL_RSA* rsa = NULL;
  51677. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  51678. ExpectNotNull(rsa = RSA_new());
  51679. ExpectIntEQ(RSA_print(NULL, rsa, 0), -1);
  51680. ExpectIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  51681. ExpectIntEQ(RSA_print(bio, NULL, 0), -1);
  51682. ExpectIntEQ(RSA_print_fp(stderr, NULL, 0), 0);
  51683. /* Some very large number of indent spaces. */
  51684. ExpectIntEQ(RSA_print(bio, rsa, 128), -1);
  51685. /* RSA is empty. */
  51686. ExpectIntEQ(RSA_print(bio, rsa, 0), 0);
  51687. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 0);
  51688. RSA_free(rsa);
  51689. rsa = NULL;
  51690. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  51691. ExpectIntEQ(RSA_print(bio, rsa, 0), 1);
  51692. ExpectIntEQ(RSA_print(bio, rsa, 4), 1);
  51693. ExpectIntEQ(RSA_print(bio, rsa, -1), 1);
  51694. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 1);
  51695. ExpectIntEQ(RSA_print_fp(stderr, rsa, 4), 1);
  51696. ExpectIntEQ(RSA_print_fp(stderr, rsa, -1), 1);
  51697. BIO_free(bio);
  51698. RSA_free(rsa);
  51699. #endif
  51700. return EXPECT_RESULT();
  51701. }
  51702. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  51703. {
  51704. EXPECT_DECLS;
  51705. #ifndef NO_RSA
  51706. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  51707. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  51708. RSA *rsa = NULL;
  51709. const unsigned char *derBuf = client_key_der_2048;
  51710. unsigned char em[256] = {0}; /* len = 2048/8 */
  51711. /* Random data simulating a hash */
  51712. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  51713. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  51714. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  51715. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  51716. };
  51717. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  51718. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  51719. RSA_PSS_SALTLEN_DIGEST), 0);
  51720. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  51721. RSA_PSS_SALTLEN_DIGEST), 0);
  51722. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  51723. RSA_PSS_SALTLEN_DIGEST), 0);
  51724. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  51725. RSA_PSS_SALTLEN_DIGEST), 0);
  51726. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  51727. ExpectIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  51728. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  51729. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  51730. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  51731. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  51732. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  51733. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  51734. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  51735. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  51736. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  51737. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  51738. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  51739. RSA_PSS_SALTLEN_DIGEST), 1);
  51740. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  51741. RSA_PSS_SALTLEN_DIGEST), 1);
  51742. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  51743. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  51744. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  51745. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  51746. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  51747. RSA_PSS_SALTLEN_MAX), 1);
  51748. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  51749. RSA_PSS_SALTLEN_MAX), 1);
  51750. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  51751. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  51752. RSA_free(rsa);
  51753. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  51754. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  51755. #endif
  51756. return EXPECT_RESULT();
  51757. }
  51758. static int test_wolfSSL_RSA_sign_sha3(void)
  51759. {
  51760. EXPECT_DECLS;
  51761. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  51762. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  51763. RSA* rsa = NULL;
  51764. const unsigned char *derBuf = client_key_der_2048;
  51765. unsigned char sigRet[256] = {0};
  51766. unsigned int sigLen = sizeof(sigRet);
  51767. /* Random data simulating a hash */
  51768. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  51769. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  51770. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  51771. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  51772. };
  51773. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  51774. ExpectIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet, &sigLen,
  51775. rsa), 1);
  51776. RSA_free(rsa);
  51777. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  51778. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  51779. return EXPECT_RESULT();
  51780. }
  51781. static int test_wolfSSL_RSA_get0_key(void)
  51782. {
  51783. EXPECT_DECLS;
  51784. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51785. RSA *rsa = NULL;
  51786. const BIGNUM* n = NULL;
  51787. const BIGNUM* e = NULL;
  51788. const BIGNUM* d = NULL;
  51789. const unsigned char* der;
  51790. int derSz;
  51791. #ifdef USE_CERT_BUFFERS_1024
  51792. der = client_key_der_1024;
  51793. derSz = sizeof_client_key_der_1024;
  51794. #elif defined(USE_CERT_BUFFERS_2048)
  51795. der = client_key_der_2048;
  51796. derSz = sizeof_client_key_der_2048;
  51797. #else
  51798. der = NULL;
  51799. derSz = 0;
  51800. #endif
  51801. if (der != NULL) {
  51802. RSA_get0_key(NULL, NULL, NULL, NULL);
  51803. RSA_get0_key(rsa, NULL, NULL, NULL);
  51804. RSA_get0_key(NULL, &n, &e, &d);
  51805. ExpectNull(n);
  51806. ExpectNull(e);
  51807. ExpectNull(d);
  51808. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  51809. ExpectNotNull(rsa);
  51810. RSA_get0_key(rsa, NULL, NULL, NULL);
  51811. RSA_get0_key(rsa, &n, NULL, NULL);
  51812. ExpectNotNull(n);
  51813. RSA_get0_key(rsa, NULL, &e, NULL);
  51814. ExpectNotNull(e);
  51815. RSA_get0_key(rsa, NULL, NULL, &d);
  51816. ExpectNotNull(d);
  51817. RSA_get0_key(rsa, &n, &e, &d);
  51818. ExpectNotNull(n);
  51819. ExpectNotNull(e);
  51820. ExpectNotNull(d);
  51821. RSA_free(rsa);
  51822. }
  51823. #endif
  51824. return EXPECT_RESULT();
  51825. }
  51826. static int test_wolfSSL_RSA_meth(void)
  51827. {
  51828. EXPECT_DECLS;
  51829. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51830. RSA *rsa = NULL;
  51831. RSA_METHOD *rsa_meth = NULL;
  51832. #ifdef WOLFSSL_KEY_GEN
  51833. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  51834. RSA_free(rsa);
  51835. rsa = NULL;
  51836. #else
  51837. ExpectNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  51838. #endif
  51839. ExpectNotNull(RSA_get_default_method());
  51840. wolfSSL_RSA_meth_free(NULL);
  51841. ExpectNull(wolfSSL_RSA_meth_new(NULL, 0));
  51842. ExpectNotNull(rsa_meth = RSA_meth_new("placeholder RSA method",
  51843. RSA_METHOD_FLAG_NO_CHECK));
  51844. #ifndef NO_WOLFSSL_STUB
  51845. ExpectIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  51846. ExpectIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  51847. ExpectIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  51848. ExpectIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  51849. ExpectIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  51850. ExpectIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  51851. ExpectIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  51852. #endif
  51853. ExpectIntEQ(RSA_flags(NULL), 0);
  51854. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  51855. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  51856. ExpectIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  51857. ExpectNotNull(rsa = RSA_new());
  51858. /* No method set. */
  51859. ExpectIntEQ(RSA_flags(rsa), 0);
  51860. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  51861. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  51862. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  51863. ExpectIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  51864. ExpectIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  51865. if (EXPECT_FAIL()) {
  51866. wolfSSL_RSA_meth_free(rsa_meth);
  51867. }
  51868. ExpectNull(RSA_get_method(NULL));
  51869. ExpectPtrEq(RSA_get_method(rsa), rsa_meth);
  51870. ExpectIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  51871. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  51872. ExpectIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  51873. ExpectIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  51874. RSA_METHOD_FLAG_NO_CHECK);
  51875. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  51876. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  51877. ExpectIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  51878. /* rsa_meth is freed here */
  51879. RSA_free(rsa);
  51880. #endif
  51881. return EXPECT_RESULT();
  51882. }
  51883. static int test_wolfSSL_RSA_verify(void)
  51884. {
  51885. EXPECT_DECLS;
  51886. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  51887. #ifndef NO_BIO
  51888. XFILE fp = XBADFILE;
  51889. RSA *pKey = NULL;
  51890. RSA *pubKey = NULL;
  51891. X509 *cert = NULL;
  51892. const char *text = "Hello wolfSSL !";
  51893. unsigned char hash[SHA256_DIGEST_LENGTH];
  51894. unsigned char signature[2048/8];
  51895. unsigned int signatureLength;
  51896. byte *buf = NULL;
  51897. BIO *bio = NULL;
  51898. SHA256_CTX c;
  51899. EVP_PKEY *evpPkey = NULL;
  51900. EVP_PKEY *evpPubkey = NULL;
  51901. size_t sz;
  51902. /* generate hash */
  51903. SHA256_Init(&c);
  51904. SHA256_Update(&c, text, strlen(text));
  51905. SHA256_Final(hash, &c);
  51906. #ifdef WOLFSSL_SMALL_STACK_CACHE
  51907. /* workaround for small stack cache case */
  51908. wc_Sha256Free((wc_Sha256*)&c);
  51909. #endif
  51910. /* read privete key file */
  51911. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  51912. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_END), 0);
  51913. ExpectTrue((sz = XFTELL(fp)) > 0);
  51914. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_SET), 0);
  51915. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  51916. ExpectIntEQ(XFREAD(buf, 1, sz, fp), sz);
  51917. if (fp != XBADFILE) {
  51918. XFCLOSE(fp);
  51919. fp = XBADFILE;
  51920. }
  51921. /* read private key and sign hash data */
  51922. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  51923. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  51924. ExpectNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  51925. ExpectIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  51926. signature, &signatureLength, pKey), SSL_SUCCESS);
  51927. /* read public key and verify signed data */
  51928. ExpectTrue((fp = XFOPEN(svrCertFile,"rb")) != XBADFILE);
  51929. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  51930. if (fp != XBADFILE)
  51931. XFCLOSE(fp);
  51932. ExpectNotNull(evpPubkey = X509_get_pubkey(cert));
  51933. ExpectNotNull(pubKey = EVP_PKEY_get1_RSA(evpPubkey));
  51934. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  51935. signatureLength, pubKey), SSL_SUCCESS);
  51936. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  51937. signatureLength, NULL), SSL_FAILURE);
  51938. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  51939. signatureLength, pubKey), SSL_FAILURE);
  51940. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  51941. signatureLength, pubKey), SSL_FAILURE);
  51942. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  51943. signatureLength, NULL), SSL_FAILURE);
  51944. RSA_free(pKey);
  51945. EVP_PKEY_free(evpPkey);
  51946. RSA_free(pubKey);
  51947. EVP_PKEY_free(evpPubkey);
  51948. X509_free(cert);
  51949. BIO_free(bio);
  51950. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  51951. #endif
  51952. #endif
  51953. return EXPECT_RESULT();
  51954. }
  51955. static int test_wolfSSL_RSA_sign(void)
  51956. {
  51957. EXPECT_DECLS;
  51958. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  51959. RSA *rsa;
  51960. unsigned char hash[SHA256_DIGEST_LENGTH];
  51961. #ifdef USE_CERT_BUFFERS_1024
  51962. const unsigned char* privDer = client_key_der_1024;
  51963. size_t privDerSz = sizeof_client_key_der_1024;
  51964. const unsigned char* pubDer = client_keypub_der_1024;
  51965. size_t pubDerSz = sizeof_client_keypub_der_1024;
  51966. unsigned char signature[1024/8];
  51967. #else
  51968. const unsigned char* privDer = client_key_der_2048;
  51969. size_t privDerSz = sizeof_client_key_der_2048;
  51970. const unsigned char* pubDer = client_keypub_der_2048;
  51971. size_t pubDerSz = sizeof_client_keypub_der_2048;
  51972. unsigned char signature[2048/8];
  51973. #endif
  51974. unsigned int signatureLen;
  51975. const unsigned char* der;
  51976. XMEMSET(hash, 0, sizeof(hash));
  51977. der = privDer;
  51978. rsa = NULL;
  51979. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  51980. /* Invalid parameters. */
  51981. ExpectIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  51982. ExpectIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  51983. &signatureLen, rsa), 0);
  51984. ExpectIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  51985. &signatureLen, rsa), 0);
  51986. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  51987. &signatureLen, rsa), 0);
  51988. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  51989. NULL, rsa), 0);
  51990. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  51991. &signatureLen, NULL), 0);
  51992. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  51993. &signatureLen, rsa), 1);
  51994. RSA_free(rsa);
  51995. der = pubDer;
  51996. rsa = NULL;
  51997. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  51998. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  51999. signatureLen, rsa), 1);
  52000. RSA_free(rsa);
  52001. #endif
  52002. return EXPECT_RESULT();
  52003. }
  52004. static int test_wolfSSL_RSA_sign_ex(void)
  52005. {
  52006. EXPECT_DECLS;
  52007. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52008. RSA *rsa = NULL;
  52009. unsigned char hash[SHA256_DIGEST_LENGTH];
  52010. #ifdef USE_CERT_BUFFERS_1024
  52011. const unsigned char* privDer = client_key_der_1024;
  52012. size_t privDerSz = sizeof_client_key_der_1024;
  52013. const unsigned char* pubDer = client_keypub_der_1024;
  52014. size_t pubDerSz = sizeof_client_keypub_der_1024;
  52015. unsigned char signature[1024/8];
  52016. #else
  52017. const unsigned char* privDer = client_key_der_2048;
  52018. size_t privDerSz = sizeof_client_key_der_2048;
  52019. const unsigned char* pubDer = client_keypub_der_2048;
  52020. size_t pubDerSz = sizeof_client_keypub_der_2048;
  52021. unsigned char signature[2048/8];
  52022. #endif
  52023. unsigned int signatureLen;
  52024. const unsigned char* der;
  52025. unsigned char encodedHash[51];
  52026. unsigned int encodedHashLen;
  52027. const unsigned char expEncHash[] = {
  52028. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  52029. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  52030. 0x00, 0x04, 0x20,
  52031. /* Hash data */
  52032. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52033. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52034. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52035. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52036. };
  52037. XMEMSET(hash, 0, sizeof(hash));
  52038. ExpectNotNull(rsa = wolfSSL_RSA_new());
  52039. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52040. &signatureLen, rsa, 1), 0);
  52041. wolfSSL_RSA_free(rsa);
  52042. der = privDer;
  52043. rsa = NULL;
  52044. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52045. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  52046. -1), 0);
  52047. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  52048. signature, &signatureLen, rsa, 1), 0);
  52049. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  52050. &signatureLen, rsa, 1), 0);
  52051. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  52052. &signatureLen, rsa, 1), 0);
  52053. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52054. NULL, rsa, 1), 0);
  52055. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52056. &signatureLen, NULL, 1), 0);
  52057. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52058. &signatureLen, rsa, -1), 0);
  52059. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  52060. &signatureLen, rsa, 0), 0);
  52061. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  52062. &signatureLen, rsa, 0), 0);
  52063. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52064. NULL, rsa, 0), 0);
  52065. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52066. &signatureLen, rsa, 1), 1);
  52067. /* Test returning encoded hash. */
  52068. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  52069. &encodedHashLen, rsa, 0), 1);
  52070. ExpectIntEQ(encodedHashLen, sizeof(expEncHash));
  52071. ExpectIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  52072. RSA_free(rsa);
  52073. der = pubDer;
  52074. rsa = NULL;
  52075. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  52076. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  52077. signatureLen, rsa), 1);
  52078. RSA_free(rsa);
  52079. #endif
  52080. return EXPECT_RESULT();
  52081. }
  52082. static int test_wolfSSL_RSA_public_decrypt(void)
  52083. {
  52084. EXPECT_DECLS;
  52085. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52086. RSA *rsa;
  52087. unsigned char msg[SHA256_DIGEST_LENGTH];
  52088. #ifdef USE_CERT_BUFFERS_1024
  52089. const unsigned char* pubDer = client_keypub_der_1024;
  52090. size_t pubDerSz = sizeof_client_keypub_der_1024;
  52091. unsigned char decMsg[1024/8];
  52092. const unsigned char encMsg[] = {
  52093. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  52094. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  52095. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  52096. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  52097. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  52098. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  52099. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  52100. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  52101. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  52102. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  52103. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  52104. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  52105. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  52106. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  52107. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  52108. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  52109. };
  52110. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  52111. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  52112. defined(WC_RSA_NO_PADDING)
  52113. const unsigned char encMsgNoPad[] = {
  52114. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  52115. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  52116. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  52117. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  52118. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  52119. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  52120. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  52121. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  52122. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  52123. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  52124. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  52125. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  52126. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  52127. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  52128. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  52129. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  52130. };
  52131. #endif
  52132. #else
  52133. const unsigned char* pubDer = client_keypub_der_2048;
  52134. size_t pubDerSz = sizeof_client_keypub_der_2048;
  52135. unsigned char decMsg[2048/8];
  52136. const unsigned char encMsg[] = {
  52137. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  52138. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  52139. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  52140. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  52141. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  52142. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  52143. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  52144. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  52145. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  52146. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  52147. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  52148. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  52149. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  52150. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  52151. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  52152. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  52153. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  52154. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  52155. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  52156. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  52157. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  52158. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  52159. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  52160. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  52161. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  52162. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  52163. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  52164. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  52165. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  52166. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  52167. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  52168. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  52169. };
  52170. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  52171. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  52172. defined(WC_RSA_NO_PADDING)
  52173. const unsigned char encMsgNoPad[] = {
  52174. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  52175. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  52176. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  52177. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  52178. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  52179. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  52180. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  52181. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  52182. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  52183. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  52184. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  52185. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  52186. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  52187. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  52188. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  52189. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  52190. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  52191. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  52192. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  52193. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  52194. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  52195. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  52196. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  52197. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  52198. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  52199. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  52200. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  52201. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  52202. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  52203. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  52204. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  52205. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  52206. };
  52207. #endif
  52208. #endif
  52209. const unsigned char* der;
  52210. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  52211. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  52212. defined(WC_RSA_NO_PADDING)
  52213. int i;
  52214. #endif
  52215. XMEMSET(msg, 0, sizeof(msg));
  52216. der = pubDer;
  52217. rsa = NULL;
  52218. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  52219. ExpectIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  52220. ExpectIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  52221. RSA_PKCS1_PADDING), -1);
  52222. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  52223. RSA_PKCS1_PADDING), -1);
  52224. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  52225. RSA_PKCS1_PADDING), -1);
  52226. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  52227. RSA_PKCS1_PADDING), -1);
  52228. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  52229. RSA_PKCS1_PSS_PADDING), -1);
  52230. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  52231. RSA_PKCS1_PADDING), 32);
  52232. ExpectIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  52233. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  52234. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  52235. defined(WC_RSA_NO_PADDING)
  52236. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  52237. rsa, RSA_NO_PADDING), sizeof(decMsg));
  52238. /* Zeros before actual data. */
  52239. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  52240. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  52241. }
  52242. /* Check actual data. */
  52243. XMEMSET(msg, 0x01, sizeof(msg));
  52244. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  52245. #endif
  52246. RSA_free(rsa);
  52247. #endif
  52248. return EXPECT_RESULT();
  52249. }
  52250. static int test_wolfSSL_RSA_private_encrypt(void)
  52251. {
  52252. EXPECT_DECLS;
  52253. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52254. RSA *rsa;
  52255. unsigned char msg[SHA256_DIGEST_LENGTH];
  52256. #ifdef USE_CERT_BUFFERS_1024
  52257. const unsigned char* privDer = client_key_der_1024;
  52258. size_t privDerSz = sizeof_client_key_der_1024;
  52259. unsigned char encMsg[1024/8];
  52260. const unsigned char expEncMsg[] = {
  52261. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  52262. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  52263. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  52264. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  52265. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  52266. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  52267. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  52268. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  52269. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  52270. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  52271. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  52272. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  52273. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  52274. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  52275. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  52276. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  52277. };
  52278. #ifdef WC_RSA_NO_PADDING
  52279. const unsigned char expEncMsgNoPad[] = {
  52280. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  52281. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  52282. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  52283. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  52284. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  52285. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  52286. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  52287. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  52288. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  52289. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  52290. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  52291. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  52292. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  52293. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  52294. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  52295. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  52296. };
  52297. #endif
  52298. #else
  52299. const unsigned char* privDer = client_key_der_2048;
  52300. size_t privDerSz = sizeof_client_key_der_2048;
  52301. unsigned char encMsg[2048/8];
  52302. const unsigned char expEncMsg[] = {
  52303. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  52304. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  52305. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  52306. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  52307. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  52308. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  52309. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  52310. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  52311. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  52312. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  52313. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  52314. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  52315. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  52316. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  52317. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  52318. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  52319. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  52320. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  52321. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  52322. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  52323. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  52324. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  52325. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  52326. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  52327. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  52328. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  52329. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  52330. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  52331. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  52332. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  52333. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  52334. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  52335. };
  52336. #ifdef WC_RSA_NO_PADDING
  52337. const unsigned char expEncMsgNoPad[] = {
  52338. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  52339. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  52340. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  52341. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  52342. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  52343. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  52344. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  52345. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  52346. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  52347. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  52348. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  52349. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  52350. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  52351. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  52352. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  52353. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  52354. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  52355. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  52356. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  52357. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  52358. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  52359. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  52360. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  52361. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  52362. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  52363. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  52364. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  52365. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  52366. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  52367. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  52368. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  52369. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  52370. };
  52371. #endif
  52372. #endif
  52373. const unsigned char* der;
  52374. XMEMSET(msg, 0x00, sizeof(msg));
  52375. der = privDer;
  52376. rsa = NULL;
  52377. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52378. ExpectIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  52379. ExpectIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  52380. -1);
  52381. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  52382. RSA_PKCS1_PADDING), -1);
  52383. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  52384. RSA_PKCS1_PADDING), -1);
  52385. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  52386. RSA_PKCS1_PADDING), -1);
  52387. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  52388. RSA_PKCS1_PSS_PADDING), -1);
  52389. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  52390. RSA_PKCS1_PADDING), sizeof(encMsg));
  52391. ExpectIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  52392. #ifdef WC_RSA_NO_PADDING
  52393. /* Non-zero message. */
  52394. XMEMSET(msg, 0x01, sizeof(msg));
  52395. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  52396. RSA_NO_PADDING), sizeof(encMsg));
  52397. ExpectIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  52398. #endif
  52399. RSA_free(rsa);
  52400. #endif
  52401. return EXPECT_RESULT();
  52402. }
  52403. static int test_wolfSSL_RSA_public_encrypt(void)
  52404. {
  52405. EXPECT_DECLS;
  52406. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52407. RSA* rsa = NULL;
  52408. const unsigned char msg[2048/8] = { 0 };
  52409. unsigned char encMsg[2048/8];
  52410. ExpectNotNull(rsa = RSA_new());
  52411. ExpectIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  52412. RSA_PKCS1_PADDING), -1);
  52413. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  52414. RSA_PKCS1_PADDING), -1);
  52415. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  52416. RSA_PKCS1_PADDING), -1);
  52417. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  52418. RSA_PKCS1_PADDING), -1);
  52419. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  52420. RSA_PKCS1_PSS_PADDING), -1);
  52421. /* Empty RSA key. */
  52422. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  52423. RSA_PKCS1_PADDING), -1);
  52424. RSA_free(rsa);
  52425. #endif
  52426. return EXPECT_RESULT();
  52427. }
  52428. static int test_wolfSSL_RSA_private_decrypt(void)
  52429. {
  52430. EXPECT_DECLS;
  52431. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52432. RSA* rsa = NULL;
  52433. unsigned char msg[2048/8];
  52434. const unsigned char encMsg[2048/8] = { 0 };
  52435. ExpectNotNull(rsa = RSA_new());
  52436. ExpectIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  52437. RSA_PKCS1_PADDING), -1);
  52438. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  52439. RSA_PKCS1_PADDING), -1);
  52440. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  52441. RSA_PKCS1_PADDING), -1);
  52442. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  52443. RSA_PKCS1_PADDING), -1);
  52444. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  52445. RSA_PKCS1_PSS_PADDING), -1);
  52446. /* Empty RSA key. */
  52447. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  52448. RSA_PKCS1_PADDING), -1);
  52449. RSA_free(rsa);
  52450. #endif
  52451. return EXPECT_RESULT();
  52452. }
  52453. static int test_wolfSSL_RSA_GenAdd(void)
  52454. {
  52455. EXPECT_DECLS;
  52456. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52457. RSA *rsa;
  52458. #ifdef USE_CERT_BUFFERS_1024
  52459. const unsigned char* privDer = client_key_der_1024;
  52460. size_t privDerSz = sizeof_client_key_der_1024;
  52461. const unsigned char* pubDer = client_keypub_der_1024;
  52462. size_t pubDerSz = sizeof_client_keypub_der_1024;
  52463. #else
  52464. const unsigned char* privDer = client_key_der_2048;
  52465. size_t privDerSz = sizeof_client_key_der_2048;
  52466. const unsigned char* pubDer = client_keypub_der_2048;
  52467. size_t pubDerSz = sizeof_client_keypub_der_2048;
  52468. #endif
  52469. const unsigned char* der;
  52470. der = privDer;
  52471. rsa = NULL;
  52472. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52473. ExpectIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  52474. #ifndef RSA_LOW_MEM
  52475. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  52476. #else
  52477. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  52478. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  52479. #endif
  52480. RSA_free(rsa);
  52481. der = pubDer;
  52482. rsa = NULL;
  52483. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  52484. /* Need private values. */
  52485. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  52486. RSA_free(rsa);
  52487. #endif
  52488. return EXPECT_RESULT();
  52489. }
  52490. static int test_wolfSSL_RSA_blinding_on(void)
  52491. {
  52492. EXPECT_DECLS;
  52493. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  52494. RSA *rsa;
  52495. WOLFSSL_BN_CTX *bnCtx = NULL;
  52496. #ifdef USE_CERT_BUFFERS_1024
  52497. const unsigned char* privDer = client_key_der_1024;
  52498. size_t privDerSz = sizeof_client_key_der_1024;
  52499. #else
  52500. const unsigned char* privDer = client_key_der_2048;
  52501. size_t privDerSz = sizeof_client_key_der_2048;
  52502. #endif
  52503. const unsigned char* der;
  52504. der = privDer;
  52505. rsa = NULL;
  52506. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52507. ExpectNotNull(bnCtx = wolfSSL_BN_CTX_new());
  52508. /* Does nothing so all parameters are valid. */
  52509. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  52510. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  52511. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  52512. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  52513. wolfSSL_BN_CTX_free(bnCtx);
  52514. RSA_free(rsa);
  52515. #endif
  52516. return EXPECT_RESULT();
  52517. }
  52518. static int test_wolfSSL_RSA_ex_data(void)
  52519. {
  52520. EXPECT_DECLS;
  52521. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  52522. RSA* rsa = NULL;
  52523. unsigned char data[1];
  52524. ExpectNotNull(rsa = RSA_new());
  52525. ExpectNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  52526. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  52527. #ifdef MAX_EX_DATA
  52528. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  52529. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  52530. #endif
  52531. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  52532. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  52533. #ifdef HAVE_EX_DATA
  52534. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  52535. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  52536. ExpectPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  52537. #else
  52538. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  52539. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  52540. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  52541. #endif
  52542. RSA_free(rsa);
  52543. #endif /* !NO_RSA && OPENSSL_EXTRA */
  52544. return EXPECT_RESULT();
  52545. }
  52546. static int test_wolfSSL_RSA_LoadDer(void)
  52547. {
  52548. EXPECT_DECLS;
  52549. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  52550. defined(OPENSSL_EXTRA_X509_SMALL))
  52551. RSA *rsa = NULL;
  52552. #ifdef USE_CERT_BUFFERS_1024
  52553. const unsigned char* privDer = client_key_der_1024;
  52554. size_t privDerSz = sizeof_client_key_der_1024;
  52555. #else
  52556. const unsigned char* privDer = client_key_der_2048;
  52557. size_t privDerSz = sizeof_client_key_der_2048;
  52558. #endif
  52559. ExpectNotNull(rsa = RSA_new());
  52560. ExpectIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  52561. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  52562. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  52563. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  52564. RSA_free(rsa);
  52565. #endif /* !NO_RSA && OPENSSL_EXTRA */
  52566. return EXPECT_RESULT();
  52567. }
  52568. /* Local API. */
  52569. static int test_wolfSSL_RSA_To_Der(void)
  52570. {
  52571. EXPECT_DECLS;
  52572. #ifdef WOLFSSL_TEST_STATIC_BUILD
  52573. #if defined(WOLFSSL_KEY_GEN) && defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52574. RSA* rsa;
  52575. #ifdef USE_CERT_BUFFERS_1024
  52576. const unsigned char* privDer = client_key_der_1024;
  52577. size_t privDerSz = sizeof_client_key_der_1024;
  52578. const unsigned char* pubDer = client_keypub_der_1024;
  52579. size_t pubDerSz = sizeof_client_keypub_der_1024;
  52580. unsigned char out[sizeof(client_key_der_1024)];
  52581. #else
  52582. const unsigned char* privDer = client_key_der_2048;
  52583. size_t privDerSz = sizeof_client_key_der_2048;
  52584. const unsigned char* pubDer = client_keypub_der_2048;
  52585. size_t pubDerSz = sizeof_client_keypub_der_2048;
  52586. unsigned char out[sizeof(client_key_der_2048)];
  52587. #endif
  52588. const unsigned char* der;
  52589. unsigned char* outDer = NULL;
  52590. der = privDer;
  52591. rsa = NULL;
  52592. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52593. ExpectIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  52594. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  52595. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  52596. outDer = out;
  52597. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  52598. ExpectIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  52599. outDer = NULL;
  52600. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  52601. ExpectNotNull(outDer);
  52602. ExpectIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  52603. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  52604. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  52605. outDer = out;
  52606. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  52607. ExpectIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  52608. RSA_free(rsa);
  52609. ExpectNotNull(rsa = RSA_new());
  52610. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  52611. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  52612. RSA_free(rsa);
  52613. der = pubDer;
  52614. rsa = NULL;
  52615. ExpectNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  52616. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  52617. RSA_free(rsa);
  52618. #endif
  52619. #endif
  52620. return EXPECT_RESULT();
  52621. }
  52622. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  52623. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  52624. {
  52625. EXPECT_DECLS;
  52626. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  52627. XFILE file = XBADFILE;
  52628. const char* fname = "./certs/server-keyPub.pem";
  52629. RSA *rsa = NULL;
  52630. ExpectNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  52631. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  52632. ExpectNotNull(rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL));
  52633. ExpectIntEQ(RSA_size(rsa), 256);
  52634. RSA_free(rsa);
  52635. if (file != XBADFILE)
  52636. XFCLOSE(file);
  52637. #endif
  52638. return EXPECT_RESULT();
  52639. }
  52640. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  52641. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  52642. {
  52643. EXPECT_DECLS;
  52644. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  52645. defined(WOLFSSL_KEY_GEN)
  52646. RSA* rsa = NULL;
  52647. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  52648. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, NULL), 0);
  52649. /* Valid but stub so returns 0. */
  52650. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, rsa), 0);
  52651. #endif
  52652. return EXPECT_RESULT();
  52653. }
  52654. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  52655. {
  52656. EXPECT_DECLS;
  52657. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  52658. (defined(WOLFSSL_PEM_TO_DER) || \
  52659. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  52660. RSA* rsa = NULL;
  52661. #ifdef USE_CERT_BUFFERS_1024
  52662. const unsigned char* privDer = client_key_der_1024;
  52663. size_t privDerSz = sizeof_client_key_der_1024;
  52664. #else
  52665. const unsigned char* privDer = client_key_der_2048;
  52666. size_t privDerSz = sizeof_client_key_der_2048;
  52667. #endif
  52668. const unsigned char* der;
  52669. #ifndef NO_AES
  52670. unsigned char passwd[] = "password";
  52671. #endif
  52672. ExpectNotNull(rsa = RSA_new());
  52673. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  52674. NULL, NULL), 0);
  52675. RSA_free(rsa);
  52676. der = privDer;
  52677. rsa = NULL;
  52678. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52679. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  52680. NULL, NULL), 0);
  52681. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0,
  52682. NULL, NULL), 0);
  52683. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  52684. NULL, NULL), 1);
  52685. #ifndef NO_AES
  52686. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  52687. NULL, 0, NULL, NULL), 1);
  52688. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  52689. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  52690. #endif
  52691. RSA_free(rsa);
  52692. #endif
  52693. return EXPECT_RESULT();
  52694. }
  52695. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  52696. {
  52697. EXPECT_DECLS;
  52698. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  52699. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  52700. RSA* rsa = NULL;
  52701. #ifdef USE_CERT_BUFFERS_1024
  52702. const unsigned char* privDer = client_key_der_1024;
  52703. size_t privDerSz = sizeof_client_key_der_1024;
  52704. #else
  52705. const unsigned char* privDer = client_key_der_2048;
  52706. size_t privDerSz = sizeof_client_key_der_2048;
  52707. #endif
  52708. const unsigned char* der;
  52709. #ifndef NO_AES
  52710. unsigned char passwd[] = "password";
  52711. #endif
  52712. unsigned char* pem = NULL;
  52713. int plen;
  52714. ExpectNotNull(rsa = RSA_new());
  52715. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  52716. &plen), 0);
  52717. RSA_free(rsa);
  52718. der = privDer;
  52719. rsa = NULL;
  52720. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52721. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  52722. &plen), 0);
  52723. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  52724. &plen), 0);
  52725. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  52726. NULL), 0);
  52727. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  52728. &plen), 1);
  52729. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  52730. pem = NULL;
  52731. #ifndef NO_AES
  52732. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  52733. NULL, 0, &pem, &plen), 1);
  52734. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  52735. pem = NULL;
  52736. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  52737. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  52738. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  52739. #endif
  52740. RSA_free(rsa);
  52741. #endif
  52742. return EXPECT_RESULT();
  52743. }
  52744. static int test_wolfSSL_DH(void)
  52745. {
  52746. EXPECT_DECLS;
  52747. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  52748. DH *dh = NULL;
  52749. BIGNUM* p;
  52750. BIGNUM* q;
  52751. BIGNUM* g;
  52752. BIGNUM* pub = NULL;
  52753. BIGNUM* priv = NULL;
  52754. #if defined(OPENSSL_ALL)
  52755. #if !defined(HAVE_FIPS) || \
  52756. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  52757. FILE* f = NULL;
  52758. unsigned char buf[268];
  52759. const unsigned char* pt = buf;
  52760. long len = 0;
  52761. dh = NULL;
  52762. XMEMSET(buf, 0, sizeof(buf));
  52763. /* Test 2048 bit parameters */
  52764. ExpectTrue((f = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  52765. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  52766. if (f != XBADFILE)
  52767. XFCLOSE(f);
  52768. ExpectNotNull(dh = d2i_DHparams(NULL, &pt, len));
  52769. ExpectNotNull(dh->p);
  52770. ExpectNotNull(dh->g);
  52771. ExpectTrue(pt == buf);
  52772. ExpectIntEQ(DH_generate_key(dh), 1);
  52773. ExpectIntEQ(DH_generate_key(dh), 1);
  52774. ExpectIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
  52775. ExpectNotNull(pub = BN_new());
  52776. ExpectIntEQ(BN_set_word(pub, 1), 1);
  52777. ExpectIntEQ(DH_compute_key(buf, NULL, NULL), -1);
  52778. ExpectIntEQ(DH_compute_key(NULL, pub, NULL), -1);
  52779. ExpectIntEQ(DH_compute_key(NULL, NULL, dh), -1);
  52780. ExpectIntEQ(DH_compute_key(buf, pub, NULL), -1);
  52781. ExpectIntEQ(DH_compute_key(buf, NULL, dh), -1);
  52782. ExpectIntEQ(DH_compute_key(NULL, pub, dh), -1);
  52783. ExpectIntEQ(DH_compute_key(buf, pub, dh), -1);
  52784. BN_free(pub);
  52785. pub = NULL;
  52786. DH_get0_pqg(dh, (const BIGNUM**)&p,
  52787. (const BIGNUM**)&q,
  52788. (const BIGNUM**)&g);
  52789. ExpectPtrEq(p, dh->p);
  52790. ExpectPtrEq(q, dh->q);
  52791. ExpectPtrEq(g, dh->g);
  52792. DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  52793. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  52794. ExpectPtrEq(pub, dh->pub_key);
  52795. ExpectPtrEq(priv, dh->priv_key);
  52796. DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
  52797. ExpectPtrEq(pub, dh->pub_key);
  52798. DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
  52799. ExpectPtrEq(priv, dh->priv_key);
  52800. pub = NULL;
  52801. priv = NULL;
  52802. ExpectNotNull(pub = BN_new());
  52803. ExpectNotNull(priv = BN_new());
  52804. ExpectIntEQ(DH_set0_key(NULL, pub, priv), 0);
  52805. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  52806. if (EXPECT_FAIL()) {
  52807. BN_free(pub);
  52808. BN_free(priv);
  52809. }
  52810. pub = NULL;
  52811. priv = NULL;
  52812. ExpectNotNull(pub = BN_new());
  52813. ExpectIntEQ(DH_set0_key(dh, pub, NULL), 1);
  52814. if (EXPECT_FAIL()) {
  52815. BN_free(pub);
  52816. }
  52817. ExpectNotNull(priv = BN_new());
  52818. ExpectIntEQ(DH_set0_key(dh, NULL, priv), 1);
  52819. if (EXPECT_FAIL()) {
  52820. BN_free(priv);
  52821. }
  52822. ExpectPtrEq(pub, dh->pub_key);
  52823. ExpectPtrEq(priv, dh->priv_key);
  52824. pub = NULL;
  52825. priv = NULL;
  52826. DH_free(dh);
  52827. dh = NULL;
  52828. ExpectNotNull(dh = DH_new());
  52829. p = NULL;
  52830. ExpectNotNull(p = BN_new());
  52831. ExpectIntEQ(BN_set_word(p, 1), 1);
  52832. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  52833. ExpectNotNull(pub = BN_new());
  52834. ExpectNotNull(priv = BN_new());
  52835. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  52836. if (EXPECT_FAIL()) {
  52837. BN_free(pub);
  52838. BN_free(priv);
  52839. }
  52840. pub = NULL;
  52841. priv = NULL;
  52842. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  52843. BN_free(p);
  52844. p = NULL;
  52845. DH_free(dh);
  52846. dh = NULL;
  52847. #ifdef WOLFSSL_KEY_GEN
  52848. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  52849. ExpectIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
  52850. DH_free(dh);
  52851. dh = NULL;
  52852. #endif
  52853. #endif /* !HAVE_FIPS || (HAVE_FIPS_VERSION && HAVE_FIPS_VERSION > 2) */
  52854. #endif /* OPENSSL_ALL */
  52855. (void)dh;
  52856. (void)p;
  52857. (void)q;
  52858. (void)g;
  52859. (void)pub;
  52860. (void)priv;
  52861. ExpectNotNull(dh = wolfSSL_DH_new());
  52862. /* invalid parameters test */
  52863. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  52864. (const BIGNUM**)&q,
  52865. (const BIGNUM**)&g);
  52866. DH_get0_pqg(dh, NULL,
  52867. (const BIGNUM**)&q,
  52868. (const BIGNUM**)&g);
  52869. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  52870. DH_get0_pqg(dh, NULL, NULL, NULL);
  52871. DH_get0_pqg(dh, (const BIGNUM**)&p,
  52872. (const BIGNUM**)&q,
  52873. (const BIGNUM**)&g);
  52874. ExpectPtrEq(p, NULL);
  52875. ExpectPtrEq(q, NULL);
  52876. ExpectPtrEq(g, NULL);
  52877. DH_free(dh);
  52878. dh = NULL;
  52879. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  52880. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  52881. #if defined(OPENSSL_ALL) || \
  52882. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  52883. dh = wolfSSL_DH_new();
  52884. ExpectNotNull(dh);
  52885. p = wolfSSL_BN_new();
  52886. ExpectNotNull(p);
  52887. ExpectIntEQ(BN_set_word(p, 11), 1);
  52888. g = wolfSSL_BN_new();
  52889. ExpectNotNull(g);
  52890. ExpectIntEQ(BN_set_word(g, 2), 1);
  52891. q = wolfSSL_BN_new();
  52892. ExpectNotNull(q);
  52893. ExpectIntEQ(BN_set_word(q, 5), 1);
  52894. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
  52895. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
  52896. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
  52897. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
  52898. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
  52899. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
  52900. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
  52901. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
  52902. /* Don't need q. */
  52903. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  52904. if (EXPECT_FAIL()) {
  52905. BN_free(p);
  52906. BN_free(g);
  52907. }
  52908. p = NULL;
  52909. g = NULL;
  52910. /* Setting again will free the p and g. */
  52911. wolfSSL_BN_free(q);
  52912. q = NULL;
  52913. DH_free(dh);
  52914. dh = NULL;
  52915. dh = wolfSSL_DH_new();
  52916. ExpectNotNull(dh);
  52917. p = wolfSSL_BN_new();
  52918. ExpectNotNull(p);
  52919. ExpectIntEQ(BN_set_word(p, 11), 1);
  52920. g = wolfSSL_BN_new();
  52921. ExpectNotNull(g);
  52922. ExpectIntEQ(BN_set_word(g, 2), 1);
  52923. q = wolfSSL_BN_new();
  52924. ExpectNotNull(q);
  52925. ExpectIntEQ(BN_set_word(q, 5), 1);
  52926. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
  52927. /* p, q and g are now owned by dh - don't free. */
  52928. if (EXPECT_FAIL()) {
  52929. BN_free(p);
  52930. BN_free(q);
  52931. BN_free(g);
  52932. }
  52933. p = NULL;
  52934. q = NULL;
  52935. g = NULL;
  52936. p = wolfSSL_BN_new();
  52937. ExpectNotNull(p);
  52938. ExpectIntEQ(BN_set_word(p, 11), 1);
  52939. g = wolfSSL_BN_new();
  52940. ExpectNotNull(g);
  52941. ExpectIntEQ(BN_set_word(g, 2), 1);
  52942. q = wolfSSL_BN_new();
  52943. ExpectNotNull(q);
  52944. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
  52945. if (EXPECT_FAIL()) {
  52946. BN_free(p);
  52947. }
  52948. p = NULL;
  52949. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
  52950. if (EXPECT_FAIL()) {
  52951. BN_free(q);
  52952. }
  52953. q = NULL;
  52954. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
  52955. if (EXPECT_FAIL()) {
  52956. BN_free(g);
  52957. }
  52958. g = NULL;
  52959. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
  52960. /* p, q and g are now owned by dh - don't free. */
  52961. DH_free(dh);
  52962. dh = NULL;
  52963. ExpectIntEQ(DH_generate_key(NULL), 0);
  52964. ExpectNotNull(dh = DH_new());
  52965. ExpectIntEQ(DH_generate_key(dh), 0);
  52966. p = wolfSSL_BN_new();
  52967. ExpectNotNull(p);
  52968. ExpectIntEQ(BN_set_word(p, 0), 1);
  52969. g = wolfSSL_BN_new();
  52970. ExpectNotNull(g);
  52971. ExpectIntEQ(BN_set_word(g, 2), 1);
  52972. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  52973. if (EXPECT_FAIL()) {
  52974. BN_free(p);
  52975. BN_free(g);
  52976. }
  52977. p = NULL;
  52978. g = NULL;
  52979. ExpectIntEQ(DH_generate_key(dh), 0);
  52980. DH_free(dh);
  52981. dh = NULL;
  52982. #endif
  52983. #endif
  52984. /* Test DH_up_ref() */
  52985. dh = wolfSSL_DH_new();
  52986. ExpectNotNull(dh);
  52987. ExpectIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  52988. ExpectIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  52989. DH_free(dh); /* decrease ref count */
  52990. DH_free(dh); /* free WOLFSSL_DH */
  52991. dh = NULL;
  52992. q = NULL;
  52993. ExpectNull((dh = DH_new_by_nid(NID_sha1)));
  52994. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  52995. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  52996. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  52997. #ifdef HAVE_FFDHE_2048
  52998. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  52999. DH_free(dh);
  53000. dh = NULL;
  53001. q = NULL;
  53002. #endif
  53003. #ifdef HAVE_FFDHE_3072
  53004. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  53005. DH_free(dh);
  53006. dh = NULL;
  53007. q = NULL;
  53008. #endif
  53009. #ifdef HAVE_FFDHE_4096
  53010. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  53011. DH_free(dh);
  53012. dh = NULL;
  53013. q = NULL;
  53014. #endif
  53015. #else
  53016. ExpectNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  53017. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  53018. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  53019. ExpectIntEQ(wolfSSL_DH_size(NULL), -1);
  53020. #endif /* OPENSSL_EXTRA && !NO_DH */
  53021. return EXPECT_RESULT();
  53022. }
  53023. static int test_wolfSSL_DH_dup(void)
  53024. {
  53025. EXPECT_DECLS;
  53026. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  53027. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  53028. defined(OPENSSL_EXTRA)
  53029. DH *dh = NULL;
  53030. DH *dhDup = NULL;
  53031. ExpectNotNull(dh = wolfSSL_DH_new());
  53032. ExpectNull(dhDup = wolfSSL_DH_dup(NULL));
  53033. ExpectNull(dhDup = wolfSSL_DH_dup(dh));
  53034. #if defined(OPENSSL_ALL) || \
  53035. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  53036. {
  53037. WOLFSSL_BIGNUM* p = NULL;
  53038. WOLFSSL_BIGNUM* g = NULL;
  53039. ExpectNotNull(p = wolfSSL_BN_new());
  53040. ExpectNotNull(g = wolfSSL_BN_new());
  53041. ExpectIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
  53042. ExpectIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
  53043. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  53044. if (EXPECT_FAIL()) {
  53045. wolfSSL_BN_free(p);
  53046. wolfSSL_BN_free(g);
  53047. }
  53048. ExpectNotNull(dhDup = wolfSSL_DH_dup(dh));
  53049. wolfSSL_DH_free(dhDup);
  53050. }
  53051. #endif
  53052. wolfSSL_DH_free(dh);
  53053. #endif
  53054. #endif
  53055. return EXPECT_RESULT();
  53056. }
  53057. static int test_wolfSSL_DH_check(void)
  53058. {
  53059. EXPECT_DECLS;
  53060. #ifdef OPENSSL_ALL
  53061. #ifndef NO_DH
  53062. #ifndef NO_BIO
  53063. #ifndef NO_DSA
  53064. byte buf[6000];
  53065. char file[] = "./certs/dsaparams.pem";
  53066. XFILE f = XBADFILE;
  53067. int bytes;
  53068. BIO* bio = NULL;
  53069. DSA* dsa = NULL;
  53070. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  53071. static const byte dh2048[] = {
  53072. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  53073. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  53074. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  53075. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  53076. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  53077. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  53078. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  53079. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  53080. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  53081. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  53082. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  53083. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  53084. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  53085. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  53086. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  53087. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  53088. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  53089. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  53090. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  53091. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  53092. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  53093. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  53094. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  53095. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  53096. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  53097. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  53098. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  53099. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  53100. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  53101. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  53102. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  53103. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  53104. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  53105. 0x93, 0x02, 0x01, 0x02
  53106. };
  53107. const byte* params;
  53108. #endif
  53109. DH* dh = NULL;
  53110. WOLFSSL_BIGNUM* p = NULL;
  53111. WOLFSSL_BIGNUM* g = NULL;
  53112. WOLFSSL_BIGNUM* pTmp = NULL;
  53113. WOLFSSL_BIGNUM* gTmp = NULL;
  53114. int codes = -1;
  53115. #ifndef NO_DSA
  53116. /* Initialize DH */
  53117. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  53118. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  53119. if (f != XBADFILE)
  53120. XFCLOSE(f);
  53121. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  53122. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  53123. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  53124. ExpectNotNull(dh);
  53125. BIO_free(bio);
  53126. DSA_free(dsa);
  53127. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  53128. params = dh2048;
  53129. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &params,
  53130. (long)sizeof(dh2048)));
  53131. #else
  53132. ExpectNotNull(dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048));
  53133. #endif
  53134. /* Test assumed to be valid dh.
  53135. * Should return WOLFSSL_SUCCESS
  53136. * codes should be 0
  53137. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  53138. */
  53139. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  53140. ExpectIntEQ(codes, 0);
  53141. /* Test NULL dh: expected BAD_FUNC_ARG */
  53142. ExpectIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
  53143. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  53144. if (dh != NULL) {
  53145. pTmp = dh->p;
  53146. dh->p = NULL;
  53147. }
  53148. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  53149. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  53150. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  53151. /* set dh->p back to normal so it won't fail on next tests */
  53152. if (dh != NULL) {
  53153. dh->p = pTmp;
  53154. pTmp = NULL;
  53155. }
  53156. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  53157. if (dh != NULL) {
  53158. gTmp = dh->g;
  53159. dh->g = NULL;
  53160. }
  53161. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  53162. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  53163. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  53164. if (dh != NULL) {
  53165. dh->g = gTmp;
  53166. gTmp = NULL;
  53167. }
  53168. /* Cleanup */
  53169. DH_free(dh);
  53170. dh = NULL;
  53171. dh = DH_new();
  53172. ExpectNotNull(dh);
  53173. /* Check empty DH. */
  53174. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  53175. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  53176. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
  53177. /* Check non-prime valued p. */
  53178. ExpectNotNull(p = BN_new());
  53179. ExpectIntEQ(BN_set_word(p, 4), 1);
  53180. ExpectNotNull(g = BN_new());
  53181. ExpectIntEQ(BN_set_word(g, 2), 1);
  53182. ExpectIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
  53183. if (EXPECT_FAIL()) {
  53184. wolfSSL_BN_free(p);
  53185. wolfSSL_BN_free(g);
  53186. }
  53187. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  53188. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  53189. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  53190. DH_free(dh);
  53191. dh = NULL;
  53192. #endif
  53193. #endif /* !NO_DH && !NO_DSA */
  53194. #endif
  53195. return EXPECT_RESULT();
  53196. }
  53197. static int test_wolfSSL_DH_prime(void)
  53198. {
  53199. EXPECT_DECLS;
  53200. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  53201. WOLFSSL_BIGNUM* bn = NULL;
  53202. #if WOLFSSL_MAX_BN_BITS >= 768
  53203. WOLFSSL_BIGNUM* bn2 = NULL;
  53204. #endif
  53205. bn = wolfSSL_DH_768_prime(NULL);
  53206. #if WOLFSSL_MAX_BN_BITS >= 768
  53207. ExpectNotNull(bn);
  53208. bn2 = wolfSSL_DH_768_prime(bn);
  53209. ExpectNotNull(bn2);
  53210. ExpectTrue(bn == bn2);
  53211. wolfSSL_BN_free(bn);
  53212. bn = NULL;
  53213. #else
  53214. ExpectNull(bn);
  53215. #endif
  53216. bn = wolfSSL_DH_1024_prime(NULL);
  53217. #if WOLFSSL_MAX_BN_BITS >= 1024
  53218. ExpectNotNull(bn);
  53219. wolfSSL_BN_free(bn);
  53220. bn = NULL;
  53221. #else
  53222. ExpectNull(bn);
  53223. #endif
  53224. bn = wolfSSL_DH_2048_prime(NULL);
  53225. #if WOLFSSL_MAX_BN_BITS >= 2048
  53226. ExpectNotNull(bn);
  53227. wolfSSL_BN_free(bn);
  53228. bn = NULL;
  53229. #else
  53230. ExpectNull(bn);
  53231. #endif
  53232. bn = wolfSSL_DH_3072_prime(NULL);
  53233. #if WOLFSSL_MAX_BN_BITS >= 3072
  53234. ExpectNotNull(bn);
  53235. wolfSSL_BN_free(bn);
  53236. bn = NULL;
  53237. #else
  53238. ExpectNull(bn);
  53239. #endif
  53240. bn = wolfSSL_DH_4096_prime(NULL);
  53241. #if WOLFSSL_MAX_BN_BITS >= 4096
  53242. ExpectNotNull(bn);
  53243. wolfSSL_BN_free(bn);
  53244. bn = NULL;
  53245. #else
  53246. ExpectNull(bn);
  53247. #endif
  53248. bn = wolfSSL_DH_6144_prime(NULL);
  53249. #if WOLFSSL_MAX_BN_BITS >= 6144
  53250. ExpectNotNull(bn);
  53251. wolfSSL_BN_free(bn);
  53252. bn = NULL;
  53253. #else
  53254. ExpectNull(bn);
  53255. #endif
  53256. bn = wolfSSL_DH_8192_prime(NULL);
  53257. #if WOLFSSL_MAX_BN_BITS >= 8192
  53258. ExpectNotNull(bn);
  53259. wolfSSL_BN_free(bn);
  53260. bn = NULL;
  53261. #else
  53262. ExpectNull(bn);
  53263. #endif
  53264. #endif
  53265. return EXPECT_RESULT();
  53266. }
  53267. static int test_wolfSSL_DH_1536_prime(void)
  53268. {
  53269. EXPECT_DECLS;
  53270. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  53271. BIGNUM* bn = NULL;
  53272. unsigned char bits[200];
  53273. int sz = 192; /* known binary size */
  53274. const byte expected[] = {
  53275. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  53276. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  53277. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  53278. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  53279. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  53280. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  53281. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  53282. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  53283. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  53284. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  53285. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  53286. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  53287. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  53288. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  53289. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  53290. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  53291. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  53292. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  53293. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  53294. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  53295. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  53296. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  53297. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  53298. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  53299. };
  53300. ExpectNotNull(bn = get_rfc3526_prime_1536(NULL));
  53301. ExpectIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  53302. ExpectIntEQ(0, XMEMCMP(expected, bits, sz));
  53303. BN_free(bn);
  53304. #endif
  53305. return EXPECT_RESULT();
  53306. }
  53307. static int test_wolfSSL_DH_get_2048_256(void)
  53308. {
  53309. EXPECT_DECLS;
  53310. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  53311. WOLFSSL_DH* dh = NULL;
  53312. const WOLFSSL_BIGNUM* pBn;
  53313. const WOLFSSL_BIGNUM* gBn;
  53314. const WOLFSSL_BIGNUM* qBn;
  53315. const byte pExpected[] = {
  53316. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  53317. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  53318. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  53319. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  53320. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  53321. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  53322. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  53323. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  53324. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  53325. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  53326. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  53327. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  53328. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  53329. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  53330. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  53331. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  53332. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  53333. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  53334. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  53335. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  53336. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  53337. 0x1E, 0x1A, 0x15, 0x97
  53338. };
  53339. const byte gExpected[] = {
  53340. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  53341. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  53342. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  53343. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  53344. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  53345. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  53346. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  53347. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  53348. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  53349. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  53350. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  53351. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  53352. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  53353. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  53354. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  53355. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  53356. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  53357. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  53358. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  53359. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  53360. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  53361. 0x6C, 0xC4, 0x16, 0x59
  53362. };
  53363. const byte qExpected[] = {
  53364. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  53365. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  53366. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  53367. };
  53368. int pSz = 0;
  53369. int qSz = 0;
  53370. int gSz = 0;
  53371. byte* pReturned = NULL;
  53372. byte* qReturned = NULL;
  53373. byte* gReturned = NULL;
  53374. ExpectNotNull((dh = wolfSSL_DH_get_2048_256()));
  53375. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  53376. ExpectIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  53377. ExpectNotNull(pReturned = (byte*)XMALLOC(pSz, NULL,
  53378. DYNAMIC_TYPE_TMP_BUFFER));
  53379. ExpectIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  53380. ExpectIntEQ(pSz, sizeof(pExpected));
  53381. ExpectIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  53382. ExpectIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  53383. ExpectNotNull(qReturned = (byte*)XMALLOC(qSz, NULL,
  53384. DYNAMIC_TYPE_TMP_BUFFER));
  53385. ExpectIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  53386. ExpectIntEQ(qSz, sizeof(qExpected));
  53387. ExpectIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  53388. ExpectIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  53389. ExpectNotNull(gReturned = (byte*)XMALLOC(gSz, NULL,
  53390. DYNAMIC_TYPE_TMP_BUFFER));
  53391. ExpectIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  53392. ExpectIntEQ(gSz, sizeof(gExpected));
  53393. ExpectIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  53394. wolfSSL_DH_free(dh);
  53395. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53396. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53397. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53398. #endif
  53399. return EXPECT_RESULT();
  53400. }
  53401. static int test_wolfSSL_PEM_write_DHparams(void)
  53402. {
  53403. EXPECT_DECLS;
  53404. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  53405. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  53406. DH* dh = NULL;
  53407. BIO* bio = NULL;
  53408. XFILE fp = XBADFILE;
  53409. byte pem[2048];
  53410. int pemSz = 0;
  53411. const char expected[] =
  53412. "-----BEGIN DH PARAMETERS-----\n"
  53413. "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
  53414. "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
  53415. "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
  53416. "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
  53417. "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
  53418. "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
  53419. "-----END DH PARAMETERS-----\n";
  53420. const char badPem[] =
  53421. "-----BEGIN DH PARAMETERS-----\n"
  53422. "-----END DH PARAMETERS-----\n";
  53423. const char emptySeqPem[] =
  53424. "-----BEGIN DH PARAMETERS-----\n"
  53425. "MAA=\n"
  53426. "-----END DH PARAMETERS-----\n";
  53427. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  53428. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  53429. if (fp != XBADFILE) {
  53430. XFCLOSE(fp);
  53431. fp = XBADFILE;
  53432. }
  53433. ExpectNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
  53434. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  53435. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  53436. ExpectIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
  53437. (int)sizeof(badPem));
  53438. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  53439. BIO_free(bio);
  53440. bio = NULL;
  53441. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  53442. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  53443. ExpectIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
  53444. (int)sizeof(emptySeqPem));
  53445. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  53446. BIO_free(bio);
  53447. bio = NULL;
  53448. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  53449. ExpectIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  53450. ExpectNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  53451. BIO_free(bio);
  53452. bio = NULL;
  53453. ExpectNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  53454. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  53455. ExpectIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  53456. DH_free(dh);
  53457. dh = NULL;
  53458. dh = wolfSSL_DH_new();
  53459. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
  53460. if (fp != XBADFILE) {
  53461. XFCLOSE(fp);
  53462. fp = XBADFILE;
  53463. }
  53464. wolfSSL_DH_free(dh);
  53465. dh = NULL;
  53466. /* check results */
  53467. XMEMSET(pem, 0, sizeof(pem));
  53468. ExpectTrue((fp = XFOPEN("./test-write-dhparams.pem", "rb")) != XBADFILE);
  53469. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  53470. ExpectIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  53471. if (fp != XBADFILE)
  53472. XFCLOSE(fp);
  53473. #endif
  53474. return EXPECT_RESULT();
  53475. }
  53476. static int test_wolfSSL_d2i_DHparams(void)
  53477. {
  53478. EXPECT_DECLS;
  53479. #ifdef OPENSSL_ALL
  53480. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  53481. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  53482. XFILE f = XBADFILE;
  53483. unsigned char buf[4096];
  53484. const unsigned char* pt = buf;
  53485. #ifdef HAVE_FFDHE_2048
  53486. const char* params1 = "./certs/dh2048.der";
  53487. #endif
  53488. #ifdef HAVE_FFDHE_3072
  53489. const char* params2 = "./certs/dh3072.der";
  53490. #endif
  53491. long len = 0;
  53492. WOLFSSL_DH* dh = NULL;
  53493. XMEMSET(buf, 0, sizeof(buf));
  53494. /* Test 2048 bit parameters */
  53495. #ifdef HAVE_FFDHE_2048
  53496. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  53497. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  53498. if (f != XBADFILE) {
  53499. XFCLOSE(f);
  53500. f = XBADFILE;
  53501. }
  53502. /* Valid case */
  53503. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  53504. ExpectNotNull(dh->p);
  53505. ExpectNotNull(dh->g);
  53506. ExpectTrue(pt == buf);
  53507. ExpectIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
  53508. ExpectIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
  53509. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  53510. /* Invalid cases */
  53511. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  53512. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  53513. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  53514. DH_free(dh);
  53515. dh = NULL;
  53516. *buf = 0;
  53517. pt = buf;
  53518. #endif /* HAVE_FFDHE_2048 */
  53519. /* Test 3072 bit parameters */
  53520. #ifdef HAVE_FFDHE_3072
  53521. ExpectTrue((f = XFOPEN(params2, "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(&dh, &pt, len));
  53529. ExpectNotNull(dh->p);
  53530. ExpectNotNull(dh->g);
  53531. ExpectTrue(pt != buf);
  53532. ExpectIntEQ(DH_generate_key(dh), 1);
  53533. /* Invalid cases */
  53534. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  53535. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  53536. DH_free(dh);
  53537. dh = NULL;
  53538. #endif /* HAVE_FFDHE_3072 */
  53539. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  53540. #endif /* !NO_DH */
  53541. #endif
  53542. return EXPECT_RESULT();
  53543. }
  53544. static int test_wolfSSL_DH_LoadDer(void)
  53545. {
  53546. EXPECT_DECLS;
  53547. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
  53548. defined(OPENSSL_EXTRA)
  53549. static const byte dh2048[] = {
  53550. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  53551. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  53552. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  53553. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  53554. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  53555. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  53556. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  53557. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  53558. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  53559. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  53560. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  53561. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  53562. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  53563. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  53564. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  53565. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  53566. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  53567. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  53568. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  53569. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  53570. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  53571. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  53572. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  53573. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  53574. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  53575. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  53576. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  53577. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  53578. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  53579. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  53580. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  53581. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  53582. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  53583. 0x93, 0x02, 0x01, 0x02
  53584. };
  53585. WOLFSSL_DH* dh = NULL;
  53586. ExpectNotNull(dh = wolfSSL_DH_new());
  53587. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
  53588. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
  53589. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
  53590. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
  53591. wolfSSL_DH_free(dh);
  53592. #endif
  53593. return EXPECT_RESULT();
  53594. }
  53595. static int test_wolfSSL_i2d_DHparams(void)
  53596. {
  53597. EXPECT_DECLS;
  53598. #ifdef OPENSSL_ALL
  53599. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  53600. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  53601. XFILE f = XBADFILE;
  53602. unsigned char buf[4096];
  53603. const unsigned char* pt;
  53604. unsigned char* pt2;
  53605. #ifdef HAVE_FFDHE_2048
  53606. const char* params1 = "./certs/dh2048.der";
  53607. #endif
  53608. #ifdef HAVE_FFDHE_3072
  53609. const char* params2 = "./certs/dh3072.der";
  53610. #endif
  53611. long len = 0;
  53612. WOLFSSL_DH* dh = NULL;
  53613. /* Test 2048 bit parameters */
  53614. #ifdef HAVE_FFDHE_2048
  53615. pt = buf;
  53616. pt2 = buf;
  53617. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  53618. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  53619. if (f != XBADFILE) {
  53620. XFCLOSE(f);
  53621. f = XBADFILE;
  53622. }
  53623. /* Valid case */
  53624. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  53625. ExpectTrue(pt == buf);
  53626. ExpectIntEQ(DH_generate_key(dh), 1);
  53627. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  53628. /* Invalid case */
  53629. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  53630. /* Return length only */
  53631. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  53632. DH_free(dh);
  53633. dh = NULL;
  53634. *buf = 0;
  53635. #endif
  53636. /* Test 3072 bit parameters */
  53637. #ifdef HAVE_FFDHE_3072
  53638. pt = buf;
  53639. pt2 = buf;
  53640. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  53641. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  53642. if (f != XBADFILE) {
  53643. XFCLOSE(f);
  53644. f = XBADFILE;
  53645. }
  53646. /* Valid case */
  53647. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  53648. ExpectTrue(pt == buf);
  53649. ExpectIntEQ(DH_generate_key(dh), 1);
  53650. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  53651. /* Invalid case */
  53652. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  53653. /* Return length only */
  53654. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  53655. DH_free(dh);
  53656. dh = NULL;
  53657. #endif
  53658. dh = DH_new();
  53659. ExpectNotNull(dh);
  53660. pt2 = buf;
  53661. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
  53662. DH_free(dh);
  53663. dh = NULL;
  53664. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  53665. #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
  53666. #endif
  53667. return EXPECT_RESULT();
  53668. }
  53669. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  53670. /*----------------------------------------------------------------------------*
  53671. | EC
  53672. *----------------------------------------------------------------------------*/
  53673. static int test_wolfSSL_EC_GROUP(void)
  53674. {
  53675. EXPECT_DECLS;
  53676. #ifdef OPENSSL_EXTRA
  53677. EC_GROUP *group = NULL;
  53678. EC_GROUP *group2 = NULL;
  53679. EC_GROUP *group3 = NULL;
  53680. #ifndef HAVE_ECC_BRAINPOOL
  53681. EC_GROUP *group4 = NULL;
  53682. #endif
  53683. WOLFSSL_BIGNUM* order = NULL;
  53684. int group_bits;
  53685. int i;
  53686. static const int knownEccNids[] = {
  53687. NID_X9_62_prime192v1,
  53688. NID_X9_62_prime192v2,
  53689. NID_X9_62_prime192v3,
  53690. NID_X9_62_prime239v1,
  53691. NID_X9_62_prime239v2,
  53692. NID_X9_62_prime239v3,
  53693. NID_X9_62_prime256v1,
  53694. NID_secp112r1,
  53695. NID_secp112r2,
  53696. NID_secp128r1,
  53697. NID_secp128r2,
  53698. NID_secp160r1,
  53699. NID_secp160r2,
  53700. NID_secp224r1,
  53701. NID_secp384r1,
  53702. NID_secp521r1,
  53703. NID_secp160k1,
  53704. NID_secp192k1,
  53705. NID_secp224k1,
  53706. NID_secp256k1,
  53707. NID_brainpoolP160r1,
  53708. NID_brainpoolP192r1,
  53709. NID_brainpoolP224r1,
  53710. NID_brainpoolP256r1,
  53711. NID_brainpoolP320r1,
  53712. NID_brainpoolP384r1,
  53713. NID_brainpoolP512r1,
  53714. };
  53715. int knowEccNidsLen = (int)(sizeof(knownEccNids) / sizeof(*knownEccNids));
  53716. static const int knownEccEnums[] = {
  53717. ECC_SECP192R1,
  53718. ECC_PRIME192V2,
  53719. ECC_PRIME192V3,
  53720. ECC_PRIME239V1,
  53721. ECC_PRIME239V2,
  53722. ECC_PRIME239V3,
  53723. ECC_SECP256R1,
  53724. ECC_SECP112R1,
  53725. ECC_SECP112R2,
  53726. ECC_SECP128R1,
  53727. ECC_SECP128R2,
  53728. ECC_SECP160R1,
  53729. ECC_SECP160R2,
  53730. ECC_SECP224R1,
  53731. ECC_SECP384R1,
  53732. ECC_SECP521R1,
  53733. ECC_SECP160K1,
  53734. ECC_SECP192K1,
  53735. ECC_SECP224K1,
  53736. ECC_SECP256K1,
  53737. ECC_BRAINPOOLP160R1,
  53738. ECC_BRAINPOOLP192R1,
  53739. ECC_BRAINPOOLP224R1,
  53740. ECC_BRAINPOOLP256R1,
  53741. ECC_BRAINPOOLP320R1,
  53742. ECC_BRAINPOOLP384R1,
  53743. ECC_BRAINPOOLP512R1,
  53744. };
  53745. int knowEccEnumsLen = (int)(sizeof(knownEccEnums) / sizeof(*knownEccEnums));
  53746. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  53747. ExpectNotNull(group2 = EC_GROUP_dup(group));
  53748. ExpectNotNull(group3 = wolfSSL_EC_GROUP_new_by_curve_name(NID_secp384r1));
  53749. #ifndef HAVE_ECC_BRAINPOOL
  53750. ExpectNotNull(group4 = wolfSSL_EC_GROUP_new_by_curve_name(
  53751. NID_brainpoolP256r1));
  53752. #endif
  53753. ExpectNull(EC_GROUP_dup(NULL));
  53754. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(NULL), 0);
  53755. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  53756. ExpectIntEQ((group_bits = EC_GROUP_order_bits(NULL)), 0);
  53757. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  53758. #ifndef HAVE_ECC_BRAINPOOL
  53759. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group4)), 0);
  53760. #endif
  53761. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(NULL), 0);
  53762. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(group), 256);
  53763. ExpectNotNull(order = BN_new());
  53764. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, NULL, NULL), 0);
  53765. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, NULL, NULL), 0);
  53766. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, order, NULL), 0);
  53767. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, order, NULL), 1);
  53768. wolfSSL_BN_free(order);
  53769. ExpectNotNull(EC_GROUP_method_of(group));
  53770. ExpectIntEQ(EC_METHOD_get_field_type(NULL), 0);
  53771. ExpectIntEQ(EC_METHOD_get_field_type(EC_GROUP_method_of(group)),
  53772. NID_X9_62_prime_field);
  53773. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, NULL, NULL), -1);
  53774. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, NULL, NULL), -1);
  53775. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, group, NULL), -1);
  53776. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, group3, NULL), 1);
  53777. #ifndef NO_WOLFSSL_STUB
  53778. wolfSSL_EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  53779. #endif
  53780. #ifndef HAVE_ECC_BRAINPOOL
  53781. EC_GROUP_free(group4);
  53782. #endif
  53783. EC_GROUP_free(group3);
  53784. EC_GROUP_free(group2);
  53785. EC_GROUP_free(group);
  53786. for (i = 0; i < knowEccNidsLen; i++) {
  53787. group = NULL;
  53788. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccNids[i]));
  53789. ExpectIntGT(wolfSSL_EC_GROUP_get_degree(group), 0);
  53790. EC_GROUP_free(group);
  53791. }
  53792. for (i = 0; i < knowEccEnumsLen; i++) {
  53793. group = NULL;
  53794. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccEnums[i]));
  53795. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), knownEccNids[i]);
  53796. EC_GROUP_free(group);
  53797. }
  53798. #endif
  53799. return EXPECT_RESULT();
  53800. }
  53801. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  53802. {
  53803. EXPECT_DECLS;
  53804. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  53805. EC_GROUP *group = NULL;
  53806. BIO* bio = NULL;
  53807. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  53808. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  53809. EC_GROUP *ret = NULL;
  53810. static char ec_nc_p384[] = "-----BEGIN EC PARAMETERS-----\n"
  53811. "BgUrgQQAIg==\n"
  53812. "-----END EC PARAMETERS-----";
  53813. #endif
  53814. static char ec_nc_bad_1[] = "-----BEGIN EC PARAMETERS-----\n"
  53815. "MAA=\n"
  53816. "-----END EC PARAMETERS-----";
  53817. static char ec_nc_bad_2[] = "-----BEGIN EC PARAMETERS-----\n"
  53818. "BgA=\n"
  53819. "-----END EC PARAMETERS-----";
  53820. static char ec_nc_bad_3[] = "-----BEGIN EC PARAMETERS-----\n"
  53821. "BgE=\n"
  53822. "-----END EC PARAMETERS-----";
  53823. static char ec_nc_bad_4[] = "-----BEGIN EC PARAMETERS-----\n"
  53824. "BgE*\n"
  53825. "-----END EC PARAMETERS-----";
  53826. /* Test that first parameter, bio, being NULL fails. */
  53827. ExpectNull(PEM_read_bio_ECPKParameters(NULL, NULL, NULL, NULL));
  53828. /* Test that reading named parameters works. */
  53829. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  53830. ExpectIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  53831. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53832. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  53833. BIO_free(bio);
  53834. bio = NULL;
  53835. EC_GROUP_free(group);
  53836. group = NULL;
  53837. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  53838. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  53839. /* Test that reusing group works. */
  53840. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  53841. sizeof(ec_nc_p384)));
  53842. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  53843. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  53844. BIO_free(bio);
  53845. bio = NULL;
  53846. EC_GROUP_free(group);
  53847. group = NULL;
  53848. /* Test that returning through group works. */
  53849. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  53850. sizeof(ec_nc_p384)));
  53851. ExpectNotNull(ret = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  53852. ExpectIntEQ(group == ret, 1);
  53853. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  53854. BIO_free(bio);
  53855. bio = NULL;
  53856. EC_GROUP_free(group);
  53857. group = NULL;
  53858. #endif
  53859. /* Test 0x30, 0x00 (not and object id) fails. */
  53860. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_1,
  53861. sizeof(ec_nc_bad_1)));
  53862. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53863. BIO_free(bio);
  53864. bio = NULL;
  53865. /* Test 0x06, 0x00 (empty object id) fails. */
  53866. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_2,
  53867. sizeof(ec_nc_bad_2)));
  53868. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53869. BIO_free(bio);
  53870. bio = NULL;
  53871. /* Test 0x06, 0x01 (badly formed object id) fails. */
  53872. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_3,
  53873. sizeof(ec_nc_bad_3)));
  53874. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53875. BIO_free(bio);
  53876. bio = NULL;
  53877. /* Test invalid PEM encoding - invalid character. */
  53878. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_4,
  53879. sizeof(ec_nc_bad_4)));
  53880. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  53881. BIO_free(bio);
  53882. #endif
  53883. return EXPECT_RESULT();
  53884. }
  53885. static int test_wolfSSL_EC_POINT(void)
  53886. {
  53887. EXPECT_DECLS;
  53888. #if !defined(WOLFSSL_SP_MATH) && \
  53889. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  53890. #ifdef OPENSSL_EXTRA
  53891. BN_CTX* ctx = NULL;
  53892. EC_GROUP* group = NULL;
  53893. #ifndef HAVE_ECC_BRAINPOOL
  53894. EC_GROUP* group2 = NULL;
  53895. #endif
  53896. EC_POINT* Gxy = NULL;
  53897. EC_POINT* new_point = NULL;
  53898. EC_POINT* set_point = NULL;
  53899. EC_POINT* infinity = NULL;
  53900. BIGNUM* k = NULL;
  53901. BIGNUM* Gx = NULL;
  53902. BIGNUM* Gy = NULL;
  53903. BIGNUM* Gz = NULL;
  53904. BIGNUM* X = NULL;
  53905. BIGNUM* Y = NULL;
  53906. BIGNUM* set_point_bn = NULL;
  53907. char* hexStr = NULL;
  53908. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD"
  53909. "17AF381913FF7A96314EA47055EA0FD0";
  53910. /* NISTP256R1 Gx/Gy */
  53911. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F2"
  53912. "77037D812DEB33A0F4A13945D898C296";
  53913. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  53914. "2BCE33576B315ECECBB6406837BF51F5";
  53915. #ifndef HAVE_SELFTEST
  53916. EC_POINT *tmp = NULL;
  53917. size_t bin_len;
  53918. unsigned int blen = 0;
  53919. unsigned char* buf = NULL;
  53920. unsigned char bufInf[1] = { 0x00 };
  53921. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F2"
  53922. "77037D812DEB33A0F4A13945D898C296"
  53923. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  53924. "2BCE33576B315ECECBB6406837BF51F5";
  53925. const unsigned char binUncompG[] = {
  53926. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  53927. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  53928. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  53929. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  53930. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  53931. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  53932. };
  53933. const unsigned char binUncompGBad[] = {
  53934. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  53935. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  53936. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  53937. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  53938. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  53939. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  53940. };
  53941. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F2"
  53942. "77037D812DEB33A0F4A13945D898C296";
  53943. #ifdef HAVE_COMP_KEY
  53944. const unsigned char binCompG[] = {
  53945. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  53946. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  53947. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  53948. };
  53949. #endif
  53950. #endif
  53951. ExpectNotNull(ctx = BN_CTX_new());
  53952. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  53953. #ifndef HAVE_ECC_BRAINPOOL
  53954. /* Used to make groups curve_idx == -1. */
  53955. ExpectNotNull(group2 = EC_GROUP_new_by_curve_name(NID_brainpoolP256r1));
  53956. #endif
  53957. ExpectNull(EC_POINT_new(NULL));
  53958. ExpectNotNull(Gxy = EC_POINT_new(group));
  53959. ExpectNotNull(new_point = EC_POINT_new(group));
  53960. ExpectNotNull(set_point = EC_POINT_new(group));
  53961. ExpectNotNull(X = BN_new());
  53962. ExpectNotNull(Y = BN_new());
  53963. ExpectNotNull(set_point_bn = BN_new());
  53964. ExpectNotNull(infinity = EC_POINT_new(group));
  53965. /* load test values */
  53966. ExpectIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  53967. ExpectIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  53968. ExpectIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  53969. ExpectIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  53970. /* populate coordinates for input point */
  53971. if (Gxy != NULL) {
  53972. Gxy->X = Gx;
  53973. Gxy->Y = Gy;
  53974. Gxy->Z = Gz;
  53975. }
  53976. /* Test handling of NULL point. */
  53977. EC_POINT_clear_free(NULL);
  53978. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  53979. NULL, NULL, ctx), 0);
  53980. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  53981. NULL, NULL, ctx), 0);
  53982. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  53983. NULL, NULL, ctx), 0);
  53984. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  53985. X, NULL, ctx), 0);
  53986. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  53987. NULL, Y, ctx), 0);
  53988. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  53989. X, Y, ctx), 0);
  53990. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  53991. X, Y, ctx), 0);
  53992. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  53993. NULL, Y, ctx), 0);
  53994. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  53995. X, NULL, ctx), 0);
  53996. /* Getting point at infinity returns an error. */
  53997. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, infinity,
  53998. X, Y, ctx), 0);
  53999. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  54000. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  54001. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  54002. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, NULL, ctx), 0);
  54003. ExpectIntEQ(EC_POINT_add(group, NULL, NULL, NULL, ctx), 0);
  54004. ExpectIntEQ(EC_POINT_add(NULL, new_point, NULL, NULL, ctx), 0);
  54005. ExpectIntEQ(EC_POINT_add(NULL, NULL, new_point, NULL, ctx), 0);
  54006. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, Gxy, ctx), 0);
  54007. ExpectIntEQ(EC_POINT_add(NULL, new_point, new_point, Gxy, ctx), 0);
  54008. ExpectIntEQ(EC_POINT_add(group, NULL, new_point, Gxy, ctx), 0);
  54009. ExpectIntEQ(EC_POINT_add(group, new_point, NULL, Gxy, ctx), 0);
  54010. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, NULL, ctx), 0);
  54011. ExpectIntEQ(EC_POINT_mul(NULL, NULL, Gx, Gxy, k, ctx), 0);
  54012. ExpectIntEQ(EC_POINT_mul(NULL, new_point, Gx, Gxy, k, ctx), 0);
  54013. ExpectIntEQ(EC_POINT_mul(group, NULL, Gx, Gxy, k, ctx), 0);
  54014. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), 1);
  54015. /* perform point multiplication */
  54016. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), 1);
  54017. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  54018. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  54019. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  54020. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), 1);
  54021. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  54022. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  54023. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  54024. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), 1);
  54025. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  54026. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  54027. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  54028. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, NULL, NULL, ctx), 1);
  54029. ExpectIntEQ(BN_is_zero(new_point->X), 1);
  54030. ExpectIntEQ(BN_is_zero(new_point->Y), 1);
  54031. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  54032. /* Set point to something. */
  54033. ExpectIntEQ(EC_POINT_add(group, new_point, Gxy, Gxy, ctx), 1);
  54034. #else
  54035. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy,
  54036. ctx), 1);
  54037. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  54038. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  54039. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  54040. #endif
  54041. /* check if point X coordinate is zero */
  54042. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  54043. #if defined(USE_ECC_B_PARAM) && !defined(HAVE_SELFTEST) && \
  54044. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  54045. ExpectIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  54046. #endif
  54047. /* extract the coordinates from point */
  54048. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  54049. ctx), WOLFSSL_SUCCESS);
  54050. /* check if point X coordinate is zero */
  54051. ExpectIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  54052. /* set the same X and Y points in another object */
  54053. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y,
  54054. ctx), WOLFSSL_SUCCESS);
  54055. /* compare points as they should be the same */
  54056. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, NULL, ctx), -1);
  54057. ExpectIntEQ(EC_POINT_cmp(group, NULL, NULL, ctx), -1);
  54058. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, NULL, ctx), -1);
  54059. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, set_point, ctx), -1);
  54060. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, set_point, ctx), -1);
  54061. ExpectIntEQ(EC_POINT_cmp(group, NULL, set_point, ctx), -1);
  54062. ExpectIntEQ(EC_POINT_cmp(group, new_point, NULL, ctx), -1);
  54063. ExpectIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  54064. /* Test copying */
  54065. ExpectIntEQ(EC_POINT_copy(NULL, NULL), 0);
  54066. ExpectIntEQ(EC_POINT_copy(NULL, set_point), 0);
  54067. ExpectIntEQ(EC_POINT_copy(new_point, NULL), 0);
  54068. ExpectIntEQ(EC_POINT_copy(new_point, set_point), 1);
  54069. /* Test inverting */
  54070. ExpectIntEQ(EC_POINT_invert(NULL, NULL, ctx), 0);
  54071. ExpectIntEQ(EC_POINT_invert(NULL, new_point, ctx), 0);
  54072. ExpectIntEQ(EC_POINT_invert(group, NULL, ctx), 0);
  54073. ExpectIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  54074. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  54075. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  54076. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  54077. {
  54078. EC_POINT* orig_point = NULL;
  54079. ExpectNotNull(orig_point = EC_POINT_new(group));
  54080. ExpectIntEQ(EC_POINT_add(group, orig_point, set_point, set_point, NULL),
  54081. 1);
  54082. /* new_point should be set_point inverted so adding it will revert
  54083. * the point back to set_point */
  54084. ExpectIntEQ(EC_POINT_add(group, orig_point, orig_point, new_point,
  54085. NULL), 1);
  54086. ExpectIntEQ(EC_POINT_cmp(group, orig_point, set_point, NULL), 0);
  54087. EC_POINT_free(orig_point);
  54088. }
  54089. #endif
  54090. /* Test getting affine converts from projective. */
  54091. ExpectIntEQ(EC_POINT_copy(set_point, new_point), 1);
  54092. /* Force non-affine coordinates */
  54093. ExpectIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  54094. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  54095. if (new_point != NULL) {
  54096. new_point->inSet = 0;
  54097. }
  54098. /* extract the coordinates from point */
  54099. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  54100. ctx), WOLFSSL_SUCCESS);
  54101. /* check if point ordinates have changed. */
  54102. ExpectIntNE(BN_cmp(X, set_point->X), 0);
  54103. ExpectIntNE(BN_cmp(Y, set_point->Y), 0);
  54104. /* Test check for infinity */
  54105. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  54106. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, NULL), 0);
  54107. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, infinity), 0);
  54108. ExpectIntEQ(EC_POINT_is_at_infinity(group, NULL), 0);
  54109. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 1);
  54110. ExpectIntEQ(EC_POINT_is_at_infinity(group, Gxy), 0);
  54111. #else
  54112. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 0);
  54113. #endif
  54114. ExpectPtrEq(EC_POINT_point2bn(group, set_point,
  54115. POINT_CONVERSION_UNCOMPRESSED, set_point_bn, ctx), set_point_bn);
  54116. /* check bn2hex */
  54117. hexStr = BN_bn2hex(k);
  54118. ExpectStrEQ(hexStr, kTest);
  54119. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  54120. defined(XFPRINTF)
  54121. BN_print_fp(stderr, k);
  54122. fprintf(stderr, "\n");
  54123. #endif
  54124. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  54125. hexStr = BN_bn2hex(Gx);
  54126. ExpectStrEQ(hexStr, kGx);
  54127. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  54128. defined(XFPRINTF)
  54129. BN_print_fp(stderr, Gx);
  54130. fprintf(stderr, "\n");
  54131. #endif
  54132. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  54133. hexStr = BN_bn2hex(Gy);
  54134. ExpectStrEQ(hexStr, kGy);
  54135. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  54136. defined(XFPRINTF)
  54137. BN_print_fp(stderr, Gy);
  54138. fprintf(stderr, "\n");
  54139. #endif
  54140. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  54141. #ifndef HAVE_SELFTEST
  54142. /* Test point to hex */
  54143. ExpectNull(EC_POINT_point2hex(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  54144. ctx));
  54145. ExpectNull(EC_POINT_point2hex(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  54146. ctx));
  54147. ExpectNull(EC_POINT_point2hex(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  54148. ctx));
  54149. #ifndef HAVE_ECC_BRAINPOOL
  54150. /* Group not supported in wolfCrypt. */
  54151. ExpectNull(EC_POINT_point2hex(group2, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  54152. ctx));
  54153. #endif
  54154. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  54155. ExpectNotNull(hexStr);
  54156. ExpectStrEQ(hexStr, uncompG);
  54157. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  54158. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  54159. ExpectNotNull(hexStr);
  54160. ExpectStrEQ(hexStr, compG);
  54161. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  54162. /* Test point to oct */
  54163. ExpectIntEQ(EC_POINT_point2oct(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  54164. NULL, 0, ctx), 0);
  54165. ExpectIntEQ(EC_POINT_point2oct(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  54166. NULL, 0, ctx), 0);
  54167. ExpectIntEQ(EC_POINT_point2oct(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  54168. NULL, 0, ctx), 0);
  54169. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  54170. NULL, 0, ctx);
  54171. ExpectIntEQ(bin_len, sizeof(binUncompG));
  54172. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  54173. DYNAMIC_TYPE_ECC));
  54174. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  54175. buf, bin_len, ctx), bin_len);
  54176. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  54177. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  54178. /* Infinity (x=0, y=0) encodes as '0x00'. */
  54179. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  54180. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), 1);
  54181. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  54182. POINT_CONVERSION_UNCOMPRESSED, bufInf, 0, ctx), 0);
  54183. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  54184. POINT_CONVERSION_UNCOMPRESSED, bufInf, 1, ctx), 1);
  54185. ExpectIntEQ(bufInf[0], 0);
  54186. wolfSSL_EC_POINT_dump(NULL, NULL);
  54187. /* Test point i2d */
  54188. ExpectIntEQ(ECPoint_i2d(NULL, NULL, NULL, &blen), 0);
  54189. ExpectIntEQ(ECPoint_i2d(NULL, Gxy, NULL, &blen), 0);
  54190. ExpectIntEQ(ECPoint_i2d(group, NULL, NULL, &blen), 0);
  54191. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, NULL), 0);
  54192. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, &blen), 1);
  54193. ExpectIntEQ(blen, sizeof(binUncompG));
  54194. ExpectNotNull(buf = (unsigned char*)XMALLOC(blen, NULL, DYNAMIC_TYPE_ECC));
  54195. blen -= 1;
  54196. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 0);
  54197. blen += 1;
  54198. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 1);
  54199. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  54200. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  54201. #ifdef HAVE_COMP_KEY
  54202. /* Test point to oct compressed */
  54203. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL,
  54204. 0, ctx);
  54205. ExpectIntEQ(bin_len, sizeof(binCompG));
  54206. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  54207. DYNAMIC_TYPE_ECC));
  54208. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  54209. bin_len, ctx), bin_len);
  54210. ExpectIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  54211. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  54212. #endif
  54213. /* Test point BN */
  54214. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, NULL,
  54215. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  54216. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, Gxy,
  54217. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  54218. ExpectNull(wolfSSL_EC_POINT_point2bn(group, NULL,
  54219. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  54220. ExpectNull(wolfSSL_EC_POINT_point2bn(group, Gxy, 0, NULL, ctx));
  54221. /* Test oct to point */
  54222. ExpectNotNull(tmp = EC_POINT_new(group));
  54223. ExpectIntEQ(EC_POINT_oct2point(NULL, NULL, binUncompG, sizeof(binUncompG),
  54224. ctx), 0);
  54225. ExpectIntEQ(EC_POINT_oct2point(NULL, tmp, binUncompG, sizeof(binUncompG),
  54226. ctx), 0);
  54227. ExpectIntEQ(EC_POINT_oct2point(group, NULL, binUncompG, sizeof(binUncompG),
  54228. ctx), 0);
  54229. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompGBad,
  54230. sizeof(binUncompGBad), ctx), 0);
  54231. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG),
  54232. ctx), 1);
  54233. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  54234. EC_POINT_free(tmp);
  54235. tmp = NULL;
  54236. /* Test setting BN ordinates. */
  54237. ExpectNotNull(tmp = EC_POINT_new(group));
  54238. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  54239. NULL, ctx), 0);
  54240. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, NULL,
  54241. NULL, ctx), 0);
  54242. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, NULL,
  54243. NULL, ctx), 0);
  54244. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, Gx,
  54245. NULL, ctx), 0);
  54246. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  54247. Gy, ctx), 0);
  54248. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, Gx, Gy,
  54249. ctx), 0);
  54250. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, Gx, Gy,
  54251. ctx), 0);
  54252. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, NULL,
  54253. Gy, ctx), 0);
  54254. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx,
  54255. NULL, ctx), 0);
  54256. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx, Gy,
  54257. ctx), 1);
  54258. EC_POINT_free(tmp);
  54259. tmp = NULL;
  54260. /* Test point d2i */
  54261. ExpectNotNull(tmp = EC_POINT_new(group));
  54262. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, NULL), 0);
  54263. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, NULL), 0);
  54264. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, NULL), 0);
  54265. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, tmp), 0);
  54266. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, tmp), 0);
  54267. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, tmp), 0);
  54268. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, NULL), 0);
  54269. ExpectIntEQ(ECPoint_d2i(binUncompGBad, sizeof(binUncompG), group, tmp), 0);
  54270. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, tmp), 1);
  54271. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  54272. EC_POINT_free(tmp);
  54273. tmp = NULL;
  54274. #ifdef HAVE_COMP_KEY
  54275. /* Test oct compressed to point */
  54276. ExpectNotNull(tmp = EC_POINT_new(group));
  54277. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx),
  54278. 1);
  54279. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  54280. EC_POINT_free(tmp);
  54281. tmp = NULL;
  54282. /* Test point d2i - compressed */
  54283. ExpectNotNull(tmp = EC_POINT_new(group));
  54284. ExpectIntEQ(ECPoint_d2i(binCompG, sizeof(binCompG), group, tmp), 1);
  54285. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  54286. EC_POINT_free(tmp);
  54287. tmp = NULL;
  54288. #endif
  54289. #endif
  54290. /* test BN_mod_add */
  54291. ExpectIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  54292. (WOLFSSL_BIGNUM*)BN_value_one(), (WOLFSSL_BIGNUM*)BN_value_one(), NULL),
  54293. 1);
  54294. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  54295. /* cleanup */
  54296. BN_free(X);
  54297. BN_free(Y);
  54298. BN_free(k);
  54299. BN_free(set_point_bn);
  54300. EC_POINT_free(infinity);
  54301. EC_POINT_free(new_point);
  54302. EC_POINT_free(set_point);
  54303. EC_POINT_clear_free(Gxy);
  54304. #ifndef HAVE_ECC_BRAINPOOL
  54305. EC_GROUP_free(group2);
  54306. #endif
  54307. EC_GROUP_free(group);
  54308. BN_CTX_free(ctx);
  54309. #endif
  54310. #endif /* !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  54311. return EXPECT_RESULT();
  54312. }
  54313. static int test_wolfSSL_SPAKE(void)
  54314. {
  54315. EXPECT_DECLS;
  54316. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(WOLFSSL_ATECC508A) \
  54317. && !defined(WOLFSSL_ATECC608A) && !defined(HAVE_SELFTEST) && \
  54318. !defined(WOLFSSL_SP_MATH) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  54319. BIGNUM* x = NULL; /* kdc priv */
  54320. BIGNUM* y = NULL; /* client priv */
  54321. BIGNUM* w = NULL; /* shared value */
  54322. byte M_bytes[] = {
  54323. /* uncompressed */
  54324. 0x04,
  54325. /* x */
  54326. 0x88, 0x6e, 0x2f, 0x97, 0xac, 0xe4, 0x6e, 0x55, 0xba, 0x9d, 0xd7, 0x24,
  54327. 0x25, 0x79, 0xf2, 0x99, 0x3b, 0x64, 0xe1, 0x6e, 0xf3, 0xdc, 0xab, 0x95,
  54328. 0xaf, 0xd4, 0x97, 0x33, 0x3d, 0x8f, 0xa1, 0x2f,
  54329. /* y */
  54330. 0x5f, 0xf3, 0x55, 0x16, 0x3e, 0x43, 0xce, 0x22, 0x4e, 0x0b, 0x0e, 0x65,
  54331. 0xff, 0x02, 0xac, 0x8e, 0x5c, 0x7b, 0xe0, 0x94, 0x19, 0xc7, 0x85, 0xe0,
  54332. 0xca, 0x54, 0x7d, 0x55, 0xa1, 0x2e, 0x2d, 0x20
  54333. };
  54334. EC_POINT* M = NULL; /* shared value */
  54335. byte N_bytes[] = {
  54336. /* uncompressed */
  54337. 0x04,
  54338. /* x */
  54339. 0xd8, 0xbb, 0xd6, 0xc6, 0x39, 0xc6, 0x29, 0x37, 0xb0, 0x4d, 0x99, 0x7f,
  54340. 0x38, 0xc3, 0x77, 0x07, 0x19, 0xc6, 0x29, 0xd7, 0x01, 0x4d, 0x49, 0xa2,
  54341. 0x4b, 0x4f, 0x98, 0xba, 0xa1, 0x29, 0x2b, 0x49,
  54342. /* y */
  54343. 0x07, 0xd6, 0x0a, 0xa6, 0xbf, 0xad, 0xe4, 0x50, 0x08, 0xa6, 0x36, 0x33,
  54344. 0x7f, 0x51, 0x68, 0xc6, 0x4d, 0x9b, 0xd3, 0x60, 0x34, 0x80, 0x8c, 0xd5,
  54345. 0x64, 0x49, 0x0b, 0x1e, 0x65, 0x6e, 0xdb, 0xe7
  54346. };
  54347. EC_POINT* N = NULL; /* shared value */
  54348. EC_POINT* T = NULL; /* kdc pub */
  54349. EC_POINT* tmp1 = NULL; /* kdc pub */
  54350. EC_POINT* tmp2 = NULL; /* kdc pub */
  54351. EC_POINT* S = NULL; /* client pub */
  54352. EC_POINT* client_secret = NULL;
  54353. EC_POINT* kdc_secret = NULL;
  54354. EC_GROUP* group = NULL;
  54355. BN_CTX* bn_ctx = NULL;
  54356. /* Values taken from a test run of Kerberos 5 */
  54357. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  54358. ExpectNotNull(bn_ctx = BN_CTX_new());
  54359. ExpectNotNull(M = EC_POINT_new(group));
  54360. ExpectNotNull(N = EC_POINT_new(group));
  54361. ExpectNotNull(T = EC_POINT_new(group));
  54362. ExpectNotNull(tmp1 = EC_POINT_new(group));
  54363. ExpectNotNull(tmp2 = EC_POINT_new(group));
  54364. ExpectNotNull(S = EC_POINT_new(group));
  54365. ExpectNotNull(client_secret = EC_POINT_new(group));
  54366. ExpectNotNull(kdc_secret = EC_POINT_new(group));
  54367. ExpectIntEQ(BN_hex2bn(&x, "DAC3027CD692B4BDF0EDFE9B7D0E4E7"
  54368. "E5D8768A725EAEEA6FC68EC239A17C0"), 1);
  54369. ExpectIntEQ(BN_hex2bn(&y, "6F6A1D394E26B1655A54B26DCE30D49"
  54370. "90CC47EBE08F809EF3FF7F6AEAABBB5"), 1);
  54371. ExpectIntEQ(BN_hex2bn(&w, "1D992AB8BA851B9BA05353453D81EE9"
  54372. "506AB395478F0AAB647752CF117B36250"), 1);
  54373. ExpectIntEQ(EC_POINT_oct2point(group, M, M_bytes, sizeof(M_bytes), bn_ctx),
  54374. 1);
  54375. ExpectIntEQ(EC_POINT_oct2point(group, N, N_bytes, sizeof(N_bytes), bn_ctx),
  54376. 1);
  54377. /* Function pattern similar to ossl_keygen and ossl_result in krb5 */
  54378. /* kdc */
  54379. /* T=x*P+w*M */
  54380. /* All in one function call */
  54381. ExpectIntEQ(EC_POINT_mul(group, T, x, M, w, bn_ctx), 1);
  54382. /* Spread into separate calls */
  54383. ExpectIntEQ(EC_POINT_mul(group, tmp1, x, NULL, NULL, bn_ctx), 1);
  54384. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, M, w, bn_ctx), 1);
  54385. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  54386. 1);
  54387. ExpectIntEQ(EC_POINT_cmp(group, T, tmp1, bn_ctx), 0);
  54388. /* client */
  54389. /* S=y*P+w*N */
  54390. /* All in one function call */
  54391. ExpectIntEQ(EC_POINT_mul(group, S, y, N, w, bn_ctx), 1);
  54392. /* Spread into separate calls */
  54393. ExpectIntEQ(EC_POINT_mul(group, tmp1, y, NULL, NULL, bn_ctx), 1);
  54394. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, N, w, bn_ctx), 1);
  54395. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  54396. 1);
  54397. ExpectIntEQ(EC_POINT_cmp(group, S, tmp1, bn_ctx), 0);
  54398. /* K=y*(T-w*M) */
  54399. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, M, w, bn_ctx), 1);
  54400. ExpectIntEQ(EC_POINT_invert(group, client_secret, bn_ctx), 1);
  54401. ExpectIntEQ(EC_POINT_add(group, client_secret, T, client_secret, bn_ctx),
  54402. 1);
  54403. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, client_secret, y,
  54404. bn_ctx), 1);
  54405. /* kdc */
  54406. /* K=x*(S-w*N) */
  54407. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, N, w, bn_ctx), 1);
  54408. ExpectIntEQ(EC_POINT_invert(group, kdc_secret, bn_ctx), 1);
  54409. ExpectIntEQ(EC_POINT_add(group, kdc_secret, S, kdc_secret, bn_ctx),
  54410. 1);
  54411. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, kdc_secret, x, bn_ctx),
  54412. 1);
  54413. /* kdc_secret == client_secret */
  54414. ExpectIntEQ(EC_POINT_cmp(group, client_secret, kdc_secret, bn_ctx), 0);
  54415. BN_free(x);
  54416. BN_free(y);
  54417. BN_free(w);
  54418. EC_POINT_free(M);
  54419. EC_POINT_free(N);
  54420. EC_POINT_free(T);
  54421. EC_POINT_free(tmp1);
  54422. EC_POINT_free(tmp2);
  54423. EC_POINT_free(S);
  54424. EC_POINT_free(client_secret);
  54425. EC_POINT_free(kdc_secret);
  54426. EC_GROUP_free(group);
  54427. BN_CTX_free(bn_ctx);
  54428. #endif
  54429. return EXPECT_RESULT();
  54430. }
  54431. static int test_wolfSSL_EC_KEY_generate(void)
  54432. {
  54433. EXPECT_DECLS;
  54434. #ifdef OPENSSL_EXTRA
  54435. WOLFSSL_EC_KEY* key = NULL;
  54436. #ifndef HAVE_ECC_BRAINPOOL
  54437. WOLFSSL_EC_GROUP* group = NULL;
  54438. #endif
  54439. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54440. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(NULL), 0);
  54441. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  54442. wolfSSL_EC_KEY_free(key);
  54443. key = NULL;
  54444. #ifndef HAVE_ECC_BRAINPOOL
  54445. ExpectNotNull(group = wolfSSL_EC_GROUP_new_by_curve_name(
  54446. NID_brainpoolP256r1));
  54447. ExpectNotNull(key = wolfSSL_EC_KEY_new());
  54448. ExpectIntEQ(wolfSSL_EC_KEY_set_group(key, group), 1);
  54449. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 0);
  54450. wolfSSL_EC_KEY_free(key);
  54451. wolfSSL_EC_GROUP_free(group);
  54452. #endif
  54453. #endif
  54454. return EXPECT_RESULT();
  54455. }
  54456. static int test_EC_i2d(void)
  54457. {
  54458. EXPECT_DECLS;
  54459. #if defined(OPENSSL_EXTRA) && !defined(HAVE_FIPS)
  54460. EC_KEY *key = NULL;
  54461. EC_KEY *copy = NULL;
  54462. int len = 0;
  54463. unsigned char *buf = NULL;
  54464. unsigned char *p = NULL;
  54465. const unsigned char *tmp = NULL;
  54466. const unsigned char octBad[] = {
  54467. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  54468. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  54469. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  54470. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  54471. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  54472. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  54473. };
  54474. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54475. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  54476. ExpectIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  54477. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  54478. DYNAMIC_TYPE_TMP_BUFFER));
  54479. p = buf;
  54480. ExpectIntEQ(i2d_EC_PUBKEY(key, &p), len);
  54481. ExpectNull(o2i_ECPublicKey(NULL, NULL, -1));
  54482. ExpectNull(o2i_ECPublicKey(&copy, NULL, -1));
  54483. ExpectNull(o2i_ECPublicKey(&key, NULL, -1));
  54484. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  54485. ExpectNull(o2i_ECPublicKey(NULL, NULL, 0));
  54486. ExpectNull(o2i_ECPublicKey(&key, NULL, 0));
  54487. ExpectNull(o2i_ECPublicKey(&key, &tmp, 0));
  54488. tmp = buf;
  54489. ExpectNull(o2i_ECPublicKey(NULL, &tmp, 0));
  54490. ExpectNull(o2i_ECPublicKey(&copy, &tmp, 0));
  54491. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  54492. ExpectNull(o2i_ECPublicKey(&key, &tmp, -1));
  54493. ExpectIntEQ(i2o_ECPublicKey(NULL, NULL), 0);
  54494. ExpectIntEQ(i2o_ECPublicKey(NULL, &buf), 0);
  54495. tmp = buf;
  54496. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 0));
  54497. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 1));
  54498. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 0));
  54499. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 1));
  54500. ExpectNull(d2i_ECPrivateKey(&key, &tmp, 0));
  54501. ExpectIntEQ(i2d_ECPrivateKey(NULL, &p), 0);
  54502. ExpectIntEQ(i2d_ECPrivateKey(NULL, NULL), 0);
  54503. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer(NULL, NULL, -1), -1);
  54504. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1, 0), -1);
  54505. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, -1, 0), -1);
  54506. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, -1, 0), -1);
  54507. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, 0, 0), -1);
  54508. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1,
  54509. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  54510. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, len,
  54511. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  54512. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, len,
  54513. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  54514. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, -1,
  54515. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  54516. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len, 0), -1);
  54517. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len,
  54518. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  54519. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  54520. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  54521. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  54522. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  54523. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54524. buf = NULL;
  54525. buf = NULL;
  54526. ExpectIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  54527. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  54528. DYNAMIC_TYPE_TMP_BUFFER));
  54529. p = buf;
  54530. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  54531. p = NULL;
  54532. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  54533. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54534. p = NULL;
  54535. /* Bad point is also an invalid private key. */
  54536. tmp = octBad;
  54537. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, sizeof(octBad)));
  54538. tmp = buf;
  54539. ExpectNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  54540. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54541. buf = NULL;
  54542. buf = NULL;
  54543. ExpectIntGT((len = i2o_ECPublicKey(key, NULL)), 0);
  54544. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  54545. DYNAMIC_TYPE_TMP_BUFFER));
  54546. p = buf;
  54547. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  54548. p = NULL;
  54549. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  54550. tmp = buf;
  54551. ExpectNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  54552. tmp = octBad;
  54553. ExpectNull(o2i_ECPublicKey(&key, &tmp, sizeof(octBad)));
  54554. ExpectIntEQ(EC_KEY_check_key(NULL), 0);
  54555. ExpectIntEQ(EC_KEY_check_key(key), 1);
  54556. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  54557. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  54558. EC_KEY_free(key);
  54559. EC_KEY_free(copy);
  54560. #endif
  54561. return EXPECT_RESULT();
  54562. }
  54563. static int test_wolfSSL_EC_curve(void)
  54564. {
  54565. EXPECT_DECLS;
  54566. #if defined(OPENSSL_EXTRA)
  54567. int nid = NID_secp160k1;
  54568. const char* nid_name = NULL;
  54569. ExpectNull(EC_curve_nid2nist(NID_sha256));
  54570. ExpectNotNull(nid_name = EC_curve_nid2nist(nid));
  54571. ExpectIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  54572. ExpectIntEQ(EC_curve_nist2nid("INVALID"), 0);
  54573. ExpectIntEQ(EC_curve_nist2nid(nid_name), nid);
  54574. #endif
  54575. return EXPECT_RESULT();
  54576. }
  54577. static int test_wolfSSL_EC_KEY_dup(void)
  54578. {
  54579. EXPECT_DECLS;
  54580. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  54581. WOLFSSL_EC_KEY* ecKey = NULL;
  54582. WOLFSSL_EC_KEY* dupKey = NULL;
  54583. ecc_key* srcKey = NULL;
  54584. ecc_key* destKey = NULL;
  54585. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  54586. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  54587. /* Valid cases */
  54588. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54589. ExpectIntEQ(EC_KEY_check_key(dupKey), 1);
  54590. /* Compare pubkey */
  54591. if (ecKey != NULL) {
  54592. srcKey = (ecc_key*)ecKey->internal;
  54593. }
  54594. if (dupKey != NULL) {
  54595. destKey = (ecc_key*)dupKey->internal;
  54596. }
  54597. ExpectIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  54598. /* compare EC_GROUP */
  54599. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  54600. /* compare EC_POINT */
  54601. ExpectIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  54602. dupKey->pub_key, NULL), MP_EQ);
  54603. /* compare BIGNUM */
  54604. ExpectIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  54605. wolfSSL_EC_KEY_free(dupKey);
  54606. dupKey = NULL;
  54607. /* Invalid cases */
  54608. /* NULL key */
  54609. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  54610. /* NULL ecc_key */
  54611. if (ecKey != NULL) {
  54612. wc_ecc_free((ecc_key*)ecKey->internal);
  54613. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  54614. ecKey->internal = NULL; /* Set ecc_key to NULL */
  54615. }
  54616. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54617. wolfSSL_EC_KEY_free(ecKey);
  54618. ecKey = NULL;
  54619. wolfSSL_EC_KEY_free(dupKey);
  54620. dupKey = NULL;
  54621. /* NULL Group */
  54622. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  54623. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  54624. if (ecKey != NULL) {
  54625. wolfSSL_EC_GROUP_free(ecKey->group);
  54626. ecKey->group = NULL; /* Set group to NULL */
  54627. }
  54628. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54629. wolfSSL_EC_KEY_free(ecKey);
  54630. ecKey = NULL;
  54631. wolfSSL_EC_KEY_free(dupKey);
  54632. dupKey = NULL;
  54633. /* NULL public key */
  54634. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  54635. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  54636. if (ecKey != NULL) {
  54637. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  54638. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  54639. }
  54640. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54641. if (ecKey != NULL) {
  54642. wolfSSL_EC_POINT_free(ecKey->pub_key);
  54643. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  54644. }
  54645. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54646. wolfSSL_EC_KEY_free(ecKey);
  54647. ecKey = NULL;
  54648. wolfSSL_EC_KEY_free(dupKey);
  54649. dupKey = NULL;
  54650. /* NULL private key */
  54651. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  54652. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  54653. if (ecKey != NULL) {
  54654. wolfSSL_BN_free(ecKey->priv_key);
  54655. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  54656. }
  54657. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54658. wolfSSL_EC_KEY_free(ecKey);
  54659. ecKey = NULL;
  54660. wolfSSL_EC_KEY_free(dupKey);
  54661. dupKey = NULL;
  54662. /* Test EC_KEY_up_ref */
  54663. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  54664. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  54665. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  54666. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  54667. /* reference count doesn't follow duplicate */
  54668. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54669. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  54670. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  54671. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  54672. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  54673. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  54674. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  54675. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  54676. #endif
  54677. return EXPECT_RESULT();
  54678. }
  54679. static int test_wolfSSL_EC_KEY_set_group(void)
  54680. {
  54681. EXPECT_DECLS;
  54682. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  54683. defined(OPENSSL_EXTRA)
  54684. EC_KEY *key = NULL;
  54685. EC_GROUP *group = NULL;
  54686. const EC_GROUP *group2 = NULL;
  54687. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  54688. ExpectNotNull(key = EC_KEY_new());
  54689. ExpectNull(EC_KEY_get0_group(NULL));
  54690. ExpectIntEQ(EC_KEY_set_group(NULL, NULL), 0);
  54691. ExpectIntEQ(EC_KEY_set_group(key, NULL), 0);
  54692. ExpectIntEQ(EC_KEY_set_group(NULL, group), 0);
  54693. ExpectIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  54694. ExpectNotNull(group2 = EC_KEY_get0_group(key));
  54695. ExpectIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  54696. EC_GROUP_free(group);
  54697. EC_KEY_free(key);
  54698. #endif
  54699. return EXPECT_RESULT();
  54700. }
  54701. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  54702. {
  54703. EXPECT_DECLS;
  54704. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  54705. BIO* bio = NULL;
  54706. EC_KEY* key = NULL;
  54707. /* Error condition: NULL key. */
  54708. ExpectIntLT(EC_KEY_get_conv_form(NULL), 0);
  54709. ExpectNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  54710. ExpectNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  54711. /* Conversion form defaults to uncompressed. */
  54712. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  54713. #ifdef HAVE_COMP_KEY
  54714. /* Explicitly set to compressed. */
  54715. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  54716. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  54717. #else
  54718. /* Will still work just won't change anything. */
  54719. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  54720. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  54721. EC_KEY_set_conv_form(key, POINT_CONVERSION_UNCOMPRESSED);
  54722. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  54723. #endif
  54724. EC_KEY_set_conv_form(NULL, POINT_CONVERSION_UNCOMPRESSED);
  54725. BIO_free(bio);
  54726. EC_KEY_free(key);
  54727. #endif
  54728. return EXPECT_RESULT();
  54729. }
  54730. static int test_wolfSSL_EC_KEY_private_key(void)
  54731. {
  54732. EXPECT_DECLS;
  54733. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  54734. WOLFSSL_EC_KEY* key = NULL;
  54735. WOLFSSL_BIGNUM* priv = NULL;
  54736. WOLFSSL_BIGNUM* priv2 = NULL;
  54737. WOLFSSL_BIGNUM* bn;
  54738. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54739. ExpectNotNull(priv = wolfSSL_BN_new());
  54740. ExpectNotNull(priv2 = wolfSSL_BN_new());
  54741. ExpectIntNE(BN_set_word(priv, 2), 0);
  54742. ExpectIntNE(BN_set_word(priv2, 2), 0);
  54743. ExpectNull(wolfSSL_EC_KEY_get0_private_key(NULL));
  54744. /* No private key set. */
  54745. ExpectNull(wolfSSL_EC_KEY_get0_private_key(key));
  54746. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, NULL), 0);
  54747. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, NULL), 0);
  54748. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, priv), 0);
  54749. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv), 1);
  54750. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  54751. ExpectPtrNE(bn, priv);
  54752. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv2), 1);
  54753. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  54754. ExpectPtrNE(bn, priv2);
  54755. wolfSSL_BN_free(priv2);
  54756. wolfSSL_BN_free(priv);
  54757. wolfSSL_EC_KEY_free(key);
  54758. #endif
  54759. return EXPECT_RESULT();
  54760. }
  54761. static int test_wolfSSL_EC_KEY_public_key(void)
  54762. {
  54763. EXPECT_DECLS;
  54764. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  54765. WOLFSSL_EC_KEY* key = NULL;
  54766. WOLFSSL_EC_POINT* pub = NULL;
  54767. WOLFSSL_EC_POINT* point = NULL;
  54768. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54769. ExpectNull(wolfSSL_EC_KEY_get0_public_key(NULL));
  54770. ExpectNotNull(wolfSSL_EC_KEY_get0_public_key(key));
  54771. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  54772. ExpectNotNull(pub = wolfSSL_EC_KEY_get0_public_key(key));
  54773. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, NULL), 0);
  54774. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, NULL), 0);
  54775. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, pub), 0);
  54776. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, pub), 1);
  54777. ExpectNotNull(point = wolfSSL_EC_KEY_get0_public_key(key));
  54778. ExpectPtrEq(point, pub);
  54779. wolfSSL_EC_KEY_free(key);
  54780. #endif
  54781. return EXPECT_RESULT();
  54782. }
  54783. static int test_wolfSSL_EC_KEY_print_fp(void)
  54784. {
  54785. EXPECT_DECLS;
  54786. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  54787. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224 && \
  54788. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  54789. !defined(NO_STDIO_FILESYSTEM)
  54790. EC_KEY* key = NULL;
  54791. /* Bad file pointer. */
  54792. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  54793. /* NULL key. */
  54794. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WOLFSSL_FAILURE);
  54795. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  54796. /* Negative indent. */
  54797. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WOLFSSL_FAILURE);
  54798. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  54799. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  54800. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  54801. wolfSSL_EC_KEY_free(key);
  54802. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  54803. NID_X9_62_prime256v1)));
  54804. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  54805. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  54806. wolfSSL_EC_KEY_free(key);
  54807. #endif
  54808. return EXPECT_RESULT();
  54809. }
  54810. static int test_wolfSSL_EC_get_builtin_curves(void)
  54811. {
  54812. EXPECT_DECLS;
  54813. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  54814. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  54815. EC_builtin_curve* curves = NULL;
  54816. size_t crv_len = 0;
  54817. size_t i = 0;
  54818. ExpectIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  54819. ExpectNotNull(curves = (EC_builtin_curve*)XMALLOC(
  54820. sizeof(EC_builtin_curve) * crv_len, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  54821. ExpectIntEQ((EC_get_builtin_curves(curves, 0)), crv_len);
  54822. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  54823. for (i = 0; EXPECT_SUCCESS() && (i < crv_len); i++) {
  54824. if (curves[i].comment != NULL) {
  54825. ExpectStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  54826. }
  54827. }
  54828. if (crv_len > 1) {
  54829. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len - 1), crv_len - 1);
  54830. }
  54831. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54832. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  54833. #endif /* OPENSSL_EXTRA || OPENSSL_ALL */
  54834. return EXPECT_RESULT();
  54835. }
  54836. static int test_wolfSSL_ECDSA_SIG(void)
  54837. {
  54838. EXPECT_DECLS;
  54839. #ifdef OPENSSL_EXTRA
  54840. WOLFSSL_ECDSA_SIG* sig = NULL;
  54841. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  54842. WOLFSSL_BIGNUM* r = NULL;
  54843. WOLFSSL_BIGNUM* s = NULL;
  54844. const WOLFSSL_BIGNUM* r2 = NULL;
  54845. const WOLFSSL_BIGNUM* s2 = NULL;
  54846. const unsigned char* cp = NULL;
  54847. unsigned char* p = NULL;
  54848. unsigned char outSig[8];
  54849. unsigned char sigData[8] =
  54850. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  54851. unsigned char sigDataBad[8] =
  54852. { 0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  54853. wolfSSL_ECDSA_SIG_free(NULL);
  54854. ExpectNotNull(sig = wolfSSL_ECDSA_SIG_new());
  54855. ExpectNotNull(r = wolfSSL_BN_new());
  54856. ExpectNotNull(s = wolfSSL_BN_new());
  54857. ExpectIntEQ(wolfSSL_BN_set_word(r, 1), 1);
  54858. ExpectIntEQ(wolfSSL_BN_set_word(s, 1), 1);
  54859. wolfSSL_ECDSA_SIG_get0(NULL, NULL, NULL);
  54860. wolfSSL_ECDSA_SIG_get0(NULL, &r2, NULL);
  54861. wolfSSL_ECDSA_SIG_get0(NULL, NULL, &s2);
  54862. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  54863. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, NULL), 0);
  54864. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, NULL), 0);
  54865. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, NULL), 0);
  54866. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, s), 0);
  54867. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, s), 0);
  54868. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, s), 0);
  54869. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, NULL), 0);
  54870. r2 = NULL;
  54871. s2 = NULL;
  54872. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  54873. ExpectNull(r2);
  54874. ExpectNull(s2);
  54875. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, s), 1);
  54876. if (EXPECT_FAIL()) {
  54877. wolfSSL_BN_free(r);
  54878. wolfSSL_BN_free(s);
  54879. }
  54880. wolfSSL_ECDSA_SIG_get0(sig, &r2, &s2);
  54881. ExpectPtrEq(r2, r);
  54882. ExpectPtrEq(s2, s);
  54883. r2 = NULL;
  54884. wolfSSL_ECDSA_SIG_get0(sig, &r2, NULL);
  54885. ExpectPtrEq(r2, r);
  54886. s2 = NULL;
  54887. wolfSSL_ECDSA_SIG_get0(sig, NULL, &s2);
  54888. ExpectPtrEq(s2, s);
  54889. /* r and s are freed when sig is freed. */
  54890. wolfSSL_ECDSA_SIG_free(sig);
  54891. sig = NULL;
  54892. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData)));
  54893. cp = sigDataBad;
  54894. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigDataBad)));
  54895. cp = sigData;
  54896. ExpectNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  54897. ExpectIntEQ((cp == sigData + 8), 1);
  54898. cp = sigData;
  54899. ExpectNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  54900. ExpectNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  54901. ExpectIntEQ((sig == sig2), 1);
  54902. cp = outSig;
  54903. p = outSig;
  54904. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  54905. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  54906. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  54907. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  54908. ExpectIntEQ((p == outSig + 8), 1);
  54909. ExpectIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  54910. wolfSSL_ECDSA_SIG_free(sig);
  54911. #endif
  54912. return EXPECT_RESULT();
  54913. }
  54914. static int test_ECDSA_size_sign(void)
  54915. {
  54916. EXPECT_DECLS;
  54917. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  54918. EC_KEY* key = NULL;
  54919. ECDSA_SIG* ecdsaSig = NULL;
  54920. int id;
  54921. byte hash[WC_MAX_DIGEST_SIZE];
  54922. byte hash2[WC_MAX_DIGEST_SIZE];
  54923. byte sig[ECC_MAX_SIG_SIZE];
  54924. unsigned int sigSz = sizeof(sig);
  54925. XMEMSET(hash, 123, sizeof(hash));
  54926. XMEMSET(hash2, 234, sizeof(hash2));
  54927. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  54928. ExpectIntEQ(id, ECC_SECP256R1);
  54929. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54930. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  54931. ExpectIntGE(ECDSA_size(NULL), 0);
  54932. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, NULL), 0);
  54933. ExpectIntEQ(ECDSA_sign(0, NULL, sizeof(hash), sig, &sigSz, key), 0);
  54934. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), NULL, &sigSz, key), 0);
  54935. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, NULL), 0);
  54936. ExpectIntEQ(ECDSA_verify(0, NULL, sizeof(hash), sig, sigSz, key), 0);
  54937. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), NULL, sigSz, key), 0);
  54938. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  54939. ExpectIntGE(ECDSA_size(key), sigSz);
  54940. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, sigSz, key), 1);
  54941. ExpectIntEQ(ECDSA_verify(0, hash2, sizeof(hash2), sig, sigSz, key), 0);
  54942. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), NULL));
  54943. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), key));
  54944. ExpectNull(ECDSA_do_sign(hash, sizeof(hash), NULL));
  54945. ExpectNotNull(ecdsaSig = ECDSA_do_sign(hash, sizeof(hash), key));
  54946. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, NULL), -1);
  54947. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, NULL), -1);
  54948. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, NULL), -1);
  54949. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, key), -1);
  54950. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, key), -1);
  54951. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, key), -1);
  54952. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, NULL), -1);
  54953. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, key), 1);
  54954. ExpectIntEQ(ECDSA_do_verify(hash2, sizeof(hash2), ecdsaSig, key), 0);
  54955. ECDSA_SIG_free(ecdsaSig);
  54956. EC_KEY_free(key);
  54957. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP */
  54958. return EXPECT_RESULT();
  54959. }
  54960. static int test_ECDH_compute_key(void)
  54961. {
  54962. EXPECT_DECLS;
  54963. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  54964. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  54965. EC_KEY* key1 = NULL;
  54966. EC_KEY* key2 = NULL;
  54967. EC_POINT* pub1 = NULL;
  54968. EC_POINT* pub2 = NULL;
  54969. byte secret1[32];
  54970. byte secret2[32];
  54971. int i;
  54972. ExpectNotNull(key1 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54973. ExpectIntEQ(EC_KEY_generate_key(key1), 1);
  54974. ExpectNotNull(pub1 = wolfSSL_EC_KEY_get0_public_key(key1));
  54975. ExpectNotNull(key2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54976. ExpectIntEQ(EC_KEY_generate_key(key2), 1);
  54977. ExpectNotNull(pub2 = wolfSSL_EC_KEY_get0_public_key(key2));
  54978. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, NULL, NULL), 0);
  54979. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, NULL, NULL),
  54980. 0);
  54981. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, NULL, NULL), 0);
  54982. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, key1, NULL), 0);
  54983. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, key1, NULL), 0);
  54984. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, key1, NULL),
  54985. 0);
  54986. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, NULL, NULL),
  54987. 0);
  54988. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1) - 16, pub2, key1,
  54989. NULL), 0);
  54990. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, key1, NULL),
  54991. sizeof(secret1));
  54992. ExpectIntEQ(ECDH_compute_key(secret2, sizeof(secret2), pub1, key2, NULL),
  54993. sizeof(secret2));
  54994. for (i = 0; i < (int)sizeof(secret1); i++) {
  54995. ExpectIntEQ(secret1[i], secret2[i]);
  54996. }
  54997. EC_KEY_free(key2);
  54998. EC_KEY_free(key1);
  54999. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP &&
  55000. * !WOLF_CRYPTO_CB_ONLY_ECC */
  55001. return EXPECT_RESULT();
  55002. }
  55003. #endif /* HAVE_ECC && !OPENSSL_NO_PK */
  55004. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  55005. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  55006. !defined(NO_ASN_TIME)
  55007. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey,
  55008. int expectedDerSz)
  55009. {
  55010. EXPECT_DECLS;
  55011. X509* x509 = NULL;
  55012. BIGNUM* serial_number = NULL;
  55013. X509_NAME* name = NULL;
  55014. time_t epoch_off = 0;
  55015. ASN1_INTEGER* asn1_serial_number;
  55016. long not_before, not_after;
  55017. int derSz;
  55018. ExpectNotNull(x509 = X509_new());
  55019. ExpectIntNE(X509_set_pubkey(x509, pkey), 0);
  55020. ExpectNotNull(serial_number = BN_new());
  55021. ExpectIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  55022. ExpectNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  55023. ExpectNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  55024. /* version 3 */
  55025. ExpectIntNE(X509_set_version(x509, 2L), 0);
  55026. ExpectNotNull(name = X509_NAME_new());
  55027. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  55028. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  55029. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_pkcs9_contentType,
  55030. MBSTRING_UTF8,(unsigned char*)"Server", -1, -1, 0), 0);
  55031. ExpectIntNE(X509_set_subject_name(x509, name), 0);
  55032. ExpectIntNE(X509_set_issuer_name(x509, name), 0);
  55033. not_before = (long)wc_Time(NULL);
  55034. not_after = not_before + (365 * 24 * 60 * 60);
  55035. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before,
  55036. &epoch_off));
  55037. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after,
  55038. &epoch_off));
  55039. ExpectIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  55040. ExpectNotNull(wolfSSL_X509_get_der(x509, &derSz));
  55041. ExpectIntGE(derSz, expectedDerSz);
  55042. BN_free(serial_number);
  55043. X509_NAME_free(name);
  55044. X509_free(x509);
  55045. return EXPECT_RESULT();
  55046. }
  55047. #endif
  55048. static int test_openssl_generate_key_and_cert(void)
  55049. {
  55050. EXPECT_DECLS;
  55051. #if defined(OPENSSL_EXTRA)
  55052. int expectedDerSz;
  55053. EVP_PKEY* pkey = NULL;
  55054. #ifdef HAVE_ECC
  55055. EC_KEY* ec_key = NULL;
  55056. #endif
  55057. #if !defined(NO_RSA)
  55058. int key_length = 2048;
  55059. BIGNUM* exponent = NULL;
  55060. RSA* rsa = NULL;
  55061. ExpectNotNull(pkey = EVP_PKEY_new());
  55062. ExpectNotNull(exponent = BN_new());
  55063. ExpectNotNull(rsa = RSA_new());
  55064. ExpectIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  55065. #ifndef WOLFSSL_KEY_GEN
  55066. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  55067. #if defined(USE_CERT_BUFFERS_1024)
  55068. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  55069. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  55070. key_length = 1024;
  55071. #elif defined(USE_CERT_BUFFERS_2048)
  55072. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  55073. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  55074. #else
  55075. RSA_free(rsa);
  55076. rsa = NULL;
  55077. #endif
  55078. #else
  55079. ExpectIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  55080. ExpectIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  55081. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  55082. ExpectIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  55083. #endif
  55084. if (rsa) {
  55085. ExpectIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  55086. if (EXPECT_FAIL()) {
  55087. RSA_free(rsa);
  55088. }
  55089. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  55090. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  55091. expectedDerSz = 743;
  55092. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey,
  55093. expectedDerSz), TEST_SUCCESS);
  55094. #endif
  55095. }
  55096. EVP_PKEY_free(pkey);
  55097. pkey = NULL;
  55098. BN_free(exponent);
  55099. #endif /* !NO_RSA */
  55100. #ifdef HAVE_ECC
  55101. ExpectNotNull(pkey = EVP_PKEY_new());
  55102. ExpectNotNull(ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  55103. #ifndef NO_WOLFSSL_STUB
  55104. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  55105. #endif
  55106. ExpectIntNE(EC_KEY_generate_key(ec_key), 0);
  55107. ExpectIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  55108. if (EXPECT_FAIL()) {
  55109. EC_KEY_free(ec_key);
  55110. }
  55111. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  55112. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  55113. expectedDerSz = 344;
  55114. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey, expectedDerSz),
  55115. TEST_SUCCESS);
  55116. #endif
  55117. EVP_PKEY_free(pkey);
  55118. #endif /* HAVE_ECC */
  55119. (void)pkey;
  55120. (void)expectedDerSz;
  55121. #endif /* OPENSSL_EXTRA */
  55122. return EXPECT_RESULT();
  55123. }
  55124. static int test_stubs_are_stubs(void)
  55125. {
  55126. EXPECT_DECLS;
  55127. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB) && \
  55128. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  55129. WOLFSSL_CTX* ctx = NULL;
  55130. WOLFSSL_CTX* ctxN = NULL;
  55131. #ifndef NO_WOLFSSL_CLIENT
  55132. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  55133. #elif !defined(NO_WOLFSSL_SERVER)
  55134. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  55135. #endif
  55136. #define CHECKZERO_RET(x, y, z) ExpectIntEQ((int) x(y), 0); \
  55137. ExpectIntEQ((int) x(z), 0)
  55138. /* test logic, all stubs return same result regardless of ctx being NULL
  55139. * as there are no sanity checks, it's just a stub! If at some
  55140. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  55141. * if invalid inputs are supplied. Test calling both
  55142. * with and without valid inputs, if a stub functionality remains unchanged.
  55143. */
  55144. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  55145. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  55146. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  55147. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  55148. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  55149. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  55150. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  55151. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  55152. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  55153. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  55154. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  55155. wolfSSL_CTX_free(ctx);
  55156. ctx = NULL;
  55157. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB && (!NO_WOLFSSL_CLIENT ||
  55158. * !NO_WOLFSSL_SERVER) */
  55159. return EXPECT_RESULT();
  55160. }
  55161. static int test_CONF_modules_xxx(void)
  55162. {
  55163. int res = TEST_SKIPPED;
  55164. #if defined(OPENSSL_EXTRA)
  55165. CONF_modules_free();
  55166. CONF_modules_unload(0);
  55167. CONF_modules_unload(1);
  55168. CONF_modules_unload(-1);
  55169. res = TEST_SUCCESS;
  55170. #endif /* OPENSSL_EXTRA */
  55171. return res;
  55172. }
  55173. static int test_CRYPTO_set_dynlock_xxx(void)
  55174. {
  55175. int res = TEST_SKIPPED;
  55176. #if defined(OPENSSL_EXTRA)
  55177. CRYPTO_set_dynlock_create_callback(
  55178. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  55179. CRYPTO_set_dynlock_create_callback(
  55180. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  55181. CRYPTO_set_dynlock_destroy_callback(
  55182. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  55183. CRYPTO_set_dynlock_destroy_callback(
  55184. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  55185. CRYPTO_set_dynlock_lock_callback(
  55186. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  55187. CRYPTO_set_dynlock_lock_callback(
  55188. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  55189. res = TEST_SUCCESS;
  55190. #endif /* OPENSSL_EXTRA */
  55191. return res;
  55192. }
  55193. static int test_CRYPTO_THREADID_xxx(void)
  55194. {
  55195. EXPECT_DECLS;
  55196. #if defined(OPENSSL_EXTRA)
  55197. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  55198. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  55199. ExpectIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  55200. #endif /* OPENSSL_EXTRA */
  55201. return EXPECT_RESULT();
  55202. }
  55203. static int test_ENGINE_cleanup(void)
  55204. {
  55205. int res = TEST_SKIPPED;
  55206. #if defined(OPENSSL_EXTRA)
  55207. ENGINE_cleanup();
  55208. res = TEST_SUCCESS;
  55209. #endif /* OPENSSL_EXTRA */
  55210. return res;
  55211. }
  55212. static int test_wolfSSL_CTX_LoadCRL(void)
  55213. {
  55214. EXPECT_DECLS;
  55215. #if defined(HAVE_CRL) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  55216. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  55217. WOLFSSL_CTX* ctx = NULL;
  55218. WOLFSSL* ssl = NULL;
  55219. const char* badPath = "dummypath";
  55220. const char* validPath = "./certs/crl";
  55221. const char* validFilePath = "./certs/crl/cliCrl.pem";
  55222. const char* issuerCert = "./certs/client-cert.pem";
  55223. int derType = WOLFSSL_FILETYPE_ASN1;
  55224. int pemType = WOLFSSL_FILETYPE_PEM;
  55225. #ifdef HAVE_CRL_MONITOR
  55226. int monitor = WOLFSSL_CRL_MONITOR;
  55227. #else
  55228. int monitor = 0;
  55229. #endif
  55230. WOLFSSL_CERT_MANAGER* cm = NULL;
  55231. #define FAIL_T1(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  55232. BAD_FUNC_ARG)
  55233. #define FAIL_T2(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  55234. NOT_COMPILED_IN)
  55235. #define SUCC_T(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  55236. WOLFSSL_SUCCESS)
  55237. #ifndef NO_WOLFSSL_CLIENT
  55238. #define NEW_CTX(ctx) ExpectNotNull( \
  55239. (ctx) = wolfSSL_CTX_new(wolfSSLv23_client_method()))
  55240. #elif !defined(NO_WOLFSSL_SERVER)
  55241. #define NEW_CTX(ctx) ExpectNotNull( \
  55242. (ctx) = wolfSSL_CTX_new(wolfSSLv23_server_method()))
  55243. #else
  55244. #define NEW_CTX(ctx) return
  55245. #endif
  55246. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  55247. NEW_CTX(ctx);
  55248. #ifndef HAVE_CRL_MONITOR
  55249. FAIL_T2(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, WOLFSSL_CRL_MONITOR);
  55250. wolfSSL_CTX_free(ctx);
  55251. NEW_CTX(ctx);
  55252. #endif
  55253. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  55254. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  55255. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  55256. wolfSSL_CTX_free(ctx);
  55257. ctx = NULL;
  55258. NEW_CTX(ctx);
  55259. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  55260. WOLFSSL_SUCCESS);
  55261. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  55262. wolfSSL_CTX_free(ctx);
  55263. ctx = NULL;
  55264. NEW_CTX(ctx);
  55265. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  55266. WOLFSSL_SUCCESS);
  55267. ExpectNotNull(ssl = wolfSSL_new(ctx));
  55268. ExpectIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  55269. wolfSSL_free(ssl);
  55270. ssl = NULL;
  55271. wolfSSL_CTX_free(ctx);
  55272. ctx = NULL;
  55273. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  55274. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  55275. WOLFSSL_SUCCESS);
  55276. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType),
  55277. WOLFSSL_SUCCESS);
  55278. wolfSSL_CertManagerFree(cm);
  55279. #endif
  55280. return EXPECT_RESULT();
  55281. }
  55282. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  55283. static int test_multiple_crls_same_issuer_ctx_ready(WOLFSSL_CTX* ctx)
  55284. {
  55285. EXPECT_DECLS;
  55286. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  55287. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, "./certs/crl/crl.pem",
  55288. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  55289. return EXPECT_RESULT();
  55290. }
  55291. #endif
  55292. static int test_multiple_crls_same_issuer(void)
  55293. {
  55294. EXPECT_DECLS;
  55295. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  55296. test_ssl_cbf client_cbs, server_cbs;
  55297. struct {
  55298. const char* server_cert;
  55299. const char* server_key;
  55300. } test_params[] = {
  55301. { "./certs/server-cert.pem", "./certs/server-key.pem" },
  55302. { "./certs/server-revoked-cert.pem", "./certs/server-revoked-key.pem" }
  55303. };
  55304. size_t i;
  55305. for (i = 0; i < (sizeof(test_params)/sizeof(*test_params)); i++) {
  55306. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  55307. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  55308. server_cbs.certPemFile = test_params[i].server_cert;
  55309. server_cbs.keyPemFile = test_params[i].server_key;
  55310. client_cbs.crlPemFile = "./certs/crl/extra-crls/general-server-crl.pem";
  55311. client_cbs.ctx_ready = test_multiple_crls_same_issuer_ctx_ready;
  55312. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  55313. &server_cbs, NULL), TEST_FAIL);
  55314. }
  55315. #endif
  55316. return EXPECT_RESULT();
  55317. }
  55318. static int test_SetTmpEC_DHE_Sz(void)
  55319. {
  55320. EXPECT_DECLS;
  55321. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  55322. WOLFSSL_CTX *ctx = NULL;
  55323. WOLFSSL *ssl = NULL;
  55324. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  55325. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  55326. ExpectNotNull(ssl = wolfSSL_new(ctx));
  55327. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  55328. wolfSSL_free(ssl);
  55329. wolfSSL_CTX_free(ctx);
  55330. #endif
  55331. return EXPECT_RESULT();
  55332. }
  55333. static int test_wolfSSL_CTX_get0_privatekey(void)
  55334. {
  55335. EXPECT_DECLS;
  55336. #ifdef OPENSSL_ALL
  55337. WOLFSSL_CTX* ctx = NULL;
  55338. (void)ctx;
  55339. #ifndef NO_RSA
  55340. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  55341. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  55342. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  55343. WOLFSSL_FILETYPE_PEM));
  55344. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  55345. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  55346. WOLFSSL_FILETYPE_PEM));
  55347. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  55348. wolfSSL_CTX_free(ctx);
  55349. ctx = NULL;
  55350. #endif
  55351. #ifdef HAVE_ECC
  55352. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  55353. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  55354. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  55355. WOLFSSL_FILETYPE_PEM));
  55356. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  55357. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  55358. WOLFSSL_FILETYPE_PEM));
  55359. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  55360. wolfSSL_CTX_free(ctx);
  55361. #endif
  55362. #endif
  55363. return EXPECT_RESULT();
  55364. }
  55365. static int test_wolfSSL_dtls_set_mtu(void)
  55366. {
  55367. EXPECT_DECLS;
  55368. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  55369. !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_DTLS) && \
  55370. !defined(WOLFSSL_NO_TLS12)
  55371. WOLFSSL_CTX* ctx = NULL;
  55372. WOLFSSL* ssl = NULL;
  55373. const char* testCertFile;
  55374. const char* testKeyFile;
  55375. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  55376. #ifndef NO_RSA
  55377. testCertFile = svrCertFile;
  55378. testKeyFile = svrKeyFile;
  55379. #elif defined(HAVE_ECC)
  55380. testCertFile = eccCertFile;
  55381. testKeyFile = eccKeyFile;
  55382. #endif
  55383. if (testCertFile != NULL && testKeyFile != NULL) {
  55384. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  55385. WOLFSSL_FILETYPE_PEM));
  55386. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  55387. WOLFSSL_FILETYPE_PEM));
  55388. }
  55389. ExpectNotNull(ssl = wolfSSL_new(ctx));
  55390. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  55391. ExpectIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  55392. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  55393. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  55394. ExpectIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  55395. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  55396. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  55397. wolfSSL_free(ssl);
  55398. wolfSSL_CTX_free(ctx);
  55399. #endif
  55400. return EXPECT_RESULT();
  55401. }
  55402. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  55403. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  55404. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  55405. enum HandShakeType hsType, word16 length)
  55406. {
  55407. size_t idx = 0;
  55408. byte* l;
  55409. /* record layer */
  55410. /* handshake type */
  55411. out[idx++] = handshake;
  55412. /* protocol version */
  55413. out[idx++] = 0xfe;
  55414. out[idx++] = 0xfd; /* DTLS 1.2 */
  55415. /* epoch 0 */
  55416. XMEMSET(out + idx, 0, 2);
  55417. idx += 2;
  55418. /* sequence number */
  55419. XMEMSET(out + idx, 0, 6);
  55420. c32toa(seq, out + idx + 2);
  55421. idx += 6;
  55422. /* length in BE */
  55423. if (length)
  55424. c16toa(length, out + idx);
  55425. else
  55426. c16toa(outSz - idx - 2, out + idx);
  55427. idx += 2;
  55428. /* handshake layer */
  55429. /* handshake type */
  55430. out[idx++] = (byte)hsType;
  55431. /* length */
  55432. l = out + idx;
  55433. idx += 3;
  55434. /* message seq */
  55435. c16toa(0, out + idx);
  55436. idx += 2;
  55437. /* frag offset */
  55438. c32to24(0, out + idx);
  55439. idx += 3;
  55440. /* frag length */
  55441. c32to24((word32)outSz - (word32)idx - 3, l);
  55442. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  55443. idx += 3;
  55444. XMEMSET(out + idx, 0, outSz - idx);
  55445. }
  55446. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  55447. {
  55448. byte msg[] = "This is a msg for the client";
  55449. byte reply[40];
  55450. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  55451. reply[sizeof(reply) - 1] = '\0';
  55452. fprintf(stderr, "Client message: %s\n", reply);
  55453. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  55454. }
  55455. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  55456. {
  55457. byte ch[50];
  55458. int fd = wolfSSL_get_fd(ssl);
  55459. byte msg[] = "This is a msg for the server";
  55460. byte reply[40];
  55461. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  55462. /* Server should ignore this datagram */
  55463. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  55464. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  55465. /* Server should ignore this datagram */
  55466. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  55467. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  55468. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  55469. reply[sizeof(reply) - 1] = '\0';
  55470. fprintf(stderr, "Server response: %s\n", reply);
  55471. }
  55472. static int test_wolfSSL_dtls_plaintext(void)
  55473. {
  55474. callback_functions func_cb_client;
  55475. callback_functions func_cb_server;
  55476. size_t i;
  55477. struct test_params {
  55478. method_provider client_meth;
  55479. method_provider server_meth;
  55480. ssl_callback on_result_server;
  55481. ssl_callback on_result_client;
  55482. } params[] = {
  55483. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  55484. test_wolfSSL_dtls_plaintext_server,
  55485. test_wolfSSL_dtls_plaintext_client},
  55486. };
  55487. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  55488. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  55489. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  55490. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  55491. func_cb_server.method = params[i].server_meth;
  55492. func_cb_client.method = params[i].client_meth;
  55493. func_cb_client.on_result = params[i].on_result_client;
  55494. func_cb_server.on_result = params[i].on_result_server;
  55495. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  55496. if (!func_cb_client.return_code)
  55497. return TEST_FAIL;
  55498. if (!func_cb_server.return_code)
  55499. return TEST_FAIL;
  55500. }
  55501. return TEST_RES_CHECK(1);
  55502. }
  55503. #else
  55504. static int test_wolfSSL_dtls_plaintext(void) {
  55505. return TEST_SKIPPED;
  55506. }
  55507. #endif
  55508. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  55509. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  55510. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  55511. {
  55512. byte b[1100]; /* buffer for the messages to send */
  55513. size_t idx = 0;
  55514. size_t seq_offset = 0;
  55515. size_t msg_offset = 0;
  55516. int i;
  55517. int fd = wolfSSL_get_fd(ssl);
  55518. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  55519. word32 seq_number = 100; /* start high so server definitely reads this */
  55520. word16 msg_number = 50; /* start high so server has to buffer this */
  55521. AssertIntEQ(ret, 1);
  55522. /* Now let's start spamming the peer with fragments it needs to store */
  55523. XMEMSET(b, -1, sizeof(b));
  55524. /* record layer */
  55525. /* handshake type */
  55526. b[idx++] = 22;
  55527. /* protocol version */
  55528. b[idx++] = 0xfe;
  55529. b[idx++] = 0xfd; /* DTLS 1.2 */
  55530. /* epoch 0 */
  55531. XMEMSET(b + idx, 0, 2);
  55532. idx += 2;
  55533. /* sequence number */
  55534. XMEMSET(b + idx, 0, 6);
  55535. seq_offset = idx + 2; /* increment only the low 32 bits */
  55536. idx += 6;
  55537. /* static length in BE */
  55538. c16toa(42, b + idx);
  55539. idx += 2;
  55540. /* handshake layer */
  55541. /* cert type */
  55542. b[idx++] = 11;
  55543. /* length */
  55544. c32to24(1000, b + idx);
  55545. idx += 3;
  55546. /* message seq */
  55547. c16toa(0, b + idx);
  55548. msg_offset = idx;
  55549. idx += 2;
  55550. /* frag offset */
  55551. c32to24(500, b + idx);
  55552. idx += 3;
  55553. /* frag length */
  55554. c32to24(30, b + idx);
  55555. idx += 3;
  55556. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  55557. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  55558. seq_number++, msg_number++, i++) {
  55559. struct timespec delay;
  55560. XMEMSET(&delay, 0, sizeof(delay));
  55561. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  55562. c32toa(seq_number, b + seq_offset);
  55563. c16toa(msg_number, b + msg_offset);
  55564. ret = (int)send(fd, b, 55, 0);
  55565. nanosleep(&delay, NULL);
  55566. }
  55567. }
  55568. #ifdef WOLFSSL_DTLS13
  55569. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  55570. {
  55571. const word16 sendCountMax = 100;
  55572. byte b[150]; /* buffer for the messages to send */
  55573. size_t idx = 0;
  55574. size_t msg_offset = 0;
  55575. int fd = wolfSSL_get_fd(ssl);
  55576. word16 msg_number = 10; /* start high so server has to buffer this */
  55577. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  55578. AssertIntEQ(ret, 1);
  55579. /* Now let's start spamming the peer with fragments it needs to store */
  55580. XMEMSET(b, -1, sizeof(b));
  55581. /* handshake type */
  55582. b[idx++] = 11;
  55583. /* length */
  55584. c32to24(10000, b + idx);
  55585. idx += 3;
  55586. /* message_seq */
  55587. msg_offset = idx;
  55588. idx += 2;
  55589. /* fragment_offset */
  55590. c32to24(5000, b + idx);
  55591. idx += 3;
  55592. /* fragment_length */
  55593. c32to24(100, b + idx);
  55594. idx += 3;
  55595. /* fragment contents */
  55596. idx += 100;
  55597. for (; ret > 0 && msg_number < sendCountMax; msg_number++) {
  55598. byte sendBuf[150];
  55599. int sendSz = sizeof(sendBuf);
  55600. struct timespec delay;
  55601. XMEMSET(&delay, 0, sizeof(delay));
  55602. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  55603. c16toa(msg_number, b + msg_offset);
  55604. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  55605. (int)idx, handshake, 0, 0, 0);
  55606. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  55607. nanosleep(&delay, NULL);
  55608. }
  55609. }
  55610. #endif
  55611. static int test_wolfSSL_dtls_fragments(void)
  55612. {
  55613. EXPECT_DECLS;
  55614. callback_functions func_cb_client;
  55615. callback_functions func_cb_server;
  55616. size_t i;
  55617. struct test_params {
  55618. method_provider client_meth;
  55619. method_provider server_meth;
  55620. ssl_callback spammer;
  55621. } params[] = {
  55622. #if !defined(WOLFSSL_NO_TLS12)
  55623. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  55624. test_wolfSSL_dtls12_fragments_spammer},
  55625. #endif
  55626. #ifdef WOLFSSL_DTLS13
  55627. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  55628. test_wolfSSL_dtls13_fragments_spammer},
  55629. #endif
  55630. };
  55631. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  55632. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  55633. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  55634. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  55635. func_cb_server.method = params[i].server_meth;
  55636. func_cb_client.method = params[i].client_meth;
  55637. func_cb_client.ssl_ready = params[i].spammer;
  55638. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  55639. ExpectFalse(func_cb_client.return_code);
  55640. ExpectFalse(func_cb_server.return_code);
  55641. /* The socket should be closed by the server resulting in a
  55642. * socket error, fatal error or reading a close notify alert */
  55643. if (func_cb_client.last_err != SOCKET_ERROR_E &&
  55644. func_cb_client.last_err != WOLFSSL_ERROR_ZERO_RETURN &&
  55645. func_cb_client.last_err != FATAL_ERROR) {
  55646. ExpectIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  55647. }
  55648. /* Check the server returned an error indicating the msg buffer
  55649. * was full */
  55650. ExpectIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  55651. if (EXPECT_FAIL())
  55652. break;
  55653. }
  55654. return EXPECT_RESULT();
  55655. }
  55656. static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
  55657. {
  55658. int fd, ret;
  55659. byte alert_msg[] = {
  55660. 0x15, /* alert type */
  55661. 0xfe, 0xfd, /* version */
  55662. 0x00, 0x00, /* epoch */
  55663. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
  55664. 0x00, 0x02, /* length */
  55665. 0x02, /* level: fatal */
  55666. 0x46 /* protocol version */
  55667. };
  55668. fd = wolfSSL_get_fd(ssl);
  55669. ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
  55670. AssertIntGT(ret, 0);
  55671. }
  55672. static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
  55673. {
  55674. callback_functions client_cbs, server_cbs;
  55675. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  55676. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  55677. client_cbs.doUdp = server_cbs.doUdp = 1;
  55678. if (version12) {
  55679. #if !defined(WOLFSSL_NO_TLS12)
  55680. client_cbs.method = wolfDTLSv1_2_client_method;
  55681. server_cbs.method = wolfDTLSv1_2_server_method;
  55682. #else
  55683. return TEST_SKIPPED;
  55684. #endif
  55685. }
  55686. else
  55687. {
  55688. #ifdef WOLFSSL_DTLS13
  55689. client_cbs.method = wolfDTLSv1_3_client_method;
  55690. server_cbs.method = wolfDTLSv1_3_server_method;
  55691. #else
  55692. return TEST_SKIPPED;
  55693. #endif /* WOLFSSL_DTLS13 */
  55694. }
  55695. client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
  55696. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  55697. if (!client_cbs.return_code)
  55698. return TEST_FAIL;
  55699. if (!server_cbs.return_code)
  55700. return TEST_FAIL;
  55701. return TEST_SUCCESS;
  55702. }
  55703. static int test_wolfSSL_ignore_alert_before_cookie(void)
  55704. {
  55705. int ret;
  55706. ret =_test_wolfSSL_ignore_alert_before_cookie(0);
  55707. if (ret != 0)
  55708. return ret;
  55709. ret =_test_wolfSSL_ignore_alert_before_cookie(1);
  55710. if (ret != 0)
  55711. return ret;
  55712. return 0;
  55713. }
  55714. static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
  55715. {
  55716. int ret;
  55717. int fd;
  55718. byte bad_msg[] = {
  55719. 0x17, /* app data */
  55720. 0xaa, 0xfd, /* bad version */
  55721. 0x00, 0x01, /* epoch 1 */
  55722. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
  55723. 0x00, 0x26, /* length: 38 bytes */
  55724. 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
  55725. 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
  55726. 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
  55727. 0x03, 0x18, 0x72
  55728. };
  55729. fd = wolfSSL_get_fd(ssl);
  55730. AssertIntGE(fd, 0);
  55731. ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
  55732. AssertIntEQ(ret, sizeof(bad_msg));
  55733. ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
  55734. AssertIntEQ(ret, sizeof("badrecordtest"));
  55735. }
  55736. static void test_wolfSSL_read_string(WOLFSSL* ssl)
  55737. {
  55738. byte buf[100];
  55739. int ret;
  55740. ret = wolfSSL_read(ssl, buf, sizeof(buf));
  55741. AssertIntGT(ret, 0);
  55742. AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
  55743. }
  55744. static int _test_wolfSSL_dtls_bad_record(
  55745. method_provider client_method, method_provider server_method)
  55746. {
  55747. callback_functions client_cbs, server_cbs;
  55748. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  55749. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  55750. client_cbs.doUdp = server_cbs.doUdp = 1;
  55751. client_cbs.method = client_method;
  55752. server_cbs.method = server_method;
  55753. client_cbs.on_result = test_wolfSSL_send_bad_record;
  55754. server_cbs.on_result = test_wolfSSL_read_string;
  55755. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  55756. if (!client_cbs.return_code)
  55757. return TEST_FAIL;
  55758. if (!server_cbs.return_code)
  55759. return TEST_FAIL;
  55760. return TEST_SUCCESS;
  55761. }
  55762. static int test_wolfSSL_dtls_bad_record(void)
  55763. {
  55764. int ret = TEST_SUCCESS;
  55765. #if !defined(WOLFSSL_NO_TLS12)
  55766. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
  55767. wolfDTLSv1_2_server_method);
  55768. #endif
  55769. #ifdef WOLFSSL_DTLS13
  55770. if (ret == TEST_SUCCESS) {
  55771. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
  55772. wolfDTLSv1_3_server_method);
  55773. }
  55774. #endif /* WOLFSSL_DTLS13 */
  55775. return ret;
  55776. }
  55777. #else
  55778. static int test_wolfSSL_dtls_fragments(void) {
  55779. return TEST_SKIPPED;
  55780. }
  55781. static int test_wolfSSL_ignore_alert_before_cookie(void) {
  55782. return TEST_SKIPPED;
  55783. }
  55784. static int test_wolfSSL_dtls_bad_record(void) {
  55785. return TEST_SKIPPED;
  55786. }
  55787. #endif
  55788. #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS) && \
  55789. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  55790. defined(HAVE_IO_TESTS_DEPENDENCIES)
  55791. static byte test_AEAD_fail_decryption = 0;
  55792. static byte test_AEAD_seq_num = 0;
  55793. static byte test_AEAD_done = 0;
  55794. static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  55795. {
  55796. int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
  55797. if (ret > 0) {
  55798. if (test_AEAD_fail_decryption) {
  55799. /* Modify the packet to trigger a decryption failure */
  55800. buf[ret/2] ^= 0xFF;
  55801. if (test_AEAD_fail_decryption == 1)
  55802. test_AEAD_fail_decryption = 0;
  55803. }
  55804. }
  55805. (void)ctx;
  55806. return ret;
  55807. }
  55808. static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
  55809. w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
  55810. {
  55811. if (sendLimit)
  55812. w64Zero(sendLimit);
  55813. switch (ssl->specs.bulk_cipher_algorithm) {
  55814. case wolfssl_aes_gcm:
  55815. if (sendLimit)
  55816. *sendLimit = AEAD_AES_LIMIT;
  55817. FALL_THROUGH;
  55818. case wolfssl_chacha:
  55819. if (hardLimit)
  55820. *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
  55821. if (keyUpdateLimit)
  55822. *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
  55823. break;
  55824. case wolfssl_aes_ccm:
  55825. if (sendLimit)
  55826. *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
  55827. if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
  55828. if (hardLimit)
  55829. *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
  55830. if (keyUpdateLimit)
  55831. *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
  55832. }
  55833. else {
  55834. if (hardLimit)
  55835. *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
  55836. if (keyUpdateLimit)
  55837. *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
  55838. }
  55839. break;
  55840. default:
  55841. fprintf(stderr, "Unrecognized bulk cipher");
  55842. AssertFalse(1);
  55843. break;
  55844. }
  55845. }
  55846. static void test_AEAD_limit_client(WOLFSSL* ssl)
  55847. {
  55848. int ret;
  55849. int i;
  55850. int didReKey = 0;
  55851. char msgBuf[20];
  55852. w64wrapper hardLimit;
  55853. w64wrapper keyUpdateLimit;
  55854. w64wrapper counter;
  55855. w64wrapper sendLimit;
  55856. test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
  55857. w64Zero(&counter);
  55858. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
  55859. wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
  55860. for (i = 0; i < 10; i++) {
  55861. /* Test some failed decryptions */
  55862. test_AEAD_fail_decryption = 1;
  55863. w64Increment(&counter);
  55864. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55865. /* Should succeed since decryption failures are dropped */
  55866. AssertIntGT(ret, 0);
  55867. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
  55868. }
  55869. test_AEAD_fail_decryption = 1;
  55870. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
  55871. w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  55872. /* 100 read calls should be enough to complete the key update */
  55873. w64Zero(&counter);
  55874. for (i = 0; i < 100; i++) {
  55875. /* Key update should be sent and negotiated */
  55876. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55877. AssertIntGT(ret, 0);
  55878. /* Epoch after one key update is 4 */
  55879. if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
  55880. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
  55881. didReKey = 1;
  55882. break;
  55883. }
  55884. }
  55885. AssertTrue(didReKey);
  55886. if (!w64IsZero(sendLimit)) {
  55887. /* Test the sending limit for AEAD ciphers */
  55888. Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
  55889. test_AEAD_seq_num = 1;
  55890. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  55891. AssertIntGT(ret, 0);
  55892. didReKey = 0;
  55893. w64Zero(&counter);
  55894. /* 100 read calls should be enough to complete the key update */
  55895. for (i = 0; i < 100; i++) {
  55896. /* Key update should be sent and negotiated */
  55897. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55898. AssertIntGT(ret, 0);
  55899. /* Epoch after another key update is 5 */
  55900. if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
  55901. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
  55902. didReKey = 1;
  55903. break;
  55904. }
  55905. }
  55906. AssertTrue(didReKey);
  55907. }
  55908. test_AEAD_fail_decryption = 2;
  55909. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
  55910. w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  55911. /* Connection should fail with a DECRYPT_ERROR */
  55912. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55913. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  55914. AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
  55915. test_AEAD_done = 1;
  55916. }
  55917. int counter = 0;
  55918. static void test_AEAD_limit_server(WOLFSSL* ssl)
  55919. {
  55920. char msgBuf[] = "Sending data";
  55921. int ret = WOLFSSL_SUCCESS;
  55922. w64wrapper sendLimit;
  55923. SOCKET_T fd = wolfSSL_get_fd(ssl);
  55924. struct timespec delay;
  55925. XMEMSET(&delay, 0, sizeof(delay));
  55926. delay.tv_nsec = 100000000; /* wait 0.1 seconds */
  55927. tcp_set_nonblocking(&fd); /* So that read doesn't block */
  55928. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  55929. test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
  55930. while (!test_AEAD_done && ret > 0) {
  55931. counter++;
  55932. if (test_AEAD_seq_num) {
  55933. /* We need to update the seq number so that we can understand the
  55934. * peer. Otherwise we will incorrectly interpret the seq number. */
  55935. Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
  55936. AssertNotNull(e);
  55937. e->nextPeerSeqNumber = sendLimit;
  55938. test_AEAD_seq_num = 0;
  55939. }
  55940. (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  55941. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  55942. nanosleep(&delay, NULL);
  55943. }
  55944. }
  55945. static int test_wolfSSL_dtls_AEAD_limit(void)
  55946. {
  55947. callback_functions func_cb_client;
  55948. callback_functions func_cb_server;
  55949. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  55950. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  55951. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  55952. func_cb_server.method = wolfDTLSv1_3_server_method;
  55953. func_cb_client.method = wolfDTLSv1_3_client_method;
  55954. func_cb_server.on_result = test_AEAD_limit_server;
  55955. func_cb_client.on_result = test_AEAD_limit_client;
  55956. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  55957. if (!func_cb_client.return_code)
  55958. return TEST_FAIL;
  55959. if (!func_cb_server.return_code)
  55960. return TEST_FAIL;
  55961. return TEST_SUCCESS;
  55962. }
  55963. #else
  55964. static int test_wolfSSL_dtls_AEAD_limit(void)
  55965. {
  55966. return TEST_SKIPPED;
  55967. }
  55968. #endif
  55969. #if defined(WOLFSSL_DTLS) && \
  55970. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  55971. !defined(DEBUG_VECTOR_REGISTER_ACCESS_FUZZING)
  55972. static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
  55973. {
  55974. int fd, ret;
  55975. byte ch_msg[] = {
  55976. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  55977. 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  55978. 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
  55979. 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
  55980. 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
  55981. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  55982. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  55983. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  55984. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  55985. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  55986. 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  55987. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  55988. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  55989. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  55990. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  55991. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
  55992. 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
  55993. 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
  55994. 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
  55995. 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
  55996. 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
  55997. 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
  55998. 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
  55999. 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
  56000. 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
  56001. 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
  56002. 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
  56003. 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
  56004. 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
  56005. 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
  56006. 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
  56007. 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
  56008. 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
  56009. 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
  56010. 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
  56011. 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
  56012. 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
  56013. 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
  56014. 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
  56015. 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
  56016. 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
  56017. 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
  56018. 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
  56019. 0x31, 0x68, 0x4c
  56020. };
  56021. fd = wolfSSL_get_fd(ssl);
  56022. ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
  56023. AssertIntGT(ret, 0);
  56024. /* consume the HRR otherwise handshake will fail */
  56025. ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
  56026. AssertIntGT(ret, 0);
  56027. }
  56028. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  56029. static void test_wolfSSL_dtls_send_ch_with_invalid_cookie(WOLFSSL* ssl)
  56030. {
  56031. int fd, ret;
  56032. byte ch_msh_invalid_cookie[] = {
  56033. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
  56034. 0x4e, 0x01, 0x00, 0x02, 0x42, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02,
  56035. 0x42, 0xfe, 0xfd, 0x69, 0xca, 0x77, 0x60, 0x6f, 0xfc, 0xd1, 0x5b, 0x60,
  56036. 0x5d, 0xf1, 0xa6, 0x5c, 0x44, 0x71, 0xae, 0xca, 0x62, 0x19, 0x0c, 0xb6,
  56037. 0xf7, 0x2c, 0xa6, 0xd5, 0xd2, 0x99, 0x9d, 0x18, 0xae, 0xac, 0x11, 0x00,
  56038. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  56039. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  56040. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  56041. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  56042. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  56043. 0xe2, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  56044. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  56045. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  56046. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x2c, 0x00, 0x45,
  56047. 0x00, 0x43, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56048. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56049. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56050. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56051. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56052. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x2d, 0x00,
  56053. 0x03, 0x02, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x0a, 0x00, 0x19,
  56054. 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00, 0x00, 0x16, 0x00, 0x00,
  56055. 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17, 0x00, 0x41, 0x04, 0x7c,
  56056. 0x5a, 0xc2, 0x5a, 0xfd, 0xcd, 0x2b, 0x08, 0xb2, 0xeb, 0x8e, 0xc0, 0x02,
  56057. 0x03, 0x9d, 0xb1, 0xc1, 0x0d, 0x7b, 0x7f, 0x46, 0x43, 0xdf, 0xf3, 0xee,
  56058. 0x2b, 0x78, 0x0e, 0x29, 0x8c, 0x42, 0x11, 0x2c, 0xde, 0xd7, 0x41, 0x0f,
  56059. 0x28, 0x94, 0x80, 0x41, 0x70, 0xc4, 0x17, 0xfd, 0x6d, 0xfa, 0xee, 0x9a,
  56060. 0xf2, 0xc4, 0x15, 0x4c, 0x5f, 0x54, 0xb6, 0x78, 0x6e, 0xf9, 0x63, 0x27,
  56061. 0x33, 0xb8, 0x7b, 0x01, 0x00, 0x01, 0x00, 0xd4, 0x46, 0x62, 0x9c, 0xbf,
  56062. 0x8f, 0x1b, 0x65, 0x9b, 0xf0, 0x29, 0x64, 0xd8, 0x50, 0x0e, 0x74, 0xf1,
  56063. 0x58, 0x10, 0xc9, 0xd9, 0x82, 0x5b, 0xd9, 0xbe, 0x14, 0xdf, 0xde, 0x86,
  56064. 0xb4, 0x2e, 0x15, 0xee, 0x4f, 0xf6, 0x74, 0x9e, 0x59, 0x11, 0x36, 0x2d,
  56065. 0xb9, 0x67, 0xaa, 0x5a, 0x09, 0x9b, 0x45, 0xf1, 0x01, 0x4c, 0x4e, 0xf6,
  56066. 0xda, 0x6a, 0xae, 0xa7, 0x73, 0x7b, 0x2e, 0xb6, 0x24, 0x89, 0x99, 0xb7,
  56067. 0x52, 0x16, 0x62, 0x0a, 0xab, 0x58, 0xf8, 0x3f, 0x10, 0x5b, 0x83, 0xfd,
  56068. 0x7b, 0x81, 0x77, 0x81, 0x8d, 0xef, 0x24, 0x56, 0x6d, 0xba, 0x49, 0xd4,
  56069. 0x8b, 0xb5, 0xa0, 0xb1, 0xc9, 0x8c, 0x32, 0x95, 0x1c, 0x5e, 0x0a, 0x4b,
  56070. 0xf6, 0x00, 0x50, 0x0a, 0x87, 0x99, 0x59, 0xcf, 0x6f, 0x9d, 0x02, 0xd0,
  56071. 0x1b, 0xa1, 0x96, 0x45, 0x28, 0x76, 0x40, 0x33, 0x28, 0xc9, 0xa1, 0xfd,
  56072. 0x46, 0xab, 0x2c, 0x9e, 0x5e, 0xc6, 0x74, 0x19, 0x9a, 0xf5, 0x9b, 0x51,
  56073. 0x11, 0x4f, 0xc8, 0xb9, 0x99, 0x6b, 0x4e, 0x3e, 0x31, 0x64, 0xb4, 0x92,
  56074. 0xf4, 0x0d, 0x41, 0x4b, 0x2c, 0x65, 0x23, 0xf7, 0x47, 0xe3, 0xa5, 0x2e,
  56075. 0xe4, 0x9c, 0x2b, 0xc9, 0x41, 0x22, 0x83, 0x8a, 0x23, 0xef, 0x29, 0x7e,
  56076. 0x4f, 0x3f, 0xa3, 0xbf, 0x73, 0x2b, 0xd7, 0xcc, 0xc8, 0xc6, 0xe9, 0xbc,
  56077. 0x01, 0xb7, 0x32, 0x63, 0xd4, 0x7e, 0x7f, 0x9a, 0xaf, 0x5f, 0x05, 0x31,
  56078. 0x53, 0xd6, 0x1f, 0xa2, 0xd0, 0xdf, 0x67, 0x56, 0xf1, 0x9c, 0x4a, 0x9d,
  56079. 0x83, 0xb4, 0xef, 0xb3, 0xf2, 0xcc, 0xf1, 0x91, 0x6c, 0x47, 0xc3, 0x8b,
  56080. 0xd0, 0x92, 0x79, 0x3d, 0xa0, 0xc0, 0x3a, 0x57, 0x26, 0x6d, 0x0a, 0xad,
  56081. 0x5f, 0xad, 0xb4, 0x74, 0x48, 0x4a, 0x51, 0xe1, 0xb5, 0x82, 0x0a, 0x4c,
  56082. 0x4f, 0x9d, 0xaf, 0xee, 0x5a, 0xa2, 0x4d, 0x4d, 0x5f, 0xe0, 0x17, 0x00,
  56083. 0x23, 0x00, 0x00
  56084. };
  56085. byte alert_reply[50];
  56086. byte expected_alert_reply[] = {
  56087. 0x15, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  56088. 0x02, 0x02, 0x2f
  56089. };
  56090. fd = wolfSSL_get_fd(ssl);
  56091. ret = (int)send(fd, ch_msh_invalid_cookie, sizeof(ch_msh_invalid_cookie), 0);
  56092. AssertIntGT(ret, 0);
  56093. /* should reply with an illegal_parameter reply */
  56094. ret = (int)recv(fd, alert_reply, sizeof(alert_reply), 0);
  56095. AssertIntEQ(ret, sizeof(expected_alert_reply));
  56096. AssertIntEQ(XMEMCMP(alert_reply, expected_alert_reply, sizeof(expected_alert_reply)), 0);
  56097. }
  56098. #endif
  56099. static word32 test_wolfSSL_dtls_stateless_HashWOLFSSL(const WOLFSSL* ssl)
  56100. {
  56101. #ifndef NO_MD5
  56102. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  56103. #elif !defined(NO_SHA)
  56104. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  56105. #elif !defined(NO_SHA256)
  56106. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  56107. #else
  56108. #error "We need a digest to hash the WOLFSSL object"
  56109. #endif
  56110. byte hashBuf[WC_MAX_DIGEST_SIZE];
  56111. wc_HashAlg hash;
  56112. const TLSX* exts = ssl->extensions;
  56113. WOLFSSL sslCopy; /* Use a copy to omit certain fields */
  56114. HS_Hashes* hsHashes = ssl->hsHashes; /* Is re-allocated in
  56115. * InitHandshakeHashes */
  56116. XMEMCPY(&sslCopy, ssl, sizeof(*ssl));
  56117. XMEMSET(hashBuf, 0, sizeof(hashBuf));
  56118. /* Following fields are not important to compare */
  56119. XMEMSET(sslCopy.buffers.inputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  56120. sslCopy.buffers.inputBuffer.buffer = NULL;
  56121. sslCopy.buffers.inputBuffer.bufferSize = 0;
  56122. sslCopy.buffers.inputBuffer.dynamicFlag = 0;
  56123. sslCopy.buffers.inputBuffer.offset = 0;
  56124. XMEMSET(sslCopy.buffers.outputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  56125. sslCopy.buffers.outputBuffer.buffer = NULL;
  56126. sslCopy.buffers.outputBuffer.bufferSize = 0;
  56127. sslCopy.buffers.outputBuffer.dynamicFlag = 0;
  56128. sslCopy.buffers.outputBuffer.offset = 0;
  56129. sslCopy.error = 0;
  56130. sslCopy.curSize = 0;
  56131. sslCopy.curStartIdx = 0;
  56132. sslCopy.keys.curSeq_lo = 0;
  56133. XMEMSET(&sslCopy.curRL, 0, sizeof(sslCopy.curRL));
  56134. #ifdef WOLFSSL_DTLS13
  56135. XMEMSET(&sslCopy.keys.curSeq, 0, sizeof(sslCopy.keys.curSeq));
  56136. sslCopy.dtls13FastTimeout = 0;
  56137. #endif
  56138. sslCopy.keys.dtls_peer_handshake_number = 0;
  56139. XMEMSET(&sslCopy.alert_history, 0, sizeof(sslCopy.alert_history));
  56140. sslCopy.hsHashes = NULL;
  56141. #ifdef WOLFSSL_ASYNC_IO
  56142. #ifdef WOLFSSL_ASYNC_CRYPT
  56143. sslCopy.asyncDev = NULL;
  56144. #endif
  56145. sslCopy.async = NULL;
  56146. #endif
  56147. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  56148. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)&sslCopy, sizeof(sslCopy)), 0);
  56149. /* hash extension list */
  56150. while (exts != NULL) {
  56151. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)exts, sizeof(*exts)), 0);
  56152. exts = exts->next;
  56153. }
  56154. /* Hash suites */
  56155. if (sslCopy.suites != NULL) {
  56156. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)sslCopy.suites,
  56157. sizeof(struct Suites)), 0);
  56158. }
  56159. /* Hash hsHashes */
  56160. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)hsHashes,
  56161. sizeof(*hsHashes)), 0);
  56162. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  56163. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  56164. return MakeWordFromHash(hashBuf);
  56165. }
  56166. static CallbackIORecv test_wolfSSL_dtls_compare_stateless_cb;
  56167. static int test_wolfSSL_dtls_compare_stateless_cb_call_once;
  56168. static int test_wolfSSL_dtls_compare_stateless_read_cb_once(WOLFSSL *ssl,
  56169. char *buf, int sz, void *ctx)
  56170. {
  56171. if (test_wolfSSL_dtls_compare_stateless_cb_call_once) {
  56172. test_wolfSSL_dtls_compare_stateless_cb_call_once = 0;
  56173. return test_wolfSSL_dtls_compare_stateless_cb(ssl, buf, sz, ctx);
  56174. }
  56175. else {
  56176. return WOLFSSL_CBIO_ERR_WANT_READ;
  56177. }
  56178. }
  56179. static void test_wolfSSL_dtls_compare_stateless(WOLFSSL* ssl)
  56180. {
  56181. /* Compare the ssl object before and after one ClientHello msg */
  56182. SOCKET_T fd = wolfSSL_get_fd(ssl);
  56183. int res;
  56184. int err;
  56185. word32 initHash;
  56186. test_wolfSSL_dtls_compare_stateless_cb = ssl->CBIORecv;
  56187. test_wolfSSL_dtls_compare_stateless_cb_call_once = 1;
  56188. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  56189. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_read_cb_once;
  56190. initHash = test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl);
  56191. (void)initHash;
  56192. res = tcp_select(fd, 5);
  56193. /* We are expecting a msg. A timeout indicates failure. */
  56194. AssertIntEQ(res, TEST_RECV_READY);
  56195. res = wolfSSL_accept(ssl);
  56196. err = wolfSSL_get_error(ssl, res);
  56197. AssertIntEQ(res, WOLFSSL_FATAL_ERROR);
  56198. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  56199. AssertIntEQ(initHash, test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl));
  56200. wolfSSL_dtls_set_using_nonblock(ssl, 0);
  56201. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_cb;
  56202. }
  56203. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  56204. static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
  56205. {
  56206. int ret;
  56207. ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
  56208. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  56209. test_wolfSSL_dtls_compare_stateless(ssl);
  56210. }
  56211. #endif
  56212. static int test_wolfSSL_dtls_stateless(void)
  56213. {
  56214. callback_functions client_cbs, server_cbs;
  56215. size_t i;
  56216. struct {
  56217. method_provider client_meth;
  56218. method_provider server_meth;
  56219. ssl_callback client_ssl_ready;
  56220. ssl_callback server_ssl_ready;
  56221. } test_params[] = {
  56222. #if !defined(WOLFSSL_NO_TLS12)
  56223. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  56224. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_compare_stateless},
  56225. #endif
  56226. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  56227. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  56228. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
  56229. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  56230. test_wolfSSL_dtls_send_ch_with_invalid_cookie, test_wolfSSL_dtls_enable_hrrcookie},
  56231. #endif
  56232. };
  56233. if (0 == sizeof(test_params)){
  56234. return TEST_SKIPPED;
  56235. }
  56236. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  56237. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56238. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56239. client_cbs.doUdp = server_cbs.doUdp = 1;
  56240. client_cbs.method = test_params[i].client_meth;
  56241. server_cbs.method = test_params[i].server_meth;
  56242. client_cbs.ssl_ready = test_params[i].client_ssl_ready;
  56243. server_cbs.ssl_ready = test_params[i].server_ssl_ready;
  56244. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56245. if (!client_cbs.return_code)
  56246. return TEST_FAIL;
  56247. if (!server_cbs.return_code)
  56248. return TEST_FAIL;
  56249. }
  56250. return TEST_SUCCESS;
  56251. }
  56252. #else
  56253. static int test_wolfSSL_dtls_stateless(void)
  56254. {
  56255. return TEST_SKIPPED;
  56256. }
  56257. #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
  56258. * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
  56259. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  56260. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  56261. !defined(WOLFSSL_NO_CLIENT_AUTH))
  56262. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  56263. {
  56264. int ret;
  56265. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  56266. fprintf(stderr, "loading cert %s failed\n", certA);
  56267. fprintf(stderr, "Error: (%d): %s\n", ret,
  56268. wolfSSL_ERR_reason_error_string(ret));
  56269. return -1;
  56270. }
  56271. return 0;
  56272. }
  56273. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  56274. {
  56275. int ret;
  56276. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  56277. != WOLFSSL_SUCCESS) {
  56278. fprintf(stderr, "could not verify the cert: %s\n", certA);
  56279. fprintf(stderr, "Error: (%d): %s\n", ret,
  56280. wolfSSL_ERR_reason_error_string(ret));
  56281. return -1;
  56282. }
  56283. else {
  56284. fprintf(stderr, "successfully verified: %s\n", certA);
  56285. }
  56286. return 0;
  56287. }
  56288. #define LOAD_ONE_CA(a, b, c, d) \
  56289. do { \
  56290. (a) = load_ca_into_cm(c, d); \
  56291. if ((a) != 0) \
  56292. return (b); \
  56293. else \
  56294. (b)--; \
  56295. } while(0)
  56296. #define VERIFY_ONE_CERT(a, b, c, d) \
  56297. do { \
  56298. (a) = verify_cert_with_cm(c, d);\
  56299. if ((a) != 0) \
  56300. return (b); \
  56301. else \
  56302. (b)--; \
  56303. } while(0)
  56304. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  56305. {
  56306. int ret;
  56307. int i = -1;
  56308. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  56309. char chainGArr[9][50] = {"certs/ca-cert.pem",
  56310. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  56311. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  56312. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  56313. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  56314. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  56315. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  56316. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  56317. "certs/test-pathlen/chainG-entity.pem"};
  56318. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  56319. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  56320. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  56321. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  56322. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  56323. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  56324. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  56325. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  56326. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  56327. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  56328. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  56329. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  56330. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  56331. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  56332. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  56333. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  56334. /* test validating the entity twice, should have no effect on pathLen since
  56335. * entity/leaf cert */
  56336. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  56337. return ret;
  56338. }
  56339. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  56340. {
  56341. int ret;
  56342. int i = -1;
  56343. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  56344. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  56345. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  56346. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  56347. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  56348. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  56349. */
  56350. char chainHArr[6][50] = {"certs/ca-cert.pem",
  56351. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  56352. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  56353. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  56354. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  56355. "certs/test-pathlen/chainH-entity.pem"};
  56356. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  56357. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  56358. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  56359. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  56360. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  56361. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  56362. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  56363. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  56364. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  56365. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  56366. return ret;
  56367. }
  56368. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  56369. {
  56370. int ret;
  56371. int i = -1;
  56372. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  56373. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  56374. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  56375. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  56376. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  56377. */
  56378. char chainIArr[5][50] = {"certs/ca-cert.pem",
  56379. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  56380. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  56381. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  56382. "certs/test-pathlen/chainI-entity.pem"};
  56383. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  56384. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  56385. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  56386. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  56387. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  56388. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  56389. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  56390. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  56391. return ret;
  56392. }
  56393. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  56394. {
  56395. int ret;
  56396. int i = -1;
  56397. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  56398. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  56399. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  56400. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  56401. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  56402. */
  56403. char chainJArr[6][50] = {"certs/ca-cert.pem",
  56404. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  56405. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  56406. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  56407. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  56408. "certs/test-pathlen/chainJ-entity.pem"};
  56409. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  56410. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  56411. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  56412. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  56413. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  56414. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  56415. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  56416. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  56417. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  56418. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  56419. return ret;
  56420. }
  56421. static int test_various_pathlen_chains(void)
  56422. {
  56423. EXPECT_DECLS;
  56424. WOLFSSL_CERT_MANAGER* cm = NULL;
  56425. /* Test chain G (large chain with varying pathLens) */
  56426. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56427. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  56428. ExpectIntEQ(test_chainG(cm), -1);
  56429. #else
  56430. ExpectIntEQ(test_chainG(cm), 0);
  56431. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  56432. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56433. wolfSSL_CertManagerFree(cm);
  56434. /* end test chain G */
  56435. /* Test chain H (5 chain with same pathLens) */
  56436. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56437. ExpectIntLT(test_chainH(cm), 0);
  56438. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56439. wolfSSL_CertManagerFree(cm);
  56440. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56441. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56442. wolfSSL_CertManagerFree(cm);
  56443. /* end test chain H */
  56444. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  56445. * followed by 2 ICA's, should pass) */
  56446. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56447. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  56448. ExpectIntEQ(test_chainI(cm), -1);
  56449. #else
  56450. ExpectIntEQ(test_chainI(cm), 0);
  56451. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  56452. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56453. wolfSSL_CertManagerFree(cm);
  56454. cm = NULL;
  56455. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56456. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56457. wolfSSL_CertManagerFree(cm);
  56458. cm = NULL;
  56459. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  56460. * this time followed by 3 ICA's, should fail */
  56461. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56462. ExpectIntLT(test_chainJ(cm), 0);
  56463. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56464. wolfSSL_CertManagerFree(cm);
  56465. cm = NULL;
  56466. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56467. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56468. wolfSSL_CertManagerFree(cm);
  56469. return EXPECT_RESULT();
  56470. }
  56471. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  56472. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  56473. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  56474. {
  56475. EXPECT_DECLS;
  56476. byte ekm[100] = {0};
  56477. (void)ctx;
  56478. /* Success Cases */
  56479. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56480. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  56481. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56482. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  56483. /* Use some random context */
  56484. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56485. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  56486. /* Failure cases */
  56487. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56488. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  56489. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56490. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  56491. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56492. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  56493. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56494. "extended master secret", XSTR_SIZEOF("extended master secret"),
  56495. NULL, 0, 0), 0);
  56496. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56497. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  56498. return EXPECT_RESULT();
  56499. }
  56500. static int test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  56501. {
  56502. wolfSSL_KeepArrays(ssl);
  56503. return TEST_SUCCESS;
  56504. }
  56505. static int test_export_keying_material(void)
  56506. {
  56507. EXPECT_DECLS;
  56508. test_ssl_cbf serverCb;
  56509. test_ssl_cbf clientCb;
  56510. XMEMSET(&serverCb, 0, sizeof(serverCb));
  56511. XMEMSET(&clientCb, 0, sizeof(clientCb));
  56512. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  56513. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&clientCb,
  56514. &serverCb, test_export_keying_material_cb), TEST_SUCCESS);
  56515. return EXPECT_RESULT();
  56516. }
  56517. #endif /* HAVE_KEYING_MATERIAL */
  56518. static int test_wolfSSL_THREADID_hash(void)
  56519. {
  56520. EXPECT_DECLS;
  56521. #if defined(OPENSSL_EXTRA)
  56522. CRYPTO_THREADID id;
  56523. CRYPTO_THREADID_current(NULL);
  56524. /* Hash result is unsigned long. */
  56525. ExpectTrue(CRYPTO_THREADID_hash(NULL) == 0UL);
  56526. XMEMSET(&id, 0, sizeof(id));
  56527. ExpectTrue(CRYPTO_THREADID_hash(&id) == 0UL);
  56528. #endif /* OPENSSL_EXTRA */
  56529. return EXPECT_RESULT();
  56530. }
  56531. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  56532. {
  56533. EXPECT_DECLS;
  56534. #if defined(OPENSSL_EXTRA)
  56535. WOLFSSL_CTX* ctx = NULL;
  56536. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,0), 1);
  56537. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,1), 1);
  56538. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,0), 1);
  56539. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,1), 1);
  56540. #endif /* OPENSSL_EXTRA */
  56541. return EXPECT_RESULT();
  56542. }
  56543. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  56544. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  56545. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  56546. {
  56547. EXPECT_DECLS;
  56548. callback_functions* callbacks = NULL;
  56549. WOLFSSL_CTX* ctx = NULL;
  56550. WOLFSSL* ssl = NULL;
  56551. SOCKET_T sfd = 0;
  56552. SOCKET_T cfd = 0;
  56553. word16 port;
  56554. char msg[] = "I hear you fa shizzle!";
  56555. int len = (int) XSTRLEN(msg);
  56556. char input[1024];
  56557. int ret = 0;
  56558. int err = 0;
  56559. if (!args)
  56560. WOLFSSL_RETURN_FROM_THREAD(0);
  56561. ((func_args*)args)->return_code = TEST_FAIL;
  56562. callbacks = ((func_args*)args)->callbacks;
  56563. ctx = wolfSSL_CTX_new(callbacks->method());
  56564. #if defined(USE_WINDOWS_API)
  56565. port = ((func_args*)args)->signal->port;
  56566. #else
  56567. /* Let tcp_listen assign port */
  56568. port = 0;
  56569. #endif
  56570. #ifdef WOLFSSL_TIRTOS
  56571. fdOpenSession(Task_self());
  56572. #endif
  56573. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  56574. caCertFile, 0));
  56575. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  56576. svrCertFile, WOLFSSL_FILETYPE_PEM));
  56577. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  56578. svrKeyFile, WOLFSSL_FILETYPE_PEM));
  56579. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  56580. ExpectIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  56581. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  56582. #elif !defined(NO_DH)
  56583. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  56584. #endif
  56585. if (callbacks->ctx_ready)
  56586. callbacks->ctx_ready(ctx);
  56587. ssl = wolfSSL_new(ctx);
  56588. ExpectNotNull(ssl);
  56589. /* listen and accept */
  56590. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  56591. CloseSocket(sfd);
  56592. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  56593. if (callbacks->ssl_ready)
  56594. callbacks->ssl_ready(ssl);
  56595. if (EXPECT_SUCCESS()) {
  56596. do {
  56597. err = 0; /* Reset error */
  56598. ret = wolfSSL_accept(ssl);
  56599. if (ret != WOLFSSL_SUCCESS) {
  56600. err = wolfSSL_get_error(ssl, 0);
  56601. }
  56602. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  56603. }
  56604. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  56605. /* read and write data */
  56606. XMEMSET(input, 0, sizeof(input));
  56607. while (EXPECT_SUCCESS()) {
  56608. ret = wolfSSL_read(ssl, input, sizeof(input));
  56609. if (ret > 0) {
  56610. break;
  56611. }
  56612. else {
  56613. err = wolfSSL_get_error(ssl,ret);
  56614. if (err == WOLFSSL_ERROR_WANT_READ) {
  56615. continue;
  56616. }
  56617. break;
  56618. }
  56619. }
  56620. if (EXPECT_SUCCESS() && (err == WOLFSSL_ERROR_ZERO_RETURN)) {
  56621. do {
  56622. ret = wolfSSL_write(ssl, msg, len);
  56623. if (ret > 0) {
  56624. break;
  56625. }
  56626. } while (ret < 0);
  56627. }
  56628. /* bidirectional shutdown */
  56629. while (EXPECT_SUCCESS()) {
  56630. ret = wolfSSL_shutdown(ssl);
  56631. ExpectIntNE(ret, WOLFSSL_FATAL_ERROR);
  56632. if (ret == WOLFSSL_SUCCESS) {
  56633. break;
  56634. }
  56635. }
  56636. if (EXPECT_SUCCESS()) {
  56637. /* wait for the peer to disconnect the tcp connection */
  56638. do {
  56639. ret = wolfSSL_read(ssl, input, sizeof(input));
  56640. err = wolfSSL_get_error(ssl, ret);
  56641. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  56642. }
  56643. /* detect TCP disconnect */
  56644. ExpectIntLE(ret,WOLFSSL_FAILURE);
  56645. ExpectIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  56646. ((func_args*)args)->return_code = EXPECT_RESULT();
  56647. wolfSSL_free(ssl);
  56648. wolfSSL_CTX_free(ctx);
  56649. CloseSocket(cfd);
  56650. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  56651. wc_ecc_fp_free(); /* free per thread cache */
  56652. #endif
  56653. WOLFSSL_RETURN_FROM_THREAD(0);
  56654. }
  56655. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  56656. {
  56657. EXPECT_DECLS;
  56658. callback_functions* callbacks = NULL;
  56659. WOLFSSL_CTX* ctx = NULL;
  56660. WOLFSSL* ssl = NULL;
  56661. SOCKET_T sfd = 0;
  56662. char msg[] = "hello wolfssl server!";
  56663. int len = (int) XSTRLEN(msg);
  56664. char input[1024];
  56665. int idx;
  56666. int ret, err;
  56667. if (!args)
  56668. WOLFSSL_RETURN_FROM_THREAD(0);
  56669. ((func_args*)args)->return_code = TEST_FAIL;
  56670. callbacks = ((func_args*)args)->callbacks;
  56671. ctx = wolfSSL_CTX_new(callbacks->method());
  56672. #ifdef WOLFSSL_TIRTOS
  56673. fdOpenSession(Task_self());
  56674. #endif
  56675. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  56676. caCertFile, 0));
  56677. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  56678. cliCertFile, WOLFSSL_FILETYPE_PEM));
  56679. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  56680. cliKeyFile, WOLFSSL_FILETYPE_PEM));
  56681. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  56682. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  56683. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  56684. if (EXPECT_SUCCESS()) {
  56685. do {
  56686. err = 0; /* Reset error */
  56687. ret = wolfSSL_connect(ssl);
  56688. if (ret != WOLFSSL_SUCCESS) {
  56689. err = wolfSSL_get_error(ssl, 0);
  56690. }
  56691. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  56692. }
  56693. ExpectIntGE(wolfSSL_write(ssl, msg, len), 0);
  56694. if (EXPECT_SUCCESS()) {
  56695. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  56696. input[idx] = 0;
  56697. }
  56698. }
  56699. if (EXPECT_SUCCESS()) {
  56700. ret = wolfSSL_shutdown(ssl);
  56701. if (ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  56702. ret = wolfSSL_shutdown(ssl);
  56703. }
  56704. }
  56705. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  56706. ((func_args*)args)->return_code = EXPECT_RESULT();
  56707. wolfSSL_free(ssl);
  56708. wolfSSL_CTX_free(ctx);
  56709. CloseSocket(sfd);
  56710. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  56711. wc_ecc_fp_free(); /* free per thread cache */
  56712. #endif
  56713. WOLFSSL_RETURN_FROM_THREAD(0);
  56714. }
  56715. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  56716. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  56717. /* This test is to check wolfSSL_read behaves as same as
  56718. * openSSL when it is called after SSL_shutdown completes.
  56719. */
  56720. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  56721. {
  56722. EXPECT_DECLS;
  56723. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  56724. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  56725. tcp_ready ready;
  56726. func_args client_args;
  56727. func_args server_args;
  56728. THREAD_TYPE serverThread;
  56729. THREAD_TYPE clientThread;
  56730. callback_functions server_cbf;
  56731. callback_functions client_cbf;
  56732. #ifdef WOLFSSL_TIRTOS
  56733. fdOpenSession(Task_self());
  56734. #endif
  56735. StartTCP();
  56736. InitTcpReady(&ready);
  56737. #if defined(USE_WINDOWS_API)
  56738. /* use RNG to get random port if using windows */
  56739. ready.port = GetRandomPort();
  56740. #endif
  56741. XMEMSET(&client_args, 0, sizeof(func_args));
  56742. XMEMSET(&server_args, 0, sizeof(func_args));
  56743. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  56744. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  56745. server_cbf.method = wolfTLSv1_2_server_method;
  56746. client_cbf.method = wolfTLSv1_2_client_method;
  56747. server_args.callbacks = &server_cbf;
  56748. client_args.callbacks = &client_cbf;
  56749. server_args.signal = &ready;
  56750. client_args.signal = &ready;
  56751. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  56752. wait_tcp_ready(&server_args);
  56753. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  56754. join_thread(clientThread);
  56755. join_thread(serverThread);
  56756. ExpectTrue(client_args.return_code);
  56757. ExpectTrue(server_args.return_code);
  56758. FreeTcpReady(&ready);
  56759. #endif
  56760. return EXPECT_RESULT();
  56761. }
  56762. static int test_wolfSSL_CTX_get_min_proto_version(void)
  56763. {
  56764. EXPECT_DECLS;
  56765. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  56766. WOLFSSL_CTX *ctx = NULL;
  56767. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  56768. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  56769. WOLFSSL_SUCCESS);
  56770. #ifdef WOLFSSL_ALLOW_SSLV3
  56771. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  56772. #else
  56773. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  56774. #endif
  56775. wolfSSL_CTX_free(ctx);
  56776. ctx = NULL;
  56777. #ifndef NO_OLD_TLS
  56778. #ifdef WOLFSSL_ALLOW_TLSV10
  56779. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  56780. #else
  56781. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  56782. #endif
  56783. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION),
  56784. WOLFSSL_SUCCESS);
  56785. #ifdef WOLFSSL_ALLOW_TLSV10
  56786. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  56787. #else
  56788. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  56789. #endif
  56790. wolfSSL_CTX_free(ctx);
  56791. ctx = NULL;
  56792. #endif
  56793. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  56794. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION),
  56795. WOLFSSL_SUCCESS);
  56796. #ifndef NO_OLD_TLS
  56797. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  56798. #else
  56799. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  56800. #endif
  56801. wolfSSL_CTX_free(ctx);
  56802. ctx = NULL;
  56803. #ifndef WOLFSSL_NO_TLS12
  56804. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  56805. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION),
  56806. WOLFSSL_SUCCESS);
  56807. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  56808. wolfSSL_CTX_free(ctx);
  56809. ctx = NULL;
  56810. #endif
  56811. #ifdef WOLFSSL_TLS13
  56812. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  56813. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION),
  56814. WOLFSSL_SUCCESS);
  56815. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  56816. wolfSSL_CTX_free(ctx);
  56817. ctx = NULL;
  56818. #endif
  56819. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  56820. return EXPECT_RESULT();
  56821. }
  56822. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  56823. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  56824. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  56825. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  56826. static int test_wolfSSL_set_SSL_CTX(void)
  56827. {
  56828. EXPECT_DECLS;
  56829. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) \
  56830. && !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13) && \
  56831. !defined(NO_RSA)
  56832. WOLFSSL_CTX *ctx1 = NULL;
  56833. WOLFSSL_CTX *ctx2 = NULL;
  56834. WOLFSSL *ssl = NULL;
  56835. const byte *session_id1 = (const byte *)"CTX1";
  56836. const byte *session_id2 = (const byte *)"CTX2";
  56837. ExpectNotNull(ctx1 = wolfSSL_CTX_new(wolfTLS_server_method()));
  56838. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx1, svrCertFile,
  56839. WOLFSSL_FILETYPE_PEM));
  56840. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx1, svrKeyFile,
  56841. WOLFSSL_FILETYPE_PEM));
  56842. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx1, TLS1_2_VERSION),
  56843. WOLFSSL_SUCCESS);
  56844. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx1), TLS1_2_VERSION);
  56845. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx1), TLS1_3_VERSION);
  56846. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx1, session_id1, 4),
  56847. WOLFSSL_SUCCESS);
  56848. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfTLS_server_method()));
  56849. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx2, svrCertFile,
  56850. WOLFSSL_FILETYPE_PEM));
  56851. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx2, svrKeyFile,
  56852. WOLFSSL_FILETYPE_PEM));
  56853. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx2, TLS1_2_VERSION),
  56854. WOLFSSL_SUCCESS);
  56855. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx2, TLS1_2_VERSION),
  56856. WOLFSSL_SUCCESS);
  56857. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx2), TLS1_2_VERSION);
  56858. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx2), TLS1_2_VERSION);
  56859. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx2, session_id2, 4),
  56860. WOLFSSL_SUCCESS);
  56861. #ifdef HAVE_SESSION_TICKET
  56862. ExpectIntEQ((wolfSSL_CTX_get_options(ctx1) & SSL_OP_NO_TICKET), 0);
  56863. wolfSSL_CTX_set_options(ctx2, SSL_OP_NO_TICKET);
  56864. ExpectIntNE((wolfSSL_CTX_get_options(ctx2) & SSL_OP_NO_TICKET), 0);
  56865. #endif
  56866. ExpectNotNull(ssl = wolfSSL_new(ctx2));
  56867. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  56868. #ifdef WOLFSSL_INT_H
  56869. #ifdef WOLFSSL_SESSION_ID_CTX
  56870. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id2, 4), 0);
  56871. #endif
  56872. ExpectTrue(ssl->buffers.certificate == ctx2->certificate);
  56873. ExpectTrue(ssl->buffers.certChain == ctx2->certChain);
  56874. #endif
  56875. #ifdef HAVE_SESSION_TICKET
  56876. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  56877. #endif
  56878. /* Set the ctx1 that has TLSv1.3 as max proto version */
  56879. ExpectNotNull(wolfSSL_set_SSL_CTX(ssl, ctx1));
  56880. /* MUST not change proto versions of ssl */
  56881. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  56882. #ifdef HAVE_SESSION_TICKET
  56883. /* MUST not change */
  56884. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  56885. #endif
  56886. /* MUST change */
  56887. #ifdef WOLFSSL_INT_H
  56888. ExpectTrue(ssl->buffers.certificate == ctx1->certificate);
  56889. ExpectTrue(ssl->buffers.certChain == ctx1->certChain);
  56890. #ifdef WOLFSSL_SESSION_ID_CTX
  56891. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id1, 4), 0);
  56892. #endif
  56893. #endif
  56894. wolfSSL_free(ssl);
  56895. wolfSSL_CTX_free(ctx1);
  56896. wolfSSL_CTX_free(ctx2);
  56897. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  56898. return EXPECT_RESULT();
  56899. }
  56900. #endif /* defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  56901. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  56902. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  56903. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))) */
  56904. static int test_wolfSSL_security_level(void)
  56905. {
  56906. EXPECT_DECLS;
  56907. #if defined(OPENSSL_EXTRA)
  56908. SSL_CTX *ctx = NULL;
  56909. #ifdef WOLFSSL_TLS13
  56910. #ifdef NO_WOLFSSL_SERVER
  56911. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  56912. #else
  56913. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  56914. #endif
  56915. SSL_CTX_set_security_level(NULL, 1);
  56916. SSL_CTX_set_security_level(ctx, 1);
  56917. ExpectIntEQ(SSL_CTX_get_security_level(NULL), 0);
  56918. /* Stub so nothing happens. */
  56919. ExpectIntEQ(SSL_CTX_get_security_level(ctx), 0);
  56920. SSL_CTX_free(ctx);
  56921. #else
  56922. (void)ctx;
  56923. #endif
  56924. #endif
  56925. return EXPECT_RESULT();
  56926. }
  56927. static int test_wolfSSL_SSL_in_init(void)
  56928. {
  56929. EXPECT_DECLS;
  56930. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  56931. SSL_CTX* ctx = NULL;
  56932. SSL* ssl = NULL;
  56933. const char* testCertFile;
  56934. const char* testKeyFile;
  56935. #ifdef WOLFSSL_TLS13
  56936. #ifdef NO_WOLFSSL_SERVER
  56937. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  56938. #else
  56939. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  56940. #endif
  56941. #else
  56942. #ifdef NO_WOLFSSL_SERVER
  56943. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  56944. #else
  56945. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56946. #endif
  56947. #endif
  56948. #ifndef NO_RSA
  56949. testCertFile = svrCertFile;
  56950. testKeyFile = svrKeyFile;
  56951. #elif defined(HAVE_ECC)
  56952. testCertFile = eccCertFile;
  56953. testKeyFile = eccKeyFile;
  56954. #else
  56955. testCertFile = NULL;
  56956. testKeyFile = NULL;
  56957. #endif
  56958. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  56959. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  56960. SSL_FILETYPE_PEM));
  56961. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  56962. SSL_FILETYPE_PEM));
  56963. }
  56964. ExpectNotNull(ssl = SSL_new(ctx));
  56965. ExpectIntEQ(SSL_in_init(ssl), 1);
  56966. SSL_CTX_free(ctx);
  56967. SSL_free(ssl);
  56968. #endif
  56969. return EXPECT_RESULT();
  56970. }
  56971. static int test_wolfSSL_CTX_set_timeout(void)
  56972. {
  56973. EXPECT_DECLS;
  56974. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  56975. int timeout;
  56976. WOLFSSL_CTX* ctx = NULL;
  56977. (void)timeout;
  56978. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  56979. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  56980. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  56981. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  56982. */
  56983. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  56984. /* giving 0 as timeout value sets default timeout */
  56985. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  56986. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  56987. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  56988. #else
  56989. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  56990. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  56991. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  56992. #endif
  56993. wolfSSL_CTX_free(ctx);
  56994. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  56995. return EXPECT_RESULT();
  56996. }
  56997. static int test_wolfSSL_OpenSSL_version(void)
  56998. {
  56999. EXPECT_DECLS;
  57000. #if defined(OPENSSL_EXTRA)
  57001. const char* ver;
  57002. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  57003. ExpectNotNull(ver = OpenSSL_version(0));
  57004. #else
  57005. ExpectNotNull(ver = OpenSSL_version());
  57006. #endif
  57007. ExpectIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  57008. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  57009. #endif
  57010. return EXPECT_RESULT();
  57011. }
  57012. static int test_CONF_CTX_CMDLINE(void)
  57013. {
  57014. EXPECT_DECLS;
  57015. #if defined(OPENSSL_ALL)
  57016. SSL_CTX* ctx = NULL;
  57017. SSL_CONF_CTX* cctx = NULL;
  57018. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  57019. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  57020. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  57021. /* set flags */
  57022. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  57023. WOLFSSL_CONF_FLAG_CMDLINE);
  57024. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  57025. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  57026. /* cmd invalid command */
  57027. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  57028. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  57029. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  57030. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  57031. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  57032. /* cmd Certificate and Private Key*/
  57033. {
  57034. #if !defined(NO_CERTS) && !defined(NO_RSA)
  57035. const char* ourCert = svrCertFile;
  57036. const char* ourKey = svrKeyFile;
  57037. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  57038. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert), WOLFSSL_SUCCESS);
  57039. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  57040. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  57041. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57042. #endif
  57043. }
  57044. /* cmd curves */
  57045. {
  57046. #if defined(HAVE_ECC)
  57047. const char* curve = "secp256r1";
  57048. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  57049. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  57050. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57051. #endif
  57052. }
  57053. /* cmd CipherString */
  57054. {
  57055. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  57056. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  57057. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  57058. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57059. }
  57060. /* cmd DH parameter */
  57061. {
  57062. #if !defined(NO_DH) && !defined(NO_BIO)
  57063. const char* ourdhcert = "./certs/dh2048.pem";
  57064. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL), -3);
  57065. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert), WOLFSSL_SUCCESS);
  57066. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57067. #endif
  57068. }
  57069. SSL_CTX_free(ctx);
  57070. SSL_CONF_CTX_free(cctx);
  57071. #endif /* OPENSSL_EXTRA */
  57072. return EXPECT_RESULT();
  57073. }
  57074. static int test_CONF_CTX_FILE(void)
  57075. {
  57076. EXPECT_DECLS;
  57077. #if defined(OPENSSL_ALL)
  57078. SSL_CTX* ctx = NULL;
  57079. SSL_CONF_CTX* cctx = NULL;
  57080. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  57081. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  57082. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  57083. /* set flags */
  57084. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  57085. WOLFSSL_CONF_FLAG_FILE);
  57086. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  57087. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  57088. /* sanity check */
  57089. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  57090. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  57091. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  57092. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  57093. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  57094. /* cmd Certificate and Private Key*/
  57095. {
  57096. #if !defined(NO_CERTS) && !defined(NO_RSA)
  57097. const char* ourCert = svrCertFile;
  57098. const char* ourKey = svrKeyFile;
  57099. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  57100. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  57101. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  57102. WOLFSSL_SUCCESS);
  57103. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  57104. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57105. #endif
  57106. }
  57107. /* cmd curves */
  57108. {
  57109. #if defined(HAVE_ECC)
  57110. const char* curve = "secp256r1";
  57111. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  57112. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  57113. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57114. #endif
  57115. }
  57116. /* cmd CipherString */
  57117. {
  57118. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  57119. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  57120. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher),
  57121. WOLFSSL_SUCCESS);
  57122. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57123. }
  57124. /* cmd DH parameter */
  57125. {
  57126. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  57127. const char* ourdhcert = "./certs/dh3072.pem";
  57128. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  57129. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  57130. WOLFSSL_SUCCESS);
  57131. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57132. #endif
  57133. }
  57134. SSL_CTX_free(ctx);
  57135. SSL_CONF_CTX_free(cctx);
  57136. #endif /* OPENSSL_EXTRA */
  57137. return EXPECT_RESULT();
  57138. }
  57139. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  57140. {
  57141. EXPECT_DECLS;
  57142. #ifdef HAVE_EX_DATA
  57143. int idx1, idx2;
  57144. /* test for unsupported class index */
  57145. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  57146. 0,NULL, NULL, NULL, NULL ), -1);
  57147. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(
  57148. WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  57149. 0,NULL, NULL, NULL, NULL ), -1);
  57150. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  57151. 0,NULL, NULL, NULL, NULL ), -1);
  57152. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  57153. 0,NULL, NULL, NULL, NULL ), -1);
  57154. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  57155. 0,NULL, NULL, NULL, NULL ), -1);
  57156. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  57157. 0,NULL, NULL, NULL, NULL ), -1);
  57158. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  57159. 0,NULL, NULL, NULL, NULL ), -1);
  57160. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  57161. 0,NULL, NULL, NULL, NULL ), -1);
  57162. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  57163. 0,NULL, NULL, NULL, NULL ), -1);
  57164. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  57165. 0,NULL, NULL, NULL, NULL ), -1);
  57166. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  57167. 0,NULL, NULL, NULL, NULL ), -1);
  57168. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  57169. 0,NULL, NULL, NULL, NULL ), -1);
  57170. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20,
  57171. 0,NULL, NULL, NULL, NULL ), -1);
  57172. /* test for supported class index */
  57173. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  57174. 0,NULL, NULL, NULL, NULL );
  57175. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  57176. 0,NULL, NULL, NULL, NULL );
  57177. ExpectIntNE(idx1, -1);
  57178. ExpectIntNE(idx2, -1);
  57179. ExpectIntNE(idx1, idx2);
  57180. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  57181. 0,NULL, NULL, NULL, NULL );
  57182. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  57183. 0,NULL, NULL, NULL, NULL );
  57184. ExpectIntNE(idx1, -1);
  57185. ExpectIntNE(idx2, -1);
  57186. ExpectIntNE(idx1, idx2);
  57187. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  57188. 0,NULL, NULL, NULL, NULL );
  57189. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  57190. 0,NULL, NULL, NULL, NULL );
  57191. ExpectIntNE(idx1, -1);
  57192. ExpectIntNE(idx2, -1);
  57193. ExpectIntNE(idx1, idx2);
  57194. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  57195. 0,NULL, NULL, NULL, NULL );
  57196. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  57197. 0,NULL, NULL, NULL, NULL );
  57198. ExpectIntNE(idx1, -1);
  57199. ExpectIntNE(idx2, -1);
  57200. ExpectIntNE(idx1, idx2);
  57201. #endif /* HAVE_EX_DATA */
  57202. return EXPECT_RESULT();
  57203. }
  57204. #if defined(HAVE_EX_DATA) && defined(HAVE_EXT_CACHE) && \
  57205. (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  57206. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  57207. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  57208. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))))
  57209. #define SESSION_NEW_IDX_LONG 0xDEADBEEF
  57210. #define SESSION_NEW_IDX_VAL ((void*)0xAEADAEAD)
  57211. #define SESSION_DUP_IDX_VAL ((void*)0xDEDEDEDE)
  57212. #define SESSION_NEW_IDX_PTR "Testing"
  57213. static void test_wolfSSL_SESSION_get_ex_new_index_new_cb(void* p, void* ptr,
  57214. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  57215. {
  57216. AssertNotNull(p);
  57217. AssertNull(ptr);
  57218. AssertIntEQ(CRYPTO_set_ex_data(a, idx, SESSION_NEW_IDX_VAL), SSL_SUCCESS);
  57219. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  57220. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  57221. }
  57222. static int test_wolfSSL_SESSION_get_ex_new_index_dup_cb(CRYPTO_EX_DATA* out,
  57223. const CRYPTO_EX_DATA* in, void* inPtr, int idx, long argV,
  57224. void* arg)
  57225. {
  57226. EXPECT_DECLS;
  57227. ExpectNotNull(out);
  57228. ExpectNotNull(in);
  57229. ExpectPtrEq(*(void**)inPtr, SESSION_NEW_IDX_VAL);
  57230. ExpectPtrEq(CRYPTO_get_ex_data(in, idx), SESSION_NEW_IDX_VAL);
  57231. ExpectPtrEq(CRYPTO_get_ex_data(out, idx), SESSION_NEW_IDX_VAL);
  57232. ExpectIntEQ(argV, SESSION_NEW_IDX_LONG);
  57233. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  57234. *(void**)inPtr = SESSION_DUP_IDX_VAL;
  57235. if (EXPECT_SUCCESS()) {
  57236. return SSL_SUCCESS;
  57237. }
  57238. else {
  57239. return SSL_FAILURE;
  57240. }
  57241. }
  57242. static int test_wolfSSL_SESSION_get_ex_new_index_free_cb_called = 0;
  57243. static void test_wolfSSL_SESSION_get_ex_new_index_free_cb(void* p, void* ptr,
  57244. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  57245. {
  57246. EXPECT_DECLS;
  57247. ExpectNotNull(p);
  57248. ExpectNull(ptr);
  57249. ExpectPtrNE(CRYPTO_get_ex_data(a, idx), 0);
  57250. ExpectIntEQ(argValue, SESSION_NEW_IDX_LONG);
  57251. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  57252. if (EXPECT_SUCCESS()) {
  57253. test_wolfSSL_SESSION_get_ex_new_index_free_cb_called++;
  57254. }
  57255. }
  57256. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  57257. {
  57258. EXPECT_DECLS;
  57259. int idx = SSL_SESSION_get_ex_new_index(SESSION_NEW_IDX_LONG,
  57260. (void*)SESSION_NEW_IDX_PTR,
  57261. test_wolfSSL_SESSION_get_ex_new_index_new_cb,
  57262. test_wolfSSL_SESSION_get_ex_new_index_dup_cb,
  57263. test_wolfSSL_SESSION_get_ex_new_index_free_cb);
  57264. SSL_SESSION* s = SSL_SESSION_new();
  57265. SSL_SESSION* d = NULL;
  57266. ExpectNotNull(s);
  57267. ExpectPtrEq(SSL_SESSION_get_ex_data(s, idx), SESSION_NEW_IDX_VAL);
  57268. ExpectNotNull(d = SSL_SESSION_dup(s));
  57269. ExpectPtrEq(SSL_SESSION_get_ex_data(d, idx), SESSION_DUP_IDX_VAL);
  57270. SSL_SESSION_free(s);
  57271. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 1);
  57272. SSL_SESSION_free(d);
  57273. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 2);
  57274. crypto_ex_cb_free(crypto_ex_cb_ctx_session);
  57275. crypto_ex_cb_ctx_session = NULL;
  57276. return EXPECT_RESULT();
  57277. }
  57278. #else
  57279. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  57280. {
  57281. return TEST_SKIPPED;
  57282. }
  57283. #endif
  57284. static int test_wolfSSL_set_psk_use_session_callback(void)
  57285. {
  57286. EXPECT_DECLS;
  57287. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  57288. SSL_CTX* ctx = NULL;
  57289. SSL* ssl = NULL;
  57290. const char* testCertFile;
  57291. const char* testKeyFile;
  57292. #ifdef WOLFSSL_TLS13
  57293. #ifdef NO_WOLFSSL_SERVER
  57294. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  57295. #else
  57296. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  57297. #endif
  57298. #else
  57299. #ifdef NO_WOLFSSL_SERVER
  57300. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  57301. #else
  57302. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  57303. #endif
  57304. #endif
  57305. #ifndef NO_RSA
  57306. testCertFile = svrCertFile;
  57307. testKeyFile = svrKeyFile;
  57308. #elif defined(HAVE_ECC)
  57309. testCertFile = eccCertFile;
  57310. testKeyFile = eccKeyFile;
  57311. #else
  57312. testCertFile = NULL;
  57313. testKeyFile = NULL;
  57314. #endif
  57315. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  57316. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  57317. SSL_FILETYPE_PEM));
  57318. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  57319. SSL_FILETYPE_PEM));
  57320. }
  57321. ExpectNotNull(ssl = SSL_new(ctx));
  57322. SSL_set_psk_use_session_callback(ssl, my_psk_use_session_cb);
  57323. SSL_CTX_free(ctx);
  57324. SSL_free(ssl);
  57325. #endif
  57326. return EXPECT_RESULT();
  57327. }
  57328. static int test_wolfSSL_ERR_strings(void)
  57329. {
  57330. EXPECT_DECLS;
  57331. #if !defined(NO_ERROR_STRINGS)
  57332. const char* err1 = "unsupported cipher suite";
  57333. const char* err2 = "wolfSSL PEM routines";
  57334. const char* err = NULL;
  57335. (void)err;
  57336. (void)err1;
  57337. (void)err2;
  57338. #if defined(OPENSSL_EXTRA)
  57339. ExpectNotNull(err = ERR_reason_error_string(UNSUPPORTED_SUITE));
  57340. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  57341. ExpectNotNull(err = ERR_func_error_string(UNSUPPORTED_SUITE));
  57342. ExpectIntEQ((*err == '\0'), 1);
  57343. ExpectNotNull(err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD));
  57344. ExpectIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  57345. #else
  57346. ExpectNotNull(err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE));
  57347. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  57348. ExpectNotNull(err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE));
  57349. ExpectIntEQ((*err == '\0'), 1);
  57350. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  57351. ExpectNotNull(err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2));
  57352. ExpectIntEQ((*err == '\0'), 1);
  57353. #endif
  57354. #endif
  57355. return EXPECT_RESULT();
  57356. }
  57357. static int test_wolfSSL_EVP_shake128(void)
  57358. {
  57359. EXPECT_DECLS;
  57360. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  57361. defined(WOLFSSL_SHAKE128)
  57362. const EVP_MD* md = NULL;
  57363. ExpectNotNull(md = EVP_shake128());
  57364. ExpectIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  57365. #endif
  57366. return EXPECT_RESULT();
  57367. }
  57368. static int test_wolfSSL_EVP_shake256(void)
  57369. {
  57370. EXPECT_DECLS;
  57371. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  57372. defined(WOLFSSL_SHAKE256)
  57373. const EVP_MD* md = NULL;
  57374. ExpectNotNull(md = EVP_shake256());
  57375. ExpectIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  57376. #endif
  57377. return EXPECT_RESULT();
  57378. }
  57379. /*
  57380. * Testing EVP digest API with SM3
  57381. */
  57382. static int test_wolfSSL_EVP_sm3(void)
  57383. {
  57384. int res = TEST_SKIPPED;
  57385. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM3)
  57386. EXPECT_DECLS;
  57387. const EVP_MD* md = NULL;
  57388. EVP_MD_CTX* mdCtx = NULL;
  57389. byte data[WC_SM3_BLOCK_SIZE * 4];
  57390. byte hash[WC_SM3_DIGEST_SIZE];
  57391. byte calcHash[WC_SM3_DIGEST_SIZE];
  57392. byte expHash[WC_SM3_DIGEST_SIZE] = {
  57393. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  57394. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  57395. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  57396. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  57397. };
  57398. word32 chunk;
  57399. word32 i;
  57400. unsigned int sz;
  57401. int ret;
  57402. XMEMSET(data, 0, sizeof(data));
  57403. md = EVP_sm3();
  57404. ExpectTrue(md != NULL);
  57405. ExpectIntEQ(XSTRNCMP(md, "SM3", XSTRLEN("SM3")), 0);
  57406. mdCtx = EVP_MD_CTX_new();
  57407. ExpectTrue(mdCtx != NULL);
  57408. /* Invalid Parameters */
  57409. ExpectIntEQ(EVP_DigestInit(NULL, md), BAD_FUNC_ARG);
  57410. /* Valid Parameters */
  57411. ExpectIntEQ(EVP_DigestInit(mdCtx, md), WOLFSSL_SUCCESS);
  57412. ExpectIntEQ(EVP_DigestUpdate(NULL, NULL, 1), WOLFSSL_FAILURE);
  57413. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 1), WOLFSSL_FAILURE);
  57414. ExpectIntEQ(EVP_DigestUpdate(NULL, data, 1), WOLFSSL_FAILURE);
  57415. /* Valid Parameters */
  57416. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 0), WOLFSSL_SUCCESS);
  57417. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  57418. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  57419. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE),
  57420. WOLFSSL_SUCCESS);
  57421. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE - 2),
  57422. WOLFSSL_SUCCESS);
  57423. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE * 2),
  57424. WOLFSSL_SUCCESS);
  57425. /* Ensure too many bytes for lengths. */
  57426. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_PAD_SIZE),
  57427. WOLFSSL_SUCCESS);
  57428. /* Invalid Parameters */
  57429. ExpectIntEQ(EVP_DigestFinal(NULL, NULL, NULL), WOLFSSL_FAILURE);
  57430. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  57431. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  57432. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  57433. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  57434. /* Valid Parameters */
  57435. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  57436. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  57437. /* Chunk tests. */
  57438. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, sizeof(data)), WOLFSSL_SUCCESS);
  57439. ExpectIntEQ(EVP_DigestFinal(mdCtx, calcHash, &sz), WOLFSSL_SUCCESS);
  57440. ExpectIntEQ(sz, WC_SM3_DIGEST_SIZE);
  57441. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  57442. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  57443. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i, chunk),
  57444. WOLFSSL_SUCCESS);
  57445. }
  57446. if (i < (word32)sizeof(data)) {
  57447. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i,
  57448. (word32)sizeof(data) - i), WOLFSSL_SUCCESS);
  57449. }
  57450. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  57451. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  57452. }
  57453. /* Not testing when the low 32-bit length overflows. */
  57454. ret = EVP_MD_CTX_cleanup(mdCtx);
  57455. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  57456. wolfSSL_EVP_MD_CTX_free(mdCtx);
  57457. res = EXPECT_RESULT();
  57458. #endif
  57459. return res;
  57460. } /* END test_EVP_sm3 */
  57461. static int test_EVP_blake2(void)
  57462. {
  57463. EXPECT_DECLS;
  57464. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  57465. const EVP_MD* md = NULL;
  57466. (void)md;
  57467. #if defined(HAVE_BLAKE2)
  57468. ExpectNotNull(md = EVP_blake2b512());
  57469. ExpectIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  57470. #endif
  57471. #if defined(HAVE_BLAKE2S)
  57472. ExpectNotNull(md = EVP_blake2s256());
  57473. ExpectIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  57474. #endif
  57475. #endif
  57476. return EXPECT_RESULT();
  57477. }
  57478. #if defined(OPENSSL_EXTRA)
  57479. static void list_md_fn(const EVP_MD* m, const char* from,
  57480. const char* to, void* arg)
  57481. {
  57482. const char* mn;
  57483. BIO *bio;
  57484. (void) from;
  57485. (void) to;
  57486. (void) arg;
  57487. (void) mn;
  57488. (void) bio;
  57489. if (!m) {
  57490. /* alias */
  57491. AssertNull(m);
  57492. AssertNotNull(to);
  57493. }
  57494. else {
  57495. AssertNotNull(m);
  57496. AssertNull(to);
  57497. }
  57498. AssertNotNull(from);
  57499. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  57500. mn = EVP_get_digestbyname(from);
  57501. /* print to stderr */
  57502. AssertNotNull(arg);
  57503. bio = BIO_new(BIO_s_file());
  57504. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  57505. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  57506. BIO_free(bio);
  57507. #endif
  57508. }
  57509. #endif
  57510. static int test_EVP_MD_do_all(void)
  57511. {
  57512. int res = TEST_SKIPPED;
  57513. #if defined(OPENSSL_EXTRA)
  57514. EVP_MD_do_all(NULL, stderr);
  57515. EVP_MD_do_all(list_md_fn, stderr);
  57516. res = TEST_SUCCESS;
  57517. #endif
  57518. return res;
  57519. }
  57520. #if defined(OPENSSL_EXTRA)
  57521. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  57522. {
  57523. (void)arg;
  57524. (void)nm;
  57525. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  57526. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  57527. /* print to stderr */
  57528. AssertNotNull(arg);
  57529. BIO *bio = BIO_new(BIO_s_file());
  57530. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  57531. BIO_printf(bio, "%s\n", nm);
  57532. BIO_free(bio);
  57533. #endif
  57534. }
  57535. #endif
  57536. static int test_OBJ_NAME_do_all(void)
  57537. {
  57538. int res = TEST_SKIPPED;
  57539. #if defined(OPENSSL_EXTRA)
  57540. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  57541. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
  57542. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
  57543. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
  57544. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
  57545. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
  57546. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
  57547. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
  57548. OBJ_NAME_do_all(-1, obj_name_t, stderr);
  57549. res = TEST_SUCCESS;
  57550. #endif
  57551. return res;
  57552. }
  57553. static int test_SSL_CIPHER_get_xxx(void)
  57554. {
  57555. EXPECT_DECLS;
  57556. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  57557. !defined(NO_FILESYSTEM)
  57558. const SSL_CIPHER* cipher = NULL;
  57559. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  57560. int i, numCiphers = 0;
  57561. SSL_CTX* ctx = NULL;
  57562. SSL* ssl = NULL;
  57563. const char* testCertFile;
  57564. const char* testKeyFile;
  57565. char buf[256] = {0};
  57566. const char* cipher_id = NULL;
  57567. int expect_nid1 = NID_undef;
  57568. int expect_nid2 = NID_undef;
  57569. int expect_nid3 = NID_undef;
  57570. int expect_nid4 = NID_undef;
  57571. int expect_nid5 = 0;
  57572. const char* cipher_id2 = NULL;
  57573. int expect_nid21 = NID_undef;
  57574. int expect_nid22 = NID_undef;
  57575. int expect_nid23 = NID_undef;
  57576. int expect_nid24 = NID_undef;
  57577. int expect_nid25 = 0;
  57578. (void)cipher;
  57579. (void)supportedCiphers;
  57580. (void)i;
  57581. (void)numCiphers;
  57582. (void)ctx;
  57583. (void)ssl;
  57584. (void)testCertFile;
  57585. (void)testKeyFile;
  57586. #if defined(WOLFSSL_TLS13)
  57587. cipher_id = "TLS13-AES128-GCM-SHA256";
  57588. expect_nid1 = NID_auth_rsa;
  57589. expect_nid2 = NID_aes_128_gcm;
  57590. expect_nid3 = NID_sha256;
  57591. expect_nid4 = NID_kx_any;
  57592. expect_nid5 = 1;
  57593. #if !defined(WOLFSSL_NO_TLS12)
  57594. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  57595. expect_nid21 = NID_auth_rsa;
  57596. expect_nid22 = NID_aes_256_gcm;
  57597. expect_nid23 = NID_sha384;
  57598. expect_nid24 = NID_kx_ecdhe;
  57599. expect_nid25 = 1;
  57600. #endif
  57601. #endif
  57602. #ifdef NO_WOLFSSL_SERVER
  57603. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  57604. #else
  57605. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  57606. #endif
  57607. if (cipher_id) {
  57608. #ifndef NO_RSA
  57609. testCertFile = svrCertFile;
  57610. testKeyFile = svrKeyFile;
  57611. #elif defined(HAVE_ECC)
  57612. testCertFile = eccCertFile;
  57613. testKeyFile = eccKeyFile;
  57614. #else
  57615. testCertFile = NULL;
  57616. testKeyFile = NULL;
  57617. #endif
  57618. if (testCertFile != NULL && testKeyFile != NULL) {
  57619. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  57620. SSL_FILETYPE_PEM));
  57621. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  57622. SSL_FILETYPE_PEM));
  57623. }
  57624. ExpectNotNull(ssl = SSL_new(ctx));
  57625. ExpectIntEQ(SSL_in_init(ssl), 1);
  57626. supportedCiphers = SSL_get_ciphers(ssl);
  57627. numCiphers = sk_num(supportedCiphers);
  57628. for (i = 0; i < numCiphers; ++i) {
  57629. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  57630. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  57631. }
  57632. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  57633. break;
  57634. }
  57635. }
  57636. /* test case for */
  57637. if (i != numCiphers) {
  57638. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  57639. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  57640. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  57641. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  57642. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  57643. }
  57644. if (cipher_id2) {
  57645. for (i = 0; i < numCiphers; ++i) {
  57646. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  57647. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  57648. }
  57649. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  57650. break;
  57651. }
  57652. }
  57653. /* test case for */
  57654. if (i != numCiphers) {
  57655. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  57656. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  57657. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  57658. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  57659. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  57660. }
  57661. }
  57662. }
  57663. SSL_CTX_free(ctx);
  57664. SSL_free(ssl);
  57665. #endif
  57666. return EXPECT_RESULT();
  57667. }
  57668. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  57669. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  57670. int* keyFormat)
  57671. {
  57672. int ret;
  57673. byte* key_buf = NULL;
  57674. size_t key_sz = 0;
  57675. EncryptedInfo encInfo;
  57676. XMEMSET(&encInfo, 0, sizeof(encInfo));
  57677. ret = load_file(privKeyFile, &key_buf, &key_sz);
  57678. if (ret == 0) {
  57679. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  57680. NULL, &encInfo, keyFormat);
  57681. }
  57682. if (key_buf != NULL) {
  57683. free(key_buf); key_buf = NULL;
  57684. }
  57685. (void)encInfo; /* not used in this test */
  57686. #ifdef DEBUG_WOLFSSL
  57687. fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  57688. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  57689. (*pDer)->length);
  57690. #endif
  57691. return ret;
  57692. }
  57693. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  57694. {
  57695. int ret = CRYPTOCB_UNAVAILABLE;
  57696. const char* privKeyFile = (const char*)ctx;
  57697. DerBuffer* pDer = NULL;
  57698. int keyFormat = 0;
  57699. if (info->algo_type == WC_ALGO_TYPE_PK) {
  57700. #ifdef DEBUG_WOLFSSL
  57701. fprintf(stderr, "test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  57702. #endif
  57703. #ifndef NO_RSA
  57704. if (info->pk.type == WC_PK_TYPE_RSA) {
  57705. switch (info->pk.rsa.type) {
  57706. case RSA_PUBLIC_ENCRYPT:
  57707. case RSA_PUBLIC_DECRYPT:
  57708. /* perform software based RSA public op */
  57709. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  57710. break;
  57711. case RSA_PRIVATE_ENCRYPT:
  57712. case RSA_PRIVATE_DECRYPT:
  57713. {
  57714. RsaKey key;
  57715. /* perform software based RSA private op */
  57716. #ifdef DEBUG_WOLFSSL
  57717. fprintf(stderr, "test_CryptoCb_Func: RSA Priv\n");
  57718. #endif
  57719. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  57720. &keyFormat);
  57721. if (ret != 0) {
  57722. return ret;
  57723. }
  57724. ret = wc_InitRsaKey(&key, HEAP_HINT);
  57725. if (ret == 0) {
  57726. word32 keyIdx = 0;
  57727. /* load RSA private key and perform private transform */
  57728. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  57729. &key, pDer->length);
  57730. if (ret == 0) {
  57731. ret = wc_RsaFunction(
  57732. info->pk.rsa.in, info->pk.rsa.inLen,
  57733. info->pk.rsa.out, info->pk.rsa.outLen,
  57734. info->pk.rsa.type, &key, info->pk.rsa.rng);
  57735. }
  57736. else {
  57737. /* if decode fails, then fall-back to software based crypto */
  57738. fprintf(stderr, "test_CryptoCb_Func: RSA private "
  57739. "key decode failed %d, falling back to "
  57740. "software\n", ret);
  57741. ret = CRYPTOCB_UNAVAILABLE;
  57742. }
  57743. wc_FreeRsaKey(&key);
  57744. }
  57745. wc_FreeDer(&pDer); pDer = NULL;
  57746. break;
  57747. }
  57748. }
  57749. #ifdef DEBUG_WOLFSSL
  57750. fprintf(stderr, "test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  57751. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  57752. #endif
  57753. }
  57754. #endif /* !NO_RSA */
  57755. #ifdef HAVE_ECC
  57756. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  57757. /* mark this key as ephemeral */
  57758. if (info->pk.eckg.key != NULL) {
  57759. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  57760. sizeof(info->pk.eckg.key->label));
  57761. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  57762. }
  57763. }
  57764. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  57765. ecc_key key;
  57766. /* perform software based ECC sign */
  57767. #ifdef DEBUG_WOLFSSL
  57768. fprintf(stderr, "test_CryptoCb_Func: ECC Sign\n");
  57769. #endif
  57770. if (info->pk.eccsign.key != NULL &&
  57771. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  57772. /* this is an empheral key */
  57773. #ifdef DEBUG_WOLFSSL
  57774. fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
  57775. "ephemeral key\n");
  57776. #endif
  57777. return CRYPTOCB_UNAVAILABLE;
  57778. }
  57779. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  57780. if (ret != 0) {
  57781. return ret;
  57782. }
  57783. ret = wc_ecc_init(&key);
  57784. if (ret == 0) {
  57785. word32 keyIdx = 0;
  57786. /* load ECC private key and perform private transform */
  57787. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  57788. &key, pDer->length);
  57789. if (ret == 0) {
  57790. ret = wc_ecc_sign_hash(
  57791. info->pk.eccsign.in, info->pk.eccsign.inlen,
  57792. info->pk.eccsign.out, info->pk.eccsign.outlen,
  57793. info->pk.eccsign.rng, &key);
  57794. }
  57795. else {
  57796. /* if decode fails, then fall-back to software based crypto */
  57797. fprintf(stderr, "test_CryptoCb_Func: ECC private key "
  57798. "decode failed %d, falling back to software\n", ret);
  57799. ret = CRYPTOCB_UNAVAILABLE;
  57800. }
  57801. wc_ecc_free(&key);
  57802. }
  57803. wc_FreeDer(&pDer); pDer = NULL;
  57804. #ifdef DEBUG_WOLFSSL
  57805. fprintf(stderr, "test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  57806. ret, *info->pk.eccsign.outlen);
  57807. #endif
  57808. }
  57809. #endif /* HAVE_ECC */
  57810. #ifdef HAVE_ED25519
  57811. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  57812. ed25519_key key;
  57813. /* perform software based ED25519 sign */
  57814. #ifdef DEBUG_WOLFSSL
  57815. fprintf(stderr, "test_CryptoCb_Func: ED25519 Sign\n");
  57816. #endif
  57817. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  57818. if (ret != 0) {
  57819. return ret;
  57820. }
  57821. ret = wc_ed25519_init(&key);
  57822. if (ret == 0) {
  57823. word32 keyIdx = 0;
  57824. /* load ED25519 private key and perform private transform */
  57825. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  57826. &key, pDer->length);
  57827. if (ret == 0) {
  57828. /* calculate public key */
  57829. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  57830. if (ret == 0) {
  57831. key.pubKeySet = 1;
  57832. ret = wc_ed25519_sign_msg_ex(
  57833. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  57834. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  57835. &key, info->pk.ed25519sign.type,
  57836. info->pk.ed25519sign.context,
  57837. info->pk.ed25519sign.contextLen);
  57838. }
  57839. }
  57840. else {
  57841. /* if decode fails, then fall-back to software based crypto */
  57842. fprintf(stderr, "test_CryptoCb_Func: ED25519 private key "
  57843. "decode failed %d, falling back to software\n", ret);
  57844. ret = CRYPTOCB_UNAVAILABLE;
  57845. }
  57846. wc_ed25519_free(&key);
  57847. }
  57848. wc_FreeDer(&pDer); pDer = NULL;
  57849. #ifdef DEBUG_WOLFSSL
  57850. fprintf(stderr, "test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  57851. ret, *info->pk.ed25519sign.outLen);
  57852. #endif
  57853. }
  57854. #endif /* HAVE_ED25519 */
  57855. }
  57856. (void)thisDevId;
  57857. (void)keyFormat;
  57858. return ret;
  57859. }
  57860. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  57861. static int test_wc_CryptoCb_TLS(int tlsVer,
  57862. const char* cliCaPemFile, const char* cliCertPemFile,
  57863. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  57864. const char* svrCaPemFile, const char* svrCertPemFile,
  57865. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  57866. {
  57867. EXPECT_DECLS;
  57868. callback_functions client_cbf;
  57869. callback_functions server_cbf;
  57870. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  57871. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  57872. if (tlsVer == WOLFSSL_TLSV1_3) {
  57873. #ifdef WOLFSSL_TLS13
  57874. server_cbf.method = wolfTLSv1_3_server_method;
  57875. client_cbf.method = wolfTLSv1_3_client_method;
  57876. #endif
  57877. }
  57878. else if (tlsVer == WOLFSSL_TLSV1_2) {
  57879. #ifndef WOLFSSL_NO_TLS12
  57880. server_cbf.method = wolfTLSv1_2_server_method;
  57881. client_cbf.method = wolfTLSv1_2_client_method;
  57882. #endif
  57883. }
  57884. else if (tlsVer == WOLFSSL_TLSV1_1) {
  57885. #ifndef NO_OLD_TLS
  57886. server_cbf.method = wolfTLSv1_1_server_method;
  57887. client_cbf.method = wolfTLSv1_1_client_method;
  57888. #endif
  57889. }
  57890. else if (tlsVer == WOLFSSL_TLSV1) {
  57891. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  57892. server_cbf.method = wolfTLSv1_server_method;
  57893. client_cbf.method = wolfTLSv1_client_method;
  57894. #endif
  57895. }
  57896. else if (tlsVer == WOLFSSL_SSLV3) {
  57897. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  57898. defined(WOLFSSL_STATIC_RSA)
  57899. server_cbf.method = wolfSSLv3_server_method;
  57900. client_cbf.method = wolfSSLv3_client_method;
  57901. #endif
  57902. }
  57903. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  57904. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  57905. server_cbf.method = wolfDTLSv1_2_server_method;
  57906. client_cbf.method = wolfDTLSv1_2_client_method;
  57907. #endif
  57908. }
  57909. else if (tlsVer == WOLFSSL_DTLSV1) {
  57910. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  57911. server_cbf.method = wolfDTLSv1_server_method;
  57912. client_cbf.method = wolfDTLSv1_client_method;
  57913. #endif
  57914. }
  57915. if (server_cbf.method == NULL) {
  57916. /* not enabled */
  57917. return TEST_SUCCESS;
  57918. }
  57919. /* Setup the keys for the TLS test */
  57920. client_cbf.certPemFile = cliCertPemFile;
  57921. client_cbf.keyPemFile = cliPubKeyPemFile;
  57922. client_cbf.caPemFile = cliCaPemFile;
  57923. server_cbf.certPemFile = svrCertPemFile;
  57924. server_cbf.keyPemFile = svrPubKeyPemFile;
  57925. server_cbf.caPemFile = svrCaPemFile;
  57926. /* Setup a crypto callback with pointer to private key file for testing */
  57927. client_cbf.devId = 1;
  57928. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  57929. (void*)cliPrivKeyPemFile);
  57930. server_cbf.devId = 2;
  57931. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  57932. (void*)svrPrivKeyPemFile);
  57933. /* Perform TLS server and client test */
  57934. /* First test is at WOLFSSL_CTX level */
  57935. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  57936. /* Check for success */
  57937. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  57938. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  57939. if (EXPECT_SUCCESS()) {
  57940. /* Second test is a WOLFSSL object level */
  57941. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  57942. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  57943. }
  57944. /* Check for success */
  57945. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  57946. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  57947. /* Un register the devId's */
  57948. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  57949. client_cbf.devId = INVALID_DEVID;
  57950. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  57951. server_cbf.devId = INVALID_DEVID;
  57952. return EXPECT_RESULT();
  57953. }
  57954. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  57955. static int test_wc_CryptoCb(void)
  57956. {
  57957. EXPECT_DECLS;
  57958. #ifdef WOLF_CRYPTO_CB
  57959. /* TODO: Add crypto callback API tests */
  57960. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  57961. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  57962. int tlsVer;
  57963. #endif
  57964. #ifndef NO_RSA
  57965. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  57966. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  57967. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  57968. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile),
  57969. TEST_SUCCESS);
  57970. }
  57971. #endif
  57972. #ifdef HAVE_ECC
  57973. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  57974. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  57975. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  57976. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile),
  57977. TEST_SUCCESS);
  57978. }
  57979. #endif
  57980. #ifdef HAVE_ED25519
  57981. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  57982. if (tlsVer == WOLFSSL_DTLSV1) continue;
  57983. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  57984. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  57985. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile),
  57986. TEST_SUCCESS);
  57987. }
  57988. #endif
  57989. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  57990. #endif /* WOLF_CRYPTO_CB */
  57991. return EXPECT_RESULT();
  57992. }
  57993. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  57994. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  57995. static int test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  57996. const char* cliCaPemFile, const char* cliCertPemFile,
  57997. const char* cliPrivKeyPemFile,
  57998. const char* svrCaPemFile, const char* svrCertPemFile,
  57999. const char* svrPrivKeyPemFile,
  58000. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  58001. {
  58002. EXPECT_DECLS;
  58003. callback_functions client_cbf;
  58004. callback_functions server_cbf;
  58005. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  58006. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  58007. if (tlsVer == WOLFSSL_TLSV1_3) {
  58008. #ifdef WOLFSSL_TLS13
  58009. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  58010. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  58011. #endif
  58012. }
  58013. else if (tlsVer == WOLFSSL_TLSV1_2) {
  58014. #ifndef WOLFSSL_NO_TLS12
  58015. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  58016. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  58017. #endif
  58018. }
  58019. else if (tlsVer == WOLFSSL_TLSV1_1) {
  58020. #ifndef NO_OLD_TLS
  58021. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  58022. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  58023. #endif
  58024. }
  58025. else if (tlsVer == WOLFSSL_TLSV1) {
  58026. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  58027. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  58028. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  58029. #endif
  58030. }
  58031. else if (tlsVer == WOLFSSL_SSLV3) {
  58032. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  58033. defined(WOLFSSL_STATIC_RSA)
  58034. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  58035. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  58036. #endif
  58037. }
  58038. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  58039. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  58040. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  58041. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  58042. #endif
  58043. }
  58044. else if (tlsVer == WOLFSSL_DTLSV1) {
  58045. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  58046. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  58047. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  58048. #endif
  58049. }
  58050. if (server_cbf.method_ex == NULL) {
  58051. /* not enabled */
  58052. return TEST_SUCCESS;
  58053. }
  58054. /* Setup the keys for the TLS test */
  58055. client_cbf.certPemFile = cliCertPemFile;
  58056. client_cbf.keyPemFile = cliPrivKeyPemFile;
  58057. client_cbf.caPemFile = cliCaPemFile;
  58058. server_cbf.certPemFile = svrCertPemFile;
  58059. server_cbf.keyPemFile = svrPrivKeyPemFile;
  58060. server_cbf.caPemFile = svrCaPemFile;
  58061. client_cbf.mem = cliMem;
  58062. client_cbf.memSz = cliMemSz;
  58063. server_cbf.mem = svrMem;
  58064. server_cbf.memSz = svrMemSz;
  58065. client_cbf.devId = INVALID_DEVID;
  58066. server_cbf.devId = INVALID_DEVID;
  58067. /* Perform TLS server and client test */
  58068. /* First test is at WOLFSSL_CTX level */
  58069. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  58070. /* Check for success */
  58071. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  58072. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  58073. if (EXPECT_SUCCESS()) {
  58074. /* Second test is a WOLFSSL object level */
  58075. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  58076. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  58077. }
  58078. /* Check for success */
  58079. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  58080. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  58081. return EXPECT_RESULT();
  58082. }
  58083. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  58084. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  58085. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  58086. {
  58087. EXPECT_DECLS;
  58088. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  58089. WOLFSSL_MEM_STATS mem_stats;
  58090. WOLFSSL_MEM_CONN_STATS ssl_stats;
  58091. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  58092. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  58093. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  58094. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  58095. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  58096. #endif
  58097. ExpectNotNull((ssl1 = wolfSSL_new(ctx)));
  58098. ExpectNotNull((ssl2 = wolfSSL_new(ctx)));
  58099. /* this should fail because kMaxCtxClients == 2 */
  58100. ExpectNull((ssl3 = wolfSSL_new(ctx)));
  58101. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  58102. #ifdef DEBUG_WOLFSSL
  58103. wolfSSL_PrintStatsConn(&ssl_stats);
  58104. #endif
  58105. (void)ssl_stats;
  58106. }
  58107. /* display collected statistics */
  58108. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  58109. #ifdef DEBUG_WOLFSSL
  58110. wolfSSL_PrintStats(&mem_stats);
  58111. #endif
  58112. (void)mem_stats;
  58113. }
  58114. wolfSSL_free(ssl1);
  58115. wolfSSL_free(ssl2);
  58116. return EXPECT_RESULT();
  58117. }
  58118. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  58119. static int test_wolfSSL_CTX_StaticMemory(void)
  58120. {
  58121. EXPECT_DECLS;
  58122. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  58123. wolfSSL_method_func method_func;
  58124. WOLFSSL_CTX* ctx;
  58125. const int kMaxCtxClients = 2;
  58126. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  58127. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  58128. int tlsVer;
  58129. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  58130. #endif
  58131. #endif
  58132. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  58133. #ifndef NO_WOLFSSL_SERVER
  58134. #ifndef WOLFSSL_NO_TLS12
  58135. method_func = wolfTLSv1_2_server_method_ex;
  58136. #else
  58137. method_func = wolfTLSv1_3_server_method_ex;
  58138. #endif
  58139. #else
  58140. #ifndef WOLFSSL_NO_TLS12
  58141. method_func = wolfTLSv1_2_client_method_ex;
  58142. #else
  58143. method_func = wolfTLSv1_3_client_method_ex;
  58144. #endif
  58145. #endif
  58146. /* Test creating CTX directly from static memory pool */
  58147. ctx = NULL;
  58148. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, method_func, svrMem,
  58149. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  58150. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  58151. wolfSSL_CTX_free(ctx);
  58152. ctx = NULL;
  58153. /* Test for heap allocated CTX, then assigning static pool to it */
  58154. ExpectNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  58155. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, NULL, svrMem,
  58156. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  58157. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  58158. wolfSSL_CTX_free(ctx);
  58159. /* TLS Level Tests using static memory */
  58160. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  58161. #ifndef NO_RSA
  58162. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  58163. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  58164. svrCertFile, cliCertFile, cliKeyFile,
  58165. cliCertFile, svrCertFile, svrKeyFile,
  58166. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  58167. TEST_SUCCESS);
  58168. }
  58169. #endif
  58170. #ifdef HAVE_ECC
  58171. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  58172. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  58173. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  58174. cliEccCertFile, eccCertFile, eccKeyFile,
  58175. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  58176. TEST_SUCCESS);
  58177. }
  58178. #endif
  58179. #ifdef HAVE_ED25519
  58180. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  58181. if (tlsVer == WOLFSSL_DTLSV1) continue;
  58182. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  58183. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  58184. cliEdCertFile, edCertFile, edKeyFile,
  58185. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  58186. TEST_SUCCESS);
  58187. }
  58188. #endif
  58189. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  58190. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  58191. return EXPECT_RESULT();
  58192. }
  58193. static int test_openssl_FIPS_drbg(void)
  58194. {
  58195. EXPECT_DECLS;
  58196. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  58197. DRBG_CTX* dctx = NULL;
  58198. byte data1[32], data2[32], zeroData[32];
  58199. byte testSeed[16];
  58200. size_t dlen = sizeof(data1);
  58201. int i;
  58202. XMEMSET(data1, 0, dlen);
  58203. XMEMSET(data2, 0, dlen);
  58204. XMEMSET(zeroData, 0, sizeof(zeroData));
  58205. for (i = 0; i < (int)sizeof(testSeed); i++) {
  58206. testSeed[i] = (byte)i;
  58207. }
  58208. ExpectNotNull(dctx = FIPS_get_default_drbg());
  58209. ExpectIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  58210. ExpectIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  58211. WOLFSSL_SUCCESS);
  58212. ExpectIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  58213. ExpectIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  58214. WOLFSSL_SUCCESS);
  58215. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  58216. ExpectIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  58217. WOLFSSL_SUCCESS);
  58218. ExpectIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  58219. WOLFSSL_SUCCESS);
  58220. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  58221. ExpectIntNE(XMEMCMP(data1, data2, dlen), 0);
  58222. ExpectIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  58223. #ifndef HAVE_GLOBAL_RNG
  58224. /* gets freed by wolfSSL_Cleanup() when HAVE_GLOBAL_RNG defined */
  58225. wolfSSL_FIPS_drbg_free(dctx);
  58226. #endif
  58227. #endif
  58228. return EXPECT_RESULT();
  58229. }
  58230. static int test_wolfSSL_FIPS_mode(void)
  58231. {
  58232. EXPECT_DECLS;
  58233. #if defined(OPENSSL_ALL)
  58234. #ifdef HAVE_FIPS
  58235. ExpectIntEQ(wolfSSL_FIPS_mode(), 1);
  58236. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  58237. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  58238. #else
  58239. ExpectIntEQ(wolfSSL_FIPS_mode(), 0);
  58240. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  58241. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  58242. #endif
  58243. #endif
  58244. return EXPECT_RESULT();
  58245. }
  58246. #ifdef WOLFSSL_DTLS
  58247. /* Prints out the current window */
  58248. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  58249. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  58250. int i;
  58251. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  58252. word32 b = w[i];
  58253. int j;
  58254. /* Prints out a 32 bit binary number in big endian order */
  58255. for (j = 0; j < 32; j++, b <<= 1) {
  58256. if (b & (((word32)1) << 31))
  58257. fprintf(stderr, "1");
  58258. else
  58259. fprintf(stderr, "0");
  58260. }
  58261. fprintf(stderr, " ");
  58262. }
  58263. fprintf(stderr, "cur_hi %u cur_lo %u\n", h, l);
  58264. #else
  58265. (void)h;
  58266. (void)l;
  58267. (void)w;
  58268. #endif
  58269. }
  58270. /* a - cur_hi
  58271. * b - cur_lo
  58272. * c - next_hi
  58273. * d - next_lo
  58274. * e - window
  58275. * f - expected next_hi
  58276. * g - expected next_lo
  58277. * h - expected window[1]
  58278. * i - expected window[0]
  58279. */
  58280. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  58281. ExpectIntEQ(wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)), 1); \
  58282. DUW_TEST_print_window_binary((a), (b), (e)); \
  58283. ExpectIntEQ((c), (f)); \
  58284. ExpectIntEQ((d), (g)); \
  58285. ExpectIntEQ((e)[1], (h)); \
  58286. ExpectIntEQ((e)[0], (i)); \
  58287. } while (0)
  58288. static int test_wolfSSL_DtlsUpdateWindow(void)
  58289. {
  58290. EXPECT_DECLS;
  58291. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  58292. word32 next_lo = 0;
  58293. word16 next_hi = 0;
  58294. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  58295. fprintf(stderr, "\n");
  58296. #endif
  58297. XMEMSET(window, 0, sizeof window);
  58298. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  58299. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  58300. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  58301. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  58302. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  58303. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  58304. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  58305. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  58306. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  58307. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  58308. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  58309. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  58310. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  58311. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  58312. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  58313. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  58314. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  58315. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  58316. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  58317. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  58318. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  58319. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  58320. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  58321. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  58322. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  58323. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  58324. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  58325. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  58326. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  58327. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  58328. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  58329. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  58330. return EXPECT_RESULT();
  58331. }
  58332. #endif /* WOLFSSL_DTLS */
  58333. #ifdef WOLFSSL_DTLS
  58334. static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
  58335. word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
  58336. {
  58337. DtlsMsg* cur;
  58338. static byte msg[100];
  58339. static byte msgInit = 0;
  58340. if (!msgInit) {
  58341. int i;
  58342. for (i = 0; i < 100; i++)
  58343. msg[i] = i + 1;
  58344. msgInit = 1;
  58345. }
  58346. /* Sanitize test parameters */
  58347. if (len > sizeof(msg))
  58348. return -1;
  58349. if (f_offset + f_len > sizeof(msg))
  58350. return -1;
  58351. DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
  58352. if (ssl->dtls_rx_msg_list == NULL)
  58353. return -100;
  58354. if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
  58355. return -200;
  58356. if (cur->fragBucketListCount != f_count)
  58357. return -300;
  58358. if (cur->ready != ready)
  58359. return -400;
  58360. if (cur->bytesReceived != bytesReceived)
  58361. return -500;
  58362. if (ready) {
  58363. if (cur->fragBucketList != NULL)
  58364. return -600;
  58365. if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
  58366. return -700;
  58367. }
  58368. else {
  58369. DtlsFragBucket* fb;
  58370. if (cur->fragBucketList == NULL)
  58371. return -800;
  58372. for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
  58373. if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
  58374. return -900;
  58375. }
  58376. }
  58377. return 0;
  58378. }
  58379. static int test_wolfSSL_DTLS_fragment_buckets(void)
  58380. {
  58381. EXPECT_DECLS;
  58382. WOLFSSL ssl[1];
  58383. XMEMSET(ssl, 0, sizeof(*ssl));
  58384. ExpectIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
  58385. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  58386. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
  58387. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
  58388. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
  58389. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
  58390. /* Test all permutations of 3 regions */
  58391. /* 1 2 3 */
  58392. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  58393. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
  58394. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  58395. /* 1 3 2 */
  58396. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  58397. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
  58398. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  58399. /* 2 1 3 */
  58400. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  58401. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
  58402. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  58403. /* 2 3 1 */
  58404. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  58405. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
  58406. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  58407. /* 3 1 2 */
  58408. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  58409. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
  58410. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  58411. /* 3 2 1 */
  58412. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  58413. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
  58414. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  58415. /* Test overlapping regions */
  58416. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  58417. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
  58418. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
  58419. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
  58420. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
  58421. /* Test overlapping multiple regions */
  58422. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  58423. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
  58424. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
  58425. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
  58426. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
  58427. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
  58428. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
  58429. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
  58430. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
  58431. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
  58432. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  58433. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
  58434. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
  58435. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
  58436. DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
  58437. ssl->dtls_rx_msg_list = NULL;
  58438. ssl->dtls_rx_msg_list_sz = 0;
  58439. return EXPECT_RESULT();
  58440. }
  58441. #endif
  58442. #if !defined(NO_FILESYSTEM) && \
  58443. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58444. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  58445. static int test_wolfSSL_dtls_stateless2(void)
  58446. {
  58447. EXPECT_DECLS;
  58448. WOLFSSL *ssl_c = NULL;
  58449. WOLFSSL *ssl_c2 = NULL;
  58450. WOLFSSL *ssl_s = NULL;
  58451. struct test_memio_ctx test_ctx;
  58452. WOLFSSL_CTX *ctx_c = NULL;
  58453. WOLFSSL_CTX *ctx_s = NULL;
  58454. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58455. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58456. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  58457. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  58458. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  58459. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  58460. /* send CH */
  58461. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  58462. (ssl_c2->error == WANT_READ));
  58463. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58464. (ssl_s->error == WANT_READ));
  58465. ExpectIntNE(test_ctx.c_len, 0);
  58466. /* consume HRR */
  58467. test_ctx.c_len = 0;
  58468. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58469. wolfSSL_free(ssl_c2);
  58470. wolfSSL_free(ssl_c);
  58471. wolfSSL_free(ssl_s);
  58472. wolfSSL_CTX_free(ctx_c);
  58473. wolfSSL_CTX_free(ctx_s);
  58474. return EXPECT_RESULT();
  58475. }
  58476. #ifdef HAVE_MAX_FRAGMENT
  58477. static int test_wolfSSL_dtls_stateless_maxfrag(void)
  58478. {
  58479. EXPECT_DECLS;
  58480. WOLFSSL *ssl_c = NULL;
  58481. WOLFSSL *ssl_c2 = NULL;
  58482. WOLFSSL *ssl_s = NULL;
  58483. struct test_memio_ctx test_ctx;
  58484. WOLFSSL_CTX *ctx_c = NULL;
  58485. WOLFSSL_CTX *ctx_s = NULL;
  58486. word16 max_fragment = 0;
  58487. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58488. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58489. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  58490. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  58491. ExpectIntEQ(wolfSSL_UseMaxFragment(ssl_c2, WOLFSSL_MFL_2_8),
  58492. WOLFSSL_SUCCESS);
  58493. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  58494. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  58495. if (ssl_s != NULL) {
  58496. max_fragment = ssl_s->max_fragment;
  58497. }
  58498. /* send CH */
  58499. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  58500. (ssl_c2->error == WANT_READ));
  58501. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58502. (ssl_s->error == WANT_READ));
  58503. /* CH without cookie shouldn't change state */
  58504. ExpectIntEQ(ssl_s->max_fragment, max_fragment);
  58505. ExpectIntNE(test_ctx.c_len, 0);
  58506. /* consume HRR from buffer */
  58507. test_ctx.c_len = 0;
  58508. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58509. wolfSSL_free(ssl_c2);
  58510. wolfSSL_free(ssl_c);
  58511. wolfSSL_free(ssl_s);
  58512. wolfSSL_CTX_free(ctx_c);
  58513. wolfSSL_CTX_free(ctx_s);
  58514. return EXPECT_RESULT();
  58515. }
  58516. #endif /* HAVE_MAX_FRAGMENT */
  58517. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  58518. #define ROUNDS_WITH_HVR 4
  58519. #define ROUNDS_WITHOUT_HVR 2
  58520. #define HANDSHAKE_TYPE_OFFSET DTLS_RECORD_HEADER_SZ
  58521. static int buf_is_hvr(const byte *data, int len)
  58522. {
  58523. if (len < DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ)
  58524. return 0;
  58525. return data[HANDSHAKE_TYPE_OFFSET] == hello_verify_request;
  58526. }
  58527. static int _test_wolfSSL_dtls_stateless_resume(byte useticket, byte bad)
  58528. {
  58529. EXPECT_DECLS;
  58530. struct test_memio_ctx test_ctx;
  58531. WOLFSSL_CTX *ctx_c = NULL;
  58532. WOLFSSL_CTX *ctx_s = NULL;
  58533. WOLFSSL *ssl_c = NULL;
  58534. WOLFSSL *ssl_s = NULL;
  58535. WOLFSSL_SESSION *sess = NULL;
  58536. int round_trips;
  58537. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58538. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  58539. &ssl_s, wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  58540. #ifdef HAVE_SESSION_TICKET
  58541. if (useticket) {
  58542. ExpectIntEQ(wolfSSL_UseSessionTicket(ssl_c), WOLFSSL_SUCCESS);
  58543. }
  58544. #endif
  58545. round_trips = ROUNDS_WITH_HVR;
  58546. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, round_trips,
  58547. &round_trips), 0);
  58548. ExpectIntEQ(round_trips, ROUNDS_WITH_HVR);
  58549. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  58550. wolfSSL_shutdown(ssl_c);
  58551. wolfSSL_shutdown(ssl_s);
  58552. wolfSSL_free(ssl_c);
  58553. ssl_c = NULL;
  58554. wolfSSL_free(ssl_s);
  58555. ssl_s = NULL;
  58556. test_ctx.c_len = test_ctx.s_len = 0;
  58557. /* make resumption invalid */
  58558. if (bad && (sess != NULL)) {
  58559. if (useticket) {
  58560. #ifdef HAVE_SESSION_TICKET
  58561. if (sess->ticket != NULL) {
  58562. sess->ticket[0] = !sess->ticket[0];
  58563. }
  58564. #endif /* HAVE_SESSION_TICKET */
  58565. }
  58566. else {
  58567. sess->sessionID[0] = !sess->sessionID[0];
  58568. }
  58569. }
  58570. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  58571. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  58572. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  58573. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  58574. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  58575. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  58576. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  58577. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  58578. (ssl_c->error == WANT_READ));
  58579. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58580. (ssl_s->error == WANT_READ));
  58581. ExpectFalse(bad && !buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  58582. ExpectFalse(!bad && buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  58583. if (!useticket) {
  58584. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, &round_trips), 0);
  58585. ExpectFalse(bad && round_trips != ROUNDS_WITH_HVR - 1);
  58586. ExpectFalse(!bad && round_trips != ROUNDS_WITHOUT_HVR - 1);
  58587. }
  58588. wolfSSL_SESSION_free(sess);
  58589. wolfSSL_free(ssl_c);
  58590. wolfSSL_free(ssl_s);
  58591. wolfSSL_CTX_free(ctx_c);
  58592. wolfSSL_CTX_free(ctx_s);
  58593. return EXPECT_RESULT();
  58594. }
  58595. static int test_wolfSSL_dtls_stateless_resume(void)
  58596. {
  58597. EXPECT_DECLS;
  58598. #ifdef HAVE_SESSION_TICKET
  58599. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 0), TEST_SUCCESS);
  58600. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 1), TEST_SUCCESS);
  58601. #endif /* HAVE_SESION_TICKET */
  58602. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 0), TEST_SUCCESS);
  58603. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 1), TEST_SUCCESS);
  58604. return EXPECT_RESULT();
  58605. }
  58606. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  58607. #if !defined(NO_OLD_TLS)
  58608. static int test_wolfSSL_dtls_stateless_downgrade(void)
  58609. {
  58610. EXPECT_DECLS;
  58611. WOLFSSL_CTX *ctx_c = NULL;
  58612. WOLFSSL_CTX *ctx_c2 = NULL;
  58613. WOLFSSL_CTX *ctx_s = NULL;
  58614. WOLFSSL *ssl_c = NULL;
  58615. WOLFSSL *ssl_c2 = NULL;
  58616. WOLFSSL *ssl_s = NULL;
  58617. struct test_memio_ctx test_ctx;
  58618. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58619. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58620. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  58621. ExpectIntEQ(wolfSSL_CTX_SetMinVersion(ctx_s, WOLFSSL_DTLSV1),
  58622. WOLFSSL_SUCCESS);
  58623. ExpectNotNull(ctx_c2 = wolfSSL_CTX_new(wolfDTLSv1_client_method()));
  58624. wolfSSL_SetIORecv(ctx_c2, test_memio_read_cb);
  58625. wolfSSL_SetIOSend(ctx_c2, test_memio_write_cb);
  58626. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c2));
  58627. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  58628. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  58629. /* send CH */
  58630. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  58631. (ssl_c2->error == WANT_READ));
  58632. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58633. (ssl_s->error == WANT_READ));
  58634. ExpectIntNE(test_ctx.c_len, 0);
  58635. /* consume HRR */
  58636. test_ctx.c_len = 0;
  58637. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58638. wolfSSL_free(ssl_c2);
  58639. wolfSSL_free(ssl_c);
  58640. wolfSSL_free(ssl_s);
  58641. wolfSSL_CTX_free(ctx_c);
  58642. wolfSSL_CTX_free(ctx_c2);
  58643. wolfSSL_CTX_free(ctx_s);
  58644. return EXPECT_RESULT();
  58645. }
  58646. #endif /* !defined(NO_OLD_TLS) */
  58647. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58648. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
  58649. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58650. !defined(NO_OLD_TLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  58651. static int test_WOLFSSL_dtls_version_alert(void)
  58652. {
  58653. EXPECT_DECLS;
  58654. struct test_memio_ctx test_ctx;
  58655. WOLFSSL_CTX *ctx_c = NULL;
  58656. WOLFSSL_CTX *ctx_s = NULL;
  58657. WOLFSSL *ssl_c = NULL;
  58658. WOLFSSL *ssl_s = NULL;
  58659. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58660. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58661. wolfDTLSv1_2_client_method, wolfDTLSv1_server_method), 0);
  58662. /* client hello */
  58663. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  58664. (ssl_c->error == WANT_READ));
  58665. /* hrr */
  58666. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58667. (ssl_s->error == WANT_READ));
  58668. /* client hello 1 */
  58669. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  58670. (ssl_c->error == WANT_READ));
  58671. /* server hello */
  58672. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58673. (ssl_s->error == WANT_READ));
  58674. /* should fail */
  58675. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  58676. (ssl_c->error == VERSION_ERROR));
  58677. /* shuould fail */
  58678. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58679. (ssl_s->error == VERSION_ERROR || ssl_s->error == FATAL_ERROR));
  58680. wolfSSL_free(ssl_c);
  58681. wolfSSL_free(ssl_s);
  58682. wolfSSL_CTX_free(ctx_c);
  58683. wolfSSL_CTX_free(ctx_s);
  58684. return EXPECT_RESULT();
  58685. }
  58686. #else
  58687. static int test_WOLFSSL_dtls_version_alert(void)
  58688. {
  58689. return TEST_SKIPPED;
  58690. }
  58691. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
  58692. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
  58693. * !defined(NO_OLD_TLS) && !defined(NO_RSA)
  58694. */
  58695. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  58696. && defined(WOLFSSL_TLS13) && \
  58697. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))\
  58698. && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  58699. static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
  58700. {
  58701. struct test_memio_ctx *test_ctx;
  58702. byte buf[2048];
  58703. int idx, sz;
  58704. word32 tmp;
  58705. int ret;
  58706. idx = 5; /* space for record header */
  58707. buf[idx] = session_ticket; /* type */
  58708. idx++;
  58709. tmp = OPAQUE32_LEN +
  58710. OPAQUE32_LEN +
  58711. OPAQUE8_LEN + nonceLength +
  58712. OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
  58713. c32to24(tmp, buf + idx);
  58714. idx += OPAQUE24_LEN;
  58715. c32toa((word32)12345, buf+idx); /* lifetime */
  58716. idx += OPAQUE32_LEN;
  58717. c32toa((word32)12345, buf+idx); /* add */
  58718. idx += OPAQUE32_LEN;
  58719. buf[idx] = nonceLength; /* nonce length */
  58720. idx++;
  58721. XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
  58722. idx += nonceLength;
  58723. tmp = 1; /* ticket len */
  58724. c16toa((word16)tmp, buf+idx);
  58725. idx += 2;
  58726. buf[idx] = 0xFF; /* ticket */
  58727. idx++;
  58728. tmp = 0; /* ext len */
  58729. c16toa((word16)tmp, buf+idx);
  58730. idx += 2;
  58731. sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
  58732. handshake, 0, 0, 0);
  58733. test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
  58734. ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
  58735. return !(ret == sz);
  58736. }
  58737. static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
  58738. {
  58739. int ret = 0;
  58740. if ((sess == NULL) || (sess->ticketNonce.len != len)) {
  58741. ret = -1;
  58742. }
  58743. else {
  58744. int i;
  58745. for (i = 0; i < len; i++) {
  58746. if (sess->ticketNonce.data[i] != len) {
  58747. ret = -1;
  58748. break;
  58749. }
  58750. }
  58751. }
  58752. return ret;
  58753. }
  58754. static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  58755. {
  58756. EXPECT_DECLS;
  58757. char *buf[1024];
  58758. ExpectIntEQ(send_new_session_ticket(ssl_s, len, len), 0);
  58759. ExpectTrue((wolfSSL_recv(ssl_c, buf, 1024, 0) == WOLFSSL_FATAL_ERROR) &&
  58760. (ssl_c->error == WANT_READ));
  58761. ExpectIntEQ(test_ticket_nonce_check(ssl_c->session, len), 0);
  58762. return EXPECT_RESULT();
  58763. }
  58764. static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  58765. {
  58766. EXPECT_DECLS;
  58767. WOLFSSL_SESSION *sess = NULL;
  58768. WOLFSSL_SESSION *cached = NULL;
  58769. WOLFSSL_CTX *ctx = ssl_c->ctx;
  58770. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, len), TEST_SUCCESS);
  58771. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  58772. ExpectIntEQ(AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
  58773. NULL, ssl_c->options.side, 1,NULL), 0);
  58774. ExpectNotNull(cached = wolfSSL_SESSION_new());
  58775. ExpectIntEQ(wolfSSL_GetSessionFromCache(ssl_c, cached), WOLFSSL_SUCCESS);
  58776. ExpectIntEQ(test_ticket_nonce_check(cached, len), 0);
  58777. wolfSSL_SESSION_free(cached);
  58778. wolfSSL_SESSION_free(sess);
  58779. return EXPECT_RESULT();
  58780. }
  58781. static int test_ticket_nonce_malloc(void)
  58782. {
  58783. EXPECT_DECLS;
  58784. struct test_memio_ctx test_ctx;
  58785. WOLFSSL_CTX *ctx_c = NULL;
  58786. WOLFSSL_CTX *ctx_s = NULL;
  58787. WOLFSSL *ssl_c = NULL;
  58788. WOLFSSL *ssl_s = NULL;
  58789. byte small;
  58790. byte medium;
  58791. byte big;
  58792. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58793. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58794. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  58795. /* will send ticket manually */
  58796. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(ssl_s), 0);
  58797. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  58798. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  58799. while (EXPECT_SUCCESS() && (ssl_c->options.handShakeDone == 0) &&
  58800. (ssl_s->options.handShakeDone == 0)) {
  58801. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_SUCCESS) ||
  58802. (ssl_c->error == WANT_READ));
  58803. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_SUCCESS) ||
  58804. (ssl_s->error == WANT_READ));
  58805. }
  58806. small = TLS13_TICKET_NONCE_STATIC_SZ;
  58807. medium = small + 20 <= 255 ? small + 20 : 255;
  58808. big = medium + 20 <= 255 ? small + 20 : 255;
  58809. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  58810. ExpectPtrEq(ssl_c->session->ticketNonce.data,
  58811. ssl_c->session->ticketNonce.dataStatic);
  58812. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  58813. TEST_SUCCESS);
  58814. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, big), TEST_SUCCESS);
  58815. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  58816. TEST_SUCCESS);
  58817. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  58818. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  58819. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  58820. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, big), TEST_SUCCESS);
  58821. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  58822. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  58823. wolfSSL_free(ssl_c);
  58824. wolfSSL_free(ssl_s);
  58825. wolfSSL_CTX_free(ctx_c);
  58826. wolfSSL_CTX_free(ctx_s);
  58827. return EXPECT_RESULT();
  58828. }
  58829. #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
  58830. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_TLS12) && \
  58831. !defined(WOLFSSL_TICKET_DECRYPT_NO_CREATE) && \
  58832. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  58833. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_RSA) && \
  58834. defined(HAVE_ECC) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  58835. static int test_ticket_ret_create(void)
  58836. {
  58837. EXPECT_DECLS;
  58838. WOLFSSL_CTX *ctx_c = NULL;
  58839. WOLFSSL_CTX *ctx_s = NULL;
  58840. WOLFSSL *ssl_c = NULL;
  58841. WOLFSSL *ssl_s = NULL;
  58842. byte ticket[SESSION_TICKET_LEN];
  58843. struct test_memio_ctx test_ctx;
  58844. WOLFSSL_SESSION *sess = NULL;
  58845. word16 ticketLen = 0;
  58846. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58847. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58848. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  58849. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  58850. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  58851. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx_c), WOLFSSL_SUCCESS);
  58852. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58853. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  58854. ExpectIntLE(sess->ticketLen, SESSION_TICKET_LEN);
  58855. if (sess != NULL) {
  58856. ticketLen = sess->ticketLen;
  58857. XMEMCPY(ticket, sess->ticket, sess->ticketLen);
  58858. }
  58859. wolfSSL_free(ssl_c);
  58860. ssl_c = NULL;
  58861. wolfSSL_free(ssl_s);
  58862. ssl_s = NULL;
  58863. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  58864. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  58865. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  58866. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  58867. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  58868. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  58869. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  58870. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58871. ExpectIntLE(ssl_c->session->ticketLen, SESSION_TICKET_LEN);
  58872. ExpectIntEQ(ssl_c->session->ticketLen, ticketLen);
  58873. ExpectTrue(XMEMCMP(ssl_c->session->ticket, ticket, ticketLen) != 0);
  58874. wolfSSL_SESSION_free(sess);
  58875. wolfSSL_free(ssl_c);
  58876. wolfSSL_free(ssl_s);
  58877. wolfSSL_CTX_free(ctx_c);
  58878. wolfSSL_CTX_free(ctx_s);
  58879. return EXPECT_RESULT();
  58880. }
  58881. #else
  58882. static int test_ticket_ret_create(void)
  58883. {
  58884. return TEST_SKIPPED;
  58885. }
  58886. #endif
  58887. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && \
  58888. defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  58889. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  58890. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  58891. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  58892. static void test_ticket_and_psk_mixing_on_result(WOLFSSL* ssl)
  58893. {
  58894. int ret;
  58895. WOLFSSL_SESSION* session = NULL;
  58896. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  58897. if (!wolfSSL_is_server(ssl)) {
  58898. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  58899. AssertNotNull(session);
  58900. }
  58901. do {
  58902. ret = wolfSSL_shutdown(ssl);
  58903. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  58904. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  58905. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  58906. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  58907. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  58908. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  58909. #endif
  58910. if (!wolfSSL_is_server(ssl)) {
  58911. /* client */
  58912. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  58913. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  58914. wolfSSL_set_session(ssl, session);
  58915. wolfSSL_SESSION_free(session);
  58916. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  58917. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  58918. }
  58919. else {
  58920. /* server */
  58921. /* Different ciphersuite so that the ticket will be invalidated based on
  58922. * the ciphersuite */
  58923. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"),
  58924. WOLFSSL_SUCCESS);
  58925. wolfSSL_set_psk_server_tls13_callback(ssl, my_psk_server_tls13_cb);
  58926. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  58927. }
  58928. }
  58929. static void test_ticket_and_psk_mixing_ssl_ready(WOLFSSL* ssl)
  58930. {
  58931. AssertIntEQ(wolfSSL_UseSessionTicket(ssl), WOLFSSL_SUCCESS);
  58932. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  58933. WOLFSSL_SUCCESS);
  58934. }
  58935. static int test_ticket_and_psk_mixing(void)
  58936. {
  58937. EXPECT_DECLS;
  58938. /* Test mixing tickets and regular PSK */
  58939. callback_functions client_cbs, server_cbs;
  58940. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  58941. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  58942. client_cbs.method = wolfTLSv1_3_client_method;
  58943. server_cbs.method = wolfTLSv1_3_server_method;
  58944. client_cbs.ssl_ready = test_ticket_and_psk_mixing_ssl_ready;
  58945. client_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  58946. server_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  58947. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  58948. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  58949. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  58950. return EXPECT_RESULT();
  58951. }
  58952. #else
  58953. static int test_ticket_and_psk_mixing(void)
  58954. {
  58955. return TEST_SKIPPED;
  58956. }
  58957. #endif
  58958. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  58959. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  58960. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  58961. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  58962. static int test_prioritize_psk_cb_called = FALSE;
  58963. static unsigned int test_prioritize_psk_cb(WOLFSSL* ssl,
  58964. const char* identity, unsigned char* key, unsigned int key_max_len,
  58965. const char** ciphersuite)
  58966. {
  58967. test_prioritize_psk_cb_called = TRUE;
  58968. return my_psk_server_tls13_cb(ssl, identity, key, key_max_len, ciphersuite);
  58969. }
  58970. static void test_prioritize_psk_on_result(WOLFSSL* ssl)
  58971. {
  58972. int ret;
  58973. WOLFSSL_SESSION* session = NULL;
  58974. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  58975. if (!wolfSSL_is_server(ssl)) {
  58976. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  58977. AssertNotNull(session);
  58978. }
  58979. do {
  58980. ret = wolfSSL_shutdown(ssl);
  58981. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  58982. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  58983. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  58984. /* Previous connection was made with TLS13-AES128-GCM-SHA256. Order is
  58985. * important. */
  58986. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  58987. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  58988. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  58989. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  58990. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  58991. #endif
  58992. if (!wolfSSL_is_server(ssl)) {
  58993. /* client */
  58994. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  58995. wolfSSL_set_session(ssl, session);
  58996. wolfSSL_SESSION_free(session);
  58997. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  58998. }
  58999. else {
  59000. /* server */
  59001. wolfSSL_set_psk_server_tls13_callback(ssl, test_prioritize_psk_cb);
  59002. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  59003. #ifdef WOLFSSL_PRIORITIZE_PSK
  59004. /* The ticket should be first tried with all ciphersuites and chosen */
  59005. AssertFalse(test_prioritize_psk_cb_called);
  59006. #else
  59007. /* Ciphersuites should be tried with each PSK. This triggers the PSK
  59008. * callback that sets this var. */
  59009. AssertTrue(test_prioritize_psk_cb_called);
  59010. #endif
  59011. }
  59012. }
  59013. static void test_prioritize_psk_ssl_ready(WOLFSSL* ssl)
  59014. {
  59015. if (!wolfSSL_is_server(ssl))
  59016. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  59017. WOLFSSL_SUCCESS);
  59018. else
  59019. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  59020. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  59021. }
  59022. static int test_prioritize_psk(void)
  59023. {
  59024. EXPECT_DECLS;
  59025. /* We always send the ticket first. With WOLFSSL_PRIORITIZE_PSK the order
  59026. * of the PSK's will be followed instead of the ciphersuite. */
  59027. callback_functions client_cbs, server_cbs;
  59028. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59029. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59030. client_cbs.method = wolfTLSv1_3_client_method;
  59031. server_cbs.method = wolfTLSv1_3_server_method;
  59032. client_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  59033. server_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  59034. client_cbs.on_result = test_prioritize_psk_on_result;
  59035. server_cbs.on_result = test_prioritize_psk_on_result;
  59036. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  59037. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  59038. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  59039. return EXPECT_RESULT();
  59040. }
  59041. #else
  59042. static int test_prioritize_psk(void)
  59043. {
  59044. return TEST_SKIPPED;
  59045. }
  59046. #endif
  59047. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  59048. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  59049. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  59050. !defined(WOLFSSL_NO_TLS12)
  59051. static int test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server(WOLFSSL_CTX* ctx)
  59052. {
  59053. EXPECT_DECLS;
  59054. ExpectTrue(SSL_CTX_set_cipher_list(ctx, "DEFAULT"));
  59055. /* Set TLS 1.3 specific suite */
  59056. ExpectTrue(SSL_CTX_set_ciphersuites(ctx, "TLS13-AES128-GCM-SHA256"));
  59057. return EXPECT_RESULT();
  59058. }
  59059. static int test_wolfSSL_CTX_set_ciphersuites(void)
  59060. {
  59061. EXPECT_DECLS;
  59062. /* Test using SSL_CTX_set_cipher_list and SSL_CTX_set_ciphersuites and then
  59063. * do a 1.2 connection. */
  59064. test_ssl_cbf client_cbs, server_cbs;
  59065. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59066. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59067. client_cbs.method = wolfTLSv1_2_client_method;
  59068. server_cbs.method = wolfTLS_server_method; /* Allow downgrade */
  59069. server_cbs.ctx_ready = test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server;
  59070. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  59071. &server_cbs, NULL), TEST_SUCCESS);
  59072. return EXPECT_RESULT();
  59073. }
  59074. #else
  59075. static int test_wolfSSL_CTX_set_ciphersuites(void)
  59076. {
  59077. return TEST_SKIPPED;
  59078. }
  59079. #endif
  59080. #if defined(HAVE_CRL) && defined(WOLFSSL_CHECK_ALERT_ON_ERR) && \
  59081. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  59082. static int test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready(WOLFSSL_CTX* ctx)
  59083. {
  59084. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  59085. return TEST_SUCCESS;
  59086. }
  59087. static int test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup(WOLFSSL* ssl)
  59088. {
  59089. EXPECT_DECLS;
  59090. WOLFSSL_ALERT_HISTORY h;
  59091. ExpectIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  59092. ExpectIntEQ(h.last_rx.level, alert_fatal);
  59093. ExpectIntEQ(h.last_rx.code, certificate_revoked);
  59094. return EXPECT_RESULT();
  59095. }
  59096. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  59097. {
  59098. EXPECT_DECLS;
  59099. test_ssl_cbf client_cbs, server_cbs;
  59100. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59101. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59102. server_cbs.certPemFile = "./certs/server-revoked-cert.pem";
  59103. server_cbs.keyPemFile = "./certs/server-revoked-key.pem";
  59104. client_cbs.crlPemFile = "./certs/crl/crl.revoked";
  59105. client_cbs.ctx_ready = test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready;
  59106. server_cbs.on_cleanup = test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup;
  59107. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  59108. &server_cbs, NULL), TEST_FAIL);
  59109. return EXPECT_RESULT();
  59110. }
  59111. #else
  59112. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  59113. {
  59114. return TEST_SKIPPED;
  59115. }
  59116. #endif
  59117. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET) \
  59118. && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  59119. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  59120. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  59121. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  59122. static WOLFSSL_CTX* test_TLS_13_ticket_different_ciphers_ctx = NULL;
  59123. static WOLFSSL_SESSION* test_TLS_13_ticket_different_ciphers_session = NULL;
  59124. static int test_TLS_13_ticket_different_ciphers_run = 0;
  59125. static int test_TLS_13_ticket_different_ciphers_ssl_ready(WOLFSSL* ssl)
  59126. {
  59127. EXPECT_DECLS;
  59128. switch (test_TLS_13_ticket_different_ciphers_run) {
  59129. case 0:
  59130. /* First run */
  59131. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  59132. WOLFSSL_SUCCESS);
  59133. if (wolfSSL_is_server(ssl)) {
  59134. ExpectNotNull(test_TLS_13_ticket_different_ciphers_ctx =
  59135. wolfSSL_get_SSL_CTX(ssl));
  59136. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_up_ref(
  59137. test_TLS_13_ticket_different_ciphers_ctx));
  59138. }
  59139. break;
  59140. case 1:
  59141. /* Second run */
  59142. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  59143. "TLS13-AES128-GCM-SHA256"),
  59144. WOLFSSL_SUCCESS);
  59145. if (!wolfSSL_is_server(ssl)) {
  59146. ExpectIntEQ(wolfSSL_set_session(ssl,
  59147. test_TLS_13_ticket_different_ciphers_session),
  59148. WOLFSSL_SUCCESS);
  59149. }
  59150. break;
  59151. default:
  59152. /* Bad state? */
  59153. Fail(("Should not enter here"), ("Should not enter here"));
  59154. }
  59155. return EXPECT_RESULT();
  59156. }
  59157. static int test_TLS_13_ticket_different_ciphers_on_result(WOLFSSL* ssl)
  59158. {
  59159. EXPECT_DECLS;
  59160. switch (test_TLS_13_ticket_different_ciphers_run) {
  59161. case 0:
  59162. /* First run */
  59163. ExpectNotNull(test_TLS_13_ticket_different_ciphers_session =
  59164. wolfSSL_get1_session(ssl));
  59165. break;
  59166. case 1:
  59167. /* Second run */
  59168. ExpectTrue(wolfSSL_session_reused(ssl));
  59169. break;
  59170. default:
  59171. /* Bad state? */
  59172. Fail(("Should not enter here"), ("Should not enter here"));
  59173. }
  59174. return EXPECT_RESULT();
  59175. }
  59176. static int test_TLS_13_ticket_different_ciphers(void)
  59177. {
  59178. EXPECT_DECLS;
  59179. /* Check that we handle the connection when the ticket doesn't match
  59180. * the first ciphersuite. */
  59181. test_ssl_cbf client_cbs, server_cbs;
  59182. struct test_params {
  59183. method_provider client_meth;
  59184. method_provider server_meth;
  59185. int doUdp;
  59186. } params[] = {
  59187. #ifdef WOLFSSL_DTLS13
  59188. /* Test that the stateless code handles sessions correctly */
  59189. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, 1},
  59190. #endif
  59191. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, 0},
  59192. };
  59193. size_t i;
  59194. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  59195. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59196. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59197. test_TLS_13_ticket_different_ciphers_run = 0;
  59198. client_cbs.doUdp = server_cbs.doUdp = params[i].doUdp;
  59199. client_cbs.method = params[i].client_meth;
  59200. server_cbs.method = params[i].server_meth;
  59201. client_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  59202. server_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  59203. client_cbs.on_result = test_TLS_13_ticket_different_ciphers_on_result;
  59204. server_cbs.ticNoInit = 1;
  59205. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  59206. &server_cbs, NULL), TEST_SUCCESS);
  59207. test_TLS_13_ticket_different_ciphers_run++;
  59208. server_cbs.ctx = test_TLS_13_ticket_different_ciphers_ctx;
  59209. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  59210. &server_cbs, NULL), TEST_SUCCESS);
  59211. wolfSSL_SESSION_free(test_TLS_13_ticket_different_ciphers_session);
  59212. test_TLS_13_ticket_different_ciphers_session = NULL;
  59213. wolfSSL_CTX_free(test_TLS_13_ticket_different_ciphers_ctx);
  59214. test_TLS_13_ticket_different_ciphers_ctx = NULL;
  59215. }
  59216. return EXPECT_RESULT();
  59217. }
  59218. #else
  59219. static int test_TLS_13_ticket_different_ciphers(void)
  59220. {
  59221. return TEST_SKIPPED;
  59222. }
  59223. #endif
  59224. #if defined(WOLFSSL_EXTRA_ALERTS) && !defined(WOLFSSL_NO_TLS12) && \
  59225. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  59226. #define TEST_WRONG_CS_CLIENT "DHE-RSA-AES128-SHA"
  59227. /* AKA TLS_DHE_RSA_WITH_AES_128_CBC_SHA */
  59228. byte test_extra_alerts_wrong_cs_sh[] = {
  59229. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0xef,
  59230. 0x0c, 0x30, 0x98, 0xa2, 0xac, 0xfa, 0x68, 0xe9, 0x3e, 0xaa, 0x5c, 0xcf,
  59231. 0xa7, 0x42, 0x72, 0xaf, 0xa0, 0xe8, 0x39, 0x2b, 0x3e, 0x81, 0xa7, 0x7a,
  59232. 0xa5, 0x62, 0x8a, 0x0e, 0x41, 0xba, 0xda, 0x20, 0x18, 0x9f, 0xe1, 0x8c,
  59233. 0x1d, 0xc0, 0x37, 0x9c, 0xf4, 0x90, 0x5d, 0x8d, 0xa0, 0x79, 0xa7, 0x4b,
  59234. 0xa8, 0x79, 0xdf, 0xcd, 0x8d, 0xf5, 0xb5, 0x50, 0x5f, 0xf1, 0xdb, 0x4d,
  59235. 0xbb, 0x07, 0x54, 0x1c,
  59236. 0x00, 0x02, /* TLS_RSA_WITH_NULL_SHA */
  59237. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  59238. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  59239. };
  59240. static int test_extra_alerts_wrong_cs(void)
  59241. {
  59242. EXPECT_DECLS;
  59243. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
  59244. struct test_memio_ctx test_ctx;
  59245. WOLFSSL_CTX *ctx_c = NULL;
  59246. WOLFSSL_ALERT_HISTORY h;
  59247. WOLFSSL *ssl_c = NULL;
  59248. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59249. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  59250. wolfTLSv1_2_client_method, NULL), 0);
  59251. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, TEST_WRONG_CS_CLIENT),
  59252. WOLFSSL_SUCCESS);
  59253. /* CH */
  59254. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59255. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59256. WOLFSSL_ERROR_WANT_READ);
  59257. /* consume CH */
  59258. test_ctx.s_len = 0;
  59259. /* inject SH */
  59260. XMEMCPY(test_ctx.c_buff, test_extra_alerts_wrong_cs_sh,
  59261. sizeof(test_extra_alerts_wrong_cs_sh));
  59262. test_ctx.c_len = sizeof(test_extra_alerts_wrong_cs_sh);
  59263. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59264. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59265. WOLFSSL_ERROR_WANT_READ);
  59266. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  59267. ExpectIntEQ(h.last_tx.code, handshake_failure);
  59268. ExpectIntEQ(h.last_tx.level, alert_fatal);
  59269. wolfSSL_free(ssl_c);
  59270. wolfSSL_CTX_free(ctx_c);
  59271. #endif
  59272. return EXPECT_RESULT();
  59273. }
  59274. #else
  59275. static int test_extra_alerts_wrong_cs(void)
  59276. {
  59277. return TEST_SKIPPED;
  59278. }
  59279. #endif
  59280. #if !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_EXTRA_ALERTS) && \
  59281. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_SP_MATH)
  59282. static void test_remove_msg(byte *msg, int tail_len, int *len, int msg_length)
  59283. {
  59284. tail_len -= msg_length;
  59285. XMEMMOVE(msg, msg + msg_length, tail_len);
  59286. *len = *len - msg_length;
  59287. }
  59288. static int test_remove_hs_msg_from_buffer(byte *buf, int *len, byte type,
  59289. byte *found)
  59290. {
  59291. const unsigned int _HANDSHAKE_HEADER_SZ = 4;
  59292. const unsigned int _RECORD_HEADER_SZ = 5;
  59293. const int _change_cipher_hs = 55;
  59294. const int _change_cipher = 20;
  59295. const int _handshake = 22;
  59296. unsigned int tail_len;
  59297. byte *idx, *curr;
  59298. word8 currType;
  59299. word16 rLength;
  59300. word32 hLength;
  59301. idx = buf;
  59302. tail_len = *len;
  59303. *found = 0;
  59304. while (tail_len > _RECORD_HEADER_SZ) {
  59305. curr = idx;
  59306. currType = *idx;
  59307. ato16(idx + 3, &rLength);
  59308. idx += _RECORD_HEADER_SZ;
  59309. tail_len -= _RECORD_HEADER_SZ;
  59310. if (tail_len < rLength)
  59311. return -1;
  59312. if (type == _change_cipher_hs && currType == _change_cipher) {
  59313. if (rLength != 1)
  59314. return -1;
  59315. /* match */
  59316. test_remove_msg(curr, *len - (int)(curr - buf),
  59317. len, _RECORD_HEADER_SZ + 1);
  59318. *found = 1;
  59319. return 0;
  59320. }
  59321. if (currType != _handshake) {
  59322. idx += rLength;
  59323. tail_len -= rLength;
  59324. continue;
  59325. }
  59326. if (rLength < _HANDSHAKE_HEADER_SZ)
  59327. return -1;
  59328. currType = *idx;
  59329. ato24(idx+1, &hLength);
  59330. hLength += _HANDSHAKE_HEADER_SZ;
  59331. if (tail_len < hLength)
  59332. return -1;
  59333. if (currType != type) {
  59334. idx += hLength;
  59335. tail_len -= hLength;
  59336. continue;
  59337. }
  59338. /* match */
  59339. test_remove_msg(curr, *len - (int)(curr - buf), len,
  59340. hLength + _RECORD_HEADER_SZ);
  59341. *found = 1;
  59342. return 0;
  59343. }
  59344. /* not found */
  59345. return 0;
  59346. }
  59347. static int test_remove_hs_message(byte hs_message_type,
  59348. int extra_round, byte alert_type)
  59349. {
  59350. EXPECT_DECLS;
  59351. WOLFSSL_CTX *ctx_c = NULL;
  59352. WOLFSSL_CTX *ctx_s = NULL;
  59353. WOLFSSL *ssl_c = NULL;
  59354. WOLFSSL *ssl_s = NULL;
  59355. struct test_memio_ctx test_ctx;
  59356. WOLFSSL_ALERT_HISTORY h;
  59357. byte found = 0;
  59358. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59359. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59360. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  59361. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59362. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59363. WOLFSSL_ERROR_WANT_READ);
  59364. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  59365. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59366. WOLFSSL_ERROR_WANT_READ);
  59367. if (extra_round) {
  59368. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59369. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59370. WOLFSSL_ERROR_WANT_READ);
  59371. /* this will complete handshake from server side */
  59372. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  59373. }
  59374. ExpectIntEQ(test_remove_hs_msg_from_buffer(test_ctx.c_buff,
  59375. &test_ctx.c_len, hs_message_type, &found), 0);
  59376. if (!found) {
  59377. wolfSSL_free(ssl_c);
  59378. wolfSSL_CTX_free(ctx_c);
  59379. wolfSSL_free(ssl_s);
  59380. wolfSSL_CTX_free(ctx_s);
  59381. return TEST_SKIPPED;
  59382. }
  59383. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59384. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59385. WOLFSSL_ERROR_WANT_READ);
  59386. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  59387. ExpectTrue(alert_type == 0xff || h.last_tx.code == alert_type);
  59388. ExpectIntEQ(h.last_tx.level, alert_fatal);
  59389. wolfSSL_free(ssl_c);
  59390. wolfSSL_CTX_free(ctx_c);
  59391. wolfSSL_free(ssl_s);
  59392. wolfSSL_CTX_free(ctx_s);
  59393. return EXPECT_RESULT();
  59394. }
  59395. static int test_extra_alerts_skip_hs(void)
  59396. {
  59397. EXPECT_DECLS;
  59398. const byte _server_key_exchange = 12;
  59399. const byte _server_hello = 2;
  59400. const byte _certificate = 11;
  59401. /* server_hello */
  59402. ExpectIntNE(test_remove_hs_message(_server_hello, 0,
  59403. unexpected_message), TEST_FAIL);
  59404. ExpectIntNE(test_remove_hs_message(_certificate, 0,
  59405. 0xff), TEST_FAIL);
  59406. ExpectIntNE(test_remove_hs_message(_server_key_exchange, 0,
  59407. unexpected_message), TEST_FAIL);
  59408. return EXPECT_RESULT();
  59409. }
  59410. #else
  59411. static int test_extra_alerts_skip_hs(void)
  59412. {
  59413. return TEST_SKIPPED;
  59414. }
  59415. #endif
  59416. #if !defined(WOLFSSL_NO_TLS12) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  59417. && defined(WOLFSSL_EXTRA_ALERTS) && !defined(NO_PSK) && !defined(NO_DH)
  59418. static unsigned int test_server_psk_cb(WOLFSSL* ssl, const char* id,
  59419. unsigned char* key, unsigned int key_max_len)
  59420. {
  59421. (void)ssl;
  59422. (void)id;
  59423. (void)key_max_len;
  59424. /* zero means error */
  59425. key[0] = 0x10;
  59426. return 1;
  59427. }
  59428. static int test_extra_alerts_bad_psk(void)
  59429. {
  59430. EXPECT_DECLS;
  59431. WOLFSSL_CTX *ctx_c = NULL;
  59432. WOLFSSL_CTX *ctx_s = NULL;
  59433. WOLFSSL *ssl_c = NULL;
  59434. WOLFSSL *ssl_s = NULL;
  59435. struct test_memio_ctx test_ctx;
  59436. WOLFSSL_ALERT_HISTORY h;
  59437. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59438. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59439. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  59440. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, "DHE-PSK-AES128-GCM-SHA256"),
  59441. WOLFSSL_SUCCESS);
  59442. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_s, "DHE-PSK-AES128-GCM-SHA256"),
  59443. WOLFSSL_SUCCESS);
  59444. wolfSSL_set_psk_server_callback(ssl_s, test_server_psk_cb);
  59445. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59446. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59447. WOLFSSL_ERROR_WANT_READ);
  59448. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  59449. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  59450. WOLFSSL_ERROR_WANT_READ);
  59451. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59452. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59453. WOLFSSL_ERROR_WANT_READ);
  59454. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  59455. ExpectIntEQ(h.last_tx.code, handshake_failure);
  59456. ExpectIntEQ(h.last_tx.level, alert_fatal);
  59457. wolfSSL_free(ssl_c);
  59458. wolfSSL_CTX_free(ctx_c);
  59459. wolfSSL_free(ssl_s);
  59460. wolfSSL_CTX_free(ctx_s);
  59461. return EXPECT_RESULT();
  59462. }
  59463. #else
  59464. static int test_extra_alerts_bad_psk(void)
  59465. {
  59466. return TEST_SKIPPED;
  59467. }
  59468. #endif
  59469. #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  59470. && !defined(NO_PSK)
  59471. static unsigned int test_tls13_bad_psk_binder_client_cb(WOLFSSL* ssl,
  59472. const char* hint, char* identity, unsigned int id_max_len,
  59473. unsigned char* key, unsigned int key_max_len)
  59474. {
  59475. (void)ssl;
  59476. (void)hint;
  59477. (void)key_max_len;
  59478. /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */
  59479. XSTRNCPY(identity, "Client_identity", id_max_len);
  59480. key[0] = 0x20;
  59481. return 1;
  59482. }
  59483. static unsigned int test_tls13_bad_psk_binder_server_cb(WOLFSSL* ssl,
  59484. const char* id, unsigned char* key, unsigned int key_max_len)
  59485. {
  59486. (void)ssl;
  59487. (void)id;
  59488. (void)key_max_len;
  59489. /* zero means error */
  59490. key[0] = 0x10;
  59491. return 1;
  59492. }
  59493. #endif
  59494. static int test_tls13_bad_psk_binder(void)
  59495. {
  59496. EXPECT_DECLS;
  59497. #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  59498. && !defined(NO_PSK)
  59499. WOLFSSL_CTX *ctx_c = NULL;
  59500. WOLFSSL_CTX *ctx_s = NULL;
  59501. WOLFSSL *ssl_c = NULL;
  59502. WOLFSSL *ssl_s = NULL;
  59503. struct test_memio_ctx test_ctx;
  59504. WOLFSSL_ALERT_HISTORY h;
  59505. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59506. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59507. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  59508. wolfSSL_set_psk_client_callback(ssl_c, test_tls13_bad_psk_binder_client_cb);
  59509. wolfSSL_set_psk_server_callback(ssl_s, test_tls13_bad_psk_binder_server_cb);
  59510. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59511. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59512. WOLFSSL_ERROR_WANT_READ);
  59513. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  59514. ExpectIntEQ( wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  59515. BAD_BINDER);
  59516. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59517. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59518. FATAL_ERROR);
  59519. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  59520. ExpectIntEQ(h.last_rx.code, illegal_parameter);
  59521. ExpectIntEQ(h.last_rx.level, alert_fatal);
  59522. wolfSSL_free(ssl_c);
  59523. wolfSSL_CTX_free(ctx_c);
  59524. wolfSSL_free(ssl_s);
  59525. wolfSSL_CTX_free(ctx_s);
  59526. #endif
  59527. return EXPECT_RESULT();
  59528. }
  59529. #if defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  59530. defined(HAVE_IO_TESTS_DEPENDENCIES)
  59531. static int test_harden_no_secure_renegotiation_io_cb(WOLFSSL *ssl, char *buf,
  59532. int sz, void *ctx)
  59533. {
  59534. static int sentServerHello = FALSE;
  59535. if (!sentServerHello) {
  59536. byte renegExt[] = { 0xFF, 0x01, 0x00, 0x01, 0x00 };
  59537. size_t i;
  59538. if (sz < (int)sizeof(renegExt))
  59539. return WOLFSSL_CBIO_ERR_GENERAL;
  59540. /* Remove SCR from ServerHello */
  59541. for (i = 0; i < sz - sizeof(renegExt); i++) {
  59542. if (XMEMCMP(buf + i, renegExt, sizeof(renegExt)) == 0) {
  59543. /* Found the extension. Change it to something unrecognized. */
  59544. buf[i+1] = 0x11;
  59545. break;
  59546. }
  59547. }
  59548. sentServerHello = TRUE;
  59549. }
  59550. return EmbedSend(ssl, buf, sz, ctx);
  59551. }
  59552. static void test_harden_no_secure_renegotiation_ssl_ready(WOLFSSL* ssl)
  59553. {
  59554. wolfSSL_SSLSetIOSend(ssl, test_harden_no_secure_renegotiation_io_cb);
  59555. }
  59556. static void test_harden_no_secure_renegotiation_on_cleanup(WOLFSSL* ssl)
  59557. {
  59558. WOLFSSL_ALERT_HISTORY h;
  59559. AssertIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  59560. AssertIntEQ(h.last_rx.code, handshake_failure);
  59561. AssertIntEQ(h.last_rx.level, alert_fatal);
  59562. }
  59563. static int test_harden_no_secure_renegotiation(void)
  59564. {
  59565. EXPECT_DECLS;
  59566. callback_functions client_cbs, server_cbs;
  59567. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59568. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59569. client_cbs.method = wolfTLSv1_2_client_method;
  59570. server_cbs.method = wolfTLSv1_2_server_method;
  59571. server_cbs.ssl_ready = test_harden_no_secure_renegotiation_ssl_ready;
  59572. server_cbs.on_cleanup = test_harden_no_secure_renegotiation_on_cleanup;
  59573. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  59574. ExpectIntEQ(client_cbs.return_code, TEST_FAIL);
  59575. ExpectIntEQ(client_cbs.last_err, SECURE_RENEGOTIATION_E);
  59576. ExpectIntEQ(server_cbs.return_code, TEST_FAIL);
  59577. ExpectTrue(server_cbs.last_err == SOCKET_ERROR_E ||
  59578. server_cbs.last_err == FATAL_ERROR);
  59579. return EXPECT_RESULT();
  59580. }
  59581. #else
  59582. static int test_harden_no_secure_renegotiation(void)
  59583. {
  59584. return TEST_SKIPPED;
  59585. }
  59586. #endif
  59587. #if defined(HAVE_OCSP) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  59588. static int test_override_alt_cert_chain_cert_cb(int preverify,
  59589. WOLFSSL_X509_STORE_CTX* store)
  59590. {
  59591. fprintf(stderr, "preverify: %d\n", preverify);
  59592. fprintf(stderr, "store->error: %d\n", store->error);
  59593. fprintf(stderr, "error reason: %s\n", wolfSSL_ERR_reason_error_string(store->error));
  59594. if (store->error == OCSP_INVALID_STATUS) {
  59595. fprintf(stderr, "Overriding OCSP error\n");
  59596. return 1;
  59597. }
  59598. #ifndef WOLFSSL_ALT_CERT_CHAINS
  59599. else if ((store->error == ASN_NO_SIGNER_E ||
  59600. store->error == ASN_SELF_SIGNED_E
  59601. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  59602. defined(HAVE_WEBSERVER)
  59603. || store->error == WOLFSSL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
  59604. #endif
  59605. ) && store->error_depth == store->totalCerts - 1) {
  59606. fprintf(stderr, "Overriding no signer error only for root cert\n");
  59607. return 1;
  59608. }
  59609. #endif
  59610. else
  59611. return preverify;
  59612. }
  59613. static int test_override_alt_cert_chain_ocsp_cb(void* ioCtx, const char* url,
  59614. int urlSz, unsigned char* request, int requestSz,
  59615. unsigned char** response)
  59616. {
  59617. (void)ioCtx;
  59618. (void)url;
  59619. (void)urlSz;
  59620. (void)request;
  59621. (void)requestSz;
  59622. (void)response;
  59623. return -1;
  59624. }
  59625. static int test_override_alt_cert_chain_client_ctx_ready(WOLFSSL_CTX* ctx)
  59626. {
  59627. EXPECT_DECLS;
  59628. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER,
  59629. test_override_alt_cert_chain_cert_cb);
  59630. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  59631. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  59632. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  59633. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  59634. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  59635. WOLFSSL_SUCCESS);
  59636. return EXPECT_RESULT();
  59637. }
  59638. static int test_override_alt_cert_chain_client_ctx_ready2(WOLFSSL_CTX* ctx)
  59639. {
  59640. EXPECT_DECLS;
  59641. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  59642. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  59643. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  59644. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  59645. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  59646. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  59647. WOLFSSL_SUCCESS);
  59648. return EXPECT_RESULT();
  59649. }
  59650. static int test_override_alt_cert_chain_server_ctx_ready(WOLFSSL_CTX* ctx)
  59651. {
  59652. EXPECT_DECLS;
  59653. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  59654. "./certs/intermediate/server-chain-alt.pem"), WOLFSSL_SUCCESS);
  59655. return EXPECT_RESULT();
  59656. }
  59657. static int test_override_alt_cert_chain(void)
  59658. {
  59659. EXPECT_DECLS;
  59660. size_t i;
  59661. struct test_params {
  59662. ctx_cb client_ctx_cb;
  59663. ctx_cb server_ctx_cb;
  59664. int result;
  59665. } params[] = {
  59666. {test_override_alt_cert_chain_client_ctx_ready,
  59667. test_override_alt_cert_chain_server_ctx_ready, TEST_SUCCESS},
  59668. {test_override_alt_cert_chain_client_ctx_ready2,
  59669. test_override_alt_cert_chain_server_ctx_ready, TEST_FAIL},
  59670. };
  59671. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  59672. test_ssl_cbf client_cbs, server_cbs;
  59673. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59674. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59675. fprintf(stderr, "test config: %d\n", (int)i);
  59676. client_cbs.ctx_ready = params[i].client_ctx_cb;
  59677. server_cbs.ctx_ready = params[i].server_ctx_cb;
  59678. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  59679. &server_cbs, NULL), params[i].result);
  59680. ExpectIntEQ(client_cbs.return_code, params[i].result);
  59681. ExpectIntEQ(server_cbs.return_code, params[i].result);
  59682. }
  59683. return EXPECT_RESULT();
  59684. }
  59685. #else
  59686. static int test_override_alt_cert_chain(void)
  59687. {
  59688. return TEST_SKIPPED;
  59689. }
  59690. #endif
  59691. #if defined(HAVE_RPK)
  59692. #define svrRpkCertFile "./certs/rpk/server-cert-rpk.der"
  59693. #define clntRpkCertFile "./certs/rpk/client-cert-rpk.der"
  59694. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  59695. static int MyRpkVerifyCb(int mode, WOLFSSL_X509_STORE_CTX* strctx)
  59696. {
  59697. int ret = WOLFSSL_SUCCESS;
  59698. (void)mode;
  59699. (void)strctx;
  59700. WOLFSSL_ENTER("MyRpkVerifyCb");
  59701. return ret;
  59702. }
  59703. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  59704. static WC_INLINE int test_rpk_memio_setup(
  59705. struct test_memio_ctx *ctx,
  59706. WOLFSSL_CTX **ctx_c,
  59707. WOLFSSL_CTX **ctx_s,
  59708. WOLFSSL **ssl_c,
  59709. WOLFSSL **ssl_s,
  59710. method_provider method_c,
  59711. method_provider method_s,
  59712. const char* certfile_c, int fmt_cc, /* client cert file path and format */
  59713. const char* certfile_s, int fmt_cs, /* server cert file path and format */
  59714. const char* pkey_c, int fmt_kc, /* client private key and format */
  59715. const char* pkey_s, int fmt_ks /* server private key and format */
  59716. )
  59717. {
  59718. int ret;
  59719. if (ctx_c != NULL && *ctx_c == NULL) {
  59720. *ctx_c = wolfSSL_CTX_new(method_c());
  59721. if (*ctx_c == NULL) {
  59722. return -1;
  59723. }
  59724. wolfSSL_CTX_set_verify(*ctx_c, WOLFSSL_VERIFY_PEER, NULL);
  59725. ret = wolfSSL_CTX_load_verify_locations(*ctx_c, caCertFile, 0);
  59726. if (ret != WOLFSSL_SUCCESS) {
  59727. return -1;
  59728. }
  59729. wolfSSL_SetIORecv(*ctx_c, test_memio_read_cb);
  59730. wolfSSL_SetIOSend(*ctx_c, test_memio_write_cb);
  59731. ret = wolfSSL_CTX_use_certificate_file(*ctx_c, certfile_c, fmt_cc);
  59732. if (ret != WOLFSSL_SUCCESS) {
  59733. return -1;
  59734. }
  59735. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_c, pkey_c, fmt_kc);
  59736. if (ret != WOLFSSL_SUCCESS) {
  59737. return -1;
  59738. }
  59739. }
  59740. if (ctx_s != NULL && *ctx_s == NULL) {
  59741. *ctx_s = wolfSSL_CTX_new(method_s());
  59742. if (*ctx_s == NULL) {
  59743. return -1;
  59744. }
  59745. wolfSSL_CTX_set_verify(*ctx_s, WOLFSSL_VERIFY_PEER, NULL);
  59746. ret = wolfSSL_CTX_load_verify_locations(*ctx_s, cliCertFile, 0);
  59747. if (ret != WOLFSSL_SUCCESS) {
  59748. return -1;
  59749. }
  59750. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_s, pkey_s, fmt_ks);
  59751. if (ret != WOLFSSL_SUCCESS) {
  59752. return -1;
  59753. }
  59754. ret = wolfSSL_CTX_use_certificate_file(*ctx_s, certfile_s, fmt_cs);
  59755. if (ret != WOLFSSL_SUCCESS) {
  59756. return -1;
  59757. }
  59758. wolfSSL_SetIORecv(*ctx_s, test_memio_read_cb);
  59759. wolfSSL_SetIOSend(*ctx_s, test_memio_write_cb);
  59760. if (ctx->s_ciphers != NULL) {
  59761. ret = wolfSSL_CTX_set_cipher_list(*ctx_s, ctx->s_ciphers);
  59762. if (ret != WOLFSSL_SUCCESS) {
  59763. return -1;
  59764. }
  59765. }
  59766. }
  59767. if (ctx_c != NULL && ssl_c != NULL) {
  59768. *ssl_c = wolfSSL_new(*ctx_c);
  59769. if (*ssl_c == NULL) {
  59770. return -1;
  59771. }
  59772. wolfSSL_SetIOWriteCtx(*ssl_c, ctx);
  59773. wolfSSL_SetIOReadCtx(*ssl_c, ctx);
  59774. }
  59775. if (ctx_s != NULL && ssl_s != NULL) {
  59776. *ssl_s = wolfSSL_new(*ctx_s);
  59777. if (*ssl_s == NULL) {
  59778. return -1;
  59779. }
  59780. wolfSSL_SetIOWriteCtx(*ssl_s, ctx);
  59781. wolfSSL_SetIOReadCtx(*ssl_s, ctx);
  59782. #if !defined(NO_DH)
  59783. SetDH(*ssl_s);
  59784. #endif
  59785. }
  59786. return 0;
  59787. }
  59788. #endif /* HAVE_RPK */
  59789. static int test_rpk_set_xxx_cert_type(void)
  59790. {
  59791. EXPECT_DECLS;
  59792. #if defined(HAVE_RPK)
  59793. char ctype[MAX_CLIENT_CERT_TYPE_CNT + 1]; /* prepare bigger buffer */
  59794. WOLFSSL_CTX* ctx = NULL;
  59795. WOLFSSL* ssl = NULL;
  59796. int tp;
  59797. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  59798. ExpectNotNull(ctx);
  59799. ssl = wolfSSL_new(ctx);
  59800. ExpectNotNull(ssl);
  59801. /*--------------------------------------------*/
  59802. /* tests for wolfSSL_CTX_set_client_cert_type */
  59803. /*--------------------------------------------*/
  59804. /* illegal parameter test caces */
  59805. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(NULL, ctype,
  59806. MAX_CLIENT_CERT_TYPE_CNT),
  59807. BAD_FUNC_ARG);
  59808. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59809. sizeof(ctype)),
  59810. BAD_FUNC_ARG);
  59811. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  59812. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  59813. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59814. MAX_CLIENT_CERT_TYPE_CNT),
  59815. BAD_FUNC_ARG);
  59816. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  59817. ctype[1] = 10; /* set unknown cert type */
  59818. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59819. MAX_CLIENT_CERT_TYPE_CNT),
  59820. BAD_FUNC_ARG);
  59821. /* pass larger type count */
  59822. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  59823. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  59824. ctype[2] = 1; /* pass unacceptable type count */
  59825. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59826. MAX_CLIENT_CERT_TYPE_CNT + 1),
  59827. BAD_FUNC_ARG);
  59828. /* should accept NULL for type buffer */
  59829. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, NULL,
  59830. MAX_CLIENT_CERT_TYPE_CNT),
  59831. WOLFSSL_SUCCESS);
  59832. /* should accept zero for type count */
  59833. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59834. 0),
  59835. WOLFSSL_SUCCESS);
  59836. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  59837. MAX_CLIENT_CERT_TYPE_CNT),
  59838. WOLFSSL_SUCCESS);
  59839. /*--------------------------------------------*/
  59840. /* tests for wolfSSL_CTX_set_server_cert_type */
  59841. /*--------------------------------------------*/
  59842. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(NULL, ctype,
  59843. MAX_SERVER_CERT_TYPE_CNT),
  59844. BAD_FUNC_ARG);
  59845. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59846. sizeof(ctype)),
  59847. BAD_FUNC_ARG);
  59848. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  59849. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  59850. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59851. MAX_SERVER_CERT_TYPE_CNT),
  59852. BAD_FUNC_ARG);
  59853. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  59854. ctype[1] = 10; /* set unknown cert type */
  59855. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59856. MAX_SERVER_CERT_TYPE_CNT),
  59857. BAD_FUNC_ARG);
  59858. /* pass larger type count */
  59859. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  59860. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  59861. ctype[2] = 1; /* pass unacceptable type count */
  59862. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59863. MAX_SERVER_CERT_TYPE_CNT + 1),
  59864. BAD_FUNC_ARG);
  59865. /* should accept NULL for type buffer */
  59866. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, NULL,
  59867. MAX_SERVER_CERT_TYPE_CNT),
  59868. WOLFSSL_SUCCESS);
  59869. /* should accept zero for type count */
  59870. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59871. 0),
  59872. WOLFSSL_SUCCESS);
  59873. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  59874. MAX_CLIENT_CERT_TYPE_CNT),
  59875. WOLFSSL_SUCCESS);
  59876. /*--------------------------------------------*/
  59877. /* tests for wolfSSL_set_client_cert_type */
  59878. /*--------------------------------------------*/
  59879. ExpectIntEQ(wolfSSL_set_client_cert_type(NULL, ctype,
  59880. MAX_CLIENT_CERT_TYPE_CNT),
  59881. BAD_FUNC_ARG);
  59882. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59883. sizeof(ctype)),
  59884. BAD_FUNC_ARG);
  59885. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  59886. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  59887. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59888. MAX_CLIENT_CERT_TYPE_CNT),
  59889. BAD_FUNC_ARG);
  59890. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  59891. ctype[1] = 10; /* set unknown cert type */
  59892. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59893. MAX_CLIENT_CERT_TYPE_CNT),
  59894. BAD_FUNC_ARG);
  59895. /* pass larger type count */
  59896. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  59897. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  59898. ctype[2] = 1; /* pass unacceptable type count */
  59899. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59900. MAX_CLIENT_CERT_TYPE_CNT + 1),
  59901. BAD_FUNC_ARG);
  59902. /* should accept NULL for type buffer */
  59903. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, NULL,
  59904. MAX_CLIENT_CERT_TYPE_CNT),
  59905. WOLFSSL_SUCCESS);
  59906. /* should accept zero for type count */
  59907. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59908. 0),
  59909. WOLFSSL_SUCCESS);
  59910. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  59911. MAX_CLIENT_CERT_TYPE_CNT),
  59912. WOLFSSL_SUCCESS);
  59913. /*--------------------------------------------*/
  59914. /* tests for wolfSSL_CTX_set_server_cert_type */
  59915. /*--------------------------------------------*/
  59916. ExpectIntEQ(wolfSSL_set_server_cert_type(NULL, ctype,
  59917. MAX_SERVER_CERT_TYPE_CNT),
  59918. BAD_FUNC_ARG);
  59919. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59920. sizeof(ctype)),
  59921. BAD_FUNC_ARG);
  59922. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  59923. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  59924. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59925. MAX_SERVER_CERT_TYPE_CNT),
  59926. BAD_FUNC_ARG);
  59927. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  59928. ctype[1] = 10; /* set unknown cert type */
  59929. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59930. MAX_SERVER_CERT_TYPE_CNT),
  59931. BAD_FUNC_ARG);
  59932. /* pass larger type count */
  59933. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  59934. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  59935. ctype[2] = 1; /* pass unacceptable type count */
  59936. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59937. MAX_SERVER_CERT_TYPE_CNT + 1),
  59938. BAD_FUNC_ARG);
  59939. /* should accept NULL for type buffer */
  59940. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, NULL,
  59941. MAX_SERVER_CERT_TYPE_CNT),
  59942. WOLFSSL_SUCCESS);
  59943. /* should accept zero for type count */
  59944. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59945. 0),
  59946. WOLFSSL_SUCCESS);
  59947. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  59948. MAX_SERVER_CERT_TYPE_CNT),
  59949. WOLFSSL_SUCCESS);
  59950. /*------------------------------------------------*/
  59951. /* tests for wolfSSL_get_negotiated_xxx_cert_type */
  59952. /*------------------------------------------------*/
  59953. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(NULL, &tp),
  59954. BAD_FUNC_ARG);
  59955. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl, NULL),
  59956. BAD_FUNC_ARG);
  59957. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(NULL, &tp),
  59958. BAD_FUNC_ARG);
  59959. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl, NULL),
  59960. BAD_FUNC_ARG);
  59961. /* clean up */
  59962. wolfSSL_free(ssl);
  59963. wolfSSL_CTX_free(ctx);
  59964. #endif
  59965. return EXPECT_RESULT();
  59966. }
  59967. static int test_tls13_rpk_handshake(void)
  59968. {
  59969. EXPECT_DECLS;
  59970. #if defined(HAVE_RPK)
  59971. int ret = 0;
  59972. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  59973. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  59974. struct test_memio_ctx test_ctx;
  59975. int err;
  59976. char certType_c[MAX_CLIENT_CERT_TYPE_CNT];
  59977. char certType_s[MAX_CLIENT_CERT_TYPE_CNT];
  59978. int typeCnt_c;
  59979. int typeCnt_s;
  59980. int tp;
  59981. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  59982. int isServer;
  59983. #endif
  59984. (void)err;
  59985. (void)typeCnt_c;
  59986. (void)typeCnt_s;
  59987. (void)certType_c;
  59988. (void)certType_s;
  59989. /* TLS1.2
  59990. * Both client and server load x509 cert and start handshaking.
  59991. * Check no negotiation occurred.
  59992. */
  59993. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59994. ExpectIntEQ(
  59995. test_rpk_memio_setup(
  59996. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59997. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  59998. cliCertFile, WOLFSSL_FILETYPE_PEM,
  59999. svrCertFile, WOLFSSL_FILETYPE_PEM,
  60000. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60001. svrKeyFile, WOLFSSL_FILETYPE_PEM)
  60002. , 0);
  60003. /* set client certificate type in client end */
  60004. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60005. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60006. typeCnt_c = 2;
  60007. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  60008. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  60009. typeCnt_s = 2;
  60010. /* both client and server do not call client/server_cert_type APIs,
  60011. * expecting default settings works and no negotiation performed.
  60012. */
  60013. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  60014. return TEST_FAIL;
  60015. /* confirm no negotiation occurred */
  60016. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60017. WOLFSSL_SUCCESS);
  60018. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60019. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60020. WOLFSSL_SUCCESS);
  60021. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60022. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60023. WOLFSSL_SUCCESS);
  60024. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60025. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60026. WOLFSSL_SUCCESS);
  60027. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60028. (void)typeCnt_c;
  60029. (void)typeCnt_s;
  60030. wolfSSL_free(ssl_c);
  60031. wolfSSL_CTX_free(ctx_c);
  60032. wolfSSL_free(ssl_s);
  60033. wolfSSL_CTX_free(ctx_s);
  60034. ssl_c = ssl_s = NULL;
  60035. ctx_c = ctx_s = NULL;
  60036. /* Both client and server load x509 cert and start handshaking.
  60037. * Check no negotiation occurred.
  60038. */
  60039. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60040. ExpectIntEQ(
  60041. test_rpk_memio_setup(
  60042. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60043. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60044. cliCertFile, WOLFSSL_FILETYPE_PEM,
  60045. svrCertFile, WOLFSSL_FILETYPE_PEM,
  60046. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60047. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60048. , 0);
  60049. /* set client certificate type in client end */
  60050. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60051. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60052. typeCnt_c = 2;
  60053. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  60054. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  60055. typeCnt_s = 2;
  60056. /* both client and server do not call client/server_cert_type APIs,
  60057. * expecting default settings works and no negotiation performed.
  60058. */
  60059. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  60060. return TEST_FAIL;
  60061. /* confirm no negotiation occurred */
  60062. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60063. WOLFSSL_SUCCESS);
  60064. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60065. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60066. WOLFSSL_SUCCESS);
  60067. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60068. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60069. WOLFSSL_SUCCESS);
  60070. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60071. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60072. WOLFSSL_SUCCESS);
  60073. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60074. (void)typeCnt_c;
  60075. (void)typeCnt_s;
  60076. wolfSSL_free(ssl_c);
  60077. wolfSSL_CTX_free(ctx_c);
  60078. wolfSSL_free(ssl_s);
  60079. wolfSSL_CTX_free(ctx_s);
  60080. ssl_c = ssl_s = NULL;
  60081. ctx_c = ctx_s = NULL;
  60082. /* Both client and server load RPK cert and start handshaking.
  60083. * Confirm negotiated cert types match as expected.
  60084. */
  60085. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60086. ExpectIntEQ(
  60087. test_rpk_memio_setup(
  60088. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60089. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60090. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60091. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60092. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60093. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60094. , 0);
  60095. /* set client certificate type in client end */
  60096. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60097. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60098. typeCnt_c = 2;
  60099. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  60100. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  60101. typeCnt_s = 2;
  60102. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60103. WOLFSSL_SUCCESS);
  60104. /* set server certificate type in client end */
  60105. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60106. WOLFSSL_SUCCESS);
  60107. /* set client certificate type in server end */
  60108. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60109. WOLFSSL_SUCCESS);
  60110. /* set server certificate type in server end */
  60111. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60112. WOLFSSL_SUCCESS);
  60113. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  60114. return TEST_FAIL;
  60115. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60116. WOLFSSL_SUCCESS);
  60117. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60118. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60119. WOLFSSL_SUCCESS);
  60120. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60121. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60122. WOLFSSL_SUCCESS);
  60123. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60124. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60125. WOLFSSL_SUCCESS);
  60126. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60127. wolfSSL_free(ssl_c);
  60128. wolfSSL_CTX_free(ctx_c);
  60129. wolfSSL_free(ssl_s);
  60130. wolfSSL_CTX_free(ctx_s);
  60131. ssl_c = ssl_s = NULL;
  60132. ctx_c = ctx_s = NULL;
  60133. /* TLS1.2
  60134. * Both client and server load RPK cert and start handshaking.
  60135. * Confirm negotiated cert types match as expected.
  60136. */
  60137. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60138. ExpectIntEQ(
  60139. test_rpk_memio_setup(
  60140. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60141. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  60142. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60143. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60144. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60145. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60146. , 0);
  60147. /* set client certificate type in client end */
  60148. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60149. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60150. typeCnt_c = 2;
  60151. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  60152. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  60153. typeCnt_s = 2;
  60154. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60155. WOLFSSL_SUCCESS);
  60156. /* set server certificate type in client end */
  60157. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60158. WOLFSSL_SUCCESS);
  60159. /* set client certificate type in server end */
  60160. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60161. WOLFSSL_SUCCESS);
  60162. /* set server certificate type in server end */
  60163. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60164. WOLFSSL_SUCCESS);
  60165. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  60166. return TEST_FAIL;
  60167. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60168. WOLFSSL_SUCCESS);
  60169. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60170. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60171. WOLFSSL_SUCCESS);
  60172. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60173. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60174. WOLFSSL_SUCCESS);
  60175. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60176. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60177. WOLFSSL_SUCCESS);
  60178. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60179. wolfSSL_free(ssl_c);
  60180. wolfSSL_CTX_free(ctx_c);
  60181. wolfSSL_free(ssl_s);
  60182. wolfSSL_CTX_free(ctx_s);
  60183. ssl_c = ssl_s = NULL;
  60184. ctx_c = ctx_s = NULL;
  60185. /* Both client and server load x509 cert.
  60186. * Have client call set_client_cert_type with both RPK and x509.
  60187. * This doesn't makes client add client cert type extension to ClientHello,
  60188. * since it does not load RPK cert actually.
  60189. */
  60190. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60191. ExpectIntEQ(
  60192. test_rpk_memio_setup(
  60193. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60194. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60195. cliCertFile, WOLFSSL_FILETYPE_PEM,
  60196. svrCertFile, WOLFSSL_FILETYPE_PEM,
  60197. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60198. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60199. , 0);
  60200. /* set client certificate type in client end
  60201. *
  60202. * client indicates both RPK and x509 certs are available but loaded RPK
  60203. * cert only. It does not have client add client-cert-type extension in CH.
  60204. */
  60205. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60206. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60207. typeCnt_c = 2;
  60208. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60209. WOLFSSL_SUCCESS);
  60210. /* client indicates both RPK and x509 certs are acceptable */
  60211. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  60212. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  60213. typeCnt_s = 2;
  60214. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60215. WOLFSSL_SUCCESS);
  60216. /* server indicates both RPK and x509 certs are acceptable */
  60217. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60218. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60219. typeCnt_c = 2;
  60220. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60221. WOLFSSL_SUCCESS);
  60222. /* server should indicate only RPK cert is available */
  60223. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60224. certType_s[1] = -1;
  60225. typeCnt_s = 1;
  60226. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60227. WOLFSSL_SUCCESS);
  60228. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  60229. return TEST_FAIL;
  60230. /* Negotiation for client-cert-type should NOT happen. Therefore -1 should
  60231. * be returned as cert type.
  60232. */
  60233. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60234. WOLFSSL_SUCCESS);
  60235. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60236. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60237. WOLFSSL_SUCCESS);
  60238. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60239. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60240. WOLFSSL_SUCCESS);
  60241. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60242. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60243. WOLFSSL_SUCCESS);
  60244. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60245. wolfSSL_free(ssl_c);
  60246. wolfSSL_CTX_free(ctx_c);
  60247. wolfSSL_free(ssl_s);
  60248. wolfSSL_CTX_free(ctx_s);
  60249. ssl_c = ssl_s = NULL;
  60250. ctx_c = ctx_s = NULL;
  60251. /* Have client load RPK cert and have server load x509 cert.
  60252. * Check the negotiation result from both ends.
  60253. */
  60254. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60255. ExpectIntEQ(
  60256. test_rpk_memio_setup(
  60257. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60258. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60259. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60260. svrCertFile, WOLFSSL_FILETYPE_PEM,
  60261. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60262. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60263. , 0);
  60264. /* have client tell to use RPK cert */
  60265. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60266. certType_c[1] = -1;
  60267. typeCnt_c = 1;
  60268. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60269. WOLFSSL_SUCCESS);
  60270. /* have client tell to accept both RPK and x509 cert */
  60271. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60272. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  60273. typeCnt_s = 2;
  60274. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60275. WOLFSSL_SUCCESS);
  60276. /* have server accept to both RPK and x509 cert */
  60277. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  60278. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  60279. typeCnt_c = 2;
  60280. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60281. WOLFSSL_SUCCESS);
  60282. /* does not call wolfSSL_set_server_cert_type intentionally in sesrver
  60283. * end, expecting the default setting works.
  60284. */
  60285. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  60286. return TEST_FAIL;
  60287. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60288. WOLFSSL_SUCCESS);
  60289. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60290. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60291. WOLFSSL_SUCCESS);
  60292. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60293. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60294. WOLFSSL_SUCCESS);
  60295. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60296. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60297. WOLFSSL_SUCCESS);
  60298. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60299. wolfSSL_free(ssl_c);
  60300. wolfSSL_CTX_free(ctx_c);
  60301. wolfSSL_free(ssl_s);
  60302. wolfSSL_CTX_free(ctx_s);
  60303. ssl_c = ssl_s = NULL;
  60304. ctx_c = ctx_s = NULL;
  60305. /* Have both client and server load RPK cert, however, have server
  60306. * indicate its cert type x509.
  60307. * Client is expected to detect the cert type mismatch then to send alert
  60308. * with "unsupported_certificate".
  60309. */
  60310. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60311. ExpectIntEQ(
  60312. test_rpk_memio_setup(
  60313. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60314. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60315. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60316. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1, /* server sends RPK cert */
  60317. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60318. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60319. , 0);
  60320. /* have client tell to use RPK cert */
  60321. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60322. certType_c[1] = -1;
  60323. typeCnt_c = 1;
  60324. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60325. WOLFSSL_SUCCESS);
  60326. /* have client tell to accept both RPK and x509 cert */
  60327. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60328. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  60329. typeCnt_s = 2;
  60330. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60331. WOLFSSL_SUCCESS);
  60332. /* have server accept to both RPK and x509 cert */
  60333. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  60334. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  60335. typeCnt_c = 2;
  60336. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60337. WOLFSSL_SUCCESS);
  60338. /* have server tell to use x509 cert intentionally. This will bring
  60339. * certificate type mismatch in client side.
  60340. */
  60341. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60342. certType_s[1] = -1;
  60343. typeCnt_s = 1;
  60344. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60345. WOLFSSL_SUCCESS);
  60346. /* expect client detect cert type mismatch then send Alert */
  60347. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  60348. if (ret != -1)
  60349. return TEST_FAIL;
  60350. ExpectIntEQ(wolfSSL_get_error(ssl_c, ret), UNSUPPORTED_CERTIFICATE);
  60351. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60352. WOLFSSL_SUCCESS);
  60353. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60354. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60355. WOLFSSL_SUCCESS);
  60356. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60357. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60358. WOLFSSL_SUCCESS);
  60359. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60360. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60361. WOLFSSL_SUCCESS);
  60362. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60363. wolfSSL_free(ssl_c);
  60364. wolfSSL_CTX_free(ctx_c);
  60365. wolfSSL_free(ssl_s);
  60366. wolfSSL_CTX_free(ctx_s);
  60367. ssl_c = ssl_s = NULL;
  60368. ctx_c = ctx_s = NULL;
  60369. /* Have client load x509 cert and server load RPK cert,
  60370. * however, have client indicate its cert type RPK.
  60371. * Server is expected to detect the cert type mismatch then to send alert
  60372. * with "unsupported_certificate".
  60373. */
  60374. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60375. ExpectIntEQ(
  60376. test_rpk_memio_setup(
  60377. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60378. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60379. cliCertFile, WOLFSSL_FILETYPE_PEM,
  60380. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60381. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60382. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60383. , 0);
  60384. /* have client tell to use RPK cert intentionally */
  60385. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60386. certType_c[1] = -1;
  60387. typeCnt_c = 1;
  60388. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60389. WOLFSSL_SUCCESS);
  60390. /* have client tell to accept both RPK and x509 cert */
  60391. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60392. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  60393. typeCnt_s = 2;
  60394. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60395. WOLFSSL_SUCCESS);
  60396. /* have server accept to both RPK and x509 cert */
  60397. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  60398. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  60399. typeCnt_c = 2;
  60400. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60401. WOLFSSL_SUCCESS);
  60402. /* have server tell to use x509 cert intentionally. This will bring
  60403. * certificate type mismatch in client side.
  60404. */
  60405. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60406. certType_s[1] = -1;
  60407. typeCnt_s = 1;
  60408. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60409. WOLFSSL_SUCCESS);
  60410. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  60411. /* expect server detect cert type mismatch then send Alert */
  60412. ExpectIntNE(ret, 0);
  60413. err = wolfSSL_get_error(ssl_c, ret);
  60414. ExpectIntEQ(err, UNSUPPORTED_CERTIFICATE);
  60415. /* client did not load RPK cert actually, so negotiation did not happen */
  60416. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60417. WOLFSSL_SUCCESS);
  60418. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60419. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60420. WOLFSSL_SUCCESS);
  60421. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60422. /* client did not load RPK cert actually, so negotiation did not happen */
  60423. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60424. WOLFSSL_SUCCESS);
  60425. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60426. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60427. WOLFSSL_SUCCESS);
  60428. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60429. wolfSSL_free(ssl_c);
  60430. wolfSSL_CTX_free(ctx_c);
  60431. wolfSSL_free(ssl_s);
  60432. wolfSSL_CTX_free(ctx_s);
  60433. ssl_c = ssl_s = NULL;
  60434. ctx_c = ctx_s = NULL;
  60435. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  60436. /* Both client and server load RPK cert and set certificate verify
  60437. * callbacks then start handshaking.
  60438. * Confirm both side can refer the peer's cert.
  60439. */
  60440. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60441. ExpectIntEQ(
  60442. test_rpk_memio_setup(
  60443. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60444. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60445. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60446. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60447. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60448. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60449. , 0);
  60450. /* set client certificate type in client end */
  60451. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60452. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60453. typeCnt_c = 2;
  60454. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  60455. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  60456. typeCnt_s = 2;
  60457. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60458. WOLFSSL_SUCCESS);
  60459. /* set server certificate type in client end */
  60460. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60461. WOLFSSL_SUCCESS);
  60462. /* set client certificate type in server end */
  60463. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60464. WOLFSSL_SUCCESS);
  60465. /* set server certificate type in server end */
  60466. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60467. WOLFSSL_SUCCESS);
  60468. /* set certificate verify callback to both client and server */
  60469. isServer = 0;
  60470. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  60471. wolfSSL_set_verify(ssl_c, SSL_VERIFY_PEER, MyRpkVerifyCb);
  60472. isServer = 1;
  60473. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  60474. wolfSSL_set_verify(ssl_s, SSL_VERIFY_PEER, MyRpkVerifyCb);
  60475. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  60476. if (ret != 0)
  60477. return TEST_FAIL;
  60478. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60479. WOLFSSL_SUCCESS);
  60480. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60481. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60482. WOLFSSL_SUCCESS);
  60483. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60484. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60485. WOLFSSL_SUCCESS);
  60486. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60487. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60488. WOLFSSL_SUCCESS);
  60489. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60490. wolfSSL_free(ssl_c);
  60491. wolfSSL_CTX_free(ctx_c);
  60492. wolfSSL_free(ssl_s);
  60493. wolfSSL_CTX_free(ctx_s);
  60494. ssl_c = ssl_s = NULL;
  60495. ctx_c = ctx_s = NULL;
  60496. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  60497. #endif /* HAVE_RPK */
  60498. return EXPECT_RESULT();
  60499. }
  60500. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  60501. static int test_dtls13_bad_epoch_ch(void)
  60502. {
  60503. EXPECT_DECLS;
  60504. WOLFSSL_CTX *ctx_c = NULL;
  60505. WOLFSSL_CTX *ctx_s = NULL;
  60506. WOLFSSL *ssl_c = NULL;
  60507. WOLFSSL *ssl_s = NULL;
  60508. struct test_memio_ctx test_ctx;
  60509. const int EPOCH_OFF = 3;
  60510. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60511. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60512. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  60513. /* disable hrr cookie so we can later check msgsReceived.got_client_hello
  60514. * with just one message */
  60515. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  60516. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60517. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60518. WOLFSSL_ERROR_WANT_READ);
  60519. ExpectIntGE(test_ctx.s_len, EPOCH_OFF + 2);
  60520. /* first CH should use epoch 0x0 */
  60521. ExpectTrue((test_ctx.s_buff[EPOCH_OFF] == 0x0) &&
  60522. (test_ctx.s_buff[EPOCH_OFF + 1] == 0x0));
  60523. /* change epoch to 2 */
  60524. test_ctx.s_buff[EPOCH_OFF + 1] = 0x2;
  60525. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  60526. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  60527. WOLFSSL_ERROR_WANT_READ);
  60528. ExpectIntNE(ssl_s->msgsReceived.got_client_hello, 1);
  60529. /* resend the CH */
  60530. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  60531. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60532. wolfSSL_free(ssl_c);
  60533. wolfSSL_CTX_free(ctx_c);
  60534. wolfSSL_free(ssl_s);
  60535. wolfSSL_CTX_free(ctx_s);
  60536. return EXPECT_RESULT();
  60537. }
  60538. #else
  60539. static int test_dtls13_bad_epoch_ch(void)
  60540. {
  60541. return TEST_SKIPPED;
  60542. }
  60543. #endif
  60544. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_SESSION_CACHE)
  60545. static int test_short_session_id_ssl_ready(WOLFSSL* ssl)
  60546. {
  60547. EXPECT_DECLS;
  60548. WOLFSSL_SESSION *sess = NULL;
  60549. /* Setup the session to avoid errors */
  60550. ssl->session->timeout = -1;
  60551. ssl->session->side = WOLFSSL_CLIENT_END;
  60552. #if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
  60553. defined(HAVE_SESSION_TICKET))
  60554. ssl->session->version = ssl->version;
  60555. #endif
  60556. /* Force a short session ID to be sent */
  60557. ssl->session->sessionIDSz = 4;
  60558. #ifndef NO_SESSION_CACHE_REF
  60559. /* Allow the client cache to be used */
  60560. ssl->session->idLen = 4;
  60561. #endif
  60562. ssl->session->isSetup = 1;
  60563. ExpectNotNull(sess = wolfSSL_get_session(ssl));
  60564. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  60565. return EXPECT_RESULT();
  60566. }
  60567. static int test_short_session_id(void)
  60568. {
  60569. EXPECT_DECLS;
  60570. test_ssl_cbf client_cbf;
  60571. test_ssl_cbf server_cbf;
  60572. size_t i;
  60573. struct {
  60574. method_provider client_meth;
  60575. method_provider server_meth;
  60576. const char* tls_version;
  60577. } params[] = {
  60578. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  60579. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID) && \
  60580. !defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
  60581. /* With WOLFSSL_TLS13_MIDDLEBOX_COMPAT a short ID will result in an error */
  60582. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  60583. #ifdef WOLFSSL_DTLS13
  60584. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  60585. #endif
  60586. #endif
  60587. #ifndef WOLFSSL_NO_TLS12
  60588. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  60589. #ifdef WOLFSSL_DTLS
  60590. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  60591. #endif
  60592. #endif
  60593. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  60594. !defined(NO_DES3))
  60595. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  60596. #ifdef WOLFSSL_DTLS
  60597. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  60598. #endif
  60599. #endif
  60600. };
  60601. fprintf(stderr, "\n");
  60602. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  60603. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  60604. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  60605. fprintf(stderr, "\tTesting short ID with %s\n", params[i].tls_version);
  60606. client_cbf.ssl_ready = test_short_session_id_ssl_ready;
  60607. client_cbf.method = params[i].client_meth;
  60608. server_cbf.method = params[i].server_meth;
  60609. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  60610. &server_cbf, NULL), TEST_SUCCESS);
  60611. }
  60612. return EXPECT_RESULT();
  60613. }
  60614. #else
  60615. static int test_short_session_id(void)
  60616. {
  60617. return TEST_SKIPPED;
  60618. }
  60619. #endif
  60620. #if defined(HAVE_NULL_CIPHER) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) \
  60621. && defined(WOLFSSL_DTLS13)
  60622. static byte* test_find_string(const char *string,
  60623. byte *buf, int buf_size)
  60624. {
  60625. int string_size, i;
  60626. string_size = (int)XSTRLEN(string);
  60627. for (i = 0; i < buf_size - string_size - 1; i++) {
  60628. if (XSTRCMP((char*)&buf[i], string) == 0)
  60629. return &buf[i];
  60630. }
  60631. return NULL;
  60632. }
  60633. static int test_wolfSSL_dtls13_null_cipher(void)
  60634. {
  60635. EXPECT_DECLS;
  60636. WOLFSSL_CTX *ctx_c = NULL;
  60637. WOLFSSL_CTX *ctx_s = NULL;
  60638. WOLFSSL *ssl_c = NULL;
  60639. WOLFSSL *ssl_s = NULL;
  60640. struct test_memio_ctx test_ctx;
  60641. const char *test_str = "test";
  60642. int test_str_size;
  60643. byte buf[255], *ptr = NULL;
  60644. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60645. test_ctx.c_ciphers = test_ctx.s_ciphers = "TLS13-SHA256-SHA256";
  60646. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60647. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  60648. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60649. test_str_size = XSTRLEN("test") + 1;
  60650. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  60651. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  60652. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  60653. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  60654. /* check that the packet was sent cleartext */
  60655. ExpectNotNull(ptr = test_find_string(test_str, test_ctx.s_buff,
  60656. test_ctx.s_len));
  60657. if (ptr != NULL) {
  60658. /* modify the message */
  60659. *ptr = 'H';
  60660. /* bad messages should be ignored in DTLS */
  60661. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), -1);
  60662. ExpectIntEQ(ssl_s->error, WANT_READ);
  60663. }
  60664. wolfSSL_free(ssl_c);
  60665. wolfSSL_free(ssl_s);
  60666. wolfSSL_CTX_free(ctx_c);
  60667. wolfSSL_CTX_free(ctx_s);
  60668. return TEST_SUCCESS;
  60669. }
  60670. #else
  60671. static int test_wolfSSL_dtls13_null_cipher(void)
  60672. {
  60673. return TEST_SKIPPED;
  60674. }
  60675. #endif
  60676. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  60677. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  60678. !defined(SINGLE_THREADED) && !defined(NO_RSA)
  60679. static int test_dtls_msg_get_connected_port(int fd, word16 *port)
  60680. {
  60681. SOCKADDR_S peer;
  60682. XSOCKLENT len;
  60683. int ret;
  60684. XMEMSET((byte*)&peer, 0, sizeof(peer));
  60685. len = sizeof(peer);
  60686. ret = getpeername(fd, (SOCKADDR*)&peer, &len);
  60687. if (ret != 0 || len > (XSOCKLENT)sizeof(peer))
  60688. return -1;
  60689. switch (peer.ss_family) {
  60690. #ifdef WOLFSSL_IPV6
  60691. case WOLFSSL_IP6: {
  60692. *port = ntohs(((SOCKADDR_IN6*)&peer)->sin6_port);
  60693. break;
  60694. }
  60695. #endif /* WOLFSSL_IPV6 */
  60696. case WOLFSSL_IP4:
  60697. *port = ntohs(((SOCKADDR_IN*)&peer)->sin_port);
  60698. break;
  60699. default:
  60700. return -1;
  60701. }
  60702. return 0;
  60703. }
  60704. static int test_dtls_msg_from_other_peer_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  60705. {
  60706. char buf[1] = {'t'};
  60707. SOCKADDR_IN_T addr;
  60708. int sock_fd;
  60709. word16 port;
  60710. int err;
  60711. (void)ssl;
  60712. (void)ctx;
  60713. if (ssl == NULL)
  60714. return -1;
  60715. err = test_dtls_msg_get_connected_port(wolfSSL_get_fd(ssl), &port);
  60716. if (err != 0)
  60717. return -1;
  60718. sock_fd = socket(AF_INET_V, SOCK_DGRAM, 0);
  60719. if (sock_fd == -1)
  60720. return -1;
  60721. build_addr(&addr, wolfSSLIP, port, 1, 0);
  60722. /* send a packet to the server. Being another socket, the kernel will ensure
  60723. * the source port will be different. */
  60724. err = (int)sendto(sock_fd, buf, sizeof(buf), 0, (SOCKADDR*)&addr,
  60725. sizeof(addr));
  60726. close(sock_fd);
  60727. if (err == -1)
  60728. return -1;
  60729. return 0;
  60730. }
  60731. /* setup a SSL session but just after the handshake send a packet to the server
  60732. * with a source address different than the one of the connected client. The I/O
  60733. * callback EmbedRecvFrom should just ignore the packet. Sending of the packet
  60734. * is done in test_dtls_msg_from_other_peer_cb */
  60735. static int test_dtls_msg_from_other_peer(void)
  60736. {
  60737. EXPECT_DECLS;
  60738. callback_functions client_cbs;
  60739. callback_functions server_cbs;
  60740. XMEMSET((byte*)&client_cbs, 0, sizeof(client_cbs));
  60741. XMEMSET((byte*)&server_cbs, 0, sizeof(server_cbs));
  60742. client_cbs.method = wolfDTLSv1_2_client_method;
  60743. server_cbs.method = wolfDTLSv1_2_server_method;
  60744. client_cbs.doUdp = 1;
  60745. server_cbs.doUdp = 1;
  60746. test_wolfSSL_client_server_nofail_ex(&client_cbs, &server_cbs,
  60747. test_dtls_msg_from_other_peer_cb);
  60748. ExpectIntEQ(client_cbs.return_code, WOLFSSL_SUCCESS);
  60749. ExpectIntEQ(server_cbs.return_code, WOLFSSL_SUCCESS);
  60750. return EXPECT_RESULT();
  60751. }
  60752. #else
  60753. static int test_dtls_msg_from_other_peer(void)
  60754. {
  60755. return TEST_SKIPPED;
  60756. }
  60757. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  60758. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  60759. * !defined(SINGLE_THREADED) && !defined(NO_RSA) */
  60760. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_IPV6) && \
  60761. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  60762. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  60763. static int test_dtls_ipv6_check(void)
  60764. {
  60765. EXPECT_DECLS;
  60766. WOLFSSL_CTX *ctx_c = NULL;
  60767. WOLFSSL_CTX *ctx_s = NULL;
  60768. WOLFSSL *ssl_c = NULL;
  60769. WOLFSSL *ssl_s = NULL;
  60770. SOCKADDR_IN fake_addr6;
  60771. int sockfd = -1;
  60772. ExpectNotNull(ctx_c = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  60773. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  60774. ExpectNotNull(ctx_s = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  60775. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile,
  60776. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60777. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile,
  60778. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  60779. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  60780. XMEMSET((byte*)&fake_addr6, 0, sizeof(fake_addr6));
  60781. /* mimic a sockaddr_in6 struct, this way we can't test without
  60782. * WOLFSSL_IPV6 */
  60783. fake_addr6.sin_family = WOLFSSL_IP6;
  60784. ExpectIntNE(sockfd = socket(AF_INET, SOCK_DGRAM, 0), -1);
  60785. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  60786. /* can't return error here, as the peer is opaque for wolfssl library at
  60787. * this point */
  60788. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_c, &fake_addr6, sizeof(fake_addr6)),
  60789. WOLFSSL_SUCCESS);
  60790. ExpectIntNE(fcntl(sockfd, F_SETFL, O_NONBLOCK), -1);
  60791. wolfSSL_dtls_set_using_nonblock(ssl_c, 1);
  60792. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60793. ExpectIntEQ(ssl_c->error, SOCKET_ERROR_E);
  60794. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_s, &fake_addr6, sizeof(fake_addr6)),
  60795. WOLFSSL_SUCCESS);
  60796. /* reuse the socket */
  60797. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  60798. wolfSSL_dtls_set_using_nonblock(ssl_s, 1);
  60799. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  60800. ExpectIntEQ(ssl_s->error, SOCKET_ERROR_E);
  60801. if (sockfd != -1)
  60802. close(sockfd);
  60803. wolfSSL_free(ssl_c);
  60804. wolfSSL_CTX_free(ctx_c);
  60805. wolfSSL_free(ssl_s);
  60806. wolfSSL_CTX_free(ctx_s);
  60807. return EXPECT_RESULT();
  60808. }
  60809. #else
  60810. static int test_dtls_ipv6_check(void)
  60811. {
  60812. return TEST_SKIPPED;
  60813. }
  60814. #endif
  60815. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  60816. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  60817. static WOLFSSL_SESSION* test_wolfSSL_SCR_after_resumption_session = NULL;
  60818. static void test_wolfSSL_SCR_after_resumption_ctx_ready(WOLFSSL_CTX* ctx)
  60819. {
  60820. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  60821. }
  60822. static void test_wolfSSL_SCR_after_resumption_on_result(WOLFSSL* ssl)
  60823. {
  60824. if (test_wolfSSL_SCR_after_resumption_session == NULL) {
  60825. test_wolfSSL_SCR_after_resumption_session = wolfSSL_get1_session(ssl);
  60826. AssertNotNull(test_wolfSSL_SCR_after_resumption_session);
  60827. }
  60828. else {
  60829. char testMsg[] = "Message after SCR";
  60830. char msgBuf[sizeof(testMsg)];
  60831. int ret;
  60832. if (!wolfSSL_is_server(ssl)) {
  60833. AssertIntEQ(WOLFSSL_SUCCESS,
  60834. wolfSSL_set_session(ssl,
  60835. test_wolfSSL_SCR_after_resumption_session));
  60836. }
  60837. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  60838. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  60839. sizeof(testMsg));
  60840. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  60841. if (ret != sizeof(msgBuf)) /* Possibly APP_DATA_READY error. Retry. */
  60842. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  60843. AssertIntEQ(ret, sizeof(msgBuf));
  60844. }
  60845. }
  60846. static void test_wolfSSL_SCR_after_resumption_ssl_ready(WOLFSSL* ssl)
  60847. {
  60848. AssertIntEQ(WOLFSSL_SUCCESS,
  60849. wolfSSL_set_session(ssl, test_wolfSSL_SCR_after_resumption_session));
  60850. }
  60851. static int test_wolfSSL_SCR_after_resumption(void)
  60852. {
  60853. EXPECT_DECLS;
  60854. callback_functions func_cb_client;
  60855. callback_functions func_cb_server;
  60856. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  60857. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  60858. func_cb_client.method = wolfTLSv1_2_client_method;
  60859. func_cb_client.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  60860. func_cb_client.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  60861. func_cb_server.method = wolfTLSv1_2_server_method;
  60862. func_cb_server.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  60863. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  60864. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  60865. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  60866. func_cb_client.ssl_ready = test_wolfSSL_SCR_after_resumption_ssl_ready;
  60867. func_cb_server.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  60868. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  60869. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  60870. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  60871. wolfSSL_SESSION_free(test_wolfSSL_SCR_after_resumption_session);
  60872. return EXPECT_RESULT();
  60873. }
  60874. #else
  60875. static int test_wolfSSL_SCR_after_resumption(void)
  60876. {
  60877. return TEST_SKIPPED;
  60878. }
  60879. #endif
  60880. static int test_wolfSSL_configure_args(void)
  60881. {
  60882. EXPECT_DECLS;
  60883. #if defined(LIBWOLFSSL_CONFIGURE_ARGS) && defined(HAVE_WC_INTROSPECTION)
  60884. ExpectNotNull(wolfSSL_configure_args());
  60885. #endif
  60886. return EXPECT_RESULT();
  60887. }
  60888. static int test_dtls_no_extensions(void)
  60889. {
  60890. EXPECT_DECLS;
  60891. #if defined(WOLFSSL_DTLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  60892. !defined(WOLFSSL_NO_TLS12)
  60893. WOLFSSL *ssl_s = NULL;
  60894. WOLFSSL_CTX *ctx_s = NULL;
  60895. struct test_memio_ctx test_ctx;
  60896. const byte chNoExtensions[] = {
  60897. /* Handshake type */
  60898. 0x16,
  60899. /* Version */
  60900. 0xfe, 0xff,
  60901. /* Epoch */
  60902. 0x00, 0x00,
  60903. /* Seq number */
  60904. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  60905. /* Length */
  60906. 0x00, 0x40,
  60907. /* CH type */
  60908. 0x01,
  60909. /* Length */
  60910. 0x00, 0x00, 0x34,
  60911. /* Msg Seq */
  60912. 0x00, 0x00,
  60913. /* Frag offset */
  60914. 0x00, 0x00, 0x00,
  60915. /* Frag length */
  60916. 0x00, 0x00, 0x34,
  60917. /* Version */
  60918. 0xfe, 0xff,
  60919. /* Random */
  60920. 0x62, 0xfe, 0xbc, 0xfe, 0x2b, 0xfe, 0x3f, 0xeb, 0x03, 0xc4, 0xea, 0x37,
  60921. 0xe7, 0x47, 0x7e, 0x8a, 0xd9, 0xbf, 0x77, 0x0f, 0x6c, 0xb6, 0x77, 0x0b,
  60922. 0x03, 0x3f, 0x82, 0x2b, 0x21, 0x64, 0x57, 0x1d,
  60923. /* Session Length */
  60924. 0x00,
  60925. /* Cookie Length */
  60926. 0x00,
  60927. /* CS Length */
  60928. 0x00, 0x0c,
  60929. /* CS */
  60930. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x39, 0x00, 0x33,
  60931. /* Comp Meths Length */
  60932. 0x01,
  60933. /* Comp Meths */
  60934. 0x00
  60935. /* And finally... no extensions */
  60936. };
  60937. int i;
  60938. #ifdef OPENSSL_EXTRA
  60939. int repeats = 2;
  60940. #else
  60941. int repeats = 1;
  60942. #endif
  60943. for (i = 0; i < repeats; i++) {
  60944. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60945. ssl_s = NULL;
  60946. ctx_s = NULL;
  60947. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  60948. NULL, wolfDTLS_server_method), 0);
  60949. XMEMCPY(test_ctx.s_buff, chNoExtensions, sizeof(chNoExtensions));
  60950. test_ctx.s_len = sizeof(chNoExtensions);
  60951. #ifdef OPENSSL_EXTRA
  60952. if (i > 0) {
  60953. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl_s, DTLS1_2_VERSION),
  60954. WOLFSSL_SUCCESS);
  60955. }
  60956. #endif
  60957. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  60958. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  60959. /* Expecting a handshake msg. Either HVR or SH. */
  60960. ExpectIntGT(test_ctx.c_len, 0);
  60961. ExpectIntEQ(test_ctx.c_buff[0], 0x16);
  60962. wolfSSL_free(ssl_s);
  60963. wolfSSL_CTX_free(ctx_s);
  60964. }
  60965. #endif
  60966. return EXPECT_RESULT();
  60967. }
  60968. static int test_TLSX_CA_NAMES_bad_extension(void)
  60969. {
  60970. EXPECT_DECLS;
  60971. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  60972. !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES) && \
  60973. defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA384) && \
  60974. defined(HAVE_NULL_CIPHER)
  60975. /* This test should only fail (with BUFFER_ERROR) when we actually try to
  60976. * parse the CA Names extension. Otherwise it will return other non-related
  60977. * errors. If CA Names will be parsed in more configurations, that should
  60978. * be reflected in the macro guard above. */
  60979. WOLFSSL *ssl_c = NULL;
  60980. WOLFSSL_CTX *ctx_c = NULL;
  60981. struct test_memio_ctx test_ctx;
  60982. /* HRR + SH using TLS_DHE_PSK_WITH_NULL_SHA384 */
  60983. const byte shBadCaNamesExt[] = {
  60984. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  60985. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  60986. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  60987. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  60988. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  60989. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  60990. 0x5c, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x03, 0xcf, 0x21, 0xad, 0x74,
  60991. 0x00, 0x00, 0x83, 0x3f, 0x3b, 0x80, 0x01, 0xac, 0x65, 0x8c, 0x19, 0x2a,
  60992. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x02, 0x00, 0x9e, 0x09, 0x1c, 0xe8,
  60993. 0xa8, 0x09, 0x9c, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  60994. 0x03, 0x3f, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x13, 0x05,
  60995. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x00, 0x09, 0x00, 0x00,
  60996. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x01, 0xff,
  60997. 0xff, 0xff, 0xff, 0xfa, 0x0d, 0x00, 0x00, 0x00, 0xad, 0x02
  60998. };
  60999. const byte shBadCaNamesExt2[] = {
  61000. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  61001. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  61002. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  61003. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  61004. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  61005. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  61006. 0x5e, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x7f, 0xd0, 0x2d, 0xea, 0x6e,
  61007. 0x53, 0xa1, 0x6a, 0xc9, 0xc8, 0x54, 0xef, 0x75, 0xe4, 0xd9, 0xc6, 0x3e,
  61008. 0x74, 0xcb, 0x30, 0x80, 0xcc, 0x83, 0x3a, 0x00, 0x00, 0x00, 0x00, 0x00,
  61009. 0x00, 0xc0, 0x5a, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  61010. 0x03, 0x03, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x53, 0x25,
  61011. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x02, 0x05, 0x00, 0x00,
  61012. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x06, 0x00,
  61013. 0x04, 0x00, 0x03, 0x30, 0x00, 0x13, 0x94, 0x00, 0x06, 0x00, 0x04, 0x02
  61014. };
  61015. int i = 0;
  61016. for (i = 0; i < 2; i++) {
  61017. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61018. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  61019. wolfTLSv1_3_client_method, NULL), 0);
  61020. switch (i) {
  61021. case 0:
  61022. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt,
  61023. sizeof(shBadCaNamesExt));
  61024. test_ctx.c_len = sizeof(shBadCaNamesExt);
  61025. break;
  61026. case 1:
  61027. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt2,
  61028. sizeof(shBadCaNamesExt2));
  61029. test_ctx.c_len = sizeof(shBadCaNamesExt2);
  61030. break;
  61031. }
  61032. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  61033. #ifndef WOLFSSL_DISABLE_EARLY_SANITY_CHECKS
  61034. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), OUT_OF_ORDER_E);
  61035. #else
  61036. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), BUFFER_ERROR);
  61037. #endif
  61038. wolfSSL_free(ssl_c);
  61039. ssl_c = NULL;
  61040. wolfSSL_CTX_free(ctx_c);
  61041. ctx_c = NULL;
  61042. }
  61043. #endif
  61044. return EXPECT_RESULT();
  61045. }
  61046. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  61047. defined(HAVE_IO_TESTS_DEPENDENCIES)
  61048. static void test_dtls_1_0_hvr_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  61049. {
  61050. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  61051. WOLFSSL_SUCCESS);
  61052. }
  61053. static int test_dtls_1_0_hvr_downgrade(void)
  61054. {
  61055. EXPECT_DECLS;
  61056. callback_functions func_cb_client;
  61057. callback_functions func_cb_server;
  61058. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  61059. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  61060. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  61061. func_cb_client.method = wolfDTLS_client_method;
  61062. func_cb_server.method = wolfDTLSv1_2_server_method;
  61063. func_cb_client.ctx_ready = test_dtls_1_0_hvr_downgrade_ctx_ready;
  61064. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61065. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61066. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61067. return EXPECT_RESULT();
  61068. }
  61069. #else
  61070. static int test_dtls_1_0_hvr_downgrade(void)
  61071. {
  61072. EXPECT_DECLS;
  61073. return EXPECT_RESULT();
  61074. }
  61075. #endif
  61076. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12) && \
  61077. defined(HAVE_SESSION_TICKET)
  61078. static WOLFSSL_SESSION* test_session_ticket_no_id_session = NULL;
  61079. static void test_session_ticket_no_id_on_result(WOLFSSL* ssl)
  61080. {
  61081. test_session_ticket_no_id_session = wolfSSL_get1_session(ssl);
  61082. AssertNotNull(test_session_ticket_no_id_session);
  61083. }
  61084. static void test_session_ticket_no_id_ctx_ready(WOLFSSL_CTX* ctx)
  61085. {
  61086. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  61087. }
  61088. static void test_session_ticket_no_id_ssl_ready(WOLFSSL* ssl)
  61089. {
  61090. test_session_ticket_no_id_session->sessionIDSz = 0;
  61091. AssertIntEQ(WOLFSSL_SUCCESS,
  61092. wolfSSL_set_session(ssl, test_session_ticket_no_id_session));
  61093. }
  61094. static int test_session_ticket_no_id(void)
  61095. {
  61096. /* We are testing an expired (invalid crypto context in out case since the
  61097. * ctx changes) session ticket being sent with the session ID being 0
  61098. * length. */
  61099. EXPECT_DECLS;
  61100. callback_functions func_cb_client;
  61101. callback_functions func_cb_server;
  61102. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  61103. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  61104. func_cb_client.method = wolfTLSv1_2_client_method;
  61105. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  61106. func_cb_client.on_result = test_session_ticket_no_id_on_result;
  61107. func_cb_server.method = wolfTLSv1_2_server_method;
  61108. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  61109. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61110. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61111. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61112. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  61113. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  61114. func_cb_client.method = wolfTLSv1_2_client_method;
  61115. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  61116. func_cb_client.ssl_ready = test_session_ticket_no_id_ssl_ready;
  61117. func_cb_server.method = wolfTLSv1_2_server_method;
  61118. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  61119. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61120. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61121. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61122. wolfSSL_SESSION_free(test_session_ticket_no_id_session);
  61123. return EXPECT_RESULT();
  61124. }
  61125. #else
  61126. static int test_session_ticket_no_id(void)
  61127. {
  61128. EXPECT_DECLS;
  61129. return EXPECT_RESULT();
  61130. }
  61131. #endif
  61132. static int test_session_ticket_hs_update(void)
  61133. {
  61134. EXPECT_DECLS;
  61135. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  61136. defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  61137. struct test_memio_ctx test_ctx;
  61138. struct test_memio_ctx test_ctx2;
  61139. struct test_memio_ctx test_ctx3;
  61140. WOLFSSL_CTX *ctx_c = NULL;
  61141. WOLFSSL_CTX *ctx_s = NULL;
  61142. WOLFSSL *ssl_c = NULL;
  61143. WOLFSSL *ssl_c2 = NULL;
  61144. WOLFSSL *ssl_c3 = NULL;
  61145. WOLFSSL *ssl_s = NULL;
  61146. WOLFSSL *ssl_s2 = NULL;
  61147. WOLFSSL *ssl_s3 = NULL;
  61148. WOLFSSL_SESSION *sess = NULL;
  61149. byte read_data[1];
  61150. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61151. XMEMSET(&test_ctx2, 0, sizeof(test_ctx2));
  61152. XMEMSET(&test_ctx3, 0, sizeof(test_ctx3));
  61153. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61154. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  61155. /* Generate tickets */
  61156. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61157. wolfSSL_SetLoggingPrefix("client");
  61158. /* Read the ticket msg */
  61159. ExpectIntEQ(wolfSSL_read(ssl_c, read_data, sizeof(read_data)),
  61160. WOLFSSL_FATAL_ERROR);
  61161. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61162. WOLFSSL_ERROR_WANT_READ);
  61163. wolfSSL_SetLoggingPrefix(NULL);
  61164. ExpectIntEQ(test_memio_setup(&test_ctx2, &ctx_c, &ctx_s, &ssl_c2, &ssl_s2,
  61165. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  61166. ExpectIntEQ(test_memio_setup(&test_ctx3, &ctx_c, &ctx_s, &ssl_c3, &ssl_s3,
  61167. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  61168. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  61169. ExpectIntEQ(wolfSSL_set_session(ssl_c2, sess), WOLFSSL_SUCCESS);
  61170. ExpectIntEQ(wolfSSL_set_session(ssl_c3, sess), WOLFSSL_SUCCESS);
  61171. wolfSSL_SetLoggingPrefix("client");
  61172. /* Exchange initial flights for the second connection */
  61173. ExpectIntEQ(wolfSSL_connect(ssl_c2), WOLFSSL_FATAL_ERROR);
  61174. ExpectIntEQ(wolfSSL_get_error(ssl_c2, WOLFSSL_FATAL_ERROR),
  61175. WOLFSSL_ERROR_WANT_READ);
  61176. wolfSSL_SetLoggingPrefix(NULL);
  61177. wolfSSL_SetLoggingPrefix("server");
  61178. ExpectIntEQ(wolfSSL_accept(ssl_s2), WOLFSSL_FATAL_ERROR);
  61179. ExpectIntEQ(wolfSSL_get_error(ssl_s2, WOLFSSL_FATAL_ERROR),
  61180. WOLFSSL_ERROR_WANT_READ);
  61181. wolfSSL_SetLoggingPrefix(NULL);
  61182. /* Complete third connection so that new tickets are exchanged */
  61183. ExpectIntEQ(test_memio_do_handshake(ssl_c3, ssl_s3, 10, NULL), 0);
  61184. /* Read the ticket msg */
  61185. wolfSSL_SetLoggingPrefix("client");
  61186. ExpectIntEQ(wolfSSL_read(ssl_c3, read_data, sizeof(read_data)),
  61187. WOLFSSL_FATAL_ERROR);
  61188. ExpectIntEQ(wolfSSL_get_error(ssl_c3, WOLFSSL_FATAL_ERROR),
  61189. WOLFSSL_ERROR_WANT_READ);
  61190. wolfSSL_SetLoggingPrefix(NULL);
  61191. /* Complete second connection */
  61192. ExpectIntEQ(test_memio_do_handshake(ssl_c2, ssl_s2, 10, NULL), 0);
  61193. ExpectIntEQ(wolfSSL_session_reused(ssl_c2), 1);
  61194. ExpectIntEQ(wolfSSL_session_reused(ssl_c3), 1);
  61195. wolfSSL_free(ssl_c);
  61196. wolfSSL_free(ssl_c2);
  61197. wolfSSL_free(ssl_c3);
  61198. wolfSSL_free(ssl_s);
  61199. wolfSSL_free(ssl_s2);
  61200. wolfSSL_free(ssl_s3);
  61201. wolfSSL_CTX_free(ctx_c);
  61202. wolfSSL_CTX_free(ctx_s);
  61203. wolfSSL_SESSION_free(sess);
  61204. #endif
  61205. return EXPECT_RESULT();
  61206. }
  61207. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  61208. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  61209. static void test_dtls_downgrade_scr_server_ctx_ready_server(WOLFSSL_CTX* ctx)
  61210. {
  61211. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  61212. WOLFSSL_SUCCESS);
  61213. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  61214. }
  61215. static void test_dtls_downgrade_scr_server_ctx_ready(WOLFSSL_CTX* ctx)
  61216. {
  61217. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  61218. }
  61219. static void test_dtls_downgrade_scr_server_on_result(WOLFSSL* ssl)
  61220. {
  61221. char testMsg[] = "Message after SCR";
  61222. char msgBuf[sizeof(testMsg)];
  61223. if (wolfSSL_is_server(ssl)) {
  61224. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  61225. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  61226. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  61227. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  61228. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  61229. sizeof(testMsg));
  61230. }
  61231. else {
  61232. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  61233. sizeof(testMsg));
  61234. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  61235. }
  61236. }
  61237. static int test_dtls_downgrade_scr_server(void)
  61238. {
  61239. EXPECT_DECLS;
  61240. callback_functions func_cb_client;
  61241. callback_functions func_cb_server;
  61242. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  61243. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  61244. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  61245. func_cb_client.method = wolfDTLSv1_2_client_method;
  61246. func_cb_server.method = wolfDTLS_server_method;
  61247. func_cb_client.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready;
  61248. func_cb_server.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready_server;
  61249. func_cb_client.on_result = test_dtls_downgrade_scr_server_on_result;
  61250. func_cb_server.on_result = test_dtls_downgrade_scr_server_on_result;
  61251. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61252. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61253. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61254. return EXPECT_RESULT();
  61255. }
  61256. #else
  61257. static int test_dtls_downgrade_scr_server(void)
  61258. {
  61259. EXPECT_DECLS;
  61260. return EXPECT_RESULT();
  61261. }
  61262. #endif
  61263. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  61264. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  61265. static void test_dtls_downgrade_scr_ctx_ready(WOLFSSL_CTX* ctx)
  61266. {
  61267. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  61268. WOLFSSL_SUCCESS);
  61269. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  61270. }
  61271. static void test_dtls_downgrade_scr_on_result(WOLFSSL* ssl)
  61272. {
  61273. char testMsg[] = "Message after SCR";
  61274. char msgBuf[sizeof(testMsg)];
  61275. if (wolfSSL_is_server(ssl)) {
  61276. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  61277. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  61278. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  61279. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  61280. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  61281. sizeof(testMsg));
  61282. }
  61283. else {
  61284. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  61285. sizeof(testMsg));
  61286. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  61287. }
  61288. }
  61289. static int test_dtls_downgrade_scr(void)
  61290. {
  61291. EXPECT_DECLS;
  61292. callback_functions func_cb_client;
  61293. callback_functions func_cb_server;
  61294. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  61295. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  61296. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  61297. func_cb_client.method = wolfDTLS_client_method;
  61298. func_cb_server.method = wolfDTLSv1_2_server_method;
  61299. func_cb_client.ctx_ready = test_dtls_downgrade_scr_ctx_ready;
  61300. func_cb_client.on_result = test_dtls_downgrade_scr_on_result;
  61301. func_cb_server.on_result = test_dtls_downgrade_scr_on_result;
  61302. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61303. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61304. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61305. return EXPECT_RESULT();
  61306. }
  61307. #else
  61308. static int test_dtls_downgrade_scr(void)
  61309. {
  61310. EXPECT_DECLS;
  61311. return EXPECT_RESULT();
  61312. }
  61313. #endif
  61314. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  61315. && !defined(WOLFSSL_NO_TLS12)
  61316. static int test_dtls_client_hello_timeout_downgrade_read_cb(WOLFSSL *ssl,
  61317. char *data, int sz, void *ctx)
  61318. {
  61319. static int call_counter = 0;
  61320. call_counter++;
  61321. (void)ssl;
  61322. (void)data;
  61323. (void)sz;
  61324. (void)ctx;
  61325. switch (call_counter) {
  61326. case 1:
  61327. case 2:
  61328. return WOLFSSL_CBIO_ERR_TIMEOUT;
  61329. case 3:
  61330. return WOLFSSL_CBIO_ERR_WANT_READ;
  61331. default:
  61332. AssertIntLE(call_counter, 3);
  61333. return -1;
  61334. }
  61335. }
  61336. #endif
  61337. /* Make sure we don't send acks before getting a server hello */
  61338. static int test_dtls_client_hello_timeout_downgrade(void)
  61339. {
  61340. EXPECT_DECLS;
  61341. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  61342. && !defined(WOLFSSL_NO_TLS12)
  61343. WOLFSSL_CTX *ctx_c = NULL;
  61344. WOLFSSL_CTX *ctx_s = NULL;
  61345. WOLFSSL *ssl_c = NULL;
  61346. WOLFSSL *ssl_s = NULL;
  61347. struct test_memio_ctx test_ctx;
  61348. DtlsRecordLayerHeader* dtlsRH;
  61349. size_t len;
  61350. byte sequence_number[8];
  61351. int i;
  61352. for (i = 0; i < 2; i++) {
  61353. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61354. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61355. wolfDTLS_client_method, wolfDTLSv1_2_server_method), 0);
  61356. if (i == 0) {
  61357. /* First time simulate timeout in IO layer */
  61358. /* CH1 */
  61359. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61360. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61361. /* HVR */
  61362. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61363. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61364. /* CH2 */
  61365. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61366. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61367. /* SH flight */
  61368. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61369. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61370. /* Drop the SH */
  61371. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  61372. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61373. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  61374. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  61375. (sizeof(DtlsRecordLayerHeader) + len));
  61376. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  61377. /* Read the remainder of the flight */
  61378. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61379. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61380. wolfSSL_SSLSetIORecv(ssl_c,
  61381. test_dtls_client_hello_timeout_downgrade_read_cb);
  61382. /* CH3 */
  61383. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61384. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61385. wolfSSL_SSLSetIORecv(ssl_c, test_memio_read_cb);
  61386. }
  61387. else {
  61388. /* Second time call wolfSSL_dtls_got_timeout */
  61389. /* CH1 */
  61390. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61391. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61392. /* HVR */
  61393. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61394. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61395. /* CH2 */
  61396. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61397. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61398. /* SH flight */
  61399. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61400. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61401. /* Drop the SH */
  61402. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  61403. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61404. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  61405. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  61406. (sizeof(DtlsRecordLayerHeader) + len));
  61407. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  61408. /* Read the remainder of the flight */
  61409. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61410. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61411. /* Quick timeout should be set as we received at least one msg */
  61412. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 1);
  61413. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  61414. /* Quick timeout should be cleared after a quick timeout */
  61415. /* CH3 */
  61416. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 0);
  61417. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  61418. }
  61419. /* Parse out to make sure we got exactly one ClientHello message */
  61420. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  61421. /* Second ClientHello after HVR */
  61422. sequence_number[7] = 2;
  61423. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.s_buff;
  61424. ExpectIntEQ(dtlsRH->type, handshake);
  61425. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  61426. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  61427. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  61428. sizeof(sequence_number)), 0);
  61429. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61430. ExpectIntEQ(sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  61431. /* Connection should be able to continue */
  61432. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61433. wolfSSL_free(ssl_c);
  61434. wolfSSL_free(ssl_s);
  61435. wolfSSL_CTX_free(ctx_c);
  61436. wolfSSL_CTX_free(ctx_s);
  61437. ssl_c = NULL;
  61438. ssl_s = NULL;
  61439. ctx_c = NULL;
  61440. ctx_s = NULL;
  61441. if (!EXPECT_SUCCESS())
  61442. break;
  61443. }
  61444. #endif
  61445. return EXPECT_RESULT();
  61446. }
  61447. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  61448. static int test_dtls_client_hello_timeout_read_cb(WOLFSSL *ssl, char *data,
  61449. int sz, void *ctx)
  61450. {
  61451. static int call_counter = 0;
  61452. call_counter++;
  61453. (void)ssl;
  61454. (void)data;
  61455. (void)sz;
  61456. (void)ctx;
  61457. switch (call_counter) {
  61458. case 1:
  61459. return WOLFSSL_CBIO_ERR_TIMEOUT;
  61460. case 2:
  61461. return WOLFSSL_CBIO_ERR_WANT_READ;
  61462. default:
  61463. AssertIntLE(call_counter, 2);
  61464. return -1;
  61465. }
  61466. }
  61467. #endif
  61468. /* Make sure we don't send acks before getting a server hello */
  61469. static int test_dtls_client_hello_timeout(void)
  61470. {
  61471. EXPECT_DECLS;
  61472. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  61473. WOLFSSL *ssl_c = NULL;
  61474. WOLFSSL_CTX *ctx_c = NULL;
  61475. struct test_memio_ctx test_ctx;
  61476. DtlsRecordLayerHeader* dtlsRH;
  61477. size_t idx;
  61478. size_t len;
  61479. byte sequence_number[8];
  61480. int i;
  61481. for (i = 0; i < 2; i++) {
  61482. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61483. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  61484. wolfDTLSv1_3_client_method, NULL), 0);
  61485. if (i == 0) {
  61486. /* First time simulate timeout in IO layer */
  61487. wolfSSL_SSLSetIORecv(ssl_c, test_dtls_client_hello_timeout_read_cb);
  61488. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  61489. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61490. }
  61491. else {
  61492. /* Second time call wolfSSL_dtls_got_timeout */
  61493. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  61494. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61495. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  61496. }
  61497. /* Parse out to make sure we got exactly two ClientHello messages */
  61498. idx = 0;
  61499. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  61500. /* First ClientHello */
  61501. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  61502. ExpectIntEQ(dtlsRH->type, handshake);
  61503. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  61504. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  61505. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  61506. sizeof(sequence_number)), 0);
  61507. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61508. ExpectIntLT(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  61509. idx += sizeof(DtlsRecordLayerHeader) + len;
  61510. /* Second ClientHello */
  61511. sequence_number[7] = 1;
  61512. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  61513. ExpectIntEQ(dtlsRH->type, handshake);
  61514. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  61515. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  61516. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  61517. sizeof(sequence_number)), 0);
  61518. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61519. ExpectIntEQ(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  61520. wolfSSL_free(ssl_c);
  61521. wolfSSL_CTX_free(ctx_c);
  61522. ssl_c = NULL;
  61523. ctx_c = NULL;
  61524. if (!EXPECT_SUCCESS())
  61525. break;
  61526. }
  61527. #endif
  61528. return EXPECT_RESULT();
  61529. }
  61530. /* DTLS test when dropping the changed cipher spec message */
  61531. static int test_dtls_dropped_ccs(void)
  61532. {
  61533. EXPECT_DECLS;
  61534. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  61535. && !defined(WOLFSSL_NO_TLS12)
  61536. WOLFSSL_CTX *ctx_c = NULL;
  61537. WOLFSSL_CTX *ctx_s = NULL;
  61538. WOLFSSL *ssl_c = NULL;
  61539. WOLFSSL *ssl_s = NULL;
  61540. struct test_memio_ctx test_ctx;
  61541. DtlsRecordLayerHeader* dtlsRH;
  61542. size_t len;
  61543. byte data[1];
  61544. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61545. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61546. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  61547. /* CH1 */
  61548. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61549. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61550. /* HVR */
  61551. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61552. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61553. /* CH2 */
  61554. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61555. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61556. /* Server first flight */
  61557. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61558. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61559. /* Client flight */
  61560. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61561. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61562. /* Server ccs + finished */
  61563. ExpectIntEQ(wolfSSL_negotiate(ssl_s), 1);
  61564. /* Drop the ccs */
  61565. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  61566. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61567. ExpectIntEQ(len, 1);
  61568. ExpectIntEQ(dtlsRH->type, change_cipher_spec);
  61569. if (EXPECT_SUCCESS()) {
  61570. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  61571. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  61572. (sizeof(DtlsRecordLayerHeader) + len));
  61573. }
  61574. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  61575. /* Client rtx flight */
  61576. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61577. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61578. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  61579. /* Server ccs + finished rtx */
  61580. ExpectIntEQ(wolfSSL_read(ssl_s, data, sizeof(data)), -1);
  61581. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61582. /* Client processes finished */
  61583. ExpectIntEQ(wolfSSL_negotiate(ssl_c), 1);
  61584. wolfSSL_free(ssl_c);
  61585. wolfSSL_free(ssl_s);
  61586. wolfSSL_CTX_free(ctx_c);
  61587. wolfSSL_CTX_free(ctx_s);
  61588. #endif
  61589. return EXPECT_RESULT();
  61590. }
  61591. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  61592. && !defined(WOLFSSL_NO_TLS12)
  61593. static int test_dtls_seq_num_downgrade_check_num(byte* ioBuf, int ioBufLen,
  61594. byte seq_num)
  61595. {
  61596. EXPECT_DECLS;
  61597. DtlsRecordLayerHeader* dtlsRH;
  61598. byte sequence_number[8];
  61599. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  61600. ExpectIntGE(ioBufLen, sizeof(*dtlsRH));
  61601. dtlsRH = (DtlsRecordLayerHeader*)ioBuf;
  61602. ExpectIntEQ(dtlsRH->type, handshake);
  61603. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  61604. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  61605. sequence_number[7] = seq_num;
  61606. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  61607. sizeof(sequence_number)), 0);
  61608. return EXPECT_RESULT();
  61609. }
  61610. #endif
  61611. /*
  61612. * Make sure that we send the correct sequence number after a HelloVerifyRequest
  61613. * and after a HelloRetryRequest. This is testing the server side as it is
  61614. * operating statelessly and should copy the sequence number of the ClientHello.
  61615. */
  61616. static int test_dtls_seq_num_downgrade(void)
  61617. {
  61618. EXPECT_DECLS;
  61619. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  61620. && !defined(WOLFSSL_NO_TLS12)
  61621. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  61622. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  61623. struct test_memio_ctx test_ctx;
  61624. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61625. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61626. wolfDTLSv1_2_client_method, wolfDTLS_server_method), 0);
  61627. /* CH1 */
  61628. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61629. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61630. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.s_buff,
  61631. test_ctx.s_len, 0), TEST_SUCCESS);
  61632. /* HVR */
  61633. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61634. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61635. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.c_buff,
  61636. test_ctx.c_len, 0), TEST_SUCCESS);
  61637. /* CH2 */
  61638. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61639. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61640. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.s_buff,
  61641. test_ctx.s_len, 1), TEST_SUCCESS);
  61642. /* Server first flight */
  61643. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61644. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61645. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.c_buff,
  61646. test_ctx.c_len, 1), TEST_SUCCESS);
  61647. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61648. wolfSSL_free(ssl_c);
  61649. wolfSSL_CTX_free(ctx_c);
  61650. wolfSSL_free(ssl_s);
  61651. wolfSSL_CTX_free(ctx_s);
  61652. #endif
  61653. return EXPECT_RESULT();
  61654. }
  61655. /**
  61656. * Make sure we don't send RSA Signature Hash Algorithms in the
  61657. * CertificateRequest when we don't have any such ciphers set.
  61658. * @return EXPECT_RESULT()
  61659. */
  61660. static int test_certreq_sighash_algos(void)
  61661. {
  61662. EXPECT_DECLS;
  61663. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  61664. !defined(WOLFSSL_MAX_STRENGTH) && defined(HAVE_ECC) && \
  61665. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  61666. defined(HAVE_AES_CBC) && !defined(WOLFSSL_NO_TLS12)
  61667. WOLFSSL_CTX *ctx_c = NULL;
  61668. WOLFSSL_CTX *ctx_s = NULL;
  61669. WOLFSSL *ssl_c = NULL;
  61670. WOLFSSL *ssl_s = NULL;
  61671. struct test_memio_ctx test_ctx;
  61672. int idx = 0;
  61673. int maxIdx = 0;
  61674. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61675. test_ctx.c_ciphers = test_ctx.s_ciphers =
  61676. "ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA384";
  61677. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61678. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  61679. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx_c,
  61680. "./certs/ca-ecc-cert.pem", NULL), WOLFSSL_SUCCESS);
  61681. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_PEER, NULL);
  61682. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ssl_s, "./certs/ecc-key.pem",
  61683. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61684. ExpectIntEQ(wolfSSL_use_certificate_file(ssl_s, "./certs/server-ecc.pem",
  61685. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61686. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_FATAL_ERROR);
  61687. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61688. WOLFSSL_ERROR_WANT_READ);
  61689. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_FATAL_ERROR);
  61690. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  61691. WOLFSSL_ERROR_WANT_READ);
  61692. /* Find the CertificateRequest message */
  61693. for (idx = 0; idx < test_ctx.c_len && EXPECT_SUCCESS();) {
  61694. word16 len;
  61695. ExpectIntEQ(test_ctx.c_buff[idx++], handshake);
  61696. ExpectIntEQ(test_ctx.c_buff[idx++], SSLv3_MAJOR);
  61697. ExpectIntEQ(test_ctx.c_buff[idx++], TLSv1_2_MINOR);
  61698. ato16(test_ctx.c_buff + idx, &len);
  61699. idx += OPAQUE16_LEN;
  61700. if (test_ctx.c_buff[idx] == certificate_request) {
  61701. idx++;
  61702. /* length */
  61703. idx += OPAQUE24_LEN;
  61704. /* cert types */
  61705. idx += 1 + test_ctx.c_buff[idx];
  61706. /* Sig algos */
  61707. ato16(test_ctx.c_buff + idx, &len);
  61708. idx += OPAQUE16_LEN;
  61709. maxIdx = idx + (int)len;
  61710. for (; idx < maxIdx && EXPECT_SUCCESS(); idx += OPAQUE16_LEN) {
  61711. if (test_ctx.c_buff[idx+1] == ED25519_SA_MINOR ||
  61712. test_ctx.c_buff[idx+1] == ED448_SA_MINOR)
  61713. ExpectIntEQ(test_ctx.c_buff[idx], NEW_SA_MAJOR);
  61714. else
  61715. ExpectIntEQ(test_ctx.c_buff[idx+1], ecc_dsa_sa_algo);
  61716. }
  61717. break;
  61718. }
  61719. else {
  61720. idx += (int)len;
  61721. }
  61722. }
  61723. ExpectIntLT(idx, test_ctx.c_len);
  61724. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61725. wolfSSL_free(ssl_c);
  61726. wolfSSL_free(ssl_s);
  61727. wolfSSL_CTX_free(ctx_c);
  61728. wolfSSL_CTX_free(ctx_s);
  61729. #endif
  61730. return EXPECT_RESULT();
  61731. }
  61732. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  61733. static int test_revoked_loaded_int_cert_ctx_ready1(WOLFSSL_CTX* ctx)
  61734. {
  61735. EXPECT_DECLS;
  61736. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  61737. myVerifyAction = VERIFY_USE_PREVERFIY;
  61738. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  61739. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  61740. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  61741. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  61742. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  61743. WOLFSSL_SUCCESS);
  61744. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  61745. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  61746. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61747. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  61748. "./certs/crl/ca-int.pem",
  61749. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61750. return EXPECT_RESULT();
  61751. }
  61752. static int test_revoked_loaded_int_cert_ctx_ready2(WOLFSSL_CTX* ctx)
  61753. {
  61754. EXPECT_DECLS;
  61755. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  61756. myVerifyAction = VERIFY_USE_PREVERFIY;
  61757. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  61758. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  61759. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  61760. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  61761. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  61762. "./certs/intermediate/ca-int2-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  61763. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  61764. WOLFSSL_SUCCESS);
  61765. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  61766. "./certs/crl/ca-int2.pem",
  61767. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61768. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  61769. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  61770. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61771. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  61772. "./certs/crl/ca-int.pem",
  61773. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61774. return EXPECT_RESULT();
  61775. }
  61776. #endif
  61777. static int test_revoked_loaded_int_cert(void)
  61778. {
  61779. EXPECT_DECLS;
  61780. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  61781. test_ssl_cbf client_cbf;
  61782. test_ssl_cbf server_cbf;
  61783. struct {
  61784. const char* certPemFile;
  61785. const char* keyPemFile;
  61786. ctx_cb client_ctx_ready;
  61787. } test_params[] = {
  61788. {"./certs/intermediate/ca-int2-cert.pem",
  61789. "./certs/intermediate/ca-int2-key.pem",
  61790. test_revoked_loaded_int_cert_ctx_ready1},
  61791. {"./certs/intermediate/server-chain.pem",
  61792. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  61793. {"./certs/intermediate/server-chain-short.pem",
  61794. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  61795. };
  61796. size_t i;
  61797. printf("\n");
  61798. for (i = 0; i < XELEM_CNT(test_params); i++) {
  61799. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  61800. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  61801. printf("\tTesting with %s...\n", test_params[i].certPemFile);
  61802. server_cbf.certPemFile = test_params[i].certPemFile;
  61803. server_cbf.keyPemFile = test_params[i].keyPemFile;
  61804. client_cbf.ctx_ready = test_params[i].client_ctx_ready;
  61805. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  61806. &server_cbf, NULL), TEST_FAIL);
  61807. #ifndef WOLFSSL_HAPROXY
  61808. ExpectIntEQ(client_cbf.last_err, CRL_CERT_REVOKED);
  61809. #else
  61810. ExpectIntEQ(client_cbf.last_err, WOLFSSL_X509_V_ERR_CERT_REVOKED);
  61811. #endif
  61812. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  61813. if (!EXPECT_SUCCESS())
  61814. break;
  61815. printf("\t%s passed\n", test_params[i].certPemFile);
  61816. }
  61817. #endif
  61818. return EXPECT_RESULT();
  61819. }
  61820. static int test_dtls13_frag_ch_pq(void)
  61821. {
  61822. EXPECT_DECLS;
  61823. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  61824. && defined(WOLFSSL_DTLS_CH_FRAG) && defined(HAVE_LIBOQS)
  61825. WOLFSSL_CTX *ctx_c = NULL;
  61826. WOLFSSL_CTX *ctx_s = NULL;
  61827. WOLFSSL *ssl_c = NULL;
  61828. WOLFSSL *ssl_s = NULL;
  61829. struct test_memio_ctx test_ctx;
  61830. const char *test_str = "test";
  61831. int test_str_size;
  61832. byte buf[255];
  61833. int group = WOLFSSL_KYBER_LEVEL5;
  61834. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61835. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61836. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  61837. /* Add in a large post-quantum key share to make the CH long. */
  61838. ExpectIntEQ(wolfSSL_set_groups(ssl_c, &group, 1), WOLFSSL_SUCCESS);
  61839. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, group), WOLFSSL_SUCCESS);
  61840. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  61841. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61842. ExpectStrEQ(wolfSSL_get_curve_name(ssl_c), "KYBER_LEVEL5");
  61843. ExpectStrEQ(wolfSSL_get_curve_name(ssl_s), "KYBER_LEVEL5");
  61844. test_str_size = XSTRLEN("test") + 1;
  61845. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  61846. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  61847. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  61848. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  61849. wolfSSL_free(ssl_c);
  61850. wolfSSL_free(ssl_s);
  61851. wolfSSL_CTX_free(ctx_c);
  61852. wolfSSL_CTX_free(ctx_s);
  61853. #endif
  61854. return EXPECT_RESULT();
  61855. }
  61856. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  61857. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  61858. static int test_dtls_frag_ch_count_records(byte* b, int len)
  61859. {
  61860. DtlsRecordLayerHeader* dtlsRH;
  61861. int records = 0;
  61862. size_t recordLen;
  61863. while (len > 0) {
  61864. records++;
  61865. dtlsRH = (DtlsRecordLayerHeader*)b;
  61866. recordLen = (dtlsRH->length[0] << 8) | dtlsRH->length[1];
  61867. b += sizeof(DtlsRecordLayerHeader) + recordLen;
  61868. len -= sizeof(DtlsRecordLayerHeader) + recordLen;
  61869. }
  61870. return records;
  61871. }
  61872. #endif
  61873. static int test_dtls_frag_ch(void)
  61874. {
  61875. EXPECT_DECLS;
  61876. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  61877. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  61878. WOLFSSL_CTX *ctx_c = NULL;
  61879. WOLFSSL_CTX *ctx_s = NULL;
  61880. WOLFSSL *ssl_c = NULL;
  61881. WOLFSSL *ssl_s = NULL;
  61882. struct test_memio_ctx test_ctx;
  61883. static unsigned int DUMMY_MTU = 256;
  61884. unsigned char four_frag_CH[] = {
  61885. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61886. 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61887. 0xce, 0xfe, 0xfd, 0xf3, 0x94, 0x01, 0x33, 0x2c, 0xcf, 0x2c, 0x47, 0xb1,
  61888. 0xe5, 0xa1, 0x7b, 0x19, 0x3e, 0xac, 0x68, 0xdd, 0xe6, 0x17, 0x6b, 0x85,
  61889. 0xad, 0x5f, 0xfc, 0x7f, 0x6e, 0xf0, 0xb9, 0xe0, 0x2e, 0xca, 0x47, 0x00,
  61890. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  61891. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  61892. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  61893. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  61894. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x02,
  61895. 0x7c, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  61896. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  61897. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  61898. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  61899. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  61900. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x02, 0x39, 0x02, 0x37, 0x00, 0x17,
  61901. 0x00, 0x41, 0x04, 0x94, 0xdf, 0x36, 0xd7, 0xb3, 0x90, 0x6d, 0x01, 0xa1,
  61902. 0xe6, 0xed, 0x67, 0xf4, 0xd9, 0x9d, 0x2c, 0xac, 0x57, 0x74, 0xff, 0x19,
  61903. 0xbe, 0x5a, 0xc9, 0x30, 0x11, 0xb7, 0x2b, 0x59, 0x47, 0x80, 0x7c, 0xa9,
  61904. 0xb7, 0x31, 0x8c, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61905. 0x00, 0x01, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00,
  61906. 0xce, 0x00, 0x00, 0xce, 0x9e, 0x13, 0x74, 0x3b, 0x86, 0xba, 0x69, 0x1f,
  61907. 0x12, 0xf7, 0xcd, 0x78, 0x53, 0xe8, 0x50, 0x4d, 0x71, 0x3f, 0x4b, 0x4e,
  61908. 0xeb, 0x3e, 0xe5, 0x43, 0x54, 0x78, 0x17, 0x6d, 0x00, 0x18, 0x00, 0x61,
  61909. 0x04, 0xd1, 0x99, 0x66, 0x4f, 0xda, 0xc7, 0x12, 0x3b, 0xff, 0xb2, 0xd6,
  61910. 0x2f, 0x35, 0xb6, 0x17, 0x1f, 0xb3, 0xd0, 0xb6, 0x52, 0xff, 0x97, 0x8b,
  61911. 0x01, 0xe8, 0xd9, 0x68, 0x71, 0x40, 0x02, 0xd5, 0x68, 0x3a, 0x58, 0xb2,
  61912. 0x5d, 0xee, 0xa4, 0xe9, 0x5f, 0xf4, 0xaf, 0x3e, 0x30, 0x9c, 0x3e, 0x2b,
  61913. 0xda, 0x61, 0x43, 0x99, 0x02, 0x35, 0x33, 0x9f, 0xcf, 0xb5, 0xd3, 0x28,
  61914. 0x19, 0x9d, 0x1c, 0xbe, 0x69, 0x07, 0x9e, 0xfc, 0xe4, 0x8e, 0xcd, 0x86,
  61915. 0x4a, 0x1b, 0xf0, 0xfc, 0x17, 0x94, 0x66, 0x53, 0xda, 0x24, 0x5e, 0xaf,
  61916. 0xce, 0xec, 0x62, 0x4c, 0x06, 0xb4, 0x52, 0x94, 0xb1, 0x4a, 0x7a, 0x8c,
  61917. 0x4f, 0x00, 0x19, 0x00, 0x85, 0x04, 0x00, 0x27, 0xeb, 0x99, 0x49, 0x7f,
  61918. 0xcb, 0x2c, 0x46, 0x54, 0x2d, 0x93, 0x5d, 0x25, 0x92, 0x58, 0x5e, 0x06,
  61919. 0xc3, 0x7c, 0xfb, 0x9a, 0xa7, 0xec, 0xcd, 0x9f, 0xe1, 0x6b, 0x2d, 0x78,
  61920. 0xf5, 0x16, 0xa9, 0x20, 0x52, 0x48, 0x19, 0x0f, 0x1a, 0xd0, 0xce, 0xd8,
  61921. 0x68, 0xb1, 0x4e, 0x7f, 0x33, 0x03, 0x7d, 0x0c, 0x39, 0xdb, 0x9c, 0x4b,
  61922. 0xf4, 0xe7, 0xc2, 0xf5, 0xdd, 0x51, 0x9b, 0x03, 0xa8, 0x53, 0x2b, 0xe6,
  61923. 0x00, 0x15, 0x4b, 0xff, 0xd2, 0xa0, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00,
  61924. 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00,
  61925. 0x00, 0x00, 0x01, 0x9c, 0x00, 0x00, 0xce, 0x58, 0x30, 0x10, 0x3d, 0x46,
  61926. 0xcc, 0xca, 0x1a, 0x44, 0xc8, 0x58, 0x9b, 0x27, 0x17, 0x67, 0x31, 0x96,
  61927. 0x8a, 0x66, 0x39, 0xf4, 0xcc, 0xc1, 0x9f, 0x12, 0x1f, 0x01, 0x30, 0x50,
  61928. 0x16, 0xd6, 0x89, 0x97, 0xa3, 0x66, 0xd7, 0x99, 0x50, 0x09, 0x6e, 0x80,
  61929. 0x87, 0xe4, 0xa2, 0x88, 0xae, 0xb4, 0x23, 0x57, 0x2f, 0x12, 0x60, 0xe7,
  61930. 0x7d, 0x44, 0x2d, 0xad, 0xbe, 0xe9, 0x0d, 0x01, 0x00, 0x01, 0x00, 0xd5,
  61931. 0xdd, 0x62, 0xee, 0xf3, 0x0e, 0xd9, 0x30, 0x0e, 0x38, 0xf3, 0x48, 0xf4,
  61932. 0xc9, 0x8f, 0x8c, 0x20, 0xf7, 0xd3, 0xa8, 0xb3, 0x87, 0x3c, 0x98, 0x5d,
  61933. 0x70, 0xc5, 0x03, 0x76, 0xb7, 0xd5, 0x0b, 0x7b, 0x23, 0x97, 0x6b, 0xe3,
  61934. 0xb5, 0x18, 0xeb, 0x64, 0x55, 0x18, 0xb2, 0x8a, 0x90, 0x1a, 0x8f, 0x0e,
  61935. 0x15, 0xda, 0xb1, 0x8e, 0x7f, 0xee, 0x1f, 0xe0, 0x3b, 0xb9, 0xed, 0xfc,
  61936. 0x4e, 0x3f, 0x78, 0x16, 0x39, 0x95, 0x5f, 0xb7, 0xcb, 0x65, 0x55, 0x72,
  61937. 0x7b, 0x7d, 0x86, 0x2f, 0x8a, 0xe5, 0xee, 0xf7, 0x57, 0x40, 0xf3, 0xc4,
  61938. 0x96, 0x4f, 0x11, 0x4d, 0x85, 0xf9, 0x56, 0xfa, 0x3d, 0xf0, 0xc9, 0xa4,
  61939. 0xec, 0x1e, 0xaa, 0x47, 0x90, 0x53, 0xdf, 0xe1, 0xb7, 0x78, 0x18, 0xeb,
  61940. 0xdd, 0x0d, 0x89, 0xb7, 0xf6, 0x15, 0x0e, 0x55, 0x12, 0xb3, 0x23, 0x17,
  61941. 0x0b, 0x59, 0x6f, 0x83, 0x05, 0x6b, 0xa6, 0xf8, 0x6c, 0x3a, 0x9b, 0x1b,
  61942. 0x50, 0x93, 0x51, 0xea, 0x95, 0x2d, 0x99, 0x96, 0x38, 0x16, 0xfe, 0xfd,
  61943. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x7e, 0x01, 0x00,
  61944. 0x02, 0xdc, 0x00, 0x00, 0x00, 0x02, 0x6a, 0x00, 0x00, 0x72, 0x2d, 0x66,
  61945. 0x3e, 0xf2, 0x36, 0x5a, 0xf2, 0x23, 0x8f, 0x28, 0x09, 0xa9, 0x55, 0x8c,
  61946. 0x8f, 0xc0, 0x0d, 0x61, 0x98, 0x33, 0x56, 0x87, 0x7a, 0xfd, 0xa7, 0x50,
  61947. 0x71, 0x84, 0x2e, 0x41, 0x58, 0x00, 0x87, 0xd9, 0x27, 0xe5, 0x7b, 0xf4,
  61948. 0x6d, 0x84, 0x4e, 0x2e, 0x0c, 0x80, 0x0c, 0xf3, 0x8a, 0x02, 0x4b, 0x99,
  61949. 0x3a, 0x1f, 0x9f, 0x18, 0x7d, 0x1c, 0xec, 0xad, 0x60, 0x54, 0xa6, 0xa3,
  61950. 0x2c, 0x82, 0x5e, 0xf8, 0x8f, 0xae, 0xe1, 0xc4, 0x82, 0x7e, 0x43, 0x43,
  61951. 0xc5, 0x99, 0x49, 0x05, 0xd3, 0xf6, 0xdf, 0xa1, 0xb5, 0x2d, 0x0c, 0x13,
  61952. 0x2f, 0x1e, 0xb6, 0x28, 0x7c, 0x5c, 0xa1, 0x02, 0x6b, 0x8d, 0xa3, 0xeb,
  61953. 0xd4, 0x58, 0xe6, 0xa0, 0x7e, 0x6b, 0xaa, 0x09, 0x43, 0x67, 0x71, 0x87,
  61954. 0xa5, 0xcb, 0x68, 0xf3
  61955. };
  61956. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61957. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61958. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  61959. /* Fragment msgs */
  61960. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_c, DUMMY_MTU), WOLFSSL_SUCCESS);
  61961. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_s, DUMMY_MTU), WOLFSSL_SUCCESS);
  61962. /* Add in some key shares to make the CH long */
  61963. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP256R1),
  61964. WOLFSSL_SUCCESS);
  61965. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP384R1),
  61966. WOLFSSL_SUCCESS);
  61967. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP521R1),
  61968. WOLFSSL_SUCCESS);
  61969. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_FFDHE_2048),
  61970. WOLFSSL_SUCCESS);
  61971. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  61972. /* Reject fragmented first CH */
  61973. ExpectIntEQ(test_dtls_frag_ch_count_records(four_frag_CH,
  61974. sizeof(four_frag_CH)), 4);
  61975. XMEMCPY(test_ctx.s_buff, four_frag_CH, sizeof(four_frag_CH));
  61976. test_ctx.s_len = sizeof(four_frag_CH);
  61977. while (test_ctx.s_len > 0 && EXPECT_SUCCESS()) {
  61978. int s_len = test_ctx.s_len;
  61979. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61980. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61981. /* Fail if we didn't advance the buffer to avoid infinite loops */
  61982. ExpectIntLT(test_ctx.s_len, s_len);
  61983. }
  61984. /* Expect all fragments read */
  61985. ExpectIntEQ(test_ctx.s_len, 0);
  61986. /* Expect quietly dropping fragmented first CH */
  61987. ExpectIntEQ(test_ctx.c_len, 0);
  61988. /* CH1 */
  61989. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61990. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61991. /* Count records. Expect 1 unfragmented CH */
  61992. ExpectIntEQ(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  61993. test_ctx.s_len), 1);
  61994. /* HRR */
  61995. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61996. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61997. /* CH2 */
  61998. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61999. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62000. /* Count records. Expect fragmented CH */
  62001. ExpectIntGT(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  62002. test_ctx.s_len), 1);
  62003. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62004. wolfSSL_free(ssl_c);
  62005. wolfSSL_free(ssl_s);
  62006. wolfSSL_CTX_free(ctx_c);
  62007. wolfSSL_CTX_free(ctx_s);
  62008. ssl_c = ssl_s = NULL;
  62009. ctx_c = ctx_s = NULL;
  62010. #endif
  62011. return EXPECT_RESULT();
  62012. }
  62013. static int test_dtls_empty_keyshare_with_cookie(void)
  62014. {
  62015. EXPECT_DECLS;
  62016. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  62017. WOLFSSL_CTX *ctx_s = NULL;
  62018. WOLFSSL *ssl_s = NULL;
  62019. struct test_memio_ctx test_ctx;
  62020. unsigned char ch_empty_keyshare_with_cookie[] = {
  62021. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  62022. 0x12, 0x01, 0x00, 0x01, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
  62023. 0x06, 0xfe, 0xfd, 0xfb, 0x8c, 0x9b, 0x28, 0xae, 0x50, 0x1c, 0x4d, 0xf3,
  62024. 0xb8, 0xcf, 0x4d, 0xd8, 0x7e, 0x93, 0x13, 0x7b, 0x9e, 0xd9, 0xeb, 0xe9,
  62025. 0x13, 0x4b, 0x0d, 0x7f, 0x2e, 0x43, 0x62, 0x8c, 0xe4, 0x57, 0x79, 0x00,
  62026. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  62027. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  62028. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  62029. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  62030. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x00,
  62031. 0xa6, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x2c, 0x00, 0x47,
  62032. 0x00, 0x45, 0x20, 0xee, 0x4b, 0x17, 0x70, 0x63, 0xa0, 0x4c, 0x82, 0xbf,
  62033. 0x43, 0x01, 0x7d, 0x8d, 0xc1, 0x1b, 0x4e, 0x9b, 0xa0, 0x3c, 0x53, 0x1f,
  62034. 0xb7, 0xd1, 0x10, 0x81, 0xa8, 0xdf, 0xdf, 0x8c, 0x7f, 0xf3, 0x11, 0x13,
  62035. 0x01, 0x02, 0x3d, 0x3b, 0x7d, 0x14, 0x2c, 0x31, 0xb3, 0x60, 0x72, 0x4d,
  62036. 0xe5, 0x1a, 0xb2, 0xa3, 0x61, 0x77, 0x73, 0x03, 0x40, 0x0e, 0x5f, 0xc5,
  62037. 0x61, 0x38, 0x43, 0x56, 0x21, 0x4a, 0x95, 0xd5, 0x35, 0xa8, 0x0d, 0x00,
  62038. 0x0d, 0x00, 0x2a, 0x00, 0x28, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02,
  62039. 0x03, 0xfe, 0x0b, 0xfe, 0x0e, 0xfe, 0xa0, 0xfe, 0xa3, 0xfe, 0xa5, 0x08,
  62040. 0x06, 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06,
  62041. 0x01, 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00,
  62042. 0x18, 0x00, 0x16, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01,
  62043. 0x00, 0x02, 0x3a, 0x02, 0x3c, 0x02, 0x3d, 0x2f, 0x3a, 0x2f, 0x3c, 0x2f,
  62044. 0x3d, 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x00, 0x02, 0x00, 0x00
  62045. };
  62046. DtlsRecordLayerHeader* dtlsRH;
  62047. byte sequence_number[8];
  62048. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  62049. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62050. XMEMCPY(test_ctx.s_buff, ch_empty_keyshare_with_cookie,
  62051. sizeof(ch_empty_keyshare_with_cookie));
  62052. test_ctx.s_len = sizeof(ch_empty_keyshare_with_cookie);
  62053. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  62054. NULL, wolfDTLSv1_3_server_method), 0);
  62055. /* CH1 */
  62056. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62057. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62058. /* Expect an alert. A plaintext alert should be exactly 15 bytes. */
  62059. ExpectIntEQ(test_ctx.c_len, 15);
  62060. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  62061. ExpectIntEQ(dtlsRH->type, alert);
  62062. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  62063. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  62064. sequence_number[7] = 1;
  62065. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  62066. sizeof(sequence_number)), 0);
  62067. ExpectIntEQ(dtlsRH->length[0], 0);
  62068. ExpectIntEQ(dtlsRH->length[1], 2);
  62069. ExpectIntEQ(test_ctx.c_buff[13], alert_fatal);
  62070. ExpectIntEQ(test_ctx.c_buff[14], illegal_parameter);
  62071. wolfSSL_free(ssl_s);
  62072. wolfSSL_CTX_free(ctx_s);
  62073. #endif
  62074. return EXPECT_RESULT();
  62075. }
  62076. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  62077. defined(HAVE_LIBOQS)
  62078. static void test_tls13_pq_groups_ctx_ready(WOLFSSL_CTX* ctx)
  62079. {
  62080. int group = WOLFSSL_KYBER_LEVEL5;
  62081. AssertIntEQ(wolfSSL_CTX_set_groups(ctx, &group, 1), WOLFSSL_SUCCESS);
  62082. }
  62083. static void test_tls13_pq_groups_on_result(WOLFSSL* ssl)
  62084. {
  62085. AssertStrEQ(wolfSSL_get_curve_name(ssl), "KYBER_LEVEL5");
  62086. }
  62087. #endif
  62088. static int test_tls13_pq_groups(void)
  62089. {
  62090. EXPECT_DECLS;
  62091. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  62092. defined(HAVE_LIBOQS)
  62093. callback_functions func_cb_client;
  62094. callback_functions func_cb_server;
  62095. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  62096. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  62097. func_cb_client.method = wolfTLSv1_3_client_method;
  62098. func_cb_server.method = wolfTLSv1_3_server_method;
  62099. func_cb_client.ctx_ready = test_tls13_pq_groups_ctx_ready;
  62100. func_cb_client.on_result = test_tls13_pq_groups_on_result;
  62101. func_cb_server.on_result = test_tls13_pq_groups_on_result;
  62102. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  62103. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  62104. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  62105. #endif
  62106. return EXPECT_RESULT();
  62107. }
  62108. static int test_tls13_early_data(void)
  62109. {
  62110. EXPECT_DECLS;
  62111. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  62112. defined(WOLFSSL_EARLY_DATA) && defined(HAVE_SESSION_TICKET)
  62113. int written = 0;
  62114. int read = 0;
  62115. size_t i;
  62116. int splitEarlyData;
  62117. char msg[] = "This is early data";
  62118. char msg2[] = "This is client data";
  62119. char msg3[] = "This is server data";
  62120. char msg4[] = "This is server immediate data";
  62121. char msgBuf[50];
  62122. struct {
  62123. method_provider client_meth;
  62124. method_provider server_meth;
  62125. const char* tls_version;
  62126. int isUdp;
  62127. } params[] = {
  62128. #ifdef WOLFSSL_TLS13
  62129. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  62130. "TLS 1.3", 0 },
  62131. #endif
  62132. #ifdef WOLFSSL_DTLS13
  62133. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  62134. "DTLS 1.3", 1 },
  62135. #endif
  62136. };
  62137. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  62138. for (splitEarlyData = 0; splitEarlyData < 2; splitEarlyData++) {
  62139. struct test_memio_ctx test_ctx;
  62140. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  62141. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  62142. WOLFSSL_SESSION *sess = NULL;
  62143. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62144. fprintf(stderr, "\tEarly data with %s\n", params[i].tls_version);
  62145. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  62146. &ssl_s, params[i].client_meth, params[i].server_meth), 0);
  62147. /* Get a ticket so that we can do 0-RTT on the next connection */
  62148. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62149. /* Make sure we read the ticket */
  62150. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), -1);
  62151. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62152. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  62153. wolfSSL_free(ssl_c);
  62154. ssl_c = NULL;
  62155. wolfSSL_free(ssl_s);
  62156. ssl_s = NULL;
  62157. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62158. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62159. params[i].client_meth, params[i].server_meth), 0);
  62160. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  62161. #ifdef WOLFSSL_DTLS13
  62162. if (params[i].isUdp) {
  62163. #ifdef WOLFSSL_DTLS13_NO_HRR_ON_RESUME
  62164. ExpectIntEQ(wolfSSL_dtls13_no_hrr_on_resume(ssl_s, 1), WOLFSSL_SUCCESS);
  62165. #else
  62166. /* Let's test this but we generally don't recommend turning off the
  62167. * cookie exchange */
  62168. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  62169. #endif
  62170. }
  62171. #endif
  62172. /* Test 0-RTT data */
  62173. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  62174. &written), sizeof(msg));
  62175. ExpectIntEQ(written, sizeof(msg));
  62176. if (splitEarlyData) {
  62177. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  62178. &written), sizeof(msg));
  62179. ExpectIntEQ(written, sizeof(msg));
  62180. }
  62181. /* Read first 0-RTT data (if split otherwise entire data) */
  62182. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  62183. &read), sizeof(msg));
  62184. ExpectIntEQ(read, sizeof(msg));
  62185. ExpectStrEQ(msg, msgBuf);
  62186. /* Test 0.5-RTT data */
  62187. ExpectIntEQ(wolfSSL_write(ssl_s, msg4, sizeof(msg4)), sizeof(msg4));
  62188. if (splitEarlyData) {
  62189. /* Read second 0-RTT data */
  62190. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  62191. &read), sizeof(msg));
  62192. ExpectIntEQ(read, sizeof(msg));
  62193. ExpectStrEQ(msg, msgBuf);
  62194. }
  62195. if (params[i].isUdp) {
  62196. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  62197. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), APP_DATA_READY);
  62198. /* Read server 0.5-RTT data */
  62199. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  62200. ExpectStrEQ(msg4, msgBuf);
  62201. /* Complete handshake */
  62202. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  62203. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62204. /* Use wolfSSL_is_init_finished to check if handshake is complete. Normally
  62205. * a user would loop until it is true but here we control both sides so we
  62206. * just assert the expected value. wolfSSL_read_early_data does not provide
  62207. * handshake status to us with non-blocking IO and we can't use
  62208. * wolfSSL_accept as TLS layer may return ZERO_RETURN due to early data
  62209. * parsing logic. */
  62210. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  62211. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  62212. &read), 0);
  62213. ExpectIntEQ(read, 0);
  62214. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  62215. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  62216. }
  62217. else {
  62218. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  62219. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  62220. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  62221. &read), 0);
  62222. ExpectIntEQ(read, 0);
  62223. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  62224. /* Read server 0.5-RTT data */
  62225. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  62226. ExpectStrEQ(msg4, msgBuf);
  62227. }
  62228. /* Test bi-directional write */
  62229. ExpectIntEQ(wolfSSL_write(ssl_c, msg2, sizeof(msg2)), sizeof(msg2));
  62230. ExpectIntEQ(wolfSSL_read(ssl_s, msgBuf, sizeof(msgBuf)), sizeof(msg2));
  62231. ExpectStrEQ(msg2, msgBuf);
  62232. ExpectIntEQ(wolfSSL_write(ssl_s, msg3, sizeof(msg3)), sizeof(msg3));
  62233. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg3));
  62234. ExpectStrEQ(msg3, msgBuf);
  62235. ExpectTrue(wolfSSL_session_reused(ssl_c));
  62236. ExpectTrue(wolfSSL_session_reused(ssl_s));
  62237. wolfSSL_SESSION_free(sess);
  62238. wolfSSL_free(ssl_c);
  62239. wolfSSL_free(ssl_s);
  62240. wolfSSL_CTX_free(ctx_c);
  62241. wolfSSL_CTX_free(ctx_s);
  62242. }
  62243. }
  62244. #endif
  62245. return EXPECT_RESULT();
  62246. }
  62247. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  62248. static int test_self_signed_stapling_client_v1_ctx_ready(WOLFSSL_CTX* ctx)
  62249. {
  62250. EXPECT_DECLS;
  62251. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  62252. ExpectIntEQ(wolfSSL_CTX_UseOCSPStapling(ctx, WOLFSSL_CSR_OCSP,
  62253. WOLFSSL_CSR_OCSP_USE_NONCE), 1);
  62254. return EXPECT_RESULT();
  62255. }
  62256. #endif
  62257. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  62258. static int test_self_signed_stapling_client_v2_ctx_ready(WOLFSSL_CTX* ctx)
  62259. {
  62260. EXPECT_DECLS;
  62261. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  62262. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP,
  62263. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  62264. return EXPECT_RESULT();
  62265. }
  62266. static int test_self_signed_stapling_client_v2_multi_ctx_ready(WOLFSSL_CTX* ctx)
  62267. {
  62268. EXPECT_DECLS;
  62269. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  62270. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP_MULTI,
  62271. 0), 1);
  62272. return EXPECT_RESULT();
  62273. }
  62274. #endif
  62275. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  62276. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  62277. static int test_self_signed_stapling_server_ctx_ready(WOLFSSL_CTX* ctx)
  62278. {
  62279. EXPECT_DECLS;
  62280. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  62281. return EXPECT_RESULT();
  62282. }
  62283. #endif
  62284. static int test_self_signed_stapling(void)
  62285. {
  62286. EXPECT_DECLS;
  62287. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  62288. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  62289. test_ssl_cbf client_cbf;
  62290. test_ssl_cbf server_cbf;
  62291. size_t i;
  62292. struct {
  62293. method_provider client_meth;
  62294. method_provider server_meth;
  62295. ctx_cb client_ctx;
  62296. const char* tls_version;
  62297. } params[] = {
  62298. #if defined(WOLFSSL_TLS13) && defined(HAVE_CERTIFICATE_STATUS_REQUEST)
  62299. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  62300. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_3 v1" },
  62301. #endif
  62302. #ifndef WOLFSSL_NO_TLS12
  62303. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  62304. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  62305. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_2 v1" },
  62306. #endif
  62307. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  62308. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  62309. test_self_signed_stapling_client_v2_ctx_ready, "TLSv1_2 v2" },
  62310. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  62311. test_self_signed_stapling_client_v2_multi_ctx_ready,
  62312. "TLSv1_2 v2 multi" },
  62313. #endif
  62314. #endif
  62315. };
  62316. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  62317. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  62318. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  62319. printf("\nTesting self-signed cert with status request: %s\n",
  62320. params[i].tls_version);
  62321. client_cbf.method = params[i].client_meth;
  62322. client_cbf.ctx_ready = params[i].client_ctx;
  62323. server_cbf.method = params[i].server_meth;
  62324. server_cbf.certPemFile = "certs/ca-cert.pem";
  62325. server_cbf.keyPemFile = "certs/ca-key.pem";
  62326. server_cbf.ctx_ready = test_self_signed_stapling_server_ctx_ready;
  62327. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  62328. &server_cbf, NULL), TEST_SUCCESS);
  62329. }
  62330. #endif
  62331. return EXPECT_RESULT();
  62332. }
  62333. static int test_tls_multi_handshakes_one_record(void)
  62334. {
  62335. EXPECT_DECLS;
  62336. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  62337. struct test_memio_ctx test_ctx;
  62338. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  62339. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  62340. RecordLayerHeader* rh = NULL;
  62341. byte *len ;
  62342. int newRecIdx = RECORD_HEADER_SZ;
  62343. int idx = 0;
  62344. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62345. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62346. wolfTLS_client_method, wolfTLSv1_2_server_method), 0);
  62347. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  62348. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62349. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  62350. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62351. /* Combine server handshake msgs into one record */
  62352. while (idx < test_ctx.c_len) {
  62353. word16 recLen;
  62354. rh = (RecordLayerHeader*)(test_ctx.c_buff + idx);
  62355. len = &rh->length[0];
  62356. ato16((const byte*)len, &recLen);
  62357. idx += RECORD_HEADER_SZ;
  62358. XMEMMOVE(test_ctx.c_buff + newRecIdx, test_ctx.c_buff + idx,
  62359. (size_t)recLen);
  62360. newRecIdx += recLen;
  62361. idx += recLen;
  62362. }
  62363. rh = (RecordLayerHeader*)(test_ctx.c_buff);
  62364. len = &rh->length[0];
  62365. c16toa(newRecIdx - RECORD_HEADER_SZ, len);
  62366. test_ctx.c_len = newRecIdx;
  62367. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  62368. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62369. wolfSSL_free(ssl_c);
  62370. wolfSSL_free(ssl_s);
  62371. wolfSSL_CTX_free(ctx_c);
  62372. wolfSSL_CTX_free(ctx_s);
  62373. #endif
  62374. return EXPECT_RESULT();
  62375. }
  62376. static int test_write_dup(void)
  62377. {
  62378. EXPECT_DECLS;
  62379. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_WRITE_DUP)
  62380. size_t i, j;
  62381. char hiWorld[] = "dup message";
  62382. char readData[sizeof(hiWorld) + 5];
  62383. struct {
  62384. method_provider client_meth;
  62385. method_provider server_meth;
  62386. const char* version_name;
  62387. int version;
  62388. } methods[] = {
  62389. #ifndef WOLFSSL_NO_TLS12
  62390. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLS 1.2", WOLFSSL_TLSV1_2},
  62391. #endif
  62392. #ifdef WOLFSSL_TLS13
  62393. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLS 1.3", WOLFSSL_TLSV1_3},
  62394. #endif
  62395. };
  62396. struct {
  62397. const char* cipher;
  62398. int version;
  62399. } ciphers[] = {
  62400. /* For simplicity the macros are copied from internal.h */
  62401. /* TLS 1.2 */
  62402. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && !defined(NO_SHA256)
  62403. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  62404. #ifndef NO_RSA
  62405. {"ECDHE-RSA-CHACHA20-POLY1305", WOLFSSL_TLSV1_2},
  62406. #endif
  62407. #endif
  62408. #if !defined(NO_DH) && !defined(NO_RSA) && !defined(NO_TLS_DH)
  62409. {"DHE-RSA-CHACHA20-POLY1305", WOLFSSL_TLSV1_2},
  62410. #endif
  62411. #endif
  62412. #if !defined(NO_DH) && !defined(NO_AES) && !defined(NO_TLS) && \
  62413. !defined(NO_RSA) && defined(HAVE_AESGCM) && !defined(NO_TLS_DH)
  62414. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  62415. {"DHE-RSA-AES128-GCM-SHA256", WOLFSSL_TLSV1_2},
  62416. #endif
  62417. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  62418. {"DHE-RSA-AES256-GCM-SHA384", WOLFSSL_TLSV1_2},
  62419. #endif
  62420. #endif
  62421. #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)) \
  62422. && !defined(NO_TLS) && !defined(NO_AES)
  62423. #ifdef HAVE_AESGCM
  62424. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  62425. #ifndef NO_RSA
  62426. {"ECDHE-RSA-AES128-GCM-SHA256", WOLFSSL_TLSV1_2},
  62427. #endif
  62428. #endif
  62429. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  62430. #ifndef NO_RSA
  62431. {"ECDHE-RSA-AES256-GCM-SHA384", WOLFSSL_TLSV1_2},
  62432. #endif
  62433. #endif
  62434. #endif
  62435. #endif
  62436. /* TLS 1.3 */
  62437. #ifdef WOLFSSL_TLS13
  62438. #ifdef HAVE_AESGCM
  62439. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  62440. {"TLS13-AES128-GCM-SHA256", WOLFSSL_TLSV1_3},
  62441. #endif
  62442. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  62443. {"TLS13-AES256-GCM-SHA384", WOLFSSL_TLSV1_3},
  62444. #endif
  62445. #endif
  62446. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  62447. #ifndef NO_SHA256
  62448. {"TLS13-CHACHA20-POLY1305-SHA256", WOLFSSL_TLSV1_3},
  62449. #endif
  62450. #endif
  62451. #ifdef HAVE_AESCCM
  62452. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  62453. {"TLS13-AES128-CCM-SHA256", WOLFSSL_TLSV1_3},
  62454. #endif
  62455. #endif
  62456. #endif
  62457. };
  62458. for (i = 0; i < XELEM_CNT(methods); i++) {
  62459. for (j = 0; j < XELEM_CNT(ciphers) && !EXPECT_FAIL(); j++) {
  62460. struct test_memio_ctx test_ctx;
  62461. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  62462. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  62463. WOLFSSL *ssl_c2 = NULL;
  62464. if (methods[i].version != ciphers[j].version)
  62465. continue;
  62466. if (i == 0 && j == 0)
  62467. printf("\n");
  62468. printf("Testing %s with %s... ", methods[i].version_name,
  62469. ciphers[j].cipher);
  62470. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62471. test_ctx.c_ciphers = test_ctx.s_ciphers = ciphers[j].cipher;
  62472. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62473. methods[i].client_meth, methods[i].server_meth), 0);
  62474. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62475. ExpectNotNull(ssl_c2 = wolfSSL_write_dup(ssl_c));
  62476. ExpectIntEQ(wolfSSL_write(ssl_c, hiWorld, sizeof(hiWorld)),
  62477. WRITE_DUP_WRITE_E);
  62478. ExpectIntEQ(wolfSSL_write(ssl_c2, hiWorld, sizeof(hiWorld)),
  62479. sizeof(hiWorld));
  62480. ExpectIntEQ(wolfSSL_read(ssl_s, readData, sizeof(readData)),
  62481. sizeof(hiWorld));
  62482. ExpectIntEQ(wolfSSL_write(ssl_s, hiWorld, sizeof(hiWorld)),
  62483. sizeof(hiWorld));
  62484. ExpectIntEQ(wolfSSL_read(ssl_c2, readData, sizeof(readData)),
  62485. WRITE_DUP_READ_E);
  62486. ExpectIntEQ(wolfSSL_read(ssl_c, readData, sizeof(readData)),
  62487. sizeof(hiWorld));
  62488. if (EXPECT_SUCCESS())
  62489. printf("ok\n");
  62490. else
  62491. printf("failed\n");
  62492. wolfSSL_free(ssl_c);
  62493. wolfSSL_free(ssl_c2);
  62494. wolfSSL_free(ssl_s);
  62495. wolfSSL_CTX_free(ctx_c);
  62496. wolfSSL_CTX_free(ctx_s);
  62497. }
  62498. }
  62499. #endif
  62500. return EXPECT_RESULT();
  62501. }
  62502. static int test_read_write_hs(void)
  62503. {
  62504. EXPECT_DECLS;
  62505. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  62506. WOLFSSL_CTX *ctx_s = NULL, *ctx_c = NULL;
  62507. WOLFSSL *ssl_s = NULL, *ssl_c = NULL;
  62508. struct test_memio_ctx test_ctx;
  62509. uint8_t test_buffer[16];
  62510. unsigned int test;
  62511. /* test == 0 : client writes, server reads */
  62512. /* test == 1 : server writes, client reads */
  62513. for (test = 0; test < 2; test++) {
  62514. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62515. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62516. wolfTLSv1_2_client_method,
  62517. wolfTLSv1_2_server_method), 0);
  62518. ExpectIntEQ(wolfSSL_set_group_messages(ssl_s), WOLFSSL_SUCCESS);
  62519. /* CH -> */
  62520. if (test == 0) {
  62521. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  62522. } else {
  62523. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  62524. sizeof(test_buffer)), -1);
  62525. }
  62526. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62527. /* <- SH + SKE + SHD */
  62528. if (test == 0) {
  62529. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  62530. sizeof(test_buffer)), -1);
  62531. } else {
  62532. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), -1);
  62533. }
  62534. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62535. /* -> CKE + CLIENT FINISHED */
  62536. if (test == 0) {
  62537. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  62538. } else {
  62539. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  62540. sizeof(test_buffer)), -1);
  62541. }
  62542. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62543. /* abide clang static analyzer */
  62544. if (ssl_s != NULL) {
  62545. /* disable group message to separate sending of ChangeCipherspec
  62546. * from Finished */
  62547. ssl_s->options.groupMessages = 0;
  62548. }
  62549. /* allow writing of CS, but not FINISHED */
  62550. test_ctx.c_len = TEST_MEMIO_BUF_SZ - 6;
  62551. /* <- CS */
  62552. if (test == 0) {
  62553. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  62554. sizeof(test_buffer)), -1);
  62555. } else {
  62556. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), -1);
  62557. }
  62558. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_WRITE);
  62559. /* move CS message where the client can read it */
  62560. memmove(test_ctx.c_buff,
  62561. (test_ctx.c_buff + TEST_MEMIO_BUF_SZ - 6), 6);
  62562. test_ctx.c_len = 6;
  62563. /* read CS */
  62564. if (test == 0) {
  62565. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  62566. } else {
  62567. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  62568. sizeof(test_buffer)), -1);
  62569. }
  62570. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62571. ExpectIntEQ(test_ctx.c_len, 0);
  62572. if (test == 0) {
  62573. /* send SERVER FINISHED */
  62574. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  62575. sizeof(test_buffer)), -1);
  62576. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1),
  62577. WOLFSSL_ERROR_WANT_READ);
  62578. } else {
  62579. /* send SERVER FINISHED + App Data */
  62580. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), 5);
  62581. }
  62582. ExpectIntGT(test_ctx.c_len, 0);
  62583. /* Send and receive the data */
  62584. if (test == 0) {
  62585. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), 5);
  62586. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  62587. sizeof(test_buffer)), 5);
  62588. } else {
  62589. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  62590. sizeof(test_buffer)), 5);
  62591. }
  62592. ExpectBufEQ(test_buffer, "hello", 5);
  62593. wolfSSL_free(ssl_c);
  62594. wolfSSL_free(ssl_s);
  62595. wolfSSL_CTX_free(ctx_c);
  62596. wolfSSL_CTX_free(ctx_s);
  62597. ssl_c = ssl_s = NULL;
  62598. ctx_c = ctx_s = NULL;
  62599. }
  62600. #endif
  62601. return EXPECT_RESULT();
  62602. }
  62603. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA)
  62604. static const char* test_get_signature_nid_siglag;
  62605. static int test_get_signature_nid_sig;
  62606. static int test_get_signature_nid_hash;
  62607. static int test_get_signature_nid_ssl_ready(WOLFSSL* ssl)
  62608. {
  62609. EXPECT_DECLS;
  62610. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "ALL"), WOLFSSL_SUCCESS);
  62611. if (!wolfSSL_is_server(ssl)) {
  62612. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  62613. test_get_signature_nid_siglag), WOLFSSL_SUCCESS);
  62614. }
  62615. return EXPECT_RESULT();
  62616. }
  62617. static int test_get_signature_nid_on_hs_client(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  62618. {
  62619. EXPECT_DECLS;
  62620. int nid = 0;
  62621. (void)ctx;
  62622. if (XSTRSTR(wolfSSL_get_cipher(*ssl), "TLS_RSA_") == NULL) {
  62623. ExpectIntEQ(SSL_get_peer_signature_type_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  62624. ExpectIntEQ(nid, test_get_signature_nid_sig);
  62625. ExpectIntEQ(SSL_get_peer_signature_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  62626. ExpectIntEQ(nid, test_get_signature_nid_hash);
  62627. }
  62628. else /* No sigalg info on static ciphersuite */
  62629. return TEST_SUCCESS;
  62630. return EXPECT_RESULT();
  62631. }
  62632. static int test_get_signature_nid_on_hs_server(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  62633. {
  62634. EXPECT_DECLS;
  62635. int nid = 0;
  62636. (void)ctx;
  62637. ExpectIntEQ(SSL_get_signature_type_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  62638. ExpectIntEQ(nid, test_get_signature_nid_sig);
  62639. ExpectIntEQ(SSL_get_signature_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  62640. ExpectIntEQ(nid, test_get_signature_nid_hash);
  62641. return EXPECT_RESULT();
  62642. }
  62643. #endif
  62644. static int test_get_signature_nid(void)
  62645. {
  62646. EXPECT_DECLS;
  62647. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA)
  62648. test_ssl_cbf client_cbf;
  62649. test_ssl_cbf server_cbf;
  62650. size_t i;
  62651. #define TGSN_TLS12_RSA(sigalg, sig_nid, hash_nid) \
  62652. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_2, svrCertFile, svrKeyFile, \
  62653. caCertFile }
  62654. #define TGSN_TLS12_ECDSA(sigalg, sig_nid, hash_nid) \
  62655. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_2, eccCertFile, eccKeyFile, \
  62656. caEccCertFile }
  62657. #define TGSN_TLS13_RSA(sigalg, sig_nid, hash_nid) \
  62658. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, svrCertFile, svrKeyFile, \
  62659. caCertFile }
  62660. #define TGSN_TLS13_ECDSA(sigalg, sig_nid, hash_nid) \
  62661. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, eccCertFile, eccKeyFile, \
  62662. caEccCertFile }
  62663. #define TGSN_TLS13_ED25519(sigalg, sig_nid, hash_nid) \
  62664. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, edCertFile, edKeyFile, \
  62665. caEdCertFile }
  62666. #define TGSN_TLS13_ED448(sigalg, sig_nid, hash_nid) \
  62667. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, ed448CertFile, ed448KeyFile, \
  62668. caEd448CertFile }
  62669. struct {
  62670. const char* siglag;
  62671. int sig_nid;
  62672. int hash_nid;
  62673. int tls_ver;
  62674. const char* server_cert;
  62675. const char* server_key;
  62676. const char* client_ca;
  62677. } params[] = {
  62678. #ifndef NO_RSA
  62679. #ifndef NO_SHA256
  62680. TGSN_TLS12_RSA("RSA+SHA256", NID_rsaEncryption, NID_sha256),
  62681. #ifdef WC_RSA_PSS
  62682. TGSN_TLS12_RSA("RSA-PSS+SHA256", NID_rsassaPss, NID_sha256),
  62683. TGSN_TLS13_RSA("RSA-PSS+SHA256", NID_rsassaPss, NID_sha256),
  62684. #endif
  62685. #endif
  62686. #ifdef WOLFSSL_SHA512
  62687. TGSN_TLS12_RSA("RSA+SHA512", NID_rsaEncryption, NID_sha512),
  62688. #ifdef WC_RSA_PSS
  62689. TGSN_TLS12_RSA("RSA-PSS+SHA512", NID_rsassaPss, NID_sha512),
  62690. TGSN_TLS13_RSA("RSA-PSS+SHA512", NID_rsassaPss, NID_sha512),
  62691. #endif
  62692. #endif
  62693. #endif
  62694. #ifdef HAVE_ECC
  62695. #ifndef NO_SHA256
  62696. TGSN_TLS12_ECDSA("ECDSA+SHA256", NID_X9_62_id_ecPublicKey, NID_sha256),
  62697. TGSN_TLS13_ECDSA("ECDSA+SHA256", NID_X9_62_id_ecPublicKey, NID_sha256),
  62698. #endif
  62699. #endif
  62700. #ifdef HAVE_ED25519
  62701. TGSN_TLS13_ED25519("ED25519", NID_ED25519, NID_sha512),
  62702. #endif
  62703. #ifdef HAVE_ED448
  62704. TGSN_TLS13_ED448("ED448", NID_ED448, NID_sha512),
  62705. #endif
  62706. };
  62707. printf("\n");
  62708. for (i = 0; i < XELEM_CNT(params) && !EXPECT_FAIL(); i++) {
  62709. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  62710. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  62711. printf("Testing %s with %s...", tls_desc[params[i].tls_ver],
  62712. params[i].siglag);
  62713. switch (params[i].tls_ver) {
  62714. #ifndef WOLFSSL_NO_TLS12
  62715. case WOLFSSL_TLSV1_2:
  62716. client_cbf.method = wolfTLSv1_2_client_method;
  62717. server_cbf.method = wolfTLSv1_2_server_method;
  62718. break;
  62719. #endif
  62720. #ifdef WOLFSSL_TLS13
  62721. case WOLFSSL_TLSV1_3:
  62722. client_cbf.method = wolfTLSv1_3_client_method;
  62723. server_cbf.method = wolfTLSv1_3_server_method;
  62724. break;
  62725. #endif
  62726. default:
  62727. printf("skipping\n");
  62728. continue;
  62729. }
  62730. test_get_signature_nid_siglag = params[i].siglag;
  62731. test_get_signature_nid_sig = params[i].sig_nid;
  62732. test_get_signature_nid_hash = params[i].hash_nid;
  62733. client_cbf.ssl_ready = test_get_signature_nid_ssl_ready;
  62734. server_cbf.ssl_ready = test_get_signature_nid_ssl_ready;
  62735. client_cbf.on_handshake = test_get_signature_nid_on_hs_client;
  62736. server_cbf.on_handshake = test_get_signature_nid_on_hs_server;
  62737. server_cbf.certPemFile = params[i].server_cert;
  62738. server_cbf.keyPemFile = params[i].server_key;
  62739. client_cbf.caPemFile = params[i].client_ca;
  62740. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  62741. &server_cbf, NULL), TEST_SUCCESS);
  62742. if (EXPECT_SUCCESS())
  62743. printf("passed\n");
  62744. }
  62745. #endif
  62746. return EXPECT_RESULT();
  62747. }
  62748. #if !defined(NO_CERTS) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  62749. static word32 test_tls_cert_store_unchanged_HashCaTable(Signer** caTable)
  62750. {
  62751. #ifndef NO_MD5
  62752. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  62753. #elif !defined(NO_SHA)
  62754. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  62755. #elif !defined(NO_SHA256)
  62756. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  62757. #else
  62758. #error "We need a digest to hash the Signer object"
  62759. #endif
  62760. byte hashBuf[WC_MAX_DIGEST_SIZE];
  62761. wc_HashAlg hash;
  62762. size_t i;
  62763. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  62764. for (i = 0; i < CA_TABLE_SIZE; i++) {
  62765. Signer* cur;
  62766. for (cur = caTable[i]; cur != NULL; cur = cur->next)
  62767. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)cur,
  62768. sizeof(*cur)), 0);
  62769. }
  62770. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  62771. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  62772. return MakeWordFromHash(hashBuf);
  62773. }
  62774. static word32 test_tls_cert_store_unchanged_before_hashes[2];
  62775. static size_t test_tls_cert_store_unchanged_before_hashes_idx;
  62776. static word32 test_tls_cert_store_unchanged_after_hashes[2];
  62777. static size_t test_tls_cert_store_unchanged_after_hashes_idx;
  62778. static int test_tls_cert_store_unchanged_ctx_ready(WOLFSSL_CTX* ctx)
  62779. {
  62780. EXPECT_DECLS;
  62781. ExpectIntNE(test_tls_cert_store_unchanged_before_hashes
  62782. [test_tls_cert_store_unchanged_before_hashes_idx++] =
  62783. test_tls_cert_store_unchanged_HashCaTable(ctx->cm->caTable), 0);
  62784. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER |
  62785. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  62786. return EXPECT_RESULT();
  62787. }
  62788. static int test_tls_cert_store_unchanged_ctx_cleanup(WOLFSSL_CTX* ctx)
  62789. {
  62790. EXPECT_DECLS;
  62791. ExpectIntEQ(wolfSSL_CTX_UnloadIntermediateCerts(ctx), WOLFSSL_SUCCESS);
  62792. ExpectIntNE(test_tls_cert_store_unchanged_after_hashes
  62793. [test_tls_cert_store_unchanged_after_hashes_idx++] =
  62794. test_tls_cert_store_unchanged_HashCaTable(ctx->cm->caTable), 0);
  62795. return EXPECT_RESULT();
  62796. }
  62797. static int test_tls_cert_store_unchanged_on_hs(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  62798. {
  62799. EXPECT_DECLS;
  62800. WOLFSSL_CERT_MANAGER* cm;
  62801. (void)ssl;
  62802. /* WARNING: this approach bypasses the reference counter check in
  62803. * wolfSSL_CTX_UnloadIntermediateCerts. It is not recommended as it may
  62804. * cause unexpected behaviour when other active connections try accessing
  62805. * the caTable. */
  62806. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(*ctx));
  62807. ExpectIntEQ(wolfSSL_CertManagerUnloadIntermediateCerts(cm),
  62808. WOLFSSL_SUCCESS);
  62809. ExpectIntNE(test_tls_cert_store_unchanged_after_hashes
  62810. [test_tls_cert_store_unchanged_after_hashes_idx++] =
  62811. test_tls_cert_store_unchanged_HashCaTable((*ctx)->cm->caTable), 0);
  62812. return EXPECT_RESULT();
  62813. }
  62814. static int test_tls_cert_store_unchanged_ssl_ready(WOLFSSL* ssl)
  62815. {
  62816. EXPECT_DECLS;
  62817. WOLFSSL_CTX* ctx;
  62818. ExpectNotNull(ctx = wolfSSL_get_SSL_CTX(ssl));
  62819. return EXPECT_RESULT();
  62820. }
  62821. #endif
  62822. static int test_tls_cert_store_unchanged(void)
  62823. {
  62824. EXPECT_DECLS;
  62825. #if !defined(NO_CERTS) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  62826. test_ssl_cbf client_cbf;
  62827. test_ssl_cbf server_cbf;
  62828. int i;
  62829. for (i = 0; i < 2; i++) {
  62830. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  62831. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  62832. test_tls_cert_store_unchanged_before_hashes_idx = 0;
  62833. XMEMSET(test_tls_cert_store_unchanged_before_hashes, 0,
  62834. sizeof(test_tls_cert_store_unchanged_before_hashes));
  62835. test_tls_cert_store_unchanged_after_hashes_idx = 0;
  62836. XMEMSET(test_tls_cert_store_unchanged_after_hashes, 0,
  62837. sizeof(test_tls_cert_store_unchanged_after_hashes));
  62838. client_cbf.ctx_ready = test_tls_cert_store_unchanged_ctx_ready;
  62839. server_cbf.ctx_ready = test_tls_cert_store_unchanged_ctx_ready;
  62840. client_cbf.ssl_ready = test_tls_cert_store_unchanged_ssl_ready;
  62841. server_cbf.ssl_ready = test_tls_cert_store_unchanged_ssl_ready;
  62842. switch (i) {
  62843. case 0:
  62844. client_cbf.on_ctx_cleanup =
  62845. test_tls_cert_store_unchanged_ctx_cleanup;
  62846. server_cbf.on_ctx_cleanup =
  62847. test_tls_cert_store_unchanged_ctx_cleanup;
  62848. break;
  62849. case 1:
  62850. client_cbf.on_handshake = test_tls_cert_store_unchanged_on_hs;
  62851. server_cbf.on_handshake = test_tls_cert_store_unchanged_on_hs;
  62852. break;
  62853. default:
  62854. Fail(("Should not enter here"), ("Entered here"));
  62855. }
  62856. client_cbf.certPemFile = "certs/intermediate/client-chain.pem";
  62857. server_cbf.certPemFile = "certs/intermediate/server-chain.pem";
  62858. server_cbf.caPemFile = caCertFile;
  62859. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  62860. &server_cbf, NULL), TEST_SUCCESS);
  62861. ExpectBufEQ(test_tls_cert_store_unchanged_before_hashes,
  62862. test_tls_cert_store_unchanged_after_hashes,
  62863. sizeof(test_tls_cert_store_unchanged_after_hashes));
  62864. }
  62865. #endif
  62866. return EXPECT_RESULT();
  62867. }
  62868. /*----------------------------------------------------------------------------*
  62869. | Main
  62870. *----------------------------------------------------------------------------*/
  62871. typedef int (*TEST_FUNC)(void);
  62872. typedef struct {
  62873. const char *name;
  62874. TEST_FUNC func;
  62875. byte run:1;
  62876. byte fail:1;
  62877. } TEST_CASE;
  62878. #define TEST_DECL(func) { #func, func, 0, 0 }
  62879. int testAll = 1;
  62880. TEST_CASE testCases[] = {
  62881. TEST_DECL(test_fileAccess),
  62882. /*********************************
  62883. * wolfcrypt
  62884. *********************************/
  62885. TEST_DECL(test_ForceZero),
  62886. TEST_DECL(test_wolfCrypt_Init),
  62887. TEST_DECL(test_wc_LoadStaticMemory_ex),
  62888. /* Locking with Compat Mutex */
  62889. TEST_DECL(test_wc_SetMutexCb),
  62890. TEST_DECL(test_wc_LockMutex_ex),
  62891. /* Digests */
  62892. TEST_DECL(test_wc_InitMd5),
  62893. TEST_DECL(test_wc_Md5Update),
  62894. TEST_DECL(test_wc_Md5Final),
  62895. TEST_DECL(test_wc_InitSha),
  62896. TEST_DECL(test_wc_ShaUpdate),
  62897. TEST_DECL(test_wc_ShaFinal),
  62898. TEST_DECL(test_wc_InitSha256),
  62899. TEST_DECL(test_wc_Sha256Update),
  62900. TEST_DECL(test_wc_Sha256Final),
  62901. TEST_DECL(test_wc_Sha256FinalRaw),
  62902. TEST_DECL(test_wc_Sha256GetFlags),
  62903. TEST_DECL(test_wc_Sha256Free),
  62904. TEST_DECL(test_wc_Sha256GetHash),
  62905. TEST_DECL(test_wc_Sha256Copy),
  62906. TEST_DECL(test_wc_InitSha224),
  62907. TEST_DECL(test_wc_Sha224Update),
  62908. TEST_DECL(test_wc_Sha224Final),
  62909. TEST_DECL(test_wc_Sha224SetFlags),
  62910. TEST_DECL(test_wc_Sha224GetFlags),
  62911. TEST_DECL(test_wc_Sha224Free),
  62912. TEST_DECL(test_wc_Sha224GetHash),
  62913. TEST_DECL(test_wc_Sha224Copy),
  62914. TEST_DECL(test_wc_InitSha512),
  62915. TEST_DECL(test_wc_Sha512Update),
  62916. TEST_DECL(test_wc_Sha512Final),
  62917. TEST_DECL(test_wc_Sha512GetFlags),
  62918. TEST_DECL(test_wc_Sha512FinalRaw),
  62919. TEST_DECL(test_wc_Sha512Free),
  62920. TEST_DECL(test_wc_Sha512GetHash),
  62921. TEST_DECL(test_wc_Sha512Copy),
  62922. TEST_DECL(test_wc_InitSha512_224),
  62923. TEST_DECL(test_wc_Sha512_224Update),
  62924. TEST_DECL(test_wc_Sha512_224Final),
  62925. TEST_DECL(test_wc_Sha512_224GetFlags),
  62926. TEST_DECL(test_wc_Sha512_224FinalRaw),
  62927. TEST_DECL(test_wc_Sha512_224Free),
  62928. TEST_DECL(test_wc_Sha512_224GetHash),
  62929. TEST_DECL(test_wc_Sha512_224Copy),
  62930. TEST_DECL(test_wc_InitSha512_256),
  62931. TEST_DECL(test_wc_Sha512_256Update),
  62932. TEST_DECL(test_wc_Sha512_256Final),
  62933. TEST_DECL(test_wc_Sha512_256GetFlags),
  62934. TEST_DECL(test_wc_Sha512_256FinalRaw),
  62935. TEST_DECL(test_wc_Sha512_256Free),
  62936. TEST_DECL(test_wc_Sha512_256GetHash),
  62937. TEST_DECL(test_wc_Sha512_256Copy),
  62938. TEST_DECL(test_wc_InitSha384),
  62939. TEST_DECL(test_wc_Sha384Update),
  62940. TEST_DECL(test_wc_Sha384Final),
  62941. TEST_DECL(test_wc_Sha384GetFlags),
  62942. TEST_DECL(test_wc_Sha384FinalRaw),
  62943. TEST_DECL(test_wc_Sha384Free),
  62944. TEST_DECL(test_wc_Sha384GetHash),
  62945. TEST_DECL(test_wc_Sha384Copy),
  62946. TEST_DECL(test_wc_InitBlake2b),
  62947. TEST_DECL(test_wc_InitBlake2b_WithKey),
  62948. TEST_DECL(test_wc_InitBlake2s_WithKey),
  62949. TEST_DECL(test_wc_InitRipeMd),
  62950. TEST_DECL(test_wc_RipeMdUpdate),
  62951. TEST_DECL(test_wc_RipeMdFinal),
  62952. TEST_DECL(test_wc_InitSha3),
  62953. TEST_DECL(testing_wc_Sha3_Update),
  62954. TEST_DECL(test_wc_Sha3_224_Final),
  62955. TEST_DECL(test_wc_Sha3_256_Final),
  62956. TEST_DECL(test_wc_Sha3_384_Final),
  62957. TEST_DECL(test_wc_Sha3_512_Final),
  62958. TEST_DECL(test_wc_Sha3_224_Copy),
  62959. TEST_DECL(test_wc_Sha3_256_Copy),
  62960. TEST_DECL(test_wc_Sha3_384_Copy),
  62961. TEST_DECL(test_wc_Sha3_512_Copy),
  62962. TEST_DECL(test_wc_Sha3_GetFlags),
  62963. TEST_DECL(test_wc_InitShake256),
  62964. TEST_DECL(testing_wc_Shake256_Update),
  62965. TEST_DECL(test_wc_Shake256_Final),
  62966. TEST_DECL(test_wc_Shake256_Copy),
  62967. TEST_DECL(test_wc_Shake256Hash),
  62968. /* SM3 Digest */
  62969. TEST_DECL(test_wc_InitSm3Free),
  62970. TEST_DECL(test_wc_Sm3UpdateFinal),
  62971. TEST_DECL(test_wc_Sm3GetHash),
  62972. TEST_DECL(test_wc_Sm3Copy),
  62973. TEST_DECL(test_wc_Sm3FinalRaw),
  62974. TEST_DECL(test_wc_Sm3GetSetFlags),
  62975. TEST_DECL(test_wc_Sm3Hash),
  62976. TEST_DECL(test_wc_HashInit),
  62977. TEST_DECL(test_wc_HashSetFlags),
  62978. TEST_DECL(test_wc_HashGetFlags),
  62979. /* HMAC */
  62980. TEST_DECL(test_wc_Md5HmacSetKey),
  62981. TEST_DECL(test_wc_Md5HmacUpdate),
  62982. TEST_DECL(test_wc_Md5HmacFinal),
  62983. TEST_DECL(test_wc_ShaHmacSetKey),
  62984. TEST_DECL(test_wc_ShaHmacUpdate),
  62985. TEST_DECL(test_wc_ShaHmacFinal),
  62986. TEST_DECL(test_wc_Sha224HmacSetKey),
  62987. TEST_DECL(test_wc_Sha224HmacUpdate),
  62988. TEST_DECL(test_wc_Sha224HmacFinal),
  62989. TEST_DECL(test_wc_Sha256HmacSetKey),
  62990. TEST_DECL(test_wc_Sha256HmacUpdate),
  62991. TEST_DECL(test_wc_Sha256HmacFinal),
  62992. TEST_DECL(test_wc_Sha384HmacSetKey),
  62993. TEST_DECL(test_wc_Sha384HmacUpdate),
  62994. TEST_DECL(test_wc_Sha384HmacFinal),
  62995. /* CMAC */
  62996. TEST_DECL(test_wc_InitCmac),
  62997. TEST_DECL(test_wc_CmacUpdate),
  62998. TEST_DECL(test_wc_CmacFinal),
  62999. TEST_DECL(test_wc_AesCmacGenerate),
  63000. /* Cipher */
  63001. TEST_DECL(test_wc_AesGcmStream),
  63002. TEST_DECL(test_wc_Des3_SetIV),
  63003. TEST_DECL(test_wc_Des3_SetKey),
  63004. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  63005. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  63006. TEST_DECL(test_wc_Des3_EcbEncrypt),
  63007. TEST_DECL(test_wc_Chacha_SetKey),
  63008. TEST_DECL(test_wc_Chacha_Process),
  63009. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  63010. TEST_DECL(test_wc_Poly1305SetKey),
  63011. TEST_DECL(test_wc_CamelliaSetKey),
  63012. TEST_DECL(test_wc_CamelliaSetIV),
  63013. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  63014. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  63015. TEST_DECL(test_wc_Arc4SetKey),
  63016. TEST_DECL(test_wc_Arc4Process),
  63017. TEST_DECL(test_wc_Rc2SetKey),
  63018. TEST_DECL(test_wc_Rc2SetIV),
  63019. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  63020. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  63021. /* AES cipher and GMAC. */
  63022. TEST_DECL(test_wc_AesSetKey),
  63023. TEST_DECL(test_wc_AesSetIV),
  63024. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  63025. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  63026. TEST_DECL(test_wc_AesGcmSetKey),
  63027. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  63028. TEST_DECL(test_wc_AesGcmMixedEncDecLongIV),
  63029. TEST_DECL(test_wc_GmacSetKey),
  63030. TEST_DECL(test_wc_GmacUpdate),
  63031. TEST_DECL(test_wc_AesCcmSetKey),
  63032. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  63033. #if defined(WOLFSSL_AES_EAX) && \
  63034. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  63035. TEST_DECL(test_wc_AesEaxVectors),
  63036. TEST_DECL(test_wc_AesEaxEncryptAuth),
  63037. TEST_DECL(test_wc_AesEaxDecryptAuth),
  63038. #endif /* WOLFSSL_AES_EAX */
  63039. /* SM4 cipher */
  63040. TEST_DECL(test_wc_Sm4),
  63041. TEST_DECL(test_wc_Sm4Ecb),
  63042. TEST_DECL(test_wc_Sm4Cbc),
  63043. TEST_DECL(test_wc_Sm4Ctr),
  63044. TEST_DECL(test_wc_Sm4Gcm),
  63045. TEST_DECL(test_wc_Sm4Ccm),
  63046. /* RNG tests */
  63047. #ifdef HAVE_HASHDRBG
  63048. #ifdef TEST_RESEED_INTERVAL
  63049. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  63050. #endif
  63051. TEST_DECL(test_wc_RNG_GenerateBlock),
  63052. #endif
  63053. TEST_DECL(test_get_rand_digit),
  63054. TEST_DECL(test_wc_InitRngNonce),
  63055. TEST_DECL(test_wc_InitRngNonce_ex),
  63056. /* MP API tests */
  63057. TEST_DECL(test_get_digit_count),
  63058. TEST_DECL(test_mp_cond_copy),
  63059. TEST_DECL(test_mp_rand),
  63060. TEST_DECL(test_get_digit),
  63061. TEST_DECL(test_wc_export_int),
  63062. /* RSA */
  63063. TEST_DECL(test_wc_InitRsaKey),
  63064. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  63065. TEST_DECL(test_wc_RsaPublicKeyDecode),
  63066. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  63067. TEST_DECL(test_wc_MakeRsaKey),
  63068. TEST_DECL(test_wc_CheckProbablePrime),
  63069. TEST_DECL(test_wc_RsaPSS_Verify),
  63070. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  63071. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  63072. TEST_DECL(test_wc_RsaKeyToDer),
  63073. TEST_DECL(test_wc_RsaKeyToPublicDer),
  63074. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  63075. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  63076. TEST_DECL(test_wc_RsaEncryptSize),
  63077. TEST_DECL(test_wc_RsaSSL_SignVerify),
  63078. TEST_DECL(test_wc_RsaFlattenPublicKey),
  63079. TEST_DECL(test_RsaDecryptBoundsCheck),
  63080. /* DSA */
  63081. TEST_DECL(test_wc_InitDsaKey),
  63082. TEST_DECL(test_wc_DsaSignVerify),
  63083. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  63084. TEST_DECL(test_wc_MakeDsaKey),
  63085. TEST_DECL(test_wc_DsaKeyToDer),
  63086. TEST_DECL(test_wc_DsaKeyToPublicDer),
  63087. TEST_DECL(test_wc_DsaImportParamsRaw),
  63088. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  63089. TEST_DECL(test_wc_DsaExportParamsRaw),
  63090. TEST_DECL(test_wc_DsaExportKeyRaw),
  63091. /* DH */
  63092. TEST_DECL(test_wc_DhPublicKeyDecode),
  63093. /* wolfCrypt ECC tests */
  63094. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  63095. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  63096. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  63097. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  63098. !defined(HAVE_SELFTEST) && \
  63099. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  63100. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  63101. #endif
  63102. TEST_DECL(test_wc_ecc_make_key),
  63103. TEST_DECL(test_wc_ecc_init),
  63104. TEST_DECL(test_wc_ecc_check_key),
  63105. TEST_DECL(test_wc_ecc_get_generator),
  63106. TEST_DECL(test_wc_ecc_size),
  63107. TEST_DECL(test_wc_ecc_params),
  63108. TEST_DECL(test_wc_ecc_signVerify_hash),
  63109. TEST_DECL(test_wc_ecc_shared_secret),
  63110. TEST_DECL(test_wc_ecc_export_x963),
  63111. TEST_DECL(test_wc_ecc_export_x963_ex),
  63112. TEST_DECL(test_wc_ecc_import_x963),
  63113. TEST_DECL(test_wc_ecc_import_private_key),
  63114. TEST_DECL(test_wc_ecc_export_private_only),
  63115. TEST_DECL(test_wc_ecc_rs_to_sig),
  63116. TEST_DECL(test_wc_ecc_import_raw),
  63117. TEST_DECL(test_wc_ecc_import_unsigned),
  63118. TEST_DECL(test_wc_ecc_sig_size),
  63119. TEST_DECL(test_wc_ecc_ctx_new),
  63120. TEST_DECL(test_wc_ecc_ctx_reset),
  63121. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  63122. TEST_DECL(test_wc_ecc_ctx_set_info),
  63123. TEST_DECL(test_wc_ecc_encryptDecrypt),
  63124. TEST_DECL(test_wc_ecc_del_point),
  63125. TEST_DECL(test_wc_ecc_pointFns),
  63126. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  63127. TEST_DECL(test_wc_ecc_verify_hash_ex),
  63128. TEST_DECL(test_wc_ecc_mulmod),
  63129. TEST_DECL(test_wc_ecc_is_valid_idx),
  63130. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  63131. TEST_DECL(test_wc_ecc_sig_size_calc),
  63132. TEST_DECL(test_wc_EccPrivateKeyToDer),
  63133. /* SM2 elliptic curve */
  63134. TEST_DECL(test_wc_ecc_sm2_make_key),
  63135. TEST_DECL(test_wc_ecc_sm2_shared_secret),
  63136. TEST_DECL(test_wc_ecc_sm2_create_digest),
  63137. TEST_DECL(test_wc_ecc_sm2_verify_hash_ex),
  63138. TEST_DECL(test_wc_ecc_sm2_verify_hash),
  63139. TEST_DECL(test_wc_ecc_sm2_sign_hash_ex),
  63140. TEST_DECL(test_wc_ecc_sm2_sign_hash),
  63141. /* Curve25519 */
  63142. TEST_DECL(test_wc_curve25519_init),
  63143. TEST_DECL(test_wc_curve25519_size),
  63144. TEST_DECL(test_wc_curve25519_export_key_raw),
  63145. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  63146. TEST_DECL(test_wc_curve25519_make_key),
  63147. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  63148. TEST_DECL(test_wc_curve25519_make_pub),
  63149. TEST_DECL(test_wc_curve25519_export_public_ex),
  63150. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  63151. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  63152. TEST_DECL(test_wc_curve25519_import_private),
  63153. /* ED25519 */
  63154. TEST_DECL(test_wc_ed25519_make_key),
  63155. TEST_DECL(test_wc_ed25519_init),
  63156. TEST_DECL(test_wc_ed25519_sign_msg),
  63157. TEST_DECL(test_wc_ed25519_import_public),
  63158. TEST_DECL(test_wc_ed25519_import_private_key),
  63159. TEST_DECL(test_wc_ed25519_export),
  63160. TEST_DECL(test_wc_ed25519_size),
  63161. TEST_DECL(test_wc_ed25519_exportKey),
  63162. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  63163. TEST_DECL(test_wc_Ed25519KeyToDer),
  63164. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  63165. /* Curve448 */
  63166. TEST_DECL(test_wc_curve448_make_key),
  63167. TEST_DECL(test_wc_curve448_shared_secret_ex),
  63168. TEST_DECL(test_wc_curve448_export_public_ex),
  63169. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  63170. TEST_DECL(test_wc_curve448_export_key_raw),
  63171. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  63172. TEST_DECL(test_wc_curve448_import_private),
  63173. TEST_DECL(test_wc_curve448_init),
  63174. TEST_DECL(test_wc_curve448_size),
  63175. /* Ed448 */
  63176. TEST_DECL(test_wc_ed448_make_key),
  63177. TEST_DECL(test_wc_ed448_init),
  63178. TEST_DECL(test_wc_ed448_sign_msg),
  63179. TEST_DECL(test_wc_ed448_import_public),
  63180. TEST_DECL(test_wc_ed448_import_private_key),
  63181. TEST_DECL(test_wc_ed448_export),
  63182. TEST_DECL(test_wc_ed448_size),
  63183. TEST_DECL(test_wc_ed448_exportKey),
  63184. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  63185. TEST_DECL(test_wc_Ed448KeyToDer),
  63186. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  63187. /* Signature API */
  63188. TEST_DECL(test_wc_SignatureGetSize_ecc),
  63189. TEST_DECL(test_wc_SignatureGetSize_rsa),
  63190. /* PEM and DER APIs. */
  63191. TEST_DECL(test_wc_PemToDer),
  63192. TEST_DECL(test_wc_AllocDer),
  63193. TEST_DECL(test_wc_CertPemToDer),
  63194. TEST_DECL(test_wc_KeyPemToDer),
  63195. TEST_DECL(test_wc_PubKeyPemToDer),
  63196. TEST_DECL(test_wc_PemPubKeyToDer),
  63197. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  63198. TEST_DECL(test_wc_CheckCertSigPubKey),
  63199. /* wolfCrypt ASN tests */
  63200. TEST_DECL(test_ToTraditional),
  63201. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  63202. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  63203. /* Certificate */
  63204. TEST_DECL(test_wc_SetSubjectRaw),
  63205. TEST_DECL(test_wc_GetSubjectRaw),
  63206. TEST_DECL(test_wc_SetIssuerRaw),
  63207. TEST_DECL(test_wc_SetIssueBuffer),
  63208. TEST_DECL(test_wc_SetSubjectKeyId),
  63209. TEST_DECL(test_wc_SetSubject),
  63210. TEST_DECL(test_CheckCertSignature),
  63211. TEST_DECL(test_wc_ParseCert),
  63212. TEST_DECL(test_wc_ParseCert_Error),
  63213. TEST_DECL(test_MakeCertWithPathLen),
  63214. TEST_DECL(test_MakeCertWithCaFalse),
  63215. TEST_DECL(test_wc_SetKeyUsage),
  63216. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  63217. TEST_DECL(test_wc_SetSubjectBuffer),
  63218. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  63219. /* wolfcrypt PKCS#7 */
  63220. TEST_DECL(test_wc_PKCS7_New),
  63221. TEST_DECL(test_wc_PKCS7_Init),
  63222. TEST_DECL(test_wc_PKCS7_InitWithCert),
  63223. TEST_DECL(test_wc_PKCS7_EncodeData),
  63224. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  63225. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  63226. TEST_DECL(test_wc_PKCS7_VerifySignedData_RSA),
  63227. TEST_DECL(test_wc_PKCS7_VerifySignedData_ECC),
  63228. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  63229. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  63230. TEST_DECL(test_wc_PKCS7_Degenerate),
  63231. TEST_DECL(test_wc_PKCS7_BER),
  63232. TEST_DECL(test_wc_PKCS7_signed_enveloped),
  63233. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  63234. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  63235. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  63236. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  63237. /* wolfCrypt PKCS#12 */
  63238. TEST_DECL(test_wc_i2d_PKCS12),
  63239. /*
  63240. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  63241. * avoid memory leaks.
  63242. */
  63243. TEST_DECL(test_wolfCrypt_Cleanup),
  63244. TEST_DECL(test_wolfSSL_Init),
  63245. TEST_DECL(test_dual_alg_support),
  63246. /*********************************
  63247. * OpenSSL compatibility API tests
  63248. *********************************/
  63249. /* If at some point a stub get implemented this test should fail indicating
  63250. * a need to implement a new test case
  63251. */
  63252. TEST_DECL(test_stubs_are_stubs),
  63253. /* ASN.1 compatibility API tests */
  63254. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  63255. TEST_DECL(test_wolfSSL_ASN1_INTEGER),
  63256. TEST_DECL(test_wolfSSL_ASN1_INTEGER_cmp),
  63257. TEST_DECL(test_wolfSSL_ASN1_INTEGER_BN),
  63258. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  63259. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  63260. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  63261. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  63262. TEST_DECL(test_wolfSSL_ASN1_OBJECT),
  63263. TEST_DECL(test_wolfSSL_ASN1_get_object),
  63264. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  63265. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  63266. TEST_DECL(test_wolfSSL_sk_ASN1_OBJECT),
  63267. TEST_DECL(test_wolfSSL_ASN1_STRING),
  63268. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  63269. TEST_DECL(test_wolfSSL_i2s_ASN1_STRING),
  63270. TEST_DECL(test_wolfSSL_ASN1_STRING_canon),
  63271. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  63272. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  63273. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  63274. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  63275. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_print),
  63276. TEST_DECL(test_wolfSSL_ASN1_TIME),
  63277. TEST_DECL(test_wolfSSL_ASN1_TIME_to_string),
  63278. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  63279. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  63280. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  63281. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  63282. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  63283. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  63284. TEST_DECL(test_wolfSSL_ASN1_TYPE),
  63285. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  63286. TEST_DECL(test_wolfSSL_lhash),
  63287. TEST_DECL(test_wolfSSL_certs),
  63288. TEST_DECL(test_wolfSSL_private_keys),
  63289. TEST_DECL(test_wolfSSL_PEM_def_callback),
  63290. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  63291. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  63292. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  63293. TEST_DECL(test_wolfSSL_PEM_PrivateKey_rsa),
  63294. TEST_DECL(test_wolfSSL_PEM_PrivateKey_ecc),
  63295. TEST_DECL(test_wolfSSL_PEM_PrivateKey_dsa),
  63296. TEST_DECL(test_wolfSSL_PEM_PrivateKey_dh),
  63297. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  63298. TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
  63299. TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
  63300. #ifndef NO_BIO
  63301. TEST_DECL(test_wolfSSL_BIO),
  63302. TEST_DECL(test_wolfSSL_BIO_BIO_ring_read),
  63303. TEST_DECL(test_wolfSSL_PEM_read_bio),
  63304. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  63305. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  63306. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  63307. TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
  63308. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  63309. #endif
  63310. /* EVP API testing */
  63311. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  63312. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  63313. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  63314. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  63315. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  63316. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  63317. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  63318. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  63319. TEST_DECL(test_wolfSSL_EVP_shake128),
  63320. TEST_DECL(test_wolfSSL_EVP_shake256),
  63321. TEST_DECL(test_wolfSSL_EVP_sm3),
  63322. TEST_DECL(test_EVP_blake2),
  63323. #ifdef OPENSSL_ALL
  63324. TEST_DECL(test_wolfSSL_EVP_md4),
  63325. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  63326. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  63327. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  63328. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  63329. #endif
  63330. TEST_DECL(test_EVP_MD_do_all),
  63331. TEST_DECL(test_wolfSSL_EVP_MD_size),
  63332. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  63333. TEST_DECL(test_wolfSSL_EVP_Digest),
  63334. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  63335. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  63336. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  63337. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  63338. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  63339. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  63340. TEST_DECL(test_wolfssl_EVP_aes_gcm_zeroLen),
  63341. TEST_DECL(test_wolfssl_EVP_aes_ccm),
  63342. TEST_DECL(test_wolfssl_EVP_aes_ccm_zeroLen),
  63343. TEST_DECL(test_wolfssl_EVP_chacha20),
  63344. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  63345. TEST_DECL(test_wolfssl_EVP_sm4_ecb),
  63346. TEST_DECL(test_wolfssl_EVP_sm4_cbc),
  63347. TEST_DECL(test_wolfssl_EVP_sm4_ctr),
  63348. TEST_DECL(test_wolfssl_EVP_sm4_gcm_zeroLen),
  63349. TEST_DECL(test_wolfssl_EVP_sm4_gcm),
  63350. TEST_DECL(test_wolfssl_EVP_sm4_ccm_zeroLen),
  63351. TEST_DECL(test_wolfssl_EVP_sm4_ccm),
  63352. #ifdef OPENSSL_ALL
  63353. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  63354. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  63355. TEST_DECL(test_wolfSSL_EVP_aes_256_ccm),
  63356. TEST_DECL(test_wolfSSL_EVP_aes_192_ccm),
  63357. TEST_DECL(test_wolfSSL_EVP_aes_128_ccm),
  63358. TEST_DECL(test_wolfSSL_EVP_rc4),
  63359. TEST_DECL(test_wolfSSL_EVP_enc_null),
  63360. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  63361. TEST_DECL(test_wolfSSL_EVP_mdc2),
  63362. TEST_DECL(test_evp_cipher_aes_gcm),
  63363. #endif
  63364. TEST_DECL(test_wolfssl_EVP_aria_gcm),
  63365. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  63366. #ifdef OPENSSL_EXTRA
  63367. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  63368. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  63369. #endif
  63370. #ifdef OPENSSL_ALL
  63371. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  63372. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  63373. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  63374. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  63375. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  63376. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  63377. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  63378. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  63379. #endif
  63380. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  63381. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  63382. TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
  63383. TEST_DECL(test_wolfSSL_EVP_PKEY_up_ref),
  63384. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  63385. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  63386. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  63387. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey_ecc),
  63388. #ifndef NO_BIO
  63389. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  63390. #endif
  63391. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  63392. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  63393. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  63394. #ifndef NO_BIO
  63395. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  63396. #endif /* !NO_BIO */
  63397. #endif
  63398. #ifdef OPENSSL_ALL
  63399. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  63400. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  63401. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  63402. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  63403. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  63404. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  63405. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  63406. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  63407. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  63408. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  63409. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  63410. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  63411. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  63412. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  63413. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  63414. #endif
  63415. TEST_DECL(test_EVP_PKEY_rsa),
  63416. TEST_DECL(test_EVP_PKEY_ec),
  63417. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  63418. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_rsa),
  63419. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_dsa),
  63420. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_ec),
  63421. TEST_DECL(test_EVP_PKEY_cmp),
  63422. #ifdef OPENSSL_ALL
  63423. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  63424. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  63425. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  63426. #endif
  63427. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  63428. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  63429. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  63430. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  63431. #endif
  63432. #ifndef NO_BIO
  63433. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  63434. TEST_DECL(test_wolfSSL_GetLoggingCb),
  63435. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  63436. TEST_DECL(test_wc_ERR_remove_state),
  63437. TEST_DECL(test_wc_ERR_print_errors_fp),
  63438. #endif
  63439. TEST_DECL(test_wolfSSL_configure_args),
  63440. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  63441. TEST_DECL(test_wolfSSL_set1_curves_list),
  63442. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  63443. TEST_DECL(test_wolfSSL_OtherName),
  63444. TEST_DECL(test_wolfSSL_FPKI),
  63445. TEST_DECL(test_wolfSSL_URI),
  63446. TEST_DECL(test_wolfSSL_TBS),
  63447. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  63448. TEST_DECL(test_X509_STORE_untrusted),
  63449. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  63450. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  63451. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  63452. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  63453. TEST_DECL(test_wolfSSL_X509_Name_canon),
  63454. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  63455. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  63456. TEST_DECL(test_wolfSSL_X509_NID),
  63457. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  63458. TEST_DECL(test_wolfSSL_get0_param),
  63459. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  63460. TEST_DECL(test_wolfSSL_set1_host),
  63461. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  63462. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  63463. TEST_DECL(test_wolfSSL_X509_STORE),
  63464. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  63465. TEST_DECL(test_X509_STORE_get0_objects),
  63466. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  63467. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  63468. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  63469. TEST_DECL(test_wolfSSL_X509_cmp_time),
  63470. TEST_DECL(test_wolfSSL_X509_time_adj),
  63471. /* X509 tests */
  63472. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  63473. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  63474. TEST_DECL(test_wolfSSL_X509_check_host),
  63475. TEST_DECL(test_wolfSSL_X509_check_email),
  63476. TEST_DECL(test_wolfSSL_X509_check_private_key),
  63477. TEST_DECL(test_wolfSSL_X509),
  63478. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  63479. TEST_DECL(test_wolfSSL_X509_sign),
  63480. TEST_DECL(test_wolfSSL_X509_sign2),
  63481. TEST_DECL(test_wolfSSL_X509_verify),
  63482. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  63483. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  63484. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  63485. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  63486. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  63487. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  63488. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  63489. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  63490. TEST_DECL(test_wolfSSL_X509_NAME),
  63491. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  63492. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  63493. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  63494. TEST_DECL(test_wolfSSL_X509_set_name),
  63495. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  63496. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  63497. TEST_DECL(test_wolfSSL_X509_set_version),
  63498. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  63499. TEST_DECL(test_wolfSSL_X509_CRL),
  63500. TEST_DECL(test_wolfSSL_i2d_X509),
  63501. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  63502. TEST_DECL(test_wolfSSL_PEM_read_X509),
  63503. TEST_DECL(test_wolfSSL_X509_check_ca),
  63504. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  63505. TEST_DECL(test_wolfSSL_make_cert),
  63506. #ifndef NO_BIO
  63507. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  63508. TEST_DECL(test_wolfSSL_X509_INFO),
  63509. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  63510. #endif
  63511. #ifdef OPENSSL_ALL
  63512. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  63513. #endif
  63514. TEST_DECL(test_wolfSSL_X509_CA_num),
  63515. TEST_DECL(test_wolfSSL_X509_get_version),
  63516. #ifndef NO_BIO
  63517. TEST_DECL(test_wolfSSL_X509_print),
  63518. TEST_DECL(test_wolfSSL_X509_CRL_print),
  63519. #endif
  63520. TEST_DECL(test_X509_get_signature_nid),
  63521. /* X509 extension testing. */
  63522. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  63523. TEST_DECL(test_wolfSSL_X509_get_ext),
  63524. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  63525. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  63526. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  63527. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  63528. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  63529. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  63530. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  63531. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  63532. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  63533. TEST_DECL(test_wolfSSL_X509V3_EXT),
  63534. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  63535. TEST_DECL(test_wolfSSL_X509_cmp),
  63536. TEST_DECL(test_GENERAL_NAME_set0_othername),
  63537. TEST_DECL(test_othername_and_SID_ext),
  63538. TEST_DECL(test_wolfSSL_dup_CA_list),
  63539. /* OpenSSL sk_X509 API test */
  63540. TEST_DECL(test_sk_X509),
  63541. /* OpenSSL sk_X509_CRL API test */
  63542. TEST_DECL(test_sk_X509_CRL),
  63543. /* OpenSSL X509 REQ API test */
  63544. TEST_DECL(test_X509_REQ),
  63545. /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
  63546. TEST_DECL(test_X509_STORE_No_SSL_CTX),
  63547. TEST_DECL(test_X509_LOOKUP_add_dir),
  63548. /* RAND compatibility API */
  63549. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  63550. TEST_DECL(test_wolfSSL_RAND_bytes),
  63551. TEST_DECL(test_wolfSSL_RAND),
  63552. /* BN compatibility API */
  63553. TEST_DECL(test_wolfSSL_BN_CTX),
  63554. TEST_DECL(test_wolfSSL_BN),
  63555. TEST_DECL(test_wolfSSL_BN_init),
  63556. TEST_DECL(test_wolfSSL_BN_enc_dec),
  63557. TEST_DECL(test_wolfSSL_BN_word),
  63558. TEST_DECL(test_wolfSSL_BN_bits),
  63559. TEST_DECL(test_wolfSSL_BN_shift),
  63560. TEST_DECL(test_wolfSSL_BN_math),
  63561. TEST_DECL(test_wolfSSL_BN_math_mod),
  63562. TEST_DECL(test_wolfSSL_BN_math_other),
  63563. TEST_DECL(test_wolfSSL_BN_rand),
  63564. TEST_DECL(test_wolfSSL_BN_prime),
  63565. /* OpenSSL PKCS5 API test */
  63566. TEST_DECL(test_wolfSSL_PKCS5),
  63567. /* OpenSSL PKCS8 API test */
  63568. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  63569. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  63570. /* OpenSSL PKCS7 API test */
  63571. TEST_DECL(test_wolfssl_PKCS7),
  63572. TEST_DECL(test_wolfSSL_PKCS7_certs),
  63573. TEST_DECL(test_wolfSSL_PKCS7_sign),
  63574. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  63575. #ifndef NO_BIO
  63576. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  63577. #ifdef HAVE_SMIME
  63578. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  63579. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  63580. #endif /* HAVE_SMIME */
  63581. #endif /* !NO_BIO */
  63582. /* OpenSSL PKCS12 API test */
  63583. TEST_DECL(test_wolfSSL_PKCS12),
  63584. /* Can't memory test as callbacks use Assert. */
  63585. TEST_DECL(test_error_queue_per_thread),
  63586. TEST_DECL(test_wolfSSL_ERR_put_error),
  63587. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  63588. #ifndef NO_BIO
  63589. TEST_DECL(test_wolfSSL_ERR_print_errors),
  63590. #endif
  63591. TEST_DECL(test_OBJ_NAME_do_all),
  63592. TEST_DECL(test_wolfSSL_OBJ),
  63593. TEST_DECL(test_wolfSSL_OBJ_cmp),
  63594. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  63595. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  63596. #ifdef OPENSSL_ALL
  63597. TEST_DECL(test_wolfSSL_OBJ_ln),
  63598. TEST_DECL(test_wolfSSL_OBJ_sn),
  63599. #endif
  63600. #ifndef NO_BIO
  63601. TEST_DECL(test_wolfSSL_BIO_gets),
  63602. TEST_DECL(test_wolfSSL_BIO_puts),
  63603. TEST_DECL(test_wolfSSL_BIO_dump),
  63604. /* Can't memory test as server hangs. */
  63605. TEST_DECL(test_wolfSSL_BIO_should_retry),
  63606. TEST_DECL(test_wolfSSL_BIO_write),
  63607. TEST_DECL(test_wolfSSL_BIO_printf),
  63608. TEST_DECL(test_wolfSSL_BIO_f_md),
  63609. TEST_DECL(test_wolfSSL_BIO_up_ref),
  63610. TEST_DECL(test_wolfSSL_BIO_reset),
  63611. TEST_DECL(test_wolfSSL_BIO_get_len),
  63612. #endif
  63613. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  63614. TEST_DECL(test_wolfSSL_check_domain),
  63615. #endif
  63616. TEST_DECL(test_wolfSSL_cert_cb),
  63617. TEST_DECL(test_wolfSSL_cert_cb_dyn_ciphers),
  63618. TEST_DECL(test_wolfSSL_ciphersuite_auth),
  63619. TEST_DECL(test_wolfSSL_sigalg_info),
  63620. /* Can't memory test as tcp_connect aborts. */
  63621. TEST_DECL(test_wolfSSL_SESSION),
  63622. TEST_DECL(test_wolfSSL_SESSION_expire_downgrade),
  63623. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  63624. TEST_DECL(test_wolfSSL_ticket_keys),
  63625. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  63626. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  63627. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  63628. TEST_DECL(test_wolfSSL_verify_mode),
  63629. TEST_DECL(test_wolfSSL_verify_depth),
  63630. TEST_DECL(test_wolfSSL_verify_result),
  63631. TEST_DECL(test_wolfSSL_msg_callback),
  63632. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  63633. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  63634. TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
  63635. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  63636. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  63637. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  63638. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  63639. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  63640. TEST_DECL(test_wolfSSL_PEM_read),
  63641. TEST_DECL(test_wolfSSL_OpenSSL_version),
  63642. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  63643. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  63644. TEST_DECL(test_CONF_modules_xxx),
  63645. #ifdef OPENSSL_ALL
  63646. TEST_DECL(test_wolfSSL_TXT_DB),
  63647. TEST_DECL(test_wolfSSL_NCONF),
  63648. #endif
  63649. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  63650. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  63651. TEST_DECL(test_wolfSSL_SESSION_get_ex_new_index),
  63652. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  63653. TEST_DECL(test_CRYPTO_THREADID_xxx),
  63654. TEST_DECL(test_ENGINE_cleanup),
  63655. /* test the no op functions for compatibility */
  63656. TEST_DECL(test_no_op_functions),
  63657. /* OpenSSL error API tests */
  63658. TEST_DECL(test_ERR_load_crypto_strings),
  63659. #ifdef OPENSSL_ALL
  63660. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  63661. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  63662. TEST_DECL(test_wolfSSL_CTX_ctrl),
  63663. #endif /* OPENSSL_ALL */
  63664. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  63665. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  63666. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  63667. /*********************************
  63668. * Crypto API tests
  63669. *********************************/
  63670. TEST_DECL(test_wolfSSL_MD4),
  63671. TEST_DECL(test_wolfSSL_MD5),
  63672. TEST_DECL(test_wolfSSL_MD5_Transform),
  63673. TEST_DECL(test_wolfSSL_SHA),
  63674. TEST_DECL(test_wolfSSL_SHA_Transform),
  63675. TEST_DECL(test_wolfSSL_SHA224),
  63676. TEST_DECL(test_wolfSSL_SHA256),
  63677. TEST_DECL(test_wolfSSL_SHA256_Transform),
  63678. TEST_DECL(test_wolfSSL_SHA512_Transform),
  63679. TEST_DECL(test_wolfSSL_SHA512_224_Transform),
  63680. TEST_DECL(test_wolfSSL_SHA512_256_Transform),
  63681. TEST_DECL(test_wolfSSL_HMAC_CTX),
  63682. TEST_DECL(test_wolfSSL_HMAC),
  63683. TEST_DECL(test_wolfSSL_CMAC),
  63684. TEST_DECL(test_wolfSSL_DES),
  63685. TEST_DECL(test_wolfSSL_DES_ncbc),
  63686. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  63687. TEST_DECL(test_wolfSSL_DES_ede3_cbc_encrypt),
  63688. TEST_DECL(test_wolfSSL_AES_encrypt),
  63689. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  63690. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  63691. TEST_DECL(test_wolfSSL_AES_cfb128_encrypt),
  63692. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  63693. TEST_DECL(test_wolfSSL_RC4),
  63694. TEST_DECL(test_wolfSSL_RSA),
  63695. TEST_DECL(test_wolfSSL_RSA_DER),
  63696. TEST_DECL(test_wolfSSL_RSA_print),
  63697. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  63698. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  63699. TEST_DECL(test_wolfSSL_RSA_get0_key),
  63700. TEST_DECL(test_wolfSSL_RSA_meth),
  63701. TEST_DECL(test_wolfSSL_RSA_verify),
  63702. TEST_DECL(test_wolfSSL_RSA_sign),
  63703. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  63704. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  63705. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  63706. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  63707. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  63708. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  63709. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  63710. TEST_DECL(test_wolfSSL_RSA_ex_data),
  63711. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  63712. TEST_DECL(test_wolfSSL_RSA_To_Der),
  63713. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  63714. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  63715. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  63716. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  63717. TEST_DECL(test_wolfSSL_DH),
  63718. TEST_DECL(test_wolfSSL_DH_dup),
  63719. TEST_DECL(test_wolfSSL_DH_check),
  63720. TEST_DECL(test_wolfSSL_DH_prime),
  63721. TEST_DECL(test_wolfSSL_DH_1536_prime),
  63722. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  63723. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  63724. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  63725. TEST_DECL(test_wolfSSL_d2i_DHparams),
  63726. TEST_DECL(test_wolfSSL_DH_LoadDer),
  63727. TEST_DECL(test_wolfSSL_i2d_DHparams),
  63728. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  63729. TEST_DECL(test_wolfSSL_EC_GROUP),
  63730. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  63731. TEST_DECL(test_wolfSSL_EC_POINT),
  63732. TEST_DECL(test_wolfSSL_SPAKE),
  63733. TEST_DECL(test_wolfSSL_EC_KEY_generate),
  63734. TEST_DECL(test_EC_i2d),
  63735. TEST_DECL(test_wolfSSL_EC_curve),
  63736. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  63737. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  63738. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  63739. TEST_DECL(test_wolfSSL_EC_KEY_private_key),
  63740. TEST_DECL(test_wolfSSL_EC_KEY_public_key),
  63741. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  63742. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  63743. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  63744. TEST_DECL(test_ECDSA_size_sign),
  63745. TEST_DECL(test_ECDH_compute_key),
  63746. #endif
  63747. #ifdef OPENSSL_EXTRA
  63748. TEST_DECL(test_EC25519),
  63749. TEST_DECL(test_ED25519),
  63750. TEST_DECL(test_EC448),
  63751. TEST_DECL(test_ED448),
  63752. #endif
  63753. TEST_DECL(test_DSA_do_sign_verify),
  63754. #ifdef OPENSSL_ALL
  63755. TEST_DECL(test_wolfSSL_DSA_generate_parameters),
  63756. TEST_DECL(test_wolfSSL_DSA_SIG),
  63757. #endif
  63758. TEST_DECL(test_openssl_generate_key_and_cert),
  63759. TEST_DECL(test_wolfSSL_FIPS_mode),
  63760. TEST_DECL(test_openssl_FIPS_drbg),
  63761. /*********************************
  63762. * CertManager API tests
  63763. *********************************/
  63764. TEST_DECL(test_wolfSSL_CertManagerAPI),
  63765. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  63766. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer_ex),
  63767. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  63768. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  63769. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  63770. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  63771. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  63772. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  63773. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  63774. TEST_DECL(test_wolfSSL_CertManagerCRL),
  63775. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  63776. TEST_DECL(test_wolfSSL_CheckOCSPResponse),
  63777. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  63778. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  63779. !defined(WOLFSSL_NO_CLIENT_AUTH))
  63780. TEST_DECL(test_various_pathlen_chains),
  63781. #endif
  63782. /*********************************
  63783. * SSL/TLS API tests
  63784. *********************************/
  63785. TEST_DECL(test_wolfSSL_Method_Allocators),
  63786. #ifndef NO_WOLFSSL_SERVER
  63787. TEST_DECL(test_wolfSSL_CTX_new),
  63788. #endif
  63789. TEST_DECL(test_server_wolfSSL_new),
  63790. TEST_DECL(test_client_wolfSSL_new),
  63791. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  63792. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  63793. TEST_DECL(test_for_double_Free),
  63794. #endif
  63795. TEST_DECL(test_wolfSSL_set_options),
  63796. #ifdef WOLFSSL_TLS13
  63797. /* TLS v1.3 API tests */
  63798. TEST_DECL(test_tls13_apis),
  63799. TEST_DECL(test_tls13_cipher_suites),
  63800. #endif
  63801. TEST_DECL(test_wolfSSL_tmp_dh),
  63802. TEST_DECL(test_wolfSSL_ctrl),
  63803. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  63804. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  63805. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  63806. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  63807. TEST_DECL(test_wolfSSL_set_SSL_CTX),
  63808. #endif
  63809. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  63810. TEST_DECL(test_wolfSSL_security_level),
  63811. TEST_DECL(test_wolfSSL_SSL_in_init),
  63812. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  63813. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  63814. TEST_DECL(test_CONF_CTX_FILE),
  63815. TEST_DECL(test_CONF_CTX_CMDLINE),
  63816. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  63817. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  63818. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  63819. /* Bad certificate signature tests */
  63820. TEST_DECL(test_EccSigFailure_cm),
  63821. TEST_DECL(test_RsaSigFailure_cm),
  63822. #endif /* NO_CERTS */
  63823. /* PKCS8 testing */
  63824. TEST_DECL(test_wolfSSL_no_password_cb),
  63825. TEST_DECL(test_wolfSSL_PKCS8),
  63826. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  63827. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  63828. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  63829. TEST_DECL(test_wolfSSL_get_finished),
  63830. /* Uses Assert in handshake callback. */
  63831. TEST_DECL(test_wolfSSL_CTX_add_session),
  63832. /* Large number of memory allocations. */
  63833. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls13),
  63834. /* Large number of memory allocations. */
  63835. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls13),
  63836. /* Large number of memory allocations. */
  63837. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls12),
  63838. /* Large number of memory allocations. */
  63839. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls12),
  63840. /* Large number of memory allocations. */
  63841. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls11),
  63842. /* Large number of memory allocations. */
  63843. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls1),
  63844. #endif
  63845. TEST_DECL(test_SSL_CIPHER_get_xxx),
  63846. TEST_DECL(test_wolfSSL_ERR_strings),
  63847. TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
  63848. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  63849. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  63850. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  63851. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  63852. /* Large number of memory allocations. */
  63853. TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
  63854. TEST_DECL(test_wolfSSL_CertRsaPss),
  63855. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  63856. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  63857. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  63858. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  63859. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  63860. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  63861. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  63862. TEST_DECL(test_multiple_crls_same_issuer),
  63863. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  63864. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  63865. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  63866. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  63867. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  63868. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  63869. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  63870. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  63871. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  63872. TEST_DECL(test_SetTmpEC_DHE_Sz),
  63873. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  63874. #ifdef WOLFSSL_DTLS
  63875. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  63876. TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
  63877. #endif
  63878. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  63879. /* Uses Assert in handshake callback. */
  63880. TEST_DECL(test_wolfSSL_dtls_plaintext),
  63881. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  63882. defined(HAVE_IO_TESTS_DEPENDENCIES)
  63883. TEST_DECL(test_wolfSSL_read_write),
  63884. /* Can't memory test as server hangs if client fails before second connect.
  63885. */
  63886. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  63887. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_1),
  63888. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_2),
  63889. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_3),
  63890. TEST_DECL(test_wolfSSL_CTX_set_cipher_list),
  63891. /* Can't memory test as server hangs. */
  63892. TEST_DECL(test_wolfSSL_dtls_export),
  63893. /* Uses Assert in handshake callback. */
  63894. TEST_DECL(test_wolfSSL_tls_export),
  63895. #endif
  63896. TEST_DECL(test_wolfSSL_dtls_export_peers),
  63897. TEST_DECL(test_wolfSSL_SetMinVersion),
  63898. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  63899. /* wolfSSL handshake APIs. */
  63900. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  63901. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  63902. TEST_DECL(test_wolfSSL_BUF),
  63903. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  63904. /* Can't memory test as server hangs. */
  63905. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  63906. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  63907. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  63908. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  63909. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  63910. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  63911. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  63912. /* Can't memory test as server hangs. */
  63913. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  63914. TEST_DECL(test_wolfSSL_Tls13_postauth),
  63915. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  63916. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  63917. TEST_DECL(test_wolfSSL_CTX_set_max_proto_version),
  63918. TEST_DECL(test_wolfSSL_THREADID_hash),
  63919. /* TLS extensions tests */
  63920. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  63921. #ifdef HAVE_SNI
  63922. TEST_DECL(test_wolfSSL_UseSNI_params),
  63923. /* Uses Assert in handshake callback. */
  63924. TEST_DECL(test_wolfSSL_UseSNI_connection),
  63925. TEST_DECL(test_wolfSSL_SNI_GetFromBuffer),
  63926. #endif /* HAVE_SNI */
  63927. #endif
  63928. TEST_DECL(test_wolfSSL_UseTrustedCA),
  63929. TEST_DECL(test_wolfSSL_UseMaxFragment),
  63930. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  63931. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  63932. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  63933. /* Uses Assert in handshake callback. */
  63934. TEST_DECL(test_wolfSSL_UseALPN_connection),
  63935. TEST_DECL(test_wolfSSL_UseALPN_params),
  63936. #endif
  63937. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  63938. /* Uses Assert in handshake callback. */
  63939. TEST_DECL(test_wolfSSL_set_alpn_protos),
  63940. #endif
  63941. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  63942. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  63943. TEST_DECL(test_wolfSSL_SCR_Reconnect),
  63944. TEST_DECL(test_tls_ext_duplicate),
  63945. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  63946. defined(HAVE_IO_TESTS_DEPENDENCIES)
  63947. TEST_DECL(test_wolfSSL_Tls13_ECH_params),
  63948. /* Uses Assert in handshake callback. */
  63949. TEST_DECL(test_wolfSSL_Tls13_ECH),
  63950. #endif
  63951. TEST_DECL(test_wolfSSL_X509_TLS_version_test_1),
  63952. TEST_DECL(test_wolfSSL_X509_TLS_version_test_2),
  63953. /* OCSP Stapling */
  63954. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  63955. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  63956. TEST_DECL(test_self_signed_stapling),
  63957. /* Multicast */
  63958. TEST_DECL(test_wolfSSL_mcast),
  63959. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  63960. TEST_DECL(test_wolfSSL_msgCb),
  63961. TEST_DECL(test_wolfSSL_either_side),
  63962. TEST_DECL(test_wolfSSL_DTLS_either_side),
  63963. /* Uses Assert in handshake callback. */
  63964. TEST_DECL(test_wolfSSL_dtls_fragments),
  63965. /* Uses Assert in handshake callback. */
  63966. TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
  63967. /* Uses Assert in handshake callback. */
  63968. TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
  63969. /* Uses Assert in handshake callback. */
  63970. TEST_DECL(test_wolfSSL_dtls_bad_record),
  63971. /* Uses Assert in handshake callback. */
  63972. TEST_DECL(test_wolfSSL_dtls_stateless),
  63973. TEST_DECL(test_generate_cookie),
  63974. #ifndef NO_BIO
  63975. /* Can't memory test as server hangs. */
  63976. TEST_DECL(test_wolfSSL_BIO_connect),
  63977. /* Can't memory test as server Asserts in thread. */
  63978. TEST_DECL(test_wolfSSL_BIO_accept),
  63979. TEST_DECL(test_wolfSSL_BIO_tls),
  63980. #endif
  63981. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  63982. TEST_DECL(test_DhCallbacks),
  63983. #endif
  63984. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  63985. TEST_DECL(test_export_keying_material),
  63986. #endif
  63987. /* Can't memory test as client/server Asserts in thread. */
  63988. TEST_DECL(test_ticket_and_psk_mixing),
  63989. /* Can't memory test as client/server Asserts in thread. */
  63990. TEST_DECL(test_prioritize_psk),
  63991. /* Can't memory test as client/server hangs. */
  63992. TEST_DECL(test_wc_CryptoCb),
  63993. /* Can't memory test as client/server hangs. */
  63994. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  63995. #if !defined(NO_FILESYSTEM) && \
  63996. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  63997. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  63998. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  63999. TEST_DECL(test_wolfSSL_dtls_stateless_resume),
  64000. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  64001. #ifdef HAVE_MAX_FRAGMENT
  64002. TEST_DECL(test_wolfSSL_dtls_stateless_maxfrag),
  64003. #endif /* HAVE_MAX_FRAGMENT */
  64004. #ifndef NO_RSA
  64005. TEST_DECL(test_wolfSSL_dtls_stateless2),
  64006. #if !defined(NO_OLD_TLS)
  64007. TEST_DECL(test_wolfSSL_dtls_stateless_downgrade),
  64008. #endif /* !defined(NO_OLD_TLS) */
  64009. #endif /* ! NO_RSA */
  64010. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  64011. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) */
  64012. TEST_DECL(test_wolfSSL_CTX_set_ciphersuites),
  64013. TEST_DECL(test_wolfSSL_CRL_CERT_REVOKED_alert),
  64014. TEST_DECL(test_TLS_13_ticket_different_ciphers),
  64015. TEST_DECL(test_WOLFSSL_dtls_version_alert),
  64016. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  64017. && defined(WOLFSSL_TLS13) && \
  64018. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  64019. TEST_DECL(test_ticket_nonce_malloc),
  64020. #endif
  64021. TEST_DECL(test_ticket_ret_create),
  64022. TEST_DECL(test_extra_alerts_wrong_cs),
  64023. TEST_DECL(test_extra_alerts_skip_hs),
  64024. TEST_DECL(test_extra_alerts_bad_psk),
  64025. TEST_DECL(test_tls13_bad_psk_binder),
  64026. /* Can't memory test as client/server Asserts. */
  64027. TEST_DECL(test_harden_no_secure_renegotiation),
  64028. TEST_DECL(test_override_alt_cert_chain),
  64029. TEST_DECL(test_rpk_set_xxx_cert_type),
  64030. TEST_DECL(test_tls13_rpk_handshake),
  64031. TEST_DECL(test_dtls13_bad_epoch_ch),
  64032. TEST_DECL(test_short_session_id),
  64033. TEST_DECL(test_wolfSSL_dtls13_null_cipher),
  64034. /* Can't memory test as client/server hangs. */
  64035. TEST_DECL(test_dtls_msg_from_other_peer),
  64036. TEST_DECL(test_dtls_ipv6_check),
  64037. TEST_DECL(test_wolfSSL_SCR_after_resumption),
  64038. TEST_DECL(test_dtls_no_extensions),
  64039. TEST_DECL(test_TLSX_CA_NAMES_bad_extension),
  64040. TEST_DECL(test_dtls_1_0_hvr_downgrade),
  64041. TEST_DECL(test_session_ticket_no_id),
  64042. TEST_DECL(test_session_ticket_hs_update),
  64043. TEST_DECL(test_dtls_downgrade_scr_server),
  64044. TEST_DECL(test_dtls_downgrade_scr),
  64045. TEST_DECL(test_dtls_client_hello_timeout_downgrade),
  64046. TEST_DECL(test_dtls_client_hello_timeout),
  64047. TEST_DECL(test_dtls_dropped_ccs),
  64048. TEST_DECL(test_dtls_seq_num_downgrade),
  64049. TEST_DECL(test_certreq_sighash_algos),
  64050. TEST_DECL(test_revoked_loaded_int_cert),
  64051. TEST_DECL(test_dtls_frag_ch),
  64052. TEST_DECL(test_dtls13_frag_ch_pq),
  64053. TEST_DECL(test_dtls_empty_keyshare_with_cookie),
  64054. TEST_DECL(test_tls13_pq_groups),
  64055. TEST_DECL(test_tls13_early_data),
  64056. TEST_DECL(test_tls_multi_handshakes_one_record),
  64057. TEST_DECL(test_write_dup),
  64058. TEST_DECL(test_read_write_hs),
  64059. TEST_DECL(test_get_signature_nid),
  64060. TEST_DECL(test_tls_cert_store_unchanged),
  64061. /* This test needs to stay at the end to clean up any caches allocated. */
  64062. TEST_DECL(test_wolfSSL_Cleanup)
  64063. };
  64064. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  64065. static void TestSetup(void)
  64066. {
  64067. /* Stub, for now. Add common test setup code here. */
  64068. }
  64069. static void TestCleanup(void)
  64070. {
  64071. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  64072. /* Clear any errors added to the error queue during the test run. */
  64073. wolfSSL_ERR_clear_error();
  64074. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  64075. }
  64076. /* Print out all API test cases with numeric identifier.
  64077. */
  64078. void ApiTest_PrintTestCases(void)
  64079. {
  64080. int i;
  64081. printf("All Test Cases:\n");
  64082. for (i = 0; i < TEST_CASE_CNT; i++) {
  64083. printf("%3d: %s\n", i + 1, testCases[i].name);
  64084. }
  64085. }
  64086. /* Add test case with index to the list to run.
  64087. *
  64088. * @param [in] idx Index of test case to run starting at 1.
  64089. * @return 0 on success.
  64090. * @return BAD_FUNC_ARG when index is out of range of test case identifiers.
  64091. */
  64092. int ApiTest_RunIdx(int idx)
  64093. {
  64094. if (idx < 1 || idx > TEST_CASE_CNT) {
  64095. printf("Index out of range (1 - %d): %d\n", TEST_CASE_CNT, idx);
  64096. return BAD_FUNC_ARG;
  64097. }
  64098. testAll = 0;
  64099. testCases[idx-1].run = 1;
  64100. return 0;
  64101. }
  64102. /* Add test case with name to the list to run.
  64103. *
  64104. * @param [in] name Name of test case to run.
  64105. * @return 0 on success.
  64106. * @return BAD_FUNC_ARG when name is not a known test case name.
  64107. */
  64108. int ApiTest_RunName(char* name)
  64109. {
  64110. int i;
  64111. for (i = 0; i < TEST_CASE_CNT; i++) {
  64112. if (XSTRCMP(testCases[i].name, name) == 0) {
  64113. testAll = 0;
  64114. testCases[i].run = 1;
  64115. return 0;
  64116. }
  64117. }
  64118. printf("Test case name not found: %s\n", name);
  64119. printf("Use --list to see all test case names.\n");
  64120. return BAD_FUNC_ARG;
  64121. }
  64122. /* Converts the result code to a string.
  64123. *
  64124. * @param [in] res Test result code.
  64125. * @return String describing test result.
  64126. */
  64127. static const char* apitest_res_string(int res)
  64128. {
  64129. const char* str = "invalid result";
  64130. switch (res) {
  64131. case TEST_SUCCESS:
  64132. str = "passed";
  64133. break;
  64134. case TEST_FAIL:
  64135. str = "failed";
  64136. break;
  64137. case TEST_SKIPPED:
  64138. str = "skipped";
  64139. break;
  64140. }
  64141. return str;
  64142. }
  64143. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  64144. static double gettime_secs(void)
  64145. #if defined(_MSC_VER) && defined(_WIN32)
  64146. {
  64147. /* there's no gettimeofday for Windows, so we'll use system time */
  64148. #define EPOCH_DIFF 11644473600LL
  64149. FILETIME currentFileTime;
  64150. GetSystemTimePreciseAsFileTime(&currentFileTime);
  64151. ULARGE_INTEGER uli = { 0, 0 };
  64152. uli.LowPart = currentFileTime.dwLowDateTime;
  64153. uli.HighPart = currentFileTime.dwHighDateTime;
  64154. /* Convert to seconds since Unix epoch */
  64155. return (double)((uli.QuadPart - (EPOCH_DIFF * 10000000)) / 10000000.0);
  64156. }
  64157. #else
  64158. {
  64159. struct timeval tv;
  64160. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  64161. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
  64162. }
  64163. #endif
  64164. #endif
  64165. int ApiTest(void)
  64166. {
  64167. int i;
  64168. int ret;
  64169. int res = 0;
  64170. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  64171. double timeDiff;
  64172. #endif
  64173. printf(" Begin API Tests\n");
  64174. fflush(stdout);
  64175. /* we must perform init and cleanup if not all tests are running */
  64176. if (!testAll) {
  64177. #ifdef WOLFCRYPT_ONLY
  64178. if (wolfCrypt_Init() != 0) {
  64179. printf("wolfCrypt Initialization failed\n");
  64180. res = 1;
  64181. }
  64182. #else
  64183. if (wolfSSL_Init() != WOLFSSL_SUCCESS) {
  64184. printf("wolfSSL Initialization failed\n");
  64185. res = 1;
  64186. }
  64187. #endif
  64188. }
  64189. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  64190. if (res == 0) {
  64191. if (create_tmp_dir(tmpDirName, sizeof(tmpDirName) - 1) == NULL) {
  64192. printf("failed to create tmp dir\n");
  64193. res = 1;
  64194. }
  64195. else {
  64196. tmpDirNameSet = 1;
  64197. }
  64198. }
  64199. #endif
  64200. if (res == 0) {
  64201. for (i = 0; i < TEST_CASE_CNT; ++i) {
  64202. EXPECT_DECLS;
  64203. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  64204. currentTestName = testCases[i].name;
  64205. #endif
  64206. /* When not testing all cases then skip if not marked for running.
  64207. */
  64208. if (!testAll && !testCases[i].run) {
  64209. continue;
  64210. }
  64211. TestSetup();
  64212. printf(" %3d: %-52s:", i + 1, testCases[i].name);
  64213. fflush(stdout);
  64214. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  64215. timeDiff = gettime_secs();
  64216. #endif
  64217. ret = testCases[i].func();
  64218. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  64219. timeDiff = gettime_secs() - timeDiff;
  64220. #endif
  64221. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  64222. if (ret != TEST_SKIPPED) {
  64223. printf(" %s (%9.5lf)\n", apitest_res_string(ret), timeDiff);
  64224. }
  64225. else
  64226. #endif
  64227. {
  64228. printf(" %s\n", apitest_res_string(ret));
  64229. }
  64230. fflush(stdout);
  64231. /* if return code is < 0 and not skipped then assert error */
  64232. Expect((ret > 0 || ret == TEST_SKIPPED),
  64233. ("Test failed\n"),
  64234. ("ret %d", ret));
  64235. testCases[i].fail = ((ret <= 0) && (ret != TEST_SKIPPED));
  64236. res |= ((ret <= 0) && (ret != TEST_SKIPPED));
  64237. TestCleanup();
  64238. }
  64239. }
  64240. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  64241. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  64242. wc_ecc_fp_free(); /* free per thread cache */
  64243. #endif
  64244. if (!testAll) {
  64245. #ifdef WOLFCRYPT_ONLY
  64246. wolfCrypt_Cleanup();
  64247. #else
  64248. wolfSSL_Cleanup();
  64249. #endif
  64250. }
  64251. (void)testDevId;
  64252. if (res != 0) {
  64253. printf("\nFAILURES:\n");
  64254. for (i = 0; i < TEST_CASE_CNT; ++i) {
  64255. if (testCases[i].fail) {
  64256. printf(" %3d: %s\n", i + 1, testCases[i].name);
  64257. }
  64258. }
  64259. printf("\n");
  64260. fflush(stdout);
  64261. }
  64262. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  64263. if (tmpDirNameSet) {
  64264. printf("\nBinary dumps of the memio streams can be found in the\n"
  64265. "%s directory. This can be imported into\n"
  64266. "Wireshark by transforming the file with\n"
  64267. "\tod -Ax -tx1 -v stream.dump > stream.dump.hex\n"
  64268. "And then loading test_output.dump.hex into Wireshark using\n"
  64269. "the \"Import from Hex Dump...\" option and selecting the\n"
  64270. "TCP encapsulation option.\n", tmpDirName);
  64271. }
  64272. #endif
  64273. printf(" End API Tests\n");
  64274. fflush(stdout);
  64275. return res;
  64276. }