api.c 2.5 MB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156211572115821159211602116121162211632116421165211662116721168211692117021171211722117321174211752117621177211782117921180211812118221183211842118521186211872118821189211902119121192211932119421195211962119721198211992120021201212022120321204212052120621207212082120921210212112121221213212142121521216212172121821219212202122121222212232122421225212262122721228212292123021231212322123321234212352123621237212382123921240212412124221243212442124521246212472124821249212502125121252212532125421255212562125721258212592126021261212622126321264212652126621267212682126921270212712127221273212742127521276212772127821279212802128121282212832128421285212862128721288212892129021291212922129321294212952129621297212982129921300213012130221303213042130521306213072130821309213102131121312213132131421315213162131721318213192132021321213222132321324213252132621327213282132921330213312133221333213342133521336213372133821339213402134121342213432134421345213462134721348213492135021351213522135321354213552135621357213582135921360213612136221363213642136521366213672136821369213702137121372213732137421375213762137721378213792138021381213822138321384213852138621387213882138921390213912139221393213942139521396213972139821399214002140121402214032140421405214062140721408214092141021411214122141321414214152141621417214182141921420214212142221423214242142521426214272142821429214302143121432214332143421435214362143721438214392144021441214422144321444214452144621447214482144921450214512145221453214542145521456214572145821459214602146121462214632146421465214662146721468214692147021471214722147321474214752147621477214782147921480214812148221483214842148521486214872148821489214902149121492214932149421495214962149721498214992150021501215022150321504215052150621507215082150921510215112151221513215142151521516215172151821519215202152121522215232152421525215262152721528215292153021531215322153321534215352153621537215382153921540215412154221543215442154521546215472154821549215502155121552215532155421555215562155721558215592156021561215622156321564215652156621567215682156921570215712157221573215742157521576215772157821579215802158121582215832158421585215862158721588215892159021591215922159321594215952159621597215982159921600216012160221603216042160521606216072160821609216102161121612216132161421615216162161721618216192162021621216222162321624216252162621627216282162921630216312163221633216342163521636216372163821639216402164121642216432164421645216462164721648216492165021651216522165321654216552165621657216582165921660216612166221663216642166521666216672166821669216702167121672216732167421675216762167721678216792168021681216822168321684216852168621687216882168921690216912169221693216942169521696216972169821699217002170121702217032170421705217062170721708217092171021711217122171321714217152171621717217182171921720217212172221723217242172521726217272172821729217302173121732217332173421735217362173721738217392174021741217422174321744217452174621747217482174921750217512175221753217542175521756217572175821759217602176121762217632176421765217662176721768217692177021771217722177321774217752177621777217782177921780217812178221783217842178521786217872178821789217902179121792217932179421795217962179721798217992180021801218022180321804218052180621807218082180921810218112181221813218142181521816218172181821819218202182121822218232182421825218262182721828218292183021831218322183321834218352183621837218382183921840218412184221843218442184521846218472184821849218502185121852218532185421855218562185721858218592186021861218622186321864218652186621867218682186921870218712187221873218742187521876218772187821879218802188121882218832188421885218862188721888218892189021891218922189321894218952189621897218982189921900219012190221903219042190521906219072190821909219102191121912219132191421915219162191721918219192192021921219222192321924219252192621927219282192921930219312193221933219342193521936219372193821939219402194121942219432194421945219462194721948219492195021951219522195321954219552195621957219582195921960219612196221963219642196521966219672196821969219702197121972219732197421975219762197721978219792198021981219822198321984219852198621987219882198921990219912199221993219942199521996219972199821999220002200122002220032200422005220062200722008220092201022011220122201322014220152201622017220182201922020220212202222023220242202522026220272202822029220302203122032220332203422035220362203722038220392204022041220422204322044220452204622047220482204922050220512205222053220542205522056220572205822059220602206122062220632206422065220662206722068220692207022071220722207322074220752207622077220782207922080220812208222083220842208522086220872208822089220902209122092220932209422095220962209722098220992210022101221022210322104221052210622107221082210922110221112211222113221142211522116221172211822119221202212122122221232212422125221262212722128221292213022131221322213322134221352213622137221382213922140221412214222143221442214522146221472214822149221502215122152221532215422155221562215722158221592216022161221622216322164221652216622167221682216922170221712217222173221742217522176221772217822179221802218122182221832218422185221862218722188221892219022191221922219322194221952219622197221982219922200222012220222203222042220522206222072220822209222102221122212222132221422215222162221722218222192222022221222222222322224222252222622227222282222922230222312223222233222342223522236222372223822239222402224122242222432224422245222462224722248222492225022251222522225322254222552225622257222582225922260222612226222263222642226522266222672226822269222702227122272222732227422275222762227722278222792228022281222822228322284222852228622287222882228922290222912229222293222942229522296222972229822299223002230122302223032230422305223062230722308223092231022311223122231322314223152231622317223182231922320223212232222323223242232522326223272232822329223302233122332223332233422335223362233722338223392234022341223422234322344223452234622347223482234922350223512235222353223542235522356223572235822359223602236122362223632236422365223662236722368223692237022371223722237322374223752237622377223782237922380223812238222383223842238522386223872238822389223902239122392223932239422395223962239722398223992240022401224022240322404224052240622407224082240922410224112241222413224142241522416224172241822419224202242122422224232242422425224262242722428224292243022431224322243322434224352243622437224382243922440224412244222443224442244522446224472244822449224502245122452224532245422455224562245722458224592246022461224622246322464224652246622467224682246922470224712247222473224742247522476224772247822479224802248122482224832248422485224862248722488224892249022491224922249322494224952249622497224982249922500225012250222503225042250522506225072250822509225102251122512225132251422515225162251722518225192252022521225222252322524225252252622527225282252922530225312253222533225342253522536225372253822539225402254122542225432254422545225462254722548225492255022551225522255322554225552255622557225582255922560225612256222563225642256522566225672256822569225702257122572225732257422575225762257722578225792258022581225822258322584225852258622587225882258922590225912259222593225942259522596225972259822599226002260122602226032260422605226062260722608226092261022611226122261322614226152261622617226182261922620226212262222623226242262522626226272262822629226302263122632226332263422635226362263722638226392264022641226422264322644226452264622647226482264922650226512265222653226542265522656226572265822659226602266122662226632266422665226662266722668226692267022671226722267322674226752267622677226782267922680226812268222683226842268522686226872268822689226902269122692226932269422695226962269722698226992270022701227022270322704227052270622707227082270922710227112271222713227142271522716227172271822719227202272122722227232272422725227262272722728227292273022731227322273322734227352273622737227382273922740227412274222743227442274522746227472274822749227502275122752227532275422755227562275722758227592276022761227622276322764227652276622767227682276922770227712277222773227742277522776227772277822779227802278122782227832278422785227862278722788227892279022791227922279322794227952279622797227982279922800228012280222803228042280522806228072280822809228102281122812228132281422815228162281722818228192282022821228222282322824228252282622827228282282922830228312283222833228342283522836228372283822839228402284122842228432284422845228462284722848228492285022851228522285322854228552285622857228582285922860228612286222863228642286522866228672286822869228702287122872228732287422875228762287722878228792288022881228822288322884228852288622887228882288922890228912289222893228942289522896228972289822899229002290122902229032290422905229062290722908229092291022911229122291322914229152291622917229182291922920229212292222923229242292522926229272292822929229302293122932229332293422935229362293722938229392294022941229422294322944229452294622947229482294922950229512295222953229542295522956229572295822959229602296122962229632296422965229662296722968229692297022971229722297322974229752297622977229782297922980229812298222983229842298522986229872298822989229902299122992229932299422995229962299722998229992300023001230022300323004230052300623007230082300923010230112301223013230142301523016230172301823019230202302123022230232302423025230262302723028230292303023031230322303323034230352303623037230382303923040230412304223043230442304523046230472304823049230502305123052230532305423055230562305723058230592306023061230622306323064230652306623067230682306923070230712307223073230742307523076230772307823079230802308123082230832308423085230862308723088230892309023091230922309323094230952309623097230982309923100231012310223103231042310523106231072310823109231102311123112231132311423115231162311723118231192312023121231222312323124231252312623127231282312923130231312313223133231342313523136231372313823139231402314123142231432314423145231462314723148231492315023151231522315323154231552315623157231582315923160231612316223163231642316523166231672316823169231702317123172231732317423175231762317723178231792318023181231822318323184231852318623187231882318923190231912319223193231942319523196231972319823199232002320123202232032320423205232062320723208232092321023211232122321323214232152321623217232182321923220232212322223223232242322523226232272322823229232302323123232232332323423235232362323723238232392324023241232422324323244232452324623247232482324923250232512325223253232542325523256232572325823259232602326123262232632326423265232662326723268232692327023271232722327323274232752327623277232782327923280232812328223283232842328523286232872328823289232902329123292232932329423295232962329723298232992330023301233022330323304233052330623307233082330923310233112331223313233142331523316233172331823319233202332123322233232332423325233262332723328233292333023331233322333323334233352333623337233382333923340233412334223343233442334523346233472334823349233502335123352233532335423355233562335723358233592336023361233622336323364233652336623367233682336923370233712337223373233742337523376233772337823379233802338123382233832338423385233862338723388233892339023391233922339323394233952339623397233982339923400234012340223403234042340523406234072340823409234102341123412234132341423415234162341723418234192342023421234222342323424234252342623427234282342923430234312343223433234342343523436234372343823439234402344123442234432344423445234462344723448234492345023451234522345323454234552345623457234582345923460234612346223463234642346523466234672346823469234702347123472234732347423475234762347723478234792348023481234822348323484234852348623487234882348923490234912349223493234942349523496234972349823499235002350123502235032350423505235062350723508235092351023511235122351323514235152351623517235182351923520235212352223523235242352523526235272352823529235302353123532235332353423535235362353723538235392354023541235422354323544235452354623547235482354923550235512355223553235542355523556235572355823559235602356123562235632356423565235662356723568235692357023571235722357323574235752357623577235782357923580235812358223583235842358523586235872358823589235902359123592235932359423595235962359723598235992360023601236022360323604236052360623607236082360923610236112361223613236142361523616236172361823619236202362123622236232362423625236262362723628236292363023631236322363323634236352363623637236382363923640236412364223643236442364523646236472364823649236502365123652236532365423655236562365723658236592366023661236622366323664236652366623667236682366923670236712367223673236742367523676236772367823679236802368123682236832368423685236862368723688236892369023691236922369323694236952369623697236982369923700237012370223703237042370523706237072370823709237102371123712237132371423715237162371723718237192372023721237222372323724237252372623727237282372923730237312373223733237342373523736237372373823739237402374123742237432374423745237462374723748237492375023751237522375323754237552375623757237582375923760237612376223763237642376523766237672376823769237702377123772237732377423775237762377723778237792378023781237822378323784237852378623787237882378923790237912379223793237942379523796237972379823799238002380123802238032380423805238062380723808238092381023811238122381323814238152381623817238182381923820238212382223823238242382523826238272382823829238302383123832238332383423835238362383723838238392384023841238422384323844238452384623847238482384923850238512385223853238542385523856238572385823859238602386123862238632386423865238662386723868238692387023871238722387323874238752387623877238782387923880238812388223883238842388523886238872388823889238902389123892238932389423895238962389723898238992390023901239022390323904239052390623907239082390923910239112391223913239142391523916239172391823919239202392123922239232392423925239262392723928239292393023931239322393323934239352393623937239382393923940239412394223943239442394523946239472394823949239502395123952239532395423955239562395723958239592396023961239622396323964239652396623967239682396923970239712397223973239742397523976239772397823979239802398123982239832398423985239862398723988239892399023991239922399323994239952399623997239982399924000240012400224003240042400524006240072400824009240102401124012240132401424015240162401724018240192402024021240222402324024240252402624027240282402924030240312403224033240342403524036240372403824039240402404124042240432404424045240462404724048240492405024051240522405324054240552405624057240582405924060240612406224063240642406524066240672406824069240702407124072240732407424075240762407724078240792408024081240822408324084240852408624087240882408924090240912409224093240942409524096240972409824099241002410124102241032410424105241062410724108241092411024111241122411324114241152411624117241182411924120241212412224123241242412524126241272412824129241302413124132241332413424135241362413724138241392414024141241422414324144241452414624147241482414924150241512415224153241542415524156241572415824159241602416124162241632416424165241662416724168241692417024171241722417324174241752417624177241782417924180241812418224183241842418524186241872418824189241902419124192241932419424195241962419724198241992420024201242022420324204242052420624207242082420924210242112421224213242142421524216242172421824219242202422124222242232422424225242262422724228242292423024231242322423324234242352423624237242382423924240242412424224243242442424524246242472424824249242502425124252242532425424255242562425724258242592426024261242622426324264242652426624267242682426924270242712427224273242742427524276242772427824279242802428124282242832428424285242862428724288242892429024291242922429324294242952429624297242982429924300243012430224303243042430524306243072430824309243102431124312243132431424315243162431724318243192432024321243222432324324243252432624327243282432924330243312433224333243342433524336243372433824339243402434124342243432434424345243462434724348243492435024351243522435324354243552435624357243582435924360243612436224363243642436524366243672436824369243702437124372243732437424375243762437724378243792438024381243822438324384243852438624387243882438924390243912439224393243942439524396243972439824399244002440124402244032440424405244062440724408244092441024411244122441324414244152441624417244182441924420244212442224423244242442524426244272442824429244302443124432244332443424435244362443724438244392444024441244422444324444244452444624447244482444924450244512445224453244542445524456244572445824459244602446124462244632446424465244662446724468244692447024471244722447324474244752447624477244782447924480244812448224483244842448524486244872448824489244902449124492244932449424495244962449724498244992450024501245022450324504245052450624507245082450924510245112451224513245142451524516245172451824519245202452124522245232452424525245262452724528245292453024531245322453324534245352453624537245382453924540245412454224543245442454524546245472454824549245502455124552245532455424555245562455724558245592456024561245622456324564245652456624567245682456924570245712457224573245742457524576245772457824579245802458124582245832458424585245862458724588245892459024591245922459324594245952459624597245982459924600246012460224603246042460524606246072460824609246102461124612246132461424615246162461724618246192462024621246222462324624246252462624627246282462924630246312463224633246342463524636246372463824639246402464124642246432464424645246462464724648246492465024651246522465324654246552465624657246582465924660246612466224663246642466524666246672466824669246702467124672246732467424675246762467724678246792468024681246822468324684246852468624687246882468924690246912469224693246942469524696246972469824699247002470124702247032470424705247062470724708247092471024711247122471324714247152471624717247182471924720247212472224723247242472524726247272472824729247302473124732247332473424735247362473724738247392474024741247422474324744247452474624747247482474924750247512475224753247542475524756247572475824759247602476124762247632476424765247662476724768247692477024771247722477324774247752477624777247782477924780247812478224783247842478524786247872478824789247902479124792247932479424795247962479724798247992480024801248022480324804248052480624807248082480924810248112481224813248142481524816248172481824819248202482124822248232482424825248262482724828248292483024831248322483324834248352483624837248382483924840248412484224843248442484524846248472484824849248502485124852248532485424855248562485724858248592486024861248622486324864248652486624867248682486924870248712487224873248742487524876248772487824879248802488124882248832488424885248862488724888248892489024891248922489324894248952489624897248982489924900249012490224903249042490524906249072490824909249102491124912249132491424915249162491724918249192492024921249222492324924249252492624927249282492924930249312493224933249342493524936249372493824939249402494124942249432494424945249462494724948249492495024951249522495324954249552495624957249582495924960249612496224963249642496524966249672496824969249702497124972249732497424975249762497724978249792498024981249822498324984249852498624987249882498924990249912499224993249942499524996249972499824999250002500125002250032500425005250062500725008250092501025011250122501325014250152501625017250182501925020250212502225023250242502525026250272502825029250302503125032250332503425035250362503725038250392504025041250422504325044250452504625047250482504925050250512505225053250542505525056250572505825059250602506125062250632506425065250662506725068250692507025071250722507325074250752507625077250782507925080250812508225083250842508525086250872508825089250902509125092250932509425095250962509725098250992510025101251022510325104251052510625107251082510925110251112511225113251142511525116251172511825119251202512125122251232512425125251262512725128251292513025131251322513325134251352513625137251382513925140251412514225143251442514525146251472514825149251502515125152251532515425155251562515725158251592516025161251622516325164251652516625167251682516925170251712517225173251742517525176251772517825179251802518125182251832518425185251862518725188251892519025191251922519325194251952519625197251982519925200252012520225203252042520525206252072520825209252102521125212252132521425215252162521725218252192522025221252222522325224252252522625227252282522925230252312523225233252342523525236252372523825239252402524125242252432524425245252462524725248252492525025251252522525325254252552525625257252582525925260252612526225263252642526525266252672526825269252702527125272252732527425275252762527725278252792528025281252822528325284252852528625287252882528925290252912529225293252942529525296252972529825299253002530125302253032530425305253062530725308253092531025311253122531325314253152531625317253182531925320253212532225323253242532525326253272532825329253302533125332253332533425335253362533725338253392534025341253422534325344253452534625347253482534925350253512535225353253542535525356253572535825359253602536125362253632536425365253662536725368253692537025371253722537325374253752537625377253782537925380253812538225383253842538525386253872538825389253902539125392253932539425395253962539725398253992540025401254022540325404254052540625407254082540925410254112541225413254142541525416254172541825419254202542125422254232542425425254262542725428254292543025431254322543325434254352543625437254382543925440254412544225443254442544525446254472544825449254502545125452254532545425455254562545725458254592546025461254622546325464254652546625467254682546925470254712547225473254742547525476254772547825479254802548125482254832548425485254862548725488254892549025491254922549325494254952549625497254982549925500255012550225503255042550525506255072550825509255102551125512255132551425515255162551725518255192552025521255222552325524255252552625527255282552925530255312553225533255342553525536255372553825539255402554125542255432554425545255462554725548255492555025551255522555325554255552555625557255582555925560255612556225563255642556525566255672556825569255702557125572255732557425575255762557725578255792558025581255822558325584255852558625587255882558925590255912559225593255942559525596255972559825599256002560125602256032560425605256062560725608256092561025611256122561325614256152561625617256182561925620256212562225623256242562525626256272562825629256302563125632256332563425635256362563725638256392564025641256422564325644256452564625647256482564925650256512565225653256542565525656256572565825659256602566125662256632566425665256662566725668256692567025671256722567325674256752567625677256782567925680256812568225683256842568525686256872568825689256902569125692256932569425695256962569725698256992570025701257022570325704257052570625707257082570925710257112571225713257142571525716257172571825719257202572125722257232572425725257262572725728257292573025731257322573325734257352573625737257382573925740257412574225743257442574525746257472574825749257502575125752257532575425755257562575725758257592576025761257622576325764257652576625767257682576925770257712577225773257742577525776257772577825779257802578125782257832578425785257862578725788257892579025791257922579325794257952579625797257982579925800258012580225803258042580525806258072580825809258102581125812258132581425815258162581725818258192582025821258222582325824258252582625827258282582925830258312583225833258342583525836258372583825839258402584125842258432584425845258462584725848258492585025851258522585325854258552585625857258582585925860258612586225863258642586525866258672586825869258702587125872258732587425875258762587725878258792588025881258822588325884258852588625887258882588925890258912589225893258942589525896258972589825899259002590125902259032590425905259062590725908259092591025911259122591325914259152591625917259182591925920259212592225923259242592525926259272592825929259302593125932259332593425935259362593725938259392594025941259422594325944259452594625947259482594925950259512595225953259542595525956259572595825959259602596125962259632596425965259662596725968259692597025971259722597325974259752597625977259782597925980259812598225983259842598525986259872598825989259902599125992259932599425995259962599725998259992600026001260022600326004260052600626007260082600926010260112601226013260142601526016260172601826019260202602126022260232602426025260262602726028260292603026031260322603326034260352603626037260382603926040260412604226043260442604526046260472604826049260502605126052260532605426055260562605726058260592606026061260622606326064260652606626067260682606926070260712607226073260742607526076260772607826079260802608126082260832608426085260862608726088260892609026091260922609326094260952609626097260982609926100261012610226103261042610526106261072610826109261102611126112261132611426115261162611726118261192612026121261222612326124261252612626127261282612926130261312613226133261342613526136261372613826139261402614126142261432614426145261462614726148261492615026151261522615326154261552615626157261582615926160261612616226163261642616526166261672616826169261702617126172261732617426175261762617726178261792618026181261822618326184261852618626187261882618926190261912619226193261942619526196261972619826199262002620126202262032620426205262062620726208262092621026211262122621326214262152621626217262182621926220262212622226223262242622526226262272622826229262302623126232262332623426235262362623726238262392624026241262422624326244262452624626247262482624926250262512625226253262542625526256262572625826259262602626126262262632626426265262662626726268262692627026271262722627326274262752627626277262782627926280262812628226283262842628526286262872628826289262902629126292262932629426295262962629726298262992630026301263022630326304263052630626307263082630926310263112631226313263142631526316263172631826319263202632126322263232632426325263262632726328263292633026331263322633326334263352633626337263382633926340263412634226343263442634526346263472634826349263502635126352263532635426355263562635726358263592636026361263622636326364263652636626367263682636926370263712637226373263742637526376263772637826379263802638126382263832638426385263862638726388263892639026391263922639326394263952639626397263982639926400264012640226403264042640526406264072640826409264102641126412264132641426415264162641726418264192642026421264222642326424264252642626427264282642926430264312643226433264342643526436264372643826439264402644126442264432644426445264462644726448264492645026451264522645326454264552645626457264582645926460264612646226463264642646526466264672646826469264702647126472264732647426475264762647726478264792648026481264822648326484264852648626487264882648926490264912649226493264942649526496264972649826499265002650126502265032650426505265062650726508265092651026511265122651326514265152651626517265182651926520265212652226523265242652526526265272652826529265302653126532265332653426535265362653726538265392654026541265422654326544265452654626547265482654926550265512655226553265542655526556265572655826559265602656126562265632656426565265662656726568265692657026571265722657326574265752657626577265782657926580265812658226583265842658526586265872658826589265902659126592265932659426595265962659726598265992660026601266022660326604266052660626607266082660926610266112661226613266142661526616266172661826619266202662126622266232662426625266262662726628266292663026631266322663326634266352663626637266382663926640266412664226643266442664526646266472664826649266502665126652266532665426655266562665726658266592666026661266622666326664266652666626667266682666926670266712667226673266742667526676266772667826679266802668126682266832668426685266862668726688266892669026691266922669326694266952669626697266982669926700267012670226703267042670526706267072670826709267102671126712267132671426715267162671726718267192672026721267222672326724267252672626727267282672926730267312673226733267342673526736267372673826739267402674126742267432674426745267462674726748267492675026751267522675326754267552675626757267582675926760267612676226763267642676526766267672676826769267702677126772267732677426775267762677726778267792678026781267822678326784267852678626787267882678926790267912679226793267942679526796267972679826799268002680126802268032680426805268062680726808268092681026811268122681326814268152681626817268182681926820268212682226823268242682526826268272682826829268302683126832268332683426835268362683726838268392684026841268422684326844268452684626847268482684926850268512685226853268542685526856268572685826859268602686126862268632686426865268662686726868268692687026871268722687326874268752687626877268782687926880268812688226883268842688526886268872688826889268902689126892268932689426895268962689726898268992690026901269022690326904269052690626907269082690926910269112691226913269142691526916269172691826919269202692126922269232692426925269262692726928269292693026931269322693326934269352693626937269382693926940269412694226943269442694526946269472694826949269502695126952269532695426955269562695726958269592696026961269622696326964269652696626967269682696926970269712697226973269742697526976269772697826979269802698126982269832698426985269862698726988269892699026991269922699326994269952699626997269982699927000270012700227003270042700527006270072700827009270102701127012270132701427015270162701727018270192702027021270222702327024270252702627027270282702927030270312703227033270342703527036270372703827039270402704127042270432704427045270462704727048270492705027051270522705327054270552705627057270582705927060270612706227063270642706527066270672706827069270702707127072270732707427075270762707727078270792708027081270822708327084270852708627087270882708927090270912709227093270942709527096270972709827099271002710127102271032710427105271062710727108271092711027111271122711327114271152711627117271182711927120271212712227123271242712527126271272712827129271302713127132271332713427135271362713727138271392714027141271422714327144271452714627147271482714927150271512715227153271542715527156271572715827159271602716127162271632716427165271662716727168271692717027171271722717327174271752717627177271782717927180271812718227183271842718527186271872718827189271902719127192271932719427195271962719727198271992720027201272022720327204272052720627207272082720927210272112721227213272142721527216272172721827219272202722127222272232722427225272262722727228272292723027231272322723327234272352723627237272382723927240272412724227243272442724527246272472724827249272502725127252272532725427255272562725727258272592726027261272622726327264272652726627267272682726927270272712727227273272742727527276272772727827279272802728127282272832728427285272862728727288272892729027291272922729327294272952729627297272982729927300273012730227303273042730527306273072730827309273102731127312273132731427315273162731727318273192732027321273222732327324273252732627327273282732927330273312733227333273342733527336273372733827339273402734127342273432734427345273462734727348273492735027351273522735327354273552735627357273582735927360273612736227363273642736527366273672736827369273702737127372273732737427375273762737727378273792738027381273822738327384273852738627387273882738927390273912739227393273942739527396273972739827399274002740127402274032740427405274062740727408274092741027411274122741327414274152741627417274182741927420274212742227423274242742527426274272742827429274302743127432274332743427435274362743727438274392744027441274422744327444274452744627447274482744927450274512745227453274542745527456274572745827459274602746127462274632746427465274662746727468274692747027471274722747327474274752747627477274782747927480274812748227483274842748527486274872748827489274902749127492274932749427495274962749727498274992750027501275022750327504275052750627507275082750927510275112751227513275142751527516275172751827519275202752127522275232752427525275262752727528275292753027531275322753327534275352753627537275382753927540275412754227543275442754527546275472754827549275502755127552275532755427555275562755727558275592756027561275622756327564275652756627567275682756927570275712757227573275742757527576275772757827579275802758127582275832758427585275862758727588275892759027591275922759327594275952759627597275982759927600276012760227603276042760527606276072760827609276102761127612276132761427615276162761727618276192762027621276222762327624276252762627627276282762927630276312763227633276342763527636276372763827639276402764127642276432764427645276462764727648276492765027651276522765327654276552765627657276582765927660276612766227663276642766527666276672766827669276702767127672276732767427675276762767727678276792768027681276822768327684276852768627687276882768927690276912769227693276942769527696276972769827699277002770127702277032770427705277062770727708277092771027711277122771327714277152771627717277182771927720277212772227723277242772527726277272772827729277302773127732277332773427735277362773727738277392774027741277422774327744277452774627747277482774927750277512775227753277542775527756277572775827759277602776127762277632776427765277662776727768277692777027771277722777327774277752777627777277782777927780277812778227783277842778527786277872778827789277902779127792277932779427795277962779727798277992780027801278022780327804278052780627807278082780927810278112781227813278142781527816278172781827819278202782127822278232782427825278262782727828278292783027831278322783327834278352783627837278382783927840278412784227843278442784527846278472784827849278502785127852278532785427855278562785727858278592786027861278622786327864278652786627867278682786927870278712787227873278742787527876278772787827879278802788127882278832788427885278862788727888278892789027891278922789327894278952789627897278982789927900279012790227903279042790527906279072790827909279102791127912279132791427915279162791727918279192792027921279222792327924279252792627927279282792927930279312793227933279342793527936279372793827939279402794127942279432794427945279462794727948279492795027951279522795327954279552795627957279582795927960279612796227963279642796527966279672796827969279702797127972279732797427975279762797727978279792798027981279822798327984279852798627987279882798927990279912799227993279942799527996279972799827999280002800128002280032800428005280062800728008280092801028011280122801328014280152801628017280182801928020280212802228023280242802528026280272802828029280302803128032280332803428035280362803728038280392804028041280422804328044280452804628047280482804928050280512805228053280542805528056280572805828059280602806128062280632806428065280662806728068280692807028071280722807328074280752807628077280782807928080280812808228083280842808528086280872808828089280902809128092280932809428095280962809728098280992810028101281022810328104281052810628107281082810928110281112811228113281142811528116281172811828119281202812128122281232812428125281262812728128281292813028131281322813328134281352813628137281382813928140281412814228143281442814528146281472814828149281502815128152281532815428155281562815728158281592816028161281622816328164281652816628167281682816928170281712817228173281742817528176281772817828179281802818128182281832818428185281862818728188281892819028191281922819328194281952819628197281982819928200282012820228203282042820528206282072820828209282102821128212282132821428215282162821728218282192822028221282222822328224282252822628227282282822928230282312823228233282342823528236282372823828239282402824128242282432824428245282462824728248282492825028251282522825328254282552825628257282582825928260282612826228263282642826528266282672826828269282702827128272282732827428275282762827728278282792828028281282822828328284282852828628287282882828928290282912829228293282942829528296282972829828299283002830128302283032830428305283062830728308283092831028311283122831328314283152831628317283182831928320283212832228323283242832528326283272832828329283302833128332283332833428335283362833728338283392834028341283422834328344283452834628347283482834928350283512835228353283542835528356283572835828359283602836128362283632836428365283662836728368283692837028371283722837328374283752837628377283782837928380283812838228383283842838528386283872838828389283902839128392283932839428395283962839728398283992840028401284022840328404284052840628407284082840928410284112841228413284142841528416284172841828419284202842128422284232842428425284262842728428284292843028431284322843328434284352843628437284382843928440284412844228443284442844528446284472844828449284502845128452284532845428455284562845728458284592846028461284622846328464284652846628467284682846928470284712847228473284742847528476284772847828479284802848128482284832848428485284862848728488284892849028491284922849328494284952849628497284982849928500285012850228503285042850528506285072850828509285102851128512285132851428515285162851728518285192852028521285222852328524285252852628527285282852928530285312853228533285342853528536285372853828539285402854128542285432854428545285462854728548285492855028551285522855328554285552855628557285582855928560285612856228563285642856528566285672856828569285702857128572285732857428575285762857728578285792858028581285822858328584285852858628587285882858928590285912859228593285942859528596285972859828599286002860128602286032860428605286062860728608286092861028611286122861328614286152861628617286182861928620286212862228623286242862528626286272862828629286302863128632286332863428635286362863728638286392864028641286422864328644286452864628647286482864928650286512865228653286542865528656286572865828659286602866128662286632866428665286662866728668286692867028671286722867328674286752867628677286782867928680286812868228683286842868528686286872868828689286902869128692286932869428695286962869728698286992870028701287022870328704287052870628707287082870928710287112871228713287142871528716287172871828719287202872128722287232872428725287262872728728287292873028731287322873328734287352873628737287382873928740287412874228743287442874528746287472874828749287502875128752287532875428755287562875728758287592876028761287622876328764287652876628767287682876928770287712877228773287742877528776287772877828779287802878128782287832878428785287862878728788287892879028791287922879328794287952879628797287982879928800288012880228803288042880528806288072880828809288102881128812288132881428815288162881728818288192882028821288222882328824288252882628827288282882928830288312883228833288342883528836288372883828839288402884128842288432884428845288462884728848288492885028851288522885328854288552885628857288582885928860288612886228863288642886528866288672886828869288702887128872288732887428875288762887728878288792888028881288822888328884288852888628887288882888928890288912889228893288942889528896288972889828899289002890128902289032890428905289062890728908289092891028911289122891328914289152891628917289182891928920289212892228923289242892528926289272892828929289302893128932289332893428935289362893728938289392894028941289422894328944289452894628947289482894928950289512895228953289542895528956289572895828959289602896128962289632896428965289662896728968289692897028971289722897328974289752897628977289782897928980289812898228983289842898528986289872898828989289902899128992289932899428995289962899728998289992900029001290022900329004290052900629007290082900929010290112901229013290142901529016290172901829019290202902129022290232902429025290262902729028290292903029031290322903329034290352903629037290382903929040290412904229043290442904529046290472904829049290502905129052290532905429055290562905729058290592906029061290622906329064290652906629067290682906929070290712907229073290742907529076290772907829079290802908129082290832908429085290862908729088290892909029091290922909329094290952909629097290982909929100291012910229103291042910529106291072910829109291102911129112291132911429115291162911729118291192912029121291222912329124291252912629127291282912929130291312913229133291342913529136291372913829139291402914129142291432914429145291462914729148291492915029151291522915329154291552915629157291582915929160291612916229163291642916529166291672916829169291702917129172291732917429175291762917729178291792918029181291822918329184291852918629187291882918929190291912919229193291942919529196291972919829199292002920129202292032920429205292062920729208292092921029211292122921329214292152921629217292182921929220292212922229223292242922529226292272922829229292302923129232292332923429235292362923729238292392924029241292422924329244292452924629247292482924929250292512925229253292542925529256292572925829259292602926129262292632926429265292662926729268292692927029271292722927329274292752927629277292782927929280292812928229283292842928529286292872928829289292902929129292292932929429295292962929729298292992930029301293022930329304293052930629307293082930929310293112931229313293142931529316293172931829319293202932129322293232932429325293262932729328293292933029331293322933329334293352933629337293382933929340293412934229343293442934529346293472934829349293502935129352293532935429355293562935729358293592936029361293622936329364293652936629367293682936929370293712937229373293742937529376293772937829379293802938129382293832938429385293862938729388293892939029391293922939329394293952939629397293982939929400294012940229403294042940529406294072940829409294102941129412294132941429415294162941729418294192942029421294222942329424294252942629427294282942929430294312943229433294342943529436294372943829439294402944129442294432944429445294462944729448294492945029451294522945329454294552945629457294582945929460294612946229463294642946529466294672946829469294702947129472294732947429475294762947729478294792948029481294822948329484294852948629487294882948929490294912949229493294942949529496294972949829499295002950129502295032950429505295062950729508295092951029511295122951329514295152951629517295182951929520295212952229523295242952529526295272952829529295302953129532295332953429535295362953729538295392954029541295422954329544295452954629547295482954929550295512955229553295542955529556295572955829559295602956129562295632956429565295662956729568295692957029571295722957329574295752957629577295782957929580295812958229583295842958529586295872958829589295902959129592295932959429595295962959729598295992960029601296022960329604296052960629607296082960929610296112961229613296142961529616296172961829619296202962129622296232962429625296262962729628296292963029631296322963329634296352963629637296382963929640296412964229643296442964529646296472964829649296502965129652296532965429655296562965729658296592966029661296622966329664296652966629667296682966929670296712967229673296742967529676296772967829679296802968129682296832968429685296862968729688296892969029691296922969329694296952969629697296982969929700297012970229703297042970529706297072970829709297102971129712297132971429715297162971729718297192972029721297222972329724297252972629727297282972929730297312973229733297342973529736297372973829739297402974129742297432974429745297462974729748297492975029751297522975329754297552975629757297582975929760297612976229763297642976529766297672976829769297702977129772297732977429775297762977729778297792978029781297822978329784297852978629787297882978929790297912979229793297942979529796297972979829799298002980129802298032980429805298062980729808298092981029811298122981329814298152981629817298182981929820298212982229823298242982529826298272982829829298302983129832298332983429835298362983729838298392984029841298422984329844298452984629847298482984929850298512985229853298542985529856298572985829859298602986129862298632986429865298662986729868298692987029871298722987329874298752987629877298782987929880298812988229883298842988529886298872988829889298902989129892298932989429895298962989729898298992990029901299022990329904299052990629907299082990929910299112991229913299142991529916299172991829919299202992129922299232992429925299262992729928299292993029931299322993329934299352993629937299382993929940299412994229943299442994529946299472994829949299502995129952299532995429955299562995729958299592996029961299622996329964299652996629967299682996929970299712997229973299742997529976299772997829979299802998129982299832998429985299862998729988299892999029991299922999329994299952999629997299982999930000300013000230003300043000530006300073000830009300103001130012300133001430015300163001730018300193002030021300223002330024300253002630027300283002930030300313003230033300343003530036300373003830039300403004130042300433004430045300463004730048300493005030051300523005330054300553005630057300583005930060300613006230063300643006530066300673006830069300703007130072300733007430075300763007730078300793008030081300823008330084300853008630087300883008930090300913009230093300943009530096300973009830099301003010130102301033010430105301063010730108301093011030111301123011330114301153011630117301183011930120301213012230123301243012530126301273012830129301303013130132301333013430135301363013730138301393014030141301423014330144301453014630147301483014930150301513015230153301543015530156301573015830159301603016130162301633016430165301663016730168301693017030171301723017330174301753017630177301783017930180301813018230183301843018530186301873018830189301903019130192301933019430195301963019730198301993020030201302023020330204302053020630207302083020930210302113021230213302143021530216302173021830219302203022130222302233022430225302263022730228302293023030231302323023330234302353023630237302383023930240302413024230243302443024530246302473024830249302503025130252302533025430255302563025730258302593026030261302623026330264302653026630267302683026930270302713027230273302743027530276302773027830279302803028130282302833028430285302863028730288302893029030291302923029330294302953029630297302983029930300303013030230303303043030530306303073030830309303103031130312303133031430315303163031730318303193032030321303223032330324303253032630327303283032930330303313033230333303343033530336303373033830339303403034130342303433034430345303463034730348303493035030351303523035330354303553035630357303583035930360303613036230363303643036530366303673036830369303703037130372303733037430375303763037730378303793038030381303823038330384303853038630387303883038930390303913039230393303943039530396303973039830399304003040130402304033040430405304063040730408304093041030411304123041330414304153041630417304183041930420304213042230423304243042530426304273042830429304303043130432304333043430435304363043730438304393044030441304423044330444304453044630447304483044930450304513045230453304543045530456304573045830459304603046130462304633046430465304663046730468304693047030471304723047330474304753047630477304783047930480304813048230483304843048530486304873048830489304903049130492304933049430495304963049730498304993050030501305023050330504305053050630507305083050930510305113051230513305143051530516305173051830519305203052130522305233052430525305263052730528305293053030531305323053330534305353053630537305383053930540305413054230543305443054530546305473054830549305503055130552305533055430555305563055730558305593056030561305623056330564305653056630567305683056930570305713057230573305743057530576305773057830579305803058130582305833058430585305863058730588305893059030591305923059330594305953059630597305983059930600306013060230603306043060530606306073060830609306103061130612306133061430615306163061730618306193062030621306223062330624306253062630627306283062930630306313063230633306343063530636306373063830639306403064130642306433064430645306463064730648306493065030651306523065330654306553065630657306583065930660306613066230663306643066530666306673066830669306703067130672306733067430675306763067730678306793068030681306823068330684306853068630687306883068930690306913069230693306943069530696306973069830699307003070130702307033070430705307063070730708307093071030711307123071330714307153071630717307183071930720307213072230723307243072530726307273072830729307303073130732307333073430735307363073730738307393074030741307423074330744307453074630747307483074930750307513075230753307543075530756307573075830759307603076130762307633076430765307663076730768307693077030771307723077330774307753077630777307783077930780307813078230783307843078530786307873078830789307903079130792307933079430795307963079730798307993080030801308023080330804308053080630807308083080930810308113081230813308143081530816308173081830819308203082130822308233082430825308263082730828308293083030831308323083330834308353083630837308383083930840308413084230843308443084530846308473084830849308503085130852308533085430855308563085730858308593086030861308623086330864308653086630867308683086930870308713087230873308743087530876308773087830879308803088130882308833088430885308863088730888308893089030891308923089330894308953089630897308983089930900309013090230903309043090530906309073090830909309103091130912309133091430915309163091730918309193092030921309223092330924309253092630927309283092930930309313093230933309343093530936309373093830939309403094130942309433094430945309463094730948309493095030951309523095330954309553095630957309583095930960309613096230963309643096530966309673096830969309703097130972309733097430975309763097730978309793098030981309823098330984309853098630987309883098930990309913099230993309943099530996309973099830999310003100131002310033100431005310063100731008310093101031011310123101331014310153101631017310183101931020310213102231023310243102531026310273102831029310303103131032310333103431035310363103731038310393104031041310423104331044310453104631047310483104931050310513105231053310543105531056310573105831059310603106131062310633106431065310663106731068310693107031071310723107331074310753107631077310783107931080310813108231083310843108531086310873108831089310903109131092310933109431095310963109731098310993110031101311023110331104311053110631107311083110931110311113111231113311143111531116311173111831119311203112131122311233112431125311263112731128311293113031131311323113331134311353113631137311383113931140311413114231143311443114531146311473114831149311503115131152311533115431155311563115731158311593116031161311623116331164311653116631167311683116931170311713117231173311743117531176311773117831179311803118131182311833118431185311863118731188311893119031191311923119331194311953119631197311983119931200312013120231203312043120531206312073120831209312103121131212312133121431215312163121731218312193122031221312223122331224312253122631227312283122931230312313123231233312343123531236312373123831239312403124131242312433124431245312463124731248312493125031251312523125331254312553125631257312583125931260312613126231263312643126531266312673126831269312703127131272312733127431275312763127731278312793128031281312823128331284312853128631287312883128931290312913129231293312943129531296312973129831299313003130131302313033130431305313063130731308313093131031311313123131331314313153131631317313183131931320313213132231323313243132531326313273132831329313303133131332313333133431335313363133731338313393134031341313423134331344313453134631347313483134931350313513135231353313543135531356313573135831359313603136131362313633136431365313663136731368313693137031371313723137331374313753137631377313783137931380313813138231383313843138531386313873138831389313903139131392313933139431395313963139731398313993140031401314023140331404314053140631407314083140931410314113141231413314143141531416314173141831419314203142131422314233142431425314263142731428314293143031431314323143331434314353143631437314383143931440314413144231443314443144531446314473144831449314503145131452314533145431455314563145731458314593146031461314623146331464314653146631467314683146931470314713147231473314743147531476314773147831479314803148131482314833148431485314863148731488314893149031491314923149331494314953149631497314983149931500315013150231503315043150531506315073150831509315103151131512315133151431515315163151731518315193152031521315223152331524315253152631527315283152931530315313153231533315343153531536315373153831539315403154131542315433154431545315463154731548315493155031551315523155331554315553155631557315583155931560315613156231563315643156531566315673156831569315703157131572315733157431575315763157731578315793158031581315823158331584315853158631587315883158931590315913159231593315943159531596315973159831599316003160131602316033160431605316063160731608316093161031611316123161331614316153161631617316183161931620316213162231623316243162531626316273162831629316303163131632316333163431635316363163731638316393164031641316423164331644316453164631647316483164931650316513165231653316543165531656316573165831659316603166131662316633166431665316663166731668316693167031671316723167331674316753167631677316783167931680316813168231683316843168531686316873168831689316903169131692316933169431695316963169731698316993170031701317023170331704317053170631707317083170931710317113171231713317143171531716317173171831719317203172131722317233172431725317263172731728317293173031731317323173331734317353173631737317383173931740317413174231743317443174531746317473174831749317503175131752317533175431755317563175731758317593176031761317623176331764317653176631767317683176931770317713177231773317743177531776317773177831779317803178131782317833178431785317863178731788317893179031791317923179331794317953179631797317983179931800318013180231803318043180531806318073180831809318103181131812318133181431815318163181731818318193182031821318223182331824318253182631827318283182931830318313183231833318343183531836318373183831839318403184131842318433184431845318463184731848318493185031851318523185331854318553185631857318583185931860318613186231863318643186531866318673186831869318703187131872318733187431875318763187731878318793188031881318823188331884318853188631887318883188931890318913189231893318943189531896318973189831899319003190131902319033190431905319063190731908319093191031911319123191331914319153191631917319183191931920319213192231923319243192531926319273192831929319303193131932319333193431935319363193731938319393194031941319423194331944319453194631947319483194931950319513195231953319543195531956319573195831959319603196131962319633196431965319663196731968319693197031971319723197331974319753197631977319783197931980319813198231983319843198531986319873198831989319903199131992319933199431995319963199731998319993200032001320023200332004320053200632007320083200932010320113201232013320143201532016320173201832019320203202132022320233202432025320263202732028320293203032031320323203332034320353203632037320383203932040320413204232043320443204532046320473204832049320503205132052320533205432055320563205732058320593206032061320623206332064320653206632067320683206932070320713207232073320743207532076320773207832079320803208132082320833208432085320863208732088320893209032091320923209332094320953209632097320983209932100321013210232103321043210532106321073210832109321103211132112321133211432115321163211732118321193212032121321223212332124321253212632127321283212932130321313213232133321343213532136321373213832139321403214132142321433214432145321463214732148321493215032151321523215332154321553215632157321583215932160321613216232163321643216532166321673216832169321703217132172321733217432175321763217732178321793218032181321823218332184321853218632187321883218932190321913219232193321943219532196321973219832199322003220132202322033220432205322063220732208322093221032211322123221332214322153221632217322183221932220322213222232223322243222532226322273222832229322303223132232322333223432235322363223732238322393224032241322423224332244322453224632247322483224932250322513225232253322543225532256322573225832259322603226132262322633226432265322663226732268322693227032271322723227332274322753227632277322783227932280322813228232283322843228532286322873228832289322903229132292322933229432295322963229732298322993230032301323023230332304323053230632307323083230932310323113231232313323143231532316323173231832319323203232132322323233232432325323263232732328323293233032331323323233332334323353233632337323383233932340323413234232343323443234532346323473234832349323503235132352323533235432355323563235732358323593236032361323623236332364323653236632367323683236932370323713237232373323743237532376323773237832379323803238132382323833238432385323863238732388323893239032391323923239332394323953239632397323983239932400324013240232403324043240532406324073240832409324103241132412324133241432415324163241732418324193242032421324223242332424324253242632427324283242932430324313243232433324343243532436324373243832439324403244132442324433244432445324463244732448324493245032451324523245332454324553245632457324583245932460324613246232463324643246532466324673246832469324703247132472324733247432475324763247732478324793248032481324823248332484324853248632487324883248932490324913249232493324943249532496324973249832499325003250132502325033250432505325063250732508325093251032511325123251332514325153251632517325183251932520325213252232523325243252532526325273252832529325303253132532325333253432535325363253732538325393254032541325423254332544325453254632547325483254932550325513255232553325543255532556325573255832559325603256132562325633256432565325663256732568325693257032571325723257332574325753257632577325783257932580325813258232583325843258532586325873258832589325903259132592325933259432595325963259732598325993260032601326023260332604326053260632607326083260932610326113261232613326143261532616326173261832619326203262132622326233262432625326263262732628326293263032631326323263332634326353263632637326383263932640326413264232643326443264532646326473264832649326503265132652326533265432655326563265732658326593266032661326623266332664326653266632667326683266932670326713267232673326743267532676326773267832679326803268132682326833268432685326863268732688326893269032691326923269332694326953269632697326983269932700327013270232703327043270532706327073270832709327103271132712327133271432715327163271732718327193272032721327223272332724327253272632727327283272932730327313273232733327343273532736327373273832739327403274132742327433274432745327463274732748327493275032751327523275332754327553275632757327583275932760327613276232763327643276532766327673276832769327703277132772327733277432775327763277732778327793278032781327823278332784327853278632787327883278932790327913279232793327943279532796327973279832799328003280132802328033280432805328063280732808328093281032811328123281332814328153281632817328183281932820328213282232823328243282532826328273282832829328303283132832328333283432835328363283732838328393284032841328423284332844328453284632847328483284932850328513285232853328543285532856328573285832859328603286132862328633286432865328663286732868328693287032871328723287332874328753287632877328783287932880328813288232883328843288532886328873288832889328903289132892328933289432895328963289732898328993290032901329023290332904329053290632907329083290932910329113291232913329143291532916329173291832919329203292132922329233292432925329263292732928329293293032931329323293332934329353293632937329383293932940329413294232943329443294532946329473294832949329503295132952329533295432955329563295732958329593296032961329623296332964329653296632967329683296932970329713297232973329743297532976329773297832979329803298132982329833298432985329863298732988329893299032991329923299332994329953299632997329983299933000330013300233003330043300533006330073300833009330103301133012330133301433015330163301733018330193302033021330223302333024330253302633027330283302933030330313303233033330343303533036330373303833039330403304133042330433304433045330463304733048330493305033051330523305333054330553305633057330583305933060330613306233063330643306533066330673306833069330703307133072330733307433075330763307733078330793308033081330823308333084330853308633087330883308933090330913309233093330943309533096330973309833099331003310133102331033310433105331063310733108331093311033111331123311333114331153311633117331183311933120331213312233123331243312533126331273312833129331303313133132331333313433135331363313733138331393314033141331423314333144331453314633147331483314933150331513315233153331543315533156331573315833159331603316133162331633316433165331663316733168331693317033171331723317333174331753317633177331783317933180331813318233183331843318533186331873318833189331903319133192331933319433195331963319733198331993320033201332023320333204332053320633207332083320933210332113321233213332143321533216332173321833219332203322133222332233322433225332263322733228332293323033231332323323333234332353323633237332383323933240332413324233243332443324533246332473324833249332503325133252332533325433255332563325733258332593326033261332623326333264332653326633267332683326933270332713327233273332743327533276332773327833279332803328133282332833328433285332863328733288332893329033291332923329333294332953329633297332983329933300333013330233303333043330533306333073330833309333103331133312333133331433315333163331733318333193332033321333223332333324333253332633327333283332933330333313333233333333343333533336333373333833339333403334133342333433334433345333463334733348333493335033351333523335333354333553335633357333583335933360333613336233363333643336533366333673336833369333703337133372333733337433375333763337733378333793338033381333823338333384333853338633387333883338933390333913339233393333943339533396333973339833399334003340133402334033340433405334063340733408334093341033411334123341333414334153341633417334183341933420334213342233423334243342533426334273342833429334303343133432334333343433435334363343733438334393344033441334423344333444334453344633447334483344933450334513345233453334543345533456334573345833459334603346133462334633346433465334663346733468334693347033471334723347333474334753347633477334783347933480334813348233483334843348533486334873348833489334903349133492334933349433495334963349733498334993350033501335023350333504335053350633507335083350933510335113351233513335143351533516335173351833519335203352133522335233352433525335263352733528335293353033531335323353333534335353353633537335383353933540335413354233543335443354533546335473354833549335503355133552335533355433555335563355733558335593356033561335623356333564335653356633567335683356933570335713357233573335743357533576335773357833579335803358133582335833358433585335863358733588335893359033591335923359333594335953359633597335983359933600336013360233603336043360533606336073360833609336103361133612336133361433615336163361733618336193362033621336223362333624336253362633627336283362933630336313363233633336343363533636336373363833639336403364133642336433364433645336463364733648336493365033651336523365333654336553365633657336583365933660336613366233663336643366533666336673366833669336703367133672336733367433675336763367733678336793368033681336823368333684336853368633687336883368933690336913369233693336943369533696336973369833699337003370133702337033370433705337063370733708337093371033711337123371333714337153371633717337183371933720337213372233723337243372533726337273372833729337303373133732337333373433735337363373733738337393374033741337423374333744337453374633747337483374933750337513375233753337543375533756337573375833759337603376133762337633376433765337663376733768337693377033771337723377333774337753377633777337783377933780337813378233783337843378533786337873378833789337903379133792337933379433795337963379733798337993380033801338023380333804338053380633807338083380933810338113381233813338143381533816338173381833819338203382133822338233382433825338263382733828338293383033831338323383333834338353383633837338383383933840338413384233843338443384533846338473384833849338503385133852338533385433855338563385733858338593386033861338623386333864338653386633867338683386933870338713387233873338743387533876338773387833879338803388133882338833388433885338863388733888338893389033891338923389333894338953389633897338983389933900339013390233903339043390533906339073390833909339103391133912339133391433915339163391733918339193392033921339223392333924339253392633927339283392933930339313393233933339343393533936339373393833939339403394133942339433394433945339463394733948339493395033951339523395333954339553395633957339583395933960339613396233963339643396533966339673396833969339703397133972339733397433975339763397733978339793398033981339823398333984339853398633987339883398933990339913399233993339943399533996339973399833999340003400134002340033400434005340063400734008340093401034011340123401334014340153401634017340183401934020340213402234023340243402534026340273402834029340303403134032340333403434035340363403734038340393404034041340423404334044340453404634047340483404934050340513405234053340543405534056340573405834059340603406134062340633406434065340663406734068340693407034071340723407334074340753407634077340783407934080340813408234083340843408534086340873408834089340903409134092340933409434095340963409734098340993410034101341023410334104341053410634107341083410934110341113411234113341143411534116341173411834119341203412134122341233412434125341263412734128341293413034131341323413334134341353413634137341383413934140341413414234143341443414534146341473414834149341503415134152341533415434155341563415734158341593416034161341623416334164341653416634167341683416934170341713417234173341743417534176341773417834179341803418134182341833418434185341863418734188341893419034191341923419334194341953419634197341983419934200342013420234203342043420534206342073420834209342103421134212342133421434215342163421734218342193422034221342223422334224342253422634227342283422934230342313423234233342343423534236342373423834239342403424134242342433424434245342463424734248342493425034251342523425334254342553425634257342583425934260342613426234263342643426534266342673426834269342703427134272342733427434275342763427734278342793428034281342823428334284342853428634287342883428934290342913429234293342943429534296342973429834299343003430134302343033430434305343063430734308343093431034311343123431334314343153431634317343183431934320343213432234323343243432534326343273432834329343303433134332343333433434335343363433734338343393434034341343423434334344343453434634347343483434934350343513435234353343543435534356343573435834359343603436134362343633436434365343663436734368343693437034371343723437334374343753437634377343783437934380343813438234383343843438534386343873438834389343903439134392343933439434395343963439734398343993440034401344023440334404344053440634407344083440934410344113441234413344143441534416344173441834419344203442134422344233442434425344263442734428344293443034431344323443334434344353443634437344383443934440344413444234443344443444534446344473444834449344503445134452344533445434455344563445734458344593446034461344623446334464344653446634467344683446934470344713447234473344743447534476344773447834479344803448134482344833448434485344863448734488344893449034491344923449334494344953449634497344983449934500345013450234503345043450534506345073450834509345103451134512345133451434515345163451734518345193452034521345223452334524345253452634527345283452934530345313453234533345343453534536345373453834539345403454134542345433454434545345463454734548345493455034551345523455334554345553455634557345583455934560345613456234563345643456534566345673456834569345703457134572345733457434575345763457734578345793458034581345823458334584345853458634587345883458934590345913459234593345943459534596345973459834599346003460134602346033460434605346063460734608346093461034611346123461334614346153461634617346183461934620346213462234623346243462534626346273462834629346303463134632346333463434635346363463734638346393464034641346423464334644346453464634647346483464934650346513465234653346543465534656346573465834659346603466134662346633466434665346663466734668346693467034671346723467334674346753467634677346783467934680346813468234683346843468534686346873468834689346903469134692346933469434695346963469734698346993470034701347023470334704347053470634707347083470934710347113471234713347143471534716347173471834719347203472134722347233472434725347263472734728347293473034731347323473334734347353473634737347383473934740347413474234743347443474534746347473474834749347503475134752347533475434755347563475734758347593476034761347623476334764347653476634767347683476934770347713477234773347743477534776347773477834779347803478134782347833478434785347863478734788347893479034791347923479334794347953479634797347983479934800348013480234803348043480534806348073480834809348103481134812348133481434815348163481734818348193482034821348223482334824348253482634827348283482934830348313483234833348343483534836348373483834839348403484134842348433484434845348463484734848348493485034851348523485334854348553485634857348583485934860348613486234863348643486534866348673486834869348703487134872348733487434875348763487734878348793488034881348823488334884348853488634887348883488934890348913489234893348943489534896348973489834899349003490134902349033490434905349063490734908349093491034911349123491334914349153491634917349183491934920349213492234923349243492534926349273492834929349303493134932349333493434935349363493734938349393494034941349423494334944349453494634947349483494934950349513495234953349543495534956349573495834959349603496134962349633496434965349663496734968349693497034971349723497334974349753497634977349783497934980349813498234983349843498534986349873498834989349903499134992349933499434995349963499734998349993500035001350023500335004350053500635007350083500935010350113501235013350143501535016350173501835019350203502135022350233502435025350263502735028350293503035031350323503335034350353503635037350383503935040350413504235043350443504535046350473504835049350503505135052350533505435055350563505735058350593506035061350623506335064350653506635067350683506935070350713507235073350743507535076350773507835079350803508135082350833508435085350863508735088350893509035091350923509335094350953509635097350983509935100351013510235103351043510535106351073510835109351103511135112351133511435115351163511735118351193512035121351223512335124351253512635127351283512935130351313513235133351343513535136351373513835139351403514135142351433514435145351463514735148351493515035151351523515335154351553515635157351583515935160351613516235163351643516535166351673516835169351703517135172351733517435175351763517735178351793518035181351823518335184351853518635187351883518935190351913519235193351943519535196351973519835199352003520135202352033520435205352063520735208352093521035211352123521335214352153521635217352183521935220352213522235223352243522535226352273522835229352303523135232352333523435235352363523735238352393524035241352423524335244352453524635247352483524935250352513525235253352543525535256352573525835259352603526135262352633526435265352663526735268352693527035271352723527335274352753527635277352783527935280352813528235283352843528535286352873528835289352903529135292352933529435295352963529735298352993530035301353023530335304353053530635307353083530935310353113531235313353143531535316353173531835319353203532135322353233532435325353263532735328353293533035331353323533335334353353533635337353383533935340353413534235343353443534535346353473534835349353503535135352353533535435355353563535735358353593536035361353623536335364353653536635367353683536935370353713537235373353743537535376353773537835379353803538135382353833538435385353863538735388353893539035391353923539335394353953539635397353983539935400354013540235403354043540535406354073540835409354103541135412354133541435415354163541735418354193542035421354223542335424354253542635427354283542935430354313543235433354343543535436354373543835439354403544135442354433544435445354463544735448354493545035451354523545335454354553545635457354583545935460354613546235463354643546535466354673546835469354703547135472354733547435475354763547735478354793548035481354823548335484354853548635487354883548935490354913549235493354943549535496354973549835499355003550135502355033550435505355063550735508355093551035511355123551335514355153551635517355183551935520355213552235523355243552535526355273552835529355303553135532355333553435535355363553735538355393554035541355423554335544355453554635547355483554935550355513555235553355543555535556355573555835559355603556135562355633556435565355663556735568355693557035571355723557335574355753557635577355783557935580355813558235583355843558535586355873558835589355903559135592355933559435595355963559735598355993560035601356023560335604356053560635607356083560935610356113561235613356143561535616356173561835619356203562135622356233562435625356263562735628356293563035631356323563335634356353563635637356383563935640356413564235643356443564535646356473564835649356503565135652356533565435655356563565735658356593566035661356623566335664356653566635667356683566935670356713567235673356743567535676356773567835679356803568135682356833568435685356863568735688356893569035691356923569335694356953569635697356983569935700357013570235703357043570535706357073570835709357103571135712357133571435715357163571735718357193572035721357223572335724357253572635727357283572935730357313573235733357343573535736357373573835739357403574135742357433574435745357463574735748357493575035751357523575335754357553575635757357583575935760357613576235763357643576535766357673576835769357703577135772357733577435775357763577735778357793578035781357823578335784357853578635787357883578935790357913579235793357943579535796357973579835799358003580135802358033580435805358063580735808358093581035811358123581335814358153581635817358183581935820358213582235823358243582535826358273582835829358303583135832358333583435835358363583735838358393584035841358423584335844358453584635847358483584935850358513585235853358543585535856358573585835859358603586135862358633586435865358663586735868358693587035871358723587335874358753587635877358783587935880358813588235883358843588535886358873588835889358903589135892358933589435895358963589735898358993590035901359023590335904359053590635907359083590935910359113591235913359143591535916359173591835919359203592135922359233592435925359263592735928359293593035931359323593335934359353593635937359383593935940359413594235943359443594535946359473594835949359503595135952359533595435955359563595735958359593596035961359623596335964359653596635967359683596935970359713597235973359743597535976359773597835979359803598135982359833598435985359863598735988359893599035991359923599335994359953599635997359983599936000360013600236003360043600536006360073600836009360103601136012360133601436015360163601736018360193602036021360223602336024360253602636027360283602936030360313603236033360343603536036360373603836039360403604136042360433604436045360463604736048360493605036051360523605336054360553605636057360583605936060360613606236063360643606536066360673606836069360703607136072360733607436075360763607736078360793608036081360823608336084360853608636087360883608936090360913609236093360943609536096360973609836099361003610136102361033610436105361063610736108361093611036111361123611336114361153611636117361183611936120361213612236123361243612536126361273612836129361303613136132361333613436135361363613736138361393614036141361423614336144361453614636147361483614936150361513615236153361543615536156361573615836159361603616136162361633616436165361663616736168361693617036171361723617336174361753617636177361783617936180361813618236183361843618536186361873618836189361903619136192361933619436195361963619736198361993620036201362023620336204362053620636207362083620936210362113621236213362143621536216362173621836219362203622136222362233622436225362263622736228362293623036231362323623336234362353623636237362383623936240362413624236243362443624536246362473624836249362503625136252362533625436255362563625736258362593626036261362623626336264362653626636267362683626936270362713627236273362743627536276362773627836279362803628136282362833628436285362863628736288362893629036291362923629336294362953629636297362983629936300363013630236303363043630536306363073630836309363103631136312363133631436315363163631736318363193632036321363223632336324363253632636327363283632936330363313633236333363343633536336363373633836339363403634136342363433634436345363463634736348363493635036351363523635336354363553635636357363583635936360363613636236363363643636536366363673636836369363703637136372363733637436375363763637736378363793638036381363823638336384363853638636387363883638936390363913639236393363943639536396363973639836399364003640136402364033640436405364063640736408364093641036411364123641336414364153641636417364183641936420364213642236423364243642536426364273642836429364303643136432364333643436435364363643736438364393644036441364423644336444364453644636447364483644936450364513645236453364543645536456364573645836459364603646136462364633646436465364663646736468364693647036471364723647336474364753647636477364783647936480364813648236483364843648536486364873648836489364903649136492364933649436495364963649736498364993650036501365023650336504365053650636507365083650936510365113651236513365143651536516365173651836519365203652136522365233652436525365263652736528365293653036531365323653336534365353653636537365383653936540365413654236543365443654536546365473654836549365503655136552365533655436555365563655736558365593656036561365623656336564365653656636567365683656936570365713657236573365743657536576365773657836579365803658136582365833658436585365863658736588365893659036591365923659336594365953659636597365983659936600366013660236603366043660536606366073660836609366103661136612366133661436615366163661736618366193662036621366223662336624366253662636627366283662936630366313663236633366343663536636366373663836639366403664136642366433664436645366463664736648366493665036651366523665336654366553665636657366583665936660366613666236663366643666536666366673666836669366703667136672366733667436675366763667736678366793668036681366823668336684366853668636687366883668936690366913669236693366943669536696366973669836699367003670136702367033670436705367063670736708367093671036711367123671336714367153671636717367183671936720367213672236723367243672536726367273672836729367303673136732367333673436735367363673736738367393674036741367423674336744367453674636747367483674936750367513675236753367543675536756367573675836759367603676136762367633676436765367663676736768367693677036771367723677336774367753677636777367783677936780367813678236783367843678536786367873678836789367903679136792367933679436795367963679736798367993680036801368023680336804368053680636807368083680936810368113681236813368143681536816368173681836819368203682136822368233682436825368263682736828368293683036831368323683336834368353683636837368383683936840368413684236843368443684536846368473684836849368503685136852368533685436855368563685736858368593686036861368623686336864368653686636867368683686936870368713687236873368743687536876368773687836879368803688136882368833688436885368863688736888368893689036891368923689336894368953689636897368983689936900369013690236903369043690536906369073690836909369103691136912369133691436915369163691736918369193692036921369223692336924369253692636927369283692936930369313693236933369343693536936369373693836939369403694136942369433694436945369463694736948369493695036951369523695336954369553695636957369583695936960369613696236963369643696536966369673696836969369703697136972369733697436975369763697736978369793698036981369823698336984369853698636987369883698936990369913699236993369943699536996369973699836999370003700137002370033700437005370063700737008370093701037011370123701337014370153701637017370183701937020370213702237023370243702537026370273702837029370303703137032370333703437035370363703737038370393704037041370423704337044370453704637047370483704937050370513705237053370543705537056370573705837059370603706137062370633706437065370663706737068370693707037071370723707337074370753707637077370783707937080370813708237083370843708537086370873708837089370903709137092370933709437095370963709737098370993710037101371023710337104371053710637107371083710937110371113711237113371143711537116371173711837119371203712137122371233712437125371263712737128371293713037131371323713337134371353713637137371383713937140371413714237143371443714537146371473714837149371503715137152371533715437155371563715737158371593716037161371623716337164371653716637167371683716937170371713717237173371743717537176371773717837179371803718137182371833718437185371863718737188371893719037191371923719337194371953719637197371983719937200372013720237203372043720537206372073720837209372103721137212372133721437215372163721737218372193722037221372223722337224372253722637227372283722937230372313723237233372343723537236372373723837239372403724137242372433724437245372463724737248372493725037251372523725337254372553725637257372583725937260372613726237263372643726537266372673726837269372703727137272372733727437275372763727737278372793728037281372823728337284372853728637287372883728937290372913729237293372943729537296372973729837299373003730137302373033730437305373063730737308373093731037311373123731337314373153731637317373183731937320373213732237323373243732537326373273732837329373303733137332373333733437335373363733737338373393734037341373423734337344373453734637347373483734937350373513735237353373543735537356373573735837359373603736137362373633736437365373663736737368373693737037371373723737337374373753737637377373783737937380373813738237383373843738537386373873738837389373903739137392373933739437395373963739737398373993740037401374023740337404374053740637407374083740937410374113741237413374143741537416374173741837419374203742137422374233742437425374263742737428374293743037431374323743337434374353743637437374383743937440374413744237443374443744537446374473744837449374503745137452374533745437455374563745737458374593746037461374623746337464374653746637467374683746937470374713747237473374743747537476374773747837479374803748137482374833748437485374863748737488374893749037491374923749337494374953749637497374983749937500375013750237503375043750537506375073750837509375103751137512375133751437515375163751737518375193752037521375223752337524375253752637527375283752937530375313753237533375343753537536375373753837539375403754137542375433754437545375463754737548375493755037551375523755337554375553755637557375583755937560375613756237563375643756537566375673756837569375703757137572375733757437575375763757737578375793758037581375823758337584375853758637587375883758937590375913759237593375943759537596375973759837599376003760137602376033760437605376063760737608376093761037611376123761337614376153761637617376183761937620376213762237623376243762537626376273762837629376303763137632376333763437635376363763737638376393764037641376423764337644376453764637647376483764937650376513765237653376543765537656376573765837659376603766137662376633766437665376663766737668376693767037671376723767337674376753767637677376783767937680376813768237683376843768537686376873768837689376903769137692376933769437695376963769737698376993770037701377023770337704377053770637707377083770937710377113771237713377143771537716377173771837719377203772137722377233772437725377263772737728377293773037731377323773337734377353773637737377383773937740377413774237743377443774537746377473774837749377503775137752377533775437755377563775737758377593776037761377623776337764377653776637767377683776937770377713777237773377743777537776377773777837779377803778137782377833778437785377863778737788377893779037791377923779337794377953779637797377983779937800378013780237803378043780537806378073780837809378103781137812378133781437815378163781737818378193782037821378223782337824378253782637827378283782937830378313783237833378343783537836378373783837839378403784137842378433784437845378463784737848378493785037851378523785337854378553785637857378583785937860378613786237863378643786537866378673786837869378703787137872378733787437875378763787737878378793788037881378823788337884378853788637887378883788937890378913789237893378943789537896378973789837899379003790137902379033790437905379063790737908379093791037911379123791337914379153791637917379183791937920379213792237923379243792537926379273792837929379303793137932379333793437935379363793737938379393794037941379423794337944379453794637947379483794937950379513795237953379543795537956379573795837959379603796137962379633796437965379663796737968379693797037971379723797337974379753797637977379783797937980379813798237983379843798537986379873798837989379903799137992379933799437995379963799737998379993800038001380023800338004380053800638007380083800938010380113801238013380143801538016380173801838019380203802138022380233802438025380263802738028380293803038031380323803338034380353803638037380383803938040380413804238043380443804538046380473804838049380503805138052380533805438055380563805738058380593806038061380623806338064380653806638067380683806938070380713807238073380743807538076380773807838079380803808138082380833808438085380863808738088380893809038091380923809338094380953809638097380983809938100381013810238103381043810538106381073810838109381103811138112381133811438115381163811738118381193812038121381223812338124381253812638127381283812938130381313813238133381343813538136381373813838139381403814138142381433814438145381463814738148381493815038151381523815338154381553815638157381583815938160381613816238163381643816538166381673816838169381703817138172381733817438175381763817738178381793818038181381823818338184381853818638187381883818938190381913819238193381943819538196381973819838199382003820138202382033820438205382063820738208382093821038211382123821338214382153821638217382183821938220382213822238223382243822538226382273822838229382303823138232382333823438235382363823738238382393824038241382423824338244382453824638247382483824938250382513825238253382543825538256382573825838259382603826138262382633826438265382663826738268382693827038271382723827338274382753827638277382783827938280382813828238283382843828538286382873828838289382903829138292382933829438295382963829738298382993830038301383023830338304383053830638307383083830938310383113831238313383143831538316383173831838319383203832138322383233832438325383263832738328383293833038331383323833338334383353833638337383383833938340383413834238343383443834538346383473834838349383503835138352383533835438355383563835738358383593836038361383623836338364383653836638367383683836938370383713837238373383743837538376383773837838379383803838138382383833838438385383863838738388383893839038391383923839338394383953839638397383983839938400384013840238403384043840538406384073840838409384103841138412384133841438415384163841738418384193842038421384223842338424384253842638427384283842938430384313843238433384343843538436384373843838439384403844138442384433844438445384463844738448384493845038451384523845338454384553845638457384583845938460384613846238463384643846538466384673846838469384703847138472384733847438475384763847738478384793848038481384823848338484384853848638487384883848938490384913849238493384943849538496384973849838499385003850138502385033850438505385063850738508385093851038511385123851338514385153851638517385183851938520385213852238523385243852538526385273852838529385303853138532385333853438535385363853738538385393854038541385423854338544385453854638547385483854938550385513855238553385543855538556385573855838559385603856138562385633856438565385663856738568385693857038571385723857338574385753857638577385783857938580385813858238583385843858538586385873858838589385903859138592385933859438595385963859738598385993860038601386023860338604386053860638607386083860938610386113861238613386143861538616386173861838619386203862138622386233862438625386263862738628386293863038631386323863338634386353863638637386383863938640386413864238643386443864538646386473864838649386503865138652386533865438655386563865738658386593866038661386623866338664386653866638667386683866938670386713867238673386743867538676386773867838679386803868138682386833868438685386863868738688386893869038691386923869338694386953869638697386983869938700387013870238703387043870538706387073870838709387103871138712387133871438715387163871738718387193872038721387223872338724387253872638727387283872938730387313873238733387343873538736387373873838739387403874138742387433874438745387463874738748387493875038751387523875338754387553875638757387583875938760387613876238763387643876538766387673876838769387703877138772387733877438775387763877738778387793878038781387823878338784387853878638787387883878938790387913879238793387943879538796387973879838799388003880138802388033880438805388063880738808388093881038811388123881338814388153881638817388183881938820388213882238823388243882538826388273882838829388303883138832388333883438835388363883738838388393884038841388423884338844388453884638847388483884938850388513885238853388543885538856388573885838859388603886138862388633886438865388663886738868388693887038871388723887338874388753887638877388783887938880388813888238883388843888538886388873888838889388903889138892388933889438895388963889738898388993890038901389023890338904389053890638907389083890938910389113891238913389143891538916389173891838919389203892138922389233892438925389263892738928389293893038931389323893338934389353893638937389383893938940389413894238943389443894538946389473894838949389503895138952389533895438955389563895738958389593896038961389623896338964389653896638967389683896938970389713897238973389743897538976389773897838979389803898138982389833898438985389863898738988389893899038991389923899338994389953899638997389983899939000390013900239003390043900539006390073900839009390103901139012390133901439015390163901739018390193902039021390223902339024390253902639027390283902939030390313903239033390343903539036390373903839039390403904139042390433904439045390463904739048390493905039051390523905339054390553905639057390583905939060390613906239063390643906539066390673906839069390703907139072390733907439075390763907739078390793908039081390823908339084390853908639087390883908939090390913909239093390943909539096390973909839099391003910139102391033910439105391063910739108391093911039111391123911339114391153911639117391183911939120391213912239123391243912539126391273912839129391303913139132391333913439135391363913739138391393914039141391423914339144391453914639147391483914939150391513915239153391543915539156391573915839159391603916139162391633916439165391663916739168391693917039171391723917339174391753917639177391783917939180391813918239183391843918539186391873918839189391903919139192391933919439195391963919739198391993920039201392023920339204392053920639207392083920939210392113921239213392143921539216392173921839219392203922139222392233922439225392263922739228392293923039231392323923339234392353923639237392383923939240392413924239243392443924539246392473924839249392503925139252392533925439255392563925739258392593926039261392623926339264392653926639267392683926939270392713927239273392743927539276392773927839279392803928139282392833928439285392863928739288392893929039291392923929339294392953929639297392983929939300393013930239303393043930539306393073930839309393103931139312393133931439315393163931739318393193932039321393223932339324393253932639327393283932939330393313933239333393343933539336393373933839339393403934139342393433934439345393463934739348393493935039351393523935339354393553935639357393583935939360393613936239363393643936539366393673936839369393703937139372393733937439375393763937739378393793938039381393823938339384393853938639387393883938939390393913939239393393943939539396393973939839399394003940139402394033940439405394063940739408394093941039411394123941339414394153941639417394183941939420394213942239423394243942539426394273942839429394303943139432394333943439435394363943739438394393944039441394423944339444394453944639447394483944939450394513945239453394543945539456394573945839459394603946139462394633946439465394663946739468394693947039471394723947339474394753947639477394783947939480394813948239483394843948539486394873948839489394903949139492394933949439495394963949739498394993950039501395023950339504395053950639507395083950939510395113951239513395143951539516395173951839519395203952139522395233952439525395263952739528395293953039531395323953339534395353953639537395383953939540395413954239543395443954539546395473954839549395503955139552395533955439555395563955739558395593956039561395623956339564395653956639567395683956939570395713957239573395743957539576395773957839579395803958139582395833958439585395863958739588395893959039591395923959339594395953959639597395983959939600396013960239603396043960539606396073960839609396103961139612396133961439615396163961739618396193962039621396223962339624396253962639627396283962939630396313963239633396343963539636396373963839639396403964139642396433964439645396463964739648396493965039651396523965339654396553965639657396583965939660396613966239663396643966539666396673966839669396703967139672396733967439675396763967739678396793968039681396823968339684396853968639687396883968939690396913969239693396943969539696396973969839699397003970139702397033970439705397063970739708397093971039711397123971339714397153971639717397183971939720397213972239723397243972539726397273972839729397303973139732397333973439735397363973739738397393974039741397423974339744397453974639747397483974939750397513975239753397543975539756397573975839759397603976139762397633976439765397663976739768397693977039771397723977339774397753977639777397783977939780397813978239783397843978539786397873978839789397903979139792397933979439795397963979739798397993980039801398023980339804398053980639807398083980939810398113981239813398143981539816398173981839819398203982139822398233982439825398263982739828398293983039831398323983339834398353983639837398383983939840398413984239843398443984539846398473984839849398503985139852398533985439855398563985739858398593986039861398623986339864398653986639867398683986939870398713987239873398743987539876398773987839879398803988139882398833988439885398863988739888398893989039891398923989339894398953989639897398983989939900399013990239903399043990539906399073990839909399103991139912399133991439915399163991739918399193992039921399223992339924399253992639927399283992939930399313993239933399343993539936399373993839939399403994139942399433994439945399463994739948399493995039951399523995339954399553995639957399583995939960399613996239963399643996539966399673996839969399703997139972399733997439975399763997739978399793998039981399823998339984399853998639987399883998939990399913999239993399943999539996399973999839999400004000140002400034000440005400064000740008400094001040011400124001340014400154001640017400184001940020400214002240023400244002540026400274002840029400304003140032400334003440035400364003740038400394004040041400424004340044400454004640047400484004940050400514005240053400544005540056400574005840059400604006140062400634006440065400664006740068400694007040071400724007340074400754007640077400784007940080400814008240083400844008540086400874008840089400904009140092400934009440095400964009740098400994010040101401024010340104401054010640107401084010940110401114011240113401144011540116401174011840119401204012140122401234012440125401264012740128401294013040131401324013340134401354013640137401384013940140401414014240143401444014540146401474014840149401504015140152401534015440155401564015740158401594016040161401624016340164401654016640167401684016940170401714017240173401744017540176401774017840179401804018140182401834018440185401864018740188401894019040191401924019340194401954019640197401984019940200402014020240203402044020540206402074020840209402104021140212402134021440215402164021740218402194022040221402224022340224402254022640227402284022940230402314023240233402344023540236402374023840239402404024140242402434024440245402464024740248402494025040251402524025340254402554025640257402584025940260402614026240263402644026540266402674026840269402704027140272402734027440275402764027740278402794028040281402824028340284402854028640287402884028940290402914029240293402944029540296402974029840299403004030140302403034030440305403064030740308403094031040311403124031340314403154031640317403184031940320403214032240323403244032540326403274032840329403304033140332403334033440335403364033740338403394034040341403424034340344403454034640347403484034940350403514035240353403544035540356403574035840359403604036140362403634036440365403664036740368403694037040371403724037340374403754037640377403784037940380403814038240383403844038540386403874038840389403904039140392403934039440395403964039740398403994040040401404024040340404404054040640407404084040940410404114041240413404144041540416404174041840419404204042140422404234042440425404264042740428404294043040431404324043340434404354043640437404384043940440404414044240443404444044540446404474044840449404504045140452404534045440455404564045740458404594046040461404624046340464404654046640467404684046940470404714047240473404744047540476404774047840479404804048140482404834048440485404864048740488404894049040491404924049340494404954049640497404984049940500405014050240503405044050540506405074050840509405104051140512405134051440515405164051740518405194052040521405224052340524405254052640527405284052940530405314053240533405344053540536405374053840539405404054140542405434054440545405464054740548405494055040551405524055340554405554055640557405584055940560405614056240563405644056540566405674056840569405704057140572405734057440575405764057740578405794058040581405824058340584405854058640587405884058940590405914059240593405944059540596405974059840599406004060140602406034060440605406064060740608406094061040611406124061340614406154061640617406184061940620406214062240623406244062540626406274062840629406304063140632406334063440635406364063740638406394064040641406424064340644406454064640647406484064940650406514065240653406544065540656406574065840659406604066140662406634066440665406664066740668406694067040671406724067340674406754067640677406784067940680406814068240683406844068540686406874068840689406904069140692406934069440695406964069740698406994070040701407024070340704407054070640707407084070940710407114071240713407144071540716407174071840719407204072140722407234072440725407264072740728407294073040731407324073340734407354073640737407384073940740407414074240743407444074540746407474074840749407504075140752407534075440755407564075740758407594076040761407624076340764407654076640767407684076940770407714077240773407744077540776407774077840779407804078140782407834078440785407864078740788407894079040791407924079340794407954079640797407984079940800408014080240803408044080540806408074080840809408104081140812408134081440815408164081740818408194082040821408224082340824408254082640827408284082940830408314083240833408344083540836408374083840839408404084140842408434084440845408464084740848408494085040851408524085340854408554085640857408584085940860408614086240863408644086540866408674086840869408704087140872408734087440875408764087740878408794088040881408824088340884408854088640887408884088940890408914089240893408944089540896408974089840899409004090140902409034090440905409064090740908409094091040911409124091340914409154091640917409184091940920409214092240923409244092540926409274092840929409304093140932409334093440935409364093740938409394094040941409424094340944409454094640947409484094940950409514095240953409544095540956409574095840959409604096140962409634096440965409664096740968409694097040971409724097340974409754097640977409784097940980409814098240983409844098540986409874098840989409904099140992409934099440995409964099740998409994100041001410024100341004410054100641007410084100941010410114101241013410144101541016410174101841019410204102141022410234102441025410264102741028410294103041031410324103341034410354103641037410384103941040410414104241043410444104541046410474104841049410504105141052410534105441055410564105741058410594106041061410624106341064410654106641067410684106941070410714107241073410744107541076410774107841079410804108141082410834108441085410864108741088410894109041091410924109341094410954109641097410984109941100411014110241103411044110541106411074110841109411104111141112411134111441115411164111741118411194112041121411224112341124411254112641127411284112941130411314113241133411344113541136411374113841139411404114141142411434114441145411464114741148411494115041151411524115341154411554115641157411584115941160411614116241163411644116541166411674116841169411704117141172411734117441175411764117741178411794118041181411824118341184411854118641187411884118941190411914119241193411944119541196411974119841199412004120141202412034120441205412064120741208412094121041211412124121341214412154121641217412184121941220412214122241223412244122541226412274122841229412304123141232412334123441235412364123741238412394124041241412424124341244412454124641247412484124941250412514125241253412544125541256412574125841259412604126141262412634126441265412664126741268412694127041271412724127341274412754127641277412784127941280412814128241283412844128541286412874128841289412904129141292412934129441295412964129741298412994130041301413024130341304413054130641307413084130941310413114131241313413144131541316413174131841319413204132141322413234132441325413264132741328413294133041331413324133341334413354133641337413384133941340413414134241343413444134541346413474134841349413504135141352413534135441355413564135741358413594136041361413624136341364413654136641367413684136941370413714137241373413744137541376413774137841379413804138141382413834138441385413864138741388413894139041391413924139341394413954139641397413984139941400414014140241403414044140541406414074140841409414104141141412414134141441415414164141741418414194142041421414224142341424414254142641427414284142941430414314143241433414344143541436414374143841439414404144141442414434144441445414464144741448414494145041451414524145341454414554145641457414584145941460414614146241463414644146541466414674146841469414704147141472414734147441475414764147741478414794148041481414824148341484414854148641487414884148941490414914149241493414944149541496414974149841499415004150141502415034150441505415064150741508415094151041511415124151341514415154151641517415184151941520415214152241523415244152541526415274152841529415304153141532415334153441535415364153741538415394154041541415424154341544415454154641547415484154941550415514155241553415544155541556415574155841559415604156141562415634156441565415664156741568415694157041571415724157341574415754157641577415784157941580415814158241583415844158541586415874158841589415904159141592415934159441595415964159741598415994160041601416024160341604416054160641607416084160941610416114161241613416144161541616416174161841619416204162141622416234162441625416264162741628416294163041631416324163341634416354163641637416384163941640416414164241643416444164541646416474164841649416504165141652416534165441655416564165741658416594166041661416624166341664416654166641667416684166941670416714167241673416744167541676416774167841679416804168141682416834168441685416864168741688416894169041691416924169341694416954169641697416984169941700417014170241703417044170541706417074170841709417104171141712417134171441715417164171741718417194172041721417224172341724417254172641727417284172941730417314173241733417344173541736417374173841739417404174141742417434174441745417464174741748417494175041751417524175341754417554175641757417584175941760417614176241763417644176541766417674176841769417704177141772417734177441775417764177741778417794178041781417824178341784417854178641787417884178941790417914179241793417944179541796417974179841799418004180141802418034180441805418064180741808418094181041811418124181341814418154181641817418184181941820418214182241823418244182541826418274182841829418304183141832418334183441835418364183741838418394184041841418424184341844418454184641847418484184941850418514185241853418544185541856418574185841859418604186141862418634186441865418664186741868418694187041871418724187341874418754187641877418784187941880418814188241883418844188541886418874188841889418904189141892418934189441895418964189741898418994190041901419024190341904419054190641907419084190941910419114191241913419144191541916419174191841919419204192141922419234192441925419264192741928419294193041931419324193341934419354193641937419384193941940419414194241943419444194541946419474194841949419504195141952419534195441955419564195741958419594196041961419624196341964419654196641967419684196941970419714197241973419744197541976419774197841979419804198141982419834198441985419864198741988419894199041991419924199341994419954199641997419984199942000420014200242003420044200542006420074200842009420104201142012420134201442015420164201742018420194202042021420224202342024420254202642027420284202942030420314203242033420344203542036420374203842039420404204142042420434204442045420464204742048420494205042051420524205342054420554205642057420584205942060420614206242063420644206542066420674206842069420704207142072420734207442075420764207742078420794208042081420824208342084420854208642087420884208942090420914209242093420944209542096420974209842099421004210142102421034210442105421064210742108421094211042111421124211342114421154211642117421184211942120421214212242123421244212542126421274212842129421304213142132421334213442135421364213742138421394214042141421424214342144421454214642147421484214942150421514215242153421544215542156421574215842159421604216142162421634216442165421664216742168421694217042171421724217342174421754217642177421784217942180421814218242183421844218542186421874218842189421904219142192421934219442195421964219742198421994220042201422024220342204422054220642207422084220942210422114221242213422144221542216422174221842219422204222142222422234222442225422264222742228422294223042231422324223342234422354223642237422384223942240422414224242243422444224542246422474224842249422504225142252422534225442255422564225742258422594226042261422624226342264422654226642267422684226942270422714227242273422744227542276422774227842279422804228142282422834228442285422864228742288422894229042291422924229342294422954229642297422984229942300423014230242303423044230542306423074230842309423104231142312423134231442315423164231742318423194232042321423224232342324423254232642327423284232942330423314233242333423344233542336423374233842339423404234142342423434234442345423464234742348423494235042351423524235342354423554235642357423584235942360423614236242363423644236542366423674236842369423704237142372423734237442375423764237742378423794238042381423824238342384423854238642387423884238942390423914239242393423944239542396423974239842399424004240142402424034240442405424064240742408424094241042411424124241342414424154241642417424184241942420424214242242423424244242542426424274242842429424304243142432424334243442435424364243742438424394244042441424424244342444424454244642447424484244942450424514245242453424544245542456424574245842459424604246142462424634246442465424664246742468424694247042471424724247342474424754247642477424784247942480424814248242483424844248542486424874248842489424904249142492424934249442495424964249742498424994250042501425024250342504425054250642507425084250942510425114251242513425144251542516425174251842519425204252142522425234252442525425264252742528425294253042531425324253342534425354253642537425384253942540425414254242543425444254542546425474254842549425504255142552425534255442555425564255742558425594256042561425624256342564425654256642567425684256942570425714257242573425744257542576425774257842579425804258142582425834258442585425864258742588425894259042591425924259342594425954259642597425984259942600426014260242603426044260542606426074260842609426104261142612426134261442615426164261742618426194262042621426224262342624426254262642627426284262942630426314263242633426344263542636426374263842639426404264142642426434264442645426464264742648426494265042651426524265342654426554265642657426584265942660426614266242663426644266542666426674266842669426704267142672426734267442675426764267742678426794268042681426824268342684426854268642687426884268942690426914269242693426944269542696426974269842699427004270142702427034270442705427064270742708427094271042711427124271342714427154271642717427184271942720427214272242723427244272542726427274272842729427304273142732427334273442735427364273742738427394274042741427424274342744427454274642747427484274942750427514275242753427544275542756427574275842759427604276142762427634276442765427664276742768427694277042771427724277342774427754277642777427784277942780427814278242783427844278542786427874278842789427904279142792427934279442795427964279742798427994280042801428024280342804428054280642807428084280942810428114281242813428144281542816428174281842819428204282142822428234282442825428264282742828428294283042831428324283342834428354283642837428384283942840428414284242843428444284542846428474284842849428504285142852428534285442855428564285742858428594286042861428624286342864428654286642867428684286942870428714287242873428744287542876428774287842879428804288142882428834288442885428864288742888428894289042891428924289342894428954289642897428984289942900429014290242903429044290542906429074290842909429104291142912429134291442915429164291742918429194292042921429224292342924429254292642927429284292942930429314293242933429344293542936429374293842939429404294142942429434294442945429464294742948429494295042951429524295342954429554295642957429584295942960429614296242963429644296542966429674296842969429704297142972429734297442975429764297742978429794298042981429824298342984429854298642987429884298942990429914299242993429944299542996429974299842999430004300143002430034300443005430064300743008430094301043011430124301343014430154301643017430184301943020430214302243023430244302543026430274302843029430304303143032430334303443035430364303743038430394304043041430424304343044430454304643047430484304943050430514305243053430544305543056430574305843059430604306143062430634306443065430664306743068430694307043071430724307343074430754307643077430784307943080430814308243083430844308543086430874308843089430904309143092430934309443095430964309743098430994310043101431024310343104431054310643107431084310943110431114311243113431144311543116431174311843119431204312143122431234312443125431264312743128431294313043131431324313343134431354313643137431384313943140431414314243143431444314543146431474314843149431504315143152431534315443155431564315743158431594316043161431624316343164431654316643167431684316943170431714317243173431744317543176431774317843179431804318143182431834318443185431864318743188431894319043191431924319343194431954319643197431984319943200432014320243203432044320543206432074320843209432104321143212432134321443215432164321743218432194322043221432224322343224432254322643227432284322943230432314323243233432344323543236432374323843239432404324143242432434324443245432464324743248432494325043251432524325343254432554325643257432584325943260432614326243263432644326543266432674326843269432704327143272432734327443275432764327743278432794328043281432824328343284432854328643287432884328943290432914329243293432944329543296432974329843299433004330143302433034330443305433064330743308433094331043311433124331343314433154331643317433184331943320433214332243323433244332543326433274332843329433304333143332433334333443335433364333743338433394334043341433424334343344433454334643347433484334943350433514335243353433544335543356433574335843359433604336143362433634336443365433664336743368433694337043371433724337343374433754337643377433784337943380433814338243383433844338543386433874338843389433904339143392433934339443395433964339743398433994340043401434024340343404434054340643407434084340943410434114341243413434144341543416434174341843419434204342143422434234342443425434264342743428434294343043431434324343343434434354343643437434384343943440434414344243443434444344543446434474344843449434504345143452434534345443455434564345743458434594346043461434624346343464434654346643467434684346943470434714347243473434744347543476434774347843479434804348143482434834348443485434864348743488434894349043491434924349343494434954349643497434984349943500435014350243503435044350543506435074350843509435104351143512435134351443515435164351743518435194352043521435224352343524435254352643527435284352943530435314353243533435344353543536435374353843539435404354143542435434354443545435464354743548435494355043551435524355343554435554355643557435584355943560435614356243563435644356543566435674356843569435704357143572435734357443575435764357743578435794358043581435824358343584435854358643587435884358943590435914359243593435944359543596435974359843599436004360143602436034360443605436064360743608436094361043611436124361343614436154361643617436184361943620436214362243623436244362543626436274362843629436304363143632436334363443635436364363743638436394364043641436424364343644436454364643647436484364943650436514365243653436544365543656436574365843659436604366143662436634366443665436664366743668436694367043671436724367343674436754367643677436784367943680436814368243683436844368543686436874368843689436904369143692436934369443695436964369743698436994370043701437024370343704437054370643707437084370943710437114371243713437144371543716437174371843719437204372143722437234372443725437264372743728437294373043731437324373343734437354373643737437384373943740437414374243743437444374543746437474374843749437504375143752437534375443755437564375743758437594376043761437624376343764437654376643767437684376943770437714377243773437744377543776437774377843779437804378143782437834378443785437864378743788437894379043791437924379343794437954379643797437984379943800438014380243803438044380543806438074380843809438104381143812438134381443815438164381743818438194382043821438224382343824438254382643827438284382943830438314383243833438344383543836438374383843839438404384143842438434384443845438464384743848438494385043851438524385343854438554385643857438584385943860438614386243863438644386543866438674386843869438704387143872438734387443875438764387743878438794388043881438824388343884438854388643887438884388943890438914389243893438944389543896438974389843899439004390143902439034390443905439064390743908439094391043911439124391343914439154391643917439184391943920439214392243923439244392543926439274392843929439304393143932439334393443935439364393743938439394394043941439424394343944439454394643947439484394943950439514395243953439544395543956439574395843959439604396143962439634396443965439664396743968439694397043971439724397343974439754397643977439784397943980439814398243983439844398543986439874398843989439904399143992439934399443995439964399743998439994400044001440024400344004440054400644007440084400944010440114401244013440144401544016440174401844019440204402144022440234402444025440264402744028440294403044031440324403344034440354403644037440384403944040440414404244043440444404544046440474404844049440504405144052440534405444055440564405744058440594406044061440624406344064440654406644067440684406944070440714407244073440744407544076440774407844079440804408144082440834408444085440864408744088440894409044091440924409344094440954409644097440984409944100441014410244103441044410544106441074410844109441104411144112441134411444115441164411744118441194412044121441224412344124441254412644127441284412944130441314413244133441344413544136441374413844139441404414144142441434414444145441464414744148441494415044151441524415344154441554415644157441584415944160441614416244163441644416544166441674416844169441704417144172441734417444175441764417744178441794418044181441824418344184441854418644187441884418944190441914419244193441944419544196441974419844199442004420144202442034420444205442064420744208442094421044211442124421344214442154421644217442184421944220442214422244223442244422544226442274422844229442304423144232442334423444235442364423744238442394424044241442424424344244442454424644247442484424944250442514425244253442544425544256442574425844259442604426144262442634426444265442664426744268442694427044271442724427344274442754427644277442784427944280442814428244283442844428544286442874428844289442904429144292442934429444295442964429744298442994430044301443024430344304443054430644307443084430944310443114431244313443144431544316443174431844319443204432144322443234432444325443264432744328443294433044331443324433344334443354433644337443384433944340443414434244343443444434544346443474434844349443504435144352443534435444355443564435744358443594436044361443624436344364443654436644367443684436944370443714437244373443744437544376443774437844379443804438144382443834438444385443864438744388443894439044391443924439344394443954439644397443984439944400444014440244403444044440544406444074440844409444104441144412444134441444415444164441744418444194442044421444224442344424444254442644427444284442944430444314443244433444344443544436444374443844439444404444144442444434444444445444464444744448444494445044451444524445344454444554445644457444584445944460444614446244463444644446544466444674446844469444704447144472444734447444475444764447744478444794448044481444824448344484444854448644487444884448944490444914449244493444944449544496444974449844499445004450144502445034450444505445064450744508445094451044511445124451344514445154451644517445184451944520445214452244523445244452544526445274452844529445304453144532445334453444535445364453744538445394454044541445424454344544445454454644547445484454944550445514455244553445544455544556445574455844559445604456144562445634456444565445664456744568445694457044571445724457344574445754457644577445784457944580445814458244583445844458544586445874458844589445904459144592445934459444595445964459744598445994460044601446024460344604446054460644607446084460944610446114461244613446144461544616446174461844619446204462144622446234462444625446264462744628446294463044631446324463344634446354463644637446384463944640446414464244643446444464544646446474464844649446504465144652446534465444655446564465744658446594466044661446624466344664446654466644667446684466944670446714467244673446744467544676446774467844679446804468144682446834468444685446864468744688446894469044691446924469344694446954469644697446984469944700447014470244703447044470544706447074470844709447104471144712447134471444715447164471744718447194472044721447224472344724447254472644727447284472944730447314473244733447344473544736447374473844739447404474144742447434474444745447464474744748447494475044751447524475344754447554475644757447584475944760447614476244763447644476544766447674476844769447704477144772447734477444775447764477744778447794478044781447824478344784447854478644787447884478944790447914479244793447944479544796447974479844799448004480144802448034480444805448064480744808448094481044811448124481344814448154481644817448184481944820448214482244823448244482544826448274482844829448304483144832448334483444835448364483744838448394484044841448424484344844448454484644847448484484944850448514485244853448544485544856448574485844859448604486144862448634486444865448664486744868448694487044871448724487344874448754487644877448784487944880448814488244883448844488544886448874488844889448904489144892448934489444895448964489744898448994490044901449024490344904449054490644907449084490944910449114491244913449144491544916449174491844919449204492144922449234492444925449264492744928449294493044931449324493344934449354493644937449384493944940449414494244943449444494544946449474494844949449504495144952449534495444955449564495744958449594496044961449624496344964449654496644967449684496944970449714497244973449744497544976449774497844979449804498144982449834498444985449864498744988449894499044991449924499344994449954499644997449984499945000450014500245003450044500545006450074500845009450104501145012450134501445015450164501745018450194502045021450224502345024450254502645027450284502945030450314503245033450344503545036450374503845039450404504145042450434504445045450464504745048450494505045051450524505345054450554505645057450584505945060450614506245063450644506545066450674506845069450704507145072450734507445075450764507745078450794508045081450824508345084450854508645087450884508945090450914509245093450944509545096450974509845099451004510145102451034510445105451064510745108451094511045111451124511345114451154511645117451184511945120451214512245123451244512545126451274512845129451304513145132451334513445135451364513745138451394514045141451424514345144451454514645147451484514945150451514515245153451544515545156451574515845159451604516145162451634516445165451664516745168451694517045171451724517345174451754517645177451784517945180451814518245183451844518545186451874518845189451904519145192451934519445195451964519745198451994520045201452024520345204452054520645207452084520945210452114521245213452144521545216452174521845219452204522145222452234522445225452264522745228452294523045231452324523345234452354523645237452384523945240452414524245243452444524545246452474524845249452504525145252452534525445255452564525745258452594526045261452624526345264452654526645267452684526945270452714527245273452744527545276452774527845279452804528145282452834528445285452864528745288452894529045291452924529345294452954529645297452984529945300453014530245303453044530545306453074530845309453104531145312453134531445315453164531745318453194532045321453224532345324453254532645327453284532945330453314533245333453344533545336453374533845339453404534145342453434534445345453464534745348453494535045351453524535345354453554535645357453584535945360453614536245363453644536545366453674536845369453704537145372453734537445375453764537745378453794538045381453824538345384453854538645387453884538945390453914539245393453944539545396453974539845399454004540145402454034540445405454064540745408454094541045411454124541345414454154541645417454184541945420454214542245423454244542545426454274542845429454304543145432454334543445435454364543745438454394544045441454424544345444454454544645447454484544945450454514545245453454544545545456454574545845459454604546145462454634546445465454664546745468454694547045471454724547345474454754547645477454784547945480454814548245483454844548545486454874548845489454904549145492454934549445495454964549745498454994550045501455024550345504455054550645507455084550945510455114551245513455144551545516455174551845519455204552145522455234552445525455264552745528455294553045531455324553345534455354553645537455384553945540455414554245543455444554545546455474554845549455504555145552455534555445555455564555745558455594556045561455624556345564455654556645567455684556945570455714557245573455744557545576455774557845579455804558145582455834558445585455864558745588455894559045591455924559345594455954559645597455984559945600456014560245603456044560545606456074560845609456104561145612456134561445615456164561745618456194562045621456224562345624456254562645627456284562945630456314563245633456344563545636456374563845639456404564145642456434564445645456464564745648456494565045651456524565345654456554565645657456584565945660456614566245663456644566545666456674566845669456704567145672456734567445675456764567745678456794568045681456824568345684456854568645687456884568945690456914569245693456944569545696456974569845699457004570145702457034570445705457064570745708457094571045711457124571345714457154571645717457184571945720457214572245723457244572545726457274572845729457304573145732457334573445735457364573745738457394574045741457424574345744457454574645747457484574945750457514575245753457544575545756457574575845759457604576145762457634576445765457664576745768457694577045771457724577345774457754577645777457784577945780457814578245783457844578545786457874578845789457904579145792457934579445795457964579745798457994580045801458024580345804458054580645807458084580945810458114581245813458144581545816458174581845819458204582145822458234582445825458264582745828458294583045831458324583345834458354583645837458384583945840458414584245843458444584545846458474584845849458504585145852458534585445855458564585745858458594586045861458624586345864458654586645867458684586945870458714587245873458744587545876458774587845879458804588145882458834588445885458864588745888458894589045891458924589345894458954589645897458984589945900459014590245903459044590545906459074590845909459104591145912459134591445915459164591745918459194592045921459224592345924459254592645927459284592945930459314593245933459344593545936459374593845939459404594145942459434594445945459464594745948459494595045951459524595345954459554595645957459584595945960459614596245963459644596545966459674596845969459704597145972459734597445975459764597745978459794598045981459824598345984459854598645987459884598945990459914599245993459944599545996459974599845999460004600146002460034600446005460064600746008460094601046011460124601346014460154601646017460184601946020460214602246023460244602546026460274602846029460304603146032460334603446035460364603746038460394604046041460424604346044460454604646047460484604946050460514605246053460544605546056460574605846059460604606146062460634606446065460664606746068460694607046071460724607346074460754607646077460784607946080460814608246083460844608546086460874608846089460904609146092460934609446095460964609746098460994610046101461024610346104461054610646107461084610946110461114611246113461144611546116461174611846119461204612146122461234612446125461264612746128461294613046131461324613346134461354613646137461384613946140461414614246143461444614546146461474614846149461504615146152461534615446155461564615746158461594616046161461624616346164461654616646167461684616946170461714617246173461744617546176461774617846179461804618146182461834618446185461864618746188461894619046191461924619346194461954619646197461984619946200462014620246203462044620546206462074620846209462104621146212462134621446215462164621746218462194622046221462224622346224462254622646227462284622946230462314623246233462344623546236462374623846239462404624146242462434624446245462464624746248462494625046251462524625346254462554625646257462584625946260462614626246263462644626546266462674626846269462704627146272462734627446275462764627746278462794628046281462824628346284462854628646287462884628946290462914629246293462944629546296462974629846299463004630146302463034630446305463064630746308463094631046311463124631346314463154631646317463184631946320463214632246323463244632546326463274632846329463304633146332463334633446335463364633746338463394634046341463424634346344463454634646347463484634946350463514635246353463544635546356463574635846359463604636146362463634636446365463664636746368463694637046371463724637346374463754637646377463784637946380463814638246383463844638546386463874638846389463904639146392463934639446395463964639746398463994640046401464024640346404464054640646407464084640946410464114641246413464144641546416464174641846419464204642146422464234642446425464264642746428464294643046431464324643346434464354643646437464384643946440464414644246443464444644546446464474644846449464504645146452464534645446455464564645746458464594646046461464624646346464464654646646467464684646946470464714647246473464744647546476464774647846479464804648146482464834648446485464864648746488464894649046491464924649346494464954649646497464984649946500465014650246503465044650546506465074650846509465104651146512465134651446515465164651746518465194652046521465224652346524465254652646527465284652946530465314653246533465344653546536465374653846539465404654146542465434654446545465464654746548465494655046551465524655346554465554655646557465584655946560465614656246563465644656546566465674656846569465704657146572465734657446575465764657746578465794658046581465824658346584465854658646587465884658946590465914659246593465944659546596465974659846599466004660146602466034660446605466064660746608466094661046611466124661346614466154661646617466184661946620466214662246623466244662546626466274662846629466304663146632466334663446635466364663746638466394664046641466424664346644466454664646647466484664946650466514665246653466544665546656466574665846659466604666146662466634666446665466664666746668466694667046671466724667346674466754667646677466784667946680466814668246683466844668546686466874668846689466904669146692466934669446695466964669746698466994670046701467024670346704467054670646707467084670946710467114671246713467144671546716467174671846719467204672146722467234672446725467264672746728467294673046731467324673346734467354673646737467384673946740467414674246743467444674546746467474674846749467504675146752467534675446755467564675746758467594676046761467624676346764467654676646767467684676946770467714677246773467744677546776467774677846779467804678146782467834678446785467864678746788467894679046791467924679346794467954679646797467984679946800468014680246803468044680546806468074680846809468104681146812468134681446815468164681746818468194682046821468224682346824468254682646827468284682946830468314683246833468344683546836468374683846839468404684146842468434684446845468464684746848468494685046851468524685346854468554685646857468584685946860468614686246863468644686546866468674686846869468704687146872468734687446875468764687746878468794688046881468824688346884468854688646887468884688946890468914689246893468944689546896468974689846899469004690146902469034690446905469064690746908469094691046911469124691346914469154691646917469184691946920469214692246923469244692546926469274692846929469304693146932469334693446935469364693746938469394694046941469424694346944469454694646947469484694946950469514695246953469544695546956469574695846959469604696146962469634696446965469664696746968469694697046971469724697346974469754697646977469784697946980469814698246983469844698546986469874698846989469904699146992469934699446995469964699746998469994700047001470024700347004470054700647007470084700947010470114701247013470144701547016470174701847019470204702147022470234702447025470264702747028470294703047031470324703347034470354703647037470384703947040470414704247043470444704547046470474704847049470504705147052470534705447055470564705747058470594706047061470624706347064470654706647067470684706947070470714707247073470744707547076470774707847079470804708147082470834708447085470864708747088470894709047091470924709347094470954709647097470984709947100471014710247103471044710547106471074710847109471104711147112471134711447115471164711747118471194712047121471224712347124471254712647127471284712947130471314713247133471344713547136471374713847139471404714147142471434714447145471464714747148471494715047151471524715347154471554715647157471584715947160471614716247163471644716547166471674716847169471704717147172471734717447175471764717747178471794718047181471824718347184471854718647187471884718947190471914719247193471944719547196471974719847199472004720147202472034720447205472064720747208472094721047211472124721347214472154721647217472184721947220472214722247223472244722547226472274722847229472304723147232472334723447235472364723747238472394724047241472424724347244472454724647247472484724947250472514725247253472544725547256472574725847259472604726147262472634726447265472664726747268472694727047271472724727347274472754727647277472784727947280472814728247283472844728547286472874728847289472904729147292472934729447295472964729747298472994730047301473024730347304473054730647307473084730947310473114731247313473144731547316473174731847319473204732147322473234732447325473264732747328473294733047331473324733347334473354733647337473384733947340473414734247343473444734547346473474734847349473504735147352473534735447355473564735747358473594736047361473624736347364473654736647367473684736947370473714737247373473744737547376473774737847379473804738147382473834738447385473864738747388473894739047391473924739347394473954739647397473984739947400474014740247403474044740547406474074740847409474104741147412474134741447415474164741747418474194742047421474224742347424474254742647427474284742947430474314743247433474344743547436474374743847439474404744147442474434744447445474464744747448474494745047451474524745347454474554745647457474584745947460474614746247463474644746547466474674746847469474704747147472474734747447475474764747747478474794748047481474824748347484474854748647487474884748947490474914749247493474944749547496474974749847499475004750147502475034750447505475064750747508475094751047511475124751347514475154751647517475184751947520475214752247523475244752547526475274752847529475304753147532475334753447535475364753747538475394754047541475424754347544475454754647547475484754947550475514755247553475544755547556475574755847559475604756147562475634756447565475664756747568475694757047571475724757347574475754757647577475784757947580475814758247583475844758547586475874758847589475904759147592475934759447595475964759747598475994760047601476024760347604476054760647607476084760947610476114761247613476144761547616476174761847619476204762147622476234762447625476264762747628476294763047631476324763347634476354763647637476384763947640476414764247643476444764547646476474764847649476504765147652476534765447655476564765747658476594766047661476624766347664476654766647667476684766947670476714767247673476744767547676476774767847679476804768147682476834768447685476864768747688476894769047691476924769347694476954769647697476984769947700477014770247703477044770547706477074770847709477104771147712477134771447715477164771747718477194772047721477224772347724477254772647727477284772947730477314773247733477344773547736477374773847739477404774147742477434774447745477464774747748477494775047751477524775347754477554775647757477584775947760477614776247763477644776547766477674776847769477704777147772477734777447775477764777747778477794778047781477824778347784477854778647787477884778947790477914779247793477944779547796477974779847799478004780147802478034780447805478064780747808478094781047811478124781347814478154781647817478184781947820478214782247823478244782547826478274782847829478304783147832478334783447835478364783747838478394784047841478424784347844478454784647847478484784947850478514785247853478544785547856478574785847859478604786147862478634786447865478664786747868478694787047871478724787347874478754787647877478784787947880478814788247883478844788547886478874788847889478904789147892478934789447895478964789747898478994790047901479024790347904479054790647907479084790947910479114791247913479144791547916479174791847919479204792147922479234792447925479264792747928479294793047931479324793347934479354793647937479384793947940479414794247943479444794547946479474794847949479504795147952479534795447955479564795747958479594796047961479624796347964479654796647967479684796947970479714797247973479744797547976479774797847979479804798147982479834798447985479864798747988479894799047991479924799347994479954799647997479984799948000480014800248003480044800548006480074800848009480104801148012480134801448015480164801748018480194802048021480224802348024480254802648027480284802948030480314803248033480344803548036480374803848039480404804148042480434804448045480464804748048480494805048051480524805348054480554805648057480584805948060480614806248063480644806548066480674806848069480704807148072480734807448075480764807748078480794808048081480824808348084480854808648087480884808948090480914809248093480944809548096480974809848099481004810148102481034810448105481064810748108481094811048111481124811348114481154811648117481184811948120481214812248123481244812548126481274812848129481304813148132481334813448135481364813748138481394814048141481424814348144481454814648147481484814948150481514815248153481544815548156481574815848159481604816148162481634816448165481664816748168481694817048171481724817348174481754817648177481784817948180481814818248183481844818548186481874818848189481904819148192481934819448195481964819748198481994820048201482024820348204482054820648207482084820948210482114821248213482144821548216482174821848219482204822148222482234822448225482264822748228482294823048231482324823348234482354823648237482384823948240482414824248243482444824548246482474824848249482504825148252482534825448255482564825748258482594826048261482624826348264482654826648267482684826948270482714827248273482744827548276482774827848279482804828148282482834828448285482864828748288482894829048291482924829348294482954829648297482984829948300483014830248303483044830548306483074830848309483104831148312483134831448315483164831748318483194832048321483224832348324483254832648327483284832948330483314833248333483344833548336483374833848339483404834148342483434834448345483464834748348483494835048351483524835348354483554835648357483584835948360483614836248363483644836548366483674836848369483704837148372483734837448375483764837748378483794838048381483824838348384483854838648387483884838948390483914839248393483944839548396483974839848399484004840148402484034840448405484064840748408484094841048411484124841348414484154841648417484184841948420484214842248423484244842548426484274842848429484304843148432484334843448435484364843748438484394844048441484424844348444484454844648447484484844948450484514845248453484544845548456484574845848459484604846148462484634846448465484664846748468484694847048471484724847348474484754847648477484784847948480484814848248483484844848548486484874848848489484904849148492484934849448495484964849748498484994850048501485024850348504485054850648507485084850948510485114851248513485144851548516485174851848519485204852148522485234852448525485264852748528485294853048531485324853348534485354853648537485384853948540485414854248543485444854548546485474854848549485504855148552485534855448555485564855748558485594856048561485624856348564485654856648567485684856948570485714857248573485744857548576485774857848579485804858148582485834858448585485864858748588485894859048591485924859348594485954859648597485984859948600486014860248603486044860548606486074860848609486104861148612486134861448615486164861748618486194862048621486224862348624486254862648627486284862948630486314863248633486344863548636486374863848639486404864148642486434864448645486464864748648486494865048651486524865348654486554865648657486584865948660486614866248663486644866548666486674866848669486704867148672486734867448675486764867748678486794868048681486824868348684486854868648687486884868948690486914869248693486944869548696486974869848699487004870148702487034870448705487064870748708487094871048711487124871348714487154871648717487184871948720487214872248723487244872548726487274872848729487304873148732487334873448735487364873748738487394874048741487424874348744487454874648747487484874948750487514875248753487544875548756487574875848759487604876148762487634876448765487664876748768487694877048771487724877348774487754877648777487784877948780487814878248783487844878548786487874878848789487904879148792487934879448795487964879748798487994880048801488024880348804488054880648807488084880948810488114881248813488144881548816488174881848819488204882148822488234882448825488264882748828488294883048831488324883348834488354883648837488384883948840488414884248843488444884548846488474884848849488504885148852488534885448855488564885748858488594886048861488624886348864488654886648867488684886948870488714887248873488744887548876488774887848879488804888148882488834888448885488864888748888488894889048891488924889348894488954889648897488984889948900489014890248903489044890548906489074890848909489104891148912489134891448915489164891748918489194892048921489224892348924489254892648927489284892948930489314893248933489344893548936489374893848939489404894148942489434894448945489464894748948489494895048951489524895348954489554895648957489584895948960489614896248963489644896548966489674896848969489704897148972489734897448975489764897748978489794898048981489824898348984489854898648987489884898948990489914899248993489944899548996489974899848999490004900149002490034900449005490064900749008490094901049011490124901349014490154901649017490184901949020490214902249023490244902549026490274902849029490304903149032490334903449035490364903749038490394904049041490424904349044490454904649047490484904949050490514905249053490544905549056490574905849059490604906149062490634906449065490664906749068490694907049071490724907349074490754907649077490784907949080490814908249083490844908549086490874908849089490904909149092490934909449095490964909749098490994910049101491024910349104491054910649107491084910949110491114911249113491144911549116491174911849119491204912149122491234912449125491264912749128491294913049131491324913349134491354913649137491384913949140491414914249143491444914549146491474914849149491504915149152491534915449155491564915749158491594916049161491624916349164491654916649167491684916949170491714917249173491744917549176491774917849179491804918149182491834918449185491864918749188491894919049191491924919349194491954919649197491984919949200492014920249203492044920549206492074920849209492104921149212492134921449215492164921749218492194922049221492224922349224492254922649227492284922949230492314923249233492344923549236492374923849239492404924149242492434924449245492464924749248492494925049251492524925349254492554925649257492584925949260492614926249263492644926549266492674926849269492704927149272492734927449275492764927749278492794928049281492824928349284492854928649287492884928949290492914929249293492944929549296492974929849299493004930149302493034930449305493064930749308493094931049311493124931349314493154931649317493184931949320493214932249323493244932549326493274932849329493304933149332493334933449335493364933749338493394934049341493424934349344493454934649347493484934949350493514935249353493544935549356493574935849359493604936149362493634936449365493664936749368493694937049371493724937349374493754937649377493784937949380493814938249383493844938549386493874938849389493904939149392493934939449395493964939749398493994940049401494024940349404494054940649407494084940949410494114941249413494144941549416494174941849419494204942149422494234942449425494264942749428494294943049431494324943349434494354943649437494384943949440494414944249443494444944549446494474944849449494504945149452494534945449455494564945749458494594946049461494624946349464494654946649467494684946949470494714947249473494744947549476494774947849479494804948149482494834948449485494864948749488494894949049491494924949349494494954949649497494984949949500495014950249503495044950549506495074950849509495104951149512495134951449515495164951749518495194952049521495224952349524495254952649527495284952949530495314953249533495344953549536495374953849539495404954149542495434954449545495464954749548495494955049551495524955349554495554955649557495584955949560495614956249563495644956549566495674956849569495704957149572495734957449575495764957749578495794958049581495824958349584495854958649587495884958949590495914959249593495944959549596495974959849599496004960149602496034960449605496064960749608496094961049611496124961349614496154961649617496184961949620496214962249623496244962549626496274962849629496304963149632496334963449635496364963749638496394964049641496424964349644496454964649647496484964949650496514965249653496544965549656496574965849659496604966149662496634966449665496664966749668496694967049671496724967349674496754967649677496784967949680496814968249683496844968549686496874968849689496904969149692496934969449695496964969749698496994970049701497024970349704497054970649707497084970949710497114971249713497144971549716497174971849719497204972149722497234972449725497264972749728497294973049731497324973349734497354973649737497384973949740497414974249743497444974549746497474974849749497504975149752497534975449755497564975749758497594976049761497624976349764497654976649767497684976949770497714977249773497744977549776497774977849779497804978149782497834978449785497864978749788497894979049791497924979349794497954979649797497984979949800498014980249803498044980549806498074980849809498104981149812498134981449815498164981749818498194982049821498224982349824498254982649827498284982949830498314983249833498344983549836498374983849839498404984149842498434984449845498464984749848498494985049851498524985349854498554985649857498584985949860498614986249863498644986549866498674986849869498704987149872498734987449875498764987749878498794988049881498824988349884498854988649887498884988949890498914989249893498944989549896498974989849899499004990149902499034990449905499064990749908499094991049911499124991349914499154991649917499184991949920499214992249923499244992549926499274992849929499304993149932499334993449935499364993749938499394994049941499424994349944499454994649947499484994949950499514995249953499544995549956499574995849959499604996149962499634996449965499664996749968499694997049971499724997349974499754997649977499784997949980499814998249983499844998549986499874998849989499904999149992499934999449995499964999749998499995000050001500025000350004500055000650007500085000950010500115001250013500145001550016500175001850019500205002150022500235002450025500265002750028500295003050031500325003350034500355003650037500385003950040500415004250043500445004550046500475004850049500505005150052500535005450055500565005750058500595006050061500625006350064500655006650067500685006950070500715007250073500745007550076500775007850079500805008150082500835008450085500865008750088500895009050091500925009350094500955009650097500985009950100501015010250103501045010550106501075010850109501105011150112501135011450115501165011750118501195012050121501225012350124501255012650127501285012950130501315013250133501345013550136501375013850139501405014150142501435014450145501465014750148501495015050151501525015350154501555015650157501585015950160501615016250163501645016550166501675016850169501705017150172501735017450175501765017750178501795018050181501825018350184501855018650187501885018950190501915019250193501945019550196501975019850199502005020150202502035020450205502065020750208502095021050211502125021350214502155021650217502185021950220502215022250223502245022550226502275022850229502305023150232502335023450235502365023750238502395024050241502425024350244502455024650247502485024950250502515025250253502545025550256502575025850259502605026150262502635026450265502665026750268502695027050271502725027350274502755027650277502785027950280502815028250283502845028550286502875028850289502905029150292502935029450295502965029750298502995030050301503025030350304503055030650307503085030950310503115031250313503145031550316503175031850319503205032150322503235032450325503265032750328503295033050331503325033350334503355033650337503385033950340503415034250343503445034550346503475034850349503505035150352503535035450355503565035750358503595036050361503625036350364503655036650367503685036950370503715037250373503745037550376503775037850379503805038150382503835038450385503865038750388503895039050391503925039350394503955039650397503985039950400504015040250403504045040550406504075040850409504105041150412504135041450415504165041750418504195042050421504225042350424504255042650427504285042950430504315043250433504345043550436504375043850439504405044150442504435044450445504465044750448504495045050451504525045350454504555045650457504585045950460504615046250463504645046550466504675046850469504705047150472504735047450475504765047750478504795048050481504825048350484504855048650487504885048950490504915049250493504945049550496504975049850499505005050150502505035050450505505065050750508505095051050511505125051350514505155051650517505185051950520505215052250523505245052550526505275052850529505305053150532505335053450535505365053750538505395054050541505425054350544505455054650547505485054950550505515055250553505545055550556505575055850559505605056150562505635056450565505665056750568505695057050571505725057350574505755057650577505785057950580505815058250583505845058550586505875058850589505905059150592505935059450595505965059750598505995060050601506025060350604506055060650607506085060950610506115061250613506145061550616506175061850619506205062150622506235062450625506265062750628506295063050631506325063350634506355063650637506385063950640506415064250643506445064550646506475064850649506505065150652506535065450655506565065750658506595066050661506625066350664506655066650667506685066950670506715067250673506745067550676506775067850679506805068150682506835068450685506865068750688506895069050691506925069350694506955069650697506985069950700507015070250703507045070550706507075070850709507105071150712507135071450715507165071750718507195072050721507225072350724507255072650727507285072950730507315073250733507345073550736507375073850739507405074150742507435074450745507465074750748507495075050751507525075350754507555075650757507585075950760507615076250763507645076550766507675076850769507705077150772507735077450775507765077750778507795078050781507825078350784507855078650787507885078950790507915079250793507945079550796507975079850799508005080150802508035080450805508065080750808508095081050811508125081350814508155081650817508185081950820508215082250823508245082550826508275082850829508305083150832508335083450835508365083750838508395084050841508425084350844508455084650847508485084950850508515085250853508545085550856508575085850859508605086150862508635086450865508665086750868508695087050871508725087350874508755087650877508785087950880508815088250883508845088550886508875088850889508905089150892508935089450895508965089750898508995090050901509025090350904509055090650907509085090950910509115091250913509145091550916509175091850919509205092150922509235092450925509265092750928509295093050931509325093350934509355093650937509385093950940509415094250943509445094550946509475094850949509505095150952509535095450955509565095750958509595096050961509625096350964509655096650967509685096950970509715097250973509745097550976509775097850979509805098150982509835098450985509865098750988509895099050991509925099350994509955099650997509985099951000510015100251003510045100551006510075100851009510105101151012510135101451015510165101751018510195102051021510225102351024510255102651027510285102951030510315103251033510345103551036510375103851039510405104151042510435104451045510465104751048510495105051051510525105351054510555105651057510585105951060510615106251063510645106551066510675106851069510705107151072510735107451075510765107751078510795108051081510825108351084510855108651087510885108951090510915109251093510945109551096510975109851099511005110151102511035110451105511065110751108511095111051111511125111351114511155111651117511185111951120511215112251123511245112551126511275112851129511305113151132511335113451135511365113751138511395114051141511425114351144511455114651147511485114951150511515115251153511545115551156511575115851159511605116151162511635116451165511665116751168511695117051171511725117351174511755117651177511785117951180511815118251183511845118551186511875118851189511905119151192511935119451195511965119751198511995120051201512025120351204512055120651207512085120951210512115121251213512145121551216512175121851219512205122151222512235122451225512265122751228512295123051231512325123351234512355123651237512385123951240512415124251243512445124551246512475124851249512505125151252512535125451255512565125751258512595126051261512625126351264512655126651267512685126951270512715127251273512745127551276512775127851279512805128151282512835128451285512865128751288512895129051291512925129351294512955129651297512985129951300513015130251303513045130551306513075130851309513105131151312513135131451315513165131751318513195132051321513225132351324513255132651327513285132951330513315133251333513345133551336513375133851339513405134151342513435134451345513465134751348513495135051351513525135351354513555135651357513585135951360513615136251363513645136551366513675136851369513705137151372513735137451375513765137751378513795138051381513825138351384513855138651387513885138951390513915139251393513945139551396513975139851399514005140151402514035140451405514065140751408514095141051411514125141351414514155141651417514185141951420514215142251423514245142551426514275142851429514305143151432514335143451435514365143751438514395144051441514425144351444514455144651447514485144951450514515145251453514545145551456514575145851459514605146151462514635146451465514665146751468514695147051471514725147351474514755147651477514785147951480514815148251483514845148551486514875148851489514905149151492514935149451495514965149751498514995150051501515025150351504515055150651507515085150951510515115151251513515145151551516515175151851519515205152151522515235152451525515265152751528515295153051531515325153351534515355153651537515385153951540515415154251543515445154551546515475154851549515505155151552515535155451555515565155751558515595156051561515625156351564515655156651567515685156951570515715157251573515745157551576515775157851579515805158151582515835158451585515865158751588515895159051591515925159351594515955159651597515985159951600516015160251603516045160551606516075160851609516105161151612516135161451615516165161751618516195162051621516225162351624516255162651627516285162951630516315163251633516345163551636516375163851639516405164151642516435164451645516465164751648516495165051651516525165351654516555165651657516585165951660516615166251663516645166551666516675166851669516705167151672516735167451675516765167751678516795168051681516825168351684516855168651687516885168951690516915169251693516945169551696516975169851699517005170151702517035170451705517065170751708517095171051711517125171351714517155171651717517185171951720517215172251723517245172551726517275172851729517305173151732517335173451735517365173751738517395174051741517425174351744517455174651747517485174951750517515175251753517545175551756517575175851759517605176151762517635176451765517665176751768517695177051771517725177351774517755177651777517785177951780517815178251783517845178551786517875178851789517905179151792517935179451795517965179751798517995180051801518025180351804518055180651807518085180951810518115181251813518145181551816518175181851819518205182151822518235182451825518265182751828518295183051831518325183351834518355183651837518385183951840518415184251843518445184551846518475184851849518505185151852518535185451855518565185751858518595186051861518625186351864518655186651867518685186951870518715187251873518745187551876518775187851879518805188151882518835188451885518865188751888518895189051891518925189351894518955189651897518985189951900519015190251903519045190551906519075190851909519105191151912519135191451915519165191751918519195192051921519225192351924519255192651927519285192951930519315193251933519345193551936519375193851939519405194151942519435194451945519465194751948519495195051951519525195351954519555195651957519585195951960519615196251963519645196551966519675196851969519705197151972519735197451975519765197751978519795198051981519825198351984519855198651987519885198951990519915199251993519945199551996519975199851999520005200152002520035200452005520065200752008520095201052011520125201352014520155201652017520185201952020520215202252023520245202552026520275202852029520305203152032520335203452035520365203752038520395204052041520425204352044520455204652047520485204952050520515205252053520545205552056520575205852059520605206152062520635206452065520665206752068520695207052071520725207352074520755207652077520785207952080520815208252083520845208552086520875208852089520905209152092520935209452095520965209752098520995210052101521025210352104521055210652107521085210952110521115211252113521145211552116521175211852119521205212152122521235212452125521265212752128521295213052131521325213352134521355213652137521385213952140521415214252143521445214552146521475214852149521505215152152521535215452155521565215752158521595216052161521625216352164521655216652167521685216952170521715217252173521745217552176521775217852179521805218152182521835218452185521865218752188521895219052191521925219352194521955219652197521985219952200522015220252203522045220552206522075220852209522105221152212522135221452215522165221752218522195222052221522225222352224522255222652227522285222952230522315223252233522345223552236522375223852239522405224152242522435224452245522465224752248522495225052251522525225352254522555225652257522585225952260522615226252263522645226552266522675226852269522705227152272522735227452275522765227752278522795228052281522825228352284522855228652287522885228952290522915229252293522945229552296522975229852299523005230152302523035230452305523065230752308523095231052311523125231352314523155231652317523185231952320523215232252323523245232552326523275232852329523305233152332523335233452335523365233752338523395234052341523425234352344523455234652347523485234952350523515235252353523545235552356523575235852359523605236152362523635236452365523665236752368523695237052371523725237352374523755237652377523785237952380523815238252383523845238552386523875238852389523905239152392523935239452395523965239752398523995240052401524025240352404524055240652407524085240952410524115241252413524145241552416524175241852419524205242152422524235242452425524265242752428524295243052431524325243352434524355243652437524385243952440524415244252443524445244552446524475244852449524505245152452524535245452455524565245752458524595246052461524625246352464524655246652467524685246952470524715247252473524745247552476524775247852479524805248152482524835248452485524865248752488524895249052491524925249352494524955249652497524985249952500525015250252503525045250552506525075250852509525105251152512525135251452515525165251752518525195252052521525225252352524525255252652527525285252952530525315253252533525345253552536525375253852539525405254152542525435254452545525465254752548525495255052551525525255352554525555255652557525585255952560525615256252563525645256552566525675256852569525705257152572525735257452575525765257752578525795258052581525825258352584525855258652587525885258952590525915259252593525945259552596525975259852599526005260152602526035260452605526065260752608526095261052611526125261352614526155261652617526185261952620526215262252623526245262552626526275262852629526305263152632526335263452635526365263752638526395264052641526425264352644526455264652647526485264952650526515265252653526545265552656526575265852659526605266152662526635266452665526665266752668526695267052671526725267352674526755267652677526785267952680526815268252683526845268552686526875268852689526905269152692526935269452695526965269752698526995270052701527025270352704527055270652707527085270952710527115271252713527145271552716527175271852719527205272152722527235272452725527265272752728527295273052731527325273352734527355273652737527385273952740527415274252743527445274552746527475274852749527505275152752527535275452755527565275752758527595276052761527625276352764527655276652767527685276952770527715277252773527745277552776527775277852779527805278152782527835278452785527865278752788527895279052791527925279352794527955279652797527985279952800528015280252803528045280552806528075280852809528105281152812528135281452815528165281752818528195282052821528225282352824528255282652827528285282952830528315283252833528345283552836528375283852839528405284152842528435284452845528465284752848528495285052851528525285352854528555285652857528585285952860528615286252863528645286552866528675286852869528705287152872528735287452875528765287752878528795288052881528825288352884528855288652887528885288952890528915289252893528945289552896528975289852899529005290152902529035290452905529065290752908529095291052911529125291352914529155291652917529185291952920529215292252923529245292552926529275292852929529305293152932529335293452935529365293752938529395294052941529425294352944529455294652947529485294952950529515295252953529545295552956529575295852959529605296152962529635296452965529665296752968529695297052971529725297352974529755297652977529785297952980529815298252983529845298552986529875298852989529905299152992529935299452995529965299752998529995300053001530025300353004530055300653007530085300953010530115301253013530145301553016530175301853019530205302153022530235302453025530265302753028530295303053031530325303353034530355303653037530385303953040530415304253043530445304553046530475304853049530505305153052530535305453055530565305753058530595306053061530625306353064530655306653067530685306953070530715307253073530745307553076530775307853079530805308153082530835308453085530865308753088530895309053091530925309353094530955309653097530985309953100531015310253103531045310553106531075310853109531105311153112531135311453115531165311753118531195312053121531225312353124531255312653127531285312953130531315313253133531345313553136531375313853139531405314153142531435314453145531465314753148531495315053151531525315353154531555315653157531585315953160531615316253163531645316553166531675316853169531705317153172531735317453175531765317753178531795318053181531825318353184531855318653187531885318953190531915319253193531945319553196531975319853199532005320153202532035320453205532065320753208532095321053211532125321353214532155321653217532185321953220532215322253223532245322553226532275322853229532305323153232532335323453235532365323753238532395324053241532425324353244532455324653247532485324953250532515325253253532545325553256532575325853259532605326153262532635326453265532665326753268532695327053271532725327353274532755327653277532785327953280532815328253283532845328553286532875328853289532905329153292532935329453295532965329753298532995330053301533025330353304533055330653307533085330953310533115331253313533145331553316533175331853319533205332153322533235332453325533265332753328533295333053331533325333353334533355333653337533385333953340533415334253343533445334553346533475334853349533505335153352533535335453355533565335753358533595336053361533625336353364533655336653367533685336953370533715337253373533745337553376533775337853379533805338153382533835338453385533865338753388533895339053391533925339353394533955339653397533985339953400534015340253403534045340553406534075340853409534105341153412534135341453415534165341753418534195342053421534225342353424534255342653427534285342953430534315343253433534345343553436534375343853439534405344153442534435344453445534465344753448534495345053451534525345353454534555345653457534585345953460534615346253463534645346553466534675346853469534705347153472534735347453475534765347753478534795348053481534825348353484534855348653487534885348953490534915349253493534945349553496534975349853499535005350153502535035350453505535065350753508535095351053511535125351353514535155351653517535185351953520535215352253523535245352553526535275352853529535305353153532535335353453535535365353753538535395354053541535425354353544535455354653547535485354953550535515355253553535545355553556535575355853559535605356153562535635356453565535665356753568535695357053571535725357353574535755357653577535785357953580535815358253583535845358553586535875358853589535905359153592535935359453595535965359753598535995360053601536025360353604536055360653607536085360953610536115361253613536145361553616536175361853619536205362153622536235362453625536265362753628536295363053631536325363353634536355363653637536385363953640536415364253643536445364553646536475364853649536505365153652536535365453655536565365753658536595366053661536625366353664536655366653667536685366953670536715367253673536745367553676536775367853679536805368153682536835368453685536865368753688536895369053691536925369353694536955369653697536985369953700537015370253703537045370553706537075370853709537105371153712537135371453715537165371753718537195372053721537225372353724537255372653727537285372953730537315373253733537345373553736537375373853739537405374153742537435374453745537465374753748537495375053751537525375353754537555375653757537585375953760537615376253763537645376553766537675376853769537705377153772537735377453775537765377753778537795378053781537825378353784537855378653787537885378953790537915379253793537945379553796537975379853799538005380153802538035380453805538065380753808538095381053811538125381353814538155381653817538185381953820538215382253823538245382553826538275382853829538305383153832538335383453835538365383753838538395384053841538425384353844538455384653847538485384953850538515385253853538545385553856538575385853859538605386153862538635386453865538665386753868538695387053871538725387353874538755387653877538785387953880538815388253883538845388553886538875388853889538905389153892538935389453895538965389753898538995390053901539025390353904539055390653907539085390953910539115391253913539145391553916539175391853919539205392153922539235392453925539265392753928539295393053931539325393353934539355393653937539385393953940539415394253943539445394553946539475394853949539505395153952539535395453955539565395753958539595396053961539625396353964539655396653967539685396953970539715397253973539745397553976539775397853979539805398153982539835398453985539865398753988539895399053991539925399353994539955399653997539985399954000540015400254003540045400554006540075400854009540105401154012540135401454015540165401754018540195402054021540225402354024540255402654027540285402954030540315403254033540345403554036540375403854039540405404154042540435404454045540465404754048540495405054051540525405354054540555405654057540585405954060540615406254063540645406554066540675406854069540705407154072540735407454075540765407754078540795408054081540825408354084540855408654087540885408954090540915409254093540945409554096540975409854099541005410154102541035410454105541065410754108541095411054111541125411354114541155411654117541185411954120541215412254123541245412554126541275412854129541305413154132541335413454135541365413754138541395414054141541425414354144541455414654147541485414954150541515415254153541545415554156541575415854159541605416154162541635416454165541665416754168541695417054171541725417354174541755417654177541785417954180541815418254183541845418554186541875418854189541905419154192541935419454195541965419754198541995420054201542025420354204542055420654207542085420954210542115421254213542145421554216542175421854219542205422154222542235422454225542265422754228542295423054231542325423354234542355423654237542385423954240542415424254243542445424554246542475424854249542505425154252542535425454255542565425754258542595426054261542625426354264542655426654267542685426954270542715427254273542745427554276542775427854279542805428154282542835428454285542865428754288542895429054291542925429354294542955429654297542985429954300543015430254303543045430554306543075430854309543105431154312543135431454315543165431754318543195432054321543225432354324543255432654327543285432954330543315433254333543345433554336543375433854339543405434154342543435434454345543465434754348543495435054351543525435354354543555435654357543585435954360543615436254363543645436554366543675436854369543705437154372543735437454375543765437754378543795438054381543825438354384543855438654387543885438954390543915439254393543945439554396543975439854399544005440154402544035440454405544065440754408544095441054411544125441354414544155441654417544185441954420544215442254423544245442554426544275442854429544305443154432544335443454435544365443754438544395444054441544425444354444544455444654447544485444954450544515445254453544545445554456544575445854459544605446154462544635446454465544665446754468544695447054471544725447354474544755447654477544785447954480544815448254483544845448554486544875448854489544905449154492544935449454495544965449754498544995450054501545025450354504545055450654507545085450954510545115451254513545145451554516545175451854519545205452154522545235452454525545265452754528545295453054531545325453354534545355453654537545385453954540545415454254543545445454554546545475454854549545505455154552545535455454555545565455754558545595456054561545625456354564545655456654567545685456954570545715457254573545745457554576545775457854579545805458154582545835458454585545865458754588545895459054591545925459354594545955459654597545985459954600546015460254603546045460554606546075460854609546105461154612546135461454615546165461754618546195462054621546225462354624546255462654627546285462954630546315463254633546345463554636546375463854639546405464154642546435464454645546465464754648546495465054651546525465354654546555465654657546585465954660546615466254663546645466554666546675466854669546705467154672546735467454675546765467754678546795468054681546825468354684546855468654687546885468954690546915469254693546945469554696546975469854699547005470154702547035470454705547065470754708547095471054711547125471354714547155471654717547185471954720547215472254723547245472554726547275472854729547305473154732547335473454735547365473754738547395474054741547425474354744547455474654747547485474954750547515475254753547545475554756547575475854759547605476154762547635476454765547665476754768547695477054771547725477354774547755477654777547785477954780547815478254783547845478554786547875478854789547905479154792547935479454795547965479754798547995480054801548025480354804548055480654807548085480954810548115481254813548145481554816548175481854819548205482154822548235482454825548265482754828548295483054831548325483354834548355483654837548385483954840548415484254843548445484554846548475484854849548505485154852548535485454855548565485754858548595486054861548625486354864548655486654867548685486954870548715487254873548745487554876548775487854879548805488154882548835488454885548865488754888548895489054891548925489354894548955489654897548985489954900549015490254903549045490554906549075490854909549105491154912549135491454915549165491754918549195492054921549225492354924549255492654927549285492954930549315493254933549345493554936549375493854939549405494154942549435494454945549465494754948549495495054951549525495354954549555495654957549585495954960549615496254963549645496554966549675496854969549705497154972549735497454975549765497754978549795498054981549825498354984549855498654987549885498954990549915499254993549945499554996549975499854999550005500155002550035500455005550065500755008550095501055011550125501355014550155501655017550185501955020550215502255023550245502555026550275502855029550305503155032550335503455035550365503755038550395504055041550425504355044550455504655047550485504955050550515505255053550545505555056550575505855059550605506155062550635506455065550665506755068550695507055071550725507355074550755507655077550785507955080550815508255083550845508555086550875508855089550905509155092550935509455095550965509755098550995510055101551025510355104551055510655107551085510955110551115511255113551145511555116551175511855119551205512155122551235512455125551265512755128551295513055131551325513355134551355513655137551385513955140551415514255143551445514555146551475514855149551505515155152551535515455155551565515755158551595516055161551625516355164551655516655167551685516955170551715517255173551745517555176551775517855179551805518155182551835518455185551865518755188551895519055191551925519355194551955519655197551985519955200552015520255203552045520555206552075520855209552105521155212552135521455215552165521755218552195522055221552225522355224552255522655227552285522955230552315523255233552345523555236552375523855239552405524155242552435524455245552465524755248552495525055251552525525355254552555525655257552585525955260552615526255263552645526555266552675526855269552705527155272552735527455275552765527755278552795528055281552825528355284552855528655287552885528955290552915529255293552945529555296552975529855299553005530155302553035530455305553065530755308553095531055311553125531355314553155531655317553185531955320553215532255323553245532555326553275532855329553305533155332553335533455335553365533755338553395534055341553425534355344553455534655347553485534955350553515535255353553545535555356553575535855359553605536155362553635536455365553665536755368553695537055371553725537355374553755537655377553785537955380553815538255383553845538555386553875538855389553905539155392553935539455395553965539755398553995540055401554025540355404554055540655407554085540955410554115541255413554145541555416554175541855419554205542155422554235542455425554265542755428554295543055431554325543355434554355543655437554385543955440554415544255443554445544555446554475544855449554505545155452554535545455455554565545755458554595546055461554625546355464554655546655467554685546955470554715547255473554745547555476554775547855479554805548155482554835548455485554865548755488554895549055491554925549355494554955549655497554985549955500555015550255503555045550555506555075550855509555105551155512555135551455515555165551755518555195552055521555225552355524555255552655527555285552955530555315553255533555345553555536555375553855539555405554155542555435554455545555465554755548555495555055551555525555355554555555555655557555585555955560555615556255563555645556555566555675556855569555705557155572555735557455575555765557755578555795558055581555825558355584555855558655587555885558955590555915559255593555945559555596555975559855599556005560155602556035560455605556065560755608556095561055611556125561355614556155561655617556185561955620556215562255623556245562555626556275562855629556305563155632556335563455635556365563755638556395564055641556425564355644556455564655647556485564955650556515565255653556545565555656556575565855659556605566155662556635566455665556665566755668556695567055671556725567355674556755567655677556785567955680556815568255683556845568555686556875568855689556905569155692556935569455695556965569755698556995570055701557025570355704557055570655707557085570955710557115571255713557145571555716557175571855719557205572155722557235572455725557265572755728557295573055731557325573355734557355573655737557385573955740557415574255743557445574555746557475574855749557505575155752557535575455755557565575755758557595576055761557625576355764557655576655767557685576955770557715577255773557745577555776557775577855779557805578155782557835578455785557865578755788557895579055791557925579355794557955579655797557985579955800558015580255803558045580555806558075580855809558105581155812558135581455815558165581755818558195582055821558225582355824558255582655827558285582955830558315583255833558345583555836558375583855839558405584155842558435584455845558465584755848558495585055851558525585355854558555585655857558585585955860558615586255863558645586555866558675586855869558705587155872558735587455875558765587755878558795588055881558825588355884558855588655887558885588955890558915589255893558945589555896558975589855899559005590155902559035590455905559065590755908559095591055911559125591355914559155591655917559185591955920559215592255923559245592555926559275592855929559305593155932559335593455935559365593755938559395594055941559425594355944559455594655947559485594955950559515595255953559545595555956559575595855959559605596155962559635596455965559665596755968559695597055971559725597355974559755597655977559785597955980559815598255983559845598555986559875598855989559905599155992559935599455995559965599755998559995600056001560025600356004560055600656007560085600956010560115601256013560145601556016560175601856019560205602156022560235602456025560265602756028560295603056031560325603356034560355603656037560385603956040560415604256043560445604556046560475604856049560505605156052560535605456055560565605756058560595606056061560625606356064560655606656067560685606956070560715607256073560745607556076560775607856079560805608156082560835608456085560865608756088560895609056091560925609356094560955609656097560985609956100561015610256103561045610556106561075610856109561105611156112561135611456115561165611756118561195612056121561225612356124561255612656127561285612956130561315613256133561345613556136561375613856139561405614156142561435614456145561465614756148561495615056151561525615356154561555615656157561585615956160561615616256163561645616556166561675616856169561705617156172561735617456175561765617756178561795618056181561825618356184561855618656187561885618956190561915619256193561945619556196561975619856199562005620156202562035620456205562065620756208562095621056211562125621356214562155621656217562185621956220562215622256223562245622556226562275622856229562305623156232562335623456235562365623756238562395624056241562425624356244562455624656247562485624956250562515625256253562545625556256562575625856259562605626156262562635626456265562665626756268562695627056271562725627356274562755627656277562785627956280562815628256283562845628556286562875628856289562905629156292562935629456295562965629756298562995630056301563025630356304563055630656307563085630956310563115631256313563145631556316563175631856319563205632156322563235632456325563265632756328563295633056331563325633356334563355633656337563385633956340563415634256343563445634556346563475634856349563505635156352563535635456355563565635756358563595636056361563625636356364563655636656367563685636956370563715637256373563745637556376563775637856379563805638156382563835638456385563865638756388563895639056391563925639356394563955639656397563985639956400564015640256403564045640556406564075640856409564105641156412564135641456415564165641756418564195642056421564225642356424564255642656427564285642956430564315643256433564345643556436564375643856439564405644156442564435644456445564465644756448564495645056451564525645356454564555645656457564585645956460564615646256463564645646556466564675646856469564705647156472564735647456475564765647756478564795648056481564825648356484564855648656487564885648956490564915649256493564945649556496564975649856499565005650156502565035650456505565065650756508565095651056511565125651356514565155651656517565185651956520565215652256523565245652556526565275652856529565305653156532565335653456535565365653756538565395654056541565425654356544565455654656547565485654956550565515655256553565545655556556565575655856559565605656156562565635656456565565665656756568565695657056571565725657356574565755657656577565785657956580565815658256583565845658556586565875658856589565905659156592565935659456595565965659756598565995660056601566025660356604566055660656607566085660956610566115661256613566145661556616566175661856619566205662156622566235662456625566265662756628566295663056631566325663356634566355663656637566385663956640566415664256643566445664556646566475664856649566505665156652566535665456655566565665756658566595666056661566625666356664566655666656667566685666956670566715667256673566745667556676566775667856679566805668156682566835668456685566865668756688566895669056691566925669356694566955669656697566985669956700567015670256703567045670556706567075670856709567105671156712567135671456715567165671756718567195672056721567225672356724567255672656727567285672956730567315673256733567345673556736567375673856739567405674156742567435674456745567465674756748567495675056751567525675356754567555675656757567585675956760567615676256763567645676556766567675676856769567705677156772567735677456775567765677756778567795678056781567825678356784567855678656787567885678956790567915679256793567945679556796567975679856799568005680156802568035680456805568065680756808568095681056811568125681356814568155681656817568185681956820568215682256823568245682556826568275682856829568305683156832568335683456835568365683756838568395684056841568425684356844568455684656847568485684956850568515685256853568545685556856568575685856859568605686156862568635686456865568665686756868568695687056871568725687356874568755687656877568785687956880568815688256883568845688556886568875688856889568905689156892568935689456895568965689756898568995690056901569025690356904569055690656907569085690956910569115691256913569145691556916569175691856919569205692156922569235692456925569265692756928569295693056931569325693356934569355693656937569385693956940569415694256943569445694556946569475694856949569505695156952569535695456955569565695756958569595696056961569625696356964569655696656967569685696956970569715697256973569745697556976569775697856979569805698156982569835698456985569865698756988569895699056991569925699356994569955699656997569985699957000570015700257003570045700557006570075700857009570105701157012570135701457015570165701757018570195702057021570225702357024570255702657027570285702957030570315703257033570345703557036570375703857039570405704157042570435704457045570465704757048570495705057051570525705357054570555705657057570585705957060570615706257063570645706557066570675706857069570705707157072570735707457075570765707757078570795708057081570825708357084570855708657087570885708957090570915709257093570945709557096570975709857099571005710157102571035710457105571065710757108571095711057111571125711357114571155711657117571185711957120571215712257123571245712557126571275712857129571305713157132571335713457135571365713757138571395714057141571425714357144571455714657147571485714957150571515715257153571545715557156571575715857159571605716157162571635716457165571665716757168571695717057171571725717357174571755717657177571785717957180571815718257183571845718557186571875718857189571905719157192571935719457195571965719757198571995720057201572025720357204572055720657207572085720957210572115721257213572145721557216572175721857219572205722157222572235722457225572265722757228572295723057231572325723357234572355723657237572385723957240572415724257243572445724557246572475724857249572505725157252572535725457255572565725757258572595726057261572625726357264572655726657267572685726957270572715727257273572745727557276572775727857279572805728157282572835728457285572865728757288572895729057291572925729357294572955729657297572985729957300573015730257303573045730557306573075730857309573105731157312573135731457315573165731757318573195732057321573225732357324573255732657327573285732957330573315733257333573345733557336573375733857339573405734157342573435734457345573465734757348573495735057351573525735357354573555735657357573585735957360573615736257363573645736557366573675736857369573705737157372573735737457375573765737757378573795738057381573825738357384573855738657387573885738957390573915739257393573945739557396573975739857399574005740157402574035740457405574065740757408574095741057411574125741357414574155741657417574185741957420574215742257423574245742557426574275742857429574305743157432574335743457435574365743757438574395744057441574425744357444574455744657447574485744957450574515745257453574545745557456574575745857459574605746157462574635746457465574665746757468574695747057471574725747357474574755747657477574785747957480574815748257483574845748557486574875748857489574905749157492574935749457495574965749757498574995750057501575025750357504575055750657507575085750957510575115751257513575145751557516575175751857519575205752157522575235752457525575265752757528575295753057531575325753357534575355753657537575385753957540575415754257543575445754557546575475754857549575505755157552575535755457555575565755757558575595756057561575625756357564575655756657567575685756957570575715757257573575745757557576575775757857579575805758157582575835758457585575865758757588575895759057591575925759357594575955759657597575985759957600576015760257603576045760557606576075760857609576105761157612576135761457615576165761757618576195762057621576225762357624576255762657627576285762957630576315763257633576345763557636576375763857639576405764157642576435764457645576465764757648576495765057651576525765357654576555765657657576585765957660576615766257663576645766557666576675766857669576705767157672576735767457675576765767757678576795768057681576825768357684576855768657687576885768957690576915769257693576945769557696576975769857699577005770157702577035770457705577065770757708577095771057711577125771357714577155771657717577185771957720577215772257723577245772557726577275772857729577305773157732577335773457735577365773757738577395774057741577425774357744577455774657747577485774957750577515775257753577545775557756577575775857759577605776157762577635776457765577665776757768577695777057771577725777357774577755777657777577785777957780577815778257783577845778557786577875778857789577905779157792577935779457795577965779757798577995780057801578025780357804578055780657807578085780957810578115781257813578145781557816578175781857819578205782157822578235782457825578265782757828578295783057831578325783357834578355783657837578385783957840578415784257843578445784557846578475784857849578505785157852578535785457855578565785757858578595786057861578625786357864578655786657867578685786957870578715787257873578745787557876578775787857879578805788157882578835788457885578865788757888578895789057891578925789357894578955789657897578985789957900579015790257903579045790557906579075790857909579105791157912579135791457915579165791757918579195792057921579225792357924579255792657927579285792957930579315793257933579345793557936579375793857939579405794157942579435794457945579465794757948579495795057951579525795357954579555795657957579585795957960579615796257963579645796557966579675796857969579705797157972579735797457975579765797757978579795798057981579825798357984579855798657987579885798957990579915799257993579945799557996579975799857999580005800158002580035800458005580065800758008580095801058011580125801358014580155801658017580185801958020580215802258023580245802558026580275802858029580305803158032580335803458035580365803758038580395804058041580425804358044580455804658047580485804958050580515805258053580545805558056580575805858059580605806158062580635806458065580665806758068580695807058071580725807358074580755807658077580785807958080580815808258083580845808558086580875808858089580905809158092580935809458095580965809758098580995810058101581025810358104581055810658107581085810958110581115811258113581145811558116581175811858119581205812158122581235812458125581265812758128581295813058131581325813358134581355813658137581385813958140581415814258143581445814558146581475814858149581505815158152581535815458155581565815758158581595816058161581625816358164581655816658167581685816958170581715817258173581745817558176581775817858179581805818158182581835818458185581865818758188581895819058191581925819358194581955819658197581985819958200582015820258203582045820558206582075820858209582105821158212582135821458215582165821758218582195822058221582225822358224582255822658227582285822958230582315823258233582345823558236582375823858239582405824158242582435824458245582465824758248582495825058251582525825358254582555825658257582585825958260582615826258263582645826558266582675826858269582705827158272582735827458275582765827758278582795828058281582825828358284582855828658287582885828958290582915829258293582945829558296582975829858299583005830158302583035830458305583065830758308583095831058311583125831358314583155831658317583185831958320583215832258323583245832558326583275832858329583305833158332583335833458335583365833758338583395834058341583425834358344583455834658347583485834958350583515835258353583545835558356583575835858359583605836158362583635836458365583665836758368583695837058371583725837358374583755837658377583785837958380583815838258383583845838558386583875838858389583905839158392583935839458395583965839758398583995840058401584025840358404584055840658407584085840958410584115841258413584145841558416584175841858419584205842158422584235842458425584265842758428584295843058431584325843358434584355843658437584385843958440584415844258443584445844558446584475844858449584505845158452584535845458455584565845758458584595846058461584625846358464584655846658467584685846958470584715847258473584745847558476584775847858479584805848158482584835848458485584865848758488584895849058491584925849358494584955849658497584985849958500585015850258503585045850558506585075850858509585105851158512585135851458515585165851758518585195852058521585225852358524585255852658527585285852958530585315853258533585345853558536585375853858539585405854158542585435854458545585465854758548585495855058551585525855358554585555855658557585585855958560585615856258563585645856558566585675856858569585705857158572585735857458575585765857758578585795858058581585825858358584585855858658587585885858958590585915859258593585945859558596585975859858599586005860158602586035860458605586065860758608586095861058611586125861358614586155861658617586185861958620586215862258623586245862558626586275862858629586305863158632586335863458635586365863758638586395864058641586425864358644586455864658647586485864958650586515865258653586545865558656586575865858659586605866158662586635866458665586665866758668586695867058671586725867358674586755867658677586785867958680586815868258683586845868558686586875868858689586905869158692586935869458695586965869758698586995870058701587025870358704587055870658707587085870958710587115871258713587145871558716587175871858719587205872158722587235872458725587265872758728587295873058731587325873358734587355873658737587385873958740587415874258743587445874558746587475874858749587505875158752587535875458755587565875758758587595876058761587625876358764587655876658767587685876958770587715877258773587745877558776587775877858779587805878158782587835878458785587865878758788587895879058791587925879358794587955879658797587985879958800588015880258803588045880558806588075880858809588105881158812588135881458815588165881758818588195882058821588225882358824588255882658827588285882958830588315883258833588345883558836588375883858839588405884158842588435884458845588465884758848588495885058851588525885358854588555885658857588585885958860588615886258863588645886558866588675886858869588705887158872588735887458875588765887758878588795888058881588825888358884588855888658887588885888958890588915889258893588945889558896588975889858899589005890158902589035890458905589065890758908589095891058911589125891358914589155891658917589185891958920589215892258923589245892558926589275892858929589305893158932589335893458935589365893758938589395894058941589425894358944589455894658947589485894958950589515895258953589545895558956589575895858959589605896158962589635896458965589665896758968589695897058971589725897358974589755897658977589785897958980589815898258983589845898558986589875898858989589905899158992589935899458995589965899758998589995900059001590025900359004590055900659007590085900959010590115901259013590145901559016590175901859019590205902159022590235902459025590265902759028590295903059031590325903359034590355903659037590385903959040590415904259043590445904559046590475904859049590505905159052590535905459055590565905759058590595906059061590625906359064590655906659067590685906959070590715907259073590745907559076590775907859079590805908159082590835908459085590865908759088590895909059091590925909359094590955909659097590985909959100591015910259103591045910559106591075910859109591105911159112591135911459115591165911759118591195912059121591225912359124591255912659127591285912959130591315913259133591345913559136591375913859139591405914159142591435914459145591465914759148591495915059151591525915359154591555915659157591585915959160591615916259163591645916559166591675916859169591705917159172591735917459175591765917759178591795918059181591825918359184591855918659187591885918959190591915919259193591945919559196591975919859199592005920159202592035920459205592065920759208592095921059211592125921359214592155921659217592185921959220592215922259223592245922559226592275922859229592305923159232592335923459235592365923759238592395924059241592425924359244592455924659247592485924959250592515925259253592545925559256592575925859259592605926159262592635926459265592665926759268592695927059271592725927359274592755927659277592785927959280592815928259283592845928559286592875928859289592905929159292592935929459295592965929759298592995930059301593025930359304593055930659307593085930959310593115931259313593145931559316593175931859319593205932159322593235932459325593265932759328593295933059331593325933359334593355933659337593385933959340593415934259343593445934559346593475934859349593505935159352593535935459355593565935759358593595936059361593625936359364593655936659367593685936959370593715937259373593745937559376593775937859379593805938159382593835938459385593865938759388593895939059391593925939359394593955939659397593985939959400594015940259403594045940559406594075940859409594105941159412594135941459415594165941759418594195942059421594225942359424594255942659427594285942959430594315943259433594345943559436594375943859439594405944159442594435944459445594465944759448594495945059451594525945359454594555945659457594585945959460594615946259463594645946559466594675946859469594705947159472594735947459475594765947759478594795948059481594825948359484594855948659487594885948959490594915949259493594945949559496594975949859499595005950159502595035950459505595065950759508595095951059511595125951359514595155951659517595185951959520595215952259523595245952559526595275952859529595305953159532595335953459535595365953759538595395954059541595425954359544595455954659547595485954959550595515955259553595545955559556595575955859559595605956159562595635956459565595665956759568595695957059571595725957359574595755957659577595785957959580595815958259583595845958559586595875958859589595905959159592595935959459595595965959759598595995960059601596025960359604596055960659607596085960959610596115961259613596145961559616596175961859619596205962159622596235962459625596265962759628596295963059631596325963359634596355963659637596385963959640596415964259643596445964559646596475964859649596505965159652596535965459655596565965759658596595966059661596625966359664596655966659667596685966959670596715967259673596745967559676596775967859679596805968159682596835968459685596865968759688596895969059691596925969359694596955969659697596985969959700597015970259703597045970559706597075970859709597105971159712597135971459715597165971759718597195972059721597225972359724597255972659727597285972959730597315973259733597345973559736597375973859739597405974159742597435974459745597465974759748597495975059751597525975359754597555975659757597585975959760597615976259763597645976559766597675976859769597705977159772597735977459775597765977759778597795978059781597825978359784597855978659787597885978959790597915979259793597945979559796597975979859799598005980159802598035980459805598065980759808598095981059811598125981359814598155981659817598185981959820598215982259823598245982559826598275982859829598305983159832598335983459835598365983759838598395984059841598425984359844598455984659847598485984959850598515985259853598545985559856598575985859859598605986159862598635986459865598665986759868598695987059871598725987359874598755987659877598785987959880598815988259883598845988559886598875988859889598905989159892598935989459895598965989759898598995990059901599025990359904599055990659907599085990959910599115991259913599145991559916599175991859919599205992159922599235992459925599265992759928599295993059931599325993359934599355993659937599385993959940599415994259943599445994559946599475994859949599505995159952599535995459955599565995759958599595996059961599625996359964599655996659967599685996959970599715997259973599745997559976599775997859979599805998159982599835998459985599865998759988599895999059991599925999359994599955999659997599985999960000600016000260003600046000560006600076000860009600106001160012600136001460015600166001760018600196002060021600226002360024600256002660027600286002960030600316003260033600346003560036600376003860039600406004160042600436004460045600466004760048600496005060051600526005360054600556005660057600586005960060600616006260063600646006560066600676006860069600706007160072600736007460075600766007760078600796008060081600826008360084600856008660087600886008960090600916009260093600946009560096600976009860099601006010160102601036010460105601066010760108601096011060111601126011360114601156011660117601186011960120601216012260123601246012560126601276012860129601306013160132601336013460135601366013760138601396014060141601426014360144601456014660147601486014960150601516015260153601546015560156601576015860159601606016160162601636016460165601666016760168601696017060171601726017360174601756017660177601786017960180601816018260183601846018560186601876018860189601906019160192601936019460195601966019760198601996020060201602026020360204602056020660207602086020960210602116021260213602146021560216602176021860219602206022160222602236022460225602266022760228602296023060231602326023360234602356023660237602386023960240602416024260243602446024560246602476024860249602506025160252602536025460255602566025760258602596026060261602626026360264602656026660267602686026960270602716027260273602746027560276602776027860279602806028160282602836028460285602866028760288602896029060291602926029360294602956029660297602986029960300603016030260303603046030560306603076030860309603106031160312603136031460315603166031760318603196032060321603226032360324603256032660327603286032960330603316033260333603346033560336603376033860339603406034160342603436034460345603466034760348603496035060351603526035360354603556035660357603586035960360603616036260363603646036560366603676036860369603706037160372603736037460375603766037760378603796038060381603826038360384603856038660387603886038960390603916039260393603946039560396603976039860399604006040160402604036040460405604066040760408604096041060411604126041360414604156041660417604186041960420604216042260423604246042560426604276042860429604306043160432604336043460435604366043760438604396044060441604426044360444604456044660447604486044960450604516045260453604546045560456604576045860459604606046160462604636046460465604666046760468604696047060471604726047360474604756047660477604786047960480604816048260483604846048560486604876048860489604906049160492604936049460495604966049760498604996050060501605026050360504605056050660507605086050960510605116051260513605146051560516605176051860519605206052160522605236052460525605266052760528605296053060531605326053360534605356053660537605386053960540605416054260543605446054560546605476054860549605506055160552605536055460555605566055760558605596056060561605626056360564605656056660567605686056960570605716057260573605746057560576605776057860579605806058160582605836058460585605866058760588605896059060591605926059360594605956059660597605986059960600606016060260603606046060560606606076060860609606106061160612606136061460615606166061760618606196062060621606226062360624606256062660627606286062960630606316063260633606346063560636606376063860639606406064160642606436064460645606466064760648606496065060651606526065360654606556065660657606586065960660606616066260663606646066560666606676066860669606706067160672606736067460675606766067760678606796068060681606826068360684606856068660687606886068960690606916069260693606946069560696606976069860699607006070160702607036070460705607066070760708607096071060711607126071360714607156071660717607186071960720607216072260723607246072560726607276072860729607306073160732607336073460735607366073760738607396074060741607426074360744607456074660747607486074960750607516075260753607546075560756607576075860759607606076160762607636076460765607666076760768607696077060771607726077360774607756077660777607786077960780607816078260783607846078560786607876078860789607906079160792607936079460795607966079760798607996080060801608026080360804608056080660807608086080960810608116081260813608146081560816608176081860819608206082160822608236082460825608266082760828608296083060831608326083360834608356083660837608386083960840608416084260843608446084560846608476084860849608506085160852608536085460855608566085760858608596086060861608626086360864608656086660867608686086960870608716087260873608746087560876608776087860879608806088160882608836088460885608866088760888608896089060891608926089360894608956089660897608986089960900609016090260903609046090560906609076090860909609106091160912609136091460915609166091760918609196092060921609226092360924609256092660927609286092960930609316093260933609346093560936609376093860939609406094160942609436094460945609466094760948609496095060951609526095360954609556095660957609586095960960609616096260963609646096560966609676096860969609706097160972609736097460975609766097760978609796098060981609826098360984609856098660987609886098960990609916099260993609946099560996609976099860999610006100161002610036100461005610066100761008610096101061011610126101361014610156101661017610186101961020610216102261023610246102561026610276102861029610306103161032610336103461035610366103761038610396104061041610426104361044610456104661047610486104961050610516105261053610546105561056610576105861059610606106161062610636106461065610666106761068610696107061071610726107361074610756107661077610786107961080610816108261083610846108561086610876108861089610906109161092610936109461095610966109761098610996110061101611026110361104611056110661107611086110961110611116111261113611146111561116611176111861119611206112161122611236112461125611266112761128611296113061131611326113361134611356113661137611386113961140611416114261143611446114561146611476114861149611506115161152611536115461155611566115761158611596116061161611626116361164611656116661167611686116961170611716117261173611746117561176611776117861179611806118161182611836118461185611866118761188611896119061191611926119361194611956119661197611986119961200612016120261203612046120561206612076120861209612106121161212612136121461215612166121761218612196122061221612226122361224612256122661227612286122961230612316123261233612346123561236612376123861239612406124161242612436124461245612466124761248612496125061251612526125361254612556125661257612586125961260612616126261263612646126561266612676126861269612706127161272612736127461275612766127761278612796128061281612826128361284612856128661287612886128961290612916129261293612946129561296612976129861299613006130161302613036130461305613066130761308613096131061311613126131361314613156131661317613186131961320613216132261323613246132561326613276132861329613306133161332613336133461335613366133761338613396134061341613426134361344613456134661347613486134961350613516135261353613546135561356613576135861359613606136161362613636136461365613666136761368613696137061371613726137361374613756137661377613786137961380613816138261383613846138561386613876138861389613906139161392613936139461395613966139761398613996140061401614026140361404614056140661407614086140961410614116141261413614146141561416614176141861419614206142161422614236142461425614266142761428614296143061431614326143361434614356143661437614386143961440614416144261443614446144561446614476144861449614506145161452614536145461455614566145761458614596146061461614626146361464614656146661467614686146961470614716147261473614746147561476614776147861479614806148161482614836148461485614866148761488614896149061491614926149361494614956149661497614986149961500615016150261503615046150561506615076150861509615106151161512615136151461515615166151761518615196152061521615226152361524615256152661527615286152961530615316153261533615346153561536615376153861539615406154161542615436154461545615466154761548615496155061551615526155361554615556155661557615586155961560615616156261563615646156561566615676156861569615706157161572615736157461575615766157761578615796158061581615826158361584615856158661587615886158961590615916159261593615946159561596615976159861599616006160161602616036160461605616066160761608616096161061611616126161361614616156161661617616186161961620616216162261623616246162561626616276162861629616306163161632616336163461635616366163761638616396164061641616426164361644616456164661647616486164961650616516165261653616546165561656616576165861659616606166161662616636166461665616666166761668616696167061671616726167361674616756167661677616786167961680616816168261683616846168561686616876168861689616906169161692616936169461695616966169761698616996170061701617026170361704617056170661707617086170961710617116171261713617146171561716617176171861719617206172161722617236172461725617266172761728617296173061731617326173361734617356173661737617386173961740617416174261743617446174561746617476174861749617506175161752617536175461755617566175761758617596176061761617626176361764617656176661767617686176961770617716177261773617746177561776617776177861779617806178161782617836178461785617866178761788617896179061791617926179361794617956179661797617986179961800618016180261803618046180561806618076180861809618106181161812618136181461815618166181761818618196182061821618226182361824618256182661827618286182961830618316183261833618346183561836618376183861839618406184161842618436184461845618466184761848618496185061851618526185361854618556185661857618586185961860618616186261863618646186561866618676186861869618706187161872618736187461875618766187761878618796188061881618826188361884618856188661887618886188961890618916189261893618946189561896618976189861899619006190161902619036190461905619066190761908619096191061911619126191361914619156191661917619186191961920619216192261923619246192561926619276192861929619306193161932619336193461935619366193761938619396194061941619426194361944619456194661947619486194961950619516195261953619546195561956619576195861959619606196161962619636196461965619666196761968619696197061971619726197361974619756197661977619786197961980619816198261983619846198561986619876198861989619906199161992619936199461995619966199761998619996200062001620026200362004620056200662007620086200962010620116201262013620146201562016620176201862019620206202162022620236202462025620266202762028620296203062031620326203362034620356203662037620386203962040620416204262043620446204562046620476204862049620506205162052620536205462055620566205762058620596206062061620626206362064620656206662067620686206962070620716207262073620746207562076620776207862079620806208162082620836208462085620866208762088620896209062091620926209362094620956209662097620986209962100621016210262103621046210562106621076210862109621106211162112621136211462115621166211762118621196212062121621226212362124621256212662127621286212962130621316213262133621346213562136621376213862139621406214162142621436214462145621466214762148621496215062151621526215362154621556215662157621586215962160621616216262163621646216562166621676216862169621706217162172621736217462175621766217762178621796218062181621826218362184621856218662187621886218962190621916219262193621946219562196621976219862199622006220162202622036220462205622066220762208622096221062211622126221362214622156221662217622186221962220622216222262223622246222562226622276222862229622306223162232622336223462235622366223762238622396224062241622426224362244622456224662247622486224962250622516225262253622546225562256622576225862259622606226162262622636226462265622666226762268622696227062271622726227362274622756227662277622786227962280622816228262283622846228562286622876228862289622906229162292622936229462295622966229762298622996230062301623026230362304623056230662307623086230962310623116231262313623146231562316623176231862319623206232162322623236232462325623266232762328623296233062331623326233362334623356233662337623386233962340623416234262343623446234562346623476234862349623506235162352623536235462355623566235762358623596236062361623626236362364623656236662367623686236962370623716237262373623746237562376623776237862379623806238162382623836238462385623866238762388623896239062391623926239362394623956239662397623986239962400624016240262403624046240562406624076240862409624106241162412624136241462415624166241762418624196242062421624226242362424624256242662427624286242962430624316243262433624346243562436624376243862439624406244162442624436244462445624466244762448624496245062451624526245362454624556245662457624586245962460624616246262463624646246562466624676246862469624706247162472624736247462475624766247762478624796248062481624826248362484624856248662487624886248962490624916249262493624946249562496624976249862499625006250162502625036250462505625066250762508625096251062511625126251362514625156251662517625186251962520625216252262523625246252562526625276252862529625306253162532625336253462535625366253762538625396254062541625426254362544625456254662547625486254962550625516255262553625546255562556625576255862559625606256162562625636256462565625666256762568625696257062571625726257362574625756257662577625786257962580625816258262583625846258562586625876258862589625906259162592625936259462595625966259762598625996260062601626026260362604626056260662607626086260962610626116261262613626146261562616626176261862619626206262162622626236262462625626266262762628626296263062631626326263362634626356263662637626386263962640626416264262643626446264562646626476264862649626506265162652626536265462655626566265762658626596266062661626626266362664626656266662667626686266962670626716267262673626746267562676626776267862679626806268162682626836268462685626866268762688626896269062691626926269362694626956269662697626986269962700627016270262703627046270562706627076270862709627106271162712627136271462715627166271762718627196272062721627226272362724627256272662727627286272962730627316273262733627346273562736627376273862739627406274162742627436274462745627466274762748627496275062751627526275362754627556275662757627586275962760627616276262763627646276562766627676276862769627706277162772627736277462775627766277762778627796278062781627826278362784627856278662787627886278962790627916279262793627946279562796627976279862799628006280162802628036280462805628066280762808628096281062811628126281362814628156281662817628186281962820628216282262823628246282562826628276282862829628306283162832628336283462835628366283762838628396284062841628426284362844628456284662847628486284962850628516285262853628546285562856628576285862859628606286162862628636286462865628666286762868628696287062871628726287362874628756287662877628786287962880628816288262883628846288562886628876288862889628906289162892628936289462895628966289762898628996290062901629026290362904629056290662907629086290962910629116291262913629146291562916629176291862919629206292162922629236292462925629266292762928629296293062931629326293362934629356293662937629386293962940629416294262943629446294562946629476294862949629506295162952629536295462955629566295762958629596296062961629626296362964629656296662967629686296962970629716297262973629746297562976629776297862979629806298162982629836298462985629866298762988629896299062991629926299362994629956299662997629986299963000630016300263003630046300563006630076300863009630106301163012630136301463015630166301763018630196302063021630226302363024630256302663027630286302963030630316303263033630346303563036630376303863039630406304163042630436304463045630466304763048630496305063051630526305363054630556305663057630586305963060630616306263063630646306563066630676306863069630706307163072630736307463075630766307763078630796308063081630826308363084630856308663087630886308963090630916309263093630946309563096630976309863099631006310163102631036310463105631066310763108631096311063111631126311363114631156311663117631186311963120631216312263123631246312563126631276312863129631306313163132631336313463135631366313763138631396314063141631426314363144631456314663147631486314963150631516315263153631546315563156631576315863159631606316163162631636316463165631666316763168631696317063171631726317363174631756317663177631786317963180631816318263183631846318563186631876318863189631906319163192631936319463195631966319763198631996320063201632026320363204632056320663207632086320963210632116321263213632146321563216632176321863219632206322163222632236322463225632266322763228632296323063231632326323363234632356323663237632386323963240632416324263243632446324563246632476324863249632506325163252632536325463255632566325763258632596326063261632626326363264632656326663267632686326963270632716327263273632746327563276632776327863279632806328163282632836328463285632866328763288632896329063291632926329363294632956329663297632986329963300633016330263303633046330563306633076330863309633106331163312633136331463315633166331763318633196332063321633226332363324633256332663327633286332963330633316333263333633346333563336633376333863339633406334163342633436334463345633466334763348633496335063351633526335363354633556335663357633586335963360633616336263363633646336563366633676336863369633706337163372633736337463375633766337763378633796338063381633826338363384633856338663387633886338963390633916339263393633946339563396633976339863399634006340163402634036340463405634066340763408634096341063411634126341363414634156341663417634186341963420634216342263423634246342563426634276342863429634306343163432634336343463435634366343763438634396344063441634426344363444634456344663447634486344963450634516345263453634546345563456634576345863459634606346163462634636346463465634666346763468634696347063471634726347363474634756347663477634786347963480634816348263483634846348563486634876348863489634906349163492634936349463495634966349763498634996350063501635026350363504635056350663507635086350963510635116351263513635146351563516635176351863519635206352163522635236352463525635266352763528635296353063531635326353363534635356353663537635386353963540635416354263543635446354563546635476354863549635506355163552635536355463555635566355763558635596356063561635626356363564635656356663567635686356963570635716357263573635746357563576635776357863579635806358163582635836358463585635866358763588635896359063591635926359363594635956359663597635986359963600636016360263603636046360563606636076360863609636106361163612636136361463615636166361763618636196362063621636226362363624636256362663627636286362963630636316363263633636346363563636636376363863639636406364163642636436364463645636466364763648636496365063651636526365363654636556365663657636586365963660636616366263663636646366563666636676366863669636706367163672636736367463675636766367763678636796368063681636826368363684636856368663687636886368963690636916369263693636946369563696636976369863699637006370163702637036370463705637066370763708637096371063711637126371363714637156371663717637186371963720637216372263723637246372563726637276372863729637306373163732637336373463735637366373763738637396374063741637426374363744637456374663747637486374963750637516375263753637546375563756637576375863759637606376163762637636376463765637666376763768637696377063771637726377363774637756377663777637786377963780637816378263783637846378563786637876378863789637906379163792637936379463795637966379763798637996380063801638026380363804638056380663807638086380963810638116381263813638146381563816638176381863819638206382163822638236382463825638266382763828638296383063831638326383363834638356383663837638386383963840638416384263843638446384563846638476384863849638506385163852638536385463855638566385763858638596386063861638626386363864638656386663867638686386963870638716387263873638746387563876638776387863879638806388163882638836388463885638866388763888638896389063891638926389363894638956389663897638986389963900639016390263903639046390563906639076390863909639106391163912639136391463915639166391763918639196392063921639226392363924639256392663927639286392963930639316393263933639346393563936639376393863939639406394163942639436394463945639466394763948639496395063951639526395363954639556395663957639586395963960639616396263963639646396563966639676396863969639706397163972639736397463975639766397763978639796398063981639826398363984639856398663987639886398963990639916399263993639946399563996639976399863999640006400164002640036400464005640066400764008640096401064011640126401364014640156401664017640186401964020640216402264023640246402564026640276402864029640306403164032640336403464035640366403764038640396404064041640426404364044640456404664047640486404964050640516405264053640546405564056640576405864059640606406164062640636406464065640666406764068640696407064071640726407364074640756407664077640786407964080640816408264083640846408564086640876408864089640906409164092640936409464095640966409764098640996410064101641026410364104641056410664107641086410964110641116411264113641146411564116641176411864119641206412164122641236412464125641266412764128641296413064131641326413364134641356413664137641386413964140641416414264143641446414564146641476414864149641506415164152641536415464155641566415764158641596416064161641626416364164641656416664167641686416964170641716417264173641746417564176641776417864179641806418164182641836418464185641866418764188641896419064191641926419364194641956419664197641986419964200642016420264203642046420564206642076420864209642106421164212642136421464215642166421764218642196422064221642226422364224642256422664227642286422964230642316423264233642346423564236642376423864239642406424164242642436424464245642466424764248642496425064251642526425364254642556425664257642586425964260642616426264263642646426564266642676426864269642706427164272642736427464275642766427764278642796428064281642826428364284642856428664287642886428964290642916429264293642946429564296642976429864299643006430164302643036430464305643066430764308643096431064311643126431364314643156431664317643186431964320643216432264323643246432564326643276432864329643306433164332643336433464335643366433764338643396434064341643426434364344643456434664347643486434964350643516435264353643546435564356643576435864359643606436164362643636436464365643666436764368643696437064371643726437364374643756437664377643786437964380643816438264383643846438564386643876438864389643906439164392643936439464395643966439764398643996440064401644026440364404644056440664407644086440964410644116441264413644146441564416644176441864419644206442164422644236442464425644266442764428644296443064431644326443364434644356443664437644386443964440644416444264443644446444564446644476444864449644506445164452644536445464455644566445764458644596446064461644626446364464644656446664467644686446964470644716447264473644746447564476644776447864479644806448164482644836448464485644866448764488644896449064491644926449364494644956449664497644986449964500645016450264503645046450564506645076450864509645106451164512645136451464515645166451764518645196452064521645226452364524645256452664527645286452964530645316453264533645346453564536645376453864539645406454164542645436454464545645466454764548645496455064551645526455364554645556455664557645586455964560645616456264563645646456564566645676456864569645706457164572645736457464575645766457764578645796458064581645826458364584645856458664587645886458964590645916459264593645946459564596645976459864599646006460164602646036460464605646066460764608646096461064611646126461364614646156461664617646186461964620646216462264623646246462564626646276462864629646306463164632646336463464635646366463764638646396464064641646426464364644646456464664647646486464964650646516465264653646546465564656646576465864659646606466164662646636466464665646666466764668646696467064671646726467364674646756467664677646786467964680646816468264683646846468564686646876468864689646906469164692646936469464695646966469764698646996470064701647026470364704647056470664707647086470964710647116471264713647146471564716647176471864719647206472164722647236472464725647266472764728647296473064731647326473364734647356473664737647386473964740647416474264743647446474564746647476474864749647506475164752647536475464755647566475764758647596476064761647626476364764647656476664767647686476964770647716477264773647746477564776647776477864779647806478164782647836478464785647866478764788647896479064791647926479364794647956479664797647986479964800648016480264803648046480564806648076480864809648106481164812648136481464815648166481764818648196482064821648226482364824648256482664827648286482964830648316483264833648346483564836648376483864839648406484164842648436484464845648466484764848648496485064851648526485364854648556485664857648586485964860648616486264863648646486564866648676486864869648706487164872648736487464875648766487764878648796488064881648826488364884648856488664887648886488964890648916489264893648946489564896648976489864899649006490164902649036490464905649066490764908649096491064911649126491364914649156491664917649186491964920649216492264923649246492564926649276492864929649306493164932649336493464935649366493764938649396494064941649426494364944649456494664947649486494964950649516495264953649546495564956649576495864959649606496164962649636496464965649666496764968649696497064971649726497364974649756497664977649786497964980649816498264983649846498564986649876498864989649906499164992649936499464995649966499764998649996500065001650026500365004650056500665007650086500965010650116501265013650146501565016650176501865019650206502165022650236502465025650266502765028650296503065031650326503365034650356503665037650386503965040650416504265043650446504565046650476504865049650506505165052650536505465055650566505765058650596506065061650626506365064650656506665067650686506965070650716507265073650746507565076650776507865079650806508165082650836508465085650866508765088650896509065091650926509365094650956509665097650986509965100651016510265103651046510565106651076510865109651106511165112651136511465115651166511765118651196512065121651226512365124651256512665127651286512965130651316513265133651346513565136651376513865139651406514165142651436514465145651466514765148651496515065151651526515365154651556515665157651586515965160651616516265163651646516565166651676516865169651706517165172651736517465175651766517765178651796518065181651826518365184651856518665187651886518965190651916519265193651946519565196651976519865199652006520165202652036520465205652066520765208652096521065211652126521365214652156521665217652186521965220652216522265223652246522565226652276522865229652306523165232652336523465235652366523765238652396524065241652426524365244652456524665247652486524965250652516525265253652546525565256652576525865259652606526165262652636526465265652666526765268652696527065271652726527365274652756527665277652786527965280652816528265283652846528565286652876528865289652906529165292652936529465295652966529765298652996530065301653026530365304653056530665307653086530965310653116531265313653146531565316653176531865319653206532165322653236532465325653266532765328653296533065331653326533365334653356533665337653386533965340653416534265343653446534565346653476534865349653506535165352653536535465355653566535765358653596536065361653626536365364653656536665367653686536965370653716537265373653746537565376653776537865379653806538165382653836538465385653866538765388653896539065391653926539365394653956539665397653986539965400654016540265403654046540565406654076540865409654106541165412654136541465415654166541765418654196542065421654226542365424654256542665427654286542965430654316543265433654346543565436654376543865439654406544165442654436544465445654466544765448654496545065451654526545365454654556545665457654586545965460654616546265463654646546565466654676546865469654706547165472654736547465475654766547765478654796548065481654826548365484654856548665487654886548965490654916549265493654946549565496654976549865499655006550165502655036550465505655066550765508655096551065511655126551365514655156551665517655186551965520655216552265523655246552565526655276552865529655306553165532655336553465535655366553765538655396554065541655426554365544655456554665547655486554965550655516555265553655546555565556655576555865559655606556165562655636556465565655666556765568655696557065571655726557365574655756557665577655786557965580655816558265583655846558565586655876558865589655906559165592655936559465595655966559765598655996560065601656026560365604656056560665607656086560965610656116561265613656146561565616656176561865619656206562165622656236562465625656266562765628656296563065631656326563365634656356563665637656386563965640656416564265643656446564565646656476564865649656506565165652656536565465655656566565765658656596566065661656626566365664656656566665667656686566965670656716567265673656746567565676656776567865679656806568165682656836568465685656866568765688656896569065691656926569365694656956569665697656986569965700657016570265703657046570565706657076570865709657106571165712657136571465715657166571765718657196572065721657226572365724657256572665727657286572965730657316573265733657346573565736657376573865739657406574165742657436574465745657466574765748657496575065751657526575365754657556575665757657586575965760657616576265763657646576565766657676576865769657706577165772657736577465775657766577765778657796578065781657826578365784657856578665787657886578965790657916579265793657946579565796657976579865799658006580165802658036580465805658066580765808658096581065811658126581365814658156581665817658186581965820658216582265823658246582565826658276582865829658306583165832658336583465835658366583765838658396584065841658426584365844658456584665847658486584965850658516585265853658546585565856658576585865859658606586165862658636586465865658666586765868658696587065871658726587365874658756587665877658786587965880658816588265883658846588565886658876588865889658906589165892658936589465895658966589765898658996590065901659026590365904659056590665907659086590965910659116591265913659146591565916659176591865919659206592165922659236592465925659266592765928659296593065931659326593365934659356593665937659386593965940659416594265943659446594565946659476594865949659506595165952659536595465955659566595765958659596596065961659626596365964659656596665967659686596965970659716597265973659746597565976659776597865979659806598165982659836598465985659866598765988659896599065991659926599365994659956599665997659986599966000660016600266003660046600566006660076600866009660106601166012660136601466015660166601766018660196602066021660226602366024660256602666027660286602966030660316603266033660346603566036660376603866039660406604166042660436604466045660466604766048660496605066051660526605366054660556605666057660586605966060660616606266063660646606566066660676606866069660706607166072660736607466075660766607766078660796608066081660826608366084660856608666087660886608966090660916609266093660946609566096660976609866099661006610166102661036610466105661066610766108661096611066111661126611366114661156611666117661186611966120661216612266123661246612566126661276612866129661306613166132661336613466135661366613766138661396614066141661426614366144661456614666147661486614966150661516615266153661546615566156661576615866159661606616166162661636616466165661666616766168661696617066171661726617366174661756617666177661786617966180661816618266183661846618566186661876618866189661906619166192661936619466195661966619766198661996620066201662026620366204662056620666207662086620966210662116621266213662146621566216662176621866219662206622166222662236622466225662266622766228662296623066231662326623366234662356623666237662386623966240662416624266243662446624566246662476624866249662506625166252662536625466255662566625766258662596626066261662626626366264662656626666267662686626966270662716627266273662746627566276662776627866279662806628166282662836628466285662866628766288662896629066291662926629366294662956629666297662986629966300663016630266303663046630566306663076630866309663106631166312663136631466315663166631766318663196632066321663226632366324663256632666327663286632966330663316633266333663346633566336663376633866339663406634166342663436634466345663466634766348663496635066351663526635366354663556635666357663586635966360663616636266363663646636566366663676636866369663706637166372663736637466375663766637766378663796638066381663826638366384663856638666387663886638966390663916639266393663946639566396663976639866399664006640166402664036640466405664066640766408664096641066411664126641366414664156641666417664186641966420664216642266423664246642566426664276642866429664306643166432664336643466435664366643766438664396644066441664426644366444664456644666447664486644966450664516645266453664546645566456664576645866459664606646166462664636646466465664666646766468664696647066471664726647366474664756647666477664786647966480664816648266483664846648566486664876648866489664906649166492664936649466495664966649766498664996650066501665026650366504665056650666507665086650966510665116651266513665146651566516665176651866519665206652166522665236652466525665266652766528665296653066531665326653366534665356653666537665386653966540665416654266543665446654566546665476654866549665506655166552665536655466555665566655766558665596656066561665626656366564665656656666567665686656966570665716657266573665746657566576665776657866579665806658166582665836658466585665866658766588665896659066591665926659366594665956659666597665986659966600666016660266603666046660566606666076660866609666106661166612666136661466615666166661766618666196662066621666226662366624666256662666627666286662966630666316663266633666346663566636666376663866639666406664166642666436664466645666466664766648666496665066651666526665366654666556665666657666586665966660666616666266663666646666566666666676666866669666706667166672666736667466675666766667766678666796668066681666826668366684666856668666687666886668966690666916669266693666946669566696666976669866699667006670166702667036670466705667066670766708667096671066711667126671366714667156671666717667186671966720667216672266723667246672566726667276672866729667306673166732667336673466735667366673766738667396674066741667426674366744667456674666747667486674966750667516675266753667546675566756667576675866759667606676166762667636676466765667666676766768667696677066771667726677366774667756677666777667786677966780667816678266783667846678566786667876678866789667906679166792667936679466795667966679766798667996680066801668026680366804668056680666807668086680966810668116681266813668146681566816668176681866819668206682166822668236682466825668266682766828668296683066831668326683366834668356683666837668386683966840668416684266843668446684566846668476684866849668506685166852668536685466855668566685766858668596686066861668626686366864668656686666867668686686966870668716687266873668746687566876668776687866879668806688166882668836688466885668866688766888668896689066891668926689366894668956689666897668986689966900669016690266903669046690566906669076690866909669106691166912669136691466915669166691766918669196692066921669226692366924669256692666927669286692966930669316693266933669346693566936669376693866939669406694166942669436694466945669466694766948669496695066951669526695366954669556695666957669586695966960669616696266963669646696566966669676696866969669706697166972669736697466975669766697766978669796698066981669826698366984669856698666987669886698966990669916699266993669946699566996669976699866999670006700167002670036700467005670066700767008670096701067011670126701367014670156701667017670186701967020670216702267023670246702567026670276702867029670306703167032670336703467035670366703767038670396704067041670426704367044670456704667047670486704967050670516705267053670546705567056670576705867059670606706167062670636706467065670666706767068670696707067071670726707367074670756707667077670786707967080670816708267083670846708567086670876708867089670906709167092670936709467095670966709767098670996710067101671026710367104671056710667107671086710967110671116711267113671146711567116671176711867119671206712167122671236712467125671266712767128671296713067131671326713367134671356713667137671386713967140671416714267143671446714567146671476714867149671506715167152671536715467155671566715767158671596716067161671626716367164671656716667167671686716967170671716717267173671746717567176671776717867179671806718167182671836718467185671866718767188671896719067191671926719367194671956719667197671986719967200672016720267203672046720567206672076720867209672106721167212672136721467215672166721767218672196722067221672226722367224672256722667227672286722967230672316723267233672346723567236672376723867239672406724167242672436724467245672466724767248672496725067251672526725367254672556725667257672586725967260672616726267263672646726567266672676726867269672706727167272672736727467275672766727767278672796728067281672826728367284672856728667287672886728967290672916729267293672946729567296672976729867299673006730167302673036730467305673066730767308673096731067311673126731367314673156731667317673186731967320673216732267323673246732567326673276732867329673306733167332673336733467335673366733767338673396734067341673426734367344673456734667347673486734967350673516735267353673546735567356673576735867359673606736167362673636736467365673666736767368673696737067371673726737367374673756737667377673786737967380673816738267383673846738567386673876738867389673906739167392673936739467395673966739767398673996740067401674026740367404674056740667407674086740967410674116741267413674146741567416674176741867419674206742167422674236742467425674266742767428674296743067431674326743367434674356743667437674386743967440674416744267443674446744567446674476744867449674506745167452674536745467455674566745767458674596746067461674626746367464674656746667467674686746967470674716747267473674746747567476674776747867479674806748167482674836748467485674866748767488674896749067491674926749367494674956749667497674986749967500675016750267503675046750567506675076750867509675106751167512675136751467515675166751767518675196752067521675226752367524675256752667527675286752967530675316753267533675346753567536675376753867539675406754167542675436754467545675466754767548675496755067551675526755367554675556755667557675586755967560675616756267563675646756567566675676756867569675706757167572675736757467575675766757767578675796758067581675826758367584675856758667587675886758967590675916759267593675946759567596675976759867599676006760167602676036760467605676066760767608676096761067611676126761367614676156761667617676186761967620676216762267623676246762567626676276762867629676306763167632676336763467635676366763767638676396764067641676426764367644676456764667647676486764967650676516765267653676546765567656676576765867659676606766167662676636766467665676666766767668676696767067671676726767367674676756767667677676786767967680676816768267683676846768567686676876768867689676906769167692676936769467695676966769767698676996770067701677026770367704677056770667707677086770967710677116771267713677146771567716677176771867719677206772167722677236772467725677266772767728677296773067731677326773367734677356773667737677386773967740677416774267743677446774567746677476774867749677506775167752677536775467755677566775767758677596776067761677626776367764677656776667767677686776967770677716777267773677746777567776677776777867779677806778167782677836778467785677866778767788677896779067791677926779367794677956779667797677986779967800678016780267803678046780567806678076780867809678106781167812678136781467815678166781767818678196782067821678226782367824678256782667827678286782967830678316783267833678346783567836678376783867839678406784167842678436784467845678466784767848678496785067851678526785367854678556785667857678586785967860678616786267863678646786567866678676786867869678706787167872678736787467875678766787767878678796788067881678826788367884678856788667887678886788967890678916789267893678946789567896678976789867899679006790167902679036790467905679066790767908679096791067911679126791367914679156791667917679186791967920679216792267923679246792567926679276792867929679306793167932679336793467935679366793767938679396794067941679426794367944679456794667947679486794967950679516795267953679546795567956679576795867959679606796167962679636796467965679666796767968679696797067971679726797367974679756797667977679786797967980679816798267983679846798567986679876798867989679906799167992679936799467995679966799767998679996800068001680026800368004680056800668007680086800968010680116801268013680146801568016680176801868019680206802168022680236802468025680266802768028680296803068031680326803368034680356803668037680386803968040680416804268043680446804568046680476804868049680506805168052680536805468055680566805768058680596806068061680626806368064680656806668067680686806968070680716807268073680746807568076680776807868079680806808168082680836808468085680866808768088680896809068091680926809368094680956809668097680986809968100681016810268103681046810568106681076810868109681106811168112681136811468115681166811768118681196812068121681226812368124681256812668127681286812968130681316813268133681346813568136681376813868139681406814168142681436814468145681466814768148681496815068151681526815368154681556815668157681586815968160681616816268163681646816568166681676816868169681706817168172681736817468175681766817768178681796818068181681826818368184681856818668187681886818968190681916819268193681946819568196681976819868199682006820168202682036820468205682066820768208682096821068211682126821368214682156821668217682186821968220682216822268223682246822568226682276822868229682306823168232682336823468235682366823768238682396824068241682426824368244682456824668247682486824968250682516825268253682546825568256682576825868259682606826168262682636826468265682666826768268682696827068271682726827368274682756827668277682786827968280682816828268283682846828568286682876828868289682906829168292682936829468295682966829768298682996830068301683026830368304683056830668307683086830968310683116831268313683146831568316683176831868319683206832168322683236832468325683266832768328683296833068331683326833368334683356833668337683386833968340683416834268343683446834568346683476834868349683506835168352683536835468355683566835768358683596836068361683626836368364683656836668367683686836968370683716837268373683746837568376683776837868379683806838168382683836838468385683866838768388683896839068391683926839368394683956839668397683986839968400684016840268403684046840568406684076840868409684106841168412684136841468415684166841768418684196842068421684226842368424684256842668427684286842968430684316843268433684346843568436684376843868439684406844168442684436844468445684466844768448684496845068451684526845368454684556845668457684586845968460684616846268463684646846568466684676846868469684706847168472684736847468475684766847768478684796848068481684826848368484684856848668487684886848968490684916849268493684946849568496684976849868499685006850168502685036850468505685066850768508685096851068511685126851368514685156851668517685186851968520685216852268523685246852568526685276852868529685306853168532685336853468535685366853768538685396854068541685426854368544685456854668547685486854968550685516855268553685546855568556685576855868559685606856168562685636856468565685666856768568685696857068571685726857368574685756857668577685786857968580685816858268583685846858568586685876858868589685906859168592685936859468595685966859768598685996860068601686026860368604686056860668607686086860968610686116861268613686146861568616686176861868619686206862168622686236862468625686266862768628686296863068631686326863368634686356863668637686386863968640686416864268643686446864568646686476864868649686506865168652686536865468655686566865768658686596866068661686626866368664686656866668667686686866968670686716867268673686746867568676686776867868679686806868168682686836868468685686866868768688686896869068691686926869368694686956869668697686986869968700687016870268703687046870568706687076870868709687106871168712687136871468715687166871768718687196872068721687226872368724687256872668727687286872968730687316873268733687346873568736687376873868739687406874168742687436874468745687466874768748687496875068751687526875368754687556875668757687586875968760687616876268763687646876568766687676876868769687706877168772687736877468775687766877768778687796878068781687826878368784687856878668787687886878968790687916879268793687946879568796687976879868799688006880168802688036880468805688066880768808688096881068811688126881368814688156881668817688186881968820688216882268823688246882568826688276882868829688306883168832688336883468835688366883768838688396884068841688426884368844688456884668847688486884968850688516885268853688546885568856688576885868859688606886168862688636886468865688666886768868688696887068871688726887368874688756887668877688786887968880688816888268883688846888568886688876888868889688906889168892688936889468895688966889768898688996890068901689026890368904689056890668907689086890968910689116891268913689146891568916689176891868919689206892168922689236892468925689266892768928689296893068931689326893368934689356893668937689386893968940689416894268943689446894568946689476894868949689506895168952689536895468955689566895768958689596896068961689626896368964689656896668967689686896968970689716897268973689746897568976689776897868979689806898168982689836898468985689866898768988689896899068991689926899368994689956899668997689986899969000690016900269003690046900569006690076900869009690106901169012690136901469015690166901769018690196902069021690226902369024690256902669027690286902969030690316903269033690346903569036690376903869039690406904169042690436904469045690466904769048690496905069051690526905369054690556905669057690586905969060690616906269063690646906569066690676906869069690706907169072690736907469075690766907769078690796908069081690826908369084690856908669087690886908969090690916909269093690946909569096690976909869099691006910169102691036910469105691066910769108691096911069111691126911369114691156911669117691186911969120691216912269123691246912569126691276912869129691306913169132691336913469135691366913769138691396914069141691426914369144691456914669147691486914969150691516915269153691546915569156691576915869159691606916169162691636916469165691666916769168691696917069171691726917369174691756917669177691786917969180691816918269183691846918569186691876918869189691906919169192691936919469195691966919769198691996920069201692026920369204692056920669207692086920969210692116921269213692146921569216692176921869219692206922169222692236922469225692266922769228692296923069231692326923369234692356923669237692386923969240692416924269243692446924569246692476924869249692506925169252692536925469255692566925769258692596926069261692626926369264692656926669267692686926969270692716927269273692746927569276692776927869279692806928169282692836928469285692866928769288692896929069291692926929369294692956929669297692986929969300693016930269303693046930569306693076930869309693106931169312693136931469315693166931769318693196932069321693226932369324693256932669327693286932969330693316933269333693346933569336693376933869339693406934169342693436934469345693466934769348693496935069351693526935369354693556935669357693586935969360693616936269363693646936569366693676936869369693706937169372693736937469375693766937769378693796938069381693826938369384693856938669387693886938969390693916939269393693946939569396693976939869399694006940169402694036940469405694066940769408694096941069411694126941369414694156941669417694186941969420694216942269423694246942569426694276942869429694306943169432694336943469435694366943769438694396944069441694426944369444694456944669447694486944969450694516945269453694546945569456694576945869459694606946169462694636946469465694666946769468694696947069471694726947369474694756947669477694786947969480694816948269483694846948569486694876948869489694906949169492694936949469495694966949769498694996950069501695026950369504695056950669507695086950969510695116951269513695146951569516695176951869519695206952169522695236952469525695266952769528695296953069531695326953369534695356953669537695386953969540695416954269543695446954569546695476954869549695506955169552695536955469555695566955769558695596956069561695626956369564695656956669567695686956969570695716957269573695746957569576695776957869579695806958169582695836958469585695866958769588695896959069591695926959369594695956959669597695986959969600696016960269603696046960569606696076960869609696106961169612696136961469615696166961769618696196962069621696226962369624696256962669627696286962969630696316963269633696346963569636696376963869639696406964169642696436964469645696466964769648696496965069651696526965369654696556965669657696586965969660696616966269663696646966569666696676966869669696706967169672696736967469675696766967769678696796968069681696826968369684696856968669687696886968969690696916969269693696946969569696696976969869699697006970169702697036970469705697066970769708697096971069711697126971369714697156971669717697186971969720697216972269723697246972569726697276972869729697306973169732697336973469735697366973769738697396974069741697426974369744697456974669747697486974969750697516975269753697546975569756697576975869759697606976169762697636976469765697666976769768697696977069771697726977369774697756977669777697786977969780697816978269783697846978569786697876978869789697906979169792697936979469795697966979769798697996980069801698026980369804698056980669807698086980969810698116981269813698146981569816698176981869819698206982169822698236982469825698266982769828698296983069831698326983369834698356983669837698386983969840698416984269843698446984569846698476984869849698506985169852698536985469855698566985769858698596986069861698626986369864698656986669867698686986969870698716987269873698746987569876698776987869879698806988169882698836988469885698866988769888698896989069891698926989369894698956989669897698986989969900699016990269903699046990569906699076990869909699106991169912699136991469915699166991769918699196992069921699226992369924699256992669927699286992969930699316993269933699346993569936699376993869939699406994169942699436994469945699466994769948699496995069951699526995369954699556995669957699586995969960699616996269963699646996569966699676996869969699706997169972699736997469975699766997769978699796998069981699826998369984699856998669987699886998969990699916999269993699946999569996699976999869999700007000170002700037000470005700067000770008700097001070011700127001370014700157001670017700187001970020700217002270023700247002570026700277002870029700307003170032700337003470035700367003770038700397004070041700427004370044700457004670047700487004970050700517005270053700547005570056700577005870059700607006170062700637006470065700667006770068700697007070071700727007370074700757007670077700787007970080700817008270083700847008570086700877008870089700907009170092700937009470095700967009770098700997010070101701027010370104701057010670107701087010970110701117011270113701147011570116701177011870119701207012170122701237012470125701267012770128701297013070131701327013370134701357013670137701387013970140701417014270143701447014570146701477014870149701507015170152701537015470155701567015770158701597016070161701627016370164701657016670167701687016970170701717017270173701747017570176701777017870179701807018170182701837018470185701867018770188701897019070191701927019370194701957019670197701987019970200702017020270203702047020570206702077020870209702107021170212702137021470215702167021770218702197022070221702227022370224702257022670227702287022970230702317023270233702347023570236702377023870239702407024170242702437024470245702467024770248702497025070251702527025370254702557025670257702587025970260702617026270263702647026570266702677026870269702707027170272702737027470275702767027770278702797028070281702827028370284702857028670287702887028970290702917029270293702947029570296702977029870299703007030170302703037030470305703067030770308703097031070311703127031370314703157031670317703187031970320703217032270323703247032570326703277032870329703307033170332703337033470335703367033770338703397034070341703427034370344703457034670347703487034970350703517035270353703547035570356703577035870359703607036170362703637036470365703667036770368703697037070371703727037370374703757037670377703787037970380703817038270383703847038570386703877038870389703907039170392703937039470395703967039770398703997040070401704027040370404704057040670407704087040970410704117041270413704147041570416704177041870419704207042170422704237042470425704267042770428704297043070431704327043370434704357043670437704387043970440704417044270443704447044570446704477044870449704507045170452704537045470455704567045770458704597046070461704627046370464704657046670467704687046970470704717047270473704747047570476704777047870479704807048170482704837048470485704867048770488704897049070491704927049370494704957049670497704987049970500705017050270503705047050570506705077050870509705107051170512705137051470515705167051770518705197052070521705227052370524705257052670527705287052970530705317053270533705347053570536705377053870539705407054170542705437054470545705467054770548705497055070551705527055370554705557055670557705587055970560705617056270563705647056570566705677056870569705707057170572705737057470575705767057770578705797058070581705827058370584705857058670587705887058970590705917059270593705947059570596705977059870599706007060170602706037060470605706067060770608706097061070611706127061370614706157061670617706187061970620706217062270623706247062570626706277062870629706307063170632706337063470635706367063770638706397064070641706427064370644706457064670647706487064970650706517065270653706547065570656706577065870659706607066170662706637066470665706667066770668706697067070671706727067370674706757067670677706787067970680706817068270683706847068570686706877068870689706907069170692706937069470695706967069770698706997070070701707027070370704707057070670707707087070970710707117071270713707147071570716707177071870719707207072170722707237072470725707267072770728707297073070731707327073370734707357073670737707387073970740707417074270743707447074570746707477074870749707507075170752707537075470755707567075770758707597076070761707627076370764707657076670767707687076970770707717077270773707747077570776707777077870779707807078170782707837078470785707867078770788707897079070791707927079370794707957079670797707987079970800708017080270803708047080570806708077080870809708107081170812708137081470815708167081770818708197082070821708227082370824708257082670827708287082970830708317083270833708347083570836708377083870839708407084170842708437084470845708467084770848708497085070851708527085370854708557085670857708587085970860708617086270863708647086570866708677086870869708707087170872708737087470875708767087770878708797088070881708827088370884708857088670887708887088970890708917089270893708947089570896708977089870899709007090170902709037090470905709067090770908709097091070911709127091370914709157091670917709187091970920709217092270923709247092570926709277092870929709307093170932709337093470935709367093770938709397094070941709427094370944709457094670947709487094970950709517095270953709547095570956709577095870959709607096170962709637096470965709667096770968709697097070971709727097370974709757097670977709787097970980709817098270983709847098570986709877098870989709907099170992709937099470995709967099770998709997100071001710027100371004710057100671007710087100971010710117101271013710147101571016710177101871019710207102171022710237102471025710267102771028710297103071031710327103371034710357103671037710387103971040710417104271043710447104571046710477104871049710507105171052710537105471055710567105771058710597106071061710627106371064710657106671067710687106971070710717107271073710747107571076710777107871079710807108171082710837108471085710867108771088710897109071091710927109371094710957109671097710987109971100711017110271103711047110571106711077110871109711107111171112711137111471115711167111771118711197112071121711227112371124711257112671127711287112971130711317113271133711347113571136711377113871139711407114171142711437114471145711467114771148711497115071151711527115371154711557115671157711587115971160711617116271163711647116571166711677116871169711707117171172711737117471175711767117771178711797118071181711827118371184711857118671187711887118971190711917119271193711947119571196711977119871199712007120171202712037120471205712067120771208712097121071211712127121371214712157121671217712187121971220712217122271223712247122571226712277122871229712307123171232712337123471235712367123771238712397124071241712427124371244712457124671247712487124971250712517125271253712547125571256712577125871259712607126171262712637126471265712667126771268712697127071271712727127371274712757127671277712787127971280712817128271283712847128571286712877128871289712907129171292712937129471295712967129771298712997130071301713027130371304713057130671307713087130971310713117131271313713147131571316713177131871319713207132171322713237132471325713267132771328713297133071331713327133371334713357133671337713387133971340713417134271343713447134571346713477134871349713507135171352713537135471355713567135771358713597136071361713627136371364713657136671367713687136971370713717137271373713747137571376713777137871379713807138171382713837138471385713867138771388713897139071391713927139371394713957139671397713987139971400714017140271403714047140571406714077140871409714107141171412714137141471415714167141771418714197142071421714227142371424714257142671427714287142971430714317143271433714347143571436714377143871439714407144171442714437144471445714467144771448714497145071451714527145371454714557145671457714587145971460714617146271463714647146571466714677146871469714707147171472714737147471475714767147771478714797148071481714827148371484714857148671487714887148971490714917149271493714947149571496714977149871499715007150171502715037150471505715067150771508715097151071511715127151371514715157151671517715187151971520715217152271523715247152571526715277152871529715307153171532715337153471535715367153771538715397154071541715427154371544715457154671547715487154971550715517155271553715547155571556715577155871559715607156171562715637156471565715667156771568715697157071571715727157371574715757157671577715787157971580715817158271583715847158571586715877158871589715907159171592715937159471595715967159771598715997160071601716027160371604716057160671607716087160971610716117161271613716147161571616716177161871619716207162171622716237162471625716267162771628716297163071631716327163371634716357163671637716387163971640716417164271643716447164571646716477164871649716507165171652716537165471655716567165771658716597166071661716627166371664716657166671667716687166971670716717167271673716747167571676716777167871679716807168171682716837168471685716867168771688716897169071691716927169371694716957169671697716987169971700717017170271703717047170571706717077170871709717107171171712717137171471715717167171771718717197172071721717227172371724717257172671727717287172971730717317173271733717347173571736717377173871739717407174171742717437174471745717467174771748717497175071751717527175371754717557175671757717587175971760717617176271763717647176571766717677176871769717707177171772717737177471775717767177771778717797178071781717827178371784717857178671787717887178971790717917179271793717947179571796717977179871799718007180171802718037180471805718067180771808718097181071811718127181371814718157181671817718187181971820718217182271823718247182571826718277182871829718307183171832718337183471835718367183771838718397184071841718427184371844718457184671847718487184971850718517185271853718547185571856718577185871859718607186171862718637186471865718667186771868718697187071871718727187371874718757187671877718787187971880718817188271883718847188571886718877188871889718907189171892718937189471895718967189771898718997190071901719027190371904719057190671907719087190971910719117191271913719147191571916719177191871919719207192171922719237192471925719267192771928719297193071931719327193371934719357193671937719387193971940719417194271943719447194571946719477194871949719507195171952719537195471955719567195771958719597196071961719627196371964719657196671967719687196971970719717197271973719747197571976719777197871979719807198171982719837198471985719867198771988719897199071991719927199371994719957199671997719987199972000720017200272003720047200572006720077200872009720107201172012720137201472015720167201772018720197202072021720227202372024720257202672027720287202972030720317203272033720347203572036720377203872039720407204172042720437204472045720467204772048720497205072051720527205372054720557205672057720587205972060720617206272063720647206572066720677206872069720707207172072720737207472075720767207772078720797208072081720827208372084720857208672087720887208972090720917209272093720947209572096720977209872099721007210172102721037210472105721067210772108721097211072111721127211372114721157211672117721187211972120721217212272123721247212572126721277212872129721307213172132721337213472135721367213772138721397214072141721427214372144721457214672147721487214972150721517215272153721547215572156721577215872159721607216172162721637216472165721667216772168721697217072171721727217372174721757217672177721787217972180721817218272183721847218572186721877218872189721907219172192721937219472195721967219772198721997220072201722027220372204722057220672207722087220972210722117221272213722147221572216722177221872219722207222172222722237222472225722267222772228722297223072231722327223372234722357223672237722387223972240722417224272243722447224572246722477224872249722507225172252722537225472255722567225772258722597226072261722627226372264722657226672267722687226972270722717227272273722747227572276722777227872279722807228172282722837228472285722867228772288722897229072291722927229372294722957229672297722987229972300723017230272303723047230572306723077230872309723107231172312723137231472315723167231772318723197232072321723227232372324723257232672327723287232972330723317233272333723347233572336723377233872339723407234172342723437234472345723467234772348723497235072351723527235372354723557235672357723587235972360723617236272363723647236572366723677236872369723707237172372723737237472375723767237772378723797238072381723827238372384723857238672387723887238972390723917239272393723947239572396723977239872399724007240172402724037240472405724067240772408724097241072411724127241372414724157241672417724187241972420724217242272423724247242572426724277242872429724307243172432724337243472435724367243772438724397244072441724427244372444724457244672447724487244972450724517245272453724547245572456724577245872459724607246172462724637246472465724667246772468724697247072471724727247372474724757247672477724787247972480724817248272483724847248572486724877248872489724907249172492724937249472495724967249772498724997250072501725027250372504725057250672507725087250972510725117251272513725147251572516725177251872519725207252172522725237252472525725267252772528725297253072531725327253372534725357253672537725387253972540725417254272543725447254572546725477254872549725507255172552725537255472555725567255772558725597256072561725627256372564725657256672567725687256972570725717257272573725747257572576725777257872579725807258172582725837258472585725867258772588725897259072591725927259372594725957259672597725987259972600726017260272603726047260572606726077260872609726107261172612726137261472615726167261772618726197262072621726227262372624726257262672627726287262972630726317263272633726347263572636726377263872639726407264172642726437264472645726467264772648726497265072651726527265372654726557265672657726587265972660726617266272663726647266572666726677266872669726707267172672726737267472675726767267772678726797268072681726827268372684726857268672687726887268972690726917269272693726947269572696726977269872699727007270172702727037270472705727067270772708727097271072711727127271372714727157271672717727187271972720727217272272723727247272572726727277272872729727307273172732727337273472735727367273772738727397274072741727427274372744727457274672747727487274972750727517275272753727547275572756727577275872759727607276172762727637276472765727667276772768727697277072771727727277372774727757277672777727787277972780727817278272783727847278572786727877278872789727907279172792727937279472795727967279772798727997280072801728027280372804728057280672807728087280972810728117281272813728147281572816728177281872819728207282172822728237282472825728267282772828728297283072831728327283372834728357283672837728387283972840728417284272843728447284572846728477284872849728507285172852728537285472855728567285772858728597286072861728627286372864728657286672867728687286972870728717287272873728747287572876728777287872879728807288172882728837288472885728867288772888728897289072891728927289372894728957289672897728987289972900729017290272903729047290572906729077290872909729107291172912729137291472915729167291772918729197292072921729227292372924729257292672927729287292972930729317293272933729347293572936729377293872939729407294172942729437294472945729467294772948729497295072951729527295372954729557295672957729587295972960729617296272963729647296572966729677296872969729707297172972729737297472975729767297772978729797298072981729827298372984729857298672987729887298972990729917299272993729947299572996729977299872999730007300173002730037300473005730067300773008730097301073011730127301373014730157301673017730187301973020730217302273023730247302573026730277302873029730307303173032730337303473035730367303773038730397304073041730427304373044730457304673047730487304973050730517305273053730547305573056730577305873059730607306173062730637306473065730667306773068730697307073071730727307373074730757307673077730787307973080730817308273083730847308573086730877308873089730907309173092730937309473095730967309773098730997310073101731027310373104731057310673107731087310973110731117311273113731147311573116731177311873119731207312173122731237312473125731267312773128731297313073131731327313373134731357313673137731387313973140731417314273143731447314573146731477314873149731507315173152731537315473155731567315773158731597316073161731627316373164731657316673167731687316973170731717317273173731747317573176731777317873179731807318173182731837318473185731867318773188731897319073191731927319373194731957319673197731987319973200732017320273203732047320573206732077320873209732107321173212732137321473215732167321773218732197322073221732227322373224732257322673227732287322973230732317323273233732347323573236732377323873239732407324173242732437324473245732467324773248732497325073251732527325373254732557325673257732587325973260732617326273263732647326573266732677326873269732707327173272732737327473275732767327773278732797328073281732827328373284732857328673287732887328973290732917329273293732947329573296732977329873299733007330173302733037330473305733067330773308733097331073311733127331373314733157331673317733187331973320733217332273323733247332573326733277332873329733307333173332733337333473335733367333773338733397334073341733427334373344733457334673347733487334973350733517335273353733547335573356733577335873359733607336173362733637336473365733667336773368733697337073371733727337373374733757337673377733787337973380733817338273383733847338573386733877338873389733907339173392733937339473395733967339773398733997340073401734027340373404734057340673407734087340973410734117341273413734147341573416734177341873419734207342173422734237342473425734267342773428734297343073431734327343373434734357343673437734387343973440734417344273443734447344573446734477344873449734507345173452734537345473455734567345773458734597346073461734627346373464734657346673467734687346973470734717347273473734747347573476734777347873479734807348173482734837348473485734867348773488734897349073491734927349373494734957349673497734987349973500735017350273503735047350573506735077350873509735107351173512735137351473515735167351773518735197352073521735227352373524735257352673527735287352973530735317353273533735347353573536735377353873539735407354173542735437354473545735467354773548735497355073551735527355373554735557355673557735587355973560735617356273563735647356573566735677356873569735707357173572735737357473575735767357773578735797358073581735827358373584735857358673587735887358973590735917359273593735947359573596735977359873599736007360173602736037360473605736067360773608736097361073611736127361373614736157361673617736187361973620736217362273623736247362573626736277362873629736307363173632736337363473635736367363773638736397364073641736427364373644736457364673647736487364973650736517365273653736547365573656736577365873659736607366173662736637366473665736667366773668736697367073671736727367373674736757367673677736787367973680736817368273683736847368573686736877368873689736907369173692736937369473695736967369773698736997370073701737027370373704
  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, (word32)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, (word32)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, (word32)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, (word32)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. /* modify case and compare subject from certificate in ca to expected.
  10755. * The first bit of the name is:
  10756. * /C=US/ST=Washington
  10757. * So we'll change subject->name[1] to 'c' (lower case) */
  10758. if (subject != NULL) {
  10759. subject->name[1] = 'c';
  10760. ExpectIntEQ(wolfSSL_X509_NAME_cmp((const WOLFSSL_X509_NAME*)subject,
  10761. (const WOLFSSL_X509_NAME*)wolfSSL_X509_get_subject_name(x509)), 0);
  10762. }
  10763. EVP_PKEY_free(pkey);
  10764. pkey = NULL;
  10765. X509_free(x509);
  10766. x509 = NULL;
  10767. X509_free(cert);
  10768. cert = NULL;
  10769. BIO_free(bio);
  10770. bio = NULL;
  10771. PKCS12_free(pkcs12);
  10772. pkcs12 = NULL;
  10773. sk_X509_pop_free(ca, NULL); /* TEST d2i_PKCS12_fp */
  10774. ca = NULL;
  10775. /* test order of parsing */
  10776. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10777. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  10778. if (f != XBADFILE) {
  10779. XFCLOSE(f);
  10780. f = XBADFILE;
  10781. }
  10782. /* check verify MAC fail case */
  10783. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10784. ExpectNull(pkey);
  10785. ExpectNull(cert);
  10786. /* check parse with no extra certs kept */
  10787. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10788. 1);
  10789. ExpectNotNull(pkey);
  10790. ExpectNotNull(cert);
  10791. wolfSSL_EVP_PKEY_free(pkey);
  10792. pkey = NULL;
  10793. wolfSSL_X509_free(cert);
  10794. cert = NULL;
  10795. /* check parse with extra certs kept */
  10796. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10797. 1);
  10798. ExpectNotNull(pkey);
  10799. ExpectNotNull(cert);
  10800. ExpectNotNull(ca);
  10801. wolfSSL_EVP_PKEY_free(pkey);
  10802. pkey = NULL;
  10803. wolfSSL_X509_free(cert);
  10804. cert = NULL;
  10805. sk_X509_pop_free(ca, NULL);
  10806. ca = NULL;
  10807. PKCS12_free(pkcs12);
  10808. pkcs12 = NULL;
  10809. #endif /* HAVE_ECC */
  10810. #ifdef WC_RC2
  10811. /* test PKCS#12 with RC2 encryption */
  10812. ExpectTrue((f = XFOPEN(rc2p12, "rb")) != XBADFILE);
  10813. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  10814. if (f != XBADFILE) {
  10815. XFCLOSE(f);
  10816. f = XBADFILE;
  10817. }
  10818. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  10819. ExpectNotNull(pkcs12 = d2i_PKCS12_bio(bio, NULL));
  10820. /* check verify MAC fail case */
  10821. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "bad", &pkey, &cert, NULL), 0);
  10822. ExpectNull(pkey);
  10823. ExpectNull(cert);
  10824. /* check parse with not extra certs kept */
  10825. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, NULL),
  10826. WOLFSSL_SUCCESS);
  10827. ExpectNotNull(pkey);
  10828. ExpectNotNull(cert);
  10829. wolfSSL_EVP_PKEY_free(pkey);
  10830. pkey = NULL;
  10831. wolfSSL_X509_free(cert);
  10832. cert = NULL;
  10833. /* check parse with extra certs kept */
  10834. ExpectIntEQ(ret = PKCS12_parse(pkcs12, "wolfSSL test", &pkey, &cert, &ca),
  10835. WOLFSSL_SUCCESS);
  10836. ExpectNotNull(pkey);
  10837. ExpectNotNull(cert);
  10838. ExpectNotNull(ca);
  10839. wolfSSL_EVP_PKEY_free(pkey);
  10840. wolfSSL_X509_free(cert);
  10841. sk_X509_pop_free(ca, NULL);
  10842. BIO_free(bio);
  10843. bio = NULL;
  10844. PKCS12_free(pkcs12);
  10845. pkcs12 = NULL;
  10846. #endif /* WC_RC2 */
  10847. /* Test i2d_PKCS12_bio */
  10848. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  10849. ExpectNotNull(pkcs12 = d2i_PKCS12_fp(f, NULL));
  10850. if (f != XBADFILE)
  10851. XFCLOSE(f);
  10852. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  10853. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, pkcs12), 1);
  10854. ExpectIntEQ(ret = i2d_PKCS12_bio(NULL, pkcs12), 0);
  10855. ExpectIntEQ(ret = i2d_PKCS12_bio(bio, NULL), 0);
  10856. PKCS12_free(pkcs12);
  10857. BIO_free(bio);
  10858. (void)order;
  10859. #endif /* OPENSSL_EXTRA */
  10860. #endif /* HAVE_FIPS */
  10861. return EXPECT_RESULT();
  10862. }
  10863. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  10864. defined(WOLFSSL_ENCRYPTED_KEYS) && !defined(NO_DES3) && !defined(NO_PWDBASED) && \
  10865. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_MD5)
  10866. #define TEST_PKCS8_ENC
  10867. #endif
  10868. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  10869. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  10870. /* used to keep track if FailTestCallback was called */
  10871. static int failTestCallbackCalled = 0;
  10872. static WC_INLINE int FailTestCallBack(char* passwd, int sz, int rw, void* userdata)
  10873. {
  10874. (void)passwd;
  10875. (void)sz;
  10876. (void)rw;
  10877. (void)userdata;
  10878. /* mark called, test_wolfSSL_no_password_cb() will check and fail if set */
  10879. failTestCallbackCalled = 1;
  10880. return -1;
  10881. }
  10882. #endif
  10883. static int test_wolfSSL_no_password_cb(void)
  10884. {
  10885. EXPECT_DECLS;
  10886. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) \
  10887. && defined(HAVE_ECC) && defined(WOLFSSL_ENCRYPTED_KEYS)
  10888. WOLFSSL_CTX* ctx = NULL;
  10889. byte buff[FOURK_BUF];
  10890. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  10891. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  10892. XFILE f = XBADFILE;
  10893. int bytes = 0;
  10894. #ifndef NO_WOLFSSL_CLIENT
  10895. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_client_method()));
  10896. #else
  10897. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLS_server_method()));
  10898. #endif
  10899. wolfSSL_CTX_set_default_passwd_cb(ctx, FailTestCallBack);
  10900. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  10901. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10902. if (f != XBADFILE) {
  10903. XFCLOSE(f);
  10904. f = XBADFILE;
  10905. }
  10906. ExpectIntLE(bytes, sizeof(buff));
  10907. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10908. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  10909. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  10910. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  10911. if (f != XBADFILE)
  10912. XFCLOSE(f);
  10913. ExpectIntLE(bytes, sizeof(buff));
  10914. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  10915. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  10916. wolfSSL_CTX_free(ctx);
  10917. /* Password callback should not be called by default */
  10918. ExpectIntEQ(failTestCallbackCalled, 0);
  10919. #endif
  10920. return EXPECT_RESULT();
  10921. }
  10922. #ifdef TEST_PKCS8_ENC
  10923. /* for PKCS8 test case */
  10924. static int PKCS8TestCallBack(char* passwd, int sz, int rw, void* userdata)
  10925. {
  10926. int flag = 0;
  10927. (void)rw;
  10928. if (userdata != NULL) {
  10929. flag = *((int*)userdata); /* user set data */
  10930. }
  10931. switch (flag) {
  10932. case 1: /* flag set for specific WOLFSSL_CTX structure, note userdata
  10933. * can be anything the user wishes to be passed to the callback
  10934. * associated with the WOLFSSL_CTX */
  10935. XSTRNCPY(passwd, "yassl123", sz);
  10936. return 8;
  10937. default:
  10938. return BAD_FUNC_ARG;
  10939. }
  10940. }
  10941. #endif /* TEST_PKCS8_ENC */
  10942. /* Testing functions dealing with PKCS8 */
  10943. static int test_wolfSSL_PKCS8(void)
  10944. {
  10945. EXPECT_DECLS;
  10946. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN) && defined(HAVE_PKCS8) && \
  10947. !defined(WOLFCRYPT_ONLY)
  10948. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  10949. byte buff[FOURK_BUF];
  10950. byte der[FOURK_BUF];
  10951. #ifndef NO_RSA
  10952. const char serverKeyPkcs8PemFile[] = "./certs/server-keyPkcs8.pem";
  10953. const char serverKeyPkcs8DerFile[] = "./certs/server-keyPkcs8.der";
  10954. #endif
  10955. const char eccPkcs8PrivKeyPemFile[] = "./certs/ecc-privkeyPkcs8.pem";
  10956. #ifdef HAVE_ECC
  10957. const char eccPkcs8PrivKeyDerFile[] = "./certs/ecc-privkeyPkcs8.der";
  10958. #endif
  10959. XFILE f = XBADFILE;
  10960. int bytes = 0;
  10961. WOLFSSL_CTX* ctx = NULL;
  10962. #if defined(HAVE_ECC) && !defined(NO_CODING) && !defined(WOLFSSL_NO_PEM)
  10963. int ret;
  10964. ecc_key key;
  10965. word32 x = 0;
  10966. #endif
  10967. #ifdef TEST_PKCS8_ENC
  10968. #if !defined(NO_RSA) && !defined(NO_SHA)
  10969. const char serverKeyPkcs8EncPemFile[] = "./certs/server-keyPkcs8Enc.pem";
  10970. const char serverKeyPkcs8EncDerFile[] = "./certs/server-keyPkcs8Enc.der";
  10971. #endif
  10972. #if defined(HAVE_ECC) && !defined(NO_SHA)
  10973. const char eccPkcs8EncPrivKeyPemFile[] = "./certs/ecc-keyPkcs8Enc.pem";
  10974. const char eccPkcs8EncPrivKeyDerFile[] = "./certs/ecc-keyPkcs8Enc.der";
  10975. #endif
  10976. int flag;
  10977. #endif
  10978. (void)der;
  10979. #ifndef NO_WOLFSSL_CLIENT
  10980. #ifndef WOLFSSL_NO_TLS12
  10981. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  10982. #else
  10983. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  10984. #endif
  10985. #else
  10986. #ifndef WOLFSSL_NO_TLS12
  10987. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  10988. #else
  10989. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  10990. #endif
  10991. #endif
  10992. #ifdef TEST_PKCS8_ENC
  10993. wolfSSL_CTX_set_default_passwd_cb(ctx, PKCS8TestCallBack);
  10994. wolfSSL_CTX_set_default_passwd_cb_userdata(ctx, (void*)&flag);
  10995. flag = 1; /* used by password callback as return code */
  10996. #if !defined(NO_RSA) && !defined(NO_SHA)
  10997. /* test loading PEM PKCS8 encrypted file */
  10998. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncPemFile, "rb")) != XBADFILE);
  10999. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11000. if (f != XBADFILE) {
  11001. XFCLOSE(f);
  11002. f = XBADFILE;
  11003. }
  11004. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11005. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11006. /* this next case should fail because of password callback return code */
  11007. flag = 0; /* used by password callback as return code */
  11008. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11009. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11010. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  11011. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11012. "yassl123"), 0);
  11013. /* test that error value is returned with a bad password */
  11014. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11015. "bad"), 0);
  11016. /* test loading PEM PKCS8 encrypted file */
  11017. ExpectTrue((f = XFOPEN(serverKeyPkcs8EncDerFile, "rb")) != XBADFILE);
  11018. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11019. if (f != XBADFILE) {
  11020. XFCLOSE(f);
  11021. f = XBADFILE;
  11022. }
  11023. flag = 1; /* used by password callback as return code */
  11024. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11025. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11026. /* this next case should fail because of password callback return code */
  11027. flag = 0; /* used by password callback as return code */
  11028. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11029. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11030. #endif /* !NO_RSA && !NO_SHA */
  11031. #if defined(HAVE_ECC) && !defined(NO_SHA)
  11032. /* test loading PEM PKCS8 encrypted ECC Key file */
  11033. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyPemFile, "rb")) != XBADFILE);
  11034. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11035. if (f != XBADFILE) {
  11036. XFCLOSE(f);
  11037. f = XBADFILE;
  11038. }
  11039. flag = 1; /* used by password callback as return code */
  11040. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11041. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11042. /* this next case should fail because of password callback return code */
  11043. flag = 0; /* used by password callback as return code */
  11044. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11045. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11046. /* decrypt PKCS8 PEM to key in DER format with not using WOLFSSL_CTX */
  11047. ExpectIntGT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11048. "yassl123"), 0);
  11049. /* test that error value is returned with a bad password */
  11050. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der),
  11051. "bad"), 0);
  11052. /* test loading DER PKCS8 encrypted ECC Key file */
  11053. ExpectTrue((f = XFOPEN(eccPkcs8EncPrivKeyDerFile, "rb")) != XBADFILE);
  11054. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11055. if (f != XBADFILE) {
  11056. XFCLOSE(f);
  11057. f = XBADFILE;
  11058. }
  11059. flag = 1; /* used by password callback as return code */
  11060. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11061. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11062. /* this next case should fail because of password callback return code */
  11063. flag = 0; /* used by password callback as return code */
  11064. ExpectIntNE(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11065. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11066. /* leave flag as "okay" */
  11067. flag = 1;
  11068. #endif /* HAVE_ECC && !NO_SHA */
  11069. #endif /* TEST_PKCS8_ENC */
  11070. #ifndef NO_RSA
  11071. /* test loading ASN.1 (DER) PKCS8 private key file (not encrypted) */
  11072. ExpectTrue((f = XFOPEN(serverKeyPkcs8DerFile, "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_ASN1), WOLFSSL_SUCCESS);
  11080. /* test loading PEM PKCS8 private key file (not encrypted) */
  11081. ExpectTrue((f = XFOPEN(serverKeyPkcs8PemFile, "rb")) != XBADFILE);
  11082. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11083. if (f != XBADFILE) {
  11084. XFCLOSE(f);
  11085. f = XBADFILE;
  11086. }
  11087. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11088. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11089. #endif /* !NO_RSA */
  11090. /* Test PKCS8 PEM ECC key no crypt */
  11091. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyPemFile, "rb")) != XBADFILE);
  11092. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11093. if (f != XBADFILE) {
  11094. XFCLOSE(f);
  11095. f = XBADFILE;
  11096. }
  11097. #ifdef HAVE_ECC
  11098. /* Test PKCS8 PEM ECC key no crypt */
  11099. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11100. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  11101. #if !defined(NO_CODING) && !defined(WOLFSSL_NO_PEM)
  11102. /* decrypt PKCS8 PEM to key in DER format */
  11103. ExpectIntGT((bytes = wc_KeyPemToDer(buff, bytes, der,
  11104. (word32)sizeof(der), NULL)), 0);
  11105. ret = wc_ecc_init(&key);
  11106. if (ret == 0) {
  11107. ret = wc_EccPrivateKeyDecode(der, &x, &key, (word32)bytes);
  11108. wc_ecc_free(&key);
  11109. }
  11110. ExpectIntEQ(ret, 0);
  11111. #endif
  11112. /* Test PKCS8 DER ECC key no crypt */
  11113. ExpectTrue((f = XFOPEN(eccPkcs8PrivKeyDerFile, "rb")) != XBADFILE);
  11114. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  11115. if (f != XBADFILE)
  11116. XFCLOSE(f);
  11117. /* Test using a PKCS8 ECC PEM */
  11118. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, buff, bytes,
  11119. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11120. #else
  11121. /* if HAVE_ECC is not defined then BEGIN EC PRIVATE KEY is not found */
  11122. ExpectIntEQ((bytes = wc_KeyPemToDer(buff, bytes, der,
  11123. (word32)sizeof(der), NULL)), ASN_NO_PEM_HEADER);
  11124. #endif /* HAVE_ECC */
  11125. wolfSSL_CTX_free(ctx);
  11126. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11127. #endif /* !NO_FILESYSTEM && !NO_ASN && HAVE_PKCS8 */
  11128. return EXPECT_RESULT();
  11129. }
  11130. static int test_wolfSSL_PKCS8_ED25519(void)
  11131. {
  11132. EXPECT_DECLS;
  11133. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  11134. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED25519) && \
  11135. defined(HAVE_ED25519_KEY_IMPORT)
  11136. const byte encPrivKey[] = \
  11137. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  11138. "MIGbMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAheCGLmWGh7+AICCAAw\n"
  11139. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEC4L5P6GappsTyhOOoQfvh8EQJMX\n"
  11140. "OAdlsYKCOcFo4djg6AI1lRdeBRwVFWkha7gBdoCJOzS8wDvTbYcJMPvANu5ft3nl\n"
  11141. "2L9W4v7swXkV+X+a1ww=\n"
  11142. "-----END ENCRYPTED PRIVATE KEY-----\n";
  11143. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  11144. byte der[FOURK_BUF];
  11145. WOLFSSL_CTX* ctx = NULL;
  11146. int bytes;
  11147. XMEMSET(der, 0, sizeof(der));
  11148. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  11149. (word32)sizeof(der), password)), 0);
  11150. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  11151. #ifndef NO_WOLFSSL_SERVER
  11152. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  11153. #else
  11154. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  11155. #endif
  11156. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  11157. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11158. wolfSSL_CTX_free(ctx);
  11159. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11160. #endif
  11161. return EXPECT_RESULT();
  11162. }
  11163. static int test_wolfSSL_PKCS8_ED448(void)
  11164. {
  11165. EXPECT_DECLS;
  11166. #if !defined(NO_ASN) && defined(HAVE_PKCS8) && defined(HAVE_AES_CBC) && \
  11167. defined(WOLFSSL_ENCRYPTED_KEYS) && defined(HAVE_ED448) && \
  11168. defined(HAVE_ED448_KEY_IMPORT)
  11169. const byte encPrivKey[] = \
  11170. "-----BEGIN ENCRYPTED PRIVATE KEY-----\n"
  11171. "MIGrMFcGCSqGSIb3DQEFDTBKMCkGCSqGSIb3DQEFDDAcBAjSbZKnG4EPggICCAAw\n"
  11172. "DAYIKoZIhvcNAgkFADAdBglghkgBZQMEASoEEFvCFWBBHBlJBsYleBJlJWcEUNC7\n"
  11173. "Tf5pZviT5Btar4D/MNg6BsQHSDf5KW4ix871EsgDY2Zz+euaoWspiMntz7gU+PQu\n"
  11174. "T/JJcbD2Ly8BbE3l5WHMifAQqNLxJBfXrHkfYtAo\n"
  11175. "-----END ENCRYPTED PRIVATE KEY-----\n";
  11176. const char password[] = "abcdefghijklmnopqrstuvwxyz";
  11177. byte der[FOURK_BUF];
  11178. WOLFSSL_CTX* ctx = NULL;
  11179. int bytes;
  11180. XMEMSET(der, 0, sizeof(der));
  11181. ExpectIntGT((bytes = wc_KeyPemToDer(encPrivKey, sizeof(encPrivKey), der,
  11182. (word32)sizeof(der), password)), 0);
  11183. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  11184. #ifndef NO_WOLFSSL_SERVER
  11185. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  11186. #else
  11187. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  11188. #endif
  11189. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_buffer(ctx, der, bytes,
  11190. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11191. wolfSSL_CTX_free(ctx);
  11192. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  11193. #endif
  11194. return EXPECT_RESULT();
  11195. }
  11196. /* Testing functions dealing with PKCS5 */
  11197. static int test_wolfSSL_PKCS5(void)
  11198. {
  11199. EXPECT_DECLS;
  11200. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA) && !defined(NO_PWDBASED)
  11201. #ifdef HAVE_FIPS /* Password minimum length is 14 (112-bit) in FIPS MODE */
  11202. const char* passwd = "myfipsPa$$W0rd";
  11203. #else
  11204. const char *passwd = "pass1234";
  11205. #endif
  11206. const unsigned char *salt = (unsigned char *)"salt1234";
  11207. unsigned char *out = (unsigned char *)XMALLOC(WC_SHA_DIGEST_SIZE, NULL,
  11208. DYNAMIC_TYPE_TMP_BUFFER);
  11209. int ret = 0;
  11210. ExpectNotNull(out);
  11211. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC_SHA1(passwd,(int)XSTRLEN(passwd), salt,
  11212. (int)XSTRLEN((const char *) salt), 10, WC_SHA_DIGEST_SIZE,out),
  11213. WOLFSSL_SUCCESS);
  11214. #ifdef WOLFSSL_SHA512
  11215. ExpectIntEQ(ret = PKCS5_PBKDF2_HMAC(passwd,(int)XSTRLEN(passwd), salt,
  11216. (int)XSTRLEN((const char *) salt), 10, wolfSSL_EVP_sha512(),
  11217. WC_SHA_DIGEST_SIZE, out), SSL_SUCCESS);
  11218. #endif
  11219. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  11220. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_SHA) */
  11221. return EXPECT_RESULT();
  11222. }
  11223. /* test parsing URI from certificate */
  11224. static int test_wolfSSL_URI(void)
  11225. {
  11226. EXPECT_DECLS;
  11227. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  11228. && (defined(KEEP_PEER_CERT) || defined(SESSION_CERTS) || \
  11229. defined(OPENSSL_EXTRA))
  11230. WOLFSSL_X509* x509 = NULL;
  11231. const char uri[] = "./certs/client-uri-cert.pem";
  11232. const char urn[] = "./certs/client-absolute-urn.pem";
  11233. const char badUri[] = "./certs/client-relative-uri.pem";
  11234. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(uri,
  11235. WOLFSSL_FILETYPE_PEM));
  11236. wolfSSL_FreeX509(x509);
  11237. x509 = NULL;
  11238. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(urn,
  11239. WOLFSSL_FILETYPE_PEM));
  11240. wolfSSL_FreeX509(x509);
  11241. x509 = NULL;
  11242. #if !defined(IGNORE_NAME_CONSTRAINTS) && !defined(WOLFSSL_NO_ASN_STRICT) \
  11243. && !defined(WOLFSSL_FPKI)
  11244. ExpectNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  11245. WOLFSSL_FILETYPE_PEM));
  11246. #else
  11247. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(badUri,
  11248. WOLFSSL_FILETYPE_PEM));
  11249. #endif
  11250. wolfSSL_FreeX509(x509);
  11251. #endif
  11252. return EXPECT_RESULT();
  11253. }
  11254. static int test_wolfSSL_TBS(void)
  11255. {
  11256. EXPECT_DECLS;
  11257. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) \
  11258. && defined(OPENSSL_EXTRA)
  11259. WOLFSSL_X509* x509 = NULL;
  11260. const unsigned char* tbs;
  11261. int tbsSz;
  11262. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caCertFile,
  11263. WOLFSSL_FILETYPE_PEM));
  11264. ExpectNull(tbs = wolfSSL_X509_get_tbs(NULL, &tbsSz));
  11265. ExpectNull(tbs = wolfSSL_X509_get_tbs(x509, NULL));
  11266. ExpectNotNull(tbs = wolfSSL_X509_get_tbs(x509, &tbsSz));
  11267. ExpectIntEQ(tbsSz, 1003);
  11268. wolfSSL_FreeX509(x509);
  11269. #endif
  11270. return EXPECT_RESULT();
  11271. }
  11272. static int test_wolfSSL_X509_verify(void)
  11273. {
  11274. EXPECT_DECLS;
  11275. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  11276. defined(OPENSSL_EXTRA)
  11277. WOLFSSL_X509* ca = NULL;
  11278. WOLFSSL_X509* serv = NULL;
  11279. WOLFSSL_EVP_PKEY* pkey = NULL;
  11280. unsigned char buf[2048];
  11281. const unsigned char* pt = NULL;
  11282. int bufSz;
  11283. ExpectNotNull(ca = wolfSSL_X509_load_certificate_file(caCertFile,
  11284. WOLFSSL_FILETYPE_PEM));
  11285. ExpectIntNE(wolfSSL_X509_get_pubkey_buffer(NULL, buf, &bufSz),
  11286. WOLFSSL_SUCCESS);
  11287. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, NULL, &bufSz),
  11288. WOLFSSL_SUCCESS);
  11289. ExpectIntEQ(bufSz, 294);
  11290. bufSz = 2048;
  11291. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(ca, buf, &bufSz),
  11292. WOLFSSL_SUCCESS);
  11293. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(NULL), WOLFSSL_FAILURE);
  11294. ExpectIntEQ(wolfSSL_X509_get_pubkey_type(ca), RSAk);
  11295. ExpectNotNull(serv = wolfSSL_X509_load_certificate_file(svrCertFile,
  11296. WOLFSSL_FILETYPE_PEM));
  11297. /* success case */
  11298. pt = buf;
  11299. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  11300. ExpectIntEQ(i2d_PUBKEY(pkey, NULL), bufSz);
  11301. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_SUCCESS);
  11302. wolfSSL_EVP_PKEY_free(pkey);
  11303. pkey = NULL;
  11304. /* fail case */
  11305. bufSz = 2048;
  11306. ExpectIntEQ(wolfSSL_X509_get_pubkey_buffer(serv, buf, &bufSz),
  11307. WOLFSSL_SUCCESS);
  11308. pt = buf;
  11309. ExpectNotNull(pkey = wolfSSL_d2i_PUBKEY(NULL, &pt, bufSz));
  11310. ExpectIntEQ(wolfSSL_X509_verify(serv, pkey), WOLFSSL_FAILURE);
  11311. ExpectIntEQ(wolfSSL_X509_verify(NULL, pkey), WOLFSSL_FATAL_ERROR);
  11312. ExpectIntEQ(wolfSSL_X509_verify(serv, NULL), WOLFSSL_FATAL_ERROR);
  11313. wolfSSL_EVP_PKEY_free(pkey);
  11314. wolfSSL_FreeX509(ca);
  11315. wolfSSL_FreeX509(serv);
  11316. #endif
  11317. return EXPECT_RESULT();
  11318. }
  11319. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11320. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11321. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11322. /* create certificate with version 2 */
  11323. static int test_set_x509_badversion(WOLFSSL_CTX* ctx)
  11324. {
  11325. EXPECT_DECLS;
  11326. WOLFSSL_X509 *x509 = NULL, *x509v2 = NULL;
  11327. WOLFSSL_EVP_PKEY *priv = NULL, *pub = NULL;
  11328. unsigned char *der = NULL, *key = NULL, *pt;
  11329. char *header = NULL, *name = NULL;
  11330. int derSz;
  11331. long keySz;
  11332. XFILE fp = XBADFILE;
  11333. WOLFSSL_ASN1_TIME *notBefore = NULL, *notAfter = NULL;
  11334. time_t t;
  11335. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  11336. WOLFSSL_FILETYPE_PEM));
  11337. ExpectTrue((fp = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  11338. ExpectIntEQ(wolfSSL_PEM_read(fp, &name, &header, &key, &keySz),
  11339. WOLFSSL_SUCCESS);
  11340. if (fp != XBADFILE)
  11341. XFCLOSE(fp);
  11342. pt = key;
  11343. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  11344. (const unsigned char**)&pt, keySz));
  11345. /* create the version 2 certificate */
  11346. ExpectNotNull(x509v2 = X509_new());
  11347. ExpectIntEQ(wolfSSL_X509_set_version(x509v2, 1), WOLFSSL_SUCCESS);
  11348. ExpectIntEQ(wolfSSL_X509_set_subject_name(x509v2,
  11349. wolfSSL_X509_get_subject_name(x509)), WOLFSSL_SUCCESS);
  11350. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509v2,
  11351. wolfSSL_X509_get_issuer_name(x509)), WOLFSSL_SUCCESS);
  11352. ExpectNotNull(pub = wolfSSL_X509_get_pubkey(x509));
  11353. ExpectIntEQ(X509_set_pubkey(x509v2, pub), WOLFSSL_SUCCESS);
  11354. t = time(NULL);
  11355. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  11356. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  11357. ExpectTrue(wolfSSL_X509_set_notBefore(x509v2, notBefore));
  11358. ExpectTrue(wolfSSL_X509_set_notAfter(x509v2, notAfter));
  11359. ExpectIntGT(wolfSSL_X509_sign(x509v2, priv, EVP_sha256()), 0);
  11360. derSz = wolfSSL_i2d_X509(x509v2, &der);
  11361. ExpectIntGT(derSz, 0);
  11362. ExpectIntEQ(wolfSSL_CTX_use_certificate_buffer(ctx, der, derSz,
  11363. WOLFSSL_FILETYPE_ASN1), WOLFSSL_SUCCESS);
  11364. /* TODO: Replace with API call */
  11365. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  11366. XFREE(key, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11367. XFREE(name, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11368. XFREE(header, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  11369. wolfSSL_X509_free(x509);
  11370. wolfSSL_X509_free(x509v2);
  11371. wolfSSL_EVP_PKEY_free(priv);
  11372. wolfSSL_EVP_PKEY_free(pub);
  11373. wolfSSL_ASN1_TIME_free(notBefore);
  11374. wolfSSL_ASN1_TIME_free(notAfter);
  11375. return EXPECT_RESULT();
  11376. }
  11377. /* override certificate version error */
  11378. static int test_override_x509(int preverify, WOLFSSL_X509_STORE_CTX* store)
  11379. {
  11380. EXPECT_DECLS;
  11381. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  11382. ExpectIntEQ(store->error, ASN_VERSION_E);
  11383. #else
  11384. ExpectIntEQ(store->error, 0);
  11385. #endif
  11386. ExpectIntEQ((int)wolfSSL_X509_get_version(store->current_cert), 1);
  11387. (void)preverify;
  11388. return EXPECT_RESULT() == TEST_SUCCESS;
  11389. }
  11390. /* set verify callback that will override bad certificate version */
  11391. static int test_set_override_x509(WOLFSSL_CTX* ctx)
  11392. {
  11393. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, test_override_x509);
  11394. return TEST_SUCCESS;
  11395. }
  11396. #endif
  11397. static int test_wolfSSL_X509_TLS_version_test_1(void)
  11398. {
  11399. EXPECT_DECLS;
  11400. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11401. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11402. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11403. test_ssl_cbf func_cb_client;
  11404. test_ssl_cbf func_cb_server;
  11405. /* test server rejects a client certificate that is not version 3 */
  11406. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  11407. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  11408. func_cb_client.ctx_ready = &test_set_x509_badversion;
  11409. #ifndef WOLFSSL_NO_TLS12
  11410. func_cb_client.method = wolfTLSv1_2_client_method;
  11411. #else
  11412. func_cb_client.method = wolfTLSv1_3_client_method;
  11413. #endif
  11414. #ifndef WOLFSSL_NO_TLS12
  11415. func_cb_server.method = wolfTLSv1_2_server_method;
  11416. #else
  11417. func_cb_server.method = wolfTLSv1_3_server_method;
  11418. #endif
  11419. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  11420. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11421. &func_cb_server, NULL), TEST_FAIL);
  11422. #else
  11423. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11424. &func_cb_server, NULL), TEST_SUCCESS);
  11425. #endif
  11426. #endif
  11427. return EXPECT_RESULT();
  11428. }
  11429. static int test_wolfSSL_X509_TLS_version_test_2(void)
  11430. {
  11431. EXPECT_DECLS;
  11432. #if !defined(NO_DH) && !defined(NO_AES) && defined(WOLFSSL_CERT_GEN) && \
  11433. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  11434. defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  11435. test_ssl_cbf func_cb_client;
  11436. test_ssl_cbf func_cb_server;
  11437. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  11438. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  11439. func_cb_client.ctx_ready = &test_set_x509_badversion;
  11440. func_cb_server.ctx_ready = &test_set_override_x509;
  11441. #ifndef WOLFSSL_NO_TLS12
  11442. func_cb_client.method = wolfTLSv1_2_client_method;
  11443. #else
  11444. func_cb_client.method = wolfTLSv1_3_client_method;
  11445. #endif
  11446. #ifndef WOLFSSL_NO_TLS12
  11447. func_cb_server.method = wolfTLSv1_2_server_method;
  11448. #else
  11449. func_cb_server.method = wolfTLSv1_3_server_method;
  11450. #endif
  11451. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  11452. &func_cb_server, NULL), TEST_SUCCESS);
  11453. #endif
  11454. return EXPECT_RESULT();
  11455. }
  11456. /* Testing function wolfSSL_CTX_SetMinVersion; sets the minimum downgrade
  11457. * version allowed.
  11458. * POST: 1 on success.
  11459. */
  11460. static int test_wolfSSL_CTX_SetMinVersion(void)
  11461. {
  11462. int res = TEST_SKIPPED;
  11463. #ifndef NO_WOLFSSL_CLIENT
  11464. int failFlag = WOLFSSL_SUCCESS;
  11465. WOLFSSL_CTX* ctx;
  11466. int itr;
  11467. #ifndef NO_OLD_TLS
  11468. const int versions[] = {
  11469. #ifdef WOLFSSL_ALLOW_TLSV10
  11470. WOLFSSL_TLSV1,
  11471. #endif
  11472. WOLFSSL_TLSV1_1,
  11473. WOLFSSL_TLSV1_2 };
  11474. #elif !defined(WOLFSSL_NO_TLS12)
  11475. const int versions[] = { WOLFSSL_TLSV1_2 };
  11476. #elif defined(WOLFSSL_TLS13)
  11477. const int versions[] = { WOLFSSL_TLSV1_3 };
  11478. #else
  11479. const int versions[0];
  11480. #endif
  11481. ctx = wolfSSL_CTX_new(wolfSSLv23_client_method());
  11482. for (itr = 0; itr < (int)(sizeof(versions)/sizeof(int)); itr++) {
  11483. if (wolfSSL_CTX_SetMinVersion(ctx, *(versions + itr))
  11484. != WOLFSSL_SUCCESS) {
  11485. failFlag = WOLFSSL_FAILURE;
  11486. }
  11487. }
  11488. wolfSSL_CTX_free(ctx);
  11489. res = TEST_RES_CHECK(failFlag == WOLFSSL_SUCCESS);
  11490. #endif
  11491. return res;
  11492. } /* END test_wolfSSL_CTX_SetMinVersion */
  11493. /*----------------------------------------------------------------------------*
  11494. | OCSP Stapling
  11495. *----------------------------------------------------------------------------*/
  11496. /* Testing wolfSSL_UseOCSPStapling function. OCSP stapling eliminates the need
  11497. * need to contact the CA, lowering the cost of cert revocation checking.
  11498. * PRE: HAVE_OCSP and HAVE_CERTIFICATE_STATUS_REQUEST
  11499. * POST: 1 returned for success.
  11500. */
  11501. static int test_wolfSSL_UseOCSPStapling(void)
  11502. {
  11503. EXPECT_DECLS;
  11504. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) && defined(HAVE_OCSP) && \
  11505. !defined(NO_WOLFSSL_CLIENT)
  11506. WOLFSSL_CTX* ctx = NULL;
  11507. WOLFSSL* ssl = NULL;
  11508. #ifndef NO_WOLFSSL_CLIENT
  11509. #ifndef WOLFSSL_NO_TLS12
  11510. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11511. #else
  11512. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11513. #endif
  11514. #else
  11515. #ifndef WOLFSSL_NO_TLS12
  11516. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11517. #else
  11518. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11519. #endif
  11520. #endif
  11521. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11522. ExpectIntEQ(wolfSSL_UseOCSPStapling(NULL, WOLFSSL_CSR2_OCSP,
  11523. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11524. #ifndef NO_WOLFSSL_CLIENT
  11525. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  11526. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  11527. #else
  11528. ExpectIntEQ(wolfSSL_UseOCSPStapling(ssl, WOLFSSL_CSR2_OCSP,
  11529. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11530. #endif
  11531. wolfSSL_free(ssl);
  11532. wolfSSL_CTX_free(ctx);
  11533. #endif
  11534. return EXPECT_RESULT();
  11535. } /* END test_wolfSSL_UseOCSPStapling */
  11536. /* Testing OCSP stapling version 2, wolfSSL_UseOCSPStaplingV2 function. OCSP
  11537. * stapling eliminates the need to contact the CA and lowers cert revocation
  11538. * check.
  11539. * PRE: HAVE_CERTIFICATE_STATUS_REQUEST_V2 and HAVE_OCSP defined.
  11540. */
  11541. static int test_wolfSSL_UseOCSPStaplingV2(void)
  11542. {
  11543. EXPECT_DECLS;
  11544. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2) && defined(HAVE_OCSP) && \
  11545. !defined(NO_WOLFSSL_CLIENT)
  11546. WOLFSSL_CTX* ctx = NULL;
  11547. WOLFSSL* ssl = NULL;
  11548. #ifndef NO_WOLFSSL_CLIENT
  11549. #ifndef WOLFSSL_NO_TLS12
  11550. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  11551. #else
  11552. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  11553. #endif
  11554. #else
  11555. #ifndef WOLFSSL_NO_TLS12
  11556. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method()));
  11557. #else
  11558. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  11559. #endif
  11560. #endif
  11561. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11562. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(NULL, WOLFSSL_CSR2_OCSP,
  11563. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11564. #ifndef NO_WOLFSSL_CLIENT
  11565. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  11566. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  11567. #else
  11568. ExpectIntEQ(wolfSSL_UseOCSPStaplingV2(ssl, WOLFSSL_CSR2_OCSP,
  11569. WOLFSSL_CSR2_OCSP_USE_NONCE), BAD_FUNC_ARG);
  11570. #endif
  11571. wolfSSL_free(ssl);
  11572. wolfSSL_CTX_free(ctx);
  11573. #endif
  11574. return EXPECT_RESULT();
  11575. } /* END test_wolfSSL_UseOCSPStaplingV2 */
  11576. /*----------------------------------------------------------------------------*
  11577. | Multicast Tests
  11578. *----------------------------------------------------------------------------*/
  11579. static int test_wolfSSL_mcast(void)
  11580. {
  11581. EXPECT_DECLS;
  11582. #if defined(WOLFSSL_DTLS) && defined(WOLFSSL_MULTICAST) && \
  11583. (defined(WOLFSSL_TLS13) || defined(WOLFSSL_SNIFFER))
  11584. WOLFSSL_CTX* ctx = NULL;
  11585. WOLFSSL* ssl = NULL;
  11586. byte preMasterSecret[512];
  11587. byte clientRandom[32];
  11588. byte serverRandom[32];
  11589. byte suite[2] = {0, 0xfe}; /* WDM_WITH_NULL_SHA256 */
  11590. byte buf[256];
  11591. word16 newId;
  11592. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  11593. ExpectIntEQ(wolfSSL_CTX_mcast_set_member_id(ctx, 0), WOLFSSL_SUCCESS);
  11594. ExpectNotNull(ssl = wolfSSL_new(ctx));
  11595. XMEMSET(preMasterSecret, 0x23, sizeof(preMasterSecret));
  11596. XMEMSET(clientRandom, 0xA5, sizeof(clientRandom));
  11597. XMEMSET(serverRandom, 0x5A, sizeof(serverRandom));
  11598. ExpectIntEQ(wolfSSL_set_secret(ssl, 23, preMasterSecret,
  11599. sizeof(preMasterSecret), clientRandom, serverRandom, suite),
  11600. WOLFSSL_SUCCESS);
  11601. ExpectIntLE(wolfSSL_mcast_read(ssl, &newId, buf, sizeof(buf)), 0);
  11602. ExpectIntLE(newId, 100);
  11603. wolfSSL_free(ssl);
  11604. wolfSSL_CTX_free(ctx);
  11605. #endif /* WOLFSSL_DTLS && WOLFSSL_MULTICAST && (WOLFSSL_TLS13 ||
  11606. * WOLFSSL_SNIFFER) */
  11607. return EXPECT_RESULT();
  11608. }
  11609. /*----------------------------------------------------------------------------*
  11610. | Wolfcrypt
  11611. *----------------------------------------------------------------------------*/
  11612. /*
  11613. * Unit test for the wc_InitBlake2b()
  11614. */
  11615. static int test_wc_InitBlake2b(void)
  11616. {
  11617. EXPECT_DECLS;
  11618. #ifdef HAVE_BLAKE2
  11619. Blake2b blake;
  11620. /* Test good arg. */
  11621. ExpectIntEQ(wc_InitBlake2b(&blake, 64), 0);
  11622. /* Test bad arg. */
  11623. ExpectIntEQ(wc_InitBlake2b(NULL, 64), BAD_FUNC_ARG);
  11624. ExpectIntEQ(wc_InitBlake2b(NULL, 128), BAD_FUNC_ARG);
  11625. ExpectIntEQ(wc_InitBlake2b(&blake, 128), BAD_FUNC_ARG);
  11626. ExpectIntEQ(wc_InitBlake2b(NULL, 0), BAD_FUNC_ARG);
  11627. ExpectIntEQ(wc_InitBlake2b(&blake, 0), BAD_FUNC_ARG);
  11628. #endif
  11629. return EXPECT_RESULT();
  11630. } /* END test_wc_InitBlake2b*/
  11631. /*
  11632. * Unit test for the wc_InitBlake2b_WithKey()
  11633. */
  11634. static int test_wc_InitBlake2b_WithKey(void)
  11635. {
  11636. EXPECT_DECLS;
  11637. #ifdef HAVE_BLAKE2
  11638. Blake2b blake;
  11639. word32 digestSz = BLAKE2B_KEYBYTES;
  11640. byte key[BLAKE2B_KEYBYTES];
  11641. word32 keylen = BLAKE2B_KEYBYTES;
  11642. XMEMSET(key, 0, sizeof(key));
  11643. /* Test good arg. */
  11644. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, keylen), 0);
  11645. /* Test bad args. */
  11646. ExpectIntEQ(wc_InitBlake2b_WithKey(NULL, digestSz, key, keylen),
  11647. BAD_FUNC_ARG);
  11648. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, key, 256),
  11649. BAD_FUNC_ARG);
  11650. ExpectIntEQ(wc_InitBlake2b_WithKey(&blake, digestSz, NULL, keylen), 0);
  11651. #endif
  11652. return EXPECT_RESULT();
  11653. } /* END wc_InitBlake2b_WithKey*/
  11654. /*
  11655. * Unit test for the wc_InitBlake2s_WithKey()
  11656. */
  11657. static int test_wc_InitBlake2s_WithKey(void)
  11658. {
  11659. EXPECT_DECLS;
  11660. #ifdef HAVE_BLAKE2S
  11661. Blake2s blake;
  11662. word32 digestSz = BLAKE2S_KEYBYTES;
  11663. byte *key = (byte*)"01234567890123456789012345678901";
  11664. word32 keylen = BLAKE2S_KEYBYTES;
  11665. /* Test good arg. */
  11666. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, keylen), 0);
  11667. /* Test bad args. */
  11668. ExpectIntEQ(wc_InitBlake2s_WithKey(NULL, digestSz, key, keylen),
  11669. BAD_FUNC_ARG);
  11670. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, key, 256),
  11671. BAD_FUNC_ARG);
  11672. ExpectIntEQ(wc_InitBlake2s_WithKey(&blake, digestSz, NULL, keylen), 0);
  11673. #endif
  11674. return EXPECT_RESULT();
  11675. } /* END wc_InitBlake2s_WithKey*/
  11676. /*
  11677. * Unit test for the wc_InitMd5()
  11678. */
  11679. static int test_wc_InitMd5(void)
  11680. {
  11681. EXPECT_DECLS;
  11682. #ifndef NO_MD5
  11683. wc_Md5 md5;
  11684. /* Test good arg. */
  11685. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11686. /* Test bad arg. */
  11687. ExpectIntEQ(wc_InitMd5(NULL), BAD_FUNC_ARG);
  11688. wc_Md5Free(&md5);
  11689. #endif
  11690. return EXPECT_RESULT();
  11691. } /* END test_wc_InitMd5 */
  11692. /*
  11693. * Testing wc_UpdateMd5()
  11694. */
  11695. static int test_wc_Md5Update(void)
  11696. {
  11697. EXPECT_DECLS;
  11698. #ifndef NO_MD5
  11699. wc_Md5 md5;
  11700. byte hash[WC_MD5_DIGEST_SIZE];
  11701. testVector a, b, c;
  11702. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11703. /* Input */
  11704. a.input = "a";
  11705. a.inLen = XSTRLEN(a.input);
  11706. ExpectIntEQ(wc_Md5Update(&md5, (byte*)a.input, (word32)a.inLen), 0);
  11707. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  11708. /* Update input. */
  11709. a.input = "abc";
  11710. a.output = "\x90\x01\x50\x98\x3c\xd2\x4f\xb0\xd6\x96\x3f\x7d\x28\xe1\x7f"
  11711. "\x72";
  11712. a.inLen = XSTRLEN(a.input);
  11713. a.outLen = XSTRLEN(a.output);
  11714. ExpectIntEQ(wc_Md5Update(&md5, (byte*) a.input, (word32) a.inLen), 0);
  11715. ExpectIntEQ(wc_Md5Final(&md5, hash), 0);
  11716. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  11717. /* Pass in bad values. */
  11718. b.input = NULL;
  11719. b.inLen = 0;
  11720. ExpectIntEQ(wc_Md5Update(&md5, (byte*)b.input, (word32)b.inLen), 0);
  11721. c.input = NULL;
  11722. c.inLen = WC_MD5_DIGEST_SIZE;
  11723. ExpectIntEQ(wc_Md5Update(&md5, (byte*)c.input, (word32)c.inLen),
  11724. BAD_FUNC_ARG);
  11725. ExpectIntEQ(wc_Md5Update(NULL, (byte*)a.input, (word32)a.inLen),
  11726. BAD_FUNC_ARG);
  11727. wc_Md5Free(&md5);
  11728. #endif
  11729. return EXPECT_RESULT();
  11730. } /* END test_wc_Md5Update() */
  11731. /*
  11732. * Unit test on wc_Md5Final() in wolfcrypt/src/md5.c
  11733. */
  11734. static int test_wc_Md5Final(void)
  11735. {
  11736. EXPECT_DECLS;
  11737. #ifndef NO_MD5
  11738. /* Instantiate */
  11739. wc_Md5 md5;
  11740. byte* hash_test[3];
  11741. byte hash1[WC_MD5_DIGEST_SIZE];
  11742. byte hash2[2*WC_MD5_DIGEST_SIZE];
  11743. byte hash3[5*WC_MD5_DIGEST_SIZE];
  11744. int times, i;
  11745. /* Initialize */
  11746. ExpectIntEQ(wc_InitMd5(&md5), 0);
  11747. hash_test[0] = hash1;
  11748. hash_test[1] = hash2;
  11749. hash_test[2] = hash3;
  11750. times = sizeof(hash_test)/sizeof(byte*);
  11751. for (i = 0; i < times; i++) {
  11752. ExpectIntEQ(wc_Md5Final(&md5, hash_test[i]), 0);
  11753. }
  11754. /* Test bad args. */
  11755. ExpectIntEQ(wc_Md5Final(NULL, NULL), BAD_FUNC_ARG);
  11756. ExpectIntEQ(wc_Md5Final(NULL, hash1), BAD_FUNC_ARG);
  11757. ExpectIntEQ(wc_Md5Final(&md5, NULL), BAD_FUNC_ARG);
  11758. wc_Md5Free(&md5);
  11759. #endif
  11760. return EXPECT_RESULT();
  11761. }
  11762. /*
  11763. * Unit test for the wc_InitSha()
  11764. */
  11765. static int test_wc_InitSha(void)
  11766. {
  11767. EXPECT_DECLS;
  11768. #ifndef NO_SHA
  11769. wc_Sha sha;
  11770. /* Test good arg. */
  11771. ExpectIntEQ(wc_InitSha(&sha), 0);
  11772. /* Test bad arg. */
  11773. ExpectIntEQ(wc_InitSha(NULL), BAD_FUNC_ARG);
  11774. wc_ShaFree(&sha);
  11775. #endif
  11776. return EXPECT_RESULT();
  11777. } /* END test_wc_InitSha */
  11778. /*
  11779. * Tesing wc_ShaUpdate()
  11780. */
  11781. static int test_wc_ShaUpdate(void)
  11782. {
  11783. EXPECT_DECLS;
  11784. #ifndef NO_SHA
  11785. wc_Sha sha;
  11786. byte hash[WC_SHA_DIGEST_SIZE];
  11787. testVector a, b, c;
  11788. ExpectIntEQ(wc_InitSha(&sha), 0);
  11789. /* Input. */
  11790. a.input = "a";
  11791. a.inLen = XSTRLEN(a.input);
  11792. ExpectIntEQ(wc_ShaUpdate(&sha, NULL, 0), 0);
  11793. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, 0), 0);
  11794. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  11795. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  11796. /* Update input. */
  11797. a.input = "abc";
  11798. a.output = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E\x25\x71\x78\x50\xC2"
  11799. "\x6C\x9C\xD0\xD8\x9D";
  11800. a.inLen = XSTRLEN(a.input);
  11801. a.outLen = XSTRLEN(a.output);
  11802. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)a.input, (word32)a.inLen), 0);
  11803. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  11804. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  11805. /* Try passing in bad values. */
  11806. b.input = NULL;
  11807. b.inLen = 0;
  11808. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)b.input, (word32)b.inLen), 0);
  11809. c.input = NULL;
  11810. c.inLen = WC_SHA_DIGEST_SIZE;
  11811. ExpectIntEQ(wc_ShaUpdate(&sha, (byte*)c.input, (word32)c.inLen),
  11812. BAD_FUNC_ARG);
  11813. ExpectIntEQ(wc_ShaUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  11814. BAD_FUNC_ARG);
  11815. wc_ShaFree(&sha);
  11816. #endif
  11817. return EXPECT_RESULT();
  11818. } /* END test_wc_ShaUpdate() */
  11819. /*
  11820. * Unit test on wc_ShaFinal
  11821. */
  11822. static int test_wc_ShaFinal(void)
  11823. {
  11824. EXPECT_DECLS;
  11825. #ifndef NO_SHA
  11826. wc_Sha sha;
  11827. byte* hash_test[3];
  11828. byte hash1[WC_SHA_DIGEST_SIZE];
  11829. byte hash2[2*WC_SHA_DIGEST_SIZE];
  11830. byte hash3[5*WC_SHA_DIGEST_SIZE];
  11831. int times, i;
  11832. /* Initialize*/
  11833. ExpectIntEQ(wc_InitSha(&sha), 0);
  11834. hash_test[0] = hash1;
  11835. hash_test[1] = hash2;
  11836. hash_test[2] = hash3;
  11837. times = sizeof(hash_test)/sizeof(byte*);
  11838. for (i = 0; i < times; i++) {
  11839. ExpectIntEQ(wc_ShaFinal(&sha, hash_test[i]), 0);
  11840. }
  11841. /* Test bad args. */
  11842. ExpectIntEQ(wc_ShaFinal(NULL, NULL), BAD_FUNC_ARG);
  11843. ExpectIntEQ(wc_ShaFinal(NULL, hash1), BAD_FUNC_ARG);
  11844. ExpectIntEQ(wc_ShaFinal(&sha, NULL), BAD_FUNC_ARG);
  11845. wc_ShaFree(&sha);
  11846. #endif
  11847. return EXPECT_RESULT();
  11848. } /* END test_wc_ShaFinal */
  11849. /*
  11850. * Unit test for wc_InitSha256()
  11851. */
  11852. static int test_wc_InitSha256(void)
  11853. {
  11854. EXPECT_DECLS;
  11855. #ifndef NO_SHA256
  11856. wc_Sha256 sha256;
  11857. /* Test good arg. */
  11858. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11859. /* Test bad arg. */
  11860. ExpectIntEQ(wc_InitSha256(NULL), BAD_FUNC_ARG);
  11861. wc_Sha256Free(&sha256);
  11862. #endif
  11863. return EXPECT_RESULT();
  11864. } /* END test_wc_InitSha256 */
  11865. /*
  11866. * Unit test for wc_Sha256Update()
  11867. */
  11868. static int test_wc_Sha256Update(void)
  11869. {
  11870. EXPECT_DECLS;
  11871. #ifndef NO_SHA256
  11872. wc_Sha256 sha256;
  11873. byte hash[WC_SHA256_DIGEST_SIZE];
  11874. byte hash_unaligned[WC_SHA256_DIGEST_SIZE+1];
  11875. testVector a, b, c;
  11876. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11877. /* Input. */
  11878. a.input = "a";
  11879. a.inLen = XSTRLEN(a.input);
  11880. ExpectIntEQ(wc_Sha256Update(&sha256, NULL, 0), 0);
  11881. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, 0), 0);
  11882. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  11883. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  11884. /* Update input. */
  11885. a.input = "abc";
  11886. a.output = "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  11887. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  11888. "\x15\xAD";
  11889. a.inLen = XSTRLEN(a.input);
  11890. a.outLen = XSTRLEN(a.output);
  11891. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input, (word32)a.inLen), 0);
  11892. ExpectIntEQ(wc_Sha256Final(&sha256, hash), 0);
  11893. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  11894. /* Unaligned check. */
  11895. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)a.input+1, (word32)a.inLen-1),
  11896. 0);
  11897. ExpectIntEQ(wc_Sha256Final(&sha256, hash_unaligned + 1), 0);
  11898. /* Try passing in bad values */
  11899. b.input = NULL;
  11900. b.inLen = 0;
  11901. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)b.input, (word32)b.inLen), 0);
  11902. c.input = NULL;
  11903. c.inLen = WC_SHA256_DIGEST_SIZE;
  11904. ExpectIntEQ(wc_Sha256Update(&sha256, (byte*)c.input, (word32)c.inLen),
  11905. BAD_FUNC_ARG);
  11906. ExpectIntEQ(wc_Sha256Update(NULL, (byte*)a.input, (word32)a.inLen),
  11907. BAD_FUNC_ARG);
  11908. wc_Sha256Free(&sha256);
  11909. #endif
  11910. return EXPECT_RESULT();
  11911. } /* END test_wc_Sha256Update */
  11912. /*
  11913. * Unit test function for wc_Sha256Final()
  11914. */
  11915. static int test_wc_Sha256Final(void)
  11916. {
  11917. EXPECT_DECLS;
  11918. #ifndef NO_SHA256
  11919. wc_Sha256 sha256;
  11920. byte* hash_test[3];
  11921. byte hash1[WC_SHA256_DIGEST_SIZE];
  11922. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  11923. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  11924. int times, i;
  11925. /* Initialize */
  11926. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11927. hash_test[0] = hash1;
  11928. hash_test[1] = hash2;
  11929. hash_test[2] = hash3;
  11930. times = sizeof(hash_test) / sizeof(byte*);
  11931. for (i = 0; i < times; i++) {
  11932. ExpectIntEQ(wc_Sha256Final(&sha256, hash_test[i]), 0);
  11933. }
  11934. /* Test bad args. */
  11935. ExpectIntEQ(wc_Sha256Final(NULL, NULL), BAD_FUNC_ARG);
  11936. ExpectIntEQ(wc_Sha256Final(NULL, hash1), BAD_FUNC_ARG);
  11937. ExpectIntEQ(wc_Sha256Final(&sha256, NULL), BAD_FUNC_ARG);
  11938. wc_Sha256Free(&sha256);
  11939. #endif
  11940. return EXPECT_RESULT();
  11941. } /* END test_wc_Sha256Final */
  11942. /*
  11943. * Unit test function for wc_Sha256FinalRaw()
  11944. */
  11945. static int test_wc_Sha256FinalRaw(void)
  11946. {
  11947. EXPECT_DECLS;
  11948. #if !defined(NO_SHA256) && !defined(HAVE_SELFTEST) && !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  11949. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  11950. !defined(WOLFSSL_NO_HASH_RAW)
  11951. wc_Sha256 sha256;
  11952. byte* hash_test[3];
  11953. byte hash1[WC_SHA256_DIGEST_SIZE];
  11954. byte hash2[2*WC_SHA256_DIGEST_SIZE];
  11955. byte hash3[5*WC_SHA256_DIGEST_SIZE];
  11956. int times, i;
  11957. /* Initialize */
  11958. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11959. hash_test[0] = hash1;
  11960. hash_test[1] = hash2;
  11961. hash_test[2] = hash3;
  11962. times = sizeof(hash_test) / sizeof(byte*);
  11963. for (i = 0; i < times; i++) {
  11964. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, hash_test[i]), 0);
  11965. }
  11966. /* Test bad args. */
  11967. ExpectIntEQ(wc_Sha256FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  11968. ExpectIntEQ(wc_Sha256FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  11969. ExpectIntEQ(wc_Sha256FinalRaw(&sha256, NULL), BAD_FUNC_ARG);
  11970. wc_Sha256Free(&sha256);
  11971. #endif
  11972. return EXPECT_RESULT();
  11973. } /* END test_wc_Sha256FinalRaw */
  11974. /*
  11975. * Unit test function for wc_Sha256GetFlags()
  11976. */
  11977. static int test_wc_Sha256GetFlags(void)
  11978. {
  11979. EXPECT_DECLS;
  11980. #if !defined(NO_SHA256) && defined(WOLFSSL_HASH_FLAGS)
  11981. wc_Sha256 sha256;
  11982. word32 flags = 0;
  11983. /* Initialize */
  11984. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  11985. ExpectIntEQ(wc_Sha256GetFlags(&sha256, &flags), 0);
  11986. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  11987. wc_Sha256Free(&sha256);
  11988. #endif
  11989. return EXPECT_RESULT();
  11990. } /* END test_wc_Sha256GetFlags */
  11991. /*
  11992. * Unit test function for wc_Sha256Free()
  11993. */
  11994. static int test_wc_Sha256Free(void)
  11995. {
  11996. EXPECT_DECLS;
  11997. #ifndef NO_SHA256
  11998. wc_Sha256Free(NULL);
  11999. /* Set result to SUCCESS. */
  12000. ExpectTrue(1);
  12001. #endif
  12002. return EXPECT_RESULT();
  12003. } /* END test_wc_Sha256Free */
  12004. /*
  12005. * Unit test function for wc_Sha256GetHash()
  12006. */
  12007. static int test_wc_Sha256GetHash(void)
  12008. {
  12009. EXPECT_DECLS;
  12010. #ifndef NO_SHA256
  12011. wc_Sha256 sha256;
  12012. byte hash1[WC_SHA256_DIGEST_SIZE];
  12013. /* Initialize */
  12014. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  12015. ExpectIntEQ(wc_Sha256GetHash(&sha256, hash1), 0);
  12016. /* test bad arguments*/
  12017. ExpectIntEQ(wc_Sha256GetHash(NULL, NULL), BAD_FUNC_ARG);
  12018. ExpectIntEQ(wc_Sha256GetHash(NULL, hash1), BAD_FUNC_ARG);
  12019. ExpectIntEQ(wc_Sha256GetHash(&sha256, NULL), BAD_FUNC_ARG);
  12020. wc_Sha256Free(&sha256);
  12021. #endif
  12022. return EXPECT_RESULT();
  12023. } /* END test_wc_Sha256GetHash */
  12024. /*
  12025. * Unit test function for wc_Sha256Copy()
  12026. */
  12027. static int test_wc_Sha256Copy(void)
  12028. {
  12029. EXPECT_DECLS;
  12030. #ifndef NO_SHA256
  12031. wc_Sha256 sha256;
  12032. wc_Sha256 temp;
  12033. XMEMSET(&sha256, 0, sizeof(sha256));
  12034. XMEMSET(&temp, 0, sizeof(temp));
  12035. /* Initialize */
  12036. ExpectIntEQ(wc_InitSha256(&sha256), 0);
  12037. ExpectIntEQ(wc_InitSha256(&temp), 0);
  12038. ExpectIntEQ(wc_Sha256Copy(&sha256, &temp), 0);
  12039. /* test bad arguments*/
  12040. ExpectIntEQ(wc_Sha256Copy(NULL, NULL), BAD_FUNC_ARG);
  12041. ExpectIntEQ(wc_Sha256Copy(NULL, &temp), BAD_FUNC_ARG);
  12042. ExpectIntEQ(wc_Sha256Copy(&sha256, NULL), BAD_FUNC_ARG);
  12043. wc_Sha256Free(&sha256);
  12044. wc_Sha256Free(&temp);
  12045. #endif
  12046. return EXPECT_RESULT();
  12047. } /* END test_wc_Sha256Copy */
  12048. /*
  12049. * Testing wc_InitSha512()
  12050. */
  12051. static int test_wc_InitSha512(void)
  12052. {
  12053. EXPECT_DECLS;
  12054. #ifdef WOLFSSL_SHA512
  12055. wc_Sha512 sha512;
  12056. /* Test good arg. */
  12057. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12058. /* Test bad arg. */
  12059. ExpectIntEQ(wc_InitSha512(NULL), BAD_FUNC_ARG);
  12060. wc_Sha512Free(&sha512);
  12061. #endif
  12062. return EXPECT_RESULT();
  12063. } /* END test_wc_InitSha512 */
  12064. /*
  12065. * wc_Sha512Update() test.
  12066. */
  12067. static int test_wc_Sha512Update(void)
  12068. {
  12069. EXPECT_DECLS;
  12070. #ifdef WOLFSSL_SHA512
  12071. wc_Sha512 sha512;
  12072. byte hash[WC_SHA512_DIGEST_SIZE];
  12073. byte hash_unaligned[WC_SHA512_DIGEST_SIZE + 1];
  12074. testVector a, b, c;
  12075. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12076. /* Input. */
  12077. a.input = "a";
  12078. a.inLen = XSTRLEN(a.input);
  12079. ExpectIntEQ(wc_Sha512Update(&sha512, NULL, 0), 0);
  12080. ExpectIntEQ(wc_Sha512Update(&sha512,(byte*)a.input, 0), 0);
  12081. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input, (word32)a.inLen), 0);
  12082. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  12083. /* Update input. */
  12084. a.input = "abc";
  12085. a.output = "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  12086. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b"
  12087. "\x55\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c"
  12088. "\x23\xa3\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a"
  12089. "\x9a\xc9\x4f\xa5\x4c\xa4\x9f";
  12090. a.inLen = XSTRLEN(a.input);
  12091. a.outLen = XSTRLEN(a.output);
  12092. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*) a.input, (word32) a.inLen), 0);
  12093. ExpectIntEQ(wc_Sha512Final(&sha512, hash), 0);
  12094. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_DIGEST_SIZE), 0);
  12095. /* Unaligned check. */
  12096. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)a.input+1, (word32)a.inLen-1),
  12097. 0);
  12098. ExpectIntEQ(wc_Sha512Final(&sha512, hash_unaligned+1), 0);
  12099. /* Try passing in bad values */
  12100. b.input = NULL;
  12101. b.inLen = 0;
  12102. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)b.input, (word32)b.inLen), 0);
  12103. c.input = NULL;
  12104. c.inLen = WC_SHA512_DIGEST_SIZE;
  12105. ExpectIntEQ(wc_Sha512Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12106. BAD_FUNC_ARG);
  12107. ExpectIntEQ(wc_Sha512Update(NULL, (byte*)a.input, (word32)a.inLen),
  12108. BAD_FUNC_ARG);
  12109. wc_Sha512Free(&sha512);
  12110. #endif
  12111. return EXPECT_RESULT();
  12112. } /* END test_wc_Sha512Update */
  12113. #ifdef WOLFSSL_SHA512
  12114. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12115. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  12116. /* Performs test for
  12117. * - wc_Sha512Final/wc_Sha512FinalRaw
  12118. * - wc_Sha512_224Final/wc_Sha512_224Final
  12119. * - wc_Sha512_256Final/wc_Sha512_256Final
  12120. * parameter:
  12121. * - type : must be one of WC_HASH_TYPE_SHA512, WC_HASH_TYPE_SHA512_224 or
  12122. * WC_HASH_TYPE_SHA512_256
  12123. * - isRaw: if is non-zero, xxxFinalRaw function will be tested
  12124. *return 0 on success
  12125. */
  12126. static int test_Sha512_Family_Final(int type, int isRaw)
  12127. {
  12128. EXPECT_DECLS;
  12129. wc_Sha512 sha512;
  12130. byte* hash_test[3];
  12131. byte hash1[WC_SHA512_DIGEST_SIZE];
  12132. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12133. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12134. int times, i;
  12135. int(*initFp)(wc_Sha512*);
  12136. int(*finalFp)(wc_Sha512*, byte*);
  12137. void(*freeFp)(wc_Sha512*);
  12138. if (type == WC_HASH_TYPE_SHA512) {
  12139. initFp = wc_InitSha512;
  12140. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12141. !defined(WOLFSSL_NO_HASH_RAW)
  12142. finalFp = (isRaw)? wc_Sha512FinalRaw : wc_Sha512Final;
  12143. #else
  12144. finalFp = (isRaw)? NULL : wc_Sha512Final;
  12145. #endif
  12146. freeFp = wc_Sha512Free;
  12147. }
  12148. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12149. #if !defined(WOLFSSL_NOSHA512_224)
  12150. else if (type == WC_HASH_TYPE_SHA512_224) {
  12151. initFp = wc_InitSha512_224;
  12152. #if !defined(WOLFSSL_NO_HASH_RAW)
  12153. finalFp = (isRaw)? wc_Sha512_224FinalRaw : wc_Sha512_224Final;
  12154. #else
  12155. finalFp = (isRaw)? NULL : wc_Sha512_224Final;
  12156. #endif
  12157. freeFp = wc_Sha512_224Free;
  12158. }
  12159. #endif
  12160. #if !defined(WOLFSSL_NOSHA512_256)
  12161. else if (type == WC_HASH_TYPE_SHA512_256) {
  12162. initFp = wc_InitSha512_256;
  12163. #if !defined(WOLFSSL_NO_HASH_RAW)
  12164. finalFp = (isRaw)? wc_Sha512_256FinalRaw : wc_Sha512_256Final;
  12165. #else
  12166. finalFp = (isRaw)? NULL : wc_Sha512_256Final;
  12167. #endif
  12168. freeFp = wc_Sha512_256Free;
  12169. }
  12170. #endif
  12171. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12172. else
  12173. return TEST_FAIL;
  12174. /* Initialize */
  12175. ExpectIntEQ(initFp(&sha512), 0);
  12176. hash_test[0] = hash1;
  12177. hash_test[1] = hash2;
  12178. hash_test[2] = hash3;
  12179. times = sizeof(hash_test) / sizeof(byte *);
  12180. /* Good test args. */
  12181. for (i = 0; i < times; i++) {
  12182. ExpectIntEQ(finalFp(&sha512, hash_test[i]), 0);
  12183. }
  12184. /* Test bad args. */
  12185. ExpectIntEQ(finalFp(NULL, NULL), BAD_FUNC_ARG);
  12186. ExpectIntEQ(finalFp(NULL, hash1), BAD_FUNC_ARG);
  12187. ExpectIntEQ(finalFp(&sha512, NULL), BAD_FUNC_ARG);
  12188. freeFp(&sha512);
  12189. return EXPECT_RESULT();
  12190. }
  12191. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  12192. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  12193. #endif /* WOLFSSL_SHA512 */
  12194. /*
  12195. * Unit test function for wc_Sha512Final()
  12196. */
  12197. static int test_wc_Sha512Final(void)
  12198. {
  12199. EXPECT_DECLS;
  12200. #ifdef WOLFSSL_SHA512
  12201. wc_Sha512 sha512;
  12202. byte* hash_test[3];
  12203. byte hash1[WC_SHA512_DIGEST_SIZE];
  12204. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12205. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12206. int times, i;
  12207. /* Initialize */
  12208. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12209. hash_test[0] = hash1;
  12210. hash_test[1] = hash2;
  12211. hash_test[2] = hash3;
  12212. times = sizeof(hash_test) / sizeof(byte *);
  12213. for (i = 0; i < times; i++) {
  12214. ExpectIntEQ(wc_Sha512Final(&sha512, hash_test[i]), 0);
  12215. }
  12216. /* Test bad args. */
  12217. ExpectIntEQ(wc_Sha512Final(NULL, NULL), BAD_FUNC_ARG);
  12218. ExpectIntEQ(wc_Sha512Final(NULL, hash1), BAD_FUNC_ARG);
  12219. ExpectIntEQ(wc_Sha512Final(&sha512, NULL), BAD_FUNC_ARG);
  12220. wc_Sha512Free(&sha512);
  12221. #endif
  12222. return EXPECT_RESULT();
  12223. } /* END test_wc_Sha512Final */
  12224. /*
  12225. * Unit test function for wc_Sha512GetFlags()
  12226. */
  12227. static int test_wc_Sha512GetFlags(void)
  12228. {
  12229. EXPECT_DECLS;
  12230. #if defined(WOLFSSL_SHA512) && defined(WOLFSSL_HASH_FLAGS)
  12231. wc_Sha512 sha512;
  12232. word32 flags = 0;
  12233. /* Initialize */
  12234. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12235. ExpectIntEQ(wc_Sha512GetFlags(&sha512, &flags), 0);
  12236. ExpectIntEQ((flags & WC_HASH_FLAG_ISCOPY), 0);
  12237. wc_Sha512Free(&sha512);
  12238. #endif
  12239. return EXPECT_RESULT();
  12240. } /* END test_wc_Sha512GetFlags */
  12241. /*
  12242. * Unit test function for wc_Sha512FinalRaw()
  12243. */
  12244. static int test_wc_Sha512FinalRaw(void)
  12245. {
  12246. EXPECT_DECLS;
  12247. #if (defined(WOLFSSL_SHA512) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  12248. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  12249. !defined(WOLFSSL_NO_HASH_RAW)
  12250. wc_Sha512 sha512;
  12251. byte* hash_test[3];
  12252. byte hash1[WC_SHA512_DIGEST_SIZE];
  12253. byte hash2[2*WC_SHA512_DIGEST_SIZE];
  12254. byte hash3[5*WC_SHA512_DIGEST_SIZE];
  12255. int times, i;
  12256. /* Initialize */
  12257. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12258. hash_test[0] = hash1;
  12259. hash_test[1] = hash2;
  12260. hash_test[2] = hash3;
  12261. times = sizeof(hash_test) / sizeof(byte*);
  12262. /* Good test args. */
  12263. for (i = 0; i < times; i++) {
  12264. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, hash_test[i]), 0);
  12265. }
  12266. /* Test bad args. */
  12267. ExpectIntEQ(wc_Sha512FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12268. ExpectIntEQ(wc_Sha512FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12269. ExpectIntEQ(wc_Sha512FinalRaw(&sha512, NULL), BAD_FUNC_ARG);
  12270. wc_Sha512Free(&sha512);
  12271. #endif
  12272. return EXPECT_RESULT();
  12273. } /* END test_wc_Sha512FinalRaw */
  12274. /*
  12275. * Unit test function for wc_Sha512Free()
  12276. */
  12277. static int test_wc_Sha512Free(void)
  12278. {
  12279. EXPECT_DECLS;
  12280. #ifdef WOLFSSL_SHA512
  12281. wc_Sha512Free(NULL);
  12282. /* Set result to SUCCESS. */
  12283. ExpectTrue(1);
  12284. #endif
  12285. return EXPECT_RESULT();
  12286. } /* END test_wc_Sha512Free */
  12287. #ifdef WOLFSSL_SHA512
  12288. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12289. (!defined(WOLFSSL_NOSHA512_224) || !defined(WOLFSSL_NOSHA512_256))
  12290. static int test_Sha512_Family_GetHash(int type )
  12291. {
  12292. EXPECT_DECLS;
  12293. int(*initFp)(wc_Sha512*);
  12294. int(*ghashFp)(wc_Sha512*, byte*);
  12295. wc_Sha512 sha512;
  12296. byte hash1[WC_SHA512_DIGEST_SIZE];
  12297. if (type == WC_HASH_TYPE_SHA512) {
  12298. initFp = wc_InitSha512;
  12299. ghashFp = wc_Sha512GetHash;
  12300. }
  12301. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12302. #if !defined(WOLFSSL_NOSHA512_224)
  12303. else if (type == WC_HASH_TYPE_SHA512_224) {
  12304. initFp = wc_InitSha512_224;
  12305. ghashFp = wc_Sha512_224GetHash;
  12306. }
  12307. #endif
  12308. #if !defined(WOLFSSL_NOSHA512_256)
  12309. else if (type == WC_HASH_TYPE_SHA512_256) {
  12310. initFp = wc_InitSha512_256;
  12311. ghashFp = wc_Sha512_256GetHash;
  12312. }
  12313. #endif
  12314. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12315. else {
  12316. initFp = NULL;
  12317. ghashFp = NULL;
  12318. }
  12319. if (initFp == NULL || ghashFp == NULL)
  12320. return TEST_FAIL;
  12321. ExpectIntEQ(initFp(&sha512), 0);
  12322. ExpectIntEQ(ghashFp(&sha512, hash1), 0);
  12323. /* test bad arguments*/
  12324. ExpectIntEQ(ghashFp(NULL, NULL), BAD_FUNC_ARG);
  12325. ExpectIntEQ(ghashFp(NULL, hash1), BAD_FUNC_ARG);
  12326. ExpectIntEQ(ghashFp(&sha512, NULL), BAD_FUNC_ARG);
  12327. wc_Sha512Free(&sha512);
  12328. return EXPECT_RESULT();
  12329. }
  12330. #endif /* !HAVE_FIPS && !HAVE_SELFTEST &&
  12331. (!WOLFSSL_NOSHA512_224 || !WOLFSSL_NOSHA512_256) */
  12332. #endif /* WOLFSSL_SHA512 */
  12333. /*
  12334. * Unit test function for wc_Sha512GetHash()
  12335. */
  12336. static int test_wc_Sha512GetHash(void)
  12337. {
  12338. EXPECT_DECLS;
  12339. #ifdef WOLFSSL_SHA512
  12340. wc_Sha512 sha512;
  12341. byte hash1[WC_SHA512_DIGEST_SIZE];
  12342. /* Initialize */
  12343. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12344. ExpectIntEQ(wc_Sha512GetHash(&sha512, hash1), 0);
  12345. /* test bad arguments*/
  12346. ExpectIntEQ(wc_Sha512GetHash(NULL, NULL), BAD_FUNC_ARG);
  12347. ExpectIntEQ(wc_Sha512GetHash(NULL, hash1), BAD_FUNC_ARG);
  12348. ExpectIntEQ(wc_Sha512GetHash(&sha512, NULL), BAD_FUNC_ARG);
  12349. wc_Sha512Free(&sha512);
  12350. #endif
  12351. return EXPECT_RESULT();
  12352. } /* END test_wc_Sha512GetHash */
  12353. /*
  12354. * Unit test function for wc_Sha512Copy()
  12355. */
  12356. static int test_wc_Sha512Copy(void)
  12357. {
  12358. EXPECT_DECLS;
  12359. #ifdef WOLFSSL_SHA512
  12360. wc_Sha512 sha512;
  12361. wc_Sha512 temp;
  12362. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12363. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12364. /* Initialize */
  12365. ExpectIntEQ(wc_InitSha512(&sha512), 0);
  12366. ExpectIntEQ(wc_InitSha512(&temp), 0);
  12367. ExpectIntEQ(wc_Sha512Copy(&sha512, &temp), 0);
  12368. /* test bad arguments*/
  12369. ExpectIntEQ(wc_Sha512Copy(NULL, NULL), BAD_FUNC_ARG);
  12370. ExpectIntEQ(wc_Sha512Copy(NULL, &temp), BAD_FUNC_ARG);
  12371. ExpectIntEQ(wc_Sha512Copy(&sha512, NULL), BAD_FUNC_ARG);
  12372. wc_Sha512Free(&sha512);
  12373. wc_Sha512Free(&temp);
  12374. #endif
  12375. return EXPECT_RESULT();
  12376. } /* END test_wc_Sha512Copy */
  12377. static int test_wc_InitSha512_224(void)
  12378. {
  12379. EXPECT_DECLS;
  12380. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12381. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12382. wc_Sha512 sha512;
  12383. /* Test good arg. */
  12384. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12385. /* Test bad arg. */
  12386. ExpectIntEQ(wc_InitSha512_224(NULL), BAD_FUNC_ARG);
  12387. wc_Sha512_224Free(&sha512);
  12388. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12389. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12390. return EXPECT_RESULT();
  12391. }
  12392. static int test_wc_Sha512_224Update(void)
  12393. {
  12394. EXPECT_DECLS;
  12395. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12396. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12397. wc_Sha512 sha512;
  12398. byte hash[WC_SHA512_DIGEST_SIZE];
  12399. testVector a, c;
  12400. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12401. /* Input. */
  12402. a.input = "a";
  12403. a.inLen = XSTRLEN(a.input);
  12404. ExpectIntEQ(wc_Sha512_224Update(&sha512, NULL, 0), 0);
  12405. ExpectIntEQ(wc_Sha512_224Update(&sha512,(byte*)a.input, 0), 0);
  12406. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)a.input, (word32)a.inLen),
  12407. 0);
  12408. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  12409. /* Update input. */
  12410. a.input = "abc";
  12411. a.output = "\x46\x34\x27\x0f\x70\x7b\x6a\x54\xda\xae\x75\x30\x46\x08"
  12412. "\x42\xe2\x0e\x37\xed\x26\x5c\xee\xe9\xa4\x3e\x89\x24\xaa";
  12413. a.inLen = XSTRLEN(a.input);
  12414. a.outLen = XSTRLEN(a.output);
  12415. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*) a.input, (word32) a.inLen),
  12416. 0);
  12417. ExpectIntEQ(wc_Sha512_224Final(&sha512, hash), 0);
  12418. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_224_DIGEST_SIZE), 0);
  12419. c.input = NULL;
  12420. c.inLen = WC_SHA512_224_DIGEST_SIZE;
  12421. ExpectIntEQ(wc_Sha512_224Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12422. BAD_FUNC_ARG);
  12423. ExpectIntEQ(wc_Sha512_224Update(NULL, (byte*)a.input, (word32)a.inLen),
  12424. BAD_FUNC_ARG);
  12425. wc_Sha512_224Free(&sha512);
  12426. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12427. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12428. return EXPECT_RESULT();
  12429. }
  12430. static int test_wc_Sha512_224Final(void)
  12431. {
  12432. EXPECT_DECLS;
  12433. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12434. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12435. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 0),
  12436. TEST_SUCCESS);
  12437. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_224 */
  12438. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12439. return EXPECT_RESULT();
  12440. }
  12441. static int test_wc_Sha512_224GetFlags(void)
  12442. {
  12443. EXPECT_DECLS;
  12444. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12445. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && defined(WOLFSSL_HASH_FLAGS)
  12446. wc_Sha512 sha512;
  12447. wc_Sha512 copy;
  12448. word32 flags = 0;
  12449. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12450. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  12451. /* Initialize */
  12452. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12453. ExpectIntEQ(wc_InitSha512_224(&copy), 0);
  12454. ExpectIntEQ(wc_Sha512_224GetFlags(&sha512, &flags), 0);
  12455. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12456. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &copy), 0);
  12457. ExpectIntEQ(wc_Sha512_224GetFlags(&copy, &flags), 0);
  12458. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  12459. wc_Sha512_224Free(&copy);
  12460. wc_Sha512_224Free(&sha512);
  12461. #endif
  12462. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12463. return EXPECT_RESULT();
  12464. }
  12465. static int test_wc_Sha512_224FinalRaw(void)
  12466. {
  12467. EXPECT_DECLS;
  12468. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12469. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224) && \
  12470. !defined(WOLFSSL_NO_HASH_RAW)
  12471. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_224, 1),
  12472. TEST_SUCCESS);
  12473. #endif
  12474. return EXPECT_RESULT();
  12475. }
  12476. static int test_wc_Sha512_224Free(void)
  12477. {
  12478. EXPECT_DECLS;
  12479. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12480. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12481. wc_Sha512_224Free(NULL);
  12482. /* Set result to SUCCESS. */
  12483. ExpectTrue(1);
  12484. #endif
  12485. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12486. return EXPECT_RESULT();
  12487. }
  12488. static int test_wc_Sha512_224GetHash(void)
  12489. {
  12490. EXPECT_DECLS;
  12491. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12492. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12493. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_224),
  12494. TEST_SUCCESS);
  12495. #endif
  12496. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12497. return EXPECT_RESULT();
  12498. }
  12499. static int test_wc_Sha512_224Copy(void)
  12500. {
  12501. EXPECT_DECLS;
  12502. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12503. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  12504. wc_Sha512 sha512;
  12505. wc_Sha512 temp;
  12506. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12507. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12508. /* Initialize */
  12509. ExpectIntEQ(wc_InitSha512_224(&sha512), 0);
  12510. ExpectIntEQ(wc_InitSha512_224(&temp), 0);
  12511. ExpectIntEQ(wc_Sha512_224Copy(&sha512, &temp), 0);
  12512. /* test bad arguments*/
  12513. ExpectIntEQ(wc_Sha512_224Copy(NULL, NULL), BAD_FUNC_ARG);
  12514. ExpectIntEQ(wc_Sha512_224Copy(NULL, &temp), BAD_FUNC_ARG);
  12515. ExpectIntEQ(wc_Sha512_224Copy(&sha512, NULL), BAD_FUNC_ARG);
  12516. wc_Sha512_224Free(&sha512);
  12517. wc_Sha512_224Free(&temp);
  12518. #endif
  12519. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12520. return EXPECT_RESULT();
  12521. }
  12522. static int test_wc_InitSha512_256(void)
  12523. {
  12524. EXPECT_DECLS;
  12525. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12526. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12527. wc_Sha512 sha512;
  12528. /* Test good arg. */
  12529. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12530. /* Test bad arg. */
  12531. ExpectIntEQ(wc_InitSha512_256(NULL), BAD_FUNC_ARG);
  12532. wc_Sha512_256Free(&sha512);
  12533. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12534. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12535. return EXPECT_RESULT();
  12536. }
  12537. static int test_wc_Sha512_256Update(void)
  12538. {
  12539. EXPECT_DECLS;
  12540. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12541. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12542. wc_Sha512 sha512;
  12543. byte hash[WC_SHA512_DIGEST_SIZE];
  12544. testVector a, c;
  12545. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12546. /* Input. */
  12547. a.input = "a";
  12548. a.inLen = XSTRLEN(a.input);
  12549. ExpectIntEQ(wc_Sha512_256Update(&sha512, NULL, 0), 0);
  12550. ExpectIntEQ(wc_Sha512_256Update(&sha512,(byte*)a.input, 0), 0);
  12551. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)a.input, (word32)a.inLen),
  12552. 0);
  12553. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  12554. /* Update input. */
  12555. a.input = "abc";
  12556. a.output = "\x53\x04\x8e\x26\x81\x94\x1e\xf9\x9b\x2e\x29\xb7\x6b\x4c"
  12557. "\x7d\xab\xe4\xc2\xd0\xc6\x34\xfc\x6d\x46\xe0\xe2\xf1\x31"
  12558. "\x07\xe7\xaf\x23";
  12559. a.inLen = XSTRLEN(a.input);
  12560. a.outLen = XSTRLEN(a.output);
  12561. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*) a.input, (word32) a.inLen),
  12562. 0);
  12563. ExpectIntEQ(wc_Sha512_256Final(&sha512, hash), 0);
  12564. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA512_256_DIGEST_SIZE), 0);
  12565. c.input = NULL;
  12566. c.inLen = WC_SHA512_256_DIGEST_SIZE;
  12567. ExpectIntEQ(wc_Sha512_256Update(&sha512, (byte*)c.input, (word32)c.inLen),
  12568. BAD_FUNC_ARG);
  12569. ExpectIntEQ(wc_Sha512_256Update(NULL, (byte*)a.input, (word32)a.inLen),
  12570. BAD_FUNC_ARG);
  12571. wc_Sha512_256Free(&sha512);
  12572. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12573. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12574. return EXPECT_RESULT();
  12575. }
  12576. static int test_wc_Sha512_256Final(void)
  12577. {
  12578. EXPECT_DECLS;
  12579. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12580. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12581. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 0),
  12582. TEST_SUCCESS);
  12583. #endif /* WOLFSSL_SHA512 && !WOLFSSL_NOSHA512_256 */
  12584. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12585. return EXPECT_RESULT();
  12586. }
  12587. static int test_wc_Sha512_256GetFlags(void)
  12588. {
  12589. EXPECT_DECLS;
  12590. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12591. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && defined(WOLFSSL_HASH_FLAGS)
  12592. wc_Sha512 sha512, copy;
  12593. word32 flags = 0;
  12594. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12595. XMEMSET(&copy, 0, sizeof(wc_Sha512));
  12596. /* Initialize */
  12597. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12598. ExpectIntEQ(wc_InitSha512_256(&copy), 0);
  12599. ExpectIntEQ(wc_Sha512_256GetFlags(&sha512, &flags), 0);
  12600. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12601. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &copy), 0);
  12602. ExpectIntEQ(wc_Sha512_256GetFlags(&copy, &flags), 0);
  12603. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == WC_HASH_FLAG_ISCOPY);
  12604. wc_Sha512_256Free(&sha512);
  12605. #endif
  12606. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12607. return EXPECT_RESULT();
  12608. }
  12609. static int test_wc_Sha512_256FinalRaw(void)
  12610. {
  12611. EXPECT_DECLS;
  12612. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && \
  12613. defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256) && \
  12614. !defined(WOLFSSL_NO_HASH_RAW)
  12615. ExpectIntEQ(test_Sha512_Family_Final(WC_HASH_TYPE_SHA512_256, 1),
  12616. TEST_SUCCESS);
  12617. #endif
  12618. return EXPECT_RESULT();
  12619. }
  12620. static int test_wc_Sha512_256Free(void)
  12621. {
  12622. EXPECT_DECLS;
  12623. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12624. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12625. wc_Sha512_256Free(NULL);
  12626. /* Set result to SUCCESS. */
  12627. ExpectTrue(1);
  12628. #endif
  12629. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12630. return EXPECT_RESULT();
  12631. }
  12632. static int test_wc_Sha512_256GetHash(void)
  12633. {
  12634. EXPECT_DECLS;
  12635. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12636. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12637. ExpectIntEQ(test_Sha512_Family_GetHash(WC_HASH_TYPE_SHA512_256),
  12638. TEST_SUCCESS);
  12639. #endif
  12640. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12641. return EXPECT_RESULT();
  12642. }
  12643. static int test_wc_Sha512_256Copy(void)
  12644. {
  12645. EXPECT_DECLS;
  12646. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  12647. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  12648. wc_Sha512 sha512;
  12649. wc_Sha512 temp;
  12650. XMEMSET(&sha512, 0, sizeof(wc_Sha512));
  12651. XMEMSET(&temp, 0, sizeof(wc_Sha512));
  12652. /* Initialize */
  12653. ExpectIntEQ(wc_InitSha512_256(&sha512), 0);
  12654. ExpectIntEQ(wc_InitSha512_256(&temp), 0);
  12655. ExpectIntEQ(wc_Sha512_256Copy(&sha512, &temp), 0);
  12656. /* test bad arguments*/
  12657. ExpectIntEQ(wc_Sha512_256Copy(NULL, NULL), BAD_FUNC_ARG);
  12658. ExpectIntEQ(wc_Sha512_256Copy(NULL, &temp), BAD_FUNC_ARG);
  12659. ExpectIntEQ(wc_Sha512_256Copy(&sha512, NULL), BAD_FUNC_ARG);
  12660. wc_Sha512_256Free(&sha512);
  12661. wc_Sha512_256Free(&temp);
  12662. #endif
  12663. #endif /* !HAVE_FIPS && !HAVE_SELFTEST */
  12664. return EXPECT_RESULT();
  12665. }
  12666. /*
  12667. * Testing wc_InitSha384()
  12668. */
  12669. static int test_wc_InitSha384(void)
  12670. {
  12671. EXPECT_DECLS;
  12672. #ifdef WOLFSSL_SHA384
  12673. wc_Sha384 sha384;
  12674. /* Test good arg. */
  12675. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12676. /* Test bad arg. */
  12677. ExpectIntEQ(wc_InitSha384(NULL), BAD_FUNC_ARG);
  12678. wc_Sha384Free(&sha384);
  12679. #endif
  12680. return EXPECT_RESULT();
  12681. } /* END test_wc_InitSha384 */
  12682. /*
  12683. * test wc_Sha384Update()
  12684. */
  12685. static int test_wc_Sha384Update(void)
  12686. {
  12687. EXPECT_DECLS;
  12688. #ifdef WOLFSSL_SHA384
  12689. wc_Sha384 sha384;
  12690. byte hash[WC_SHA384_DIGEST_SIZE];
  12691. testVector a, b, c;
  12692. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12693. /* Input */
  12694. a.input = "a";
  12695. a.inLen = XSTRLEN(a.input);
  12696. ExpectIntEQ(wc_Sha384Update(&sha384, NULL, 0), 0);
  12697. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, 0), 0);
  12698. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  12699. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  12700. /* Update input. */
  12701. a.input = "abc";
  12702. a.output = "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  12703. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  12704. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  12705. "\xc8\x25\xa7";
  12706. a.inLen = XSTRLEN(a.input);
  12707. a.outLen = XSTRLEN(a.output);
  12708. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)a.input, (word32)a.inLen), 0);
  12709. ExpectIntEQ(wc_Sha384Final(&sha384, hash), 0);
  12710. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  12711. /* Pass in bad values. */
  12712. b.input = NULL;
  12713. b.inLen = 0;
  12714. ExpectIntEQ(wc_Sha384Update(&sha384, (byte*)b.input, (word32)b.inLen), 0);
  12715. c.input = NULL;
  12716. c.inLen = WC_SHA384_DIGEST_SIZE;
  12717. ExpectIntEQ( wc_Sha384Update(&sha384, (byte*)c.input, (word32)c.inLen),
  12718. BAD_FUNC_ARG);
  12719. ExpectIntEQ(wc_Sha384Update(NULL, (byte*)a.input, (word32)a.inLen),
  12720. BAD_FUNC_ARG);
  12721. wc_Sha384Free(&sha384);
  12722. #endif
  12723. return EXPECT_RESULT();
  12724. } /* END test_wc_Sha384Update */
  12725. /*
  12726. * Unit test function for wc_Sha384Final();
  12727. */
  12728. static int test_wc_Sha384Final(void)
  12729. {
  12730. EXPECT_DECLS;
  12731. #ifdef WOLFSSL_SHA384
  12732. wc_Sha384 sha384;
  12733. byte* hash_test[3];
  12734. byte hash1[WC_SHA384_DIGEST_SIZE];
  12735. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  12736. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  12737. int times, i;
  12738. /* Initialize */
  12739. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12740. hash_test[0] = hash1;
  12741. hash_test[1] = hash2;
  12742. hash_test[2] = hash3;
  12743. times = sizeof(hash_test) / sizeof(byte*);
  12744. /* Good test args. */
  12745. for (i = 0; i < times; i++) {
  12746. ExpectIntEQ(wc_Sha384Final(&sha384, hash_test[i]), 0);
  12747. }
  12748. /* Test bad args. */
  12749. ExpectIntEQ(wc_Sha384Final(NULL, NULL), BAD_FUNC_ARG);
  12750. ExpectIntEQ(wc_Sha384Final(NULL, hash1), BAD_FUNC_ARG);
  12751. ExpectIntEQ(wc_Sha384Final(&sha384, NULL), BAD_FUNC_ARG);
  12752. wc_Sha384Free(&sha384);
  12753. #endif
  12754. return EXPECT_RESULT();
  12755. } /* END test_wc_Sha384Final */
  12756. /*
  12757. * Unit test function for wc_Sha384GetFlags()
  12758. */
  12759. static int test_wc_Sha384GetFlags(void)
  12760. {
  12761. EXPECT_DECLS;
  12762. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_HASH_FLAGS)
  12763. wc_Sha384 sha384;
  12764. word32 flags = 0;
  12765. /* Initialize */
  12766. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12767. ExpectIntEQ(wc_Sha384GetFlags(&sha384, &flags), 0);
  12768. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12769. wc_Sha384Free(&sha384);
  12770. #endif
  12771. return EXPECT_RESULT();
  12772. } /* END test_wc_Sha384GetFlags */
  12773. /*
  12774. * Unit test function for wc_Sha384FinalRaw()
  12775. */
  12776. static int test_wc_Sha384FinalRaw(void)
  12777. {
  12778. EXPECT_DECLS;
  12779. #if (defined(WOLFSSL_SHA384) && !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  12780. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3)))) && \
  12781. !defined(WOLFSSL_NO_HASH_RAW)
  12782. wc_Sha384 sha384;
  12783. byte* hash_test[3];
  12784. byte hash1[WC_SHA384_DIGEST_SIZE];
  12785. byte hash2[2*WC_SHA384_DIGEST_SIZE];
  12786. byte hash3[5*WC_SHA384_DIGEST_SIZE];
  12787. int times, i;
  12788. /* Initialize */
  12789. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12790. hash_test[0] = hash1;
  12791. hash_test[1] = hash2;
  12792. hash_test[2] = hash3;
  12793. times = sizeof(hash_test) / sizeof(byte*);
  12794. /* Good test args. */
  12795. for (i = 0; i < times; i++) {
  12796. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, hash_test[i]), 0);
  12797. }
  12798. /* Test bad args. */
  12799. ExpectIntEQ(wc_Sha384FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  12800. ExpectIntEQ(wc_Sha384FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  12801. ExpectIntEQ(wc_Sha384FinalRaw(&sha384, NULL), BAD_FUNC_ARG);
  12802. wc_Sha384Free(&sha384);
  12803. #endif
  12804. return EXPECT_RESULT();
  12805. } /* END test_wc_Sha384FinalRaw */
  12806. /*
  12807. * Unit test function for wc_Sha384Free()
  12808. */
  12809. static int test_wc_Sha384Free(void)
  12810. {
  12811. EXPECT_DECLS;
  12812. #ifdef WOLFSSL_SHA384
  12813. wc_Sha384Free(NULL);
  12814. /* Set result to SUCCESS. */
  12815. ExpectTrue(1);
  12816. #endif
  12817. return EXPECT_RESULT();
  12818. } /* END test_wc_Sha384Free */
  12819. /*
  12820. * Unit test function for wc_Sha384GetHash()
  12821. */
  12822. static int test_wc_Sha384GetHash(void)
  12823. {
  12824. EXPECT_DECLS;
  12825. #ifdef WOLFSSL_SHA384
  12826. wc_Sha384 sha384;
  12827. byte hash1[WC_SHA384_DIGEST_SIZE];
  12828. /* Initialize */
  12829. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12830. ExpectIntEQ(wc_Sha384GetHash(&sha384, hash1), 0);
  12831. /* test bad arguments*/
  12832. ExpectIntEQ(wc_Sha384GetHash(NULL, NULL), BAD_FUNC_ARG);
  12833. ExpectIntEQ(wc_Sha384GetHash(NULL, hash1), BAD_FUNC_ARG);
  12834. ExpectIntEQ(wc_Sha384GetHash(&sha384, NULL), BAD_FUNC_ARG);
  12835. wc_Sha384Free(&sha384);
  12836. #endif
  12837. return EXPECT_RESULT();
  12838. } /* END test_wc_Sha384GetHash */
  12839. /*
  12840. * Unit test function for wc_Sha384Copy()
  12841. */
  12842. static int test_wc_Sha384Copy(void)
  12843. {
  12844. EXPECT_DECLS;
  12845. #ifdef WOLFSSL_SHA384
  12846. wc_Sha384 sha384;
  12847. wc_Sha384 temp;
  12848. XMEMSET(&sha384, 0, sizeof(wc_Sha384));
  12849. XMEMSET(&temp, 0, sizeof(wc_Sha384));
  12850. /* Initialize */
  12851. ExpectIntEQ(wc_InitSha384(&sha384), 0);
  12852. ExpectIntEQ(wc_InitSha384(&temp), 0);
  12853. ExpectIntEQ(wc_Sha384Copy(&sha384, &temp), 0);
  12854. /* test bad arguments*/
  12855. ExpectIntEQ(wc_Sha384Copy(NULL, NULL), BAD_FUNC_ARG);
  12856. ExpectIntEQ(wc_Sha384Copy(NULL, &temp), BAD_FUNC_ARG);
  12857. ExpectIntEQ(wc_Sha384Copy(&sha384, NULL), BAD_FUNC_ARG);
  12858. wc_Sha384Free(&sha384);
  12859. wc_Sha384Free(&temp);
  12860. #endif
  12861. return EXPECT_RESULT();
  12862. } /* END test_wc_Sha384Copy */
  12863. /*
  12864. * Testing wc_InitSha224();
  12865. */
  12866. static int test_wc_InitSha224(void)
  12867. {
  12868. EXPECT_DECLS;
  12869. #ifdef WOLFSSL_SHA224
  12870. wc_Sha224 sha224;
  12871. /* Test good arg. */
  12872. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12873. /* Test bad arg. */
  12874. ExpectIntEQ(wc_InitSha224(NULL), BAD_FUNC_ARG);
  12875. wc_Sha224Free(&sha224);
  12876. #endif
  12877. return EXPECT_RESULT();
  12878. } /* END test_wc_InitSha224 */
  12879. /*
  12880. * Unit test on wc_Sha224Update
  12881. */
  12882. static int test_wc_Sha224Update(void)
  12883. {
  12884. EXPECT_DECLS;
  12885. #ifdef WOLFSSL_SHA224
  12886. wc_Sha224 sha224;
  12887. byte hash[WC_SHA224_DIGEST_SIZE];
  12888. testVector a, b, c;
  12889. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12890. /* Input. */
  12891. a.input = "a";
  12892. a.inLen = XSTRLEN(a.input);
  12893. ExpectIntEQ(wc_Sha224Update(&sha224, NULL, 0), 0);
  12894. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, 0), 0);
  12895. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  12896. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  12897. /* Update input. */
  12898. a.input = "abc";
  12899. a.output = "\x23\x09\x7d\x22\x34\x05\xd8\x22\x86\x42\xa4\x77\xbd\xa2"
  12900. "\x55\xb3\x2a\xad\xbc\xe4\xbd\xa0\xb3\xf7\xe3\x6c\x9d\xa7";
  12901. a.inLen = XSTRLEN(a.input);
  12902. a.outLen = XSTRLEN(a.output);
  12903. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)a.input, (word32)a.inLen), 0);
  12904. ExpectIntEQ(wc_Sha224Final(&sha224, hash), 0);
  12905. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  12906. /* Pass in bad values. */
  12907. b.input = NULL;
  12908. b.inLen = 0;
  12909. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)b.input, (word32)b.inLen), 0);
  12910. c.input = NULL;
  12911. c.inLen = WC_SHA224_DIGEST_SIZE;
  12912. ExpectIntEQ(wc_Sha224Update(&sha224, (byte*)c.input, (word32)c.inLen),
  12913. BAD_FUNC_ARG);
  12914. ExpectIntEQ(wc_Sha224Update(NULL, (byte*)a.input, (word32)a.inLen),
  12915. BAD_FUNC_ARG);
  12916. wc_Sha224Free(&sha224);
  12917. #endif
  12918. return EXPECT_RESULT();
  12919. } /* END test_wc_Sha224Update */
  12920. /*
  12921. * Unit test for wc_Sha224Final();
  12922. */
  12923. static int test_wc_Sha224Final(void)
  12924. {
  12925. EXPECT_DECLS;
  12926. #ifdef WOLFSSL_SHA224
  12927. wc_Sha224 sha224;
  12928. byte* hash_test[3];
  12929. byte hash1[WC_SHA224_DIGEST_SIZE];
  12930. byte hash2[2*WC_SHA224_DIGEST_SIZE];
  12931. byte hash3[5*WC_SHA224_DIGEST_SIZE];
  12932. int times, i;
  12933. /* Initialize */
  12934. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12935. hash_test[0] = hash1;
  12936. hash_test[1] = hash2;
  12937. hash_test[2] = hash3;
  12938. times = sizeof(hash_test) / sizeof(byte*);
  12939. /* Good test args. */
  12940. /* Testing oversized buffers. */
  12941. for (i = 0; i < times; i++) {
  12942. ExpectIntEQ(wc_Sha224Final(&sha224, hash_test[i]), 0);
  12943. }
  12944. /* Test bad args. */
  12945. ExpectIntEQ(wc_Sha224Final(NULL, NULL), BAD_FUNC_ARG);
  12946. ExpectIntEQ(wc_Sha224Final(NULL, hash1), BAD_FUNC_ARG);
  12947. ExpectIntEQ(wc_Sha224Final(&sha224, NULL), BAD_FUNC_ARG);
  12948. wc_Sha224Free(&sha224);
  12949. #endif
  12950. return EXPECT_RESULT();
  12951. } /* END test_wc_Sha224Final */
  12952. /*
  12953. * Unit test function for wc_Sha224SetFlags()
  12954. */
  12955. static int test_wc_Sha224SetFlags(void)
  12956. {
  12957. EXPECT_DECLS;
  12958. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  12959. wc_Sha224 sha224;
  12960. word32 flags = WC_HASH_FLAG_WILLCOPY;
  12961. /* Initialize */
  12962. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12963. ExpectIntEQ(wc_Sha224SetFlags(&sha224, flags), 0);
  12964. flags = 0;
  12965. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  12966. ExpectTrue(flags == WC_HASH_FLAG_WILLCOPY);
  12967. wc_Sha224Free(&sha224);
  12968. #endif
  12969. return EXPECT_RESULT();
  12970. } /* END test_wc_Sha224SetFlags */
  12971. /*
  12972. * Unit test function for wc_Sha224GetFlags()
  12973. */
  12974. static int test_wc_Sha224GetFlags(void)
  12975. {
  12976. EXPECT_DECLS;
  12977. #if defined(WOLFSSL_SHA224) && defined(WOLFSSL_HASH_FLAGS)
  12978. wc_Sha224 sha224;
  12979. word32 flags = 0;
  12980. /* Initialize */
  12981. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  12982. ExpectIntEQ(wc_Sha224GetFlags(&sha224, &flags), 0);
  12983. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  12984. wc_Sha224Free(&sha224);
  12985. #endif
  12986. return EXPECT_RESULT();
  12987. } /* END test_wc_Sha224GetFlags */
  12988. /*
  12989. * Unit test function for wc_Sha224Free()
  12990. */
  12991. static int test_wc_Sha224Free(void)
  12992. {
  12993. EXPECT_DECLS;
  12994. #ifdef WOLFSSL_SHA224
  12995. wc_Sha224Free(NULL);
  12996. /* Set result to SUCCESS. */
  12997. ExpectTrue(1);
  12998. #endif
  12999. return EXPECT_RESULT();
  13000. } /* END test_wc_Sha224Free */
  13001. /*
  13002. * Unit test function for wc_Sha224GetHash()
  13003. */
  13004. static int test_wc_Sha224GetHash(void)
  13005. {
  13006. EXPECT_DECLS;
  13007. #ifdef WOLFSSL_SHA224
  13008. wc_Sha224 sha224;
  13009. byte hash1[WC_SHA224_DIGEST_SIZE];
  13010. /* Initialize */
  13011. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  13012. ExpectIntEQ(wc_Sha224GetHash(&sha224, hash1), 0);
  13013. /* test bad arguments*/
  13014. ExpectIntEQ(wc_Sha224GetHash(NULL, NULL), BAD_FUNC_ARG);
  13015. ExpectIntEQ(wc_Sha224GetHash(NULL, hash1), BAD_FUNC_ARG);
  13016. ExpectIntEQ(wc_Sha224GetHash(&sha224, NULL), BAD_FUNC_ARG);
  13017. wc_Sha224Free(&sha224);
  13018. #endif
  13019. return EXPECT_RESULT();
  13020. } /* END test_wc_Sha224GetHash */
  13021. /*
  13022. * Unit test function for wc_Sha224Copy()
  13023. */
  13024. static int test_wc_Sha224Copy(void)
  13025. {
  13026. EXPECT_DECLS;
  13027. #ifdef WOLFSSL_SHA224
  13028. wc_Sha224 sha224;
  13029. wc_Sha224 temp;
  13030. XMEMSET(&sha224, 0, sizeof(wc_Sha224));
  13031. XMEMSET(&temp, 0, sizeof(wc_Sha224));
  13032. /* Initialize */
  13033. ExpectIntEQ(wc_InitSha224(&sha224), 0);
  13034. ExpectIntEQ(wc_InitSha224(&temp), 0);
  13035. ExpectIntEQ(wc_Sha224Copy(&sha224, &temp), 0);
  13036. /* test bad arguments*/
  13037. ExpectIntEQ(wc_Sha224Copy(NULL, NULL), BAD_FUNC_ARG);
  13038. ExpectIntEQ(wc_Sha224Copy(NULL, &temp), BAD_FUNC_ARG);
  13039. ExpectIntEQ(wc_Sha224Copy(&sha224, NULL), BAD_FUNC_ARG);
  13040. wc_Sha224Free(&sha224);
  13041. wc_Sha224Free(&temp);
  13042. #endif
  13043. return EXPECT_RESULT();
  13044. } /* END test_wc_Sha224Copy */
  13045. /*
  13046. * Testing wc_InitRipeMd()
  13047. */
  13048. static int test_wc_InitRipeMd(void)
  13049. {
  13050. EXPECT_DECLS;
  13051. #ifdef WOLFSSL_RIPEMD
  13052. RipeMd ripemd;
  13053. /* Test good arg. */
  13054. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  13055. /* Test bad arg. */
  13056. ExpectIntEQ(wc_InitRipeMd(NULL), BAD_FUNC_ARG);
  13057. #endif
  13058. return EXPECT_RESULT();
  13059. } /* END test_wc_InitRipeMd */
  13060. /*
  13061. * Testing wc_RipeMdUpdate()
  13062. */
  13063. static int test_wc_RipeMdUpdate(void)
  13064. {
  13065. EXPECT_DECLS;
  13066. #ifdef WOLFSSL_RIPEMD
  13067. RipeMd ripemd;
  13068. byte hash[RIPEMD_DIGEST_SIZE];
  13069. testVector a, b, c;
  13070. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  13071. /* Input */
  13072. a.input = "a";
  13073. a.inLen = XSTRLEN(a.input);
  13074. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  13075. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  13076. /* Update input. */
  13077. a.input = "abc";
  13078. a.output = "\x8e\xb2\x08\xf7\xe0\x5d\x98\x7a\x9b\x04\x4a\x8e\x98\xc6"
  13079. "\xb0\x87\xf1\x5a\x0b\xfc";
  13080. a.inLen = XSTRLEN(a.input);
  13081. a.outLen = XSTRLEN(a.output);
  13082. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)a.input, (word32)a.inLen), 0);
  13083. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash), 0);
  13084. ExpectIntEQ(XMEMCMP(hash, a.output, RIPEMD_DIGEST_SIZE), 0);
  13085. /* Pass in bad values. */
  13086. b.input = NULL;
  13087. b.inLen = 0;
  13088. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)b.input, (word32)b.inLen), 0);
  13089. c.input = NULL;
  13090. c.inLen = RIPEMD_DIGEST_SIZE;
  13091. ExpectIntEQ(wc_RipeMdUpdate(&ripemd, (byte*)c.input, (word32)c.inLen),
  13092. BAD_FUNC_ARG);
  13093. ExpectIntEQ(wc_RipeMdUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  13094. BAD_FUNC_ARG);
  13095. #endif
  13096. return EXPECT_RESULT();
  13097. } /* END test_wc_RipeMdUdpate */
  13098. /*
  13099. * Unit test function for wc_RipeMdFinal()
  13100. */
  13101. static int test_wc_RipeMdFinal(void)
  13102. {
  13103. EXPECT_DECLS;
  13104. #ifdef WOLFSSL_RIPEMD
  13105. RipeMd ripemd;
  13106. byte* hash_test[3];
  13107. byte hash1[RIPEMD_DIGEST_SIZE];
  13108. byte hash2[2*RIPEMD_DIGEST_SIZE];
  13109. byte hash3[5*RIPEMD_DIGEST_SIZE];
  13110. int times, i;
  13111. /* Initialize */
  13112. ExpectIntEQ(wc_InitRipeMd(&ripemd), 0);
  13113. hash_test[0] = hash1;
  13114. hash_test[1] = hash2;
  13115. hash_test[2] = hash3;
  13116. times = sizeof(hash_test) / sizeof(byte*);
  13117. /* Testing oversized buffers. */
  13118. for (i = 0; i < times; i++) {
  13119. ExpectIntEQ(wc_RipeMdFinal(&ripemd, hash_test[i]), 0);
  13120. }
  13121. /* Test bad args. */
  13122. ExpectIntEQ(wc_RipeMdFinal(NULL, NULL), BAD_FUNC_ARG);
  13123. ExpectIntEQ(wc_RipeMdFinal(NULL, hash1), BAD_FUNC_ARG);
  13124. ExpectIntEQ(wc_RipeMdFinal(&ripemd, NULL), BAD_FUNC_ARG);
  13125. #endif
  13126. return EXPECT_RESULT();
  13127. } /* END test_wc_RipeMdFinal */
  13128. /*
  13129. * Testing wc_InitSha3_224, wc_InitSha3_256, wc_InitSha3_384, and
  13130. * wc_InitSha3_512
  13131. */
  13132. static int test_wc_InitSha3(void)
  13133. {
  13134. EXPECT_DECLS;
  13135. #if defined(WOLFSSL_SHA3)
  13136. wc_Sha3 sha3;
  13137. (void)sha3;
  13138. #if !defined(WOLFSSL_NOSHA3_224)
  13139. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13140. /* Test bad args. */
  13141. ExpectIntEQ(wc_InitSha3_224(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13142. wc_Sha3_224_Free(&sha3);
  13143. #endif /* NOSHA3_224 */
  13144. #if !defined(WOLFSSL_NOSHA3_256)
  13145. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13146. /* Test bad args. */
  13147. ExpectIntEQ(wc_InitSha3_256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13148. wc_Sha3_256_Free(&sha3);
  13149. #endif /* NOSHA3_256 */
  13150. #if !defined(WOLFSSL_NOSHA3_384)
  13151. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13152. /* Test bad args. */
  13153. ExpectIntEQ(wc_InitSha3_384(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13154. wc_Sha3_384_Free(&sha3);
  13155. #endif /* NOSHA3_384 */
  13156. #if !defined(WOLFSSL_NOSHA3_512)
  13157. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13158. /* Test bad args. */
  13159. ExpectIntEQ(wc_InitSha3_512(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13160. wc_Sha3_512_Free(&sha3);
  13161. #endif /* NOSHA3_512 */
  13162. #endif
  13163. return EXPECT_RESULT();
  13164. } /* END test_wc_InitSha3 */
  13165. /*
  13166. * Testing wc_Sha3_Update()
  13167. */
  13168. static int testing_wc_Sha3_Update(void)
  13169. {
  13170. EXPECT_DECLS;
  13171. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_XILINX_CRYPT) && \
  13172. !defined(WOLFSSL_AFALG_XILINX)
  13173. wc_Sha3 sha3;
  13174. byte msg[] = "Everybody's working for the weekend.";
  13175. byte msg2[] = "Everybody gets Friday off.";
  13176. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  13177. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  13178. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  13179. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  13180. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  13181. word32 msglen = sizeof(msg) - 1;
  13182. word32 msg2len = sizeof(msg2);
  13183. word32 msgCmplen = sizeof(msgCmp);
  13184. #if !defined(WOLFSSL_NOSHA3_224)
  13185. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13186. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg, msglen), 0);
  13187. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13188. ExpectTrue(sha3.i == msglen);
  13189. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  13190. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13191. /* Pass bad args. */
  13192. ExpectIntEQ(wc_Sha3_224_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13193. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13194. wc_Sha3_224_Free(&sha3);
  13195. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13196. ExpectIntEQ(wc_Sha3_224_Update(&sha3, NULL, 0), 0);
  13197. ExpectIntEQ(wc_Sha3_224_Update(&sha3, msg2, msg2len), 0);
  13198. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13199. wc_Sha3_224_Free(&sha3);
  13200. #endif /* SHA3_224 */
  13201. #if !defined(WOLFSSL_NOSHA3_256)
  13202. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13203. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg, msglen), 0);
  13204. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13205. ExpectTrue(sha3.i == msglen);
  13206. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  13207. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13208. /* Pass bad args. */
  13209. ExpectIntEQ(wc_Sha3_256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13210. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13211. wc_Sha3_256_Free(&sha3);
  13212. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13213. ExpectIntEQ(wc_Sha3_256_Update(&sha3, NULL, 0), 0);
  13214. ExpectIntEQ(wc_Sha3_256_Update(&sha3, msg2, msg2len), 0);
  13215. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13216. wc_Sha3_256_Free(&sha3);
  13217. #endif /* SHA3_256 */
  13218. #if !defined(WOLFSSL_NOSHA3_384)
  13219. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13220. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg, msglen), 0);
  13221. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13222. ExpectTrue(sha3.i == msglen);
  13223. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  13224. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13225. /* Pass bad args. */
  13226. ExpectIntEQ(wc_Sha3_384_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13227. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13228. wc_Sha3_384_Free(&sha3);
  13229. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13230. ExpectIntEQ(wc_Sha3_384_Update(&sha3, NULL, 0), 0);
  13231. ExpectIntEQ(wc_Sha3_384_Update(&sha3, msg2, msg2len), 0);
  13232. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13233. wc_Sha3_384_Free(&sha3);
  13234. #endif /* SHA3_384 */
  13235. #if !defined(WOLFSSL_NOSHA3_512)
  13236. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13237. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg, msglen), 0);
  13238. ExpectIntEQ(XMEMCMP(msg, sha3.t, msglen), 0);
  13239. ExpectTrue(sha3.i == msglen);
  13240. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  13241. ExpectIntEQ(XMEMCMP(sha3.t, msgCmp, msgCmplen), 0);
  13242. /* Pass bad args. */
  13243. ExpectIntEQ(wc_Sha3_512_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13244. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 5), BAD_FUNC_ARG);
  13245. wc_Sha3_512_Free(&sha3);
  13246. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13247. ExpectIntEQ(wc_Sha3_512_Update(&sha3, NULL, 0), 0);
  13248. ExpectIntEQ(wc_Sha3_512_Update(&sha3, msg2, msg2len), 0);
  13249. ExpectIntEQ(XMEMCMP(msg2, sha3.t, msg2len), 0);
  13250. wc_Sha3_512_Free(&sha3);
  13251. #endif /* SHA3_512 */
  13252. #endif /* WOLFSSL_SHA3 */
  13253. return EXPECT_RESULT();
  13254. } /* END testing_wc_Sha3_Update */
  13255. /*
  13256. * Testing wc_Sha3_224_Final()
  13257. */
  13258. static int test_wc_Sha3_224_Final(void)
  13259. {
  13260. EXPECT_DECLS;
  13261. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  13262. wc_Sha3 sha3;
  13263. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13264. "nopnopq";
  13265. const char* expOut = "\x8a\x24\x10\x8b\x15\x4a\xda\x21\xc9\xfd\x55"
  13266. "\x74\x49\x44\x79\xba\x5c\x7e\x7a\xb7\x6e\xf2"
  13267. "\x64\xea\xd0\xfc\xce\x33";
  13268. byte hash[WC_SHA3_224_DIGEST_SIZE];
  13269. byte hashRet[WC_SHA3_224_DIGEST_SIZE];
  13270. /* Init stack variables. */
  13271. XMEMSET(hash, 0, sizeof(hash));
  13272. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13273. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13274. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13275. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_224_DIGEST_SIZE), 0);
  13276. /* Test bad args. */
  13277. ExpectIntEQ(wc_Sha3_224_Final(NULL, hash), BAD_FUNC_ARG);
  13278. ExpectIntEQ(wc_Sha3_224_Final(&sha3, NULL), BAD_FUNC_ARG);
  13279. wc_Sha3_224_Free(&sha3);
  13280. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13281. /* Init stack variables. */
  13282. XMEMSET(hash, 0, sizeof(hash));
  13283. XMEMSET(hashRet, 0, sizeof(hashRet));
  13284. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13285. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, hashRet), 0);
  13286. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13287. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_224_DIGEST_SIZE), 0);
  13288. /* Test bad args. */
  13289. ExpectIntEQ(wc_Sha3_224_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13290. ExpectIntEQ(wc_Sha3_224_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13291. wc_Sha3_224_Free(&sha3);
  13292. #endif
  13293. return EXPECT_RESULT();
  13294. } /* END test_wc_Sha3_224_Final */
  13295. /*
  13296. * Testing wc_Sha3_256_Final()
  13297. */
  13298. static int test_wc_Sha3_256_Final(void)
  13299. {
  13300. EXPECT_DECLS;
  13301. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  13302. wc_Sha3 sha3;
  13303. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13304. "nopnopq";
  13305. const char* expOut = "\x41\xc0\xdb\xa2\xa9\xd6\x24\x08\x49\x10\x03\x76\xa8"
  13306. "\x23\x5e\x2c\x82\xe1\xb9\x99\x8a\x99\x9e\x21\xdb\x32"
  13307. "\xdd\x97\x49\x6d\x33\x76";
  13308. byte hash[WC_SHA3_256_DIGEST_SIZE];
  13309. byte hashRet[WC_SHA3_256_DIGEST_SIZE];
  13310. /* Init stack variables. */
  13311. XMEMSET(hash, 0, sizeof(hash));
  13312. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13313. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13314. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13315. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_256_DIGEST_SIZE), 0);
  13316. /* Test bad args. */
  13317. ExpectIntEQ(wc_Sha3_256_Final(NULL, hash), BAD_FUNC_ARG);
  13318. ExpectIntEQ(wc_Sha3_256_Final(&sha3, NULL), BAD_FUNC_ARG);
  13319. wc_Sha3_256_Free(&sha3);
  13320. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13321. /* Init stack variables. */
  13322. XMEMSET(hash, 0, sizeof(hash));
  13323. XMEMSET(hashRet, 0, sizeof(hashRet));
  13324. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13325. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, hashRet), 0);
  13326. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13327. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_256_DIGEST_SIZE), 0);
  13328. /* Test bad args. */
  13329. ExpectIntEQ(wc_Sha3_256_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13330. ExpectIntEQ(wc_Sha3_256_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13331. wc_Sha3_256_Free(&sha3);
  13332. #endif
  13333. return EXPECT_RESULT();
  13334. } /* END test_wc_Sha3_256_Final */
  13335. /*
  13336. * Testing wc_Sha3_384_Final()
  13337. */
  13338. static int test_wc_Sha3_384_Final(void)
  13339. {
  13340. EXPECT_DECLS;
  13341. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  13342. wc_Sha3 sha3;
  13343. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13344. "nopnopq";
  13345. const char* expOut = "\x99\x1c\x66\x57\x55\xeb\x3a\x4b\x6b\xbd\xfb\x75\xc7"
  13346. "\x8a\x49\x2e\x8c\x56\xa2\x2c\x5c\x4d\x7e\x42\x9b\xfd"
  13347. "\xbc\x32\xb9\xd4\xad\x5a\xa0\x4a\x1f\x07\x6e\x62\xfe"
  13348. "\xa1\x9e\xef\x51\xac\xd0\x65\x7c\x22";
  13349. byte hash[WC_SHA3_384_DIGEST_SIZE];
  13350. byte hashRet[WC_SHA3_384_DIGEST_SIZE];
  13351. /* Init stack variables. */
  13352. XMEMSET(hash, 0, sizeof(hash));
  13353. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13354. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13355. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13356. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_384_DIGEST_SIZE), 0);
  13357. /* Test bad args. */
  13358. ExpectIntEQ(wc_Sha3_384_Final(NULL, hash), BAD_FUNC_ARG);
  13359. ExpectIntEQ(wc_Sha3_384_Final(&sha3, NULL), BAD_FUNC_ARG);
  13360. wc_Sha3_384_Free(&sha3);
  13361. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13362. /* Init stack variables. */
  13363. XMEMSET(hash, 0, sizeof(hash));
  13364. XMEMSET(hashRet, 0, sizeof(hashRet));
  13365. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13366. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, hashRet), 0);
  13367. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13368. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_384_DIGEST_SIZE), 0);
  13369. /* Test bad args. */
  13370. ExpectIntEQ(wc_Sha3_384_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13371. ExpectIntEQ(wc_Sha3_384_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13372. wc_Sha3_384_Free(&sha3);
  13373. #endif
  13374. return EXPECT_RESULT();
  13375. } /* END test_wc_Sha3_384_Final */
  13376. /*
  13377. * Testing wc_Sha3_512_Final()
  13378. */
  13379. static int test_wc_Sha3_512_Final(void)
  13380. {
  13381. EXPECT_DECLS;
  13382. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512) && \
  13383. !defined(WOLFSSL_NOSHA3_384)
  13384. wc_Sha3 sha3;
  13385. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13386. "nopnopq";
  13387. const char* expOut = "\x04\xa3\x71\xe8\x4e\xcf\xb5\xb8\xb7\x7c\xb4\x86\x10"
  13388. "\xfc\xa8\x18\x2d\xd4\x57\xce\x6f\x32\x6a\x0f\xd3\xd7"
  13389. "\xec\x2f\x1e\x91\x63\x6d\xee\x69\x1f\xbe\x0c\x98\x53"
  13390. "\x02\xba\x1b\x0d\x8d\xc7\x8c\x08\x63\x46\xb5\x33\xb4"
  13391. "\x9c\x03\x0d\x99\xa2\x7d\xaf\x11\x39\xd6\xe7\x5e";
  13392. byte hash[WC_SHA3_512_DIGEST_SIZE];
  13393. byte hashRet[WC_SHA3_512_DIGEST_SIZE];
  13394. /* Init stack variables. */
  13395. XMEMSET(hash, 0, sizeof(hash));
  13396. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13397. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13398. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13399. ExpectIntEQ(XMEMCMP(expOut, hash, WC_SHA3_512_DIGEST_SIZE), 0);
  13400. /* Test bad args. */
  13401. ExpectIntEQ(wc_Sha3_512_Final(NULL, hash), BAD_FUNC_ARG);
  13402. ExpectIntEQ(wc_Sha3_512_Final(&sha3, NULL), BAD_FUNC_ARG);
  13403. wc_Sha3_512_Free(&sha3);
  13404. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13405. /* Init stack variables. */
  13406. XMEMSET(hash, 0, sizeof(hash));
  13407. XMEMSET(hashRet, 0, sizeof(hashRet));
  13408. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, (word32)XSTRLEN(msg)), 0);
  13409. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, hashRet), 0);
  13410. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13411. ExpectIntEQ(XMEMCMP(hash, hashRet, WC_SHA3_512_DIGEST_SIZE), 0);
  13412. /* Test bad args. */
  13413. ExpectIntEQ(wc_Sha3_512_GetHash(NULL, hashRet), BAD_FUNC_ARG);
  13414. ExpectIntEQ(wc_Sha3_512_GetHash(&sha3, NULL), BAD_FUNC_ARG);
  13415. wc_Sha3_512_Free(&sha3);
  13416. #endif
  13417. return EXPECT_RESULT();
  13418. } /* END test_wc_Sha3_512_Final */
  13419. /*
  13420. * Testing wc_Sha3_224_Copy()
  13421. */
  13422. static int test_wc_Sha3_224_Copy(void)
  13423. {
  13424. EXPECT_DECLS;
  13425. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_224)
  13426. wc_Sha3 sha3, sha3Cpy;
  13427. const char* msg = TEST_STRING;
  13428. word32 msglen = (word32)TEST_STRING_SZ;
  13429. byte hash[WC_SHA3_224_DIGEST_SIZE];
  13430. byte hashCpy[WC_SHA3_224_DIGEST_SIZE];
  13431. XMEMSET(hash, 0, sizeof(hash));
  13432. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13433. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13434. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13435. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13436. ExpectIntEQ(wc_InitSha3_224(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13437. ExpectIntEQ(wc_Sha3_224_Update(&sha3, (byte*)msg, msglen), 0);
  13438. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, &sha3), 0);
  13439. ExpectIntEQ(wc_Sha3_224_Final(&sha3, hash), 0);
  13440. ExpectIntEQ(wc_Sha3_224_Final(&sha3Cpy, hashCpy), 0);
  13441. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13442. /* Test bad args. */
  13443. ExpectIntEQ(wc_Sha3_224_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13444. ExpectIntEQ(wc_Sha3_224_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13445. wc_Sha3_224_Free(&sha3);
  13446. wc_Sha3_224_Free(&sha3Cpy);
  13447. #endif
  13448. return EXPECT_RESULT();
  13449. } /* END test_wc_Sha3_224_Copy */
  13450. /*
  13451. * Testing wc_Sha3_256_Copy()
  13452. */
  13453. static int test_wc_Sha3_256_Copy(void)
  13454. {
  13455. EXPECT_DECLS;
  13456. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  13457. wc_Sha3 sha3, sha3Cpy;
  13458. const char* msg = TEST_STRING;
  13459. word32 msglen = (word32)TEST_STRING_SZ;
  13460. byte hash[WC_SHA3_256_DIGEST_SIZE];
  13461. byte hashCpy[WC_SHA3_256_DIGEST_SIZE];
  13462. XMEMSET(hash, 0, sizeof(hash));
  13463. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13464. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13465. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13466. ExpectIntEQ(wc_InitSha3_256(&sha3, HEAP_HINT, testDevId), 0);
  13467. ExpectIntEQ(wc_InitSha3_256(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13468. ExpectIntEQ(wc_Sha3_256_Update(&sha3, (byte*)msg, msglen), 0);
  13469. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, &sha3), 0);
  13470. ExpectIntEQ(wc_Sha3_256_Final(&sha3, hash), 0);
  13471. ExpectIntEQ(wc_Sha3_256_Final(&sha3Cpy, hashCpy), 0);
  13472. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13473. /* Test bad args. */
  13474. ExpectIntEQ(wc_Sha3_256_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13475. ExpectIntEQ(wc_Sha3_256_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13476. wc_Sha3_256_Free(&sha3);
  13477. wc_Sha3_256_Free(&sha3Cpy);
  13478. #endif
  13479. return EXPECT_RESULT();
  13480. } /* END test_wc_Sha3_256_Copy */
  13481. /*
  13482. * Testing wc_Sha3_384_Copy()
  13483. */
  13484. static int test_wc_Sha3_384_Copy(void)
  13485. {
  13486. EXPECT_DECLS;
  13487. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_384)
  13488. wc_Sha3 sha3, sha3Cpy;
  13489. const char* msg = TEST_STRING;
  13490. word32 msglen = (word32)TEST_STRING_SZ;
  13491. byte hash[WC_SHA3_384_DIGEST_SIZE];
  13492. byte hashCpy[WC_SHA3_384_DIGEST_SIZE];
  13493. XMEMSET(hash, 0, sizeof(hash));
  13494. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13495. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13496. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13497. ExpectIntEQ(wc_InitSha3_384(&sha3, HEAP_HINT, testDevId), 0);
  13498. ExpectIntEQ(wc_InitSha3_384(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13499. ExpectIntEQ(wc_Sha3_384_Update(&sha3, (byte*)msg, msglen), 0);
  13500. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, &sha3), 0);
  13501. ExpectIntEQ(wc_Sha3_384_Final(&sha3, hash), 0);
  13502. ExpectIntEQ(wc_Sha3_384_Final(&sha3Cpy, hashCpy), 0);
  13503. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13504. /* Test bad args. */
  13505. ExpectIntEQ(wc_Sha3_384_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13506. ExpectIntEQ(wc_Sha3_384_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13507. wc_Sha3_384_Free(&sha3);
  13508. wc_Sha3_384_Free(&sha3Cpy);
  13509. #endif
  13510. return EXPECT_RESULT();
  13511. } /* END test_wc_Sha3_384_Copy */
  13512. /*
  13513. * Testing wc_Sha3_512_Copy()
  13514. */
  13515. static int test_wc_Sha3_512_Copy(void)
  13516. {
  13517. EXPECT_DECLS;
  13518. #if defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_512)
  13519. wc_Sha3 sha3, sha3Cpy;
  13520. const char* msg = TEST_STRING;
  13521. word32 msglen = (word32)TEST_STRING_SZ;
  13522. byte hash[WC_SHA3_512_DIGEST_SIZE];
  13523. byte hashCpy[WC_SHA3_512_DIGEST_SIZE];
  13524. XMEMSET(hash, 0, sizeof(hash));
  13525. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13526. XMEMSET(&sha3, 0, sizeof(wc_Sha3));
  13527. XMEMSET(&sha3Cpy, 0, sizeof(wc_Sha3));
  13528. ExpectIntEQ(wc_InitSha3_512(&sha3, HEAP_HINT, testDevId), 0);
  13529. ExpectIntEQ(wc_InitSha3_512(&sha3Cpy, HEAP_HINT, testDevId), 0);
  13530. ExpectIntEQ(wc_Sha3_512_Update(&sha3, (byte*)msg, msglen), 0);
  13531. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, &sha3), 0);
  13532. ExpectIntEQ(wc_Sha3_512_Final(&sha3, hash), 0);
  13533. ExpectIntEQ(wc_Sha3_512_Final(&sha3Cpy, hashCpy), 0);
  13534. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13535. /* Test bad args. */
  13536. ExpectIntEQ(wc_Sha3_512_Copy(NULL, &sha3), BAD_FUNC_ARG);
  13537. ExpectIntEQ(wc_Sha3_512_Copy(&sha3Cpy, NULL), BAD_FUNC_ARG);
  13538. wc_Sha3_512_Free(&sha3);
  13539. wc_Sha3_512_Free(&sha3Cpy);
  13540. #endif
  13541. return EXPECT_RESULT();
  13542. } /* END test_wc_Sha3_512_Copy */
  13543. /*
  13544. * Unit test function for wc_Sha3_GetFlags()
  13545. */
  13546. static int test_wc_Sha3_GetFlags(void)
  13547. {
  13548. EXPECT_DECLS;
  13549. #if defined(WOLFSSL_SHA3) && defined(WOLFSSL_HASH_FLAGS)
  13550. wc_Sha3 sha3;
  13551. word32 flags = 0;
  13552. /* Initialize */
  13553. ExpectIntEQ(wc_InitSha3_224(&sha3, HEAP_HINT, testDevId), 0);
  13554. ExpectIntEQ(wc_Sha3_GetFlags(&sha3, &flags), 0);
  13555. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  13556. wc_Sha3_224_Free(&sha3);
  13557. #endif
  13558. return EXPECT_RESULT();
  13559. } /* END test_wc_Sha3_GetFlags */
  13560. static int test_wc_InitShake256(void)
  13561. {
  13562. EXPECT_DECLS;
  13563. #ifdef WOLFSSL_SHAKE256
  13564. wc_Shake shake;
  13565. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13566. /* Test bad args. */
  13567. ExpectIntEQ(wc_InitShake256(NULL, HEAP_HINT, testDevId), BAD_FUNC_ARG);
  13568. wc_Shake256_Free(&shake);
  13569. #endif
  13570. return EXPECT_RESULT();
  13571. }
  13572. static int testing_wc_Shake256_Update(void)
  13573. {
  13574. EXPECT_DECLS;
  13575. #ifdef WOLFSSL_SHAKE256
  13576. wc_Shake shake;
  13577. byte msg[] = "Everybody's working for the weekend.";
  13578. byte msg2[] = "Everybody gets Friday off.";
  13579. byte msgCmp[] = "\x45\x76\x65\x72\x79\x62\x6f\x64\x79\x27\x73\x20"
  13580. "\x77\x6f\x72\x6b\x69\x6e\x67\x20\x66\x6f\x72\x20\x74"
  13581. "\x68\x65\x20\x77\x65\x65\x6b\x65\x6e\x64\x2e\x45\x76"
  13582. "\x65\x72\x79\x62\x6f\x64\x79\x20\x67\x65\x74\x73\x20"
  13583. "\x46\x72\x69\x64\x61\x79\x20\x6f\x66\x66\x2e";
  13584. word32 msglen = sizeof(msg) - 1;
  13585. word32 msg2len = sizeof(msg2);
  13586. word32 msgCmplen = sizeof(msgCmp);
  13587. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13588. ExpectIntEQ(wc_Shake256_Update(&shake, msg, msglen), 0);
  13589. ExpectIntEQ(XMEMCMP(msg, shake.t, msglen), 0);
  13590. ExpectTrue(shake.i == msglen);
  13591. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  13592. ExpectIntEQ(XMEMCMP(shake.t, msgCmp, msgCmplen), 0);
  13593. /* Pass bad args. */
  13594. ExpectIntEQ(wc_Shake256_Update(NULL, msg2, msg2len), BAD_FUNC_ARG);
  13595. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 5), BAD_FUNC_ARG);
  13596. wc_Shake256_Free(&shake);
  13597. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13598. ExpectIntEQ(wc_Shake256_Update(&shake, NULL, 0), 0);
  13599. ExpectIntEQ(wc_Shake256_Update(&shake, msg2, msg2len), 0);
  13600. ExpectIntEQ(XMEMCMP(msg2, shake.t, msg2len), 0);
  13601. wc_Shake256_Free(&shake);
  13602. #endif /* WOLFSSL_SHAKE256 */
  13603. return EXPECT_RESULT();
  13604. }
  13605. static int test_wc_Shake256_Final(void)
  13606. {
  13607. EXPECT_DECLS;
  13608. #ifdef WOLFSSL_SHAKE256
  13609. wc_Shake shake;
  13610. const char* msg = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnom"
  13611. "nopnopq";
  13612. const char* expOut = "\x4d\x8c\x2d\xd2\x43\x5a\x01\x28\xee\xfb\xb8\xc3\x6f"
  13613. "\x6f\x87\x13\x3a\x79\x11\xe1\x8d\x97\x9e\xe1\xae\x6b"
  13614. "\xe5\xd4\xfd\x2e\x33\x29\x40\xd8\x68\x8a\x4e\x6a\x59"
  13615. "\xaa\x80\x60\xf1\xf9\xbc\x99\x6c\x05\xac\xa3\xc6\x96"
  13616. "\xa8\xb6\x62\x79\xdc\x67\x2c\x74\x0b\xb2\x24\xec\x37"
  13617. "\xa9\x2b\x65\xdb\x05\x39\xc0\x20\x34\x55\xf5\x1d\x97"
  13618. "\xcc\xe4\xcf\xc4\x91\x27\xd7\x26\x0a\xfc\x67\x3a\xf2"
  13619. "\x08\xba\xf1\x9b\xe2\x12\x33\xf3\xde\xbe\x78\xd0\x67"
  13620. "\x60\xcf\xa5\x51\xee\x1e\x07\x91\x41\xd4";
  13621. byte hash[114];
  13622. /* Init stack variables. */
  13623. XMEMSET(hash, 0, sizeof(hash));
  13624. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13625. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, (word32)XSTRLEN(msg)),
  13626. 0);
  13627. ExpectIntEQ(wc_Shake256_Final(&shake, hash, (word32)sizeof(hash)), 0);
  13628. ExpectIntEQ(XMEMCMP(expOut, hash, (word32)sizeof(hash)), 0);
  13629. /* Test bad args. */
  13630. ExpectIntEQ(wc_Shake256_Final(NULL, hash, (word32)sizeof(hash)),
  13631. BAD_FUNC_ARG);
  13632. ExpectIntEQ(wc_Shake256_Final(&shake, NULL, (word32)sizeof(hash)),
  13633. BAD_FUNC_ARG);
  13634. wc_Shake256_Free(&shake);
  13635. #endif
  13636. return EXPECT_RESULT();
  13637. }
  13638. /*
  13639. * Testing wc_Shake256_Copy()
  13640. */
  13641. static int test_wc_Shake256_Copy(void)
  13642. {
  13643. EXPECT_DECLS;
  13644. #ifdef WOLFSSL_SHAKE256
  13645. wc_Shake shake, shakeCpy;
  13646. const char* msg = TEST_STRING;
  13647. word32 msglen = (word32)TEST_STRING_SZ;
  13648. byte hash[144];
  13649. byte hashCpy[144];
  13650. word32 hashLen = sizeof(hash);
  13651. word32 hashLenCpy = sizeof(hashCpy);
  13652. XMEMSET(hash, 0, sizeof(hash));
  13653. XMEMSET(hashCpy, 0, sizeof(hashCpy));
  13654. ExpectIntEQ(wc_InitShake256(&shake, HEAP_HINT, testDevId), 0);
  13655. ExpectIntEQ(wc_InitShake256(&shakeCpy, HEAP_HINT, testDevId), 0);
  13656. ExpectIntEQ(wc_Shake256_Update(&shake, (byte*)msg, msglen), 0);
  13657. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, &shake), 0);
  13658. ExpectIntEQ(wc_Shake256_Final(&shake, hash, hashLen), 0);
  13659. ExpectIntEQ(wc_Shake256_Final(&shakeCpy, hashCpy, hashLenCpy), 0);
  13660. ExpectIntEQ(XMEMCMP(hash, hashCpy, sizeof(hash)), 0);
  13661. /* Test bad args. */
  13662. ExpectIntEQ(wc_Shake256_Copy(NULL, &shake), BAD_FUNC_ARG);
  13663. ExpectIntEQ(wc_Shake256_Copy(&shakeCpy, NULL), BAD_FUNC_ARG);
  13664. wc_Shake256_Free(&shake);
  13665. wc_Shake256_Free(&shakeCpy);
  13666. #endif
  13667. return EXPECT_RESULT();
  13668. } /* END test_wc_Shake256_Copy */
  13669. /*
  13670. * Unit test function for wc_Shake256Hash()
  13671. */
  13672. static int test_wc_Shake256Hash(void)
  13673. {
  13674. EXPECT_DECLS;
  13675. #ifdef WOLFSSL_SHAKE256
  13676. const byte data[] = { /* Hello World */
  13677. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  13678. 0x72,0x6c,0x64
  13679. };
  13680. word32 len = sizeof(data);
  13681. byte hash[144];
  13682. word32 hashLen = sizeof(hash);
  13683. ExpectIntEQ(wc_Shake256Hash(data, len, hash, hashLen), 0);
  13684. #endif
  13685. return EXPECT_RESULT();
  13686. } /* END test_wc_Shake256Hash */
  13687. /*
  13688. * Testing wc_InitSm3(), wc_Sm3Free()
  13689. */
  13690. static int test_wc_InitSm3Free(void)
  13691. {
  13692. EXPECT_DECLS;
  13693. #ifdef WOLFSSL_SM3
  13694. wc_Sm3 sm3;
  13695. /* Invalid Parameters */
  13696. ExpectIntEQ(wc_InitSm3(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  13697. /* Valid Parameters */
  13698. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13699. wc_Sm3Free(NULL);
  13700. wc_Sm3Free(&sm3);
  13701. #endif
  13702. return EXPECT_RESULT();
  13703. } /* END test_wc_InitSm3 */
  13704. /*
  13705. * Testing wc_Sm3Update(), wc_Sm3Final()
  13706. */
  13707. static int test_wc_Sm3UpdateFinal(void)
  13708. {
  13709. EXPECT_DECLS;
  13710. #ifdef WOLFSSL_SM3
  13711. wc_Sm3 sm3;
  13712. byte data[WC_SM3_BLOCK_SIZE * 4];
  13713. byte hash[WC_SM3_DIGEST_SIZE];
  13714. byte calcHash[WC_SM3_DIGEST_SIZE];
  13715. byte expHash[WC_SM3_DIGEST_SIZE] = {
  13716. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  13717. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  13718. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  13719. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  13720. };
  13721. word32 chunk;
  13722. word32 i;
  13723. XMEMSET(data, 0, sizeof(data));
  13724. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13725. /* Invalid Parameters */
  13726. ExpectIntEQ(wc_Sm3Update(NULL, NULL, 1), BAD_FUNC_ARG);
  13727. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 1), BAD_FUNC_ARG);
  13728. ExpectIntEQ(wc_Sm3Update(NULL, data, 1), BAD_FUNC_ARG);
  13729. /* Valid Parameters */
  13730. ExpectIntEQ(wc_Sm3Update(&sm3, NULL, 0), 0);
  13731. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13732. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13733. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE), 0);
  13734. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE - 2), 0);
  13735. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_BLOCK_SIZE * 2), 0);
  13736. /* Ensure too many bytes for lengths. */
  13737. ExpectIntEQ(wc_Sm3Update(&sm3, data, WC_SM3_PAD_SIZE), 0);
  13738. /* Invalid Parameters */
  13739. ExpectIntEQ(wc_Sm3Final(NULL, NULL), BAD_FUNC_ARG);
  13740. ExpectIntEQ(wc_Sm3Final(&sm3, NULL), BAD_FUNC_ARG);
  13741. ExpectIntEQ(wc_Sm3Final(NULL, hash), BAD_FUNC_ARG);
  13742. /* Valid Parameters */
  13743. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13744. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  13745. /* Chunk tests. */
  13746. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  13747. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13748. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  13749. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  13750. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, chunk), 0);
  13751. }
  13752. if (i < (word32)sizeof(data)) {
  13753. ExpectIntEQ(wc_Sm3Update(&sm3, data + i, (word32)sizeof(data) - i),
  13754. 0);
  13755. }
  13756. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13757. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13758. }
  13759. /* Not testing when the low 32-bit length overflows. */
  13760. wc_Sm3Free(&sm3);
  13761. #endif
  13762. return EXPECT_RESULT();
  13763. } /* END test_wc_Sm3Update */
  13764. /*
  13765. * Testing wc_Sm3GetHash()
  13766. */
  13767. static int test_wc_Sm3GetHash(void)
  13768. {
  13769. EXPECT_DECLS;
  13770. #ifdef WOLFSSL_SM3
  13771. wc_Sm3 sm3;
  13772. byte hash[WC_SM3_DIGEST_SIZE];
  13773. byte calcHash[WC_SM3_DIGEST_SIZE];
  13774. byte data[WC_SM3_BLOCK_SIZE];
  13775. XMEMSET(data, 0, sizeof(data));
  13776. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13777. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13778. /* Invalid Parameters */
  13779. ExpectIntEQ(wc_Sm3GetHash(NULL, NULL), BAD_FUNC_ARG);
  13780. ExpectIntEQ(wc_Sm3GetHash(&sm3, NULL), BAD_FUNC_ARG);
  13781. ExpectIntEQ(wc_Sm3GetHash(NULL, hash), BAD_FUNC_ARG);
  13782. /* Valid Parameters */
  13783. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  13784. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13785. /* With update. */
  13786. ExpectIntEQ(wc_Sm3Update(&sm3, data, sizeof(data)), 0);
  13787. ExpectIntEQ(wc_Sm3GetHash(&sm3, hash), 0);
  13788. ExpectIntEQ(wc_Sm3Final(&sm3, calcHash), 0);
  13789. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  13790. wc_Sm3Free(&sm3);
  13791. #endif
  13792. return EXPECT_RESULT();
  13793. } /* END test_wc_Sm3Update */
  13794. /*
  13795. * Testing wc_Sm3Copy()
  13796. */
  13797. static int test_wc_Sm3Copy(void)
  13798. {
  13799. EXPECT_DECLS;
  13800. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13801. wc_Sm3 sm3;
  13802. wc_Sm3 sm3Copy;
  13803. byte hash[WC_SM3_DIGEST_SIZE];
  13804. byte hashCopy[WC_SM3_DIGEST_SIZE];
  13805. byte data[WC_SM3_BLOCK_SIZE + 1];
  13806. int i;
  13807. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13808. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  13809. /* Invalid Parameters */
  13810. ExpectIntEQ(wc_Sm3Copy(NULL, NULL), BAD_FUNC_ARG);
  13811. ExpectIntEQ(wc_Sm3Copy(&sm3, NULL), BAD_FUNC_ARG);
  13812. ExpectIntEQ(wc_Sm3Copy(NULL, &sm3Copy), BAD_FUNC_ARG);
  13813. /* Valid Parameters */
  13814. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13815. /* Ensure all parts of data updated during hashing are copied. */
  13816. for (i = 0; i < WC_SM3_BLOCK_SIZE + 1; i++) {
  13817. ExpectIntEQ(wc_Sm3Update(&sm3, data, i), 0);
  13818. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13819. ExpectIntEQ(wc_Sm3Update(&sm3, data, 1), 0);
  13820. ExpectIntEQ(wc_Sm3Update(&sm3Copy, data, 1), 0);
  13821. ExpectIntEQ(wc_Sm3Final(&sm3, hash), 0);
  13822. ExpectIntEQ(wc_Sm3Final(&sm3Copy, hashCopy), 0);
  13823. ExpectBufEQ(hash, hashCopy, WC_SM3_DIGEST_SIZE);
  13824. }
  13825. wc_Sm3Free(&sm3Copy);
  13826. wc_Sm3Free(&sm3);
  13827. #endif
  13828. return EXPECT_RESULT();
  13829. } /* END test_wc_Sm3Copy */
  13830. /*
  13831. * Testing wc_Sm3FinalRaw()
  13832. */
  13833. static int test_wc_Sm3FinalRaw(void)
  13834. {
  13835. EXPECT_DECLS;
  13836. #if defined(WOLFSSL_SM3) && !defined(HAVE_SELFTEST) && \
  13837. !defined(WOLFSSL_DEVCRYPTO) && (!defined(HAVE_FIPS) || \
  13838. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 3))) && \
  13839. !defined(WOLFSSL_NO_HASH_RAW)
  13840. wc_Sm3 sm3;
  13841. byte hash1[WC_SM3_DIGEST_SIZE];
  13842. byte hash2[WC_SM3_DIGEST_SIZE];
  13843. byte hash3[WC_SM3_DIGEST_SIZE];
  13844. byte* hash_test[3] = { hash1, hash2, hash3 };
  13845. int times;
  13846. int i;
  13847. XMEMSET(&sm3, 0, sizeof(sm3));
  13848. /* Initialize */
  13849. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13850. /* Invalid Parameters */
  13851. ExpectIntEQ(wc_Sm3FinalRaw(NULL, NULL), BAD_FUNC_ARG);
  13852. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, NULL), BAD_FUNC_ARG);
  13853. ExpectIntEQ(wc_Sm3FinalRaw(NULL, hash1), BAD_FUNC_ARG);
  13854. times = sizeof(hash_test) / sizeof(byte*);
  13855. for (i = 0; i < times; i++) {
  13856. ExpectIntEQ(wc_Sm3FinalRaw(&sm3, hash_test[i]), 0);
  13857. }
  13858. wc_Sm3Free(&sm3);
  13859. #endif
  13860. return EXPECT_RESULT();
  13861. } /* END test_wc_Sm3FinalRaw */
  13862. /*
  13863. * Testing wc_Sm3GetFlags, wc_Sm3SetFlags()
  13864. */
  13865. static int test_wc_Sm3GetSetFlags(void)
  13866. {
  13867. EXPECT_DECLS;
  13868. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13869. wc_Sm3 sm3;
  13870. wc_Sm3 sm3Copy;
  13871. word32 flags = 0;
  13872. ExpectIntEQ(wc_InitSm3(&sm3, NULL, INVALID_DEVID), 0);
  13873. ExpectIntEQ(wc_InitSm3(&sm3Copy, NULL, INVALID_DEVID), 0);
  13874. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  13875. ExpectIntEQ(flags, 0);
  13876. ExpectIntEQ(wc_Sm3SetFlags(NULL, WC_HASH_FLAG_WILLCOPY), 0);
  13877. ExpectIntEQ(wc_Sm3GetFlags(NULL, &flags), 0);
  13878. ExpectIntEQ(flags, 0);
  13879. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  13880. ExpectIntEQ(flags, 0);
  13881. ExpectIntEQ(wc_Sm3SetFlags(&sm3, WC_HASH_FLAG_WILLCOPY), 0);
  13882. ExpectIntEQ(wc_Sm3GetFlags(&sm3, &flags), 0);
  13883. ExpectIntEQ(flags, WC_HASH_FLAG_WILLCOPY);
  13884. ExpectIntEQ(wc_Sm3Copy(&sm3, &sm3Copy), 0);
  13885. ExpectIntEQ(wc_Sm3GetFlags(&sm3Copy, &flags), 0);
  13886. ExpectIntEQ(flags, WC_HASH_FLAG_ISCOPY | WC_HASH_FLAG_WILLCOPY);
  13887. wc_Sm3Free(&sm3Copy);
  13888. wc_Sm3Free(&sm3);
  13889. #endif
  13890. return EXPECT_RESULT();
  13891. } /* END test_wc_Sm3Update */
  13892. /*
  13893. * Testing wc_Sm3Hash()
  13894. */
  13895. static int test_wc_Sm3Hash(void)
  13896. {
  13897. EXPECT_DECLS;
  13898. #if defined(WOLFSSL_SM3) && defined(WOLFSSL_HASH_FLAGS)
  13899. byte data[WC_SM3_BLOCK_SIZE];
  13900. byte hash[WC_SM3_DIGEST_SIZE];
  13901. /* Invalid parameters. */
  13902. ExpectIntEQ(wc_Sm3Hash(NULL, sizeof(data), hash), BAD_FUNC_ARG);
  13903. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), NULL), BAD_FUNC_ARG);
  13904. /* Valid parameters. */
  13905. ExpectIntEQ(wc_Sm3Hash(data, sizeof(data), hash), 0);
  13906. #endif
  13907. return EXPECT_RESULT();
  13908. } /* END test_wc_Sm3Hash */
  13909. /*
  13910. * Test function for wc_HmacSetKey
  13911. */
  13912. static int test_wc_Md5HmacSetKey(void)
  13913. {
  13914. EXPECT_DECLS;
  13915. #if !defined(NO_HMAC) && !defined(NO_MD5)
  13916. Hmac hmac;
  13917. int ret, times, itr;
  13918. const char* keys[]=
  13919. {
  13920. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b",
  13921. #ifndef HAVE_FIPS
  13922. "Jefe", /* smaller than minimum FIPS key size */
  13923. #endif
  13924. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13925. };
  13926. times = sizeof(keys) / sizeof(char*);
  13927. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13928. for (itr = 0; itr < times; itr++) {
  13929. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[itr],
  13930. (word32)XSTRLEN(keys[itr]));
  13931. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13932. wc_HmacFree(&hmac);
  13933. ExpectIntEQ(ret, BAD_FUNC_ARG);
  13934. #else
  13935. ExpectIntEQ(ret, 0);
  13936. #endif
  13937. }
  13938. /* Bad args. */
  13939. ExpectIntEQ(wc_HmacSetKey(NULL, WC_MD5, (byte*)keys[0],
  13940. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13941. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, NULL, (word32)XSTRLEN(keys[0])),
  13942. BAD_FUNC_ARG);
  13943. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13944. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13945. ret = wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys[0], 0);
  13946. #if defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5)
  13947. ExpectIntEQ(ret, BAD_FUNC_ARG);
  13948. #elif defined(HAVE_FIPS)
  13949. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13950. #else
  13951. ExpectIntEQ(ret, 0);
  13952. #endif
  13953. wc_HmacFree(&hmac);
  13954. #endif
  13955. return EXPECT_RESULT();
  13956. } /* END test_wc_Md5HmacSetKey */
  13957. /*
  13958. * testing wc_HmacSetKey() on wc_Sha hash.
  13959. */
  13960. static int test_wc_ShaHmacSetKey(void)
  13961. {
  13962. EXPECT_DECLS;
  13963. #if !defined(NO_HMAC) && !defined(NO_SHA)
  13964. Hmac hmac;
  13965. int ret, times, itr;
  13966. const char* keys[]=
  13967. {
  13968. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  13969. "\x0b\x0b\x0b",
  13970. #ifndef HAVE_FIPS
  13971. "Jefe", /* smaller than minimum FIPS key size */
  13972. #endif
  13973. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  13974. "\xAA\xAA\xAA"
  13975. };
  13976. times = sizeof(keys) / sizeof(char*);
  13977. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  13978. for (itr = 0; itr < times; itr++) {
  13979. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[itr],
  13980. (word32)XSTRLEN(keys[itr])), 0);
  13981. }
  13982. /* Bad args. */
  13983. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA, (byte*)keys[0],
  13984. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13985. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, NULL, (word32)XSTRLEN(keys[0])),
  13986. BAD_FUNC_ARG);
  13987. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  13988. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  13989. ret = wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys[0], 0);
  13990. #ifdef HAVE_FIPS
  13991. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  13992. #else
  13993. ExpectIntEQ(ret, 0);
  13994. #endif
  13995. wc_HmacFree(&hmac);
  13996. #endif
  13997. return EXPECT_RESULT();
  13998. } /* END test_wc_ShaHmacSetKey() */
  13999. /*
  14000. * testing wc_HmacSetKey() on Sha224 hash.
  14001. */
  14002. static int test_wc_Sha224HmacSetKey(void)
  14003. {
  14004. EXPECT_DECLS;
  14005. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14006. Hmac hmac;
  14007. int ret, times, itr;
  14008. const char* keys[]=
  14009. {
  14010. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14011. "\x0b\x0b\x0b",
  14012. #ifndef HAVE_FIPS
  14013. "Jefe", /* smaller than minimum FIPS key size */
  14014. #endif
  14015. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14016. "\xAA\xAA\xAA"
  14017. };
  14018. times = sizeof(keys) / sizeof(char*);
  14019. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14020. for (itr = 0; itr < times; itr++) {
  14021. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[itr],
  14022. (word32)XSTRLEN(keys[itr])), 0);
  14023. }
  14024. /* Bad args. */
  14025. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA224, (byte*)keys[0],
  14026. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14027. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, NULL, (word32)XSTRLEN(keys[0])),
  14028. BAD_FUNC_ARG);
  14029. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14030. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14031. ret = wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys[0], 0);
  14032. #ifdef HAVE_FIPS
  14033. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14034. #else
  14035. ExpectIntEQ(ret, 0);
  14036. #endif
  14037. wc_HmacFree(&hmac);
  14038. #endif
  14039. return EXPECT_RESULT();
  14040. } /* END test_wc_Sha224HmacSetKey() */
  14041. /*
  14042. * testing wc_HmacSetKey() on Sha256 hash
  14043. */
  14044. static int test_wc_Sha256HmacSetKey(void)
  14045. {
  14046. EXPECT_DECLS;
  14047. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14048. Hmac hmac;
  14049. int ret, times, itr;
  14050. const char* keys[]=
  14051. {
  14052. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14053. "\x0b\x0b\x0b",
  14054. #ifndef HAVE_FIPS
  14055. "Jefe", /* smaller than minimum FIPS key size */
  14056. #endif
  14057. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14058. "\xAA\xAA\xAA"
  14059. };
  14060. times = sizeof(keys) / sizeof(char*);
  14061. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14062. for (itr = 0; itr < times; itr++) {
  14063. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[itr],
  14064. (word32)XSTRLEN(keys[itr])), 0);
  14065. }
  14066. /* Bad args. */
  14067. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA256, (byte*)keys[0],
  14068. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14069. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, NULL, (word32)XSTRLEN(keys[0])),
  14070. BAD_FUNC_ARG);
  14071. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14072. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14073. ret = wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys[0], 0);
  14074. #ifdef HAVE_FIPS
  14075. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14076. #else
  14077. ExpectIntEQ(ret, 0);
  14078. #endif
  14079. wc_HmacFree(&hmac);
  14080. #endif
  14081. return EXPECT_RESULT();
  14082. } /* END test_wc_Sha256HmacSetKey() */
  14083. /*
  14084. * testing wc_HmacSetKey on Sha384 hash.
  14085. */
  14086. static int test_wc_Sha384HmacSetKey(void)
  14087. {
  14088. EXPECT_DECLS;
  14089. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14090. Hmac hmac;
  14091. int ret, times, itr;
  14092. const char* keys[]=
  14093. {
  14094. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14095. "\x0b\x0b\x0b",
  14096. #ifndef HAVE_FIPS
  14097. "Jefe", /* smaller than minimum FIPS key size */
  14098. #endif
  14099. "\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA\xAA"
  14100. "\xAA\xAA\xAA"
  14101. };
  14102. times = sizeof(keys) / sizeof(char*);
  14103. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14104. for (itr = 0; itr < times; itr++) {
  14105. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[itr],
  14106. (word32)XSTRLEN(keys[itr])), 0);
  14107. }
  14108. /* Bad args. */
  14109. ExpectIntEQ(wc_HmacSetKey(NULL, WC_SHA384, (byte*)keys[0],
  14110. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14111. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, NULL, (word32)XSTRLEN(keys[0])),
  14112. BAD_FUNC_ARG);
  14113. ExpectIntEQ(wc_HmacSetKey(&hmac, 21, (byte*)keys[0],
  14114. (word32)XSTRLEN(keys[0])), BAD_FUNC_ARG);
  14115. ret = wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys[0], 0);
  14116. #ifdef HAVE_FIPS
  14117. ExpectIntEQ(ret, HMAC_MIN_KEYLEN_E);
  14118. #else
  14119. ExpectIntEQ(ret, 0);
  14120. #endif
  14121. wc_HmacFree(&hmac);
  14122. #endif
  14123. return EXPECT_RESULT();
  14124. } /* END test_wc_Sha384HmacSetKey() */
  14125. /*
  14126. * testing wc_HmacUpdate on wc_Md5 hash.
  14127. */
  14128. static int test_wc_Md5HmacUpdate(void)
  14129. {
  14130. EXPECT_DECLS;
  14131. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  14132. Hmac hmac;
  14133. testVector a, b;
  14134. #ifdef HAVE_FIPS
  14135. const char* keys =
  14136. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14137. #else
  14138. const char* keys = "Jefe";
  14139. #endif
  14140. a.input = "what do ya want for nothing?";
  14141. a.inLen = XSTRLEN(a.input);
  14142. b.input = "Hi There";
  14143. b.inLen = XSTRLEN(b.input);
  14144. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14145. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)keys,
  14146. (word32)XSTRLEN(keys)), 0);
  14147. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14148. /* Update Hmac. */
  14149. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14150. /* Test bad args. */
  14151. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14152. BAD_FUNC_ARG);
  14153. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14154. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14155. wc_HmacFree(&hmac);
  14156. #endif
  14157. return EXPECT_RESULT();
  14158. } /* END test_wc_Md5HmacUpdate */
  14159. /*
  14160. * testing wc_HmacUpdate on SHA hash.
  14161. */
  14162. static int test_wc_ShaHmacUpdate(void)
  14163. {
  14164. EXPECT_DECLS;
  14165. #if !defined(NO_HMAC) && !defined(NO_SHA)
  14166. Hmac hmac;
  14167. testVector a, b;
  14168. #ifdef HAVE_FIPS
  14169. const char* keys =
  14170. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14171. #else
  14172. const char* keys = "Jefe";
  14173. #endif
  14174. a.input = "what do ya want for nothing?";
  14175. a.inLen = XSTRLEN(a.input);
  14176. b.input = "Hi There";
  14177. b.inLen = XSTRLEN(b.input);
  14178. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14179. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)keys,
  14180. (word32)XSTRLEN(keys)), 0);
  14181. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14182. /* Update Hmac. */
  14183. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14184. /* Test bad args. */
  14185. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14186. BAD_FUNC_ARG);
  14187. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14188. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14189. wc_HmacFree(&hmac);
  14190. #endif
  14191. return EXPECT_RESULT();
  14192. } /* END test_wc_ShaHmacUpdate */
  14193. /*
  14194. * testing wc_HmacUpdate on SHA224 hash.
  14195. */
  14196. static int test_wc_Sha224HmacUpdate(void)
  14197. {
  14198. EXPECT_DECLS;
  14199. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14200. Hmac hmac;
  14201. testVector a, b;
  14202. #ifdef HAVE_FIPS
  14203. const char* keys =
  14204. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14205. #else
  14206. const char* keys = "Jefe";
  14207. #endif
  14208. a.input = "what do ya want for nothing?";
  14209. a.inLen = XSTRLEN(a.input);
  14210. b.input = "Hi There";
  14211. b.inLen = XSTRLEN(b.input);
  14212. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14213. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)keys,
  14214. (word32)XSTRLEN(keys)), 0);
  14215. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14216. /* Update Hmac. */
  14217. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14218. /* Test bad args. */
  14219. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14220. BAD_FUNC_ARG);
  14221. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14222. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14223. wc_HmacFree(&hmac);
  14224. #endif
  14225. return EXPECT_RESULT();
  14226. } /* END test_wc_Sha224HmacUpdate */
  14227. /*
  14228. * testing wc_HmacUpdate on SHA256 hash.
  14229. */
  14230. static int test_wc_Sha256HmacUpdate(void)
  14231. {
  14232. EXPECT_DECLS;
  14233. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14234. Hmac hmac;
  14235. testVector a, b;
  14236. #ifdef HAVE_FIPS
  14237. const char* keys =
  14238. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14239. #else
  14240. const char* keys = "Jefe";
  14241. #endif
  14242. a.input = "what do ya want for nothing?";
  14243. a.inLen = XSTRLEN(a.input);
  14244. b.input = "Hi There";
  14245. b.inLen = XSTRLEN(b.input);
  14246. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14247. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)keys,
  14248. (word32)XSTRLEN(keys)), 0);
  14249. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14250. /* Update Hmac. */
  14251. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14252. /* Test bad args. */
  14253. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14254. BAD_FUNC_ARG);
  14255. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14256. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14257. wc_HmacFree(&hmac);
  14258. #endif
  14259. return EXPECT_RESULT();
  14260. } /* END test_wc_Sha256HmacUpdate */
  14261. /*
  14262. * testing wc_HmacUpdate on SHA384 hash.
  14263. */
  14264. static int test_wc_Sha384HmacUpdate(void)
  14265. {
  14266. EXPECT_DECLS;
  14267. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14268. Hmac hmac;
  14269. testVector a, b;
  14270. #ifdef HAVE_FIPS
  14271. const char* keys =
  14272. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14273. #else
  14274. const char* keys = "Jefe";
  14275. #endif
  14276. a.input = "what do ya want for nothing?";
  14277. a.inLen = XSTRLEN(a.input);
  14278. b.input = "Hi There";
  14279. b.inLen = XSTRLEN(b.input);
  14280. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14281. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)keys,
  14282. (word32)XSTRLEN(keys)), 0);
  14283. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)b.input, (word32)b.inLen), 0);
  14284. /* Update Hmac. */
  14285. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14286. /* Test bad args. */
  14287. ExpectIntEQ(wc_HmacUpdate(NULL, (byte*)a.input, (word32)a.inLen),
  14288. BAD_FUNC_ARG);
  14289. ExpectIntEQ(wc_HmacUpdate(&hmac, NULL, (word32)a.inLen), BAD_FUNC_ARG);
  14290. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, 0), 0);
  14291. wc_HmacFree(&hmac);
  14292. #endif
  14293. return EXPECT_RESULT();
  14294. } /* END test_wc_Sha384HmacUpdate */
  14295. /*
  14296. * Testing wc_HmacFinal() with MD5
  14297. */
  14298. static int test_wc_Md5HmacFinal(void)
  14299. {
  14300. EXPECT_DECLS;
  14301. #if !defined(NO_HMAC) && !defined(NO_MD5) && !(defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 5))
  14302. Hmac hmac;
  14303. byte hash[WC_MD5_DIGEST_SIZE];
  14304. testVector a;
  14305. const char* key;
  14306. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  14307. a.input = "Hi There";
  14308. a.output = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc"
  14309. "\x9d";
  14310. a.inLen = XSTRLEN(a.input);
  14311. a.outLen = XSTRLEN(a.output);
  14312. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14313. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_MD5, (byte*)key, (word32)XSTRLEN(key)),
  14314. 0);
  14315. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14316. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14317. ExpectIntEQ(XMEMCMP(hash, a.output, WC_MD5_DIGEST_SIZE), 0);
  14318. /* Try bad parameters. */
  14319. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14320. #ifndef HAVE_FIPS
  14321. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14322. #endif
  14323. wc_HmacFree(&hmac);
  14324. #endif
  14325. return EXPECT_RESULT();
  14326. } /* END test_wc_Md5HmacFinal */
  14327. /*
  14328. * Testing wc_HmacFinal() with SHA
  14329. */
  14330. static int test_wc_ShaHmacFinal(void)
  14331. {
  14332. EXPECT_DECLS;
  14333. #if !defined(NO_HMAC) && !defined(NO_SHA)
  14334. Hmac hmac;
  14335. byte hash[WC_SHA_DIGEST_SIZE];
  14336. testVector a;
  14337. const char* key;
  14338. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14339. "\x0b\x0b\x0b";
  14340. a.input = "Hi There";
  14341. a.output = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c"
  14342. "\x8e\xf1\x46\xbe\x00";
  14343. a.inLen = XSTRLEN(a.input);
  14344. a.outLen = XSTRLEN(a.output);
  14345. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14346. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA, (byte*)key, (word32)XSTRLEN(key)),
  14347. 0);
  14348. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14349. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14350. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA_DIGEST_SIZE), 0);
  14351. /* Try bad parameters. */
  14352. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14353. #ifndef HAVE_FIPS
  14354. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14355. #endif
  14356. wc_HmacFree(&hmac);
  14357. #endif
  14358. return EXPECT_RESULT();
  14359. } /* END test_wc_ShaHmacFinal */
  14360. /*
  14361. * Testing wc_HmacFinal() with SHA224
  14362. */
  14363. static int test_wc_Sha224HmacFinal(void)
  14364. {
  14365. EXPECT_DECLS;
  14366. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA224)
  14367. Hmac hmac;
  14368. byte hash[WC_SHA224_DIGEST_SIZE];
  14369. testVector a;
  14370. const char* key;
  14371. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14372. "\x0b\x0b\x0b";
  14373. a.input = "Hi There";
  14374. a.output = "\x89\x6f\xb1\x12\x8a\xbb\xdf\x19\x68\x32\x10\x7c\xd4\x9d\xf3"
  14375. "\x3f\x47\xb4\xb1\x16\x99\x12\xba\x4f\x53\x68\x4b\x22";
  14376. a.inLen = XSTRLEN(a.input);
  14377. a.outLen = XSTRLEN(a.output);
  14378. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14379. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA224, (byte*)key,
  14380. (word32)XSTRLEN(key)), 0);
  14381. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14382. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14383. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA224_DIGEST_SIZE), 0);
  14384. /* Try bad parameters. */
  14385. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14386. #ifndef HAVE_FIPS
  14387. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14388. #endif
  14389. wc_HmacFree(&hmac);
  14390. #endif
  14391. return EXPECT_RESULT();
  14392. } /* END test_wc_Sha224HmacFinal */
  14393. /*
  14394. * Testing wc_HmacFinal() with SHA256
  14395. */
  14396. static int test_wc_Sha256HmacFinal(void)
  14397. {
  14398. EXPECT_DECLS;
  14399. #if !defined(NO_HMAC) && !defined(NO_SHA256)
  14400. Hmac hmac;
  14401. byte hash[WC_SHA256_DIGEST_SIZE];
  14402. testVector a;
  14403. const char* key;
  14404. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14405. "\x0b\x0b\x0b";
  14406. a.input = "Hi There";
  14407. a.output = "\xb0\x34\x4c\x61\xd8\xdb\x38\x53\x5c\xa8\xaf\xce\xaf\x0b\xf1"
  14408. "\x2b\x88\x1d\xc2\x00\xc9\x83\x3d\xa7\x26\xe9\x37\x6c\x2e\x32"
  14409. "\xcf\xf7";
  14410. a.inLen = XSTRLEN(a.input);
  14411. a.outLen = XSTRLEN(a.output);
  14412. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14413. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA256, (byte*)key,
  14414. (word32)XSTRLEN(key)), 0);
  14415. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14416. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14417. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA256_DIGEST_SIZE), 0);
  14418. /* Try bad parameters. */
  14419. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14420. #ifndef HAVE_FIPS
  14421. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14422. #endif
  14423. wc_HmacFree(&hmac);
  14424. #endif
  14425. return EXPECT_RESULT();
  14426. } /* END test_wc_Sha256HmacFinal */
  14427. /*
  14428. * Testing wc_HmacFinal() with SHA384
  14429. */
  14430. static int test_wc_Sha384HmacFinal(void)
  14431. {
  14432. EXPECT_DECLS;
  14433. #if !defined(NO_HMAC) && defined(WOLFSSL_SHA384)
  14434. Hmac hmac;
  14435. byte hash[WC_SHA384_DIGEST_SIZE];
  14436. testVector a;
  14437. const char* key;
  14438. key = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  14439. "\x0b\x0b\x0b";
  14440. a.input = "Hi There";
  14441. a.output = "\xaf\xd0\x39\x44\xd8\x48\x95\x62\x6b\x08\x25\xf4\xab\x46\x90"
  14442. "\x7f\x15\xf9\xda\xdb\xe4\x10\x1e\xc6\x82\xaa\x03\x4c\x7c\xeb"
  14443. "\xc5\x9c\xfa\xea\x9e\xa9\x07\x6e\xde\x7f\x4a\xf1\x52\xe8\xb2"
  14444. "\xfa\x9c\xb6";
  14445. a.inLen = XSTRLEN(a.input);
  14446. a.outLen = XSTRLEN(a.output);
  14447. ExpectIntEQ(wc_HmacInit(&hmac, NULL, INVALID_DEVID), 0);
  14448. ExpectIntEQ(wc_HmacSetKey(&hmac, WC_SHA384, (byte*)key,
  14449. (word32)XSTRLEN(key)), 0);
  14450. ExpectIntEQ(wc_HmacUpdate(&hmac, (byte*)a.input, (word32)a.inLen), 0);
  14451. ExpectIntEQ(wc_HmacFinal(&hmac, hash), 0);
  14452. ExpectIntEQ(XMEMCMP(hash, a.output, WC_SHA384_DIGEST_SIZE), 0);
  14453. /* Try bad parameters. */
  14454. ExpectIntEQ(wc_HmacFinal(NULL, hash), BAD_FUNC_ARG);
  14455. #ifndef HAVE_FIPS
  14456. ExpectIntEQ(wc_HmacFinal(&hmac, NULL), BAD_FUNC_ARG);
  14457. #endif
  14458. wc_HmacFree(&hmac);
  14459. #endif
  14460. return EXPECT_RESULT();
  14461. } /* END test_wc_Sha384HmacFinal */
  14462. /*
  14463. * Testing wc_InitCmac()
  14464. */
  14465. static int test_wc_InitCmac(void)
  14466. {
  14467. EXPECT_DECLS;
  14468. #if defined(WOLFSSL_CMAC) && !defined(NO_AES)
  14469. Cmac cmac1;
  14470. Cmac cmac2;
  14471. Cmac cmac3;
  14472. /* AES 128 key. */
  14473. byte key1[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14474. "\x09\x10\x11\x12\x13\x14\x15\x16";
  14475. /* AES 192 key. */
  14476. byte key2[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14477. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14478. "\x01\x02\x03\x04\x05\x06\x07\x08";
  14479. /* AES 256 key. */
  14480. byte key3[] = "\x01\x02\x03\x04\x05\x06\x07\x08"
  14481. "\x09\x01\x11\x12\x13\x14\x15\x16"
  14482. "\x01\x02\x03\x04\x05\x06\x07\x08"
  14483. "\x09\x01\x11\x12\x13\x14\x15\x16";
  14484. word32 key1Sz = (word32)sizeof(key1) - 1;
  14485. word32 key2Sz = (word32)sizeof(key2) - 1;
  14486. word32 key3Sz = (word32)sizeof(key3) - 1;
  14487. int type = WC_CMAC_AES;
  14488. (void)key1;
  14489. (void)key1Sz;
  14490. (void)key2;
  14491. (void)key2Sz;
  14492. XMEMSET(&cmac1, 0, sizeof(Cmac));
  14493. XMEMSET(&cmac2, 0, sizeof(Cmac));
  14494. XMEMSET(&cmac3, 0, sizeof(Cmac));
  14495. #ifdef WOLFSSL_AES_128
  14496. ExpectIntEQ(wc_InitCmac(&cmac1, key1, key1Sz, type, NULL), 0);
  14497. #endif
  14498. #ifdef WOLFSSL_AES_192
  14499. wc_AesFree(&cmac1.aes);
  14500. ExpectIntEQ(wc_InitCmac(&cmac2, key2, key2Sz, type, NULL), 0);
  14501. #endif
  14502. #ifdef WOLFSSL_AES_256
  14503. wc_AesFree(&cmac2.aes);
  14504. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, type, NULL), 0);
  14505. #endif
  14506. wc_AesFree(&cmac3.aes);
  14507. /* Test bad args. */
  14508. ExpectIntEQ(wc_InitCmac(NULL, key3, key3Sz, type, NULL), BAD_FUNC_ARG);
  14509. ExpectIntEQ(wc_InitCmac(&cmac3, NULL, key3Sz, type, NULL), BAD_FUNC_ARG);
  14510. ExpectIntEQ(wc_InitCmac(&cmac3, key3, 0, type, NULL), BAD_FUNC_ARG);
  14511. ExpectIntEQ(wc_InitCmac(&cmac3, key3, key3Sz, 0, NULL), BAD_FUNC_ARG);
  14512. #endif
  14513. return EXPECT_RESULT();
  14514. } /* END test_wc_InitCmac */
  14515. /*
  14516. * Testing wc_CmacUpdate()
  14517. */
  14518. static int test_wc_CmacUpdate(void)
  14519. {
  14520. EXPECT_DECLS;
  14521. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14522. Cmac cmac;
  14523. byte key[] = {
  14524. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14525. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14526. };
  14527. byte in[] = "\xe2\xb4\xb6\xf9\x48\x44\x02\x64"
  14528. "\x5c\x47\x80\x9e\xd5\xa8\x3a\x17"
  14529. "\xb3\x78\xcf\x85\x22\x41\x74\xd9"
  14530. "\xa0\x97\x39\x71\x62\xf1\x8e\x8f"
  14531. "\xf4";
  14532. word32 inSz = (word32)sizeof(in) - 1;
  14533. word32 keySz = (word32)sizeof(key);
  14534. int type = WC_CMAC_AES;
  14535. XMEMSET(&cmac, 0, sizeof(Cmac));
  14536. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  14537. ExpectIntEQ(wc_CmacUpdate(&cmac, in, inSz), 0);
  14538. /* Test bad args. */
  14539. ExpectIntEQ(wc_CmacUpdate(NULL, in, inSz), BAD_FUNC_ARG);
  14540. ExpectIntEQ(wc_CmacUpdate(&cmac, NULL, 30), BAD_FUNC_ARG);
  14541. wc_AesFree(&cmac.aes);
  14542. #endif
  14543. return EXPECT_RESULT();
  14544. } /* END test_wc_CmacUpdate */
  14545. /*
  14546. * Testing wc_CmacFinal()
  14547. */
  14548. static int test_wc_CmacFinal(void)
  14549. {
  14550. EXPECT_DECLS;
  14551. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14552. Cmac cmac;
  14553. byte key[] = {
  14554. 0x64, 0x4c, 0xbf, 0x12, 0x85, 0x9d, 0xf0, 0x55,
  14555. 0x7e, 0xa9, 0x1f, 0x08, 0xe0, 0x51, 0xff, 0x27
  14556. };
  14557. byte msg[] = {
  14558. 0xe2, 0xb4, 0xb6, 0xf9, 0x48, 0x44, 0x02, 0x64,
  14559. 0x5c, 0x47, 0x80, 0x9e, 0xd5, 0xa8, 0x3a, 0x17,
  14560. 0xb3, 0x78, 0xcf, 0x85, 0x22, 0x41, 0x74, 0xd9,
  14561. 0xa0, 0x97, 0x39, 0x71, 0x62, 0xf1, 0x8e, 0x8f,
  14562. 0xf4
  14563. };
  14564. /* Test vectors from CMACGenAES128.rsp from
  14565. * http://csrc.nist.gov/groups/STM/cavp/block-cipher-modes.html#cmac
  14566. * Per RFC4493 truncation of lsb is possible.
  14567. */
  14568. byte expMac[] = {
  14569. 0x4e, 0x6e, 0xc5, 0x6f, 0xf9, 0x5d, 0x0e, 0xae,
  14570. 0x1c, 0xf8, 0x3e, 0xfc, 0xf4, 0x4b, 0xeb
  14571. };
  14572. byte mac[AES_BLOCK_SIZE];
  14573. word32 msgSz = (word32)sizeof(msg);
  14574. word32 keySz = (word32)sizeof(key);
  14575. word32 macSz = sizeof(mac);
  14576. word32 badMacSz = 17;
  14577. int expMacSz = sizeof(expMac);
  14578. int type = WC_CMAC_AES;
  14579. XMEMSET(&cmac, 0, sizeof(Cmac));
  14580. XMEMSET(mac, 0, macSz);
  14581. ExpectIntEQ(wc_InitCmac(&cmac, key, keySz, type, NULL), 0);
  14582. ExpectIntEQ(wc_CmacUpdate(&cmac, msg, msgSz), 0);
  14583. #if (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  14584. /* Pass in bad args. */
  14585. ExpectIntEQ(wc_CmacFinalNoFree(NULL, mac, &macSz), BAD_FUNC_ARG);
  14586. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  14587. ExpectIntEQ(wc_CmacFinalNoFree(&cmac, mac, &badMacSz), BUFFER_E);
  14588. /* For the last call, use the API with implicit wc_CmacFree(). */
  14589. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  14590. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14591. #else /* !HAVE_FIPS || FIPS>=5.3 */
  14592. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &macSz), 0);
  14593. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14594. /* Pass in bad args. */
  14595. ExpectIntEQ(wc_CmacFinal(NULL, mac, &macSz), BAD_FUNC_ARG);
  14596. ExpectIntEQ(wc_CmacFinal(&cmac, NULL, &macSz), BAD_FUNC_ARG);
  14597. ExpectIntEQ(wc_CmacFinal(&cmac, mac, &badMacSz), BUFFER_E);
  14598. #endif /* !HAVE_FIPS || FIPS>=5.3 */
  14599. #endif
  14600. return EXPECT_RESULT();
  14601. } /* END test_wc_CmacFinal */
  14602. /*
  14603. * Testing wc_AesCmacGenerate() && wc_AesCmacVerify()
  14604. */
  14605. static int test_wc_AesCmacGenerate(void)
  14606. {
  14607. EXPECT_DECLS;
  14608. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_128)
  14609. byte key[] = {
  14610. 0x26, 0xef, 0x8b, 0x40, 0x34, 0x11, 0x7d, 0x9e,
  14611. 0xbe, 0xc0, 0xc7, 0xfc, 0x31, 0x08, 0x54, 0x69
  14612. };
  14613. byte msg[] = "\x18\x90\x49\xef\xfd\x7c\xf9\xc8"
  14614. "\xf3\x59\x65\xbc\xb0\x97\x8f\xd4";
  14615. byte expMac[] = "\x29\x5f\x2f\x71\xfc\x58\xe6\xf6"
  14616. "\x3d\x32\x65\x4c\x66\x23\xc5";
  14617. byte mac[AES_BLOCK_SIZE];
  14618. word32 keySz = sizeof(key);
  14619. word32 macSz = sizeof(mac);
  14620. word32 msgSz = sizeof(msg) - 1;
  14621. word32 expMacSz = sizeof(expMac) - 1;
  14622. XMEMSET(mac, 0, macSz);
  14623. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, keySz), 0);
  14624. ExpectIntEQ(XMEMCMP(mac, expMac, expMacSz), 0);
  14625. /* Pass in bad args. */
  14626. ExpectIntEQ(wc_AesCmacGenerate(NULL, &macSz, msg, msgSz, key, keySz),
  14627. BAD_FUNC_ARG);
  14628. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, NULL, keySz),
  14629. BAD_FUNC_ARG);
  14630. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, msg, msgSz, key, 0),
  14631. BAD_FUNC_ARG);
  14632. ExpectIntEQ(wc_AesCmacGenerate(mac, &macSz, NULL, msgSz, key, keySz),
  14633. BAD_FUNC_ARG);
  14634. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, keySz), 0);
  14635. /* Test bad args. */
  14636. ExpectIntEQ(wc_AesCmacVerify(NULL, macSz, msg, msgSz, key, keySz),
  14637. BAD_FUNC_ARG);
  14638. ExpectIntEQ(wc_AesCmacVerify(mac, 0, msg, msgSz, key, keySz),
  14639. BAD_FUNC_ARG);
  14640. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, NULL, keySz),
  14641. BAD_FUNC_ARG);
  14642. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, msg, msgSz, key, 0),
  14643. BAD_FUNC_ARG);
  14644. ExpectIntEQ(wc_AesCmacVerify(mac, macSz, NULL, msgSz, key, keySz),
  14645. BAD_FUNC_ARG);
  14646. #endif
  14647. return EXPECT_RESULT();
  14648. } /* END test_wc_AesCmacGenerate */
  14649. /*
  14650. * Testing streaming AES-GCM API.
  14651. */
  14652. static int test_wc_AesGcmStream(void)
  14653. {
  14654. EXPECT_DECLS;
  14655. #if !defined(NO_AES) && defined(WOLFSSL_AES_128) && defined(HAVE_AESGCM) && \
  14656. defined(WOLFSSL_AESGCM_STREAM)
  14657. int i;
  14658. WC_RNG rng[1];
  14659. Aes aesEnc[1];
  14660. Aes aesDec[1];
  14661. byte tag[AES_BLOCK_SIZE];
  14662. byte in[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  14663. byte out[AES_BLOCK_SIZE * 3 + 2];
  14664. byte plain[AES_BLOCK_SIZE * 3 + 2];
  14665. byte aad[AES_BLOCK_SIZE * 3 + 2] = { 0, };
  14666. byte key[AES_128_KEY_SIZE] = { 0, };
  14667. byte iv[AES_IV_SIZE] = { 1, };
  14668. byte ivOut[AES_IV_SIZE];
  14669. static const byte expTagAAD1[AES_BLOCK_SIZE] = {
  14670. 0x6c, 0x35, 0xe6, 0x7f, 0x59, 0x9e, 0xa9, 0x2f,
  14671. 0x27, 0x2d, 0x5f, 0x8e, 0x7e, 0x42, 0xd3, 0x05
  14672. };
  14673. static const byte expTagPlain1[AES_BLOCK_SIZE] = {
  14674. 0x24, 0xba, 0x57, 0x95, 0xd0, 0x27, 0x9e, 0x78,
  14675. 0x3a, 0x88, 0x4c, 0x0a, 0x5d, 0x50, 0x23, 0xd1
  14676. };
  14677. static const byte expTag[AES_BLOCK_SIZE] = {
  14678. 0x22, 0x91, 0x70, 0xad, 0x42, 0xc3, 0xad, 0x96,
  14679. 0xe0, 0x31, 0x57, 0x60, 0xb7, 0x92, 0xa3, 0x6d
  14680. };
  14681. XMEMSET(&rng, 0, sizeof(WC_RNG));
  14682. XMEMSET(&aesEnc, 0, sizeof(Aes));
  14683. XMEMSET(&aesDec, 0, sizeof(Aes));
  14684. /* Create a random for generating IV/nonce. */
  14685. ExpectIntEQ(wc_InitRng(rng), 0);
  14686. /* Initialize data structures. */
  14687. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14688. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14689. /* BadParameters to streaming init. */
  14690. ExpectIntEQ(wc_AesGcmEncryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14691. ExpectIntEQ(wc_AesGcmDecryptInit(NULL, NULL, 0, NULL, 0), BAD_FUNC_ARG);
  14692. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, AES_128_KEY_SIZE, NULL, 0),
  14693. BAD_FUNC_ARG);
  14694. ExpectIntEQ(wc_AesGcmDecryptInit(aesEnc, NULL, 0, NULL, GCM_NONCE_MID_SZ),
  14695. BAD_FUNC_ARG);
  14696. /* Bad parameters to encrypt update. */
  14697. ExpectIntEQ(wc_AesGcmEncryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14698. BAD_FUNC_ARG);
  14699. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 1, NULL, 0),
  14700. BAD_FUNC_ARG);
  14701. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, in, 1, NULL, 0),
  14702. BAD_FUNC_ARG);
  14703. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, NULL, 1, NULL, 0),
  14704. BAD_FUNC_ARG);
  14705. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, NULL, 1),
  14706. BAD_FUNC_ARG);
  14707. /* Bad parameters to decrypt update. */
  14708. ExpectIntEQ(wc_AesGcmDecryptUpdate(NULL, NULL, NULL, 0, NULL, 0),
  14709. BAD_FUNC_ARG);
  14710. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 1, NULL, 0),
  14711. BAD_FUNC_ARG);
  14712. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, in, 1, NULL, 0),
  14713. BAD_FUNC_ARG);
  14714. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, out, NULL, 1, NULL, 0),
  14715. BAD_FUNC_ARG);
  14716. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, NULL, 1),
  14717. BAD_FUNC_ARG);
  14718. /* Bad parameters to encrypt final. */
  14719. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14720. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14721. ExpectIntEQ(wc_AesGcmEncryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14722. BAD_FUNC_ARG);
  14723. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, 0), BAD_FUNC_ARG);
  14724. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, NULL, AES_BLOCK_SIZE),
  14725. BAD_FUNC_ARG);
  14726. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE + 1),
  14727. BAD_FUNC_ARG);
  14728. /* Bad parameters to decrypt final. */
  14729. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, 0), BAD_FUNC_ARG);
  14730. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, tag, 0), BAD_FUNC_ARG);
  14731. ExpectIntEQ(wc_AesGcmDecryptFinal(NULL, NULL, AES_BLOCK_SIZE),
  14732. BAD_FUNC_ARG);
  14733. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, 0), BAD_FUNC_ARG);
  14734. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, NULL, AES_BLOCK_SIZE),
  14735. BAD_FUNC_ARG);
  14736. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE + 1),
  14737. BAD_FUNC_ARG);
  14738. /* Check calling final before setting key fails. */
  14739. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_KEY);
  14740. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_KEY);
  14741. /* Check calling update before setting key else fails. */
  14742. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14743. MISSING_KEY);
  14744. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14745. MISSING_KEY);
  14746. /* Set key but not IV. */
  14747. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), NULL, 0), 0);
  14748. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), NULL, 0), 0);
  14749. /* Check calling final before setting IV fails. */
  14750. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, sizeof(tag)), MISSING_IV);
  14751. ExpectIntEQ(wc_AesGcmEncryptFinal(aesDec, tag, sizeof(tag)), MISSING_IV);
  14752. /* Check calling update before setting IV else fails. */
  14753. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1),
  14754. MISSING_IV);
  14755. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1),
  14756. MISSING_IV);
  14757. /* Set IV using fixed part IV and external IV APIs. */
  14758. ExpectIntEQ(wc_AesGcmSetIV(aesEnc, GCM_NONCE_MID_SZ, iv, AES_IV_FIXED_SZ,
  14759. rng), 0);
  14760. ExpectIntEQ(wc_AesGcmEncryptInit_ex(aesEnc, NULL, 0, ivOut,
  14761. GCM_NONCE_MID_SZ), 0);
  14762. ExpectIntEQ(wc_AesGcmSetExtIV(aesDec, ivOut, GCM_NONCE_MID_SZ), 0);
  14763. ExpectIntEQ(wc_AesGcmInit(aesDec, NULL, 0, NULL, 0), 0);
  14764. /* Encrypt and decrypt data. */
  14765. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, aad, 1), 0);
  14766. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, aad, 1), 0);
  14767. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  14768. /* Finalize and check tag matches. */
  14769. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14770. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14771. /* Set key and IV through streaming init API. */
  14772. wc_AesFree(aesEnc);
  14773. wc_AesFree(aesDec);
  14774. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14775. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14776. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14777. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14778. /* Encrypt/decrypt one block and AAD of one block. */
  14779. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, AES_BLOCK_SIZE, aad,
  14780. AES_BLOCK_SIZE), 0);
  14781. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, AES_BLOCK_SIZE, aad,
  14782. AES_BLOCK_SIZE), 0);
  14783. ExpectIntEQ(XMEMCMP(plain, in, AES_BLOCK_SIZE), 0);
  14784. /* Finalize and check tag matches. */
  14785. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14786. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14787. /* Set key and IV through streaming init API. */
  14788. wc_AesFree(aesEnc);
  14789. wc_AesFree(aesDec);
  14790. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14791. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14792. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14793. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14794. /* No data to encrypt/decrypt one byte of AAD. */
  14795. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad, 1), 0);
  14796. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad, 1), 0);
  14797. /* Finalize and check tag matches. */
  14798. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14799. ExpectIntEQ(XMEMCMP(tag, expTagAAD1, AES_BLOCK_SIZE), 0);
  14800. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14801. /* Set key and IV through streaming init API. */
  14802. wc_AesFree(aesEnc);
  14803. wc_AesFree(aesDec);
  14804. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14805. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14806. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14807. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14808. /* Encrypt/decrypt one byte and no AAD. */
  14809. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out, in, 1, NULL, 0), 0);
  14810. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain, out, 1, NULL, 0), 0);
  14811. ExpectIntEQ(XMEMCMP(plain, in, 1), 0);
  14812. /* Finalize and check tag matches. */
  14813. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14814. ExpectIntEQ(XMEMCMP(tag, expTagPlain1, AES_BLOCK_SIZE), 0);
  14815. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14816. /* Set key and IV through streaming init API. */
  14817. wc_AesFree(aesEnc);
  14818. wc_AesFree(aesDec);
  14819. ExpectIntEQ(wc_AesInit(aesEnc, NULL, INVALID_DEVID), 0);
  14820. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14821. ExpectIntEQ(wc_AesGcmInit(aesEnc, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14822. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14823. /* Encryption AES is one byte at a time */
  14824. for (i = 0; i < (int)sizeof(aad); i++) {
  14825. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, NULL, NULL, 0, aad + i, 1),
  14826. 0);
  14827. }
  14828. for (i = 0; i < (int)sizeof(in); i++) {
  14829. ExpectIntEQ(wc_AesGcmEncryptUpdate(aesEnc, out + i, in + i, 1, NULL, 0),
  14830. 0);
  14831. }
  14832. /* Decryption AES is two bytes at a time */
  14833. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14834. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, NULL, NULL, 0, aad + i, 2),
  14835. 0);
  14836. }
  14837. for (i = 0; i < (int)sizeof(aad); i += 2) {
  14838. ExpectIntEQ(wc_AesGcmDecryptUpdate(aesDec, plain + i, out + i, 2, NULL,
  14839. 0), 0);
  14840. }
  14841. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14842. /* Finalize and check tag matches. */
  14843. ExpectIntEQ(wc_AesGcmEncryptFinal(aesEnc, tag, AES_BLOCK_SIZE), 0);
  14844. ExpectIntEQ(XMEMCMP(tag, expTag, AES_BLOCK_SIZE), 0);
  14845. ExpectIntEQ(wc_AesGcmDecryptFinal(aesDec, tag, AES_BLOCK_SIZE), 0);
  14846. /* Check streaming encryption can be decrypted with one shot. */
  14847. wc_AesFree(aesDec);
  14848. ExpectIntEQ(wc_AesInit(aesDec, NULL, INVALID_DEVID), 0);
  14849. ExpectIntEQ(wc_AesGcmInit(aesDec, key, sizeof(key), iv, AES_IV_SIZE), 0);
  14850. ExpectIntEQ(wc_AesGcmSetKey(aesDec, key, sizeof(key)), 0);
  14851. ExpectIntEQ(wc_AesGcmDecrypt(aesDec, plain, out, sizeof(in), iv,
  14852. AES_IV_SIZE, tag, AES_BLOCK_SIZE, aad, sizeof(aad)), 0);
  14853. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  14854. wc_AesFree(aesEnc);
  14855. wc_AesFree(aesDec);
  14856. wc_FreeRng(rng);
  14857. #endif
  14858. return EXPECT_RESULT();
  14859. } /* END test_wc_AesGcmStream */
  14860. /*
  14861. * Testing streaming SM4 API.
  14862. */
  14863. static int test_wc_Sm4(void)
  14864. {
  14865. int res = TEST_SKIPPED;
  14866. #ifdef WOLFSSL_SM4
  14867. EXPECT_DECLS;
  14868. wc_Sm4 sm4;
  14869. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  14870. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  14871. unsigned char key[SM4_KEY_SIZE];
  14872. #endif
  14873. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  14874. unsigned char iv[SM4_IV_SIZE];
  14875. #endif
  14876. /* Invalid parameters - wc_Sm4Init */
  14877. ExpectIntEQ(wc_Sm4Init(NULL, NULL, INVALID_DEVID), BAD_FUNC_ARG);
  14878. /* Valid cases - wc_Sm4Init */
  14879. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14880. #if defined(WOLFSSL_SM4_ECB) || defined(WOLFSSL_SM4_CBC) || \
  14881. defined(WOLFSSL_SM4_CTR) || defined(WOLFSSL_SM4_CCM)
  14882. XMEMSET(key, 0, sizeof(key));
  14883. /* Invalid parameters - wc_Sm4SetKey. */
  14884. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  14885. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  14886. ExpectIntEQ(wc_Sm4SetKey(NULL, key, 0), BAD_FUNC_ARG);
  14887. ExpectIntEQ(wc_Sm4SetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14888. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, 0), BAD_FUNC_ARG);
  14889. ExpectIntEQ(wc_Sm4SetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14890. ExpectIntEQ(wc_Sm4SetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  14891. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE-1), BAD_FUNC_ARG);
  14892. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE+1), BAD_FUNC_ARG);
  14893. /* Valid cases - wc_Sm4SetKey. */
  14894. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14895. #endif
  14896. #if defined(WOLFSSL_SM4_CBC) || defined(WOLFSSL_SM4_CTR)
  14897. XMEMSET(iv, 0, sizeof(iv));
  14898. /* Invalid parameters - wc_Sm4SetIV. */
  14899. ExpectIntEQ(wc_Sm4SetIV(NULL, NULL), BAD_FUNC_ARG);
  14900. ExpectIntEQ(wc_Sm4SetIV(&sm4, NULL), BAD_FUNC_ARG);
  14901. ExpectIntEQ(wc_Sm4SetIV(NULL, iv), BAD_FUNC_ARG);
  14902. /* Valid cases - wc_Sm4SetIV. */
  14903. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14904. #endif
  14905. /* Valid cases - wc_Sm4Free */
  14906. wc_Sm4Free(NULL);
  14907. wc_Sm4Free(&sm4);
  14908. res = EXPECT_RESULT();
  14909. #endif
  14910. return res;
  14911. } /* END test_wc_Sm4 */
  14912. /*
  14913. * Testing block based SM4-ECB API.
  14914. */
  14915. static int test_wc_Sm4Ecb(void)
  14916. {
  14917. int res = TEST_SKIPPED;
  14918. #ifdef WOLFSSL_SM4_ECB
  14919. EXPECT_DECLS;
  14920. wc_Sm4 sm4;
  14921. unsigned char key[SM4_KEY_SIZE];
  14922. unsigned char in[SM4_BLOCK_SIZE * 2];
  14923. unsigned char out[SM4_BLOCK_SIZE * 2];
  14924. unsigned char out2[SM4_BLOCK_SIZE];
  14925. XMEMSET(key, 0, sizeof(key));
  14926. XMEMSET(in, 0, sizeof(in));
  14927. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14928. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14929. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), MISSING_KEY);
  14930. /* Tested in test_wc_Sm4. */
  14931. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14932. /* Invalid parameters - wc_Sm4EcbEncrypt. */
  14933. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14934. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14935. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14936. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14937. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14938. ExpectIntEQ(wc_Sm4EcbEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14939. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14940. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14941. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14942. /* Valid cases - wc_Sm4EcbEncrypt. */
  14943. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, 0), 0);
  14944. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14945. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14946. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14947. /* In and out are same pointer. */
  14948. ExpectIntEQ(wc_Sm4EcbEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14949. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14950. /* Invalid parameters - wc_Sm4EcbDecrypt. */
  14951. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  14952. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  14953. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  14954. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  14955. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  14956. ExpectIntEQ(wc_Sm4EcbDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  14957. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  14958. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  14959. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  14960. /* Valid cases - wc_Sm4EcbDecrypt. */
  14961. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, 0), 0);
  14962. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  14963. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  14964. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  14965. /* In and out are same pointer. */
  14966. ExpectIntEQ(wc_Sm4EcbDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  14967. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  14968. wc_Sm4Free(&sm4);
  14969. res = EXPECT_RESULT();
  14970. #endif
  14971. return res;
  14972. } /* END test_wc_Sm4Ecb */
  14973. /*
  14974. * Testing block based SM4-CBC API.
  14975. */
  14976. static int test_wc_Sm4Cbc(void)
  14977. {
  14978. int res = TEST_SKIPPED;
  14979. #ifdef WOLFSSL_SM4_CBC
  14980. EXPECT_DECLS;
  14981. wc_Sm4 sm4;
  14982. unsigned char key[SM4_KEY_SIZE];
  14983. unsigned char iv[SM4_IV_SIZE];
  14984. unsigned char in[SM4_BLOCK_SIZE * 2];
  14985. unsigned char out[SM4_BLOCK_SIZE * 2];
  14986. unsigned char out2[SM4_BLOCK_SIZE];
  14987. XMEMSET(key, 0, sizeof(key));
  14988. XMEMSET(iv, 0, sizeof(iv));
  14989. XMEMSET(in, 0, sizeof(in));
  14990. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  14991. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_KEY);
  14992. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_KEY);
  14993. /* Tested in test_wc_Sm4. */
  14994. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  14995. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), MISSING_IV);
  14996. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), MISSING_IV);
  14997. /* Tested in test_wc_Sm4. */
  14998. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  14999. /* Invalid parameters - wc_Sm4CbcEncrypt. */
  15000. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  15001. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  15002. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  15003. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  15004. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  15005. ExpectIntEQ(wc_Sm4CbcEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  15006. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  15007. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  15008. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  15009. /* Valid cases - wc_Sm4CbcEncrypt. */
  15010. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, 0), 0);
  15011. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  15012. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15013. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15014. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  15015. /* In and out are same pointer. */
  15016. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15017. ExpectIntEQ(wc_Sm4CbcEncrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  15018. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  15019. /* Invalid parameters - wc_Sm4CbcDecrypt. */
  15020. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 1), BAD_FUNC_ARG);
  15021. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, NULL, 1), BAD_FUNC_ARG);
  15022. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, NULL, 1), BAD_FUNC_ARG);
  15023. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, in, 1), BAD_FUNC_ARG);
  15024. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  15025. ExpectIntEQ(wc_Sm4CbcDecrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  15026. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  15027. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  15028. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 1), BAD_FUNC_ARG);
  15029. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15030. /* Valid cases - wc_Sm4CbcDecrypt. */
  15031. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, 0), 0);
  15032. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  15033. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15034. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15035. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  15036. /* In and out are same pointer. */
  15037. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15038. ExpectIntEQ(wc_Sm4CbcDecrypt(&sm4, in, in, SM4_BLOCK_SIZE * 2), 0);
  15039. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  15040. wc_Sm4Free(&sm4);
  15041. res = EXPECT_RESULT();
  15042. #endif
  15043. return res;
  15044. } /* END test_wc_Sm4Cbc */
  15045. /*
  15046. * Testing streaming SM4-CTR API.
  15047. */
  15048. static int test_wc_Sm4Ctr(void)
  15049. {
  15050. int res = TEST_SKIPPED;
  15051. #ifdef WOLFSSL_SM4_CTR
  15052. EXPECT_DECLS;
  15053. wc_Sm4 sm4;
  15054. unsigned char key[SM4_KEY_SIZE];
  15055. unsigned char iv[SM4_IV_SIZE];
  15056. unsigned char in[SM4_BLOCK_SIZE * 4];
  15057. unsigned char out[SM4_BLOCK_SIZE * 4];
  15058. unsigned char out2[SM4_BLOCK_SIZE * 4];
  15059. word32 chunk;
  15060. word32 i;
  15061. XMEMSET(key, 0, sizeof(key));
  15062. XMEMSET(iv, 0, sizeof(iv));
  15063. XMEMSET(in, 0, sizeof(in));
  15064. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15065. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_KEY);
  15066. /* Tested in test_wc_Sm4. */
  15067. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15068. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), MISSING_IV);
  15069. /* Tested in test_wc_Sm4. */
  15070. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15071. /* Invalid parameters - wc_Sm4CtrEncrypt. */
  15072. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, NULL, 0), BAD_FUNC_ARG);
  15073. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, NULL, 0), BAD_FUNC_ARG);
  15074. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, NULL, 0), BAD_FUNC_ARG);
  15075. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, NULL, in, 0), BAD_FUNC_ARG);
  15076. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, NULL, 0), BAD_FUNC_ARG);
  15077. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, NULL, in, 0), BAD_FUNC_ARG);
  15078. ExpectIntEQ(wc_Sm4CtrEncrypt(NULL, out, in, 0), BAD_FUNC_ARG);
  15079. /* Valid cases - wc_Sm4CtrEncrypt. */
  15080. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 0), 0);
  15081. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, 1), 0);
  15082. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15083. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, 2), 0);
  15084. ExpectIntEQ(XMEMCMP(out, out2, 1), 0);
  15085. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15086. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, SM4_BLOCK_SIZE), 0);
  15087. ExpectIntEQ(XMEMCMP(out2, out, 2), 0);
  15088. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15089. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15090. ExpectIntEQ(XMEMCMP(out, out2, SM4_BLOCK_SIZE), 0);
  15091. /* In and out are same pointer. Also check encrypt of cipher text produces
  15092. * plaintext.
  15093. */
  15094. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15095. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, out, SM4_BLOCK_SIZE * 2), 0);
  15096. ExpectIntEQ(XMEMCMP(in, out, SM4_BLOCK_SIZE * 2), 0);
  15097. /* Chunking tests. */
  15098. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15099. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, in, (word32)sizeof(in)), 0);
  15100. for (chunk = 1; chunk <= SM4_BLOCK_SIZE + 1; chunk++) {
  15101. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15102. for (i = 0; i + chunk <= (word32)sizeof(in); i += chunk) {
  15103. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i, chunk), 0);
  15104. }
  15105. if (i < (word32)sizeof(in)) {
  15106. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out + i, in + i,
  15107. (word32)sizeof(in) - i), 0);
  15108. }
  15109. ExpectIntEQ(XMEMCMP(out, out2, (word32)sizeof(out)), 0);
  15110. }
  15111. for (i = 0; i < (word32)sizeof(iv); i++) {
  15112. iv[i] = 0xff;
  15113. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15114. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2), 0);
  15115. ExpectIntEQ(wc_Sm4SetIV(&sm4, iv), 0);
  15116. ExpectIntEQ(wc_Sm4CtrEncrypt(&sm4, out2, out, SM4_BLOCK_SIZE * 2), 0);
  15117. ExpectIntEQ(XMEMCMP(out2, in, SM4_BLOCK_SIZE * 2), 0);
  15118. }
  15119. wc_Sm4Free(&sm4);
  15120. res = EXPECT_RESULT();
  15121. #endif
  15122. return res;
  15123. } /* END test_wc_Sm4Ctr */
  15124. /*
  15125. * Testing stream SM4-GCM API.
  15126. */
  15127. static int test_wc_Sm4Gcm(void)
  15128. {
  15129. int res = TEST_SKIPPED;
  15130. #ifdef WOLFSSL_SM4_GCM
  15131. EXPECT_DECLS;
  15132. wc_Sm4 sm4;
  15133. unsigned char key[SM4_KEY_SIZE];
  15134. unsigned char nonce[GCM_NONCE_MAX_SZ];
  15135. unsigned char in[SM4_BLOCK_SIZE * 2];
  15136. unsigned char in2[SM4_BLOCK_SIZE * 2];
  15137. unsigned char out[SM4_BLOCK_SIZE * 2];
  15138. unsigned char out2[SM4_BLOCK_SIZE * 2];
  15139. unsigned char dec[SM4_BLOCK_SIZE * 2];
  15140. unsigned char tag[SM4_BLOCK_SIZE];
  15141. unsigned char aad[SM4_BLOCK_SIZE * 2];
  15142. word32 i;
  15143. XMEMSET(key, 0, sizeof(key));
  15144. XMEMSET(nonce, 0, sizeof(nonce));
  15145. XMEMSET(in, 0, sizeof(in));
  15146. XMEMSET(in2, 0, sizeof(in2));
  15147. XMEMSET(aad, 0, sizeof(aad));
  15148. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15149. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  15150. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15151. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce, GCM_NONCE_MID_SZ, tag,
  15152. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15153. /* Invalid parameters - wc_Sm4GcmSetKey. */
  15154. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, 0), BAD_FUNC_ARG);
  15155. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, 0), BAD_FUNC_ARG);
  15156. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, 0), BAD_FUNC_ARG);
  15157. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15158. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, 0), BAD_FUNC_ARG);
  15159. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, NULL, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15160. ExpectIntEQ(wc_Sm4GcmSetKey(NULL, key, SM4_KEY_SIZE), BAD_FUNC_ARG);
  15161. /* Valid parameters - wc_Sm4GcmSetKey. */
  15162. ExpectIntEQ(wc_Sm4GcmSetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15163. /* Invalid parameters - wc_Sm4GcmEncrypt. */
  15164. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15165. 0), BAD_FUNC_ARG);
  15166. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15167. 0), BAD_FUNC_ARG);
  15168. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15169. 0), BAD_FUNC_ARG);
  15170. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15171. 0), BAD_FUNC_ARG);
  15172. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15173. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15174. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15175. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15176. ExpectIntEQ(wc_Sm4GcmEncrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15177. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15178. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  15179. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15180. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15181. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15182. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  15183. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15184. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  15185. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15186. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  15187. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15188. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15189. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15190. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15191. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15192. /* Invalid parameters - wc_Sm4GcmDecrypt. */
  15193. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15194. 0), BAD_FUNC_ARG);
  15195. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15196. 0), BAD_FUNC_ARG);
  15197. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15198. 0), BAD_FUNC_ARG);
  15199. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15200. 0), BAD_FUNC_ARG);
  15201. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15202. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15203. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15204. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15205. ExpectIntEQ(wc_Sm4GcmDecrypt(NULL, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15206. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15207. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, in, 1, nonce, GCM_NONCE_MID_SZ,
  15208. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15209. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, NULL, 1, nonce, GCM_NONCE_MID_SZ,
  15210. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15211. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, NULL, GCM_NONCE_MID_SZ, tag,
  15212. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15213. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  15214. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15215. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ,
  15216. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15217. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15218. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15219. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 1, nonce, GCM_NONCE_MID_SZ, tag,
  15220. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15221. /* Valid cases - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15222. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15223. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15224. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15225. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15226. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15227. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15228. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, NULL, NULL, 0, nonce, GCM_NONCE_MID_SZ,
  15229. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15230. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15231. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15232. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15233. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15234. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15235. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15236. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15237. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15238. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15239. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15240. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15241. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15242. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15243. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15244. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  15245. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15246. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15247. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  15248. /* Check vald values of nonce - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15249. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15250. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15251. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15252. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15253. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15254. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15255. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15256. GCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15257. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15258. GCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  15259. SM4_GCM_AUTH_E);
  15260. /* Check valid values of tag size - wc_Sm4GcmEncrypt/wc_Sm4GcmDecrypt. */
  15261. for (i = WOLFSSL_MIN_AUTH_TAG_SZ; i < SM4_BLOCK_SIZE; i++) {
  15262. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15263. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  15264. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15265. GCM_NONCE_MID_SZ, tag, i, aad, sizeof(aad)), 0);
  15266. }
  15267. /* Check different in/out sizes. */
  15268. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 0, nonce,
  15269. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15270. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, out, in, 0, nonce,
  15271. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15272. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, 1, nonce,
  15273. GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15274. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  15275. XMEMCPY(out2, out, i - 1);
  15276. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, out, in, i, nonce, GCM_NONCE_MID_SZ,
  15277. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15278. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  15279. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, dec, out, i, nonce, GCM_NONCE_MID_SZ,
  15280. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15281. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  15282. }
  15283. /* Force the counter to roll over in first byte. */
  15284. {
  15285. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  15286. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  15287. ExpectIntEQ(wc_Sm4GcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  15288. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15289. ExpectIntEQ(wc_Sm4GcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  15290. nonce, GCM_NONCE_MID_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15291. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  15292. }
  15293. wc_Sm4Free(&sm4);
  15294. res = EXPECT_RESULT();
  15295. #endif
  15296. return res;
  15297. } /* END test_wc_Sm4Gcm */
  15298. /*
  15299. * Testing stream SM4-CCM API.
  15300. */
  15301. static int test_wc_Sm4Ccm(void)
  15302. {
  15303. int res = TEST_SKIPPED;
  15304. #ifdef WOLFSSL_SM4_CCM
  15305. EXPECT_DECLS;
  15306. wc_Sm4 sm4;
  15307. unsigned char key[SM4_KEY_SIZE];
  15308. unsigned char nonce[CCM_NONCE_MAX_SZ];
  15309. unsigned char in[SM4_BLOCK_SIZE * 2];
  15310. unsigned char in2[SM4_BLOCK_SIZE * 2];
  15311. unsigned char out[SM4_BLOCK_SIZE * 2];
  15312. unsigned char out2[SM4_BLOCK_SIZE * 2];
  15313. unsigned char dec[SM4_BLOCK_SIZE * 2];
  15314. unsigned char tag[SM4_BLOCK_SIZE];
  15315. unsigned char aad[SM4_BLOCK_SIZE * 2];
  15316. word32 i;
  15317. XMEMSET(key, 0, sizeof(key));
  15318. XMEMSET(nonce, 0, sizeof(nonce));
  15319. XMEMSET(in, 0, sizeof(in));
  15320. XMEMSET(in2, 0, sizeof(in2));
  15321. XMEMSET(aad, 0, sizeof(aad));
  15322. ExpectIntEQ(wc_Sm4Init(&sm4, NULL, INVALID_DEVID), 0);
  15323. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  15324. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15325. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce, CCM_NONCE_MAX_SZ, tag,
  15326. SM4_BLOCK_SIZE, aad, sizeof(aad)), MISSING_KEY);
  15327. ExpectIntEQ(wc_Sm4SetKey(&sm4, key, SM4_KEY_SIZE), 0);
  15328. /* Invalid parameters - wc_Sm4CcmEncrypt. */
  15329. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15330. 0), BAD_FUNC_ARG);
  15331. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15332. 0), BAD_FUNC_ARG);
  15333. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15334. 0), BAD_FUNC_ARG);
  15335. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15336. 0), BAD_FUNC_ARG);
  15337. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15338. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15339. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15340. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15341. ExpectIntEQ(wc_Sm4CcmEncrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15342. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15343. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15344. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15345. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15346. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15347. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  15348. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15349. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, 0, tag,
  15350. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15351. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15352. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15353. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15354. WOLFSSL_MIN_AUTH_TAG_SZ-1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15355. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15356. SM4_BLOCK_SIZE+1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15357. /* Invalid parameters - wc_Sm4CcmDecrypt. */
  15358. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15359. 0), BAD_FUNC_ARG);
  15360. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 1, NULL, 0, NULL, 0, NULL,
  15361. 0), BAD_FUNC_ARG);
  15362. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, NULL, 1, NULL, 0, NULL, 0, NULL,
  15363. 0), BAD_FUNC_ARG);
  15364. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, in, 1, NULL, 0, NULL, 0, NULL,
  15365. 0), BAD_FUNC_ARG);
  15366. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15367. NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15368. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, NULL, NULL, 1, NULL, 0, tag,
  15369. SM4_BLOCK_SIZE, NULL, 0), BAD_FUNC_ARG);
  15370. ExpectIntEQ(wc_Sm4CcmDecrypt(NULL, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15371. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15372. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15373. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15374. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, NULL, 1, nonce, CCM_NONCE_MAX_SZ,
  15375. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15376. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, NULL, CCM_NONCE_MAX_SZ, tag,
  15377. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15378. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, 0, tag,
  15379. SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15380. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ,
  15381. NULL, SM4_BLOCK_SIZE, aad, sizeof(aad)), BAD_FUNC_ARG);
  15382. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15383. WOLFSSL_MIN_AUTH_TAG_SZ - 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15384. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 1, nonce, CCM_NONCE_MAX_SZ, tag,
  15385. SM4_BLOCK_SIZE + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15386. /* Valid cases - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15387. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15388. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15389. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15390. tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15391. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15392. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15393. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, NULL, NULL, 0, nonce, CCM_NONCE_MAX_SZ,
  15394. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15395. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15396. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15397. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15398. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15399. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15400. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15401. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15402. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 1), 0);
  15403. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE * 2, nonce,
  15404. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15405. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE * 2, nonce,
  15406. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15407. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15408. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15409. ExpectIntEQ(XMEMCMP(in2, out, SM4_BLOCK_SIZE * 2), 0);
  15410. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in2, in2, SM4_BLOCK_SIZE * 2, nonce,
  15411. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15412. ExpectIntEQ(XMEMCMP(in2, in, SM4_BLOCK_SIZE * 2), 0);
  15413. /* Check vald values of nonce - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15414. for (i = CCM_NONCE_MIN_SZ; i <= CCM_NONCE_MAX_SZ; i++) {
  15415. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15416. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15417. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15418. i, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15419. }
  15420. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15421. CCM_NONCE_MIN_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)),
  15422. SM4_CCM_AUTH_E);
  15423. /* Check invalid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt. */
  15424. for (i = 0; i < 4; i++) {
  15425. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15426. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15427. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15428. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15429. }
  15430. /* Odd values in range 4..SM4_BLOCK_SIZE. */
  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 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15434. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15435. CCM_NONCE_MAX_SZ, tag, i * 2 + 1, aad, sizeof(aad)), BAD_FUNC_ARG);
  15436. }
  15437. /* Check valid values of tag size - wc_Sm4CcmEncrypt/wc_Sm4CcmDecrypt.
  15438. * Even values in range 4..SM4_BLOCK_SIZE.
  15439. */
  15440. for (i = 2; i < SM4_BLOCK_SIZE / 2; i++) {
  15441. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, SM4_BLOCK_SIZE, nonce,
  15442. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  15443. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, in, out, SM4_BLOCK_SIZE, nonce,
  15444. CCM_NONCE_MAX_SZ, tag, i * 2, aad, sizeof(aad)), 0);
  15445. }
  15446. /* Check different in/out sizes. */
  15447. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 0, nonce,
  15448. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15449. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, out, in, 0, nonce,
  15450. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15451. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, 1, nonce,
  15452. CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, NULL, 0), 0);
  15453. for (i = 2; i <= SM4_BLOCK_SIZE * 2; i++) {
  15454. XMEMCPY(out2, out, i - 1);
  15455. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, out, in, i, nonce, CCM_NONCE_MAX_SZ,
  15456. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15457. ExpectIntEQ(XMEMCMP(out, out2, i - 1), 0);
  15458. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, dec, out, i, nonce, CCM_NONCE_MAX_SZ,
  15459. tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15460. ExpectIntEQ(XMEMCMP(in, dec, i), 0);
  15461. }
  15462. /* Force the counter to roll over in first byte. */
  15463. {
  15464. static unsigned char largeIn[256 * SM4_BLOCK_SIZE];
  15465. static unsigned char largeOut[256 * SM4_BLOCK_SIZE];
  15466. ExpectIntEQ(wc_Sm4CcmEncrypt(&sm4, largeOut, largeIn, sizeof(largeIn),
  15467. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15468. ExpectIntEQ(wc_Sm4CcmDecrypt(&sm4, largeOut, largeOut, sizeof(largeIn),
  15469. nonce, CCM_NONCE_MAX_SZ, tag, SM4_BLOCK_SIZE, aad, sizeof(aad)), 0);
  15470. ExpectIntEQ(XMEMCMP(largeOut, largeIn, sizeof(largeIn)), 0);
  15471. }
  15472. wc_Sm4Free(&sm4);
  15473. res = EXPECT_RESULT();
  15474. #endif
  15475. return res;
  15476. } /* END test_wc_Sm4Ccm */
  15477. /*
  15478. * unit test for wc_Des3_SetIV()
  15479. */
  15480. static int test_wc_Des3_SetIV(void)
  15481. {
  15482. EXPECT_DECLS;
  15483. #ifndef NO_DES3
  15484. Des3 des;
  15485. const byte key[] = {
  15486. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15487. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15488. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15489. };
  15490. const byte iv[] = {
  15491. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15492. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15493. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15494. };
  15495. XMEMSET(&des, 0, sizeof(Des3));
  15496. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15497. /* DES_ENCRYPTION or DES_DECRYPTION */
  15498. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15499. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  15500. #ifndef HAVE_FIPS /* no sanity checks with FIPS wrapper */
  15501. /* Test explicitly wc_Des3_SetIV() */
  15502. ExpectIntEQ(wc_Des3_SetIV(NULL, iv), BAD_FUNC_ARG);
  15503. ExpectIntEQ(wc_Des3_SetIV(&des, NULL), 0);
  15504. #endif
  15505. wc_Des3Free(&des);
  15506. #endif
  15507. return EXPECT_RESULT();
  15508. } /* END test_wc_Des3_SetIV */
  15509. /*
  15510. * unit test for wc_Des3_SetKey()
  15511. */
  15512. static int test_wc_Des3_SetKey(void)
  15513. {
  15514. EXPECT_DECLS;
  15515. #ifndef NO_DES3
  15516. Des3 des;
  15517. const byte key[] = {
  15518. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15519. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15520. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15521. };
  15522. const byte iv[] = {
  15523. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15524. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15525. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15526. };
  15527. XMEMSET(&des, 0, sizeof(Des3));
  15528. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15529. /* DES_ENCRYPTION or DES_DECRYPTION */
  15530. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15531. ExpectIntEQ(XMEMCMP(iv, des.reg, DES_BLOCK_SIZE), 0);
  15532. /* Test bad args. */
  15533. ExpectIntEQ(wc_Des3_SetKey(NULL, key, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  15534. ExpectIntEQ(wc_Des3_SetKey(&des, NULL, iv, DES_ENCRYPTION), BAD_FUNC_ARG);
  15535. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, -1), BAD_FUNC_ARG);
  15536. /* Default case. Should return 0. */
  15537. ExpectIntEQ(wc_Des3_SetKey(&des, key, NULL, DES_ENCRYPTION), 0);
  15538. wc_Des3Free(&des);
  15539. #endif
  15540. return EXPECT_RESULT();
  15541. } /* END test_wc_Des3_SetKey */
  15542. /*
  15543. * Test function for wc_Des3_CbcEncrypt and wc_Des3_CbcDecrypt
  15544. */
  15545. static int test_wc_Des3_CbcEncryptDecrypt(void)
  15546. {
  15547. EXPECT_DECLS;
  15548. #ifndef NO_DES3
  15549. Des3 des;
  15550. byte cipher[24];
  15551. byte plain[24];
  15552. const byte key[] = {
  15553. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15554. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15555. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15556. };
  15557. const byte iv[] = {
  15558. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15559. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15560. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15561. };
  15562. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  15563. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15564. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15565. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15566. };
  15567. XMEMSET(&des, 0, sizeof(Des3));
  15568. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15569. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15570. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, vector, 24), 0);
  15571. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_DECRYPTION), 0);
  15572. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, cipher, 24), 0);
  15573. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  15574. /* Pass in bad args. */
  15575. ExpectIntEQ(wc_Des3_CbcEncrypt(NULL, cipher, vector, 24), BAD_FUNC_ARG);
  15576. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, NULL, vector, 24), BAD_FUNC_ARG);
  15577. ExpectIntEQ(wc_Des3_CbcEncrypt(&des, cipher, NULL, sizeof(vector)),
  15578. BAD_FUNC_ARG);
  15579. ExpectIntEQ(wc_Des3_CbcDecrypt(NULL, plain, cipher, 24), BAD_FUNC_ARG);
  15580. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, NULL, cipher, 24), BAD_FUNC_ARG);
  15581. ExpectIntEQ(wc_Des3_CbcDecrypt(&des, plain, NULL, 24), BAD_FUNC_ARG);
  15582. wc_Des3Free(&des);
  15583. #endif
  15584. return EXPECT_RESULT();
  15585. } /* END wc_Des3_CbcEncrypt */
  15586. /*
  15587. * Unit test for wc_Des3_CbcEncryptWithKey and wc_Des3_CbcDecryptWithKey
  15588. */
  15589. static int test_wc_Des3_CbcEncryptDecryptWithKey(void)
  15590. {
  15591. EXPECT_DECLS;
  15592. #ifndef NO_DES3
  15593. word32 vectorSz, cipherSz;
  15594. byte cipher[24];
  15595. byte plain[24];
  15596. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  15597. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15598. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15599. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15600. };
  15601. byte key[] = {
  15602. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15603. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15604. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15605. };
  15606. byte iv[] = {
  15607. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15608. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15609. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15610. };
  15611. vectorSz = sizeof(byte) * 24;
  15612. cipherSz = sizeof(byte) * 24;
  15613. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, iv),
  15614. 0);
  15615. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, iv), 0);
  15616. ExpectIntEQ(XMEMCMP(plain, vector, 24), 0);
  15617. /* pass in bad args. */
  15618. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(NULL, vector, vectorSz, key, iv),
  15619. BAD_FUNC_ARG);
  15620. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, NULL, vectorSz, key, iv),
  15621. BAD_FUNC_ARG);
  15622. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, NULL, iv),
  15623. BAD_FUNC_ARG);
  15624. ExpectIntEQ(wc_Des3_CbcEncryptWithKey(cipher, vector, vectorSz, key, NULL),
  15625. 0);
  15626. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(NULL, cipher, cipherSz, key, iv),
  15627. BAD_FUNC_ARG);
  15628. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, NULL, cipherSz, key, iv),
  15629. BAD_FUNC_ARG);
  15630. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, NULL, iv),
  15631. BAD_FUNC_ARG);
  15632. ExpectIntEQ(wc_Des3_CbcDecryptWithKey(plain, cipher, cipherSz, key, NULL),
  15633. 0);
  15634. #endif
  15635. return EXPECT_RESULT();
  15636. } /* END test_wc_Des3_CbcEncryptDecryptWithKey */
  15637. /*
  15638. * Unit test for wc_Des3_EcbEncrypt
  15639. */
  15640. static int test_wc_Des3_EcbEncrypt(void)
  15641. {
  15642. EXPECT_DECLS;
  15643. #if !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  15644. Des3 des;
  15645. byte cipher[24];
  15646. word32 cipherSz = sizeof(cipher);
  15647. const byte key[] = {
  15648. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  15649. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  15650. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  15651. };
  15652. const byte iv[] = {
  15653. 0x12,0x34,0x56,0x78,0x90,0xab,0xcd,0xef,
  15654. 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
  15655. 0x11,0x21,0x31,0x41,0x51,0x61,0x71,0x81
  15656. };
  15657. const byte vector[] = { /* "Now is the time for all " w/o trailing 0 */
  15658. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  15659. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  15660. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  15661. };
  15662. XMEMSET(&des, 0, sizeof(Des3));
  15663. ExpectIntEQ(wc_Des3Init(&des, NULL, INVALID_DEVID), 0);
  15664. ExpectIntEQ(wc_Des3_SetKey(&des, key, iv, DES_ENCRYPTION), 0);
  15665. /* Bad Cases */
  15666. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, 0, NULL, 0), BAD_FUNC_ARG);
  15667. ExpectIntEQ(wc_Des3_EcbEncrypt(NULL, cipher, vector, cipherSz),
  15668. BAD_FUNC_ARG);
  15669. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, 0, vector, cipherSz), BAD_FUNC_ARG);
  15670. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, NULL, cipherSz), BAD_FUNC_ARG);
  15671. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, 0), 0);
  15672. /* Good Cases */
  15673. ExpectIntEQ(wc_Des3_EcbEncrypt(&des, cipher, vector, cipherSz), 0);
  15674. wc_Des3Free(&des);
  15675. #endif
  15676. return EXPECT_RESULT();
  15677. } /* END test_wc_Des3_EcbEncrypt */
  15678. /*
  15679. * Testing wc_Chacha_SetKey() and wc_Chacha_SetIV()
  15680. */
  15681. static int test_wc_Chacha_SetKey(void)
  15682. {
  15683. EXPECT_DECLS;
  15684. #ifdef HAVE_CHACHA
  15685. ChaCha ctx;
  15686. const byte key[] = {
  15687. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15688. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15689. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15690. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15691. };
  15692. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  15693. byte cipher[128];
  15694. XMEMSET(cipher, 0, sizeof(cipher));
  15695. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, keySz), 0);
  15696. /* Test bad args. */
  15697. ExpectIntEQ(wc_Chacha_SetKey(NULL, key, keySz), BAD_FUNC_ARG);
  15698. ExpectIntEQ(wc_Chacha_SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  15699. ExpectIntEQ(wc_Chacha_SetIV(&ctx, cipher, 0), 0);
  15700. /* Test bad args. */
  15701. ExpectIntEQ(wc_Chacha_SetIV(NULL, cipher, 0), BAD_FUNC_ARG);
  15702. #endif
  15703. return EXPECT_RESULT();
  15704. } /* END test_wc_Chacha_SetKey */
  15705. /*
  15706. * unit test for wc_Poly1305SetKey()
  15707. */
  15708. static int test_wc_Poly1305SetKey(void)
  15709. {
  15710. EXPECT_DECLS;
  15711. #ifdef HAVE_POLY1305
  15712. Poly1305 ctx;
  15713. const byte key[] =
  15714. {
  15715. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15716. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15717. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15718. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15719. };
  15720. word32 keySz = (word32)(sizeof(key)/sizeof(byte));
  15721. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, keySz), 0);
  15722. /* Test bad args. */
  15723. ExpectIntEQ(wc_Poly1305SetKey(NULL, key,keySz), BAD_FUNC_ARG);
  15724. ExpectIntEQ(wc_Poly1305SetKey(&ctx, NULL, keySz), BAD_FUNC_ARG);
  15725. ExpectIntEQ(wc_Poly1305SetKey(&ctx, key, 18), BAD_FUNC_ARG);
  15726. #endif
  15727. return EXPECT_RESULT();
  15728. } /* END test_wc_Poly1305_SetKey() */
  15729. /*
  15730. * Testing wc_Chacha_Process()
  15731. */
  15732. static int test_wc_Chacha_Process(void)
  15733. {
  15734. EXPECT_DECLS;
  15735. #ifdef HAVE_CHACHA
  15736. ChaCha enc, dec;
  15737. byte cipher[128];
  15738. byte plain[128];
  15739. const byte key[] =
  15740. {
  15741. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15742. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15743. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  15744. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01
  15745. };
  15746. const char* input = "Everybody gets Friday off.";
  15747. word32 keySz = sizeof(key)/sizeof(byte);
  15748. unsigned long int inlen = XSTRLEN(input);
  15749. /* Initialize stack variables. */
  15750. XMEMSET(cipher, 0, 128);
  15751. XMEMSET(plain, 0, 128);
  15752. ExpectIntEQ(wc_Chacha_SetKey(&enc, key, keySz), 0);
  15753. ExpectIntEQ(wc_Chacha_SetKey(&dec, key, keySz), 0);
  15754. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  15755. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  15756. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input, (word32)inlen),
  15757. 0);
  15758. ExpectIntEQ(wc_Chacha_Process(&dec, plain, cipher, (word32)inlen), 0);
  15759. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  15760. #if !defined(USE_INTEL_CHACHA_SPEEDUP) && !defined(WOLFSSL_ARMASM)
  15761. /* test checking and using leftovers, currently just in C code */
  15762. ExpectIntEQ(wc_Chacha_SetIV(&enc, cipher, 0), 0);
  15763. ExpectIntEQ(wc_Chacha_SetIV(&dec, cipher, 0), 0);
  15764. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, (byte*)input,
  15765. (word32)inlen - 2), 0);
  15766. ExpectIntEQ(wc_Chacha_Process(&enc, cipher + (inlen - 2),
  15767. (byte*)input + (inlen - 2), 2), 0);
  15768. ExpectIntEQ(wc_Chacha_Process(&dec, plain, (byte*)cipher,
  15769. (word32)inlen - 2), 0);
  15770. ExpectIntEQ(wc_Chacha_Process(&dec, cipher + (inlen - 2),
  15771. (byte*)input + (inlen - 2), 2), 0);
  15772. ExpectIntEQ(XMEMCMP(input, plain, (int)inlen), 0);
  15773. /* check edge cases with counter increment */
  15774. {
  15775. /* expected results collected from wolfSSL 4.3.0 encrypted in one call*/
  15776. const byte expected[] = {
  15777. 0x54,0xB1,0xE2,0xD4,0xA2,0x4D,0x52,0x5F,
  15778. 0x42,0x04,0x89,0x7C,0x6E,0x2D,0xFC,0x2D,
  15779. 0x10,0x25,0xB6,0x92,0x71,0xD5,0xC3,0x20,
  15780. 0xE3,0x0E,0xEC,0xF4,0xD8,0x10,0x70,0x29,
  15781. 0x2D,0x4C,0x2A,0x56,0x21,0xE1,0xC7,0x37,
  15782. 0x0B,0x86,0xF5,0x02,0x8C,0xB8,0xB8,0x38,
  15783. 0x41,0xFD,0xDF,0xD9,0xC3,0xE6,0xC8,0x88,
  15784. 0x06,0x82,0xD4,0x80,0x6A,0x50,0x69,0xD5,
  15785. 0xB9,0xB0,0x2F,0x44,0x36,0x5D,0xDA,0x5E,
  15786. 0xDE,0xF6,0xF5,0xFC,0x44,0xDC,0x07,0x51,
  15787. 0xA7,0x32,0x42,0xDB,0xCC,0xBD,0xE2,0xE5,
  15788. 0x0B,0xB1,0x14,0xFF,0x12,0x80,0x16,0x43,
  15789. 0xE7,0x40,0xD5,0xEA,0xC7,0x3F,0x69,0x07,
  15790. 0x64,0xD4,0x86,0x6C,0xE2,0x1F,0x8F,0x6E,
  15791. 0x35,0x41,0xE7,0xD3,0xB5,0x5D,0xD6,0xD4,
  15792. 0x9F,0x00,0xA9,0xAE,0x3D,0x28,0xA5,0x37,
  15793. 0x80,0x3D,0x11,0x25,0xE2,0xB6,0x99,0xD9,
  15794. 0x9B,0x98,0xE9,0x37,0xB9,0xF8,0xA0,0x04,
  15795. 0xDF,0x13,0x49,0x3F,0x19,0x6A,0x45,0x06,
  15796. 0x21,0xB4,0xC7,0x3B,0x49,0x45,0xB4,0xC8,
  15797. 0x03,0x5B,0x43,0x89,0xBD,0xB3,0x96,0x4B,
  15798. 0x17,0x6F,0x85,0xC6,0xCF,0xA6,0x05,0x35,
  15799. 0x1E,0x25,0x03,0xBB,0x55,0x0A,0xD5,0x54,
  15800. 0x41,0xEA,0xEB,0x50,0x40,0x1B,0x43,0x19,
  15801. 0x59,0x1B,0x0E,0x12,0x3E,0xA2,0x71,0xC3,
  15802. 0x1A,0xA7,0x11,0x50,0x43,0x9D,0x56,0x3B,
  15803. 0x63,0x2F,0x63,0xF1,0x8D,0xAE,0xF3,0x23,
  15804. 0xFA,0x1E,0xD8,0x6A,0xE1,0xB2,0x4B,0xF3,
  15805. 0xB9,0x13,0x7A,0x72,0x2B,0x6D,0xCC,0x41,
  15806. 0x1C,0x69,0x7C,0xCD,0x43,0x6F,0xE4,0xE2,
  15807. 0x38,0x99,0xFB,0xC3,0x38,0x92,0x62,0x35,
  15808. 0xC0,0x1D,0x60,0xE4,0x4B,0xDD,0x0C,0x14
  15809. };
  15810. const byte iv2[] = {
  15811. 0x9D,0xED,0xE7,0x0F,0xEC,0x81,0x51,0xD9,
  15812. 0x77,0x39,0x71,0xA6,0x21,0xDF,0xB8,0x93
  15813. };
  15814. byte input2[256];
  15815. int i;
  15816. for (i = 0; i < 256; i++)
  15817. input2[i] = i;
  15818. ExpectIntEQ(wc_Chacha_SetIV(&enc, iv2, 0), 0);
  15819. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2, 64), 0);
  15820. ExpectIntEQ(XMEMCMP(expected, cipher, 64), 0);
  15821. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 64, 128), 0);
  15822. ExpectIntEQ(XMEMCMP(expected + 64, cipher, 128), 0);
  15823. /* partial */
  15824. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 192, 32), 0);
  15825. ExpectIntEQ(XMEMCMP(expected + 192, cipher, 32), 0);
  15826. ExpectIntEQ(wc_Chacha_Process(&enc, cipher, input2 + 224, 32), 0);
  15827. ExpectIntEQ(XMEMCMP(expected + 224, cipher, 32), 0);
  15828. }
  15829. #endif
  15830. /* Test bad args. */
  15831. ExpectIntEQ(wc_Chacha_Process(NULL, cipher, (byte*)input, (word32)inlen),
  15832. BAD_FUNC_ARG);
  15833. #endif
  15834. return EXPECT_RESULT();
  15835. } /* END test_wc_Chacha_Process */
  15836. /*
  15837. * Testing wc_ChaCha20Poly1305_Encrypt() and wc_ChaCha20Poly1305_Decrypt()
  15838. */
  15839. static int test_wc_ChaCha20Poly1305_aead(void)
  15840. {
  15841. EXPECT_DECLS;
  15842. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  15843. const byte key[] = {
  15844. 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  15845. 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
  15846. 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
  15847. 0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
  15848. };
  15849. const byte plaintext[] = {
  15850. 0x4c, 0x61, 0x64, 0x69, 0x65, 0x73, 0x20, 0x61,
  15851. 0x6e, 0x64, 0x20, 0x47, 0x65, 0x6e, 0x74, 0x6c,
  15852. 0x65, 0x6d, 0x65, 0x6e, 0x20, 0x6f, 0x66, 0x20,
  15853. 0x74, 0x68, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73,
  15854. 0x73, 0x20, 0x6f, 0x66, 0x20, 0x27, 0x39, 0x39,
  15855. 0x3a, 0x20, 0x49, 0x66, 0x20, 0x49, 0x20, 0x63,
  15856. 0x6f, 0x75, 0x6c, 0x64, 0x20, 0x6f, 0x66, 0x66,
  15857. 0x65, 0x72, 0x20, 0x79, 0x6f, 0x75, 0x20, 0x6f,
  15858. 0x6e, 0x6c, 0x79, 0x20, 0x6f, 0x6e, 0x65, 0x20,
  15859. 0x74, 0x69, 0x70, 0x20, 0x66, 0x6f, 0x72, 0x20,
  15860. 0x74, 0x68, 0x65, 0x20, 0x66, 0x75, 0x74, 0x75,
  15861. 0x72, 0x65, 0x2c, 0x20, 0x73, 0x75, 0x6e, 0x73,
  15862. 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x77, 0x6f,
  15863. 0x75, 0x6c, 0x64, 0x20, 0x62, 0x65, 0x20, 0x69,
  15864. 0x74, 0x2e
  15865. };
  15866. const byte iv[] = {
  15867. 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43,
  15868. 0x44, 0x45, 0x46, 0x47
  15869. };
  15870. const byte aad[] = { /* additional data */
  15871. 0x50, 0x51, 0x52, 0x53, 0xc0, 0xc1, 0xc2, 0xc3,
  15872. 0xc4, 0xc5, 0xc6, 0xc7
  15873. };
  15874. const byte cipher[] = { /* expected output from operation */
  15875. 0xd3, 0x1a, 0x8d, 0x34, 0x64, 0x8e, 0x60, 0xdb,
  15876. 0x7b, 0x86, 0xaf, 0xbc, 0x53, 0xef, 0x7e, 0xc2,
  15877. 0xa4, 0xad, 0xed, 0x51, 0x29, 0x6e, 0x08, 0xfe,
  15878. 0xa9, 0xe2, 0xb5, 0xa7, 0x36, 0xee, 0x62, 0xd6,
  15879. 0x3d, 0xbe, 0xa4, 0x5e, 0x8c, 0xa9, 0x67, 0x12,
  15880. 0x82, 0xfa, 0xfb, 0x69, 0xda, 0x92, 0x72, 0x8b,
  15881. 0x1a, 0x71, 0xde, 0x0a, 0x9e, 0x06, 0x0b, 0x29,
  15882. 0x05, 0xd6, 0xa5, 0xb6, 0x7e, 0xcd, 0x3b, 0x36,
  15883. 0x92, 0xdd, 0xbd, 0x7f, 0x2d, 0x77, 0x8b, 0x8c,
  15884. 0x98, 0x03, 0xae, 0xe3, 0x28, 0x09, 0x1b, 0x58,
  15885. 0xfa, 0xb3, 0x24, 0xe4, 0xfa, 0xd6, 0x75, 0x94,
  15886. 0x55, 0x85, 0x80, 0x8b, 0x48, 0x31, 0xd7, 0xbc,
  15887. 0x3f, 0xf4, 0xde, 0xf0, 0x8e, 0x4b, 0x7a, 0x9d,
  15888. 0xe5, 0x76, 0xd2, 0x65, 0x86, 0xce, 0xc6, 0x4b,
  15889. 0x61, 0x16
  15890. };
  15891. const byte authTag[] = { /* expected output from operation */
  15892. 0x1a, 0xe1, 0x0b, 0x59, 0x4f, 0x09, 0xe2, 0x6a,
  15893. 0x7e, 0x90, 0x2e, 0xcb, 0xd0, 0x60, 0x06, 0x91
  15894. };
  15895. byte generatedCiphertext[272];
  15896. byte generatedPlaintext[272];
  15897. byte generatedAuthTag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  15898. /* Initialize stack variables. */
  15899. XMEMSET(generatedCiphertext, 0, 272);
  15900. XMEMSET(generatedPlaintext, 0, 272);
  15901. /* Test Encrypt */
  15902. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15903. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15904. 0);
  15905. ExpectIntEQ(XMEMCMP(generatedCiphertext, cipher,
  15906. sizeof(cipher)/sizeof(byte)), 0);
  15907. /* Test bad args. */
  15908. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(NULL, iv, aad, sizeof(aad),
  15909. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15910. BAD_FUNC_ARG);
  15911. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, NULL, aad, sizeof(aad),
  15912. plaintext, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15913. BAD_FUNC_ARG);
  15914. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad), NULL,
  15915. sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15916. BAD_FUNC_ARG);
  15917. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15918. NULL, sizeof(plaintext), generatedCiphertext, generatedAuthTag),
  15919. BAD_FUNC_ARG);
  15920. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15921. plaintext, sizeof(plaintext), NULL, generatedAuthTag), BAD_FUNC_ARG);
  15922. ExpectIntEQ(wc_ChaCha20Poly1305_Encrypt(key, iv, aad, sizeof(aad),
  15923. plaintext, sizeof(plaintext), generatedCiphertext, NULL), BAD_FUNC_ARG);
  15924. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15925. sizeof(cipher), authTag, generatedPlaintext), 0);
  15926. ExpectIntEQ(XMEMCMP(generatedPlaintext, plaintext,
  15927. sizeof(plaintext)/sizeof(byte)), 0);
  15928. /* Test bad args. */
  15929. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(NULL, iv, aad, sizeof(aad), cipher,
  15930. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15931. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, NULL, aad, sizeof(aad),
  15932. cipher, sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15933. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15934. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15935. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15936. sizeof(cipher), NULL, generatedPlaintext), BAD_FUNC_ARG);
  15937. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), cipher,
  15938. sizeof(cipher), authTag, NULL), BAD_FUNC_ARG);
  15939. ExpectIntEQ(wc_ChaCha20Poly1305_Decrypt(key, iv, aad, sizeof(aad), NULL,
  15940. sizeof(cipher), authTag, generatedPlaintext), BAD_FUNC_ARG);
  15941. #endif
  15942. return EXPECT_RESULT();
  15943. } /* END test_wc_ChaCha20Poly1305_aead */
  15944. /*
  15945. * Testing function for wc_Rc2SetKey().
  15946. */
  15947. static int test_wc_Rc2SetKey(void)
  15948. {
  15949. EXPECT_DECLS;
  15950. #ifdef WC_RC2
  15951. Rc2 rc2;
  15952. byte key40[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  15953. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15954. /* valid key and IV */
  15955. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15956. iv, 40), 0);
  15957. /* valid key, no IV */
  15958. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32) sizeof(key40) / sizeof(byte),
  15959. NULL, 40), 0);
  15960. /* bad arguments */
  15961. /* null Rc2 struct */
  15962. ExpectIntEQ(wc_Rc2SetKey(NULL, key40, (word32) sizeof(key40) / sizeof(byte),
  15963. iv, 40), BAD_FUNC_ARG);
  15964. /* null key */
  15965. ExpectIntEQ(wc_Rc2SetKey(&rc2, NULL, (word32) sizeof(key40) / sizeof(byte),
  15966. iv, 40), BAD_FUNC_ARG);
  15967. /* key size == 0 */
  15968. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 0, iv, 40), WC_KEY_SIZE_E);
  15969. /* key size > 128 */
  15970. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, 129, iv, 40), WC_KEY_SIZE_E);
  15971. /* effective bits == 0 */
  15972. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15973. iv, 0), WC_KEY_SIZE_E);
  15974. /* effective bits > 1024 */
  15975. ExpectIntEQ(wc_Rc2SetKey(&rc2, key40, (word32)sizeof(key40) / sizeof(byte),
  15976. iv, 1025), WC_KEY_SIZE_E);
  15977. #endif
  15978. return EXPECT_RESULT();
  15979. } /* END test_wc_Rc2SetKey */
  15980. /*
  15981. * Testing function for wc_Rc2SetIV().
  15982. */
  15983. static int test_wc_Rc2SetIV(void)
  15984. {
  15985. EXPECT_DECLS;
  15986. #ifdef WC_RC2
  15987. Rc2 rc2;
  15988. byte iv[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  15989. /* valid IV */
  15990. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  15991. /* valid NULL IV */
  15992. ExpectIntEQ(wc_Rc2SetIV(&rc2, NULL), 0);
  15993. /* bad arguments */
  15994. ExpectIntEQ(wc_Rc2SetIV(NULL, iv), BAD_FUNC_ARG);
  15995. ExpectIntEQ(wc_Rc2SetIV(NULL, NULL), BAD_FUNC_ARG);
  15996. #endif
  15997. return EXPECT_RESULT();
  15998. } /* END test_wc_Rc2SetIV */
  15999. /*
  16000. * Testing function for wc_Rc2EcbEncrypt() and wc_Rc2EcbDecrypt().
  16001. */
  16002. static int test_wc_Rc2EcbEncryptDecrypt(void)
  16003. {
  16004. EXPECT_DECLS;
  16005. #ifdef WC_RC2
  16006. Rc2 rc2;
  16007. int effectiveKeyBits = 63;
  16008. byte cipher[RC2_BLOCK_SIZE];
  16009. byte plain[RC2_BLOCK_SIZE];
  16010. byte key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  16011. byte input[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  16012. byte output[] = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  16013. XMEMSET(cipher, 0, sizeof(cipher));
  16014. XMEMSET(plain, 0, sizeof(plain));
  16015. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  16016. NULL, effectiveKeyBits), 0);
  16017. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, RC2_BLOCK_SIZE), 0);
  16018. ExpectIntEQ(XMEMCMP(cipher, output, RC2_BLOCK_SIZE), 0);
  16019. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, cipher, RC2_BLOCK_SIZE), 0);
  16020. ExpectIntEQ(XMEMCMP(plain, input, RC2_BLOCK_SIZE), 0);
  16021. /* Rc2EcbEncrypt bad arguments */
  16022. /* null Rc2 struct */
  16023. ExpectIntEQ(wc_Rc2EcbEncrypt(NULL, cipher, input, RC2_BLOCK_SIZE),
  16024. BAD_FUNC_ARG);
  16025. /* null out buffer */
  16026. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, NULL, input, RC2_BLOCK_SIZE),
  16027. BAD_FUNC_ARG);
  16028. /* null input buffer */
  16029. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, NULL, RC2_BLOCK_SIZE),
  16030. BAD_FUNC_ARG);
  16031. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  16032. ExpectIntEQ(wc_Rc2EcbEncrypt(&rc2, cipher, input, 7), BUFFER_E);
  16033. /* Rc2EcbDecrypt bad arguments */
  16034. /* null Rc2 struct */
  16035. ExpectIntEQ(wc_Rc2EcbDecrypt(NULL, plain, output, RC2_BLOCK_SIZE),
  16036. BAD_FUNC_ARG);
  16037. /* null out buffer */
  16038. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, NULL, output, RC2_BLOCK_SIZE),
  16039. BAD_FUNC_ARG);
  16040. /* null input buffer */
  16041. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, NULL, RC2_BLOCK_SIZE),
  16042. BAD_FUNC_ARG);
  16043. /* output buffer sz != RC2_BLOCK_SIZE (8) */
  16044. ExpectIntEQ(wc_Rc2EcbDecrypt(&rc2, plain, output, 7), BUFFER_E);
  16045. #endif
  16046. return EXPECT_RESULT();
  16047. } /* END test_wc_Rc2EcbEncryptDecrypt */
  16048. /*
  16049. * Testing function for wc_Rc2CbcEncrypt() and wc_Rc2CbcDecrypt().
  16050. */
  16051. static int test_wc_Rc2CbcEncryptDecrypt(void)
  16052. {
  16053. EXPECT_DECLS;
  16054. #ifdef WC_RC2
  16055. Rc2 rc2;
  16056. int effectiveKeyBits = 63;
  16057. byte cipher[RC2_BLOCK_SIZE*2];
  16058. byte plain[RC2_BLOCK_SIZE*2];
  16059. /* vector taken from test.c */
  16060. byte key[] = {
  16061. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16062. };
  16063. byte iv[] = {
  16064. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16065. };
  16066. byte input[] = {
  16067. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  16068. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  16069. };
  16070. byte output[] = {
  16071. 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff,
  16072. 0xf0, 0x51, 0x77, 0x8b, 0x65, 0xdb, 0x13, 0x57
  16073. };
  16074. XMEMSET(cipher, 0, sizeof(cipher));
  16075. XMEMSET(plain, 0, sizeof(plain));
  16076. ExpectIntEQ(wc_Rc2SetKey(&rc2, key, (word32) sizeof(key) / sizeof(byte),
  16077. iv, effectiveKeyBits), 0);
  16078. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, input, sizeof(input)), 0);
  16079. ExpectIntEQ(XMEMCMP(cipher, output, sizeof(output)), 0);
  16080. /* reset IV for decrypt */
  16081. ExpectIntEQ(wc_Rc2SetIV(&rc2, iv), 0);
  16082. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, cipher, sizeof(cipher)), 0);
  16083. ExpectIntEQ(XMEMCMP(plain, input, sizeof(input)), 0);
  16084. /* Rc2CbcEncrypt bad arguments */
  16085. /* null Rc2 struct */
  16086. ExpectIntEQ(wc_Rc2CbcEncrypt(NULL, cipher, input, sizeof(input)),
  16087. BAD_FUNC_ARG);
  16088. /* null out buffer */
  16089. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, NULL, input, sizeof(input)),
  16090. BAD_FUNC_ARG);
  16091. /* null input buffer */
  16092. ExpectIntEQ(wc_Rc2CbcEncrypt(&rc2, cipher, NULL, sizeof(input)),
  16093. BAD_FUNC_ARG);
  16094. /* Rc2CbcDecrypt bad arguments */
  16095. /* in size is 0 */
  16096. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, output, 0), 0);
  16097. /* null Rc2 struct */
  16098. ExpectIntEQ(wc_Rc2CbcDecrypt(NULL, plain, output, sizeof(output)),
  16099. BAD_FUNC_ARG);
  16100. /* null out buffer */
  16101. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, NULL, output, sizeof(output)),
  16102. BAD_FUNC_ARG);
  16103. /* null input buffer */
  16104. ExpectIntEQ(wc_Rc2CbcDecrypt(&rc2, plain, NULL, sizeof(output)),
  16105. BAD_FUNC_ARG);
  16106. #endif
  16107. return EXPECT_RESULT();
  16108. } /* END test_wc_Rc2CbcEncryptDecrypt */
  16109. /*
  16110. * Testing function for wc_AesSetIV
  16111. */
  16112. static int test_wc_AesSetIV(void)
  16113. {
  16114. int res = TEST_SKIPPED;
  16115. #if !defined(NO_AES) && defined(WOLFSSL_AES_128)
  16116. Aes aes;
  16117. int ret = 0;
  16118. byte key16[] =
  16119. {
  16120. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16121. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16122. };
  16123. byte iv1[] = "1234567890abcdef";
  16124. byte iv2[] = "0987654321fedcba";
  16125. ret = wc_AesInit(&aes, NULL, INVALID_DEVID);
  16126. if (ret != 0)
  16127. return ret;
  16128. ret = wc_AesSetKey(&aes, key16, (word32) sizeof(key16) / sizeof(byte),
  16129. iv1, AES_ENCRYPTION);
  16130. if (ret == 0) {
  16131. ret = wc_AesSetIV(&aes, iv2);
  16132. }
  16133. /* Test bad args. */
  16134. if (ret == 0) {
  16135. ret = wc_AesSetIV(NULL, iv1);
  16136. if (ret == BAD_FUNC_ARG) {
  16137. /* NULL iv should return 0. */
  16138. ret = wc_AesSetIV(&aes, NULL);
  16139. }
  16140. else {
  16141. ret = WOLFSSL_FATAL_ERROR;
  16142. }
  16143. }
  16144. wc_AesFree(&aes);
  16145. res = TEST_RES_CHECK(ret == 0);
  16146. #endif
  16147. return res;
  16148. } /* test_wc_AesSetIV */
  16149. /*
  16150. * Testing function for wc_AesSetKey().
  16151. */
  16152. static int test_wc_AesSetKey(void)
  16153. {
  16154. EXPECT_DECLS;
  16155. #ifndef NO_AES
  16156. Aes aes;
  16157. byte key16[] = {
  16158. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16159. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16160. };
  16161. #ifdef WOLFSSL_AES_192
  16162. byte key24[] = {
  16163. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16164. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16165. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16166. };
  16167. #endif
  16168. #ifdef WOLFSSL_AES_256
  16169. byte key32[] = {
  16170. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16171. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16172. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16173. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16174. };
  16175. #endif
  16176. byte badKey16[] = {
  16177. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16178. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16179. };
  16180. byte iv[] = "1234567890abcdef";
  16181. XMEMSET(&aes, 0, sizeof(Aes));
  16182. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16183. #ifdef WOLFSSL_AES_128
  16184. ExpectIntEQ(wc_AesSetKey(&aes, key16, (word32)sizeof(key16) / sizeof(byte),
  16185. iv, AES_ENCRYPTION), 0);
  16186. #endif
  16187. #ifdef WOLFSSL_AES_192
  16188. ExpectIntEQ(wc_AesSetKey(&aes, key24, (word32)sizeof(key24) / sizeof(byte),
  16189. iv, AES_ENCRYPTION), 0);
  16190. #endif
  16191. #ifdef WOLFSSL_AES_256
  16192. ExpectIntEQ(wc_AesSetKey(&aes, key32, (word32)sizeof(key32) / sizeof(byte),
  16193. iv, AES_ENCRYPTION), 0);
  16194. #endif
  16195. /* Pass in bad args. */
  16196. ExpectIntEQ(wc_AesSetKey(NULL, key16, (word32)sizeof(key16) / sizeof(byte),
  16197. iv, AES_ENCRYPTION), BAD_FUNC_ARG);
  16198. ExpectIntEQ(wc_AesSetKey(&aes, badKey16,
  16199. (word32)sizeof(badKey16) / sizeof(byte), iv, AES_ENCRYPTION),
  16200. BAD_FUNC_ARG);
  16201. wc_AesFree(&aes);
  16202. #endif
  16203. return EXPECT_RESULT();
  16204. } /* END test_wc_AesSetKey */
  16205. /*
  16206. * test function for wc_AesCbcEncrypt(), wc_AesCbcDecrypt(),
  16207. * and wc_AesCbcDecryptWithKey()
  16208. */
  16209. static int test_wc_AesCbcEncryptDecrypt(void)
  16210. {
  16211. EXPECT_DECLS;
  16212. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(HAVE_AES_DECRYPT)&& \
  16213. defined(WOLFSSL_AES_256)
  16214. Aes aes;
  16215. byte key32[] = {
  16216. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16217. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16218. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16219. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16220. };
  16221. byte vector[] = { /* Now is the time for all good men w/o trailing 0 */
  16222. 0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74,
  16223. 0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20,
  16224. 0x66, 0x6f, 0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20,
  16225. 0x67, 0x6f, 0x6f, 0x64, 0x20, 0x6d, 0x65, 0x6e
  16226. };
  16227. byte iv[] = "1234567890abcdef";
  16228. byte enc[sizeof(vector)];
  16229. byte dec[sizeof(vector)];
  16230. byte dec2[sizeof(vector)];
  16231. /* Init stack variables. */
  16232. XMEMSET(&aes, 0, sizeof(Aes));
  16233. XMEMSET(enc, 0, sizeof(enc));
  16234. XMEMSET(dec, 0, sizeof(vector));
  16235. XMEMSET(dec2, 0, sizeof(vector));
  16236. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16237. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  16238. AES_ENCRYPTION), 0);
  16239. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector)), 0);
  16240. /* Re init for decrypt and set flag. */
  16241. ExpectIntEQ(wc_AesSetKey(&aes, key32, AES_BLOCK_SIZE * 2, iv,
  16242. AES_DECRYPTION), 0);
  16243. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, sizeof(vector)), 0);
  16244. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16245. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE, key32,
  16246. sizeof(key32)/sizeof(byte), iv), 0);
  16247. ExpectIntEQ(XMEMCMP(vector, dec2, AES_BLOCK_SIZE), 0);
  16248. /* Pass in bad args */
  16249. ExpectIntEQ(wc_AesCbcEncrypt(NULL, enc, vector, sizeof(vector)),
  16250. BAD_FUNC_ARG);
  16251. ExpectIntEQ(wc_AesCbcEncrypt(&aes, NULL, vector, sizeof(vector)),
  16252. BAD_FUNC_ARG);
  16253. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, NULL, sizeof(vector)),
  16254. BAD_FUNC_ARG);
  16255. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  16256. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, sizeof(vector) - 1),
  16257. BAD_LENGTH_E);
  16258. #endif
  16259. #if defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16260. (HAVE_FIPS_VERSION == 2) && defined(WOLFSSL_AESNI)
  16261. fprintf(stderr, "Zero length inputs not supported with AESNI in FIPS "
  16262. "mode (v2), skip test");
  16263. #else
  16264. /* Test passing in size of 0 */
  16265. XMEMSET(enc, 0, sizeof(enc));
  16266. ExpectIntEQ(wc_AesCbcEncrypt(&aes, enc, vector, 0), 0);
  16267. /* Check enc was not modified */
  16268. {
  16269. int i;
  16270. for (i = 0; i < (int)sizeof(enc); i++)
  16271. ExpectIntEQ(enc[i], 0);
  16272. }
  16273. #endif
  16274. ExpectIntEQ(wc_AesCbcDecrypt(NULL, dec, enc, AES_BLOCK_SIZE), BAD_FUNC_ARG);
  16275. ExpectIntEQ(wc_AesCbcDecrypt(&aes, NULL, enc, AES_BLOCK_SIZE),
  16276. BAD_FUNC_ARG);
  16277. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, NULL, AES_BLOCK_SIZE),
  16278. BAD_FUNC_ARG);
  16279. #ifdef WOLFSSL_AES_CBC_LENGTH_CHECKS
  16280. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  16281. BAD_LENGTH_E);
  16282. #else
  16283. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, AES_BLOCK_SIZE * 2 - 1),
  16284. BAD_FUNC_ARG);
  16285. #endif
  16286. /* Test passing in size of 0 */
  16287. XMEMSET(dec, 0, sizeof(dec));
  16288. ExpectIntEQ(wc_AesCbcDecrypt(&aes, dec, enc, 0), 0);
  16289. /* Check dec was not modified */
  16290. {
  16291. int i;
  16292. for (i = 0; i < (int)sizeof(dec); i++)
  16293. ExpectIntEQ(dec[i], 0);
  16294. }
  16295. ExpectIntEQ(wc_AesCbcDecryptWithKey(NULL, enc, AES_BLOCK_SIZE,
  16296. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16297. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, NULL, AES_BLOCK_SIZE,
  16298. key32, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16299. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  16300. NULL, sizeof(key32)/sizeof(byte), iv), BAD_FUNC_ARG);
  16301. ExpectIntEQ(wc_AesCbcDecryptWithKey(dec2, enc, AES_BLOCK_SIZE,
  16302. key32, sizeof(key32)/sizeof(byte), NULL), BAD_FUNC_ARG);
  16303. wc_AesFree(&aes);
  16304. #endif
  16305. return EXPECT_RESULT();
  16306. } /* END test_wc_AesCbcEncryptDecrypt */
  16307. /*
  16308. * Testing wc_AesCtrEncrypt and wc_AesCtrDecrypt
  16309. */
  16310. static int test_wc_AesCtrEncryptDecrypt(void)
  16311. {
  16312. EXPECT_DECLS;
  16313. #if !defined(NO_AES) && defined(WOLFSSL_AES_COUNTER) && defined(WOLFSSL_AES_256)
  16314. Aes aesEnc;
  16315. Aes aesDec;
  16316. byte key32[] = {
  16317. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16318. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16319. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16320. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16321. };
  16322. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  16323. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16324. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16325. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16326. };
  16327. byte iv[] = "1234567890abcdef";
  16328. byte enc[AES_BLOCK_SIZE * 2];
  16329. byte dec[AES_BLOCK_SIZE * 2];
  16330. /* Init stack variables. */
  16331. XMEMSET(&aesEnc, 0, sizeof(Aes));
  16332. XMEMSET(&aesDec, 0, sizeof(Aes));
  16333. XMEMSET(enc, 0, AES_BLOCK_SIZE * 2);
  16334. XMEMSET(dec, 0, AES_BLOCK_SIZE * 2);
  16335. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  16336. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  16337. ExpectIntEQ(wc_AesSetKey(&aesEnc, key32, AES_BLOCK_SIZE * 2, iv,
  16338. AES_ENCRYPTION), 0);
  16339. ExpectIntEQ(wc_AesCtrEncrypt(&aesEnc, enc, vector,
  16340. sizeof(vector)/sizeof(byte)), 0);
  16341. /* Decrypt with wc_AesCtrEncrypt() */
  16342. ExpectIntEQ(wc_AesSetKey(&aesDec, key32, AES_BLOCK_SIZE * 2, iv,
  16343. AES_ENCRYPTION), 0);
  16344. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, enc, sizeof(enc)/sizeof(byte)),
  16345. 0);
  16346. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16347. /* Test bad args. */
  16348. ExpectIntEQ(wc_AesCtrEncrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte)),
  16349. BAD_FUNC_ARG);
  16350. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, NULL, enc, sizeof(enc)/sizeof(byte)),
  16351. BAD_FUNC_ARG);
  16352. ExpectIntEQ(wc_AesCtrEncrypt(&aesDec, dec, NULL, sizeof(enc)/sizeof(byte)),
  16353. BAD_FUNC_ARG);
  16354. wc_AesFree(&aesEnc);
  16355. wc_AesFree(&aesDec);
  16356. #endif
  16357. return EXPECT_RESULT();
  16358. } /* END test_wc_AesCtrEncryptDecrypt */
  16359. /*
  16360. * test function for wc_AesGcmSetKey()
  16361. */
  16362. static int test_wc_AesGcmSetKey(void)
  16363. {
  16364. EXPECT_DECLS;
  16365. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16366. Aes aes;
  16367. #ifdef WOLFSSL_AES_128
  16368. byte key16[] = {
  16369. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16370. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16371. };
  16372. #endif
  16373. #ifdef WOLFSSL_AES_192
  16374. byte key24[] = {
  16375. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16376. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16377. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16378. };
  16379. #endif
  16380. #ifdef WOLFSSL_AES_256
  16381. byte key32[] = {
  16382. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16383. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16384. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16385. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16386. };
  16387. #endif
  16388. byte badKey16[] = {
  16389. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16390. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16391. };
  16392. byte badKey24[] = {
  16393. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16394. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16395. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36
  16396. };
  16397. byte badKey32[] = {
  16398. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x37, 0x37,
  16399. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16400. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16401. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65
  16402. };
  16403. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16404. #ifdef WOLFSSL_AES_128
  16405. ExpectIntEQ(wc_AesGcmSetKey(&aes, key16, sizeof(key16)/sizeof(byte)), 0);
  16406. #endif
  16407. #ifdef WOLFSSL_AES_192
  16408. ExpectIntEQ(wc_AesGcmSetKey(&aes, key24, sizeof(key24)/sizeof(byte)), 0);
  16409. #endif
  16410. #ifdef WOLFSSL_AES_256
  16411. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  16412. #endif
  16413. /* Pass in bad args. */
  16414. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey16, sizeof(badKey16)/sizeof(byte)),
  16415. BAD_FUNC_ARG);
  16416. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey24, sizeof(badKey24)/sizeof(byte)),
  16417. BAD_FUNC_ARG);
  16418. ExpectIntEQ(wc_AesGcmSetKey(&aes, badKey32, sizeof(badKey32)/sizeof(byte)),
  16419. BAD_FUNC_ARG);
  16420. wc_AesFree(&aes);
  16421. #endif
  16422. return EXPECT_RESULT();
  16423. } /* END test_wc_AesGcmSetKey */
  16424. /*
  16425. * test function for wc_AesGcmEncrypt and wc_AesGcmDecrypt
  16426. */
  16427. static int test_wc_AesGcmEncryptDecrypt(void)
  16428. {
  16429. EXPECT_DECLS;
  16430. /* WOLFSSL_AFALG requires 12 byte IV */
  16431. #if !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AES_256) && \
  16432. !defined(WOLFSSL_AFALG) && !defined(WOLFSSL_DEVCRYPTO_AES)
  16433. Aes aes;
  16434. byte key32[] = {
  16435. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16436. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16437. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16438. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16439. };
  16440. byte vector[] = { /* Now is the time for all w/o trailing 0 */
  16441. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16442. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16443. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16444. };
  16445. const byte a[] = {
  16446. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16447. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16448. 0xab, 0xad, 0xda, 0xd2
  16449. };
  16450. byte iv[] = "1234567890a";
  16451. byte longIV[] = "1234567890abcdefghij";
  16452. byte enc[sizeof(vector)];
  16453. byte resultT[AES_BLOCK_SIZE];
  16454. byte dec[sizeof(vector)];
  16455. /* Init stack variables. */
  16456. XMEMSET(&aes, 0, sizeof(Aes));
  16457. XMEMSET(enc, 0, sizeof(vector));
  16458. XMEMSET(dec, 0, sizeof(vector));
  16459. XMEMSET(resultT, 0, AES_BLOCK_SIZE);
  16460. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  16461. ExpectIntEQ(wc_AesGcmSetKey(&aes, key32, sizeof(key32)/sizeof(byte)), 0);
  16462. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16463. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  16464. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(vector), iv,
  16465. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)), 0);
  16466. ExpectIntEQ(XMEMCMP(vector, dec, sizeof(vector)), 0);
  16467. /* Test bad args for wc_AesGcmEncrypt and wc_AesGcmDecrypt */
  16468. ExpectIntEQ(wc_AesGcmEncrypt(NULL, enc, vector, sizeof(vector), iv,
  16469. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16470. BAD_FUNC_ARG);
  16471. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16472. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16473. BAD_FUNC_ARG);
  16474. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv,
  16475. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) - 5, a, sizeof(a)),
  16476. BAD_FUNC_ARG);
  16477. #if (defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16478. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST) || \
  16479. defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16480. /* FIPS does not check the lower bound of ivSz */
  16481. #else
  16482. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), iv, 0,
  16483. resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  16484. #endif
  16485. /* This case is now considered good. Long IVs are now allowed.
  16486. * Except for the original FIPS release, it still has an upper
  16487. * bound on the IV length. */
  16488. #if (!defined(HAVE_FIPS) || \
  16489. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16490. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16491. ExpectIntEQ(wc_AesGcmEncrypt(&aes, enc, vector, sizeof(vector), longIV,
  16492. sizeof(longIV)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16493. 0);
  16494. #else
  16495. (void)longIV;
  16496. #endif /* Old FIPS */
  16497. /* END wc_AesGcmEncrypt */
  16498. #ifdef HAVE_AES_DECRYPT
  16499. ExpectIntEQ(wc_AesGcmDecrypt(NULL, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16500. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16501. BAD_FUNC_ARG);
  16502. ExpectIntEQ(wc_AesGcmDecrypt(&aes, NULL, enc, sizeof(enc)/sizeof(byte), iv,
  16503. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16504. BAD_FUNC_ARG);
  16505. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, NULL, sizeof(enc)/sizeof(byte), iv,
  16506. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16507. BAD_FUNC_ARG);
  16508. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), NULL,
  16509. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT), a, sizeof(a)),
  16510. BAD_FUNC_ARG);
  16511. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16512. sizeof(iv)/sizeof(byte), NULL, sizeof(resultT), a, sizeof(a)),
  16513. BAD_FUNC_ARG);
  16514. #if (defined(HAVE_FIPS) && FIPS_VERSION_LE(2,0) && defined(WOLFSSL_ARMASM))
  16515. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16516. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16517. AES_GCM_AUTH_E);
  16518. #else
  16519. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte), iv,
  16520. sizeof(iv)/sizeof(byte), resultT, sizeof(resultT) + 1, a, sizeof(a)),
  16521. BAD_FUNC_ARG);
  16522. #endif
  16523. #if ((defined(HAVE_FIPS) && defined(HAVE_FIPS_VERSION) && \
  16524. (HAVE_FIPS_VERSION == 2)) || defined(HAVE_SELFTEST)) && \
  16525. !defined(WOLFSSL_AES_GCM_FIXED_IV_AAD)
  16526. /* FIPS does not check the lower bound of ivSz */
  16527. #else
  16528. ExpectIntEQ(wc_AesGcmDecrypt(&aes, dec, enc, sizeof(enc)/sizeof(byte),
  16529. iv, 0, resultT, sizeof(resultT), a, sizeof(a)), BAD_FUNC_ARG);
  16530. #endif
  16531. #endif /* HAVE_AES_DECRYPT */
  16532. wc_AesFree(&aes);
  16533. #endif
  16534. return EXPECT_RESULT();
  16535. } /* END test_wc_AesGcmEncryptDecrypt */
  16536. /*
  16537. * test function for mixed (one-shot encrpytion + stream decryption) AES GCM
  16538. * using a long IV (older FIPS does NOT support long IVs). Relates to zd15423
  16539. */
  16540. static int test_wc_AesGcmMixedEncDecLongIV(void)
  16541. {
  16542. EXPECT_DECLS;
  16543. #if (!defined(HAVE_FIPS) || \
  16544. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))) && \
  16545. !defined(NO_AES) && defined(HAVE_AESGCM) && defined(WOLFSSL_AESGCM_STREAM)
  16546. const byte key[] = {
  16547. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16548. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16549. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16550. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16551. };
  16552. const byte in[] = {
  16553. 0x4e,0x6f,0x77,0x20,0x69,0x73,0x20,0x74,
  16554. 0x68,0x65,0x20,0x74,0x69,0x6d,0x65,0x20,
  16555. 0x66,0x6f,0x72,0x20,0x61,0x6c,0x6c,0x20
  16556. };
  16557. const byte aad[] = {
  16558. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16559. 0xfe, 0xed, 0xfa, 0xce, 0xde, 0xad, 0xbe, 0xef,
  16560. 0xab, 0xad, 0xda, 0xd2
  16561. };
  16562. Aes aesEnc;
  16563. Aes aesDec;
  16564. byte iv[] = "1234567890abcdefghij";
  16565. byte out[sizeof(in)];
  16566. byte plain[sizeof(in)];
  16567. byte tag[AES_BLOCK_SIZE];
  16568. XMEMSET(&aesEnc, 0, sizeof(Aes));
  16569. XMEMSET(&aesDec, 0, sizeof(Aes));
  16570. XMEMSET(out, 0, sizeof(out));
  16571. XMEMSET(plain, 0, sizeof(plain));
  16572. XMEMSET(tag, 0, sizeof(tag));
  16573. /* Perform one-shot encryption using long IV */
  16574. ExpectIntEQ(wc_AesInit(&aesEnc, NULL, INVALID_DEVID), 0);
  16575. ExpectIntEQ(wc_AesGcmSetKey(&aesEnc, key, sizeof(key)), 0);
  16576. ExpectIntEQ(wc_AesGcmEncrypt(&aesEnc, out, in, sizeof(in), iv, sizeof(iv),
  16577. tag, sizeof(tag), aad, sizeof(aad)), 0);
  16578. /* Perform streaming decryption using long IV */
  16579. ExpectIntEQ(wc_AesInit(&aesDec, NULL, INVALID_DEVID), 0);
  16580. ExpectIntEQ(wc_AesGcmInit(&aesDec, key, sizeof(key), iv, sizeof(iv)), 0);
  16581. ExpectIntEQ(wc_AesGcmDecryptUpdate(&aesDec, plain, out, sizeof(out), aad,
  16582. sizeof(aad)), 0);
  16583. ExpectIntEQ(wc_AesGcmDecryptFinal(&aesDec, tag, sizeof(tag)), 0);
  16584. ExpectIntEQ(XMEMCMP(plain, in, sizeof(in)), 0);
  16585. /* Free resources */
  16586. wc_AesFree(&aesEnc);
  16587. wc_AesFree(&aesDec);
  16588. #endif
  16589. return EXPECT_RESULT();
  16590. } /* END wc_AesGcmMixedEncDecLongIV */
  16591. /*
  16592. * unit test for wc_GmacSetKey()
  16593. */
  16594. static int test_wc_GmacSetKey(void)
  16595. {
  16596. EXPECT_DECLS;
  16597. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16598. Gmac gmac;
  16599. byte key16[] = {
  16600. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16601. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16602. };
  16603. #ifdef WOLFSSL_AES_192
  16604. byte key24[] = {
  16605. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16606. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16607. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16608. };
  16609. #endif
  16610. #ifdef WOLFSSL_AES_256
  16611. byte key32[] = {
  16612. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16613. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16614. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16615. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16616. };
  16617. #endif
  16618. byte badKey16[] = {
  16619. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16620. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x66
  16621. };
  16622. byte badKey24[] = {
  16623. 0x30, 0x31, 0x32, 0x33, 0x34, 0x36, 0x37,
  16624. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  16625. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  16626. };
  16627. byte badKey32[] = {
  16628. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16629. 0x38, 0x39, 0x61, 0x62, 0x64, 0x65, 0x66,
  16630. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  16631. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  16632. };
  16633. XMEMSET(&gmac, 0, sizeof(Gmac));
  16634. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16635. #ifdef WOLFSSL_AES_128
  16636. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)/sizeof(byte)), 0);
  16637. #endif
  16638. #ifdef WOLFSSL_AES_192
  16639. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  16640. #endif
  16641. #ifdef WOLFSSL_AES_256
  16642. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  16643. #endif
  16644. /* Pass in bad args. */
  16645. ExpectIntEQ(wc_GmacSetKey(NULL, key16, sizeof(key16)/sizeof(byte)),
  16646. BAD_FUNC_ARG);
  16647. ExpectIntEQ(wc_GmacSetKey(&gmac, NULL, sizeof(key16)/sizeof(byte)),
  16648. BAD_FUNC_ARG);
  16649. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey16, sizeof(badKey16)/sizeof(byte)),
  16650. BAD_FUNC_ARG);
  16651. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey24, sizeof(badKey24)/sizeof(byte)),
  16652. BAD_FUNC_ARG);
  16653. ExpectIntEQ(wc_GmacSetKey(&gmac, badKey32, sizeof(badKey32)/sizeof(byte)),
  16654. BAD_FUNC_ARG);
  16655. wc_AesFree(&gmac.aes);
  16656. #endif
  16657. return EXPECT_RESULT();
  16658. } /* END test_wc_GmacSetKey */
  16659. /*
  16660. * unit test for wc_GmacUpdate
  16661. */
  16662. static int test_wc_GmacUpdate(void)
  16663. {
  16664. EXPECT_DECLS;
  16665. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  16666. Gmac gmac;
  16667. #ifdef WOLFSSL_AES_128
  16668. const byte key16[] = {
  16669. 0x89, 0xc9, 0x49, 0xe9, 0xc8, 0x04, 0xaf, 0x01,
  16670. 0x4d, 0x56, 0x04, 0xb3, 0x94, 0x59, 0xf2, 0xc8
  16671. };
  16672. #endif
  16673. #ifdef WOLFSSL_AES_192
  16674. byte key24[] = {
  16675. 0x41, 0xc5, 0xda, 0x86, 0x67, 0xef, 0x72, 0x52,
  16676. 0x20, 0xff, 0xe3, 0x9a, 0xe0, 0xac, 0x59, 0x0a,
  16677. 0xc9, 0xfc, 0xa7, 0x29, 0xab, 0x60, 0xad, 0xa0
  16678. };
  16679. #endif
  16680. #ifdef WOLFSSL_AES_256
  16681. byte key32[] = {
  16682. 0x78, 0xdc, 0x4e, 0x0a, 0xaf, 0x52, 0xd9, 0x35,
  16683. 0xc3, 0xc0, 0x1e, 0xea, 0x57, 0x42, 0x8f, 0x00,
  16684. 0xca, 0x1f, 0xd4, 0x75, 0xf5, 0xda, 0x86, 0xa4,
  16685. 0x9c, 0x8d, 0xd7, 0x3d, 0x68, 0xc8, 0xe2, 0x23
  16686. };
  16687. #endif
  16688. #ifdef WOLFSSL_AES_128
  16689. const byte authIn[] = {
  16690. 0x82, 0xad, 0xcd, 0x63, 0x8d, 0x3f, 0xa9, 0xd9,
  16691. 0xf3, 0xe8, 0x41, 0x00, 0xd6, 0x1e, 0x07, 0x77
  16692. };
  16693. #endif
  16694. #ifdef WOLFSSL_AES_192
  16695. const byte authIn2[] = {
  16696. 0x8b, 0x5c, 0x12, 0x4b, 0xef, 0x6e, 0x2f, 0x0f,
  16697. 0xe4, 0xd8, 0xc9, 0x5c, 0xd5, 0xfa, 0x4c, 0xf1
  16698. };
  16699. #endif
  16700. const byte authIn3[] = {
  16701. 0xb9, 0x6b, 0xaa, 0x8c, 0x1c, 0x75, 0xa6, 0x71,
  16702. 0xbf, 0xb2, 0xd0, 0x8d, 0x06, 0xbe, 0x5f, 0x36
  16703. };
  16704. #ifdef WOLFSSL_AES_128
  16705. const byte tag1[] = { /* Known. */
  16706. 0x88, 0xdb, 0x9d, 0x62, 0x17, 0x2e, 0xd0, 0x43,
  16707. 0xaa, 0x10, 0xf1, 0x6d, 0x22, 0x7d, 0xc4, 0x1b
  16708. };
  16709. #endif
  16710. #ifdef WOLFSSL_AES_192
  16711. const byte tag2[] = { /* Known */
  16712. 0x20, 0x4b, 0xdb, 0x1b, 0xd6, 0x21, 0x54, 0xbf,
  16713. 0x08, 0x92, 0x2a, 0xaa, 0x54, 0xee, 0xd7, 0x05
  16714. };
  16715. #endif
  16716. const byte tag3[] = { /* Known */
  16717. 0x3e, 0x5d, 0x48, 0x6a, 0xa2, 0xe3, 0x0b, 0x22,
  16718. 0xe0, 0x40, 0xb8, 0x57, 0x23, 0xa0, 0x6e, 0x76
  16719. };
  16720. #ifdef WOLFSSL_AES_128
  16721. const byte iv[] = {
  16722. 0xd1, 0xb1, 0x04, 0xc8, 0x15, 0xbf, 0x1e, 0x94,
  16723. 0xe2, 0x8c, 0x8f, 0x16
  16724. };
  16725. #endif
  16726. #ifdef WOLFSSL_AES_192
  16727. const byte iv2[] = {
  16728. 0x05, 0xad, 0x13, 0xa5, 0xe2, 0xc2, 0xab, 0x66,
  16729. 0x7e, 0x1a, 0x6f, 0xbc
  16730. };
  16731. #endif
  16732. const byte iv3[] = {
  16733. 0xd7, 0x9c, 0xf2, 0x2d, 0x50, 0x4c, 0xc7, 0x93,
  16734. 0xc3, 0xfb, 0x6c, 0x8a
  16735. };
  16736. byte tagOut[16];
  16737. byte tagOut2[24];
  16738. byte tagOut3[32];
  16739. /* Init stack variables. */
  16740. XMEMSET(&gmac, 0, sizeof(Gmac));
  16741. XMEMSET(tagOut, 0, sizeof(tagOut));
  16742. XMEMSET(tagOut2, 0, sizeof(tagOut2));
  16743. XMEMSET(tagOut3, 0, sizeof(tagOut3));
  16744. #ifdef WOLFSSL_AES_128
  16745. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16746. ExpectIntEQ(wc_GmacSetKey(&gmac, key16, sizeof(key16)), 0);
  16747. ExpectIntEQ(wc_GmacUpdate(&gmac, iv, sizeof(iv), authIn, sizeof(authIn),
  16748. tagOut, sizeof(tag1)), 0);
  16749. ExpectIntEQ(XMEMCMP(tag1, tagOut, sizeof(tag1)), 0);
  16750. wc_AesFree(&gmac.aes);
  16751. #endif
  16752. #ifdef WOLFSSL_AES_192
  16753. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  16754. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  16755. ExpectIntEQ(wc_GmacSetKey(&gmac, key24, sizeof(key24)/sizeof(byte)), 0);
  16756. ExpectIntEQ(wc_GmacUpdate(&gmac, iv2, sizeof(iv2), authIn2, sizeof(authIn2),
  16757. tagOut2, sizeof(tag2)), 0);
  16758. ExpectIntEQ(XMEMCMP(tagOut2, tag2, sizeof(tag2)), 0);
  16759. wc_AesFree(&gmac.aes);
  16760. #endif
  16761. #ifdef WOLFSSL_AES_256
  16762. ExpectNotNull(XMEMSET(&gmac, 0, sizeof(Gmac)));
  16763. ExpectIntEQ(wc_AesInit(&gmac.aes, HEAP_HINT, INVALID_DEVID), 0);
  16764. ExpectIntEQ(wc_GmacSetKey(&gmac, key32, sizeof(key32)/sizeof(byte)), 0);
  16765. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16766. tagOut3, sizeof(tag3)), 0);
  16767. ExpectIntEQ(XMEMCMP(tag3, tagOut3, sizeof(tag3)), 0);
  16768. wc_AesFree(&gmac.aes);
  16769. #endif
  16770. /* Pass bad args. */
  16771. ExpectIntEQ(wc_AesInit(&gmac.aes, NULL, INVALID_DEVID), 0);
  16772. ExpectIntEQ(wc_GmacUpdate(NULL, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16773. tagOut3, sizeof(tag3)), BAD_FUNC_ARG);
  16774. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16775. tagOut3, sizeof(tag3) - 5), BAD_FUNC_ARG);
  16776. ExpectIntEQ(wc_GmacUpdate(&gmac, iv3, sizeof(iv3), authIn3, sizeof(authIn3),
  16777. tagOut3, sizeof(tag3) + 1), BAD_FUNC_ARG);
  16778. wc_AesFree(&gmac.aes);
  16779. #endif
  16780. return EXPECT_RESULT();
  16781. } /* END test_wc_GmacUpdate */
  16782. /*
  16783. * testing wc_CamelliaSetKey
  16784. */
  16785. static int test_wc_CamelliaSetKey(void)
  16786. {
  16787. EXPECT_DECLS;
  16788. #ifdef HAVE_CAMELLIA
  16789. Camellia camellia;
  16790. /*128-bit key*/
  16791. static const byte key16[] = {
  16792. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16793. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10
  16794. };
  16795. /* 192-bit key */
  16796. static const byte key24[] = {
  16797. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16798. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16799. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16800. };
  16801. /* 256-bit key */
  16802. static const byte key32[] = {
  16803. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16804. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16805. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
  16806. 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff
  16807. };
  16808. static const byte iv[] = {
  16809. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16810. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16811. };
  16812. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16), iv),
  16813. 0);
  16814. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key16, (word32)sizeof(key16),
  16815. NULL), 0);
  16816. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  16817. 0);
  16818. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16819. NULL), 0);
  16820. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32), iv),
  16821. 0);
  16822. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key32, (word32)sizeof(key32),
  16823. NULL), 0);
  16824. /* Bad args. */
  16825. ExpectIntEQ(wc_CamelliaSetKey(NULL, key32, (word32)sizeof(key32), iv),
  16826. BAD_FUNC_ARG);
  16827. #endif
  16828. return EXPECT_RESULT();
  16829. } /* END test_wc_CammeliaSetKey */
  16830. /*
  16831. * Testing wc_CamelliaSetIV()
  16832. */
  16833. static int test_wc_CamelliaSetIV(void)
  16834. {
  16835. EXPECT_DECLS;
  16836. #ifdef HAVE_CAMELLIA
  16837. Camellia camellia;
  16838. static const byte iv[] = {
  16839. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16840. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16841. };
  16842. ExpectIntEQ(wc_CamelliaSetIV(&camellia, iv), 0);
  16843. ExpectIntEQ(wc_CamelliaSetIV(&camellia, NULL), 0);
  16844. /* Bad args. */
  16845. ExpectIntEQ(wc_CamelliaSetIV(NULL, NULL), BAD_FUNC_ARG);
  16846. ExpectIntEQ(wc_CamelliaSetIV(NULL, iv), BAD_FUNC_ARG);
  16847. #endif
  16848. return EXPECT_RESULT();
  16849. } /* END test_wc_CamelliaSetIV*/
  16850. /*
  16851. * Test wc_CamelliaEncryptDirect and wc_CamelliaDecryptDirect
  16852. */
  16853. static int test_wc_CamelliaEncryptDecryptDirect(void)
  16854. {
  16855. EXPECT_DECLS;
  16856. #ifdef HAVE_CAMELLIA
  16857. Camellia camellia;
  16858. static const byte key24[] = {
  16859. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16860. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16861. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16862. };
  16863. static const byte iv[] = {
  16864. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  16865. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F
  16866. };
  16867. static const byte plainT[] = {
  16868. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16869. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16870. };
  16871. byte enc[sizeof(plainT)];
  16872. byte dec[sizeof(enc)];
  16873. /* Init stack variables.*/
  16874. XMEMSET(enc, 0, 16);
  16875. XMEMSET(enc, 0, 16);
  16876. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24), iv),
  16877. 0);
  16878. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, plainT), 0);
  16879. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, enc), 0);
  16880. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  16881. /* Pass bad args. */
  16882. ExpectIntEQ(wc_CamelliaEncryptDirect(NULL, enc, plainT), BAD_FUNC_ARG);
  16883. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, NULL, plainT),
  16884. BAD_FUNC_ARG);
  16885. ExpectIntEQ(wc_CamelliaEncryptDirect(&camellia, enc, NULL), BAD_FUNC_ARG);
  16886. ExpectIntEQ(wc_CamelliaDecryptDirect(NULL, dec, enc), BAD_FUNC_ARG);
  16887. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, NULL, enc), BAD_FUNC_ARG);
  16888. ExpectIntEQ(wc_CamelliaDecryptDirect(&camellia, dec, NULL), BAD_FUNC_ARG);
  16889. #endif
  16890. return EXPECT_RESULT();
  16891. } /* END test-wc_CamelliaEncryptDecryptDirect */
  16892. /*
  16893. * Testing wc_CamelliaCbcEncrypt and wc_CamelliaCbcDecrypt
  16894. */
  16895. static int test_wc_CamelliaCbcEncryptDecrypt(void)
  16896. {
  16897. EXPECT_DECLS;
  16898. #ifdef HAVE_CAMELLIA
  16899. Camellia camellia;
  16900. static const byte key24[] = {
  16901. 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef,
  16902. 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10,
  16903. 0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77
  16904. };
  16905. static const byte plainT[] = {
  16906. 0x6B, 0xC1, 0xBE, 0xE2, 0x2E, 0x40, 0x9F, 0x96,
  16907. 0xE9, 0x3D, 0x7E, 0x11, 0x73, 0x93, 0x17, 0x2A
  16908. };
  16909. byte enc[CAMELLIA_BLOCK_SIZE];
  16910. byte dec[CAMELLIA_BLOCK_SIZE];
  16911. /* Init stack variables. */
  16912. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16913. XMEMSET(enc, 0, CAMELLIA_BLOCK_SIZE);
  16914. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16915. NULL), 0);
  16916. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, plainT,
  16917. CAMELLIA_BLOCK_SIZE), 0);
  16918. ExpectIntEQ(wc_CamelliaSetKey(&camellia, key24, (word32)sizeof(key24),
  16919. NULL), 0);
  16920. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, enc, CAMELLIA_BLOCK_SIZE),
  16921. 0);
  16922. ExpectIntEQ(XMEMCMP(plainT, dec, CAMELLIA_BLOCK_SIZE), 0);
  16923. /* Pass in bad args. */
  16924. ExpectIntEQ(wc_CamelliaCbcEncrypt(NULL, enc, plainT, CAMELLIA_BLOCK_SIZE),
  16925. BAD_FUNC_ARG);
  16926. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, NULL, plainT,
  16927. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16928. ExpectIntEQ(wc_CamelliaCbcEncrypt(&camellia, enc, NULL,
  16929. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16930. ExpectIntEQ(wc_CamelliaCbcDecrypt(NULL, dec, enc, CAMELLIA_BLOCK_SIZE),
  16931. BAD_FUNC_ARG);
  16932. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, NULL, enc,
  16933. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16934. ExpectIntEQ(wc_CamelliaCbcDecrypt(&camellia, dec, NULL,
  16935. CAMELLIA_BLOCK_SIZE), BAD_FUNC_ARG);
  16936. #endif
  16937. return EXPECT_RESULT();
  16938. } /* END test_wc_CamelliaCbcEncryptDecrypt */
  16939. /*
  16940. * Testing wc_Arc4SetKey()
  16941. */
  16942. static int test_wc_Arc4SetKey(void)
  16943. {
  16944. EXPECT_DECLS;
  16945. #ifndef NO_RC4
  16946. Arc4 arc;
  16947. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16948. int keyLen = 8;
  16949. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, (word32)keyLen), 0);
  16950. /* Test bad args. */
  16951. ExpectIntEQ(wc_Arc4SetKey(NULL, (byte*)key, (word32)keyLen), BAD_FUNC_ARG);
  16952. ExpectIntEQ(wc_Arc4SetKey(&arc, NULL , (word32)keyLen), BAD_FUNC_ARG);
  16953. ExpectIntEQ(wc_Arc4SetKey(&arc, (byte*)key, 0 ), BAD_FUNC_ARG);
  16954. #endif
  16955. return EXPECT_RESULT();
  16956. } /* END test_wc_Arc4SetKey */
  16957. /*
  16958. * Testing wc_Arc4Process for ENC/DEC.
  16959. */
  16960. static int test_wc_Arc4Process(void)
  16961. {
  16962. EXPECT_DECLS;
  16963. #ifndef NO_RC4
  16964. Arc4 enc;
  16965. Arc4 dec;
  16966. const char* key = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16967. int keyLen = 8;
  16968. const char* input = "\x01\x23\x45\x67\x89\xab\xcd\xef";
  16969. byte cipher[8];
  16970. byte plain[8];
  16971. /* Init stack variables */
  16972. XMEMSET(&enc, 0, sizeof(Arc4));
  16973. XMEMSET(&dec, 0, sizeof(Arc4));
  16974. XMEMSET(cipher, 0, sizeof(cipher));
  16975. XMEMSET(plain, 0, sizeof(plain));
  16976. /* Use for async. */
  16977. ExpectIntEQ(wc_Arc4Init(&enc, NULL, INVALID_DEVID), 0);
  16978. ExpectIntEQ(wc_Arc4Init(&dec, NULL, INVALID_DEVID), 0);
  16979. ExpectIntEQ(wc_Arc4SetKey(&enc, (byte*)key, (word32)keyLen), 0);
  16980. ExpectIntEQ(wc_Arc4SetKey(&dec, (byte*)key, (word32)keyLen), 0);
  16981. ExpectIntEQ(wc_Arc4Process(&enc, cipher, (byte*)input, (word32)keyLen), 0);
  16982. ExpectIntEQ(wc_Arc4Process(&dec, plain, cipher, (word32)keyLen), 0);
  16983. ExpectIntEQ(XMEMCMP(plain, input, keyLen), 0);
  16984. /* Bad args. */
  16985. ExpectIntEQ(wc_Arc4Process(NULL, plain, cipher, (word32)keyLen), BAD_FUNC_ARG);
  16986. ExpectIntEQ(wc_Arc4Process(&dec, NULL, cipher, (word32)keyLen), BAD_FUNC_ARG);
  16987. ExpectIntEQ(wc_Arc4Process(&dec, plain, NULL, (word32)keyLen), BAD_FUNC_ARG);
  16988. wc_Arc4Free(&enc);
  16989. wc_Arc4Free(&dec);
  16990. #endif
  16991. return EXPECT_RESULT();
  16992. } /* END test_wc_Arc4Process */
  16993. /*
  16994. * Testing wc_Init RsaKey()
  16995. */
  16996. static int test_wc_InitRsaKey(void)
  16997. {
  16998. EXPECT_DECLS;
  16999. #ifndef NO_RSA
  17000. RsaKey key;
  17001. XMEMSET(&key, 0, sizeof(RsaKey));
  17002. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17003. /* Test bad args. */
  17004. ExpectIntEQ(wc_InitRsaKey(NULL, HEAP_HINT), BAD_FUNC_ARG);
  17005. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17006. #endif
  17007. return EXPECT_RESULT();
  17008. } /* END test_wc_InitRsaKey */
  17009. /*
  17010. * Testing wc_RsaPrivateKeyDecode()
  17011. */
  17012. static int test_wc_RsaPrivateKeyDecode(void)
  17013. {
  17014. EXPECT_DECLS;
  17015. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  17016. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  17017. RsaKey key;
  17018. byte* tmp = NULL;
  17019. word32 idx = 0;
  17020. int bytes = 0;
  17021. XMEMSET(&key, 0, sizeof(RsaKey));
  17022. ExpectNotNull(tmp = (byte*)XMALLOC(FOURK_BUF, NULL,
  17023. DYNAMIC_TYPE_TMP_BUFFER));
  17024. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17025. if (tmp != NULL) {
  17026. #ifdef USE_CERT_BUFFERS_1024
  17027. XMEMCPY(tmp, client_key_der_1024, sizeof_client_key_der_1024);
  17028. bytes = sizeof_client_key_der_1024;
  17029. #else
  17030. XMEMCPY(tmp, client_key_der_2048, sizeof_client_key_der_2048);
  17031. bytes = sizeof_client_key_der_2048;
  17032. #endif /* Use cert buffers. */
  17033. }
  17034. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &key, (word32)bytes), 0);
  17035. /* Test bad args. */
  17036. ExpectIntEQ(wc_RsaPrivateKeyDecode(NULL, &idx, &key, (word32)bytes),
  17037. BAD_FUNC_ARG);
  17038. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, NULL, &key, (word32)bytes),
  17039. BAD_FUNC_ARG);
  17040. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, NULL, (word32)bytes),
  17041. BAD_FUNC_ARG);
  17042. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17043. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17044. #endif
  17045. return EXPECT_RESULT();
  17046. } /* END test_wc_RsaPrivateKeyDecode */
  17047. /*
  17048. * Testing wc_RsaPublicKeyDecode()
  17049. */
  17050. static int test_wc_RsaPublicKeyDecode(void)
  17051. {
  17052. EXPECT_DECLS;
  17053. #if !defined(NO_RSA) && (defined(USE_CERT_BUFFERS_1024)\
  17054. || defined(USE_CERT_BUFFERS_2048)) && !defined(HAVE_FIPS)
  17055. RsaKey keyPub;
  17056. byte* tmp = NULL;
  17057. word32 idx = 0;
  17058. int bytes = 0;
  17059. word32 keySz = 0;
  17060. word32 tstKeySz = 0;
  17061. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  17062. XFILE f = XBADFILE;
  17063. const char* rsaPssPubKey = "./certs/rsapss/ca-rsapss-key.der";
  17064. const char* rsaPssPubKeyNoParams = "./certs/rsapss/ca-3072-rsapss-key.der";
  17065. byte buf[4096];
  17066. #endif
  17067. XMEMSET(&keyPub, 0, sizeof(RsaKey));
  17068. ExpectNotNull(tmp = (byte*)XMALLOC(GEN_BUF, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17069. ExpectIntEQ(wc_InitRsaKey(&keyPub, HEAP_HINT), 0);
  17070. if (tmp != NULL) {
  17071. #ifdef USE_CERT_BUFFERS_1024
  17072. XMEMCPY(tmp, client_keypub_der_1024, sizeof_client_keypub_der_1024);
  17073. bytes = sizeof_client_keypub_der_1024;
  17074. keySz = 1024;
  17075. #else
  17076. XMEMCPY(tmp, client_keypub_der_2048, sizeof_client_keypub_der_2048);
  17077. bytes = sizeof_client_keypub_der_2048;
  17078. keySz = 2048;
  17079. #endif
  17080. }
  17081. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, &keyPub, (word32)bytes), 0);
  17082. /* Pass in bad args. */
  17083. ExpectIntEQ(wc_RsaPublicKeyDecode(NULL, &idx, &keyPub, (word32)bytes),
  17084. BAD_FUNC_ARG);
  17085. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, NULL, &keyPub, (word32)bytes),
  17086. BAD_FUNC_ARG);
  17087. ExpectIntEQ(wc_RsaPublicKeyDecode(tmp, &idx, NULL, (word32)bytes),
  17088. BAD_FUNC_ARG);
  17089. DoExpectIntEQ(wc_FreeRsaKey(&keyPub), 0);
  17090. /* Test for getting modulus key size */
  17091. idx = 0;
  17092. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(tmp, &idx, (word32)bytes, NULL,
  17093. &tstKeySz, NULL, NULL), 0);
  17094. ExpectIntEQ(tstKeySz, keySz/8);
  17095. #if defined(WC_RSA_PSS) && !defined(NO_FILESYSTEM)
  17096. ExpectTrue((f = XFOPEN(rsaPssPubKey, "rb")) != XBADFILE);
  17097. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  17098. if (f != XBADFILE) {
  17099. XFCLOSE(f);
  17100. f = XBADFILE;
  17101. }
  17102. idx = 0;
  17103. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, (word32)bytes, NULL, NULL, NULL,
  17104. NULL), 0);
  17105. ExpectTrue((f = XFOPEN(rsaPssPubKeyNoParams, "rb")) != XBADFILE);
  17106. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  17107. if (f != XBADFILE)
  17108. XFCLOSE(f);
  17109. idx = 0;
  17110. ExpectIntEQ(wc_RsaPublicKeyDecode_ex(buf, &idx, (word32)bytes, NULL, NULL, NULL,
  17111. NULL), 0);
  17112. #endif
  17113. XFREE(tmp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17114. #endif
  17115. return EXPECT_RESULT();
  17116. } /* END test_wc_RsaPublicKeyDecode */
  17117. /*
  17118. * Testing wc_RsaPublicKeyDecodeRaw()
  17119. */
  17120. static int test_wc_RsaPublicKeyDecodeRaw(void)
  17121. {
  17122. EXPECT_DECLS;
  17123. #if !defined(NO_RSA)
  17124. RsaKey key;
  17125. const byte n = 0x23;
  17126. const byte e = 0x03;
  17127. int nSz = sizeof(n);
  17128. int eSz = sizeof(e);
  17129. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17130. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, &key), 0);
  17131. /* Pass in bad args. */
  17132. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(NULL, nSz, &e, eSz, &key),
  17133. BAD_FUNC_ARG);
  17134. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, NULL, eSz, &key),
  17135. BAD_FUNC_ARG);
  17136. ExpectIntEQ(wc_RsaPublicKeyDecodeRaw(&n, nSz, &e, eSz, NULL),
  17137. BAD_FUNC_ARG);
  17138. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17139. #endif
  17140. return EXPECT_RESULT();
  17141. } /* END test_wc_RsaPublicKeyDecodeRaw */
  17142. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17143. /* In FIPS builds, wc_MakeRsaKey() will return an error if it cannot find
  17144. * a probable prime in 5*(modLen/2) attempts. In non-FIPS builds, it keeps
  17145. * trying until it gets a probable prime. */
  17146. #ifdef HAVE_FIPS
  17147. static int MakeRsaKeyRetry(RsaKey* key, int size, long e, WC_RNG* rng)
  17148. {
  17149. int ret;
  17150. for (;;) {
  17151. ret = wc_MakeRsaKey(key, size, e, rng);
  17152. if (ret != PRIME_GEN_E) break;
  17153. fprintf(stderr, "MakeRsaKey couldn't find prime; "
  17154. "trying again.\n");
  17155. }
  17156. return ret;
  17157. }
  17158. #define MAKE_RSA_KEY(a, b, c, d) MakeRsaKeyRetry(a, b, c, d)
  17159. #else
  17160. #define MAKE_RSA_KEY(a, b, c, d) wc_MakeRsaKey(a, b, c, d)
  17161. #endif
  17162. #endif
  17163. /*
  17164. * Testing wc_MakeRsaKey()
  17165. */
  17166. static int test_wc_MakeRsaKey(void)
  17167. {
  17168. EXPECT_DECLS;
  17169. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17170. RsaKey genKey;
  17171. WC_RNG rng;
  17172. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17173. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17174. int bits = 1024;
  17175. #else
  17176. int bits = 2048;
  17177. #endif
  17178. XMEMSET(&genKey, 0, sizeof(RsaKey));
  17179. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17180. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  17181. ExpectIntEQ(wc_InitRng(&rng), 0);
  17182. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  17183. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  17184. /* Test bad args. */
  17185. ExpectIntEQ(MAKE_RSA_KEY(NULL, bits, WC_RSA_EXPONENT, &rng), BAD_FUNC_ARG);
  17186. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, NULL),
  17187. BAD_FUNC_ARG);
  17188. /* e < 3 */
  17189. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 2, &rng), BAD_FUNC_ARG);
  17190. /* e & 1 == 0 */
  17191. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, 6, &rng), BAD_FUNC_ARG);
  17192. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17193. #endif
  17194. return EXPECT_RESULT();
  17195. } /* END test_wc_MakeRsaKey */
  17196. /*
  17197. * Test the bounds checking on the cipher text versus the key modulus.
  17198. * 1. Make a new RSA key.
  17199. * 2. Set c to 1.
  17200. * 3. Decrypt c into k. (error)
  17201. * 4. Copy the key modulus to c and sub 1 from the copy.
  17202. * 5. Decrypt c into k. (error)
  17203. * Valid bounds test cases are covered by all the other RSA tests.
  17204. */
  17205. static int test_RsaDecryptBoundsCheck(void)
  17206. {
  17207. EXPECT_DECLS;
  17208. #if !defined(NO_RSA) && defined(WC_RSA_NO_PADDING) && \
  17209. (defined(USE_CERT_BUFFERS_1024) || defined(USE_CERT_BUFFERS_2048)) && \
  17210. defined(WOLFSSL_PUBLIC_MP) && !defined(NO_RSA_BOUNDS_CHECK)
  17211. WC_RNG rng;
  17212. RsaKey key;
  17213. byte flatC[256];
  17214. word32 flatCSz;
  17215. byte out[256];
  17216. word32 outSz = sizeof(out);
  17217. XMEMSET(&key, 0, sizeof(RsaKey));
  17218. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17219. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17220. ExpectIntEQ(wc_InitRng(&rng), 0);
  17221. if (EXPECT_SUCCESS()) {
  17222. const byte* derKey;
  17223. word32 derKeySz;
  17224. word32 idx = 0;
  17225. #ifdef USE_CERT_BUFFERS_1024
  17226. derKey = server_key_der_1024;
  17227. derKeySz = (word32)sizeof_server_key_der_1024;
  17228. flatCSz = 128;
  17229. #else
  17230. derKey = server_key_der_2048;
  17231. derKeySz = (word32)sizeof_server_key_der_2048;
  17232. flatCSz = 256;
  17233. #endif
  17234. ExpectIntEQ(wc_RsaPrivateKeyDecode(derKey, &idx, &key, derKeySz), 0);
  17235. }
  17236. if (EXPECT_SUCCESS()) {
  17237. XMEMSET(flatC, 0, flatCSz);
  17238. flatC[flatCSz-1] = 1;
  17239. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  17240. RSA_PRIVATE_DECRYPT, &rng), RSA_OUT_OF_RANGE_E);
  17241. if (EXPECT_SUCCESS()) {
  17242. mp_int c;
  17243. ExpectIntEQ(mp_init_copy(&c, &key.n), 0);
  17244. ExpectIntEQ(mp_sub_d(&c, 1, &c), 0);
  17245. ExpectIntEQ(mp_to_unsigned_bin(&c, flatC), 0);
  17246. ExpectIntEQ(wc_RsaDirect(flatC, flatCSz, out, &outSz, &key,
  17247. RSA_PRIVATE_DECRYPT, NULL), RSA_OUT_OF_RANGE_E);
  17248. mp_clear(&c);
  17249. }
  17250. }
  17251. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17252. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17253. #endif
  17254. return EXPECT_RESULT();
  17255. } /* END test_wc_RsaDecryptBoundsCheck */
  17256. /*
  17257. * Testing wc_SetKeyUsage()
  17258. */
  17259. static int test_wc_SetKeyUsage(void)
  17260. {
  17261. EXPECT_DECLS;
  17262. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN) && !defined(HAVE_FIPS)
  17263. Cert myCert;
  17264. ExpectIntEQ(wc_InitCert(&myCert), 0);
  17265. ExpectIntEQ(wc_SetKeyUsage(&myCert, "keyEncipherment,keyAgreement"), 0);
  17266. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature,nonRepudiation"), 0);
  17267. ExpectIntEQ(wc_SetKeyUsage(&myCert, "contentCommitment,encipherOnly"), 0);
  17268. ExpectIntEQ(wc_SetKeyUsage(&myCert, "decipherOnly"), 0);
  17269. ExpectIntEQ(wc_SetKeyUsage(&myCert, "cRLSign,keyCertSign"), 0);
  17270. /* Test bad args. */
  17271. ExpectIntEQ(wc_SetKeyUsage(NULL, "decipherOnly"), BAD_FUNC_ARG);
  17272. ExpectIntEQ(wc_SetKeyUsage(&myCert, NULL), BAD_FUNC_ARG);
  17273. ExpectIntEQ(wc_SetKeyUsage(&myCert, ""), KEYUSAGE_E);
  17274. ExpectIntEQ(wc_SetKeyUsage(&myCert, ","), KEYUSAGE_E);
  17275. ExpectIntEQ(wc_SetKeyUsage(&myCert, "digitalSignature, cRLSign"),
  17276. KEYUSAGE_E);
  17277. #endif
  17278. return EXPECT_RESULT();
  17279. } /* END test_wc_SetKeyUsage */
  17280. /*
  17281. * Testing wc_CheckProbablePrime()
  17282. */
  17283. static int test_wc_CheckProbablePrime(void)
  17284. {
  17285. EXPECT_DECLS;
  17286. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17287. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING)
  17288. #define CHECK_PROBABLE_PRIME_KEY_BITS 2048
  17289. RsaKey key;
  17290. WC_RNG rng;
  17291. byte e[3];
  17292. word32 eSz = (word32)sizeof(e);
  17293. byte n[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  17294. word32 nSz = (word32)sizeof(n);
  17295. byte d[CHECK_PROBABLE_PRIME_KEY_BITS / 8];
  17296. word32 dSz = (word32)sizeof(d);
  17297. byte p[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  17298. word32 pSz = (word32)sizeof(p);
  17299. byte q[CHECK_PROBABLE_PRIME_KEY_BITS / 8 / 2];
  17300. word32 qSz = (word32)sizeof(q);
  17301. int nlen = CHECK_PROBABLE_PRIME_KEY_BITS;
  17302. int* isPrime;
  17303. int test[5];
  17304. isPrime = test;
  17305. XMEMSET(&key, 0, sizeof(RsaKey));
  17306. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17307. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17308. ExpectIntEQ(wc_InitRng(&rng), 0);
  17309. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17310. ExpectIntEQ(wc_MakeRsaKey(&key, CHECK_PROBABLE_PRIME_KEY_BITS,
  17311. WC_RSA_EXPONENT, &rng), 0);
  17312. PRIVATE_KEY_UNLOCK();
  17313. ExpectIntEQ(wc_RsaExportKey(&key, e, &eSz, n, &nSz, d, &dSz, p, &pSz, q,
  17314. &qSz), 0);
  17315. PRIVATE_KEY_LOCK();
  17316. /* Bad cases */
  17317. ExpectIntEQ(wc_CheckProbablePrime(NULL, pSz, q, qSz, e, eSz, nlen, isPrime),
  17318. BAD_FUNC_ARG);
  17319. ExpectIntEQ(wc_CheckProbablePrime(p, 0, q, qSz, e, eSz, nlen, isPrime),
  17320. BAD_FUNC_ARG);
  17321. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, NULL, qSz, e, eSz, nlen, isPrime),
  17322. BAD_FUNC_ARG);
  17323. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, 0, e, eSz, nlen, isPrime),
  17324. BAD_FUNC_ARG);
  17325. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, NULL, eSz, nlen, isPrime),
  17326. BAD_FUNC_ARG);
  17327. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, 0, nlen, isPrime),
  17328. BAD_FUNC_ARG);
  17329. ExpectIntEQ(wc_CheckProbablePrime(NULL, 0, NULL, 0, NULL, 0, nlen, isPrime),
  17330. BAD_FUNC_ARG);
  17331. /* Good case */
  17332. ExpectIntEQ(wc_CheckProbablePrime(p, pSz, q, qSz, e, eSz, nlen, isPrime),
  17333. 0);
  17334. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17335. wc_FreeRng(&rng);
  17336. #undef CHECK_PROBABLE_PRIME_KEY_BITS
  17337. #endif
  17338. return EXPECT_RESULT();
  17339. } /* END test_wc_CheckProbablePrime */
  17340. /*
  17341. * Testing wc_RsaPSS_Verify()
  17342. */
  17343. static int test_wc_RsaPSS_Verify(void)
  17344. {
  17345. EXPECT_DECLS;
  17346. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17347. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17348. RsaKey key;
  17349. WC_RNG rng;
  17350. int sz = 256;
  17351. const char* szMessage = "This is the string to be signed";
  17352. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17353. unsigned char pDecrypted[2048/8];
  17354. byte* pt = pDecrypted;
  17355. word32 outLen = sizeof(pDecrypted);
  17356. XMEMSET(&key, 0, sizeof(RsaKey));
  17357. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17358. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17359. ExpectIntEQ(wc_InitRng(&rng), 0);
  17360. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17361. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17362. ExpectIntGT(sz = wc_RsaPSS_Sign((byte*)szMessage,
  17363. (word32)XSTRLEN(szMessage)+1, pSignature, sizeof(pSignature),
  17364. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17365. /* Bad cases */
  17366. ExpectIntEQ(wc_RsaPSS_Verify(NULL, (word32)sz, pt, outLen,
  17367. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17368. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, 0, pt, outLen,
  17369. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17370. ExpectIntEQ(wc_RsaPSS_Verify(pSignature, (word32)sz, NULL, outLen,
  17371. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17372. ExpectIntEQ(wc_RsaPSS_Verify(NULL, 0, NULL, outLen,
  17373. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17374. /* Good case */
  17375. ExpectIntGT(wc_RsaPSS_Verify(pSignature, (word32)sz, pt, outLen,
  17376. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17377. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17378. wc_FreeRng(&rng);
  17379. #endif
  17380. return EXPECT_RESULT();
  17381. } /* END test_wc_RsaPSS_Verify */
  17382. /*
  17383. * Testing wc_RsaPSS_VerifyCheck()
  17384. */
  17385. static int test_wc_RsaPSS_VerifyCheck(void)
  17386. {
  17387. EXPECT_DECLS;
  17388. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17389. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17390. RsaKey key;
  17391. WC_RNG rng;
  17392. int sz = 256; /* 2048/8 */
  17393. byte digest[32];
  17394. word32 digestSz = sizeof(digest);
  17395. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17396. word32 pSignatureSz = sizeof(pSignature);
  17397. unsigned char pDecrypted[2048/8];
  17398. byte* pt = pDecrypted;
  17399. word32 outLen = sizeof(pDecrypted);
  17400. XMEMSET(&key, 0, sizeof(RsaKey));
  17401. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17402. XMEMSET(digest, 0, sizeof(digest));
  17403. XMEMSET(pSignature, 0, sizeof(pSignature));
  17404. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17405. ExpectIntEQ(wc_InitRng(&rng), 0);
  17406. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17407. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17408. ExpectTrue((digestSz = (word32)wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  17409. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, (word32)sz, digest, digestSz),
  17410. 0);
  17411. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature, pSignatureSz,
  17412. WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17413. /* Bad cases */
  17414. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, (word32)sz, pt, outLen, digest,
  17415. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17416. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, 0, pt, outLen, digest,
  17417. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17418. ExpectIntEQ(wc_RsaPSS_VerifyCheck(pSignature, (word32)sz, NULL, outLen, digest,
  17419. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17420. ExpectIntEQ(wc_RsaPSS_VerifyCheck(NULL, 0, NULL, outLen, digest,
  17421. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17422. /* Good case */
  17423. ExpectIntGT(wc_RsaPSS_VerifyCheck(pSignature, (word32)sz, pt, outLen, digest,
  17424. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17425. ExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17426. wc_FreeRng(&rng);
  17427. #endif
  17428. return EXPECT_RESULT();
  17429. } /* END test_wc_RsaPSS_VerifyCheck */
  17430. /*
  17431. * Testing wc_RsaPSS_VerifyCheckInline()
  17432. */
  17433. static int test_wc_RsaPSS_VerifyCheckInline(void)
  17434. {
  17435. EXPECT_DECLS;
  17436. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_SELFTEST) && \
  17437. !defined(HAVE_FIPS) && defined(WC_RSA_BLINDING) && defined(WC_RSA_PSS)
  17438. RsaKey key;
  17439. WC_RNG rng;
  17440. int sz = 256;
  17441. byte digest[32];
  17442. word32 digestSz = sizeof(digest);
  17443. unsigned char pSignature[2048/8]; /* 2048 is RSA_KEY_SIZE */
  17444. unsigned char pDecrypted[2048/8];
  17445. byte* pt = pDecrypted;
  17446. XMEMSET(&key, 0, sizeof(RsaKey));
  17447. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17448. XMEMSET(digest, 0, sizeof(digest));
  17449. XMEMSET(pSignature, 0, sizeof(pSignature));
  17450. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17451. ExpectIntEQ(wc_InitRng(&rng), 0);
  17452. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17453. ExpectIntEQ(wc_MakeRsaKey(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17454. ExpectTrue((digestSz = (word32)wc_HashGetDigestSize(WC_HASH_TYPE_SHA256)) > 0);
  17455. ExpectIntEQ(wc_Hash(WC_HASH_TYPE_SHA256, pSignature, (word32)sz, digest, digestSz),
  17456. 0);
  17457. ExpectIntGT(sz = wc_RsaPSS_Sign(digest, digestSz, pSignature,
  17458. sizeof(pSignature), WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key, &rng), 0);
  17459. /* Bad Cases */
  17460. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, (word32)sz, &pt, digest,
  17461. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17462. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, 0, NULL, digest,
  17463. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17464. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(NULL, 0, &pt, digest,
  17465. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17466. ExpectIntEQ(wc_RsaPSS_VerifyCheckInline(pSignature, (word32)sz, &pt, digest,
  17467. digestSz, WC_HASH_TYPE_SHA, WC_MGF1SHA256, &key), BAD_FUNC_ARG);
  17468. /* Good case */
  17469. ExpectIntGT(wc_RsaPSS_VerifyCheckInline(pSignature, (word32)sz, &pt, digest,
  17470. digestSz, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, &key), 0);
  17471. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17472. wc_FreeRng(&rng);
  17473. #endif
  17474. return EXPECT_RESULT();
  17475. } /* END test_wc_RsaPSS_VerifyCheckInline */
  17476. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17477. static void sample_mutex_cb (int flag, int type, const char* file, int line)
  17478. {
  17479. (void)flag;
  17480. (void)type;
  17481. (void)file;
  17482. (void)line;
  17483. }
  17484. #endif
  17485. /*
  17486. * Testing wc_LockMutex_ex
  17487. */
  17488. static int test_wc_LockMutex_ex(void)
  17489. {
  17490. EXPECT_DECLS;
  17491. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17492. int flag = CRYPTO_LOCK;
  17493. int type = 0;
  17494. const char* file = "./test-LockMutex_ex.txt";
  17495. int line = 0;
  17496. /* without SetMutexCb */
  17497. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), BAD_STATE_E);
  17498. /* with SetMutexCb */
  17499. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  17500. ExpectIntEQ(wc_LockMutex_ex(flag, type, file, line), 0);
  17501. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  17502. #endif
  17503. return EXPECT_RESULT();
  17504. } /* End test_wc_LockMutex_ex*/
  17505. /*
  17506. * Testing wc_SetMutexCb
  17507. */
  17508. static int test_wc_SetMutexCb(void)
  17509. {
  17510. EXPECT_DECLS;
  17511. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  17512. ExpectIntEQ(wc_SetMutexCb(sample_mutex_cb), 0);
  17513. ExpectIntEQ(wc_SetMutexCb(NULL), 0);
  17514. #endif
  17515. return EXPECT_RESULT();
  17516. } /* End test_wc_SetMutexCb*/
  17517. /*
  17518. * Testing wc_RsaKeyToDer()
  17519. */
  17520. static int test_wc_RsaKeyToDer(void)
  17521. {
  17522. EXPECT_DECLS;
  17523. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17524. RsaKey genKey;
  17525. WC_RNG rng;
  17526. byte* der = NULL;
  17527. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17528. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17529. int bits = 1024;
  17530. word32 derSz = 611;
  17531. /* (2 x 128) + 2 (possible leading 00) + (5 x 64) + 5 (possible leading 00)
  17532. + 3 (e) + 8 (ASN tag) + 10 (ASN length) + 4 seqSz + 3 version */
  17533. #else
  17534. int bits = 2048;
  17535. word32 derSz = 1196;
  17536. /* (2 x 256) + 2 (possible leading 00) + (5 x 128) + 5 (possible leading 00)
  17537. + 3 (e) + 8 (ASN tag) + 17 (ASN length) + 4 seqSz + 3 version */
  17538. #endif
  17539. XMEMSET(&rng, 0, sizeof(rng));
  17540. XMEMSET(&genKey, 0, sizeof(genKey));
  17541. ExpectNotNull(der = (byte*)XMALLOC(derSz, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17542. /* Init structures. */
  17543. ExpectIntEQ(wc_InitRsaKey(&genKey, HEAP_HINT), 0);
  17544. ExpectIntEQ(wc_InitRng(&rng), 0);
  17545. /* Make key. */
  17546. ExpectIntEQ(MAKE_RSA_KEY(&genKey, bits, WC_RSA_EXPONENT, &rng), 0);
  17547. ExpectIntGT(wc_RsaKeyToDer(&genKey, der, derSz), 0);
  17548. /* Pass good/bad args. */
  17549. ExpectIntEQ(wc_RsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  17550. /* Get just the output length */
  17551. ExpectIntGT(wc_RsaKeyToDer(&genKey, NULL, 0), 0);
  17552. /* Try Public Key. */
  17553. genKey.type = 0;
  17554. ExpectIntEQ(wc_RsaKeyToDer(&genKey, der, FOURK_BUF), BAD_FUNC_ARG);
  17555. #ifdef WOLFSSL_CHECK_MEM_ZERO
  17556. /* Put back to Private Key */
  17557. genKey.type = 1;
  17558. #endif
  17559. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17560. DoExpectIntEQ(wc_FreeRsaKey(&genKey), 0);
  17561. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17562. #endif
  17563. return EXPECT_RESULT();
  17564. } /* END test_wc_RsaKeyToDer */
  17565. /*
  17566. * Testing wc_RsaKeyToPublicDer()
  17567. */
  17568. static int test_wc_RsaKeyToPublicDer(void)
  17569. {
  17570. EXPECT_DECLS;
  17571. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17572. RsaKey key;
  17573. WC_RNG rng;
  17574. byte* der = NULL;
  17575. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17576. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17577. int bits = 1024;
  17578. word32 derLen = 162;
  17579. #else
  17580. int bits = 2048;
  17581. word32 derLen = 294;
  17582. #endif
  17583. int ret;
  17584. XMEMSET(&rng, 0, sizeof(rng));
  17585. XMEMSET(&key, 0, sizeof(key));
  17586. ExpectNotNull(der = (byte*)XMALLOC(derLen, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  17587. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17588. ExpectIntEQ(wc_InitRng(&rng), 0);
  17589. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17590. /* test getting size only */
  17591. ExpectIntGT(wc_RsaKeyToPublicDer(&key, NULL, derLen), 0);
  17592. ExpectIntGT(wc_RsaKeyToPublicDer(&key, der, derLen), 0);
  17593. /* test getting size only */
  17594. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, NULL, derLen, 0), 0);
  17595. ExpectIntGT(wc_RsaKeyToPublicDer_ex(&key, der, derLen, 0), 0);
  17596. /* Pass in bad args. */
  17597. ExpectIntEQ(wc_RsaKeyToPublicDer(NULL, der, derLen), BAD_FUNC_ARG);
  17598. ExpectIntLT(ret = wc_RsaKeyToPublicDer(&key, der, -1), 0);
  17599. ExpectTrue((ret == BUFFER_E) || (ret == BAD_FUNC_ARG));
  17600. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  17601. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17602. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17603. #endif
  17604. return EXPECT_RESULT();
  17605. } /* END test_wc_RsaKeyToPublicDer */
  17606. /*
  17607. * Testing wc_RsaPublicEncrypt() and wc_RsaPrivateDecrypt()
  17608. */
  17609. static int test_wc_RsaPublicEncryptDecrypt(void)
  17610. {
  17611. EXPECT_DECLS;
  17612. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17613. RsaKey key;
  17614. WC_RNG rng;
  17615. const char inStr[] = TEST_STRING;
  17616. const word32 plainLen = (word32)TEST_STRING_SZ;
  17617. const word32 inLen = (word32)TEST_STRING_SZ;
  17618. int bits = TEST_RSA_BITS;
  17619. const word32 cipherLen = TEST_RSA_BYTES;
  17620. word32 cipherLenResult = cipherLen;
  17621. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17622. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17623. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17624. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17625. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17626. WC_ALLOC_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17627. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17628. ExpectNotNull(in);
  17629. ExpectNotNull(plain);
  17630. ExpectNotNull(cipher);
  17631. #endif
  17632. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17633. /* Initialize stack structures. */
  17634. XMEMSET(&key, 0, sizeof(RsaKey));
  17635. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17636. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17637. ExpectIntEQ(wc_InitRng(&rng), 0);
  17638. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17639. /* Encrypt. */
  17640. ExpectIntGT(cipherLenResult = (word32)wc_RsaPublicEncrypt(in, inLen, cipher,
  17641. cipherLen, &key, &rng), 0);
  17642. /* Pass bad args - tested in another testing function.*/
  17643. /* Decrypt */
  17644. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17645. /* Bind rng */
  17646. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17647. #endif
  17648. ExpectIntGE(wc_RsaPrivateDecrypt(cipher, cipherLenResult, plain, plainLen,
  17649. &key), 0);
  17650. ExpectIntEQ(XMEMCMP(plain, inStr, plainLen), 0);
  17651. /* Pass bad args - tested in another testing function.*/
  17652. WC_FREE_VAR(in, NULL);
  17653. WC_FREE_VAR(plain, NULL);
  17654. WC_FREE_VAR(cipher, NULL);
  17655. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17656. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17657. #endif
  17658. return EXPECT_RESULT();
  17659. } /* END test_wc_RsaPublicEncryptDecrypt */
  17660. /*
  17661. * Testing wc_RsaPrivateDecrypt_ex() and wc_RsaPrivateDecryptInline_ex()
  17662. */
  17663. static int test_wc_RsaPublicEncryptDecrypt_ex(void)
  17664. {
  17665. EXPECT_DECLS;
  17666. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && !defined(HAVE_FIPS)\
  17667. && !defined(WC_NO_RSA_OAEP) && !defined(NO_SHA256)
  17668. RsaKey key;
  17669. WC_RNG rng;
  17670. const char inStr[] = TEST_STRING;
  17671. const word32 inLen = (word32)TEST_STRING_SZ;
  17672. const word32 plainSz = (word32)TEST_STRING_SZ;
  17673. byte* res = NULL;
  17674. int idx = 0;
  17675. int bits = TEST_RSA_BITS;
  17676. const word32 cipherSz = TEST_RSA_BYTES;
  17677. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17678. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17679. WC_DECLARE_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17680. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17681. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17682. WC_ALLOC_VAR(cipher, byte, TEST_RSA_BYTES, NULL);
  17683. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17684. ExpectNotNull(in);
  17685. ExpectNotNull(plain);
  17686. ExpectNotNull(cipher);
  17687. #endif
  17688. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17689. /* Initialize stack structures. */
  17690. XMEMSET(&key, 0, sizeof(RsaKey));
  17691. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17692. ExpectIntEQ(wc_InitRsaKey_ex(&key, HEAP_HINT, INVALID_DEVID), 0);
  17693. ExpectIntEQ(wc_InitRng(&rng), 0);
  17694. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17695. /* Encrypt */
  17696. ExpectIntGE(idx = wc_RsaPublicEncrypt_ex(in, inLen, cipher, cipherSz, &key,
  17697. &rng, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17698. /* Pass bad args - tested in another testing function.*/
  17699. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  17700. /* Decrypt */
  17701. #if defined(WC_RSA_BLINDING) && !defined(HAVE_FIPS)
  17702. ExpectIntEQ(wc_RsaSetRNG(&key, &rng), 0);
  17703. #endif
  17704. ExpectIntGE(wc_RsaPrivateDecrypt_ex(cipher, (word32)idx, plain, plainSz,
  17705. &key, WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17706. ExpectIntEQ(XMEMCMP(plain, inStr, plainSz), 0);
  17707. /* Pass bad args - tested in another testing function.*/
  17708. ExpectIntGE(wc_RsaPrivateDecryptInline_ex(cipher, (word32)idx, &res, &key,
  17709. WC_RSA_OAEP_PAD, WC_HASH_TYPE_SHA256, WC_MGF1SHA256, NULL, 0), 0);
  17710. ExpectIntEQ(XMEMCMP(inStr, res, plainSz), 0);
  17711. #endif
  17712. WC_FREE_VAR(in, NULL);
  17713. WC_FREE_VAR(plain, NULL);
  17714. WC_FREE_VAR(cipher, NULL);
  17715. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17716. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17717. #endif
  17718. return EXPECT_RESULT();
  17719. } /* END test_wc_RsaPublicEncryptDecrypt_ex */
  17720. /*
  17721. * Tesing wc_RsaSSL_Sign() and wc_RsaSSL_Verify()
  17722. */
  17723. static int test_wc_RsaSSL_SignVerify(void)
  17724. {
  17725. EXPECT_DECLS;
  17726. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17727. RsaKey key;
  17728. WC_RNG rng;
  17729. const char inStr[] = TEST_STRING;
  17730. const word32 plainSz = (word32)TEST_STRING_SZ;
  17731. const word32 inLen = (word32)TEST_STRING_SZ;
  17732. word32 idx = 0;
  17733. int bits = TEST_RSA_BITS;
  17734. const word32 outSz = TEST_RSA_BYTES;
  17735. WC_DECLARE_VAR(in, byte, TEST_STRING_SZ, NULL);
  17736. WC_DECLARE_VAR(out, byte, TEST_RSA_BYTES, NULL);
  17737. WC_DECLARE_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17738. WC_ALLOC_VAR(in, byte, TEST_STRING_SZ, NULL);
  17739. WC_ALLOC_VAR(out, byte, TEST_RSA_BYTES, NULL);
  17740. WC_ALLOC_VAR(plain, byte, TEST_STRING_SZ, NULL);
  17741. #ifdef WC_DECLARE_VAR_IS_HEAP_ALLOC
  17742. ExpectNotNull(in);
  17743. ExpectNotNull(out);
  17744. ExpectNotNull(plain);
  17745. #endif
  17746. ExpectNotNull(XMEMCPY(in, inStr, inLen));
  17747. XMEMSET(&key, 0, sizeof(RsaKey));
  17748. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17749. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17750. ExpectIntEQ(wc_InitRng(&rng), 0);
  17751. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17752. /* Sign. */
  17753. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, &key, &rng), (int)outSz);
  17754. idx = (int)outSz;
  17755. /* Test bad args. */
  17756. ExpectIntEQ(wc_RsaSSL_Sign(NULL, inLen, out, outSz, &key, &rng),
  17757. BAD_FUNC_ARG);
  17758. ExpectIntEQ(wc_RsaSSL_Sign(in, 0, out, outSz, &key, &rng),
  17759. BAD_FUNC_ARG);
  17760. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, NULL, outSz, &key, &rng),
  17761. BAD_FUNC_ARG);
  17762. ExpectIntEQ(wc_RsaSSL_Sign(in, inLen, out, outSz, NULL, &rng),
  17763. BAD_FUNC_ARG);
  17764. /* Verify. */
  17765. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, &key), (int)inLen);
  17766. /* Pass bad args. */
  17767. ExpectIntEQ(wc_RsaSSL_Verify(NULL, idx, plain, plainSz, &key),
  17768. BAD_FUNC_ARG);
  17769. ExpectIntEQ(wc_RsaSSL_Verify(out, 0, plain, plainSz, &key),
  17770. BAD_FUNC_ARG);
  17771. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, NULL, plainSz, &key),
  17772. BAD_FUNC_ARG);
  17773. ExpectIntEQ(wc_RsaSSL_Verify(out, idx, plain, plainSz, NULL),
  17774. BAD_FUNC_ARG);
  17775. WC_FREE_VAR(in, NULL);
  17776. WC_FREE_VAR(out, NULL);
  17777. WC_FREE_VAR(plain, NULL);
  17778. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17779. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17780. #endif
  17781. return EXPECT_RESULT();
  17782. } /* END test_wc_RsaSSL_SignVerify */
  17783. /*
  17784. * Testing wc_RsaEncryptSize()
  17785. */
  17786. static int test_wc_RsaEncryptSize(void)
  17787. {
  17788. EXPECT_DECLS;
  17789. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17790. RsaKey key;
  17791. WC_RNG rng;
  17792. XMEMSET(&key, 0, sizeof(RsaKey));
  17793. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17794. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17795. ExpectIntEQ(wc_InitRng(&rng), 0);
  17796. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17797. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17798. ExpectIntEQ(MAKE_RSA_KEY(&key, 1024, WC_RSA_EXPONENT, &rng), 0);
  17799. ExpectIntEQ(wc_RsaEncryptSize(&key), 128);
  17800. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17801. #endif
  17802. ExpectIntEQ(MAKE_RSA_KEY(&key, 2048, WC_RSA_EXPONENT, &rng), 0);
  17803. ExpectIntEQ(wc_RsaEncryptSize(&key), 256);
  17804. /* Pass in bad arg. */
  17805. ExpectIntEQ(wc_RsaEncryptSize(NULL), BAD_FUNC_ARG);
  17806. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17807. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17808. #endif
  17809. return EXPECT_RESULT();
  17810. } /* END test_wc_RsaEncryptSize*/
  17811. /*
  17812. * Testing wc_RsaFlattenPublicKey()
  17813. */
  17814. static int test_wc_RsaFlattenPublicKey(void)
  17815. {
  17816. EXPECT_DECLS;
  17817. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  17818. RsaKey key;
  17819. WC_RNG rng;
  17820. byte e[256];
  17821. byte n[256];
  17822. word32 eSz = sizeof(e);
  17823. word32 nSz = sizeof(n);
  17824. #if (!defined(WOLFSSL_SP_MATH) || defined(WOLFSSL_SP_MATH_ALL)) && \
  17825. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 4))
  17826. int bits = 1024;
  17827. #else
  17828. int bits = 2048;
  17829. #endif
  17830. XMEMSET(&key, 0, sizeof(RsaKey));
  17831. XMEMSET(&rng, 0, sizeof(WC_RNG));
  17832. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  17833. ExpectIntEQ(wc_InitRng(&rng), 0);
  17834. ExpectIntEQ(MAKE_RSA_KEY(&key, bits, WC_RSA_EXPONENT, &rng), 0);
  17835. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, &nSz), 0);
  17836. /* Pass bad args. */
  17837. ExpectIntEQ(wc_RsaFlattenPublicKey(NULL, e, &eSz, n, &nSz),
  17838. BAD_FUNC_ARG);
  17839. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, NULL, &eSz, n, &nSz),
  17840. BAD_FUNC_ARG);
  17841. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, NULL, n, &nSz),
  17842. BAD_FUNC_ARG);
  17843. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, NULL, &nSz),
  17844. BAD_FUNC_ARG);
  17845. ExpectIntEQ(wc_RsaFlattenPublicKey(&key, e, &eSz, n, NULL),
  17846. BAD_FUNC_ARG);
  17847. DoExpectIntEQ(wc_FreeRsaKey(&key), 0);
  17848. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  17849. #endif
  17850. return EXPECT_RESULT();
  17851. } /* END test_wc_RsaFlattenPublicKey */
  17852. /*
  17853. * unit test for wc_AesCcmSetKey
  17854. */
  17855. static int test_wc_AesCcmSetKey(void)
  17856. {
  17857. EXPECT_DECLS;
  17858. #ifdef HAVE_AESCCM
  17859. Aes aes;
  17860. const byte key16[] = {
  17861. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17862. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17863. };
  17864. const byte key24[] = {
  17865. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17866. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17867. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37
  17868. };
  17869. const byte key32[] = {
  17870. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17871. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66,
  17872. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  17873. 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66
  17874. };
  17875. XMEMSET(&aes, 0, sizeof(Aes));
  17876. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  17877. #ifdef WOLFSSL_AES_128
  17878. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  17879. #endif
  17880. #ifdef WOLFSSL_AES_192
  17881. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24)), 0);
  17882. #endif
  17883. #ifdef WOLFSSL_AES_256
  17884. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32)), 0);
  17885. #endif
  17886. /* Test bad args. */
  17887. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16) - 1), BAD_FUNC_ARG);
  17888. ExpectIntEQ(wc_AesCcmSetKey(&aes, key24, sizeof(key24) - 1), BAD_FUNC_ARG);
  17889. ExpectIntEQ(wc_AesCcmSetKey(&aes, key32, sizeof(key32) - 1), BAD_FUNC_ARG);
  17890. wc_AesFree(&aes);
  17891. #endif
  17892. return EXPECT_RESULT();
  17893. } /* END test_wc_AesCcmSetKey */
  17894. /*
  17895. * Unit test function for wc_AesCcmEncrypt and wc_AesCcmDecrypt
  17896. */
  17897. static int test_wc_AesCcmEncryptDecrypt(void)
  17898. {
  17899. EXPECT_DECLS;
  17900. #if defined(HAVE_AESCCM) && defined(WOLFSSL_AES_128)
  17901. Aes aes;
  17902. const byte key16[] = {
  17903. 0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
  17904. 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf
  17905. };
  17906. /* plaintext */
  17907. const byte plainT[] = {
  17908. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
  17909. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  17910. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e
  17911. };
  17912. /* nonce */
  17913. const byte iv[] = {
  17914. 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0xa0,
  17915. 0xa1, 0xa2, 0xa3, 0xa4, 0xa5
  17916. };
  17917. const byte c[] = { /* cipher text. */
  17918. 0x58, 0x8c, 0x97, 0x9a, 0x61, 0xc6, 0x63, 0xd2,
  17919. 0xf0, 0x66, 0xd0, 0xc2, 0xc0, 0xf9, 0x89, 0x80,
  17920. 0x6d, 0x5f, 0x6b, 0x61, 0xda, 0xc3, 0x84
  17921. };
  17922. const byte t[] = { /* Auth tag */
  17923. 0x17, 0xe8, 0xd1, 0x2c, 0xfd, 0xf9, 0x26, 0xe0
  17924. };
  17925. const byte authIn[] = {
  17926. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07
  17927. };
  17928. byte cipherOut[sizeof(plainT)];
  17929. byte authTag[sizeof(t)];
  17930. #ifdef HAVE_AES_DECRYPT
  17931. byte plainOut[sizeof(cipherOut)];
  17932. #endif
  17933. XMEMSET(&aes, 0, sizeof(Aes));
  17934. ExpectIntEQ(wc_AesInit(&aes, NULL, INVALID_DEVID), 0);
  17935. ExpectIntEQ(wc_AesCcmSetKey(&aes, key16, sizeof(key16)), 0);
  17936. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17937. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)), 0);
  17938. ExpectIntEQ(XMEMCMP(cipherOut, c, sizeof(c)), 0);
  17939. ExpectIntEQ(XMEMCMP(t, authTag, sizeof(t)), 0);
  17940. #ifdef HAVE_AES_DECRYPT
  17941. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17942. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)), 0);
  17943. ExpectIntEQ(XMEMCMP(plainOut, plainT, sizeof(plainT)), 0);
  17944. #endif
  17945. /* Pass in bad args. Encrypt*/
  17946. ExpectIntEQ(wc_AesCcmEncrypt(NULL, cipherOut, plainT, sizeof(cipherOut),
  17947. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17948. BAD_FUNC_ARG);
  17949. ExpectIntEQ(wc_AesCcmEncrypt(&aes, NULL, plainT, sizeof(cipherOut),
  17950. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17951. BAD_FUNC_ARG);
  17952. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, NULL, sizeof(cipherOut),
  17953. iv, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17954. BAD_FUNC_ARG);
  17955. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17956. NULL, sizeof(iv), authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17957. BAD_FUNC_ARG);
  17958. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17959. iv, sizeof(iv), NULL, sizeof(authTag), authIn , sizeof(authIn)),
  17960. BAD_FUNC_ARG);
  17961. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17962. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17963. BAD_FUNC_ARG);
  17964. ExpectIntEQ(wc_AesCcmEncrypt(&aes, cipherOut, plainT, sizeof(cipherOut),
  17965. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn , sizeof(authIn)),
  17966. BAD_FUNC_ARG);
  17967. #ifdef HAVE_AES_DECRYPT
  17968. /* Pass in bad args. Decrypt*/
  17969. ExpectIntEQ(wc_AesCcmDecrypt(NULL, plainOut, cipherOut, sizeof(plainOut),
  17970. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17971. BAD_FUNC_ARG);
  17972. ExpectIntEQ(wc_AesCcmDecrypt(&aes, NULL, cipherOut, sizeof(plainOut),
  17973. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17974. BAD_FUNC_ARG);
  17975. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, NULL, sizeof(plainOut),
  17976. iv, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17977. BAD_FUNC_ARG);
  17978. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17979. NULL, sizeof(iv), authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17980. BAD_FUNC_ARG);
  17981. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17982. iv, sizeof(iv), NULL, sizeof(authTag), authIn, sizeof(authIn)),
  17983. BAD_FUNC_ARG);
  17984. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17985. iv, sizeof(iv) + 1, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17986. BAD_FUNC_ARG);
  17987. ExpectIntEQ(wc_AesCcmDecrypt(&aes, plainOut, cipherOut, sizeof(plainOut),
  17988. iv, sizeof(iv) - 7, authTag, sizeof(authTag), authIn, sizeof(authIn)),
  17989. BAD_FUNC_ARG);
  17990. #endif
  17991. wc_AesFree(&aes);
  17992. #endif /* HAVE_AESCCM */
  17993. return EXPECT_RESULT();
  17994. } /* END test_wc_AesCcmEncryptDecrypt */
  17995. #if defined(WOLFSSL_AES_EAX) && \
  17996. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  17997. /*
  17998. * Testing test_wc_AesEaxVectors()
  17999. */
  18000. static int test_wc_AesEaxVectors(void)
  18001. {
  18002. EXPECT_DECLS;
  18003. typedef struct {
  18004. byte key[AES_256_KEY_SIZE];
  18005. int key_length;
  18006. byte iv[AES_BLOCK_SIZE];
  18007. int iv_length;
  18008. byte aad[AES_BLOCK_SIZE * 2];
  18009. int aad_length;
  18010. byte msg[AES_BLOCK_SIZE * 5];
  18011. int msg_length;
  18012. byte ct[AES_BLOCK_SIZE * 5];
  18013. int ct_length;
  18014. byte tag[AES_BLOCK_SIZE];
  18015. int tag_length;
  18016. int valid;
  18017. } AadVector;
  18018. /* Test vectors obtained from Google wycheproof project
  18019. * https://github.com/google/wycheproof
  18020. * from testvectors/aes_eax_test.json
  18021. */
  18022. const AadVector vectors[] = {
  18023. {
  18024. /* key, key length */
  18025. {0x23, 0x39, 0x52, 0xde, 0xe4, 0xd5, 0xed, 0x5f,
  18026. 0x9b, 0x9c, 0x6d, 0x6f, 0xf8, 0x0f, 0xf4, 0x78}, 16,
  18027. /* iv, iv length */
  18028. {0x62, 0xec, 0x67, 0xf9, 0xc3, 0xa4, 0xa4, 0x07,
  18029. 0xfc, 0xb2, 0xa8, 0xc4, 0x90, 0x31, 0xa8, 0xb3}, 16,
  18030. /* aad, aad length */
  18031. {0x6b, 0xfb, 0x91, 0x4f, 0xd0, 0x7e, 0xae, 0x6b}, 8,
  18032. /* msg, msg length */
  18033. {0x00}, 0,
  18034. /* ct, ct length */
  18035. {0x00}, 0,
  18036. /* tag, tag length */
  18037. {0xe0, 0x37, 0x83, 0x0e, 0x83, 0x89, 0xf2, 0x7b,
  18038. 0x02, 0x5a, 0x2d, 0x65, 0x27, 0xe7, 0x9d, 0x01}, 16,
  18039. /* valid */
  18040. 1,
  18041. },
  18042. {
  18043. /* key, key length */
  18044. {0x91, 0x94, 0x5d, 0x3f, 0x4d, 0xcb, 0xee, 0x0b,
  18045. 0xf4, 0x5e, 0xf5, 0x22, 0x55, 0xf0, 0x95, 0xa4}, 16,
  18046. /* iv, iv length */
  18047. {0xbe, 0xca, 0xf0, 0x43, 0xb0, 0xa2, 0x3d, 0x84,
  18048. 0x31, 0x94, 0xba, 0x97, 0x2c, 0x66, 0xde, 0xbd}, 16,
  18049. /* aad, aad length */
  18050. {0xfa, 0x3b, 0xfd, 0x48, 0x06, 0xeb, 0x53, 0xfa}, 8,
  18051. /* msg, msg length */
  18052. {0xf7, 0xfb}, 2,
  18053. /* ct, ct length */
  18054. {0x19, 0xdd}, 2,
  18055. /* tag, tag length */
  18056. {0x5c, 0x4c, 0x93, 0x31, 0x04, 0x9d, 0x0b, 0xda,
  18057. 0xb0, 0x27, 0x74, 0x08, 0xf6, 0x79, 0x67, 0xe5}, 16,
  18058. /* valid */
  18059. 1,
  18060. },
  18061. {
  18062. /* key, key length */
  18063. {0x01, 0xf7, 0x4a, 0xd6, 0x40, 0x77, 0xf2, 0xe7,
  18064. 0x04, 0xc0, 0xf6, 0x0a, 0xda, 0x3d, 0xd5, 0x23}, 16,
  18065. /* iv, iv length */
  18066. {0x70, 0xc3, 0xdb, 0x4f, 0x0d, 0x26, 0x36, 0x84,
  18067. 0x00, 0xa1, 0x0e, 0xd0, 0x5d, 0x2b, 0xff, 0x5e}, 16,
  18068. /* aad, aad length */
  18069. {0x23, 0x4a, 0x34, 0x63, 0xc1, 0x26, 0x4a, 0xc6}, 8,
  18070. /* msg, msg length */
  18071. {0x1a, 0x47, 0xcb, 0x49, 0x33}, 5,
  18072. /* ct, ct length */
  18073. {0xd8, 0x51, 0xd5, 0xba, 0xe0}, 5,
  18074. /* tag, tag length */
  18075. {0x3a, 0x59, 0xf2, 0x38, 0xa2, 0x3e, 0x39, 0x19,
  18076. 0x9d, 0xc9, 0x26, 0x66, 0x26, 0xc4, 0x0f, 0x80}, 16,
  18077. /* valid */
  18078. 1,
  18079. },
  18080. {
  18081. /* key, key length */
  18082. {0xd0, 0x7c, 0xf6, 0xcb, 0xb7, 0xf3, 0x13, 0xbd,
  18083. 0xde, 0x66, 0xb7, 0x27, 0xaf, 0xd3, 0xc5, 0xe8}, 16,
  18084. /* iv, iv length */
  18085. {0x84, 0x08, 0xdf, 0xff, 0x3c, 0x1a, 0x2b, 0x12,
  18086. 0x92, 0xdc, 0x19, 0x9e, 0x46, 0xb7, 0xd6, 0x17}, 16,
  18087. /* aad, aad length */
  18088. {0x33, 0xcc, 0xe2, 0xea, 0xbf, 0xf5, 0xa7, 0x9d}, 8,
  18089. /* msg, msg length */
  18090. {0x48, 0x1c, 0x9e, 0x39, 0xb1}, 5,
  18091. /* ct, ct length */
  18092. {0x63, 0x2a, 0x9d, 0x13, 0x1a}, 5,
  18093. /* tag, tag length */
  18094. {0xd4, 0xc1, 0x68, 0xa4, 0x22, 0x5d, 0x8e, 0x1f,
  18095. 0xf7, 0x55, 0x93, 0x99, 0x74, 0xa7, 0xbe, 0xde}, 16,
  18096. /* valid */
  18097. 1,
  18098. },
  18099. {
  18100. /* key, key length */
  18101. {0x35, 0xb6, 0xd0, 0x58, 0x00, 0x05, 0xbb, 0xc1,
  18102. 0x2b, 0x05, 0x87, 0x12, 0x45, 0x57, 0xd2, 0xc2}, 16,
  18103. /* iv, iv length */
  18104. {0xfd, 0xb6, 0xb0, 0x66, 0x76, 0xee, 0xdc, 0x5c,
  18105. 0x61, 0xd7, 0x42, 0x76, 0xe1, 0xf8, 0xe8, 0x16}, 16,
  18106. /* aad, aad length */
  18107. {0xae, 0xb9, 0x6e, 0xae, 0xbe, 0x29, 0x70, 0xe9}, 8,
  18108. /* msg, msg length */
  18109. {0x40, 0xd0, 0xc0, 0x7d, 0xa5, 0xe4}, 6,
  18110. /* ct, ct length */
  18111. {0x07, 0x1d, 0xfe, 0x16, 0xc6, 0x75}, 6,
  18112. /* tag, tag length */
  18113. {0xcb, 0x06, 0x77, 0xe5, 0x36, 0xf7, 0x3a, 0xfe,
  18114. 0x6a, 0x14, 0xb7, 0x4e, 0xe4, 0x98, 0x44, 0xdd}, 16,
  18115. /* valid */
  18116. 1,
  18117. },
  18118. {
  18119. /* key, key length */
  18120. {0xbd, 0x8e, 0x6e, 0x11, 0x47, 0x5e, 0x60, 0xb2,
  18121. 0x68, 0x78, 0x4c, 0x38, 0xc6, 0x2f, 0xeb, 0x22}, 16,
  18122. /* iv, iv length */
  18123. {0x6e, 0xac, 0x5c, 0x93, 0x07, 0x2d, 0x8e, 0x85,
  18124. 0x13, 0xf7, 0x50, 0x93, 0x5e, 0x46, 0xda, 0x1b}, 16,
  18125. /* aad, aad length */
  18126. {0xd4, 0x48, 0x2d, 0x1c, 0xa7, 0x8d, 0xce, 0x0f}, 8,
  18127. /* msg, msg length */
  18128. {0x4d, 0xe3, 0xb3, 0x5c, 0x3f, 0xc0, 0x39, 0x24,
  18129. 0x5b, 0xd1, 0xfb, 0x7d}, 12,
  18130. /* ct, ct length */
  18131. {0x83, 0x5b, 0xb4, 0xf1, 0x5d, 0x74, 0x3e, 0x35,
  18132. 0x0e, 0x72, 0x84, 0x14}, 12,
  18133. /* tag, tag length */
  18134. {0xab, 0xb8, 0x64, 0x4f, 0xd6, 0xcc, 0xb8, 0x69,
  18135. 0x47, 0xc5, 0xe1, 0x05, 0x90, 0x21, 0x0a, 0x4f}, 16,
  18136. /* valid */
  18137. 1,
  18138. },
  18139. {
  18140. /* key, key length */
  18141. {0x7c, 0x77, 0xd6, 0xe8, 0x13, 0xbe, 0xd5, 0xac,
  18142. 0x98, 0xba, 0xa4, 0x17, 0x47, 0x7a, 0x2e, 0x7d}, 16,
  18143. /* iv, iv length */
  18144. {0x1a, 0x8c, 0x98, 0xdc, 0xd7, 0x3d, 0x38, 0x39,
  18145. 0x3b, 0x2b, 0xf1, 0x56, 0x9d, 0xee, 0xfc, 0x19}, 16,
  18146. /* aad, aad length */
  18147. {0x65, 0xd2, 0x01, 0x79, 0x90, 0xd6, 0x25, 0x28}, 8,
  18148. /* msg, msg length */
  18149. {0x8b, 0x0a, 0x79, 0x30, 0x6c, 0x9c, 0xe7, 0xed,
  18150. 0x99, 0xda, 0xe4, 0xf8, 0x7f, 0x8d, 0xd6, 0x16,
  18151. 0x36}, 17,
  18152. /* ct, ct length */
  18153. {0x02, 0x08, 0x3e, 0x39, 0x79, 0xda, 0x01, 0x48,
  18154. 0x12, 0xf5, 0x9f, 0x11, 0xd5, 0x26, 0x30, 0xda,
  18155. 0x30}, 17,
  18156. /* tag, tag length */
  18157. {0x13, 0x73, 0x27, 0xd1, 0x06, 0x49, 0xb0, 0xaa,
  18158. 0x6e, 0x1c, 0x18, 0x1d, 0xb6, 0x17, 0xd7, 0xf2}, 16,
  18159. /* valid */
  18160. 1,
  18161. },
  18162. {
  18163. /* key, key length */
  18164. {0x5f, 0xff, 0x20, 0xca, 0xfa, 0xb1, 0x19, 0xca,
  18165. 0x2f, 0xc7, 0x35, 0x49, 0xe2, 0x0f, 0x5b, 0x0d}, 16,
  18166. /* iv, iv length */
  18167. {0xdd, 0xe5, 0x9b, 0x97, 0xd7, 0x22, 0x15, 0x6d,
  18168. 0x4d, 0x9a, 0xff, 0x2b, 0xc7, 0x55, 0x98, 0x26}, 16,
  18169. /* aad, aad length */
  18170. {0x54, 0xb9, 0xf0, 0x4e, 0x6a, 0x09, 0x18, 0x9a}, 8,
  18171. /* msg, msg length */
  18172. {0x1b, 0xda, 0x12, 0x2b, 0xce, 0x8a, 0x8d, 0xba,
  18173. 0xf1, 0x87, 0x7d, 0x96, 0x2b, 0x85, 0x92, 0xdd,
  18174. 0x2d, 0x56}, 18,
  18175. /* ct, ct length */
  18176. {0x2e, 0xc4, 0x7b, 0x2c, 0x49, 0x54, 0xa4, 0x89,
  18177. 0xaf, 0xc7, 0xba, 0x48, 0x97, 0xed, 0xcd, 0xae,
  18178. 0x8c, 0xc3}, 18,
  18179. /* tag, tag length */
  18180. {0x3b, 0x60, 0x45, 0x05, 0x99, 0xbd, 0x02, 0xc9,
  18181. 0x63, 0x82, 0x90, 0x2a, 0xef, 0x7f, 0x83, 0x2a}, 16,
  18182. /* valid */
  18183. 1,
  18184. },
  18185. {
  18186. /* key, key length */
  18187. {0xa4, 0xa4, 0x78, 0x2b, 0xcf, 0xfd, 0x3e, 0xc5,
  18188. 0xe7, 0xef, 0x6d, 0x8c, 0x34, 0xa5, 0x61, 0x23}, 16,
  18189. /* iv, iv length */
  18190. {0xb7, 0x81, 0xfc, 0xf2, 0xf7, 0x5f, 0xa5, 0xa8,
  18191. 0xde, 0x97, 0xa9, 0xca, 0x48, 0xe5, 0x22, 0xec}, 16,
  18192. /* aad, aad length */
  18193. {0x89, 0x9a, 0x17, 0x58, 0x97, 0x56, 0x1d, 0x7e}, 8,
  18194. /* msg, msg length */
  18195. {0x6c, 0xf3, 0x67, 0x20, 0x87, 0x2b, 0x85, 0x13,
  18196. 0xf6, 0xea, 0xb1, 0xa8, 0xa4, 0x44, 0x38, 0xd5,
  18197. 0xef, 0x11}, 18,
  18198. /* ct, ct length */
  18199. {0x0d, 0xe1, 0x8f, 0xd0, 0xfd, 0xd9, 0x1e, 0x7a,
  18200. 0xf1, 0x9f, 0x1d, 0x8e, 0xe8, 0x73, 0x39, 0x38,
  18201. 0xb1, 0xe8}, 18,
  18202. /* tag, tag length */
  18203. {0xe7, 0xf6, 0xd2, 0x23, 0x16, 0x18, 0x10, 0x2f,
  18204. 0xdb, 0x7f, 0xe5, 0x5f, 0xf1, 0x99, 0x17, 0x00}, 16,
  18205. /* valid */
  18206. 1,
  18207. },
  18208. {
  18209. /* key, key length */
  18210. {0x83, 0x95, 0xfc, 0xf1, 0xe9, 0x5b, 0xeb, 0xd6,
  18211. 0x97, 0xbd, 0x01, 0x0b, 0xc7, 0x66, 0xaa, 0xc3}, 16,
  18212. /* iv, iv length */
  18213. {0x22, 0xe7, 0xad, 0xd9, 0x3c, 0xfc, 0x63, 0x93,
  18214. 0xc5, 0x7e, 0xc0, 0xb3, 0xc1, 0x7d, 0x6b, 0x44}, 16,
  18215. /* aad, aad length */
  18216. {0x12, 0x67, 0x35, 0xfc, 0xc3, 0x20, 0xd2, 0x5a}, 8,
  18217. /* msg, msg length */
  18218. {0xca, 0x40, 0xd7, 0x44, 0x6e, 0x54, 0x5f, 0xfa,
  18219. 0xed, 0x3b, 0xd1, 0x2a, 0x74, 0x0a, 0x65, 0x9f,
  18220. 0xfb, 0xbb, 0x3c, 0xea, 0xb7}, 21,
  18221. /* ct, ct length */
  18222. {0xcb, 0x89, 0x20, 0xf8, 0x7a, 0x6c, 0x75, 0xcf,
  18223. 0xf3, 0x96, 0x27, 0xb5, 0x6e, 0x3e, 0xd1, 0x97,
  18224. 0xc5, 0x52, 0xd2, 0x95, 0xa7}, 21,
  18225. /* tag, tag length */
  18226. {0xcf, 0xc4, 0x6a, 0xfc, 0x25, 0x3b, 0x46, 0x52,
  18227. 0xb1, 0xaf, 0x37, 0x95, 0xb1, 0x24, 0xab, 0x6e}, 16,
  18228. /* valid */
  18229. 1,
  18230. },
  18231. {
  18232. /* key, key length */
  18233. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18234. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18235. /* iv, iv length */
  18236. {0x3c, 0x8c, 0xc2, 0x97, 0x0a, 0x00, 0x8f, 0x75,
  18237. 0xcc, 0x5b, 0xea, 0xe2, 0x84, 0x72, 0x58, 0xc2}, 16,
  18238. /* aad, aad length */
  18239. {0x00}, 0,
  18240. /* msg, msg length */
  18241. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18242. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18243. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18244. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18245. /* ct, ct length */
  18246. {0x3c, 0x44, 0x1f, 0x32, 0xce, 0x07, 0x82, 0x23,
  18247. 0x64, 0xd7, 0xa2, 0x99, 0x0e, 0x50, 0xbb, 0x13,
  18248. 0xd7, 0xb0, 0x2a, 0x26, 0x96, 0x9e, 0x4a, 0x93,
  18249. 0x7e, 0x5e, 0x90, 0x73, 0xb0, 0xd9, 0xc9, 0x68}, 32,
  18250. /* tag, tag length */
  18251. {0xdb, 0x90, 0xbd, 0xb3, 0xda, 0x3d, 0x00, 0xaf,
  18252. 0xd0, 0xfc, 0x6a, 0x83, 0x55, 0x1d, 0xa9, 0x5e}, 16,
  18253. /* valid */
  18254. 1,
  18255. },
  18256. {
  18257. /* key, key length */
  18258. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18259. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18260. /* iv, iv length */
  18261. {0xae, 0xf0, 0x3d, 0x00, 0x59, 0x84, 0x94, 0xe9,
  18262. 0xfb, 0x03, 0xcd, 0x7d, 0x8b, 0x59, 0x08, 0x66}, 16,
  18263. /* aad, aad length */
  18264. {0x00}, 0,
  18265. /* msg, msg length */
  18266. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18267. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18268. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18269. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18270. /* ct, ct length */
  18271. {0xd1, 0x9a, 0xc5, 0x98, 0x49, 0x02, 0x6a, 0x91,
  18272. 0xaa, 0x1b, 0x9a, 0xec, 0x29, 0xb1, 0x1a, 0x20,
  18273. 0x2a, 0x4d, 0x73, 0x9f, 0xd8, 0x6c, 0x28, 0xe3,
  18274. 0xae, 0x3d, 0x58, 0x8e, 0xa2, 0x1d, 0x70, 0xc6}, 32,
  18275. /* tag, tag length */
  18276. {0xc3, 0x0f, 0x6c, 0xd9, 0x20, 0x20, 0x74, 0xed,
  18277. 0x6e, 0x2a, 0x2a, 0x36, 0x0e, 0xac, 0x8c, 0x47}, 16,
  18278. /* valid */
  18279. 1,
  18280. },
  18281. {
  18282. /* key, key length */
  18283. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18284. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18285. /* iv, iv length */
  18286. {0x55, 0xd1, 0x25, 0x11, 0xc6, 0x96, 0xa8, 0x0d,
  18287. 0x05, 0x14, 0xd1, 0xff, 0xba, 0x49, 0xca, 0xda}, 16,
  18288. /* aad, aad length */
  18289. {0x00}, 0,
  18290. /* msg, msg length */
  18291. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18292. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18293. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18294. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18295. /* ct, ct length */
  18296. {0x21, 0x08, 0x55, 0x8a, 0xc4, 0xb2, 0xc2, 0xd5,
  18297. 0xcc, 0x66, 0xce, 0xa5, 0x1d, 0x62, 0x10, 0xe0,
  18298. 0x46, 0x17, 0x7a, 0x67, 0x63, 0x1c, 0xd2, 0xdd,
  18299. 0x8f, 0x09, 0x46, 0x97, 0x33, 0xac, 0xb5, 0x17}, 32,
  18300. /* tag, tag length */
  18301. {0xfc, 0x35, 0x5e, 0x87, 0xa2, 0x67, 0xbe, 0x3a,
  18302. 0xe3, 0xe4, 0x4c, 0x0b, 0xf3, 0xf9, 0x9b, 0x2b}, 16,
  18303. /* valid */
  18304. 1,
  18305. },
  18306. {
  18307. /* key, key length */
  18308. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18309. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18310. /* iv, iv length */
  18311. {0x79, 0x42, 0x2d, 0xdd, 0x91, 0xc4, 0xee, 0xe2,
  18312. 0xde, 0xae, 0xf1, 0xf9, 0x68, 0x30, 0x53, 0x04}, 16,
  18313. /* aad, aad length */
  18314. {0x00}, 0,
  18315. /* msg, msg length */
  18316. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18317. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18318. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18319. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18320. /* ct, ct length */
  18321. {0x4d, 0x2c, 0x15, 0x24, 0xca, 0x4b, 0xaa, 0x4e,
  18322. 0xef, 0xcc, 0xe6, 0xb9, 0x1b, 0x22, 0x7e, 0xe8,
  18323. 0x3a, 0xba, 0xff, 0x81, 0x05, 0xdc, 0xaf, 0xa2,
  18324. 0xab, 0x19, 0x1f, 0x5d, 0xf2, 0x57, 0x50, 0x35}, 32,
  18325. /* tag, tag length */
  18326. {0xe2, 0xc8, 0x65, 0xce, 0x2d, 0x7a, 0xbd, 0xac,
  18327. 0x02, 0x4c, 0x6f, 0x99, 0x1a, 0x84, 0x83, 0x90}, 16,
  18328. /* valid */
  18329. 1,
  18330. },
  18331. {
  18332. /* key, key length */
  18333. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18334. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18335. /* iv, iv length */
  18336. {0x0a, 0xf5, 0xaa, 0x7a, 0x76, 0x76, 0xe2, 0x83,
  18337. 0x06, 0x30, 0x6b, 0xcd, 0x9b, 0xf2, 0x00, 0x3a}, 16,
  18338. /* aad, aad length */
  18339. {0x00}, 0,
  18340. /* msg, msg length */
  18341. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18342. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18343. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18344. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18345. /* ct, ct length */
  18346. {0x8e, 0xb0, 0x1e, 0x62, 0x18, 0x5d, 0x78, 0x2e,
  18347. 0xb9, 0x28, 0x7a, 0x34, 0x1a, 0x68, 0x62, 0xac,
  18348. 0x52, 0x57, 0xd6, 0xf9, 0xad, 0xc9, 0x9e, 0xe0,
  18349. 0xa2, 0x4d, 0x9c, 0x22, 0xb3, 0xe9, 0xb3, 0x8a}, 32,
  18350. /* tag, tag length */
  18351. {0x39, 0xc3, 0x39, 0xbc, 0x8a, 0x74, 0xc7, 0x5e,
  18352. 0x2c, 0x65, 0xc6, 0x11, 0x95, 0x44, 0xd6, 0x1e}, 16,
  18353. /* valid */
  18354. 1,
  18355. },
  18356. {
  18357. /* key, key length */
  18358. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18359. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18360. /* iv, iv length */
  18361. {0xaf, 0x5a, 0x03, 0xae, 0x7e, 0xdd, 0x73, 0x47,
  18362. 0x1b, 0xdc, 0xdf, 0xac, 0x5e, 0x19, 0x4a, 0x60}, 16,
  18363. /* aad, aad length */
  18364. {0x00}, 0,
  18365. /* msg, msg length */
  18366. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18367. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18368. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18369. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}, 32,
  18370. /* ct, ct length */
  18371. {0x94, 0xc5, 0xd2, 0xac, 0xa6, 0xdb, 0xbc, 0xe8,
  18372. 0xc2, 0x45, 0x13, 0xa2, 0x5e, 0x09, 0x5c, 0x0e,
  18373. 0x54, 0xa9, 0x42, 0x86, 0x0d, 0x32, 0x7a, 0x22,
  18374. 0x2a, 0x81, 0x5c, 0xc7, 0x13, 0xb1, 0x63, 0xb4}, 32,
  18375. /* tag, tag length */
  18376. {0xf5, 0x0b, 0x30, 0x30, 0x4e, 0x45, 0xc9, 0xd4,
  18377. 0x11, 0xe8, 0xdf, 0x45, 0x08, 0xa9, 0x86, 0x12}, 16,
  18378. /* valid */
  18379. 1,
  18380. },
  18381. {
  18382. /* key, key length */
  18383. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18384. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18385. /* iv, iv length */
  18386. {0xb3, 0x70, 0x87, 0x68, 0x0f, 0x0e, 0xdd, 0x5a,
  18387. 0x52, 0x22, 0x8b, 0x8c, 0x7a, 0xae, 0xa6, 0x64}, 16,
  18388. /* aad, aad length */
  18389. {0x00}, 0,
  18390. /* msg, msg length */
  18391. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18392. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18393. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18394. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18395. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18396. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18397. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18398. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33}, 64,
  18399. /* ct, ct length */
  18400. {0x3b, 0xb6, 0x17, 0x3e, 0x37, 0x72, 0xd4, 0xb6,
  18401. 0x2e, 0xef, 0x37, 0xf9, 0xef, 0x07, 0x81, 0xf3,
  18402. 0x60, 0xb6, 0xc7, 0x4b, 0xe3, 0xbf, 0x6b, 0x37,
  18403. 0x10, 0x67, 0xbc, 0x1b, 0x09, 0x0d, 0x9d, 0x66,
  18404. 0x22, 0xa1, 0xfb, 0xec, 0x6a, 0xc4, 0x71, 0xb3,
  18405. 0x34, 0x9c, 0xd4, 0x27, 0x7a, 0x10, 0x1d, 0x40,
  18406. 0x89, 0x0f, 0xbf, 0x27, 0xdf, 0xdc, 0xd0, 0xb4,
  18407. 0xe3, 0x78, 0x1f, 0x98, 0x06, 0xda, 0xab, 0xb6}, 64,
  18408. /* tag, tag length */
  18409. {0xa0, 0x49, 0x87, 0x45, 0xe5, 0x99, 0x99, 0xdd,
  18410. 0xc3, 0x2d, 0x5b, 0x14, 0x02, 0x41, 0x12, 0x4e}, 16,
  18411. /* valid */
  18412. 1,
  18413. },
  18414. {
  18415. /* key, key length */
  18416. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18417. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18418. /* iv, iv length */
  18419. {0x4f, 0x80, 0x2d, 0xa6, 0x2a, 0x38, 0x45, 0x55,
  18420. 0xa1, 0x9b, 0xc2, 0xb3, 0x82, 0xeb, 0x25, 0xaf}, 16,
  18421. /* aad, aad length */
  18422. {0x00}, 0,
  18423. /* msg, msg length */
  18424. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18425. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  18426. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18427. 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11,
  18428. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18429. 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22, 0x22,
  18430. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18431. 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x33,
  18432. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44,
  18433. 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44}, 80,
  18434. /* ct, ct length */
  18435. {0xe9, 0xb0, 0xbb, 0x88, 0x57, 0x81, 0x8c, 0xe3,
  18436. 0x20, 0x1c, 0x36, 0x90, 0xd2, 0x1d, 0xaa, 0x7f,
  18437. 0x26, 0x4f, 0xb8, 0xee, 0x93, 0xcc, 0x7a, 0x46,
  18438. 0x74, 0xea, 0x2f, 0xc3, 0x2b, 0xf1, 0x82, 0xfb,
  18439. 0x2a, 0x7e, 0x8a, 0xd5, 0x15, 0x07, 0xad, 0x4f,
  18440. 0x31, 0xce, 0xfc, 0x23, 0x56, 0xfe, 0x79, 0x36,
  18441. 0xa7, 0xf6, 0xe1, 0x9f, 0x95, 0xe8, 0x8f, 0xdb,
  18442. 0xf1, 0x76, 0x20, 0x91, 0x6d, 0x3a, 0x6f, 0x3d,
  18443. 0x01, 0xfc, 0x17, 0xd3, 0x58, 0x67, 0x2f, 0x77,
  18444. 0x7f, 0xd4, 0x09, 0x92, 0x46, 0xe4, 0x36, 0xe1}, 80,
  18445. /* tag, tag length */
  18446. {0x67, 0x91, 0x0b, 0xe7, 0x44, 0xb8, 0x31, 0x5a,
  18447. 0xe0, 0xeb, 0x61, 0x24, 0x59, 0x0c, 0x5d, 0x8b}, 16,
  18448. /* valid */
  18449. 1,
  18450. },
  18451. {
  18452. /* key, key length */
  18453. {0xb6, 0x7b, 0x1a, 0x6e, 0xfd, 0xd4, 0x0d, 0x37,
  18454. 0x08, 0x0f, 0xbe, 0x8f, 0x80, 0x47, 0xae, 0xb9}, 16,
  18455. /* iv, iv length */
  18456. {0xfa, 0x29, 0x4b, 0x12, 0x99, 0x72, 0xf7, 0xfc,
  18457. 0x5b, 0xbd, 0x5b, 0x96, 0xbb, 0xa8, 0x37, 0xc9}, 16,
  18458. /* aad, aad length */
  18459. {0x00}, 0,
  18460. /* msg, msg length */
  18461. {0x00}, 0,
  18462. /* ct, ct length */
  18463. {0x00}, 0,
  18464. /* tag, tag length */
  18465. {0xb1, 0x4b, 0x64, 0xfb, 0x58, 0x98, 0x99, 0x69,
  18466. 0x95, 0x70, 0xcc, 0x91, 0x60, 0xe3, 0x98, 0x96}, 16,
  18467. /* valid */
  18468. 1,
  18469. },
  18470. {
  18471. /* key, key length */
  18472. {0x20, 0x9e, 0x6d, 0xbf, 0x2a, 0xd2, 0x6a, 0x10,
  18473. 0x54, 0x45, 0xfc, 0x02, 0x07, 0xcd, 0x9e, 0x9a}, 16,
  18474. /* iv, iv length */
  18475. {0x94, 0x77, 0x84, 0x9d, 0x6c, 0xcd, 0xfc, 0xa1,
  18476. 0x12, 0xd9, 0x2e, 0x53, 0xfa, 0xe4, 0xa7, 0xca}, 16,
  18477. /* aad, aad length */
  18478. {0x00}, 0,
  18479. /* msg, msg length */
  18480. {0x01}, 1,
  18481. /* ct, ct length */
  18482. {0x1d}, 1,
  18483. /* tag, tag length */
  18484. {0x52, 0xa5, 0xf6, 0x00, 0xfe, 0x53, 0x38, 0x02,
  18485. 0x6a, 0x7c, 0xb0, 0x9c, 0x11, 0x64, 0x00, 0x82}, 16,
  18486. /* valid */
  18487. 1,
  18488. },
  18489. {
  18490. /* key, key length */
  18491. {0xa5, 0x49, 0x44, 0x2e, 0x35, 0x15, 0x40, 0x32,
  18492. 0xd0, 0x7c, 0x86, 0x66, 0x00, 0x6a, 0xa6, 0xa2}, 16,
  18493. /* iv, iv length */
  18494. {0x51, 0x71, 0x52, 0x45, 0x68, 0xe8, 0x1d, 0x97,
  18495. 0xe8, 0xc4, 0xde, 0x4b, 0xa5, 0x6c, 0x10, 0xa0}, 16,
  18496. /* aad, aad length */
  18497. {0x00}, 0,
  18498. /* msg, msg length */
  18499. {0x11, 0x82, 0xe9, 0x35, 0x96, 0xca, 0xc5, 0x60,
  18500. 0x89, 0x46, 0x40, 0x0b, 0xc7, 0x3f, 0x3a}, 15,
  18501. /* ct, ct length */
  18502. {0xd7, 0xb8, 0xa6, 0xb4, 0x3d, 0x2e, 0x9f, 0x98,
  18503. 0xc2, 0xb4, 0x4c, 0xe5, 0xe3, 0xcf, 0xdb}, 15,
  18504. /* tag, tag length */
  18505. {0x1b, 0xdd, 0x52, 0xfc, 0x98, 0x7d, 0xaf, 0x0e,
  18506. 0xe1, 0x92, 0x34, 0xc9, 0x05, 0xea, 0x64, 0x5f}, 16,
  18507. /* valid */
  18508. 1,
  18509. },
  18510. {
  18511. /* key, key length */
  18512. {0x95, 0x8b, 0xcd, 0xb6, 0x6a, 0x39, 0x52, 0xb5,
  18513. 0x37, 0x01, 0x58, 0x2a, 0x68, 0xa0, 0xe4, 0x74}, 16,
  18514. /* iv, iv length */
  18515. {0x0e, 0x6e, 0xc8, 0x79, 0xb0, 0x2c, 0x6f, 0x51,
  18516. 0x69, 0x76, 0xe3, 0x58, 0x98, 0x42, 0x8d, 0xa7}, 16,
  18517. /* aad, aad length */
  18518. {0x00}, 0,
  18519. /* msg, msg length */
  18520. {0x14, 0x04, 0x15, 0x82, 0x3e, 0xcc, 0x89, 0x32,
  18521. 0xa0, 0x58, 0x38, 0x4b, 0x73, 0x8e, 0xa6, 0xea,
  18522. 0x6d, 0x4d, 0xfe, 0x3b, 0xbe, 0xee}, 22,
  18523. /* ct, ct length */
  18524. {0x73, 0xe5, 0xc6, 0xf0, 0xe7, 0x03, 0xa5, 0x2d,
  18525. 0x02, 0xf7, 0xf7, 0xfa, 0xeb, 0x1b, 0x77, 0xfd,
  18526. 0x4f, 0xd0, 0xcb, 0x42, 0x1e, 0xaf}, 22,
  18527. /* tag, tag length */
  18528. {0x6c, 0x15, 0x4a, 0x85, 0x96, 0x8e, 0xdd, 0x74,
  18529. 0x77, 0x65, 0x75, 0xa4, 0x45, 0x0b, 0xd8, 0x97}, 16,
  18530. /* valid */
  18531. 1,
  18532. },
  18533. {
  18534. /* key, key length */
  18535. {0x96, 0x5b, 0x75, 0x7b, 0xa5, 0x01, 0x8a, 0x8d,
  18536. 0x66, 0xed, 0xc7, 0x8e, 0x0c, 0xee, 0xe8, 0x6b}, 16,
  18537. /* iv, iv length */
  18538. {0x2e, 0x35, 0x90, 0x1a, 0xe7, 0xd4, 0x91, 0xee,
  18539. 0xcc, 0x88, 0x38, 0xfe, 0xdd, 0x63, 0x14, 0x05}, 16,
  18540. /* aad, aad length */
  18541. {0xdf, 0x10, 0xd0, 0xd2, 0x12, 0x24, 0x24, 0x50}, 8,
  18542. /* msg, msg length */
  18543. {0x36, 0xe5, 0x7a, 0x76, 0x39, 0x58, 0xb0, 0x2c,
  18544. 0xea, 0x9d, 0x6a, 0x67, 0x6e, 0xbc, 0xe8, 0x1f}, 16,
  18545. /* ct, ct length */
  18546. {0x93, 0x6b, 0x69, 0xb6, 0xc9, 0x55, 0xad, 0xfd,
  18547. 0x15, 0x53, 0x9b, 0x9b, 0xe4, 0x98, 0x9c, 0xb6}, 16,
  18548. /* tag, tag length */
  18549. {0xee, 0x15, 0xa1, 0x45, 0x4e, 0x88, 0xfa, 0xad,
  18550. 0x8e, 0x48, 0xa8, 0xdf, 0x29, 0x83, 0xb4, 0x25}, 16,
  18551. /* valid */
  18552. 1,
  18553. },
  18554. {
  18555. /* key, key length */
  18556. {0x88, 0xd0, 0x20, 0x33, 0x78, 0x1c, 0x7b, 0x41,
  18557. 0x64, 0x71, 0x1a, 0x05, 0x42, 0x0f, 0x25, 0x6e}, 16,
  18558. /* iv, iv length */
  18559. {0x7f, 0x29, 0x85, 0x29, 0x63, 0x15, 0x50, 0x7a,
  18560. 0xa4, 0xc0, 0xa9, 0x3d, 0x5c, 0x12, 0xbd, 0x77}, 16,
  18561. /* aad, aad length */
  18562. {0x7c, 0x57, 0x1d, 0x2f, 0xbb, 0x5f, 0x62, 0x52,
  18563. 0x3c, 0x0e, 0xb3, 0x38, 0xbe, 0xf9, 0xa9}, 15,
  18564. /* msg, msg length */
  18565. {0xd9, 0x8a, 0xdc, 0x03, 0xd9, 0xd5, 0x82, 0x73,
  18566. 0x2e, 0xb0, 0x7d, 0xf2, 0x3d, 0x7b, 0x9f, 0x74}, 16,
  18567. /* ct, ct length */
  18568. {0x67, 0xca, 0xac, 0x35, 0x44, 0x3a, 0x31, 0x38,
  18569. 0xd2, 0xcb, 0x81, 0x1f, 0x0c, 0xe0, 0x4d, 0xd2}, 16,
  18570. /* tag, tag length */
  18571. {0xb7, 0x96, 0x8e, 0x0b, 0x56, 0x40, 0xe3, 0xb2,
  18572. 0x36, 0x56, 0x96, 0x53, 0x20, 0x8b, 0x9d, 0xeb}, 16,
  18573. /* valid */
  18574. 1,
  18575. },
  18576. {
  18577. /* key, key length */
  18578. {0x51, 0x58, 0x40, 0xcf, 0x67, 0xd2, 0xe4, 0x0e,
  18579. 0xb6, 0x5e, 0x54, 0xa2, 0x4c, 0x72, 0xcb, 0xf2}, 16,
  18580. /* iv, iv length */
  18581. {0xbf, 0x47, 0xaf, 0xdf, 0xd4, 0x92, 0x13, 0x7a,
  18582. 0x24, 0x23, 0x6b, 0xc3, 0x67, 0x97, 0xa8, 0x8e}, 16,
  18583. /* aad, aad length */
  18584. {0x16, 0x84, 0x3c, 0x09, 0x1d, 0x43, 0xb0, 0xa1,
  18585. 0x91, 0xd0, 0xc7, 0x3d, 0x15, 0x60, 0x1b, 0xe9}, 16,
  18586. /* msg, msg length */
  18587. {0xc8, 0x34, 0x58, 0x8c, 0xb6, 0xda, 0xf9, 0xf0,
  18588. 0x6d, 0xd2, 0x35, 0x19, 0xf4, 0xbe, 0x9f, 0x56}, 16,
  18589. /* ct, ct length */
  18590. {0x20, 0x0a, 0xc4, 0x51, 0xfb, 0xeb, 0x0f, 0x61,
  18591. 0x51, 0xd6, 0x15, 0x83, 0xa4, 0x3b, 0x73, 0x43}, 16,
  18592. /* tag, tag length */
  18593. {0x2a, 0xd4, 0x3e, 0x4c, 0xaa, 0x51, 0x98, 0x3a,
  18594. 0x9d, 0x4d, 0x24, 0x48, 0x1b, 0xf4, 0xc8, 0x39}, 16,
  18595. /* valid */
  18596. 1,
  18597. },
  18598. {
  18599. /* key, key length */
  18600. {0x2e, 0x44, 0x92, 0xd4, 0x44, 0xe5, 0xb6, 0xf4,
  18601. 0xce, 0xc8, 0xc2, 0xd3, 0x61, 0x5a, 0xc8, 0x58}, 16,
  18602. /* iv, iv length */
  18603. {0xd0, 0x2b, 0xf0, 0x76, 0x3a, 0x9f, 0xef, 0xbf,
  18604. 0x70, 0xc3, 0x3a, 0xee, 0x1e, 0x9d, 0xa1, 0xd6}, 16,
  18605. /* aad, aad length */
  18606. {0x90, 0x4d, 0x86, 0xf1, 0x33, 0xce, 0xc1, 0x5a,
  18607. 0x0c, 0x3c, 0xaf, 0x14, 0xd7, 0xe0, 0x29, 0xc8,
  18608. 0x2a, 0x07, 0x70, 0x5a, 0x23, 0xf0, 0xd0, 0x80}, 24,
  18609. /* msg, msg length */
  18610. {0x9e, 0x62, 0xd6, 0x51, 0x1b, 0x0b, 0xda, 0x7d,
  18611. 0xd7, 0x74, 0x0b, 0x61, 0x4d, 0x97, 0xba, 0xe0}, 16,
  18612. /* ct, ct length */
  18613. {0x27, 0xc6, 0xe9, 0xa6, 0x53, 0xc5, 0x25, 0x3c,
  18614. 0xa1, 0xc5, 0x67, 0x3f, 0x97, 0xb9, 0xb3, 0x3e}, 16,
  18615. /* tag, tag length */
  18616. {0x2d, 0x58, 0x12, 0x71, 0xe1, 0xfa, 0x9e, 0x36,
  18617. 0x86, 0x13, 0x6c, 0xaa, 0x8f, 0x4d, 0x6c, 0x8e}, 16,
  18618. /* valid */
  18619. 1,
  18620. },
  18621. {
  18622. /* key, key length */
  18623. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18624. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18625. /* iv, iv length */
  18626. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18627. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18628. /* aad, aad length */
  18629. {0x00}, 0,
  18630. /* msg, msg length */
  18631. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18632. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18633. /* ct, ct length */
  18634. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18635. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18636. /* tag, tag length */
  18637. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18638. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18639. /* valid */
  18640. 0,
  18641. },
  18642. {
  18643. /* key, key length */
  18644. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18645. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18646. /* iv, iv length */
  18647. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18648. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18649. /* aad, aad length */
  18650. {0x00}, 0,
  18651. /* msg, msg length */
  18652. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18653. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18654. /* ct, ct length */
  18655. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18656. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18657. /* tag, tag length */
  18658. {0xe4, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18659. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18660. /* valid */
  18661. 0,
  18662. },
  18663. {
  18664. /* key, key length */
  18665. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18666. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18667. /* iv, iv length */
  18668. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18669. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18670. /* aad, aad length */
  18671. {0x00}, 0,
  18672. /* msg, msg length */
  18673. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18674. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18675. /* ct, ct length */
  18676. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18677. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18678. /* tag, tag length */
  18679. {0x66, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18680. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18681. /* valid */
  18682. 0,
  18683. },
  18684. {
  18685. /* key, key length */
  18686. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18687. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18688. /* iv, iv length */
  18689. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18690. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18691. /* aad, aad length */
  18692. {0x00}, 0,
  18693. /* msg, msg length */
  18694. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18695. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18696. /* ct, ct length */
  18697. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18698. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18699. /* tag, tag length */
  18700. {0xe6, 0x0f, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18701. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18702. /* valid */
  18703. 0,
  18704. },
  18705. {
  18706. /* key, key length */
  18707. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18708. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18709. /* iv, iv length */
  18710. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18711. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18712. /* aad, aad length */
  18713. {0x00}, 0,
  18714. /* msg, msg length */
  18715. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18716. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18717. /* ct, ct length */
  18718. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18719. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18720. /* tag, tag length */
  18721. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0xf2,
  18722. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18723. /* valid */
  18724. 0,
  18725. },
  18726. {
  18727. /* key, key length */
  18728. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18729. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18730. /* iv, iv length */
  18731. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18732. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18733. /* aad, aad length */
  18734. {0x00}, 0,
  18735. /* msg, msg length */
  18736. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18737. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18738. /* ct, ct length */
  18739. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18740. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18741. /* tag, tag length */
  18742. {0xe6, 0x0e, 0x7c, 0x50, 0x12, 0xa6, 0xdb, 0xf2,
  18743. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18744. /* valid */
  18745. 0,
  18746. },
  18747. {
  18748. /* key, key length */
  18749. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18750. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18751. /* iv, iv length */
  18752. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18753. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18754. /* aad, aad length */
  18755. {0x00}, 0,
  18756. /* msg, msg length */
  18757. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18758. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18759. /* ct, ct length */
  18760. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18761. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18762. /* tag, tag length */
  18763. {0xe6, 0x0e, 0x7c, 0x50, 0x11, 0xa6, 0xdb, 0xf2,
  18764. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18765. /* valid */
  18766. 0,
  18767. },
  18768. {
  18769. /* key, key length */
  18770. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18771. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18772. /* iv, iv length */
  18773. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18774. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18775. /* aad, aad length */
  18776. {0x00}, 0,
  18777. /* msg, msg length */
  18778. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18779. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18780. /* ct, ct length */
  18781. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18782. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18783. /* tag, tag length */
  18784. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  18785. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18786. /* valid */
  18787. 0,
  18788. },
  18789. {
  18790. /* key, key length */
  18791. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18792. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18793. /* iv, iv length */
  18794. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18795. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18796. /* aad, aad length */
  18797. {0x00}, 0,
  18798. /* msg, msg length */
  18799. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18800. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18801. /* ct, ct length */
  18802. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18803. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18804. /* tag, tag length */
  18805. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18806. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18807. /* valid */
  18808. 0,
  18809. },
  18810. {
  18811. /* key, key length */
  18812. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18813. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18814. /* iv, iv length */
  18815. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18816. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18817. /* aad, aad length */
  18818. {0x00}, 0,
  18819. /* msg, msg length */
  18820. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18821. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18822. /* ct, ct length */
  18823. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18824. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18825. /* tag, tag length */
  18826. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18827. 0xd2, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18828. /* valid */
  18829. 0,
  18830. },
  18831. {
  18832. /* key, key length */
  18833. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18834. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18835. /* iv, iv length */
  18836. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18837. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18838. /* aad, aad length */
  18839. {0x00}, 0,
  18840. /* msg, msg length */
  18841. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18842. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18843. /* ct, ct length */
  18844. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18845. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18846. /* tag, tag length */
  18847. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18848. 0x52, 0xb8, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18849. /* valid */
  18850. 0,
  18851. },
  18852. {
  18853. /* key, key length */
  18854. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18855. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18856. /* iv, iv length */
  18857. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18858. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18859. /* aad, aad length */
  18860. {0x00}, 0,
  18861. /* msg, msg length */
  18862. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18863. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18864. /* ct, ct length */
  18865. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18866. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18867. /* tag, tag length */
  18868. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18869. 0x52, 0x98, 0xb0, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  18870. /* valid */
  18871. 0,
  18872. },
  18873. {
  18874. /* key, key length */
  18875. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18876. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18877. /* iv, iv length */
  18878. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18879. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18880. /* aad, aad length */
  18881. {0x00}, 0,
  18882. /* msg, msg length */
  18883. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18884. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18885. /* ct, ct length */
  18886. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18887. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18888. /* tag, tag length */
  18889. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18890. 0x52, 0x98, 0xb1, 0x92, 0x9a, 0xc3, 0x56, 0xa7}, 16,
  18891. /* valid */
  18892. 0,
  18893. },
  18894. {
  18895. /* key, key length */
  18896. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18897. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18898. /* iv, iv length */
  18899. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18900. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18901. /* aad, aad length */
  18902. {0x00}, 0,
  18903. /* msg, msg length */
  18904. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18905. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18906. /* ct, ct length */
  18907. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18908. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18909. /* tag, tag length */
  18910. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18911. 0x52, 0x98, 0xb1, 0x92, 0x99, 0xc3, 0x56, 0xa7}, 16,
  18912. /* valid */
  18913. 0,
  18914. },
  18915. {
  18916. /* key, key length */
  18917. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18918. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18919. /* iv, iv length */
  18920. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18921. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18922. /* aad, aad length */
  18923. {0x00}, 0,
  18924. /* msg, msg length */
  18925. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18926. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18927. /* ct, ct length */
  18928. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18929. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18930. /* tag, tag length */
  18931. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18932. 0x52, 0x98, 0xb1, 0x92, 0x1b, 0xc3, 0x56, 0xa7}, 16,
  18933. /* valid */
  18934. 0,
  18935. },
  18936. {
  18937. /* key, key length */
  18938. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18939. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18940. /* iv, iv length */
  18941. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18942. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18943. /* aad, aad length */
  18944. {0x00}, 0,
  18945. /* msg, msg length */
  18946. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18947. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18948. /* ct, ct length */
  18949. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18950. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18951. /* tag, tag length */
  18952. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18953. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa6}, 16,
  18954. /* valid */
  18955. 0,
  18956. },
  18957. {
  18958. /* key, key length */
  18959. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18960. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18961. /* iv, iv length */
  18962. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18963. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18964. /* aad, aad length */
  18965. {0x00}, 0,
  18966. /* msg, msg length */
  18967. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18968. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18969. /* ct, ct length */
  18970. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18971. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18972. /* tag, tag length */
  18973. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18974. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa5}, 16,
  18975. /* valid */
  18976. 0,
  18977. },
  18978. {
  18979. /* key, key length */
  18980. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  18981. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  18982. /* iv, iv length */
  18983. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  18984. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  18985. /* aad, aad length */
  18986. {0x00}, 0,
  18987. /* msg, msg length */
  18988. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  18989. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  18990. /* ct, ct length */
  18991. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  18992. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  18993. /* tag, tag length */
  18994. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  18995. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xe7}, 16,
  18996. /* valid */
  18997. 0,
  18998. },
  18999. {
  19000. /* key, key length */
  19001. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19002. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19003. /* iv, iv length */
  19004. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19005. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19006. /* aad, aad length */
  19007. {0x00}, 0,
  19008. /* msg, msg length */
  19009. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19010. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19011. /* ct, ct length */
  19012. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19013. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19014. /* tag, tag length */
  19015. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19016. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  19017. /* valid */
  19018. 0,
  19019. },
  19020. {
  19021. /* key, key length */
  19022. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19023. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19024. /* iv, iv length */
  19025. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19026. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19027. /* aad, aad length */
  19028. {0x00}, 0,
  19029. /* msg, msg length */
  19030. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19031. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19032. /* ct, ct length */
  19033. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19034. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19035. /* tag, tag length */
  19036. {0xe7, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0xf2,
  19037. 0x53, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19038. /* valid */
  19039. 0,
  19040. },
  19041. {
  19042. /* key, key length */
  19043. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19044. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19045. /* iv, iv length */
  19046. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19047. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19048. /* aad, aad length */
  19049. {0x00}, 0,
  19050. /* msg, msg length */
  19051. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19052. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19053. /* ct, ct length */
  19054. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19055. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19056. /* tag, tag length */
  19057. {0xe6, 0x0e, 0x7c, 0xd0, 0x13, 0xa6, 0xdb, 0x72,
  19058. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0xa7}, 16,
  19059. /* valid */
  19060. 0,
  19061. },
  19062. {
  19063. /* key, key length */
  19064. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19065. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19066. /* iv, iv length */
  19067. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19068. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19069. /* aad, aad length */
  19070. {0x00}, 0,
  19071. /* msg, msg length */
  19072. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19073. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19074. /* ct, ct length */
  19075. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19076. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19077. /* tag, tag length */
  19078. {0xe6, 0x0e, 0x7c, 0x50, 0x13, 0xa6, 0xdb, 0x72,
  19079. 0x52, 0x98, 0xb1, 0x92, 0x9b, 0xc3, 0x56, 0x27}, 16,
  19080. /* valid */
  19081. 0,
  19082. },
  19083. {
  19084. /* key, key length */
  19085. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19086. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19087. /* iv, iv length */
  19088. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19089. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19090. /* aad, aad length */
  19091. {0x00}, 0,
  19092. /* msg, msg length */
  19093. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19094. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19095. /* ct, ct length */
  19096. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19097. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19098. /* tag, tag length */
  19099. {0x19, 0xf1, 0x83, 0xaf, 0xec, 0x59, 0x24, 0x0d,
  19100. 0xad, 0x67, 0x4e, 0x6d, 0x64, 0x3c, 0xa9, 0x58}, 16,
  19101. /* valid */
  19102. 0,
  19103. },
  19104. {
  19105. /* key, key length */
  19106. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19107. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19108. /* iv, iv length */
  19109. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19110. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19111. /* aad, aad length */
  19112. {0x00}, 0,
  19113. /* msg, msg length */
  19114. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19115. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19116. /* ct, ct length */
  19117. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19118. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19119. /* tag, tag length */
  19120. {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  19121. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 16,
  19122. /* valid */
  19123. 0,
  19124. },
  19125. {
  19126. /* key, key length */
  19127. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19128. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19129. /* iv, iv length */
  19130. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19131. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19132. /* aad, aad length */
  19133. {0x00}, 0,
  19134. /* msg, msg length */
  19135. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19136. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19137. /* ct, ct length */
  19138. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19139. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19140. /* tag, tag length */
  19141. {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  19142. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 16,
  19143. /* valid */
  19144. 0,
  19145. },
  19146. {
  19147. /* key, key length */
  19148. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19149. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19150. /* iv, iv length */
  19151. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19152. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19153. /* aad, aad length */
  19154. {0x00}, 0,
  19155. /* msg, msg length */
  19156. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19157. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19158. /* ct, ct length */
  19159. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19160. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19161. /* tag, tag length */
  19162. {0x66, 0x8e, 0xfc, 0xd0, 0x93, 0x26, 0x5b, 0x72,
  19163. 0xd2, 0x18, 0x31, 0x12, 0x1b, 0x43, 0xd6, 0x27}, 16,
  19164. /* valid */
  19165. 0,
  19166. },
  19167. {
  19168. /* key, key length */
  19169. {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19170. 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}, 16,
  19171. /* iv, iv length */
  19172. {0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  19173. 0x58, 0x59, 0x5a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f}, 16,
  19174. /* aad, aad length */
  19175. {0x00}, 0,
  19176. /* msg, msg length */
  19177. {0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  19178. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f}, 16,
  19179. /* ct, ct length */
  19180. {0x29, 0xa0, 0x91, 0x4f, 0xec, 0x4b, 0xef, 0x54,
  19181. 0xba, 0xbf, 0x66, 0x13, 0xa9, 0xf9, 0xcd, 0x70}, 16,
  19182. /* tag, tag length */
  19183. {0xe7, 0x0f, 0x7d, 0x51, 0x12, 0xa7, 0xda, 0xf3,
  19184. 0x53, 0x99, 0xb0, 0x93, 0x9a, 0xc2, 0x57, 0xa6}, 16,
  19185. /* valid */
  19186. 0,
  19187. },
  19188. };
  19189. byte ciphertext[sizeof(vectors[0].ct)];
  19190. byte authtag[sizeof(vectors[0].tag)];
  19191. int i;
  19192. int len;
  19193. int ret;
  19194. for (i = 0; i < (int)(sizeof(vectors)/sizeof(vectors[0])); i++) {
  19195. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  19196. len = sizeof(authtag);
  19197. ExpectIntEQ(wc_AesEaxEncryptAuth(vectors[i].key, vectors[i].key_length,
  19198. ciphertext,
  19199. vectors[i].msg, vectors[i].msg_length,
  19200. vectors[i].iv, vectors[i].iv_length,
  19201. authtag, len,
  19202. vectors[i].aad, vectors[i].aad_length),
  19203. 0);
  19204. /* check ciphertext matches vector */
  19205. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].ct, vectors[i].ct_length),
  19206. 0);
  19207. /* check that computed tag matches vector only for vectors marked as valid */
  19208. ret = XMEMCMP(authtag, vectors[i].tag, len);
  19209. if (vectors[i].valid) {
  19210. ExpectIntEQ(ret, 0);
  19211. }
  19212. else {
  19213. ExpectIntNE(ret, 0);
  19214. }
  19215. XMEMSET(ciphertext, 0, sizeof(ciphertext));
  19216. /* Decrypt, checking that the computed auth tags match */
  19217. ExpectIntEQ(wc_AesEaxDecryptAuth(vectors[i].key, vectors[i].key_length,
  19218. ciphertext,
  19219. vectors[i].ct, vectors[i].ct_length,
  19220. vectors[i].iv, vectors[i].iv_length,
  19221. authtag, len,
  19222. vectors[i].aad, vectors[i].aad_length),
  19223. 0);
  19224. /* check decrypted ciphertext matches vector plaintext */
  19225. ExpectIntEQ(XMEMCMP(ciphertext, vectors[i].msg, vectors[i].msg_length),
  19226. 0);
  19227. }
  19228. return EXPECT_RESULT();
  19229. } /* END test_wc_AesEaxVectors */
  19230. /*
  19231. * Testing test_wc_AesEaxEncryptAuth()
  19232. */
  19233. static int test_wc_AesEaxEncryptAuth(void)
  19234. {
  19235. EXPECT_DECLS;
  19236. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19237. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19238. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19239. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19240. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19241. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19242. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19243. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19244. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  19245. const byte msg[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  19246. byte ciphertext[sizeof(msg)];
  19247. byte authtag[AES_BLOCK_SIZE];
  19248. int i;
  19249. int len;
  19250. len = sizeof(authtag);
  19251. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19252. ciphertext,
  19253. msg, sizeof(msg),
  19254. iv, sizeof(iv),
  19255. authtag, (word32)len,
  19256. aad, sizeof(aad)),
  19257. 0);
  19258. /* Test null checking */
  19259. ExpectIntEQ(wc_AesEaxEncryptAuth(NULL, sizeof(key),
  19260. ciphertext,
  19261. msg, sizeof(msg),
  19262. iv, sizeof(iv),
  19263. authtag, (word32)len,
  19264. aad, sizeof(aad)),
  19265. BAD_FUNC_ARG);
  19266. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19267. NULL,
  19268. msg, sizeof(msg),
  19269. iv, sizeof(iv),
  19270. authtag, (word32)len,
  19271. aad, sizeof(aad)),
  19272. BAD_FUNC_ARG);
  19273. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19274. ciphertext,
  19275. NULL, sizeof(msg),
  19276. iv, sizeof(iv),
  19277. authtag, (word32)len,
  19278. aad, sizeof(aad)),
  19279. BAD_FUNC_ARG);
  19280. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19281. ciphertext,
  19282. msg, sizeof(msg),
  19283. NULL, sizeof(iv),
  19284. authtag, (word32)len,
  19285. aad, sizeof(aad)),
  19286. BAD_FUNC_ARG);
  19287. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19288. ciphertext,
  19289. msg, sizeof(msg),
  19290. iv, sizeof(iv),
  19291. NULL, (word32)len,
  19292. aad, sizeof(aad)),
  19293. BAD_FUNC_ARG);
  19294. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19295. ciphertext,
  19296. msg, sizeof(msg),
  19297. iv, sizeof(iv),
  19298. authtag, (word32)len,
  19299. NULL, sizeof(aad)),
  19300. BAD_FUNC_ARG);
  19301. /* Test bad key lengths */
  19302. for (i = 0; i <= 32; i++) {
  19303. int exp_ret;
  19304. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  19305. || i == AES_256_KEY_SIZE) {
  19306. exp_ret = 0;
  19307. }
  19308. else {
  19309. exp_ret = BAD_FUNC_ARG;
  19310. }
  19311. ExpectIntEQ(wc_AesEaxEncryptAuth(key, (word32)i,
  19312. ciphertext,
  19313. msg, sizeof(msg),
  19314. iv, sizeof(iv),
  19315. authtag, (word32)len,
  19316. aad, sizeof(aad)),
  19317. exp_ret);
  19318. }
  19319. /* Test auth tag size out of range */
  19320. len = AES_BLOCK_SIZE + 1;
  19321. ExpectIntEQ(wc_AesEaxEncryptAuth(key, sizeof(key),
  19322. ciphertext,
  19323. msg, sizeof(msg),
  19324. iv, sizeof(iv),
  19325. authtag, (word32)len,
  19326. aad, sizeof(aad)),
  19327. BAD_FUNC_ARG);
  19328. return EXPECT_RESULT();
  19329. } /* END test_wc_AesEaxEncryptAuth() */
  19330. /*
  19331. * Testing test_wc_AesEaxDecryptAuth()
  19332. */
  19333. static int test_wc_AesEaxDecryptAuth(void)
  19334. {
  19335. EXPECT_DECLS;
  19336. const byte key[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19337. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19338. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19339. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19340. const byte iv[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19341. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
  19342. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  19343. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  19344. const byte aad[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  19345. const byte ct[] = {0x00, 0x01, 0x02, 0x03, 0x04};
  19346. /* Garbage tag that should always fail for above aad */
  19347. const byte tag[] = {0xFE, 0xED, 0xBE, 0xEF, 0xDE, 0xAD, 0xC0, 0xDE,
  19348. 0xCA, 0xFE, 0xBE, 0xEF, 0xDE, 0xAF, 0xBE, 0xEF};
  19349. byte plaintext[sizeof(ct)];
  19350. int i;
  19351. int len;
  19352. len = sizeof(tag);
  19353. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19354. plaintext,
  19355. ct, sizeof(ct),
  19356. iv, sizeof(iv),
  19357. tag, (word32)len,
  19358. aad, sizeof(aad)),
  19359. AES_EAX_AUTH_E);
  19360. /* Test null checking */
  19361. ExpectIntEQ(wc_AesEaxDecryptAuth(NULL, sizeof(key),
  19362. plaintext,
  19363. ct, sizeof(ct),
  19364. iv, sizeof(iv),
  19365. tag, (word32)len,
  19366. aad, sizeof(aad)),
  19367. BAD_FUNC_ARG);
  19368. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19369. NULL,
  19370. ct, sizeof(ct),
  19371. iv, sizeof(iv),
  19372. tag, (word32)len,
  19373. aad, sizeof(aad)),
  19374. BAD_FUNC_ARG);
  19375. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19376. plaintext,
  19377. NULL, sizeof(ct),
  19378. iv, sizeof(iv),
  19379. tag, (word32)len,
  19380. aad, sizeof(aad)),
  19381. BAD_FUNC_ARG);
  19382. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19383. plaintext,
  19384. ct, sizeof(ct),
  19385. NULL, sizeof(iv),
  19386. tag, (word32)len,
  19387. aad, sizeof(aad)),
  19388. BAD_FUNC_ARG);
  19389. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19390. plaintext,
  19391. ct, sizeof(ct),
  19392. iv, sizeof(iv),
  19393. NULL, (word32)len,
  19394. aad, sizeof(aad)),
  19395. BAD_FUNC_ARG);
  19396. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19397. plaintext,
  19398. ct, sizeof(ct),
  19399. iv, sizeof(iv),
  19400. tag, (word32)len,
  19401. NULL, sizeof(aad)),
  19402. BAD_FUNC_ARG);
  19403. /* Test bad key lengths */
  19404. for (i = 0; i <= 32; i++) {
  19405. int exp_ret;
  19406. if (i == AES_128_KEY_SIZE || i == AES_192_KEY_SIZE
  19407. || i == AES_256_KEY_SIZE) {
  19408. exp_ret = AES_EAX_AUTH_E;
  19409. }
  19410. else {
  19411. exp_ret = BAD_FUNC_ARG;
  19412. }
  19413. ExpectIntEQ(wc_AesEaxDecryptAuth(key, (word32)i,
  19414. plaintext,
  19415. ct, sizeof(ct),
  19416. iv, sizeof(iv),
  19417. tag, (word32)len,
  19418. aad, sizeof(aad)),
  19419. exp_ret);
  19420. }
  19421. /* Test auth tag size out of range */
  19422. len = AES_BLOCK_SIZE + 1;
  19423. ExpectIntEQ(wc_AesEaxDecryptAuth(key, sizeof(key),
  19424. plaintext,
  19425. ct, sizeof(ct),
  19426. iv, sizeof(iv),
  19427. tag, (word32)len,
  19428. aad, sizeof(aad)),
  19429. BAD_FUNC_ARG);
  19430. return EXPECT_RESULT();
  19431. } /* END test_wc_AesEaxDecryptAuth() */
  19432. #endif /* WOLFSSL_AES_EAX &&
  19433. * (!HAVE_FIPS || FIPS_VERSION_GE(5, 3)) && !HAVE_SELFTEST
  19434. */
  19435. /*
  19436. * Testing wc_InitDsaKey()
  19437. */
  19438. static int test_wc_InitDsaKey(void)
  19439. {
  19440. EXPECT_DECLS;
  19441. #ifndef NO_DSA
  19442. DsaKey key;
  19443. XMEMSET(&key, 0, sizeof(DsaKey));
  19444. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19445. /* Pass in bad args. */
  19446. ExpectIntEQ(wc_InitDsaKey(NULL), BAD_FUNC_ARG);
  19447. wc_FreeDsaKey(&key);
  19448. #endif
  19449. return EXPECT_RESULT();
  19450. } /* END test_wc_InitDsaKey */
  19451. /*
  19452. * Testing wc_DsaSign() and wc_DsaVerify()
  19453. */
  19454. static int test_wc_DsaSignVerify(void)
  19455. {
  19456. EXPECT_DECLS;
  19457. #if !defined(NO_DSA)
  19458. DsaKey key;
  19459. WC_RNG rng;
  19460. wc_Sha sha;
  19461. byte signature[DSA_SIG_SIZE];
  19462. byte hash[WC_SHA_DIGEST_SIZE];
  19463. word32 idx = 0;
  19464. word32 bytes;
  19465. int answer;
  19466. #ifdef USE_CERT_BUFFERS_1024
  19467. byte tmp[ONEK_BUF];
  19468. XMEMSET(tmp, 0, sizeof(tmp));
  19469. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19470. bytes = sizeof_dsa_key_der_1024;
  19471. #elif defined(USE_CERT_BUFFERS_2048)
  19472. byte tmp[TWOK_BUF];
  19473. XMEMSET(tmp, 0, sizeof(tmp));
  19474. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19475. bytes = sizeof_dsa_key_der_2048;
  19476. #else
  19477. byte tmp[TWOK_BUF];
  19478. XFILE fp = XBADFILE;
  19479. XMEMSET(tmp, 0, sizeof(tmp));
  19480. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19481. ExpectTrue((bytes = (word32)XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19482. if (fp != XBADFILE)
  19483. XFCLOSE(fp);
  19484. #endif /* END USE_CERT_BUFFERS_1024 */
  19485. ExpectIntEQ(wc_InitSha(&sha), 0);
  19486. ExpectIntEQ(wc_ShaUpdate(&sha, tmp, bytes), 0);
  19487. ExpectIntEQ(wc_ShaFinal(&sha, hash), 0);
  19488. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19489. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19490. ExpectIntEQ(wc_InitRng(&rng), 0);
  19491. /* Sign. */
  19492. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), 0);
  19493. /* Test bad args. */
  19494. ExpectIntEQ(wc_DsaSign(NULL, signature, &key, &rng), BAD_FUNC_ARG);
  19495. ExpectIntEQ(wc_DsaSign(hash, NULL, &key, &rng), BAD_FUNC_ARG);
  19496. ExpectIntEQ(wc_DsaSign(hash, signature, NULL, &rng), BAD_FUNC_ARG);
  19497. ExpectIntEQ(wc_DsaSign(hash, signature, &key, NULL), BAD_FUNC_ARG);
  19498. /* Verify. */
  19499. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, &answer), 0);
  19500. ExpectIntEQ(answer, 1);
  19501. /* Pass in bad args. */
  19502. ExpectIntEQ(wc_DsaVerify(NULL, signature, &key, &answer), BAD_FUNC_ARG);
  19503. ExpectIntEQ(wc_DsaVerify(hash, NULL, &key, &answer), BAD_FUNC_ARG);
  19504. ExpectIntEQ(wc_DsaVerify(hash, signature, NULL, &answer), BAD_FUNC_ARG);
  19505. ExpectIntEQ(wc_DsaVerify(hash, signature, &key, NULL), BAD_FUNC_ARG);
  19506. #if !defined(HAVE_FIPS) && defined(WOLFSSL_PUBLIC_MP)
  19507. /* hard set q to 0 and test fail case */
  19508. mp_free(&key.q);
  19509. mp_init(&key.q);
  19510. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  19511. mp_set(&key.q, 1);
  19512. ExpectIntEQ(wc_DsaSign(hash, signature, &key, &rng), BAD_FUNC_ARG);
  19513. #endif
  19514. DoExpectIntEQ(wc_FreeRng(&rng),0);
  19515. wc_FreeDsaKey(&key);
  19516. wc_ShaFree(&sha);
  19517. #endif
  19518. return EXPECT_RESULT();
  19519. } /* END test_wc_DsaSign */
  19520. /*
  19521. * Testing wc_DsaPrivateKeyDecode() and wc_DsaPublicKeyDecode()
  19522. */
  19523. static int test_wc_DsaPublicPrivateKeyDecode(void)
  19524. {
  19525. EXPECT_DECLS;
  19526. #if !defined(NO_DSA)
  19527. DsaKey key;
  19528. word32 bytes = 0;
  19529. word32 idx = 0;
  19530. int ret = 0;
  19531. #ifdef USE_CERT_BUFFERS_1024
  19532. byte tmp[ONEK_BUF];
  19533. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19534. bytes = sizeof_dsa_key_der_1024;
  19535. #elif defined(USE_CERT_BUFFERS_2048)
  19536. byte tmp[TWOK_BUF];
  19537. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19538. bytes = sizeof_dsa_key_der_2048;
  19539. #else
  19540. byte tmp[TWOK_BUF];
  19541. XFILE fp = XBADFILE;
  19542. XMEMSET(tmp, 0, sizeof(tmp));
  19543. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19544. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19545. if (fp != XBADFILE)
  19546. XFCLOSE(fp);
  19547. #endif /* END USE_CERT_BUFFERS_1024 */
  19548. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19549. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19550. /* Test bad args. */
  19551. ExpectIntEQ(wc_DsaPrivateKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  19552. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  19553. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  19554. ExpectIntLT(ret = wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19555. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  19556. wc_FreeDsaKey(&key);
  19557. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19558. idx = 0; /* Reset */
  19559. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  19560. /* Test bad args. */
  19561. ExpectIntEQ(wc_DsaPublicKeyDecode(NULL, &idx, &key, bytes), BAD_FUNC_ARG);
  19562. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, NULL, &key, bytes), BAD_FUNC_ARG);
  19563. ExpectIntEQ(wc_DsaPublicKeyDecode(tmp, &idx, NULL, bytes), BAD_FUNC_ARG);
  19564. ExpectIntLT(ret = wc_DsaPublicKeyDecode(tmp, &idx, &key, bytes), 0);
  19565. ExpectTrue((ret == ASN_PARSE_E) || (ret == BUFFER_E));
  19566. wc_FreeDsaKey(&key);
  19567. #endif /* !NO_DSA */
  19568. return EXPECT_RESULT();
  19569. } /* END test_wc_DsaPublicPrivateKeyDecode */
  19570. /*
  19571. * Testing wc_MakeDsaKey() and wc_MakeDsaParameters()
  19572. */
  19573. static int test_wc_MakeDsaKey(void)
  19574. {
  19575. EXPECT_DECLS;
  19576. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19577. DsaKey genKey;
  19578. WC_RNG rng;
  19579. XMEMSET(&genKey, 0, sizeof(genKey));
  19580. XMEMSET(&rng, 0, sizeof(rng));
  19581. ExpectIntEQ(wc_InitDsaKey(&genKey), 0);
  19582. ExpectIntEQ(wc_InitRng(&rng), 0);
  19583. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &genKey), 0);
  19584. /* Test bad args. */
  19585. ExpectIntEQ(wc_MakeDsaParameters(NULL, ONEK_BUF, &genKey), BAD_FUNC_ARG);
  19586. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, NULL), BAD_FUNC_ARG);
  19587. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF + 1, &genKey),
  19588. BAD_FUNC_ARG);
  19589. ExpectIntEQ(wc_MakeDsaKey(&rng, &genKey), 0);
  19590. /* Test bad args. */
  19591. ExpectIntEQ(wc_MakeDsaKey(NULL, &genKey), BAD_FUNC_ARG);
  19592. ExpectIntEQ(wc_MakeDsaKey(&rng, NULL), BAD_FUNC_ARG);
  19593. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19594. wc_FreeDsaKey(&genKey);
  19595. #endif
  19596. return EXPECT_RESULT();
  19597. } /* END test_wc_MakeDsaKey */
  19598. /*
  19599. * Testing wc_DsaKeyToDer()
  19600. */
  19601. static int test_wc_DsaKeyToDer(void)
  19602. {
  19603. EXPECT_DECLS;
  19604. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19605. DsaKey key;
  19606. word32 bytes;
  19607. word32 idx = 0;
  19608. #ifdef USE_CERT_BUFFERS_1024
  19609. byte tmp[ONEK_BUF];
  19610. byte der[ONEK_BUF];
  19611. XMEMSET(tmp, 0, sizeof(tmp));
  19612. XMEMSET(der, 0, sizeof(der));
  19613. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  19614. bytes = sizeof_dsa_key_der_1024;
  19615. #elif defined(USE_CERT_BUFFERS_2048)
  19616. byte tmp[TWOK_BUF];
  19617. byte der[TWOK_BUF];
  19618. XMEMSET(tmp, 0, sizeof(tmp));
  19619. XMEMSET(der, 0, sizeof(der));
  19620. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  19621. bytes = sizeof_dsa_key_der_2048;
  19622. #else
  19623. byte tmp[TWOK_BUF];
  19624. byte der[TWOK_BUF];
  19625. XFILE fp = XBADFILE;
  19626. XMEMSET(tmp, 0, sizeof(tmp));
  19627. XMEMSET(der, 0, sizeof(der));
  19628. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  19629. ExpectTrue((bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp)) > 0);
  19630. if (fp != XBADFILE)
  19631. XFCLOSE(fp);
  19632. #endif /* END USE_CERT_BUFFERS_1024 */
  19633. XMEMSET(&key, 0, sizeof(DsaKey));
  19634. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19635. ExpectIntEQ(wc_DsaPrivateKeyDecode(tmp, &idx, &key, bytes), 0);
  19636. ExpectIntGE(wc_DsaKeyToDer(&key, der, bytes), 0);
  19637. ExpectIntEQ(XMEMCMP(der, tmp, bytes), 0);
  19638. /* Test bad args. */
  19639. ExpectIntEQ(wc_DsaKeyToDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  19640. ExpectIntEQ(wc_DsaKeyToDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  19641. wc_FreeDsaKey(&key);
  19642. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19643. return EXPECT_RESULT();
  19644. } /* END test_wc_DsaKeyToDer */
  19645. /*
  19646. * Testing wc_DsaKeyToPublicDer()
  19647. * (indirectly testing setDsaPublicKey())
  19648. */
  19649. static int test_wc_DsaKeyToPublicDer(void)
  19650. {
  19651. EXPECT_DECLS;
  19652. #ifndef HAVE_SELFTEST
  19653. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19654. DsaKey key;
  19655. WC_RNG rng;
  19656. byte* der = NULL;
  19657. word32 sz = 0;
  19658. word32 idx = 0;
  19659. XMEMSET(&key, 0, sizeof(DsaKey));
  19660. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19661. ExpectNotNull(der = (byte*)XMALLOC(ONEK_BUF, NULL,
  19662. DYNAMIC_TYPE_TMP_BUFFER));
  19663. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19664. ExpectIntEQ(wc_InitRng(&rng), 0);
  19665. ExpectIntEQ(wc_MakeDsaParameters(&rng, ONEK_BUF, &key), 0);
  19666. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  19667. ExpectIntGE(sz = (word32)wc_DsaKeyToPublicDer(&key, der, ONEK_BUF), 0);
  19668. wc_FreeDsaKey(&key);
  19669. idx = 0;
  19670. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  19671. /* Test without the SubjectPublicKeyInfo header */
  19672. ExpectIntGE(sz = (word32)wc_SetDsaPublicKey(der, &key, ONEK_BUF, 0), 0);
  19673. wc_FreeDsaKey(&key);
  19674. idx = 0;
  19675. ExpectIntEQ(wc_DsaPublicKeyDecode(der, &idx, &key, sz), 0);
  19676. /* Test bad args. */
  19677. ExpectIntEQ(wc_DsaKeyToPublicDer(NULL, der, FOURK_BUF), BAD_FUNC_ARG);
  19678. ExpectIntEQ(wc_DsaKeyToPublicDer(&key, NULL, FOURK_BUF), BAD_FUNC_ARG);
  19679. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19680. wc_FreeDsaKey(&key);
  19681. XFREE(der, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  19682. #endif /* !NO_DSA && WOLFSSL_KEY_GEN */
  19683. #endif /* !HAVE_SELFTEST */
  19684. return EXPECT_RESULT();
  19685. } /* END test_wc_DsaKeyToPublicDer */
  19686. /*
  19687. * Testing wc_DsaImportParamsRaw()
  19688. */
  19689. static int test_wc_DsaImportParamsRaw(void)
  19690. {
  19691. EXPECT_DECLS;
  19692. #if !defined(NO_DSA)
  19693. DsaKey key;
  19694. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19695. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19696. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19697. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19698. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19699. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19700. "80a0093113a8bd73";
  19701. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19702. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19703. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19704. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19705. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19706. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19707. "76341a7e7d9";
  19708. /* invalid p and q parameters */
  19709. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19710. const char* invalidQ = "96c5390a";
  19711. XMEMSET(&key, 0, sizeof(DsaKey));
  19712. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19713. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  19714. /* test bad args */
  19715. /* null key struct */
  19716. ExpectIntEQ(wc_DsaImportParamsRaw(NULL, p, q, g), BAD_FUNC_ARG);
  19717. /* null param pointers */
  19718. ExpectIntEQ(wc_DsaImportParamsRaw(&key, NULL, NULL, NULL), BAD_FUNC_ARG);
  19719. /* illegal p length */
  19720. ExpectIntEQ(wc_DsaImportParamsRaw(&key, invalidP, q, g), BAD_FUNC_ARG);
  19721. /* illegal q length */
  19722. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, invalidQ, g), BAD_FUNC_ARG);
  19723. wc_FreeDsaKey(&key);
  19724. #endif
  19725. return EXPECT_RESULT();
  19726. } /* END test_wc_DsaImportParamsRaw */
  19727. /*
  19728. * Testing wc_DsaImportParamsRawCheck()
  19729. */
  19730. static int test_wc_DsaImportParamsRawCheck(void)
  19731. {
  19732. EXPECT_DECLS;
  19733. #if !defined(NO_DSA) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  19734. DsaKey key;
  19735. int trusted = 0;
  19736. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19737. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19738. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19739. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19740. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19741. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19742. "80a0093113a8bd73";
  19743. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19744. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19745. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19746. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19747. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19748. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19749. "76341a7e7d9";
  19750. /* invalid p and q parameters */
  19751. const char* invalidP = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d";
  19752. const char* invalidQ = "96c5390a";
  19753. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19754. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, q, g, trusted, NULL), 0);
  19755. /* test bad args */
  19756. /* null key struct */
  19757. ExpectIntEQ(wc_DsaImportParamsRawCheck(NULL, p, q, g, trusted, NULL),
  19758. BAD_FUNC_ARG);
  19759. /* null param pointers */
  19760. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, NULL, NULL, NULL, trusted,
  19761. NULL), BAD_FUNC_ARG);
  19762. /* illegal p length */
  19763. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, invalidP, q, g, trusted, NULL),
  19764. BAD_FUNC_ARG);
  19765. /* illegal q length */
  19766. ExpectIntEQ(wc_DsaImportParamsRawCheck(&key, p, invalidQ, g, trusted, NULL),
  19767. BAD_FUNC_ARG);
  19768. wc_FreeDsaKey(&key);
  19769. #endif
  19770. return EXPECT_RESULT();
  19771. } /* END test_wc_DsaImportParamsRawCheck */
  19772. /*
  19773. * Testing wc_DsaExportParamsRaw()
  19774. */
  19775. static int test_wc_DsaExportParamsRaw(void)
  19776. {
  19777. EXPECT_DECLS;
  19778. #if !defined(NO_DSA)
  19779. DsaKey key;
  19780. /* [mod = L=1024, N=160], from CAVP KeyPair */
  19781. const char* p = "d38311e2cd388c3ed698e82fdf88eb92b5a9a483dc88005d"
  19782. "4b725ef341eabb47cf8a7a8a41e792a156b7ce97206c4f9c"
  19783. "5ce6fc5ae7912102b6b502e59050b5b21ce263dddb2044b6"
  19784. "52236f4d42ab4b5d6aa73189cef1ace778d7845a5c1c1c71"
  19785. "47123188f8dc551054ee162b634d60f097f719076640e209"
  19786. "80a0093113a8bd73";
  19787. const char* q = "96c5390a8b612c0e422bb2b0ea194a3ec935a281";
  19788. const char* g = "06b7861abbd35cc89e79c52f68d20875389b127361ca66822"
  19789. "138ce4991d2b862259d6b4548a6495b195aa0e0b6137ca37e"
  19790. "b23b94074d3c3d300042bdf15762812b6333ef7b07ceba786"
  19791. "07610fcc9ee68491dbc1e34cd12615474e52b18bc934fb00c"
  19792. "61d39e7da8902291c4434a4e2224c3f4fd9f93cd6f4f17fc0"
  19793. "76341a7e7d9";
  19794. const char* pCompare = "\xd3\x83\x11\xe2\xcd\x38\x8c\x3e\xd6\x98\xe8\x2f"
  19795. "\xdf\x88\xeb\x92\xb5\xa9\xa4\x83\xdc\x88\x00\x5d"
  19796. "\x4b\x72\x5e\xf3\x41\xea\xbb\x47\xcf\x8a\x7a\x8a"
  19797. "\x41\xe7\x92\xa1\x56\xb7\xce\x97\x20\x6c\x4f\x9c"
  19798. "\x5c\xe6\xfc\x5a\xe7\x91\x21\x02\xb6\xb5\x02\xe5"
  19799. "\x90\x50\xb5\xb2\x1c\xe2\x63\xdd\xdb\x20\x44\xb6"
  19800. "\x52\x23\x6f\x4d\x42\xab\x4b\x5d\x6a\xa7\x31\x89"
  19801. "\xce\xf1\xac\xe7\x78\xd7\x84\x5a\x5c\x1c\x1c\x71"
  19802. "\x47\x12\x31\x88\xf8\xdc\x55\x10\x54\xee\x16\x2b"
  19803. "\x63\x4d\x60\xf0\x97\xf7\x19\x07\x66\x40\xe2\x09"
  19804. "\x80\xa0\x09\x31\x13\xa8\xbd\x73";
  19805. const char* qCompare = "\x96\xc5\x39\x0a\x8b\x61\x2c\x0e\x42\x2b\xb2\xb0"
  19806. "\xea\x19\x4a\x3e\xc9\x35\xa2\x81";
  19807. const char* gCompare = "\x06\xb7\x86\x1a\xbb\xd3\x5c\xc8\x9e\x79\xc5\x2f"
  19808. "\x68\xd2\x08\x75\x38\x9b\x12\x73\x61\xca\x66\x82"
  19809. "\x21\x38\xce\x49\x91\xd2\xb8\x62\x25\x9d\x6b\x45"
  19810. "\x48\xa6\x49\x5b\x19\x5a\xa0\xe0\xb6\x13\x7c\xa3"
  19811. "\x7e\xb2\x3b\x94\x07\x4d\x3c\x3d\x30\x00\x42\xbd"
  19812. "\xf1\x57\x62\x81\x2b\x63\x33\xef\x7b\x07\xce\xba"
  19813. "\x78\x60\x76\x10\xfc\xc9\xee\x68\x49\x1d\xbc\x1e"
  19814. "\x34\xcd\x12\x61\x54\x74\xe5\x2b\x18\xbc\x93\x4f"
  19815. "\xb0\x0c\x61\xd3\x9e\x7d\xa8\x90\x22\x91\xc4\x43"
  19816. "\x4a\x4e\x22\x24\xc3\xf4\xfd\x9f\x93\xcd\x6f\x4f"
  19817. "\x17\xfc\x07\x63\x41\xa7\xe7\xd9";
  19818. byte pOut[MAX_DSA_PARAM_SIZE];
  19819. byte qOut[MAX_DSA_PARAM_SIZE];
  19820. byte gOut[MAX_DSA_PARAM_SIZE];
  19821. word32 pOutSz;
  19822. word32 qOutSz;
  19823. word32 gOutSz;
  19824. XMEMSET(&key, 0, sizeof(DsaKey));
  19825. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19826. /* first test using imported raw parameters, for expected */
  19827. ExpectIntEQ(wc_DsaImportParamsRaw(&key, p, q, g), 0);
  19828. pOutSz = sizeof(pOut);
  19829. qOutSz = sizeof(qOut);
  19830. gOutSz = sizeof(gOut);
  19831. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19832. &gOutSz), 0);
  19833. /* validate exported parameters are correct */
  19834. ExpectIntEQ(XMEMCMP(pOut, pCompare, pOutSz), 0);
  19835. ExpectIntEQ(XMEMCMP(qOut, qCompare, qOutSz), 0);
  19836. ExpectIntEQ(XMEMCMP(gOut, gCompare, gOutSz), 0);
  19837. /* test bad args */
  19838. /* null key struct */
  19839. ExpectIntEQ(wc_DsaExportParamsRaw(NULL, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19840. &gOutSz), BAD_FUNC_ARG);
  19841. /* null output pointers */
  19842. ExpectIntEQ(wc_DsaExportParamsRaw(&key, NULL, &pOutSz, NULL, &qOutSz, NULL,
  19843. &gOutSz), LENGTH_ONLY_E);
  19844. /* null output size pointers */
  19845. ExpectIntEQ( wc_DsaExportParamsRaw(&key, pOut, NULL, qOut, NULL, gOut,
  19846. NULL), BAD_FUNC_ARG);
  19847. /* p output buffer size too small */
  19848. pOutSz = 1;
  19849. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19850. &gOutSz), BUFFER_E);
  19851. pOutSz = sizeof(pOut);
  19852. /* q output buffer size too small */
  19853. qOutSz = 1;
  19854. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19855. &gOutSz), BUFFER_E);
  19856. qOutSz = sizeof(qOut);
  19857. /* g output buffer size too small */
  19858. gOutSz = 1;
  19859. ExpectIntEQ(wc_DsaExportParamsRaw(&key, pOut, &pOutSz, qOut, &qOutSz, gOut,
  19860. &gOutSz), BUFFER_E);
  19861. wc_FreeDsaKey(&key);
  19862. #endif
  19863. return EXPECT_RESULT();
  19864. } /* END test_wc_DsaExportParamsRaw */
  19865. /*
  19866. * Testing wc_DsaExportKeyRaw()
  19867. */
  19868. static int test_wc_DsaExportKeyRaw(void)
  19869. {
  19870. EXPECT_DECLS;
  19871. #if !defined(NO_DSA) && defined(WOLFSSL_KEY_GEN)
  19872. DsaKey key;
  19873. WC_RNG rng;
  19874. byte xOut[MAX_DSA_PARAM_SIZE];
  19875. byte yOut[MAX_DSA_PARAM_SIZE];
  19876. word32 xOutSz, yOutSz;
  19877. XMEMSET(&key, 0, sizeof(key));
  19878. XMEMSET(&rng, 0, sizeof(rng));
  19879. ExpectIntEQ(wc_InitDsaKey(&key), 0);
  19880. ExpectIntEQ(wc_InitRng(&rng), 0);
  19881. ExpectIntEQ(wc_MakeDsaParameters(&rng, 1024, &key), 0);
  19882. ExpectIntEQ(wc_MakeDsaKey(&rng, &key), 0);
  19883. /* try successful export */
  19884. xOutSz = sizeof(xOut);
  19885. yOutSz = sizeof(yOut);
  19886. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz), 0);
  19887. /* test bad args */
  19888. /* null key struct */
  19889. ExpectIntEQ(wc_DsaExportKeyRaw(NULL, xOut, &xOutSz, yOut, &yOutSz),
  19890. BAD_FUNC_ARG);
  19891. /* null output pointers */
  19892. ExpectIntEQ(wc_DsaExportKeyRaw(&key, NULL, &xOutSz, NULL, &yOutSz),
  19893. LENGTH_ONLY_E);
  19894. /* null output size pointers */
  19895. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, NULL, yOut, NULL),
  19896. BAD_FUNC_ARG);
  19897. /* x output buffer size too small */
  19898. xOutSz = 1;
  19899. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  19900. BUFFER_E);
  19901. xOutSz = sizeof(xOut);
  19902. /* y output buffer size too small */
  19903. yOutSz = 1;
  19904. ExpectIntEQ(wc_DsaExportKeyRaw(&key, xOut, &xOutSz, yOut, &yOutSz),
  19905. BUFFER_E);
  19906. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19907. wc_FreeDsaKey(&key);
  19908. #endif
  19909. return EXPECT_RESULT();
  19910. } /* END test_wc_DsaExportParamsRaw */
  19911. /*
  19912. * Testing wc_ed25519_make_key().
  19913. */
  19914. static int test_wc_ed25519_make_key(void)
  19915. {
  19916. EXPECT_DECLS;
  19917. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_MAKE_KEY)
  19918. ed25519_key key;
  19919. WC_RNG rng;
  19920. unsigned char pubkey[ED25519_PUB_KEY_SIZE+1];
  19921. int pubkey_sz = ED25519_PUB_KEY_SIZE;
  19922. XMEMSET(&key, 0, sizeof(ed25519_key));
  19923. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19924. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19925. ExpectIntEQ(wc_InitRng(&rng), 0);
  19926. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey, (word32)pubkey_sz),
  19927. ECC_PRIV_KEY_E);
  19928. ExpectIntEQ(wc_ed25519_make_public(&key, pubkey+1, (word32)pubkey_sz),
  19929. ECC_PRIV_KEY_E);
  19930. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19931. /* Test bad args. */
  19932. ExpectIntEQ(wc_ed25519_make_key(NULL, ED25519_KEY_SIZE, &key),
  19933. BAD_FUNC_ARG);
  19934. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, NULL),
  19935. BAD_FUNC_ARG);
  19936. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE - 1, &key),
  19937. BAD_FUNC_ARG);
  19938. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE + 1, &key),
  19939. BAD_FUNC_ARG);
  19940. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  19941. wc_ed25519_free(&key);
  19942. #endif
  19943. return EXPECT_RESULT();
  19944. } /* END test_wc_ed25519_make_key */
  19945. /*
  19946. * Testing wc_ed25519_init()
  19947. */
  19948. static int test_wc_ed25519_init(void)
  19949. {
  19950. EXPECT_DECLS;
  19951. #if defined(HAVE_ED25519)
  19952. ed25519_key key;
  19953. XMEMSET(&key, 0, sizeof(ed25519_key));
  19954. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19955. /* Test bad args. */
  19956. ExpectIntEQ(wc_ed25519_init(NULL), BAD_FUNC_ARG);
  19957. wc_ed25519_free(&key);
  19958. #endif
  19959. return EXPECT_RESULT();
  19960. } /* END test_wc_ed25519_init */
  19961. /*
  19962. * Test wc_ed25519_sign_msg() and wc_ed25519_verify_msg()
  19963. */
  19964. static int test_wc_ed25519_sign_msg(void)
  19965. {
  19966. EXPECT_DECLS;
  19967. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_SIGN)
  19968. WC_RNG rng;
  19969. ed25519_key key;
  19970. byte msg[] = "Everybody gets Friday off.\n";
  19971. byte sig[ED25519_SIG_SIZE+1];
  19972. word32 msglen = sizeof(msg);
  19973. word32 siglen = ED25519_SIG_SIZE;
  19974. word32 badSigLen = ED25519_SIG_SIZE - 1;
  19975. #ifdef HAVE_ED25519_VERIFY
  19976. int verify_ok = 0; /*1 = Verify success.*/
  19977. #endif
  19978. /* Initialize stack variables. */
  19979. XMEMSET(&key, 0, sizeof(ed25519_key));
  19980. XMEMSET(&rng, 0, sizeof(WC_RNG));
  19981. XMEMSET(sig, 0, sizeof(sig));
  19982. /* Initialize key. */
  19983. ExpectIntEQ(wc_ed25519_init(&key), 0);
  19984. ExpectIntEQ(wc_InitRng(&rng), 0);
  19985. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  19986. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, &key), 0);
  19987. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  19988. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig+1, &siglen, &key), 0);
  19989. ExpectIntEQ(siglen, ED25519_SIG_SIZE);
  19990. /* Test bad args. */
  19991. ExpectIntEQ(wc_ed25519_sign_msg(NULL, msglen, sig, &siglen, &key),
  19992. BAD_FUNC_ARG);
  19993. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, NULL, &siglen, &key),
  19994. BAD_FUNC_ARG);
  19995. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, NULL, &key),
  19996. BAD_FUNC_ARG);
  19997. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &siglen, NULL),
  19998. BAD_FUNC_ARG);
  19999. ExpectIntEQ(wc_ed25519_sign_msg(msg, msglen, sig, &badSigLen, &key),
  20000. BUFFER_E);
  20001. ExpectIntEQ(badSigLen, ED25519_SIG_SIZE);
  20002. badSigLen -= 1;
  20003. #ifdef HAVE_ED25519_VERIFY
  20004. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  20005. &key), 0);
  20006. ExpectIntEQ(verify_ok, 1);
  20007. /* Test bad args. */
  20008. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen - 1, msg, msglen,
  20009. &verify_ok, &key), BAD_FUNC_ARG);
  20010. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen + 1, msg, msglen,
  20011. &verify_ok, &key), BAD_FUNC_ARG);
  20012. ExpectIntEQ(wc_ed25519_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  20013. &key), BAD_FUNC_ARG);
  20014. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, NULL, msglen, &verify_ok,
  20015. &key), BAD_FUNC_ARG);
  20016. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, NULL, &key),
  20017. BAD_FUNC_ARG);
  20018. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, siglen, msg, msglen, &verify_ok,
  20019. NULL), BAD_FUNC_ARG);
  20020. ExpectIntEQ(wc_ed25519_verify_msg(sig+1, badSigLen, msg, msglen, &verify_ok,
  20021. &key), BAD_FUNC_ARG);
  20022. #endif /* Verify. */
  20023. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20024. wc_ed25519_free(&key);
  20025. #endif
  20026. return EXPECT_RESULT();
  20027. } /* END test_wc_ed25519_sign_msg */
  20028. /*
  20029. * Testing wc_ed25519_import_public()
  20030. */
  20031. static int test_wc_ed25519_import_public(void)
  20032. {
  20033. EXPECT_DECLS;
  20034. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  20035. ed25519_key pubKey;
  20036. WC_RNG rng;
  20037. const byte in[] = "Ed25519PublicKeyUnitTest......\n";
  20038. word32 inlen = sizeof(in);
  20039. XMEMSET(&pubKey, 0, sizeof(ed25519_key));
  20040. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20041. ExpectIntEQ(wc_ed25519_init(&pubKey), 0);
  20042. ExpectIntEQ(wc_InitRng(&rng), 0);
  20043. #ifdef HAVE_ED25519_MAKE_KEY
  20044. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &pubKey), 0);
  20045. #endif
  20046. ExpectIntEQ(wc_ed25519_import_public_ex(in, inlen, &pubKey, 1), 0);
  20047. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  20048. /* Test bad args. */
  20049. ExpectIntEQ(wc_ed25519_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  20050. ExpectIntEQ(wc_ed25519_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  20051. ExpectIntEQ(wc_ed25519_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  20052. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20053. wc_ed25519_free(&pubKey);
  20054. #endif
  20055. return EXPECT_RESULT();
  20056. } /* END wc_ed25519_import_public */
  20057. /*
  20058. * Testing wc_ed25519_import_private_key()
  20059. */
  20060. static int test_wc_ed25519_import_private_key(void)
  20061. {
  20062. EXPECT_DECLS;
  20063. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  20064. ed25519_key key;
  20065. WC_RNG rng;
  20066. const byte privKey[] = "Ed25519PrivateKeyUnitTest.....\n";
  20067. const byte pubKey[] = "Ed25519PublicKeyUnitTest......\n";
  20068. word32 privKeySz = sizeof(privKey);
  20069. word32 pubKeySz = sizeof(pubKey);
  20070. #ifdef HAVE_ED25519_KEY_EXPORT
  20071. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  20072. word32 bothKeysSz = sizeof(bothKeys);
  20073. #endif
  20074. XMEMSET(&key, 0, sizeof(ed25519_key));
  20075. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20076. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20077. ExpectIntEQ(wc_InitRng(&rng), 0);
  20078. #ifdef HAVE_ED25519_MAKE_KEY
  20079. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20080. #endif
  20081. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, privKeySz, pubKey,
  20082. pubKeySz, &key, 1), 0);
  20083. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20084. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20085. #ifdef HAVE_ED25519_KEY_EXPORT
  20086. PRIVATE_KEY_UNLOCK();
  20087. ExpectIntEQ(wc_ed25519_export_private(&key, bothKeys, &bothKeysSz), 0);
  20088. PRIVATE_KEY_LOCK();
  20089. ExpectIntEQ(wc_ed25519_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  20090. &key, 1), 0);
  20091. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20092. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20093. #endif
  20094. /* Test bad args. */
  20095. ExpectIntEQ(wc_ed25519_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  20096. &key), BAD_FUNC_ARG);
  20097. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL,
  20098. pubKeySz, &key), BAD_FUNC_ARG);
  20099. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  20100. pubKeySz, NULL), BAD_FUNC_ARG);
  20101. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz - 1, pubKey,
  20102. pubKeySz, &key), BAD_FUNC_ARG);
  20103. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, pubKey,
  20104. pubKeySz - 1, &key), BAD_FUNC_ARG);
  20105. ExpectIntEQ(wc_ed25519_import_private_key(privKey, privKeySz, NULL, 0,
  20106. &key), BAD_FUNC_ARG);
  20107. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20108. wc_ed25519_free(&key);
  20109. #endif
  20110. return EXPECT_RESULT();
  20111. } /* END test_wc_ed25519_import_private_key */
  20112. /*
  20113. * Testing wc_ed25519_export_public() and wc_ed25519_export_private_only()
  20114. */
  20115. static int test_wc_ed25519_export(void)
  20116. {
  20117. EXPECT_DECLS;
  20118. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20119. ed25519_key key;
  20120. WC_RNG rng;
  20121. byte priv[ED25519_PRV_KEY_SIZE];
  20122. byte pub[ED25519_PUB_KEY_SIZE];
  20123. word32 privSz = sizeof(priv);
  20124. word32 pubSz = sizeof(pub);
  20125. #ifndef HAVE_ED25519_MAKE_KEY
  20126. const byte privKey[] = {
  20127. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20128. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20129. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20130. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20131. };
  20132. const byte pubKey[] = {
  20133. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20134. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20135. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20136. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20137. };
  20138. #endif
  20139. XMEMSET(&key, 0, sizeof(ed25519_key));
  20140. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20141. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20142. ExpectIntEQ(wc_InitRng(&rng), 0);
  20143. #ifdef HAVE_ED25519_MAKE_KEY
  20144. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20145. #else
  20146. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20147. pubKey, sizeof(pubKey), &key, 1), 0);
  20148. #endif
  20149. PRIVATE_KEY_UNLOCK();
  20150. ExpectIntEQ(wc_ed25519_export_public(&key, pub, &pubSz), 0);
  20151. ExpectIntEQ(pubSz, ED25519_KEY_SIZE);
  20152. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  20153. /* Test bad args. */
  20154. ExpectIntEQ(wc_ed25519_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  20155. ExpectIntEQ(wc_ed25519_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  20156. ExpectIntEQ(wc_ed25519_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  20157. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, &privSz), 0);
  20158. ExpectIntEQ(privSz, ED25519_KEY_SIZE);
  20159. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  20160. /* Test bad args. */
  20161. ExpectIntEQ(wc_ed25519_export_private_only(NULL, priv, &privSz),
  20162. BAD_FUNC_ARG);
  20163. ExpectIntEQ(wc_ed25519_export_private_only(&key, NULL, &privSz),
  20164. BAD_FUNC_ARG);
  20165. ExpectIntEQ(wc_ed25519_export_private_only(&key, priv, NULL),
  20166. BAD_FUNC_ARG);
  20167. PRIVATE_KEY_LOCK();
  20168. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20169. wc_ed25519_free(&key);
  20170. #endif
  20171. return EXPECT_RESULT();
  20172. } /* END test_wc_ed25519_export */
  20173. /*
  20174. * Testing wc_ed25519_size()
  20175. */
  20176. static int test_wc_ed25519_size(void)
  20177. {
  20178. EXPECT_DECLS;
  20179. #if defined(HAVE_ED25519)
  20180. ed25519_key key;
  20181. WC_RNG rng;
  20182. #ifndef HAVE_ED25519_MAKE_KEY
  20183. const byte privKey[] = {
  20184. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20185. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20186. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20187. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20188. };
  20189. const byte pubKey[] = {
  20190. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20191. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20192. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20193. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20194. };
  20195. #endif
  20196. XMEMSET(&key, 0, sizeof(ed25519_key));
  20197. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20198. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20199. ExpectIntEQ(wc_InitRng(&rng), 0);
  20200. #ifdef HAVE_ED25519_MAKE_KEY
  20201. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20202. #else
  20203. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20204. pubKey, sizeof(pubKey), &key, 1), 0);
  20205. #endif
  20206. ExpectIntEQ(wc_ed25519_size(&key), ED25519_KEY_SIZE);
  20207. /* Test bad args. */
  20208. ExpectIntEQ(wc_ed25519_size(NULL), BAD_FUNC_ARG);
  20209. ExpectIntEQ(wc_ed25519_sig_size(&key), ED25519_SIG_SIZE);
  20210. /* Test bad args. */
  20211. ExpectIntEQ(wc_ed25519_sig_size(NULL), BAD_FUNC_ARG);
  20212. ExpectIntEQ(wc_ed25519_pub_size(&key), ED25519_PUB_KEY_SIZE);
  20213. /* Test bad args. */
  20214. ExpectIntEQ(wc_ed25519_pub_size(NULL), BAD_FUNC_ARG);
  20215. ExpectIntEQ(wc_ed25519_priv_size(&key), ED25519_PRV_KEY_SIZE);
  20216. /* Test bad args. */
  20217. ExpectIntEQ(wc_ed25519_priv_size(NULL), BAD_FUNC_ARG);
  20218. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20219. wc_ed25519_free(&key);
  20220. #endif
  20221. return EXPECT_RESULT();
  20222. } /* END test_wc_ed25519_size */
  20223. /*
  20224. * Testing wc_ed25519_export_private() and wc_ed25519_export_key()
  20225. */
  20226. static int test_wc_ed25519_exportKey(void)
  20227. {
  20228. EXPECT_DECLS;
  20229. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  20230. WC_RNG rng;
  20231. ed25519_key key;
  20232. byte priv[ED25519_PRV_KEY_SIZE];
  20233. byte pub[ED25519_PUB_KEY_SIZE];
  20234. byte privOnly[ED25519_PRV_KEY_SIZE];
  20235. word32 privSz = sizeof(priv);
  20236. word32 pubSz = sizeof(pub);
  20237. word32 privOnlySz = sizeof(privOnly);
  20238. #ifndef HAVE_ED25519_MAKE_KEY
  20239. const byte privKey[] = {
  20240. 0xf8, 0x55, 0xb7, 0xb6, 0x49, 0x3f, 0x99, 0x9c,
  20241. 0x88, 0xe3, 0xc5, 0x42, 0x6a, 0xa4, 0x47, 0x4a,
  20242. 0xe4, 0x95, 0xda, 0xdb, 0xbf, 0xf8, 0xa7, 0x42,
  20243. 0x9d, 0x0e, 0xe7, 0xd0, 0x57, 0x8f, 0x16, 0x69
  20244. };
  20245. const byte pubKey[] = {
  20246. 0x42, 0x3b, 0x7a, 0xf9, 0x82, 0xcf, 0xf9, 0xdf,
  20247. 0x19, 0xdd, 0xf3, 0xf0, 0x32, 0x29, 0x6d, 0xfa,
  20248. 0xfd, 0x76, 0x4f, 0x68, 0xc2, 0xc2, 0xe0, 0x6c,
  20249. 0x47, 0xae, 0xc2, 0x55, 0x68, 0xac, 0x0d, 0x4d
  20250. };
  20251. #endif
  20252. XMEMSET(&key, 0, sizeof(ed25519_key));
  20253. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20254. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20255. ExpectIntEQ(wc_InitRng(&rng), 0);
  20256. #ifdef HAVE_ED25519_MAKE_KEY
  20257. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20258. #else
  20259. ExpectIntEQ(wc_ed25519_import_private_key_ex(privKey, sizeof(privKey),
  20260. pubKey, sizeof(pubKey), &key, 1), 0);
  20261. #endif
  20262. PRIVATE_KEY_UNLOCK();
  20263. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, &privOnlySz), 0);
  20264. /* Test bad args. */
  20265. ExpectIntEQ(wc_ed25519_export_private(NULL, privOnly, &privOnlySz),
  20266. BAD_FUNC_ARG);
  20267. ExpectIntEQ(wc_ed25519_export_private(&key, NULL, &privOnlySz),
  20268. BAD_FUNC_ARG);
  20269. ExpectIntEQ(wc_ed25519_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  20270. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  20271. /* Test bad args. */
  20272. ExpectIntEQ(wc_ed25519_export_key(NULL, priv, &privSz, pub, &pubSz),
  20273. BAD_FUNC_ARG);
  20274. ExpectIntEQ(wc_ed25519_export_key(&key, NULL, &privSz, pub, &pubSz),
  20275. BAD_FUNC_ARG);
  20276. ExpectIntEQ(wc_ed25519_export_key(&key, priv, NULL, pub, &pubSz),
  20277. BAD_FUNC_ARG);
  20278. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, NULL, &pubSz),
  20279. BAD_FUNC_ARG);
  20280. ExpectIntEQ(wc_ed25519_export_key(&key, priv, &privSz, pub, NULL),
  20281. BAD_FUNC_ARG);
  20282. PRIVATE_KEY_LOCK();
  20283. /* Cross check output. */
  20284. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  20285. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20286. wc_ed25519_free(&key);
  20287. #endif
  20288. return EXPECT_RESULT();
  20289. } /* END test_wc_ed25519_exportKey */
  20290. /*
  20291. * Testing wc_Ed25519PublicKeyToDer
  20292. */
  20293. static int test_wc_Ed25519PublicKeyToDer(void)
  20294. {
  20295. EXPECT_DECLS;
  20296. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  20297. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  20298. ed25519_key key;
  20299. byte derBuf[1024];
  20300. XMEMSET(&key, 0, sizeof(ed25519_key));
  20301. /* Test bad args */
  20302. ExpectIntEQ(wc_Ed25519PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  20303. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20304. ExpectIntEQ(wc_Ed25519PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  20305. wc_ed25519_free(&key);
  20306. /* Test good args */
  20307. if (EXPECT_SUCCESS()) {
  20308. WC_RNG rng;
  20309. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20310. ExpectIntEQ(wc_ed25519_init(&key), 0);
  20311. ExpectIntEQ(wc_InitRng(&rng), 0);
  20312. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &key), 0);
  20313. ExpectIntGT(wc_Ed25519PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  20314. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20315. wc_ed25519_free(&key);
  20316. }
  20317. #endif
  20318. return EXPECT_RESULT();
  20319. } /* END testing wc_Ed25519PublicKeyToDer */
  20320. /*
  20321. * Testing wc_curve25519_init and wc_curve25519_free.
  20322. */
  20323. static int test_wc_curve25519_init(void)
  20324. {
  20325. EXPECT_DECLS;
  20326. #if defined(HAVE_CURVE25519)
  20327. curve25519_key key;
  20328. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20329. /* Test bad args for wc_curve25519_init */
  20330. ExpectIntEQ(wc_curve25519_init(NULL), BAD_FUNC_ARG);
  20331. /* Test good args for wc_curve_25519_free */
  20332. wc_curve25519_free(&key);
  20333. /* Test bad args for wc_curve25519 free. */
  20334. wc_curve25519_free(NULL);
  20335. #endif
  20336. return EXPECT_RESULT();
  20337. } /* END test_wc_curve25519_init and wc_curve_25519_free*/
  20338. /*
  20339. * Testing test_wc_curve25519_size.
  20340. */
  20341. static int test_wc_curve25519_size(void)
  20342. {
  20343. EXPECT_DECLS;
  20344. #if defined(HAVE_CURVE25519)
  20345. curve25519_key key;
  20346. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20347. /* Test good args for wc_curve25519_size */
  20348. ExpectIntEQ(wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  20349. /* Test bad args for wc_curve25519_size */
  20350. ExpectIntEQ(wc_curve25519_size(NULL), 0);
  20351. wc_curve25519_free(&key);
  20352. #endif
  20353. return EXPECT_RESULT();
  20354. } /* END test_wc_curve25519_size*/
  20355. /*
  20356. * Testing test_wc_curve25519_export_key_raw().
  20357. */
  20358. static int test_wc_curve25519_export_key_raw(void)
  20359. {
  20360. EXPECT_DECLS;
  20361. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  20362. curve25519_key key;
  20363. WC_RNG rng;
  20364. byte privateKey[CURVE25519_KEYSIZE];
  20365. byte publicKey[CURVE25519_KEYSIZE];
  20366. word32 prvkSz;
  20367. word32 pubkSz;
  20368. byte prik[CURVE25519_KEYSIZE];
  20369. byte pubk[CURVE25519_KEYSIZE];
  20370. word32 prksz;
  20371. word32 pbksz;
  20372. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20373. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20374. ExpectIntEQ(wc_InitRng(&rng), 0);
  20375. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20376. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  20377. prvkSz = CURVE25519_KEYSIZE;
  20378. pubkSz = CURVE25519_KEYSIZE;
  20379. ExpectIntEQ(wc_curve25519_export_key_raw(NULL, privateKey, &prvkSz,
  20380. publicKey, &pubkSz), BAD_FUNC_ARG);
  20381. prvkSz = CURVE25519_KEYSIZE;
  20382. pubkSz = CURVE25519_KEYSIZE;
  20383. ExpectIntEQ(wc_curve25519_export_key_raw(&key, NULL, &prvkSz, publicKey,
  20384. &pubkSz), BAD_FUNC_ARG);
  20385. prvkSz = CURVE25519_KEYSIZE;
  20386. pubkSz = CURVE25519_KEYSIZE;
  20387. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, NULL,
  20388. publicKey, &pubkSz), BAD_FUNC_ARG);
  20389. /* prvkSz = CURVE25519_KEYSIZE; */
  20390. pubkSz = CURVE25519_KEYSIZE;
  20391. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20392. NULL, &pubkSz), BAD_FUNC_ARG);
  20393. prvkSz = CURVE25519_KEYSIZE;
  20394. pubkSz = CURVE25519_KEYSIZE;
  20395. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20396. publicKey, NULL), BAD_FUNC_ARG);
  20397. /* cross-testing */
  20398. prksz = CURVE25519_KEYSIZE;
  20399. ExpectIntEQ(wc_curve25519_export_private_raw(&key, prik, &prksz), 0);
  20400. pbksz = CURVE25519_KEYSIZE;
  20401. ExpectIntEQ(wc_curve25519_export_public(&key, pubk, &pbksz), 0);
  20402. prvkSz = CURVE25519_KEYSIZE;
  20403. /* pubkSz = CURVE25519_KEYSIZE; */
  20404. ExpectIntEQ(wc_curve25519_export_key_raw(&key, privateKey, &prvkSz,
  20405. publicKey, &pubkSz), 0);
  20406. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  20407. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  20408. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20409. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20410. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  20411. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  20412. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20413. wc_curve25519_free(&key);
  20414. #endif
  20415. return EXPECT_RESULT();
  20416. } /* end of test_wc_curve25519_export_key_raw */
  20417. /*
  20418. * Testing test_wc_curve25519_export_key_raw_ex().
  20419. */
  20420. static int test_wc_curve25519_export_key_raw_ex(void)
  20421. {
  20422. EXPECT_DECLS;
  20423. #if defined(HAVE_CURVE25519) && defined(HAVE_CURVE25519_KEY_EXPORT)
  20424. curve25519_key key;
  20425. WC_RNG rng;
  20426. byte privateKey[CURVE25519_KEYSIZE];
  20427. byte publicKey[CURVE25519_KEYSIZE];
  20428. word32 prvkSz;
  20429. word32 pubkSz;
  20430. byte prik[CURVE25519_KEYSIZE];
  20431. byte pubk[CURVE25519_KEYSIZE];
  20432. word32 prksz;
  20433. word32 pbksz;
  20434. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20435. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20436. ExpectIntEQ(wc_InitRng(&rng), 0);
  20437. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20438. /* bad-argument-test cases - target function should return BAD_FUNC_ARG */
  20439. prvkSz = CURVE25519_KEYSIZE;
  20440. pubkSz = CURVE25519_KEYSIZE;
  20441. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  20442. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20443. prvkSz = CURVE25519_KEYSIZE;
  20444. pubkSz = CURVE25519_KEYSIZE;
  20445. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  20446. &prvkSz, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20447. prvkSz = CURVE25519_KEYSIZE;
  20448. pubkSz = CURVE25519_KEYSIZE;
  20449. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20450. NULL, publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20451. /* prvkSz = CURVE25519_KEYSIZE; */
  20452. pubkSz = CURVE25519_KEYSIZE;
  20453. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20454. &prvkSz, NULL, &pubkSz, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20455. prvkSz = CURVE25519_KEYSIZE;
  20456. pubkSz = CURVE25519_KEYSIZE;
  20457. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20458. &prvkSz, publicKey, NULL, EC25519_LITTLE_ENDIAN), BAD_FUNC_ARG);
  20459. prvkSz = CURVE25519_KEYSIZE;
  20460. /* pubkSz = CURVE25519_KEYSIZE; */
  20461. ExpectIntEQ(wc_curve25519_export_key_raw_ex(NULL, privateKey,
  20462. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20463. prvkSz = CURVE25519_KEYSIZE;
  20464. pubkSz = CURVE25519_KEYSIZE;
  20465. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, NULL,
  20466. &prvkSz, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20467. prvkSz = CURVE25519_KEYSIZE;
  20468. pubkSz = CURVE25519_KEYSIZE;
  20469. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20470. NULL, publicKey, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20471. /* prvkSz = CURVE25519_KEYSIZE; */
  20472. pubkSz = CURVE25519_KEYSIZE;
  20473. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20474. &prvkSz, NULL, &pubkSz, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20475. prvkSz = CURVE25519_KEYSIZE;
  20476. pubkSz = CURVE25519_KEYSIZE;
  20477. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey,
  20478. &prvkSz, publicKey, NULL, EC25519_BIG_ENDIAN), BAD_FUNC_ARG);
  20479. /* illegal value for endian */
  20480. prvkSz = CURVE25519_KEYSIZE;
  20481. /* pubkSz = CURVE25519_KEYSIZE; */
  20482. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20483. publicKey, NULL, EC25519_BIG_ENDIAN + 10), BAD_FUNC_ARG);
  20484. /* cross-testing */
  20485. prksz = CURVE25519_KEYSIZE;
  20486. ExpectIntEQ(wc_curve25519_export_private_raw( &key, prik, &prksz), 0);
  20487. pbksz = CURVE25519_KEYSIZE;
  20488. ExpectIntEQ(wc_curve25519_export_public( &key, pubk, &pbksz), 0);
  20489. prvkSz = CURVE25519_KEYSIZE;
  20490. /* pubkSz = CURVE25519_KEYSIZE; */
  20491. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20492. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  20493. ExpectIntEQ(prksz, CURVE25519_KEYSIZE);
  20494. ExpectIntEQ(pbksz, CURVE25519_KEYSIZE);
  20495. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20496. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20497. ExpectIntEQ(XMEMCMP(privateKey, prik, CURVE25519_KEYSIZE), 0);
  20498. ExpectIntEQ(XMEMCMP(publicKey, pubk, CURVE25519_KEYSIZE), 0);
  20499. ExpectIntEQ(wc_curve25519_export_key_raw_ex(&key, privateKey, &prvkSz,
  20500. publicKey, &pubkSz, EC25519_LITTLE_ENDIAN), 0);
  20501. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20502. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20503. /* try once with another endian */
  20504. prvkSz = CURVE25519_KEYSIZE;
  20505. pubkSz = CURVE25519_KEYSIZE;
  20506. ExpectIntEQ(wc_curve25519_export_key_raw_ex( &key, privateKey, &prvkSz,
  20507. publicKey, &pubkSz, EC25519_BIG_ENDIAN), 0);
  20508. ExpectIntEQ(prvkSz, CURVE25519_KEYSIZE);
  20509. ExpectIntEQ(pubkSz, CURVE25519_KEYSIZE);
  20510. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20511. wc_curve25519_free(&key);
  20512. #endif
  20513. return EXPECT_RESULT();
  20514. } /* end of test_wc_curve25519_export_key_raw_ex */
  20515. /*
  20516. * Testing wc_curve25519_make_key
  20517. */
  20518. static int test_wc_curve25519_make_key(void)
  20519. {
  20520. EXPECT_DECLS;
  20521. #if defined(HAVE_CURVE25519)
  20522. curve25519_key key;
  20523. WC_RNG rng;
  20524. int keysize;
  20525. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20526. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20527. ExpectIntEQ(wc_InitRng(&rng), 0);
  20528. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20529. ExpectIntEQ(keysize = wc_curve25519_size(&key), CURVE25519_KEYSIZE);
  20530. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, &key), 0);
  20531. /* test bad cases*/
  20532. ExpectIntEQ(wc_curve25519_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  20533. ExpectIntEQ(wc_curve25519_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  20534. ExpectIntEQ(wc_curve25519_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  20535. ExpectIntEQ(wc_curve25519_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  20536. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20537. wc_curve25519_free(&key);
  20538. #endif
  20539. return EXPECT_RESULT();
  20540. } /* END test_wc_curve25519_make_key*/
  20541. /*
  20542. * Testing wc_curve25519_shared_secret_ex
  20543. */
  20544. static int test_wc_curve25519_shared_secret_ex(void)
  20545. {
  20546. EXPECT_DECLS;
  20547. #if defined(HAVE_CURVE25519)
  20548. curve25519_key private_key;
  20549. curve25519_key public_key;
  20550. WC_RNG rng;
  20551. byte out[CURVE25519_KEYSIZE];
  20552. word32 outLen = sizeof(out);
  20553. int endian = EC25519_BIG_ENDIAN;
  20554. ExpectIntEQ(wc_curve25519_init(&private_key), 0);
  20555. ExpectIntEQ(wc_curve25519_init(&public_key), 0);
  20556. ExpectIntEQ(wc_InitRng(&rng), 0);
  20557. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &private_key),
  20558. 0);
  20559. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &public_key),
  20560. 0);
  20561. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20562. &outLen, endian), 0);
  20563. /* test bad cases*/
  20564. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  20565. BAD_FUNC_ARG);
  20566. ExpectIntEQ(wc_curve25519_shared_secret_ex(NULL, &public_key, out, &outLen,
  20567. endian), BAD_FUNC_ARG);
  20568. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, NULL, out, &outLen,
  20569. endian), BAD_FUNC_ARG);
  20570. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, NULL,
  20571. &outLen, endian), BAD_FUNC_ARG);
  20572. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20573. NULL, endian), BAD_FUNC_ARG);
  20574. /* curve25519.c is checking for public_key size less than or equal to 0x7f,
  20575. * increasing to 0x8f checks for error being returned*/
  20576. public_key.p.point[CURVE25519_KEYSIZE-1] = 0x8F;
  20577. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20578. &outLen, endian), ECC_BAD_ARG_E);
  20579. outLen = outLen - 2;
  20580. ExpectIntEQ(wc_curve25519_shared_secret_ex(&private_key, &public_key, out,
  20581. &outLen, endian), BAD_FUNC_ARG);
  20582. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20583. wc_curve25519_free(&private_key);
  20584. wc_curve25519_free(&public_key);
  20585. #endif
  20586. return EXPECT_RESULT();
  20587. } /* END test_wc_curve25519_shared_secret_ex*/
  20588. /*
  20589. * Testing wc_curve25519_make_pub
  20590. */
  20591. static int test_wc_curve25519_make_pub(void)
  20592. {
  20593. EXPECT_DECLS;
  20594. #ifdef HAVE_CURVE25519
  20595. curve25519_key key;
  20596. WC_RNG rng;
  20597. byte out[CURVE25519_KEYSIZE];
  20598. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20599. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20600. ExpectIntEQ(wc_InitRng(&rng), 0);
  20601. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20602. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(out), out,
  20603. (int)sizeof(key.k), key.k), 0);
  20604. /* test bad cases*/
  20605. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof(key.k) - 1, key.k,
  20606. (int)sizeof out, out), ECC_BAD_ARG_E);
  20607. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20608. NULL), ECC_BAD_ARG_E);
  20609. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out - 1, out,
  20610. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  20611. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, NULL,
  20612. (int)sizeof(key.k), key.k), ECC_BAD_ARG_E);
  20613. /* verify clamping test */
  20614. key.k[0] |= ~248;
  20615. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20616. key.k), ECC_BAD_ARG_E);
  20617. key.k[0] &= 248;
  20618. /* repeat the expected-to-succeed test. */
  20619. ExpectIntEQ(wc_curve25519_make_pub((int)sizeof out, out, (int)sizeof(key.k),
  20620. key.k), 0);
  20621. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20622. wc_curve25519_free(&key);
  20623. #endif
  20624. return EXPECT_RESULT();
  20625. } /* END test_wc_curve25519_make_pub */
  20626. /*
  20627. * Testing test_wc_curve25519_export_public_ex
  20628. */
  20629. static int test_wc_curve25519_export_public_ex(void)
  20630. {
  20631. EXPECT_DECLS;
  20632. #if defined(HAVE_CURVE25519)
  20633. curve25519_key key;
  20634. WC_RNG rng;
  20635. byte out[CURVE25519_KEYSIZE];
  20636. word32 outLen = sizeof(out);
  20637. int endian = EC25519_BIG_ENDIAN;
  20638. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20639. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20640. ExpectIntEQ(wc_InitRng(&rng), 0);
  20641. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20642. ExpectIntEQ(wc_curve25519_export_public(&key, out, &outLen), 0);
  20643. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian), 0);
  20644. /* test bad cases*/
  20645. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, NULL, NULL, endian),
  20646. BAD_FUNC_ARG);
  20647. ExpectIntEQ(wc_curve25519_export_public_ex(NULL, out, &outLen, endian),
  20648. BAD_FUNC_ARG);
  20649. ExpectIntEQ(wc_curve25519_export_public_ex(&key, NULL, &outLen, endian),
  20650. BAD_FUNC_ARG);
  20651. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, NULL, endian),
  20652. BAD_FUNC_ARG);
  20653. outLen = outLen - 2;
  20654. ExpectIntEQ(wc_curve25519_export_public_ex(&key, out, &outLen, endian),
  20655. ECC_BAD_ARG_E);
  20656. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20657. wc_curve25519_free(&key);
  20658. #endif
  20659. return EXPECT_RESULT();
  20660. } /* END test_wc_curve25519_export_public_ex*/
  20661. /*
  20662. * Testing test_wc_curve25519_import_private_raw_ex
  20663. */
  20664. static int test_wc_curve25519_import_private_raw_ex(void)
  20665. {
  20666. EXPECT_DECLS;
  20667. #if defined(HAVE_CURVE25519)
  20668. curve25519_key key;
  20669. WC_RNG rng;
  20670. byte priv[CURVE25519_KEYSIZE];
  20671. byte pub[CURVE25519_KEYSIZE];
  20672. word32 privSz = sizeof(priv);
  20673. word32 pubSz = sizeof(pub);
  20674. int endian = EC25519_BIG_ENDIAN;
  20675. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20676. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20677. ExpectIntEQ(wc_InitRng(&rng), 0);
  20678. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20679. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, priv, &privSz,
  20680. endian), 0);
  20681. ExpectIntEQ(wc_curve25519_export_public(&key, pub, &pubSz), 0);
  20682. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20683. &key, endian), 0);
  20684. /* test bad cases*/
  20685. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, 0, NULL, 0, NULL,
  20686. endian), BAD_FUNC_ARG);
  20687. ExpectIntEQ(wc_curve25519_import_private_raw_ex(NULL, privSz, pub, pubSz,
  20688. &key, endian), BAD_FUNC_ARG);
  20689. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, NULL, pubSz,
  20690. &key, endian), BAD_FUNC_ARG);
  20691. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20692. NULL, endian), BAD_FUNC_ARG);
  20693. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, 0, pub, pubSz,
  20694. &key, endian), ECC_BAD_ARG_E);
  20695. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, 0,
  20696. &key, endian), ECC_BAD_ARG_E);
  20697. ExpectIntEQ(wc_curve25519_import_private_raw_ex(priv, privSz, pub, pubSz,
  20698. &key, EC25519_LITTLE_ENDIAN), 0);
  20699. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20700. wc_curve25519_free(&key);
  20701. #endif
  20702. return EXPECT_RESULT();
  20703. } /* END test_wc_curve25519_import_private_raw_ex*/
  20704. /*
  20705. * Testing test_wc_curve25519_import_private
  20706. */
  20707. static int test_wc_curve25519_import_private(void)
  20708. {
  20709. EXPECT_DECLS;
  20710. #if defined(HAVE_CURVE25519)
  20711. curve25519_key key;
  20712. WC_RNG rng;
  20713. byte priv[CURVE25519_KEYSIZE];
  20714. word32 privSz = sizeof(priv);
  20715. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20716. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20717. ExpectIntEQ(wc_InitRng(&rng), 0);
  20718. ExpectIntEQ(wc_curve25519_make_key(&rng, CURVE25519_KEYSIZE, &key), 0);
  20719. ExpectIntEQ(wc_curve25519_export_private_raw(&key, priv, &privSz), 0);
  20720. ExpectIntEQ(wc_curve25519_import_private(priv, privSz, &key), 0);
  20721. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20722. wc_curve25519_free(&key);
  20723. #endif
  20724. return EXPECT_RESULT();
  20725. } /* END test_wc_curve25519_import*/
  20726. /*
  20727. * Testing test_wc_curve25519_export_private_raw_ex
  20728. */
  20729. static int test_wc_curve25519_export_private_raw_ex(void)
  20730. {
  20731. EXPECT_DECLS;
  20732. #if defined(HAVE_CURVE25519)
  20733. curve25519_key key;
  20734. byte out[CURVE25519_KEYSIZE];
  20735. word32 outLen = sizeof(out);
  20736. int endian = EC25519_BIG_ENDIAN;
  20737. ExpectIntEQ(wc_curve25519_init(&key), 0);
  20738. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  20739. 0);
  20740. /* test bad cases*/
  20741. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, NULL, NULL, endian),
  20742. BAD_FUNC_ARG);
  20743. ExpectIntEQ(wc_curve25519_export_private_raw_ex(NULL, out, &outLen, endian),
  20744. BAD_FUNC_ARG);
  20745. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, NULL, &outLen,
  20746. endian), BAD_FUNC_ARG);
  20747. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, NULL, endian),
  20748. BAD_FUNC_ARG);
  20749. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen,
  20750. EC25519_LITTLE_ENDIAN), 0);
  20751. outLen = outLen - 2;
  20752. ExpectIntEQ(wc_curve25519_export_private_raw_ex(&key, out, &outLen, endian),
  20753. ECC_BAD_ARG_E);
  20754. wc_curve25519_free(&key);
  20755. #endif
  20756. return EXPECT_RESULT();
  20757. } /* END test_wc_curve25519_export_private_raw_ex*/
  20758. /*
  20759. * Testing wc_ed448_make_key().
  20760. */
  20761. static int test_wc_ed448_make_key(void)
  20762. {
  20763. EXPECT_DECLS;
  20764. #if defined(HAVE_ED448)
  20765. ed448_key key;
  20766. WC_RNG rng;
  20767. unsigned char pubkey[ED448_PUB_KEY_SIZE];
  20768. XMEMSET(&key, 0, sizeof(ed448_key));
  20769. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20770. ExpectIntEQ(wc_ed448_init(&key), 0);
  20771. ExpectIntEQ(wc_InitRng(&rng), 0);
  20772. ExpectIntEQ(wc_ed448_make_public(&key, pubkey, sizeof(pubkey)),
  20773. ECC_PRIV_KEY_E);
  20774. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20775. /* Test bad args. */
  20776. ExpectIntEQ(wc_ed448_make_key(NULL, ED448_KEY_SIZE, &key), BAD_FUNC_ARG);
  20777. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, NULL), BAD_FUNC_ARG);
  20778. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE - 1, &key),
  20779. BAD_FUNC_ARG);
  20780. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE + 1, &key),
  20781. BAD_FUNC_ARG);
  20782. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20783. wc_ed448_free(&key);
  20784. #endif
  20785. return EXPECT_RESULT();
  20786. } /* END test_wc_ed448_make_key */
  20787. /*
  20788. * Testing wc_ed448_init()
  20789. */
  20790. static int test_wc_ed448_init(void)
  20791. {
  20792. EXPECT_DECLS;
  20793. #if defined(HAVE_ED448)
  20794. ed448_key key;
  20795. XMEMSET(&key, 0, sizeof(ed448_key));
  20796. ExpectIntEQ(wc_ed448_init(&key), 0);
  20797. /* Test bad args. */
  20798. ExpectIntEQ(wc_ed448_init(NULL), BAD_FUNC_ARG);
  20799. wc_ed448_free(&key);
  20800. #endif
  20801. return EXPECT_RESULT();
  20802. } /* END test_wc_ed448_init */
  20803. /*
  20804. * Test wc_ed448_sign_msg() and wc_ed448_verify_msg()
  20805. */
  20806. static int test_wc_ed448_sign_msg(void)
  20807. {
  20808. EXPECT_DECLS;
  20809. #if defined(HAVE_ED448) && defined(HAVE_ED448_SIGN)
  20810. ed448_key key;
  20811. WC_RNG rng;
  20812. byte msg[] = "Everybody gets Friday off.\n";
  20813. byte sig[ED448_SIG_SIZE];
  20814. word32 msglen = sizeof(msg);
  20815. word32 siglen = sizeof(sig);
  20816. word32 badSigLen = sizeof(sig) - 1;
  20817. #ifdef HAVE_ED448_VERIFY
  20818. int verify_ok = 0; /*1 = Verify success.*/
  20819. #endif
  20820. /* Initialize stack variables. */
  20821. XMEMSET(&key, 0, sizeof(ed448_key));
  20822. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20823. XMEMSET(sig, 0, siglen);
  20824. /* Initialize key. */
  20825. ExpectIntEQ(wc_ed448_init(&key), 0);
  20826. ExpectIntEQ(wc_InitRng(&rng), 0);
  20827. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20828. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, &key, NULL, 0), 0);
  20829. ExpectIntEQ(siglen, ED448_SIG_SIZE);
  20830. /* Test bad args. */
  20831. ExpectIntEQ(wc_ed448_sign_msg(NULL, msglen, sig, &siglen, &key, NULL, 0),
  20832. BAD_FUNC_ARG);
  20833. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, NULL, &siglen, &key, NULL, 0),
  20834. BAD_FUNC_ARG);
  20835. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, NULL, &key, NULL, 0),
  20836. BAD_FUNC_ARG);
  20837. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &siglen, NULL, NULL, 0),
  20838. BAD_FUNC_ARG);
  20839. ExpectIntEQ(wc_ed448_sign_msg(msg, msglen, sig, &badSigLen, &key, NULL, 0),
  20840. BUFFER_E);
  20841. ExpectIntEQ(badSigLen, ED448_SIG_SIZE);
  20842. badSigLen -= 1;
  20843. #ifdef HAVE_ED448_VERIFY
  20844. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok, &key,
  20845. NULL, 0), 0);
  20846. ExpectIntEQ(verify_ok, 1);
  20847. /* Test bad args. */
  20848. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen - 1, msg, msglen, &verify_ok,
  20849. &key, NULL, 0), BAD_FUNC_ARG);
  20850. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen + 1, msg, msglen, &verify_ok,
  20851. &key, NULL, 0), BAD_FUNC_ARG);
  20852. ExpectIntEQ(wc_ed448_verify_msg(NULL, siglen, msg, msglen, &verify_ok,
  20853. &key, NULL, 0), BAD_FUNC_ARG);
  20854. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, NULL, msglen, &verify_ok,
  20855. &key, NULL, 0), BAD_FUNC_ARG);
  20856. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, NULL,
  20857. &key, NULL, 0), BAD_FUNC_ARG);
  20858. ExpectIntEQ(wc_ed448_verify_msg(sig, siglen, msg, msglen, &verify_ok,
  20859. NULL, NULL, 0), BAD_FUNC_ARG);
  20860. ExpectIntEQ(wc_ed448_verify_msg(sig, badSigLen, msg, msglen, &verify_ok,
  20861. &key, NULL, 0), BAD_FUNC_ARG);
  20862. #endif /* Verify. */
  20863. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20864. wc_ed448_free(&key);
  20865. #endif
  20866. return EXPECT_RESULT();
  20867. } /* END test_wc_ed448_sign_msg */
  20868. /*
  20869. * Testing wc_ed448_import_public()
  20870. */
  20871. static int test_wc_ed448_import_public(void)
  20872. {
  20873. EXPECT_DECLS;
  20874. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20875. ed448_key pubKey;
  20876. WC_RNG rng;
  20877. const byte in[] =
  20878. "Ed448PublicKeyUnitTest.................................\n";
  20879. word32 inlen = sizeof(in);
  20880. XMEMSET(&pubKey, 0, sizeof(ed448_key));
  20881. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20882. ExpectIntEQ(wc_ed448_init(&pubKey), 0);
  20883. ExpectIntEQ(wc_InitRng(&rng), 0);
  20884. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &pubKey), 0);
  20885. ExpectIntEQ(wc_ed448_import_public_ex(in, inlen, &pubKey, 1), 0);
  20886. ExpectIntEQ(XMEMCMP(in, pubKey.p, inlen), 0);
  20887. /* Test bad args. */
  20888. ExpectIntEQ(wc_ed448_import_public(NULL, inlen, &pubKey), BAD_FUNC_ARG);
  20889. ExpectIntEQ(wc_ed448_import_public(in, inlen, NULL), BAD_FUNC_ARG);
  20890. ExpectIntEQ(wc_ed448_import_public(in, inlen - 1, &pubKey), BAD_FUNC_ARG);
  20891. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20892. wc_ed448_free(&pubKey);
  20893. #endif
  20894. return EXPECT_RESULT();
  20895. } /* END wc_ed448_import_public */
  20896. /*
  20897. * Testing wc_ed448_import_private_key()
  20898. */
  20899. static int test_wc_ed448_import_private_key(void)
  20900. {
  20901. EXPECT_DECLS;
  20902. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  20903. ed448_key key;
  20904. WC_RNG rng;
  20905. const byte privKey[] =
  20906. "Ed448PrivateKeyUnitTest................................\n";
  20907. const byte pubKey[] =
  20908. "Ed448PublicKeyUnitTest.................................\n";
  20909. word32 privKeySz = sizeof(privKey);
  20910. word32 pubKeySz = sizeof(pubKey);
  20911. #ifdef HAVE_ED448_KEY_EXPORT
  20912. byte bothKeys[sizeof(privKey) + sizeof(pubKey)];
  20913. word32 bothKeysSz = sizeof(bothKeys);
  20914. #endif
  20915. XMEMSET(&key, 0, sizeof(ed448_key));
  20916. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20917. ExpectIntEQ(wc_ed448_init(&key), 0);
  20918. ExpectIntEQ(wc_InitRng(&rng), 0);
  20919. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20920. ExpectIntEQ(wc_ed448_import_private_key_ex(privKey, privKeySz, pubKey,
  20921. pubKeySz, &key, 1), 0);
  20922. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20923. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20924. #ifdef HAVE_ED448_KEY_EXPORT
  20925. PRIVATE_KEY_UNLOCK();
  20926. ExpectIntEQ(wc_ed448_export_private(&key, bothKeys, &bothKeysSz), 0);
  20927. PRIVATE_KEY_LOCK();
  20928. ExpectIntEQ(wc_ed448_import_private_key_ex(bothKeys, bothKeysSz, NULL, 0,
  20929. &key, 1), 0);
  20930. ExpectIntEQ(XMEMCMP(pubKey, key.p, privKeySz), 0);
  20931. ExpectIntEQ(XMEMCMP(privKey, key.k, pubKeySz), 0);
  20932. #endif
  20933. /* Test bad args. */
  20934. ExpectIntEQ(wc_ed448_import_private_key(NULL, privKeySz, pubKey, pubKeySz,
  20935. &key), BAD_FUNC_ARG);
  20936. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, pubKeySz,
  20937. &key), BAD_FUNC_ARG);
  20938. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20939. pubKeySz, NULL), BAD_FUNC_ARG);
  20940. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz - 1, pubKey,
  20941. pubKeySz, &key), BAD_FUNC_ARG);
  20942. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, pubKey,
  20943. pubKeySz - 1, &key), BAD_FUNC_ARG);
  20944. ExpectIntEQ(wc_ed448_import_private_key(privKey, privKeySz, NULL, 0, &key),
  20945. BAD_FUNC_ARG);
  20946. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20947. wc_ed448_free(&key);
  20948. #endif
  20949. return EXPECT_RESULT();
  20950. } /* END test_wc_ed448_import_private_key */
  20951. /*
  20952. * Testing wc_ed448_export_public() and wc_ed448_export_private_only()
  20953. */
  20954. static int test_wc_ed448_export(void)
  20955. {
  20956. EXPECT_DECLS;
  20957. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  20958. ed448_key key;
  20959. WC_RNG rng;
  20960. byte priv[ED448_PRV_KEY_SIZE];
  20961. byte pub[ED448_PUB_KEY_SIZE];
  20962. word32 privSz = sizeof(priv);
  20963. word32 pubSz = sizeof(pub);
  20964. XMEMSET(&key, 0, sizeof(ed448_key));
  20965. XMEMSET(&rng, 0, sizeof(WC_RNG));
  20966. ExpectIntEQ(wc_ed448_init(&key), 0);
  20967. ExpectIntEQ(wc_InitRng(&rng), 0);
  20968. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  20969. ExpectIntEQ(wc_ed448_export_public(&key, pub, &pubSz), 0);
  20970. ExpectIntEQ(pubSz, ED448_KEY_SIZE);
  20971. ExpectIntEQ(XMEMCMP(key.p, pub, pubSz), 0);
  20972. /* Test bad args. */
  20973. ExpectIntEQ(wc_ed448_export_public(NULL, pub, &pubSz), BAD_FUNC_ARG);
  20974. ExpectIntEQ(wc_ed448_export_public(&key, NULL, &pubSz), BAD_FUNC_ARG);
  20975. ExpectIntEQ(wc_ed448_export_public(&key, pub, NULL), BAD_FUNC_ARG);
  20976. PRIVATE_KEY_UNLOCK();
  20977. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, &privSz), 0);
  20978. ExpectIntEQ(privSz, ED448_KEY_SIZE);
  20979. ExpectIntEQ(XMEMCMP(key.k, priv, privSz), 0);
  20980. /* Test bad args. */
  20981. ExpectIntEQ(wc_ed448_export_private_only(NULL, priv, &privSz),
  20982. BAD_FUNC_ARG);
  20983. ExpectIntEQ(wc_ed448_export_private_only(&key, NULL, &privSz),
  20984. BAD_FUNC_ARG);
  20985. ExpectIntEQ(wc_ed448_export_private_only(&key, priv, NULL), BAD_FUNC_ARG);
  20986. PRIVATE_KEY_LOCK();
  20987. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  20988. wc_ed448_free(&key);
  20989. #endif
  20990. return EXPECT_RESULT();
  20991. } /* END test_wc_ed448_export */
  20992. /*
  20993. * Testing wc_ed448_size()
  20994. */
  20995. static int test_wc_ed448_size(void)
  20996. {
  20997. EXPECT_DECLS;
  20998. #if defined(HAVE_ED448)
  20999. ed448_key key;
  21000. WC_RNG rng;
  21001. XMEMSET(&key, 0, sizeof(ed448_key));
  21002. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21003. ExpectIntEQ(wc_ed448_init(&key), 0);
  21004. ExpectIntEQ(wc_InitRng(&rng), 0);
  21005. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21006. ExpectIntEQ(wc_ed448_size(&key), ED448_KEY_SIZE);
  21007. /* Test bad args. */
  21008. ExpectIntEQ(wc_ed448_size(NULL), BAD_FUNC_ARG);
  21009. ExpectIntEQ(wc_ed448_sig_size(&key), ED448_SIG_SIZE);
  21010. /* Test bad args. */
  21011. ExpectIntEQ(wc_ed448_sig_size(NULL), BAD_FUNC_ARG);
  21012. ExpectIntEQ(wc_ed448_pub_size(&key), ED448_PUB_KEY_SIZE);
  21013. /* Test bad args. */
  21014. ExpectIntEQ(wc_ed448_pub_size(NULL), BAD_FUNC_ARG);
  21015. ExpectIntEQ(wc_ed448_priv_size(&key), ED448_PRV_KEY_SIZE);
  21016. /* Test bad args. */
  21017. ExpectIntEQ(wc_ed448_priv_size(NULL), BAD_FUNC_ARG);
  21018. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21019. wc_ed448_free(&key);
  21020. #endif
  21021. return EXPECT_RESULT();
  21022. } /* END test_wc_ed448_size */
  21023. /*
  21024. * Testing wc_ed448_export_private() and wc_ed448_export_key()
  21025. */
  21026. static int test_wc_ed448_exportKey(void)
  21027. {
  21028. EXPECT_DECLS;
  21029. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  21030. ed448_key key;
  21031. WC_RNG rng;
  21032. byte priv[ED448_PRV_KEY_SIZE];
  21033. byte pub[ED448_PUB_KEY_SIZE];
  21034. byte privOnly[ED448_PRV_KEY_SIZE];
  21035. word32 privSz = sizeof(priv);
  21036. word32 pubSz = sizeof(pub);
  21037. word32 privOnlySz = sizeof(privOnly);
  21038. XMEMSET(&key, 0, sizeof(ed448_key));
  21039. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21040. ExpectIntEQ(wc_ed448_init(&key), 0);
  21041. ExpectIntEQ(wc_InitRng(&rng), 0);
  21042. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21043. PRIVATE_KEY_UNLOCK();
  21044. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, &privOnlySz), 0);
  21045. /* Test bad args. */
  21046. ExpectIntEQ(wc_ed448_export_private(NULL, privOnly, &privOnlySz),
  21047. BAD_FUNC_ARG);
  21048. ExpectIntEQ(wc_ed448_export_private(&key, NULL, &privOnlySz), BAD_FUNC_ARG);
  21049. ExpectIntEQ(wc_ed448_export_private(&key, privOnly, NULL), BAD_FUNC_ARG);
  21050. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, &pubSz), 0);
  21051. /* Test bad args. */
  21052. ExpectIntEQ(wc_ed448_export_key(NULL, priv, &privSz, pub, &pubSz),
  21053. BAD_FUNC_ARG);
  21054. ExpectIntEQ(wc_ed448_export_key(&key, NULL, &privSz, pub, &pubSz),
  21055. BAD_FUNC_ARG);
  21056. ExpectIntEQ(wc_ed448_export_key(&key, priv, NULL, pub, &pubSz),
  21057. BAD_FUNC_ARG);
  21058. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, NULL, &pubSz),
  21059. BAD_FUNC_ARG);
  21060. ExpectIntEQ(wc_ed448_export_key(&key, priv, &privSz, pub, NULL),
  21061. BAD_FUNC_ARG);
  21062. PRIVATE_KEY_LOCK();
  21063. /* Cross check output. */
  21064. ExpectIntEQ(XMEMCMP(priv, privOnly, privSz), 0);
  21065. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21066. wc_ed448_free(&key);
  21067. #endif
  21068. return EXPECT_RESULT();
  21069. } /* END test_wc_ed448_exportKey */
  21070. /*
  21071. * Testing wc_Ed448PublicKeyToDer
  21072. */
  21073. static int test_wc_Ed448PublicKeyToDer(void)
  21074. {
  21075. EXPECT_DECLS;
  21076. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  21077. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  21078. ed448_key key;
  21079. byte derBuf[1024];
  21080. XMEMSET(&key, 0, sizeof(ed448_key));
  21081. /* Test bad args */
  21082. ExpectIntEQ(wc_Ed448PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  21083. ExpectIntEQ(wc_ed448_init(&key), 0);
  21084. ExpectIntEQ(wc_Ed448PublicKeyToDer(&key, derBuf, 0, 0), BUFFER_E);
  21085. wc_ed448_free(&key);
  21086. /* Test good args */
  21087. if (EXPECT_SUCCESS()) {
  21088. WC_RNG rng;
  21089. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21090. ExpectIntEQ(wc_ed448_init(&key), 0);
  21091. ExpectIntEQ(wc_InitRng(&rng), 0);
  21092. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &key), 0);
  21093. ExpectIntGT(wc_Ed448PublicKeyToDer(&key, derBuf, 1024, 1), 0);
  21094. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21095. wc_ed448_free(&key);
  21096. }
  21097. #endif
  21098. return EXPECT_RESULT();
  21099. } /* END testing wc_Ed448PublicKeyToDer */
  21100. /*
  21101. * Testing wc_curve448_init and wc_curve448_free.
  21102. */
  21103. static int test_wc_curve448_init(void)
  21104. {
  21105. EXPECT_DECLS;
  21106. #if defined(HAVE_CURVE448)
  21107. curve448_key key;
  21108. /* Test bad args for wc_curve448_init */
  21109. ExpectIntEQ(wc_curve448_init(&key), 0);
  21110. /* Test bad args for wc_curve448_init */
  21111. ExpectIntEQ(wc_curve448_init(NULL), BAD_FUNC_ARG);
  21112. /* Test good args for wc_curve_448_free */
  21113. wc_curve448_free(&key);
  21114. /* Test bad args for wc_curve448_free */
  21115. wc_curve448_free(NULL);
  21116. #endif
  21117. return EXPECT_RESULT();
  21118. } /* END test_wc_curve448_init and wc_curve_448_free*/
  21119. /*
  21120. * Testing wc_curve448_make_key
  21121. */
  21122. static int test_wc_curve448_make_key(void)
  21123. {
  21124. EXPECT_DECLS;
  21125. #if defined(HAVE_CURVE448)
  21126. curve448_key key;
  21127. WC_RNG rng;
  21128. int keysize;
  21129. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21130. ExpectIntEQ(wc_curve448_init(&key), 0);
  21131. ExpectIntEQ(wc_InitRng(&rng), 0);
  21132. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21133. ExpectIntEQ(keysize = wc_curve448_size(&key), CURVE448_KEY_SIZE);
  21134. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, &key), 0);
  21135. /* test bad cases */
  21136. ExpectIntEQ(wc_curve448_make_key(NULL, 0, NULL), BAD_FUNC_ARG);
  21137. ExpectIntEQ(wc_curve448_make_key(&rng, keysize, NULL), BAD_FUNC_ARG);
  21138. ExpectIntEQ(wc_curve448_make_key(NULL, keysize, &key), BAD_FUNC_ARG);
  21139. ExpectIntEQ(wc_curve448_make_key(&rng, 0, &key), ECC_BAD_ARG_E);
  21140. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21141. wc_curve448_free(&key);
  21142. #endif
  21143. return EXPECT_RESULT();
  21144. } /* END test_wc_curve448_make_key*/
  21145. /*
  21146. * Testing test_wc_curve448_shared_secret_ex
  21147. */
  21148. static int test_wc_curve448_shared_secret_ex(void)
  21149. {
  21150. EXPECT_DECLS;
  21151. #if defined(HAVE_CURVE448)
  21152. curve448_key private_key;
  21153. curve448_key public_key;
  21154. WC_RNG rng;
  21155. byte out[CURVE448_KEY_SIZE];
  21156. word32 outLen = sizeof(out);
  21157. int endian = EC448_BIG_ENDIAN;
  21158. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21159. ExpectIntEQ(wc_curve448_init(&private_key), 0);
  21160. ExpectIntEQ(wc_InitRng(&rng), 0);
  21161. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &private_key), 0);
  21162. ExpectIntEQ(wc_curve448_init(&public_key), 0);
  21163. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &public_key), 0);
  21164. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21165. &outLen, endian), 0);
  21166. /* test bad cases */
  21167. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, NULL, NULL, 0, endian),
  21168. BAD_FUNC_ARG);
  21169. ExpectIntEQ(wc_curve448_shared_secret_ex(NULL, &public_key, out, &outLen,
  21170. endian), BAD_FUNC_ARG);
  21171. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, NULL, out, &outLen,
  21172. endian), BAD_FUNC_ARG);
  21173. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, NULL,
  21174. &outLen, endian), BAD_FUNC_ARG);
  21175. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21176. NULL, endian), BAD_FUNC_ARG);
  21177. outLen = outLen - 2;
  21178. ExpectIntEQ(wc_curve448_shared_secret_ex(&private_key, &public_key, out,
  21179. &outLen, endian), BAD_FUNC_ARG);
  21180. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21181. wc_curve448_free(&private_key);
  21182. wc_curve448_free(&public_key);
  21183. #endif
  21184. return EXPECT_RESULT();
  21185. } /* END test_wc_curve448_shared_secret_ex*/
  21186. /*
  21187. * Testing test_wc_curve448_export_public_ex
  21188. */
  21189. static int test_wc_curve448_export_public_ex(void)
  21190. {
  21191. EXPECT_DECLS;
  21192. #if defined(HAVE_CURVE448)
  21193. WC_RNG rng;
  21194. curve448_key key;
  21195. byte out[CURVE448_KEY_SIZE];
  21196. word32 outLen = sizeof(out);
  21197. int endian = EC448_BIG_ENDIAN;
  21198. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21199. ExpectIntEQ(wc_curve448_init(&key), 0);
  21200. ExpectIntEQ(wc_InitRng(&rng), 0);
  21201. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21202. ExpectIntEQ(wc_curve448_export_public(&key, out, &outLen), 0);
  21203. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian), 0);
  21204. /* test bad cases*/
  21205. ExpectIntEQ(wc_curve448_export_public_ex(NULL, NULL, NULL, endian),
  21206. BAD_FUNC_ARG);
  21207. ExpectIntEQ(wc_curve448_export_public_ex(NULL, out, &outLen, endian),
  21208. BAD_FUNC_ARG);
  21209. ExpectIntEQ(wc_curve448_export_public_ex(&key, NULL, &outLen, endian),
  21210. BAD_FUNC_ARG);
  21211. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, NULL, endian),
  21212. BAD_FUNC_ARG);
  21213. outLen = outLen - 2;
  21214. ExpectIntEQ(wc_curve448_export_public_ex(&key, out, &outLen, endian),
  21215. ECC_BAD_ARG_E);
  21216. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21217. wc_curve448_free(&key);
  21218. #endif
  21219. return EXPECT_RESULT();
  21220. } /* END test_wc_curve448_export_public_ex*/
  21221. /*
  21222. * Testing test_wc_curve448_export_private_raw_ex
  21223. */
  21224. static int test_wc_curve448_export_private_raw_ex(void)
  21225. {
  21226. EXPECT_DECLS;
  21227. #if defined(HAVE_CURVE448)
  21228. curve448_key key;
  21229. byte out[CURVE448_KEY_SIZE];
  21230. word32 outLen = sizeof(out);
  21231. int endian = EC448_BIG_ENDIAN;
  21232. ExpectIntEQ(wc_curve448_init(&key), 0);
  21233. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  21234. 0);
  21235. /* test bad cases*/
  21236. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, NULL, NULL, endian),
  21237. BAD_FUNC_ARG);
  21238. ExpectIntEQ(wc_curve448_export_private_raw_ex(NULL, out, &outLen, endian),
  21239. BAD_FUNC_ARG);
  21240. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, NULL, &outLen, endian),
  21241. BAD_FUNC_ARG);
  21242. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, NULL, endian),
  21243. BAD_FUNC_ARG);
  21244. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen,
  21245. EC448_LITTLE_ENDIAN), 0);
  21246. outLen = outLen - 2;
  21247. ExpectIntEQ(wc_curve448_export_private_raw_ex(&key, out, &outLen, endian),
  21248. ECC_BAD_ARG_E);
  21249. wc_curve448_free(&key);
  21250. #endif
  21251. return EXPECT_RESULT();
  21252. } /* END test_wc_curve448_export_private_raw_ex*/
  21253. /*
  21254. * Testing test_wc_curve448_import_private_raw_ex
  21255. */
  21256. static int test_wc_curve448_import_private_raw_ex(void)
  21257. {
  21258. EXPECT_DECLS;
  21259. #if defined(HAVE_CURVE448)
  21260. curve448_key key;
  21261. WC_RNG rng;
  21262. byte priv[CURVE448_KEY_SIZE];
  21263. byte pub[CURVE448_KEY_SIZE];
  21264. word32 privSz = sizeof(priv);
  21265. word32 pubSz = sizeof(pub);
  21266. int endian = EC448_BIG_ENDIAN;
  21267. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21268. ExpectIntEQ(wc_curve448_init(&key), 0);
  21269. ExpectIntEQ(wc_InitRng(&rng), 0);
  21270. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21271. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21272. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  21273. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21274. &key, endian), 0);
  21275. /* test bad cases */
  21276. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, 0, NULL, 0, NULL, 0),
  21277. BAD_FUNC_ARG);
  21278. ExpectIntEQ(wc_curve448_import_private_raw_ex(NULL, privSz, pub, pubSz,
  21279. &key, endian), BAD_FUNC_ARG);
  21280. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, NULL, pubSz,
  21281. &key, endian), BAD_FUNC_ARG);
  21282. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21283. NULL, endian), BAD_FUNC_ARG);
  21284. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, 0, pub, pubSz,
  21285. &key, endian), ECC_BAD_ARG_E);
  21286. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, 0,
  21287. &key, endian), ECC_BAD_ARG_E);
  21288. ExpectIntEQ(wc_curve448_import_private_raw_ex(priv, privSz, pub, pubSz,
  21289. &key, EC448_LITTLE_ENDIAN), 0);
  21290. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21291. wc_curve448_free(&key);
  21292. #endif
  21293. return EXPECT_RESULT();
  21294. } /* END test_wc_curve448_import_private_raw_ex*/
  21295. /*
  21296. * Testing test_curve448_export_key_raw
  21297. */
  21298. static int test_wc_curve448_export_key_raw(void)
  21299. {
  21300. EXPECT_DECLS;
  21301. #if defined(HAVE_CURVE448)
  21302. curve448_key key;
  21303. WC_RNG rng;
  21304. byte priv[CURVE448_KEY_SIZE];
  21305. byte pub[CURVE448_KEY_SIZE];
  21306. word32 privSz = sizeof(priv);
  21307. word32 pubSz = sizeof(pub);
  21308. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21309. ExpectIntEQ(wc_curve448_init(&key), 0);
  21310. ExpectIntEQ(wc_InitRng(&rng), 0);
  21311. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21312. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21313. ExpectIntEQ(wc_curve448_export_public(&key, pub, &pubSz), 0);
  21314. ExpectIntEQ(wc_curve448_export_key_raw(&key, priv, &privSz, pub, &pubSz),
  21315. 0);
  21316. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21317. wc_curve448_free(&key);
  21318. #endif
  21319. return EXPECT_RESULT();
  21320. } /* END test_wc_curve448_import_private_raw_ex*/
  21321. /*
  21322. * Testing test_wc_curve448_import_private
  21323. */
  21324. static int test_wc_curve448_import_private(void)
  21325. {
  21326. EXPECT_DECLS;
  21327. #if defined(HAVE_CURVE448)
  21328. curve448_key key;
  21329. WC_RNG rng;
  21330. byte priv[CURVE448_KEY_SIZE];
  21331. word32 privSz = sizeof(priv);
  21332. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21333. ExpectIntEQ(wc_curve448_init(&key), 0);
  21334. ExpectIntEQ(wc_InitRng(&rng), 0);
  21335. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &key), 0);
  21336. ExpectIntEQ(wc_curve448_export_private_raw(&key, priv, &privSz), 0);
  21337. ExpectIntEQ(wc_curve448_import_private(priv, privSz, &key), 0);
  21338. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21339. wc_curve448_free(&key);
  21340. #endif
  21341. return EXPECT_RESULT();
  21342. } /* END test_wc_curve448_import*/
  21343. /*
  21344. * Testing test_wc_curve448_size.
  21345. */
  21346. static int test_wc_curve448_size(void)
  21347. {
  21348. EXPECT_DECLS;
  21349. #if defined(HAVE_CURVE448)
  21350. curve448_key key;
  21351. ExpectIntEQ(wc_curve448_init(&key), 0);
  21352. /* Test good args for wc_curve448_size */
  21353. ExpectIntEQ(wc_curve448_size(&key), CURVE448_KEY_SIZE);
  21354. /* Test bad args for wc_curve448_size */
  21355. ExpectIntEQ(wc_curve448_size(NULL), 0);
  21356. wc_curve448_free(&key);
  21357. #endif
  21358. return EXPECT_RESULT();
  21359. } /* END test_wc_curve448_size*/
  21360. /*
  21361. * Testing wc_ecc_make_key.
  21362. */
  21363. static int test_wc_ecc_make_key(void)
  21364. {
  21365. EXPECT_DECLS;
  21366. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21367. ecc_key key;
  21368. WC_RNG rng;
  21369. int ret;
  21370. XMEMSET(&key, 0, sizeof(ecc_key));
  21371. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21372. ExpectIntEQ(wc_ecc_init(&key), 0);
  21373. ExpectIntEQ(wc_InitRng(&rng), 0);
  21374. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21375. #if defined(WOLFSSL_ASYNC_CRYPT)
  21376. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21377. #endif
  21378. ExpectIntEQ(ret, 0);
  21379. /* Pass in bad args. */
  21380. ExpectIntEQ(wc_ecc_make_key(NULL, KEY14, &key), BAD_FUNC_ARG);
  21381. ExpectIntEQ(wc_ecc_make_key(&rng, KEY14, NULL), BAD_FUNC_ARG);
  21382. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21383. wc_ecc_free(&key);
  21384. #ifdef FP_ECC
  21385. wc_ecc_fp_free();
  21386. #endif
  21387. #endif
  21388. return EXPECT_RESULT();
  21389. } /* END test_wc_ecc_make_key */
  21390. /*
  21391. * Testing wc_ecc_init()
  21392. */
  21393. static int test_wc_ecc_init(void)
  21394. {
  21395. EXPECT_DECLS;
  21396. #ifdef HAVE_ECC
  21397. ecc_key key;
  21398. XMEMSET(&key, 0, sizeof(ecc_key));
  21399. ExpectIntEQ(wc_ecc_init(&key), 0);
  21400. /* Pass in bad args. */
  21401. ExpectIntEQ(wc_ecc_init(NULL), BAD_FUNC_ARG);
  21402. wc_ecc_free(&key);
  21403. #endif
  21404. return EXPECT_RESULT();
  21405. } /* END test_wc_ecc_init */
  21406. /*
  21407. * Testing wc_ecc_check_key()
  21408. */
  21409. static int test_wc_ecc_check_key(void)
  21410. {
  21411. EXPECT_DECLS;
  21412. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21413. ecc_key key;
  21414. WC_RNG rng;
  21415. int ret;
  21416. XMEMSET(&rng, 0, sizeof(rng));
  21417. XMEMSET(&key, 0, sizeof(key));
  21418. ExpectIntEQ(wc_ecc_init(&key), 0);
  21419. ExpectIntEQ(wc_InitRng(&rng), 0);
  21420. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21421. #if defined(WOLFSSL_ASYNC_CRYPT)
  21422. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21423. #endif
  21424. ExpectIntEQ(ret, 0);
  21425. ExpectIntEQ(wc_ecc_check_key(&key), 0);
  21426. /* Pass in bad args. */
  21427. ExpectIntEQ(wc_ecc_check_key(NULL), BAD_FUNC_ARG);
  21428. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21429. wc_ecc_free(&key);
  21430. #ifdef FP_ECC
  21431. wc_ecc_fp_free();
  21432. #endif
  21433. #endif
  21434. return EXPECT_RESULT();
  21435. } /* END test_wc_ecc_check_key */
  21436. /*
  21437. * Testing wc_ecc_get_generator()
  21438. */
  21439. static int test_wc_ecc_get_generator(void)
  21440. {
  21441. EXPECT_DECLS;
  21442. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  21443. !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  21444. ecc_point* pt = NULL;
  21445. ExpectNotNull(pt = wc_ecc_new_point());
  21446. ExpectIntEQ(wc_ecc_get_generator(pt, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  21447. MP_OKAY);
  21448. /* Test bad args. */
  21449. /* Returns Zero for bad arg. */
  21450. ExpectIntNE(wc_ecc_get_generator(pt, -1), MP_OKAY);
  21451. ExpectIntNE(wc_ecc_get_generator(NULL, wc_ecc_get_curve_idx(ECC_SECP256R1)),
  21452. MP_OKAY);
  21453. /* If we ever get to 1000 curves increase this number */
  21454. ExpectIntNE(wc_ecc_get_generator(pt, 1000), MP_OKAY);
  21455. ExpectIntNE(wc_ecc_get_generator(NULL, -1), MP_OKAY);
  21456. wc_ecc_del_point(pt);
  21457. #endif
  21458. return EXPECT_RESULT();
  21459. } /* END test_wc_ecc_get_generator */
  21460. /*
  21461. * Testing wc_ecc_size()
  21462. */
  21463. static int test_wc_ecc_size(void)
  21464. {
  21465. EXPECT_DECLS;
  21466. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  21467. WC_RNG rng;
  21468. ecc_key key;
  21469. int ret;
  21470. XMEMSET(&key, 0, sizeof(ecc_key));
  21471. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21472. ExpectIntEQ(wc_ecc_init(&key), 0);
  21473. ExpectIntEQ(wc_InitRng(&rng), 0);
  21474. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21475. #if defined(WOLFSSL_ASYNC_CRYPT)
  21476. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21477. #endif
  21478. ExpectIntEQ(ret, 0);
  21479. ExpectIntEQ(wc_ecc_size(&key), KEY14);
  21480. /* Test bad args. */
  21481. /* Returns Zero for bad arg. */
  21482. ExpectIntEQ(wc_ecc_size(NULL), 0);
  21483. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21484. wc_ecc_free(&key);
  21485. #endif
  21486. return EXPECT_RESULT();
  21487. } /* END test_wc_ecc_size */
  21488. static int test_wc_ecc_params(void)
  21489. {
  21490. EXPECT_DECLS;
  21491. /* FIPS/CAVP self-test modules do not have `wc_ecc_get_curve_params`.
  21492. It was added after certifications */
  21493. #if defined(HAVE_ECC) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  21494. const ecc_set_type* ecc_set;
  21495. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  21496. /* Test for SECP256R1 curve */
  21497. int curve_id = ECC_SECP256R1;
  21498. int curve_idx;
  21499. ExpectIntNE(curve_idx = wc_ecc_get_curve_idx(curve_id), ECC_CURVE_INVALID);
  21500. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(curve_idx));
  21501. ExpectIntEQ(ecc_set->id, curve_id);
  21502. #endif
  21503. /* Test case when SECP256R1 is not enabled */
  21504. /* Test that we get curve params for index 0 */
  21505. ExpectNotNull(ecc_set = wc_ecc_get_curve_params(0));
  21506. #endif /* HAVE_ECC && !HAVE_FIPS && !HAVE_SELFTEST */
  21507. return EXPECT_RESULT();
  21508. }
  21509. /*
  21510. * Testing wc_ecc_sign_hash() and wc_ecc_verify_hash()
  21511. */
  21512. static int test_wc_ecc_signVerify_hash(void)
  21513. {
  21514. EXPECT_DECLS;
  21515. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && !defined(NO_ASN) && !defined(WC_NO_RNG)
  21516. ecc_key key;
  21517. WC_RNG rng;
  21518. int ret;
  21519. #ifdef HAVE_ECC_VERIFY
  21520. int verify = 0;
  21521. #endif
  21522. word32 siglen = ECC_BUFSIZE;
  21523. byte sig[ECC_BUFSIZE];
  21524. byte adjustedSig[ECC_BUFSIZE+1];
  21525. byte digest[] = TEST_STRING;
  21526. word32 digestlen = (word32)TEST_STRING_SZ;
  21527. /* Init stack var */
  21528. XMEMSET(&key, 0, sizeof(ecc_key));
  21529. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21530. XMEMSET(sig, 0, siglen);
  21531. XMEMSET(adjustedSig, 0, ECC_BUFSIZE+1);
  21532. /* Init structs. */
  21533. ExpectIntEQ(wc_ecc_init(&key), 0);
  21534. ExpectIntEQ(wc_InitRng(&rng), 0);
  21535. ret = wc_ecc_make_key(&rng, KEY14, &key);
  21536. #if defined(WOLFSSL_ASYNC_CRYPT)
  21537. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21538. #endif
  21539. ExpectIntEQ(ret, 0);
  21540. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, &key),
  21541. 0);
  21542. /* Check bad args. */
  21543. ExpectIntEQ(wc_ecc_sign_hash(NULL, digestlen, sig, &siglen, &rng, &key),
  21544. ECC_BAD_ARG_E);
  21545. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, NULL, &siglen, &rng, &key),
  21546. ECC_BAD_ARG_E);
  21547. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, NULL, &rng, &key),
  21548. ECC_BAD_ARG_E);
  21549. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, NULL, &key),
  21550. ECC_BAD_ARG_E);
  21551. ExpectIntEQ(wc_ecc_sign_hash(digest, digestlen, sig, &siglen, &rng, NULL),
  21552. ECC_BAD_ARG_E);
  21553. #ifdef HAVE_ECC_VERIFY
  21554. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  21555. &key), 0);
  21556. ExpectIntEQ(verify, 1);
  21557. /* test check on length of signature passed in */
  21558. XMEMCPY(adjustedSig, sig, siglen);
  21559. adjustedSig[1] = adjustedSig[1] + 1; /* add 1 to length for extra byte*/
  21560. #ifndef NO_STRICT_ECDSA_LEN
  21561. ExpectIntNE(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21562. &verify, &key), 0);
  21563. #else
  21564. /* if NO_STRICT_ECDSA_LEN is set then extra bytes after the signature
  21565. * is allowed */
  21566. ExpectIntEQ(wc_ecc_verify_hash(adjustedSig, siglen+1, digest, digestlen,
  21567. &verify, &key), 0);
  21568. #endif
  21569. /* Test bad args. */
  21570. ExpectIntEQ(wc_ecc_verify_hash(NULL, siglen, digest, digestlen, &verify,
  21571. &key), ECC_BAD_ARG_E);
  21572. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, NULL, digestlen, &verify, &key),
  21573. ECC_BAD_ARG_E);
  21574. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, NULL, &key),
  21575. ECC_BAD_ARG_E);
  21576. ExpectIntEQ(wc_ecc_verify_hash(sig, siglen, digest, digestlen, &verify,
  21577. NULL), ECC_BAD_ARG_E);
  21578. #endif /* HAVE_ECC_VERIFY */
  21579. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21580. wc_ecc_free(&key);
  21581. #ifdef FP_ECC
  21582. wc_ecc_fp_free();
  21583. #endif
  21584. #endif
  21585. return EXPECT_RESULT();
  21586. } /* END test_wc_ecc_sign_hash */
  21587. /*
  21588. * Testing wc_ecc_shared_secret()
  21589. */
  21590. static int test_wc_ecc_shared_secret(void)
  21591. {
  21592. EXPECT_DECLS;
  21593. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && !defined(WC_NO_RNG)
  21594. ecc_key key;
  21595. ecc_key pubKey;
  21596. WC_RNG rng;
  21597. #if defined(NO_ECC256)
  21598. int ret;
  21599. #endif
  21600. byte out[KEY32];
  21601. int keySz = sizeof(out);
  21602. word32 outlen = (word32)sizeof(out);
  21603. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21604. const char* qx =
  21605. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21606. const char* qy =
  21607. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21608. const char* d =
  21609. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21610. const char* curveName = "SECP256R1";
  21611. const byte expected_shared_secret[] =
  21612. {
  21613. 0x65, 0xc0, 0xd4, 0x61, 0x17, 0xe6, 0x09, 0x75,
  21614. 0xf0, 0x12, 0xa0, 0x4d, 0x0b, 0x41, 0x30, 0x7a,
  21615. 0x51, 0xf0, 0xb3, 0xaf, 0x23, 0x8f, 0x0f, 0xdf,
  21616. 0xf1, 0xff, 0x23, 0x64, 0x28, 0xca, 0xf8, 0x06
  21617. };
  21618. #endif
  21619. PRIVATE_KEY_UNLOCK();
  21620. /* Initialize variables. */
  21621. XMEMSET(&key, 0, sizeof(ecc_key));
  21622. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  21623. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21624. XMEMSET(out, 0, keySz);
  21625. ExpectIntEQ(wc_ecc_init(&key), 0);
  21626. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  21627. ExpectIntEQ(wc_InitRng(&rng), 0);
  21628. #if !defined(NO_ECC256)
  21629. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  21630. ExpectIntEQ(wc_ecc_import_raw(&pubKey, qx, qy, NULL, curveName), 0);
  21631. #else
  21632. ret = wc_ecc_make_key(&rng, keySz, &key);
  21633. #if defined(WOLFSSL_ASYNC_CRYPT)
  21634. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21635. #endif
  21636. ExpectIntEQ(ret, 0);
  21637. ret = wc_ecc_make_key(&rng, keySz, &key);
  21638. #if defined(WOLFSSL_ASYNC_CRYPT)
  21639. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21640. #endif
  21641. ExpectIntEQ(ret, 0);
  21642. #endif
  21643. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  21644. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  21645. !defined(HAVE_SELFTEST)
  21646. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  21647. #endif
  21648. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen), 0);
  21649. #if !defined(NO_ECC256)
  21650. ExpectIntEQ(XMEMCMP(out, expected_shared_secret, outlen), 0);
  21651. #endif
  21652. /* Test bad args. */
  21653. ExpectIntEQ(wc_ecc_shared_secret(NULL, &pubKey, out, &outlen),
  21654. BAD_FUNC_ARG);
  21655. ExpectIntEQ(wc_ecc_shared_secret(&key, NULL, out, &outlen),
  21656. BAD_FUNC_ARG);
  21657. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, NULL, &outlen),
  21658. BAD_FUNC_ARG);
  21659. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, NULL),
  21660. BAD_FUNC_ARG);
  21661. /* Invalid length */
  21662. outlen = 1;
  21663. ExpectIntEQ(wc_ecc_shared_secret(&key, &pubKey, out, &outlen),
  21664. BUFFER_E);
  21665. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21666. wc_ecc_free(&pubKey);
  21667. wc_ecc_free(&key);
  21668. #ifdef FP_ECC
  21669. wc_ecc_fp_free();
  21670. #endif
  21671. PRIVATE_KEY_LOCK();
  21672. #endif
  21673. return EXPECT_RESULT();
  21674. } /* END tests_wc_ecc_shared_secret */
  21675. /*
  21676. * testint wc_ecc_export_x963()
  21677. */
  21678. static int test_wc_ecc_export_x963(void)
  21679. {
  21680. EXPECT_DECLS;
  21681. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21682. ecc_key key;
  21683. WC_RNG rng;
  21684. byte out[ECC_ASN963_MAX_BUF_SZ];
  21685. word32 outlen = sizeof(out);
  21686. int ret;
  21687. PRIVATE_KEY_UNLOCK();
  21688. /* Initialize variables. */
  21689. XMEMSET(&key, 0, sizeof(ecc_key));
  21690. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21691. XMEMSET(out, 0, outlen);
  21692. ExpectIntEQ(wc_ecc_init(&key), 0);
  21693. ExpectIntEQ(wc_InitRng(&rng), 0);
  21694. ret = wc_ecc_make_key(&rng, KEY20, &key);
  21695. #if defined(WOLFSSL_ASYNC_CRYPT)
  21696. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21697. #endif
  21698. ExpectIntEQ(ret, 0);
  21699. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), 0);
  21700. /* Test bad args. */
  21701. ExpectIntEQ(wc_ecc_export_x963(NULL, out, &outlen), ECC_BAD_ARG_E);
  21702. ExpectIntEQ(wc_ecc_export_x963(&key, NULL, &outlen), LENGTH_ONLY_E);
  21703. ExpectIntEQ(wc_ecc_export_x963(&key, out, NULL), ECC_BAD_ARG_E);
  21704. key.idx = -4;
  21705. ExpectIntEQ(wc_ecc_export_x963(&key, out, &outlen), ECC_BAD_ARG_E);
  21706. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21707. wc_ecc_free(&key);
  21708. #ifdef FP_ECC
  21709. wc_ecc_fp_free();
  21710. #endif
  21711. PRIVATE_KEY_LOCK();
  21712. #endif
  21713. return EXPECT_RESULT();
  21714. } /* END test_wc_ecc_export_x963 */
  21715. /*
  21716. * Testing wc_ecc_export_x963_ex()
  21717. * compile with --enable-compkey will use compression.
  21718. */
  21719. static int test_wc_ecc_export_x963_ex(void)
  21720. {
  21721. EXPECT_DECLS;
  21722. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21723. ecc_key key;
  21724. WC_RNG rng;
  21725. int ret;
  21726. byte out[ECC_ASN963_MAX_BUF_SZ];
  21727. word32 outlen = sizeof(out);
  21728. #ifdef HAVE_COMP_KEY
  21729. word32 badOutLen = 5;
  21730. #endif
  21731. /* Init stack variables. */
  21732. XMEMSET(&key, 0, sizeof(ecc_key));
  21733. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21734. XMEMSET(out, 0, outlen);
  21735. ExpectIntEQ(wc_ecc_init(&key), 0);
  21736. ExpectIntEQ(wc_InitRng(&rng), 0);
  21737. ret = wc_ecc_make_key(&rng, KEY64, &key);
  21738. #if defined(WOLFSSL_ASYNC_CRYPT)
  21739. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21740. #endif
  21741. ExpectIntEQ(ret, 0);
  21742. #ifdef HAVE_COMP_KEY
  21743. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), 0);
  21744. #else
  21745. ExpectIntEQ(ret = wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP), 0);
  21746. #endif
  21747. /* Test bad args. */
  21748. #ifdef HAVE_COMP_KEY
  21749. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, COMP), BAD_FUNC_ARG);
  21750. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, COMP), BAD_FUNC_ARG);
  21751. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, COMP), BAD_FUNC_ARG);
  21752. #if defined(HAVE_FIPS) && (!defined(FIPS_VERSION_LT) || FIPS_VERSION_LT(5,3))
  21753. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP), BUFFER_E);
  21754. #else
  21755. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &badOutLen, COMP),
  21756. LENGTH_ONLY_E);
  21757. #endif
  21758. key.idx = -4;
  21759. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, COMP), ECC_BAD_ARG_E);
  21760. #else
  21761. ExpectIntEQ(wc_ecc_export_x963_ex(NULL, out, &outlen, NOCOMP),
  21762. ECC_BAD_ARG_E);
  21763. ExpectIntEQ(wc_ecc_export_x963_ex(&key, NULL, &outlen, NOCOMP),
  21764. LENGTH_ONLY_E);
  21765. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, 1), NOT_COMPILED_IN);
  21766. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, NULL, NOCOMP),
  21767. ECC_BAD_ARG_E);
  21768. key.idx = -4;
  21769. ExpectIntEQ(wc_ecc_export_x963_ex(&key, out, &outlen, NOCOMP),
  21770. ECC_BAD_ARG_E);
  21771. #endif
  21772. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21773. wc_ecc_free(&key);
  21774. #ifdef FP_ECC
  21775. wc_ecc_fp_free();
  21776. #endif
  21777. #endif
  21778. return EXPECT_RESULT();
  21779. } /* END test_wc_ecc_export_x963_ex */
  21780. /*
  21781. * testing wc_ecc_import_x963()
  21782. */
  21783. static int test_wc_ecc_import_x963(void)
  21784. {
  21785. EXPECT_DECLS;
  21786. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21787. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21788. ecc_key pubKey;
  21789. ecc_key key;
  21790. WC_RNG rng;
  21791. byte x963[ECC_ASN963_MAX_BUF_SZ];
  21792. word32 x963Len = (word32)sizeof(x963);
  21793. int ret;
  21794. /* Init stack variables. */
  21795. XMEMSET(&key, 0, sizeof(ecc_key));
  21796. XMEMSET(&pubKey, 0, sizeof(ecc_key));
  21797. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21798. XMEMSET(x963, 0, x963Len);
  21799. ExpectIntEQ(wc_ecc_init(&pubKey), 0);
  21800. ExpectIntEQ(wc_ecc_init(&key), 0);
  21801. ExpectIntEQ(wc_InitRng(&rng), 0);
  21802. #if FIPS_VERSION3_GE(6,0,0)
  21803. ret = wc_ecc_make_key(&rng, KEY32, &key);
  21804. #else
  21805. ret = wc_ecc_make_key(&rng, KEY24, &key);
  21806. #endif
  21807. #if defined(WOLFSSL_ASYNC_CRYPT)
  21808. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21809. #endif
  21810. ExpectIntEQ(ret, 0);
  21811. PRIVATE_KEY_UNLOCK();
  21812. ExpectIntEQ(wc_ecc_export_x963(&key, x963, &x963Len), 0);
  21813. PRIVATE_KEY_LOCK();
  21814. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, &pubKey), 0);
  21815. /* Test bad args. */
  21816. ExpectIntEQ(wc_ecc_import_x963(NULL, x963Len, &pubKey), BAD_FUNC_ARG);
  21817. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len, NULL), BAD_FUNC_ARG);
  21818. ExpectIntEQ(wc_ecc_import_x963(x963, x963Len + 1, &pubKey), ECC_BAD_ARG_E);
  21819. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21820. wc_ecc_free(&key);
  21821. wc_ecc_free(&pubKey);
  21822. #ifdef FP_ECC
  21823. wc_ecc_fp_free();
  21824. #endif
  21825. #endif
  21826. return EXPECT_RESULT();
  21827. } /* END wc_ecc_import_x963 */
  21828. /*
  21829. * testing wc_ecc_import_private_key()
  21830. */
  21831. static int test_wc_ecc_import_private_key(void)
  21832. {
  21833. EXPECT_DECLS;
  21834. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_IMPORT) && \
  21835. defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21836. ecc_key key;
  21837. ecc_key keyImp;
  21838. WC_RNG rng;
  21839. byte privKey[ECC_PRIV_KEY_BUF]; /* Raw private key.*/
  21840. byte x963Key[ECC_ASN963_MAX_BUF_SZ];
  21841. word32 privKeySz = (word32)sizeof(privKey);
  21842. word32 x963KeySz = (word32)sizeof(x963Key);
  21843. int ret;
  21844. /* Init stack variables. */
  21845. XMEMSET(&key, 0, sizeof(ecc_key));
  21846. XMEMSET(&keyImp, 0, sizeof(ecc_key));
  21847. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21848. XMEMSET(privKey, 0, privKeySz);
  21849. XMEMSET(x963Key, 0, x963KeySz);
  21850. ExpectIntEQ(wc_ecc_init(&key), 0);
  21851. ExpectIntEQ(wc_ecc_init(&keyImp), 0);
  21852. ExpectIntEQ(wc_InitRng(&rng), 0);
  21853. ret = wc_ecc_make_key(&rng, KEY48, &key);
  21854. #if defined(WOLFSSL_ASYNC_CRYPT)
  21855. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21856. #endif
  21857. ExpectIntEQ(ret, 0);
  21858. PRIVATE_KEY_UNLOCK();
  21859. ExpectIntEQ(wc_ecc_export_x963(&key, x963Key, &x963KeySz), 0);
  21860. PRIVATE_KEY_LOCK();
  21861. ExpectIntEQ(wc_ecc_export_private_only(&key, privKey, &privKeySz), 0);
  21862. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21863. x963KeySz, &keyImp), 0);
  21864. /* Pass in bad args. */
  21865. ExpectIntEQ(wc_ecc_import_private_key(privKey, privKeySz, x963Key,
  21866. x963KeySz, NULL), BAD_FUNC_ARG);
  21867. ExpectIntEQ(wc_ecc_import_private_key(NULL, privKeySz, x963Key, x963KeySz,
  21868. &keyImp), BAD_FUNC_ARG);
  21869. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21870. wc_ecc_free(&keyImp);
  21871. wc_ecc_free(&key);
  21872. #ifdef FP_ECC
  21873. wc_ecc_fp_free();
  21874. #endif
  21875. #endif
  21876. return EXPECT_RESULT();
  21877. } /* END test_wc_ecc_import_private_key */
  21878. /*
  21879. * Testing wc_ecc_export_private_only()
  21880. */
  21881. static int test_wc_ecc_export_private_only(void)
  21882. {
  21883. EXPECT_DECLS;
  21884. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  21885. ecc_key key;
  21886. WC_RNG rng;
  21887. byte out[ECC_PRIV_KEY_BUF];
  21888. word32 outlen = sizeof(out);
  21889. int ret;
  21890. /* Init stack variables. */
  21891. XMEMSET(&key, 0, sizeof(ecc_key));
  21892. XMEMSET(&rng, 0, sizeof(WC_RNG));
  21893. XMEMSET(out, 0, outlen);
  21894. ExpectIntEQ(wc_ecc_init(&key), 0);
  21895. ExpectIntEQ(wc_InitRng(&rng), 0);
  21896. ret = wc_ecc_make_key(&rng, KEY32, &key);
  21897. #if defined(WOLFSSL_ASYNC_CRYPT)
  21898. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  21899. #endif
  21900. ExpectIntEQ(ret, 0);
  21901. ExpectIntEQ(wc_ecc_export_private_only(&key, out, &outlen), 0);
  21902. /* Pass in bad args. */
  21903. ExpectIntEQ(wc_ecc_export_private_only(NULL, out, &outlen), BAD_FUNC_ARG);
  21904. ExpectIntEQ(wc_ecc_export_private_only(&key, NULL, &outlen), BAD_FUNC_ARG);
  21905. ExpectIntEQ(wc_ecc_export_private_only(&key, out, NULL), BAD_FUNC_ARG);
  21906. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  21907. wc_ecc_free(&key);
  21908. #ifdef FP_ECC
  21909. wc_ecc_fp_free();
  21910. #endif
  21911. #endif
  21912. return EXPECT_RESULT();
  21913. } /* END test_wc_ecc_export_private_only */
  21914. /*
  21915. * Testing wc_ecc_rs_to_sig()
  21916. */
  21917. static int test_wc_ecc_rs_to_sig(void)
  21918. {
  21919. EXPECT_DECLS;
  21920. #if defined(HAVE_ECC) && !defined(NO_ASN)
  21921. /* first [P-192,SHA-1] vector from FIPS 186-3 NIST vectors */
  21922. const char* R = "6994d962bdd0d793ffddf855ec5bf2f91a9698b46258a63e";
  21923. const char* S = "02ba6465a234903744ab02bc8521405b73cf5fc00e1a9f41";
  21924. const char* zeroStr = "0";
  21925. byte sig[ECC_MAX_SIG_SIZE];
  21926. word32 siglen = (word32)sizeof(sig);
  21927. /* R and S max size is the order of curve. 2^192.*/
  21928. int keySz = KEY24;
  21929. byte r[KEY24];
  21930. byte s[KEY24];
  21931. word32 rlen = (word32)sizeof(r);
  21932. word32 slen = (word32)sizeof(s);
  21933. /* Init stack variables. */
  21934. XMEMSET(sig, 0, ECC_MAX_SIG_SIZE);
  21935. XMEMSET(r, 0, keySz);
  21936. XMEMSET(s, 0, keySz);
  21937. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, &siglen), 0);
  21938. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, &slen), 0);
  21939. /* Test bad args. */
  21940. ExpectIntEQ(wc_ecc_rs_to_sig(NULL, S, sig, &siglen), ECC_BAD_ARG_E);
  21941. ExpectIntEQ(wc_ecc_rs_to_sig(R, NULL, sig, &siglen), ECC_BAD_ARG_E);
  21942. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, sig, NULL), ECC_BAD_ARG_E);
  21943. ExpectIntEQ(wc_ecc_rs_to_sig(R, S, NULL, &siglen), ECC_BAD_ARG_E);
  21944. ExpectIntEQ(wc_ecc_rs_to_sig(R, zeroStr, sig, &siglen), MP_ZERO_E);
  21945. ExpectIntEQ(wc_ecc_rs_to_sig(zeroStr, S, sig, &siglen), MP_ZERO_E);
  21946. ExpectIntEQ(wc_ecc_sig_to_rs(NULL, siglen, r, &rlen, s, &slen),
  21947. ECC_BAD_ARG_E);
  21948. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, NULL, &rlen, s, &slen),
  21949. ECC_BAD_ARG_E);
  21950. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, NULL, s, &slen),
  21951. ECC_BAD_ARG_E);
  21952. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, NULL, &slen),
  21953. ECC_BAD_ARG_E);
  21954. ExpectIntEQ(wc_ecc_sig_to_rs(sig, siglen, r, &rlen, s, NULL),
  21955. ECC_BAD_ARG_E);
  21956. #endif
  21957. return EXPECT_RESULT();
  21958. } /* END test_wc_ecc_rs_to_sig */
  21959. static int test_wc_ecc_import_raw(void)
  21960. {
  21961. EXPECT_DECLS;
  21962. #if defined(HAVE_ECC) && !defined(NO_ECC256)
  21963. ecc_key key;
  21964. const char* qx =
  21965. "bb33ac4c27504ac64aa504c33cde9f36db722dce94ea2bfacb2009392c16e861";
  21966. const char* qy =
  21967. "02e9af4dd302939a315b9792217ff0cf18da9111023486e82058330b803489d8";
  21968. const char* d =
  21969. "45b66902739c6c85a1385b72e8e8c7acc4038d533504fa6c28dc348de1a8098c";
  21970. const char* curveName = "SECP256R1";
  21971. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21972. const char* kNullStr = "";
  21973. int ret;
  21974. #endif
  21975. XMEMSET(&key, 0, sizeof(ecc_key));
  21976. ExpectIntEQ(wc_ecc_init(&key), 0);
  21977. /* Test good import */
  21978. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, curveName), 0);
  21979. /* Test bad args. */
  21980. ExpectIntEQ(wc_ecc_import_raw(NULL, qx, qy, d, curveName), BAD_FUNC_ARG);
  21981. ExpectIntEQ(wc_ecc_import_raw(&key, NULL, qy, d, curveName), BAD_FUNC_ARG);
  21982. ExpectIntEQ(wc_ecc_import_raw(&key, qx, NULL, d, curveName), BAD_FUNC_ARG);
  21983. ExpectIntEQ(wc_ecc_import_raw(&key, qx, qy, d, NULL), BAD_FUNC_ARG);
  21984. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  21985. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  21986. wc_ecc_free(&key);
  21987. #endif
  21988. ExpectIntLT(ret = wc_ecc_import_raw(&key, kNullStr, kNullStr, kNullStr,
  21989. curveName), 0);
  21990. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  21991. #endif
  21992. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  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, "0", qy, d, curveName), 0);
  21998. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  21999. #else
  22000. ExpectIntEQ(wc_ecc_import_raw(&key, "0", qy, d, curveName), 0);
  22001. #endif
  22002. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22003. wc_ecc_free(&key);
  22004. #endif
  22005. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22006. ExpectIntLT(ret = wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  22007. ExpectTrue((ret == BAD_FUNC_ARG) || (ret == MP_VAL));
  22008. #else
  22009. ExpectIntEQ(wc_ecc_import_raw(&key, qx, "0", d, curveName), 0);
  22010. #endif
  22011. #if !defined(USE_FAST_MATH) && !defined(WOLFSSL_SP_MATH)
  22012. wc_ecc_free(&key);
  22013. #endif
  22014. ExpectIntEQ(wc_ecc_import_raw(&key, "0", "0", d, curveName), ECC_INF_E);
  22015. #endif
  22016. wc_ecc_free(&key);
  22017. #endif
  22018. return EXPECT_RESULT();
  22019. } /* END test_wc_ecc_import_raw */
  22020. static int test_wc_ecc_import_unsigned(void)
  22021. {
  22022. EXPECT_DECLS;
  22023. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22024. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  22025. HAVE_FIPS_VERSION >= 2))
  22026. ecc_key key;
  22027. const byte qx[] = {
  22028. 0xbb, 0x33, 0xac, 0x4c, 0x27, 0x50, 0x4a, 0xc6,
  22029. 0x4a, 0xa5, 0x04, 0xc3, 0x3c, 0xde, 0x9f, 0x36,
  22030. 0xdb, 0x72, 0x2d, 0xce, 0x94, 0xea, 0x2b, 0xfa,
  22031. 0xcb, 0x20, 0x09, 0x39, 0x2c, 0x16, 0xe8, 0x61
  22032. };
  22033. const byte qy[] = {
  22034. 0x02, 0xe9, 0xaf, 0x4d, 0xd3, 0x02, 0x93, 0x9a,
  22035. 0x31, 0x5b, 0x97, 0x92, 0x21, 0x7f, 0xf0, 0xcf,
  22036. 0x18, 0xda, 0x91, 0x11, 0x02, 0x34, 0x86, 0xe8,
  22037. 0x20, 0x58, 0x33, 0x0b, 0x80, 0x34, 0x89, 0xd8
  22038. };
  22039. const byte d[] = {
  22040. 0x45, 0xb6, 0x69, 0x02, 0x73, 0x9c, 0x6c, 0x85,
  22041. 0xa1, 0x38, 0x5b, 0x72, 0xe8, 0xe8, 0xc7, 0xac,
  22042. 0xc4, 0x03, 0x8d, 0x53, 0x35, 0x04, 0xfa, 0x6c,
  22043. 0x28, 0xdc, 0x34, 0x8d, 0xe1, 0xa8, 0x09, 0x8c
  22044. };
  22045. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22046. const byte nullBytes[32] = {0};
  22047. int ret;
  22048. #endif
  22049. int curveId = ECC_SECP256R1;
  22050. XMEMSET(&key, 0, sizeof(ecc_key));
  22051. ExpectIntEQ(wc_ecc_init(&key), 0);
  22052. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  22053. curveId), 0);
  22054. /* Test bad args. */
  22055. ExpectIntEQ(wc_ecc_import_unsigned(NULL, (byte*)qx, (byte*)qy, (byte*)d,
  22056. curveId), BAD_FUNC_ARG);
  22057. ExpectIntEQ(wc_ecc_import_unsigned(&key, NULL, (byte*)qy, (byte*)d,
  22058. curveId), BAD_FUNC_ARG);
  22059. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, NULL, (byte*)d,
  22060. curveId), BAD_FUNC_ARG);
  22061. ExpectIntEQ(wc_ecc_import_unsigned(&key, (byte*)qx, (byte*)qy, (byte*)d,
  22062. ECC_CURVE_INVALID), BAD_FUNC_ARG);
  22063. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  22064. ExpectIntLT(ret = wc_ecc_import_unsigned(&key, (byte*)nullBytes,
  22065. (byte*)nullBytes, (byte*)nullBytes, curveId), 0);
  22066. ExpectTrue((ret == ECC_INF_E) || (ret == BAD_FUNC_ARG));
  22067. #endif
  22068. wc_ecc_free(&key);
  22069. #endif
  22070. return EXPECT_RESULT();
  22071. } /* END test_wc_ecc_import_unsigned */
  22072. /*
  22073. * Testing wc_ecc_sig_size()
  22074. */
  22075. static int test_wc_ecc_sig_size(void)
  22076. {
  22077. EXPECT_DECLS;
  22078. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22079. ecc_key key;
  22080. WC_RNG rng;
  22081. int keySz = KEY16;
  22082. int ret;
  22083. XMEMSET(&rng, 0, sizeof(rng));
  22084. XMEMSET(&key, 0, sizeof(key));
  22085. ExpectIntEQ(wc_ecc_init(&key), 0);
  22086. ExpectIntEQ(wc_InitRng(&rng), 0);
  22087. ret = wc_ecc_make_key(&rng, keySz, &key);
  22088. #if defined(WOLFSSL_ASYNC_CRYPT)
  22089. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22090. #endif
  22091. ExpectIntEQ(ret, 0);
  22092. ExpectIntLE(wc_ecc_sig_size(&key),
  22093. (2 * keySz + SIG_HEADER_SZ + ECC_MAX_PAD_SZ));
  22094. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22095. wc_ecc_free(&key);
  22096. #endif
  22097. return EXPECT_RESULT();
  22098. } /* END test_wc_ecc_sig_size */
  22099. /*
  22100. * Testing wc_ecc_ctx_new()
  22101. */
  22102. static int test_wc_ecc_ctx_new(void)
  22103. {
  22104. EXPECT_DECLS;
  22105. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22106. WC_RNG rng;
  22107. ecEncCtx* cli = NULL;
  22108. ecEncCtx* srv = NULL;
  22109. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22110. ExpectIntEQ(wc_InitRng(&rng), 0);
  22111. ExpectNotNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22112. ExpectNotNull(srv = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  22113. wc_ecc_ctx_free(cli);
  22114. cli = NULL;
  22115. wc_ecc_ctx_free(srv);
  22116. /* Test bad args. */
  22117. /* wc_ecc_ctx_new_ex() will free if returned NULL. */
  22118. ExpectNull(cli = wc_ecc_ctx_new(0, &rng));
  22119. ExpectNull(cli = wc_ecc_ctx_new(REQ_RESP_CLIENT, NULL));
  22120. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22121. wc_ecc_ctx_free(cli);
  22122. #endif
  22123. return EXPECT_RESULT();
  22124. } /* END test_wc_ecc_ctx_new */
  22125. /*
  22126. * Tesing wc_ecc_reset()
  22127. */
  22128. static int test_wc_ecc_ctx_reset(void)
  22129. {
  22130. EXPECT_DECLS;
  22131. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22132. ecEncCtx* ctx = NULL;
  22133. WC_RNG rng;
  22134. XMEMSET(&rng, 0, sizeof(rng));
  22135. ExpectIntEQ(wc_InitRng(&rng), 0);
  22136. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22137. ExpectIntEQ(wc_ecc_ctx_reset(ctx, &rng), 0);
  22138. /* Pass in bad args. */
  22139. ExpectIntEQ(wc_ecc_ctx_reset(NULL, &rng), BAD_FUNC_ARG);
  22140. ExpectIntEQ(wc_ecc_ctx_reset(ctx, NULL), BAD_FUNC_ARG);
  22141. wc_ecc_ctx_free(ctx);
  22142. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22143. #endif
  22144. return EXPECT_RESULT();
  22145. } /* END test_wc_ecc_ctx_reset */
  22146. /*
  22147. * Testing wc_ecc_ctx_set_peer_salt() and wc_ecc_ctx_get_own_salt()
  22148. */
  22149. static int test_wc_ecc_ctx_set_peer_salt(void)
  22150. {
  22151. EXPECT_DECLS;
  22152. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22153. WC_RNG rng;
  22154. ecEncCtx* cliCtx = NULL;
  22155. ecEncCtx* servCtx = NULL;
  22156. const byte* cliSalt = NULL;
  22157. const byte* servSalt = NULL;
  22158. XMEMSET(&rng, 0, sizeof(rng));
  22159. ExpectIntEQ(wc_InitRng(&rng), 0);
  22160. ExpectNotNull(cliCtx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22161. ExpectNotNull(servCtx = wc_ecc_ctx_new(REQ_RESP_SERVER, &rng));
  22162. /* Test bad args. */
  22163. ExpectNull(cliSalt = wc_ecc_ctx_get_own_salt(NULL));
  22164. ExpectNotNull(cliSalt = wc_ecc_ctx_get_own_salt(cliCtx));
  22165. ExpectNotNull(servSalt = wc_ecc_ctx_get_own_salt(servCtx));
  22166. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, servSalt), 0);
  22167. /* Test bad args. */
  22168. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(NULL, servSalt), BAD_FUNC_ARG);
  22169. ExpectIntEQ(wc_ecc_ctx_set_peer_salt(cliCtx, NULL), BAD_FUNC_ARG);
  22170. wc_ecc_ctx_free(cliCtx);
  22171. wc_ecc_ctx_free(servCtx);
  22172. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22173. #endif
  22174. return EXPECT_RESULT();
  22175. } /* END test_wc_ecc_ctx_set_peer_salt */
  22176. /*
  22177. * Testing wc_ecc_ctx_set_info()
  22178. */
  22179. static int test_wc_ecc_ctx_set_info(void)
  22180. {
  22181. EXPECT_DECLS;
  22182. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG)
  22183. ecEncCtx* ctx = NULL;
  22184. WC_RNG rng;
  22185. const char* optInfo = "Optional Test Info.";
  22186. int optInfoSz = (int)XSTRLEN(optInfo);
  22187. const char* badOptInfo = NULL;
  22188. XMEMSET(&rng, 0, sizeof(rng));
  22189. ExpectIntEQ(wc_InitRng(&rng), 0);
  22190. ExpectNotNull(ctx = wc_ecc_ctx_new(REQ_RESP_CLIENT, &rng));
  22191. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, optInfoSz), 0);
  22192. /* Test bad args. */
  22193. ExpectIntEQ(wc_ecc_ctx_set_info(NULL, (byte*)optInfo, optInfoSz),
  22194. BAD_FUNC_ARG);
  22195. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)badOptInfo, optInfoSz),
  22196. BAD_FUNC_ARG);
  22197. ExpectIntEQ(wc_ecc_ctx_set_info(ctx, (byte*)optInfo, -1), BAD_FUNC_ARG);
  22198. wc_ecc_ctx_free(ctx);
  22199. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22200. #endif
  22201. return EXPECT_RESULT();
  22202. } /* END test_wc_ecc_ctx_set_info */
  22203. /*
  22204. * Testing wc_ecc_encrypt() and wc_ecc_decrypt()
  22205. */
  22206. static int test_wc_ecc_encryptDecrypt(void)
  22207. {
  22208. EXPECT_DECLS;
  22209. #if defined(HAVE_ECC) && defined(HAVE_ECC_ENCRYPT) && !defined(WC_NO_RNG) && \
  22210. defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  22211. ecc_key srvKey;
  22212. ecc_key cliKey;
  22213. ecc_key tmpKey;
  22214. WC_RNG rng;
  22215. int ret;
  22216. const char* msg = "EccBlock Size 16";
  22217. word32 msgSz = (word32)XSTRLEN("EccBlock Size 16");
  22218. #ifdef WOLFSSL_ECIES_OLD
  22219. byte out[(sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22220. #elif defined(WOLFSSL_ECIES_GEN_IV)
  22221. byte out[KEY20 * 2 + 1 + AES_BLOCK_SIZE +
  22222. (sizeof("EccBlock Size 16") - 1) + WC_SHA256_DIGEST_SIZE];
  22223. #else
  22224. byte out[KEY20 * 2 + 1 + (sizeof("EccBlock Size 16") - 1) +
  22225. WC_SHA256_DIGEST_SIZE];
  22226. #endif
  22227. word32 outSz = (word32)sizeof(out);
  22228. byte plain[sizeof("EccBlock Size 16")];
  22229. word32 plainSz = (word32)sizeof(plain);
  22230. int keySz = KEY20;
  22231. /* Init stack variables. */
  22232. XMEMSET(out, 0, outSz);
  22233. XMEMSET(plain, 0, plainSz);
  22234. XMEMSET(&rng, 0, sizeof(rng));
  22235. XMEMSET(&srvKey, 0, sizeof(ecc_key));
  22236. XMEMSET(&cliKey, 0, sizeof(ecc_key));
  22237. XMEMSET(&tmpKey, 0, sizeof(ecc_key));
  22238. ExpectIntEQ(wc_InitRng(&rng), 0);
  22239. ExpectIntEQ(wc_ecc_init(&cliKey), 0);
  22240. ret = wc_ecc_make_key(&rng, keySz, &cliKey);
  22241. #if defined(WOLFSSL_ASYNC_CRYPT)
  22242. ret = wc_AsyncWait(ret, &cliKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22243. #endif
  22244. ExpectIntEQ(ret, 0);
  22245. ExpectIntEQ(wc_ecc_init(&srvKey), 0);
  22246. ret = wc_ecc_make_key(&rng, keySz, &srvKey);
  22247. #if defined(WOLFSSL_ASYNC_CRYPT)
  22248. ret = wc_AsyncWait(ret, &srvKey.asyncDev, WC_ASYNC_FLAG_NONE);
  22249. #endif
  22250. ExpectIntEQ(ret, 0);
  22251. ExpectIntEQ(wc_ecc_init(&tmpKey), 0);
  22252. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22253. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22254. !defined(HAVE_SELFTEST)
  22255. ExpectIntEQ(wc_ecc_set_rng(&srvKey, &rng), 0);
  22256. ExpectIntEQ(wc_ecc_set_rng(&cliKey, &rng), 0);
  22257. #endif
  22258. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out,
  22259. &outSz, NULL), 0);
  22260. /* Test bad args. */
  22261. ExpectIntEQ(wc_ecc_encrypt(NULL, &srvKey, (byte*)msg, msgSz, out, &outSz,
  22262. NULL), BAD_FUNC_ARG);
  22263. ExpectIntEQ(wc_ecc_encrypt(&cliKey, NULL, (byte*)msg, msgSz, out, &outSz,
  22264. NULL), BAD_FUNC_ARG);
  22265. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, NULL, msgSz, out, &outSz,
  22266. NULL), BAD_FUNC_ARG);
  22267. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, NULL,
  22268. &outSz, NULL), BAD_FUNC_ARG);
  22269. ExpectIntEQ(wc_ecc_encrypt(&cliKey, &srvKey, (byte*)msg, msgSz, out, NULL,
  22270. NULL), BAD_FUNC_ARG);
  22271. #ifdef WOLFSSL_ECIES_OLD
  22272. tmpKey.dp = cliKey.dp;
  22273. ExpectIntEQ(wc_ecc_copy_point(&cliKey.pubkey, &tmpKey.pubkey), 0);
  22274. #endif
  22275. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, &plainSz,
  22276. NULL), 0);
  22277. ExpectIntEQ(wc_ecc_decrypt(NULL, &tmpKey, out, outSz, plain, &plainSz,
  22278. NULL), BAD_FUNC_ARG);
  22279. #ifdef WOLFSSL_ECIES_OLD
  22280. /* NULL parameter allowed in new implementations - public key comes from
  22281. * the message. */
  22282. ExpectIntEQ(wc_ecc_decrypt(&srvKey, NULL, out, outSz, plain, &plainSz,
  22283. NULL), BAD_FUNC_ARG);
  22284. #endif
  22285. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, NULL, outSz, plain, &plainSz,
  22286. NULL), BAD_FUNC_ARG);
  22287. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, NULL, &plainSz,
  22288. NULL), BAD_FUNC_ARG);
  22289. ExpectIntEQ(wc_ecc_decrypt(&srvKey, &tmpKey, out, outSz, plain, NULL, NULL),
  22290. BAD_FUNC_ARG);
  22291. ExpectIntEQ(XMEMCMP(msg, plain, msgSz), 0);
  22292. wc_ecc_free(&tmpKey);
  22293. wc_ecc_free(&srvKey);
  22294. wc_ecc_free(&cliKey);
  22295. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22296. #endif
  22297. return EXPECT_RESULT();
  22298. } /* END test_wc_ecc_encryptDecrypt */
  22299. /*
  22300. * Testing wc_ecc_del_point() and wc_ecc_new_point()
  22301. */
  22302. static int test_wc_ecc_del_point(void)
  22303. {
  22304. EXPECT_DECLS;
  22305. #if defined(HAVE_ECC)
  22306. ecc_point* pt = NULL;
  22307. ExpectNotNull(pt = wc_ecc_new_point());
  22308. wc_ecc_del_point(pt);
  22309. #endif
  22310. return EXPECT_RESULT();
  22311. } /* END test_wc_ecc_del_point */
  22312. /*
  22313. * Testing wc_ecc_point_is_at_infinity(), wc_ecc_export_point_der(),
  22314. * wc_ecc_import_point_der(), wc_ecc_copy_point(), wc_ecc_point_is_on_curve(),
  22315. * and wc_ecc_cmp_point()
  22316. */
  22317. static int test_wc_ecc_pointFns(void)
  22318. {
  22319. EXPECT_DECLS;
  22320. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && \
  22321. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22322. !defined(WOLFSSL_ATECC608A)
  22323. ecc_key key;
  22324. WC_RNG rng;
  22325. int ret;
  22326. ecc_point* point = NULL;
  22327. ecc_point* cpypt = NULL;
  22328. int idx = 0;
  22329. int keySz = KEY32;
  22330. byte der[DER_SZ(KEY32)];
  22331. word32 derlenChk = 0;
  22332. word32 derSz = DER_SZ(KEY32);
  22333. /* Init stack variables. */
  22334. XMEMSET(der, 0, derSz);
  22335. XMEMSET(&key, 0, sizeof(ecc_key));
  22336. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22337. ExpectIntEQ(wc_InitRng(&rng), 0);
  22338. ExpectIntEQ(wc_ecc_init(&key), 0);
  22339. ret = wc_ecc_make_key(&rng, keySz, &key);
  22340. #if defined(WOLFSSL_ASYNC_CRYPT)
  22341. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22342. #endif
  22343. ExpectIntEQ(ret, 0);
  22344. ExpectNotNull(point = wc_ecc_new_point());
  22345. ExpectNotNull(cpypt = wc_ecc_new_point());
  22346. /* Export */
  22347. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, NULL,
  22348. &derlenChk), LENGTH_ONLY_E);
  22349. /* Check length value. */
  22350. ExpectIntEQ(derSz, derlenChk);
  22351. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  22352. &derSz), 0);
  22353. /* Test bad args. */
  22354. ExpectIntEQ(wc_ecc_export_point_der(-2, &key.pubkey, der, &derSz),
  22355. ECC_BAD_ARG_E);
  22356. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), NULL, der, &derSz),
  22357. ECC_BAD_ARG_E);
  22358. ExpectIntEQ(wc_ecc_export_point_der((idx = key.idx), &key.pubkey, der,
  22359. NULL), ECC_BAD_ARG_E);
  22360. /* Import */
  22361. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, point), 0);
  22362. ExpectIntEQ(wc_ecc_cmp_point(&key.pubkey, point), 0);
  22363. /* Test bad args. */
  22364. ExpectIntEQ( wc_ecc_import_point_der(NULL, derSz, idx, point),
  22365. ECC_BAD_ARG_E);
  22366. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, idx, NULL), ECC_BAD_ARG_E);
  22367. ExpectIntEQ(wc_ecc_import_point_der(der, derSz, -1, point), ECC_BAD_ARG_E);
  22368. ExpectIntEQ(wc_ecc_import_point_der(der, derSz + 1, idx, point),
  22369. ECC_BAD_ARG_E);
  22370. /* Copy */
  22371. ExpectIntEQ(wc_ecc_copy_point(point, cpypt), 0);
  22372. /* Test bad args. */
  22373. ExpectIntEQ(wc_ecc_copy_point(NULL, cpypt), ECC_BAD_ARG_E);
  22374. ExpectIntEQ(wc_ecc_copy_point(point, NULL), ECC_BAD_ARG_E);
  22375. /* Compare point */
  22376. ExpectIntEQ(wc_ecc_cmp_point(point, cpypt), 0);
  22377. /* Test bad args. */
  22378. ExpectIntEQ(wc_ecc_cmp_point(NULL, cpypt), BAD_FUNC_ARG);
  22379. ExpectIntEQ(wc_ecc_cmp_point(point, NULL), BAD_FUNC_ARG);
  22380. /* At infinity if return == 1, otherwise return == 0. */
  22381. ExpectIntEQ(wc_ecc_point_is_at_infinity(point), 0);
  22382. /* Test bad args. */
  22383. ExpectIntEQ(wc_ecc_point_is_at_infinity(NULL), BAD_FUNC_ARG);
  22384. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  22385. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  22386. #ifdef USE_ECC_B_PARAM
  22387. /* On curve if ret == 0 */
  22388. ExpectIntEQ(wc_ecc_point_is_on_curve(point, idx), 0);
  22389. /* Test bad args. */
  22390. ExpectIntEQ(wc_ecc_point_is_on_curve(NULL, idx), BAD_FUNC_ARG);
  22391. ExpectIntEQ(wc_ecc_point_is_on_curve(point, 1000), ECC_BAD_ARG_E);
  22392. #endif /* USE_ECC_B_PARAM */
  22393. #endif /* !HAVE_SELFTEST && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  22394. /* Free */
  22395. wc_ecc_del_point(point);
  22396. wc_ecc_del_point(cpypt);
  22397. wc_ecc_free(&key);
  22398. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22399. #endif
  22400. return EXPECT_RESULT();
  22401. } /* END test_wc_ecc_pointFns */
  22402. /*
  22403. * Testing wc_ecc_shared_secret_ssh()
  22404. */
  22405. static int test_wc_ecc_shared_secret_ssh(void)
  22406. {
  22407. EXPECT_DECLS;
  22408. #if defined(HAVE_ECC) && defined(HAVE_ECC_DHE) && \
  22409. !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22410. !defined(WOLFSSL_ATECC608A) && !defined(PLUTON_CRYPTO_ECC) && \
  22411. !defined(WOLFSSL_CRYPTOCELL)
  22412. ecc_key key;
  22413. ecc_key key2;
  22414. WC_RNG rng;
  22415. int ret;
  22416. int keySz = KEY32;
  22417. #if FIPS_VERSION3_GE(6,0,0)
  22418. int key2Sz = KEY28;
  22419. #else
  22420. int key2Sz = KEY24;
  22421. #endif
  22422. byte secret[KEY32];
  22423. word32 secretLen = (word32)keySz;
  22424. /* Init stack variables. */
  22425. XMEMSET(&key, 0, sizeof(ecc_key));
  22426. XMEMSET(&key2, 0, sizeof(ecc_key));
  22427. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22428. XMEMSET(secret, 0, secretLen);
  22429. /* Make keys */
  22430. ExpectIntEQ(wc_ecc_init(&key), 0);
  22431. ExpectIntEQ(wc_InitRng(&rng), 0);
  22432. ret = wc_ecc_make_key(&rng, keySz, &key);
  22433. #if defined(WOLFSSL_ASYNC_CRYPT)
  22434. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22435. #endif
  22436. ExpectIntEQ(ret, 0);
  22437. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22438. ExpectIntEQ(wc_ecc_init(&key2), 0);
  22439. ExpectIntEQ(wc_InitRng(&rng), 0);
  22440. ret = wc_ecc_make_key(&rng, key2Sz, &key2);
  22441. #if defined(WOLFSSL_ASYNC_CRYPT)
  22442. ret = wc_AsyncWait(ret, &key2.asyncDev, WC_ASYNC_FLAG_NONE);
  22443. #endif
  22444. ExpectIntEQ(ret, 0);
  22445. #if defined(ECC_TIMING_RESISTANT) && (!defined(HAVE_FIPS) || \
  22446. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION != 2))) && \
  22447. !defined(HAVE_SELFTEST)
  22448. ExpectIntEQ(wc_ecc_set_rng(&key, &rng), 0);
  22449. #endif
  22450. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  22451. &secretLen), 0);
  22452. /* Pass in bad args. */
  22453. ExpectIntEQ(wc_ecc_shared_secret_ssh(NULL, &key2.pubkey, secret,
  22454. &secretLen), BAD_FUNC_ARG);
  22455. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, NULL, secret, &secretLen),
  22456. BAD_FUNC_ARG);
  22457. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, NULL, &secretLen),
  22458. BAD_FUNC_ARG);
  22459. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret, NULL),
  22460. BAD_FUNC_ARG);
  22461. key.type = ECC_PUBLICKEY;
  22462. ExpectIntEQ(wc_ecc_shared_secret_ssh(&key, &key2.pubkey, secret,
  22463. &secretLen), ECC_BAD_ARG_E);
  22464. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22465. wc_ecc_free(&key);
  22466. wc_ecc_free(&key2);
  22467. #ifdef FP_ECC
  22468. wc_ecc_fp_free();
  22469. #endif
  22470. #endif
  22471. return EXPECT_RESULT();
  22472. } /* END test_wc_ecc_shared_secret_ssh */
  22473. /*
  22474. * Testing wc_ecc_verify_hash_ex() and wc_ecc_verify_hash_ex()
  22475. */
  22476. static int test_wc_ecc_verify_hash_ex(void)
  22477. {
  22478. EXPECT_DECLS;
  22479. #if defined(HAVE_ECC) && defined(HAVE_ECC_SIGN) && defined(WOLFSSL_PUBLIC_MP) \
  22480. && !defined(WC_NO_RNG) && !defined(WOLFSSL_ATECC508A) && \
  22481. !defined(WOLFSSL_ATECC608A) && !defined(WOLFSSL_KCAPI_ECC)
  22482. ecc_key key;
  22483. WC_RNG rng;
  22484. int ret;
  22485. mp_int r;
  22486. mp_int s;
  22487. mp_int z;
  22488. unsigned char hash[] = "Everyone gets Friday off.EccSig";
  22489. unsigned char iHash[] = "Everyone gets Friday off.......";
  22490. unsigned char shortHash[] = TEST_STRING;
  22491. word32 hashlen = sizeof(hash);
  22492. word32 iHashLen = sizeof(iHash);
  22493. word32 shortHashLen = sizeof(shortHash);
  22494. int keySz = KEY32;
  22495. int verify_ok = 0;
  22496. XMEMSET(&key, 0, sizeof(ecc_key));
  22497. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22498. XMEMSET(&r, 0, sizeof(mp_int));
  22499. XMEMSET(&s, 0, sizeof(mp_int));
  22500. XMEMSET(&z, 0, sizeof(mp_int));
  22501. /* Initialize r, s and z. */
  22502. ExpectIntEQ(mp_init_multi(&r, &s, &z, NULL, NULL, NULL), MP_OKAY);
  22503. ExpectIntEQ(wc_ecc_init(&key), 0);
  22504. ExpectIntEQ(wc_InitRng(&rng), 0);
  22505. ret = wc_ecc_make_key(&rng, keySz, &key);
  22506. #if defined(WOLFSSL_ASYNC_CRYPT)
  22507. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22508. #endif
  22509. ExpectIntEQ(ret, 0);
  22510. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, &s), 0);
  22511. /* verify_ok should be 1. */
  22512. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, hash, hashlen, &verify_ok, &key),
  22513. 0);
  22514. ExpectIntEQ(verify_ok, 1);
  22515. /* verify_ok should be 0 */
  22516. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, iHash, iHashLen, &verify_ok,
  22517. &key), 0);
  22518. ExpectIntEQ(verify_ok, 0);
  22519. /* verify_ok should be 0. */
  22520. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22521. &verify_ok, &key), 0);
  22522. ExpectIntEQ(verify_ok, 0);
  22523. /* Test bad args. */
  22524. ExpectIntEQ(wc_ecc_sign_hash_ex(NULL, hashlen, &rng, &key, &r, &s),
  22525. ECC_BAD_ARG_E);
  22526. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, NULL, &key, &r, &s),
  22527. ECC_BAD_ARG_E);
  22528. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, NULL, &r, &s),
  22529. ECC_BAD_ARG_E);
  22530. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, NULL, &s),
  22531. ECC_BAD_ARG_E);
  22532. ExpectIntEQ(wc_ecc_sign_hash_ex(hash, hashlen, &rng, &key, &r, NULL),
  22533. ECC_BAD_ARG_E);
  22534. /* Test bad args. */
  22535. ExpectIntEQ(wc_ecc_verify_hash_ex(NULL, &s, shortHash, shortHashLen,
  22536. &verify_ok, &key), ECC_BAD_ARG_E);
  22537. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, NULL, shortHash, shortHashLen,
  22538. &verify_ok, &key), ECC_BAD_ARG_E);
  22539. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &s, shortHash, shortHashLen,
  22540. &verify_ok, &key), MP_ZERO_E);
  22541. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &z, shortHash, shortHashLen,
  22542. &verify_ok, &key), MP_ZERO_E);
  22543. ExpectIntEQ(wc_ecc_verify_hash_ex(&z, &z, shortHash, shortHashLen,
  22544. &verify_ok, &key), MP_ZERO_E);
  22545. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, NULL, shortHashLen, &verify_ok,
  22546. &key), ECC_BAD_ARG_E);
  22547. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen, NULL,
  22548. &key), ECC_BAD_ARG_E);
  22549. ExpectIntEQ(wc_ecc_verify_hash_ex(&r, &s, shortHash, shortHashLen,
  22550. &verify_ok, NULL), ECC_BAD_ARG_E);
  22551. wc_ecc_free(&key);
  22552. mp_free(&r);
  22553. mp_free(&s);
  22554. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22555. #endif
  22556. return EXPECT_RESULT();
  22557. } /* END test_wc_ecc_verify_hash_ex */
  22558. /*
  22559. * Testing wc_ecc_mulmod()
  22560. */
  22561. static int test_wc_ecc_mulmod(void)
  22562. {
  22563. EXPECT_DECLS;
  22564. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && \
  22565. !(defined(WOLFSSL_ATECC508A) || defined(WOLFSSL_ATECC608A) || \
  22566. defined(WOLFSSL_VALIDATE_ECC_IMPORT))
  22567. ecc_key key1;
  22568. ecc_key key2;
  22569. ecc_key key3;
  22570. WC_RNG rng;
  22571. int ret;
  22572. XMEMSET(&key1, 0, sizeof(ecc_key));
  22573. XMEMSET(&key2, 0, sizeof(ecc_key));
  22574. XMEMSET(&key3, 0, sizeof(ecc_key));
  22575. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22576. ExpectIntEQ(wc_ecc_init(&key1), 0);
  22577. ExpectIntEQ(wc_ecc_init(&key2), 0);
  22578. ExpectIntEQ(wc_ecc_init(&key3), 0);
  22579. ExpectIntEQ(wc_InitRng(&rng), 0);
  22580. ret = wc_ecc_make_key(&rng, KEY32, &key1);
  22581. #if defined(WOLFSSL_ASYNC_CRYPT)
  22582. ret = wc_AsyncWait(ret, &key1.asyncDev, WC_ASYNC_FLAG_NONE);
  22583. #endif
  22584. ExpectIntEQ(ret, 0);
  22585. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22586. ExpectIntEQ(wc_ecc_import_raw_ex(&key2, key1.dp->Gx, key1.dp->Gy,
  22587. key1.dp->Af, ECC_SECP256R1), 0);
  22588. ExpectIntEQ(wc_ecc_import_raw_ex(&key3, key1.dp->Gx, key1.dp->Gy,
  22589. key1.dp->prime, ECC_SECP256R1), 0);
  22590. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  22591. &key3.pubkey, wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3),
  22592. 1), 0);
  22593. /* Test bad args. */
  22594. ExpectIntEQ(ret = wc_ecc_mulmod(NULL, &key2.pubkey, &key3.pubkey,
  22595. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22596. ECC_BAD_ARG_E);
  22597. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), NULL, &key3.pubkey,
  22598. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22599. ECC_BAD_ARG_E);
  22600. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey, NULL,
  22601. wc_ecc_key_get_priv(&key2), wc_ecc_key_get_priv(&key3), 1),
  22602. ECC_BAD_ARG_E);
  22603. ExpectIntEQ(wc_ecc_mulmod(wc_ecc_key_get_priv(&key1), &key2.pubkey,
  22604. &key3.pubkey, wc_ecc_key_get_priv(&key2), NULL, 1), ECC_BAD_ARG_E);
  22605. wc_ecc_free(&key1);
  22606. wc_ecc_free(&key2);
  22607. wc_ecc_free(&key3);
  22608. #ifdef FP_ECC
  22609. wc_ecc_fp_free();
  22610. #endif
  22611. #endif /* HAVE_ECC && !WOLFSSL_ATECC508A */
  22612. return EXPECT_RESULT();
  22613. } /* END test_wc_ecc_mulmod */
  22614. /*
  22615. * Testing wc_ecc_is_valid_idx()
  22616. */
  22617. static int test_wc_ecc_is_valid_idx(void)
  22618. {
  22619. EXPECT_DECLS;
  22620. #if defined(HAVE_ECC) && !defined(WC_NO_RNG)
  22621. ecc_key key;
  22622. WC_RNG rng;
  22623. int ret;
  22624. int iVal = -2;
  22625. int iVal2 = 3000;
  22626. XMEMSET(&key, 0, sizeof(ecc_key));
  22627. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22628. ExpectIntEQ(wc_ecc_init(&key), 0);
  22629. ExpectIntEQ(wc_InitRng(&rng), 0);
  22630. ret = wc_ecc_make_key(&rng, 32, &key);
  22631. #if defined(WOLFSSL_ASYNC_CRYPT)
  22632. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22633. #endif
  22634. ExpectIntEQ(ret, 0);
  22635. ExpectIntEQ(wc_ecc_is_valid_idx(key.idx), 1);
  22636. /* Test bad args. */
  22637. ExpectIntEQ(wc_ecc_is_valid_idx(iVal), 0);
  22638. ExpectIntEQ(wc_ecc_is_valid_idx(iVal2), 0);
  22639. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22640. wc_ecc_free(&key);
  22641. #ifdef FP_ECC
  22642. wc_ecc_fp_free();
  22643. #endif
  22644. #endif
  22645. return EXPECT_RESULT();
  22646. } /* END test_wc_ecc_is_valid_idx */
  22647. /*
  22648. * Testing wc_ecc_get_curve_id_from_oid()
  22649. */
  22650. static int test_wc_ecc_get_curve_id_from_oid(void)
  22651. {
  22652. EXPECT_DECLS;
  22653. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(HAVE_SELFTEST) && \
  22654. !defined(HAVE_FIPS)
  22655. const byte oid[] = {0x2A,0x86,0x48,0xCE,0x3D,0x03,0x01,0x07};
  22656. word32 len = sizeof(oid);
  22657. /* Bad Cases */
  22658. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(NULL, len), BAD_FUNC_ARG);
  22659. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, 0), ECC_CURVE_INVALID);
  22660. /* Good Case */
  22661. ExpectIntEQ(wc_ecc_get_curve_id_from_oid(oid, len), ECC_SECP256R1);
  22662. #endif
  22663. return EXPECT_RESULT();
  22664. } /* END test_wc_ecc_get_curve_id_from_oid */
  22665. /*
  22666. * Testing wc_ecc_sig_size_calc()
  22667. */
  22668. static int test_wc_ecc_sig_size_calc(void)
  22669. {
  22670. EXPECT_DECLS;
  22671. #if defined(HAVE_ECC) && !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST)
  22672. ecc_key key;
  22673. WC_RNG rng;
  22674. int sz = 0;
  22675. int ret;
  22676. XMEMSET(&key, 0, sizeof(ecc_key));
  22677. XMEMSET(&rng, 0, sizeof(WC_RNG));
  22678. ExpectIntEQ(wc_ecc_init(&key), 0);
  22679. ExpectIntEQ(wc_InitRng(&rng), 0);
  22680. ret = wc_ecc_make_key(&rng, 16, &key);
  22681. #if defined(WOLFSSL_ASYNC_CRYPT)
  22682. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_NONE);
  22683. #endif
  22684. #if FIPS_VERSION3_GE(6,0,0)
  22685. ExpectIntEQ(ret, BAD_FUNC_ARG);
  22686. #else
  22687. ExpectIntEQ(ret, 0);
  22688. #endif
  22689. #if FIPS_VERSION3_LT(6,0,0)
  22690. sz = key.dp->size;
  22691. ExpectIntGT(wc_ecc_sig_size_calc(sz), 0);
  22692. #else
  22693. (void) sz;
  22694. #endif
  22695. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  22696. wc_ecc_free(&key);
  22697. #endif
  22698. return EXPECT_RESULT();
  22699. } /* END test_wc_ecc_sig_size_calc */
  22700. /*
  22701. * Testing wc_ecc_sm2_make_key()
  22702. */
  22703. static int test_wc_ecc_sm2_make_key(void)
  22704. {
  22705. int res = TEST_SKIPPED;
  22706. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  22707. EXPECT_DECLS;
  22708. WC_RNG rng[1];
  22709. ecc_key key[1];
  22710. XMEMSET(rng, 0, sizeof(*rng));
  22711. XMEMSET(key, 0, sizeof(*key));
  22712. ExpectIntEQ(wc_InitRng(rng), 0);
  22713. ExpectIntEQ(wc_ecc_init(key), 0);
  22714. /* Test invalid parameters. */
  22715. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, NULL, WC_ECC_FLAG_NONE),
  22716. BAD_FUNC_ARG);
  22717. ExpectIntEQ(wc_ecc_sm2_make_key(rng, NULL, WC_ECC_FLAG_NONE),
  22718. BAD_FUNC_ARG);
  22719. ExpectIntEQ(wc_ecc_sm2_make_key(NULL, key, WC_ECC_FLAG_NONE),
  22720. BAD_FUNC_ARG);
  22721. /* Test valid parameters. */
  22722. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  22723. ExpectIntEQ(key->dp->id, ECC_SM2P256V1);
  22724. wc_ecc_free(key);
  22725. wc_FreeRng(rng);
  22726. #ifdef FP_ECC
  22727. wc_ecc_fp_free();
  22728. #endif
  22729. res = EXPECT_RESULT();
  22730. #endif
  22731. return res;
  22732. }
  22733. /*
  22734. * Testing wc_ecc_sm2_shared_secret()
  22735. */
  22736. static int test_wc_ecc_sm2_shared_secret(void)
  22737. {
  22738. int res = TEST_SKIPPED;
  22739. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2)
  22740. EXPECT_DECLS;
  22741. WC_RNG rng[1];
  22742. ecc_key keyA[1];
  22743. ecc_key keyB[1];
  22744. byte outA[32];
  22745. byte outB[32];
  22746. word32 outALen = 32;
  22747. word32 outBLen = 32;
  22748. XMEMSET(rng, 0, sizeof(*rng));
  22749. XMEMSET(keyA, 0, sizeof(*keyA));
  22750. XMEMSET(keyB, 0, sizeof(*keyB));
  22751. ExpectIntEQ(wc_InitRng(rng), 0);
  22752. ExpectIntEQ(wc_ecc_init(keyA), 0);
  22753. ExpectIntEQ(wc_ecc_init(keyB), 0);
  22754. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyA, WC_ECC_FLAG_NONE), 0);
  22755. ExpectIntEQ(wc_ecc_sm2_make_key(rng, keyB, WC_ECC_FLAG_NONE), 0);
  22756. #ifdef ECC_TIMING_RESISTANT
  22757. ExpectIntEQ(wc_ecc_set_rng(keyA, rng), 0);
  22758. ExpectIntEQ(wc_ecc_set_rng(keyB, rng), 0);
  22759. #endif
  22760. /* Test invalid parameters. */
  22761. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, NULL), BAD_FUNC_ARG);
  22762. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, NULL, NULL), BAD_FUNC_ARG);
  22763. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, NULL, NULL), BAD_FUNC_ARG);
  22764. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, outA, NULL), BAD_FUNC_ARG);
  22765. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, NULL, NULL, &outALen),
  22766. BAD_FUNC_ARG);
  22767. ExpectIntEQ(wc_ecc_sm2_shared_secret(NULL, keyB, outA, &outALen),
  22768. BAD_FUNC_ARG);
  22769. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, NULL, outA, &outALen),
  22770. BAD_FUNC_ARG);
  22771. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, NULL, &outALen),
  22772. BAD_FUNC_ARG);
  22773. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, NULL), BAD_FUNC_ARG);
  22774. /* Test valid parameters. */
  22775. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyA, keyB, outA, &outALen), 0);
  22776. ExpectIntLE(outALen, 32);
  22777. ExpectIntEQ(wc_ecc_sm2_shared_secret(keyB, keyA, outB, &outBLen), 0);
  22778. ExpectIntLE(outBLen, 32);
  22779. ExpectIntEQ(outALen, outBLen);
  22780. ExpectBufEQ(outA, outB, outALen);
  22781. wc_ecc_free(keyB);
  22782. wc_ecc_free(keyA);
  22783. wc_FreeRng(rng);
  22784. #ifdef FP_ECC
  22785. wc_ecc_fp_free();
  22786. #endif
  22787. res = EXPECT_RESULT();
  22788. #endif
  22789. return res;
  22790. }
  22791. /*
  22792. * Testing wc_ecc_sm2_create_digest()
  22793. */
  22794. static int test_wc_ecc_sm2_create_digest(void)
  22795. {
  22796. int res = TEST_SKIPPED;
  22797. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && !defined(NO_HASH_WRAPPER) && \
  22798. (defined(WOLFSSL_SM3) || !defined(NO_SHA256))
  22799. EXPECT_DECLS;
  22800. ecc_key key[1];
  22801. enum wc_HashType hashType;
  22802. unsigned char pub[] = {
  22803. 0x04,
  22804. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22805. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22806. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22807. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22808. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22809. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22810. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22811. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22812. };
  22813. unsigned char id[] = {
  22814. 0x01, 0x02, 0x03,
  22815. };
  22816. unsigned char msg[] = {
  22817. 0x01, 0x02, 0x03,
  22818. };
  22819. unsigned char hash[32];
  22820. #ifdef WOLFSSL_SM3
  22821. unsigned char expHash[32] = {
  22822. 0xc1, 0xdd, 0x92, 0xc5, 0x60, 0xd3, 0x94, 0x28,
  22823. 0xeb, 0x0f, 0x57, 0x79, 0x3f, 0xc9, 0x96, 0xc5,
  22824. 0xfa, 0xf5, 0x90, 0xb2, 0x64, 0x2f, 0xaf, 0x9c,
  22825. 0xc8, 0x57, 0x21, 0x6a, 0x52, 0x7e, 0xf1, 0x95
  22826. };
  22827. #else
  22828. unsigned char expHash[32] = {
  22829. 0xea, 0x41, 0x55, 0x21, 0x61, 0x00, 0x5c, 0x9a,
  22830. 0x57, 0x35, 0x6b, 0x49, 0xca, 0x8f, 0x65, 0xc2,
  22831. 0x0e, 0x29, 0x0c, 0xa0, 0x1d, 0xa7, 0xc4, 0xed,
  22832. 0xdd, 0x51, 0x12, 0xf6, 0xe7, 0x55, 0xc5, 0xf4
  22833. };
  22834. #endif
  22835. #ifdef WOLFSSL_SM3
  22836. hashType = WC_HASH_TYPE_SM3;
  22837. #else
  22838. hashType = WC_HASH_TYPE_SHA256;
  22839. #endif
  22840. XMEMSET(key, 0, sizeof(*key));
  22841. ExpectIntEQ(wc_ecc_init(key), 0);
  22842. /* Test with no curve set. */
  22843. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22844. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22845. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22846. /* Test invalid parameters. */
  22847. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22848. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22849. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  22850. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22851. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  22852. hashType, NULL, sizeof(hash), NULL), BAD_FUNC_ARG);
  22853. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22854. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  22855. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), NULL, sizeof(msg),
  22856. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  22857. ExpectIntEQ(wc_ecc_sm2_create_digest(NULL, sizeof(id), msg, sizeof(msg),
  22858. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22859. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), NULL, sizeof(msg),
  22860. hashType, hash, sizeof(hash), key), BAD_FUNC_ARG);
  22861. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22862. hashType, NULL, sizeof(hash), key), BAD_FUNC_ARG);
  22863. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22864. hashType, hash, sizeof(hash), NULL), BAD_FUNC_ARG);
  22865. /* Bad hash type. */
  22866. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  22867. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22868. -1, hash, 0, key), BAD_FUNC_ARG);
  22869. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  22870. /* Bad hash size. */
  22871. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22872. hashType, hash, 0, key), BUFFER_E);
  22873. /* Test valid parameters. */
  22874. ExpectIntEQ(wc_ecc_sm2_create_digest(id, sizeof(id), msg, sizeof(msg),
  22875. hashType, hash, sizeof(hash), key), 0);
  22876. ExpectBufEQ(hash, expHash, sizeof(expHash));
  22877. wc_ecc_free(key);
  22878. res = EXPECT_RESULT();
  22879. #endif
  22880. return res;
  22881. }
  22882. /*
  22883. * Testing wc_ecc_sm2_verify_hash_ex()
  22884. */
  22885. static int test_wc_ecc_sm2_verify_hash_ex(void)
  22886. {
  22887. int res = TEST_SKIPPED;
  22888. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY) && \
  22889. defined(WOLFSSL_PUBLIC_MP)
  22890. EXPECT_DECLS;
  22891. ecc_key key[1];
  22892. mp_int r[1];
  22893. mp_int s[1];
  22894. int verified;
  22895. unsigned char pub[] = {
  22896. 0x04,
  22897. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  22898. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  22899. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  22900. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  22901. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  22902. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  22903. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  22904. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  22905. };
  22906. unsigned char hash[] = {
  22907. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  22908. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  22909. 0x50, 0x69, 0x5B, 0x20
  22910. };
  22911. unsigned char rData[] = {
  22912. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  22913. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  22914. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  22915. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE7,
  22916. };
  22917. unsigned char sData[] = {
  22918. 0x1D,
  22919. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  22920. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  22921. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  22922. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDC
  22923. };
  22924. unsigned char rBadData[] = {
  22925. 0xD2, 0xFC, 0xA3, 0x88, 0xE3, 0xDF, 0xA3, 0x00,
  22926. 0x73, 0x9B, 0x3C, 0x2A, 0x0D, 0xAD, 0x44, 0xA2,
  22927. 0xFC, 0x62, 0xD5, 0x6B, 0x84, 0x54, 0xD8, 0x40,
  22928. 0x22, 0x62, 0x3D, 0x5C, 0xA6, 0x61, 0x9B, 0xE8,
  22929. };
  22930. XMEMSET(key, 0, sizeof(*key));
  22931. XMEMSET(r, 0, sizeof(*r));
  22932. XMEMSET(s, 0, sizeof(*s));
  22933. ExpectIntEQ(mp_init(r), 0);
  22934. ExpectIntEQ(mp_init(s), 0);
  22935. ExpectIntEQ(mp_read_unsigned_bin(r, rData, sizeof(rData)), 0);
  22936. ExpectIntEQ(mp_read_unsigned_bin(s, sData, sizeof(sData)), 0);
  22937. ExpectIntEQ(wc_ecc_init(key), 0);
  22938. /* Test with no curve set. */
  22939. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22940. &verified, key), BAD_FUNC_ARG);
  22941. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  22942. /* Test invalid parameters. */
  22943. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22944. NULL, NULL), BAD_FUNC_ARG);
  22945. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, NULL, sizeof(hash),
  22946. NULL, NULL), BAD_FUNC_ARG);
  22947. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, NULL, sizeof(hash),
  22948. NULL, NULL), BAD_FUNC_ARG);
  22949. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, hash, sizeof(hash),
  22950. NULL, NULL), BAD_FUNC_ARG);
  22951. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22952. &verified, NULL), BAD_FUNC_ARG);
  22953. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, NULL, NULL, sizeof(hash),
  22954. NULL, key), BAD_FUNC_ARG);
  22955. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(NULL, s, hash, sizeof(hash),
  22956. &verified, key), BAD_FUNC_ARG);
  22957. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, NULL, hash, sizeof(hash),
  22958. &verified, key), BAD_FUNC_ARG);
  22959. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, NULL, sizeof(hash),
  22960. &verified, key), BAD_FUNC_ARG);
  22961. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22962. NULL, key), BAD_FUNC_ARG);
  22963. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22964. &verified, NULL), BAD_FUNC_ARG);
  22965. /* Make key not on the SM2 curve. */
  22966. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  22967. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22968. &verified, key), BAD_FUNC_ARG);
  22969. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  22970. /* Test valid parameters. */
  22971. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22972. &verified, key), 0);
  22973. ExpectIntEQ(verified, 1);
  22974. ExpectIntEQ(mp_read_unsigned_bin(r, rBadData, sizeof(rBadData)), 0);
  22975. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash),
  22976. &verified, key), 0);
  22977. ExpectIntEQ(verified, 0);
  22978. mp_free(s);
  22979. mp_free(r);
  22980. wc_ecc_free(key);
  22981. #ifdef FP_ECC
  22982. wc_ecc_fp_free();
  22983. #endif
  22984. res = EXPECT_RESULT();
  22985. #endif
  22986. return res;
  22987. }
  22988. /*
  22989. * Testing wc_ecc_sm2_verify_hash()
  22990. */
  22991. static int test_wc_ecc_sm2_verify_hash(void)
  22992. {
  22993. int res = TEST_SKIPPED;
  22994. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_VERIFY)
  22995. EXPECT_DECLS;
  22996. ecc_key key[1];
  22997. int verified;
  22998. unsigned char pub[] = {
  22999. 0x04,
  23000. 0x63, 0x7F, 0x1B, 0x13, 0x50, 0x36, 0xC9, 0x33,
  23001. 0xDC, 0x3F, 0x7A, 0x8E, 0xBB, 0x1B, 0x7B, 0x2F,
  23002. 0xD1, 0xDF, 0xBD, 0x26, 0x8D, 0x4F, 0x89, 0x4B,
  23003. 0x5A, 0xD4, 0x7D, 0xBD, 0xBE, 0xCD, 0x55, 0x8F,
  23004. 0xE8, 0x81, 0x01, 0xD0, 0x80, 0x48, 0xE3, 0x6C,
  23005. 0xCB, 0xF6, 0x1C, 0xA3, 0x8D, 0xDF, 0x7A, 0xBA,
  23006. 0x54, 0x2B, 0x44, 0x86, 0xE9, 0x9E, 0x49, 0xF3,
  23007. 0xA7, 0x47, 0x0A, 0x85, 0x7A, 0x09, 0x64, 0x33
  23008. };
  23009. unsigned char hash[] = {
  23010. 0x3B, 0xFA, 0x5F, 0xFB, 0xC4, 0x27, 0x8C, 0x9D,
  23011. 0x02, 0x3A, 0x19, 0xCB, 0x1E, 0xAA, 0xD2, 0xF1,
  23012. 0x50, 0x69, 0x5B, 0x20
  23013. };
  23014. unsigned char sig[] = {
  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, 0xDC
  23024. };
  23025. unsigned char sigBad[] = {
  23026. 0x30, 0x45, 0x02, 0x21, 0x00, 0xD2, 0xFC, 0xA3,
  23027. 0x88, 0xE3, 0xDF, 0xA3, 0x00, 0x73, 0x9B, 0x3C,
  23028. 0x2A, 0x0D, 0xAD, 0x44, 0xA2, 0xFC, 0x62, 0xD5,
  23029. 0x6B, 0x84, 0x54, 0xD8, 0x40, 0x22, 0x62, 0x3D,
  23030. 0x5C, 0xA6, 0x61, 0x9B, 0xE7, 0x02, 0x20, 0x1D,
  23031. 0xB5, 0xB5, 0xD9, 0xD8, 0xF1, 0x20, 0xDD, 0x97,
  23032. 0x92, 0xBF, 0x7E, 0x9B, 0x3F, 0xE6, 0x3C, 0x4B,
  23033. 0x03, 0xD8, 0x80, 0xBD, 0xB7, 0x27, 0x7E, 0x6A,
  23034. 0x84, 0x23, 0xDE, 0x61, 0x7C, 0x8D, 0xDD
  23035. };
  23036. XMEMSET(key, 0, sizeof(*key));
  23037. ExpectIntEQ(wc_ecc_init(key), 0);
  23038. /* Test with no curve set. */
  23039. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23040. &verified, key), BAD_FUNC_ARG);
  23041. ExpectIntEQ(wc_ecc_import_x963_ex(pub, sizeof(pub), key, ECC_SM2P256V1), 0);
  23042. /* Test invalid parameters. */
  23043. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  23044. NULL, NULL), BAD_FUNC_ARG);
  23045. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  23046. NULL, NULL), BAD_FUNC_ARG);
  23047. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  23048. NULL, NULL), BAD_FUNC_ARG);
  23049. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  23050. &verified, NULL), BAD_FUNC_ARG);
  23051. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), NULL, sizeof(hash),
  23052. NULL, key), BAD_FUNC_ARG);
  23053. ExpectIntEQ(wc_ecc_sm2_verify_hash(NULL, sizeof(sig), hash, sizeof(hash),
  23054. &verified, key), BAD_FUNC_ARG);
  23055. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), NULL, sizeof(hash),
  23056. &verified, key), BAD_FUNC_ARG);
  23057. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23058. NULL, key), BAD_FUNC_ARG);
  23059. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23060. &verified, NULL), BAD_FUNC_ARG);
  23061. /* Make key not on the SM2 curve. */
  23062. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23063. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23064. &verified, key), BAD_FUNC_ARG);
  23065. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23066. /* Test valid parameters. */
  23067. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sizeof(sig), hash, sizeof(hash),
  23068. &verified, key), 0);
  23069. ExpectIntEQ(verified, 1);
  23070. ExpectIntEQ(wc_ecc_sm2_verify_hash(sigBad, sizeof(sigBad), hash,
  23071. sizeof(hash), &verified, key), 0);
  23072. ExpectIntEQ(verified, 0);
  23073. wc_ecc_free(key);
  23074. #ifdef FP_ECC
  23075. wc_ecc_fp_free();
  23076. #endif
  23077. res = EXPECT_RESULT();
  23078. #endif
  23079. return res;
  23080. }
  23081. /*
  23082. * Testing wc_ecc_sm2_verify_hash_ex()
  23083. */
  23084. static int test_wc_ecc_sm2_sign_hash_ex(void)
  23085. {
  23086. int res = TEST_SKIPPED;
  23087. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN) && \
  23088. defined(WOLFSSL_PUBLIC_MP)
  23089. EXPECT_DECLS;
  23090. WC_RNG rng[1];
  23091. ecc_key key[1];
  23092. mp_int r[1];
  23093. mp_int s[1];
  23094. unsigned char hash[32];
  23095. #ifdef HAVE_ECC_VERIFY
  23096. int verified;
  23097. #endif
  23098. XMEMSET(rng, 0, sizeof(*rng));
  23099. XMEMSET(key, 0, sizeof(*key));
  23100. XMEMSET(r, 0, sizeof(*r));
  23101. XMEMSET(s, 0, sizeof(*s));
  23102. ExpectIntEQ(wc_InitRng(rng), 0);
  23103. ExpectIntEQ(mp_init(r), 0);
  23104. ExpectIntEQ(mp_init(s), 0);
  23105. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  23106. ExpectIntEQ(wc_ecc_init(key), 0);
  23107. /* Test with no curve set. */
  23108. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23109. BAD_FUNC_ARG);
  23110. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  23111. /* Test invalid parameters. */
  23112. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  23113. NULL), BAD_FUNC_ARG);
  23114. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, NULL, NULL,
  23115. NULL), BAD_FUNC_ARG);
  23116. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, NULL, NULL,
  23117. NULL), BAD_FUNC_ARG);
  23118. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, key, NULL,
  23119. NULL), BAD_FUNC_ARG);
  23120. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, r,
  23121. NULL), BAD_FUNC_ARG);
  23122. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), NULL, NULL, NULL,
  23123. s), BAD_FUNC_ARG);
  23124. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(NULL, sizeof(hash), rng, key, r, s),
  23125. BAD_FUNC_ARG);
  23126. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), NULL, key, r, s),
  23127. BAD_FUNC_ARG);
  23128. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, NULL, r, s),
  23129. BAD_FUNC_ARG);
  23130. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, NULL, s),
  23131. BAD_FUNC_ARG);
  23132. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, NULL),
  23133. BAD_FUNC_ARG);
  23134. /* Make key not on the SM2 curve. */
  23135. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23136. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23137. BAD_FUNC_ARG);
  23138. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23139. #ifdef WOLFSSL_SP_MATH_ALL
  23140. {
  23141. mp_int smallR[1];
  23142. sp_init_size(smallR, 1);
  23143. /* Force failure in _ecc_sm2_calc_r_s by r being too small. */
  23144. ExpectIntLT(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key,
  23145. smallR, s), 0);
  23146. }
  23147. #endif
  23148. /* Test valid parameters. */
  23149. ExpectIntEQ(wc_ecc_sm2_sign_hash_ex(hash, sizeof(hash), rng, key, r, s),
  23150. 0);
  23151. #ifdef HAVE_ECC_VERIFY
  23152. ExpectIntEQ(wc_ecc_sm2_verify_hash_ex(r, s, hash, sizeof(hash), &verified,
  23153. key), 0);
  23154. ExpectIntEQ(verified, 1);
  23155. #endif
  23156. mp_free(s);
  23157. mp_free(r);
  23158. wc_ecc_free(key);
  23159. wc_FreeRng(rng);
  23160. #ifdef FP_ECC
  23161. wc_ecc_fp_free();
  23162. #endif
  23163. res = EXPECT_RESULT();
  23164. #endif
  23165. return res;
  23166. }
  23167. /*
  23168. * Testing wc_ecc_sm2_verify_hash()
  23169. */
  23170. static int test_wc_ecc_sm2_sign_hash(void)
  23171. {
  23172. int res = TEST_SKIPPED;
  23173. #if defined(HAVE_ECC) && defined(WOLFSSL_SM2) && defined(HAVE_ECC_SIGN)
  23174. EXPECT_DECLS;
  23175. WC_RNG rng[1];
  23176. ecc_key key[1];
  23177. unsigned char hash[32];
  23178. unsigned char sig[72];
  23179. word32 sigSz = sizeof(sig);
  23180. #ifdef HAVE_ECC_VERIFY
  23181. int verified;
  23182. #endif
  23183. XMEMSET(rng, 0, sizeof(*rng));
  23184. XMEMSET(key, 0, sizeof(*key));
  23185. ExpectIntEQ(wc_InitRng(rng), 0);
  23186. ExpectIntEQ(wc_RNG_GenerateBlock(rng, hash, sizeof(hash)), 0);
  23187. ExpectIntEQ(wc_ecc_init(key), 0);
  23188. /* Test with no curve set. */
  23189. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23190. BAD_FUNC_ARG);
  23191. ExpectIntEQ(wc_ecc_sm2_make_key(rng, key, WC_ECC_FLAG_NONE), 0);
  23192. /* Test invalid parameters. */
  23193. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  23194. NULL), BAD_FUNC_ARG);
  23195. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, NULL, NULL,
  23196. NULL), BAD_FUNC_ARG);
  23197. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, NULL, NULL,
  23198. NULL), BAD_FUNC_ARG);
  23199. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, &sigSz, NULL,
  23200. NULL), BAD_FUNC_ARG);
  23201. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, rng,
  23202. NULL), BAD_FUNC_ARG);
  23203. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), NULL, NULL, NULL,
  23204. key), BAD_FUNC_ARG);
  23205. ExpectIntEQ(wc_ecc_sm2_sign_hash(NULL, sizeof(hash), sig, &sigSz, rng,
  23206. key), BAD_FUNC_ARG);
  23207. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), NULL, &sigSz, rng,
  23208. key), BAD_FUNC_ARG);
  23209. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, NULL, rng,
  23210. key), BAD_FUNC_ARG);
  23211. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, NULL,
  23212. key), BAD_FUNC_ARG);
  23213. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng,
  23214. NULL), BAD_FUNC_ARG);
  23215. /* Make key not on the SM2 curve. */
  23216. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SECP256R1), 0);
  23217. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23218. BAD_FUNC_ARG);
  23219. ExpectIntEQ(wc_ecc_set_curve(key, 32, ECC_SM2P256V1), 0);
  23220. /* Test valid parameters. */
  23221. ExpectIntEQ(wc_ecc_sm2_sign_hash(hash, sizeof(hash), sig, &sigSz, rng, key),
  23222. 0);
  23223. #ifdef HAVE_ECC_VERIFY
  23224. ExpectIntEQ(wc_ecc_sm2_verify_hash(sig, sigSz, hash, sizeof(hash),
  23225. &verified, key), 0);
  23226. ExpectIntEQ(verified, 1);
  23227. #endif
  23228. wc_ecc_free(key);
  23229. wc_FreeRng(rng);
  23230. #ifdef FP_ECC
  23231. wc_ecc_fp_free();
  23232. #endif
  23233. res = EXPECT_RESULT();
  23234. #endif
  23235. return res;
  23236. }
  23237. /*
  23238. * Testing ToTraditional
  23239. */
  23240. static int test_ToTraditional(void)
  23241. {
  23242. EXPECT_DECLS;
  23243. #if !defined(NO_ASN) && (defined(HAVE_PKCS8) || defined(HAVE_PKCS12)) && \
  23244. (defined(WOLFSSL_TEST_CERT) || defined(OPENSSL_EXTRA) || \
  23245. defined(OPENSSL_EXTRA_X509_SMALL)) && !defined(NO_FILESYSTEM)
  23246. XFILE f = XBADFILE;
  23247. byte input[TWOK_BUF];
  23248. word32 sz = 0;
  23249. ExpectTrue((f = XFOPEN("./certs/server-keyPkcs8.der", "rb")) != XBADFILE);
  23250. ExpectTrue((sz = (word32)XFREAD(input, 1, sizeof(input), f)) > 0);
  23251. if (f != XBADFILE)
  23252. XFCLOSE(f);
  23253. /* Good case */
  23254. ExpectIntGT(ToTraditional(input, sz), 0);
  23255. /* Bad cases */
  23256. ExpectIntEQ(ToTraditional(NULL, 0), BAD_FUNC_ARG);
  23257. ExpectIntEQ(ToTraditional(NULL, sz), BAD_FUNC_ARG);
  23258. #ifdef WOLFSSL_ASN_TEMPLATE
  23259. ExpectIntEQ(ToTraditional(input, 0), BUFFER_E);
  23260. #else
  23261. ExpectIntEQ(ToTraditional(input, 0), ASN_PARSE_E);
  23262. #endif
  23263. #endif
  23264. return EXPECT_RESULT();
  23265. } /* End test_ToTraditional*/
  23266. /*
  23267. * Testing wc_EccPrivateKeyToDer
  23268. */
  23269. static int test_wc_EccPrivateKeyToDer(void)
  23270. {
  23271. EXPECT_DECLS;
  23272. #if defined(HAVE_ECC) && defined(HAVE_ECC_KEY_EXPORT) && !defined(WC_NO_RNG)
  23273. byte output[ONEK_BUF];
  23274. ecc_key eccKey;
  23275. WC_RNG rng;
  23276. word32 inLen;
  23277. word32 outLen = 0;
  23278. int ret;
  23279. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23280. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23281. ExpectIntEQ(wc_InitRng(&rng), 0);
  23282. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23283. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23284. #if defined(WOLFSSL_ASYNC_CRYPT)
  23285. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23286. #endif
  23287. ExpectIntEQ(ret, 0);
  23288. inLen = (word32)sizeof(output);
  23289. /* Bad Cases */
  23290. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23291. ExpectIntEQ(wc_EccPrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23292. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, NULL, inLen), LENGTH_ONLY_E);
  23293. ExpectIntEQ(wc_EccPrivateKeyToDer(&eccKey, output, 0), BAD_FUNC_ARG);
  23294. /* Good Case */
  23295. ExpectIntGT(outLen = (word32)wc_EccPrivateKeyToDer(&eccKey, output, inLen), 0);
  23296. wc_ecc_free(&eccKey);
  23297. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23298. #if defined(OPENSSL_EXTRA) && defined(HAVE_ALL_CURVES)
  23299. {
  23300. /* test importing private only into a PKEY struct */
  23301. EC_KEY* ec = NULL;
  23302. EVP_PKEY* pkey = NULL;
  23303. const unsigned char* der;
  23304. der = output;
  23305. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &der, outLen));
  23306. der = output;
  23307. ExpectNotNull(ec = d2i_ECPrivateKey(NULL, &der, outLen));
  23308. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ec), SSL_SUCCESS);
  23309. if (EXPECT_FAIL()) {
  23310. EC_KEY_free(ec);
  23311. }
  23312. EVP_PKEY_free(pkey); /* EC_KEY should be free'd by free'ing pkey */
  23313. }
  23314. #endif
  23315. #endif
  23316. return EXPECT_RESULT();
  23317. } /* End test_wc_EccPrivateKeyToDer*/
  23318. /*
  23319. * Testing wc_DhPublicKeyDecode
  23320. */
  23321. static int test_wc_DhPublicKeyDecode(void)
  23322. {
  23323. EXPECT_DECLS;
  23324. #ifndef NO_DH
  23325. #if defined(WOLFSSL_DH_EXTRA) && defined(USE_CERT_BUFFERS_2048)
  23326. DhKey key;
  23327. word32 inOutIdx;
  23328. XMEMSET(&key, 0, sizeof(DhKey));
  23329. ExpectIntEQ(wc_InitDhKey(&key), 0);
  23330. ExpectIntEQ(wc_DhPublicKeyDecode(NULL,NULL,NULL,0), BAD_FUNC_ARG);
  23331. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23332. BAD_FUNC_ARG);
  23333. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,NULL,NULL,0),
  23334. BAD_FUNC_ARG);
  23335. inOutIdx = 0;
  23336. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,NULL, 0),
  23337. BAD_FUNC_ARG);
  23338. inOutIdx = 0;
  23339. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key, 0),
  23340. BAD_FUNC_ARG);
  23341. inOutIdx = 0;
  23342. ExpectIntEQ(wc_DhPublicKeyDecode(dh_pub_key_der_2048,&inOutIdx,&key,
  23343. sizeof_dh_pub_key_der_2048), 0);
  23344. ExpectIntNE(key.p.used, 0);
  23345. ExpectIntNE(key.g.used, 0);
  23346. ExpectIntEQ(key.q.used, 0);
  23347. ExpectIntNE(key.pub.used, 0);
  23348. ExpectIntEQ(key.priv.used, 0);
  23349. DoExpectIntEQ(wc_FreeDhKey(&key), 0);
  23350. #endif
  23351. #endif /* !NO_DH */
  23352. return EXPECT_RESULT();
  23353. }
  23354. /*
  23355. * Testing wc_Ed25519KeyToDer
  23356. */
  23357. static int test_wc_Ed25519KeyToDer(void)
  23358. {
  23359. EXPECT_DECLS;
  23360. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23361. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23362. byte output[ONEK_BUF];
  23363. ed25519_key ed25519Key;
  23364. WC_RNG rng;
  23365. word32 inLen;
  23366. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23367. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23368. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23369. ExpectIntEQ(wc_InitRng(&rng), 0);
  23370. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23371. inLen = (word32)sizeof(output);
  23372. /* Bad Cases */
  23373. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23374. ExpectIntEQ(wc_Ed25519KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23375. ExpectIntEQ(wc_Ed25519KeyToDer(&ed25519Key, output, 0), BAD_FUNC_ARG);
  23376. /* Good Cases */
  23377. /* length only */
  23378. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, NULL, inLen), 0);
  23379. ExpectIntGT(wc_Ed25519KeyToDer(&ed25519Key, output, inLen), 0);
  23380. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23381. wc_ed25519_free(&ed25519Key);
  23382. #endif
  23383. return EXPECT_RESULT();
  23384. } /* End test_wc_Ed25519KeyToDer*/
  23385. /*
  23386. * Testing wc_Ed25519PrivateKeyToDer
  23387. */
  23388. static int test_wc_Ed25519PrivateKeyToDer(void)
  23389. {
  23390. EXPECT_DECLS;
  23391. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT) && \
  23392. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23393. byte output[ONEK_BUF];
  23394. ed25519_key ed25519PrivKey;
  23395. WC_RNG rng;
  23396. word32 inLen;
  23397. XMEMSET(&ed25519PrivKey, 0, sizeof(ed25519_key));
  23398. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23399. ExpectIntEQ(wc_ed25519_init(&ed25519PrivKey), 0);
  23400. ExpectIntEQ(wc_InitRng(&rng), 0);
  23401. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519PrivKey),
  23402. 0);
  23403. inLen = (word32)sizeof(output);
  23404. /* Bad Cases */
  23405. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23406. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23407. ExpectIntEQ(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, 0),
  23408. BAD_FUNC_ARG);
  23409. /* Good Cases */
  23410. /* length only */
  23411. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, NULL, inLen), 0);
  23412. ExpectIntGT(wc_Ed25519PrivateKeyToDer(&ed25519PrivKey, output, inLen), 0);
  23413. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23414. wc_ed25519_free(&ed25519PrivKey);
  23415. #endif
  23416. return EXPECT_RESULT();
  23417. } /* End test_wc_Ed25519PrivateKeyToDer*/
  23418. /*
  23419. * Testing wc_Ed448KeyToDer
  23420. */
  23421. static int test_wc_Ed448KeyToDer(void)
  23422. {
  23423. EXPECT_DECLS;
  23424. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23425. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23426. byte output[ONEK_BUF];
  23427. ed448_key ed448Key;
  23428. WC_RNG rng;
  23429. word32 inLen;
  23430. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23431. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23432. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23433. ExpectIntEQ(wc_InitRng(&rng), 0);
  23434. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23435. inLen = (word32)sizeof(output);
  23436. /* Bad Cases */
  23437. ExpectIntEQ(wc_Ed448KeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23438. ExpectIntEQ(wc_Ed448KeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23439. ExpectIntEQ(wc_Ed448KeyToDer(&ed448Key, output, 0), BAD_FUNC_ARG);
  23440. /* Good Cases */
  23441. /* length only */
  23442. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, NULL, inLen), 0);
  23443. ExpectIntGT(wc_Ed448KeyToDer(&ed448Key, output, inLen), 0);
  23444. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23445. wc_ed448_free(&ed448Key);
  23446. #endif
  23447. return EXPECT_RESULT();
  23448. } /* End test_wc_Ed448KeyToDer*/
  23449. /*
  23450. * Testing wc_Ed448PrivateKeyToDer
  23451. */
  23452. static int test_wc_Ed448PrivateKeyToDer(void)
  23453. {
  23454. EXPECT_DECLS;
  23455. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT) && \
  23456. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23457. byte output[ONEK_BUF];
  23458. ed448_key ed448PrivKey;
  23459. WC_RNG rng;
  23460. word32 inLen;
  23461. XMEMSET(&ed448PrivKey, 0, sizeof(ed448_key));
  23462. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23463. ExpectIntEQ(wc_ed448_init(&ed448PrivKey), 0);
  23464. ExpectIntEQ(wc_InitRng(&rng), 0);
  23465. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448PrivKey),
  23466. 0);
  23467. inLen = (word32)sizeof(output);
  23468. /* Bad Cases */
  23469. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23470. ExpectIntEQ(wc_Ed448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23471. ExpectIntEQ(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, 0),
  23472. BAD_FUNC_ARG);
  23473. /* Good cases */
  23474. /* length only */
  23475. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, NULL, inLen), 0);
  23476. ExpectIntGT(wc_Ed448PrivateKeyToDer(&ed448PrivKey, output, inLen), 0);
  23477. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23478. wc_ed448_free(&ed448PrivKey);
  23479. #endif
  23480. return EXPECT_RESULT();
  23481. } /* End test_wc_Ed448PrivateKeyToDer*/
  23482. /*
  23483. * Testing wc_Curve448PrivateKeyToDer
  23484. */
  23485. static int test_wc_Curve448PrivateKeyToDer(void)
  23486. {
  23487. EXPECT_DECLS;
  23488. #if defined(HAVE_CURVE448) && defined(HAVE_CURVE448_KEY_EXPORT) && \
  23489. (defined(WOLFSSL_CERT_GEN) || defined(WOLFSSL_KEY_GEN))
  23490. byte output[ONEK_BUF];
  23491. curve448_key curve448PrivKey;
  23492. WC_RNG rng;
  23493. word32 inLen;
  23494. XMEMSET(&curve448PrivKey, 0, sizeof(curve448PrivKey));
  23495. XMEMSET(&rng, 0, sizeof(WC_RNG));
  23496. ExpectIntEQ(wc_curve448_init(&curve448PrivKey), 0);
  23497. ExpectIntEQ(wc_InitRng(&rng), 0);
  23498. ExpectIntEQ(wc_curve448_make_key(&rng, CURVE448_KEY_SIZE, &curve448PrivKey),
  23499. 0);
  23500. inLen = (word32)sizeof(output);
  23501. /* Bad Cases */
  23502. ExpectIntEQ(wc_Curve448PrivateKeyToDer(NULL, NULL, 0), BAD_FUNC_ARG);
  23503. ExpectIntEQ(wc_Curve448PrivateKeyToDer(NULL, output, inLen), BAD_FUNC_ARG);
  23504. ExpectIntEQ(wc_Curve448PrivateKeyToDer(&curve448PrivKey, output, 0),
  23505. BAD_FUNC_ARG);
  23506. /* Good cases */
  23507. /* length only */
  23508. ExpectIntGT(wc_Curve448PrivateKeyToDer(&curve448PrivKey, NULL, inLen), 0);
  23509. ExpectIntGT(wc_Curve448PrivateKeyToDer(&curve448PrivKey, output, inLen), 0);
  23510. /* Bad Cases */
  23511. ExpectIntEQ(wc_Curve448PublicKeyToDer(NULL, NULL, 0, 0), BAD_FUNC_ARG);
  23512. ExpectIntEQ(wc_Curve448PublicKeyToDer(NULL, output, inLen, 0), BAD_FUNC_ARG);
  23513. ExpectIntEQ(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, 0, 0),
  23514. BUFFER_E);
  23515. ExpectIntEQ(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, 0, 1),
  23516. BUFFER_E);
  23517. /* Good cases */
  23518. /* length only */
  23519. ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, NULL, inLen, 0), 0);
  23520. ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, NULL, inLen, 1), 0);
  23521. ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, inLen, 0), 0);
  23522. ExpectIntGT(wc_Curve448PublicKeyToDer(&curve448PrivKey, output, inLen, 1), 0);
  23523. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23524. wc_curve448_free(&curve448PrivKey);
  23525. #endif
  23526. return EXPECT_RESULT();
  23527. } /* End wc_Curve448PrivateKeyToDer*/
  23528. /*
  23529. * Testing wc_SetSubjectBuffer
  23530. */
  23531. static int test_wc_SetSubjectBuffer(void)
  23532. {
  23533. EXPECT_DECLS;
  23534. #if defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  23535. Cert cert;
  23536. XFILE file = XBADFILE;
  23537. byte* der = NULL;
  23538. word32 derSz;
  23539. derSz = FOURK_BUF;
  23540. ExpectNotNull(der = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  23541. DYNAMIC_TYPE_TMP_BUFFER));
  23542. ExpectTrue((file = XFOPEN("./certs/ca-cert.der", "rb")) != XBADFILE);
  23543. ExpectTrue((derSz = (word32)XFREAD(der, 1, FOURK_BUF, file)) > 0);
  23544. if (file != XBADFILE)
  23545. XFCLOSE(file);
  23546. ExpectIntEQ(wc_InitCert(&cert), 0);
  23547. ExpectIntEQ(wc_SetSubjectBuffer(&cert, der, (int)derSz), 0);
  23548. ExpectIntEQ(wc_SetSubjectBuffer(NULL, der, (int)derSz), BAD_FUNC_ARG);
  23549. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  23550. #endif
  23551. return EXPECT_RESULT();
  23552. } /* End test_wc_SetSubjectBuffer*/
  23553. /*
  23554. * Testing wc_SetSubjectKeyIdFromPublicKey_ex
  23555. */
  23556. static int test_wc_SetSubjectKeyIdFromPublicKey_ex(void)
  23557. {
  23558. EXPECT_DECLS;
  23559. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23560. WC_RNG rng;
  23561. Cert cert;
  23562. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23563. RsaKey rsaKey;
  23564. int bits = 2048;
  23565. #endif
  23566. #if defined(HAVE_ECC)
  23567. ecc_key eccKey;
  23568. int ret;
  23569. #endif
  23570. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23571. ed25519_key ed25519Key;
  23572. #endif
  23573. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23574. ed448_key ed448Key;
  23575. #endif
  23576. #ifndef HAVE_FIPS
  23577. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  23578. #else
  23579. ExpectIntEQ(wc_InitRng(&rng), 0);
  23580. #endif
  23581. ExpectIntEQ(wc_InitCert(&cert), 0);
  23582. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23583. /* RSA */
  23584. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  23585. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  23586. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  23587. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey),
  23588. 0);
  23589. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  23590. #endif
  23591. #if defined(HAVE_ECC)
  23592. /* ECC */
  23593. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23594. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23595. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23596. #if defined(WOLFSSL_ASYNC_CRYPT)
  23597. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23598. #endif
  23599. ExpectIntEQ(ret, 0);
  23600. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey),
  23601. 0);
  23602. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  23603. #endif
  23604. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23605. /* ED25519 */
  23606. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23607. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23608. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23609. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23610. &ed25519Key), 0);
  23611. wc_ed25519_free(&ed25519Key);
  23612. #endif
  23613. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23614. /* ED448 */
  23615. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23616. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23617. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23618. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey_ex(&cert, ED448_TYPE,
  23619. &ed448Key), 0);
  23620. wc_ed448_free(&ed448Key);
  23621. #endif
  23622. wc_FreeRng(&rng);
  23623. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23624. #endif /* WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  23625. return EXPECT_RESULT();
  23626. } /* End test_wc_SetSubjectKeyIdFromPublicKey_ex*/
  23627. /*
  23628. * Testing wc_SetAuthKeyIdFromPublicKey_ex
  23629. */
  23630. static int test_wc_SetAuthKeyIdFromPublicKey_ex(void)
  23631. {
  23632. EXPECT_DECLS;
  23633. #if defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  23634. WC_RNG rng;
  23635. Cert cert;
  23636. #if !defined(NO_RSA) && defined(HAVE_RSA)
  23637. RsaKey rsaKey;
  23638. int bits = 2048;
  23639. #endif
  23640. #if defined(HAVE_ECC)
  23641. ecc_key eccKey;
  23642. int ret;
  23643. #endif
  23644. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23645. ed25519_key ed25519Key;
  23646. #endif
  23647. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23648. ed448_key ed448Key;
  23649. #endif
  23650. #ifndef HAVE_FIPS
  23651. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  23652. #else
  23653. ExpectIntEQ(wc_InitRng(&rng), 0);
  23654. #endif
  23655. ExpectIntEQ(wc_InitCert(&cert), 0);
  23656. #if !defined(NO_RSA) && defined(HAVE_RSA) && defined(WOLFSSL_KEY_GEN)
  23657. /* RSA */
  23658. XMEMSET(&rsaKey, 0, sizeof(RsaKey));
  23659. ExpectIntEQ(wc_InitRsaKey(&rsaKey, HEAP_HINT), 0);
  23660. ExpectIntEQ(MAKE_RSA_KEY(&rsaKey, bits, WC_RSA_EXPONENT, &rng), 0);
  23661. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, RSA_TYPE, &rsaKey), 0);
  23662. DoExpectIntEQ(wc_FreeRsaKey(&rsaKey), 0);
  23663. #endif
  23664. #if defined(HAVE_ECC)
  23665. /* ECC */
  23666. XMEMSET(&eccKey, 0, sizeof(ecc_key));
  23667. ExpectIntEQ(wc_ecc_init(&eccKey), 0);
  23668. ret = wc_ecc_make_key(&rng, KEY14, &eccKey);
  23669. #if defined(WOLFSSL_ASYNC_CRYPT)
  23670. ret = wc_AsyncWait(ret, &eccKey.asyncDev, WC_ASYNC_FLAG_NONE);
  23671. #endif
  23672. ExpectIntEQ(ret, 0);
  23673. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ECC_TYPE, &eccKey), 0);
  23674. DoExpectIntEQ(wc_ecc_free(&eccKey), 0);
  23675. #endif
  23676. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_EXPORT)
  23677. /* ED25519 */
  23678. XMEMSET(&ed25519Key, 0, sizeof(ed25519_key));
  23679. ExpectIntEQ(wc_ed25519_init(&ed25519Key), 0);
  23680. ExpectIntEQ(wc_ed25519_make_key(&rng, ED25519_KEY_SIZE, &ed25519Key), 0);
  23681. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED25519_TYPE,
  23682. &ed25519Key), 0);
  23683. wc_ed25519_free(&ed25519Key);
  23684. #endif
  23685. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_EXPORT)
  23686. /* ED448 */
  23687. XMEMSET(&ed448Key, 0, sizeof(ed448_key));
  23688. ExpectIntEQ(wc_ed448_init(&ed448Key), 0);
  23689. ExpectIntEQ(wc_ed448_make_key(&rng, ED448_KEY_SIZE, &ed448Key), 0);
  23690. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey_ex(&cert, ED448_TYPE, &ed448Key),
  23691. 0);
  23692. wc_ed448_free(&ed448Key);
  23693. #endif
  23694. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  23695. #endif /* defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)*/
  23696. return EXPECT_RESULT();
  23697. } /* End test_wc_SetAuthKeyIdFromPublicKey_ex*/
  23698. /*
  23699. * Testing wc_PKCS7_New()
  23700. */
  23701. static int test_wc_PKCS7_New(void)
  23702. {
  23703. EXPECT_DECLS;
  23704. #if defined(HAVE_PKCS7)
  23705. PKCS7* pkcs7 = NULL;
  23706. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, testDevId));
  23707. wc_PKCS7_Free(pkcs7);
  23708. #endif
  23709. return EXPECT_RESULT();
  23710. } /* END test-wc_PKCS7_New */
  23711. /*
  23712. * Testing wc_PKCS7_Init()
  23713. */
  23714. static int test_wc_PKCS7_Init(void)
  23715. {
  23716. EXPECT_DECLS;
  23717. #if defined(HAVE_PKCS7)
  23718. PKCS7* pkcs7 = NULL;
  23719. void* heap = NULL;
  23720. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  23721. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  23722. /* Pass in bad args. */
  23723. ExpectIntEQ(wc_PKCS7_Init(NULL, heap, testDevId), BAD_FUNC_ARG);
  23724. wc_PKCS7_Free(pkcs7);
  23725. #endif
  23726. return EXPECT_RESULT();
  23727. } /* END test-wc_PKCS7_Init */
  23728. /*
  23729. * Testing wc_PKCS7_InitWithCert()
  23730. */
  23731. static int test_wc_PKCS7_InitWithCert(void)
  23732. {
  23733. EXPECT_DECLS;
  23734. #if defined(HAVE_PKCS7)
  23735. PKCS7* pkcs7 = NULL;
  23736. #ifndef NO_RSA
  23737. #if defined(USE_CERT_BUFFERS_2048)
  23738. unsigned char cert[sizeof(client_cert_der_2048)];
  23739. int certSz = (int)sizeof(cert);
  23740. XMEMSET(cert, 0, certSz);
  23741. XMEMCPY(cert, client_cert_der_2048, sizeof(client_cert_der_2048));
  23742. #elif defined(USE_CERT_BUFFERS_1024)
  23743. unsigned char cert[sizeof(client_cert_der_1024)];
  23744. int certSz = (int)sizeof(cert);
  23745. XMEMSET(cert, 0, certSz);
  23746. XMEMCPY(cert, client_cert_der_1024, sizeof_client_cert_der_1024);
  23747. #else
  23748. unsigned char cert[ONEK_BUF];
  23749. XFILE fp = XBADFILE;
  23750. int certSz;
  23751. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23752. XBADFILE);
  23753. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23754. fp), 0);
  23755. if (fp != XBADFILE)
  23756. XFCLOSE(fp);
  23757. #endif
  23758. #elif defined(HAVE_ECC)
  23759. #if defined(USE_CERT_BUFFERS_256)
  23760. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23761. int certSz = (int)sizeof(cert);
  23762. XMEMSET(cert, 0, certSz);
  23763. XMEMCPY(cert, cliecc_cert_der_256, sizeof(cliecc_cert_der_256));
  23764. #else
  23765. unsigned char cert[ONEK_BUF];
  23766. XFILE fp = XBADFILE;
  23767. int certSz;
  23768. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23769. XBADFILE);
  23770. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof(cliecc_cert_der_256),
  23771. fp), 0);
  23772. if (fp != XBADFILE)
  23773. XFCLOSE(fp);
  23774. #endif
  23775. #else
  23776. #error PKCS7 requires ECC or RSA
  23777. #endif
  23778. #ifdef HAVE_ECC
  23779. {
  23780. /* bad test case from ZD 11011, malformed cert gives bad ECC key */
  23781. static unsigned char certWithInvalidEccKey[] = {
  23782. 0x30, 0x82, 0x03, 0x5F, 0x30, 0x82, 0x03, 0x04, 0xA0, 0x03, 0x02, 0x01,
  23783. 0x02, 0x02, 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79,
  23784. 0x42, 0x83, 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x31, 0xAA, 0x2C, 0x30,
  23785. 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02, 0x30,
  23786. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23787. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08,
  23788. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23789. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23790. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23791. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23792. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23793. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23794. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23795. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23796. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23797. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30,
  23798. 0x1E, 0x17, 0x0D, 0x32, 0x30, 0x30, 0x36, 0x31, 0x39, 0x31, 0x33, 0x32,
  23799. 0x33, 0x34, 0x31, 0x5A, 0x17, 0x0D, 0x32, 0x33, 0x30, 0x33, 0x31, 0x36,
  23800. 0x31, 0x33, 0x32, 0x33, 0x34, 0x31, 0x5A, 0x30, 0x81, 0x8D, 0x31, 0x0B,
  23801. 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31,
  23802. 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x06, 0x4F, 0x72,
  23803. 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03, 0x55, 0x04,
  23804. 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D, 0x31, 0x13, 0x30, 0x11,
  23805. 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43, 0x6C, 0x69, 0x65, 0x6E,
  23806. 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30, 0x0B, 0x06, 0x03, 0x55,
  23807. 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74, 0x31, 0x18, 0x30, 0x26,
  23808. 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  23809. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F,
  23810. 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  23811. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66,
  23812. 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x59, 0x30, 0x13, 0x06,
  23813. 0x07, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86,
  23814. 0x48, 0xCE, 0x3D, 0x03, 0x01, 0x07, 0x03, 0x02, 0x00, 0x04, 0x55, 0xBF,
  23815. 0xF4, 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5, 0x4D,
  23816. 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80, 0xEC, 0x5A, 0x4C,
  23817. 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA, 0xEF, 0xA2, 0x35, 0x12, 0x43,
  23818. 0x84, 0x76, 0x16, 0xC6, 0x56, 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6,
  23819. 0x75, 0x1A, 0x42, 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D,
  23820. 0x7F, 0xB4, 0xA3, 0x82, 0x01, 0x3E, 0x30, 0x82, 0x01, 0x3A, 0x30, 0x1D,
  23821. 0x06, 0x03, 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0xEB, 0xD4, 0x4B,
  23822. 0x59, 0x6B, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23823. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0x30, 0x81, 0xCD, 0x06, 0x03, 0x55, 0x1D,
  23824. 0x23, 0x04, 0x81, 0xC5, 0x30, 0x81, 0xC2, 0x80, 0x14, 0xEB, 0xD4, 0x4B,
  23825. 0x59, 0x72, 0x95, 0x61, 0x3F, 0x51, 0x57, 0xB6, 0x04, 0x4D, 0x89, 0x41,
  23826. 0x88, 0x44, 0x5C, 0xAB, 0xF2, 0xA1, 0x81, 0x93, 0xA4, 0x81, 0x90, 0x30,
  23827. 0x81, 0x8D, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13,
  23828. 0x02, 0x55, 0x53, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x03, 0x55, 0x08, 0x08,
  23829. 0x0C, 0x06, 0x4F, 0x72, 0x65, 0x67, 0x6F, 0x6E, 0x31, 0x0E, 0x30, 0x0C,
  23830. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x05, 0x53, 0x61, 0x6C, 0x65, 0x6D,
  23831. 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0A, 0x43,
  23832. 0x6C, 0x69, 0x65, 0x6E, 0x74, 0x20, 0x45, 0x43, 0x43, 0x31, 0x0D, 0x30,
  23833. 0x0B, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x04, 0x46, 0x61, 0x73, 0x74,
  23834. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77,
  23835. 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  23836. 0x6F, 0x6D, 0x30, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  23837. 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  23838. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x82,
  23839. 0x14, 0x61, 0xB3, 0x1E, 0x59, 0xF3, 0x68, 0x6C, 0xA4, 0x79, 0x42, 0x83,
  23840. 0x2F, 0x1A, 0x50, 0x71, 0x03, 0xBE, 0x32, 0xAA, 0x2C, 0x30, 0x0C, 0x06,
  23841. 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01, 0x01, 0xFF, 0x30,
  23842. 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30, 0x13, 0x82, 0x0B,
  23843. 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63, 0x6F, 0x6D, 0x87,
  23844. 0x04, 0x23, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25,
  23845. 0x04, 0x16, 0x30, 0x14, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07,
  23846. 0x03, 0x01, 0x06, 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02,
  23847. 0x30, 0x0A, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D, 0x04, 0x03, 0x02,
  23848. 0x03, 0x49, 0x00, 0x30, 0x46, 0x02, 0x21, 0x00, 0xE4, 0xA0, 0x23, 0x26,
  23849. 0x2B, 0x0B, 0x42, 0x0F, 0x97, 0x37, 0x6D, 0xCB, 0x14, 0x23, 0xC3, 0xC3,
  23850. 0xE6, 0x44, 0xCF, 0x5F, 0x4C, 0x26, 0xA3, 0x72, 0x64, 0x7A, 0x9C, 0xCB,
  23851. 0x64, 0xAB, 0xA6, 0xBE, 0x02, 0x21, 0x00, 0xAA, 0xC5, 0xA3, 0x50, 0xF6,
  23852. 0xF1, 0xA5, 0xDB, 0x05, 0xE0, 0x75, 0xD2, 0xF7, 0xBA, 0x49, 0x5F, 0x8F,
  23853. 0x7D, 0x1C, 0x44, 0xB1, 0x6E, 0xDF, 0xC8, 0xDA, 0x10, 0x48, 0x2D, 0x53,
  23854. 0x08, 0xA8, 0xB4
  23855. };
  23856. #endif
  23857. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23858. /* If initialization is not successful, it's free'd in init func. */
  23859. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz),
  23860. 0);
  23861. wc_PKCS7_Free(pkcs7);
  23862. pkcs7 = NULL;
  23863. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23864. /* Valid initialization usage. */
  23865. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  23866. /* Pass in bad args. No need free for null checks, free at end.*/
  23867. ExpectIntEQ(wc_PKCS7_InitWithCert(NULL, (byte*)cert, (word32)certSz),
  23868. BAD_FUNC_ARG);
  23869. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, (word32)certSz),
  23870. BAD_FUNC_ARG);
  23871. #ifdef HAVE_ECC
  23872. ExpectIntLT(wc_PKCS7_InitWithCert(pkcs7, certWithInvalidEccKey,
  23873. sizeof(certWithInvalidEccKey)), 0);
  23874. }
  23875. #endif
  23876. wc_PKCS7_Free(pkcs7);
  23877. #endif
  23878. return EXPECT_RESULT();
  23879. } /* END test_wc_PKCS7_InitWithCert */
  23880. /*
  23881. * Testing wc_PKCS7_EncodeData()
  23882. */
  23883. static int test_wc_PKCS7_EncodeData(void)
  23884. {
  23885. EXPECT_DECLS;
  23886. #if defined(HAVE_PKCS7)
  23887. PKCS7* pkcs7 = NULL;
  23888. byte output[FOURK_BUF];
  23889. byte data[] = "My encoded DER cert.";
  23890. #ifndef NO_RSA
  23891. #if defined(USE_CERT_BUFFERS_2048)
  23892. unsigned char cert[sizeof(client_cert_der_2048)];
  23893. unsigned char key[sizeof(client_key_der_2048)];
  23894. int certSz = (int)sizeof(cert);
  23895. int keySz = (int)sizeof(key);
  23896. XMEMSET(cert, 0, certSz);
  23897. XMEMSET(key, 0, keySz);
  23898. XMEMCPY(cert, client_cert_der_2048, certSz);
  23899. XMEMCPY(key, client_key_der_2048, keySz);
  23900. #elif defined(USE_CERT_BUFFERS_1024)
  23901. unsigned char cert[sizeof(sizeof_client_cert_der_1024)];
  23902. unsigned char key[sizeof_client_key_der_1024];
  23903. int certSz = (int)sizeof(cert);
  23904. int keySz = (int)sizeof(key);
  23905. XMEMSET(cert, 0, certSz);
  23906. XMEMSET(key, 0, keySz);
  23907. XMEMCPY(cert, client_cert_der_1024, certSz);
  23908. XMEMCPY(key, client_key_der_1024, keySz);
  23909. #else
  23910. unsigned char cert[ONEK_BUF];
  23911. unsigned char key[ONEK_BUF];
  23912. XFILE fp = XBADFILE;
  23913. int certSz;
  23914. int keySz;
  23915. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  23916. XBADFILE);
  23917. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  23918. fp), 0);
  23919. if (fp != XBADFILE) {
  23920. XFCLOSE(fp);
  23921. fp = XBADFILE;
  23922. }
  23923. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  23924. XBADFILE);
  23925. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  23926. 0);
  23927. if (fp != XBADFILE)
  23928. XFCLOSE(fp);
  23929. #endif
  23930. #elif defined(HAVE_ECC)
  23931. #if defined(USE_CERT_BUFFERS_256)
  23932. unsigned char cert[sizeof(cliecc_cert_der_256)];
  23933. unsigned char key[sizeof(ecc_clikey_der_256)];
  23934. int certSz = (int)sizeof(cert);
  23935. int keySz = (int)sizeof(key);
  23936. XMEMSET(cert, 0, certSz);
  23937. XMEMSET(key, 0, keySz);
  23938. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  23939. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  23940. #else
  23941. unsigned char cert[ONEK_BUF];
  23942. unsigned char key[ONEK_BUF];
  23943. XFILE fp = XBADFILE;
  23944. int certSz, keySz;
  23945. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  23946. XBADFILE);
  23947. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  23948. fp), 0);
  23949. if (fp != XBADFILE) {
  23950. XFCLOSE(fp);
  23951. fp = XBADFILE;
  23952. }
  23953. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  23954. XBADFILE);
  23955. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  23956. 0);
  23957. if (fp != XBADFILE)
  23958. XFCLOSE(fp);
  23959. #endif
  23960. #endif
  23961. XMEMSET(output, 0, sizeof(output));
  23962. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  23963. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  23964. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  23965. if (pkcs7 != NULL) {
  23966. pkcs7->content = data;
  23967. pkcs7->contentSz = sizeof(data);
  23968. pkcs7->privateKey = key;
  23969. pkcs7->privateKeySz = (word32)keySz;
  23970. }
  23971. ExpectIntGT(wc_PKCS7_EncodeData(pkcs7, output, (word32)sizeof(output)), 0);
  23972. /* Test bad args. */
  23973. ExpectIntEQ(wc_PKCS7_EncodeData(NULL, output, (word32)sizeof(output)),
  23974. BAD_FUNC_ARG);
  23975. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, NULL, (word32)sizeof(output)),
  23976. BAD_FUNC_ARG);
  23977. ExpectIntEQ(wc_PKCS7_EncodeData(pkcs7, output, 5), BUFFER_E);
  23978. wc_PKCS7_Free(pkcs7);
  23979. #endif
  23980. return EXPECT_RESULT();
  23981. } /* END test_wc_PKCS7_EncodeData */
  23982. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  23983. !defined(NO_RSA) && !defined(NO_SHA256)
  23984. /* RSA sign raw digest callback */
  23985. static int rsaSignRawDigestCb(PKCS7* pkcs7, byte* digest, word32 digestSz,
  23986. byte* out, word32 outSz, byte* privateKey,
  23987. word32 privateKeySz, int devid, int hashOID)
  23988. {
  23989. /* specific DigestInfo ASN.1 encoding prefix for a SHA2565 digest */
  23990. byte digInfoEncoding[] = {
  23991. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  23992. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  23993. 0x00, 0x04, 0x20
  23994. };
  23995. int ret;
  23996. byte digestInfo[ONEK_BUF];
  23997. byte sig[FOURK_BUF];
  23998. word32 digestInfoSz = 0;
  23999. word32 idx = 0;
  24000. RsaKey rsa;
  24001. /* SHA-256 required only for this example callback due to above
  24002. * digInfoEncoding[] */
  24003. if (pkcs7 == NULL || digest == NULL || out == NULL ||
  24004. (sizeof(digestInfo) < sizeof(digInfoEncoding) + digestSz) ||
  24005. (hashOID != SHA256h)) {
  24006. return -1;
  24007. }
  24008. /* build DigestInfo */
  24009. XMEMCPY(digestInfo, digInfoEncoding, sizeof(digInfoEncoding));
  24010. digestInfoSz += sizeof(digInfoEncoding);
  24011. XMEMCPY(digestInfo + digestInfoSz, digest, digestSz);
  24012. digestInfoSz += digestSz;
  24013. /* set up RSA key */
  24014. ret = wc_InitRsaKey_ex(&rsa, pkcs7->heap, devid);
  24015. if (ret != 0) {
  24016. return ret;
  24017. }
  24018. ret = wc_RsaPrivateKeyDecode(privateKey, &idx, &rsa, privateKeySz);
  24019. /* sign DigestInfo */
  24020. if (ret == 0) {
  24021. ret = wc_RsaSSL_Sign(digestInfo, digestInfoSz, sig, sizeof(sig),
  24022. &rsa, pkcs7->rng);
  24023. if (ret > 0) {
  24024. if (ret > (int)outSz) {
  24025. /* output buffer too small */
  24026. ret = -1;
  24027. }
  24028. else {
  24029. /* success, ret holds sig size */
  24030. XMEMCPY(out, sig, ret);
  24031. }
  24032. }
  24033. }
  24034. wc_FreeRsaKey(&rsa);
  24035. return ret;
  24036. }
  24037. #endif
  24038. #if defined(HAVE_PKCS7) && defined(ASN_BER_TO_DER)
  24039. typedef struct encodeSignedDataStream {
  24040. byte out[FOURK_BUF*3];
  24041. int idx;
  24042. word32 outIdx;
  24043. } encodeSignedDataStream;
  24044. /* content is 8k of partially created bundle */
  24045. static int GetContentCB(PKCS7* pkcs7, byte** content, void* ctx)
  24046. {
  24047. int ret = 0;
  24048. encodeSignedDataStream* strm = (encodeSignedDataStream*)ctx;
  24049. if (strm->outIdx < pkcs7->contentSz) {
  24050. ret = (pkcs7->contentSz > strm->outIdx + FOURK_BUF)?
  24051. FOURK_BUF : pkcs7->contentSz - strm->outIdx;
  24052. *content = strm->out + strm->outIdx;
  24053. strm->outIdx += ret;
  24054. }
  24055. (void)pkcs7;
  24056. return ret;
  24057. }
  24058. static int StreamOutputCB(PKCS7* pkcs7, const byte* output, word32 outputSz,
  24059. void* ctx)
  24060. {
  24061. encodeSignedDataStream* strm = (encodeSignedDataStream*)ctx;
  24062. XMEMCPY(strm->out + strm->idx, output, outputSz);
  24063. strm->idx += outputSz;
  24064. (void)pkcs7;
  24065. return 0;
  24066. }
  24067. #endif
  24068. /*
  24069. * Testing wc_PKCS7_EncodeSignedData()
  24070. */
  24071. static int test_wc_PKCS7_EncodeSignedData(void)
  24072. {
  24073. EXPECT_DECLS;
  24074. #if defined(HAVE_PKCS7)
  24075. PKCS7* pkcs7 = NULL;
  24076. WC_RNG rng;
  24077. byte output[FOURK_BUF];
  24078. byte badOut[1];
  24079. word32 outputSz = (word32)sizeof(output);
  24080. word32 badOutSz = 0;
  24081. byte data[] = "Test data to encode.";
  24082. #ifndef NO_RSA
  24083. #if defined(USE_CERT_BUFFERS_2048)
  24084. byte key[sizeof(client_key_der_2048)];
  24085. byte cert[sizeof(client_cert_der_2048)];
  24086. word32 keySz = (word32)sizeof(key);
  24087. word32 certSz = (word32)sizeof(cert);
  24088. XMEMSET(key, 0, keySz);
  24089. XMEMSET(cert, 0, certSz);
  24090. XMEMCPY(key, client_key_der_2048, keySz);
  24091. XMEMCPY(cert, client_cert_der_2048, certSz);
  24092. #elif defined(USE_CERT_BUFFERS_1024)
  24093. byte key[sizeof_client_key_der_1024];
  24094. byte cert[sizeof(sizeof_client_cert_der_1024)];
  24095. word32 keySz = (word32)sizeof(key);
  24096. word32 certSz = (word32)sizeof(cert);
  24097. XMEMSET(key, 0, keySz);
  24098. XMEMSET(cert, 0, certSz);
  24099. XMEMCPY(key, client_key_der_1024, keySz);
  24100. XMEMCPY(cert, client_cert_der_1024, certSz);
  24101. #else
  24102. unsigned char cert[ONEK_BUF];
  24103. unsigned char key[ONEK_BUF];
  24104. XFILE fp = XBADFILE;
  24105. int certSz;
  24106. int keySz;
  24107. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  24108. XBADFILE);
  24109. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  24110. fp), 0);
  24111. if (fp != XBADFILE) {
  24112. XFCLOSE(fp);
  24113. fp = XBADFILE;
  24114. }
  24115. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  24116. XBADFILE);
  24117. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  24118. 0);
  24119. if (fp != XBADFILE)
  24120. XFCLOSE(fp);
  24121. #endif
  24122. #elif defined(HAVE_ECC)
  24123. #if defined(USE_CERT_BUFFERS_256)
  24124. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24125. unsigned char key[sizeof(ecc_clikey_der_256)];
  24126. int certSz = (int)sizeof(cert);
  24127. int keySz = (int)sizeof(key);
  24128. XMEMSET(cert, 0, certSz);
  24129. XMEMSET(key, 0, keySz);
  24130. XMEMCPY(cert, cliecc_cert_der_256, certSz);
  24131. XMEMCPY(key, ecc_clikey_der_256, keySz);
  24132. #else
  24133. unsigned char cert[ONEK_BUF];
  24134. unsigned char key[ONEK_BUF];
  24135. XFILE fp = XBADFILE;
  24136. int certSz;
  24137. int keySz;
  24138. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  24139. XBADFILE);
  24140. ExpectIntGT(certSz = (int)XFREAD(cert, 1, ONEK_BUF, fp), 0);
  24141. if (fp != XBADFILE) {
  24142. XFCLOSE(fp);
  24143. fp = XBADFILE;
  24144. }
  24145. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  24146. XBADFILE);
  24147. ExpectIntGT(keySz = (int)XFREAD(key, 1, ONEK_BUF, fp), 0);
  24148. if (fp != XBADFILE)
  24149. XFCLOSE(fp);
  24150. #endif
  24151. #endif
  24152. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24153. XMEMSET(output, 0, outputSz);
  24154. ExpectIntEQ(wc_InitRng(&rng), 0);
  24155. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24156. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24157. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24158. if (pkcs7 != NULL) {
  24159. pkcs7->content = data;
  24160. pkcs7->contentSz = (word32)sizeof(data);
  24161. pkcs7->privateKey = key;
  24162. pkcs7->privateKeySz = (word32)sizeof(key);
  24163. pkcs7->encryptOID = RSAk;
  24164. #ifdef NO_SHA
  24165. pkcs7->hashOID = SHA256h;
  24166. #else
  24167. pkcs7->hashOID = SHAh;
  24168. #endif
  24169. pkcs7->rng = &rng;
  24170. }
  24171. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  24172. wc_PKCS7_Free(pkcs7);
  24173. pkcs7 = NULL;
  24174. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24175. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24176. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24177. #ifdef ASN_BER_TO_DER
  24178. wc_PKCS7_Free(pkcs7);
  24179. /* reinitialize and test setting stream mode */
  24180. {
  24181. int signedSz = 0;
  24182. encodeSignedDataStream strm;
  24183. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24184. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24185. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24186. if (pkcs7 != NULL) {
  24187. pkcs7->content = data;
  24188. pkcs7->contentSz = (word32)sizeof(data);
  24189. pkcs7->privateKey = key;
  24190. pkcs7->privateKeySz = (word32)sizeof(key);
  24191. pkcs7->encryptOID = RSAk;
  24192. #ifdef NO_SHA
  24193. pkcs7->hashOID = SHA256h;
  24194. #else
  24195. pkcs7->hashOID = SHAh;
  24196. #endif
  24197. pkcs7->rng = &rng;
  24198. }
  24199. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
  24200. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL), 0);
  24201. ExpectIntEQ(wc_PKCS7_SetStreamMode(NULL, 1, NULL, NULL, NULL),
  24202. BAD_FUNC_ARG);
  24203. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 1);
  24204. ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, output,
  24205. outputSz), 0);
  24206. wc_PKCS7_Free(pkcs7);
  24207. pkcs7 = NULL;
  24208. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24209. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24210. /* use exact signed buffer size since BER encoded */
  24211. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, (word32)signedSz), 0);
  24212. wc_PKCS7_Free(pkcs7);
  24213. /* now try with using callbacks for IO */
  24214. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24215. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24216. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24217. if (pkcs7 != NULL) {
  24218. pkcs7->contentSz = FOURK_BUF*2;
  24219. pkcs7->privateKey = key;
  24220. pkcs7->privateKeySz = (word32)sizeof(key);
  24221. pkcs7->encryptOID = RSAk;
  24222. #ifdef NO_SHA
  24223. pkcs7->hashOID = SHA256h;
  24224. #else
  24225. pkcs7->hashOID = SHAh;
  24226. #endif
  24227. pkcs7->rng = &rng;
  24228. }
  24229. XMEMSET(&strm, 0, sizeof(strm));
  24230. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
  24231. StreamOutputCB, (void*)&strm), 0);
  24232. ExpectIntGT(signedSz = wc_PKCS7_EncodeSignedData(pkcs7, NULL, 0), 0);
  24233. wc_PKCS7_Free(pkcs7);
  24234. pkcs7 = NULL;
  24235. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24236. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24237. /* use exact signed buffer size since BER encoded */
  24238. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, strm.out, (word32)signedSz), 0);
  24239. }
  24240. #endif
  24241. #ifndef NO_PKCS7_STREAM
  24242. wc_PKCS7_Free(pkcs7);
  24243. pkcs7 = NULL;
  24244. {
  24245. word32 z;
  24246. int ret;
  24247. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24248. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24249. /* test for streaming mode */
  24250. ret = -1;
  24251. for (z = 0; z < outputSz && ret != 0; z++) {
  24252. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24253. if (ret < 0){
  24254. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24255. }
  24256. }
  24257. ExpectIntEQ(ret, 0);
  24258. ExpectIntNE(pkcs7->contentSz, 0);
  24259. ExpectNotNull(pkcs7->contentDynamic);
  24260. }
  24261. #endif /* !NO_PKCS7_STREAM */
  24262. /* Pass in bad args. */
  24263. ExpectIntEQ(wc_PKCS7_EncodeSignedData(NULL, output, outputSz),
  24264. BAD_FUNC_ARG);
  24265. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, NULL, outputSz), BAD_FUNC_ARG);
  24266. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, badOut,
  24267. badOutSz), BAD_FUNC_ARG);
  24268. if (pkcs7 != NULL) {
  24269. pkcs7->hashOID = 0; /* bad hashOID */
  24270. }
  24271. ExpectIntEQ(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz),
  24272. BAD_FUNC_ARG);
  24273. #if defined(HAVE_PKCS7) && defined(HAVE_PKCS7_RSA_RAW_SIGN_CALLBACK) && \
  24274. !defined(NO_RSA) && !defined(NO_SHA256)
  24275. /* test RSA sign raw digest callback, if using RSA and compiled in.
  24276. * Example callback assumes SHA-256, so only run test if compiled in. */
  24277. wc_PKCS7_Free(pkcs7);
  24278. pkcs7 = NULL;
  24279. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24280. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24281. if (pkcs7 != NULL) {
  24282. pkcs7->content = data;
  24283. pkcs7->contentSz = (word32)sizeof(data);
  24284. pkcs7->privateKey = key;
  24285. pkcs7->privateKeySz = (word32)sizeof(key);
  24286. pkcs7->encryptOID = RSAk;
  24287. pkcs7->hashOID = SHA256h;
  24288. pkcs7->rng = &rng;
  24289. }
  24290. ExpectIntEQ(wc_PKCS7_SetRsaSignRawDigestCb(pkcs7, rsaSignRawDigestCb), 0);
  24291. ExpectIntGT(wc_PKCS7_EncodeSignedData(pkcs7, output, outputSz), 0);
  24292. #endif
  24293. wc_PKCS7_Free(pkcs7);
  24294. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  24295. #endif
  24296. return EXPECT_RESULT();
  24297. } /* END test_wc_PKCS7_EncodeSignedData */
  24298. /*
  24299. * Testing wc_PKCS7_EncodeSignedData_ex() and wc_PKCS7_VerifySignedData_ex()
  24300. */
  24301. static int test_wc_PKCS7_EncodeSignedData_ex(void)
  24302. {
  24303. EXPECT_DECLS;
  24304. #if defined(HAVE_PKCS7)
  24305. int i;
  24306. PKCS7* pkcs7 = NULL;
  24307. WC_RNG rng;
  24308. byte outputHead[FOURK_BUF/2];
  24309. byte outputFoot[FOURK_BUF/2];
  24310. word32 outputHeadSz = (word32)sizeof(outputHead);
  24311. word32 outputFootSz = (word32)sizeof(outputFoot);
  24312. byte data[FOURK_BUF];
  24313. wc_HashAlg hash;
  24314. #ifdef NO_SHA
  24315. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  24316. #else
  24317. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24318. #endif
  24319. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24320. word32 hashSz = (word32)wc_HashGetDigestSize(hashType);
  24321. #ifndef NO_RSA
  24322. #if defined(USE_CERT_BUFFERS_2048)
  24323. byte key[sizeof(client_key_der_2048)];
  24324. byte cert[sizeof(client_cert_der_2048)];
  24325. word32 keySz = (word32)sizeof(key);
  24326. word32 certSz = (word32)sizeof(cert);
  24327. XMEMSET(key, 0, keySz);
  24328. XMEMSET(cert, 0, certSz);
  24329. XMEMCPY(key, client_key_der_2048, keySz);
  24330. XMEMCPY(cert, client_cert_der_2048, certSz);
  24331. #elif defined(USE_CERT_BUFFERS_1024)
  24332. byte key[sizeof_client_key_der_1024];
  24333. byte cert[sizeof(sizeof_client_cert_der_1024)];
  24334. word32 keySz = (word32)sizeof(key);
  24335. word32 certSz = (word32)sizeof(cert);
  24336. XMEMSET(key, 0, keySz);
  24337. XMEMSET(cert, 0, certSz);
  24338. XMEMCPY(key, client_key_der_1024, keySz);
  24339. XMEMCPY(cert, client_cert_der_1024, certSz);
  24340. #else
  24341. unsigned char cert[ONEK_BUF];
  24342. unsigned char key[ONEK_BUF];
  24343. XFILE fp = XBADFILE;
  24344. int certSz;
  24345. int keySz;
  24346. ExpectTure((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  24347. XBADFILE);
  24348. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  24349. fp), 0);
  24350. if (fp != XBADFILE) {
  24351. XFCLOSE(fp);
  24352. fp = XBADFILE;
  24353. }
  24354. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  24355. XBADFILE);
  24356. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  24357. 0);
  24358. if (fp != XBADFILE)
  24359. XFCLOSE(fp);
  24360. #endif
  24361. #elif defined(HAVE_ECC)
  24362. #if defined(USE_CERT_BUFFERS_256)
  24363. unsigned char cert[sizeof(cliecc_cert_der_256)];
  24364. unsigned char key[sizeof(ecc_clikey_der_256)];
  24365. int certSz = (int)sizeof(cert);
  24366. int keySz = (int)sizeof(key);
  24367. XMEMSET(cert, 0, certSz);
  24368. XMEMSET(key, 0, keySz);
  24369. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  24370. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  24371. #else
  24372. unsigned char cert[ONEK_BUF];
  24373. unsigned char key[ONEK_BUF];
  24374. XFILE fp = XBADFILE;
  24375. int certSz;
  24376. int keySz;
  24377. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  24378. XBADFILE);
  24379. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  24380. fp), 0);
  24381. if (fp != XBADFILE) {
  24382. XFCLOSE(fp);
  24383. fp = XBADFILE;
  24384. }
  24385. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  24386. XBADFILE);
  24387. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  24388. 0);
  24389. if (fp != XBADFILE)
  24390. XFCLOSE(fp);
  24391. #endif
  24392. #endif
  24393. XMEMSET(&rng, 0, sizeof(WC_RNG));
  24394. /* initialize large data with sequence */
  24395. for (i=0; i<(int)sizeof(data); i++)
  24396. data[i] = i & 0xff;
  24397. XMEMSET(outputHead, 0, outputHeadSz);
  24398. XMEMSET(outputFoot, 0, outputFootSz);
  24399. ExpectIntEQ(wc_InitRng(&rng), 0);
  24400. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24401. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24402. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24403. if (pkcs7 != NULL) {
  24404. pkcs7->content = NULL; /* not used for ex */
  24405. pkcs7->contentSz = (word32)sizeof(data);
  24406. pkcs7->privateKey = key;
  24407. pkcs7->privateKeySz = (word32)sizeof(key);
  24408. pkcs7->encryptOID = RSAk;
  24409. #ifdef NO_SHA
  24410. pkcs7->hashOID = SHA256h;
  24411. #else
  24412. pkcs7->hashOID = SHAh;
  24413. #endif
  24414. pkcs7->rng = &rng;
  24415. }
  24416. /* calculate hash for content */
  24417. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  24418. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  24419. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  24420. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  24421. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  24422. /* Perform PKCS7 sign using hash directly */
  24423. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24424. outputHead, &outputHeadSz, outputFoot, &outputFootSz), 0);
  24425. ExpectIntGT(outputHeadSz, 0);
  24426. ExpectIntGT(outputFootSz, 0);
  24427. wc_PKCS7_Free(pkcs7);
  24428. pkcs7 = NULL;
  24429. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24430. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24431. /* required parameter even on verify when using _ex, if using outputHead
  24432. * and outputFoot */
  24433. if (pkcs7 != NULL) {
  24434. pkcs7->contentSz = (word32)sizeof(data);
  24435. }
  24436. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24437. outputHead, outputHeadSz, outputFoot, outputFootSz), 0);
  24438. wc_PKCS7_Free(pkcs7);
  24439. pkcs7 = NULL;
  24440. /* assembly complete PKCS7 sign and use normal verify */
  24441. {
  24442. byte* output = NULL;
  24443. word32 outputSz = 0;
  24444. #ifndef NO_PKCS7_STREAM
  24445. word32 z;
  24446. int ret;
  24447. #endif /* !NO_PKCS7_STREAM */
  24448. ExpectNotNull(output = (byte*)XMALLOC(
  24449. outputHeadSz + sizeof(data) + outputFootSz, HEAP_HINT,
  24450. DYNAMIC_TYPE_TMP_BUFFER));
  24451. if (output != NULL) {
  24452. XMEMCPY(&output[outputSz], outputHead, outputHeadSz);
  24453. outputSz += outputHeadSz;
  24454. XMEMCPY(&output[outputSz], data, sizeof(data));
  24455. outputSz += sizeof(data);
  24456. XMEMCPY(&output[outputSz], outputFoot, outputFootSz);
  24457. outputSz += outputFootSz;
  24458. }
  24459. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24460. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24461. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24462. #ifndef NO_PKCS7_STREAM
  24463. wc_PKCS7_Free(pkcs7);
  24464. pkcs7 = NULL;
  24465. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24466. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24467. /* test for streaming mode */
  24468. ret = -1;
  24469. for (z = 0; z < outputSz && ret != 0; z++) {
  24470. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24471. if (ret < 0){
  24472. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24473. }
  24474. }
  24475. ExpectIntEQ(ret, 0);
  24476. ExpectIntNE(pkcs7->contentSz, 0);
  24477. ExpectNotNull(pkcs7->contentDynamic);
  24478. wc_PKCS7_Free(pkcs7);
  24479. pkcs7 = NULL;
  24480. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24481. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24482. #endif /* !NO_PKCS7_STREAM */
  24483. XFREE(output, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  24484. }
  24485. /* Pass in bad args. */
  24486. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24487. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24488. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24489. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24490. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24491. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24492. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24493. &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24494. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24495. outputHead, NULL, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24496. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24497. outputHead, &outputHeadSz, NULL, &outputFootSz), BAD_FUNC_ARG);
  24498. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24499. outputHead, &outputHeadSz, outputFoot, NULL), BAD_FUNC_ARG);
  24500. if (pkcs7 != NULL) {
  24501. pkcs7->hashOID = 0; /* bad hashOID */
  24502. }
  24503. ExpectIntEQ(wc_PKCS7_EncodeSignedData_ex(pkcs7, hashBuf, hashSz,
  24504. outputHead, &outputHeadSz, outputFoot, &outputFootSz), BAD_FUNC_ARG);
  24505. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(NULL, hashBuf, hashSz, outputHead,
  24506. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24507. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, NULL, hashSz, outputHead,
  24508. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24509. #ifndef NO_PKCS7_STREAM
  24510. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24511. outputHeadSz, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24512. #else
  24513. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, 0, outputHead,
  24514. outputHeadSz, outputFoot, outputFootSz), BUFFER_E);
  24515. #endif
  24516. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz, NULL,
  24517. outputHeadSz, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24518. #ifndef NO_PKCS7_STREAM
  24519. /* can pass in 0 buffer length with streaming API */
  24520. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24521. outputHead, 0, outputFoot, outputFootSz), WC_PKCS7_WANT_READ_E);
  24522. #else
  24523. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24524. outputHead, 0, outputFoot, outputFootSz), BAD_FUNC_ARG);
  24525. #endif
  24526. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24527. outputHead, outputHeadSz, NULL, outputFootSz), BAD_FUNC_ARG);
  24528. #ifndef NO_PKCS7_STREAM
  24529. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24530. outputHead, outputHeadSz, outputFoot, 0), WC_PKCS7_WANT_READ_E);
  24531. #else
  24532. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  24533. outputHead, outputHeadSz, outputFoot, 0), BUFFER_E);
  24534. #endif
  24535. wc_PKCS7_Free(pkcs7);
  24536. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  24537. #endif
  24538. return EXPECT_RESULT();
  24539. } /* END test_wc_PKCS7_EncodeSignedData_ex */
  24540. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  24541. /**
  24542. * Loads certs/keys from files or buffers into the argument buffers,
  24543. * helper function called by CreatePKCS7SignedData().
  24544. *
  24545. * Returns 0 on success, negative on error.
  24546. */
  24547. static int LoadPKCS7SignedDataCerts(
  24548. int useIntermediateCertChain, int pkAlgoType,
  24549. byte* intCARoot, word32* intCARootSz,
  24550. byte* intCA1, word32* intCA1Sz,
  24551. byte* intCA2, word32* intCA2Sz,
  24552. byte* cert, word32* certSz,
  24553. byte* key, word32* keySz)
  24554. {
  24555. EXPECT_DECLS;
  24556. int ret = 0;
  24557. XFILE fp = XBADFILE;
  24558. #ifndef NO_RSA
  24559. const char* intCARootRSA = "./certs/ca-cert.der";
  24560. const char* intCA1RSA = "./certs/intermediate/ca-int-cert.der";
  24561. const char* intCA2RSA = "./certs/intermediate/ca-int2-cert.der";
  24562. const char* intServCertRSA = "./certs/intermediate/server-int-cert.der";
  24563. const char* intServKeyRSA = "./certs/server-key.der";
  24564. #if !defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)
  24565. const char* cli1024Cert = "./certs/1024/client-cert.der";
  24566. const char* cli1024Key = "./certs/1024/client-key.der";
  24567. #endif
  24568. #endif
  24569. #ifdef HAVE_ECC
  24570. const char* intCARootECC = "./certs/ca-ecc-cert.der";
  24571. const char* intCA1ECC = "./certs/intermediate/ca-int-ecc-cert.der";
  24572. const char* intCA2ECC = "./certs/intermediate/ca-int2-ecc-cert.der";
  24573. const char* intServCertECC = "./certs/intermediate/server-int-ecc-cert.der";
  24574. const char* intServKeyECC = "./certs/ecc-key.der";
  24575. #ifndef USE_CERT_BUFFERS_256
  24576. const char* cliEccCert = "./certs/client-ecc-cert.der";
  24577. const char* cliEccKey = "./certs/client-ecc-key.der";
  24578. #endif
  24579. #endif
  24580. if (cert == NULL || certSz == NULL || key == NULL || keySz == NULL ||
  24581. ((useIntermediateCertChain == 1) &&
  24582. (intCARoot == NULL || intCARootSz == NULL || intCA1 == NULL ||
  24583. intCA1Sz == NULL || intCA2 == NULL || intCA2Sz == NULL))) {
  24584. return BAD_FUNC_ARG;
  24585. }
  24586. /* Read/load certs and keys to use for signing based on PK type and chain */
  24587. switch (pkAlgoType) {
  24588. #ifndef NO_RSA
  24589. case RSA_TYPE:
  24590. if (useIntermediateCertChain == 1) {
  24591. ExpectTrue((fp = XFOPEN(intCARootRSA, "rb")) != XBADFILE);
  24592. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz, fp);
  24593. if (fp != XBADFILE) {
  24594. XFCLOSE(fp);
  24595. fp = XBADFILE;
  24596. }
  24597. ExpectIntGT(*intCARootSz, 0);
  24598. ExpectTrue((fp = XFOPEN(intCA1RSA, "rb")) != XBADFILE);
  24599. if (fp != XBADFILE) {
  24600. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24601. XFCLOSE(fp);
  24602. fp = XBADFILE;
  24603. }
  24604. ExpectIntGT(*intCA1Sz, 0);
  24605. ExpectTrue((fp = XFOPEN(intCA2RSA, "rb")) != XBADFILE);
  24606. if (fp != XBADFILE) {
  24607. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24608. XFCLOSE(fp);
  24609. fp = XBADFILE;
  24610. }
  24611. ExpectIntGT(*intCA2Sz, 0);
  24612. ExpectTrue((fp = XFOPEN(intServCertRSA, "rb")) != XBADFILE);
  24613. if (fp != XBADFILE) {
  24614. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24615. XFCLOSE(fp);
  24616. fp = XBADFILE;
  24617. }
  24618. ExpectIntGT(*certSz, 0);
  24619. ExpectTrue((fp = XFOPEN(intServKeyRSA, "rb")) != XBADFILE);
  24620. if (fp != XBADFILE) {
  24621. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24622. XFCLOSE(fp);
  24623. fp = XBADFILE;
  24624. }
  24625. ExpectIntGT(*keySz, 0);
  24626. }
  24627. else {
  24628. #if defined(USE_CERT_BUFFERS_2048)
  24629. *keySz = sizeof_client_key_der_2048;
  24630. *certSz = sizeof_client_cert_der_2048;
  24631. XMEMCPY(key, client_key_der_2048, *keySz);
  24632. XMEMCPY(cert, client_cert_der_2048, *certSz);
  24633. #elif defined(USE_CERT_BUFFERS_1024)
  24634. *keySz = sizeof_client_key_der_1024;
  24635. *certSz = sizeof_client_cert_der_1024;
  24636. XMEMCPY(key, client_key_der_1024, *keySz);
  24637. XMEMCPY(cert, client_cert_der_1024, *certSz);
  24638. #else
  24639. ExpectTrue((fp = XFOPEN(cli1024Key, "rb")) != XBADFILE);
  24640. if (fp != XBADFILE) {
  24641. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24642. XFCLOSE(fp);
  24643. fp = XBADFILE;
  24644. }
  24645. ExpectIntGT(*keySz, 0);
  24646. ExpectTrue((fp = XFOPEN(cli1024Cert, "rb")) != XBADFILE);
  24647. if (fp != XBADFILE) {
  24648. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24649. XFCLOSE(fp);
  24650. fp = XBADFILE;
  24651. }
  24652. ExpectIntGT(*certSz, 0);
  24653. #endif /* USE_CERT_BUFFERS_2048 */
  24654. }
  24655. break;
  24656. #endif /* !NO_RSA */
  24657. #ifdef HAVE_ECC
  24658. case ECC_TYPE:
  24659. if (useIntermediateCertChain == 1) {
  24660. ExpectTrue((fp = XFOPEN(intCARootECC, "rb")) != XBADFILE);
  24661. if (fp != XBADFILE) {
  24662. *intCARootSz = (word32)XFREAD(intCARoot, 1, *intCARootSz,
  24663. fp);
  24664. XFCLOSE(fp);
  24665. fp = XBADFILE;
  24666. }
  24667. ExpectIntGT(*intCARootSz, 0);
  24668. ExpectTrue((fp = XFOPEN(intCA1ECC, "rb")) != XBADFILE);
  24669. if (fp != XBADFILE) {
  24670. *intCA1Sz = (word32)XFREAD(intCA1, 1, *intCA1Sz, fp);
  24671. XFCLOSE(fp);
  24672. fp = XBADFILE;
  24673. }
  24674. ExpectIntGT(*intCA1Sz, 0);
  24675. ExpectTrue((fp = XFOPEN(intCA2ECC, "rb")) != XBADFILE);
  24676. if (fp != XBADFILE) {
  24677. *intCA2Sz = (word32)XFREAD(intCA2, 1, *intCA2Sz, fp);
  24678. XFCLOSE(fp);
  24679. fp = XBADFILE;
  24680. }
  24681. ExpectIntGT(*intCA2Sz, 0);
  24682. ExpectTrue((fp = XFOPEN(intServCertECC, "rb")) != XBADFILE);
  24683. if (fp != XBADFILE) {
  24684. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24685. XFCLOSE(fp);
  24686. fp = XBADFILE;
  24687. }
  24688. ExpectIntGT(*certSz, 0);
  24689. ExpectTrue((fp = XFOPEN(intServKeyECC, "rb")) != XBADFILE);
  24690. if (fp != XBADFILE) {
  24691. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24692. XFCLOSE(fp);
  24693. fp = XBADFILE;
  24694. }
  24695. ExpectIntGT(*keySz, 0);
  24696. }
  24697. else {
  24698. #if defined(USE_CERT_BUFFERS_256)
  24699. *keySz = sizeof_ecc_clikey_der_256;
  24700. *certSz = sizeof_cliecc_cert_der_256;
  24701. XMEMCPY(key, ecc_clikey_der_256, *keySz);
  24702. XMEMCPY(cert, cliecc_cert_der_256, *certSz);
  24703. #else
  24704. ExpectTrue((fp = XFOPEN(cliEccKey, "rb")) != XBADFILE);
  24705. if (fp != XBADFILE) {
  24706. *keySz = (word32)XFREAD(key, 1, *keySz, fp);
  24707. XFCLOSE(fp);
  24708. fp = XBADFILE;
  24709. }
  24710. ExpectIntGT(*keySz, 0);
  24711. ExpectTrue((fp = XFOPEN(cliEccCert, "rb")) != XBADFILE);
  24712. if (fp != XBADFILE) {
  24713. *certSz = (word32)XFREAD(cert, 1, *certSz, fp);
  24714. XFCLOSE(fp);
  24715. fp = XBADFILE;
  24716. }
  24717. ExpectIntGT(*certSz, 0);
  24718. #endif /* USE_CERT_BUFFERS_256 */
  24719. }
  24720. break;
  24721. #endif /* HAVE_ECC */
  24722. default:
  24723. WOLFSSL_MSG("Unsupported SignedData PK type");
  24724. ret = BAD_FUNC_ARG;
  24725. break;
  24726. }
  24727. if (EXPECT_FAIL() && (ret == 0)) {
  24728. ret = BAD_FUNC_ARG;
  24729. }
  24730. return ret;
  24731. }
  24732. /**
  24733. * Creates a PKCS7/CMS SignedData bundle to use for testing.
  24734. *
  24735. * output output buffer to place SignedData
  24736. * outputSz size of output buffer
  24737. * data data buffer to be signed
  24738. * dataSz size of data buffer
  24739. * withAttribs [1/0] include attributes in SignedData message
  24740. * detachedSig [1/0] create detached signature, no content
  24741. * useIntCertChain [1/0] use certificate chain and include intermediate and
  24742. * root CAs in bundle
  24743. * pkAlgoType RSA_TYPE or ECC_TYPE, choose what key/cert type to use
  24744. *
  24745. * Return size of bundle created on success, negative on error */
  24746. static int CreatePKCS7SignedData(unsigned char* output, int outputSz,
  24747. byte* data, word32 dataSz,
  24748. int withAttribs, int detachedSig,
  24749. int useIntermediateCertChain,
  24750. int pkAlgoType)
  24751. {
  24752. EXPECT_DECLS;
  24753. int ret = 0;
  24754. WC_RNG rng;
  24755. PKCS7* pkcs7 = NULL;
  24756. static byte messageTypeOid[] =
  24757. { 0x06, 0x0a, 0x60, 0x86, 0x48, 0x01, 0x86, 0xF8, 0x45, 0x01,
  24758. 0x09, 0x02 };
  24759. static byte messageType[] = { 0x13, 2, '1', '9' };
  24760. PKCS7Attrib attribs[] =
  24761. {
  24762. { messageTypeOid, sizeof(messageTypeOid), messageType,
  24763. sizeof(messageType) }
  24764. };
  24765. byte intCARoot[TWOK_BUF];
  24766. byte intCA1[TWOK_BUF];
  24767. byte intCA2[TWOK_BUF];
  24768. byte cert[TWOK_BUF];
  24769. byte key[TWOK_BUF];
  24770. word32 intCARootSz = sizeof(intCARoot);
  24771. word32 intCA1Sz = sizeof(intCA1);
  24772. word32 intCA2Sz = sizeof(intCA2);
  24773. word32 certSz = sizeof(cert);
  24774. word32 keySz = sizeof(key);
  24775. XMEMSET(intCARoot, 0, intCARootSz);
  24776. XMEMSET(intCA1, 0, intCA1Sz);
  24777. XMEMSET(intCA2, 0, intCA2Sz);
  24778. XMEMSET(cert, 0, certSz);
  24779. XMEMSET(key, 0, keySz);
  24780. ret = LoadPKCS7SignedDataCerts(useIntermediateCertChain, pkAlgoType,
  24781. intCARoot, &intCARootSz, intCA1, &intCA1Sz, intCA2, &intCA2Sz,
  24782. cert, &certSz, key, &keySz);
  24783. ExpectIntEQ(ret, 0);
  24784. XMEMSET(output, 0, outputSz);
  24785. ExpectIntEQ(wc_InitRng(&rng), 0);
  24786. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24787. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24788. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, certSz), 0);
  24789. if (useIntermediateCertChain == 1) {
  24790. /* Add intermediate and root CA certs into SignedData Certs SET */
  24791. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA2, intCA2Sz), 0);
  24792. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCA1, intCA1Sz), 0);
  24793. ExpectIntEQ(wc_PKCS7_AddCertificate(pkcs7, intCARoot, intCARootSz), 0);
  24794. }
  24795. if (pkcs7 != NULL) {
  24796. pkcs7->content = data;
  24797. pkcs7->contentSz = dataSz;
  24798. pkcs7->privateKey = key;
  24799. pkcs7->privateKeySz = (word32)sizeof(key);
  24800. if (pkAlgoType == RSA_TYPE) {
  24801. pkcs7->encryptOID = RSAk;
  24802. }
  24803. else {
  24804. pkcs7->encryptOID = ECDSAk;
  24805. }
  24806. #ifdef NO_SHA
  24807. pkcs7->hashOID = SHA256h;
  24808. #else
  24809. pkcs7->hashOID = SHAh;
  24810. #endif
  24811. pkcs7->rng = &rng;
  24812. if (withAttribs) {
  24813. /* include a signed attribute */
  24814. pkcs7->signedAttribs = attribs;
  24815. pkcs7->signedAttribsSz = (sizeof(attribs)/sizeof(PKCS7Attrib));
  24816. }
  24817. }
  24818. if (detachedSig) {
  24819. ExpectIntEQ(wc_PKCS7_SetDetached(pkcs7, 1), 0);
  24820. }
  24821. outputSz = wc_PKCS7_EncodeSignedData(pkcs7, output, (word32)outputSz);
  24822. ExpectIntGT(outputSz, 0);
  24823. wc_PKCS7_Free(pkcs7);
  24824. pkcs7 = NULL;
  24825. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24826. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24827. if (detachedSig && (pkcs7 != NULL)) {
  24828. pkcs7->content = data;
  24829. pkcs7->contentSz = dataSz;
  24830. }
  24831. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, (word32)outputSz), 0);
  24832. wc_PKCS7_Free(pkcs7);
  24833. wc_FreeRng(&rng);
  24834. if (EXPECT_FAIL()) {
  24835. outputSz = 0;
  24836. }
  24837. return outputSz;
  24838. }
  24839. #endif
  24840. /*
  24841. * Testing wc_PKCS_VerifySignedData()
  24842. */
  24843. static int test_wc_PKCS7_VerifySignedData_RSA(void)
  24844. {
  24845. EXPECT_DECLS;
  24846. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  24847. PKCS7* pkcs7 = NULL;
  24848. byte output[6000]; /* Large size needed for bundles with int CA certs */
  24849. word32 outputSz = sizeof(output);
  24850. byte data[] = "Test data to encode.";
  24851. byte badOut[1];
  24852. word32 badOutSz = 0;
  24853. byte badContent[] = "This is different content than was signed";
  24854. wc_HashAlg hash;
  24855. #ifdef NO_SHA
  24856. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  24857. #else
  24858. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  24859. #endif
  24860. byte hashBuf[WC_MAX_DIGEST_SIZE];
  24861. word32 hashSz = (word32)wc_HashGetDigestSize(hashType);
  24862. #ifndef NO_RSA
  24863. PKCS7DecodedAttrib* decodedAttrib = NULL;
  24864. /* contentType OID (1.2.840.113549.1.9.3) */
  24865. static const byte contentTypeOid[] =
  24866. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xF7, 0x0d, 0x01, 0x09, 0x03 };
  24867. /* PKCS#7 DATA content type (contentType defaults to DATA) */
  24868. static const byte dataType[] =
  24869. { 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x07, 0x01 };
  24870. /* messageDigest OID (1.2.840.113549.1.9.4) */
  24871. static const byte messageDigestOid[] =
  24872. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x04 };
  24873. #ifndef NO_ASN_TIME
  24874. /* signingTime OID () */
  24875. static const byte signingTimeOid[] =
  24876. { 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x09, 0x05};
  24877. #endif
  24878. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  24879. int dateLength = 0;
  24880. byte dateFormat;
  24881. const byte* datePart = NULL;
  24882. struct tm timearg;
  24883. time_t now;
  24884. struct tm* nowTm = NULL;
  24885. #ifdef NEED_TMP_TIME
  24886. struct tm tmpTimeStorage;
  24887. struct tm* tmpTime = &tmpTimeStorage;
  24888. #endif
  24889. #endif /* !NO_ASN && !NO_ASN_TIME */
  24890. #ifndef NO_PKCS7_STREAM
  24891. word32 z;
  24892. int ret;
  24893. #endif /* !NO_PKCS7_STREAM */
  24894. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  24895. /* Success test with RSA certs/key */
  24896. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  24897. (word32)sizeof(data), 0, 0, 0, RSA_TYPE)), 0);
  24898. /* calculate hash for content, used later */
  24899. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  24900. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  24901. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  24902. DoExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  24903. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24904. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  24905. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24906. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  24907. #ifndef NO_PKCS7_STREAM
  24908. wc_PKCS7_Free(pkcs7);
  24909. pkcs7 = NULL;
  24910. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  24911. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  24912. /* test for streaming */
  24913. ret = -1;
  24914. for (z = 0; z < outputSz && ret != 0; z++) {
  24915. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  24916. if (ret < 0){
  24917. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  24918. }
  24919. }
  24920. ExpectIntEQ(ret, 0);
  24921. ExpectIntNE(pkcs7->contentSz, 0);
  24922. ExpectNotNull(pkcs7->contentDynamic);
  24923. #endif /* !NO_PKCS7_STREAM */
  24924. /* Check that decoded signed attributes are correct */
  24925. /* messageDigest should be first */
  24926. if (pkcs7 != NULL) {
  24927. decodedAttrib = pkcs7->decodedAttrib;
  24928. }
  24929. ExpectNotNull(decodedAttrib);
  24930. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(messageDigestOid));
  24931. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, messageDigestOid,
  24932. decodedAttrib->oidSz), 0);
  24933. /* + 2 for OCTET STRING and length bytes */
  24934. ExpectIntEQ(decodedAttrib->valueSz, hashSz + 2);
  24935. ExpectNotNull(decodedAttrib->value);
  24936. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, hashBuf, hashSz), 0);
  24937. #ifndef NO_ASN_TIME
  24938. /* signingTime should be second */
  24939. if (decodedAttrib != NULL) {
  24940. decodedAttrib = decodedAttrib->next;
  24941. }
  24942. ExpectNotNull(decodedAttrib);
  24943. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(signingTimeOid));
  24944. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, signingTimeOid,
  24945. decodedAttrib->oidSz), 0);
  24946. ExpectIntGT(decodedAttrib->valueSz, 0);
  24947. ExpectNotNull(decodedAttrib->value);
  24948. #endif
  24949. /* Verify signingTime if ASN and time are available */
  24950. #if !defined(NO_ASN) && !defined(NO_ASN_TIME)
  24951. ExpectIntEQ(wc_GetDateInfo(decodedAttrib->value, decodedAttrib->valueSz,
  24952. &datePart, &dateFormat, &dateLength), 0);
  24953. ExpectNotNull(datePart);
  24954. ExpectIntGT(dateLength, 0);
  24955. XMEMSET(&timearg, 0, sizeof(timearg));
  24956. ExpectIntEQ(wc_GetDateAsCalendarTime(datePart, dateLength, dateFormat,
  24957. &timearg), 0);
  24958. /* Get current time and compare year/month/day against attribute value */
  24959. ExpectIntEQ(wc_GetTime(&now, sizeof(now)), 0);
  24960. nowTm = (struct tm*)XGMTIME((time_t*)&now, tmpTime);
  24961. ExpectNotNull(nowTm);
  24962. ExpectIntEQ(timearg.tm_year, nowTm->tm_year);
  24963. ExpectIntEQ(timearg.tm_mon, nowTm->tm_mon);
  24964. ExpectIntEQ(timearg.tm_mday, nowTm->tm_mday);
  24965. #endif /* !NO_ASN && !NO_ASN_TIME */
  24966. /* contentType should be third */
  24967. if (decodedAttrib != NULL) {
  24968. decodedAttrib = decodedAttrib->next;
  24969. }
  24970. ExpectNotNull(decodedAttrib);
  24971. ExpectIntEQ(decodedAttrib->oidSz, (word32)sizeof(contentTypeOid));
  24972. ExpectIntEQ(XMEMCMP(decodedAttrib->oid, contentTypeOid,
  24973. decodedAttrib->oidSz), 0);
  24974. ExpectIntEQ(decodedAttrib->valueSz, (int)sizeof(dataType) + 2);
  24975. ExpectNotNull(decodedAttrib->value);
  24976. ExpectIntEQ(XMEMCMP(decodedAttrib->value + 2, dataType, sizeof(dataType)),
  24977. 0);
  24978. #endif /* !NO_RSA */
  24979. /* Test bad args. */
  24980. ExpectIntEQ(wc_PKCS7_VerifySignedData(NULL, output, outputSz),
  24981. BAD_FUNC_ARG);
  24982. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, NULL, outputSz),
  24983. BAD_FUNC_ARG);
  24984. #ifndef NO_PKCS7_STREAM
  24985. /* can pass in 0 buffer length with streaming API */
  24986. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  24987. badOutSz), WC_PKCS7_WANT_READ_E);
  24988. #else
  24989. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, badOut,
  24990. badOutSz), BAD_FUNC_ARG);
  24991. #endif
  24992. wc_PKCS7_Free(pkcs7);
  24993. pkcs7 = NULL;
  24994. #ifndef NO_RSA
  24995. /* Try RSA certs/key/sig first */
  24996. outputSz = sizeof(output);
  24997. XMEMSET(output, 0, outputSz);
  24998. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  24999. (word32)sizeof(data),
  25000. 1, 1, 0, RSA_TYPE)), 0);
  25001. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25002. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25003. if (pkcs7 != NULL) {
  25004. pkcs7->content = badContent;
  25005. pkcs7->contentSz = sizeof(badContent);
  25006. }
  25007. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  25008. SIG_VERIFY_E);
  25009. wc_PKCS7_Free(pkcs7);
  25010. pkcs7 = NULL;
  25011. #ifndef NO_PKCS7_STREAM
  25012. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25013. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25014. if (pkcs7 != NULL) {
  25015. pkcs7->content = badContent;
  25016. pkcs7->contentSz = sizeof(badContent);
  25017. }
  25018. /* test for streaming */
  25019. ret = -1;
  25020. for (z = 0; z < outputSz && ret != 0; z++) {
  25021. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25022. if (ret == WC_PKCS7_WANT_READ_E){
  25023. continue;
  25024. }
  25025. else if (ret < 0) {
  25026. break;
  25027. }
  25028. }
  25029. ExpectIntEQ(ret, SIG_VERIFY_E);
  25030. ExpectIntNE(pkcs7->contentSz, 0);
  25031. ExpectNotNull(pkcs7->contentDynamic);
  25032. wc_PKCS7_Free(pkcs7);
  25033. pkcs7 = NULL;
  25034. #endif /* !NO_PKCS7_STREAM */
  25035. /* Test success case with detached signature and valid content */
  25036. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25037. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25038. if (pkcs7 != NULL) {
  25039. pkcs7->content = data;
  25040. pkcs7->contentSz = sizeof(data);
  25041. }
  25042. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25043. wc_PKCS7_Free(pkcs7);
  25044. pkcs7 = NULL;
  25045. #ifndef NO_PKCS7_STREAM
  25046. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25047. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25048. if (pkcs7 != NULL) {
  25049. pkcs7->content = data;
  25050. pkcs7->contentSz = sizeof(data);
  25051. }
  25052. /* test for streaming */
  25053. ret = -1;
  25054. for (z = 0; z < outputSz && ret != 0; z++) {
  25055. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25056. if (ret < 0){
  25057. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25058. }
  25059. }
  25060. ExpectIntEQ(ret, 0);
  25061. ExpectIntNE(pkcs7->contentSz, 0);
  25062. ExpectNotNull(pkcs7->contentDynamic);
  25063. wc_PKCS7_Free(pkcs7);
  25064. pkcs7 = NULL;
  25065. #endif /* !NO_PKCS7_STREAM */
  25066. /* verify using pre-computed content digest only (no content) */
  25067. {
  25068. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25069. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  25070. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25071. output, outputSz, NULL, 0), 0);
  25072. wc_PKCS7_Free(pkcs7);
  25073. pkcs7 = NULL;
  25074. }
  25075. #endif /* !NO_RSA */
  25076. /* Test verify on signedData containing intermediate/root CA certs */
  25077. #ifndef NO_RSA
  25078. outputSz = sizeof(output);
  25079. XMEMSET(output, 0, outputSz);
  25080. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  25081. (word32)sizeof(data),
  25082. 0, 0, 1, RSA_TYPE)), 0);
  25083. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25084. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25085. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25086. wc_PKCS7_Free(pkcs7);
  25087. pkcs7 = NULL;
  25088. #ifndef NO_PKCS7_STREAM
  25089. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25090. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25091. /* test for streaming */
  25092. ret = -1;
  25093. for (z = 0; z < outputSz && ret != 0; z++) {
  25094. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25095. if (ret < 0){
  25096. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25097. }
  25098. }
  25099. ExpectIntEQ(ret, 0);
  25100. ExpectIntNE(pkcs7->contentSz, 0);
  25101. ExpectNotNull(pkcs7->contentDynamic);
  25102. wc_PKCS7_Free(pkcs7);
  25103. pkcs7 = NULL;
  25104. #endif /* !NO_PKCS7_STREAM */
  25105. #endif /* !NO_RSA */
  25106. #if defined(ASN_BER_TO_DER) && !defined(NO_PKCS7_STREAM) && \
  25107. !defined(NO_FILESYSTEM)
  25108. {
  25109. XFILE signedBundle = XBADFILE;
  25110. int signedBundleSz = 0;
  25111. int chunkSz = 1;
  25112. int i, rc = 0;
  25113. byte* buf = NULL;
  25114. ExpectTrue((signedBundle = XFOPEN("./certs/test-stream-sign.p7b",
  25115. "rb")) != XBADFILE);
  25116. ExpectTrue(XFSEEK(signedBundle, 0, XSEEK_END) == 0);
  25117. ExpectIntGT(signedBundleSz = (int)XFTELL(signedBundle), 0);
  25118. ExpectTrue(XFSEEK(signedBundle, 0, XSEEK_SET) == 0);
  25119. ExpectNotNull(buf = (byte*)XMALLOC(signedBundleSz, HEAP_HINT,
  25120. DYNAMIC_TYPE_FILE));
  25121. if (buf != NULL) {
  25122. ExpectIntEQ(XFREAD(buf, 1, (size_t)signedBundleSz, signedBundle),
  25123. signedBundleSz);
  25124. }
  25125. if (signedBundle != XBADFILE) {
  25126. XFCLOSE(signedBundle);
  25127. signedBundle = XBADFILE;
  25128. }
  25129. if (buf != NULL) {
  25130. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25131. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25132. for (i = 0; i < signedBundleSz;) {
  25133. int sz = (i + chunkSz > signedBundleSz)? signedBundleSz - i :
  25134. chunkSz;
  25135. rc = wc_PKCS7_VerifySignedData(pkcs7, buf + i, (word32)sz);
  25136. if (rc < 0 ) {
  25137. if (rc == WC_PKCS7_WANT_READ_E) {
  25138. i += sz;
  25139. continue;
  25140. }
  25141. break;
  25142. }
  25143. else {
  25144. break;
  25145. }
  25146. }
  25147. ExpectIntEQ(rc, PKCS7_SIGNEEDS_CHECK);
  25148. wc_PKCS7_Free(pkcs7);
  25149. pkcs7 = NULL;
  25150. }
  25151. /* now try with malformed bundle */
  25152. if (buf != NULL) {
  25153. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25154. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25155. buf[signedBundleSz - 2] = buf[signedBundleSz - 2] + 1;
  25156. for (i = 0; i < signedBundleSz;) {
  25157. int sz = (i + chunkSz > signedBundleSz)? signedBundleSz - i :
  25158. chunkSz;
  25159. rc = wc_PKCS7_VerifySignedData(pkcs7, buf + i, (word32)sz);
  25160. if (rc < 0 ) {
  25161. if (rc == WC_PKCS7_WANT_READ_E) {
  25162. i += sz;
  25163. continue;
  25164. }
  25165. break;
  25166. }
  25167. else {
  25168. break;
  25169. }
  25170. }
  25171. ExpectIntEQ(rc, ASN_PARSE_E);
  25172. wc_PKCS7_Free(pkcs7);
  25173. pkcs7 = NULL;
  25174. }
  25175. if (buf != NULL)
  25176. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  25177. }
  25178. #endif /* BER and stream */
  25179. #endif
  25180. return EXPECT_RESULT();
  25181. } /* END test_wc_PKCS7_VerifySignedData()_RSA */
  25182. /*
  25183. * Testing wc_PKCS_VerifySignedData()
  25184. */
  25185. static int test_wc_PKCS7_VerifySignedData_ECC(void)
  25186. {
  25187. EXPECT_DECLS;
  25188. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  25189. PKCS7* pkcs7 = NULL;
  25190. byte output[6000]; /* Large size needed for bundles with int CA certs */
  25191. word32 outputSz = sizeof(output);
  25192. byte data[] = "Test data to encode.";
  25193. byte badContent[] = "This is different content than was signed";
  25194. wc_HashAlg hash;
  25195. #ifndef NO_PKCS7_STREAM
  25196. word32 z;
  25197. int ret;
  25198. #endif /* !NO_PKCS7_STREAM */
  25199. #ifdef NO_SHA
  25200. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  25201. #else
  25202. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  25203. #endif
  25204. byte hashBuf[WC_MAX_DIGEST_SIZE];
  25205. word32 hashSz = (word32)wc_HashGetDigestSize(hashType);
  25206. XMEMSET(&hash, 0, sizeof(wc_HashAlg));
  25207. /* Success test with ECC certs/key */
  25208. outputSz = sizeof(output);
  25209. XMEMSET(output, 0, outputSz);
  25210. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  25211. (word32)sizeof(data), 0, 0, 0, ECC_TYPE)), 0);
  25212. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25213. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  25214. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25215. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25216. wc_PKCS7_Free(pkcs7);
  25217. pkcs7 = NULL;
  25218. #ifndef NO_PKCS7_STREAM
  25219. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25220. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25221. /* test for streaming */
  25222. ret = -1;
  25223. for (z = 0; z < outputSz && ret != 0; z++) {
  25224. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25225. if (ret < 0){
  25226. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25227. }
  25228. }
  25229. ExpectIntEQ(ret, 0);
  25230. ExpectIntNE(pkcs7->contentSz, 0);
  25231. ExpectNotNull(pkcs7->contentDynamic);
  25232. wc_PKCS7_Free(pkcs7);
  25233. pkcs7 = NULL;
  25234. #endif /* !NO_PKCS7_STREAM */
  25235. /* Invalid content should error, use detached signature so we can
  25236. * easily change content */
  25237. outputSz = sizeof(output);
  25238. XMEMSET(output, 0, outputSz);
  25239. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  25240. (word32)sizeof(data), 1, 1, 0, ECC_TYPE)), 0);
  25241. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25242. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25243. if (pkcs7 != NULL) {
  25244. pkcs7->content = badContent;
  25245. pkcs7->contentSz = sizeof(badContent);
  25246. }
  25247. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz),
  25248. SIG_VERIFY_E);
  25249. wc_PKCS7_Free(pkcs7);
  25250. pkcs7 = NULL;
  25251. #ifndef NO_PKCS7_STREAM
  25252. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25253. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25254. if (pkcs7 != NULL) {
  25255. pkcs7->content = badContent;
  25256. pkcs7->contentSz = sizeof(badContent);
  25257. }
  25258. /* test for streaming */
  25259. ret = -1;
  25260. for (z = 0; z < outputSz && ret != 0; z++) {
  25261. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25262. if (ret == WC_PKCS7_WANT_READ_E){
  25263. continue;
  25264. }
  25265. else if (ret < 0) {
  25266. break;
  25267. }
  25268. }
  25269. ExpectIntEQ(ret, SIG_VERIFY_E);
  25270. ExpectIntNE(pkcs7->contentSz, 0);
  25271. ExpectNotNull(pkcs7->contentDynamic);
  25272. wc_PKCS7_Free(pkcs7);
  25273. pkcs7 = NULL;
  25274. #endif /* !NO_PKCS7_STREAM */
  25275. /* Test success case with detached signature and valid content */
  25276. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25277. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25278. if (pkcs7 != NULL) {
  25279. pkcs7->content = data;
  25280. pkcs7->contentSz = sizeof(data);
  25281. }
  25282. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25283. wc_PKCS7_Free(pkcs7);
  25284. pkcs7 = NULL;
  25285. #ifndef NO_PKCS7_STREAM
  25286. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25287. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25288. if (pkcs7 != NULL) {
  25289. pkcs7->content = data;
  25290. pkcs7->contentSz = sizeof(data);
  25291. }
  25292. /* test for streaming */
  25293. ret = -1;
  25294. for (z = 0; z < outputSz && ret != 0; z++) {
  25295. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25296. if (ret < 0){
  25297. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25298. }
  25299. }
  25300. ExpectIntEQ(ret, 0);
  25301. ExpectIntNE(pkcs7->contentSz, 0);
  25302. ExpectNotNull(pkcs7->contentDynamic);
  25303. wc_PKCS7_Free(pkcs7);
  25304. pkcs7 = NULL;
  25305. #endif /* !NO_PKCS7_STREAM */
  25306. /* verify using pre-computed content digest only (no content) */
  25307. {
  25308. /* calculate hash for content */
  25309. ExpectIntEQ(wc_HashInit(&hash, hashType), 0);
  25310. ExpectIntEQ(wc_HashUpdate(&hash, hashType, data, sizeof(data)), 0);
  25311. ExpectIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  25312. ExpectIntEQ(wc_HashFree(&hash, hashType), 0);
  25313. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25314. ExpectIntEQ(wc_PKCS7_Init(pkcs7, NULL, 0), 0);
  25315. ExpectIntEQ(wc_PKCS7_VerifySignedData_ex(pkcs7, hashBuf, hashSz,
  25316. output, outputSz, NULL, 0), 0);
  25317. wc_PKCS7_Free(pkcs7);
  25318. pkcs7 = NULL;
  25319. }
  25320. /* Test verify on signedData containing intermediate/root CA certs */
  25321. outputSz = sizeof(output);
  25322. XMEMSET(output, 0, outputSz);
  25323. ExpectIntGT((outputSz = (word32)CreatePKCS7SignedData(output, (int)outputSz, data,
  25324. (word32)sizeof(data), 0, 0, 1, ECC_TYPE)), 0);
  25325. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25326. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25327. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, output, outputSz), 0);
  25328. wc_PKCS7_Free(pkcs7);
  25329. pkcs7 = NULL;
  25330. #ifndef NO_PKCS7_STREAM
  25331. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25332. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  25333. /* test for streaming */
  25334. ret = -1;
  25335. for (z = 0; z < outputSz && ret != 0; z++) {
  25336. ret = wc_PKCS7_VerifySignedData(pkcs7, output + z, 1);
  25337. if (ret < 0){
  25338. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  25339. }
  25340. }
  25341. ExpectIntEQ(ret, 0);
  25342. ExpectIntNE(pkcs7->contentSz, 0);
  25343. ExpectNotNull(pkcs7->contentDynamic);
  25344. wc_PKCS7_Free(pkcs7);
  25345. pkcs7 = NULL;
  25346. #endif /* !NO_PKCS7_STREAM */
  25347. #endif
  25348. return EXPECT_RESULT();
  25349. } /* END test_wc_PKCS7_VerifySignedData_ECC() */
  25350. #if defined(HAVE_PKCS7) && !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  25351. !defined(NO_AES_256)
  25352. static const byte defKey[] = {
  25353. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25354. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25355. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25356. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25357. };
  25358. static byte aesHandle[32]; /* simulated hardware key handle */
  25359. /* return 0 on success */
  25360. static int myDecryptionFunc(PKCS7* pkcs7, int encryptOID, byte* iv, int ivSz,
  25361. byte* aad, word32 aadSz, byte* authTag, word32 authTagSz,
  25362. byte* in, int inSz, byte* out, void* usrCtx)
  25363. {
  25364. int ret;
  25365. Aes aes;
  25366. if (usrCtx == NULL) {
  25367. /* no simulated handle passed in */
  25368. return -1;
  25369. }
  25370. switch (encryptOID) {
  25371. case AES256CBCb:
  25372. if (ivSz != AES_BLOCK_SIZE)
  25373. return BAD_FUNC_ARG;
  25374. break;
  25375. default:
  25376. WOLFSSL_MSG("Unsupported content cipher type for test");
  25377. return ALGO_ID_E;
  25378. };
  25379. /* simulate using handle to get key */
  25380. ret = wc_AesInit(&aes, HEAP_HINT, INVALID_DEVID);
  25381. if (ret == 0) {
  25382. ret = wc_AesSetKey(&aes, (byte*)usrCtx, 32, iv, AES_DECRYPTION);
  25383. if (ret == 0)
  25384. ret = wc_AesCbcDecrypt(&aes, out, in, (word32)inSz);
  25385. wc_AesFree(&aes);
  25386. }
  25387. (void)aad;
  25388. (void)aadSz;
  25389. (void)authTag;
  25390. (void)authTagSz;
  25391. (void)pkcs7;
  25392. return ret;
  25393. }
  25394. /* returns key size on success */
  25395. static int myCEKwrapFunc(PKCS7* pkcs7, byte* cek, word32 cekSz, byte* keyId,
  25396. word32 keyIdSz, byte* orginKey, word32 orginKeySz,
  25397. byte* out, word32 outSz, int keyWrapAlgo, int type, int direction)
  25398. {
  25399. int ret = -1;
  25400. (void)cekSz;
  25401. (void)cek;
  25402. (void)outSz;
  25403. (void)keyIdSz;
  25404. (void)direction;
  25405. (void)orginKey; /* used with KAKRI */
  25406. (void)orginKeySz;
  25407. if (out == NULL)
  25408. return BAD_FUNC_ARG;
  25409. if (keyId[0] != 0x00) {
  25410. return -1;
  25411. }
  25412. if (type != (int)PKCS7_KEKRI) {
  25413. return -1;
  25414. }
  25415. switch (keyWrapAlgo) {
  25416. case AES256_WRAP:
  25417. /* simulate setting a handle for later decryption but use key
  25418. * as handle in the test case here */
  25419. ret = wc_AesKeyUnWrap(defKey, sizeof(defKey), cek, cekSz,
  25420. aesHandle, sizeof(aesHandle), NULL);
  25421. if (ret < 0)
  25422. return ret;
  25423. ret = wc_PKCS7_SetDecodeEncryptedCtx(pkcs7, (void*)aesHandle);
  25424. if (ret < 0)
  25425. return ret;
  25426. /* return key size on success */
  25427. return sizeof(defKey);
  25428. default:
  25429. WOLFSSL_MSG("Unsupported key wrap algorithm in example");
  25430. return BAD_KEYWRAP_ALG_E;
  25431. };
  25432. }
  25433. #endif /* HAVE_PKCS7 && !NO_AES && HAVE_AES_CBC && !NO_AES_256 */
  25434. /*
  25435. * Testing wc_PKCS7_EncodeEnvelopedData()
  25436. */
  25437. static int test_wc_PKCS7_EncodeDecodeEnvelopedData(void)
  25438. {
  25439. EXPECT_DECLS;
  25440. #if defined(HAVE_PKCS7)
  25441. PKCS7* pkcs7 = NULL;
  25442. #ifdef ASN_BER_TO_DER
  25443. int encodedSz = 0;
  25444. #endif
  25445. #ifdef ECC_TIMING_RESISTANT
  25446. WC_RNG rng;
  25447. #endif
  25448. word32 tempWrd32 = 0;
  25449. byte* tmpBytePtr = NULL;
  25450. const char input[] = "Test data to encode.";
  25451. int i;
  25452. int testSz = 0;
  25453. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) || \
  25454. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25455. byte* rsaCert = NULL;
  25456. byte* rsaPrivKey = NULL;
  25457. word32 rsaCertSz;
  25458. word32 rsaPrivKeySz;
  25459. #if !defined(NO_FILESYSTEM) && (!defined(USE_CERT_BUFFERS_1024) && \
  25460. !defined(USE_CERT_BUFFERS_2048) )
  25461. static const char* rsaClientCert = "./certs/client-cert.der";
  25462. static const char* rsaClientKey = "./certs/client-key.der";
  25463. rsaCertSz = (word32)sizeof(rsaClientCert);
  25464. rsaPrivKeySz = (word32)sizeof(rsaClientKey);
  25465. #endif
  25466. #endif
  25467. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25468. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25469. byte* eccCert = NULL;
  25470. byte* eccPrivKey = NULL;
  25471. word32 eccCertSz;
  25472. word32 eccPrivKeySz;
  25473. #if !defined(NO_FILESYSTEM) && !defined(USE_CERT_BUFFERS_256)
  25474. static const char* eccClientCert = "./certs/client-ecc-cert.der";
  25475. static const char* eccClientKey = "./certs/ecc-client-key.der";
  25476. #endif
  25477. #endif
  25478. /* Generic buffer size. */
  25479. byte output[ONEK_BUF];
  25480. byte decoded[sizeof(input)/sizeof(char)];
  25481. int decodedSz = 0;
  25482. #ifndef NO_FILESYSTEM
  25483. XFILE certFile = XBADFILE;
  25484. XFILE keyFile = XBADFILE;
  25485. #endif
  25486. #ifdef ECC_TIMING_RESISTANT
  25487. XMEMSET(&rng, 0, sizeof(WC_RNG));
  25488. #endif
  25489. #if !defined(NO_RSA) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25490. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25491. /* RSA certs and keys. */
  25492. #if defined(USE_CERT_BUFFERS_1024)
  25493. rsaCertSz = (word32)sizeof_client_cert_der_1024;
  25494. /* Allocate buffer space. */
  25495. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  25496. DYNAMIC_TYPE_TMP_BUFFER));
  25497. /* Init buffer. */
  25498. if (rsaCert != NULL) {
  25499. XMEMCPY(rsaCert, client_cert_der_1024, rsaCertSz);
  25500. }
  25501. rsaPrivKeySz = (word32)sizeof_client_key_der_1024;
  25502. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  25503. DYNAMIC_TYPE_TMP_BUFFER));
  25504. if (rsaPrivKey != NULL) {
  25505. XMEMCPY(rsaPrivKey, client_key_der_1024, rsaPrivKeySz);
  25506. }
  25507. #elif defined(USE_CERT_BUFFERS_2048)
  25508. rsaCertSz = (word32)sizeof_client_cert_der_2048;
  25509. /* Allocate buffer */
  25510. ExpectNotNull(rsaCert = (byte*)XMALLOC(rsaCertSz, HEAP_HINT,
  25511. DYNAMIC_TYPE_TMP_BUFFER));
  25512. /* Init buffer. */
  25513. if (rsaCert != NULL) {
  25514. XMEMCPY(rsaCert, client_cert_der_2048, rsaCertSz);
  25515. }
  25516. rsaPrivKeySz = (word32)sizeof_client_key_der_2048;
  25517. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(rsaPrivKeySz, HEAP_HINT,
  25518. DYNAMIC_TYPE_TMP_BUFFER));
  25519. if (rsaPrivKey != NULL) {
  25520. XMEMCPY(rsaPrivKey, client_key_der_2048, rsaPrivKeySz);
  25521. }
  25522. #else
  25523. /* File system. */
  25524. ExpectTrue((certFile = XFOPEN(rsaClientCert, "rb")) != XBADFILE);
  25525. rsaCertSz = (word32)FOURK_BUF;
  25526. ExpectNotNull(rsaCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25527. DYNAMIC_TYPE_TMP_BUFFER));
  25528. ExpectTrue((rsaCertSz = (word32)XFREAD(rsaCert, 1, rsaCertSz,
  25529. certFile)) > 0);
  25530. if (certFile != XBADFILE)
  25531. XFCLOSE(certFile);
  25532. ExpectTrue((keyFile = XFOPEN(rsaClientKey, "rb")) != XBADFILE);
  25533. ExpectNotNull(rsaPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25534. DYNAMIC_TYPE_TMP_BUFFER));
  25535. rsaPrivKeySz = (word32)FOURK_BUF;
  25536. ExpectTrue((rsaPrivKeySz = (word32)XFREAD(rsaPrivKey, 1, rsaPrivKeySz,
  25537. keyFile)) > 0);
  25538. if (keyFile != XBADFILE)
  25539. XFCLOSE(keyFile);
  25540. #endif /* USE_CERT_BUFFERS */
  25541. #endif /* NO_RSA */
  25542. /* ECC */
  25543. #if defined(HAVE_ECC) && (!defined(NO_AES) || (!defined(NO_SHA) ||\
  25544. !defined(NO_SHA256) || defined(WOLFSSL_SHA512)))
  25545. #ifdef USE_CERT_BUFFERS_256
  25546. ExpectNotNull(eccCert = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  25547. DYNAMIC_TYPE_TMP_BUFFER));
  25548. /* Init buffer. */
  25549. eccCertSz = (word32)sizeof_cliecc_cert_der_256;
  25550. if (eccCert != NULL) {
  25551. XMEMCPY(eccCert, cliecc_cert_der_256, eccCertSz);
  25552. }
  25553. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(TWOK_BUF, HEAP_HINT,
  25554. DYNAMIC_TYPE_TMP_BUFFER));
  25555. eccPrivKeySz = (word32)sizeof_ecc_clikey_der_256;
  25556. if (eccPrivKey != NULL) {
  25557. XMEMCPY(eccPrivKey, ecc_clikey_der_256, eccPrivKeySz);
  25558. }
  25559. #else /* File system. */
  25560. ExpectTrue((certFile = XFOPEN(eccClientCert, "rb")) != XBADFILE);
  25561. eccCertSz = (word32)FOURK_BUF;
  25562. ExpectNotNull(eccCert = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25563. DYNAMIC_TYPE_TMP_BUFFER));
  25564. ExpectTrue((eccCertSz = (word32)XFREAD(eccCert, 1, eccCertSz,
  25565. certFile)) > 0);
  25566. if (certFile != XBADFILE) {
  25567. XFCLOSE(certFile);
  25568. }
  25569. ExpectTrue((keyFile = XFOPEN(eccClientKey, "rb")) != XBADFILE);
  25570. eccPrivKeySz = (word32)FOURK_BUF;
  25571. ExpectNotNull(eccPrivKey = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25572. DYNAMIC_TYPE_TMP_BUFFER));
  25573. ExpectTrue((eccPrivKeySz = (word32)XFREAD(eccPrivKey, 1, eccPrivKeySz,
  25574. keyFile)) > 0);
  25575. if (keyFile != XBADFILE) {
  25576. XFCLOSE(keyFile);
  25577. }
  25578. #endif /* USE_CERT_BUFFERS_256 */
  25579. #endif /* END HAVE_ECC */
  25580. #ifndef NO_FILESYSTEM
  25581. /* Silence. */
  25582. (void)keyFile;
  25583. (void)certFile;
  25584. #endif
  25585. {
  25586. const pkcs7EnvelopedVector testVectors[] = {
  25587. /* DATA is a global variable defined in the makefile. */
  25588. #if !defined(NO_RSA)
  25589. #ifndef NO_DES3
  25590. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, DES3b, 0, 0,
  25591. rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25592. #endif /* NO_DES3 */
  25593. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25594. #ifndef NO_AES_128
  25595. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES128CBCb,
  25596. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25597. #endif
  25598. #ifndef NO_AES_192
  25599. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES192CBCb,
  25600. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25601. #endif
  25602. #ifndef NO_AES_256
  25603. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA, AES256CBCb,
  25604. 0, 0, rsaCert, rsaCertSz, rsaPrivKey, rsaPrivKeySz},
  25605. #endif
  25606. #endif /* NO_AES && HAVE_AES_CBC */
  25607. #endif /* NO_RSA */
  25608. #if defined(HAVE_ECC)
  25609. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25610. #if !defined(NO_SHA) && !defined(NO_AES_128)
  25611. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25612. AES128CBCb, AES128_WRAP, dhSinglePass_stdDH_sha1kdf_scheme,
  25613. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25614. #endif
  25615. #if !defined(NO_SHA256) && !defined(NO_AES_256)
  25616. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25617. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha256kdf_scheme,
  25618. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25619. #endif
  25620. #if defined(WOLFSSL_SHA512) && !defined(NO_AES_256)
  25621. {(byte*)input, (word32)(sizeof(input)/sizeof(char)), DATA,
  25622. AES256CBCb, AES256_WRAP, dhSinglePass_stdDH_sha512kdf_scheme,
  25623. eccCert, eccCertSz, eccPrivKey, eccPrivKeySz},
  25624. #endif
  25625. #endif /* NO_AES && HAVE_AES_CBC*/
  25626. #endif /* END HAVE_ECC */
  25627. }; /* END pkcs7EnvelopedVector */
  25628. #ifdef ECC_TIMING_RESISTANT
  25629. ExpectIntEQ(wc_InitRng(&rng), 0);
  25630. #endif
  25631. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25632. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25633. testSz = (int)sizeof(testVectors)/(int)sizeof(pkcs7EnvelopedVector);
  25634. for (i = 0; i < testSz; i++) {
  25635. #ifdef ASN_BER_TO_DER
  25636. encodeSignedDataStream strm;
  25637. /* test setting stream mode, the first one using IO callbacks */
  25638. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  25639. (word32)(testVectors + i)->certSz), 0);
  25640. if (pkcs7 != NULL) {
  25641. #ifdef ECC_TIMING_RESISTANT
  25642. pkcs7->rng = &rng;
  25643. #endif
  25644. if (i != 0)
  25645. pkcs7->content = (byte*)(testVectors + i)->content;
  25646. pkcs7->contentSz = (testVectors + i)->contentSz;
  25647. pkcs7->contentOID = (testVectors + i)->contentOID;
  25648. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  25649. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  25650. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  25651. pkcs7->privateKey = (testVectors + i)->privateKey;
  25652. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  25653. }
  25654. if (i == 0) {
  25655. XMEMSET(&strm, 0, sizeof(strm));
  25656. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, GetContentCB,
  25657. StreamOutputCB, (void*)&strm), 0);
  25658. encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL, 0);
  25659. }
  25660. else {
  25661. ExpectIntEQ(wc_PKCS7_SetStreamMode(pkcs7, 1, NULL, NULL, NULL), 0);
  25662. encodedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25663. (word32)sizeof(output));
  25664. }
  25665. switch ((testVectors + i)->encryptOID) {
  25666. #ifndef NO_DES3
  25667. case DES3b:
  25668. case DESb:
  25669. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25670. break;
  25671. #endif
  25672. #ifdef HAVE_AESCCM
  25673. #ifdef WOLFSSL_AES_128
  25674. case AES128CCMb:
  25675. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25676. break;
  25677. #endif
  25678. #ifdef WOLFSSL_AES_192
  25679. case AES192CCMb:
  25680. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25681. break;
  25682. #endif
  25683. #ifdef WOLFSSL_AES_256
  25684. case AES256CCMb:
  25685. ExpectIntEQ(encodedSz, BAD_FUNC_ARG);
  25686. break;
  25687. #endif
  25688. #endif
  25689. default:
  25690. ExpectIntGE(encodedSz, 0);
  25691. }
  25692. if (encodedSz > 0) {
  25693. if (i == 0) {
  25694. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7,
  25695. strm.out, (word32)encodedSz, decoded,
  25696. (word32)sizeof(decoded));
  25697. }
  25698. else {
  25699. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25700. (word32)encodedSz, decoded, (word32)sizeof(decoded));
  25701. }
  25702. ExpectIntGE(decodedSz, 0);
  25703. /* Verify the size of each buffer. */
  25704. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  25705. }
  25706. wc_PKCS7_Free(pkcs7);
  25707. pkcs7 = NULL;
  25708. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25709. #endif
  25710. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (testVectors + i)->cert,
  25711. (word32)(testVectors + i)->certSz), 0);
  25712. if (pkcs7 != NULL) {
  25713. #ifdef ECC_TIMING_RESISTANT
  25714. pkcs7->rng = &rng;
  25715. #endif
  25716. pkcs7->content = (byte*)(testVectors + i)->content;
  25717. pkcs7->contentSz = (testVectors + i)->contentSz;
  25718. pkcs7->contentOID = (testVectors + i)->contentOID;
  25719. pkcs7->encryptOID = (testVectors + i)->encryptOID;
  25720. pkcs7->keyWrapOID = (testVectors + i)->keyWrapOID;
  25721. pkcs7->keyAgreeOID = (testVectors + i)->keyAgreeOID;
  25722. pkcs7->privateKey = (testVectors + i)->privateKey;
  25723. pkcs7->privateKeySz = (testVectors + i)->privateKeySz;
  25724. }
  25725. #ifdef ASN_BER_TO_DER
  25726. /* test without setting stream mode */
  25727. ExpectIntEQ(wc_PKCS7_GetStreamMode(pkcs7), 0);
  25728. #endif
  25729. ExpectIntGE(wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25730. (word32)sizeof(output)), 0);
  25731. decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25732. (word32)sizeof(output), decoded, (word32)sizeof(decoded));
  25733. ExpectIntGE(decodedSz, 0);
  25734. /* Verify the size of each buffer. */
  25735. ExpectIntEQ((word32)sizeof(input)/sizeof(char), decodedSz);
  25736. /* Don't free the last time through the loop. */
  25737. if (i < testSz - 1) {
  25738. wc_PKCS7_Free(pkcs7);
  25739. pkcs7 = NULL;
  25740. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25741. }
  25742. } /* END test loop. */
  25743. }
  25744. /* Test bad args. */
  25745. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(NULL, output,
  25746. (word32)sizeof(output)), BAD_FUNC_ARG);
  25747. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, NULL,
  25748. (word32)sizeof(output)), BAD_FUNC_ARG);
  25749. ExpectIntEQ(wc_PKCS7_EncodeEnvelopedData(pkcs7, output, 0), BAD_FUNC_ARG);
  25750. /* Decode. */
  25751. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(NULL, output,
  25752. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25753. BAD_FUNC_ARG);
  25754. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25755. (word32)sizeof(output), NULL, (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25756. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25757. (word32)sizeof(output), decoded, 0), BAD_FUNC_ARG);
  25758. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, NULL,
  25759. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25760. BAD_FUNC_ARG);
  25761. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output, 0, decoded,
  25762. (word32)sizeof(decoded)), BAD_FUNC_ARG);
  25763. /* Should get a return of BAD_FUNC_ARG with structure data. Order matters.*/
  25764. #if defined(HAVE_ECC) && !defined(NO_AES) && defined(HAVE_AES_CBC)
  25765. /* only a failure for KARI test cases */
  25766. if (pkcs7 != NULL) {
  25767. tempWrd32 = pkcs7->singleCertSz;
  25768. pkcs7->singleCertSz = 0;
  25769. }
  25770. #if defined(WOLFSSL_ASN_TEMPLATE)
  25771. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25772. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25773. BUFFER_E);
  25774. #else
  25775. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25776. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25777. ASN_PARSE_E);
  25778. #endif
  25779. if (pkcs7 != NULL) {
  25780. pkcs7->singleCertSz = tempWrd32;
  25781. tmpBytePtr = pkcs7->singleCert;
  25782. pkcs7->singleCert = NULL;
  25783. }
  25784. #ifndef NO_RSA
  25785. #if defined(NO_PKCS7_STREAM)
  25786. /* when none streaming mode is used and PKCS7 is in bad state buffer error
  25787. * is returned from kari parse which gets set to bad func arg */
  25788. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25789. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25790. BAD_FUNC_ARG);
  25791. #else
  25792. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25793. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25794. ASN_PARSE_E);
  25795. #endif
  25796. #endif /* !NO_RSA */
  25797. if (pkcs7 != NULL) {
  25798. pkcs7->singleCert = tmpBytePtr;
  25799. }
  25800. #endif
  25801. if (pkcs7 != NULL) {
  25802. tempWrd32 = pkcs7->privateKeySz;
  25803. pkcs7->privateKeySz = 0;
  25804. }
  25805. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25806. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25807. BAD_FUNC_ARG);
  25808. if (pkcs7 != NULL) {
  25809. pkcs7->privateKeySz = tempWrd32;
  25810. tmpBytePtr = pkcs7->privateKey;
  25811. pkcs7->privateKey = NULL;
  25812. }
  25813. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25814. (word32)sizeof(output), decoded, (word32)sizeof(decoded)),
  25815. BAD_FUNC_ARG);
  25816. if (pkcs7 != NULL) {
  25817. pkcs7->privateKey = tmpBytePtr;
  25818. }
  25819. wc_PKCS7_Free(pkcs7);
  25820. pkcs7 = NULL;
  25821. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && !defined(NO_AES_256)
  25822. /* test of decrypt callback with KEKRI enveloped data */
  25823. {
  25824. int envelopedSz = 0;
  25825. const byte keyId[] = { 0x00 };
  25826. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25827. if (pkcs7 != NULL) {
  25828. pkcs7->content = (byte*)input;
  25829. pkcs7->contentSz = (word32)(sizeof(input)/sizeof(char));
  25830. pkcs7->contentOID = DATA;
  25831. pkcs7->encryptOID = AES256CBCb;
  25832. }
  25833. ExpectIntGT(wc_PKCS7_AddRecipient_KEKRI(pkcs7, AES256_WRAP,
  25834. (byte*)defKey, sizeof(defKey), (byte*)keyId,
  25835. sizeof(keyId), NULL, NULL, 0, NULL, 0, 0), 0);
  25836. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, CMS_SKID), 0);
  25837. ExpectIntGT((envelopedSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, output,
  25838. (word32)sizeof(output))), 0);
  25839. wc_PKCS7_Free(pkcs7);
  25840. pkcs7 = NULL;
  25841. /* decode envelopedData */
  25842. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25843. ExpectIntEQ(wc_PKCS7_SetWrapCEKCb(pkcs7, myCEKwrapFunc), 0);
  25844. ExpectIntEQ(wc_PKCS7_SetDecodeEncryptedCb(pkcs7, myDecryptionFunc), 0);
  25845. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(pkcs7, output,
  25846. (word32)envelopedSz, decoded, sizeof(decoded))), 0);
  25847. wc_PKCS7_Free(pkcs7);
  25848. pkcs7 = NULL;
  25849. }
  25850. #endif /* !NO_AES && !NO_AES_256 */
  25851. #ifndef NO_RSA
  25852. XFREE(rsaCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25853. XFREE(rsaPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25854. #endif /* NO_RSA */
  25855. #ifdef HAVE_ECC
  25856. XFREE(eccCert, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25857. XFREE(eccPrivKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25858. #endif /* HAVE_ECC */
  25859. #ifdef ECC_TIMING_RESISTANT
  25860. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  25861. #endif
  25862. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DES3) && \
  25863. !defined(NO_RSA) && !defined(NO_SHA)
  25864. {
  25865. byte out[7];
  25866. byte *cms = NULL;
  25867. word32 cmsSz;
  25868. XFILE cmsFile = XBADFILE;
  25869. XMEMSET(out, 0, sizeof(out));
  25870. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25871. ExpectTrue((cmsFile = XFOPEN("./certs/test/ktri-keyid-cms.msg", "rb"))
  25872. != XBADFILE);
  25873. cmsSz = (word32)FOURK_BUF;
  25874. ExpectNotNull(cms = (byte*)XMALLOC(FOURK_BUF, HEAP_HINT,
  25875. DYNAMIC_TYPE_TMP_BUFFER));
  25876. ExpectTrue((cmsSz = (word32)XFREAD(cms, 1, cmsSz, cmsFile)) > 0);
  25877. if (cmsFile != XBADFILE)
  25878. XFCLOSE(cmsFile);
  25879. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)client_cert_der_2048,
  25880. sizeof_client_cert_der_2048), 0);
  25881. if (pkcs7 != NULL) {
  25882. pkcs7->privateKey = (byte*)client_key_der_2048;
  25883. pkcs7->privateKeySz = sizeof_client_key_der_2048;
  25884. }
  25885. ExpectIntLT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  25886. 2), 0);
  25887. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, cms, cmsSz, out,
  25888. sizeof(out)), 0);
  25889. XFREE(cms, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  25890. ExpectIntEQ(XMEMCMP(out, "test", 4), 0);
  25891. wc_PKCS7_Free(pkcs7);
  25892. pkcs7 = NULL;
  25893. }
  25894. #endif /* USE_CERT_BUFFERS_2048 && !NO_DES3 && !NO_RSA && !NO_SHA */
  25895. #endif /* HAVE_PKCS7 */
  25896. return EXPECT_RESULT();
  25897. } /* END test_wc_PKCS7_EncodeDecodeEnvelopedData() */
  25898. /*
  25899. * Testing wc_PKCS7_EncodeEncryptedData()
  25900. */
  25901. static int test_wc_PKCS7_EncodeEncryptedData(void)
  25902. {
  25903. EXPECT_DECLS;
  25904. #if defined(HAVE_PKCS7) && !defined(NO_PKCS7_ENCRYPTED_DATA)
  25905. PKCS7* pkcs7 = NULL;
  25906. byte* tmpBytePtr = NULL;
  25907. byte encrypted[TWOK_BUF];
  25908. byte decoded[TWOK_BUF];
  25909. word32 tmpWrd32 = 0;
  25910. int tmpInt = 0;
  25911. int decodedSz = 0;
  25912. int encryptedSz = 0;
  25913. int testSz = 0;
  25914. int i = 0;
  25915. const byte data[] = { /* Hello World */
  25916. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  25917. 0x72,0x6c,0x64
  25918. };
  25919. #ifndef NO_DES3
  25920. byte desKey[] = {
  25921. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef
  25922. };
  25923. byte des3Key[] = {
  25924. 0x01,0x23,0x45,0x67,0x89,0xab,0xcd,0xef,
  25925. 0xfe,0xde,0xba,0x98,0x76,0x54,0x32,0x10,
  25926. 0x89,0xab,0xcd,0xef,0x01,0x23,0x45,0x67
  25927. };
  25928. #endif
  25929. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25930. #ifndef NO_AES_128
  25931. byte aes128Key[] = {
  25932. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25933. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25934. };
  25935. #endif
  25936. #ifndef NO_AES_192
  25937. byte aes192Key[] = {
  25938. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25939. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25940. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25941. };
  25942. #endif
  25943. #ifndef NO_AES_256
  25944. byte aes256Key[] = {
  25945. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25946. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25947. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,
  25948. 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08
  25949. };
  25950. #endif
  25951. #endif /* !NO_AES && HAVE_AES_CBC */
  25952. const pkcs7EncryptedVector testVectors[] =
  25953. {
  25954. #ifndef NO_DES3
  25955. {data, (word32)sizeof(data), DATA, DES3b, des3Key, sizeof(des3Key)},
  25956. {data, (word32)sizeof(data), DATA, DESb, desKey, sizeof(desKey)},
  25957. #endif /* !NO_DES3 */
  25958. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  25959. #ifndef NO_AES_128
  25960. {data, (word32)sizeof(data), DATA, AES128CBCb, aes128Key,
  25961. sizeof(aes128Key)},
  25962. #endif
  25963. #ifndef NO_AES_192
  25964. {data, (word32)sizeof(data), DATA, AES192CBCb, aes192Key,
  25965. sizeof(aes192Key)},
  25966. #endif
  25967. #ifndef NO_AES_256
  25968. {data, (word32)sizeof(data), DATA, AES256CBCb, aes256Key,
  25969. sizeof(aes256Key)},
  25970. #endif
  25971. #endif /* !NO_AES && HAVE_AES_CBC */
  25972. };
  25973. testSz = sizeof(testVectors) / sizeof(pkcs7EncryptedVector);
  25974. for (i = 0; i < testSz; i++) {
  25975. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  25976. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  25977. if (pkcs7 != NULL) {
  25978. pkcs7->content = (byte*)testVectors[i].content;
  25979. pkcs7->contentSz = testVectors[i].contentSz;
  25980. pkcs7->contentOID = testVectors[i].contentOID;
  25981. pkcs7->encryptOID = testVectors[i].encryptOID;
  25982. pkcs7->encryptionKey = testVectors[i].encryptionKey;
  25983. pkcs7->encryptionKeySz = testVectors[i].encryptionKeySz;
  25984. pkcs7->heap = HEAP_HINT;
  25985. }
  25986. /* encode encryptedData */
  25987. ExpectIntGT(encryptedSz = wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  25988. sizeof(encrypted)), 0);
  25989. /* Decode encryptedData */
  25990. ExpectIntGT(decodedSz = wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted,
  25991. (word32)encryptedSz, decoded, sizeof(decoded)), 0);
  25992. ExpectIntEQ(XMEMCMP(decoded, data, decodedSz), 0);
  25993. /* Keep values for last itr. */
  25994. if (i < testSz - 1) {
  25995. wc_PKCS7_Free(pkcs7);
  25996. pkcs7 = NULL;
  25997. }
  25998. }
  25999. if (pkcs7 == NULL || testSz == 0) {
  26000. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26001. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26002. }
  26003. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(NULL, encrypted,
  26004. sizeof(encrypted)),BAD_FUNC_ARG);
  26005. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, NULL,
  26006. sizeof(encrypted)), BAD_FUNC_ARG);
  26007. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26008. 0), BAD_FUNC_ARG);
  26009. /* Testing the struct. */
  26010. if (pkcs7 != NULL) {
  26011. tmpBytePtr = pkcs7->content;
  26012. pkcs7->content = NULL;
  26013. }
  26014. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26015. sizeof(encrypted)), BAD_FUNC_ARG);
  26016. if (pkcs7 != NULL) {
  26017. pkcs7->content = tmpBytePtr;
  26018. tmpWrd32 = pkcs7->contentSz;
  26019. pkcs7->contentSz = 0;
  26020. }
  26021. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26022. sizeof(encrypted)), BAD_FUNC_ARG);
  26023. if (pkcs7 != NULL) {
  26024. pkcs7->contentSz = tmpWrd32;
  26025. tmpInt = pkcs7->encryptOID;
  26026. pkcs7->encryptOID = 0;
  26027. }
  26028. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26029. sizeof(encrypted)), BAD_FUNC_ARG);
  26030. if (pkcs7 != NULL) {
  26031. pkcs7->encryptOID = tmpInt;
  26032. tmpBytePtr = pkcs7->encryptionKey;
  26033. pkcs7->encryptionKey = NULL;
  26034. }
  26035. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26036. sizeof(encrypted)), BAD_FUNC_ARG);
  26037. if (pkcs7 != NULL) {
  26038. pkcs7->encryptionKey = tmpBytePtr;
  26039. tmpWrd32 = pkcs7->encryptionKeySz;
  26040. pkcs7->encryptionKeySz = 0;
  26041. }
  26042. ExpectIntEQ(wc_PKCS7_EncodeEncryptedData(pkcs7, encrypted,
  26043. sizeof(encrypted)), BAD_FUNC_ARG);
  26044. if (pkcs7 != NULL) {
  26045. pkcs7->encryptionKeySz = tmpWrd32;
  26046. }
  26047. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(NULL, encrypted, (word32)encryptedSz,
  26048. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26049. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, NULL, (word32)encryptedSz,
  26050. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26051. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, 0,
  26052. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26053. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  26054. NULL, sizeof(decoded)), BAD_FUNC_ARG);
  26055. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  26056. decoded, 0), BAD_FUNC_ARG);
  26057. /* Test struct fields */
  26058. if (pkcs7 != NULL) {
  26059. tmpBytePtr = pkcs7->encryptionKey;
  26060. pkcs7->encryptionKey = NULL;
  26061. }
  26062. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  26063. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26064. if (pkcs7 != NULL) {
  26065. pkcs7->encryptionKey = tmpBytePtr;
  26066. pkcs7->encryptionKeySz = 0;
  26067. }
  26068. ExpectIntEQ(wc_PKCS7_DecodeEncryptedData(pkcs7, encrypted, (word32)encryptedSz,
  26069. decoded, sizeof(decoded)), BAD_FUNC_ARG);
  26070. wc_PKCS7_Free(pkcs7);
  26071. #endif
  26072. return EXPECT_RESULT();
  26073. } /* END test_wc_PKCS7_EncodeEncryptedData() */
  26074. /*
  26075. * Testing wc_PKCS7_Degenerate()
  26076. */
  26077. static int test_wc_PKCS7_Degenerate(void)
  26078. {
  26079. EXPECT_DECLS;
  26080. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  26081. PKCS7* pkcs7 = NULL;
  26082. char fName[] = "./certs/test-degenerate.p7b";
  26083. XFILE f = XBADFILE;
  26084. byte der[4096];
  26085. word32 derSz = 0;
  26086. #ifndef NO_PKCS7_STREAM
  26087. word32 z;
  26088. int ret;
  26089. #endif /* !NO_PKCS7_STREAM */
  26090. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  26091. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  26092. if (f != XBADFILE)
  26093. XFCLOSE(f);
  26094. /* test degenerate success */
  26095. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26096. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26097. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26098. #ifndef NO_RSA
  26099. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26100. #ifndef NO_PKCS7_STREAM
  26101. wc_PKCS7_Free(pkcs7);
  26102. pkcs7 = NULL;
  26103. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26104. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26105. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26106. /* test for streaming */
  26107. ret = -1;
  26108. for (z = 0; z < derSz && ret != 0; z++) {
  26109. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  26110. if (ret < 0){
  26111. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26112. }
  26113. }
  26114. ExpectIntEQ(ret, 0);
  26115. #endif /* !NO_PKCS7_STREAM */
  26116. #else
  26117. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26118. #endif /* NO_RSA */
  26119. wc_PKCS7_Free(pkcs7);
  26120. pkcs7 = NULL;
  26121. /* test with turning off degenerate cases */
  26122. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26123. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26124. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26125. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  26126. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz),
  26127. PKCS7_NO_SIGNER_E);
  26128. #ifndef NO_PKCS7_STREAM
  26129. wc_PKCS7_Free(pkcs7);
  26130. pkcs7 = NULL;
  26131. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26132. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26133. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26134. wc_PKCS7_AllowDegenerate(pkcs7, 0); /* override allowing degenerate case */
  26135. /* test for streaming */
  26136. ret = -1;
  26137. for (z = 0; z < derSz && ret != 0; z++) {
  26138. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  26139. if (ret == WC_PKCS7_WANT_READ_E){
  26140. continue;
  26141. }
  26142. else
  26143. break;
  26144. }
  26145. ExpectIntEQ(ret, PKCS7_NO_SIGNER_E);
  26146. #endif /* !NO_PKCS7_STREAM */
  26147. wc_PKCS7_Free(pkcs7);
  26148. #endif
  26149. return EXPECT_RESULT();
  26150. } /* END test_wc_PKCS7_Degenerate() */
  26151. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  26152. defined(ASN_BER_TO_DER) && !defined(NO_DES3) && !defined(NO_SHA)
  26153. static byte berContent[] = {
  26154. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  26155. 0xF7, 0x0D, 0x01, 0x07, 0x03, 0xA0, 0x80, 0x30,
  26156. 0x80, 0x02, 0x01, 0x00, 0x31, 0x82, 0x01, 0x48,
  26157. 0x30, 0x82, 0x01, 0x44, 0x02, 0x01, 0x00, 0x30,
  26158. 0x81, 0xAC, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30,
  26159. 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02,
  26160. 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  26161. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E,
  26162. 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E,
  26163. 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  26164. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15,
  26165. 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C,
  26166. 0x0C, 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C,
  26167. 0x5F, 0x31, 0x30, 0x32, 0x34, 0x31, 0x19, 0x30,
  26168. 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10,
  26169. 0x50, 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D,
  26170. 0x69, 0x6E, 0x67, 0x2D, 0x31, 0x30, 0x32, 0x34,
  26171. 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04,
  26172. 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77,
  26173. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  26174. 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09,
  26175. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  26176. 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40,
  26177. 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E,
  26178. 0x63, 0x6F, 0x6D, 0x02, 0x09, 0x00, 0xBB, 0xD3,
  26179. 0x10, 0x03, 0xE6, 0x9D, 0x28, 0x03, 0x30, 0x0D,
  26180. 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  26181. 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x81, 0x80,
  26182. 0x2F, 0xF9, 0x77, 0x4F, 0x04, 0x5C, 0x16, 0x62,
  26183. 0xF0, 0x77, 0x8D, 0x95, 0x4C, 0xB1, 0x44, 0x9A,
  26184. 0x8C, 0x3C, 0x8C, 0xE4, 0xD1, 0xC1, 0x14, 0x72,
  26185. 0xD0, 0x4A, 0x1A, 0x94, 0x27, 0x0F, 0xAA, 0xE8,
  26186. 0xD0, 0xA2, 0xE7, 0xED, 0x4C, 0x7F, 0x0F, 0xC7,
  26187. 0x1B, 0xFB, 0x81, 0x0E, 0x76, 0x8F, 0xDD, 0x32,
  26188. 0x11, 0x68, 0xA0, 0x13, 0xD2, 0x8D, 0x95, 0xEF,
  26189. 0x80, 0x53, 0x81, 0x0E, 0x1F, 0xC8, 0xD6, 0x76,
  26190. 0x5C, 0x31, 0xD3, 0x77, 0x33, 0x29, 0xA6, 0x1A,
  26191. 0xD3, 0xC6, 0x14, 0x36, 0xCA, 0x8E, 0x7D, 0x72,
  26192. 0xA0, 0x29, 0x4C, 0xC7, 0x3A, 0xAF, 0xFE, 0xF7,
  26193. 0xFC, 0xD7, 0xE2, 0x8F, 0x6A, 0x20, 0x46, 0x09,
  26194. 0x40, 0x22, 0x2D, 0x79, 0x38, 0x11, 0xB1, 0x4A,
  26195. 0xE3, 0x48, 0xE8, 0x10, 0x37, 0xA0, 0x22, 0xF7,
  26196. 0xB4, 0x79, 0xD1, 0xA9, 0x3D, 0xC2, 0xAB, 0x37,
  26197. 0xAE, 0x82, 0x68, 0x1A, 0x16, 0xEF, 0x33, 0x0C,
  26198. 0x30, 0x80, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86,
  26199. 0xF7, 0x0D, 0x01, 0x07, 0x01, 0x30, 0x14, 0x06,
  26200. 0x08, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x03,
  26201. 0x07, 0x04, 0x08, 0xAD, 0xD0, 0x38, 0x9B, 0x16,
  26202. 0x4B, 0x7F, 0x99, 0xA0, 0x80, 0x04, 0x82, 0x03,
  26203. 0xE8, 0x6D, 0x48, 0xFB, 0x8A, 0xBD, 0xED, 0x6C,
  26204. 0xCD, 0xC6, 0x48, 0xFD, 0xB7, 0xB0, 0x7C, 0x86,
  26205. 0x2C, 0x8D, 0xF0, 0x23, 0x12, 0xD8, 0xA3, 0x2A,
  26206. 0x21, 0x6F, 0x8B, 0x75, 0xBB, 0x47, 0x7F, 0xC9,
  26207. 0xBA, 0xBA, 0xFF, 0x91, 0x09, 0x01, 0x7A, 0x5C,
  26208. 0x96, 0x02, 0xB8, 0x8E, 0xF8, 0x67, 0x7E, 0x8F,
  26209. 0xF9, 0x51, 0x0E, 0xFF, 0x8E, 0xE2, 0x61, 0xC0,
  26210. 0xDF, 0xFA, 0xE2, 0x4C, 0x50, 0x90, 0xAE, 0xA1,
  26211. 0x15, 0x38, 0x3D, 0xBE, 0x88, 0xD7, 0x57, 0xC0,
  26212. 0x11, 0x44, 0xA2, 0x61, 0x05, 0x49, 0x6A, 0x94,
  26213. 0x04, 0x10, 0xD9, 0xC2, 0x2D, 0x15, 0x20, 0x0D,
  26214. 0xBD, 0xA2, 0xEF, 0xE4, 0x68, 0xFA, 0x39, 0x75,
  26215. 0x7E, 0xD8, 0x64, 0x44, 0xCB, 0xE0, 0x00, 0x6D,
  26216. 0x57, 0x4E, 0x8A, 0x17, 0xA9, 0x83, 0x6C, 0x7F,
  26217. 0xFE, 0x01, 0xEE, 0xDE, 0x99, 0x3A, 0xB2, 0xFF,
  26218. 0xD3, 0x72, 0x78, 0xBA, 0xF1, 0x23, 0x54, 0x48,
  26219. 0x02, 0xD8, 0x38, 0xA9, 0x54, 0xE5, 0x4A, 0x81,
  26220. 0xB9, 0xC0, 0x67, 0xB2, 0x7D, 0x3C, 0x6F, 0xCE,
  26221. 0xA4, 0xDD, 0x34, 0x5F, 0x60, 0xB1, 0xA3, 0x7A,
  26222. 0xE4, 0x43, 0xF2, 0x89, 0x64, 0x35, 0x09, 0x32,
  26223. 0x51, 0xFB, 0x5C, 0x67, 0x0C, 0x3B, 0xFC, 0x36,
  26224. 0x6B, 0x37, 0x43, 0x6C, 0x03, 0xCD, 0x44, 0xC7,
  26225. 0x2B, 0x62, 0xD6, 0xD1, 0xF4, 0x07, 0x7B, 0x19,
  26226. 0x91, 0xF0, 0xD7, 0xF5, 0x54, 0xBC, 0x0F, 0x42,
  26227. 0x6B, 0x69, 0xF7, 0xA3, 0xC8, 0xEE, 0xB9, 0x7A,
  26228. 0x9E, 0x3D, 0xDF, 0x53, 0x47, 0xF7, 0x50, 0x67,
  26229. 0x00, 0xCF, 0x2B, 0x3B, 0xE9, 0x85, 0xEE, 0xBD,
  26230. 0x4C, 0x64, 0x66, 0x0B, 0x77, 0x80, 0x9D, 0xEF,
  26231. 0x11, 0x32, 0x77, 0xA8, 0xA4, 0x5F, 0xEE, 0x2D,
  26232. 0xE0, 0x43, 0x87, 0x76, 0x87, 0x53, 0x4E, 0xD7,
  26233. 0x1A, 0x04, 0x7B, 0xE1, 0xD1, 0xE1, 0xF5, 0x87,
  26234. 0x51, 0x13, 0xE0, 0xC2, 0xAA, 0xA3, 0x4B, 0xAA,
  26235. 0x9E, 0xB4, 0xA6, 0x1D, 0x4E, 0x28, 0x57, 0x0B,
  26236. 0x80, 0x90, 0x81, 0x4E, 0x04, 0xF5, 0x30, 0x8D,
  26237. 0x51, 0xCE, 0x57, 0x2F, 0x88, 0xC5, 0x70, 0xC4,
  26238. 0x06, 0x8F, 0xDD, 0x37, 0xC1, 0x34, 0x1E, 0x0E,
  26239. 0x15, 0x32, 0x23, 0x92, 0xAB, 0x40, 0xEA, 0xF7,
  26240. 0x43, 0xE2, 0x1D, 0xE2, 0x4B, 0xC9, 0x91, 0xF4,
  26241. 0x63, 0x21, 0x34, 0xDB, 0xE9, 0x86, 0x83, 0x1A,
  26242. 0xD2, 0x52, 0xEF, 0x7A, 0xA2, 0xEE, 0xA4, 0x11,
  26243. 0x56, 0xD3, 0x6C, 0xF5, 0x6D, 0xE4, 0xA5, 0x2D,
  26244. 0x99, 0x02, 0x10, 0xDF, 0x29, 0xC5, 0xE3, 0x0B,
  26245. 0xC4, 0xA1, 0xEE, 0x5F, 0x4A, 0x10, 0xEE, 0x85,
  26246. 0x73, 0x2A, 0x92, 0x15, 0x2C, 0xC8, 0xF4, 0x8C,
  26247. 0xD7, 0x3D, 0xBC, 0xAD, 0x18, 0xE0, 0x59, 0xD3,
  26248. 0xEE, 0x75, 0x90, 0x1C, 0xCC, 0x76, 0xC6, 0x64,
  26249. 0x17, 0xD2, 0xD0, 0x91, 0xA6, 0xD0, 0xC1, 0x4A,
  26250. 0xAA, 0x58, 0x22, 0xEC, 0x45, 0x98, 0xF2, 0xCC,
  26251. 0x4C, 0xE4, 0xBF, 0xED, 0xF6, 0x44, 0x72, 0x36,
  26252. 0x65, 0x3F, 0xE3, 0xB5, 0x8B, 0x3E, 0x54, 0x9C,
  26253. 0x82, 0x86, 0x5E, 0xB0, 0xF2, 0x12, 0xE5, 0x69,
  26254. 0xFA, 0x46, 0xA2, 0x54, 0xFC, 0xF5, 0x4B, 0xE0,
  26255. 0x24, 0x3B, 0x99, 0x04, 0x1A, 0x7A, 0xF7, 0xD1,
  26256. 0xFF, 0x68, 0x97, 0xB2, 0x85, 0x82, 0x95, 0x27,
  26257. 0x2B, 0xF4, 0xE7, 0x1A, 0x74, 0x19, 0xEC, 0x8C,
  26258. 0x4E, 0xA7, 0x0F, 0xAD, 0x4F, 0x5A, 0x02, 0x80,
  26259. 0xC1, 0x6A, 0x9E, 0x54, 0xE4, 0x8E, 0xA3, 0x41,
  26260. 0x3F, 0x6F, 0x9C, 0x82, 0x9F, 0x83, 0xB0, 0x44,
  26261. 0x01, 0x5F, 0x10, 0x9D, 0xD3, 0xB6, 0x33, 0x5B,
  26262. 0xAF, 0xAC, 0x6B, 0x57, 0x2A, 0x01, 0xED, 0x0E,
  26263. 0x17, 0xB9, 0x80, 0x76, 0x12, 0x1C, 0x51, 0x56,
  26264. 0xDD, 0x6D, 0x94, 0xAB, 0xD2, 0xE5, 0x15, 0x2D,
  26265. 0x3C, 0xC5, 0xE8, 0x62, 0x05, 0x8B, 0x40, 0xB1,
  26266. 0xC2, 0x83, 0xCA, 0xAC, 0x4B, 0x8B, 0x39, 0xF7,
  26267. 0xA0, 0x08, 0x43, 0x5C, 0xF7, 0xE8, 0xED, 0x40,
  26268. 0x72, 0x73, 0xE3, 0x6B, 0x18, 0x67, 0xA0, 0xB6,
  26269. 0x0F, 0xED, 0x8F, 0x9A, 0xE4, 0x27, 0x62, 0x23,
  26270. 0xAA, 0x6D, 0x6C, 0x31, 0xC9, 0x9D, 0x6B, 0xE0,
  26271. 0xBF, 0x9D, 0x7D, 0x2E, 0x76, 0x71, 0x06, 0x39,
  26272. 0xAC, 0x96, 0x1C, 0xAF, 0x30, 0xF2, 0x62, 0x9C,
  26273. 0x84, 0x3F, 0x43, 0x5E, 0x19, 0xA8, 0xE5, 0x3C,
  26274. 0x9D, 0x43, 0x3C, 0x43, 0x41, 0xE8, 0x82, 0xE7,
  26275. 0x5B, 0xF3, 0xE2, 0x15, 0xE3, 0x52, 0x20, 0xFD,
  26276. 0x0D, 0xB2, 0x4D, 0x48, 0xAD, 0x53, 0x7E, 0x0C,
  26277. 0xF0, 0xB9, 0xBE, 0xC9, 0x58, 0x4B, 0xC8, 0xA8,
  26278. 0xA3, 0x36, 0xF1, 0x2C, 0xD2, 0xE1, 0xC8, 0xC4,
  26279. 0x3C, 0x48, 0x70, 0xC2, 0x6D, 0x6C, 0x3D, 0x99,
  26280. 0xAC, 0x43, 0x19, 0x69, 0xCA, 0x67, 0x1A, 0xC9,
  26281. 0xE1, 0x47, 0xFA, 0x0A, 0xE6, 0x5B, 0x6F, 0x61,
  26282. 0xD0, 0x03, 0xE4, 0x03, 0x4B, 0xFD, 0xE2, 0xA5,
  26283. 0x8D, 0x83, 0x01, 0x7E, 0xC0, 0x7B, 0x2E, 0x0B,
  26284. 0x29, 0xDD, 0xD6, 0xDC, 0x71, 0x46, 0xBD, 0x9A,
  26285. 0x40, 0x46, 0x1E, 0x0A, 0xB1, 0x00, 0xE7, 0x71,
  26286. 0x29, 0x77, 0xFC, 0x9A, 0x76, 0x8A, 0x5F, 0x66,
  26287. 0x9B, 0x63, 0x91, 0x12, 0x78, 0xBF, 0x67, 0xAD,
  26288. 0xA1, 0x72, 0x9E, 0xC5, 0x3E, 0xE5, 0xCB, 0xAF,
  26289. 0xD6, 0x5A, 0x0D, 0xB6, 0x9B, 0xA3, 0x78, 0xE8,
  26290. 0xB0, 0x8F, 0x69, 0xED, 0xC1, 0x73, 0xD5, 0xE5,
  26291. 0x1C, 0x18, 0xA0, 0x58, 0x4C, 0x49, 0xBD, 0x91,
  26292. 0xCE, 0x15, 0x0D, 0xAA, 0x5A, 0x07, 0xEA, 0x1C,
  26293. 0xA7, 0x4B, 0x11, 0x31, 0x80, 0xAF, 0xA1, 0x0A,
  26294. 0xED, 0x6C, 0x70, 0xE4, 0xDB, 0x75, 0x86, 0xAE,
  26295. 0xBF, 0x4A, 0x05, 0x72, 0xDE, 0x84, 0x8C, 0x7B,
  26296. 0x59, 0x81, 0x58, 0xE0, 0xC0, 0x15, 0xB5, 0xF3,
  26297. 0xD5, 0x73, 0x78, 0x83, 0x53, 0xDA, 0x92, 0xC1,
  26298. 0xE6, 0x71, 0x74, 0xC7, 0x7E, 0xAA, 0x36, 0x06,
  26299. 0xF0, 0xDF, 0xBA, 0xFB, 0xEF, 0x54, 0xE8, 0x11,
  26300. 0xB2, 0x33, 0xA3, 0x0B, 0x9E, 0x0C, 0x59, 0x75,
  26301. 0x13, 0xFA, 0x7F, 0x88, 0xB9, 0x86, 0xBD, 0x1A,
  26302. 0xDB, 0x52, 0x12, 0xFB, 0x6D, 0x1A, 0xCB, 0x49,
  26303. 0x94, 0x94, 0xC4, 0xA9, 0x99, 0xC0, 0xA4, 0xB6,
  26304. 0x60, 0x36, 0x09, 0x94, 0x2A, 0xD5, 0xC4, 0x26,
  26305. 0xF4, 0xA3, 0x6A, 0x0E, 0x57, 0x8B, 0x7C, 0xA4,
  26306. 0x1D, 0x75, 0xE8, 0x2A, 0xF3, 0xC4, 0x3C, 0x7D,
  26307. 0x45, 0x6D, 0xD8, 0x24, 0xD1, 0x3B, 0xF7, 0xCF,
  26308. 0xE4, 0x45, 0x2A, 0x55, 0xE5, 0xA9, 0x1F, 0x1C,
  26309. 0x8F, 0x55, 0x8D, 0xC1, 0xF7, 0x74, 0xCC, 0x26,
  26310. 0xC7, 0xBA, 0x2E, 0x5C, 0xC1, 0x71, 0x0A, 0xAA,
  26311. 0xD9, 0x6D, 0x76, 0xA7, 0xF9, 0xD1, 0x18, 0xCB,
  26312. 0x5A, 0x52, 0x98, 0xA8, 0x0D, 0x3F, 0x06, 0xFC,
  26313. 0x49, 0x11, 0x21, 0x5F, 0x86, 0x19, 0x33, 0x81,
  26314. 0xB5, 0x7A, 0xDA, 0xA1, 0x47, 0xBF, 0x7C, 0xD7,
  26315. 0x05, 0x96, 0xC7, 0xF5, 0xC1, 0x61, 0xE5, 0x18,
  26316. 0xA5, 0x38, 0x68, 0xED, 0xB4, 0x17, 0x62, 0x0D,
  26317. 0x01, 0x5E, 0xC3, 0x04, 0xA6, 0xBA, 0xB1, 0x01,
  26318. 0x60, 0x5C, 0xC1, 0x3A, 0x34, 0x97, 0xD6, 0xDB,
  26319. 0x67, 0x73, 0x4D, 0x33, 0x96, 0x01, 0x67, 0x44,
  26320. 0xEA, 0x47, 0x5E, 0x44, 0xB5, 0xE5, 0xD1, 0x6C,
  26321. 0x20, 0xA9, 0x6D, 0x4D, 0xBC, 0x02, 0xF0, 0x70,
  26322. 0xE4, 0xDD, 0xE9, 0xD5, 0x5C, 0x28, 0x29, 0x0B,
  26323. 0xB4, 0x60, 0x2A, 0xF1, 0xF7, 0x1A, 0xF0, 0x36,
  26324. 0xAE, 0x51, 0x3A, 0xAE, 0x6E, 0x48, 0x7D, 0xC7,
  26325. 0x5C, 0xF3, 0xDC, 0xF6, 0xED, 0x27, 0x4E, 0x8E,
  26326. 0x48, 0x18, 0x3E, 0x08, 0xF1, 0xD8, 0x3D, 0x0D,
  26327. 0xE7, 0x2F, 0x65, 0x8A, 0x6F, 0xE2, 0x1E, 0x06,
  26328. 0xC1, 0x04, 0x58, 0x7B, 0x4A, 0x75, 0x60, 0x92,
  26329. 0x13, 0xC6, 0x40, 0x2D, 0x3A, 0x8A, 0xD1, 0x03,
  26330. 0x05, 0x1F, 0x28, 0x66, 0xC2, 0x57, 0x2A, 0x4C,
  26331. 0xE1, 0xA3, 0xCB, 0xA1, 0x95, 0x30, 0x10, 0xED,
  26332. 0xDF, 0xAE, 0x70, 0x49, 0x4E, 0xF6, 0xB4, 0x5A,
  26333. 0xB6, 0x22, 0x56, 0x37, 0x05, 0xE7, 0x3E, 0xB2,
  26334. 0xE3, 0x96, 0x62, 0xEC, 0x09, 0x53, 0xC0, 0x50,
  26335. 0x3D, 0xA7, 0xBC, 0x9B, 0x39, 0x02, 0x26, 0x16,
  26336. 0xB5, 0x34, 0x17, 0xD4, 0xCA, 0xFE, 0x1D, 0xE4,
  26337. 0x5A, 0xDA, 0x4C, 0xC2, 0xCA, 0x8E, 0x79, 0xBF,
  26338. 0xD8, 0x4C, 0xBB, 0xFA, 0x30, 0x7B, 0xA9, 0x3E,
  26339. 0x52, 0x19, 0xB1, 0x00, 0x00, 0x00, 0x00, 0x00,
  26340. 0x00, 0x00, 0x00, 0x00, 0x00
  26341. };
  26342. #endif /* HAVE_PKCS7 && !NO_FILESYSTEM && ASN_BER_TO_DER &&
  26343. * !NO_DES3 && !NO_SHA
  26344. */
  26345. /*
  26346. * Testing wc_PKCS7_BER()
  26347. */
  26348. static int test_wc_PKCS7_BER(void)
  26349. {
  26350. EXPECT_DECLS;
  26351. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  26352. !defined(NO_SHA) && defined(ASN_BER_TO_DER)
  26353. PKCS7* pkcs7 = NULL;
  26354. char fName[] = "./certs/test-ber-exp02-05-2022.p7b";
  26355. XFILE f = XBADFILE;
  26356. byte der[4096];
  26357. #ifndef NO_DES3
  26358. byte decoded[2048];
  26359. #endif
  26360. word32 derSz = 0;
  26361. #ifndef NO_PKCS7_STREAM
  26362. word32 z;
  26363. int ret;
  26364. #endif /* !NO_PKCS7_STREAM */
  26365. ExpectTrue((f = XFOPEN(fName, "rb")) != XBADFILE);
  26366. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  26367. if (f != XBADFILE) {
  26368. XFCLOSE(f);
  26369. f = XBADFILE;
  26370. }
  26371. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26372. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26373. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26374. #ifndef NO_RSA
  26375. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26376. #ifndef NO_PKCS7_STREAM
  26377. wc_PKCS7_Free(pkcs7);
  26378. pkcs7 = NULL;
  26379. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26380. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, INVALID_DEVID), 0);
  26381. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26382. /* test for streaming */
  26383. ret = -1;
  26384. for (z = 0; z < derSz && ret != 0; z++) {
  26385. ret = wc_PKCS7_VerifySignedData(pkcs7, der + z, 1);
  26386. if (ret < 0){
  26387. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26388. }
  26389. }
  26390. ExpectIntEQ(ret, 0);
  26391. #endif /* !NO_PKCS7_STREAM */
  26392. #else
  26393. ExpectIntNE(wc_PKCS7_VerifySignedData(pkcs7, der, derSz), 0);
  26394. #endif
  26395. wc_PKCS7_Free(pkcs7);
  26396. pkcs7 = NULL;
  26397. #ifndef NO_DES3
  26398. /* decode BER content */
  26399. ExpectTrue((f = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  26400. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  26401. if (f != XBADFILE) {
  26402. XFCLOSE(f);
  26403. f = XBADFILE;
  26404. }
  26405. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26406. #ifndef NO_RSA
  26407. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  26408. #else
  26409. ExpectIntNE(wc_PKCS7_InitWithCert(pkcs7, der, derSz), 0);
  26410. #endif
  26411. ExpectTrue((f = XFOPEN("./certs/1024/client-key.der", "rb")) != XBADFILE);
  26412. ExpectTrue((derSz = (word32)XFREAD(der, 1, sizeof(der), f)) > 0);
  26413. if (f != XBADFILE) {
  26414. XFCLOSE(f);
  26415. f = XBADFILE;
  26416. }
  26417. if (pkcs7 != NULL) {
  26418. pkcs7->privateKey = der;
  26419. pkcs7->privateKeySz = derSz;
  26420. }
  26421. #ifndef NO_RSA
  26422. #ifdef WOLFSSL_SP_MATH
  26423. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26424. sizeof(berContent), decoded, sizeof(decoded)), WC_KEY_SIZE_E);
  26425. #else
  26426. ExpectIntGT(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26427. sizeof(berContent), decoded, sizeof(decoded)), 0);
  26428. #endif
  26429. #else
  26430. ExpectIntEQ(wc_PKCS7_DecodeEnvelopedData(pkcs7, berContent,
  26431. sizeof(berContent), decoded, sizeof(decoded)), NOT_COMPILED_IN);
  26432. #endif
  26433. wc_PKCS7_Free(pkcs7);
  26434. #endif /* !NO_DES3 */
  26435. #endif
  26436. return EXPECT_RESULT();
  26437. } /* END test_wc_PKCS7_BER() */
  26438. static int test_wc_PKCS7_signed_enveloped(void)
  26439. {
  26440. EXPECT_DECLS;
  26441. #if defined(HAVE_PKCS7) && !defined(NO_RSA) && !defined(NO_AES) && \
  26442. !defined(NO_FILESYSTEM)
  26443. XFILE f = XBADFILE;
  26444. PKCS7* pkcs7 = NULL;
  26445. #ifdef HAVE_AES_CBC
  26446. PKCS7* inner = NULL;
  26447. #endif
  26448. WC_RNG rng;
  26449. unsigned char key[FOURK_BUF/2];
  26450. unsigned char cert[FOURK_BUF/2];
  26451. unsigned char env[FOURK_BUF];
  26452. int envSz = FOURK_BUF;
  26453. int keySz = 0;
  26454. int certSz = 0;
  26455. unsigned char sig[FOURK_BUF * 2];
  26456. int sigSz = FOURK_BUF * 2;
  26457. #ifdef HAVE_AES_CBC
  26458. unsigned char decoded[FOURK_BUF];
  26459. int decodedSz = FOURK_BUF;
  26460. #endif
  26461. #ifndef NO_PKCS7_STREAM
  26462. int z;
  26463. int ret;
  26464. #endif /* !NO_PKCS7_STREAM */
  26465. XMEMSET(&rng, 0, sizeof(WC_RNG));
  26466. /* load cert */
  26467. ExpectTrue((f = XFOPEN(cliCertDerFile, "rb")) != XBADFILE);
  26468. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), f)), 0);
  26469. if (f != XBADFILE) {
  26470. XFCLOSE(f);
  26471. f = XBADFILE;
  26472. }
  26473. /* load key */
  26474. ExpectTrue((f = XFOPEN(cliKeyFile, "rb")) != XBADFILE);
  26475. ExpectIntGT((keySz = (int)XFREAD(key, 1, sizeof(key), f)), 0);
  26476. if (f != XBADFILE) {
  26477. XFCLOSE(f);
  26478. f = XBADFILE;
  26479. }
  26480. ExpectIntGT(keySz = wolfSSL_KeyPemToDer(key, keySz, key, keySz, NULL), 0);
  26481. /* sign cert for envelope */
  26482. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26483. ExpectIntEQ(wc_InitRng(&rng), 0);
  26484. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  26485. if (pkcs7 != NULL) {
  26486. pkcs7->content = cert;
  26487. pkcs7->contentSz = (word32)certSz;
  26488. pkcs7->contentOID = DATA;
  26489. pkcs7->privateKey = key;
  26490. pkcs7->privateKeySz = (word32)keySz;
  26491. pkcs7->encryptOID = RSAk;
  26492. pkcs7->hashOID = SHA256h;
  26493. pkcs7->rng = &rng;
  26494. }
  26495. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, (word32)sigSz)), 0);
  26496. wc_PKCS7_Free(pkcs7);
  26497. pkcs7 = NULL;
  26498. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  26499. #ifdef HAVE_AES_CBC
  26500. /* create envelope */
  26501. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26502. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  26503. if (pkcs7 != NULL) {
  26504. pkcs7->content = sig;
  26505. pkcs7->contentSz = (word32)sigSz;
  26506. pkcs7->contentOID = DATA;
  26507. pkcs7->encryptOID = AES256CBCb;
  26508. pkcs7->privateKey = key;
  26509. pkcs7->privateKeySz = (word32)keySz;
  26510. }
  26511. ExpectIntGT((envSz = wc_PKCS7_EncodeEnvelopedData(pkcs7, env, (word32)envSz)), 0);
  26512. ExpectIntLT(wc_PKCS7_EncodeEnvelopedData(pkcs7, env, 2), 0);
  26513. wc_PKCS7_Free(pkcs7);
  26514. pkcs7 = NULL;
  26515. #endif
  26516. /* create bad signed enveloped data */
  26517. sigSz = FOURK_BUF * 2;
  26518. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26519. ExpectIntEQ(wc_InitRng(&rng), 0);
  26520. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  26521. if (pkcs7 != NULL) {
  26522. pkcs7->content = env;
  26523. pkcs7->contentSz = (word32)envSz;
  26524. pkcs7->contentOID = DATA;
  26525. pkcs7->privateKey = key;
  26526. pkcs7->privateKeySz = (word32)keySz;
  26527. pkcs7->encryptOID = RSAk;
  26528. pkcs7->hashOID = SHA256h;
  26529. pkcs7->rng = &rng;
  26530. }
  26531. /* Set no certs in bundle for this test. */
  26532. if (pkcs7 != NULL) {
  26533. ExpectIntEQ(wc_PKCS7_SetNoCerts(pkcs7, 1), 0);
  26534. ExpectIntEQ(wc_PKCS7_SetNoCerts(NULL, 1), BAD_FUNC_ARG);
  26535. ExpectIntEQ(wc_PKCS7_GetNoCerts(pkcs7), 1);
  26536. }
  26537. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, (word32)sigSz)), 0);
  26538. wc_PKCS7_Free(pkcs7);
  26539. pkcs7 = NULL;
  26540. /* check verify fails */
  26541. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26542. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26543. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, (word32)sigSz),
  26544. PKCS7_SIGNEEDS_CHECK);
  26545. /* try verifying the signature manually */
  26546. {
  26547. RsaKey rKey;
  26548. word32 idx = 0;
  26549. byte digest[MAX_SEQ_SZ + MAX_ALGO_SZ + MAX_OCTET_STR_SZ +
  26550. WC_MAX_DIGEST_SIZE];
  26551. int digestSz = 0;
  26552. ExpectIntEQ(wc_InitRsaKey(&rKey, HEAP_HINT), 0);
  26553. ExpectIntEQ(wc_RsaPrivateKeyDecode(key, &idx, &rKey, (word32)keySz), 0);
  26554. ExpectIntGT(digestSz = wc_RsaSSL_Verify(pkcs7->signature,
  26555. pkcs7->signatureSz, digest, sizeof(digest), &rKey), 0);
  26556. ExpectIntEQ(digestSz, pkcs7->pkcs7DigestSz);
  26557. ExpectIntEQ(XMEMCMP(digest, pkcs7->pkcs7Digest, digestSz), 0);
  26558. ExpectIntEQ(wc_FreeRsaKey(&rKey), 0);
  26559. /* verify was success */
  26560. }
  26561. wc_PKCS7_Free(pkcs7);
  26562. pkcs7 = NULL;
  26563. /* initializing the PKCS7 struct with the signing certificate should pass */
  26564. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26565. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  26566. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, (word32)sigSz), 0);
  26567. #ifndef NO_PKCS7_STREAM
  26568. wc_PKCS7_Free(pkcs7);
  26569. pkcs7 = NULL;
  26570. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  26571. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, cert, (word32)certSz), 0);
  26572. /* test for streaming */
  26573. ret = -1;
  26574. for (z = 0; z < sigSz && ret != 0; z++) {
  26575. ret = wc_PKCS7_VerifySignedData(pkcs7, sig + z, 1);
  26576. if (ret < 0){
  26577. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26578. }
  26579. }
  26580. ExpectIntEQ(ret, 0);
  26581. #endif /* !NO_PKCS7_STREAM */
  26582. wc_PKCS7_Free(pkcs7);
  26583. pkcs7 = NULL;
  26584. /* create valid degenerate bundle */
  26585. sigSz = FOURK_BUF * 2;
  26586. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26587. if (pkcs7 != NULL) {
  26588. pkcs7->content = env;
  26589. pkcs7->contentSz = (word32)envSz;
  26590. pkcs7->contentOID = DATA;
  26591. pkcs7->privateKey = key;
  26592. pkcs7->privateKeySz = (word32)keySz;
  26593. pkcs7->encryptOID = RSAk;
  26594. pkcs7->hashOID = SHA256h;
  26595. pkcs7->rng = &rng;
  26596. }
  26597. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  26598. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, (word32)sigSz)), 0);
  26599. wc_PKCS7_Free(pkcs7);
  26600. pkcs7 = NULL;
  26601. wc_FreeRng(&rng);
  26602. /* check verify */
  26603. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26604. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26605. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, sig, (word32)sigSz), 0);
  26606. ExpectNotNull(pkcs7->content);
  26607. #ifndef NO_PKCS7_STREAM
  26608. wc_PKCS7_Free(pkcs7);
  26609. pkcs7 = NULL;
  26610. /* create valid degenerate bundle */
  26611. sigSz = FOURK_BUF * 2;
  26612. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26613. if (pkcs7 != NULL) {
  26614. pkcs7->content = env;
  26615. pkcs7->contentSz = (word32)envSz;
  26616. pkcs7->contentOID = DATA;
  26617. pkcs7->privateKey = key;
  26618. pkcs7->privateKeySz = (word32)keySz;
  26619. pkcs7->encryptOID = RSAk;
  26620. pkcs7->hashOID = SHA256h;
  26621. pkcs7->rng = &rng;
  26622. }
  26623. ExpectIntEQ(wc_PKCS7_SetSignerIdentifierType(pkcs7, DEGENERATE_SID), 0);
  26624. ExpectIntGT((sigSz = wc_PKCS7_EncodeSignedData(pkcs7, sig, (word32)sigSz)), 0);
  26625. wc_PKCS7_Free(pkcs7);
  26626. pkcs7 = NULL;
  26627. wc_FreeRng(&rng);
  26628. /* check verify */
  26629. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26630. ExpectIntEQ(wc_PKCS7_Init(pkcs7, HEAP_HINT, testDevId), 0);
  26631. /* test for streaming */
  26632. ret = -1;
  26633. for (z = 0; z < sigSz && ret != 0; z++) {
  26634. ret = wc_PKCS7_VerifySignedData(pkcs7, sig + z, 1);
  26635. if (ret < 0){
  26636. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26637. }
  26638. }
  26639. ExpectIntEQ(ret, 0);
  26640. #endif /* !NO_PKCS7_STREAM */
  26641. #ifdef HAVE_AES_CBC
  26642. /* check decode */
  26643. ExpectNotNull(inner = wc_PKCS7_New(NULL, 0));
  26644. ExpectIntEQ(wc_PKCS7_InitWithCert(inner, cert, (word32)certSz), 0);
  26645. if (inner != NULL) {
  26646. inner->privateKey = key;
  26647. inner->privateKeySz = (word32)keySz;
  26648. }
  26649. ExpectIntGT((decodedSz = wc_PKCS7_DecodeEnvelopedData(inner, pkcs7->content,
  26650. pkcs7->contentSz, decoded, (word32)decodedSz)), 0);
  26651. wc_PKCS7_Free(inner);
  26652. inner = NULL;
  26653. #endif
  26654. wc_PKCS7_Free(pkcs7);
  26655. pkcs7 = NULL;
  26656. #ifdef HAVE_AES_CBC
  26657. /* check cert set */
  26658. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26659. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26660. ExpectIntEQ(wc_PKCS7_VerifySignedData(pkcs7, decoded, (word32)decodedSz), 0);
  26661. ExpectNotNull(pkcs7->singleCert);
  26662. ExpectIntNE(pkcs7->singleCertSz, 0);
  26663. wc_PKCS7_Free(pkcs7);
  26664. pkcs7 = NULL;
  26665. #ifndef NO_PKCS7_STREAM
  26666. ExpectNotNull(pkcs7 = wc_PKCS7_New(NULL, 0));
  26667. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, NULL, 0), 0);
  26668. /* test for streaming */
  26669. ret = -1;
  26670. for (z = 0; z < decodedSz && ret != 0; z++) {
  26671. ret = wc_PKCS7_VerifySignedData(pkcs7, decoded + z, 1);
  26672. if (ret < 0){
  26673. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  26674. }
  26675. }
  26676. ExpectIntEQ(ret, 0);
  26677. ExpectNotNull(pkcs7->singleCert);
  26678. ExpectIntNE(pkcs7->singleCertSz, 0);
  26679. wc_PKCS7_Free(pkcs7);
  26680. pkcs7 = NULL;
  26681. #endif /* !NO_PKCS7_STREAM */
  26682. #endif
  26683. #endif /* HAVE_PKCS7 && !NO_RSA && !NO_AES */
  26684. return EXPECT_RESULT();
  26685. }
  26686. static int test_wc_PKCS7_NoDefaultSignedAttribs(void)
  26687. {
  26688. EXPECT_DECLS;
  26689. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26690. && !defined(NO_AES)
  26691. PKCS7* pkcs7 = NULL;
  26692. void* heap = NULL;
  26693. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26694. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26695. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(NULL), BAD_FUNC_ARG);
  26696. ExpectIntEQ(wc_PKCS7_NoDefaultSignedAttribs(pkcs7), 0);
  26697. wc_PKCS7_Free(pkcs7);
  26698. #endif
  26699. return EXPECT_RESULT();
  26700. }
  26701. static int test_wc_PKCS7_SetOriEncryptCtx(void)
  26702. {
  26703. EXPECT_DECLS;
  26704. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26705. && !defined(NO_AES)
  26706. PKCS7* pkcs7 = NULL;
  26707. void* heap = NULL;
  26708. WOLFSSL_CTX* ctx = NULL;
  26709. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26710. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26711. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26712. ExpectIntEQ(wc_PKCS7_SetOriEncryptCtx(pkcs7, ctx), 0);
  26713. wc_PKCS7_Free(pkcs7);
  26714. #endif
  26715. return EXPECT_RESULT();
  26716. }
  26717. static int test_wc_PKCS7_SetOriDecryptCtx(void)
  26718. {
  26719. EXPECT_DECLS;
  26720. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26721. && !defined(NO_AES)
  26722. PKCS7* pkcs7 = NULL;
  26723. void* heap = NULL;
  26724. WOLFSSL_CTX* ctx = NULL;
  26725. ExpectNotNull(pkcs7 = wc_PKCS7_New(heap, testDevId));
  26726. ExpectIntEQ(wc_PKCS7_Init(pkcs7, heap, testDevId), 0);
  26727. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(NULL, ctx), BAD_FUNC_ARG);
  26728. ExpectIntEQ(wc_PKCS7_SetOriDecryptCtx(pkcs7, ctx), 0);
  26729. wc_PKCS7_Free(pkcs7);
  26730. #endif
  26731. return EXPECT_RESULT();
  26732. }
  26733. static int test_wc_PKCS7_DecodeCompressedData(void)
  26734. {
  26735. EXPECT_DECLS;
  26736. #if defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26737. && !defined(NO_AES) && defined(HAVE_LIBZ)
  26738. PKCS7* pkcs7 = NULL;
  26739. void* heap = NULL;
  26740. byte out[4096];
  26741. byte* decompressed = NULL;
  26742. int outSz;
  26743. int decompressedSz;
  26744. const char* cert = "./certs/client-cert.pem";
  26745. byte* cert_buf = NULL;
  26746. size_t cert_sz = 0;
  26747. ExpectIntEQ(load_file(cert, &cert_buf, &cert_sz), 0);
  26748. ExpectNotNull((decompressed = (byte*)XMALLOC(cert_sz, heap,
  26749. DYNAMIC_TYPE_TMP_BUFFER)));
  26750. decompressedSz = (int)cert_sz;
  26751. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26752. if (pkcs7 != NULL) {
  26753. pkcs7->content = (byte*)cert_buf;
  26754. pkcs7->contentSz = (word32)cert_sz;
  26755. pkcs7->contentOID = DATA;
  26756. }
  26757. ExpectIntGT((outSz = wc_PKCS7_EncodeCompressedData(pkcs7, out,
  26758. sizeof(out))), 0);
  26759. wc_PKCS7_Free(pkcs7);
  26760. pkcs7 = NULL;
  26761. /* compressed key should be smaller than when started */
  26762. ExpectIntLT(outSz, cert_sz);
  26763. /* test decompression */
  26764. ExpectNotNull((pkcs7 = wc_PKCS7_New(heap, testDevId)));
  26765. ExpectIntEQ(pkcs7->contentOID, 0);
  26766. /* fail case with out buffer too small */
  26767. ExpectIntLT(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26768. decompressed, outSz), 0);
  26769. /* success case */
  26770. ExpectIntEQ(wc_PKCS7_DecodeCompressedData(pkcs7, out, outSz,
  26771. decompressed, decompressedSz), cert_sz);
  26772. ExpectIntEQ(pkcs7->contentOID, DATA);
  26773. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26774. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26775. decompressed = NULL;
  26776. /* test decompression function with different 'max' inputs */
  26777. outSz = sizeof(out);
  26778. ExpectIntGT((outSz = wc_Compress(out, outSz, cert_buf, (word32)cert_sz, 0)),
  26779. 0);
  26780. ExpectIntLT(wc_DeCompressDynamic(&decompressed, 1, DYNAMIC_TYPE_TMP_BUFFER,
  26781. out, outSz, 0, heap), 0);
  26782. ExpectNull(decompressed);
  26783. ExpectIntGT(wc_DeCompressDynamic(&decompressed, -1, DYNAMIC_TYPE_TMP_BUFFER,
  26784. out, outSz, 0, heap), 0);
  26785. ExpectNotNull(decompressed);
  26786. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26787. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26788. decompressed = NULL;
  26789. ExpectIntGT(wc_DeCompressDynamic(&decompressed, DYNAMIC_TYPE_TMP_BUFFER, 5,
  26790. out, outSz, 0, heap), 0);
  26791. ExpectNotNull(decompressed);
  26792. ExpectIntEQ(XMEMCMP(decompressed, cert_buf, cert_sz), 0);
  26793. XFREE(decompressed, heap, DYNAMIC_TYPE_TMP_BUFFER);
  26794. if (cert_buf != NULL)
  26795. free(cert_buf);
  26796. wc_PKCS7_Free(pkcs7);
  26797. #endif
  26798. return EXPECT_RESULT();
  26799. }
  26800. static int test_wc_i2d_PKCS12(void)
  26801. {
  26802. EXPECT_DECLS;
  26803. #if !defined(NO_ASN) && !defined(NO_PWDBASED) && defined(HAVE_PKCS12) \
  26804. && !defined(NO_FILESYSTEM) && !defined(NO_RSA) \
  26805. && !defined(NO_AES) && !defined(NO_SHA)
  26806. WC_PKCS12* pkcs12 = NULL;
  26807. unsigned char der[FOURK_BUF * 2];
  26808. unsigned char* pt;
  26809. int derSz = 0;
  26810. unsigned char out[FOURK_BUF * 2];
  26811. int outSz = FOURK_BUF * 2;
  26812. const char p12_f[] = "./certs/test-servercert.p12";
  26813. XFILE f = XBADFILE;
  26814. ExpectTrue((f = XFOPEN(p12_f, "rb")) != XBADFILE);
  26815. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  26816. if (f != XBADFILE)
  26817. XFCLOSE(f);
  26818. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  26819. ExpectIntEQ(wc_d2i_PKCS12(der, (word32)derSz, pkcs12), 0);
  26820. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26821. ExpectIntEQ(outSz, derSz);
  26822. outSz = derSz - 1;
  26823. pt = out;
  26824. ExpectIntLE(wc_i2d_PKCS12(pkcs12, &pt, &outSz), 0);
  26825. outSz = derSz;
  26826. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, &outSz), derSz);
  26827. ExpectIntEQ((pt == out), 0);
  26828. pt = NULL;
  26829. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, &pt, NULL), derSz);
  26830. XFREE(pt, NULL, DYNAMIC_TYPE_PKCS);
  26831. wc_PKCS12_free(pkcs12);
  26832. pkcs12 = NULL;
  26833. /* Run the same test but use wc_d2i_PKCS12_fp. */
  26834. ExpectNotNull(pkcs12 = wc_PKCS12_new());
  26835. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26836. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26837. ExpectIntEQ(outSz, derSz);
  26838. wc_PKCS12_free(pkcs12);
  26839. pkcs12 = NULL;
  26840. /* wc_d2i_PKCS12_fp can also allocate the PKCS12 object for the caller. */
  26841. ExpectIntEQ(wc_d2i_PKCS12_fp("./certs/test-servercert.p12", &pkcs12), 0);
  26842. ExpectIntEQ(wc_i2d_PKCS12(pkcs12, NULL, &outSz), LENGTH_ONLY_E);
  26843. ExpectIntEQ(outSz, derSz);
  26844. wc_PKCS12_free(pkcs12);
  26845. pkcs12 = NULL;
  26846. #endif
  26847. return EXPECT_RESULT();
  26848. }
  26849. /* Testing wc_SignatureGetSize() for signature type ECC */
  26850. static int test_wc_SignatureGetSize_ecc(void)
  26851. {
  26852. EXPECT_DECLS;
  26853. #if !defined(NO_SIG_WRAPPER) && defined(HAVE_ECC) && !defined(NO_ECC256)
  26854. enum wc_SignatureType sig_type;
  26855. word32 key_len;
  26856. ecc_key ecc;
  26857. const char* qx =
  26858. "fa2737fb93488d19caef11ae7faf6b7f4bcd67b286e3fc54e8a65c2b74aeccb0";
  26859. const char* qy =
  26860. "d4ccd6dae698208aa8c3a6f39e45510d03be09b2f124bfc067856c324f9b4d09";
  26861. const char* d =
  26862. "be34baa8d040a3b991f9075b56ba292f755b90e4b6dc10dad36715c33cfdac25";
  26863. XMEMSET(&ecc, 0, sizeof(ecc_key));
  26864. ExpectIntEQ(wc_ecc_init(&ecc), 0);
  26865. ExpectIntEQ(wc_ecc_import_raw(&ecc, qx, qy, d, "SECP256R1"), 0);
  26866. /* Input for signature type ECC */
  26867. sig_type = WC_SIGNATURE_TYPE_ECC;
  26868. key_len = sizeof(ecc_key);
  26869. ExpectIntGT(wc_SignatureGetSize(sig_type, &ecc, key_len), 0);
  26870. /* Test bad args */
  26871. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26872. sig_type = (enum wc_SignatureType) 100;
  26873. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26874. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  26875. sig_type = WC_SIGNATURE_TYPE_ECC;
  26876. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), 0);
  26877. key_len = (word32)0;
  26878. ExpectIntEQ(wc_SignatureGetSize(sig_type, &ecc, key_len), BAD_FUNC_ARG);
  26879. DoExpectIntEQ(wc_ecc_free(&ecc), 0);
  26880. #endif /* !NO_SIG_WRAPPER && HAVE_ECC && !NO_ECC256 */
  26881. return EXPECT_RESULT();
  26882. } /* END test_wc_SignatureGetSize_ecc() */
  26883. /* Testing wc_SignatureGetSize() for signature type rsa */
  26884. static int test_wc_SignatureGetSize_rsa(void)
  26885. {
  26886. EXPECT_DECLS;
  26887. #if !defined(NO_SIG_WRAPPER) && !defined(NO_RSA)
  26888. enum wc_SignatureType sig_type;
  26889. word32 key_len;
  26890. word32 idx = 0;
  26891. RsaKey rsa_key;
  26892. byte* tmp = NULL;
  26893. size_t bytes;
  26894. XMEMSET(&rsa_key, 0, sizeof(RsaKey));
  26895. #ifdef USE_CERT_BUFFERS_1024
  26896. bytes = (size_t)sizeof_client_key_der_1024;
  26897. if (bytes < (size_t)sizeof_client_key_der_1024)
  26898. bytes = (size_t)sizeof_client_cert_der_1024;
  26899. #elif defined(USE_CERT_BUFFERS_2048)
  26900. bytes = (size_t)sizeof_client_key_der_2048;
  26901. if (bytes < (size_t)sizeof_client_cert_der_2048)
  26902. bytes = (size_t)sizeof_client_cert_der_2048;
  26903. #else
  26904. bytes = FOURK_BUF;
  26905. #endif
  26906. ExpectNotNull(tmp = (byte*)XMALLOC(bytes, HEAP_HINT,
  26907. DYNAMIC_TYPE_TMP_BUFFER));
  26908. if (tmp != NULL) {
  26909. #ifdef USE_CERT_BUFFERS_1024
  26910. XMEMCPY(tmp, client_key_der_1024, (size_t)sizeof_client_key_der_1024);
  26911. #elif defined(USE_CERT_BUFFERS_2048)
  26912. XMEMCPY(tmp, client_key_der_2048, (size_t)sizeof_client_key_der_2048);
  26913. #elif !defined(NO_FILESYSTEM)
  26914. XFILE file = XBADFILE;
  26915. ExpectTrue((file = XFOPEN(clientKey, "rb")) != XBADFILE);
  26916. ExpectIntGT(bytes = (size_t)XFREAD(tmp, 1, FOURK_BUF, file), 0);
  26917. if (file != XBADFILE)
  26918. XFCLOSE(file);
  26919. }
  26920. #else
  26921. ExpectFail();
  26922. #endif
  26923. }
  26924. ExpectIntEQ(wc_InitRsaKey_ex(&rsa_key, HEAP_HINT, testDevId), 0);
  26925. ExpectIntEQ(wc_RsaPrivateKeyDecode(tmp, &idx, &rsa_key, (word32)bytes), 0);
  26926. /* Input for signature type RSA */
  26927. sig_type = WC_SIGNATURE_TYPE_RSA;
  26928. key_len = sizeof(RsaKey);
  26929. ExpectIntGT(wc_SignatureGetSize(sig_type, &rsa_key, key_len), 0);
  26930. /* Test bad args */
  26931. /* // NOLINTBEGIN(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26932. sig_type = (enum wc_SignatureType)100;
  26933. /* // NOLINTEND(clang-analyzer-optin.core.EnumCastOutOfRange) */
  26934. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  26935. sig_type = WC_SIGNATURE_TYPE_RSA;
  26936. ExpectIntEQ(wc_SignatureGetSize(sig_type, NULL, key_len), BAD_FUNC_ARG);
  26937. key_len = (word32)0;
  26938. ExpectIntEQ(wc_SignatureGetSize(sig_type, &rsa_key, key_len), BAD_FUNC_ARG);
  26939. DoExpectIntEQ(wc_FreeRsaKey(&rsa_key), 0);
  26940. XFREE(tmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  26941. #endif /* !NO_SIG_WRAPPER && !NO_RSA */
  26942. return EXPECT_RESULT();
  26943. } /* END test_wc_SignatureGetSize_rsa(void) */
  26944. /*----------------------------------------------------------------------------*
  26945. | hash.h Tests
  26946. *----------------------------------------------------------------------------*/
  26947. static int test_wc_HashInit(void)
  26948. {
  26949. EXPECT_DECLS;
  26950. int i; /* 0 indicates tests passed, 1 indicates failure */
  26951. wc_HashAlg hash;
  26952. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26953. enum wc_HashType enumArray[] = {
  26954. #ifndef NO_MD5
  26955. WC_HASH_TYPE_MD5,
  26956. #endif
  26957. #ifndef NO_SHA
  26958. WC_HASH_TYPE_SHA,
  26959. #endif
  26960. #ifdef WOLFSSL_SHA224
  26961. WC_HASH_TYPE_SHA224,
  26962. #endif
  26963. #ifndef NO_SHA256
  26964. WC_HASH_TYPE_SHA256,
  26965. #endif
  26966. #ifdef WOLFSSL_SHA384
  26967. WC_HASH_TYPE_SHA384,
  26968. #endif
  26969. #ifdef WOLFSSL_SHA512
  26970. WC_HASH_TYPE_SHA512,
  26971. #endif
  26972. };
  26973. /* dynamically finds the length */
  26974. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  26975. /* For loop to test various arguments... */
  26976. for (i = 0; i < enumlen; i++) {
  26977. /* check for bad args */
  26978. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  26979. wc_HashFree(&hash, enumArray[i]);
  26980. /* check for null ptr */
  26981. ExpectIntEQ(wc_HashInit(NULL, enumArray[i]), BAD_FUNC_ARG);
  26982. } /* end of for loop */
  26983. return EXPECT_RESULT();
  26984. } /* end of test_wc_HashInit */
  26985. /*
  26986. * Unit test function for wc_HashSetFlags()
  26987. */
  26988. static int test_wc_HashSetFlags(void)
  26989. {
  26990. EXPECT_DECLS;
  26991. #ifdef WOLFSSL_HASH_FLAGS
  26992. wc_HashAlg hash;
  26993. word32 flags = 0;
  26994. int i, j;
  26995. int notSupportedLen;
  26996. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  26997. enum wc_HashType enumArray[] = {
  26998. #ifndef NO_MD5
  26999. WC_HASH_TYPE_MD5,
  27000. #endif
  27001. #ifndef NO_SHA
  27002. WC_HASH_TYPE_SHA,
  27003. #endif
  27004. #ifdef WOLFSSL_SHA224
  27005. WC_HASH_TYPE_SHA224,
  27006. #endif
  27007. #ifndef NO_SHA256
  27008. WC_HASH_TYPE_SHA256,
  27009. #endif
  27010. #ifdef WOLFSSL_SHA384
  27011. WC_HASH_TYPE_SHA384,
  27012. #endif
  27013. #ifdef WOLFSSL_SHA512
  27014. WC_HASH_TYPE_SHA512,
  27015. #endif
  27016. #ifdef WOLFSSL_SHA3
  27017. WC_HASH_TYPE_SHA3_224,
  27018. #endif
  27019. };
  27020. enum wc_HashType notSupported[] = {
  27021. WC_HASH_TYPE_MD5_SHA,
  27022. WC_HASH_TYPE_MD2,
  27023. WC_HASH_TYPE_MD4,
  27024. WC_HASH_TYPE_BLAKE2B,
  27025. WC_HASH_TYPE_BLAKE2S,
  27026. WC_HASH_TYPE_NONE,
  27027. };
  27028. /* dynamically finds the length */
  27029. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  27030. /* For loop to test various arguments... */
  27031. for (i = 0; i < enumlen; i++) {
  27032. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  27033. ExpectIntEQ(wc_HashSetFlags(&hash, enumArray[i], flags), 0);
  27034. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  27035. ExpectIntEQ(wc_HashSetFlags(NULL, enumArray[i], flags), BAD_FUNC_ARG);
  27036. wc_HashFree(&hash, enumArray[i]);
  27037. }
  27038. /* For loop to test not supported cases */
  27039. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  27040. for (j = 0; j < notSupportedLen; j++) {
  27041. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  27042. ExpectIntEQ(wc_HashSetFlags(&hash, notSupported[j], flags),
  27043. BAD_FUNC_ARG);
  27044. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  27045. }
  27046. #endif
  27047. return EXPECT_RESULT();
  27048. } /* END test_wc_HashSetFlags */
  27049. /*
  27050. * Unit test function for wc_HashGetFlags()
  27051. */
  27052. static int test_wc_HashGetFlags(void)
  27053. {
  27054. EXPECT_DECLS;
  27055. #ifdef WOLFSSL_HASH_FLAGS
  27056. wc_HashAlg hash;
  27057. word32 flags = 0;
  27058. int i, j;
  27059. /* enum for holding supported algorithms, #ifndef's restrict if disabled */
  27060. enum wc_HashType enumArray[] = {
  27061. #ifndef NO_MD5
  27062. WC_HASH_TYPE_MD5,
  27063. #endif
  27064. #ifndef NO_SHA
  27065. WC_HASH_TYPE_SHA,
  27066. #endif
  27067. #ifdef WOLFSSL_SHA224
  27068. WC_HASH_TYPE_SHA224,
  27069. #endif
  27070. #ifndef NO_SHA256
  27071. WC_HASH_TYPE_SHA256,
  27072. #endif
  27073. #ifdef WOLFSSL_SHA384
  27074. WC_HASH_TYPE_SHA384,
  27075. #endif
  27076. #ifdef WOLFSSL_SHA512
  27077. WC_HASH_TYPE_SHA512,
  27078. #endif
  27079. #ifdef WOLFSSL_SHA3
  27080. WC_HASH_TYPE_SHA3_224,
  27081. #endif
  27082. };
  27083. enum wc_HashType notSupported[] = {
  27084. WC_HASH_TYPE_MD5_SHA,
  27085. WC_HASH_TYPE_MD2,
  27086. WC_HASH_TYPE_MD4,
  27087. WC_HASH_TYPE_BLAKE2B,
  27088. WC_HASH_TYPE_BLAKE2S,
  27089. WC_HASH_TYPE_NONE,
  27090. };
  27091. int enumlen = (sizeof(enumArray)/sizeof(enum wc_HashType));
  27092. int notSupportedLen;
  27093. /* For loop to test various arguments... */
  27094. for (i = 0; i < enumlen; i++) {
  27095. ExpectIntEQ(wc_HashInit(&hash, enumArray[i]), 0);
  27096. ExpectIntEQ(wc_HashGetFlags(&hash, enumArray[i], &flags), 0);
  27097. ExpectTrue((flags & WC_HASH_FLAG_ISCOPY) == 0);
  27098. ExpectIntEQ(wc_HashGetFlags(NULL, enumArray[i], &flags), BAD_FUNC_ARG);
  27099. wc_HashFree(&hash, enumArray[i]);
  27100. }
  27101. /* For loop to test not supported cases */
  27102. notSupportedLen = (sizeof(notSupported)/sizeof(enum wc_HashType));
  27103. for (j = 0; j < notSupportedLen; j++) {
  27104. ExpectIntEQ(wc_HashInit(&hash, notSupported[j]), BAD_FUNC_ARG);
  27105. ExpectIntEQ(wc_HashGetFlags(&hash, notSupported[j], &flags),
  27106. BAD_FUNC_ARG);
  27107. ExpectIntEQ(wc_HashFree(&hash, notSupported[j]), BAD_FUNC_ARG);
  27108. }
  27109. #endif
  27110. return EXPECT_RESULT();
  27111. } /* END test_wc_HashGetFlags */
  27112. /*----------------------------------------------------------------------------*
  27113. | Compatibility Tests
  27114. *----------------------------------------------------------------------------*/
  27115. /*----------------------------------------------------------------------------*
  27116. | ASN.1 Tests
  27117. *----------------------------------------------------------------------------*/
  27118. static int test_wolfSSL_ASN1_BIT_STRING(void)
  27119. {
  27120. EXPECT_DECLS;
  27121. #if !defined(NO_CERTS) && defined(OPENSSL_ALL)
  27122. ASN1_BIT_STRING* str = NULL;
  27123. ExpectNotNull(str = ASN1_BIT_STRING_new());
  27124. /* Empty data testing. */
  27125. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 1), 0);
  27126. ASN1_BIT_STRING_free(str);
  27127. str = NULL;
  27128. ExpectNotNull(str = ASN1_BIT_STRING_new());
  27129. /* Invalid parameter testing. */
  27130. ExpectIntEQ(ASN1_BIT_STRING_set_bit(NULL, 42, 1), 0);
  27131. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, -1, 1), 0);
  27132. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 2), 0);
  27133. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, -1), 0);
  27134. /* No bit string - bit is always 0. */
  27135. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, 42), 0);
  27136. ExpectIntEQ(ASN1_BIT_STRING_get_bit(NULL, -1), 0);
  27137. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  27138. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 0), 0);
  27139. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 1), 1);
  27140. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 1);
  27141. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 41), 0);
  27142. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, -1), 0);
  27143. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 84, 1), 1);
  27144. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 84), 1);
  27145. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 83), 0);
  27146. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 91, 0), 1);
  27147. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 91), 0);
  27148. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 89, 0), 1);
  27149. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 89), 0);
  27150. ExpectIntEQ(ASN1_BIT_STRING_set_bit(str, 42, 0), 1);
  27151. ExpectIntEQ(ASN1_BIT_STRING_get_bit(str, 42), 0);
  27152. ASN1_BIT_STRING_free(str);
  27153. ASN1_BIT_STRING_free(NULL);
  27154. #endif
  27155. return EXPECT_RESULT();
  27156. }
  27157. static int test_wolfSSL_ASN1_INTEGER(void)
  27158. {
  27159. EXPECT_DECLS;
  27160. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27161. ASN1_INTEGER* a = NULL;
  27162. ASN1_INTEGER* dup = NULL;
  27163. const unsigned char invalidLenDer[] = {
  27164. 0x02, 0x20, 0x00
  27165. };
  27166. const unsigned char longDer[] = {
  27167. 0x02, 0x20,
  27168. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27169. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27170. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  27171. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
  27172. };
  27173. const unsigned char* p;
  27174. /* Invalid parameter testing. */
  27175. ASN1_INTEGER_free(NULL);
  27176. ExpectNull(wolfSSL_ASN1_INTEGER_dup(NULL));
  27177. ExpectNotNull(a = ASN1_INTEGER_new());
  27178. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  27179. ASN1_INTEGER_free(dup);
  27180. dup = NULL;
  27181. ASN1_INTEGER_free(a);
  27182. a = NULL;
  27183. p = longDer;
  27184. ExpectNull(d2i_ASN1_INTEGER(NULL, &p, sizeof(invalidLenDer)));
  27185. p = longDer;
  27186. ExpectNotNull(a = d2i_ASN1_INTEGER(NULL, &p, sizeof(longDer)));
  27187. ExpectPtrNE(p, longDer);
  27188. ExpectNotNull(dup = wolfSSL_ASN1_INTEGER_dup(a));
  27189. ASN1_INTEGER_free(dup);
  27190. ASN1_INTEGER_free(a);
  27191. #endif
  27192. return EXPECT_RESULT();
  27193. }
  27194. static int test_wolfSSL_ASN1_INTEGER_cmp(void)
  27195. {
  27196. EXPECT_DECLS;
  27197. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27198. ASN1_INTEGER* a = NULL;
  27199. ASN1_INTEGER* b = NULL;
  27200. ExpectNotNull(a = ASN1_INTEGER_new());
  27201. ExpectNotNull(b = ASN1_INTEGER_new());
  27202. ExpectIntEQ(ASN1_INTEGER_set(a, 1), 1);
  27203. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  27204. /* Invalid parameter testing. */
  27205. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, NULL), -1);
  27206. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, NULL), -1);
  27207. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(NULL, b), -1);
  27208. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27209. ExpectIntEQ(ASN1_INTEGER_set(b, -1), 1);
  27210. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27211. ExpectIntEQ(ASN1_INTEGER_set(a, -2), 1);
  27212. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27213. ExpectIntEQ(ASN1_INTEGER_set(b, 1), 1);
  27214. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27215. ExpectIntEQ(ASN1_INTEGER_set(a, 0x01), 1);
  27216. ExpectIntEQ(ASN1_INTEGER_set(b, 0x1000), 1);
  27217. ExpectIntLT(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27218. ExpectIntGT(wolfSSL_ASN1_INTEGER_cmp(b, a), 0);
  27219. ASN1_INTEGER_free(b);
  27220. ASN1_INTEGER_free(a);
  27221. #endif
  27222. return EXPECT_RESULT();
  27223. }
  27224. static int test_wolfSSL_ASN1_INTEGER_BN(void)
  27225. {
  27226. EXPECT_DECLS;
  27227. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27228. ASN1_INTEGER* ai = NULL;
  27229. ASN1_INTEGER* ai2 = NULL;
  27230. BIGNUM* bn = NULL;
  27231. BIGNUM* bn2 = NULL;
  27232. ExpectNotNull(ai = ASN1_INTEGER_new());
  27233. ExpectNotNull(bn2 = BN_new());
  27234. /* Invalid parameter testing. */
  27235. ExpectNull(bn = ASN1_INTEGER_to_BN(NULL, NULL));
  27236. ExpectNull(ai2 = BN_to_ASN1_INTEGER(NULL, NULL));
  27237. /* at the moment hard setting since no set function */
  27238. if (ai != NULL) {
  27239. ai->data[0] = 0xff; /* No DER encoding. */
  27240. ai->length = 1;
  27241. }
  27242. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27243. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27244. BN_free(bn);
  27245. bn = NULL;
  27246. #else
  27247. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  27248. #endif
  27249. if (ai != NULL) {
  27250. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27251. ai->data[1] = 0x04; /* bad length of integer */
  27252. ai->data[2] = 0x03;
  27253. ai->length = 3;
  27254. }
  27255. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27256. /* Interpreted as a number 0x020403. */
  27257. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27258. BN_free(bn);
  27259. bn = NULL;
  27260. #else
  27261. ExpectNull(ASN1_INTEGER_to_BN(ai, NULL));
  27262. #endif
  27263. if (ai != NULL) {
  27264. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27265. ai->data[1] = 0x01; /* length of integer */
  27266. ai->data[2] = 0x03;
  27267. ai->length = 3;
  27268. }
  27269. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, NULL));
  27270. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, NULL));
  27271. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27272. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27273. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27274. if (ai != NULL) {
  27275. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27276. ai->data[1] = 0x02; /* length of integer */
  27277. ai->data[2] = 0x00; /* padding byte to ensure positive */
  27278. ai->data[3] = 0xff;
  27279. ai->length = 4;
  27280. }
  27281. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  27282. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  27283. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27284. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27285. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27286. if (ai != NULL) {
  27287. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27288. ai->data[1] = 0x01; /* length of integer */
  27289. ai->data[2] = 0x00;
  27290. ai->length = 3;
  27291. }
  27292. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  27293. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  27294. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27295. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27296. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27297. if (ai != NULL) {
  27298. ai->data[0] = 0x02; /* tag for ASN_INTEGER */
  27299. ai->data[1] = 0x01; /* length of integer */
  27300. ai->data[2] = 0x01;
  27301. ai->length = 3;
  27302. ai->negative = 1;
  27303. }
  27304. ExpectNotNull(bn = ASN1_INTEGER_to_BN(ai, bn));
  27305. ExpectNotNull(ai2 = BN_to_ASN1_INTEGER(bn, ai2));
  27306. ExpectIntEQ(ASN1_INTEGER_cmp(ai, ai2), 0);
  27307. ExpectNotNull(bn2 = ASN1_INTEGER_to_BN(ai2, bn2));
  27308. ExpectIntEQ(BN_cmp(bn, bn2), 0);
  27309. BN_free(bn2);
  27310. BN_free(bn);
  27311. ASN1_INTEGER_free(ai2);
  27312. ASN1_INTEGER_free(ai);
  27313. #endif
  27314. return EXPECT_RESULT();
  27315. }
  27316. static int test_wolfSSL_ASN1_INTEGER_get_set(void)
  27317. {
  27318. EXPECT_DECLS;
  27319. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27320. ASN1_INTEGER *a = NULL;
  27321. long val;
  27322. ExpectNotNull(a = ASN1_INTEGER_new());
  27323. /* Invalid parameter testing. */
  27324. ExpectIntEQ(ASN1_INTEGER_get(NULL), 0);
  27325. #if defined(WOLFSSL_QT) || defined(WOLFSSL_HAPROXY)
  27326. ExpectIntEQ(ASN1_INTEGER_get(a), 0);
  27327. #else
  27328. ExpectIntEQ(ASN1_INTEGER_get(a), -1);
  27329. #endif
  27330. ASN1_INTEGER_free(a);
  27331. a = NULL;
  27332. ExpectNotNull(a = ASN1_INTEGER_new());
  27333. val = 0;
  27334. ExpectIntEQ(ASN1_INTEGER_set(NULL, val), 0);
  27335. ASN1_INTEGER_free(a);
  27336. a = NULL;
  27337. /* 0 */
  27338. ExpectNotNull(a = ASN1_INTEGER_new());
  27339. val = 0;
  27340. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27341. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27342. ASN1_INTEGER_free(a);
  27343. a = NULL;
  27344. /* 40 */
  27345. ExpectNotNull(a = ASN1_INTEGER_new());
  27346. val = 40;
  27347. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27348. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27349. ASN1_INTEGER_free(a);
  27350. a = NULL;
  27351. /* -40 */
  27352. ExpectNotNull(a = ASN1_INTEGER_new());
  27353. val = -40;
  27354. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27355. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27356. ASN1_INTEGER_free(a);
  27357. a = NULL;
  27358. /* 128 */
  27359. ExpectNotNull(a = ASN1_INTEGER_new());
  27360. val = 128;
  27361. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27362. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27363. ASN1_INTEGER_free(a);
  27364. a = NULL;
  27365. /* -128 */
  27366. ExpectNotNull(a = ASN1_INTEGER_new());
  27367. val = -128;
  27368. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27369. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27370. ASN1_INTEGER_free(a);
  27371. a = NULL;
  27372. /* 200 */
  27373. ExpectNotNull(a = ASN1_INTEGER_new());
  27374. val = 200;
  27375. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27376. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27377. ASN1_INTEGER_free(a);
  27378. a = NULL;
  27379. /* int max (2147483647) */
  27380. ExpectNotNull(a = ASN1_INTEGER_new());
  27381. val = 2147483647;
  27382. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27383. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27384. ASN1_INTEGER_free(a);
  27385. a = NULL;
  27386. /* int min (-2147483648) */
  27387. ExpectNotNull(a = ASN1_INTEGER_new());
  27388. val = -2147483647 - 1;
  27389. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27390. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27391. ASN1_INTEGER_free(a);
  27392. a = NULL;
  27393. /* long max positive */
  27394. ExpectNotNull(a = ASN1_INTEGER_new());
  27395. val = (long)(((unsigned long)-1) >> 1);
  27396. ExpectIntEQ(ASN1_INTEGER_set(a, val), 1);
  27397. ExpectTrue(ASN1_INTEGER_get(a) == val);
  27398. ASN1_INTEGER_free(a);
  27399. #endif
  27400. return EXPECT_RESULT();
  27401. }
  27402. #if defined(OPENSSL_EXTRA)
  27403. typedef struct ASN1IntTestVector {
  27404. const byte* der;
  27405. const size_t derSz;
  27406. const long value;
  27407. } ASN1IntTestVector;
  27408. #endif
  27409. static int test_wolfSSL_d2i_ASN1_INTEGER(void)
  27410. {
  27411. EXPECT_DECLS;
  27412. #if defined(OPENSSL_EXTRA)
  27413. size_t i;
  27414. WOLFSSL_ASN1_INTEGER* a = NULL;
  27415. WOLFSSL_ASN1_INTEGER* b = NULL;
  27416. WOLFSSL_ASN1_INTEGER* c = NULL;
  27417. const byte* p = NULL;
  27418. byte* p2 = NULL;
  27419. byte* reEncoded = NULL;
  27420. int reEncodedSz = 0;
  27421. static const byte zeroDer[] = {
  27422. 0x02, 0x01, 0x00
  27423. };
  27424. static const byte oneDer[] = {
  27425. 0x02, 0x01, 0x01
  27426. };
  27427. static const byte negativeDer[] = {
  27428. 0x02, 0x03, 0xC1, 0x16, 0x0D
  27429. };
  27430. static const byte positiveDer[] = {
  27431. 0x02, 0x03, 0x01, 0x00, 0x01
  27432. };
  27433. static const byte primeDer[] = {
  27434. 0x02, 0x82, 0x01, 0x01, 0x00, 0xc0, 0x95, 0x08, 0xe1, 0x57, 0x41,
  27435. 0xf2, 0x71, 0x6d, 0xb7, 0xd2, 0x45, 0x41, 0x27, 0x01, 0x65, 0xc6,
  27436. 0x45, 0xae, 0xf2, 0xbc, 0x24, 0x30, 0xb8, 0x95, 0xce, 0x2f, 0x4e,
  27437. 0xd6, 0xf6, 0x1c, 0x88, 0xbc, 0x7c, 0x9f, 0xfb, 0xa8, 0x67, 0x7f,
  27438. 0xfe, 0x5c, 0x9c, 0x51, 0x75, 0xf7, 0x8a, 0xca, 0x07, 0xe7, 0x35,
  27439. 0x2f, 0x8f, 0xe1, 0xbd, 0x7b, 0xc0, 0x2f, 0x7c, 0xab, 0x64, 0xa8,
  27440. 0x17, 0xfc, 0xca, 0x5d, 0x7b, 0xba, 0xe0, 0x21, 0xe5, 0x72, 0x2e,
  27441. 0x6f, 0x2e, 0x86, 0xd8, 0x95, 0x73, 0xda, 0xac, 0x1b, 0x53, 0xb9,
  27442. 0x5f, 0x3f, 0xd7, 0x19, 0x0d, 0x25, 0x4f, 0xe1, 0x63, 0x63, 0x51,
  27443. 0x8b, 0x0b, 0x64, 0x3f, 0xad, 0x43, 0xb8, 0xa5, 0x1c, 0x5c, 0x34,
  27444. 0xb3, 0xae, 0x00, 0xa0, 0x63, 0xc5, 0xf6, 0x7f, 0x0b, 0x59, 0x68,
  27445. 0x78, 0x73, 0xa6, 0x8c, 0x18, 0xa9, 0x02, 0x6d, 0xaf, 0xc3, 0x19,
  27446. 0x01, 0x2e, 0xb8, 0x10, 0xe3, 0xc6, 0xcc, 0x40, 0xb4, 0x69, 0xa3,
  27447. 0x46, 0x33, 0x69, 0x87, 0x6e, 0xc4, 0xbb, 0x17, 0xa6, 0xf3, 0xe8,
  27448. 0xdd, 0xad, 0x73, 0xbc, 0x7b, 0x2f, 0x21, 0xb5, 0xfd, 0x66, 0x51,
  27449. 0x0c, 0xbd, 0x54, 0xb3, 0xe1, 0x6d, 0x5f, 0x1c, 0xbc, 0x23, 0x73,
  27450. 0xd1, 0x09, 0x03, 0x89, 0x14, 0xd2, 0x10, 0xb9, 0x64, 0xc3, 0x2a,
  27451. 0xd0, 0xa1, 0x96, 0x4a, 0xbc, 0xe1, 0xd4, 0x1a, 0x5b, 0xc7, 0xa0,
  27452. 0xc0, 0xc1, 0x63, 0x78, 0x0f, 0x44, 0x37, 0x30, 0x32, 0x96, 0x80,
  27453. 0x32, 0x23, 0x95, 0xa1, 0x77, 0xba, 0x13, 0xd2, 0x97, 0x73, 0xe2,
  27454. 0x5d, 0x25, 0xc9, 0x6a, 0x0d, 0xc3, 0x39, 0x60, 0xa4, 0xb4, 0xb0,
  27455. 0x69, 0x42, 0x42, 0x09, 0xe9, 0xd8, 0x08, 0xbc, 0x33, 0x20, 0xb3,
  27456. 0x58, 0x22, 0xa7, 0xaa, 0xeb, 0xc4, 0xe1, 0xe6, 0x61, 0x83, 0xc5,
  27457. 0xd2, 0x96, 0xdf, 0xd9, 0xd0, 0x4f, 0xad, 0xd7
  27458. };
  27459. static const byte garbageDer[] = {0xDE, 0xAD, 0xBE, 0xEF};
  27460. static const ASN1IntTestVector testVectors[] = {
  27461. {zeroDer, sizeof(zeroDer), 0},
  27462. {oneDer, sizeof(oneDer), 1},
  27463. {negativeDer, sizeof(negativeDer), -4123123},
  27464. {positiveDer, sizeof(positiveDer), 65537},
  27465. {primeDer, sizeof(primeDer), 0}
  27466. };
  27467. static const size_t NUM_TEST_VECTORS =
  27468. sizeof(testVectors)/sizeof(testVectors[0]);
  27469. /* Check d2i error conditions */
  27470. /* NULL pointer to input. */
  27471. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, NULL, 1)));
  27472. ExpectNull(b);
  27473. /* NULL input. */
  27474. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 1)));
  27475. ExpectNull(b);
  27476. /* 0 length. */
  27477. p = testVectors[0].der;
  27478. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, 0)));
  27479. ExpectNull(b);
  27480. /* Negative length. */
  27481. p = testVectors[0].der;
  27482. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, -1)));
  27483. ExpectNull(b);
  27484. /* Garbage DER input. */
  27485. p = garbageDer;
  27486. ExpectNull((a = wolfSSL_d2i_ASN1_INTEGER(&b, &p, sizeof(garbageDer))));
  27487. ExpectNull(b);
  27488. /* Check i2d error conditions */
  27489. /* NULL input. */
  27490. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(NULL, &p2), 0);
  27491. /* 0 length input data buffer (a->length == 0). */
  27492. ExpectNotNull((a = wolfSSL_ASN1_INTEGER_new()));
  27493. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  27494. if (a != NULL)
  27495. a->data = NULL;
  27496. /* NULL input data buffer. */
  27497. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, &p2), 0);
  27498. if (a != NULL) {
  27499. /* Reset a->data. */
  27500. a->data = a->intData;
  27501. }
  27502. /* Set a to valid value. */
  27503. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(a, 1), WOLFSSL_SUCCESS);
  27504. /* NULL output buffer. */
  27505. ExpectIntLT(wolfSSL_i2d_ASN1_INTEGER(a, NULL), 0);
  27506. wolfSSL_ASN1_INTEGER_free(a);
  27507. a = NULL;
  27508. for (i = 0; i < NUM_TEST_VECTORS; ++i) {
  27509. p = testVectors[i].der;
  27510. ExpectNotNull(a = wolfSSL_d2i_ASN1_INTEGER(&b, &p,
  27511. testVectors[i].derSz));
  27512. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, b), 0);
  27513. if (testVectors[i].derSz <= sizeof(long)) {
  27514. ExpectNotNull(c = wolfSSL_ASN1_INTEGER_new());
  27515. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(c, testVectors[i].value), 1);
  27516. ExpectIntEQ(wolfSSL_ASN1_INTEGER_cmp(a, c), 0);
  27517. wolfSSL_ASN1_INTEGER_free(c);
  27518. c = NULL;
  27519. }
  27520. /* Convert to DER without a pre-allocated output buffer. */
  27521. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &reEncoded)), 0);
  27522. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  27523. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  27524. /* Convert to DER with a pre-allocated output buffer. In this case, the
  27525. * output buffer pointer should be incremented just past the end of the
  27526. * encoded data. */
  27527. p2 = reEncoded;
  27528. ExpectIntGT((reEncodedSz = wolfSSL_i2d_ASN1_INTEGER(a, &p2)), 0);
  27529. ExpectIntEQ(reEncodedSz, testVectors[i].derSz);
  27530. ExpectPtrEq(reEncoded, p2 - reEncodedSz);
  27531. ExpectIntEQ(XMEMCMP(reEncoded, testVectors[i].der, reEncodedSz), 0);
  27532. XFREE(reEncoded, NULL, DYNAMIC_TYPE_ASN1);
  27533. reEncoded = NULL;
  27534. wolfSSL_ASN1_INTEGER_free(a);
  27535. a = NULL;
  27536. }
  27537. #endif /* OPENSSL_EXTRA */
  27538. return EXPECT_RESULT();
  27539. }
  27540. static int test_wolfSSL_a2i_ASN1_INTEGER(void)
  27541. {
  27542. EXPECT_DECLS;
  27543. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  27544. BIO* bio = NULL;
  27545. BIO* out = NULL;
  27546. BIO* fixed = NULL;
  27547. ASN1_INTEGER* ai = NULL;
  27548. char buf[] = "123456\n12345\n1123456789123456\\\n78901234567890 \r\n\n";
  27549. char tmp[1024];
  27550. int tmpSz;
  27551. const char expected1[] = "123456";
  27552. const char expected2[] = "112345678912345678901234567890";
  27553. char longStr[] = "123456781234567812345678123456781234567812345678\n"
  27554. "123456781234567812345678123456781234567812345678\\\n12345678\n";
  27555. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  27556. ExpectNotNull(ai = ASN1_INTEGER_new());
  27557. ExpectNotNull(bio = BIO_new_mem_buf(buf, -1));
  27558. /* Invalid parameter testing. */
  27559. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, -1), 0);
  27560. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, NULL, -1), 0);
  27561. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, NULL, -1), 0);
  27562. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, tmp, -1), 0);
  27563. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, NULL, NULL, 1024), 0);
  27564. ExpectIntEQ(a2i_ASN1_INTEGER(NULL, ai, tmp, 1024), 0);
  27565. ExpectIntEQ(a2i_ASN1_INTEGER(bio, NULL, tmp, 1024), 0);
  27566. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, NULL, 1024), 0);
  27567. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, -1), 0);
  27568. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, NULL), 0);
  27569. ExpectIntEQ(i2a_ASN1_INTEGER(bio, NULL), 0);
  27570. ExpectIntEQ(i2a_ASN1_INTEGER(NULL, ai), 0);
  27571. /* No data to read from BIO. */
  27572. ExpectIntEQ(a2i_ASN1_INTEGER(out, ai, tmp, 1024), 0);
  27573. /* read first line */
  27574. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27575. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 6);
  27576. XMEMSET(tmp, 0, 1024);
  27577. tmpSz = BIO_read(out, tmp, 1024);
  27578. ExpectIntEQ(tmpSz, 6);
  27579. ExpectIntEQ(XMEMCMP(tmp, expected1, tmpSz), 0);
  27580. /* fail on second line (not % 2) */
  27581. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  27582. /* read 3rd long line */
  27583. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27584. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 30);
  27585. XMEMSET(tmp, 0, 1024);
  27586. tmpSz = BIO_read(out, tmp, 1024);
  27587. ExpectIntEQ(tmpSz, 30);
  27588. ExpectIntEQ(XMEMCMP(tmp, expected2, tmpSz), 0);
  27589. /* fail on empty line */
  27590. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 0);
  27591. BIO_free(bio);
  27592. bio = NULL;
  27593. /* Make long integer, requiring dynamic memory, even longer. */
  27594. ExpectNotNull(bio = BIO_new_mem_buf(longStr, -1));
  27595. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27596. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 48);
  27597. XMEMSET(tmp, 0, 1024);
  27598. tmpSz = BIO_read(out, tmp, 1024);
  27599. ExpectIntEQ(tmpSz, 48);
  27600. ExpectIntEQ(a2i_ASN1_INTEGER(bio, ai, tmp, 1024), 1);
  27601. ExpectIntEQ(i2a_ASN1_INTEGER(out, ai), 56);
  27602. XMEMSET(tmp, 0, 1024);
  27603. tmpSz = BIO_read(out, tmp, 1024);
  27604. ExpectIntEQ(tmpSz, 56);
  27605. ExpectIntEQ(wolfSSL_ASN1_INTEGER_set(ai, 1), 1);
  27606. BIO_free(bio);
  27607. BIO_free(out);
  27608. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  27609. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27610. /* Ensure there is 0 bytes available to write into. */
  27611. ExpectIntEQ(BIO_write(fixed, tmp, 1), 1);
  27612. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  27613. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  27614. ExpectIntEQ(i2a_ASN1_INTEGER(fixed, ai), 0);
  27615. BIO_free(fixed);
  27616. ASN1_INTEGER_free(ai);
  27617. #endif
  27618. return EXPECT_RESULT();
  27619. }
  27620. static int test_wolfSSL_i2c_ASN1_INTEGER(void)
  27621. {
  27622. EXPECT_DECLS;
  27623. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  27624. ASN1_INTEGER *a = NULL;
  27625. unsigned char *pp = NULL,*tpp = NULL;
  27626. int ret = 0;
  27627. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  27628. /* Invalid parameter testing. */
  27629. /* Set pp to an invalid value. */
  27630. pp = NULL;
  27631. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, &pp), 0);
  27632. ExpectIntEQ(i2c_ASN1_INTEGER(a, &pp), 0);
  27633. ExpectIntEQ(i2c_ASN1_INTEGER(NULL, NULL), 0);
  27634. /* 40 */
  27635. if (a != NULL) {
  27636. a->intData[0] = ASN_INTEGER;
  27637. a->intData[1] = 1;
  27638. a->intData[2] = 40;
  27639. }
  27640. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27641. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27642. DYNAMIC_TYPE_TMP_BUFFER));
  27643. tpp = pp;
  27644. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27645. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27646. tpp--;
  27647. ExpectIntEQ(*tpp, 40);
  27648. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27649. pp = NULL;
  27650. /* 128 */
  27651. if (a != NULL) {
  27652. a->intData[0] = ASN_INTEGER;
  27653. a->intData[1] = 1;
  27654. a->intData[2] = 128;
  27655. }
  27656. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27657. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27658. DYNAMIC_TYPE_TMP_BUFFER));
  27659. tpp = pp;
  27660. if (tpp != NULL) {
  27661. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27662. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27663. tpp--;
  27664. ExpectIntEQ(*(tpp--), 128);
  27665. ExpectIntEQ(*tpp, 0);
  27666. }
  27667. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27668. pp = NULL;
  27669. /* -40 */
  27670. if (a != NULL) {
  27671. a->intData[0] = ASN_INTEGER;
  27672. a->intData[1] = 1;
  27673. a->intData[2] = 40;
  27674. a->negative = 1;
  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, 216);
  27685. }
  27686. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27687. pp = NULL;
  27688. /* -128 */
  27689. if (a != NULL) {
  27690. a->intData[0] = ASN_INTEGER;
  27691. a->intData[1] = 1;
  27692. a->intData[2] = 128;
  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. tpp = pp;
  27699. if (tpp != NULL) {
  27700. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27701. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27702. tpp--;
  27703. ExpectIntEQ(*tpp, 128);
  27704. }
  27705. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27706. pp = NULL;
  27707. /* -200 */
  27708. if (a != NULL) {
  27709. a->intData[0] = ASN_INTEGER;
  27710. a->intData[1] = 1;
  27711. a->intData[2] = 200;
  27712. a->negative = 1;
  27713. }
  27714. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27715. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27716. DYNAMIC_TYPE_TMP_BUFFER));
  27717. tpp = pp;
  27718. if (tpp != NULL) {
  27719. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27720. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27721. tpp--;
  27722. ExpectIntEQ(*(tpp--), 56);
  27723. ExpectIntEQ(*tpp, 255);
  27724. }
  27725. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27726. pp = NULL;
  27727. /* Empty */
  27728. if (a != NULL) {
  27729. a->intData[0] = ASN_INTEGER;
  27730. a->intData[1] = 0;
  27731. a->negative = 0;
  27732. }
  27733. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27734. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27735. DYNAMIC_TYPE_TMP_BUFFER));
  27736. tpp = pp;
  27737. if (tpp != NULL) {
  27738. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27739. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27740. tpp--;
  27741. ExpectIntEQ(*tpp, 0);
  27742. }
  27743. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27744. pp = NULL;
  27745. /* 0 */
  27746. if (a != NULL) {
  27747. a->intData[0] = ASN_INTEGER;
  27748. a->intData[1] = 1;
  27749. a->intData[2] = 0;
  27750. a->negative = 1;
  27751. }
  27752. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 1);
  27753. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27754. DYNAMIC_TYPE_TMP_BUFFER));
  27755. if (tpp != NULL) {
  27756. tpp = pp;
  27757. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27758. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 1);
  27759. tpp--;
  27760. ExpectIntEQ(*tpp, 0);
  27761. }
  27762. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27763. pp = NULL;
  27764. /* 0x100 */
  27765. if (a != NULL) {
  27766. a->intData[0] = ASN_INTEGER;
  27767. a->intData[1] = 2;
  27768. a->intData[2] = 0x01;
  27769. a->intData[3] = 0x00;
  27770. a->negative = 0;
  27771. }
  27772. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27773. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27774. DYNAMIC_TYPE_TMP_BUFFER));
  27775. if (tpp != NULL) {
  27776. tpp = pp;
  27777. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27778. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27779. tpp -= 2;
  27780. ExpectIntEQ(tpp[0], 0x01);
  27781. ExpectIntEQ(tpp[1], 0x00);
  27782. }
  27783. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27784. pp = NULL;
  27785. /* -0x8000 => 0x8000 */
  27786. if (a != NULL) {
  27787. a->intData[0] = ASN_INTEGER;
  27788. a->intData[1] = 2;
  27789. a->intData[2] = 0x80;
  27790. a->intData[3] = 0x00;
  27791. a->negative = 1;
  27792. }
  27793. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 2);
  27794. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27795. DYNAMIC_TYPE_TMP_BUFFER));
  27796. tpp = pp;
  27797. if (tpp != NULL) {
  27798. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27799. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 2);
  27800. tpp -= 2;
  27801. ExpectIntEQ(tpp[0], 0x80);
  27802. ExpectIntEQ(tpp[1], 0x00);
  27803. }
  27804. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27805. pp = NULL;
  27806. /* -0x8001 => 0xFF7FFF */
  27807. if (a != NULL) {
  27808. a->intData[0] = ASN_INTEGER;
  27809. a->intData[1] = 2;
  27810. a->intData[2] = 0x80;
  27811. a->intData[3] = 0x01;
  27812. a->negative = 1;
  27813. }
  27814. ExpectIntEQ(ret = i2c_ASN1_INTEGER(a, NULL), 3);
  27815. ExpectNotNull(pp = (unsigned char*)XMALLOC(ret + 1, NULL,
  27816. DYNAMIC_TYPE_TMP_BUFFER));
  27817. tpp = pp;
  27818. if (tpp != NULL) {
  27819. ExpectNotNull(XMEMSET(tpp, 0, ret + 1));
  27820. ExpectIntEQ(i2c_ASN1_INTEGER(a, &tpp), 3);
  27821. tpp -= 3;
  27822. ExpectIntEQ(tpp[0], 0xFF);
  27823. ExpectIntEQ(tpp[1], 0x7F);
  27824. ExpectIntEQ(tpp[2], 0xFF);
  27825. }
  27826. XFREE(pp, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  27827. wolfSSL_ASN1_INTEGER_free(a);
  27828. #endif /* OPENSSL_EXTRA && !NO_ASN */
  27829. return EXPECT_RESULT();
  27830. }
  27831. static int test_wolfSSL_ASN1_OBJECT(void)
  27832. {
  27833. EXPECT_DECLS;
  27834. #if defined(OPENSSL_EXTRA)
  27835. ASN1_OBJECT* a = NULL;
  27836. ASN1_OBJECT s;
  27837. const unsigned char der[] = { 0x06, 0x01, 0x00 };
  27838. /* Invalid parameter testing. */
  27839. ASN1_OBJECT_free(NULL);
  27840. ExpectNull(wolfSSL_ASN1_OBJECT_dup(NULL));
  27841. /* Test that a static ASN1_OBJECT can be freed. */
  27842. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  27843. ASN1_OBJECT_free(&s);
  27844. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  27845. ASN1_OBJECT_free(a);
  27846. a = NULL;
  27847. s.obj = der;
  27848. s.objSz = sizeof(der);
  27849. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_dup(&s));
  27850. ASN1_OBJECT_free(a);
  27851. ASN1_OBJECT_free(&s);
  27852. #endif /* OPENSSL_EXTRA */
  27853. return EXPECT_RESULT();
  27854. }
  27855. static int test_wolfSSL_ASN1_get_object(void)
  27856. {
  27857. EXPECT_DECLS;
  27858. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  27859. const unsigned char* derBuf = cliecc_cert_der_256;
  27860. const unsigned char* nullPtr = NULL;
  27861. const unsigned char objDerInvalidLen[] = { 0x30, 0x81 };
  27862. const unsigned char objDerBadLen[] = { 0x30, 0x04 };
  27863. const unsigned char objDerNotObj[] = { 0x02, 0x01, 0x00 };
  27864. const unsigned char objDerNoData[] = { 0x06, 0x00 };
  27865. const unsigned char* p;
  27866. unsigned char objDer[8];
  27867. unsigned char* der;
  27868. unsigned char* derPtr;
  27869. int len = sizeof_cliecc_cert_der_256;
  27870. long asnLen = 0;
  27871. int tag = 0;
  27872. int cls = 0;
  27873. ASN1_OBJECT* a = NULL;
  27874. ASN1_OBJECT s;
  27875. XMEMSET(&s, 0, sizeof(ASN1_OBJECT));
  27876. /* Invalid encoding at length. */
  27877. p = objDerInvalidLen;
  27878. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  27879. 0x80);
  27880. p = objDerBadLen;
  27881. /* Error = 0x80, Constructed = 0x20 */
  27882. ExpectIntEQ(ASN1_get_object(&p, &asnLen, &tag, &cls, sizeof(objDerBadLen)),
  27883. 0x80 | 0x20);
  27884. /* Read a couple TLV triplets and make sure they match the expected values
  27885. */
  27886. /* SEQUENCE */
  27887. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, len) & 0x80, 0);
  27888. ExpectIntEQ(asnLen, 861);
  27889. ExpectIntEQ(tag, 0x10);
  27890. ExpectIntEQ(cls, 0);
  27891. /* SEQUENCE */
  27892. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27893. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27894. ExpectIntEQ(asnLen, 772);
  27895. ExpectIntEQ(tag, 0x10);
  27896. ExpectIntEQ(cls, 0);
  27897. /* [0] */
  27898. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27899. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27900. ExpectIntEQ(asnLen, 3);
  27901. ExpectIntEQ(tag, 0);
  27902. ExpectIntEQ(cls, 0x80);
  27903. /* INTEGER */
  27904. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27905. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27906. ExpectIntEQ(asnLen, 1);
  27907. ExpectIntEQ(tag, 0x2);
  27908. ExpectIntEQ(cls, 0);
  27909. derBuf += asnLen;
  27910. /* INTEGER */
  27911. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27912. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27913. ExpectIntEQ(asnLen, 20);
  27914. ExpectIntEQ(tag, 0x2);
  27915. ExpectIntEQ(cls, 0);
  27916. derBuf += asnLen;
  27917. /* SEQUENCE */
  27918. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls,
  27919. len - (derBuf - cliecc_cert_der_256)) & 0x80, 0);
  27920. ExpectIntEQ(asnLen, 10);
  27921. ExpectIntEQ(tag, 0x10);
  27922. ExpectIntEQ(cls, 0);
  27923. /* Found OBJECT_ID. */
  27924. /* Invalid parameter testing. */
  27925. ExpectIntEQ(ASN1_get_object(NULL, NULL, NULL, NULL, 0), 0x80);
  27926. ExpectIntEQ(ASN1_get_object(&nullPtr, NULL, NULL, NULL, 0), 0x80);
  27927. ExpectIntEQ(ASN1_get_object(NULL, &asnLen, &tag, &cls, len), 0x80);
  27928. ExpectIntEQ(ASN1_get_object(&nullPtr, &asnLen, &tag, &cls, len), 0x80);
  27929. ExpectIntEQ(ASN1_get_object(&derBuf, NULL, &tag, &cls, len), 0x80);
  27930. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, NULL, &cls, len), 0x80);
  27931. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, NULL, len), 0x80);
  27932. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, 0), 0x80);
  27933. ExpectIntEQ(ASN1_get_object(&derBuf, &asnLen, &tag, &cls, -1), 0x80);
  27934. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, -1));
  27935. ExpectNull(d2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  27936. ExpectNull(d2i_ASN1_OBJECT(NULL, &derBuf, -1));
  27937. ExpectNull(d2i_ASN1_OBJECT(NULL, NULL, 0));
  27938. ExpectNull(d2i_ASN1_OBJECT(&a, NULL, len));
  27939. ExpectNull(d2i_ASN1_OBJECT(&a, &nullPtr, len));
  27940. ExpectNull(d2i_ASN1_OBJECT(&a, &derBuf, -1));
  27941. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, -1));
  27942. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, -1));
  27943. ExpectNull(c2i_ASN1_OBJECT(NULL, &derBuf, -1));
  27944. ExpectNull(c2i_ASN1_OBJECT(NULL, NULL, 1));
  27945. ExpectNull(c2i_ASN1_OBJECT(NULL, &nullPtr, 1));
  27946. /* Invalid encoding at length. */
  27947. p = objDerInvalidLen;
  27948. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerInvalidLen)));
  27949. p = objDerBadLen;
  27950. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerBadLen)));
  27951. p = objDerNotObj;
  27952. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNotObj)));
  27953. p = objDerNoData;
  27954. ExpectNull(d2i_ASN1_OBJECT(&a, &p, sizeof(objDerNoData)));
  27955. /* Create an ASN OBJECT from content */
  27956. p = derBuf + 2;
  27957. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 8));
  27958. ASN1_OBJECT_free(a);
  27959. a = NULL;
  27960. /* Create an ASN OBJECT from DER */
  27961. ExpectNotNull(d2i_ASN1_OBJECT(&a, &derBuf, len));
  27962. /* Invalid parameter testing. */
  27963. ExpectIntEQ(i2d_ASN1_OBJECT(NULL, NULL), 0);
  27964. ExpectIntEQ(i2d_ASN1_OBJECT(&s, NULL), 0);
  27965. ExpectIntEQ(i2d_ASN1_OBJECT(a, NULL), 8);
  27966. der = NULL;
  27967. ExpectIntEQ(i2d_ASN1_OBJECT(a, &der), 8);
  27968. derPtr = objDer;
  27969. ExpectIntEQ(i2d_ASN1_OBJECT(a, &derPtr), 8);
  27970. ExpectPtrNE(derPtr, objDer);
  27971. ExpectIntEQ(XMEMCMP(der, objDer, 8), 0);
  27972. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  27973. ASN1_OBJECT_free(a);
  27974. #endif /* OPENSSL_EXTRA && HAVE_ECC && USE_CERT_BUFFERS_256 */
  27975. return EXPECT_RESULT();
  27976. }
  27977. static int test_wolfSSL_i2a_ASN1_OBJECT(void)
  27978. {
  27979. EXPECT_DECLS;
  27980. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  27981. ASN1_OBJECT* obj = NULL;
  27982. ASN1_OBJECT* a = NULL;
  27983. BIO *bio = NULL;
  27984. const unsigned char notObjDer[] = { 0x04, 0x01, 0xff };
  27985. const unsigned char badLenDer[] = { 0x06, 0x04, 0x01 };
  27986. const unsigned char goodDer[] = { 0x06, 0x01, 0x01 };
  27987. const unsigned char* p;
  27988. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  27989. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  27990. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, obj), 0);
  27991. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, NULL), 0);
  27992. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(NULL, obj), 0);
  27993. /* No DER encoding in ASN1_OBJECT. */
  27994. ExpectNotNull(a = wolfSSL_ASN1_OBJECT_new());
  27995. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  27996. ASN1_OBJECT_free(a);
  27997. a = NULL;
  27998. /* DER encoding - not OBJECT_ID */
  27999. p = notObjDer;
  28000. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  28001. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  28002. ASN1_OBJECT_free(a);
  28003. a = NULL;
  28004. /* Bad length encoding. */
  28005. p = badLenDer;
  28006. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  28007. ExpectIntEQ(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  28008. ASN1_OBJECT_free(a);
  28009. a = NULL;
  28010. /* Good encoding - but unknown. */
  28011. p = goodDer;
  28012. ExpectNotNull(a = c2i_ASN1_OBJECT(NULL, &p, 3));
  28013. ExpectIntGT(wolfSSL_i2a_ASN1_OBJECT(bio, a), 0);
  28014. ASN1_OBJECT_free(a);
  28015. BIO_free(bio);
  28016. ASN1_OBJECT_free(obj);
  28017. #endif
  28018. return EXPECT_RESULT();
  28019. }
  28020. static int test_wolfSSL_i2t_ASN1_OBJECT(void)
  28021. {
  28022. EXPECT_DECLS;
  28023. #if defined(OPENSSL_EXTRA) && \
  28024. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN)
  28025. char buf[50] = {0};
  28026. ASN1_OBJECT* obj;
  28027. const char* oid = "2.5.29.19";
  28028. const char* ln = "X509v3 Basic Constraints";
  28029. obj = NULL;
  28030. ExpectIntEQ(i2t_ASN1_OBJECT(NULL, sizeof(buf), obj), 0);
  28031. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), NULL), 0);
  28032. ExpectIntEQ(i2t_ASN1_OBJECT(buf, 0, NULL), 0);
  28033. ExpectNotNull(obj = OBJ_txt2obj(oid, 0));
  28034. XMEMSET(buf, 0, sizeof(buf));
  28035. ExpectIntEQ(i2t_ASN1_OBJECT(buf, sizeof(buf), obj), XSTRLEN(ln));
  28036. ExpectIntEQ(XSTRNCMP(buf, ln, XSTRLEN(ln)), 0);
  28037. ASN1_OBJECT_free(obj);
  28038. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_EXT && WOLFSSL_CERT_GEN */
  28039. return EXPECT_RESULT();
  28040. }
  28041. static int test_wolfSSL_sk_ASN1_OBJECT(void)
  28042. {
  28043. EXPECT_DECLS;
  28044. #if !defined(NO_ASN) && (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL))
  28045. WOLFSSL_STACK* sk = NULL;
  28046. WOLFSSL_ASN1_OBJECT* obj;
  28047. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  28048. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  28049. wolfSSL_sk_ASN1_OBJECT_free(sk);
  28050. sk = NULL;
  28051. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  28052. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, NULL), 0);
  28053. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, NULL), 0);
  28054. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(NULL, obj), 0);
  28055. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  28056. wolfSSL_sk_ASN1_OBJECT_pop_free(sk, NULL);
  28057. sk = NULL;
  28058. /* obj freed in pop_free call. */
  28059. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  28060. ExpectNotNull(sk = wolfSSL_sk_new_asn1_obj());
  28061. ExpectIntEQ(wolfSSL_sk_ASN1_OBJECT_push(sk, obj), 1);
  28062. ExpectPtrEq(obj, wolfSSL_sk_ASN1_OBJECT_pop(sk));
  28063. wolfSSL_sk_ASN1_OBJECT_free(sk);
  28064. wolfSSL_ASN1_OBJECT_free(obj);
  28065. #endif /* !NO_ASN && (OPENSSL_EXTRA || WOLFSSL_WPAS_SMALL) */
  28066. return EXPECT_RESULT();
  28067. }
  28068. static int test_wolfSSL_ASN1_STRING(void)
  28069. {
  28070. EXPECT_DECLS;
  28071. #if defined(OPENSSL_EXTRA)
  28072. ASN1_STRING* str = NULL;
  28073. ASN1_STRING* c = NULL;
  28074. const char data[] = "hello wolfSSL";
  28075. const char data2[] = "Same len data";
  28076. const char longData[] =
  28077. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  28078. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28079. ASN1_STRING_free(str);
  28080. str = NULL;
  28081. ExpectNotNull(str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28082. ExpectIntEQ(ASN1_STRING_type(str), V_ASN1_OCTET_STRING);
  28083. ExpectIntEQ(ASN1_STRING_type(NULL), 0);
  28084. /* Check setting to NULL works. */
  28085. ExpectIntEQ(ASN1_STRING_set(str, NULL, 0), 1);
  28086. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, sizeof(data)), 1);
  28087. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  28088. ExpectIntEQ(ASN1_STRING_set(str, NULL, -1), 0);
  28089. ExpectIntEQ(ASN1_STRING_set(NULL, NULL, 0), 0);
  28090. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, NULL), 0);
  28091. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(str, NULL), 0);
  28092. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(NULL, str), 0);
  28093. ExpectNull(wolfSSL_ASN1_STRING_dup(NULL));
  28094. ExpectNotNull(c = wolfSSL_ASN1_STRING_dup(str));
  28095. ExpectIntEQ(ASN1_STRING_cmp(NULL, NULL), -1);
  28096. ExpectIntEQ(ASN1_STRING_cmp(str, NULL), -1);
  28097. ExpectIntEQ(ASN1_STRING_cmp(NULL, c), -1);
  28098. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  28099. ExpectIntEQ(ASN1_STRING_set(c, (const void*)data2, -1), 1);
  28100. ExpectIntGT(ASN1_STRING_cmp(str, c), 0);
  28101. ExpectIntEQ(ASN1_STRING_set(str, (const void*)longData, -1), 1);
  28102. ExpectIntEQ(wolfSSL_ASN1_STRING_copy(c, str), 1);
  28103. ExpectIntEQ(ASN1_STRING_cmp(str, c), 0);
  28104. /* Check setting back to smaller size frees dynamic data. */
  28105. ExpectIntEQ(ASN1_STRING_set(str, (const void*)data, -1), 1);
  28106. ExpectIntLT(ASN1_STRING_cmp(str, c), 0);
  28107. ExpectIntGT(ASN1_STRING_cmp(c, str), 0);
  28108. ExpectNull(ASN1_STRING_get0_data(NULL));
  28109. ExpectNotNull(ASN1_STRING_get0_data(str));
  28110. ExpectNull(ASN1_STRING_data(NULL));
  28111. ExpectNotNull(ASN1_STRING_data(str));
  28112. ExpectIntEQ(ASN1_STRING_length(NULL), 0);
  28113. ExpectIntGT(ASN1_STRING_length(str), 0);
  28114. ASN1_STRING_free(c);
  28115. ASN1_STRING_free(str);
  28116. ASN1_STRING_free(NULL);
  28117. #ifndef NO_WOLFSSL_STUB
  28118. ExpectNull(d2i_DISPLAYTEXT(NULL, NULL, 0));
  28119. #endif
  28120. #endif
  28121. return EXPECT_RESULT();
  28122. }
  28123. static int test_wolfSSL_ASN1_STRING_to_UTF8(void)
  28124. {
  28125. EXPECT_DECLS;
  28126. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_RSA) && \
  28127. !defined(NO_FILESYSTEM)
  28128. WOLFSSL_X509* x509 = NULL;
  28129. WOLFSSL_X509_NAME* subject = NULL;
  28130. WOLFSSL_X509_NAME_ENTRY* e = NULL;
  28131. WOLFSSL_ASN1_STRING* a = NULL;
  28132. FILE* file = XBADFILE;
  28133. int idx = 0;
  28134. char targetOutput[16] = "www.wolfssl.com";
  28135. unsigned char* actual_output = NULL;
  28136. int len = 0;
  28137. ExpectNotNull(file = fopen("./certs/server-cert.pem", "rb"));
  28138. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  28139. if (file != NULL)
  28140. fclose(file);
  28141. /* wolfSSL_ASN1_STRING_to_UTF8(): NID_commonName */
  28142. ExpectNotNull(subject = wolfSSL_X509_get_subject_name(x509));
  28143. ExpectIntEQ((idx = wolfSSL_X509_NAME_get_index_by_NID(subject,
  28144. NID_commonName, -1)), 5);
  28145. ExpectNotNull(e = wolfSSL_X509_NAME_get_entry(subject, idx));
  28146. ExpectNotNull(a = wolfSSL_X509_NAME_ENTRY_get_data(e));
  28147. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a)), 15);
  28148. ExpectIntEQ(strncmp((const char*)actual_output, targetOutput, (size_t)len), 0);
  28149. a = NULL;
  28150. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, valid) */
  28151. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, a)), -1);
  28152. /* wolfSSL_ASN1_STRING_to_UTF8(valid, NULL) */
  28153. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(&actual_output, NULL)), -1);
  28154. /* wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL) */
  28155. ExpectIntEQ((len = wolfSSL_ASN1_STRING_to_UTF8(NULL, NULL)), -1);
  28156. wolfSSL_X509_free(x509);
  28157. XFREE(actual_output, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28158. ExpectNotNull(a = ASN1_STRING_new());
  28159. ExpectIntEQ(wolfSSL_ASN1_STRING_to_UTF8(&actual_output, a), -1);
  28160. ASN1_STRING_free(a);
  28161. #endif
  28162. return EXPECT_RESULT();
  28163. }
  28164. static int test_wolfSSL_i2s_ASN1_STRING(void)
  28165. {
  28166. EXPECT_DECLS;
  28167. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN)
  28168. WOLFSSL_ASN1_STRING* str = NULL;
  28169. const char* data = "test_wolfSSL_i2s_ASN1_STRING";
  28170. char* ret = NULL;
  28171. ExpectNotNull(str = ASN1_STRING_new());
  28172. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, NULL));
  28173. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28174. ret = NULL;
  28175. /* No data. */
  28176. ExpectNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  28177. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28178. ret = NULL;
  28179. ExpectIntEQ(ASN1_STRING_set(str, data, 0), 1);
  28180. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  28181. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28182. ret = NULL;
  28183. ExpectIntEQ(ASN1_STRING_set(str, data, -1), 1);
  28184. /* No type. */
  28185. ExpectNotNull(ret = wolfSSL_i2s_ASN1_STRING(NULL, str));
  28186. XFREE(ret, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28187. ASN1_STRING_free(str);
  28188. #endif
  28189. return EXPECT_RESULT();
  28190. }
  28191. static int test_wolfSSL_ASN1_STRING_canon(void)
  28192. {
  28193. EXPECT_DECLS;
  28194. #if defined(WOLFSSL_TEST_STATIC_BUILD)
  28195. #if !defined(NO_CERTS) && (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA) || \
  28196. defined(OPENSSL_EXTRA_X509_SMALL))
  28197. WOLFSSL_ASN1_STRING* orig = NULL;
  28198. WOLFSSL_ASN1_STRING* canon = NULL;
  28199. const char* data = "test_wolfSSL_ASN1_STRING_canon";
  28200. const char* whitespaceOnly = "\t\r\n";
  28201. const char* modData = " \x01\f\t\x02\r\n\v\xff\nTt \n";
  28202. const char* canonData = "\x01 \x02 \xff tt";
  28203. const char longData[] =
  28204. "This string must be longer than CTC_NAME_SIZE that is defined as 64.";
  28205. ExpectNotNull(orig = ASN1_STRING_new());
  28206. ExpectNotNull(canon = ASN1_STRING_new());
  28207. /* Invalid parameter testing. */
  28208. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, NULL), BAD_FUNC_ARG);
  28209. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, NULL), BAD_FUNC_ARG);
  28210. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(NULL, orig), BAD_FUNC_ARG);
  28211. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28212. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28213. ExpectIntEQ(ASN1_STRING_set(orig, longData, (int)XSTRLEN(data)), 1);
  28214. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28215. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28216. ExpectIntEQ(ASN1_STRING_set(orig, data, (int)XSTRLEN(data)), 1);
  28217. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28218. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28219. ASN1_STRING_free(orig);
  28220. orig = NULL;
  28221. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  28222. ExpectIntEQ(ASN1_STRING_set(orig, modData, 15), 1);
  28223. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28224. ExpectIntEQ(ASN1_STRING_set(orig, canonData, 8), 1);
  28225. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28226. ASN1_STRING_free(orig);
  28227. orig = NULL;
  28228. ExpectNotNull(orig = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  28229. ExpectIntEQ(ASN1_STRING_set(orig, whitespaceOnly, 3), 1);
  28230. ExpectIntEQ(wolfSSL_ASN1_STRING_canon(canon, orig), 1);
  28231. ASN1_STRING_free(orig);
  28232. orig = NULL;
  28233. ExpectNotNull(orig = ASN1_STRING_type_new(MBSTRING_UTF8));
  28234. ExpectIntEQ(ASN1_STRING_cmp(orig, canon), 0);
  28235. ASN1_STRING_free(orig);
  28236. ASN1_STRING_free(canon);
  28237. #endif
  28238. #endif
  28239. return EXPECT_RESULT();
  28240. }
  28241. static int test_wolfSSL_ASN1_STRING_print(void)
  28242. {
  28243. EXPECT_DECLS;
  28244. #if defined(OPENSSL_ALL) && !defined(NO_ASN) && !defined(NO_CERTS) && \
  28245. !defined(NO_BIO)
  28246. ASN1_STRING* asnStr = NULL;
  28247. const char HELLO_DATA[]= \
  28248. {'H','e','l','l','o',' ','w','o','l','f','S','S','L','!'};
  28249. #define MAX_UNPRINTABLE_CHAR 32
  28250. #define MAX_BUF 255
  28251. unsigned char unprintableData[MAX_UNPRINTABLE_CHAR + sizeof(HELLO_DATA)];
  28252. unsigned char expected[sizeof(unprintableData)+1];
  28253. unsigned char rbuf[MAX_BUF];
  28254. BIO *bio = NULL;
  28255. int p_len;
  28256. int i;
  28257. /* setup */
  28258. for (i = 0; i < (int)sizeof(HELLO_DATA); i++) {
  28259. unprintableData[i] = (unsigned char)HELLO_DATA[i];
  28260. expected[i] = (unsigned char)HELLO_DATA[i];
  28261. }
  28262. for (i = 0; i < (int)MAX_UNPRINTABLE_CHAR; i++) {
  28263. unprintableData[sizeof(HELLO_DATA)+i] = i;
  28264. if (i == (int)'\n' || i == (int)'\r')
  28265. expected[sizeof(HELLO_DATA)+i] = i;
  28266. else
  28267. expected[sizeof(HELLO_DATA)+i] = '.';
  28268. }
  28269. unprintableData[sizeof(unprintableData)-1] = '\0';
  28270. expected[sizeof(expected)-1] = '\0';
  28271. XMEMSET(rbuf, 0, MAX_BUF);
  28272. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28273. ExpectIntEQ(BIO_set_write_buf_size(bio, MAX_BUF), 0);
  28274. ExpectNotNull(asnStr = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28275. ExpectIntEQ(ASN1_STRING_set(asnStr,(const void*)unprintableData,
  28276. (int)sizeof(unprintableData)), 1);
  28277. /* test */
  28278. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, NULL), 0);
  28279. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, NULL), 0);
  28280. ExpectIntEQ(wolfSSL_ASN1_STRING_print(NULL, asnStr), 0);
  28281. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print(bio, asnStr), 46);
  28282. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 46), 46);
  28283. ExpectStrEQ((char*)rbuf, (const char*)expected);
  28284. BIO_free(bio);
  28285. bio = NULL;
  28286. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28287. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28288. /* Ensure there is 0 bytes available to write into. */
  28289. ExpectIntEQ(BIO_write(bio, rbuf, 1), 1);
  28290. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28291. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28292. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28293. ExpectIntEQ(BIO_set_write_buf_size(bio, 45), 1);
  28294. ExpectIntEQ(wolfSSL_ASN1_STRING_print(bio, asnStr), 0);
  28295. BIO_free(bio);
  28296. ASN1_STRING_free(asnStr);
  28297. #endif /* OPENSSL_EXTRA && !NO_ASN && !NO_CERTS && !NO_BIO */
  28298. return EXPECT_RESULT();
  28299. }
  28300. static int test_wolfSSL_ASN1_STRING_print_ex(void)
  28301. {
  28302. EXPECT_DECLS;
  28303. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(NO_BIO)
  28304. ASN1_STRING* asn_str = NULL;
  28305. const char data[] = "Hello wolfSSL!";
  28306. ASN1_STRING* esc_str = NULL;
  28307. const char esc_data[] = "a+;<>";
  28308. ASN1_STRING* neg_int = NULL;
  28309. const char neg_int_data[] = "\xff";
  28310. ASN1_STRING* neg_enum = NULL;
  28311. const char neg_enum_data[] = "\xff";
  28312. BIO *bio = NULL;
  28313. BIO *fixed = NULL;
  28314. unsigned long flags;
  28315. int p_len;
  28316. unsigned char rbuf[255];
  28317. /* setup */
  28318. XMEMSET(rbuf, 0, 255);
  28319. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28320. ExpectIntEQ(BIO_set_write_buf_size(bio, 255), 0);
  28321. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28322. ExpectNotNull(asn_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28323. ExpectIntEQ(ASN1_STRING_set(asn_str, (const void*)data, sizeof(data)), 1);
  28324. ExpectNotNull(esc_str = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28325. ExpectIntEQ(ASN1_STRING_set(esc_str, (const void*)esc_data,
  28326. sizeof(esc_data)), 1);
  28327. ExpectNotNull(neg_int = ASN1_STRING_type_new(V_ASN1_NEG_INTEGER));
  28328. ExpectIntEQ(ASN1_STRING_set(neg_int, (const void*)neg_int_data,
  28329. sizeof(neg_int_data) - 1), 1);
  28330. ExpectNotNull(neg_enum = ASN1_STRING_type_new(V_ASN1_NEG_ENUMERATED));
  28331. ExpectIntEQ(ASN1_STRING_set(neg_enum, (const void*)neg_enum_data,
  28332. sizeof(neg_enum_data) - 1), 1);
  28333. /* Invalid parameter testing. */
  28334. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, NULL, 0), 0);
  28335. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(bio, NULL, 0), 0);
  28336. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(NULL, asn_str, 0), 0);
  28337. /* no flags */
  28338. XMEMSET(rbuf, 0, 255);
  28339. flags = 0;
  28340. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 15);
  28341. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 15), 15);
  28342. ExpectStrEQ((char*)rbuf, "Hello wolfSSL!");
  28343. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28344. /* Ensure there is 0 bytes available to write into. */
  28345. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28346. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28347. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28348. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28349. ExpectIntEQ(BIO_set_write_buf_size(fixed, 14), 1);
  28350. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28351. /* RFC2253 Escape */
  28352. XMEMSET(rbuf, 0, 255);
  28353. flags = ASN1_STRFLGS_ESC_2253;
  28354. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, esc_str, flags), 9);
  28355. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 9), 9);
  28356. ExpectStrEQ((char*)rbuf, "a\\+\\;\\<\\>");
  28357. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28358. /* Ensure there is 0 bytes available to write into. */
  28359. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28360. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28361. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28362. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28363. ExpectIntEQ(BIO_set_write_buf_size(fixed, 8), 1);
  28364. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, esc_str, flags), 0);
  28365. /* Show type */
  28366. XMEMSET(rbuf, 0, 255);
  28367. flags = ASN1_STRFLGS_SHOW_TYPE;
  28368. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 28);
  28369. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 28), 28);
  28370. ExpectStrEQ((char*)rbuf, "OCTET STRING:Hello wolfSSL!");
  28371. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28372. /* Ensure there is 0 bytes available to write into. */
  28373. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28374. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28375. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28376. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28377. ExpectIntEQ(BIO_set_write_buf_size(fixed, 12), 1);
  28378. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28379. ExpectIntEQ(BIO_set_write_buf_size(fixed, 27), 1);
  28380. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28381. /* Dump All */
  28382. XMEMSET(rbuf, 0, 255);
  28383. flags = ASN1_STRFLGS_DUMP_ALL;
  28384. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 31);
  28385. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 31), 31);
  28386. ExpectStrEQ((char*)rbuf, "#48656C6C6F20776F6C6653534C2100");
  28387. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28388. /* Ensure there is 0 bytes available to write into. */
  28389. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28390. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28391. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28392. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28393. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  28394. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28395. /* Dump Der */
  28396. XMEMSET(rbuf, 0, 255);
  28397. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_DUMP_DER;
  28398. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 35);
  28399. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 35), 35);
  28400. ExpectStrEQ((char*)rbuf, "#040F48656C6C6F20776F6C6653534C2100");
  28401. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28402. /* Ensure there is 0 bytes available to write into. */
  28403. ExpectIntEQ(BIO_write(fixed, rbuf, 1), 1);
  28404. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28405. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28406. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28407. ExpectIntEQ(BIO_set_write_buf_size(fixed, 2), 1);
  28408. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28409. ExpectIntEQ(BIO_set_write_buf_size(fixed, 30), 1);
  28410. ExpectIntEQ(wolfSSL_ASN1_STRING_print_ex(fixed, asn_str, flags), 0);
  28411. /* Dump All + Show type */
  28412. XMEMSET(rbuf, 0, 255);
  28413. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28414. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, asn_str, flags), 44);
  28415. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 44), 44);
  28416. ExpectStrEQ((char*)rbuf, "OCTET STRING:#48656C6C6F20776F6C6653534C2100");
  28417. /* Dump All + Show type - Negative Integer. */
  28418. XMEMSET(rbuf, 0, 255);
  28419. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28420. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_int, flags), 11);
  28421. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 11), 11);
  28422. ExpectStrEQ((char*)rbuf, "INTEGER:#FF");
  28423. /* Dump All + Show type - Negative Enumerated. */
  28424. XMEMSET(rbuf, 0, 255);
  28425. flags = ASN1_STRFLGS_DUMP_ALL | ASN1_STRFLGS_SHOW_TYPE;
  28426. ExpectIntEQ(p_len = wolfSSL_ASN1_STRING_print_ex(bio, neg_enum, flags), 14);
  28427. ExpectIntEQ(BIO_read(bio, (void*)rbuf, 14), 14);
  28428. ExpectStrEQ((char*)rbuf, "ENUMERATED:#FF");
  28429. BIO_free(fixed);
  28430. BIO_free(bio);
  28431. ASN1_STRING_free(asn_str);
  28432. ASN1_STRING_free(esc_str);
  28433. ASN1_STRING_free(neg_int);
  28434. ASN1_STRING_free(neg_enum);
  28435. ExpectStrEQ(wolfSSL_ASN1_tag2str(-1), "(unknown)");
  28436. ExpectStrEQ(wolfSSL_ASN1_tag2str(31), "(unknown)");
  28437. #endif
  28438. return EXPECT_RESULT();
  28439. }
  28440. static int test_wolfSSL_ASN1_UNIVERSALSTRING_to_string(void)
  28441. {
  28442. EXPECT_DECLS;
  28443. #if defined(OPENSSL_ALL) && !defined(NO_ASN)
  28444. ASN1_STRING* asn1str_test = NULL;
  28445. ASN1_STRING* asn1str_answer = NULL;
  28446. /* Each character is encoded using 4 bytes */
  28447. char input[] = {
  28448. 0, 0, 0, 'T',
  28449. 0, 0, 0, 'e',
  28450. 0, 0, 0, 's',
  28451. 0, 0, 0, 't',
  28452. };
  28453. char output[] = "Test";
  28454. char badInput[] = {
  28455. 1, 0, 0, 'T',
  28456. 0, 1, 0, 'e',
  28457. 0, 0, 1, 's',
  28458. };
  28459. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(NULL), 0);
  28460. /* Test wrong type. */
  28461. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_OCTET_STRING));
  28462. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28463. ASN1_STRING_free(asn1str_test);
  28464. asn1str_test = NULL;
  28465. ExpectNotNull(asn1str_test = ASN1_STRING_type_new(V_ASN1_UNIVERSALSTRING));
  28466. /* Test bad length. */
  28467. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input) - 1), 1);
  28468. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28469. /* Test bad input. */
  28470. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 0, 4), 1);
  28471. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28472. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 4, 4), 1);
  28473. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28474. ExpectIntEQ(ASN1_STRING_set(asn1str_test, badInput + 8, 4), 1);
  28475. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 0);
  28476. ExpectIntEQ(ASN1_STRING_set(asn1str_test, input, sizeof(input)), 1);
  28477. ExpectIntEQ(ASN1_UNIVERSALSTRING_to_string(asn1str_test), 1);
  28478. ExpectNotNull(
  28479. asn1str_answer = ASN1_STRING_type_new(V_ASN1_PRINTABLESTRING));
  28480. ExpectIntEQ(ASN1_STRING_set(asn1str_answer, output, sizeof(output)-1), 1);
  28481. ExpectIntEQ(ASN1_STRING_cmp(asn1str_test, asn1str_answer), 0);
  28482. ASN1_STRING_free(asn1str_test);
  28483. ASN1_STRING_free(asn1str_answer);
  28484. #endif /* OPENSSL_ALL && !NO_ASN */
  28485. return EXPECT_RESULT();
  28486. }
  28487. static int test_wolfSSL_ASN1_GENERALIZEDTIME_free(void)
  28488. {
  28489. EXPECT_DECLS;
  28490. #if defined(OPENSSL_EXTRA)
  28491. WOLFSSL_ASN1_GENERALIZEDTIME* asn1_gtime = NULL;
  28492. ExpectNotNull(asn1_gtime = ASN1_GENERALIZEDTIME_new());
  28493. if (asn1_gtime != NULL)
  28494. XMEMCPY(asn1_gtime->data, "20180504123500Z", ASN_GENERALIZED_TIME_SIZE);
  28495. ASN1_GENERALIZEDTIME_free(asn1_gtime);
  28496. #endif /* OPENSSL_EXTRA */
  28497. return EXPECT_RESULT();
  28498. }
  28499. static int test_wolfSSL_ASN1_GENERALIZEDTIME_print(void)
  28500. {
  28501. EXPECT_DECLS;
  28502. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  28503. WOLFSSL_ASN1_GENERALIZEDTIME* gtime = NULL;
  28504. BIO* bio = NULL;
  28505. unsigned char buf[24];
  28506. int i;
  28507. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28508. BIO_set_write_buf_size(bio, 24);
  28509. ExpectNotNull(gtime = ASN1_GENERALIZEDTIME_new());
  28510. /* Type not set. */
  28511. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, gtime), 0);
  28512. ExpectIntEQ(wolfSSL_ASN1_TIME_set_string(gtime, "20180504123500Z"), 1);
  28513. /* Invalid parameters testing. */
  28514. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, NULL), BAD_FUNC_ARG);
  28515. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, NULL), BAD_FUNC_ARG);
  28516. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(NULL, gtime), BAD_FUNC_ARG);
  28517. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, gtime), 1);
  28518. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 20);
  28519. ExpectIntEQ(XMEMCMP(buf, "May 04 12:35:00 2018", 20), 0);
  28520. BIO_free(bio);
  28521. bio = NULL;
  28522. ExpectNotNull(bio = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28523. ExpectIntEQ(BIO_set_write_buf_size(bio, 1), 1);
  28524. /* Ensure there is 0 bytes available to write into. */
  28525. ExpectIntEQ(BIO_write(bio, buf, 1), 1);
  28526. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, gtime), 0);
  28527. for (i = 1; i < 20; i++) {
  28528. ExpectIntEQ(BIO_set_write_buf_size(bio, i), 1);
  28529. ExpectIntEQ(wolfSSL_ASN1_GENERALIZEDTIME_print(bio, gtime), 0);
  28530. }
  28531. BIO_free(bio);
  28532. wolfSSL_ASN1_GENERALIZEDTIME_free(gtime);
  28533. #endif /* OPENSSL_EXTRA */
  28534. return EXPECT_RESULT();
  28535. }
  28536. static int test_wolfSSL_ASN1_TIME(void)
  28537. {
  28538. EXPECT_DECLS;
  28539. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28540. WOLFSSL_ASN1_TIME* asn_time = NULL;
  28541. unsigned char *data;
  28542. ExpectNotNull(asn_time = ASN1_TIME_new());
  28543. #ifndef NO_WOLFSSL_STUB
  28544. ExpectNotNull(ASN1_TIME_set(asn_time, 1));
  28545. #endif
  28546. ExpectIntEQ(ASN1_TIME_set_string(NULL, NULL), 0);
  28547. ExpectIntEQ(ASN1_TIME_set_string(asn_time, NULL), 0);
  28548. ExpectIntEQ(ASN1_TIME_set_string(NULL,
  28549. "String longer than CTC_DATA_SIZE that is 32 bytes"), 0);
  28550. ExpectIntEQ(ASN1_TIME_set_string(NULL, "101219181011Z"), 1);
  28551. ExpectIntEQ(ASN1_TIME_set_string(asn_time, "101219181011Z"), 1);
  28552. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(NULL), 0);
  28553. ExpectIntEQ(wolfSSL_ASN1_TIME_get_length(asn_time), ASN_UTC_TIME_SIZE - 1);
  28554. ExpectNull(wolfSSL_ASN1_TIME_get_data(NULL));
  28555. ExpectNotNull(data = wolfSSL_ASN1_TIME_get_data(asn_time));
  28556. ExpectIntEQ(XMEMCMP(data, "101219181011Z", 14), 0);
  28557. ExpectIntEQ(ASN1_TIME_check(NULL), 0);
  28558. ExpectIntEQ(ASN1_TIME_check(asn_time), 1);
  28559. ASN1_TIME_free(asn_time);
  28560. ASN1_TIME_free(NULL);
  28561. #endif
  28562. return EXPECT_RESULT();
  28563. }
  28564. static int test_wolfSSL_ASN1_TIME_to_string(void)
  28565. {
  28566. EXPECT_DECLS;
  28567. #ifndef NO_ASN_TIME
  28568. #if defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28569. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  28570. WOLFSSL_ASN1_TIME* t = NULL;
  28571. char buf[ASN_GENERALIZED_TIME_SIZE];
  28572. ExpectNotNull((t = ASN1_TIME_new()));
  28573. ExpectIntEQ(ASN1_TIME_set_string(t, "030222211515Z"), 1);
  28574. /* Invalid parameter testing. */
  28575. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 4));
  28576. ExpectNull(ASN1_TIME_to_string(t, NULL, 4));
  28577. ExpectNull(ASN1_TIME_to_string(NULL, buf, 4));
  28578. ExpectNull(ASN1_TIME_to_string(NULL, NULL, 5));
  28579. ExpectNull(ASN1_TIME_to_string(NULL, buf, 5));
  28580. ExpectNull(ASN1_TIME_to_string(t, NULL, 5));
  28581. ExpectNull(ASN1_TIME_to_string(t, buf, 4));
  28582. /* Buffer needs to be longer than minimum of 5 characters. */
  28583. ExpectNull(ASN1_TIME_to_string(t, buf, 5));
  28584. ASN1_TIME_free(t);
  28585. #endif
  28586. #endif /* NO_ASN_TIME */
  28587. return EXPECT_RESULT();
  28588. }
  28589. static int test_wolfSSL_ASN1_TIME_diff_compare(void)
  28590. {
  28591. EXPECT_DECLS;
  28592. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28593. ASN1_TIME* fromTime = NULL;
  28594. ASN1_TIME* closeToTime = NULL;
  28595. ASN1_TIME* toTime = NULL;
  28596. ASN1_TIME* invalidTime = NULL;
  28597. int daysDiff = 0;
  28598. int secsDiff = 0;
  28599. ExpectNotNull((fromTime = ASN1_TIME_new()));
  28600. /* Feb 22, 2003, 21:15:15 */
  28601. ExpectIntEQ(ASN1_TIME_set_string(fromTime, "030222211515Z"), 1);
  28602. ExpectNotNull((closeToTime = ASN1_TIME_new()));
  28603. /* Feb 22, 2003, 21:16:15 */
  28604. ExpectIntEQ(ASN1_TIME_set_string(closeToTime, "030222211615Z"), 1);
  28605. ExpectNotNull((toTime = ASN1_TIME_new()));
  28606. /* Dec 19, 2010, 18:10:11 */
  28607. ExpectIntEQ(ASN1_TIME_set_string(toTime, "101219181011Z"), 1);
  28608. ExpectNotNull((invalidTime = ASN1_TIME_new()));
  28609. /* Dec 19, 2010, 18:10:11 but 'U' instead of 'Z' which is invalid. */
  28610. ExpectIntEQ(ASN1_TIME_set_string(invalidTime, "102519181011U"), 1);
  28611. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, invalidTime), 0);
  28612. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, invalidTime, toTime), 0);
  28613. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28614. /* Error conditions. */
  28615. ExpectIntEQ(ASN1_TIME_diff(NULL, &secsDiff, fromTime, toTime), 0);
  28616. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, NULL, fromTime, toTime), 0);
  28617. /* If both times are NULL, difference is 0. */
  28618. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, NULL), 1);
  28619. ExpectIntEQ(daysDiff, 0);
  28620. ExpectIntEQ(secsDiff, 0);
  28621. /* If one time is NULL, it defaults to the current time. */
  28622. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, NULL, toTime), 1);
  28623. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, NULL), 1);
  28624. /* Normal operation. Both times non-NULL. */
  28625. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28626. ExpectIntEQ(daysDiff, 2856);
  28627. ExpectIntEQ(secsDiff, 75296);
  28628. /* Swapping the times should return negative values. */
  28629. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, toTime, fromTime), 1);
  28630. ExpectIntEQ(daysDiff, -2856);
  28631. ExpectIntEQ(secsDiff, -75296);
  28632. /* Compare with invalid time string. */
  28633. ExpectIntEQ(ASN1_TIME_compare(fromTime, invalidTime), -2);
  28634. ExpectIntEQ(ASN1_TIME_compare(invalidTime, toTime), -2);
  28635. /* Compare with days difference of 0. */
  28636. ExpectIntEQ(ASN1_TIME_compare(fromTime, closeToTime), -1);
  28637. ExpectIntEQ(ASN1_TIME_compare(closeToTime, fromTime), 1);
  28638. /* Days and seconds differences not 0. */
  28639. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  28640. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  28641. /* Same time. */
  28642. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  28643. /* Compare regression test: No seconds difference, just difference in days.
  28644. */
  28645. ASN1_TIME_set_string(fromTime, "19700101000000Z");
  28646. ASN1_TIME_set_string(toTime, "19800101000000Z");
  28647. ExpectIntEQ(ASN1_TIME_compare(fromTime, toTime), -1);
  28648. ExpectIntEQ(ASN1_TIME_compare(toTime, fromTime), 1);
  28649. ExpectIntEQ(ASN1_TIME_compare(fromTime, fromTime), 0);
  28650. /* Edge case with Unix epoch. */
  28651. ExpectNotNull(ASN1_TIME_set_string(fromTime, "19700101000000Z"));
  28652. ExpectNotNull(ASN1_TIME_set_string(toTime, "19800101000000Z"));
  28653. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28654. ExpectIntEQ(daysDiff, 3652);
  28655. ExpectIntEQ(secsDiff, 0);
  28656. /* Edge case with year > 2038 (year 2038 problem). */
  28657. ExpectNotNull(ASN1_TIME_set_string(toTime, "99991231235959Z"));
  28658. ExpectIntEQ(ASN1_TIME_diff(&daysDiff, &secsDiff, fromTime, toTime), 1);
  28659. ExpectIntEQ(daysDiff, 2932896);
  28660. ExpectIntEQ(secsDiff, 86399);
  28661. ASN1_TIME_free(fromTime);
  28662. ASN1_TIME_free(closeToTime);
  28663. ASN1_TIME_free(toTime);
  28664. ASN1_TIME_free(invalidTime);
  28665. #endif
  28666. return EXPECT_RESULT();
  28667. }
  28668. static int test_wolfSSL_ASN1_TIME_adj(void)
  28669. {
  28670. EXPECT_DECLS;
  28671. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  28672. !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  28673. const int year = 365*24*60*60;
  28674. const int day = 24*60*60;
  28675. const int hour = 60*60;
  28676. const int mini = 60;
  28677. const byte asn_utc_time = ASN_UTC_TIME;
  28678. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  28679. const byte asn_gen_time = ASN_GENERALIZED_TIME;
  28680. #endif
  28681. WOLFSSL_ASN1_TIME* asn_time = NULL;
  28682. WOLFSSL_ASN1_TIME* s = NULL;
  28683. int offset_day;
  28684. long offset_sec;
  28685. char date_str[CTC_DATE_SIZE + 1];
  28686. time_t t;
  28687. ExpectNotNull(s = wolfSSL_ASN1_TIME_new());
  28688. /* UTC notation test */
  28689. /* 2000/2/15 20:30:00 */
  28690. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  28691. offset_day = 7;
  28692. offset_sec = 45 * mini;
  28693. /* offset_sec = -45 * min;*/
  28694. ExpectNotNull(asn_time =
  28695. wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec));
  28696. ExpectTrue(asn_time->type == asn_utc_time);
  28697. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28698. CTC_DATE_SIZE));
  28699. date_str[CTC_DATE_SIZE] = '\0';
  28700. ExpectIntEQ(0, XMEMCMP(date_str, "000222211500Z", 13));
  28701. /* negative offset */
  28702. offset_sec = -45 * mini;
  28703. asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day, offset_sec);
  28704. ExpectNotNull(asn_time);
  28705. ExpectTrue(asn_time->type == asn_utc_time);
  28706. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28707. CTC_DATE_SIZE));
  28708. date_str[CTC_DATE_SIZE] = '\0';
  28709. ExpectIntEQ(0, XMEMCMP(date_str, "000222194500Z", 13));
  28710. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  28711. s = NULL;
  28712. XMEMSET(date_str, 0, sizeof(date_str));
  28713. /* Generalized time will overflow time_t if not long */
  28714. #if !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT)
  28715. s = (WOLFSSL_ASN1_TIME*)XMALLOC(sizeof(WOLFSSL_ASN1_TIME), NULL,
  28716. DYNAMIC_TYPE_OPENSSL);
  28717. /* GeneralizedTime notation test */
  28718. /* 2055/03/01 09:00:00 */
  28719. t = (time_t)85 * year + 59 * day + 9 * hour + 21 * day;
  28720. offset_day = 12;
  28721. offset_sec = 10 * mini;
  28722. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  28723. offset_sec));
  28724. ExpectTrue(asn_time->type == asn_gen_time);
  28725. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28726. CTC_DATE_SIZE));
  28727. date_str[CTC_DATE_SIZE] = '\0';
  28728. ExpectIntEQ(0, XMEMCMP(date_str, "20550313091000Z", 15));
  28729. XFREE(s, NULL, DYNAMIC_TYPE_OPENSSL);
  28730. s = NULL;
  28731. XMEMSET(date_str, 0, sizeof(date_str));
  28732. #endif /* !TIME_T_NOT_64BIT && !NO_64BIT */
  28733. /* if WOLFSSL_ASN1_TIME struct is not allocated */
  28734. s = NULL;
  28735. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 15 + 7 * day;
  28736. offset_day = 7;
  28737. offset_sec = 45 * mini;
  28738. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(s, t, offset_day,
  28739. offset_sec));
  28740. ExpectTrue(asn_time->type == asn_utc_time);
  28741. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28742. CTC_DATE_SIZE));
  28743. date_str[CTC_DATE_SIZE] = '\0';
  28744. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  28745. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  28746. asn_time = NULL;
  28747. ExpectNotNull(asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day,
  28748. offset_sec));
  28749. ExpectTrue(asn_time->type == asn_utc_time);
  28750. ExpectNotNull(XSTRNCPY(date_str, (const char*)&asn_time->data,
  28751. CTC_DATE_SIZE));
  28752. date_str[CTC_DATE_SIZE] = '\0';
  28753. ExpectIntEQ(0, XMEMCMP(date_str, "000222211515Z", 13));
  28754. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  28755. #endif
  28756. return EXPECT_RESULT();
  28757. }
  28758. static int test_wolfSSL_ASN1_TIME_to_tm(void)
  28759. {
  28760. EXPECT_DECLS;
  28761. #if (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28762. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  28763. defined(OPENSSL_ALL)) && !defined(NO_ASN_TIME)
  28764. ASN1_TIME asnTime;
  28765. struct tm tm;
  28766. time_t testTime = 1683926567; /* Fri May 12 09:22:47 PM UTC 2023 */
  28767. XMEMSET(&tm, 0, sizeof(struct tm));
  28768. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  28769. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515Z"), 1);
  28770. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, NULL), 1);
  28771. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28772. ExpectIntEQ(tm.tm_sec, 15);
  28773. ExpectIntEQ(tm.tm_min, 15);
  28774. ExpectIntEQ(tm.tm_hour, 21);
  28775. ExpectIntEQ(tm.tm_mday, 22);
  28776. ExpectIntEQ(tm.tm_mon, 1);
  28777. ExpectIntEQ(tm.tm_year, 100);
  28778. ExpectIntEQ(tm.tm_isdst, 0);
  28779. #ifdef XMKTIME
  28780. ExpectIntEQ(tm.tm_wday, 2);
  28781. ExpectIntEQ(tm.tm_yday, 52);
  28782. #endif
  28783. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "500222211515Z"), 1);
  28784. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28785. ExpectIntEQ(tm.tm_year, 50);
  28786. /* Get current time. */
  28787. ExpectIntEQ(ASN1_TIME_to_tm(NULL, NULL), 0);
  28788. ExpectIntEQ(ASN1_TIME_to_tm(NULL, &tm), 1);
  28789. XMEMSET(&asnTime, 0, sizeof(ASN1_TIME));
  28790. /* 0 length. */
  28791. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28792. /* No type. */
  28793. asnTime.length = 1;
  28794. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28795. /* Not UTCTIME length. */
  28796. asnTime.type = V_ASN1_UTCTIME;
  28797. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28798. /* Not GENERALIZEDTIME length. */
  28799. asnTime.type = V_ASN1_GENERALIZEDTIME;
  28800. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28801. /* Not Zulu timezone. */
  28802. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "000222211515U"), 1);
  28803. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28804. ExpectIntEQ(ASN1_TIME_set_string(&asnTime, "20000222211515U"), 1);
  28805. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 0);
  28806. #ifdef XMKTIME
  28807. ExpectNotNull(ASN1_TIME_adj(&asnTime, testTime, 0, 0));
  28808. ExpectIntEQ(ASN1_TIME_to_tm(&asnTime, &tm), 1);
  28809. ExpectIntEQ(tm.tm_sec, 47);
  28810. ExpectIntEQ(tm.tm_min, 22);
  28811. ExpectIntEQ(tm.tm_hour, 21);
  28812. ExpectIntEQ(tm.tm_mday, 12);
  28813. ExpectIntEQ(tm.tm_mon, 4);
  28814. ExpectIntEQ(tm.tm_year, 123);
  28815. ExpectIntEQ(tm.tm_wday, 5);
  28816. ExpectIntEQ(tm.tm_yday, 131);
  28817. /* Confirm that when used with a tm struct from ASN1_TIME_adj, all other
  28818. fields are zeroed out as expected. */
  28819. ExpectIntEQ(tm.tm_isdst, 0);
  28820. #endif
  28821. #endif
  28822. return EXPECT_RESULT();
  28823. }
  28824. static int test_wolfSSL_ASN1_TIME_to_generalizedtime(void)
  28825. {
  28826. EXPECT_DECLS;
  28827. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME)
  28828. WOLFSSL_ASN1_TIME *t = NULL;
  28829. WOLFSSL_ASN1_TIME *out = NULL;
  28830. WOLFSSL_ASN1_TIME *gtime = NULL;
  28831. int tlen = 0;
  28832. unsigned char *data = NULL;
  28833. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  28834. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(NULL, &out));
  28835. /* type not set. */
  28836. ExpectNull(wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28837. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28838. t = NULL;
  28839. /* UTC Time test */
  28840. ExpectNotNull(t = wolfSSL_ASN1_TIME_new());
  28841. if (t != NULL) {
  28842. XMEMSET(t->data, 0, ASN_GENERALIZED_TIME_SIZE);
  28843. t->type = ASN_UTC_TIME;
  28844. t->length = ASN_UTC_TIME_SIZE;
  28845. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  28846. }
  28847. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t), ASN_UTC_TIME_SIZE);
  28848. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)), "050727123456Z");
  28849. out = NULL;
  28850. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28851. wolfSSL_ASN1_TIME_free(gtime);
  28852. gtime = NULL;
  28853. ExpectNotNull(out = wolfSSL_ASN1_TIME_new());
  28854. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28855. ExpectPtrEq(gtime, out);
  28856. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28857. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28858. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  28859. /* Generalized Time test */
  28860. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28861. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  28862. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  28863. if (t != NULL) {
  28864. t->type = ASN_GENERALIZED_TIME;
  28865. t->length = ASN_GENERALIZED_TIME_SIZE;
  28866. XMEMCPY(t->data, "20050727123456Z", ASN_GENERALIZED_TIME_SIZE);
  28867. }
  28868. ExpectIntEQ(tlen = wolfSSL_ASN1_TIME_get_length(t),
  28869. ASN_GENERALIZED_TIME_SIZE);
  28870. ExpectStrEQ((char*)(data = wolfSSL_ASN1_TIME_get_data(t)),
  28871. "20050727123456Z");
  28872. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28873. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28874. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28875. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  28876. /* UTC Time to Generalized Time 1900's test */
  28877. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28878. ExpectNotNull(XMEMSET(out, 0, ASN_GENERALIZED_TIME_SIZE));
  28879. ExpectNotNull(XMEMSET(data, 0, ASN_GENERALIZED_TIME_SIZE));
  28880. if (t != NULL) {
  28881. t->type = ASN_UTC_TIME;
  28882. t->length = ASN_UTC_TIME_SIZE;
  28883. XMEMCPY(t->data, "500727123456Z", ASN_UTC_TIME_SIZE);
  28884. }
  28885. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, &out));
  28886. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28887. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28888. ExpectStrEQ((char*)gtime->data, "19500727123456Z");
  28889. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28890. /* Null parameter test */
  28891. ExpectNotNull(XMEMSET(t, 0, ASN_GENERALIZED_TIME_SIZE));
  28892. gtime = NULL;
  28893. out = NULL;
  28894. if (t != NULL) {
  28895. t->type = ASN_UTC_TIME;
  28896. t->length = ASN_UTC_TIME_SIZE;
  28897. XMEMCPY(t->data, "050727123456Z", ASN_UTC_TIME_SIZE);
  28898. }
  28899. ExpectNotNull(gtime = wolfSSL_ASN1_TIME_to_generalizedtime(t, NULL));
  28900. ExpectIntEQ(gtime->type, ASN_GENERALIZED_TIME);
  28901. ExpectIntEQ(gtime->length, ASN_GENERALIZED_TIME_SIZE);
  28902. ExpectStrEQ((char*)gtime->data, "20050727123456Z");
  28903. XFREE(gtime, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28904. XFREE(t, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  28905. #endif
  28906. return EXPECT_RESULT();
  28907. }
  28908. static int test_wolfSSL_ASN1_TIME_print(void)
  28909. {
  28910. EXPECT_DECLS;
  28911. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(NO_BIO) && \
  28912. (defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(WOLFSSL_NGINX) || \
  28913. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA) || \
  28914. defined(OPENSSL_ALL)) && defined(USE_CERT_BUFFERS_2048) && \
  28915. !defined(NO_ASN_TIME)
  28916. BIO* bio = NULL;
  28917. BIO* fixed = NULL;
  28918. X509* x509 = NULL;
  28919. const unsigned char* der = client_cert_der_2048;
  28920. ASN1_TIME* notAfter = NULL;
  28921. ASN1_TIME* notBefore = NULL;
  28922. unsigned char buf[25];
  28923. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28924. ExpectNotNull(fixed = BIO_new(wolfSSL_BIO_s_fixed_mem()));
  28925. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(der,
  28926. sizeof_client_cert_der_2048, WOLFSSL_FILETYPE_ASN1));
  28927. ExpectNotNull(notBefore = X509_get_notBefore(x509));
  28928. ExpectIntEQ(ASN1_TIME_print(NULL, NULL), 0);
  28929. ExpectIntEQ(ASN1_TIME_print(bio, NULL), 0);
  28930. ExpectIntEQ(ASN1_TIME_print(NULL, notBefore), 0);
  28931. ExpectIntEQ(ASN1_TIME_print(bio, notBefore), 1);
  28932. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  28933. ExpectIntEQ(XMEMCMP(buf, "Dec 13 22:19:28 2023 GMT", sizeof(buf) - 1), 0);
  28934. /* Test BIO_write fails. */
  28935. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28936. /* Ensure there is 0 bytes available to write into. */
  28937. ExpectIntEQ(BIO_write(fixed, buf, 1), 1);
  28938. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  28939. ExpectIntEQ(BIO_set_write_buf_size(fixed, 1), 1);
  28940. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  28941. ExpectIntEQ(BIO_set_write_buf_size(fixed, 23), 1);
  28942. ExpectIntEQ(ASN1_TIME_print(fixed, notBefore), 0);
  28943. /* create a bad time and test results */
  28944. ExpectNotNull(notAfter = X509_get_notAfter(x509));
  28945. ExpectIntEQ(ASN1_TIME_check(notAfter), 1);
  28946. if (EXPECT_SUCCESS()) {
  28947. notAfter->data[8] = 0;
  28948. notAfter->data[3] = 0;
  28949. }
  28950. ExpectIntNE(ASN1_TIME_print(bio, notAfter), 1);
  28951. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  28952. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  28953. ExpectIntEQ(ASN1_TIME_check(notAfter), 0);
  28954. BIO_free(bio);
  28955. BIO_free(fixed);
  28956. X509_free(x509);
  28957. #endif
  28958. return EXPECT_RESULT();
  28959. }
  28960. static int test_wolfSSL_ASN1_UTCTIME_print(void)
  28961. {
  28962. EXPECT_DECLS;
  28963. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && !defined(NO_BIO)
  28964. BIO* bio = NULL;
  28965. ASN1_UTCTIME* utc = NULL;
  28966. unsigned char buf[25];
  28967. const char* validDate = "190424111501Z"; /* UTC = YYMMDDHHMMSSZ */
  28968. const char* invalidDate = "190424111501X"; /* UTC = YYMMDDHHMMSSZ */
  28969. const char* genDate = "20190424111501Z"; /* GEN = YYYYMMDDHHMMSSZ */
  28970. /* Valid date */
  28971. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28972. ExpectNotNull(utc = (ASN1_UTCTIME*)XMALLOC(sizeof(ASN1_UTCTIME), NULL,
  28973. DYNAMIC_TYPE_ASN1));
  28974. if (utc != NULL) {
  28975. utc->type = ASN_UTC_TIME;
  28976. utc->length = ASN_UTC_TIME_SIZE;
  28977. XMEMCPY(utc->data, (byte*)validDate, ASN_UTC_TIME_SIZE);
  28978. }
  28979. ExpectIntEQ(ASN1_UTCTIME_print(NULL, NULL), 0);
  28980. ExpectIntEQ(ASN1_UTCTIME_print(bio, NULL), 0);
  28981. ExpectIntEQ(ASN1_UTCTIME_print(NULL, utc), 0);
  28982. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 1);
  28983. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  28984. ExpectIntEQ(XMEMCMP(buf, "Apr 24 11:15:01 2019 GMT", sizeof(buf)-1), 0);
  28985. XMEMSET(buf, 0, sizeof(buf));
  28986. BIO_free(bio);
  28987. bio = NULL;
  28988. /* Invalid format */
  28989. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  28990. if (utc != NULL) {
  28991. utc->type = ASN_UTC_TIME;
  28992. utc->length = ASN_UTC_TIME_SIZE;
  28993. XMEMCPY(utc->data, (byte*)invalidDate, ASN_UTC_TIME_SIZE);
  28994. }
  28995. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  28996. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 14);
  28997. ExpectIntEQ(XMEMCMP(buf, "Bad time value", 14), 0);
  28998. /* Invalid type */
  28999. if (utc != NULL) {
  29000. utc->type = ASN_GENERALIZED_TIME;
  29001. utc->length = ASN_GENERALIZED_TIME_SIZE;
  29002. XMEMCPY(utc->data, (byte*)genDate, ASN_GENERALIZED_TIME_SIZE);
  29003. }
  29004. ExpectIntEQ(ASN1_UTCTIME_print(bio, utc), 0);
  29005. XFREE(utc, NULL, DYNAMIC_TYPE_ASN1);
  29006. BIO_free(bio);
  29007. #endif /* OPENSSL_EXTRA && !NO_ASN_TIME && !NO_BIO */
  29008. return EXPECT_RESULT();
  29009. }
  29010. static int test_wolfSSL_ASN1_TYPE(void)
  29011. {
  29012. EXPECT_DECLS;
  29013. #if defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD) || \
  29014. defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_WPAS)
  29015. WOLFSSL_ASN1_TYPE* t = NULL;
  29016. WOLFSSL_ASN1_OBJECT* obj = NULL;
  29017. #ifndef NO_ASN_TIME
  29018. WOLFSSL_ASN1_TIME* time = NULL;
  29019. #endif
  29020. WOLFSSL_ASN1_STRING* str = NULL;
  29021. unsigned char data[] = { 0x00 };
  29022. ASN1_TYPE_set(NULL, V_ASN1_NULL, NULL);
  29023. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29024. ASN1_TYPE_set(t, V_ASN1_EOC, NULL);
  29025. wolfSSL_ASN1_TYPE_free(t);
  29026. t = NULL;
  29027. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29028. ASN1_TYPE_set(t, V_ASN1_NULL, NULL);
  29029. ASN1_TYPE_set(t, V_ASN1_NULL, data);
  29030. wolfSSL_ASN1_TYPE_free(t);
  29031. t = NULL;
  29032. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29033. ExpectNotNull(obj = wolfSSL_ASN1_OBJECT_new());
  29034. ASN1_TYPE_set(t, V_ASN1_OBJECT, obj);
  29035. wolfSSL_ASN1_TYPE_free(t);
  29036. t = NULL;
  29037. #ifndef NO_ASN_TIME
  29038. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29039. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  29040. ASN1_TYPE_set(t, V_ASN1_UTCTIME, time);
  29041. wolfSSL_ASN1_TYPE_free(t);
  29042. t = NULL;
  29043. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29044. ExpectNotNull(time = wolfSSL_ASN1_TIME_new());
  29045. ASN1_TYPE_set(t, V_ASN1_GENERALIZEDTIME, time);
  29046. wolfSSL_ASN1_TYPE_free(t);
  29047. t = NULL;
  29048. #endif
  29049. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29050. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29051. ASN1_TYPE_set(t, V_ASN1_UTF8STRING, str);
  29052. wolfSSL_ASN1_TYPE_free(t);
  29053. t = NULL;
  29054. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29055. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29056. ASN1_TYPE_set(t, V_ASN1_PRINTABLESTRING, str);
  29057. wolfSSL_ASN1_TYPE_free(t);
  29058. t = NULL;
  29059. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29060. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29061. ASN1_TYPE_set(t, V_ASN1_T61STRING, str);
  29062. wolfSSL_ASN1_TYPE_free(t);
  29063. t = NULL;
  29064. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29065. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29066. ASN1_TYPE_set(t, V_ASN1_IA5STRING, str);
  29067. wolfSSL_ASN1_TYPE_free(t);
  29068. t = NULL;
  29069. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29070. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29071. ASN1_TYPE_set(t, V_ASN1_UNIVERSALSTRING, str);
  29072. wolfSSL_ASN1_TYPE_free(t);
  29073. t = NULL;
  29074. ExpectNotNull(t = wolfSSL_ASN1_TYPE_new());
  29075. ExpectNotNull(str = wolfSSL_ASN1_STRING_new());
  29076. ASN1_TYPE_set(t, V_ASN1_SEQUENCE, str);
  29077. wolfSSL_ASN1_TYPE_free(t);
  29078. t = NULL;
  29079. #endif
  29080. return EXPECT_RESULT();
  29081. }
  29082. /* Testing code used in dpp.c in hostap */
  29083. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  29084. typedef struct {
  29085. /* AlgorithmIdentifier ecPublicKey with optional parameters present
  29086. * as an OID identifying the curve */
  29087. X509_ALGOR *alg;
  29088. /* Compressed format public key per ANSI X9.63 */
  29089. ASN1_BIT_STRING *pub_key;
  29090. } DPP_BOOTSTRAPPING_KEY;
  29091. ASN1_SEQUENCE(DPP_BOOTSTRAPPING_KEY) = {
  29092. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, alg, X509_ALGOR),
  29093. ASN1_SIMPLE(DPP_BOOTSTRAPPING_KEY, pub_key, ASN1_BIT_STRING)
  29094. } ASN1_SEQUENCE_END(DPP_BOOTSTRAPPING_KEY)
  29095. IMPLEMENT_ASN1_FUNCTIONS(DPP_BOOTSTRAPPING_KEY)
  29096. typedef struct {
  29097. ASN1_INTEGER *integer;
  29098. } TEST_ASN1;
  29099. ASN1_SEQUENCE(TEST_ASN1) = {
  29100. ASN1_SIMPLE(TEST_ASN1, integer, ASN1_INTEGER),
  29101. } ASN1_SEQUENCE_END(TEST_ASN1)
  29102. IMPLEMENT_ASN1_FUNCTIONS(TEST_ASN1)
  29103. typedef struct {
  29104. ASN1_OCTET_STRING *octet_string;
  29105. } TEST_FAIL_ASN1;
  29106. #define WOLFSSL_ASN1_OCTET_STRING_ASN1 4
  29107. ASN1_SEQUENCE(TEST_FAIL_ASN1) = {
  29108. ASN1_SIMPLE(TEST_FAIL_ASN1, octet_string, ASN1_OCTET_STRING),
  29109. } ASN1_SEQUENCE_END(TEST_FAIL_ASN1)
  29110. IMPLEMENT_ASN1_FUNCTIONS(TEST_FAIL_ASN1)
  29111. #endif
  29112. static int test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS(void)
  29113. {
  29114. EXPECT_DECLS;
  29115. /* Testing code used in dpp.c in hostap */
  29116. #if defined(OPENSSL_ALL) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  29117. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  29118. EC_KEY *eckey = NULL;
  29119. EVP_PKEY *key = NULL;
  29120. size_t len = 0;
  29121. unsigned char *der = NULL;
  29122. DPP_BOOTSTRAPPING_KEY *bootstrap = NULL;
  29123. const unsigned char *in = ecc_clikey_der_256;
  29124. WOLFSSL_ASN1_OBJECT* ec_obj = NULL;
  29125. WOLFSSL_ASN1_OBJECT* group_obj = NULL;
  29126. const EC_GROUP *group = NULL;
  29127. const EC_POINT *point = NULL;
  29128. int nid;
  29129. TEST_ASN1 *test_asn1 = NULL;
  29130. TEST_FAIL_ASN1 test_fail_asn1;
  29131. const unsigned char badObjDer[] = { 0x06, 0x00 };
  29132. const unsigned char goodObjDer[] = {
  29133. 0x06, 0x07, 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02, 0x01
  29134. };
  29135. WOLFSSL_ASN1_ITEM emptyTemplate;
  29136. XMEMSET(&emptyTemplate, 0, sizeof(WOLFSSL_ASN1_ITEM));
  29137. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  29138. der = NULL;
  29139. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(NULL, &der), 0);
  29140. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, NULL), 0);
  29141. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29142. ExpectNotNull(key = d2i_PrivateKey(EVP_PKEY_EC, NULL, &in,
  29143. (long)sizeof_ecc_clikey_der_256));
  29144. ExpectNotNull(eckey = EVP_PKEY_get1_EC_KEY(key));
  29145. ExpectNotNull(group = EC_KEY_get0_group(eckey));
  29146. ExpectNotNull(point = EC_KEY_get0_public_key(eckey));
  29147. nid = EC_GROUP_get_curve_name(group);
  29148. ec_obj = OBJ_nid2obj(EVP_PKEY_EC);
  29149. group_obj = OBJ_nid2obj(nid);
  29150. if ((ec_obj != NULL) && (group_obj != NULL)) {
  29151. ExpectIntEQ(X509_ALGOR_set0(bootstrap->alg, ec_obj, V_ASN1_OBJECT,
  29152. group_obj), 1);
  29153. if (EXPECT_SUCCESS()) {
  29154. ec_obj = NULL;
  29155. group_obj = NULL;
  29156. }
  29157. }
  29158. wolfSSL_ASN1_OBJECT_free(group_obj);
  29159. wolfSSL_ASN1_OBJECT_free(ec_obj);
  29160. ExpectIntEQ(EC_POINT_point2oct(group, point, 0, NULL, 0, NULL), 0);
  29161. #ifdef HAVE_COMP_KEY
  29162. ExpectIntGT((len = EC_POINT_point2oct(
  29163. group, point, POINT_CONVERSION_COMPRESSED,
  29164. NULL, 0, NULL)), 0);
  29165. #else
  29166. ExpectIntGT((len = EC_POINT_point2oct(
  29167. group, point, POINT_CONVERSION_UNCOMPRESSED,
  29168. NULL, 0, NULL)), 0);
  29169. #endif
  29170. ExpectNotNull(der = (unsigned char*)XMALLOC(len, NULL, DYNAMIC_TYPE_ASN1));
  29171. #ifdef HAVE_COMP_KEY
  29172. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  29173. der, len-1, NULL), 0);
  29174. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_COMPRESSED,
  29175. der, len, NULL), len);
  29176. #else
  29177. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  29178. der, len-1, NULL), 0);
  29179. ExpectIntEQ(EC_POINT_point2oct(group, point, POINT_CONVERSION_UNCOMPRESSED,
  29180. der, len, NULL), len);
  29181. #endif
  29182. if (EXPECT_SUCCESS()) {
  29183. bootstrap->pub_key->data = der;
  29184. bootstrap->pub_key->length = (int)len;
  29185. /* Not actually used */
  29186. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  29187. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  29188. }
  29189. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, NULL), 0);
  29190. der = NULL;
  29191. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29192. ExpectIntGT(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29193. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  29194. EVP_PKEY_free(key);
  29195. EC_KEY_free(eckey);
  29196. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  29197. bootstrap = NULL;
  29198. DPP_BOOTSTRAPPING_KEY_free(NULL);
  29199. /* Create bootstrap key with bad OBJECT_ID DER data, parameter that is
  29200. * a NULL and an empty BIT_STRING. */
  29201. ExpectNotNull(bootstrap = DPP_BOOTSTRAPPING_KEY_new());
  29202. ExpectNotNull(bootstrap->alg->algorithm = wolfSSL_ASN1_OBJECT_new());
  29203. if (EXPECT_SUCCESS()) {
  29204. bootstrap->alg->algorithm->obj = badObjDer;
  29205. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(badObjDer);
  29206. }
  29207. ExpectNotNull(bootstrap->alg->parameter = wolfSSL_ASN1_TYPE_new());
  29208. if (EXPECT_SUCCESS()) {
  29209. bootstrap->alg->parameter->type = V_ASN1_NULL;
  29210. bootstrap->alg->parameter->value.ptr = NULL;
  29211. bootstrap->pub_key->data = NULL;
  29212. bootstrap->pub_key->length = 0;
  29213. /* Not actually used */
  29214. bootstrap->pub_key->flags &= ~(ASN1_STRING_FLAG_BITS_LEFT | 0x07);
  29215. bootstrap->pub_key->flags |= ASN1_STRING_FLAG_BITS_LEFT;
  29216. }
  29217. /* Encode with bad OBJECT_ID. */
  29218. der = NULL;
  29219. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 0);
  29220. /* Fix OBJECT_ID and encode with empty BIT_STRING. */
  29221. if (EXPECT_SUCCESS()) {
  29222. bootstrap->alg->algorithm->obj = goodObjDer;
  29223. bootstrap->alg->algorithm->objSz = (unsigned int)sizeof(goodObjDer);
  29224. bootstrap->alg->algorithm->grp = 2;
  29225. }
  29226. der = NULL;
  29227. ExpectIntEQ(i2d_DPP_BOOTSTRAPPING_KEY(bootstrap, &der), 16);
  29228. ExpectIntEQ(wolfSSL_ASN1_item_i2d(bootstrap, &der, &emptyTemplate), 0);
  29229. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  29230. DPP_BOOTSTRAPPING_KEY_free(bootstrap);
  29231. /* Test integer */
  29232. ExpectNotNull(test_asn1 = TEST_ASN1_new());
  29233. der = NULL;
  29234. ExpectIntEQ(ASN1_INTEGER_set(test_asn1->integer, 100), 1);
  29235. ExpectIntEQ(i2d_TEST_ASN1(test_asn1, &der), 5);
  29236. XFREE(der, NULL, DYNAMIC_TYPE_ASN1);
  29237. TEST_ASN1_free(test_asn1);
  29238. /* Test integer cases. */
  29239. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  29240. TEST_ASN1_free(NULL);
  29241. /* Test error cases. */
  29242. ExpectNull(TEST_FAIL_ASN1_new());
  29243. ExpectNull(wolfSSL_ASN1_item_new(NULL));
  29244. TEST_FAIL_ASN1_free(NULL);
  29245. XMEMSET(&test_fail_asn1, 0, sizeof(TEST_FAIL_ASN1));
  29246. ExpectIntEQ(i2d_TEST_FAIL_ASN1(&test_fail_asn1, &der), 0);
  29247. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  29248. #endif /* OPENSSL_ALL && HAVE_ECC && USE_CERT_BUFFERS_256 */
  29249. return EXPECT_RESULT();
  29250. }
  29251. static int test_wolfSSL_lhash(void)
  29252. {
  29253. EXPECT_DECLS;
  29254. #ifdef OPENSSL_ALL
  29255. const char testStr[] = "Like a true nature's child\n"
  29256. "We were born\n"
  29257. "Born to be wild";
  29258. #ifdef NO_SHA
  29259. ExpectIntEQ(lh_strhash(testStr), 0xf9dc8a43);
  29260. #else
  29261. ExpectIntEQ(lh_strhash(testStr), 0x5b7541dc);
  29262. #endif
  29263. #endif
  29264. return EXPECT_RESULT();
  29265. }
  29266. static int test_wolfSSL_X509_NAME(void)
  29267. {
  29268. EXPECT_DECLS;
  29269. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  29270. !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29271. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN) && \
  29272. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT) || \
  29273. defined(OPENSSL_EXTRA))
  29274. X509* x509 = NULL;
  29275. const unsigned char* c = NULL;
  29276. unsigned char buf[4096];
  29277. int bytes = 0;
  29278. XFILE f = XBADFILE;
  29279. const X509_NAME* a = NULL;
  29280. const X509_NAME* b = NULL;
  29281. X509_NAME* d2i_name = NULL;
  29282. int sz = 0;
  29283. unsigned char* tmp = NULL;
  29284. char file[] = "./certs/ca-cert.der";
  29285. #ifndef OPENSSL_EXTRA_X509_SMALL
  29286. byte empty[] = { /* CN=empty emailAddress= */
  29287. 0x30, 0x21, 0x31, 0x0E, 0x30, 0x0C, 0x06, 0x03,
  29288. 0x55, 0x04, 0x03, 0x0C, 0x05, 0x65, 0x6D, 0x70,
  29289. 0x74, 0x79, 0x31, 0x0F, 0x30, 0x0D, 0x06, 0x09,
  29290. 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09,
  29291. 0x01, 0x16, 0x00
  29292. };
  29293. #endif
  29294. #ifndef OPENSSL_EXTRA_X509_SMALL
  29295. /* test compile of deprecated function, returns 0 */
  29296. ExpectIntEQ(CRYPTO_thread_id(), 0);
  29297. #endif
  29298. ExpectNotNull(a = X509_NAME_new());
  29299. X509_NAME_free((X509_NAME*)a);
  29300. a = NULL;
  29301. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  29302. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  29303. if (f != XBADFILE)
  29304. XFCLOSE(f);
  29305. c = buf;
  29306. ExpectNotNull(x509 = wolfSSL_X509_d2i_ex(NULL, c, bytes, HEAP_HINT));
  29307. /* test cmp function */
  29308. ExpectNotNull(a = X509_get_issuer_name(x509));
  29309. ExpectNotNull(b = X509_get_subject_name(x509));
  29310. #ifndef OPENSSL_EXTRA_X509_SMALL
  29311. ExpectIntEQ(X509_NAME_cmp(a, b), 0); /* self signed should be 0 */
  29312. #endif
  29313. tmp = buf;
  29314. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)a, &tmp)), 0);
  29315. if (sz > 0 && tmp == buf) {
  29316. fprintf(stderr, "\nERROR - %s line %d failed with:", __FILE__,
  29317. __LINE__);
  29318. fprintf(stderr, " Expected pointer to be incremented\n");
  29319. abort();
  29320. }
  29321. #ifndef OPENSSL_EXTRA_X509_SMALL
  29322. tmp = buf;
  29323. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  29324. #endif
  29325. /* if output parameter is NULL, should still return required size. */
  29326. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, NULL)), 0);
  29327. /* retry but with the function creating a buffer */
  29328. tmp = NULL;
  29329. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  29330. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  29331. tmp = NULL;
  29332. #ifdef WOLFSSL_CERT_NAME_ALL
  29333. /* test for givenName and name */
  29334. {
  29335. WOLFSSL_X509_NAME_ENTRY* entry = NULL;
  29336. const byte gName[] = "test-given-name";
  29337. const byte name[] = "test-name";
  29338. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  29339. NID_givenName, ASN_UTF8STRING, gName, sizeof(gName)));
  29340. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  29341. 1);
  29342. wolfSSL_X509_NAME_ENTRY_free(entry);
  29343. entry = NULL;
  29344. ExpectNotNull(entry = wolfSSL_X509_NAME_ENTRY_create_by_NID(NULL,
  29345. NID_name, ASN_UTF8STRING, name, sizeof(name)));
  29346. ExpectIntEQ(wolfSSL_X509_NAME_add_entry((X509_NAME*)b, entry, -1, 0),
  29347. 1);
  29348. wolfSSL_X509_NAME_ENTRY_free(entry);
  29349. tmp = NULL;
  29350. ExpectIntGT((sz = i2d_X509_NAME((X509_NAME*)b, &tmp)), 0);
  29351. XFREE(tmp, NULL, DYNAMIC_TYPE_OPENSSL);
  29352. }
  29353. #endif
  29354. b = NULL;
  29355. ExpectNotNull(b = X509_NAME_dup((X509_NAME*)a));
  29356. #ifndef OPENSSL_EXTRA_X509_SMALL
  29357. ExpectIntEQ(X509_NAME_cmp(a, b), 0);
  29358. #endif
  29359. X509_NAME_free((X509_NAME*)b);
  29360. X509_NAME_free(d2i_name);
  29361. d2i_name = NULL;
  29362. X509_free(x509);
  29363. #ifndef OPENSSL_EXTRA_X509_SMALL
  29364. /* test with an empty domain component */
  29365. tmp = empty;
  29366. sz = sizeof(empty);
  29367. ExpectNotNull(d2i_name = d2i_X509_NAME(NULL, &tmp, sz));
  29368. ExpectIntEQ(X509_NAME_entry_count(d2i_name), 2);
  29369. /* size of empty emailAddress will be 0 */
  29370. tmp = buf;
  29371. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_emailAddress,
  29372. (char*)tmp, sizeof(buf)), 0);
  29373. /* should contain no organization name */
  29374. tmp = buf;
  29375. ExpectIntEQ(X509_NAME_get_text_by_NID(d2i_name, NID_organizationName,
  29376. (char*)tmp, sizeof(buf)), -1);
  29377. X509_NAME_free(d2i_name);
  29378. #endif
  29379. #endif
  29380. return EXPECT_RESULT();
  29381. }
  29382. static int test_wolfSSL_X509_NAME_hash(void)
  29383. {
  29384. EXPECT_DECLS;
  29385. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  29386. !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_BIO)
  29387. BIO* bio = NULL;
  29388. X509* x509 = NULL;
  29389. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  29390. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  29391. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  29392. ExpectIntEQ(X509_NAME_hash(X509_get_subject_name(x509)), 0x137DC03F);
  29393. ExpectIntEQ(X509_NAME_hash(X509_get_issuer_name(x509)), 0xFDB2DA4);
  29394. X509_free(x509);
  29395. BIO_free(bio);
  29396. #endif
  29397. return EXPECT_RESULT();
  29398. }
  29399. static int test_wolfSSL_X509_NAME_print_ex(void)
  29400. {
  29401. EXPECT_DECLS;
  29402. #if (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  29403. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  29404. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  29405. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))) && \
  29406. !defined(NO_BIO) && !defined(NO_RSA)
  29407. int memSz = 0;
  29408. byte* mem = NULL;
  29409. BIO* bio = NULL;
  29410. BIO* membio = NULL;
  29411. X509* x509 = NULL;
  29412. X509_NAME* name = NULL;
  29413. const char* expNormal = "C=US, CN=wolfssl.com";
  29414. const char* expReverse = "CN=wolfssl.com, C=US";
  29415. const char* expNotEscaped = "C= US,+\"\\ , CN=#wolfssl.com<>;";
  29416. const char* expNotEscapedRev = "CN=#wolfssl.com<>;, C= US,+\"\\ ";
  29417. const char* expRFC5523 =
  29418. "CN=\\#wolfssl.com\\<\\>\\;, C=\\ US\\,\\+\\\"\\\\\\ ";
  29419. /* Test with real cert (svrCertFile) first */
  29420. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  29421. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  29422. ExpectNotNull(PEM_read_bio_X509(bio, &x509, NULL, NULL));
  29423. ExpectNotNull(name = X509_get_subject_name(x509));
  29424. /* Test without flags */
  29425. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29426. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29427. BIO_free(membio);
  29428. membio = NULL;
  29429. /* Test flag: XN_FLAG_RFC2253 */
  29430. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29431. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29432. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29433. BIO_free(membio);
  29434. membio = NULL;
  29435. /* Test flag: XN_FLAG_RFC2253 | XN_FLAG_DN_REV */
  29436. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29437. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29438. XN_FLAG_RFC2253 | XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29439. BIO_free(membio);
  29440. membio = NULL;
  29441. X509_free(x509);
  29442. BIO_free(bio);
  29443. name = NULL;
  29444. /* Test normal case without escaped characters */
  29445. {
  29446. /* Create name: "/C=US/CN=wolfssl.com" */
  29447. ExpectNotNull(name = X509_NAME_new());
  29448. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  29449. MBSTRING_UTF8, (byte*)"US", 2, -1, 0),
  29450. WOLFSSL_SUCCESS);
  29451. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  29452. MBSTRING_UTF8, (byte*)"wolfssl.com", 11, -1, 0),
  29453. WOLFSSL_SUCCESS);
  29454. /* Test without flags */
  29455. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29456. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29457. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29458. ExpectIntEQ(memSz, XSTRLEN(expNormal));
  29459. ExpectIntEQ(XSTRNCMP((char*)mem, expNormal, XSTRLEN(expNormal)), 0);
  29460. BIO_free(membio);
  29461. membio = NULL;
  29462. /* Test flags: XN_FLAG_RFC2253 - should be reversed */
  29463. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29464. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29465. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29466. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29467. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  29468. BIO_free(membio);
  29469. membio = NULL;
  29470. /* Test flags: XN_FLAG_DN_REV - reversed */
  29471. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29472. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29473. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29474. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29475. ExpectIntEQ(memSz, XSTRLEN(expReverse));
  29476. ExpectIntEQ(XSTRNCMP((char*)mem, expReverse, XSTRLEN(expReverse)), 0);
  29477. BIO_free(membio);
  29478. membio = NULL;
  29479. X509_NAME_free(name);
  29480. name = NULL;
  29481. }
  29482. /* Test RFC2253 characters are escaped with backslashes */
  29483. {
  29484. ExpectNotNull(name = X509_NAME_new());
  29485. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName",
  29486. /* space at beginning and end, and: ,+"\ */
  29487. MBSTRING_UTF8, (byte*)" US,+\"\\ ", 8, -1, 0),
  29488. WOLFSSL_SUCCESS);
  29489. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName",
  29490. /* # at beginning, and: <>;*/
  29491. MBSTRING_UTF8, (byte*)"#wolfssl.com<>;", 15, -1, 0),
  29492. WOLFSSL_SUCCESS);
  29493. /* Test without flags */
  29494. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29495. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0, 0), WOLFSSL_SUCCESS);
  29496. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29497. ExpectIntEQ(memSz, XSTRLEN(expNotEscaped));
  29498. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscaped,
  29499. XSTRLEN(expNotEscaped)), 0);
  29500. BIO_free(membio);
  29501. membio = NULL;
  29502. /* Test flags: XN_FLAG_RFC5523 - should be reversed and escaped */
  29503. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29504. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29505. XN_FLAG_RFC2253), WOLFSSL_SUCCESS);
  29506. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29507. ExpectIntEQ(memSz, XSTRLEN(expRFC5523));
  29508. ExpectIntEQ(XSTRNCMP((char*)mem, expRFC5523, XSTRLEN(expRFC5523)), 0);
  29509. BIO_free(membio);
  29510. membio = NULL;
  29511. /* Test flags: XN_FLAG_DN_REV - reversed but not escaped */
  29512. ExpectNotNull(membio = BIO_new(BIO_s_mem()));
  29513. ExpectIntEQ(X509_NAME_print_ex(membio, name, 0,
  29514. XN_FLAG_DN_REV), WOLFSSL_SUCCESS);
  29515. ExpectIntGE((memSz = BIO_get_mem_data(membio, &mem)), 0);
  29516. ExpectIntEQ(memSz, XSTRLEN(expNotEscapedRev));
  29517. ExpectIntEQ(XSTRNCMP((char*)mem, expNotEscapedRev,
  29518. XSTRLEN(expNotEscapedRev)), 0);
  29519. BIO_free(membio);
  29520. X509_NAME_free(name);
  29521. }
  29522. #endif
  29523. return EXPECT_RESULT();
  29524. }
  29525. #ifndef NO_BIO
  29526. static int test_wolfSSL_X509_INFO_multiple_info(void)
  29527. {
  29528. EXPECT_DECLS;
  29529. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  29530. STACK_OF(X509_INFO) *info_stack = NULL;
  29531. X509_INFO *info = NULL;
  29532. int len;
  29533. int i;
  29534. const char* files[] = {
  29535. cliCertFile,
  29536. cliKeyFile,
  29537. /* This needs to be the order as svrCertFile contains the
  29538. * intermediate cert as well. */
  29539. svrKeyFile,
  29540. svrCertFile,
  29541. NULL,
  29542. };
  29543. const char** curFile;
  29544. BIO *fileBIO = NULL;
  29545. BIO *concatBIO = NULL;
  29546. byte tmp[FOURK_BUF];
  29547. /* concatenate the cert and the key file to force PEM_X509_INFO_read_bio
  29548. * to group objects together. */
  29549. ExpectNotNull(concatBIO = BIO_new(BIO_s_mem()));
  29550. for (curFile = files; EXPECT_SUCCESS() && *curFile != NULL; curFile++) {
  29551. int fileLen = 0;
  29552. ExpectNotNull(fileBIO = BIO_new_file(*curFile, "rb"));
  29553. ExpectIntGT(fileLen = wolfSSL_BIO_get_len(fileBIO), 0);
  29554. if (EXPECT_SUCCESS()) {
  29555. while ((len = BIO_read(fileBIO, tmp, sizeof(tmp))) > 0) {
  29556. ExpectIntEQ(BIO_write(concatBIO, tmp, len), len);
  29557. fileLen -= len;
  29558. if (EXPECT_FAIL())
  29559. break;
  29560. }
  29561. /* Make sure we read the entire file */
  29562. ExpectIntEQ(fileLen, 0);
  29563. }
  29564. BIO_free(fileBIO);
  29565. fileBIO = NULL;
  29566. }
  29567. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(concatBIO, NULL, NULL,
  29568. NULL));
  29569. ExpectIntEQ(sk_X509_INFO_num(info_stack), 3);
  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. if (i != 0) {
  29575. ExpectNotNull(info->x_pkey);
  29576. ExpectIntEQ(X509_check_private_key(info->x509,
  29577. info->x_pkey->dec_pkey), 1);
  29578. }
  29579. else {
  29580. ExpectNull(info->x_pkey);
  29581. }
  29582. }
  29583. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29584. BIO_free(concatBIO);
  29585. #endif
  29586. return EXPECT_RESULT();
  29587. }
  29588. #endif
  29589. #ifndef NO_BIO
  29590. static int test_wolfSSL_X509_INFO(void)
  29591. {
  29592. EXPECT_DECLS;
  29593. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  29594. STACK_OF(X509_INFO) *info_stack = NULL;
  29595. X509_INFO *info = NULL;
  29596. BIO *cert = NULL;
  29597. int i;
  29598. /* PEM in hex format to avoid null terminator */
  29599. byte data[] = {
  29600. 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x42, 0x45, 0x47,
  29601. 0x49, 0x4e, 0x20, 0x43, 0x45, 0x52, 0x54, 0x63, 0x2d, 0x2d, 0x2d, 0x2d,
  29602. 0x2d, 0x0a, 0x4d, 0x49, 0x49, 0x44, 0x4d, 0x54, 0x42, 0x75, 0x51, 0x3d,
  29603. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d, 0x45, 0x4e, 0x44, 0x20, 0x2d, 0x2d,
  29604. 0x2d, 0x2d, 0x2d
  29605. };
  29606. /* PEM in hex format to avoid null terminator */
  29607. byte data2[] = {
  29608. 0x41, 0x53, 0x4e, 0x31, 0x20, 0x4f, 0x49, 0x44, 0x3a, 0x20, 0x70, 0x72,
  29609. 0x69, 0x6d, 0x65, 0x32, 0x35, 0x36, 0x76, 0x31, 0x0a, 0x2d, 0x2d, 0x2d,
  29610. 0x2d, 0x2d, 0x42, 0x45, 0x47, 0x49, 0x4e, 0x20, 0x45, 0x43, 0x20, 0x50,
  29611. 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x53, 0x2d, 0x2d, 0x2d,
  29612. 0x2d, 0x43, 0x65, 0x72, 0x74, 0x69, 0x2d, 0x0a, 0x42, 0x67, 0x67, 0x71,
  29613. 0x68, 0x6b, 0x6a, 0x4f, 0x50, 0x51, 0x4d, 0x42, 0x42, 0x77, 0x3d, 0x3d,
  29614. 0x0a, 0x2d, 0x2d, 0x2d, 0x2d, 0x2d
  29615. };
  29616. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  29617. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29618. for (i = 0; i < sk_X509_INFO_num(info_stack); i++) {
  29619. ExpectNotNull(info = sk_X509_INFO_value(info_stack, i));
  29620. ExpectNotNull(info->x509);
  29621. ExpectNull(info->crl);
  29622. ExpectNull(info->x_pkey);
  29623. }
  29624. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29625. info_stack = NULL;
  29626. BIO_free(cert);
  29627. cert = NULL;
  29628. ExpectNotNull(cert = BIO_new_file(cliCertFileExt, "rb"));
  29629. ExpectNotNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29630. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29631. info_stack = NULL;
  29632. BIO_free(cert);
  29633. cert = NULL;
  29634. /* This case should fail due to invalid input. */
  29635. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  29636. ExpectIntEQ(BIO_write(cert, data, sizeof(data)), sizeof(data));
  29637. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29638. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29639. info_stack = NULL;
  29640. BIO_free(cert);
  29641. cert = NULL;
  29642. ExpectNotNull(cert = BIO_new(BIO_s_mem()));
  29643. ExpectIntEQ(BIO_write(cert, data2, sizeof(data2)), sizeof(data2));
  29644. ExpectNull(info_stack = PEM_X509_INFO_read_bio(cert, NULL, NULL, NULL));
  29645. sk_X509_INFO_pop_free(info_stack, X509_INFO_free);
  29646. BIO_free(cert);
  29647. #endif
  29648. return EXPECT_RESULT();
  29649. }
  29650. #endif
  29651. static int test_wolfSSL_X509_subject_name_hash(void)
  29652. {
  29653. EXPECT_DECLS;
  29654. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  29655. !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  29656. X509* x509 = NULL;
  29657. X509_NAME* subjectName = NULL;
  29658. unsigned long ret1 = 0;
  29659. unsigned long ret2 = 0;
  29660. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29661. SSL_FILETYPE_PEM));
  29662. ExpectNotNull(subjectName = wolfSSL_X509_get_subject_name(x509));
  29663. /* These two
  29664. * - X509_subject_name_hash(x509)
  29665. * - X509_NAME_hash(X509_get_subject_name(x509))
  29666. * should give the same hash, if !defined(NO_SHA) is true. */
  29667. ret1 = X509_subject_name_hash(x509);
  29668. ExpectIntNE(ret1, 0);
  29669. #if !defined(NO_SHA)
  29670. ret2 = X509_NAME_hash(X509_get_subject_name(x509));
  29671. ExpectIntNE(ret2, 0);
  29672. ExpectIntEQ(ret1, ret2);
  29673. #else
  29674. (void) ret2;
  29675. #endif
  29676. X509_free(x509);
  29677. #endif
  29678. return EXPECT_RESULT();
  29679. }
  29680. static int test_wolfSSL_X509_issuer_name_hash(void)
  29681. {
  29682. EXPECT_DECLS;
  29683. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  29684. && !defined(NO_RSA) && (!defined(NO_SHA) || !defined(NO_SHA256))
  29685. X509* x509 = NULL;
  29686. X509_NAME* issuertName = NULL;
  29687. unsigned long ret1 = 0;
  29688. unsigned long ret2 = 0;
  29689. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29690. SSL_FILETYPE_PEM));
  29691. ExpectNotNull(issuertName = wolfSSL_X509_get_issuer_name(x509));
  29692. /* These two
  29693. * - X509_issuer_name_hash(x509)
  29694. * - X509_NAME_hash(X509_get_issuer_name(x509))
  29695. * should give the same hash, if !defined(NO_SHA) is true. */
  29696. ret1 = X509_issuer_name_hash(x509);
  29697. ExpectIntNE(ret1, 0);
  29698. #if !defined(NO_SHA)
  29699. ret2 = X509_NAME_hash(X509_get_issuer_name(x509));
  29700. ExpectIntNE(ret2, 0);
  29701. ExpectIntEQ(ret1, ret2);
  29702. #else
  29703. (void) ret2;
  29704. #endif
  29705. X509_free(x509);
  29706. #endif
  29707. return EXPECT_RESULT();
  29708. }
  29709. static int test_wolfSSL_X509_check_host(void)
  29710. {
  29711. EXPECT_DECLS;
  29712. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) \
  29713. && !defined(NO_SHA) && !defined(NO_RSA)
  29714. X509* x509 = NULL;
  29715. const char altName[] = "example.com";
  29716. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29717. SSL_FILETYPE_PEM));
  29718. ExpectIntEQ(X509_check_host(x509, altName, XSTRLEN(altName), 0, NULL),
  29719. WOLFSSL_SUCCESS);
  29720. ExpectIntEQ(X509_check_host(x509, NULL, 0, 0, NULL),
  29721. WOLFSSL_FAILURE);
  29722. X509_free(x509);
  29723. ExpectIntEQ(X509_check_host(NULL, altName, XSTRLEN(altName), 0, NULL),
  29724. WOLFSSL_FAILURE);
  29725. #endif
  29726. return EXPECT_RESULT();
  29727. }
  29728. static int test_wolfSSL_X509_check_email(void)
  29729. {
  29730. EXPECT_DECLS;
  29731. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_CERT_GEN) && !defined(NO_RSA)
  29732. X509* x509 = NULL;
  29733. const char goodEmail[] = "info@wolfssl.com";
  29734. const char badEmail[] = "disinfo@wolfssl.com";
  29735. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  29736. SSL_FILETYPE_PEM));
  29737. /* Should fail on non-matching email address */
  29738. ExpectIntEQ(wolfSSL_X509_check_email(x509, badEmail, XSTRLEN(badEmail), 0),
  29739. WOLFSSL_FAILURE);
  29740. /* Should succeed on matching email address */
  29741. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, XSTRLEN(goodEmail), 0),
  29742. WOLFSSL_SUCCESS);
  29743. /* Should compute length internally when not provided */
  29744. ExpectIntEQ(wolfSSL_X509_check_email(x509, goodEmail, 0, 0),
  29745. WOLFSSL_SUCCESS);
  29746. /* Should fail when email address is NULL */
  29747. ExpectIntEQ(wolfSSL_X509_check_email(x509, NULL, 0, 0),
  29748. WOLFSSL_FAILURE);
  29749. X509_free(x509);
  29750. /* Should fail when x509 is NULL */
  29751. ExpectIntEQ(wolfSSL_X509_check_email(NULL, goodEmail, 0, 0),
  29752. WOLFSSL_FAILURE);
  29753. #endif /* OPENSSL_EXTRA && WOLFSSL_CERT_GEN */
  29754. return EXPECT_RESULT();
  29755. }
  29756. static int test_wc_PemToDer(void)
  29757. {
  29758. EXPECT_DECLS;
  29759. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  29760. int ret;
  29761. DerBuffer* pDer = NULL;
  29762. const char* ca_cert = "./certs/server-cert.pem";
  29763. byte* cert_buf = NULL;
  29764. size_t cert_sz = 0;
  29765. int eccKey = 0;
  29766. EncryptedInfo info;
  29767. XMEMSET(&info, 0, sizeof(info));
  29768. ExpectIntEQ(ret = load_file(ca_cert, &cert_buf, &cert_sz), 0);
  29769. ExpectIntEQ(ret = wc_PemToDer(cert_buf, (long int)cert_sz, CERT_TYPE, &pDer, NULL,
  29770. &info, &eccKey), 0);
  29771. wc_FreeDer(&pDer);
  29772. pDer = NULL;
  29773. if (cert_buf != NULL) {
  29774. free(cert_buf);
  29775. cert_buf = NULL;
  29776. }
  29777. #ifdef HAVE_ECC
  29778. {
  29779. const char* ecc_private_key = "./certs/ecc-privOnlyKey.pem";
  29780. byte key_buf[256] = {0};
  29781. /* Test fail of loading a key with cert type */
  29782. ExpectIntEQ(load_file(ecc_private_key, &cert_buf, &cert_sz), 0);
  29783. key_buf[0] = '\n';
  29784. ExpectNotNull(XMEMCPY(key_buf + 1, cert_buf, cert_sz));
  29785. ExpectIntNE((ret = wc_PemToDer(key_buf, cert_sz + 1, CERT_TYPE,
  29786. &pDer, NULL, &info, &eccKey)), 0);
  29787. #ifdef OPENSSL_EXTRA
  29788. ExpectIntEQ((ret = wc_PemToDer(key_buf, cert_sz + 1, PRIVATEKEY_TYPE,
  29789. &pDer, NULL, &info, &eccKey)), 0);
  29790. #endif
  29791. wc_FreeDer(&pDer);
  29792. if (cert_buf != NULL)
  29793. free(cert_buf);
  29794. }
  29795. #endif
  29796. #endif
  29797. return EXPECT_RESULT();
  29798. }
  29799. static int test_wc_AllocDer(void)
  29800. {
  29801. EXPECT_DECLS;
  29802. #if !defined(NO_CERTS)
  29803. DerBuffer* pDer = NULL;
  29804. word32 testSize = 1024;
  29805. ExpectIntEQ(wc_AllocDer(NULL, testSize, CERT_TYPE, HEAP_HINT),
  29806. BAD_FUNC_ARG);
  29807. ExpectIntEQ(wc_AllocDer(&pDer, testSize, CERT_TYPE, HEAP_HINT), 0);
  29808. ExpectNotNull(pDer);
  29809. wc_FreeDer(&pDer);
  29810. #endif
  29811. return EXPECT_RESULT();
  29812. }
  29813. static int test_wc_CertPemToDer(void)
  29814. {
  29815. EXPECT_DECLS;
  29816. #if !defined(NO_CERTS) && defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM)
  29817. const char* ca_cert = "./certs/ca-cert.pem";
  29818. byte* cert_buf = NULL;
  29819. size_t cert_sz = 0;
  29820. size_t cert_dersz = 0;
  29821. byte* cert_der = NULL;
  29822. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  29823. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  29824. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29825. ExpectIntGE(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  29826. (int)cert_dersz, CERT_TYPE), 0);
  29827. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, -1, CERT_TYPE),
  29828. BAD_FUNC_ARG);
  29829. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL, -1, CERT_TYPE),
  29830. BAD_FUNC_ARG);
  29831. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der, -1, CERT_TYPE),
  29832. BAD_FUNC_ARG);
  29833. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, NULL, (int)cert_dersz,
  29834. CERT_TYPE), BAD_FUNC_ARG);
  29835. ExpectIntEQ(wc_CertPemToDer(NULL, (int)cert_sz, cert_der,
  29836. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  29837. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, NULL,
  29838. (int)cert_dersz, CERT_TYPE), BAD_FUNC_ARG);
  29839. ExpectIntEQ(wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der, -1,
  29840. CERT_TYPE), BAD_FUNC_ARG);
  29841. if (cert_der != NULL)
  29842. free(cert_der);
  29843. if (cert_buf != NULL)
  29844. free(cert_buf);
  29845. #endif
  29846. return EXPECT_RESULT();
  29847. }
  29848. static int test_wc_KeyPemToDer(void)
  29849. {
  29850. EXPECT_DECLS;
  29851. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  29852. int ret = 0;
  29853. const byte cert_buf[] = \
  29854. "-----BEGIN PRIVATE KEY-----\n"
  29855. "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDMG5KgWxP002pA\n"
  29856. "QJIdA4H5N0oM1Wf0LrHcos5RYUlrHDkC2b5p2BUpVRPmgDAFD2+8leim98x0BvcB\n"
  29857. "k48TNzrVynuwyVEY664+iQyzEBO5v27HPRydOddprbLCvRO036XINGIjauy1jHFi\n"
  29858. "HaDVx3bexSwgp9aefUGAszFXi4q1J4GacV7Cr2b/wBqUHqWv4ZXPu6R9/UYngTkD\n"
  29859. "UDJL5gLlLfcLzNyyodKPHPCIAKdWn6mSVdcHk8XVpK4y9lgz4E7YDWA6ohKZgWgG\n"
  29860. "2RDha8CMilFMDgYa0G0SiS9g3PQx0qh3AMXJJsKSVhScFCZufAE0kV6KvjP7jAqP\n"
  29861. "XBiSkRGPAgMBAAECggEAW7hmRyY2jRX2UMJThrM9VIs6fRLnYI0dQ0tsEJj536ay\n"
  29862. "nevQjArc05KWW0Yujg+WRDZPcry3RUqd9Djlmhp/F3Si6dpF1b+PMS3wJYVrf9Sd\n"
  29863. "SO5W7faArU4vnyBNe0HnY1Ta5xSVI65lg1RSIs88RTZwsooJwXYDGf0shq0/21CE\n"
  29864. "V8HOb27DDYNcEnm35lzaONjFnMqQQT2Vs9anRrPiSEXNleEvTgLVXZtGTyCGTz6v\n"
  29865. "x86Y8eSWL9YNHvPE1I+mDPuocfSR7eRNgRu7SK3mn94W5mqd7Ns072YKX/2XN1mO\n"
  29866. "66+ZFHO6v4dK1u7cSjuwrU1EhLHpUsgDz6Bna5InyQKBgQDv5l8RPy8UneKSADaf\n"
  29867. "M5L/5675I/5t4nqVjvbnQje00YveLTAEjlJBNR93Biln3sYgnvNamYDCxyEuUZ/I\n"
  29868. "S/vmBL9PoxfGZow4FcsIBOEbIn3E0SYJgCBNWthquUvGpKsYDnThJuhO+1cVmxAJ\n"
  29869. "BUOjLFnJYHM0a+Vmk9GexT2OBwKBgQDZzkUBOK7Im3eiYytFocUJyhqMH30d49X9\n"
  29870. "ujC7kGw4UWAqVe7YCSvlBa8nzWpRWK2kRpu3M0272RU0V4geyWqT+nr/SvRRPtNP\n"
  29871. "F5dY8l3yR7hjtSejqqjOfBcZT6ETJxI4tiG0+Nl5BlfM5M+0nxnkWpRcHuOR3j79\n"
  29872. "YUFERyN+OQKBgQCjlOKeUAc6d65W/+4/AFvsQ378Q57qLtSHxsR1TKHPmlNVXFqx\n"
  29873. "wJo1/JNIBduWCEHxXHF0BdfW+RGXE/FwEt/hKLuLAhrkHmjelX2sKieU6R/5ZOQa\n"
  29874. "9lMQbDHGFDOncAF6leD85hriQGBRSzrT69MDIOrYdfwYcroqCAGX0cb3YQKBgQC8\n"
  29875. "iIFQylj5SyHmjcMSNjKSA8CxFDzAV8yPIdE3Oo+CvGXqn5HsrRuy1hXE9VmXapR8\n"
  29876. "A6ackSszdHiXY0FvrNe1mfdH7wDHJwPQjdIzazCJHS3uGQxj7sDKY7226ie6pXJv\n"
  29877. "ZrCMr2/IBAaSVGm6ppHKCeIsT4ybYm7R85KEYLPHeQKBgBeJOMBinXQfWN/1jT9b\n"
  29878. "6Ywrutvp2zP8hVxQGSZJ0WG4iewZyFLsPUlbWRXOSYNPElHmdD0ZomdLVm+lSpAA\n"
  29879. "XSH5FJ/IFCwqq7Eft6Gf8NFRV+NjPMUny+PnjHe4oFP8YK/Ek22K3ttNG8Hw69Aw\n"
  29880. "AQue5o6oVfhgLiJzMdo/77gw\n"
  29881. "-----END PRIVATE KEY-----\n";
  29882. const int cert_sz = sizeof(cert_buf);
  29883. const char cert_pw[] = "password";
  29884. int cert_dersz = 0;
  29885. byte* cert_der = NULL;
  29886. /* Bad arg: Cert buffer is NULL */
  29887. ExpectIntEQ(wc_KeyPemToDer(NULL, cert_sz, cert_der, cert_dersz, ""),
  29888. BAD_FUNC_ARG);
  29889. /* Bad arg: Cert DER buffer non-NULL but size zero (or less) */
  29890. ExpectIntEQ(wc_KeyPemToDer(cert_buf, cert_sz, (byte*)&cert_der, 0, ""),
  29891. BAD_FUNC_ARG);
  29892. /* Test normal operation */
  29893. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  29894. ExpectNotNull(cert_der = (byte*)malloc((size_t)cert_dersz));
  29895. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  29896. cert_pw), 0);
  29897. ExpectIntLE(ret, cert_sz);
  29898. if (cert_der != NULL) {
  29899. free(cert_der);
  29900. cert_der = NULL;
  29901. }
  29902. /* Test NULL for DER buffer to return needed DER buffer size */
  29903. ExpectIntGT(ret = wc_KeyPemToDer(cert_buf, cert_sz, NULL, 0, ""), 0);
  29904. ExpectIntLE(ret, cert_sz);
  29905. if (EXPECT_SUCCESS())
  29906. cert_dersz = ret;
  29907. ExpectNotNull(cert_der = (byte*)malloc((size_t)cert_dersz));
  29908. ExpectIntGE(ret = wc_KeyPemToDer(cert_buf, cert_sz, cert_der, cert_dersz,
  29909. cert_pw), 0);
  29910. ExpectIntLE(ret, cert_sz);
  29911. if (cert_der != NULL)
  29912. free(cert_der);
  29913. #endif
  29914. return EXPECT_RESULT();
  29915. }
  29916. static int test_wc_PubKeyPemToDer(void)
  29917. {
  29918. EXPECT_DECLS;
  29919. #if defined(WOLFSSL_PEM_TO_DER) && !defined(NO_FILESYSTEM) && \
  29920. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  29921. int ret = 0;
  29922. const char* key = "./certs/ecc-client-keyPub.pem";
  29923. byte* cert_buf = NULL;
  29924. size_t cert_sz = 0, cert_dersz = 0;
  29925. byte* cert_der = NULL;
  29926. ExpectIntEQ(wc_PubKeyPemToDer(cert_buf, (int)cert_sz,
  29927. cert_der, (int)cert_dersz), BAD_FUNC_ARG);
  29928. ExpectIntEQ(load_file(key, &cert_buf, &cert_sz), 0);
  29929. cert_dersz = cert_sz; /* DER will be smaller than PEM */
  29930. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29931. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  29932. (int)cert_dersz), 0);
  29933. if (cert_der != NULL) {
  29934. free(cert_der);
  29935. cert_der = NULL;
  29936. }
  29937. /* Test NULL for DER buffer to return needed DER buffer size */
  29938. ExpectIntGT(ret = wc_PubKeyPemToDer(cert_buf, (int)cert_sz, NULL, 0), 0);
  29939. ExpectIntLE(ret, cert_sz);
  29940. cert_dersz = (size_t)ret;
  29941. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29942. ExpectIntGE(wc_PubKeyPemToDer(cert_buf, (int)cert_sz, cert_der,
  29943. (int)cert_dersz), 0);
  29944. if (cert_der != NULL) {
  29945. free(cert_der);
  29946. }
  29947. if (cert_buf != NULL) {
  29948. free(cert_buf);
  29949. }
  29950. #endif
  29951. return EXPECT_RESULT();
  29952. }
  29953. static int test_wc_PemPubKeyToDer(void)
  29954. {
  29955. EXPECT_DECLS;
  29956. #if !defined(NO_FILESYSTEM) && \
  29957. (defined(WOLFSSL_CERT_EXT) || defined(WOLFSSL_PUB_PEM_TO_DER))
  29958. const char* key = "./certs/ecc-client-keyPub.pem";
  29959. size_t cert_dersz = 1024;
  29960. byte* cert_der = NULL;
  29961. ExpectIntGE(wc_PemPubKeyToDer(NULL, cert_der, (int)cert_dersz),
  29962. BAD_FUNC_ARG);
  29963. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  29964. ExpectIntGE(wc_PemPubKeyToDer(key, cert_der, (int)cert_dersz), 0);
  29965. if (cert_der != NULL) {
  29966. free(cert_der);
  29967. }
  29968. #endif
  29969. return EXPECT_RESULT();
  29970. }
  29971. static int test_wc_GetPubKeyDerFromCert(void)
  29972. {
  29973. EXPECT_DECLS;
  29974. #if !defined(NO_RSA) || defined(HAVE_ECC)
  29975. int ret;
  29976. word32 idx = 0;
  29977. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  29978. word32 keyDerSz = (word32)sizeof(keyDer);
  29979. DecodedCert decoded;
  29980. #if !defined(NO_RSA) && defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  29981. byte certBuf[6000]; /* for PEM and CSR, client-cert.pem is 5-6kB */
  29982. word32 certBufSz = sizeof(certBuf);
  29983. #endif
  29984. #if ((!defined(USE_CERT_BUFFERS_2048) && !defined(USE_CERT_BUFFERS_1024)) || \
  29985. defined(WOLFSSL_CERT_REQ)) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  29986. XFILE fp = XBADFILE;
  29987. #endif
  29988. #ifndef NO_RSA
  29989. RsaKey rsaKey;
  29990. #if defined(USE_CERT_BUFFERS_2048)
  29991. byte* rsaCertDer = (byte*)client_cert_der_2048;
  29992. word32 rsaCertDerSz = sizeof_client_cert_der_2048;
  29993. #elif defined(USE_CERT_BUFFERS_1024)
  29994. byte* rsaCertDer = (byte*)client_cert_der_1024;
  29995. word32 rsaCertDerSz = sizeof_client_cert_der_1024;
  29996. #else
  29997. unsigned char rsaCertDer[TWOK_BUF];
  29998. word32 rsaCertDerSz;
  29999. #endif
  30000. #endif
  30001. #ifdef HAVE_ECC
  30002. ecc_key eccKey;
  30003. #if defined(USE_CERT_BUFFERS_256)
  30004. byte* eccCert = (byte*)cliecc_cert_der_256;
  30005. word32 eccCertSz = sizeof_cliecc_cert_der_256;
  30006. #else
  30007. unsigned char eccCert[ONEK_BUF];
  30008. word32 eccCertSz;
  30009. XFILE fp2 = XBADFILE;
  30010. #endif
  30011. #endif
  30012. #ifndef NO_RSA
  30013. #if !defined(USE_CERT_BUFFERS_1024) && !defined(USE_CERT_BUFFERS_2048)
  30014. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) != XBADFILE);
  30015. ExpectIntGT(rsaCertDerSz = (word32)XFREAD(rsaCertDer, 1, sizeof(rsaCertDer),
  30016. fp), 0);
  30017. if (fp != XBADFILE) {
  30018. XFCLOSE(fp);
  30019. fp = XBADFILE;
  30020. }
  30021. #endif
  30022. /* good test case - RSA DER cert */
  30023. wc_InitDecodedCert(&decoded, rsaCertDer, rsaCertDerSz, NULL);
  30024. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  30025. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  30026. ExpectIntGT(keyDerSz, 0);
  30027. /* sanity check, verify we can import DER public key */
  30028. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  30029. ExpectIntEQ(ret, 0);
  30030. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  30031. if (ret == 0) {
  30032. wc_FreeRsaKey(&rsaKey);
  30033. }
  30034. /* test LENGTH_ONLY_E case */
  30035. keyDerSz = 0;
  30036. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  30037. LENGTH_ONLY_E);
  30038. ExpectIntGT(keyDerSz, 0);
  30039. /* bad args: DecodedCert NULL */
  30040. ExpectIntEQ(wc_GetPubKeyDerFromCert(NULL, keyDer, &keyDerSz), BAD_FUNC_ARG);
  30041. /* bad args: output key buff size */
  30042. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, NULL), BAD_FUNC_ARG);
  30043. /* bad args: zero size output key buffer */
  30044. keyDerSz = 0;
  30045. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  30046. BAD_FUNC_ARG);
  30047. wc_FreeDecodedCert(&decoded);
  30048. /* Certificate Request Tests */
  30049. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_FILESYSTEM)
  30050. {
  30051. XMEMSET(certBuf, 0, sizeof(certBuf));
  30052. ExpectTrue((fp = XFOPEN("./certs/csr.signed.der", "rb")) != XBADFILE);
  30053. ExpectIntGT(certBufSz = (word32)XFREAD(certBuf, 1, certBufSz, fp), 0);
  30054. if (fp != XBADFILE) {
  30055. XFCLOSE(fp);
  30056. }
  30057. wc_InitDecodedCert(&decoded, certBuf, certBufSz, NULL);
  30058. ExpectIntEQ(wc_ParseCert(&decoded, CERTREQ_TYPE, VERIFY, NULL), 0);
  30059. /* good test case - RSA DER certificate request */
  30060. keyDerSz = sizeof(keyDer);
  30061. ExpectIntEQ(ret = wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz),
  30062. 0);
  30063. ExpectIntGT(keyDerSz, 0);
  30064. /* sanity check, verify we can import DER public key */
  30065. ret = wc_InitRsaKey(&rsaKey, HEAP_HINT);
  30066. ExpectIntEQ(ret, 0);
  30067. idx = 0;
  30068. ExpectIntEQ(wc_RsaPublicKeyDecode(keyDer, &idx, &rsaKey, keyDerSz), 0);
  30069. if (ret == 0) {
  30070. wc_FreeRsaKey(&rsaKey);
  30071. }
  30072. wc_FreeDecodedCert(&decoded);
  30073. }
  30074. #endif /* WOLFSSL_CERT_REQ */
  30075. #endif /* NO_RSA */
  30076. #ifdef HAVE_ECC
  30077. #ifndef USE_CERT_BUFFERS_256
  30078. ExpectTrue((fp2 = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  30079. XBADFILE);
  30080. ExpectIntGT(eccCertSz = (word32)XFREAD(eccCert, 1, ONEK_BUF, fp2), 0);
  30081. if (fp2 != XBADFILE) {
  30082. XFCLOSE(fp2);
  30083. }
  30084. #endif
  30085. wc_InitDecodedCert(&decoded, eccCert, eccCertSz, NULL);
  30086. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  30087. /* good test case - ECC */
  30088. XMEMSET(keyDer, 0, sizeof(keyDer));
  30089. keyDerSz = sizeof(keyDer);
  30090. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  30091. ExpectIntGT(keyDerSz, 0);
  30092. /* sanity check, verify we can import DER public key */
  30093. ret = wc_ecc_init(&eccKey);
  30094. ExpectIntEQ(ret, 0);
  30095. idx = 0; /* reset idx to 0, used above in RSA case */
  30096. ExpectIntEQ(wc_EccPublicKeyDecode(keyDer, &idx, &eccKey, keyDerSz), 0);
  30097. if (ret == 0) {
  30098. wc_ecc_free(&eccKey);
  30099. }
  30100. /* test LENGTH_ONLY_E case */
  30101. keyDerSz = 0;
  30102. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, NULL, &keyDerSz),
  30103. LENGTH_ONLY_E);
  30104. ExpectIntGT(keyDerSz, 0);
  30105. wc_FreeDecodedCert(&decoded);
  30106. #endif
  30107. #endif /* !NO_RSA || HAVE_ECC */
  30108. return EXPECT_RESULT();
  30109. }
  30110. static int test_wc_CheckCertSigPubKey(void)
  30111. {
  30112. EXPECT_DECLS;
  30113. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  30114. !defined(NO_RSA) && defined(WOLFSSL_PEM_TO_DER) && defined(HAVE_ECC)
  30115. int ret = 0;
  30116. const char* ca_cert = "./certs/ca-cert.pem";
  30117. byte* cert_buf = NULL;
  30118. size_t cert_sz = 0;
  30119. byte* cert_der = NULL;
  30120. word32 cert_dersz = 0;
  30121. byte keyDer[TWOK_BUF]; /* large enough for up to RSA 2048 */
  30122. word32 keyDerSz = (word32)sizeof(keyDer);
  30123. DecodedCert decoded;
  30124. ExpectIntEQ(load_file(ca_cert, &cert_buf, &cert_sz), 0);
  30125. cert_dersz = (word32)cert_sz; /* DER will be smaller than PEM */
  30126. ExpectNotNull(cert_der = (byte*)malloc(cert_dersz));
  30127. ExpectIntGE(ret = wc_CertPemToDer(cert_buf, (int)cert_sz, cert_der,
  30128. (int)cert_dersz, CERT_TYPE), 0);
  30129. wc_InitDecodedCert(&decoded, cert_der, cert_dersz, NULL);
  30130. ExpectIntEQ(wc_ParseCert(&decoded, CERT_TYPE, NO_VERIFY, NULL), 0);
  30131. ExpectIntEQ(wc_GetPubKeyDerFromCert(&decoded, keyDer, &keyDerSz), 0);
  30132. ExpectIntGT(keyDerSz, 0);
  30133. /* Good test case. */
  30134. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  30135. keyDerSz, RSAk), 0);
  30136. /* No certificate. */
  30137. ExpectIntEQ(wc_CheckCertSigPubKey(NULL, cert_dersz, NULL, keyDer, keyDerSz,
  30138. ECDSAk), BAD_FUNC_ARG);
  30139. /* Bad cert size. */
  30140. ExpectIntNE(ret = wc_CheckCertSigPubKey(cert_der, 0, NULL, keyDer, keyDerSz,
  30141. RSAk), 0);
  30142. ExpectTrue(ret == ASN_PARSE_E || ret == BUFFER_E);
  30143. /* No public key. */
  30144. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, NULL,
  30145. keyDerSz, RSAk), ASN_NO_SIGNER_E);
  30146. /* Bad public key size. */
  30147. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer, 0,
  30148. RSAk), BAD_FUNC_ARG);
  30149. /* Wrong aglo. */
  30150. ExpectIntEQ(wc_CheckCertSigPubKey(cert_der, cert_dersz, NULL, keyDer,
  30151. keyDerSz, ECDSAk), ASN_PARSE_E);
  30152. wc_FreeDecodedCert(&decoded);
  30153. if (cert_der != NULL)
  30154. free(cert_der);
  30155. if (cert_buf != NULL)
  30156. free(cert_buf);
  30157. #endif
  30158. return EXPECT_RESULT();
  30159. }
  30160. static int test_wolfSSL_certs(void)
  30161. {
  30162. EXPECT_DECLS;
  30163. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  30164. !defined(NO_RSA)
  30165. X509* x509ext = NULL;
  30166. #ifdef OPENSSL_ALL
  30167. X509* x509 = NULL;
  30168. WOLFSSL_X509_EXTENSION* ext = NULL;
  30169. ASN1_OBJECT* obj = NULL;
  30170. #endif
  30171. WOLFSSL* ssl = NULL;
  30172. WOLFSSL_CTX* ctx = NULL;
  30173. STACK_OF(ASN1_OBJECT)* sk = NULL;
  30174. ASN1_STRING* asn1_str = NULL;
  30175. AUTHORITY_KEYID* akey = NULL;
  30176. BASIC_CONSTRAINTS* bc = NULL;
  30177. int crit = 0;
  30178. #ifndef NO_WOLFSSL_SERVER
  30179. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  30180. #else
  30181. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  30182. #endif
  30183. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  30184. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30185. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  30186. #if !defined(NO_CHECK_PRIVATE_KEY)
  30187. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_FAILURE);
  30188. #endif
  30189. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  30190. #if !defined(NO_CHECK_PRIVATE_KEY)
  30191. ExpectIntEQ(SSL_CTX_check_private_key(ctx), SSL_SUCCESS);
  30192. #endif
  30193. ExpectNotNull(ssl = SSL_new(ctx));
  30194. #if !defined(NO_CHECK_PRIVATE_KEY)
  30195. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30196. #endif
  30197. #ifdef HAVE_PK_CALLBACKS
  30198. ExpectIntEQ((int)SSL_set_tlsext_debug_arg(ssl, NULL), WOLFSSL_SUCCESS);
  30199. #endif /* HAVE_PK_CALLBACKS */
  30200. /* create and use x509 */
  30201. #ifdef OPENSSL_ALL
  30202. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  30203. WOLFSSL_FILETYPE_PEM));
  30204. #endif
  30205. ExpectNotNull(x509ext = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  30206. WOLFSSL_FILETYPE_PEM));
  30207. ExpectIntEQ(SSL_use_certificate(ssl, x509ext), WOLFSSL_SUCCESS);
  30208. #if !defined(NO_CHECK_PRIVATE_KEY)
  30209. /* with loading in a new cert the check on private key should now fail */
  30210. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30211. #endif
  30212. #if defined(USE_CERT_BUFFERS_2048)
  30213. ExpectIntEQ(SSL_use_certificate_ASN1(ssl,
  30214. (unsigned char*)server_cert_der_2048,
  30215. sizeof_server_cert_der_2048), WOLFSSL_SUCCESS);
  30216. #endif
  30217. #if !defined(NO_SHA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  30218. /************* Get Digest of Certificate ******************/
  30219. {
  30220. byte digest[64]; /* max digest size */
  30221. word32 digestSz;
  30222. XMEMSET(digest, 0, sizeof(digest));
  30223. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha1(), digest, &digestSz),
  30224. WOLFSSL_SUCCESS);
  30225. ExpectIntEQ(X509_digest(x509ext, wolfSSL_EVP_sha256(), digest, &digestSz),
  30226. WOLFSSL_SUCCESS);
  30227. ExpectIntEQ(X509_digest(NULL, wolfSSL_EVP_sha1(), digest, &digestSz),
  30228. WOLFSSL_FAILURE);
  30229. }
  30230. #endif /* !NO_SHA && !NO_SHA256 && !NO_PWDBASED */
  30231. /* test and checkout X509 extensions */
  30232. ExpectNotNull(bc = (BASIC_CONSTRAINTS*)X509_get_ext_d2i(x509ext,
  30233. NID_basic_constraints, &crit, NULL));
  30234. ExpectIntEQ(crit, 0);
  30235. #ifdef OPENSSL_ALL
  30236. ExpectNotNull(ext = X509V3_EXT_i2d(NID_basic_constraints, crit, bc));
  30237. X509_EXTENSION_free(ext);
  30238. ext = NULL;
  30239. ExpectNotNull(ext = X509_EXTENSION_new());
  30240. X509_EXTENSION_set_critical(ext, 1);
  30241. ExpectNotNull(obj = OBJ_nid2obj(NID_basic_constraints));
  30242. ExpectIntEQ(X509_EXTENSION_set_object(ext, obj), SSL_SUCCESS);
  30243. ASN1_OBJECT_free(obj);
  30244. obj = NULL;
  30245. X509_EXTENSION_free(ext);
  30246. ext = NULL;
  30247. ExpectNotNull(ext = X509_EXTENSION_new());
  30248. X509_EXTENSION_set_critical(ext, 0);
  30249. ExpectIntEQ(X509_EXTENSION_set_data(ext, NULL), SSL_FAILURE);
  30250. asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext, NID_key_usage, &crit,
  30251. NULL);
  30252. ExpectIntEQ(X509_EXTENSION_set_data(ext, asn1_str), SSL_SUCCESS);
  30253. ASN1_STRING_free(asn1_str); /* X509_EXTENSION_set_data has made a copy
  30254. * and X509_get_ext_d2i has created new */
  30255. asn1_str = NULL;
  30256. X509_EXTENSION_free(ext);
  30257. ext = NULL;
  30258. #endif
  30259. BASIC_CONSTRAINTS_free(bc);
  30260. bc = NULL;
  30261. ExpectNotNull(asn1_str = (ASN1_STRING*)X509_get_ext_d2i(x509ext,
  30262. NID_key_usage, &crit, NULL));
  30263. ExpectIntEQ(crit, 1);
  30264. ExpectIntEQ(asn1_str->type, NID_key_usage);
  30265. #ifdef OPENSSL_ALL
  30266. ExpectNotNull(ext = X509V3_EXT_i2d(NID_key_usage, crit, asn1_str));
  30267. X509_EXTENSION_free(ext);
  30268. ext = NULL;
  30269. #endif
  30270. ASN1_STRING_free(asn1_str);
  30271. asn1_str = NULL;
  30272. #ifdef OPENSSL_ALL
  30273. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  30274. NID_ext_key_usage, &crit, NULL));
  30275. ExpectNotNull(ext = X509V3_EXT_i2d(NID_ext_key_usage, crit, sk));
  30276. X509_EXTENSION_free(ext);
  30277. ext = NULL;
  30278. EXTENDED_KEY_USAGE_free(sk);
  30279. sk = NULL;
  30280. #else
  30281. sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, NID_ext_key_usage,
  30282. &crit, NULL);
  30283. ExpectNull(sk);
  30284. #endif
  30285. ExpectNotNull(akey = (AUTHORITY_KEYID*)X509_get_ext_d2i(x509ext,
  30286. NID_authority_key_identifier, &crit, NULL));
  30287. #ifdef OPENSSL_ALL
  30288. ExpectNotNull(ext = X509V3_EXT_i2d(NID_authority_key_identifier, crit,
  30289. akey));
  30290. X509_EXTENSION_free(ext);
  30291. ext = NULL;
  30292. #endif
  30293. wolfSSL_AUTHORITY_KEYID_free(akey);
  30294. akey = NULL;
  30295. /* NID not yet supported */
  30296. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30297. NID_private_key_usage_period, &crit, NULL));
  30298. ExpectIntEQ(crit, -1);
  30299. sk_ASN1_OBJECT_free(sk);
  30300. sk = NULL;
  30301. ExpectNotNull(sk = (STACK_OF(GENERAL_NAME)*)X509_get_ext_d2i(x509ext,
  30302. NID_subject_alt_name, &crit, NULL));
  30303. {
  30304. int i;
  30305. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  30306. GENERAL_NAME* gen = sk_GENERAL_NAME_value(sk, i);
  30307. ExpectIntEQ(gen->type, GEN_DNS);
  30308. ExpectIntEQ(gen->d.dNSName->type, V_ASN1_IA5STRING);
  30309. }
  30310. }
  30311. sk_GENERAL_NAME_free(sk);
  30312. sk = NULL;
  30313. /* NID not yet supported */
  30314. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30315. NID_issuer_alt_name, &crit, NULL));
  30316. ExpectIntEQ(crit, -1);
  30317. sk_ASN1_OBJECT_free(sk);
  30318. sk = NULL;
  30319. /* NID not yet supported */
  30320. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30321. NID_info_access, &crit, NULL));
  30322. sk_ASN1_OBJECT_free(sk);
  30323. sk = NULL;
  30324. /* NID not yet supported */
  30325. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30326. NID_sinfo_access, &crit, NULL));
  30327. ExpectIntEQ(crit, -1);
  30328. sk_ASN1_OBJECT_free(sk);
  30329. sk = NULL;
  30330. /* NID not yet supported */
  30331. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30332. NID_name_constraints, &crit, NULL));
  30333. ExpectIntEQ(crit, -1);
  30334. sk_ASN1_OBJECT_free(sk);
  30335. sk = NULL;
  30336. /* no cert policy set */
  30337. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30338. NID_certificate_policies, &crit, NULL));
  30339. sk_ASN1_OBJECT_free(sk);
  30340. sk = NULL;
  30341. /* NID not yet supported */
  30342. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30343. NID_policy_mappings, &crit, NULL));
  30344. ExpectIntEQ(crit, -1);
  30345. sk_ASN1_OBJECT_free(sk);
  30346. sk = NULL;
  30347. /* NID not yet supported */
  30348. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30349. NID_policy_constraints, &crit, NULL));
  30350. ExpectIntEQ(crit, -1);
  30351. sk_ASN1_OBJECT_free(sk);
  30352. sk = NULL;
  30353. /* NID not yet supported */
  30354. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30355. NID_inhibit_any_policy, &crit, NULL));
  30356. ExpectIntEQ(crit, -1);
  30357. sk_ASN1_OBJECT_free(sk);
  30358. sk = NULL;
  30359. /* NID not yet supported */
  30360. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext,
  30361. NID_tlsfeature, &crit, NULL));
  30362. ExpectIntEQ(crit, -1);
  30363. sk_ASN1_OBJECT_free(sk);
  30364. sk = NULL;
  30365. /* test invalid cases */
  30366. crit = 0;
  30367. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509ext, -1, &crit,
  30368. NULL));
  30369. ExpectIntEQ(crit, -1);
  30370. /* NULL passed for criticality. */
  30371. ExpectNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(NULL,
  30372. NID_tlsfeature, NULL, NULL));
  30373. ExpectIntEQ(SSL_get_hit(ssl), 0);
  30374. #ifdef OPENSSL_ALL
  30375. X509_free(x509);
  30376. #endif
  30377. X509_free(x509ext);
  30378. SSL_free(ssl);
  30379. SSL_CTX_free(ctx);
  30380. #endif /* OPENSSL_EXTRA && !NO_CERTS */
  30381. return EXPECT_RESULT();
  30382. }
  30383. static int test_wolfSSL_X509_check_private_key(void)
  30384. {
  30385. EXPECT_DECLS;
  30386. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  30387. defined(USE_CERT_BUFFERS_2048) && !defined(NO_CHECK_PRIVATE_KEY)
  30388. X509* x509 = NULL;
  30389. EVP_PKEY* pkey = NULL;
  30390. const byte* key;
  30391. /* Check with correct key */
  30392. ExpectNotNull((x509 = X509_load_certificate_file(cliCertFile,
  30393. SSL_FILETYPE_PEM)));
  30394. key = client_key_der_2048;
  30395. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  30396. (long)sizeof_client_key_der_2048));
  30397. ExpectIntEQ(X509_check_private_key(x509, pkey), 1);
  30398. EVP_PKEY_free(pkey);
  30399. pkey = NULL;
  30400. /* Check with wrong key */
  30401. key = server_key_der_2048;
  30402. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &key,
  30403. (long)sizeof_server_key_der_2048));
  30404. ExpectIntEQ(X509_check_private_key(x509, pkey), 0);
  30405. /* test for incorrect parameter */
  30406. ExpectIntEQ(X509_check_private_key(NULL, pkey), 0);
  30407. ExpectIntEQ(X509_check_private_key(x509, NULL), 0);
  30408. ExpectIntEQ(X509_check_private_key(NULL, NULL), 0);
  30409. EVP_PKEY_free(pkey);
  30410. X509_free(x509);
  30411. #endif
  30412. return EXPECT_RESULT();
  30413. }
  30414. static int test_wolfSSL_private_keys(void)
  30415. {
  30416. EXPECT_DECLS;
  30417. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  30418. !defined(NO_FILESYSTEM)
  30419. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  30420. WOLFSSL* ssl = NULL;
  30421. WOLFSSL_CTX* ctx = NULL;
  30422. EVP_PKEY* pkey = NULL;
  30423. OpenSSL_add_all_digests();
  30424. OpenSSL_add_all_algorithms();
  30425. #ifndef NO_RSA
  30426. #ifndef NO_WOLFSSL_SERVER
  30427. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30428. #else
  30429. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30430. #endif
  30431. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, WOLFSSL_FILETYPE_PEM));
  30432. /* Have to load a cert before you can check the private key against that
  30433. * certificates public key! */
  30434. #if !defined(NO_CHECK_PRIVATE_KEY)
  30435. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_FAILURE);
  30436. #endif
  30437. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, WOLFSSL_FILETYPE_PEM));
  30438. #if !defined(NO_CHECK_PRIVATE_KEY)
  30439. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30440. #endif
  30441. ExpectNotNull(ssl = SSL_new(ctx));
  30442. #if !defined(NO_CHECK_PRIVATE_KEY)
  30443. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30444. #endif
  30445. #ifdef USE_CERT_BUFFERS_2048
  30446. {
  30447. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  30448. unsigned char buf[FOURK_BUF];
  30449. word32 bufSz;
  30450. ExpectIntEQ(SSL_use_RSAPrivateKey_ASN1(ssl,
  30451. (unsigned char*)client_key_der_2048,
  30452. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  30453. #if !defined(NO_CHECK_PRIVATE_KEY)
  30454. /* Should mismatch now that a different private key loaded */
  30455. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30456. #endif
  30457. ExpectIntEQ(SSL_use_PrivateKey_ASN1(0, ssl,
  30458. (unsigned char*)server_key,
  30459. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  30460. #if !defined(NO_CHECK_PRIVATE_KEY)
  30461. /* After loading back in DER format of original key, should match */
  30462. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30463. #endif
  30464. /* test loading private key to the WOLFSSL_CTX */
  30465. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  30466. (unsigned char*)client_key_der_2048,
  30467. sizeof_client_key_der_2048), WOLFSSL_SUCCESS);
  30468. #if !defined(NO_CHECK_PRIVATE_KEY)
  30469. /* Should mismatch now that a different private key loaded */
  30470. ExpectIntNE(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30471. #endif
  30472. ExpectIntEQ(SSL_CTX_use_PrivateKey_ASN1(0, ctx,
  30473. (unsigned char*)server_key,
  30474. sizeof_server_key_der_2048), WOLFSSL_SUCCESS);
  30475. #if !defined(NO_CHECK_PRIVATE_KEY)
  30476. /* After loading back in DER format of original key, should match */
  30477. ExpectIntEQ(wolfSSL_CTX_check_private_key(ctx), WOLFSSL_SUCCESS);
  30478. #endif
  30479. /* pkey not set yet, expecting to fail */
  30480. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_FAILURE);
  30481. /* set PKEY and test again */
  30482. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  30483. &server_key, (long)sizeof_server_key_der_2048));
  30484. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  30485. /* reuse PKEY structure and test
  30486. * this should be checked with a memory management sanity checker */
  30487. ExpectFalse(server_key == (const unsigned char*)server_key_der_2048);
  30488. server_key = (const unsigned char*)server_key_der_2048;
  30489. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey,
  30490. &server_key, (long)sizeof_server_key_der_2048));
  30491. ExpectIntEQ(SSL_use_PrivateKey(ssl, pkey), WOLFSSL_SUCCESS);
  30492. /* check striping PKCS8 header with wolfSSL_d2i_PrivateKey */
  30493. bufSz = FOURK_BUF;
  30494. ExpectIntGT((bufSz = (word32)wc_CreatePKCS8Key(buf, &bufSz,
  30495. (byte*)server_key_der_2048, sizeof_server_key_der_2048,
  30496. RSAk, NULL, 0)), 0);
  30497. server_key = (const unsigned char*)buf;
  30498. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  30499. (long)bufSz));
  30500. }
  30501. #endif
  30502. EVP_PKEY_free(pkey);
  30503. pkey = NULL;
  30504. SSL_free(ssl); /* frees x509 also since loaded into ssl */
  30505. ssl = NULL;
  30506. SSL_CTX_free(ctx);
  30507. ctx = NULL;
  30508. #endif /* end of RSA private key match tests */
  30509. #ifdef HAVE_ECC
  30510. #ifndef NO_WOLFSSL_SERVER
  30511. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30512. #else
  30513. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30514. #endif
  30515. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  30516. WOLFSSL_FILETYPE_PEM));
  30517. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  30518. WOLFSSL_FILETYPE_PEM));
  30519. ExpectNotNull(ssl = SSL_new(ctx));
  30520. #if !defined(NO_CHECK_PRIVATE_KEY)
  30521. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30522. #endif
  30523. SSL_free(ssl);
  30524. ssl = NULL;
  30525. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEccKeyFile,
  30526. WOLFSSL_FILETYPE_PEM));
  30527. ExpectNotNull(ssl = SSL_new(ctx));
  30528. #ifdef WOLFSSL_VALIDATE_ECC_IMPORT
  30529. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30530. #endif
  30531. SSL_free(ssl);
  30532. ssl = NULL;
  30533. SSL_CTX_free(ctx);
  30534. ctx = NULL;
  30535. #endif /* end of ECC private key match tests */
  30536. #if defined(HAVE_ED25519) && defined(HAVE_ED25519_KEY_IMPORT)
  30537. #ifndef NO_WOLFSSL_SERVER
  30538. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30539. #else
  30540. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30541. #endif
  30542. ExpectTrue(SSL_CTX_use_certificate_file(ctx, edCertFile,
  30543. WOLFSSL_FILETYPE_PEM));
  30544. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, edKeyFile,
  30545. WOLFSSL_FILETYPE_PEM));
  30546. ExpectNotNull(ssl = SSL_new(ctx));
  30547. #if !defined(NO_CHECK_PRIVATE_KEY)
  30548. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30549. #endif
  30550. SSL_free(ssl);
  30551. ssl = NULL;
  30552. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEdKeyFile,
  30553. WOLFSSL_FILETYPE_PEM));
  30554. ExpectNotNull(ssl = SSL_new(ctx));
  30555. #if !defined(NO_CHECK_PRIVATE_KEY)
  30556. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30557. #endif
  30558. SSL_free(ssl);
  30559. ssl = NULL;
  30560. SSL_CTX_free(ctx);
  30561. ctx = NULL;
  30562. #endif /* end of Ed25519 private key match tests */
  30563. #if defined(HAVE_ED448) && defined(HAVE_ED448_KEY_IMPORT)
  30564. #ifndef NO_WOLFSSL_SERVER
  30565. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  30566. #else
  30567. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  30568. #endif
  30569. ExpectTrue(SSL_CTX_use_certificate_file(ctx, ed448CertFile,
  30570. WOLFSSL_FILETYPE_PEM));
  30571. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, ed448KeyFile,
  30572. WOLFSSL_FILETYPE_PEM));
  30573. ExpectNotNull(ssl = SSL_new(ctx));
  30574. #if !defined(NO_CHECK_PRIVATE_KEY)
  30575. ExpectIntEQ(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30576. #endif
  30577. SSL_free(ssl);
  30578. ssl = NULL;
  30579. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, cliEd448KeyFile,
  30580. WOLFSSL_FILETYPE_PEM));
  30581. ExpectNotNull(ssl = SSL_new(ctx));
  30582. #if !defined(NO_CHECK_PRIVATE_KEY)
  30583. ExpectIntNE(wolfSSL_check_private_key(ssl), WOLFSSL_SUCCESS);
  30584. #endif
  30585. SSL_free(ssl);
  30586. ssl = NULL;
  30587. SSL_CTX_free(ctx);
  30588. ctx = NULL;
  30589. #endif /* end of Ed448 private key match tests */
  30590. EVP_cleanup();
  30591. /* test existence of no-op macros in wolfssl/openssl/ssl.h */
  30592. CONF_modules_free();
  30593. ENGINE_cleanup();
  30594. CONF_modules_unload();
  30595. (void)ssl;
  30596. (void)ctx;
  30597. (void)pkey;
  30598. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  30599. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  30600. return EXPECT_RESULT();
  30601. }
  30602. static int test_wolfSSL_PEM_def_callback(void)
  30603. {
  30604. EXPECT_DECLS;
  30605. #ifdef OPENSSL_EXTRA
  30606. char buf[10];
  30607. const char* defpwd = "DEF PWD";
  30608. int defpwdLen = (int)XSTRLEN(defpwd);
  30609. int smallLen = 1;
  30610. /* Bad parameters. */
  30611. ExpectIntEQ(wolfSSL_PEM_def_callback(NULL, sizeof(buf), 0, NULL), 0);
  30612. ExpectIntEQ(wolfSSL_PEM_def_callback(NULL, sizeof(buf), 0, (void*)defpwd),
  30613. 0);
  30614. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, sizeof(buf), 0, NULL), 0);
  30615. XMEMSET(buf, 0, sizeof(buf));
  30616. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, sizeof(buf), 0, (void*)defpwd),
  30617. defpwdLen);
  30618. ExpectIntEQ(XMEMCMP(buf, defpwd, defpwdLen), 0);
  30619. ExpectIntEQ(buf[defpwdLen], 0);
  30620. /* Size of buffer is smaller than default password. */
  30621. XMEMSET(buf, 0, sizeof(buf));
  30622. ExpectIntEQ(wolfSSL_PEM_def_callback(buf, smallLen, 0, (void*)defpwd),
  30623. smallLen);
  30624. ExpectIntEQ(XMEMCMP(buf, defpwd, smallLen), 0);
  30625. ExpectIntEQ(buf[smallLen], 0);
  30626. #endif /* OPENSSL_EXTRA */
  30627. return EXPECT_RESULT();
  30628. }
  30629. static int test_wolfSSL_PEM_read_PrivateKey(void)
  30630. {
  30631. EXPECT_DECLS;
  30632. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || \
  30633. !defined(NO_DSA) || defined(HAVE_ECC) || !defined(NO_DH))
  30634. XFILE file = XBADFILE;
  30635. #if !defined(NO_RSA)
  30636. const char* fname_rsa = "./certs/server-key.pem";
  30637. RSA* rsa = NULL;
  30638. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  30639. unsigned char* sig = NULL;
  30640. size_t sigLen = 0;
  30641. const unsigned char tbs[] = {0, 1, 2, 3, 4, 5, 6, 7};
  30642. size_t tbsLen = sizeof(tbs);
  30643. #endif
  30644. #if !defined(NO_DSA)
  30645. const char* fname_dsa = "./certs/dsa2048.pem";
  30646. #endif
  30647. #if defined(HAVE_ECC)
  30648. const char* fname_ec = "./certs/ecc-key.pem";
  30649. #endif
  30650. #if !defined(NO_DH)
  30651. const char* fname_dh = "./certs/dh-priv-2048.pem";
  30652. #endif
  30653. EVP_PKEY* pkey = NULL;
  30654. /* Check error case. */
  30655. ExpectNull(pkey = PEM_read_PrivateKey(NULL, NULL, NULL, NULL));
  30656. /* not a PEM key. */
  30657. ExpectTrue((file = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  30658. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30659. if (file != XBADFILE)
  30660. XFCLOSE(file);
  30661. file = XBADFILE;
  30662. #ifndef NO_RSA
  30663. /* Read in an RSA key. */
  30664. ExpectTrue((file = XFOPEN(fname_rsa, "rb")) != XBADFILE);
  30665. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30666. if (file != XBADFILE)
  30667. XFCLOSE(file);
  30668. file = XBADFILE;
  30669. /* Make sure the key is usable by signing some data with it. */
  30670. ExpectNotNull(rsa = EVP_PKEY_get0_RSA(pkey));
  30671. ExpectIntGT((sigLen = RSA_size(rsa)), 0);
  30672. ExpectNotNull(sig = (unsigned char*)XMALLOC(sigLen, HEAP_HINT,
  30673. DYNAMIC_TYPE_TMP_BUFFER));
  30674. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  30675. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  30676. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &sigLen, tbs, tbsLen),
  30677. WOLFSSL_SUCCESS);
  30678. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  30679. EVP_PKEY_CTX_free(ctx);
  30680. EVP_PKEY_free(pkey);
  30681. pkey = NULL;
  30682. #endif
  30683. #ifndef NO_DSA
  30684. /* Read in a DSA key. */
  30685. ExpectTrue((file = XFOPEN(fname_dsa, "rb")) != XBADFILE);
  30686. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH)
  30687. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30688. EVP_PKEY_free(pkey);
  30689. pkey = NULL;
  30690. #else
  30691. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30692. #endif
  30693. if (file != XBADFILE)
  30694. XFCLOSE(file);
  30695. file = XBADFILE;
  30696. #endif
  30697. #ifdef HAVE_ECC
  30698. /* Read in an EC key. */
  30699. ExpectTrue((file = XFOPEN(fname_ec, "rb")) != XBADFILE);
  30700. ExpectNotNull(pkey = EVP_PKEY_new());
  30701. ExpectPtrEq(PEM_read_PrivateKey(file, &pkey, NULL, NULL), pkey);
  30702. if (file != XBADFILE)
  30703. XFCLOSE(file);
  30704. file = XBADFILE;
  30705. EVP_PKEY_free(pkey);
  30706. pkey = NULL;
  30707. #endif
  30708. #ifndef NO_DH
  30709. /* Read in a DH key. */
  30710. ExpectTrue((file = XFOPEN(fname_dh, "rb")) != XBADFILE);
  30711. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  30712. defined(WOLFSSL_OPENSSH)) && (!defined(HAVE_FIPS) || \
  30713. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  30714. ExpectNotNull(pkey = PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30715. EVP_PKEY_free(pkey);
  30716. pkey = NULL;
  30717. #else
  30718. ExpectNull(PEM_read_PrivateKey(file, NULL, NULL, NULL));
  30719. #endif
  30720. if (file != XBADFILE)
  30721. XFCLOSE(file);
  30722. file = XBADFILE;
  30723. #endif
  30724. #endif
  30725. return EXPECT_RESULT();
  30726. }
  30727. static int test_wolfSSL_PEM_read_PUBKEY(void)
  30728. {
  30729. EXPECT_DECLS;
  30730. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) \
  30731. && !defined(NO_FILESYSTEM)
  30732. XFILE file = XBADFILE;
  30733. const char* fname = "./certs/client-keyPub.pem";
  30734. EVP_PKEY* pkey = NULL;
  30735. /* Check error case. */
  30736. ExpectNull(pkey = PEM_read_PUBKEY(NULL, NULL, NULL, NULL));
  30737. /* Read in an RSA key. */
  30738. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30739. ExpectNotNull(pkey = PEM_read_PUBKEY(file, NULL, NULL, NULL));
  30740. EVP_PKEY_free(pkey);
  30741. pkey = NULL;
  30742. if (file != XBADFILE)
  30743. XFCLOSE(file);
  30744. file = XBADFILE;
  30745. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30746. ExpectNotNull(pkey = EVP_PKEY_new());
  30747. ExpectPtrEq(PEM_read_PUBKEY(file, &pkey, NULL, NULL), pkey);
  30748. EVP_PKEY_free(pkey);
  30749. if (file != XBADFILE)
  30750. XFCLOSE(file);
  30751. #endif
  30752. return EXPECT_RESULT();
  30753. }
  30754. /* test loading RSA key using BIO */
  30755. static int test_wolfSSL_PEM_PrivateKey_rsa(void)
  30756. {
  30757. EXPECT_DECLS;
  30758. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA) && \
  30759. defined(USE_CERT_BUFFERS_2048) && !defined(NO_FILESYSTEM) && \
  30760. !defined(NO_BIO)
  30761. BIO* bio = NULL;
  30762. XFILE file = XBADFILE;
  30763. const char* fname = "./certs/server-key.pem";
  30764. const char* fname_rsa_p8 = "./certs/server-keyPkcs8.pem";
  30765. EVP_PKEY* pkey = NULL;
  30766. size_t sz = 0;
  30767. byte* buf = NULL;
  30768. EVP_PKEY* pkey2 = NULL;
  30769. EVP_PKEY* pkey3 = NULL;
  30770. RSA* rsa_key = NULL;
  30771. #if defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)
  30772. unsigned char extra[10];
  30773. int i;
  30774. BIO* pub_bio = NULL;
  30775. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  30776. #endif
  30777. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30778. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30779. ExpectIntGT(sz = XFTELL(file), 0);
  30780. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30781. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30782. if (buf != NULL) {
  30783. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30784. }
  30785. if (file != XBADFILE) {
  30786. XFCLOSE(file);
  30787. file = XBADFILE;
  30788. }
  30789. /* Test using BIO new mem and loading PEM private key */
  30790. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30791. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30792. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30793. buf = NULL;
  30794. BIO_free(bio);
  30795. bio = NULL;
  30796. /* New empty EVP_PKEY */
  30797. ExpectNotNull(pkey2 = EVP_PKEY_new());
  30798. if (pkey2 != NULL) {
  30799. pkey2->type = EVP_PKEY_RSA;
  30800. }
  30801. /* Test parameter copy */
  30802. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 0);
  30803. EVP_PKEY_free(pkey2);
  30804. EVP_PKEY_free(pkey);
  30805. pkey = NULL;
  30806. /* Qt unit test case : rsa pkcs8 key */
  30807. ExpectTrue((file = XFOPEN(fname_rsa_p8, "rb")) != XBADFILE);
  30808. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30809. ExpectIntGT(sz = XFTELL(file), 0);
  30810. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30811. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30812. if (buf) {
  30813. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30814. }
  30815. if (file != XBADFILE) {
  30816. XFCLOSE(file);
  30817. file = XBADFILE;
  30818. }
  30819. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30820. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30821. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30822. buf = NULL;
  30823. BIO_free(bio);
  30824. bio = NULL;
  30825. ExpectNotNull(pkey3 = EVP_PKEY_new());
  30826. ExpectNotNull(rsa_key = EVP_PKEY_get1_RSA(pkey));
  30827. ExpectIntEQ(EVP_PKEY_set1_RSA(pkey3, rsa_key), WOLFSSL_SUCCESS);
  30828. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30829. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  30830. #else
  30831. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  30832. #endif
  30833. RSA_free(rsa_key);
  30834. EVP_PKEY_free(pkey3);
  30835. EVP_PKEY_free(pkey);
  30836. pkey = NULL;
  30837. pkey2 = NULL;
  30838. #if defined(WOLFSSL_KEY_GEN) || defined(WOLFSSL_CERT_GEN)
  30839. #define BIO_PEM_TEST_CHAR 'a'
  30840. XMEMSET(extra, BIO_PEM_TEST_CHAR, sizeof(extra));
  30841. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30842. ExpectIntEQ(BIO_set_write_buf_size(bio, 4096), SSL_FAILURE);
  30843. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30844. ExpectIntEQ(BIO_set_write_buf_size(pub_bio, 4096), SSL_FAILURE);
  30845. ExpectNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key,
  30846. (long)sizeof_server_key_der_2048));
  30847. ExpectNull(pkey);
  30848. ExpectNotNull(wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key,
  30849. (long)sizeof_server_key_der_2048));
  30850. ExpectIntEQ(PEM_write_bio_PrivateKey(NULL, pkey, NULL, NULL, 0, NULL, NULL),
  30851. WOLFSSL_FAILURE);
  30852. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, NULL, NULL, NULL, 0, NULL, NULL),
  30853. WOLFSSL_FAILURE);
  30854. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30855. WOLFSSL_SUCCESS);
  30856. ExpectIntGT(BIO_pending(bio), 0);
  30857. ExpectIntEQ(BIO_pending(bio), 1679);
  30858. /* Check if the pubkey API writes only the public key */
  30859. #ifdef WOLFSSL_KEY_GEN
  30860. ExpectIntEQ(PEM_write_bio_PUBKEY(NULL, pkey), WOLFSSL_FAILURE);
  30861. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, NULL), WOLFSSL_FAILURE);
  30862. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  30863. ExpectIntGT(BIO_pending(pub_bio), 0);
  30864. /* Previously both the private key and the pubkey calls would write
  30865. * out the private key and the PEM header was the only difference.
  30866. * The public PEM should be significantly shorter than the
  30867. * private key versison. */
  30868. ExpectIntEQ(BIO_pending(pub_bio), 451);
  30869. #else
  30870. /* Not supported. */
  30871. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), 0);
  30872. #endif
  30873. /* test creating new EVP_PKEY with good args */
  30874. ExpectNotNull((pkey2 = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30875. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  30876. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr,
  30877. pkey->pkey_sz), 0);
  30878. }
  30879. /* test of reuse of EVP_PKEY */
  30880. ExpectNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  30881. ExpectIntEQ(BIO_pending(bio), 0);
  30882. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30883. SSL_SUCCESS);
  30884. /* add 10 extra bytes after PEM */
  30885. ExpectIntEQ(BIO_write(bio, extra, 10), 10);
  30886. ExpectNotNull(PEM_read_bio_PrivateKey(bio, &pkey, NULL, NULL));
  30887. ExpectNotNull(pkey);
  30888. if (pkey && pkey->pkey.ptr && pkey2 && pkey2->pkey.ptr) {
  30889. ExpectIntEQ((int)XMEMCMP(pkey->pkey.ptr, pkey2->pkey.ptr,
  30890. pkey->pkey_sz), 0);
  30891. }
  30892. /* check 10 extra bytes still there */
  30893. ExpectIntEQ(BIO_pending(bio), 10);
  30894. ExpectIntEQ(BIO_read(bio, extra, 10), 10);
  30895. for (i = 0; i < 10; i++) {
  30896. ExpectIntEQ(extra[i], BIO_PEM_TEST_CHAR);
  30897. }
  30898. BIO_free(pub_bio);
  30899. BIO_free(bio);
  30900. bio = NULL;
  30901. EVP_PKEY_free(pkey);
  30902. pkey = NULL;
  30903. EVP_PKEY_free(pkey2);
  30904. #endif /* WOLFSSL_KEY_GEN || WOLFSSL_CERT_GEN */
  30905. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 &&
  30906. * !NO_FILESYSTEM && !NO_BIO */
  30907. return EXPECT_RESULT();
  30908. }
  30909. /* test loading ECC key using BIO */
  30910. static int test_wolfSSL_PEM_PrivateKey_ecc(void)
  30911. {
  30912. EXPECT_DECLS;
  30913. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_ECC) && \
  30914. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  30915. BIO* bio = NULL;
  30916. EVP_PKEY* pkey = NULL;
  30917. XFILE file = XBADFILE;
  30918. const char* fname = "./certs/ecc-key.pem";
  30919. const char* fname_ecc_p8 = "./certs/ecc-keyPkcs8.pem";
  30920. size_t sz = 0;
  30921. byte* buf = NULL;
  30922. EVP_PKEY* pkey2 = NULL;
  30923. EVP_PKEY* pkey3 = NULL;
  30924. EC_KEY* ec_key = NULL;
  30925. int nid = 0;
  30926. BIO* pub_bio = NULL;
  30927. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  30928. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30929. ExpectIntGT(sz = XFTELL(file), 0);
  30930. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30931. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30932. if (buf) {
  30933. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  30934. }
  30935. if (file != XBADFILE) {
  30936. XFCLOSE(file);
  30937. file = XBADFILE;
  30938. }
  30939. /* Test using BIO new mem and loading PEM private key */
  30940. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  30941. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  30942. BIO_free(bio);
  30943. bio = NULL;
  30944. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  30945. buf = NULL;
  30946. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30947. ExpectNotNull(pub_bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  30948. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  30949. WOLFSSL_SUCCESS);
  30950. ExpectIntGT(BIO_pending(bio), 0);
  30951. /* No parmeters. */
  30952. ExpectIntEQ(BIO_pending(bio), 227);
  30953. /* Check if the pubkey API writes only the public key */
  30954. #ifdef WOLFSSL_KEY_GEN
  30955. ExpectIntEQ(PEM_write_bio_PUBKEY(pub_bio, pkey), WOLFSSL_SUCCESS);
  30956. ExpectIntGT(BIO_pending(pub_bio), 0);
  30957. /* Previously both the private key and the pubkey calls would write
  30958. * out the private key and the PEM header was the only difference.
  30959. * The public PEM should be significantly shorter than the
  30960. * private key versison. */
  30961. ExpectIntEQ(BIO_pending(pub_bio), 178);
  30962. #endif
  30963. BIO_free(pub_bio);
  30964. BIO_free(bio);
  30965. bio = NULL;
  30966. ExpectNotNull(pkey2 = EVP_PKEY_new());
  30967. ExpectNotNull(pkey3 = EVP_PKEY_new());
  30968. if (pkey2 != NULL) {
  30969. pkey2->type = EVP_PKEY_EC;
  30970. }
  30971. /* Test parameter copy */
  30972. ExpectIntEQ(EVP_PKEY_copy_parameters(pkey2, pkey), 1);
  30973. /* Qt unit test case 1*/
  30974. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  30975. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  30976. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  30977. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  30978. #else
  30979. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  30980. #endif
  30981. /* Test default digest */
  30982. ExpectIntEQ(EVP_PKEY_get_default_digest_nid(pkey, &nid), 1);
  30983. ExpectIntEQ(nid, NID_sha256);
  30984. EC_KEY_free(ec_key);
  30985. ec_key = NULL;
  30986. EVP_PKEY_free(pkey3);
  30987. pkey3 = NULL;
  30988. EVP_PKEY_free(pkey2);
  30989. pkey2 = NULL;
  30990. EVP_PKEY_free(pkey);
  30991. pkey = NULL;
  30992. /* Qt unit test case ec pkcs8 key */
  30993. ExpectTrue((file = XFOPEN(fname_ecc_p8, "rb")) != XBADFILE);
  30994. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  30995. ExpectIntGT(sz = XFTELL(file), 0);
  30996. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  30997. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  30998. if (buf) {
  30999. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  31000. }
  31001. if (file != XBADFILE) {
  31002. XFCLOSE(file);
  31003. file = XBADFILE;
  31004. }
  31005. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31006. ExpectNotNull((pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL)));
  31007. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  31008. buf = NULL;
  31009. BIO_free(bio);
  31010. bio = NULL;
  31011. ExpectNotNull(pkey3 = EVP_PKEY_new());
  31012. /* Qt unit test case */
  31013. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  31014. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey3, ec_key), WOLFSSL_SUCCESS);
  31015. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31016. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 1/* match */);
  31017. #else
  31018. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey3), 0);
  31019. #endif
  31020. EC_KEY_free(ec_key);
  31021. EVP_PKEY_free(pkey3);
  31022. EVP_PKEY_free(pkey);
  31023. pkey = NULL;
  31024. #endif
  31025. return EXPECT_RESULT();
  31026. }
  31027. /* test loading DSA key using BIO */
  31028. static int test_wolfSSL_PEM_PrivateKey_dsa(void)
  31029. {
  31030. EXPECT_DECLS;
  31031. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_DSA) && \
  31032. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  31033. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL)
  31034. BIO* bio = NULL;
  31035. EVP_PKEY* pkey = NULL;
  31036. ExpectNotNull(bio = BIO_new_file("./certs/dsa2048.pem", "rb"));
  31037. /* Private DSA EVP_PKEY */
  31038. ExpectNotNull(pkey = wolfSSL_PEM_read_bio_PrivateKey(bio, NULL, NULL,
  31039. NULL));
  31040. BIO_free(bio);
  31041. bio = NULL;
  31042. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31043. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  31044. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  31045. NULL), 0);
  31046. #endif
  31047. #ifdef WOLFSSL_KEY_GEN
  31048. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), 1);
  31049. ExpectIntEQ(BIO_pending(bio), 1178);
  31050. BIO_reset(bio);
  31051. #endif
  31052. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  31053. 1);
  31054. ExpectIntEQ(BIO_pending(bio), 1196);
  31055. BIO_free(bio);
  31056. bio = NULL;
  31057. EVP_PKEY_free(pkey);
  31058. pkey = NULL;
  31059. #endif
  31060. #endif
  31061. return EXPECT_RESULT();
  31062. }
  31063. /* test loading DH key using BIO */
  31064. static int test_wolfSSL_PEM_PrivateKey_dh(void)
  31065. {
  31066. EXPECT_DECLS;
  31067. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_DH) && \
  31068. !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  31069. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  31070. defined(WOLFSSL_OPENSSH)) && (!defined(HAVE_FIPS) || \
  31071. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  31072. BIO* bio = NULL;
  31073. EVP_PKEY* pkey = NULL;
  31074. ExpectNotNull(bio = BIO_new_file("./certs/dh-priv-2048.pem", "rb"));
  31075. /* Private DH EVP_PKEY */
  31076. ExpectNotNull(pkey = wolfSSL_PEM_read_bio_PrivateKey(bio, NULL, NULL,
  31077. NULL));
  31078. BIO_free(bio);
  31079. bio = NULL;
  31080. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31081. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  31082. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  31083. NULL), 0);
  31084. #endif
  31085. #ifdef WOLFSSL_KEY_GEN
  31086. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), 0);
  31087. #endif
  31088. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  31089. 1);
  31090. ExpectIntEQ(BIO_pending(bio), 806);
  31091. BIO_free(bio);
  31092. bio = NULL;
  31093. EVP_PKEY_free(pkey);
  31094. pkey = NULL;
  31095. #endif
  31096. #endif
  31097. return EXPECT_RESULT();
  31098. }
  31099. static int test_wolfSSL_PEM_PrivateKey(void)
  31100. {
  31101. EXPECT_DECLS;
  31102. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31103. (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(USE_CERT_BUFFERS_2048)
  31104. #ifndef NO_BIO
  31105. BIO* bio = NULL;
  31106. #endif
  31107. EVP_PKEY* pkey = NULL;
  31108. const unsigned char* server_key = (const unsigned char*)server_key_der_2048;
  31109. #ifndef NO_BIO
  31110. /* test creating new EVP_PKEY with bad arg */
  31111. ExpectNull((pkey = PEM_read_bio_PrivateKey(NULL, NULL, NULL, NULL)));
  31112. /* Test bad EVP_PKEY type. */
  31113. /* New HMAC EVP_PKEY */
  31114. ExpectNotNull(bio = BIO_new_mem_buf("", 1));
  31115. ExpectNotNull(pkey = EVP_PKEY_new());
  31116. if (pkey != NULL) {
  31117. pkey->type = EVP_PKEY_HMAC;
  31118. }
  31119. ExpectIntEQ(PEM_write_bio_PrivateKey(bio, pkey, NULL, NULL, 0, NULL, NULL),
  31120. 0);
  31121. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  31122. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  31123. NULL), 0);
  31124. #endif
  31125. #ifdef WOLFSSL_KEY_GEN
  31126. ExpectIntEQ(PEM_write_bio_PUBKEY(bio, pkey), WOLFSSL_FAILURE);
  31127. #endif
  31128. EVP_PKEY_free(pkey);
  31129. pkey = NULL;
  31130. BIO_free(bio);
  31131. bio = NULL;
  31132. /* key is DES encrypted */
  31133. #if !defined(NO_DES3) && defined(WOLFSSL_ENCRYPTED_KEYS) && \
  31134. !defined(NO_RSA) && !defined(NO_BIO) && !defined(NO_FILESYSTEM) && \
  31135. !defined(NO_MD5) && defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  31136. {
  31137. XFILE f = XBADFILE;
  31138. wc_pem_password_cb* passwd_cb = NULL;
  31139. void* passwd_cb_userdata;
  31140. SSL_CTX* ctx = NULL;
  31141. char passwd[] = "bad password";
  31142. #ifndef WOLFSSL_NO_TLS12
  31143. #ifndef NO_WOLFSSL_SERVER
  31144. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  31145. #else
  31146. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  31147. #endif
  31148. #else
  31149. #ifndef NO_WOLFSSL_SERVER
  31150. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  31151. #else
  31152. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  31153. #endif
  31154. #endif
  31155. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  31156. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  31157. ExpectNotNull(passwd_cb = SSL_CTX_get_default_passwd_cb(ctx));
  31158. ExpectNull(passwd_cb_userdata =
  31159. SSL_CTX_get_default_passwd_cb_userdata(ctx));
  31160. /* fail case with password call back */
  31161. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, NULL,
  31162. (void*)passwd));
  31163. BIO_free(bio);
  31164. ExpectNotNull(bio = BIO_new_file("./certs/server-keyEnc.pem", "rb"));
  31165. ExpectNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  31166. (void*)passwd));
  31167. BIO_free(bio);
  31168. ExpectTrue((f = XFOPEN("./certs/server-keyEnc.pem", "rb")) != XBADFILE);
  31169. ExpectNotNull(bio = BIO_new_fp(f, BIO_CLOSE));
  31170. if ((bio == NULL) && (f != XBADFILE)) {
  31171. XFCLOSE(f);
  31172. }
  31173. /* use callback that works */
  31174. ExpectNotNull(pkey = PEM_read_bio_PrivateKey(bio, NULL, passwd_cb,
  31175. (void*)"yassl123"));
  31176. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  31177. EVP_PKEY_free(pkey);
  31178. pkey = NULL;
  31179. BIO_free(bio);
  31180. bio = NULL;
  31181. SSL_CTX_free(ctx);
  31182. }
  31183. #endif /* !defined(NO_DES3) */
  31184. #endif /* !NO_BIO */
  31185. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  31186. {
  31187. unsigned char buf[2048];
  31188. size_t bytes = 0;
  31189. XFILE f = XBADFILE;
  31190. SSL_CTX* ctx = NULL;
  31191. #ifndef WOLFSSL_NO_TLS12
  31192. #ifndef NO_WOLFSSL_SERVER
  31193. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_server_method()));
  31194. #else
  31195. ExpectNotNull(ctx = SSL_CTX_new(TLSv1_2_client_method()));
  31196. #endif
  31197. #else
  31198. #ifndef NO_WOLFSSL_SERVER
  31199. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_server_method()));
  31200. #else
  31201. ExpectNotNull(ctx = SSL_CTX_new(wolfTLSv1_3_client_method()));
  31202. #endif
  31203. #endif
  31204. ExpectTrue((f = XFOPEN("./certs/ecc-key.der", "rb")) != XBADFILE);
  31205. ExpectIntGT(bytes = (size_t)XFREAD(buf, 1, sizeof(buf), f), 0);
  31206. if (f != XBADFILE)
  31207. XFCLOSE(f);
  31208. server_key = buf;
  31209. pkey = NULL;
  31210. ExpectNull(d2i_PrivateKey(EVP_PKEY_RSA, &pkey, &server_key, (long int)bytes));
  31211. ExpectNull(pkey);
  31212. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_EC, &pkey, &server_key, (long int)bytes));
  31213. ExpectIntEQ(SSL_CTX_use_PrivateKey(ctx, pkey), SSL_SUCCESS);
  31214. EVP_PKEY_free(pkey);
  31215. pkey = NULL;
  31216. SSL_CTX_free(ctx);
  31217. server_key = NULL;
  31218. }
  31219. #endif
  31220. #ifndef NO_BIO
  31221. (void)bio;
  31222. #endif
  31223. (void)pkey;
  31224. (void)server_key;
  31225. #endif /* OPENSSL_EXTRA && !NO_CERTS && !NO_RSA && USE_CERT_BUFFERS_2048 */
  31226. return EXPECT_RESULT();
  31227. }
  31228. static int test_wolfSSL_PEM_file_RSAKey(void)
  31229. {
  31230. EXPECT_DECLS;
  31231. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31232. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  31233. !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  31234. RSA* rsa = NULL;
  31235. XFILE fp = XBADFILE;
  31236. ExpectTrue((fp = XFOPEN("./certs/rsa-pub-2048.pem", "rb")) != XBADFILE);
  31237. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL)));
  31238. if (fp != XBADFILE)
  31239. XFCLOSE(fp);
  31240. ExpectIntEQ(RSA_size(rsa), 256);
  31241. ExpectIntEQ(PEM_write_RSAPublicKey(XBADFILE, rsa), WOLFSSL_FAILURE);
  31242. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, NULL), WOLFSSL_FAILURE);
  31243. ExpectIntEQ(PEM_write_RSAPublicKey(stderr, rsa), WOLFSSL_SUCCESS);
  31244. ExpectIntEQ(PEM_write_RSA_PUBKEY(XBADFILE, rsa), WOLFSSL_FAILURE);
  31245. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  31246. ExpectIntEQ(PEM_write_RSA_PUBKEY(stderr, rsa), WOLFSSL_SUCCESS);
  31247. RSA_free(rsa);
  31248. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31249. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  31250. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  31251. return EXPECT_RESULT();
  31252. }
  31253. static int test_wolfSSL_PEM_file_RSAPrivateKey(void)
  31254. {
  31255. EXPECT_DECLS;
  31256. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  31257. !defined(NO_FILESYSTEM) && \
  31258. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  31259. RSA* rsa = NULL;
  31260. XFILE f = NULL;
  31261. ExpectTrue((f = XFOPEN(svrKeyFile, "r")) != XBADFILE);
  31262. ExpectNotNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  31263. ExpectIntEQ(RSA_size(rsa), 256);
  31264. if (f != XBADFILE) {
  31265. XFCLOSE(f);
  31266. f = XBADFILE;
  31267. }
  31268. ExpectIntEQ(PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0, NULL,
  31269. NULL), WOLFSSL_FAILURE);
  31270. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  31271. NULL), WOLFSSL_FAILURE);
  31272. ExpectIntEQ(PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0, NULL, NULL),
  31273. WOLFSSL_SUCCESS);
  31274. RSA_free(rsa);
  31275. #ifdef HAVE_ECC
  31276. ExpectTrue((f = XFOPEN(eccKeyFile, "r")) != XBADFILE);
  31277. ExpectNull((rsa = PEM_read_RSAPrivateKey(f, NULL, NULL, NULL)));
  31278. if (f != XBADFILE)
  31279. XFCLOSE(f);
  31280. #endif /* HAVE_ECC */
  31281. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31282. return EXPECT_RESULT();
  31283. }
  31284. static int test_wolfSSL_PEM_read_RSA_PUBKEY(void)
  31285. {
  31286. EXPECT_DECLS;
  31287. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31288. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31289. XFILE file = XBADFILE;
  31290. const char* fname = "./certs/client-keyPub.pem";
  31291. RSA *rsa = NULL;
  31292. ExpectNull(wolfSSL_PEM_read_RSA_PUBKEY(XBADFILE, NULL, NULL, NULL));
  31293. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  31294. ExpectNotNull((rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL)));
  31295. ExpectIntEQ(RSA_size(rsa), 256);
  31296. RSA_free(rsa);
  31297. if (file != XBADFILE)
  31298. XFCLOSE(file);
  31299. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31300. return EXPECT_RESULT();
  31301. }
  31302. #ifndef NO_BIO
  31303. static int test_wolfSSL_PEM_bio_RSAKey(void)
  31304. {
  31305. EXPECT_DECLS;
  31306. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31307. defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && \
  31308. !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  31309. RSA* rsa = NULL;
  31310. BIO* bio = NULL;
  31311. /* PrivateKey */
  31312. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  31313. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(NULL, NULL, NULL, NULL)));
  31314. ExpectNotNull(PEM_read_bio_RSAPrivateKey(bio, &rsa, NULL, NULL));
  31315. ExpectNotNull(rsa);
  31316. ExpectIntEQ(RSA_size(rsa), 256);
  31317. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL, \
  31318. NULL), WOLFSSL_FAILURE);
  31319. BIO_free(bio);
  31320. bio = NULL;
  31321. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31322. ExpectIntEQ(PEM_write_bio_RSAPrivateKey(bio, rsa, NULL, NULL, 0, NULL, \
  31323. NULL), WOLFSSL_SUCCESS);
  31324. BIO_free(bio);
  31325. bio = NULL;
  31326. RSA_free(rsa);
  31327. rsa = NULL;
  31328. /* PUBKEY */
  31329. ExpectNotNull(bio = BIO_new_file("./certs/rsa-pub-2048.pem", "rb"));
  31330. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(NULL, NULL, NULL, NULL)));
  31331. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31332. ExpectIntEQ(RSA_size(rsa), 256);
  31333. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31334. BIO_free(bio);
  31335. bio = NULL;
  31336. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31337. ExpectIntEQ(PEM_write_bio_RSA_PUBKEY(bio, rsa), WOLFSSL_SUCCESS);
  31338. BIO_free(bio);
  31339. bio = NULL;
  31340. RSA_free(rsa);
  31341. rsa = NULL;
  31342. /* Ensure that keys beginning with BEGIN RSA PUBLIC KEY can be read, too. */
  31343. ExpectNotNull(bio = BIO_new_file("./certs/server-keyPub.pem", "rb"));
  31344. ExpectNotNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31345. BIO_free(bio);
  31346. bio = NULL;
  31347. RSA_free(rsa);
  31348. rsa = NULL;
  31349. #ifdef HAVE_ECC
  31350. /* ensure that non-rsa keys do not work */
  31351. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  31352. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31353. ExpectNull((rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL)));
  31354. BIO_free(bio);
  31355. bio = NULL;
  31356. RSA_free(rsa);
  31357. rsa = NULL;
  31358. #endif /* HAVE_ECC */
  31359. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31360. (defined(WOLFSSL_KEY_GEN) || WOLFSSL_CERT_GEN) && \
  31361. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_CERTS) */
  31362. return EXPECT_RESULT();
  31363. }
  31364. static int test_wolfSSL_PEM_bio_RSAPrivateKey(void)
  31365. {
  31366. EXPECT_DECLS;
  31367. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  31368. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  31369. RSA* rsa = NULL;
  31370. RSA* rsa_dup = NULL;
  31371. BIO* bio = NULL;
  31372. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb"));
  31373. ExpectNotNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31374. ExpectIntEQ(RSA_size(rsa), 256);
  31375. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  31376. ExpectNull(rsa_dup = RSAPublicKey_dup(NULL));
  31377. /* Test duplicating empty key. */
  31378. ExpectNotNull(rsa_dup = RSA_new());
  31379. ExpectNull(RSAPublicKey_dup(rsa_dup));
  31380. RSA_free(rsa_dup);
  31381. rsa_dup = NULL;
  31382. ExpectNotNull(rsa_dup = RSAPublicKey_dup(rsa));
  31383. ExpectPtrNE(rsa_dup, rsa);
  31384. #endif
  31385. /* test if valgrind complains about unreleased memory */
  31386. RSA_up_ref(rsa);
  31387. RSA_free(rsa);
  31388. BIO_free(bio);
  31389. bio = NULL;
  31390. RSA_free(rsa);
  31391. rsa = NULL;
  31392. RSA_free(rsa_dup);
  31393. rsa_dup = NULL;
  31394. #ifdef HAVE_ECC
  31395. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb"));
  31396. ExpectNull((rsa = PEM_read_bio_RSAPrivateKey(bio, NULL, NULL, NULL)));
  31397. BIO_free(bio);
  31398. #endif /* HAVE_ECC */
  31399. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31400. return EXPECT_RESULT();
  31401. }
  31402. static int test_wolfSSL_PEM_bio_DSAKey(void)
  31403. {
  31404. EXPECT_DECLS;
  31405. #ifndef HAVE_SELFTEST
  31406. #if (defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && !defined(NO_CERTS) && \
  31407. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && !defined(NO_DSA)
  31408. DSA* dsa = NULL;
  31409. BIO* bio = NULL;
  31410. /* PrivateKey */
  31411. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa1024.pem", "rb"));
  31412. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(NULL, NULL, NULL, NULL)));
  31413. ExpectNotNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  31414. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  31415. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  31416. NULL), WOLFSSL_FAILURE);
  31417. BIO_free(bio);
  31418. bio = NULL;
  31419. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31420. ExpectIntEQ(PEM_write_bio_DSAPrivateKey(bio, dsa, NULL, NULL, 0, NULL,
  31421. NULL), WOLFSSL_SUCCESS);
  31422. BIO_free(bio);
  31423. bio = NULL;
  31424. DSA_free(dsa);
  31425. dsa = NULL;
  31426. /* PUBKEY */
  31427. ExpectNotNull(bio = BIO_new_file("./certs/1024/dsa-pub-1024.pem", "rb"));
  31428. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(NULL, NULL, NULL, NULL)));
  31429. ExpectNotNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  31430. ExpectIntEQ(BN_num_bytes(dsa->g), 128);
  31431. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31432. BIO_free(bio);
  31433. bio = NULL;
  31434. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31435. ExpectIntEQ(PEM_write_bio_DSA_PUBKEY(bio, dsa), WOLFSSL_SUCCESS);
  31436. BIO_free(bio);
  31437. bio = NULL;
  31438. DSA_free(dsa);
  31439. dsa = NULL;
  31440. #ifdef HAVE_ECC
  31441. /* ensure that non-dsa keys do not work */
  31442. ExpectNotNull(bio = BIO_new_file(eccKeyFile, "rb")); /* ecc key */
  31443. ExpectNull((dsa = PEM_read_bio_DSAPrivateKey(bio, NULL, NULL, NULL)));
  31444. ExpectNull((dsa = PEM_read_bio_DSA_PUBKEY(bio, NULL, NULL, NULL)));
  31445. BIO_free(bio);
  31446. bio = NULL;
  31447. DSA_free(dsa);
  31448. dsa = NULL;
  31449. #endif /* HAVE_ECC */
  31450. #endif /* defined(WOLFSSL_QT) || defined(OPENSSL_ALL)) && \
  31451. !defined(NO_CERTS) && defined(WOLFSSL_KEY_GEN) && \
  31452. !defined(NO_FILESYSTEM) && !defined(NO_DSA) */
  31453. #endif /* HAVE_SELFTEST */
  31454. return EXPECT_RESULT();
  31455. }
  31456. static int test_wolfSSL_PEM_bio_ECKey(void)
  31457. {
  31458. EXPECT_DECLS;
  31459. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) && \
  31460. defined(WOLFSSL_KEY_GEN) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC)
  31461. EC_KEY* ec = NULL;
  31462. EC_KEY* ec2;
  31463. BIO* bio = NULL;
  31464. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  31465. unsigned char* pem = NULL;
  31466. int pLen;
  31467. #endif
  31468. static char ec_key_bad_1[] = "-----BEGIN PUBLIC KEY-----\n"
  31469. "MAA=\n"
  31470. "-----END PUBLIC KEY-----";
  31471. static char ec_priv_key_bad_1[] = "-----BEGIN EC PRIVATE KEY-----\n"
  31472. "MAA=\n"
  31473. "-----END EC PRIVATE KEY-----";
  31474. /* PrivateKey */
  31475. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  31476. ExpectNull((ec = PEM_read_bio_ECPrivateKey(NULL, NULL, NULL, NULL)));
  31477. ec2 = NULL;
  31478. ExpectNotNull((ec = PEM_read_bio_ECPrivateKey(bio, &ec2, NULL, NULL)));
  31479. ExpectIntEQ(ec == ec2, 1);
  31480. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  31481. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, NULL, NULL, NULL, 0, NULL,
  31482. NULL), WOLFSSL_FAILURE);
  31483. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  31484. NULL), WOLFSSL_FAILURE);
  31485. ExpectIntEQ(PEM_write_bio_ECPrivateKey(NULL, ec, NULL, NULL, 0, NULL, NULL),
  31486. WOLFSSL_FAILURE);
  31487. BIO_free(bio);
  31488. bio = NULL;
  31489. /* Public key data - fail. */
  31490. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  31491. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  31492. BIO_free(bio);
  31493. bio = NULL;
  31494. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31495. ExpectIntEQ(PEM_write_bio_ECPrivateKey(bio, ec, NULL, NULL, 0, NULL, \
  31496. NULL), WOLFSSL_SUCCESS);
  31497. BIO_free(bio);
  31498. bio = NULL;
  31499. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, NULL, NULL, NULL, 0, NULL,
  31500. NULL),WOLFSSL_FAILURE);
  31501. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, NULL, NULL, NULL, 0, NULL, NULL),
  31502. WOLFSSL_FAILURE);
  31503. ExpectIntEQ(PEM_write_ECPrivateKey(XBADFILE, ec, NULL, NULL, 0, NULL, NULL),
  31504. WOLFSSL_FAILURE);
  31505. ExpectIntEQ(PEM_write_ECPrivateKey(stderr, ec, NULL, NULL, 0, NULL, NULL),
  31506. WOLFSSL_SUCCESS);
  31507. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  31508. NULL), 0);
  31509. #if defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM)
  31510. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  31511. NULL), 0);
  31512. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  31513. NULL), 0);
  31514. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, NULL,
  31515. &pLen), 0);
  31516. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(NULL, NULL, NULL, 0, &pem,
  31517. &pLen), 0);
  31518. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, NULL,
  31519. &pLen), 0);
  31520. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  31521. NULL), 0);
  31522. ExpectIntEQ(wolfSSL_PEM_write_mem_ECPrivateKey(ec, NULL, NULL, 0, &pem,
  31523. &pLen), 1);
  31524. ExpectIntGT(pLen, 0);
  31525. XFREE(pem, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  31526. #endif
  31527. EC_KEY_free(ec);
  31528. ec = NULL;
  31529. /* PUBKEY */
  31530. ExpectNotNull(bio = BIO_new_file("./certs/ecc-client-keyPub.pem", "rb"));
  31531. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(NULL, NULL, NULL, NULL)));
  31532. ec2 = NULL;
  31533. ExpectNotNull((ec = PEM_read_bio_EC_PUBKEY(bio, &ec2, NULL, NULL)));
  31534. ExpectIntEQ(ec == ec2, 1);
  31535. ExpectIntEQ(wc_ecc_size((ecc_key*)ec->internal), 32);
  31536. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31537. BIO_free(bio);
  31538. bio = NULL;
  31539. /* Test 0x30, 0x00 fails. */
  31540. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_key_bad_1,
  31541. sizeof(ec_key_bad_1)));
  31542. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  31543. BIO_free(bio);
  31544. bio = NULL;
  31545. /* Private key data - fail. */
  31546. ExpectNotNull(bio = BIO_new_file("./certs/ecc-key.pem", "rb"));
  31547. ExpectNull(PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  31548. BIO_free(bio);
  31549. bio = NULL;
  31550. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  31551. ExpectIntEQ(PEM_write_bio_EC_PUBKEY(bio, ec), WOLFSSL_SUCCESS);
  31552. BIO_free(bio);
  31553. bio = NULL;
  31554. /* Same test as above, but with a file pointer rather than a BIO. */
  31555. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, NULL), WOLFSSL_FAILURE);
  31556. ExpectIntEQ(PEM_write_EC_PUBKEY(NULL, ec), WOLFSSL_FAILURE);
  31557. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, NULL), WOLFSSL_FAILURE);
  31558. ExpectIntEQ(PEM_write_EC_PUBKEY(stderr, ec), WOLFSSL_SUCCESS);
  31559. EC_KEY_free(ec);
  31560. ec = NULL;
  31561. #ifndef NO_RSA
  31562. /* ensure that non-ec keys do not work */
  31563. ExpectNotNull(bio = BIO_new_file(svrKeyFile, "rb")); /* rsa key */
  31564. ExpectNull((ec = PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL)));
  31565. ExpectNull((ec = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL)));
  31566. BIO_free(bio);
  31567. bio = NULL;
  31568. EC_KEY_free(ec);
  31569. ec = NULL;
  31570. #endif /* !NO_RSA */
  31571. /* Test 0x30, 0x00 fails. */
  31572. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_priv_key_bad_1,
  31573. sizeof(ec_priv_key_bad_1)));
  31574. ExpectNull(PEM_read_bio_ECPrivateKey(bio, NULL, NULL, NULL));
  31575. BIO_free(bio);
  31576. bio = NULL;
  31577. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) */
  31578. return EXPECT_RESULT();
  31579. }
  31580. static int test_wolfSSL_PEM_PUBKEY(void)
  31581. {
  31582. EXPECT_DECLS;
  31583. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  31584. BIO* bio = NULL;
  31585. EVP_PKEY* pkey = NULL;
  31586. /* test creating new EVP_PKEY with bad arg */
  31587. ExpectNull((pkey = PEM_read_bio_PUBKEY(NULL, NULL, NULL, NULL)));
  31588. /* test loading ECC key using BIO */
  31589. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  31590. {
  31591. XFILE file = XBADFILE;
  31592. const char* fname = "./certs/ecc-client-keyPub.pem";
  31593. size_t sz = 0;
  31594. byte* buf = NULL;
  31595. EVP_PKEY* pkey2 = NULL;
  31596. EC_KEY* ec_key = NULL;
  31597. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  31598. ExpectIntEQ(XFSEEK(file, 0, XSEEK_END), 0);
  31599. ExpectIntGT(sz = XFTELL(file), 0);
  31600. ExpectIntEQ(XFSEEK(file, 0, XSEEK_SET), 0);
  31601. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  31602. if (buf != NULL) {
  31603. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  31604. }
  31605. if (file != XBADFILE) {
  31606. XFCLOSE(file);
  31607. }
  31608. /* Test using BIO new mem and loading PEM private key */
  31609. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31610. ExpectNotNull((pkey = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL)));
  31611. BIO_free(bio);
  31612. bio = NULL;
  31613. EVP_PKEY_free(pkey);
  31614. pkey = NULL;
  31615. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  31616. ExpectNotNull(pkey = EVP_PKEY_new());
  31617. ExpectPtrEq(PEM_read_bio_PUBKEY(bio, &pkey, NULL, NULL), pkey);
  31618. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  31619. BIO_free(bio);
  31620. bio = NULL;
  31621. /* Qt unit test case*/
  31622. ExpectNotNull(pkey2 = EVP_PKEY_new());
  31623. ExpectNotNull(ec_key = EVP_PKEY_get1_EC_KEY(pkey));
  31624. ExpectIntEQ(EVP_PKEY_set1_EC_KEY(pkey2, ec_key), WOLFSSL_SUCCESS);
  31625. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  31626. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 1/* match */);
  31627. #else
  31628. ExpectIntEQ(EVP_PKEY_cmp(pkey, pkey2), 0);
  31629. #endif
  31630. EC_KEY_free(ec_key);
  31631. EVP_PKEY_free(pkey2);
  31632. EVP_PKEY_free(pkey);
  31633. pkey = NULL;
  31634. }
  31635. #endif
  31636. (void)bio;
  31637. (void)pkey;
  31638. #endif
  31639. return EXPECT_RESULT();
  31640. }
  31641. #endif /* !NO_BIO */
  31642. static int test_DSA_do_sign_verify(void)
  31643. {
  31644. EXPECT_DECLS;
  31645. #if !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  31646. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  31647. !defined(NO_DSA)
  31648. unsigned char digest[WC_SHA_DIGEST_SIZE];
  31649. DSA_SIG* sig = NULL;
  31650. DSA* dsa = NULL;
  31651. word32 bytes;
  31652. byte sigBin[DSA_SIG_SIZE];
  31653. int dsacheck;
  31654. #ifdef USE_CERT_BUFFERS_1024
  31655. byte tmp[ONEK_BUF];
  31656. XMEMSET(tmp, 0, sizeof(tmp));
  31657. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  31658. bytes = sizeof_dsa_key_der_1024;
  31659. #elif defined(USE_CERT_BUFFERS_2048)
  31660. byte tmp[TWOK_BUF];
  31661. XMEMSET(tmp, 0, sizeof(tmp));
  31662. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  31663. bytes = sizeof_dsa_key_der_2048;
  31664. #else
  31665. byte tmp[TWOK_BUF];
  31666. XFILE fp = XBADFILE;
  31667. XMEMSET(tmp, 0, sizeof(tmp));
  31668. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb") != XBADFILE);
  31669. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  31670. if (fp != XBADFILE)
  31671. XFCLOSE(fp);
  31672. #endif /* END USE_CERT_BUFFERS_1024 */
  31673. XMEMSET(digest, 202, sizeof(digest));
  31674. ExpectNotNull(dsa = DSA_new());
  31675. ExpectIntEQ(DSA_LoadDer(dsa, tmp, (int)bytes), 1);
  31676. ExpectIntEQ(wolfSSL_DSA_do_sign(digest, sigBin, dsa), 1);
  31677. ExpectIntEQ(wolfSSL_DSA_do_verify(digest, sigBin, dsa, &dsacheck), 1);
  31678. ExpectNotNull(sig = DSA_do_sign(digest, WC_SHA_DIGEST_SIZE, dsa));
  31679. ExpectIntEQ(DSA_do_verify(digest, WC_SHA_DIGEST_SIZE, sig, dsa), 1);
  31680. DSA_SIG_free(sig);
  31681. DSA_free(dsa);
  31682. #endif
  31683. #endif /* !HAVE_SELFTEST && !HAVE_FIPS */
  31684. return EXPECT_RESULT();
  31685. }
  31686. static int test_wolfSSL_tmp_dh(void)
  31687. {
  31688. EXPECT_DECLS;
  31689. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  31690. !defined(NO_DSA) && !defined(NO_RSA) && !defined(NO_DH) && !defined(NO_BIO)
  31691. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  31692. byte buff[6000];
  31693. char file[] = "./certs/dsaparams.pem";
  31694. XFILE f = XBADFILE;
  31695. int bytes = 0;
  31696. DSA* dsa = NULL;
  31697. DH* dh = NULL;
  31698. #if defined(WOLFSSL_DH_EXTRA) && \
  31699. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31700. DH* dh2 = NULL;
  31701. #endif
  31702. BIO* bio = NULL;
  31703. SSL* ssl = NULL;
  31704. SSL_CTX* ctx = NULL;
  31705. #ifndef NO_WOLFSSL_SERVER
  31706. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  31707. #else
  31708. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  31709. #endif
  31710. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  31711. WOLFSSL_FILETYPE_PEM));
  31712. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  31713. WOLFSSL_FILETYPE_PEM));
  31714. ExpectNotNull(ssl = SSL_new(ctx));
  31715. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  31716. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  31717. if (f != XBADFILE)
  31718. XFCLOSE(f);
  31719. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  31720. dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL);
  31721. ExpectNotNull(dsa);
  31722. dh = wolfSSL_DSA_dup_DH(dsa);
  31723. ExpectNotNull(dh);
  31724. #if defined(WOLFSSL_DH_EXTRA) && \
  31725. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31726. ExpectNotNull(dh2 = wolfSSL_DH_dup(dh));
  31727. #endif
  31728. ExpectIntEQ((int)SSL_CTX_set_tmp_dh(ctx, dh), WOLFSSL_SUCCESS);
  31729. #ifndef NO_WOLFSSL_SERVER
  31730. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), WOLFSSL_SUCCESS);
  31731. #else
  31732. ExpectIntEQ((int)SSL_set_tmp_dh(ssl, dh), SIDE_ERROR);
  31733. #endif
  31734. BIO_free(bio);
  31735. DSA_free(dsa);
  31736. DH_free(dh);
  31737. dh = NULL;
  31738. #if defined(WOLFSSL_DH_EXTRA) && \
  31739. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH))
  31740. DH_free(dh2);
  31741. dh2 = NULL;
  31742. #endif
  31743. SSL_free(ssl);
  31744. SSL_CTX_free(ctx);
  31745. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  31746. #endif
  31747. return EXPECT_RESULT();
  31748. }
  31749. static int test_wolfSSL_ctrl(void)
  31750. {
  31751. EXPECT_DECLS;
  31752. #if defined (OPENSSL_EXTRA) && !defined(NO_BIO)
  31753. byte buff[6000];
  31754. BIO* bio = NULL;
  31755. int bytes;
  31756. BUF_MEM* ptr = NULL;
  31757. XMEMSET(buff, 0, sizeof(buff));
  31758. bytes = sizeof(buff);
  31759. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  31760. ExpectNotNull(BIO_s_socket());
  31761. ExpectIntEQ((int)wolfSSL_BIO_get_mem_ptr(bio, &ptr), WOLFSSL_SUCCESS);
  31762. /* needs tested after stubs filled out @TODO
  31763. SSL_ctrl
  31764. SSL_CTX_ctrl
  31765. */
  31766. BIO_free(bio);
  31767. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_BIO) */
  31768. return EXPECT_RESULT();
  31769. }
  31770. static int test_wolfSSL_EVP_PKEY_new_mac_key(void)
  31771. {
  31772. EXPECT_DECLS;
  31773. #ifdef OPENSSL_EXTRA
  31774. static const unsigned char pw[] = "password";
  31775. static const int pwSz = sizeof(pw) - 1;
  31776. size_t checkPwSz = 0;
  31777. const unsigned char* checkPw = NULL;
  31778. WOLFSSL_EVP_PKEY* key = NULL;
  31779. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, pw, pwSz));
  31780. ExpectNull(key = wolfSSL_EVP_PKEY_new_mac_key(0, NULL, NULL, pwSz));
  31781. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  31782. pwSz));
  31783. if (key != NULL) {
  31784. ExpectIntEQ(key->type, EVP_PKEY_HMAC);
  31785. ExpectIntEQ(key->save_type, EVP_PKEY_HMAC);
  31786. ExpectIntEQ(key->pkey_sz, pwSz);
  31787. ExpectIntEQ(XMEMCMP(key->pkey.ptr, pw, pwSz), 0);
  31788. }
  31789. ExpectNotNull(checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz));
  31790. ExpectIntEQ((int)checkPwSz, pwSz);
  31791. ExpectIntEQ(XMEMCMP(checkPw, pw, pwSz), 0);
  31792. wolfSSL_EVP_PKEY_free(key);
  31793. key = NULL;
  31794. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, pw,
  31795. 0));
  31796. ExpectIntEQ(key->pkey_sz, 0);
  31797. if (EXPECT_SUCCESS()) {
  31798. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  31799. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  31800. }
  31801. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  31802. ExpectIntEQ((int)checkPwSz, 0);
  31803. wolfSSL_EVP_PKEY_free(key);
  31804. key = NULL;
  31805. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, NULL,
  31806. 0));
  31807. ExpectIntEQ(key->pkey_sz, 0);
  31808. if (EXPECT_SUCCESS()) {
  31809. /* Allocation for key->pkey.ptr may fail - OK key len is 0 */
  31810. checkPw = wolfSSL_EVP_PKEY_get0_hmac(key, &checkPwSz);
  31811. }
  31812. ExpectTrue((checkPwSz == 0) || (checkPw != NULL));
  31813. ExpectIntEQ((int)checkPwSz, 0);
  31814. wolfSSL_EVP_PKEY_free(key);
  31815. key = NULL;
  31816. #endif /* OPENSSL_EXTRA */
  31817. return EXPECT_RESULT();
  31818. }
  31819. static int test_wolfSSL_EVP_PKEY_new_CMAC_key(void)
  31820. {
  31821. EXPECT_DECLS;
  31822. #ifdef OPENSSL_EXTRA
  31823. #if defined(WOLFSSL_CMAC) && !defined(NO_AES) && defined(WOLFSSL_AES_DIRECT)
  31824. const char *priv = "ABCDEFGHIJKLMNOP";
  31825. const WOLFSSL_EVP_CIPHER* cipher = EVP_aes_128_cbc();
  31826. WOLFSSL_EVP_PKEY* key = NULL;
  31827. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31828. NULL, NULL, AES_128_KEY_SIZE, cipher));
  31829. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31830. NULL, (const unsigned char *)priv, 0, cipher));
  31831. ExpectNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31832. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, NULL));
  31833. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_CMAC_key(
  31834. NULL, (const unsigned char *)priv, AES_128_KEY_SIZE, cipher));
  31835. wolfSSL_EVP_PKEY_free(key);
  31836. #endif /* WOLFSSL_CMAC && !NO_AES && WOLFSSL_AES_DIRECT */
  31837. #endif /* OPENSSL_EXTRA */
  31838. return EXPECT_RESULT();
  31839. }
  31840. static int test_wolfSSL_EVP_Digest(void)
  31841. {
  31842. EXPECT_DECLS;
  31843. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_PWDBASED)
  31844. const char* in = "abc";
  31845. int inLen = (int)XSTRLEN(in);
  31846. byte out[WC_SHA256_DIGEST_SIZE];
  31847. unsigned int outLen;
  31848. const char* expOut =
  31849. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  31850. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  31851. "\x15\xAD";
  31852. ExpectIntEQ(wolfSSL_EVP_Digest((unsigned char*)in, inLen, out, &outLen,
  31853. "SHA256", NULL), 1);
  31854. ExpectIntEQ(outLen, WC_SHA256_DIGEST_SIZE);
  31855. ExpectIntEQ(XMEMCMP(out, expOut, WC_SHA256_DIGEST_SIZE), 0);
  31856. #endif /* OPEN_EXTRA && ! NO_SHA256 */
  31857. return EXPECT_RESULT();
  31858. }
  31859. static int test_wolfSSL_EVP_Digest_all(void)
  31860. {
  31861. EXPECT_DECLS;
  31862. #ifdef OPENSSL_EXTRA
  31863. const char* digests[] = {
  31864. #ifndef NO_MD5
  31865. "MD5",
  31866. #endif
  31867. #ifndef NO_SHA
  31868. "SHA",
  31869. #endif
  31870. #ifdef WOLFSSL_SHA224
  31871. "SHA224",
  31872. #endif
  31873. #ifndef NO_SHA256
  31874. "SHA256",
  31875. #endif
  31876. #ifdef WOLFSSL_SHA384
  31877. "SHA384",
  31878. #endif
  31879. #ifdef WOLFSSL_SHA512
  31880. "SHA512",
  31881. #endif
  31882. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_224)
  31883. "SHA512_224",
  31884. #endif
  31885. #if defined(WOLFSSL_SHA512) && !defined(WOLFSSL_NOSHA512_256)
  31886. "SHA512_256",
  31887. #endif
  31888. #ifdef WOLFSSL_SHA3
  31889. #ifndef WOLFSSL_NOSHA3_224
  31890. "SHA3_224",
  31891. #endif
  31892. #ifndef WOLFSSL_NOSHA3_256
  31893. "SHA3_256",
  31894. #endif
  31895. "SHA3_384",
  31896. #ifndef WOLFSSL_NOSHA3_512
  31897. "SHA3_512",
  31898. #endif
  31899. #endif /* WOLFSSL_SHA3 */
  31900. NULL
  31901. };
  31902. const char** d;
  31903. const unsigned char in[] = "abc";
  31904. int inLen = XSTR_SIZEOF(in);
  31905. byte out[WC_MAX_DIGEST_SIZE];
  31906. unsigned int outLen;
  31907. for (d = digests; *d != NULL; d++) {
  31908. ExpectIntEQ(EVP_Digest(in, inLen, out, &outLen, *d, NULL), 1);
  31909. ExpectIntGT(outLen, 0);
  31910. ExpectIntEQ(EVP_MD_size(*d), outLen);
  31911. }
  31912. #endif
  31913. return EXPECT_RESULT();
  31914. }
  31915. static int test_wolfSSL_EVP_MD_size(void)
  31916. {
  31917. EXPECT_DECLS;
  31918. #ifdef OPENSSL_EXTRA
  31919. WOLFSSL_EVP_MD_CTX mdCtx;
  31920. #ifdef WOLFSSL_SHA3
  31921. #ifndef WOLFSSL_NOSHA3_224
  31922. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31923. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_224"), 1);
  31924. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_224_DIGEST_SIZE);
  31925. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_224_BLOCK_SIZE);
  31926. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31927. #endif
  31928. #ifndef WOLFSSL_NOSHA3_256
  31929. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31930. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_256"), 1);
  31931. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_256_DIGEST_SIZE);
  31932. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_256_BLOCK_SIZE);
  31933. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31934. #endif
  31935. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31936. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_384"), 1);
  31937. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_384_DIGEST_SIZE);
  31938. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_384_BLOCK_SIZE);
  31939. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31940. #ifndef WOLFSSL_NOSHA3_512
  31941. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31942. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA3_512"), 1);
  31943. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA3_512_DIGEST_SIZE);
  31944. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA3_512_BLOCK_SIZE);
  31945. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31946. #endif
  31947. #endif /* WOLFSSL_SHA3 */
  31948. #ifndef NO_SHA256
  31949. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31950. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA256"), 1);
  31951. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31952. WC_SHA256_DIGEST_SIZE);
  31953. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31954. WC_SHA256_BLOCK_SIZE);
  31955. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA256_DIGEST_SIZE);
  31956. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA256_BLOCK_SIZE);
  31957. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31958. #endif
  31959. #ifndef NO_MD5
  31960. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31961. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "MD5"), 1);
  31962. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31963. WC_MD5_DIGEST_SIZE);
  31964. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31965. WC_MD5_BLOCK_SIZE);
  31966. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_MD5_DIGEST_SIZE);
  31967. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_MD5_BLOCK_SIZE);
  31968. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31969. #endif
  31970. #ifdef WOLFSSL_SHA224
  31971. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31972. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA224"), 1);
  31973. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31974. WC_SHA224_DIGEST_SIZE);
  31975. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31976. WC_SHA224_BLOCK_SIZE);
  31977. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA224_DIGEST_SIZE);
  31978. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA224_BLOCK_SIZE);
  31979. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31980. #endif
  31981. #ifdef WOLFSSL_SHA384
  31982. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31983. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA384"), 1);
  31984. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31985. WC_SHA384_DIGEST_SIZE);
  31986. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31987. WC_SHA384_BLOCK_SIZE);
  31988. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA384_DIGEST_SIZE);
  31989. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA384_BLOCK_SIZE);
  31990. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  31991. #endif
  31992. #ifdef WOLFSSL_SHA512
  31993. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  31994. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA512"), 1);
  31995. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31996. WC_SHA512_DIGEST_SIZE);
  31997. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  31998. WC_SHA512_BLOCK_SIZE);
  31999. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA512_DIGEST_SIZE);
  32000. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA512_BLOCK_SIZE);
  32001. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32002. #endif
  32003. #ifndef NO_SHA
  32004. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32005. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA"), 1);
  32006. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32007. WC_SHA_DIGEST_SIZE);
  32008. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32009. WC_SHA_BLOCK_SIZE);
  32010. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  32011. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  32012. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32013. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32014. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, "SHA1"), 1);
  32015. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32016. WC_SHA_DIGEST_SIZE);
  32017. ExpectIntEQ(wolfSSL_EVP_MD_block_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32018. WC_SHA_BLOCK_SIZE);
  32019. ExpectIntEQ(wolfSSL_EVP_MD_CTX_size(&mdCtx), WC_SHA_DIGEST_SIZE);
  32020. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), WC_SHA_BLOCK_SIZE);
  32021. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32022. #endif
  32023. /* error case */
  32024. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32025. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, ""), BAD_FUNC_ARG);
  32026. ExpectIntEQ(wolfSSL_EVP_MD_size(wolfSSL_EVP_MD_CTX_md(&mdCtx)),
  32027. BAD_FUNC_ARG);
  32028. ExpectIntEQ(wolfSSL_EVP_MD_CTX_block_size(&mdCtx), BAD_FUNC_ARG);
  32029. /* Cleanup is valid on uninit'ed struct */
  32030. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32031. #endif /* OPENSSL_EXTRA */
  32032. return EXPECT_RESULT();
  32033. }
  32034. static int test_wolfSSL_EVP_MD_pkey_type(void)
  32035. {
  32036. EXPECT_DECLS;
  32037. #ifdef OPENSSL_EXTRA
  32038. const WOLFSSL_EVP_MD* md;
  32039. #ifndef NO_MD5
  32040. ExpectNotNull(md = EVP_md5());
  32041. ExpectIntEQ(EVP_MD_pkey_type(md), NID_md5WithRSAEncryption);
  32042. #endif
  32043. #ifndef NO_SHA
  32044. ExpectNotNull(md = EVP_sha1());
  32045. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha1WithRSAEncryption);
  32046. #endif
  32047. #ifdef WOLFSSL_SHA224
  32048. ExpectNotNull(md = EVP_sha224());
  32049. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha224WithRSAEncryption);
  32050. #endif
  32051. ExpectNotNull(md = EVP_sha256());
  32052. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha256WithRSAEncryption);
  32053. #ifdef WOLFSSL_SHA384
  32054. ExpectNotNull(md = EVP_sha384());
  32055. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha384WithRSAEncryption);
  32056. #endif
  32057. #ifdef WOLFSSL_SHA512
  32058. ExpectNotNull(md = EVP_sha512());
  32059. ExpectIntEQ(EVP_MD_pkey_type(md), NID_sha512WithRSAEncryption);
  32060. #endif
  32061. #endif
  32062. return EXPECT_RESULT();
  32063. }
  32064. #ifdef OPENSSL_EXTRA
  32065. static int test_hmac_signing(const WOLFSSL_EVP_MD *type, const byte* testKey,
  32066. size_t testKeySz, const char* testData, size_t testDataSz,
  32067. const byte* testResult, size_t testResultSz)
  32068. {
  32069. EXPECT_DECLS;
  32070. unsigned char check[WC_MAX_DIGEST_SIZE];
  32071. size_t checkSz = -1;
  32072. WOLFSSL_EVP_PKEY* key = NULL;
  32073. WOLFSSL_EVP_MD_CTX mdCtx;
  32074. ExpectNotNull(key = wolfSSL_EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
  32075. testKey, (int)testKeySz));
  32076. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32077. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  32078. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32079. (unsigned int)testDataSz), 1);
  32080. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32081. ExpectIntEQ((int)checkSz, (int)testResultSz);
  32082. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32083. ExpectIntEQ((int)checkSz,(int)testResultSz);
  32084. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  32085. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32086. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  32087. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32088. (unsigned int)testDataSz), 1);
  32089. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  32090. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32091. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32092. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, type, NULL, key), 1);
  32093. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  32094. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32095. ExpectIntEQ((int)checkSz, (int)testResultSz);
  32096. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32097. ExpectIntEQ((int)checkSz,(int)testResultSz);
  32098. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  32099. (unsigned int)testDataSz - 4), 1);
  32100. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32101. ExpectIntEQ((int)checkSz,(int)testResultSz);
  32102. ExpectIntEQ(XMEMCMP(testResult, check, testResultSz), 0);
  32103. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32104. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, type, NULL, key), 1);
  32105. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  32106. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  32107. (unsigned int)testDataSz - 4), 1);
  32108. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, testResult, checkSz), 1);
  32109. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  32110. wolfSSL_EVP_PKEY_free(key);
  32111. return EXPECT_RESULT();
  32112. }
  32113. #endif
  32114. static int test_wolfSSL_EVP_MD_hmac_signing(void)
  32115. {
  32116. EXPECT_DECLS;
  32117. #ifdef OPENSSL_EXTRA
  32118. static const unsigned char testKey[] =
  32119. {
  32120. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  32121. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  32122. 0x0b, 0x0b, 0x0b, 0x0b
  32123. };
  32124. static const char testData[] = "Hi There";
  32125. #ifdef WOLFSSL_SHA224
  32126. static const unsigned char testResultSha224[] =
  32127. {
  32128. 0x89, 0x6f, 0xb1, 0x12, 0x8a, 0xbb, 0xdf, 0x19,
  32129. 0x68, 0x32, 0x10, 0x7c, 0xd4, 0x9d, 0xf3, 0x3f,
  32130. 0x47, 0xb4, 0xb1, 0x16, 0x99, 0x12, 0xba, 0x4f,
  32131. 0x53, 0x68, 0x4b, 0x22
  32132. };
  32133. #endif
  32134. #ifndef NO_SHA256
  32135. static const unsigned char testResultSha256[] =
  32136. {
  32137. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  32138. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  32139. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  32140. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  32141. };
  32142. #endif
  32143. #ifdef WOLFSSL_SHA384
  32144. static const unsigned char testResultSha384[] =
  32145. {
  32146. 0xaf, 0xd0, 0x39, 0x44, 0xd8, 0x48, 0x95, 0x62,
  32147. 0x6b, 0x08, 0x25, 0xf4, 0xab, 0x46, 0x90, 0x7f,
  32148. 0x15, 0xf9, 0xda, 0xdb, 0xe4, 0x10, 0x1e, 0xc6,
  32149. 0x82, 0xaa, 0x03, 0x4c, 0x7c, 0xeb, 0xc5, 0x9c,
  32150. 0xfa, 0xea, 0x9e, 0xa9, 0x07, 0x6e, 0xde, 0x7f,
  32151. 0x4a, 0xf1, 0x52, 0xe8, 0xb2, 0xfa, 0x9c, 0xb6
  32152. };
  32153. #endif
  32154. #ifdef WOLFSSL_SHA512
  32155. static const unsigned char testResultSha512[] =
  32156. {
  32157. 0x87, 0xaa, 0x7c, 0xde, 0xa5, 0xef, 0x61, 0x9d,
  32158. 0x4f, 0xf0, 0xb4, 0x24, 0x1a, 0x1d, 0x6c, 0xb0,
  32159. 0x23, 0x79, 0xf4, 0xe2, 0xce, 0x4e, 0xc2, 0x78,
  32160. 0x7a, 0xd0, 0xb3, 0x05, 0x45, 0xe1, 0x7c, 0xde,
  32161. 0xda, 0xa8, 0x33, 0xb7, 0xd6, 0xb8, 0xa7, 0x02,
  32162. 0x03, 0x8b, 0x27, 0x4e, 0xae, 0xa3, 0xf4, 0xe4,
  32163. 0xbe, 0x9d, 0x91, 0x4e, 0xeb, 0x61, 0xf1, 0x70,
  32164. 0x2e, 0x69, 0x6c, 0x20, 0x3a, 0x12, 0x68, 0x54
  32165. };
  32166. #endif
  32167. #ifdef WOLFSSL_SHA3
  32168. #ifndef WOLFSSL_NOSHA3_224
  32169. static const unsigned char testResultSha3_224[] =
  32170. {
  32171. 0x3b, 0x16, 0x54, 0x6b, 0xbc, 0x7b, 0xe2, 0x70,
  32172. 0x6a, 0x03, 0x1d, 0xca, 0xfd, 0x56, 0x37, 0x3d,
  32173. 0x98, 0x84, 0x36, 0x76, 0x41, 0xd8, 0xc5, 0x9a,
  32174. 0xf3, 0xc8, 0x60, 0xf7
  32175. };
  32176. #endif
  32177. #ifndef WOLFSSL_NOSHA3_256
  32178. static const unsigned char testResultSha3_256[] =
  32179. {
  32180. 0xba, 0x85, 0x19, 0x23, 0x10, 0xdf, 0xfa, 0x96,
  32181. 0xe2, 0xa3, 0xa4, 0x0e, 0x69, 0x77, 0x43, 0x51,
  32182. 0x14, 0x0b, 0xb7, 0x18, 0x5e, 0x12, 0x02, 0xcd,
  32183. 0xcc, 0x91, 0x75, 0x89, 0xf9, 0x5e, 0x16, 0xbb
  32184. };
  32185. #endif
  32186. #ifndef WOLFSSL_NOSHA3_384
  32187. static const unsigned char testResultSha3_384[] =
  32188. {
  32189. 0x68, 0xd2, 0xdc, 0xf7, 0xfd, 0x4d, 0xdd, 0x0a,
  32190. 0x22, 0x40, 0xc8, 0xa4, 0x37, 0x30, 0x5f, 0x61,
  32191. 0xfb, 0x73, 0x34, 0xcf, 0xb5, 0xd0, 0x22, 0x6e,
  32192. 0x1b, 0xc2, 0x7d, 0xc1, 0x0a, 0x2e, 0x72, 0x3a,
  32193. 0x20, 0xd3, 0x70, 0xb4, 0x77, 0x43, 0x13, 0x0e,
  32194. 0x26, 0xac, 0x7e, 0x3d, 0x53, 0x28, 0x86, 0xbd
  32195. };
  32196. #endif
  32197. #ifndef WOLFSSL_NOSHA3_512
  32198. static const unsigned char testResultSha3_512[] =
  32199. {
  32200. 0xeb, 0x3f, 0xbd, 0x4b, 0x2e, 0xaa, 0xb8, 0xf5,
  32201. 0xc5, 0x04, 0xbd, 0x3a, 0x41, 0x46, 0x5a, 0xac,
  32202. 0xec, 0x15, 0x77, 0x0a, 0x7c, 0xab, 0xac, 0x53,
  32203. 0x1e, 0x48, 0x2f, 0x86, 0x0b, 0x5e, 0xc7, 0xba,
  32204. 0x47, 0xcc, 0xb2, 0xc6, 0xf2, 0xaf, 0xce, 0x8f,
  32205. 0x88, 0xd2, 0x2b, 0x6d, 0xc6, 0x13, 0x80, 0xf2,
  32206. 0x3a, 0x66, 0x8f, 0xd3, 0x88, 0x8b, 0xb8, 0x05,
  32207. 0x37, 0xc0, 0xa0, 0xb8, 0x64, 0x07, 0x68, 0x9e
  32208. };
  32209. #endif
  32210. #endif
  32211. #ifndef NO_SHA256
  32212. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha256(), testKey,
  32213. sizeof(testKey), testData, XSTRLEN(testData), testResultSha256,
  32214. sizeof(testResultSha256)), TEST_SUCCESS);
  32215. #endif
  32216. #ifdef WOLFSSL_SHA224
  32217. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha224(), testKey,
  32218. sizeof(testKey), testData, XSTRLEN(testData), testResultSha224,
  32219. sizeof(testResultSha224)), TEST_SUCCESS);
  32220. #endif
  32221. #ifdef WOLFSSL_SHA384
  32222. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha384(), testKey,
  32223. sizeof(testKey), testData, XSTRLEN(testData), testResultSha384,
  32224. sizeof(testResultSha384)), TEST_SUCCESS);
  32225. #endif
  32226. #ifdef WOLFSSL_SHA512
  32227. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha512(), testKey,
  32228. sizeof(testKey), testData, XSTRLEN(testData), testResultSha512,
  32229. sizeof(testResultSha512)), TEST_SUCCESS);
  32230. #endif
  32231. #ifdef WOLFSSL_SHA3
  32232. #ifndef WOLFSSL_NOSHA3_224
  32233. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_224(), testKey,
  32234. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_224,
  32235. sizeof(testResultSha3_224)), TEST_SUCCESS);
  32236. #endif
  32237. #ifndef WOLFSSL_NOSHA3_256
  32238. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_256(), testKey,
  32239. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_256,
  32240. sizeof(testResultSha3_256)), TEST_SUCCESS);
  32241. #endif
  32242. #ifndef WOLFSSL_NOSHA3_384
  32243. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_384(), testKey,
  32244. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_384,
  32245. sizeof(testResultSha3_384)), TEST_SUCCESS);
  32246. #endif
  32247. #ifndef WOLFSSL_NOSHA3_512
  32248. ExpectIntEQ(test_hmac_signing(wolfSSL_EVP_sha3_512(), testKey,
  32249. sizeof(testKey), testData, XSTRLEN(testData), testResultSha3_512,
  32250. sizeof(testResultSha3_512)), TEST_SUCCESS);
  32251. #endif
  32252. #endif
  32253. #endif /* OPENSSL_EXTRA */
  32254. return EXPECT_RESULT();
  32255. }
  32256. static int test_wolfSSL_EVP_MD_rsa_signing(void)
  32257. {
  32258. EXPECT_DECLS;
  32259. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  32260. WOLFSSL_EVP_PKEY* privKey = NULL;
  32261. WOLFSSL_EVP_PKEY* pubKey = NULL;
  32262. WOLFSSL_EVP_PKEY_CTX* keyCtx = NULL;
  32263. const char testData[] = "Hi There";
  32264. WOLFSSL_EVP_MD_CTX mdCtx;
  32265. WOLFSSL_EVP_MD_CTX mdCtxCopy;
  32266. int ret;
  32267. size_t checkSz = -1;
  32268. int sz = 2048 / 8;
  32269. const unsigned char* cp;
  32270. const unsigned char* p;
  32271. unsigned char check[2048/8];
  32272. size_t i;
  32273. int paddings[] = {
  32274. RSA_PKCS1_PADDING,
  32275. #if !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST) && defined(WC_RSA_PSS)
  32276. RSA_PKCS1_PSS_PADDING,
  32277. #endif
  32278. };
  32279. cp = client_key_der_2048;
  32280. ExpectNotNull((privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &cp,
  32281. sizeof_client_key_der_2048)));
  32282. p = client_keypub_der_2048;
  32283. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  32284. sizeof_client_keypub_der_2048)));
  32285. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32286. wolfSSL_EVP_MD_CTX_init(&mdCtxCopy);
  32287. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32288. NULL, privKey), 1);
  32289. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32290. (unsigned int)XSTRLEN(testData)), 1);
  32291. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32292. ExpectIntEQ((int)checkSz, sz);
  32293. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32294. ExpectIntEQ((int)checkSz,sz);
  32295. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  32296. ExpectIntEQ(wolfSSL_EVP_MD_CTX_copy_ex(&mdCtxCopy, &mdCtx), 1);
  32297. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtxCopy);
  32298. ExpectIntEQ(ret, 1);
  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, NULL, wolfSSL_EVP_sha256(),
  32303. NULL, pubKey), 1);
  32304. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32305. (unsigned int)XSTRLEN(testData)),
  32306. 1);
  32307. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32308. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32309. ExpectIntEQ(ret, 1);
  32310. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32311. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32312. NULL, privKey), 1);
  32313. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  32314. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32315. ExpectIntEQ((int)checkSz, sz);
  32316. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32317. ExpectIntEQ((int)checkSz, sz);
  32318. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  32319. (unsigned int)XSTRLEN(testData) - 4), 1);
  32320. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32321. ExpectIntEQ((int)checkSz, sz);
  32322. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32323. ExpectIntEQ(ret, 1);
  32324. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32325. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32326. NULL, pubKey), 1);
  32327. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  32328. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  32329. (unsigned int)XSTRLEN(testData) - 4),
  32330. 1);
  32331. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32332. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32333. ExpectIntEQ(ret, 1);
  32334. /* Check all signing padding types */
  32335. for (i = 0; i < sizeof(paddings)/sizeof(int); i++) {
  32336. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32337. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, &keyCtx,
  32338. wolfSSL_EVP_sha256(), NULL, privKey), 1);
  32339. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  32340. paddings[i]), 1);
  32341. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32342. (unsigned int)XSTRLEN(testData)), 1);
  32343. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32344. ExpectIntEQ((int)checkSz, sz);
  32345. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32346. ExpectIntEQ((int)checkSz,sz);
  32347. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32348. ExpectIntEQ(ret, 1);
  32349. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32350. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, &keyCtx,
  32351. wolfSSL_EVP_sha256(), NULL, pubKey), 1);
  32352. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_padding(keyCtx,
  32353. paddings[i]), 1);
  32354. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32355. (unsigned int)XSTRLEN(testData)), 1);
  32356. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32357. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32358. ExpectIntEQ(ret, 1);
  32359. }
  32360. wolfSSL_EVP_PKEY_free(pubKey);
  32361. wolfSSL_EVP_PKEY_free(privKey);
  32362. #endif
  32363. return EXPECT_RESULT();
  32364. }
  32365. static int test_wolfSSL_EVP_MD_ecc_signing(void)
  32366. {
  32367. EXPECT_DECLS;
  32368. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  32369. WOLFSSL_EVP_PKEY* privKey = NULL;
  32370. WOLFSSL_EVP_PKEY* pubKey = NULL;
  32371. const char testData[] = "Hi There";
  32372. WOLFSSL_EVP_MD_CTX mdCtx;
  32373. int ret;
  32374. size_t checkSz = -1;
  32375. const unsigned char* cp;
  32376. const unsigned char* p;
  32377. unsigned char check[2048/8];
  32378. cp = ecc_clikey_der_256;
  32379. ExpectNotNull(privKey = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &cp,
  32380. sizeof_ecc_clikey_der_256));
  32381. p = ecc_clikeypub_der_256;
  32382. ExpectNotNull((pubKey = wolfSSL_d2i_PUBKEY(NULL, &p,
  32383. sizeof_ecc_clikeypub_der_256)));
  32384. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32385. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32386. NULL, privKey), 1);
  32387. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData,
  32388. (unsigned int)XSTRLEN(testData)), 1);
  32389. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32390. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32391. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32392. ExpectIntEQ(ret, 1);
  32393. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32394. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32395. NULL, pubKey), 1);
  32396. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData,
  32397. (unsigned int)XSTRLEN(testData)),
  32398. 1);
  32399. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32400. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32401. ExpectIntEQ(ret, 1);
  32402. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32403. ExpectIntEQ(wolfSSL_EVP_DigestSignInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32404. NULL, privKey), 1);
  32405. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData, 4), 1);
  32406. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, NULL, &checkSz), 1);
  32407. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32408. ExpectIntEQ(wolfSSL_EVP_DigestSignUpdate(&mdCtx, testData + 4,
  32409. (unsigned int)XSTRLEN(testData) - 4), 1);
  32410. ExpectIntEQ(wolfSSL_EVP_DigestSignFinal(&mdCtx, check, &checkSz), 1);
  32411. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32412. ExpectIntEQ(ret, 1);
  32413. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  32414. ExpectIntEQ(wolfSSL_EVP_DigestVerifyInit(&mdCtx, NULL, wolfSSL_EVP_sha256(),
  32415. NULL, pubKey), 1);
  32416. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData, 4), 1);
  32417. ExpectIntEQ(wolfSSL_EVP_DigestVerifyUpdate(&mdCtx, testData + 4,
  32418. (unsigned int)XSTRLEN(testData) - 4),
  32419. 1);
  32420. ExpectIntEQ(wolfSSL_EVP_DigestVerifyFinal(&mdCtx, check, checkSz), 1);
  32421. ret = wolfSSL_EVP_MD_CTX_cleanup(&mdCtx);
  32422. ExpectIntEQ(ret, 1);
  32423. wolfSSL_EVP_PKEY_free(pubKey);
  32424. wolfSSL_EVP_PKEY_free(privKey);
  32425. #endif
  32426. return EXPECT_RESULT();
  32427. }
  32428. static int test_wolfSSL_CTX_add_extra_chain_cert(void)
  32429. {
  32430. EXPECT_DECLS;
  32431. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32432. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  32433. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  32434. char caFile[] = "./certs/client-ca.pem";
  32435. char clientFile[] = "./certs/client-cert.pem";
  32436. SSL_CTX* ctx = NULL;
  32437. X509* x509 = NULL;
  32438. BIO *bio = NULL;
  32439. X509 *cert = NULL;
  32440. X509 *ca = NULL;
  32441. STACK_OF(X509) *chain = NULL;
  32442. STACK_OF(X509) *chain2 = NULL;
  32443. #ifndef NO_WOLFSSL_SERVER
  32444. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32445. #else
  32446. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32447. #endif
  32448. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  32449. WOLFSSL_FILETYPE_PEM));
  32450. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  32451. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  32452. WOLFSSL_FILETYPE_PEM));
  32453. /* additional test of getting EVP_PKEY key size from X509
  32454. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  32455. * allowed with user RSA */
  32456. {
  32457. EVP_PKEY* pkey = NULL;
  32458. #if defined(HAVE_ECC)
  32459. X509* ecX509 = NULL;
  32460. #endif /* HAVE_ECC */
  32461. ExpectNotNull(pkey = X509_get_pubkey(x509));
  32462. /* current RSA key is 2048 bit (256 bytes) */
  32463. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  32464. EVP_PKEY_free(pkey);
  32465. pkey = NULL;
  32466. #if defined(HAVE_ECC)
  32467. #if defined(USE_CERT_BUFFERS_256)
  32468. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  32469. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  32470. SSL_FILETYPE_ASN1));
  32471. #else
  32472. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  32473. cliEccCertFile, SSL_FILETYPE_PEM));
  32474. #endif
  32475. pkey = X509_get_pubkey(ecX509);
  32476. ExpectNotNull(pkey);
  32477. /* current ECC key is 256 bit (32 bytes) */
  32478. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  32479. X509_free(ecX509);
  32480. ecX509 = NULL;
  32481. EVP_PKEY_free(pkey);
  32482. pkey = NULL;
  32483. #endif /* HAVE_ECC */
  32484. }
  32485. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), SSL_SUCCESS);
  32486. if (EXPECT_SUCCESS()) {
  32487. x509 = NULL;
  32488. }
  32489. #ifdef WOLFSSL_ENCRYPTED_KEYS
  32490. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  32491. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  32492. #endif
  32493. SSL_CTX_free(ctx);
  32494. ctx = NULL;
  32495. #ifndef NO_WOLFSSL_SERVER
  32496. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  32497. #else
  32498. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  32499. #endif
  32500. /* Test haproxy use case */
  32501. ExpectNotNull(bio = BIO_new_file(svrCertFile, "r"));
  32502. /* Read Certificate */
  32503. ExpectNotNull(cert = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  32504. ExpectNotNull(ca = PEM_read_bio_X509(bio, NULL, NULL, NULL));
  32505. ExpectNotNull(chain = sk_X509_new_null());
  32506. ExpectIntEQ(sk_X509_push(chain, ca), 1);
  32507. if (EXPECT_SUCCESS()) {
  32508. ca = NULL;
  32509. }
  32510. ExpectNotNull(chain2 = X509_chain_up_ref(chain));
  32511. ExpectNotNull(ca = sk_X509_shift(chain2));
  32512. ExpectIntEQ(SSL_CTX_use_certificate(ctx, cert), 1);
  32513. ExpectIntEQ(SSL_CTX_add_extra_chain_cert(ctx, ca), 1);
  32514. if (EXPECT_SUCCESS()) {
  32515. ca = NULL;
  32516. }
  32517. BIO_free(bio);
  32518. X509_free(cert);
  32519. X509_free(ca);
  32520. X509_free(x509);
  32521. sk_X509_pop_free(chain, X509_free);
  32522. sk_X509_pop_free(chain2, X509_free);
  32523. SSL_CTX_free(ctx);
  32524. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  32525. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32526. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && !defined (NO_BIO) */
  32527. return EXPECT_RESULT();
  32528. }
  32529. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  32530. static int test_wolfSSL_ERR_peek_last_error_line(void)
  32531. {
  32532. EXPECT_DECLS;
  32533. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32534. !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL) && \
  32535. !defined(NO_OLD_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  32536. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(NO_ERROR_QUEUE)
  32537. callback_functions client_cb;
  32538. callback_functions server_cb;
  32539. int line = 0;
  32540. int flag = ERR_TXT_STRING;
  32541. const char* file = NULL;
  32542. const char* data = NULL;
  32543. /* create a failed connection and inspect the error */
  32544. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  32545. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  32546. client_cb.method = wolfTLSv1_1_client_method;
  32547. server_cb.method = wolfTLSv1_2_server_method;
  32548. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  32549. ExpectIntGT(ERR_get_error_line_data(NULL, NULL, &data, &flag), 0);
  32550. ExpectNotNull(data);
  32551. /* check clearing error state */
  32552. ERR_remove_state(0);
  32553. ExpectIntEQ((int)ERR_peek_last_error_line(NULL, NULL), 0);
  32554. ERR_peek_last_error_line(NULL, &line);
  32555. ExpectIntEQ(line, 0);
  32556. ERR_peek_last_error_line(&file, NULL);
  32557. ExpectNull(file);
  32558. /* retry connection to fill error queue */
  32559. XMEMSET(&client_cb, 0, sizeof(callback_functions));
  32560. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  32561. client_cb.method = wolfTLSv1_1_client_method;
  32562. server_cb.method = wolfTLSv1_2_server_method;
  32563. test_wolfSSL_client_server_nofail(&client_cb, &server_cb);
  32564. /* check that error code was stored */
  32565. ExpectIntNE((int)ERR_peek_last_error_line(NULL, NULL), 0);
  32566. ERR_peek_last_error_line(NULL, &line);
  32567. ExpectIntNE(line, 0);
  32568. ERR_peek_last_error_line(&file, NULL);
  32569. ExpectNotNull(file);
  32570. fprintf(stderr, "\nTesting error print out\n");
  32571. ERR_print_errors_fp(stderr);
  32572. fprintf(stderr, "Done testing print out\n\n");
  32573. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  32574. * !defined(NO_FILESYSTEM) && !defined(DEBUG_WOLFSSL) */
  32575. return EXPECT_RESULT();
  32576. }
  32577. #endif /* !NO_WOLFSSL_CLIENT && !NO_WOLFSSL_SERVER */
  32578. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32579. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32580. static int verify_cb(int ok, X509_STORE_CTX *ctx)
  32581. {
  32582. (void) ok;
  32583. (void) ctx;
  32584. fprintf(stderr, "ENTER verify_cb\n");
  32585. return SSL_SUCCESS;
  32586. }
  32587. #endif
  32588. static int test_wolfSSL_X509_Name_canon(void)
  32589. {
  32590. EXPECT_DECLS;
  32591. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32592. !defined(NO_FILESYSTEM) && !defined(NO_SHA) && \
  32593. defined(WOLFSSL_CERT_GEN) && \
  32594. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && !defined(NO_RSA)
  32595. const long ex_hash1 = 0x0fdb2da4;
  32596. const long ex_hash2 = 0x9f3e8c9e;
  32597. X509_NAME *name = NULL;
  32598. X509 *x509 = NULL;
  32599. XFILE file = XBADFILE;
  32600. unsigned long hash = 0;
  32601. byte digest[WC_MAX_DIGEST_SIZE] = {0};
  32602. byte *pbuf = NULL;
  32603. word32 len = 0;
  32604. (void) ex_hash2;
  32605. ExpectTrue((file = XFOPEN(caCertFile, "rb")) != XBADFILE);
  32606. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  32607. ExpectNotNull(name = X509_get_issuer_name(x509));
  32608. /* When output buffer is NULL, should return necessary output buffer
  32609. * length.*/
  32610. ExpectIntGT(wolfSSL_i2d_X509_NAME_canon(name, NULL), 0);
  32611. ExpectIntGT((len = (word32)wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  32612. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  32613. hash = (((unsigned long)digest[3] << 24) |
  32614. ((unsigned long)digest[2] << 16) |
  32615. ((unsigned long)digest[1] << 8) |
  32616. ((unsigned long)digest[0]));
  32617. ExpectIntEQ(hash, ex_hash1);
  32618. if (file != XBADFILE) {
  32619. XFCLOSE(file);
  32620. file = XBADFILE;
  32621. }
  32622. X509_free(x509);
  32623. x509 = NULL;
  32624. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  32625. pbuf = NULL;
  32626. ExpectTrue((file = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  32627. ExpectNotNull(x509 = PEM_read_X509(file, NULL, NULL, NULL));
  32628. ExpectNotNull(name = X509_get_issuer_name(x509));
  32629. ExpectIntGT((len = (word32)wolfSSL_i2d_X509_NAME_canon(name, &pbuf)), 0);
  32630. ExpectIntEQ(wc_ShaHash((const byte*)pbuf, (word32)len, digest), 0);
  32631. hash = (((unsigned long)digest[3] << 24) |
  32632. ((unsigned long)digest[2] << 16) |
  32633. ((unsigned long)digest[1] << 8) |
  32634. ((unsigned long)digest[0]));
  32635. ExpectIntEQ(hash, ex_hash2);
  32636. if (file != XBADFILE)
  32637. XFCLOSE(file);
  32638. X509_free(x509);
  32639. XFREE(pbuf, NULL, DYNAMIC_TYPE_OPENSSL);
  32640. #endif
  32641. return EXPECT_RESULT();
  32642. }
  32643. static int test_wolfSSL_X509_LOOKUP_ctrl_hash_dir(void)
  32644. {
  32645. EXPECT_DECLS;
  32646. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR)
  32647. const int MAX_DIR = 4;
  32648. const char paths[][32] = {
  32649. "./certs/ed25519",
  32650. "./certs/ecc",
  32651. "./certs/crl",
  32652. "./certs/",
  32653. };
  32654. char CertCrl_path[MAX_FILENAME_SZ];
  32655. char *p;
  32656. X509_STORE* str = NULL;
  32657. X509_LOOKUP* lookup = NULL;
  32658. WOLFSSL_STACK* sk = NULL;
  32659. int len, total_len, i;
  32660. (void)sk;
  32661. XMEMSET(CertCrl_path, 0, MAX_FILENAME_SZ);
  32662. /* illegal string */
  32663. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32664. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32665. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "",
  32666. SSL_FILETYPE_PEM,NULL), 0);
  32667. /* free store */
  32668. X509_STORE_free(str);
  32669. str = NULL;
  32670. /* short folder string */
  32671. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32672. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32673. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, "./",
  32674. SSL_FILETYPE_PEM,NULL), 1);
  32675. #if defined(WOLFSSL_INT_H)
  32676. /* only available when including internal.h */
  32677. ExpectNotNull(sk = lookup->dirs->dir_entry);
  32678. #endif
  32679. /* free store */
  32680. X509_STORE_free(str);
  32681. str = NULL;
  32682. /* typical function check */
  32683. p = &CertCrl_path[0];
  32684. total_len = 0;
  32685. for (i = MAX_DIR - 1; i>=0 && total_len < MAX_FILENAME_SZ; i--) {
  32686. len = (int)XSTRLEN((const char*)&paths[i]);
  32687. total_len += len;
  32688. XSTRNCPY(p, paths[i], MAX_FILENAME_SZ - total_len);
  32689. p += len;
  32690. if (i != 0) *(p++) = SEPARATOR_CHAR;
  32691. }
  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_ADD_DIR, CertCrl_path,
  32695. SSL_FILETYPE_PEM,NULL), 1);
  32696. #if defined(WOLFSSL_INT_H)
  32697. /* only available when including internal.h */
  32698. ExpectNotNull(sk = lookup->dirs->dir_entry);
  32699. #endif
  32700. X509_STORE_free(str);
  32701. #endif
  32702. return EXPECT_RESULT();
  32703. }
  32704. static int test_wolfSSL_X509_LOOKUP_ctrl_file(void)
  32705. {
  32706. EXPECT_DECLS;
  32707. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32708. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  32709. defined(WOLFSSL_SIGNER_DER_CERT)
  32710. X509_STORE_CTX* ctx = NULL;
  32711. X509_STORE* str = NULL;
  32712. X509_LOOKUP* lookup = NULL;
  32713. X509* cert1 = NULL;
  32714. X509* x509Ca = NULL;
  32715. X509* x509Svr = NULL;
  32716. X509* issuer = NULL;
  32717. WOLFSSL_STACK* sk = NULL;
  32718. X509_NAME* caName = NULL;
  32719. X509_NAME* issuerName = NULL;
  32720. XFILE file1 = XBADFILE;
  32721. int i;
  32722. int cert_count = 0;
  32723. int cmp;
  32724. char der[] = "certs/ca-cert.der";
  32725. #ifdef HAVE_CRL
  32726. char pem[][100] = {
  32727. "./certs/crl/crl.pem",
  32728. "./certs/crl/crl2.pem",
  32729. "./certs/crl/caEccCrl.pem",
  32730. "./certs/crl/eccCliCRL.pem",
  32731. "./certs/crl/eccSrvCRL.pem",
  32732. ""
  32733. };
  32734. #endif
  32735. ExpectTrue((file1 = XFOPEN("./certs/ca-cert.pem", "rb")) != XBADFILE);
  32736. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  32737. if (file1 != XBADFILE)
  32738. XFCLOSE(file1);
  32739. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32740. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32741. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32742. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  32743. SSL_FILETYPE_PEM,NULL), 1);
  32744. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  32745. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  32746. /* check if CA cert is loaded into the store */
  32747. for (i = 0; i < cert_count; i++) {
  32748. x509Ca = sk_X509_value(sk, i);
  32749. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  32750. }
  32751. ExpectNotNull((x509Svr =
  32752. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32753. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  32754. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  32755. issuer = X509_STORE_CTX_get0_current_issuer(ctx);
  32756. ExpectNotNull(issuer);
  32757. caName = X509_get_subject_name(x509Ca);
  32758. ExpectNotNull(caName);
  32759. issuerName = X509_get_subject_name(issuer);
  32760. ExpectNotNull(issuerName);
  32761. cmp = X509_NAME_cmp(caName, issuerName);
  32762. ExpectIntEQ(cmp, 0);
  32763. /* load der format */
  32764. X509_free(issuer);
  32765. issuer = NULL;
  32766. X509_STORE_CTX_free(ctx);
  32767. ctx = NULL;
  32768. X509_STORE_free(str);
  32769. str = NULL;
  32770. sk_X509_pop_free(sk, NULL);
  32771. sk = NULL;
  32772. X509_free(x509Svr);
  32773. x509Svr = NULL;
  32774. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32775. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32776. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, der,
  32777. SSL_FILETYPE_ASN1,NULL), 1);
  32778. ExpectNotNull(sk = wolfSSL_CertManagerGetCerts(str->cm));
  32779. ExpectIntEQ((cert_count = sk_X509_num(sk)), 1);
  32780. /* check if CA cert is loaded into the store */
  32781. for (i = 0; i < cert_count; i++) {
  32782. x509Ca = sk_X509_value(sk, i);
  32783. ExpectIntEQ(0, wolfSSL_X509_cmp(x509Ca, cert1));
  32784. }
  32785. X509_STORE_free(str);
  32786. str = NULL;
  32787. sk_X509_pop_free(sk, NULL);
  32788. sk = NULL;
  32789. X509_free(cert1);
  32790. cert1 = NULL;
  32791. #ifdef HAVE_CRL
  32792. ExpectNotNull(str = wolfSSL_X509_STORE_new());
  32793. ExpectNotNull(lookup = X509_STORE_add_lookup(str, X509_LOOKUP_file()));
  32794. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, caCertFile,
  32795. SSL_FILETYPE_PEM,NULL), 1);
  32796. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD,
  32797. "certs/server-revoked-cert.pem",
  32798. SSL_FILETYPE_PEM,NULL), 1);
  32799. if (str) {
  32800. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm, svrCertFile,
  32801. WOLFSSL_FILETYPE_PEM), 1);
  32802. /* since store hasn't yet known the revoked cert*/
  32803. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  32804. "certs/server-revoked-cert.pem",
  32805. WOLFSSL_FILETYPE_PEM), 1);
  32806. }
  32807. for (i = 0; pem[i][0] != '\0'; i++)
  32808. {
  32809. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_FILE_LOAD, pem[i],
  32810. SSL_FILETYPE_PEM, NULL), 1);
  32811. }
  32812. if (str) {
  32813. /* since store knows crl list */
  32814. ExpectIntEQ(wolfSSL_CertManagerVerify(str->cm,
  32815. "certs/server-revoked-cert.pem",
  32816. WOLFSSL_FILETYPE_PEM ), CRL_CERT_REVOKED);
  32817. }
  32818. ExpectIntEQ(X509_LOOKUP_ctrl(NULL, 0, NULL, 0, NULL), 0);
  32819. X509_STORE_free(str);
  32820. #endif
  32821. #endif
  32822. return EXPECT_RESULT();
  32823. }
  32824. static int test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup(void)
  32825. {
  32826. int res = TEST_SKIPPED;
  32827. #if defined(OPENSSL_EXTRA)
  32828. X509_STORE_CTX_cleanup(NULL);
  32829. X509_STORE_CTX_trusted_stack(NULL, NULL);
  32830. res = TEST_SUCCESS;
  32831. #endif
  32832. return res;
  32833. }
  32834. static int test_wolfSSL_X509_STORE_CTX_get0_current_issuer(void)
  32835. {
  32836. EXPECT_DECLS;
  32837. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  32838. X509_STORE_CTX* ctx = NULL;
  32839. X509_STORE* str = NULL;
  32840. X509* x509Ca = NULL;
  32841. X509* x509Svr = NULL;
  32842. X509* issuer = NULL;
  32843. X509_NAME* caName = NULL;
  32844. X509_NAME* issuerName = NULL;
  32845. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32846. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32847. ExpectNotNull((x509Ca =
  32848. wolfSSL_X509_load_certificate_file(caCertFile, SSL_FILETYPE_PEM)));
  32849. ExpectIntEQ(X509_STORE_add_cert(str, x509Ca), SSL_SUCCESS);
  32850. ExpectNotNull((x509Svr =
  32851. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32852. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509Svr, NULL), SSL_SUCCESS);
  32853. ExpectNull(X509_STORE_CTX_get0_current_issuer(NULL));
  32854. ExpectNotNull(issuer = X509_STORE_CTX_get0_current_issuer(ctx));
  32855. ExpectNotNull(caName = X509_get_subject_name(x509Ca));
  32856. ExpectNotNull(issuerName = X509_get_subject_name(issuer));
  32857. #ifdef WOLFSSL_SIGNER_DER_CERT
  32858. ExpectIntEQ(X509_NAME_cmp(caName, issuerName), 0);
  32859. #endif
  32860. X509_free(issuer);
  32861. X509_STORE_CTX_free(ctx);
  32862. X509_free(x509Svr);
  32863. X509_STORE_free(str);
  32864. X509_free(x509Ca);
  32865. #endif
  32866. return EXPECT_RESULT();
  32867. }
  32868. static int test_wolfSSL_PKCS7_certs(void)
  32869. {
  32870. EXPECT_DECLS;
  32871. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_BIO) && \
  32872. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7)
  32873. STACK_OF(X509)* sk = NULL;
  32874. STACK_OF(X509_INFO)* info_sk = NULL;
  32875. PKCS7 *p7 = NULL;
  32876. BIO* bio = NULL;
  32877. const byte* p = NULL;
  32878. int buflen = 0;
  32879. int i;
  32880. /* Test twice. Once with d2i and once without to test
  32881. * that everything is free'd correctly. */
  32882. for (i = 0; i < 2; i++) {
  32883. ExpectNotNull(p7 = PKCS7_new());
  32884. if (p7 != NULL) {
  32885. p7->version = 1;
  32886. #ifdef NO_SHA
  32887. p7->hashOID = SHA256h;
  32888. #else
  32889. p7->hashOID = SHAh;
  32890. #endif
  32891. }
  32892. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  32893. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  32894. ExpectNotNull(info_sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  32895. ExpectIntEQ(sk_X509_INFO_num(info_sk), 2);
  32896. ExpectNotNull(sk = sk_X509_new_null());
  32897. while (EXPECT_SUCCESS() && (sk_X509_INFO_num(info_sk) > 0)) {
  32898. X509_INFO* info = NULL;
  32899. ExpectNotNull(info = sk_X509_INFO_shift(info_sk));
  32900. ExpectIntEQ(sk_X509_push(sk, info->x509), 1);
  32901. if (EXPECT_SUCCESS() && (info != NULL)) {
  32902. info->x509 = NULL;
  32903. }
  32904. X509_INFO_free(info);
  32905. }
  32906. sk_X509_INFO_pop_free(info_sk, X509_INFO_free);
  32907. info_sk = NULL;
  32908. BIO_free(bio);
  32909. bio = NULL;
  32910. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  32911. ExpectIntEQ(wolfSSL_PKCS7_encode_certs(p7, sk, bio), 1);
  32912. if ((sk != NULL) && ((p7 == NULL) || (bio == NULL))) {
  32913. sk_X509_pop_free(sk, X509_free);
  32914. }
  32915. sk = NULL;
  32916. ExpectIntGT((buflen = BIO_get_mem_data(bio, &p)), 0);
  32917. if (i == 0) {
  32918. PKCS7_free(p7);
  32919. p7 = NULL;
  32920. ExpectNotNull(d2i_PKCS7(&p7, &p, buflen));
  32921. if (p7 != NULL) {
  32922. /* Reset certs to force wolfSSL_PKCS7_to_stack to regenerate
  32923. * them */
  32924. ((WOLFSSL_PKCS7*)p7)->certs = NULL;
  32925. }
  32926. /* PKCS7_free free's the certs */
  32927. ExpectNotNull(wolfSSL_PKCS7_to_stack(p7));
  32928. }
  32929. BIO_free(bio);
  32930. bio = NULL;
  32931. PKCS7_free(p7);
  32932. p7 = NULL;
  32933. }
  32934. #endif /* defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  32935. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && defined(HAVE_PKCS7) */
  32936. return EXPECT_RESULT();
  32937. }
  32938. static int test_wolfSSL_X509_STORE_CTX(void)
  32939. {
  32940. EXPECT_DECLS;
  32941. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  32942. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  32943. X509_STORE_CTX* ctx = NULL;
  32944. X509_STORE* str = NULL;
  32945. X509* x509 = NULL;
  32946. #ifdef OPENSSL_ALL
  32947. X509* x5092 = NULL;
  32948. STACK_OF(X509) *sk = NULL;
  32949. STACK_OF(X509) *sk2 = NULL;
  32950. STACK_OF(X509) *sk3 = NULL;
  32951. #endif
  32952. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32953. ExpectNotNull((str = wolfSSL_X509_STORE_new()));
  32954. ExpectNotNull((x509 =
  32955. wolfSSL_X509_load_certificate_file(svrCertFile, SSL_FILETYPE_PEM)));
  32956. ExpectIntEQ(X509_STORE_add_cert(str, x509), SSL_SUCCESS);
  32957. #ifdef OPENSSL_ALL
  32958. /* sk_X509_new only in OPENSSL_ALL */
  32959. sk = sk_X509_new_null();
  32960. ExpectNotNull(sk);
  32961. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, sk), SSL_SUCCESS);
  32962. #else
  32963. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x509, NULL), SSL_SUCCESS);
  32964. #endif
  32965. ExpectIntEQ(SSL_get_ex_data_X509_STORE_CTX_idx(), 0);
  32966. X509_STORE_CTX_set_error(ctx, -5);
  32967. X509_STORE_CTX_set_error(NULL, -5);
  32968. X509_STORE_CTX_free(ctx);
  32969. ctx = NULL;
  32970. #ifdef OPENSSL_ALL
  32971. sk_X509_pop_free(sk, NULL);
  32972. sk = NULL;
  32973. #endif
  32974. X509_STORE_free(str);
  32975. str = NULL;
  32976. X509_free(x509);
  32977. x509 = NULL;
  32978. ExpectNotNull(ctx = X509_STORE_CTX_new());
  32979. X509_STORE_CTX_set_verify_cb(ctx, verify_cb);
  32980. X509_STORE_CTX_free(ctx);
  32981. ctx = NULL;
  32982. #ifdef OPENSSL_ALL
  32983. /* test X509_STORE_CTX_get(1)_chain */
  32984. ExpectNotNull((x509 = X509_load_certificate_file(svrCertFile,
  32985. SSL_FILETYPE_PEM)));
  32986. ExpectNotNull((x5092 = X509_load_certificate_file(cliCertFile,
  32987. SSL_FILETYPE_PEM)));
  32988. ExpectNotNull((sk = sk_X509_new_null()));
  32989. ExpectIntEQ(sk_X509_push(sk, x509), 1);
  32990. if (EXPECT_FAIL()) {
  32991. X509_free(x509);
  32992. x509 = NULL;
  32993. }
  32994. ExpectNotNull((str = X509_STORE_new()));
  32995. ExpectNotNull((ctx = X509_STORE_CTX_new()));
  32996. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, x5092, sk), 1);
  32997. ExpectNull((sk2 = X509_STORE_CTX_get_chain(NULL)));
  32998. ExpectNotNull((sk2 = X509_STORE_CTX_get_chain(ctx)));
  32999. ExpectIntEQ(sk_num(sk2), 1); /* sanity, make sure chain has 1 cert */
  33000. ExpectNull((sk3 = X509_STORE_CTX_get1_chain(NULL)));
  33001. ExpectNotNull((sk3 = X509_STORE_CTX_get1_chain(ctx)));
  33002. ExpectIntEQ(sk_num(sk3), 1); /* sanity, make sure chain has 1 cert */
  33003. X509_STORE_CTX_free(ctx);
  33004. ctx = NULL;
  33005. X509_STORE_free(str);
  33006. str = NULL;
  33007. /* CTX certs not freed yet */
  33008. X509_free(x5092);
  33009. x5092 = NULL;
  33010. sk_X509_pop_free(sk, NULL);
  33011. sk = NULL;
  33012. /* sk3 is dup so free here */
  33013. sk_X509_pop_free(sk3, NULL);
  33014. sk3 = NULL;
  33015. #endif
  33016. /* test X509_STORE_CTX_get/set_ex_data */
  33017. {
  33018. int i = 0, tmpData = 5;
  33019. void* tmpDataRet;
  33020. ExpectNotNull(ctx = X509_STORE_CTX_new());
  33021. #ifdef HAVE_EX_DATA
  33022. for (i = 0; i < MAX_EX_DATA; i++) {
  33023. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  33024. WOLFSSL_SUCCESS);
  33025. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  33026. ExpectNotNull(tmpDataRet);
  33027. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  33028. }
  33029. #else
  33030. ExpectIntEQ(X509_STORE_CTX_set_ex_data(ctx, i, &tmpData),
  33031. WOLFSSL_FAILURE);
  33032. tmpDataRet = (int*)X509_STORE_CTX_get_ex_data(ctx, i);
  33033. ExpectNull(tmpDataRet);
  33034. #endif
  33035. X509_STORE_CTX_free(ctx);
  33036. ctx = NULL;
  33037. }
  33038. /* test X509_STORE_get/set_ex_data */
  33039. {
  33040. int i = 0, tmpData = 99;
  33041. void* tmpDataRet;
  33042. ExpectNotNull(str = X509_STORE_new());
  33043. #ifdef HAVE_EX_DATA
  33044. for (i = 0; i < MAX_EX_DATA; i++) {
  33045. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  33046. WOLFSSL_SUCCESS);
  33047. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  33048. ExpectNotNull(tmpDataRet);
  33049. ExpectIntEQ(tmpData, *(int*)tmpDataRet);
  33050. }
  33051. #else
  33052. ExpectIntEQ(X509_STORE_set_ex_data(str, i, &tmpData),
  33053. WOLFSSL_FAILURE);
  33054. tmpDataRet = (int*)X509_STORE_get_ex_data(str, i);
  33055. ExpectNull(tmpDataRet);
  33056. #endif
  33057. X509_STORE_free(str);
  33058. str = NULL;
  33059. }
  33060. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33061. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33062. return EXPECT_RESULT();
  33063. }
  33064. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33065. static int test_X509_STORE_untrusted_load_cert_to_stack(const char* filename,
  33066. STACK_OF(X509)* chain)
  33067. {
  33068. EXPECT_DECLS;
  33069. XFILE fp = XBADFILE;
  33070. X509* cert = NULL;
  33071. ExpectTrue((fp = XFOPEN(filename, "rb"))
  33072. != XBADFILE);
  33073. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  33074. if (fp != XBADFILE) {
  33075. XFCLOSE(fp);
  33076. fp = XBADFILE;
  33077. }
  33078. ExpectIntEQ(sk_X509_push(chain, cert), 1);
  33079. if (EXPECT_FAIL())
  33080. X509_free(cert);
  33081. return EXPECT_RESULT();
  33082. }
  33083. static int test_X509_STORE_untrusted_certs(const char** filenames, int ret,
  33084. int err, int loadCA)
  33085. {
  33086. EXPECT_DECLS;
  33087. X509_STORE_CTX* ctx = NULL;
  33088. X509_STORE* str = NULL;
  33089. XFILE fp = XBADFILE;
  33090. X509* cert = NULL;
  33091. STACK_OF(X509)* untrusted = NULL;
  33092. ExpectTrue((fp = XFOPEN("./certs/intermediate/server-int-cert.pem", "rb"))
  33093. != XBADFILE);
  33094. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  33095. if (fp != XBADFILE) {
  33096. XFCLOSE(fp);
  33097. fp = XBADFILE;
  33098. }
  33099. ExpectNotNull(str = X509_STORE_new());
  33100. ExpectNotNull(ctx = X509_STORE_CTX_new());
  33101. ExpectNotNull(untrusted = sk_X509_new_null());
  33102. ExpectIntEQ(X509_STORE_set_flags(str, 0), 1);
  33103. if (loadCA) {
  33104. ExpectIntEQ(X509_STORE_load_locations(str, "./certs/ca-cert.pem", NULL),
  33105. 1);
  33106. }
  33107. for (; *filenames; filenames++) {
  33108. ExpectIntEQ(test_X509_STORE_untrusted_load_cert_to_stack(*filenames,
  33109. untrusted), TEST_SUCCESS);
  33110. }
  33111. ExpectIntEQ(X509_STORE_CTX_init(ctx, str, cert, untrusted), 1);
  33112. ExpectIntEQ(X509_verify_cert(ctx), ret);
  33113. ExpectIntEQ(X509_STORE_CTX_get_error(ctx), err);
  33114. X509_free(cert);
  33115. X509_STORE_free(str);
  33116. X509_STORE_CTX_free(ctx);
  33117. sk_X509_pop_free(untrusted, NULL);
  33118. return EXPECT_RESULT();
  33119. }
  33120. #endif
  33121. static int test_X509_STORE_untrusted(void)
  33122. {
  33123. EXPECT_DECLS;
  33124. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33125. const char* untrusted1[] = {
  33126. "./certs/intermediate/ca-int2-cert.pem",
  33127. NULL
  33128. };
  33129. const char* untrusted2[] = {
  33130. "./certs/intermediate/ca-int-cert.pem",
  33131. "./certs/intermediate/ca-int2-cert.pem",
  33132. NULL
  33133. };
  33134. const char* untrusted3[] = {
  33135. "./certs/intermediate/ca-int-cert.pem",
  33136. "./certs/intermediate/ca-int2-cert.pem",
  33137. "./certs/ca-cert.pem",
  33138. NULL
  33139. };
  33140. /* Adding unrelated certs that should be ignored */
  33141. const char* untrusted4[] = {
  33142. "./certs/client-ca.pem",
  33143. "./certs/intermediate/ca-int-cert.pem",
  33144. "./certs/server-cert.pem",
  33145. "./certs/intermediate/ca-int2-cert.pem",
  33146. NULL
  33147. };
  33148. /* Only immediate issuer in untrusted chain. Fails since can't build chain
  33149. * to loaded CA. */
  33150. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted1, 0,
  33151. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 1), TEST_SUCCESS);
  33152. /* Succeeds because path to loaded CA is available. */
  33153. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted2, 1, 0, 1),
  33154. TEST_SUCCESS);
  33155. /* Fails because root CA is in the untrusted stack */
  33156. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted3, 0,
  33157. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY, 0), TEST_SUCCESS);
  33158. /* Succeeds because path to loaded CA is available. */
  33159. ExpectIntEQ(test_X509_STORE_untrusted_certs(untrusted4, 1, 0, 1),
  33160. TEST_SUCCESS);
  33161. #endif
  33162. return EXPECT_RESULT();
  33163. }
  33164. static int test_wolfSSL_X509_STORE_set_flags(void)
  33165. {
  33166. EXPECT_DECLS;
  33167. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33168. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  33169. X509_STORE* store = NULL;
  33170. X509* x509 = NULL;
  33171. ExpectNotNull((store = wolfSSL_X509_STORE_new()));
  33172. ExpectNotNull((x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  33173. WOLFSSL_FILETYPE_PEM)));
  33174. ExpectIntEQ(X509_STORE_add_cert(store, x509), WOLFSSL_SUCCESS);
  33175. #ifdef HAVE_CRL
  33176. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  33177. WOLFSSL_SUCCESS);
  33178. #else
  33179. ExpectIntEQ(X509_STORE_set_flags(store, WOLFSSL_CRL_CHECKALL),
  33180. NOT_COMPILED_IN);
  33181. #endif
  33182. wolfSSL_X509_free(x509);
  33183. wolfSSL_X509_STORE_free(store);
  33184. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  33185. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33186. return EXPECT_RESULT();
  33187. }
  33188. static int test_wolfSSL_X509_LOOKUP_load_file(void)
  33189. {
  33190. EXPECT_DECLS;
  33191. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && \
  33192. !defined(NO_FILESYSTEM) && !defined(NO_RSA) && \
  33193. (!defined(NO_WOLFSSL_CLIENT) || !defined(WOLFSSL_NO_CLIENT_AUTH))
  33194. WOLFSSL_X509_STORE* store = NULL;
  33195. WOLFSSL_X509_LOOKUP* lookup = NULL;
  33196. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  33197. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  33198. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/client-ca.pem",
  33199. X509_FILETYPE_PEM), 1);
  33200. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/crl/crl2.pem",
  33201. X509_FILETYPE_PEM), 1);
  33202. if (store != NULL) {
  33203. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, cliCertFile,
  33204. WOLFSSL_FILETYPE_PEM), 1);
  33205. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  33206. WOLFSSL_FILETYPE_PEM), ASN_NO_SIGNER_E);
  33207. }
  33208. ExpectIntEQ(wolfSSL_X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  33209. X509_FILETYPE_PEM), 1);
  33210. if (store != NULL) {
  33211. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  33212. WOLFSSL_FILETYPE_PEM), 1);
  33213. }
  33214. wolfSSL_X509_STORE_free(store);
  33215. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_CRL) &&
  33216. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  33217. return EXPECT_RESULT();
  33218. }
  33219. static int test_wolfSSL_X509_STORE_CTX_set_time(void)
  33220. {
  33221. EXPECT_DECLS;
  33222. #if defined(OPENSSL_EXTRA)
  33223. WOLFSSL_X509_STORE_CTX* ctx = NULL;
  33224. time_t c_time;
  33225. ExpectNotNull(ctx = wolfSSL_X509_STORE_CTX_new());
  33226. c_time = 365*24*60*60;
  33227. wolfSSL_X509_STORE_CTX_set_time(ctx, 0, c_time);
  33228. ExpectTrue((ctx->param->flags & WOLFSSL_USE_CHECK_TIME) ==
  33229. WOLFSSL_USE_CHECK_TIME);
  33230. ExpectTrue(ctx->param->check_time == c_time);
  33231. wolfSSL_X509_STORE_CTX_free(ctx);
  33232. #endif /* OPENSSL_EXTRA */
  33233. return EXPECT_RESULT();
  33234. }
  33235. static int test_wolfSSL_CTX_get0_set1_param(void)
  33236. {
  33237. EXPECT_DECLS;
  33238. #if defined(OPENSSL_EXTRA)
  33239. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33240. SSL_CTX* ctx = NULL;
  33241. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  33242. WOLFSSL_X509_VERIFY_PARAM* pvpm = NULL;
  33243. char testIPv4[] = "127.0.0.1";
  33244. char testhostName[] = "foo.hoge.com";
  33245. #ifndef NO_WOLFSSL_SERVER
  33246. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33247. #else
  33248. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33249. #endif
  33250. ExpectNull(SSL_CTX_get0_param(NULL));
  33251. ExpectNotNull(pParam = SSL_CTX_get0_param(ctx));
  33252. ExpectNotNull(pvpm = (WOLFSSL_X509_VERIFY_PARAM *)XMALLOC(
  33253. sizeof(WOLFSSL_X509_VERIFY_PARAM), NULL, DYNAMIC_TYPE_OPENSSL));
  33254. ExpectNotNull(XMEMSET(pvpm, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM)));
  33255. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_host(pvpm, testhostName,
  33256. (int)XSTRLEN(testhostName)), WOLFSSL_SUCCESS);
  33257. ExpectIntEQ(wolfSSL_X509_VERIFY_PARAM_set1_ip_asc(pvpm, testIPv4),
  33258. WOLFSSL_SUCCESS);
  33259. wolfSSL_X509_VERIFY_PARAM_set_hostflags(pvpm, 0x01);
  33260. ExpectIntEQ(SSL_CTX_set1_param(ctx, pvpm), 1);
  33261. ExpectIntEQ(0, XSTRNCMP(pParam->hostName, testhostName,
  33262. (int)XSTRLEN(testhostName)));
  33263. ExpectIntEQ(0x01, pParam->hostFlags);
  33264. ExpectIntEQ(0, XSTRNCMP(pParam->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  33265. /* test for incorrect parameter */
  33266. ExpectIntEQ(1,SSL_CTX_set1_param(ctx, NULL));
  33267. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, pvpm));
  33268. ExpectIntEQ(1,SSL_CTX_set1_param(NULL, NULL));
  33269. SSL_CTX_free(ctx);
  33270. XFREE(pvpm, NULL, DYNAMIC_TYPE_OPENSSL);
  33271. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33272. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  33273. return EXPECT_RESULT();
  33274. }
  33275. static int test_wolfSSL_get0_param(void)
  33276. {
  33277. EXPECT_DECLS;
  33278. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33279. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33280. SSL_CTX* ctx = NULL;
  33281. SSL* ssl = NULL;
  33282. #ifndef NO_WOLFSSL_SERVER
  33283. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33284. #else
  33285. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33286. #endif
  33287. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33288. SSL_FILETYPE_PEM));
  33289. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33290. ExpectNotNull(ssl = SSL_new(ctx));
  33291. ExpectNotNull(SSL_get0_param(ssl));
  33292. SSL_free(ssl);
  33293. SSL_CTX_free(ctx);
  33294. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33295. #endif /* OPENSSL_EXTRA && !defined(NO_RSA)*/
  33296. return EXPECT_RESULT();
  33297. }
  33298. static int test_wolfSSL_X509_VERIFY_PARAM_set1_host(void)
  33299. {
  33300. EXPECT_DECLS;
  33301. #if defined(OPENSSL_EXTRA)
  33302. const char host[] = "www.example.com";
  33303. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  33304. ExpectNotNull(pParam = (WOLFSSL_X509_VERIFY_PARAM*)XMALLOC(
  33305. sizeof(WOLFSSL_X509_VERIFY_PARAM), HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  33306. if (pParam != NULL) {
  33307. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  33308. X509_VERIFY_PARAM_set1_host(pParam, host, sizeof(host));
  33309. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33310. XMEMSET(pParam, 0, sizeof(WOLFSSL_X509_VERIFY_PARAM));
  33311. ExpectIntNE(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33312. XFREE(pParam, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  33313. }
  33314. #endif /* OPENSSL_EXTRA */
  33315. return EXPECT_RESULT();
  33316. }
  33317. static int test_wolfSSL_set1_host(void)
  33318. {
  33319. EXPECT_DECLS;
  33320. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  33321. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  33322. const char host[] = "www.test_wolfSSL_set1_host.com";
  33323. const char emptyStr[] = "";
  33324. SSL_CTX* ctx = NULL;
  33325. SSL* ssl = NULL;
  33326. WOLFSSL_X509_VERIFY_PARAM* pParam = NULL;
  33327. #ifndef NO_WOLFSSL_SERVER
  33328. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  33329. #else
  33330. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33331. #endif
  33332. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33333. SSL_FILETYPE_PEM));
  33334. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33335. ExpectNotNull(ssl = SSL_new(ctx));
  33336. pParam = SSL_get0_param(ssl);
  33337. /* we should get back host string */
  33338. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  33339. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33340. /* we should get back empty string */
  33341. ExpectIntEQ(SSL_set1_host(ssl, emptyStr), WOLFSSL_SUCCESS);
  33342. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  33343. /* we should get back host string */
  33344. ExpectIntEQ(SSL_set1_host(ssl, host), WOLFSSL_SUCCESS);
  33345. ExpectIntEQ(XMEMCMP(pParam->hostName, host, sizeof(host)), 0);
  33346. /* we should get back empty string */
  33347. ExpectIntEQ(SSL_set1_host(ssl, NULL), WOLFSSL_SUCCESS);
  33348. ExpectIntEQ(XMEMCMP(pParam->hostName, emptyStr, sizeof(emptyStr)), 0);
  33349. SSL_free(ssl);
  33350. SSL_CTX_free(ctx);
  33351. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  33352. #endif /* OPENSSL_EXTRA */
  33353. return EXPECT_RESULT();
  33354. }
  33355. static int test_wolfSSL_X509_VERIFY_PARAM_set1_ip(void)
  33356. {
  33357. EXPECT_DECLS;
  33358. #if defined(OPENSSL_EXTRA)
  33359. unsigned char buf[16] = {0};
  33360. WOLFSSL_X509_VERIFY_PARAM* param = NULL;
  33361. ExpectNotNull(param = X509_VERIFY_PARAM_new());
  33362. /* test 127.0.0.1 */
  33363. buf[0] =0x7f; buf[1] = 0; buf[2] = 0; buf[3] = 1;
  33364. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 4), SSL_SUCCESS);
  33365. ExpectIntEQ(XSTRNCMP(param->ipasc, "127.0.0.1", sizeof(param->ipasc)), 0);
  33366. /* test 2001:db8:3333:4444:5555:6666:7777:8888 */
  33367. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33368. buf[4]=51;buf[5]=51;buf[6]=68;buf[7]=68;
  33369. buf[8]=85;buf[9]=85;buf[10]=102;buf[11]=102;
  33370. buf[12]=119;buf[13]=119;buf[14]=136;buf[15]=136;
  33371. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33372. ExpectIntEQ(XSTRNCMP(param->ipasc,
  33373. "2001:db8:3333:4444:5555:6666:7777:8888", sizeof(param->ipasc)), 0);
  33374. /* test 2001:db8:: */
  33375. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33376. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33377. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33378. buf[12]=0;buf[13]=0;buf[14]=0;buf[15]=0;
  33379. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33380. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::", sizeof(param->ipasc)), 0);
  33381. /* test ::1234:5678 */
  33382. buf[0]=0;buf[1]=0;buf[2]=0;buf[3]=0;
  33383. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33384. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33385. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  33386. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33387. ExpectIntEQ(XSTRNCMP(param->ipasc, "::1234:5678", sizeof(param->ipasc)), 0);
  33388. /* test 2001:db8::1234:5678 */
  33389. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33390. buf[4]=0;buf[5]=0;buf[6]=0;buf[7]=0;
  33391. buf[8]=0;buf[9]=0;buf[10]=0;buf[11]=0;
  33392. buf[12]=18;buf[13]=52;buf[14]=86;buf[15]=120;
  33393. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33394. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8::1234:5678",
  33395. sizeof(param->ipasc)), 0);
  33396. /* test 2001:0db8:0001:0000:0000:0ab9:c0a8:0102*/
  33397. /* 2001:db8:1::ab9:c0a8:102 */
  33398. buf[0]=32;buf[1]=1;buf[2]=13;buf[3]=184;
  33399. buf[4]=0;buf[5]=1;buf[6]=0;buf[7]=0;
  33400. buf[8]=0;buf[9]=0;buf[10]=10;buf[11]=185;
  33401. buf[12]=192;buf[13]=168;buf[14]=1;buf[15]=2;
  33402. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip(param, &buf[0], 16), SSL_SUCCESS);
  33403. ExpectIntEQ(XSTRNCMP(param->ipasc, "2001:db8:1::ab9:c0a8:102",
  33404. sizeof(param->ipasc)), 0);
  33405. XFREE(param, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  33406. #endif /* OPENSSL_EXTRA */
  33407. return EXPECT_RESULT();
  33408. }
  33409. static int test_wolfSSL_X509_STORE_CTX_get0_store(void)
  33410. {
  33411. EXPECT_DECLS;
  33412. #if defined(OPENSSL_EXTRA)
  33413. X509_STORE* store = NULL;
  33414. X509_STORE_CTX* ctx = NULL;
  33415. X509_STORE_CTX* ctx_no_init = NULL;
  33416. ExpectNotNull((store = X509_STORE_new()));
  33417. ExpectNotNull(ctx = X509_STORE_CTX_new());
  33418. ExpectNotNull(ctx_no_init = X509_STORE_CTX_new());
  33419. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, NULL, NULL), SSL_SUCCESS);
  33420. ExpectNull(X509_STORE_CTX_get0_store(NULL));
  33421. /* should return NULL if ctx has not bee initialized */
  33422. ExpectNull(X509_STORE_CTX_get0_store(ctx_no_init));
  33423. ExpectNotNull(X509_STORE_CTX_get0_store(ctx));
  33424. wolfSSL_X509_STORE_CTX_free(ctx);
  33425. wolfSSL_X509_STORE_CTX_free(ctx_no_init);
  33426. X509_STORE_free(store);
  33427. #endif /* OPENSSL_EXTRA */
  33428. return EXPECT_RESULT();
  33429. }
  33430. static int test_wolfSSL_CTX_set_client_CA_list(void)
  33431. {
  33432. EXPECT_DECLS;
  33433. #if defined(OPENSSL_ALL) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  33434. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_BIO)
  33435. WOLFSSL_CTX* ctx = NULL;
  33436. WOLFSSL* ssl = NULL;
  33437. X509_NAME* name = NULL;
  33438. STACK_OF(X509_NAME)* names = NULL;
  33439. STACK_OF(X509_NAME)* ca_list = NULL;
  33440. int names_len = 0;
  33441. int i;
  33442. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  33443. /* Send two X501 names in cert request */
  33444. names = SSL_load_client_CA_file(cliCertFile);
  33445. ExpectNotNull(names);
  33446. ca_list = SSL_load_client_CA_file(caCertFile);
  33447. ExpectNotNull(ca_list);
  33448. ExpectNotNull(name = sk_X509_NAME_value(ca_list, 0));
  33449. ExpectIntEQ(sk_X509_NAME_push(names, name), 1);
  33450. if (EXPECT_FAIL()) {
  33451. wolfSSL_X509_NAME_free(name);
  33452. name = NULL;
  33453. }
  33454. SSL_CTX_set_client_CA_list(ctx, names);
  33455. /* This should only free the stack structure */
  33456. sk_X509_NAME_free(ca_list);
  33457. ca_list = NULL;
  33458. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  33459. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  33460. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  33461. for (i = 0; i < names_len; i++) {
  33462. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  33463. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  33464. }
  33465. /* Needed to be able to create ssl object */
  33466. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  33467. SSL_FILETYPE_PEM));
  33468. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  33469. ExpectNotNull(ssl = wolfSSL_new(ctx));
  33470. /* load again as old names are responsibility of ctx to free*/
  33471. names = SSL_load_client_CA_file(cliCertFile);
  33472. ExpectNotNull(names);
  33473. SSL_set_client_CA_list(ssl, names);
  33474. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl));
  33475. ExpectIntEQ(sk_X509_NAME_num(ca_list), sk_X509_NAME_num(names));
  33476. ExpectIntGT((names_len = sk_X509_NAME_num(names)), 0);
  33477. for (i = 0; i < names_len; i++) {
  33478. ExpectNotNull(name = sk_X509_NAME_value(names, i));
  33479. ExpectIntEQ(sk_X509_NAME_find(names, name), i);
  33480. }
  33481. #if !defined(SINGLE_THREADED) && defined(SESSION_CERTS)
  33482. {
  33483. tcp_ready ready;
  33484. func_args server_args;
  33485. callback_functions server_cb;
  33486. THREAD_TYPE serverThread;
  33487. WOLFSSL* ssl_client = NULL;
  33488. WOLFSSL_CTX* ctx_client = NULL;
  33489. SOCKET_T sockfd = 0;
  33490. /* wolfSSL_get_client_CA_list() with handshake */
  33491. StartTCP();
  33492. InitTcpReady(&ready);
  33493. XMEMSET(&server_args, 0, sizeof(func_args));
  33494. XMEMSET(&server_cb, 0, sizeof(callback_functions));
  33495. server_args.signal = &ready;
  33496. server_args.callbacks = &server_cb;
  33497. /* we are responsible for free'ing WOLFSSL_CTX */
  33498. server_cb.ctx = ctx;
  33499. server_cb.isSharedCtx = 1;
  33500. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  33501. cliCertFile, 0));
  33502. start_thread(test_server_nofail, &server_args, &serverThread);
  33503. wait_tcp_ready(&server_args);
  33504. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  33505. ExpectNotNull(ctx_client =
  33506. wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  33507. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(
  33508. ctx_client, caCertFile, 0));
  33509. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(
  33510. ctx_client, cliCertFile, SSL_FILETYPE_PEM));
  33511. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(
  33512. ctx_client, cliKeyFile, SSL_FILETYPE_PEM));
  33513. ExpectNotNull(ssl_client = wolfSSL_new(ctx_client));
  33514. ExpectIntEQ(wolfSSL_set_fd(ssl_client, sockfd), WOLFSSL_SUCCESS);
  33515. ExpectIntEQ(wolfSSL_connect(ssl_client), WOLFSSL_SUCCESS);
  33516. ExpectNotNull(ca_list = SSL_get_client_CA_list(ssl_client));
  33517. /* We are expecting two cert names to be sent */
  33518. ExpectIntEQ(sk_X509_NAME_num(ca_list), 2);
  33519. ExpectNotNull(names = SSL_CTX_get_client_CA_list(ctx));
  33520. for (i=0; i<sk_X509_NAME_num(ca_list); i++) {
  33521. ExpectNotNull(name = sk_X509_NAME_value(ca_list, i));
  33522. ExpectIntGE(sk_X509_NAME_find(names, name), 0);
  33523. }
  33524. wolfSSL_shutdown(ssl_client);
  33525. wolfSSL_free(ssl_client);
  33526. wolfSSL_CTX_free(ctx_client);
  33527. CloseSocket(sockfd);
  33528. join_thread(serverThread);
  33529. FreeTcpReady(&ready);
  33530. }
  33531. #endif
  33532. wolfSSL_free(ssl);
  33533. wolfSSL_CTX_free(ctx);
  33534. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT &&
  33535. * !NO_BIO */
  33536. return EXPECT_RESULT();
  33537. }
  33538. static int test_wolfSSL_CTX_add_client_CA(void)
  33539. {
  33540. EXPECT_DECLS;
  33541. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  33542. !defined(NO_WOLFSSL_CLIENT)
  33543. WOLFSSL_CTX* ctx = NULL;
  33544. WOLFSSL_X509* x509 = NULL;
  33545. WOLFSSL_X509* x509_a = NULL;
  33546. STACK_OF(X509_NAME)* ca_list = NULL;
  33547. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33548. /* Add client cert */
  33549. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  33550. SSL_FILETYPE_PEM));
  33551. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509), SSL_SUCCESS);
  33552. ExpectNotNull(ca_list = SSL_CTX_get_client_CA_list(ctx));
  33553. /* Add another client cert */
  33554. ExpectNotNull(x509_a = X509_load_certificate_file(cliCertFile,
  33555. SSL_FILETYPE_PEM));
  33556. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, x509_a), SSL_SUCCESS);
  33557. /* test for incorrect parameter */
  33558. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, x509), 0);
  33559. ExpectIntEQ(SSL_CTX_add_client_CA(ctx, NULL), 0);
  33560. ExpectIntEQ(SSL_CTX_add_client_CA(NULL, NULL), 0);
  33561. X509_free(x509);
  33562. X509_free(x509_a);
  33563. SSL_CTX_free(ctx);
  33564. #endif /* OPENSSL_EXTRA && !NO_RSA && !NO_CERTS && !NO_WOLFSSL_CLIENT */
  33565. return EXPECT_RESULT();
  33566. }
  33567. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  33568. defined(HAVE_IO_TESTS_DEPENDENCIES)
  33569. static THREAD_RETURN WOLFSSL_THREAD server_task_ech(void* args)
  33570. {
  33571. callback_functions* callbacks = ((func_args*)args)->callbacks;
  33572. WOLFSSL_CTX* ctx = callbacks->ctx;
  33573. WOLFSSL* ssl = NULL;
  33574. SOCKET_T sfd = 0;
  33575. SOCKET_T cfd = 0;
  33576. word16 port;
  33577. char input[1024];
  33578. int idx;
  33579. int ret, err = 0;
  33580. const char* privateName = "ech-private-name.com";
  33581. int privateNameLen = (int)XSTRLEN(privateName);
  33582. ((func_args*)args)->return_code = TEST_FAIL;
  33583. port = ((func_args*)args)->signal->port;
  33584. AssertIntEQ(WOLFSSL_SUCCESS,
  33585. wolfSSL_CTX_load_verify_locations(ctx, cliCertFile, 0));
  33586. AssertIntEQ(WOLFSSL_SUCCESS,
  33587. wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  33588. WOLFSSL_FILETYPE_PEM));
  33589. AssertIntEQ(WOLFSSL_SUCCESS,
  33590. wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  33591. WOLFSSL_FILETYPE_PEM));
  33592. if (callbacks->ctx_ready)
  33593. callbacks->ctx_ready(ctx);
  33594. ssl = wolfSSL_new(ctx);
  33595. /* set the sni for the server */
  33596. wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME, privateName, privateNameLen);
  33597. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, NULL, NULL);
  33598. CloseSocket(sfd);
  33599. AssertIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  33600. if (callbacks->ssl_ready)
  33601. callbacks->ssl_ready(ssl);
  33602. do {
  33603. err = 0; /* Reset error */
  33604. ret = wolfSSL_accept(ssl);
  33605. if (ret != WOLFSSL_SUCCESS) {
  33606. err = wolfSSL_get_error(ssl, 0);
  33607. }
  33608. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  33609. if (ret != WOLFSSL_SUCCESS) {
  33610. char buff[WOLFSSL_MAX_ERROR_SZ];
  33611. fprintf(stderr, "error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buff));
  33612. }
  33613. else {
  33614. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  33615. input[idx] = 0;
  33616. fprintf(stderr, "Client message: %s\n", input);
  33617. }
  33618. AssertIntEQ(privateNameLen, wolfSSL_write(ssl, privateName,
  33619. privateNameLen));
  33620. ((func_args*)args)->return_code = TEST_SUCCESS;
  33621. }
  33622. if (callbacks->on_result)
  33623. callbacks->on_result(ssl);
  33624. wolfSSL_shutdown(ssl);
  33625. wolfSSL_free(ssl);
  33626. wolfSSL_CTX_free(ctx);
  33627. CloseSocket(cfd);
  33628. #ifdef FP_ECC
  33629. wc_ecc_fp_free();
  33630. #endif
  33631. WOLFSSL_RETURN_FROM_THREAD(0);
  33632. }
  33633. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  33634. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  33635. static void keyLog_callback(const WOLFSSL* ssl, const char* line )
  33636. {
  33637. AssertNotNull(ssl);
  33638. AssertNotNull(line);
  33639. XFILE fp;
  33640. const byte lf = '\n';
  33641. fp = XFOPEN("./MyKeyLog.txt", "a");
  33642. XFWRITE( line, 1, strlen(line),fp);
  33643. XFWRITE( (void*)&lf,1,1,fp);
  33644. XFFLUSH(fp);
  33645. XFCLOSE(fp);
  33646. }
  33647. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  33648. static int test_wolfSSL_CTX_set_keylog_callback(void)
  33649. {
  33650. EXPECT_DECLS;
  33651. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  33652. !defined(NO_WOLFSSL_CLIENT)
  33653. SSL_CTX* ctx = NULL;
  33654. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33655. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  33656. SSL_CTX_free(ctx);
  33657. SSL_CTX_set_keylog_callback(NULL, NULL);
  33658. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  33659. return EXPECT_RESULT();
  33660. }
  33661. static int test_wolfSSL_CTX_get_keylog_callback(void)
  33662. {
  33663. EXPECT_DECLS;
  33664. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK) && \
  33665. !defined(NO_WOLFSSL_CLIENT)
  33666. SSL_CTX* ctx = NULL;
  33667. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  33668. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  33669. SSL_CTX_set_keylog_callback(ctx, keyLog_callback );
  33670. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),keyLog_callback);
  33671. SSL_CTX_set_keylog_callback(ctx, NULL );
  33672. ExpectPtrEq(SSL_CTX_get_keylog_callback(ctx),NULL);
  33673. SSL_CTX_free(ctx);
  33674. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && !NO_WOLFSSL_CLIENT */
  33675. return EXPECT_RESULT();
  33676. }
  33677. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  33678. static int test_wolfSSL_Tls12_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  33679. {
  33680. /* set keylog callback */
  33681. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  33682. return TEST_SUCCESS;
  33683. }
  33684. #endif
  33685. static int test_wolfSSL_Tls12_Key_Logging_test(void)
  33686. {
  33687. EXPECT_DECLS;
  33688. #if defined(OPENSSL_EXTRA) && defined(HAVE_SECRET_CALLBACK)
  33689. /* This test is intended for checking whether keylog callback is called
  33690. * in client during TLS handshake between the client and a server.
  33691. */
  33692. test_ssl_cbf server_cbf;
  33693. test_ssl_cbf client_cbf;
  33694. XFILE fp = XBADFILE;
  33695. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  33696. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  33697. server_cbf.method = wolfTLSv1_2_server_method;
  33698. client_cbf.ctx_ready = &test_wolfSSL_Tls12_Key_Logging_client_ctx_ready;
  33699. /* clean up keylog file */
  33700. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  33701. if (fp != XBADFILE) {
  33702. XFFLUSH(fp);
  33703. XFCLOSE(fp);
  33704. fp = XBADFILE;
  33705. }
  33706. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33707. &server_cbf, NULL), TEST_SUCCESS);
  33708. XSLEEP_MS(100);
  33709. /* check if the keylog file exists */
  33710. char buff[300] = {0};
  33711. int found = 0;
  33712. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  33713. XFFLUSH(fp); /* Just to make sure any buffers get flushed */
  33714. while (EXPECT_SUCCESS() && XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  33715. if (0 == strncmp(buff,"CLIENT_RANDOM ", sizeof("CLIENT_RANDOM ")-1)) {
  33716. found = 1;
  33717. break;
  33718. }
  33719. }
  33720. if (fp != XBADFILE) {
  33721. XFCLOSE(fp);
  33722. }
  33723. /* a log starting with "CLIENT_RANDOM " should exit in the file */
  33724. ExpectIntEQ(found, 1);
  33725. /* clean up */
  33726. ExpectIntEQ(rem_file("./MyKeyLog.txt"), 0);
  33727. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK */
  33728. return EXPECT_RESULT();
  33729. }
  33730. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  33731. defined(HAVE_SECRET_CALLBACK)
  33732. static int test_wolfSSL_Tls13_Key_Logging_client_ctx_ready(WOLFSSL_CTX* ctx)
  33733. {
  33734. /* set keylog callback */
  33735. wolfSSL_CTX_set_keylog_callback(ctx, keyLog_callback);
  33736. return TEST_SUCCESS;
  33737. }
  33738. #endif
  33739. static int test_wolfSSL_Tls13_Key_Logging_test(void)
  33740. {
  33741. EXPECT_DECLS;
  33742. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  33743. defined(HAVE_SECRET_CALLBACK)
  33744. /* This test is intended for checking whether keylog callback is called
  33745. * in client during TLS handshake between the client and a server.
  33746. */
  33747. test_ssl_cbf server_cbf;
  33748. test_ssl_cbf client_cbf;
  33749. XFILE fp = XBADFILE;
  33750. XMEMSET(&server_cbf, 0, sizeof(test_ssl_cbf));
  33751. XMEMSET(&client_cbf, 0, sizeof(test_ssl_cbf));
  33752. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  33753. client_cbf.ctx_ready = &test_wolfSSL_Tls13_Key_Logging_client_ctx_ready;
  33754. /* clean up keylog file */
  33755. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "w")) != XBADFILE);
  33756. if (fp != XBADFILE) {
  33757. XFCLOSE(fp);
  33758. fp = XBADFILE;
  33759. }
  33760. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33761. &server_cbf, NULL), TEST_SUCCESS);
  33762. /* check if the keylog file exists */
  33763. {
  33764. char buff[300] = {0};
  33765. int found[4] = {0};
  33766. int numfnd = 0;
  33767. int i;
  33768. ExpectTrue((fp = XFOPEN("./MyKeyLog.txt", "r")) != XBADFILE);
  33769. while (EXPECT_SUCCESS() &&
  33770. XFGETS(buff, (int)sizeof(buff), fp) != NULL) {
  33771. if (0 == strncmp(buff, "CLIENT_HANDSHAKE_TRAFFIC_SECRET ",
  33772. sizeof("CLIENT_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  33773. found[0] = 1;
  33774. continue;
  33775. }
  33776. else if (0 == strncmp(buff, "SERVER_HANDSHAKE_TRAFFIC_SECRET ",
  33777. sizeof("SERVER_HANDSHAKE_TRAFFIC_SECRET ")-1)) {
  33778. found[1] = 1;
  33779. continue;
  33780. }
  33781. else if (0 == strncmp(buff, "CLIENT_TRAFFIC_SECRET_0 ",
  33782. sizeof("CLIENT_TRAFFIC_SECRET_0 ")-1)) {
  33783. found[2] = 1;
  33784. continue;
  33785. }
  33786. else if (0 == strncmp(buff, "SERVER_TRAFFIC_SECRET_0 ",
  33787. sizeof("SERVER_TRAFFIC_SECRET_0 ")-1)) {
  33788. found[3] = 1;
  33789. continue;
  33790. }
  33791. }
  33792. if (fp != XBADFILE)
  33793. XFCLOSE(fp);
  33794. for (i = 0; i < 4; i++) {
  33795. if (found[i] != 0)
  33796. numfnd++;
  33797. }
  33798. ExpectIntEQ(numfnd, 4);
  33799. }
  33800. #endif /* OPENSSL_EXTRA && HAVE_SECRET_CALLBACK && WOLFSSL_TLS13 */
  33801. return EXPECT_RESULT();
  33802. }
  33803. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  33804. defined(HAVE_IO_TESTS_DEPENDENCIES)
  33805. static int test_wolfSSL_Tls13_ECH_params(void)
  33806. {
  33807. EXPECT_DECLS;
  33808. #if !defined(NO_WOLFSSL_CLIENT)
  33809. word32 outputLen = 0;
  33810. byte testBuf[72];
  33811. WOLFSSL_CTX *ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  33812. WOLFSSL *ssl = wolfSSL_new(ctx);
  33813. ExpectNotNull(ctx);
  33814. ExpectNotNull(ssl);
  33815. /* invalid ctx */
  33816. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(NULL,
  33817. "ech-public-name.com", 0, 0, 0));
  33818. /* invalid public name */
  33819. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx, NULL, 0,
  33820. 0, 0));
  33821. /* invalid algorithms */
  33822. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(ctx,
  33823. "ech-public-name.com", 1000, 1000, 1000));
  33824. /* invalid ctx */
  33825. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(NULL, NULL,
  33826. &outputLen));
  33827. /* invalid output len */
  33828. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_CTX_GetEchConfigs(ctx, NULL, NULL));
  33829. /* invalid ssl */
  33830. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(NULL,
  33831. (char*)testBuf, sizeof(testBuf)));
  33832. /* invalid configs64 */
  33833. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl, NULL,
  33834. sizeof(testBuf)));
  33835. /* invalid size */
  33836. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigsBase64(ssl,
  33837. (char*)testBuf, 0));
  33838. /* invalid ssl */
  33839. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(NULL, testBuf,
  33840. sizeof(testBuf)));
  33841. /* invalid configs */
  33842. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, NULL,
  33843. sizeof(testBuf)));
  33844. /* invalid size */
  33845. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, testBuf, 0));
  33846. /* invalid ssl */
  33847. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(NULL, NULL, &outputLen));
  33848. /* invalid size */
  33849. ExpectIntNE(WOLFSSL_SUCCESS, wolfSSL_GetEchConfigs(ssl, NULL, NULL));
  33850. wolfSSL_free(ssl);
  33851. wolfSSL_CTX_free(ctx);
  33852. #endif /* !NO_WOLFSSL_CLIENT */
  33853. return EXPECT_RESULT();
  33854. }
  33855. static int test_wolfSSL_Tls13_ECH(void)
  33856. {
  33857. EXPECT_DECLS;
  33858. tcp_ready ready;
  33859. func_args client_args;
  33860. func_args server_args;
  33861. THREAD_TYPE serverThread;
  33862. callback_functions server_cbf;
  33863. callback_functions client_cbf;
  33864. SOCKET_T sockfd = 0;
  33865. WOLFSSL_CTX* ctx = NULL;
  33866. WOLFSSL* ssl = NULL;
  33867. const char* publicName = "ech-public-name.com";
  33868. const char* privateName = "ech-private-name.com";
  33869. int privateNameLen = 20;
  33870. char reply[1024];
  33871. int replyLen = 0;
  33872. byte rawEchConfig[128];
  33873. word32 rawEchConfigLen = sizeof(rawEchConfig);
  33874. InitTcpReady(&ready);
  33875. ready.port = 22222;
  33876. XMEMSET(&client_args, 0, sizeof(func_args));
  33877. XMEMSET(&server_args, 0, sizeof(func_args));
  33878. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  33879. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  33880. server_cbf.method = wolfTLSv1_3_server_method; /* TLS1.3 */
  33881. /* create the server context here so we can get the ech config */
  33882. ExpectNotNull(server_cbf.ctx =
  33883. wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  33884. /* generate ech config */
  33885. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_GenerateEchConfig(server_cbf.ctx,
  33886. publicName, 0, 0, 0));
  33887. /* get the config for the client to use */
  33888. ExpectIntEQ(WOLFSSL_SUCCESS,
  33889. wolfSSL_CTX_GetEchConfigs(server_cbf.ctx, rawEchConfig,
  33890. &rawEchConfigLen));
  33891. server_args.callbacks = &server_cbf;
  33892. server_args.signal = &ready;
  33893. /* start server task */
  33894. start_thread(server_task_ech, &server_args, &serverThread);
  33895. wait_tcp_ready(&server_args);
  33896. /* run as a TLS1.3 client */
  33897. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  33898. ExpectIntEQ(WOLFSSL_SUCCESS,
  33899. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  33900. ExpectIntEQ(WOLFSSL_SUCCESS,
  33901. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  33902. ExpectIntEQ(WOLFSSL_SUCCESS,
  33903. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  33904. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  33905. /* get connected the server task */
  33906. ExpectNotNull(ssl = wolfSSL_new(ctx));
  33907. /* set the ech configs for the client */
  33908. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetEchConfigs(ssl, rawEchConfig,
  33909. rawEchConfigLen));
  33910. /* set the sni for the client */
  33911. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_UseSNI(ssl, WOLFSSL_SNI_HOST_NAME,
  33912. privateName, privateNameLen));
  33913. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  33914. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  33915. ExpectIntEQ(wolfSSL_write(ssl, privateName, privateNameLen),
  33916. privateNameLen);
  33917. ExpectIntGT((replyLen = wolfSSL_read(ssl, reply, sizeof(reply))), 0);
  33918. /* add th null terminator for string compare */
  33919. reply[replyLen] = 0;
  33920. /* check that the server replied with the private name */
  33921. ExpectStrEQ(privateName, reply);
  33922. wolfSSL_free(ssl);
  33923. wolfSSL_CTX_free(ctx);
  33924. CloseSocket(sockfd);
  33925. join_thread(serverThread);
  33926. FreeTcpReady(&ready);
  33927. return EXPECT_RESULT();
  33928. }
  33929. #endif /* HAVE_ECH && WOLFSSL_TLS13 */
  33930. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  33931. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33932. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  33933. static int post_auth_version_cb(WOLFSSL* ssl)
  33934. {
  33935. EXPECT_DECLS;
  33936. /* do handshake and then test version error */
  33937. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  33938. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  33939. return EXPECT_RESULT();
  33940. }
  33941. static int post_auth_version_client_cb(WOLFSSL* ssl)
  33942. {
  33943. EXPECT_DECLS;
  33944. /* do handshake and then test version error */
  33945. ExpectIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  33946. ExpectStrEQ("TLSv1.2", wolfSSL_get_version(ssl));
  33947. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_FAILURE);
  33948. #if defined(OPENSSL_ALL) && !defined(NO_ERROR_QUEUE)
  33949. /* check was added to error queue */
  33950. ExpectIntEQ(wolfSSL_ERR_get_error(), -UNSUPPORTED_PROTO_VERSION);
  33951. /* check the string matches expected string */
  33952. ExpectStrEQ(wolfSSL_ERR_error_string(-UNSUPPORTED_PROTO_VERSION, NULL),
  33953. "WRONG_SSL_VERSION");
  33954. #endif
  33955. return EXPECT_RESULT();
  33956. }
  33957. static int post_auth_cb(WOLFSSL* ssl)
  33958. {
  33959. EXPECT_DECLS;
  33960. WOLFSSL_X509* x509 = NULL;
  33961. /* do handshake and then test version error */
  33962. ExpectIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  33963. ExpectStrEQ("TLSv1.3", wolfSSL_get_version(ssl));
  33964. ExpectNull(x509 = wolfSSL_get_peer_certificate(ssl));
  33965. wolfSSL_X509_free(x509);
  33966. ExpectIntEQ(wolfSSL_verify_client_post_handshake(ssl), WOLFSSL_SUCCESS);
  33967. return EXPECT_RESULT();
  33968. }
  33969. static int set_post_auth_cb(WOLFSSL* ssl)
  33970. {
  33971. if (!wolfSSL_is_server(ssl)) {
  33972. EXPECT_DECLS;
  33973. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(ssl), 0);
  33974. return EXPECT_RESULT();
  33975. }
  33976. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_POST_HANDSHAKE, NULL);
  33977. return TEST_SUCCESS;
  33978. }
  33979. #endif
  33980. static int test_wolfSSL_Tls13_postauth(void)
  33981. {
  33982. EXPECT_DECLS;
  33983. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  33984. defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  33985. defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  33986. test_ssl_cbf server_cbf;
  33987. test_ssl_cbf client_cbf;
  33988. /* test version failure doing post auth with TLS 1.2 connection */
  33989. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  33990. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  33991. server_cbf.method = wolfTLSv1_2_server_method;
  33992. server_cbf.ssl_ready = set_post_auth_cb;
  33993. server_cbf.on_result = post_auth_version_cb;
  33994. client_cbf.ssl_ready = set_post_auth_cb;
  33995. client_cbf.on_result = post_auth_version_client_cb;
  33996. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  33997. &server_cbf, NULL), TEST_SUCCESS);
  33998. /* tests on post auth with TLS 1.3 */
  33999. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  34000. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  34001. server_cbf.method = wolfTLSv1_3_server_method;
  34002. server_cbf.ssl_ready = set_post_auth_cb;
  34003. client_cbf.ssl_ready = set_post_auth_cb;
  34004. server_cbf.on_result = post_auth_cb;
  34005. client_cbf.on_result = NULL;
  34006. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  34007. &server_cbf, NULL), TEST_SUCCESS);
  34008. #endif
  34009. return EXPECT_RESULT();
  34010. }
  34011. static int test_wolfSSL_X509_NID(void)
  34012. {
  34013. EXPECT_DECLS;
  34014. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)) && \
  34015. !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_ASN)
  34016. int sigType;
  34017. int nameSz;
  34018. X509* cert = NULL;
  34019. EVP_PKEY* pubKeyTmp = NULL;
  34020. X509_NAME* name = NULL;
  34021. char commonName[80];
  34022. char countryName[80];
  34023. char localityName[80];
  34024. char stateName[80];
  34025. char orgName[80];
  34026. char orgUnit[80];
  34027. /* ------ PARSE ORIGINAL SELF-SIGNED CERTIFICATE ------ */
  34028. /* convert cert from DER to internal WOLFSSL_X509 struct */
  34029. ExpectNotNull(cert = wolfSSL_X509_d2i_ex(&cert, client_cert_der_2048,
  34030. sizeof_client_cert_der_2048, HEAP_HINT));
  34031. /* ------ EXTRACT CERTIFICATE ELEMENTS ------ */
  34032. /* extract PUBLIC KEY from cert */
  34033. ExpectNotNull(pubKeyTmp = X509_get_pubkey(cert));
  34034. /* extract signatureType */
  34035. ExpectIntNE((sigType = wolfSSL_X509_get_signature_type(cert)), 0);
  34036. /* extract subjectName info */
  34037. ExpectNotNull(name = X509_get_subject_name(cert));
  34038. ExpectIntEQ(X509_NAME_get_text_by_NID(name, -1, NULL, 0), -1);
  34039. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  34040. NULL, 0)), 0);
  34041. ExpectIntEQ(nameSz, 15);
  34042. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  34043. commonName, sizeof(commonName))), 0);
  34044. ExpectIntEQ(nameSz, 15);
  34045. ExpectIntEQ(XMEMCMP(commonName, "www.wolfssl.com", nameSz), 0);
  34046. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_commonName,
  34047. commonName, 9)), 0);
  34048. ExpectIntEQ(nameSz, 8);
  34049. ExpectIntEQ(XMEMCMP(commonName, "www.wolf", nameSz), 0);
  34050. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_countryName,
  34051. countryName, sizeof(countryName))), 0);
  34052. ExpectIntEQ(XMEMCMP(countryName, "US", nameSz), 0);
  34053. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_localityName,
  34054. localityName, sizeof(localityName))), 0);
  34055. ExpectIntEQ(XMEMCMP(localityName, "Bozeman", nameSz), 0);
  34056. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  34057. NID_stateOrProvinceName, stateName, sizeof(stateName))), 0);
  34058. ExpectIntEQ(XMEMCMP(stateName, "Montana", nameSz), 0);
  34059. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name, NID_organizationName,
  34060. orgName, sizeof(orgName))), 0);
  34061. ExpectIntEQ(XMEMCMP(orgName, "wolfSSL_2048", nameSz), 0);
  34062. ExpectIntGT((nameSz = X509_NAME_get_text_by_NID(name,
  34063. NID_organizationalUnitName, orgUnit, sizeof(orgUnit))), 0);
  34064. ExpectIntEQ(XMEMCMP(orgUnit, "Programming-2048", nameSz), 0);
  34065. EVP_PKEY_free(pubKeyTmp);
  34066. X509_free(cert);
  34067. #endif
  34068. return EXPECT_RESULT();
  34069. }
  34070. static int test_wolfSSL_CTX_set_srp_username(void)
  34071. {
  34072. EXPECT_DECLS;
  34073. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) \
  34074. && !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  34075. WOLFSSL_CTX* ctx = NULL;
  34076. WOLFSSL* ssl = NULL;
  34077. const char *username = "TESTUSER";
  34078. const char *password = "TESTPASSWORD";
  34079. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34080. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  34081. SSL_SUCCESS);
  34082. wolfSSL_CTX_free(ctx);
  34083. ctx = NULL;
  34084. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34085. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  34086. SSL_SUCCESS);
  34087. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  34088. SSL_SUCCESS);
  34089. ExpectNotNull(ssl = SSL_new(ctx));
  34090. ExpectNotNull(SSL_get_srp_username(ssl));
  34091. ExpectStrEQ(SSL_get_srp_username(ssl), username);
  34092. wolfSSL_free(ssl);
  34093. wolfSSL_CTX_free(ctx);
  34094. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  34095. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  34096. return EXPECT_RESULT();
  34097. }
  34098. static int test_wolfSSL_CTX_set_srp_password(void)
  34099. {
  34100. EXPECT_DECLS;
  34101. #if defined(OPENSSL_EXTRA) && defined(WOLFCRYPT_HAVE_SRP) && \
  34102. !defined(NO_SHA256) && !defined(WC_NO_RNG) && !defined(NO_WOLFSSL_CLIENT)
  34103. WOLFSSL_CTX* ctx = NULL;
  34104. const char *username = "TESTUSER";
  34105. const char *password = "TESTPASSWORD";
  34106. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34107. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  34108. SSL_SUCCESS);
  34109. wolfSSL_CTX_free(ctx);
  34110. ctx = NULL;
  34111. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  34112. ExpectIntEQ(wolfSSL_CTX_set_srp_username(ctx, (char *)username),
  34113. SSL_SUCCESS);
  34114. ExpectIntEQ(wolfSSL_CTX_set_srp_password(ctx, (char *)password),
  34115. SSL_SUCCESS);
  34116. wolfSSL_CTX_free(ctx);
  34117. #endif /* OPENSSL_EXTRA && WOLFCRYPT_HAVE_SRP */
  34118. /* && !NO_SHA256 && !WC_NO_RNG && !NO_WOLFSSL_CLIENT */
  34119. return EXPECT_RESULT();
  34120. }
  34121. static int test_wolfSSL_X509_STORE(void)
  34122. {
  34123. EXPECT_DECLS;
  34124. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  34125. X509_STORE *store = NULL;
  34126. #ifdef HAVE_CRL
  34127. X509_STORE_CTX *storeCtx = NULL;
  34128. X509_CRL *crl = NULL;
  34129. X509 *ca = NULL;
  34130. X509 *cert = NULL;
  34131. const char crlPem[] = "./certs/crl/crl.revoked";
  34132. const char srvCert[] = "./certs/server-revoked-cert.pem";
  34133. const char caCert[] = "./certs/ca-cert.pem";
  34134. XFILE fp = XBADFILE;
  34135. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34136. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  34137. SSL_FILETYPE_PEM)));
  34138. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  34139. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  34140. SSL_FILETYPE_PEM)));
  34141. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  34142. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  34143. ExpectIntEQ(X509_verify_cert(storeCtx), SSL_SUCCESS);
  34144. X509_STORE_free(store);
  34145. store = NULL;
  34146. X509_STORE_CTX_free(storeCtx);
  34147. storeCtx = NULL;
  34148. X509_free(cert);
  34149. cert = NULL;
  34150. X509_free(ca);
  34151. ca = NULL;
  34152. /* should fail to verify now after adding in CRL */
  34153. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34154. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  34155. SSL_FILETYPE_PEM)));
  34156. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  34157. ExpectTrue((fp = XFOPEN(crlPem, "rb")) != XBADFILE);
  34158. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  34159. NULL, NULL));
  34160. if (fp != XBADFILE)
  34161. XFCLOSE(fp);
  34162. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  34163. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),SSL_SUCCESS);
  34164. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  34165. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  34166. SSL_FILETYPE_PEM)));
  34167. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  34168. ExpectIntNE(X509_verify_cert(storeCtx), SSL_SUCCESS);
  34169. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx),
  34170. WOLFSSL_X509_V_ERR_CERT_REVOKED);
  34171. X509_CRL_free(crl);
  34172. crl = NULL;
  34173. X509_STORE_free(store);
  34174. store = NULL;
  34175. X509_STORE_CTX_free(storeCtx);
  34176. storeCtx = NULL;
  34177. X509_free(cert);
  34178. cert = NULL;
  34179. X509_free(ca);
  34180. ca = NULL;
  34181. #endif /* HAVE_CRL */
  34182. #ifndef WOLFCRYPT_ONLY
  34183. {
  34184. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  34185. SSL_CTX* ctx = NULL;
  34186. SSL* ssl = NULL;
  34187. int i;
  34188. for (i = 0; i < 2; i++) {
  34189. #ifndef NO_WOLFSSL_SERVER
  34190. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  34191. #else
  34192. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  34193. #endif
  34194. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34195. SSL_CTX_set_cert_store(ctx, store);
  34196. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34197. SSL_CTX_set_cert_store(ctx, store);
  34198. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  34199. ExpectIntEQ(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  34200. SSL_FILETYPE_PEM), SSL_SUCCESS);
  34201. ExpectIntEQ(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  34202. SSL_FILETYPE_PEM), SSL_SUCCESS);
  34203. ExpectNotNull(ssl = SSL_new(ctx));
  34204. if (i == 0) {
  34205. ExpectIntEQ(SSL_set0_verify_cert_store(ssl, store),
  34206. SSL_SUCCESS);
  34207. }
  34208. else {
  34209. ExpectIntEQ(SSL_set1_verify_cert_store(ssl, store), SSL_SUCCESS);
  34210. #ifdef OPENSSL_ALL
  34211. ExpectIntEQ(SSL_CTX_set1_verify_cert_store(ctx, store), SSL_SUCCESS);
  34212. #endif
  34213. }
  34214. if (EXPECT_FAIL() || (i == 1)) {
  34215. X509_STORE_free(store);
  34216. store = NULL;
  34217. }
  34218. SSL_free(ssl);
  34219. ssl = NULL;
  34220. SSL_CTX_free(ctx);
  34221. ctx = NULL;
  34222. }
  34223. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  34224. }
  34225. #endif
  34226. #endif
  34227. return EXPECT_RESULT();
  34228. }
  34229. static int test_wolfSSL_X509_STORE_load_locations(void)
  34230. {
  34231. EXPECT_DECLS;
  34232. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  34233. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  34234. SSL_CTX *ctx = NULL;
  34235. X509_STORE *store = NULL;
  34236. const char ca_file[] = "./certs/ca-cert.pem";
  34237. const char client_pem_file[] = "./certs/client-cert.pem";
  34238. const char client_der_file[] = "./certs/client-cert.der";
  34239. const char ecc_file[] = "./certs/ecc-key.pem";
  34240. const char certs_path[] = "./certs/";
  34241. const char bad_path[] = "./bad-path/";
  34242. #ifdef HAVE_CRL
  34243. const char crl_path[] = "./certs/crl/";
  34244. const char crl_file[] = "./certs/crl/crl.pem";
  34245. #endif
  34246. #ifndef NO_WOLFSSL_SERVER
  34247. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  34248. #else
  34249. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  34250. #endif
  34251. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  34252. ExpectIntEQ(wolfSSL_CertManagerLoadCA(store->cm, ca_file, NULL),
  34253. WOLFSSL_SUCCESS);
  34254. /* Test bad arguments */
  34255. ExpectIntEQ(X509_STORE_load_locations(NULL, ca_file, NULL),
  34256. WOLFSSL_FAILURE);
  34257. ExpectIntEQ(X509_STORE_load_locations(store, NULL, NULL), WOLFSSL_FAILURE);
  34258. ExpectIntEQ(X509_STORE_load_locations(store, client_der_file, NULL),
  34259. WOLFSSL_FAILURE);
  34260. ExpectIntEQ(X509_STORE_load_locations(store, ecc_file, NULL),
  34261. WOLFSSL_FAILURE);
  34262. ExpectIntEQ(X509_STORE_load_locations(store, NULL, bad_path),
  34263. WOLFSSL_FAILURE);
  34264. #ifdef HAVE_CRL
  34265. /* Test with CRL */
  34266. ExpectIntEQ(X509_STORE_load_locations(store, crl_file, NULL),
  34267. WOLFSSL_SUCCESS);
  34268. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crl_path),
  34269. WOLFSSL_SUCCESS);
  34270. #endif
  34271. /* Test with CA */
  34272. ExpectIntEQ(X509_STORE_load_locations(store, ca_file, NULL),
  34273. WOLFSSL_SUCCESS);
  34274. /* Test with client_cert and certs path */
  34275. ExpectIntEQ(X509_STORE_load_locations(store, client_pem_file, NULL),
  34276. WOLFSSL_SUCCESS);
  34277. ExpectIntEQ(X509_STORE_load_locations(store, NULL, certs_path),
  34278. WOLFSSL_SUCCESS);
  34279. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  34280. /* Clear nodes */
  34281. ERR_clear_error();
  34282. #endif
  34283. SSL_CTX_free(ctx);
  34284. #endif
  34285. return EXPECT_RESULT();
  34286. }
  34287. static int test_X509_STORE_get0_objects(void)
  34288. {
  34289. EXPECT_DECLS;
  34290. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && \
  34291. !defined(NO_WOLFSSL_DIR) && !defined(NO_RSA)
  34292. X509_STORE *store = NULL;
  34293. X509_STORE *store_cpy = NULL;
  34294. SSL_CTX *ctx = NULL;
  34295. X509_OBJECT *obj = NULL;
  34296. STACK_OF(X509_OBJECT) *objs = NULL;
  34297. int i;
  34298. /* Setup store */
  34299. #ifndef NO_WOLFSSL_SERVER
  34300. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_server_method()));
  34301. #else
  34302. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_client_method()));
  34303. #endif
  34304. ExpectNotNull(store_cpy = X509_STORE_new());
  34305. ExpectNotNull(store = SSL_CTX_get_cert_store(ctx));
  34306. ExpectIntEQ(X509_STORE_load_locations(store, cliCertFile, NULL),
  34307. WOLFSSL_SUCCESS);
  34308. ExpectIntEQ(X509_STORE_load_locations(store, caCertFile, NULL),
  34309. WOLFSSL_SUCCESS);
  34310. ExpectIntEQ(X509_STORE_load_locations(store, svrCertFile, NULL),
  34311. WOLFSSL_SUCCESS);
  34312. #ifdef HAVE_CRL
  34313. ExpectIntEQ(X509_STORE_load_locations(store, NULL, crlPemDir),
  34314. WOLFSSL_SUCCESS);
  34315. #endif
  34316. /* Store ready */
  34317. /* Similar to HaProxy ssl_set_cert_crl_file use case */
  34318. ExpectNotNull(objs = X509_STORE_get0_objects(store));
  34319. #ifdef HAVE_CRL
  34320. #ifdef WOLFSSL_SIGNER_DER_CERT
  34321. ExpectIntEQ(sk_X509_OBJECT_num(objs), 4);
  34322. #else
  34323. ExpectIntEQ(sk_X509_OBJECT_num(objs), 1);
  34324. #endif
  34325. #else
  34326. #ifdef WOLFSSL_SIGNER_DER_CERT
  34327. ExpectIntEQ(sk_X509_OBJECT_num(objs), 3);
  34328. #else
  34329. ExpectIntEQ(sk_X509_OBJECT_num(objs), 0);
  34330. #endif
  34331. #endif
  34332. for (i = 0; i < sk_X509_OBJECT_num(objs); i++) {
  34333. obj = (X509_OBJECT*)sk_X509_OBJECT_value(objs, i);
  34334. switch (X509_OBJECT_get_type(obj)) {
  34335. case X509_LU_X509:
  34336. {
  34337. WOLFSSL_X509* x509;
  34338. ExpectNotNull(x509 = X509_OBJECT_get0_X509(obj));
  34339. ExpectIntEQ(X509_STORE_add_cert(store_cpy, x509), WOLFSSL_SUCCESS);
  34340. break;
  34341. }
  34342. case X509_LU_CRL:
  34343. #ifdef HAVE_CRL
  34344. {
  34345. WOLFSSL_CRL* crl = NULL;
  34346. ExpectNotNull(crl = X509_OBJECT_get0_X509_CRL(obj));
  34347. ExpectIntEQ(X509_STORE_add_crl(store_cpy, crl), WOLFSSL_SUCCESS);
  34348. break;
  34349. }
  34350. #endif
  34351. case X509_LU_NONE:
  34352. default:
  34353. Fail(("X509_OBJECT_get_type should return x509 or crl "
  34354. "(when built with crl support)"),
  34355. ("Unrecognized X509_OBJECT type or none"));
  34356. }
  34357. }
  34358. X509_STORE_free(store_cpy);
  34359. SSL_CTX_free(ctx);
  34360. #endif
  34361. return EXPECT_RESULT();
  34362. }
  34363. static int test_wolfSSL_BN_CTX(void)
  34364. {
  34365. EXPECT_DECLS;
  34366. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34367. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34368. WOLFSSL_BN_CTX* bn_ctx = NULL;
  34369. WOLFSSL_BIGNUM* t = NULL;
  34370. ExpectNotNull(bn_ctx = wolfSSL_BN_CTX_new());
  34371. /* No implementation. */
  34372. BN_CTX_init(NULL);
  34373. ExpectNotNull(t = BN_CTX_get(NULL));
  34374. BN_free(t);
  34375. ExpectNotNull(t = BN_CTX_get(bn_ctx));
  34376. BN_free(t);
  34377. #ifndef NO_WOLFSSL_STUB
  34378. /* No implementation. */
  34379. BN_CTX_start(NULL);
  34380. BN_CTX_start(bn_ctx);
  34381. #endif
  34382. BN_CTX_free(NULL);
  34383. BN_CTX_free(bn_ctx);
  34384. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34385. return EXPECT_RESULT();
  34386. }
  34387. static int test_wolfSSL_BN(void)
  34388. {
  34389. EXPECT_DECLS;
  34390. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34391. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34392. BIGNUM* a = NULL;
  34393. BIGNUM* b = NULL;
  34394. BIGNUM* c = NULL;
  34395. BIGNUM* d = NULL;
  34396. BIGNUM emptyBN;
  34397. /* Setup */
  34398. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34399. /* internal not set emptyBN. */
  34400. ExpectNotNull(a = BN_new());
  34401. ExpectNotNull(b = BN_new());
  34402. ExpectNotNull(c = BN_dup(b));
  34403. ExpectNotNull(d = BN_new());
  34404. /* Invalid parameter testing. */
  34405. BN_free(NULL);
  34406. ExpectNull(BN_dup(NULL));
  34407. ExpectNull(BN_dup(&emptyBN));
  34408. ExpectNull(BN_copy(NULL, NULL));
  34409. ExpectNull(BN_copy(b, NULL));
  34410. ExpectNull(BN_copy(NULL, c));
  34411. ExpectNull(BN_copy(b, &emptyBN));
  34412. ExpectNull(BN_copy(&emptyBN, c));
  34413. BN_clear(NULL);
  34414. BN_clear(&emptyBN);
  34415. ExpectIntEQ(BN_num_bytes(NULL), 0);
  34416. ExpectIntEQ(BN_num_bytes(&emptyBN), 0);
  34417. ExpectIntEQ(BN_num_bits(NULL), 0);
  34418. ExpectIntEQ(BN_num_bits(&emptyBN), 0);
  34419. ExpectIntEQ(BN_is_negative(NULL), 0);
  34420. ExpectIntEQ(BN_is_negative(&emptyBN), 0);
  34421. /* END Invalid Parameters */
  34422. ExpectIntEQ(BN_set_word(a, 3), SSL_SUCCESS);
  34423. ExpectIntEQ(BN_set_word(b, 2), SSL_SUCCESS);
  34424. ExpectIntEQ(BN_set_word(c, 5), SSL_SUCCESS);
  34425. ExpectIntEQ(BN_num_bits(a), 2);
  34426. ExpectIntEQ(BN_num_bytes(a), 1);
  34427. #if !defined(WOLFSSL_SP_MATH) && (!defined(WOLFSSL_SP_MATH_ALL) || \
  34428. defined(WOLFSSL_SP_INT_NEGATIVE))
  34429. ExpectIntEQ(BN_set_word(a, 1), SSL_SUCCESS);
  34430. ExpectIntEQ(BN_set_word(b, 5), SSL_SUCCESS);
  34431. ExpectIntEQ(BN_is_word(a, (WOLFSSL_BN_ULONG)BN_get_word(a)), SSL_SUCCESS);
  34432. ExpectIntEQ(BN_is_word(a, 3), SSL_FAILURE);
  34433. ExpectIntEQ(BN_sub(c, a, b), SSL_SUCCESS);
  34434. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34435. {
  34436. /* Do additional tests on negative BN conversions. */
  34437. char* ret = NULL;
  34438. ASN1_INTEGER* asn1 = NULL;
  34439. BIGNUM* tmp = NULL;
  34440. /* Sanity check we have a negative BN. */
  34441. ExpectIntEQ(BN_is_negative(c), 1);
  34442. ExpectNotNull(ret = BN_bn2dec(c));
  34443. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  34444. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  34445. ret = NULL;
  34446. /* Convert to ASN1_INTEGER and back to BN. */
  34447. ExpectNotNull(asn1 = BN_to_ASN1_INTEGER(c, NULL));
  34448. ExpectNotNull(tmp = ASN1_INTEGER_to_BN(asn1, NULL));
  34449. /* After converting back BN should be negative and correct. */
  34450. ExpectIntEQ(BN_is_negative(tmp), 1);
  34451. ExpectNotNull(ret = BN_bn2dec(tmp));
  34452. ExpectIntEQ(XMEMCMP(ret, "-4", sizeof("-4")), 0);
  34453. XFREE(ret, NULL, DYNAMIC_TYPE_OPENSSL);
  34454. ASN1_INTEGER_free(asn1);
  34455. BN_free(tmp);
  34456. }
  34457. #endif
  34458. ExpectIntEQ(BN_get_word(c), 4);
  34459. #endif
  34460. ExpectIntEQ(BN_set_word(a, 3), 1);
  34461. ExpectIntEQ(BN_set_word(b, 3), 1);
  34462. ExpectIntEQ(BN_set_word(c, 4), 1);
  34463. /* NULL == NULL, NULL < num, num > NULL */
  34464. ExpectIntEQ(BN_cmp(NULL, NULL), 0);
  34465. ExpectIntEQ(BN_cmp(&emptyBN, &emptyBN), 0);
  34466. ExpectIntLT(BN_cmp(NULL, b), 0);
  34467. ExpectIntLT(BN_cmp(&emptyBN, b), 0);
  34468. ExpectIntGT(BN_cmp(a, NULL), 0);
  34469. ExpectIntGT(BN_cmp(a, &emptyBN), 0);
  34470. ExpectIntEQ(BN_cmp(a, b), 0);
  34471. ExpectIntLT(BN_cmp(a, c), 0);
  34472. ExpectIntGT(BN_cmp(c, b), 0);
  34473. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  34474. ExpectIntEQ(BN_print_fp(XBADFILE, NULL), 0);
  34475. ExpectIntEQ(BN_print_fp(XBADFILE, &emptyBN), 0);
  34476. ExpectIntEQ(BN_print_fp(stderr, NULL), 0);
  34477. ExpectIntEQ(BN_print_fp(stderr, &emptyBN), 0);
  34478. ExpectIntEQ(BN_print_fp(XBADFILE, a), 0);
  34479. ExpectIntEQ(BN_print_fp(stderr, a), 1);
  34480. #endif
  34481. BN_clear(a);
  34482. BN_free(a);
  34483. BN_free(b);
  34484. BN_free(c);
  34485. BN_clear_free(d);
  34486. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34487. return EXPECT_RESULT();
  34488. }
  34489. static int test_wolfSSL_BN_init(void)
  34490. {
  34491. EXPECT_DECLS;
  34492. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34493. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34494. #if !defined(USE_INTEGER_HEAP_MATH) && !defined(HAVE_WOLF_BIGINT)
  34495. BIGNUM* ap = NULL;
  34496. BIGNUM bv;
  34497. BIGNUM cv;
  34498. BIGNUM dv;
  34499. ExpectNotNull(ap = BN_new());
  34500. BN_init(NULL);
  34501. XMEMSET(&bv, 0, sizeof(bv));
  34502. ExpectNull(BN_dup(&bv));
  34503. BN_init(&bv);
  34504. BN_init(&cv);
  34505. BN_init(&dv);
  34506. ExpectIntEQ(BN_set_word(ap, 3), SSL_SUCCESS);
  34507. ExpectIntEQ(BN_set_word(&bv, 2), SSL_SUCCESS);
  34508. ExpectIntEQ(BN_set_word(&cv, 5), SSL_SUCCESS);
  34509. /* a^b mod c = */
  34510. ExpectIntEQ(BN_mod_exp(&dv, NULL, &bv, &cv, NULL), WOLFSSL_FAILURE);
  34511. ExpectIntEQ(BN_mod_exp(&dv, ap, &bv, &cv, NULL), WOLFSSL_SUCCESS);
  34512. /* check result 3^2 mod 5 */
  34513. ExpectIntEQ(BN_get_word(&dv), 4);
  34514. /* a*b mod c = */
  34515. ExpectIntEQ(BN_mod_mul(&dv, NULL, &bv, &cv, NULL), SSL_FAILURE);
  34516. ExpectIntEQ(BN_mod_mul(&dv, ap, &bv, &cv, NULL), SSL_SUCCESS);
  34517. /* check result 3*2 mod 5 */
  34518. ExpectIntEQ(BN_get_word(&dv), 1);
  34519. BN_free(ap);
  34520. #endif
  34521. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34522. return EXPECT_RESULT();
  34523. }
  34524. static int test_wolfSSL_BN_enc_dec(void)
  34525. {
  34526. EXPECT_DECLS;
  34527. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  34528. BIGNUM* a = NULL;
  34529. BIGNUM* b = NULL;
  34530. BIGNUM* c = NULL;
  34531. BIGNUM emptyBN;
  34532. char* str = NULL;
  34533. const char* emptyStr = "";
  34534. const char* numberStr = "12345";
  34535. const char* badStr = "g12345";
  34536. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34537. const char* twoStr = "2";
  34538. #endif
  34539. unsigned char binNum[] = { 0x01, 0x02, 0x03, 0x04, 0x05 };
  34540. unsigned char outNum[5];
  34541. /* Setup */
  34542. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34543. ExpectNotNull(a = BN_new());
  34544. ExpectNotNull(b = BN_new());
  34545. ExpectIntEQ(BN_set_word(a, 2), 1);
  34546. /* Invalid parameters */
  34547. ExpectIntEQ(BN_bn2bin(NULL, NULL), -1);
  34548. ExpectIntEQ(BN_bn2bin(&emptyBN, NULL), -1);
  34549. ExpectIntEQ(BN_bn2bin(NULL, outNum), -1);
  34550. ExpectIntEQ(BN_bn2bin(&emptyBN, outNum), -1);
  34551. ExpectNull(BN_bn2hex(NULL));
  34552. ExpectNull(BN_bn2hex(&emptyBN));
  34553. ExpectNull(BN_bn2dec(NULL));
  34554. ExpectNull(BN_bn2dec(&emptyBN));
  34555. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), NULL));
  34556. ExpectNull(BN_bin2bn(NULL, sizeof(binNum), a));
  34557. ExpectNull(BN_bin2bn(binNum, -1, a));
  34558. ExpectNull(BN_bin2bn(binNum, -1, NULL));
  34559. ExpectNull(BN_bin2bn(binNum, sizeof(binNum), &emptyBN));
  34560. ExpectIntEQ(BN_hex2bn(NULL, NULL), 0);
  34561. ExpectIntEQ(BN_hex2bn(NULL, numberStr), 0);
  34562. ExpectIntEQ(BN_hex2bn(&a, NULL), 0);
  34563. ExpectIntEQ(BN_hex2bn(&a, emptyStr), 0);
  34564. ExpectIntEQ(BN_hex2bn(&a, badStr), 0);
  34565. ExpectIntEQ(BN_hex2bn(&c, badStr), 0);
  34566. ExpectIntEQ(BN_dec2bn(NULL, NULL), 0);
  34567. ExpectIntEQ(BN_dec2bn(NULL, numberStr), 0);
  34568. ExpectIntEQ(BN_dec2bn(&a, NULL), 0);
  34569. ExpectIntEQ(BN_dec2bn(&a, emptyStr), 0);
  34570. ExpectIntEQ(BN_dec2bn(&a, badStr), 0);
  34571. ExpectIntEQ(BN_dec2bn(&c, badStr), 0);
  34572. ExpectIntEQ(BN_set_word(a, 2), 1);
  34573. ExpectIntEQ(BN_bn2bin(a, NULL), 1);
  34574. ExpectIntEQ(BN_bn2bin(a, outNum), 1);
  34575. ExpectNotNull(BN_bin2bn(outNum, 1, b));
  34576. ExpectIntEQ(BN_cmp(a, b), 0);
  34577. ExpectNotNull(BN_bin2bn(binNum, sizeof(binNum), b));
  34578. ExpectIntEQ(BN_cmp(a, b), -1);
  34579. ExpectNotNull(str = BN_bn2hex(a));
  34580. ExpectNotNull(BN_hex2bn(&b, str));
  34581. ExpectIntEQ(BN_cmp(a, b), 0);
  34582. ExpectNotNull(BN_hex2bn(&b, numberStr));
  34583. ExpectIntEQ(BN_cmp(a, b), -1);
  34584. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34585. str = NULL;
  34586. #if defined(WOLFSSL_KEY_GEN) || defined(HAVE_COMP_KEY)
  34587. ExpectNotNull(str = BN_bn2dec(a));
  34588. ExpectStrEQ(str, twoStr);
  34589. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34590. str = NULL;
  34591. #ifndef NO_RSA
  34592. ExpectNotNull(str = BN_bn2dec(a));
  34593. ExpectNotNull(BN_dec2bn(&b, str));
  34594. ExpectIntEQ(BN_cmp(a, b), 0);
  34595. ExpectNotNull(BN_dec2bn(&b, numberStr));
  34596. ExpectIntEQ(BN_cmp(a, b), -1);
  34597. XFREE(str, NULL, DYNAMIC_TYPE_OPENSSL);
  34598. str = NULL;
  34599. #else
  34600. /* No implementation - fail with good parameters. */
  34601. ExpectIntEQ(BN_dec2bn(&a, numberStr), 0);
  34602. #endif
  34603. #endif
  34604. BN_free(b);
  34605. BN_free(a);
  34606. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34607. return EXPECT_RESULT();
  34608. }
  34609. static int test_wolfSSL_BN_word(void)
  34610. {
  34611. EXPECT_DECLS;
  34612. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && !defined(WOLFSSL_SP_MATH)
  34613. BIGNUM* a = NULL;
  34614. BIGNUM* b = NULL;
  34615. BIGNUM* c = NULL;
  34616. BIGNUM av;
  34617. ExpectNotNull(a = BN_new());
  34618. ExpectNotNull(b = BN_new());
  34619. ExpectNotNull(c = BN_new());
  34620. XMEMSET(&av, 0, sizeof(av));
  34621. /* Invalid parameter. */
  34622. ExpectIntEQ(BN_add_word(NULL, 3), 0);
  34623. ExpectIntEQ(BN_add_word(&av, 3), 0);
  34624. ExpectIntEQ(BN_sub_word(NULL, 3), 0);
  34625. ExpectIntEQ(BN_sub_word(&av, 3), 0);
  34626. ExpectIntEQ(BN_set_word(NULL, 3), 0);
  34627. ExpectIntEQ(BN_set_word(&av, 3), 0);
  34628. ExpectIntEQ(BN_get_word(NULL), 0);
  34629. ExpectIntEQ(BN_get_word(&av), 0);
  34630. ExpectIntEQ(BN_is_word(NULL, 3), 0);
  34631. ExpectIntEQ(BN_is_word(&av, 3), 0);
  34632. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  34633. !defined(NO_DSA))
  34634. ExpectIntEQ(BN_mod_word(NULL, 3), -1);
  34635. ExpectIntEQ(BN_mod_word(&av, 3), -1);
  34636. #endif
  34637. ExpectIntEQ(BN_one(NULL), 0);
  34638. ExpectIntEQ(BN_one(&av), 0);
  34639. BN_zero(NULL);
  34640. BN_zero(&av);
  34641. ExpectIntEQ(BN_is_one(NULL), 0);
  34642. ExpectIntEQ(BN_is_one(&av), 0);
  34643. ExpectIntEQ(BN_is_zero(NULL), 0);
  34644. ExpectIntEQ(BN_is_zero(&av), 0);
  34645. ExpectIntEQ(BN_set_word(a, 3), 1);
  34646. ExpectIntEQ(BN_set_word(b, 2), 1);
  34647. ExpectIntEQ(BN_set_word(c, 5), 1);
  34648. /* a + 3 = */
  34649. ExpectIntEQ(BN_add_word(a, 3), 1);
  34650. /* check result 3 + 3*/
  34651. ExpectIntEQ(BN_get_word(a), 6);
  34652. ExpectIntEQ(BN_is_word(a, 6), 1);
  34653. ExpectIntEQ(BN_is_word(a, 5), 0);
  34654. /* set a back to 3 */
  34655. ExpectIntEQ(BN_set_word(a, 3), 1);
  34656. /* a - 3 = */
  34657. ExpectIntEQ(BN_sub_word(a, 3), 1);
  34658. /* check result 3 - 3*/
  34659. ExpectIntEQ(BN_get_word(a), 0);
  34660. ExpectIntEQ(BN_one(a), 1);
  34661. ExpectIntEQ(BN_is_word(a, 1), 1);
  34662. ExpectIntEQ(BN_is_word(a, 0), 0);
  34663. ExpectIntEQ(BN_is_one(a), 1);
  34664. ExpectIntEQ(BN_is_zero(a), 0);
  34665. BN_zero(a);
  34666. ExpectIntEQ(BN_is_word(a, 0), 1);
  34667. ExpectIntEQ(BN_is_word(a, 1), 0);
  34668. ExpectIntEQ(BN_is_zero(a), 1);
  34669. ExpectIntEQ(BN_is_one(a), 0);
  34670. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || \
  34671. !defined(NO_DSA))
  34672. ExpectIntEQ(BN_set_word(a, 5), 1);
  34673. ExpectIntEQ(BN_mod_word(a, 3), 2);
  34674. ExpectIntEQ(BN_mod_word(a, 0), -1);
  34675. #endif
  34676. BN_free(c);
  34677. BN_free(b);
  34678. BN_free(a);
  34679. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  34680. return EXPECT_RESULT();
  34681. }
  34682. static int test_wolfSSL_BN_bits(void)
  34683. {
  34684. EXPECT_DECLS;
  34685. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34686. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34687. BIGNUM* a = NULL;
  34688. BIGNUM emptyBN;
  34689. /* Setup */
  34690. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34691. ExpectNotNull(a = BN_new());
  34692. /* Invalid parameters. */
  34693. ExpectIntEQ(BN_set_bit(NULL, 1), 0);
  34694. ExpectIntEQ(BN_set_bit(&emptyBN, 1), 0);
  34695. ExpectIntEQ(BN_set_bit(a, -1), 0);
  34696. ExpectIntEQ(BN_clear_bit(NULL, 1), 0);
  34697. ExpectIntEQ(BN_clear_bit(&emptyBN, 1), 0);
  34698. ExpectIntEQ(BN_clear_bit(a, -1), 0);
  34699. ExpectIntEQ(BN_is_bit_set(NULL, 1), 0);
  34700. ExpectIntEQ(BN_is_bit_set(&emptyBN, 1), 0);
  34701. ExpectIntEQ(BN_is_bit_set(a, -1), 0);
  34702. ExpectIntEQ(BN_is_odd(NULL), 0);
  34703. ExpectIntEQ(BN_is_odd(&emptyBN), 0);
  34704. ExpectIntEQ(BN_set_word(a, 0), 1);
  34705. ExpectIntEQ(BN_is_zero(a), 1);
  34706. ExpectIntEQ(BN_set_bit(a, 0x45), 1);
  34707. ExpectIntEQ(BN_is_zero(a), 0);
  34708. ExpectIntEQ(BN_is_bit_set(a, 0x45), 1);
  34709. ExpectIntEQ(BN_clear_bit(a, 0x45), 1);
  34710. ExpectIntEQ(BN_is_bit_set(a, 0x45), 0);
  34711. ExpectIntEQ(BN_is_zero(a), 1);
  34712. ExpectIntEQ(BN_set_bit(a, 0), 1);
  34713. ExpectIntEQ(BN_is_odd(a), 1);
  34714. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  34715. ExpectIntEQ(BN_is_odd(a), 0);
  34716. ExpectIntEQ(BN_set_bit(a, 1), 1);
  34717. ExpectIntEQ(BN_is_odd(a), 0);
  34718. ExpectIntEQ(BN_set_bit(a, 129), 1);
  34719. ExpectIntEQ(BN_get_word(a), WOLFSSL_BN_MAX_VAL);
  34720. #ifndef NO_WOLFSSL_STUB
  34721. ExpectIntEQ(BN_mask_bits(a, 1), 0);
  34722. #endif
  34723. BN_free(a);
  34724. #endif
  34725. return EXPECT_RESULT();
  34726. }
  34727. static int test_wolfSSL_BN_shift(void)
  34728. {
  34729. EXPECT_DECLS;
  34730. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34731. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34732. BIGNUM* a = NULL;
  34733. BIGNUM* b = NULL;
  34734. BIGNUM emptyBN;
  34735. /* Setup */
  34736. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34737. ExpectNotNull(a = BN_new());
  34738. ExpectNotNull(b = BN_new());
  34739. /* Invalid parameters. */
  34740. ExpectIntEQ(BN_lshift(NULL, NULL, 1), 0);
  34741. ExpectIntEQ(BN_lshift(&emptyBN, NULL, 1), 0);
  34742. ExpectIntEQ(BN_lshift(NULL, &emptyBN, 1), 0);
  34743. ExpectIntEQ(BN_lshift(b, NULL, 1), 0);
  34744. ExpectIntEQ(BN_lshift(b, &emptyBN, 1), 0);
  34745. ExpectIntEQ(BN_lshift(NULL, a, 1), 0);
  34746. ExpectIntEQ(BN_lshift(&emptyBN, a, 1), 0);
  34747. ExpectIntEQ(BN_lshift(b, a, -1), 0);
  34748. ExpectIntEQ(BN_rshift(NULL, NULL, 1), 0);
  34749. ExpectIntEQ(BN_rshift(&emptyBN, NULL, 1), 0);
  34750. ExpectIntEQ(BN_rshift(NULL, &emptyBN, 1), 0);
  34751. ExpectIntEQ(BN_rshift(b, NULL, 1), 0);
  34752. ExpectIntEQ(BN_rshift(b, &emptyBN, 1), 0);
  34753. ExpectIntEQ(BN_rshift(NULL, a, 1), 0);
  34754. ExpectIntEQ(BN_rshift(&emptyBN, a, 1), 0);
  34755. ExpectIntEQ(BN_rshift(b, a, -1), 0);
  34756. ExpectIntEQ(BN_set_word(a, 1), 1);
  34757. ExpectIntEQ(BN_lshift(b, a, 1), 1);
  34758. ExpectIntEQ(BN_is_word(b, 2), 1);
  34759. ExpectIntEQ(BN_lshift(a, a, 1), 1);
  34760. ExpectIntEQ(BN_is_word(a, 2), 1);
  34761. ExpectIntEQ(BN_rshift(b, a, 1), 1);
  34762. ExpectIntEQ(BN_is_word(b, 1), 1);
  34763. ExpectIntEQ(BN_rshift(a, a, 1), 1);
  34764. ExpectIntEQ(BN_is_word(a, 1), 1);
  34765. BN_free(b);
  34766. BN_free(a);
  34767. #endif
  34768. return EXPECT_RESULT();
  34769. }
  34770. static int test_wolfSSL_BN_math(void)
  34771. {
  34772. EXPECT_DECLS;
  34773. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34774. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34775. BIGNUM* a = NULL;
  34776. BIGNUM* b = NULL;
  34777. BIGNUM* r = NULL;
  34778. BIGNUM* rem = NULL;
  34779. BIGNUM emptyBN;
  34780. BN_ULONG val1;
  34781. BN_ULONG val2;
  34782. /* Setup */
  34783. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34784. ExpectNotNull(a = BN_new());
  34785. ExpectNotNull(b = BN_new());
  34786. ExpectNotNull(r = BN_new());
  34787. ExpectNotNull(rem = BN_new());
  34788. /* Invalid parameters. */
  34789. ExpectIntEQ(BN_add(NULL, NULL, NULL), 0);
  34790. ExpectIntEQ(BN_add(r, NULL, NULL), 0);
  34791. ExpectIntEQ(BN_add(NULL, a, NULL), 0);
  34792. ExpectIntEQ(BN_add(NULL, NULL, b), 0);
  34793. ExpectIntEQ(BN_add(r, a, NULL), 0);
  34794. ExpectIntEQ(BN_add(r, NULL, b), 0);
  34795. ExpectIntEQ(BN_add(NULL, a, b), 0);
  34796. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, &emptyBN), 0);
  34797. ExpectIntEQ(BN_add(r, &emptyBN, &emptyBN), 0);
  34798. ExpectIntEQ(BN_add(&emptyBN, a, &emptyBN), 0);
  34799. ExpectIntEQ(BN_add(&emptyBN, &emptyBN, b), 0);
  34800. ExpectIntEQ(BN_add(r, a, &emptyBN), 0);
  34801. ExpectIntEQ(BN_add(r, &emptyBN, b), 0);
  34802. ExpectIntEQ(BN_add(&emptyBN, a, b), 0);
  34803. ExpectIntEQ(BN_sub(NULL, NULL, NULL), 0);
  34804. ExpectIntEQ(BN_sub(r, NULL, NULL), 0);
  34805. ExpectIntEQ(BN_sub(NULL, a, NULL), 0);
  34806. ExpectIntEQ(BN_sub(NULL, NULL, b), 0);
  34807. ExpectIntEQ(BN_sub(r, a, NULL), 0);
  34808. ExpectIntEQ(BN_sub(r, NULL, b), 0);
  34809. ExpectIntEQ(BN_sub(NULL, a, b), 0);
  34810. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, &emptyBN), 0);
  34811. ExpectIntEQ(BN_sub(r, &emptyBN, &emptyBN), 0);
  34812. ExpectIntEQ(BN_sub(&emptyBN, a, &emptyBN), 0);
  34813. ExpectIntEQ(BN_sub(&emptyBN, &emptyBN, b), 0);
  34814. ExpectIntEQ(BN_sub(r, a, &emptyBN), 0);
  34815. ExpectIntEQ(BN_sub(r, &emptyBN, b), 0);
  34816. ExpectIntEQ(BN_sub(&emptyBN, a, b), 0);
  34817. ExpectIntEQ(BN_mul(NULL, NULL, NULL, NULL), 0);
  34818. ExpectIntEQ(BN_mul(r, NULL, NULL, NULL), 0);
  34819. ExpectIntEQ(BN_mul(NULL, a, NULL, NULL), 0);
  34820. ExpectIntEQ(BN_mul(NULL, NULL, b, NULL), 0);
  34821. ExpectIntEQ(BN_mul(r, a, NULL, NULL), 0);
  34822. ExpectIntEQ(BN_mul(r, NULL, b, NULL), 0);
  34823. ExpectIntEQ(BN_mul(NULL, a, b, NULL), 0);
  34824. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34825. ExpectIntEQ(BN_mul(r, &emptyBN, &emptyBN, NULL), 0);
  34826. ExpectIntEQ(BN_mul(&emptyBN, a, &emptyBN, NULL), 0);
  34827. ExpectIntEQ(BN_mul(&emptyBN, &emptyBN, b, NULL), 0);
  34828. ExpectIntEQ(BN_mul(r, a, &emptyBN, NULL), 0);
  34829. ExpectIntEQ(BN_mul(r, &emptyBN, b, NULL), 0);
  34830. ExpectIntEQ(BN_mul(&emptyBN, a, b, NULL), 0);
  34831. ExpectIntEQ(BN_div(NULL, NULL, NULL, NULL, NULL), 0);
  34832. ExpectIntEQ(BN_div(r, NULL, NULL, NULL, NULL), 0);
  34833. ExpectIntEQ(BN_div(NULL, rem, NULL, NULL, NULL), 0);
  34834. ExpectIntEQ(BN_div(NULL, NULL, a, NULL, NULL), 0);
  34835. ExpectIntEQ(BN_div(NULL, NULL, NULL, b, NULL), 0);
  34836. ExpectIntEQ(BN_div(NULL, rem, a, b, NULL), 0);
  34837. ExpectIntEQ(BN_div(r, NULL, a, b, NULL), 0);
  34838. ExpectIntEQ(BN_div(r, rem, NULL, b, NULL), 0);
  34839. ExpectIntEQ(BN_div(r, rem, a, NULL, NULL), 0);
  34840. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34841. ExpectIntEQ(BN_div(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34842. ExpectIntEQ(BN_div(&emptyBN, rem, &emptyBN, &emptyBN, NULL), 0);
  34843. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, a, &emptyBN, NULL), 0);
  34844. ExpectIntEQ(BN_div(&emptyBN, &emptyBN, &emptyBN, b, NULL), 0);
  34845. ExpectIntEQ(BN_div(&emptyBN, rem, a, b, NULL), 0);
  34846. ExpectIntEQ(BN_div(r, &emptyBN, a, b, NULL), 0);
  34847. ExpectIntEQ(BN_div(r, rem, &emptyBN, b, NULL), 0);
  34848. ExpectIntEQ(BN_div(r, rem, a, &emptyBN, NULL), 0);
  34849. ExpectIntEQ(BN_mod(NULL, NULL, NULL, NULL), 0);
  34850. ExpectIntEQ(BN_mod(r, NULL, NULL, NULL), 0);
  34851. ExpectIntEQ(BN_mod(NULL, a, NULL, NULL), 0);
  34852. ExpectIntEQ(BN_mod(NULL, NULL, b, NULL), 0);
  34853. ExpectIntEQ(BN_mod(r, a, NULL, NULL), 0);
  34854. ExpectIntEQ(BN_mod(r, NULL, b, NULL), 0);
  34855. ExpectIntEQ(BN_mod(NULL, a, b, NULL), 0);
  34856. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34857. ExpectIntEQ(BN_mod(r, &emptyBN, &emptyBN, NULL), 0);
  34858. ExpectIntEQ(BN_mod(&emptyBN, a, &emptyBN, NULL), 0);
  34859. ExpectIntEQ(BN_mod(&emptyBN, &emptyBN, b, NULL), 0);
  34860. ExpectIntEQ(BN_mod(r, a, &emptyBN, NULL), 0);
  34861. ExpectIntEQ(BN_mod(r, &emptyBN, b, NULL), 0);
  34862. ExpectIntEQ(BN_mod(&emptyBN, a, b, NULL), 0);
  34863. /* END Invalid parameters. */
  34864. val1 = 8;
  34865. val2 = 3;
  34866. ExpectIntEQ(BN_set_word(a, val1), 1);
  34867. ExpectIntEQ(BN_set_word(b, val2), 1);
  34868. ExpectIntEQ(BN_add(r, a, b), 1);
  34869. ExpectIntEQ(BN_is_word(r, val1 + val2), 1);
  34870. ExpectIntEQ(BN_sub(r, a, b), 1);
  34871. ExpectIntEQ(BN_is_word(r, val1 - val2), 1);
  34872. ExpectIntEQ(BN_mul(r, a, b, NULL), 1);
  34873. ExpectIntEQ(BN_is_word(r, val1 * val2), 1);
  34874. ExpectIntEQ(BN_div(r, rem, a, b, NULL), 1);
  34875. ExpectIntEQ(BN_is_word(r, val1 / val2), 1);
  34876. ExpectIntEQ(BN_is_word(rem, val1 % val2), 1);
  34877. ExpectIntEQ(BN_mod(r, a, b, NULL), 1);
  34878. ExpectIntEQ(BN_is_word(r, val1 % val2), 1);
  34879. BN_free(rem);
  34880. BN_free(r);
  34881. BN_free(b);
  34882. BN_free(a);
  34883. #endif
  34884. return EXPECT_RESULT();
  34885. }
  34886. static int test_wolfSSL_BN_math_mod(void)
  34887. {
  34888. EXPECT_DECLS;
  34889. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  34890. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  34891. BIGNUM* a = NULL;
  34892. BIGNUM* b = NULL;
  34893. BIGNUM* m = NULL;
  34894. BIGNUM* r = NULL;
  34895. BIGNUM* t = NULL;
  34896. BIGNUM emptyBN;
  34897. BN_ULONG val1;
  34898. BN_ULONG val2;
  34899. BN_ULONG val3;
  34900. /* Setup */
  34901. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  34902. ExpectNotNull(a = BN_new());
  34903. ExpectNotNull(b = BN_new());
  34904. ExpectNotNull(m = BN_new());
  34905. ExpectNotNull(r = BN_new());
  34906. /* Invalid parameters. */
  34907. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, NULL, NULL), 0);
  34908. ExpectIntEQ(BN_mod_add(r, NULL, NULL, NULL, NULL), 0);
  34909. ExpectIntEQ(BN_mod_add(NULL, a, NULL, NULL, NULL), 0);
  34910. ExpectIntEQ(BN_mod_add(NULL, NULL, b, NULL, NULL), 0);
  34911. ExpectIntEQ(BN_mod_add(NULL, NULL, NULL, m, NULL), 0);
  34912. ExpectIntEQ(BN_mod_add(NULL, a, b, m, NULL), 0);
  34913. ExpectIntEQ(BN_mod_add(r, NULL, b, m, NULL), 0);
  34914. ExpectIntEQ(BN_mod_add(r, a, NULL, m, NULL), 0);
  34915. ExpectIntEQ(BN_mod_add(r, a, m, NULL, NULL), 0);
  34916. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34917. ExpectIntEQ(BN_mod_add(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34918. ExpectIntEQ(BN_mod_add(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34919. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34920. ExpectIntEQ(BN_mod_add(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34921. ExpectIntEQ(BN_mod_add(&emptyBN, a, b, m, NULL), 0);
  34922. ExpectIntEQ(BN_mod_add(r, &emptyBN, b, m, NULL), 0);
  34923. ExpectIntEQ(BN_mod_add(r, a, &emptyBN, m, NULL), 0);
  34924. ExpectIntEQ(BN_mod_add(r, a, m, &emptyBN, NULL), 0);
  34925. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, NULL, NULL), 0);
  34926. ExpectIntEQ(BN_mod_mul(r, NULL, NULL, NULL, NULL), 0);
  34927. ExpectIntEQ(BN_mod_mul(NULL, a, NULL, NULL, NULL), 0);
  34928. ExpectIntEQ(BN_mod_mul(NULL, NULL, b, NULL, NULL), 0);
  34929. ExpectIntEQ(BN_mod_mul(NULL, NULL, NULL, m, NULL), 0);
  34930. ExpectIntEQ(BN_mod_mul(NULL, a, b, m, NULL), 0);
  34931. ExpectIntEQ(BN_mod_mul(r, NULL, b, m, NULL), 0);
  34932. ExpectIntEQ(BN_mod_mul(r, a, NULL, m, NULL), 0);
  34933. ExpectIntEQ(BN_mod_mul(r, a, m, NULL, NULL), 0);
  34934. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34935. ExpectIntEQ(BN_mod_mul(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34936. ExpectIntEQ(BN_mod_mul(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34937. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34938. ExpectIntEQ(BN_mod_mul(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34939. ExpectIntEQ(BN_mod_mul(&emptyBN, a, b, m, NULL), 0);
  34940. ExpectIntEQ(BN_mod_mul(r, &emptyBN, b, m, NULL), 0);
  34941. ExpectIntEQ(BN_mod_mul(r, a, &emptyBN, m, NULL), 0);
  34942. ExpectIntEQ(BN_mod_mul(r, a, m, &emptyBN, NULL), 0);
  34943. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, NULL, NULL), 0);
  34944. ExpectIntEQ(BN_mod_exp(r, NULL, NULL, NULL, NULL), 0);
  34945. ExpectIntEQ(BN_mod_exp(NULL, a, NULL, NULL, NULL), 0);
  34946. ExpectIntEQ(BN_mod_exp(NULL, NULL, b, NULL, NULL), 0);
  34947. ExpectIntEQ(BN_mod_exp(NULL, NULL, NULL, m, NULL), 0);
  34948. ExpectIntEQ(BN_mod_exp(NULL, a, b, m, NULL), 0);
  34949. ExpectIntEQ(BN_mod_exp(r, NULL, b, m, NULL), 0);
  34950. ExpectIntEQ(BN_mod_exp(r, a, NULL, m, NULL), 0);
  34951. ExpectIntEQ(BN_mod_exp(r, a, m, NULL, NULL), 0);
  34952. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34953. ExpectIntEQ(BN_mod_exp(r, &emptyBN, &emptyBN, &emptyBN, NULL), 0);
  34954. ExpectIntEQ(BN_mod_exp(&emptyBN, a, &emptyBN, &emptyBN, NULL), 0);
  34955. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, b, &emptyBN, NULL), 0);
  34956. ExpectIntEQ(BN_mod_exp(&emptyBN, &emptyBN, &emptyBN, m, NULL), 0);
  34957. ExpectIntEQ(BN_mod_exp(&emptyBN, a, b, m, NULL), 0);
  34958. ExpectIntEQ(BN_mod_exp(r, &emptyBN, b, m, NULL), 0);
  34959. ExpectIntEQ(BN_mod_exp(r, a, &emptyBN, m, NULL), 0);
  34960. ExpectIntEQ(BN_mod_exp(r, a, m, &emptyBN, NULL), 0);
  34961. ExpectNull(BN_mod_inverse(r, NULL, NULL, NULL));
  34962. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  34963. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  34964. ExpectNull(BN_mod_inverse(r, NULL, m, NULL));
  34965. ExpectNull(BN_mod_inverse(r, a, NULL, NULL));
  34966. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, &emptyBN, NULL));
  34967. ExpectNull(BN_mod_inverse(r, &emptyBN, &emptyBN, NULL));
  34968. ExpectNull(BN_mod_inverse(&emptyBN, a, &emptyBN, NULL));
  34969. ExpectNull(BN_mod_inverse(&emptyBN, &emptyBN, m, NULL));
  34970. ExpectNull(BN_mod_inverse(&emptyBN, a, m, NULL));
  34971. ExpectNull(BN_mod_inverse(r, &emptyBN, m, NULL));
  34972. ExpectNull(BN_mod_inverse(r, a, &emptyBN, NULL));
  34973. /* END Invalid parameters. */
  34974. val1 = 9;
  34975. val2 = 13;
  34976. val3 = 5;
  34977. ExpectIntEQ(BN_set_word(a, val1), 1);
  34978. ExpectIntEQ(BN_set_word(b, val2), 1);
  34979. ExpectIntEQ(BN_set_word(m, val3), 1);
  34980. ExpectIntEQ(BN_mod_add(r, a, b, m, NULL), 1);
  34981. ExpectIntEQ(BN_is_word(r, (val1 + val2) % val3), 1);
  34982. ExpectIntEQ(BN_mod_mul(r, a, b, m, NULL), 1);
  34983. ExpectIntEQ(BN_is_word(r, (val1 * val2) % val3), 1);
  34984. ExpectIntEQ(BN_set_word(a, 2), 1);
  34985. ExpectIntEQ(BN_set_word(b, 3), 1);
  34986. ExpectIntEQ(BN_set_word(m, 5), 1);
  34987. /* (2 ^ 3) % 5 = 8 % 5 = 3 */
  34988. ExpectIntEQ(BN_mod_exp(r, a, b, m, NULL), 1);
  34989. ExpectIntEQ(BN_is_word(r, 3), 1);
  34990. /* (2 * 3) % 5 = 6 % 5 = 1 => inv = 3 */
  34991. ExpectNotNull(BN_mod_inverse(r, a, m, NULL));
  34992. ExpectIntEQ(BN_is_word(r, 3), 1);
  34993. ExpectNotNull(t = BN_mod_inverse(NULL, a, m, NULL));
  34994. ExpectIntEQ(BN_is_word(t, 3), 1);
  34995. BN_free(t);
  34996. /* No inverse case. No inverse when a divides b. */
  34997. ExpectIntEQ(BN_set_word(a, 3), 1);
  34998. ExpectIntEQ(BN_set_word(m, 9), 1);
  34999. ExpectNull(BN_mod_inverse(r, a, m, NULL));
  35000. BN_free(r);
  35001. BN_free(m);
  35002. BN_free(b);
  35003. BN_free(a);
  35004. #endif
  35005. return EXPECT_RESULT();
  35006. }
  35007. static int test_wolfSSL_BN_math_other(void)
  35008. {
  35009. EXPECT_DECLS;
  35010. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  35011. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  35012. #if !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  35013. BIGNUM* a = NULL;
  35014. BIGNUM* b = NULL;
  35015. BIGNUM* r = NULL;
  35016. BIGNUM emptyBN;
  35017. /* Setup */
  35018. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35019. ExpectNotNull(a = BN_new());
  35020. ExpectNotNull(b = BN_new());
  35021. ExpectNotNull(r = BN_new());
  35022. /* Invalid parameters. */
  35023. ExpectIntEQ(BN_gcd(NULL, NULL, NULL, NULL), 0);
  35024. ExpectIntEQ(BN_gcd(r, NULL, NULL, NULL), 0);
  35025. ExpectIntEQ(BN_gcd(NULL, a, NULL, NULL), 0);
  35026. ExpectIntEQ(BN_gcd(NULL, NULL, b, NULL), 0);
  35027. ExpectIntEQ(BN_gcd(NULL, a, b, NULL), 0);
  35028. ExpectIntEQ(BN_gcd(r, NULL, b, NULL), 0);
  35029. ExpectIntEQ(BN_gcd(r, a, NULL, NULL), 0);
  35030. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, &emptyBN, NULL), 0);
  35031. ExpectIntEQ(BN_gcd(r, &emptyBN, &emptyBN, NULL), 0);
  35032. ExpectIntEQ(BN_gcd(&emptyBN, a, &emptyBN, NULL), 0);
  35033. ExpectIntEQ(BN_gcd(&emptyBN, &emptyBN, b, NULL), 0);
  35034. ExpectIntEQ(BN_gcd(&emptyBN, a, b, NULL), 0);
  35035. ExpectIntEQ(BN_gcd(r, &emptyBN, b, NULL), 0);
  35036. ExpectIntEQ(BN_gcd(r, a, &emptyBN, NULL), 0);
  35037. /* END Invalid parameters. */
  35038. /* No common factors between 2 and 3. */
  35039. ExpectIntEQ(BN_set_word(a, 2), 1);
  35040. ExpectIntEQ(BN_set_word(b, 3), 1);
  35041. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  35042. ExpectIntEQ(BN_is_word(r, 1), 1);
  35043. /* 3 is largest value that divides both 6 and 9. */
  35044. ExpectIntEQ(BN_set_word(a, 6), 1);
  35045. ExpectIntEQ(BN_set_word(b, 9), 1);
  35046. ExpectIntEQ(BN_gcd(r, a, b, NULL), 1);
  35047. ExpectIntEQ(BN_is_word(r, 3), 1);
  35048. /* GCD of 0 and 0 is undefined. */
  35049. ExpectIntEQ(BN_set_word(a, 0), 1);
  35050. ExpectIntEQ(BN_set_word(b, 0), 1);
  35051. ExpectIntEQ(BN_gcd(r, a, b, NULL), 0);
  35052. /* Teardown */
  35053. BN_free(r);
  35054. BN_free(b);
  35055. BN_free(a);
  35056. #endif
  35057. #endif
  35058. return EXPECT_RESULT();
  35059. }
  35060. static int test_wolfSSL_BN_rand(void)
  35061. {
  35062. EXPECT_DECLS;
  35063. #if defined(OPENSSL_EXTRA) && !defined(OPENSSL_EXTRA_NO_BN)
  35064. BIGNUM* bn = NULL;
  35065. BIGNUM* range = NULL;
  35066. BIGNUM emptyBN;
  35067. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35068. ExpectNotNull(bn = BN_new());
  35069. ExpectNotNull(range = BN_new());
  35070. /* Invalid parameters. */
  35071. ExpectIntEQ(BN_rand(NULL, -1, 0, 0), 0);
  35072. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  35073. ExpectIntEQ(BN_rand(NULL, 1, 0, 0), 0);
  35074. ExpectIntEQ(BN_rand(&emptyBN, -1, 0, 0), 0);
  35075. ExpectIntEQ(BN_rand(bn, -1, 0, 0), 0);
  35076. ExpectIntEQ(BN_rand(&emptyBN, 1, 0, 0), 0);
  35077. ExpectIntEQ(BN_pseudo_rand(NULL, -1, 0, 0), 0);
  35078. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  35079. ExpectIntEQ(BN_pseudo_rand(NULL, 1, 0, 0), 0);
  35080. ExpectIntEQ(BN_pseudo_rand(&emptyBN, -1, 0, 0), 0);
  35081. ExpectIntEQ(BN_pseudo_rand(bn, -1, 0, 0), 0);
  35082. ExpectIntEQ(BN_pseudo_rand(&emptyBN, 1, 0, 0), 0);
  35083. ExpectIntEQ(BN_rand_range(NULL, NULL), 0);
  35084. ExpectIntEQ(BN_rand_range(bn, NULL), 0);
  35085. ExpectIntEQ(BN_rand_range(NULL, range), 0);
  35086. ExpectIntEQ(BN_rand_range(&emptyBN, &emptyBN), 0);
  35087. ExpectIntEQ(BN_rand_range(bn, &emptyBN), 0);
  35088. ExpectIntEQ(BN_rand_range(&emptyBN, range), 0);
  35089. /* 0 bit random value must be 0 and so cannot set bit in any position. */
  35090. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  35091. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35092. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35093. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35094. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35095. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35096. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  35097. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35098. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35099. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35100. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  35101. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35102. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35103. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35104. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35105. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35106. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ONE,
  35107. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35108. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_TWO,
  35109. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35110. /* 1 bit random value must have no more than one top bit set. */
  35111. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35112. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35113. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35114. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35115. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35116. WOLFSSL_BN_RAND_BOTTOM_ANY), 0);
  35117. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_TWO,
  35118. WOLFSSL_BN_RAND_BOTTOM_ODD), 0);
  35119. /* END Invalid parameters. */
  35120. /* 0 bit random: 0. */
  35121. ExpectIntEQ(BN_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35122. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35123. ExpectIntEQ(BN_is_zero(bn), 1);
  35124. ExpectIntEQ(BN_set_word(bn, 2), 1); /* Make sure not zero. */
  35125. ExpectIntEQ(BN_pseudo_rand(bn, 0, WOLFSSL_BN_RAND_TOP_ANY,
  35126. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35127. ExpectIntEQ(BN_is_zero(bn), 1);
  35128. /* 1 bit random: 0 or 1. */
  35129. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  35130. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35131. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  35132. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35133. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35134. ExpectIntEQ(BN_get_word(bn), 1);
  35135. ExpectIntEQ(BN_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35136. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35137. ExpectIntEQ(BN_get_word(bn), 1);
  35138. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ANY,
  35139. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35140. ExpectIntLT(BN_get_word(bn), 2); /* Make sure valid range. */
  35141. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35142. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35143. ExpectIntEQ(BN_get_word(bn), 1);
  35144. ExpectIntEQ(BN_pseudo_rand(bn, 1, WOLFSSL_BN_RAND_TOP_ONE,
  35145. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35146. ExpectIntEQ(BN_get_word(bn), 1);
  35147. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35148. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35149. ExpectIntEQ(BN_num_bits(bn), 8);
  35150. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35151. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35152. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35153. ExpectIntEQ(BN_num_bits(bn), 8);
  35154. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35155. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  35156. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35157. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35158. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  35159. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_TWO,
  35160. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35161. ExpectIntEQ(BN_is_bit_set(bn, 7), 1);
  35162. ExpectIntEQ(BN_is_bit_set(bn, 6), 1);
  35163. ExpectIntEQ(BN_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35164. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35165. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  35166. ExpectIntEQ(BN_pseudo_rand(bn, 8, WOLFSSL_BN_RAND_TOP_ONE,
  35167. WOLFSSL_BN_RAND_BOTTOM_ODD), 1);
  35168. ExpectIntEQ(BN_is_bit_set(bn, 0), 1);
  35169. /* Regression test: Older versions of wolfSSL_BN_rand would round the
  35170. * requested number of bits up to the nearest multiple of 8. E.g. in this
  35171. * case, requesting a 13-bit random number would actually return a 16-bit
  35172. * random number. */
  35173. ExpectIntEQ(BN_rand(bn, 13, WOLFSSL_BN_RAND_TOP_ONE,
  35174. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35175. ExpectIntEQ(BN_num_bits(bn), 13);
  35176. ExpectIntEQ(BN_rand(range, 64, WOLFSSL_BN_RAND_TOP_ONE,
  35177. WOLFSSL_BN_RAND_BOTTOM_ANY), 1);
  35178. ExpectIntEQ(BN_rand_range(bn, range), 1);
  35179. ExpectIntEQ(BN_set_word(range, 0), 1);
  35180. ExpectIntEQ(BN_rand_range(bn, range), 1);
  35181. ExpectIntEQ(BN_set_word(range, 1), 1);
  35182. ExpectIntEQ(BN_rand_range(bn, range), 1);
  35183. BN_free(bn);
  35184. BN_free(range);
  35185. #endif
  35186. return EXPECT_RESULT();
  35187. }
  35188. static int test_wolfSSL_BN_prime(void)
  35189. {
  35190. EXPECT_DECLS;
  35191. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN) && \
  35192. !defined(OPENSSL_EXTRA_NO_BN) && !defined(WOLFSSL_SP_MATH)
  35193. #if defined(WOLFSSL_KEY_GEN) && (!defined(NO_RSA) || !defined(NO_DH) || !defined(NO_DSA))
  35194. BIGNUM* a = NULL;
  35195. BIGNUM* add = NULL;
  35196. BIGNUM* rem = NULL;
  35197. BIGNUM emptyBN;
  35198. XMEMSET(&emptyBN, 0, sizeof(emptyBN));
  35199. ExpectNotNull(a = BN_new());
  35200. ExpectNotNull(add = BN_new());
  35201. ExpectNotNull(rem = BN_new());
  35202. /* Invalid parameters. */
  35203. /* BN_generate_prime_ex()
  35204. * prime - must have valid BIGNUM
  35205. * bits - Greater then 0
  35206. * safe - not supported, must be 0
  35207. * add - not supported, must be NULL
  35208. * rem - not supported, must be NULL
  35209. * cb - anything
  35210. */
  35211. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, rem, NULL), 0);
  35212. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, rem, NULL), 0);
  35213. ExpectIntEQ(BN_generate_prime_ex(a, -1, 1, add, rem, NULL), 0);
  35214. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 1, add, rem, NULL), 0);
  35215. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 1, add, rem, NULL), 0);
  35216. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 0, add, rem, NULL), 0);
  35217. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 0, add, rem, NULL), 0);
  35218. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, NULL, rem, NULL), 0);
  35219. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, NULL, rem, NULL), 0);
  35220. ExpectIntEQ(BN_generate_prime_ex(NULL, -1, 1, add, NULL, NULL), 0);
  35221. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, -1, 1, add, NULL, NULL), 0);
  35222. ExpectIntEQ(BN_generate_prime_ex(NULL, 2, 0, NULL, NULL, NULL), 0);
  35223. ExpectIntEQ(BN_generate_prime_ex(&emptyBN, 2, 0, NULL, NULL, NULL), 0);
  35224. ExpectIntEQ(BN_generate_prime_ex(a, -1, 0, NULL, NULL, NULL), 0);
  35225. ExpectIntEQ(BN_generate_prime_ex(a, 0, 0, NULL, NULL, NULL), 0);
  35226. ExpectIntEQ(BN_generate_prime_ex(a, 2, 1, NULL, NULL, NULL), 0);
  35227. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, add, NULL, NULL), 0);
  35228. ExpectIntEQ(BN_generate_prime_ex(a, 2, 0, NULL, rem, NULL), 0);
  35229. ExpectIntEQ(BN_is_prime_ex(NULL, -1, NULL, NULL), -1);
  35230. ExpectIntEQ(BN_is_prime_ex(&emptyBN, -1, NULL, NULL), -1);
  35231. ExpectIntEQ(BN_is_prime_ex(a, -1, NULL, NULL), -1);
  35232. ExpectIntEQ(BN_is_prime_ex(a, 2048, NULL, NULL), -1);
  35233. ExpectIntEQ(BN_is_prime_ex(NULL, 1, NULL, NULL), -1);
  35234. ExpectIntEQ(BN_is_prime_ex(&emptyBN, 1, NULL, NULL), -1);
  35235. /* END Invalid parameters. */
  35236. ExpectIntEQ(BN_generate_prime_ex(a, 512, 0, NULL, NULL, NULL), 1);
  35237. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 1);
  35238. ExpectIntEQ(BN_clear_bit(a, 0), 1);
  35239. ExpectIntEQ(BN_is_prime_ex(a, 8, NULL, NULL), 0);
  35240. BN_free(rem);
  35241. BN_free(add);
  35242. BN_free(a);
  35243. #endif
  35244. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_ASN) */
  35245. return EXPECT_RESULT();
  35246. }
  35247. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35248. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35249. #define TEST_ARG 0x1234
  35250. static void msg_cb(int write_p, int version, int content_type,
  35251. const void *buf, size_t len, SSL *ssl, void *arg)
  35252. {
  35253. (void)write_p;
  35254. (void)version;
  35255. (void)content_type;
  35256. (void)buf;
  35257. (void)len;
  35258. (void)ssl;
  35259. AssertTrue(arg == (void*)TEST_ARG);
  35260. }
  35261. #endif
  35262. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  35263. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35264. #if defined(SESSION_CERTS)
  35265. #include "wolfssl/internal.h"
  35266. #endif
  35267. static int msgCb(SSL_CTX *ctx, SSL *ssl)
  35268. {
  35269. EXPECT_DECLS;
  35270. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  35271. STACK_OF(X509)* sk = NULL;
  35272. X509* x509 = NULL;
  35273. int i, num;
  35274. BIO* bio = NULL;
  35275. #endif
  35276. ExpectNotNull(ctx);
  35277. ExpectNotNull(ssl);
  35278. fprintf(stderr, "\n===== msgcb called ====\n");
  35279. #if defined(SESSION_CERTS) && defined(TEST_PEER_CERT_CHAIN)
  35280. ExpectTrue(SSL_get_peer_cert_chain(ssl) != NULL);
  35281. ExpectIntEQ(((WOLFSSL_X509_CHAIN *)SSL_get_peer_cert_chain(ssl))->count, 2);
  35282. ExpectNotNull(SSL_get0_verified_chain(ssl));
  35283. #endif
  35284. #if defined(OPENSSL_ALL) && defined(SESSION_CERTS) && !defined(NO_BIO)
  35285. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  35286. ExpectNotNull(sk = SSL_get_peer_cert_chain(ssl));
  35287. if (sk == NULL) {
  35288. BIO_free(bio);
  35289. return TEST_FAIL;
  35290. }
  35291. num = sk_X509_num(sk);
  35292. ExpectTrue(num > 0);
  35293. for (i = 0; i < num; i++) {
  35294. ExpectNotNull(x509 = sk_X509_value(sk,i));
  35295. if (x509 == NULL)
  35296. break;
  35297. fprintf(stderr, "Certificate at index [%d] = :\n",i);
  35298. X509_print(bio,x509);
  35299. fprintf(stderr, "\n\n");
  35300. }
  35301. BIO_free(bio);
  35302. #endif
  35303. return EXPECT_RESULT();
  35304. }
  35305. #endif
  35306. static int test_wolfSSL_msgCb(void)
  35307. {
  35308. EXPECT_DECLS;
  35309. #if defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL) && \
  35310. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35311. test_ssl_cbf client_cb;
  35312. test_ssl_cbf server_cb;
  35313. XMEMSET(&client_cb, 0, sizeof(client_cb));
  35314. XMEMSET(&server_cb, 0, sizeof(server_cb));
  35315. #ifndef WOLFSSL_NO_TLS12
  35316. client_cb.method = wolfTLSv1_2_client_method;
  35317. server_cb.method = wolfTLSv1_2_server_method;
  35318. #else
  35319. client_cb.method = wolfTLSv1_3_client_method;
  35320. server_cb.method = wolfTLSv1_3_server_method;
  35321. #endif
  35322. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  35323. &server_cb, msgCb), TEST_SUCCESS);
  35324. #endif
  35325. return EXPECT_RESULT();
  35326. }
  35327. static int test_wolfSSL_either_side(void)
  35328. {
  35329. EXPECT_DECLS;
  35330. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  35331. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  35332. test_ssl_cbf client_cb;
  35333. test_ssl_cbf server_cb;
  35334. XMEMSET(&client_cb, 0, sizeof(client_cb));
  35335. XMEMSET(&server_cb, 0, sizeof(server_cb));
  35336. /* Use different CTX for client and server */
  35337. client_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  35338. ExpectNotNull(client_cb.ctx);
  35339. server_cb.ctx = wolfSSL_CTX_new(wolfSSLv23_method());
  35340. ExpectNotNull(server_cb.ctx);
  35341. /* we are responsible for free'ing WOLFSSL_CTX */
  35342. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  35343. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  35344. &server_cb, NULL), TEST_SUCCESS);
  35345. wolfSSL_CTX_free(client_cb.ctx);
  35346. wolfSSL_CTX_free(server_cb.ctx);
  35347. #endif
  35348. return EXPECT_RESULT();
  35349. }
  35350. static int test_wolfSSL_DTLS_either_side(void)
  35351. {
  35352. EXPECT_DECLS;
  35353. #if (defined(OPENSSL_EXTRA) || defined(WOLFSSL_EITHER_SIDE)) && \
  35354. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  35355. test_ssl_cbf client_cb;
  35356. test_ssl_cbf server_cb;
  35357. XMEMSET(&client_cb, 0, sizeof(client_cb));
  35358. XMEMSET(&server_cb, 0, sizeof(server_cb));
  35359. /* Use different CTX for client and server */
  35360. client_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  35361. ExpectNotNull(client_cb.ctx);
  35362. server_cb.ctx = wolfSSL_CTX_new(wolfDTLS_method());
  35363. ExpectNotNull(server_cb.ctx);
  35364. /* we are responsible for free'ing WOLFSSL_CTX */
  35365. server_cb.isSharedCtx = client_cb.isSharedCtx = 1;
  35366. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cb,
  35367. &server_cb, NULL), TEST_SUCCESS);
  35368. wolfSSL_CTX_free(client_cb.ctx);
  35369. wolfSSL_CTX_free(server_cb.ctx);
  35370. #endif
  35371. return EXPECT_RESULT();
  35372. }
  35373. static int test_generate_cookie(void)
  35374. {
  35375. EXPECT_DECLS;
  35376. #if defined(WOLFSSL_DTLS) && defined(OPENSSL_EXTRA) && defined(USE_WOLFSSL_IO)
  35377. SSL_CTX* ctx = NULL;
  35378. SSL* ssl = NULL;
  35379. byte buf[FOURK_BUF] = {0};
  35380. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLS_method()));
  35381. ExpectNotNull(ssl = SSL_new(ctx));
  35382. /* Test unconnected */
  35383. ExpectIntEQ(EmbedGenerateCookie(ssl, buf, FOURK_BUF, NULL), GEN_COOKIE_E);
  35384. wolfSSL_CTX_SetGenCookie(ctx, EmbedGenerateCookie);
  35385. wolfSSL_SetCookieCtx(ssl, ctx);
  35386. ExpectNotNull(wolfSSL_GetCookieCtx(ssl));
  35387. ExpectNull(wolfSSL_GetCookieCtx(NULL));
  35388. SSL_free(ssl);
  35389. SSL_CTX_free(ctx);
  35390. #endif
  35391. return EXPECT_RESULT();
  35392. }
  35393. static int test_wolfSSL_set_options(void)
  35394. {
  35395. EXPECT_DECLS;
  35396. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35397. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35398. WOLFSSL* ssl = NULL;
  35399. WOLFSSL_CTX* ctx = NULL;
  35400. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  35401. char appData[] = "extra msg";
  35402. #endif
  35403. #ifdef OPENSSL_EXTRA
  35404. unsigned char protos[] = {
  35405. 7, 't', 'l', 's', '/', '1', '.', '2',
  35406. 8, 'h', 't', 't', 'p', '/', '1', '.', '1'
  35407. };
  35408. unsigned int len = sizeof(protos);
  35409. void *arg = (void *)TEST_ARG;
  35410. #endif
  35411. #ifndef NO_WOLFSSL_SERVER
  35412. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  35413. #else
  35414. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35415. #endif
  35416. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  35417. WOLFSSL_FILETYPE_PEM));
  35418. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  35419. WOLFSSL_FILETYPE_PEM));
  35420. ExpectTrue(wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1)
  35421. == WOLFSSL_OP_NO_TLSv1);
  35422. ExpectTrue(wolfSSL_CTX_get_options(ctx) == WOLFSSL_OP_NO_TLSv1);
  35423. ExpectIntGT((int)wolfSSL_CTX_set_options(ctx, (WOLFSSL_OP_COOKIE_EXCHANGE |
  35424. WOLFSSL_OP_NO_SSLv2)), 0);
  35425. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_COOKIE_EXCHANGE) &
  35426. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  35427. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_TLSv1_2) &
  35428. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  35429. ExpectTrue((wolfSSL_CTX_set_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  35430. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  35431. ExpectFalse((wolfSSL_CTX_clear_options(ctx, WOLFSSL_OP_NO_COMPRESSION) &
  35432. WOLFSSL_OP_NO_COMPRESSION));
  35433. wolfSSL_CTX_free(ctx);
  35434. ctx = NULL;
  35435. #ifndef NO_WOLFSSL_SERVER
  35436. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  35437. #else
  35438. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  35439. #endif
  35440. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  35441. WOLFSSL_FILETYPE_PEM));
  35442. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  35443. WOLFSSL_FILETYPE_PEM));
  35444. #ifdef OPENSSL_EXTRA
  35445. ExpectTrue(wolfSSL_CTX_set_msg_callback(ctx, msg_cb) == WOLFSSL_SUCCESS);
  35446. #endif
  35447. ExpectNotNull(ssl = wolfSSL_new(ctx));
  35448. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL)
  35449. #ifdef HAVE_EX_DATA
  35450. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_SUCCESS);
  35451. ExpectNotNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  35452. if (ssl != NULL) {
  35453. ExpectIntEQ(XMEMCMP(wolfSSL_get_app_data((const WOLFSSL*)ssl),
  35454. appData, sizeof(appData)), 0);
  35455. }
  35456. #else
  35457. ExpectIntEQ(wolfSSL_set_app_data(ssl, (void*)appData), WOLFSSL_FAILURE);
  35458. ExpectNull(wolfSSL_get_app_data((const WOLFSSL*)ssl));
  35459. #endif
  35460. #endif
  35461. ExpectTrue(wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1) ==
  35462. WOLFSSL_OP_NO_TLSv1);
  35463. ExpectTrue(wolfSSL_get_options(ssl) == WOLFSSL_OP_NO_TLSv1);
  35464. ExpectIntGT((int)wolfSSL_set_options(ssl, (WOLFSSL_OP_COOKIE_EXCHANGE |
  35465. WOLFSSL_OP_NO_SSLv2)), 0);
  35466. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_COOKIE_EXCHANGE) &
  35467. WOLFSSL_OP_COOKIE_EXCHANGE) == WOLFSSL_OP_COOKIE_EXCHANGE);
  35468. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_TLSv1_2) &
  35469. WOLFSSL_OP_NO_TLSv1_2) == WOLFSSL_OP_NO_TLSv1_2);
  35470. ExpectTrue((wolfSSL_set_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  35471. WOLFSSL_OP_NO_COMPRESSION) == WOLFSSL_OP_NO_COMPRESSION);
  35472. #ifdef OPENSSL_EXTRA
  35473. ExpectFalse((wolfSSL_clear_options(ssl, WOLFSSL_OP_NO_COMPRESSION) &
  35474. WOLFSSL_OP_NO_COMPRESSION));
  35475. #endif
  35476. #ifdef OPENSSL_EXTRA
  35477. ExpectTrue(wolfSSL_set_msg_callback(ssl, msg_cb) == WOLFSSL_SUCCESS);
  35478. wolfSSL_set_msg_callback_arg(ssl, arg);
  35479. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  35480. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == 0);
  35481. #else
  35482. ExpectTrue(wolfSSL_CTX_set_alpn_protos(ctx, protos, len) == WOLFSSL_SUCCESS);
  35483. #endif
  35484. #endif
  35485. #if defined(WOLFSSL_NGINX) || defined(WOLFSSL_HAPROXY) || \
  35486. defined(WOLFSSL_MYSQL_COMPATIBLE) || defined(OPENSSL_ALL) || \
  35487. defined(HAVE_LIGHTY) || defined(HAVE_STUNNEL)
  35488. #if defined(HAVE_ALPN) && !defined(NO_BIO)
  35489. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  35490. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == 0);
  35491. #else
  35492. ExpectTrue(wolfSSL_set_alpn_protos(ssl, protos, len) == WOLFSSL_SUCCESS);
  35493. #endif
  35494. #endif /* HAVE_ALPN && !NO_BIO */
  35495. #endif
  35496. wolfSSL_free(ssl);
  35497. wolfSSL_CTX_free(ctx);
  35498. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35499. #endif /* !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35500. return EXPECT_RESULT();
  35501. }
  35502. static int test_wolfSSL_sk_SSL_CIPHER(void)
  35503. {
  35504. EXPECT_DECLS;
  35505. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  35506. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35507. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35508. SSL* ssl = NULL;
  35509. SSL_CTX* ctx = NULL;
  35510. STACK_OF(SSL_CIPHER) *sk = NULL;
  35511. STACK_OF(SSL_CIPHER) *dupSk = NULL;
  35512. #ifndef NO_WOLFSSL_SERVER
  35513. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35514. #else
  35515. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35516. #endif
  35517. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile, SSL_FILETYPE_PEM));
  35518. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35519. ExpectNotNull(ssl = SSL_new(ctx));
  35520. ExpectNotNull(sk = SSL_get_ciphers(ssl));
  35521. ExpectNotNull(dupSk = sk_SSL_CIPHER_dup(sk));
  35522. ExpectIntGT(sk_SSL_CIPHER_num(sk), 0);
  35523. ExpectIntEQ(sk_SSL_CIPHER_num(sk), sk_SSL_CIPHER_num(dupSk));
  35524. /* error case because connection has not been established yet */
  35525. ExpectIntEQ(sk_SSL_CIPHER_find(sk, SSL_get_current_cipher(ssl)), -1);
  35526. sk_SSL_CIPHER_free(dupSk);
  35527. /* sk is pointer to internal struct that should be free'd in SSL_free */
  35528. SSL_free(ssl);
  35529. SSL_CTX_free(ctx);
  35530. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35531. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35532. !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35533. return EXPECT_RESULT();
  35534. }
  35535. static int test_wolfSSL_set1_curves_list(void)
  35536. {
  35537. EXPECT_DECLS;
  35538. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  35539. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35540. SSL* ssl = NULL;
  35541. SSL_CTX* ctx = NULL;
  35542. #ifndef NO_WOLFSSL_SERVER
  35543. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35544. #else
  35545. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35546. #endif
  35547. ExpectTrue(SSL_CTX_use_certificate_file(ctx, eccCertFile,
  35548. SSL_FILETYPE_PEM));
  35549. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, eccKeyFile, SSL_FILETYPE_PEM));
  35550. ExpectNotNull(ssl = SSL_new(ctx));
  35551. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, NULL), WOLFSSL_FAILURE);
  35552. #ifdef HAVE_ECC
  35553. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-25X"), WOLFSSL_FAILURE);
  35554. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "P-256"), WOLFSSL_SUCCESS);
  35555. #endif
  35556. #ifdef HAVE_CURVE25519
  35557. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_SUCCESS);
  35558. #else
  35559. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X25519"), WOLFSSL_FAILURE);
  35560. #endif
  35561. #ifdef HAVE_CURVE448
  35562. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_SUCCESS);
  35563. #else
  35564. ExpectIntEQ(SSL_CTX_set1_curves_list(ctx, "X448"), WOLFSSL_FAILURE);
  35565. #endif
  35566. ExpectIntEQ(SSL_set1_curves_list(ssl, NULL), WOLFSSL_FAILURE);
  35567. #ifdef HAVE_ECC
  35568. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-25X"), WOLFSSL_FAILURE);
  35569. ExpectIntEQ(SSL_set1_curves_list(ssl, "P-256"), WOLFSSL_SUCCESS);
  35570. #endif
  35571. #ifdef HAVE_CURVE25519
  35572. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_SUCCESS);
  35573. #else
  35574. ExpectIntEQ(SSL_set1_curves_list(ssl, "X25519"), WOLFSSL_FAILURE);
  35575. #endif
  35576. #ifdef HAVE_CURVE448
  35577. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_SUCCESS);
  35578. #else
  35579. ExpectIntEQ(SSL_set1_curves_list(ssl, "X448"), WOLFSSL_FAILURE);
  35580. #endif
  35581. SSL_free(ssl);
  35582. SSL_CTX_free(ctx);
  35583. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35584. #endif
  35585. return EXPECT_RESULT();
  35586. }
  35587. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  35588. (defined(OPENSSL_EXTRA) || defined(HAVE_CURL)) && defined(HAVE_ECC)
  35589. static int test_wolfSSL_curves_mismatch_ctx_ready(WOLFSSL_CTX* ctx)
  35590. {
  35591. static int counter = 0;
  35592. EXPECT_DECLS;
  35593. if (counter % 2) {
  35594. ExpectIntEQ(wolfSSL_CTX_set1_curves_list(ctx, "P-256"),
  35595. WOLFSSL_SUCCESS);
  35596. }
  35597. else {
  35598. ExpectIntEQ(wolfSSL_CTX_set1_curves_list(ctx, "P-384"),
  35599. WOLFSSL_SUCCESS);
  35600. }
  35601. /* Ciphersuites that require curves */
  35602. wolfSSL_CTX_set_cipher_list(ctx, "TLS13-AES256-GCM-SHA384:"
  35603. "TLS13-CHACHA20-POLY1305-SHA256:TLS13-AES128-GCM-SHA256:"
  35604. "ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:"
  35605. "ECDHE-ECDSA-AES128-GCM-SHA256:"
  35606. "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-CHACHA20-POLY1305:"
  35607. "ECDHE-ECDSA-CHACHA20-POLY1305");
  35608. counter++;
  35609. return EXPECT_RESULT();
  35610. }
  35611. #endif
  35612. static int test_wolfSSL_curves_mismatch(void)
  35613. {
  35614. EXPECT_DECLS;
  35615. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  35616. (defined(OPENSSL_EXTRA) || defined(HAVE_CURL)) && defined(HAVE_ECC)
  35617. test_ssl_cbf func_cb_client;
  35618. test_ssl_cbf func_cb_server;
  35619. size_t i;
  35620. struct {
  35621. method_provider client_meth;
  35622. method_provider server_meth;
  35623. const char* desc;
  35624. int client_last_err;
  35625. int server_last_err;
  35626. } test_params[] = {
  35627. #ifdef WOLFSSL_TLS13
  35628. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLS 1.3",
  35629. FATAL_ERROR, BAD_KEY_SHARE_DATA},
  35630. #endif
  35631. #ifndef WOLFSSL_NO_TLS12
  35632. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLS 1.2",
  35633. FATAL_ERROR, MATCH_SUITE_ERROR},
  35634. #endif
  35635. #ifndef NO_OLD_TLS
  35636. {wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLS 1.1",
  35637. FATAL_ERROR, MATCH_SUITE_ERROR},
  35638. #endif
  35639. };
  35640. for (i = 0; i < XELEM_CNT(test_params) && !EXPECT_FAIL(); i++) {
  35641. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  35642. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  35643. printf("\tTesting with %s...\n", test_params[i].desc);
  35644. func_cb_client.ctx_ready = &test_wolfSSL_curves_mismatch_ctx_ready;
  35645. func_cb_server.ctx_ready = &test_wolfSSL_curves_mismatch_ctx_ready;
  35646. func_cb_client.method = test_params[i].client_meth;
  35647. func_cb_server.method = test_params[i].server_meth;
  35648. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  35649. &func_cb_server, NULL), TEST_FAIL);
  35650. ExpectIntEQ(func_cb_client.last_err, test_params[i].client_last_err);
  35651. ExpectIntEQ(func_cb_server.last_err, test_params[i].server_last_err);
  35652. if (!EXPECT_SUCCESS())
  35653. break;
  35654. printf("\t%s passed\n", test_params[i].desc);
  35655. }
  35656. #endif
  35657. return EXPECT_RESULT();
  35658. }
  35659. static int test_wolfSSL_set1_sigalgs_list(void)
  35660. {
  35661. EXPECT_DECLS;
  35662. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  35663. #if !defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)
  35664. SSL* ssl = NULL;
  35665. SSL_CTX* ctx = NULL;
  35666. #ifndef NO_WOLFSSL_SERVER
  35667. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35668. #else
  35669. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  35670. #endif
  35671. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  35672. SSL_FILETYPE_PEM));
  35673. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35674. ExpectNotNull(ssl = SSL_new(ctx));
  35675. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  35676. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, NULL), WOLFSSL_FAILURE);
  35677. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, NULL), WOLFSSL_FAILURE);
  35678. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, NULL), WOLFSSL_FAILURE);
  35679. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, ""), WOLFSSL_FAILURE);
  35680. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, ""), WOLFSSL_FAILURE);
  35681. #ifndef NO_RSA
  35682. #ifndef NO_SHA256
  35683. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(NULL, "RSA+SHA256"),
  35684. WOLFSSL_FAILURE);
  35685. ExpectIntEQ(wolfSSL_set1_sigalgs_list(NULL, "RSA+SHA256"),
  35686. WOLFSSL_FAILURE);
  35687. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256"),
  35688. WOLFSSL_SUCCESS);
  35689. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256"),
  35690. WOLFSSL_SUCCESS);
  35691. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-SHA256"),
  35692. WOLFSSL_FAILURE);
  35693. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-SHA256"),
  35694. WOLFSSL_FAILURE);
  35695. #ifdef WC_RSA_PSS
  35696. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA-PSS+SHA256"),
  35697. WOLFSSL_SUCCESS);
  35698. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA-PSS+SHA256"),
  35699. WOLFSSL_SUCCESS);
  35700. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "PSS+SHA256"),
  35701. WOLFSSL_SUCCESS);
  35702. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "PSS+SHA256"),
  35703. WOLFSSL_SUCCESS);
  35704. #endif
  35705. #ifdef WOLFSSL_SHA512
  35706. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35707. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  35708. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35709. "RSA+SHA256:RSA+SHA512"), WOLFSSL_SUCCESS);
  35710. #elif defined(WOLFSSL_SHA384)
  35711. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35712. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  35713. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35714. "RSA+SHA256:RSA+SHA384"), WOLFSSL_SUCCESS);
  35715. #endif
  35716. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA"), WOLFSSL_FAILURE);
  35717. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA"), WOLFSSL_FAILURE);
  35718. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA:RSA+SHA256"),
  35719. WOLFSSL_FAILURE);
  35720. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA:RSA+SHA256"),
  35721. WOLFSSL_FAILURE);
  35722. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "RSA+SHA256+SHA256"),
  35723. WOLFSSL_FAILURE);
  35724. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "RSA+SHA256+RSA"),
  35725. WOLFSSL_FAILURE);
  35726. #endif
  35727. #endif
  35728. #ifdef HAVE_ECC
  35729. #ifndef NO_SHA256
  35730. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ECDSA+SHA256"),
  35731. WOLFSSL_SUCCESS);
  35732. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ECDSA+SHA256"),
  35733. WOLFSSL_SUCCESS);
  35734. #ifdef WOLFSSL_SHA512
  35735. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35736. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  35737. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35738. "ECDSA+SHA256:ECDSA+SHA512"), WOLFSSL_SUCCESS);
  35739. #elif defined(WOLFSSL_SHA384)
  35740. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  35741. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  35742. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  35743. "ECDSA+SHA256:ECDSA+SHA384"), WOLFSSL_SUCCESS);
  35744. #endif
  35745. #endif
  35746. #endif
  35747. #ifdef HAVE_ED25519
  35748. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED25519"), WOLFSSL_SUCCESS);
  35749. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED25519"), WOLFSSL_SUCCESS);
  35750. #endif
  35751. #ifdef HAVE_ED448
  35752. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "ED448"), WOLFSSL_SUCCESS);
  35753. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "ED448"), WOLFSSL_SUCCESS);
  35754. #endif
  35755. #ifndef NO_DSA
  35756. #ifndef NO_SHA256
  35757. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA256"),
  35758. WOLFSSL_SUCCESS);
  35759. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA256"),
  35760. WOLFSSL_SUCCESS);
  35761. #endif
  35762. #if !defined(NO_SHA) && (!defined(NO_OLD_TLS) || \
  35763. defined(WOLFSSL_ALLOW_TLS_SHA1))
  35764. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx, "DSA+SHA1"),
  35765. WOLFSSL_SUCCESS);
  35766. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl, "DSA+SHA1"),
  35767. WOLFSSL_SUCCESS);
  35768. #endif
  35769. #endif
  35770. SSL_free(ssl);
  35771. SSL_CTX_free(ctx);
  35772. #endif /* !NO_WOLFSSL_CLIENT || !NO_WOLFSSL_SERVER */
  35773. #endif
  35774. return EXPECT_RESULT();
  35775. }
  35776. /* Testing wolfSSL_set_tlsext_status_type function.
  35777. * PRE: OPENSSL and HAVE_CERTIFICATE_STATUS_REQUEST defined.
  35778. */
  35779. static int test_wolfSSL_set_tlsext_status_type(void)
  35780. {
  35781. EXPECT_DECLS;
  35782. #if defined(OPENSSL_EXTRA) && defined(HAVE_CERTIFICATE_STATUS_REQUEST) && \
  35783. !defined(NO_RSA) && !defined(NO_WOLFSSL_SERVER)
  35784. SSL* ssl = NULL;
  35785. SSL_CTX* ctx = NULL;
  35786. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  35787. ExpectTrue(SSL_CTX_use_certificate_file(ctx, svrCertFile,
  35788. SSL_FILETYPE_PEM));
  35789. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, svrKeyFile, SSL_FILETYPE_PEM));
  35790. ExpectNotNull(ssl = SSL_new(ctx));
  35791. ExpectIntEQ(SSL_set_tlsext_status_type(ssl,TLSEXT_STATUSTYPE_ocsp),
  35792. SSL_SUCCESS);
  35793. ExpectIntEQ(SSL_get_tlsext_status_type(ssl), TLSEXT_STATUSTYPE_ocsp);
  35794. SSL_free(ssl);
  35795. SSL_CTX_free(ctx);
  35796. #endif /* OPENSSL_EXTRA && HAVE_CERTIFICATE_STATUS_REQUEST && !NO_RSA */
  35797. return EXPECT_RESULT();
  35798. }
  35799. #ifndef NO_BIO
  35800. static int test_wolfSSL_PEM_read_bio(void)
  35801. {
  35802. EXPECT_DECLS;
  35803. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  35804. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  35805. byte buff[6000];
  35806. XFILE f = XBADFILE;
  35807. int bytes;
  35808. X509* x509 = NULL;
  35809. BIO* bio = NULL;
  35810. BUF_MEM* buf = NULL;
  35811. ExpectTrue((f = XFOPEN(cliCertFile, "rb")) != XBADFILE);
  35812. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  35813. if (f != XBADFILE)
  35814. XFCLOSE(f);
  35815. ExpectNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  35816. ExpectNotNull(bio = BIO_new_mem_buf((void*)buff, bytes));
  35817. ExpectIntEQ(BIO_set_mem_eof_return(bio, -0xDEAD), 1);
  35818. ExpectNotNull(x509 = PEM_read_bio_X509_AUX(bio, NULL, NULL, NULL));
  35819. ExpectIntEQ((int)BIO_set_fd(bio, 0, BIO_CLOSE), 1);
  35820. /* BIO should return the set EOF value */
  35821. ExpectIntEQ(BIO_read(bio, buff, sizeof(buff)), -0xDEAD);
  35822. ExpectIntEQ(BIO_set_close(bio, BIO_NOCLOSE), 1);
  35823. ExpectIntEQ(BIO_set_close(NULL, BIO_NOCLOSE), 1);
  35824. ExpectIntEQ(SSL_SUCCESS, BIO_get_mem_ptr(bio, &buf));
  35825. BIO_free(bio);
  35826. BUF_MEM_free(buf);
  35827. X509_free(x509);
  35828. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) &&
  35829. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  35830. return EXPECT_RESULT();
  35831. }
  35832. #if defined(OPENSSL_EXTRA)
  35833. static long bioCallback(BIO *bio, int cmd, const char* argp, int argi,
  35834. long argl, long ret)
  35835. {
  35836. (void)bio;
  35837. (void)cmd;
  35838. (void)argp;
  35839. (void)argi;
  35840. (void)argl;
  35841. return ret;
  35842. }
  35843. #endif
  35844. static int test_wolfSSL_BIO(void)
  35845. {
  35846. EXPECT_DECLS;
  35847. #if defined(OPENSSL_EXTRA)
  35848. const unsigned char* p = NULL;
  35849. byte buff[20];
  35850. BIO* bio1 = NULL;
  35851. BIO* bio2 = NULL;
  35852. BIO* bio3 = NULL;
  35853. char* bufPt = NULL;
  35854. int i;
  35855. for (i = 0; i < 20; i++) {
  35856. buff[i] = i;
  35857. }
  35858. /* test BIO_free with NULL */
  35859. ExpectIntEQ(BIO_free(NULL), WOLFSSL_FAILURE);
  35860. /* Creating and testing type BIO_s_bio */
  35861. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  35862. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  35863. ExpectNotNull(bio3 = BIO_new(BIO_s_bio()));
  35864. /* read/write before set up */
  35865. ExpectIntEQ(BIO_read(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  35866. ExpectIntEQ(BIO_write(bio1, buff, 2), WOLFSSL_BIO_UNSET);
  35867. ExpectIntEQ(BIO_set_nbio(bio1, 1), 1);
  35868. ExpectIntEQ(BIO_set_write_buf_size(bio1, 20), WOLFSSL_SUCCESS);
  35869. ExpectIntEQ(BIO_set_write_buf_size(bio2, 8), WOLFSSL_SUCCESS);
  35870. ExpectIntEQ(BIO_make_bio_pair(bio1, bio2), WOLFSSL_SUCCESS);
  35871. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 10), 10);
  35872. ExpectNotNull(XMEMCPY(bufPt, buff, 10));
  35873. ExpectIntEQ(BIO_write(bio1, buff + 10, 10), 10);
  35874. /* write buffer full */
  35875. ExpectIntEQ(BIO_write(bio1, buff, 10), WOLFSSL_BIO_ERROR);
  35876. ExpectIntEQ(BIO_flush(bio1), WOLFSSL_SUCCESS);
  35877. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 0);
  35878. /* write the other direction with pair */
  35879. ExpectIntEQ((int)BIO_nwrite(bio2, &bufPt, 10), 8);
  35880. ExpectNotNull(XMEMCPY(bufPt, buff, 8));
  35881. ExpectIntEQ(BIO_write(bio2, buff, 10), WOLFSSL_BIO_ERROR);
  35882. /* try read */
  35883. ExpectIntEQ((int)BIO_ctrl_pending(bio1), 8);
  35884. ExpectIntEQ((int)BIO_ctrl_pending(bio2), 20);
  35885. /* try read using ctrl function */
  35886. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_WPENDING, 0, NULL), 8);
  35887. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_PENDING, 0, NULL), 8);
  35888. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_WPENDING, 0, NULL), 20);
  35889. ExpectIntEQ((int)BIO_ctrl(bio2, BIO_CTRL_PENDING, 0, NULL), 20);
  35890. ExpectIntEQ(BIO_nread(bio2, &bufPt, (int)BIO_ctrl_pending(bio2)), 20);
  35891. for (i = 0; i < 20; i++) {
  35892. ExpectIntEQ((int)bufPt[i], i);
  35893. }
  35894. ExpectIntEQ(BIO_nread(bio2, &bufPt, 1), 0);
  35895. ExpectIntEQ(BIO_nread(bio1, &bufPt, (int)BIO_ctrl_pending(bio1)), 8);
  35896. for (i = 0; i < 8; i++) {
  35897. ExpectIntEQ((int)bufPt[i], i);
  35898. }
  35899. ExpectIntEQ(BIO_nread(bio1, &bufPt, 1), 0);
  35900. ExpectIntEQ(BIO_ctrl_reset_read_request(bio1), 1);
  35901. /* new pair */
  35902. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_FAILURE);
  35903. BIO_free(bio2); /* free bio2 and automatically remove from pair */
  35904. bio2 = NULL;
  35905. ExpectIntEQ(BIO_make_bio_pair(bio1, bio3), WOLFSSL_SUCCESS);
  35906. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  35907. ExpectIntEQ(BIO_nread(bio3, &bufPt, 10), 0);
  35908. /* test wrap around... */
  35909. ExpectIntEQ(BIO_reset(bio1), 0);
  35910. ExpectIntEQ(BIO_reset(bio3), 0);
  35911. /* fill write buffer, read only small amount then write again */
  35912. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35913. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  35914. ExpectIntEQ(BIO_nread(bio3, &bufPt, 4), 4);
  35915. for (i = 0; i < 4; i++) {
  35916. ExpectIntEQ(bufPt[i], i);
  35917. }
  35918. /* try writing over read index */
  35919. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 5), 4);
  35920. ExpectNotNull(XMEMSET(bufPt, 0, 4));
  35921. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 20);
  35922. /* read and write 0 bytes */
  35923. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  35924. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 0), 0);
  35925. /* should read only to end of write buffer then need to read again */
  35926. ExpectIntEQ(BIO_nread(bio3, &bufPt, 20), 16);
  35927. for (i = 0; i < 16; i++) {
  35928. ExpectIntEQ(bufPt[i], buff[4 + i]);
  35929. }
  35930. ExpectIntEQ(BIO_nread(bio3, NULL, 0), WOLFSSL_FAILURE);
  35931. ExpectIntEQ(BIO_nread0(bio3, &bufPt), 4);
  35932. for (i = 0; i < 4; i++) {
  35933. ExpectIntEQ(bufPt[i], 0);
  35934. }
  35935. /* read index should not have advanced with nread0 */
  35936. ExpectIntEQ(BIO_nread(bio3, &bufPt, 5), 4);
  35937. for (i = 0; i < 4; i++) {
  35938. ExpectIntEQ(bufPt[i], 0);
  35939. }
  35940. /* write and fill up buffer checking reset of index state */
  35941. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35942. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  35943. /* test reset on data in bio1 write buffer */
  35944. ExpectIntEQ(BIO_reset(bio1), 0);
  35945. ExpectIntEQ((int)BIO_ctrl_pending(bio3), 0);
  35946. ExpectIntEQ(BIO_nread(bio3, &bufPt, 3), 0);
  35947. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 20), 20);
  35948. ExpectIntEQ((int)BIO_ctrl(bio1, BIO_CTRL_INFO, 0, &p), 20);
  35949. ExpectNotNull(p);
  35950. ExpectNotNull(XMEMCPY(bufPt, buff, 20));
  35951. ExpectIntEQ(BIO_nread(bio3, &bufPt, 6), 6);
  35952. for (i = 0; i < 6; i++) {
  35953. ExpectIntEQ(bufPt[i], i);
  35954. }
  35955. /* test case of writing twice with offset read index */
  35956. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 3), 3);
  35957. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 3); /* try overwriting */
  35958. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35959. ExpectIntEQ(BIO_nread(bio3, &bufPt, 0), 0);
  35960. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35961. ExpectIntEQ(BIO_nread(bio3, &bufPt, 1), 1);
  35962. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), 1);
  35963. ExpectIntEQ(BIO_nwrite(bio1, &bufPt, 4), WOLFSSL_BIO_ERROR);
  35964. BIO_free(bio1);
  35965. bio1 = NULL;
  35966. BIO_free(bio3);
  35967. bio3 = NULL;
  35968. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)
  35969. {
  35970. BIO* bioA = NULL;
  35971. BIO* bioB = NULL;
  35972. ExpectIntEQ(BIO_new_bio_pair(NULL, 256, NULL, 256), BAD_FUNC_ARG);
  35973. ExpectIntEQ(BIO_new_bio_pair(&bioA, 256, &bioB, 256), WOLFSSL_SUCCESS);
  35974. BIO_free(bioA);
  35975. bioA = NULL;
  35976. BIO_free(bioB);
  35977. bioB = NULL;
  35978. }
  35979. #endif /* OPENSSL_ALL || WOLFSSL_ASIO */
  35980. /* BIOs with file pointers */
  35981. #if !defined(NO_FILESYSTEM)
  35982. {
  35983. XFILE f1 = XBADFILE;
  35984. XFILE f2 = XBADFILE;
  35985. BIO* f_bio1 = NULL;
  35986. BIO* f_bio2 = NULL;
  35987. unsigned char cert[300];
  35988. char testFile[] = "tests/bio_write_test.txt";
  35989. char msg[] = "bio_write_test.txt contains the first 300 bytes of certs/server-cert.pem\ncreated by tests/unit.test\n\n";
  35990. ExpectNotNull(f_bio1 = BIO_new(BIO_s_file()));
  35991. ExpectNotNull(f_bio2 = BIO_new(BIO_s_file()));
  35992. /* Failure due to wrong BIO type */
  35993. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  35994. ExpectIntEQ((int)BIO_set_mem_eof_return(NULL, -1), 0);
  35995. ExpectTrue((f1 = XFOPEN(svrCertFile, "rwb")) != XBADFILE);
  35996. ExpectIntEQ((int)BIO_set_fp(f_bio1, f1, BIO_CLOSE), WOLFSSL_SUCCESS);
  35997. ExpectIntEQ(BIO_write_filename(f_bio2, testFile),
  35998. WOLFSSL_SUCCESS);
  35999. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  36000. ExpectIntEQ(BIO_tell(f_bio1),sizeof(cert));
  36001. ExpectIntEQ(BIO_write(f_bio2, msg, sizeof(msg)), sizeof(msg));
  36002. ExpectIntEQ(BIO_tell(f_bio2),sizeof(msg));
  36003. ExpectIntEQ(BIO_write(f_bio2, cert, sizeof(cert)), sizeof(cert));
  36004. ExpectIntEQ(BIO_tell(f_bio2),sizeof(cert) + sizeof(msg));
  36005. ExpectIntEQ((int)BIO_get_fp(f_bio2, &f2), WOLFSSL_SUCCESS);
  36006. ExpectIntEQ(BIO_reset(f_bio2), 0);
  36007. ExpectIntEQ(BIO_tell(NULL),-1);
  36008. ExpectIntEQ(BIO_tell(f_bio2),0);
  36009. ExpectIntEQ(BIO_seek(f_bio2, 4), 0);
  36010. ExpectIntEQ(BIO_tell(f_bio2),4);
  36011. BIO_free(f_bio1);
  36012. f_bio1 = NULL;
  36013. BIO_free(f_bio2);
  36014. f_bio2 = NULL;
  36015. ExpectNotNull(f_bio1 = BIO_new_file(svrCertFile, "rwb"));
  36016. ExpectIntEQ((int)BIO_set_mem_eof_return(f_bio1, -1), 0);
  36017. ExpectIntEQ(BIO_read(f_bio1, cert, sizeof(cert)), sizeof(cert));
  36018. BIO_free(f_bio1);
  36019. f_bio1 = NULL;
  36020. }
  36021. #endif /* !defined(NO_FILESYSTEM) */
  36022. /* BIO info callback */
  36023. {
  36024. const char* testArg = "test";
  36025. BIO* cb_bio = NULL;
  36026. ExpectNotNull(cb_bio = BIO_new(BIO_s_mem()));
  36027. BIO_set_callback(cb_bio, bioCallback);
  36028. ExpectNotNull(BIO_get_callback(cb_bio));
  36029. BIO_set_callback(cb_bio, NULL);
  36030. ExpectNull(BIO_get_callback(cb_bio));
  36031. BIO_set_callback_arg(cb_bio, (char*)testArg);
  36032. ExpectStrEQ(BIO_get_callback_arg(cb_bio), testArg);
  36033. ExpectNull(BIO_get_callback_arg(NULL));
  36034. BIO_free(cb_bio);
  36035. cb_bio = NULL;
  36036. }
  36037. /* BIO_vfree */
  36038. ExpectNotNull(bio1 = BIO_new(BIO_s_bio()));
  36039. BIO_vfree(NULL);
  36040. BIO_vfree(bio1);
  36041. #endif
  36042. return EXPECT_RESULT();
  36043. }
  36044. static int test_wolfSSL_BIO_BIO_ring_read(void)
  36045. {
  36046. EXPECT_DECLS;
  36047. #if defined(OPENSSL_ALL)
  36048. BIO* bio1 = NULL;
  36049. BIO* bio2 = NULL;
  36050. byte data[50];
  36051. byte tmp[50];
  36052. XMEMSET(data, 42, sizeof(data));
  36053. ExpectIntEQ(BIO_new_bio_pair(&bio1, sizeof(data), &bio2, sizeof(data)),
  36054. SSL_SUCCESS);
  36055. ExpectIntEQ(BIO_write(bio1, data, 40), 40);
  36056. ExpectIntEQ(BIO_read(bio1, tmp, 20), -1);
  36057. ExpectIntEQ(BIO_read(bio2, tmp, 20), 20);
  36058. ExpectBufEQ(tmp, data, 20);
  36059. ExpectIntEQ(BIO_write(bio1, data, 20), 20);
  36060. ExpectIntEQ(BIO_read(bio2, tmp, 40), 40);
  36061. ExpectBufEQ(tmp, data, 40);
  36062. BIO_free(bio1);
  36063. BIO_free(bio2);
  36064. #endif
  36065. return EXPECT_RESULT();
  36066. }
  36067. #endif /* !NO_BIO */
  36068. static int test_wolfSSL_a2i_IPADDRESS(void)
  36069. {
  36070. EXPECT_DECLS;
  36071. #if defined(OPENSSL_ALL) && !defined(WOLFSSL_USER_IO)
  36072. const unsigned char* data = NULL;
  36073. int dataSz = 0;
  36074. ASN1_OCTET_STRING *st = NULL;
  36075. const unsigned char ipv4_exp[] = {0x7F, 0, 0, 1};
  36076. const unsigned char ipv6_exp[] = {
  36077. 0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  36078. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77
  36079. };
  36080. const unsigned char ipv6_home[] = {
  36081. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  36082. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
  36083. };
  36084. ExpectNull(st = a2i_IPADDRESS("127.0.0.1bad"));
  36085. ExpectNotNull(st = a2i_IPADDRESS("127.0.0.1"));
  36086. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  36087. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP4_ADDR_LEN);
  36088. ExpectIntEQ(XMEMCMP(data, ipv4_exp, dataSz), 0);
  36089. ASN1_STRING_free(st);
  36090. st = NULL;
  36091. ExpectNotNull(st = a2i_IPADDRESS("::1"));
  36092. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  36093. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  36094. ExpectIntEQ(XMEMCMP(data, ipv6_home, dataSz), 0);
  36095. ASN1_STRING_free(st);
  36096. st = NULL;
  36097. ExpectNotNull(st = a2i_IPADDRESS("2021:db8::ff00:42:7777"));
  36098. ExpectNotNull(data = ASN1_STRING_get0_data(st));
  36099. ExpectIntEQ(dataSz = ASN1_STRING_length(st), WOLFSSL_IP6_ADDR_LEN);
  36100. ExpectIntEQ(XMEMCMP(data, ipv6_exp, dataSz), 0);
  36101. ASN1_STRING_free(st);
  36102. #endif
  36103. return EXPECT_RESULT();
  36104. }
  36105. static int test_wolfSSL_X509_cmp_time(void)
  36106. {
  36107. EXPECT_DECLS;
  36108. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) \
  36109. && !defined(USER_TIME) && !defined(TIME_OVERRIDES)
  36110. WOLFSSL_ASN1_TIME asn_time;
  36111. time_t t;
  36112. ExpectIntEQ(0, wolfSSL_X509_cmp_time(NULL, &t));
  36113. XMEMSET(&asn_time, 0, sizeof(WOLFSSL_ASN1_TIME));
  36114. ExpectIntEQ(0, wolfSSL_X509_cmp_time(&asn_time, &t));
  36115. ExpectIntEQ(ASN1_TIME_set_string(&asn_time, "000222211515Z"), 1);
  36116. ExpectIntEQ(-1, wolfSSL_X509_cmp_time(&asn_time, NULL));
  36117. #endif
  36118. return EXPECT_RESULT();
  36119. }
  36120. static int test_wolfSSL_X509_time_adj(void)
  36121. {
  36122. EXPECT_DECLS;
  36123. #if defined(OPENSSL_EXTRA) && !defined(NO_ASN_TIME) && \
  36124. !defined(USER_TIME) && !defined(TIME_OVERRIDES) && \
  36125. defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA) && \
  36126. !defined(NO_ASN_TIME)
  36127. X509* x509 = NULL;
  36128. time_t t;
  36129. time_t not_before;
  36130. time_t not_after;
  36131. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  36132. client_cert_der_2048, sizeof_client_cert_der_2048,
  36133. WOLFSSL_FILETYPE_ASN1));
  36134. t = 0;
  36135. not_before = wc_Time(0);
  36136. not_after = wc_Time(0) + (60 * 24 * 30); /* 30 days after */
  36137. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before, &t));
  36138. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after, &t));
  36139. /* Check X509_gmtime_adj, too. */
  36140. ExpectNotNull(X509_gmtime_adj(X509_get_notAfter(x509), not_after));
  36141. X509_free(x509);
  36142. #endif
  36143. return EXPECT_RESULT();
  36144. }
  36145. static int test_wolfSSL_X509_bad_altname(void)
  36146. {
  36147. EXPECT_DECLS;
  36148. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  36149. const unsigned char malformed_alt_name_cert[] = {
  36150. 0x30, 0x82, 0x02, 0xf9, 0x30, 0x82, 0x01, 0xe1, 0xa0, 0x03, 0x02, 0x01,
  36151. 0x02, 0x02, 0x02, 0x10, 0x21, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86, 0x48,
  36152. 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00, 0x30, 0x0f, 0x31, 0x0d,
  36153. 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x04, 0x61, 0x61, 0x31,
  36154. 0x31, 0x30, 0x1e, 0x17, 0x0d, 0x31, 0x36, 0x30, 0x32, 0x30, 0x37, 0x31,
  36155. 0x37, 0x32, 0x34, 0x30, 0x30, 0x5a, 0x17, 0x0d, 0x33, 0x34, 0x30, 0x32,
  36156. 0x31, 0x34, 0x30, 0x36, 0x32, 0x36, 0x35, 0x33, 0x5a, 0x30, 0x0f, 0x31,
  36157. 0x0d, 0x30, 0x0b, 0x06, 0x03, 0x55, 0x04, 0x03, 0x13, 0x04, 0x61, 0x61,
  36158. 0x61, 0x61, 0x30, 0x82, 0x01, 0x20, 0x30, 0x0d, 0x06, 0x09, 0x2a, 0x86,
  36159. 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01,
  36160. 0x0d, 0x00, 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01, 0x00, 0xa8,
  36161. 0x8a, 0x5e, 0x26, 0x23, 0x1b, 0x31, 0xd3, 0x37, 0x1a, 0x70, 0xb2, 0xec,
  36162. 0x3f, 0x74, 0xd4, 0xb4, 0x44, 0xe3, 0x7a, 0xa5, 0xc0, 0xf5, 0xaa, 0x97,
  36163. 0x26, 0x9a, 0x04, 0xff, 0xda, 0xbe, 0xe5, 0x09, 0x03, 0x98, 0x3d, 0xb5,
  36164. 0xbf, 0x01, 0x2c, 0x9a, 0x0a, 0x3a, 0xfb, 0xbc, 0x3c, 0xe7, 0xbe, 0x83,
  36165. 0x5c, 0xb3, 0x70, 0xe8, 0x5c, 0xe3, 0xd1, 0x83, 0xc3, 0x94, 0x08, 0xcd,
  36166. 0x1a, 0x87, 0xe5, 0xe0, 0x5b, 0x9c, 0x5c, 0x6e, 0xb0, 0x7d, 0xe2, 0x58,
  36167. 0x6c, 0xc3, 0xb5, 0xc8, 0x9d, 0x11, 0xf1, 0x5d, 0x96, 0x0d, 0x66, 0x1e,
  36168. 0x56, 0x7f, 0x8f, 0x59, 0xa7, 0xa5, 0xe1, 0xc5, 0xe7, 0x81, 0x4c, 0x09,
  36169. 0x9d, 0x5e, 0x96, 0xf0, 0x9a, 0xc2, 0x8b, 0x70, 0xd5, 0xab, 0x79, 0x58,
  36170. 0x5d, 0xb7, 0x58, 0xaa, 0xfd, 0x75, 0x52, 0xaa, 0x4b, 0xa7, 0x25, 0x68,
  36171. 0x76, 0x59, 0x00, 0xee, 0x78, 0x2b, 0x91, 0xc6, 0x59, 0x91, 0x99, 0x38,
  36172. 0x3e, 0xa1, 0x76, 0xc3, 0xf5, 0x23, 0x6b, 0xe6, 0x07, 0xea, 0x63, 0x1c,
  36173. 0x97, 0x49, 0xef, 0xa0, 0xfe, 0xfd, 0x13, 0xc9, 0xa9, 0x9f, 0xc2, 0x0b,
  36174. 0xe6, 0x87, 0x92, 0x5b, 0xcc, 0xf5, 0x42, 0x95, 0x4a, 0xa4, 0x6d, 0x64,
  36175. 0xba, 0x7d, 0xce, 0xcb, 0x04, 0xd0, 0xf8, 0xe7, 0xe3, 0xda, 0x75, 0x60,
  36176. 0xd3, 0x8b, 0x6a, 0x64, 0xfc, 0x78, 0x56, 0x21, 0x69, 0x5a, 0xe8, 0xa7,
  36177. 0x8f, 0xfb, 0x8f, 0x82, 0xe3, 0xae, 0x36, 0xa2, 0x93, 0x66, 0x92, 0xcb,
  36178. 0x82, 0xa3, 0xbe, 0x84, 0x00, 0x86, 0xdc, 0x7e, 0x6d, 0x53, 0x77, 0x84,
  36179. 0x17, 0xb9, 0x55, 0x43, 0x0d, 0xf1, 0x16, 0x1f, 0xd5, 0x43, 0x75, 0x99,
  36180. 0x66, 0x19, 0x52, 0xd0, 0xac, 0x5f, 0x74, 0xad, 0xb2, 0x90, 0x15, 0x50,
  36181. 0x04, 0x74, 0x43, 0xdf, 0x6c, 0x35, 0xd0, 0xfd, 0x32, 0x37, 0xb3, 0x8d,
  36182. 0xf5, 0xe5, 0x09, 0x02, 0x01, 0x03, 0xa3, 0x61, 0x30, 0x5f, 0x30, 0x0c,
  36183. 0x06, 0x03, 0x55, 0x1d, 0x13, 0x01, 0x01, 0xff, 0x04, 0x02, 0x30, 0x00,
  36184. 0x30, 0x0f, 0x06, 0x03, 0x55, 0x1d, 0x11, 0x04, 0x08, 0x30, 0x06, 0x82,
  36185. 0x04, 0x61, 0x2a, 0x00, 0x2a, 0x30, 0x1d, 0x06, 0x03, 0x55, 0x1d, 0x0e,
  36186. 0x04, 0x16, 0x04, 0x14, 0x92, 0x6a, 0x1e, 0x52, 0x3a, 0x1a, 0x57, 0x9f,
  36187. 0xc9, 0x82, 0x9a, 0xce, 0xc8, 0xc0, 0xa9, 0x51, 0x9d, 0x2f, 0xc7, 0x72,
  36188. 0x30, 0x1f, 0x06, 0x03, 0x55, 0x1d, 0x23, 0x04, 0x18, 0x30, 0x16, 0x80,
  36189. 0x14, 0x6b, 0xf9, 0xa4, 0x2d, 0xa5, 0xe9, 0x39, 0x89, 0xa8, 0x24, 0x58,
  36190. 0x79, 0x87, 0x11, 0xfc, 0x6f, 0x07, 0x91, 0xef, 0xa6, 0x30, 0x0d, 0x06,
  36191. 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x0b, 0x05, 0x00,
  36192. 0x03, 0x82, 0x01, 0x01, 0x00, 0x3f, 0xd5, 0x37, 0x2f, 0xc7, 0xf8, 0x8b,
  36193. 0x39, 0x1c, 0xe3, 0xdf, 0x77, 0xee, 0xc6, 0x4b, 0x5f, 0x84, 0xcf, 0xfa,
  36194. 0x33, 0x2c, 0xb2, 0xb5, 0x4b, 0x09, 0xee, 0x56, 0xc0, 0xf2, 0xf0, 0xeb,
  36195. 0xad, 0x1c, 0x02, 0xef, 0xae, 0x09, 0x53, 0xc0, 0x06, 0xad, 0x4e, 0xfd,
  36196. 0x3e, 0x8c, 0x13, 0xb3, 0xbf, 0x80, 0x05, 0x36, 0xb5, 0x3f, 0x2b, 0xc7,
  36197. 0x60, 0x53, 0x14, 0xbf, 0x33, 0x63, 0x47, 0xc3, 0xc6, 0x28, 0xda, 0x10,
  36198. 0x12, 0xe2, 0xc4, 0xeb, 0xc5, 0x64, 0x66, 0xc0, 0xcc, 0x6b, 0x84, 0xda,
  36199. 0x0c, 0xe9, 0xf6, 0xe3, 0xf8, 0x8e, 0x3d, 0x95, 0x5f, 0xba, 0x9f, 0xe1,
  36200. 0xc7, 0xed, 0x6e, 0x97, 0xcc, 0xbd, 0x7d, 0xe5, 0x4e, 0xab, 0xbc, 0x1b,
  36201. 0xf1, 0x3a, 0x09, 0x33, 0x09, 0xe1, 0xcc, 0xec, 0x21, 0x16, 0x8e, 0xb1,
  36202. 0x74, 0x9e, 0xc8, 0x13, 0x7c, 0xdf, 0x07, 0xaa, 0xeb, 0x70, 0xd7, 0x91,
  36203. 0x5c, 0xc4, 0xef, 0x83, 0x88, 0xc3, 0xe4, 0x97, 0xfa, 0xe4, 0xdf, 0xd7,
  36204. 0x0d, 0xff, 0xba, 0x78, 0x22, 0xfc, 0x3f, 0xdc, 0xd8, 0x02, 0x8d, 0x93,
  36205. 0x57, 0xf9, 0x9e, 0x39, 0x3a, 0x77, 0x00, 0xd9, 0x19, 0xaa, 0x68, 0xa1,
  36206. 0xe6, 0x9e, 0x13, 0xeb, 0x37, 0x16, 0xf5, 0x77, 0xa4, 0x0b, 0x40, 0x04,
  36207. 0xd3, 0xa5, 0x49, 0x78, 0x35, 0xfa, 0x3b, 0xf6, 0x02, 0xab, 0x85, 0xee,
  36208. 0xcb, 0x9b, 0x62, 0xda, 0x05, 0x00, 0x22, 0x2f, 0xf8, 0xbd, 0x0b, 0xe5,
  36209. 0x2c, 0xb2, 0x53, 0x78, 0x0a, 0xcb, 0x69, 0xc0, 0xb6, 0x9f, 0x96, 0xff,
  36210. 0x58, 0x22, 0x70, 0x9c, 0x01, 0x2e, 0x56, 0x60, 0x5d, 0x37, 0xe3, 0x40,
  36211. 0x25, 0xc9, 0x90, 0xc8, 0x0f, 0x41, 0x68, 0xb4, 0xfd, 0x10, 0xe2, 0x09,
  36212. 0x99, 0x08, 0x5d, 0x7b, 0xc9, 0xe3, 0x29, 0xd4, 0x5a, 0xcf, 0xc9, 0x34,
  36213. 0x55, 0xa1, 0x40, 0x44, 0xd6, 0x88, 0x16, 0xbb, 0xdd
  36214. };
  36215. X509* x509 = NULL;
  36216. int certSize = (int)sizeof(malformed_alt_name_cert) / sizeof(unsigned char);
  36217. const char *name = "aaaaa";
  36218. int nameLen = (int)XSTRLEN(name);
  36219. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_buffer(
  36220. malformed_alt_name_cert, certSize, SSL_FILETYPE_ASN1));
  36221. /* malformed_alt_name_cert has a malformed alternative
  36222. * name of "a*\0*". Ensure that it does not match "aaaaa" */
  36223. ExpectIntNE(wolfSSL_X509_check_host(x509, name, nameLen,
  36224. WOLFSSL_ALWAYS_CHECK_SUBJECT, NULL), 1);
  36225. X509_free(x509);
  36226. #endif
  36227. return EXPECT_RESULT();
  36228. }
  36229. static int test_wolfSSL_X509(void)
  36230. {
  36231. EXPECT_DECLS;
  36232. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  36233. !defined(NO_RSA)
  36234. X509* x509 = NULL;
  36235. #ifndef NO_BIO
  36236. BIO* bio = NULL;
  36237. X509_STORE_CTX* ctx = NULL;
  36238. X509_STORE* store = NULL;
  36239. #endif
  36240. char der[] = "certs/ca-cert.der";
  36241. XFILE fp = XBADFILE;
  36242. ExpectNotNull(x509 = X509_new());
  36243. X509_free(x509);
  36244. x509 = NULL;
  36245. #ifndef NO_BIO
  36246. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  36247. SSL_FILETYPE_PEM));
  36248. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  36249. #ifdef WOLFSSL_CERT_GEN
  36250. ExpectIntEQ(i2d_X509_bio(bio, x509), SSL_SUCCESS);
  36251. #endif
  36252. ExpectNotNull(ctx = X509_STORE_CTX_new());
  36253. ExpectIntEQ(X509_verify_cert(ctx), SSL_FATAL_ERROR);
  36254. ExpectNotNull(store = X509_STORE_new());
  36255. ExpectIntEQ(X509_STORE_add_cert(store, x509), SSL_SUCCESS);
  36256. ExpectIntEQ(X509_STORE_CTX_init(ctx, store, x509, NULL), SSL_SUCCESS);
  36257. ExpectIntEQ(X509_verify_cert(ctx), SSL_SUCCESS);
  36258. X509_STORE_CTX_free(ctx);
  36259. X509_STORE_free(store);
  36260. X509_free(x509);
  36261. x509 = NULL;
  36262. BIO_free(bio);
  36263. #endif
  36264. /** d2i_X509_fp test **/
  36265. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  36266. ExpectNotNull(x509 = (X509 *)d2i_X509_fp(fp, (X509 **)NULL));
  36267. ExpectNotNull(x509);
  36268. X509_free(x509);
  36269. x509 = NULL;
  36270. if (fp != XBADFILE) {
  36271. XFCLOSE(fp);
  36272. fp = XBADFILE;
  36273. }
  36274. ExpectTrue((fp = XFOPEN(der, "rb")) != XBADFILE);
  36275. ExpectNotNull((X509 *)d2i_X509_fp(fp, (X509 **)&x509));
  36276. ExpectNotNull(x509);
  36277. X509_free(x509);
  36278. if (fp != XBADFILE)
  36279. XFCLOSE(fp);
  36280. /* X509_up_ref test */
  36281. ExpectIntEQ(X509_up_ref(NULL), 0);
  36282. ExpectNotNull(x509 = X509_new()); /* refCount = 1 */
  36283. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 2 */
  36284. ExpectIntEQ(X509_up_ref(x509), 1); /* refCount = 3 */
  36285. X509_free(x509); /* refCount = 2 */
  36286. X509_free(x509); /* refCount = 1 */
  36287. X509_free(x509); /* refCount = 0, free */
  36288. #endif
  36289. return EXPECT_RESULT();
  36290. }
  36291. static int test_wolfSSL_X509_get_ext_count(void)
  36292. {
  36293. EXPECT_DECLS;
  36294. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  36295. !defined(NO_RSA)
  36296. int ret = 0;
  36297. WOLFSSL_X509* x509 = NULL;
  36298. const char ocspRootCaFile[] = "./certs/ocsp/root-ca-cert.pem";
  36299. XFILE f = XBADFILE;
  36300. /* NULL parameter check */
  36301. ExpectIntEQ(X509_get_ext_count(NULL), WOLFSSL_FAILURE);
  36302. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  36303. SSL_FILETYPE_PEM));
  36304. ExpectIntEQ(X509_get_ext_count(x509), 5);
  36305. wolfSSL_X509_free(x509);
  36306. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(ocspRootCaFile,
  36307. SSL_FILETYPE_PEM));
  36308. ExpectIntEQ(X509_get_ext_count(x509), 5);
  36309. wolfSSL_X509_free(x509);
  36310. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  36311. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  36312. if (f != XBADFILE)
  36313. XFCLOSE(f);
  36314. /* wolfSSL_X509_get_ext_count() valid input */
  36315. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  36316. /* wolfSSL_X509_get_ext_count() NULL argument */
  36317. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(NULL)), WOLFSSL_FAILURE);
  36318. wolfSSL_X509_free(x509);
  36319. #endif
  36320. return EXPECT_RESULT();
  36321. }
  36322. static int test_wolfSSL_X509_sign2(void)
  36323. {
  36324. EXPECT_DECLS;
  36325. /* test requires WOLFSSL_AKID_NAME to match expected output */
  36326. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_CERTS) && \
  36327. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_ALT_NAMES) && \
  36328. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_AKID_NAME) && \
  36329. (defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || \
  36330. defined(WOLFSSL_IP_ALT_NAME))
  36331. WOLFSSL_X509 *x509 = NULL;
  36332. WOLFSSL_X509 *ca = NULL;
  36333. const unsigned char *der = NULL;
  36334. const unsigned char *pt = NULL;
  36335. WOLFSSL_EVP_PKEY *priv = NULL;
  36336. WOLFSSL_X509_NAME *name = NULL;
  36337. int derSz;
  36338. #ifndef NO_ASN_TIME
  36339. WOLFSSL_ASN1_TIME *notBefore = NULL;
  36340. WOLFSSL_ASN1_TIME *notAfter = NULL;
  36341. const int year = 365*24*60*60;
  36342. const int day = 24*60*60;
  36343. const int hour = 60*60;
  36344. const int mini = 60;
  36345. time_t t;
  36346. #endif
  36347. const unsigned char expected[] = {
  36348. 0x30, 0x82, 0x05, 0x13, 0x30, 0x82, 0x03, 0xFB, 0xA0, 0x03, 0x02, 0x01,
  36349. 0x02, 0x02, 0x14, 0x08, 0xB0, 0x54, 0x7A, 0x03, 0x5A, 0xEC, 0x55, 0x8A,
  36350. 0x12, 0xE8, 0xF9, 0x8E, 0x34, 0xB6, 0x13, 0xD9, 0x59, 0xB8, 0xE8, 0x30,
  36351. 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B,
  36352. 0x05, 0x00, 0x30, 0x81, 0x94, 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55,
  36353. 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03,
  36354. 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61,
  36355. 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42,
  36356. 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x11, 0x30, 0x0F, 0x06, 0x03,
  36357. 0x55, 0x04, 0x0A, 0x0C, 0x08, 0x53, 0x61, 0x77, 0x74, 0x6F, 0x6F, 0x74,
  36358. 0x68, 0x31, 0x13, 0x30, 0x11, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x0A,
  36359. 0x43, 0x6F, 0x6E, 0x73, 0x75, 0x6C, 0x74, 0x69, 0x6E, 0x67, 0x31, 0x18,
  36360. 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77, 0x77,
  36361. 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D,
  36362. 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  36363. 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77, 0x6F,
  36364. 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x1E, 0x17,
  36365. 0x0D, 0x30, 0x30, 0x30, 0x32, 0x31, 0x35, 0x32, 0x30, 0x33, 0x30, 0x30,
  36366. 0x30, 0x5A, 0x17, 0x0D, 0x30, 0x31, 0x30, 0x32, 0x31, 0x34, 0x32, 0x30,
  36367. 0x33, 0x30, 0x30, 0x30, 0x5A, 0x30, 0x81, 0x9E, 0x31, 0x0B, 0x30, 0x09,
  36368. 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55, 0x53, 0x31, 0x10, 0x30,
  36369. 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07, 0x4D, 0x6F, 0x6E, 0x74,
  36370. 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x07,
  36371. 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61, 0x6E, 0x31, 0x15, 0x30,
  36372. 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C, 0x77, 0x6F, 0x6C, 0x66,
  36373. 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38, 0x31, 0x19, 0x30, 0x17,
  36374. 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50, 0x72, 0x6F, 0x67, 0x72,
  36375. 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32, 0x30, 0x34, 0x38, 0x31,
  36376. 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03, 0x0C, 0x0F, 0x77, 0x77,
  36377. 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F,
  36378. 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7,
  36379. 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E, 0x66, 0x6F, 0x40, 0x77,
  36380. 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x30, 0x82,
  36381. 0x01, 0x22, 0x30, 0x0D, 0x06, 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D,
  36382. 0x01, 0x01, 0x01, 0x05, 0x00, 0x03, 0x82, 0x01, 0x0F, 0x00, 0x30, 0x82,
  36383. 0x01, 0x0A, 0x02, 0x82, 0x01, 0x01, 0x00, 0xC3, 0x03, 0xD1, 0x2B, 0xFE,
  36384. 0x39, 0xA4, 0x32, 0x45, 0x3B, 0x53, 0xC8, 0x84, 0x2B, 0x2A, 0x7C, 0x74,
  36385. 0x9A, 0xBD, 0xAA, 0x2A, 0x52, 0x07, 0x47, 0xD6, 0xA6, 0x36, 0xB2, 0x07,
  36386. 0x32, 0x8E, 0xD0, 0xBA, 0x69, 0x7B, 0xC6, 0xC3, 0x44, 0x9E, 0xD4, 0x81,
  36387. 0x48, 0xFD, 0x2D, 0x68, 0xA2, 0x8B, 0x67, 0xBB, 0xA1, 0x75, 0xC8, 0x36,
  36388. 0x2C, 0x4A, 0xD2, 0x1B, 0xF7, 0x8B, 0xBA, 0xCF, 0x0D, 0xF9, 0xEF, 0xEC,
  36389. 0xF1, 0x81, 0x1E, 0x7B, 0x9B, 0x03, 0x47, 0x9A, 0xBF, 0x65, 0xCC, 0x7F,
  36390. 0x65, 0x24, 0x69, 0xA6, 0xE8, 0x14, 0x89, 0x5B, 0xE4, 0x34, 0xF7, 0xC5,
  36391. 0xB0, 0x14, 0x93, 0xF5, 0x67, 0x7B, 0x3A, 0x7A, 0x78, 0xE1, 0x01, 0x56,
  36392. 0x56, 0x91, 0xA6, 0x13, 0x42, 0x8D, 0xD2, 0x3C, 0x40, 0x9C, 0x4C, 0xEF,
  36393. 0xD1, 0x86, 0xDF, 0x37, 0x51, 0x1B, 0x0C, 0xA1, 0x3B, 0xF5, 0xF1, 0xA3,
  36394. 0x4A, 0x35, 0xE4, 0xE1, 0xCE, 0x96, 0xDF, 0x1B, 0x7E, 0xBF, 0x4E, 0x97,
  36395. 0xD0, 0x10, 0xE8, 0xA8, 0x08, 0x30, 0x81, 0xAF, 0x20, 0x0B, 0x43, 0x14,
  36396. 0xC5, 0x74, 0x67, 0xB4, 0x32, 0x82, 0x6F, 0x8D, 0x86, 0xC2, 0x88, 0x40,
  36397. 0x99, 0x36, 0x83, 0xBA, 0x1E, 0x40, 0x72, 0x22, 0x17, 0xD7, 0x52, 0x65,
  36398. 0x24, 0x73, 0xB0, 0xCE, 0xEF, 0x19, 0xCD, 0xAE, 0xFF, 0x78, 0x6C, 0x7B,
  36399. 0xC0, 0x12, 0x03, 0xD4, 0x4E, 0x72, 0x0D, 0x50, 0x6D, 0x3B, 0xA3, 0x3B,
  36400. 0xA3, 0x99, 0x5E, 0x9D, 0xC8, 0xD9, 0x0C, 0x85, 0xB3, 0xD9, 0x8A, 0xD9,
  36401. 0x54, 0x26, 0xDB, 0x6D, 0xFA, 0xAC, 0xBB, 0xFF, 0x25, 0x4C, 0xC4, 0xD1,
  36402. 0x79, 0xF4, 0x71, 0xD3, 0x86, 0x40, 0x18, 0x13, 0xB0, 0x63, 0xB5, 0x72,
  36403. 0x4E, 0x30, 0xC4, 0x97, 0x84, 0x86, 0x2D, 0x56, 0x2F, 0xD7, 0x15, 0xF7,
  36404. 0x7F, 0xC0, 0xAE, 0xF5, 0xFC, 0x5B, 0xE5, 0xFB, 0xA1, 0xBA, 0xD3, 0x02,
  36405. 0x03, 0x01, 0x00, 0x01, 0xA3, 0x82, 0x01, 0x4F, 0x30, 0x82, 0x01, 0x4B,
  36406. 0x30, 0x0C, 0x06, 0x03, 0x55, 0x1D, 0x13, 0x04, 0x05, 0x30, 0x03, 0x01,
  36407. 0x01, 0xFF, 0x30, 0x1C, 0x06, 0x03, 0x55, 0x1D, 0x11, 0x04, 0x15, 0x30,
  36408. 0x13, 0x82, 0x0B, 0x65, 0x78, 0x61, 0x6D, 0x70, 0x6C, 0x65, 0x2E, 0x63,
  36409. 0x6F, 0x6D, 0x87, 0x04, 0x7F, 0x00, 0x00, 0x01, 0x30, 0x1D, 0x06, 0x03,
  36410. 0x55, 0x1D, 0x0E, 0x04, 0x16, 0x04, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  36411. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  36412. 0x85, 0x65, 0xC0, 0x30, 0x81, 0xDE, 0x06, 0x03, 0x55, 0x1D, 0x23, 0x04,
  36413. 0x81, 0xD6, 0x30, 0x81, 0xD3, 0x80, 0x14, 0x33, 0xD8, 0x45, 0x66, 0xD7,
  36414. 0x68, 0x87, 0x18, 0x7E, 0x54, 0x0D, 0x70, 0x27, 0x91, 0xC7, 0x26, 0xD7,
  36415. 0x85, 0x65, 0xC0, 0xA1, 0x81, 0xA4, 0xA4, 0x81, 0xA1, 0x30, 0x81, 0x9E,
  36416. 0x31, 0x0B, 0x30, 0x09, 0x06, 0x03, 0x55, 0x04, 0x06, 0x13, 0x02, 0x55,
  36417. 0x53, 0x31, 0x10, 0x30, 0x0E, 0x06, 0x03, 0x55, 0x04, 0x08, 0x0C, 0x07,
  36418. 0x4D, 0x6F, 0x6E, 0x74, 0x61, 0x6E, 0x61, 0x31, 0x10, 0x30, 0x0E, 0x06,
  36419. 0x03, 0x55, 0x04, 0x07, 0x0C, 0x07, 0x42, 0x6F, 0x7A, 0x65, 0x6D, 0x61,
  36420. 0x6E, 0x31, 0x15, 0x30, 0x13, 0x06, 0x03, 0x55, 0x04, 0x0A, 0x0C, 0x0C,
  36421. 0x77, 0x6F, 0x6C, 0x66, 0x53, 0x53, 0x4C, 0x5F, 0x32, 0x30, 0x34, 0x38,
  36422. 0x31, 0x19, 0x30, 0x17, 0x06, 0x03, 0x55, 0x04, 0x0B, 0x0C, 0x10, 0x50,
  36423. 0x72, 0x6F, 0x67, 0x72, 0x61, 0x6D, 0x6D, 0x69, 0x6E, 0x67, 0x2D, 0x32,
  36424. 0x30, 0x34, 0x38, 0x31, 0x18, 0x30, 0x16, 0x06, 0x03, 0x55, 0x04, 0x03,
  36425. 0x0C, 0x0F, 0x77, 0x77, 0x77, 0x2E, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  36426. 0x6C, 0x2E, 0x63, 0x6F, 0x6D, 0x31, 0x1F, 0x30, 0x1D, 0x06, 0x09, 0x2A,
  36427. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x09, 0x01, 0x16, 0x10, 0x69, 0x6E,
  36428. 0x66, 0x6F, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73, 0x6C, 0x2E, 0x63,
  36429. 0x6F, 0x6D, 0x82, 0x14, 0x08, 0xB0, 0x54, 0x7A, 0x03, 0x5A, 0xEC, 0x55,
  36430. 0x8A, 0x12, 0xE8, 0xF9, 0x8E, 0x34, 0xB6, 0x13, 0xD9, 0x59, 0xB8, 0xE8,
  36431. 0x30, 0x1D, 0x06, 0x03, 0x55, 0x1D, 0x25, 0x04, 0x16, 0x30, 0x14, 0x06,
  36432. 0x08, 0x2B, 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x01, 0x06, 0x08, 0x2B,
  36433. 0x06, 0x01, 0x05, 0x05, 0x07, 0x03, 0x02, 0x30, 0x0D, 0x06, 0x09, 0x2A,
  36434. 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x01, 0x0B, 0x05, 0x00, 0x03, 0x82,
  36435. 0x01, 0x01, 0x00, 0x14, 0xFB, 0xD0, 0xCE, 0x31, 0x7F, 0xA5, 0x59, 0xFA,
  36436. 0x7C, 0x68, 0x26, 0xA7, 0xE8, 0x0D, 0x9F, 0x50, 0x57, 0xFA, 0x1C, 0x7C,
  36437. 0x5E, 0x43, 0xA4, 0x97, 0x47, 0xB6, 0x41, 0xAC, 0x63, 0xD3, 0x61, 0x8C,
  36438. 0x1F, 0x42, 0xEF, 0x53, 0xD0, 0xBA, 0x31, 0x4D, 0x99, 0x74, 0xA4, 0x60,
  36439. 0xDC, 0xC6, 0x6F, 0xCC, 0x1E, 0x25, 0x98, 0xE1, 0xA4, 0xA0, 0x67, 0x69,
  36440. 0x97, 0xE3, 0x97, 0x7C, 0x83, 0x28, 0xF1, 0xF4, 0x7D, 0x03, 0xA8, 0x31,
  36441. 0x77, 0xCC, 0xD1, 0x37, 0xEF, 0x7B, 0x4A, 0x71, 0x2D, 0x11, 0x7E, 0x92,
  36442. 0xF5, 0x67, 0xB7, 0x56, 0xBA, 0x28, 0xF8, 0xD6, 0xCE, 0x2A, 0x71, 0xE3,
  36443. 0x70, 0x6B, 0x09, 0x0F, 0x67, 0x6F, 0x7A, 0xE0, 0x89, 0xF6, 0x5E, 0x23,
  36444. 0x0C, 0x0A, 0x44, 0x4E, 0x65, 0x8E, 0x7B, 0x68, 0xD0, 0xAD, 0x76, 0x3E,
  36445. 0x2A, 0x0E, 0xA2, 0x05, 0x11, 0x74, 0x24, 0x08, 0x60, 0xED, 0x9F, 0x98,
  36446. 0x18, 0xE9, 0x91, 0x58, 0x36, 0xEC, 0xEC, 0x25, 0x6B, 0xBA, 0x9C, 0x87,
  36447. 0x38, 0x68, 0xDC, 0xDC, 0x15, 0x6F, 0x20, 0x68, 0xC4, 0xBF, 0x05, 0x5B,
  36448. 0x4A, 0x0C, 0x44, 0x2B, 0x92, 0x3F, 0x10, 0x99, 0xDC, 0xF6, 0x6C, 0x0E,
  36449. 0x34, 0x26, 0x6E, 0x6D, 0x4E, 0x12, 0xBC, 0x60, 0x8F, 0x27, 0x1D, 0x7A,
  36450. 0x00, 0x50, 0xBE, 0x23, 0xDE, 0x48, 0x47, 0x9F, 0xAD, 0x2F, 0x94, 0x3D,
  36451. 0x16, 0x73, 0x48, 0x6B, 0xC8, 0x97, 0xE6, 0xB4, 0xB3, 0x4B, 0xE1, 0x68,
  36452. 0x08, 0xC3, 0xE5, 0x34, 0x5F, 0x9B, 0xDA, 0xAB, 0xCA, 0x6D, 0x55, 0x32,
  36453. 0xEF, 0x6C, 0xEF, 0x9B, 0x8B, 0x5B, 0xC7, 0xF0, 0xC2, 0x0F, 0x8E, 0x93,
  36454. 0x09, 0x60, 0x3C, 0x0B, 0xDC, 0xBD, 0xDB, 0x4A, 0x2D, 0xD0, 0x98, 0xAA,
  36455. 0xAB, 0x6C, 0x6F, 0x6D, 0x6B, 0x6A, 0x5C, 0x33, 0xAC, 0xAD, 0xA8, 0x1B,
  36456. 0x38, 0x5D, 0x9F, 0xDA, 0xE7, 0x70, 0x07
  36457. };
  36458. pt = ca_key_der_2048;
  36459. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &pt,
  36460. sizeof_ca_key_der_2048));
  36461. pt = client_cert_der_2048;
  36462. ExpectNotNull(x509 = wolfSSL_d2i_X509(NULL, &pt,
  36463. sizeof_client_cert_der_2048));
  36464. pt = ca_cert_der_2048;
  36465. ExpectNotNull(ca = wolfSSL_d2i_X509(NULL, &pt, sizeof_ca_cert_der_2048));
  36466. ExpectNotNull(name = wolfSSL_X509_get_subject_name(ca));
  36467. ExpectIntEQ(wolfSSL_X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  36468. #ifndef NO_ASN_TIME
  36469. t = (time_t)30 * year + 45 * day + 20 * hour + 30 * mini + 7 * day;
  36470. ExpectNotNull(notBefore = wolfSSL_ASN1_TIME_adj(NULL, t, 0, 0));
  36471. ExpectNotNull(notAfter = wolfSSL_ASN1_TIME_adj(NULL, t, 365, 0));
  36472. ExpectIntEQ(notAfter->length, 13);
  36473. ExpectTrue(wolfSSL_X509_set_notBefore(x509, notBefore));
  36474. ExpectTrue(wolfSSL_X509_set_notAfter(x509, notAfter));
  36475. #endif
  36476. ExpectIntGT(wolfSSL_X509_sign(x509, priv, EVP_sha256()), 0);
  36477. ExpectNotNull((der = wolfSSL_X509_get_der(x509, &derSz)));
  36478. ExpectIntEQ(derSz, sizeof(expected));
  36479. #ifndef NO_ASN_TIME
  36480. ExpectIntEQ(XMEMCMP(der, expected, derSz), 0);
  36481. #endif
  36482. wolfSSL_X509_free(ca);
  36483. wolfSSL_X509_free(x509);
  36484. wolfSSL_EVP_PKEY_free(priv);
  36485. #ifndef NO_ASN_TIME
  36486. wolfSSL_ASN1_TIME_free(notBefore);
  36487. wolfSSL_ASN1_TIME_free(notAfter);
  36488. #endif
  36489. #endif
  36490. return EXPECT_RESULT();
  36491. }
  36492. static int test_wolfSSL_X509_sign(void)
  36493. {
  36494. EXPECT_DECLS;
  36495. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_ASN_TIME) && \
  36496. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA)
  36497. int ret = 0;
  36498. char *cn = NULL;
  36499. word32 cnSz = 0;
  36500. X509_NAME *name = NULL;
  36501. X509 *x509 = NULL;
  36502. X509 *ca = NULL;
  36503. DecodedCert dCert;
  36504. EVP_PKEY *pub = NULL;
  36505. EVP_PKEY *priv = NULL;
  36506. EVP_MD_CTX *mctx = NULL;
  36507. #if defined(USE_CERT_BUFFERS_1024)
  36508. const unsigned char* rsaPriv = client_key_der_1024;
  36509. const unsigned char* rsaPub = client_keypub_der_1024;
  36510. const unsigned char* certIssuer = client_cert_der_1024;
  36511. long clientKeySz = (long)sizeof_client_key_der_1024;
  36512. long clientPubKeySz = (long)sizeof_client_keypub_der_1024;
  36513. long certIssuerSz = (long)sizeof_client_cert_der_1024;
  36514. #elif defined(USE_CERT_BUFFERS_2048)
  36515. const unsigned char* rsaPriv = client_key_der_2048;
  36516. const unsigned char* rsaPub = client_keypub_der_2048;
  36517. const unsigned char* certIssuer = client_cert_der_2048;
  36518. long clientKeySz = (long)sizeof_client_key_der_2048;
  36519. long clientPubKeySz = (long)sizeof_client_keypub_der_2048;
  36520. long certIssuerSz = (long)sizeof_client_cert_der_2048;
  36521. #endif
  36522. byte sn[16];
  36523. int snSz = sizeof(sn);
  36524. /* Set X509_NAME fields */
  36525. ExpectNotNull(name = X509_NAME_new());
  36526. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "countryName", MBSTRING_UTF8,
  36527. (byte*)"US", 2, -1, 0), SSL_SUCCESS);
  36528. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  36529. (byte*)"wolfssl.com", 11, -1, 0), SSL_SUCCESS);
  36530. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  36531. (byte*)"support@wolfssl.com", 19, -1, 0), SSL_SUCCESS);
  36532. /* Get private and public keys */
  36533. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  36534. clientKeySz));
  36535. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &rsaPub, clientPubKeySz));
  36536. ExpectNotNull(x509 = X509_new());
  36537. /* Set version 3 */
  36538. ExpectIntNE(X509_set_version(x509, 2L), 0);
  36539. /* Set subject name, add pubkey, and sign certificate */
  36540. ExpectIntEQ(X509_set_subject_name(x509, name), SSL_SUCCESS);
  36541. X509_NAME_free(name);
  36542. name = NULL;
  36543. ExpectIntEQ(X509_set_pubkey(x509, pub), SSL_SUCCESS);
  36544. #ifdef WOLFSSL_ALT_NAMES
  36545. /* Add some subject alt names */
  36546. ExpectIntNE(wolfSSL_X509_add_altname(NULL,
  36547. "ipsum", ASN_DNS_TYPE), SSL_SUCCESS);
  36548. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  36549. NULL, ASN_DNS_TYPE), SSL_SUCCESS);
  36550. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  36551. "sphygmomanometer",
  36552. ASN_DNS_TYPE), SSL_SUCCESS);
  36553. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  36554. "supercalifragilisticexpialidocious",
  36555. ASN_DNS_TYPE), SSL_SUCCESS);
  36556. ExpectIntEQ(wolfSSL_X509_add_altname(x509,
  36557. "Llanfairpwllgwyngyllgogerychwyrndrobwllllantysiliogogogoch",
  36558. ASN_DNS_TYPE), SSL_SUCCESS);
  36559. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  36560. {
  36561. unsigned char ip4_type[] = {127,128,0,255};
  36562. unsigned char ip6_type[] = {0xdd, 0xcc, 0xba, 0xab,
  36563. 0xff, 0xee, 0x99, 0x88,
  36564. 0x77, 0x66, 0x55, 0x44,
  36565. 0x00, 0x33, 0x22, 0x11};
  36566. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip4_type,
  36567. sizeof(ip4_type), ASN_IP_TYPE), SSL_SUCCESS);
  36568. ExpectIntEQ(wolfSSL_X509_add_altname_ex(x509, (char*)ip6_type,
  36569. sizeof(ip6_type), ASN_IP_TYPE), SSL_SUCCESS);
  36570. }
  36571. #endif
  36572. #endif /* WOLFSSL_ALT_NAMES */
  36573. {
  36574. ASN1_UTCTIME* infinite_past = NULL;
  36575. ExpectNotNull(infinite_past = ASN1_UTCTIME_set(NULL, 0));
  36576. ExpectIntEQ(X509_set1_notBefore(x509, infinite_past), 1);
  36577. ASN1_UTCTIME_free(infinite_past);
  36578. }
  36579. /* test valid sign case */
  36580. ExpectIntGT(ret = X509_sign(x509, priv, EVP_sha256()), 0);
  36581. /* test valid X509_sign_ctx case */
  36582. ExpectNotNull(mctx = EVP_MD_CTX_new());
  36583. ExpectIntEQ(EVP_DigestSignInit(mctx, NULL, EVP_sha256(), NULL, priv), 1);
  36584. ExpectIntGT(X509_sign_ctx(x509, mctx), 0);
  36585. #if defined(OPENSSL_ALL) && defined(WOLFSSL_ALT_NAMES)
  36586. ExpectIntEQ(X509_get_ext_count(x509), 1);
  36587. #endif
  36588. #if defined(WOLFSSL_ALT_NAMES) && (defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME))
  36589. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.128.0.255", 0), 1);
  36590. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "DDCC:BAAB:FFEE:9988:7766:5544:0033:2211", 0), 1);
  36591. #endif
  36592. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, sn, &snSz),
  36593. WOLFSSL_SUCCESS);
  36594. DEBUG_WRITE_CERT_X509(x509, "signed.pem");
  36595. /* Variation in size depends on ASN.1 encoding when MSB is set.
  36596. * WOLFSSL_ASN_TEMPLATE code does not generate a serial number
  36597. * with the MSB set. See GenerateInteger in asn.c */
  36598. #ifndef USE_CERT_BUFFERS_1024
  36599. #ifndef WOLFSSL_ALT_NAMES
  36600. /* Valid case - size should be 781-786 with 16 byte serial number */
  36601. ExpectTrue((781 + snSz <= ret) && (ret <= 781 + 5 + snSz));
  36602. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  36603. /* Valid case - size should be 955-960 with 16 byte serial number */
  36604. ExpectTrue((939 + snSz <= ret) && (ret <= 939 + 5 + snSz));
  36605. #else
  36606. /* Valid case - size should be 926-931 with 16 byte serial number */
  36607. ExpectTrue((910 + snSz <= ret) && (ret <= 910 + 5 + snSz));
  36608. #endif
  36609. #else
  36610. #ifndef WOLFSSL_ALT_NAMES
  36611. /* Valid case - size should be 537-542 with 16 byte serial number */
  36612. ExpectTrue((521 + snSz <= ret) && (ret <= 521 + 5 + snSz));
  36613. #elif defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  36614. /* Valid case - size should be 695-670 with 16 byte serial number */
  36615. ExpectTrue((679 + snSz <= ret) && (ret <= 679 + 5 + snSz));
  36616. #else
  36617. /* Valid case - size should be 666-671 with 16 byte serial number */
  36618. ExpectTrue((650 + snSz <= ret) && (ret <= 650 + 5 + snSz));
  36619. #endif
  36620. #endif
  36621. /* check that issuer name is as expected after signature */
  36622. InitDecodedCert(&dCert, certIssuer, (word32)certIssuerSz, 0);
  36623. ExpectIntEQ(ParseCert(&dCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  36624. ExpectNotNull(ca = d2i_X509(NULL, &certIssuer, (int)certIssuerSz));
  36625. ExpectNotNull(name = X509_get_subject_name(ca));
  36626. cnSz = X509_NAME_get_sz(name);
  36627. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  36628. ExpectNotNull(cn = X509_NAME_oneline(name, cn, (int)cnSz));
  36629. ExpectIntEQ(0, XSTRNCMP(cn, dCert.subject, XSTRLEN(cn)));
  36630. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  36631. cn = NULL;
  36632. #ifdef WOLFSSL_MULTI_ATTRIB
  36633. /* test adding multiple OU's to the signer */
  36634. ExpectNotNull(name = X509_get_subject_name(ca));
  36635. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  36636. (byte*)"OU1", 3, -1, 0), SSL_SUCCESS);
  36637. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "OU", MBSTRING_UTF8,
  36638. (byte*)"OU2", 3, -1, 0), SSL_SUCCESS);
  36639. ExpectIntGT(X509_sign(ca, priv, EVP_sha256()), 0);
  36640. #endif
  36641. ExpectNotNull(name = X509_get_subject_name(ca));
  36642. ExpectIntEQ(X509_set_issuer_name(x509, name), SSL_SUCCESS);
  36643. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  36644. ExpectNotNull(name = X509_get_issuer_name(x509));
  36645. cnSz = X509_NAME_get_sz(name);
  36646. ExpectNotNull(cn = (char*)XMALLOC(cnSz, HEAP_HINT, DYNAMIC_TYPE_OPENSSL));
  36647. ExpectNotNull(cn = X509_NAME_oneline(name, cn, (int)cnSz));
  36648. /* compare and don't include the multi-attrib "/OU=OU1/OU=OU2" above */
  36649. ExpectIntEQ(0, XSTRNCMP(cn, dCert.issuer, XSTRLEN(dCert.issuer)));
  36650. XFREE(cn, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  36651. cn = NULL;
  36652. FreeDecodedCert(&dCert);
  36653. /* Test invalid parameters */
  36654. ExpectIntEQ(X509_sign(NULL, priv, EVP_sha256()), 0);
  36655. ExpectIntEQ(X509_sign(x509, NULL, EVP_sha256()), 0);
  36656. ExpectIntEQ(X509_sign(x509, priv, NULL), 0);
  36657. ExpectIntEQ(X509_sign_ctx(NULL, mctx), 0);
  36658. EVP_MD_CTX_free(mctx);
  36659. mctx = NULL;
  36660. ExpectNotNull(mctx = EVP_MD_CTX_new());
  36661. ExpectIntEQ(X509_sign_ctx(x509, mctx), 0);
  36662. ExpectIntEQ(X509_sign_ctx(x509, NULL), 0);
  36663. /* test invalid version number */
  36664. #if defined(OPENSSL_ALL)
  36665. ExpectIntNE(X509_set_version(x509, 6L), 0);
  36666. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  36667. /* uses ParseCert which fails on bad version number */
  36668. ExpectIntEQ(X509_get_ext_count(x509), SSL_FAILURE);
  36669. #endif
  36670. EVP_MD_CTX_free(mctx);
  36671. EVP_PKEY_free(priv);
  36672. EVP_PKEY_free(pub);
  36673. X509_free(x509);
  36674. X509_free(ca);
  36675. #endif
  36676. return EXPECT_RESULT();
  36677. }
  36678. static int test_wolfSSL_X509_get0_tbs_sigalg(void)
  36679. {
  36680. EXPECT_DECLS;
  36681. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  36682. X509* x509 = NULL;
  36683. const X509_ALGOR* alg;
  36684. ExpectNotNull(x509 = X509_new());
  36685. ExpectNull(alg = X509_get0_tbs_sigalg(NULL));
  36686. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  36687. X509_free(x509);
  36688. #endif
  36689. return EXPECT_RESULT();
  36690. }
  36691. static int test_wolfSSL_X509_ALGOR_get0(void)
  36692. {
  36693. EXPECT_DECLS;
  36694. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  36695. !defined(NO_SHA256) && !defined(NO_RSA)
  36696. X509* x509 = NULL;
  36697. const ASN1_OBJECT* obj = NULL;
  36698. const X509_ALGOR* alg = NULL;
  36699. int pptype = 0;
  36700. const void *ppval = NULL;
  36701. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  36702. SSL_FILETYPE_PEM));
  36703. ExpectNotNull(alg = X509_get0_tbs_sigalg(x509));
  36704. /* Invalid case */
  36705. X509_ALGOR_get0(&obj, NULL, NULL, NULL);
  36706. ExpectNull(obj);
  36707. /* Valid case */
  36708. X509_ALGOR_get0(&obj, &pptype, &ppval, alg);
  36709. ExpectNotNull(obj);
  36710. ExpectNull(ppval);
  36711. ExpectIntNE(pptype, 0);
  36712. /* Make sure NID of X509_ALGOR is Sha256 with RSA */
  36713. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256WithRSAEncryption);
  36714. X509_free(x509);
  36715. #endif
  36716. return EXPECT_RESULT();
  36717. }
  36718. static int test_wolfSSL_X509_VERIFY_PARAM(void)
  36719. {
  36720. EXPECT_DECLS;
  36721. #if defined(OPENSSL_EXTRA)
  36722. X509_VERIFY_PARAM *paramTo = NULL;
  36723. X509_VERIFY_PARAM *paramFrom = NULL;
  36724. char testIPv4[] = "127.0.0.1";
  36725. char testIPv6[] = "0001:0000:0000:0000:0000:0000:0000:0000/32";
  36726. char testhostName1[] = "foo.hoge.com";
  36727. char testhostName2[] = "foobar.hoge.com";
  36728. ExpectNotNull(paramTo = X509_VERIFY_PARAM_new());
  36729. ExpectNotNull(XMEMSET(paramTo, 0, sizeof(X509_VERIFY_PARAM)));
  36730. ExpectNotNull(paramFrom = X509_VERIFY_PARAM_new());
  36731. ExpectNotNull(XMEMSET(paramFrom, 0, sizeof(X509_VERIFY_PARAM)));
  36732. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramFrom, testhostName1,
  36733. (int)XSTRLEN(testhostName1)), 1);
  36734. ExpectIntEQ(0, XSTRNCMP(paramFrom->hostName, testhostName1,
  36735. (int)XSTRLEN(testhostName1)));
  36736. X509_VERIFY_PARAM_set_hostflags(NULL, 0x00);
  36737. X509_VERIFY_PARAM_set_hostflags(paramFrom, 0x01);
  36738. ExpectIntEQ(0x01, paramFrom->hostFlags);
  36739. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(NULL, testIPv4), 0);
  36740. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv4), 1);
  36741. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  36742. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, NULL), 1);
  36743. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramFrom, testIPv6), 1);
  36744. ExpectIntEQ(0, XSTRNCMP(paramFrom->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36745. /* null pointer */
  36746. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, paramFrom), 0);
  36747. /* in the case of "from" null, returns success */
  36748. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, NULL), 1);
  36749. ExpectIntEQ(X509_VERIFY_PARAM_set1(NULL, NULL), 0);
  36750. /* inherit flags test : VPARAM_DEFAULT */
  36751. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  36752. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  36753. (int)XSTRLEN(testhostName1)));
  36754. ExpectIntEQ(0x01, paramTo->hostFlags);
  36755. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36756. /* inherit flags test : VPARAM OVERWRITE */
  36757. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  36758. (int)XSTRLEN(testhostName2)), 1);
  36759. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  36760. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  36761. if (paramTo != NULL) {
  36762. paramTo->inherit_flags = X509_VP_FLAG_OVERWRITE;
  36763. }
  36764. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  36765. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  36766. (int)XSTRLEN(testhostName1)));
  36767. ExpectIntEQ(0x01, paramTo->hostFlags);
  36768. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36769. /* inherit flags test : VPARAM_RESET_FLAGS */
  36770. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  36771. (int)XSTRLEN(testhostName2)), 1);
  36772. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  36773. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x10);
  36774. if (paramTo != NULL) {
  36775. paramTo->inherit_flags = X509_VP_FLAG_RESET_FLAGS;
  36776. }
  36777. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  36778. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName1,
  36779. (int)XSTRLEN(testhostName1)));
  36780. ExpectIntEQ(0x01, paramTo->hostFlags);
  36781. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv6, WOLFSSL_MAX_IPSTR));
  36782. /* inherit flags test : VPARAM_LOCKED */
  36783. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(paramTo, testhostName2,
  36784. (int)XSTRLEN(testhostName2)), 1);
  36785. ExpectIntEQ(X509_VERIFY_PARAM_set1_ip_asc(paramTo, testIPv4), 1);
  36786. X509_VERIFY_PARAM_set_hostflags(paramTo, 0x00);
  36787. if (paramTo != NULL) {
  36788. paramTo->inherit_flags = X509_VP_FLAG_LOCKED;
  36789. }
  36790. ExpectIntEQ(X509_VERIFY_PARAM_set1(paramTo, paramFrom), 1);
  36791. ExpectIntEQ(0, XSTRNCMP(paramTo->hostName, testhostName2,
  36792. (int)XSTRLEN(testhostName2)));
  36793. ExpectIntEQ(0x00, paramTo->hostFlags);
  36794. ExpectIntEQ(0, XSTRNCMP(paramTo->ipasc, testIPv4, WOLFSSL_MAX_IPSTR));
  36795. /* test for incorrect parameters */
  36796. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, X509_V_FLAG_CRL_CHECK_ALL),
  36797. 0);
  36798. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(NULL, 0), 0);
  36799. /* inherit flags test : VPARAM_ONCE, not testable yet */
  36800. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(paramTo, X509_V_FLAG_CRL_CHECK_ALL),
  36801. 1);
  36802. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo),
  36803. X509_V_FLAG_CRL_CHECK_ALL);
  36804. ExpectIntEQ(X509_VERIFY_PARAM_clear_flags(paramTo,
  36805. X509_V_FLAG_CRL_CHECK_ALL), 1);
  36806. ExpectIntEQ(X509_VERIFY_PARAM_get_flags(paramTo), 0);
  36807. X509_VERIFY_PARAM_free(paramTo);
  36808. X509_VERIFY_PARAM_free(paramFrom);
  36809. X509_VERIFY_PARAM_free(NULL); /* to confirm NULL parameter gives no harm */
  36810. #endif
  36811. return EXPECT_RESULT();
  36812. }
  36813. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  36814. !defined(WOLFSSL_HOSTNAME_VERIFY_ALT_NAME_ONLY)
  36815. static int test_wolfSSL_check_domain_verify_count = 0;
  36816. static WC_INLINE int test_wolfSSL_check_domain_verify_cb(int preverify,
  36817. WOLFSSL_X509_STORE_CTX* store)
  36818. {
  36819. EXPECT_DECLS;
  36820. ExpectIntEQ(X509_STORE_CTX_get_error(store), 0);
  36821. ExpectIntEQ(preverify, 1);
  36822. ExpectIntGT(++test_wolfSSL_check_domain_verify_count, 0);
  36823. return EXPECT_SUCCESS();
  36824. }
  36825. static int test_wolfSSL_check_domain_client_cb(WOLFSSL* ssl)
  36826. {
  36827. EXPECT_DECLS;
  36828. X509_VERIFY_PARAM *param = NULL;
  36829. ExpectNotNull(param = SSL_get0_param(ssl));
  36830. /* Domain check should only be done on the leaf cert */
  36831. X509_VERIFY_PARAM_set_hostflags(param,
  36832. X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS);
  36833. ExpectIntEQ(X509_VERIFY_PARAM_set1_host(param,
  36834. "wolfSSL Server Chain", 0), 1);
  36835. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_PEER,
  36836. test_wolfSSL_check_domain_verify_cb);
  36837. return EXPECT_RESULT();
  36838. }
  36839. static int test_wolfSSL_check_domain_server_cb(WOLFSSL_CTX* ctx)
  36840. {
  36841. EXPECT_DECLS;
  36842. /* Use a cert with different domains in chain */
  36843. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  36844. "certs/intermediate/server-chain.pem"), WOLFSSL_SUCCESS);
  36845. return EXPECT_RESULT();
  36846. }
  36847. static int test_wolfSSL_check_domain(void)
  36848. {
  36849. EXPECT_DECLS;
  36850. test_ssl_cbf func_cb_client;
  36851. test_ssl_cbf func_cb_server;
  36852. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  36853. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  36854. func_cb_client.ssl_ready = &test_wolfSSL_check_domain_client_cb;
  36855. func_cb_server.ctx_ready = &test_wolfSSL_check_domain_server_cb;
  36856. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  36857. &func_cb_server, NULL), TEST_SUCCESS);
  36858. /* Should have been called once for each cert in sent chain */
  36859. #ifdef WOLFSSL_VERIFY_CB_ALL_CERTS
  36860. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 3);
  36861. #else
  36862. ExpectIntEQ(test_wolfSSL_check_domain_verify_count, 1);
  36863. #endif
  36864. return EXPECT_RESULT();
  36865. }
  36866. #else
  36867. static int test_wolfSSL_check_domain(void)
  36868. {
  36869. EXPECT_DECLS;
  36870. return EXPECT_RESULT();
  36871. }
  36872. #endif /* OPENSSL_EXTRA && HAVE_SSL_MEMIO_TESTS_DEPENDENCIES */
  36873. static int test_wolfSSL_X509_get_X509_PUBKEY(void)
  36874. {
  36875. EXPECT_DECLS;
  36876. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD))
  36877. X509* x509 = NULL;
  36878. X509_PUBKEY* pubKey;
  36879. ExpectNotNull(x509 = X509_new());
  36880. ExpectNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(NULL));
  36881. ExpectNotNull(pubKey = wolfSSL_X509_get_X509_PUBKEY(x509));
  36882. X509_free(x509);
  36883. #endif
  36884. return EXPECT_RESULT();
  36885. }
  36886. static int test_wolfSSL_X509_PUBKEY_RSA(void)
  36887. {
  36888. EXPECT_DECLS;
  36889. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  36890. !defined(NO_SHA256) && !defined(NO_RSA)
  36891. X509* x509 = NULL;
  36892. ASN1_OBJECT* obj = NULL;
  36893. const ASN1_OBJECT* pa_oid = NULL;
  36894. X509_PUBKEY* pubKey = NULL;
  36895. X509_PUBKEY* pubKey2 = NULL;
  36896. EVP_PKEY* evpKey = NULL;
  36897. const unsigned char *pk = NULL;
  36898. int ppklen;
  36899. int pptype;
  36900. X509_ALGOR *pa = NULL;
  36901. const void *pval;
  36902. ExpectNotNull(x509 = X509_load_certificate_file(cliCertFile,
  36903. SSL_FILETYPE_PEM));
  36904. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  36905. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  36906. ExpectNotNull(pk);
  36907. ExpectNotNull(pa);
  36908. ExpectNotNull(pubKey);
  36909. ExpectIntGT(ppklen, 0);
  36910. ExpectIntEQ(OBJ_obj2nid(obj), NID_rsaEncryption);
  36911. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  36912. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  36913. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  36914. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  36915. ExpectNotNull(pk);
  36916. ExpectNotNull(pa);
  36917. ExpectIntGT(ppklen, 0);
  36918. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  36919. ExpectNotNull(pa_oid);
  36920. ExpectNull(pval);
  36921. ExpectIntEQ(pptype, V_ASN1_NULL);
  36922. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_RSA);
  36923. X509_PUBKEY_free(pubKey2);
  36924. X509_free(x509);
  36925. EVP_PKEY_free(evpKey);
  36926. #endif
  36927. return EXPECT_RESULT();
  36928. }
  36929. static int test_wolfSSL_X509_PUBKEY_EC(void)
  36930. {
  36931. EXPECT_DECLS;
  36932. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && defined(HAVE_ECC)
  36933. X509* x509 = NULL;
  36934. ASN1_OBJECT* obj = NULL;
  36935. ASN1_OBJECT* poid = NULL;
  36936. const ASN1_OBJECT* pa_oid = NULL;
  36937. X509_PUBKEY* pubKey = NULL;
  36938. X509_PUBKEY* pubKey2 = NULL;
  36939. EVP_PKEY* evpKey = NULL;
  36940. const unsigned char *pk = NULL;
  36941. int ppklen;
  36942. int pptype;
  36943. X509_ALGOR *pa = NULL;
  36944. const void *pval;
  36945. char buf[50];
  36946. ExpectNotNull(x509 = X509_load_certificate_file(cliEccCertFile,
  36947. SSL_FILETYPE_PEM));
  36948. ExpectNotNull(pubKey = X509_get_X509_PUBKEY(x509));
  36949. ExpectNotNull(evpKey = X509_PUBKEY_get(pubKey));
  36950. ExpectNotNull(pubKey2 = X509_PUBKEY_new());
  36951. ExpectIntEQ(X509_PUBKEY_set(&pubKey2, evpKey), 1);
  36952. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey2), 1);
  36953. ExpectNotNull(pk);
  36954. ExpectNotNull(pa);
  36955. ExpectIntGT(ppklen, 0);
  36956. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  36957. ExpectNotNull(pa_oid);
  36958. ExpectNotNull(pval);
  36959. ExpectIntEQ(pptype, V_ASN1_OBJECT);
  36960. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_EC);
  36961. poid = (ASN1_OBJECT *)pval;
  36962. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), poid, 0), 0);
  36963. ExpectIntEQ(OBJ_txt2nid(buf), NID_X9_62_prime256v1);
  36964. X509_PUBKEY_free(pubKey2);
  36965. X509_free(x509);
  36966. EVP_PKEY_free(evpKey);
  36967. #endif
  36968. return EXPECT_RESULT();
  36969. }
  36970. static int test_wolfSSL_X509_PUBKEY_DSA(void)
  36971. {
  36972. EXPECT_DECLS;
  36973. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && !defined(NO_DSA)
  36974. word32 bytes;
  36975. #ifdef USE_CERT_BUFFERS_1024
  36976. byte tmp[ONEK_BUF];
  36977. #elif defined(USE_CERT_BUFFERS_2048)
  36978. byte tmp[TWOK_BUF];
  36979. #else
  36980. byte tmp[TWOK_BUF];
  36981. #endif /* END USE_CERT_BUFFERS_1024 */
  36982. const unsigned char* dsaKeyDer = tmp;
  36983. ASN1_OBJECT* obj = NULL;
  36984. ASN1_STRING* str;
  36985. const ASN1_OBJECT* pa_oid = NULL;
  36986. X509_PUBKEY* pubKey = NULL;
  36987. EVP_PKEY* evpKey = NULL;
  36988. const unsigned char *pk = NULL;
  36989. int ppklen, pptype;
  36990. X509_ALGOR *pa = NULL;
  36991. const void *pval;
  36992. #ifdef USE_CERT_BUFFERS_1024
  36993. XMEMSET(tmp, 0, sizeof(tmp));
  36994. XMEMCPY(tmp, dsa_key_der_1024, sizeof_dsa_key_der_1024);
  36995. bytes = sizeof_dsa_key_der_1024;
  36996. #elif defined(USE_CERT_BUFFERS_2048)
  36997. XMEMSET(tmp, 0, sizeof(tmp));
  36998. XMEMCPY(tmp, dsa_key_der_2048, sizeof_dsa_key_der_2048);
  36999. bytes = sizeof_dsa_key_der_2048;
  37000. #else
  37001. {
  37002. XFILE fp = XBADFILE;
  37003. XMEMSET(tmp, 0, sizeof(tmp));
  37004. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  37005. ExpectIntGT(bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  37006. if (fp != XBADFILE)
  37007. XFCLOSE(fp);
  37008. }
  37009. #endif
  37010. /* Initialize pkey with der format dsa key */
  37011. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &evpKey, &dsaKeyDer, bytes));
  37012. ExpectNotNull(pubKey = X509_PUBKEY_new());
  37013. ExpectIntEQ(X509_PUBKEY_set(&pubKey, evpKey), 1);
  37014. ExpectIntEQ(X509_PUBKEY_get0_param(&obj, &pk, &ppklen, &pa, pubKey), 1);
  37015. ExpectNotNull(pk);
  37016. ExpectNotNull(pa);
  37017. ExpectIntGT(ppklen, 0);
  37018. X509_ALGOR_get0(&pa_oid, &pptype, &pval, pa);
  37019. ExpectNotNull(pa_oid);
  37020. ExpectNotNull(pval);
  37021. ExpectIntEQ(pptype, V_ASN1_SEQUENCE);
  37022. ExpectIntEQ(OBJ_obj2nid(pa_oid), EVP_PKEY_DSA);
  37023. str = (ASN1_STRING *)pval;
  37024. DEBUG_WRITE_DER(ASN1_STRING_data(str), ASN1_STRING_length(str), "str.der");
  37025. #ifdef USE_CERT_BUFFERS_1024
  37026. ExpectIntEQ(ASN1_STRING_length(str), 291);
  37027. #else
  37028. ExpectIntEQ(ASN1_STRING_length(str), 549);
  37029. #endif /* END USE_CERT_BUFFERS_1024 */
  37030. X509_PUBKEY_free(pubKey);
  37031. EVP_PKEY_free(evpKey);
  37032. #endif
  37033. return EXPECT_RESULT();
  37034. }
  37035. static int test_wolfSSL_BUF(void)
  37036. {
  37037. EXPECT_DECLS;
  37038. #if defined(OPENSSL_EXTRA)
  37039. BUF_MEM* buf = NULL;
  37040. ExpectNotNull(buf = BUF_MEM_new());
  37041. ExpectIntEQ(BUF_MEM_grow(buf, 10), 10);
  37042. ExpectIntEQ(BUF_MEM_grow(buf, -1), 0);
  37043. BUF_MEM_free(buf);
  37044. #endif
  37045. return EXPECT_RESULT();
  37046. }
  37047. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  37048. static int stub_rand_seed(const void *buf, int num)
  37049. {
  37050. (void)buf;
  37051. (void)num;
  37052. return 123;
  37053. }
  37054. static int stub_rand_bytes(unsigned char *buf, int num)
  37055. {
  37056. (void)buf;
  37057. (void)num;
  37058. return 456;
  37059. }
  37060. static byte* was_stub_rand_cleanup_called(void)
  37061. {
  37062. static byte was_called = 0;
  37063. return &was_called;
  37064. }
  37065. static void stub_rand_cleanup(void)
  37066. {
  37067. byte* was_called = was_stub_rand_cleanup_called();
  37068. *was_called = 1;
  37069. return;
  37070. }
  37071. static byte* was_stub_rand_add_called(void)
  37072. {
  37073. static byte was_called = 0;
  37074. return &was_called;
  37075. }
  37076. static int stub_rand_add(const void *buf, int num, double entropy)
  37077. {
  37078. byte* was_called = was_stub_rand_add_called();
  37079. (void)buf;
  37080. (void)num;
  37081. (void)entropy;
  37082. *was_called = 1;
  37083. return 0;
  37084. }
  37085. static int stub_rand_pseudo_bytes(unsigned char *buf, int num)
  37086. {
  37087. (void)buf;
  37088. (void)num;
  37089. return 9876;
  37090. }
  37091. static int stub_rand_status(void)
  37092. {
  37093. return 5432;
  37094. }
  37095. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  37096. static int test_wolfSSL_RAND_set_rand_method(void)
  37097. {
  37098. EXPECT_DECLS;
  37099. #if defined(OPENSSL_EXTRA) && !defined(WOLFSSL_NO_OPENSSL_RAND_CB)
  37100. RAND_METHOD rand_methods = {NULL, NULL, NULL, NULL, NULL, NULL};
  37101. unsigned char* buf = NULL;
  37102. int num = 0;
  37103. double entropy = 0;
  37104. int ret;
  37105. byte* was_cleanup_called = was_stub_rand_cleanup_called();
  37106. byte* was_add_called = was_stub_rand_add_called();
  37107. ExpectNotNull(buf = (byte*)XMALLOC(32 * sizeof(byte), NULL,
  37108. DYNAMIC_TYPE_TMP_BUFFER));
  37109. ExpectIntNE(wolfSSL_RAND_status(), 5432);
  37110. ExpectIntEQ(*was_cleanup_called, 0);
  37111. RAND_cleanup();
  37112. ExpectIntEQ(*was_cleanup_called, 0);
  37113. rand_methods.seed = &stub_rand_seed;
  37114. rand_methods.bytes = &stub_rand_bytes;
  37115. rand_methods.cleanup = &stub_rand_cleanup;
  37116. rand_methods.add = &stub_rand_add;
  37117. rand_methods.pseudorand = &stub_rand_pseudo_bytes;
  37118. rand_methods.status = &stub_rand_status;
  37119. ExpectIntEQ(RAND_set_rand_method(&rand_methods), WOLFSSL_SUCCESS);
  37120. ExpectIntEQ(RAND_seed(buf, num), 123);
  37121. ExpectIntEQ(RAND_bytes(buf, num), 456);
  37122. ExpectIntEQ(RAND_pseudo_bytes(buf, num), 9876);
  37123. ExpectIntEQ(RAND_status(), 5432);
  37124. ExpectIntEQ(*was_add_called, 0);
  37125. /* The function pointer for RAND_add returns int, but RAND_add itself
  37126. * returns void. */
  37127. RAND_add(buf, num, entropy);
  37128. ExpectIntEQ(*was_add_called, 1);
  37129. was_add_called = 0;
  37130. ExpectIntEQ(*was_cleanup_called, 0);
  37131. RAND_cleanup();
  37132. ExpectIntEQ(*was_cleanup_called, 1);
  37133. *was_cleanup_called = 0;
  37134. ret = RAND_set_rand_method(NULL);
  37135. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  37136. ExpectIntNE(RAND_status(), 5432);
  37137. ExpectIntEQ(*was_cleanup_called, 0);
  37138. RAND_cleanup();
  37139. ExpectIntEQ(*was_cleanup_called, 0);
  37140. RAND_set_rand_method(NULL);
  37141. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  37142. #endif /* OPENSSL_EXTRA && !WOLFSSL_NO_OPENSSL_RAND_CB */
  37143. return EXPECT_RESULT();
  37144. }
  37145. static int test_wolfSSL_RAND_bytes(void)
  37146. {
  37147. EXPECT_DECLS;
  37148. #if defined(OPENSSL_EXTRA)
  37149. const int size1 = RNG_MAX_BLOCK_LEN; /* in bytes */
  37150. const int size2 = RNG_MAX_BLOCK_LEN + 1; /* in bytes */
  37151. const int size3 = RNG_MAX_BLOCK_LEN * 2; /* in bytes */
  37152. const int size4 = RNG_MAX_BLOCK_LEN * 4; /* in bytes */
  37153. int max_bufsize;
  37154. byte *my_buf = NULL;
  37155. /* sanity check */
  37156. ExpectIntEQ(RAND_bytes(NULL, 16), 0);
  37157. ExpectIntEQ(RAND_bytes(NULL, 0), 0);
  37158. max_bufsize = size4;
  37159. ExpectNotNull(my_buf = (byte*)XMALLOC(max_bufsize * sizeof(byte), NULL,
  37160. DYNAMIC_TYPE_TMP_BUFFER));
  37161. ExpectIntEQ(RAND_bytes(my_buf, 0), 1);
  37162. ExpectIntEQ(RAND_bytes(my_buf, -1), 0);
  37163. ExpectNotNull(XMEMSET(my_buf, 0, max_bufsize));
  37164. ExpectIntEQ(RAND_bytes(my_buf, size1), 1);
  37165. ExpectIntEQ(RAND_bytes(my_buf, size2), 1);
  37166. ExpectIntEQ(RAND_bytes(my_buf, size3), 1);
  37167. ExpectIntEQ(RAND_bytes(my_buf, size4), 1);
  37168. XFREE(my_buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  37169. #endif
  37170. return EXPECT_RESULT();
  37171. }
  37172. static int test_wolfSSL_RAND(void)
  37173. {
  37174. EXPECT_DECLS;
  37175. #if defined(OPENSSL_EXTRA)
  37176. byte seed[16];
  37177. XMEMSET(seed, 0, sizeof(seed));
  37178. /* No global methods set. */
  37179. ExpectIntEQ(RAND_seed(seed, sizeof(seed)), 1);
  37180. ExpectIntEQ(RAND_poll(), 1);
  37181. RAND_cleanup();
  37182. ExpectIntEQ(RAND_egd(NULL), -1);
  37183. #ifndef NO_FILESYSTEM
  37184. {
  37185. char fname[100];
  37186. ExpectNotNull(RAND_file_name(fname, (sizeof(fname) - 1)));
  37187. ExpectIntEQ(RAND_write_file(NULL), 0);
  37188. }
  37189. #endif
  37190. #endif
  37191. return EXPECT_RESULT();
  37192. }
  37193. static int test_wolfSSL_PKCS8_Compat(void)
  37194. {
  37195. EXPECT_DECLS;
  37196. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && defined(HAVE_ECC) && \
  37197. !defined(NO_BIO)
  37198. PKCS8_PRIV_KEY_INFO* pt = NULL;
  37199. BIO* bio = NULL;
  37200. XFILE f = XBADFILE;
  37201. int bytes;
  37202. char pkcs8_buffer[512];
  37203. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  37204. EVP_PKEY *pkey = NULL;
  37205. #endif
  37206. /* file from wolfssl/certs/ directory */
  37207. ExpectTrue((f = XFOPEN("./certs/ecc-keyPkcs8.pem", "rb")) != XBADFILE);
  37208. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer), f)),
  37209. 0);
  37210. if (f != XBADFILE)
  37211. XFCLOSE(f);
  37212. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  37213. ExpectNotNull(pt = d2i_PKCS8_PRIV_KEY_INFO_bio(bio, NULL));
  37214. #if defined(OPENSSL_ALL) || defined(WOLFSSL_WPAS_SMALL)
  37215. ExpectNotNull(pkey = EVP_PKCS82PKEY(pt));
  37216. ExpectIntEQ(EVP_PKEY_type(pkey->type), EVP_PKEY_EC);
  37217. /* gets PKCS8 pointer to pkey */
  37218. ExpectNotNull(EVP_PKEY2PKCS8(pkey));
  37219. EVP_PKEY_free(pkey);
  37220. #endif
  37221. BIO_free(bio);
  37222. PKCS8_PRIV_KEY_INFO_free(pt);
  37223. #endif
  37224. return EXPECT_RESULT();
  37225. }
  37226. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  37227. static int NoPasswordCallBack(char* passwd, int sz, int rw, void* userdata)
  37228. {
  37229. (void)passwd;
  37230. (void)sz;
  37231. (void)rw;
  37232. (void)userdata;
  37233. return -1;
  37234. }
  37235. #endif
  37236. static int test_wolfSSL_PKCS8_d2i(void)
  37237. {
  37238. EXPECT_DECLS;
  37239. #if !defined(HAVE_FIPS) && defined(OPENSSL_EXTRA)
  37240. /* This test ends up using HMAC as a part of PBKDF2, and HMAC
  37241. * requires a 12 byte password in FIPS mode. This test ends up
  37242. * trying to use an 8 byte password. */
  37243. #ifndef NO_FILESYSTEM
  37244. unsigned char pkcs8_buffer[2048];
  37245. const unsigned char* p = NULL;
  37246. int bytes = 0;
  37247. XFILE file = XBADFILE;
  37248. WOLFSSL_EVP_PKEY* pkey = NULL;
  37249. #ifndef NO_BIO
  37250. BIO* bio = NULL;
  37251. #if defined(OPENSSL_ALL) && \
  37252. ((!defined(NO_RSA) && !defined(NO_DES3)) || \
  37253. defined(HAVE_ECC)) && \
  37254. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37255. WOLFSSL_EVP_PKEY* evpPkey = NULL;
  37256. #endif
  37257. #endif
  37258. #ifndef NO_RSA
  37259. const char rsaDerPkcs8File[] = "./certs/server-keyPkcs8.der";
  37260. const char rsaPemPkcs8File[] = "./certs/server-keyPkcs8.pem";
  37261. #ifndef NO_DES3
  37262. const char rsaDerPkcs8EncFile[] = "./certs/server-keyPkcs8Enc.der";
  37263. #endif
  37264. #endif /* NO_RSA */
  37265. #ifdef HAVE_ECC
  37266. const char ecDerPkcs8File[] = "certs/ecc-keyPkcs8.der";
  37267. const char ecPemPkcs8File[] = "certs/ecc-keyPkcs8.pem";
  37268. #ifndef NO_DES3
  37269. const char ecDerPkcs8EncFile[] = "certs/ecc-keyPkcs8Enc.der";
  37270. #endif
  37271. #endif /* HAVE_ECC */
  37272. #endif /* !NO_FILESYSTEM */
  37273. #if defined(OPENSSL_ALL) && (!defined(NO_RSA) || defined(HAVE_ECC))
  37274. #ifndef NO_RSA
  37275. #ifdef USE_CERT_BUFFERS_1024
  37276. const unsigned char* rsa = (unsigned char*)server_key_der_1024;
  37277. int rsaSz = sizeof_server_key_der_1024;
  37278. #else
  37279. const unsigned char* rsa = (unsigned char*)server_key_der_2048;
  37280. int rsaSz = sizeof_server_key_der_2048;
  37281. #endif
  37282. #endif
  37283. #ifdef HAVE_ECC
  37284. const unsigned char* ec = (unsigned char*)ecc_key_der_256;
  37285. int ecSz = sizeof_ecc_key_der_256;
  37286. #endif
  37287. #endif /* OPENSSL_ALL && (!NO_RSA || HAVE_ECC) */
  37288. #ifndef NO_FILESYSTEM
  37289. (void)pkcs8_buffer;
  37290. (void)p;
  37291. (void)bytes;
  37292. (void)file;
  37293. #ifndef NO_BIO
  37294. (void)bio;
  37295. #endif
  37296. #endif
  37297. #ifdef OPENSSL_ALL
  37298. #ifndef NO_RSA
  37299. /* Try to auto-detect normal RSA private key */
  37300. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &rsa, rsaSz));
  37301. EVP_PKEY_free(pkey);
  37302. pkey = NULL;
  37303. #endif
  37304. #ifdef HAVE_ECC
  37305. /* Try to auto-detect normal EC private key */
  37306. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &ec, ecSz));
  37307. EVP_PKEY_free(pkey);
  37308. pkey = NULL;
  37309. #endif
  37310. #endif /* OPENSSL_ALL */
  37311. #ifndef NO_FILESYSTEM
  37312. #if defined(OPENSSL_ALL) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37313. ExpectIntEQ(PEM_write_PKCS8PrivateKey(XBADFILE, pkey, NULL, NULL, 0, NULL,
  37314. NULL), 0);
  37315. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, NULL, NULL, NULL, 0, NULL,
  37316. NULL), 0);
  37317. #endif
  37318. #ifndef NO_RSA
  37319. /* Get DER encoded RSA PKCS#8 data. */
  37320. ExpectTrue((file = XFOPEN(rsaDerPkcs8File, "rb")) != XBADFILE);
  37321. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37322. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37323. file)), 0);
  37324. if (file != XBADFILE) {
  37325. XFCLOSE(file);
  37326. file = XBADFILE;
  37327. }
  37328. p = pkcs8_buffer;
  37329. #ifdef OPENSSL_ALL
  37330. /* Try to decode - auto-detect key type. */
  37331. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  37332. #else
  37333. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &p, bytes));
  37334. #endif
  37335. /* Get PEM encoded RSA PKCS#8 data. */
  37336. ExpectTrue((file = XFOPEN(rsaPemPkcs8File, "rb")) != XBADFILE);
  37337. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37338. file)), 0);
  37339. if (file != XBADFILE) {
  37340. XFCLOSE(file);
  37341. file = XBADFILE;
  37342. }
  37343. #if defined(OPENSSL_ALL) && \
  37344. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37345. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37346. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(NULL, pkey, NULL, NULL, 0, NULL,
  37347. NULL), 0);
  37348. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, NULL, NULL, NULL, 0, NULL,
  37349. NULL), 0);
  37350. /* Write PKCS#8 PEM to BIO. */
  37351. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  37352. NULL), bytes);
  37353. /* Write PKCS#8 PEM to stderr. */
  37354. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, NULL, NULL, 0, NULL,
  37355. NULL), bytes);
  37356. /* Compare file and written data */
  37357. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  37358. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  37359. BIO_free(bio);
  37360. bio = NULL;
  37361. #if !defined(NO_AES) && defined(HAVE_AESGCM)
  37362. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_gcm(),
  37363. NULL, 0, PasswordCallBack, (void*)"yassl123"), 0);
  37364. #endif
  37365. #if !defined(NO_DES3) && !defined(NO_SHA)
  37366. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37367. /* Write Encrypted PKCS#8 PEM to BIO. */
  37368. bytes = 1834;
  37369. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_des_ede3_cbc(),
  37370. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37371. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_des_ede3_cbc(),
  37372. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37373. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  37374. (void*)"yassl123"));
  37375. EVP_PKEY_free(evpPkey);
  37376. evpPkey = NULL;
  37377. BIO_free(bio);
  37378. bio = NULL;
  37379. #endif /* !NO_DES3 && !NO_SHA */
  37380. #endif /* !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37381. EVP_PKEY_free(pkey);
  37382. pkey = NULL;
  37383. /* PKCS#8 encrypted RSA key */
  37384. #ifndef NO_DES3
  37385. ExpectTrue((file = XFOPEN(rsaDerPkcs8EncFile, "rb")) != XBADFILE);
  37386. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37387. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37388. file)), 0);
  37389. if (file != XBADFILE) {
  37390. XFCLOSE(file);
  37391. file = XBADFILE;
  37392. }
  37393. #if defined(OPENSSL_ALL) && \
  37394. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37395. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  37396. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  37397. (void*)"yassl123"));
  37398. EVP_PKEY_free(pkey);
  37399. pkey = NULL;
  37400. BIO_free(bio);
  37401. bio = NULL;
  37402. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37403. #endif /* !NO_DES3 */
  37404. #endif /* NO_RSA */
  37405. #ifdef HAVE_ECC
  37406. /* PKCS#8 encode EC key */
  37407. ExpectTrue((file = XFOPEN(ecDerPkcs8File, "rb")) != XBADFILE);
  37408. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37409. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37410. file)), 0);
  37411. if (file != XBADFILE) {
  37412. XFCLOSE(file);
  37413. file = XBADFILE;
  37414. }
  37415. p = pkcs8_buffer;
  37416. #ifdef OPENSSL_ALL
  37417. /* Try to decode - auto-detect key type. */
  37418. ExpectNotNull(pkey = d2i_AutoPrivateKey(NULL, &p, bytes));
  37419. #else
  37420. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &p, bytes));
  37421. #endif
  37422. /* Get PEM encoded RSA PKCS#8 data. */
  37423. ExpectTrue((file = XFOPEN(ecPemPkcs8File, "rb")) != XBADFILE);
  37424. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37425. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37426. file)), 0);
  37427. if (file != XBADFILE) {
  37428. XFCLOSE(file);
  37429. file = XBADFILE;
  37430. }
  37431. #if defined(OPENSSL_ALL) && \
  37432. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8) && \
  37433. defined(HAVE_AES_CBC)
  37434. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37435. /* Write PKCS#8 PEM to BIO. */
  37436. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, NULL, NULL, 0, NULL,
  37437. NULL), bytes);
  37438. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, NULL, NULL, 0, NULL,
  37439. NULL), bytes);
  37440. /* Compare file and written data */
  37441. ExpectIntEQ(BIO_get_mem_data(bio, &p), bytes);
  37442. ExpectIntEQ(XMEMCMP(p, pkcs8_buffer, bytes), 0);
  37443. BIO_free(bio);
  37444. bio = NULL;
  37445. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37446. /* Write Encrypted PKCS#8 PEM to BIO. */
  37447. bytes = 379;
  37448. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  37449. NULL, 0, NoPasswordCallBack, (void*)"yassl123"), 0);
  37450. ExpectIntEQ(PEM_write_bio_PKCS8PrivateKey(bio, pkey, EVP_aes_256_cbc(),
  37451. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37452. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_cbc(),
  37453. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37454. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_128_cbc(),
  37455. (char*)"yassl123", 8, PasswordCallBack, NULL), bytes);
  37456. ExpectIntEQ(PEM_write_PKCS8PrivateKey(stderr, pkey, EVP_aes_256_cbc(),
  37457. NULL, 0, PasswordCallBack, (void*)"yassl123"), bytes);
  37458. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, PasswordCallBack,
  37459. (void*)"yassl123"));
  37460. EVP_PKEY_free(evpPkey);
  37461. evpPkey = NULL;
  37462. BIO_free(bio);
  37463. bio = NULL;
  37464. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 && HAVE_AES_CBC */
  37465. EVP_PKEY_free(pkey);
  37466. pkey = NULL;
  37467. /* PKCS#8 encrypted EC key */
  37468. #ifndef NO_DES3
  37469. ExpectTrue((file = XFOPEN(ecDerPkcs8EncFile, "rb")) != XBADFILE);
  37470. ExpectNotNull(XMEMSET(pkcs8_buffer, 0, sizeof(pkcs8_buffer)));
  37471. ExpectIntGT((bytes = (int)XFREAD(pkcs8_buffer, 1, sizeof(pkcs8_buffer),
  37472. file)), 0);
  37473. if (file != XBADFILE) {
  37474. XFCLOSE(file);
  37475. file = XBADFILE;
  37476. }
  37477. #if defined(OPENSSL_ALL) && \
  37478. !defined(NO_BIO) && !defined(NO_PWDBASED) && defined(HAVE_PKCS8)
  37479. ExpectNotNull(bio = BIO_new_mem_buf((void*)pkcs8_buffer, bytes));
  37480. ExpectNotNull(pkey = d2i_PKCS8PrivateKey_bio(bio, NULL, PasswordCallBack,
  37481. (void*)"yassl123"));
  37482. EVP_PKEY_free(pkey);
  37483. pkey = NULL;
  37484. BIO_free(bio);
  37485. bio = NULL;
  37486. #endif /* OPENSSL_ALL && !NO_BIO && !NO_PWDBASED && HAVE_PKCS8 */
  37487. #endif /* !NO_DES3 */
  37488. #endif /* HAVE_ECC */
  37489. #endif /* !NO_FILESYSTEM */
  37490. #endif /* HAVE_FIPS && OPENSSL_EXTRA */
  37491. return EXPECT_RESULT();
  37492. }
  37493. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  37494. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  37495. #define LOGGING_THREADS 5
  37496. #define ERROR_COUNT 10
  37497. /* copied from logging.c since this is not exposed otherwise */
  37498. #ifndef ERROR_QUEUE_MAX
  37499. #ifdef ERROR_QUEUE_PER_THREAD
  37500. #define ERROR_QUEUE_MAX 16
  37501. #else
  37502. /* this breaks from compat of unlimited error queue size */
  37503. #define ERROR_QUEUE_MAX 100
  37504. #endif
  37505. #endif
  37506. static volatile int loggingThreadsReady;
  37507. static THREAD_RETURN WOLFSSL_THREAD test_logging(void* args)
  37508. {
  37509. const char* file;
  37510. int line;
  37511. unsigned long err;
  37512. int errorCount = 0;
  37513. int i;
  37514. (void)args;
  37515. while (!loggingThreadsReady);
  37516. for (i = 0; i < ERROR_COUNT; i++)
  37517. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  37518. while ((err = ERR_get_error_line(&file, &line))) {
  37519. AssertIntEQ(err, 990 + errorCount);
  37520. errorCount++;
  37521. }
  37522. AssertIntEQ(errorCount, ERROR_COUNT);
  37523. /* test max queue behavior, trying to add an arbitrary 3 errors over */
  37524. ERR_clear_error(); /* ERR_get_error_line() does not remove */
  37525. errorCount = 0;
  37526. for (i = 0; i < ERROR_QUEUE_MAX + 3; i++)
  37527. ERR_put_error(ERR_LIB_PEM, SYS_F_ACCEPT, -990 - i, __FILE__, __LINE__);
  37528. while ((err = ERR_get_error_line(&file, &line))) {
  37529. AssertIntEQ(err, 990 + errorCount);
  37530. errorCount++;
  37531. }
  37532. /* test that the 3 errors over the max were dropped */
  37533. AssertIntEQ(errorCount, ERROR_QUEUE_MAX);
  37534. WOLFSSL_RETURN_FROM_THREAD(0);
  37535. }
  37536. #endif
  37537. static int test_error_queue_per_thread(void)
  37538. {
  37539. int res = TEST_SKIPPED;
  37540. #if defined(ERROR_QUEUE_PER_THREAD) && !defined(NO_ERROR_QUEUE) && \
  37541. defined(OPENSSL_EXTRA) && defined(DEBUG_WOLFSSL)
  37542. THREAD_TYPE loggingThreads[LOGGING_THREADS];
  37543. int i;
  37544. ERR_clear_error(); /* clear out any error nodes */
  37545. loggingThreadsReady = 0;
  37546. for (i = 0; i < LOGGING_THREADS; i++)
  37547. start_thread(test_logging, NULL, &loggingThreads[i]);
  37548. loggingThreadsReady = 1;
  37549. for (i = 0; i < LOGGING_THREADS; i++)
  37550. join_thread(loggingThreads[i]);
  37551. res = TEST_SUCCESS;
  37552. #endif
  37553. return res;
  37554. }
  37555. static int test_wolfSSL_ERR_put_error(void)
  37556. {
  37557. EXPECT_DECLS;
  37558. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37559. defined(DEBUG_WOLFSSL)
  37560. const char* file;
  37561. int line;
  37562. ERR_clear_error(); /* clear out any error nodes */
  37563. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  37564. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  37565. ERR_put_error(0,SYS_F_BIND, 1, "this file", 1);
  37566. ExpectIntEQ(ERR_get_error_line(&file, &line), 1);
  37567. ERR_put_error(0,SYS_F_CONNECT, 2, "this file", 2);
  37568. ExpectIntEQ(ERR_get_error_line(&file, &line), 2);
  37569. ERR_put_error(0,SYS_F_FOPEN, 3, "this file", 3);
  37570. ExpectIntEQ(ERR_get_error_line(&file, &line), 3);
  37571. ERR_put_error(0,SYS_F_FREAD, 4, "this file", 4);
  37572. ExpectIntEQ(ERR_get_error_line(&file, &line), 4);
  37573. ERR_put_error(0,SYS_F_GETADDRINFO, 5, "this file", 5);
  37574. ExpectIntEQ(ERR_get_error_line(&file, &line), 5);
  37575. ERR_put_error(0,SYS_F_GETSOCKOPT, 6, "this file", 6);
  37576. ExpectIntEQ(ERR_get_error_line(&file, &line), 6);
  37577. ERR_put_error(0,SYS_F_GETSOCKNAME, 7, "this file", 7);
  37578. ExpectIntEQ(ERR_get_error_line(&file, &line), 7);
  37579. ERR_put_error(0,SYS_F_GETHOSTBYNAME, 8, "this file", 8);
  37580. ExpectIntEQ(ERR_get_error_line(&file, &line), 8);
  37581. ERR_put_error(0,SYS_F_GETNAMEINFO, 9, "this file", 9);
  37582. ExpectIntEQ(ERR_get_error_line(&file, &line), 9);
  37583. ERR_put_error(0,SYS_F_GETSERVBYNAME, 10, "this file", 10);
  37584. ExpectIntEQ(ERR_get_error_line(&file, &line), 10);
  37585. ERR_put_error(0,SYS_F_IOCTLSOCKET, 11, "this file", 11);
  37586. ExpectIntEQ(ERR_get_error_line(&file, &line), 11);
  37587. ERR_put_error(0,SYS_F_LISTEN, 12, "this file", 12);
  37588. ExpectIntEQ(ERR_get_error_line(&file, &line), 12);
  37589. ERR_put_error(0,SYS_F_OPENDIR, 13, "this file", 13);
  37590. ExpectIntEQ(ERR_get_error_line(&file, &line), 13);
  37591. ERR_put_error(0,SYS_F_SETSOCKOPT, 14, "this file", 14);
  37592. ExpectIntEQ(ERR_get_error_line(&file, &line), 14);
  37593. ERR_put_error(0,SYS_F_SOCKET, 15, "this file", 15);
  37594. ExpectIntEQ(ERR_get_error_line(&file, &line), 15);
  37595. #if defined(OPENSSL_ALL) && defined(WOLFSSL_PYTHON)
  37596. ERR_put_error(ERR_LIB_ASN1, SYS_F_ACCEPT, ASN1_R_HEADER_TOO_LONG,
  37597. "this file", 100);
  37598. ExpectIntEQ(wolfSSL_ERR_peek_last_error_line(&file, &line),
  37599. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  37600. ExpectIntEQ(line, 100);
  37601. ExpectIntEQ(wolfSSL_ERR_peek_error(),
  37602. (ERR_LIB_ASN1 << 24) | ASN1_R_HEADER_TOO_LONG);
  37603. ExpectIntEQ(ERR_get_error_line(&file, &line), ASN1_R_HEADER_TOO_LONG);
  37604. #endif
  37605. /* try reading past end of error queue */
  37606. file = NULL;
  37607. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  37608. ExpectNull(file);
  37609. ExpectIntEQ(ERR_get_error_line_data(&file, &line, NULL, NULL), 0);
  37610. PEMerr(4,4);
  37611. ExpectIntEQ(ERR_get_error(), 4);
  37612. /* Empty and free up all error nodes */
  37613. ERR_clear_error();
  37614. /* Verify all nodes are cleared */
  37615. ERR_put_error(0,SYS_F_ACCEPT, 0, "this file", 0);
  37616. ERR_clear_error();
  37617. ExpectIntEQ(ERR_get_error_line(&file, &line), 0);
  37618. #endif
  37619. return EXPECT_RESULT();
  37620. }
  37621. /*
  37622. * This is a regression test for a bug where the peek/get error functions were
  37623. * drawing from the end of the queue rather than the front.
  37624. */
  37625. static int test_wolfSSL_ERR_get_error_order(void)
  37626. {
  37627. EXPECT_DECLS;
  37628. #if defined(WOLFSSL_HAVE_ERROR_QUEUE) && defined(OPENSSL_EXTRA)
  37629. /* Empty the queue. */
  37630. wolfSSL_ERR_clear_error();
  37631. wolfSSL_ERR_put_error(0, 0, ASN_NO_SIGNER_E, "test", 0);
  37632. wolfSSL_ERR_put_error(0, 0, ASN_SELF_SIGNED_E, "test", 0);
  37633. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_NO_SIGNER_E);
  37634. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_NO_SIGNER_E);
  37635. ExpectIntEQ(wolfSSL_ERR_peek_error(), -ASN_SELF_SIGNED_E);
  37636. ExpectIntEQ(wolfSSL_ERR_get_error(), -ASN_SELF_SIGNED_E);
  37637. #endif /* WOLFSSL_HAVE_ERROR_QUEUE && OPENSSL_EXTRA */
  37638. return EXPECT_RESULT();
  37639. }
  37640. #ifndef NO_BIO
  37641. static int test_wolfSSL_ERR_print_errors(void)
  37642. {
  37643. EXPECT_DECLS;
  37644. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37645. defined(DEBUG_WOLFSSL) && !defined(NO_ERROR_STRINGS)
  37646. BIO* bio = NULL;
  37647. char buf[1024];
  37648. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37649. ERR_clear_error(); /* clear out any error nodes */
  37650. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  37651. /* Choosing -600 as an unused errno. */
  37652. ERR_put_error(0,SYS_F_BIND, -600, "asn.c", 100);
  37653. ERR_print_errors(bio);
  37654. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 56);
  37655. ExpectIntEQ(XSTRNCMP(
  37656. "error:173:wolfSSL library:Bad function argument:ssl.c:0",
  37657. buf, 55), 0);
  37658. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 57);
  37659. ExpectIntEQ(XSTRNCMP(
  37660. "error:600:wolfSSL library:unknown error number:asn.c:100",
  37661. buf, 56), 0);
  37662. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 1);
  37663. ExpectIntEQ(buf[0], '\0');
  37664. ExpectIntEQ(ERR_get_error_line(NULL, NULL), 0);
  37665. BIO_free(bio);
  37666. #endif
  37667. return EXPECT_RESULT();
  37668. }
  37669. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37670. defined(DEBUG_WOLFSSL)
  37671. static int test_wolfSSL_error_cb(const char *str, size_t len, void *u)
  37672. {
  37673. wolfSSL_BIO_write((BIO*)u, str, (int)len);
  37674. return 0;
  37675. }
  37676. #endif
  37677. static int test_wolfSSL_ERR_print_errors_cb(void)
  37678. {
  37679. EXPECT_DECLS;
  37680. #if !defined(NO_ERROR_QUEUE) && defined(OPENSSL_EXTRA) && \
  37681. defined(DEBUG_WOLFSSL)
  37682. BIO* bio = NULL;
  37683. char buf[1024];
  37684. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  37685. ERR_clear_error(); /* clear out any error nodes */
  37686. ERR_put_error(0,SYS_F_ACCEPT, -173, "ssl.c", 0);
  37687. ERR_put_error(0,SYS_F_BIND, -275, "asn.c", 100);
  37688. ERR_print_errors_cb(test_wolfSSL_error_cb, bio);
  37689. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 108);
  37690. ExpectIntEQ(XSTRNCMP(
  37691. "wolfSSL error occurred, error = 173 line:0 file:ssl.c",
  37692. buf, 53), 0);
  37693. ExpectIntEQ(XSTRNCMP(
  37694. "wolfSSL error occurred, error = 275 line:100 file:asn.c",
  37695. buf + 53, 55), 0);
  37696. ExpectIntEQ(BIO_gets(bio, buf, sizeof(buf)), 0);
  37697. BIO_free(bio);
  37698. #endif
  37699. return EXPECT_RESULT();
  37700. }
  37701. /*
  37702. * Testing WOLFSSL_ERROR_MSG
  37703. */
  37704. static int test_WOLFSSL_ERROR_MSG(void)
  37705. {
  37706. int res = TEST_SKIPPED;
  37707. #if defined(DEBUG_WOLFSSL) || defined(OPENSSL_ALL) || defined(WOLFSSL_NGINX) ||\
  37708. defined(WOLFSSL_HAPROXY) || defined(OPENSSL_EXTRA)
  37709. const char* msg = TEST_STRING;
  37710. WOLFSSL_ERROR_MSG(msg);
  37711. res = TEST_SUCCESS;
  37712. #endif
  37713. return res;
  37714. } /* End test_WOLFSSL_ERROR_MSG */
  37715. /*
  37716. * Testing wc_ERR_remove_state
  37717. */
  37718. static int test_wc_ERR_remove_state(void)
  37719. {
  37720. int res = TEST_SKIPPED;
  37721. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  37722. wc_ERR_remove_state();
  37723. res = TEST_SUCCESS;
  37724. #endif
  37725. return res;
  37726. } /* End test_wc_ERR_remove_state */
  37727. /*
  37728. * Testing wc_ERR_print_errors_fp
  37729. */
  37730. static int test_wc_ERR_print_errors_fp(void)
  37731. {
  37732. EXPECT_DECLS;
  37733. #if (defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)) && \
  37734. (!defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM))
  37735. long sz;
  37736. XFILE fp = XBADFILE;
  37737. WOLFSSL_ERROR(BAD_FUNC_ARG);
  37738. ExpectTrue((fp = XFOPEN("./tests/test-log-dump-to-file.txt", "ar")) !=
  37739. XBADFILE);
  37740. wc_ERR_print_errors_fp(fp);
  37741. #if defined(DEBUG_WOLFSSL)
  37742. ExpectTrue(XFSEEK(fp, 0, XSEEK_END) == 0);
  37743. #ifdef NO_ERROR_QUEUE
  37744. ExpectIntEQ(sz = XFTELL(fp), 0);
  37745. #else
  37746. ExpectIntNE(sz = XFTELL(fp), 0);
  37747. #endif
  37748. #endif
  37749. if (fp != XBADFILE)
  37750. XFCLOSE(fp);
  37751. (void)sz;
  37752. #endif
  37753. return EXPECT_RESULT();
  37754. } /* End test_wc_ERR_print_errors_fp */
  37755. #ifdef DEBUG_WOLFSSL
  37756. static void Logging_cb(const int logLevel, const char *const logMessage)
  37757. {
  37758. (void)logLevel;
  37759. (void)logMessage;
  37760. }
  37761. #endif
  37762. /*
  37763. * Testing wolfSSL_GetLoggingCb
  37764. */
  37765. static int test_wolfSSL_GetLoggingCb(void)
  37766. {
  37767. EXPECT_DECLS;
  37768. #ifdef DEBUG_WOLFSSL
  37769. /* Testing without wolfSSL_SetLoggingCb() */
  37770. ExpectNull(wolfSSL_GetLoggingCb());
  37771. /* Testing with wolfSSL_SetLoggingCb() */
  37772. ExpectIntEQ(wolfSSL_SetLoggingCb(Logging_cb), 0);
  37773. ExpectNotNull(wolfSSL_GetLoggingCb());
  37774. ExpectIntEQ(wolfSSL_SetLoggingCb(NULL), 0);
  37775. #endif
  37776. ExpectNull(wolfSSL_GetLoggingCb());
  37777. return EXPECT_RESULT();
  37778. } /* End test_wolfSSL_GetLoggingCb */
  37779. #endif /* !NO_BIO */
  37780. static int test_wolfSSL_MD4(void)
  37781. {
  37782. EXPECT_DECLS;
  37783. #if defined(OPENSSL_EXTRA) && !defined(NO_MD4)
  37784. MD4_CTX md4;
  37785. unsigned char out[16]; /* MD4_DIGEST_SIZE */
  37786. const char* msg = "12345678901234567890123456789012345678901234567890123456"
  37787. "789012345678901234567890";
  37788. const char* test = "\xe3\x3b\x4d\xdc\x9c\x38\xf2\x19\x9c\x3e\x7b\x16\x4f"
  37789. "\xcc\x05\x36";
  37790. int msgSz = (int)XSTRLEN(msg);
  37791. XMEMSET(out, 0, sizeof(out));
  37792. MD4_Init(&md4);
  37793. MD4_Update(&md4, (const void*)msg, (unsigned long)msgSz);
  37794. MD4_Final(out, &md4);
  37795. ExpectIntEQ(XMEMCMP(out, test, sizeof(out)), 0);
  37796. #endif
  37797. return EXPECT_RESULT();
  37798. }
  37799. static int test_wolfSSL_MD5(void)
  37800. {
  37801. EXPECT_DECLS;
  37802. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  37803. byte input1[] = "";
  37804. byte input2[] = "message digest";
  37805. byte hash[WC_MD5_DIGEST_SIZE];
  37806. unsigned char output1[] =
  37807. "\xd4\x1d\x8c\xd9\x8f\x00\xb2\x04\xe9\x80\x09\x98\xec\xf8\x42\x7e";
  37808. unsigned char output2[] =
  37809. "\xf9\x6b\x69\x7d\x7c\xb7\x93\x8d\x52\x5a\x2f\x31\xaa\xf1\x61\xd0";
  37810. WOLFSSL_MD5_CTX md5;
  37811. XMEMSET(&md5, 0, sizeof(md5));
  37812. /* Test cases for illegal parameters */
  37813. ExpectIntEQ(MD5_Init(NULL), 0);
  37814. ExpectIntEQ(MD5_Init(&md5), 1);
  37815. ExpectIntEQ(MD5_Update(NULL, input1, 0), 0);
  37816. ExpectIntEQ(MD5_Update(NULL, NULL, 0), 0);
  37817. ExpectIntEQ(MD5_Update(&md5, NULL, 1), 0);
  37818. ExpectIntEQ(MD5_Final(NULL, &md5), 0);
  37819. ExpectIntEQ(MD5_Final(hash, NULL), 0);
  37820. ExpectIntEQ(MD5_Final(NULL, NULL), 0);
  37821. /* Init MD5 CTX */
  37822. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  37823. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input1, XSTRLEN((const char*)&input1)),
  37824. 1);
  37825. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  37826. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  37827. /* Init MD5 CTX */
  37828. ExpectIntEQ(wolfSSL_MD5_Init(&md5), 1);
  37829. ExpectIntEQ(wolfSSL_MD5_Update(&md5, input2,
  37830. (int)XSTRLEN((const char*)input2)), 1);
  37831. ExpectIntEQ(wolfSSL_MD5_Final(hash, &md5), 1);
  37832. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  37833. #if !defined(NO_OLD_NAMES) && \
  37834. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  37835. ExpectPtrNE(MD5(NULL, 1, (byte*)&hash), &hash);
  37836. ExpectPtrEq(MD5(input1, 0, (byte*)&hash), &hash);
  37837. ExpectPtrNE(MD5(input1, 1, NULL), NULL);
  37838. ExpectPtrNE(MD5(NULL, 0, NULL), NULL);
  37839. ExpectPtrEq(MD5(input1, (int)XSTRLEN((const char*)&input1), (byte*)&hash),
  37840. &hash);
  37841. ExpectIntEQ(XMEMCMP(&hash, output1, WC_MD5_DIGEST_SIZE), 0);
  37842. ExpectPtrEq(MD5(input2, (int)XSTRLEN((const char*)&input2), (byte*)&hash),
  37843. &hash);
  37844. ExpectIntEQ(XMEMCMP(&hash, output2, WC_MD5_DIGEST_SIZE), 0);
  37845. {
  37846. byte data[] = "Data to be hashed.";
  37847. XMEMSET(hash, 0, WC_MD5_DIGEST_SIZE);
  37848. ExpectNotNull(MD5(data, sizeof(data), NULL));
  37849. ExpectNotNull(MD5(data, sizeof(data), hash));
  37850. ExpectNotNull(MD5(NULL, 0, hash));
  37851. ExpectNull(MD5(NULL, sizeof(data), hash));
  37852. }
  37853. #endif
  37854. #endif
  37855. return EXPECT_RESULT();
  37856. }
  37857. static int test_wolfSSL_MD5_Transform(void)
  37858. {
  37859. EXPECT_DECLS;
  37860. #if defined(OPENSSL_EXTRA) && !defined(NO_MD5)
  37861. byte input1[] = "";
  37862. byte input2[] = "abc";
  37863. byte local[WC_MD5_BLOCK_SIZE];
  37864. word32 sLen = 0;
  37865. #ifdef BIG_ENDIAN_ORDER
  37866. unsigned char output1[] =
  37867. "\x03\x1f\x1d\xac\x6e\xa5\x8e\xd0\x1f\xab\x67\xb7\x74\x31\x77\x91";
  37868. unsigned char output2[] =
  37869. "\xef\xd3\x79\x8d\x67\x17\x25\x90\xa4\x13\x79\xc7\xe3\xa7\x7b\xbc";
  37870. #else
  37871. unsigned char output1[] =
  37872. "\xac\x1d\x1f\x03\xd0\x8e\xa5\x6e\xb7\x67\xab\x1f\x91\x77\x31\x74";
  37873. unsigned char output2[] =
  37874. "\x8d\x79\xd3\xef\x90\x25\x17\x67\xc7\x79\x13\xa4\xbc\x7b\xa7\xe3";
  37875. #endif
  37876. union {
  37877. wc_Md5 native;
  37878. MD5_CTX compat;
  37879. } md5;
  37880. XMEMSET(&md5.compat, 0, sizeof(md5.compat));
  37881. XMEMSET(&local, 0, sizeof(local));
  37882. /* sanity check */
  37883. ExpectIntEQ(MD5_Transform(NULL, NULL), 0);
  37884. ExpectIntEQ(MD5_Transform(NULL, (const byte*)&input1), 0);
  37885. ExpectIntEQ(MD5_Transform(&md5.compat, NULL), 0);
  37886. ExpectIntEQ(wc_Md5Transform(NULL, NULL), BAD_FUNC_ARG);
  37887. ExpectIntEQ(wc_Md5Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  37888. ExpectIntEQ(wc_Md5Transform(&md5.native, NULL), BAD_FUNC_ARG);
  37889. /* Init MD5 CTX */
  37890. ExpectIntEQ(wolfSSL_MD5_Init(&md5.compat), 1);
  37891. /* Do Transform*/
  37892. sLen = (word32)XSTRLEN((char*)input1);
  37893. XMEMCPY(local, input1, sLen);
  37894. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  37895. ExpectIntEQ(XMEMCMP(md5.native.digest, output1, WC_MD5_DIGEST_SIZE), 0);
  37896. /* Init MD5 CTX */
  37897. ExpectIntEQ(MD5_Init(&md5.compat), 1);
  37898. sLen = (word32)XSTRLEN((char*)input2);
  37899. XMEMSET(local, 0, WC_MD5_BLOCK_SIZE);
  37900. XMEMCPY(local, input2, sLen);
  37901. ExpectIntEQ(MD5_Transform(&md5.compat, (const byte*)&local[0]), 1);
  37902. ExpectIntEQ(XMEMCMP(md5.native.digest, output2, WC_MD5_DIGEST_SIZE), 0);
  37903. #endif
  37904. return EXPECT_RESULT();
  37905. }
  37906. static int test_wolfSSL_SHA(void)
  37907. {
  37908. EXPECT_DECLS;
  37909. #if defined(OPENSSL_EXTRA) && !defined(HAVE_SELFTEST)
  37910. #if !defined(NO_SHA) && defined(NO_OLD_SHA_NAMES) && \
  37911. (!defined(HAVE_FIPS) || \
  37912. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  37913. {
  37914. const unsigned char in[] = "abc";
  37915. unsigned char expected[] = "\xA9\x99\x3E\x36\x47\x06\x81\x6A\xBA\x3E"
  37916. "\x25\x71\x78\x50\xC2\x6C\x9C\xD0\xD8\x9D";
  37917. unsigned char out[WC_SHA_DIGEST_SIZE];
  37918. unsigned char* p = NULL;
  37919. WOLFSSL_SHA_CTX sha;
  37920. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  37921. ExpectNotNull(SHA1(in, XSTRLEN((char*)in), out));
  37922. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37923. /* SHA interface test */
  37924. XMEMSET(out, 0, WC_SHA_DIGEST_SIZE);
  37925. ExpectNull(SHA(NULL, XSTRLEN((char*)in), out));
  37926. ExpectNotNull(SHA(in, 0, out));
  37927. ExpectNotNull(SHA(in, XSTRLEN((char*)in), NULL));
  37928. ExpectNotNull(SHA(NULL, 0, out));
  37929. ExpectNotNull(SHA(NULL, 0, NULL));
  37930. ExpectNotNull(SHA(in, XSTRLEN((char*)in), out));
  37931. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37932. ExpectNotNull(p = SHA(in, XSTRLEN((char*)in), NULL));
  37933. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA_DIGEST_SIZE), 0);
  37934. ExpectIntEQ(wolfSSL_SHA_Init(&sha), 1);
  37935. ExpectIntEQ(wolfSSL_SHA_Update(&sha, in, XSTRLEN((char*)in)), 1);
  37936. ExpectIntEQ(wolfSSL_SHA_Final(out, &sha), 1);
  37937. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37938. ExpectIntEQ(wolfSSL_SHA1_Init(&sha), 1);
  37939. ExpectIntEQ(wolfSSL_SHA1_Update(&sha, in, XSTRLEN((char*)in)), 1);
  37940. ExpectIntEQ(wolfSSL_SHA1_Final(out, &sha), 1);
  37941. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA_DIGEST_SIZE), 0);
  37942. }
  37943. #endif
  37944. #if !defined(NO_SHA256)
  37945. {
  37946. const unsigned char in[] = "abc";
  37947. unsigned char expected[] =
  37948. "\xBA\x78\x16\xBF\x8F\x01\xCF\xEA\x41\x41\x40\xDE\x5D\xAE\x22"
  37949. "\x23\xB0\x03\x61\xA3\x96\x17\x7A\x9C\xB4\x10\xFF\x61\xF2\x00"
  37950. "\x15\xAD";
  37951. unsigned char out[WC_SHA256_DIGEST_SIZE];
  37952. unsigned char* p = NULL;
  37953. XMEMSET(out, 0, WC_SHA256_DIGEST_SIZE);
  37954. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37955. ExpectNotNull(SHA256(in, XSTRLEN((char*)in), out));
  37956. #else
  37957. ExpectNotNull(wolfSSL_SHA256(in, XSTRLEN((char*)in), out));
  37958. #endif
  37959. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA256_DIGEST_SIZE), 0);
  37960. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37961. ExpectNotNull(p = SHA256(in, XSTRLEN((char*)in), NULL));
  37962. #else
  37963. ExpectNotNull(p = wolfSSL_SHA256(in, XSTRLEN((char*)in), NULL));
  37964. #endif
  37965. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA256_DIGEST_SIZE), 0);
  37966. }
  37967. #endif
  37968. #if defined(WOLFSSL_SHA384)
  37969. {
  37970. const unsigned char in[] = "abc";
  37971. unsigned char expected[] =
  37972. "\xcb\x00\x75\x3f\x45\xa3\x5e\x8b\xb5\xa0\x3d\x69\x9a\xc6\x50"
  37973. "\x07\x27\x2c\x32\xab\x0e\xde\xd1\x63\x1a\x8b\x60\x5a\x43\xff"
  37974. "\x5b\xed\x80\x86\x07\x2b\xa1\xe7\xcc\x23\x58\xba\xec\xa1\x34"
  37975. "\xc8\x25\xa7";
  37976. unsigned char out[WC_SHA384_DIGEST_SIZE];
  37977. unsigned char* p = NULL;
  37978. XMEMSET(out, 0, WC_SHA384_DIGEST_SIZE);
  37979. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37980. ExpectNotNull(SHA384(in, XSTRLEN((char*)in), out));
  37981. #else
  37982. ExpectNotNull(wolfSSL_SHA384(in, XSTRLEN((char*)in), out));
  37983. #endif
  37984. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA384_DIGEST_SIZE), 0);
  37985. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  37986. ExpectNotNull(p = SHA384(in, XSTRLEN((char*)in), NULL));
  37987. #else
  37988. ExpectNotNull(p = wolfSSL_SHA384(in, XSTRLEN((char*)in), NULL));
  37989. #endif
  37990. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA384_DIGEST_SIZE), 0);
  37991. }
  37992. #endif
  37993. #if defined(WOLFSSL_SHA512)
  37994. {
  37995. const unsigned char in[] = "abc";
  37996. unsigned char expected[] =
  37997. "\xdd\xaf\x35\xa1\x93\x61\x7a\xba\xcc\x41\x73\x49\xae\x20\x41"
  37998. "\x31\x12\xe6\xfa\x4e\x89\xa9\x7e\xa2\x0a\x9e\xee\xe6\x4b\x55"
  37999. "\xd3\x9a\x21\x92\x99\x2a\x27\x4f\xc1\xa8\x36\xba\x3c\x23\xa3"
  38000. "\xfe\xeb\xbd\x45\x4d\x44\x23\x64\x3c\xe8\x0e\x2a\x9a\xc9\x4f"
  38001. "\xa5\x4c\xa4\x9f";
  38002. unsigned char out[WC_SHA512_DIGEST_SIZE];
  38003. unsigned char* p = NULL;
  38004. XMEMSET(out, 0, WC_SHA512_DIGEST_SIZE);
  38005. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  38006. ExpectNotNull(SHA512(in, XSTRLEN((char*)in), out));
  38007. #else
  38008. ExpectNotNull(wolfSSL_SHA512(in, XSTRLEN((char*)in), out));
  38009. #endif
  38010. ExpectIntEQ(XMEMCMP(out, expected, WC_SHA512_DIGEST_SIZE), 0);
  38011. #if !defined(NO_OLD_NAMES) && !defined(HAVE_FIPS)
  38012. ExpectNotNull(p = SHA512(in, XSTRLEN((char*)in), NULL));
  38013. #else
  38014. ExpectNotNull(p = wolfSSL_SHA512(in, XSTRLEN((char*)in), NULL));
  38015. #endif
  38016. ExpectIntEQ(XMEMCMP(p, expected, WC_SHA512_DIGEST_SIZE), 0);
  38017. }
  38018. #endif
  38019. #endif
  38020. return EXPECT_RESULT();
  38021. }
  38022. static int test_wolfSSL_SHA_Transform(void)
  38023. {
  38024. EXPECT_DECLS;
  38025. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA)
  38026. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  38027. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  38028. byte input1[] = "";
  38029. byte input2[] = "abc";
  38030. byte local[WC_SHA_BLOCK_SIZE];
  38031. word32 sLen = 0;
  38032. #ifdef BIG_ENDIAN_ORDER
  38033. unsigned char output1[] =
  38034. "\x92\xb4\x04\xe5\x56\x58\x8c\xed\x6c\x1a\xcd\x4e\xbf\x05\x3f\x68"
  38035. "\x09\xf7\x3a\x93";
  38036. unsigned char output2[] =
  38037. "\x97\xb2\x74\x8b\x4f\x5b\xbc\xca\x5b\xc0\xe6\xea\x2d\x40\xb4\xa0"
  38038. "\x7c\x6e\x08\xb8";
  38039. #else
  38040. unsigned char output1[] =
  38041. "\xe5\x04\xb4\x92\xed\x8c\x58\x56\x4e\xcd\x1a\x6c\x68\x3f\x05\xbf"
  38042. "\x93\x3a\xf7\x09";
  38043. unsigned char output2[] =
  38044. "\x8b\x74\xb2\x97\xca\xbc\x5b\x4f\xea\xe6\xc0\x5b\xa0\xb4\x40\x2d"
  38045. "\xb8\x08\x6e\x7c";
  38046. #endif
  38047. union {
  38048. wc_Sha native;
  38049. SHA_CTX compat;
  38050. } sha;
  38051. union {
  38052. wc_Sha native;
  38053. SHA_CTX compat;
  38054. } sha1;
  38055. XMEMSET(&sha.compat, 0, sizeof(sha.compat));
  38056. XMEMSET(&local, 0, sizeof(local));
  38057. /* sanity check */
  38058. ExpectIntEQ(SHA_Transform(NULL, NULL), 0);
  38059. ExpectIntEQ(SHA_Transform(NULL, (const byte*)&input1), 0);
  38060. ExpectIntEQ(SHA_Transform(&sha.compat, NULL), 0);
  38061. ExpectIntEQ(SHA1_Transform(NULL, NULL), 0);
  38062. ExpectIntEQ(SHA1_Transform(NULL, (const byte*)&input1), 0);
  38063. ExpectIntEQ(SHA1_Transform(&sha.compat, NULL), 0);
  38064. ExpectIntEQ(wc_ShaTransform(NULL, NULL), BAD_FUNC_ARG);
  38065. ExpectIntEQ(wc_ShaTransform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  38066. ExpectIntEQ(wc_ShaTransform(&sha.native, NULL), BAD_FUNC_ARG);
  38067. /* Init SHA CTX */
  38068. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  38069. /* Do Transform*/
  38070. sLen = (word32)XSTRLEN((char*)input1);
  38071. XMEMCPY(local, input1, sLen);
  38072. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  38073. ExpectIntEQ(XMEMCMP(sha.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  38074. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  38075. /* Init SHA CTX */
  38076. ExpectIntEQ(SHA_Init(&sha.compat), 1);
  38077. sLen = (word32)XSTRLEN((char*)input2);
  38078. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  38079. XMEMCPY(local, input2, sLen);
  38080. ExpectIntEQ(SHA_Transform(&sha.compat, (const byte*)&local[0]), 1);
  38081. ExpectIntEQ(XMEMCMP(sha.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  38082. ExpectIntEQ(SHA_Final(local, &sha.compat), 1); /* frees resources */
  38083. /* SHA1 */
  38084. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  38085. /* Init SHA CTX */
  38086. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  38087. /* Do Transform*/
  38088. sLen = (word32)XSTRLEN((char*)input1);
  38089. XMEMCPY(local, input1, sLen);
  38090. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  38091. ExpectIntEQ(XMEMCMP(sha1.native.digest, output1, WC_SHA_DIGEST_SIZE), 0);
  38092. ExpectIntEQ(SHA1_Final(local, &sha1.compat), 1); /* frees resources */
  38093. /* Init SHA CTX */
  38094. ExpectIntEQ(SHA1_Init(&sha1.compat), 1);
  38095. sLen = (word32)XSTRLEN((char*)input2);
  38096. XMEMSET(local, 0, WC_SHA_BLOCK_SIZE);
  38097. XMEMCPY(local, input2, sLen);
  38098. ExpectIntEQ(SHA1_Transform(&sha1.compat, (const byte*)&local[0]), 1);
  38099. ExpectIntEQ(XMEMCMP(sha1.native.digest, output2, WC_SHA_DIGEST_SIZE), 0);
  38100. ExpectIntEQ(SHA_Final(local, &sha1.compat), 1); /* frees resources */
  38101. #endif
  38102. #endif
  38103. return EXPECT_RESULT();
  38104. }
  38105. static int test_wolfSSL_SHA224(void)
  38106. {
  38107. EXPECT_DECLS;
  38108. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA224) && \
  38109. !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  38110. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2))
  38111. unsigned char input[] =
  38112. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  38113. unsigned char output[] =
  38114. "\x75\x38\x8b\x16\x51\x27\x76\xcc\x5d\xba\x5d\xa1\xfd\x89\x01"
  38115. "\x50\xb0\xc6\x45\x5c\xb4\xf5\x8b\x19\x52\x52\x25\x25";
  38116. size_t inLen;
  38117. byte hash[WC_SHA224_DIGEST_SIZE];
  38118. unsigned char* p;
  38119. inLen = XSTRLEN((char*)input);
  38120. XMEMSET(hash, 0, WC_SHA224_DIGEST_SIZE);
  38121. ExpectNull(SHA224(NULL, inLen, hash));
  38122. ExpectNotNull(SHA224(input, 0, hash));
  38123. ExpectNotNull(SHA224(input, inLen, NULL));
  38124. ExpectNotNull(SHA224(NULL, 0, hash));
  38125. ExpectNotNull(SHA224(NULL, 0, NULL));
  38126. ExpectNotNull(SHA224(input, inLen, hash));
  38127. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA224_DIGEST_SIZE), 0);
  38128. ExpectNotNull(p = SHA224(input, inLen, NULL));
  38129. ExpectIntEQ(XMEMCMP(p, output, WC_SHA224_DIGEST_SIZE), 0);
  38130. #endif
  38131. return EXPECT_RESULT();
  38132. }
  38133. static int test_wolfSSL_SHA256(void)
  38134. {
  38135. EXPECT_DECLS;
  38136. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && \
  38137. defined(NO_OLD_SHA_NAMES) && !defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)
  38138. unsigned char input[] =
  38139. "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
  38140. unsigned char output[] =
  38141. "\x24\x8D\x6A\x61\xD2\x06\x38\xB8\xE5\xC0\x26\x93\x0C\x3E\x60"
  38142. "\x39\xA3\x3C\xE4\x59\x64\xFF\x21\x67\xF6\xEC\xED\xD4\x19\xDB"
  38143. "\x06\xC1";
  38144. size_t inLen;
  38145. byte hash[WC_SHA256_DIGEST_SIZE];
  38146. inLen = XSTRLEN((char*)input);
  38147. XMEMSET(hash, 0, WC_SHA256_DIGEST_SIZE);
  38148. ExpectNotNull(SHA256(input, inLen, hash));
  38149. ExpectIntEQ(XMEMCMP(hash, output, WC_SHA256_DIGEST_SIZE), 0);
  38150. #endif
  38151. return EXPECT_RESULT();
  38152. }
  38153. static int test_wolfSSL_SHA256_Transform(void)
  38154. {
  38155. EXPECT_DECLS;
  38156. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  38157. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  38158. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  38159. !defined(WOLFSSL_DEVCRYPTO_HASH) && !defined(WOLFSSL_AFALG_HASH) && \
  38160. !defined(WOLFSSL_KCAPI_HASH)
  38161. byte input1[] = "";
  38162. byte input2[] = "abc";
  38163. byte local[WC_SHA256_BLOCK_SIZE];
  38164. word32 sLen = 0;
  38165. #ifdef BIG_ENDIAN_ORDER
  38166. unsigned char output1[] =
  38167. "\xda\x56\x98\xbe\x17\xb9\xb4\x69\x62\x33\x57\x99\x77\x9f\xbe\xca"
  38168. "\x8c\xe5\xd4\x91\xc0\xd2\x62\x43\xba\xfe\xf9\xea\x18\x37\xa9\xd8";
  38169. unsigned char output2[] =
  38170. "\x1d\x4e\xd4\x67\x67\x7c\x61\x67\x44\x10\x76\x26\x78\x10\xff\xb8"
  38171. "\x40\xc8\x9a\x39\x73\x16\x60\x8c\xa6\x61\xd6\x05\x91\xf2\x8c\x35";
  38172. #else
  38173. unsigned char output1[] =
  38174. "\xbe\x98\x56\xda\x69\xb4\xb9\x17\x99\x57\x33\x62\xca\xbe\x9f\x77"
  38175. "\x91\xd4\xe5\x8c\x43\x62\xd2\xc0\xea\xf9\xfe\xba\xd8\xa9\x37\x18";
  38176. unsigned char output2[] =
  38177. "\x67\xd4\x4e\x1d\x67\x61\x7c\x67\x26\x76\x10\x44\xb8\xff\x10\x78"
  38178. "\x39\x9a\xc8\x40\x8c\x60\x16\x73\x05\xd6\x61\xa6\x35\x8c\xf2\x91";
  38179. #endif
  38180. union {
  38181. wc_Sha256 native;
  38182. SHA256_CTX compat;
  38183. } sha256;
  38184. XMEMSET(&sha256.compat, 0, sizeof(sha256.compat));
  38185. XMEMSET(&local, 0, sizeof(local));
  38186. /* sanity check */
  38187. ExpectIntEQ(SHA256_Transform(NULL, NULL), 0);
  38188. ExpectIntEQ(SHA256_Transform(NULL, (const byte*)&input1), 0);
  38189. ExpectIntEQ(SHA256_Transform(&sha256.compat, NULL), 0);
  38190. ExpectIntEQ(wc_Sha256Transform(NULL, NULL), BAD_FUNC_ARG);
  38191. ExpectIntEQ(wc_Sha256Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  38192. ExpectIntEQ(wc_Sha256Transform(&sha256.native, NULL), BAD_FUNC_ARG);
  38193. /* Init SHA256 CTX */
  38194. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  38195. /* Do Transform*/
  38196. sLen = (word32)XSTRLEN((char*)input1);
  38197. XMEMCPY(local, input1, sLen);
  38198. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  38199. ExpectIntEQ(XMEMCMP(sha256.native.digest, output1, WC_SHA256_DIGEST_SIZE),
  38200. 0);
  38201. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  38202. /* Init SHA256 CTX */
  38203. ExpectIntEQ(SHA256_Init(&sha256.compat), 1);
  38204. sLen = (word32)XSTRLEN((char*)input2);
  38205. XMEMSET(local, 0, WC_SHA256_BLOCK_SIZE);
  38206. XMEMCPY(local, input2, sLen);
  38207. ExpectIntEQ(SHA256_Transform(&sha256.compat, (const byte*)&local[0]), 1);
  38208. ExpectIntEQ(XMEMCMP(sha256.native.digest, output2, WC_SHA256_DIGEST_SIZE),
  38209. 0);
  38210. ExpectIntEQ(SHA256_Final(local, &sha256.compat), 1); /* frees resources */
  38211. #endif
  38212. #endif
  38213. return EXPECT_RESULT();
  38214. }
  38215. static int test_wolfSSL_SHA512_Transform(void)
  38216. {
  38217. EXPECT_DECLS;
  38218. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512)
  38219. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  38220. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  38221. !defined(WOLFSSL_KCAPI_HASH)
  38222. byte input1[] = "";
  38223. byte input2[] = "abc";
  38224. byte local[WC_SHA512_BLOCK_SIZE];
  38225. word32 sLen = 0;
  38226. #ifdef BIG_ENDIAN_ORDER
  38227. unsigned char output1[] =
  38228. "\xcf\x78\x81\xd5\x77\x4a\xcb\xe8\x53\x33\x62\xe0\xfb\xc7\x80\x70"
  38229. "\x02\x67\x63\x9d\x87\x46\x0e\xda\x30\x86\xcb\x40\xe8\x59\x31\xb0"
  38230. "\x71\x7d\xc9\x52\x88\xa0\x23\xa3\x96\xba\xb2\xc1\x4c\xe0\xb5\xe0"
  38231. "\x6f\xc4\xfe\x04\xea\xe3\x3e\x0b\x91\xf4\xd8\x0c\xbd\x66\x8b\xee";
  38232. unsigned char output2[] =
  38233. "\x11\x10\x93\x4e\xeb\xa0\xcc\x0d\xfd\x33\x43\x9c\xfb\x04\xc8\x21"
  38234. "\xa9\xb4\x26\x3d\xca\xab\x31\x41\xe2\xc6\xaa\xaf\xe1\x67\xd7\xab"
  38235. "\x31\x8f\x2e\x54\x2c\xba\x4e\x83\xbe\x88\xec\x9d\x8f\x2b\x38\x98"
  38236. "\x14\xd2\x4e\x9d\x53\x8b\x5e\x4d\xde\x68\x6c\x69\xaf\x20\x96\xf0";
  38237. #else
  38238. unsigned char output1[] =
  38239. "\xe8\xcb\x4a\x77\xd5\x81\x78\xcf\x70\x80\xc7\xfb\xe0\x62\x33\x53"
  38240. "\xda\x0e\x46\x87\x9d\x63\x67\x02\xb0\x31\x59\xe8\x40\xcb\x86\x30"
  38241. "\xa3\x23\xa0\x88\x52\xc9\x7d\x71\xe0\xb5\xe0\x4c\xc1\xb2\xba\x96"
  38242. "\x0b\x3e\xe3\xea\x04\xfe\xc4\x6f\xee\x8b\x66\xbd\x0c\xd8\xf4\x91";
  38243. unsigned char output2[] =
  38244. "\x0d\xcc\xa0\xeb\x4e\x93\x10\x11\x21\xc8\x04\xfb\x9c\x43\x33\xfd"
  38245. "\x41\x31\xab\xca\x3d\x26\xb4\xa9\xab\xd7\x67\xe1\xaf\xaa\xc6\xe2"
  38246. "\x83\x4e\xba\x2c\x54\x2e\x8f\x31\x98\x38\x2b\x8f\x9d\xec\x88\xbe"
  38247. "\x4d\x5e\x8b\x53\x9d\x4e\xd2\x14\xf0\x96\x20\xaf\x69\x6c\x68\xde";
  38248. #endif
  38249. union {
  38250. wc_Sha512 native;
  38251. SHA512_CTX compat;
  38252. } sha512;
  38253. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  38254. XMEMSET(&local, 0, sizeof(local));
  38255. /* sanity check */
  38256. ExpectIntEQ(SHA512_Transform(NULL, NULL), 0);
  38257. ExpectIntEQ(SHA512_Transform(NULL, (const byte*)&input1), 0);
  38258. ExpectIntEQ(SHA512_Transform(&sha512.compat, NULL), 0);
  38259. ExpectIntEQ(wc_Sha512Transform(NULL, NULL), BAD_FUNC_ARG);
  38260. ExpectIntEQ(wc_Sha512Transform(NULL, (const byte*)&input1), BAD_FUNC_ARG);
  38261. ExpectIntEQ(wc_Sha512Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  38262. /* Init SHA512 CTX */
  38263. ExpectIntEQ(wolfSSL_SHA512_Init(&sha512.compat), 1);
  38264. /* Do Transform*/
  38265. sLen = (word32)XSTRLEN((char*)input1);
  38266. XMEMCPY(local, input1, sLen);
  38267. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  38268. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  38269. WC_SHA512_DIGEST_SIZE), 0);
  38270. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  38271. /* Init SHA512 CTX */
  38272. ExpectIntEQ(SHA512_Init(&sha512.compat), 1);
  38273. sLen = (word32)XSTRLEN((char*)input2);
  38274. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  38275. XMEMCPY(local, input2, sLen);
  38276. ExpectIntEQ(SHA512_Transform(&sha512.compat, (const byte*)&local[0]), 1);
  38277. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  38278. WC_SHA512_DIGEST_SIZE), 0);
  38279. ExpectIntEQ(SHA512_Final(local, &sha512.compat), 1); /* frees resources */
  38280. (void)input1;
  38281. #endif
  38282. #endif
  38283. return EXPECT_RESULT();
  38284. }
  38285. static int test_wolfSSL_SHA512_224_Transform(void)
  38286. {
  38287. EXPECT_DECLS;
  38288. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  38289. !defined(WOLFSSL_NOSHA512_224)
  38290. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  38291. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  38292. !defined(WOLFSSL_KCAPI_HASH)
  38293. byte input1[] = "";
  38294. byte input2[] = "abc";
  38295. byte local[WC_SHA512_BLOCK_SIZE];
  38296. word32 sLen = 0;
  38297. unsigned char output1[] =
  38298. "\x94\x24\x66\xd4\x60\x3a\xeb\x23\x1d\xa8\x69\x31\x3c\xd2\xde\x11"
  38299. "\x48\x0f\x4a\x5a\xdf\x3a\x8d\x87\xcf\xcd\xbf\xa5\x03\x21\x50\xf1"
  38300. "\x8a\x0d\x0f\x0d\x3c\x07\xba\x52\xe0\xaa\x3c\xbb\xf1\xd3\x3f\xca"
  38301. "\x12\xa7\x61\xf8\x47\xda\x0d\x1b\x79\xc2\x65\x13\x92\xc1\x9c\xa5";
  38302. unsigned char output2[] =
  38303. "\x51\x28\xe7\x0b\xca\x1e\xbc\x5f\xd7\x34\x0b\x48\x30\xd7\xc2\x75"
  38304. "\x6d\x8d\x48\x2c\x1f\xc7\x9e\x2b\x20\x5e\xbb\x0f\x0e\x4d\xb7\x61"
  38305. "\x31\x76\x33\xa0\xb4\x3d\x5f\x93\xc1\x73\xac\xf7\x21\xff\x69\x17"
  38306. "\xce\x66\xe5\x1e\x31\xe7\xf3\x22\x0f\x0b\x34\xd7\x5a\x57\xeb\xbf";
  38307. union {
  38308. wc_Sha512 native;
  38309. SHA512_CTX compat;
  38310. } sha512;
  38311. #ifdef BIG_ENDIAN_ORDER
  38312. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  38313. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  38314. #endif
  38315. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  38316. XMEMSET(&local, 0, sizeof(local));
  38317. /* sanity check */
  38318. ExpectIntEQ(SHA512_224_Transform(NULL, NULL), 0);
  38319. ExpectIntEQ(SHA512_224_Transform(NULL, (const byte*)&input1), 0);
  38320. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, NULL), 0);
  38321. ExpectIntEQ(wc_Sha512_224Transform(NULL, NULL), BAD_FUNC_ARG);
  38322. ExpectIntEQ(wc_Sha512_224Transform(NULL, (const byte*)&input1),
  38323. BAD_FUNC_ARG);
  38324. ExpectIntEQ(wc_Sha512_224Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  38325. /* Init SHA512 CTX */
  38326. ExpectIntEQ(wolfSSL_SHA512_224_Init(&sha512.compat), 1);
  38327. /* Do Transform*/
  38328. sLen = (word32)XSTRLEN((char*)input1);
  38329. XMEMCPY(local, input1, sLen);
  38330. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  38331. 1);
  38332. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  38333. WC_SHA512_DIGEST_SIZE), 0);
  38334. /* frees resources */
  38335. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  38336. /* Init SHA512 CTX */
  38337. ExpectIntEQ(SHA512_224_Init(&sha512.compat), 1);
  38338. sLen = (word32)XSTRLEN((char*)input2);
  38339. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  38340. XMEMCPY(local, input2, sLen);
  38341. ExpectIntEQ(SHA512_224_Transform(&sha512.compat, (const byte*)&local[0]),
  38342. 1);
  38343. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  38344. WC_SHA512_DIGEST_SIZE), 0);
  38345. /* frees resources */
  38346. ExpectIntEQ(SHA512_224_Final(local, &sha512.compat), 1);
  38347. #endif
  38348. #endif
  38349. return EXPECT_RESULT();
  38350. }
  38351. static int test_wolfSSL_SHA512_256_Transform(void)
  38352. {
  38353. EXPECT_DECLS;
  38354. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA512) && \
  38355. !defined(WOLFSSL_NOSHA512_256)
  38356. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  38357. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))) && \
  38358. !defined(WOLFSSL_KCAPI_HASH)
  38359. byte input1[] = "";
  38360. byte input2[] = "abc";
  38361. byte local[WC_SHA512_BLOCK_SIZE];
  38362. word32 sLen = 0;
  38363. unsigned char output1[] =
  38364. "\xf8\x37\x37\x5a\xd7\x2e\x56\xec\xe2\x51\xa8\x31\x3a\xa0\x63\x2b"
  38365. "\x7e\x7c\x64\xcc\xd9\xff\x2b\x6b\xeb\xc3\xd4\x4d\x7f\x8a\x3a\xb5"
  38366. "\x61\x85\x0b\x37\x30\x9f\x3b\x08\x5e\x7b\xd3\xbc\x6d\x00\x61\xc0"
  38367. "\x65\x9a\xd7\x73\xda\x40\xbe\xc1\xe5\x2f\xc6\x5d\xb7\x9f\xbe\x60";
  38368. unsigned char output2[] =
  38369. "\x22\xad\xc0\x30\xee\xd4\x6a\xef\x13\xee\x5a\x95\x8b\x1f\xb7\xb6"
  38370. "\xb6\xba\xc0\x44\xb8\x18\x3b\xf0\xf6\x4b\x70\x9f\x03\xba\x64\xa1"
  38371. "\xe1\xe3\x45\x15\x91\x7d\xcb\x0b\x9a\xf0\xd2\x8e\x47\x8b\x37\x78"
  38372. "\x91\x41\xa6\xc4\xb0\x29\x8f\x8b\xdd\x78\x5c\xf2\x73\x3f\x21\x31";
  38373. union {
  38374. wc_Sha512 native;
  38375. SHA512_CTX compat;
  38376. } sha512;
  38377. #ifdef BIG_ENDIAN_ORDER
  38378. ByteReverseWords64((word64*)output1, (word64*)output1, sizeof(output1));
  38379. ByteReverseWords64((word64*)output2, (word64*)output2, sizeof(output2));
  38380. #endif
  38381. XMEMSET(&sha512.compat, 0, sizeof(sha512.compat));
  38382. XMEMSET(&local, 0, sizeof(local));
  38383. /* sanity check */
  38384. ExpectIntEQ(SHA512_256_Transform(NULL, NULL), 0);
  38385. ExpectIntEQ(SHA512_256_Transform(NULL, (const byte*)&input1), 0);
  38386. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, NULL), 0);
  38387. ExpectIntEQ(wc_Sha512_256Transform(NULL, NULL), BAD_FUNC_ARG);
  38388. ExpectIntEQ(wc_Sha512_256Transform(NULL, (const byte*)&input1),
  38389. BAD_FUNC_ARG);
  38390. ExpectIntEQ(wc_Sha512_256Transform(&sha512.native, NULL), BAD_FUNC_ARG);
  38391. /* Init SHA512 CTX */
  38392. ExpectIntEQ(wolfSSL_SHA512_256_Init(&sha512.compat), 1);
  38393. /* Do Transform*/
  38394. sLen = (word32)XSTRLEN((char*)input1);
  38395. XMEMCPY(local, input1, sLen);
  38396. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  38397. 1);
  38398. ExpectIntEQ(XMEMCMP(sha512.native.digest, output1,
  38399. WC_SHA512_DIGEST_SIZE), 0);
  38400. /* frees resources */
  38401. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  38402. /* Init SHA512 CTX */
  38403. ExpectIntEQ(SHA512_256_Init(&sha512.compat), 1);
  38404. sLen = (word32)XSTRLEN((char*)input2);
  38405. XMEMSET(local, 0, WC_SHA512_BLOCK_SIZE);
  38406. XMEMCPY(local, input2, sLen);
  38407. ExpectIntEQ(SHA512_256_Transform(&sha512.compat, (const byte*)&local[0]),
  38408. 1);
  38409. ExpectIntEQ(XMEMCMP(sha512.native.digest, output2,
  38410. WC_SHA512_DIGEST_SIZE), 0);
  38411. /* frees resources */
  38412. ExpectIntEQ(SHA512_256_Final(local, &sha512.compat), 1);
  38413. #endif
  38414. #endif
  38415. return EXPECT_RESULT();
  38416. }
  38417. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  38418. /* helper function for test_wolfSSL_HMAC_CTX, digest size is expected to be a
  38419. * buffer of 64 bytes.
  38420. *
  38421. * returns the size of the digest buffer on success and a negative value on
  38422. * failure.
  38423. */
  38424. static int test_HMAC_CTX_helper(const EVP_MD* type, unsigned char* digest,
  38425. int* sz)
  38426. {
  38427. EXPECT_DECLS;
  38428. HMAC_CTX ctx1;
  38429. HMAC_CTX ctx2;
  38430. unsigned char key[] = "\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b\x0b"
  38431. "\x0b\x0b\x0b\x0b\x0b\x0b\x0b";
  38432. unsigned char long_key[] =
  38433. "0123456789012345678901234567890123456789"
  38434. "0123456789012345678901234567890123456789"
  38435. "0123456789012345678901234567890123456789"
  38436. "0123456789012345678901234567890123456789";
  38437. unsigned char msg[] = "message to hash";
  38438. unsigned int digestSz = 64;
  38439. int keySz = sizeof(key);
  38440. int long_keySz = sizeof(long_key);
  38441. int msgSz = sizeof(msg);
  38442. unsigned char digest2[64];
  38443. unsigned int digestSz2 = 64;
  38444. HMAC_CTX_init(&ctx1);
  38445. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  38446. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38447. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38448. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38449. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38450. HMAC_CTX_cleanup(&ctx1);
  38451. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38452. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz2), SSL_SUCCESS);
  38453. HMAC_CTX_cleanup(&ctx2);
  38454. ExpectIntEQ(digestSz, digestSz2);
  38455. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38456. /* test HMAC_Init with NULL key */
  38457. /* init after copy */
  38458. HMAC_CTX_init(&ctx1);
  38459. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  38460. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38461. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38462. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  38463. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38464. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38465. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38466. HMAC_CTX_cleanup(&ctx1);
  38467. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  38468. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38469. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38470. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  38471. HMAC_CTX_cleanup(&ctx2);
  38472. ExpectIntEQ(digestSz, digestSz2);
  38473. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38474. /* long key */
  38475. HMAC_CTX_init(&ctx1);
  38476. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)long_key, long_keySz, type),
  38477. SSL_SUCCESS);
  38478. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38479. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38480. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  38481. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38482. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38483. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38484. HMAC_CTX_cleanup(&ctx1);
  38485. ExpectIntEQ(HMAC_Init(&ctx2, NULL, 0, NULL), SSL_SUCCESS);
  38486. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38487. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38488. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  38489. HMAC_CTX_cleanup(&ctx2);
  38490. ExpectIntEQ(digestSz, digestSz2);
  38491. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38492. /* init before copy */
  38493. HMAC_CTX_init(&ctx1);
  38494. ExpectIntEQ(HMAC_Init(&ctx1, (const void*)key, keySz, type), SSL_SUCCESS);
  38495. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38496. ExpectIntEQ(HMAC_Init(&ctx1, NULL, 0, NULL), SSL_SUCCESS);
  38497. ExpectIntEQ(HMAC_CTX_copy(&ctx2, &ctx1), SSL_SUCCESS);
  38498. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38499. ExpectIntEQ(HMAC_Update(&ctx1, msg, msgSz), SSL_SUCCESS);
  38500. ExpectIntEQ(HMAC_Final(&ctx1, digest, &digestSz), SSL_SUCCESS);
  38501. HMAC_CTX_cleanup(&ctx1);
  38502. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38503. ExpectIntEQ(HMAC_Update(&ctx2, msg, msgSz), SSL_SUCCESS);
  38504. ExpectIntEQ(HMAC_Final(&ctx2, digest2, &digestSz), SSL_SUCCESS);
  38505. HMAC_CTX_cleanup(&ctx2);
  38506. ExpectIntEQ(digestSz, digestSz2);
  38507. ExpectIntEQ(XMEMCMP(digest, digest2, digestSz), 0);
  38508. *sz = (int)digestSz;
  38509. return EXPECT_RESULT();
  38510. }
  38511. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_HMAC) */
  38512. static int test_wolfSSL_HMAC_CTX(void)
  38513. {
  38514. EXPECT_DECLS;
  38515. #if defined(OPENSSL_EXTRA) && !defined(NO_HMAC)
  38516. unsigned char digest[64];
  38517. int digestSz;
  38518. WOLFSSL_HMAC_CTX* hmac_ctx = NULL;
  38519. WOLFSSL_HMAC_CTX ctx1;
  38520. WOLFSSL_HMAC_CTX ctx2;
  38521. ExpectNotNull(hmac_ctx = wolfSSL_HMAC_CTX_new());
  38522. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(NULL), 1);
  38523. ExpectIntEQ(wolfSSL_HMAC_CTX_Init(hmac_ctx), 1);
  38524. wolfSSL_HMAC_CTX_free(NULL);
  38525. wolfSSL_HMAC_CTX_free(hmac_ctx);
  38526. XMEMSET(&ctx2, 0, sizeof(WOLFSSL_HMAC_CTX));
  38527. ExpectIntEQ(HMAC_CTX_init(NULL), 1);
  38528. ExpectIntEQ(HMAC_CTX_init(&ctx2), 1);
  38529. ExpectIntEQ(HMAC_CTX_copy(NULL, NULL), 0);
  38530. ExpectIntEQ(HMAC_CTX_copy(NULL, &ctx2), 0);
  38531. ExpectIntEQ(HMAC_CTX_copy(&ctx2, NULL), 0);
  38532. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  38533. ((! defined(HAVE_FIPS_VERSION)) || \
  38534. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  38535. /* Copy object that hasn't had a digest set - MD5. */
  38536. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 1);
  38537. #else
  38538. /* Copy object that hasn't had a digest set. */
  38539. ExpectIntEQ(HMAC_CTX_copy(&ctx1, &ctx2), 0);
  38540. #endif
  38541. HMAC_CTX_cleanup(NULL);
  38542. HMAC_CTX_cleanup(&ctx2);
  38543. ExpectNull(HMAC_CTX_get_md(NULL));
  38544. #ifndef NO_SHA
  38545. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha1(), digest, &digestSz)),
  38546. TEST_SUCCESS);
  38547. ExpectIntEQ(digestSz, 20);
  38548. ExpectIntEQ(XMEMCMP("\xD9\x68\x77\x23\x70\xFB\x53\x70\x53\xBA\x0E\xDC\xDA"
  38549. "\xBF\x03\x98\x31\x19\xB2\xCC", digest, digestSz), 0);
  38550. #endif /* !NO_SHA */
  38551. #ifdef WOLFSSL_SHA224
  38552. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha224(), digest, &digestSz)),
  38553. TEST_SUCCESS);
  38554. ExpectIntEQ(digestSz, 28);
  38555. ExpectIntEQ(XMEMCMP("\x57\xFD\xF4\xE1\x2D\xB0\x79\xD7\x4B\x25\x7E\xB1\x95"
  38556. "\x9C\x11\xAC\x2D\x1E\x78\x94\x4F\x3A\x0F\xED\xF8\xAD"
  38557. "\x02\x0E", digest, digestSz), 0);
  38558. #endif /* WOLFSSL_SHA224 */
  38559. #ifndef NO_SHA256
  38560. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha256(), digest, &digestSz)),
  38561. TEST_SUCCESS);
  38562. ExpectIntEQ(digestSz, 32);
  38563. ExpectIntEQ(XMEMCMP("\x13\xAB\x76\x91\x0C\x37\x86\x8D\xB3\x7E\x30\x0C\xFC"
  38564. "\xB0\x2E\x8E\x4A\xD7\xD4\x25\xCC\x3A\xA9\x0F\xA2\xF2"
  38565. "\x47\x1E\x62\x6F\x5D\xF2", digest, digestSz), 0);
  38566. #endif /* !NO_SHA256 */
  38567. #ifdef WOLFSSL_SHA384
  38568. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha384(), digest, &digestSz)),
  38569. TEST_SUCCESS);
  38570. ExpectIntEQ(digestSz, 48);
  38571. ExpectIntEQ(XMEMCMP("\x9E\xCB\x07\x0C\x11\x76\x3F\x23\xC3\x25\x0E\xC4\xB7"
  38572. "\x28\x77\x95\x99\xD5\x9D\x7A\xBB\x1A\x9F\xB7\xFD\x25"
  38573. "\xC9\x72\x47\x9F\x8F\x86\x76\xD6\x20\x57\x87\xB7\xE7"
  38574. "\xCD\xFB\xC2\xCC\x9F\x2B\xC5\x41\xAB",
  38575. digest, digestSz), 0);
  38576. #endif /* WOLFSSL_SHA384 */
  38577. #ifdef WOLFSSL_SHA512
  38578. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha512(), digest, &digestSz)),
  38579. TEST_SUCCESS);
  38580. ExpectIntEQ(digestSz, 64);
  38581. ExpectIntEQ(XMEMCMP("\xD4\x21\x0C\x8B\x60\x6F\xF4\xBF\x07\x2F\x26\xCC\xAD"
  38582. "\xBC\x06\x0B\x34\x78\x8B\x4F\xD6\xC0\x42\xF1\x33\x10"
  38583. "\x6C\x4F\x1E\x55\x59\xDD\x2A\x9F\x15\x88\x62\xF8\x60"
  38584. "\xA3\x99\x91\xE2\x08\x7B\xF7\x95\x3A\xB0\x92\x48\x60"
  38585. "\x88\x8B\x5B\xB8\x5F\xE9\xB6\xB1\x96\xE3\xB5\xF0",
  38586. digest, digestSz), 0);
  38587. #endif /* WOLFSSL_SHA512 */
  38588. #ifdef WOLFSSL_SHA3
  38589. #ifndef WOLFSSL_NOSHA3_224
  38590. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_224(), digest, &digestSz)),
  38591. TEST_SUCCESS);
  38592. ExpectIntEQ(digestSz, 28);
  38593. ExpectIntEQ(XMEMCMP("\xdc\x53\x25\x3f\xc0\x9d\x2b\x0c\x7f\x59\x11\x17\x08"
  38594. "\x5c\xe8\x43\x31\x01\x5a\xb3\xe3\x08\x37\x71\x26\x0b"
  38595. "\x29\x0f", digest, digestSz), 0);
  38596. #endif
  38597. #ifndef WOLFSSL_NOSHA3_256
  38598. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_256(), digest, &digestSz)),
  38599. TEST_SUCCESS);
  38600. ExpectIntEQ(digestSz, 32);
  38601. ExpectIntEQ(XMEMCMP("\x0f\x00\x89\x82\x15\xce\xd6\x45\x01\x83\xce\xc8\x35"
  38602. "\xab\x71\x07\xc9\xfe\x61\x22\x38\xf9\x09\xad\x35\x65"
  38603. "\x43\x77\x24\xd4\x1e\xf4", digest, digestSz), 0);
  38604. #endif
  38605. #ifndef WOLFSSL_NOSHA3_384
  38606. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_384(), digest, &digestSz)),
  38607. TEST_SUCCESS);
  38608. ExpectIntEQ(digestSz, 48);
  38609. ExpectIntEQ(XMEMCMP("\x0f\x6a\xc0\xfb\xc3\xf2\x80\xb1\xb4\x04\xb6\xc8\x45"
  38610. "\x23\x3b\xb4\xbe\xc6\xea\x85\x07\xca\x8c\x71\xbb\x6e"
  38611. "\x79\xf6\xf9\x2b\x98\xf5\xef\x11\x39\xd4\x5d\xd3\xca"
  38612. "\xc0\xe6\x81\xf7\x73\xf9\x85\x5d\x4f",
  38613. digest, digestSz), 0);
  38614. #endif
  38615. #ifndef WOLFSSL_NOSHA3_512
  38616. ExpectIntEQ((test_HMAC_CTX_helper(EVP_sha3_512(), digest, &digestSz)),
  38617. TEST_SUCCESS);
  38618. ExpectIntEQ(digestSz, 64);
  38619. ExpectIntEQ(XMEMCMP("\x3e\x77\xe3\x59\x42\x89\xed\xc3\xa4\x26\x3d\xa4\x75"
  38620. "\xd2\x84\x8c\xb2\xf3\x25\x04\x47\x61\xce\x1c\x42\x86"
  38621. "\xcd\xf4\x56\xaa\x2f\x84\xb1\x3b\x18\xed\xe6\xd6\x48"
  38622. "\x15\xb0\x29\xc5\x9d\x32\xef\xdd\x3e\x09\xf6\xed\x9e"
  38623. "\x70\xbc\x1c\x63\xf7\x3b\x3e\xe1\xdc\x84\x9c\x1c",
  38624. digest, digestSz), 0);
  38625. #endif
  38626. #endif
  38627. #if !defined(NO_MD5) && (!defined(HAVE_FIPS_VERSION) || \
  38628. HAVE_FIPS_VERSION <= 2)
  38629. ExpectIntEQ((test_HMAC_CTX_helper(EVP_md5(), digest, &digestSz)),
  38630. TEST_SUCCESS);
  38631. ExpectIntEQ(digestSz, 16);
  38632. ExpectIntEQ(XMEMCMP("\xB7\x27\xC4\x41\xE5\x2E\x62\xBA\x54\xED\x72\x70\x9F"
  38633. "\xE4\x98\xDD", digest, digestSz), 0);
  38634. #endif /* !NO_MD5 */
  38635. #endif
  38636. return EXPECT_RESULT();
  38637. }
  38638. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  38639. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  38640. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  38641. static int test_openssl_hmac(const WOLFSSL_EVP_MD* md, int md_len)
  38642. {
  38643. EXPECT_DECLS;
  38644. static const unsigned char key[] = "simple test key";
  38645. HMAC_CTX* hmac = NULL;
  38646. ENGINE* e = NULL;
  38647. unsigned char hash[WC_MAX_DIGEST_SIZE];
  38648. unsigned int len;
  38649. ExpectNotNull(hmac = HMAC_CTX_new());
  38650. HMAC_CTX_init(hmac);
  38651. #if defined(HAVE_SELFTEST) || (defined(HAVE_FIPS) && \
  38652. ((! defined(HAVE_FIPS_VERSION)) || \
  38653. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION <= 2)))
  38654. /* Get size on object that hasn't had a digest set - MD5. */
  38655. ExpectIntEQ(HMAC_size(hmac), 16);
  38656. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 1);
  38657. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 1);
  38658. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 1);
  38659. #else
  38660. ExpectIntEQ(HMAC_size(hmac), BAD_FUNC_ARG);
  38661. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, NULL), 0);
  38662. ExpectIntEQ(HMAC_Init(hmac, (void*)key, (int)sizeof(key), NULL), 0);
  38663. ExpectIntEQ(HMAC_Init(hmac, NULL, 0, md), 0);
  38664. #endif
  38665. ExpectIntEQ(HMAC_Init_ex(NULL, (void*)key, (int)sizeof(key), md, e), 0);
  38666. ExpectIntEQ(HMAC_Init_ex(hmac, (void*)key, (int)sizeof(key), md, e), 1);
  38667. /* re-using test key as data to hash */
  38668. ExpectIntEQ(HMAC_Update(NULL, key, (int)sizeof(key)), 0);
  38669. ExpectIntEQ(HMAC_Update(hmac, key, (int)sizeof(key)), 1);
  38670. ExpectIntEQ(HMAC_Update(hmac, key, 0), 1);
  38671. ExpectIntEQ(HMAC_Update(hmac, NULL, 0), 1);
  38672. ExpectIntEQ(HMAC_Update(hmac, NULL, (int)sizeof(key)), 1);
  38673. ExpectIntEQ(HMAC_Final(NULL, NULL, &len), 0);
  38674. ExpectIntEQ(HMAC_Final(hmac, NULL, &len), 0);
  38675. ExpectIntEQ(HMAC_Final(NULL, hash, &len), 0);
  38676. ExpectIntEQ(HMAC_Final(hmac, hash, &len), 1);
  38677. ExpectIntEQ(HMAC_Final(hmac, hash, NULL), 1);
  38678. ExpectIntEQ(len, md_len);
  38679. ExpectIntEQ(HMAC_size(NULL), 0);
  38680. ExpectIntEQ(HMAC_size(hmac), md_len);
  38681. ExpectStrEQ(HMAC_CTX_get_md(hmac), md);
  38682. HMAC_cleanup(NULL);
  38683. HMAC_cleanup(hmac);
  38684. HMAC_CTX_free(hmac);
  38685. len = 0;
  38686. ExpectNull(HMAC(NULL, key, (int)sizeof(key), NULL, 0, hash, &len));
  38687. ExpectNull(HMAC(md, NULL, (int)sizeof(key), NULL, 0, hash, &len));
  38688. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, 0, NULL, &len));
  38689. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, &len));
  38690. ExpectIntEQ(len, md_len);
  38691. ExpectNotNull(HMAC(md, key, (int)sizeof(key), NULL, 0, hash, NULL));
  38692. /* With data. */
  38693. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, (int)sizeof(key), hash,
  38694. &len));
  38695. /* With NULL data. */
  38696. ExpectNull(HMAC(md, key, (int)sizeof(key), NULL, (int)sizeof(key), hash,
  38697. &len));
  38698. /* With zero length data. */
  38699. ExpectNotNull(HMAC(md, key, (int)sizeof(key), key, 0, hash, &len));
  38700. return EXPECT_RESULT();
  38701. }
  38702. #endif
  38703. static int test_wolfSSL_HMAC(void)
  38704. {
  38705. EXPECT_DECLS;
  38706. #if defined(OPENSSL_EXTRA) && (!defined(NO_SHA256) || \
  38707. defined(WOLFSSL_SHA224) || defined(WOLFSSL_SHA384) || \
  38708. defined(WOLFSSL_SHA512) || defined(WOLFSSL_SHA3))
  38709. #ifndef NO_SHA256
  38710. ExpectIntEQ(test_openssl_hmac(EVP_sha256(), (int)WC_SHA256_DIGEST_SIZE),
  38711. TEST_SUCCESS);
  38712. #endif
  38713. #ifdef WOLFSSL_SHA224
  38714. ExpectIntEQ(test_openssl_hmac(EVP_sha224(), (int)WC_SHA224_DIGEST_SIZE),
  38715. TEST_SUCCESS);
  38716. #endif
  38717. #ifdef WOLFSSL_SHA384
  38718. ExpectIntEQ(test_openssl_hmac(EVP_sha384(), (int)WC_SHA384_DIGEST_SIZE),
  38719. TEST_SUCCESS);
  38720. #endif
  38721. #ifdef WOLFSSL_SHA512
  38722. ExpectIntEQ(test_openssl_hmac(EVP_sha512(), (int)WC_SHA512_DIGEST_SIZE),
  38723. TEST_SUCCESS);
  38724. #endif
  38725. #ifdef WOLFSSL_SHA3
  38726. #ifndef WOLFSSL_NOSHA3_224
  38727. ExpectIntEQ(test_openssl_hmac(EVP_sha3_224(),
  38728. (int)WC_SHA3_224_DIGEST_SIZE), TEST_SUCCESS);
  38729. #endif
  38730. #ifndef WOLFSSL_NOSHA3_256
  38731. ExpectIntEQ(test_openssl_hmac(EVP_sha3_256(),
  38732. (int)WC_SHA3_256_DIGEST_SIZE), TEST_SUCCESS);
  38733. #endif
  38734. #ifndef WOLFSSL_NOSHA3_384
  38735. ExpectIntEQ(test_openssl_hmac(EVP_sha3_384(),
  38736. (int)WC_SHA3_384_DIGEST_SIZE), TEST_SUCCESS);
  38737. #endif
  38738. #ifndef WOLFSSL_NOSHA3_512
  38739. ExpectIntEQ(test_openssl_hmac(EVP_sha3_512(),
  38740. (int)WC_SHA3_512_DIGEST_SIZE), TEST_SUCCESS);
  38741. #endif
  38742. #endif
  38743. #ifndef NO_SHA
  38744. ExpectIntEQ(test_openssl_hmac(EVP_sha1(), (int)WC_SHA_DIGEST_SIZE),
  38745. TEST_SUCCESS);
  38746. #endif
  38747. #endif
  38748. return EXPECT_RESULT();
  38749. }
  38750. static int test_wolfSSL_CMAC(void)
  38751. {
  38752. EXPECT_DECLS;
  38753. #if defined(WOLFSSL_CMAC) && defined(OPENSSL_EXTRA) && \
  38754. defined(WOLFSSL_AES_DIRECT)
  38755. int i;
  38756. byte key[AES_256_KEY_SIZE];
  38757. CMAC_CTX* cmacCtx = NULL;
  38758. byte out[AES_BLOCK_SIZE];
  38759. size_t outLen = AES_BLOCK_SIZE;
  38760. for (i=0; i < AES_256_KEY_SIZE; ++i) {
  38761. key[i] = i;
  38762. }
  38763. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38764. /* Check CMAC_CTX_get0_cipher_ctx; return value not used. */
  38765. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  38766. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  38767. NULL), 1);
  38768. /* re-using test key as data to hash */
  38769. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  38770. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  38771. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 1);
  38772. ExpectIntEQ(outLen, AES_BLOCK_SIZE);
  38773. /* No Update works. */
  38774. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  38775. NULL), 1);
  38776. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  38777. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_cbc(),
  38778. NULL), 1);
  38779. /* Test parameters with CMAC_Update. */
  38780. ExpectIntEQ(CMAC_Update(NULL, NULL, 0), 0);
  38781. ExpectIntEQ(CMAC_Update(NULL, key, 0), 0);
  38782. ExpectIntEQ(CMAC_Update(NULL, NULL, AES_128_KEY_SIZE), 0);
  38783. ExpectIntEQ(CMAC_Update(NULL, key, AES_128_KEY_SIZE), 0);
  38784. ExpectIntEQ(CMAC_Update(cmacCtx, key, 0), 1);
  38785. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, 0), 1);
  38786. ExpectIntEQ(CMAC_Update(cmacCtx, NULL, AES_128_KEY_SIZE), 1);
  38787. /* Test parameters with CMAC_Final. */
  38788. ExpectIntEQ(CMAC_Final(NULL, NULL, NULL), 0);
  38789. ExpectIntEQ(CMAC_Final(NULL, out, NULL), 0);
  38790. ExpectIntEQ(CMAC_Final(NULL, NULL, &outLen), 0);
  38791. ExpectIntEQ(CMAC_Final(NULL, out, &outLen), 0);
  38792. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, NULL), 1);
  38793. ExpectIntEQ(CMAC_Final(cmacCtx, NULL, &outLen), 1);
  38794. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  38795. CMAC_CTX_free(cmacCtx);
  38796. /* Test parameters with CMAC Init. */
  38797. cmacCtx = NULL;
  38798. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38799. ExpectNotNull(CMAC_CTX_get0_cipher_ctx(cmacCtx));
  38800. ExpectIntEQ(CMAC_Init(NULL, NULL, 0, NULL, NULL), 0);
  38801. ExpectIntEQ(CMAC_Init(NULL, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  38802. NULL), 0);
  38803. ExpectIntEQ(CMAC_Init(cmacCtx, NULL, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  38804. NULL), 0);
  38805. /* give a key too small for the cipher, verify we get failure */
  38806. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_192_cbc(),
  38807. NULL), 0);
  38808. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, NULL, NULL), 0);
  38809. #if defined(HAVE_AESGCM) && defined(WOLFSSL_AES_128)
  38810. /* Only AES-CBC supported. */
  38811. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_128_KEY_SIZE, EVP_aes_128_gcm(),
  38812. NULL), 0);
  38813. #endif
  38814. CMAC_CTX_free(cmacCtx);
  38815. ExpectNull(CMAC_CTX_get0_cipher_ctx(NULL));
  38816. cmacCtx = NULL;
  38817. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38818. /* No Init. */
  38819. ExpectIntEQ(CMAC_Final(cmacCtx, out, &outLen), 0);
  38820. CMAC_CTX_free(cmacCtx);
  38821. /* Test AES-256-CBC */
  38822. cmacCtx = NULL;
  38823. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38824. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_256_KEY_SIZE, EVP_aes_256_cbc(),
  38825. NULL), 1);
  38826. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  38827. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  38828. CMAC_CTX_free(cmacCtx);
  38829. /* Test AES-192-CBC */
  38830. cmacCtx = NULL;
  38831. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38832. ExpectIntEQ(CMAC_Init(cmacCtx, key, AES_192_KEY_SIZE, EVP_aes_192_cbc(),
  38833. NULL), 1);
  38834. ExpectIntEQ(CMAC_Update(cmacCtx, key, AES_128_KEY_SIZE), 1);
  38835. ExpectIntEQ(CMAC_Final(cmacCtx, out, NULL), 1);
  38836. CMAC_CTX_free(cmacCtx);
  38837. cmacCtx = NULL;
  38838. ExpectNotNull(cmacCtx = CMAC_CTX_new());
  38839. CMAC_CTX_free(cmacCtx);
  38840. #endif /* WOLFSSL_CMAC && OPENSSL_EXTRA && WOLFSSL_AES_DIRECT */
  38841. return EXPECT_RESULT();
  38842. }
  38843. static int test_wolfSSL_DES(void)
  38844. {
  38845. EXPECT_DECLS;
  38846. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  38847. const_DES_cblock myDes;
  38848. DES_cblock iv;
  38849. DES_key_schedule key;
  38850. word32 i = 0;
  38851. DES_LONG dl = 0;
  38852. unsigned char msg[] = "hello wolfssl";
  38853. unsigned char weakKey[][8] = {
  38854. { 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01 },
  38855. { 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE, 0xFE },
  38856. { 0xE0, 0xE0, 0xE0, 0xE0, 0xF1, 0xF1, 0xF1, 0xF1 },
  38857. { 0x1F, 0x1F, 0x1F, 0x1F, 0x0E, 0x0E, 0x0E, 0x0E }
  38858. };
  38859. unsigned char semiWeakKey[][8] = {
  38860. { 0x01, 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E },
  38861. { 0x1F, 0x01, 0x1F, 0x01, 0x0E, 0x01, 0x0E, 0x01 },
  38862. { 0x01, 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1 },
  38863. { 0xE0, 0x01, 0xE0, 0x01, 0xF1, 0x01, 0xF1, 0x01 },
  38864. { 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE },
  38865. { 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01, 0xFE, 0x01 },
  38866. { 0x1F, 0xE0, 0x1F, 0xE0, 0x0E, 0xF1, 0x0E, 0xF1 },
  38867. { 0xE0, 0x1F, 0xE0, 0x1F, 0xF1, 0x0E, 0xF1, 0x0E },
  38868. { 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E, 0xFE },
  38869. { 0xFE, 0x1F, 0xFE, 0x1F, 0xFE, 0x0E, 0xFE, 0x0E },
  38870. { 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1, 0xFE },
  38871. { 0xFE, 0xE0, 0xFE, 0xE0, 0xFE, 0xF1, 0xFE, 0xF1 }
  38872. };
  38873. DES_check_key(1);
  38874. DES_set_key(&myDes, &key);
  38875. /* check, check of odd parity */
  38876. XMEMSET(myDes, 4, sizeof(const_DES_cblock));
  38877. myDes[0] = 6; /* set even parity */
  38878. XMEMSET(key, 5, sizeof(DES_key_schedule));
  38879. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -1);
  38880. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  38881. ExpectIntEQ(DES_set_key_checked(NULL, NULL), -2);
  38882. ExpectIntEQ(DES_set_key_checked(&myDes, NULL), -2);
  38883. ExpectIntEQ(DES_set_key_checked(NULL, &key), -2);
  38884. /* set odd parity for success case */
  38885. DES_set_odd_parity(&myDes);
  38886. ExpectIntEQ(DES_check_key_parity(&myDes), 1);
  38887. fprintf(stderr, "%02x %02x %02x %02x", myDes[0], myDes[1], myDes[2],
  38888. myDes[3]);
  38889. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  38890. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  38891. ExpectIntEQ(key[i], myDes[i]);
  38892. }
  38893. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  38894. /* check weak key */
  38895. XMEMSET(myDes, 1, sizeof(const_DES_cblock));
  38896. XMEMSET(key, 5, sizeof(DES_key_schedule));
  38897. ExpectIntEQ(DES_set_key_checked(&myDes, &key), -2);
  38898. ExpectIntNE(key[0], myDes[0]); /* should not have copied over key */
  38899. DES_set_key_unchecked(NULL, NULL);
  38900. DES_set_key_unchecked(&myDes, NULL);
  38901. DES_set_key_unchecked(NULL, &key);
  38902. /* compare arrays, should be the same */
  38903. /* now do unchecked copy of a weak key over */
  38904. DES_set_key_unchecked(&myDes, &key);
  38905. /* compare arrays, should be the same */
  38906. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  38907. ExpectIntEQ(key[i], myDes[i]);
  38908. }
  38909. ExpectIntEQ(DES_is_weak_key(&myDes), 1);
  38910. myDes[7] = 2;
  38911. ExpectIntEQ(DES_set_key_checked(&myDes, &key), 0);
  38912. ExpectIntEQ(DES_is_weak_key(&myDes), 0);
  38913. ExpectIntEQ(DES_is_weak_key(NULL), 1);
  38914. /* Test all weak keys. */
  38915. for (i = 0; i < sizeof(weakKey) / sizeof(*weakKey); i++) {
  38916. ExpectIntEQ(DES_set_key_checked(&weakKey[i], &key), -2);
  38917. }
  38918. /* Test all semi-weak keys. */
  38919. for (i = 0; i < sizeof(semiWeakKey) / sizeof(*semiWeakKey); i++) {
  38920. ExpectIntEQ(DES_set_key_checked(&semiWeakKey[i], &key), -2);
  38921. }
  38922. /* check DES_key_sched API */
  38923. XMEMSET(key, 1, sizeof(DES_key_schedule));
  38924. ExpectIntEQ(DES_key_sched(&myDes, NULL), 0);
  38925. ExpectIntEQ(DES_key_sched(NULL, &key), 0);
  38926. ExpectIntEQ(DES_key_sched(&myDes, &key), 0);
  38927. /* compare arrays, should be the same */
  38928. for (i = 0; i < sizeof(DES_key_schedule); i++) {
  38929. ExpectIntEQ(key[i], myDes[i]);
  38930. }
  38931. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, NULL)), 0);
  38932. ExpectIntEQ((DES_cbc_cksum(msg, NULL, 0, NULL, NULL)), 0);
  38933. ExpectIntEQ((DES_cbc_cksum(NULL, &key, 0, NULL, NULL)), 0);
  38934. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, &myDes, NULL)), 0);
  38935. ExpectIntEQ((DES_cbc_cksum(NULL, NULL, 0, NULL, &iv)), 0);
  38936. ExpectIntEQ((DES_cbc_cksum(NULL, &key, sizeof(msg), &myDes, &iv)), 0);
  38937. ExpectIntEQ((DES_cbc_cksum(msg, NULL, sizeof(msg), &myDes, &iv)), 0);
  38938. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), NULL, &iv)), 0);
  38939. ExpectIntEQ((DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, NULL)), 0);
  38940. /* DES_cbc_cksum should return the last 4 of the last 8 bytes after
  38941. * DES_cbc_encrypt on the input */
  38942. XMEMSET(iv, 0, sizeof(DES_cblock));
  38943. XMEMSET(myDes, 5, sizeof(DES_key_schedule));
  38944. ExpectIntGT((dl = DES_cbc_cksum(msg, &key, sizeof(msg), &myDes, &iv)), 0);
  38945. ExpectIntEQ(dl, 480052723);
  38946. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_DES3) */
  38947. return EXPECT_RESULT();
  38948. }
  38949. static int test_wolfSSL_DES_ncbc(void)
  38950. {
  38951. EXPECT_DECLS;
  38952. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  38953. const_DES_cblock myDes;
  38954. DES_cblock iv = {1};
  38955. DES_key_schedule key = {0};
  38956. unsigned char msg[] = "hello wolfssl";
  38957. unsigned char out[DES_BLOCK_SIZE * 2] = {0};
  38958. unsigned char pln[DES_BLOCK_SIZE * 2] = {0};
  38959. unsigned char exp[] = {0x31, 0x98, 0x2F, 0x3A, 0x55, 0xBF, 0xD8, 0xC4};
  38960. unsigned char exp2[] = {0xC7, 0x45, 0x8B, 0x28, 0x10, 0x53, 0xE0, 0x58};
  38961. /* partial block test */
  38962. DES_set_key(&key, &myDes);
  38963. DES_ncbc_encrypt(msg, out, 3, &myDes, &iv, DES_ENCRYPT);
  38964. ExpectIntEQ(XMEMCMP(exp, out, DES_BLOCK_SIZE), 0);
  38965. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  38966. DES_set_key(&key, &myDes);
  38967. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38968. *((byte*)&iv) = 1;
  38969. DES_ncbc_encrypt(out, pln, 3, &myDes, &iv, DES_DECRYPT);
  38970. ExpectIntEQ(XMEMCMP(msg, pln, 3), 0);
  38971. ExpectIntEQ(XMEMCMP(exp, iv, DES_BLOCK_SIZE), 0);
  38972. /* full block test */
  38973. DES_set_key(&key, &myDes);
  38974. XMEMSET(pln, 0, DES_BLOCK_SIZE);
  38975. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38976. *((byte*)&iv) = 1;
  38977. DES_ncbc_encrypt(msg, out, 8, &myDes, &iv, DES_ENCRYPT);
  38978. ExpectIntEQ(XMEMCMP(exp2, out, DES_BLOCK_SIZE), 0);
  38979. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  38980. DES_set_key(&key, &myDes);
  38981. XMEMSET((byte*)&iv, 0, DES_BLOCK_SIZE);
  38982. *((byte*)&iv) = 1;
  38983. DES_ncbc_encrypt(out, pln, 8, &myDes, &iv, DES_DECRYPT);
  38984. ExpectIntEQ(XMEMCMP(msg, pln, 8), 0);
  38985. ExpectIntEQ(XMEMCMP(exp2, iv, DES_BLOCK_SIZE), 0);
  38986. #endif
  38987. return EXPECT_RESULT();
  38988. }
  38989. static int test_wolfSSL_DES_ecb_encrypt(void)
  38990. {
  38991. EXPECT_DECLS;
  38992. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3) && defined(WOLFSSL_DES_ECB)
  38993. WOLFSSL_DES_cblock input1, input2, output1, output2, back1, back2;
  38994. WOLFSSL_DES_key_schedule key;
  38995. XMEMCPY(key, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  38996. XMEMCPY(input1, "Iamhuman", sizeof(WOLFSSL_DES_cblock));
  38997. XMEMCPY(input2, "Whoisit?", sizeof(WOLFSSL_DES_cblock));
  38998. XMEMSET(output1, 0, sizeof(WOLFSSL_DES_cblock));
  38999. XMEMSET(output2, 0, sizeof(WOLFSSL_DES_cblock));
  39000. XMEMSET(back1, 0, sizeof(WOLFSSL_DES_cblock));
  39001. XMEMSET(back2, 0, sizeof(WOLFSSL_DES_cblock));
  39002. wolfSSL_DES_ecb_encrypt(NULL, NULL, NULL, DES_ENCRYPT);
  39003. wolfSSL_DES_ecb_encrypt(&input1, NULL, NULL, DES_ENCRYPT);
  39004. wolfSSL_DES_ecb_encrypt(NULL, &output1, NULL, DES_ENCRYPT);
  39005. wolfSSL_DES_ecb_encrypt(NULL, NULL, &key, DES_ENCRYPT);
  39006. wolfSSL_DES_ecb_encrypt(&input1, &output1, NULL, DES_ENCRYPT);
  39007. wolfSSL_DES_ecb_encrypt(&input1, NULL, &key, DES_ENCRYPT);
  39008. wolfSSL_DES_ecb_encrypt(NULL, &output1, &key, DES_ENCRYPT);
  39009. /* Encrypt messages */
  39010. wolfSSL_DES_ecb_encrypt(&input1, &output1, &key, DES_ENCRYPT);
  39011. wolfSSL_DES_ecb_encrypt(&input2, &output2, &key, DES_ENCRYPT);
  39012. {
  39013. /* Decrypt messages */
  39014. int ret1 = 0;
  39015. int ret2 = 0;
  39016. wolfSSL_DES_ecb_encrypt(&output1, &back1, &key, DES_DECRYPT);
  39017. ExpectIntEQ(ret1 = XMEMCMP((unsigned char *)back1,
  39018. (unsigned char *)input1, sizeof(WOLFSSL_DES_cblock)), 0);
  39019. wolfSSL_DES_ecb_encrypt(&output2, &back2, &key, DES_DECRYPT);
  39020. ExpectIntEQ(ret2 = XMEMCMP((unsigned char *)back2,
  39021. (unsigned char *)input2, sizeof(WOLFSSL_DES_cblock)), 0);
  39022. }
  39023. #endif
  39024. return EXPECT_RESULT();
  39025. }
  39026. static int test_wolfSSL_DES_ede3_cbc_encrypt(void)
  39027. {
  39028. EXPECT_DECLS;
  39029. #if defined(OPENSSL_EXTRA) && !defined(NO_DES3)
  39030. unsigned char input1[8], input2[8];
  39031. unsigned char output1[8], output2[8];
  39032. unsigned char back1[8], back2[8];
  39033. WOLFSSL_DES_cblock iv1, iv2;
  39034. WOLFSSL_DES_key_schedule key1, key2, key3;
  39035. int i;
  39036. XMEMCPY(key1, "12345678", sizeof(WOLFSSL_DES_key_schedule));
  39037. XMEMCPY(key2, "23456781", sizeof(WOLFSSL_DES_key_schedule));
  39038. XMEMCPY(key3, "34567823", sizeof(WOLFSSL_DES_key_schedule));
  39039. XMEMCPY(input1, "Iamhuman", sizeof(input1));
  39040. XMEMCPY(input2, "Whoisit?", sizeof(input2));
  39041. XMEMSET(output1, 0, sizeof(output1));
  39042. XMEMSET(output2, 0, sizeof(output2));
  39043. XMEMSET(back1, 0, sizeof(back1));
  39044. XMEMSET(back2, 0, sizeof(back2));
  39045. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  39046. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  39047. /* Encrypt messages */
  39048. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, 8, &key1, &key2, &key3, &iv1,
  39049. DES_ENCRYPT);
  39050. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, 8, &key1, &key2, &key3, &iv2,
  39051. DES_ENCRYPT);
  39052. {
  39053. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  39054. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  39055. /* Decrypt messages */
  39056. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, 8, &key1, &key2, &key3,
  39057. &iv1, DES_DECRYPT);
  39058. ExpectIntEQ(XMEMCMP(back1, input1, sizeof(input1)), 0);
  39059. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, 8, &key1, &key2, &key3,
  39060. &iv2, DES_DECRYPT);
  39061. ExpectIntEQ(XMEMCMP(back2, input2, sizeof(input2)), 0);
  39062. }
  39063. for (i = 0; i < 8; i++) {
  39064. XMEMSET(output1, 0, sizeof(output1));
  39065. XMEMSET(output2, 0, sizeof(output2));
  39066. XMEMSET(back1, 0, sizeof(back1));
  39067. XMEMSET(back2, 0, sizeof(back2));
  39068. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  39069. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  39070. /* Encrypt partial messages */
  39071. wolfSSL_DES_ede3_cbc_encrypt(input1, output1, i, &key1, &key2, &key3,
  39072. &iv1, DES_ENCRYPT);
  39073. wolfSSL_DES_ede3_cbc_encrypt(input2, output2, i, &key1, &key2, &key3,
  39074. &iv2, DES_ENCRYPT);
  39075. {
  39076. XMEMCPY(iv1, "87654321", sizeof(WOLFSSL_DES_cblock));
  39077. XMEMCPY(iv2, "98765432", sizeof(WOLFSSL_DES_cblock));
  39078. /* Decrypt messages */
  39079. wolfSSL_DES_ede3_cbc_encrypt(output1, back1, i, &key1, &key2,
  39080. &key3, &iv1, DES_DECRYPT);
  39081. ExpectIntEQ(XMEMCMP(back1, input1, i), 0);
  39082. wolfSSL_DES_ede3_cbc_encrypt(output2, back2, i, &key1, &key2,
  39083. &key3, &iv2, DES_DECRYPT);
  39084. ExpectIntEQ(XMEMCMP(back2, input2, i), 0);
  39085. }
  39086. }
  39087. #endif
  39088. return EXPECT_RESULT();
  39089. }
  39090. static int test_wolfSSL_AES_encrypt(void)
  39091. {
  39092. EXPECT_DECLS;
  39093. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  39094. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  39095. AES_KEY enc;
  39096. AES_KEY dec;
  39097. const byte msg[] = {
  39098. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  39099. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  39100. };
  39101. const byte exp[] = {
  39102. 0xf3, 0xee, 0xd1, 0xbd, 0xb5, 0xd2, 0xa0, 0x3c,
  39103. 0x06, 0x4b, 0x5a, 0x7e, 0x3d, 0xb1, 0x81, 0xf8,
  39104. };
  39105. const byte key[] = {
  39106. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  39107. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  39108. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  39109. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  39110. };
  39111. byte eout[sizeof(msg)];
  39112. byte dout[sizeof(msg)];
  39113. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &enc), 0);
  39114. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &dec), 0);
  39115. wolfSSL_AES_encrypt(NULL, NULL, NULL);
  39116. wolfSSL_AES_encrypt(msg, NULL, NULL);
  39117. wolfSSL_AES_encrypt(NULL, eout, NULL);
  39118. wolfSSL_AES_encrypt(NULL, NULL, &enc);
  39119. wolfSSL_AES_encrypt(msg, eout, NULL);
  39120. wolfSSL_AES_encrypt(msg, NULL, &enc);
  39121. wolfSSL_AES_encrypt(NULL, eout, &enc);
  39122. wolfSSL_AES_decrypt(NULL, NULL, NULL);
  39123. wolfSSL_AES_decrypt(eout, NULL, NULL);
  39124. wolfSSL_AES_decrypt(NULL, dout, NULL);
  39125. wolfSSL_AES_decrypt(NULL, NULL, &dec);
  39126. wolfSSL_AES_decrypt(eout, dout, NULL);
  39127. wolfSSL_AES_decrypt(eout, NULL, &dec);
  39128. wolfSSL_AES_decrypt(NULL, dout, &dec);
  39129. wolfSSL_AES_encrypt(msg, eout, &enc);
  39130. ExpectIntEQ(XMEMCMP(eout, exp, AES_BLOCK_SIZE), 0);
  39131. wolfSSL_AES_decrypt(eout, dout, &dec);
  39132. ExpectIntEQ(XMEMCMP(dout, msg, AES_BLOCK_SIZE), 0);
  39133. #endif
  39134. return EXPECT_RESULT();
  39135. }
  39136. static int test_wolfSSL_AES_ecb_encrypt(void)
  39137. {
  39138. EXPECT_DECLS;
  39139. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AES_ECB) \
  39140. && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  39141. AES_KEY aes;
  39142. const byte msg[] =
  39143. {
  39144. 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  39145. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a
  39146. };
  39147. const byte verify[] =
  39148. {
  39149. 0xf3,0xee,0xd1,0xbd,0xb5,0xd2,0xa0,0x3c,
  39150. 0x06,0x4b,0x5a,0x7e,0x3d,0xb1,0x81,0xf8
  39151. };
  39152. const byte key[] =
  39153. {
  39154. 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  39155. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  39156. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  39157. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4
  39158. };
  39159. byte out[AES_BLOCK_SIZE];
  39160. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aes), 0);
  39161. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39162. AES_ecb_encrypt(msg, out, &aes, AES_ENCRYPT);
  39163. ExpectIntEQ(XMEMCMP(out, verify, AES_BLOCK_SIZE), 0);
  39164. #ifdef HAVE_AES_DECRYPT
  39165. ExpectIntEQ(AES_set_decrypt_key(key, sizeof(key)*8, &aes), 0);
  39166. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39167. AES_ecb_encrypt(verify, out, &aes, AES_DECRYPT);
  39168. ExpectIntEQ(XMEMCMP(out, msg, AES_BLOCK_SIZE), 0);
  39169. #endif
  39170. /* test bad arguments */
  39171. AES_ecb_encrypt(NULL, out, &aes, AES_DECRYPT);
  39172. AES_ecb_encrypt(verify, NULL, &aes, AES_DECRYPT);
  39173. AES_ecb_encrypt(verify, out, NULL, AES_DECRYPT);
  39174. #endif
  39175. return EXPECT_RESULT();
  39176. }
  39177. static int test_wolfSSL_AES_cbc_encrypt(void)
  39178. {
  39179. EXPECT_DECLS;
  39180. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  39181. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  39182. AES_KEY aes;
  39183. AES_KEY* aesN = NULL;
  39184. size_t len = 0;
  39185. size_t lenB = 0;
  39186. int keySz0 = 0;
  39187. int keySzN = -1;
  39188. byte out[AES_BLOCK_SIZE] = {0};
  39189. byte* outN = NULL;
  39190. /* Test vectors retrieved from:
  39191. * <begin URL>
  39192. * https://csrc.nist.gov/
  39193. * CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/
  39194. * documents/aes/KAT_AES.zip
  39195. * </end URL>
  39196. */
  39197. const byte* pt128N = NULL;
  39198. byte* key128N = NULL;
  39199. byte* iv128N = NULL;
  39200. byte iv128tmp[AES_BLOCK_SIZE] = {0};
  39201. const byte pt128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  39202. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  39203. const byte ct128[] = { 0x87,0x85,0xb1,0xa7,0x5b,0x0f,0x3b,0xd9,
  39204. 0x58,0xdc,0xd0,0xe2,0x93,0x18,0xc5,0x21 };
  39205. const byte iv128[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  39206. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 };
  39207. byte key128[] = { 0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
  39208. 0xff,0xff,0xf0,0x00,0x00,0x00,0x00,0x00 };
  39209. len = sizeof(pt128);
  39210. #define STRESS_T(a, b, c, d, e, f, g, h, i) \
  39211. wolfSSL_AES_cbc_encrypt(a, b, c, d, e, f); \
  39212. ExpectIntNE(XMEMCMP(b, g, h), i)
  39213. #define RESET_IV(x, y) XMEMCPY(x, y, AES_BLOCK_SIZE)
  39214. /* Stressing wolfSSL_AES_cbc_encrypt() */
  39215. STRESS_T(pt128N, out, len, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  39216. STRESS_T(pt128, out, len, &aes, iv128N, 1, ct128, AES_BLOCK_SIZE, 0);
  39217. wolfSSL_AES_cbc_encrypt(pt128, outN, len, &aes, iv128tmp, AES_ENCRYPT);
  39218. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  39219. wolfSSL_AES_cbc_encrypt(pt128, out, len, aesN, iv128tmp, AES_ENCRYPT);
  39220. ExpectIntNE(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  39221. STRESS_T(pt128, out, lenB, &aes, iv128tmp, 1, ct128, AES_BLOCK_SIZE, 0);
  39222. /* Stressing wolfSSL_AES_set_encrypt_key */
  39223. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128N, sizeof(key128)*8, &aes),0);
  39224. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, aesN),0);
  39225. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySz0, &aes), 0);
  39226. ExpectIntNE(wolfSSL_AES_set_encrypt_key(key128, keySzN, &aes), 0);
  39227. /* Stressing wolfSSL_AES_set_decrypt_key */
  39228. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, &aes),0);
  39229. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128N, sizeof(key128)*8, aesN),0);
  39230. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySz0, &aes), 0);
  39231. ExpectIntNE(wolfSSL_AES_set_decrypt_key(key128, keySzN, &aes), 0);
  39232. #ifdef WOLFSSL_AES_128
  39233. /* wolfSSL_AES_cbc_encrypt() 128-bit */
  39234. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39235. RESET_IV(iv128tmp, iv128);
  39236. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key128, sizeof(key128)*8, &aes), 0);
  39237. wolfSSL_AES_cbc_encrypt(pt128, out, len, &aes, iv128tmp, AES_ENCRYPT);
  39238. ExpectIntEQ(XMEMCMP(out, ct128, AES_BLOCK_SIZE), 0);
  39239. wc_AesFree((Aes*)&aes);
  39240. #ifdef HAVE_AES_DECRYPT
  39241. /* wolfSSL_AES_cbc_encrypt() 128-bit in decrypt mode */
  39242. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39243. RESET_IV(iv128tmp, iv128);
  39244. len = sizeof(ct128);
  39245. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key128, sizeof(key128)*8, &aes), 0);
  39246. wolfSSL_AES_cbc_encrypt(ct128, out, len, &aes, iv128tmp, AES_DECRYPT);
  39247. ExpectIntEQ(XMEMCMP(out, pt128, AES_BLOCK_SIZE), 0);
  39248. wc_AesFree((Aes*)&aes);
  39249. #endif
  39250. #endif /* WOLFSSL_AES_128 */
  39251. #ifdef WOLFSSL_AES_192
  39252. {
  39253. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition
  39254. * Appendix F.2.3 */
  39255. byte iv192tmp[AES_BLOCK_SIZE] = {0};
  39256. const byte pt192[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  39257. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  39258. const byte ct192[] = { 0x4f,0x02,0x1d,0xb2,0x43,0xbc,0x63,0x3d,
  39259. 0x71,0x78,0x18,0x3a,0x9f,0xa0,0x71,0xe8 };
  39260. const byte iv192[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  39261. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  39262. byte key192[] = { 0x8e,0x73,0xb0,0xf7,0xda,0x0e,0x64,0x52,
  39263. 0xc8,0x10,0xf3,0x2b,0x80,0x90,0x79,0xe5,
  39264. 0x62,0xf8,0xea,0xd2,0x52,0x2c,0x6b,0x7b };
  39265. len = sizeof(pt192);
  39266. /* wolfSSL_AES_cbc_encrypt() 192-bit */
  39267. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39268. RESET_IV(iv192tmp, iv192);
  39269. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key192, sizeof(key192)*8, &aes), 0);
  39270. wolfSSL_AES_cbc_encrypt(pt192, out, len, &aes, iv192tmp, AES_ENCRYPT);
  39271. ExpectIntEQ(XMEMCMP(out, ct192, AES_BLOCK_SIZE), 0);
  39272. wc_AesFree((Aes*)&aes);
  39273. #ifdef HAVE_AES_DECRYPT
  39274. /* wolfSSL_AES_cbc_encrypt() 192-bit in decrypt mode */
  39275. len = sizeof(ct192);
  39276. RESET_IV(iv192tmp, iv192);
  39277. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39278. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key192, sizeof(key192)*8, &aes), 0);
  39279. wolfSSL_AES_cbc_encrypt(ct192, out, len, &aes, iv192tmp, AES_DECRYPT);
  39280. ExpectIntEQ(XMEMCMP(out, pt192, AES_BLOCK_SIZE), 0);
  39281. wc_AesFree((Aes*)&aes);
  39282. #endif
  39283. }
  39284. #endif /* WOLFSSL_AES_192 */
  39285. #ifdef WOLFSSL_AES_256
  39286. {
  39287. /* Test vectors from NIST Special Publication 800-38A, 2001 Edition,
  39288. * Appendix F.2.5 */
  39289. byte iv256tmp[AES_BLOCK_SIZE] = {0};
  39290. const byte pt256[] = { 0x6b,0xc1,0xbe,0xe2,0x2e,0x40,0x9f,0x96,
  39291. 0xe9,0x3d,0x7e,0x11,0x73,0x93,0x17,0x2a };
  39292. const byte ct256[] = { 0xf5,0x8c,0x4c,0x04,0xd6,0xe5,0xf1,0xba,
  39293. 0x77,0x9e,0xab,0xfb,0x5f,0x7b,0xfb,0xd6 };
  39294. const byte iv256[] = { 0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,
  39295. 0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F };
  39296. byte key256[] = { 0x60,0x3d,0xeb,0x10,0x15,0xca,0x71,0xbe,
  39297. 0x2b,0x73,0xae,0xf0,0x85,0x7d,0x77,0x81,
  39298. 0x1f,0x35,0x2c,0x07,0x3b,0x61,0x08,0xd7,
  39299. 0x2d,0x98,0x10,0xa3,0x09,0x14,0xdf,0xf4 };
  39300. len = sizeof(pt256);
  39301. /* wolfSSL_AES_cbc_encrypt() 256-bit */
  39302. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39303. RESET_IV(iv256tmp, iv256);
  39304. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39305. wolfSSL_AES_cbc_encrypt(pt256, out, len, &aes, iv256tmp, AES_ENCRYPT);
  39306. ExpectIntEQ(XMEMCMP(out, ct256, AES_BLOCK_SIZE), 0);
  39307. wc_AesFree((Aes*)&aes);
  39308. #ifdef HAVE_AES_DECRYPT
  39309. /* wolfSSL_AES_cbc_encrypt() 256-bit in decrypt mode */
  39310. len = sizeof(ct256);
  39311. RESET_IV(iv256tmp, iv256);
  39312. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39313. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39314. wolfSSL_AES_cbc_encrypt(ct256, out, len, &aes, iv256tmp, AES_DECRYPT);
  39315. ExpectIntEQ(XMEMCMP(out, pt256, AES_BLOCK_SIZE), 0);
  39316. wc_AesFree((Aes*)&aes);
  39317. #endif
  39318. #if defined(HAVE_AES_KEYWRAP) && !defined(HAVE_FIPS) && \
  39319. !defined(HAVE_SELFTEST)
  39320. {
  39321. byte wrapCipher[sizeof(key256) + KEYWRAP_BLOCK_SIZE] = { 0 };
  39322. byte wrapPlain[sizeof(key256)] = { 0 };
  39323. byte wrapIV[KEYWRAP_BLOCK_SIZE] = { 0 };
  39324. /* wolfSSL_AES_wrap_key() 256-bit NULL iv */
  39325. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39326. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  39327. 15), WOLFSSL_FAILURE);
  39328. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256,
  39329. sizeof(key256)), sizeof(wrapCipher));
  39330. wc_AesFree((Aes*)&aes);
  39331. /* wolfSSL_AES_unwrap_key() 256-bit NULL iv */
  39332. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39333. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  39334. 23), WOLFSSL_FAILURE);
  39335. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher,
  39336. sizeof(wrapCipher)), sizeof(wrapPlain));
  39337. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  39338. XMEMSET(wrapCipher, 0, sizeof(wrapCipher));
  39339. XMEMSET(wrapPlain, 0, sizeof(wrapPlain));
  39340. wc_AesFree((Aes*)&aes);
  39341. /* wolfSSL_AES_wrap_key() 256-bit custom iv */
  39342. ExpectIntEQ(wolfSSL_AES_set_encrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39343. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, key256,
  39344. sizeof(key256)), sizeof(wrapCipher));
  39345. wc_AesFree((Aes*)&aes);
  39346. /* wolfSSL_AES_unwrap_key() 256-bit custom iv */
  39347. ExpectIntEQ(wolfSSL_AES_set_decrypt_key(key256, sizeof(key256)*8, &aes), 0);
  39348. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, wrapPlain, wrapCipher,
  39349. sizeof(wrapCipher)), sizeof(wrapPlain));
  39350. ExpectIntEQ(XMEMCMP(wrapPlain, key256, sizeof(key256)), 0);
  39351. wc_AesFree((Aes*)&aes);
  39352. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, NULL, 0), 0);
  39353. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, NULL, NULL, 0), 0);
  39354. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  39355. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, wrapCipher, NULL, 0), 0);
  39356. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, NULL, NULL, key256, 0), 0);
  39357. ExpectIntEQ(wolfSSL_AES_wrap_key(NULL, wrapIV, wrapCipher, key256, 0), 0);
  39358. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, NULL, wrapCipher, key256, 0), 0);
  39359. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, NULL, key256, 0), 0);
  39360. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapCipher, NULL, 0), 0);
  39361. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, NULL, 0), 0);
  39362. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, NULL, NULL, 0), 0);
  39363. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, NULL, NULL, 0), 0);
  39364. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, wrapPlain, NULL, 0), 0);
  39365. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, NULL, NULL, wrapCipher, 0), 0);
  39366. ExpectIntEQ(wolfSSL_AES_unwrap_key(NULL, wrapIV, wrapPlain, wrapCipher, 0),
  39367. 0);
  39368. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, NULL, wrapPlain, wrapCipher, 0),
  39369. 0);
  39370. ExpectIntEQ(wolfSSL_AES_unwrap_key(&aes, wrapIV, NULL, wrapCipher, 0), 0);
  39371. ExpectIntEQ(wolfSSL_AES_wrap_key(&aes, wrapIV, wrapPlain, NULL, 0), 0);
  39372. }
  39373. #endif /* HAVE_AES_KEYWRAP */
  39374. }
  39375. #endif /* WOLFSSL_AES_256 */
  39376. #endif
  39377. return EXPECT_RESULT();
  39378. }
  39379. static int test_wolfSSL_AES_cfb128_encrypt(void)
  39380. {
  39381. EXPECT_DECLS;
  39382. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(WOLFSSL_AES_CFB) && \
  39383. !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  39384. AES_KEY aesEnc;
  39385. AES_KEY aesDec;
  39386. const byte msg[] = {
  39387. 0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96,
  39388. 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a
  39389. };
  39390. const byte exp[] = {
  39391. 0x16, 0xc9, 0x90, 0x6c, 0x04, 0x0c, 0xd1, 0x2f,
  39392. 0x84, 0x7b, 0x18, 0xed, 0xed, 0x6a, 0xb5, 0xfd
  39393. };
  39394. const byte key[] = {
  39395. 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe,
  39396. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  39397. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  39398. 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4
  39399. };
  39400. const byte ivData[] = {
  39401. 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
  39402. 0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7,
  39403. };
  39404. byte out[AES_BLOCK_SIZE];
  39405. byte iv[AES_BLOCK_SIZE];
  39406. word32 i;
  39407. int num;
  39408. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  39409. XMEMCPY(iv, ivData, sizeof(iv));
  39410. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39411. AES_cfb128_encrypt(msg, out, sizeof(msg), &aesEnc, iv, NULL, AES_ENCRYPT);
  39412. ExpectIntEQ(XMEMCMP(out, exp, sizeof(msg)), 0);
  39413. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39414. #ifdef HAVE_AES_DECRYPT
  39415. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  39416. XMEMCPY(iv, ivData, sizeof(iv));
  39417. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39418. AES_cfb128_encrypt(exp, out, sizeof(msg), &aesDec, iv, NULL, AES_DECRYPT);
  39419. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  39420. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39421. #endif
  39422. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(msg)); i++) {
  39423. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesEnc), 0);
  39424. XMEMCPY(iv, ivData, sizeof(iv));
  39425. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39426. AES_cfb128_encrypt(msg, out, i, &aesEnc, iv, &num, AES_ENCRYPT);
  39427. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  39428. ExpectIntEQ(XMEMCMP(out, exp, i), 0);
  39429. if (i == 0) {
  39430. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39431. }
  39432. else {
  39433. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39434. }
  39435. #ifdef HAVE_AES_DECRYPT
  39436. ExpectIntEQ(AES_set_encrypt_key(key, sizeof(key)*8, &aesDec), 0);
  39437. XMEMCPY(iv, ivData, sizeof(iv));
  39438. XMEMSET(out, 0, AES_BLOCK_SIZE);
  39439. AES_cfb128_encrypt(exp, out, i, &aesDec, iv, &num, AES_DECRYPT);
  39440. ExpectIntEQ(num, i % AES_BLOCK_SIZE);
  39441. ExpectIntEQ(XMEMCMP(out, msg, i), 0);
  39442. if (i == 0) {
  39443. ExpectIntEQ(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39444. }
  39445. else {
  39446. ExpectIntNE(XMEMCMP(iv, ivData, sizeof(iv)), 0);
  39447. }
  39448. #endif
  39449. }
  39450. if (EXPECT_SUCCESS()) {
  39451. /* test bad arguments */
  39452. AES_cfb128_encrypt(NULL, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  39453. AES_cfb128_encrypt(msg, NULL, 0, NULL, NULL, NULL, AES_DECRYPT);
  39454. AES_cfb128_encrypt(NULL, out, 0, NULL, NULL, NULL, AES_DECRYPT);
  39455. AES_cfb128_encrypt(NULL, NULL, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  39456. AES_cfb128_encrypt(NULL, NULL, 0, NULL, iv, NULL, AES_DECRYPT);
  39457. AES_cfb128_encrypt(NULL, out, 0, &aesDec, iv, NULL, AES_DECRYPT);
  39458. AES_cfb128_encrypt(msg, NULL, 0, &aesDec, iv, NULL, AES_DECRYPT);
  39459. AES_cfb128_encrypt(msg, out, 0, NULL, iv, NULL, AES_DECRYPT);
  39460. AES_cfb128_encrypt(msg, out, 0, &aesDec, NULL, NULL, AES_DECRYPT);
  39461. }
  39462. #endif
  39463. return EXPECT_RESULT();
  39464. }
  39465. static int test_wolfSSL_CRYPTO_cts128(void)
  39466. {
  39467. EXPECT_DECLS;
  39468. #if !defined(NO_AES) && defined(HAVE_AES_CBC) && defined(OPENSSL_EXTRA) && \
  39469. defined(HAVE_CTS) && !defined(WOLFSSL_NO_OPENSSL_AES_LOW_LEVEL_API)
  39470. byte tmp[64]; /* Largest vector size */
  39471. /* Test vectors taken form RFC3962 Appendix B */
  39472. const testVector vects[] = {
  39473. {
  39474. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39475. "\x20",
  39476. "\xc6\x35\x35\x68\xf2\xbf\x8c\xb4\xd8\xa5\x80\x36\x2d\xa7\xff\x7f"
  39477. "\x97",
  39478. 17, 17
  39479. },
  39480. {
  39481. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39482. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20",
  39483. "\xfc\x00\x78\x3e\x0e\xfd\xb2\xc1\xd4\x45\xd4\xc8\xef\xf7\xed\x22"
  39484. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5",
  39485. 31, 31
  39486. },
  39487. {
  39488. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39489. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43",
  39490. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  39491. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84",
  39492. 32, 32
  39493. },
  39494. {
  39495. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39496. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  39497. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c",
  39498. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  39499. "\xb3\xff\xfd\x94\x0c\x16\xa1\x8c\x1b\x55\x49\xd2\xf8\x38\x02\x9e"
  39500. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5",
  39501. 47, 47
  39502. },
  39503. {
  39504. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39505. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  39506. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20",
  39507. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  39508. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8"
  39509. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8",
  39510. 48, 48
  39511. },
  39512. {
  39513. "\x49\x20\x77\x6f\x75\x6c\x64\x20\x6c\x69\x6b\x65\x20\x74\x68\x65"
  39514. "\x20\x47\x65\x6e\x65\x72\x61\x6c\x20\x47\x61\x75\x27\x73\x20\x43"
  39515. "\x68\x69\x63\x6b\x65\x6e\x2c\x20\x70\x6c\x65\x61\x73\x65\x2c\x20"
  39516. "\x61\x6e\x64\x20\x77\x6f\x6e\x74\x6f\x6e\x20\x73\x6f\x75\x70\x2e",
  39517. "\x97\x68\x72\x68\xd6\xec\xcc\xc0\xc0\x7b\x25\xe2\x5e\xcf\xe5\x84"
  39518. "\x39\x31\x25\x23\xa7\x86\x62\xd5\xbe\x7f\xcb\xcc\x98\xeb\xf5\xa8"
  39519. "\x48\x07\xef\xe8\x36\xee\x89\xa5\x26\x73\x0d\xbc\x2f\x7b\xc8\x40"
  39520. "\x9d\xad\x8b\xbb\x96\xc4\xcd\xc0\x3b\xc1\x03\xe1\xa1\x94\xbb\xd8",
  39521. 64, 64
  39522. }
  39523. };
  39524. byte keyBytes[AES_128_KEY_SIZE] = {
  39525. 0x63, 0x68, 0x69, 0x63, 0x6b, 0x65, 0x6e, 0x20,
  39526. 0x74, 0x65, 0x72, 0x69, 0x79, 0x61, 0x6b, 0x69
  39527. };
  39528. size_t i;
  39529. AES_KEY encKey;
  39530. byte iv[AES_IV_SIZE]; /* All-zero IV for all cases */
  39531. XMEMSET(tmp, 0, sizeof(tmp));
  39532. for (i = 0; i < sizeof(vects)/sizeof(vects[0]); i++) {
  39533. AES_KEY decKey;
  39534. ExpectIntEQ(AES_set_encrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  39535. &encKey), 0);
  39536. ExpectIntEQ(AES_set_decrypt_key(keyBytes, AES_128_KEY_SIZE * 8,
  39537. &decKey), 0);
  39538. XMEMSET(iv, 0, sizeof(iv));
  39539. ExpectIntEQ(CRYPTO_cts128_encrypt((const unsigned char*)vects[i].input,
  39540. tmp, vects[i].inLen, &encKey, iv, (cbc128_f)AES_cbc_encrypt),
  39541. vects[i].outLen);
  39542. ExpectIntEQ(XMEMCMP(tmp, vects[i].output, vects[i].outLen), 0);
  39543. XMEMSET(iv, 0, sizeof(iv));
  39544. ExpectIntEQ(CRYPTO_cts128_decrypt((const unsigned char*)vects[i].output,
  39545. tmp, vects[i].outLen, &decKey, iv, (cbc128_f)AES_cbc_encrypt),
  39546. vects[i].inLen);
  39547. ExpectIntEQ(XMEMCMP(tmp, vects[i].input, vects[i].inLen), 0);
  39548. }
  39549. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  39550. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  39551. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  39552. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  39553. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  39554. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, NULL, 17, NULL, NULL,
  39555. (cbc128_f)AES_cbc_encrypt), 0);
  39556. ExpectIntEQ(CRYPTO_cts128_encrypt(NULL, tmp, 17, &encKey, iv,
  39557. (cbc128_f)AES_cbc_encrypt), 0);
  39558. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, NULL, 17, &encKey, iv,
  39559. (cbc128_f)AES_cbc_encrypt), 0);
  39560. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, NULL, iv,
  39561. (cbc128_f)AES_cbc_encrypt), 0);
  39562. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, NULL,
  39563. (cbc128_f)AES_cbc_encrypt), 0);
  39564. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  39565. /* Length too small. */
  39566. ExpectIntEQ(CRYPTO_cts128_encrypt(tmp, tmp, 0, &encKey, iv,
  39567. (cbc128_f)AES_cbc_encrypt), 0);
  39568. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL, NULL), 0);
  39569. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, NULL, NULL, NULL), 0);
  39570. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, NULL, NULL, NULL), 0);
  39571. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, &encKey, NULL, NULL), 0);
  39572. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, iv, NULL), 0);
  39573. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, NULL, 17, NULL, NULL,
  39574. (cbc128_f)AES_cbc_encrypt), 0);
  39575. ExpectIntEQ(CRYPTO_cts128_decrypt(NULL, tmp, 17, &encKey, iv,
  39576. (cbc128_f)AES_cbc_encrypt), 0);
  39577. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, NULL, 17, &encKey, iv,
  39578. (cbc128_f)AES_cbc_encrypt), 0);
  39579. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, NULL, iv,
  39580. (cbc128_f)AES_cbc_encrypt), 0);
  39581. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, NULL,
  39582. (cbc128_f)AES_cbc_encrypt), 0);
  39583. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 17, &encKey, iv, NULL), 0);
  39584. /* Length too small. */
  39585. ExpectIntEQ(CRYPTO_cts128_decrypt(tmp, tmp, 0, &encKey, iv,
  39586. (cbc128_f)AES_cbc_encrypt), 0);
  39587. #endif /* !NO_AES && HAVE_AES_CBC && OPENSSL_EXTRA && HAVE_CTS */
  39588. return EXPECT_RESULT();
  39589. }
  39590. static int test_wolfSSL_RC4(void)
  39591. {
  39592. EXPECT_DECLS;
  39593. #if !defined(NO_RC4) && defined(OPENSSL_EXTRA)
  39594. WOLFSSL_RC4_KEY rc4Key;
  39595. unsigned char key[] = {
  39596. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  39597. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  39598. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  39599. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  39600. };
  39601. unsigned char data[] = {
  39602. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  39603. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  39604. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  39605. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  39606. };
  39607. unsigned char enc[sizeof(data)];
  39608. unsigned char dec[sizeof(data)];
  39609. word32 i;
  39610. word32 j;
  39611. wolfSSL_RC4_set_key(NULL, -1, NULL);
  39612. wolfSSL_RC4_set_key(&rc4Key, -1, NULL);
  39613. wolfSSL_RC4_set_key(NULL, 0, NULL);
  39614. wolfSSL_RC4_set_key(NULL, -1, key);
  39615. wolfSSL_RC4_set_key(&rc4Key, 0, NULL);
  39616. wolfSSL_RC4_set_key(&rc4Key, -1, key);
  39617. wolfSSL_RC4_set_key(NULL, 0, key);
  39618. wolfSSL_RC4(NULL, 0, NULL, NULL);
  39619. wolfSSL_RC4(&rc4Key, 0, NULL, NULL);
  39620. wolfSSL_RC4(NULL, 0, data, NULL);
  39621. wolfSSL_RC4(NULL, 0, NULL, enc);
  39622. wolfSSL_RC4(&rc4Key, 0, data, NULL);
  39623. wolfSSL_RC4(&rc4Key, 0, NULL, enc);
  39624. wolfSSL_RC4(NULL, 0, data, enc);
  39625. ExpectIntEQ(1, 1);
  39626. for (i = 0; EXPECT_SUCCESS() && (i <= sizeof(key)); i++) {
  39627. for (j = 0; EXPECT_SUCCESS() && (j <= sizeof(data)); j++) {
  39628. XMEMSET(enc, 0, sizeof(enc));
  39629. XMEMSET(dec, 0, sizeof(dec));
  39630. /* Encrypt */
  39631. wolfSSL_RC4_set_key(&rc4Key, (int)i, key);
  39632. wolfSSL_RC4(&rc4Key, j, data, enc);
  39633. /* Decrypt */
  39634. wolfSSL_RC4_set_key(&rc4Key, (int)i, key);
  39635. wolfSSL_RC4(&rc4Key, j, enc, dec);
  39636. ExpectIntEQ(XMEMCMP(dec, data, j), 0);
  39637. }
  39638. }
  39639. #endif
  39640. return EXPECT_RESULT();
  39641. }
  39642. static int test_wolfSSL_OBJ(void)
  39643. {
  39644. /* Password "wolfSSL test" is only 12 (96-bit) too short for testing in FIPS
  39645. * mode
  39646. */
  39647. EXPECT_DECLS;
  39648. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256) && !defined(NO_ASN) && \
  39649. !defined(HAVE_FIPS) && !defined(NO_SHA) && defined(WOLFSSL_CERT_EXT) && \
  39650. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && \
  39651. !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM)
  39652. ASN1_OBJECT *obj = NULL;
  39653. ASN1_OBJECT *obj2 = NULL;
  39654. char buf[50];
  39655. XFILE fp = XBADFILE;
  39656. X509 *x509 = NULL;
  39657. X509_NAME *x509Name = NULL;
  39658. X509_NAME_ENTRY *x509NameEntry = NULL;
  39659. ASN1_OBJECT *asn1Name = NULL;
  39660. int numNames = 0;
  39661. BIO *bio = NULL;
  39662. int nid;
  39663. int i, j;
  39664. const char *f[] = {
  39665. #ifndef NO_RSA
  39666. "./certs/ca-cert.der",
  39667. #endif
  39668. #ifdef HAVE_ECC
  39669. "./certs/ca-ecc-cert.der",
  39670. "./certs/ca-ecc384-cert.der",
  39671. #endif
  39672. NULL};
  39673. ASN1_OBJECT *field_name_obj = NULL;
  39674. int lastpos = -1;
  39675. int tmp = -1;
  39676. ASN1_STRING *asn1 = NULL;
  39677. unsigned char *buf_dyn = NULL;
  39678. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), SSL_FAILURE);
  39679. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  39680. ExpectIntEQ(OBJ_obj2nid(obj), NID_any_policy);
  39681. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 11);
  39682. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  39683. ASN1_OBJECT_free(obj);
  39684. obj = NULL;
  39685. ExpectNotNull(obj = OBJ_nid2obj(NID_sha256));
  39686. ExpectIntEQ(OBJ_obj2nid(obj), NID_sha256);
  39687. ExpectIntEQ(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1), 22);
  39688. #ifdef WOLFSSL_CERT_EXT
  39689. ExpectIntEQ(OBJ_txt2nid(buf), NID_sha256);
  39690. #endif
  39691. ExpectIntGT(OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0), 0);
  39692. ExpectNotNull(obj2 = OBJ_dup(obj));
  39693. ExpectIntEQ(OBJ_cmp(obj, obj2), 0);
  39694. ASN1_OBJECT_free(obj);
  39695. obj = NULL;
  39696. ASN1_OBJECT_free(obj2);
  39697. obj2 = NULL;
  39698. for (i = 0; f[i] != NULL; i++)
  39699. {
  39700. ExpectTrue((fp = XFOPEN(f[i], "rb")) != XBADFILE);
  39701. ExpectNotNull(x509 = d2i_X509_fp(fp, NULL));
  39702. if (fp != XBADFILE) {
  39703. XFCLOSE(fp);
  39704. fp = XBADFILE;
  39705. }
  39706. ExpectNotNull(x509Name = X509_get_issuer_name(x509));
  39707. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  39708. /* Get the Common Name by using OBJ_txt2obj */
  39709. ExpectNotNull(field_name_obj = OBJ_txt2obj("CN", 0));
  39710. do
  39711. {
  39712. lastpos = tmp;
  39713. tmp = X509_NAME_get_index_by_OBJ(x509Name, field_name_obj, lastpos);
  39714. } while (tmp > -1);
  39715. ExpectIntNE(lastpos, -1);
  39716. ASN1_OBJECT_free(field_name_obj);
  39717. field_name_obj = NULL;
  39718. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, lastpos));
  39719. ExpectNotNull(asn1 = X509_NAME_ENTRY_get_data(x509NameEntry));
  39720. ExpectIntGE(ASN1_STRING_to_UTF8(&buf_dyn, asn1), 0);
  39721. /*
  39722. * All Common Names should be www.wolfssl.com
  39723. * This makes testing easier as we can test for the expected value.
  39724. */
  39725. ExpectStrEQ((char*)buf_dyn, "www.wolfssl.com");
  39726. OPENSSL_free(buf_dyn);
  39727. buf_dyn = NULL;
  39728. bio = BIO_new(BIO_s_mem());
  39729. ExpectTrue(bio != NULL);
  39730. for (j = 0; j < numNames; j++)
  39731. {
  39732. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  39733. ExpectNotNull(asn1Name = X509_NAME_ENTRY_get_object(x509NameEntry));
  39734. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  39735. }
  39736. BIO_free(bio);
  39737. bio = NULL;
  39738. X509_free(x509);
  39739. x509 = NULL;
  39740. }
  39741. #ifdef HAVE_PKCS12
  39742. {
  39743. PKCS12 *p12 = NULL;
  39744. int boolRet;
  39745. EVP_PKEY *pkey = NULL;
  39746. const char *p12_f[] = {
  39747. #if !defined(NO_DES3) && !defined(NO_RSA)
  39748. "./certs/test-servercert.p12",
  39749. #endif
  39750. NULL};
  39751. for (i = 0; p12_f[i] != NULL; i++)
  39752. {
  39753. ExpectTrue((fp = XFOPEN(p12_f[i], "rb")) != XBADFILE);
  39754. ExpectNotNull(p12 = d2i_PKCS12_fp(fp, NULL));
  39755. if (fp != XBADFILE) {
  39756. XFCLOSE(fp);
  39757. fp = XBADFILE;
  39758. }
  39759. ExpectTrue((boolRet = PKCS12_parse(p12, "wolfSSL test",
  39760. &pkey, &x509, NULL)) > 0);
  39761. wc_PKCS12_free(p12);
  39762. p12 = NULL;
  39763. EVP_PKEY_free(pkey);
  39764. x509Name = X509_get_issuer_name(x509);
  39765. ExpectNotNull(x509Name);
  39766. ExpectIntNE((numNames = X509_NAME_entry_count(x509Name)), 0);
  39767. ExpectTrue((bio = BIO_new(BIO_s_mem())) != NULL);
  39768. for (j = 0; j < numNames; j++)
  39769. {
  39770. ExpectNotNull(x509NameEntry = X509_NAME_get_entry(x509Name, j));
  39771. ExpectNotNull(asn1Name =
  39772. X509_NAME_ENTRY_get_object(x509NameEntry));
  39773. ExpectTrue((nid = OBJ_obj2nid(asn1Name)) > 0);
  39774. }
  39775. BIO_free(bio);
  39776. bio = NULL;
  39777. X509_free(x509);
  39778. x509 = NULL;
  39779. }
  39780. }
  39781. #endif /* HAVE_PKCS12 */
  39782. #endif
  39783. return EXPECT_RESULT();
  39784. }
  39785. static int test_wolfSSL_OBJ_cmp(void)
  39786. {
  39787. EXPECT_DECLS;
  39788. #if defined(OPENSSL_EXTRA) && !defined(NO_SHA256)
  39789. ASN1_OBJECT *obj = NULL;
  39790. ASN1_OBJECT *obj2 = NULL;
  39791. ExpectNotNull(obj = OBJ_nid2obj(NID_any_policy));
  39792. ExpectNotNull(obj2 = OBJ_nid2obj(NID_sha256));
  39793. ExpectIntEQ(OBJ_cmp(NULL, NULL), WOLFSSL_FATAL_ERROR);
  39794. ExpectIntEQ(OBJ_cmp(obj, NULL), WOLFSSL_FATAL_ERROR);
  39795. ExpectIntEQ(OBJ_cmp(NULL, obj2), WOLFSSL_FATAL_ERROR);
  39796. ExpectIntEQ(OBJ_cmp(obj, obj2), WOLFSSL_FATAL_ERROR);
  39797. ExpectIntEQ(OBJ_cmp(obj, obj), 0);
  39798. ExpectIntEQ(OBJ_cmp(obj2, obj2), 0);
  39799. ASN1_OBJECT_free(obj);
  39800. ASN1_OBJECT_free(obj2);
  39801. #endif
  39802. return EXPECT_RESULT();
  39803. }
  39804. static int test_wolfSSL_OBJ_txt2nid(void)
  39805. {
  39806. EXPECT_DECLS;
  39807. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  39808. defined(WOLFSSL_APACHE_HTTPD)
  39809. int i;
  39810. static const struct {
  39811. const char* sn;
  39812. const char* ln;
  39813. const char* oid;
  39814. int nid;
  39815. } testVals[] = {
  39816. #ifdef WOLFSSL_APACHE_HTTPD
  39817. { "tlsfeature", "TLS Feature", "1.3.6.1.5.5.7.1.24", NID_tlsfeature },
  39818. { "id-on-dnsSRV", "SRVName", "1.3.6.1.5.5.7.8.7",
  39819. NID_id_on_dnsSRV },
  39820. { "msUPN", "Microsoft User Principal Name",
  39821. "1.3.6.1.4.1.311.20.2.3", NID_ms_upn },
  39822. #endif
  39823. { NULL, NULL, NULL, NID_undef }
  39824. };
  39825. /* Invalid cases */
  39826. ExpectIntEQ(OBJ_txt2nid(NULL), NID_undef);
  39827. ExpectIntEQ(OBJ_txt2nid("Bad name"), NID_undef);
  39828. /* Valid cases */
  39829. for (i = 0; testVals[i].sn != NULL; i++) {
  39830. ExpectIntEQ(OBJ_txt2nid(testVals[i].sn), testVals[i].nid);
  39831. ExpectIntEQ(OBJ_txt2nid(testVals[i].ln), testVals[i].nid);
  39832. ExpectIntEQ(OBJ_txt2nid(testVals[i].oid), testVals[i].nid);
  39833. }
  39834. #endif
  39835. return EXPECT_RESULT();
  39836. }
  39837. static int test_wolfSSL_OBJ_txt2obj(void)
  39838. {
  39839. EXPECT_DECLS;
  39840. #if defined(WOLFSSL_APACHE_HTTPD) || (defined(OPENSSL_EXTRA) && \
  39841. defined(WOLFSSL_CERT_EXT) && defined(WOLFSSL_CERT_GEN))
  39842. int i;
  39843. char buf[50];
  39844. ASN1_OBJECT* obj = NULL;
  39845. static const struct {
  39846. const char* oidStr;
  39847. const char* sn;
  39848. const char* ln;
  39849. } objs_list[] = {
  39850. #if defined(WOLFSSL_APACHE_HTTPD)
  39851. { "1.3.6.1.5.5.7.1.24", "tlsfeature", "TLS Feature" },
  39852. { "1.3.6.1.5.5.7.8.7", "id-on-dnsSRV", "SRVName" },
  39853. #endif
  39854. { "2.5.29.19", "basicConstraints", "X509v3 Basic Constraints"},
  39855. { NULL, NULL, NULL }
  39856. };
  39857. static const struct {
  39858. const char* numeric;
  39859. const char* name;
  39860. } objs_named[] = {
  39861. /* In dictionary but not in normal list. */
  39862. { "1.3.6.1.5.5.7.3.8", "Time Stamping" },
  39863. /* Made up OID. */
  39864. { "1.3.5.7", "1.3.5.7" },
  39865. { NULL, NULL }
  39866. };
  39867. ExpectNull(obj = OBJ_txt2obj("Bad name", 0));
  39868. ASN1_OBJECT_free(obj);
  39869. obj = NULL;
  39870. ExpectNull(obj = OBJ_txt2obj(NULL, 0));
  39871. ASN1_OBJECT_free(obj);
  39872. obj = NULL;
  39873. for (i = 0; objs_list[i].oidStr != NULL; i++) {
  39874. /* Test numerical value of oid (oidStr) */
  39875. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].oidStr, 1));
  39876. /* Convert object back to text to confirm oid is correct */
  39877. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  39878. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  39879. ASN1_OBJECT_free(obj);
  39880. obj = NULL;
  39881. XMEMSET(buf, 0, sizeof(buf));
  39882. /* Test short name (sn) */
  39883. ExpectNull(obj = OBJ_txt2obj(objs_list[i].sn, 1));
  39884. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].sn, 0));
  39885. /* Convert object back to text to confirm oid is correct */
  39886. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  39887. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  39888. ASN1_OBJECT_free(obj);
  39889. obj = NULL;
  39890. XMEMSET(buf, 0, sizeof(buf));
  39891. /* Test long name (ln) - should fail when no_name = 1 */
  39892. ExpectNull(obj = OBJ_txt2obj(objs_list[i].ln, 1));
  39893. ExpectNotNull(obj = OBJ_txt2obj(objs_list[i].ln, 0));
  39894. /* Convert object back to text to confirm oid is correct */
  39895. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  39896. ExpectIntEQ(XSTRNCMP(buf, objs_list[i].oidStr, (int)XSTRLEN(buf)), 0);
  39897. ASN1_OBJECT_free(obj);
  39898. obj = NULL;
  39899. XMEMSET(buf, 0, sizeof(buf));
  39900. }
  39901. for (i = 0; objs_named[i].numeric != NULL; i++) {
  39902. ExpectNotNull(obj = OBJ_txt2obj(objs_named[i].numeric, 1));
  39903. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 0);
  39904. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].name, (int)XSTRLEN(buf)), 0);
  39905. wolfSSL_OBJ_obj2txt(buf, (int)sizeof(buf), obj, 1);
  39906. ExpectIntEQ(XSTRNCMP(buf, objs_named[i].numeric, (int)XSTRLEN(buf)), 0);
  39907. ASN1_OBJECT_free(obj);
  39908. obj = NULL;
  39909. }
  39910. #endif
  39911. return EXPECT_RESULT();
  39912. }
  39913. static int test_wolfSSL_PEM_write_bio_X509(void)
  39914. {
  39915. EXPECT_DECLS;
  39916. #if defined(OPENSSL_EXTRA) && defined(OPENSSL_ALL) && \
  39917. defined(WOLFSSL_AKID_NAME) && defined(WOLFSSL_CERT_EXT) && \
  39918. defined(WOLFSSL_CERT_GEN) && !defined(NO_BIO) && !defined(NO_RSA) && \
  39919. !defined(NO_FILESYSTEM)
  39920. /* This test contains the hard coded expected
  39921. * lengths. Update if necessary */
  39922. XFILE fp = XBADFILE;
  39923. WOLFSSL_EVP_PKEY *priv = NULL;
  39924. BIO* input = NULL;
  39925. BIO* output = NULL;
  39926. X509* x509a = NULL;
  39927. X509* x509b = NULL;
  39928. ASN1_TIME* notBeforeA = NULL;
  39929. ASN1_TIME* notAfterA = NULL;
  39930. #ifndef NO_ASN_TIME
  39931. ASN1_TIME* notBeforeB = NULL;
  39932. ASN1_TIME* notAfterB = NULL;
  39933. #endif
  39934. int expectedLen;
  39935. ExpectTrue((fp = XFOPEN("certs/server-key.pem", "rb")) != XBADFILE);
  39936. ExpectNotNull(priv = wolfSSL_PEM_read_PrivateKey(fp, NULL, NULL, NULL));
  39937. if (fp != XBADFILE) {
  39938. XFCLOSE(fp);
  39939. fp = XBADFILE;
  39940. }
  39941. ExpectNotNull(input = BIO_new_file("certs/test/cert-ext-multiple.pem",
  39942. "rb"));
  39943. ExpectIntEQ(wolfSSL_BIO_get_len(input), 2000);
  39944. /* read PEM into X509 struct, get notBefore / notAfter to verify against */
  39945. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  39946. ExpectNotNull(notBeforeA = X509_get_notBefore(x509a));
  39947. ExpectNotNull(notAfterA = X509_get_notAfter(x509a));
  39948. /* write X509 back to PEM BIO; no need to sign as nothing changed. */
  39949. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  39950. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  39951. /* compare length against expected */
  39952. expectedLen = 2000;
  39953. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  39954. #ifndef NO_ASN_TIME
  39955. /* read exported X509 PEM back into struct, sanity check on export,
  39956. * make sure notBefore/notAfter are the same and certs are identical. */
  39957. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  39958. ExpectNotNull(notBeforeB = X509_get_notBefore(x509b));
  39959. ExpectNotNull(notAfterB = X509_get_notAfter(x509b));
  39960. ExpectIntEQ(ASN1_TIME_compare(notBeforeA, notBeforeB), 0);
  39961. ExpectIntEQ(ASN1_TIME_compare(notAfterA, notAfterB), 0);
  39962. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  39963. X509_free(x509b);
  39964. x509b = NULL;
  39965. #endif
  39966. /* Reset output buffer */
  39967. BIO_free(output);
  39968. output = NULL;
  39969. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  39970. /* Test forcing the AKID to be generated just from KeyIdentifier */
  39971. if (EXPECT_SUCCESS() && x509a->authKeyIdSrc != NULL) {
  39972. XMEMMOVE(x509a->authKeyIdSrc, x509a->authKeyId, x509a->authKeyIdSz);
  39973. x509a->authKeyId = x509a->authKeyIdSrc;
  39974. x509a->authKeyIdSrc = NULL;
  39975. x509a->authKeyIdSrcSz = 0;
  39976. }
  39977. /* Resign to re-generate the der */
  39978. ExpectIntGT(wolfSSL_X509_sign(x509a, priv, EVP_sha256()), 0);
  39979. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  39980. /* Check that we generate a smaller output since the AKID will
  39981. * only contain the KeyIdentifier without any additional
  39982. * information */
  39983. /* Here we copy the validity struct from the original */
  39984. expectedLen = 1688;
  39985. ExpectIntEQ(wolfSSL_BIO_get_len(output), expectedLen);
  39986. /* Reset buffers and x509 */
  39987. BIO_free(input);
  39988. input = NULL;
  39989. BIO_free(output);
  39990. output = NULL;
  39991. X509_free(x509a);
  39992. x509a = NULL;
  39993. /* test CA and basicConstSet values are encoded when
  39994. * the cert is a CA */
  39995. ExpectNotNull(input = BIO_new_file("certs/server-cert.pem", "rb"));
  39996. /* read PEM into X509 struct */
  39997. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  39998. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  39999. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  40000. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  40001. /* read exported X509 PEM back into struct, ensure isCa and basicConstSet
  40002. * values are maintained and certs are identical.*/
  40003. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  40004. ExpectIntEQ(x509b->isCa, 1);
  40005. ExpectIntEQ(x509b->basicConstSet, 1);
  40006. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  40007. X509_free(x509a);
  40008. x509a = NULL;
  40009. X509_free(x509b);
  40010. x509b = NULL;
  40011. BIO_free(input);
  40012. input = NULL;
  40013. BIO_free(output);
  40014. output = NULL;
  40015. /* test CA and basicConstSet values are encoded when
  40016. * the cert is not CA */
  40017. ExpectNotNull(input = BIO_new_file("certs/client-uri-cert.pem", "rb"));
  40018. /* read PEM into X509 struct */
  40019. ExpectNotNull(PEM_read_bio_X509(input, &x509a, NULL, NULL));
  40020. /* write X509 back to PEM BIO; no need to sign as nothing changed */
  40021. ExpectNotNull(output = BIO_new(wolfSSL_BIO_s_mem()));
  40022. ExpectIntEQ(PEM_write_bio_X509(output, x509a), WOLFSSL_SUCCESS);
  40023. /* read exported X509 PEM back into struct, ensure isCa and
  40024. * basicConstSet values are maintained and certs are identical */
  40025. ExpectNotNull(PEM_read_bio_X509(output, &x509b, NULL, NULL));
  40026. ExpectIntEQ(x509b->isCa, 0);
  40027. ExpectIntEQ(x509b->basicConstSet, 1);
  40028. ExpectIntEQ(0, wolfSSL_X509_cmp(x509a, x509b));
  40029. wolfSSL_EVP_PKEY_free(priv);
  40030. X509_free(x509a);
  40031. X509_free(x509b);
  40032. BIO_free(input);
  40033. BIO_free(output);
  40034. #endif
  40035. return EXPECT_RESULT();
  40036. }
  40037. static int test_wolfSSL_X509_NAME_ENTRY(void)
  40038. {
  40039. EXPECT_DECLS;
  40040. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  40041. !defined(NO_RSA) && defined(WOLFSSL_CERT_GEN)
  40042. X509* x509 = NULL;
  40043. #ifndef NO_BIO
  40044. BIO* bio = NULL;
  40045. #endif
  40046. X509_NAME* nm = NULL;
  40047. X509_NAME_ENTRY* entry = NULL;
  40048. unsigned char cn[] = "another name to add";
  40049. #ifdef OPENSSL_ALL
  40050. int i;
  40051. int names_len = 0;
  40052. #endif
  40053. ExpectNotNull(x509 =
  40054. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  40055. #ifndef NO_BIO
  40056. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40057. ExpectIntEQ(PEM_write_bio_X509_AUX(bio, x509), SSL_SUCCESS);
  40058. #endif
  40059. #ifdef WOLFSSL_CERT_REQ
  40060. {
  40061. X509_REQ* req = NULL;
  40062. #ifndef NO_BIO
  40063. BIO* bReq = NULL;
  40064. #endif
  40065. ExpectNotNull(req =
  40066. wolfSSL_X509_load_certificate_file(cliCertFile, SSL_FILETYPE_PEM));
  40067. #ifndef NO_BIO
  40068. ExpectNotNull(bReq = BIO_new(BIO_s_mem()));
  40069. ExpectIntEQ(PEM_write_bio_X509_REQ(bReq, req), SSL_SUCCESS);
  40070. BIO_free(bReq);
  40071. #endif
  40072. X509_free(req);
  40073. }
  40074. #endif
  40075. ExpectNotNull(nm = X509_get_subject_name(x509));
  40076. /* Test add entry */
  40077. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_NID(NULL, NID_commonName,
  40078. 0x0c, cn, (int)sizeof(cn)));
  40079. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  40080. #ifdef WOLFSSL_CERT_EXT
  40081. ExpectIntEQ(X509_NAME_add_entry_by_txt(nm, "emailAddress", MBSTRING_UTF8,
  40082. (byte*)"support@wolfssl.com", 19, -1,
  40083. 1), WOLFSSL_SUCCESS);
  40084. #endif
  40085. X509_NAME_ENTRY_free(entry);
  40086. entry = NULL;
  40087. #ifdef WOLFSSL_CERT_REQ
  40088. {
  40089. unsigned char srv_pkcs9p[] = "Server";
  40090. unsigned char fvrtDrnk[] = "tequila";
  40091. unsigned char* der = NULL;
  40092. char* subject = NULL;
  40093. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_pkcs9_contentType,
  40094. MBSTRING_ASC, srv_pkcs9p, -1, -1, 0), SSL_SUCCESS);
  40095. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_favouriteDrink,
  40096. MBSTRING_ASC, fvrtDrnk, -1, -1, 0), SSL_SUCCESS);
  40097. ExpectIntGT(wolfSSL_i2d_X509_NAME(nm, &der), 0);
  40098. ExpectNotNull(der);
  40099. ExpectNotNull(subject = X509_NAME_oneline(nm, 0, 0));
  40100. ExpectNotNull(XSTRSTR(subject, "favouriteDrink=tequila"));
  40101. ExpectNotNull(XSTRSTR(subject, "contentType=Server"));
  40102. #ifdef DEBUG_WOLFSSL
  40103. if (subject != NULL) {
  40104. fprintf(stderr, "\n\t%s\n", subject);
  40105. }
  40106. #endif
  40107. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  40108. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  40109. }
  40110. #endif
  40111. /* Test add entry by text */
  40112. ExpectNotNull(entry = X509_NAME_ENTRY_create_by_txt(NULL, "commonName",
  40113. 0x0c, cn, (int)sizeof(cn)));
  40114. #if defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO) \
  40115. || defined(WOLFSSL_HAPROXY) || defined(WOLFSSL_NGINX)
  40116. ExpectNull(X509_NAME_ENTRY_create_by_txt(&entry, "unknown",
  40117. V_ASN1_UTF8STRING, cn, (int)sizeof(cn)));
  40118. #endif
  40119. ExpectIntEQ(X509_NAME_add_entry(nm, entry, -1, 0), SSL_SUCCESS);
  40120. X509_NAME_ENTRY_free(entry);
  40121. entry = NULL;
  40122. /* Test add entry by NID */
  40123. ExpectIntEQ(X509_NAME_add_entry_by_NID(nm, NID_commonName, MBSTRING_UTF8,
  40124. cn, -1, -1, 0), SSL_SUCCESS);
  40125. #ifdef OPENSSL_ALL
  40126. /* stack of name entry */
  40127. ExpectIntGT((names_len = sk_X509_NAME_ENTRY_num(nm->entries)), 0);
  40128. for (i = 0; i < names_len; i++) {
  40129. ExpectNotNull(entry = sk_X509_NAME_ENTRY_value(nm->entries, i));
  40130. }
  40131. #endif
  40132. #ifndef NO_BIO
  40133. BIO_free(bio);
  40134. #endif
  40135. X509_free(x509); /* free's nm */
  40136. #endif
  40137. return EXPECT_RESULT();
  40138. }
  40139. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  40140. static int test_GENERAL_NAME_set0_othername(void) {
  40141. EXPECT_DECLS;
  40142. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  40143. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  40144. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  40145. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  40146. defined(WOLFSSL_FPKI)
  40147. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  40148. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  40149. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  40150. const char * cert_fname = "./certs/server-cert.der";
  40151. const char * key_fname = "./certs/server-key.der";
  40152. X509* x509 = NULL;
  40153. GENERAL_NAME* gn = NULL;
  40154. GENERAL_NAMES* gns = NULL;
  40155. ASN1_OBJECT* upn_oid = NULL;
  40156. ASN1_UTF8STRING *utf8str = NULL;
  40157. ASN1_TYPE *value = NULL;
  40158. X509_EXTENSION * ext = NULL;
  40159. byte* pt = NULL;
  40160. byte der[4096];
  40161. int derSz = 0;
  40162. EVP_PKEY* priv = NULL;
  40163. XFILE f = XBADFILE;
  40164. ExpectTrue((f = XFOPEN(cert_fname, "rb")) != XBADFILE);
  40165. ExpectNotNull(x509 = d2i_X509_fp(f, NULL));
  40166. if (f != XBADFILE) {
  40167. XFCLOSE(f);
  40168. f = XBADFILE;
  40169. }
  40170. ExpectNotNull(gn = GENERAL_NAME_new());
  40171. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  40172. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  40173. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  40174. ExpectNotNull(value = ASN1_TYPE_new());
  40175. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  40176. if ((value == NULL) || (value->value.ptr != (char*)utf8str)) {
  40177. wolfSSL_ASN1_STRING_free(utf8str);
  40178. }
  40179. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  40180. if (EXPECT_FAIL()) {
  40181. ASN1_TYPE_free(value);
  40182. }
  40183. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  40184. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  40185. if (EXPECT_FAIL()) {
  40186. GENERAL_NAME_free(gn);
  40187. gn = NULL;
  40188. }
  40189. ExpectNotNull(ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  40190. ExpectIntEQ(X509_add_ext(x509, ext, -1), 1);
  40191. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  40192. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  40193. if (f != XBADFILE) {
  40194. XFCLOSE(f);
  40195. f = XBADFILE;
  40196. }
  40197. pt = der;
  40198. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  40199. (const unsigned char**)&pt, derSz));
  40200. ExpectIntGT(X509_sign(x509, priv, EVP_sha256()), 0);
  40201. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  40202. gns = NULL;
  40203. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  40204. NULL));
  40205. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 3);
  40206. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 2));
  40207. ExpectIntEQ(gn->type, 0);
  40208. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  40209. ASN1_OBJECT_free(upn_oid);
  40210. X509_EXTENSION_free(ext);
  40211. X509_free(x509);
  40212. EVP_PKEY_free(priv);
  40213. #endif
  40214. return EXPECT_RESULT();
  40215. }
  40216. /* Note the lack of wolfSSL_ prefix...this is a compatibility layer test. */
  40217. static int test_othername_and_SID_ext(void) {
  40218. EXPECT_DECLS;
  40219. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  40220. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  40221. defined(WOLFSSL_CUSTOM_OID) && defined(WOLFSSL_ALT_NAMES) && \
  40222. defined(WOLFSSL_CERT_EXT) && !defined(NO_FILESYSTEM) && \
  40223. defined(WOLFSSL_FPKI) && defined(WOLFSSL_ASN_TEMPLATE)
  40224. /* ./configure --enable-opensslall --enable-certgen --enable-certreq
  40225. * --enable-certext --enable-debug 'CPPFLAGS=-DWOLFSSL_CUSTOM_OID
  40226. * -DWOLFSSL_ALT_NAMES -DWOLFSSL_FPKI' */
  40227. const char* csr_fname = "./certs/csr.signed.der";
  40228. const char* key_fname = "./certs/server-key.der";
  40229. byte der[4096];
  40230. int derSz = 0;
  40231. X509_REQ* x509 = NULL;
  40232. STACK_OF(X509_EXTENSION) *exts = NULL;
  40233. X509_EXTENSION * san_ext = NULL;
  40234. X509_EXTENSION * ext = NULL;
  40235. GENERAL_NAME* gn = NULL;
  40236. GENERAL_NAMES* gns = NULL;
  40237. ASN1_OBJECT* upn_oid = NULL;
  40238. ASN1_UTF8STRING *utf8str = NULL;
  40239. ASN1_TYPE *value = NULL;
  40240. ASN1_STRING *extval = NULL;
  40241. /* SID extension. SID data format explained here:
  40242. * https://blog.qdsecurity.se/2022/05/27/manually-injecting-a-sid-in-a-certificate/
  40243. */
  40244. uint8_t SidExtension[] = {
  40245. 48, 64, 160, 62, 6, 10, 43, 6, 1, 4, 1, 130, 55, 25, 2, 1, 160,
  40246. 48, 4, 46, 83, 45, 49, 45, 53, 45, 50, 49, 45, 50, 56, 52, 51, 57,
  40247. 48, 55, 52, 49, 56, 45, 51, 57, 50, 54, 50, 55, 55, 52, 50, 49, 45,
  40248. 51, 56, 49, 53, 57, 57, 51, 57, 55, 50, 45, 52, 54, 48, 49};
  40249. uint8_t expectedAltName[] = {
  40250. 0x30, 0x27, 0xA0, 0x25, 0x06, 0x0A, 0x2B, 0x06, 0x01, 0x04, 0x01, 0x82,
  40251. 0x37, 0x14, 0x02, 0x03, 0xA0, 0x17, 0x0C, 0x15, 0x6F, 0x74, 0x68, 0x65,
  40252. 0x72, 0x6E, 0x61, 0x6D, 0x65, 0x40, 0x77, 0x6F, 0x6C, 0x66, 0x73, 0x73,
  40253. 0x6C, 0x2E, 0x63, 0x6F, 0x6D};
  40254. X509_EXTENSION *sid_ext = NULL;
  40255. ASN1_OBJECT* sid_oid = NULL;
  40256. ASN1_OCTET_STRING *sid_data = NULL;
  40257. EVP_PKEY* priv = NULL;
  40258. XFILE f = XBADFILE;
  40259. byte* pt = NULL;
  40260. BIO* bio = NULL;
  40261. ExpectTrue((f = XFOPEN(csr_fname, "rb")) != XBADFILE);
  40262. ExpectNotNull(x509 = d2i_X509_REQ_fp(f, NULL));
  40263. if (f != XBADFILE) {
  40264. XFCLOSE(f);
  40265. f = XBADFILE;
  40266. }
  40267. ExpectIntEQ(X509_REQ_set_version(x509, 2), 1);
  40268. ExpectNotNull(gn = GENERAL_NAME_new());
  40269. ExpectNotNull(upn_oid = OBJ_txt2obj("1.3.6.1.4.1.311.20.2.3", 1));
  40270. ExpectNotNull(utf8str = ASN1_UTF8STRING_new());
  40271. ExpectIntEQ(ASN1_STRING_set(utf8str, "othername@wolfssl.com", -1), 1);
  40272. ExpectNotNull(value = ASN1_TYPE_new());
  40273. ASN1_TYPE_set(value, V_ASN1_UTF8STRING, utf8str);
  40274. if (EXPECT_FAIL()) {
  40275. ASN1_UTF8STRING_free(utf8str);
  40276. }
  40277. ExpectIntEQ(GENERAL_NAME_set0_othername(gn, upn_oid, value), 1);
  40278. if (EXPECT_FAIL()) {
  40279. ASN1_TYPE_free(value);
  40280. GENERAL_NAME_free(gn);
  40281. gn = NULL;
  40282. }
  40283. ExpectNotNull(gns = sk_GENERAL_NAME_new(NULL));
  40284. ExpectIntEQ(sk_GENERAL_NAME_push(gns, gn), 1);
  40285. if (EXPECT_FAIL()) {
  40286. GENERAL_NAME_free(gn);
  40287. }
  40288. ExpectNotNull(san_ext = X509V3_EXT_i2d(NID_subject_alt_name, 0, gns));
  40289. ExpectNotNull(sid_oid = OBJ_txt2obj("1.3.6.1.4.1.311.25.2", 1));
  40290. ExpectNotNull(sid_data = ASN1_OCTET_STRING_new());
  40291. ASN1_OCTET_STRING_set(sid_data, SidExtension, sizeof(SidExtension));
  40292. ExpectNotNull(sid_ext = X509_EXTENSION_create_by_OBJ(NULL, sid_oid, 0,
  40293. sid_data));
  40294. ExpectNotNull(exts = sk_X509_EXTENSION_new_null());
  40295. /* Ensure an empty stack doesn't raise an error. */
  40296. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  40297. ExpectIntEQ(sk_X509_EXTENSION_push(exts, san_ext), 1);
  40298. if (EXPECT_FAIL()) {
  40299. X509_EXTENSION_free(san_ext);
  40300. }
  40301. ExpectIntEQ(sk_X509_EXTENSION_push(exts, sid_ext), 2);
  40302. if (EXPECT_FAIL()) {
  40303. X509_EXTENSION_free(sid_ext);
  40304. }
  40305. ExpectIntEQ(X509_REQ_add_extensions(x509, exts), 1);
  40306. ExpectTrue((f = XFOPEN(key_fname, "rb")) != XBADFILE);
  40307. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), f), 0);
  40308. if (f != XBADFILE)
  40309. XFCLOSE(f);
  40310. pt = der;
  40311. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  40312. (const unsigned char**)&pt, derSz));
  40313. ExpectIntGT(X509_REQ_sign(x509, priv, EVP_sha256()), 0);
  40314. pt = der;
  40315. ExpectIntGT(derSz = i2d_X509_REQ(x509, &pt), 0);
  40316. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  40317. gns = NULL;
  40318. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  40319. exts = NULL;
  40320. ASN1_OBJECT_free(upn_oid);
  40321. ASN1_OBJECT_free(sid_oid);
  40322. ASN1_OCTET_STRING_free(sid_data);
  40323. X509_REQ_free(x509);
  40324. x509 = NULL;
  40325. EVP_PKEY_free(priv);
  40326. /* At this point everything used to generate what is in der is cleaned up.
  40327. * We now read back from der to confirm the extensions were inserted
  40328. * correctly. */
  40329. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem());
  40330. ExpectNotNull(bio);
  40331. ExpectIntEQ(BIO_write(bio, der, derSz), derSz); /* d2i consumes BIO */
  40332. ExpectNotNull(d2i_X509_REQ_bio(bio, &x509));
  40333. ExpectNotNull(x509);
  40334. BIO_free(bio);
  40335. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  40336. x509));
  40337. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  40338. /* Check the SID extension. */
  40339. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 0));
  40340. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  40341. ExpectIntEQ(extval->length, sizeof(SidExtension));
  40342. ExpectIntEQ(XMEMCMP(SidExtension, extval->data, sizeof(SidExtension)), 0);
  40343. /* Check the AltNames extension. */
  40344. ExpectNotNull(ext = sk_X509_EXTENSION_value(exts, 1));
  40345. ExpectNotNull(extval = X509_EXTENSION_get_data(ext));
  40346. ExpectIntEQ(extval->length, sizeof(expectedAltName));
  40347. ExpectIntEQ(XMEMCMP(expectedAltName, extval->data, sizeof(expectedAltName)),
  40348. 0);
  40349. /* Cleanup */
  40350. ExpectNotNull(gns = X509_get_ext_d2i(x509, NID_subject_alt_name, NULL,
  40351. NULL));
  40352. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  40353. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, 0));
  40354. ExpectIntEQ(gn->type, 0);
  40355. sk_GENERAL_NAME_pop_free(gns, GENERAL_NAME_free);
  40356. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  40357. X509_REQ_free(x509);
  40358. #endif
  40359. return EXPECT_RESULT();
  40360. }
  40361. static int test_wolfSSL_X509_set_name(void)
  40362. {
  40363. EXPECT_DECLS;
  40364. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  40365. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  40366. X509* x509 = NULL;
  40367. X509_NAME* name = NULL;
  40368. ExpectNotNull(name = X509_NAME_new());
  40369. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  40370. (byte*)"wolfssl.com", 11, 0, 1),
  40371. WOLFSSL_SUCCESS);
  40372. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  40373. (byte*)"support@wolfssl.com", 19, -1,
  40374. 1), WOLFSSL_SUCCESS);
  40375. ExpectNotNull(x509 = X509_new());
  40376. ExpectIntEQ(X509_set_subject_name(NULL, NULL), WOLFSSL_FAILURE);
  40377. ExpectIntEQ(X509_set_subject_name(x509, NULL), WOLFSSL_FAILURE);
  40378. ExpectIntEQ(X509_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  40379. ExpectIntEQ(X509_set_subject_name(x509, name), WOLFSSL_SUCCESS);
  40380. ExpectIntEQ(X509_set_issuer_name(NULL, NULL), WOLFSSL_FAILURE);
  40381. ExpectIntEQ(X509_set_issuer_name(x509, NULL), WOLFSSL_FAILURE);
  40382. ExpectIntEQ(X509_set_issuer_name(NULL, name), WOLFSSL_FAILURE);
  40383. ExpectIntEQ(X509_set_issuer_name(x509, name), WOLFSSL_SUCCESS);
  40384. X509_free(x509);
  40385. X509_NAME_free(name);
  40386. #endif /* OPENSSL_ALL && !NO_CERTS */
  40387. return EXPECT_RESULT();
  40388. }
  40389. static int test_wolfSSL_X509_set_notAfter(void)
  40390. {
  40391. EXPECT_DECLS;
  40392. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  40393. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  40394. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  40395. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) &&\
  40396. !defined(TIME_T_NOT_64BIT) && !defined(NO_64BIT) && !defined(NO_BIO)
  40397. /* Generalized time will overflow time_t if not long */
  40398. X509* x = NULL;
  40399. BIO* bio = NULL;
  40400. ASN1_TIME *asn_time = NULL;
  40401. ASN1_TIME *time_check = NULL;
  40402. const int year = 365*24*60*60;
  40403. const int day = 24*60*60;
  40404. const int hour = 60*60;
  40405. const int mini = 60;
  40406. int offset_day;
  40407. unsigned char buf[25];
  40408. time_t t;
  40409. /*
  40410. * Setup asn_time. APACHE HTTPD uses time(NULL)
  40411. */
  40412. t = (time_t)107 * year + 31 * day + 34 * hour + 30 * mini + 7 * day;
  40413. offset_day = 7;
  40414. /*
  40415. * Free these.
  40416. */
  40417. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  40418. ExpectNotNull(asn_time);
  40419. ExpectNotNull(x = X509_new());
  40420. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40421. /*
  40422. * Tests
  40423. */
  40424. ExpectTrue(wolfSSL_X509_set_notAfter(x, asn_time));
  40425. /* time_check is simply (ANS1_TIME*)x->notAfter */
  40426. ExpectNotNull(time_check = X509_get_notAfter(x));
  40427. /* ANS1_TIME_check validates by checking if argument can be parsed */
  40428. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  40429. /* Convert to human readable format and compare to intended date */
  40430. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  40431. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  40432. ExpectIntEQ(XMEMCMP(buf, "Jan 20 10:30:00 2077 GMT", sizeof(buf) - 1), 0);
  40433. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, NULL));
  40434. ExpectFalse(wolfSSL_X509_set_notAfter(x, NULL));
  40435. ExpectFalse(wolfSSL_X509_set_notAfter(NULL, asn_time));
  40436. /*
  40437. * Cleanup
  40438. */
  40439. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  40440. X509_free(x);
  40441. BIO_free(bio);
  40442. #endif
  40443. return EXPECT_RESULT();
  40444. }
  40445. static int test_wolfSSL_X509_set_notBefore(void)
  40446. {
  40447. EXPECT_DECLS;
  40448. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) \
  40449. && !defined(NO_ASN_TIME) && !defined(USER_TIME) && \
  40450. !defined(TIME_OVERRIDES) && !defined(NO_CERTS) && \
  40451. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  40452. X509* x = NULL;
  40453. BIO* bio = NULL;
  40454. ASN1_TIME *asn_time = NULL;
  40455. ASN1_TIME *time_check = NULL;
  40456. const int year = 365*24*60*60;
  40457. const int day = 24*60*60;
  40458. const int hour = 60*60;
  40459. const int mini = 60;
  40460. int offset_day;
  40461. unsigned char buf[25];
  40462. time_t t;
  40463. /*
  40464. * Setup asn_time. APACHE HTTPD uses time(NULL)
  40465. */
  40466. t = (time_t)49 * year + 125 * day + 20 * hour + 30 * mini + 7 * day;
  40467. offset_day = 7;
  40468. /*
  40469. * Free these.
  40470. */
  40471. asn_time = wolfSSL_ASN1_TIME_adj(NULL, t, offset_day, 0);
  40472. ExpectNotNull(asn_time);
  40473. ExpectNotNull(x = X509_new());
  40474. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40475. ExpectIntEQ(ASN1_TIME_check(asn_time), WOLFSSL_SUCCESS);
  40476. /*
  40477. * Main Tests
  40478. */
  40479. ExpectTrue(wolfSSL_X509_set_notBefore(x, asn_time));
  40480. /* time_check == (ANS1_TIME*)x->notBefore */
  40481. ExpectNotNull(time_check = X509_get_notBefore(x));
  40482. /* ANS1_TIME_check validates by checking if argument can be parsed */
  40483. ExpectIntEQ(ASN1_TIME_check(time_check), WOLFSSL_SUCCESS);
  40484. /* Convert to human readable format and compare to intended date */
  40485. ExpectIntEQ(ASN1_TIME_print(bio, time_check), 1);
  40486. ExpectIntEQ(BIO_read(bio, buf, sizeof(buf)), 24);
  40487. ExpectIntEQ(XMEMCMP(buf, "May 8 20:30:00 2019 GMT", sizeof(buf) - 1), 0);
  40488. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, NULL));
  40489. ExpectFalse(wolfSSL_X509_set_notBefore(x, NULL));
  40490. ExpectFalse(wolfSSL_X509_set_notBefore(NULL, asn_time));
  40491. /*
  40492. * Cleanup
  40493. */
  40494. XFREE(asn_time, NULL, DYNAMIC_TYPE_OPENSSL);
  40495. X509_free(x);
  40496. BIO_free(bio);
  40497. #endif
  40498. return EXPECT_RESULT();
  40499. }
  40500. static int test_wolfSSL_X509_set_version(void)
  40501. {
  40502. EXPECT_DECLS;
  40503. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_APACHE_HTTPD)) && \
  40504. !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ)
  40505. X509* x509 = NULL;
  40506. long v = 2L;
  40507. long maxInt = INT_MAX;
  40508. ExpectNotNull(x509 = X509_new());
  40509. /* These should pass. */
  40510. ExpectTrue(wolfSSL_X509_set_version(x509, v));
  40511. ExpectIntEQ(v, wolfSSL_X509_get_version(x509));
  40512. /* Fail Case: When v(long) is greater than x509->version(int). */
  40513. v = maxInt+1;
  40514. ExpectFalse(wolfSSL_X509_set_version(x509, v));
  40515. ExpectFalse(wolfSSL_X509_set_version(NULL, 2L));
  40516. ExpectFalse(wolfSSL_X509_set_version(NULL, maxInt+1));
  40517. /* Cleanup */
  40518. X509_free(x509);
  40519. #endif
  40520. return EXPECT_RESULT();
  40521. }
  40522. #ifndef NO_BIO
  40523. static int test_wolfSSL_BIO_gets(void)
  40524. {
  40525. EXPECT_DECLS;
  40526. #if defined(OPENSSL_EXTRA)
  40527. BIO* bio = NULL;
  40528. BIO* bio2 = NULL;
  40529. char msg[] = "\nhello wolfSSL\n security plus\t---...**adf\na...b.c";
  40530. char emp[] = "";
  40531. char bio_buffer[20];
  40532. int bufferSz = 20;
  40533. #ifdef OPENSSL_ALL
  40534. BUF_MEM* emp_bm = NULL;
  40535. BUF_MEM* msg_bm = NULL;
  40536. #endif
  40537. /* try with bad args */
  40538. ExpectNull(bio = BIO_new_mem_buf(NULL, sizeof(msg)));
  40539. #ifdef OPENSSL_ALL
  40540. ExpectIntEQ(BIO_set_mem_buf(bio, NULL, BIO_NOCLOSE), BAD_FUNC_ARG);
  40541. #endif
  40542. /* try with real msg */
  40543. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  40544. XMEMSET(bio_buffer, 0, bufferSz);
  40545. ExpectNotNull(BIO_push(bio, BIO_new(BIO_s_bio())));
  40546. ExpectNull(bio2 = BIO_find_type(bio, BIO_TYPE_FILE));
  40547. ExpectNotNull(bio2 = BIO_find_type(bio, BIO_TYPE_BIO));
  40548. ExpectFalse(bio2 != BIO_next(bio));
  40549. /* make buffer filled with no terminating characters */
  40550. XMEMSET(bio_buffer, 1, bufferSz);
  40551. /* BIO_gets reads a line of data */
  40552. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  40553. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  40554. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  40555. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  40556. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  40557. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  40558. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  40559. #ifdef OPENSSL_ALL
  40560. /* test setting the mem_buf manually */
  40561. BIO_free(bio);
  40562. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, -1));
  40563. ExpectNotNull(emp_bm = BUF_MEM_new());
  40564. ExpectNotNull(msg_bm = BUF_MEM_new());
  40565. ExpectIntEQ(BUF_MEM_grow(msg_bm, sizeof(msg)), sizeof(msg));
  40566. if (EXPECT_SUCCESS()) {
  40567. XFREE(msg_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  40568. msg_bm->data = NULL;
  40569. }
  40570. /* emp size is 1 for terminator */
  40571. ExpectIntEQ(BUF_MEM_grow(emp_bm, sizeof(emp)), sizeof(emp));
  40572. if (EXPECT_SUCCESS()) {
  40573. XFREE(emp_bm->data, NULL, DYNAMIC_TYPE_OPENSSL);
  40574. emp_bm->data = emp;
  40575. msg_bm->data = msg;
  40576. }
  40577. ExpectIntEQ(BIO_set_mem_buf(bio, emp_bm, BIO_CLOSE), WOLFSSL_SUCCESS);
  40578. /* check reading an empty string */
  40579. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  40580. ExpectStrEQ(emp, bio_buffer);
  40581. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  40582. /* BIO_gets reads a line of data */
  40583. ExpectIntEQ(BIO_set_mem_buf(bio, msg_bm, BIO_NOCLOSE), WOLFSSL_SUCCESS);
  40584. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  40585. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  40586. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  40587. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  40588. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  40589. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  40590. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  40591. if (EXPECT_SUCCESS())
  40592. emp_bm->data = NULL;
  40593. BUF_MEM_free(emp_bm);
  40594. if (EXPECT_SUCCESS())
  40595. msg_bm->data = NULL;
  40596. BUF_MEM_free(msg_bm);
  40597. #endif
  40598. /* check not null terminated string */
  40599. BIO_free(bio);
  40600. bio = NULL;
  40601. msg[0] = 0x33;
  40602. msg[1] = 0x33;
  40603. msg[2] = 0x33;
  40604. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  40605. ExpectIntEQ(BIO_gets(bio, bio_buffer, 3), 2);
  40606. ExpectIntEQ(bio_buffer[0], msg[0]);
  40607. ExpectIntEQ(bio_buffer[1], msg[1]);
  40608. ExpectIntNE(bio_buffer[2], msg[2]);
  40609. BIO_free(bio);
  40610. bio = NULL;
  40611. msg[3] = 0x33;
  40612. bio_buffer[3] = 0x33;
  40613. ExpectNotNull(bio = BIO_new_mem_buf((void*)msg, 3));
  40614. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 3);
  40615. ExpectIntEQ(bio_buffer[0], msg[0]);
  40616. ExpectIntEQ(bio_buffer[1], msg[1]);
  40617. ExpectIntEQ(bio_buffer[2], msg[2]);
  40618. ExpectIntNE(bio_buffer[3], 0x33); /* make sure null terminator was set */
  40619. /* check reading an empty string */
  40620. BIO_free(bio);
  40621. bio = NULL;
  40622. ExpectNotNull(bio = BIO_new_mem_buf((void*)emp, sizeof(emp)));
  40623. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1); /* just terminator */
  40624. ExpectStrEQ(emp, bio_buffer);
  40625. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  40626. /* check error cases */
  40627. BIO_free(bio);
  40628. bio = NULL;
  40629. ExpectIntEQ(BIO_gets(NULL, NULL, 0), SSL_FAILURE);
  40630. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40631. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  40632. #if !defined(NO_FILESYSTEM)
  40633. {
  40634. BIO* f_bio = NULL;
  40635. XFILE f = XBADFILE;
  40636. ExpectNotNull(f_bio = BIO_new(BIO_s_file()));
  40637. ExpectIntLE(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  40638. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  40639. ExpectIntEQ((int)BIO_set_fp(f_bio, f, BIO_CLOSE), SSL_SUCCESS);
  40640. if (EXPECT_FAIL() && (f != XBADFILE)) {
  40641. XFCLOSE(f);
  40642. }
  40643. ExpectIntGT(BIO_gets(f_bio, bio_buffer, bufferSz), 0);
  40644. BIO_free(f_bio);
  40645. f_bio = NULL;
  40646. }
  40647. #endif /* NO_FILESYSTEM */
  40648. BIO_free(bio);
  40649. bio = NULL;
  40650. BIO_free(bio2);
  40651. bio2 = NULL;
  40652. /* try with type BIO */
  40653. XMEMCPY(msg, "\nhello wolfSSL\n security plus\t---...**adf\na...b.c",
  40654. sizeof(msg));
  40655. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  40656. ExpectIntEQ(BIO_gets(bio, bio_buffer, 2), 0); /* nothing to read */
  40657. ExpectNotNull(bio2 = BIO_new(BIO_s_bio()));
  40658. ExpectIntEQ(BIO_set_write_buf_size(bio, 10), SSL_SUCCESS);
  40659. ExpectIntEQ(BIO_set_write_buf_size(bio2, sizeof(msg)), SSL_SUCCESS);
  40660. ExpectIntEQ(BIO_make_bio_pair(bio, bio2), SSL_SUCCESS);
  40661. ExpectIntEQ(BIO_write(bio2, msg, sizeof(msg)), sizeof(msg));
  40662. ExpectIntEQ(BIO_gets(bio, bio_buffer, -3), 0);
  40663. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 1);
  40664. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 14);
  40665. ExpectStrEQ(bio_buffer, "hello wolfSSL\n");
  40666. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 19);
  40667. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 8);
  40668. ExpectIntEQ(BIO_gets(bio, bio_buffer, -1), 0);
  40669. BIO_free(bio);
  40670. bio = NULL;
  40671. BIO_free(bio2);
  40672. bio2 = NULL;
  40673. /* check reading an empty string */
  40674. ExpectNotNull(bio = BIO_new(BIO_s_bio()));
  40675. ExpectIntEQ(BIO_set_write_buf_size(bio, sizeof(emp)), SSL_SUCCESS);
  40676. ExpectIntEQ(BIO_gets(bio, bio_buffer, bufferSz), 0); /* Nothing to read */
  40677. ExpectStrEQ(emp, bio_buffer);
  40678. BIO_free(bio);
  40679. bio = NULL;
  40680. #endif
  40681. return EXPECT_RESULT();
  40682. }
  40683. static int test_wolfSSL_BIO_puts(void)
  40684. {
  40685. EXPECT_DECLS;
  40686. #if defined(OPENSSL_EXTRA)
  40687. BIO* bio = NULL;
  40688. char input[] = "hello\0world\n.....ok\n\0";
  40689. char output[128];
  40690. XMEMSET(output, 0, sizeof(output));
  40691. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40692. ExpectIntEQ(BIO_puts(bio, input), 5);
  40693. ExpectIntEQ(BIO_pending(bio), 5);
  40694. ExpectIntEQ(BIO_puts(bio, input + 6), 14);
  40695. ExpectIntEQ(BIO_pending(bio), 19);
  40696. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 11);
  40697. ExpectStrEQ(output, "helloworld\n");
  40698. ExpectIntEQ(BIO_pending(bio), 8);
  40699. ExpectIntEQ(BIO_gets(bio, output, sizeof(output)), 8);
  40700. ExpectStrEQ(output, ".....ok\n");
  40701. ExpectIntEQ(BIO_pending(bio), 0);
  40702. ExpectIntEQ(BIO_puts(bio, ""), -1);
  40703. BIO_free(bio);
  40704. #endif
  40705. return EXPECT_RESULT();
  40706. }
  40707. static int test_wolfSSL_BIO_dump(void)
  40708. {
  40709. EXPECT_DECLS;
  40710. #if defined(OPENSSL_EXTRA)
  40711. BIO* bio;
  40712. static const unsigned char data[] = {
  40713. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07, 0x2A, 0x86, 0x48, 0xCE,
  40714. 0x3D, 0x02, 0x01, 0x06, 0x08, 0x2A, 0x86, 0x48, 0xCE, 0x3D,
  40715. 0x03, 0x01, 0x07, 0x03, 0x42, 0x00, 0x04, 0x55, 0xBF, 0xF4,
  40716. 0x0F, 0x44, 0x50, 0x9A, 0x3D, 0xCE, 0x9B, 0xB7, 0xF0, 0xC5,
  40717. 0x4D, 0xF5, 0x70, 0x7B, 0xD4, 0xEC, 0x24, 0x8E, 0x19, 0x80,
  40718. 0xEC, 0x5A, 0x4C, 0xA2, 0x24, 0x03, 0x62, 0x2C, 0x9B, 0xDA,
  40719. 0xEF, 0xA2, 0x35, 0x12, 0x43, 0x84, 0x76, 0x16, 0xC6, 0x56,
  40720. 0x95, 0x06, 0xCC, 0x01, 0xA9, 0xBD, 0xF6, 0x75, 0x1A, 0x42,
  40721. 0xF7, 0xBD, 0xA9, 0xB2, 0x36, 0x22, 0x5F, 0xC7, 0x5D, 0x7F,
  40722. 0xB4
  40723. };
  40724. /* Generated with OpenSSL. */
  40725. static const char expected[] =
  40726. "0000 - 30 59 30 13 06 07 2a 86-48 ce 3d 02 01 06 08 2a 0Y0...*.H.=....*\n"
  40727. "0010 - 86 48 ce 3d 03 01 07 03-42 00 04 55 bf f4 0f 44 .H.=....B..U...D\n"
  40728. "0020 - 50 9a 3d ce 9b b7 f0 c5-4d f5 70 7b d4 ec 24 8e P.=.....M.p{..$.\n"
  40729. "0030 - 19 80 ec 5a 4c a2 24 03-62 2c 9b da ef a2 35 12 ...ZL.$.b,....5.\n"
  40730. "0040 - 43 84 76 16 c6 56 95 06-cc 01 a9 bd f6 75 1a 42 C.v..V.......u.B\n"
  40731. "0050 - f7 bd a9 b2 36 22 5f c7-5d 7f b4 ....6\"_.]..\n";
  40732. static const char expectedAll[] =
  40733. "0000 - 00 01 02 03 04 05 06 07-08 09 0a 0b 0c 0d 0e 0f ................\n"
  40734. "0010 - 10 11 12 13 14 15 16 17-18 19 1a 1b 1c 1d 1e 1f ................\n"
  40735. "0020 - 20 21 22 23 24 25 26 27-28 29 2a 2b 2c 2d 2e 2f !\"#$%&'()*+,-./\n"
  40736. "0030 - 30 31 32 33 34 35 36 37-38 39 3a 3b 3c 3d 3e 3f 0123456789:;<=>?\n"
  40737. "0040 - 40 41 42 43 44 45 46 47-48 49 4a 4b 4c 4d 4e 4f @ABCDEFGHIJKLMNO\n"
  40738. "0050 - 50 51 52 53 54 55 56 57-58 59 5a 5b 5c 5d 5e 5f PQRSTUVWXYZ[\\]^_\n"
  40739. "0060 - 60 61 62 63 64 65 66 67-68 69 6a 6b 6c 6d 6e 6f `abcdefghijklmno\n"
  40740. "0070 - 70 71 72 73 74 75 76 77-78 79 7a 7b 7c 7d 7e 7f pqrstuvwxyz{|}~.\n"
  40741. "0080 - 80 81 82 83 84 85 86 87-88 89 8a 8b 8c 8d 8e 8f ................\n"
  40742. "0090 - 90 91 92 93 94 95 96 97-98 99 9a 9b 9c 9d 9e 9f ................\n"
  40743. "00a0 - a0 a1 a2 a3 a4 a5 a6 a7-a8 a9 aa ab ac ad ae af ................\n"
  40744. "00b0 - b0 b1 b2 b3 b4 b5 b6 b7-b8 b9 ba bb bc bd be bf ................\n"
  40745. "00c0 - c0 c1 c2 c3 c4 c5 c6 c7-c8 c9 ca cb cc cd ce cf ................\n"
  40746. "00d0 - d0 d1 d2 d3 d4 d5 d6 d7-d8 d9 da db dc dd de df ................\n"
  40747. "00e0 - e0 e1 e2 e3 e4 e5 e6 e7-e8 e9 ea eb ec ed ee ef ................\n"
  40748. "00f0 - f0 f1 f2 f3 f4 f5 f6 f7-f8 f9 fa fb fc fd fe ff ................\n";
  40749. char output[16 * 80];
  40750. int i;
  40751. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  40752. /* Example key dumped. */
  40753. ExpectIntEQ(BIO_dump(bio, (const char*)data, (int)sizeof(data)),
  40754. sizeof(expected) - 1);
  40755. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expected) - 1);
  40756. ExpectIntEQ(XMEMCMP(output, expected, sizeof(expected) - 1), 0);
  40757. /* Try every possible value for a character. */
  40758. for (i = 0; i < 256; i++)
  40759. output[i] = i;
  40760. ExpectIntEQ(BIO_dump(bio, output, 256), sizeof(expectedAll) - 1);
  40761. ExpectIntEQ(BIO_read(bio, output, sizeof(output)), sizeof(expectedAll) - 1);
  40762. ExpectIntEQ(XMEMCMP(output, expectedAll, sizeof(expectedAll) - 1), 0);
  40763. BIO_free(bio);
  40764. #endif
  40765. return EXPECT_RESULT();
  40766. }
  40767. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40768. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  40769. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  40770. static int forceWantRead(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  40771. {
  40772. (void)ssl;
  40773. (void)buf;
  40774. (void)sz;
  40775. (void)ctx;
  40776. return WOLFSSL_CBIO_ERR_WANT_READ;
  40777. }
  40778. #endif
  40779. static int test_wolfSSL_BIO_should_retry(void)
  40780. {
  40781. EXPECT_DECLS;
  40782. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  40783. !defined(NO_RSA) && defined(HAVE_EXT_CACHE) && \
  40784. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(USE_WOLFSSL_IO)
  40785. tcp_ready ready;
  40786. func_args server_args;
  40787. THREAD_TYPE serverThread;
  40788. SOCKET_T sockfd = 0;
  40789. WOLFSSL_CTX* ctx = NULL;
  40790. WOLFSSL* ssl = NULL;
  40791. char msg[64] = "hello wolfssl!";
  40792. char reply[1024];
  40793. int msgSz = (int)XSTRLEN(msg);
  40794. int ret;
  40795. BIO* bio = NULL;
  40796. XMEMSET(&server_args, 0, sizeof(func_args));
  40797. #ifdef WOLFSSL_TIRTOS
  40798. fdOpenSession(Task_self());
  40799. #endif
  40800. StartTCP();
  40801. InitTcpReady(&ready);
  40802. #if defined(USE_WINDOWS_API)
  40803. /* use RNG to get random port if using windows */
  40804. ready.port = GetRandomPort();
  40805. #endif
  40806. server_args.signal = &ready;
  40807. start_thread(test_server_nofail, &server_args, &serverThread);
  40808. wait_tcp_ready(&server_args);
  40809. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40810. #ifdef OPENSSL_COMPATIBLE_DEFAULTS
  40811. ExpectIntEQ(wolfSSL_CTX_clear_mode(ctx, SSL_MODE_AUTO_RETRY), 0);
  40812. #endif
  40813. ExpectIntEQ(WOLFSSL_SUCCESS,
  40814. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  40815. ExpectIntEQ(WOLFSSL_SUCCESS,
  40816. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  40817. ExpectIntEQ(WOLFSSL_SUCCESS,
  40818. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  40819. tcp_connect(&sockfd, wolfSSLIP, server_args.signal->port, 0, 0, NULL);
  40820. /* force retry */
  40821. ExpectNotNull(bio = wolfSSL_BIO_new_ssl(ctx, 1));
  40822. ExpectIntEQ(BIO_get_ssl(bio, &ssl), 1);
  40823. ExpectNotNull(ssl);
  40824. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  40825. wolfSSL_SSLSetIORecv(ssl, forceWantRead);
  40826. if (EXPECT_FAIL()) {
  40827. wolfSSL_free(ssl);
  40828. ssl = NULL;
  40829. }
  40830. ExpectIntLE(BIO_write(bio, msg, msgSz), 0);
  40831. ExpectIntNE(BIO_should_retry(bio), 0);
  40832. ExpectIntEQ(BIO_should_read(bio), 0);
  40833. ExpectIntEQ(BIO_should_write(bio), 0);
  40834. /* now perform successful connection */
  40835. wolfSSL_SSLSetIORecv(ssl, EmbedReceive);
  40836. ExpectIntEQ(BIO_write(bio, msg, msgSz), msgSz);
  40837. ExpectIntNE(BIO_read(bio, reply, sizeof(reply)), 0);
  40838. ret = wolfSSL_get_error(ssl, -1);
  40839. if (ret == WOLFSSL_ERROR_WANT_READ || ret == WOLFSSL_ERROR_WANT_WRITE) {
  40840. ExpectIntNE(BIO_should_retry(bio), 0);
  40841. if (ret == WOLFSSL_ERROR_WANT_READ)
  40842. ExpectIntEQ(BIO_should_read(bio), 1);
  40843. else
  40844. ExpectIntEQ(BIO_should_read(bio), 0);
  40845. if (ret == WOLFSSL_ERROR_WANT_WRITE)
  40846. ExpectIntEQ(BIO_should_write(bio), 1);
  40847. else
  40848. ExpectIntEQ(BIO_should_write(bio), 0);
  40849. }
  40850. else {
  40851. ExpectIntEQ(BIO_should_retry(bio), 0);
  40852. ExpectIntEQ(BIO_should_read(bio), 0);
  40853. ExpectIntEQ(BIO_should_write(bio), 0);
  40854. }
  40855. ExpectIntEQ(XMEMCMP(reply, "I hear you fa shizzle!",
  40856. XSTRLEN("I hear you fa shizzle!")), 0);
  40857. BIO_free(bio);
  40858. wolfSSL_CTX_free(ctx);
  40859. CloseSocket(sockfd);
  40860. join_thread(serverThread);
  40861. FreeTcpReady(&ready);
  40862. #ifdef WOLFSSL_TIRTOS
  40863. fdOpenSession(Task_self());
  40864. #endif
  40865. #endif
  40866. return EXPECT_RESULT();
  40867. }
  40868. static int test_wolfSSL_BIO_connect(void)
  40869. {
  40870. EXPECT_DECLS;
  40871. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  40872. defined(HAVE_HTTP_CLIENT) && !defined(NO_WOLFSSL_CLIENT)
  40873. tcp_ready ready;
  40874. func_args server_args;
  40875. THREAD_TYPE serverThread;
  40876. BIO *tcpBio = NULL;
  40877. BIO *sslBio = NULL;
  40878. SSL_CTX* ctx = NULL;
  40879. SSL *ssl = NULL;
  40880. SSL *sslPtr;
  40881. char msg[] = "hello wolfssl!";
  40882. char reply[30];
  40883. char buff[10] = {0};
  40884. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  40885. ExpectIntEQ(WOLFSSL_SUCCESS,
  40886. wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0));
  40887. ExpectIntEQ(WOLFSSL_SUCCESS,
  40888. wolfSSL_CTX_use_certificate_file(ctx, cliCertFile, SSL_FILETYPE_PEM));
  40889. ExpectIntEQ(WOLFSSL_SUCCESS,
  40890. wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile, SSL_FILETYPE_PEM));
  40891. /* Setup server */
  40892. XMEMSET(&server_args, 0, sizeof(func_args));
  40893. StartTCP();
  40894. InitTcpReady(&ready);
  40895. #if defined(USE_WINDOWS_API)
  40896. /* use RNG to get random port if using windows */
  40897. ready.port = GetRandomPort();
  40898. #endif
  40899. server_args.signal = &ready;
  40900. start_thread(test_server_nofail, &server_args, &serverThread);
  40901. wait_tcp_ready(&server_args);
  40902. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  40903. /* Start the test proper */
  40904. /* Setup the TCP BIO */
  40905. ExpectNotNull(tcpBio = BIO_new_connect(wolfSSLIP));
  40906. ExpectIntEQ(BIO_set_conn_port(tcpBio, buff), 1);
  40907. /* Setup the SSL object */
  40908. ExpectNotNull(ssl = SSL_new(ctx));
  40909. SSL_set_connect_state(ssl);
  40910. /* Setup the SSL BIO */
  40911. ExpectNotNull(sslBio = BIO_new(BIO_f_ssl()));
  40912. ExpectIntEQ(BIO_set_ssl(sslBio, ssl, BIO_CLOSE), 1);
  40913. if (EXPECT_FAIL()) {
  40914. wolfSSL_free(ssl);
  40915. }
  40916. /* Verify that BIO_get_ssl works. */
  40917. ExpectIntEQ(BIO_get_ssl(sslBio, &sslPtr), 1);
  40918. ExpectPtrEq(ssl, sslPtr);
  40919. /* Link BIO's so that sslBio uses tcpBio for IO */
  40920. ExpectPtrEq(BIO_push(sslBio, tcpBio), sslBio);
  40921. /* Do TCP connect */
  40922. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  40923. /* Do TLS handshake */
  40924. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  40925. /* Test writing */
  40926. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  40927. /* Expect length of default wolfSSL reply */
  40928. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  40929. /* Clean it all up */
  40930. BIO_free_all(sslBio);
  40931. /* Server clean up */
  40932. join_thread(serverThread);
  40933. FreeTcpReady(&ready);
  40934. /* Run the same test, but use BIO_new_ssl_connect and set the IP and port
  40935. * after. */
  40936. XMEMSET(&server_args, 0, sizeof(func_args));
  40937. StartTCP();
  40938. InitTcpReady(&ready);
  40939. #if defined(USE_WINDOWS_API)
  40940. /* use RNG to get random port if using windows */
  40941. ready.port = GetRandomPort();
  40942. #endif
  40943. server_args.signal = &ready;
  40944. start_thread(test_server_nofail, &server_args, &serverThread);
  40945. wait_tcp_ready(&server_args);
  40946. ExpectIntGT(XSPRINTF(buff, "%d", ready.port), 0);
  40947. ExpectNotNull(sslBio = BIO_new_ssl_connect(ctx));
  40948. ExpectIntEQ(BIO_set_conn_hostname(sslBio, (char*)wolfSSLIP), 1);
  40949. ExpectIntEQ(BIO_set_conn_port(sslBio, buff), 1);
  40950. ExpectIntEQ(BIO_do_connect(sslBio), 1);
  40951. ExpectIntEQ(BIO_do_handshake(sslBio), 1);
  40952. ExpectIntEQ(BIO_write(sslBio, msg, sizeof(msg)), sizeof(msg));
  40953. ExpectIntEQ(BIO_read(sslBio, reply, sizeof(reply)), 23);
  40954. /* Attempt to close the TLS connection gracefully. */
  40955. BIO_ssl_shutdown(sslBio);
  40956. BIO_free_all(sslBio);
  40957. join_thread(serverThread);
  40958. FreeTcpReady(&ready);
  40959. SSL_CTX_free(ctx);
  40960. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  40961. wc_ecc_fp_free(); /* free per thread cache */
  40962. #endif
  40963. #endif
  40964. return EXPECT_RESULT();
  40965. }
  40966. static int test_wolfSSL_BIO_tls(void)
  40967. {
  40968. EXPECT_DECLS;
  40969. #if !defined(NO_BIO) && defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_CLIENT)
  40970. SSL_CTX* ctx = NULL;
  40971. SSL *ssl = NULL;
  40972. BIO *readBio = NULL;
  40973. BIO *writeBio = NULL;
  40974. int ret;
  40975. int err = 0;
  40976. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  40977. ExpectNotNull(ssl = SSL_new(ctx));
  40978. ExpectNotNull(readBio = BIO_new(BIO_s_mem()));
  40979. ExpectNotNull(writeBio = BIO_new(BIO_s_mem()));
  40980. /* Qt reads data from write-bio,
  40981. * then writes the read data into plain packet.
  40982. * Qt reads data from plain packet,
  40983. * then writes the read data into read-bio.
  40984. */
  40985. SSL_set_bio(ssl, readBio, writeBio);
  40986. do {
  40987. #ifdef WOLFSSL_ASYNC_CRYPT
  40988. if (err == WC_PENDING_E) {
  40989. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  40990. if (ret < 0) { break; } else if (ret == 0) { continue; }
  40991. }
  40992. #endif
  40993. ret = SSL_connect(ssl);
  40994. err = SSL_get_error(ssl, 0);
  40995. } while (err == WC_PENDING_E);
  40996. ExpectIntEQ(ret, WOLFSSL_FATAL_ERROR);
  40997. /* in this use case, should return WANT READ
  40998. * so that Qt will read the data from plain packet for next state.
  40999. */
  41000. ExpectIntEQ(err, SSL_ERROR_WANT_READ);
  41001. SSL_free(ssl);
  41002. SSL_CTX_free(ctx);
  41003. #endif
  41004. return EXPECT_RESULT();
  41005. }
  41006. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  41007. defined(HAVE_HTTP_CLIENT)
  41008. static THREAD_RETURN WOLFSSL_THREAD test_wolfSSL_BIO_accept_client(void* args)
  41009. {
  41010. BIO* clientBio;
  41011. SSL* sslClient;
  41012. SSL_CTX* ctx;
  41013. char connectAddr[20]; /* IP + port */;
  41014. (void)args;
  41015. AssertIntGT(snprintf(connectAddr, sizeof(connectAddr), "%s:%d", wolfSSLIP, wolfSSLPort), 0);
  41016. AssertNotNull(clientBio = BIO_new_connect(connectAddr));
  41017. AssertIntEQ(BIO_do_connect(clientBio), 1);
  41018. AssertNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  41019. AssertNotNull(sslClient = SSL_new(ctx));
  41020. AssertIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0), WOLFSSL_SUCCESS);
  41021. SSL_set_bio(sslClient, clientBio, clientBio);
  41022. AssertIntEQ(SSL_connect(sslClient), 1);
  41023. SSL_free(sslClient);
  41024. SSL_CTX_free(ctx);
  41025. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  41026. wc_ecc_fp_free(); /* free per thread cache */
  41027. #endif
  41028. WOLFSSL_RETURN_FROM_THREAD(0);
  41029. }
  41030. #endif
  41031. static int test_wolfSSL_BIO_accept(void)
  41032. {
  41033. EXPECT_DECLS;
  41034. #if defined(OPENSSL_ALL) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  41035. defined(HAVE_HTTP_CLIENT)
  41036. BIO* serverBindBio = NULL;
  41037. BIO* serverAcceptBio = NULL;
  41038. SSL* sslServer = NULL;
  41039. SSL_CTX* ctx = NULL;
  41040. func_args args;
  41041. THREAD_TYPE thread;
  41042. char port[10]; /* 10 bytes should be enough to store the string
  41043. * representation of the port */
  41044. ExpectIntGT(snprintf(port, sizeof(port), "%d", wolfSSLPort), 0);
  41045. ExpectNotNull(serverBindBio = BIO_new_accept(port));
  41046. /* First BIO_do_accept binds the port */
  41047. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  41048. XMEMSET(&args, 0, sizeof(func_args));
  41049. start_thread(test_wolfSSL_BIO_accept_client, &args, &thread);
  41050. ExpectIntEQ(BIO_do_accept(serverBindBio), 1);
  41051. /* Let's plug it into SSL to test */
  41052. ExpectNotNull(ctx = SSL_CTX_new(SSLv23_method()));
  41053. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  41054. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  41055. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  41056. SSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  41057. ExpectNotNull(sslServer = SSL_new(ctx));
  41058. ExpectNotNull(serverAcceptBio = BIO_pop(serverBindBio));
  41059. SSL_set_bio(sslServer, serverAcceptBio, serverAcceptBio);
  41060. ExpectIntEQ(SSL_accept(sslServer), 1);
  41061. join_thread(thread);
  41062. BIO_free(serverBindBio);
  41063. SSL_free(sslServer);
  41064. SSL_CTX_free(ctx);
  41065. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  41066. wc_ecc_fp_free(); /* free per thread cache */
  41067. #endif
  41068. #endif
  41069. return EXPECT_RESULT();
  41070. }
  41071. static int test_wolfSSL_BIO_write(void)
  41072. {
  41073. EXPECT_DECLS;
  41074. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_BASE64_ENCODE)
  41075. BIO* bio = NULL;
  41076. BIO* bio64 = NULL;
  41077. BIO* bio_mem = NULL;
  41078. BIO* ptr = NULL;
  41079. int sz;
  41080. char msg[] = "conversion test";
  41081. char out[40];
  41082. char expected[] = "Y29udmVyc2lvbiB0ZXN0AA==\n";
  41083. void* bufPtr = NULL;
  41084. BUF_MEM* buf = NULL;
  41085. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  41086. ExpectNotNull(bio = BIO_push(bio64, BIO_new(BIO_s_mem())));
  41087. if (EXPECT_FAIL()) {
  41088. BIO_free(bio64);
  41089. }
  41090. /* now should convert to base64 then write to memory */
  41091. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  41092. BIO_flush(bio);
  41093. /* test BIO chain */
  41094. ExpectIntEQ(SSL_SUCCESS, (int)BIO_get_mem_ptr(bio, &buf));
  41095. ExpectNotNull(buf);
  41096. ExpectIntEQ(buf->length, 25);
  41097. ExpectIntEQ(BIO_get_mem_data(bio, &bufPtr), 25);
  41098. ExpectPtrEq(buf->data, bufPtr);
  41099. ExpectNotNull(ptr = BIO_find_type(bio, BIO_TYPE_MEM));
  41100. sz = sizeof(out);
  41101. XMEMSET(out, 0, sz);
  41102. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 25);
  41103. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  41104. /* write then read should return the same message */
  41105. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  41106. sz = sizeof(out);
  41107. XMEMSET(out, 0, sz);
  41108. ExpectIntEQ(BIO_read(bio, out, sz), 16);
  41109. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  41110. /* now try encoding with no line ending */
  41111. BIO_set_flags(bio64, BIO_FLAGS_BASE64_NO_NL);
  41112. #ifdef HAVE_EX_DATA
  41113. BIO_set_ex_data(bio64, 0, (void*) "data");
  41114. ExpectIntEQ(strcmp((const char*)BIO_get_ex_data(bio64, 0), "data"), 0);
  41115. #endif
  41116. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  41117. BIO_flush(bio);
  41118. sz = sizeof(out);
  41119. XMEMSET(out, 0, sz);
  41120. ExpectIntEQ((sz = BIO_read(ptr, out, sz)), 24);
  41121. ExpectIntEQ(XMEMCMP(out, expected, sz), 0);
  41122. BIO_free_all(bio); /* frees bio64 also */
  41123. bio = NULL;
  41124. /* test with more than one bio64 in list */
  41125. ExpectNotNull(bio64 = BIO_new(BIO_f_base64()));
  41126. ExpectNotNull(bio = BIO_push(BIO_new(BIO_f_base64()), bio64));
  41127. if (EXPECT_FAIL()) {
  41128. BIO_free(bio64);
  41129. bio64 = NULL;
  41130. }
  41131. ExpectNotNull(bio_mem = BIO_new(BIO_s_mem()));
  41132. ExpectNotNull(BIO_push(bio64, bio_mem));
  41133. if (EXPECT_FAIL()) {
  41134. BIO_free(bio_mem);
  41135. }
  41136. /* now should convert to base64 when stored and then decode with read */
  41137. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 25);
  41138. BIO_flush(bio);
  41139. sz = sizeof(out);
  41140. XMEMSET(out, 0, sz);
  41141. ExpectIntEQ((sz = BIO_read(bio, out, sz)), 16);
  41142. ExpectIntEQ(XMEMCMP(out, msg, sz), 0);
  41143. BIO_clear_flags(bio64, ~0);
  41144. BIO_set_retry_read(bio);
  41145. BIO_free_all(bio); /* frees bio64s also */
  41146. bio = NULL;
  41147. ExpectNotNull(bio = BIO_new_mem_buf(out, 0));
  41148. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), sizeof(msg));
  41149. BIO_free(bio);
  41150. #endif
  41151. return EXPECT_RESULT();
  41152. }
  41153. static int test_wolfSSL_BIO_printf(void)
  41154. {
  41155. EXPECT_DECLS;
  41156. #if defined(OPENSSL_ALL)
  41157. BIO* bio = NULL;
  41158. int sz = 7;
  41159. char msg[] = "TLS 1.3 for the world";
  41160. char out[60];
  41161. char expected[] = "TLS 1.3 for the world : sz = 7";
  41162. XMEMSET(out, 0, sizeof(out));
  41163. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  41164. ExpectIntEQ(BIO_printf(bio, "%s : sz = %d", msg, sz), 30);
  41165. ExpectIntEQ(BIO_printf(NULL, ""), WOLFSSL_FATAL_ERROR);
  41166. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 30);
  41167. ExpectIntEQ(XSTRNCMP(out, expected, sizeof(expected)), 0);
  41168. BIO_free(bio);
  41169. #endif
  41170. return EXPECT_RESULT();
  41171. }
  41172. static int test_wolfSSL_BIO_f_md(void)
  41173. {
  41174. EXPECT_DECLS;
  41175. #if defined(OPENSSL_ALL) && !defined(NO_SHA256)
  41176. BIO* bio = NULL;
  41177. BIO* mem = NULL;
  41178. char msg[] = "message to hash";
  41179. char out[60];
  41180. EVP_MD_CTX* ctx = NULL;
  41181. const unsigned char testKey[] =
  41182. {
  41183. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  41184. 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b, 0x0b,
  41185. 0x0b, 0x0b, 0x0b, 0x0b
  41186. };
  41187. const char testData[] = "Hi There";
  41188. const unsigned char testResult[] =
  41189. {
  41190. 0xb0, 0x34, 0x4c, 0x61, 0xd8, 0xdb, 0x38, 0x53,
  41191. 0x5c, 0xa8, 0xaf, 0xce, 0xaf, 0x0b, 0xf1, 0x2b,
  41192. 0x88, 0x1d, 0xc2, 0x00, 0xc9, 0x83, 0x3d, 0xa7,
  41193. 0x26, 0xe9, 0x37, 0x6c, 0x2e, 0x32, 0xcf, 0xf7
  41194. };
  41195. const unsigned char expectedHash[] =
  41196. {
  41197. 0x66, 0x49, 0x3C, 0xE8, 0x8A, 0x57, 0xB0, 0x60,
  41198. 0xDC, 0x55, 0x7D, 0xFC, 0x1F, 0xA5, 0xE5, 0x07,
  41199. 0x70, 0x5A, 0xF6, 0xD7, 0xC4, 0x1F, 0x1A, 0xE4,
  41200. 0x2D, 0xA6, 0xFD, 0xD1, 0x29, 0x7D, 0x60, 0x0D
  41201. };
  41202. const unsigned char emptyHash[] =
  41203. {
  41204. 0xE3, 0xB0, 0xC4, 0x42, 0x98, 0xFC, 0x1C, 0x14,
  41205. 0x9A, 0xFB, 0xF4, 0xC8, 0x99, 0x6F, 0xB9, 0x24,
  41206. 0x27, 0xAE, 0x41, 0xE4, 0x64, 0x9B, 0x93, 0x4C,
  41207. 0xA4, 0x95, 0x99, 0x1B, 0x78, 0x52, 0xB8, 0x55
  41208. };
  41209. unsigned char check[sizeof(testResult) + 1];
  41210. size_t checkSz = -1;
  41211. EVP_PKEY* key = NULL;
  41212. XMEMSET(out, 0, sizeof(out));
  41213. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  41214. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  41215. ExpectIntEQ(BIO_get_md_ctx(bio, &ctx), 1);
  41216. ExpectIntEQ(EVP_DigestInit(ctx, EVP_sha256()), 1);
  41217. /* should not be able to write/read yet since just digest wrapper and no
  41218. * data is passing through the bio */
  41219. ExpectIntEQ(BIO_write(bio, msg, 0), 0);
  41220. ExpectIntEQ(BIO_pending(bio), 0);
  41221. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 0);
  41222. ExpectIntEQ(BIO_gets(bio, out, 3), 0);
  41223. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  41224. ExpectIntEQ(XMEMCMP(emptyHash, out, 32), 0);
  41225. BIO_reset(bio);
  41226. /* append BIO mem to bio in order to read/write */
  41227. ExpectNotNull(bio = BIO_push(bio, mem));
  41228. XMEMSET(out, 0, sizeof(out));
  41229. ExpectIntEQ(BIO_write(mem, msg, sizeof(msg)), 16);
  41230. ExpectIntEQ(BIO_pending(bio), 16);
  41231. /* this just reads the message and does not hash it (gets calls final) */
  41232. ExpectIntEQ(BIO_read(bio, out, sizeof(out)), 16);
  41233. ExpectIntEQ(XMEMCMP(out, msg, sizeof(msg)), 0);
  41234. /* create a message digest using BIO */
  41235. XMEMSET(out, 0, sizeof(out));
  41236. ExpectIntEQ(BIO_write(bio, msg, sizeof(msg)), 16);
  41237. ExpectIntEQ(BIO_pending(mem), 16);
  41238. ExpectIntEQ(BIO_pending(bio), 16);
  41239. ExpectIntEQ(BIO_gets(bio, out, sizeof(out)), 32);
  41240. ExpectIntEQ(XMEMCMP(expectedHash, out, 32), 0);
  41241. BIO_free(bio);
  41242. bio = NULL;
  41243. BIO_free(mem);
  41244. mem = NULL;
  41245. /* test with HMAC */
  41246. XMEMSET(out, 0, sizeof(out));
  41247. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  41248. ExpectNotNull(mem = BIO_new(BIO_s_mem()));
  41249. BIO_get_md_ctx(bio, &ctx);
  41250. ExpectNotNull(key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, testKey,
  41251. (int)sizeof(testKey)));
  41252. EVP_DigestSignInit(ctx, NULL, EVP_sha256(), NULL, key);
  41253. ExpectNotNull(bio = BIO_push(bio, mem));
  41254. BIO_write(bio, testData, (int)strlen(testData));
  41255. ExpectIntEQ(EVP_DigestSignFinal(ctx, NULL, &checkSz), 1);
  41256. ExpectIntEQ(EVP_DigestSignFinal(ctx, check, &checkSz), 1);
  41257. ExpectIntEQ(XMEMCMP(check, testResult, sizeof(testResult)), 0);
  41258. EVP_PKEY_free(key);
  41259. BIO_free(bio);
  41260. BIO_free(mem);
  41261. #endif
  41262. return EXPECT_RESULT();
  41263. }
  41264. static int test_wolfSSL_BIO_up_ref(void)
  41265. {
  41266. EXPECT_DECLS;
  41267. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  41268. BIO* bio = NULL;
  41269. ExpectNotNull(bio = BIO_new(BIO_f_md()));
  41270. ExpectIntEQ(BIO_up_ref(NULL), 0);
  41271. ExpectIntEQ(BIO_up_ref(bio), 1);
  41272. BIO_free(bio);
  41273. ExpectIntEQ(BIO_up_ref(bio), 1);
  41274. BIO_free(bio);
  41275. BIO_free(bio);
  41276. #endif
  41277. return EXPECT_RESULT();
  41278. }
  41279. static int test_wolfSSL_BIO_reset(void)
  41280. {
  41281. EXPECT_DECLS;
  41282. #if defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)
  41283. BIO* bio = NULL;
  41284. byte buf[16];
  41285. ExpectNotNull(bio = BIO_new_mem_buf("secure your data",
  41286. (word32)XSTRLEN("secure your data")));
  41287. ExpectIntEQ(BIO_read(bio, buf, 6), 6);
  41288. ExpectIntEQ(XMEMCMP(buf, "secure", 6), 0);
  41289. XMEMSET(buf, 0, 16);
  41290. ExpectIntEQ(BIO_read(bio, buf, 16), 10);
  41291. ExpectIntEQ(XMEMCMP(buf, " your data", 10), 0);
  41292. /* You cannot write to MEM BIO with read-only mode. */
  41293. ExpectIntEQ(BIO_write(bio, "WriteToReadonly", 15), 0);
  41294. ExpectIntEQ(BIO_read(bio, buf, 16), -1);
  41295. XMEMSET(buf, 0, 16);
  41296. ExpectIntEQ(BIO_reset(bio), 0);
  41297. ExpectIntEQ(BIO_read(bio, buf, 16), 16);
  41298. ExpectIntEQ(XMEMCMP(buf, "secure your data", 16), 0);
  41299. BIO_free(bio);
  41300. #endif
  41301. return EXPECT_RESULT();
  41302. }
  41303. #endif /* !NO_BIO */
  41304. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  41305. /* test that the callback arg is correct */
  41306. static int certCbArg = 0;
  41307. static int certCb(WOLFSSL* ssl, void* arg)
  41308. {
  41309. if (ssl == NULL || arg != &certCbArg)
  41310. return 0;
  41311. if (wolfSSL_is_server(ssl)) {
  41312. if (wolfSSL_use_certificate_file(ssl, svrCertFile,
  41313. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41314. return 0;
  41315. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile,
  41316. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41317. return 0;
  41318. }
  41319. else {
  41320. if (wolfSSL_use_certificate_file(ssl, cliCertFile,
  41321. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41322. return 0;
  41323. if (wolfSSL_use_PrivateKey_file(ssl, cliKeyFile,
  41324. WOLFSSL_FILETYPE_PEM) != WOLFSSL_SUCCESS)
  41325. return 0;
  41326. }
  41327. return 1;
  41328. }
  41329. static int certSetupCb(WOLFSSL_CTX* ctx)
  41330. {
  41331. SSL_CTX_set_cert_cb(ctx, certCb, &certCbArg);
  41332. return TEST_SUCCESS;
  41333. }
  41334. /**
  41335. * This is only done because test_wolfSSL_client_server_nofail_memio has no way
  41336. * to stop certificate and key loading
  41337. */
  41338. static int certClearCb(WOLFSSL* ssl)
  41339. {
  41340. /* Clear the loaded certs to force the callbacks to set them up */
  41341. SSL_certs_clear(ssl);
  41342. return TEST_SUCCESS;
  41343. }
  41344. #endif
  41345. static int test_wolfSSL_cert_cb(void)
  41346. {
  41347. EXPECT_DECLS;
  41348. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  41349. test_ssl_cbf func_cb_client;
  41350. test_ssl_cbf func_cb_server;
  41351. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  41352. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  41353. func_cb_client.ctx_ready = certSetupCb;
  41354. func_cb_client.ssl_ready = certClearCb;
  41355. func_cb_server.ctx_ready = certSetupCb;
  41356. func_cb_server.ssl_ready = certClearCb;
  41357. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  41358. &func_cb_server, NULL), TEST_SUCCESS);
  41359. #endif
  41360. return EXPECT_RESULT();
  41361. }
  41362. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  41363. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_cipher = NULL;
  41364. static const char* test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs = NULL;
  41365. static int test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready(WOLFSSL_CTX* ctx)
  41366. {
  41367. EXPECT_DECLS;
  41368. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx,
  41369. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher), WOLFSSL_SUCCESS);
  41370. ExpectIntEQ(wolfSSL_CTX_set1_sigalgs_list(ctx,
  41371. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs), WOLFSSL_SUCCESS);
  41372. return EXPECT_RESULT();
  41373. }
  41374. static int test_wolfSSL_cert_cb_dyn_ciphers_certCB(WOLFSSL* ssl, void* arg)
  41375. {
  41376. const byte* suites = NULL;
  41377. word16 suiteSz = 0;
  41378. const byte* hashSigAlgo = NULL;
  41379. word16 hashSigAlgoSz = 0;
  41380. word16 idx = 0;
  41381. int haveRSA = 0;
  41382. int haveECC = 0;
  41383. (void)arg;
  41384. if (wolfSSL_get_client_suites_sigalgs(ssl, &suites, &suiteSz, &hashSigAlgo,
  41385. &hashSigAlgoSz) != WOLFSSL_SUCCESS)
  41386. return 0;
  41387. if (suites == NULL || suiteSz == 0 || hashSigAlgo == NULL ||
  41388. hashSigAlgoSz == 0)
  41389. return 0;
  41390. for (idx = 0; idx < suiteSz; idx += 2) {
  41391. WOLFSSL_CIPHERSUITE_INFO info =
  41392. wolfSSL_get_ciphersuite_info(suites[idx], suites[idx+1]);
  41393. if (info.rsaAuth)
  41394. haveRSA = 1;
  41395. else if (info.eccAuth)
  41396. haveECC = 1;
  41397. }
  41398. if (hashSigAlgoSz > 0) {
  41399. /* sigalgs extension takes precedence over ciphersuites */
  41400. haveRSA = 0;
  41401. haveECC = 0;
  41402. }
  41403. for (idx = 0; idx < hashSigAlgoSz; idx += 2) {
  41404. int hashAlgo = 0;
  41405. int sigAlgo = 0;
  41406. if (wolfSSL_get_sigalg_info(hashSigAlgo[idx+0], hashSigAlgo[idx+1],
  41407. &hashAlgo, &sigAlgo) != 0)
  41408. return 0;
  41409. if (sigAlgo == RSAk || sigAlgo == RSAPSSk)
  41410. haveRSA = 1;
  41411. else if (sigAlgo == ECDSAk)
  41412. haveECC = 1;
  41413. }
  41414. if (haveRSA) {
  41415. if (wolfSSL_use_certificate_file(ssl, svrCertFile, WOLFSSL_FILETYPE_PEM)
  41416. != WOLFSSL_SUCCESS)
  41417. return 0;
  41418. if (wolfSSL_use_PrivateKey_file(ssl, svrKeyFile, WOLFSSL_FILETYPE_PEM)
  41419. != WOLFSSL_SUCCESS)
  41420. return 0;
  41421. }
  41422. else if (haveECC) {
  41423. if (wolfSSL_use_certificate_file(ssl, eccCertFile, WOLFSSL_FILETYPE_PEM)
  41424. != WOLFSSL_SUCCESS)
  41425. return 0;
  41426. if (wolfSSL_use_PrivateKey_file(ssl, eccKeyFile, WOLFSSL_FILETYPE_PEM)
  41427. != WOLFSSL_SUCCESS)
  41428. return 0;
  41429. }
  41430. return 1;
  41431. }
  41432. static int test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready(WOLFSSL_CTX* ctx)
  41433. {
  41434. SSL_CTX_set_cert_cb(ctx, test_wolfSSL_cert_cb_dyn_ciphers_certCB, NULL);
  41435. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, NULL);
  41436. return TEST_SUCCESS;
  41437. }
  41438. #endif
  41439. /* Testing dynamic ciphers offered by client */
  41440. static int test_wolfSSL_cert_cb_dyn_ciphers(void)
  41441. {
  41442. EXPECT_DECLS;
  41443. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  41444. test_ssl_cbf func_cb_client;
  41445. test_ssl_cbf func_cb_server;
  41446. struct {
  41447. method_provider client_meth;
  41448. const char* client_ciphers;
  41449. const char* client_sigalgs;
  41450. const char* client_ca;
  41451. method_provider server_meth;
  41452. } test_params[] = {
  41453. #if !defined(NO_SHA256) && defined(HAVE_AESGCM)
  41454. #ifdef WOLFSSL_TLS13
  41455. #if !defined(NO_RSA) && defined(WC_RSA_PSS)
  41456. {wolfTLSv1_3_client_method,
  41457. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  41458. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_3_server_method},
  41459. #endif
  41460. #ifdef HAVE_ECC
  41461. {wolfTLSv1_3_client_method,
  41462. "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256",
  41463. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_3_server_method},
  41464. #endif
  41465. #endif
  41466. #ifndef WOLFSSL_NO_TLS12
  41467. #if !defined(NO_RSA) && defined(WC_RSA_PSS) && !defined(NO_DH)
  41468. {wolfTLSv1_2_client_method,
  41469. "DHE-RSA-AES128-GCM-SHA256",
  41470. "RSA-PSS+SHA256", caCertFile, wolfTLSv1_2_server_method},
  41471. #endif
  41472. #ifdef HAVE_ECC
  41473. {wolfTLSv1_2_client_method,
  41474. "ECDHE-ECDSA-AES128-GCM-SHA256",
  41475. "ECDSA+SHA256", caEccCertFile, wolfTLSv1_2_server_method},
  41476. #endif
  41477. #endif
  41478. #endif
  41479. };
  41480. size_t i;
  41481. size_t testCount = sizeof(test_params)/sizeof(*test_params);
  41482. if (testCount > 0) {
  41483. for (i = 0; i < testCount; i++) {
  41484. printf("\tTesting %s ciphers with %s sigalgs\n",
  41485. test_params[i].client_ciphers,
  41486. test_params[i].client_sigalgs);
  41487. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  41488. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  41489. test_wolfSSL_cert_cb_dyn_ciphers_client_cipher =
  41490. test_params[i].client_ciphers;
  41491. test_wolfSSL_cert_cb_dyn_ciphers_client_sigalgs =
  41492. test_params[i].client_sigalgs;
  41493. func_cb_client.method = test_params[i].client_meth;
  41494. func_cb_client.caPemFile = test_params[i].client_ca;
  41495. func_cb_client.ctx_ready =
  41496. test_wolfSSL_cert_cb_dyn_ciphers_client_ctx_ready;
  41497. func_cb_server.ctx_ready =
  41498. test_wolfSSL_cert_cb_dyn_ciphers_server_ctx_ready;
  41499. func_cb_server.ssl_ready = certClearCb; /* Reuse from prev test */
  41500. func_cb_server.method = test_params[i].server_meth;
  41501. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  41502. &func_cb_server, NULL), TEST_SUCCESS);
  41503. }
  41504. }
  41505. #endif
  41506. return EXPECT_RESULT();
  41507. }
  41508. static int test_wolfSSL_ciphersuite_auth(void)
  41509. {
  41510. EXPECT_DECLS;
  41511. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  41512. WOLFSSL_CIPHERSUITE_INFO info;
  41513. (void)info;
  41514. #ifndef WOLFSSL_NO_TLS12
  41515. #ifdef HAVE_CHACHA
  41516. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  41517. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256);
  41518. ExpectIntEQ(info.rsaAuth, 1);
  41519. ExpectIntEQ(info.eccAuth, 0);
  41520. ExpectIntEQ(info.eccStatic, 0);
  41521. ExpectIntEQ(info.psk, 0);
  41522. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  41523. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256);
  41524. ExpectIntEQ(info.rsaAuth, 0);
  41525. ExpectIntEQ(info.eccAuth, 1);
  41526. ExpectIntEQ(info.eccStatic, 0);
  41527. ExpectIntEQ(info.psk, 0);
  41528. info = wolfSSL_get_ciphersuite_info(CHACHA_BYTE,
  41529. TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256);
  41530. ExpectIntEQ(info.rsaAuth, 0);
  41531. ExpectIntEQ(info.eccAuth, 0);
  41532. ExpectIntEQ(info.eccStatic, 0);
  41533. ExpectIntEQ(info.psk, 1);
  41534. #endif
  41535. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  41536. #ifndef NO_RSA
  41537. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  41538. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA);
  41539. ExpectIntEQ(info.rsaAuth, 1);
  41540. ExpectIntEQ(info.eccAuth, 0);
  41541. ExpectIntEQ(info.eccStatic, 0);
  41542. ExpectIntEQ(info.psk, 0);
  41543. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  41544. TLS_ECDH_RSA_WITH_AES_128_CBC_SHA);
  41545. ExpectIntEQ(info.rsaAuth, 1);
  41546. ExpectIntEQ(info.eccAuth, 0);
  41547. ExpectIntEQ(info.eccStatic, 1);
  41548. ExpectIntEQ(info.psk, 0);
  41549. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  41550. TLS_ECDH_RSA_WITH_AES_256_CBC_SHA);
  41551. ExpectIntEQ(info.rsaAuth, 1);
  41552. ExpectIntEQ(info.eccAuth, 0);
  41553. ExpectIntEQ(info.eccStatic, 1);
  41554. ExpectIntEQ(info.psk, 0);
  41555. #endif
  41556. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  41557. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA);
  41558. ExpectIntEQ(info.rsaAuth, 0);
  41559. ExpectIntEQ(info.eccAuth, 1);
  41560. ExpectIntEQ(info.eccStatic, 0);
  41561. ExpectIntEQ(info.psk, 0);
  41562. info = wolfSSL_get_ciphersuite_info(ECC_BYTE,
  41563. TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA);
  41564. ExpectIntEQ(info.rsaAuth, 0);
  41565. ExpectIntEQ(info.eccAuth, 1);
  41566. ExpectIntEQ(info.eccStatic, 1);
  41567. ExpectIntEQ(info.psk, 0);
  41568. info = wolfSSL_get_ciphersuite_info(ECDHE_PSK_BYTE,
  41569. TLS_ECDHE_PSK_WITH_AES_128_GCM_SHA256);
  41570. ExpectIntEQ(info.rsaAuth, 0);
  41571. ExpectIntEQ(info.eccAuth, 0);
  41572. ExpectIntEQ(info.eccStatic, 0);
  41573. ExpectIntEQ(info.psk, 1);
  41574. #endif
  41575. #endif
  41576. #ifdef WOLFSSL_TLS13
  41577. info = wolfSSL_get_ciphersuite_info(TLS13_BYTE,
  41578. TLS_AES_128_GCM_SHA256);
  41579. ExpectIntEQ(info.rsaAuth, 0);
  41580. ExpectIntEQ(info.eccAuth, 0);
  41581. ExpectIntEQ(info.eccStatic, 0);
  41582. ExpectIntEQ(info.psk, 0);
  41583. #endif
  41584. #endif
  41585. return EXPECT_RESULT();
  41586. }
  41587. static int test_wolfSSL_sigalg_info(void)
  41588. {
  41589. EXPECT_DECLS;
  41590. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_EXTRA)
  41591. byte hashSigAlgo[WOLFSSL_MAX_SIGALGO];
  41592. word16 len = 0;
  41593. word16 idx = 0;
  41594. int allSigAlgs = SIG_ECDSA | SIG_RSA | SIG_SM2 | SIG_FALCON | SIG_DILITHIUM;
  41595. InitSuitesHashSigAlgo(hashSigAlgo, allSigAlgs, 1, 0xFFFFFFFF, &len);
  41596. for (idx = 0; idx < len; idx += 2) {
  41597. int hashAlgo = 0;
  41598. int sigAlgo = 0;
  41599. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  41600. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  41601. ExpectIntNE(hashAlgo, 0);
  41602. ExpectIntNE(sigAlgo, 0);
  41603. }
  41604. InitSuitesHashSigAlgo(hashSigAlgo, allSigAlgs | SIG_ANON, 1,
  41605. 0xFFFFFFFF, &len);
  41606. for (idx = 0; idx < len; idx += 2) {
  41607. int hashAlgo = 0;
  41608. int sigAlgo = 0;
  41609. ExpectIntEQ(wolfSSL_get_sigalg_info(hashSigAlgo[idx+0],
  41610. hashSigAlgo[idx+1], &hashAlgo, &sigAlgo), 0);
  41611. ExpectIntNE(hashAlgo, 0);
  41612. }
  41613. #endif
  41614. return EXPECT_RESULT();
  41615. }
  41616. static int test_wolfSSL_SESSION(void)
  41617. {
  41618. EXPECT_DECLS;
  41619. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41620. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  41621. !defined(NO_SESSION_CACHE)
  41622. WOLFSSL* ssl = NULL;
  41623. WOLFSSL_CTX* ctx = NULL;
  41624. WOLFSSL_SESSION* sess = NULL;
  41625. WOLFSSL_SESSION* sess_copy = NULL;
  41626. #ifdef OPENSSL_EXTRA
  41627. #ifdef HAVE_EXT_CACHE
  41628. unsigned char* sessDer = NULL;
  41629. unsigned char* ptr = NULL;
  41630. int sz = 0;
  41631. #endif
  41632. const unsigned char context[] = "user app context";
  41633. unsigned int contextSz = (unsigned int)sizeof(context);
  41634. #endif
  41635. int ret = 0, err = 0;
  41636. SOCKET_T sockfd;
  41637. tcp_ready ready;
  41638. func_args server_args;
  41639. THREAD_TYPE serverThread;
  41640. char msg[80];
  41641. const char* sendGET = "GET";
  41642. /* TLS v1.3 requires session tickets */
  41643. /* CHACHA and POLY1305 required for myTicketEncCb */
  41644. #if defined(WOLFSSL_TLS13) && (!defined(HAVE_SESSION_TICKET) && \
  41645. !defined(WOLFSSL_NO_TLS12) || !(defined(HAVE_CHACHA) && \
  41646. defined(HAVE_POLY1305) && !defined(HAVE_AESGCM)))
  41647. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method()));
  41648. #else
  41649. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  41650. #endif
  41651. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, cliCertFile,
  41652. WOLFSSL_FILETYPE_PEM));
  41653. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, cliKeyFile,
  41654. WOLFSSL_FILETYPE_PEM));
  41655. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  41656. WOLFSSL_SUCCESS);
  41657. #ifdef WOLFSSL_ENCRYPTED_KEYS
  41658. wolfSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  41659. #endif
  41660. #ifdef HAVE_SESSION_TICKET
  41661. /* Use session tickets, for ticket tests below */
  41662. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  41663. #endif
  41664. XMEMSET(&server_args, 0, sizeof(func_args));
  41665. #ifdef WOLFSSL_TIRTOS
  41666. fdOpenSession(Task_self());
  41667. #endif
  41668. StartTCP();
  41669. InitTcpReady(&ready);
  41670. #if defined(USE_WINDOWS_API)
  41671. /* use RNG to get random port if using windows */
  41672. ready.port = GetRandomPort();
  41673. #endif
  41674. server_args.signal = &ready;
  41675. start_thread(test_server_nofail, &server_args, &serverThread);
  41676. wait_tcp_ready(&server_args);
  41677. /* client connection */
  41678. ExpectNotNull(ssl = wolfSSL_new(ctx));
  41679. tcp_connect(&sockfd, wolfSSLIP, ready.port, 0, 0, ssl);
  41680. ExpectIntEQ(wolfSSL_set_fd(ssl, sockfd), WOLFSSL_SUCCESS);
  41681. #ifdef WOLFSSL_ASYNC_CRYPT
  41682. err = 0; /* Reset error */
  41683. #endif
  41684. do {
  41685. #ifdef WOLFSSL_ASYNC_CRYPT
  41686. if (err == WC_PENDING_E) {
  41687. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  41688. if (ret < 0) { break; } else if (ret == 0) { continue; }
  41689. }
  41690. #endif
  41691. ret = wolfSSL_connect(ssl);
  41692. err = wolfSSL_get_error(ssl, 0);
  41693. } while (err == WC_PENDING_E);
  41694. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  41695. #ifdef WOLFSSL_ASYNC_CRYPT
  41696. err = 0; /* Reset error */
  41697. #endif
  41698. do {
  41699. #ifdef WOLFSSL_ASYNC_CRYPT
  41700. if (err == WC_PENDING_E) {
  41701. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  41702. if (ret < 0) { break; } else if (ret == 0) { continue; }
  41703. }
  41704. #endif
  41705. ret = wolfSSL_write(ssl, sendGET, (int)XSTRLEN(sendGET));
  41706. err = wolfSSL_get_error(ssl, 0);
  41707. } while (err == WC_PENDING_E);
  41708. ExpectIntEQ(ret, (int)XSTRLEN(sendGET));
  41709. #ifdef WOLFSSL_ASYNC_CRYPT
  41710. err = 0; /* Reset error */
  41711. #endif
  41712. do {
  41713. #ifdef WOLFSSL_ASYNC_CRYPT
  41714. if (err == WC_PENDING_E) {
  41715. ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
  41716. if (ret < 0) { break; } else if (ret == 0) { continue; }
  41717. }
  41718. #endif
  41719. ret = wolfSSL_read(ssl, msg, sizeof(msg));
  41720. err = wolfSSL_get_error(ssl, 0);
  41721. } while (err == WC_PENDING_E);
  41722. ExpectIntEQ(ret, 23);
  41723. ExpectPtrNE((sess = wolfSSL_get1_session(ssl)), NULL); /* ref count 1 */
  41724. ExpectPtrNE((sess_copy = wolfSSL_get1_session(ssl)), NULL); /* ref count 2 */
  41725. ExpectIntEQ(wolfSSL_SessionIsSetup(sess), 1);
  41726. #ifdef HAVE_EXT_CACHE
  41727. ExpectPtrEq(sess, sess_copy); /* they should be the same pointer but without
  41728. * HAVE_EXT_CACHE we get new objects each time */
  41729. #endif
  41730. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  41731. wolfSSL_SESSION_free(sess); sess = NULL; /* free session ref */
  41732. sess = wolfSSL_get_session(ssl);
  41733. #ifdef OPENSSL_EXTRA
  41734. ExpectIntEQ(SSL_SESSION_is_resumable(NULL), 0);
  41735. ExpectIntEQ(SSL_SESSION_is_resumable(sess), 1);
  41736. ExpectIntEQ(wolfSSL_SESSION_has_ticket(NULL), 0);
  41737. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(NULL), 0);
  41738. #ifdef HAVE_SESSION_TICKET
  41739. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 1);
  41740. ExpectIntEQ(wolfSSL_SESSION_get_ticket_lifetime_hint(sess),
  41741. SESSION_TICKET_HINT_DEFAULT);
  41742. #else
  41743. ExpectIntEQ(wolfSSL_SESSION_has_ticket(sess), 0);
  41744. #endif
  41745. #else
  41746. (void)sess;
  41747. #endif /* OPENSSL_EXTRA */
  41748. /* Retain copy of the session for later testing */
  41749. ExpectNotNull(sess = wolfSSL_get1_session(ssl));
  41750. wolfSSL_shutdown(ssl);
  41751. wolfSSL_free(ssl); ssl = NULL;
  41752. CloseSocket(sockfd);
  41753. join_thread(serverThread);
  41754. FreeTcpReady(&ready);
  41755. #ifdef WOLFSSL_TIRTOS
  41756. fdOpenSession(Task_self());
  41757. #endif
  41758. #if defined(SESSION_CERTS) && defined(OPENSSL_EXTRA)
  41759. {
  41760. X509 *x509 = NULL;
  41761. char buf[30];
  41762. int bufSz = 0;
  41763. ExpectNotNull(x509 = SSL_SESSION_get0_peer(sess));
  41764. ExpectIntGT((bufSz = X509_NAME_get_text_by_NID(
  41765. X509_get_subject_name(x509), NID_organizationalUnitName, buf,
  41766. sizeof(buf))), 0);
  41767. ExpectIntNE((bufSz == 7 || bufSz == 16), 0); /* should be one of these*/
  41768. if (bufSz == 7) {
  41769. ExpectIntEQ(XMEMCMP(buf, "Support", bufSz), 0);
  41770. }
  41771. if (bufSz == 16) {
  41772. ExpectIntEQ(XMEMCMP(buf, "Programming-2048", bufSz), 0);
  41773. }
  41774. }
  41775. #endif
  41776. #ifdef HAVE_EXT_CACHE
  41777. ExpectNotNull(sess_copy = wolfSSL_SESSION_dup(sess));
  41778. wolfSSL_SESSION_free(sess_copy); sess_copy = NULL;
  41779. sess_copy = NULL;
  41780. #endif
  41781. #if defined(OPENSSL_EXTRA) && defined(HAVE_EXT_CACHE)
  41782. /* get session from DER and update the timeout */
  41783. ExpectIntEQ(wolfSSL_i2d_SSL_SESSION(NULL, &sessDer), BAD_FUNC_ARG);
  41784. ExpectIntGT((sz = wolfSSL_i2d_SSL_SESSION(sess, &sessDer)), 0);
  41785. wolfSSL_SESSION_free(sess); sess = NULL;
  41786. sess = NULL;
  41787. ptr = sessDer;
  41788. ExpectNull(sess = wolfSSL_d2i_SSL_SESSION(NULL, NULL, sz));
  41789. ExpectNotNull(sess = wolfSSL_d2i_SSL_SESSION(NULL,
  41790. (const unsigned char**)&ptr, sz));
  41791. XFREE(sessDer, NULL, DYNAMIC_TYPE_OPENSSL);
  41792. sessDer = NULL;
  41793. ExpectIntGT(wolfSSL_SESSION_get_time(sess), 0);
  41794. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  41795. #endif
  41796. /* successful set session test */
  41797. ExpectNotNull(ssl = wolfSSL_new(ctx));
  41798. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  41799. #ifdef HAVE_SESSION_TICKET
  41800. /* Test set/get session ticket */
  41801. {
  41802. const char* ticket = "This is a session ticket";
  41803. char buf[64] = {0};
  41804. word32 bufSz = (word32)sizeof(buf);
  41805. ExpectIntEQ(SSL_SUCCESS,
  41806. wolfSSL_set_SessionTicket(ssl, (byte *)ticket,
  41807. (word32)XSTRLEN(ticket)));
  41808. ExpectIntEQ(SSL_SUCCESS,
  41809. wolfSSL_get_SessionTicket(ssl, (byte *)buf, &bufSz));
  41810. ExpectStrEQ(ticket, buf);
  41811. }
  41812. #endif
  41813. #ifdef OPENSSL_EXTRA
  41814. /* session timeout case */
  41815. /* make the session to be expired */
  41816. ExpectIntEQ(SSL_SESSION_set_timeout(sess,1), SSL_SUCCESS);
  41817. XSLEEP_MS(1200);
  41818. /* SSL_set_session should reject specified session but return success
  41819. * if WOLFSSL_ERROR_CODE_OPENSSL macro is defined for OpenSSL compatibility.
  41820. */
  41821. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  41822. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_SUCCESS);
  41823. #else
  41824. ExpectIntEQ(wolfSSL_set_session(ssl,sess), SSL_FAILURE);
  41825. #endif
  41826. ExpectIntEQ(wolfSSL_SSL_SESSION_set_timeout(sess, 500), SSL_SUCCESS);
  41827. #ifdef WOLFSSL_SESSION_ID_CTX
  41828. /* fail case with miss match session context IDs (use compatibility API) */
  41829. ExpectIntEQ(SSL_set_session_id_context(ssl, context, contextSz),
  41830. SSL_SUCCESS);
  41831. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  41832. wolfSSL_free(ssl); ssl = NULL;
  41833. ExpectIntEQ(SSL_CTX_set_session_id_context(NULL, context, contextSz),
  41834. SSL_FAILURE);
  41835. ExpectIntEQ(SSL_CTX_set_session_id_context(ctx, context, contextSz),
  41836. SSL_SUCCESS);
  41837. ExpectNotNull(ssl = wolfSSL_new(ctx));
  41838. ExpectIntEQ(wolfSSL_set_session(ssl, sess), SSL_FAILURE);
  41839. #endif
  41840. #endif /* OPENSSL_EXTRA */
  41841. wolfSSL_free(ssl);
  41842. wolfSSL_SESSION_free(sess);
  41843. wolfSSL_CTX_free(ctx);
  41844. #endif
  41845. return EXPECT_RESULT();
  41846. }
  41847. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41848. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  41849. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  41850. !defined(WOLFSSL_NO_TLS12)
  41851. static WOLFSSL_SESSION* test_wolfSSL_SESSION_expire_sess = NULL;
  41852. static void test_wolfSSL_SESSION_expire_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  41853. {
  41854. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  41855. /* returns previous timeout value */
  41856. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), 500);
  41857. #else
  41858. AssertIntEQ(wolfSSL_CTX_set_timeout(ctx, 1), WOLFSSL_SUCCESS);
  41859. #endif
  41860. }
  41861. /* set the session to timeout in a second */
  41862. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready(WOLFSSL* ssl)
  41863. {
  41864. AssertIntEQ(wolfSSL_set_timeout(ssl, 2), 1);
  41865. }
  41866. /* store the client side session from the first successful connection */
  41867. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result(WOLFSSL* ssl)
  41868. {
  41869. AssertPtrNE((test_wolfSSL_SESSION_expire_sess = wolfSSL_get1_session(ssl)),
  41870. NULL); /* ref count 1 */
  41871. }
  41872. /* wait till session is expired then set it in the WOLFSSL struct for use */
  41873. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait(WOLFSSL* ssl)
  41874. {
  41875. AssertIntEQ(wolfSSL_set_timeout(ssl, 1), 1);
  41876. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  41877. WOLFSSL_SUCCESS);
  41878. XSLEEP_MS(2000); /* wait 2 seconds for session to expire */
  41879. }
  41880. /* set expired session in the WOLFSSL struct for use */
  41881. static void test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set(WOLFSSL* ssl)
  41882. {
  41883. XSLEEP_MS(1200); /* wait a second for session to expire */
  41884. /* set the expired session, call to set session fails but continuing on
  41885. after failure should be handled here */
  41886. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL)
  41887. AssertIntEQ(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  41888. WOLFSSL_SUCCESS);
  41889. #else
  41890. AssertIntNE(wolfSSL_set_session(ssl, test_wolfSSL_SESSION_expire_sess),
  41891. WOLFSSL_SUCCESS);
  41892. #endif
  41893. }
  41894. /* check that the expired session was not reused */
  41895. static void test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse(WOLFSSL* ssl)
  41896. {
  41897. /* since the session has expired it should not have been reused */
  41898. AssertIntEQ(wolfSSL_session_reused(ssl), 0);
  41899. }
  41900. #endif
  41901. static int test_wolfSSL_SESSION_expire_downgrade(void)
  41902. {
  41903. EXPECT_DECLS;
  41904. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  41905. !defined(NO_RSA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  41906. !defined(NO_SESSION_CACHE) && defined(OPENSSL_EXTRA) && \
  41907. !defined(WOLFSSL_NO_TLS12)
  41908. WOLFSSL_CTX* ctx = NULL;
  41909. callback_functions server_cbf, client_cbf;
  41910. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  41911. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  41912. /* force server side to use TLS 1.2 */
  41913. server_cbf.ctx = ctx;
  41914. server_cbf.method = wolfTLSv1_2_server_method;
  41915. client_cbf.method = wolfSSLv23_client_method;
  41916. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  41917. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready;
  41918. client_cbf.on_result = test_wolfSSL_SESSION_expire_downgrade_ssl_result;
  41919. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  41920. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  41921. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  41922. /* set the previously created session and wait till expired */
  41923. server_cbf.ctx = ctx;
  41924. client_cbf.method = wolfSSLv23_client_method;
  41925. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  41926. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_wait;
  41927. client_cbf.on_result =
  41928. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  41929. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  41930. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  41931. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  41932. /* set the previously created expired session */
  41933. server_cbf.ctx = ctx;
  41934. client_cbf.method = wolfSSLv23_client_method;
  41935. server_cbf.ctx_ready = test_wolfSSL_SESSION_expire_downgrade_ctx_ready;
  41936. client_cbf.ssl_ready = test_wolfSSL_SESSION_expire_downgrade_ssl_ready_set;
  41937. client_cbf.on_result =
  41938. test_wolfSSL_SESSION_expire_downgrade_ssl_result_reuse;
  41939. test_wolfSSL_client_server_nofail(&client_cbf, &server_cbf);
  41940. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  41941. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  41942. wolfSSL_SESSION_free(test_wolfSSL_SESSION_expire_sess);
  41943. wolfSSL_CTX_free(ctx);
  41944. #endif
  41945. return EXPECT_RESULT();
  41946. }
  41947. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  41948. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  41949. static int clientSessRemCountMalloc = 0;
  41950. static int serverSessRemCountMalloc = 0;
  41951. static int clientSessRemCountFree = 0;
  41952. static int serverSessRemCountFree = 0;
  41953. static WOLFSSL_CTX* serverSessCtx = NULL;
  41954. static WOLFSSL_SESSION* serverSess = NULL;
  41955. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  41956. !defined(NO_SESSION_CACHE_REF)
  41957. static WOLFSSL_CTX* clientSessCtx = NULL;
  41958. static WOLFSSL_SESSION* clientSess = NULL;
  41959. #endif
  41960. static int serverSessRemIdx = 3;
  41961. static int sessRemCtx_Server = WOLFSSL_SERVER_END;
  41962. static int sessRemCtx_Client = WOLFSSL_CLIENT_END;
  41963. static void SessRemCtxCb(WOLFSSL_CTX *ctx, WOLFSSL_SESSION *sess)
  41964. {
  41965. int* side;
  41966. (void)ctx;
  41967. side = (int*)SSL_SESSION_get_ex_data(sess, serverSessRemIdx);
  41968. if (side != NULL) {
  41969. if (*side == WOLFSSL_CLIENT_END)
  41970. clientSessRemCountFree++;
  41971. else
  41972. serverSessRemCountFree++;
  41973. SSL_SESSION_set_ex_data(sess, serverSessRemIdx, NULL);
  41974. }
  41975. }
  41976. static int SessRemCtxSetupCb(WOLFSSL_CTX* ctx)
  41977. {
  41978. SSL_CTX_sess_set_remove_cb(ctx, SessRemCtxCb);
  41979. #if defined(WOLFSSL_TLS13) && !defined(HAVE_SESSION_TICKET) && \
  41980. !defined(NO_SESSION_CACHE_REF)
  41981. {
  41982. EXPECT_DECLS;
  41983. /* Allow downgrade, set min version, and disable TLS 1.3.
  41984. * Do this because without NO_SESSION_CACHE_REF we will want to return a
  41985. * reference to the session cache. But with WOLFSSL_TLS13 and without
  41986. * HAVE_SESSION_TICKET we won't have a session ID to be able to place
  41987. * the session in the cache. In this case we need to downgrade to
  41988. * previous versions to just use the legacy session ID field. */
  41989. ExpectIntEQ(SSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  41990. SSL_SUCCESS);
  41991. ExpectIntEQ(SSL_CTX_set_max_proto_version(ctx, TLS1_2_VERSION),
  41992. SSL_SUCCESS);
  41993. return EXPECT_RESULT();
  41994. }
  41995. #else
  41996. return TEST_SUCCESS;
  41997. #endif
  41998. }
  41999. static int SessRemSslSetupCb(WOLFSSL* ssl)
  42000. {
  42001. EXPECT_DECLS;
  42002. int* side;
  42003. if (SSL_is_server(ssl)) {
  42004. side = &sessRemCtx_Server;
  42005. serverSessRemCountMalloc++;
  42006. ExpectNotNull(serverSess = SSL_get1_session(ssl));
  42007. ExpectIntEQ(SSL_CTX_up_ref(serverSessCtx = SSL_get_SSL_CTX(ssl)),
  42008. SSL_SUCCESS);
  42009. }
  42010. else {
  42011. side = &sessRemCtx_Client;
  42012. clientSessRemCountMalloc++;
  42013. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  42014. !defined(NO_SESSION_CACHE_REF)
  42015. ExpectNotNull(clientSess = SSL_get1_session(ssl));
  42016. ExpectIntEQ(SSL_CTX_up_ref(clientSessCtx = SSL_get_SSL_CTX(ssl)),
  42017. SSL_SUCCESS);
  42018. #endif
  42019. }
  42020. ExpectIntEQ(SSL_SESSION_set_ex_data(SSL_get_session(ssl),
  42021. serverSessRemIdx, side), SSL_SUCCESS);
  42022. return EXPECT_RESULT();
  42023. }
  42024. #endif
  42025. static int test_wolfSSL_CTX_sess_set_remove_cb(void)
  42026. {
  42027. EXPECT_DECLS;
  42028. #if defined(OPENSSL_EXTRA) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  42029. defined(HAVE_EX_DATA) && !defined(NO_SESSION_CACHE)
  42030. /* Check that the remove callback gets called for external data in a
  42031. * session object */
  42032. test_ssl_cbf func_cb;
  42033. XMEMSET(&func_cb, 0, sizeof(func_cb));
  42034. func_cb.ctx_ready = SessRemCtxSetupCb;
  42035. func_cb.on_result = SessRemSslSetupCb;
  42036. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb, &func_cb,
  42037. NULL), TEST_SUCCESS);
  42038. /* Both should have been allocated */
  42039. ExpectIntEQ(clientSessRemCountMalloc, 1);
  42040. ExpectIntEQ(serverSessRemCountMalloc, 1);
  42041. /* This should not be called yet. Session wasn't evicted from cache yet. */
  42042. ExpectIntEQ(clientSessRemCountFree, 0);
  42043. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  42044. !defined(NO_SESSION_CACHE_REF)
  42045. /* Force a cache lookup */
  42046. ExpectNotNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  42047. /* Force a cache update */
  42048. ExpectNotNull(SSL_SESSION_set_ex_data(clientSess, serverSessRemIdx - 1, 0));
  42049. /* This should set the timeout to 0 and call the remove callback from within
  42050. * the session cache. */
  42051. ExpectIntEQ(SSL_CTX_remove_session(clientSessCtx, clientSess), 0);
  42052. ExpectNull(SSL_SESSION_get_ex_data(clientSess, serverSessRemIdx));
  42053. ExpectIntEQ(clientSessRemCountFree, 1);
  42054. #endif
  42055. /* Server session is in the cache so ex_data isn't free'd with the SSL
  42056. * object */
  42057. ExpectIntEQ(serverSessRemCountFree, 0);
  42058. /* Force a cache lookup */
  42059. ExpectNotNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  42060. /* Force a cache update */
  42061. ExpectNotNull(SSL_SESSION_set_ex_data(serverSess, serverSessRemIdx - 1, 0));
  42062. /* This should set the timeout to 0 and call the remove callback from within
  42063. * the session cache. */
  42064. ExpectIntEQ(SSL_CTX_remove_session(serverSessCtx, serverSess), 0);
  42065. ExpectNull(SSL_SESSION_get_ex_data(serverSess, serverSessRemIdx));
  42066. ExpectIntEQ(serverSessRemCountFree, 1);
  42067. /* Need to free the references that we kept */
  42068. SSL_CTX_free(serverSessCtx);
  42069. SSL_SESSION_free(serverSess);
  42070. #if (defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET)) || \
  42071. !defined(NO_SESSION_CACHE_REF)
  42072. SSL_CTX_free(clientSessCtx);
  42073. SSL_SESSION_free(clientSess);
  42074. #endif
  42075. #endif
  42076. return EXPECT_RESULT();
  42077. }
  42078. static int test_wolfSSL_ticket_keys(void)
  42079. {
  42080. EXPECT_DECLS;
  42081. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  42082. !defined(NO_WOLFSSL_SERVER)
  42083. WOLFSSL_CTX* ctx = NULL;
  42084. byte keys[WOLFSSL_TICKET_KEYS_SZ];
  42085. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  42086. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, 0),
  42087. WOLFSSL_FAILURE);
  42088. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, 0),
  42089. WOLFSSL_FAILURE);
  42090. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, 0),
  42091. WOLFSSL_FAILURE);
  42092. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, 0),
  42093. WOLFSSL_FAILURE);
  42094. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  42095. WOLFSSL_FAILURE);
  42096. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  42097. WOLFSSL_FAILURE);
  42098. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  42099. WOLFSSL_FAILURE);
  42100. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, 0),
  42101. WOLFSSL_FAILURE);
  42102. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, 0),
  42103. WOLFSSL_FAILURE);
  42104. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, 0),
  42105. WOLFSSL_FAILURE);
  42106. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, 0),
  42107. WOLFSSL_FAILURE);
  42108. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, NULL, sizeof(keys)),
  42109. WOLFSSL_FAILURE);
  42110. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, NULL, sizeof(keys)),
  42111. WOLFSSL_FAILURE);
  42112. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(NULL, keys, sizeof(keys)),
  42113. WOLFSSL_FAILURE);
  42114. ExpectIntEQ(wolfSSL_CTX_get_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  42115. WOLFSSL_SUCCESS);
  42116. ExpectIntEQ(wolfSSL_CTX_set_tlsext_ticket_keys(ctx, keys, sizeof(keys)),
  42117. WOLFSSL_SUCCESS);
  42118. wolfSSL_CTX_free(ctx);
  42119. #endif
  42120. return EXPECT_RESULT();
  42121. }
  42122. #ifndef NO_BIO
  42123. static int test_wolfSSL_d2i_PUBKEY(void)
  42124. {
  42125. EXPECT_DECLS;
  42126. #if defined(OPENSSL_EXTRA)
  42127. BIO* bio = NULL;
  42128. EVP_PKEY* pkey = NULL;
  42129. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  42130. ExpectNull(d2i_PUBKEY_bio(NULL, NULL));
  42131. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  42132. /* RSA PUBKEY test */
  42133. ExpectIntGT(BIO_write(bio, client_keypub_der_2048,
  42134. sizeof_client_keypub_der_2048), 0);
  42135. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  42136. EVP_PKEY_free(pkey);
  42137. pkey = NULL;
  42138. #endif
  42139. #if defined(USE_CERT_BUFFERS_256) && defined(HAVE_ECC)
  42140. /* ECC PUBKEY test */
  42141. ExpectIntGT(BIO_write(bio, ecc_clikeypub_der_256,
  42142. sizeof_ecc_clikeypub_der_256), 0);
  42143. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  42144. EVP_PKEY_free(pkey);
  42145. pkey = NULL;
  42146. #endif
  42147. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DSA)
  42148. /* DSA PUBKEY test */
  42149. ExpectIntGT(BIO_write(bio, dsa_pub_key_der_2048,
  42150. sizeof_dsa_pub_key_der_2048), 0);
  42151. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  42152. EVP_PKEY_free(pkey);
  42153. pkey = NULL;
  42154. #endif
  42155. #if defined(USE_CERT_BUFFERS_2048) && !defined(NO_DH) && \
  42156. defined(OPENSSL_EXTRA) && defined(WOLFSSL_DH_EXTRA)
  42157. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  42158. (HAVE_FIPS_VERSION > 2))
  42159. /* DH PUBKEY test */
  42160. ExpectIntGT(BIO_write(bio, dh_pub_key_der_2048,
  42161. sizeof_dh_pub_key_der_2048), 0);
  42162. ExpectNotNull(pkey = d2i_PUBKEY_bio(bio, NULL));
  42163. EVP_PKEY_free(pkey);
  42164. pkey = NULL;
  42165. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  42166. #endif /* USE_CERT_BUFFERS_2048 && !NO_DH && && OPENSSL_EXTRA */
  42167. BIO_free(bio);
  42168. (void)pkey;
  42169. #endif
  42170. return EXPECT_RESULT();
  42171. }
  42172. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  42173. static int test_wolfSSL_d2i_PrivateKeys_bio(void)
  42174. {
  42175. EXPECT_DECLS;
  42176. BIO* bio = NULL;
  42177. EVP_PKEY* pkey = NULL;
  42178. WOLFSSL_CTX* ctx = NULL;
  42179. #if defined(WOLFSSL_KEY_GEN)
  42180. unsigned char buff[4096];
  42181. unsigned char* bufPtr = buff;
  42182. #endif
  42183. /* test creating new EVP_PKEY with bad arg */
  42184. ExpectNull((pkey = d2i_PrivateKey_bio(NULL, NULL)));
  42185. /* test loading RSA key using BIO */
  42186. #if !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  42187. {
  42188. XFILE file = XBADFILE;
  42189. const char* fname = "./certs/server-key.der";
  42190. size_t sz = 0;
  42191. byte* buf = NULL;
  42192. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  42193. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  42194. ExpectTrue((sz = XFTELL(file)) != 0);
  42195. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  42196. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  42197. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  42198. if (file != XBADFILE) {
  42199. XFCLOSE(file);
  42200. }
  42201. /* Test using BIO new mem and loading DER private key */
  42202. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  42203. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  42204. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  42205. BIO_free(bio);
  42206. bio = NULL;
  42207. EVP_PKEY_free(pkey);
  42208. pkey = NULL;
  42209. }
  42210. #endif
  42211. /* test loading ECC key using BIO */
  42212. #if defined(HAVE_ECC) && !defined(NO_FILESYSTEM)
  42213. {
  42214. XFILE file = XBADFILE;
  42215. const char* fname = "./certs/ecc-key.der";
  42216. size_t sz = 0;
  42217. byte* buf = NULL;
  42218. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  42219. ExpectTrue(XFSEEK(file, 0, XSEEK_END) == 0);
  42220. ExpectTrue((sz = XFTELL(file)) != 0);
  42221. ExpectTrue(XFSEEK(file, 0, XSEEK_SET) == 0);
  42222. ExpectNotNull(buf = (byte*)XMALLOC(sz, HEAP_HINT, DYNAMIC_TYPE_FILE));
  42223. ExpectIntEQ(XFREAD(buf, 1, sz, file), sz);
  42224. if (file != XBADFILE)
  42225. XFCLOSE(file);
  42226. /* Test using BIO new mem and loading DER private key */
  42227. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  42228. ExpectNotNull((pkey = d2i_PrivateKey_bio(bio, NULL)));
  42229. XFREE(buf, HEAP_HINT, DYNAMIC_TYPE_FILE);
  42230. BIO_free(bio);
  42231. bio = NULL;
  42232. EVP_PKEY_free(pkey);
  42233. pkey = NULL;
  42234. }
  42235. #endif
  42236. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  42237. #ifndef NO_WOLFSSL_SERVER
  42238. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  42239. #else
  42240. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_client_method()));
  42241. #endif
  42242. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA)
  42243. {
  42244. RSA* rsa = NULL;
  42245. /* Tests bad parameters */
  42246. ExpectNull(d2i_RSAPrivateKey_bio(NULL, NULL));
  42247. /* RSA not set yet, expecting to fail*/
  42248. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), BAD_FUNC_ARG);
  42249. #if defined(USE_CERT_BUFFERS_2048) && defined(WOLFSSL_KEY_GEN)
  42250. /* set RSA using bio*/
  42251. ExpectIntGT(BIO_write(bio, client_key_der_2048,
  42252. sizeof_client_key_der_2048), 0);
  42253. ExpectNotNull(d2i_RSAPrivateKey_bio(bio, &rsa));
  42254. ExpectNotNull(rsa);
  42255. ExpectIntEQ(SSL_CTX_use_RSAPrivateKey(ctx, rsa), WOLFSSL_SUCCESS);
  42256. /* i2d RSAprivate key tests */
  42257. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(NULL, NULL), BAD_FUNC_ARG);
  42258. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 1192);
  42259. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  42260. sizeof_client_key_der_2048);
  42261. bufPtr -= sizeof_client_key_der_2048;
  42262. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  42263. sizeof_client_key_der_2048), 0);
  42264. bufPtr = NULL;
  42265. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, &bufPtr),
  42266. sizeof_client_key_der_2048);
  42267. ExpectNotNull(bufPtr);
  42268. ExpectIntEQ(XMEMCMP(bufPtr, client_key_der_2048,
  42269. sizeof_client_key_der_2048), 0);
  42270. XFREE(bufPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  42271. RSA_free(rsa);
  42272. rsa = RSA_new();
  42273. ExpectIntEQ(wolfSSL_i2d_RSAPrivateKey(rsa, NULL), 0);
  42274. #endif /* USE_CERT_BUFFERS_2048 WOLFSSL_KEY_GEN */
  42275. RSA_free(rsa);
  42276. }
  42277. #endif /* WOLFSSL_KEY_GEN && !NO_RSA */
  42278. SSL_CTX_free(ctx);
  42279. ctx = NULL;
  42280. BIO_free(bio);
  42281. bio = NULL;
  42282. return EXPECT_RESULT();
  42283. }
  42284. #endif /* OPENSSL_ALL || (WOLFSSL_ASIO && !NO_RSA) */
  42285. #endif /* !NO_BIO */
  42286. static int test_wolfSSL_sk_GENERAL_NAME(void)
  42287. {
  42288. EXPECT_DECLS;
  42289. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  42290. !defined(NO_RSA)
  42291. X509* x509 = NULL;
  42292. GENERAL_NAME* gn = NULL;
  42293. unsigned char buf[4096];
  42294. const unsigned char* bufPt = NULL;
  42295. int bytes = 0;
  42296. int i;
  42297. int j;
  42298. XFILE f = XBADFILE;
  42299. STACK_OF(GENERAL_NAME)* sk = NULL;
  42300. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  42301. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  42302. if (f != XBADFILE)
  42303. XFCLOSE(f);
  42304. for (j = 0; j < 2; ++j) {
  42305. bufPt = buf;
  42306. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  42307. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  42308. NID_subject_alt_name, NULL, NULL));
  42309. ExpectIntEQ(sk_GENERAL_NAME_num(sk), 1);
  42310. for (i = 0; i < sk_GENERAL_NAME_num(sk); i++) {
  42311. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, i));
  42312. if (gn != NULL) {
  42313. switch (gn->type) {
  42314. case GEN_DNS:
  42315. fprintf(stderr, "found type GEN_DNS\n");
  42316. break;
  42317. case GEN_EMAIL:
  42318. fprintf(stderr, "found type GEN_EMAIL\n");
  42319. break;
  42320. case GEN_URI:
  42321. fprintf(stderr, "found type GEN_URI\n");
  42322. break;
  42323. }
  42324. }
  42325. }
  42326. X509_free(x509);
  42327. x509 = NULL;
  42328. if (j == 0) {
  42329. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  42330. }
  42331. else {
  42332. /*
  42333. * We had a bug where GENERAL_NAMES_free didn't free all the memory
  42334. * it was supposed to. This is a regression test for that bug.
  42335. */
  42336. GENERAL_NAMES_free(sk);
  42337. }
  42338. sk = NULL;
  42339. }
  42340. #endif
  42341. return EXPECT_RESULT();
  42342. }
  42343. static int test_wolfSSL_GENERAL_NAME_print(void)
  42344. {
  42345. EXPECT_DECLS;
  42346. #if defined(OPENSSL_ALL) && !defined(NO_BIO) && !defined(NO_RSA)
  42347. X509* x509 = NULL;
  42348. GENERAL_NAME* gn = NULL;
  42349. unsigned char buf[4096];
  42350. const unsigned char* bufPt = NULL;
  42351. int bytes;
  42352. XFILE f = XBADFILE;
  42353. STACK_OF(GENERAL_NAME)* sk = NULL;
  42354. BIO* out = NULL;
  42355. unsigned char outbuf[128];
  42356. X509_EXTENSION* ext = NULL;
  42357. AUTHORITY_INFO_ACCESS* aia = NULL;
  42358. ACCESS_DESCRIPTION* ad = NULL;
  42359. ASN1_IA5STRING *dnsname = NULL;
  42360. const unsigned char v4Addr[] = {192,168,53,1};
  42361. const unsigned char v6Addr[] =
  42362. {0x20, 0x21, 0x0d, 0xb8, 0x00, 0x00, 0x00, 0x00,
  42363. 0x00, 0x00, 0xff, 0x00, 0x00, 0x42, 0x77, 0x77};
  42364. const unsigned char email[] =
  42365. {'i', 'n', 'f', 'o', '@', 'w', 'o', 'l',
  42366. 'f', 's', 's', 'l', '.', 'c', 'o', 'm'};
  42367. const char* dnsStr = "DNS:example.com";
  42368. const char* uriStr = "URI:http://127.0.0.1:22220";
  42369. const char* v4addStr = "IP Address:192.168.53.1";
  42370. const char* v6addStr = "IP Address:2021:DB8:0:0:0:FF00:42:7777";
  42371. const char* emailStr = "email:info@wolfssl.com";
  42372. const char* othrStr = "othername:<unsupported>";
  42373. const char* x400Str = "X400Name:<unsupported>";
  42374. const char* ediStr = "EdiPartyName:<unsupported>";
  42375. /* BIO to output */
  42376. ExpectNotNull(out = BIO_new(BIO_s_mem()));
  42377. /* test for NULL param */
  42378. gn = NULL;
  42379. ExpectIntEQ(GENERAL_NAME_print(NULL, NULL), 0);
  42380. ExpectIntEQ(GENERAL_NAME_print(NULL, gn), 0);
  42381. ExpectIntEQ(GENERAL_NAME_print(out, NULL), 0);
  42382. /* test for GEN_DNS */
  42383. ExpectTrue((f = XFOPEN(cliCertDerFileExt, "rb")) != XBADFILE);
  42384. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  42385. if (f != XBADFILE) {
  42386. XFCLOSE(f);
  42387. f = XBADFILE;
  42388. }
  42389. bufPt = buf;
  42390. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  42391. ExpectNotNull(sk = (STACK_OF(ASN1_OBJECT)*)X509_get_ext_d2i(x509,
  42392. NID_subject_alt_name, NULL, NULL));
  42393. ExpectNotNull(gn = sk_GENERAL_NAME_value(sk, 0));
  42394. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42395. XMEMSET(outbuf, 0, sizeof(outbuf));
  42396. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42397. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  42398. sk_GENERAL_NAME_pop_free(sk, GENERAL_NAME_free);
  42399. gn = NULL;
  42400. sk = NULL;
  42401. X509_free(x509);
  42402. x509 = NULL;
  42403. /* Lets test for setting as well. */
  42404. ExpectNotNull(gn = GENERAL_NAME_new());
  42405. ExpectNotNull(dnsname = ASN1_IA5STRING_new());
  42406. ExpectIntEQ(ASN1_STRING_set(dnsname, "example.com", -1), 1);
  42407. GENERAL_NAME_set0_value(gn, GEN_DNS, dnsname);
  42408. dnsname = NULL;
  42409. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42410. XMEMSET(outbuf, 0, sizeof(outbuf));
  42411. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42412. ExpectIntEQ(XSTRNCMP((const char*)outbuf, dnsStr, XSTRLEN(dnsStr)), 0);
  42413. GENERAL_NAME_free(gn);
  42414. /* test for GEN_URI */
  42415. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  42416. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  42417. if (f != XBADFILE) {
  42418. XFCLOSE(f);
  42419. f = XBADFILE;
  42420. }
  42421. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 4));
  42422. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  42423. ext));
  42424. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION *)wolfSSL_sk_value(aia, 0));
  42425. if (ad != NULL) {
  42426. gn = ad->location;
  42427. }
  42428. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42429. gn = NULL;
  42430. XMEMSET(outbuf,0,sizeof(outbuf));
  42431. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42432. ExpectIntEQ(XSTRNCMP((const char*)outbuf, uriStr, XSTRLEN(uriStr)), 0);
  42433. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  42434. aia = NULL;
  42435. aia = (AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(ext);
  42436. ExpectNotNull(aia);
  42437. AUTHORITY_INFO_ACCESS_pop_free(aia, NULL);
  42438. aia = NULL;
  42439. X509_free(x509);
  42440. x509 = NULL;
  42441. /* test for GEN_IPADD */
  42442. /* ip v4 address */
  42443. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42444. if (gn != NULL) {
  42445. gn->type = GEN_IPADD;
  42446. if (gn->d.iPAddress != NULL) {
  42447. gn->d.iPAddress->length = sizeof(v4Addr);
  42448. }
  42449. }
  42450. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v4Addr,
  42451. sizeof(v4Addr)), 1);
  42452. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42453. XMEMSET(outbuf,0,sizeof(outbuf));
  42454. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42455. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v4addStr, XSTRLEN(v4addStr)), 0);
  42456. GENERAL_NAME_free(gn);
  42457. gn = NULL;
  42458. /* ip v6 address */
  42459. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42460. if (gn != NULL) {
  42461. gn->type = GEN_IPADD;
  42462. if (gn->d.iPAddress != NULL) {
  42463. gn->d.iPAddress->length = sizeof(v6Addr);
  42464. }
  42465. }
  42466. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.iPAddress, v6Addr,
  42467. sizeof(v6Addr)), 1);
  42468. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42469. XMEMSET(outbuf,0,sizeof(outbuf));
  42470. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42471. ExpectIntEQ(XSTRNCMP((const char*)outbuf, v6addStr, XSTRLEN(v6addStr)), 0);
  42472. GENERAL_NAME_free(gn);
  42473. gn = NULL;
  42474. /* test for GEN_EMAIL */
  42475. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42476. if (gn != NULL) {
  42477. gn->type = GEN_EMAIL;
  42478. if (gn->d.rfc822Name != NULL) {
  42479. gn->d.rfc822Name->length = sizeof(email);
  42480. }
  42481. }
  42482. ExpectIntEQ(wolfSSL_ASN1_STRING_set(gn->d.rfc822Name, email, sizeof(email)),
  42483. 1);
  42484. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42485. XMEMSET(outbuf,0,sizeof(outbuf));
  42486. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42487. ExpectIntEQ(XSTRNCMP((const char*)outbuf, emailStr, XSTRLEN(emailStr)), 0);
  42488. GENERAL_NAME_free(gn);
  42489. gn = NULL;
  42490. /* test for GEN_OTHERNAME */
  42491. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42492. if (gn != NULL) {
  42493. gn->type = GEN_OTHERNAME;
  42494. }
  42495. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42496. XMEMSET(outbuf,0,sizeof(outbuf));
  42497. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42498. ExpectIntEQ(XSTRNCMP((const char*)outbuf, othrStr, XSTRLEN(othrStr)), 0);
  42499. GENERAL_NAME_free(gn);
  42500. gn = NULL;
  42501. /* test for GEN_X400 */
  42502. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42503. if (gn != NULL) {
  42504. gn->type = GEN_X400;
  42505. }
  42506. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42507. XMEMSET(outbuf,0,sizeof(outbuf));
  42508. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42509. ExpectIntEQ(XSTRNCMP((const char*)outbuf, x400Str, XSTRLEN(x400Str)), 0);
  42510. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  42511. if (gn != NULL) {
  42512. gn->type = GEN_IA5;
  42513. }
  42514. GENERAL_NAME_free(gn);
  42515. gn = NULL;
  42516. /* test for GEN_EDIPARTY */
  42517. ExpectNotNull(gn = wolfSSL_GENERAL_NAME_new());
  42518. if (gn != NULL) {
  42519. gn->type = GEN_EDIPARTY;
  42520. }
  42521. ExpectIntEQ(GENERAL_NAME_print(out, gn), 1);
  42522. XMEMSET(outbuf,0,sizeof(outbuf));
  42523. ExpectIntGT(BIO_read(out, outbuf, sizeof(outbuf)), 0);
  42524. ExpectIntEQ(XSTRNCMP((const char*)outbuf, ediStr, XSTRLEN(ediStr)), 0);
  42525. /* Restore to GEN_IA5 (default) to avoid memory leak. */
  42526. if (gn != NULL) {
  42527. gn->type = GEN_IA5;
  42528. }
  42529. GENERAL_NAME_free(gn);
  42530. gn = NULL;
  42531. BIO_free(out);
  42532. #endif /* OPENSSL_ALL */
  42533. return EXPECT_RESULT();
  42534. }
  42535. static int test_wolfSSL_sk_DIST_POINT(void)
  42536. {
  42537. EXPECT_DECLS;
  42538. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && \
  42539. !defined(NO_RSA)
  42540. X509* x509 = NULL;
  42541. unsigned char buf[4096];
  42542. const unsigned char* bufPt;
  42543. int bytes = 0;
  42544. int i = 0;
  42545. int j = 0;
  42546. XFILE f = XBADFILE;
  42547. DIST_POINT* dp = NULL;
  42548. DIST_POINT_NAME* dpn = NULL;
  42549. GENERAL_NAME* gn = NULL;
  42550. ASN1_IA5STRING* uri = NULL;
  42551. STACK_OF(DIST_POINT)* dps = NULL;
  42552. STACK_OF(GENERAL_NAME)* gns = NULL;
  42553. const char cliCertDerCrlDistPoint[] = "./certs/client-crl-dist.der";
  42554. ExpectTrue((f = XFOPEN(cliCertDerCrlDistPoint, "rb")) != XBADFILE);
  42555. ExpectIntGT((bytes = (int)XFREAD(buf, 1, sizeof(buf), f)), 0);
  42556. if (f != XBADFILE)
  42557. XFCLOSE(f);
  42558. bufPt = buf;
  42559. ExpectNotNull(x509 = d2i_X509(NULL, &bufPt, bytes));
  42560. ExpectNotNull(dps = (STACK_OF(DIST_POINT)*)X509_get_ext_d2i(x509,
  42561. NID_crl_distribution_points, NULL, NULL));
  42562. ExpectIntEQ(sk_DIST_POINT_num(dps), 1);
  42563. for (i = 0; i < sk_DIST_POINT_num(dps); i++) {
  42564. ExpectNotNull(dp = sk_DIST_POINT_value(dps, i));
  42565. ExpectNotNull(dpn = dp->distpoint);
  42566. /* this should be type 0, fullname */
  42567. ExpectIntEQ(dpn->type, 0);
  42568. ExpectNotNull(gns = dp->distpoint->name.fullname);
  42569. ExpectIntEQ(sk_GENERAL_NAME_num(gns), 1);
  42570. for (j = 0; j < sk_GENERAL_NAME_num(gns); j++) {
  42571. ExpectNotNull(gn = sk_GENERAL_NAME_value(gns, j));
  42572. ExpectIntEQ(gn->type, GEN_URI);
  42573. ExpectNotNull(uri = gn->d.uniformResourceIdentifier);
  42574. ExpectNotNull(uri->data);
  42575. ExpectIntGT(uri->length, 0);
  42576. }
  42577. }
  42578. X509_free(x509);
  42579. CRL_DIST_POINTS_free(dps);
  42580. #endif
  42581. return EXPECT_RESULT();
  42582. }
  42583. static int test_wolfSSL_verify_mode(void)
  42584. {
  42585. EXPECT_DECLS;
  42586. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  42587. WOLFSSL* ssl = NULL;
  42588. WOLFSSL_CTX* ctx = NULL;
  42589. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42590. ExpectNotNull(ssl = SSL_new(ctx));
  42591. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  42592. SSL_free(ssl);
  42593. ssl = NULL;
  42594. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  42595. ExpectNotNull(ssl = SSL_new(ctx));
  42596. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  42597. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  42598. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  42599. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx), SSL_VERIFY_PEER);
  42600. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  42601. SSL_free(ssl);
  42602. ssl = NULL;
  42603. wolfSSL_CTX_set_verify(ctx,
  42604. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  42605. ExpectNotNull(ssl = SSL_new(ctx));
  42606. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_CTX_get_verify_mode(ctx));
  42607. ExpectIntEQ(SSL_get_verify_mode(ssl),
  42608. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  42609. wolfSSL_set_verify(ssl, SSL_VERIFY_PEER, 0);
  42610. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  42611. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  42612. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_PEER);
  42613. wolfSSL_set_verify(ssl, SSL_VERIFY_NONE, 0);
  42614. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_NONE);
  42615. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  42616. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  42617. wolfSSL_set_verify(ssl, SSL_VERIFY_FAIL_EXCEPT_PSK, 0);
  42618. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_FAIL_EXCEPT_PSK);
  42619. #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  42620. wolfSSL_set_verify(ssl, SSL_VERIFY_POST_HANDSHAKE, 0);
  42621. ExpectIntEQ(SSL_get_verify_mode(ssl), SSL_VERIFY_POST_HANDSHAKE);
  42622. #endif
  42623. ExpectIntEQ(SSL_CTX_get_verify_mode(ctx),
  42624. WOLFSSL_VERIFY_PEER | WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT);
  42625. SSL_free(ssl);
  42626. SSL_CTX_free(ctx);
  42627. #endif
  42628. return EXPECT_RESULT();
  42629. }
  42630. static int test_wolfSSL_verify_depth(void)
  42631. {
  42632. EXPECT_DECLS;
  42633. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  42634. WOLFSSL* ssl = NULL;
  42635. WOLFSSL_CTX* ctx = NULL;
  42636. long depth;
  42637. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42638. ExpectIntGT((depth = SSL_CTX_get_verify_depth(ctx)), 0);
  42639. ExpectNotNull(ssl = SSL_new(ctx));
  42640. ExpectIntEQ(SSL_get_verify_depth(ssl), SSL_CTX_get_verify_depth(ctx));
  42641. SSL_free(ssl);
  42642. ssl = NULL;
  42643. SSL_CTX_set_verify_depth(ctx, -1);
  42644. ExpectIntEQ(depth, SSL_CTX_get_verify_depth(ctx));
  42645. SSL_CTX_set_verify_depth(ctx, 2);
  42646. ExpectIntEQ(2, SSL_CTX_get_verify_depth(ctx));
  42647. ExpectNotNull(ssl = SSL_new(ctx));
  42648. ExpectIntEQ(2, SSL_get_verify_depth(ssl));
  42649. SSL_free(ssl);
  42650. SSL_CTX_free(ctx);
  42651. #endif
  42652. return EXPECT_RESULT();
  42653. }
  42654. static int test_wolfSSL_verify_result(void)
  42655. {
  42656. EXPECT_DECLS;
  42657. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  42658. defined(OPENSSL_ALL)) && !defined(NO_WOLFSSL_CLIENT)
  42659. WOLFSSL* ssl = NULL;
  42660. WOLFSSL_CTX* ctx = NULL;
  42661. long result = 0xDEADBEEF;
  42662. ExpectIntEQ(WOLFSSL_FAILURE, wolfSSL_get_verify_result(ssl));
  42663. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42664. ExpectNotNull(ssl = SSL_new(ctx));
  42665. wolfSSL_set_verify_result(ssl, result);
  42666. ExpectIntEQ(result, wolfSSL_get_verify_result(ssl));
  42667. SSL_free(ssl);
  42668. SSL_CTX_free(ctx);
  42669. #endif
  42670. return EXPECT_RESULT();
  42671. }
  42672. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  42673. static void sslMsgCb(int w, int version, int type, const void* buf,
  42674. size_t sz, SSL* ssl, void* arg)
  42675. {
  42676. int i;
  42677. unsigned char* pt = (unsigned char*)buf;
  42678. fprintf(stderr, "%s %d bytes of version %d , type %d : ",
  42679. (w)?"Writing":"Reading", (int)sz, version, type);
  42680. for (i = 0; i < (int)sz; i++) fprintf(stderr, "%02X", pt[i]);
  42681. fprintf(stderr, "\n");
  42682. (void)ssl;
  42683. (void)arg;
  42684. }
  42685. #endif /* OPENSSL_EXTRA */
  42686. static int test_wolfSSL_msg_callback(void)
  42687. {
  42688. EXPECT_DECLS;
  42689. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_CLIENT)
  42690. WOLFSSL* ssl = NULL;
  42691. WOLFSSL_CTX* ctx = NULL;
  42692. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  42693. ExpectNotNull(ssl = SSL_new(ctx));
  42694. ExpectIntEQ(SSL_set_msg_callback(ssl, NULL), SSL_SUCCESS);
  42695. ExpectIntEQ(SSL_set_msg_callback(ssl, &sslMsgCb), SSL_SUCCESS);
  42696. ExpectIntEQ(SSL_set_msg_callback(NULL, &sslMsgCb), SSL_FAILURE);
  42697. SSL_free(ssl);
  42698. SSL_CTX_free(ctx);
  42699. #endif
  42700. return EXPECT_RESULT();
  42701. }
  42702. /* test_EVP_Cipher_extra, Extra-test on EVP_CipherUpdate/Final. see also test.c */
  42703. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  42704. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  42705. static void binary_dump(void *ptr, int size)
  42706. {
  42707. #ifdef WOLFSSL_EVP_PRINT
  42708. int i = 0;
  42709. unsigned char *p = (unsigned char *) ptr;
  42710. fprintf(stderr, "{");
  42711. while ((p != NULL) && (i < size)) {
  42712. if ((i % 8) == 0) {
  42713. fprintf(stderr, "\n");
  42714. fprintf(stderr, " ");
  42715. }
  42716. fprintf(stderr, "0x%02x, ", p[i]);
  42717. i++;
  42718. }
  42719. fprintf(stderr, "\n};\n");
  42720. #else
  42721. (void) ptr;
  42722. (void) size;
  42723. #endif
  42724. }
  42725. static int last_val = 0x0f;
  42726. static int check_result(unsigned char *data, int len)
  42727. {
  42728. int i;
  42729. for ( ; len; ) {
  42730. last_val = (last_val + 1) % 16;
  42731. for (i = 0; i < 16; len--, i++, data++)
  42732. if (*data != last_val) {
  42733. return -1;
  42734. }
  42735. }
  42736. return 0;
  42737. }
  42738. static int r_offset;
  42739. static int w_offset;
  42740. static void init_offset(void)
  42741. {
  42742. r_offset = 0;
  42743. w_offset = 0;
  42744. }
  42745. static void get_record(unsigned char *data, unsigned char *buf, int len)
  42746. {
  42747. XMEMCPY(buf, data+r_offset, len);
  42748. r_offset += len;
  42749. }
  42750. static void set_record(unsigned char *data, unsigned char *buf, int len)
  42751. {
  42752. XMEMCPY(data+w_offset, buf, len);
  42753. w_offset += len;
  42754. }
  42755. static void set_plain(unsigned char *plain, int rec)
  42756. {
  42757. int i, j;
  42758. unsigned char *p = plain;
  42759. #define BLOCKSZ 16
  42760. for (i=0; i<(rec/BLOCKSZ); i++) {
  42761. for (j=0; j<BLOCKSZ; j++)
  42762. *p++ = (i % 16);
  42763. }
  42764. }
  42765. #endif
  42766. static int test_wolfSSL_EVP_Cipher_extra(void)
  42767. {
  42768. EXPECT_DECLS;
  42769. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) &&\
  42770. (!defined(NO_AES) && defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128))
  42771. /* aes128-cbc, keylen=16, ivlen=16 */
  42772. byte aes128_cbc_key[] = {
  42773. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  42774. 0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef,
  42775. };
  42776. byte aes128_cbc_iv[] = {
  42777. 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88,
  42778. 0x99, 0x00, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff,
  42779. };
  42780. /* teset data size table */
  42781. int test_drive1[] = {8, 3, 5, 512, 8, 3, 8, 512, 0};
  42782. int test_drive2[] = {8, 3, 8, 512, 0};
  42783. int test_drive3[] = {512, 512, 504, 512, 512, 8, 512, 0};
  42784. int *test_drive[] = {test_drive1, test_drive2, test_drive3, NULL};
  42785. int test_drive_len[100];
  42786. int ret = 0;
  42787. EVP_CIPHER_CTX *evp = NULL;
  42788. int ilen = 0;
  42789. int klen = 0;
  42790. int i, j;
  42791. const EVP_CIPHER *type;
  42792. byte *iv;
  42793. byte *key;
  42794. int ivlen;
  42795. int keylen;
  42796. #define RECORDS 16
  42797. #define BUFFSZ 512
  42798. byte plain [BUFFSZ * RECORDS];
  42799. byte cipher[BUFFSZ * RECORDS];
  42800. byte inb[BUFFSZ];
  42801. byte outb[BUFFSZ+16];
  42802. int outl = 0;
  42803. int inl;
  42804. iv = aes128_cbc_iv;
  42805. ivlen = sizeof(aes128_cbc_iv);
  42806. key = aes128_cbc_key;
  42807. keylen = sizeof(aes128_cbc_key);
  42808. type = EVP_aes_128_cbc();
  42809. set_plain(plain, BUFFSZ * RECORDS);
  42810. SSL_library_init();
  42811. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  42812. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  42813. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  42814. klen = EVP_CIPHER_CTX_key_length(evp);
  42815. if (klen > 0 && keylen != klen) {
  42816. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  42817. }
  42818. ilen = EVP_CIPHER_CTX_iv_length(evp);
  42819. if (ilen > 0 && ivlen != ilen) {
  42820. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  42821. }
  42822. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  42823. for (j = 0; j<RECORDS; j++)
  42824. {
  42825. inl = BUFFSZ;
  42826. get_record(plain, inb, inl);
  42827. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, inb, inl)), 0);
  42828. set_record(cipher, outb, outl);
  42829. }
  42830. for (i = 0; test_drive[i]; i++) {
  42831. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  42832. init_offset();
  42833. test_drive_len[i] = 0;
  42834. for (j = 0; test_drive[i][j]; j++)
  42835. {
  42836. inl = test_drive[i][j];
  42837. test_drive_len[i] += inl;
  42838. get_record(plain, inb, inl);
  42839. ExpectIntNE((ret = EVP_EncryptUpdate(evp, outb, &outl, inb, inl)),
  42840. 0);
  42841. /* output to cipher buffer, so that following Dec test can detect
  42842. if any error */
  42843. set_record(cipher, outb, outl);
  42844. }
  42845. EVP_CipherFinal(evp, outb, &outl);
  42846. if (outl > 0)
  42847. set_record(cipher, outb, outl);
  42848. }
  42849. for (i = 0; test_drive[i]; i++) {
  42850. last_val = 0x0f;
  42851. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 0)), 0);
  42852. init_offset();
  42853. for (j = 0; test_drive[i][j]; j++) {
  42854. inl = test_drive[i][j];
  42855. get_record(cipher, inb, inl);
  42856. ExpectIntNE((ret = EVP_DecryptUpdate(evp, outb, &outl, inb, inl)),
  42857. 0);
  42858. binary_dump(outb, outl);
  42859. ExpectIntEQ((ret = check_result(outb, outl)), 0);
  42860. ExpectFalse(outl > ((inl/16+1)*16) && outl > 16);
  42861. }
  42862. ret = EVP_CipherFinal(evp, outb, &outl);
  42863. binary_dump(outb, outl);
  42864. ret = (((test_drive_len[i] % 16) != 0) && (ret == 0)) ||
  42865. (((test_drive_len[i] % 16) == 0) && (ret == 1));
  42866. ExpectTrue(ret);
  42867. }
  42868. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(evp), WOLFSSL_SUCCESS);
  42869. EVP_CIPHER_CTX_free(evp);
  42870. evp = NULL;
  42871. /* Do an extra test to verify correct behavior with empty input. */
  42872. ExpectNotNull(evp = EVP_CIPHER_CTX_new());
  42873. ExpectIntNE((ret = EVP_CipherInit(evp, type, NULL, iv, 0)), 0);
  42874. ExpectIntEQ(EVP_CIPHER_CTX_nid(evp), NID_aes_128_cbc);
  42875. klen = EVP_CIPHER_CTX_key_length(evp);
  42876. if (klen > 0 && keylen != klen) {
  42877. ExpectIntNE(EVP_CIPHER_CTX_set_key_length(evp, keylen), 0);
  42878. }
  42879. ilen = EVP_CIPHER_CTX_iv_length(evp);
  42880. if (ilen > 0 && ivlen != ilen) {
  42881. ExpectIntNE(EVP_CIPHER_CTX_set_iv_length(evp, ivlen), 0);
  42882. }
  42883. ExpectIntNE((ret = EVP_CipherInit(evp, NULL, key, iv, 1)), 0);
  42884. /* outl should be set to 0 after passing NULL, 0 for input args. */
  42885. outl = -1;
  42886. ExpectIntNE((ret = EVP_CipherUpdate(evp, outb, &outl, NULL, 0)), 0);
  42887. ExpectIntEQ(outl, 0);
  42888. EVP_CIPHER_CTX_free(evp);
  42889. #endif /* test_EVP_Cipher */
  42890. return EXPECT_RESULT();
  42891. }
  42892. static int test_wolfSSL_PEM_read_DHparams(void)
  42893. {
  42894. EXPECT_DECLS;
  42895. #if defined(OPENSSL_ALL) && !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && \
  42896. !defined(NO_FILESYSTEM)
  42897. DH* dh = NULL;
  42898. XFILE fp = XBADFILE;
  42899. unsigned char derOut[300];
  42900. unsigned char* derOutBuf = derOut;
  42901. int derOutSz = 0;
  42902. unsigned char derExpected[300];
  42903. int derExpectedSz = 0;
  42904. XMEMSET(derOut, 0, sizeof(derOut));
  42905. XMEMSET(derExpected, 0, sizeof(derExpected));
  42906. /* open DH param file, read into DH struct */
  42907. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  42908. /* bad args */
  42909. ExpectNull(dh = PEM_read_DHparams(NULL, &dh, NULL, NULL));
  42910. ExpectNull(dh = PEM_read_DHparams(NULL, NULL, NULL, NULL));
  42911. /* good args */
  42912. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  42913. if (fp != XBADFILE) {
  42914. XFCLOSE(fp);
  42915. fp = XBADFILE;
  42916. }
  42917. /* read in certs/dh2048.der for comparison against exported params */
  42918. ExpectTrue((fp = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  42919. ExpectIntGT(derExpectedSz = (int)XFREAD(derExpected, 1, sizeof(derExpected),
  42920. fp), 0);
  42921. if (fp != XBADFILE) {
  42922. XFCLOSE(fp);
  42923. fp = XBADFILE;
  42924. }
  42925. /* export DH back to DER and compare */
  42926. derOutSz = wolfSSL_i2d_DHparams(dh, &derOutBuf);
  42927. ExpectIntEQ(derOutSz, derExpectedSz);
  42928. ExpectIntEQ(XMEMCMP(derOut, derExpected, derOutSz), 0);
  42929. DH_free(dh);
  42930. dh = NULL;
  42931. /* Test parsing with X9.42 header */
  42932. ExpectTrue((fp = XFOPEN("./certs/x942dh2048.pem", "rb")) != XBADFILE);
  42933. ExpectNotNull(dh = PEM_read_DHparams(fp, &dh, NULL, NULL));
  42934. if (fp != XBADFILE)
  42935. XFCLOSE(fp);
  42936. DH_free(dh);
  42937. dh = NULL;
  42938. #endif
  42939. return EXPECT_RESULT();
  42940. }
  42941. static int test_wolfSSL_X509_get_serialNumber(void)
  42942. {
  42943. EXPECT_DECLS;
  42944. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_RSA)
  42945. ASN1_INTEGER* a = NULL;
  42946. BIGNUM* bn = NULL;
  42947. X509* x509 = NULL;
  42948. char *serialHex = NULL;
  42949. byte serial[3];
  42950. int serialSz;
  42951. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  42952. SSL_FILETYPE_PEM));
  42953. ExpectNotNull(a = X509_get_serialNumber(x509));
  42954. /* check on value of ASN1 Integer */
  42955. ExpectNotNull(bn = ASN1_INTEGER_to_BN(a, NULL));
  42956. a = NULL;
  42957. /* test setting serial number and then retrieving it */
  42958. ExpectNotNull(a = ASN1_INTEGER_new());
  42959. ExpectIntEQ(ASN1_INTEGER_set(a, 3), 1);
  42960. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  42961. serialSz = sizeof(serial);
  42962. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  42963. WOLFSSL_SUCCESS);
  42964. ExpectIntEQ(serialSz, 1);
  42965. ExpectIntEQ(serial[0], 3);
  42966. ASN1_INTEGER_free(a);
  42967. a = NULL;
  42968. /* test setting serial number with 0's in it */
  42969. serial[0] = 0x01;
  42970. serial[1] = 0x00;
  42971. serial[2] = 0x02;
  42972. ExpectNotNull(a = wolfSSL_ASN1_INTEGER_new());
  42973. if (a != NULL) {
  42974. a->data[0] = ASN_INTEGER;
  42975. a->data[1] = sizeof(serial);
  42976. XMEMCPY(&a->data[2], serial, sizeof(serial));
  42977. a->length = sizeof(serial) + 2;
  42978. }
  42979. ExpectIntEQ(X509_set_serialNumber(x509, a), WOLFSSL_SUCCESS);
  42980. XMEMSET(serial, 0, sizeof(serial));
  42981. serialSz = sizeof(serial);
  42982. ExpectIntEQ(wolfSSL_X509_get_serial_number(x509, serial, &serialSz),
  42983. WOLFSSL_SUCCESS);
  42984. ExpectIntEQ(serialSz, 3);
  42985. ExpectIntEQ(serial[0], 0x01);
  42986. ExpectIntEQ(serial[1], 0x00);
  42987. ExpectIntEQ(serial[2], 0x02);
  42988. ASN1_INTEGER_free(a);
  42989. a = NULL;
  42990. X509_free(x509); /* free's a */
  42991. ExpectNotNull(serialHex = BN_bn2hex(bn));
  42992. #ifndef WC_DISABLE_RADIX_ZERO_PAD
  42993. ExpectStrEQ(serialHex, "01");
  42994. #else
  42995. ExpectStrEQ(serialHex, "1");
  42996. #endif
  42997. OPENSSL_free(serialHex);
  42998. ExpectIntEQ(BN_get_word(bn), 1);
  42999. BN_free(bn);
  43000. /* hard test free'ing with dynamic buffer to make sure there is no leaks */
  43001. ExpectNotNull(a = ASN1_INTEGER_new());
  43002. if (a != NULL) {
  43003. ExpectNotNull(a->data = (unsigned char*)XMALLOC(100, NULL,
  43004. DYNAMIC_TYPE_OPENSSL));
  43005. a->isDynamic = 1;
  43006. ASN1_INTEGER_free(a);
  43007. }
  43008. #endif
  43009. return EXPECT_RESULT();
  43010. }
  43011. static int test_wolfSSL_OpenSSL_add_all_algorithms(void)
  43012. {
  43013. EXPECT_DECLS;
  43014. #if defined(OPENSSL_EXTRA)
  43015. ExpectIntEQ(wolfSSL_add_all_algorithms(), WOLFSSL_SUCCESS);
  43016. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_noconf(), WOLFSSL_SUCCESS);
  43017. ExpectIntEQ(wolfSSL_OpenSSL_add_all_algorithms_conf(), WOLFSSL_SUCCESS);
  43018. #endif
  43019. return EXPECT_RESULT();
  43020. }
  43021. static int test_wolfSSL_OPENSSL_hexstr2buf(void)
  43022. {
  43023. EXPECT_DECLS;
  43024. #if defined(OPENSSL_EXTRA)
  43025. #define MAX_HEXSTR_BUFSZ 9
  43026. #define NUM_CASES 5
  43027. struct Output {
  43028. const unsigned char buffer[MAX_HEXSTR_BUFSZ];
  43029. long ret;
  43030. };
  43031. int i;
  43032. int j;
  43033. const char* inputs[NUM_CASES] = {
  43034. "aabcd1357e",
  43035. "01:12:23:34:a5:b6:c7:d8:e9",
  43036. ":01:02",
  43037. "012",
  43038. ":ab:ac:d"
  43039. };
  43040. struct Output expectedOutputs[NUM_CASES] = {
  43041. {{0xaa, 0xbc, 0xd1, 0x35, 0x7e}, 5},
  43042. {{0x01, 0x12, 0x23, 0x34, 0xa5, 0xb6, 0xc7, 0xd8, 0xe9}, 9},
  43043. {{0x01, 0x02}, 2},
  43044. {{0x00}, 0},
  43045. {{0x00}, 0}
  43046. };
  43047. long len = 0;
  43048. unsigned char* returnedBuf = NULL;
  43049. for (i = 0; i < NUM_CASES && !EXPECT_FAIL(); ++i) {
  43050. returnedBuf = wolfSSL_OPENSSL_hexstr2buf(inputs[i], &len);
  43051. if (returnedBuf == NULL) {
  43052. ExpectIntEQ(expectedOutputs[i].ret, 0);
  43053. continue;
  43054. }
  43055. ExpectIntEQ(expectedOutputs[i].ret, len);
  43056. for (j = 0; j < len; ++j) {
  43057. ExpectIntEQ(expectedOutputs[i].buffer[j], returnedBuf[j]);
  43058. }
  43059. OPENSSL_free(returnedBuf);
  43060. returnedBuf = NULL;
  43061. }
  43062. #endif
  43063. return EXPECT_RESULT();
  43064. }
  43065. static int test_wolfSSL_X509_CA_num(void)
  43066. {
  43067. EXPECT_DECLS;
  43068. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && !defined(NO_FILESYSTEM) && \
  43069. defined(HAVE_ECC) && !defined(NO_RSA)
  43070. WOLFSSL_X509_STORE *store = NULL;
  43071. WOLFSSL_X509 *x509_1 = NULL;
  43072. WOLFSSL_X509 *x509_2 = NULL;
  43073. int ca_num = 0;
  43074. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  43075. ExpectNotNull(x509_1 = wolfSSL_X509_load_certificate_file(svrCertFile,
  43076. WOLFSSL_FILETYPE_PEM));
  43077. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_1), 1);
  43078. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 1);
  43079. ExpectNotNull(x509_2 = wolfSSL_X509_load_certificate_file(eccCertFile,
  43080. WOLFSSL_FILETYPE_PEM));
  43081. ExpectIntEQ(wolfSSL_X509_STORE_add_cert(store, x509_2), 1);
  43082. ExpectIntEQ(ca_num = wolfSSL_X509_CA_num(store), 2);
  43083. wolfSSL_X509_free(x509_1);
  43084. wolfSSL_X509_free(x509_2);
  43085. wolfSSL_X509_STORE_free(store);
  43086. #endif
  43087. return EXPECT_RESULT();
  43088. }
  43089. static int test_wolfSSL_X509_check_ca(void)
  43090. {
  43091. EXPECT_DECLS;
  43092. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  43093. WOLFSSL_X509 *x509 = NULL;
  43094. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  43095. WOLFSSL_FILETYPE_PEM));
  43096. ExpectIntEQ(wolfSSL_X509_check_ca(x509), 1);
  43097. wolfSSL_X509_free(x509);
  43098. #endif
  43099. return EXPECT_RESULT();
  43100. }
  43101. static int test_wolfSSL_X509_check_ip_asc(void)
  43102. {
  43103. EXPECT_DECLS;
  43104. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  43105. WOLFSSL_X509 *x509 = NULL;
  43106. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  43107. WOLFSSL_FILETYPE_PEM));
  43108. #if 0
  43109. /* TODO: add cert gen for testing positive case */
  43110. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "127.0.0.1", 0), 1);
  43111. #endif
  43112. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, "0.0.0.0", 0), 0);
  43113. ExpectIntEQ(wolfSSL_X509_check_ip_asc(x509, NULL, 0), 0);
  43114. wolfSSL_X509_free(x509);
  43115. #endif
  43116. return EXPECT_RESULT();
  43117. }
  43118. static int test_wolfSSL_make_cert(void)
  43119. {
  43120. EXPECT_DECLS;
  43121. #if !defined(NO_RSA) && !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && \
  43122. defined(WOLFSSL_CERT_EXT)
  43123. int ret = 0;
  43124. Cert cert;
  43125. CertName name;
  43126. RsaKey key;
  43127. WC_RNG rng;
  43128. byte der[FOURK_BUF];
  43129. word32 idx = 0;
  43130. const byte mySerial[8] = {1,2,3,4,5,6,7,8};
  43131. #ifdef OPENSSL_EXTRA
  43132. const unsigned char* pt = NULL;
  43133. int certSz = 0;
  43134. X509* x509 = NULL;
  43135. X509_NAME* x509name = NULL;
  43136. X509_NAME_ENTRY* entry = NULL;
  43137. ASN1_STRING* entryValue = NULL;
  43138. #endif
  43139. XMEMSET(&name, 0, sizeof(CertName));
  43140. /* set up cert name */
  43141. XMEMCPY(name.country, "US", sizeof("US"));
  43142. name.countryEnc = CTC_PRINTABLE;
  43143. XMEMCPY(name.state, "Oregon", sizeof("Oregon"));
  43144. name.stateEnc = CTC_UTF8;
  43145. XMEMCPY(name.locality, "Portland", sizeof("Portland"));
  43146. name.localityEnc = CTC_UTF8;
  43147. XMEMCPY(name.sur, "Test", sizeof("Test"));
  43148. name.surEnc = CTC_UTF8;
  43149. XMEMCPY(name.org, "wolfSSL", sizeof("wolfSSL"));
  43150. name.orgEnc = CTC_UTF8;
  43151. XMEMCPY(name.unit, "Development", sizeof("Development"));
  43152. name.unitEnc = CTC_UTF8;
  43153. XMEMCPY(name.commonName, "www.wolfssl.com", sizeof("www.wolfssl.com"));
  43154. name.commonNameEnc = CTC_UTF8;
  43155. XMEMCPY(name.serialDev, "wolfSSL12345", sizeof("wolfSSL12345"));
  43156. name.serialDevEnc = CTC_PRINTABLE;
  43157. XMEMCPY(name.userId, "TestUserID", sizeof("TestUserID"));
  43158. name.userIdEnc = CTC_PRINTABLE;
  43159. #ifdef WOLFSSL_MULTI_ATTRIB
  43160. #if CTC_MAX_ATTRIB > 2
  43161. {
  43162. NameAttrib* n;
  43163. n = &name.name[0];
  43164. n->id = ASN_DOMAIN_COMPONENT;
  43165. n->type = CTC_UTF8;
  43166. n->sz = sizeof("com");
  43167. XMEMCPY(n->value, "com", sizeof("com"));
  43168. n = &name.name[1];
  43169. n->id = ASN_DOMAIN_COMPONENT;
  43170. n->type = CTC_UTF8;
  43171. n->sz = sizeof("wolfssl");
  43172. XMEMCPY(n->value, "wolfssl", sizeof("wolfssl"));
  43173. }
  43174. #endif
  43175. #endif /* WOLFSSL_MULTI_ATTRIB */
  43176. ExpectIntEQ(wc_InitRsaKey(&key, HEAP_HINT), 0);
  43177. #ifndef HAVE_FIPS
  43178. ExpectIntEQ(wc_InitRng_ex(&rng, HEAP_HINT, testDevId), 0);
  43179. #else
  43180. ExpectIntEQ(wc_InitRng(&rng), 0);
  43181. #endif
  43182. /* load test RSA key */
  43183. idx = 0;
  43184. #if defined(USE_CERT_BUFFERS_1024)
  43185. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_1024, &idx, &key,
  43186. sizeof_server_key_der_1024), 0);
  43187. #elif defined(USE_CERT_BUFFERS_2048)
  43188. ExpectIntEQ(wc_RsaPrivateKeyDecode(server_key_der_2048, &idx, &key,
  43189. sizeof_server_key_der_2048), 0);
  43190. #else
  43191. /* error case, no RSA key loaded, happens later */
  43192. (void)idx;
  43193. #endif
  43194. XMEMSET(&cert, 0 , sizeof(Cert));
  43195. ExpectIntEQ(wc_InitCert(&cert), 0);
  43196. XMEMCPY(&cert.subject, &name, sizeof(CertName));
  43197. XMEMCPY(cert.serial, mySerial, sizeof(mySerial));
  43198. cert.serialSz = (int)sizeof(mySerial);
  43199. cert.isCA = 1;
  43200. #ifndef NO_SHA256
  43201. cert.sigType = CTC_SHA256wRSA;
  43202. #else
  43203. cert.sigType = CTC_SHAwRSA;
  43204. #endif
  43205. /* add SKID from the Public Key */
  43206. ExpectIntEQ(wc_SetSubjectKeyIdFromPublicKey(&cert, &key, NULL), 0);
  43207. /* add AKID from the Public Key */
  43208. ExpectIntEQ(wc_SetAuthKeyIdFromPublicKey(&cert, &key, NULL), 0);
  43209. ret = 0;
  43210. do {
  43211. #if defined(WOLFSSL_ASYNC_CRYPT)
  43212. ret = wc_AsyncWait(ret, &key.asyncDev, WC_ASYNC_FLAG_CALL_AGAIN);
  43213. #endif
  43214. if (ret >= 0) {
  43215. ret = wc_MakeSelfCert(&cert, der, FOURK_BUF, &key, &rng);
  43216. }
  43217. } while (ret == WC_PENDING_E);
  43218. ExpectIntGT(ret, 0);
  43219. #ifdef OPENSSL_EXTRA
  43220. /* der holds a certificate with DC's now check X509 parsing of it */
  43221. certSz = ret;
  43222. pt = der;
  43223. ExpectNotNull(x509 = d2i_X509(NULL, &pt, certSz));
  43224. ExpectNotNull(x509name = X509_get_subject_name(x509));
  43225. #ifdef WOLFSSL_MULTI_ATTRIB
  43226. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  43227. -1)), 5);
  43228. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  43229. (int)idx)), 6);
  43230. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  43231. (int)idx)), -1);
  43232. #endif /* WOLFSSL_MULTI_ATTRIB */
  43233. /* compare DN at index 0 */
  43234. ExpectNotNull(entry = X509_NAME_get_entry(x509name, 0));
  43235. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  43236. ExpectIntEQ(ASN1_STRING_length(entryValue), 2);
  43237. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "US");
  43238. #ifndef WOLFSSL_MULTI_ATTRIB
  43239. /* compare Serial Number */
  43240. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_serialNumber,
  43241. -1)), 7);
  43242. ExpectNotNull(entry = X509_NAME_get_entry(x509name, idx));
  43243. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  43244. ExpectIntEQ(ASN1_STRING_length(entryValue), XSTRLEN("wolfSSL12345"));
  43245. ExpectStrEQ((const char*)ASN1_STRING_data(entryValue), "wolfSSL12345");
  43246. #endif
  43247. #ifdef WOLFSSL_MULTI_ATTRIB
  43248. /* get first and second DC and compare result */
  43249. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  43250. -1)), 5);
  43251. ExpectNotNull(entry = X509_NAME_get_entry(x509name, (int)idx));
  43252. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  43253. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "com");
  43254. ExpectIntEQ((idx = X509_NAME_get_index_by_NID(x509name, NID_domainComponent,
  43255. (int)idx)), 6);
  43256. ExpectNotNull(entry = X509_NAME_get_entry(x509name, (int)idx));
  43257. ExpectNotNull(entryValue = X509_NAME_ENTRY_get_data(entry));
  43258. ExpectStrEQ((const char *)ASN1_STRING_data(entryValue), "wolfssl");
  43259. #endif /* WOLFSSL_MULTI_ATTRIB */
  43260. /* try invalid index locations for regression test and sanity check */
  43261. ExpectNull(entry = X509_NAME_get_entry(x509name, 11));
  43262. ExpectNull(entry = X509_NAME_get_entry(x509name, 20));
  43263. X509_free(x509);
  43264. #endif /* OPENSSL_EXTRA */
  43265. wc_FreeRsaKey(&key);
  43266. wc_FreeRng(&rng);
  43267. #endif
  43268. return EXPECT_RESULT();
  43269. }
  43270. static int test_wolfSSL_X509_get_version(void)
  43271. {
  43272. EXPECT_DECLS;
  43273. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  43274. WOLFSSL_X509 *x509 = NULL;
  43275. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  43276. WOLFSSL_FILETYPE_PEM));
  43277. ExpectIntEQ((int)wolfSSL_X509_get_version(x509), 2);
  43278. wolfSSL_X509_free(x509);
  43279. #endif
  43280. return EXPECT_RESULT();
  43281. }
  43282. #if defined(OPENSSL_ALL)
  43283. static int test_wolfSSL_sk_CIPHER_description(void)
  43284. {
  43285. EXPECT_DECLS;
  43286. #if !defined(NO_RSA)
  43287. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  43288. int i;
  43289. int numCiphers = 0;
  43290. const SSL_METHOD *method = NULL;
  43291. const SSL_CIPHER *cipher = NULL;
  43292. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  43293. SSL_CTX *ctx = NULL;
  43294. SSL *ssl = NULL;
  43295. char buf[256];
  43296. char test_str[9] = "0000000";
  43297. const char badStr[] = "unknown";
  43298. const char certPath[] = "./certs/client-cert.pem";
  43299. XMEMSET(buf, 0, sizeof(buf));
  43300. ExpectNotNull(method = TLSv1_2_client_method());
  43301. ExpectNotNull(ctx = SSL_CTX_new(method));
  43302. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  43303. SSL_CTX_set_verify_depth(ctx, 4);
  43304. SSL_CTX_set_options(ctx, flags);
  43305. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  43306. WOLFSSL_SUCCESS);
  43307. ExpectNotNull(ssl = SSL_new(ctx));
  43308. /* SSL_get_ciphers returns a stack of all configured ciphers
  43309. * A flag, getCipherAtOffset, is set to later have SSL_CIPHER_description
  43310. */
  43311. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  43312. /* loop through the amount of supportedCiphers */
  43313. numCiphers = sk_num(supportedCiphers);
  43314. for (i = 0; i < numCiphers; ++i) {
  43315. int j;
  43316. /* sk_value increments "sk->data.cipher->cipherOffset".
  43317. * wolfSSL_sk_CIPHER_description sets the description for
  43318. * the cipher based on the provided offset.
  43319. */
  43320. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  43321. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  43322. }
  43323. /* Search cipher description string for "unknown" descriptor */
  43324. for (j = 0; j < (int)XSTRLEN(buf); j++) {
  43325. int k = 0;
  43326. while ((k < (int)XSTRLEN(badStr)) && (buf[j] == badStr[k])) {
  43327. test_str[k] = badStr[k];
  43328. j++;
  43329. k++;
  43330. }
  43331. }
  43332. /* Fail if test_str == badStr == "unknown" */
  43333. ExpectStrNE(test_str,badStr);
  43334. }
  43335. SSL_free(ssl);
  43336. SSL_CTX_free(ctx);
  43337. #endif
  43338. return EXPECT_RESULT();
  43339. }
  43340. static int test_wolfSSL_get_ciphers_compat(void)
  43341. {
  43342. EXPECT_DECLS;
  43343. #if !defined(NO_RSA)
  43344. const SSL_METHOD *method = NULL;
  43345. const char certPath[] = "./certs/client-cert.pem";
  43346. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  43347. SSL_CTX *ctx = NULL;
  43348. WOLFSSL *ssl = NULL;
  43349. const long flags = SSL_OP_NO_SSLv2 | SSL_OP_NO_COMPRESSION;
  43350. ExpectNotNull(method = SSLv23_client_method());
  43351. ExpectNotNull(ctx = SSL_CTX_new(method));
  43352. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
  43353. SSL_CTX_set_verify_depth(ctx, 4);
  43354. SSL_CTX_set_options(ctx, flags);
  43355. ExpectIntEQ(SSL_CTX_load_verify_locations(ctx, certPath, NULL),
  43356. WOLFSSL_SUCCESS);
  43357. ExpectNotNull(ssl = SSL_new(ctx));
  43358. /* Test Bad NULL input */
  43359. ExpectNull(supportedCiphers = SSL_get_ciphers(NULL));
  43360. /* Test for Good input */
  43361. ExpectNotNull(supportedCiphers = SSL_get_ciphers(ssl));
  43362. /* Further usage of SSL_get_ciphers/wolfSSL_get_ciphers_compat is
  43363. * tested in test_wolfSSL_sk_CIPHER_description according to Qt usage */
  43364. SSL_free(ssl);
  43365. SSL_CTX_free(ctx);
  43366. #endif
  43367. return EXPECT_RESULT();
  43368. }
  43369. static int test_wolfSSL_X509_PUBKEY_get(void)
  43370. {
  43371. EXPECT_DECLS;
  43372. WOLFSSL_X509_PUBKEY pubkey;
  43373. WOLFSSL_X509_PUBKEY* key;
  43374. WOLFSSL_EVP_PKEY evpkey ;
  43375. WOLFSSL_EVP_PKEY* evpPkey;
  43376. WOLFSSL_EVP_PKEY* retEvpPkey;
  43377. XMEMSET(&pubkey, 0, sizeof(WOLFSSL_X509_PUBKEY));
  43378. XMEMSET(&evpkey, 0, sizeof(WOLFSSL_EVP_PKEY));
  43379. key = &pubkey;
  43380. evpPkey = &evpkey;
  43381. evpPkey->type = WOLFSSL_SUCCESS;
  43382. key->pkey = evpPkey;
  43383. ExpectNotNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  43384. ExpectIntEQ(retEvpPkey->type, WOLFSSL_SUCCESS);
  43385. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(NULL));
  43386. key->pkey = NULL;
  43387. ExpectNull(retEvpPkey = wolfSSL_X509_PUBKEY_get(key));
  43388. return EXPECT_RESULT();
  43389. }
  43390. static int test_wolfSSL_EVP_PKEY_set1_get1_DSA(void)
  43391. {
  43392. EXPECT_DECLS;
  43393. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  43394. DSA *dsa = NULL;
  43395. DSA *setDsa = NULL;
  43396. EVP_PKEY *pkey = NULL;
  43397. EVP_PKEY *set1Pkey = NULL;
  43398. SHA_CTX sha;
  43399. byte signature[DSA_SIG_SIZE];
  43400. byte hash[WC_SHA_DIGEST_SIZE];
  43401. word32 bytes;
  43402. int answer;
  43403. #ifdef USE_CERT_BUFFERS_1024
  43404. const unsigned char* dsaKeyDer = dsa_key_der_1024;
  43405. int dsaKeySz = sizeof_dsa_key_der_1024;
  43406. byte tmp[ONEK_BUF];
  43407. XMEMSET(tmp, 0, sizeof(tmp));
  43408. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  43409. bytes = dsaKeySz;
  43410. #elif defined(USE_CERT_BUFFERS_2048)
  43411. const unsigned char* dsaKeyDer = dsa_key_der_2048;
  43412. int dsaKeySz = sizeof_dsa_key_der_2048;
  43413. byte tmp[TWOK_BUF];
  43414. XMEMSET(tmp, 0, sizeof(tmp));
  43415. XMEMCPY(tmp, dsaKeyDer , dsaKeySz);
  43416. bytes = (word32)dsaKeySz;
  43417. #else
  43418. byte tmp[TWOK_BUF];
  43419. const unsigned char* dsaKeyDer = (const unsigned char*)tmp;
  43420. int dsaKeySz;
  43421. XFILE fp = XBADFILE;
  43422. XMEMSET(tmp, 0, sizeof(tmp));
  43423. ExpectTrue((fp = XFOPEN("./certs/dsa2048.der", "rb")) != XBADFILE);
  43424. ExpectIntGT(dsaKeySz = bytes = (word32) XFREAD(tmp, 1, sizeof(tmp), fp), 0);
  43425. if (fp != XBADFILE)
  43426. XFCLOSE(fp);
  43427. #endif /* END USE_CERT_BUFFERS_1024 */
  43428. /* Create hash to later Sign and Verify */
  43429. ExpectIntEQ(SHA1_Init(&sha), WOLFSSL_SUCCESS);
  43430. ExpectIntEQ(SHA1_Update(&sha, tmp, bytes), WOLFSSL_SUCCESS);
  43431. ExpectIntEQ(SHA1_Final(hash,&sha), WOLFSSL_SUCCESS);
  43432. /* Initialize pkey with der format dsa key */
  43433. ExpectNotNull(d2i_PrivateKey(EVP_PKEY_DSA, &pkey, &dsaKeyDer,
  43434. (long)dsaKeySz));
  43435. /* Test wolfSSL_EVP_PKEY_get1_DSA */
  43436. /* Should Fail: NULL argument */
  43437. ExpectNull(dsa = EVP_PKEY_get0_DSA(NULL));
  43438. ExpectNull(dsa = EVP_PKEY_get1_DSA(NULL));
  43439. /* Should Pass: Initialized pkey argument */
  43440. ExpectNotNull(dsa = EVP_PKEY_get0_DSA(pkey));
  43441. ExpectNotNull(dsa = EVP_PKEY_get1_DSA(pkey));
  43442. #ifdef USE_CERT_BUFFERS_1024
  43443. ExpectIntEQ(DSA_bits(dsa), 1024);
  43444. #else
  43445. ExpectIntEQ(DSA_bits(dsa), 2048);
  43446. #endif
  43447. /* Sign */
  43448. ExpectIntEQ(wolfSSL_DSA_do_sign(hash, signature, dsa), WOLFSSL_SUCCESS);
  43449. /* Verify. */
  43450. ExpectIntEQ(wolfSSL_DSA_do_verify(hash, signature, dsa, &answer),
  43451. WOLFSSL_SUCCESS);
  43452. /* Test wolfSSL_EVP_PKEY_set1_DSA */
  43453. /* Should Fail: set1Pkey not initialized */
  43454. ExpectIntNE(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  43455. /* Initialize set1Pkey */
  43456. set1Pkey = EVP_PKEY_new();
  43457. /* Should Fail Verify: setDsa not initialized from set1Pkey */
  43458. ExpectIntNE(wolfSSL_DSA_do_verify(hash,signature,setDsa,&answer),
  43459. WOLFSSL_SUCCESS);
  43460. /* Should Pass: set dsa into set1Pkey */
  43461. ExpectIntEQ(EVP_PKEY_set1_DSA(set1Pkey, dsa), WOLFSSL_SUCCESS);
  43462. DSA_free(dsa);
  43463. DSA_free(setDsa);
  43464. EVP_PKEY_free(pkey);
  43465. EVP_PKEY_free(set1Pkey);
  43466. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  43467. return EXPECT_RESULT();
  43468. } /* END test_EVP_PKEY_set1_get1_DSA */
  43469. static int test_wolfSSL_DSA_generate_parameters(void)
  43470. {
  43471. EXPECT_DECLS;
  43472. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  43473. !defined(HAVE_FIPS)
  43474. DSA *dsa = NULL;
  43475. ExpectNotNull(dsa = DSA_generate_parameters(2048, NULL, 0, NULL, NULL, NULL,
  43476. NULL));
  43477. DSA_free(dsa);
  43478. #endif
  43479. return EXPECT_RESULT();
  43480. }
  43481. static int test_wolfSSL_DSA_SIG(void)
  43482. {
  43483. EXPECT_DECLS;
  43484. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN) && \
  43485. !defined(HAVE_FIPS)
  43486. DSA *dsa = NULL;
  43487. DSA *dsa2 = NULL;
  43488. DSA_SIG *sig = NULL;
  43489. const BIGNUM *p = NULL;
  43490. const BIGNUM *q = NULL;
  43491. const BIGNUM *g = NULL;
  43492. const BIGNUM *pub = NULL;
  43493. const BIGNUM *priv = NULL;
  43494. BIGNUM *dup_p = NULL;
  43495. BIGNUM *dup_q = NULL;
  43496. BIGNUM *dup_g = NULL;
  43497. BIGNUM *dup_pub = NULL;
  43498. BIGNUM *dup_priv = NULL;
  43499. const byte digest[WC_SHA_DIGEST_SIZE] = {0};
  43500. ExpectNotNull(dsa = DSA_new());
  43501. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048, NULL, 0, NULL, NULL,
  43502. NULL), 1);
  43503. ExpectIntEQ(DSA_generate_key(dsa), 1);
  43504. DSA_get0_pqg(dsa, &p, &q, &g);
  43505. DSA_get0_key(dsa, &pub, &priv);
  43506. ExpectNotNull(dup_p = BN_dup(p));
  43507. ExpectNotNull(dup_q = BN_dup(q));
  43508. ExpectNotNull(dup_g = BN_dup(g));
  43509. ExpectNotNull(dup_pub = BN_dup(pub));
  43510. ExpectNotNull(dup_priv = BN_dup(priv));
  43511. ExpectNotNull(sig = DSA_do_sign(digest, sizeof(digest), dsa));
  43512. ExpectNotNull(dsa2 = DSA_new());
  43513. ExpectIntEQ(DSA_set0_pqg(dsa2, dup_p, dup_q, dup_g), 1);
  43514. if (EXPECT_FAIL()) {
  43515. BN_free(dup_p);
  43516. BN_free(dup_q);
  43517. BN_free(dup_g);
  43518. }
  43519. ExpectIntEQ(DSA_set0_key(dsa2, dup_pub, dup_priv), 1);
  43520. if (EXPECT_FAIL()) {
  43521. BN_free(dup_pub);
  43522. BN_free(dup_priv);
  43523. }
  43524. ExpectIntEQ(DSA_do_verify(digest, sizeof(digest), sig, dsa2), 1);
  43525. DSA_free(dsa);
  43526. DSA_free(dsa2);
  43527. DSA_SIG_free(sig);
  43528. #endif
  43529. return EXPECT_RESULT();
  43530. }
  43531. static int test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY (void)
  43532. {
  43533. EXPECT_DECLS;
  43534. #ifdef HAVE_ECC
  43535. WOLFSSL_EC_KEY* ecKey = NULL;
  43536. WOLFSSL_EC_KEY* ecGet1 = NULL;
  43537. EVP_PKEY* pkey = NULL;
  43538. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  43539. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43540. /* Test wolfSSL_EVP_PKEY_set1_EC_KEY */
  43541. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  43542. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  43543. /* Should fail since ecKey is empty */
  43544. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  43545. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  43546. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  43547. /* Test wolfSSL_EVP_PKEY_get1_EC_KEY */
  43548. ExpectNull(wolfSSL_EVP_PKEY_get1_EC_KEY(NULL));
  43549. ExpectNotNull(ecGet1 = wolfSSL_EVP_PKEY_get1_EC_KEY(pkey));
  43550. wolfSSL_EC_KEY_free(ecKey);
  43551. wolfSSL_EC_KEY_free(ecGet1);
  43552. EVP_PKEY_free(pkey);
  43553. #endif /* HAVE_ECC */
  43554. return EXPECT_RESULT();
  43555. } /* END test_EVP_PKEY_set1_get1_EC_KEY */
  43556. static int test_wolfSSL_EVP_PKEY_set1_get1_DH (void)
  43557. {
  43558. EXPECT_DECLS;
  43559. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  43560. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  43561. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  43562. DH *dh = NULL;
  43563. DH *setDh = NULL;
  43564. EVP_PKEY *pkey = NULL;
  43565. XFILE f = XBADFILE;
  43566. unsigned char buf[4096];
  43567. const unsigned char* pt = buf;
  43568. const char* dh2048 = "./certs/dh2048.der";
  43569. long len = 0;
  43570. int code = -1;
  43571. XMEMSET(buf, 0, sizeof(buf));
  43572. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  43573. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  43574. if (f != XBADFILE)
  43575. XFCLOSE(f);
  43576. /* Load dh2048.der into DH with internal format */
  43577. ExpectNotNull(setDh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  43578. ExpectIntEQ(wolfSSL_DH_check(setDh, &code), WOLFSSL_SUCCESS);
  43579. ExpectIntEQ(code, 0);
  43580. code = -1;
  43581. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43582. /* Set DH into PKEY */
  43583. ExpectIntEQ(wolfSSL_EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  43584. /* Get DH from PKEY */
  43585. ExpectNotNull(dh = wolfSSL_EVP_PKEY_get1_DH(pkey));
  43586. ExpectIntEQ(wolfSSL_DH_check(dh, &code), WOLFSSL_SUCCESS);
  43587. ExpectIntEQ(code, 0);
  43588. EVP_PKEY_free(pkey);
  43589. DH_free(setDh);
  43590. setDh = NULL;
  43591. DH_free(dh);
  43592. dh = NULL;
  43593. #endif /* !NO_DH && WOLFSSL_DH_EXTRA && !NO_FILESYSTEM */
  43594. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  43595. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  43596. return EXPECT_RESULT();
  43597. } /* END test_EVP_PKEY_set1_get1_DH */
  43598. static int test_wolfSSL_CTX_ctrl(void)
  43599. {
  43600. EXPECT_DECLS;
  43601. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  43602. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  43603. char caFile[] = "./certs/client-ca.pem";
  43604. char clientFile[] = "./certs/client-cert.pem";
  43605. SSL_CTX* ctx = NULL;
  43606. X509* x509 = NULL;
  43607. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  43608. byte buf[6000];
  43609. char file[] = "./certs/dsaparams.pem";
  43610. XFILE f = XBADFILE;
  43611. int bytes = 0;
  43612. BIO* bio = NULL;
  43613. DSA* dsa = NULL;
  43614. DH* dh = NULL;
  43615. #endif
  43616. #ifdef HAVE_ECC
  43617. WOLFSSL_EC_KEY* ecKey = NULL;
  43618. #endif
  43619. ExpectNotNull(ctx = SSL_CTX_new(wolfSSLv23_server_method()));
  43620. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(caFile,
  43621. WOLFSSL_FILETYPE_PEM));
  43622. ExpectIntEQ((int)SSL_CTX_add_extra_chain_cert(ctx, x509), WOLFSSL_SUCCESS);
  43623. if (EXPECT_FAIL()) {
  43624. wolfSSL_X509_free(x509);
  43625. }
  43626. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(clientFile,
  43627. WOLFSSL_FILETYPE_PEM));
  43628. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  43629. /* Initialize DH */
  43630. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  43631. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  43632. if (f != XBADFILE)
  43633. XFCLOSE(f);
  43634. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  43635. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  43636. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  43637. #endif
  43638. #ifdef HAVE_ECC
  43639. /* Initialize WOLFSSL_EC_KEY */
  43640. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  43641. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  43642. #endif
  43643. /* additional test of getting EVP_PKEY key size from X509
  43644. * Do not run with user RSA because wolfSSL_RSA_size is not currently
  43645. * allowed with user RSA */
  43646. {
  43647. EVP_PKEY* pkey = NULL;
  43648. #if defined(HAVE_ECC)
  43649. X509* ecX509 = NULL;
  43650. #endif /* HAVE_ECC */
  43651. ExpectNotNull(pkey = X509_get_pubkey(x509));
  43652. /* current RSA key is 2048 bit (256 bytes) */
  43653. ExpectIntEQ(EVP_PKEY_size(pkey), 256);
  43654. EVP_PKEY_free(pkey);
  43655. pkey = NULL;
  43656. #if defined(HAVE_ECC)
  43657. #if defined(USE_CERT_BUFFERS_256)
  43658. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_buffer(
  43659. cliecc_cert_der_256, sizeof_cliecc_cert_der_256,
  43660. SSL_FILETYPE_ASN1));
  43661. #else
  43662. ExpectNotNull(ecX509 = wolfSSL_X509_load_certificate_file(
  43663. cliEccCertFile, SSL_FILETYPE_PEM));
  43664. #endif
  43665. ExpectNotNull(pkey = X509_get_pubkey(ecX509));
  43666. /* current ECC key is 256 bit (32 bytes) */
  43667. ExpectIntEQ(EVP_PKEY_size(pkey), 32);
  43668. X509_free(ecX509);
  43669. EVP_PKEY_free(pkey);
  43670. #endif /* HAVE_ECC */
  43671. }
  43672. /* Tests should fail with passed in NULL pointer */
  43673. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, NULL),
  43674. SSL_FAILURE);
  43675. #if !defined(NO_DH) && !defined(NO_DSA)
  43676. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, NULL),
  43677. SSL_FAILURE);
  43678. #endif
  43679. #ifdef HAVE_ECC
  43680. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, NULL),
  43681. SSL_FAILURE);
  43682. #endif
  43683. /* Test with SSL_CTRL_EXTRA_CHAIN_CERT
  43684. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_add_extra_chain_cert
  43685. */
  43686. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_EXTRA_CHAIN_CERT, 0, x509),
  43687. SSL_SUCCESS);
  43688. if (EXPECT_FAIL()) {
  43689. wolfSSL_X509_free(x509);
  43690. }
  43691. /* Test with SSL_CTRL_OPTIONS
  43692. * wolfSSL_CTX_ctrl should succesffuly call SSL_CTX_set_options
  43693. */
  43694. ExpectTrue(wolfSSL_CTX_ctrl(ctx, SSL_CTRL_OPTIONS, SSL_OP_NO_TLSv1,
  43695. NULL) == SSL_OP_NO_TLSv1);
  43696. ExpectTrue(SSL_CTX_get_options(ctx) == SSL_OP_NO_TLSv1);
  43697. /* Test with SSL_CTRL_SET_TMP_DH
  43698. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_dh
  43699. */
  43700. #if !defined(NO_DH) && !defined(NO_DSA) && !defined(NO_BIO)
  43701. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_DH, 0, dh),
  43702. SSL_SUCCESS);
  43703. #endif
  43704. /* Test with SSL_CTRL_SET_TMP_ECDH
  43705. * wolfSSL_CTX_ctrl should succesffuly call wolfSSL_SSL_CTX_set_tmp_ecdh
  43706. */
  43707. #ifdef HAVE_ECC
  43708. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_TMP_ECDH, 0, ecKey),
  43709. SSL_SUCCESS);
  43710. #endif
  43711. #ifdef WOLFSSL_ENCRYPTED_KEYS
  43712. ExpectNull(SSL_CTX_get_default_passwd_cb(ctx));
  43713. ExpectNull(SSL_CTX_get_default_passwd_cb_userdata(ctx));
  43714. #endif
  43715. /* Test for min/max proto */
  43716. #ifndef WOLFSSL_NO_TLS12
  43717. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  43718. 0, NULL), SSL_SUCCESS);
  43719. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MIN_PROTO_VERSION,
  43720. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  43721. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  43722. #endif
  43723. #ifdef WOLFSSL_TLS13
  43724. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  43725. 0, NULL), SSL_SUCCESS);
  43726. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  43727. TLS1_3_VERSION, NULL), SSL_SUCCESS);
  43728. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_3_VERSION);
  43729. #ifndef WOLFSSL_NO_TLS12
  43730. ExpectIntEQ((int)wolfSSL_CTX_ctrl(ctx, SSL_CTRL_SET_MAX_PROTO_VERSION,
  43731. TLS1_2_VERSION, NULL), SSL_SUCCESS);
  43732. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx), TLS1_2_VERSION);
  43733. #endif
  43734. #endif
  43735. /* Cleanup and Pass */
  43736. #if !defined(NO_DH) && !defined(NO_DSA)
  43737. #ifndef NO_BIO
  43738. BIO_free(bio);
  43739. DSA_free(dsa);
  43740. DH_free(dh);
  43741. dh = NULL;
  43742. #endif
  43743. #endif
  43744. #ifdef HAVE_ECC
  43745. wolfSSL_EC_KEY_free(ecKey);
  43746. #endif
  43747. SSL_CTX_free(ctx);
  43748. #endif /* defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  43749. * !defined(NO_FILESYSTEM) && !defined(NO_RSA) */
  43750. return EXPECT_RESULT();
  43751. }
  43752. static int test_wolfSSL_EVP_PKEY_assign(void)
  43753. {
  43754. EXPECT_DECLS;
  43755. #if !defined(NO_RSA) || !defined(NO_DSA) || defined(HAVE_ECC)
  43756. int type;
  43757. WOLFSSL_EVP_PKEY* pkey = NULL;
  43758. #ifndef NO_RSA
  43759. WOLFSSL_RSA* rsa = NULL;
  43760. #endif
  43761. #ifndef NO_DSA
  43762. WOLFSSL_DSA* dsa = NULL;
  43763. #endif
  43764. #ifdef HAVE_ECC
  43765. WOLFSSL_EC_KEY* ecKey = NULL;
  43766. #endif
  43767. #ifndef NO_RSA
  43768. type = EVP_PKEY_RSA;
  43769. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43770. ExpectNotNull(rsa = wolfSSL_RSA_new());
  43771. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, rsa), WOLFSSL_FAILURE);
  43772. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  43773. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, rsa), WOLFSSL_FAILURE);
  43774. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, rsa), WOLFSSL_SUCCESS);
  43775. if (EXPECT_FAIL()) {
  43776. wolfSSL_RSA_free(rsa);
  43777. }
  43778. wolfSSL_EVP_PKEY_free(pkey);
  43779. pkey = NULL;
  43780. #endif /* NO_RSA */
  43781. #ifndef NO_DSA
  43782. type = EVP_PKEY_DSA;
  43783. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43784. ExpectNotNull(dsa = wolfSSL_DSA_new());
  43785. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, dsa), WOLFSSL_FAILURE);
  43786. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  43787. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, dsa), WOLFSSL_FAILURE);
  43788. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, dsa), WOLFSSL_SUCCESS);
  43789. if (EXPECT_FAIL()) {
  43790. wolfSSL_DSA_free(dsa);
  43791. }
  43792. wolfSSL_EVP_PKEY_free(pkey);
  43793. pkey = NULL;
  43794. #endif /* NO_DSA */
  43795. #ifdef HAVE_ECC
  43796. type = EVP_PKEY_EC;
  43797. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43798. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  43799. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(NULL, type, ecKey), WOLFSSL_FAILURE);
  43800. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, NULL), WOLFSSL_FAILURE);
  43801. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, -1, ecKey), WOLFSSL_FAILURE);
  43802. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_FAILURE);
  43803. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  43804. ExpectIntEQ(wolfSSL_EVP_PKEY_assign(pkey, type, ecKey), WOLFSSL_SUCCESS);
  43805. if (EXPECT_FAIL()) {
  43806. wolfSSL_EC_KEY_free(ecKey);
  43807. }
  43808. wolfSSL_EVP_PKEY_free(pkey);
  43809. pkey = NULL;
  43810. #endif /* HAVE_ECC */
  43811. #endif /* !NO_RSA || !NO_DSA || HAVE_ECC */
  43812. return EXPECT_RESULT();
  43813. }
  43814. static int test_wolfSSL_EVP_PKEY_assign_DH(void)
  43815. {
  43816. EXPECT_DECLS;
  43817. #if !defined(NO_DH) && \
  43818. !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  43819. XFILE f = XBADFILE;
  43820. unsigned char buf[4096];
  43821. const unsigned char* pt = buf;
  43822. const char* params1 = "./certs/dh2048.der";
  43823. long len = 0;
  43824. WOLFSSL_DH* dh = NULL;
  43825. WOLFSSL_EVP_PKEY* pkey = NULL;
  43826. XMEMSET(buf, 0, sizeof(buf));
  43827. /* Load DH parameters DER. */
  43828. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  43829. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  43830. if (f != XBADFILE)
  43831. XFCLOSE(f);
  43832. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  43833. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  43834. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43835. /* Bad cases */
  43836. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, dh), WOLFSSL_FAILURE);
  43837. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, NULL), WOLFSSL_FAILURE);
  43838. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(NULL, NULL), WOLFSSL_FAILURE);
  43839. /* Good case */
  43840. ExpectIntEQ(wolfSSL_EVP_PKEY_assign_DH(pkey, dh), WOLFSSL_SUCCESS);
  43841. if (EXPECT_FAIL()) {
  43842. wolfSSL_DH_free(dh);
  43843. }
  43844. EVP_PKEY_free(pkey);
  43845. #endif
  43846. return EXPECT_RESULT();
  43847. }
  43848. static int test_wolfSSL_EVP_PKEY_base_id(void)
  43849. {
  43850. EXPECT_DECLS;
  43851. WOLFSSL_EVP_PKEY* pkey = NULL;
  43852. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43853. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(NULL), NID_undef);
  43854. ExpectIntEQ(wolfSSL_EVP_PKEY_base_id(pkey), EVP_PKEY_RSA);
  43855. EVP_PKEY_free(pkey);
  43856. return EXPECT_RESULT();
  43857. }
  43858. static int test_wolfSSL_EVP_PKEY_id(void)
  43859. {
  43860. EXPECT_DECLS;
  43861. WOLFSSL_EVP_PKEY* pkey = NULL;
  43862. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43863. ExpectIntEQ(wolfSSL_EVP_PKEY_id(NULL), 0);
  43864. ExpectIntEQ(wolfSSL_EVP_PKEY_id(pkey), EVP_PKEY_RSA);
  43865. EVP_PKEY_free(pkey);
  43866. return EXPECT_RESULT();
  43867. }
  43868. static int test_wolfSSL_EVP_PKEY_paramgen(void)
  43869. {
  43870. EXPECT_DECLS;
  43871. /* ECC check taken from ecc.c. It is the condition that defines ECC256 */
  43872. #if defined(OPENSSL_ALL) && !defined(NO_ECC_SECP) && \
  43873. ((!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && \
  43874. ECC_MIN_KEY_SZ <= 256)
  43875. EVP_PKEY_CTX* ctx = NULL;
  43876. EVP_PKEY* pkey = NULL;
  43877. /* Test error conditions. */
  43878. ExpectIntEQ(EVP_PKEY_paramgen(NULL, &pkey), WOLFSSL_FAILURE);
  43879. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  43880. ExpectIntEQ(EVP_PKEY_paramgen(ctx, NULL), WOLFSSL_FAILURE);
  43881. #ifndef NO_RSA
  43882. EVP_PKEY_CTX_free(ctx);
  43883. /* Parameter generation for RSA not supported yet. */
  43884. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_RSA, NULL));
  43885. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_FAILURE);
  43886. #endif
  43887. #ifdef HAVE_ECC
  43888. EVP_PKEY_CTX_free(ctx);
  43889. ExpectNotNull(ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_EC, NULL));
  43890. ExpectIntEQ(EVP_PKEY_paramgen_init(ctx), WOLFSSL_SUCCESS);
  43891. ExpectIntEQ(EVP_PKEY_CTX_set_ec_paramgen_curve_nid(ctx,
  43892. NID_X9_62_prime256v1), WOLFSSL_SUCCESS);
  43893. ExpectIntEQ(EVP_PKEY_paramgen(ctx, &pkey), WOLFSSL_SUCCESS);
  43894. ExpectIntEQ(EVP_PKEY_CTX_set_ec_param_enc(ctx, OPENSSL_EC_NAMED_CURVE),
  43895. WOLFSSL_SUCCESS);
  43896. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  43897. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  43898. #endif
  43899. EVP_PKEY_CTX_free(ctx);
  43900. EVP_PKEY_free(pkey);
  43901. #endif
  43902. return EXPECT_RESULT();
  43903. }
  43904. static int test_wolfSSL_EVP_PKEY_keygen(void)
  43905. {
  43906. EXPECT_DECLS;
  43907. WOLFSSL_EVP_PKEY* pkey = NULL;
  43908. EVP_PKEY_CTX* ctx = NULL;
  43909. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  43910. WOLFSSL_EVP_PKEY* params = NULL;
  43911. DH* dh = NULL;
  43912. const BIGNUM* pubkey = NULL;
  43913. const BIGNUM* privkey = NULL;
  43914. ASN1_INTEGER* asn1int = NULL;
  43915. unsigned int length = 0;
  43916. byte* derBuffer = NULL;
  43917. #endif
  43918. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43919. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43920. /* Bad cases */
  43921. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, &pkey), BAD_FUNC_ARG);
  43922. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, NULL), BAD_FUNC_ARG);
  43923. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(NULL, NULL), BAD_FUNC_ARG);
  43924. /* Good case */
  43925. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen(ctx, &pkey), 0);
  43926. EVP_PKEY_CTX_free(ctx);
  43927. ctx = NULL;
  43928. EVP_PKEY_free(pkey);
  43929. pkey = NULL;
  43930. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  43931. /* Test DH keygen */
  43932. {
  43933. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  43934. ExpectNotNull(dh = DH_get_2048_256());
  43935. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  43936. ExpectNotNull(ctx = EVP_PKEY_CTX_new(params, NULL));
  43937. ExpectIntEQ(EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  43938. ExpectIntEQ(EVP_PKEY_keygen(ctx, &pkey), WOLFSSL_SUCCESS);
  43939. DH_free(dh);
  43940. dh = NULL;
  43941. EVP_PKEY_CTX_free(ctx);
  43942. EVP_PKEY_free(params);
  43943. /* try exporting generated key to DER, to verify */
  43944. ExpectNotNull(dh = EVP_PKEY_get1_DH(pkey));
  43945. DH_get0_key(dh, &pubkey, &privkey);
  43946. ExpectNotNull(pubkey);
  43947. ExpectNotNull(privkey);
  43948. ExpectNotNull(asn1int = BN_to_ASN1_INTEGER(pubkey, NULL));
  43949. ExpectIntGT((length = i2d_ASN1_INTEGER(asn1int, &derBuffer)), 0);
  43950. ASN1_INTEGER_free(asn1int);
  43951. DH_free(dh);
  43952. dh = NULL;
  43953. XFREE(derBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  43954. EVP_PKEY_free(pkey);
  43955. }
  43956. #endif
  43957. return EXPECT_RESULT();
  43958. }
  43959. static int test_wolfSSL_EVP_PKEY_keygen_init(void)
  43960. {
  43961. EXPECT_DECLS;
  43962. WOLFSSL_EVP_PKEY* pkey = NULL;
  43963. EVP_PKEY_CTX *ctx = NULL;
  43964. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43965. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  43966. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(ctx), WOLFSSL_SUCCESS);
  43967. ExpectIntEQ(wolfSSL_EVP_PKEY_keygen_init(NULL), WOLFSSL_SUCCESS);
  43968. EVP_PKEY_CTX_free(ctx);
  43969. EVP_PKEY_free(pkey);
  43970. return EXPECT_RESULT();
  43971. }
  43972. static int test_wolfSSL_EVP_PKEY_missing_parameters(void)
  43973. {
  43974. EXPECT_DECLS;
  43975. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_STUB)
  43976. WOLFSSL_EVP_PKEY* pkey = NULL;
  43977. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  43978. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(pkey), 0);
  43979. ExpectIntEQ(wolfSSL_EVP_PKEY_missing_parameters(NULL), 0);
  43980. EVP_PKEY_free(pkey);
  43981. #endif
  43982. return EXPECT_RESULT();
  43983. }
  43984. static int test_wolfSSL_EVP_PKEY_copy_parameters(void)
  43985. {
  43986. EXPECT_DECLS;
  43987. #if defined(OPENSSL_EXTRA) && !defined(NO_DH) && defined(WOLFSSL_KEY_GEN) && \
  43988. !defined(HAVE_SELFTEST) && (defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || \
  43989. defined(WOLFSSL_OPENSSH)) && defined(WOLFSSL_DH_EXTRA) && \
  43990. !defined(NO_FILESYSTEM)
  43991. WOLFSSL_EVP_PKEY* params = NULL;
  43992. WOLFSSL_EVP_PKEY* copy = NULL;
  43993. DH* dh = NULL;
  43994. BIGNUM* p1;
  43995. BIGNUM* g1;
  43996. BIGNUM* q1;
  43997. BIGNUM* p2;
  43998. BIGNUM* g2;
  43999. BIGNUM* q2;
  44000. /* create DH with DH_get_2048_256 params */
  44001. ExpectNotNull(params = wolfSSL_EVP_PKEY_new());
  44002. ExpectNotNull(dh = DH_get_2048_256());
  44003. ExpectIntEQ(EVP_PKEY_set1_DH(params, dh), WOLFSSL_SUCCESS);
  44004. DH_get0_pqg(dh, (const BIGNUM**)&p1,
  44005. (const BIGNUM**)&q1,
  44006. (const BIGNUM**)&g1);
  44007. DH_free(dh);
  44008. dh = NULL;
  44009. /* create DH with random generated DH params */
  44010. ExpectNotNull(copy = wolfSSL_EVP_PKEY_new());
  44011. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  44012. ExpectIntEQ(EVP_PKEY_set1_DH(copy, dh), WOLFSSL_SUCCESS);
  44013. DH_free(dh);
  44014. dh = NULL;
  44015. ExpectIntEQ(EVP_PKEY_copy_parameters(copy, params), WOLFSSL_SUCCESS);
  44016. ExpectNotNull(dh = EVP_PKEY_get1_DH(copy));
  44017. ExpectNotNull(dh->p);
  44018. ExpectNotNull(dh->g);
  44019. ExpectNotNull(dh->q);
  44020. DH_get0_pqg(dh, (const BIGNUM**)&p2,
  44021. (const BIGNUM**)&q2,
  44022. (const BIGNUM**)&g2);
  44023. ExpectIntEQ(BN_cmp(p1, p2), 0);
  44024. ExpectIntEQ(BN_cmp(q1, q2), 0);
  44025. ExpectIntEQ(BN_cmp(g1, g2), 0);
  44026. DH_free(dh);
  44027. dh = NULL;
  44028. EVP_PKEY_free(copy);
  44029. EVP_PKEY_free(params);
  44030. #endif
  44031. return EXPECT_RESULT();
  44032. }
  44033. static int test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(void)
  44034. {
  44035. EXPECT_DECLS;
  44036. WOLFSSL_EVP_PKEY* pkey = NULL;
  44037. EVP_PKEY_CTX* ctx = NULL;
  44038. int bits = 2048;
  44039. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44040. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44041. ExpectIntEQ(wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits(ctx, bits),
  44042. WOLFSSL_SUCCESS);
  44043. EVP_PKEY_CTX_free(ctx);
  44044. EVP_PKEY_free(pkey);
  44045. return EXPECT_RESULT();
  44046. }
  44047. static int test_wolfSSL_EVP_CIPHER_CTX_iv_length(void)
  44048. {
  44049. EXPECT_DECLS;
  44050. /* This is large enough to be used for all key sizes */
  44051. byte key[AES_256_KEY_SIZE] = {0};
  44052. byte iv[AES_BLOCK_SIZE] = {0};
  44053. int i;
  44054. int nids[] = {
  44055. #ifdef HAVE_AES_CBC
  44056. NID_aes_128_cbc,
  44057. #endif
  44058. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  44059. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  44060. #ifdef HAVE_AESGCM
  44061. NID_aes_128_gcm,
  44062. #endif
  44063. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  44064. #ifdef WOLFSSL_AES_COUNTER
  44065. NID_aes_128_ctr,
  44066. #endif
  44067. #ifndef NO_DES3
  44068. NID_des_cbc,
  44069. NID_des_ede3_cbc,
  44070. #endif
  44071. };
  44072. int iv_lengths[] = {
  44073. #ifdef HAVE_AES_CBC
  44074. AES_BLOCK_SIZE,
  44075. #endif
  44076. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  44077. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  44078. #ifdef HAVE_AESGCM
  44079. GCM_NONCE_MID_SZ,
  44080. #endif
  44081. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  44082. #ifdef WOLFSSL_AES_COUNTER
  44083. AES_BLOCK_SIZE,
  44084. #endif
  44085. #ifndef NO_DES3
  44086. DES_BLOCK_SIZE,
  44087. DES_BLOCK_SIZE,
  44088. #endif
  44089. };
  44090. int nidsLen = (sizeof(nids)/sizeof(int));
  44091. for (i = 0; i < nidsLen; i++) {
  44092. const EVP_CIPHER* init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  44093. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  44094. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44095. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44096. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_iv_length(ctx), iv_lengths[i]);
  44097. EVP_CIPHER_CTX_free(ctx);
  44098. }
  44099. return EXPECT_RESULT();
  44100. }
  44101. static int test_wolfSSL_EVP_CIPHER_CTX_key_length(void)
  44102. {
  44103. EXPECT_DECLS;
  44104. byte key[AES_256_KEY_SIZE] = {0};
  44105. byte iv[AES_BLOCK_SIZE] = {0};
  44106. int i;
  44107. int nids[] = {
  44108. #ifdef HAVE_AES_CBC
  44109. NID_aes_128_cbc,
  44110. NID_aes_256_cbc,
  44111. #endif
  44112. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  44113. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  44114. #ifdef HAVE_AESGCM
  44115. NID_aes_128_gcm,
  44116. NID_aes_256_gcm,
  44117. #endif
  44118. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  44119. #ifdef WOLFSSL_AES_COUNTER
  44120. NID_aes_128_ctr,
  44121. NID_aes_256_ctr,
  44122. #endif
  44123. #ifndef NO_DES3
  44124. NID_des_cbc,
  44125. NID_des_ede3_cbc,
  44126. #endif
  44127. };
  44128. int key_lengths[] = {
  44129. #ifdef HAVE_AES_CBC
  44130. AES_128_KEY_SIZE,
  44131. AES_256_KEY_SIZE,
  44132. #endif
  44133. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  44134. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  44135. #ifdef HAVE_AESGCM
  44136. AES_128_KEY_SIZE,
  44137. AES_256_KEY_SIZE,
  44138. #endif
  44139. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  44140. #ifdef WOLFSSL_AES_COUNTER
  44141. AES_128_KEY_SIZE,
  44142. AES_256_KEY_SIZE,
  44143. #endif
  44144. #ifndef NO_DES3
  44145. DES_KEY_SIZE,
  44146. DES3_KEY_SIZE,
  44147. #endif
  44148. };
  44149. int nidsLen = (sizeof(nids)/sizeof(int));
  44150. for (i = 0; i < nidsLen; i++) {
  44151. const EVP_CIPHER *init = wolfSSL_EVP_get_cipherbynid(nids[i]);
  44152. EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
  44153. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44154. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44155. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_key_length(ctx), key_lengths[i]);
  44156. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_key_length(ctx, key_lengths[i]),
  44157. WOLFSSL_SUCCESS);
  44158. EVP_CIPHER_CTX_free(ctx);
  44159. }
  44160. return EXPECT_RESULT();
  44161. }
  44162. static int test_wolfSSL_EVP_CIPHER_CTX_set_iv(void)
  44163. {
  44164. EXPECT_DECLS;
  44165. #if defined(HAVE_AESGCM) && !defined(NO_DES3)
  44166. int ivLen, keyLen;
  44167. EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
  44168. #ifdef HAVE_AESGCM
  44169. byte key[AES_128_KEY_SIZE] = {0};
  44170. byte iv[AES_BLOCK_SIZE] = {0};
  44171. const EVP_CIPHER *init = EVP_aes_128_gcm();
  44172. #else
  44173. byte key[DES3_KEY_SIZE] = {0};
  44174. byte iv[DES_BLOCK_SIZE] = {0};
  44175. const EVP_CIPHER *init = EVP_des_ede3_cbc();
  44176. #endif
  44177. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44178. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44179. ivLen = wolfSSL_EVP_CIPHER_CTX_iv_length(ctx);
  44180. keyLen = wolfSSL_EVP_CIPHER_CTX_key_length(ctx);
  44181. /* Bad cases */
  44182. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, iv, ivLen),
  44183. WOLFSSL_FAILURE);
  44184. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, NULL, ivLen),
  44185. WOLFSSL_FAILURE);
  44186. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, 0), WOLFSSL_FAILURE);
  44187. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(NULL, NULL, 0), WOLFSSL_FAILURE);
  44188. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, keyLen),
  44189. WOLFSSL_FAILURE);
  44190. /* Good case */
  44191. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_set_iv(ctx, iv, ivLen), 1);
  44192. EVP_CIPHER_CTX_free(ctx);
  44193. #endif
  44194. return EXPECT_RESULT();
  44195. }
  44196. static int test_wolfSSL_EVP_PKEY_CTX_new_id(void)
  44197. {
  44198. EXPECT_DECLS;
  44199. WOLFSSL_ENGINE* e = NULL;
  44200. int id = 0;
  44201. EVP_PKEY_CTX *ctx = NULL;
  44202. ExpectNotNull(ctx = wolfSSL_EVP_PKEY_CTX_new_id(id, e));
  44203. EVP_PKEY_CTX_free(ctx);
  44204. return EXPECT_RESULT();
  44205. }
  44206. static int test_wolfSSL_EVP_rc4(void)
  44207. {
  44208. EXPECT_DECLS;
  44209. #if !defined(NO_RC4)
  44210. ExpectNotNull(wolfSSL_EVP_rc4());
  44211. #endif
  44212. return EXPECT_RESULT();
  44213. }
  44214. static int test_wolfSSL_EVP_enc_null(void)
  44215. {
  44216. EXPECT_DECLS;
  44217. ExpectNotNull(wolfSSL_EVP_enc_null());
  44218. return EXPECT_RESULT();
  44219. }
  44220. static int test_wolfSSL_EVP_rc2_cbc(void)
  44221. {
  44222. EXPECT_DECLS;
  44223. #if defined(WOLFSSL_QT) && !defined(NO_WOLFSSL_STUB)
  44224. ExpectNull(wolfSSL_EVP_rc2_cbc());
  44225. #endif
  44226. return EXPECT_RESULT();
  44227. }
  44228. static int test_wolfSSL_EVP_mdc2(void)
  44229. {
  44230. EXPECT_DECLS;
  44231. #if !defined(NO_WOLFSSL_STUB)
  44232. ExpectNull(wolfSSL_EVP_mdc2());
  44233. #endif
  44234. return EXPECT_RESULT();
  44235. }
  44236. static int test_wolfSSL_EVP_md4(void)
  44237. {
  44238. EXPECT_DECLS;
  44239. #if !defined(NO_MD4)
  44240. ExpectNotNull(wolfSSL_EVP_md4());
  44241. #endif
  44242. return EXPECT_RESULT();
  44243. }
  44244. static int test_wolfSSL_EVP_aes_256_gcm(void)
  44245. {
  44246. EXPECT_DECLS;
  44247. #ifdef HAVE_AESGCM
  44248. ExpectNotNull(wolfSSL_EVP_aes_256_gcm());
  44249. #endif
  44250. return EXPECT_RESULT();
  44251. }
  44252. static int test_wolfSSL_EVP_aes_192_gcm(void)
  44253. {
  44254. EXPECT_DECLS;
  44255. #ifdef HAVE_AESGCM
  44256. ExpectNotNull(wolfSSL_EVP_aes_192_gcm());
  44257. #endif
  44258. return EXPECT_RESULT();
  44259. }
  44260. static int test_wolfSSL_EVP_aes_256_ccm(void)
  44261. {
  44262. EXPECT_DECLS;
  44263. #ifdef HAVE_AESCCM
  44264. ExpectNotNull(wolfSSL_EVP_aes_256_ccm());
  44265. #endif
  44266. return EXPECT_RESULT();
  44267. }
  44268. static int test_wolfSSL_EVP_aes_192_ccm(void)
  44269. {
  44270. EXPECT_DECLS;
  44271. #ifdef HAVE_AESCCM
  44272. ExpectNotNull(wolfSSL_EVP_aes_192_ccm());
  44273. #endif
  44274. return EXPECT_RESULT();
  44275. }
  44276. static int test_wolfSSL_EVP_aes_128_ccm(void)
  44277. {
  44278. EXPECT_DECLS;
  44279. #ifdef HAVE_AESCCM
  44280. ExpectNotNull(wolfSSL_EVP_aes_128_ccm());
  44281. #endif
  44282. return EXPECT_RESULT();
  44283. }
  44284. static int test_wolfSSL_EVP_ripemd160(void)
  44285. {
  44286. EXPECT_DECLS;
  44287. #if !defined(NO_WOLFSSL_STUB)
  44288. ExpectNull(wolfSSL_EVP_ripemd160());
  44289. #endif
  44290. return EXPECT_RESULT();
  44291. }
  44292. static int test_wolfSSL_EVP_get_digestbynid(void)
  44293. {
  44294. EXPECT_DECLS;
  44295. #ifndef NO_MD5
  44296. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_md5));
  44297. #endif
  44298. #ifndef NO_SHA
  44299. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha1));
  44300. #endif
  44301. #ifndef NO_SHA256
  44302. ExpectNotNull(wolfSSL_EVP_get_digestbynid(NID_sha256));
  44303. #endif
  44304. ExpectNull(wolfSSL_EVP_get_digestbynid(0));
  44305. return EXPECT_RESULT();
  44306. }
  44307. static int test_wolfSSL_EVP_MD_nid(void)
  44308. {
  44309. EXPECT_DECLS;
  44310. #ifndef NO_MD5
  44311. ExpectIntEQ(EVP_MD_nid(EVP_md5()), NID_md5);
  44312. #endif
  44313. #ifndef NO_SHA
  44314. ExpectIntEQ(EVP_MD_nid(EVP_sha1()), NID_sha1);
  44315. #endif
  44316. #ifndef NO_SHA256
  44317. ExpectIntEQ(EVP_MD_nid(EVP_sha256()), NID_sha256);
  44318. #endif
  44319. ExpectIntEQ(EVP_MD_nid(NULL), NID_undef);
  44320. return EXPECT_RESULT();
  44321. }
  44322. static int test_wolfSSL_EVP_PKEY_get0_EC_KEY(void)
  44323. {
  44324. EXPECT_DECLS;
  44325. #if defined(HAVE_ECC)
  44326. WOLFSSL_EVP_PKEY* pkey = NULL;
  44327. ExpectNull(EVP_PKEY_get0_EC_KEY(NULL));
  44328. ExpectNotNull(pkey = EVP_PKEY_new());
  44329. ExpectNull(EVP_PKEY_get0_EC_KEY(pkey));
  44330. EVP_PKEY_free(pkey);
  44331. #endif
  44332. return EXPECT_RESULT();
  44333. }
  44334. static int test_wolfSSL_EVP_X_STATE(void)
  44335. {
  44336. EXPECT_DECLS;
  44337. #if !defined(NO_DES3) && !defined(NO_RC4)
  44338. byte key[DES3_KEY_SIZE] = {0};
  44339. byte iv[DES_IV_SIZE] = {0};
  44340. EVP_CIPHER_CTX *ctx = NULL;
  44341. const EVP_CIPHER *init = NULL;
  44342. /* Bad test cases */
  44343. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44344. ExpectNotNull(init = EVP_des_ede3_cbc());
  44345. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44346. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44347. ExpectNull(wolfSSL_EVP_X_STATE(NULL));
  44348. ExpectNull(wolfSSL_EVP_X_STATE(ctx));
  44349. EVP_CIPHER_CTX_free(ctx);
  44350. ctx = NULL;
  44351. /* Good test case */
  44352. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44353. ExpectNotNull(init = wolfSSL_EVP_rc4());
  44354. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44355. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44356. ExpectNotNull(wolfSSL_EVP_X_STATE(ctx));
  44357. EVP_CIPHER_CTX_free(ctx);
  44358. #endif
  44359. return EXPECT_RESULT();
  44360. }
  44361. static int test_wolfSSL_EVP_X_STATE_LEN(void)
  44362. {
  44363. EXPECT_DECLS;
  44364. #if !defined(NO_DES3) && !defined(NO_RC4)
  44365. byte key[DES3_KEY_SIZE] = {0};
  44366. byte iv[DES_IV_SIZE] = {0};
  44367. EVP_CIPHER_CTX *ctx = NULL;
  44368. const EVP_CIPHER *init = NULL;
  44369. /* Bad test cases */
  44370. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44371. ExpectNotNull(init = EVP_des_ede3_cbc());
  44372. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44373. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44374. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(NULL), 0);
  44375. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), 0);
  44376. EVP_CIPHER_CTX_free(ctx);
  44377. ctx = NULL;
  44378. /* Good test case */
  44379. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  44380. ExpectNotNull(init = wolfSSL_EVP_rc4());
  44381. wolfSSL_EVP_CIPHER_CTX_init(ctx);
  44382. ExpectIntEQ(EVP_CipherInit(ctx, init, key, iv, 1), WOLFSSL_SUCCESS);
  44383. ExpectIntEQ(wolfSSL_EVP_X_STATE_LEN(ctx), sizeof(Arc4));
  44384. EVP_CIPHER_CTX_free(ctx);
  44385. #endif
  44386. return EXPECT_RESULT();
  44387. }
  44388. static int test_wolfSSL_EVP_CIPHER_block_size(void)
  44389. {
  44390. EXPECT_DECLS;
  44391. #if defined(HAVE_AES_CBC) || defined(HAVE_AESGCM) || \
  44392. defined(WOLFSSL_AES_COUNTER) || defined(HAVE_AES_ECB) || \
  44393. defined(WOLFSSL_AES_OFB) || !defined(NO_RC4) || \
  44394. (defined(HAVE_CHACHA) && defined(HAVE_POLY1305))
  44395. #ifdef HAVE_AES_CBC
  44396. #ifdef WOLFSSL_AES_128
  44397. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_cbc()), AES_BLOCK_SIZE);
  44398. #endif
  44399. #ifdef WOLFSSL_AES_192
  44400. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_cbc()), AES_BLOCK_SIZE);
  44401. #endif
  44402. #ifdef WOLFSSL_AES_256
  44403. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_cbc()), AES_BLOCK_SIZE);
  44404. #endif
  44405. #endif
  44406. #ifdef HAVE_AESGCM
  44407. #ifdef WOLFSSL_AES_128
  44408. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_gcm()), 1);
  44409. #endif
  44410. #ifdef WOLFSSL_AES_192
  44411. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_gcm()), 1);
  44412. #endif
  44413. #ifdef WOLFSSL_AES_256
  44414. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_gcm()), 1);
  44415. #endif
  44416. #endif
  44417. #ifdef HAVE_AESCCM
  44418. #ifdef WOLFSSL_AES_128
  44419. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ccm()), 1);
  44420. #endif
  44421. #ifdef WOLFSSL_AES_192
  44422. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ccm()), 1);
  44423. #endif
  44424. #ifdef WOLFSSL_AES_256
  44425. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ccm()), 1);
  44426. #endif
  44427. #endif
  44428. #ifdef WOLFSSL_AES_COUNTER
  44429. #ifdef WOLFSSL_AES_128
  44430. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ctr()), 1);
  44431. #endif
  44432. #ifdef WOLFSSL_AES_192
  44433. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ctr()), 1);
  44434. #endif
  44435. #ifdef WOLFSSL_AES_256
  44436. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ctr()), 1);
  44437. #endif
  44438. #endif
  44439. #ifdef HAVE_AES_ECB
  44440. #ifdef WOLFSSL_AES_128
  44441. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ecb()), AES_BLOCK_SIZE);
  44442. #endif
  44443. #ifdef WOLFSSL_AES_192
  44444. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ecb()), AES_BLOCK_SIZE);
  44445. #endif
  44446. #ifdef WOLFSSL_AES_256
  44447. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ecb()), AES_BLOCK_SIZE);
  44448. #endif
  44449. #endif
  44450. #ifdef WOLFSSL_AES_OFB
  44451. #ifdef WOLFSSL_AES_128
  44452. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_128_ofb()), 1);
  44453. #endif
  44454. #ifdef WOLFSSL_AES_192
  44455. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_192_ofb()), 1);
  44456. #endif
  44457. #ifdef WOLFSSL_AES_256
  44458. ExpectIntEQ(EVP_CIPHER_block_size(EVP_aes_256_ofb()), 1);
  44459. #endif
  44460. #endif
  44461. #ifndef NO_RC4
  44462. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_rc4()), 1);
  44463. #endif
  44464. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  44465. ExpectIntEQ(EVP_CIPHER_block_size(wolfSSL_EVP_chacha20_poly1305()), 1);
  44466. #endif
  44467. #endif
  44468. #ifdef WOLFSSL_SM4_ECB
  44469. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ecb()), SM4_BLOCK_SIZE);
  44470. #endif
  44471. #ifdef WOLFSSL_SM4_CBC
  44472. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_cbc()), SM4_BLOCK_SIZE);
  44473. #endif
  44474. #ifdef WOLFSSL_SM4_CTR
  44475. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ctr()), 1);
  44476. #endif
  44477. #ifdef WOLFSSL_SM4_GCM
  44478. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_gcm()), 1);
  44479. #endif
  44480. #ifdef WOLFSSL_SM4_CCM
  44481. ExpectIntEQ(EVP_CIPHER_block_size(EVP_sm4_ccm()), 1);
  44482. #endif
  44483. return EXPECT_RESULT();
  44484. }
  44485. static int test_wolfSSL_EVP_CIPHER_iv_length(void)
  44486. {
  44487. EXPECT_DECLS;
  44488. int nids[] = {
  44489. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  44490. #ifdef WOLFSSL_AES_128
  44491. NID_aes_128_cbc,
  44492. #endif
  44493. #ifdef WOLFSSL_AES_192
  44494. NID_aes_192_cbc,
  44495. #endif
  44496. #ifdef WOLFSSL_AES_256
  44497. NID_aes_256_cbc,
  44498. #endif
  44499. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  44500. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  44501. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  44502. #ifdef HAVE_AESGCM
  44503. #ifdef WOLFSSL_AES_128
  44504. NID_aes_128_gcm,
  44505. #endif
  44506. #ifdef WOLFSSL_AES_192
  44507. NID_aes_192_gcm,
  44508. #endif
  44509. #ifdef WOLFSSL_AES_256
  44510. NID_aes_256_gcm,
  44511. #endif
  44512. #endif /* HAVE_AESGCM */
  44513. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  44514. #ifdef WOLFSSL_AES_COUNTER
  44515. #ifdef WOLFSSL_AES_128
  44516. NID_aes_128_ctr,
  44517. #endif
  44518. #ifdef WOLFSSL_AES_192
  44519. NID_aes_192_ctr,
  44520. #endif
  44521. #ifdef WOLFSSL_AES_256
  44522. NID_aes_256_ctr,
  44523. #endif
  44524. #endif
  44525. #ifndef NO_DES3
  44526. NID_des_cbc,
  44527. NID_des_ede3_cbc,
  44528. #endif
  44529. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  44530. NID_chacha20_poly1305,
  44531. #endif
  44532. };
  44533. int iv_lengths[] = {
  44534. #if defined(HAVE_AES_CBC) || defined(WOLFSSL_AES_DIRECT)
  44535. #ifdef WOLFSSL_AES_128
  44536. AES_BLOCK_SIZE,
  44537. #endif
  44538. #ifdef WOLFSSL_AES_192
  44539. AES_BLOCK_SIZE,
  44540. #endif
  44541. #ifdef WOLFSSL_AES_256
  44542. AES_BLOCK_SIZE,
  44543. #endif
  44544. #endif /* HAVE_AES_CBC || WOLFSSL_AES_DIRECT */
  44545. #if (!defined(HAVE_FIPS) && !defined(HAVE_SELFTEST)) || \
  44546. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  44547. #ifdef HAVE_AESGCM
  44548. #ifdef WOLFSSL_AES_128
  44549. GCM_NONCE_MID_SZ,
  44550. #endif
  44551. #ifdef WOLFSSL_AES_192
  44552. GCM_NONCE_MID_SZ,
  44553. #endif
  44554. #ifdef WOLFSSL_AES_256
  44555. GCM_NONCE_MID_SZ,
  44556. #endif
  44557. #endif /* HAVE_AESGCM */
  44558. #endif /* (HAVE_FIPS && !HAVE_SELFTEST) || HAVE_FIPS_VERSION > 2 */
  44559. #ifdef WOLFSSL_AES_COUNTER
  44560. #ifdef WOLFSSL_AES_128
  44561. AES_BLOCK_SIZE,
  44562. #endif
  44563. #ifdef WOLFSSL_AES_192
  44564. AES_BLOCK_SIZE,
  44565. #endif
  44566. #ifdef WOLFSSL_AES_256
  44567. AES_BLOCK_SIZE,
  44568. #endif
  44569. #endif
  44570. #ifndef NO_DES3
  44571. DES_BLOCK_SIZE,
  44572. DES_BLOCK_SIZE,
  44573. #endif
  44574. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  44575. CHACHA20_POLY1305_AEAD_IV_SIZE,
  44576. #endif
  44577. };
  44578. int i;
  44579. int nidsLen = (sizeof(nids)/sizeof(int));
  44580. for (i = 0; i < nidsLen; i++) {
  44581. const EVP_CIPHER *c = EVP_get_cipherbynid(nids[i]);
  44582. ExpectIntEQ(EVP_CIPHER_iv_length(c), iv_lengths[i]);
  44583. }
  44584. return EXPECT_RESULT();
  44585. }
  44586. static int test_wolfSSL_EVP_SignInit_ex(void)
  44587. {
  44588. EXPECT_DECLS;
  44589. WOLFSSL_EVP_MD_CTX mdCtx;
  44590. WOLFSSL_ENGINE* e = 0;
  44591. const EVP_MD* md = EVP_sha256();
  44592. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  44593. ExpectIntEQ(wolfSSL_EVP_SignInit_ex(&mdCtx, md, e), WOLFSSL_SUCCESS);
  44594. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  44595. return EXPECT_RESULT();
  44596. }
  44597. static int test_wolfSSL_EVP_DigestFinal_ex(void)
  44598. {
  44599. EXPECT_DECLS;
  44600. #if !defined(NO_SHA256)
  44601. WOLFSSL_EVP_MD_CTX mdCtx;
  44602. unsigned int s = 0;
  44603. unsigned char md[WC_SHA256_DIGEST_SIZE];
  44604. unsigned char md2[WC_SHA256_DIGEST_SIZE];
  44605. /* Bad Case */
  44606. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  44607. (HAVE_FIPS_VERSION > 2))
  44608. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  44609. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), 0);
  44610. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), 1);
  44611. #else
  44612. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  44613. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md, &s), WOLFSSL_SUCCESS);
  44614. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  44615. #endif
  44616. /* Good Case */
  44617. wolfSSL_EVP_MD_CTX_init(&mdCtx);
  44618. ExpectIntEQ(wolfSSL_EVP_DigestInit(&mdCtx, EVP_sha256()), WOLFSSL_SUCCESS);
  44619. ExpectIntEQ(wolfSSL_EVP_DigestFinal_ex(&mdCtx, md2, &s), WOLFSSL_SUCCESS);
  44620. ExpectIntEQ(wolfSSL_EVP_MD_CTX_cleanup(&mdCtx), WOLFSSL_SUCCESS);
  44621. #endif
  44622. return EXPECT_RESULT();
  44623. }
  44624. static int test_wolfSSL_QT_EVP_PKEY_CTX_free(void)
  44625. {
  44626. EXPECT_DECLS;
  44627. #if defined(OPENSSL_EXTRA)
  44628. EVP_PKEY* pkey = NULL;
  44629. EVP_PKEY_CTX* ctx = NULL;
  44630. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  44631. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44632. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  44633. /* void */
  44634. EVP_PKEY_CTX_free(ctx);
  44635. #else
  44636. /* int */
  44637. ExpectIntEQ(EVP_PKEY_CTX_free(ctx), WOLFSSL_SUCCESS);
  44638. #endif
  44639. EVP_PKEY_free(pkey);
  44640. #endif
  44641. return EXPECT_RESULT();
  44642. }
  44643. static int test_wolfSSL_EVP_PKEY_param_check(void)
  44644. {
  44645. EXPECT_DECLS;
  44646. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT)
  44647. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  44648. DH *dh = NULL;
  44649. DH *setDh = NULL;
  44650. EVP_PKEY *pkey = NULL;
  44651. EVP_PKEY_CTX* ctx = NULL;
  44652. FILE* f = NULL;
  44653. unsigned char buf[512];
  44654. const unsigned char* pt = buf;
  44655. const char* dh2048 = "./certs/dh2048.der";
  44656. long len = 0;
  44657. int code = -1;
  44658. XMEMSET(buf, 0, sizeof(buf));
  44659. ExpectTrue((f = XFOPEN(dh2048, "rb")) != XBADFILE);
  44660. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  44661. if (f != XBADFILE)
  44662. XFCLOSE(f);
  44663. /* Load dh2048.der into DH with internal format */
  44664. ExpectNotNull(setDh = d2i_DHparams(NULL, &pt, len));
  44665. ExpectIntEQ(DH_check(setDh, &code), WOLFSSL_SUCCESS);
  44666. ExpectIntEQ(code, 0);
  44667. code = -1;
  44668. pkey = wolfSSL_EVP_PKEY_new();
  44669. /* Set DH into PKEY */
  44670. ExpectIntEQ(EVP_PKEY_set1_DH(pkey, setDh), WOLFSSL_SUCCESS);
  44671. /* create ctx from pkey */
  44672. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  44673. ExpectIntEQ(EVP_PKEY_param_check(ctx), 1/* valid */);
  44674. /* TODO: more invalid cases */
  44675. ExpectIntEQ(EVP_PKEY_param_check(NULL), 0);
  44676. EVP_PKEY_CTX_free(ctx);
  44677. EVP_PKEY_free(pkey);
  44678. DH_free(setDh);
  44679. setDh = NULL;
  44680. DH_free(dh);
  44681. dh = NULL;
  44682. #endif
  44683. #endif
  44684. return EXPECT_RESULT();
  44685. }
  44686. static int test_wolfSSL_EVP_BytesToKey(void)
  44687. {
  44688. EXPECT_DECLS;
  44689. #if !defined(NO_AES) && defined(HAVE_AES_CBC)
  44690. byte key[AES_BLOCK_SIZE] = {0};
  44691. byte iv[AES_BLOCK_SIZE] = {0};
  44692. int count = 0;
  44693. const EVP_MD* md = EVP_sha256();
  44694. const EVP_CIPHER *type;
  44695. const unsigned char *salt = (unsigned char *)"salt1234";
  44696. int sz = 5;
  44697. const byte data[] = {
  44698. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  44699. 0x72,0x6c,0x64
  44700. };
  44701. type = wolfSSL_EVP_get_cipherbynid(NID_aes_128_cbc);
  44702. /* Bad cases */
  44703. ExpectIntEQ(EVP_BytesToKey(NULL, md, salt, data, sz, count, key, iv),
  44704. 0);
  44705. ExpectIntEQ(EVP_BytesToKey(type, md, salt, NULL, sz, count, key, iv),
  44706. 16);
  44707. md = "2";
  44708. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  44709. WOLFSSL_FAILURE);
  44710. /* Good case */
  44711. md = EVP_sha256();
  44712. ExpectIntEQ(EVP_BytesToKey(type, md, salt, data, sz, count, key, iv),
  44713. 16);
  44714. #endif
  44715. return EXPECT_RESULT();
  44716. }
  44717. static int test_evp_cipher_aes_gcm(void)
  44718. {
  44719. EXPECT_DECLS;
  44720. #if defined(HAVE_AESGCM) && ((!defined(HAVE_FIPS) && \
  44721. !defined(HAVE_SELFTEST)) || (defined(HAVE_FIPS_VERSION) && \
  44722. (HAVE_FIPS_VERSION >= 2)))
  44723. /*
  44724. * This test checks data at various points in the encrypt/decrypt process
  44725. * against known values produced using the same test with OpenSSL. This
  44726. * interop testing is critical for verifying the correctness of our
  44727. * EVP_Cipher implementation with AES-GCM. Specifically, this test exercises
  44728. * a flow supported by OpenSSL that uses the control command
  44729. * EVP_CTRL_GCM_IV_GEN to increment the IV between cipher operations without
  44730. * the need to call EVP_CipherInit. OpenSSH uses this flow, for example. We
  44731. * had a bug with OpenSSH where wolfSSL OpenSSH servers could only talk to
  44732. * wolfSSL OpenSSH clients because there was a bug in this flow that
  44733. * happened to "cancel out" if both sides of the connection had the bug.
  44734. */
  44735. enum {
  44736. NUM_ENCRYPTIONS = 3,
  44737. AAD_SIZE = 4
  44738. };
  44739. byte plainText1[] = {
  44740. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  44741. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  44742. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23
  44743. };
  44744. byte plainText2[] = {
  44745. 0x42, 0x49, 0x3b, 0x27, 0x03, 0x35, 0x59, 0x14, 0x41, 0x47, 0x37, 0x14,
  44746. 0x0e, 0x34, 0x0d, 0x28, 0x63, 0x09, 0x0a, 0x5b, 0x22, 0x57, 0x42, 0x22,
  44747. 0x0f, 0x5c, 0x1e, 0x53, 0x45, 0x15, 0x62, 0x08, 0x60, 0x43, 0x50, 0x2c
  44748. };
  44749. byte plainText3[] = {
  44750. 0x36, 0x0d, 0x2b, 0x09, 0x4a, 0x56, 0x3b, 0x4c, 0x21, 0x22, 0x58, 0x0e,
  44751. 0x5b, 0x57, 0x10
  44752. };
  44753. byte* plainTexts[NUM_ENCRYPTIONS] = {
  44754. plainText1,
  44755. plainText2,
  44756. plainText3
  44757. };
  44758. const int plainTextSzs[NUM_ENCRYPTIONS] = {
  44759. sizeof(plainText1),
  44760. sizeof(plainText2),
  44761. sizeof(plainText3)
  44762. };
  44763. byte aad1[AAD_SIZE] = {
  44764. 0x00, 0x00, 0x00, 0x01
  44765. };
  44766. byte aad2[AAD_SIZE] = {
  44767. 0x00, 0x00, 0x00, 0x10
  44768. };
  44769. byte aad3[AAD_SIZE] = {
  44770. 0x00, 0x00, 0x01, 0x00
  44771. };
  44772. byte* aads[NUM_ENCRYPTIONS] = {
  44773. aad1,
  44774. aad2,
  44775. aad3
  44776. };
  44777. const byte iv[GCM_NONCE_MID_SZ] = {
  44778. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  44779. };
  44780. byte currentIv[GCM_NONCE_MID_SZ];
  44781. const byte key[] = {
  44782. 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b,
  44783. 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
  44784. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  44785. };
  44786. const byte expIvs[NUM_ENCRYPTIONS][GCM_NONCE_MID_SZ] = {
  44787. {
  44788. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  44789. 0xEF
  44790. },
  44791. {
  44792. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  44793. 0xF0
  44794. },
  44795. {
  44796. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE,
  44797. 0xF1
  44798. }
  44799. };
  44800. const byte expTags[NUM_ENCRYPTIONS][AES_BLOCK_SIZE] = {
  44801. {
  44802. 0x65, 0x4F, 0xF7, 0xA0, 0xBB, 0x7B, 0x90, 0xB7, 0x9C, 0xC8, 0x14,
  44803. 0x3D, 0x32, 0x18, 0x34, 0xA9
  44804. },
  44805. {
  44806. 0x50, 0x3A, 0x13, 0x8D, 0x91, 0x1D, 0xEC, 0xBB, 0xBA, 0x5B, 0x57,
  44807. 0xA2, 0xFD, 0x2D, 0x6B, 0x7F
  44808. },
  44809. {
  44810. 0x3B, 0xED, 0x18, 0x9C, 0xB3, 0xE3, 0x61, 0x1E, 0x11, 0xEB, 0x13,
  44811. 0x5B, 0xEC, 0x52, 0x49, 0x32,
  44812. }
  44813. };
  44814. const byte expCipherText1[] = {
  44815. 0xCB, 0x93, 0x4F, 0xC8, 0x22, 0xE2, 0xC0, 0x35, 0xAA, 0x6B, 0x41, 0x15,
  44816. 0x17, 0x30, 0x2F, 0x97, 0x20, 0x74, 0x39, 0x28, 0xF8, 0xEB, 0xC5, 0x51,
  44817. 0x7B, 0xD9, 0x8A, 0x36, 0xB8, 0xDA, 0x24, 0x80, 0xE7, 0x9E, 0x09, 0xDE
  44818. };
  44819. const byte expCipherText2[] = {
  44820. 0xF9, 0x32, 0xE1, 0x87, 0x37, 0x0F, 0x04, 0xC1, 0xB5, 0x59, 0xF0, 0x45,
  44821. 0x3A, 0x0D, 0xA0, 0x26, 0xFF, 0xA6, 0x8D, 0x38, 0xFE, 0xB8, 0xE5, 0xC2,
  44822. 0x2A, 0x98, 0x4A, 0x54, 0x8F, 0x1F, 0xD6, 0x13, 0x03, 0xB2, 0x1B, 0xC0
  44823. };
  44824. const byte expCipherText3[] = {
  44825. 0xD0, 0x37, 0x59, 0x1C, 0x2F, 0x85, 0x39, 0x4D, 0xED, 0xC2, 0x32, 0x5B,
  44826. 0x80, 0x5E, 0x6B,
  44827. };
  44828. const byte* expCipherTexts[NUM_ENCRYPTIONS] = {
  44829. expCipherText1,
  44830. expCipherText2,
  44831. expCipherText3
  44832. };
  44833. byte* cipherText = NULL;
  44834. byte* calcPlainText = NULL;
  44835. byte tag[AES_BLOCK_SIZE];
  44836. EVP_CIPHER_CTX* encCtx = NULL;
  44837. EVP_CIPHER_CTX* decCtx = NULL;
  44838. int i, j, outl;
  44839. /****************************************************/
  44840. for (i = 0; i < 3; ++i) {
  44841. ExpectNotNull(encCtx = EVP_CIPHER_CTX_new());
  44842. ExpectNotNull(decCtx = EVP_CIPHER_CTX_new());
  44843. /* First iteration, set key before IV. */
  44844. if (i == 0) {
  44845. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), key, NULL, 1),
  44846. SSL_SUCCESS);
  44847. /*
  44848. * The call to EVP_CipherInit below (with NULL key) should clear the
  44849. * authIvGenEnable flag set by EVP_CTRL_GCM_SET_IV_FIXED. As such, a
  44850. * subsequent EVP_CTRL_GCM_IV_GEN should fail. This matches OpenSSL
  44851. * behavior.
  44852. */
  44853. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  44854. (void*)iv), SSL_SUCCESS);
  44855. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, NULL, iv, 1),
  44856. SSL_SUCCESS);
  44857. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  44858. currentIv), SSL_FAILURE);
  44859. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), key, NULL, 0),
  44860. SSL_SUCCESS);
  44861. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, NULL, iv, 0),
  44862. SSL_SUCCESS);
  44863. }
  44864. /* Second iteration, IV before key. */
  44865. else {
  44866. ExpectIntEQ(EVP_CipherInit(encCtx, EVP_aes_256_gcm(), NULL, iv, 1),
  44867. SSL_SUCCESS);
  44868. ExpectIntEQ(EVP_CipherInit(encCtx, NULL, key, NULL, 1),
  44869. SSL_SUCCESS);
  44870. ExpectIntEQ(EVP_CipherInit(decCtx, EVP_aes_256_gcm(), NULL, iv, 0),
  44871. SSL_SUCCESS);
  44872. ExpectIntEQ(EVP_CipherInit(decCtx, NULL, key, NULL, 0),
  44873. SSL_SUCCESS);
  44874. }
  44875. /*
  44876. * EVP_CTRL_GCM_IV_GEN should fail if EVP_CTRL_GCM_SET_IV_FIXED hasn't
  44877. * been issued first.
  44878. */
  44879. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  44880. currentIv), SSL_FAILURE);
  44881. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  44882. (void*)iv), SSL_SUCCESS);
  44883. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_IV_FIXED, -1,
  44884. (void*)iv), SSL_SUCCESS);
  44885. for (j = 0; j < NUM_ENCRYPTIONS; ++j) {
  44886. /*************** Encrypt ***************/
  44887. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_IV_GEN, -1,
  44888. currentIv), SSL_SUCCESS);
  44889. /* Check current IV against expected. */
  44890. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  44891. /* Add AAD. */
  44892. if (i == 2) {
  44893. /* Test streaming API. */
  44894. ExpectIntEQ(EVP_CipherUpdate(encCtx, NULL, &outl, aads[j],
  44895. AAD_SIZE), SSL_SUCCESS);
  44896. }
  44897. else {
  44898. ExpectIntEQ(EVP_Cipher(encCtx, NULL, aads[j], AAD_SIZE),
  44899. AAD_SIZE);
  44900. }
  44901. ExpectNotNull(cipherText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  44902. DYNAMIC_TYPE_TMP_BUFFER));
  44903. /* Encrypt plaintext. */
  44904. if (i == 2) {
  44905. ExpectIntEQ(EVP_CipherUpdate(encCtx, cipherText, &outl,
  44906. plainTexts[j], plainTextSzs[j]),
  44907. SSL_SUCCESS);
  44908. }
  44909. else {
  44910. ExpectIntEQ(EVP_Cipher(encCtx, cipherText, plainTexts[j],
  44911. plainTextSzs[j]), plainTextSzs[j]);
  44912. }
  44913. if (i == 2) {
  44914. ExpectIntEQ(EVP_CipherFinal(encCtx, cipherText, &outl),
  44915. SSL_SUCCESS);
  44916. }
  44917. else {
  44918. /*
  44919. * Calling EVP_Cipher with NULL input and output for AES-GCM is
  44920. * akin to calling EVP_CipherFinal.
  44921. */
  44922. ExpectIntGE(EVP_Cipher(encCtx, NULL, NULL, 0), 0);
  44923. }
  44924. /* Check ciphertext against expected. */
  44925. ExpectIntEQ(XMEMCMP(cipherText, expCipherTexts[j], plainTextSzs[j]),
  44926. 0);
  44927. /* Get and check tag against expected. */
  44928. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(encCtx, EVP_CTRL_GCM_GET_TAG,
  44929. sizeof(tag), tag), SSL_SUCCESS);
  44930. ExpectIntEQ(XMEMCMP(tag, expTags[j], sizeof(tag)), 0);
  44931. /*************** Decrypt ***************/
  44932. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_IV_GEN, -1,
  44933. currentIv), SSL_SUCCESS);
  44934. /* Check current IV against expected. */
  44935. ExpectIntEQ(XMEMCMP(currentIv, expIvs[j], GCM_NONCE_MID_SZ), 0);
  44936. /* Add AAD. */
  44937. if (i == 2) {
  44938. /* Test streaming API. */
  44939. ExpectIntEQ(EVP_CipherUpdate(decCtx, NULL, &outl, aads[j],
  44940. AAD_SIZE), SSL_SUCCESS);
  44941. }
  44942. else {
  44943. ExpectIntEQ(EVP_Cipher(decCtx, NULL, aads[j], AAD_SIZE),
  44944. AAD_SIZE);
  44945. }
  44946. /* Set expected tag. */
  44947. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(decCtx, EVP_CTRL_GCM_SET_TAG,
  44948. sizeof(tag), tag), SSL_SUCCESS);
  44949. /* Decrypt ciphertext. */
  44950. ExpectNotNull(calcPlainText = (byte*)XMALLOC(plainTextSzs[j], NULL,
  44951. DYNAMIC_TYPE_TMP_BUFFER));
  44952. if (i == 2) {
  44953. ExpectIntEQ(EVP_CipherUpdate(decCtx, calcPlainText, &outl,
  44954. cipherText, plainTextSzs[j]),
  44955. SSL_SUCCESS);
  44956. }
  44957. else {
  44958. /* This first EVP_Cipher call will check the tag, too. */
  44959. ExpectIntEQ(EVP_Cipher(decCtx, calcPlainText, cipherText,
  44960. plainTextSzs[j]), plainTextSzs[j]);
  44961. }
  44962. if (i == 2) {
  44963. ExpectIntEQ(EVP_CipherFinal(decCtx, calcPlainText, &outl),
  44964. SSL_SUCCESS);
  44965. }
  44966. else {
  44967. ExpectIntGE(EVP_Cipher(decCtx, NULL, NULL, 0), 0);
  44968. }
  44969. /* Check plaintext against expected. */
  44970. ExpectIntEQ(XMEMCMP(calcPlainText, plainTexts[j], plainTextSzs[j]),
  44971. 0);
  44972. XFREE(cipherText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44973. cipherText = NULL;
  44974. XFREE(calcPlainText, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  44975. calcPlainText = NULL;
  44976. }
  44977. EVP_CIPHER_CTX_free(encCtx);
  44978. encCtx = NULL;
  44979. EVP_CIPHER_CTX_free(decCtx);
  44980. decCtx = NULL;
  44981. }
  44982. #endif
  44983. return EXPECT_RESULT();
  44984. }
  44985. static int test_wolfSSL_OBJ_ln(void)
  44986. {
  44987. EXPECT_DECLS;
  44988. const int nid_set[] = {
  44989. NID_commonName,
  44990. NID_serialNumber,
  44991. NID_countryName,
  44992. NID_localityName,
  44993. NID_stateOrProvinceName,
  44994. NID_organizationName,
  44995. NID_organizationalUnitName,
  44996. NID_domainComponent,
  44997. NID_businessCategory,
  44998. NID_jurisdictionCountryName,
  44999. NID_jurisdictionStateOrProvinceName,
  45000. NID_emailAddress
  45001. };
  45002. const char* ln_set[] = {
  45003. "commonName",
  45004. "serialNumber",
  45005. "countryName",
  45006. "localityName",
  45007. "stateOrProvinceName",
  45008. "organizationName",
  45009. "organizationalUnitName",
  45010. "domainComponent",
  45011. "businessCategory",
  45012. "jurisdictionCountryName",
  45013. "jurisdictionStateOrProvinceName",
  45014. "emailAddress",
  45015. };
  45016. size_t i = 0, maxIdx = sizeof(ln_set)/sizeof(char*);
  45017. ExpectIntEQ(OBJ_ln2nid(NULL), NID_undef);
  45018. #ifdef HAVE_ECC
  45019. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  45020. {
  45021. EC_builtin_curve r[27];
  45022. size_t nCurves = sizeof(r) / sizeof(r[0]);
  45023. nCurves = EC_get_builtin_curves(r, nCurves);
  45024. for (i = 0; i < nCurves; i++) {
  45025. /* skip ECC_CURVE_INVALID */
  45026. if (r[i].nid != ECC_CURVE_INVALID) {
  45027. ExpectIntEQ(OBJ_ln2nid(r[i].comment), r[i].nid);
  45028. ExpectStrEQ(OBJ_nid2ln(r[i].nid), r[i].comment);
  45029. }
  45030. }
  45031. }
  45032. #endif
  45033. #endif
  45034. for (i = 0; i < maxIdx; i++) {
  45035. ExpectIntEQ(OBJ_ln2nid(ln_set[i]), nid_set[i]);
  45036. ExpectStrEQ(OBJ_nid2ln(nid_set[i]), ln_set[i]);
  45037. }
  45038. return EXPECT_RESULT();
  45039. }
  45040. static int test_wolfSSL_OBJ_sn(void)
  45041. {
  45042. EXPECT_DECLS;
  45043. int i = 0, maxIdx = 7;
  45044. const int nid_set[] = {NID_commonName,NID_countryName,NID_localityName,
  45045. NID_stateOrProvinceName,NID_organizationName,
  45046. NID_organizationalUnitName,NID_emailAddress};
  45047. const char* sn_open_set[] = {"CN","C","L","ST","O","OU","emailAddress"};
  45048. const char* sn_wolf_set[] = {WOLFSSL_COMMON_NAME,WOLFSSL_COUNTRY_NAME,
  45049. WOLFSSL_LOCALITY_NAME, WOLFSSL_STATE_NAME,
  45050. WOLFSSL_ORG_NAME, WOLFSSL_ORGUNIT_NAME,
  45051. WOLFSSL_EMAIL_ADDR};
  45052. ExpectIntEQ(wolfSSL_OBJ_sn2nid(NULL), NID_undef);
  45053. for (i = 0; i < maxIdx; i++) {
  45054. ExpectIntEQ(wolfSSL_OBJ_sn2nid(sn_wolf_set[i]), nid_set[i]);
  45055. ExpectStrEQ(wolfSSL_OBJ_nid2sn(nid_set[i]), sn_open_set[i]);
  45056. }
  45057. return EXPECT_RESULT();
  45058. }
  45059. #if !defined(NO_BIO)
  45060. static unsigned long TXT_DB_hash(const WOLFSSL_STRING *s)
  45061. {
  45062. return lh_strhash(s[3]);
  45063. }
  45064. static int TXT_DB_cmp(const WOLFSSL_STRING *a, const WOLFSSL_STRING *b)
  45065. {
  45066. return XSTRCMP(a[3], b[3]);
  45067. }
  45068. #endif
  45069. static int test_wolfSSL_TXT_DB(void)
  45070. {
  45071. EXPECT_DECLS;
  45072. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  45073. BIO *bio = NULL;
  45074. TXT_DB *db = NULL;
  45075. const int columns = 6;
  45076. const char *fields[6] = {
  45077. "V",
  45078. "320926161116Z",
  45079. "",
  45080. "12BD",
  45081. "unknown",
  45082. "/CN=rsa doe",
  45083. };
  45084. char** fields_copy = NULL;
  45085. /* Test read */
  45086. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  45087. ExpectIntGT(BIO_read_filename(bio, "./tests/TXT_DB.txt"), 0);
  45088. ExpectNotNull(db = TXT_DB_read(bio, columns));
  45089. ExpectNotNull(fields_copy = (char**)XMALLOC(sizeof(fields), NULL,
  45090. DYNAMIC_TYPE_OPENSSL));
  45091. if (fields_copy != NULL) {
  45092. XMEMCPY(fields_copy, fields, sizeof(fields));
  45093. }
  45094. ExpectIntEQ(TXT_DB_insert(db, fields_copy), 1);
  45095. if (EXPECT_FAIL()) {
  45096. XFREE(fields_copy, NULL, DYNAMIC_TYPE_OPENSSL);
  45097. }
  45098. BIO_free(bio);
  45099. bio = NULL;
  45100. /* Test write */
  45101. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  45102. ExpectIntEQ(TXT_DB_write(bio, db), 1484);
  45103. BIO_free(bio);
  45104. /* Test index */
  45105. ExpectIntEQ(TXT_DB_create_index(db, 3, NULL, (wolf_sk_hash_cb)TXT_DB_hash,
  45106. (wolf_lh_compare_cb)TXT_DB_cmp), 1);
  45107. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  45108. fields[3] = "12DA";
  45109. ExpectNotNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  45110. fields[3] = "FFFF";
  45111. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  45112. fields[3] = "";
  45113. ExpectNull(TXT_DB_get_by_index(db, 3, (WOLFSSL_STRING*)fields));
  45114. TXT_DB_free(db);
  45115. #endif
  45116. return EXPECT_RESULT();
  45117. }
  45118. static int test_wolfSSL_NCONF(void)
  45119. {
  45120. EXPECT_DECLS;
  45121. #if !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  45122. const char* confFile = "./tests/NCONF_test.cnf";
  45123. CONF* conf = NULL;
  45124. long eline = 0;
  45125. long num = 0;
  45126. ExpectNotNull(conf = NCONF_new(NULL));
  45127. ExpectIntEQ(NCONF_load(conf, confFile, &eline), 1);
  45128. ExpectIntEQ(NCONF_get_number(conf, NULL, "port", &num), 1);
  45129. ExpectIntEQ(num, 1234);
  45130. ExpectIntEQ(NCONF_get_number(conf, "section2", "port", &num), 1);
  45131. ExpectIntEQ(num, 4321);
  45132. ExpectStrEQ(NCONF_get_string(conf, NULL, "dir"), "./test-dir");
  45133. ExpectStrEQ(NCONF_get_string(conf, "section1", "file1_copy"),
  45134. "./test-dir/file1");
  45135. ExpectStrEQ(NCONF_get_string(conf, "section2", "file_list"),
  45136. "./test-dir/file1:./test-dir/file2:./section1:file2");
  45137. NCONF_free(conf);
  45138. #endif
  45139. return EXPECT_RESULT();
  45140. }
  45141. #endif /* OPENSSL_ALL */
  45142. static int test_wolfSSL_X509V3_EXT_get(void) {
  45143. EXPECT_DECLS;
  45144. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45145. XFILE f = XBADFILE;
  45146. int numOfExt =0;
  45147. int extNid = 0;
  45148. int i = 0;
  45149. WOLFSSL_X509* x509 = NULL;
  45150. WOLFSSL_X509_EXTENSION* ext = NULL;
  45151. const WOLFSSL_v3_ext_method* method = NULL;
  45152. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45153. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45154. if (f != XBADFILE)
  45155. XFCLOSE(f);
  45156. /* wolfSSL_X509V3_EXT_get() return struct and nid test */
  45157. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  45158. for (i = 0; i < numOfExt; i++) {
  45159. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45160. ExpectIntNE((extNid = ext->obj->nid), NID_undef);
  45161. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  45162. ExpectIntEQ(method->ext_nid, extNid);
  45163. }
  45164. /* wolfSSL_X509V3_EXT_get() NULL argument test */
  45165. ExpectNull(method = wolfSSL_X509V3_EXT_get(NULL));
  45166. wolfSSL_X509_free(x509);
  45167. #endif
  45168. return EXPECT_RESULT();
  45169. }
  45170. static int test_wolfSSL_X509V3_EXT_nconf(void)
  45171. {
  45172. EXPECT_DECLS;
  45173. #ifdef OPENSSL_ALL
  45174. const char *ext_names[] = {
  45175. "subjectKeyIdentifier",
  45176. "authorityKeyIdentifier",
  45177. "subjectAltName",
  45178. "keyUsage",
  45179. "extendedKeyUsage",
  45180. };
  45181. size_t ext_names_count = sizeof(ext_names)/sizeof(*ext_names);
  45182. int ext_nids[] = {
  45183. NID_subject_key_identifier,
  45184. NID_authority_key_identifier,
  45185. NID_subject_alt_name,
  45186. NID_key_usage,
  45187. NID_ext_key_usage,
  45188. };
  45189. size_t ext_nids_count = sizeof(ext_nids)/sizeof(*ext_nids);
  45190. const char *ext_values[] = {
  45191. "hash",
  45192. "hash",
  45193. "DNS:example.com, IP:127.0.0.1",
  45194. "digitalSignature,nonRepudiation,keyEncipherment,dataEncipherment,"
  45195. "keyAgreement,keyCertSign,cRLSign,encipherOnly,decipherOnly",
  45196. "serverAuth,clientAuth,codeSigning,emailProtection,timeStamping,"
  45197. "OCSPSigning",
  45198. };
  45199. size_t i;
  45200. X509_EXTENSION* ext = NULL;
  45201. X509* x509 = NULL;
  45202. unsigned int keyUsageFlags;
  45203. unsigned int extKeyUsageFlags;
  45204. ExpectNotNull(x509 = X509_new());
  45205. /* keyUsage / extKeyUsage should match string above */
  45206. keyUsageFlags = KU_DIGITAL_SIGNATURE
  45207. | KU_NON_REPUDIATION
  45208. | KU_KEY_ENCIPHERMENT
  45209. | KU_DATA_ENCIPHERMENT
  45210. | KU_KEY_AGREEMENT
  45211. | KU_KEY_CERT_SIGN
  45212. | KU_CRL_SIGN
  45213. | KU_ENCIPHER_ONLY
  45214. | KU_DECIPHER_ONLY;
  45215. extKeyUsageFlags = XKU_SSL_CLIENT
  45216. | XKU_SSL_SERVER
  45217. | XKU_CODE_SIGN
  45218. | XKU_SMIME
  45219. | XKU_TIMESTAMP
  45220. | XKU_OCSP_SIGN;
  45221. for (i = 0; i < ext_names_count; i++) {
  45222. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  45223. ext_values[i]));
  45224. X509_EXTENSION_free(ext);
  45225. ext = NULL;
  45226. }
  45227. for (i = 0; i < ext_nids_count; i++) {
  45228. ExpectNotNull(ext = X509V3_EXT_nconf_nid(NULL, NULL, ext_nids[i],
  45229. ext_values[i]));
  45230. X509_EXTENSION_free(ext);
  45231. ext = NULL;
  45232. }
  45233. /* Test adding extension to X509 */
  45234. for (i = 0; i < ext_nids_count; i++) {
  45235. ExpectNotNull(ext = X509V3_EXT_nconf(NULL, NULL, ext_names[i],
  45236. ext_values[i]));
  45237. ExpectIntEQ(X509_add_ext(x509, ext, -1), WOLFSSL_SUCCESS);
  45238. if (ext_nids[i] == NID_key_usage) {
  45239. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  45240. }
  45241. else if (ext_nids[i] == NID_ext_key_usage) {
  45242. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  45243. }
  45244. X509_EXTENSION_free(ext);
  45245. ext = NULL;
  45246. }
  45247. X509_free(x509);
  45248. #endif
  45249. return EXPECT_RESULT();
  45250. }
  45251. static int test_wolfSSL_X509V3_EXT(void) {
  45252. EXPECT_DECLS;
  45253. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45254. XFILE f = XBADFILE;
  45255. int numOfExt = 0, nid = 0, i = 0, expected, actual = 0;
  45256. char* str = NULL;
  45257. unsigned char* data = NULL;
  45258. const WOLFSSL_v3_ext_method* method = NULL;
  45259. WOLFSSL_X509* x509 = NULL;
  45260. WOLFSSL_X509_EXTENSION* ext = NULL;
  45261. WOLFSSL_X509_EXTENSION* ext2 = NULL;
  45262. WOLFSSL_ASN1_OBJECT *obj = NULL;
  45263. WOLFSSL_ASN1_OBJECT *adObj = NULL;
  45264. WOLFSSL_ASN1_STRING* asn1str = NULL;
  45265. WOLFSSL_AUTHORITY_KEYID* aKeyId = NULL;
  45266. WOLFSSL_AUTHORITY_INFO_ACCESS* aia = NULL;
  45267. WOLFSSL_BASIC_CONSTRAINTS* bc = NULL;
  45268. WOLFSSL_ACCESS_DESCRIPTION* ad = NULL;
  45269. WOLFSSL_GENERAL_NAME* gn = NULL;
  45270. /* Check NULL argument */
  45271. ExpectNull(wolfSSL_X509V3_EXT_d2i(NULL));
  45272. /* Using OCSP cert with X509V3 extensions */
  45273. ExpectTrue((f = XFOPEN("./certs/ocsp/root-ca-cert.pem", "rb")) != XBADFILE);
  45274. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45275. if (f != XBADFILE)
  45276. XFCLOSE(f);
  45277. ExpectIntEQ((numOfExt = wolfSSL_X509_get_ext_count(x509)), 5);
  45278. /* Basic Constraints */
  45279. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45280. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45281. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_basic_constraints);
  45282. ExpectNotNull(bc = (WOLFSSL_BASIC_CONSTRAINTS*)wolfSSL_X509V3_EXT_d2i(ext));
  45283. ExpectIntEQ(bc->ca, 1);
  45284. ExpectNull(bc->pathlen);
  45285. wolfSSL_BASIC_CONSTRAINTS_free(bc);
  45286. bc = NULL;
  45287. i++;
  45288. /* Subject Key Identifier */
  45289. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45290. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45291. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_subject_key_identifier);
  45292. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  45293. ExpectNotNull(ext2 = wolfSSL_X509V3_EXT_i2d(NID_subject_key_identifier, 0,
  45294. asn1str));
  45295. X509_EXTENSION_free(ext2);
  45296. ext2 = NULL;
  45297. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  45298. ExpectNotNull(method->i2s);
  45299. ExpectNotNull(str = method->i2s((WOLFSSL_v3_ext_method*)method, asn1str));
  45300. wolfSSL_ASN1_STRING_free(asn1str);
  45301. asn1str = NULL;
  45302. if (str != NULL) {
  45303. actual = strcmp(str,
  45304. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  45305. }
  45306. ExpectIntEQ(actual, 0);
  45307. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45308. str = NULL;
  45309. i++;
  45310. /* Authority Key Identifier */
  45311. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45312. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45313. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_authority_key_identifier);
  45314. ExpectNotNull(aKeyId = (WOLFSSL_AUTHORITY_KEYID*)wolfSSL_X509V3_EXT_d2i(
  45315. ext));
  45316. ExpectNotNull(method = wolfSSL_X509V3_EXT_get(ext));
  45317. ExpectNotNull(asn1str = aKeyId->keyid);
  45318. ExpectNotNull(str = wolfSSL_i2s_ASN1_STRING((WOLFSSL_v3_ext_method*)method,
  45319. asn1str));
  45320. asn1str = NULL;
  45321. if (str != NULL) {
  45322. actual = strcmp(str,
  45323. "73:B0:1C:A4:2F:82:CB:CF:47:A5:38:D7:B0:04:82:3A:7E:72:15:21");
  45324. }
  45325. ExpectIntEQ(actual, 0);
  45326. XFREE(str, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45327. str = NULL;
  45328. wolfSSL_AUTHORITY_KEYID_free(aKeyId);
  45329. aKeyId = NULL;
  45330. i++;
  45331. /* Key Usage */
  45332. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45333. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45334. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_key_usage);
  45335. ExpectNotNull(asn1str = (WOLFSSL_ASN1_STRING*)wolfSSL_X509V3_EXT_d2i(ext));
  45336. #if defined(WOLFSSL_QT)
  45337. ExpectNotNull(data = (unsigned char*)ASN1_STRING_get0_data(asn1str));
  45338. #else
  45339. ExpectNotNull(data = wolfSSL_ASN1_STRING_data(asn1str));
  45340. #endif
  45341. expected = KEYUSE_KEY_CERT_SIGN | KEYUSE_CRL_SIGN;
  45342. if (data != NULL) {
  45343. #ifdef BIG_ENDIAN_ORDER
  45344. actual = data[1];
  45345. #else
  45346. actual = data[0];
  45347. #endif
  45348. }
  45349. ExpectIntEQ(actual, expected);
  45350. wolfSSL_ASN1_STRING_free(asn1str);
  45351. asn1str = NULL;
  45352. #if 1
  45353. i++;
  45354. /* Authority Info Access */
  45355. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, i));
  45356. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(ext));
  45357. ExpectIntEQ((nid = wolfSSL_OBJ_obj2nid(obj)), NID_info_access);
  45358. ExpectNotNull(aia = (WOLFSSL_AUTHORITY_INFO_ACCESS*)wolfSSL_X509V3_EXT_d2i(
  45359. ext));
  45360. #if defined(WOLFSSL_QT)
  45361. ExpectIntEQ(OPENSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  45362. #else
  45363. ExpectIntEQ(wolfSSL_sk_num(aia), 1); /* Only one URI entry for this cert */
  45364. #endif
  45365. /* URI entry is an ACCESS_DESCRIPTION type */
  45366. #if defined(WOLFSSL_QT)
  45367. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)wolfSSL_sk_value(aia, 0));
  45368. #else
  45369. ExpectNotNull(ad = (WOLFSSL_ACCESS_DESCRIPTION*)OPENSSL_sk_value(aia, 0));
  45370. #endif
  45371. ExpectNotNull(adObj = ad->method);
  45372. /* Make sure nid is OCSP */
  45373. ExpectIntEQ(wolfSSL_OBJ_obj2nid(adObj), NID_ad_OCSP);
  45374. /* GENERAL_NAME stores URI as an ASN1_STRING */
  45375. ExpectNotNull(gn = ad->location);
  45376. ExpectIntEQ(gn->type, GEN_URI); /* Type should always be GEN_URI */
  45377. ExpectNotNull(asn1str = gn->d.uniformResourceIdentifier);
  45378. ExpectIntEQ(wolfSSL_ASN1_STRING_length(asn1str), 22);
  45379. #if defined(WOLFSSL_QT)
  45380. ExpectNotNull(str = (char*)ASN1_STRING_get0_data(asn1str));
  45381. #else
  45382. ExpectNotNull(str = (char*)wolfSSL_ASN1_STRING_data(asn1str));
  45383. #endif
  45384. if (str != NULL) {
  45385. actual = strcmp(str, "http://127.0.0.1:22220");
  45386. }
  45387. ExpectIntEQ(actual, 0);
  45388. wolfSSL_sk_ACCESS_DESCRIPTION_pop_free(aia, NULL);
  45389. aia = NULL;
  45390. #else
  45391. (void) aia; (void) ad; (void) adObj; (void) gn;
  45392. #endif
  45393. wolfSSL_X509_free(x509);
  45394. #endif
  45395. return EXPECT_RESULT();
  45396. }
  45397. static int test_wolfSSL_X509_get_extension_flags(void)
  45398. {
  45399. EXPECT_DECLS;
  45400. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  45401. XFILE f = XBADFILE;
  45402. X509* x509 = NULL;
  45403. unsigned int extFlags;
  45404. unsigned int keyUsageFlags;
  45405. unsigned int extKeyUsageFlags;
  45406. /* client-int-cert.pem has the following extension flags. */
  45407. extFlags = EXFLAG_KUSAGE | EXFLAG_XKUSAGE;
  45408. /* and the following key usage flags. */
  45409. keyUsageFlags = KU_DIGITAL_SIGNATURE
  45410. | KU_NON_REPUDIATION
  45411. | KU_KEY_ENCIPHERMENT;
  45412. /* and the following extended key usage flags. */
  45413. extKeyUsageFlags = XKU_SSL_CLIENT | XKU_SMIME;
  45414. ExpectTrue((f = XFOPEN("./certs/intermediate/client-int-cert.pem", "rb")) !=
  45415. XBADFILE);
  45416. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  45417. if (f != XBADFILE) {
  45418. XFCLOSE(f);
  45419. f = XBADFILE;
  45420. }
  45421. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  45422. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  45423. ExpectIntEQ(X509_get_extended_key_usage(x509), extKeyUsageFlags);
  45424. X509_free(x509);
  45425. x509 = NULL;
  45426. /* client-cert-ext.pem has the following extension flags. */
  45427. extFlags = EXFLAG_KUSAGE;
  45428. /* and the following key usage flags. */
  45429. keyUsageFlags = KU_DIGITAL_SIGNATURE
  45430. | KU_KEY_CERT_SIGN
  45431. | KU_CRL_SIGN;
  45432. ExpectTrue((f = fopen("./certs/client-cert-ext.pem", "rb")) != XBADFILE);
  45433. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  45434. if (f != XBADFILE)
  45435. XFCLOSE(f);
  45436. ExpectIntEQ(X509_get_extension_flags(x509), extFlags);
  45437. ExpectIntEQ(X509_get_key_usage(x509), keyUsageFlags);
  45438. X509_free(x509);
  45439. #endif /* OPENSSL_ALL */
  45440. return EXPECT_RESULT();
  45441. }
  45442. static int test_wolfSSL_X509_get_ext(void)
  45443. {
  45444. EXPECT_DECLS;
  45445. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45446. int ret = 0;
  45447. XFILE f = XBADFILE;
  45448. WOLFSSL_X509* x509 = NULL;
  45449. WOLFSSL_X509_EXTENSION* foundExtension;
  45450. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45451. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45452. if (f != XBADFILE)
  45453. XFCLOSE(f);
  45454. ExpectIntEQ((ret = wolfSSL_X509_get_ext_count(x509)), 5);
  45455. /* wolfSSL_X509_get_ext() valid input */
  45456. ExpectNotNull(foundExtension = wolfSSL_X509_get_ext(x509, 0));
  45457. /* wolfSSL_X509_get_ext() valid x509, idx out of bounds */
  45458. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, -1));
  45459. ExpectNull(foundExtension = wolfSSL_X509_get_ext(x509, 100));
  45460. /* wolfSSL_X509_get_ext() NULL x509, idx out of bounds */
  45461. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, -1));
  45462. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 100));
  45463. /* wolfSSL_X509_get_ext() NULL x509, valid idx */
  45464. ExpectNull(foundExtension = wolfSSL_X509_get_ext(NULL, 0));
  45465. wolfSSL_X509_free(x509);
  45466. #endif
  45467. return EXPECT_RESULT();
  45468. }
  45469. static int test_wolfSSL_X509_get_ext_by_NID(void)
  45470. {
  45471. EXPECT_DECLS;
  45472. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  45473. int rc = 0;
  45474. XFILE f = XBADFILE;
  45475. WOLFSSL_X509* x509 = NULL;
  45476. ASN1_OBJECT* obj = NULL;
  45477. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45478. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45479. if (f != XBADFILE)
  45480. XFCLOSE(f);
  45481. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  45482. -1), 0);
  45483. /* Start search from last location (should fail) */
  45484. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  45485. rc), -1);
  45486. ExpectIntGE(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_basic_constraints,
  45487. -2), -1);
  45488. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(NULL, NID_basic_constraints,
  45489. -1), -1);
  45490. ExpectIntEQ(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_undef, -1), -1);
  45491. /* NID_ext_key_usage, check also its nid and oid */
  45492. ExpectIntGT(rc = wolfSSL_X509_get_ext_by_NID(x509, NID_ext_key_usage, -1),
  45493. -1);
  45494. ExpectNotNull(obj = wolfSSL_X509_EXTENSION_get_object(wolfSSL_X509_get_ext(
  45495. x509, rc)));
  45496. ExpectIntEQ(obj->nid, NID_ext_key_usage);
  45497. ExpectIntEQ(obj->type, EXT_KEY_USAGE_OID);
  45498. wolfSSL_X509_free(x509);
  45499. #endif
  45500. return EXPECT_RESULT();
  45501. }
  45502. static int test_wolfSSL_X509_get_ext_subj_alt_name(void)
  45503. {
  45504. EXPECT_DECLS;
  45505. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  45506. int rc = 0;
  45507. XFILE f = XBADFILE;
  45508. WOLFSSL_X509* x509 = NULL;
  45509. WOLFSSL_X509_EXTENSION* ext = NULL;
  45510. WOLFSSL_ASN1_STRING* sanString = NULL;
  45511. byte* sanDer = NULL;
  45512. const byte expectedDer[] = {
  45513. 0x30, 0x13, 0x82, 0x0b, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2e,
  45514. 0x63, 0x6f, 0x6d, 0x87, 0x04, 0x7f, 0x00, 0x00, 0x01};
  45515. ExpectTrue((f = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45516. ExpectNotNull(x509 = PEM_read_X509(f, NULL, NULL, NULL));
  45517. if (f != XBADFILE)
  45518. XFCLOSE(f);
  45519. ExpectIntNE(rc = X509_get_ext_by_NID(x509, NID_subject_alt_name, -1), -1);
  45520. ExpectNotNull(ext = X509_get_ext(x509, rc));
  45521. ExpectNotNull(sanString = X509_EXTENSION_get_data(ext));
  45522. ExpectIntEQ(ASN1_STRING_length(sanString), sizeof(expectedDer));
  45523. ExpectNotNull(sanDer = ASN1_STRING_data(sanString));
  45524. ExpectIntEQ(XMEMCMP(sanDer, expectedDer, sizeof(expectedDer)), 0);
  45525. X509_free(x509);
  45526. #endif
  45527. return EXPECT_RESULT();
  45528. }
  45529. static int test_wolfSSL_X509_EXTENSION_new(void)
  45530. {
  45531. EXPECT_DECLS;
  45532. #if defined (OPENSSL_ALL)
  45533. WOLFSSL_X509_EXTENSION* ext = NULL;
  45534. ExpectNotNull(ext = wolfSSL_X509_EXTENSION_new());
  45535. ExpectNotNull(ext->obj = wolfSSL_ASN1_OBJECT_new());
  45536. wolfSSL_X509_EXTENSION_free(ext);
  45537. #endif
  45538. return EXPECT_RESULT();
  45539. }
  45540. static int test_wolfSSL_X509_EXTENSION_get_object(void)
  45541. {
  45542. EXPECT_DECLS;
  45543. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45544. WOLFSSL_X509* x509 = NULL;
  45545. WOLFSSL_X509_EXTENSION* ext = NULL;
  45546. WOLFSSL_ASN1_OBJECT* o = NULL;
  45547. XFILE file = XBADFILE;
  45548. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45549. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  45550. if (file != XBADFILE)
  45551. XFCLOSE(file);
  45552. /* wolfSSL_X509_EXTENSION_get_object() testing ext idx 0 */
  45553. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  45554. ExpectNull(wolfSSL_X509_EXTENSION_get_object(NULL));
  45555. ExpectNotNull(o = wolfSSL_X509_EXTENSION_get_object(ext));
  45556. ExpectIntEQ(o->nid, 128);
  45557. /* wolfSSL_X509_EXTENSION_get_object() NULL argument */
  45558. ExpectNull(o = wolfSSL_X509_EXTENSION_get_object(NULL));
  45559. wolfSSL_X509_free(x509);
  45560. #endif
  45561. return EXPECT_RESULT();
  45562. }
  45563. static int test_wolfSSL_X509_EXTENSION_get_data(void)
  45564. {
  45565. EXPECT_DECLS;
  45566. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45567. WOLFSSL_X509* x509 = NULL;
  45568. WOLFSSL_X509_EXTENSION* ext = NULL;
  45569. WOLFSSL_ASN1_STRING* str = NULL;
  45570. XFILE file = XBADFILE;
  45571. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45572. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  45573. if (file != XBADFILE)
  45574. XFCLOSE(file);
  45575. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  45576. ExpectNull(str = wolfSSL_X509_EXTENSION_get_data(NULL));
  45577. ExpectNotNull(str = wolfSSL_X509_EXTENSION_get_data(ext));
  45578. wolfSSL_X509_free(x509);
  45579. #endif
  45580. return EXPECT_RESULT();
  45581. }
  45582. static int test_wolfSSL_X509_EXTENSION_get_critical(void)
  45583. {
  45584. EXPECT_DECLS;
  45585. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_RSA)
  45586. WOLFSSL_X509* x509 = NULL;
  45587. WOLFSSL_X509_EXTENSION* ext = NULL;
  45588. XFILE file = XBADFILE;
  45589. int crit = 0;
  45590. ExpectTrue((file = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45591. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(file, NULL, NULL, NULL));
  45592. if (file != XBADFILE)
  45593. XFCLOSE(file);
  45594. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, 0));
  45595. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(NULL), BAD_FUNC_ARG);
  45596. ExpectIntEQ(crit = wolfSSL_X509_EXTENSION_get_critical(ext), 0);
  45597. wolfSSL_X509_free(x509);
  45598. #endif
  45599. return EXPECT_RESULT();
  45600. }
  45601. static int test_wolfSSL_X509V3_EXT_print(void)
  45602. {
  45603. EXPECT_DECLS;
  45604. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_ALL) && !defined(NO_BIO) && \
  45605. !defined(NO_RSA)
  45606. {
  45607. XFILE f = XBADFILE;
  45608. WOLFSSL_X509* x509 = NULL;
  45609. X509_EXTENSION * ext = NULL;
  45610. int loc = 0;
  45611. BIO *bio = NULL;
  45612. ExpectTrue((f = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  45613. ExpectNotNull(x509 = wolfSSL_PEM_read_X509(f, NULL, NULL, NULL));
  45614. if (f != XBADFILE)
  45615. fclose(f);
  45616. ExpectNotNull(bio = wolfSSL_BIO_new(BIO_s_mem()));
  45617. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  45618. NID_basic_constraints, -1), -1);
  45619. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  45620. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  45621. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  45622. NID_subject_key_identifier, -1), -1);
  45623. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  45624. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  45625. ExpectIntGT(loc = wolfSSL_X509_get_ext_by_NID(x509,
  45626. NID_authority_key_identifier, -1), -1);
  45627. ExpectNotNull(ext = wolfSSL_X509_get_ext(x509, loc));
  45628. ExpectIntEQ(wolfSSL_X509V3_EXT_print(bio, ext, 0, 0), WOLFSSL_SUCCESS);
  45629. wolfSSL_BIO_free(bio);
  45630. wolfSSL_X509_free(x509);
  45631. }
  45632. {
  45633. X509 *x509 = NULL;
  45634. BIO *bio = NULL;
  45635. X509_EXTENSION *ext = NULL;
  45636. unsigned int i = 0;
  45637. unsigned int idx = 0;
  45638. /* Some NIDs to test with */
  45639. int nids[] = {
  45640. /* NID_key_usage, currently X509_get_ext returns this as a bit
  45641. * string, which messes up X509V3_EXT_print */
  45642. /* NID_ext_key_usage, */
  45643. NID_subject_alt_name,
  45644. };
  45645. int* n = NULL;
  45646. ExpectNotNull(bio = BIO_new_fp(stderr, BIO_NOCLOSE));
  45647. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFileExt,
  45648. WOLFSSL_FILETYPE_PEM));
  45649. ExpectIntGT(fprintf(stderr, "\nPrinting extension values:\n"), 0);
  45650. for (i = 0, n = nids; i<(sizeof(nids)/sizeof(int)); i++, n++) {
  45651. /* X509_get_ext_by_NID should return 3 for now. If that changes then
  45652. * update the index */
  45653. ExpectIntEQ((idx = X509_get_ext_by_NID(x509, *n, -1)), 3);
  45654. ExpectNotNull(ext = X509_get_ext(x509, (int)idx));
  45655. ExpectIntEQ(X509V3_EXT_print(bio, ext, 0, 0), 1);
  45656. ExpectIntGT(fprintf(stderr, "\n"), 0);
  45657. }
  45658. BIO_free(bio);
  45659. X509_free(x509);
  45660. }
  45661. #endif
  45662. return EXPECT_RESULT();
  45663. }
  45664. static int test_wolfSSL_X509_cmp(void)
  45665. {
  45666. EXPECT_DECLS;
  45667. #if defined(OPENSSL_ALL) && !defined(NO_RSA)
  45668. XFILE file1 = XBADFILE;
  45669. XFILE file2 = XBADFILE;
  45670. WOLFSSL_X509* cert1 = NULL;
  45671. WOLFSSL_X509* cert2 = NULL;
  45672. ExpectTrue((file1 = XFOPEN("./certs/server-cert.pem", "rb")) != XBADFILE);
  45673. ExpectTrue((file2 = XFOPEN("./certs/3072/client-cert.pem", "rb")) !=
  45674. XBADFILE);
  45675. ExpectNotNull(cert1 = wolfSSL_PEM_read_X509(file1, NULL, NULL, NULL));
  45676. ExpectNotNull(cert2 = wolfSSL_PEM_read_X509(file2, NULL, NULL, NULL));
  45677. if (file1 != XBADFILE)
  45678. fclose(file1);
  45679. if (file2 != XBADFILE)
  45680. fclose(file2);
  45681. /* wolfSSL_X509_cmp() testing matching certs */
  45682. ExpectIntEQ(0, wolfSSL_X509_cmp(cert1, cert1));
  45683. /* wolfSSL_X509_cmp() testing mismatched certs */
  45684. ExpectIntEQ(-1, wolfSSL_X509_cmp(cert1, cert2));
  45685. /* wolfSSL_X509_cmp() testing NULL, valid args */
  45686. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, cert2));
  45687. /* wolfSSL_X509_cmp() testing valid, NULL args */
  45688. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(cert1, NULL));
  45689. /* wolfSSL_X509_cmp() testing NULL, NULL args */
  45690. ExpectIntEQ(BAD_FUNC_ARG, wolfSSL_X509_cmp(NULL, NULL));
  45691. wolfSSL_X509_free(cert1);
  45692. wolfSSL_X509_free(cert2);
  45693. #endif
  45694. return EXPECT_RESULT();
  45695. }
  45696. static int test_wolfSSL_EVP_PKEY_up_ref(void)
  45697. {
  45698. EXPECT_DECLS;
  45699. #if defined(OPENSSL_ALL)
  45700. EVP_PKEY* pkey;
  45701. pkey = EVP_PKEY_new();
  45702. ExpectNotNull(pkey);
  45703. ExpectIntEQ(EVP_PKEY_up_ref(NULL), 0);
  45704. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  45705. EVP_PKEY_free(pkey);
  45706. ExpectIntEQ(EVP_PKEY_up_ref(pkey), 1);
  45707. EVP_PKEY_free(pkey);
  45708. EVP_PKEY_free(pkey);
  45709. #endif
  45710. return EXPECT_RESULT();
  45711. }
  45712. static int test_wolfSSL_d2i_and_i2d_PublicKey(void)
  45713. {
  45714. EXPECT_DECLS;
  45715. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  45716. EVP_PKEY* pkey = NULL;
  45717. const unsigned char* p;
  45718. unsigned char *der = NULL;
  45719. unsigned char *tmp = NULL;
  45720. int derLen;
  45721. p = client_keypub_der_2048;
  45722. /* Check that key can be successfully decoded. */
  45723. ExpectNotNull(pkey = wolfSSL_d2i_PublicKey(EVP_PKEY_RSA, NULL, &p,
  45724. sizeof_client_keypub_der_2048));
  45725. /* Check that key can be successfully encoded. */
  45726. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  45727. /* Ensure that the encoded version matches the original. */
  45728. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  45729. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  45730. /* Do same test except with pre-allocated buffer to ensure the der pointer
  45731. * is advanced. */
  45732. tmp = der;
  45733. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  45734. ExpectIntEQ(derLen, sizeof_client_keypub_der_2048);
  45735. ExpectIntEQ(XMEMCMP(der, client_keypub_der_2048, derLen), 0);
  45736. ExpectTrue(der + derLen == tmp);
  45737. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  45738. EVP_PKEY_free(pkey);
  45739. #endif
  45740. return EXPECT_RESULT();
  45741. }
  45742. static int test_wolfSSL_d2i_and_i2d_PublicKey_ecc(void)
  45743. {
  45744. EXPECT_DECLS;
  45745. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(NO_CERTS) && \
  45746. !defined(NO_ASN) && !defined(NO_PWDBASED)
  45747. EVP_PKEY* pkey = NULL;
  45748. const unsigned char* p;
  45749. unsigned char *der = NULL;
  45750. unsigned char *tmp = NULL;
  45751. int derLen;
  45752. unsigned char pub_buf[65];
  45753. const int pub_len = 65;
  45754. BN_CTX* ctx;
  45755. EC_GROUP* curve = NULL;
  45756. EC_KEY* ephemeral_key = NULL;
  45757. const EC_POINT* h;
  45758. /* Generate an x963 key pair and get public part into pub_buf */
  45759. ExpectNotNull(ctx = BN_CTX_new());
  45760. ExpectNotNull(curve = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  45761. ExpectNotNull(ephemeral_key = EC_KEY_new_by_curve_name(
  45762. NID_X9_62_prime256v1));
  45763. ExpectIntEQ(EC_KEY_generate_key(ephemeral_key), 1);
  45764. ExpectNotNull(h = EC_KEY_get0_public_key(ephemeral_key));
  45765. ExpectIntEQ(pub_len, EC_POINT_point2oct(curve, h,
  45766. POINT_CONVERSION_UNCOMPRESSED, pub_buf, pub_len, ctx));
  45767. /* Prepare the EVP_PKEY */
  45768. ExpectNotNull(pkey = EVP_PKEY_new());
  45769. p = pub_buf;
  45770. /* Check that key can be successfully decoded. */
  45771. ExpectNotNull(wolfSSL_d2i_PublicKey(EVP_PKEY_EC, &pkey, &p,
  45772. pub_len));
  45773. /* Check that key can be successfully encoded. */
  45774. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &der)), 0);
  45775. /* Ensure that the encoded version matches the original. */
  45776. ExpectIntEQ(derLen, pub_len);
  45777. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  45778. /* Do same test except with pre-allocated buffer to ensure the der pointer
  45779. * is advanced. */
  45780. tmp = der;
  45781. ExpectIntGE((derLen = wolfSSL_i2d_PublicKey(pkey, &tmp)), 0);
  45782. ExpectIntEQ(derLen, pub_len);
  45783. ExpectIntEQ(XMEMCMP(der, pub_buf, derLen), 0);
  45784. ExpectTrue(der + derLen == tmp);
  45785. XFREE(der, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  45786. EVP_PKEY_free(pkey);
  45787. EC_KEY_free(ephemeral_key);
  45788. EC_GROUP_free(curve);
  45789. #endif
  45790. return EXPECT_RESULT();
  45791. }
  45792. static int test_wolfSSL_d2i_and_i2d_DSAparams(void)
  45793. {
  45794. EXPECT_DECLS;
  45795. #if defined(OPENSSL_EXTRA) && !defined(NO_DSA)
  45796. DSA* dsa = NULL;
  45797. byte derIn[] = {
  45798. 0x30, 0x82, 0x01, 0x1f, 0x02, 0x81, 0x81, 0x00,
  45799. 0xcd, 0xde, 0x25, 0x68, 0x80, 0x53, 0x0d, 0xe5,
  45800. 0x77, 0xd6, 0xd2, 0x90, 0x39, 0x3f, 0x90, 0xa2,
  45801. 0x3f, 0x33, 0x94, 0x6e, 0xe8, 0x4f, 0x2b, 0x63,
  45802. 0xab, 0x30, 0xab, 0x15, 0xba, 0x11, 0xea, 0x8a,
  45803. 0x5d, 0x8d, 0xcc, 0xb8, 0xd4, 0xa1, 0xd5, 0xc1,
  45804. 0x47, 0x9d, 0x5a, 0x73, 0x6a, 0x62, 0x49, 0xd1,
  45805. 0x06, 0x07, 0x67, 0xf6, 0x2f, 0xa3, 0x39, 0xbd,
  45806. 0x4e, 0x0d, 0xb4, 0xd3, 0x22, 0x23, 0x84, 0xec,
  45807. 0x93, 0x26, 0x5a, 0x49, 0xee, 0x7c, 0x89, 0x48,
  45808. 0x66, 0x4d, 0xe8, 0xe8, 0xd8, 0x50, 0xfb, 0xa5,
  45809. 0x71, 0x9f, 0x22, 0x18, 0xe5, 0xe6, 0x0b, 0x46,
  45810. 0x87, 0x66, 0xee, 0x52, 0x8f, 0x46, 0x4f, 0xb5,
  45811. 0x03, 0xce, 0xed, 0xe3, 0xbe, 0xe5, 0xb5, 0x81,
  45812. 0xd2, 0x59, 0xe9, 0xc0, 0xad, 0x4d, 0xd0, 0x4d,
  45813. 0x26, 0xf7, 0xba, 0x50, 0xe8, 0xc9, 0x8f, 0xfe,
  45814. 0x24, 0x19, 0x3d, 0x2e, 0xa7, 0x52, 0x3c, 0x6d,
  45815. 0x02, 0x15, 0x00, 0xfb, 0x47, 0xfb, 0xec, 0x81,
  45816. 0x20, 0xc8, 0x1c, 0xe9, 0x4a, 0xba, 0x04, 0x6f,
  45817. 0x19, 0x9b, 0x94, 0xee, 0x82, 0x67, 0xd3, 0x02,
  45818. 0x81, 0x81, 0x00, 0x9b, 0x95, 0xbb, 0x85, 0xc5,
  45819. 0x58, 0x4a, 0x32, 0x9c, 0xaa, 0x44, 0x85, 0xd6,
  45820. 0x68, 0xdc, 0x3e, 0x14, 0xf4, 0xce, 0x6d, 0xa3,
  45821. 0x49, 0x38, 0xea, 0xd6, 0x61, 0x48, 0x92, 0x5a,
  45822. 0x40, 0x95, 0x49, 0x38, 0xaa, 0xe1, 0x39, 0x29,
  45823. 0x68, 0x58, 0x47, 0x8a, 0x4b, 0x01, 0xe1, 0x2e,
  45824. 0x8e, 0x6c, 0x63, 0x6f, 0x40, 0xca, 0x50, 0x3f,
  45825. 0x8c, 0x0b, 0x99, 0xe4, 0x72, 0x42, 0xb8, 0xb1,
  45826. 0xc2, 0x26, 0x48, 0xf1, 0x9c, 0x83, 0xc6, 0x37,
  45827. 0x2e, 0x5a, 0xae, 0x11, 0x09, 0xd9, 0xf3, 0xad,
  45828. 0x1f, 0x6f, 0xad, 0xad, 0x50, 0xe3, 0x78, 0x32,
  45829. 0xe6, 0xde, 0x8e, 0xaa, 0xbf, 0xd1, 0x00, 0x9f,
  45830. 0xb3, 0x02, 0x12, 0x19, 0xa2, 0x15, 0xec, 0x14,
  45831. 0x18, 0x5c, 0x0e, 0x26, 0xce, 0xf9, 0xae, 0xcc,
  45832. 0x7b, 0xb5, 0xd1, 0x26, 0xfc, 0x85, 0xfe, 0x14,
  45833. 0x93, 0xb6, 0x9d, 0x7d, 0x76, 0xe3, 0x35, 0x97,
  45834. 0x1e, 0xde, 0xc4
  45835. };
  45836. int derInLen = sizeof(derIn);
  45837. byte* derOut = NULL;
  45838. int derOutLen;
  45839. byte* p = derIn;
  45840. /* Check that params can be successfully decoded. */
  45841. ExpectNotNull(dsa = d2i_DSAparams(NULL, (const byte**)&p, derInLen));
  45842. /* Check that params can be successfully encoded. */
  45843. ExpectIntGE((derOutLen = i2d_DSAparams(dsa, &derOut)), 0);
  45844. /* Ensure that the encoded version matches the original. */
  45845. ExpectIntEQ(derInLen, derOutLen);
  45846. ExpectIntEQ(XMEMCMP(derIn, derOut, derInLen), 0);
  45847. XFREE(derOut, HEAP_HINT, DYNAMIC_TYPE_OPENSSL);
  45848. DSA_free(dsa);
  45849. #endif
  45850. return EXPECT_RESULT();
  45851. }
  45852. static int test_wolfSSL_i2d_PrivateKey(void)
  45853. {
  45854. EXPECT_DECLS;
  45855. #if (!defined(NO_RSA) || defined(HAVE_ECC)) && defined(OPENSSL_EXTRA) && \
  45856. !defined(NO_ASN) && !defined(NO_PWDBASED)
  45857. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  45858. {
  45859. EVP_PKEY* pkey = NULL;
  45860. const unsigned char* server_key =
  45861. (const unsigned char*)server_key_der_2048;
  45862. unsigned char buf[FOURK_BUF];
  45863. unsigned char* pt = NULL;
  45864. int bufSz = 0;
  45865. ExpectNotNull(pkey = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &server_key,
  45866. (long)sizeof_server_key_der_2048));
  45867. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 1193);
  45868. pt = buf;
  45869. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 1193);
  45870. ExpectIntNE((pt - buf), 0);
  45871. ExpectIntEQ(XMEMCMP(buf, server_key_der_2048, bufSz), 0);
  45872. EVP_PKEY_free(pkey);
  45873. }
  45874. #endif
  45875. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  45876. {
  45877. EVP_PKEY* pkey = NULL;
  45878. const unsigned char* client_key =
  45879. (const unsigned char*)ecc_clikey_der_256;
  45880. unsigned char buf[FOURK_BUF];
  45881. unsigned char* pt = NULL;
  45882. int bufSz = 0;
  45883. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &client_key,
  45884. (long)sizeof_ecc_clikey_der_256)));
  45885. ExpectIntEQ(i2d_PrivateKey(pkey, NULL), 121);
  45886. pt = buf;
  45887. ExpectIntEQ((bufSz = i2d_PrivateKey(pkey, &pt)), 121);
  45888. ExpectIntNE((pt - buf), 0);
  45889. ExpectIntEQ(XMEMCMP(buf, ecc_clikey_der_256, bufSz), 0);
  45890. EVP_PKEY_free(pkey);
  45891. }
  45892. #endif
  45893. #endif
  45894. return EXPECT_RESULT();
  45895. }
  45896. static int test_wolfSSL_OCSP_id_get0_info(void)
  45897. {
  45898. EXPECT_DECLS;
  45899. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && \
  45900. defined(HAVE_OCSP) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  45901. X509* cert = NULL;
  45902. X509* issuer = NULL;
  45903. OCSP_CERTID* id = NULL;
  45904. OCSP_CERTID* id2 = NULL;
  45905. ASN1_STRING* name = NULL;
  45906. ASN1_OBJECT* pmd = NULL;
  45907. ASN1_STRING* keyHash = NULL;
  45908. ASN1_INTEGER* serial = NULL;
  45909. ASN1_INTEGER* x509Int = NULL;
  45910. ExpectNotNull(cert = wolfSSL_X509_load_certificate_file(svrCertFile,
  45911. SSL_FILETYPE_PEM));
  45912. ExpectNotNull(issuer = wolfSSL_X509_load_certificate_file(caCertFile,
  45913. SSL_FILETYPE_PEM));
  45914. ExpectNotNull(id = OCSP_cert_to_id(NULL, cert, issuer));
  45915. ExpectNotNull(id2 = OCSP_cert_to_id(NULL, cert, issuer));
  45916. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, NULL), 0);
  45917. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, NULL, id), 1);
  45918. /* name, pmd, keyHash not supported yet, expect failure if not NULL */
  45919. ExpectIntEQ(OCSP_id_get0_info(&name, NULL, NULL, NULL, id), 0);
  45920. ExpectIntEQ(OCSP_id_get0_info(NULL, &pmd, NULL, NULL, id), 0);
  45921. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, &keyHash, NULL, id), 0);
  45922. ExpectIntEQ(OCSP_id_get0_info(NULL, NULL, NULL, &serial, id), 1);
  45923. ExpectNotNull(serial);
  45924. /* compare serial number to one in cert, should be equal */
  45925. ExpectNotNull(x509Int = X509_get_serialNumber(cert));
  45926. ExpectIntEQ(x509Int->length, serial->length);
  45927. ExpectIntEQ(XMEMCMP(x509Int->data, serial->data, serial->length), 0);
  45928. /* test OCSP_id_cmp */
  45929. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  45930. ExpectIntNE(OCSP_id_cmp(id, NULL), 0);
  45931. ExpectIntNE(OCSP_id_cmp(NULL, id2), 0);
  45932. ExpectIntEQ(OCSP_id_cmp(id, id2), 0);
  45933. if (id != NULL) {
  45934. id->issuerHash[0] = ~id->issuerHash[0];
  45935. }
  45936. ExpectIntNE(OCSP_id_cmp(id, id2), 0);
  45937. OCSP_CERTID_free(id);
  45938. OCSP_CERTID_free(id2);
  45939. X509_free(cert); /* free's x509Int */
  45940. X509_free(issuer);
  45941. #endif
  45942. return EXPECT_RESULT();
  45943. }
  45944. static int test_wolfSSL_i2d_OCSP_CERTID(void)
  45945. {
  45946. EXPECT_DECLS;
  45947. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  45948. WOLFSSL_OCSP_CERTID certId;
  45949. byte* targetBuffer = NULL;
  45950. byte* p;
  45951. /* OCSP CertID bytes taken from PCAP */
  45952. byte rawCertId[] = {
  45953. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  45954. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  45955. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  45956. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  45957. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  45958. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  45959. 0xcf, 0xbc, 0x91
  45960. };
  45961. int ret = 0;
  45962. int i;
  45963. XMEMSET(&certId, 0, sizeof(WOLFSSL_OCSP_CERTID));
  45964. certId.rawCertId = rawCertId;
  45965. certId.rawCertIdSize = sizeof(rawCertId);
  45966. ExpectNotNull(targetBuffer = (byte*)XMALLOC(sizeof(rawCertId), NULL,
  45967. DYNAMIC_TYPE_TMP_BUFFER));
  45968. p = targetBuffer;
  45969. /* Function returns the size of the encoded data. */
  45970. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &p), sizeof(rawCertId));
  45971. /* If target buffer is not null, function increments targetBuffer to point
  45972. * just past the end of the encoded data. */
  45973. ExpectPtrEq(p, (targetBuffer + sizeof(rawCertId)));
  45974. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  45975. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  45976. }
  45977. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  45978. targetBuffer = NULL;
  45979. /* If target buffer is null, function allocates memory for a buffer and
  45980. * copies the encoded data into it. targetBuffer then points to the start of
  45981. * this newly allocate buffer. */
  45982. ExpectIntEQ(ret = wolfSSL_i2d_OCSP_CERTID(&certId, &targetBuffer),
  45983. sizeof(rawCertId));
  45984. for (i = 0; EXPECT_SUCCESS() && i < ret; ++i) {
  45985. ExpectIntEQ(targetBuffer[i], rawCertId[i]);
  45986. }
  45987. XFREE(targetBuffer, NULL, DYNAMIC_TYPE_OPENSSL);
  45988. #endif
  45989. return EXPECT_RESULT();
  45990. }
  45991. static int test_wolfSSL_d2i_OCSP_CERTID(void)
  45992. {
  45993. EXPECT_DECLS;
  45994. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_HAPROXY)) && defined(HAVE_OCSP)
  45995. WOLFSSL_OCSP_CERTID* certId;
  45996. WOLFSSL_OCSP_CERTID* certIdGood;
  45997. WOLFSSL_OCSP_CERTID* certIdBad;
  45998. const unsigned char* rawCertIdPtr;
  45999. const unsigned char rawCertId[] = {
  46000. 0x30, 0x49, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  46001. 0x00, 0x04, 0x14, 0x80, 0x51, 0x06, 0x01, 0x32, 0xad, 0x9a, 0xc2, 0x7d,
  46002. 0x51, 0x87, 0xa0, 0xe8, 0x87, 0xfb, 0x01, 0x62, 0x01, 0x55, 0xee, 0x04,
  46003. 0x14, 0x03, 0xde, 0x50, 0x35, 0x56, 0xd1, 0x4c, 0xbb, 0x66, 0xf0, 0xa3,
  46004. 0xe2, 0x1b, 0x1b, 0xc3, 0x97, 0xb2, 0x3d, 0xd1, 0x55, 0x02, 0x10, 0x01,
  46005. 0xfd, 0xa3, 0xeb, 0x6e, 0xca, 0x75, 0xc8, 0x88, 0x43, 0x8b, 0x72, 0x4b,
  46006. 0xcf, 0xbc, 0x91
  46007. };
  46008. rawCertIdPtr = &rawCertId[0];
  46009. /* If the cert ID is NULL the function should allocate it and copy the
  46010. * data to it. */
  46011. certId = NULL;
  46012. ExpectNotNull(certId = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  46013. sizeof(rawCertId)));
  46014. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  46015. if (certId != NULL) {
  46016. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  46017. XFREE(certId, NULL, DYNAMIC_TYPE_OPENSSL);
  46018. }
  46019. /* If the cert ID is not NULL the function will just copy the data to it. */
  46020. ExpectNotNull(certId = (WOLFSSL_OCSP_CERTID*)XMALLOC(sizeof(*certId), NULL,
  46021. DYNAMIC_TYPE_TMP_BUFFER));
  46022. ExpectNotNull(certId);
  46023. ExpectNotNull(XMEMSET(certId, 0, sizeof(*certId)));
  46024. /* Reset rawCertIdPtr since it was push forward in the previous call. */
  46025. rawCertIdPtr = &rawCertId[0];
  46026. ExpectNotNull(certIdGood = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr,
  46027. sizeof(rawCertId)));
  46028. ExpectPtrEq(certIdGood, certId);
  46029. ExpectIntEQ(certId->rawCertIdSize, sizeof(rawCertId));
  46030. if (certId != NULL) {
  46031. XFREE(certId->rawCertId, NULL, DYNAMIC_TYPE_OPENSSL);
  46032. XFREE(certId, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  46033. certId = NULL;
  46034. }
  46035. /* The below tests should fail when passed bad parameters. NULL should
  46036. * always be returned. */
  46037. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(NULL, &rawCertIdPtr,
  46038. sizeof(rawCertId)));
  46039. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, NULL,
  46040. sizeof(rawCertId)));
  46041. ExpectNull(certIdBad = wolfSSL_d2i_OCSP_CERTID(&certId, &rawCertIdPtr, 0));
  46042. #endif
  46043. return EXPECT_RESULT();
  46044. }
  46045. static int test_wolfSSL_OCSP_id_cmp(void)
  46046. {
  46047. EXPECT_DECLS;
  46048. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  46049. OCSP_CERTID id1;
  46050. OCSP_CERTID id2;
  46051. XMEMSET(&id1, 0, sizeof(id1));
  46052. XMEMSET(&id2, 0, sizeof(id2));
  46053. ExpectIntEQ(OCSP_id_cmp(&id1, &id2), 0);
  46054. ExpectIntNE(OCSP_id_cmp(NULL, NULL), 0);
  46055. ExpectIntNE(OCSP_id_cmp(&id1, NULL), 0);
  46056. ExpectIntNE(OCSP_id_cmp(NULL, &id2), 0);
  46057. #endif
  46058. return EXPECT_RESULT();
  46059. }
  46060. static int test_wolfSSL_OCSP_SINGLERESP_get0_id(void)
  46061. {
  46062. EXPECT_DECLS;
  46063. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  46064. WOLFSSL_OCSP_SINGLERESP single;
  46065. const WOLFSSL_OCSP_CERTID* certId;
  46066. XMEMSET(&single, 0, sizeof(single));
  46067. certId = wolfSSL_OCSP_SINGLERESP_get0_id(&single);
  46068. ExpectPtrEq(&single, certId);
  46069. ExpectNull(wolfSSL_OCSP_SINGLERESP_get0_id(NULL));
  46070. #endif
  46071. return EXPECT_RESULT();
  46072. }
  46073. static int test_wolfSSL_OCSP_single_get0_status(void)
  46074. {
  46075. EXPECT_DECLS;
  46076. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  46077. WOLFSSL_OCSP_SINGLERESP single;
  46078. CertStatus certStatus;
  46079. WOLFSSL_ASN1_TIME* thisDate;
  46080. WOLFSSL_ASN1_TIME* nextDate;
  46081. int ret, i;
  46082. XMEMSET(&single, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  46083. XMEMSET(&certStatus, 0, sizeof(CertStatus));
  46084. /* Fill the date fields with some dummy data. */
  46085. for (i = 0; i < CTC_DATE_SIZE; ++i) {
  46086. certStatus.thisDateParsed.data[i] = i;
  46087. certStatus.nextDateParsed.data[i] = i;
  46088. }
  46089. certStatus.status = CERT_GOOD;
  46090. single.status = &certStatus;
  46091. ret = wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, &thisDate,
  46092. &nextDate);
  46093. ExpectIntEQ(ret, CERT_GOOD);
  46094. ExpectPtrEq(thisDate, &certStatus.thisDateParsed);
  46095. ExpectPtrEq(nextDate, &certStatus.nextDateParsed);
  46096. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(NULL, NULL, NULL, NULL, NULL),
  46097. CERT_GOOD);
  46098. ExpectIntEQ(wolfSSL_OCSP_single_get0_status(&single, NULL, NULL, NULL,
  46099. NULL), CERT_GOOD);
  46100. #endif
  46101. return EXPECT_RESULT();
  46102. }
  46103. static int test_wolfSSL_OCSP_resp_count(void)
  46104. {
  46105. EXPECT_DECLS;
  46106. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  46107. WOLFSSL_OCSP_BASICRESP basicResp;
  46108. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  46109. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  46110. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  46111. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  46112. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  46113. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 0);
  46114. basicResp.single = &singleRespOne;
  46115. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 1);
  46116. singleRespOne.next = &singleRespTwo;
  46117. ExpectIntEQ(wolfSSL_OCSP_resp_count(&basicResp), 2);
  46118. #endif
  46119. return EXPECT_RESULT();
  46120. }
  46121. static int test_wolfSSL_OCSP_resp_get0(void)
  46122. {
  46123. EXPECT_DECLS;
  46124. #if defined(OPENSSL_ALL) && defined(HAVE_OCSP)
  46125. WOLFSSL_OCSP_BASICRESP basicResp;
  46126. WOLFSSL_OCSP_SINGLERESP singleRespOne;
  46127. WOLFSSL_OCSP_SINGLERESP singleRespTwo;
  46128. XMEMSET(&basicResp, 0, sizeof(WOLFSSL_OCSP_BASICRESP));
  46129. XMEMSET(&singleRespOne, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  46130. XMEMSET(&singleRespTwo, 0, sizeof(WOLFSSL_OCSP_SINGLERESP));
  46131. basicResp.single = &singleRespOne;
  46132. singleRespOne.next = &singleRespTwo;
  46133. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 0), &singleRespOne);
  46134. ExpectPtrEq(wolfSSL_OCSP_resp_get0(&basicResp, 1), &singleRespTwo);
  46135. #endif
  46136. return EXPECT_RESULT();
  46137. }
  46138. static int test_wolfSSL_EVP_PKEY_derive(void)
  46139. {
  46140. EXPECT_DECLS;
  46141. #if defined(OPENSSL_ALL) || defined(WOLFSSL_QT) || defined(WOLFSSL_OPENSSH)
  46142. #if (!defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)) || defined(HAVE_ECC)
  46143. EVP_PKEY_CTX *ctx = NULL;
  46144. unsigned char *skey = NULL;
  46145. size_t skeylen;
  46146. EVP_PKEY *pkey = NULL;
  46147. EVP_PKEY *peerkey = NULL;
  46148. const unsigned char* key;
  46149. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  46150. /* DH */
  46151. key = dh_key_der_2048;
  46152. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  46153. sizeof_dh_key_der_2048)));
  46154. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(pkey)), 1);
  46155. key = dh_key_der_2048;
  46156. ExpectNotNull((peerkey = d2i_PrivateKey(EVP_PKEY_DH, NULL, &key,
  46157. sizeof_dh_key_der_2048)));
  46158. ExpectIntEQ(DH_generate_key(EVP_PKEY_get0_DH(peerkey)), 1);
  46159. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  46160. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  46161. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  46162. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  46163. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  46164. DYNAMIC_TYPE_OPENSSL));
  46165. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  46166. EVP_PKEY_CTX_free(ctx);
  46167. ctx = NULL;
  46168. EVP_PKEY_free(peerkey);
  46169. peerkey = NULL;
  46170. EVP_PKEY_free(pkey);
  46171. pkey = NULL;
  46172. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  46173. skey = NULL;
  46174. #endif
  46175. #ifdef HAVE_ECC
  46176. /* ECDH */
  46177. key = ecc_clikey_der_256;
  46178. ExpectNotNull((pkey = d2i_PrivateKey(EVP_PKEY_EC, NULL, &key,
  46179. sizeof_ecc_clikey_der_256)));
  46180. key = ecc_clikeypub_der_256;
  46181. ExpectNotNull((peerkey = d2i_PUBKEY(NULL, &key,
  46182. sizeof_ecc_clikeypub_der_256)));
  46183. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  46184. ExpectIntEQ(EVP_PKEY_derive_init(ctx), 1);
  46185. ExpectIntEQ(EVP_PKEY_derive_set_peer(ctx, peerkey), 1);
  46186. ExpectIntEQ(EVP_PKEY_derive(ctx, NULL, &skeylen), 1);
  46187. ExpectNotNull(skey = (unsigned char*)XMALLOC(skeylen, NULL,
  46188. DYNAMIC_TYPE_OPENSSL));
  46189. ExpectIntEQ(EVP_PKEY_derive(ctx, skey, &skeylen), 1);
  46190. EVP_PKEY_CTX_free(ctx);
  46191. EVP_PKEY_free(peerkey);
  46192. EVP_PKEY_free(pkey);
  46193. XFREE(skey, NULL, DYNAMIC_TYPE_OPENSSL);
  46194. #endif /* HAVE_ECC */
  46195. #endif /* (!NO_DH && WOLFSSL_DH_EXTRA) || HAVE_ECC */
  46196. #endif /* OPENSSL_ALL || WOLFSSL_QT || WOLFSSL_OPENSSH */
  46197. return EXPECT_RESULT();
  46198. }
  46199. static int test_wolfSSL_EVP_PBE_scrypt(void)
  46200. {
  46201. EXPECT_DECLS;
  46202. #if defined(OPENSSL_EXTRA) && defined(HAVE_SCRYPT) && defined(HAVE_PBKDF2) && \
  46203. (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 5))
  46204. #if !defined(NO_PWDBASED) && !defined(NO_SHA256)
  46205. int ret;
  46206. const char pwd[] = {'p','a','s','s','w','o','r','d'};
  46207. int pwdlen = sizeof(pwd);
  46208. const byte salt[] = {'N','a','C','l'};
  46209. int saltlen = sizeof(salt);
  46210. byte key[80];
  46211. word64 numOvr32 = (word64)INT32_MAX + 1;
  46212. /* expected derived key for N:16, r:1, p:1 */
  46213. const byte expectedKey[] = {
  46214. 0xAE, 0xC6, 0xB7, 0x48, 0x3E, 0xD2, 0x6E, 0x08, 0x80, 0x2B,
  46215. 0x41, 0xF4, 0x03, 0x20, 0x86, 0xA0, 0xE8, 0x86, 0xBE, 0x7A,
  46216. 0xC4, 0x8F, 0xCF, 0xD9, 0x2F, 0xF0, 0xCE, 0xF8, 0x10, 0x97,
  46217. 0x52, 0xF4, 0xAC, 0x74, 0xB0, 0x77, 0x26, 0x32, 0x56, 0xA6,
  46218. 0x5A, 0x99, 0x70, 0x1B, 0x7A, 0x30, 0x4D, 0x46, 0x61, 0x1C,
  46219. 0x8A, 0xA3, 0x91, 0xE7, 0x99, 0xCE, 0x10, 0xA2, 0x77, 0x53,
  46220. 0xE7, 0xE9, 0xC0, 0x9A};
  46221. /* N r p mx key keylen */
  46222. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 0, 1, 1, 0, key, 64);
  46223. ExpectIntEQ(ret, 0); /* N must be greater than 1 */
  46224. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 3, 1, 1, 0, key, 64);
  46225. ExpectIntEQ(ret, 0); /* N must be power of 2 */
  46226. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 0, 1, 0, key, 64);
  46227. ExpectIntEQ(ret, 0); /* r must be greater than 0 */
  46228. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 0, 0, key, 64);
  46229. ExpectIntEQ(ret, 0); /* p must be greater than 0 */
  46230. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 0);
  46231. ExpectIntEQ(ret, 0); /* keylen must be greater than 0 */
  46232. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 9, 1, 0, key, 64);
  46233. ExpectIntEQ(ret, 0); /* r must be smaller than 9 */
  46234. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, NULL, 64);
  46235. ExpectIntEQ(ret, 1); /* should succeed if key is NULL */
  46236. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, 1, 0, key, 64);
  46237. ExpectIntEQ(ret, 1); /* should succeed */
  46238. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, numOvr32, 1, 0,
  46239. key, 64);
  46240. ExpectIntEQ(ret, 0); /* should fail since r is greater than INT32_MAC */
  46241. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 2, 1, numOvr32, 0,
  46242. key, 64);
  46243. ExpectIntEQ(ret, 0); /* should fail since p is greater than INT32_MAC */
  46244. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 0, 2, 1, 1, 0, key, 64);
  46245. ExpectIntEQ(ret, 1); /* should succeed even if salt is NULL */
  46246. ret = EVP_PBE_scrypt(pwd, pwdlen, NULL, 4, 2, 1, 1, 0, key, 64);
  46247. ExpectIntEQ(ret, 0); /* if salt is NULL, saltlen must be 0, otherwise fail*/
  46248. ret = EVP_PBE_scrypt(NULL, 0, salt, saltlen, 2, 1, 1, 0, key, 64);
  46249. ExpectIntEQ(ret, 1); /* should succeed if pwd is NULL and pwdlen is 0*/
  46250. ret = EVP_PBE_scrypt(NULL, 4, salt, saltlen, 2, 1, 1, 0, key, 64);
  46251. ExpectIntEQ(ret, 0); /* if pwd is NULL, pwdlen must be 0 */
  46252. ret = EVP_PBE_scrypt(NULL, 0, NULL, 0, 2, 1, 1, 0, key, 64);
  46253. ExpectIntEQ(ret, 1); /* should succeed even both pwd and salt are NULL */
  46254. ret = EVP_PBE_scrypt(pwd, pwdlen, salt, saltlen, 16, 1, 1, 0, key, 64);
  46255. ExpectIntEQ(ret, 1);
  46256. ret = XMEMCMP(expectedKey, key, sizeof(expectedKey));
  46257. ExpectIntEQ(ret, 0); /* derived key must be the same as expected-key */
  46258. #endif /* !NO_PWDBASED && !NO_SHA256 */
  46259. #endif /* OPENSSL_EXTRA && HAVE_SCRYPT && HAVE_PBKDF2 */
  46260. return EXPECT_RESULT();
  46261. }
  46262. static int test_no_op_functions(void)
  46263. {
  46264. EXPECT_DECLS;
  46265. #if defined(OPENSSL_EXTRA)
  46266. /* this makes sure wolfSSL can compile and run these no-op functions */
  46267. SSL_load_error_strings();
  46268. ENGINE_load_builtin_engines();
  46269. OpenSSL_add_all_ciphers();
  46270. ExpectIntEQ(CRYPTO_malloc_init(), 0);
  46271. #endif
  46272. return EXPECT_RESULT();
  46273. }
  46274. static int test_wolfSSL_CRYPTO_memcmp(void)
  46275. {
  46276. EXPECT_DECLS;
  46277. #ifdef OPENSSL_EXTRA
  46278. char a[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  46279. "implementation of TLS/SSL for embedded devices to the cloud.";
  46280. char b[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  46281. "implementation of TLS/SSL for embedded devices to the cloud.";
  46282. char c[] = "wolfSSL (formerly CyaSSL) is a small, fast, portable "
  46283. "implementation of TLS/SSL for embedded devices to the cloud!";
  46284. ExpectIntEQ(CRYPTO_memcmp(a, b, sizeof(a)), 0);
  46285. ExpectIntNE(CRYPTO_memcmp(a, c, sizeof(a)), 0);
  46286. #endif
  46287. return EXPECT_RESULT();
  46288. }
  46289. /*----------------------------------------------------------------------------*
  46290. | wolfCrypt ASN
  46291. *----------------------------------------------------------------------------*/
  46292. static int test_wc_CreateEncryptedPKCS8Key(void)
  46293. {
  46294. EXPECT_DECLS;
  46295. #if defined(HAVE_PKCS8) && !defined(NO_PWDBASED) && defined(WOLFSSL_AES_256) \
  46296. && !defined(NO_AES_CBC) && !defined(NO_RSA) && !defined(NO_SHA)
  46297. WC_RNG rng;
  46298. byte* encKey = NULL;
  46299. word32 encKeySz = 0;
  46300. word32 decKeySz = 0;
  46301. const char password[] = "Lorem ipsum dolor sit amet";
  46302. word32 passwordSz = (word32)XSTRLEN(password);
  46303. word32 tradIdx = 0;
  46304. XMEMSET(&rng, 0, sizeof(WC_RNG));
  46305. ExpectIntEQ(wc_InitRng(&rng), 0);
  46306. PRIVATE_KEY_UNLOCK();
  46307. /* Call with NULL for out buffer to get necessary length. */
  46308. ExpectIntEQ(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  46309. sizeof_server_key_der_2048, NULL, &encKeySz, password, (int)passwordSz,
  46310. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  46311. LENGTH_ONLY_E);
  46312. ExpectNotNull(encKey = (byte*)XMALLOC(encKeySz, HEAP_HINT,
  46313. DYNAMIC_TYPE_TMP_BUFFER));
  46314. /* Call with the allocated out buffer. */
  46315. ExpectIntGT(wc_CreateEncryptedPKCS8Key((byte*)server_key_der_2048,
  46316. sizeof_server_key_der_2048, encKey, &encKeySz, password, (int)passwordSz,
  46317. PKCS5, PBES2, AES256CBCb, NULL, 0, WC_PKCS12_ITT_DEFAULT, &rng, NULL),
  46318. 0);
  46319. /* Decrypt the encrypted PKCS8 key we just made. */
  46320. ExpectIntGT((decKeySz = (word32)wc_DecryptPKCS8Key(encKey, encKeySz, password,
  46321. (int)passwordSz)), 0);
  46322. /* encKey now holds the decrypted key (decrypted in place). */
  46323. ExpectIntGT(wc_GetPkcs8TraditionalOffset(encKey, &tradIdx, decKeySz), 0);
  46324. /* Check that the decrypted key matches the key prior to encryption. */
  46325. ExpectIntEQ(XMEMCMP(encKey + tradIdx, server_key_der_2048,
  46326. sizeof_server_key_der_2048), 0);
  46327. PRIVATE_KEY_LOCK();
  46328. XFREE(encKey, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46329. wc_FreeRng(&rng);
  46330. #endif
  46331. return EXPECT_RESULT();
  46332. }
  46333. static int test_wc_GetPkcs8TraditionalOffset(void)
  46334. {
  46335. EXPECT_DECLS;
  46336. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(HAVE_PKCS8)
  46337. int length;
  46338. int derSz = 0;
  46339. word32 inOutIdx;
  46340. const char* path = "./certs/server-keyPkcs8.der";
  46341. XFILE file = XBADFILE;
  46342. byte der[2048];
  46343. ExpectTrue((file = XFOPEN(path, "rb")) != XBADFILE);
  46344. ExpectIntGT(derSz = (int)XFREAD(der, 1, sizeof(der), file), 0);
  46345. if (file != XBADFILE)
  46346. XFCLOSE(file);
  46347. /* valid case */
  46348. inOutIdx = 0;
  46349. ExpectIntGT(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, (word32)derSz),
  46350. 0);
  46351. /* inOutIdx > sz */
  46352. inOutIdx = 4000;
  46353. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, (word32)derSz),
  46354. BAD_FUNC_ARG);
  46355. /* null input */
  46356. inOutIdx = 0;
  46357. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(NULL, &inOutIdx, 0),
  46358. BAD_FUNC_ARG);
  46359. /* invalid input, fill buffer with 1's */
  46360. XMEMSET(der, 1, sizeof(der));
  46361. inOutIdx = 0;
  46362. ExpectIntEQ(length = wc_GetPkcs8TraditionalOffset(der, &inOutIdx, (word32)derSz),
  46363. ASN_PARSE_E);
  46364. #endif /* NO_ASN */
  46365. return EXPECT_RESULT();
  46366. }
  46367. static int test_wc_SetSubjectRaw(void)
  46368. {
  46369. EXPECT_DECLS;
  46370. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46371. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  46372. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  46373. WOLFSSL_X509* x509 = NULL;
  46374. int peerCertSz;
  46375. const byte* peerCertBuf = NULL;
  46376. Cert forgedCert;
  46377. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  46378. WOLFSSL_FILETYPE_ASN1));
  46379. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  46380. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  46381. ExpectIntEQ(0, wc_SetSubjectRaw(&forgedCert, peerCertBuf, peerCertSz));
  46382. wolfSSL_FreeX509(x509);
  46383. #endif
  46384. return EXPECT_RESULT();
  46385. }
  46386. static int test_wc_GetSubjectRaw(void)
  46387. {
  46388. EXPECT_DECLS;
  46389. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46390. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT)
  46391. Cert cert;
  46392. byte *subjectRaw;
  46393. ExpectIntEQ(0, wc_InitCert(&cert));
  46394. ExpectIntEQ(0, wc_GetSubjectRaw(&subjectRaw, &cert));
  46395. #endif
  46396. return EXPECT_RESULT();
  46397. }
  46398. static int test_wc_SetIssuerRaw(void)
  46399. {
  46400. EXPECT_DECLS;
  46401. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46402. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  46403. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  46404. WOLFSSL_X509* x509 = NULL;
  46405. int peerCertSz;
  46406. const byte* peerCertBuf;
  46407. Cert forgedCert;
  46408. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  46409. WOLFSSL_FILETYPE_ASN1));
  46410. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  46411. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  46412. ExpectIntEQ(0, wc_SetIssuerRaw(&forgedCert, peerCertBuf, peerCertSz));
  46413. wolfSSL_FreeX509(x509);
  46414. #endif
  46415. return EXPECT_RESULT();
  46416. }
  46417. static int test_wc_SetIssueBuffer(void)
  46418. {
  46419. EXPECT_DECLS;
  46420. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46421. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && !defined(NO_RSA)
  46422. const char* joiCertFile = "./certs/test/cert-ext-joi.der";
  46423. WOLFSSL_X509* x509 = NULL;
  46424. int peerCertSz;
  46425. const byte* peerCertBuf;
  46426. Cert forgedCert;
  46427. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(joiCertFile,
  46428. WOLFSSL_FILETYPE_ASN1));
  46429. ExpectNotNull(peerCertBuf = wolfSSL_X509_get_der(x509, &peerCertSz));
  46430. ExpectIntEQ(0, wc_InitCert(&forgedCert));
  46431. ExpectIntEQ(0, wc_SetIssuerBuffer(&forgedCert, peerCertBuf, peerCertSz));
  46432. wolfSSL_FreeX509(x509);
  46433. #endif
  46434. return EXPECT_RESULT();
  46435. }
  46436. /*
  46437. * Testing wc_SetSubjectKeyId
  46438. */
  46439. static int test_wc_SetSubjectKeyId(void)
  46440. {
  46441. EXPECT_DECLS;
  46442. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46443. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  46444. Cert cert;
  46445. const char* file = "certs/ecc-client-keyPub.pem";
  46446. ExpectIntEQ(0, wc_InitCert(&cert));
  46447. ExpectIntEQ(0, wc_SetSubjectKeyId(&cert, file));
  46448. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubjectKeyId(NULL, file));
  46449. ExpectIntGT(0, wc_SetSubjectKeyId(&cert, "badfile.name"));
  46450. #endif
  46451. return EXPECT_RESULT();
  46452. } /* END test_wc_SetSubjectKeyId */
  46453. /*
  46454. * Testing wc_SetSubject
  46455. */
  46456. static int test_wc_SetSubject(void)
  46457. {
  46458. EXPECT_DECLS;
  46459. #if !defined(NO_ASN) && !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && \
  46460. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_EXT) && defined(HAVE_ECC)
  46461. Cert cert;
  46462. const char* file = "./certs/ca-ecc-cert.pem";
  46463. ExpectIntEQ(0, wc_InitCert(&cert));
  46464. ExpectIntEQ(0, wc_SetSubject(&cert, file));
  46465. ExpectIntEQ(BAD_FUNC_ARG, wc_SetSubject(NULL, file));
  46466. ExpectIntGT(0, wc_SetSubject(&cert, "badfile.name"));
  46467. #endif
  46468. return EXPECT_RESULT();
  46469. } /* END test_wc_SetSubject */
  46470. static int test_CheckCertSignature(void)
  46471. {
  46472. EXPECT_DECLS;
  46473. #if !defined(NO_CERTS) && defined(WOLFSSL_SMALL_CERT_VERIFY)
  46474. WOLFSSL_CERT_MANAGER* cm = NULL;
  46475. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  46476. XFILE fp = XBADFILE;
  46477. byte cert[4096];
  46478. int certSz;
  46479. #endif
  46480. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, NULL));
  46481. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  46482. ExpectIntEQ(BAD_FUNC_ARG, CheckCertSignature(NULL, 0, NULL, cm));
  46483. #ifndef NO_RSA
  46484. #ifdef USE_CERT_BUFFERS_1024
  46485. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_1024,
  46486. sizeof_server_cert_der_1024, NULL, cm));
  46487. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  46488. ca_cert_der_1024, sizeof_ca_cert_der_1024,
  46489. WOLFSSL_FILETYPE_ASN1));
  46490. ExpectIntEQ(0, CheckCertSignature(server_cert_der_1024,
  46491. sizeof_server_cert_der_1024, NULL, cm));
  46492. #elif defined(USE_CERT_BUFFERS_2048)
  46493. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(server_cert_der_2048,
  46494. sizeof_server_cert_der_2048, NULL, cm));
  46495. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  46496. ca_cert_der_2048, sizeof_ca_cert_der_2048,
  46497. WOLFSSL_FILETYPE_ASN1));
  46498. ExpectIntEQ(0, CheckCertSignature(server_cert_der_2048,
  46499. sizeof_server_cert_der_2048, NULL, cm));
  46500. #endif
  46501. #endif
  46502. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  46503. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(serv_ecc_der_256,
  46504. sizeof_serv_ecc_der_256, NULL, cm));
  46505. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCABuffer(cm,
  46506. ca_ecc_cert_der_256, sizeof_ca_ecc_cert_der_256,
  46507. WOLFSSL_FILETYPE_ASN1));
  46508. ExpectIntEQ(0, CheckCertSignature(serv_ecc_der_256, sizeof_serv_ecc_der_256,
  46509. NULL, cm));
  46510. #endif
  46511. #if !defined(NO_FILESYSTEM)
  46512. wolfSSL_CertManagerFree(cm);
  46513. cm = NULL;
  46514. ExpectNotNull(cm = wolfSSL_CertManagerNew_ex(NULL));
  46515. #ifndef NO_RSA
  46516. ExpectTrue((fp = XFOPEN("./certs/server-cert.der", "rb")) != XBADFILE);
  46517. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  46518. if (fp != XBADFILE) {
  46519. XFCLOSE(fp);
  46520. fp = XBADFILE;
  46521. }
  46522. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  46523. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  46524. "./certs/ca-cert.pem", NULL));
  46525. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  46526. #endif
  46527. #ifdef HAVE_ECC
  46528. ExpectTrue((fp = XFOPEN("./certs/server-ecc.der", "rb")) != XBADFILE);
  46529. ExpectIntGT((certSz = (int)XFREAD(cert, 1, sizeof(cert), fp)), 0);
  46530. if (fp != XBADFILE) {
  46531. XFCLOSE(fp);
  46532. fp = XBADFILE;
  46533. }
  46534. ExpectIntEQ(ASN_NO_SIGNER_E, CheckCertSignature(cert, certSz, NULL, cm));
  46535. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CertManagerLoadCA(cm,
  46536. "./certs/ca-ecc-cert.pem", NULL));
  46537. ExpectIntEQ(0, CheckCertSignature(cert, certSz, NULL, cm));
  46538. #endif
  46539. #endif
  46540. #if !defined(NO_FILESYSTEM) && (!defined(NO_RSA) || defined(HAVE_ECC))
  46541. (void)fp;
  46542. (void)cert;
  46543. (void)certSz;
  46544. #endif
  46545. wolfSSL_CertManagerFree(cm);
  46546. #endif
  46547. return EXPECT_RESULT();
  46548. }
  46549. static int test_wc_ParseCert(void)
  46550. {
  46551. EXPECT_DECLS;
  46552. #if !defined(NO_CERTS) && !defined(NO_RSA)
  46553. DecodedCert decodedCert;
  46554. const byte* rawCert = client_cert_der_2048;
  46555. const int rawCertSize = sizeof_client_cert_der_2048;
  46556. wc_InitDecodedCert(&decodedCert, rawCert, rawCertSize, NULL);
  46557. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  46558. #ifndef IGNORE_NAME_CONSTRAINTS
  46559. /* check that the subjects emailAddress was not put in the alt name list */
  46560. ExpectNotNull(decodedCert.subjectEmail);
  46561. ExpectNull(decodedCert.altEmailNames);
  46562. #endif
  46563. wc_FreeDecodedCert(&decodedCert);
  46564. #endif
  46565. return EXPECT_RESULT();
  46566. }
  46567. /* Test wc_ParseCert decoding of various encodings and scenarios ensuring that
  46568. * the API safely errors out on badly-formed ASN input.
  46569. * NOTE: Test not compatible with released FIPS implementations!
  46570. */
  46571. static int test_wc_ParseCert_Error(void)
  46572. {
  46573. EXPECT_DECLS;
  46574. #if !defined(NO_CERTS) && !defined(NO_RSA) && !defined(HAVE_SELFTEST) && \
  46575. (!defined(HAVE_FIPS) || \
  46576. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)))
  46577. DecodedCert decodedCert;
  46578. int i;
  46579. /* Certificate data */
  46580. const byte c0[] = { 0x30, 0x04, 0x30, 0x02, 0x02, 0x80, 0x00, 0x00};
  46581. const byte c1[] = { 0x30, 0x04, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  46582. const byte c2[] = { 0x30, 0x06, 0x30, 0x04, 0x02, 0x80, 0x00, 0x00};
  46583. const byte c3[] = { 0x30, 0x07, 0x30, 0x05, 0x02, 0x80, 0x10, 0x00, 0x00};
  46584. const byte c4[] = { 0x02, 0x80, 0x10, 0x00, 0x00};
  46585. /* Test data */
  46586. const struct testStruct {
  46587. const byte* c;
  46588. const int cSz;
  46589. const int expRet;
  46590. } t[] = {
  46591. {c0, sizeof(c0), ASN_PARSE_E}, /* Invalid bit-string length */
  46592. {c1, sizeof(c1), ASN_PARSE_E}, /* Invalid bit-string length */
  46593. {c2, sizeof(c2), ASN_PARSE_E}, /* Invalid integer length (zero) */
  46594. {c3, sizeof(c3), ASN_PARSE_E}, /* Valid INTEGER, but buffer too short */
  46595. {c4, sizeof(c4), ASN_PARSE_E}, /* Valid INTEGER, but not in bit-string */
  46596. };
  46597. const int tSz = (int)(sizeof(t) / sizeof(struct testStruct));
  46598. for (i = 0; i < tSz; i++) {
  46599. WOLFSSL_MSG_EX("i == %d", i);
  46600. wc_InitDecodedCert(&decodedCert, t[i].c, t[i].cSz, NULL);
  46601. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), t[i].expRet);
  46602. wc_FreeDecodedCert(&decodedCert);
  46603. }
  46604. #endif
  46605. return EXPECT_RESULT();
  46606. }
  46607. static int test_MakeCertWithPathLen(void)
  46608. {
  46609. EXPECT_DECLS;
  46610. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME) && \
  46611. defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  46612. const byte expectedPathLen = 7;
  46613. Cert cert;
  46614. DecodedCert decodedCert;
  46615. byte der[FOURK_BUF];
  46616. int derSize = 0;
  46617. WC_RNG rng;
  46618. ecc_key key;
  46619. int ret;
  46620. XMEMSET(&rng, 0, sizeof(WC_RNG));
  46621. XMEMSET(&key, 0, sizeof(ecc_key));
  46622. XMEMSET(&cert, 0, sizeof(Cert));
  46623. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  46624. ExpectIntEQ(wc_InitRng(&rng), 0);
  46625. ExpectIntEQ(wc_ecc_init(&key), 0);
  46626. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  46627. ExpectIntEQ(wc_InitCert(&cert), 0);
  46628. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  46629. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  46630. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  46631. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  46632. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  46633. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  46634. CTC_NAME_SIZE);
  46635. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  46636. CTC_NAME_SIZE);
  46637. cert.selfSigned = 1;
  46638. cert.isCA = 1;
  46639. cert.pathLen = expectedPathLen;
  46640. cert.pathLenSet = 1;
  46641. cert.sigType = CTC_SHA256wECDSA;
  46642. #ifdef WOLFSSL_CERT_EXT
  46643. cert.keyUsage |= KEYUSE_KEY_CERT_SIGN;
  46644. #endif
  46645. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  46646. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  46647. FOURK_BUF, NULL, &key, &rng), 0);
  46648. wc_InitDecodedCert(&decodedCert, der, (word32)derSize, NULL);
  46649. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  46650. ExpectIntEQ(decodedCert.pathLength, expectedPathLen);
  46651. wc_FreeDecodedCert(&decodedCert);
  46652. ret = wc_ecc_free(&key);
  46653. ExpectIntEQ(ret, 0);
  46654. ret = wc_FreeRng(&rng);
  46655. ExpectIntEQ(ret, 0);
  46656. #endif
  46657. return EXPECT_RESULT();
  46658. }
  46659. static int test_MakeCertWithCaFalse(void)
  46660. {
  46661. EXPECT_DECLS;
  46662. #if defined(WOLFSSL_ALLOW_ENCODING_CA_FALSE) && defined(WOLFSSL_CERT_REQ) && \
  46663. !defined(NO_ASN_TIME) && defined(WOLFSSL_CERT_GEN) && defined(HAVE_ECC)
  46664. const byte expectedIsCa = 0;
  46665. Cert cert;
  46666. DecodedCert decodedCert;
  46667. byte der[FOURK_BUF];
  46668. int derSize = 0;
  46669. WC_RNG rng;
  46670. ecc_key key;
  46671. int ret;
  46672. XMEMSET(&rng, 0, sizeof(WC_RNG));
  46673. XMEMSET(&key, 0, sizeof(ecc_key));
  46674. XMEMSET(&cert, 0, sizeof(Cert));
  46675. XMEMSET(&decodedCert, 0, sizeof(DecodedCert));
  46676. ExpectIntEQ(wc_InitRng(&rng), 0);
  46677. ExpectIntEQ(wc_ecc_init(&key), 0);
  46678. ExpectIntEQ(wc_ecc_make_key(&rng, 32, &key), 0);
  46679. ExpectIntEQ(wc_InitCert(&cert), 0);
  46680. (void)XSTRNCPY(cert.subject.country, "US", CTC_NAME_SIZE);
  46681. (void)XSTRNCPY(cert.subject.state, "state", CTC_NAME_SIZE);
  46682. (void)XSTRNCPY(cert.subject.locality, "Bozeman", CTC_NAME_SIZE);
  46683. (void)XSTRNCPY(cert.subject.org, "yourOrgNameHere", CTC_NAME_SIZE);
  46684. (void)XSTRNCPY(cert.subject.unit, "yourUnitNameHere", CTC_NAME_SIZE);
  46685. (void)XSTRNCPY(cert.subject.commonName, "www.yourDomain.com",
  46686. CTC_NAME_SIZE);
  46687. (void)XSTRNCPY(cert.subject.email, "yourEmail@yourDomain.com",
  46688. CTC_NAME_SIZE);
  46689. cert.selfSigned = 1;
  46690. cert.isCA = expectedIsCa;
  46691. cert.isCaSet = 1;
  46692. cert.sigType = CTC_SHA256wECDSA;
  46693. ExpectIntGE(wc_MakeCert(&cert, der, FOURK_BUF, NULL, &key, &rng), 0);
  46694. ExpectIntGE(derSize = wc_SignCert(cert.bodySz, cert.sigType, der,
  46695. FOURK_BUF, NULL, &key, &rng), 0);
  46696. wc_InitDecodedCert(&decodedCert, der, derSize, NULL);
  46697. ExpectIntEQ(wc_ParseCert(&decodedCert, CERT_TYPE, NO_VERIFY, NULL), 0);
  46698. ExpectIntEQ(decodedCert.isCA, expectedIsCa);
  46699. wc_FreeDecodedCert(&decodedCert);
  46700. ret = wc_ecc_free(&key);
  46701. ExpectIntEQ(ret, 0);
  46702. ret = wc_FreeRng(&rng);
  46703. ExpectIntEQ(ret, 0);
  46704. #endif
  46705. return EXPECT_RESULT();
  46706. }
  46707. /*----------------------------------------------------------------------------*
  46708. | wolfCrypt ECC
  46709. *----------------------------------------------------------------------------*/
  46710. static int test_wc_ecc_get_curve_size_from_name(void)
  46711. {
  46712. EXPECT_DECLS;
  46713. #ifdef HAVE_ECC
  46714. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46715. ExpectIntEQ(wc_ecc_get_curve_size_from_name("SECP256R1"), 32);
  46716. #endif
  46717. /* invalid case */
  46718. ExpectIntEQ(wc_ecc_get_curve_size_from_name("BADCURVE"), -1);
  46719. /* NULL input */
  46720. ExpectIntEQ(wc_ecc_get_curve_size_from_name(NULL), BAD_FUNC_ARG);
  46721. #endif /* HAVE_ECC */
  46722. return EXPECT_RESULT();
  46723. }
  46724. static int test_wc_ecc_get_curve_id_from_name(void)
  46725. {
  46726. EXPECT_DECLS;
  46727. #ifdef HAVE_ECC
  46728. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46729. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"),
  46730. ECC_SECP256R1);
  46731. #endif
  46732. /* invalid case */
  46733. ExpectIntEQ(wc_ecc_get_curve_id_from_name("BADCURVE"), -1);
  46734. /* NULL input */
  46735. ExpectIntEQ(wc_ecc_get_curve_id_from_name(NULL), BAD_FUNC_ARG);
  46736. #endif /* HAVE_ECC */
  46737. return EXPECT_RESULT();
  46738. }
  46739. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  46740. !defined(HAVE_SELFTEST) && \
  46741. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  46742. static int test_wc_ecc_get_curve_id_from_dp_params(void)
  46743. {
  46744. EXPECT_DECLS;
  46745. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46746. ecc_key* key;
  46747. const ecc_set_type* params = NULL;
  46748. int ret;
  46749. #endif
  46750. WOLFSSL_EC_KEY *ecKey = NULL;
  46751. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46752. ExpectIntEQ(wc_ecc_get_curve_id_from_name("SECP256R1"), ECC_SECP256R1);
  46753. ExpectNotNull(ecKey = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  46754. if (EXPECT_SUCCESS()) {
  46755. ret = EC_KEY_generate_key(ecKey);
  46756. } else
  46757. ret = 0;
  46758. if (ret == 1) {
  46759. /* normal test */
  46760. key = (ecc_key*)ecKey->internal;
  46761. if (key != NULL) {
  46762. params = key->dp;
  46763. }
  46764. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(params),
  46765. ECC_SECP256R1);
  46766. }
  46767. #endif
  46768. /* invalid case, NULL input*/
  46769. ExpectIntEQ(wc_ecc_get_curve_id_from_dp_params(NULL), BAD_FUNC_ARG);
  46770. wolfSSL_EC_KEY_free(ecKey);
  46771. return EXPECT_RESULT();
  46772. }
  46773. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  46774. static int test_wc_ecc_get_curve_id_from_params(void)
  46775. {
  46776. EXPECT_DECLS;
  46777. #ifdef HAVE_ECC
  46778. const byte prime[] =
  46779. {
  46780. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  46781. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46782. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  46783. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF
  46784. };
  46785. const byte primeInvalid[] =
  46786. {
  46787. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  46788. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46789. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  46790. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x01,0x01
  46791. };
  46792. const byte Af[] =
  46793. {
  46794. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x01,
  46795. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  46796. 0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,
  46797. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFC
  46798. };
  46799. const byte Bf[] =
  46800. {
  46801. 0x5A,0xC6,0x35,0xD8,0xAA,0x3A,0x93,0xE7,
  46802. 0xB3,0xEB,0xBD,0x55,0x76,0x98,0x86,0xBC,
  46803. 0x65,0x1D,0x06,0xB0,0xCC,0x53,0xB0,0xF6,
  46804. 0x3B,0xCE,0x3C,0x3E,0x27,0xD2,0x60,0x4B
  46805. };
  46806. const byte order[] =
  46807. {
  46808. 0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,
  46809. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  46810. 0xBC,0xE6,0xFA,0xAD,0xA7,0x17,0x9E,0x84,
  46811. 0xF3,0xB9,0xCA,0xC2,0xFC,0x63,0x25,0x51
  46812. };
  46813. const byte Gx[] =
  46814. {
  46815. 0x6B,0x17,0xD1,0xF2,0xE1,0x2C,0x42,0x47,
  46816. 0xF8,0xBC,0xE6,0xE5,0x63,0xA4,0x40,0xF2,
  46817. 0x77,0x03,0x7D,0x81,0x2D,0xEB,0x33,0xA0,
  46818. 0xF4,0xA1,0x39,0x45,0xD8,0x98,0xC2,0x96
  46819. };
  46820. const byte Gy[] =
  46821. {
  46822. 0x4F,0xE3,0x42,0xE2,0xFE,0x1A,0x7F,0x9B,
  46823. 0x8E,0xE7,0xEB,0x4A,0x7C,0x0F,0x9E,0x16,
  46824. 0x2B,0xCE,0x33,0x57,0x6B,0x31,0x5E,0xCE,
  46825. 0xCB,0xB6,0x40,0x68,0x37,0xBF,0x51,0xF5
  46826. };
  46827. int cofactor = 1;
  46828. int fieldSize = 256;
  46829. #if !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  46830. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  46831. prime, sizeof(prime), Af, sizeof(Af), Bf, sizeof(Bf),
  46832. order, sizeof(order), Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor),
  46833. ECC_SECP256R1);
  46834. #endif
  46835. /* invalid case, fieldSize = 0 */
  46836. ExpectIntEQ(wc_ecc_get_curve_id_from_params(0, prime, sizeof(prime),
  46837. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  46838. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  46839. /* invalid case, NULL prime */
  46840. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize, NULL, sizeof(prime),
  46841. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  46842. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), BAD_FUNC_ARG);
  46843. /* invalid case, invalid prime */
  46844. ExpectIntEQ(wc_ecc_get_curve_id_from_params(fieldSize,
  46845. primeInvalid, sizeof(primeInvalid),
  46846. Af, sizeof(Af), Bf, sizeof(Bf), order, sizeof(order),
  46847. Gx, sizeof(Gx), Gy, sizeof(Gy), cofactor), ECC_CURVE_INVALID);
  46848. #endif
  46849. return EXPECT_RESULT();
  46850. }
  46851. static int test_wolfSSL_EVP_PKEY_encrypt(void)
  46852. {
  46853. EXPECT_DECLS;
  46854. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  46855. WOLFSSL_RSA* rsa = NULL;
  46856. WOLFSSL_EVP_PKEY* pkey = NULL;
  46857. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  46858. const char* in = "What is easy to do is easy not to do.";
  46859. size_t inlen = XSTRLEN(in);
  46860. size_t outEncLen = 0;
  46861. byte* outEnc = NULL;
  46862. byte* outDec = NULL;
  46863. size_t outDecLen = 0;
  46864. size_t rsaKeySz = 2048/8; /* Bytes */
  46865. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  46866. byte* inTmp = NULL;
  46867. byte* outEncTmp = NULL;
  46868. byte* outDecTmp = NULL;
  46869. #endif
  46870. ExpectNotNull(outEnc = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  46871. DYNAMIC_TYPE_TMP_BUFFER));
  46872. if (outEnc != NULL) {
  46873. XMEMSET(outEnc, 0, rsaKeySz);
  46874. }
  46875. ExpectNotNull(outDec = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  46876. DYNAMIC_TYPE_TMP_BUFFER));
  46877. if (outDec != NULL) {
  46878. XMEMSET(outDec, 0, rsaKeySz);
  46879. }
  46880. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  46881. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  46882. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  46883. if (EXPECT_FAIL()) {
  46884. RSA_free(rsa);
  46885. }
  46886. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  46887. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  46888. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  46889. WOLFSSL_SUCCESS);
  46890. /* Test pkey references count is decremented. pkey shouldn't be destroyed
  46891. since ctx uses it.*/
  46892. ExpectIntEQ(pkey->ref.count, 2);
  46893. EVP_PKEY_free(pkey);
  46894. ExpectIntEQ(pkey->ref.count, 1);
  46895. /* Encrypt data */
  46896. /* Check that we can get the required output buffer length by passing in a
  46897. * NULL output buffer. */
  46898. ExpectIntEQ(EVP_PKEY_encrypt(ctx, NULL, &outEncLen,
  46899. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  46900. ExpectIntEQ(rsaKeySz, outEncLen);
  46901. /* Now do the actual encryption. */
  46902. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEnc, &outEncLen,
  46903. (const unsigned char*)in, inlen), WOLFSSL_SUCCESS);
  46904. /* Decrypt data */
  46905. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  46906. /* Check that we can get the required output buffer length by passing in a
  46907. * NULL output buffer. */
  46908. ExpectIntEQ(EVP_PKEY_decrypt(ctx, NULL, &outDecLen, outEnc, outEncLen),
  46909. WOLFSSL_SUCCESS);
  46910. ExpectIntEQ(rsaKeySz, outDecLen);
  46911. /* Now do the actual decryption. */
  46912. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDec, &outDecLen, outEnc, outEncLen),
  46913. WOLFSSL_SUCCESS);
  46914. ExpectIntEQ(XMEMCMP(in, outDec, outDecLen), 0);
  46915. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  46916. /* The input length must be the same size as the RSA key.*/
  46917. ExpectNotNull(inTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  46918. DYNAMIC_TYPE_TMP_BUFFER));
  46919. if (inTmp != NULL) {
  46920. XMEMSET(inTmp, 9, rsaKeySz);
  46921. }
  46922. ExpectNotNull(outEncTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  46923. DYNAMIC_TYPE_TMP_BUFFER));
  46924. if (outEncTmp != NULL) {
  46925. XMEMSET(outEncTmp, 0, rsaKeySz);
  46926. }
  46927. ExpectNotNull(outDecTmp = (byte*)XMALLOC(rsaKeySz, HEAP_HINT,
  46928. DYNAMIC_TYPE_TMP_BUFFER));
  46929. if (outDecTmp != NULL) {
  46930. XMEMSET(outDecTmp, 0, rsaKeySz);
  46931. }
  46932. ExpectIntEQ(EVP_PKEY_encrypt_init(ctx), WOLFSSL_SUCCESS);
  46933. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  46934. WOLFSSL_SUCCESS);
  46935. ExpectIntEQ(EVP_PKEY_encrypt(ctx, outEncTmp, &outEncLen, inTmp, rsaKeySz),
  46936. WOLFSSL_SUCCESS);
  46937. ExpectIntEQ(EVP_PKEY_decrypt_init(ctx), WOLFSSL_SUCCESS);
  46938. ExpectIntEQ(EVP_PKEY_decrypt(ctx, outDecTmp, &outDecLen, outEncTmp,
  46939. outEncLen), WOLFSSL_SUCCESS);
  46940. ExpectIntEQ(XMEMCMP(inTmp, outDecTmp, outDecLen), 0);
  46941. #endif
  46942. EVP_PKEY_CTX_free(ctx);
  46943. XFREE(outEnc, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46944. XFREE(outDec, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46945. #if !defined(HAVE_FIPS) && defined(WC_RSA_NO_PADDING)
  46946. XFREE(inTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46947. XFREE(outEncTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46948. XFREE(outDecTmp, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  46949. #endif
  46950. #endif
  46951. return EXPECT_RESULT();
  46952. }
  46953. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46954. !defined(HAVE_SELFTEST)
  46955. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46956. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46957. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46958. #endif
  46959. #endif
  46960. #endif
  46961. #if defined(OPENSSL_EXTRA)
  46962. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46963. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46964. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46965. #endif
  46966. #endif
  46967. #endif
  46968. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46969. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46970. #ifndef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46971. #define TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46972. #endif
  46973. #endif
  46974. #endif
  46975. #ifdef TEST_WOLFSSL_EVP_PKEY_SIGN_VERIFY
  46976. static int test_wolfSSL_EVP_PKEY_sign_verify(int keyType)
  46977. {
  46978. EXPECT_DECLS;
  46979. #if defined(OPENSSL_EXTRA)
  46980. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  46981. !defined(HAVE_SELFTEST)
  46982. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46983. WOLFSSL_RSA* rsa = NULL;
  46984. #endif
  46985. #endif
  46986. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  46987. WOLFSSL_DSA* dsa = NULL;
  46988. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  46989. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  46990. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  46991. WOLFSSL_EC_KEY* ecKey = NULL;
  46992. #endif
  46993. #endif
  46994. WOLFSSL_EVP_PKEY* pkey = NULL;
  46995. WOLFSSL_EVP_PKEY_CTX* ctx = NULL;
  46996. WOLFSSL_EVP_PKEY_CTX* ctx_verify = NULL;
  46997. const char* in = "What is easy to do is easy not to do.";
  46998. size_t inlen = XSTRLEN(in);
  46999. byte hash[SHA256_DIGEST_LENGTH] = {0};
  47000. byte zero[SHA256_DIGEST_LENGTH] = {0};
  47001. SHA256_CTX c;
  47002. byte* sig = NULL;
  47003. byte* sigVerify = NULL;
  47004. size_t siglen;
  47005. size_t siglenOnlyLen;
  47006. size_t keySz = 2048/8; /* Bytes */
  47007. ExpectNotNull(sig =
  47008. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  47009. ExpectNotNull(sigVerify =
  47010. (byte*)XMALLOC(keySz, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER));
  47011. siglen = keySz;
  47012. ExpectNotNull(XMEMSET(sig, 0, keySz));
  47013. ExpectNotNull(XMEMSET(sigVerify, 0, keySz));
  47014. /* Generate hash */
  47015. SHA256_Init(&c);
  47016. SHA256_Update(&c, in, inlen);
  47017. SHA256_Final(hash, &c);
  47018. #ifdef WOLFSSL_SMALL_STACK_CACHE
  47019. /* workaround for small stack cache case */
  47020. wc_Sha256Free((wc_Sha256*)&c);
  47021. #endif
  47022. /* Generate key */
  47023. ExpectNotNull(pkey = EVP_PKEY_new());
  47024. switch (keyType) {
  47025. case EVP_PKEY_RSA:
  47026. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  47027. !defined(HAVE_SELFTEST)
  47028. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47029. {
  47030. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  47031. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  47032. }
  47033. #endif
  47034. #endif
  47035. break;
  47036. case EVP_PKEY_DSA:
  47037. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  47038. ExpectNotNull(dsa = DSA_new());
  47039. ExpectIntEQ(DSA_generate_parameters_ex(dsa, 2048,
  47040. NULL, 0, NULL, NULL, NULL), 1);
  47041. ExpectIntEQ(DSA_generate_key(dsa), 1);
  47042. ExpectIntEQ(EVP_PKEY_set1_DSA(pkey, dsa), WOLFSSL_SUCCESS);
  47043. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  47044. break;
  47045. case EVP_PKEY_EC:
  47046. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  47047. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47048. {
  47049. ExpectNotNull(ecKey = EC_KEY_new());
  47050. ExpectIntEQ(EC_KEY_generate_key(ecKey), 1);
  47051. ExpectIntEQ(
  47052. EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  47053. if (EXPECT_FAIL()) {
  47054. EC_KEY_free(ecKey);
  47055. }
  47056. }
  47057. #endif
  47058. #endif
  47059. break;
  47060. }
  47061. ExpectNotNull(ctx = EVP_PKEY_CTX_new(pkey, NULL));
  47062. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  47063. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  47064. !defined(HAVE_SELFTEST)
  47065. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47066. if (keyType == EVP_PKEY_RSA)
  47067. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  47068. WOLFSSL_SUCCESS);
  47069. #endif
  47070. #endif
  47071. /* Check returning only length */
  47072. ExpectIntEQ(EVP_PKEY_sign(ctx, NULL, &siglenOnlyLen, hash,
  47073. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  47074. ExpectIntGT(siglenOnlyLen, 0);
  47075. /* Sign data */
  47076. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, hash,
  47077. SHA256_DIGEST_LENGTH), WOLFSSL_SUCCESS);
  47078. ExpectIntGE(siglenOnlyLen, siglen);
  47079. /* Verify signature */
  47080. ExpectNotNull(ctx_verify = EVP_PKEY_CTX_new(pkey, NULL));
  47081. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  47082. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  47083. !defined(HAVE_SELFTEST)
  47084. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47085. if (keyType == EVP_PKEY_RSA)
  47086. ExpectIntEQ(
  47087. EVP_PKEY_CTX_set_rsa_padding(ctx_verify, RSA_PKCS1_PADDING),
  47088. WOLFSSL_SUCCESS);
  47089. #endif
  47090. #endif
  47091. ExpectIntEQ(EVP_PKEY_verify(
  47092. ctx_verify, sig, siglen, hash, SHA256_DIGEST_LENGTH),
  47093. WOLFSSL_SUCCESS);
  47094. ExpectIntEQ(EVP_PKEY_verify(
  47095. ctx_verify, sig, siglen, zero, SHA256_DIGEST_LENGTH),
  47096. WOLFSSL_FAILURE);
  47097. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  47098. !defined(HAVE_SELFTEST)
  47099. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47100. if (keyType == EVP_PKEY_RSA) {
  47101. #if defined(WC_RSA_NO_PADDING) || defined(WC_RSA_DIRECT)
  47102. /* Try RSA sign/verify with no padding. */
  47103. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  47104. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_NO_PADDING),
  47105. WOLFSSL_SUCCESS);
  47106. ExpectIntEQ(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  47107. siglen), WOLFSSL_SUCCESS);
  47108. ExpectIntGE(siglenOnlyLen, siglen);
  47109. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  47110. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  47111. RSA_NO_PADDING), WOLFSSL_SUCCESS);
  47112. ExpectIntEQ(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  47113. siglen), WOLFSSL_SUCCESS);
  47114. #endif
  47115. /* Wrong padding schemes. */
  47116. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  47117. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx,
  47118. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  47119. ExpectIntNE(EVP_PKEY_sign(ctx, sigVerify, &siglen, sig,
  47120. siglen), WOLFSSL_SUCCESS);
  47121. ExpectIntEQ(EVP_PKEY_verify_init(ctx_verify), WOLFSSL_SUCCESS);
  47122. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  47123. RSA_PKCS1_OAEP_PADDING), WOLFSSL_SUCCESS);
  47124. ExpectIntNE(EVP_PKEY_verify(ctx_verify, sigVerify, siglen, sig,
  47125. siglen), WOLFSSL_SUCCESS);
  47126. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx, RSA_PKCS1_PADDING),
  47127. WOLFSSL_SUCCESS);
  47128. ExpectIntEQ(EVP_PKEY_CTX_set_rsa_padding(ctx_verify,
  47129. RSA_PKCS1_PADDING), WOLFSSL_SUCCESS);
  47130. }
  47131. #endif
  47132. #endif
  47133. /* error cases */
  47134. siglen = keySz; /* Reset because sig size may vary slightly */
  47135. ExpectIntNE(EVP_PKEY_sign_init(NULL), WOLFSSL_SUCCESS);
  47136. ExpectIntEQ(EVP_PKEY_sign_init(ctx), WOLFSSL_SUCCESS);
  47137. ExpectIntNE(EVP_PKEY_sign(NULL, sig, &siglen, (byte*)in, inlen),
  47138. WOLFSSL_SUCCESS);
  47139. ExpectIntEQ(EVP_PKEY_sign(ctx, sig, &siglen, (byte*)in, inlen),
  47140. WOLFSSL_SUCCESS);
  47141. EVP_PKEY_free(pkey);
  47142. pkey = NULL;
  47143. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  47144. DSA_free(dsa);
  47145. dsa = NULL;
  47146. #endif /* !NO_DSA && !HAVE_SELFTEST && WOLFSSL_KEY_GEN */
  47147. EVP_PKEY_CTX_free(ctx_verify);
  47148. ctx_verify = NULL;
  47149. EVP_PKEY_CTX_free(ctx);
  47150. ctx = NULL;
  47151. XFREE(sig, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47152. XFREE(sigVerify, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  47153. #endif /* OPENSSL_EXTRA */
  47154. return EXPECT_RESULT();
  47155. }
  47156. #endif
  47157. static int test_wolfSSL_EVP_PKEY_sign_verify_rsa(void)
  47158. {
  47159. EXPECT_DECLS;
  47160. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  47161. !defined(HAVE_SELFTEST)
  47162. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47163. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_RSA), TEST_SUCCESS);
  47164. #endif
  47165. #endif
  47166. return EXPECT_RESULT();
  47167. }
  47168. static int test_wolfSSL_EVP_PKEY_sign_verify_dsa(void)
  47169. {
  47170. EXPECT_DECLS;
  47171. #if defined(OPENSSL_EXTRA)
  47172. #if !defined (NO_DSA) && !defined(HAVE_SELFTEST) && defined(WOLFSSL_KEY_GEN)
  47173. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_DSA), TEST_SUCCESS);
  47174. #endif
  47175. #endif
  47176. return EXPECT_RESULT();
  47177. }
  47178. static int test_wolfSSL_EVP_PKEY_sign_verify_ec(void)
  47179. {
  47180. EXPECT_DECLS;
  47181. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  47182. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47183. ExpectIntEQ(test_wolfSSL_EVP_PKEY_sign_verify(EVP_PKEY_EC), TEST_SUCCESS);
  47184. #endif
  47185. #endif
  47186. return EXPECT_RESULT();
  47187. }
  47188. static int test_EVP_PKEY_rsa(void)
  47189. {
  47190. EXPECT_DECLS;
  47191. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  47192. WOLFSSL_RSA* rsa = NULL;
  47193. WOLFSSL_EVP_PKEY* pkey = NULL;
  47194. ExpectNotNull(rsa = wolfSSL_RSA_new());
  47195. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  47196. ExpectIntEQ(EVP_PKEY_assign_RSA(NULL, rsa), WOLFSSL_FAILURE);
  47197. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, NULL), WOLFSSL_FAILURE);
  47198. ExpectIntEQ(EVP_PKEY_assign_RSA(pkey, rsa), WOLFSSL_SUCCESS);
  47199. if (EXPECT_FAIL()) {
  47200. wolfSSL_RSA_free(rsa);
  47201. }
  47202. ExpectPtrEq(EVP_PKEY_get0_RSA(pkey), rsa);
  47203. wolfSSL_EVP_PKEY_free(pkey);
  47204. #endif
  47205. return EXPECT_RESULT();
  47206. }
  47207. static int test_EVP_PKEY_ec(void)
  47208. {
  47209. EXPECT_DECLS;
  47210. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  47211. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  47212. WOLFSSL_EC_KEY* ecKey = NULL;
  47213. WOLFSSL_EVP_PKEY* pkey = NULL;
  47214. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  47215. ExpectNotNull(pkey = wolfSSL_EVP_PKEY_new());
  47216. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(NULL, ecKey), WOLFSSL_FAILURE);
  47217. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, NULL), WOLFSSL_FAILURE);
  47218. /* Should fail since ecKey is empty */
  47219. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_FAILURE);
  47220. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  47221. ExpectIntEQ(EVP_PKEY_assign_EC_KEY(pkey, ecKey), WOLFSSL_SUCCESS);
  47222. if (EXPECT_FAIL()) {
  47223. wolfSSL_EC_KEY_free(ecKey);
  47224. }
  47225. wolfSSL_EVP_PKEY_free(pkey);
  47226. #endif
  47227. #endif
  47228. return EXPECT_RESULT();
  47229. }
  47230. static int test_EVP_PKEY_cmp(void)
  47231. {
  47232. EXPECT_DECLS;
  47233. #if defined(OPENSSL_EXTRA)
  47234. EVP_PKEY *a = NULL;
  47235. EVP_PKEY *b = NULL;
  47236. const unsigned char *in;
  47237. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048)
  47238. in = client_key_der_2048;
  47239. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  47240. &in, (long)sizeof_client_key_der_2048));
  47241. in = client_key_der_2048;
  47242. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  47243. &in, (long)sizeof_client_key_der_2048));
  47244. /* Test success case RSA */
  47245. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  47246. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  47247. #else
  47248. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  47249. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  47250. EVP_PKEY_free(b);
  47251. b = NULL;
  47252. EVP_PKEY_free(a);
  47253. a = NULL;
  47254. #endif
  47255. #if defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  47256. in = ecc_clikey_der_256;
  47257. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  47258. &in, (long)sizeof_ecc_clikey_der_256));
  47259. in = ecc_clikey_der_256;
  47260. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  47261. &in, (long)sizeof_ecc_clikey_der_256));
  47262. /* Test success case ECC */
  47263. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  47264. ExpectIntEQ(EVP_PKEY_cmp(a, b), 1);
  47265. #else
  47266. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  47267. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  47268. EVP_PKEY_free(b);
  47269. b = NULL;
  47270. EVP_PKEY_free(a);
  47271. a = NULL;
  47272. #endif
  47273. /* Test failure cases */
  47274. #if !defined(NO_RSA) && defined(USE_CERT_BUFFERS_2048) && \
  47275. defined(HAVE_ECC) && defined(USE_CERT_BUFFERS_256)
  47276. in = client_key_der_2048;
  47277. ExpectNotNull(a = wolfSSL_d2i_PrivateKey(EVP_PKEY_RSA, NULL,
  47278. &in, (long)sizeof_client_key_der_2048));
  47279. in = ecc_clikey_der_256;
  47280. ExpectNotNull(b = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL,
  47281. &in, (long)sizeof_ecc_clikey_der_256));
  47282. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  47283. ExpectIntEQ(EVP_PKEY_cmp(a, b), -1);
  47284. #else
  47285. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  47286. #endif /* WOLFSSL_ERROR_CODE_OPENSSL */
  47287. EVP_PKEY_free(b);
  47288. b = NULL;
  47289. EVP_PKEY_free(a);
  47290. a = NULL;
  47291. #endif
  47292. /* invalid or empty failure cases */
  47293. a = EVP_PKEY_new();
  47294. b = EVP_PKEY_new();
  47295. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  47296. ExpectIntEQ(EVP_PKEY_cmp(NULL, NULL), 0);
  47297. ExpectIntEQ(EVP_PKEY_cmp(a, NULL), 0);
  47298. ExpectIntEQ(EVP_PKEY_cmp(NULL, b), 0);
  47299. #ifdef NO_RSA
  47300. /* Type check will fail since RSA is the default EVP key type */
  47301. ExpectIntEQ(EVP_PKEY_cmp(a, b), -2);
  47302. #else
  47303. ExpectIntEQ(EVP_PKEY_cmp(a, b), 0);
  47304. #endif
  47305. #else
  47306. ExpectIntNE(EVP_PKEY_cmp(NULL, NULL), 0);
  47307. ExpectIntNE(EVP_PKEY_cmp(a, NULL), 0);
  47308. ExpectIntNE(EVP_PKEY_cmp(NULL, b), 0);
  47309. ExpectIntNE(EVP_PKEY_cmp(a, b), 0);
  47310. #endif
  47311. EVP_PKEY_free(b);
  47312. EVP_PKEY_free(a);
  47313. (void)in;
  47314. #endif
  47315. return EXPECT_RESULT();
  47316. }
  47317. static int test_ERR_load_crypto_strings(void)
  47318. {
  47319. #if defined(OPENSSL_ALL)
  47320. ERR_load_crypto_strings();
  47321. return TEST_SUCCESS;
  47322. #else
  47323. return TEST_SKIPPED;
  47324. #endif
  47325. }
  47326. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  47327. static void free_x509(X509* x)
  47328. {
  47329. AssertIntEQ((x == (X509*)1 || x == (X509*)2), 1);
  47330. }
  47331. #endif
  47332. static int test_sk_X509(void)
  47333. {
  47334. EXPECT_DECLS;
  47335. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  47336. {
  47337. STACK_OF(X509)* s = NULL;
  47338. ExpectNotNull(s = sk_X509_new_null());
  47339. ExpectIntEQ(sk_X509_num(s), 0);
  47340. sk_X509_pop_free(s, NULL);
  47341. ExpectNotNull(s = sk_X509_new_null());
  47342. ExpectIntEQ(sk_X509_num(s), 0);
  47343. sk_X509_pop_free(s, NULL);
  47344. ExpectNotNull(s = sk_X509_new_null());
  47345. sk_X509_push(s, (X509*)1);
  47346. ExpectIntEQ(sk_X509_num(s), 1);
  47347. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)1), 1);
  47348. sk_X509_push(s, (X509*)2);
  47349. ExpectIntEQ(sk_X509_num(s), 2);
  47350. ExpectIntEQ((sk_X509_value(s, 0) == (X509*)2), 1);
  47351. ExpectIntEQ((sk_X509_value(s, 1) == (X509*)1), 1);
  47352. sk_X509_push(s, (X509*)2);
  47353. sk_X509_pop_free(s, free_x509);
  47354. }
  47355. {
  47356. /* Push a list of 10 X509s onto stack, then verify that
  47357. * value(), push(), shift(), and pop() behave as expected. */
  47358. STACK_OF(X509)* s = NULL;
  47359. X509* xList[10];
  47360. int i = 0;
  47361. const int len = (sizeof(xList) / sizeof(xList[0]));
  47362. for (i = 0; i < len; ++i) {
  47363. xList[i] = NULL;
  47364. ExpectNotNull(xList[i] = X509_new());
  47365. }
  47366. /* test push, pop, and free */
  47367. ExpectNotNull(s = sk_X509_new_null());
  47368. for (i = 0; i < len; ++i) {
  47369. sk_X509_push(s, xList[i]);
  47370. ExpectIntEQ(sk_X509_num(s), i + 1);
  47371. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  47372. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  47373. }
  47374. /* pop returns and removes last pushed on stack, which is index 0
  47375. * in sk_x509_value */
  47376. for (i = 0; i < len; ++i) {
  47377. X509 * x = sk_X509_value(s, 0);
  47378. X509 * y = sk_X509_pop(s);
  47379. X509 * z = xList[len - 1 - i];
  47380. ExpectIntEQ((x == y), 1);
  47381. ExpectIntEQ((x == z), 1);
  47382. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  47383. }
  47384. sk_free(s);
  47385. s = NULL;
  47386. /* test push, shift, and free */
  47387. ExpectNotNull(s = sk_X509_new_null());
  47388. for (i = 0; i < len; ++i) {
  47389. sk_X509_push(s, xList[i]);
  47390. ExpectIntEQ(sk_X509_num(s), i + 1);
  47391. ExpectIntEQ((sk_X509_value(s, 0) == xList[i]), 1);
  47392. ExpectIntEQ((sk_X509_value(s, i) == xList[0]), 1);
  47393. }
  47394. /* shift returns and removes first pushed on stack, which is index i
  47395. * in sk_x509_value() */
  47396. for (i = 0; i < len; ++i) {
  47397. X509 * x = sk_X509_value(s, len - 1 - i);
  47398. X509 * y = sk_X509_shift(s);
  47399. X509 * z = xList[i];
  47400. ExpectIntEQ((x == y), 1);
  47401. ExpectIntEQ((x == z), 1);
  47402. ExpectIntEQ(sk_X509_num(s), len - 1 - i);
  47403. }
  47404. sk_free(s);
  47405. for (i = 0; i < len; ++i)
  47406. X509_free(xList[i]);
  47407. }
  47408. #endif
  47409. return EXPECT_RESULT();
  47410. }
  47411. static int test_sk_X509_CRL(void)
  47412. {
  47413. EXPECT_DECLS;
  47414. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && defined(HAVE_CRL)
  47415. X509_CRL* crl = NULL;
  47416. XFILE fp = XBADFILE;
  47417. STACK_OF(X509_CRL)* s = NULL;
  47418. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  47419. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  47420. NULL, NULL));
  47421. if (fp != XBADFILE)
  47422. XFCLOSE(fp);
  47423. ExpectNotNull(s = sk_X509_CRL_new());
  47424. ExpectIntEQ(sk_X509_CRL_num(s), 0);
  47425. ExpectIntEQ(sk_X509_CRL_push(s, crl), 1);
  47426. if (EXPECT_FAIL()) {
  47427. X509_CRL_free(crl);
  47428. }
  47429. ExpectIntEQ(sk_X509_CRL_num(s), 1);
  47430. ExpectPtrEq(sk_X509_CRL_value(s, 0), crl);
  47431. sk_X509_CRL_free(s);
  47432. #endif
  47433. return EXPECT_RESULT();
  47434. }
  47435. static int test_X509_get_signature_nid(void)
  47436. {
  47437. EXPECT_DECLS;
  47438. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  47439. X509* x509 = NULL;
  47440. ExpectIntEQ(X509_get_signature_nid(NULL), 0);
  47441. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  47442. SSL_FILETYPE_PEM));
  47443. ExpectIntEQ(X509_get_signature_nid(x509), NID_sha256WithRSAEncryption);
  47444. X509_free(x509);
  47445. #endif
  47446. return EXPECT_RESULT();
  47447. }
  47448. static int test_X509_REQ(void)
  47449. {
  47450. EXPECT_DECLS;
  47451. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  47452. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && !defined(NO_BIO)
  47453. X509_NAME* name = NULL;
  47454. #ifndef NO_RSA
  47455. X509_NAME* subject = NULL;
  47456. #endif
  47457. #if !defined(NO_RSA) || defined(HAVE_ECC)
  47458. X509_REQ* req = NULL;
  47459. EVP_PKEY* priv = NULL;
  47460. EVP_PKEY* pub = NULL;
  47461. unsigned char* der = NULL;
  47462. int len;
  47463. #endif
  47464. #ifndef NO_RSA
  47465. EVP_MD_CTX *mctx = NULL;
  47466. EVP_PKEY_CTX *pkctx = NULL;
  47467. #ifdef USE_CERT_BUFFERS_1024
  47468. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_1024;
  47469. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_1024;
  47470. #elif defined(USE_CERT_BUFFERS_2048)
  47471. const unsigned char* rsaPriv = (const unsigned char*)client_key_der_2048;
  47472. const unsigned char* rsaPub = (unsigned char*)client_keypub_der_2048;
  47473. #endif
  47474. #endif
  47475. #ifdef HAVE_ECC
  47476. const unsigned char* ecPriv = (const unsigned char*)ecc_clikey_der_256;
  47477. const unsigned char* ecPub = (unsigned char*)ecc_clikeypub_der_256;
  47478. #endif
  47479. ExpectNotNull(name = X509_NAME_new());
  47480. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "commonName", MBSTRING_UTF8,
  47481. (byte*)"wolfssl.com", 11, 0, 1), WOLFSSL_SUCCESS);
  47482. ExpectIntEQ(X509_NAME_add_entry_by_txt(name, "emailAddress", MBSTRING_UTF8,
  47483. (byte*)"support@wolfssl.com", 19, -1, 1), WOLFSSL_SUCCESS);
  47484. #ifndef NO_RSA
  47485. ExpectNotNull(priv = d2i_PrivateKey(EVP_PKEY_RSA, NULL, &rsaPriv,
  47486. (long)sizeof_client_key_der_2048));
  47487. ExpectNotNull(pub = d2i_PUBKEY(NULL, &rsaPub,
  47488. (long)sizeof_client_keypub_der_2048));
  47489. ExpectNotNull(req = X509_REQ_new());
  47490. ExpectIntEQ(X509_REQ_set_subject_name(NULL, name), WOLFSSL_FAILURE);
  47491. ExpectIntEQ(X509_REQ_set_subject_name(req, NULL), WOLFSSL_FAILURE);
  47492. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  47493. ExpectIntEQ(X509_REQ_set_pubkey(NULL, pub), WOLFSSL_FAILURE);
  47494. ExpectIntEQ(X509_REQ_set_pubkey(req, NULL), WOLFSSL_FAILURE);
  47495. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  47496. ExpectIntEQ(X509_REQ_sign(NULL, priv, EVP_sha256()), WOLFSSL_FAILURE);
  47497. ExpectIntEQ(X509_REQ_sign(req, NULL, EVP_sha256()), WOLFSSL_FAILURE);
  47498. ExpectIntEQ(X509_REQ_sign(req, priv, NULL), WOLFSSL_FAILURE);
  47499. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  47500. len = i2d_X509_REQ(req, &der);
  47501. DEBUG_WRITE_DER(der, len, "req.der");
  47502. #ifdef USE_CERT_BUFFERS_1024
  47503. ExpectIntEQ(len, 381);
  47504. #else
  47505. ExpectIntEQ(len, 643);
  47506. #endif
  47507. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  47508. der = NULL;
  47509. mctx = EVP_MD_CTX_new();
  47510. ExpectIntEQ(EVP_DigestSignInit(mctx, &pkctx, EVP_sha256(), NULL, priv),
  47511. WOLFSSL_SUCCESS);
  47512. ExpectIntEQ(X509_REQ_sign_ctx(req, mctx), WOLFSSL_SUCCESS);
  47513. EVP_MD_CTX_free(mctx);
  47514. mctx = NULL;
  47515. X509_REQ_free(NULL);
  47516. X509_REQ_free(req);
  47517. req = NULL;
  47518. /* Test getting the subject from a newly created X509_REQ */
  47519. ExpectNotNull(req = X509_REQ_new());
  47520. ExpectNotNull(subject = X509_REQ_get_subject_name(req));
  47521. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_commonName,
  47522. MBSTRING_UTF8, (unsigned char*)"www.wolfssl.com", -1, -1, 0), 1);
  47523. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_countryName,
  47524. MBSTRING_UTF8, (unsigned char*)"US", -1, -1, 0), 1);
  47525. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_localityName,
  47526. MBSTRING_UTF8, (unsigned char*)"Bozeman", -1, -1, 0), 1);
  47527. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_stateOrProvinceName,
  47528. MBSTRING_UTF8, (unsigned char*)"Montana", -1, -1, 0), 1);
  47529. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationName,
  47530. MBSTRING_UTF8, (unsigned char*)"wolfSSL", -1, -1, 0), 1);
  47531. ExpectIntEQ(X509_NAME_add_entry_by_NID(subject, NID_organizationalUnitName,
  47532. MBSTRING_UTF8, (unsigned char*)"Testing", -1, -1, 0), 1);
  47533. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  47534. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  47535. len = i2d_X509_REQ(req, &der);
  47536. DEBUG_WRITE_DER(der, len, "req2.der");
  47537. #ifdef USE_CERT_BUFFERS_1024
  47538. ExpectIntEQ(len, 435);
  47539. #else
  47540. ExpectIntEQ(len, 696);
  47541. #endif
  47542. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  47543. der = NULL;
  47544. EVP_PKEY_free(pub);
  47545. pub = NULL;
  47546. EVP_PKEY_free(priv);
  47547. priv = NULL;
  47548. X509_REQ_free(req);
  47549. req = NULL;
  47550. #endif
  47551. #ifdef HAVE_ECC
  47552. ExpectNotNull(priv = wolfSSL_d2i_PrivateKey(EVP_PKEY_EC, NULL, &ecPriv,
  47553. sizeof_ecc_clikey_der_256));
  47554. ExpectNotNull(pub = wolfSSL_d2i_PUBKEY(NULL, &ecPub,
  47555. sizeof_ecc_clikeypub_der_256));
  47556. ExpectNotNull(req = X509_REQ_new());
  47557. ExpectIntEQ(X509_REQ_set_subject_name(req, name), WOLFSSL_SUCCESS);
  47558. ExpectIntEQ(X509_REQ_set_pubkey(req, pub), WOLFSSL_SUCCESS);
  47559. ExpectIntEQ(X509_REQ_sign(req, priv, EVP_sha256()), WOLFSSL_SUCCESS);
  47560. /* Signature is random and may be shorter or longer. */
  47561. ExpectIntGE((len = i2d_X509_REQ(req, &der)), 245);
  47562. ExpectIntLE(len, 253);
  47563. XFREE(der, NULL, DYNAMIC_TYPE_OPENSSL);
  47564. X509_REQ_free(req);
  47565. EVP_PKEY_free(pub);
  47566. EVP_PKEY_free(priv);
  47567. #ifdef FP_ECC
  47568. wc_ecc_fp_free();
  47569. #endif
  47570. #endif /* HAVE_ECC */
  47571. X509_NAME_free(name);
  47572. #endif
  47573. return EXPECT_RESULT();
  47574. }
  47575. static int test_wolfssl_PKCS7(void)
  47576. {
  47577. EXPECT_DECLS;
  47578. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  47579. !defined(NO_RSA)
  47580. PKCS7* pkcs7 = NULL;
  47581. byte data[FOURK_BUF];
  47582. word32 len = sizeof(data);
  47583. const byte* p = data;
  47584. byte content[] = "Test data to encode.";
  47585. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  47586. BIO* bio = NULL;
  47587. byte key[sizeof(client_key_der_2048)];
  47588. word32 keySz = (word32)sizeof(key);
  47589. byte* out = NULL;
  47590. #endif
  47591. ExpectIntGT((len = (word32)CreatePKCS7SignedData(data, (int)len, content,
  47592. (word32)sizeof(content), 0, 0, 0, RSA_TYPE)), 0);
  47593. ExpectNull(pkcs7 = d2i_PKCS7(NULL, NULL, (int)len));
  47594. ExpectNull(pkcs7 = d2i_PKCS7(NULL, &p, 0));
  47595. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, (int)len));
  47596. ExpectIntEQ(wolfSSL_PKCS7_verify(NULL, NULL, NULL, NULL, NULL,
  47597. PKCS7_NOVERIFY), WOLFSSL_FAILURE);
  47598. PKCS7_free(pkcs7);
  47599. pkcs7 = NULL;
  47600. /* fail case, without PKCS7_NOVERIFY */
  47601. p = data;
  47602. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, (int)len));
  47603. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  47604. 0), WOLFSSL_FAILURE);
  47605. PKCS7_free(pkcs7);
  47606. pkcs7 = NULL;
  47607. /* success case, with PKCS7_NOVERIFY */
  47608. p = data;
  47609. ExpectNotNull(pkcs7 = d2i_PKCS7(NULL, &p, (int)len));
  47610. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, NULL, NULL,
  47611. PKCS7_NOVERIFY), WOLFSSL_SUCCESS);
  47612. #if !defined(NO_RSA) & defined(USE_CERT_BUFFERS_2048)
  47613. /* test i2d */
  47614. XMEMCPY(key, client_key_der_2048, keySz);
  47615. if (pkcs7 != NULL) {
  47616. pkcs7->privateKey = key;
  47617. pkcs7->privateKeySz = (word32)sizeof(key);
  47618. pkcs7->encryptOID = RSAk;
  47619. #ifdef NO_SHA
  47620. pkcs7->hashOID = SHA256h;
  47621. #else
  47622. pkcs7->hashOID = SHAh;
  47623. #endif
  47624. }
  47625. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  47626. ExpectIntEQ(i2d_PKCS7_bio(bio, pkcs7), 1);
  47627. #ifndef NO_ASN_TIME
  47628. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 655);
  47629. #else
  47630. ExpectIntEQ(i2d_PKCS7(pkcs7, &out), 625);
  47631. #endif
  47632. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47633. BIO_free(bio);
  47634. #endif
  47635. PKCS7_free(NULL);
  47636. PKCS7_free(pkcs7);
  47637. #endif
  47638. return EXPECT_RESULT();
  47639. }
  47640. static int test_wolfSSL_PKCS7_sign(void)
  47641. {
  47642. EXPECT_DECLS;
  47643. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_BIO) && \
  47644. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  47645. PKCS7* p7 = NULL;
  47646. PKCS7* p7Ver = NULL;
  47647. byte* out = NULL;
  47648. byte* tmpPtr = NULL;
  47649. int outLen = 0;
  47650. int flags = 0;
  47651. byte data[] = "Test data to encode.";
  47652. const char* cert = "./certs/server-cert.pem";
  47653. const char* key = "./certs/server-key.pem";
  47654. const char* ca = "./certs/ca-cert.pem";
  47655. WOLFSSL_BIO* certBio = NULL;
  47656. WOLFSSL_BIO* keyBio = NULL;
  47657. WOLFSSL_BIO* caBio = NULL;
  47658. WOLFSSL_BIO* inBio = NULL;
  47659. X509* signCert = NULL;
  47660. EVP_PKEY* signKey = NULL;
  47661. X509* caCert = NULL;
  47662. X509_STORE* store = NULL;
  47663. #ifndef NO_PKCS7_STREAM
  47664. int z;
  47665. int ret;
  47666. #endif /* !NO_PKCS7_STREAM */
  47667. /* read signer cert/key into BIO */
  47668. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  47669. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  47670. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  47671. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  47672. /* read CA cert into store (for verify) */
  47673. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  47674. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  47675. ExpectNotNull(store = X509_STORE_new());
  47676. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  47677. /* data to be signed into BIO */
  47678. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47679. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47680. /* PKCS7_sign, bad args: signer NULL */
  47681. ExpectNull(p7 = PKCS7_sign(NULL, signKey, NULL, inBio, 0));
  47682. /* PKCS7_sign, bad args: signer key NULL */
  47683. ExpectNull(p7 = PKCS7_sign(signCert, NULL, NULL, inBio, 0));
  47684. /* PKCS7_sign, bad args: in data NULL without PKCS7_STREAM */
  47685. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, NULL, 0));
  47686. /* PKCS7_sign, bad args: PKCS7_NOCERTS flag not supported */
  47687. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_NOCERTS));
  47688. /* PKCS7_sign, bad args: PKCS7_PARTIAL flag not supported */
  47689. ExpectNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, PKCS7_PARTIAL));
  47690. /* TEST SUCCESS: Not detached, not streaming, not MIME */
  47691. {
  47692. flags = PKCS7_BINARY;
  47693. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47694. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  47695. /* verify with d2i_PKCS7 */
  47696. tmpPtr = out;
  47697. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  47698. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  47699. PKCS7_free(p7Ver);
  47700. p7Ver = NULL;
  47701. /* verify with wc_PKCS7_VerifySignedData */
  47702. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47703. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  47704. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, (word32)outLen), 0);
  47705. #ifndef NO_PKCS7_STREAM
  47706. /* verify with wc_PKCS7_VerifySignedData streaming */
  47707. wc_PKCS7_Free(p7Ver);
  47708. p7Ver = NULL;
  47709. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47710. ExpectIntEQ(wc_PKCS7_Init(p7Ver, HEAP_HINT, INVALID_DEVID), 0);
  47711. /* test for streaming */
  47712. ret = -1;
  47713. for (z = 0; z < outLen && ret != 0; z++) {
  47714. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  47715. if (ret < 0){
  47716. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  47717. }
  47718. }
  47719. ExpectIntEQ(ret, 0);
  47720. #endif /* !NO_PKCS7_STREAM */
  47721. /* compare the signer found to expected signer */
  47722. ExpectIntNE(p7Ver->verifyCertSz, 0);
  47723. tmpPtr = NULL;
  47724. ExpectIntEQ(i2d_X509(signCert, &tmpPtr), p7Ver->verifyCertSz);
  47725. ExpectIntEQ(XMEMCMP(tmpPtr, p7Ver->verifyCert, p7Ver->verifyCertSz), 0);
  47726. XFREE(tmpPtr, NULL, DYNAMIC_TYPE_OPENSSL);
  47727. tmpPtr = NULL;
  47728. wc_PKCS7_Free(p7Ver);
  47729. p7Ver = NULL;
  47730. ExpectNotNull(out);
  47731. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47732. out = NULL;
  47733. PKCS7_free(p7);
  47734. p7 = NULL;
  47735. }
  47736. /* TEST SUCCESS: Not detached, streaming, not MIME. Also bad arg
  47737. * tests for PKCS7_final() while we have a PKCS7 pointer to use */
  47738. {
  47739. /* re-populate input BIO, may have been consumed */
  47740. BIO_free(inBio);
  47741. inBio = NULL;
  47742. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47743. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47744. flags = PKCS7_BINARY | PKCS7_STREAM;
  47745. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47746. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  47747. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  47748. /* PKCS7_final, bad args: PKCS7 null */
  47749. ExpectIntEQ(PKCS7_final(NULL, inBio, 0), 0);
  47750. /* PKCS7_final, bad args: PKCS7 null */
  47751. ExpectIntEQ(PKCS7_final(p7, NULL, 0), 0);
  47752. tmpPtr = out;
  47753. ExpectNotNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  47754. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  47755. PKCS7_free(p7Ver);
  47756. p7Ver = NULL;
  47757. ExpectNotNull(out);
  47758. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47759. out = NULL;
  47760. PKCS7_free(p7);
  47761. p7 = NULL;
  47762. }
  47763. /* TEST SUCCESS: Detached, not streaming, not MIME */
  47764. {
  47765. /* re-populate input BIO, may have been consumed */
  47766. BIO_free(inBio);
  47767. inBio = NULL;
  47768. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47769. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47770. flags = PKCS7_BINARY | PKCS7_DETACHED;
  47771. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47772. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  47773. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  47774. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47775. if (p7Ver != NULL) {
  47776. p7Ver->content = data;
  47777. p7Ver->contentSz = sizeof(data);
  47778. }
  47779. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, (word32)outLen), 0);
  47780. wc_PKCS7_Free(p7Ver);
  47781. p7Ver = NULL;
  47782. #ifndef NO_PKCS7_STREAM
  47783. /* verify with wc_PKCS7_VerifySignedData streaming */
  47784. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47785. if (p7Ver != NULL) {
  47786. p7Ver->content = data;
  47787. p7Ver->contentSz = sizeof(data);
  47788. }
  47789. /* test for streaming */
  47790. ret = -1;
  47791. for (z = 0; z < outLen && ret != 0; z++) {
  47792. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  47793. if (ret < 0){
  47794. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  47795. }
  47796. }
  47797. ExpectIntEQ(ret, 0);
  47798. wc_PKCS7_Free(p7Ver);
  47799. p7Ver = NULL;
  47800. #endif /* !NO_PKCS7_STREAM */
  47801. /* verify expected failure (NULL return) from d2i_PKCS7, it does not
  47802. * yet support detached content */
  47803. tmpPtr = out;
  47804. ExpectNull(p7Ver = d2i_PKCS7(NULL, (const byte**)&tmpPtr, outLen));
  47805. PKCS7_free(p7Ver);
  47806. p7Ver = NULL;
  47807. ExpectNotNull(out);
  47808. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47809. out = NULL;
  47810. PKCS7_free(p7);
  47811. p7 = NULL;
  47812. }
  47813. /* TEST SUCCESS: Detached, streaming, not MIME */
  47814. {
  47815. /* re-populate input BIO, may have been consumed */
  47816. BIO_free(inBio);
  47817. inBio = NULL;
  47818. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  47819. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  47820. flags = PKCS7_BINARY | PKCS7_DETACHED | PKCS7_STREAM;
  47821. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  47822. ExpectIntEQ(PKCS7_final(p7, inBio, flags), 1);
  47823. ExpectIntGT((outLen = i2d_PKCS7(p7, &out)), 0);
  47824. /* verify with wolfCrypt, d2i_PKCS7 does not support detached content */
  47825. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47826. if (p7Ver != NULL) {
  47827. p7Ver->content = data;
  47828. p7Ver->contentSz = sizeof(data);
  47829. }
  47830. ExpectIntEQ(wc_PKCS7_VerifySignedData(p7Ver, out, (word32)outLen), 0);
  47831. wc_PKCS7_Free(p7Ver);
  47832. p7Ver = NULL;
  47833. ExpectNotNull(out);
  47834. #ifndef NO_PKCS7_STREAM
  47835. /* verify with wc_PKCS7_VerifySignedData streaming */
  47836. ExpectNotNull(p7Ver = wc_PKCS7_New(HEAP_HINT, testDevId));
  47837. if (p7Ver != NULL) {
  47838. p7Ver->content = data;
  47839. p7Ver->contentSz = sizeof(data);
  47840. }
  47841. /* test for streaming */
  47842. ret = -1;
  47843. for (z = 0; z < outLen && ret != 0; z++) {
  47844. ret = wc_PKCS7_VerifySignedData(p7Ver, out + z, 1);
  47845. if (ret < 0){
  47846. ExpectIntEQ(ret, WC_PKCS7_WANT_READ_E);
  47847. }
  47848. }
  47849. ExpectIntEQ(ret, 0);
  47850. ExpectNotNull(out);
  47851. wc_PKCS7_Free(p7Ver);
  47852. p7Ver = NULL;
  47853. #endif /* !NO_PKCS7_STREAM */
  47854. XFREE(out, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  47855. PKCS7_free(p7);
  47856. p7 = NULL;
  47857. }
  47858. X509_STORE_free(store);
  47859. X509_free(caCert);
  47860. X509_free(signCert);
  47861. EVP_PKEY_free(signKey);
  47862. BIO_free(inBio);
  47863. BIO_free(keyBio);
  47864. BIO_free(certBio);
  47865. BIO_free(caBio);
  47866. #endif
  47867. return EXPECT_RESULT();
  47868. }
  47869. static int test_wolfSSL_PKCS7_SIGNED_new(void)
  47870. {
  47871. EXPECT_DECLS;
  47872. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7)
  47873. PKCS7_SIGNED* pkcs7 = NULL;
  47874. ExpectNotNull(pkcs7 = PKCS7_SIGNED_new());
  47875. ExpectIntEQ(pkcs7->contentOID, SIGNED_DATA);
  47876. PKCS7_SIGNED_free(pkcs7);
  47877. #endif
  47878. return EXPECT_RESULT();
  47879. }
  47880. #ifndef NO_BIO
  47881. static int test_wolfSSL_PEM_write_bio_PKCS7(void)
  47882. {
  47883. EXPECT_DECLS;
  47884. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM)
  47885. PKCS7* pkcs7 = NULL;
  47886. BIO* bio = NULL;
  47887. const byte* cert_buf = NULL;
  47888. int ret = 0;
  47889. WC_RNG rng;
  47890. const byte data[] = { /* Hello World */
  47891. 0x48,0x65,0x6c,0x6c,0x6f,0x20,0x57,0x6f,
  47892. 0x72,0x6c,0x64
  47893. };
  47894. #ifndef NO_RSA
  47895. #if defined(USE_CERT_BUFFERS_2048)
  47896. byte key[sizeof(client_key_der_2048)];
  47897. byte cert[sizeof(client_cert_der_2048)];
  47898. word32 keySz = (word32)sizeof(key);
  47899. word32 certSz = (word32)sizeof(cert);
  47900. XMEMSET(key, 0, keySz);
  47901. XMEMSET(cert, 0, certSz);
  47902. XMEMCPY(key, client_key_der_2048, keySz);
  47903. XMEMCPY(cert, client_cert_der_2048, certSz);
  47904. #elif defined(USE_CERT_BUFFERS_1024)
  47905. byte key[sizeof_client_key_der_1024];
  47906. byte cert[sizeof(sizeof_client_cert_der_1024)];
  47907. word32 keySz = (word32)sizeof(key);
  47908. word32 certSz = (word32)sizeof(cert);
  47909. XMEMSET(key, 0, keySz);
  47910. XMEMSET(cert, 0, certSz);
  47911. XMEMCPY(key, client_key_der_1024, keySz);
  47912. XMEMCPY(cert, client_cert_der_1024, certSz);
  47913. #else
  47914. unsigned char cert[ONEK_BUF];
  47915. unsigned char key[ONEK_BUF];
  47916. XFILE fp = XBADFILE;
  47917. int certSz;
  47918. int keySz;
  47919. ExpectTrue((fp = XFOPEN("./certs/1024/client-cert.der", "rb")) !=
  47920. XBADFILE);
  47921. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_client_cert_der_1024,
  47922. fp), 0);
  47923. if (fp != XBADFILE) {
  47924. XFCLOSE(fp);
  47925. fp = XBADFILE;
  47926. }
  47927. ExpectTrue((fp = XFOPEN("./certs/1024/client-key.der", "rb")) !=
  47928. XBADFILE);
  47929. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_client_key_der_1024, fp),
  47930. 0);
  47931. if (fp != XBADFILE) {
  47932. XFCLOSE(fp);
  47933. fp = XBADFILE;
  47934. }
  47935. #endif
  47936. #elif defined(HAVE_ECC)
  47937. #if defined(USE_CERT_BUFFERS_256)
  47938. unsigned char cert[sizeof(cliecc_cert_der_256)];
  47939. unsigned char key[sizeof(ecc_clikey_der_256)];
  47940. int certSz = (int)sizeof(cert);
  47941. int keySz = (int)sizeof(key);
  47942. XMEMSET(cert, 0, certSz);
  47943. XMEMSET(key, 0, keySz);
  47944. XMEMCPY(cert, cliecc_cert_der_256, sizeof_cliecc_cert_der_256);
  47945. XMEMCPY(key, ecc_clikey_der_256, sizeof_ecc_clikey_der_256);
  47946. #else
  47947. unsigned char cert[ONEK_BUF];
  47948. unsigned char key[ONEK_BUF];
  47949. XFILE fp = XBADFILE;
  47950. int certSz, keySz;
  47951. ExpectTrue((fp = XFOPEN("./certs/client-ecc-cert.der", "rb")) !=
  47952. XBADFILE);
  47953. ExpectIntGT(certSz = (int)XFREAD(cert, 1, sizeof_cliecc_cert_der_256,
  47954. fp), 0);
  47955. if (fp != XBADFILE) {
  47956. XFCLOSE(fp);
  47957. fp = XBADFILE;
  47958. }
  47959. ExpectTrue((fp = XFOPEN("./certs/client-ecc-key.der", "rb")) !=
  47960. XBADFILE);
  47961. ExpectIntGT(keySz = (int)XFREAD(key, 1, sizeof_ecc_clikey_der_256, fp),
  47962. 0);
  47963. if (fp != XBADFILE) {
  47964. XFCLOSE(fp);
  47965. fp = XBADFILE;
  47966. }
  47967. #endif
  47968. #else
  47969. #error PKCS7 requires ECC or RSA
  47970. #endif
  47971. ExpectNotNull(pkcs7 = wc_PKCS7_New(HEAP_HINT, testDevId));
  47972. /* initialize with DER encoded cert */
  47973. ExpectIntEQ(wc_PKCS7_InitWithCert(pkcs7, (byte*)cert, (word32)certSz), 0);
  47974. /* init rng */
  47975. XMEMSET(&rng, 0, sizeof(WC_RNG));
  47976. ExpectIntEQ(wc_InitRng(&rng), 0);
  47977. if (pkcs7 != NULL) {
  47978. pkcs7->rng = &rng;
  47979. pkcs7->content = (byte*)data; /* not used for ex */
  47980. pkcs7->contentSz = (word32)sizeof(data);
  47981. pkcs7->contentOID = SIGNED_DATA;
  47982. pkcs7->privateKey = key;
  47983. pkcs7->privateKeySz = (word32)sizeof(key);
  47984. pkcs7->encryptOID = RSAk;
  47985. #ifdef NO_SHA
  47986. pkcs7->hashOID = SHA256h;
  47987. #else
  47988. pkcs7->hashOID = SHAh;
  47989. #endif
  47990. pkcs7->signedAttribs = NULL;
  47991. pkcs7->signedAttribsSz = 0;
  47992. }
  47993. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  47994. /* Write PKCS#7 PEM to BIO, the function converts the DER to PEM cert*/
  47995. ExpectIntEQ(PEM_write_bio_PKCS7(bio, pkcs7), WOLFSSL_SUCCESS);
  47996. /* Read PKCS#7 PEM from BIO */
  47997. ret = wolfSSL_BIO_get_mem_data(bio, &cert_buf);
  47998. ExpectIntGE(ret, 0);
  47999. BIO_free(bio);
  48000. wc_PKCS7_Free(pkcs7);
  48001. wc_FreeRng(&rng);
  48002. #endif
  48003. return EXPECT_RESULT();
  48004. }
  48005. #ifdef HAVE_SMIME
  48006. /* // NOLINTBEGIN(clang-analyzer-unix.Stream) */
  48007. static int test_wolfSSL_SMIME_read_PKCS7(void)
  48008. {
  48009. EXPECT_DECLS;
  48010. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_FILESYSTEM) && \
  48011. !defined(NO_RSA)
  48012. PKCS7* pkcs7 = NULL;
  48013. BIO* bio = NULL;
  48014. BIO* bcont = NULL;
  48015. BIO* out = NULL;
  48016. const byte* outBuf = NULL;
  48017. int outBufLen = 0;
  48018. static const char contTypeText[] = "Content-Type: text/plain\r\n\r\n";
  48019. XFILE smimeTestFile = XBADFILE;
  48020. ExpectTrue((smimeTestFile = XFOPEN("./certs/test/smime-test.p7s", "r")) !=
  48021. XBADFILE);
  48022. /* smime-test.p7s */
  48023. bio = wolfSSL_BIO_new(wolfSSL_BIO_s_file());
  48024. ExpectNotNull(bio);
  48025. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  48026. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  48027. ExpectNotNull(pkcs7);
  48028. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  48029. PKCS7_NOVERIFY), SSL_SUCCESS);
  48030. if (smimeTestFile != XBADFILE) {
  48031. XFCLOSE(smimeTestFile);
  48032. smimeTestFile = XBADFILE;
  48033. }
  48034. if (bcont) BIO_free(bcont);
  48035. bcont = NULL;
  48036. wolfSSL_PKCS7_free(pkcs7);
  48037. pkcs7 = NULL;
  48038. /* smime-test-multipart.p7s */
  48039. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart.p7s", "r");
  48040. ExpectFalse(smimeTestFile == XBADFILE);
  48041. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  48042. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  48043. ExpectNotNull(pkcs7);
  48044. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  48045. PKCS7_NOVERIFY), SSL_SUCCESS);
  48046. if (smimeTestFile != XBADFILE) {
  48047. XFCLOSE(smimeTestFile);
  48048. smimeTestFile = XBADFILE;
  48049. }
  48050. if (bcont) BIO_free(bcont);
  48051. bcont = NULL;
  48052. wolfSSL_PKCS7_free(pkcs7);
  48053. pkcs7 = NULL;
  48054. /* smime-test-multipart-badsig.p7s */
  48055. smimeTestFile = XFOPEN("./certs/test/smime-test-multipart-badsig.p7s", "r");
  48056. ExpectFalse(smimeTestFile == XBADFILE);
  48057. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  48058. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  48059. ExpectNotNull(pkcs7); /* can read in the unverified smime bundle */
  48060. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  48061. PKCS7_NOVERIFY), SSL_FAILURE);
  48062. if (smimeTestFile != XBADFILE) {
  48063. XFCLOSE(smimeTestFile);
  48064. smimeTestFile = XBADFILE;
  48065. }
  48066. if (bcont) BIO_free(bcont);
  48067. bcont = NULL;
  48068. wolfSSL_PKCS7_free(pkcs7);
  48069. pkcs7 = NULL;
  48070. /* smime-test-canon.p7s */
  48071. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  48072. ExpectFalse(smimeTestFile == XBADFILE);
  48073. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  48074. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  48075. ExpectNotNull(pkcs7);
  48076. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, NULL,
  48077. PKCS7_NOVERIFY), SSL_SUCCESS);
  48078. if (smimeTestFile != XBADFILE) {
  48079. XFCLOSE(smimeTestFile);
  48080. smimeTestFile = XBADFILE;
  48081. }
  48082. if (bcont) BIO_free(bcont);
  48083. bcont = NULL;
  48084. wolfSSL_PKCS7_free(pkcs7);
  48085. pkcs7 = NULL;
  48086. /* Test PKCS7_TEXT, PKCS7_verify() should remove Content-Type: text/plain */
  48087. smimeTestFile = XFOPEN("./certs/test/smime-test-canon.p7s", "r");
  48088. ExpectFalse(smimeTestFile == XBADFILE);
  48089. ExpectIntEQ(wolfSSL_BIO_set_fp(bio, smimeTestFile, BIO_CLOSE), SSL_SUCCESS);
  48090. pkcs7 = wolfSSL_SMIME_read_PKCS7(bio, &bcont);
  48091. ExpectNotNull(pkcs7);
  48092. out = wolfSSL_BIO_new(BIO_s_mem());
  48093. ExpectNotNull(out);
  48094. ExpectIntEQ(wolfSSL_PKCS7_verify(pkcs7, NULL, NULL, bcont, out,
  48095. PKCS7_NOVERIFY | PKCS7_TEXT), SSL_SUCCESS);
  48096. ExpectIntGT((outBufLen = BIO_get_mem_data(out, &outBuf)), 0);
  48097. /* Content-Type should not show up at beginning of output buffer */
  48098. ExpectIntGT(outBufLen, XSTRLEN(contTypeText));
  48099. ExpectIntGT(XMEMCMP(outBuf, contTypeText, XSTRLEN(contTypeText)), 0);
  48100. BIO_free(out);
  48101. BIO_free(bio);
  48102. if (bcont) BIO_free(bcont);
  48103. wolfSSL_PKCS7_free(pkcs7);
  48104. #endif
  48105. return EXPECT_RESULT();
  48106. }
  48107. /* // NOLINTEND(clang-analyzer-unix.Stream) */
  48108. static int test_wolfSSL_SMIME_write_PKCS7(void)
  48109. {
  48110. EXPECT_DECLS;
  48111. #if defined(OPENSSL_ALL) && defined(HAVE_PKCS7) && !defined(NO_RSA)
  48112. PKCS7* p7 = NULL;
  48113. PKCS7* p7Ver = NULL;
  48114. int flags = 0;
  48115. byte data[] = "Test data to encode.";
  48116. const char* cert = "./certs/server-cert.pem";
  48117. const char* key = "./certs/server-key.pem";
  48118. const char* ca = "./certs/ca-cert.pem";
  48119. WOLFSSL_BIO* certBio = NULL;
  48120. WOLFSSL_BIO* keyBio = NULL;
  48121. WOLFSSL_BIO* caBio = NULL;
  48122. WOLFSSL_BIO* inBio = NULL;
  48123. WOLFSSL_BIO* outBio = NULL;
  48124. WOLFSSL_BIO* content = NULL;
  48125. X509* signCert = NULL;
  48126. EVP_PKEY* signKey = NULL;
  48127. X509* caCert = NULL;
  48128. X509_STORE* store = NULL;
  48129. /* read signer cert/key into BIO */
  48130. ExpectNotNull(certBio = BIO_new_file(cert, "r"));
  48131. ExpectNotNull(keyBio = BIO_new_file(key, "r"));
  48132. ExpectNotNull(signCert = PEM_read_bio_X509(certBio, NULL, 0, NULL));
  48133. ExpectNotNull(signKey = PEM_read_bio_PrivateKey(keyBio, NULL, 0, NULL));
  48134. /* read CA cert into store (for verify) */
  48135. ExpectNotNull(caBio = BIO_new_file(ca, "r"));
  48136. ExpectNotNull(caCert = PEM_read_bio_X509(caBio, NULL, 0, NULL));
  48137. ExpectNotNull(store = X509_STORE_new());
  48138. ExpectIntEQ(X509_STORE_add_cert(store, caCert), 1);
  48139. /* generate and verify SMIME: not detached */
  48140. {
  48141. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  48142. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  48143. flags = PKCS7_STREAM;
  48144. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  48145. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  48146. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  48147. /* bad arg: out NULL */
  48148. ExpectIntEQ(SMIME_write_PKCS7(NULL, p7, inBio, flags), 0);
  48149. /* bad arg: pkcs7 NULL */
  48150. ExpectIntEQ(SMIME_write_PKCS7(outBio, NULL, inBio, flags), 0);
  48151. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  48152. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  48153. BIO_free(content);
  48154. content = NULL;
  48155. BIO_free(inBio);
  48156. inBio = NULL;
  48157. BIO_free(outBio);
  48158. outBio = NULL;
  48159. PKCS7_free(p7Ver);
  48160. p7Ver = NULL;
  48161. PKCS7_free(p7);
  48162. p7 = NULL;
  48163. }
  48164. /* generate and verify SMIME: not detached, add Content-Type */
  48165. {
  48166. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  48167. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  48168. flags = PKCS7_STREAM | PKCS7_TEXT;
  48169. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  48170. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  48171. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  48172. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  48173. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, NULL, NULL, flags), 1);
  48174. BIO_free(content);
  48175. content = NULL;
  48176. BIO_free(inBio);
  48177. inBio = NULL;
  48178. BIO_free(outBio);
  48179. outBio = NULL;
  48180. PKCS7_free(p7Ver);
  48181. p7Ver = NULL;
  48182. PKCS7_free(p7);
  48183. p7 = NULL;
  48184. }
  48185. /* generate and verify SMIME: detached */
  48186. {
  48187. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  48188. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  48189. flags = PKCS7_DETACHED | PKCS7_STREAM;
  48190. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  48191. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  48192. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  48193. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  48194. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  48195. BIO_free(content);
  48196. content = NULL;
  48197. BIO_free(inBio);
  48198. inBio = NULL;
  48199. BIO_free(outBio);
  48200. outBio = NULL;
  48201. PKCS7_free(p7Ver);
  48202. p7Ver = NULL;
  48203. PKCS7_free(p7);
  48204. p7 = NULL;
  48205. }
  48206. /* generate and verify SMIME: PKCS7_TEXT to add Content-Type header */
  48207. {
  48208. ExpectNotNull(inBio = BIO_new(BIO_s_mem()));
  48209. ExpectIntGT(BIO_write(inBio, data, sizeof(data)), 0);
  48210. flags = PKCS7_STREAM | PKCS7_DETACHED | PKCS7_TEXT;
  48211. ExpectNotNull(p7 = PKCS7_sign(signCert, signKey, NULL, inBio, flags));
  48212. ExpectNotNull(outBio = BIO_new(BIO_s_mem()));
  48213. ExpectIntEQ(SMIME_write_PKCS7(outBio, p7, inBio, flags), 1);
  48214. ExpectNotNull(p7Ver = SMIME_read_PKCS7(outBio, &content));
  48215. ExpectIntEQ(PKCS7_verify(p7Ver, NULL, store, content, NULL, flags), 1);
  48216. BIO_free(content);
  48217. content = NULL;
  48218. BIO_free(inBio);
  48219. inBio = NULL;
  48220. BIO_free(outBio);
  48221. outBio = NULL;
  48222. PKCS7_free(p7Ver);
  48223. p7Ver = NULL;
  48224. PKCS7_free(p7);
  48225. p7 = NULL;
  48226. }
  48227. X509_STORE_free(store);
  48228. X509_free(caCert);
  48229. X509_free(signCert);
  48230. EVP_PKEY_free(signKey);
  48231. BIO_free(keyBio);
  48232. BIO_free(certBio);
  48233. BIO_free(caBio);
  48234. #endif
  48235. return EXPECT_RESULT();
  48236. }
  48237. #endif /* HAVE_SMIME */
  48238. #endif /* !NO_BIO */
  48239. /* Test of X509 store use outside of SSL context w/ CRL lookup (ALWAYS
  48240. * returns 0) */
  48241. static int test_X509_STORE_No_SSL_CTX(void)
  48242. {
  48243. EXPECT_DECLS;
  48244. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  48245. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  48246. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  48247. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  48248. defined(HAVE_CRL) && !defined(NO_RSA)
  48249. X509_STORE * store = NULL;
  48250. X509_STORE_CTX * storeCtx = NULL;
  48251. X509_CRL * crl = NULL;
  48252. X509 * ca = NULL;
  48253. X509 * cert = NULL;
  48254. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  48255. const char srvCert[] = "./certs/server-cert.pem";
  48256. const char caCert[] = "./certs/ca-cert.pem";
  48257. const char caDir[] = "./certs/crl/hash_pem";
  48258. XFILE fp = XBADFILE;
  48259. X509_LOOKUP * lookup = NULL;
  48260. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  48261. /* Set up store with CA */
  48262. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  48263. SSL_FILETYPE_PEM)));
  48264. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  48265. /* Add CRL lookup directory to store
  48266. * NOTE: test uses ./certs/crl/hash_pem/0fdb2da4.r0, which is a copy
  48267. * of crl.pem */
  48268. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  48269. X509_LOOKUP_hash_dir())));
  48270. ExpectIntEQ(X509_LOOKUP_ctrl(lookup, X509_L_ADD_DIR, caDir,
  48271. X509_FILETYPE_PEM, NULL), SSL_SUCCESS);
  48272. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  48273. SSL_SUCCESS);
  48274. /* Add CRL to store NOT containing the verified certificate, which
  48275. * forces use of the CRL lookup directory */
  48276. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  48277. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  48278. NULL, NULL));
  48279. if (fp != XBADFILE)
  48280. XFCLOSE(fp);
  48281. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  48282. /* Create verification context outside of an SSL session */
  48283. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  48284. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  48285. SSL_FILETYPE_PEM)));
  48286. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  48287. /* Perform verification, which should NOT indicate CRL missing due to the
  48288. * store CM's X509 store pointer being NULL */
  48289. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  48290. X509_CRL_free(crl);
  48291. X509_STORE_free(store);
  48292. X509_STORE_CTX_free(storeCtx);
  48293. X509_free(cert);
  48294. X509_free(ca);
  48295. #endif
  48296. return EXPECT_RESULT();
  48297. }
  48298. /* Test of X509 store use outside of SSL context w/ CRL lookup, but
  48299. * with X509_LOOKUP_add_dir and X509_FILETYPE_ASN1. */
  48300. static int test_X509_LOOKUP_add_dir(void)
  48301. {
  48302. EXPECT_DECLS;
  48303. #if defined(OPENSSL_ALL) && defined(WOLFSSL_CERT_GEN) && \
  48304. (defined(WOLFSSL_CERT_REQ) || defined(WOLFSSL_CERT_EXT)) && \
  48305. !defined(NO_FILESYSTEM) && !defined(NO_WOLFSSL_DIR) && \
  48306. (defined(OPENSSL_EXTRA) || defined(WOLFSSL_WPAS_SMALL)) && \
  48307. defined(HAVE_CRL) && !defined(NO_RSA)
  48308. X509_STORE * store = NULL;
  48309. X509_STORE_CTX * storeCtx = NULL;
  48310. X509_CRL * crl = NULL;
  48311. X509 * ca = NULL;
  48312. X509 * cert = NULL;
  48313. const char cliCrlPem[] = "./certs/crl/cliCrl.pem";
  48314. const char srvCert[] = "./certs/server-cert.pem";
  48315. const char caCert[] = "./certs/ca-cert.pem";
  48316. const char caDir[] = "./certs/crl/hash_der";
  48317. XFILE fp = XBADFILE;
  48318. X509_LOOKUP * lookup = NULL;
  48319. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  48320. /* Set up store with CA */
  48321. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  48322. SSL_FILETYPE_PEM)));
  48323. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  48324. /* Add CRL lookup directory to store.
  48325. * Test uses ./certs/crl/hash_der/0fdb2da4.r0, which is a copy
  48326. * of crl.der */
  48327. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  48328. X509_LOOKUP_hash_dir())));
  48329. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_ASN1),
  48330. SSL_SUCCESS);
  48331. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  48332. SSL_SUCCESS);
  48333. /* Add CRL to store NOT containing the verified certificate, which
  48334. * forces use of the CRL lookup directory */
  48335. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  48336. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  48337. NULL, NULL));
  48338. if (fp != XBADFILE) {
  48339. XFCLOSE(fp);
  48340. fp = XBADFILE;
  48341. }
  48342. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  48343. /* Create verification context outside of an SSL session */
  48344. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  48345. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  48346. SSL_FILETYPE_PEM)));
  48347. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  48348. /* Perform verification, which should NOT return CRL missing */
  48349. ExpectIntNE(X509_verify_cert(storeCtx), CRL_MISSING);
  48350. X509_CRL_free(crl);
  48351. crl = NULL;
  48352. X509_STORE_free(store);
  48353. store = NULL;
  48354. X509_STORE_CTX_free(storeCtx);
  48355. storeCtx = NULL;
  48356. X509_free(cert);
  48357. cert = NULL;
  48358. X509_free(ca);
  48359. ca = NULL;
  48360. /* Now repeat the same, but look for X509_FILETYPE_PEM.
  48361. * We should get CRL_MISSING at the end, because the lookup
  48362. * dir has only ASN1 CRLs. */
  48363. ExpectNotNull(store = (X509_STORE *)X509_STORE_new());
  48364. ExpectNotNull((ca = wolfSSL_X509_load_certificate_file(caCert,
  48365. SSL_FILETYPE_PEM)));
  48366. ExpectIntEQ(X509_STORE_add_cert(store, ca), SSL_SUCCESS);
  48367. ExpectNotNull((lookup = X509_STORE_add_lookup(store,
  48368. X509_LOOKUP_hash_dir())));
  48369. ExpectIntEQ(X509_LOOKUP_add_dir(lookup, caDir, X509_FILETYPE_PEM),
  48370. SSL_SUCCESS);
  48371. ExpectIntEQ(X509_STORE_set_flags(store, X509_V_FLAG_CRL_CHECK),
  48372. SSL_SUCCESS);
  48373. ExpectTrue((fp = XFOPEN(cliCrlPem, "rb")) != XBADFILE);
  48374. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  48375. NULL, NULL));
  48376. if (fp != XBADFILE) {
  48377. XFCLOSE(fp);
  48378. fp = XBADFILE;
  48379. }
  48380. ExpectIntEQ(X509_STORE_add_crl(store, crl), SSL_SUCCESS);
  48381. ExpectNotNull((storeCtx = X509_STORE_CTX_new()));
  48382. ExpectNotNull((cert = wolfSSL_X509_load_certificate_file(srvCert,
  48383. SSL_FILETYPE_PEM)));
  48384. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, cert, NULL), SSL_SUCCESS);
  48385. /* Now we SHOULD get CRL_MISSING, because we looked for PEM
  48386. * in dir containing only ASN1/DER. */
  48387. ExpectIntEQ(X509_verify_cert(storeCtx), WOLFSSL_FAILURE);
  48388. ExpectIntEQ(X509_STORE_CTX_get_error(storeCtx),
  48389. X509_V_ERR_UNABLE_TO_GET_CRL);
  48390. X509_CRL_free(crl);
  48391. X509_STORE_free(store);
  48392. X509_STORE_CTX_free(storeCtx);
  48393. X509_free(cert);
  48394. X509_free(ca);
  48395. #endif
  48396. return EXPECT_RESULT();
  48397. }
  48398. /*----------------------------------------------------------------------------*
  48399. | Certificate Failure Checks
  48400. *----------------------------------------------------------------------------*/
  48401. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  48402. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  48403. #if !defined(NO_RSA) || defined(HAVE_ECC)
  48404. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  48405. static int verify_sig_cm(const char* ca, byte* cert_buf, size_t cert_sz,
  48406. int type)
  48407. {
  48408. int ret;
  48409. WOLFSSL_CERT_MANAGER* cm = NULL;
  48410. switch (type) {
  48411. case TESTING_RSA:
  48412. #ifdef NO_RSA
  48413. fprintf(stderr, "RSA disabled, skipping test\n");
  48414. return ASN_SIG_CONFIRM_E;
  48415. #else
  48416. break;
  48417. #endif
  48418. case TESTING_ECC:
  48419. #ifndef HAVE_ECC
  48420. fprintf(stderr, "ECC disabled, skipping test\n");
  48421. return ASN_SIG_CONFIRM_E;
  48422. #else
  48423. break;
  48424. #endif
  48425. default:
  48426. fprintf(stderr, "Bad function argument\n");
  48427. return BAD_FUNC_ARG;
  48428. }
  48429. cm = wolfSSL_CertManagerNew();
  48430. if (cm == NULL) {
  48431. fprintf(stderr, "wolfSSL_CertManagerNew failed\n");
  48432. return -1;
  48433. }
  48434. #ifndef NO_FILESYSTEM
  48435. ret = wolfSSL_CertManagerLoadCA(cm, ca, 0);
  48436. if (ret != WOLFSSL_SUCCESS) {
  48437. fprintf(stderr, "wolfSSL_CertManagerLoadCA failed\n");
  48438. wolfSSL_CertManagerFree(cm);
  48439. return ret;
  48440. }
  48441. #else
  48442. (void)ca;
  48443. #endif
  48444. ret = wolfSSL_CertManagerVerifyBuffer(cm, cert_buf, (long int)cert_sz,
  48445. WOLFSSL_FILETYPE_ASN1);
  48446. /* Let ExpectIntEQ handle return code */
  48447. wolfSSL_CertManagerFree(cm);
  48448. return ret;
  48449. }
  48450. #endif
  48451. #if !defined(NO_FILESYSTEM)
  48452. static int test_RsaSigFailure_cm(void)
  48453. {
  48454. EXPECT_DECLS;
  48455. #ifndef NO_RSA
  48456. const char* ca_cert = "./certs/ca-cert.pem";
  48457. const char* server_cert = "./certs/server-cert.der";
  48458. byte* cert_buf = NULL;
  48459. size_t cert_sz = 0;
  48460. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  48461. if (cert_buf != NULL) {
  48462. /* corrupt DER - invert last byte, which is signature */
  48463. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  48464. /* test bad cert */
  48465. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  48466. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  48467. WOLFSSL_FATAL_ERROR);
  48468. #else
  48469. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_RSA),
  48470. ASN_SIG_CONFIRM_E);
  48471. #endif
  48472. }
  48473. /* load_file() uses malloc. */
  48474. if (cert_buf != NULL) {
  48475. free(cert_buf);
  48476. }
  48477. #endif /* !NO_RSA */
  48478. return EXPECT_RESULT();
  48479. }
  48480. static int test_EccSigFailure_cm(void)
  48481. {
  48482. EXPECT_DECLS;
  48483. #ifdef HAVE_ECC
  48484. /* self-signed ECC cert, so use server cert as CA */
  48485. const char* ca_cert = "./certs/ca-ecc-cert.pem";
  48486. const char* server_cert = "./certs/server-ecc.der";
  48487. byte* cert_buf = NULL;
  48488. size_t cert_sz = 0;
  48489. ExpectIntEQ(load_file(server_cert, &cert_buf, &cert_sz), 0);
  48490. if (cert_buf != NULL) {
  48491. /* corrupt DER - invert last byte, which is signature */
  48492. cert_buf[cert_sz-1] = ~cert_buf[cert_sz-1];
  48493. /* test bad cert */
  48494. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  48495. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  48496. WOLFSSL_FATAL_ERROR);
  48497. #else
  48498. ExpectIntEQ(verify_sig_cm(ca_cert, cert_buf, cert_sz, TESTING_ECC),
  48499. ASN_SIG_CONFIRM_E);
  48500. #endif
  48501. }
  48502. /* load_file() uses malloc. */
  48503. if (cert_buf != NULL) {
  48504. free(cert_buf);
  48505. }
  48506. #ifdef FP_ECC
  48507. wc_ecc_fp_free();
  48508. #endif
  48509. #endif /* HAVE_ECC */
  48510. return EXPECT_RESULT();
  48511. }
  48512. #endif /* !NO_FILESYSTEM */
  48513. #endif /* NO_CERTS */
  48514. #ifdef WOLFSSL_TLS13
  48515. #if defined(WOLFSSL_SEND_HRR_COOKIE) && !defined(NO_WOLFSSL_SERVER)
  48516. #ifdef WC_SHA384_DIGEST_SIZE
  48517. static byte fixedKey[WC_SHA384_DIGEST_SIZE] = { 0, };
  48518. #else
  48519. static byte fixedKey[WC_SHA256_DIGEST_SIZE] = { 0, };
  48520. #endif
  48521. #endif
  48522. #ifdef WOLFSSL_EARLY_DATA
  48523. static const char earlyData[] = "Early Data";
  48524. static char earlyDataBuffer[1];
  48525. #endif
  48526. static int test_tls13_apis(void)
  48527. {
  48528. EXPECT_DECLS;
  48529. int ret;
  48530. #ifndef WOLFSSL_NO_TLS12
  48531. #ifndef NO_WOLFSSL_CLIENT
  48532. WOLFSSL_CTX* clientTls12Ctx = NULL;
  48533. WOLFSSL* clientTls12Ssl = NULL;
  48534. #endif
  48535. #ifndef NO_WOLFSSL_SERVER
  48536. WOLFSSL_CTX* serverTls12Ctx = NULL;
  48537. WOLFSSL* serverTls12Ssl = NULL;
  48538. #endif
  48539. #endif
  48540. #ifndef NO_WOLFSSL_CLIENT
  48541. WOLFSSL_CTX* clientCtx = NULL;
  48542. WOLFSSL* clientSsl = NULL;
  48543. #endif
  48544. #ifndef NO_WOLFSSL_SERVER
  48545. WOLFSSL_CTX* serverCtx = NULL;
  48546. WOLFSSL* serverSsl = NULL;
  48547. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  48548. const char* ourCert = svrCertFile;
  48549. const char* ourKey = svrKeyFile;
  48550. #endif
  48551. #endif
  48552. int required;
  48553. #ifdef WOLFSSL_EARLY_DATA
  48554. int outSz;
  48555. #endif
  48556. #if defined(HAVE_ECC) && defined(HAVE_SUPPORTED_CURVES)
  48557. int groups[2] = { WOLFSSL_ECC_SECP256R1,
  48558. #ifdef HAVE_PQC
  48559. WOLFSSL_KYBER_LEVEL1
  48560. #else
  48561. WOLFSSL_ECC_SECP256R1
  48562. #endif
  48563. };
  48564. #if !defined(NO_WOLFSSL_SERVER) || !defined(NO_WOLFSSL_CLIENT)
  48565. int bad_groups[2] = { 0xDEAD, 0xBEEF };
  48566. #endif /* !NO_WOLFSSL_SERVER || !NO_WOLFSSL_CLIENT */
  48567. int numGroups = 2;
  48568. #endif
  48569. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC)
  48570. char groupList[] =
  48571. #ifdef HAVE_CURVE25519
  48572. "X25519:"
  48573. #endif
  48574. #ifdef HAVE_CURVE448
  48575. "X448:"
  48576. #endif
  48577. #ifndef NO_ECC_SECP
  48578. #if (defined(HAVE_ECC521) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 521
  48579. "P-521:secp521r1:"
  48580. #endif
  48581. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 384
  48582. "P-384:secp384r1:"
  48583. #endif
  48584. #if (!defined(NO_ECC256) || defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 256
  48585. "P-256:secp256r1"
  48586. #if defined(HAVE_PQC) && defined(HAVE_LIBOQS)
  48587. ":P256_KYBER_LEVEL1"
  48588. #endif
  48589. #endif
  48590. #endif /* !defined(NO_ECC_SECP) */
  48591. #ifdef HAVE_PQC
  48592. ":KYBER_LEVEL1"
  48593. #endif
  48594. "";
  48595. #endif /* defined(OPENSSL_EXTRA) && defined(HAVE_ECC) */
  48596. (void)ret;
  48597. #ifndef WOLFSSL_NO_TLS12
  48598. #ifndef NO_WOLFSSL_CLIENT
  48599. clientTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method());
  48600. clientTls12Ssl = wolfSSL_new(clientTls12Ctx);
  48601. #endif
  48602. #ifndef NO_WOLFSSL_SERVER
  48603. serverTls12Ctx = wolfSSL_CTX_new(wolfTLSv1_2_server_method());
  48604. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  48605. wolfSSL_CTX_use_certificate_chain_file(serverTls12Ctx, ourCert);
  48606. wolfSSL_CTX_use_PrivateKey_file(serverTls12Ctx, ourKey,
  48607. WOLFSSL_FILETYPE_PEM);
  48608. #endif
  48609. serverTls12Ssl = wolfSSL_new(serverTls12Ctx);
  48610. #endif
  48611. #endif
  48612. #ifndef NO_WOLFSSL_CLIENT
  48613. clientCtx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  48614. clientSsl = wolfSSL_new(clientCtx);
  48615. #endif
  48616. #ifndef NO_WOLFSSL_SERVER
  48617. serverCtx = wolfSSL_CTX_new(wolfTLSv1_3_server_method());
  48618. #if !defined(NO_CERTS) && !defined(NO_FILESYSTEM)
  48619. wolfSSL_CTX_use_certificate_chain_file(serverCtx, ourCert);
  48620. wolfSSL_CTX_use_PrivateKey_file(serverCtx, ourKey, WOLFSSL_FILETYPE_PEM);
  48621. #endif
  48622. serverSsl = wolfSSL_new(serverCtx);
  48623. ExpectNotNull(serverSsl);
  48624. #endif
  48625. #ifdef WOLFSSL_SEND_HRR_COOKIE
  48626. ExpectIntEQ(wolfSSL_send_hrr_cookie(NULL, NULL, 0), BAD_FUNC_ARG);
  48627. #ifndef NO_WOLFSSL_CLIENT
  48628. ExpectIntEQ(wolfSSL_send_hrr_cookie(clientSsl, NULL, 0), SIDE_ERROR);
  48629. #endif
  48630. #ifndef NO_WOLFSSL_SERVER
  48631. #ifndef WOLFSSL_NO_TLS12
  48632. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverTls12Ssl, NULL, 0),
  48633. BAD_FUNC_ARG);
  48634. #endif
  48635. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, NULL, 0), WOLFSSL_SUCCESS);
  48636. ExpectIntEQ(wolfSSL_send_hrr_cookie(serverSsl, fixedKey, sizeof(fixedKey)),
  48637. WOLFSSL_SUCCESS);
  48638. #endif
  48639. #endif
  48640. #ifdef HAVE_SUPPORTED_CURVES
  48641. #ifdef HAVE_ECC
  48642. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  48643. BAD_FUNC_ARG);
  48644. #ifndef NO_WOLFSSL_SERVER
  48645. do {
  48646. ret = wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_SECP256R1);
  48647. #ifdef WOLFSSL_ASYNC_CRYPT
  48648. if (ret == WC_PENDING_E)
  48649. wolfSSL_AsyncPoll(serverSsl, WOLF_POLL_FLAG_CHECK_HW);
  48650. #endif
  48651. }
  48652. while (ret == WC_PENDING_E);
  48653. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  48654. #endif
  48655. #ifndef NO_WOLFSSL_CLIENT
  48656. #ifndef WOLFSSL_NO_TLS12
  48657. do {
  48658. ret = wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1);
  48659. #ifdef WOLFSSL_ASYNC_CRYPT
  48660. if (ret == WC_PENDING_E)
  48661. wolfSSL_AsyncPoll(clientTls12Ssl, WOLF_POLL_FLAG_CHECK_HW);
  48662. #endif
  48663. }
  48664. while (ret == WC_PENDING_E);
  48665. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  48666. #endif
  48667. do {
  48668. ret = wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1);
  48669. #ifdef WOLFSSL_ASYNC_CRYPT
  48670. if (ret == WC_PENDING_E)
  48671. wolfSSL_AsyncPoll(clientSsl, WOLF_POLL_FLAG_CHECK_HW);
  48672. #endif
  48673. }
  48674. while (ret == WC_PENDING_E);
  48675. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  48676. #endif
  48677. #elif defined(HAVE_CURVE25519)
  48678. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X25519), BAD_FUNC_ARG);
  48679. #ifndef NO_WOLFSSL_SERVER
  48680. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X25519),
  48681. WOLFSSL_SUCCESS);
  48682. #endif
  48683. #ifndef NO_WOLFSSL_CLIENT
  48684. #ifndef WOLFSSL_NO_TLS12
  48685. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X25519),
  48686. WOLFSSL_SUCCESS);
  48687. #endif
  48688. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X25519),
  48689. WOLFSSL_SUCCESS);
  48690. #endif
  48691. #elif defined(HAVE_CURVE448)
  48692. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_X448), BAD_FUNC_ARG);
  48693. #ifndef NO_WOLFSSL_SERVER
  48694. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_ECC_X448),
  48695. WOLFSSL_SUCCESS);
  48696. #endif
  48697. #ifndef NO_WOLFSSL_CLIENT
  48698. #ifndef WOLFSSL_NO_TLS12
  48699. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_X448),
  48700. WOLFSSL_SUCCESS);
  48701. #endif
  48702. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_X448),
  48703. WOLFSSL_SUCCESS);
  48704. #endif
  48705. #else
  48706. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_ECC_SECP256R1),
  48707. BAD_FUNC_ARG);
  48708. #ifndef NO_WOLFSSL_CLIENT
  48709. #ifndef WOLFSSL_NO_TLS12
  48710. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_ECC_SECP256R1),
  48711. NOT_COMPILED_IN);
  48712. #endif
  48713. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_ECC_SECP256R1),
  48714. NOT_COMPILED_IN);
  48715. #endif
  48716. #endif
  48717. #if defined(HAVE_PQC)
  48718. ExpectIntEQ(wolfSSL_UseKeyShare(NULL, WOLFSSL_KYBER_LEVEL3), BAD_FUNC_ARG);
  48719. #ifndef NO_WOLFSSL_SERVER
  48720. ExpectIntEQ(wolfSSL_UseKeyShare(serverSsl, WOLFSSL_KYBER_LEVEL3),
  48721. WOLFSSL_SUCCESS);
  48722. #endif
  48723. #ifndef NO_WOLFSSL_CLIENT
  48724. #ifndef WOLFSSL_NO_TLS12
  48725. ExpectIntEQ(wolfSSL_UseKeyShare(clientTls12Ssl, WOLFSSL_KYBER_LEVEL3),
  48726. BAD_FUNC_ARG);
  48727. #endif
  48728. ExpectIntEQ(wolfSSL_UseKeyShare(clientSsl, WOLFSSL_KYBER_LEVEL3),
  48729. WOLFSSL_SUCCESS);
  48730. #endif
  48731. #endif
  48732. ExpectIntEQ(wolfSSL_NoKeyShares(NULL), BAD_FUNC_ARG);
  48733. #ifndef NO_WOLFSSL_SERVER
  48734. ExpectIntEQ(wolfSSL_NoKeyShares(serverSsl), SIDE_ERROR);
  48735. #endif
  48736. #ifndef NO_WOLFSSL_CLIENT
  48737. #ifndef WOLFSSL_NO_TLS12
  48738. ExpectIntEQ(wolfSSL_NoKeyShares(clientTls12Ssl), WOLFSSL_SUCCESS);
  48739. #endif
  48740. ExpectIntEQ(wolfSSL_NoKeyShares(clientSsl), WOLFSSL_SUCCESS);
  48741. #endif
  48742. #endif /* HAVE_SUPPORTED_CURVES */
  48743. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  48744. #ifndef NO_WOLFSSL_CLIENT
  48745. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(clientCtx), SIDE_ERROR);
  48746. #endif
  48747. #ifndef NO_WOLFSSL_SERVER
  48748. #ifndef WOLFSSL_NO_TLS12
  48749. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverTls12Ctx), BAD_FUNC_ARG);
  48750. #endif
  48751. ExpectIntEQ(wolfSSL_CTX_no_ticket_TLSv13(serverCtx), 0);
  48752. #endif
  48753. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(NULL), BAD_FUNC_ARG);
  48754. #ifndef NO_WOLFSSL_CLIENT
  48755. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(clientSsl), SIDE_ERROR);
  48756. #endif
  48757. #ifndef NO_WOLFSSL_SERVER
  48758. #ifndef WOLFSSL_NO_TLS12
  48759. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverTls12Ssl), BAD_FUNC_ARG);
  48760. #endif
  48761. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(serverSsl), 0);
  48762. #endif
  48763. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(NULL), BAD_FUNC_ARG);
  48764. #ifndef NO_WOLFSSL_CLIENT
  48765. #ifndef WOLFSSL_NO_TLS12
  48766. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientTls12Ctx), BAD_FUNC_ARG);
  48767. #endif
  48768. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(clientCtx), 0);
  48769. #endif
  48770. #ifndef NO_WOLFSSL_SERVER
  48771. ExpectIntEQ(wolfSSL_CTX_no_dhe_psk(serverCtx), 0);
  48772. #endif
  48773. ExpectIntEQ(wolfSSL_no_dhe_psk(NULL), BAD_FUNC_ARG);
  48774. #ifndef NO_WOLFSSL_CLIENT
  48775. #ifndef WOLFSSL_NO_TLS12
  48776. ExpectIntEQ(wolfSSL_no_dhe_psk(clientTls12Ssl), BAD_FUNC_ARG);
  48777. #endif
  48778. ExpectIntEQ(wolfSSL_no_dhe_psk(clientSsl), 0);
  48779. #endif
  48780. #ifndef NO_WOLFSSL_SERVER
  48781. ExpectIntEQ(wolfSSL_no_dhe_psk(serverSsl), 0);
  48782. #endif
  48783. ExpectIntEQ(wolfSSL_update_keys(NULL), BAD_FUNC_ARG);
  48784. #ifndef NO_WOLFSSL_CLIENT
  48785. #ifndef WOLFSSL_NO_TLS12
  48786. ExpectIntEQ(wolfSSL_update_keys(clientTls12Ssl), BAD_FUNC_ARG);
  48787. #endif
  48788. ExpectIntEQ(wolfSSL_update_keys(clientSsl), BUILD_MSG_ERROR);
  48789. #endif
  48790. #ifndef NO_WOLFSSL_SERVER
  48791. ExpectIntEQ(wolfSSL_update_keys(serverSsl), BUILD_MSG_ERROR);
  48792. #endif
  48793. ExpectIntEQ(wolfSSL_key_update_response(NULL, NULL), BAD_FUNC_ARG);
  48794. ExpectIntEQ(wolfSSL_key_update_response(NULL, &required), BAD_FUNC_ARG);
  48795. #ifndef NO_WOLFSSL_CLIENT
  48796. #ifndef WOLFSSL_NO_TLS12
  48797. ExpectIntEQ(wolfSSL_key_update_response(clientTls12Ssl, &required),
  48798. BAD_FUNC_ARG);
  48799. #endif
  48800. ExpectIntEQ(wolfSSL_key_update_response(clientSsl, NULL), BAD_FUNC_ARG);
  48801. #endif
  48802. #ifndef NO_WOLFSSL_SERVER
  48803. ExpectIntEQ(wolfSSL_key_update_response(serverSsl, NULL), BAD_FUNC_ARG);
  48804. #endif
  48805. #if !defined(NO_CERTS) && defined(WOLFSSL_POST_HANDSHAKE_AUTH)
  48806. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  48807. #ifndef NO_WOLFSSL_SERVER
  48808. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(serverCtx), SIDE_ERROR);
  48809. #endif
  48810. #ifndef NO_WOLFSSL_CLIENT
  48811. #ifndef WOLFSSL_NO_TLS12
  48812. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientTls12Ctx),
  48813. BAD_FUNC_ARG);
  48814. #endif
  48815. ExpectIntEQ(wolfSSL_CTX_allow_post_handshake_auth(clientCtx), 0);
  48816. #endif
  48817. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(NULL), BAD_FUNC_ARG);
  48818. #ifndef NO_WOLFSSL_SERVER
  48819. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(serverSsl), SIDE_ERROR);
  48820. #endif
  48821. #ifndef NO_WOLFSSL_CLIENT
  48822. #ifndef WOLFSSL_NO_TLS12
  48823. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientTls12Ssl),
  48824. BAD_FUNC_ARG);
  48825. #endif
  48826. ExpectIntEQ(wolfSSL_allow_post_handshake_auth(clientSsl), 0);
  48827. #endif
  48828. ExpectIntEQ(wolfSSL_request_certificate(NULL), BAD_FUNC_ARG);
  48829. #ifndef NO_WOLFSSL_CLIENT
  48830. ExpectIntEQ(wolfSSL_request_certificate(clientSsl), SIDE_ERROR);
  48831. #endif
  48832. #ifndef NO_WOLFSSL_SERVER
  48833. #ifndef WOLFSSL_NO_TLS12
  48834. ExpectIntEQ(wolfSSL_request_certificate(serverTls12Ssl),
  48835. BAD_FUNC_ARG);
  48836. #endif
  48837. ExpectIntEQ(wolfSSL_request_certificate(serverSsl), NOT_READY_ERROR);
  48838. #endif
  48839. #endif
  48840. #ifdef HAVE_ECC
  48841. #ifndef WOLFSSL_NO_SERVER_GROUPS_EXT
  48842. ExpectIntEQ(wolfSSL_preferred_group(NULL), BAD_FUNC_ARG);
  48843. #ifndef NO_WOLFSSL_SERVER
  48844. ExpectIntEQ(wolfSSL_preferred_group(serverSsl), SIDE_ERROR);
  48845. #endif
  48846. #ifndef NO_WOLFSSL_CLIENT
  48847. #ifndef WOLFSSL_NO_TLS12
  48848. ExpectIntEQ(wolfSSL_preferred_group(clientTls12Ssl), BAD_FUNC_ARG);
  48849. #endif
  48850. ExpectIntEQ(wolfSSL_preferred_group(clientSsl), NOT_READY_ERROR);
  48851. #endif
  48852. #endif
  48853. #ifdef HAVE_SUPPORTED_CURVES
  48854. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  48855. #ifndef NO_WOLFSSL_CLIENT
  48856. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, NULL, 0), BAD_FUNC_ARG);
  48857. #endif
  48858. ExpectIntEQ(wolfSSL_CTX_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  48859. #ifndef NO_WOLFSSL_CLIENT
  48860. #ifndef WOLFSSL_NO_TLS12
  48861. ExpectIntEQ(wolfSSL_CTX_set_groups(clientTls12Ctx, groups, numGroups),
  48862. WOLFSSL_SUCCESS);
  48863. #endif
  48864. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups,
  48865. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  48866. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, groups, numGroups),
  48867. WOLFSSL_SUCCESS);
  48868. ExpectIntEQ(wolfSSL_CTX_set_groups(clientCtx, bad_groups, numGroups),
  48869. BAD_FUNC_ARG);
  48870. #endif
  48871. #ifndef NO_WOLFSSL_SERVER
  48872. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, groups, numGroups),
  48873. WOLFSSL_SUCCESS);
  48874. ExpectIntEQ(wolfSSL_CTX_set_groups(serverCtx, bad_groups, numGroups),
  48875. BAD_FUNC_ARG);
  48876. #endif
  48877. ExpectIntEQ(wolfSSL_set_groups(NULL, NULL, 0), BAD_FUNC_ARG);
  48878. #ifndef NO_WOLFSSL_CLIENT
  48879. ExpectIntEQ(wolfSSL_set_groups(clientSsl, NULL, 0), BAD_FUNC_ARG);
  48880. #endif
  48881. ExpectIntEQ(wolfSSL_set_groups(NULL, groups, numGroups), BAD_FUNC_ARG);
  48882. #ifndef NO_WOLFSSL_CLIENT
  48883. #ifndef WOLFSSL_NO_TLS12
  48884. ExpectIntEQ(wolfSSL_set_groups(clientTls12Ssl, groups, numGroups),
  48885. WOLFSSL_SUCCESS);
  48886. #endif
  48887. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups,
  48888. WOLFSSL_MAX_GROUP_COUNT + 1), BAD_FUNC_ARG);
  48889. ExpectIntEQ(wolfSSL_set_groups(clientSsl, groups, numGroups),
  48890. WOLFSSL_SUCCESS);
  48891. ExpectIntEQ(wolfSSL_set_groups(clientSsl, bad_groups, numGroups),
  48892. BAD_FUNC_ARG);
  48893. #endif
  48894. #ifndef NO_WOLFSSL_SERVER
  48895. ExpectIntEQ(wolfSSL_set_groups(serverSsl, groups, numGroups),
  48896. WOLFSSL_SUCCESS);
  48897. ExpectIntEQ(wolfSSL_set_groups(serverSsl, bad_groups, numGroups),
  48898. BAD_FUNC_ARG);
  48899. #endif
  48900. #ifdef OPENSSL_EXTRA
  48901. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  48902. #ifndef NO_WOLFSSL_CLIENT
  48903. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, NULL),
  48904. WOLFSSL_FAILURE);
  48905. #endif
  48906. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(NULL, groupList),
  48907. WOLFSSL_FAILURE);
  48908. #ifndef NO_WOLFSSL_CLIENT
  48909. #ifndef WOLFSSL_NO_TLS12
  48910. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientTls12Ctx, groupList),
  48911. WOLFSSL_SUCCESS);
  48912. #endif
  48913. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(clientCtx, groupList),
  48914. WOLFSSL_SUCCESS);
  48915. #endif
  48916. #ifndef NO_WOLFSSL_SERVER
  48917. ExpectIntEQ(wolfSSL_CTX_set1_groups_list(serverCtx, groupList),
  48918. WOLFSSL_SUCCESS);
  48919. #endif
  48920. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, NULL), WOLFSSL_FAILURE);
  48921. #ifndef NO_WOLFSSL_CLIENT
  48922. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, NULL), WOLFSSL_FAILURE);
  48923. #endif
  48924. ExpectIntEQ(wolfSSL_set1_groups_list(NULL, groupList), WOLFSSL_FAILURE);
  48925. #ifndef NO_WOLFSSL_CLIENT
  48926. #ifndef WOLFSSL_NO_TLS12
  48927. ExpectIntEQ(wolfSSL_set1_groups_list(clientTls12Ssl, groupList),
  48928. WOLFSSL_SUCCESS);
  48929. #endif
  48930. ExpectIntEQ(wolfSSL_set1_groups_list(clientSsl, groupList),
  48931. WOLFSSL_SUCCESS);
  48932. #endif
  48933. #ifndef NO_WOLFSSL_SERVER
  48934. ExpectIntEQ(wolfSSL_set1_groups_list(serverSsl, groupList),
  48935. WOLFSSL_SUCCESS);
  48936. #endif
  48937. #endif /* OPENSSL_EXTRA */
  48938. #endif /* HAVE_SUPPORTED_CURVES */
  48939. #endif /* HAVE_ECC */
  48940. #ifdef WOLFSSL_EARLY_DATA
  48941. #ifndef OPENSSL_EXTRA
  48942. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  48943. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  48944. #else
  48945. ExpectIntEQ(SSL_CTX_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  48946. ExpectIntEQ(SSL_CTX_get_max_early_data(NULL), BAD_FUNC_ARG);
  48947. #endif
  48948. #ifndef NO_WOLFSSL_CLIENT
  48949. #ifndef OPENSSL_EXTRA
  48950. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  48951. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  48952. #else
  48953. ExpectIntEQ(SSL_CTX_set_max_early_data(clientCtx, 0), SIDE_ERROR);
  48954. ExpectIntEQ(SSL_CTX_get_max_early_data(clientCtx), SIDE_ERROR);
  48955. #endif
  48956. #endif
  48957. #ifndef NO_WOLFSSL_SERVER
  48958. #ifndef WOLFSSL_NO_TLS12
  48959. #ifndef OPENSSL_EXTRA
  48960. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  48961. BAD_FUNC_ARG);
  48962. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  48963. #else
  48964. ExpectIntEQ(SSL_CTX_set_max_early_data(serverTls12Ctx, 0),
  48965. BAD_FUNC_ARG);
  48966. ExpectIntEQ(SSL_CTX_get_max_early_data(serverTls12Ctx), BAD_FUNC_ARG);
  48967. #endif
  48968. #endif
  48969. #ifndef OPENSSL_EXTRA
  48970. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  48971. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32),
  48972. WOLFSSL_SUCCESS);
  48973. #else
  48974. ExpectIntEQ(wolfSSL_CTX_set_max_early_data(serverCtx, 32), 0);
  48975. #endif
  48976. ExpectIntEQ(wolfSSL_CTX_get_max_early_data(serverCtx), 32);
  48977. #else
  48978. ExpectIntEQ(SSL_CTX_set_max_early_data(serverCtx, 32), 1);
  48979. ExpectIntEQ(SSL_CTX_get_max_early_data(serverCtx), 32);
  48980. #endif
  48981. #endif
  48982. #ifndef OPENSSL_EXTRA
  48983. ExpectIntEQ(wolfSSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  48984. ExpectIntEQ(wolfSSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  48985. #else
  48986. ExpectIntEQ(SSL_set_max_early_data(NULL, 0), BAD_FUNC_ARG);
  48987. ExpectIntEQ(SSL_get_max_early_data(NULL), BAD_FUNC_ARG);
  48988. #endif
  48989. #ifndef NO_WOLFSSL_CLIENT
  48990. #ifndef OPENSSL_EXTRA
  48991. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  48992. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  48993. #else
  48994. ExpectIntEQ(wolfSSL_set_max_early_data(clientSsl, 17), 0);
  48995. #endif
  48996. ExpectIntEQ(wolfSSL_get_max_early_data(clientSsl), 17);
  48997. #else
  48998. ExpectIntEQ(SSL_set_max_early_data(clientSsl, 17), WOLFSSL_SUCCESS);
  48999. ExpectIntEQ(SSL_get_max_early_data(clientSsl), 17);
  49000. #endif
  49001. #endif
  49002. #ifndef NO_WOLFSSL_SERVER
  49003. #ifndef WOLFSSL_NO_TLS12
  49004. #ifndef OPENSSL_EXTRA
  49005. ExpectIntEQ(wolfSSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  49006. ExpectIntEQ(wolfSSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  49007. #else
  49008. ExpectIntEQ(SSL_set_max_early_data(serverTls12Ssl, 0), BAD_FUNC_ARG);
  49009. ExpectIntEQ(SSL_get_max_early_data(serverTls12Ssl), BAD_FUNC_ARG);
  49010. #endif
  49011. #endif
  49012. #ifndef OPENSSL_EXTRA
  49013. #ifdef WOLFSSL_ERROR_CODE_OPENSSL
  49014. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), WOLFSSL_SUCCESS);
  49015. #else
  49016. ExpectIntEQ(wolfSSL_set_max_early_data(serverSsl, 16), 0);
  49017. #endif
  49018. ExpectIntEQ(wolfSSL_get_max_early_data(serverSsl), 16);
  49019. #else
  49020. ExpectIntEQ(SSL_set_max_early_data(serverSsl, 16), 1);
  49021. ExpectIntEQ(SSL_get_max_early_data(serverSsl), 16);
  49022. #endif
  49023. #endif
  49024. ExpectIntEQ(wolfSSL_write_early_data(NULL, earlyData, sizeof(earlyData),
  49025. &outSz), BAD_FUNC_ARG);
  49026. #ifndef NO_WOLFSSL_CLIENT
  49027. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, NULL, sizeof(earlyData),
  49028. &outSz), BAD_FUNC_ARG);
  49029. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData, -1, &outSz),
  49030. BAD_FUNC_ARG);
  49031. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  49032. sizeof(earlyData), NULL), BAD_FUNC_ARG);
  49033. #endif
  49034. #ifndef NO_WOLFSSL_SERVER
  49035. ExpectIntEQ(wolfSSL_write_early_data(serverSsl, earlyData,
  49036. sizeof(earlyData), &outSz), SIDE_ERROR);
  49037. #endif
  49038. #ifndef NO_WOLFSSL_CLIENT
  49039. #ifndef WOLFSSL_NO_TLS12
  49040. ExpectIntEQ(wolfSSL_write_early_data(clientTls12Ssl, earlyData,
  49041. sizeof(earlyData), &outSz), BAD_FUNC_ARG);
  49042. #endif
  49043. ExpectIntEQ(wolfSSL_write_early_data(clientSsl, earlyData,
  49044. sizeof(earlyData), &outSz), WOLFSSL_FATAL_ERROR);
  49045. #endif
  49046. ExpectIntEQ(wolfSSL_read_early_data(NULL, earlyDataBuffer,
  49047. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  49048. #ifndef NO_WOLFSSL_SERVER
  49049. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, NULL,
  49050. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  49051. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer, -1,
  49052. &outSz), BAD_FUNC_ARG);
  49053. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  49054. sizeof(earlyDataBuffer), NULL), BAD_FUNC_ARG);
  49055. #endif
  49056. #ifndef NO_WOLFSSL_CLIENT
  49057. ExpectIntEQ(wolfSSL_read_early_data(clientSsl, earlyDataBuffer,
  49058. sizeof(earlyDataBuffer), &outSz), SIDE_ERROR);
  49059. #endif
  49060. #ifndef NO_WOLFSSL_SERVER
  49061. #ifndef WOLFSSL_NO_TLS12
  49062. ExpectIntEQ(wolfSSL_read_early_data(serverTls12Ssl, earlyDataBuffer,
  49063. sizeof(earlyDataBuffer), &outSz), BAD_FUNC_ARG);
  49064. #endif
  49065. ExpectIntEQ(wolfSSL_read_early_data(serverSsl, earlyDataBuffer,
  49066. sizeof(earlyDataBuffer), &outSz), WOLFSSL_FATAL_ERROR);
  49067. #endif
  49068. #endif
  49069. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_EARLY_DATA)
  49070. ExpectIntLT(SSL_get_early_data_status(NULL), 0);
  49071. #endif
  49072. #ifndef NO_WOLFSSL_SERVER
  49073. wolfSSL_free(serverSsl);
  49074. wolfSSL_CTX_free(serverCtx);
  49075. #endif
  49076. #ifndef NO_WOLFSSL_CLIENT
  49077. wolfSSL_free(clientSsl);
  49078. wolfSSL_CTX_free(clientCtx);
  49079. #endif
  49080. #ifndef WOLFSSL_NO_TLS12
  49081. #ifndef NO_WOLFSSL_SERVER
  49082. wolfSSL_free(serverTls12Ssl);
  49083. wolfSSL_CTX_free(serverTls12Ctx);
  49084. #endif
  49085. #ifndef NO_WOLFSSL_CLIENT
  49086. wolfSSL_free(clientTls12Ssl);
  49087. wolfSSL_CTX_free(clientTls12Ctx);
  49088. #endif
  49089. #endif
  49090. return EXPECT_RESULT();
  49091. }
  49092. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  49093. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  49094. defined(BUILD_TLS_AES_256_GCM_SHA384)
  49095. /* Called when writing. */
  49096. static int CsSend(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  49097. {
  49098. (void)ssl;
  49099. (void)buf;
  49100. (void)sz;
  49101. (void)ctx;
  49102. /* Force error return from wolfSSL_accept_TLSv13(). */
  49103. return WANT_WRITE;
  49104. }
  49105. /* Called when reading. */
  49106. static int CsRecv(WOLFSSL* ssl, char* buf, int sz, void* ctx)
  49107. {
  49108. WOLFSSL_BUFFER_INFO* msg = (WOLFSSL_BUFFER_INFO*)ctx;
  49109. int len = (int)msg->length;
  49110. (void)ssl;
  49111. (void)sz;
  49112. /* Pass back as much of message as will fit in buffer. */
  49113. if (len > sz)
  49114. len = sz;
  49115. XMEMCPY(buf, msg->buffer, len);
  49116. /* Move over returned data. */
  49117. msg->buffer += len;
  49118. msg->length -= len;
  49119. /* Amount actually copied. */
  49120. return len;
  49121. }
  49122. #endif
  49123. static int test_tls13_cipher_suites(void)
  49124. {
  49125. EXPECT_DECLS;
  49126. #if defined(HAVE_SESSION_TICKET) && !defined(NO_WOLFSSL_SERVER) && \
  49127. defined(HAVE_ECC) && defined(BUILD_TLS_AES_128_GCM_SHA256) && \
  49128. defined(BUILD_TLS_AES_256_GCM_SHA384)
  49129. WOLFSSL_CTX* ctx = NULL;
  49130. WOLFSSL *ssl = NULL;
  49131. int i;
  49132. byte clientHello[] = {
  49133. 0x16, 0x03, 0x03, 0x01, 0x9b, 0x01, 0x00, 0x01,
  49134. 0x97, 0x03, 0x03, 0xf4, 0x65, 0xbd, 0x22, 0xfe,
  49135. 0x6e, 0xab, 0x66, 0xdd, 0xcf, 0xe9, 0x65, 0x55,
  49136. 0xe8, 0xdf, 0xc3, 0x8e, 0x4b, 0x00, 0xbc, 0xf8,
  49137. 0x23, 0x57, 0x1b, 0xa0, 0xc8, 0xa9, 0xe2, 0x8c,
  49138. 0x91, 0x6e, 0xf9, 0x20, 0xf7, 0x5c, 0xc5, 0x5b,
  49139. 0x75, 0x8c, 0x47, 0x0a, 0x0e, 0xc4, 0x1a, 0xda,
  49140. 0xef, 0x75, 0xe5, 0x21, 0x00, 0x00, 0x00, 0x00,
  49141. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49142. 0x00, 0x00, 0x00, 0x00, 0x00, 0x04,
  49143. /* Cipher suites: 0x13, 0x01 = TLS13-AES128-GCM-SHA256, twice. */
  49144. 0x13, 0x01,
  49145. 0x13, 0x01, 0x01, 0x00, 0x01, 0x4a, 0x00, 0x2d,
  49146. 0x00, 0x03, 0x02, 0x00, 0x01, 0x00, 0x33, 0x00,
  49147. 0x47, 0x00, 0x45, 0x00, 0x17, 0x00, 0x41, 0x04,
  49148. 0x90, 0xfc, 0xe2, 0x97, 0x05, 0x7c, 0xb5, 0x23,
  49149. 0x5d, 0x5f, 0x5b, 0xcd, 0x0c, 0x1e, 0xe0, 0xe9,
  49150. 0xab, 0x38, 0x6b, 0x1e, 0x20, 0x5c, 0x1c, 0x90,
  49151. 0x2a, 0x9e, 0x68, 0x8e, 0x70, 0x05, 0x10, 0xa8,
  49152. 0x02, 0x1b, 0xf9, 0x5c, 0xef, 0xc9, 0xaf, 0xca,
  49153. 0x1a, 0x3b, 0x16, 0x8b, 0xe4, 0x1b, 0x3c, 0x15,
  49154. 0xb8, 0x0d, 0xbd, 0xaf, 0x62, 0x8d, 0xa7, 0x13,
  49155. 0xa0, 0x7c, 0xe0, 0x59, 0x0c, 0x4f, 0x8a, 0x6d,
  49156. 0x00, 0x2b, 0x00, 0x03, 0x02, 0x03, 0x04, 0x00,
  49157. 0x0d, 0x00, 0x20, 0x00, 0x1e, 0x06, 0x03, 0x05,
  49158. 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06, 0x08,
  49159. 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08,
  49160. 0x09, 0x06, 0x01, 0x05, 0x01, 0x04, 0x01, 0x03,
  49161. 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x04, 0x00,
  49162. 0x02, 0x00, 0x17, 0x00, 0x16, 0x00, 0x00, 0x00,
  49163. 0x23, 0x00, 0x00, 0x00, 0x29, 0x00, 0xb9, 0x00,
  49164. 0x94, 0x00, 0x8e, 0x0f, 0x12, 0xfa, 0x84, 0x1f,
  49165. 0x76, 0x94, 0xd7, 0x09, 0x5e, 0xad, 0x08, 0x51,
  49166. 0xb6, 0x80, 0x28, 0x31, 0x8b, 0xfd, 0xc6, 0xbd,
  49167. 0x9e, 0xf5, 0x3b, 0x4d, 0x02, 0xbe, 0x1d, 0x73,
  49168. 0xea, 0x13, 0x68, 0x00, 0x4c, 0xfd, 0x3d, 0x48,
  49169. 0x51, 0xf9, 0x06, 0xbb, 0x92, 0xed, 0x42, 0x9f,
  49170. 0x7f, 0x2c, 0x73, 0x9f, 0xd9, 0xb4, 0xef, 0x05,
  49171. 0x26, 0x5b, 0x60, 0x5c, 0x0a, 0xfc, 0xa3, 0xbd,
  49172. 0x2d, 0x2d, 0x8b, 0xf9, 0xaa, 0x5c, 0x96, 0x3a,
  49173. 0xf2, 0xec, 0xfa, 0xe5, 0x57, 0x2e, 0x87, 0xbe,
  49174. 0x27, 0xc5, 0x3d, 0x4f, 0x5d, 0xdd, 0xde, 0x1c,
  49175. 0x1b, 0xb3, 0xcc, 0x27, 0x27, 0x57, 0x5a, 0xd9,
  49176. 0xea, 0x99, 0x27, 0x23, 0xa6, 0x0e, 0xea, 0x9c,
  49177. 0x0d, 0x85, 0xcb, 0x72, 0xeb, 0xd7, 0x93, 0xe3,
  49178. 0xfe, 0xf7, 0x5c, 0xc5, 0x5b, 0x75, 0x8c, 0x47,
  49179. 0x0a, 0x0e, 0xc4, 0x1a, 0xda, 0xef, 0x75, 0xe5,
  49180. 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49181. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  49182. 0x00, 0xfb, 0x92, 0xce, 0xaa, 0x00, 0x21, 0x20,
  49183. 0xcb, 0x73, 0x25, 0x80, 0x46, 0x78, 0x4f, 0xe5,
  49184. 0x34, 0xf6, 0x91, 0x13, 0x7f, 0xc8, 0x8d, 0xdc,
  49185. 0x81, 0x04, 0xb7, 0x0d, 0x49, 0x85, 0x2e, 0x12,
  49186. 0x7a, 0x07, 0x23, 0xe9, 0x13, 0xa4, 0x6d, 0x8c
  49187. };
  49188. WOLFSSL_BUFFER_INFO msg;
  49189. /* Offset into ClientHello message data of first cipher suite. */
  49190. const int csOff = 78;
  49191. /* Server cipher list. */
  49192. const char* serverCs = "TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256";
  49193. /* Suite list with duplicates. */
  49194. const char* dupCs = "TLS13-AES128-GCM-SHA256:"
  49195. "TLS13-AES128-GCM-SHA256:"
  49196. "TLS13-AES256-GCM-SHA384:"
  49197. "TLS13-AES256-GCM-SHA384:"
  49198. "TLS13-AES128-GCM-SHA256";
  49199. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  49200. const byte dupCsBytes[] = { TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  49201. TLS13_BYTE, TLS_AES_256_GCM_SHA384,
  49202. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  49203. TLS13_BYTE, TLS_AES_128_GCM_SHA256,
  49204. TLS13_BYTE, TLS_AES_256_GCM_SHA384 };
  49205. #endif
  49206. /* Set up wolfSSL context. */
  49207. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  49208. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  49209. WOLFSSL_FILETYPE_PEM));
  49210. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  49211. WOLFSSL_FILETYPE_PEM));
  49212. /* Read from 'msg'. */
  49213. wolfSSL_SetIORecv(ctx, CsRecv);
  49214. /* No where to send to - dummy sender. */
  49215. wolfSSL_SetIOSend(ctx, CsSend);
  49216. /* Test cipher suite list with many copies of a cipher suite. */
  49217. ExpectNotNull(ssl = wolfSSL_new(ctx));
  49218. msg.buffer = clientHello;
  49219. msg.length = (unsigned int)sizeof(clientHello);
  49220. wolfSSL_SetIOReadCtx(ssl, &msg);
  49221. /* Force server to have as many occurrences of same cipher suite as
  49222. * possible. */
  49223. if (ssl != NULL) {
  49224. Suites* suites = (Suites*)WOLFSSL_SUITES(ssl);
  49225. suites->suiteSz = WOLFSSL_MAX_SUITE_SZ;
  49226. for (i = 0; i < suites->suiteSz; i += 2) {
  49227. suites->suites[i + 0] = TLS13_BYTE;
  49228. suites->suites[i + 1] = TLS_AES_128_GCM_SHA256;
  49229. }
  49230. }
  49231. /* Test multiple occurrences of same cipher suite. */
  49232. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  49233. wolfSSL_free(ssl);
  49234. ssl = NULL;
  49235. /* Set client order opposite to server order:
  49236. * TLS13-AES128-GCM-SHA256:TLS13-AES256-GCM-SHA384 */
  49237. clientHello[csOff + 0] = TLS13_BYTE;
  49238. clientHello[csOff + 1] = TLS_AES_128_GCM_SHA256;
  49239. clientHello[csOff + 2] = TLS13_BYTE;
  49240. clientHello[csOff + 3] = TLS_AES_256_GCM_SHA384;
  49241. /* Test server order negotiation. */
  49242. ExpectNotNull(ssl = wolfSSL_new(ctx));
  49243. msg.buffer = clientHello;
  49244. msg.length = (unsigned int)sizeof(clientHello);
  49245. wolfSSL_SetIOReadCtx(ssl, &msg);
  49246. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  49247. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  49248. /* Negotiate cipher suites in server order: TLS13-AES256-GCM-SHA384 */
  49249. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  49250. /* Check refined order - server order. */
  49251. ExpectIntEQ(ssl->suites->suiteSz, 4);
  49252. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  49253. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_256_GCM_SHA384);
  49254. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  49255. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_128_GCM_SHA256);
  49256. wolfSSL_free(ssl);
  49257. ssl = NULL;
  49258. /* Test client order negotiation. */
  49259. ExpectNotNull(ssl = wolfSSL_new(ctx));
  49260. msg.buffer = clientHello;
  49261. msg.length = (unsigned int)sizeof(clientHello);
  49262. wolfSSL_SetIOReadCtx(ssl, &msg);
  49263. /* Server order: TLS13-AES256-GCM-SHA384:TLS13-AES128-GCM-SHA256 */
  49264. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, serverCs), WOLFSSL_SUCCESS);
  49265. ExpectIntEQ(wolfSSL_UseClientSuites(ssl), 0);
  49266. /* Negotiate cipher suites in client order: TLS13-AES128-GCM-SHA256 */
  49267. ExpectIntEQ(wolfSSL_accept_TLSv13(ssl), WOLFSSL_FATAL_ERROR);
  49268. /* Check refined order - client order. */
  49269. ExpectIntEQ(ssl->suites->suiteSz, 4);
  49270. ExpectIntEQ(ssl->suites->suites[0], TLS13_BYTE);
  49271. ExpectIntEQ(ssl->suites->suites[1], TLS_AES_128_GCM_SHA256);
  49272. ExpectIntEQ(ssl->suites->suites[2], TLS13_BYTE);
  49273. ExpectIntEQ(ssl->suites->suites[3], TLS_AES_256_GCM_SHA384);
  49274. wolfSSL_free(ssl);
  49275. ssl = NULL;
  49276. /* Check duplicate detection is working. */
  49277. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, dupCs), WOLFSSL_SUCCESS);
  49278. ExpectIntEQ(ctx->suites->suiteSz, 4);
  49279. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  49280. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_128_GCM_SHA256);
  49281. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  49282. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_256_GCM_SHA384);
  49283. #if defined(OPENSSL_EXTRA) || defined(WOLFSSL_SET_CIPHER_BYTES)
  49284. ExpectIntEQ(wolfSSL_CTX_set_cipher_list_bytes(ctx, dupCsBytes,
  49285. sizeof(dupCsBytes)), WOLFSSL_SUCCESS);
  49286. ExpectIntEQ(ctx->suites->suiteSz, 4);
  49287. ExpectIntEQ(ctx->suites->suites[0], TLS13_BYTE);
  49288. ExpectIntEQ(ctx->suites->suites[1], TLS_AES_256_GCM_SHA384);
  49289. ExpectIntEQ(ctx->suites->suites[2], TLS13_BYTE);
  49290. ExpectIntEQ(ctx->suites->suites[3], TLS_AES_128_GCM_SHA256);
  49291. #endif
  49292. wolfSSL_CTX_free(ctx);
  49293. #endif
  49294. return EXPECT_RESULT();
  49295. }
  49296. #endif
  49297. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  49298. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  49299. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  49300. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  49301. static int my_DhCallback(WOLFSSL* ssl, struct DhKey* key,
  49302. const unsigned char* priv, unsigned int privSz,
  49303. const unsigned char* pubKeyDer, unsigned int pubKeySz,
  49304. unsigned char* out, unsigned int* outlen,
  49305. void* ctx)
  49306. {
  49307. int result;
  49308. /* Test fail when context associated with WOLFSSL is NULL */
  49309. if (ctx == NULL) {
  49310. return -1;
  49311. }
  49312. (void)ssl;
  49313. /* return 0 on success */
  49314. PRIVATE_KEY_UNLOCK();
  49315. result = wc_DhAgree(key, out, outlen, priv, privSz, pubKeyDer, pubKeySz);
  49316. PRIVATE_KEY_LOCK();
  49317. return result;
  49318. }
  49319. static int test_dh_ctx_setup(WOLFSSL_CTX* ctx) {
  49320. EXPECT_DECLS;
  49321. wolfSSL_CTX_SetDhAgreeCb(ctx, my_DhCallback);
  49322. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_128)
  49323. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES128-SHA256"),
  49324. WOLFSSL_SUCCESS);
  49325. #endif
  49326. #if defined(HAVE_AES_CBC) && defined(WOLFSSL_AES_256)
  49327. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(ctx, "DHE-RSA-AES256-SHA256"),
  49328. WOLFSSL_SUCCESS);
  49329. #endif
  49330. return EXPECT_RESULT();
  49331. }
  49332. static int test_dh_ssl_setup(WOLFSSL* ssl)
  49333. {
  49334. EXPECT_DECLS;
  49335. static int dh_test_ctx = 1;
  49336. int ret;
  49337. wolfSSL_SetDhAgreeCtx(ssl, &dh_test_ctx);
  49338. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), dh_test_ctx);
  49339. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  49340. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  49341. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  49342. }
  49343. return EXPECT_RESULT();
  49344. }
  49345. static int test_dh_ssl_setup_fail(WOLFSSL* ssl)
  49346. {
  49347. EXPECT_DECLS;
  49348. int ret;
  49349. wolfSSL_SetDhAgreeCtx(ssl, NULL);
  49350. ExpectNull(wolfSSL_GetDhAgreeCtx(ssl));
  49351. ret = wolfSSL_SetTmpDH_file(ssl, dhParamFile, WOLFSSL_FILETYPE_PEM);
  49352. if (ret != WOLFSSL_SUCCESS && ret != SIDE_ERROR) {
  49353. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  49354. }
  49355. return EXPECT_RESULT();
  49356. }
  49357. #endif
  49358. static int test_DhCallbacks(void)
  49359. {
  49360. EXPECT_DECLS;
  49361. #if !defined(NO_FILESYSTEM) && !defined(NO_DH) && \
  49362. !defined(NO_AES) && defined(HAVE_AES_CBC) && \
  49363. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  49364. WOLFSSL_CTX *ctx = NULL;
  49365. WOLFSSL *ssl = NULL;
  49366. int test;
  49367. test_ssl_cbf func_cb_client;
  49368. test_ssl_cbf func_cb_server;
  49369. /* Test that DH callback APIs work. */
  49370. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  49371. ExpectIntEQ(wolfSSL_CTX_set_cipher_list(NULL, "NONE"), WOLFSSL_FAILURE);
  49372. wolfSSL_CTX_SetDhAgreeCb(ctx, &my_DhCallback);
  49373. /* load client ca cert */
  49374. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, caCertFile, 0),
  49375. WOLFSSL_SUCCESS);
  49376. /* test with NULL arguments */
  49377. wolfSSL_SetDhAgreeCtx(NULL, &test);
  49378. ExpectNull(wolfSSL_GetDhAgreeCtx(NULL));
  49379. /* test success case */
  49380. test = 1;
  49381. ExpectNotNull(ssl = wolfSSL_new(ctx));
  49382. wolfSSL_SetDhAgreeCtx(ssl, &test);
  49383. ExpectIntEQ(*((int*)wolfSSL_GetDhAgreeCtx(ssl)), test);
  49384. wolfSSL_free(ssl);
  49385. wolfSSL_CTX_free(ctx);
  49386. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  49387. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  49388. /* set callbacks to use DH functions */
  49389. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  49390. func_cb_client.ssl_ready = &test_dh_ssl_setup;
  49391. func_cb_client.method = wolfTLSv1_2_client_method;
  49392. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  49393. func_cb_server.ssl_ready = &test_dh_ssl_setup;
  49394. func_cb_server.method = wolfTLSv1_2_server_method;
  49395. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  49396. &func_cb_server, NULL), TEST_SUCCESS);
  49397. /* Test fail */
  49398. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  49399. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  49400. /* set callbacks to use DH functions */
  49401. func_cb_client.ctx_ready = &test_dh_ctx_setup;
  49402. func_cb_client.ssl_ready = &test_dh_ssl_setup_fail;
  49403. func_cb_client.method = wolfTLSv1_2_client_method;
  49404. func_cb_server.ctx_ready = &test_dh_ctx_setup;
  49405. func_cb_server.ssl_ready = &test_dh_ssl_setup_fail;
  49406. func_cb_server.method = wolfTLSv1_2_server_method;
  49407. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  49408. &func_cb_server, NULL), TEST_FAIL);
  49409. #endif
  49410. return EXPECT_RESULT();
  49411. }
  49412. #endif /* HAVE_PK_CALLBACKS */
  49413. #ifdef HAVE_HASHDRBG
  49414. #ifdef TEST_RESEED_INTERVAL
  49415. static int test_wc_RNG_GenerateBlock_Reseed(void)
  49416. {
  49417. EXPECT_DECLS;
  49418. int i;
  49419. WC_RNG rng;
  49420. byte key[32];
  49421. XMEMSET(&rng, 0, sizeof(WC_RNG));
  49422. ExpectIntEQ(wc_InitRng(&rng), 0);
  49423. for (i = 0; i < WC_RESEED_INTERVAL + 10; i++) {
  49424. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  49425. }
  49426. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  49427. return EXPECT_RESULT();
  49428. }
  49429. #endif /* TEST_RESEED_INTERVAL */
  49430. static int test_wc_RNG_GenerateBlock(void)
  49431. {
  49432. EXPECT_DECLS;
  49433. int i;
  49434. WC_RNG rng;
  49435. byte key[32];
  49436. XMEMSET(&rng, 0, sizeof(WC_RNG));
  49437. ExpectIntEQ(wc_InitRng(&rng), 0);
  49438. for (i = 0; i < 10; i++) {
  49439. ExpectIntEQ(wc_RNG_GenerateBlock(&rng, key, sizeof(key)), 0);
  49440. }
  49441. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  49442. return EXPECT_RESULT();
  49443. }
  49444. #endif /* HAVE_HASHDRBG */
  49445. /*
  49446. * Testing get_rand_digit
  49447. */
  49448. static int test_get_rand_digit(void)
  49449. {
  49450. EXPECT_DECLS;
  49451. #if !defined(WC_NO_RNG) && defined(WOLFSSL_PUBLIC_MP)
  49452. WC_RNG rng;
  49453. mp_digit d;
  49454. XMEMSET(&rng, 0, sizeof(WC_RNG));
  49455. ExpectIntEQ(wc_InitRng(&rng), 0);
  49456. ExpectIntEQ(get_rand_digit(&rng, &d), 0);
  49457. ExpectIntEQ(get_rand_digit(NULL, NULL), BAD_FUNC_ARG);
  49458. ExpectIntEQ(get_rand_digit(NULL, &d), BAD_FUNC_ARG);
  49459. ExpectIntEQ(get_rand_digit(&rng, NULL), BAD_FUNC_ARG);
  49460. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  49461. #endif
  49462. return EXPECT_RESULT();
  49463. } /* End test_get_rand_digit*/
  49464. /*
  49465. * Testing get_digit_count
  49466. */
  49467. static int test_get_digit_count(void)
  49468. {
  49469. EXPECT_DECLS;
  49470. #if !defined(WOLFSSL_SP_MATH) && defined(WOLFSSL_PUBLIC_MP)
  49471. mp_int a;
  49472. XMEMSET(&a, 0, sizeof(mp_int));
  49473. ExpectIntEQ(mp_init(&a), 0);
  49474. ExpectIntEQ(get_digit_count(NULL), 0);
  49475. ExpectIntEQ(get_digit_count(&a), 0);
  49476. mp_clear(&a);
  49477. #endif
  49478. return EXPECT_RESULT();
  49479. } /* End test_get_digit_count*/
  49480. /*
  49481. * Testing mp_cond_copy
  49482. */
  49483. static int test_mp_cond_copy(void)
  49484. {
  49485. EXPECT_DECLS;
  49486. #if (defined(HAVE_ECC) || defined(WOLFSSL_MP_COND_COPY)) && \
  49487. defined(WOLFSSL_PUBLIC_MP)
  49488. mp_int a;
  49489. mp_int b;
  49490. int copy = 0;
  49491. XMEMSET(&a, 0, sizeof(mp_int));
  49492. XMEMSET(&b, 0, sizeof(mp_int));
  49493. ExpectIntEQ(mp_init(&a), MP_OKAY);
  49494. ExpectIntEQ(mp_init(&b), MP_OKAY);
  49495. ExpectIntEQ(mp_cond_copy(NULL, copy, NULL), BAD_FUNC_ARG);
  49496. ExpectIntEQ(mp_cond_copy(NULL, copy, &b), BAD_FUNC_ARG);
  49497. ExpectIntEQ(mp_cond_copy(&a, copy, NULL), BAD_FUNC_ARG);
  49498. ExpectIntEQ(mp_cond_copy(&a, copy, &b), 0);
  49499. mp_clear(&a);
  49500. mp_clear(&b);
  49501. #endif
  49502. return EXPECT_RESULT();
  49503. } /* End test_mp_cond_copy*/
  49504. /*
  49505. * Testing mp_rand
  49506. */
  49507. static int test_mp_rand(void)
  49508. {
  49509. EXPECT_DECLS;
  49510. #if defined(WC_RSA_BLINDING) && defined(WOLFSSL_PUBLIC_MP)
  49511. mp_int a;
  49512. WC_RNG rng;
  49513. int digits = 1;
  49514. XMEMSET(&a, 0, sizeof(mp_int));
  49515. XMEMSET(&rng, 0, sizeof(WC_RNG));
  49516. ExpectIntEQ(mp_init(&a), MP_OKAY);
  49517. ExpectIntEQ(wc_InitRng(&rng), 0);
  49518. ExpectIntEQ(mp_rand(&a, digits, NULL), MISSING_RNG_E);
  49519. ExpectIntEQ(mp_rand(NULL, digits, &rng), BAD_FUNC_ARG);
  49520. ExpectIntEQ(mp_rand(&a, 0, &rng), BAD_FUNC_ARG);
  49521. ExpectIntEQ(mp_rand(&a, digits, &rng), 0);
  49522. mp_clear(&a);
  49523. DoExpectIntEQ(wc_FreeRng(&rng), 0);
  49524. #endif
  49525. return EXPECT_RESULT();
  49526. } /* End test_mp_rand*/
  49527. /*
  49528. * Testing get_digit
  49529. */
  49530. static int test_get_digit(void)
  49531. {
  49532. EXPECT_DECLS;
  49533. #if defined(WOLFSSL_PUBLIC_MP)
  49534. mp_int a;
  49535. int n = 0;
  49536. XMEMSET(&a, 0, sizeof(mp_int));
  49537. ExpectIntEQ(mp_init(&a), MP_OKAY);
  49538. ExpectIntEQ(get_digit(NULL, n), 0);
  49539. ExpectIntEQ(get_digit(&a, n), 0);
  49540. mp_clear(&a);
  49541. #endif
  49542. return EXPECT_RESULT();
  49543. } /* End test_get_digit*/
  49544. /*
  49545. * Testing wc_export_int
  49546. */
  49547. static int test_wc_export_int(void)
  49548. {
  49549. EXPECT_DECLS;
  49550. #if (defined(HAVE_ECC) || defined(WOLFSSL_EXPORT_INT)) && \
  49551. defined(WOLFSSL_PUBLIC_MP)
  49552. mp_int mp;
  49553. byte buf[32];
  49554. word32 keySz = (word32)sizeof(buf);
  49555. word32 len = (word32)sizeof(buf);
  49556. XMEMSET(&mp, 0, sizeof(mp_int));
  49557. ExpectIntEQ(mp_init(&mp), MP_OKAY);
  49558. ExpectIntEQ(mp_set(&mp, 1234), 0);
  49559. ExpectIntEQ(wc_export_int(NULL, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  49560. BAD_FUNC_ARG);
  49561. len = sizeof(buf)-1;
  49562. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN),
  49563. BUFFER_E);
  49564. len = sizeof(buf);
  49565. ExpectIntEQ(wc_export_int(&mp, buf, &len, keySz, WC_TYPE_UNSIGNED_BIN), 0);
  49566. len = 4; /* test input too small */
  49567. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), BUFFER_E);
  49568. len = sizeof(buf);
  49569. ExpectIntEQ(wc_export_int(&mp, buf, &len, 0, WC_TYPE_HEX_STR), 0);
  49570. /* hex version of 1234 is 04D2 and should be 4 digits + 1 null */
  49571. ExpectIntEQ(len, 5);
  49572. mp_clear(&mp);
  49573. #endif
  49574. return EXPECT_RESULT();
  49575. } /* End test_wc_export_int*/
  49576. static int test_wc_InitRngNonce(void)
  49577. {
  49578. EXPECT_DECLS;
  49579. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  49580. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  49581. HAVE_FIPS_VERSION >= 2))
  49582. WC_RNG rng;
  49583. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  49584. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  49585. word32 nonceSz = sizeof(nonce);
  49586. ExpectIntEQ(wc_InitRngNonce(&rng, nonce, nonceSz), 0);
  49587. ExpectIntEQ(wc_FreeRng(&rng), 0);
  49588. #endif
  49589. return EXPECT_RESULT();
  49590. } /* End test_wc_InitRngNonce*/
  49591. /*
  49592. * Testing wc_InitRngNonce_ex
  49593. */
  49594. static int test_wc_InitRngNonce_ex(void)
  49595. {
  49596. EXPECT_DECLS;
  49597. #if !defined(WC_NO_RNG) && !defined(HAVE_SELFTEST) && \
  49598. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && \
  49599. HAVE_FIPS_VERSION >= 2))
  49600. WC_RNG rng;
  49601. byte nonce[] = "\x0D\x74\xDB\x42\xA9\x10\x77\xDE"
  49602. "\x45\xAC\x13\x7A\xE1\x48\xAF\x16";
  49603. word32 nonceSz = sizeof(nonce);
  49604. ExpectIntEQ(wc_InitRngNonce_ex(&rng, nonce, nonceSz, HEAP_HINT, testDevId),
  49605. 0);
  49606. ExpectIntEQ(wc_FreeRng(&rng), 0);
  49607. #endif
  49608. return EXPECT_RESULT();
  49609. } /* End test_wc_InitRngNonce_ex */
  49610. static int test_wolfSSL_X509_CRL(void)
  49611. {
  49612. EXPECT_DECLS;
  49613. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL)
  49614. X509_CRL *crl = NULL;
  49615. char pem[][100] = {
  49616. "./certs/crl/crl.pem",
  49617. "./certs/crl/crl2.pem",
  49618. "./certs/crl/caEccCrl.pem",
  49619. "./certs/crl/eccCliCRL.pem",
  49620. "./certs/crl/eccSrvCRL.pem",
  49621. ""
  49622. };
  49623. #ifndef NO_BIO
  49624. BIO *bio = NULL;
  49625. #endif
  49626. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  49627. char der[][100] = {
  49628. "./certs/crl/crl.der",
  49629. "./certs/crl/crl2.der",
  49630. ""};
  49631. #endif
  49632. XFILE fp = XBADFILE;
  49633. int i;
  49634. for (i = 0; pem[i][0] != '\0'; i++)
  49635. {
  49636. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  49637. ExpectNotNull(crl = (X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  49638. NULL, NULL));
  49639. ExpectNotNull(crl);
  49640. X509_CRL_free(crl);
  49641. if (fp != XBADFILE) {
  49642. XFCLOSE(fp);
  49643. fp = XBADFILE;
  49644. }
  49645. ExpectTrue((fp = XFOPEN(pem[i], "rb")) != XBADFILE);
  49646. ExpectNotNull((X509_CRL *)PEM_read_X509_CRL(fp, (X509_CRL **)&crl, NULL,
  49647. NULL));
  49648. if (EXPECT_FAIL()) {
  49649. crl = NULL;
  49650. }
  49651. ExpectNotNull(crl);
  49652. X509_CRL_free(crl);
  49653. crl = NULL;
  49654. if (fp != XBADFILE) {
  49655. XFCLOSE(fp);
  49656. fp = XBADFILE;
  49657. }
  49658. }
  49659. #ifndef NO_BIO
  49660. for (i = 0; pem[i][0] != '\0'; i++)
  49661. {
  49662. ExpectNotNull(bio = BIO_new_file(pem[i], "rb"));
  49663. ExpectNotNull(crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL));
  49664. X509_CRL_free(crl);
  49665. crl = NULL;
  49666. BIO_free(bio);
  49667. bio = NULL;
  49668. }
  49669. #endif
  49670. #ifdef HAVE_TEST_d2i_X509_CRL_fp
  49671. for (i = 0; der[i][0] != '\0'; i++) {
  49672. ExpectTrue((fp = XFOPEN(der[i], "rb")) != XBADFILE);
  49673. ExpectTrue((fp != XBADFILE));
  49674. ExpectNotNull(crl = (X509_CRL *)d2i_X509_CRL_fp((fp, X509_CRL **)NULL));
  49675. ExpectNotNull(crl);
  49676. X509_CRL_free(crl);
  49677. if (fp != XBADFILE) {
  49678. XFCLOSE(fp);
  49679. fp = XBADFILE;
  49680. }
  49681. fp = XFOPEN(der[i], "rb");
  49682. ExpectTrue((fp != XBADFILE));
  49683. ExpectNotNull((X509_CRL *)d2i_X509_CRL_fp(fp, (X509_CRL **)&crl));
  49684. if (EXPECT_FAIL()) {
  49685. crl = NULL;
  49686. }
  49687. ExpectNotNull(crl);
  49688. X509_CRL_free(crl);
  49689. crl = NULL;
  49690. if (fp != XBADFILE) {
  49691. XFCLOSE(fp);
  49692. fp = XBADFILE;
  49693. }
  49694. }
  49695. #endif
  49696. #endif
  49697. return EXPECT_RESULT();
  49698. }
  49699. static int test_wolfSSL_X509_load_crl_file(void)
  49700. {
  49701. EXPECT_DECLS;
  49702. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  49703. !defined(NO_STDIO_FILESYSTEM) && !defined(NO_RSA) && !defined(NO_BIO)
  49704. int i;
  49705. char pem[][100] = {
  49706. "./certs/crl/crl.pem",
  49707. "./certs/crl/crl2.pem",
  49708. "./certs/crl/caEccCrl.pem",
  49709. "./certs/crl/eccCliCRL.pem",
  49710. "./certs/crl/eccSrvCRL.pem",
  49711. #ifdef WC_RSA_PSS
  49712. "./certs/crl/crl_rsapss.pem",
  49713. #endif
  49714. ""
  49715. };
  49716. char der[][100] = {
  49717. "./certs/crl/crl.der",
  49718. "./certs/crl/crl2.der",
  49719. ""
  49720. };
  49721. WOLFSSL_X509_STORE* store = NULL;
  49722. WOLFSSL_X509_LOOKUP* lookup = NULL;
  49723. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  49724. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  49725. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  49726. X509_FILETYPE_PEM), 1);
  49727. #ifdef WC_RSA_PSS
  49728. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/rsapss/ca-rsapss.pem",
  49729. X509_FILETYPE_PEM), 1);
  49730. #endif
  49731. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  49732. X509_FILETYPE_PEM), 1);
  49733. if (store) {
  49734. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  49735. WOLFSSL_FILETYPE_PEM), 1);
  49736. /* since store hasn't yet known the revoked cert*/
  49737. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  49738. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  49739. }
  49740. for (i = 0; pem[i][0] != '\0'; i++) {
  49741. ExpectIntEQ(X509_load_crl_file(lookup, pem[i], WOLFSSL_FILETYPE_PEM),
  49742. 1);
  49743. }
  49744. if (store) {
  49745. /* since store knows crl list */
  49746. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  49747. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  49748. CRL_CERT_REVOKED);
  49749. #ifdef WC_RSA_PSS
  49750. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  49751. "certs/rsapss/server-rsapss-cert.pem", WOLFSSL_FILETYPE_PEM),
  49752. CRL_CERT_REVOKED);
  49753. #endif
  49754. }
  49755. /* once feeing store */
  49756. X509_STORE_free(store);
  49757. store = NULL;
  49758. ExpectNotNull(store = wolfSSL_X509_STORE_new());
  49759. ExpectNotNull(lookup = X509_STORE_add_lookup(store, X509_LOOKUP_file()));
  49760. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/ca-cert.pem",
  49761. X509_FILETYPE_PEM), 1);
  49762. ExpectIntEQ(X509_LOOKUP_load_file(lookup, "certs/server-revoked-cert.pem",
  49763. X509_FILETYPE_PEM), 1);
  49764. if (store) {
  49765. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm, svrCertFile,
  49766. WOLFSSL_FILETYPE_PEM), 1);
  49767. /* since store hasn't yet known the revoked cert*/
  49768. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  49769. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM), 1);
  49770. }
  49771. for (i = 0; der[i][0] != '\0'; i++) {
  49772. ExpectIntEQ(X509_load_crl_file(lookup, der[i], WOLFSSL_FILETYPE_ASN1),
  49773. 1);
  49774. }
  49775. if (store) {
  49776. /* since store knows crl list */
  49777. ExpectIntEQ(wolfSSL_CertManagerVerify(store->cm,
  49778. "certs/server-revoked-cert.pem", WOLFSSL_FILETYPE_PEM),
  49779. CRL_CERT_REVOKED);
  49780. }
  49781. /* test for incorrect parameter */
  49782. ExpectIntEQ(X509_load_crl_file(NULL, pem[0], 0), 0);
  49783. ExpectIntEQ(X509_load_crl_file(lookup, NULL, 0), 0);
  49784. ExpectIntEQ(X509_load_crl_file(NULL, NULL, 0), 0);
  49785. X509_STORE_free(store);
  49786. store = NULL;
  49787. #endif
  49788. return EXPECT_RESULT();
  49789. }
  49790. static int test_wolfSSL_i2d_X509(void)
  49791. {
  49792. EXPECT_DECLS;
  49793. #if defined(OPENSSL_EXTRA) && defined(USE_CERT_BUFFERS_2048) && !defined(NO_RSA)
  49794. const unsigned char* cert_buf = server_cert_der_2048;
  49795. unsigned char* out = NULL;
  49796. unsigned char* tmp = NULL;
  49797. X509* cert = NULL;
  49798. ExpectNotNull(d2i_X509(&cert, &cert_buf, sizeof_server_cert_der_2048));
  49799. /* Pointer should be advanced */
  49800. ExpectPtrGT(cert_buf, server_cert_der_2048);
  49801. ExpectIntGT(i2d_X509(cert, &out), 0);
  49802. ExpectNotNull(out);
  49803. tmp = out;
  49804. ExpectIntGT(i2d_X509(cert, &tmp), 0);
  49805. ExpectPtrGT(tmp, out);
  49806. if (out != NULL)
  49807. XFREE(out, NULL, DYNAMIC_TYPE_OPENSSL);
  49808. X509_free(cert);
  49809. #endif
  49810. return EXPECT_RESULT();
  49811. }
  49812. static int test_wolfSSL_d2i_X509_REQ(void)
  49813. {
  49814. EXPECT_DECLS;
  49815. #if defined(WOLFSSL_CERT_REQ) && !defined(NO_RSA) && !defined(NO_BIO) && \
  49816. (defined(OPENSSL_ALL) || defined(OPENSSL_EXTRA)) && \
  49817. !defined(WOLFSSL_SP_MATH)
  49818. /* ./certs/csr.signed.der, ./certs/csr.ext.der, and ./certs/csr.attr.der
  49819. * were generated by libest
  49820. * ./certs/csr.attr.der contains sample attributes
  49821. * ./certs/csr.ext.der contains sample extensions */
  49822. const char* csrFile = "./certs/csr.signed.der";
  49823. const char* csrPopFile = "./certs/csr.attr.der";
  49824. const char* csrExtFile = "./certs/csr.ext.der";
  49825. /* ./certs/csr.dsa.pem is generated using
  49826. * openssl req -newkey dsa:certs/dsaparams.pem \
  49827. * -keyout certs/csr.dsa.key.pem -keyform PEM -out certs/csr.dsa.pem \
  49828. * -outform PEM
  49829. * with the passphrase "wolfSSL"
  49830. */
  49831. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  49832. const char* csrDsaFile = "./certs/csr.dsa.pem";
  49833. XFILE f = XBADFILE;
  49834. #endif
  49835. BIO* bio = NULL;
  49836. X509* req = NULL;
  49837. EVP_PKEY *pub_key = NULL;
  49838. {
  49839. ExpectNotNull(bio = BIO_new_file(csrFile, "rb"));
  49840. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  49841. /*
  49842. * Extract the public key from the CSR
  49843. */
  49844. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  49845. /*
  49846. * Verify the signature in the CSR
  49847. */
  49848. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  49849. X509_free(req);
  49850. req = NULL;
  49851. BIO_free(bio);
  49852. bio = NULL;
  49853. EVP_PKEY_free(pub_key);
  49854. pub_key = NULL;
  49855. }
  49856. {
  49857. #ifdef OPENSSL_ALL
  49858. X509_ATTRIBUTE* attr = NULL;
  49859. ASN1_TYPE *at = NULL;
  49860. #endif
  49861. ExpectNotNull(bio = BIO_new_file(csrPopFile, "rb"));
  49862. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  49863. /*
  49864. * Extract the public key from the CSR
  49865. */
  49866. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  49867. /*
  49868. * Verify the signature in the CSR
  49869. */
  49870. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  49871. #ifdef OPENSSL_ALL
  49872. /*
  49873. * Obtain the challenge password from the CSR
  49874. */
  49875. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  49876. -1), 1);
  49877. ExpectNotNull(attr = X509_REQ_get_attr(req, 1));
  49878. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  49879. ExpectNotNull(at->value.asn1_string);
  49880. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string),
  49881. "2xIE+qqp/rhyTXP+");
  49882. ExpectIntEQ(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), -1);
  49883. #endif
  49884. X509_free(req);
  49885. req = NULL;
  49886. BIO_free(bio);
  49887. bio = NULL;
  49888. EVP_PKEY_free(pub_key);
  49889. pub_key = NULL;
  49890. }
  49891. {
  49892. #ifdef OPENSSL_ALL
  49893. X509_ATTRIBUTE* attr = NULL;
  49894. ASN1_TYPE *at = NULL;
  49895. STACK_OF(X509_EXTENSION) *exts = NULL;
  49896. #endif
  49897. ExpectNotNull(bio = BIO_new_file(csrExtFile, "rb"));
  49898. /* This CSR contains an Extension Request attribute so
  49899. * we test extension parsing in a CSR attribute here. */
  49900. ExpectNotNull(d2i_X509_REQ_bio(bio, &req));
  49901. /*
  49902. * Extract the public key from the CSR
  49903. */
  49904. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  49905. /*
  49906. * Verify the signature in the CSR
  49907. */
  49908. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  49909. #ifdef OPENSSL_ALL
  49910. ExpectNotNull(exts = (STACK_OF(X509_EXTENSION)*)X509_REQ_get_extensions(
  49911. req));
  49912. ExpectIntEQ(sk_X509_EXTENSION_num(exts), 2);
  49913. sk_X509_EXTENSION_pop_free(exts, X509_EXTENSION_free);
  49914. /*
  49915. * Obtain the challenge password from the CSR
  49916. */
  49917. ExpectIntEQ(X509_REQ_get_attr_by_NID(req, NID_pkcs9_challengePassword,
  49918. -1), 0);
  49919. ExpectNotNull(attr = X509_REQ_get_attr(req, 0));
  49920. ExpectNotNull(at = X509_ATTRIBUTE_get0_type(attr, 0));
  49921. ExpectNotNull(at->value.asn1_string);
  49922. ExpectStrEQ((char*)ASN1_STRING_data(at->value.asn1_string), "IGCu/xNL4/0/wOgo");
  49923. ExpectIntGE(X509_get_ext_by_NID(req, NID_key_usage, -1), 0);
  49924. ExpectIntGE(X509_get_ext_by_NID(req, NID_subject_alt_name, -1), 0);
  49925. #endif
  49926. X509_free(req);
  49927. req = NULL;
  49928. BIO_free(bio);
  49929. bio = NULL;
  49930. EVP_PKEY_free(pub_key);
  49931. pub_key = NULL;
  49932. }
  49933. #if !defined(NO_DSA) && !defined(HAVE_SELFTEST)
  49934. {
  49935. ExpectNotNull(bio = BIO_new_file(csrDsaFile, "rb"));
  49936. ExpectNotNull(PEM_read_bio_X509_REQ(bio, &req, NULL, NULL));
  49937. /*
  49938. * Extract the public key from the CSR
  49939. */
  49940. ExpectNotNull(pub_key = X509_REQ_get_pubkey(req));
  49941. /*
  49942. * Verify the signature in the CSR
  49943. */
  49944. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  49945. X509_free(req);
  49946. req = NULL;
  49947. BIO_free(bio);
  49948. /* Run the same test, but with a file pointer instead of a BIO.
  49949. * (PEM_read_X509_REQ)*/
  49950. ExpectTrue((f = XFOPEN(csrDsaFile, "rb")) != XBADFILE);
  49951. ExpectNotNull(PEM_read_X509_REQ(f, &req, NULL, NULL));
  49952. ExpectIntEQ(X509_REQ_verify(req, pub_key), 1);
  49953. X509_free(req);
  49954. EVP_PKEY_free(pub_key);
  49955. }
  49956. #endif /* !NO_DSA && !HAVE_SELFTEST */
  49957. #endif /* WOLFSSL_CERT_REQ && (OPENSSL_ALL || OPENSSL_EXTRA) */
  49958. return EXPECT_RESULT();
  49959. }
  49960. static int test_wolfSSL_PEM_read_X509(void)
  49961. {
  49962. EXPECT_DECLS;
  49963. #if defined(OPENSSL_EXTRA) && defined(HAVE_CRL) && !defined(NO_FILESYSTEM) && \
  49964. !defined(NO_RSA)
  49965. X509 *x509 = NULL;
  49966. XFILE fp = XBADFILE;
  49967. ExpectTrue((fp = XFOPEN(svrCertFile, "rb")) != XBADFILE);
  49968. ExpectNotNull(x509 = (X509 *)PEM_read_X509(fp, (X509 **)NULL, NULL, NULL));
  49969. X509_free(x509);
  49970. if (fp != XBADFILE)
  49971. XFCLOSE(fp);
  49972. #endif
  49973. return EXPECT_RESULT();
  49974. }
  49975. static int test_wolfSSL_PEM_read(void)
  49976. {
  49977. EXPECT_DECLS;
  49978. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_BIO)
  49979. const char* filename = "./certs/server-keyEnc.pem";
  49980. XFILE fp = XBADFILE;
  49981. char* name = NULL;
  49982. char* header = NULL;
  49983. byte* data = NULL;
  49984. long len;
  49985. EVP_CIPHER_INFO cipher;
  49986. WOLFSSL_BIO* bio = NULL;
  49987. byte* fileData = NULL;
  49988. size_t fileDataSz = 0;
  49989. byte* out;
  49990. ExpectNotNull(bio = BIO_new_file(filename, "rb"));
  49991. ExpectIntEQ(PEM_read_bio(bio, NULL, &header, &data, &len), 0);
  49992. ExpectIntEQ(PEM_read_bio(bio, &name, NULL, &data, &len), 0);
  49993. ExpectIntEQ(PEM_read_bio(bio, &name, &header, NULL, &len), 0);
  49994. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, NULL), 0);
  49995. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, &len), 1);
  49996. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  49997. ExpectIntGT(XSTRLEN(header), 0);
  49998. ExpectIntGT(len, 0);
  49999. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50000. name = NULL;
  50001. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50002. header = NULL;
  50003. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50004. data = NULL;
  50005. BIO_free(bio);
  50006. bio = NULL;
  50007. ExpectTrue((fp = XFOPEN(filename, "rb")) != XBADFILE);
  50008. /* Fail cases. */
  50009. ExpectIntEQ(PEM_read(fp, NULL, &header, &data, &len), WOLFSSL_FAILURE);
  50010. ExpectIntEQ(PEM_read(fp, &name, NULL, &data, &len), WOLFSSL_FAILURE);
  50011. ExpectIntEQ(PEM_read(fp, &name, &header, NULL, &len), WOLFSSL_FAILURE);
  50012. ExpectIntEQ(PEM_read(fp, &name, &header, &data, NULL), WOLFSSL_FAILURE);
  50013. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  50014. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  50015. ExpectIntGT(XSTRLEN(header), 0);
  50016. ExpectIntGT(len, 0);
  50017. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  50018. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  50019. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  50020. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  50021. DYNAMIC_TYPE_TMP_BUFFER));
  50022. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  50023. if (fp != XBADFILE) {
  50024. XFCLOSE(fp);
  50025. fp = XBADFILE;
  50026. }
  50027. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  50028. /* Fail cases. */
  50029. ExpectIntEQ(PEM_write_bio(NULL, name, header, data, len), 0);
  50030. ExpectIntEQ(PEM_write_bio(bio, NULL, header, data, len), 0);
  50031. ExpectIntEQ(PEM_write_bio(bio, name, NULL, data, len), 0);
  50032. ExpectIntEQ(PEM_write_bio(bio, name, header, NULL, len), 0);
  50033. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  50034. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  50035. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  50036. /* Fail cases. */
  50037. ExpectIntEQ(PEM_write(XBADFILE, name, header, data, len), 0);
  50038. ExpectIntEQ(PEM_write(stderr, NULL, header, data, len), 0);
  50039. ExpectIntEQ(PEM_write(stderr, name, NULL, data, len), 0);
  50040. ExpectIntEQ(PEM_write(stderr, name, header, NULL, len), 0);
  50041. /* Pass case */
  50042. ExpectIntEQ(PEM_write(stderr, name, header, data, len), fileDataSz);
  50043. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50044. name = NULL;
  50045. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50046. header = NULL;
  50047. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50048. data = NULL;
  50049. /* Read out of a fixed buffer BIO - forces malloc in PEM_read_bio. */
  50050. ExpectIntEQ(PEM_read_bio(bio, &name, &header, &data, &len), 1);
  50051. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  50052. ExpectIntGT(XSTRLEN(header), 0);
  50053. ExpectIntGT(len, 0);
  50054. /* Fail cases. */
  50055. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(NULL, &cipher), WOLFSSL_FAILURE);
  50056. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, NULL), WOLFSSL_FAILURE);
  50057. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO((char*)"", &cipher), WOLFSSL_FAILURE);
  50058. #ifndef NO_DES3
  50059. ExpectIntEQ(PEM_get_EVP_CIPHER_INFO(header, &cipher), WOLFSSL_SUCCESS);
  50060. #endif
  50061. /* Fail cases. */
  50062. ExpectIntEQ(PEM_do_header(NULL, data, &len, PasswordCallBack,
  50063. (void*)"yassl123"), WOLFSSL_FAILURE);
  50064. ExpectIntEQ(PEM_do_header(&cipher, NULL, &len, PasswordCallBack,
  50065. (void*)"yassl123"), WOLFSSL_FAILURE);
  50066. ExpectIntEQ(PEM_do_header(&cipher, data, NULL, PasswordCallBack,
  50067. (void*)"yassl123"), WOLFSSL_FAILURE);
  50068. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NULL,
  50069. (void*)"yassl123"), WOLFSSL_FAILURE);
  50070. ExpectIntEQ(PEM_do_header(&cipher, data, &len, NoPasswordCallBack,
  50071. (void*)"yassl123"), WOLFSSL_FAILURE);
  50072. #if !defined(NO_DES3) && !defined(NO_MD5)
  50073. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  50074. (void*)"yassl123"), WOLFSSL_SUCCESS);
  50075. #else
  50076. ExpectIntEQ(PEM_do_header(&cipher, data, &len, PasswordCallBack,
  50077. (void*)"yassl123"), WOLFSSL_FAILURE);
  50078. #endif
  50079. BIO_free(bio);
  50080. bio = NULL;
  50081. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50082. fileData = NULL;
  50083. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50084. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50085. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50086. name = NULL;
  50087. header = NULL;
  50088. data = NULL;
  50089. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  50090. ExpectIntEQ(PEM_read(fp, &name, &header, &data, &len), WOLFSSL_SUCCESS);
  50091. ExpectIntEQ(XSTRNCMP(name, "RSA PRIVATE KEY", 15), 0);
  50092. ExpectIntEQ(XSTRLEN(header), 0);
  50093. ExpectIntGT(len, 0);
  50094. ExpectIntEQ(XFSEEK(fp, 0, SEEK_END), 0);
  50095. ExpectIntGT((fileDataSz = XFTELL(fp)), 0);
  50096. ExpectIntEQ(XFSEEK(fp, 0, SEEK_SET), 0);
  50097. ExpectNotNull(fileData = (unsigned char*)XMALLOC(fileDataSz, NULL,
  50098. DYNAMIC_TYPE_TMP_BUFFER));
  50099. ExpectIntEQ(XFREAD(fileData, 1, fileDataSz, fp), fileDataSz);
  50100. if (fp != XBADFILE)
  50101. XFCLOSE(fp);
  50102. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  50103. ExpectIntEQ(PEM_write_bio(bio, name, header, data, len), fileDataSz);
  50104. ExpectIntEQ(wolfSSL_BIO_get_mem_data(bio, &out), fileDataSz);
  50105. ExpectIntEQ(XMEMCMP(out, fileData, fileDataSz), 0);
  50106. BIO_free(bio);
  50107. XFREE(fileData, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50108. XFREE(name, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50109. XFREE(header, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50110. XFREE(data, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  50111. #endif
  50112. return EXPECT_RESULT();
  50113. }
  50114. static int test_wolfssl_EVP_aes_gcm_AAD_2_parts(void)
  50115. {
  50116. EXPECT_DECLS;
  50117. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  50118. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50119. const byte iv[12] = { 0 };
  50120. const byte key[16] = { 0 };
  50121. const byte cleartext[16] = { 0 };
  50122. const byte aad[] = {
  50123. 0x01, 0x10, 0x00, 0x2a, 0x08, 0x00, 0x04, 0x00,
  50124. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
  50125. 0x00, 0x00, 0xdc, 0x4d, 0xad, 0x6b, 0x06, 0x93,
  50126. 0x4f
  50127. };
  50128. byte out1Part[16];
  50129. byte outTag1Part[16];
  50130. byte out2Part[16];
  50131. byte outTag2Part[16];
  50132. byte decryptBuf[16];
  50133. int len = 0;
  50134. int tlen;
  50135. EVP_CIPHER_CTX* ctx = NULL;
  50136. /* ENCRYPT */
  50137. /* Send AAD and data in 1 part */
  50138. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  50139. tlen = 0;
  50140. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  50141. 1);
  50142. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  50143. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  50144. ExpectIntEQ(EVP_EncryptUpdate(ctx, out1Part, &len, cleartext,
  50145. sizeof(cleartext)), 1);
  50146. tlen += len;
  50147. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out1Part, &len), 1);
  50148. tlen += len;
  50149. ExpectIntEQ(tlen, sizeof(cleartext));
  50150. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  50151. outTag1Part), 1);
  50152. EVP_CIPHER_CTX_free(ctx);
  50153. ctx = NULL;
  50154. /* DECRYPT */
  50155. /* Send AAD and data in 1 part */
  50156. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  50157. tlen = 0;
  50158. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  50159. 1);
  50160. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  50161. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, sizeof(aad)), 1);
  50162. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part,
  50163. sizeof(cleartext)), 1);
  50164. tlen += len;
  50165. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  50166. outTag1Part), 1);
  50167. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf, &len), 1);
  50168. tlen += len;
  50169. ExpectIntEQ(tlen, sizeof(cleartext));
  50170. EVP_CIPHER_CTX_free(ctx);
  50171. ctx = NULL;
  50172. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  50173. /* ENCRYPT */
  50174. /* Send AAD and data in 2 parts */
  50175. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  50176. tlen = 0;
  50177. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  50178. 1);
  50179. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  50180. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad, 1), 1);
  50181. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  50182. 1);
  50183. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part, &len, cleartext, 1), 1);
  50184. tlen += len;
  50185. ExpectIntEQ(EVP_EncryptUpdate(ctx, out2Part + tlen, &len, cleartext + 1,
  50186. sizeof(cleartext) - 1), 1);
  50187. tlen += len;
  50188. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, out2Part + tlen, &len), 1);
  50189. tlen += len;
  50190. ExpectIntEQ(tlen, sizeof(cleartext));
  50191. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, 16,
  50192. outTag2Part), 1);
  50193. ExpectIntEQ(XMEMCMP(out1Part, out2Part, sizeof(out1Part)), 0);
  50194. ExpectIntEQ(XMEMCMP(outTag1Part, outTag2Part, sizeof(outTag1Part)), 0);
  50195. EVP_CIPHER_CTX_free(ctx);
  50196. ctx = NULL;
  50197. /* DECRYPT */
  50198. /* Send AAD and data in 2 parts */
  50199. ExpectNotNull(ctx = EVP_CIPHER_CTX_new());
  50200. tlen = 0;
  50201. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_aes_128_gcm(), NULL, NULL, NULL),
  50202. 1);
  50203. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), 1);
  50204. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad, 1), 1);
  50205. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &len, aad + 1, sizeof(aad) - 1),
  50206. 1);
  50207. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf, &len, out1Part, 1), 1);
  50208. tlen += len;
  50209. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptBuf + tlen, &len, out1Part + 1,
  50210. sizeof(cleartext) - 1), 1);
  50211. tlen += len;
  50212. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16,
  50213. outTag1Part), 1);
  50214. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptBuf + tlen, &len), 1);
  50215. tlen += len;
  50216. ExpectIntEQ(tlen, sizeof(cleartext));
  50217. ExpectIntEQ(XMEMCMP(decryptBuf, cleartext, len), 0);
  50218. /* Test AAD reuse */
  50219. EVP_CIPHER_CTX_free(ctx);
  50220. #endif
  50221. return EXPECT_RESULT();
  50222. }
  50223. static int test_wolfssl_EVP_aes_gcm_zeroLen(void)
  50224. {
  50225. EXPECT_DECLS;
  50226. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  50227. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50228. /* Zero length plain text */
  50229. byte key[] = {
  50230. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50231. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50232. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50233. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50234. }; /* align */
  50235. byte iv[] = {
  50236. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50237. }; /* align */
  50238. byte plaintxt[1];
  50239. int ivSz = 12;
  50240. int plaintxtSz = 0;
  50241. unsigned char tag[16];
  50242. unsigned char tag_kat[] = {
  50243. 0x53,0x0f,0x8a,0xfb,0xc7,0x45,0x36,0xb9,
  50244. 0xa9,0x63,0xb4,0xf1,0xc4,0xcb,0x73,0x8b
  50245. };
  50246. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  50247. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  50248. int ciphertxtSz = 0;
  50249. int decryptedtxtSz = 0;
  50250. int len = 0;
  50251. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  50252. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  50253. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_gcm(), NULL, key, iv));
  50254. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50255. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  50256. plaintxtSz));
  50257. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  50258. ciphertxtSz += len;
  50259. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  50260. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  50261. ExpectIntEQ(0, ciphertxtSz);
  50262. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  50263. EVP_CIPHER_CTX_init(de);
  50264. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_gcm(), NULL, key, iv));
  50265. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50266. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  50267. decryptedtxtSz = len;
  50268. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  50269. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  50270. decryptedtxtSz += len;
  50271. ExpectIntEQ(0, decryptedtxtSz);
  50272. EVP_CIPHER_CTX_free(en);
  50273. EVP_CIPHER_CTX_free(de);
  50274. #endif
  50275. return EXPECT_RESULT();
  50276. }
  50277. static int test_wolfssl_EVP_aes_gcm(void)
  50278. {
  50279. EXPECT_DECLS;
  50280. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESGCM) && \
  50281. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50282. /* A 256 bit key, AES_128 will use the first 128 bit*/
  50283. byte *key = (byte*)"01234567890123456789012345678901";
  50284. /* A 128 bit IV */
  50285. byte *iv = (byte*)"0123456789012345";
  50286. int ivSz = AES_BLOCK_SIZE;
  50287. /* Message to be encrypted */
  50288. byte *plaintxt = (byte*)"for things to change you have to change";
  50289. /* Additional non-confidential data */
  50290. byte *aad = (byte*)"Don't spend major time on minor things.";
  50291. unsigned char tag[AES_BLOCK_SIZE] = {0};
  50292. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  50293. int aadSz = (int)XSTRLEN((char*)aad);
  50294. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  50295. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  50296. int ciphertxtSz = 0;
  50297. int decryptedtxtSz = 0;
  50298. int len = 0;
  50299. int i = 0;
  50300. EVP_CIPHER_CTX en[2];
  50301. EVP_CIPHER_CTX de[2];
  50302. for (i = 0; i < 2; i++) {
  50303. EVP_CIPHER_CTX_init(&en[i]);
  50304. if (i == 0) {
  50305. /* Default uses 96-bits IV length */
  50306. #ifdef WOLFSSL_AES_128
  50307. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  50308. key, iv));
  50309. #elif defined(WOLFSSL_AES_192)
  50310. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  50311. key, iv));
  50312. #elif defined(WOLFSSL_AES_256)
  50313. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  50314. key, iv));
  50315. #endif
  50316. }
  50317. else {
  50318. #ifdef WOLFSSL_AES_128
  50319. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_gcm(), NULL,
  50320. NULL, NULL));
  50321. #elif defined(WOLFSSL_AES_192)
  50322. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_gcm(), NULL,
  50323. NULL, NULL));
  50324. #elif defined(WOLFSSL_AES_256)
  50325. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_gcm(), NULL,
  50326. NULL, NULL));
  50327. #endif
  50328. /* non-default must to set the IV length first */
  50329. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  50330. ivSz, NULL));
  50331. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50332. }
  50333. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  50334. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  50335. plaintxtSz));
  50336. ciphertxtSz = len;
  50337. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50338. ciphertxtSz += len;
  50339. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  50340. AES_BLOCK_SIZE, tag));
  50341. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  50342. EVP_CIPHER_CTX_init(&de[i]);
  50343. if (i == 0) {
  50344. /* Default uses 96-bits IV length */
  50345. #ifdef WOLFSSL_AES_128
  50346. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50347. key, iv));
  50348. #elif defined(WOLFSSL_AES_192)
  50349. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50350. key, iv));
  50351. #elif defined(WOLFSSL_AES_256)
  50352. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50353. key, iv));
  50354. #endif
  50355. }
  50356. else {
  50357. #ifdef WOLFSSL_AES_128
  50358. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50359. NULL, NULL));
  50360. #elif defined(WOLFSSL_AES_192)
  50361. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50362. NULL, NULL));
  50363. #elif defined(WOLFSSL_AES_256)
  50364. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50365. NULL, NULL));
  50366. #endif
  50367. /* non-default must to set the IV length first */
  50368. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  50369. ivSz, NULL));
  50370. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50371. }
  50372. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50373. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50374. ciphertxtSz));
  50375. decryptedtxtSz = len;
  50376. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  50377. AES_BLOCK_SIZE, tag));
  50378. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50379. decryptedtxtSz += len;
  50380. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  50381. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50382. /* modify tag*/
  50383. if (i == 0) {
  50384. /* Default uses 96-bits IV length */
  50385. #ifdef WOLFSSL_AES_128
  50386. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50387. key, iv));
  50388. #elif defined(WOLFSSL_AES_192)
  50389. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50390. key, iv));
  50391. #elif defined(WOLFSSL_AES_256)
  50392. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50393. key, iv));
  50394. #endif
  50395. }
  50396. else {
  50397. #ifdef WOLFSSL_AES_128
  50398. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_gcm(), NULL,
  50399. NULL, NULL));
  50400. #elif defined(WOLFSSL_AES_192)
  50401. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_gcm(), NULL,
  50402. NULL, NULL));
  50403. #elif defined(WOLFSSL_AES_256)
  50404. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_gcm(), NULL,
  50405. NULL, NULL));
  50406. #endif
  50407. /* non-default must to set the IV length first */
  50408. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  50409. ivSz, NULL));
  50410. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50411. }
  50412. tag[AES_BLOCK_SIZE-1]+=0xBB;
  50413. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50414. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  50415. AES_BLOCK_SIZE, tag));
  50416. /* fail due to wrong tag */
  50417. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50418. ciphertxtSz));
  50419. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50420. ExpectIntEQ(0, len);
  50421. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  50422. }
  50423. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  50424. return EXPECT_RESULT();
  50425. }
  50426. static int test_wolfssl_EVP_aria_gcm(void)
  50427. {
  50428. int res = TEST_SKIPPED;
  50429. #if defined(OPENSSL_EXTRA) && defined(HAVE_ARIA) && \
  50430. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50431. /* A 256 bit key, AES_128 will use the first 128 bit*/
  50432. byte *key = (byte*)"01234567890123456789012345678901";
  50433. /* A 128 bit IV */
  50434. byte *iv = (byte*)"0123456789012345";
  50435. int ivSz = ARIA_BLOCK_SIZE;
  50436. /* Message to be encrypted */
  50437. const int plaintxtSz = 40;
  50438. byte plaintxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  50439. XMEMCPY(plaintxt,"for things to change you have to change",plaintxtSz);
  50440. /* Additional non-confidential data */
  50441. byte *aad = (byte*)"Don't spend major time on minor things.";
  50442. unsigned char tag[ARIA_BLOCK_SIZE] = {0};
  50443. int aadSz = (int)XSTRLEN((char*)aad);
  50444. byte ciphertxt[WC_ARIA_GCM_GET_CIPHERTEXT_SIZE(plaintxtSz)];
  50445. byte decryptedtxt[plaintxtSz];
  50446. int ciphertxtSz = 0;
  50447. int decryptedtxtSz = 0;
  50448. int len = 0;
  50449. int i = 0;
  50450. #define TEST_ARIA_GCM_COUNT 6
  50451. EVP_CIPHER_CTX en[TEST_ARIA_GCM_COUNT];
  50452. EVP_CIPHER_CTX de[TEST_ARIA_GCM_COUNT];
  50453. for (i = 0; i < TEST_ARIA_GCM_COUNT; i++) {
  50454. EVP_CIPHER_CTX_init(&en[i]);
  50455. switch (i) {
  50456. case 0:
  50457. /* Default uses 96-bits IV length */
  50458. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, key, iv));
  50459. break;
  50460. case 1:
  50461. /* Default uses 96-bits IV length */
  50462. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, key, iv));
  50463. break;
  50464. case 2:
  50465. /* Default uses 96-bits IV length */
  50466. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, key, iv));
  50467. break;
  50468. case 3:
  50469. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  50470. /* non-default must to set the IV length first */
  50471. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50472. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50473. break;
  50474. case 4:
  50475. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  50476. /* non-default must to set the IV length first */
  50477. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50478. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50479. break;
  50480. case 5:
  50481. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  50482. /* non-default must to set the IV length first */
  50483. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50484. AssertIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50485. break;
  50486. }
  50487. XMEMSET(ciphertxt,0,sizeof(ciphertxt));
  50488. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  50489. AssertIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt, plaintxtSz));
  50490. ciphertxtSz = len;
  50491. AssertIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50492. AssertIntNE(0, XMEMCMP(plaintxt, ciphertxt, plaintxtSz));
  50493. ciphertxtSz += len;
  50494. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG, ARIA_BLOCK_SIZE, tag));
  50495. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  50496. EVP_CIPHER_CTX_init(&de[i]);
  50497. switch (i) {
  50498. case 0:
  50499. /* Default uses 96-bits IV length */
  50500. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, key, iv));
  50501. break;
  50502. case 1:
  50503. /* Default uses 96-bits IV length */
  50504. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, key, iv));
  50505. break;
  50506. case 2:
  50507. /* Default uses 96-bits IV length */
  50508. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, key, iv));
  50509. break;
  50510. case 3:
  50511. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_128_gcm(), NULL, NULL, NULL));
  50512. /* non-default must to set the IV length first */
  50513. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50514. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50515. break;
  50516. case 4:
  50517. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_192_gcm(), NULL, NULL, NULL));
  50518. /* non-default must to set the IV length first */
  50519. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50520. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50521. break;
  50522. case 5:
  50523. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aria_256_gcm(), NULL, NULL, NULL));
  50524. /* non-default must to set the IV length first */
  50525. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  50526. AssertIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50527. break;
  50528. }
  50529. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  50530. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50531. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  50532. decryptedtxtSz = len;
  50533. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  50534. AssertIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50535. decryptedtxtSz += len;
  50536. AssertIntEQ(plaintxtSz, decryptedtxtSz);
  50537. AssertIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50538. XMEMSET(decryptedtxt,0,sizeof(decryptedtxt));
  50539. /* modify tag*/
  50540. tag[AES_BLOCK_SIZE-1]+=0xBB;
  50541. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50542. AssertIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG, ARIA_BLOCK_SIZE, tag));
  50543. /* fail due to wrong tag */
  50544. AssertIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt, ciphertxtSz));
  50545. AssertIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50546. AssertIntEQ(0, len);
  50547. AssertIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  50548. }
  50549. res = TEST_RES_CHECK(1);
  50550. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESGCM */
  50551. return res;
  50552. }
  50553. static int test_wolfssl_EVP_aes_ccm_zeroLen(void)
  50554. {
  50555. EXPECT_DECLS;
  50556. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  50557. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50558. /* Zero length plain text */
  50559. byte key[] = {
  50560. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50561. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50562. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  50563. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50564. }; /* align */
  50565. byte iv[] = {
  50566. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  50567. }; /* align */
  50568. byte plaintxt[1];
  50569. int ivSz = 12;
  50570. int plaintxtSz = 0;
  50571. unsigned char tag[16];
  50572. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  50573. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  50574. int ciphertxtSz = 0;
  50575. int decryptedtxtSz = 0;
  50576. int len = 0;
  50577. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  50578. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  50579. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_aes_256_ccm(), NULL, key, iv));
  50580. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  50581. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  50582. plaintxtSz));
  50583. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  50584. ciphertxtSz += len;
  50585. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  50586. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  50587. ExpectIntEQ(0, ciphertxtSz);
  50588. EVP_CIPHER_CTX_init(de);
  50589. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_aes_256_ccm(), NULL, key, iv));
  50590. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  50591. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  50592. decryptedtxtSz = len;
  50593. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  50594. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  50595. decryptedtxtSz += len;
  50596. ExpectIntEQ(0, decryptedtxtSz);
  50597. EVP_CIPHER_CTX_free(en);
  50598. EVP_CIPHER_CTX_free(de);
  50599. #endif
  50600. return EXPECT_RESULT();
  50601. }
  50602. static int test_wolfssl_EVP_aes_ccm(void)
  50603. {
  50604. EXPECT_DECLS;
  50605. #if defined(OPENSSL_EXTRA) && !defined(NO_AES) && defined(HAVE_AESCCM) && \
  50606. !defined(HAVE_SELFTEST) && !defined(HAVE_FIPS)
  50607. /* A 256 bit key, AES_128 will use the first 128 bit*/
  50608. byte *key = (byte*)"01234567890123456789012345678901";
  50609. /* A 128 bit IV */
  50610. byte *iv = (byte*)"0123456789012";
  50611. int ivSz = (int)XSTRLEN((char*)iv);
  50612. /* Message to be encrypted */
  50613. byte *plaintxt = (byte*)"for things to change you have to change";
  50614. /* Additional non-confidential data */
  50615. byte *aad = (byte*)"Don't spend major time on minor things.";
  50616. unsigned char tag[AES_BLOCK_SIZE] = {0};
  50617. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  50618. int aadSz = (int)XSTRLEN((char*)aad);
  50619. byte ciphertxt[AES_BLOCK_SIZE * 4] = {0};
  50620. byte decryptedtxt[AES_BLOCK_SIZE * 4] = {0};
  50621. int ciphertxtSz = 0;
  50622. int decryptedtxtSz = 0;
  50623. int len = 0;
  50624. int i = 0;
  50625. int ret;
  50626. EVP_CIPHER_CTX en[2];
  50627. EVP_CIPHER_CTX de[2];
  50628. for (i = 0; i < 2; i++) {
  50629. EVP_CIPHER_CTX_init(&en[i]);
  50630. if (i == 0) {
  50631. /* Default uses 96-bits IV length */
  50632. #ifdef WOLFSSL_AES_128
  50633. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  50634. key, iv));
  50635. #elif defined(WOLFSSL_AES_192)
  50636. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  50637. key, iv));
  50638. #elif defined(WOLFSSL_AES_256)
  50639. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  50640. key, iv));
  50641. #endif
  50642. }
  50643. else {
  50644. #ifdef WOLFSSL_AES_128
  50645. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_128_ccm(), NULL,
  50646. NULL, NULL));
  50647. #elif defined(WOLFSSL_AES_192)
  50648. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_192_ccm(), NULL,
  50649. NULL, NULL));
  50650. #elif defined(WOLFSSL_AES_256)
  50651. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_aes_256_ccm(), NULL,
  50652. NULL, NULL));
  50653. #endif
  50654. /* non-default must to set the IV length first */
  50655. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  50656. ivSz, NULL));
  50657. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  50658. }
  50659. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  50660. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  50661. plaintxtSz));
  50662. ciphertxtSz = len;
  50663. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  50664. ciphertxtSz += len;
  50665. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  50666. AES_BLOCK_SIZE, tag));
  50667. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]);
  50668. ExpectIntEQ(ret, 1);
  50669. EVP_CIPHER_CTX_init(&de[i]);
  50670. if (i == 0) {
  50671. /* Default uses 96-bits IV length */
  50672. #ifdef WOLFSSL_AES_128
  50673. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  50674. key, iv));
  50675. #elif defined(WOLFSSL_AES_192)
  50676. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  50677. key, iv));
  50678. #elif defined(WOLFSSL_AES_256)
  50679. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  50680. key, iv));
  50681. #endif
  50682. }
  50683. else {
  50684. #ifdef WOLFSSL_AES_128
  50685. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_128_ccm(), NULL,
  50686. NULL, NULL));
  50687. #elif defined(WOLFSSL_AES_192)
  50688. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_192_ccm(), NULL,
  50689. NULL, NULL));
  50690. #elif defined(WOLFSSL_AES_256)
  50691. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_aes_256_ccm(), NULL,
  50692. NULL, NULL));
  50693. #endif
  50694. /* non-default must to set the IV length first */
  50695. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  50696. ivSz, NULL));
  50697. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  50698. }
  50699. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50700. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50701. ciphertxtSz));
  50702. decryptedtxtSz = len;
  50703. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  50704. AES_BLOCK_SIZE, tag));
  50705. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50706. decryptedtxtSz += len;
  50707. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  50708. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  50709. /* modify tag*/
  50710. tag[AES_BLOCK_SIZE-1]+=0xBB;
  50711. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  50712. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  50713. AES_BLOCK_SIZE, tag));
  50714. /* fail due to wrong tag */
  50715. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  50716. ciphertxtSz));
  50717. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  50718. ExpectIntEQ(0, len);
  50719. ret = wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]);
  50720. ExpectIntEQ(ret, 1);
  50721. }
  50722. #endif /* OPENSSL_EXTRA && !NO_AES && HAVE_AESCCM */
  50723. return EXPECT_RESULT();
  50724. }
  50725. static int test_wolfssl_EVP_chacha20_poly1305(void)
  50726. {
  50727. EXPECT_DECLS;
  50728. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  50729. byte key[CHACHA20_POLY1305_AEAD_KEYSIZE];
  50730. byte iv [CHACHA20_POLY1305_AEAD_IV_SIZE];
  50731. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  50732. byte aad[] = {0xAA, 0XBB, 0xCC, 0xDD, 0xEE, 0xFF};
  50733. byte cipherText[sizeof(plainText)];
  50734. byte decryptedText[sizeof(plainText)];
  50735. byte tag[CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE];
  50736. EVP_CIPHER_CTX* ctx = NULL;
  50737. int outSz;
  50738. /* Encrypt. */
  50739. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50740. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  50741. NULL), WOLFSSL_SUCCESS);
  50742. /* Invalid IV length. */
  50743. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  50744. CHACHA20_POLY1305_AEAD_IV_SIZE-1, NULL), WOLFSSL_FAILURE);
  50745. /* Valid IV length. */
  50746. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  50747. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  50748. /* Invalid tag length. */
  50749. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  50750. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, NULL), WOLFSSL_FAILURE);
  50751. /* Valid tag length. */
  50752. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  50753. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, NULL), WOLFSSL_SUCCESS);
  50754. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50755. ExpectIntEQ(EVP_EncryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  50756. WOLFSSL_SUCCESS);
  50757. ExpectIntEQ(outSz, sizeof(aad));
  50758. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  50759. sizeof(plainText)), WOLFSSL_SUCCESS);
  50760. ExpectIntEQ(outSz, sizeof(plainText));
  50761. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  50762. ExpectIntEQ(outSz, 0);
  50763. /* Invalid tag length. */
  50764. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  50765. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE-1, tag), WOLFSSL_FAILURE);
  50766. /* Valid tag length. */
  50767. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
  50768. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  50769. EVP_CIPHER_CTX_free(ctx);
  50770. ctx = NULL;
  50771. /* Decrypt. */
  50772. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50773. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20_poly1305(), NULL, NULL,
  50774. NULL), WOLFSSL_SUCCESS);
  50775. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
  50776. CHACHA20_POLY1305_AEAD_IV_SIZE, NULL), WOLFSSL_SUCCESS);
  50777. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  50778. CHACHA20_POLY1305_AEAD_AUTHTAG_SIZE, tag), WOLFSSL_SUCCESS);
  50779. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50780. ExpectIntEQ(EVP_DecryptUpdate(ctx, NULL, &outSz, aad, sizeof(aad)),
  50781. WOLFSSL_SUCCESS);
  50782. ExpectIntEQ(outSz, sizeof(aad));
  50783. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50784. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50785. ExpectIntEQ(outSz, sizeof(cipherText));
  50786. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50787. WOLFSSL_SUCCESS);
  50788. ExpectIntEQ(outSz, 0);
  50789. EVP_CIPHER_CTX_free(ctx);
  50790. ctx = NULL;
  50791. /* Test partial Inits. CipherInit() allow setting of key and iv
  50792. * in separate calls. */
  50793. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50794. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20_poly1305(),
  50795. key, NULL, 1), WOLFSSL_SUCCESS);
  50796. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  50797. WOLFSSL_SUCCESS);
  50798. ExpectIntEQ(wolfSSL_EVP_CipherUpdate(ctx, NULL, &outSz,
  50799. aad, sizeof(aad)), WOLFSSL_SUCCESS);
  50800. ExpectIntEQ(outSz, sizeof(aad));
  50801. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50802. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50803. ExpectIntEQ(outSz, sizeof(cipherText));
  50804. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50805. WOLFSSL_SUCCESS);
  50806. ExpectIntEQ(outSz, 0);
  50807. EVP_CIPHER_CTX_free(ctx);
  50808. #endif
  50809. return EXPECT_RESULT();
  50810. }
  50811. static int test_wolfssl_EVP_chacha20(void)
  50812. {
  50813. EXPECT_DECLS;
  50814. #if defined(OPENSSL_EXTRA) && defined(HAVE_CHACHA)
  50815. byte key[CHACHA_MAX_KEY_SZ];
  50816. byte iv [WOLFSSL_EVP_CHACHA_IV_BYTES];
  50817. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  50818. byte cipherText[sizeof(plainText)];
  50819. byte decryptedText[sizeof(plainText)];
  50820. EVP_CIPHER_CTX* ctx = NULL;
  50821. int outSz;
  50822. XMEMSET(key, 0, sizeof(key));
  50823. XMEMSET(iv, 0, sizeof(iv));
  50824. /* Encrypt. */
  50825. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50826. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  50827. NULL), WOLFSSL_SUCCESS);
  50828. /* Any tag length must fail - not an AEAD cipher. */
  50829. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
  50830. 16, NULL), WOLFSSL_FAILURE);
  50831. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50832. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  50833. sizeof(plainText)), WOLFSSL_SUCCESS);
  50834. ExpectIntEQ(outSz, sizeof(plainText));
  50835. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  50836. ExpectIntEQ(outSz, 0);
  50837. EVP_CIPHER_CTX_free(ctx);
  50838. ctx = NULL;
  50839. /* Decrypt. */
  50840. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50841. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_chacha20(), NULL, NULL,
  50842. NULL), WOLFSSL_SUCCESS);
  50843. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50844. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50845. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50846. ExpectIntEQ(outSz, sizeof(cipherText));
  50847. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50848. WOLFSSL_SUCCESS);
  50849. ExpectIntEQ(outSz, 0);
  50850. EVP_CIPHER_CTX_free(ctx);
  50851. ctx = NULL;
  50852. /* Test partial Inits. CipherInit() allow setting of key and iv
  50853. * in separate calls. */
  50854. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50855. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_chacha20(),
  50856. key, NULL, 1), WOLFSSL_SUCCESS);
  50857. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  50858. WOLFSSL_SUCCESS);
  50859. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50860. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50861. ExpectIntEQ(outSz, sizeof(cipherText));
  50862. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  50863. WOLFSSL_SUCCESS);
  50864. ExpectIntEQ(outSz, 0);
  50865. EVP_CIPHER_CTX_free(ctx);
  50866. #endif
  50867. return EXPECT_RESULT();
  50868. }
  50869. static int test_wolfssl_EVP_sm4_ecb(void)
  50870. {
  50871. int res = TEST_SKIPPED;
  50872. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_ECB)
  50873. EXPECT_DECLS;
  50874. byte key[SM4_KEY_SIZE];
  50875. byte plainText[SM4_BLOCK_SIZE] = {
  50876. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  50877. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  50878. };
  50879. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  50880. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  50881. EVP_CIPHER_CTX* ctx;
  50882. int outSz;
  50883. XMEMSET(key, 0, sizeof(key));
  50884. /* Encrypt. */
  50885. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50886. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  50887. WOLFSSL_SUCCESS);
  50888. /* Any tag length must fail - not an AEAD cipher. */
  50889. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  50890. WOLFSSL_FAILURE);
  50891. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, NULL),
  50892. WOLFSSL_SUCCESS);
  50893. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  50894. sizeof(plainText)), WOLFSSL_SUCCESS);
  50895. ExpectIntEQ(outSz, sizeof(plainText));
  50896. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  50897. WOLFSSL_SUCCESS);
  50898. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  50899. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  50900. EVP_CIPHER_CTX_free(ctx);
  50901. /* Decrypt. */
  50902. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50903. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ecb(), NULL, NULL, NULL),
  50904. WOLFSSL_SUCCESS);
  50905. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, NULL),
  50906. WOLFSSL_SUCCESS);
  50907. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50908. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50909. ExpectIntEQ(outSz, sizeof(plainText));
  50910. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  50911. WOLFSSL_SUCCESS);
  50912. ExpectIntEQ(outSz, 0);
  50913. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  50914. EVP_CIPHER_CTX_free(ctx);
  50915. res = EXPECT_RESULT();
  50916. #endif
  50917. return res;
  50918. }
  50919. static int test_wolfssl_EVP_sm4_cbc(void)
  50920. {
  50921. int res = TEST_SKIPPED;
  50922. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CBC)
  50923. EXPECT_DECLS;
  50924. byte key[SM4_KEY_SIZE];
  50925. byte iv[SM4_BLOCK_SIZE];
  50926. byte plainText[SM4_BLOCK_SIZE] = {
  50927. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF,
  50928. 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF
  50929. };
  50930. byte cipherText[sizeof(plainText) + SM4_BLOCK_SIZE];
  50931. byte decryptedText[sizeof(plainText) + SM4_BLOCK_SIZE];
  50932. EVP_CIPHER_CTX* ctx;
  50933. int outSz;
  50934. XMEMSET(key, 0, sizeof(key));
  50935. XMEMSET(iv, 0, sizeof(iv));
  50936. /* Encrypt. */
  50937. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50938. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  50939. WOLFSSL_SUCCESS);
  50940. /* Any tag length must fail - not an AEAD cipher. */
  50941. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  50942. WOLFSSL_FAILURE);
  50943. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50944. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  50945. sizeof(plainText)), WOLFSSL_SUCCESS);
  50946. ExpectIntEQ(outSz, sizeof(plainText));
  50947. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText + outSz, &outSz),
  50948. WOLFSSL_SUCCESS);
  50949. ExpectIntEQ(outSz, SM4_BLOCK_SIZE);
  50950. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  50951. EVP_CIPHER_CTX_free(ctx);
  50952. /* Decrypt. */
  50953. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50954. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_cbc(), NULL, NULL, NULL),
  50955. WOLFSSL_SUCCESS);
  50956. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  50957. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50958. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50959. ExpectIntEQ(outSz, sizeof(plainText));
  50960. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  50961. WOLFSSL_SUCCESS);
  50962. ExpectIntEQ(outSz, 0);
  50963. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  50964. EVP_CIPHER_CTX_free(ctx);
  50965. /* Test partial Inits. CipherInit() allow setting of key and iv
  50966. * in separate calls. */
  50967. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  50968. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_cbc(), key, NULL, 0),
  50969. WOLFSSL_SUCCESS);
  50970. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 0),
  50971. WOLFSSL_SUCCESS);
  50972. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  50973. sizeof(cipherText)), WOLFSSL_SUCCESS);
  50974. ExpectIntEQ(outSz, sizeof(plainText));
  50975. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText + outSz, &outSz),
  50976. WOLFSSL_SUCCESS);
  50977. ExpectIntEQ(outSz, 0);
  50978. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  50979. EVP_CIPHER_CTX_free(ctx);
  50980. res = EXPECT_RESULT();
  50981. #endif
  50982. return res;
  50983. }
  50984. static int test_wolfssl_EVP_sm4_ctr(void)
  50985. {
  50986. int res = TEST_SKIPPED;
  50987. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CTR)
  50988. EXPECT_DECLS;
  50989. byte key[SM4_KEY_SIZE];
  50990. byte iv[SM4_BLOCK_SIZE];
  50991. byte plainText[] = {0xDE, 0xAD, 0xBE, 0xEF};
  50992. byte cipherText[sizeof(plainText)];
  50993. byte decryptedText[sizeof(plainText)];
  50994. EVP_CIPHER_CTX* ctx;
  50995. int outSz;
  50996. XMEMSET(key, 0, sizeof(key));
  50997. XMEMSET(iv, 0, sizeof(iv));
  50998. /* Encrypt. */
  50999. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51000. ExpectIntEQ(EVP_EncryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  51001. WOLFSSL_SUCCESS);
  51002. /* Any tag length must fail - not an AEAD cipher. */
  51003. ExpectIntEQ(EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, 16, NULL),
  51004. WOLFSSL_FAILURE);
  51005. ExpectIntEQ(EVP_EncryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  51006. ExpectIntEQ(EVP_EncryptUpdate(ctx, cipherText, &outSz, plainText,
  51007. sizeof(plainText)), WOLFSSL_SUCCESS);
  51008. ExpectIntEQ(outSz, sizeof(plainText));
  51009. ExpectIntEQ(EVP_EncryptFinal_ex(ctx, cipherText, &outSz), WOLFSSL_SUCCESS);
  51010. ExpectIntEQ(outSz, 0);
  51011. ExpectBufNE(cipherText, plainText, sizeof(plainText));
  51012. EVP_CIPHER_CTX_free(ctx);
  51013. /* Decrypt. */
  51014. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51015. ExpectIntEQ(EVP_DecryptInit_ex(ctx, EVP_sm4_ctr(), NULL, NULL, NULL),
  51016. WOLFSSL_SUCCESS);
  51017. ExpectIntEQ(EVP_DecryptInit_ex(ctx, NULL, NULL, key, iv), WOLFSSL_SUCCESS);
  51018. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  51019. sizeof(cipherText)), WOLFSSL_SUCCESS);
  51020. ExpectIntEQ(outSz, sizeof(cipherText));
  51021. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  51022. WOLFSSL_SUCCESS);
  51023. ExpectIntEQ(outSz, 0);
  51024. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  51025. EVP_CIPHER_CTX_free(ctx);
  51026. /* Test partial Inits. CipherInit() allow setting of key and iv
  51027. * in separate calls. */
  51028. ExpectNotNull((ctx = EVP_CIPHER_CTX_new()));
  51029. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, EVP_sm4_ctr(), key, NULL, 1),
  51030. WOLFSSL_SUCCESS);
  51031. ExpectIntEQ(wolfSSL_EVP_CipherInit(ctx, NULL, NULL, iv, 1),
  51032. WOLFSSL_SUCCESS);
  51033. ExpectIntEQ(EVP_DecryptUpdate(ctx, decryptedText, &outSz, cipherText,
  51034. sizeof(cipherText)), WOLFSSL_SUCCESS);
  51035. ExpectIntEQ(outSz, sizeof(cipherText));
  51036. ExpectIntEQ(EVP_DecryptFinal_ex(ctx, decryptedText, &outSz),
  51037. WOLFSSL_SUCCESS);
  51038. ExpectIntEQ(outSz, 0);
  51039. ExpectBufEQ(decryptedText, plainText, sizeof(plainText));
  51040. EVP_CIPHER_CTX_free(ctx);
  51041. res = EXPECT_RESULT();
  51042. #endif
  51043. return res;
  51044. }
  51045. static int test_wolfssl_EVP_sm4_gcm_zeroLen(void)
  51046. {
  51047. int res = TEST_SKIPPED;
  51048. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  51049. /* Zero length plain text */
  51050. EXPECT_DECLS;
  51051. byte key[] = {
  51052. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  51053. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  51054. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  51055. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  51056. }; /* align */
  51057. byte iv[] = {
  51058. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  51059. }; /* align */
  51060. byte plaintxt[1];
  51061. int ivSz = 12;
  51062. int plaintxtSz = 0;
  51063. unsigned char tag[16];
  51064. unsigned char tag_kat[16] = {
  51065. 0x23,0x2f,0x0c,0xfe,0x30,0x8b,0x49,0xea,
  51066. 0x6f,0xc8,0x82,0x29,0xb5,0xdc,0x85,0x8d
  51067. };
  51068. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  51069. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  51070. int ciphertxtSz = 0;
  51071. int decryptedtxtSz = 0;
  51072. int len = 0;
  51073. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  51074. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  51075. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_gcm(), NULL, key, iv));
  51076. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  51077. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  51078. plaintxtSz));
  51079. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  51080. ciphertxtSz += len;
  51081. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_GCM_GET_TAG, 16, tag));
  51082. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  51083. ExpectIntEQ(0, ciphertxtSz);
  51084. ExpectIntEQ(0, XMEMCMP(tag, tag_kat, sizeof(tag)));
  51085. EVP_CIPHER_CTX_init(de);
  51086. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_gcm(), NULL, key, iv));
  51087. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_IVLEN, ivSz, NULL));
  51088. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  51089. decryptedtxtSz = len;
  51090. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_GCM_SET_TAG, 16, tag));
  51091. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  51092. decryptedtxtSz += len;
  51093. ExpectIntEQ(0, decryptedtxtSz);
  51094. EVP_CIPHER_CTX_free(en);
  51095. EVP_CIPHER_CTX_free(de);
  51096. res = EXPECT_RESULT();
  51097. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  51098. return res;
  51099. }
  51100. static int test_wolfssl_EVP_sm4_gcm(void)
  51101. {
  51102. int res = TEST_SKIPPED;
  51103. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_GCM)
  51104. EXPECT_DECLS;
  51105. byte *key = (byte*)"0123456789012345";
  51106. /* A 128 bit IV */
  51107. byte *iv = (byte*)"0123456789012345";
  51108. int ivSz = SM4_BLOCK_SIZE;
  51109. /* Message to be encrypted */
  51110. byte *plaintxt = (byte*)"for things to change you have to change";
  51111. /* Additional non-confidential data */
  51112. byte *aad = (byte*)"Don't spend major time on minor things.";
  51113. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  51114. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  51115. int aadSz = (int)XSTRLEN((char*)aad);
  51116. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  51117. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  51118. int ciphertxtSz = 0;
  51119. int decryptedtxtSz = 0;
  51120. int len = 0;
  51121. int i = 0;
  51122. EVP_CIPHER_CTX en[2];
  51123. EVP_CIPHER_CTX de[2];
  51124. for (i = 0; i < 2; i++) {
  51125. EVP_CIPHER_CTX_init(&en[i]);
  51126. if (i == 0) {
  51127. /* Default uses 96-bits IV length */
  51128. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, key,
  51129. iv));
  51130. }
  51131. else {
  51132. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_gcm(), NULL, NULL,
  51133. NULL));
  51134. /* non-default must to set the IV length first */
  51135. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_SET_IVLEN,
  51136. ivSz, NULL));
  51137. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  51138. }
  51139. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  51140. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  51141. plaintxtSz));
  51142. ciphertxtSz = len;
  51143. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  51144. ciphertxtSz += len;
  51145. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_GCM_GET_TAG,
  51146. SM4_BLOCK_SIZE, tag));
  51147. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  51148. EVP_CIPHER_CTX_init(&de[i]);
  51149. if (i == 0) {
  51150. /* Default uses 96-bits IV length */
  51151. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, key,
  51152. iv));
  51153. }
  51154. else {
  51155. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_gcm(), NULL, NULL,
  51156. NULL));
  51157. /* non-default must to set the IV length first */
  51158. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_IVLEN,
  51159. ivSz, NULL));
  51160. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  51161. }
  51162. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  51163. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  51164. ciphertxtSz));
  51165. decryptedtxtSz = len;
  51166. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  51167. SM4_BLOCK_SIZE, tag));
  51168. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  51169. decryptedtxtSz += len;
  51170. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  51171. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  51172. /* modify tag*/
  51173. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  51174. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  51175. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_GCM_SET_TAG,
  51176. SM4_BLOCK_SIZE, tag));
  51177. /* fail due to wrong tag */
  51178. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  51179. ciphertxtSz));
  51180. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  51181. ExpectIntEQ(0, len);
  51182. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  51183. }
  51184. res = EXPECT_RESULT();
  51185. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_GCM */
  51186. return res;
  51187. }
  51188. static int test_wolfssl_EVP_sm4_ccm_zeroLen(void)
  51189. {
  51190. int res = TEST_SKIPPED;
  51191. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  51192. /* Zero length plain text */
  51193. EXPECT_DECLS;
  51194. byte key[] = {
  51195. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  51196. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  51197. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  51198. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  51199. }; /* align */
  51200. byte iv[] = {
  51201. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
  51202. }; /* align */
  51203. byte plaintxt[1];
  51204. int ivSz = 12;
  51205. int plaintxtSz = 0;
  51206. unsigned char tag[16];
  51207. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  51208. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  51209. int ciphertxtSz = 0;
  51210. int decryptedtxtSz = 0;
  51211. int len = 0;
  51212. EVP_CIPHER_CTX *en = EVP_CIPHER_CTX_new();
  51213. EVP_CIPHER_CTX *de = EVP_CIPHER_CTX_new();
  51214. ExpectIntEQ(1, EVP_EncryptInit_ex(en, EVP_sm4_ccm(), NULL, key, iv));
  51215. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  51216. ExpectIntEQ(1, EVP_EncryptUpdate(en, ciphertxt, &ciphertxtSz , plaintxt,
  51217. plaintxtSz));
  51218. ExpectIntEQ(1, EVP_EncryptFinal_ex(en, ciphertxt, &len));
  51219. ciphertxtSz += len;
  51220. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(en, EVP_CTRL_CCM_GET_TAG, 16, tag));
  51221. ExpectIntEQ(1, EVP_CIPHER_CTX_cleanup(en));
  51222. ExpectIntEQ(0, ciphertxtSz);
  51223. EVP_CIPHER_CTX_init(de);
  51224. ExpectIntEQ(1, EVP_DecryptInit_ex(de, EVP_sm4_ccm(), NULL, key, iv));
  51225. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_IVLEN, ivSz, NULL));
  51226. ExpectIntEQ(1, EVP_DecryptUpdate(de, NULL, &len, ciphertxt, len));
  51227. decryptedtxtSz = len;
  51228. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(de, EVP_CTRL_CCM_SET_TAG, 16, tag));
  51229. ExpectIntEQ(1, EVP_DecryptFinal_ex(de, decryptedtxt, &len));
  51230. decryptedtxtSz += len;
  51231. ExpectIntEQ(0, decryptedtxtSz);
  51232. EVP_CIPHER_CTX_free(en);
  51233. EVP_CIPHER_CTX_free(de);
  51234. res = EXPECT_RESULT();
  51235. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  51236. return res;
  51237. }
  51238. static int test_wolfssl_EVP_sm4_ccm(void)
  51239. {
  51240. int res = TEST_SKIPPED;
  51241. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM4_CCM)
  51242. EXPECT_DECLS;
  51243. byte *key = (byte*)"0123456789012345";
  51244. byte *iv = (byte*)"0123456789012";
  51245. int ivSz = (int)XSTRLEN((char*)iv);
  51246. /* Message to be encrypted */
  51247. byte *plaintxt = (byte*)"for things to change you have to change";
  51248. /* Additional non-confidential data */
  51249. byte *aad = (byte*)"Don't spend major time on minor things.";
  51250. unsigned char tag[SM4_BLOCK_SIZE] = {0};
  51251. int plaintxtSz = (int)XSTRLEN((char*)plaintxt);
  51252. int aadSz = (int)XSTRLEN((char*)aad);
  51253. byte ciphertxt[SM4_BLOCK_SIZE * 4] = {0};
  51254. byte decryptedtxt[SM4_BLOCK_SIZE * 4] = {0};
  51255. int ciphertxtSz = 0;
  51256. int decryptedtxtSz = 0;
  51257. int len = 0;
  51258. int i = 0;
  51259. EVP_CIPHER_CTX en[2];
  51260. EVP_CIPHER_CTX de[2];
  51261. for (i = 0; i < 2; i++) {
  51262. EVP_CIPHER_CTX_init(&en[i]);
  51263. if (i == 0) {
  51264. /* Default uses 96-bits IV length */
  51265. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, key,
  51266. iv));
  51267. }
  51268. else {
  51269. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], EVP_sm4_ccm(), NULL, NULL,
  51270. NULL));
  51271. /* non-default must to set the IV length first */
  51272. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_SET_IVLEN,
  51273. ivSz, NULL));
  51274. ExpectIntEQ(1, EVP_EncryptInit_ex(&en[i], NULL, NULL, key, iv));
  51275. }
  51276. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], NULL, &len, aad, aadSz));
  51277. ExpectIntEQ(1, EVP_EncryptUpdate(&en[i], ciphertxt, &len, plaintxt,
  51278. plaintxtSz));
  51279. ciphertxtSz = len;
  51280. ExpectIntEQ(1, EVP_EncryptFinal_ex(&en[i], ciphertxt, &len));
  51281. ciphertxtSz += len;
  51282. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&en[i], EVP_CTRL_CCM_GET_TAG,
  51283. SM4_BLOCK_SIZE, tag));
  51284. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&en[i]), 1);
  51285. EVP_CIPHER_CTX_init(&de[i]);
  51286. if (i == 0) {
  51287. /* Default uses 96-bits IV length */
  51288. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, key,
  51289. iv));
  51290. }
  51291. else {
  51292. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], EVP_sm4_ccm(), NULL, NULL,
  51293. NULL));
  51294. /* non-default must to set the IV length first */
  51295. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_IVLEN,
  51296. ivSz, NULL));
  51297. ExpectIntEQ(1, EVP_DecryptInit_ex(&de[i], NULL, NULL, key, iv));
  51298. }
  51299. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  51300. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  51301. ciphertxtSz));
  51302. decryptedtxtSz = len;
  51303. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  51304. SM4_BLOCK_SIZE, tag));
  51305. ExpectIntEQ(1, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  51306. decryptedtxtSz += len;
  51307. ExpectIntEQ(ciphertxtSz, decryptedtxtSz);
  51308. ExpectIntEQ(0, XMEMCMP(plaintxt, decryptedtxt, decryptedtxtSz));
  51309. /* modify tag*/
  51310. tag[SM4_BLOCK_SIZE-1]+=0xBB;
  51311. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], NULL, &len, aad, aadSz));
  51312. ExpectIntEQ(1, EVP_CIPHER_CTX_ctrl(&de[i], EVP_CTRL_CCM_SET_TAG,
  51313. SM4_BLOCK_SIZE, tag));
  51314. /* fail due to wrong tag */
  51315. ExpectIntEQ(1, EVP_DecryptUpdate(&de[i], decryptedtxt, &len, ciphertxt,
  51316. ciphertxtSz));
  51317. ExpectIntEQ(0, EVP_DecryptFinal_ex(&de[i], decryptedtxt, &len));
  51318. ExpectIntEQ(0, len);
  51319. ExpectIntEQ(wolfSSL_EVP_CIPHER_CTX_cleanup(&de[i]), 1);
  51320. }
  51321. res = EXPECT_RESULT();
  51322. #endif /* OPENSSL_EXTRA && WOLFSSL_SM4_CCM */
  51323. return res;
  51324. }
  51325. static int test_wolfSSL_EVP_PKEY_hkdf(void)
  51326. {
  51327. EXPECT_DECLS;
  51328. #if defined(OPENSSL_EXTRA) && defined(HAVE_HKDF)
  51329. EVP_PKEY_CTX* ctx = NULL;
  51330. byte salt[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  51331. 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
  51332. byte key[] = {0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  51333. 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
  51334. byte info[] = {0X01, 0x02, 0x03, 0x04, 0x05};
  51335. byte info2[] = {0X06, 0x07, 0x08, 0x09, 0x0A};
  51336. byte outKey[34];
  51337. size_t outKeySz = sizeof(outKey);
  51338. /* These expected outputs were gathered by running the same test below using
  51339. * OpenSSL. */
  51340. const byte extractAndExpand[] = {
  51341. 0x8B, 0xEB, 0x90, 0xA9, 0x04, 0xFF, 0x05, 0x10, 0xE4, 0xB5, 0xB1, 0x10,
  51342. 0x31, 0x34, 0xFF, 0x07, 0x5B, 0xE3, 0xC6, 0x93, 0xD4, 0xF8, 0xC7, 0xEE,
  51343. 0x96, 0xDA, 0x78, 0x7A, 0xE2, 0x9A, 0x2D, 0x05, 0x4B, 0xF6
  51344. };
  51345. const byte extractOnly[] = {
  51346. 0xE7, 0x6B, 0x9E, 0x0F, 0xE4, 0x02, 0x1D, 0x62, 0xEA, 0x97, 0x74, 0x5E,
  51347. 0xF4, 0x3C, 0x65, 0x4D, 0xC1, 0x46, 0x98, 0xAA, 0x79, 0x9A, 0xCB, 0x9C,
  51348. 0xCC, 0x3E, 0x7F, 0x2A, 0x2B, 0x41, 0xA1, 0x9E
  51349. };
  51350. const byte expandOnly[] = {
  51351. 0xFF, 0x29, 0x29, 0x56, 0x9E, 0xA7, 0x66, 0x02, 0xDB, 0x4F, 0xDB, 0x53,
  51352. 0x7D, 0x21, 0x67, 0x52, 0xC3, 0x0E, 0xF3, 0xFC, 0x71, 0xCE, 0x67, 0x2B,
  51353. 0xEA, 0x3B, 0xE9, 0xFC, 0xDD, 0xC8, 0xCC, 0xB7, 0x42, 0x74
  51354. };
  51355. const byte extractAndExpandAddInfo[] = {
  51356. 0x5A, 0x74, 0x79, 0x83, 0xA3, 0xA4, 0x2E, 0xB7, 0xD4, 0x08, 0xC2, 0x6A,
  51357. 0x2F, 0xA5, 0xE3, 0x4E, 0xF1, 0xF4, 0x87, 0x3E, 0xA6, 0xC7, 0x88, 0x45,
  51358. 0xD7, 0xE2, 0x15, 0xBC, 0xB8, 0x10, 0xEF, 0x6C, 0x4D, 0x7A
  51359. };
  51360. ExpectNotNull((ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL)));
  51361. ExpectIntEQ(EVP_PKEY_derive_init(ctx), WOLFSSL_SUCCESS);
  51362. /* NULL ctx. */
  51363. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(NULL, EVP_sha256()), WOLFSSL_FAILURE);
  51364. /* NULL md. */
  51365. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, NULL), WOLFSSL_FAILURE);
  51366. ExpectIntEQ(EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()), WOLFSSL_SUCCESS);
  51367. /* NULL ctx. */
  51368. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(NULL, salt, sizeof(salt)),
  51369. WOLFSSL_FAILURE);
  51370. /* NULL salt is ok. */
  51371. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, NULL, sizeof(salt)),
  51372. WOLFSSL_SUCCESS);
  51373. /* Salt length <= 0. */
  51374. /* Length 0 salt is ok. */
  51375. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, 0), WOLFSSL_SUCCESS);
  51376. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, -1), WOLFSSL_FAILURE);
  51377. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt, sizeof(salt)),
  51378. WOLFSSL_SUCCESS);
  51379. /* NULL ctx. */
  51380. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(NULL, key, sizeof(key)),
  51381. WOLFSSL_FAILURE);
  51382. /* NULL key. */
  51383. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, NULL, sizeof(key)),
  51384. WOLFSSL_FAILURE);
  51385. /* Key length <= 0 */
  51386. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, 0), WOLFSSL_FAILURE);
  51387. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, -1), WOLFSSL_FAILURE);
  51388. ExpectIntEQ(EVP_PKEY_CTX_set1_hkdf_key(ctx, key, sizeof(key)),
  51389. WOLFSSL_SUCCESS);
  51390. /* NULL ctx. */
  51391. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(NULL, info, sizeof(info)),
  51392. WOLFSSL_FAILURE);
  51393. /* NULL info is ok. */
  51394. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, NULL, sizeof(info)),
  51395. WOLFSSL_SUCCESS);
  51396. /* Info length <= 0 */
  51397. /* Length 0 info is ok. */
  51398. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, 0), WOLFSSL_SUCCESS);
  51399. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, -1), WOLFSSL_FAILURE);
  51400. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info, sizeof(info)),
  51401. WOLFSSL_SUCCESS);
  51402. /* NULL ctx. */
  51403. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(NULL, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  51404. WOLFSSL_FAILURE);
  51405. /* Extract and expand (default). */
  51406. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51407. ExpectIntEQ(outKeySz, sizeof(extractAndExpand));
  51408. ExpectIntEQ(XMEMCMP(outKey, extractAndExpand, outKeySz), 0);
  51409. /* Extract only. */
  51410. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY),
  51411. WOLFSSL_SUCCESS);
  51412. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51413. ExpectIntEQ(outKeySz, sizeof(extractOnly));
  51414. ExpectIntEQ(XMEMCMP(outKey, extractOnly, outKeySz), 0);
  51415. outKeySz = sizeof(outKey);
  51416. /* Expand only. */
  51417. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY),
  51418. WOLFSSL_SUCCESS);
  51419. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51420. ExpectIntEQ(outKeySz, sizeof(expandOnly));
  51421. ExpectIntEQ(XMEMCMP(outKey, expandOnly, outKeySz), 0);
  51422. outKeySz = sizeof(outKey);
  51423. /* Extract and expand with appended additional info. */
  51424. ExpectIntEQ(EVP_PKEY_CTX_add1_hkdf_info(ctx, info2, sizeof(info2)),
  51425. WOLFSSL_SUCCESS);
  51426. ExpectIntEQ(EVP_PKEY_CTX_hkdf_mode(ctx,
  51427. EVP_PKEY_HKDEF_MODE_EXTRACT_AND_EXPAND), WOLFSSL_SUCCESS);
  51428. ExpectIntEQ(EVP_PKEY_derive(ctx, outKey, &outKeySz), WOLFSSL_SUCCESS);
  51429. ExpectIntEQ(outKeySz, sizeof(extractAndExpandAddInfo));
  51430. ExpectIntEQ(XMEMCMP(outKey, extractAndExpandAddInfo, outKeySz), 0);
  51431. EVP_PKEY_CTX_free(ctx);
  51432. #endif /* OPENSSL_EXTRA && HAVE_HKDF */
  51433. return EXPECT_RESULT();
  51434. }
  51435. #ifndef NO_BIO
  51436. static int test_wolfSSL_PEM_X509_INFO_read_bio(void)
  51437. {
  51438. EXPECT_DECLS;
  51439. #if defined(OPENSSL_ALL) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  51440. BIO* bio = NULL;
  51441. X509_INFO* info = NULL;
  51442. STACK_OF(X509_INFO)* sk = NULL;
  51443. char* subject = NULL;
  51444. char exp1[] = "/C=US/ST=Montana/L=Bozeman/O=Sawtooth/OU=Consulting/"
  51445. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  51446. char exp2[] = "/C=US/ST=Montana/L=Bozeman/O=wolfSSL/OU=Support/"
  51447. "CN=www.wolfssl.com/emailAddress=info@wolfssl.com";
  51448. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  51449. ExpectIntGT(BIO_read_filename(bio, svrCertFile), 0);
  51450. ExpectNotNull(sk = PEM_X509_INFO_read_bio(bio, NULL, NULL, NULL));
  51451. ExpectIntEQ(sk_X509_INFO_num(sk), 2);
  51452. /* using dereference to maintain testing for Apache port*/
  51453. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  51454. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  51455. 0, 0));
  51456. ExpectIntEQ(0, XSTRNCMP(subject, exp1, sizeof(exp1)));
  51457. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  51458. X509_INFO_free(info);
  51459. info = NULL;
  51460. ExpectNotNull(info = sk_X509_INFO_pop(sk));
  51461. ExpectNotNull(subject = X509_NAME_oneline(X509_get_subject_name(info->x509),
  51462. 0, 0));
  51463. ExpectIntEQ(0, XSTRNCMP(subject, exp2, sizeof(exp2)));
  51464. XFREE(subject, 0, DYNAMIC_TYPE_OPENSSL);
  51465. X509_INFO_free(info);
  51466. ExpectNull(info = sk_X509_INFO_pop(sk));
  51467. sk_X509_INFO_pop_free(sk, X509_INFO_free);
  51468. BIO_free(bio);
  51469. #endif
  51470. return EXPECT_RESULT();
  51471. }
  51472. #endif /* !NO_BIO */
  51473. static int test_wolfSSL_X509_NAME_ENTRY_get_object(void)
  51474. {
  51475. EXPECT_DECLS;
  51476. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  51477. X509 *x509 = NULL;
  51478. X509_NAME* name = NULL;
  51479. int idx = 0;
  51480. X509_NAME_ENTRY *ne = NULL;
  51481. ASN1_OBJECT *object = NULL;
  51482. ExpectNotNull(x509 = wolfSSL_X509_load_certificate_file(cliCertFile,
  51483. WOLFSSL_FILETYPE_PEM));
  51484. ExpectNotNull(name = X509_get_subject_name(x509));
  51485. ExpectIntGE(idx = X509_NAME_get_index_by_NID(name, NID_commonName, -1), 0);
  51486. ExpectNotNull(ne = X509_NAME_get_entry(name, idx));
  51487. ExpectNotNull(object = X509_NAME_ENTRY_get_object(ne));
  51488. X509_free(x509);
  51489. #endif
  51490. return EXPECT_RESULT();
  51491. }
  51492. static int test_wolfSSL_X509_STORE_get1_certs(void)
  51493. {
  51494. EXPECT_DECLS;
  51495. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SIGNER_DER_CERT) && \
  51496. !defined(NO_FILESYSTEM) && !defined(NO_RSA)
  51497. X509_STORE_CTX *storeCtx = NULL;
  51498. X509_STORE *store = NULL;
  51499. X509 *caX509 = NULL;
  51500. X509 *svrX509 = NULL;
  51501. X509_NAME *subject = NULL;
  51502. WOLF_STACK_OF(WOLFSSL_X509) *certs = NULL;
  51503. ExpectNotNull(caX509 = X509_load_certificate_file(caCertFile,
  51504. SSL_FILETYPE_PEM));
  51505. ExpectNotNull((svrX509 = wolfSSL_X509_load_certificate_file(svrCertFile,
  51506. SSL_FILETYPE_PEM)));
  51507. ExpectNotNull(storeCtx = X509_STORE_CTX_new());
  51508. ExpectNotNull(store = X509_STORE_new());
  51509. ExpectNotNull(subject = X509_get_subject_name(caX509));
  51510. /* Errors */
  51511. ExpectNull(X509_STORE_get1_certs(storeCtx, subject));
  51512. ExpectNull(X509_STORE_get1_certs(NULL, subject));
  51513. ExpectNull(X509_STORE_get1_certs(storeCtx, NULL));
  51514. ExpectIntEQ(X509_STORE_add_cert(store, caX509), SSL_SUCCESS);
  51515. ExpectIntEQ(X509_STORE_CTX_init(storeCtx, store, caX509, NULL),
  51516. SSL_SUCCESS);
  51517. /* Should find the cert */
  51518. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  51519. ExpectIntEQ(1, wolfSSL_sk_X509_num(certs));
  51520. sk_X509_pop_free(certs, NULL);
  51521. certs = NULL;
  51522. /* Should not find the cert */
  51523. ExpectNotNull(subject = X509_get_subject_name(svrX509));
  51524. ExpectNotNull(certs = X509_STORE_get1_certs(storeCtx, subject));
  51525. ExpectIntEQ(0, wolfSSL_sk_X509_num(certs));
  51526. sk_X509_pop_free(certs, NULL);
  51527. certs = NULL;
  51528. X509_STORE_free(store);
  51529. X509_STORE_CTX_free(storeCtx);
  51530. X509_free(svrX509);
  51531. X509_free(caX509);
  51532. #endif /* OPENSSL_EXTRA && WOLFSSL_SIGNER_DER_CERT && !NO_FILESYSTEM */
  51533. return EXPECT_RESULT();
  51534. }
  51535. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  51536. defined(WOLFSSL_LOCAL_X509_STORE) && \
  51537. (defined(OPENSSL_ALL) || defined(WOLFSSL_QT)) && defined(HAVE_CRL)
  51538. static int test_wolfSSL_X509_STORE_set_get_crl_provider(X509_STORE_CTX* ctx,
  51539. X509_CRL** crl_out, X509* cert) {
  51540. X509_CRL *crl = NULL;
  51541. XFILE fp = XBADFILE;
  51542. char* cert_issuer = X509_NAME_oneline(X509_get_issuer_name(cert), NULL, 0);
  51543. int ret = 0;
  51544. (void)ctx;
  51545. if (cert_issuer == NULL)
  51546. return 0;
  51547. if ((fp = XFOPEN("certs/crl/crl.pem", "rb")) != XBADFILE) {
  51548. PEM_read_X509_CRL(fp, &crl, NULL, NULL);
  51549. XFCLOSE(fp);
  51550. if (crl != NULL) {
  51551. char* crl_issuer = X509_NAME_oneline(
  51552. X509_CRL_get_issuer(crl), NULL, 0);
  51553. if (XSTRCMP(cert_issuer, crl_issuer) == 0) {
  51554. *crl_out = X509_CRL_dup(crl);
  51555. if (*crl_out != NULL)
  51556. ret = 1;
  51557. }
  51558. OPENSSL_free(crl_issuer);
  51559. }
  51560. }
  51561. X509_CRL_free(crl);
  51562. OPENSSL_free(cert_issuer);
  51563. return ret;
  51564. }
  51565. static int test_wolfSSL_X509_STORE_set_get_crl_provider2(X509_STORE_CTX* ctx,
  51566. X509_CRL** crl_out, X509* cert) {
  51567. (void)ctx;
  51568. (void)cert;
  51569. *crl_out = NULL;
  51570. return 1;
  51571. }
  51572. #ifndef NO_WOLFSSL_STUB
  51573. static int test_wolfSSL_X509_STORE_set_get_crl_check(X509_STORE_CTX* ctx,
  51574. X509_CRL* crl) {
  51575. (void)ctx;
  51576. (void)crl;
  51577. return 1;
  51578. }
  51579. #endif
  51580. static int test_wolfSSL_X509_STORE_set_get_crl_verify(int ok,
  51581. X509_STORE_CTX* ctx) {
  51582. int cert_error = X509_STORE_CTX_get_error(ctx);
  51583. X509_VERIFY_PARAM* param = X509_STORE_CTX_get0_param(ctx);
  51584. int flags = X509_VERIFY_PARAM_get_flags(param);
  51585. if ((flags & (X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL)) !=
  51586. (X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL)) {
  51587. /* Make sure the flags are set */
  51588. return 0;
  51589. }
  51590. /* Ignore CRL missing error */
  51591. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  51592. if (cert_error == CRL_MISSING)
  51593. #else
  51594. if (cert_error == X509_V_ERR_UNABLE_TO_GET_CRL)
  51595. #endif
  51596. return 1;
  51597. return ok;
  51598. }
  51599. static int test_wolfSSL_X509_STORE_set_get_crl_ctx_ready(WOLFSSL_CTX* ctx)
  51600. {
  51601. EXPECT_DECLS;
  51602. X509_STORE* cert_store = NULL;
  51603. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  51604. WOLFSSL_SUCCESS);
  51605. ExpectNotNull(cert_store = SSL_CTX_get_cert_store(ctx));
  51606. X509_STORE_set_get_crl(cert_store,
  51607. test_wolfSSL_X509_STORE_set_get_crl_provider);
  51608. #ifndef NO_WOLFSSL_STUB
  51609. X509_STORE_set_check_crl(cert_store,
  51610. test_wolfSSL_X509_STORE_set_get_crl_check);
  51611. #endif
  51612. return EXPECT_RESULT();
  51613. }
  51614. static int test_wolfSSL_X509_STORE_set_get_crl_ctx_ready2(WOLFSSL_CTX* ctx)
  51615. {
  51616. EXPECT_DECLS;
  51617. X509_STORE* cert_store = NULL;
  51618. X509_VERIFY_PARAM* param = NULL;
  51619. SSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  51620. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  51621. WOLFSSL_SUCCESS);
  51622. ExpectNotNull(cert_store = SSL_CTX_get_cert_store(ctx));
  51623. X509_STORE_set_get_crl(cert_store,
  51624. test_wolfSSL_X509_STORE_set_get_crl_provider2);
  51625. #ifndef NO_WOLFSSL_STUB
  51626. X509_STORE_set_check_crl(cert_store,
  51627. test_wolfSSL_X509_STORE_set_get_crl_check);
  51628. #endif
  51629. X509_STORE_set_verify_cb(cert_store,
  51630. test_wolfSSL_X509_STORE_set_get_crl_verify);
  51631. ExpectNotNull(param = X509_STORE_get0_param(cert_store));
  51632. ExpectIntEQ(X509_VERIFY_PARAM_set_flags(
  51633. param, X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL), 1);
  51634. ExpectIntEQ(X509_STORE_set_flags(cert_store,
  51635. X509_V_FLAG_CRL_CHECK | X509_V_FLAG_CRL_CHECK_ALL), 1);
  51636. return EXPECT_RESULT();
  51637. }
  51638. #endif
  51639. /* This test mimics the usage of the CRL provider in gRPC */
  51640. static int test_wolfSSL_X509_STORE_set_get_crl(void)
  51641. {
  51642. EXPECT_DECLS;
  51643. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && \
  51644. defined(WOLFSSL_LOCAL_X509_STORE) && \
  51645. (defined(OPENSSL_ALL) || defined(WOLFSSL_QT)) && defined(HAVE_CRL)
  51646. test_ssl_cbf func_cb_client;
  51647. test_ssl_cbf func_cb_server;
  51648. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  51649. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  51650. func_cb_client.ctx_ready = test_wolfSSL_X509_STORE_set_get_crl_ctx_ready;
  51651. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  51652. &func_cb_server, NULL), TEST_SUCCESS);
  51653. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  51654. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  51655. func_cb_client.ctx_ready = test_wolfSSL_X509_STORE_set_get_crl_ctx_ready2;
  51656. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&func_cb_client,
  51657. &func_cb_server, NULL), TEST_SUCCESS);
  51658. #endif
  51659. return EXPECT_RESULT();
  51660. }
  51661. static int test_wolfSSL_dup_CA_list(void)
  51662. {
  51663. int res = TEST_SKIPPED;
  51664. #if defined(OPENSSL_ALL)
  51665. EXPECT_DECLS;
  51666. STACK_OF(X509_NAME) *originalStack = NULL;
  51667. STACK_OF(X509_NAME) *copyStack = NULL;
  51668. int originalCount = 0;
  51669. int copyCount = 0;
  51670. X509_NAME *name = NULL;
  51671. int i;
  51672. originalStack = sk_X509_NAME_new_null();
  51673. ExpectNotNull(originalStack);
  51674. for (i = 0; i < 3; i++) {
  51675. name = X509_NAME_new();
  51676. ExpectNotNull(name);
  51677. AssertIntEQ(sk_X509_NAME_push(originalStack, name), WOLFSSL_SUCCESS);
  51678. }
  51679. copyStack = SSL_dup_CA_list(originalStack);
  51680. ExpectNotNull(copyStack);
  51681. originalCount = sk_X509_NAME_num(originalStack);
  51682. copyCount = sk_X509_NAME_num(copyStack);
  51683. AssertIntEQ(originalCount, copyCount);
  51684. sk_X509_NAME_pop_free(originalStack, X509_NAME_free);
  51685. sk_X509_NAME_pop_free(copyStack, X509_NAME_free);
  51686. originalStack = NULL;
  51687. copyStack = NULL;
  51688. res = EXPECT_RESULT();
  51689. #endif /* OPENSSL_ALL */
  51690. return res;
  51691. }
  51692. static int test_ForceZero(void)
  51693. {
  51694. EXPECT_DECLS;
  51695. unsigned char data[32];
  51696. unsigned int i, j, len;
  51697. /* Test case with 0 length */
  51698. ForceZero(data, 0);
  51699. /* Test ForceZero */
  51700. for (i = 0; i < sizeof(data); i++) {
  51701. for (len = 1; len < sizeof(data) - i; len++) {
  51702. for (j = 0; j < sizeof(data); j++)
  51703. data[j] = j + 1;
  51704. ForceZero(data + i, len);
  51705. for (j = 0; j < sizeof(data); j++) {
  51706. if (j < i || j >= i + len) {
  51707. ExpectIntNE(data[j], 0x00);
  51708. }
  51709. else {
  51710. ExpectIntEQ(data[j], 0x00);
  51711. }
  51712. }
  51713. }
  51714. }
  51715. return EXPECT_RESULT();
  51716. }
  51717. #ifndef NO_BIO
  51718. static int test_wolfSSL_X509_print(void)
  51719. {
  51720. EXPECT_DECLS;
  51721. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  51722. !defined(NO_RSA) && defined(XSNPRINTF)
  51723. X509 *x509 = NULL;
  51724. BIO *bio = NULL;
  51725. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  51726. const X509_ALGOR *cert_sig_alg = NULL;
  51727. #endif
  51728. ExpectNotNull(x509 = X509_load_certificate_file(svrCertFile,
  51729. WOLFSSL_FILETYPE_PEM));
  51730. /* print to memory */
  51731. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  51732. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  51733. #if defined(OPENSSL_ALL) || defined(WOLFSSL_IP_ALT_NAME)
  51734. #if defined(WC_DISABLE_RADIX_ZERO_PAD)
  51735. /* Will print IP address subject alt name. */
  51736. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3349);
  51737. #elif defined(NO_ASN_TIME)
  51738. /* Will print IP address subject alt name but not Validity. */
  51739. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3235);
  51740. #else
  51741. /* Will print IP address subject alt name. */
  51742. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3350);
  51743. #endif
  51744. #elif defined(NO_ASN_TIME)
  51745. /* With NO_ASN_TIME defined, X509_print skips printing Validity. */
  51746. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3213);
  51747. #else
  51748. ExpectIntEQ(BIO_get_mem_data(bio, NULL), 3328);
  51749. #endif
  51750. BIO_free(bio);
  51751. bio = NULL;
  51752. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  51753. #if defined(OPENSSL_ALL) && !defined(NO_WOLFSSL_DIR)
  51754. /* Print signature */
  51755. ExpectNotNull(cert_sig_alg = X509_get0_tbs_sigalg(x509));
  51756. ExpectIntEQ(X509_signature_print(bio, cert_sig_alg, NULL), SSL_SUCCESS);
  51757. #endif
  51758. /* print to stderr */
  51759. #if !defined(NO_WOLFSSL_DIR)
  51760. ExpectIntEQ(X509_print(bio, x509), SSL_SUCCESS);
  51761. #endif
  51762. /* print again */
  51763. ExpectIntEQ(X509_print_fp(stderr, x509), SSL_SUCCESS);
  51764. X509_free(x509);
  51765. BIO_free(bio);
  51766. #endif
  51767. return EXPECT_RESULT();
  51768. }
  51769. static int test_wolfSSL_X509_CRL_print(void)
  51770. {
  51771. EXPECT_DECLS;
  51772. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && defined(HAVE_CRL)\
  51773. && !defined(NO_FILESYSTEM) && defined(XSNPRINTF)
  51774. X509_CRL* crl = NULL;
  51775. BIO *bio = NULL;
  51776. XFILE fp = XBADFILE;
  51777. ExpectTrue((fp = XFOPEN("./certs/crl/crl.pem", "rb")) != XBADFILE);
  51778. ExpectNotNull(crl = (X509_CRL*)PEM_read_X509_CRL(fp, (X509_CRL **)NULL,
  51779. NULL, NULL));
  51780. if (fp != XBADFILE)
  51781. XFCLOSE(fp);
  51782. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  51783. ExpectIntEQ(X509_CRL_print(bio, crl), SSL_SUCCESS);
  51784. X509_CRL_free(crl);
  51785. BIO_free(bio);
  51786. #endif
  51787. return EXPECT_RESULT();
  51788. }
  51789. static int test_wolfSSL_BIO_get_len(void)
  51790. {
  51791. EXPECT_DECLS;
  51792. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  51793. BIO *bio = NULL;
  51794. const char txt[] = "Some example text to push to the BIO.";
  51795. ExpectIntEQ(wolfSSL_BIO_get_len(bio), BAD_FUNC_ARG);
  51796. ExpectNotNull(bio = wolfSSL_BIO_new(wolfSSL_BIO_s_mem()));
  51797. ExpectIntEQ(wolfSSL_BIO_write(bio, txt, sizeof(txt)), sizeof(txt));
  51798. ExpectIntEQ(wolfSSL_BIO_get_len(bio), sizeof(txt));
  51799. BIO_free(bio);
  51800. bio = NULL;
  51801. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  51802. ExpectIntEQ(wolfSSL_BIO_get_len(bio), WOLFSSL_BAD_FILE);
  51803. BIO_free(bio);
  51804. #endif
  51805. return EXPECT_RESULT();
  51806. }
  51807. #endif /* !NO_BIO */
  51808. static int test_wolfSSL_RSA(void)
  51809. {
  51810. EXPECT_DECLS;
  51811. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN)
  51812. RSA* rsa = NULL;
  51813. const BIGNUM *n = NULL;
  51814. const BIGNUM *e = NULL;
  51815. const BIGNUM *d = NULL;
  51816. const BIGNUM *p = NULL;
  51817. const BIGNUM *q = NULL;
  51818. const BIGNUM *dmp1 = NULL;
  51819. const BIGNUM *dmq1 = NULL;
  51820. const BIGNUM *iqmp = NULL;
  51821. ExpectNotNull(rsa = RSA_new());
  51822. ExpectIntEQ(RSA_size(NULL), 0);
  51823. ExpectIntEQ(RSA_size(rsa), 0);
  51824. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 0);
  51825. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 0);
  51826. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 0);
  51827. #ifdef WOLFSSL_RSA_KEY_CHECK
  51828. ExpectIntEQ(RSA_check_key(rsa), 0);
  51829. #endif
  51830. RSA_free(rsa);
  51831. rsa = NULL;
  51832. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  51833. ExpectIntEQ(RSA_size(rsa), 256);
  51834. #ifdef WOLFSSL_RSA_KEY_CHECK
  51835. ExpectIntEQ(RSA_check_key(NULL), 0);
  51836. ExpectIntEQ(RSA_check_key(rsa), 1);
  51837. #endif
  51838. /* sanity check */
  51839. ExpectIntEQ(RSA_bits(NULL), 0);
  51840. /* key */
  51841. ExpectIntEQ(RSA_bits(rsa), 2048);
  51842. RSA_get0_key(rsa, &n, &e, &d);
  51843. ExpectPtrEq(rsa->n, n);
  51844. ExpectPtrEq(rsa->e, e);
  51845. ExpectPtrEq(rsa->d, d);
  51846. n = NULL;
  51847. e = NULL;
  51848. d = NULL;
  51849. ExpectNotNull(n = BN_new());
  51850. ExpectNotNull(e = BN_new());
  51851. ExpectNotNull(d = BN_new());
  51852. ExpectIntEQ(RSA_set0_key(rsa, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 1);
  51853. if (EXPECT_FAIL()) {
  51854. BN_free((BIGNUM*)n);
  51855. BN_free((BIGNUM*)e);
  51856. BN_free((BIGNUM*)d);
  51857. }
  51858. ExpectPtrEq(rsa->n, n);
  51859. ExpectPtrEq(rsa->e, e);
  51860. ExpectPtrEq(rsa->d, d);
  51861. ExpectIntEQ(RSA_set0_key(rsa, NULL, NULL, NULL), 1);
  51862. ExpectIntEQ(RSA_set0_key(NULL, (BIGNUM*)n, (BIGNUM*)e, (BIGNUM*)d), 0);
  51863. /* crt_params */
  51864. RSA_get0_crt_params(rsa, &dmp1, &dmq1, &iqmp);
  51865. ExpectPtrEq(rsa->dmp1, dmp1);
  51866. ExpectPtrEq(rsa->dmq1, dmq1);
  51867. ExpectPtrEq(rsa->iqmp, iqmp);
  51868. dmp1 = NULL;
  51869. dmq1 = NULL;
  51870. iqmp = NULL;
  51871. ExpectNotNull(dmp1 = BN_new());
  51872. ExpectNotNull(dmq1 = BN_new());
  51873. ExpectNotNull(iqmp = BN_new());
  51874. ExpectIntEQ(RSA_set0_crt_params(rsa, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  51875. (BIGNUM*)iqmp), 1);
  51876. if (EXPECT_FAIL()) {
  51877. BN_free((BIGNUM*)dmp1);
  51878. BN_free((BIGNUM*)dmq1);
  51879. BN_free((BIGNUM*)iqmp);
  51880. }
  51881. ExpectPtrEq(rsa->dmp1, dmp1);
  51882. ExpectPtrEq(rsa->dmq1, dmq1);
  51883. ExpectPtrEq(rsa->iqmp, iqmp);
  51884. ExpectIntEQ(RSA_set0_crt_params(rsa, NULL, NULL, NULL), 1);
  51885. ExpectIntEQ(RSA_set0_crt_params(NULL, (BIGNUM*)dmp1, (BIGNUM*)dmq1,
  51886. (BIGNUM*)iqmp), 0);
  51887. RSA_get0_crt_params(NULL, NULL, NULL, NULL);
  51888. RSA_get0_crt_params(rsa, NULL, NULL, NULL);
  51889. RSA_get0_crt_params(NULL, &dmp1, &dmq1, &iqmp);
  51890. ExpectNull(dmp1);
  51891. ExpectNull(dmq1);
  51892. ExpectNull(iqmp);
  51893. /* factors */
  51894. RSA_get0_factors(rsa, NULL, NULL);
  51895. RSA_get0_factors(rsa, &p, &q);
  51896. ExpectPtrEq(rsa->p, p);
  51897. ExpectPtrEq(rsa->q, q);
  51898. p = NULL;
  51899. q = NULL;
  51900. ExpectNotNull(p = BN_new());
  51901. ExpectNotNull(q = BN_new());
  51902. ExpectIntEQ(RSA_set0_factors(rsa, (BIGNUM*)p, (BIGNUM*)q), 1);
  51903. if (EXPECT_FAIL()) {
  51904. BN_free((BIGNUM*)p);
  51905. BN_free((BIGNUM*)q);
  51906. }
  51907. ExpectPtrEq(rsa->p, p);
  51908. ExpectPtrEq(rsa->q, q);
  51909. ExpectIntEQ(RSA_set0_factors(rsa, NULL, NULL), 1);
  51910. ExpectIntEQ(RSA_set0_factors(NULL, (BIGNUM*)p, (BIGNUM*)q), 0);
  51911. RSA_get0_factors(NULL, NULL, NULL);
  51912. RSA_get0_factors(NULL, &p, &q);
  51913. ExpectNull(p);
  51914. ExpectNull(q);
  51915. ExpectIntEQ(BN_hex2bn(&rsa->n, "1FFFFF"), 1);
  51916. ExpectIntEQ(RSA_bits(rsa), 21);
  51917. RSA_free(rsa);
  51918. rsa = NULL;
  51919. #if !defined(USE_FAST_MATH) || (FP_MAX_BITS >= (3072*2))
  51920. ExpectNotNull(rsa = RSA_generate_key(3072, 17, NULL, NULL));
  51921. ExpectIntEQ(RSA_size(rsa), 384);
  51922. ExpectIntEQ(RSA_bits(rsa), 3072);
  51923. RSA_free(rsa);
  51924. rsa = NULL;
  51925. #endif
  51926. /* remove for now with odd key size until adjusting rsa key size check with
  51927. wc_MakeRsaKey()
  51928. ExpectNotNull(rsa = RSA_generate_key(2999, 65537, NULL, NULL));
  51929. RSA_free(rsa);
  51930. rsa = NULL;
  51931. */
  51932. ExpectNull(RSA_generate_key(-1, 3, NULL, NULL));
  51933. ExpectNull(RSA_generate_key(RSA_MIN_SIZE - 1, 3, NULL, NULL));
  51934. ExpectNull(RSA_generate_key(RSA_MAX_SIZE + 1, 3, NULL, NULL));
  51935. ExpectNull(RSA_generate_key(2048, 0, NULL, NULL));
  51936. #if !defined(NO_FILESYSTEM) && !defined(NO_ASN)
  51937. {
  51938. byte buff[FOURK_BUF];
  51939. byte der[FOURK_BUF];
  51940. const char PrivKeyPemFile[] = "certs/client-keyEnc.pem";
  51941. XFILE f = XBADFILE;
  51942. int bytes = 0;
  51943. /* test loading encrypted RSA private pem w/o password */
  51944. ExpectTrue((f = XFOPEN(PrivKeyPemFile, "rb")) != XBADFILE);
  51945. ExpectIntGT(bytes = (int)XFREAD(buff, 1, sizeof(buff), f), 0);
  51946. if (f != XBADFILE)
  51947. XFCLOSE(f);
  51948. XMEMSET(der, 0, sizeof(der));
  51949. /* test that error value is returned with no password */
  51950. ExpectIntLT(wc_KeyPemToDer(buff, bytes, der, (word32)sizeof(der), ""),
  51951. 0);
  51952. }
  51953. #endif
  51954. #endif
  51955. return EXPECT_RESULT();
  51956. }
  51957. static int test_wolfSSL_RSA_DER(void)
  51958. {
  51959. EXPECT_DECLS;
  51960. #if defined(WOLFSSL_KEY_GEN) && !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  51961. RSA *rsa = NULL;
  51962. int i;
  51963. const unsigned char *buff = NULL;
  51964. unsigned char *newBuff = NULL;
  51965. struct tbl_s
  51966. {
  51967. const unsigned char *der;
  51968. int sz;
  51969. } tbl[] = {
  51970. #ifdef USE_CERT_BUFFERS_1024
  51971. {client_key_der_1024, sizeof_client_key_der_1024},
  51972. {server_key_der_1024, sizeof_server_key_der_1024},
  51973. #endif
  51974. #ifdef USE_CERT_BUFFERS_2048
  51975. {client_key_der_2048, sizeof_client_key_der_2048},
  51976. {server_key_der_2048, sizeof_server_key_der_2048},
  51977. #endif
  51978. {NULL, 0}
  51979. };
  51980. /* Public Key DER */
  51981. struct tbl_s pub[] = {
  51982. #ifdef USE_CERT_BUFFERS_1024
  51983. {client_keypub_der_1024, sizeof_client_keypub_der_1024},
  51984. #endif
  51985. #ifdef USE_CERT_BUFFERS_2048
  51986. {client_keypub_der_2048, sizeof_client_keypub_der_2048},
  51987. #endif
  51988. {NULL, 0}
  51989. };
  51990. ExpectNull(d2i_RSAPublicKey(&rsa, NULL, pub[0].sz));
  51991. buff = pub[0].der;
  51992. ExpectNull(d2i_RSAPublicKey(&rsa, &buff, 1));
  51993. ExpectNull(d2i_RSAPrivateKey(&rsa, NULL, tbl[0].sz));
  51994. buff = tbl[0].der;
  51995. ExpectNull(d2i_RSAPrivateKey(&rsa, &buff, 1));
  51996. ExpectIntEQ(i2d_RSAPublicKey(NULL, NULL), BAD_FUNC_ARG);
  51997. rsa = RSA_new();
  51998. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), 0);
  51999. RSA_free(rsa);
  52000. rsa = NULL;
  52001. for (i = 0; tbl[i].der != NULL; i++)
  52002. {
  52003. /* Passing in pointer results in pointer moving. */
  52004. buff = tbl[i].der;
  52005. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, tbl[i].sz));
  52006. ExpectNotNull(rsa);
  52007. RSA_free(rsa);
  52008. rsa = NULL;
  52009. }
  52010. for (i = 0; tbl[i].der != NULL; i++)
  52011. {
  52012. /* Passing in pointer results in pointer moving. */
  52013. buff = tbl[i].der;
  52014. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &buff, tbl[i].sz));
  52015. ExpectNotNull(rsa);
  52016. RSA_free(rsa);
  52017. rsa = NULL;
  52018. }
  52019. for (i = 0; pub[i].der != NULL; i++)
  52020. {
  52021. buff = pub[i].der;
  52022. ExpectNotNull(d2i_RSAPublicKey(&rsa, &buff, pub[i].sz));
  52023. ExpectNotNull(rsa);
  52024. ExpectIntEQ(i2d_RSAPublicKey(rsa, NULL), pub[i].sz);
  52025. newBuff = NULL;
  52026. ExpectIntEQ(i2d_RSAPublicKey(rsa, &newBuff), pub[i].sz);
  52027. ExpectNotNull(newBuff);
  52028. ExpectIntEQ(XMEMCMP((void *)newBuff, (void *)pub[i].der, pub[i].sz), 0);
  52029. XFREE((void *)newBuff, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  52030. RSA_free(rsa);
  52031. rsa = NULL;
  52032. }
  52033. #endif
  52034. return EXPECT_RESULT();
  52035. }
  52036. static int test_wolfSSL_RSA_print(void)
  52037. {
  52038. EXPECT_DECLS;
  52039. #if defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  52040. !defined(NO_STDIO_FILESYSTEM) && \
  52041. !defined(NO_RSA) && defined(WOLFSSL_KEY_GEN) && \
  52042. !defined(NO_BIO) && defined(XFPRINTF)
  52043. BIO *bio = NULL;
  52044. WOLFSSL_RSA* rsa = NULL;
  52045. ExpectNotNull(bio = BIO_new_fd(STDERR_FILENO, BIO_NOCLOSE));
  52046. ExpectNotNull(rsa = RSA_new());
  52047. ExpectIntEQ(RSA_print(NULL, rsa, 0), -1);
  52048. ExpectIntEQ(RSA_print_fp(XBADFILE, rsa, 0), 0);
  52049. ExpectIntEQ(RSA_print(bio, NULL, 0), -1);
  52050. ExpectIntEQ(RSA_print_fp(stderr, NULL, 0), 0);
  52051. /* Some very large number of indent spaces. */
  52052. ExpectIntEQ(RSA_print(bio, rsa, 128), -1);
  52053. /* RSA is empty. */
  52054. ExpectIntEQ(RSA_print(bio, rsa, 0), 0);
  52055. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 0);
  52056. RSA_free(rsa);
  52057. rsa = NULL;
  52058. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  52059. ExpectIntEQ(RSA_print(bio, rsa, 0), 1);
  52060. ExpectIntEQ(RSA_print(bio, rsa, 4), 1);
  52061. ExpectIntEQ(RSA_print(bio, rsa, -1), 1);
  52062. ExpectIntEQ(RSA_print_fp(stderr, rsa, 0), 1);
  52063. ExpectIntEQ(RSA_print_fp(stderr, rsa, 4), 1);
  52064. ExpectIntEQ(RSA_print_fp(stderr, rsa, -1), 1);
  52065. BIO_free(bio);
  52066. RSA_free(rsa);
  52067. #endif
  52068. return EXPECT_RESULT();
  52069. }
  52070. static int test_wolfSSL_RSA_padding_add_PKCS1_PSS(void)
  52071. {
  52072. EXPECT_DECLS;
  52073. #ifndef NO_RSA
  52074. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  52075. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  52076. RSA *rsa = NULL;
  52077. const unsigned char *derBuf = client_key_der_2048;
  52078. unsigned char em[256] = {0}; /* len = 2048/8 */
  52079. /* Random data simulating a hash */
  52080. const unsigned char mHash[WC_SHA256_DIGEST_SIZE] = {
  52081. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  52082. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  52083. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  52084. };
  52085. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  52086. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(NULL, em, mHash, EVP_sha256(),
  52087. RSA_PSS_SALTLEN_DIGEST), 0);
  52088. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, NULL, mHash, EVP_sha256(),
  52089. RSA_PSS_SALTLEN_DIGEST), 0);
  52090. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, NULL, EVP_sha256(),
  52091. RSA_PSS_SALTLEN_DIGEST), 0);
  52092. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, NULL,
  52093. RSA_PSS_SALTLEN_DIGEST), 0);
  52094. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), -5), 0);
  52095. ExpectIntEQ(RSA_verify_PKCS1_PSS(NULL, mHash, EVP_sha256(), em,
  52096. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  52097. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, NULL, EVP_sha256(), em,
  52098. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  52099. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, NULL, em,
  52100. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  52101. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), NULL,
  52102. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  52103. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  52104. RSA_PSS_SALTLEN_MAX_SIGN), 0);
  52105. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, -5), 0);
  52106. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  52107. RSA_PSS_SALTLEN_DIGEST), 1);
  52108. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  52109. RSA_PSS_SALTLEN_DIGEST), 1);
  52110. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  52111. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  52112. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  52113. RSA_PSS_SALTLEN_MAX_SIGN), 1);
  52114. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(),
  52115. RSA_PSS_SALTLEN_MAX), 1);
  52116. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em,
  52117. RSA_PSS_SALTLEN_MAX), 1);
  52118. ExpectIntEQ(RSA_padding_add_PKCS1_PSS(rsa, em, mHash, EVP_sha256(), 10), 1);
  52119. ExpectIntEQ(RSA_verify_PKCS1_PSS(rsa, mHash, EVP_sha256(), em, 10), 1);
  52120. RSA_free(rsa);
  52121. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  52122. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  52123. #endif
  52124. return EXPECT_RESULT();
  52125. }
  52126. static int test_wolfSSL_RSA_sign_sha3(void)
  52127. {
  52128. EXPECT_DECLS;
  52129. #if !defined(NO_RSA) && defined(WOLFSSL_SHA3) && !defined(WOLFSSL_NOSHA3_256)
  52130. #if defined(OPENSSL_ALL) && defined(WC_RSA_PSS) && !defined(WC_NO_RNG)
  52131. RSA* rsa = NULL;
  52132. const unsigned char *derBuf = client_key_der_2048;
  52133. unsigned char sigRet[256] = {0};
  52134. unsigned int sigLen = sizeof(sigRet);
  52135. /* Random data simulating a hash */
  52136. const unsigned char mHash[WC_SHA3_256_DIGEST_SIZE] = {
  52137. 0x28, 0x6e, 0xfd, 0xf8, 0x76, 0xc7, 0x00, 0x3d, 0x91, 0x4e, 0x59, 0xe4,
  52138. 0x8e, 0xb7, 0x40, 0x7b, 0xd1, 0x0c, 0x98, 0x4b, 0xe3, 0x3d, 0xb3, 0xeb,
  52139. 0x6f, 0x8a, 0x3c, 0x42, 0xab, 0x21, 0xad, 0x28
  52140. };
  52141. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &derBuf, sizeof_client_key_der_2048));
  52142. ExpectIntEQ(RSA_sign(NID_sha3_256, mHash, sizeof(mHash), sigRet, &sigLen,
  52143. rsa), 1);
  52144. RSA_free(rsa);
  52145. #endif /* OPENSSL_ALL && WC_RSA_PSS && !WC_NO_RNG*/
  52146. #endif /* !NO_RSA && WOLFSSL_SHA3 && !WOLFSSL_NOSHA3_256*/
  52147. return EXPECT_RESULT();
  52148. }
  52149. static int test_wolfSSL_RSA_get0_key(void)
  52150. {
  52151. EXPECT_DECLS;
  52152. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52153. RSA *rsa = NULL;
  52154. const BIGNUM* n = NULL;
  52155. const BIGNUM* e = NULL;
  52156. const BIGNUM* d = NULL;
  52157. const unsigned char* der;
  52158. int derSz;
  52159. #ifdef USE_CERT_BUFFERS_1024
  52160. der = client_key_der_1024;
  52161. derSz = sizeof_client_key_der_1024;
  52162. #elif defined(USE_CERT_BUFFERS_2048)
  52163. der = client_key_der_2048;
  52164. derSz = sizeof_client_key_der_2048;
  52165. #else
  52166. der = NULL;
  52167. derSz = 0;
  52168. #endif
  52169. if (der != NULL) {
  52170. RSA_get0_key(NULL, NULL, NULL, NULL);
  52171. RSA_get0_key(rsa, NULL, NULL, NULL);
  52172. RSA_get0_key(NULL, &n, &e, &d);
  52173. ExpectNull(n);
  52174. ExpectNull(e);
  52175. ExpectNull(d);
  52176. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, derSz));
  52177. ExpectNotNull(rsa);
  52178. RSA_get0_key(rsa, NULL, NULL, NULL);
  52179. RSA_get0_key(rsa, &n, NULL, NULL);
  52180. ExpectNotNull(n);
  52181. RSA_get0_key(rsa, NULL, &e, NULL);
  52182. ExpectNotNull(e);
  52183. RSA_get0_key(rsa, NULL, NULL, &d);
  52184. ExpectNotNull(d);
  52185. RSA_get0_key(rsa, &n, &e, &d);
  52186. ExpectNotNull(n);
  52187. ExpectNotNull(e);
  52188. ExpectNotNull(d);
  52189. RSA_free(rsa);
  52190. }
  52191. #endif
  52192. return EXPECT_RESULT();
  52193. }
  52194. static int test_wolfSSL_RSA_meth(void)
  52195. {
  52196. EXPECT_DECLS;
  52197. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52198. RSA *rsa = NULL;
  52199. RSA_METHOD *rsa_meth = NULL;
  52200. #ifdef WOLFSSL_KEY_GEN
  52201. ExpectNotNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  52202. RSA_free(rsa);
  52203. rsa = NULL;
  52204. #else
  52205. ExpectNull(rsa = RSA_generate_key(2048, 3, NULL, NULL));
  52206. #endif
  52207. ExpectNotNull(RSA_get_default_method());
  52208. wolfSSL_RSA_meth_free(NULL);
  52209. ExpectNull(wolfSSL_RSA_meth_new(NULL, 0));
  52210. ExpectNotNull(rsa_meth = RSA_meth_new("placeholder RSA method",
  52211. RSA_METHOD_FLAG_NO_CHECK));
  52212. #ifndef NO_WOLFSSL_STUB
  52213. ExpectIntEQ(RSA_meth_set_pub_enc(rsa_meth, NULL), 1);
  52214. ExpectIntEQ(RSA_meth_set_pub_dec(rsa_meth, NULL), 1);
  52215. ExpectIntEQ(RSA_meth_set_priv_enc(rsa_meth, NULL), 1);
  52216. ExpectIntEQ(RSA_meth_set_priv_dec(rsa_meth, NULL), 1);
  52217. ExpectIntEQ(RSA_meth_set_init(rsa_meth, NULL), 1);
  52218. ExpectIntEQ(RSA_meth_set_finish(rsa_meth, NULL), 1);
  52219. ExpectIntEQ(RSA_meth_set0_app_data(rsa_meth, NULL), 1);
  52220. #endif
  52221. ExpectIntEQ(RSA_flags(NULL), 0);
  52222. RSA_set_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  52223. RSA_clear_flags(NULL, RSA_FLAG_CACHE_PUBLIC);
  52224. ExpectIntEQ(RSA_test_flags(NULL, RSA_FLAG_CACHE_PUBLIC), 0);
  52225. ExpectNotNull(rsa = RSA_new());
  52226. /* No method set. */
  52227. ExpectIntEQ(RSA_flags(rsa), 0);
  52228. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  52229. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  52230. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  52231. ExpectIntEQ(RSA_set_method(NULL, rsa_meth), 1);
  52232. ExpectIntEQ(RSA_set_method(rsa, rsa_meth), 1);
  52233. if (EXPECT_FAIL()) {
  52234. wolfSSL_RSA_meth_free(rsa_meth);
  52235. }
  52236. ExpectNull(RSA_get_method(NULL));
  52237. ExpectPtrEq(RSA_get_method(rsa), rsa_meth);
  52238. ExpectIntEQ(RSA_flags(rsa), RSA_METHOD_FLAG_NO_CHECK);
  52239. RSA_set_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  52240. ExpectIntNE(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  52241. ExpectIntEQ(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC |
  52242. RSA_METHOD_FLAG_NO_CHECK);
  52243. RSA_clear_flags(rsa, RSA_FLAG_CACHE_PUBLIC);
  52244. ExpectIntEQ(RSA_test_flags(rsa, RSA_FLAG_CACHE_PUBLIC), 0);
  52245. ExpectIntNE(RSA_flags(rsa), RSA_FLAG_CACHE_PUBLIC);
  52246. /* rsa_meth is freed here */
  52247. RSA_free(rsa);
  52248. #endif
  52249. return EXPECT_RESULT();
  52250. }
  52251. static int test_wolfSSL_RSA_verify(void)
  52252. {
  52253. EXPECT_DECLS;
  52254. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_FILESYSTEM)
  52255. #ifndef NO_BIO
  52256. XFILE fp = XBADFILE;
  52257. RSA *pKey = NULL;
  52258. RSA *pubKey = NULL;
  52259. X509 *cert = NULL;
  52260. const char *text = "Hello wolfSSL !";
  52261. unsigned char hash[SHA256_DIGEST_LENGTH];
  52262. unsigned char signature[2048/8];
  52263. unsigned int signatureLength;
  52264. byte *buf = NULL;
  52265. BIO *bio = NULL;
  52266. SHA256_CTX c;
  52267. EVP_PKEY *evpPkey = NULL;
  52268. EVP_PKEY *evpPubkey = NULL;
  52269. size_t sz;
  52270. /* generate hash */
  52271. SHA256_Init(&c);
  52272. SHA256_Update(&c, text, strlen(text));
  52273. SHA256_Final(hash, &c);
  52274. #ifdef WOLFSSL_SMALL_STACK_CACHE
  52275. /* workaround for small stack cache case */
  52276. wc_Sha256Free((wc_Sha256*)&c);
  52277. #endif
  52278. /* read privete key file */
  52279. ExpectTrue((fp = XFOPEN(svrKeyFile, "rb")) != XBADFILE);
  52280. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_END), 0);
  52281. ExpectTrue((sz = XFTELL(fp)) > 0);
  52282. ExpectIntEQ(XFSEEK(fp, 0, XSEEK_SET), 0);
  52283. ExpectNotNull(buf = (byte*)XMALLOC(sz, NULL, DYNAMIC_TYPE_FILE));
  52284. ExpectIntEQ(XFREAD(buf, 1, sz, fp), sz);
  52285. if (fp != XBADFILE) {
  52286. XFCLOSE(fp);
  52287. fp = XBADFILE;
  52288. }
  52289. /* read private key and sign hash data */
  52290. ExpectNotNull(bio = BIO_new_mem_buf(buf, (int)sz));
  52291. ExpectNotNull(evpPkey = PEM_read_bio_PrivateKey(bio, NULL, NULL, NULL));
  52292. ExpectNotNull(pKey = EVP_PKEY_get1_RSA(evpPkey));
  52293. ExpectIntEQ(RSA_sign(NID_sha256, hash, SHA256_DIGEST_LENGTH,
  52294. signature, &signatureLength, pKey), SSL_SUCCESS);
  52295. /* read public key and verify signed data */
  52296. ExpectTrue((fp = XFOPEN(svrCertFile,"rb")) != XBADFILE);
  52297. ExpectNotNull(cert = PEM_read_X509(fp, 0, 0, 0 ));
  52298. if (fp != XBADFILE)
  52299. XFCLOSE(fp);
  52300. ExpectNotNull(evpPubkey = X509_get_pubkey(cert));
  52301. ExpectNotNull(pubKey = EVP_PKEY_get1_RSA(evpPubkey));
  52302. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  52303. signatureLength, pubKey), SSL_SUCCESS);
  52304. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, NULL,
  52305. signatureLength, NULL), SSL_FAILURE);
  52306. ExpectIntEQ(RSA_verify(NID_sha256, NULL, SHA256_DIGEST_LENGTH, signature,
  52307. signatureLength, pubKey), SSL_FAILURE);
  52308. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, NULL,
  52309. signatureLength, pubKey), SSL_FAILURE);
  52310. ExpectIntEQ(RSA_verify(NID_sha256, hash, SHA256_DIGEST_LENGTH, signature,
  52311. signatureLength, NULL), SSL_FAILURE);
  52312. RSA_free(pKey);
  52313. EVP_PKEY_free(evpPkey);
  52314. RSA_free(pubKey);
  52315. EVP_PKEY_free(evpPubkey);
  52316. X509_free(cert);
  52317. BIO_free(bio);
  52318. XFREE(buf, NULL, DYNAMIC_TYPE_FILE);
  52319. #endif
  52320. #endif
  52321. return EXPECT_RESULT();
  52322. }
  52323. static int test_wolfSSL_RSA_sign(void)
  52324. {
  52325. EXPECT_DECLS;
  52326. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52327. RSA *rsa;
  52328. unsigned char hash[SHA256_DIGEST_LENGTH];
  52329. #ifdef USE_CERT_BUFFERS_1024
  52330. const unsigned char* privDer = client_key_der_1024;
  52331. size_t privDerSz = sizeof_client_key_der_1024;
  52332. const unsigned char* pubDer = client_keypub_der_1024;
  52333. size_t pubDerSz = sizeof_client_keypub_der_1024;
  52334. unsigned char signature[1024/8];
  52335. #else
  52336. const unsigned char* privDer = client_key_der_2048;
  52337. size_t privDerSz = sizeof_client_key_der_2048;
  52338. const unsigned char* pubDer = client_keypub_der_2048;
  52339. size_t pubDerSz = sizeof_client_keypub_der_2048;
  52340. unsigned char signature[2048/8];
  52341. #endif
  52342. unsigned int signatureLen;
  52343. const unsigned char* der;
  52344. XMEMSET(hash, 0, sizeof(hash));
  52345. der = privDer;
  52346. rsa = NULL;
  52347. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52348. /* Invalid parameters. */
  52349. ExpectIntEQ(RSA_sign(NID_rsaEncryption, NULL, 0, NULL, NULL, NULL), 0);
  52350. ExpectIntEQ(RSA_sign(NID_rsaEncryption, hash, sizeof(hash), signature,
  52351. &signatureLen, rsa), 0);
  52352. ExpectIntEQ(RSA_sign(NID_sha256, NULL, sizeof(hash), signature,
  52353. &signatureLen, rsa), 0);
  52354. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), NULL,
  52355. &signatureLen, rsa), 0);
  52356. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  52357. NULL, rsa), 0);
  52358. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  52359. &signatureLen, NULL), 0);
  52360. ExpectIntEQ(RSA_sign(NID_sha256, hash, sizeof(hash), signature,
  52361. &signatureLen, rsa), 1);
  52362. RSA_free(rsa);
  52363. der = pubDer;
  52364. rsa = NULL;
  52365. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  52366. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  52367. signatureLen, rsa), 1);
  52368. RSA_free(rsa);
  52369. #endif
  52370. return EXPECT_RESULT();
  52371. }
  52372. static int test_wolfSSL_RSA_sign_ex(void)
  52373. {
  52374. EXPECT_DECLS;
  52375. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52376. RSA *rsa = NULL;
  52377. unsigned char hash[SHA256_DIGEST_LENGTH];
  52378. #ifdef USE_CERT_BUFFERS_1024
  52379. const unsigned char* privDer = client_key_der_1024;
  52380. size_t privDerSz = sizeof_client_key_der_1024;
  52381. const unsigned char* pubDer = client_keypub_der_1024;
  52382. size_t pubDerSz = sizeof_client_keypub_der_1024;
  52383. unsigned char signature[1024/8];
  52384. #else
  52385. const unsigned char* privDer = client_key_der_2048;
  52386. size_t privDerSz = sizeof_client_key_der_2048;
  52387. const unsigned char* pubDer = client_keypub_der_2048;
  52388. size_t pubDerSz = sizeof_client_keypub_der_2048;
  52389. unsigned char signature[2048/8];
  52390. #endif
  52391. unsigned int signatureLen;
  52392. const unsigned char* der;
  52393. unsigned char encodedHash[51];
  52394. unsigned int encodedHashLen;
  52395. const unsigned char expEncHash[] = {
  52396. 0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86,
  52397. 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05,
  52398. 0x00, 0x04, 0x20,
  52399. /* Hash data */
  52400. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52401. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52402. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52403. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  52404. };
  52405. XMEMSET(hash, 0, sizeof(hash));
  52406. ExpectNotNull(rsa = wolfSSL_RSA_new());
  52407. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52408. &signatureLen, rsa, 1), 0);
  52409. wolfSSL_RSA_free(rsa);
  52410. der = privDer;
  52411. rsa = NULL;
  52412. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52413. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption,NULL, 0, NULL, NULL, NULL,
  52414. -1), 0);
  52415. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_rsaEncryption, hash, sizeof(hash),
  52416. signature, &signatureLen, rsa, 1), 0);
  52417. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  52418. &signatureLen, rsa, 1), 0);
  52419. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  52420. &signatureLen, rsa, 1), 0);
  52421. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52422. NULL, rsa, 1), 0);
  52423. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52424. &signatureLen, NULL, 1), 0);
  52425. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52426. &signatureLen, rsa, -1), 0);
  52427. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, NULL, sizeof(hash), signature,
  52428. &signatureLen, rsa, 0), 0);
  52429. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), NULL,
  52430. &signatureLen, rsa, 0), 0);
  52431. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52432. NULL, rsa, 0), 0);
  52433. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), signature,
  52434. &signatureLen, rsa, 1), 1);
  52435. /* Test returning encoded hash. */
  52436. ExpectIntEQ(wolfSSL_RSA_sign_ex(NID_sha256, hash, sizeof(hash), encodedHash,
  52437. &encodedHashLen, rsa, 0), 1);
  52438. ExpectIntEQ(encodedHashLen, sizeof(expEncHash));
  52439. ExpectIntEQ(XMEMCMP(encodedHash, expEncHash, sizeof(expEncHash)), 0);
  52440. RSA_free(rsa);
  52441. der = pubDer;
  52442. rsa = NULL;
  52443. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  52444. ExpectIntEQ(RSA_verify(NID_sha256, hash, sizeof(hash), signature,
  52445. signatureLen, rsa), 1);
  52446. RSA_free(rsa);
  52447. #endif
  52448. return EXPECT_RESULT();
  52449. }
  52450. static int test_wolfSSL_RSA_public_decrypt(void)
  52451. {
  52452. EXPECT_DECLS;
  52453. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52454. RSA *rsa;
  52455. unsigned char msg[SHA256_DIGEST_LENGTH];
  52456. #ifdef USE_CERT_BUFFERS_1024
  52457. const unsigned char* pubDer = client_keypub_der_1024;
  52458. size_t pubDerSz = sizeof_client_keypub_der_1024;
  52459. unsigned char decMsg[1024/8];
  52460. const unsigned char encMsg[] = {
  52461. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  52462. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  52463. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  52464. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  52465. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  52466. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  52467. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  52468. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  52469. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  52470. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  52471. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  52472. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  52473. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  52474. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  52475. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  52476. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  52477. };
  52478. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  52479. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  52480. defined(WC_RSA_NO_PADDING)
  52481. const unsigned char encMsgNoPad[] = {
  52482. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  52483. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  52484. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  52485. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  52486. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  52487. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  52488. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  52489. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  52490. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  52491. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  52492. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  52493. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  52494. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  52495. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  52496. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  52497. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  52498. };
  52499. #endif
  52500. #else
  52501. const unsigned char* pubDer = client_keypub_der_2048;
  52502. size_t pubDerSz = sizeof_client_keypub_der_2048;
  52503. unsigned char decMsg[2048/8];
  52504. const unsigned char encMsg[] = {
  52505. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  52506. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  52507. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  52508. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  52509. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  52510. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  52511. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  52512. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  52513. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  52514. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  52515. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  52516. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  52517. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  52518. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  52519. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  52520. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  52521. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  52522. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  52523. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  52524. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  52525. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  52526. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  52527. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  52528. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  52529. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  52530. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  52531. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  52532. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  52533. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  52534. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  52535. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  52536. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  52537. };
  52538. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  52539. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  52540. defined(WC_RSA_NO_PADDING)
  52541. const unsigned char encMsgNoPad[] = {
  52542. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  52543. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  52544. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  52545. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  52546. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  52547. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  52548. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  52549. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  52550. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  52551. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  52552. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  52553. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  52554. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  52555. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  52556. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  52557. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  52558. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  52559. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  52560. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  52561. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  52562. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  52563. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  52564. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  52565. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  52566. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  52567. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  52568. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  52569. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  52570. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  52571. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  52572. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  52573. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  52574. };
  52575. #endif
  52576. #endif
  52577. const unsigned char* der;
  52578. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  52579. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  52580. defined(WC_RSA_NO_PADDING)
  52581. int i;
  52582. #endif
  52583. XMEMSET(msg, 0, sizeof(msg));
  52584. der = pubDer;
  52585. rsa = NULL;
  52586. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  52587. ExpectIntEQ(RSA_public_decrypt(0, NULL, NULL, NULL, 0), -1);
  52588. ExpectIntEQ(RSA_public_decrypt(-1, encMsg, decMsg, rsa,
  52589. RSA_PKCS1_PADDING), -1);
  52590. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), NULL, decMsg, rsa,
  52591. RSA_PKCS1_PADDING), -1);
  52592. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  52593. RSA_PKCS1_PADDING), -1);
  52594. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, NULL,
  52595. RSA_PKCS1_PADDING), -1);
  52596. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  52597. RSA_PKCS1_PSS_PADDING), -1);
  52598. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsg), encMsg, decMsg, rsa,
  52599. RSA_PKCS1_PADDING), 32);
  52600. ExpectIntEQ(XMEMCMP(decMsg, msg, sizeof(msg)), 0);
  52601. #if !defined(HAVE_SELFTEST) && (!defined(HAVE_FIPS) || \
  52602. (defined(HAVE_FIPS_VERSION) && HAVE_FIPS_VERSION > 2)) && \
  52603. defined(WC_RSA_NO_PADDING)
  52604. ExpectIntEQ(RSA_public_decrypt(sizeof(encMsgNoPad), encMsgNoPad, decMsg,
  52605. rsa, RSA_NO_PADDING), sizeof(decMsg));
  52606. /* Zeros before actual data. */
  52607. for (i = 0; i < (int)(sizeof(decMsg) - sizeof(msg)); i += sizeof(msg)) {
  52608. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  52609. }
  52610. /* Check actual data. */
  52611. XMEMSET(msg, 0x01, sizeof(msg));
  52612. ExpectIntEQ(XMEMCMP(decMsg + i, msg, sizeof(msg)), 0);
  52613. #endif
  52614. RSA_free(rsa);
  52615. #endif
  52616. return EXPECT_RESULT();
  52617. }
  52618. static int test_wolfSSL_RSA_private_encrypt(void)
  52619. {
  52620. EXPECT_DECLS;
  52621. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52622. RSA *rsa;
  52623. unsigned char msg[SHA256_DIGEST_LENGTH];
  52624. #ifdef USE_CERT_BUFFERS_1024
  52625. const unsigned char* privDer = client_key_der_1024;
  52626. size_t privDerSz = sizeof_client_key_der_1024;
  52627. unsigned char encMsg[1024/8];
  52628. const unsigned char expEncMsg[] = {
  52629. 0x45, 0x8e, 0x6e, 0x7a, 0x9c, 0xe1, 0x67, 0x36,
  52630. 0x72, 0xfc, 0x9d, 0x05, 0xdf, 0xc2, 0xaf, 0x54,
  52631. 0xc5, 0x2f, 0x94, 0xb8, 0xc7, 0x82, 0x40, 0xfa,
  52632. 0xa7, 0x8c, 0xb1, 0x89, 0x40, 0xc3, 0x59, 0x5a,
  52633. 0x77, 0x08, 0x54, 0x93, 0x43, 0x7f, 0xc4, 0xb7,
  52634. 0xc4, 0x78, 0xf1, 0xf8, 0xab, 0xbf, 0xc2, 0x81,
  52635. 0x5d, 0x97, 0xea, 0x7a, 0x60, 0x90, 0x51, 0xb7,
  52636. 0x47, 0x78, 0x48, 0x1e, 0x88, 0x6b, 0x89, 0xde,
  52637. 0xce, 0x41, 0x41, 0xae, 0x49, 0xf6, 0xfd, 0x2d,
  52638. 0x2d, 0x9c, 0x70, 0x7d, 0xf9, 0xcf, 0x77, 0x5f,
  52639. 0x06, 0xc7, 0x20, 0xe3, 0x57, 0xd4, 0xd8, 0x1a,
  52640. 0x96, 0xa2, 0x39, 0xb0, 0x6e, 0x8e, 0x68, 0xf8,
  52641. 0x57, 0x7b, 0x26, 0x88, 0x17, 0xc4, 0xb7, 0xf1,
  52642. 0x59, 0xfa, 0xb6, 0x95, 0xdd, 0x1e, 0xe8, 0xd8,
  52643. 0x4e, 0xbd, 0xcd, 0x41, 0xad, 0xc7, 0xe2, 0x39,
  52644. 0xb8, 0x00, 0xca, 0xf5, 0x59, 0xdf, 0xf8, 0x43
  52645. };
  52646. #ifdef WC_RSA_NO_PADDING
  52647. const unsigned char expEncMsgNoPad[] = {
  52648. 0x0d, 0x41, 0x5a, 0xc7, 0x60, 0xd7, 0xbe, 0xb6,
  52649. 0x42, 0xd1, 0x65, 0xb1, 0x7e, 0x59, 0x54, 0xcc,
  52650. 0x76, 0x62, 0xd0, 0x2f, 0x4d, 0xe3, 0x23, 0x62,
  52651. 0xc8, 0x14, 0xfe, 0x5e, 0xa1, 0xc7, 0x05, 0xee,
  52652. 0x9e, 0x28, 0x2e, 0xf5, 0xfd, 0xa4, 0xc0, 0x43,
  52653. 0x55, 0xa2, 0x6b, 0x6b, 0x16, 0xa7, 0x63, 0x06,
  52654. 0xa7, 0x78, 0x4f, 0xda, 0xae, 0x10, 0x6d, 0xd1,
  52655. 0x2e, 0x1d, 0xbb, 0xbc, 0xc4, 0x1d, 0x82, 0xe4,
  52656. 0xc6, 0x76, 0x77, 0xa6, 0x0a, 0xef, 0xd2, 0x89,
  52657. 0xff, 0x30, 0x85, 0x22, 0xa0, 0x68, 0x88, 0x54,
  52658. 0xa3, 0xd1, 0x92, 0xd1, 0x3f, 0x57, 0xe4, 0xc7,
  52659. 0x43, 0x5a, 0x8b, 0xb3, 0x86, 0xaf, 0xd5, 0x6d,
  52660. 0x07, 0xe1, 0xa0, 0x5f, 0xe1, 0x9a, 0x06, 0xba,
  52661. 0x56, 0xd2, 0xb0, 0x73, 0xf5, 0xb3, 0xd0, 0x5f,
  52662. 0xc0, 0xbf, 0x22, 0x4c, 0x54, 0x4e, 0x11, 0xe2,
  52663. 0xc5, 0xf8, 0x66, 0x39, 0x9d, 0x70, 0x90, 0x31
  52664. };
  52665. #endif
  52666. #else
  52667. const unsigned char* privDer = client_key_der_2048;
  52668. size_t privDerSz = sizeof_client_key_der_2048;
  52669. unsigned char encMsg[2048/8];
  52670. const unsigned char expEncMsg[] = {
  52671. 0x16, 0x5d, 0xbb, 0x00, 0x38, 0x73, 0x01, 0x34,
  52672. 0xca, 0x59, 0xc6, 0x8b, 0x64, 0x70, 0x89, 0xf5,
  52673. 0x50, 0x2d, 0x1d, 0x69, 0x1f, 0x07, 0x1e, 0x31,
  52674. 0xae, 0x9b, 0xa6, 0x6e, 0xee, 0x80, 0xd9, 0x9e,
  52675. 0x59, 0x33, 0x70, 0x30, 0x28, 0x42, 0x7d, 0x24,
  52676. 0x36, 0x95, 0x6b, 0xf9, 0x0a, 0x23, 0xcb, 0xce,
  52677. 0x66, 0xa5, 0x07, 0x5e, 0x11, 0xa7, 0xdc, 0xfb,
  52678. 0xd9, 0xc2, 0x51, 0xf0, 0x05, 0xc9, 0x39, 0xb3,
  52679. 0xae, 0xff, 0xfb, 0xe9, 0xb1, 0x9a, 0x54, 0xac,
  52680. 0x1d, 0xca, 0x42, 0x1a, 0xfd, 0x7c, 0x97, 0xa0,
  52681. 0x60, 0x2b, 0xcd, 0xb6, 0x36, 0x33, 0xfc, 0x44,
  52682. 0x69, 0xf7, 0x2e, 0x8c, 0x3b, 0x5f, 0xb4, 0x9f,
  52683. 0xa7, 0x02, 0x8f, 0x6d, 0x6b, 0x79, 0x10, 0x32,
  52684. 0x7d, 0xf4, 0x5d, 0xa1, 0x63, 0x22, 0x59, 0xc4,
  52685. 0x44, 0x8e, 0x44, 0x24, 0x8b, 0x14, 0x9d, 0x2b,
  52686. 0xb5, 0xd3, 0xad, 0x9a, 0x87, 0x0d, 0xe7, 0x70,
  52687. 0x6d, 0xe9, 0xae, 0xaa, 0x52, 0xbf, 0x1a, 0x9b,
  52688. 0xc8, 0x3d, 0x45, 0x7c, 0xd1, 0x90, 0xe3, 0xd9,
  52689. 0x57, 0xcf, 0xc3, 0x29, 0x69, 0x05, 0x07, 0x96,
  52690. 0x2e, 0x46, 0x74, 0x0a, 0xa7, 0x76, 0x8b, 0xc0,
  52691. 0x1c, 0x04, 0x80, 0x08, 0xa0, 0x94, 0x7e, 0xbb,
  52692. 0x2d, 0x99, 0xe9, 0xab, 0x18, 0x4d, 0x48, 0x2d,
  52693. 0x94, 0x5e, 0x50, 0x21, 0x42, 0xdf, 0xf5, 0x61,
  52694. 0x42, 0x7d, 0x86, 0x5d, 0x9e, 0x89, 0xc9, 0x5b,
  52695. 0x24, 0xab, 0xa1, 0xd8, 0x20, 0x45, 0xcb, 0x81,
  52696. 0xcf, 0xc5, 0x25, 0x7d, 0x11, 0x6e, 0xbd, 0x80,
  52697. 0xac, 0xba, 0xdc, 0xef, 0xb9, 0x05, 0x9c, 0xd5,
  52698. 0xc2, 0x26, 0x57, 0x69, 0x8b, 0x08, 0x27, 0xc7,
  52699. 0xea, 0xbe, 0xaf, 0x52, 0x21, 0x95, 0x9f, 0xa0,
  52700. 0x2f, 0x2f, 0x53, 0x7c, 0x2f, 0xa3, 0x0b, 0x79,
  52701. 0x39, 0x01, 0xa3, 0x37, 0x46, 0xa8, 0xc4, 0x34,
  52702. 0x41, 0x20, 0x7c, 0x3f, 0x70, 0x9a, 0x47, 0xe8
  52703. };
  52704. #ifdef WC_RSA_NO_PADDING
  52705. const unsigned char expEncMsgNoPad[] = {
  52706. 0x79, 0x69, 0xdc, 0x0d, 0xff, 0x09, 0xeb, 0x91,
  52707. 0xbc, 0xda, 0xe4, 0xd3, 0xcd, 0xd5, 0xd3, 0x1c,
  52708. 0xb9, 0x66, 0xa8, 0x02, 0xf3, 0x75, 0x40, 0xf1,
  52709. 0x38, 0x4a, 0x37, 0x7b, 0x19, 0xc8, 0xcd, 0xea,
  52710. 0x79, 0xa8, 0x51, 0x32, 0x00, 0x3f, 0x4c, 0xde,
  52711. 0xaa, 0xe5, 0xe2, 0x7c, 0x10, 0xcd, 0x6e, 0x00,
  52712. 0xc6, 0xc4, 0x63, 0x98, 0x58, 0x9b, 0x38, 0xca,
  52713. 0xf0, 0x5d, 0xc8, 0xf0, 0x57, 0xf6, 0x21, 0x50,
  52714. 0x3f, 0x63, 0x05, 0x9f, 0xbf, 0xb6, 0x3b, 0x50,
  52715. 0x85, 0x06, 0x34, 0x08, 0x57, 0xb9, 0x44, 0xce,
  52716. 0xe4, 0x66, 0xbf, 0x0c, 0xfe, 0x36, 0xa4, 0x5b,
  52717. 0xed, 0x2d, 0x7d, 0xed, 0xf1, 0xbd, 0xda, 0x3e,
  52718. 0x19, 0x1f, 0x99, 0xc8, 0xe4, 0xc2, 0xbb, 0xb5,
  52719. 0x6c, 0x83, 0x22, 0xd1, 0xe7, 0x57, 0xcf, 0x1b,
  52720. 0x91, 0x0c, 0xa5, 0x47, 0x06, 0x71, 0x8f, 0x93,
  52721. 0xf3, 0xad, 0xdb, 0xe3, 0xf8, 0xa0, 0x0b, 0xcd,
  52722. 0x89, 0x4e, 0xa5, 0xb5, 0x03, 0x68, 0x61, 0x89,
  52723. 0x0b, 0xe2, 0x03, 0x8b, 0x1f, 0x54, 0xae, 0x0f,
  52724. 0xfa, 0xf0, 0xb7, 0x0f, 0x8c, 0x84, 0x35, 0x13,
  52725. 0x8d, 0x65, 0x1f, 0x2c, 0xd5, 0xce, 0xc4, 0x6c,
  52726. 0x98, 0x67, 0xe4, 0x1a, 0x85, 0x67, 0x69, 0x17,
  52727. 0x17, 0x5a, 0x5d, 0xfd, 0x23, 0xdd, 0x03, 0x3f,
  52728. 0x6d, 0x7a, 0xb6, 0x8b, 0x99, 0xc0, 0xb6, 0x70,
  52729. 0x86, 0xac, 0xf6, 0x02, 0xc2, 0x28, 0x42, 0xed,
  52730. 0x06, 0xcf, 0xca, 0x3d, 0x07, 0x16, 0xf0, 0x0e,
  52731. 0x04, 0x55, 0x1e, 0x59, 0x3f, 0x32, 0xc7, 0x12,
  52732. 0xc5, 0x0d, 0x9d, 0x64, 0x7d, 0x2e, 0xd4, 0xbc,
  52733. 0x8c, 0x24, 0x42, 0x94, 0x2b, 0xf6, 0x11, 0x7f,
  52734. 0xb1, 0x1c, 0x09, 0x12, 0x6f, 0x5e, 0x2e, 0x7a,
  52735. 0xc6, 0x01, 0xe0, 0x98, 0x31, 0xb7, 0x13, 0x03,
  52736. 0xce, 0x29, 0xe1, 0xef, 0x9d, 0xdf, 0x9b, 0xa5,
  52737. 0xba, 0x0b, 0xad, 0xf2, 0xeb, 0x2f, 0xf9, 0xd1
  52738. };
  52739. #endif
  52740. #endif
  52741. const unsigned char* der;
  52742. XMEMSET(msg, 0x00, sizeof(msg));
  52743. der = privDer;
  52744. rsa = NULL;
  52745. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52746. ExpectIntEQ(RSA_private_encrypt(0, NULL, NULL, NULL, 0), -1);
  52747. ExpectIntEQ(RSA_private_encrypt(0, msg, encMsg, rsa, RSA_PKCS1_PADDING),
  52748. -1);
  52749. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), NULL, encMsg, rsa,
  52750. RSA_PKCS1_PADDING), -1);
  52751. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, NULL, rsa,
  52752. RSA_PKCS1_PADDING), -1);
  52753. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, NULL,
  52754. RSA_PKCS1_PADDING), -1);
  52755. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  52756. RSA_PKCS1_PSS_PADDING), -1);
  52757. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  52758. RSA_PKCS1_PADDING), sizeof(encMsg));
  52759. ExpectIntEQ(XMEMCMP(encMsg, expEncMsg, sizeof(expEncMsg)), 0);
  52760. #ifdef WC_RSA_NO_PADDING
  52761. /* Non-zero message. */
  52762. XMEMSET(msg, 0x01, sizeof(msg));
  52763. ExpectIntEQ(RSA_private_encrypt(sizeof(msg), msg, encMsg, rsa,
  52764. RSA_NO_PADDING), sizeof(encMsg));
  52765. ExpectIntEQ(XMEMCMP(encMsg, expEncMsgNoPad, sizeof(expEncMsgNoPad)), 0);
  52766. #endif
  52767. RSA_free(rsa);
  52768. #endif
  52769. return EXPECT_RESULT();
  52770. }
  52771. static int test_wolfSSL_RSA_public_encrypt(void)
  52772. {
  52773. EXPECT_DECLS;
  52774. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52775. RSA* rsa = NULL;
  52776. const unsigned char msg[2048/8] = { 0 };
  52777. unsigned char encMsg[2048/8];
  52778. ExpectNotNull(rsa = RSA_new());
  52779. ExpectIntEQ(RSA_public_encrypt(-1, msg, encMsg, rsa,
  52780. RSA_PKCS1_PADDING), -1);
  52781. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), NULL, encMsg, rsa,
  52782. RSA_PKCS1_PADDING), -1);
  52783. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, NULL, rsa,
  52784. RSA_PKCS1_PADDING), -1);
  52785. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, NULL,
  52786. RSA_PKCS1_PADDING), -1);
  52787. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  52788. RSA_PKCS1_PSS_PADDING), -1);
  52789. /* Empty RSA key. */
  52790. ExpectIntEQ(RSA_public_encrypt(sizeof(msg), msg, encMsg, rsa,
  52791. RSA_PKCS1_PADDING), -1);
  52792. RSA_free(rsa);
  52793. #endif
  52794. return EXPECT_RESULT();
  52795. }
  52796. static int test_wolfSSL_RSA_private_decrypt(void)
  52797. {
  52798. EXPECT_DECLS;
  52799. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52800. RSA* rsa = NULL;
  52801. unsigned char msg[2048/8];
  52802. const unsigned char encMsg[2048/8] = { 0 };
  52803. ExpectNotNull(rsa = RSA_new());
  52804. ExpectIntEQ(RSA_private_decrypt(-1, encMsg, msg, rsa,
  52805. RSA_PKCS1_PADDING), -1);
  52806. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), NULL, msg, rsa,
  52807. RSA_PKCS1_PADDING), -1);
  52808. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, NULL, rsa,
  52809. RSA_PKCS1_PADDING), -1);
  52810. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, NULL,
  52811. RSA_PKCS1_PADDING), -1);
  52812. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  52813. RSA_PKCS1_PSS_PADDING), -1);
  52814. /* Empty RSA key. */
  52815. ExpectIntEQ(RSA_private_decrypt(sizeof(encMsg), encMsg, msg, rsa,
  52816. RSA_PKCS1_PADDING), -1);
  52817. RSA_free(rsa);
  52818. #endif
  52819. return EXPECT_RESULT();
  52820. }
  52821. static int test_wolfSSL_RSA_GenAdd(void)
  52822. {
  52823. EXPECT_DECLS;
  52824. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52825. RSA *rsa;
  52826. #ifdef USE_CERT_BUFFERS_1024
  52827. const unsigned char* privDer = client_key_der_1024;
  52828. size_t privDerSz = sizeof_client_key_der_1024;
  52829. const unsigned char* pubDer = client_keypub_der_1024;
  52830. size_t pubDerSz = sizeof_client_keypub_der_1024;
  52831. #else
  52832. const unsigned char* privDer = client_key_der_2048;
  52833. size_t privDerSz = sizeof_client_key_der_2048;
  52834. const unsigned char* pubDer = client_keypub_der_2048;
  52835. size_t pubDerSz = sizeof_client_keypub_der_2048;
  52836. #endif
  52837. const unsigned char* der;
  52838. der = privDer;
  52839. rsa = NULL;
  52840. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52841. ExpectIntEQ(wolfSSL_RSA_GenAdd(NULL), -1);
  52842. #ifndef RSA_LOW_MEM
  52843. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), 1);
  52844. #else
  52845. /* dmp1 and dmq1 are not set (allocated) when RSA_LOW_MEM. */
  52846. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  52847. #endif
  52848. RSA_free(rsa);
  52849. der = pubDer;
  52850. rsa = NULL;
  52851. ExpectNotNull(d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  52852. /* Need private values. */
  52853. ExpectIntEQ(wolfSSL_RSA_GenAdd(rsa), -1);
  52854. RSA_free(rsa);
  52855. #endif
  52856. return EXPECT_RESULT();
  52857. }
  52858. static int test_wolfSSL_RSA_blinding_on(void)
  52859. {
  52860. EXPECT_DECLS;
  52861. #if defined(OPENSSL_EXTRA) && !defined(NO_RSA) && !defined(NO_WOLFSSL_STUB)
  52862. RSA *rsa;
  52863. WOLFSSL_BN_CTX *bnCtx = NULL;
  52864. #ifdef USE_CERT_BUFFERS_1024
  52865. const unsigned char* privDer = client_key_der_1024;
  52866. size_t privDerSz = sizeof_client_key_der_1024;
  52867. #else
  52868. const unsigned char* privDer = client_key_der_2048;
  52869. size_t privDerSz = sizeof_client_key_der_2048;
  52870. #endif
  52871. const unsigned char* der;
  52872. der = privDer;
  52873. rsa = NULL;
  52874. ExpectNotNull(d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52875. ExpectNotNull(bnCtx = wolfSSL_BN_CTX_new());
  52876. /* Does nothing so all parameters are valid. */
  52877. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, NULL), 1);
  52878. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, NULL), 1);
  52879. ExpectIntEQ(wolfSSL_RSA_blinding_on(NULL, bnCtx), 1);
  52880. ExpectIntEQ(wolfSSL_RSA_blinding_on(rsa, bnCtx), 1);
  52881. wolfSSL_BN_CTX_free(bnCtx);
  52882. RSA_free(rsa);
  52883. #endif
  52884. return EXPECT_RESULT();
  52885. }
  52886. static int test_wolfSSL_RSA_ex_data(void)
  52887. {
  52888. EXPECT_DECLS;
  52889. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA)
  52890. RSA* rsa = NULL;
  52891. unsigned char data[1];
  52892. ExpectNotNull(rsa = RSA_new());
  52893. ExpectNull(wolfSSL_RSA_get_ex_data(NULL, 0));
  52894. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  52895. #ifdef MAX_EX_DATA
  52896. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, MAX_EX_DATA));
  52897. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, MAX_EX_DATA, data), 0);
  52898. #endif
  52899. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, NULL), 0);
  52900. ExpectIntEQ(wolfSSL_RSA_set_ex_data(NULL, 0, data), 0);
  52901. #ifdef HAVE_EX_DATA
  52902. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 1);
  52903. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 1);
  52904. ExpectPtrEq(wolfSSL_RSA_get_ex_data(rsa, 0), data);
  52905. #else
  52906. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, NULL), 0);
  52907. ExpectIntEQ(wolfSSL_RSA_set_ex_data(rsa, 0, data), 0);
  52908. ExpectNull(wolfSSL_RSA_get_ex_data(rsa, 0));
  52909. #endif
  52910. RSA_free(rsa);
  52911. #endif /* !NO_RSA && OPENSSL_EXTRA */
  52912. return EXPECT_RESULT();
  52913. }
  52914. static int test_wolfSSL_RSA_LoadDer(void)
  52915. {
  52916. EXPECT_DECLS;
  52917. #if !defined(NO_RSA) && (defined(OPENSSL_EXTRA) || \
  52918. defined(OPENSSL_EXTRA_X509_SMALL))
  52919. RSA *rsa = NULL;
  52920. #ifdef USE_CERT_BUFFERS_1024
  52921. const unsigned char* privDer = client_key_der_1024;
  52922. size_t privDerSz = sizeof_client_key_der_1024;
  52923. #else
  52924. const unsigned char* privDer = client_key_der_2048;
  52925. size_t privDerSz = sizeof_client_key_der_2048;
  52926. #endif
  52927. ExpectNotNull(rsa = RSA_new());
  52928. ExpectIntEQ(wolfSSL_RSA_LoadDer(NULL, privDer, (int)privDerSz), -1);
  52929. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, NULL, (int)privDerSz), -1);
  52930. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, 0), -1);
  52931. ExpectIntEQ(wolfSSL_RSA_LoadDer(rsa, privDer, (int)privDerSz), 1);
  52932. RSA_free(rsa);
  52933. #endif /* !NO_RSA && OPENSSL_EXTRA */
  52934. return EXPECT_RESULT();
  52935. }
  52936. /* Local API. */
  52937. static int test_wolfSSL_RSA_To_Der(void)
  52938. {
  52939. EXPECT_DECLS;
  52940. #ifdef WOLFSSL_TEST_STATIC_BUILD
  52941. #if defined(WOLFSSL_KEY_GEN) && defined(OPENSSL_EXTRA) && !defined(NO_RSA)
  52942. RSA* rsa;
  52943. #ifdef USE_CERT_BUFFERS_1024
  52944. const unsigned char* privDer = client_key_der_1024;
  52945. size_t privDerSz = sizeof_client_key_der_1024;
  52946. const unsigned char* pubDer = client_keypub_der_1024;
  52947. size_t pubDerSz = sizeof_client_keypub_der_1024;
  52948. unsigned char out[sizeof(client_key_der_1024)];
  52949. #else
  52950. const unsigned char* privDer = client_key_der_2048;
  52951. size_t privDerSz = sizeof_client_key_der_2048;
  52952. const unsigned char* pubDer = client_keypub_der_2048;
  52953. size_t pubDerSz = sizeof_client_keypub_der_2048;
  52954. unsigned char out[sizeof(client_key_der_2048)];
  52955. #endif
  52956. const unsigned char* der;
  52957. unsigned char* outDer = NULL;
  52958. der = privDer;
  52959. rsa = NULL;
  52960. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  52961. ExpectIntEQ(wolfSSL_RSA_To_Der(NULL, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  52962. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 2, HEAP_HINT), BAD_FUNC_ARG);
  52963. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 0, HEAP_HINT), privDerSz);
  52964. outDer = out;
  52965. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  52966. ExpectIntEQ(XMEMCMP(out, privDer, privDerSz), 0);
  52967. outDer = NULL;
  52968. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), privDerSz);
  52969. ExpectNotNull(outDer);
  52970. ExpectIntEQ(XMEMCMP(outDer, privDer, privDerSz), 0);
  52971. XFREE(outDer, HEAP_HINT, DYNAMIC_TYPE_TMP_BUFFER);
  52972. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, NULL, 1, HEAP_HINT), pubDerSz);
  52973. outDer = out;
  52974. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), pubDerSz);
  52975. ExpectIntEQ(XMEMCMP(out, pubDer, pubDerSz), 0);
  52976. RSA_free(rsa);
  52977. ExpectNotNull(rsa = RSA_new());
  52978. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  52979. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 1, HEAP_HINT), BAD_FUNC_ARG);
  52980. RSA_free(rsa);
  52981. der = pubDer;
  52982. rsa = NULL;
  52983. ExpectNotNull(wolfSSL_d2i_RSAPublicKey(&rsa, &der, pubDerSz));
  52984. ExpectIntEQ(wolfSSL_RSA_To_Der(rsa, &outDer, 0, HEAP_HINT), BAD_FUNC_ARG);
  52985. RSA_free(rsa);
  52986. #endif
  52987. #endif
  52988. return EXPECT_RESULT();
  52989. }
  52990. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  52991. static int test_wolfSSL_PEM_read_RSAPublicKey(void)
  52992. {
  52993. EXPECT_DECLS;
  52994. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM)
  52995. XFILE file = XBADFILE;
  52996. const char* fname = "./certs/server-keyPub.pem";
  52997. RSA *rsa = NULL;
  52998. ExpectNull(wolfSSL_PEM_read_RSAPublicKey(XBADFILE, NULL, NULL, NULL));
  52999. ExpectTrue((file = XFOPEN(fname, "rb")) != XBADFILE);
  53000. ExpectNotNull(rsa = PEM_read_RSA_PUBKEY(file, NULL, NULL, NULL));
  53001. ExpectIntEQ(RSA_size(rsa), 256);
  53002. RSA_free(rsa);
  53003. if (file != XBADFILE)
  53004. XFCLOSE(file);
  53005. #endif
  53006. return EXPECT_RESULT();
  53007. }
  53008. /* wolfSSL_PEM_read_RSAPublicKey is a stub function. */
  53009. static int test_wolfSSL_PEM_write_RSA_PUBKEY(void)
  53010. {
  53011. EXPECT_DECLS;
  53012. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && !defined(NO_FILESYSTEM) && \
  53013. defined(WOLFSSL_KEY_GEN)
  53014. RSA* rsa = NULL;
  53015. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(XBADFILE, NULL), 0);
  53016. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, NULL), 0);
  53017. /* Valid but stub so returns 0. */
  53018. ExpectIntEQ(wolfSSL_PEM_write_RSA_PUBKEY(stderr, rsa), 0);
  53019. #endif
  53020. return EXPECT_RESULT();
  53021. }
  53022. static int test_wolfSSL_PEM_write_RSAPrivateKey(void)
  53023. {
  53024. EXPECT_DECLS;
  53025. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  53026. (defined(WOLFSSL_PEM_TO_DER) || \
  53027. defined(WOLFSSL_DER_TO_PEM)) && !defined(NO_FILESYSTEM)
  53028. RSA* rsa = NULL;
  53029. #ifdef USE_CERT_BUFFERS_1024
  53030. const unsigned char* privDer = client_key_der_1024;
  53031. size_t privDerSz = sizeof_client_key_der_1024;
  53032. #else
  53033. const unsigned char* privDer = client_key_der_2048;
  53034. size_t privDerSz = sizeof_client_key_der_2048;
  53035. #endif
  53036. const unsigned char* der;
  53037. #ifndef NO_AES
  53038. unsigned char passwd[] = "password";
  53039. #endif
  53040. ExpectNotNull(rsa = RSA_new());
  53041. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  53042. NULL, NULL), 0);
  53043. RSA_free(rsa);
  53044. der = privDer;
  53045. rsa = NULL;
  53046. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  53047. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(XBADFILE, rsa, NULL, NULL, 0,
  53048. NULL, NULL), 0);
  53049. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, NULL, NULL, NULL, 0,
  53050. NULL, NULL), 0);
  53051. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, NULL, NULL, 0,
  53052. NULL, NULL), 1);
  53053. #ifndef NO_AES
  53054. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  53055. NULL, 0, NULL, NULL), 1);
  53056. ExpectIntEQ(wolfSSL_PEM_write_RSAPrivateKey(stderr, rsa, EVP_aes_128_cbc(),
  53057. passwd, sizeof(passwd) - 1, NULL, NULL), 1);
  53058. #endif
  53059. RSA_free(rsa);
  53060. #endif
  53061. return EXPECT_RESULT();
  53062. }
  53063. static int test_wolfSSL_PEM_write_mem_RSAPrivateKey(void)
  53064. {
  53065. EXPECT_DECLS;
  53066. #if !defined(NO_RSA) && defined(OPENSSL_EXTRA) && defined(WOLFSSL_KEY_GEN) && \
  53067. (defined(WOLFSSL_PEM_TO_DER) || defined(WOLFSSL_DER_TO_PEM))
  53068. RSA* rsa = NULL;
  53069. #ifdef USE_CERT_BUFFERS_1024
  53070. const unsigned char* privDer = client_key_der_1024;
  53071. size_t privDerSz = sizeof_client_key_der_1024;
  53072. #else
  53073. const unsigned char* privDer = client_key_der_2048;
  53074. size_t privDerSz = sizeof_client_key_der_2048;
  53075. #endif
  53076. const unsigned char* der;
  53077. #ifndef NO_AES
  53078. unsigned char passwd[] = "password";
  53079. #endif
  53080. unsigned char* pem = NULL;
  53081. int plen;
  53082. ExpectNotNull(rsa = RSA_new());
  53083. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  53084. &plen), 0);
  53085. RSA_free(rsa);
  53086. der = privDer;
  53087. rsa = NULL;
  53088. ExpectNotNull(wolfSSL_d2i_RSAPrivateKey(&rsa, &der, privDerSz));
  53089. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(NULL, NULL, NULL, 0, &pem,
  53090. &plen), 0);
  53091. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, NULL,
  53092. &plen), 0);
  53093. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  53094. NULL), 0);
  53095. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, NULL, NULL, 0, &pem,
  53096. &plen), 1);
  53097. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  53098. pem = NULL;
  53099. #ifndef NO_AES
  53100. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  53101. NULL, 0, &pem, &plen), 1);
  53102. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  53103. pem = NULL;
  53104. ExpectIntEQ(wolfSSL_PEM_write_mem_RSAPrivateKey(rsa, EVP_aes_128_cbc(),
  53105. passwd, sizeof(passwd) - 1, &pem, &plen), 1);
  53106. XFREE(pem, NULL, DYNAMIC_TYPE_KEY);
  53107. #endif
  53108. RSA_free(rsa);
  53109. #endif
  53110. return EXPECT_RESULT();
  53111. }
  53112. static int test_wolfSSL_DH(void)
  53113. {
  53114. EXPECT_DECLS;
  53115. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  53116. DH *dh = NULL;
  53117. BIGNUM* p;
  53118. BIGNUM* q;
  53119. BIGNUM* g;
  53120. BIGNUM* pub = NULL;
  53121. BIGNUM* priv = NULL;
  53122. #if defined(OPENSSL_ALL)
  53123. #if !defined(HAVE_FIPS) || \
  53124. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  53125. FILE* f = NULL;
  53126. unsigned char buf[268];
  53127. const unsigned char* pt = buf;
  53128. long len = 0;
  53129. dh = NULL;
  53130. XMEMSET(buf, 0, sizeof(buf));
  53131. /* Test 2048 bit parameters */
  53132. ExpectTrue((f = XFOPEN("./certs/dh2048.der", "rb")) != XBADFILE);
  53133. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  53134. if (f != XBADFILE)
  53135. XFCLOSE(f);
  53136. ExpectNotNull(dh = d2i_DHparams(NULL, &pt, len));
  53137. ExpectNotNull(dh->p);
  53138. ExpectNotNull(dh->g);
  53139. ExpectTrue(pt == buf);
  53140. ExpectIntEQ(DH_generate_key(dh), 1);
  53141. ExpectIntEQ(DH_generate_key(dh), 1);
  53142. ExpectIntEQ(DH_compute_key(NULL, NULL, NULL), -1);
  53143. ExpectNotNull(pub = BN_new());
  53144. ExpectIntEQ(BN_set_word(pub, 1), 1);
  53145. ExpectIntEQ(DH_compute_key(buf, NULL, NULL), -1);
  53146. ExpectIntEQ(DH_compute_key(NULL, pub, NULL), -1);
  53147. ExpectIntEQ(DH_compute_key(NULL, NULL, dh), -1);
  53148. ExpectIntEQ(DH_compute_key(buf, pub, NULL), -1);
  53149. ExpectIntEQ(DH_compute_key(buf, NULL, dh), -1);
  53150. ExpectIntEQ(DH_compute_key(NULL, pub, dh), -1);
  53151. ExpectIntEQ(DH_compute_key(buf, pub, dh), -1);
  53152. BN_free(pub);
  53153. pub = NULL;
  53154. DH_get0_pqg(dh, (const BIGNUM**)&p,
  53155. (const BIGNUM**)&q,
  53156. (const BIGNUM**)&g);
  53157. ExpectPtrEq(p, dh->p);
  53158. ExpectPtrEq(q, dh->q);
  53159. ExpectPtrEq(g, dh->g);
  53160. DH_get0_key(NULL, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  53161. DH_get0_key(dh, (const BIGNUM**)&pub, (const BIGNUM**)&priv);
  53162. ExpectPtrEq(pub, dh->pub_key);
  53163. ExpectPtrEq(priv, dh->priv_key);
  53164. DH_get0_key(dh, (const BIGNUM**)&pub, NULL);
  53165. ExpectPtrEq(pub, dh->pub_key);
  53166. DH_get0_key(dh, NULL, (const BIGNUM**)&priv);
  53167. ExpectPtrEq(priv, dh->priv_key);
  53168. pub = NULL;
  53169. priv = NULL;
  53170. ExpectNotNull(pub = BN_new());
  53171. ExpectNotNull(priv = BN_new());
  53172. ExpectIntEQ(DH_set0_key(NULL, pub, priv), 0);
  53173. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  53174. if (EXPECT_FAIL()) {
  53175. BN_free(pub);
  53176. BN_free(priv);
  53177. }
  53178. pub = NULL;
  53179. priv = NULL;
  53180. ExpectNotNull(pub = BN_new());
  53181. ExpectIntEQ(DH_set0_key(dh, pub, NULL), 1);
  53182. if (EXPECT_FAIL()) {
  53183. BN_free(pub);
  53184. }
  53185. ExpectNotNull(priv = BN_new());
  53186. ExpectIntEQ(DH_set0_key(dh, NULL, priv), 1);
  53187. if (EXPECT_FAIL()) {
  53188. BN_free(priv);
  53189. }
  53190. ExpectPtrEq(pub, dh->pub_key);
  53191. ExpectPtrEq(priv, dh->priv_key);
  53192. pub = NULL;
  53193. priv = NULL;
  53194. DH_free(dh);
  53195. dh = NULL;
  53196. ExpectNotNull(dh = DH_new());
  53197. p = NULL;
  53198. ExpectNotNull(p = BN_new());
  53199. ExpectIntEQ(BN_set_word(p, 1), 1);
  53200. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  53201. ExpectNotNull(pub = BN_new());
  53202. ExpectNotNull(priv = BN_new());
  53203. ExpectIntEQ(DH_set0_key(dh, pub, priv), 1);
  53204. if (EXPECT_FAIL()) {
  53205. BN_free(pub);
  53206. BN_free(priv);
  53207. }
  53208. pub = NULL;
  53209. priv = NULL;
  53210. ExpectIntEQ(DH_compute_key(buf, p, dh), -1);
  53211. BN_free(p);
  53212. p = NULL;
  53213. DH_free(dh);
  53214. dh = NULL;
  53215. #ifdef WOLFSSL_KEY_GEN
  53216. ExpectNotNull(dh = DH_generate_parameters(2048, 2, NULL, NULL));
  53217. ExpectIntEQ(wolfSSL_DH_generate_parameters_ex(NULL, 2048, 2, NULL), 0);
  53218. DH_free(dh);
  53219. dh = NULL;
  53220. #endif
  53221. #endif /* !HAVE_FIPS || (HAVE_FIPS_VERSION && HAVE_FIPS_VERSION > 2) */
  53222. #endif /* OPENSSL_ALL */
  53223. (void)dh;
  53224. (void)p;
  53225. (void)q;
  53226. (void)g;
  53227. (void)pub;
  53228. (void)priv;
  53229. ExpectNotNull(dh = wolfSSL_DH_new());
  53230. /* invalid parameters test */
  53231. DH_get0_pqg(NULL, (const BIGNUM**)&p,
  53232. (const BIGNUM**)&q,
  53233. (const BIGNUM**)&g);
  53234. DH_get0_pqg(dh, NULL,
  53235. (const BIGNUM**)&q,
  53236. (const BIGNUM**)&g);
  53237. DH_get0_pqg(dh, NULL, NULL, (const BIGNUM**)&g);
  53238. DH_get0_pqg(dh, NULL, NULL, NULL);
  53239. DH_get0_pqg(dh, (const BIGNUM**)&p,
  53240. (const BIGNUM**)&q,
  53241. (const BIGNUM**)&g);
  53242. ExpectPtrEq(p, NULL);
  53243. ExpectPtrEq(q, NULL);
  53244. ExpectPtrEq(g, NULL);
  53245. DH_free(dh);
  53246. dh = NULL;
  53247. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS) && !defined(WOLFSSL_DH_EXTRA)) \
  53248. || (defined(HAVE_FIPS_VERSION) && FIPS_VERSION_GT(2,0))
  53249. #if defined(OPENSSL_ALL) || \
  53250. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  53251. dh = wolfSSL_DH_new();
  53252. ExpectNotNull(dh);
  53253. p = wolfSSL_BN_new();
  53254. ExpectNotNull(p);
  53255. ExpectIntEQ(BN_set_word(p, 11), 1);
  53256. g = wolfSSL_BN_new();
  53257. ExpectNotNull(g);
  53258. ExpectIntEQ(BN_set_word(g, 2), 1);
  53259. q = wolfSSL_BN_new();
  53260. ExpectNotNull(q);
  53261. ExpectIntEQ(BN_set_word(q, 5), 1);
  53262. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, NULL), 0);
  53263. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 0);
  53264. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, NULL, NULL), 0);
  53265. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, q, NULL), 0);
  53266. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, NULL, NULL, g), 0);
  53267. ExpectIntEQ(wolfSSL_DH_set0_pqg(NULL, p, q, g), 0);
  53268. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, g), 0);
  53269. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, NULL), 0);
  53270. /* Don't need q. */
  53271. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  53272. if (EXPECT_FAIL()) {
  53273. BN_free(p);
  53274. BN_free(g);
  53275. }
  53276. p = NULL;
  53277. g = NULL;
  53278. /* Setting again will free the p and g. */
  53279. wolfSSL_BN_free(q);
  53280. q = NULL;
  53281. DH_free(dh);
  53282. dh = NULL;
  53283. dh = wolfSSL_DH_new();
  53284. ExpectNotNull(dh);
  53285. p = wolfSSL_BN_new();
  53286. ExpectNotNull(p);
  53287. ExpectIntEQ(BN_set_word(p, 11), 1);
  53288. g = wolfSSL_BN_new();
  53289. ExpectNotNull(g);
  53290. ExpectIntEQ(BN_set_word(g, 2), 1);
  53291. q = wolfSSL_BN_new();
  53292. ExpectNotNull(q);
  53293. ExpectIntEQ(BN_set_word(q, 5), 1);
  53294. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, q, g), 1);
  53295. /* p, q and g are now owned by dh - don't free. */
  53296. if (EXPECT_FAIL()) {
  53297. BN_free(p);
  53298. BN_free(q);
  53299. BN_free(g);
  53300. }
  53301. p = NULL;
  53302. q = NULL;
  53303. g = NULL;
  53304. p = wolfSSL_BN_new();
  53305. ExpectNotNull(p);
  53306. ExpectIntEQ(BN_set_word(p, 11), 1);
  53307. g = wolfSSL_BN_new();
  53308. ExpectNotNull(g);
  53309. ExpectIntEQ(BN_set_word(g, 2), 1);
  53310. q = wolfSSL_BN_new();
  53311. ExpectNotNull(q);
  53312. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, NULL), 1);
  53313. if (EXPECT_FAIL()) {
  53314. BN_free(p);
  53315. }
  53316. p = NULL;
  53317. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, q, NULL), 1);
  53318. if (EXPECT_FAIL()) {
  53319. BN_free(q);
  53320. }
  53321. q = NULL;
  53322. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, g), 1);
  53323. if (EXPECT_FAIL()) {
  53324. BN_free(g);
  53325. }
  53326. g = NULL;
  53327. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, NULL, NULL, NULL), 1);
  53328. /* p, q and g are now owned by dh - don't free. */
  53329. DH_free(dh);
  53330. dh = NULL;
  53331. ExpectIntEQ(DH_generate_key(NULL), 0);
  53332. ExpectNotNull(dh = DH_new());
  53333. ExpectIntEQ(DH_generate_key(dh), 0);
  53334. p = wolfSSL_BN_new();
  53335. ExpectNotNull(p);
  53336. ExpectIntEQ(BN_set_word(p, 0), 1);
  53337. g = wolfSSL_BN_new();
  53338. ExpectNotNull(g);
  53339. ExpectIntEQ(BN_set_word(g, 2), 1);
  53340. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  53341. if (EXPECT_FAIL()) {
  53342. BN_free(p);
  53343. BN_free(g);
  53344. }
  53345. p = NULL;
  53346. g = NULL;
  53347. ExpectIntEQ(DH_generate_key(dh), 0);
  53348. DH_free(dh);
  53349. dh = NULL;
  53350. #endif
  53351. #endif
  53352. /* Test DH_up_ref() */
  53353. dh = wolfSSL_DH_new();
  53354. ExpectNotNull(dh);
  53355. ExpectIntEQ(wolfSSL_DH_up_ref(NULL), WOLFSSL_FAILURE);
  53356. ExpectIntEQ(wolfSSL_DH_up_ref(dh), WOLFSSL_SUCCESS);
  53357. DH_free(dh); /* decrease ref count */
  53358. DH_free(dh); /* free WOLFSSL_DH */
  53359. dh = NULL;
  53360. q = NULL;
  53361. ExpectNull((dh = DH_new_by_nid(NID_sha1)));
  53362. #if (defined(HAVE_PUBLIC_FFDHE) || (defined(HAVE_FIPS) && \
  53363. FIPS_VERSION_EQ(2,0))) || (!defined(HAVE_PUBLIC_FFDHE) && \
  53364. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)))
  53365. #ifdef HAVE_FFDHE_2048
  53366. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  53367. DH_free(dh);
  53368. dh = NULL;
  53369. q = NULL;
  53370. #endif
  53371. #ifdef HAVE_FFDHE_3072
  53372. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe3072)));
  53373. DH_free(dh);
  53374. dh = NULL;
  53375. q = NULL;
  53376. #endif
  53377. #ifdef HAVE_FFDHE_4096
  53378. ExpectNotNull((dh = DH_new_by_nid(NID_ffdhe4096)));
  53379. DH_free(dh);
  53380. dh = NULL;
  53381. q = NULL;
  53382. #endif
  53383. #else
  53384. ExpectNull((dh = DH_new_by_nid(NID_ffdhe2048)));
  53385. #endif /* (HAVE_PUBLIC_FFDHE || (HAVE_FIPS && HAVE_FIPS_VERSION == 2)) ||
  53386. * (!HAVE_PUBLIC_FFDHE && (!HAVE_FIPS || HAVE_FIPS_VERSION > 2))*/
  53387. ExpectIntEQ(wolfSSL_DH_size(NULL), -1);
  53388. #endif /* OPENSSL_EXTRA && !NO_DH */
  53389. return EXPECT_RESULT();
  53390. }
  53391. static int test_wolfSSL_DH_dup(void)
  53392. {
  53393. EXPECT_DECLS;
  53394. #if !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA)
  53395. #if defined(WOLFSSL_QT) || defined(OPENSSL_ALL) || defined(WOLFSSL_OPENSSH) || \
  53396. defined(OPENSSL_EXTRA)
  53397. DH *dh = NULL;
  53398. DH *dhDup = NULL;
  53399. ExpectNotNull(dh = wolfSSL_DH_new());
  53400. ExpectNull(dhDup = wolfSSL_DH_dup(NULL));
  53401. ExpectNull(dhDup = wolfSSL_DH_dup(dh));
  53402. #if defined(OPENSSL_ALL) || \
  53403. defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  53404. {
  53405. WOLFSSL_BIGNUM* p = NULL;
  53406. WOLFSSL_BIGNUM* g = NULL;
  53407. ExpectNotNull(p = wolfSSL_BN_new());
  53408. ExpectNotNull(g = wolfSSL_BN_new());
  53409. ExpectIntEQ(wolfSSL_BN_set_word(p, 11), WOLFSSL_SUCCESS);
  53410. ExpectIntEQ(wolfSSL_BN_set_word(g, 2), WOLFSSL_SUCCESS);
  53411. ExpectIntEQ(wolfSSL_DH_set0_pqg(dh, p, NULL, g), 1);
  53412. if (EXPECT_FAIL()) {
  53413. wolfSSL_BN_free(p);
  53414. wolfSSL_BN_free(g);
  53415. }
  53416. ExpectNotNull(dhDup = wolfSSL_DH_dup(dh));
  53417. wolfSSL_DH_free(dhDup);
  53418. }
  53419. #endif
  53420. wolfSSL_DH_free(dh);
  53421. #endif
  53422. #endif
  53423. return EXPECT_RESULT();
  53424. }
  53425. static int test_wolfSSL_DH_check(void)
  53426. {
  53427. EXPECT_DECLS;
  53428. #ifdef OPENSSL_ALL
  53429. #ifndef NO_DH
  53430. #ifndef NO_BIO
  53431. #ifndef NO_DSA
  53432. byte buf[6000];
  53433. char file[] = "./certs/dsaparams.pem";
  53434. XFILE f = XBADFILE;
  53435. int bytes;
  53436. BIO* bio = NULL;
  53437. DSA* dsa = NULL;
  53438. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  53439. static const byte dh2048[] = {
  53440. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  53441. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  53442. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  53443. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  53444. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  53445. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  53446. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  53447. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  53448. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  53449. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  53450. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  53451. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  53452. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  53453. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  53454. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  53455. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  53456. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  53457. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  53458. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  53459. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  53460. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  53461. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  53462. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  53463. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  53464. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  53465. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  53466. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  53467. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  53468. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  53469. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  53470. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  53471. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  53472. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  53473. 0x93, 0x02, 0x01, 0x02
  53474. };
  53475. const byte* params;
  53476. #endif
  53477. DH* dh = NULL;
  53478. WOLFSSL_BIGNUM* p = NULL;
  53479. WOLFSSL_BIGNUM* g = NULL;
  53480. WOLFSSL_BIGNUM* pTmp = NULL;
  53481. WOLFSSL_BIGNUM* gTmp = NULL;
  53482. int codes = -1;
  53483. #ifndef NO_DSA
  53484. /* Initialize DH */
  53485. ExpectTrue((f = XFOPEN(file, "rb")) != XBADFILE);
  53486. ExpectIntGT(bytes = (int)XFREAD(buf, 1, sizeof(buf), f), 0);
  53487. if (f != XBADFILE)
  53488. XFCLOSE(f);
  53489. ExpectNotNull(bio = BIO_new_mem_buf((void*)buf, bytes));
  53490. ExpectNotNull(dsa = wolfSSL_PEM_read_bio_DSAparams(bio, NULL, NULL, NULL));
  53491. ExpectNotNull(dh = wolfSSL_DSA_dup_DH(dsa));
  53492. ExpectNotNull(dh);
  53493. BIO_free(bio);
  53494. DSA_free(dsa);
  53495. #elif !defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)
  53496. params = dh2048;
  53497. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &params,
  53498. (long)sizeof(dh2048)));
  53499. #else
  53500. ExpectNotNull(dh = wolfSSL_DH_new_by_nid(NID_ffdhe2048));
  53501. #endif
  53502. /* Test assumed to be valid dh.
  53503. * Should return WOLFSSL_SUCCESS
  53504. * codes should be 0
  53505. * Invalid codes = {DH_NOT_SUITABLE_GENERATOR, DH_CHECK_P_NOT_PRIME}
  53506. */
  53507. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  53508. ExpectIntEQ(codes, 0);
  53509. /* Test NULL dh: expected BAD_FUNC_ARG */
  53510. ExpectIntEQ(wolfSSL_DH_check(NULL, &codes), 0);
  53511. /* Break dh prime to test if codes = DH_CHECK_P_NOT_PRIME */
  53512. if (dh != NULL) {
  53513. pTmp = dh->p;
  53514. dh->p = NULL;
  53515. }
  53516. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  53517. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  53518. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  53519. /* set dh->p back to normal so it won't fail on next tests */
  53520. if (dh != NULL) {
  53521. dh->p = pTmp;
  53522. pTmp = NULL;
  53523. }
  53524. /* Break dh generator to test if codes = DH_NOT_SUITABLE_GENERATOR */
  53525. if (dh != NULL) {
  53526. gTmp = dh->g;
  53527. dh->g = NULL;
  53528. }
  53529. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  53530. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  53531. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR);
  53532. if (dh != NULL) {
  53533. dh->g = gTmp;
  53534. gTmp = NULL;
  53535. }
  53536. /* Cleanup */
  53537. DH_free(dh);
  53538. dh = NULL;
  53539. dh = DH_new();
  53540. ExpectNotNull(dh);
  53541. /* Check empty DH. */
  53542. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  53543. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  53544. ExpectIntEQ(codes, DH_NOT_SUITABLE_GENERATOR | DH_CHECK_P_NOT_PRIME);
  53545. /* Check non-prime valued p. */
  53546. ExpectNotNull(p = BN_new());
  53547. ExpectIntEQ(BN_set_word(p, 4), 1);
  53548. ExpectNotNull(g = BN_new());
  53549. ExpectIntEQ(BN_set_word(g, 2), 1);
  53550. ExpectIntEQ(DH_set0_pqg(dh, p, NULL, g), 1);
  53551. if (EXPECT_FAIL()) {
  53552. wolfSSL_BN_free(p);
  53553. wolfSSL_BN_free(g);
  53554. }
  53555. ExpectIntEQ(wolfSSL_DH_check(dh, &codes), 1);
  53556. ExpectIntEQ(wolfSSL_DH_check(dh, NULL), 0);
  53557. ExpectIntEQ(codes, DH_CHECK_P_NOT_PRIME);
  53558. DH_free(dh);
  53559. dh = NULL;
  53560. #endif
  53561. #endif /* !NO_DH && !NO_DSA */
  53562. #endif
  53563. return EXPECT_RESULT();
  53564. }
  53565. static int test_wolfSSL_DH_prime(void)
  53566. {
  53567. EXPECT_DECLS;
  53568. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  53569. WOLFSSL_BIGNUM* bn = NULL;
  53570. #if WOLFSSL_MAX_BN_BITS >= 768
  53571. WOLFSSL_BIGNUM* bn2 = NULL;
  53572. #endif
  53573. bn = wolfSSL_DH_768_prime(NULL);
  53574. #if WOLFSSL_MAX_BN_BITS >= 768
  53575. ExpectNotNull(bn);
  53576. bn2 = wolfSSL_DH_768_prime(bn);
  53577. ExpectNotNull(bn2);
  53578. ExpectTrue(bn == bn2);
  53579. wolfSSL_BN_free(bn);
  53580. bn = NULL;
  53581. #else
  53582. ExpectNull(bn);
  53583. #endif
  53584. bn = wolfSSL_DH_1024_prime(NULL);
  53585. #if WOLFSSL_MAX_BN_BITS >= 1024
  53586. ExpectNotNull(bn);
  53587. wolfSSL_BN_free(bn);
  53588. bn = NULL;
  53589. #else
  53590. ExpectNull(bn);
  53591. #endif
  53592. bn = wolfSSL_DH_2048_prime(NULL);
  53593. #if WOLFSSL_MAX_BN_BITS >= 2048
  53594. ExpectNotNull(bn);
  53595. wolfSSL_BN_free(bn);
  53596. bn = NULL;
  53597. #else
  53598. ExpectNull(bn);
  53599. #endif
  53600. bn = wolfSSL_DH_3072_prime(NULL);
  53601. #if WOLFSSL_MAX_BN_BITS >= 3072
  53602. ExpectNotNull(bn);
  53603. wolfSSL_BN_free(bn);
  53604. bn = NULL;
  53605. #else
  53606. ExpectNull(bn);
  53607. #endif
  53608. bn = wolfSSL_DH_4096_prime(NULL);
  53609. #if WOLFSSL_MAX_BN_BITS >= 4096
  53610. ExpectNotNull(bn);
  53611. wolfSSL_BN_free(bn);
  53612. bn = NULL;
  53613. #else
  53614. ExpectNull(bn);
  53615. #endif
  53616. bn = wolfSSL_DH_6144_prime(NULL);
  53617. #if WOLFSSL_MAX_BN_BITS >= 6144
  53618. ExpectNotNull(bn);
  53619. wolfSSL_BN_free(bn);
  53620. bn = NULL;
  53621. #else
  53622. ExpectNull(bn);
  53623. #endif
  53624. bn = wolfSSL_DH_8192_prime(NULL);
  53625. #if WOLFSSL_MAX_BN_BITS >= 8192
  53626. ExpectNotNull(bn);
  53627. wolfSSL_BN_free(bn);
  53628. bn = NULL;
  53629. #else
  53630. ExpectNull(bn);
  53631. #endif
  53632. #endif
  53633. return EXPECT_RESULT();
  53634. }
  53635. static int test_wolfSSL_DH_1536_prime(void)
  53636. {
  53637. EXPECT_DECLS;
  53638. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  53639. BIGNUM* bn = NULL;
  53640. unsigned char bits[200];
  53641. int sz = 192; /* known binary size */
  53642. const byte expected[] = {
  53643. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  53644. 0xC9,0x0F,0xDA,0xA2,0x21,0x68,0xC2,0x34,
  53645. 0xC4,0xC6,0x62,0x8B,0x80,0xDC,0x1C,0xD1,
  53646. 0x29,0x02,0x4E,0x08,0x8A,0x67,0xCC,0x74,
  53647. 0x02,0x0B,0xBE,0xA6,0x3B,0x13,0x9B,0x22,
  53648. 0x51,0x4A,0x08,0x79,0x8E,0x34,0x04,0xDD,
  53649. 0xEF,0x95,0x19,0xB3,0xCD,0x3A,0x43,0x1B,
  53650. 0x30,0x2B,0x0A,0x6D,0xF2,0x5F,0x14,0x37,
  53651. 0x4F,0xE1,0x35,0x6D,0x6D,0x51,0xC2,0x45,
  53652. 0xE4,0x85,0xB5,0x76,0x62,0x5E,0x7E,0xC6,
  53653. 0xF4,0x4C,0x42,0xE9,0xA6,0x37,0xED,0x6B,
  53654. 0x0B,0xFF,0x5C,0xB6,0xF4,0x06,0xB7,0xED,
  53655. 0xEE,0x38,0x6B,0xFB,0x5A,0x89,0x9F,0xA5,
  53656. 0xAE,0x9F,0x24,0x11,0x7C,0x4B,0x1F,0xE6,
  53657. 0x49,0x28,0x66,0x51,0xEC,0xE4,0x5B,0x3D,
  53658. 0xC2,0x00,0x7C,0xB8,0xA1,0x63,0xBF,0x05,
  53659. 0x98,0xDA,0x48,0x36,0x1C,0x55,0xD3,0x9A,
  53660. 0x69,0x16,0x3F,0xA8,0xFD,0x24,0xCF,0x5F,
  53661. 0x83,0x65,0x5D,0x23,0xDC,0xA3,0xAD,0x96,
  53662. 0x1C,0x62,0xF3,0x56,0x20,0x85,0x52,0xBB,
  53663. 0x9E,0xD5,0x29,0x07,0x70,0x96,0x96,0x6D,
  53664. 0x67,0x0C,0x35,0x4E,0x4A,0xBC,0x98,0x04,
  53665. 0xF1,0x74,0x6C,0x08,0xCA,0x23,0x73,0x27,
  53666. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
  53667. };
  53668. ExpectNotNull(bn = get_rfc3526_prime_1536(NULL));
  53669. ExpectIntEQ(sz, BN_bn2bin((const BIGNUM*)bn, bits));
  53670. ExpectIntEQ(0, XMEMCMP(expected, bits, sz));
  53671. BN_free(bn);
  53672. #endif
  53673. return EXPECT_RESULT();
  53674. }
  53675. static int test_wolfSSL_DH_get_2048_256(void)
  53676. {
  53677. EXPECT_DECLS;
  53678. #if defined(OPENSSL_EXTRA) && !defined(NO_DH)
  53679. WOLFSSL_DH* dh = NULL;
  53680. const WOLFSSL_BIGNUM* pBn;
  53681. const WOLFSSL_BIGNUM* gBn;
  53682. const WOLFSSL_BIGNUM* qBn;
  53683. const byte pExpected[] = {
  53684. 0x87, 0xA8, 0xE6, 0x1D, 0xB4, 0xB6, 0x66, 0x3C, 0xFF, 0xBB, 0xD1, 0x9C,
  53685. 0x65, 0x19, 0x59, 0x99, 0x8C, 0xEE, 0xF6, 0x08, 0x66, 0x0D, 0xD0, 0xF2,
  53686. 0x5D, 0x2C, 0xEE, 0xD4, 0x43, 0x5E, 0x3B, 0x00, 0xE0, 0x0D, 0xF8, 0xF1,
  53687. 0xD6, 0x19, 0x57, 0xD4, 0xFA, 0xF7, 0xDF, 0x45, 0x61, 0xB2, 0xAA, 0x30,
  53688. 0x16, 0xC3, 0xD9, 0x11, 0x34, 0x09, 0x6F, 0xAA, 0x3B, 0xF4, 0x29, 0x6D,
  53689. 0x83, 0x0E, 0x9A, 0x7C, 0x20, 0x9E, 0x0C, 0x64, 0x97, 0x51, 0x7A, 0xBD,
  53690. 0x5A, 0x8A, 0x9D, 0x30, 0x6B, 0xCF, 0x67, 0xED, 0x91, 0xF9, 0xE6, 0x72,
  53691. 0x5B, 0x47, 0x58, 0xC0, 0x22, 0xE0, 0xB1, 0xEF, 0x42, 0x75, 0xBF, 0x7B,
  53692. 0x6C, 0x5B, 0xFC, 0x11, 0xD4, 0x5F, 0x90, 0x88, 0xB9, 0x41, 0xF5, 0x4E,
  53693. 0xB1, 0xE5, 0x9B, 0xB8, 0xBC, 0x39, 0xA0, 0xBF, 0x12, 0x30, 0x7F, 0x5C,
  53694. 0x4F, 0xDB, 0x70, 0xC5, 0x81, 0xB2, 0x3F, 0x76, 0xB6, 0x3A, 0xCA, 0xE1,
  53695. 0xCA, 0xA6, 0xB7, 0x90, 0x2D, 0x52, 0x52, 0x67, 0x35, 0x48, 0x8A, 0x0E,
  53696. 0xF1, 0x3C, 0x6D, 0x9A, 0x51, 0xBF, 0xA4, 0xAB, 0x3A, 0xD8, 0x34, 0x77,
  53697. 0x96, 0x52, 0x4D, 0x8E, 0xF6, 0xA1, 0x67, 0xB5, 0xA4, 0x18, 0x25, 0xD9,
  53698. 0x67, 0xE1, 0x44, 0xE5, 0x14, 0x05, 0x64, 0x25, 0x1C, 0xCA, 0xCB, 0x83,
  53699. 0xE6, 0xB4, 0x86, 0xF6, 0xB3, 0xCA, 0x3F, 0x79, 0x71, 0x50, 0x60, 0x26,
  53700. 0xC0, 0xB8, 0x57, 0xF6, 0x89, 0x96, 0x28, 0x56, 0xDE, 0xD4, 0x01, 0x0A,
  53701. 0xBD, 0x0B, 0xE6, 0x21, 0xC3, 0xA3, 0x96, 0x0A, 0x54, 0xE7, 0x10, 0xC3,
  53702. 0x75, 0xF2, 0x63, 0x75, 0xD7, 0x01, 0x41, 0x03, 0xA4, 0xB5, 0x43, 0x30,
  53703. 0xC1, 0x98, 0xAF, 0x12, 0x61, 0x16, 0xD2, 0x27, 0x6E, 0x11, 0x71, 0x5F,
  53704. 0x69, 0x38, 0x77, 0xFA, 0xD7, 0xEF, 0x09, 0xCA, 0xDB, 0x09, 0x4A, 0xE9,
  53705. 0x1E, 0x1A, 0x15, 0x97
  53706. };
  53707. const byte gExpected[] = {
  53708. 0x3F, 0xB3, 0x2C, 0x9B, 0x73, 0x13, 0x4D, 0x0B, 0x2E, 0x77, 0x50, 0x66,
  53709. 0x60, 0xED, 0xBD, 0x48, 0x4C, 0xA7, 0xB1, 0x8F, 0x21, 0xEF, 0x20, 0x54,
  53710. 0x07, 0xF4, 0x79, 0x3A, 0x1A, 0x0B, 0xA1, 0x25, 0x10, 0xDB, 0xC1, 0x50,
  53711. 0x77, 0xBE, 0x46, 0x3F, 0xFF, 0x4F, 0xED, 0x4A, 0xAC, 0x0B, 0xB5, 0x55,
  53712. 0xBE, 0x3A, 0x6C, 0x1B, 0x0C, 0x6B, 0x47, 0xB1, 0xBC, 0x37, 0x73, 0xBF,
  53713. 0x7E, 0x8C, 0x6F, 0x62, 0x90, 0x12, 0x28, 0xF8, 0xC2, 0x8C, 0xBB, 0x18,
  53714. 0xA5, 0x5A, 0xE3, 0x13, 0x41, 0x00, 0x0A, 0x65, 0x01, 0x96, 0xF9, 0x31,
  53715. 0xC7, 0x7A, 0x57, 0xF2, 0xDD, 0xF4, 0x63, 0xE5, 0xE9, 0xEC, 0x14, 0x4B,
  53716. 0x77, 0x7D, 0xE6, 0x2A, 0xAA, 0xB8, 0xA8, 0x62, 0x8A, 0xC3, 0x76, 0xD2,
  53717. 0x82, 0xD6, 0xED, 0x38, 0x64, 0xE6, 0x79, 0x82, 0x42, 0x8E, 0xBC, 0x83,
  53718. 0x1D, 0x14, 0x34, 0x8F, 0x6F, 0x2F, 0x91, 0x93, 0xB5, 0x04, 0x5A, 0xF2,
  53719. 0x76, 0x71, 0x64, 0xE1, 0xDF, 0xC9, 0x67, 0xC1, 0xFB, 0x3F, 0x2E, 0x55,
  53720. 0xA4, 0xBD, 0x1B, 0xFF, 0xE8, 0x3B, 0x9C, 0x80, 0xD0, 0x52, 0xB9, 0x85,
  53721. 0xD1, 0x82, 0xEA, 0x0A, 0xDB, 0x2A, 0x3B, 0x73, 0x13, 0xD3, 0xFE, 0x14,
  53722. 0xC8, 0x48, 0x4B, 0x1E, 0x05, 0x25, 0x88, 0xB9, 0xB7, 0xD2, 0xBB, 0xD2,
  53723. 0xDF, 0x01, 0x61, 0x99, 0xEC, 0xD0, 0x6E, 0x15, 0x57, 0xCD, 0x09, 0x15,
  53724. 0xB3, 0x35, 0x3B, 0xBB, 0x64, 0xE0, 0xEC, 0x37, 0x7F, 0xD0, 0x28, 0x37,
  53725. 0x0D, 0xF9, 0x2B, 0x52, 0xC7, 0x89, 0x14, 0x28, 0xCD, 0xC6, 0x7E, 0xB6,
  53726. 0x18, 0x4B, 0x52, 0x3D, 0x1D, 0xB2, 0x46, 0xC3, 0x2F, 0x63, 0x07, 0x84,
  53727. 0x90, 0xF0, 0x0E, 0xF8, 0xD6, 0x47, 0xD1, 0x48, 0xD4, 0x79, 0x54, 0x51,
  53728. 0x5E, 0x23, 0x27, 0xCF, 0xEF, 0x98, 0xC5, 0x82, 0x66, 0x4B, 0x4C, 0x0F,
  53729. 0x6C, 0xC4, 0x16, 0x59
  53730. };
  53731. const byte qExpected[] = {
  53732. 0x8C, 0xF8, 0x36, 0x42, 0xA7, 0x09, 0xA0, 0x97, 0xB4, 0x47, 0x99, 0x76,
  53733. 0x40, 0x12, 0x9D, 0xA2, 0x99, 0xB1, 0xA4, 0x7D, 0x1E, 0xB3, 0x75, 0x0B,
  53734. 0xA3, 0x08, 0xB0, 0xFE, 0x64, 0xF5, 0xFB, 0xD3
  53735. };
  53736. int pSz = 0;
  53737. int qSz = 0;
  53738. int gSz = 0;
  53739. byte* pReturned = NULL;
  53740. byte* qReturned = NULL;
  53741. byte* gReturned = NULL;
  53742. ExpectNotNull((dh = wolfSSL_DH_get_2048_256()));
  53743. wolfSSL_DH_get0_pqg(dh, &pBn, &qBn, &gBn);
  53744. ExpectIntGT((pSz = wolfSSL_BN_num_bytes(pBn)), 0);
  53745. ExpectNotNull(pReturned = (byte*)XMALLOC(pSz, NULL,
  53746. DYNAMIC_TYPE_TMP_BUFFER));
  53747. ExpectIntGT((pSz = wolfSSL_BN_bn2bin(pBn, pReturned)), 0);
  53748. ExpectIntEQ(pSz, sizeof(pExpected));
  53749. ExpectIntEQ(XMEMCMP(pExpected, pReturned, pSz), 0);
  53750. ExpectIntGT((qSz = wolfSSL_BN_num_bytes(qBn)), 0);
  53751. ExpectNotNull(qReturned = (byte*)XMALLOC(qSz, NULL,
  53752. DYNAMIC_TYPE_TMP_BUFFER));
  53753. ExpectIntGT((qSz = wolfSSL_BN_bn2bin(qBn, qReturned)), 0);
  53754. ExpectIntEQ(qSz, sizeof(qExpected));
  53755. ExpectIntEQ(XMEMCMP(qExpected, qReturned, qSz), 0);
  53756. ExpectIntGT((gSz = wolfSSL_BN_num_bytes(gBn)), 0);
  53757. ExpectNotNull(gReturned = (byte*)XMALLOC(gSz, NULL,
  53758. DYNAMIC_TYPE_TMP_BUFFER));
  53759. ExpectIntGT((gSz = wolfSSL_BN_bn2bin(gBn, gReturned)), 0);
  53760. ExpectIntEQ(gSz, sizeof(gExpected));
  53761. ExpectIntEQ(XMEMCMP(gExpected, gReturned, gSz), 0);
  53762. wolfSSL_DH_free(dh);
  53763. XFREE(pReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53764. XFREE(gReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53765. XFREE(qReturned, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  53766. #endif
  53767. return EXPECT_RESULT();
  53768. }
  53769. static int test_wolfSSL_PEM_write_DHparams(void)
  53770. {
  53771. EXPECT_DECLS;
  53772. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO) && \
  53773. !defined(NO_DH) && defined(WOLFSSL_DH_EXTRA) && !defined(NO_FILESYSTEM)
  53774. DH* dh = NULL;
  53775. BIO* bio = NULL;
  53776. XFILE fp = XBADFILE;
  53777. byte pem[2048];
  53778. int pemSz = 0;
  53779. const char expected[] =
  53780. "-----BEGIN DH PARAMETERS-----\n"
  53781. "MIIBCAKCAQEAsKEIBpwIE7pZBjy8MNX1AMFPRKfW70rGJScc6NKWUwpckd2iwpSE\n"
  53782. "v32yRJ+b0sGKxb5yXKfnkebUn3MHhVtmSMdw+rTuAsk9mkraPcFGPhlp0RdGB6NN\n"
  53783. "nyuWFzltMI0q85TTdc+gdebykh8acAWqBINXMPvadpM4UOgn/WPuPOW3yAmub1A1\n"
  53784. "joTOSgDpEn5aMdcz/CETdswWMNsM/MVipzW477ewrMA29tnJRkj5QJAAKxuqbOMa\n"
  53785. "wwsDnhvCRuRITiJzb8Nf1JrWMAdI1oyQq9T28eNI01hLprnNKb9oHwhLY4YvXGvW\n"
  53786. "tgZl96bcAGdru8OpQYP7x/rI4h5+rwA/kwIBAg==\n"
  53787. "-----END DH PARAMETERS-----\n";
  53788. const char badPem[] =
  53789. "-----BEGIN DH PARAMETERS-----\n"
  53790. "-----END DH PARAMETERS-----\n";
  53791. const char emptySeqPem[] =
  53792. "-----BEGIN DH PARAMETERS-----\n"
  53793. "MAA=\n"
  53794. "-----END DH PARAMETERS-----\n";
  53795. ExpectTrue((fp = XFOPEN(dhParamFile, "rb")) != XBADFILE);
  53796. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  53797. if (fp != XBADFILE) {
  53798. XFCLOSE(fp);
  53799. fp = XBADFILE;
  53800. }
  53801. ExpectNull(PEM_read_bio_DHparams(NULL, NULL, NULL, NULL));
  53802. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  53803. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  53804. ExpectIntEQ(BIO_write(bio, badPem, (int)sizeof(badPem)),
  53805. (int)sizeof(badPem));
  53806. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  53807. BIO_free(bio);
  53808. bio = NULL;
  53809. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  53810. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  53811. ExpectIntEQ(BIO_write(bio, emptySeqPem, (int)sizeof(emptySeqPem)),
  53812. (int)sizeof(emptySeqPem));
  53813. ExpectNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  53814. BIO_free(bio);
  53815. bio = NULL;
  53816. ExpectNotNull(bio = BIO_new(BIO_s_mem()));
  53817. ExpectIntEQ(BIO_write(bio, pem, pemSz), pemSz);
  53818. ExpectNotNull(dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL));
  53819. BIO_free(bio);
  53820. bio = NULL;
  53821. ExpectNotNull(fp = XFOPEN("./test-write-dhparams.pem", "wb"));
  53822. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_SUCCESS);
  53823. ExpectIntEQ(PEM_write_DHparams(fp, NULL), WOLFSSL_FAILURE);
  53824. DH_free(dh);
  53825. dh = NULL;
  53826. dh = wolfSSL_DH_new();
  53827. ExpectIntEQ(PEM_write_DHparams(fp, dh), WOLFSSL_FAILURE);
  53828. if (fp != XBADFILE) {
  53829. XFCLOSE(fp);
  53830. fp = XBADFILE;
  53831. }
  53832. wolfSSL_DH_free(dh);
  53833. dh = NULL;
  53834. /* check results */
  53835. XMEMSET(pem, 0, sizeof(pem));
  53836. ExpectTrue((fp = XFOPEN("./test-write-dhparams.pem", "rb")) != XBADFILE);
  53837. ExpectIntGT((pemSz = (int)XFREAD(pem, 1, sizeof(pem), fp)), 0);
  53838. ExpectIntEQ(XMEMCMP(pem, expected, pemSz), 0);
  53839. if (fp != XBADFILE)
  53840. XFCLOSE(fp);
  53841. #endif
  53842. return EXPECT_RESULT();
  53843. }
  53844. static int test_wolfSSL_d2i_DHparams(void)
  53845. {
  53846. EXPECT_DECLS;
  53847. #ifdef OPENSSL_ALL
  53848. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  53849. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  53850. XFILE f = XBADFILE;
  53851. unsigned char buf[4096];
  53852. const unsigned char* pt = buf;
  53853. #ifdef HAVE_FFDHE_2048
  53854. const char* params1 = "./certs/dh2048.der";
  53855. #endif
  53856. #ifdef HAVE_FFDHE_3072
  53857. const char* params2 = "./certs/dh3072.der";
  53858. #endif
  53859. long len = 0;
  53860. WOLFSSL_DH* dh = NULL;
  53861. XMEMSET(buf, 0, sizeof(buf));
  53862. /* Test 2048 bit parameters */
  53863. #ifdef HAVE_FFDHE_2048
  53864. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  53865. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  53866. if (f != XBADFILE) {
  53867. XFCLOSE(f);
  53868. f = XBADFILE;
  53869. }
  53870. /* Valid case */
  53871. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  53872. ExpectNotNull(dh->p);
  53873. ExpectNotNull(dh->g);
  53874. ExpectTrue(pt == buf);
  53875. ExpectIntEQ(DH_set_length(NULL, BN_num_bits(dh->p)), 0);
  53876. ExpectIntEQ(DH_set_length(dh, BN_num_bits(dh->p)), 1);
  53877. ExpectIntEQ(DH_generate_key(dh), WOLFSSL_SUCCESS);
  53878. /* Invalid cases */
  53879. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  53880. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  53881. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, 10));
  53882. DH_free(dh);
  53883. dh = NULL;
  53884. *buf = 0;
  53885. pt = buf;
  53886. #endif /* HAVE_FFDHE_2048 */
  53887. /* Test 3072 bit parameters */
  53888. #ifdef HAVE_FFDHE_3072
  53889. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  53890. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  53891. if (f != XBADFILE) {
  53892. XFCLOSE(f);
  53893. f = XBADFILE;
  53894. }
  53895. /* Valid case */
  53896. ExpectNotNull(dh = wolfSSL_d2i_DHparams(&dh, &pt, len));
  53897. ExpectNotNull(dh->p);
  53898. ExpectNotNull(dh->g);
  53899. ExpectTrue(pt != buf);
  53900. ExpectIntEQ(DH_generate_key(dh), 1);
  53901. /* Invalid cases */
  53902. ExpectNull(wolfSSL_d2i_DHparams(NULL, NULL, len));
  53903. ExpectNull(wolfSSL_d2i_DHparams(NULL, &pt, -1));
  53904. DH_free(dh);
  53905. dh = NULL;
  53906. #endif /* HAVE_FFDHE_3072 */
  53907. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  53908. #endif /* !NO_DH */
  53909. #endif
  53910. return EXPECT_RESULT();
  53911. }
  53912. static int test_wolfSSL_DH_LoadDer(void)
  53913. {
  53914. EXPECT_DECLS;
  53915. #if !defined(NO_DH) && (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0)) && \
  53916. defined(OPENSSL_EXTRA)
  53917. static const byte dh2048[] = {
  53918. 0x30, 0x82, 0x01, 0x08, 0x02, 0x82, 0x01, 0x01,
  53919. 0x00, 0xb0, 0xa1, 0x08, 0x06, 0x9c, 0x08, 0x13,
  53920. 0xba, 0x59, 0x06, 0x3c, 0xbc, 0x30, 0xd5, 0xf5,
  53921. 0x00, 0xc1, 0x4f, 0x44, 0xa7, 0xd6, 0xef, 0x4a,
  53922. 0xc6, 0x25, 0x27, 0x1c, 0xe8, 0xd2, 0x96, 0x53,
  53923. 0x0a, 0x5c, 0x91, 0xdd, 0xa2, 0xc2, 0x94, 0x84,
  53924. 0xbf, 0x7d, 0xb2, 0x44, 0x9f, 0x9b, 0xd2, 0xc1,
  53925. 0x8a, 0xc5, 0xbe, 0x72, 0x5c, 0xa7, 0xe7, 0x91,
  53926. 0xe6, 0xd4, 0x9f, 0x73, 0x07, 0x85, 0x5b, 0x66,
  53927. 0x48, 0xc7, 0x70, 0xfa, 0xb4, 0xee, 0x02, 0xc9,
  53928. 0x3d, 0x9a, 0x4a, 0xda, 0x3d, 0xc1, 0x46, 0x3e,
  53929. 0x19, 0x69, 0xd1, 0x17, 0x46, 0x07, 0xa3, 0x4d,
  53930. 0x9f, 0x2b, 0x96, 0x17, 0x39, 0x6d, 0x30, 0x8d,
  53931. 0x2a, 0xf3, 0x94, 0xd3, 0x75, 0xcf, 0xa0, 0x75,
  53932. 0xe6, 0xf2, 0x92, 0x1f, 0x1a, 0x70, 0x05, 0xaa,
  53933. 0x04, 0x83, 0x57, 0x30, 0xfb, 0xda, 0x76, 0x93,
  53934. 0x38, 0x50, 0xe8, 0x27, 0xfd, 0x63, 0xee, 0x3c,
  53935. 0xe5, 0xb7, 0xc8, 0x09, 0xae, 0x6f, 0x50, 0x35,
  53936. 0x8e, 0x84, 0xce, 0x4a, 0x00, 0xe9, 0x12, 0x7e,
  53937. 0x5a, 0x31, 0xd7, 0x33, 0xfc, 0x21, 0x13, 0x76,
  53938. 0xcc, 0x16, 0x30, 0xdb, 0x0c, 0xfc, 0xc5, 0x62,
  53939. 0xa7, 0x35, 0xb8, 0xef, 0xb7, 0xb0, 0xac, 0xc0,
  53940. 0x36, 0xf6, 0xd9, 0xc9, 0x46, 0x48, 0xf9, 0x40,
  53941. 0x90, 0x00, 0x2b, 0x1b, 0xaa, 0x6c, 0xe3, 0x1a,
  53942. 0xc3, 0x0b, 0x03, 0x9e, 0x1b, 0xc2, 0x46, 0xe4,
  53943. 0x48, 0x4e, 0x22, 0x73, 0x6f, 0xc3, 0x5f, 0xd4,
  53944. 0x9a, 0xd6, 0x30, 0x07, 0x48, 0xd6, 0x8c, 0x90,
  53945. 0xab, 0xd4, 0xf6, 0xf1, 0xe3, 0x48, 0xd3, 0x58,
  53946. 0x4b, 0xa6, 0xb9, 0xcd, 0x29, 0xbf, 0x68, 0x1f,
  53947. 0x08, 0x4b, 0x63, 0x86, 0x2f, 0x5c, 0x6b, 0xd6,
  53948. 0xb6, 0x06, 0x65, 0xf7, 0xa6, 0xdc, 0x00, 0x67,
  53949. 0x6b, 0xbb, 0xc3, 0xa9, 0x41, 0x83, 0xfb, 0xc7,
  53950. 0xfa, 0xc8, 0xe2, 0x1e, 0x7e, 0xaf, 0x00, 0x3f,
  53951. 0x93, 0x02, 0x01, 0x02
  53952. };
  53953. WOLFSSL_DH* dh = NULL;
  53954. ExpectNotNull(dh = wolfSSL_DH_new());
  53955. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, NULL, 0), -1);
  53956. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, NULL, 0), -1);
  53957. ExpectIntEQ(wolfSSL_DH_LoadDer(NULL, dh2048, sizeof(dh2048)), -1);
  53958. ExpectIntEQ(wolfSSL_DH_LoadDer(dh, dh2048, sizeof(dh2048)), 1);
  53959. wolfSSL_DH_free(dh);
  53960. #endif
  53961. return EXPECT_RESULT();
  53962. }
  53963. static int test_wolfSSL_i2d_DHparams(void)
  53964. {
  53965. EXPECT_DECLS;
  53966. #ifdef OPENSSL_ALL
  53967. #if !defined(NO_DH) && (defined(HAVE_FFDHE_2048) || defined(HAVE_FFDHE_3072))
  53968. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  53969. XFILE f = XBADFILE;
  53970. unsigned char buf[4096];
  53971. const unsigned char* pt;
  53972. unsigned char* pt2;
  53973. #ifdef HAVE_FFDHE_2048
  53974. const char* params1 = "./certs/dh2048.der";
  53975. #endif
  53976. #ifdef HAVE_FFDHE_3072
  53977. const char* params2 = "./certs/dh3072.der";
  53978. #endif
  53979. long len = 0;
  53980. WOLFSSL_DH* dh = NULL;
  53981. /* Test 2048 bit parameters */
  53982. #ifdef HAVE_FFDHE_2048
  53983. pt = buf;
  53984. pt2 = buf;
  53985. ExpectTrue((f = XFOPEN(params1, "rb")) != XBADFILE);
  53986. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  53987. if (f != XBADFILE) {
  53988. XFCLOSE(f);
  53989. f = XBADFILE;
  53990. }
  53991. /* Valid case */
  53992. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  53993. ExpectTrue(pt == buf);
  53994. ExpectIntEQ(DH_generate_key(dh), 1);
  53995. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 268);
  53996. /* Invalid case */
  53997. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  53998. /* Return length only */
  53999. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 268);
  54000. DH_free(dh);
  54001. dh = NULL;
  54002. *buf = 0;
  54003. #endif
  54004. /* Test 3072 bit parameters */
  54005. #ifdef HAVE_FFDHE_3072
  54006. pt = buf;
  54007. pt2 = buf;
  54008. ExpectTrue((f = XFOPEN(params2, "rb")) != XBADFILE);
  54009. ExpectTrue((len = (long)XFREAD(buf, 1, sizeof(buf), f)) > 0);
  54010. if (f != XBADFILE) {
  54011. XFCLOSE(f);
  54012. f = XBADFILE;
  54013. }
  54014. /* Valid case */
  54015. ExpectNotNull(dh = wolfSSL_d2i_DHparams(NULL, &pt, len));
  54016. ExpectTrue(pt == buf);
  54017. ExpectIntEQ(DH_generate_key(dh), 1);
  54018. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 396);
  54019. /* Invalid case */
  54020. ExpectIntEQ(wolfSSL_i2d_DHparams(NULL, &pt2), 0);
  54021. /* Return length only */
  54022. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, NULL), 396);
  54023. DH_free(dh);
  54024. dh = NULL;
  54025. #endif
  54026. dh = DH_new();
  54027. ExpectNotNull(dh);
  54028. pt2 = buf;
  54029. ExpectIntEQ(wolfSSL_i2d_DHparams(dh, &pt2), 0);
  54030. DH_free(dh);
  54031. dh = NULL;
  54032. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  54033. #endif /* !NO_DH && (HAVE_FFDHE_2048 || HAVE_FFDHE_3072) */
  54034. #endif
  54035. return EXPECT_RESULT();
  54036. }
  54037. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  54038. /*----------------------------------------------------------------------------*
  54039. | EC
  54040. *----------------------------------------------------------------------------*/
  54041. static int test_wolfSSL_EC_GROUP(void)
  54042. {
  54043. EXPECT_DECLS;
  54044. #ifdef OPENSSL_EXTRA
  54045. EC_GROUP *group = NULL;
  54046. EC_GROUP *group2 = NULL;
  54047. EC_GROUP *group3 = NULL;
  54048. #ifndef HAVE_ECC_BRAINPOOL
  54049. EC_GROUP *group4 = NULL;
  54050. #endif
  54051. WOLFSSL_BIGNUM* order = NULL;
  54052. int group_bits;
  54053. int i;
  54054. static const int knownEccNids[] = {
  54055. NID_X9_62_prime192v1,
  54056. NID_X9_62_prime192v2,
  54057. NID_X9_62_prime192v3,
  54058. NID_X9_62_prime239v1,
  54059. NID_X9_62_prime239v2,
  54060. NID_X9_62_prime239v3,
  54061. NID_X9_62_prime256v1,
  54062. NID_secp112r1,
  54063. NID_secp112r2,
  54064. NID_secp128r1,
  54065. NID_secp128r2,
  54066. NID_secp160r1,
  54067. NID_secp160r2,
  54068. NID_secp224r1,
  54069. NID_secp384r1,
  54070. NID_secp521r1,
  54071. NID_secp160k1,
  54072. NID_secp192k1,
  54073. NID_secp224k1,
  54074. NID_secp256k1,
  54075. NID_brainpoolP160r1,
  54076. NID_brainpoolP192r1,
  54077. NID_brainpoolP224r1,
  54078. NID_brainpoolP256r1,
  54079. NID_brainpoolP320r1,
  54080. NID_brainpoolP384r1,
  54081. NID_brainpoolP512r1,
  54082. };
  54083. int knowEccNidsLen = (int)(sizeof(knownEccNids) / sizeof(*knownEccNids));
  54084. static const int knownEccEnums[] = {
  54085. ECC_SECP192R1,
  54086. ECC_PRIME192V2,
  54087. ECC_PRIME192V3,
  54088. ECC_PRIME239V1,
  54089. ECC_PRIME239V2,
  54090. ECC_PRIME239V3,
  54091. ECC_SECP256R1,
  54092. ECC_SECP112R1,
  54093. ECC_SECP112R2,
  54094. ECC_SECP128R1,
  54095. ECC_SECP128R2,
  54096. ECC_SECP160R1,
  54097. ECC_SECP160R2,
  54098. ECC_SECP224R1,
  54099. ECC_SECP384R1,
  54100. ECC_SECP521R1,
  54101. ECC_SECP160K1,
  54102. ECC_SECP192K1,
  54103. ECC_SECP224K1,
  54104. ECC_SECP256K1,
  54105. ECC_BRAINPOOLP160R1,
  54106. ECC_BRAINPOOLP192R1,
  54107. ECC_BRAINPOOLP224R1,
  54108. ECC_BRAINPOOLP256R1,
  54109. ECC_BRAINPOOLP320R1,
  54110. ECC_BRAINPOOLP384R1,
  54111. ECC_BRAINPOOLP512R1,
  54112. };
  54113. int knowEccEnumsLen = (int)(sizeof(knownEccEnums) / sizeof(*knownEccEnums));
  54114. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  54115. ExpectNotNull(group2 = EC_GROUP_dup(group));
  54116. ExpectNotNull(group3 = wolfSSL_EC_GROUP_new_by_curve_name(NID_secp384r1));
  54117. #ifndef HAVE_ECC_BRAINPOOL
  54118. ExpectNotNull(group4 = wolfSSL_EC_GROUP_new_by_curve_name(
  54119. NID_brainpoolP256r1));
  54120. #endif
  54121. ExpectNull(EC_GROUP_dup(NULL));
  54122. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(NULL), 0);
  54123. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  54124. ExpectIntEQ((group_bits = EC_GROUP_order_bits(NULL)), 0);
  54125. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group)), 256);
  54126. #ifndef HAVE_ECC_BRAINPOOL
  54127. ExpectIntEQ((group_bits = EC_GROUP_order_bits(group4)), 0);
  54128. #endif
  54129. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(NULL), 0);
  54130. ExpectIntEQ(wolfSSL_EC_GROUP_get_degree(group), 256);
  54131. ExpectNotNull(order = BN_new());
  54132. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, NULL, NULL), 0);
  54133. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, NULL, NULL), 0);
  54134. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(NULL, order, NULL), 0);
  54135. ExpectIntEQ(wolfSSL_EC_GROUP_get_order(group, order, NULL), 1);
  54136. wolfSSL_BN_free(order);
  54137. ExpectNotNull(EC_GROUP_method_of(group));
  54138. ExpectIntEQ(EC_METHOD_get_field_type(NULL), 0);
  54139. ExpectIntEQ(EC_METHOD_get_field_type(EC_GROUP_method_of(group)),
  54140. NID_X9_62_prime_field);
  54141. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, NULL, NULL), -1);
  54142. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, NULL, NULL), -1);
  54143. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(NULL, group, NULL), -1);
  54144. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(group, group3, NULL), 1);
  54145. #ifndef NO_WOLFSSL_STUB
  54146. wolfSSL_EC_GROUP_set_asn1_flag(group, OPENSSL_EC_NAMED_CURVE);
  54147. #endif
  54148. #ifndef HAVE_ECC_BRAINPOOL
  54149. EC_GROUP_free(group4);
  54150. #endif
  54151. EC_GROUP_free(group3);
  54152. EC_GROUP_free(group2);
  54153. EC_GROUP_free(group);
  54154. for (i = 0; i < knowEccNidsLen; i++) {
  54155. group = NULL;
  54156. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccNids[i]));
  54157. ExpectIntGT(wolfSSL_EC_GROUP_get_degree(group), 0);
  54158. EC_GROUP_free(group);
  54159. }
  54160. for (i = 0; i < knowEccEnumsLen; i++) {
  54161. group = NULL;
  54162. ExpectNotNull(group = EC_GROUP_new_by_curve_name(knownEccEnums[i]));
  54163. ExpectIntEQ(wolfSSL_EC_GROUP_get_curve_name(group), knownEccNids[i]);
  54164. EC_GROUP_free(group);
  54165. }
  54166. #endif
  54167. return EXPECT_RESULT();
  54168. }
  54169. static int test_wolfSSL_PEM_read_bio_ECPKParameters(void)
  54170. {
  54171. EXPECT_DECLS;
  54172. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  54173. EC_GROUP *group = NULL;
  54174. BIO* bio = NULL;
  54175. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  54176. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  54177. EC_GROUP *ret = NULL;
  54178. static char ec_nc_p384[] = "-----BEGIN EC PARAMETERS-----\n"
  54179. "BgUrgQQAIg==\n"
  54180. "-----END EC PARAMETERS-----";
  54181. #endif
  54182. static char ec_nc_bad_1[] = "-----BEGIN EC PARAMETERS-----\n"
  54183. "MAA=\n"
  54184. "-----END EC PARAMETERS-----";
  54185. static char ec_nc_bad_2[] = "-----BEGIN EC PARAMETERS-----\n"
  54186. "BgA=\n"
  54187. "-----END EC PARAMETERS-----";
  54188. static char ec_nc_bad_3[] = "-----BEGIN EC PARAMETERS-----\n"
  54189. "BgE=\n"
  54190. "-----END EC PARAMETERS-----";
  54191. static char ec_nc_bad_4[] = "-----BEGIN EC PARAMETERS-----\n"
  54192. "BgE*\n"
  54193. "-----END EC PARAMETERS-----";
  54194. /* Test that first parameter, bio, being NULL fails. */
  54195. ExpectNull(PEM_read_bio_ECPKParameters(NULL, NULL, NULL, NULL));
  54196. /* Test that reading named parameters works. */
  54197. ExpectNotNull(bio = BIO_new(BIO_s_file()));
  54198. ExpectIntEQ(BIO_read_filename(bio, eccKeyFile), WOLFSSL_SUCCESS);
  54199. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  54200. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_X9_62_prime256v1);
  54201. BIO_free(bio);
  54202. bio = NULL;
  54203. EC_GROUP_free(group);
  54204. group = NULL;
  54205. #if (defined(HAVE_ECC384) || defined(HAVE_ALL_CURVES)) && \
  54206. ECC_MIN_KEY_SZ <= 384 && !defined(NO_ECC_SECP)
  54207. /* Test that reusing group works. */
  54208. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  54209. sizeof(ec_nc_p384)));
  54210. ExpectNotNull(group = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  54211. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  54212. BIO_free(bio);
  54213. bio = NULL;
  54214. EC_GROUP_free(group);
  54215. group = NULL;
  54216. /* Test that returning through group works. */
  54217. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_p384,
  54218. sizeof(ec_nc_p384)));
  54219. ExpectNotNull(ret = PEM_read_bio_ECPKParameters(bio, &group, NULL, NULL));
  54220. ExpectIntEQ(group == ret, 1);
  54221. ExpectIntEQ(EC_GROUP_get_curve_name(group), NID_secp384r1);
  54222. BIO_free(bio);
  54223. bio = NULL;
  54224. EC_GROUP_free(group);
  54225. group = NULL;
  54226. #endif
  54227. /* Test 0x30, 0x00 (not and object id) fails. */
  54228. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_1,
  54229. sizeof(ec_nc_bad_1)));
  54230. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  54231. BIO_free(bio);
  54232. bio = NULL;
  54233. /* Test 0x06, 0x00 (empty object id) fails. */
  54234. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_2,
  54235. sizeof(ec_nc_bad_2)));
  54236. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  54237. BIO_free(bio);
  54238. bio = NULL;
  54239. /* Test 0x06, 0x01 (badly formed object id) fails. */
  54240. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_3,
  54241. sizeof(ec_nc_bad_3)));
  54242. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  54243. BIO_free(bio);
  54244. bio = NULL;
  54245. /* Test invalid PEM encoding - invalid character. */
  54246. ExpectNotNull(bio = BIO_new_mem_buf((unsigned char*)ec_nc_bad_4,
  54247. sizeof(ec_nc_bad_4)));
  54248. ExpectNull(PEM_read_bio_ECPKParameters(bio, NULL, NULL, NULL));
  54249. BIO_free(bio);
  54250. #endif
  54251. return EXPECT_RESULT();
  54252. }
  54253. static int test_wolfSSL_EC_POINT(void)
  54254. {
  54255. EXPECT_DECLS;
  54256. #if !defined(WOLFSSL_SP_MATH) && \
  54257. (!defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2)))
  54258. #ifdef OPENSSL_EXTRA
  54259. BN_CTX* ctx = NULL;
  54260. EC_GROUP* group = NULL;
  54261. #ifndef HAVE_ECC_BRAINPOOL
  54262. EC_GROUP* group2 = NULL;
  54263. #endif
  54264. EC_POINT* Gxy = NULL;
  54265. EC_POINT* new_point = NULL;
  54266. EC_POINT* set_point = NULL;
  54267. EC_POINT* infinity = NULL;
  54268. BIGNUM* k = NULL;
  54269. BIGNUM* Gx = NULL;
  54270. BIGNUM* Gy = NULL;
  54271. BIGNUM* Gz = NULL;
  54272. BIGNUM* X = NULL;
  54273. BIGNUM* Y = NULL;
  54274. BIGNUM* set_point_bn = NULL;
  54275. char* hexStr = NULL;
  54276. const char* kTest = "F4F8338AFCC562C5C3F3E1E46A7EFECD"
  54277. "17AF381913FF7A96314EA47055EA0FD0";
  54278. /* NISTP256R1 Gx/Gy */
  54279. const char* kGx = "6B17D1F2E12C4247F8BCE6E563A440F2"
  54280. "77037D812DEB33A0F4A13945D898C296";
  54281. const char* kGy = "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  54282. "2BCE33576B315ECECBB6406837BF51F5";
  54283. #ifndef HAVE_SELFTEST
  54284. EC_POINT *tmp = NULL;
  54285. size_t bin_len;
  54286. unsigned int blen = 0;
  54287. unsigned char* buf = NULL;
  54288. unsigned char bufInf[1] = { 0x00 };
  54289. const char* uncompG = "046B17D1F2E12C4247F8BCE6E563A440F2"
  54290. "77037D812DEB33A0F4A13945D898C296"
  54291. "4FE342E2FE1A7F9B8EE7EB4A7C0F9E16"
  54292. "2BCE33576B315ECECBB6406837BF51F5";
  54293. const unsigned char binUncompG[] = {
  54294. 0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  54295. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  54296. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  54297. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  54298. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  54299. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  54300. };
  54301. const unsigned char binUncompGBad[] = {
  54302. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  54303. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  54304. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  54305. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  54306. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  54307. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  54308. };
  54309. const char* compG = "036B17D1F2E12C4247F8BCE6E563A440F2"
  54310. "77037D812DEB33A0F4A13945D898C296";
  54311. #ifdef HAVE_COMP_KEY
  54312. const unsigned char binCompG[] = {
  54313. 0x03, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  54314. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  54315. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  54316. };
  54317. #endif
  54318. #endif
  54319. ExpectNotNull(ctx = BN_CTX_new());
  54320. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  54321. #ifndef HAVE_ECC_BRAINPOOL
  54322. /* Used to make groups curve_idx == -1. */
  54323. ExpectNotNull(group2 = EC_GROUP_new_by_curve_name(NID_brainpoolP256r1));
  54324. #endif
  54325. ExpectNull(EC_POINT_new(NULL));
  54326. ExpectNotNull(Gxy = EC_POINT_new(group));
  54327. ExpectNotNull(new_point = EC_POINT_new(group));
  54328. ExpectNotNull(set_point = EC_POINT_new(group));
  54329. ExpectNotNull(X = BN_new());
  54330. ExpectNotNull(Y = BN_new());
  54331. ExpectNotNull(set_point_bn = BN_new());
  54332. ExpectNotNull(infinity = EC_POINT_new(group));
  54333. /* load test values */
  54334. ExpectIntEQ(BN_hex2bn(&k, kTest), WOLFSSL_SUCCESS);
  54335. ExpectIntEQ(BN_hex2bn(&Gx, kGx), WOLFSSL_SUCCESS);
  54336. ExpectIntEQ(BN_hex2bn(&Gy, kGy), WOLFSSL_SUCCESS);
  54337. ExpectIntEQ(BN_hex2bn(&Gz, "1"), WOLFSSL_SUCCESS);
  54338. /* populate coordinates for input point */
  54339. if (Gxy != NULL) {
  54340. Gxy->X = Gx;
  54341. Gxy->Y = Gy;
  54342. Gxy->Z = Gz;
  54343. }
  54344. /* Test handling of NULL point. */
  54345. EC_POINT_clear_free(NULL);
  54346. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  54347. NULL, NULL, ctx), 0);
  54348. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  54349. NULL, NULL, ctx), 0);
  54350. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  54351. NULL, NULL, ctx), 0);
  54352. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  54353. X, NULL, ctx), 0);
  54354. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, NULL,
  54355. NULL, Y, ctx), 0);
  54356. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(NULL, Gxy,
  54357. X, Y, ctx), 0);
  54358. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, NULL,
  54359. X, Y, ctx), 0);
  54360. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  54361. NULL, Y, ctx), 0);
  54362. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, Gxy,
  54363. X, NULL, ctx), 0);
  54364. /* Getting point at infinity returns an error. */
  54365. ExpectIntEQ(wolfSSL_EC_POINT_get_affine_coordinates_GFp(group, infinity,
  54366. X, Y, ctx), 0);
  54367. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  54368. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  54369. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  54370. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, NULL, ctx), 0);
  54371. ExpectIntEQ(EC_POINT_add(group, NULL, NULL, NULL, ctx), 0);
  54372. ExpectIntEQ(EC_POINT_add(NULL, new_point, NULL, NULL, ctx), 0);
  54373. ExpectIntEQ(EC_POINT_add(NULL, NULL, new_point, NULL, ctx), 0);
  54374. ExpectIntEQ(EC_POINT_add(NULL, NULL, NULL, Gxy, ctx), 0);
  54375. ExpectIntEQ(EC_POINT_add(NULL, new_point, new_point, Gxy, ctx), 0);
  54376. ExpectIntEQ(EC_POINT_add(group, NULL, new_point, Gxy, ctx), 0);
  54377. ExpectIntEQ(EC_POINT_add(group, new_point, NULL, Gxy, ctx), 0);
  54378. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, NULL, ctx), 0);
  54379. ExpectIntEQ(EC_POINT_mul(NULL, NULL, Gx, Gxy, k, ctx), 0);
  54380. ExpectIntEQ(EC_POINT_mul(NULL, new_point, Gx, Gxy, k, ctx), 0);
  54381. ExpectIntEQ(EC_POINT_mul(group, NULL, Gx, Gxy, k, ctx), 0);
  54382. ExpectIntEQ(EC_POINT_add(group, new_point, new_point, Gxy, ctx), 1);
  54383. /* perform point multiplication */
  54384. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, Gxy, k, ctx), 1);
  54385. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  54386. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  54387. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  54388. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, Gxy, k, ctx), 1);
  54389. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  54390. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  54391. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  54392. ExpectIntEQ(EC_POINT_mul(group, new_point, Gx, NULL, NULL, ctx), 1);
  54393. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  54394. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  54395. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  54396. ExpectIntEQ(EC_POINT_mul(group, new_point, NULL, NULL, NULL, ctx), 1);
  54397. ExpectIntEQ(BN_is_zero(new_point->X), 1);
  54398. ExpectIntEQ(BN_is_zero(new_point->Y), 1);
  54399. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  54400. /* Set point to something. */
  54401. ExpectIntEQ(EC_POINT_add(group, new_point, Gxy, Gxy, ctx), 1);
  54402. #else
  54403. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, new_point, Gx, Gy,
  54404. ctx), 1);
  54405. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  54406. ExpectIntEQ(BN_is_zero(new_point->Y), 0);
  54407. ExpectIntEQ(BN_is_zero(new_point->Z), 0);
  54408. #endif
  54409. /* check if point X coordinate is zero */
  54410. ExpectIntEQ(BN_is_zero(new_point->X), 0);
  54411. #if defined(USE_ECC_B_PARAM) && !defined(HAVE_SELFTEST) && \
  54412. (!defined(HAVE_FIPS) || FIPS_VERSION_GT(2,0))
  54413. ExpectIntEQ(EC_POINT_is_on_curve(group, new_point, ctx), 1);
  54414. #endif
  54415. /* extract the coordinates from point */
  54416. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  54417. ctx), WOLFSSL_SUCCESS);
  54418. /* check if point X coordinate is zero */
  54419. ExpectIntEQ(BN_is_zero(X), WOLFSSL_FAILURE);
  54420. /* set the same X and Y points in another object */
  54421. ExpectIntEQ(EC_POINT_set_affine_coordinates_GFp(group, set_point, X, Y,
  54422. ctx), WOLFSSL_SUCCESS);
  54423. /* compare points as they should be the same */
  54424. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, NULL, ctx), -1);
  54425. ExpectIntEQ(EC_POINT_cmp(group, NULL, NULL, ctx), -1);
  54426. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, NULL, ctx), -1);
  54427. ExpectIntEQ(EC_POINT_cmp(NULL, NULL, set_point, ctx), -1);
  54428. ExpectIntEQ(EC_POINT_cmp(NULL, new_point, set_point, ctx), -1);
  54429. ExpectIntEQ(EC_POINT_cmp(group, NULL, set_point, ctx), -1);
  54430. ExpectIntEQ(EC_POINT_cmp(group, new_point, NULL, ctx), -1);
  54431. ExpectIntEQ(EC_POINT_cmp(group, new_point, set_point, ctx), 0);
  54432. /* Test copying */
  54433. ExpectIntEQ(EC_POINT_copy(NULL, NULL), 0);
  54434. ExpectIntEQ(EC_POINT_copy(NULL, set_point), 0);
  54435. ExpectIntEQ(EC_POINT_copy(new_point, NULL), 0);
  54436. ExpectIntEQ(EC_POINT_copy(new_point, set_point), 1);
  54437. /* Test inverting */
  54438. ExpectIntEQ(EC_POINT_invert(NULL, NULL, ctx), 0);
  54439. ExpectIntEQ(EC_POINT_invert(NULL, new_point, ctx), 0);
  54440. ExpectIntEQ(EC_POINT_invert(group, NULL, ctx), 0);
  54441. ExpectIntEQ(EC_POINT_invert(group, new_point, ctx), 1);
  54442. #if !defined(WOLFSSL_ATECC508A) && !defined(WOLFSSL_ATECC608A) && \
  54443. !defined(HAVE_SELFTEST) && !defined(WOLFSSL_SP_MATH) && \
  54444. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  54445. {
  54446. EC_POINT* orig_point = NULL;
  54447. ExpectNotNull(orig_point = EC_POINT_new(group));
  54448. ExpectIntEQ(EC_POINT_add(group, orig_point, set_point, set_point, NULL),
  54449. 1);
  54450. /* new_point should be set_point inverted so adding it will revert
  54451. * the point back to set_point */
  54452. ExpectIntEQ(EC_POINT_add(group, orig_point, orig_point, new_point,
  54453. NULL), 1);
  54454. ExpectIntEQ(EC_POINT_cmp(group, orig_point, set_point, NULL), 0);
  54455. EC_POINT_free(orig_point);
  54456. }
  54457. #endif
  54458. /* Test getting affine converts from projective. */
  54459. ExpectIntEQ(EC_POINT_copy(set_point, new_point), 1);
  54460. /* Force non-affine coordinates */
  54461. ExpectIntEQ(BN_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  54462. (WOLFSSL_BIGNUM*)BN_value_one()), 1);
  54463. if (new_point != NULL) {
  54464. new_point->inSet = 0;
  54465. }
  54466. /* extract the coordinates from point */
  54467. ExpectIntEQ(EC_POINT_get_affine_coordinates_GFp(group, new_point, X, Y,
  54468. ctx), WOLFSSL_SUCCESS);
  54469. /* check if point ordinates have changed. */
  54470. ExpectIntNE(BN_cmp(X, set_point->X), 0);
  54471. ExpectIntNE(BN_cmp(Y, set_point->Y), 0);
  54472. /* Test check for infinity */
  54473. #ifndef WOLF_CRYPTO_CB_ONLY_ECC
  54474. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, NULL), 0);
  54475. ExpectIntEQ(EC_POINT_is_at_infinity(NULL, infinity), 0);
  54476. ExpectIntEQ(EC_POINT_is_at_infinity(group, NULL), 0);
  54477. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 1);
  54478. ExpectIntEQ(EC_POINT_is_at_infinity(group, Gxy), 0);
  54479. #else
  54480. ExpectIntEQ(EC_POINT_is_at_infinity(group, infinity), 0);
  54481. #endif
  54482. ExpectPtrEq(EC_POINT_point2bn(group, set_point,
  54483. POINT_CONVERSION_UNCOMPRESSED, set_point_bn, ctx), set_point_bn);
  54484. /* check bn2hex */
  54485. hexStr = BN_bn2hex(k);
  54486. ExpectStrEQ(hexStr, kTest);
  54487. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  54488. defined(XFPRINTF)
  54489. BN_print_fp(stderr, k);
  54490. fprintf(stderr, "\n");
  54491. #endif
  54492. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  54493. hexStr = BN_bn2hex(Gx);
  54494. ExpectStrEQ(hexStr, kGx);
  54495. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  54496. defined(XFPRINTF)
  54497. BN_print_fp(stderr, Gx);
  54498. fprintf(stderr, "\n");
  54499. #endif
  54500. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  54501. hexStr = BN_bn2hex(Gy);
  54502. ExpectStrEQ(hexStr, kGy);
  54503. #if !defined(NO_FILESYSTEM) && !defined(NO_STDIO_FILESYSTEM) && \
  54504. defined(XFPRINTF)
  54505. BN_print_fp(stderr, Gy);
  54506. fprintf(stderr, "\n");
  54507. #endif
  54508. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  54509. #ifndef HAVE_SELFTEST
  54510. /* Test point to hex */
  54511. ExpectNull(EC_POINT_point2hex(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  54512. ctx));
  54513. ExpectNull(EC_POINT_point2hex(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  54514. ctx));
  54515. ExpectNull(EC_POINT_point2hex(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  54516. ctx));
  54517. #ifndef HAVE_ECC_BRAINPOOL
  54518. /* Group not supported in wolfCrypt. */
  54519. ExpectNull(EC_POINT_point2hex(group2, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  54520. ctx));
  54521. #endif
  54522. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_UNCOMPRESSED, ctx);
  54523. ExpectNotNull(hexStr);
  54524. ExpectStrEQ(hexStr, uncompG);
  54525. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  54526. hexStr = EC_POINT_point2hex(group, Gxy, POINT_CONVERSION_COMPRESSED, ctx);
  54527. ExpectNotNull(hexStr);
  54528. ExpectStrEQ(hexStr, compG);
  54529. XFREE(hexStr, NULL, DYNAMIC_TYPE_ECC);
  54530. /* Test point to oct */
  54531. ExpectIntEQ(EC_POINT_point2oct(NULL, NULL, POINT_CONVERSION_UNCOMPRESSED,
  54532. NULL, 0, ctx), 0);
  54533. ExpectIntEQ(EC_POINT_point2oct(NULL, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  54534. NULL, 0, ctx), 0);
  54535. ExpectIntEQ(EC_POINT_point2oct(group, NULL, POINT_CONVERSION_UNCOMPRESSED,
  54536. NULL, 0, ctx), 0);
  54537. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  54538. NULL, 0, ctx);
  54539. ExpectIntEQ(bin_len, sizeof(binUncompG));
  54540. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  54541. DYNAMIC_TYPE_ECC));
  54542. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_UNCOMPRESSED,
  54543. buf, bin_len, ctx), bin_len);
  54544. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  54545. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  54546. /* Infinity (x=0, y=0) encodes as '0x00'. */
  54547. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  54548. POINT_CONVERSION_UNCOMPRESSED, NULL, 0, ctx), 1);
  54549. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  54550. POINT_CONVERSION_UNCOMPRESSED, bufInf, 0, ctx), 0);
  54551. ExpectIntEQ(EC_POINT_point2oct(group, infinity,
  54552. POINT_CONVERSION_UNCOMPRESSED, bufInf, 1, ctx), 1);
  54553. ExpectIntEQ(bufInf[0], 0);
  54554. wolfSSL_EC_POINT_dump(NULL, NULL);
  54555. /* Test point i2d */
  54556. ExpectIntEQ(ECPoint_i2d(NULL, NULL, NULL, &blen), 0);
  54557. ExpectIntEQ(ECPoint_i2d(NULL, Gxy, NULL, &blen), 0);
  54558. ExpectIntEQ(ECPoint_i2d(group, NULL, NULL, &blen), 0);
  54559. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, NULL), 0);
  54560. ExpectIntEQ(ECPoint_i2d(group, Gxy, NULL, &blen), 1);
  54561. ExpectIntEQ(blen, sizeof(binUncompG));
  54562. ExpectNotNull(buf = (unsigned char*)XMALLOC(blen, NULL, DYNAMIC_TYPE_ECC));
  54563. blen -= 1;
  54564. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 0);
  54565. blen += 1;
  54566. ExpectIntEQ(ECPoint_i2d(group, Gxy, buf, &blen), 1);
  54567. ExpectIntEQ(XMEMCMP(buf, binUncompG, sizeof(binUncompG)), 0);
  54568. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  54569. #ifdef HAVE_COMP_KEY
  54570. /* Test point to oct compressed */
  54571. bin_len = EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, NULL,
  54572. 0, ctx);
  54573. ExpectIntEQ(bin_len, sizeof(binCompG));
  54574. ExpectNotNull(buf = (unsigned char*)XMALLOC(bin_len, NULL,
  54575. DYNAMIC_TYPE_ECC));
  54576. ExpectIntEQ(EC_POINT_point2oct(group, Gxy, POINT_CONVERSION_COMPRESSED, buf,
  54577. bin_len, ctx), bin_len);
  54578. ExpectIntEQ(XMEMCMP(buf, binCompG, sizeof(binCompG)), 0);
  54579. XFREE(buf, NULL, DYNAMIC_TYPE_ECC);
  54580. #endif
  54581. /* Test point BN */
  54582. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, NULL,
  54583. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  54584. ExpectNull(wolfSSL_EC_POINT_point2bn(NULL, Gxy,
  54585. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  54586. ExpectNull(wolfSSL_EC_POINT_point2bn(group, NULL,
  54587. POINT_CONVERSION_UNCOMPRESSED, NULL, ctx));
  54588. ExpectNull(wolfSSL_EC_POINT_point2bn(group, Gxy, 0, NULL, ctx));
  54589. /* Test oct to point */
  54590. ExpectNotNull(tmp = EC_POINT_new(group));
  54591. ExpectIntEQ(EC_POINT_oct2point(NULL, NULL, binUncompG, sizeof(binUncompG),
  54592. ctx), 0);
  54593. ExpectIntEQ(EC_POINT_oct2point(NULL, tmp, binUncompG, sizeof(binUncompG),
  54594. ctx), 0);
  54595. ExpectIntEQ(EC_POINT_oct2point(group, NULL, binUncompG, sizeof(binUncompG),
  54596. ctx), 0);
  54597. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompGBad,
  54598. sizeof(binUncompGBad), ctx), 0);
  54599. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binUncompG, sizeof(binUncompG),
  54600. ctx), 1);
  54601. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  54602. EC_POINT_free(tmp);
  54603. tmp = NULL;
  54604. /* Test setting BN ordinates. */
  54605. ExpectNotNull(tmp = EC_POINT_new(group));
  54606. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  54607. NULL, ctx), 0);
  54608. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, NULL,
  54609. NULL, ctx), 0);
  54610. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, NULL,
  54611. NULL, ctx), 0);
  54612. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, Gx,
  54613. NULL, ctx), 0);
  54614. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, NULL, NULL,
  54615. Gy, ctx), 0);
  54616. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(NULL, tmp, Gx, Gy,
  54617. ctx), 0);
  54618. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, NULL, Gx, Gy,
  54619. ctx), 0);
  54620. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, NULL,
  54621. Gy, ctx), 0);
  54622. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx,
  54623. NULL, ctx), 0);
  54624. ExpectIntEQ(wolfSSL_EC_POINT_set_affine_coordinates_GFp(group, tmp, Gx, Gy,
  54625. ctx), 1);
  54626. EC_POINT_free(tmp);
  54627. tmp = NULL;
  54628. /* Test point d2i */
  54629. ExpectNotNull(tmp = EC_POINT_new(group));
  54630. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, NULL), 0);
  54631. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, NULL), 0);
  54632. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, NULL), 0);
  54633. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), NULL, tmp), 0);
  54634. ExpectIntEQ(ECPoint_d2i(NULL, sizeof(binUncompG), group, tmp), 0);
  54635. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), NULL, tmp), 0);
  54636. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, NULL), 0);
  54637. ExpectIntEQ(ECPoint_d2i(binUncompGBad, sizeof(binUncompG), group, tmp), 0);
  54638. ExpectIntEQ(ECPoint_d2i(binUncompG, sizeof(binUncompG), group, tmp), 1);
  54639. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  54640. EC_POINT_free(tmp);
  54641. tmp = NULL;
  54642. #ifdef HAVE_COMP_KEY
  54643. /* Test oct compressed to point */
  54644. ExpectNotNull(tmp = EC_POINT_new(group));
  54645. ExpectIntEQ(EC_POINT_oct2point(group, tmp, binCompG, sizeof(binCompG), ctx),
  54646. 1);
  54647. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  54648. EC_POINT_free(tmp);
  54649. tmp = NULL;
  54650. /* Test point d2i - compressed */
  54651. ExpectNotNull(tmp = EC_POINT_new(group));
  54652. ExpectIntEQ(ECPoint_d2i(binCompG, sizeof(binCompG), group, tmp), 1);
  54653. ExpectIntEQ(EC_POINT_cmp(group, tmp, Gxy, ctx), 0);
  54654. EC_POINT_free(tmp);
  54655. tmp = NULL;
  54656. #endif
  54657. #endif
  54658. /* test BN_mod_add */
  54659. ExpectIntEQ(BN_mod_add(new_point->Z, (WOLFSSL_BIGNUM*)BN_value_one(),
  54660. (WOLFSSL_BIGNUM*)BN_value_one(), (WOLFSSL_BIGNUM*)BN_value_one(), NULL),
  54661. 1);
  54662. ExpectIntEQ(BN_is_zero(new_point->Z), 1);
  54663. /* cleanup */
  54664. BN_free(X);
  54665. BN_free(Y);
  54666. BN_free(k);
  54667. BN_free(set_point_bn);
  54668. EC_POINT_free(infinity);
  54669. EC_POINT_free(new_point);
  54670. EC_POINT_free(set_point);
  54671. EC_POINT_clear_free(Gxy);
  54672. #ifndef HAVE_ECC_BRAINPOOL
  54673. EC_GROUP_free(group2);
  54674. #endif
  54675. EC_GROUP_free(group);
  54676. BN_CTX_free(ctx);
  54677. #endif
  54678. #endif /* !WOLFSSL_SP_MATH && ( !HAVE_FIPS || HAVE_FIPS_VERSION > 2) */
  54679. return EXPECT_RESULT();
  54680. }
  54681. static int test_wolfSSL_SPAKE(void)
  54682. {
  54683. EXPECT_DECLS;
  54684. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && !defined(WOLFSSL_ATECC508A) \
  54685. && !defined(WOLFSSL_ATECC608A) && !defined(HAVE_SELFTEST) && \
  54686. !defined(WOLFSSL_SP_MATH) && !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  54687. BIGNUM* x = NULL; /* kdc priv */
  54688. BIGNUM* y = NULL; /* client priv */
  54689. BIGNUM* w = NULL; /* shared value */
  54690. byte M_bytes[] = {
  54691. /* uncompressed */
  54692. 0x04,
  54693. /* x */
  54694. 0x88, 0x6e, 0x2f, 0x97, 0xac, 0xe4, 0x6e, 0x55, 0xba, 0x9d, 0xd7, 0x24,
  54695. 0x25, 0x79, 0xf2, 0x99, 0x3b, 0x64, 0xe1, 0x6e, 0xf3, 0xdc, 0xab, 0x95,
  54696. 0xaf, 0xd4, 0x97, 0x33, 0x3d, 0x8f, 0xa1, 0x2f,
  54697. /* y */
  54698. 0x5f, 0xf3, 0x55, 0x16, 0x3e, 0x43, 0xce, 0x22, 0x4e, 0x0b, 0x0e, 0x65,
  54699. 0xff, 0x02, 0xac, 0x8e, 0x5c, 0x7b, 0xe0, 0x94, 0x19, 0xc7, 0x85, 0xe0,
  54700. 0xca, 0x54, 0x7d, 0x55, 0xa1, 0x2e, 0x2d, 0x20
  54701. };
  54702. EC_POINT* M = NULL; /* shared value */
  54703. byte N_bytes[] = {
  54704. /* uncompressed */
  54705. 0x04,
  54706. /* x */
  54707. 0xd8, 0xbb, 0xd6, 0xc6, 0x39, 0xc6, 0x29, 0x37, 0xb0, 0x4d, 0x99, 0x7f,
  54708. 0x38, 0xc3, 0x77, 0x07, 0x19, 0xc6, 0x29, 0xd7, 0x01, 0x4d, 0x49, 0xa2,
  54709. 0x4b, 0x4f, 0x98, 0xba, 0xa1, 0x29, 0x2b, 0x49,
  54710. /* y */
  54711. 0x07, 0xd6, 0x0a, 0xa6, 0xbf, 0xad, 0xe4, 0x50, 0x08, 0xa6, 0x36, 0x33,
  54712. 0x7f, 0x51, 0x68, 0xc6, 0x4d, 0x9b, 0xd3, 0x60, 0x34, 0x80, 0x8c, 0xd5,
  54713. 0x64, 0x49, 0x0b, 0x1e, 0x65, 0x6e, 0xdb, 0xe7
  54714. };
  54715. EC_POINT* N = NULL; /* shared value */
  54716. EC_POINT* T = NULL; /* kdc pub */
  54717. EC_POINT* tmp1 = NULL; /* kdc pub */
  54718. EC_POINT* tmp2 = NULL; /* kdc pub */
  54719. EC_POINT* S = NULL; /* client pub */
  54720. EC_POINT* client_secret = NULL;
  54721. EC_POINT* kdc_secret = NULL;
  54722. EC_GROUP* group = NULL;
  54723. BN_CTX* bn_ctx = NULL;
  54724. /* Values taken from a test run of Kerberos 5 */
  54725. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  54726. ExpectNotNull(bn_ctx = BN_CTX_new());
  54727. ExpectNotNull(M = EC_POINT_new(group));
  54728. ExpectNotNull(N = EC_POINT_new(group));
  54729. ExpectNotNull(T = EC_POINT_new(group));
  54730. ExpectNotNull(tmp1 = EC_POINT_new(group));
  54731. ExpectNotNull(tmp2 = EC_POINT_new(group));
  54732. ExpectNotNull(S = EC_POINT_new(group));
  54733. ExpectNotNull(client_secret = EC_POINT_new(group));
  54734. ExpectNotNull(kdc_secret = EC_POINT_new(group));
  54735. ExpectIntEQ(BN_hex2bn(&x, "DAC3027CD692B4BDF0EDFE9B7D0E4E7"
  54736. "E5D8768A725EAEEA6FC68EC239A17C0"), 1);
  54737. ExpectIntEQ(BN_hex2bn(&y, "6F6A1D394E26B1655A54B26DCE30D49"
  54738. "90CC47EBE08F809EF3FF7F6AEAABBB5"), 1);
  54739. ExpectIntEQ(BN_hex2bn(&w, "1D992AB8BA851B9BA05353453D81EE9"
  54740. "506AB395478F0AAB647752CF117B36250"), 1);
  54741. ExpectIntEQ(EC_POINT_oct2point(group, M, M_bytes, sizeof(M_bytes), bn_ctx),
  54742. 1);
  54743. ExpectIntEQ(EC_POINT_oct2point(group, N, N_bytes, sizeof(N_bytes), bn_ctx),
  54744. 1);
  54745. /* Function pattern similar to ossl_keygen and ossl_result in krb5 */
  54746. /* kdc */
  54747. /* T=x*P+w*M */
  54748. /* All in one function call */
  54749. ExpectIntEQ(EC_POINT_mul(group, T, x, M, w, bn_ctx), 1);
  54750. /* Spread into separate calls */
  54751. ExpectIntEQ(EC_POINT_mul(group, tmp1, x, NULL, NULL, bn_ctx), 1);
  54752. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, M, w, bn_ctx), 1);
  54753. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  54754. 1);
  54755. ExpectIntEQ(EC_POINT_cmp(group, T, tmp1, bn_ctx), 0);
  54756. /* client */
  54757. /* S=y*P+w*N */
  54758. /* All in one function call */
  54759. ExpectIntEQ(EC_POINT_mul(group, S, y, N, w, bn_ctx), 1);
  54760. /* Spread into separate calls */
  54761. ExpectIntEQ(EC_POINT_mul(group, tmp1, y, NULL, NULL, bn_ctx), 1);
  54762. ExpectIntEQ(EC_POINT_mul(group, tmp2, NULL, N, w, bn_ctx), 1);
  54763. ExpectIntEQ(EC_POINT_add(group, tmp1, tmp1, tmp2, bn_ctx),
  54764. 1);
  54765. ExpectIntEQ(EC_POINT_cmp(group, S, tmp1, bn_ctx), 0);
  54766. /* K=y*(T-w*M) */
  54767. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, M, w, bn_ctx), 1);
  54768. ExpectIntEQ(EC_POINT_invert(group, client_secret, bn_ctx), 1);
  54769. ExpectIntEQ(EC_POINT_add(group, client_secret, T, client_secret, bn_ctx),
  54770. 1);
  54771. ExpectIntEQ(EC_POINT_mul(group, client_secret, NULL, client_secret, y,
  54772. bn_ctx), 1);
  54773. /* kdc */
  54774. /* K=x*(S-w*N) */
  54775. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, N, w, bn_ctx), 1);
  54776. ExpectIntEQ(EC_POINT_invert(group, kdc_secret, bn_ctx), 1);
  54777. ExpectIntEQ(EC_POINT_add(group, kdc_secret, S, kdc_secret, bn_ctx),
  54778. 1);
  54779. ExpectIntEQ(EC_POINT_mul(group, kdc_secret, NULL, kdc_secret, x, bn_ctx),
  54780. 1);
  54781. /* kdc_secret == client_secret */
  54782. ExpectIntEQ(EC_POINT_cmp(group, client_secret, kdc_secret, bn_ctx), 0);
  54783. BN_free(x);
  54784. BN_free(y);
  54785. BN_free(w);
  54786. EC_POINT_free(M);
  54787. EC_POINT_free(N);
  54788. EC_POINT_free(T);
  54789. EC_POINT_free(tmp1);
  54790. EC_POINT_free(tmp2);
  54791. EC_POINT_free(S);
  54792. EC_POINT_free(client_secret);
  54793. EC_POINT_free(kdc_secret);
  54794. EC_GROUP_free(group);
  54795. BN_CTX_free(bn_ctx);
  54796. #endif
  54797. return EXPECT_RESULT();
  54798. }
  54799. static int test_wolfSSL_EC_KEY_generate(void)
  54800. {
  54801. EXPECT_DECLS;
  54802. #ifdef OPENSSL_EXTRA
  54803. WOLFSSL_EC_KEY* key = NULL;
  54804. #ifndef HAVE_ECC_BRAINPOOL
  54805. WOLFSSL_EC_GROUP* group = NULL;
  54806. #endif
  54807. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54808. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(NULL), 0);
  54809. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  54810. wolfSSL_EC_KEY_free(key);
  54811. key = NULL;
  54812. #ifndef HAVE_ECC_BRAINPOOL
  54813. ExpectNotNull(group = wolfSSL_EC_GROUP_new_by_curve_name(
  54814. NID_brainpoolP256r1));
  54815. ExpectNotNull(key = wolfSSL_EC_KEY_new());
  54816. ExpectIntEQ(wolfSSL_EC_KEY_set_group(key, group), 1);
  54817. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 0);
  54818. wolfSSL_EC_KEY_free(key);
  54819. wolfSSL_EC_GROUP_free(group);
  54820. #endif
  54821. #endif
  54822. return EXPECT_RESULT();
  54823. }
  54824. static int test_EC_i2d(void)
  54825. {
  54826. EXPECT_DECLS;
  54827. #if defined(OPENSSL_EXTRA) && !defined(HAVE_FIPS)
  54828. EC_KEY *key = NULL;
  54829. EC_KEY *copy = NULL;
  54830. int len = 0;
  54831. unsigned char *buf = NULL;
  54832. unsigned char *p = NULL;
  54833. const unsigned char *tmp = NULL;
  54834. const unsigned char octBad[] = {
  54835. 0x09, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8, 0xbc,
  54836. 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d, 0x81, 0x2d,
  54837. 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
  54838. 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, 0x8e, 0xe7, 0xeb,
  54839. 0x4a, 0x7c, 0x0f, 0x9e, 0x16, 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31,
  54840. 0x5e, 0xce, 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5,
  54841. };
  54842. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  54843. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  54844. ExpectIntGT((len = i2d_EC_PUBKEY(key, NULL)), 0);
  54845. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  54846. DYNAMIC_TYPE_TMP_BUFFER));
  54847. p = buf;
  54848. ExpectIntEQ(i2d_EC_PUBKEY(key, &p), len);
  54849. ExpectNull(o2i_ECPublicKey(NULL, NULL, -1));
  54850. ExpectNull(o2i_ECPublicKey(&copy, NULL, -1));
  54851. ExpectNull(o2i_ECPublicKey(&key, NULL, -1));
  54852. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  54853. ExpectNull(o2i_ECPublicKey(NULL, NULL, 0));
  54854. ExpectNull(o2i_ECPublicKey(&key, NULL, 0));
  54855. ExpectNull(o2i_ECPublicKey(&key, &tmp, 0));
  54856. tmp = buf;
  54857. ExpectNull(o2i_ECPublicKey(NULL, &tmp, 0));
  54858. ExpectNull(o2i_ECPublicKey(&copy, &tmp, 0));
  54859. ExpectNull(o2i_ECPublicKey(NULL, &tmp, -1));
  54860. ExpectNull(o2i_ECPublicKey(&key, &tmp, -1));
  54861. ExpectIntEQ(i2o_ECPublicKey(NULL, NULL), 0);
  54862. ExpectIntEQ(i2o_ECPublicKey(NULL, &buf), 0);
  54863. tmp = buf;
  54864. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 0));
  54865. ExpectNull(d2i_ECPrivateKey(NULL, &tmp, 1));
  54866. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 0));
  54867. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, 1));
  54868. ExpectNull(d2i_ECPrivateKey(&key, &tmp, 0));
  54869. ExpectIntEQ(i2d_ECPrivateKey(NULL, &p), 0);
  54870. ExpectIntEQ(i2d_ECPrivateKey(NULL, NULL), 0);
  54871. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer(NULL, NULL, -1), -1);
  54872. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1, 0), -1);
  54873. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, -1, 0), -1);
  54874. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, -1, 0), -1);
  54875. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, 0, 0), -1);
  54876. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, NULL, -1,
  54877. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  54878. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(NULL, buf, len,
  54879. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  54880. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, NULL, len,
  54881. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  54882. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, -1,
  54883. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  54884. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len, 0), -1);
  54885. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, buf, len,
  54886. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  54887. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  54888. WOLFSSL_EC_KEY_LOAD_PRIVATE), -1);
  54889. ExpectIntEQ(wolfSSL_EC_KEY_LoadDer_ex(key, octBad, sizeof(octBad),
  54890. WOLFSSL_EC_KEY_LOAD_PUBLIC), -1);
  54891. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54892. buf = NULL;
  54893. buf = NULL;
  54894. ExpectIntGT((len = i2d_ECPrivateKey(key, NULL)), 0);
  54895. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  54896. DYNAMIC_TYPE_TMP_BUFFER));
  54897. p = buf;
  54898. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  54899. p = NULL;
  54900. ExpectIntEQ(i2d_ECPrivateKey(key, &p), len);
  54901. XFREE(p, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54902. p = NULL;
  54903. /* Bad point is also an invalid private key. */
  54904. tmp = octBad;
  54905. ExpectNull(d2i_ECPrivateKey(&copy, &tmp, sizeof(octBad)));
  54906. tmp = buf;
  54907. ExpectNotNull(d2i_ECPrivateKey(&copy, &tmp, len));
  54908. XFREE(buf, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  54909. buf = NULL;
  54910. buf = NULL;
  54911. ExpectIntGT((len = i2o_ECPublicKey(key, NULL)), 0);
  54912. ExpectNotNull(buf = (unsigned char*)XMALLOC(len, NULL,
  54913. DYNAMIC_TYPE_TMP_BUFFER));
  54914. p = buf;
  54915. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  54916. p = NULL;
  54917. ExpectIntGT((len = i2o_ECPublicKey(key, &p)), 0);
  54918. tmp = buf;
  54919. ExpectNotNull(o2i_ECPublicKey(&copy, &tmp, len));
  54920. tmp = octBad;
  54921. ExpectNull(o2i_ECPublicKey(&key, &tmp, sizeof(octBad)));
  54922. ExpectIntEQ(EC_KEY_check_key(NULL), 0);
  54923. ExpectIntEQ(EC_KEY_check_key(key), 1);
  54924. XFREE(p, NULL, DYNAMIC_TYPE_OPENSSL);
  54925. XFREE(buf, NULL, DYNAMIC_TYPE_OPENSSL);
  54926. EC_KEY_free(key);
  54927. EC_KEY_free(copy);
  54928. #endif
  54929. return EXPECT_RESULT();
  54930. }
  54931. static int test_wolfSSL_EC_curve(void)
  54932. {
  54933. EXPECT_DECLS;
  54934. #if defined(OPENSSL_EXTRA)
  54935. int nid = NID_secp160k1;
  54936. const char* nid_name = NULL;
  54937. ExpectNull(EC_curve_nid2nist(NID_sha256));
  54938. ExpectNotNull(nid_name = EC_curve_nid2nist(nid));
  54939. ExpectIntEQ(XMEMCMP(nid_name, "K-160", XSTRLEN("K-160")), 0);
  54940. ExpectIntEQ(EC_curve_nist2nid("INVALID"), 0);
  54941. ExpectIntEQ(EC_curve_nist2nid(nid_name), nid);
  54942. #endif
  54943. return EXPECT_RESULT();
  54944. }
  54945. static int test_wolfSSL_EC_KEY_dup(void)
  54946. {
  54947. EXPECT_DECLS;
  54948. #if defined(OPENSSL_ALL) && !defined(NO_CERTS)
  54949. WOLFSSL_EC_KEY* ecKey = NULL;
  54950. WOLFSSL_EC_KEY* dupKey = NULL;
  54951. ecc_key* srcKey = NULL;
  54952. ecc_key* destKey = NULL;
  54953. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  54954. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  54955. /* Valid cases */
  54956. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54957. ExpectIntEQ(EC_KEY_check_key(dupKey), 1);
  54958. /* Compare pubkey */
  54959. if (ecKey != NULL) {
  54960. srcKey = (ecc_key*)ecKey->internal;
  54961. }
  54962. if (dupKey != NULL) {
  54963. destKey = (ecc_key*)dupKey->internal;
  54964. }
  54965. ExpectIntEQ(wc_ecc_cmp_point(&srcKey->pubkey, &destKey->pubkey), 0);
  54966. /* compare EC_GROUP */
  54967. ExpectIntEQ(wolfSSL_EC_GROUP_cmp(ecKey->group, dupKey->group, NULL), MP_EQ);
  54968. /* compare EC_POINT */
  54969. ExpectIntEQ(wolfSSL_EC_POINT_cmp(ecKey->group, ecKey->pub_key, \
  54970. dupKey->pub_key, NULL), MP_EQ);
  54971. /* compare BIGNUM */
  54972. ExpectIntEQ(wolfSSL_BN_cmp(ecKey->priv_key, dupKey->priv_key), MP_EQ);
  54973. wolfSSL_EC_KEY_free(dupKey);
  54974. dupKey = NULL;
  54975. /* Invalid cases */
  54976. /* NULL key */
  54977. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(NULL));
  54978. /* NULL ecc_key */
  54979. if (ecKey != NULL) {
  54980. wc_ecc_free((ecc_key*)ecKey->internal);
  54981. XFREE(ecKey->internal, NULL, DYNAMIC_TYPE_ECC);
  54982. ecKey->internal = NULL; /* Set ecc_key to NULL */
  54983. }
  54984. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54985. wolfSSL_EC_KEY_free(ecKey);
  54986. ecKey = NULL;
  54987. wolfSSL_EC_KEY_free(dupKey);
  54988. dupKey = NULL;
  54989. /* NULL Group */
  54990. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  54991. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  54992. if (ecKey != NULL) {
  54993. wolfSSL_EC_GROUP_free(ecKey->group);
  54994. ecKey->group = NULL; /* Set group to NULL */
  54995. }
  54996. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  54997. wolfSSL_EC_KEY_free(ecKey);
  54998. ecKey = NULL;
  54999. wolfSSL_EC_KEY_free(dupKey);
  55000. dupKey = NULL;
  55001. /* NULL public key */
  55002. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  55003. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  55004. if (ecKey != NULL) {
  55005. wc_ecc_del_point((ecc_point*)ecKey->pub_key->internal);
  55006. ecKey->pub_key->internal = NULL; /* Set ecc_point to NULL */
  55007. }
  55008. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  55009. if (ecKey != NULL) {
  55010. wolfSSL_EC_POINT_free(ecKey->pub_key);
  55011. ecKey->pub_key = NULL; /* Set pub_key to NULL */
  55012. }
  55013. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  55014. wolfSSL_EC_KEY_free(ecKey);
  55015. ecKey = NULL;
  55016. wolfSSL_EC_KEY_free(dupKey);
  55017. dupKey = NULL;
  55018. /* NULL private key */
  55019. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  55020. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), 1);
  55021. if (ecKey != NULL) {
  55022. wolfSSL_BN_free(ecKey->priv_key);
  55023. ecKey->priv_key = NULL; /* Set priv_key to NULL */
  55024. }
  55025. ExpectNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  55026. wolfSSL_EC_KEY_free(ecKey);
  55027. ecKey = NULL;
  55028. wolfSSL_EC_KEY_free(dupKey);
  55029. dupKey = NULL;
  55030. /* Test EC_KEY_up_ref */
  55031. ExpectNotNull(ecKey = wolfSSL_EC_KEY_new());
  55032. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(ecKey), WOLFSSL_SUCCESS);
  55033. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(NULL), WOLFSSL_FAILURE);
  55034. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(ecKey), WOLFSSL_SUCCESS);
  55035. /* reference count doesn't follow duplicate */
  55036. ExpectNotNull(dupKey = wolfSSL_EC_KEY_dup(ecKey));
  55037. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +1 */
  55038. ExpectIntEQ(wolfSSL_EC_KEY_up_ref(dupKey), WOLFSSL_SUCCESS); /* +2 */
  55039. wolfSSL_EC_KEY_free(dupKey); /* 3 */
  55040. wolfSSL_EC_KEY_free(dupKey); /* 2 */
  55041. wolfSSL_EC_KEY_free(dupKey); /* 1, free */
  55042. wolfSSL_EC_KEY_free(ecKey); /* 2 */
  55043. wolfSSL_EC_KEY_free(ecKey); /* 1, free */
  55044. #endif
  55045. return EXPECT_RESULT();
  55046. }
  55047. static int test_wolfSSL_EC_KEY_set_group(void)
  55048. {
  55049. EXPECT_DECLS;
  55050. #if defined(HAVE_ECC) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  55051. defined(OPENSSL_EXTRA)
  55052. EC_KEY *key = NULL;
  55053. EC_GROUP *group = NULL;
  55054. const EC_GROUP *group2 = NULL;
  55055. ExpectNotNull(group = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1));
  55056. ExpectNotNull(key = EC_KEY_new());
  55057. ExpectNull(EC_KEY_get0_group(NULL));
  55058. ExpectIntEQ(EC_KEY_set_group(NULL, NULL), 0);
  55059. ExpectIntEQ(EC_KEY_set_group(key, NULL), 0);
  55060. ExpectIntEQ(EC_KEY_set_group(NULL, group), 0);
  55061. ExpectIntEQ(EC_KEY_set_group(key, group), WOLFSSL_SUCCESS);
  55062. ExpectNotNull(group2 = EC_KEY_get0_group(key));
  55063. ExpectIntEQ(EC_GROUP_cmp(group2, group, NULL), 0);
  55064. EC_GROUP_free(group);
  55065. EC_KEY_free(key);
  55066. #endif
  55067. return EXPECT_RESULT();
  55068. }
  55069. static int test_wolfSSL_EC_KEY_set_conv_form(void)
  55070. {
  55071. EXPECT_DECLS;
  55072. #if defined(HAVE_ECC) && defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  55073. BIO* bio = NULL;
  55074. EC_KEY* key = NULL;
  55075. /* Error condition: NULL key. */
  55076. ExpectIntLT(EC_KEY_get_conv_form(NULL), 0);
  55077. ExpectNotNull(bio = BIO_new_file("./certs/ecc-keyPub.pem", "rb"));
  55078. ExpectNotNull(key = PEM_read_bio_EC_PUBKEY(bio, NULL, NULL, NULL));
  55079. /* Conversion form defaults to uncompressed. */
  55080. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  55081. #ifdef HAVE_COMP_KEY
  55082. /* Explicitly set to compressed. */
  55083. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  55084. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_COMPRESSED);
  55085. #else
  55086. /* Will still work just won't change anything. */
  55087. EC_KEY_set_conv_form(key, POINT_CONVERSION_COMPRESSED);
  55088. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  55089. EC_KEY_set_conv_form(key, POINT_CONVERSION_UNCOMPRESSED);
  55090. ExpectIntEQ(EC_KEY_get_conv_form(key), POINT_CONVERSION_UNCOMPRESSED);
  55091. #endif
  55092. EC_KEY_set_conv_form(NULL, POINT_CONVERSION_UNCOMPRESSED);
  55093. BIO_free(bio);
  55094. EC_KEY_free(key);
  55095. #endif
  55096. return EXPECT_RESULT();
  55097. }
  55098. static int test_wolfSSL_EC_KEY_private_key(void)
  55099. {
  55100. EXPECT_DECLS;
  55101. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  55102. WOLFSSL_EC_KEY* key = NULL;
  55103. WOLFSSL_BIGNUM* priv = NULL;
  55104. WOLFSSL_BIGNUM* priv2 = NULL;
  55105. WOLFSSL_BIGNUM* bn;
  55106. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  55107. ExpectNotNull(priv = wolfSSL_BN_new());
  55108. ExpectNotNull(priv2 = wolfSSL_BN_new());
  55109. ExpectIntNE(BN_set_word(priv, 2), 0);
  55110. ExpectIntNE(BN_set_word(priv2, 2), 0);
  55111. ExpectNull(wolfSSL_EC_KEY_get0_private_key(NULL));
  55112. /* No private key set. */
  55113. ExpectNull(wolfSSL_EC_KEY_get0_private_key(key));
  55114. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, NULL), 0);
  55115. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, NULL), 0);
  55116. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(NULL, priv), 0);
  55117. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv), 1);
  55118. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  55119. ExpectPtrNE(bn, priv);
  55120. ExpectIntEQ(wolfSSL_EC_KEY_set_private_key(key, priv2), 1);
  55121. ExpectNotNull(bn = wolfSSL_EC_KEY_get0_private_key(key));
  55122. ExpectPtrNE(bn, priv2);
  55123. wolfSSL_BN_free(priv2);
  55124. wolfSSL_BN_free(priv);
  55125. wolfSSL_EC_KEY_free(key);
  55126. #endif
  55127. return EXPECT_RESULT();
  55128. }
  55129. static int test_wolfSSL_EC_KEY_public_key(void)
  55130. {
  55131. EXPECT_DECLS;
  55132. #if defined(OPENSSL_EXTRA) && !defined(NO_BIO)
  55133. WOLFSSL_EC_KEY* key = NULL;
  55134. WOLFSSL_EC_POINT* pub = NULL;
  55135. WOLFSSL_EC_POINT* point = NULL;
  55136. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  55137. ExpectNull(wolfSSL_EC_KEY_get0_public_key(NULL));
  55138. ExpectNotNull(wolfSSL_EC_KEY_get0_public_key(key));
  55139. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), 1);
  55140. ExpectNotNull(pub = wolfSSL_EC_KEY_get0_public_key(key));
  55141. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, NULL), 0);
  55142. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, NULL), 0);
  55143. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(NULL, pub), 0);
  55144. ExpectIntEQ(wolfSSL_EC_KEY_set_public_key(key, pub), 1);
  55145. ExpectNotNull(point = wolfSSL_EC_KEY_get0_public_key(key));
  55146. ExpectPtrEq(point, pub);
  55147. wolfSSL_EC_KEY_free(key);
  55148. #endif
  55149. return EXPECT_RESULT();
  55150. }
  55151. static int test_wolfSSL_EC_KEY_print_fp(void)
  55152. {
  55153. EXPECT_DECLS;
  55154. #if defined(HAVE_ECC) && ((defined(HAVE_ECC224) && defined(HAVE_ECC256)) || \
  55155. defined(HAVE_ALL_CURVES)) && ECC_MIN_KEY_SZ <= 224 && \
  55156. defined(OPENSSL_EXTRA) && defined(XFPRINTF) && !defined(NO_FILESYSTEM) && \
  55157. !defined(NO_STDIO_FILESYSTEM)
  55158. EC_KEY* key = NULL;
  55159. /* Bad file pointer. */
  55160. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(NULL, key, 0), WOLFSSL_FAILURE);
  55161. /* NULL key. */
  55162. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, NULL, 0), WOLFSSL_FAILURE);
  55163. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(NID_secp224r1)));
  55164. /* Negative indent. */
  55165. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, -1), WOLFSSL_FAILURE);
  55166. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  55167. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  55168. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  55169. wolfSSL_EC_KEY_free(key);
  55170. ExpectNotNull((key = wolfSSL_EC_KEY_new_by_curve_name(
  55171. NID_X9_62_prime256v1)));
  55172. ExpectIntEQ(wolfSSL_EC_KEY_generate_key(key), WOLFSSL_SUCCESS);
  55173. ExpectIntEQ(wolfSSL_EC_KEY_print_fp(stderr, key, 4), WOLFSSL_SUCCESS);
  55174. wolfSSL_EC_KEY_free(key);
  55175. #endif
  55176. return EXPECT_RESULT();
  55177. }
  55178. static int test_wolfSSL_EC_get_builtin_curves(void)
  55179. {
  55180. EXPECT_DECLS;
  55181. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  55182. #if !defined(HAVE_FIPS) || (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION>2))
  55183. EC_builtin_curve* curves = NULL;
  55184. size_t crv_len = 0;
  55185. size_t i = 0;
  55186. ExpectIntGT((crv_len = EC_get_builtin_curves(NULL, 0)), 0);
  55187. ExpectNotNull(curves = (EC_builtin_curve*)XMALLOC(
  55188. sizeof(EC_builtin_curve) * crv_len, NULL, DYNAMIC_TYPE_TMP_BUFFER));
  55189. ExpectIntEQ((EC_get_builtin_curves(curves, 0)), crv_len);
  55190. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len), crv_len);
  55191. for (i = 0; EXPECT_SUCCESS() && (i < crv_len); i++) {
  55192. if (curves[i].comment != NULL) {
  55193. ExpectStrEQ(OBJ_nid2sn(curves[i].nid), curves[i].comment);
  55194. }
  55195. }
  55196. if (crv_len > 1) {
  55197. ExpectIntEQ(EC_get_builtin_curves(curves, crv_len - 1), crv_len - 1);
  55198. }
  55199. XFREE(curves, NULL, DYNAMIC_TYPE_TMP_BUFFER);
  55200. #endif /* !HAVE_FIPS || HAVE_FIPS_VERSION > 2 */
  55201. #endif /* OPENSSL_EXTRA || OPENSSL_ALL */
  55202. return EXPECT_RESULT();
  55203. }
  55204. static int test_wolfSSL_ECDSA_SIG(void)
  55205. {
  55206. EXPECT_DECLS;
  55207. #ifdef OPENSSL_EXTRA
  55208. WOLFSSL_ECDSA_SIG* sig = NULL;
  55209. WOLFSSL_ECDSA_SIG* sig2 = NULL;
  55210. WOLFSSL_BIGNUM* r = NULL;
  55211. WOLFSSL_BIGNUM* s = NULL;
  55212. const WOLFSSL_BIGNUM* r2 = NULL;
  55213. const WOLFSSL_BIGNUM* s2 = NULL;
  55214. const unsigned char* cp = NULL;
  55215. unsigned char* p = NULL;
  55216. unsigned char outSig[8];
  55217. unsigned char sigData[8] =
  55218. { 0x30, 0x06, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  55219. unsigned char sigDataBad[8] =
  55220. { 0x30, 0x07, 0x02, 0x01, 0x01, 0x02, 0x01, 0x01 };
  55221. wolfSSL_ECDSA_SIG_free(NULL);
  55222. ExpectNotNull(sig = wolfSSL_ECDSA_SIG_new());
  55223. ExpectNotNull(r = wolfSSL_BN_new());
  55224. ExpectNotNull(s = wolfSSL_BN_new());
  55225. ExpectIntEQ(wolfSSL_BN_set_word(r, 1), 1);
  55226. ExpectIntEQ(wolfSSL_BN_set_word(s, 1), 1);
  55227. wolfSSL_ECDSA_SIG_get0(NULL, NULL, NULL);
  55228. wolfSSL_ECDSA_SIG_get0(NULL, &r2, NULL);
  55229. wolfSSL_ECDSA_SIG_get0(NULL, NULL, &s2);
  55230. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  55231. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, NULL), 0);
  55232. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, NULL), 0);
  55233. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, NULL), 0);
  55234. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, NULL, s), 0);
  55235. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(NULL, r, s), 0);
  55236. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, NULL, s), 0);
  55237. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, NULL), 0);
  55238. r2 = NULL;
  55239. s2 = NULL;
  55240. wolfSSL_ECDSA_SIG_get0(NULL, &r2, &s2);
  55241. ExpectNull(r2);
  55242. ExpectNull(s2);
  55243. ExpectIntEQ(wolfSSL_ECDSA_SIG_set0(sig, r, s), 1);
  55244. if (EXPECT_FAIL()) {
  55245. wolfSSL_BN_free(r);
  55246. wolfSSL_BN_free(s);
  55247. }
  55248. wolfSSL_ECDSA_SIG_get0(sig, &r2, &s2);
  55249. ExpectPtrEq(r2, r);
  55250. ExpectPtrEq(s2, s);
  55251. r2 = NULL;
  55252. wolfSSL_ECDSA_SIG_get0(sig, &r2, NULL);
  55253. ExpectPtrEq(r2, r);
  55254. s2 = NULL;
  55255. wolfSSL_ECDSA_SIG_get0(sig, NULL, &s2);
  55256. ExpectPtrEq(s2, s);
  55257. /* r and s are freed when sig is freed. */
  55258. wolfSSL_ECDSA_SIG_free(sig);
  55259. sig = NULL;
  55260. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, NULL, sizeof(sigData)));
  55261. cp = sigDataBad;
  55262. ExpectNull(wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigDataBad)));
  55263. cp = sigData;
  55264. ExpectNotNull((sig = wolfSSL_d2i_ECDSA_SIG(NULL, &cp, sizeof(sigData))));
  55265. ExpectIntEQ((cp == sigData + 8), 1);
  55266. cp = sigData;
  55267. ExpectNull(wolfSSL_d2i_ECDSA_SIG(&sig, NULL, sizeof(sigData)));
  55268. ExpectNotNull((sig2 = wolfSSL_d2i_ECDSA_SIG(&sig, &cp, sizeof(sigData))));
  55269. ExpectIntEQ((sig == sig2), 1);
  55270. cp = outSig;
  55271. p = outSig;
  55272. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, &p), 0);
  55273. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(NULL, NULL), 0);
  55274. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, NULL), 8);
  55275. ExpectIntEQ(wolfSSL_i2d_ECDSA_SIG(sig, &p), sizeof(sigData));
  55276. ExpectIntEQ((p == outSig + 8), 1);
  55277. ExpectIntEQ(XMEMCMP(sigData, outSig, 8), 0);
  55278. wolfSSL_ECDSA_SIG_free(sig);
  55279. #endif
  55280. return EXPECT_RESULT();
  55281. }
  55282. static int test_ECDSA_size_sign(void)
  55283. {
  55284. EXPECT_DECLS;
  55285. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP)
  55286. EC_KEY* key = NULL;
  55287. ECDSA_SIG* ecdsaSig = NULL;
  55288. int id;
  55289. byte hash[WC_MAX_DIGEST_SIZE];
  55290. byte hash2[WC_MAX_DIGEST_SIZE];
  55291. byte sig[ECC_MAX_SIG_SIZE];
  55292. unsigned int sigSz = sizeof(sig);
  55293. XMEMSET(hash, 123, sizeof(hash));
  55294. XMEMSET(hash2, 234, sizeof(hash2));
  55295. id = wc_ecc_get_curve_id_from_name("SECP256R1");
  55296. ExpectIntEQ(id, ECC_SECP256R1);
  55297. ExpectNotNull(key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  55298. ExpectIntEQ(EC_KEY_generate_key(key), 1);
  55299. ExpectIntGE(ECDSA_size(NULL), 0);
  55300. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, NULL), 0);
  55301. ExpectIntEQ(ECDSA_sign(0, NULL, sizeof(hash), sig, &sigSz, key), 0);
  55302. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), NULL, &sigSz, key), 0);
  55303. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, (int)sigSz, NULL), 0);
  55304. ExpectIntEQ(ECDSA_verify(0, NULL, sizeof(hash), sig, (int)sigSz, key), 0);
  55305. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), NULL, (int)sigSz, key), 0);
  55306. ExpectIntEQ(ECDSA_sign(0, hash, sizeof(hash), sig, &sigSz, key), 1);
  55307. ExpectIntGE(ECDSA_size(key), sigSz);
  55308. ExpectIntEQ(ECDSA_verify(0, hash, sizeof(hash), sig, (int)sigSz, key), 1);
  55309. ExpectIntEQ(ECDSA_verify(0, hash2, sizeof(hash2), sig, (int)sigSz, key), 0);
  55310. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), NULL));
  55311. ExpectNull(ECDSA_do_sign(NULL, sizeof(hash), key));
  55312. ExpectNull(ECDSA_do_sign(hash, sizeof(hash), NULL));
  55313. ExpectNotNull(ecdsaSig = ECDSA_do_sign(hash, sizeof(hash), key));
  55314. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, NULL), -1);
  55315. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, NULL), -1);
  55316. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, NULL), -1);
  55317. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), NULL, key), -1);
  55318. ExpectIntEQ(ECDSA_do_verify(NULL, sizeof(hash), ecdsaSig, key), -1);
  55319. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), NULL, key), -1);
  55320. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, NULL), -1);
  55321. ExpectIntEQ(ECDSA_do_verify(hash, sizeof(hash), ecdsaSig, key), 1);
  55322. ExpectIntEQ(ECDSA_do_verify(hash2, sizeof(hash2), ecdsaSig, key), 0);
  55323. ECDSA_SIG_free(ecdsaSig);
  55324. EC_KEY_free(key);
  55325. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP */
  55326. return EXPECT_RESULT();
  55327. }
  55328. static int test_ECDH_compute_key(void)
  55329. {
  55330. EXPECT_DECLS;
  55331. #if defined(OPENSSL_EXTRA) && !defined(NO_ECC256) && !defined(NO_ECC_SECP) && \
  55332. !defined(WOLF_CRYPTO_CB_ONLY_ECC)
  55333. EC_KEY* key1 = NULL;
  55334. EC_KEY* key2 = NULL;
  55335. EC_POINT* pub1 = NULL;
  55336. EC_POINT* pub2 = NULL;
  55337. byte secret1[32];
  55338. byte secret2[32];
  55339. int i;
  55340. ExpectNotNull(key1 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  55341. ExpectIntEQ(EC_KEY_generate_key(key1), 1);
  55342. ExpectNotNull(pub1 = wolfSSL_EC_KEY_get0_public_key(key1));
  55343. ExpectNotNull(key2 = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  55344. ExpectIntEQ(EC_KEY_generate_key(key2), 1);
  55345. ExpectNotNull(pub2 = wolfSSL_EC_KEY_get0_public_key(key2));
  55346. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, NULL, NULL), 0);
  55347. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, NULL, NULL),
  55348. 0);
  55349. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, NULL, NULL), 0);
  55350. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), NULL, key1, NULL), 0);
  55351. ExpectIntEQ(ECDH_compute_key(NULL, sizeof(secret1), pub2, key1, NULL), 0);
  55352. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), NULL, key1, NULL),
  55353. 0);
  55354. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, NULL, NULL),
  55355. 0);
  55356. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1) - 16, pub2, key1,
  55357. NULL), 0);
  55358. ExpectIntEQ(ECDH_compute_key(secret1, sizeof(secret1), pub2, key1, NULL),
  55359. sizeof(secret1));
  55360. ExpectIntEQ(ECDH_compute_key(secret2, sizeof(secret2), pub1, key2, NULL),
  55361. sizeof(secret2));
  55362. for (i = 0; i < (int)sizeof(secret1); i++) {
  55363. ExpectIntEQ(secret1[i], secret2[i]);
  55364. }
  55365. EC_KEY_free(key2);
  55366. EC_KEY_free(key1);
  55367. #endif /* OPENSSL_EXTRA && !NO_ECC256 && !NO_ECC_SECP &&
  55368. * !WOLF_CRYPTO_CB_ONLY_ECC */
  55369. return EXPECT_RESULT();
  55370. }
  55371. #endif /* HAVE_ECC && !OPENSSL_NO_PK */
  55372. #if defined(OPENSSL_EXTRA) && !defined(NO_CERTS) && \
  55373. defined(WOLFSSL_CERT_GEN) && defined(WOLFSSL_CERT_REQ) && \
  55374. !defined(NO_ASN_TIME)
  55375. static int test_openssl_make_self_signed_certificate(EVP_PKEY* pkey,
  55376. int expectedDerSz)
  55377. {
  55378. EXPECT_DECLS;
  55379. X509* x509 = NULL;
  55380. BIGNUM* serial_number = NULL;
  55381. X509_NAME* name = NULL;
  55382. time_t epoch_off = 0;
  55383. ASN1_INTEGER* asn1_serial_number;
  55384. long not_before, not_after;
  55385. int derSz;
  55386. ExpectNotNull(x509 = X509_new());
  55387. ExpectIntNE(X509_set_pubkey(x509, pkey), 0);
  55388. ExpectNotNull(serial_number = BN_new());
  55389. ExpectIntNE(BN_pseudo_rand(serial_number, 64, 0, 0), 0);
  55390. ExpectNotNull(asn1_serial_number = X509_get_serialNumber(x509));
  55391. ExpectNotNull(BN_to_ASN1_INTEGER(serial_number, asn1_serial_number));
  55392. /* version 3 */
  55393. ExpectIntNE(X509_set_version(x509, 2L), 0);
  55394. ExpectNotNull(name = X509_NAME_new());
  55395. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_commonName, MBSTRING_UTF8,
  55396. (unsigned char*)"www.wolfssl.com", -1, -1, 0), 0);
  55397. ExpectIntNE(X509_NAME_add_entry_by_NID(name, NID_pkcs9_contentType,
  55398. MBSTRING_UTF8,(unsigned char*)"Server", -1, -1, 0), 0);
  55399. ExpectIntNE(X509_set_subject_name(x509, name), 0);
  55400. ExpectIntNE(X509_set_issuer_name(x509, name), 0);
  55401. not_before = (long)wc_Time(NULL);
  55402. not_after = not_before + (365 * 24 * 60 * 60);
  55403. ExpectNotNull(X509_time_adj(X509_get_notBefore(x509), not_before,
  55404. &epoch_off));
  55405. ExpectNotNull(X509_time_adj(X509_get_notAfter(x509), not_after,
  55406. &epoch_off));
  55407. ExpectIntNE(X509_sign(x509, pkey, EVP_sha256()), 0);
  55408. ExpectNotNull(wolfSSL_X509_get_der(x509, &derSz));
  55409. ExpectIntGE(derSz, expectedDerSz);
  55410. BN_free(serial_number);
  55411. X509_NAME_free(name);
  55412. X509_free(x509);
  55413. return EXPECT_RESULT();
  55414. }
  55415. #endif
  55416. static int test_openssl_generate_key_and_cert(void)
  55417. {
  55418. EXPECT_DECLS;
  55419. #if defined(OPENSSL_EXTRA)
  55420. int expectedDerSz;
  55421. EVP_PKEY* pkey = NULL;
  55422. #ifdef HAVE_ECC
  55423. EC_KEY* ec_key = NULL;
  55424. #endif
  55425. #if !defined(NO_RSA)
  55426. int key_length = 2048;
  55427. BIGNUM* exponent = NULL;
  55428. RSA* rsa = NULL;
  55429. ExpectNotNull(pkey = EVP_PKEY_new());
  55430. ExpectNotNull(exponent = BN_new());
  55431. ExpectNotNull(rsa = RSA_new());
  55432. ExpectIntNE(BN_set_word(exponent, WC_RSA_EXPONENT), 0);
  55433. #ifndef WOLFSSL_KEY_GEN
  55434. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  55435. #if defined(USE_CERT_BUFFERS_1024)
  55436. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_1024,
  55437. sizeof_server_key_der_1024, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  55438. key_length = 1024;
  55439. #elif defined(USE_CERT_BUFFERS_2048)
  55440. ExpectIntNE(wolfSSL_RSA_LoadDer_ex(rsa, server_key_der_2048,
  55441. sizeof_server_key_der_2048, WOLFSSL_RSA_LOAD_PRIVATE), 0);
  55442. #else
  55443. RSA_free(rsa);
  55444. rsa = NULL;
  55445. #endif
  55446. #else
  55447. ExpectIntEQ(RSA_generate_key_ex(NULL, key_length, exponent, NULL), 0);
  55448. ExpectIntEQ(RSA_generate_key_ex(rsa, 0, exponent, NULL), 0);
  55449. ExpectIntEQ(RSA_generate_key_ex(rsa, key_length, NULL, NULL), 0);
  55450. ExpectIntNE(RSA_generate_key_ex(rsa, key_length, exponent, NULL), 0);
  55451. #endif
  55452. if (rsa) {
  55453. ExpectIntNE(EVP_PKEY_assign_RSA(pkey, rsa), 0);
  55454. if (EXPECT_FAIL()) {
  55455. RSA_free(rsa);
  55456. }
  55457. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  55458. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  55459. expectedDerSz = 743;
  55460. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey,
  55461. expectedDerSz), TEST_SUCCESS);
  55462. #endif
  55463. }
  55464. EVP_PKEY_free(pkey);
  55465. pkey = NULL;
  55466. BN_free(exponent);
  55467. #endif /* !NO_RSA */
  55468. #ifdef HAVE_ECC
  55469. ExpectNotNull(pkey = EVP_PKEY_new());
  55470. ExpectNotNull(ec_key = EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
  55471. #ifndef NO_WOLFSSL_STUB
  55472. EC_KEY_set_asn1_flag(ec_key, OPENSSL_EC_NAMED_CURVE);
  55473. #endif
  55474. ExpectIntNE(EC_KEY_generate_key(ec_key), 0);
  55475. ExpectIntNE(EVP_PKEY_assign_EC_KEY(pkey, ec_key), 0);
  55476. if (EXPECT_FAIL()) {
  55477. EC_KEY_free(ec_key);
  55478. }
  55479. #if !defined(NO_CERTS) && defined(WOLFSSL_CERT_GEN) && \
  55480. defined(WOLFSSL_CERT_REQ) && !defined(NO_ASN_TIME)
  55481. expectedDerSz = 344;
  55482. ExpectIntEQ(test_openssl_make_self_signed_certificate(pkey, expectedDerSz),
  55483. TEST_SUCCESS);
  55484. #endif
  55485. EVP_PKEY_free(pkey);
  55486. #endif /* HAVE_ECC */
  55487. (void)pkey;
  55488. (void)expectedDerSz;
  55489. #endif /* OPENSSL_EXTRA */
  55490. return EXPECT_RESULT();
  55491. }
  55492. static int test_stubs_are_stubs(void)
  55493. {
  55494. EXPECT_DECLS;
  55495. #if defined(OPENSSL_EXTRA) && !defined(NO_WOLFSSL_STUB) && \
  55496. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  55497. WOLFSSL_CTX* ctx = NULL;
  55498. WOLFSSL_CTX* ctxN = NULL;
  55499. #ifndef NO_WOLFSSL_CLIENT
  55500. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  55501. #elif !defined(NO_WOLFSSL_SERVER)
  55502. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  55503. #endif
  55504. #define CHECKZERO_RET(x, y, z) ExpectIntEQ((int) x(y), 0); \
  55505. ExpectIntEQ((int) x(z), 0)
  55506. /* test logic, all stubs return same result regardless of ctx being NULL
  55507. * as there are no sanity checks, it's just a stub! If at some
  55508. * point a stub is not a stub it should begin to return BAD_FUNC_ARG
  55509. * if invalid inputs are supplied. Test calling both
  55510. * with and without valid inputs, if a stub functionality remains unchanged.
  55511. */
  55512. CHECKZERO_RET(wolfSSL_CTX_sess_accept, ctx, ctxN);
  55513. CHECKZERO_RET(wolfSSL_CTX_sess_connect, ctx, ctxN);
  55514. CHECKZERO_RET(wolfSSL_CTX_sess_accept_good, ctx, ctxN);
  55515. CHECKZERO_RET(wolfSSL_CTX_sess_connect_good, ctx, ctxN);
  55516. CHECKZERO_RET(wolfSSL_CTX_sess_accept_renegotiate, ctx, ctxN);
  55517. CHECKZERO_RET(wolfSSL_CTX_sess_connect_renegotiate, ctx, ctxN);
  55518. CHECKZERO_RET(wolfSSL_CTX_sess_hits, ctx, ctxN);
  55519. CHECKZERO_RET(wolfSSL_CTX_sess_cb_hits, ctx, ctxN);
  55520. CHECKZERO_RET(wolfSSL_CTX_sess_cache_full, ctx, ctxN);
  55521. CHECKZERO_RET(wolfSSL_CTX_sess_misses, ctx, ctxN);
  55522. CHECKZERO_RET(wolfSSL_CTX_sess_timeouts, ctx, ctxN);
  55523. wolfSSL_CTX_free(ctx);
  55524. ctx = NULL;
  55525. #endif /* OPENSSL_EXTRA && !NO_WOLFSSL_STUB && (!NO_WOLFSSL_CLIENT ||
  55526. * !NO_WOLFSSL_SERVER) */
  55527. return EXPECT_RESULT();
  55528. }
  55529. static int test_CONF_modules_xxx(void)
  55530. {
  55531. int res = TEST_SKIPPED;
  55532. #if defined(OPENSSL_EXTRA)
  55533. CONF_modules_free();
  55534. CONF_modules_unload(0);
  55535. CONF_modules_unload(1);
  55536. CONF_modules_unload(-1);
  55537. res = TEST_SUCCESS;
  55538. #endif /* OPENSSL_EXTRA */
  55539. return res;
  55540. }
  55541. static int test_CRYPTO_set_dynlock_xxx(void)
  55542. {
  55543. int res = TEST_SKIPPED;
  55544. #if defined(OPENSSL_EXTRA)
  55545. CRYPTO_set_dynlock_create_callback(
  55546. (struct CRYPTO_dynlock_value *(*)(const char*, int))NULL);
  55547. CRYPTO_set_dynlock_create_callback(
  55548. (struct CRYPTO_dynlock_value *(*)(const char*, int))1);
  55549. CRYPTO_set_dynlock_destroy_callback(
  55550. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))NULL);
  55551. CRYPTO_set_dynlock_destroy_callback(
  55552. (void (*)(struct CRYPTO_dynlock_value*, const char*, int))1);
  55553. CRYPTO_set_dynlock_lock_callback(
  55554. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))NULL);
  55555. CRYPTO_set_dynlock_lock_callback(
  55556. (void (*)(int, struct CRYPTO_dynlock_value *, const char*, int))1);
  55557. res = TEST_SUCCESS;
  55558. #endif /* OPENSSL_EXTRA */
  55559. return res;
  55560. }
  55561. static int test_CRYPTO_THREADID_xxx(void)
  55562. {
  55563. EXPECT_DECLS;
  55564. #if defined(OPENSSL_EXTRA)
  55565. CRYPTO_THREADID_current((CRYPTO_THREADID*)NULL);
  55566. CRYPTO_THREADID_current((CRYPTO_THREADID*)1);
  55567. ExpectIntEQ(CRYPTO_THREADID_hash((const CRYPTO_THREADID*)NULL), 0);
  55568. #endif /* OPENSSL_EXTRA */
  55569. return EXPECT_RESULT();
  55570. }
  55571. static int test_ENGINE_cleanup(void)
  55572. {
  55573. int res = TEST_SKIPPED;
  55574. #if defined(OPENSSL_EXTRA)
  55575. ENGINE_cleanup();
  55576. res = TEST_SUCCESS;
  55577. #endif /* OPENSSL_EXTRA */
  55578. return res;
  55579. }
  55580. static int test_wolfSSL_CTX_LoadCRL(void)
  55581. {
  55582. EXPECT_DECLS;
  55583. #if defined(HAVE_CRL) && !defined(NO_RSA) && !defined(NO_FILESYSTEM) && \
  55584. (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER))
  55585. WOLFSSL_CTX* ctx = NULL;
  55586. WOLFSSL* ssl = NULL;
  55587. const char* badPath = "dummypath";
  55588. const char* validPath = "./certs/crl";
  55589. const char* validFilePath = "./certs/crl/cliCrl.pem";
  55590. const char* issuerCert = "./certs/client-cert.pem";
  55591. int derType = WOLFSSL_FILETYPE_ASN1;
  55592. int pemType = WOLFSSL_FILETYPE_PEM;
  55593. #ifdef HAVE_CRL_MONITOR
  55594. int monitor = WOLFSSL_CRL_MONITOR;
  55595. #else
  55596. int monitor = 0;
  55597. #endif
  55598. WOLFSSL_CERT_MANAGER* cm = NULL;
  55599. #define FAIL_T1(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  55600. BAD_FUNC_ARG)
  55601. #define FAIL_T2(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  55602. NOT_COMPILED_IN)
  55603. #define SUCC_T(x, y, z, p, d) ExpectIntEQ((int) x(y, z, p, d), \
  55604. WOLFSSL_SUCCESS)
  55605. #ifndef NO_WOLFSSL_CLIENT
  55606. #define NEW_CTX(ctx) ExpectNotNull( \
  55607. (ctx) = wolfSSL_CTX_new(wolfSSLv23_client_method()))
  55608. #elif !defined(NO_WOLFSSL_SERVER)
  55609. #define NEW_CTX(ctx) ExpectNotNull( \
  55610. (ctx) = wolfSSL_CTX_new(wolfSSLv23_server_method()))
  55611. #else
  55612. #define NEW_CTX(ctx) return
  55613. #endif
  55614. FAIL_T1(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  55615. NEW_CTX(ctx);
  55616. #ifndef HAVE_CRL_MONITOR
  55617. FAIL_T2(wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, WOLFSSL_CRL_MONITOR);
  55618. wolfSSL_CTX_free(ctx);
  55619. NEW_CTX(ctx);
  55620. #endif
  55621. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, validPath, pemType, monitor);
  55622. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, pemType, monitor);
  55623. SUCC_T (wolfSSL_CTX_LoadCRL, ctx, badPath, derType, monitor);
  55624. wolfSSL_CTX_free(ctx);
  55625. ctx = NULL;
  55626. NEW_CTX(ctx);
  55627. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  55628. WOLFSSL_SUCCESS);
  55629. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, validFilePath, pemType), WOLFSSL_SUCCESS);
  55630. wolfSSL_CTX_free(ctx);
  55631. ctx = NULL;
  55632. NEW_CTX(ctx);
  55633. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx, issuerCert, NULL),
  55634. WOLFSSL_SUCCESS);
  55635. ExpectNotNull(ssl = wolfSSL_new(ctx));
  55636. ExpectIntEQ(wolfSSL_LoadCRLFile(ssl, validFilePath, pemType), WOLFSSL_SUCCESS);
  55637. wolfSSL_free(ssl);
  55638. ssl = NULL;
  55639. wolfSSL_CTX_free(ctx);
  55640. ctx = NULL;
  55641. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  55642. ExpectIntEQ(wolfSSL_CertManagerLoadCA(cm, issuerCert, NULL),
  55643. WOLFSSL_SUCCESS);
  55644. ExpectIntEQ(wolfSSL_CertManagerLoadCRLFile(cm, validFilePath, pemType),
  55645. WOLFSSL_SUCCESS);
  55646. wolfSSL_CertManagerFree(cm);
  55647. #endif
  55648. return EXPECT_RESULT();
  55649. }
  55650. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  55651. static int test_multiple_crls_same_issuer_ctx_ready(WOLFSSL_CTX* ctx)
  55652. {
  55653. EXPECT_DECLS;
  55654. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  55655. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx, "./certs/crl/crl.pem",
  55656. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  55657. return EXPECT_RESULT();
  55658. }
  55659. #endif
  55660. static int test_multiple_crls_same_issuer(void)
  55661. {
  55662. EXPECT_DECLS;
  55663. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_CRL)
  55664. test_ssl_cbf client_cbs, server_cbs;
  55665. struct {
  55666. const char* server_cert;
  55667. const char* server_key;
  55668. } test_params[] = {
  55669. { "./certs/server-cert.pem", "./certs/server-key.pem" },
  55670. { "./certs/server-revoked-cert.pem", "./certs/server-revoked-key.pem" }
  55671. };
  55672. size_t i;
  55673. for (i = 0; i < (sizeof(test_params)/sizeof(*test_params)); i++) {
  55674. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  55675. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  55676. server_cbs.certPemFile = test_params[i].server_cert;
  55677. server_cbs.keyPemFile = test_params[i].server_key;
  55678. client_cbs.crlPemFile = "./certs/crl/extra-crls/general-server-crl.pem";
  55679. client_cbs.ctx_ready = test_multiple_crls_same_issuer_ctx_ready;
  55680. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  55681. &server_cbs, NULL), TEST_FAIL);
  55682. }
  55683. #endif
  55684. return EXPECT_RESULT();
  55685. }
  55686. static int test_SetTmpEC_DHE_Sz(void)
  55687. {
  55688. EXPECT_DECLS;
  55689. #if defined(HAVE_ECC) && !defined(NO_WOLFSSL_CLIENT)
  55690. WOLFSSL_CTX *ctx = NULL;
  55691. WOLFSSL *ssl = NULL;
  55692. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  55693. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_SetTmpEC_DHE_Sz(ctx, 32));
  55694. ExpectNotNull(ssl = wolfSSL_new(ctx));
  55695. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_SetTmpEC_DHE_Sz(ssl, 32));
  55696. wolfSSL_free(ssl);
  55697. wolfSSL_CTX_free(ctx);
  55698. #endif
  55699. return EXPECT_RESULT();
  55700. }
  55701. static int test_wolfSSL_CTX_get0_privatekey(void)
  55702. {
  55703. EXPECT_DECLS;
  55704. #ifdef OPENSSL_ALL
  55705. WOLFSSL_CTX* ctx = NULL;
  55706. (void)ctx;
  55707. #ifndef NO_RSA
  55708. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  55709. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  55710. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  55711. WOLFSSL_FILETYPE_PEM));
  55712. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  55713. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  55714. WOLFSSL_FILETYPE_PEM));
  55715. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  55716. wolfSSL_CTX_free(ctx);
  55717. ctx = NULL;
  55718. #endif
  55719. #ifdef HAVE_ECC
  55720. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  55721. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  55722. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, eccCertFile,
  55723. WOLFSSL_FILETYPE_PEM));
  55724. ExpectNull(SSL_CTX_get0_privatekey(ctx));
  55725. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, eccKeyFile,
  55726. WOLFSSL_FILETYPE_PEM));
  55727. ExpectNotNull(SSL_CTX_get0_privatekey(ctx));
  55728. wolfSSL_CTX_free(ctx);
  55729. #endif
  55730. #endif
  55731. return EXPECT_RESULT();
  55732. }
  55733. static int test_wolfSSL_dtls_set_mtu(void)
  55734. {
  55735. EXPECT_DECLS;
  55736. #if (defined(WOLFSSL_DTLS_MTU) || defined(WOLFSSL_SCTP)) && \
  55737. !defined(NO_WOLFSSL_SERVER) && defined(WOLFSSL_DTLS) && \
  55738. !defined(WOLFSSL_NO_TLS12)
  55739. WOLFSSL_CTX* ctx = NULL;
  55740. WOLFSSL* ssl = NULL;
  55741. const char* testCertFile;
  55742. const char* testKeyFile;
  55743. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  55744. #ifndef NO_RSA
  55745. testCertFile = svrCertFile;
  55746. testKeyFile = svrKeyFile;
  55747. #elif defined(HAVE_ECC)
  55748. testCertFile = eccCertFile;
  55749. testKeyFile = eccKeyFile;
  55750. #endif
  55751. if (testCertFile != NULL && testKeyFile != NULL) {
  55752. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx, testCertFile,
  55753. WOLFSSL_FILETYPE_PEM));
  55754. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  55755. WOLFSSL_FILETYPE_PEM));
  55756. }
  55757. ExpectNotNull(ssl = wolfSSL_new(ctx));
  55758. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  55759. ExpectIntEQ(wolfSSL_dtls_set_mtu(NULL, 1488), BAD_FUNC_ARG);
  55760. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 20000), BAD_FUNC_ARG);
  55761. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 20000), WOLFSSL_FAILURE);
  55762. ExpectIntEQ(wolfSSL_get_error(ssl, WOLFSSL_FAILURE), BAD_FUNC_ARG);
  55763. ExpectIntEQ(wolfSSL_CTX_dtls_set_mtu(ctx, 1488), WOLFSSL_SUCCESS);
  55764. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl, 1488), WOLFSSL_SUCCESS);
  55765. wolfSSL_free(ssl);
  55766. wolfSSL_CTX_free(ctx);
  55767. #endif
  55768. return EXPECT_RESULT();
  55769. }
  55770. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  55771. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  55772. static WC_INLINE void generateDTLSMsg(byte* out, int outSz, word32 seq,
  55773. enum HandShakeType hsType, word16 length)
  55774. {
  55775. size_t idx = 0;
  55776. byte* l;
  55777. /* record layer */
  55778. /* handshake type */
  55779. out[idx++] = handshake;
  55780. /* protocol version */
  55781. out[idx++] = 0xfe;
  55782. out[idx++] = 0xfd; /* DTLS 1.2 */
  55783. /* epoch 0 */
  55784. XMEMSET(out + idx, 0, 2);
  55785. idx += 2;
  55786. /* sequence number */
  55787. XMEMSET(out + idx, 0, 6);
  55788. c32toa(seq, out + idx + 2);
  55789. idx += 6;
  55790. /* length in BE */
  55791. if (length)
  55792. c16toa(length, out + idx);
  55793. else
  55794. c16toa(outSz - idx - 2, out + idx);
  55795. idx += 2;
  55796. /* handshake layer */
  55797. /* handshake type */
  55798. out[idx++] = (byte)hsType;
  55799. /* length */
  55800. l = out + idx;
  55801. idx += 3;
  55802. /* message seq */
  55803. c16toa(0, out + idx);
  55804. idx += 2;
  55805. /* frag offset */
  55806. c32to24(0, out + idx);
  55807. idx += 3;
  55808. /* frag length */
  55809. c32to24((word32)outSz - (word32)idx - 3, l);
  55810. c32to24((word32)outSz - (word32)idx - 3, out + idx);
  55811. idx += 3;
  55812. XMEMSET(out + idx, 0, outSz - idx);
  55813. }
  55814. static void test_wolfSSL_dtls_plaintext_server(WOLFSSL* ssl)
  55815. {
  55816. byte msg[] = "This is a msg for the client";
  55817. byte reply[40];
  55818. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  55819. reply[sizeof(reply) - 1] = '\0';
  55820. fprintf(stderr, "Client message: %s\n", reply);
  55821. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  55822. }
  55823. static void test_wolfSSL_dtls_plaintext_client(WOLFSSL* ssl)
  55824. {
  55825. byte ch[50];
  55826. int fd = wolfSSL_get_fd(ssl);
  55827. byte msg[] = "This is a msg for the server";
  55828. byte reply[40];
  55829. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 0);
  55830. /* Server should ignore this datagram */
  55831. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  55832. generateDTLSMsg(ch, sizeof(ch), 20, client_hello, 10000);
  55833. /* Server should ignore this datagram */
  55834. AssertIntEQ(send(fd, ch, sizeof(ch), 0), sizeof(ch));
  55835. AssertIntEQ(wolfSSL_write(ssl, msg, sizeof(msg)), sizeof(msg));
  55836. AssertIntGT(wolfSSL_read(ssl, reply, sizeof(reply)),0);
  55837. reply[sizeof(reply) - 1] = '\0';
  55838. fprintf(stderr, "Server response: %s\n", reply);
  55839. }
  55840. static int test_wolfSSL_dtls_plaintext(void)
  55841. {
  55842. callback_functions func_cb_client;
  55843. callback_functions func_cb_server;
  55844. size_t i;
  55845. struct test_params {
  55846. method_provider client_meth;
  55847. method_provider server_meth;
  55848. ssl_callback on_result_server;
  55849. ssl_callback on_result_client;
  55850. } params[] = {
  55851. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  55852. test_wolfSSL_dtls_plaintext_server,
  55853. test_wolfSSL_dtls_plaintext_client},
  55854. };
  55855. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  55856. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  55857. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  55858. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  55859. func_cb_server.method = params[i].server_meth;
  55860. func_cb_client.method = params[i].client_meth;
  55861. func_cb_client.on_result = params[i].on_result_client;
  55862. func_cb_server.on_result = params[i].on_result_server;
  55863. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  55864. if (!func_cb_client.return_code)
  55865. return TEST_FAIL;
  55866. if (!func_cb_server.return_code)
  55867. return TEST_FAIL;
  55868. }
  55869. return TEST_RES_CHECK(1);
  55870. }
  55871. #else
  55872. static int test_wolfSSL_dtls_plaintext(void) {
  55873. return TEST_SKIPPED;
  55874. }
  55875. #endif
  55876. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  55877. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  55878. static void test_wolfSSL_dtls12_fragments_spammer(WOLFSSL* ssl)
  55879. {
  55880. byte b[1100]; /* buffer for the messages to send */
  55881. size_t idx = 0;
  55882. size_t seq_offset = 0;
  55883. size_t msg_offset = 0;
  55884. int i;
  55885. int fd = wolfSSL_get_fd(ssl);
  55886. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  55887. word32 seq_number = 100; /* start high so server definitely reads this */
  55888. word16 msg_number = 50; /* start high so server has to buffer this */
  55889. AssertIntEQ(ret, 1);
  55890. /* Now let's start spamming the peer with fragments it needs to store */
  55891. XMEMSET(b, -1, sizeof(b));
  55892. /* record layer */
  55893. /* handshake type */
  55894. b[idx++] = 22;
  55895. /* protocol version */
  55896. b[idx++] = 0xfe;
  55897. b[idx++] = 0xfd; /* DTLS 1.2 */
  55898. /* epoch 0 */
  55899. XMEMSET(b + idx, 0, 2);
  55900. idx += 2;
  55901. /* sequence number */
  55902. XMEMSET(b + idx, 0, 6);
  55903. seq_offset = idx + 2; /* increment only the low 32 bits */
  55904. idx += 6;
  55905. /* static length in BE */
  55906. c16toa(42, b + idx);
  55907. idx += 2;
  55908. /* handshake layer */
  55909. /* cert type */
  55910. b[idx++] = 11;
  55911. /* length */
  55912. c32to24(1000, b + idx);
  55913. idx += 3;
  55914. /* message seq */
  55915. c16toa(0, b + idx);
  55916. msg_offset = idx;
  55917. idx += 2;
  55918. /* frag offset */
  55919. c32to24(500, b + idx);
  55920. idx += 3;
  55921. /* frag length */
  55922. c32to24(30, b + idx);
  55923. idx += 3;
  55924. (void)idx; /* inhibit clang-analyzer-deadcode.DeadStores */
  55925. for (i = 0; i < DTLS_POOL_SZ * 2 && ret > 0;
  55926. seq_number++, msg_number++, i++) {
  55927. struct timespec delay;
  55928. XMEMSET(&delay, 0, sizeof(delay));
  55929. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  55930. c32toa(seq_number, b + seq_offset);
  55931. c16toa(msg_number, b + msg_offset);
  55932. ret = (int)send(fd, b, 55, 0);
  55933. nanosleep(&delay, NULL);
  55934. }
  55935. }
  55936. #ifdef WOLFSSL_DTLS13
  55937. static void test_wolfSSL_dtls13_fragments_spammer(WOLFSSL* ssl)
  55938. {
  55939. const word16 sendCountMax = 100;
  55940. byte b[150]; /* buffer for the messages to send */
  55941. size_t idx = 0;
  55942. size_t msg_offset = 0;
  55943. int fd = wolfSSL_get_fd(ssl);
  55944. word16 msg_number = 10; /* start high so server has to buffer this */
  55945. int ret = wolfSSL_connect_cert(ssl); /* This gets us past the cookie */
  55946. AssertIntEQ(ret, 1);
  55947. /* Now let's start spamming the peer with fragments it needs to store */
  55948. XMEMSET(b, -1, sizeof(b));
  55949. /* handshake type */
  55950. b[idx++] = 11;
  55951. /* length */
  55952. c32to24(10000, b + idx);
  55953. idx += 3;
  55954. /* message_seq */
  55955. msg_offset = idx;
  55956. idx += 2;
  55957. /* fragment_offset */
  55958. c32to24(5000, b + idx);
  55959. idx += 3;
  55960. /* fragment_length */
  55961. c32to24(100, b + idx);
  55962. idx += 3;
  55963. /* fragment contents */
  55964. idx += 100;
  55965. for (; ret > 0 && msg_number < sendCountMax; msg_number++) {
  55966. byte sendBuf[150];
  55967. int sendSz = sizeof(sendBuf);
  55968. struct timespec delay;
  55969. XMEMSET(&delay, 0, sizeof(delay));
  55970. delay.tv_nsec = 10000000; /* wait 0.01 seconds */
  55971. c16toa(msg_number, b + msg_offset);
  55972. sendSz = BuildTls13Message(ssl, sendBuf, sendSz, b,
  55973. (int)idx, handshake, 0, 0, 0);
  55974. ret = (int)send(fd, sendBuf, (size_t)sendSz, 0);
  55975. nanosleep(&delay, NULL);
  55976. }
  55977. }
  55978. #endif
  55979. static int test_wolfSSL_dtls_fragments(void)
  55980. {
  55981. EXPECT_DECLS;
  55982. callback_functions func_cb_client;
  55983. callback_functions func_cb_server;
  55984. size_t i;
  55985. struct test_params {
  55986. method_provider client_meth;
  55987. method_provider server_meth;
  55988. ssl_callback spammer;
  55989. } params[] = {
  55990. #if !defined(WOLFSSL_NO_TLS12)
  55991. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  55992. test_wolfSSL_dtls12_fragments_spammer},
  55993. #endif
  55994. #ifdef WOLFSSL_DTLS13
  55995. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  55996. test_wolfSSL_dtls13_fragments_spammer},
  55997. #endif
  55998. };
  55999. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  56000. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  56001. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  56002. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  56003. func_cb_server.method = params[i].server_meth;
  56004. func_cb_client.method = params[i].client_meth;
  56005. func_cb_client.ssl_ready = params[i].spammer;
  56006. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  56007. ExpectFalse(func_cb_client.return_code);
  56008. ExpectFalse(func_cb_server.return_code);
  56009. /* The socket should be closed by the server resulting in a
  56010. * socket error, fatal error or reading a close notify alert */
  56011. if (func_cb_client.last_err != SOCKET_ERROR_E &&
  56012. func_cb_client.last_err != WOLFSSL_ERROR_ZERO_RETURN &&
  56013. func_cb_client.last_err != FATAL_ERROR) {
  56014. ExpectIntEQ(func_cb_client.last_err, SOCKET_ERROR_E);
  56015. }
  56016. /* Check the server returned an error indicating the msg buffer
  56017. * was full */
  56018. ExpectIntEQ(func_cb_server.last_err, DTLS_TOO_MANY_FRAGMENTS_E);
  56019. if (EXPECT_FAIL())
  56020. break;
  56021. }
  56022. return EXPECT_RESULT();
  56023. }
  56024. static void test_wolfSSL_dtls_send_alert(WOLFSSL* ssl)
  56025. {
  56026. int fd, ret;
  56027. byte alert_msg[] = {
  56028. 0x15, /* alert type */
  56029. 0xfe, 0xfd, /* version */
  56030. 0x00, 0x00, /* epoch */
  56031. 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, /* seq number */
  56032. 0x00, 0x02, /* length */
  56033. 0x02, /* level: fatal */
  56034. 0x46 /* protocol version */
  56035. };
  56036. fd = wolfSSL_get_fd(ssl);
  56037. ret = (int)send(fd, alert_msg, sizeof(alert_msg), 0);
  56038. AssertIntGT(ret, 0);
  56039. }
  56040. static int _test_wolfSSL_ignore_alert_before_cookie(byte version12)
  56041. {
  56042. callback_functions client_cbs, server_cbs;
  56043. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56044. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56045. client_cbs.doUdp = server_cbs.doUdp = 1;
  56046. if (version12) {
  56047. #if !defined(WOLFSSL_NO_TLS12)
  56048. client_cbs.method = wolfDTLSv1_2_client_method;
  56049. server_cbs.method = wolfDTLSv1_2_server_method;
  56050. #else
  56051. return TEST_SKIPPED;
  56052. #endif
  56053. }
  56054. else
  56055. {
  56056. #ifdef WOLFSSL_DTLS13
  56057. client_cbs.method = wolfDTLSv1_3_client_method;
  56058. server_cbs.method = wolfDTLSv1_3_server_method;
  56059. #else
  56060. return TEST_SKIPPED;
  56061. #endif /* WOLFSSL_DTLS13 */
  56062. }
  56063. client_cbs.ssl_ready = test_wolfSSL_dtls_send_alert;
  56064. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56065. if (!client_cbs.return_code)
  56066. return TEST_FAIL;
  56067. if (!server_cbs.return_code)
  56068. return TEST_FAIL;
  56069. return TEST_SUCCESS;
  56070. }
  56071. static int test_wolfSSL_ignore_alert_before_cookie(void)
  56072. {
  56073. int ret;
  56074. ret =_test_wolfSSL_ignore_alert_before_cookie(0);
  56075. if (ret != 0)
  56076. return ret;
  56077. ret =_test_wolfSSL_ignore_alert_before_cookie(1);
  56078. if (ret != 0)
  56079. return ret;
  56080. return 0;
  56081. }
  56082. static void test_wolfSSL_send_bad_record(WOLFSSL* ssl)
  56083. {
  56084. int ret;
  56085. int fd;
  56086. byte bad_msg[] = {
  56087. 0x17, /* app data */
  56088. 0xaa, 0xfd, /* bad version */
  56089. 0x00, 0x01, /* epoch 1 */
  56090. 0x00, 0x00, 0x00, 0x00, 0x00, 0x55, /* not seen seq number */
  56091. 0x00, 0x26, /* length: 38 bytes */
  56092. 0xae, 0x30, 0x31, 0xb1, 0xf1, 0xb9, 0x6f, 0xda, 0x17, 0x19, 0xd9, 0x57,
  56093. 0xa9, 0x9d, 0x5c, 0x51, 0x9b, 0x53, 0x63, 0xa5, 0x24, 0x70, 0xa1,
  56094. 0xae, 0xdf, 0x1c, 0xb9, 0xfc, 0xe3, 0xd7, 0x77, 0x6d, 0xb6, 0x89, 0x0f,
  56095. 0x03, 0x18, 0x72
  56096. };
  56097. fd = wolfSSL_get_fd(ssl);
  56098. AssertIntGE(fd, 0);
  56099. ret = (int)send(fd, bad_msg, sizeof(bad_msg), 0);
  56100. AssertIntEQ(ret, sizeof(bad_msg));
  56101. ret = wolfSSL_write(ssl, "badrecordtest", sizeof("badrecordtest"));
  56102. AssertIntEQ(ret, sizeof("badrecordtest"));
  56103. }
  56104. static void test_wolfSSL_read_string(WOLFSSL* ssl)
  56105. {
  56106. byte buf[100];
  56107. int ret;
  56108. ret = wolfSSL_read(ssl, buf, sizeof(buf));
  56109. AssertIntGT(ret, 0);
  56110. AssertIntEQ(strcmp((char*)buf, "badrecordtest"), 0);
  56111. }
  56112. static int _test_wolfSSL_dtls_bad_record(
  56113. method_provider client_method, method_provider server_method)
  56114. {
  56115. callback_functions client_cbs, server_cbs;
  56116. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56117. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56118. client_cbs.doUdp = server_cbs.doUdp = 1;
  56119. client_cbs.method = client_method;
  56120. server_cbs.method = server_method;
  56121. client_cbs.on_result = test_wolfSSL_send_bad_record;
  56122. server_cbs.on_result = test_wolfSSL_read_string;
  56123. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56124. if (!client_cbs.return_code)
  56125. return TEST_FAIL;
  56126. if (!server_cbs.return_code)
  56127. return TEST_FAIL;
  56128. return TEST_SUCCESS;
  56129. }
  56130. static int test_wolfSSL_dtls_bad_record(void)
  56131. {
  56132. int ret = TEST_SUCCESS;
  56133. #if !defined(WOLFSSL_NO_TLS12)
  56134. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_2_client_method,
  56135. wolfDTLSv1_2_server_method);
  56136. #endif
  56137. #ifdef WOLFSSL_DTLS13
  56138. if (ret == TEST_SUCCESS) {
  56139. ret = _test_wolfSSL_dtls_bad_record(wolfDTLSv1_3_client_method,
  56140. wolfDTLSv1_3_server_method);
  56141. }
  56142. #endif /* WOLFSSL_DTLS13 */
  56143. return ret;
  56144. }
  56145. #else
  56146. static int test_wolfSSL_dtls_fragments(void) {
  56147. return TEST_SKIPPED;
  56148. }
  56149. static int test_wolfSSL_ignore_alert_before_cookie(void) {
  56150. return TEST_SKIPPED;
  56151. }
  56152. static int test_wolfSSL_dtls_bad_record(void) {
  56153. return TEST_SKIPPED;
  56154. }
  56155. #endif
  56156. #if defined(WOLFSSL_DTLS13) && !defined(WOLFSSL_TLS13_IGNORE_AEAD_LIMITS) && \
  56157. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  56158. defined(HAVE_IO_TESTS_DEPENDENCIES)
  56159. static byte test_AEAD_fail_decryption = 0;
  56160. static byte test_AEAD_seq_num = 0;
  56161. static byte test_AEAD_done = 0;
  56162. static int test_AEAD_cbiorecv(WOLFSSL *ssl, char *buf, int sz, void *ctx)
  56163. {
  56164. int ret = (int)recv(wolfSSL_get_fd(ssl), buf, sz, 0);
  56165. if (ret > 0) {
  56166. if (test_AEAD_fail_decryption) {
  56167. /* Modify the packet to trigger a decryption failure */
  56168. buf[ret/2] ^= 0xFF;
  56169. if (test_AEAD_fail_decryption == 1)
  56170. test_AEAD_fail_decryption = 0;
  56171. }
  56172. }
  56173. (void)ctx;
  56174. return ret;
  56175. }
  56176. static void test_AEAD_get_limits(WOLFSSL* ssl, w64wrapper* hardLimit,
  56177. w64wrapper* keyUpdateLimit, w64wrapper* sendLimit)
  56178. {
  56179. if (sendLimit)
  56180. w64Zero(sendLimit);
  56181. switch (ssl->specs.bulk_cipher_algorithm) {
  56182. case wolfssl_aes_gcm:
  56183. if (sendLimit)
  56184. *sendLimit = AEAD_AES_LIMIT;
  56185. FALL_THROUGH;
  56186. case wolfssl_chacha:
  56187. if (hardLimit)
  56188. *hardLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_LIMIT;
  56189. if (keyUpdateLimit)
  56190. *keyUpdateLimit = DTLS_AEAD_AES_GCM_CHACHA_FAIL_KU_LIMIT;
  56191. break;
  56192. case wolfssl_aes_ccm:
  56193. if (sendLimit)
  56194. *sendLimit = DTLS_AEAD_AES_CCM_LIMIT;
  56195. if (ssl->specs.aead_mac_size == AES_CCM_8_AUTH_SZ) {
  56196. if (hardLimit)
  56197. *hardLimit = DTLS_AEAD_AES_CCM_8_FAIL_LIMIT;
  56198. if (keyUpdateLimit)
  56199. *keyUpdateLimit = DTLS_AEAD_AES_CCM_8_FAIL_KU_LIMIT;
  56200. }
  56201. else {
  56202. if (hardLimit)
  56203. *hardLimit = DTLS_AEAD_AES_CCM_FAIL_LIMIT;
  56204. if (keyUpdateLimit)
  56205. *keyUpdateLimit = DTLS_AEAD_AES_CCM_FAIL_KU_LIMIT;
  56206. }
  56207. break;
  56208. default:
  56209. fprintf(stderr, "Unrecognized bulk cipher");
  56210. AssertFalse(1);
  56211. break;
  56212. }
  56213. }
  56214. static void test_AEAD_limit_client(WOLFSSL* ssl)
  56215. {
  56216. int ret;
  56217. int i;
  56218. int didReKey = 0;
  56219. char msgBuf[20];
  56220. w64wrapper hardLimit;
  56221. w64wrapper keyUpdateLimit;
  56222. w64wrapper counter;
  56223. w64wrapper sendLimit;
  56224. test_AEAD_get_limits(ssl, &hardLimit, &keyUpdateLimit, &sendLimit);
  56225. w64Zero(&counter);
  56226. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter));
  56227. wolfSSL_SSLSetIORecv(ssl, test_AEAD_cbiorecv);
  56228. for (i = 0; i < 10; i++) {
  56229. /* Test some failed decryptions */
  56230. test_AEAD_fail_decryption = 1;
  56231. w64Increment(&counter);
  56232. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  56233. /* Should succeed since decryption failures are dropped */
  56234. AssertIntGT(ret, 0);
  56235. AssertTrue(w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter));
  56236. }
  56237. test_AEAD_fail_decryption = 1;
  56238. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = keyUpdateLimit;
  56239. w64Increment(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  56240. /* 100 read calls should be enough to complete the key update */
  56241. w64Zero(&counter);
  56242. for (i = 0; i < 100; i++) {
  56243. /* Key update should be sent and negotiated */
  56244. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  56245. AssertIntGT(ret, 0);
  56246. /* Epoch after one key update is 4 */
  56247. if (w64Equal(ssl->dtls13PeerEpoch, w64From32(0, 4)) &&
  56248. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount, counter)) {
  56249. didReKey = 1;
  56250. break;
  56251. }
  56252. }
  56253. AssertTrue(didReKey);
  56254. if (!w64IsZero(sendLimit)) {
  56255. /* Test the sending limit for AEAD ciphers */
  56256. Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->nextSeqNumber = sendLimit;
  56257. test_AEAD_seq_num = 1;
  56258. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  56259. AssertIntGT(ret, 0);
  56260. didReKey = 0;
  56261. w64Zero(&counter);
  56262. /* 100 read calls should be enough to complete the key update */
  56263. for (i = 0; i < 100; i++) {
  56264. /* Key update should be sent and negotiated */
  56265. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  56266. AssertIntGT(ret, 0);
  56267. /* Epoch after another key update is 5 */
  56268. if (w64Equal(ssl->dtls13Epoch, w64From32(0, 5)) &&
  56269. w64Equal(Dtls13GetEpoch(ssl, ssl->dtls13Epoch)->dropCount, counter)) {
  56270. didReKey = 1;
  56271. break;
  56272. }
  56273. }
  56274. AssertTrue(didReKey);
  56275. }
  56276. test_AEAD_fail_decryption = 2;
  56277. Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount = hardLimit;
  56278. w64Decrement(&Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch)->dropCount);
  56279. /* Connection should fail with a DECRYPT_ERROR */
  56280. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  56281. AssertIntEQ(ret, WOLFSSL_FATAL_ERROR);
  56282. AssertIntEQ(wolfSSL_get_error(ssl, ret), DECRYPT_ERROR);
  56283. test_AEAD_done = 1;
  56284. }
  56285. int counter = 0;
  56286. static void test_AEAD_limit_server(WOLFSSL* ssl)
  56287. {
  56288. char msgBuf[] = "Sending data";
  56289. int ret = WOLFSSL_SUCCESS;
  56290. w64wrapper sendLimit;
  56291. SOCKET_T fd = wolfSSL_get_fd(ssl);
  56292. struct timespec delay;
  56293. XMEMSET(&delay, 0, sizeof(delay));
  56294. delay.tv_nsec = 100000000; /* wait 0.1 seconds */
  56295. tcp_set_nonblocking(&fd); /* So that read doesn't block */
  56296. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  56297. test_AEAD_get_limits(ssl, NULL, NULL, &sendLimit);
  56298. while (!test_AEAD_done && ret > 0) {
  56299. counter++;
  56300. if (test_AEAD_seq_num) {
  56301. /* We need to update the seq number so that we can understand the
  56302. * peer. Otherwise we will incorrectly interpret the seq number. */
  56303. Dtls13Epoch* e = Dtls13GetEpoch(ssl, ssl->dtls13PeerEpoch);
  56304. AssertNotNull(e);
  56305. e->nextPeerSeqNumber = sendLimit;
  56306. test_AEAD_seq_num = 0;
  56307. }
  56308. (void)wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  56309. ret = wolfSSL_write(ssl, msgBuf, sizeof(msgBuf));
  56310. nanosleep(&delay, NULL);
  56311. }
  56312. }
  56313. static int test_wolfSSL_dtls_AEAD_limit(void)
  56314. {
  56315. callback_functions func_cb_client;
  56316. callback_functions func_cb_server;
  56317. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  56318. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  56319. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  56320. func_cb_server.method = wolfDTLSv1_3_server_method;
  56321. func_cb_client.method = wolfDTLSv1_3_client_method;
  56322. func_cb_server.on_result = test_AEAD_limit_server;
  56323. func_cb_client.on_result = test_AEAD_limit_client;
  56324. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  56325. if (!func_cb_client.return_code)
  56326. return TEST_FAIL;
  56327. if (!func_cb_server.return_code)
  56328. return TEST_FAIL;
  56329. return TEST_SUCCESS;
  56330. }
  56331. #else
  56332. static int test_wolfSSL_dtls_AEAD_limit(void)
  56333. {
  56334. return TEST_SKIPPED;
  56335. }
  56336. #endif
  56337. #if defined(WOLFSSL_DTLS) && \
  56338. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(SINGLE_THREADED) && \
  56339. !defined(DEBUG_VECTOR_REGISTER_ACCESS_FUZZING)
  56340. static void test_wolfSSL_dtls_send_ch(WOLFSSL* ssl)
  56341. {
  56342. int fd, ret;
  56343. byte ch_msg[] = {
  56344. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  56345. 0xfa, 0x01, 0x00, 0x01, 0xee, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
  56346. 0xee, 0xfe, 0xfd, 0xc0, 0xca, 0xb5, 0x6f, 0x3d, 0x23, 0xcc, 0x53, 0x9a,
  56347. 0x67, 0x17, 0x70, 0xd3, 0xfb, 0x23, 0x16, 0x9e, 0x4e, 0xd6, 0x7e, 0x29,
  56348. 0xab, 0xfa, 0x4c, 0xa5, 0x84, 0x95, 0xc3, 0xdb, 0x21, 0x9a, 0x52, 0x00,
  56349. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  56350. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  56351. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  56352. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  56353. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  56354. 0x8e, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  56355. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  56356. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  56357. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  56358. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  56359. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17,
  56360. 0x00, 0x41, 0x04, 0x96, 0xcb, 0x2e, 0x4e, 0xd9, 0x88, 0x71, 0xc7, 0xf3,
  56361. 0x1a, 0x16, 0xdd, 0x7a, 0x7c, 0xf7, 0x67, 0x8a, 0x5d, 0x9a, 0x55, 0xa6,
  56362. 0x4a, 0x90, 0xd9, 0xfb, 0xc7, 0xfb, 0xbe, 0x09, 0xa9, 0x8a, 0xb5, 0x7a,
  56363. 0xd1, 0xde, 0x83, 0x74, 0x27, 0x31, 0x1c, 0xaa, 0xae, 0xef, 0x58, 0x43,
  56364. 0x13, 0x7d, 0x15, 0x4d, 0x7f, 0x68, 0xf6, 0x8a, 0x38, 0xef, 0x0e, 0xb3,
  56365. 0xcf, 0xb8, 0x4a, 0xa9, 0xb4, 0xd7, 0xcb, 0x01, 0x00, 0x01, 0x00, 0x1d,
  56366. 0x0a, 0x22, 0x8a, 0xd1, 0x78, 0x85, 0x1e, 0x5a, 0xe1, 0x1d, 0x1e, 0xb7,
  56367. 0x2d, 0xbc, 0x5f, 0x52, 0xbc, 0x97, 0x5d, 0x8b, 0x6a, 0x8b, 0x9d, 0x1e,
  56368. 0xb1, 0xfc, 0x8a, 0xb2, 0x56, 0xcd, 0xed, 0x4b, 0xfb, 0x66, 0x3f, 0x59,
  56369. 0x3f, 0x15, 0x5d, 0x09, 0x9e, 0x2f, 0x60, 0x5b, 0x31, 0x81, 0x27, 0xf0,
  56370. 0x1c, 0xda, 0xcd, 0x48, 0x66, 0xc6, 0xbb, 0x25, 0xf0, 0x5f, 0xda, 0x4c,
  56371. 0xcf, 0x1d, 0x88, 0xc8, 0xda, 0x1b, 0x53, 0xea, 0xbd, 0xce, 0x6d, 0xf6,
  56372. 0x4a, 0x76, 0xdb, 0x75, 0x99, 0xaf, 0xcf, 0x76, 0x4a, 0xfb, 0xe3, 0xef,
  56373. 0xb2, 0xcb, 0xae, 0x4a, 0xc0, 0xe8, 0x63, 0x1f, 0xd6, 0xe8, 0xe6, 0x45,
  56374. 0xf9, 0xea, 0x0d, 0x06, 0x19, 0xfc, 0xb1, 0xfd, 0x5d, 0x92, 0x89, 0x7b,
  56375. 0xc7, 0x9f, 0x1a, 0xb3, 0x2b, 0xc7, 0xad, 0x0e, 0xfb, 0x13, 0x41, 0x83,
  56376. 0x84, 0x58, 0x3a, 0x25, 0xb9, 0x49, 0x35, 0x1c, 0x23, 0xcb, 0xd6, 0xe7,
  56377. 0xc2, 0x8c, 0x4b, 0x2a, 0x73, 0xa1, 0xdf, 0x4f, 0x73, 0x9b, 0xb3, 0xd2,
  56378. 0xb2, 0x95, 0x00, 0x3c, 0x26, 0x09, 0x89, 0x71, 0x05, 0x39, 0xc8, 0x98,
  56379. 0x8f, 0xed, 0x32, 0x15, 0x78, 0xcd, 0xd3, 0x7e, 0xfb, 0x5a, 0x78, 0x2a,
  56380. 0xdc, 0xca, 0x20, 0x09, 0xb5, 0x14, 0xf9, 0xd4, 0x58, 0xf6, 0x69, 0xf8,
  56381. 0x65, 0x9f, 0xb7, 0xe4, 0x93, 0xf1, 0xa3, 0x84, 0x7e, 0x1b, 0x23, 0x5d,
  56382. 0xea, 0x59, 0x3e, 0x4d, 0xca, 0xfd, 0xa5, 0x55, 0xdd, 0x99, 0xb5, 0x02,
  56383. 0xf8, 0x0d, 0xe5, 0xf4, 0x06, 0xb0, 0x43, 0x9e, 0x2e, 0xbf, 0x05, 0x33,
  56384. 0x65, 0x7b, 0x13, 0x8c, 0xf9, 0x16, 0x4d, 0xc5, 0x15, 0x0b, 0x40, 0x2f,
  56385. 0x66, 0x94, 0xf2, 0x43, 0x95, 0xe7, 0xa9, 0xb6, 0x39, 0x99, 0x73, 0xb3,
  56386. 0xb0, 0x06, 0xfe, 0x52, 0x9e, 0x57, 0xba, 0x75, 0xfd, 0x76, 0x7b, 0x20,
  56387. 0x31, 0x68, 0x4c
  56388. };
  56389. fd = wolfSSL_get_fd(ssl);
  56390. ret = (int)send(fd, ch_msg, sizeof(ch_msg), 0);
  56391. AssertIntGT(ret, 0);
  56392. /* consume the HRR otherwise handshake will fail */
  56393. ret = (int)recv(fd, ch_msg, sizeof(ch_msg), 0);
  56394. AssertIntGT(ret, 0);
  56395. }
  56396. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  56397. static void test_wolfSSL_dtls_send_ch_with_invalid_cookie(WOLFSSL* ssl)
  56398. {
  56399. int fd, ret;
  56400. byte ch_msh_invalid_cookie[] = {
  56401. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
  56402. 0x4e, 0x01, 0x00, 0x02, 0x42, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x02,
  56403. 0x42, 0xfe, 0xfd, 0x69, 0xca, 0x77, 0x60, 0x6f, 0xfc, 0xd1, 0x5b, 0x60,
  56404. 0x5d, 0xf1, 0xa6, 0x5c, 0x44, 0x71, 0xae, 0xca, 0x62, 0x19, 0x0c, 0xb6,
  56405. 0xf7, 0x2c, 0xa6, 0xd5, 0xd2, 0x99, 0x9d, 0x18, 0xae, 0xac, 0x11, 0x00,
  56406. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  56407. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  56408. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  56409. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  56410. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x01,
  56411. 0xe2, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  56412. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  56413. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  56414. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x2c, 0x00, 0x45,
  56415. 0x00, 0x43, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56416. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56417. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56418. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56419. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  56420. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x2d, 0x00,
  56421. 0x03, 0x02, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x0c, 0x00, 0x0a, 0x00, 0x19,
  56422. 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00, 0x00, 0x16, 0x00, 0x00,
  56423. 0x00, 0x33, 0x01, 0x4b, 0x01, 0x49, 0x00, 0x17, 0x00, 0x41, 0x04, 0x7c,
  56424. 0x5a, 0xc2, 0x5a, 0xfd, 0xcd, 0x2b, 0x08, 0xb2, 0xeb, 0x8e, 0xc0, 0x02,
  56425. 0x03, 0x9d, 0xb1, 0xc1, 0x0d, 0x7b, 0x7f, 0x46, 0x43, 0xdf, 0xf3, 0xee,
  56426. 0x2b, 0x78, 0x0e, 0x29, 0x8c, 0x42, 0x11, 0x2c, 0xde, 0xd7, 0x41, 0x0f,
  56427. 0x28, 0x94, 0x80, 0x41, 0x70, 0xc4, 0x17, 0xfd, 0x6d, 0xfa, 0xee, 0x9a,
  56428. 0xf2, 0xc4, 0x15, 0x4c, 0x5f, 0x54, 0xb6, 0x78, 0x6e, 0xf9, 0x63, 0x27,
  56429. 0x33, 0xb8, 0x7b, 0x01, 0x00, 0x01, 0x00, 0xd4, 0x46, 0x62, 0x9c, 0xbf,
  56430. 0x8f, 0x1b, 0x65, 0x9b, 0xf0, 0x29, 0x64, 0xd8, 0x50, 0x0e, 0x74, 0xf1,
  56431. 0x58, 0x10, 0xc9, 0xd9, 0x82, 0x5b, 0xd9, 0xbe, 0x14, 0xdf, 0xde, 0x86,
  56432. 0xb4, 0x2e, 0x15, 0xee, 0x4f, 0xf6, 0x74, 0x9e, 0x59, 0x11, 0x36, 0x2d,
  56433. 0xb9, 0x67, 0xaa, 0x5a, 0x09, 0x9b, 0x45, 0xf1, 0x01, 0x4c, 0x4e, 0xf6,
  56434. 0xda, 0x6a, 0xae, 0xa7, 0x73, 0x7b, 0x2e, 0xb6, 0x24, 0x89, 0x99, 0xb7,
  56435. 0x52, 0x16, 0x62, 0x0a, 0xab, 0x58, 0xf8, 0x3f, 0x10, 0x5b, 0x83, 0xfd,
  56436. 0x7b, 0x81, 0x77, 0x81, 0x8d, 0xef, 0x24, 0x56, 0x6d, 0xba, 0x49, 0xd4,
  56437. 0x8b, 0xb5, 0xa0, 0xb1, 0xc9, 0x8c, 0x32, 0x95, 0x1c, 0x5e, 0x0a, 0x4b,
  56438. 0xf6, 0x00, 0x50, 0x0a, 0x87, 0x99, 0x59, 0xcf, 0x6f, 0x9d, 0x02, 0xd0,
  56439. 0x1b, 0xa1, 0x96, 0x45, 0x28, 0x76, 0x40, 0x33, 0x28, 0xc9, 0xa1, 0xfd,
  56440. 0x46, 0xab, 0x2c, 0x9e, 0x5e, 0xc6, 0x74, 0x19, 0x9a, 0xf5, 0x9b, 0x51,
  56441. 0x11, 0x4f, 0xc8, 0xb9, 0x99, 0x6b, 0x4e, 0x3e, 0x31, 0x64, 0xb4, 0x92,
  56442. 0xf4, 0x0d, 0x41, 0x4b, 0x2c, 0x65, 0x23, 0xf7, 0x47, 0xe3, 0xa5, 0x2e,
  56443. 0xe4, 0x9c, 0x2b, 0xc9, 0x41, 0x22, 0x83, 0x8a, 0x23, 0xef, 0x29, 0x7e,
  56444. 0x4f, 0x3f, 0xa3, 0xbf, 0x73, 0x2b, 0xd7, 0xcc, 0xc8, 0xc6, 0xe9, 0xbc,
  56445. 0x01, 0xb7, 0x32, 0x63, 0xd4, 0x7e, 0x7f, 0x9a, 0xaf, 0x5f, 0x05, 0x31,
  56446. 0x53, 0xd6, 0x1f, 0xa2, 0xd0, 0xdf, 0x67, 0x56, 0xf1, 0x9c, 0x4a, 0x9d,
  56447. 0x83, 0xb4, 0xef, 0xb3, 0xf2, 0xcc, 0xf1, 0x91, 0x6c, 0x47, 0xc3, 0x8b,
  56448. 0xd0, 0x92, 0x79, 0x3d, 0xa0, 0xc0, 0x3a, 0x57, 0x26, 0x6d, 0x0a, 0xad,
  56449. 0x5f, 0xad, 0xb4, 0x74, 0x48, 0x4a, 0x51, 0xe1, 0xb5, 0x82, 0x0a, 0x4c,
  56450. 0x4f, 0x9d, 0xaf, 0xee, 0x5a, 0xa2, 0x4d, 0x4d, 0x5f, 0xe0, 0x17, 0x00,
  56451. 0x23, 0x00, 0x00
  56452. };
  56453. byte alert_reply[50];
  56454. byte expected_alert_reply[] = {
  56455. 0x15, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00,
  56456. 0x02, 0x02, 0x2f
  56457. };
  56458. fd = wolfSSL_get_fd(ssl);
  56459. ret = (int)send(fd, ch_msh_invalid_cookie, sizeof(ch_msh_invalid_cookie), 0);
  56460. AssertIntGT(ret, 0);
  56461. /* should reply with an illegal_parameter reply */
  56462. ret = (int)recv(fd, alert_reply, sizeof(alert_reply), 0);
  56463. AssertIntEQ(ret, sizeof(expected_alert_reply));
  56464. AssertIntEQ(XMEMCMP(alert_reply, expected_alert_reply, sizeof(expected_alert_reply)), 0);
  56465. }
  56466. #endif
  56467. static word32 test_wolfSSL_dtls_stateless_HashWOLFSSL(const WOLFSSL* ssl)
  56468. {
  56469. #ifndef NO_MD5
  56470. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  56471. #elif !defined(NO_SHA)
  56472. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  56473. #elif !defined(NO_SHA256)
  56474. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  56475. #else
  56476. #error "We need a digest to hash the WOLFSSL object"
  56477. #endif
  56478. byte hashBuf[WC_MAX_DIGEST_SIZE];
  56479. wc_HashAlg hash;
  56480. const TLSX* exts = ssl->extensions;
  56481. WOLFSSL sslCopy; /* Use a copy to omit certain fields */
  56482. HS_Hashes* hsHashes = ssl->hsHashes; /* Is re-allocated in
  56483. * InitHandshakeHashes */
  56484. XMEMCPY(&sslCopy, ssl, sizeof(*ssl));
  56485. XMEMSET(hashBuf, 0, sizeof(hashBuf));
  56486. /* Following fields are not important to compare */
  56487. XMEMSET(sslCopy.buffers.inputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  56488. sslCopy.buffers.inputBuffer.buffer = NULL;
  56489. sslCopy.buffers.inputBuffer.bufferSize = 0;
  56490. sslCopy.buffers.inputBuffer.dynamicFlag = 0;
  56491. sslCopy.buffers.inputBuffer.offset = 0;
  56492. XMEMSET(sslCopy.buffers.outputBuffer.staticBuffer, 0, STATIC_BUFFER_LEN);
  56493. sslCopy.buffers.outputBuffer.buffer = NULL;
  56494. sslCopy.buffers.outputBuffer.bufferSize = 0;
  56495. sslCopy.buffers.outputBuffer.dynamicFlag = 0;
  56496. sslCopy.buffers.outputBuffer.offset = 0;
  56497. sslCopy.error = 0;
  56498. sslCopy.curSize = 0;
  56499. sslCopy.curStartIdx = 0;
  56500. sslCopy.keys.curSeq_lo = 0;
  56501. XMEMSET(&sslCopy.curRL, 0, sizeof(sslCopy.curRL));
  56502. #ifdef WOLFSSL_DTLS13
  56503. XMEMSET(&sslCopy.keys.curSeq, 0, sizeof(sslCopy.keys.curSeq));
  56504. sslCopy.dtls13FastTimeout = 0;
  56505. #endif
  56506. sslCopy.keys.dtls_peer_handshake_number = 0;
  56507. XMEMSET(&sslCopy.alert_history, 0, sizeof(sslCopy.alert_history));
  56508. sslCopy.hsHashes = NULL;
  56509. #ifdef WOLFSSL_ASYNC_IO
  56510. #ifdef WOLFSSL_ASYNC_CRYPT
  56511. sslCopy.asyncDev = NULL;
  56512. #endif
  56513. sslCopy.async = NULL;
  56514. #endif
  56515. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  56516. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)&sslCopy, sizeof(sslCopy)), 0);
  56517. /* hash extension list */
  56518. while (exts != NULL) {
  56519. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)exts, sizeof(*exts)), 0);
  56520. exts = exts->next;
  56521. }
  56522. /* Hash suites */
  56523. if (sslCopy.suites != NULL) {
  56524. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)sslCopy.suites,
  56525. sizeof(struct Suites)), 0);
  56526. }
  56527. /* Hash hsHashes */
  56528. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)hsHashes,
  56529. sizeof(*hsHashes)), 0);
  56530. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  56531. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  56532. return MakeWordFromHash(hashBuf);
  56533. }
  56534. static CallbackIORecv test_wolfSSL_dtls_compare_stateless_cb;
  56535. static int test_wolfSSL_dtls_compare_stateless_cb_call_once;
  56536. static int test_wolfSSL_dtls_compare_stateless_read_cb_once(WOLFSSL *ssl,
  56537. char *buf, int sz, void *ctx)
  56538. {
  56539. if (test_wolfSSL_dtls_compare_stateless_cb_call_once) {
  56540. test_wolfSSL_dtls_compare_stateless_cb_call_once = 0;
  56541. return test_wolfSSL_dtls_compare_stateless_cb(ssl, buf, sz, ctx);
  56542. }
  56543. else {
  56544. return WOLFSSL_CBIO_ERR_WANT_READ;
  56545. }
  56546. }
  56547. static void test_wolfSSL_dtls_compare_stateless(WOLFSSL* ssl)
  56548. {
  56549. /* Compare the ssl object before and after one ClientHello msg */
  56550. SOCKET_T fd = wolfSSL_get_fd(ssl);
  56551. int res;
  56552. int err;
  56553. word32 initHash;
  56554. test_wolfSSL_dtls_compare_stateless_cb = ssl->CBIORecv;
  56555. test_wolfSSL_dtls_compare_stateless_cb_call_once = 1;
  56556. wolfSSL_dtls_set_using_nonblock(ssl, 1);
  56557. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_read_cb_once;
  56558. initHash = test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl);
  56559. (void)initHash;
  56560. res = tcp_select(fd, 5);
  56561. /* We are expecting a msg. A timeout indicates failure. */
  56562. AssertIntEQ(res, TEST_RECV_READY);
  56563. res = wolfSSL_accept(ssl);
  56564. err = wolfSSL_get_error(ssl, res);
  56565. AssertIntEQ(res, WOLFSSL_FATAL_ERROR);
  56566. AssertIntEQ(err, WOLFSSL_ERROR_WANT_READ);
  56567. AssertIntEQ(initHash, test_wolfSSL_dtls_stateless_HashWOLFSSL(ssl));
  56568. wolfSSL_dtls_set_using_nonblock(ssl, 0);
  56569. ssl->CBIORecv = test_wolfSSL_dtls_compare_stateless_cb;
  56570. }
  56571. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  56572. static void test_wolfSSL_dtls_enable_hrrcookie(WOLFSSL* ssl)
  56573. {
  56574. int ret;
  56575. ret = wolfSSL_send_hrr_cookie(ssl, NULL, 0);
  56576. AssertIntEQ(ret, WOLFSSL_SUCCESS);
  56577. test_wolfSSL_dtls_compare_stateless(ssl);
  56578. }
  56579. #endif
  56580. static int test_wolfSSL_dtls_stateless(void)
  56581. {
  56582. callback_functions client_cbs, server_cbs;
  56583. size_t i;
  56584. struct {
  56585. method_provider client_meth;
  56586. method_provider server_meth;
  56587. ssl_callback client_ssl_ready;
  56588. ssl_callback server_ssl_ready;
  56589. } test_params[] = {
  56590. #if !defined(WOLFSSL_NO_TLS12)
  56591. {wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method,
  56592. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_compare_stateless},
  56593. #endif
  56594. #if defined(WOLFSSL_DTLS13) && defined(WOLFSSL_SEND_HRR_COOKIE)
  56595. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  56596. test_wolfSSL_dtls_send_ch, test_wolfSSL_dtls_enable_hrrcookie},
  56597. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  56598. test_wolfSSL_dtls_send_ch_with_invalid_cookie, test_wolfSSL_dtls_enable_hrrcookie},
  56599. #endif
  56600. };
  56601. if (0 == sizeof(test_params)){
  56602. return TEST_SKIPPED;
  56603. }
  56604. for (i = 0; i < sizeof(test_params)/sizeof(*test_params); i++) {
  56605. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  56606. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  56607. client_cbs.doUdp = server_cbs.doUdp = 1;
  56608. client_cbs.method = test_params[i].client_meth;
  56609. server_cbs.method = test_params[i].server_meth;
  56610. client_cbs.ssl_ready = test_params[i].client_ssl_ready;
  56611. server_cbs.ssl_ready = test_params[i].server_ssl_ready;
  56612. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  56613. if (!client_cbs.return_code)
  56614. return TEST_FAIL;
  56615. if (!server_cbs.return_code)
  56616. return TEST_FAIL;
  56617. }
  56618. return TEST_SUCCESS;
  56619. }
  56620. #else
  56621. static int test_wolfSSL_dtls_stateless(void)
  56622. {
  56623. return TEST_SKIPPED;
  56624. }
  56625. #endif /* WOLFSSL_DTLS13 && WOLFSSL_SEND_HRR_COOKIE &&
  56626. * HAVE_IO_TESTS_DEPENDENCIES && !SINGLE_THREADED */
  56627. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  56628. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  56629. !defined(WOLFSSL_NO_CLIENT_AUTH))
  56630. static int load_ca_into_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  56631. {
  56632. int ret;
  56633. if ((ret = wolfSSL_CertManagerLoadCA(cm, certA, 0)) != WOLFSSL_SUCCESS) {
  56634. fprintf(stderr, "loading cert %s failed\n", certA);
  56635. fprintf(stderr, "Error: (%d): %s\n", ret,
  56636. wolfSSL_ERR_reason_error_string(ret));
  56637. return -1;
  56638. }
  56639. return 0;
  56640. }
  56641. static int verify_cert_with_cm(WOLFSSL_CERT_MANAGER* cm, char* certA)
  56642. {
  56643. int ret;
  56644. if ((ret = wolfSSL_CertManagerVerify(cm, certA, WOLFSSL_FILETYPE_PEM))
  56645. != WOLFSSL_SUCCESS) {
  56646. fprintf(stderr, "could not verify the cert: %s\n", certA);
  56647. fprintf(stderr, "Error: (%d): %s\n", ret,
  56648. wolfSSL_ERR_reason_error_string(ret));
  56649. return -1;
  56650. }
  56651. else {
  56652. fprintf(stderr, "successfully verified: %s\n", certA);
  56653. }
  56654. return 0;
  56655. }
  56656. #define LOAD_ONE_CA(a, b, c, d) \
  56657. do { \
  56658. (a) = load_ca_into_cm(c, d); \
  56659. if ((a) != 0) \
  56660. return (b); \
  56661. else \
  56662. (b)--; \
  56663. } while(0)
  56664. #define VERIFY_ONE_CERT(a, b, c, d) \
  56665. do { \
  56666. (a) = verify_cert_with_cm(c, d);\
  56667. if ((a) != 0) \
  56668. return (b); \
  56669. else \
  56670. (b)--; \
  56671. } while(0)
  56672. static int test_chainG(WOLFSSL_CERT_MANAGER* cm)
  56673. {
  56674. int ret;
  56675. int i = -1;
  56676. /* Chain G is a valid chain per RFC 5280 section 4.2.1.9 */
  56677. char chainGArr[9][50] = {"certs/ca-cert.pem",
  56678. "certs/test-pathlen/chainG-ICA7-pathlen100.pem",
  56679. "certs/test-pathlen/chainG-ICA6-pathlen10.pem",
  56680. "certs/test-pathlen/chainG-ICA5-pathlen20.pem",
  56681. "certs/test-pathlen/chainG-ICA4-pathlen5.pem",
  56682. "certs/test-pathlen/chainG-ICA3-pathlen99.pem",
  56683. "certs/test-pathlen/chainG-ICA2-pathlen1.pem",
  56684. "certs/test-pathlen/chainG-ICA1-pathlen0.pem",
  56685. "certs/test-pathlen/chainG-entity.pem"};
  56686. LOAD_ONE_CA(ret, i, cm, chainGArr[0]); /* if failure, i = -1 here */
  56687. LOAD_ONE_CA(ret, i, cm, chainGArr[1]); /* if failure, i = -2 here */
  56688. LOAD_ONE_CA(ret, i, cm, chainGArr[2]); /* if failure, i = -3 here */
  56689. LOAD_ONE_CA(ret, i, cm, chainGArr[3]); /* if failure, i = -4 here */
  56690. LOAD_ONE_CA(ret, i, cm, chainGArr[4]); /* if failure, i = -5 here */
  56691. LOAD_ONE_CA(ret, i, cm, chainGArr[5]); /* if failure, i = -6 here */
  56692. LOAD_ONE_CA(ret, i, cm, chainGArr[6]); /* if failure, i = -7 here */
  56693. LOAD_ONE_CA(ret, i, cm, chainGArr[7]); /* if failure, i = -8 here */
  56694. VERIFY_ONE_CERT(ret, i, cm, chainGArr[1]); /* if failure, i = -9 here */
  56695. VERIFY_ONE_CERT(ret, i, cm, chainGArr[2]); /* if failure, i = -10 here */
  56696. VERIFY_ONE_CERT(ret, i, cm, chainGArr[3]); /* if failure, i = -11 here */
  56697. VERIFY_ONE_CERT(ret, i, cm, chainGArr[4]); /* if failure, i = -12 here */
  56698. VERIFY_ONE_CERT(ret, i, cm, chainGArr[5]); /* if failure, i = -13 here */
  56699. VERIFY_ONE_CERT(ret, i, cm, chainGArr[6]); /* if failure, i = -14 here */
  56700. VERIFY_ONE_CERT(ret, i, cm, chainGArr[7]); /* if failure, i = -15 here */
  56701. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -16 here */
  56702. /* test validating the entity twice, should have no effect on pathLen since
  56703. * entity/leaf cert */
  56704. VERIFY_ONE_CERT(ret, i, cm, chainGArr[8]); /* if failure, i = -17 here */
  56705. return ret;
  56706. }
  56707. static int test_chainH(WOLFSSL_CERT_MANAGER* cm)
  56708. {
  56709. int ret;
  56710. int i = -1;
  56711. /* Chain H is NOT a valid chain per RFC5280 section 4.2.1.9:
  56712. * ICA4-pathlen of 2 signing ICA3-pathlen of 2 (reduce max path len to 2)
  56713. * ICA3-pathlen of 2 signing ICA2-pathlen of 2 (reduce max path len to 1)
  56714. * ICA2-pathlen of 2 signing ICA1-pathlen of 0 (reduce max path len to 0)
  56715. * ICA1-pathlen of 0 signing entity (pathlen is already 0, ERROR)
  56716. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  56717. */
  56718. char chainHArr[6][50] = {"certs/ca-cert.pem",
  56719. "certs/test-pathlen/chainH-ICA4-pathlen2.pem",
  56720. "certs/test-pathlen/chainH-ICA3-pathlen2.pem",
  56721. "certs/test-pathlen/chainH-ICA2-pathlen2.pem",
  56722. "certs/test-pathlen/chainH-ICA1-pathlen0.pem",
  56723. "certs/test-pathlen/chainH-entity.pem"};
  56724. LOAD_ONE_CA(ret, i, cm, chainHArr[0]); /* if failure, i = -1 here */
  56725. LOAD_ONE_CA(ret, i, cm, chainHArr[1]); /* if failure, i = -2 here */
  56726. LOAD_ONE_CA(ret, i, cm, chainHArr[2]); /* if failure, i = -3 here */
  56727. LOAD_ONE_CA(ret, i, cm, chainHArr[3]); /* if failure, i = -4 here */
  56728. LOAD_ONE_CA(ret, i, cm, chainHArr[4]); /* if failure, i = -5 here */
  56729. VERIFY_ONE_CERT(ret, i, cm, chainHArr[1]); /* if failure, i = -6 here */
  56730. VERIFY_ONE_CERT(ret, i, cm, chainHArr[2]); /* if failure, i = -7 here */
  56731. VERIFY_ONE_CERT(ret, i, cm, chainHArr[3]); /* if failure, i = -8 here */
  56732. VERIFY_ONE_CERT(ret, i, cm, chainHArr[4]); /* if failure, i = -9 here */
  56733. VERIFY_ONE_CERT(ret, i, cm, chainHArr[5]); /* if failure, i = -10 here */
  56734. return ret;
  56735. }
  56736. static int test_chainI(WOLFSSL_CERT_MANAGER* cm)
  56737. {
  56738. int ret;
  56739. int i = -1;
  56740. /* Chain I is a valid chain per RFC5280 section 4.2.1.9:
  56741. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 2)
  56742. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 1)
  56743. * ICA1-no_pathlen signing entity (reduce maxPathLen to 0)
  56744. * Test should successfully verify ICA4, ICA3, ICA2 and then fail on ICA1
  56745. */
  56746. char chainIArr[5][50] = {"certs/ca-cert.pem",
  56747. "certs/test-pathlen/chainI-ICA3-pathlen2.pem",
  56748. "certs/test-pathlen/chainI-ICA2-no_pathlen.pem",
  56749. "certs/test-pathlen/chainI-ICA1-no_pathlen.pem",
  56750. "certs/test-pathlen/chainI-entity.pem"};
  56751. LOAD_ONE_CA(ret, i, cm, chainIArr[0]); /* if failure, i = -1 here */
  56752. LOAD_ONE_CA(ret, i, cm, chainIArr[1]); /* if failure, i = -2 here */
  56753. LOAD_ONE_CA(ret, i, cm, chainIArr[2]); /* if failure, i = -3 here */
  56754. LOAD_ONE_CA(ret, i, cm, chainIArr[3]); /* if failure, i = -4 here */
  56755. VERIFY_ONE_CERT(ret, i, cm, chainIArr[1]); /* if failure, i = -5 here */
  56756. VERIFY_ONE_CERT(ret, i, cm, chainIArr[2]); /* if failure, i = -6 here */
  56757. VERIFY_ONE_CERT(ret, i, cm, chainIArr[3]); /* if failure, i = -7 here */
  56758. VERIFY_ONE_CERT(ret, i, cm, chainIArr[4]); /* if failure, i = -8 here */
  56759. return ret;
  56760. }
  56761. static int test_chainJ(WOLFSSL_CERT_MANAGER* cm)
  56762. {
  56763. int ret;
  56764. int i = -1;
  56765. /* Chain J is NOT a valid chain per RFC5280 section 4.2.1.9:
  56766. * ICA4-pathlen of 2 signing ICA3 without a pathlen (reduce maxPathLen to 2)
  56767. * ICA3-pathlen of 2 signing ICA2 without a pathlen (reduce maxPathLen to 1)
  56768. * ICA2-no_pathlen signing ICA1-no_pathlen (reduce maxPathLen to 0)
  56769. * ICA1-no_pathlen signing entity (ERROR, pathlen zero and non-leaf cert)
  56770. */
  56771. char chainJArr[6][50] = {"certs/ca-cert.pem",
  56772. "certs/test-pathlen/chainJ-ICA4-pathlen2.pem",
  56773. "certs/test-pathlen/chainJ-ICA3-no_pathlen.pem",
  56774. "certs/test-pathlen/chainJ-ICA2-no_pathlen.pem",
  56775. "certs/test-pathlen/chainJ-ICA1-no_pathlen.pem",
  56776. "certs/test-pathlen/chainJ-entity.pem"};
  56777. LOAD_ONE_CA(ret, i, cm, chainJArr[0]); /* if failure, i = -1 here */
  56778. LOAD_ONE_CA(ret, i, cm, chainJArr[1]); /* if failure, i = -2 here */
  56779. LOAD_ONE_CA(ret, i, cm, chainJArr[2]); /* if failure, i = -3 here */
  56780. LOAD_ONE_CA(ret, i, cm, chainJArr[3]); /* if failure, i = -4 here */
  56781. LOAD_ONE_CA(ret, i, cm, chainJArr[4]); /* if failure, i = -5 here */
  56782. VERIFY_ONE_CERT(ret, i, cm, chainJArr[1]); /* if failure, i = -6 here */
  56783. VERIFY_ONE_CERT(ret, i, cm, chainJArr[2]); /* if failure, i = -7 here */
  56784. VERIFY_ONE_CERT(ret, i, cm, chainJArr[3]); /* if failure, i = -8 here */
  56785. VERIFY_ONE_CERT(ret, i, cm, chainJArr[4]); /* if failure, i = -9 here */
  56786. VERIFY_ONE_CERT(ret, i, cm, chainJArr[5]); /* if failure, i = -10 here */
  56787. return ret;
  56788. }
  56789. static int test_various_pathlen_chains(void)
  56790. {
  56791. EXPECT_DECLS;
  56792. WOLFSSL_CERT_MANAGER* cm = NULL;
  56793. /* Test chain G (large chain with varying pathLens) */
  56794. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56795. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  56796. ExpectIntEQ(test_chainG(cm), -1);
  56797. #else
  56798. ExpectIntEQ(test_chainG(cm), 0);
  56799. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  56800. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56801. wolfSSL_CertManagerFree(cm);
  56802. /* end test chain G */
  56803. /* Test chain H (5 chain with same pathLens) */
  56804. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56805. ExpectIntLT(test_chainH(cm), 0);
  56806. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56807. wolfSSL_CertManagerFree(cm);
  56808. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56809. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56810. wolfSSL_CertManagerFree(cm);
  56811. /* end test chain H */
  56812. /* Test chain I (only first ICA has pathLen set and it's set to 2,
  56813. * followed by 2 ICA's, should pass) */
  56814. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56815. #if defined(NO_WOLFSSL_CLIENT) && defined(NO_WOLFSSL_SERVER)
  56816. ExpectIntEQ(test_chainI(cm), -1);
  56817. #else
  56818. ExpectIntEQ(test_chainI(cm), 0);
  56819. #endif /* NO_WOLFSSL_CLIENT && NO_WOLFSSL_SERVER */
  56820. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56821. wolfSSL_CertManagerFree(cm);
  56822. cm = NULL;
  56823. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56824. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56825. wolfSSL_CertManagerFree(cm);
  56826. cm = NULL;
  56827. /* Test chain J (Again only first ICA has pathLen set and it's set to 2,
  56828. * this time followed by 3 ICA's, should fail */
  56829. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56830. ExpectIntLT(test_chainJ(cm), 0);
  56831. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56832. wolfSSL_CertManagerFree(cm);
  56833. cm = NULL;
  56834. ExpectNotNull(cm = wolfSSL_CertManagerNew());
  56835. ExpectIntEQ(wolfSSL_CertManagerUnloadCAs(cm), WOLFSSL_SUCCESS);
  56836. wolfSSL_CertManagerFree(cm);
  56837. return EXPECT_RESULT();
  56838. }
  56839. #endif /* !NO_RSA && !NO_SHA && !NO_FILESYSTEM && !NO_CERTS */
  56840. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  56841. static int test_export_keying_material_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  56842. {
  56843. EXPECT_DECLS;
  56844. byte ekm[100] = {0};
  56845. (void)ctx;
  56846. /* Success Cases */
  56847. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56848. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 0), 1);
  56849. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56850. "Test label", XSTR_SIZEOF("Test label"), NULL, 0, 1), 1);
  56851. /* Use some random context */
  56852. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56853. "Test label", XSTR_SIZEOF("Test label"), ekm, 10, 1), 1);
  56854. /* Failure cases */
  56855. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56856. "client finished", XSTR_SIZEOF("client finished"), NULL, 0, 0), 0);
  56857. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56858. "server finished", XSTR_SIZEOF("server finished"), NULL, 0, 0), 0);
  56859. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56860. "master secret", XSTR_SIZEOF("master secret"), NULL, 0, 0), 0);
  56861. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56862. "extended master secret", XSTR_SIZEOF("extended master secret"),
  56863. NULL, 0, 0), 0);
  56864. ExpectIntEQ(wolfSSL_export_keying_material(ssl, ekm, sizeof(ekm),
  56865. "key expansion", XSTR_SIZEOF("key expansion"), NULL, 0, 0), 0);
  56866. return EXPECT_RESULT();
  56867. }
  56868. static int test_export_keying_material_ssl_cb(WOLFSSL* ssl)
  56869. {
  56870. wolfSSL_KeepArrays(ssl);
  56871. return TEST_SUCCESS;
  56872. }
  56873. static int test_export_keying_material(void)
  56874. {
  56875. EXPECT_DECLS;
  56876. test_ssl_cbf serverCb;
  56877. test_ssl_cbf clientCb;
  56878. XMEMSET(&serverCb, 0, sizeof(serverCb));
  56879. XMEMSET(&clientCb, 0, sizeof(clientCb));
  56880. clientCb.ssl_ready = test_export_keying_material_ssl_cb;
  56881. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&clientCb,
  56882. &serverCb, test_export_keying_material_cb), TEST_SUCCESS);
  56883. return EXPECT_RESULT();
  56884. }
  56885. #endif /* HAVE_KEYING_MATERIAL */
  56886. static int test_wolfSSL_THREADID_hash(void)
  56887. {
  56888. EXPECT_DECLS;
  56889. #if defined(OPENSSL_EXTRA)
  56890. CRYPTO_THREADID id;
  56891. CRYPTO_THREADID_current(NULL);
  56892. /* Hash result is unsigned long. */
  56893. ExpectTrue(CRYPTO_THREADID_hash(NULL) == 0UL);
  56894. XMEMSET(&id, 0, sizeof(id));
  56895. ExpectTrue(CRYPTO_THREADID_hash(&id) == 0UL);
  56896. #endif /* OPENSSL_EXTRA */
  56897. return EXPECT_RESULT();
  56898. }
  56899. static int test_wolfSSL_set_ecdh_auto(void)
  56900. {
  56901. EXPECT_DECLS;
  56902. #if defined(OPENSSL_EXTRA)
  56903. WOLFSSL* ssl = NULL;
  56904. ExpectIntEQ(SSL_set_ecdh_auto(NULL,0), 1);
  56905. ExpectIntEQ(SSL_set_ecdh_auto(NULL,1), 1);
  56906. ExpectIntEQ(SSL_set_ecdh_auto(ssl,0), 1);
  56907. ExpectIntEQ(SSL_set_ecdh_auto(ssl,1), 1);
  56908. #endif /* OPENSSL_EXTRA */
  56909. return EXPECT_RESULT();
  56910. }
  56911. static int test_wolfSSL_CTX_set_ecdh_auto(void)
  56912. {
  56913. EXPECT_DECLS;
  56914. #if defined(OPENSSL_EXTRA)
  56915. WOLFSSL_CTX* ctx = NULL;
  56916. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,0), 1);
  56917. ExpectIntEQ(SSL_CTX_set_ecdh_auto(NULL,1), 1);
  56918. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,0), 1);
  56919. ExpectIntEQ(SSL_CTX_set_ecdh_auto(ctx,1), 1);
  56920. #endif /* OPENSSL_EXTRA */
  56921. return EXPECT_RESULT();
  56922. }
  56923. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  56924. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  56925. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_server_thread(void* args)
  56926. {
  56927. EXPECT_DECLS;
  56928. callback_functions* callbacks = NULL;
  56929. WOLFSSL_CTX* ctx = NULL;
  56930. WOLFSSL* ssl = NULL;
  56931. SOCKET_T sfd = 0;
  56932. SOCKET_T cfd = 0;
  56933. word16 port;
  56934. char msg[] = "I hear you fa shizzle!";
  56935. int len = (int) XSTRLEN(msg);
  56936. char input[1024];
  56937. int ret = 0;
  56938. int err = 0;
  56939. if (!args)
  56940. WOLFSSL_RETURN_FROM_THREAD(0);
  56941. ((func_args*)args)->return_code = TEST_FAIL;
  56942. callbacks = ((func_args*)args)->callbacks;
  56943. ctx = wolfSSL_CTX_new(callbacks->method());
  56944. #if defined(USE_WINDOWS_API)
  56945. port = ((func_args*)args)->signal->port;
  56946. #else
  56947. /* Let tcp_listen assign port */
  56948. port = 0;
  56949. #endif
  56950. #ifdef WOLFSSL_TIRTOS
  56951. fdOpenSession(Task_self());
  56952. #endif
  56953. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  56954. caCertFile, 0));
  56955. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  56956. svrCertFile, WOLFSSL_FILETYPE_PEM));
  56957. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  56958. svrKeyFile, WOLFSSL_FILETYPE_PEM));
  56959. #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
  56960. ExpectIntEQ(wolfSSL_CTX_SetTmpDH_file(ctx, dhParamFile,
  56961. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  56962. #elif !defined(NO_DH)
  56963. SetDHCtx(ctx); /* will repick suites with DHE, higher priority than PSK */
  56964. #endif
  56965. if (callbacks->ctx_ready)
  56966. callbacks->ctx_ready(ctx);
  56967. ssl = wolfSSL_new(ctx);
  56968. ExpectNotNull(ssl);
  56969. /* listen and accept */
  56970. tcp_accept(&sfd, &cfd, (func_args*)args, port, 0, 0, 0, 0, 1, 0, 0);
  56971. CloseSocket(sfd);
  56972. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, cfd));
  56973. if (callbacks->ssl_ready)
  56974. callbacks->ssl_ready(ssl);
  56975. if (EXPECT_SUCCESS()) {
  56976. do {
  56977. err = 0; /* Reset error */
  56978. ret = wolfSSL_accept(ssl);
  56979. if (ret != WOLFSSL_SUCCESS) {
  56980. err = wolfSSL_get_error(ssl, 0);
  56981. }
  56982. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  56983. }
  56984. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  56985. /* read and write data */
  56986. XMEMSET(input, 0, sizeof(input));
  56987. while (EXPECT_SUCCESS()) {
  56988. ret = wolfSSL_read(ssl, input, sizeof(input));
  56989. if (ret > 0) {
  56990. break;
  56991. }
  56992. else {
  56993. err = wolfSSL_get_error(ssl,ret);
  56994. if (err == WOLFSSL_ERROR_WANT_READ) {
  56995. continue;
  56996. }
  56997. break;
  56998. }
  56999. }
  57000. if (EXPECT_SUCCESS() && (err == WOLFSSL_ERROR_ZERO_RETURN)) {
  57001. do {
  57002. ret = wolfSSL_write(ssl, msg, len);
  57003. if (ret > 0) {
  57004. break;
  57005. }
  57006. } while (ret < 0);
  57007. }
  57008. /* bidirectional shutdown */
  57009. while (EXPECT_SUCCESS()) {
  57010. ret = wolfSSL_shutdown(ssl);
  57011. ExpectIntNE(ret, WOLFSSL_FATAL_ERROR);
  57012. if (ret == WOLFSSL_SUCCESS) {
  57013. break;
  57014. }
  57015. }
  57016. if (EXPECT_SUCCESS()) {
  57017. /* wait for the peer to disconnect the tcp connection */
  57018. do {
  57019. ret = wolfSSL_read(ssl, input, sizeof(input));
  57020. err = wolfSSL_get_error(ssl, ret);
  57021. } while (ret > 0 || err != WOLFSSL_ERROR_ZERO_RETURN);
  57022. }
  57023. /* detect TCP disconnect */
  57024. ExpectIntLE(ret,WOLFSSL_FAILURE);
  57025. ExpectIntEQ(wolfSSL_get_error(ssl, ret), WOLFSSL_ERROR_ZERO_RETURN);
  57026. ((func_args*)args)->return_code = EXPECT_RESULT();
  57027. wolfSSL_free(ssl);
  57028. wolfSSL_CTX_free(ctx);
  57029. CloseSocket(cfd);
  57030. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  57031. wc_ecc_fp_free(); /* free per thread cache */
  57032. #endif
  57033. WOLFSSL_RETURN_FROM_THREAD(0);
  57034. }
  57035. static THREAD_RETURN WOLFSSL_THREAD SSL_read_test_client_thread(void* args)
  57036. {
  57037. EXPECT_DECLS;
  57038. callback_functions* callbacks = NULL;
  57039. WOLFSSL_CTX* ctx = NULL;
  57040. WOLFSSL* ssl = NULL;
  57041. SOCKET_T sfd = 0;
  57042. char msg[] = "hello wolfssl server!";
  57043. int len = (int) XSTRLEN(msg);
  57044. char input[1024];
  57045. int idx;
  57046. int ret, err;
  57047. if (!args)
  57048. WOLFSSL_RETURN_FROM_THREAD(0);
  57049. ((func_args*)args)->return_code = TEST_FAIL;
  57050. callbacks = ((func_args*)args)->callbacks;
  57051. ctx = wolfSSL_CTX_new(callbacks->method());
  57052. #ifdef WOLFSSL_TIRTOS
  57053. fdOpenSession(Task_self());
  57054. #endif
  57055. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_load_verify_locations(ctx,
  57056. caCertFile, 0));
  57057. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_certificate_file(ctx,
  57058. cliCertFile, WOLFSSL_FILETYPE_PEM));
  57059. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_use_PrivateKey_file(ctx,
  57060. cliKeyFile, WOLFSSL_FILETYPE_PEM));
  57061. ExpectNotNull((ssl = wolfSSL_new(ctx)));
  57062. tcp_connect(&sfd, wolfSSLIP, ((func_args*)args)->signal->port, 0, 0, ssl);
  57063. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_set_fd(ssl, sfd));
  57064. if (EXPECT_SUCCESS()) {
  57065. do {
  57066. err = 0; /* Reset error */
  57067. ret = wolfSSL_connect(ssl);
  57068. if (ret != WOLFSSL_SUCCESS) {
  57069. err = wolfSSL_get_error(ssl, 0);
  57070. }
  57071. } while (ret != WOLFSSL_SUCCESS && err == WC_PENDING_E);
  57072. }
  57073. ExpectIntGE(wolfSSL_write(ssl, msg, len), 0);
  57074. if (EXPECT_SUCCESS()) {
  57075. if (0 < (idx = wolfSSL_read(ssl, input, sizeof(input)-1))) {
  57076. input[idx] = 0;
  57077. }
  57078. }
  57079. if (EXPECT_SUCCESS()) {
  57080. ret = wolfSSL_shutdown(ssl);
  57081. if (ret == WOLFSSL_SHUTDOWN_NOT_DONE) {
  57082. ret = wolfSSL_shutdown(ssl);
  57083. }
  57084. }
  57085. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  57086. ((func_args*)args)->return_code = EXPECT_RESULT();
  57087. wolfSSL_free(ssl);
  57088. wolfSSL_CTX_free(ctx);
  57089. CloseSocket(sfd);
  57090. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS)
  57091. wc_ecc_fp_free(); /* free per thread cache */
  57092. #endif
  57093. WOLFSSL_RETURN_FROM_THREAD(0);
  57094. }
  57095. #endif /* OPENSSL_EXTRA && WOLFSSL_ERROR_CODE_OPENSSL &&
  57096. HAVE_IO_TESTS_DEPENDENCIES && !WOLFSSL_NO_TLS12 */
  57097. /* This test is to check wolfSSL_read behaves as same as
  57098. * openSSL when it is called after SSL_shutdown completes.
  57099. */
  57100. static int test_wolfSSL_read_detect_TCP_disconnect(void)
  57101. {
  57102. EXPECT_DECLS;
  57103. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_ERROR_CODE_OPENSSL) && \
  57104. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  57105. tcp_ready ready;
  57106. func_args client_args;
  57107. func_args server_args;
  57108. THREAD_TYPE serverThread;
  57109. THREAD_TYPE clientThread;
  57110. callback_functions server_cbf;
  57111. callback_functions client_cbf;
  57112. #ifdef WOLFSSL_TIRTOS
  57113. fdOpenSession(Task_self());
  57114. #endif
  57115. StartTCP();
  57116. InitTcpReady(&ready);
  57117. #if defined(USE_WINDOWS_API)
  57118. /* use RNG to get random port if using windows */
  57119. ready.port = GetRandomPort();
  57120. #endif
  57121. XMEMSET(&client_args, 0, sizeof(func_args));
  57122. XMEMSET(&server_args, 0, sizeof(func_args));
  57123. XMEMSET(&server_cbf, 0, sizeof(callback_functions));
  57124. XMEMSET(&client_cbf, 0, sizeof(callback_functions));
  57125. server_cbf.method = wolfTLSv1_2_server_method;
  57126. client_cbf.method = wolfTLSv1_2_client_method;
  57127. server_args.callbacks = &server_cbf;
  57128. client_args.callbacks = &client_cbf;
  57129. server_args.signal = &ready;
  57130. client_args.signal = &ready;
  57131. start_thread(SSL_read_test_server_thread, &server_args, &serverThread);
  57132. wait_tcp_ready(&server_args);
  57133. start_thread(SSL_read_test_client_thread, &client_args, &clientThread);
  57134. join_thread(clientThread);
  57135. join_thread(serverThread);
  57136. ExpectTrue(client_args.return_code);
  57137. ExpectTrue(server_args.return_code);
  57138. FreeTcpReady(&ready);
  57139. #endif
  57140. return EXPECT_RESULT();
  57141. }
  57142. static int test_wolfSSL_CTX_get_min_proto_version(void)
  57143. {
  57144. EXPECT_DECLS;
  57145. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)
  57146. WOLFSSL_CTX *ctx = NULL;
  57147. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  57148. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, SSL3_VERSION),
  57149. WOLFSSL_SUCCESS);
  57150. #ifdef WOLFSSL_ALLOW_SSLV3
  57151. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  57152. #else
  57153. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), SSL3_VERSION);
  57154. #endif
  57155. wolfSSL_CTX_free(ctx);
  57156. ctx = NULL;
  57157. #ifndef NO_OLD_TLS
  57158. #ifdef WOLFSSL_ALLOW_TLSV10
  57159. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_method()));
  57160. #else
  57161. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  57162. #endif
  57163. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_VERSION),
  57164. WOLFSSL_SUCCESS);
  57165. #ifdef WOLFSSL_ALLOW_TLSV10
  57166. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  57167. #else
  57168. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_VERSION);
  57169. #endif
  57170. wolfSSL_CTX_free(ctx);
  57171. ctx = NULL;
  57172. #endif
  57173. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_method()));
  57174. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_1_VERSION),
  57175. WOLFSSL_SUCCESS);
  57176. #ifndef NO_OLD_TLS
  57177. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  57178. #else
  57179. ExpectIntGT(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_1_VERSION);
  57180. #endif
  57181. wolfSSL_CTX_free(ctx);
  57182. ctx = NULL;
  57183. #ifndef WOLFSSL_NO_TLS12
  57184. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_2_method()));
  57185. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION),
  57186. WOLFSSL_SUCCESS);
  57187. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_2_VERSION);
  57188. wolfSSL_CTX_free(ctx);
  57189. ctx = NULL;
  57190. #endif
  57191. #ifdef WOLFSSL_TLS13
  57192. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_method()));
  57193. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx, TLS1_3_VERSION),
  57194. WOLFSSL_SUCCESS);
  57195. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx), TLS1_3_VERSION);
  57196. wolfSSL_CTX_free(ctx);
  57197. ctx = NULL;
  57198. #endif
  57199. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  57200. return EXPECT_RESULT();
  57201. }
  57202. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  57203. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  57204. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  57205. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  57206. static int test_wolfSSL_set_SSL_CTX(void)
  57207. {
  57208. EXPECT_DECLS;
  57209. #if (defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL)) \
  57210. && !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_TLS13) && \
  57211. !defined(NO_RSA)
  57212. WOLFSSL_CTX *ctx1 = NULL;
  57213. WOLFSSL_CTX *ctx2 = NULL;
  57214. WOLFSSL *ssl = NULL;
  57215. const byte *session_id1 = (const byte *)"CTX1";
  57216. const byte *session_id2 = (const byte *)"CTX2";
  57217. ExpectNotNull(ctx1 = wolfSSL_CTX_new(wolfTLS_server_method()));
  57218. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx1, svrCertFile,
  57219. WOLFSSL_FILETYPE_PEM));
  57220. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx1, svrKeyFile,
  57221. WOLFSSL_FILETYPE_PEM));
  57222. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx1, TLS1_2_VERSION),
  57223. WOLFSSL_SUCCESS);
  57224. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx1), TLS1_2_VERSION);
  57225. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx1), TLS1_3_VERSION);
  57226. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx1, session_id1, 4),
  57227. WOLFSSL_SUCCESS);
  57228. ExpectNotNull(ctx2 = wolfSSL_CTX_new(wolfTLS_server_method()));
  57229. ExpectTrue(wolfSSL_CTX_use_certificate_file(ctx2, svrCertFile,
  57230. WOLFSSL_FILETYPE_PEM));
  57231. ExpectTrue(wolfSSL_CTX_use_PrivateKey_file(ctx2, svrKeyFile,
  57232. WOLFSSL_FILETYPE_PEM));
  57233. ExpectIntEQ(wolfSSL_CTX_set_min_proto_version(ctx2, TLS1_2_VERSION),
  57234. WOLFSSL_SUCCESS);
  57235. ExpectIntEQ(wolfSSL_CTX_set_max_proto_version(ctx2, TLS1_2_VERSION),
  57236. WOLFSSL_SUCCESS);
  57237. ExpectIntEQ(wolfSSL_CTX_get_min_proto_version(ctx2), TLS1_2_VERSION);
  57238. ExpectIntEQ(wolfSSL_CTX_get_max_proto_version(ctx2), TLS1_2_VERSION);
  57239. ExpectIntEQ(wolfSSL_CTX_set_session_id_context(ctx2, session_id2, 4),
  57240. WOLFSSL_SUCCESS);
  57241. #ifdef HAVE_SESSION_TICKET
  57242. ExpectIntEQ((wolfSSL_CTX_get_options(ctx1) & SSL_OP_NO_TICKET), 0);
  57243. wolfSSL_CTX_set_options(ctx2, SSL_OP_NO_TICKET);
  57244. ExpectIntNE((wolfSSL_CTX_get_options(ctx2) & SSL_OP_NO_TICKET), 0);
  57245. #endif
  57246. ExpectNotNull(ssl = wolfSSL_new(ctx2));
  57247. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  57248. #ifdef WOLFSSL_INT_H
  57249. #ifdef WOLFSSL_SESSION_ID_CTX
  57250. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id2, 4), 0);
  57251. #endif
  57252. ExpectTrue(ssl->buffers.certificate == ctx2->certificate);
  57253. ExpectTrue(ssl->buffers.certChain == ctx2->certChain);
  57254. #endif
  57255. #ifdef HAVE_SESSION_TICKET
  57256. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  57257. #endif
  57258. /* Set the ctx1 that has TLSv1.3 as max proto version */
  57259. ExpectNotNull(wolfSSL_set_SSL_CTX(ssl, ctx1));
  57260. /* MUST not change proto versions of ssl */
  57261. ExpectIntNE((wolfSSL_get_options(ssl) & WOLFSSL_OP_NO_TLSv1_3), 0);
  57262. #ifdef HAVE_SESSION_TICKET
  57263. /* MUST not change */
  57264. ExpectIntNE((wolfSSL_get_options(ssl) & SSL_OP_NO_TICKET), 0);
  57265. #endif
  57266. /* MUST change */
  57267. #ifdef WOLFSSL_INT_H
  57268. ExpectTrue(ssl->buffers.certificate == ctx1->certificate);
  57269. ExpectTrue(ssl->buffers.certChain == ctx1->certChain);
  57270. #ifdef WOLFSSL_SESSION_ID_CTX
  57271. ExpectIntEQ(XMEMCMP(ssl->sessionCtx, session_id1, 4), 0);
  57272. #endif
  57273. #endif
  57274. wolfSSL_free(ssl);
  57275. wolfSSL_CTX_free(ctx1);
  57276. wolfSSL_CTX_free(ctx2);
  57277. #endif /* defined(OPENSSL_EXTRA) || defined(OPENSSL_ALL) */
  57278. return EXPECT_RESULT();
  57279. }
  57280. #endif /* defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  57281. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  57282. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  57283. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))) */
  57284. static int test_wolfSSL_security_level(void)
  57285. {
  57286. EXPECT_DECLS;
  57287. #if defined(OPENSSL_EXTRA)
  57288. SSL_CTX *ctx = NULL;
  57289. #ifdef WOLFSSL_TLS13
  57290. #ifdef NO_WOLFSSL_SERVER
  57291. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  57292. #else
  57293. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  57294. #endif
  57295. SSL_CTX_set_security_level(NULL, 1);
  57296. SSL_CTX_set_security_level(ctx, 1);
  57297. ExpectIntEQ(SSL_CTX_get_security_level(NULL), 0);
  57298. /* Stub so nothing happens. */
  57299. ExpectIntEQ(SSL_CTX_get_security_level(ctx), 0);
  57300. SSL_CTX_free(ctx);
  57301. #else
  57302. (void)ctx;
  57303. #endif
  57304. #endif
  57305. return EXPECT_RESULT();
  57306. }
  57307. static int test_wolfSSL_SSL_in_init(void)
  57308. {
  57309. EXPECT_DECLS;
  57310. #if defined(OPENSSL_ALL) && !defined(NO_BIO)
  57311. SSL_CTX* ctx = NULL;
  57312. SSL* ssl = NULL;
  57313. const char* testCertFile;
  57314. const char* testKeyFile;
  57315. #ifdef WOLFSSL_TLS13
  57316. #ifdef NO_WOLFSSL_SERVER
  57317. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  57318. #else
  57319. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  57320. #endif
  57321. #else
  57322. #ifdef NO_WOLFSSL_SERVER
  57323. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  57324. #else
  57325. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  57326. #endif
  57327. #endif
  57328. #ifndef NO_RSA
  57329. testCertFile = svrCertFile;
  57330. testKeyFile = svrKeyFile;
  57331. #elif defined(HAVE_ECC)
  57332. testCertFile = eccCertFile;
  57333. testKeyFile = eccKeyFile;
  57334. #else
  57335. testCertFile = NULL;
  57336. testKeyFile = NULL;
  57337. #endif
  57338. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  57339. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  57340. SSL_FILETYPE_PEM));
  57341. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  57342. SSL_FILETYPE_PEM));
  57343. }
  57344. ExpectNotNull(ssl = SSL_new(ctx));
  57345. ExpectIntEQ(SSL_in_init(ssl), 1);
  57346. SSL_CTX_free(ctx);
  57347. SSL_free(ssl);
  57348. #endif
  57349. return EXPECT_RESULT();
  57350. }
  57351. static int test_wolfSSL_CTX_set_timeout(void)
  57352. {
  57353. EXPECT_DECLS;
  57354. #if !defined(NO_WOLFSSL_SERVER) && !defined(NO_SESSION_CACHE)
  57355. int timeout;
  57356. WOLFSSL_CTX* ctx = NULL;
  57357. (void)timeout;
  57358. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  57359. #if defined(WOLFSSL_ERROR_CODE_OPENSSL)
  57360. /* in WOLFSSL_ERROR_CODE_OPENSSL macro guard,
  57361. * wolfSSL_CTX_set_timeout returns previous timeout value on success.
  57362. */
  57363. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  57364. /* giving 0 as timeout value sets default timeout */
  57365. timeout = wolfSSL_CTX_set_timeout(ctx, 0);
  57366. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 20), timeout);
  57367. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 30), 20);
  57368. #else
  57369. ExpectIntEQ(wolfSSL_CTX_set_timeout(NULL, 0), BAD_FUNC_ARG);
  57370. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 100), 1);
  57371. ExpectIntEQ(wolfSSL_CTX_set_timeout(ctx, 0), 1);
  57372. #endif
  57373. wolfSSL_CTX_free(ctx);
  57374. #endif /* !NO_WOLFSSL_SERVER && !NO_SESSION_CACHE*/
  57375. return EXPECT_RESULT();
  57376. }
  57377. static int test_wolfSSL_OpenSSL_version(void)
  57378. {
  57379. EXPECT_DECLS;
  57380. #if defined(OPENSSL_EXTRA)
  57381. const char* ver;
  57382. #if defined(OPENSSL_VERSION_NUMBER) && OPENSSL_VERSION_NUMBER >= 0x10100000L
  57383. ExpectNotNull(ver = OpenSSL_version(0));
  57384. #else
  57385. ExpectNotNull(ver = OpenSSL_version());
  57386. #endif
  57387. ExpectIntEQ(XMEMCMP(ver, "wolfSSL " LIBWOLFSSL_VERSION_STRING,
  57388. XSTRLEN("wolfSSL " LIBWOLFSSL_VERSION_STRING)), 0);
  57389. #endif
  57390. return EXPECT_RESULT();
  57391. }
  57392. static int test_CONF_CTX_CMDLINE(void)
  57393. {
  57394. EXPECT_DECLS;
  57395. #if defined(OPENSSL_ALL)
  57396. SSL_CTX* ctx = NULL;
  57397. SSL_CONF_CTX* cctx = NULL;
  57398. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  57399. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  57400. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  57401. /* set flags */
  57402. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CMDLINE),
  57403. WOLFSSL_CONF_FLAG_CMDLINE);
  57404. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  57405. WOLFSSL_CONF_FLAG_CMDLINE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  57406. /* cmd invalid command */
  57407. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  57408. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  57409. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  57410. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  57411. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  57412. /* cmd Certificate and Private Key*/
  57413. {
  57414. #if !defined(NO_CERTS) && !defined(NO_RSA)
  57415. const char* ourCert = svrCertFile;
  57416. const char* ourKey = svrKeyFile;
  57417. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", NULL), -3);
  57418. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cert", ourCert), WOLFSSL_SUCCESS);
  57419. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", NULL), -3);
  57420. ExpectIntEQ(SSL_CONF_cmd(cctx, "-key", ourKey), WOLFSSL_SUCCESS);
  57421. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57422. #endif
  57423. }
  57424. /* cmd curves */
  57425. {
  57426. #if defined(HAVE_ECC)
  57427. const char* curve = "secp256r1";
  57428. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", NULL), -3);
  57429. ExpectIntEQ(SSL_CONF_cmd(cctx, "-curves", curve), WOLFSSL_SUCCESS);
  57430. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57431. #endif
  57432. }
  57433. /* cmd CipherString */
  57434. {
  57435. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  57436. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", NULL), -3);
  57437. ExpectIntEQ(SSL_CONF_cmd(cctx, "-cipher", cipher), WOLFSSL_SUCCESS);
  57438. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57439. }
  57440. /* cmd DH parameter */
  57441. {
  57442. #if !defined(NO_DH) && !defined(NO_BIO)
  57443. const char* ourdhcert = "./certs/dh2048.pem";
  57444. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", NULL), -3);
  57445. ExpectIntEQ(SSL_CONF_cmd(cctx, "-dhparam", ourdhcert), WOLFSSL_SUCCESS);
  57446. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57447. #endif
  57448. }
  57449. SSL_CTX_free(ctx);
  57450. SSL_CONF_CTX_free(cctx);
  57451. #endif /* OPENSSL_EXTRA */
  57452. return EXPECT_RESULT();
  57453. }
  57454. static int test_CONF_CTX_FILE(void)
  57455. {
  57456. EXPECT_DECLS;
  57457. #if defined(OPENSSL_ALL)
  57458. SSL_CTX* ctx = NULL;
  57459. SSL_CONF_CTX* cctx = NULL;
  57460. ExpectNotNull(cctx = SSL_CONF_CTX_new());
  57461. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  57462. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  57463. /* set flags */
  57464. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_FILE),
  57465. WOLFSSL_CONF_FLAG_FILE);
  57466. ExpectIntEQ(SSL_CONF_CTX_set_flags(cctx, WOLFSSL_CONF_FLAG_CERTIFICATE),
  57467. WOLFSSL_CONF_FLAG_FILE | WOLFSSL_CONF_FLAG_CERTIFICATE);
  57468. /* sanity check */
  57469. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", "foobar"), -2);
  57470. ExpectIntEQ(SSL_CONF_cmd(cctx, "foo", NULL), -2);
  57471. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, NULL), WOLFSSL_FAILURE);
  57472. ExpectIntEQ(SSL_CONF_cmd(cctx, NULL, "foobar"), WOLFSSL_FAILURE);
  57473. ExpectIntEQ(SSL_CONF_cmd(NULL, "-curves", "foobar"), WOLFSSL_FAILURE);
  57474. /* cmd Certificate and Private Key*/
  57475. {
  57476. #if !defined(NO_CERTS) && !defined(NO_RSA)
  57477. const char* ourCert = svrCertFile;
  57478. const char* ourKey = svrKeyFile;
  57479. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", NULL), -3);
  57480. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", NULL), -3);
  57481. ExpectIntEQ(SSL_CONF_cmd(cctx, "Certificate", ourCert),
  57482. WOLFSSL_SUCCESS);
  57483. ExpectIntEQ(SSL_CONF_cmd(cctx, "PrivateKey", ourKey), WOLFSSL_SUCCESS);
  57484. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57485. #endif
  57486. }
  57487. /* cmd curves */
  57488. {
  57489. #if defined(HAVE_ECC)
  57490. const char* curve = "secp256r1";
  57491. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", NULL), -3);
  57492. ExpectIntEQ(SSL_CONF_cmd(cctx, "Curves", curve), WOLFSSL_SUCCESS);
  57493. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57494. #endif
  57495. }
  57496. /* cmd CipherString */
  57497. {
  57498. char* cipher = wolfSSL_get_cipher_list(0/*top priority*/);
  57499. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", NULL), -3);
  57500. ExpectIntEQ(SSL_CONF_cmd(cctx, "CipherString", cipher),
  57501. WOLFSSL_SUCCESS);
  57502. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57503. }
  57504. /* cmd DH parameter */
  57505. {
  57506. #if !defined(NO_DH) && !defined(NO_BIO) && defined(HAVE_FFDHE_3072)
  57507. const char* ourdhcert = "./certs/dh3072.pem";
  57508. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", NULL), -3);
  57509. ExpectIntEQ(SSL_CONF_cmd(cctx, "DHParameters", ourdhcert),
  57510. WOLFSSL_SUCCESS);
  57511. ExpectIntEQ(SSL_CONF_CTX_finish(cctx), WOLFSSL_SUCCESS);
  57512. #endif
  57513. }
  57514. SSL_CTX_free(ctx);
  57515. SSL_CONF_CTX_free(cctx);
  57516. #endif /* OPENSSL_EXTRA */
  57517. return EXPECT_RESULT();
  57518. }
  57519. static int test_wolfSSL_CRYPTO_get_ex_new_index(void)
  57520. {
  57521. EXPECT_DECLS;
  57522. #ifdef HAVE_EX_DATA
  57523. int idx1, idx2;
  57524. /* test for unsupported class index */
  57525. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509_STORE,
  57526. 0,NULL, NULL, NULL, NULL ), -1);
  57527. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(
  57528. WOLF_CRYPTO_EX_INDEX_X509_STORE_CTX,
  57529. 0,NULL, NULL, NULL, NULL ), -1);
  57530. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DH,
  57531. 0,NULL, NULL, NULL, NULL ), -1);
  57532. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DSA,
  57533. 0,NULL, NULL, NULL, NULL ), -1);
  57534. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_EC_KEY,
  57535. 0,NULL, NULL, NULL, NULL ), -1);
  57536. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_RSA,
  57537. 0,NULL, NULL, NULL, NULL ), -1);
  57538. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_ENGINE,
  57539. 0,NULL, NULL, NULL, NULL ), -1);
  57540. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI,
  57541. 0,NULL, NULL, NULL, NULL ), -1);
  57542. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_BIO,
  57543. 0,NULL, NULL, NULL, NULL ), -1);
  57544. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_APP,
  57545. 0,NULL, NULL, NULL, NULL ), -1);
  57546. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_UI_METHOD,
  57547. 0,NULL, NULL, NULL, NULL ), -1);
  57548. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_DRBG,
  57549. 0,NULL, NULL, NULL, NULL ), -1);
  57550. ExpectIntEQ(wolfSSL_CRYPTO_get_ex_new_index(20,
  57551. 0,NULL, NULL, NULL, NULL ), -1);
  57552. /* test for supported class index */
  57553. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  57554. 0,NULL, NULL, NULL, NULL );
  57555. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL,
  57556. 0,NULL, NULL, NULL, NULL );
  57557. ExpectIntNE(idx1, -1);
  57558. ExpectIntNE(idx2, -1);
  57559. ExpectIntNE(idx1, idx2);
  57560. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  57561. 0,NULL, NULL, NULL, NULL );
  57562. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_CTX,
  57563. 0,NULL, NULL, NULL, NULL );
  57564. ExpectIntNE(idx1, -1);
  57565. ExpectIntNE(idx2, -1);
  57566. ExpectIntNE(idx1, idx2);
  57567. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  57568. 0,NULL, NULL, NULL, NULL );
  57569. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_X509,
  57570. 0,NULL, NULL, NULL, NULL );
  57571. ExpectIntNE(idx1, -1);
  57572. ExpectIntNE(idx2, -1);
  57573. ExpectIntNE(idx1, idx2);
  57574. idx1 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  57575. 0,NULL, NULL, NULL, NULL );
  57576. idx2 = wolfSSL_CRYPTO_get_ex_new_index(WOLF_CRYPTO_EX_INDEX_SSL_SESSION,
  57577. 0,NULL, NULL, NULL, NULL );
  57578. ExpectIntNE(idx1, -1);
  57579. ExpectIntNE(idx2, -1);
  57580. ExpectIntNE(idx1, idx2);
  57581. #endif /* HAVE_EX_DATA */
  57582. return EXPECT_RESULT();
  57583. }
  57584. #if defined(HAVE_EX_DATA) && defined(HAVE_EXT_CACHE) && \
  57585. (defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  57586. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  57587. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  57588. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB))))
  57589. #define SESSION_NEW_IDX_LONG 0xDEADBEEF
  57590. #define SESSION_NEW_IDX_VAL ((void*)0xAEADAEAD)
  57591. #define SESSION_DUP_IDX_VAL ((void*)0xDEDEDEDE)
  57592. #define SESSION_NEW_IDX_PTR "Testing"
  57593. static void test_wolfSSL_SESSION_get_ex_new_index_new_cb(void* p, void* ptr,
  57594. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  57595. {
  57596. AssertNotNull(p);
  57597. AssertNull(ptr);
  57598. AssertIntEQ(CRYPTO_set_ex_data(a, idx, SESSION_NEW_IDX_VAL), SSL_SUCCESS);
  57599. AssertIntEQ(argValue, SESSION_NEW_IDX_LONG);
  57600. AssertStrEQ(arg, SESSION_NEW_IDX_PTR);
  57601. }
  57602. static int test_wolfSSL_SESSION_get_ex_new_index_dup_cb(CRYPTO_EX_DATA* out,
  57603. const CRYPTO_EX_DATA* in, void* inPtr, int idx, long argV,
  57604. void* arg)
  57605. {
  57606. EXPECT_DECLS;
  57607. ExpectNotNull(out);
  57608. ExpectNotNull(in);
  57609. ExpectPtrEq(*(void**)inPtr, SESSION_NEW_IDX_VAL);
  57610. ExpectPtrEq(CRYPTO_get_ex_data(in, idx), SESSION_NEW_IDX_VAL);
  57611. ExpectPtrEq(CRYPTO_get_ex_data(out, idx), SESSION_NEW_IDX_VAL);
  57612. ExpectIntEQ(argV, SESSION_NEW_IDX_LONG);
  57613. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  57614. *(void**)inPtr = SESSION_DUP_IDX_VAL;
  57615. if (EXPECT_SUCCESS()) {
  57616. return SSL_SUCCESS;
  57617. }
  57618. else {
  57619. return SSL_FAILURE;
  57620. }
  57621. }
  57622. static int test_wolfSSL_SESSION_get_ex_new_index_free_cb_called = 0;
  57623. static void test_wolfSSL_SESSION_get_ex_new_index_free_cb(void* p, void* ptr,
  57624. CRYPTO_EX_DATA* a, int idx, long argValue, void* arg)
  57625. {
  57626. EXPECT_DECLS;
  57627. ExpectNotNull(p);
  57628. ExpectNull(ptr);
  57629. ExpectPtrNE(CRYPTO_get_ex_data(a, idx), 0);
  57630. ExpectIntEQ(argValue, SESSION_NEW_IDX_LONG);
  57631. ExpectStrEQ(arg, SESSION_NEW_IDX_PTR);
  57632. if (EXPECT_SUCCESS()) {
  57633. test_wolfSSL_SESSION_get_ex_new_index_free_cb_called++;
  57634. }
  57635. }
  57636. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  57637. {
  57638. EXPECT_DECLS;
  57639. int idx = SSL_SESSION_get_ex_new_index(SESSION_NEW_IDX_LONG,
  57640. (void*)SESSION_NEW_IDX_PTR,
  57641. test_wolfSSL_SESSION_get_ex_new_index_new_cb,
  57642. test_wolfSSL_SESSION_get_ex_new_index_dup_cb,
  57643. test_wolfSSL_SESSION_get_ex_new_index_free_cb);
  57644. SSL_SESSION* s = SSL_SESSION_new();
  57645. SSL_SESSION* d = NULL;
  57646. ExpectNotNull(s);
  57647. ExpectPtrEq(SSL_SESSION_get_ex_data(s, idx), SESSION_NEW_IDX_VAL);
  57648. ExpectNotNull(d = SSL_SESSION_dup(s));
  57649. ExpectPtrEq(SSL_SESSION_get_ex_data(d, idx), SESSION_DUP_IDX_VAL);
  57650. SSL_SESSION_free(s);
  57651. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 1);
  57652. SSL_SESSION_free(d);
  57653. ExpectIntEQ(test_wolfSSL_SESSION_get_ex_new_index_free_cb_called, 2);
  57654. crypto_ex_cb_free(crypto_ex_cb_ctx_session);
  57655. crypto_ex_cb_ctx_session = NULL;
  57656. return EXPECT_RESULT();
  57657. }
  57658. #else
  57659. static int test_wolfSSL_SESSION_get_ex_new_index(void)
  57660. {
  57661. return TEST_SKIPPED;
  57662. }
  57663. #endif
  57664. static int test_wolfSSL_set_psk_use_session_callback(void)
  57665. {
  57666. EXPECT_DECLS;
  57667. #if defined(OPENSSL_EXTRA) && !defined(NO_PSK)
  57668. SSL_CTX* ctx = NULL;
  57669. SSL* ssl = NULL;
  57670. const char* testCertFile;
  57671. const char* testKeyFile;
  57672. #ifdef WOLFSSL_TLS13
  57673. #ifdef NO_WOLFSSL_SERVER
  57674. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method()));
  57675. #else
  57676. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfTLSv1_3_server_method()));
  57677. #endif
  57678. #else
  57679. #ifdef NO_WOLFSSL_SERVER
  57680. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  57681. #else
  57682. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  57683. #endif
  57684. #endif
  57685. #ifndef NO_RSA
  57686. testCertFile = svrCertFile;
  57687. testKeyFile = svrKeyFile;
  57688. #elif defined(HAVE_ECC)
  57689. testCertFile = eccCertFile;
  57690. testKeyFile = eccKeyFile;
  57691. #else
  57692. testCertFile = NULL;
  57693. testKeyFile = NULL;
  57694. #endif
  57695. if ((testCertFile != NULL) && (testKeyFile != NULL)) {
  57696. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  57697. SSL_FILETYPE_PEM));
  57698. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  57699. SSL_FILETYPE_PEM));
  57700. }
  57701. ExpectNotNull(ssl = SSL_new(ctx));
  57702. SSL_set_psk_use_session_callback(ssl, my_psk_use_session_cb);
  57703. SSL_CTX_free(ctx);
  57704. SSL_free(ssl);
  57705. #endif
  57706. return EXPECT_RESULT();
  57707. }
  57708. static int test_wolfSSL_ERR_strings(void)
  57709. {
  57710. EXPECT_DECLS;
  57711. #if !defined(NO_ERROR_STRINGS)
  57712. const char* err1 = "unsupported cipher suite";
  57713. const char* err2 = "wolfSSL PEM routines";
  57714. const char* err = NULL;
  57715. (void)err;
  57716. (void)err1;
  57717. (void)err2;
  57718. #if defined(OPENSSL_EXTRA)
  57719. ExpectNotNull(err = ERR_reason_error_string(UNSUPPORTED_SUITE));
  57720. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  57721. ExpectNotNull(err = ERR_func_error_string(UNSUPPORTED_SUITE));
  57722. ExpectIntEQ((*err == '\0'), 1);
  57723. ExpectNotNull(err = ERR_lib_error_string(PEM_R_PROBLEMS_GETTING_PASSWORD));
  57724. ExpectIntEQ(XSTRNCMP(err, err2, XSTRLEN(err2)), 0);
  57725. #else
  57726. ExpectNotNull(err = wolfSSL_ERR_reason_error_string(UNSUPPORTED_SUITE));
  57727. ExpectIntEQ(XSTRNCMP(err, err1, XSTRLEN(err1)), 0);
  57728. ExpectNotNull(err = wolfSSL_ERR_func_error_string(UNSUPPORTED_SUITE));
  57729. ExpectIntEQ((*err == '\0'), 1);
  57730. /* The value -MIN_CODE_E+2 is PEM_R_PROBLEMS_GETTING_PASSWORD. */
  57731. ExpectNotNull(err = wolfSSL_ERR_lib_error_string(-MIN_CODE_E+2));
  57732. ExpectIntEQ((*err == '\0'), 1);
  57733. #endif
  57734. #endif
  57735. return EXPECT_RESULT();
  57736. }
  57737. static int test_wolfSSL_EVP_shake128(void)
  57738. {
  57739. EXPECT_DECLS;
  57740. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  57741. defined(WOLFSSL_SHAKE128)
  57742. const EVP_MD* md = NULL;
  57743. ExpectNotNull(md = EVP_shake128());
  57744. ExpectIntEQ(XSTRNCMP(md, "SHAKE128", XSTRLEN("SHAKE128")), 0);
  57745. #endif
  57746. return EXPECT_RESULT();
  57747. }
  57748. static int test_wolfSSL_EVP_shake256(void)
  57749. {
  57750. EXPECT_DECLS;
  57751. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA3) && \
  57752. defined(WOLFSSL_SHAKE256)
  57753. const EVP_MD* md = NULL;
  57754. ExpectNotNull(md = EVP_shake256());
  57755. ExpectIntEQ(XSTRNCMP(md, "SHAKE256", XSTRLEN("SHAKE256")), 0);
  57756. #endif
  57757. return EXPECT_RESULT();
  57758. }
  57759. /*
  57760. * Testing EVP digest API with SM3
  57761. */
  57762. static int test_wolfSSL_EVP_sm3(void)
  57763. {
  57764. int res = TEST_SKIPPED;
  57765. #if defined(OPENSSL_EXTRA) && defined(WOLFSSL_SM3)
  57766. EXPECT_DECLS;
  57767. const EVP_MD* md = NULL;
  57768. EVP_MD_CTX* mdCtx = NULL;
  57769. byte data[WC_SM3_BLOCK_SIZE * 4];
  57770. byte hash[WC_SM3_DIGEST_SIZE];
  57771. byte calcHash[WC_SM3_DIGEST_SIZE];
  57772. byte expHash[WC_SM3_DIGEST_SIZE] = {
  57773. 0x38, 0x48, 0x15, 0xa7, 0x0e, 0xae, 0x0b, 0x27,
  57774. 0x5c, 0xde, 0x9d, 0xa5, 0xd1, 0xa4, 0x30, 0xa1,
  57775. 0xca, 0xd4, 0x54, 0x58, 0x44, 0xa2, 0x96, 0x1b,
  57776. 0xd7, 0x14, 0x80, 0x3f, 0x80, 0x1a, 0x07, 0xb6
  57777. };
  57778. word32 chunk;
  57779. word32 i;
  57780. unsigned int sz;
  57781. int ret;
  57782. XMEMSET(data, 0, sizeof(data));
  57783. md = EVP_sm3();
  57784. ExpectTrue(md != NULL);
  57785. ExpectIntEQ(XSTRNCMP(md, "SM3", XSTRLEN("SM3")), 0);
  57786. mdCtx = EVP_MD_CTX_new();
  57787. ExpectTrue(mdCtx != NULL);
  57788. /* Invalid Parameters */
  57789. ExpectIntEQ(EVP_DigestInit(NULL, md), BAD_FUNC_ARG);
  57790. /* Valid Parameters */
  57791. ExpectIntEQ(EVP_DigestInit(mdCtx, md), WOLFSSL_SUCCESS);
  57792. ExpectIntEQ(EVP_DigestUpdate(NULL, NULL, 1), WOLFSSL_FAILURE);
  57793. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 1), WOLFSSL_FAILURE);
  57794. ExpectIntEQ(EVP_DigestUpdate(NULL, data, 1), WOLFSSL_FAILURE);
  57795. /* Valid Parameters */
  57796. ExpectIntEQ(EVP_DigestUpdate(mdCtx, NULL, 0), WOLFSSL_SUCCESS);
  57797. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  57798. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, 1), WOLFSSL_SUCCESS);
  57799. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE),
  57800. WOLFSSL_SUCCESS);
  57801. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE - 2),
  57802. WOLFSSL_SUCCESS);
  57803. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_BLOCK_SIZE * 2),
  57804. WOLFSSL_SUCCESS);
  57805. /* Ensure too many bytes for lengths. */
  57806. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, WC_SM3_PAD_SIZE),
  57807. WOLFSSL_SUCCESS);
  57808. /* Invalid Parameters */
  57809. ExpectIntEQ(EVP_DigestFinal(NULL, NULL, NULL), WOLFSSL_FAILURE);
  57810. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  57811. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  57812. ExpectIntEQ(EVP_DigestFinal(NULL, hash, NULL), WOLFSSL_FAILURE);
  57813. ExpectIntEQ(EVP_DigestFinal(mdCtx, NULL, NULL), WOLFSSL_FAILURE);
  57814. /* Valid Parameters */
  57815. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  57816. ExpectBufEQ(hash, expHash, WC_SM3_DIGEST_SIZE);
  57817. /* Chunk tests. */
  57818. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data, sizeof(data)), WOLFSSL_SUCCESS);
  57819. ExpectIntEQ(EVP_DigestFinal(mdCtx, calcHash, &sz), WOLFSSL_SUCCESS);
  57820. ExpectIntEQ(sz, WC_SM3_DIGEST_SIZE);
  57821. for (chunk = 1; chunk <= WC_SM3_BLOCK_SIZE + 1; chunk++) {
  57822. for (i = 0; i + chunk <= (word32)sizeof(data); i += chunk) {
  57823. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i, chunk),
  57824. WOLFSSL_SUCCESS);
  57825. }
  57826. if (i < (word32)sizeof(data)) {
  57827. ExpectIntEQ(EVP_DigestUpdate(mdCtx, data + i,
  57828. (word32)sizeof(data) - i), WOLFSSL_SUCCESS);
  57829. }
  57830. ExpectIntEQ(EVP_DigestFinal(mdCtx, hash, NULL), WOLFSSL_SUCCESS);
  57831. ExpectBufEQ(hash, calcHash, WC_SM3_DIGEST_SIZE);
  57832. }
  57833. /* Not testing when the low 32-bit length overflows. */
  57834. ret = EVP_MD_CTX_cleanup(mdCtx);
  57835. ExpectIntEQ(ret, WOLFSSL_SUCCESS);
  57836. wolfSSL_EVP_MD_CTX_free(mdCtx);
  57837. res = EXPECT_RESULT();
  57838. #endif
  57839. return res;
  57840. } /* END test_EVP_sm3 */
  57841. static int test_EVP_blake2(void)
  57842. {
  57843. EXPECT_DECLS;
  57844. #if defined(OPENSSL_EXTRA) && (defined(HAVE_BLAKE2) || defined(HAVE_BLAKE2S))
  57845. const EVP_MD* md = NULL;
  57846. (void)md;
  57847. #if defined(HAVE_BLAKE2)
  57848. ExpectNotNull(md = EVP_blake2b512());
  57849. ExpectIntEQ(XSTRNCMP(md, "BLAKE2B512", XSTRLEN("BLAKE2B512")), 0);
  57850. #endif
  57851. #if defined(HAVE_BLAKE2S)
  57852. ExpectNotNull(md = EVP_blake2s256());
  57853. ExpectIntEQ(XSTRNCMP(md, "BLAKE2S256", XSTRLEN("BLAKE2S256")), 0);
  57854. #endif
  57855. #endif
  57856. return EXPECT_RESULT();
  57857. }
  57858. #if defined(OPENSSL_EXTRA)
  57859. static void list_md_fn(const EVP_MD* m, const char* from,
  57860. const char* to, void* arg)
  57861. {
  57862. const char* mn;
  57863. BIO *bio;
  57864. (void) from;
  57865. (void) to;
  57866. (void) arg;
  57867. (void) mn;
  57868. (void) bio;
  57869. if (!m) {
  57870. /* alias */
  57871. AssertNull(m);
  57872. AssertNotNull(to);
  57873. }
  57874. else {
  57875. AssertNotNull(m);
  57876. AssertNull(to);
  57877. }
  57878. AssertNotNull(from);
  57879. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  57880. mn = EVP_get_digestbyname(from);
  57881. /* print to stderr */
  57882. AssertNotNull(arg);
  57883. bio = BIO_new(BIO_s_file());
  57884. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  57885. BIO_printf(bio, "Use %s message digest algorithm\n", mn);
  57886. BIO_free(bio);
  57887. #endif
  57888. }
  57889. #endif
  57890. static int test_EVP_MD_do_all(void)
  57891. {
  57892. int res = TEST_SKIPPED;
  57893. #if defined(OPENSSL_EXTRA)
  57894. EVP_MD_do_all(NULL, stderr);
  57895. EVP_MD_do_all(list_md_fn, stderr);
  57896. res = TEST_SUCCESS;
  57897. #endif
  57898. return res;
  57899. }
  57900. #if defined(OPENSSL_EXTRA)
  57901. static void obj_name_t(const OBJ_NAME* nm, void* arg)
  57902. {
  57903. (void)arg;
  57904. (void)nm;
  57905. AssertIntGT(nm->type, OBJ_NAME_TYPE_UNDEF);
  57906. #if !defined(NO_FILESYSTEM) && defined(DEBUG_WOLFSSL_VERBOSE)
  57907. /* print to stderr */
  57908. AssertNotNull(arg);
  57909. BIO *bio = BIO_new(BIO_s_file());
  57910. BIO_set_fp(bio, arg, BIO_NOCLOSE);
  57911. BIO_printf(bio, "%s\n", nm);
  57912. BIO_free(bio);
  57913. #endif
  57914. }
  57915. #endif
  57916. static int test_OBJ_NAME_do_all(void)
  57917. {
  57918. int res = TEST_SKIPPED;
  57919. #if defined(OPENSSL_EXTRA)
  57920. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, NULL, NULL);
  57921. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, NULL, stderr);
  57922. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, obj_name_t, stderr);
  57923. OBJ_NAME_do_all(OBJ_NAME_TYPE_PKEY_METH, obj_name_t, stderr);
  57924. OBJ_NAME_do_all(OBJ_NAME_TYPE_COMP_METH, obj_name_t, stderr);
  57925. OBJ_NAME_do_all(OBJ_NAME_TYPE_NUM, obj_name_t, stderr);
  57926. OBJ_NAME_do_all(OBJ_NAME_TYPE_UNDEF, obj_name_t, stderr);
  57927. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, obj_name_t, stderr);
  57928. OBJ_NAME_do_all(-1, obj_name_t, stderr);
  57929. res = TEST_SUCCESS;
  57930. #endif
  57931. return res;
  57932. }
  57933. static int test_SSL_CIPHER_get_xxx(void)
  57934. {
  57935. EXPECT_DECLS;
  57936. #if defined(OPENSSL_ALL) && !defined(NO_CERTS) && \
  57937. !defined(NO_FILESYSTEM)
  57938. const SSL_CIPHER* cipher = NULL;
  57939. STACK_OF(SSL_CIPHER) *supportedCiphers = NULL;
  57940. int i, numCiphers = 0;
  57941. SSL_CTX* ctx = NULL;
  57942. SSL* ssl = NULL;
  57943. const char* testCertFile;
  57944. const char* testKeyFile;
  57945. char buf[256] = {0};
  57946. const char* cipher_id = NULL;
  57947. int expect_nid1 = NID_undef;
  57948. int expect_nid2 = NID_undef;
  57949. int expect_nid3 = NID_undef;
  57950. int expect_nid4 = NID_undef;
  57951. int expect_nid5 = 0;
  57952. const char* cipher_id2 = NULL;
  57953. int expect_nid21 = NID_undef;
  57954. int expect_nid22 = NID_undef;
  57955. int expect_nid23 = NID_undef;
  57956. int expect_nid24 = NID_undef;
  57957. int expect_nid25 = 0;
  57958. (void)cipher;
  57959. (void)supportedCiphers;
  57960. (void)i;
  57961. (void)numCiphers;
  57962. (void)ctx;
  57963. (void)ssl;
  57964. (void)testCertFile;
  57965. (void)testKeyFile;
  57966. #if defined(WOLFSSL_TLS13)
  57967. cipher_id = "TLS13-AES128-GCM-SHA256";
  57968. expect_nid1 = NID_auth_rsa;
  57969. expect_nid2 = NID_aes_128_gcm;
  57970. expect_nid3 = NID_sha256;
  57971. expect_nid4 = NID_kx_any;
  57972. expect_nid5 = 1;
  57973. #if !defined(WOLFSSL_NO_TLS12)
  57974. cipher_id2 = "ECDHE-RSA-AES256-GCM-SHA384";
  57975. expect_nid21 = NID_auth_rsa;
  57976. expect_nid22 = NID_aes_256_gcm;
  57977. expect_nid23 = NID_sha384;
  57978. expect_nid24 = NID_kx_ecdhe;
  57979. expect_nid25 = 1;
  57980. #endif
  57981. #endif
  57982. #ifdef NO_WOLFSSL_SERVER
  57983. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_client_method()));
  57984. #else
  57985. ExpectNotNull(ctx = wolfSSL_CTX_new(wolfSSLv23_server_method()));
  57986. #endif
  57987. if (cipher_id) {
  57988. #ifndef NO_RSA
  57989. testCertFile = svrCertFile;
  57990. testKeyFile = svrKeyFile;
  57991. #elif defined(HAVE_ECC)
  57992. testCertFile = eccCertFile;
  57993. testKeyFile = eccKeyFile;
  57994. #else
  57995. testCertFile = NULL;
  57996. testKeyFile = NULL;
  57997. #endif
  57998. if (testCertFile != NULL && testKeyFile != NULL) {
  57999. ExpectTrue(SSL_CTX_use_certificate_file(ctx, testCertFile,
  58000. SSL_FILETYPE_PEM));
  58001. ExpectTrue(SSL_CTX_use_PrivateKey_file(ctx, testKeyFile,
  58002. SSL_FILETYPE_PEM));
  58003. }
  58004. ExpectNotNull(ssl = SSL_new(ctx));
  58005. ExpectIntEQ(SSL_in_init(ssl), 1);
  58006. supportedCiphers = SSL_get_ciphers(ssl);
  58007. numCiphers = sk_num(supportedCiphers);
  58008. for (i = 0; i < numCiphers; ++i) {
  58009. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  58010. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  58011. }
  58012. if (XMEMCMP(cipher_id, buf, XSTRLEN(cipher_id)) == 0) {
  58013. break;
  58014. }
  58015. }
  58016. /* test case for */
  58017. if (i != numCiphers) {
  58018. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid1);
  58019. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid2);
  58020. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid3);
  58021. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid4);
  58022. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid5);
  58023. }
  58024. if (cipher_id2) {
  58025. for (i = 0; i < numCiphers; ++i) {
  58026. if ((cipher = (const WOLFSSL_CIPHER*)sk_value(supportedCiphers, i))) {
  58027. SSL_CIPHER_description(cipher, buf, sizeof(buf));
  58028. }
  58029. if (XMEMCMP(cipher_id2, buf, XSTRLEN(cipher_id2)) == 0) {
  58030. break;
  58031. }
  58032. }
  58033. /* test case for */
  58034. if (i != numCiphers) {
  58035. ExpectIntEQ(wolfSSL_CIPHER_get_auth_nid(cipher), expect_nid21);
  58036. ExpectIntEQ(wolfSSL_CIPHER_get_cipher_nid(cipher), expect_nid22);
  58037. ExpectIntEQ(wolfSSL_CIPHER_get_digest_nid(cipher), expect_nid23);
  58038. ExpectIntEQ(wolfSSL_CIPHER_get_kx_nid(cipher), expect_nid24);
  58039. ExpectIntEQ(wolfSSL_CIPHER_is_aead(cipher), expect_nid25);
  58040. }
  58041. }
  58042. }
  58043. SSL_CTX_free(ctx);
  58044. SSL_free(ssl);
  58045. #endif
  58046. return EXPECT_RESULT();
  58047. }
  58048. #if defined(WOLF_CRYPTO_CB) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  58049. static int load_pem_key_file_as_der(const char* privKeyFile, DerBuffer** pDer,
  58050. int* keyFormat)
  58051. {
  58052. int ret;
  58053. byte* key_buf = NULL;
  58054. size_t key_sz = 0;
  58055. EncryptedInfo encInfo;
  58056. XMEMSET(&encInfo, 0, sizeof(encInfo));
  58057. ret = load_file(privKeyFile, &key_buf, &key_sz);
  58058. if (ret == 0) {
  58059. ret = wc_PemToDer(key_buf, key_sz, PRIVATEKEY_TYPE, pDer,
  58060. NULL, &encInfo, keyFormat);
  58061. }
  58062. if (key_buf != NULL) {
  58063. free(key_buf); key_buf = NULL;
  58064. }
  58065. (void)encInfo; /* not used in this test */
  58066. #ifdef DEBUG_WOLFSSL
  58067. fprintf(stderr, "%s (%d): Loading PEM %s (len %d) to DER (len %d)\n",
  58068. (ret == 0) ? "Success" : "Failure", ret, privKeyFile, (int)key_sz,
  58069. (*pDer)->length);
  58070. #endif
  58071. return ret;
  58072. }
  58073. static int test_CryptoCb_Func(int thisDevId, wc_CryptoInfo* info, void* ctx)
  58074. {
  58075. int ret = CRYPTOCB_UNAVAILABLE;
  58076. const char* privKeyFile = (const char*)ctx;
  58077. DerBuffer* pDer = NULL;
  58078. int keyFormat = 0;
  58079. if (info->algo_type == WC_ALGO_TYPE_PK) {
  58080. #ifdef DEBUG_WOLFSSL
  58081. fprintf(stderr, "test_CryptoCb_Func: Pk Type %d\n", info->pk.type);
  58082. #endif
  58083. #ifndef NO_RSA
  58084. if (info->pk.type == WC_PK_TYPE_RSA) {
  58085. switch (info->pk.rsa.type) {
  58086. case RSA_PUBLIC_ENCRYPT:
  58087. case RSA_PUBLIC_DECRYPT:
  58088. /* perform software based RSA public op */
  58089. ret = CRYPTOCB_UNAVAILABLE; /* fallback to software */
  58090. break;
  58091. case RSA_PRIVATE_ENCRYPT:
  58092. case RSA_PRIVATE_DECRYPT:
  58093. {
  58094. RsaKey key;
  58095. /* perform software based RSA private op */
  58096. #ifdef DEBUG_WOLFSSL
  58097. fprintf(stderr, "test_CryptoCb_Func: RSA Priv\n");
  58098. #endif
  58099. ret = load_pem_key_file_as_der(privKeyFile, &pDer,
  58100. &keyFormat);
  58101. if (ret != 0) {
  58102. return ret;
  58103. }
  58104. ret = wc_InitRsaKey(&key, HEAP_HINT);
  58105. if (ret == 0) {
  58106. word32 keyIdx = 0;
  58107. /* load RSA private key and perform private transform */
  58108. ret = wc_RsaPrivateKeyDecode(pDer->buffer, &keyIdx,
  58109. &key, pDer->length);
  58110. if (ret == 0) {
  58111. ret = wc_RsaFunction(
  58112. info->pk.rsa.in, info->pk.rsa.inLen,
  58113. info->pk.rsa.out, info->pk.rsa.outLen,
  58114. info->pk.rsa.type, &key, info->pk.rsa.rng);
  58115. }
  58116. else {
  58117. /* if decode fails, then fall-back to software based crypto */
  58118. fprintf(stderr, "test_CryptoCb_Func: RSA private "
  58119. "key decode failed %d, falling back to "
  58120. "software\n", ret);
  58121. ret = CRYPTOCB_UNAVAILABLE;
  58122. }
  58123. wc_FreeRsaKey(&key);
  58124. }
  58125. wc_FreeDer(&pDer); pDer = NULL;
  58126. break;
  58127. }
  58128. }
  58129. #ifdef DEBUG_WOLFSSL
  58130. fprintf(stderr, "test_CryptoCb_Func: RSA Type %d, Ret %d, Out %d\n",
  58131. info->pk.rsa.type, ret, *info->pk.rsa.outLen);
  58132. #endif
  58133. }
  58134. #endif /* !NO_RSA */
  58135. #ifdef HAVE_ECC
  58136. if (info->pk.type == WC_PK_TYPE_EC_KEYGEN) {
  58137. /* mark this key as ephemeral */
  58138. if (info->pk.eckg.key != NULL) {
  58139. XSTRNCPY(info->pk.eckg.key->label, "ephemeral",
  58140. sizeof(info->pk.eckg.key->label));
  58141. info->pk.eckg.key->labelLen = (int)XSTRLEN(info->pk.eckg.key->label);
  58142. }
  58143. }
  58144. else if (info->pk.type == WC_PK_TYPE_ECDSA_SIGN) {
  58145. ecc_key key;
  58146. /* perform software based ECC sign */
  58147. #ifdef DEBUG_WOLFSSL
  58148. fprintf(stderr, "test_CryptoCb_Func: ECC Sign\n");
  58149. #endif
  58150. if (info->pk.eccsign.key != NULL &&
  58151. XSTRCMP(info->pk.eccsign.key->label, "ephemeral") == 0) {
  58152. /* this is an empheral key */
  58153. #ifdef DEBUG_WOLFSSL
  58154. fprintf(stderr, "test_CryptoCb_Func: skipping signing op on "
  58155. "ephemeral key\n");
  58156. #endif
  58157. return CRYPTOCB_UNAVAILABLE;
  58158. }
  58159. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  58160. if (ret != 0) {
  58161. return ret;
  58162. }
  58163. ret = wc_ecc_init(&key);
  58164. if (ret == 0) {
  58165. word32 keyIdx = 0;
  58166. /* load ECC private key and perform private transform */
  58167. ret = wc_EccPrivateKeyDecode(pDer->buffer, &keyIdx,
  58168. &key, pDer->length);
  58169. if (ret == 0) {
  58170. ret = wc_ecc_sign_hash(
  58171. info->pk.eccsign.in, info->pk.eccsign.inlen,
  58172. info->pk.eccsign.out, info->pk.eccsign.outlen,
  58173. info->pk.eccsign.rng, &key);
  58174. }
  58175. else {
  58176. /* if decode fails, then fall-back to software based crypto */
  58177. fprintf(stderr, "test_CryptoCb_Func: ECC private key "
  58178. "decode failed %d, falling back to software\n", ret);
  58179. ret = CRYPTOCB_UNAVAILABLE;
  58180. }
  58181. wc_ecc_free(&key);
  58182. }
  58183. wc_FreeDer(&pDer); pDer = NULL;
  58184. #ifdef DEBUG_WOLFSSL
  58185. fprintf(stderr, "test_CryptoCb_Func: ECC Ret %d, Out %d\n",
  58186. ret, *info->pk.eccsign.outlen);
  58187. #endif
  58188. }
  58189. #endif /* HAVE_ECC */
  58190. #ifdef HAVE_ED25519
  58191. if (info->pk.type == WC_PK_TYPE_ED25519_SIGN) {
  58192. ed25519_key key;
  58193. /* perform software based ED25519 sign */
  58194. #ifdef DEBUG_WOLFSSL
  58195. fprintf(stderr, "test_CryptoCb_Func: ED25519 Sign\n");
  58196. #endif
  58197. ret = load_pem_key_file_as_der(privKeyFile, &pDer, &keyFormat);
  58198. if (ret != 0) {
  58199. return ret;
  58200. }
  58201. ret = wc_ed25519_init(&key);
  58202. if (ret == 0) {
  58203. word32 keyIdx = 0;
  58204. /* load ED25519 private key and perform private transform */
  58205. ret = wc_Ed25519PrivateKeyDecode(pDer->buffer, &keyIdx,
  58206. &key, pDer->length);
  58207. if (ret == 0) {
  58208. /* calculate public key */
  58209. ret = wc_ed25519_make_public(&key, key.p, ED25519_PUB_KEY_SIZE);
  58210. if (ret == 0) {
  58211. key.pubKeySet = 1;
  58212. ret = wc_ed25519_sign_msg_ex(
  58213. info->pk.ed25519sign.in, info->pk.ed25519sign.inLen,
  58214. info->pk.ed25519sign.out, info->pk.ed25519sign.outLen,
  58215. &key, info->pk.ed25519sign.type,
  58216. info->pk.ed25519sign.context,
  58217. info->pk.ed25519sign.contextLen);
  58218. }
  58219. }
  58220. else {
  58221. /* if decode fails, then fall-back to software based crypto */
  58222. fprintf(stderr, "test_CryptoCb_Func: ED25519 private key "
  58223. "decode failed %d, falling back to software\n", ret);
  58224. ret = CRYPTOCB_UNAVAILABLE;
  58225. }
  58226. wc_ed25519_free(&key);
  58227. }
  58228. wc_FreeDer(&pDer); pDer = NULL;
  58229. #ifdef DEBUG_WOLFSSL
  58230. fprintf(stderr, "test_CryptoCb_Func: ED25519 Ret %d, Out %d\n",
  58231. ret, *info->pk.ed25519sign.outLen);
  58232. #endif
  58233. }
  58234. #endif /* HAVE_ED25519 */
  58235. }
  58236. (void)thisDevId;
  58237. (void)keyFormat;
  58238. return ret;
  58239. }
  58240. /* tlsVer: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  58241. static int test_wc_CryptoCb_TLS(int tlsVer,
  58242. const char* cliCaPemFile, const char* cliCertPemFile,
  58243. const char* cliPrivKeyPemFile, const char* cliPubKeyPemFile,
  58244. const char* svrCaPemFile, const char* svrCertPemFile,
  58245. const char* svrPrivKeyPemFile, const char* svrPubKeyPemFile)
  58246. {
  58247. EXPECT_DECLS;
  58248. callback_functions client_cbf;
  58249. callback_functions server_cbf;
  58250. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  58251. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  58252. if (tlsVer == WOLFSSL_TLSV1_3) {
  58253. #ifdef WOLFSSL_TLS13
  58254. server_cbf.method = wolfTLSv1_3_server_method;
  58255. client_cbf.method = wolfTLSv1_3_client_method;
  58256. #endif
  58257. }
  58258. else if (tlsVer == WOLFSSL_TLSV1_2) {
  58259. #ifndef WOLFSSL_NO_TLS12
  58260. server_cbf.method = wolfTLSv1_2_server_method;
  58261. client_cbf.method = wolfTLSv1_2_client_method;
  58262. #endif
  58263. }
  58264. else if (tlsVer == WOLFSSL_TLSV1_1) {
  58265. #ifndef NO_OLD_TLS
  58266. server_cbf.method = wolfTLSv1_1_server_method;
  58267. client_cbf.method = wolfTLSv1_1_client_method;
  58268. #endif
  58269. }
  58270. else if (tlsVer == WOLFSSL_TLSV1) {
  58271. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  58272. server_cbf.method = wolfTLSv1_server_method;
  58273. client_cbf.method = wolfTLSv1_client_method;
  58274. #endif
  58275. }
  58276. else if (tlsVer == WOLFSSL_SSLV3) {
  58277. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  58278. defined(WOLFSSL_STATIC_RSA)
  58279. server_cbf.method = wolfSSLv3_server_method;
  58280. client_cbf.method = wolfSSLv3_client_method;
  58281. #endif
  58282. }
  58283. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  58284. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  58285. server_cbf.method = wolfDTLSv1_2_server_method;
  58286. client_cbf.method = wolfDTLSv1_2_client_method;
  58287. #endif
  58288. }
  58289. else if (tlsVer == WOLFSSL_DTLSV1) {
  58290. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  58291. server_cbf.method = wolfDTLSv1_server_method;
  58292. client_cbf.method = wolfDTLSv1_client_method;
  58293. #endif
  58294. }
  58295. if (server_cbf.method == NULL) {
  58296. /* not enabled */
  58297. return TEST_SUCCESS;
  58298. }
  58299. /* Setup the keys for the TLS test */
  58300. client_cbf.certPemFile = cliCertPemFile;
  58301. client_cbf.keyPemFile = cliPubKeyPemFile;
  58302. client_cbf.caPemFile = cliCaPemFile;
  58303. server_cbf.certPemFile = svrCertPemFile;
  58304. server_cbf.keyPemFile = svrPubKeyPemFile;
  58305. server_cbf.caPemFile = svrCaPemFile;
  58306. /* Setup a crypto callback with pointer to private key file for testing */
  58307. client_cbf.devId = 1;
  58308. wc_CryptoCb_RegisterDevice(client_cbf.devId, test_CryptoCb_Func,
  58309. (void*)cliPrivKeyPemFile);
  58310. server_cbf.devId = 2;
  58311. wc_CryptoCb_RegisterDevice(server_cbf.devId, test_CryptoCb_Func,
  58312. (void*)svrPrivKeyPemFile);
  58313. /* Perform TLS server and client test */
  58314. /* First test is at WOLFSSL_CTX level */
  58315. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  58316. /* Check for success */
  58317. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  58318. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  58319. if (EXPECT_SUCCESS()) {
  58320. /* Second test is a WOLFSSL object level */
  58321. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  58322. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  58323. }
  58324. /* Check for success */
  58325. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  58326. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  58327. /* Un register the devId's */
  58328. wc_CryptoCb_UnRegisterDevice(client_cbf.devId);
  58329. client_cbf.devId = INVALID_DEVID;
  58330. wc_CryptoCb_UnRegisterDevice(server_cbf.devId);
  58331. server_cbf.devId = INVALID_DEVID;
  58332. return EXPECT_RESULT();
  58333. }
  58334. #endif /* WOLF_CRYPTO_CB && HAVE_IO_TESTS_DEPENDENCIES */
  58335. static int test_wc_CryptoCb(void)
  58336. {
  58337. EXPECT_DECLS;
  58338. #ifdef WOLF_CRYPTO_CB
  58339. /* TODO: Add crypto callback API tests */
  58340. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  58341. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  58342. int tlsVer;
  58343. #endif
  58344. #ifndef NO_RSA
  58345. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  58346. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  58347. svrCertFile, cliCertFile, cliKeyFile, cliKeyPubFile,
  58348. cliCertFile, svrCertFile, svrKeyFile, svrKeyPubFile),
  58349. TEST_SUCCESS);
  58350. }
  58351. #endif
  58352. #ifdef HAVE_ECC
  58353. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  58354. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  58355. caEccCertFile, cliEccCertFile, cliEccKeyFile, cliEccKeyPubFile,
  58356. cliEccCertFile, eccCertFile, eccKeyFile, eccKeyPubFile),
  58357. TEST_SUCCESS);
  58358. }
  58359. #endif
  58360. #ifdef HAVE_ED25519
  58361. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  58362. if (tlsVer == WOLFSSL_DTLSV1) continue;
  58363. ExpectIntEQ(test_wc_CryptoCb_TLS(tlsVer,
  58364. caEdCertFile, cliEdCertFile, cliEdKeyFile, cliEdKeyPubFile,
  58365. cliEdCertFile, edCertFile, edKeyFile, edKeyPubFile),
  58366. TEST_SUCCESS);
  58367. }
  58368. #endif
  58369. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  58370. #endif /* WOLF_CRYPTO_CB */
  58371. return EXPECT_RESULT();
  58372. }
  58373. #if defined(WOLFSSL_STATIC_MEMORY) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  58374. /* tlsVer: Example: WOLFSSL_TLSV1_2 or WOLFSSL_TLSV1_3 */
  58375. static int test_wolfSSL_CTX_StaticMemory_TLS(int tlsVer,
  58376. const char* cliCaPemFile, const char* cliCertPemFile,
  58377. const char* cliPrivKeyPemFile,
  58378. const char* svrCaPemFile, const char* svrCertPemFile,
  58379. const char* svrPrivKeyPemFile,
  58380. byte* cliMem, word32 cliMemSz, byte* svrMem, word32 svrMemSz)
  58381. {
  58382. EXPECT_DECLS;
  58383. callback_functions client_cbf;
  58384. callback_functions server_cbf;
  58385. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  58386. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  58387. if (tlsVer == WOLFSSL_TLSV1_3) {
  58388. #ifdef WOLFSSL_TLS13
  58389. server_cbf.method_ex = wolfTLSv1_3_server_method_ex;
  58390. client_cbf.method_ex = wolfTLSv1_3_client_method_ex;
  58391. #endif
  58392. }
  58393. else if (tlsVer == WOLFSSL_TLSV1_2) {
  58394. #ifndef WOLFSSL_NO_TLS12
  58395. server_cbf.method_ex = wolfTLSv1_2_server_method_ex;
  58396. client_cbf.method_ex = wolfTLSv1_2_client_method_ex;
  58397. #endif
  58398. }
  58399. else if (tlsVer == WOLFSSL_TLSV1_1) {
  58400. #ifndef NO_OLD_TLS
  58401. server_cbf.method_ex = wolfTLSv1_1_server_method_ex;
  58402. client_cbf.method_ex = wolfTLSv1_1_client_method_ex;
  58403. #endif
  58404. }
  58405. else if (tlsVer == WOLFSSL_TLSV1) {
  58406. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_TLSV10)
  58407. server_cbf.method_ex = wolfTLSv1_server_method_ex;
  58408. client_cbf.method_ex = wolfTLSv1_client_method_ex;
  58409. #endif
  58410. }
  58411. else if (tlsVer == WOLFSSL_SSLV3) {
  58412. #if !defined(NO_OLD_TLS) && defined(WOLFSSL_ALLOW_SSLV3) && \
  58413. defined(WOLFSSL_STATIC_RSA)
  58414. server_cbf.method_ex = wolfSSLv3_server_method_ex;
  58415. client_cbf.method_ex = wolfSSLv3_client_method_ex;
  58416. #endif
  58417. }
  58418. else if (tlsVer == WOLFSSL_DTLSV1_2) {
  58419. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12)
  58420. server_cbf.method_ex = wolfDTLSv1_2_server_method_ex;
  58421. client_cbf.method_ex = wolfDTLSv1_2_client_method_ex;
  58422. #endif
  58423. }
  58424. else if (tlsVer == WOLFSSL_DTLSV1) {
  58425. #if defined(WOLFSSL_DTLS) && !defined(NO_OLD_TLS)
  58426. server_cbf.method_ex = wolfDTLSv1_server_method_ex;
  58427. client_cbf.method_ex = wolfDTLSv1_client_method_ex;
  58428. #endif
  58429. }
  58430. if (server_cbf.method_ex == NULL) {
  58431. /* not enabled */
  58432. return TEST_SUCCESS;
  58433. }
  58434. /* Setup the keys for the TLS test */
  58435. client_cbf.certPemFile = cliCertPemFile;
  58436. client_cbf.keyPemFile = cliPrivKeyPemFile;
  58437. client_cbf.caPemFile = cliCaPemFile;
  58438. server_cbf.certPemFile = svrCertPemFile;
  58439. server_cbf.keyPemFile = svrPrivKeyPemFile;
  58440. server_cbf.caPemFile = svrCaPemFile;
  58441. client_cbf.mem = cliMem;
  58442. client_cbf.memSz = cliMemSz;
  58443. server_cbf.mem = svrMem;
  58444. server_cbf.memSz = svrMemSz;
  58445. client_cbf.devId = INVALID_DEVID;
  58446. server_cbf.devId = INVALID_DEVID;
  58447. /* Perform TLS server and client test */
  58448. /* First test is at WOLFSSL_CTX level */
  58449. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  58450. /* Check for success */
  58451. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  58452. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  58453. if (EXPECT_SUCCESS()) {
  58454. /* Second test is a WOLFSSL object level */
  58455. client_cbf.loadToSSL = 1; server_cbf.loadToSSL = 1;
  58456. test_wolfSSL_client_server(&client_cbf, &server_cbf);
  58457. }
  58458. /* Check for success */
  58459. ExpectIntEQ(server_cbf.return_code, TEST_SUCCESS);
  58460. ExpectIntEQ(client_cbf.return_code, TEST_SUCCESS);
  58461. return EXPECT_RESULT();
  58462. }
  58463. #endif /* WOLFSSL_STATIC_MEMORY && HAVE_IO_TESTS_DEPENDENCIES */
  58464. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  58465. static int test_wolfSSL_CTX_StaticMemory_SSL(WOLFSSL_CTX* ctx)
  58466. {
  58467. EXPECT_DECLS;
  58468. WOLFSSL *ssl1 = NULL, *ssl2 = NULL, *ssl3 = NULL;
  58469. WOLFSSL_MEM_STATS mem_stats;
  58470. WOLFSSL_MEM_CONN_STATS ssl_stats;
  58471. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS) && !defined(NO_RSA)
  58472. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx, svrCertFile,
  58473. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  58474. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx, svrKeyFile,
  58475. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  58476. #endif
  58477. ExpectNotNull((ssl1 = wolfSSL_new(ctx)));
  58478. ExpectNotNull((ssl2 = wolfSSL_new(ctx)));
  58479. /* this should fail because kMaxCtxClients == 2 */
  58480. ExpectNull((ssl3 = wolfSSL_new(ctx)));
  58481. if (wolfSSL_is_static_memory(ssl1, &ssl_stats) == 1) {
  58482. #ifdef DEBUG_WOLFSSL
  58483. wolfSSL_PrintStatsConn(&ssl_stats);
  58484. #endif
  58485. (void)ssl_stats;
  58486. }
  58487. /* display collected statistics */
  58488. if (wolfSSL_CTX_is_static_memory(ctx, &mem_stats) == 1) {
  58489. #ifdef DEBUG_WOLFSSL
  58490. wolfSSL_PrintStats(&mem_stats);
  58491. #endif
  58492. (void)mem_stats;
  58493. }
  58494. wolfSSL_free(ssl1);
  58495. wolfSSL_free(ssl2);
  58496. return EXPECT_RESULT();
  58497. }
  58498. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  58499. static int test_wolfSSL_CTX_StaticMemory(void)
  58500. {
  58501. EXPECT_DECLS;
  58502. #if defined(WOLFSSL_STATIC_MEMORY) && !defined(WOLFCRYPT_ONLY)
  58503. wolfSSL_method_func method_func;
  58504. WOLFSSL_CTX* ctx;
  58505. const int kMaxCtxClients = 2;
  58506. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  58507. #if !defined(NO_RSA) || defined(HAVE_ECC) || defined(HAVE_ED25519)
  58508. int tlsVer;
  58509. byte cliMem[TEST_TLS_STATIC_MEMSZ];
  58510. #endif
  58511. #endif
  58512. byte svrMem[TEST_TLS_STATIC_MEMSZ];
  58513. #ifndef NO_WOLFSSL_SERVER
  58514. #ifndef WOLFSSL_NO_TLS12
  58515. method_func = wolfTLSv1_2_server_method_ex;
  58516. #else
  58517. method_func = wolfTLSv1_3_server_method_ex;
  58518. #endif
  58519. #else
  58520. #ifndef WOLFSSL_NO_TLS12
  58521. method_func = wolfTLSv1_2_client_method_ex;
  58522. #else
  58523. method_func = wolfTLSv1_3_client_method_ex;
  58524. #endif
  58525. #endif
  58526. /* Test creating CTX directly from static memory pool */
  58527. ctx = NULL;
  58528. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, method_func, svrMem,
  58529. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  58530. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  58531. wolfSSL_CTX_free(ctx);
  58532. ctx = NULL;
  58533. /* Test for heap allocated CTX, then assigning static pool to it */
  58534. ExpectNotNull(ctx = wolfSSL_CTX_new(method_func(NULL)));
  58535. ExpectIntEQ(wolfSSL_CTX_load_static_memory(&ctx, NULL, svrMem,
  58536. sizeof(svrMem), 0, kMaxCtxClients), WOLFSSL_SUCCESS);
  58537. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_SSL(ctx), TEST_SUCCESS);
  58538. wolfSSL_CTX_free(ctx);
  58539. /* TLS Level Tests using static memory */
  58540. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  58541. #ifndef NO_RSA
  58542. for (tlsVer = WOLFSSL_SSLV3; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  58543. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  58544. svrCertFile, cliCertFile, cliKeyFile,
  58545. cliCertFile, svrCertFile, svrKeyFile,
  58546. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  58547. TEST_SUCCESS);
  58548. }
  58549. #endif
  58550. #ifdef HAVE_ECC
  58551. for (tlsVer = WOLFSSL_TLSV1; tlsVer <= WOLFSSL_DTLSV1; tlsVer++) {
  58552. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  58553. caEccCertFile, cliEccCertFile, cliEccKeyFile,
  58554. cliEccCertFile, eccCertFile, eccKeyFile,
  58555. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  58556. TEST_SUCCESS);
  58557. }
  58558. #endif
  58559. #ifdef HAVE_ED25519
  58560. for (tlsVer = WOLFSSL_TLSV1_2; tlsVer <= WOLFSSL_DTLSV1_2; tlsVer++) {
  58561. if (tlsVer == WOLFSSL_DTLSV1) continue;
  58562. ExpectIntEQ(test_wolfSSL_CTX_StaticMemory_TLS(tlsVer,
  58563. caEdCertFile, cliEdCertFile, cliEdKeyFile,
  58564. cliEdCertFile, edCertFile, edKeyFile,
  58565. cliMem, (word32)sizeof(cliMem), svrMem, (word32)sizeof(svrMem)),
  58566. TEST_SUCCESS);
  58567. }
  58568. #endif
  58569. #endif /* HAVE_IO_TESTS_DEPENDENCIES */
  58570. #endif /* WOLFSSL_STATIC_MEMORY && !WOLFCRYPT_ONLY */
  58571. return EXPECT_RESULT();
  58572. }
  58573. static int test_openssl_FIPS_drbg(void)
  58574. {
  58575. EXPECT_DECLS;
  58576. #if defined(OPENSSL_EXTRA) && !defined(WC_NO_RNG) && defined(HAVE_HASHDRBG)
  58577. DRBG_CTX* dctx = NULL;
  58578. byte data1[32], data2[32], zeroData[32];
  58579. byte testSeed[16];
  58580. size_t dlen = sizeof(data1);
  58581. int i;
  58582. XMEMSET(data1, 0, dlen);
  58583. XMEMSET(data2, 0, dlen);
  58584. XMEMSET(zeroData, 0, sizeof(zeroData));
  58585. for (i = 0; i < (int)sizeof(testSeed); i++) {
  58586. testSeed[i] = (byte)i;
  58587. }
  58588. ExpectNotNull(dctx = FIPS_get_default_drbg());
  58589. ExpectIntEQ(FIPS_drbg_init(dctx, 0, 0), WOLFSSL_SUCCESS);
  58590. ExpectIntEQ(FIPS_drbg_set_callbacks(dctx, NULL, NULL, 20, NULL, NULL),
  58591. WOLFSSL_SUCCESS);
  58592. ExpectIntEQ(FIPS_drbg_instantiate(dctx, NULL, 0), WOLFSSL_SUCCESS);
  58593. ExpectIntEQ(FIPS_drbg_generate(dctx, data1, dlen, 0, NULL, 0),
  58594. WOLFSSL_SUCCESS);
  58595. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  58596. ExpectIntEQ(FIPS_drbg_reseed(dctx, testSeed, sizeof(testSeed)),
  58597. WOLFSSL_SUCCESS);
  58598. ExpectIntEQ(FIPS_drbg_generate(dctx, data2, dlen, 0, NULL, 0),
  58599. WOLFSSL_SUCCESS);
  58600. ExpectIntNE(XMEMCMP(data1, zeroData, dlen), 0);
  58601. ExpectIntNE(XMEMCMP(data1, data2, dlen), 0);
  58602. ExpectIntEQ(FIPS_drbg_uninstantiate(dctx), WOLFSSL_SUCCESS);
  58603. #ifndef HAVE_GLOBAL_RNG
  58604. /* gets freed by wolfSSL_Cleanup() when HAVE_GLOBAL_RNG defined */
  58605. wolfSSL_FIPS_drbg_free(dctx);
  58606. #endif
  58607. #endif
  58608. return EXPECT_RESULT();
  58609. }
  58610. static int test_wolfSSL_FIPS_mode(void)
  58611. {
  58612. EXPECT_DECLS;
  58613. #if defined(OPENSSL_ALL)
  58614. #ifdef HAVE_FIPS
  58615. ExpectIntEQ(wolfSSL_FIPS_mode(), 1);
  58616. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_FAILURE);
  58617. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_SUCCESS);
  58618. #else
  58619. ExpectIntEQ(wolfSSL_FIPS_mode(), 0);
  58620. ExpectIntEQ(wolfSSL_FIPS_mode_set(0), WOLFSSL_SUCCESS);
  58621. ExpectIntEQ(wolfSSL_FIPS_mode_set(1), WOLFSSL_FAILURE);
  58622. #endif
  58623. #endif
  58624. return EXPECT_RESULT();
  58625. }
  58626. #ifdef WOLFSSL_DTLS
  58627. /* Prints out the current window */
  58628. static void DUW_TEST_print_window_binary(word32 h, word32 l, word32* w) {
  58629. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  58630. int i;
  58631. for (i = WOLFSSL_DTLS_WINDOW_WORDS - 1; i >= 0; i--) {
  58632. word32 b = w[i];
  58633. int j;
  58634. /* Prints out a 32 bit binary number in big endian order */
  58635. for (j = 0; j < 32; j++, b <<= 1) {
  58636. if (b & (((word32)1) << 31))
  58637. fprintf(stderr, "1");
  58638. else
  58639. fprintf(stderr, "0");
  58640. }
  58641. fprintf(stderr, " ");
  58642. }
  58643. fprintf(stderr, "cur_hi %u cur_lo %u\n", h, l);
  58644. #else
  58645. (void)h;
  58646. (void)l;
  58647. (void)w;
  58648. #endif
  58649. }
  58650. /* a - cur_hi
  58651. * b - cur_lo
  58652. * c - next_hi
  58653. * d - next_lo
  58654. * e - window
  58655. * f - expected next_hi
  58656. * g - expected next_lo
  58657. * h - expected window[1]
  58658. * i - expected window[0]
  58659. */
  58660. #define DUW_TEST(a,b,c,d,e,f,g,h,i) do { \
  58661. ExpectIntEQ(wolfSSL_DtlsUpdateWindow((a), (b), &(c), &(d), (e)), 1); \
  58662. DUW_TEST_print_window_binary((a), (b), (e)); \
  58663. ExpectIntEQ((c), (f)); \
  58664. ExpectIntEQ((d), (g)); \
  58665. ExpectIntEQ((e)[1], (h)); \
  58666. ExpectIntEQ((e)[0], (i)); \
  58667. } while (0)
  58668. static int test_wolfSSL_DtlsUpdateWindow(void)
  58669. {
  58670. EXPECT_DECLS;
  58671. word32 window[WOLFSSL_DTLS_WINDOW_WORDS];
  58672. word32 next_lo = 0;
  58673. word16 next_hi = 0;
  58674. #ifdef WOLFSSL_DEBUG_DTLS_WINDOW
  58675. fprintf(stderr, "\n");
  58676. #endif
  58677. XMEMSET(window, 0, sizeof window);
  58678. DUW_TEST(0, 0, next_hi, next_lo, window, 0, 1, 0, 0x01);
  58679. DUW_TEST(0, 1, next_hi, next_lo, window, 0, 2, 0, 0x03);
  58680. DUW_TEST(0, 5, next_hi, next_lo, window, 0, 6, 0, 0x31);
  58681. DUW_TEST(0, 4, next_hi, next_lo, window, 0, 6, 0, 0x33);
  58682. DUW_TEST(0, 100, next_hi, next_lo, window, 0, 101, 0, 0x01);
  58683. DUW_TEST(0, 101, next_hi, next_lo, window, 0, 102, 0, 0x03);
  58684. DUW_TEST(0, 133, next_hi, next_lo, window, 0, 134, 0x03, 0x01);
  58685. DUW_TEST(0, 200, next_hi, next_lo, window, 0, 201, 0, 0x01);
  58686. DUW_TEST(0, 264, next_hi, next_lo, window, 0, 265, 0, 0x01);
  58687. DUW_TEST(0, 0xFFFFFFFF, next_hi, next_lo, window, 1, 0, 0, 0x01);
  58688. DUW_TEST(0, 0xFFFFFFFD, next_hi, next_lo, window, 1, 0, 0, 0x05);
  58689. DUW_TEST(0, 0xFFFFFFFE, next_hi, next_lo, window, 1, 0, 0, 0x07);
  58690. DUW_TEST(1, 3, next_hi, next_lo, window, 1, 4, 0, 0x71);
  58691. DUW_TEST(1, 0, next_hi, next_lo, window, 1, 4, 0, 0x79);
  58692. DUW_TEST(1, 0xFFFFFFFF, next_hi, next_lo, window, 2, 0, 0, 0x01);
  58693. DUW_TEST(2, 3, next_hi, next_lo, window, 2, 4, 0, 0x11);
  58694. DUW_TEST(2, 0, next_hi, next_lo, window, 2, 4, 0, 0x19);
  58695. DUW_TEST(2, 25, next_hi, next_lo, window, 2, 26, 0, 0x6400001);
  58696. DUW_TEST(2, 27, next_hi, next_lo, window, 2, 28, 0, 0x19000005);
  58697. DUW_TEST(2, 29, next_hi, next_lo, window, 2, 30, 0, 0x64000015);
  58698. DUW_TEST(2, 33, next_hi, next_lo, window, 2, 34, 6, 0x40000151);
  58699. DUW_TEST(2, 60, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  58700. DUW_TEST(1, 0xFFFFFFF0, next_hi, next_lo, window, 2, 61, 0x3200000A, 0x88000001);
  58701. DUW_TEST(2, 0xFFFFFFFD, next_hi, next_lo, window, 2, 0xFFFFFFFE, 0, 0x01);
  58702. DUW_TEST(3, 1, next_hi, next_lo, window, 3, 2, 0, 0x11);
  58703. DUW_TEST(99, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  58704. DUW_TEST(50, 66, next_hi, next_lo, window, 99, 67, 0, 0x01);
  58705. DUW_TEST(100, 68, next_hi, next_lo, window, 100, 69, 0, 0x01);
  58706. DUW_TEST(99, 50, next_hi, next_lo, window, 100, 69, 0, 0x01);
  58707. DUW_TEST(99, 0xFFFFFFFF, next_hi, next_lo, window, 100, 69, 0, 0x01);
  58708. DUW_TEST(150, 0xFFFFFFFF, next_hi, next_lo, window, 151, 0, 0, 0x01);
  58709. DUW_TEST(152, 0xFFFFFFFF, next_hi, next_lo, window, 153, 0, 0, 0x01);
  58710. return EXPECT_RESULT();
  58711. }
  58712. #endif /* WOLFSSL_DTLS */
  58713. #ifdef WOLFSSL_DTLS
  58714. static int DFB_TEST(WOLFSSL* ssl, word32 seq, word32 len, word32 f_offset,
  58715. word32 f_len, word32 f_count, byte ready, word32 bytesReceived)
  58716. {
  58717. DtlsMsg* cur;
  58718. static byte msg[100];
  58719. static byte msgInit = 0;
  58720. if (!msgInit) {
  58721. int i;
  58722. for (i = 0; i < 100; i++)
  58723. msg[i] = i + 1;
  58724. msgInit = 1;
  58725. }
  58726. /* Sanitize test parameters */
  58727. if (len > sizeof(msg))
  58728. return -1;
  58729. if (f_offset + f_len > sizeof(msg))
  58730. return -1;
  58731. DtlsMsgStore(ssl, 0, seq, msg + f_offset, len, certificate, f_offset, f_len, NULL);
  58732. if (ssl->dtls_rx_msg_list == NULL)
  58733. return -100;
  58734. if ((cur = DtlsMsgFind(ssl->dtls_rx_msg_list, 0, seq)) == NULL)
  58735. return -200;
  58736. if (cur->fragBucketListCount != f_count)
  58737. return -300;
  58738. if (cur->ready != ready)
  58739. return -400;
  58740. if (cur->bytesReceived != bytesReceived)
  58741. return -500;
  58742. if (ready) {
  58743. if (cur->fragBucketList != NULL)
  58744. return -600;
  58745. if (XMEMCMP(cur->fullMsg, msg, cur->sz) != 0)
  58746. return -700;
  58747. }
  58748. else {
  58749. DtlsFragBucket* fb;
  58750. if (cur->fragBucketList == NULL)
  58751. return -800;
  58752. for (fb = cur->fragBucketList; fb != NULL; fb = fb->m.m.next) {
  58753. if (XMEMCMP(fb->buf, msg + fb->m.m.offset, fb->m.m.sz) != 0)
  58754. return -900;
  58755. }
  58756. }
  58757. return 0;
  58758. }
  58759. static int test_wolfSSL_DTLS_fragment_buckets(void)
  58760. {
  58761. EXPECT_DECLS;
  58762. WOLFSSL ssl[1];
  58763. XMEMSET(ssl, 0, sizeof(*ssl));
  58764. ExpectIntEQ(DFB_TEST(ssl, 0, 100, 0, 100, 0, 1, 100), 0); /* 0-100 */
  58765. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  58766. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 20, 20, 1, 0, 40), 0); /* 20-40 */
  58767. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 40, 20, 1, 0, 60), 0); /* 40-60 */
  58768. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 60, 20, 1, 0, 80), 0); /* 60-80 */
  58769. ExpectIntEQ(DFB_TEST(ssl, 1, 100, 80, 20, 0, 1, 100), 0); /* 80-100 */
  58770. /* Test all permutations of 3 regions */
  58771. /* 1 2 3 */
  58772. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  58773. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 30, 30, 1, 0, 60), 0); /* 30-60 */
  58774. ExpectIntEQ(DFB_TEST(ssl, 2, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  58775. /* 1 3 2 */
  58776. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  58777. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 60, 40, 2, 0, 70), 0); /* 60-100 */
  58778. ExpectIntEQ(DFB_TEST(ssl, 3, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  58779. /* 2 1 3 */
  58780. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  58781. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 0, 30, 1, 0, 60), 0); /* 0-30 */
  58782. ExpectIntEQ(DFB_TEST(ssl, 4, 100, 60, 40, 0, 1, 100), 0); /* 60-100 */
  58783. /* 2 3 1 */
  58784. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 30, 30, 1, 0, 30), 0); /* 30-60 */
  58785. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 60, 40, 1, 0, 70), 0); /* 60-100 */
  58786. ExpectIntEQ(DFB_TEST(ssl, 5, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  58787. /* 3 1 2 */
  58788. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  58789. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 0, 30, 2, 0, 70), 0); /* 0-30 */
  58790. ExpectIntEQ(DFB_TEST(ssl, 6, 100, 30, 30, 0, 1, 100), 0); /* 30-60 */
  58791. /* 3 2 1 */
  58792. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 60, 40, 1, 0, 40), 0); /* 60-100 */
  58793. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 30, 30, 1, 0, 70), 0); /* 30-60 */
  58794. ExpectIntEQ(DFB_TEST(ssl, 7, 100, 0, 30, 0, 1, 100), 0); /* 0-30 */
  58795. /* Test overlapping regions */
  58796. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 0, 30, 1, 0, 30), 0); /* 0-30 */
  58797. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 10, 1, 0, 30), 0); /* 20-30 */
  58798. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 70, 10, 2, 0, 40), 0); /* 70-80 */
  58799. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 20, 30, 2, 0, 60), 0); /* 20-50 */
  58800. ExpectIntEQ(DFB_TEST(ssl, 8, 100, 40, 60, 0, 1, 100), 0); /* 40-100 */
  58801. /* Test overlapping multiple regions */
  58802. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  58803. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 5, 2, 0, 25), 0); /* 30-35 */
  58804. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 40, 5, 3, 0, 30), 0); /* 40-45 */
  58805. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 50, 5, 4, 0, 35), 0); /* 50-55 */
  58806. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 60, 5, 5, 0, 40), 0); /* 60-65 */
  58807. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 70, 5, 6, 0, 45), 0); /* 70-75 */
  58808. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 30, 25, 4, 0, 55), 0); /* 30-55 */
  58809. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 55, 15, 2, 0, 65), 0); /* 55-70 */
  58810. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 75, 25, 2, 0, 90), 0); /* 75-100 */
  58811. ExpectIntEQ(DFB_TEST(ssl, 9, 100, 10, 25, 0, 1, 100), 0); /* 10-35 */
  58812. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 20, 1, 0, 20), 0); /* 0-20 */
  58813. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 30, 20, 2, 0, 40), 0); /* 30-50 */
  58814. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 0, 40, 1, 0, 50), 0); /* 0-40 */
  58815. ExpectIntEQ(DFB_TEST(ssl, 10, 100, 50, 50, 0, 1, 100), 0); /* 10-35 */
  58816. DtlsMsgListDelete(ssl->dtls_rx_msg_list, ssl->heap);
  58817. ssl->dtls_rx_msg_list = NULL;
  58818. ssl->dtls_rx_msg_list_sz = 0;
  58819. return EXPECT_RESULT();
  58820. }
  58821. #endif
  58822. #if !defined(NO_FILESYSTEM) && \
  58823. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  58824. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  58825. static int test_wolfSSL_dtls_stateless2(void)
  58826. {
  58827. EXPECT_DECLS;
  58828. WOLFSSL *ssl_c = NULL;
  58829. WOLFSSL *ssl_c2 = NULL;
  58830. WOLFSSL *ssl_s = NULL;
  58831. struct test_memio_ctx test_ctx;
  58832. WOLFSSL_CTX *ctx_c = NULL;
  58833. WOLFSSL_CTX *ctx_s = NULL;
  58834. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58835. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58836. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  58837. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  58838. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  58839. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  58840. /* send CH */
  58841. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  58842. (ssl_c2->error == WANT_READ));
  58843. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58844. (ssl_s->error == WANT_READ));
  58845. ExpectIntNE(test_ctx.c_len, 0);
  58846. /* consume HRR */
  58847. test_ctx.c_len = 0;
  58848. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58849. wolfSSL_free(ssl_c2);
  58850. wolfSSL_free(ssl_c);
  58851. wolfSSL_free(ssl_s);
  58852. wolfSSL_CTX_free(ctx_c);
  58853. wolfSSL_CTX_free(ctx_s);
  58854. return EXPECT_RESULT();
  58855. }
  58856. #ifdef HAVE_MAX_FRAGMENT
  58857. static int test_wolfSSL_dtls_stateless_maxfrag(void)
  58858. {
  58859. EXPECT_DECLS;
  58860. WOLFSSL *ssl_c = NULL;
  58861. WOLFSSL *ssl_c2 = NULL;
  58862. WOLFSSL *ssl_s = NULL;
  58863. struct test_memio_ctx test_ctx;
  58864. WOLFSSL_CTX *ctx_c = NULL;
  58865. WOLFSSL_CTX *ctx_s = NULL;
  58866. word16 max_fragment = 0;
  58867. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58868. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  58869. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  58870. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c));
  58871. ExpectIntEQ(wolfSSL_UseMaxFragment(ssl_c2, WOLFSSL_MFL_2_8),
  58872. WOLFSSL_SUCCESS);
  58873. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  58874. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  58875. if (ssl_s != NULL) {
  58876. max_fragment = ssl_s->max_fragment;
  58877. }
  58878. /* send CH */
  58879. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  58880. (ssl_c2->error == WANT_READ));
  58881. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58882. (ssl_s->error == WANT_READ));
  58883. /* CH without cookie shouldn't change state */
  58884. ExpectIntEQ(ssl_s->max_fragment, max_fragment);
  58885. ExpectIntNE(test_ctx.c_len, 0);
  58886. /* consume HRR from buffer */
  58887. test_ctx.c_len = 0;
  58888. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  58889. wolfSSL_free(ssl_c2);
  58890. wolfSSL_free(ssl_c);
  58891. wolfSSL_free(ssl_s);
  58892. wolfSSL_CTX_free(ctx_c);
  58893. wolfSSL_CTX_free(ctx_s);
  58894. return EXPECT_RESULT();
  58895. }
  58896. #endif /* HAVE_MAX_FRAGMENT */
  58897. #if defined(WOLFSSL_DTLS_NO_HVR_ON_RESUME)
  58898. #define ROUNDS_WITH_HVR 4
  58899. #define ROUNDS_WITHOUT_HVR 2
  58900. #define HANDSHAKE_TYPE_OFFSET DTLS_RECORD_HEADER_SZ
  58901. static int buf_is_hvr(const byte *data, int len)
  58902. {
  58903. if (len < DTLS_RECORD_HEADER_SZ + DTLS_HANDSHAKE_HEADER_SZ)
  58904. return 0;
  58905. return data[HANDSHAKE_TYPE_OFFSET] == hello_verify_request;
  58906. }
  58907. static int _test_wolfSSL_dtls_stateless_resume(byte useticket, byte bad)
  58908. {
  58909. EXPECT_DECLS;
  58910. struct test_memio_ctx test_ctx;
  58911. WOLFSSL_CTX *ctx_c = NULL;
  58912. WOLFSSL_CTX *ctx_s = NULL;
  58913. WOLFSSL *ssl_c = NULL;
  58914. WOLFSSL *ssl_s = NULL;
  58915. WOLFSSL_SESSION *sess = NULL;
  58916. int round_trips;
  58917. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58918. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  58919. &ssl_s, wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  58920. #ifdef HAVE_SESSION_TICKET
  58921. if (useticket) {
  58922. ExpectIntEQ(wolfSSL_UseSessionTicket(ssl_c), WOLFSSL_SUCCESS);
  58923. }
  58924. #endif
  58925. round_trips = ROUNDS_WITH_HVR;
  58926. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, round_trips,
  58927. &round_trips), 0);
  58928. ExpectIntEQ(round_trips, ROUNDS_WITH_HVR);
  58929. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  58930. wolfSSL_shutdown(ssl_c);
  58931. wolfSSL_shutdown(ssl_s);
  58932. wolfSSL_free(ssl_c);
  58933. ssl_c = NULL;
  58934. wolfSSL_free(ssl_s);
  58935. ssl_s = NULL;
  58936. test_ctx.c_len = test_ctx.s_len = 0;
  58937. /* make resumption invalid */
  58938. if (bad && (sess != NULL)) {
  58939. if (useticket) {
  58940. #ifdef HAVE_SESSION_TICKET
  58941. if (sess->ticket != NULL) {
  58942. sess->ticket[0] = !sess->ticket[0];
  58943. }
  58944. #endif /* HAVE_SESSION_TICKET */
  58945. }
  58946. else {
  58947. sess->sessionID[0] = !sess->sessionID[0];
  58948. }
  58949. }
  58950. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  58951. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  58952. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  58953. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  58954. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  58955. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  58956. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  58957. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  58958. (ssl_c->error == WANT_READ));
  58959. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  58960. (ssl_s->error == WANT_READ));
  58961. ExpectFalse(bad && !buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  58962. ExpectFalse(!bad && buf_is_hvr(test_ctx.c_buff, test_ctx.c_len));
  58963. if (!useticket) {
  58964. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, &round_trips), 0);
  58965. ExpectFalse(bad && round_trips != ROUNDS_WITH_HVR - 1);
  58966. ExpectFalse(!bad && round_trips != ROUNDS_WITHOUT_HVR - 1);
  58967. }
  58968. wolfSSL_SESSION_free(sess);
  58969. wolfSSL_free(ssl_c);
  58970. wolfSSL_free(ssl_s);
  58971. wolfSSL_CTX_free(ctx_c);
  58972. wolfSSL_CTX_free(ctx_s);
  58973. return EXPECT_RESULT();
  58974. }
  58975. static int test_wolfSSL_dtls_stateless_resume(void)
  58976. {
  58977. EXPECT_DECLS;
  58978. #ifdef HAVE_SESSION_TICKET
  58979. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 0), TEST_SUCCESS);
  58980. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(1, 1), TEST_SUCCESS);
  58981. #endif /* HAVE_SESION_TICKET */
  58982. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 0), TEST_SUCCESS);
  58983. ExpectIntEQ(_test_wolfSSL_dtls_stateless_resume(0, 1), TEST_SUCCESS);
  58984. return EXPECT_RESULT();
  58985. }
  58986. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  58987. #if !defined(NO_OLD_TLS)
  58988. static int test_wolfSSL_dtls_stateless_downgrade(void)
  58989. {
  58990. EXPECT_DECLS;
  58991. WOLFSSL_CTX *ctx_c = NULL;
  58992. WOLFSSL_CTX *ctx_c2 = NULL;
  58993. WOLFSSL_CTX *ctx_s = NULL;
  58994. WOLFSSL *ssl_c = NULL;
  58995. WOLFSSL *ssl_c2 = NULL;
  58996. WOLFSSL *ssl_s = NULL;
  58997. struct test_memio_ctx test_ctx;
  58998. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  58999. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59000. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  59001. ExpectIntEQ(wolfSSL_CTX_SetMinVersion(ctx_s, WOLFSSL_DTLSV1),
  59002. WOLFSSL_SUCCESS);
  59003. ExpectNotNull(ctx_c2 = wolfSSL_CTX_new(wolfDTLSv1_client_method()));
  59004. wolfSSL_SetIORecv(ctx_c2, test_memio_read_cb);
  59005. wolfSSL_SetIOSend(ctx_c2, test_memio_write_cb);
  59006. ExpectNotNull(ssl_c2 = wolfSSL_new(ctx_c2));
  59007. wolfSSL_SetIOWriteCtx(ssl_c2, &test_ctx);
  59008. wolfSSL_SetIOReadCtx(ssl_c2, &test_ctx);
  59009. /* send CH */
  59010. ExpectTrue((wolfSSL_connect(ssl_c2) == WOLFSSL_FATAL_ERROR) &&
  59011. (ssl_c2->error == WANT_READ));
  59012. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  59013. (ssl_s->error == WANT_READ));
  59014. ExpectIntNE(test_ctx.c_len, 0);
  59015. /* consume HRR */
  59016. test_ctx.c_len = 0;
  59017. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59018. wolfSSL_free(ssl_c2);
  59019. wolfSSL_free(ssl_c);
  59020. wolfSSL_free(ssl_s);
  59021. wolfSSL_CTX_free(ctx_c);
  59022. wolfSSL_CTX_free(ctx_c2);
  59023. wolfSSL_CTX_free(ctx_s);
  59024. return EXPECT_RESULT();
  59025. }
  59026. #endif /* !defined(NO_OLD_TLS) */
  59027. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  59028. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)*/
  59029. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  59030. !defined(NO_OLD_TLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  59031. static int test_WOLFSSL_dtls_version_alert(void)
  59032. {
  59033. EXPECT_DECLS;
  59034. struct test_memio_ctx test_ctx;
  59035. WOLFSSL_CTX *ctx_c = NULL;
  59036. WOLFSSL_CTX *ctx_s = NULL;
  59037. WOLFSSL *ssl_c = NULL;
  59038. WOLFSSL *ssl_s = NULL;
  59039. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59040. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59041. wolfDTLSv1_2_client_method, wolfDTLSv1_server_method), 0);
  59042. /* client hello */
  59043. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  59044. (ssl_c->error == WANT_READ));
  59045. /* hrr */
  59046. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  59047. (ssl_s->error == WANT_READ));
  59048. /* client hello 1 */
  59049. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  59050. (ssl_c->error == WANT_READ));
  59051. /* server hello */
  59052. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  59053. (ssl_s->error == WANT_READ));
  59054. /* should fail */
  59055. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_FATAL_ERROR) &&
  59056. (ssl_c->error == VERSION_ERROR));
  59057. /* shuould fail */
  59058. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_FATAL_ERROR) &&
  59059. (ssl_s->error == VERSION_ERROR || ssl_s->error == FATAL_ERROR));
  59060. wolfSSL_free(ssl_c);
  59061. wolfSSL_free(ssl_s);
  59062. wolfSSL_CTX_free(ctx_c);
  59063. wolfSSL_CTX_free(ctx_s);
  59064. return EXPECT_RESULT();
  59065. }
  59066. #else
  59067. static int test_WOLFSSL_dtls_version_alert(void)
  59068. {
  59069. return TEST_SKIPPED;
  59070. }
  59071. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) &&
  59072. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) &&
  59073. * !defined(NO_OLD_TLS) && !defined(NO_RSA)
  59074. */
  59075. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  59076. && defined(WOLFSSL_TLS13) && \
  59077. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))\
  59078. && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  59079. static int send_new_session_ticket(WOLFSSL *ssl, byte nonceLength, byte filler)
  59080. {
  59081. struct test_memio_ctx *test_ctx;
  59082. byte buf[2048];
  59083. int idx, sz;
  59084. word32 tmp;
  59085. int ret;
  59086. idx = 5; /* space for record header */
  59087. buf[idx] = session_ticket; /* type */
  59088. idx++;
  59089. tmp = OPAQUE32_LEN +
  59090. OPAQUE32_LEN +
  59091. OPAQUE8_LEN + nonceLength +
  59092. OPAQUE16_LEN + OPAQUE8_LEN + OPAQUE16_LEN;
  59093. c32to24(tmp, buf + idx);
  59094. idx += OPAQUE24_LEN;
  59095. c32toa((word32)12345, buf+idx); /* lifetime */
  59096. idx += OPAQUE32_LEN;
  59097. c32toa((word32)12345, buf+idx); /* add */
  59098. idx += OPAQUE32_LEN;
  59099. buf[idx] = nonceLength; /* nonce length */
  59100. idx++;
  59101. XMEMSET(&buf[idx], filler, nonceLength); /* nonce */
  59102. idx += nonceLength;
  59103. tmp = 1; /* ticket len */
  59104. c16toa((word16)tmp, buf+idx);
  59105. idx += 2;
  59106. buf[idx] = 0xFF; /* ticket */
  59107. idx++;
  59108. tmp = 0; /* ext len */
  59109. c16toa((word16)tmp, buf+idx);
  59110. idx += 2;
  59111. sz = BuildTls13Message(ssl, buf, 2048, buf+5, idx - 5,
  59112. handshake, 0, 0, 0);
  59113. test_ctx = (struct test_memio_ctx*)wolfSSL_GetIOWriteCtx(ssl);
  59114. ret = test_memio_write_cb(ssl, (char*)buf, sz, test_ctx);
  59115. return !(ret == sz);
  59116. }
  59117. static int test_ticket_nonce_check(WOLFSSL_SESSION *sess, byte len)
  59118. {
  59119. int ret = 0;
  59120. if ((sess == NULL) || (sess->ticketNonce.len != len)) {
  59121. ret = -1;
  59122. }
  59123. else {
  59124. int i;
  59125. for (i = 0; i < len; i++) {
  59126. if (sess->ticketNonce.data[i] != len) {
  59127. ret = -1;
  59128. break;
  59129. }
  59130. }
  59131. }
  59132. return ret;
  59133. }
  59134. static int test_ticket_nonce_malloc_do(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  59135. {
  59136. EXPECT_DECLS;
  59137. char *buf[1024];
  59138. ExpectIntEQ(send_new_session_ticket(ssl_s, len, len), 0);
  59139. ExpectTrue((wolfSSL_recv(ssl_c, buf, 1024, 0) == WOLFSSL_FATAL_ERROR) &&
  59140. (ssl_c->error == WANT_READ));
  59141. ExpectIntEQ(test_ticket_nonce_check(ssl_c->session, len), 0);
  59142. return EXPECT_RESULT();
  59143. }
  59144. static int test_ticket_nonce_cache(WOLFSSL *ssl_s, WOLFSSL *ssl_c, byte len)
  59145. {
  59146. EXPECT_DECLS;
  59147. WOLFSSL_SESSION *sess = NULL;
  59148. WOLFSSL_SESSION *cached = NULL;
  59149. WOLFSSL_CTX *ctx = ssl_c->ctx;
  59150. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, len), TEST_SUCCESS);
  59151. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  59152. ExpectIntEQ(AddSessionToCache(ctx, sess, sess->sessionID, sess->sessionIDSz,
  59153. NULL, ssl_c->options.side, 1,NULL), 0);
  59154. ExpectNotNull(cached = wolfSSL_SESSION_new());
  59155. ExpectIntEQ(wolfSSL_GetSessionFromCache(ssl_c, cached), WOLFSSL_SUCCESS);
  59156. ExpectIntEQ(test_ticket_nonce_check(cached, len), 0);
  59157. wolfSSL_SESSION_free(cached);
  59158. wolfSSL_SESSION_free(sess);
  59159. return EXPECT_RESULT();
  59160. }
  59161. static int test_ticket_nonce_malloc(void)
  59162. {
  59163. EXPECT_DECLS;
  59164. struct test_memio_ctx test_ctx;
  59165. WOLFSSL_CTX *ctx_c = NULL;
  59166. WOLFSSL_CTX *ctx_s = NULL;
  59167. WOLFSSL *ssl_c = NULL;
  59168. WOLFSSL *ssl_s = NULL;
  59169. byte small;
  59170. byte medium;
  59171. byte big;
  59172. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59173. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59174. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  59175. /* will send ticket manually */
  59176. ExpectIntEQ(wolfSSL_no_ticket_TLSv13(ssl_s), 0);
  59177. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  59178. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  59179. while (EXPECT_SUCCESS() && (ssl_c->options.handShakeDone == 0) &&
  59180. (ssl_s->options.handShakeDone == 0)) {
  59181. ExpectTrue((wolfSSL_connect(ssl_c) == WOLFSSL_SUCCESS) ||
  59182. (ssl_c->error == WANT_READ));
  59183. ExpectTrue((wolfSSL_accept(ssl_s) == WOLFSSL_SUCCESS) ||
  59184. (ssl_s->error == WANT_READ));
  59185. }
  59186. small = TLS13_TICKET_NONCE_STATIC_SZ;
  59187. medium = small + 20 <= 255 ? small + 20 : 255;
  59188. big = medium + 20 <= 255 ? small + 20 : 255;
  59189. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  59190. ExpectPtrEq(ssl_c->session->ticketNonce.data,
  59191. ssl_c->session->ticketNonce.dataStatic);
  59192. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  59193. TEST_SUCCESS);
  59194. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, big), TEST_SUCCESS);
  59195. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, medium),
  59196. TEST_SUCCESS);
  59197. ExpectIntEQ(test_ticket_nonce_malloc_do(ssl_s, ssl_c, small), TEST_SUCCESS);
  59198. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  59199. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  59200. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, big), TEST_SUCCESS);
  59201. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, medium), TEST_SUCCESS);
  59202. ExpectIntEQ(test_ticket_nonce_cache(ssl_s, ssl_c, small), TEST_SUCCESS);
  59203. wolfSSL_free(ssl_c);
  59204. wolfSSL_free(ssl_s);
  59205. wolfSSL_CTX_free(ctx_c);
  59206. wolfSSL_CTX_free(ctx_s);
  59207. return EXPECT_RESULT();
  59208. }
  59209. #endif /* WOLFSSL_TICKET_NONCE_MALLOC */
  59210. #if defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_TLS12) && \
  59211. !defined(WOLFSSL_TICKET_DECRYPT_NO_CREATE) && \
  59212. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  59213. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && !defined(NO_RSA) && \
  59214. defined(HAVE_ECC) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  59215. static int test_ticket_ret_create(void)
  59216. {
  59217. EXPECT_DECLS;
  59218. WOLFSSL_CTX *ctx_c = NULL;
  59219. WOLFSSL_CTX *ctx_s = NULL;
  59220. WOLFSSL *ssl_c = NULL;
  59221. WOLFSSL *ssl_s = NULL;
  59222. byte ticket[SESSION_TICKET_LEN];
  59223. struct test_memio_ctx test_ctx;
  59224. WOLFSSL_SESSION *sess = NULL;
  59225. word16 ticketLen = 0;
  59226. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59227. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59228. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  59229. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_NONE, 0);
  59230. wolfSSL_set_verify(ssl_c, WOLFSSL_VERIFY_NONE, 0);
  59231. ExpectIntEQ(wolfSSL_CTX_UseSessionTicket(ctx_c), WOLFSSL_SUCCESS);
  59232. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59233. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  59234. ExpectIntLE(sess->ticketLen, SESSION_TICKET_LEN);
  59235. if (sess != NULL) {
  59236. ticketLen = sess->ticketLen;
  59237. XMEMCPY(ticket, sess->ticket, sess->ticketLen);
  59238. }
  59239. wolfSSL_free(ssl_c);
  59240. ssl_c = NULL;
  59241. wolfSSL_free(ssl_s);
  59242. ssl_s = NULL;
  59243. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  59244. wolfSSL_SetIOWriteCtx(ssl_s, &test_ctx);
  59245. wolfSSL_SetIOReadCtx(ssl_s, &test_ctx);
  59246. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  59247. wolfSSL_SetIOWriteCtx(ssl_c, &test_ctx);
  59248. wolfSSL_SetIOReadCtx(ssl_c, &test_ctx);
  59249. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  59250. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  59251. ExpectIntLE(ssl_c->session->ticketLen, SESSION_TICKET_LEN);
  59252. ExpectIntEQ(ssl_c->session->ticketLen, ticketLen);
  59253. ExpectTrue(XMEMCMP(ssl_c->session->ticket, ticket, ticketLen) != 0);
  59254. wolfSSL_SESSION_free(sess);
  59255. wolfSSL_free(ssl_c);
  59256. wolfSSL_free(ssl_s);
  59257. wolfSSL_CTX_free(ctx_c);
  59258. wolfSSL_CTX_free(ctx_s);
  59259. return EXPECT_RESULT();
  59260. }
  59261. #else
  59262. static int test_ticket_ret_create(void)
  59263. {
  59264. return TEST_SKIPPED;
  59265. }
  59266. #endif
  59267. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && \
  59268. defined(HAVE_SESSION_TICKET) && defined(OPENSSL_EXTRA) && \
  59269. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  59270. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  59271. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  59272. static void test_ticket_and_psk_mixing_on_result(WOLFSSL* ssl)
  59273. {
  59274. int ret;
  59275. WOLFSSL_SESSION* session = NULL;
  59276. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  59277. if (!wolfSSL_is_server(ssl)) {
  59278. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  59279. AssertNotNull(session);
  59280. }
  59281. do {
  59282. ret = wolfSSL_shutdown(ssl);
  59283. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  59284. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  59285. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  59286. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  59287. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  59288. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  59289. #endif
  59290. if (!wolfSSL_is_server(ssl)) {
  59291. /* client */
  59292. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  59293. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  59294. wolfSSL_set_session(ssl, session);
  59295. wolfSSL_SESSION_free(session);
  59296. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  59297. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  59298. }
  59299. else {
  59300. /* server */
  59301. /* Different ciphersuite so that the ticket will be invalidated based on
  59302. * the ciphersuite */
  59303. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384"),
  59304. WOLFSSL_SUCCESS);
  59305. wolfSSL_set_psk_server_tls13_callback(ssl, my_psk_server_tls13_cb);
  59306. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  59307. }
  59308. }
  59309. static void test_ticket_and_psk_mixing_ssl_ready(WOLFSSL* ssl)
  59310. {
  59311. AssertIntEQ(wolfSSL_UseSessionTicket(ssl), WOLFSSL_SUCCESS);
  59312. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  59313. WOLFSSL_SUCCESS);
  59314. }
  59315. static int test_ticket_and_psk_mixing(void)
  59316. {
  59317. EXPECT_DECLS;
  59318. /* Test mixing tickets and regular PSK */
  59319. callback_functions client_cbs, server_cbs;
  59320. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59321. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59322. client_cbs.method = wolfTLSv1_3_client_method;
  59323. server_cbs.method = wolfTLSv1_3_server_method;
  59324. client_cbs.ssl_ready = test_ticket_and_psk_mixing_ssl_ready;
  59325. client_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  59326. server_cbs.on_result = test_ticket_and_psk_mixing_on_result;
  59327. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  59328. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  59329. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  59330. return EXPECT_RESULT();
  59331. }
  59332. #else
  59333. static int test_ticket_and_psk_mixing(void)
  59334. {
  59335. return TEST_SKIPPED;
  59336. }
  59337. #endif
  59338. #if defined(WOLFSSL_TLS13) && !defined(NO_PSK) && defined(HAVE_SESSION_TICKET) \
  59339. && defined(OPENSSL_EXTRA) && defined(HAVE_IO_TESTS_DEPENDENCIES) && \
  59340. defined(HAVE_AESGCM) && !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  59341. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  59342. static int test_prioritize_psk_cb_called = FALSE;
  59343. static unsigned int test_prioritize_psk_cb(WOLFSSL* ssl,
  59344. const char* identity, unsigned char* key, unsigned int key_max_len,
  59345. const char** ciphersuite)
  59346. {
  59347. test_prioritize_psk_cb_called = TRUE;
  59348. return my_psk_server_tls13_cb(ssl, identity, key, key_max_len, ciphersuite);
  59349. }
  59350. static void test_prioritize_psk_on_result(WOLFSSL* ssl)
  59351. {
  59352. int ret;
  59353. WOLFSSL_SESSION* session = NULL;
  59354. AssertIntEQ(wolfSSL_get_current_cipher_suite(ssl), 0x1301);
  59355. if (!wolfSSL_is_server(ssl)) {
  59356. session = wolfSSL_SESSION_dup(wolfSSL_get_session(ssl));
  59357. AssertNotNull(session);
  59358. }
  59359. do {
  59360. ret = wolfSSL_shutdown(ssl);
  59361. } while (ret == WOLFSSL_SHUTDOWN_NOT_DONE);
  59362. AssertIntEQ(wolfSSL_clear(ssl), WOLFSSL_SUCCESS);
  59363. wolfSSL_set_psk_callback_ctx(ssl, (void*)"TLS13-AES256-GCM-SHA384");
  59364. /* Previous connection was made with TLS13-AES128-GCM-SHA256. Order is
  59365. * important. */
  59366. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  59367. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  59368. #ifndef OPENSSL_COMPATIBLE_DEFAULTS
  59369. /* OpenSSL considers PSK to be verified. We error out with NO_PEER_CERT. */
  59370. wolfSSL_set_verify(ssl, WOLFSSL_VERIFY_NONE, NULL);
  59371. #endif
  59372. if (!wolfSSL_is_server(ssl)) {
  59373. /* client */
  59374. wolfSSL_set_psk_client_tls13_callback(ssl, my_psk_client_tls13_cb);
  59375. wolfSSL_set_session(ssl, session);
  59376. wolfSSL_SESSION_free(session);
  59377. AssertIntEQ(wolfSSL_connect(ssl), WOLFSSL_SUCCESS);
  59378. }
  59379. else {
  59380. /* server */
  59381. wolfSSL_set_psk_server_tls13_callback(ssl, test_prioritize_psk_cb);
  59382. AssertIntEQ(wolfSSL_accept(ssl), WOLFSSL_SUCCESS);
  59383. #ifdef WOLFSSL_PRIORITIZE_PSK
  59384. /* The ticket should be first tried with all ciphersuites and chosen */
  59385. AssertFalse(test_prioritize_psk_cb_called);
  59386. #else
  59387. /* Ciphersuites should be tried with each PSK. This triggers the PSK
  59388. * callback that sets this var. */
  59389. AssertTrue(test_prioritize_psk_cb_called);
  59390. #endif
  59391. }
  59392. }
  59393. static void test_prioritize_psk_ssl_ready(WOLFSSL* ssl)
  59394. {
  59395. if (!wolfSSL_is_server(ssl))
  59396. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  59397. WOLFSSL_SUCCESS);
  59398. else
  59399. AssertIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  59400. "TLS13-AES128-GCM-SHA256"), WOLFSSL_SUCCESS);
  59401. }
  59402. static int test_prioritize_psk(void)
  59403. {
  59404. EXPECT_DECLS;
  59405. /* We always send the ticket first. With WOLFSSL_PRIORITIZE_PSK the order
  59406. * of the PSK's will be followed instead of the ciphersuite. */
  59407. callback_functions client_cbs, server_cbs;
  59408. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59409. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59410. client_cbs.method = wolfTLSv1_3_client_method;
  59411. server_cbs.method = wolfTLSv1_3_server_method;
  59412. client_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  59413. server_cbs.ssl_ready = test_prioritize_psk_ssl_ready;
  59414. client_cbs.on_result = test_prioritize_psk_on_result;
  59415. server_cbs.on_result = test_prioritize_psk_on_result;
  59416. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  59417. ExpectIntEQ(client_cbs.return_code, TEST_SUCCESS);
  59418. ExpectIntEQ(server_cbs.return_code, TEST_SUCCESS);
  59419. return EXPECT_RESULT();
  59420. }
  59421. #else
  59422. static int test_prioritize_psk(void)
  59423. {
  59424. return TEST_SKIPPED;
  59425. }
  59426. #endif
  59427. #if defined(WOLFSSL_TLS13) && defined(OPENSSL_EXTRA) && \
  59428. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  59429. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  59430. !defined(WOLFSSL_NO_TLS12)
  59431. static int test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server(WOLFSSL_CTX* ctx)
  59432. {
  59433. EXPECT_DECLS;
  59434. ExpectTrue(SSL_CTX_set_cipher_list(ctx, "DEFAULT"));
  59435. /* Set TLS 1.3 specific suite */
  59436. ExpectTrue(SSL_CTX_set_ciphersuites(ctx, "TLS13-AES128-GCM-SHA256"));
  59437. return EXPECT_RESULT();
  59438. }
  59439. static int test_wolfSSL_CTX_set_ciphersuites(void)
  59440. {
  59441. EXPECT_DECLS;
  59442. /* Test using SSL_CTX_set_cipher_list and SSL_CTX_set_ciphersuites and then
  59443. * do a 1.2 connection. */
  59444. test_ssl_cbf client_cbs, server_cbs;
  59445. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59446. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59447. client_cbs.method = wolfTLSv1_2_client_method;
  59448. server_cbs.method = wolfTLS_server_method; /* Allow downgrade */
  59449. server_cbs.ctx_ready = test_wolfSSL_CTX_set_ciphersuites_ctx_ready_server;
  59450. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  59451. &server_cbs, NULL), TEST_SUCCESS);
  59452. return EXPECT_RESULT();
  59453. }
  59454. #else
  59455. static int test_wolfSSL_CTX_set_ciphersuites(void)
  59456. {
  59457. return TEST_SKIPPED;
  59458. }
  59459. #endif
  59460. #if defined(HAVE_CRL) && defined(WOLFSSL_CHECK_ALERT_ON_ERR) && \
  59461. defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  59462. static int test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready(WOLFSSL_CTX* ctx)
  59463. {
  59464. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  59465. return TEST_SUCCESS;
  59466. }
  59467. static int test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup(WOLFSSL* ssl)
  59468. {
  59469. EXPECT_DECLS;
  59470. WOLFSSL_ALERT_HISTORY h;
  59471. ExpectIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  59472. ExpectIntEQ(h.last_rx.level, alert_fatal);
  59473. ExpectIntEQ(h.last_rx.code, certificate_revoked);
  59474. return EXPECT_RESULT();
  59475. }
  59476. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  59477. {
  59478. EXPECT_DECLS;
  59479. test_ssl_cbf client_cbs, server_cbs;
  59480. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59481. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59482. server_cbs.certPemFile = "./certs/server-revoked-cert.pem";
  59483. server_cbs.keyPemFile = "./certs/server-revoked-key.pem";
  59484. client_cbs.crlPemFile = "./certs/crl/crl.revoked";
  59485. client_cbs.ctx_ready = test_wolfSSL_CRL_CERT_REVOKED_alert_ctx_ready;
  59486. server_cbs.on_cleanup = test_wolfSSL_CRL_CERT_REVOKED_alert_on_cleanup;
  59487. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  59488. &server_cbs, NULL), TEST_FAIL);
  59489. return EXPECT_RESULT();
  59490. }
  59491. #else
  59492. static int test_wolfSSL_CRL_CERT_REVOKED_alert(void)
  59493. {
  59494. return TEST_SKIPPED;
  59495. }
  59496. #endif
  59497. #if defined(WOLFSSL_TLS13) && defined(HAVE_SESSION_TICKET) \
  59498. && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_AESGCM) && \
  59499. !defined(NO_SHA256) && defined(WOLFSSL_AES_128) && \
  59500. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  59501. !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  59502. static WOLFSSL_CTX* test_TLS_13_ticket_different_ciphers_ctx = NULL;
  59503. static WOLFSSL_SESSION* test_TLS_13_ticket_different_ciphers_session = NULL;
  59504. static int test_TLS_13_ticket_different_ciphers_run = 0;
  59505. static int test_TLS_13_ticket_different_ciphers_ssl_ready(WOLFSSL* ssl)
  59506. {
  59507. EXPECT_DECLS;
  59508. switch (test_TLS_13_ticket_different_ciphers_run) {
  59509. case 0:
  59510. /* First run */
  59511. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES128-GCM-SHA256"),
  59512. WOLFSSL_SUCCESS);
  59513. if (wolfSSL_is_server(ssl)) {
  59514. ExpectNotNull(test_TLS_13_ticket_different_ciphers_ctx =
  59515. wolfSSL_get_SSL_CTX(ssl));
  59516. ExpectIntEQ(WOLFSSL_SUCCESS, wolfSSL_CTX_up_ref(
  59517. test_TLS_13_ticket_different_ciphers_ctx));
  59518. }
  59519. break;
  59520. case 1:
  59521. /* Second run */
  59522. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "TLS13-AES256-GCM-SHA384:"
  59523. "TLS13-AES128-GCM-SHA256"),
  59524. WOLFSSL_SUCCESS);
  59525. if (!wolfSSL_is_server(ssl)) {
  59526. ExpectIntEQ(wolfSSL_set_session(ssl,
  59527. test_TLS_13_ticket_different_ciphers_session),
  59528. WOLFSSL_SUCCESS);
  59529. }
  59530. break;
  59531. default:
  59532. /* Bad state? */
  59533. Fail(("Should not enter here"), ("Should not enter here"));
  59534. }
  59535. return EXPECT_RESULT();
  59536. }
  59537. static int test_TLS_13_ticket_different_ciphers_on_result(WOLFSSL* ssl)
  59538. {
  59539. EXPECT_DECLS;
  59540. switch (test_TLS_13_ticket_different_ciphers_run) {
  59541. case 0:
  59542. /* First run */
  59543. ExpectNotNull(test_TLS_13_ticket_different_ciphers_session =
  59544. wolfSSL_get1_session(ssl));
  59545. break;
  59546. case 1:
  59547. /* Second run */
  59548. ExpectTrue(wolfSSL_session_reused(ssl));
  59549. break;
  59550. default:
  59551. /* Bad state? */
  59552. Fail(("Should not enter here"), ("Should not enter here"));
  59553. }
  59554. return EXPECT_RESULT();
  59555. }
  59556. static int test_TLS_13_ticket_different_ciphers(void)
  59557. {
  59558. EXPECT_DECLS;
  59559. /* Check that we handle the connection when the ticket doesn't match
  59560. * the first ciphersuite. */
  59561. test_ssl_cbf client_cbs, server_cbs;
  59562. struct test_params {
  59563. method_provider client_meth;
  59564. method_provider server_meth;
  59565. int doUdp;
  59566. } params[] = {
  59567. #ifdef WOLFSSL_DTLS13
  59568. /* Test that the stateless code handles sessions correctly */
  59569. {wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, 1},
  59570. #endif
  59571. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, 0},
  59572. };
  59573. size_t i;
  59574. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  59575. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59576. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59577. test_TLS_13_ticket_different_ciphers_run = 0;
  59578. client_cbs.doUdp = server_cbs.doUdp = params[i].doUdp;
  59579. client_cbs.method = params[i].client_meth;
  59580. server_cbs.method = params[i].server_meth;
  59581. client_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  59582. server_cbs.ssl_ready = test_TLS_13_ticket_different_ciphers_ssl_ready;
  59583. client_cbs.on_result = test_TLS_13_ticket_different_ciphers_on_result;
  59584. server_cbs.ticNoInit = 1;
  59585. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  59586. &server_cbs, NULL), TEST_SUCCESS);
  59587. test_TLS_13_ticket_different_ciphers_run++;
  59588. server_cbs.ctx = test_TLS_13_ticket_different_ciphers_ctx;
  59589. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  59590. &server_cbs, NULL), TEST_SUCCESS);
  59591. wolfSSL_SESSION_free(test_TLS_13_ticket_different_ciphers_session);
  59592. test_TLS_13_ticket_different_ciphers_session = NULL;
  59593. wolfSSL_CTX_free(test_TLS_13_ticket_different_ciphers_ctx);
  59594. test_TLS_13_ticket_different_ciphers_ctx = NULL;
  59595. }
  59596. return EXPECT_RESULT();
  59597. }
  59598. #else
  59599. static int test_TLS_13_ticket_different_ciphers(void)
  59600. {
  59601. return TEST_SKIPPED;
  59602. }
  59603. #endif
  59604. #if defined(WOLFSSL_EXTRA_ALERTS) && !defined(WOLFSSL_NO_TLS12) && \
  59605. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)
  59606. #define TEST_WRONG_CS_CLIENT "DHE-RSA-AES128-SHA"
  59607. /* AKA TLS_DHE_RSA_WITH_AES_128_CBC_SHA */
  59608. byte test_extra_alerts_wrong_cs_sh[] = {
  59609. 0x16, 0x03, 0x03, 0x00, 0x56, 0x02, 0x00, 0x00, 0x52, 0x03, 0x03, 0xef,
  59610. 0x0c, 0x30, 0x98, 0xa2, 0xac, 0xfa, 0x68, 0xe9, 0x3e, 0xaa, 0x5c, 0xcf,
  59611. 0xa7, 0x42, 0x72, 0xaf, 0xa0, 0xe8, 0x39, 0x2b, 0x3e, 0x81, 0xa7, 0x7a,
  59612. 0xa5, 0x62, 0x8a, 0x0e, 0x41, 0xba, 0xda, 0x20, 0x18, 0x9f, 0xe1, 0x8c,
  59613. 0x1d, 0xc0, 0x37, 0x9c, 0xf4, 0x90, 0x5d, 0x8d, 0xa0, 0x79, 0xa7, 0x4b,
  59614. 0xa8, 0x79, 0xdf, 0xcd, 0x8d, 0xf5, 0xb5, 0x50, 0x5f, 0xf1, 0xdb, 0x4d,
  59615. 0xbb, 0x07, 0x54, 0x1c,
  59616. 0x00, 0x02, /* TLS_RSA_WITH_NULL_SHA */
  59617. 0x00, 0x00, 0x0a, 0x00, 0x0b, 0x00,
  59618. 0x02, 0x01, 0x00, 0x00, 0x17, 0x00, 0x00
  59619. };
  59620. static int test_extra_alerts_wrong_cs(void)
  59621. {
  59622. EXPECT_DECLS;
  59623. #ifdef BUILD_TLS_DHE_RSA_WITH_AES_128_CBC_SHA
  59624. struct test_memio_ctx test_ctx;
  59625. WOLFSSL_CTX *ctx_c = NULL;
  59626. WOLFSSL_ALERT_HISTORY h;
  59627. WOLFSSL *ssl_c = NULL;
  59628. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59629. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  59630. wolfTLSv1_2_client_method, NULL), 0);
  59631. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, TEST_WRONG_CS_CLIENT),
  59632. WOLFSSL_SUCCESS);
  59633. /* CH */
  59634. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59635. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59636. WOLFSSL_ERROR_WANT_READ);
  59637. /* consume CH */
  59638. test_ctx.s_len = 0;
  59639. /* inject SH */
  59640. XMEMCPY(test_ctx.c_buff, test_extra_alerts_wrong_cs_sh,
  59641. sizeof(test_extra_alerts_wrong_cs_sh));
  59642. test_ctx.c_len = sizeof(test_extra_alerts_wrong_cs_sh);
  59643. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59644. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59645. WOLFSSL_ERROR_WANT_READ);
  59646. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  59647. ExpectIntEQ(h.last_tx.code, handshake_failure);
  59648. ExpectIntEQ(h.last_tx.level, alert_fatal);
  59649. wolfSSL_free(ssl_c);
  59650. wolfSSL_CTX_free(ctx_c);
  59651. #endif
  59652. return EXPECT_RESULT();
  59653. }
  59654. #else
  59655. static int test_extra_alerts_wrong_cs(void)
  59656. {
  59657. return TEST_SKIPPED;
  59658. }
  59659. #endif
  59660. #if !defined(WOLFSSL_NO_TLS12) && defined(WOLFSSL_EXTRA_ALERTS) && \
  59661. defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_SP_MATH)
  59662. static void test_remove_msg(byte *msg, int tail_len, int *len, int msg_length)
  59663. {
  59664. tail_len -= msg_length;
  59665. XMEMMOVE(msg, msg + msg_length, tail_len);
  59666. *len = *len - msg_length;
  59667. }
  59668. static int test_remove_hs_msg_from_buffer(byte *buf, int *len, byte type,
  59669. byte *found)
  59670. {
  59671. const unsigned int _HANDSHAKE_HEADER_SZ = 4;
  59672. const unsigned int _RECORD_HEADER_SZ = 5;
  59673. const int _change_cipher_hs = 55;
  59674. const int _change_cipher = 20;
  59675. const int _handshake = 22;
  59676. unsigned int tail_len;
  59677. byte *idx, *curr;
  59678. word8 currType;
  59679. word16 rLength;
  59680. word32 hLength;
  59681. idx = buf;
  59682. tail_len = (unsigned int)*len;
  59683. *found = 0;
  59684. while (tail_len > _RECORD_HEADER_SZ) {
  59685. curr = idx;
  59686. currType = *idx;
  59687. ato16(idx + 3, &rLength);
  59688. idx += _RECORD_HEADER_SZ;
  59689. tail_len -= _RECORD_HEADER_SZ;
  59690. if (tail_len < rLength)
  59691. return -1;
  59692. if (type == _change_cipher_hs && currType == _change_cipher) {
  59693. if (rLength != 1)
  59694. return -1;
  59695. /* match */
  59696. test_remove_msg(curr, *len - (int)(curr - buf),
  59697. len, _RECORD_HEADER_SZ + 1);
  59698. *found = 1;
  59699. return 0;
  59700. }
  59701. if (currType != _handshake) {
  59702. idx += rLength;
  59703. tail_len -= rLength;
  59704. continue;
  59705. }
  59706. if (rLength < _HANDSHAKE_HEADER_SZ)
  59707. return -1;
  59708. currType = *idx;
  59709. ato24(idx+1, &hLength);
  59710. hLength += _HANDSHAKE_HEADER_SZ;
  59711. if (tail_len < hLength)
  59712. return -1;
  59713. if (currType != type) {
  59714. idx += hLength;
  59715. tail_len -= hLength;
  59716. continue;
  59717. }
  59718. /* match */
  59719. test_remove_msg(curr, *len - (int)(curr - buf), len,
  59720. hLength + _RECORD_HEADER_SZ);
  59721. *found = 1;
  59722. return 0;
  59723. }
  59724. /* not found */
  59725. return 0;
  59726. }
  59727. static int test_remove_hs_message(byte hs_message_type,
  59728. int extra_round, byte alert_type)
  59729. {
  59730. EXPECT_DECLS;
  59731. WOLFSSL_CTX *ctx_c = NULL;
  59732. WOLFSSL_CTX *ctx_s = NULL;
  59733. WOLFSSL *ssl_c = NULL;
  59734. WOLFSSL *ssl_s = NULL;
  59735. struct test_memio_ctx test_ctx;
  59736. WOLFSSL_ALERT_HISTORY h;
  59737. byte found = 0;
  59738. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59739. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59740. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  59741. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59742. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59743. WOLFSSL_ERROR_WANT_READ);
  59744. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  59745. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59746. WOLFSSL_ERROR_WANT_READ);
  59747. if (extra_round) {
  59748. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59749. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59750. WOLFSSL_ERROR_WANT_READ);
  59751. /* this will complete handshake from server side */
  59752. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  59753. }
  59754. ExpectIntEQ(test_remove_hs_msg_from_buffer(test_ctx.c_buff,
  59755. &test_ctx.c_len, hs_message_type, &found), 0);
  59756. if (!found) {
  59757. wolfSSL_free(ssl_c);
  59758. wolfSSL_CTX_free(ctx_c);
  59759. wolfSSL_free(ssl_s);
  59760. wolfSSL_CTX_free(ctx_s);
  59761. return TEST_SKIPPED;
  59762. }
  59763. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59764. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59765. WOLFSSL_ERROR_WANT_READ);
  59766. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  59767. ExpectTrue(alert_type == 0xff || h.last_tx.code == alert_type);
  59768. ExpectIntEQ(h.last_tx.level, alert_fatal);
  59769. wolfSSL_free(ssl_c);
  59770. wolfSSL_CTX_free(ctx_c);
  59771. wolfSSL_free(ssl_s);
  59772. wolfSSL_CTX_free(ctx_s);
  59773. return EXPECT_RESULT();
  59774. }
  59775. static int test_extra_alerts_skip_hs(void)
  59776. {
  59777. EXPECT_DECLS;
  59778. const byte _server_key_exchange = 12;
  59779. const byte _server_hello = 2;
  59780. const byte _certificate = 11;
  59781. /* server_hello */
  59782. ExpectIntNE(test_remove_hs_message(_server_hello, 0,
  59783. unexpected_message), TEST_FAIL);
  59784. ExpectIntNE(test_remove_hs_message(_certificate, 0,
  59785. 0xff), TEST_FAIL);
  59786. ExpectIntNE(test_remove_hs_message(_server_key_exchange, 0,
  59787. unexpected_message), TEST_FAIL);
  59788. return EXPECT_RESULT();
  59789. }
  59790. #else
  59791. static int test_extra_alerts_skip_hs(void)
  59792. {
  59793. return TEST_SKIPPED;
  59794. }
  59795. #endif
  59796. #if !defined(WOLFSSL_NO_TLS12) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  59797. && defined(WOLFSSL_EXTRA_ALERTS) && !defined(NO_PSK) && !defined(NO_DH)
  59798. static unsigned int test_server_psk_cb(WOLFSSL* ssl, const char* id,
  59799. unsigned char* key, unsigned int key_max_len)
  59800. {
  59801. (void)ssl;
  59802. (void)id;
  59803. (void)key_max_len;
  59804. /* zero means error */
  59805. key[0] = 0x10;
  59806. return 1;
  59807. }
  59808. static int test_extra_alerts_bad_psk(void)
  59809. {
  59810. EXPECT_DECLS;
  59811. WOLFSSL_CTX *ctx_c = NULL;
  59812. WOLFSSL_CTX *ctx_s = NULL;
  59813. WOLFSSL *ssl_c = NULL;
  59814. WOLFSSL *ssl_s = NULL;
  59815. struct test_memio_ctx test_ctx;
  59816. WOLFSSL_ALERT_HISTORY h;
  59817. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59818. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59819. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  59820. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_c, "DHE-PSK-AES128-GCM-SHA256"),
  59821. WOLFSSL_SUCCESS);
  59822. ExpectIntEQ(wolfSSL_set_cipher_list(ssl_s, "DHE-PSK-AES128-GCM-SHA256"),
  59823. WOLFSSL_SUCCESS);
  59824. wolfSSL_set_psk_server_callback(ssl_s, test_server_psk_cb);
  59825. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59826. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59827. WOLFSSL_ERROR_WANT_READ);
  59828. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  59829. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  59830. WOLFSSL_ERROR_WANT_READ);
  59831. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59832. ExpectIntNE(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59833. WOLFSSL_ERROR_WANT_READ);
  59834. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  59835. ExpectIntEQ(h.last_tx.code, handshake_failure);
  59836. ExpectIntEQ(h.last_tx.level, alert_fatal);
  59837. wolfSSL_free(ssl_c);
  59838. wolfSSL_CTX_free(ctx_c);
  59839. wolfSSL_free(ssl_s);
  59840. wolfSSL_CTX_free(ctx_s);
  59841. return EXPECT_RESULT();
  59842. }
  59843. #else
  59844. static int test_extra_alerts_bad_psk(void)
  59845. {
  59846. return TEST_SKIPPED;
  59847. }
  59848. #endif
  59849. #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  59850. && !defined(NO_PSK)
  59851. static unsigned int test_tls13_bad_psk_binder_client_cb(WOLFSSL* ssl,
  59852. const char* hint, char* identity, unsigned int id_max_len,
  59853. unsigned char* key, unsigned int key_max_len)
  59854. {
  59855. (void)ssl;
  59856. (void)hint;
  59857. (void)key_max_len;
  59858. /* see internal.h MAX_PSK_ID_LEN for PSK identity limit */
  59859. XSTRNCPY(identity, "Client_identity", id_max_len);
  59860. key[0] = 0x20;
  59861. return 1;
  59862. }
  59863. static unsigned int test_tls13_bad_psk_binder_server_cb(WOLFSSL* ssl,
  59864. const char* id, unsigned char* key, unsigned int key_max_len)
  59865. {
  59866. (void)ssl;
  59867. (void)id;
  59868. (void)key_max_len;
  59869. /* zero means error */
  59870. key[0] = 0x10;
  59871. return 1;
  59872. }
  59873. #endif
  59874. static int test_tls13_bad_psk_binder(void)
  59875. {
  59876. EXPECT_DECLS;
  59877. #if defined(WOLFSSL_TLS13) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES)\
  59878. && !defined(NO_PSK)
  59879. WOLFSSL_CTX *ctx_c = NULL;
  59880. WOLFSSL_CTX *ctx_s = NULL;
  59881. WOLFSSL *ssl_c = NULL;
  59882. WOLFSSL *ssl_s = NULL;
  59883. struct test_memio_ctx test_ctx;
  59884. WOLFSSL_ALERT_HISTORY h;
  59885. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  59886. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  59887. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  59888. wolfSSL_set_psk_client_callback(ssl_c, test_tls13_bad_psk_binder_client_cb);
  59889. wolfSSL_set_psk_server_callback(ssl_s, test_tls13_bad_psk_binder_server_cb);
  59890. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59891. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59892. WOLFSSL_ERROR_WANT_READ);
  59893. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  59894. ExpectIntEQ( wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  59895. BAD_BINDER);
  59896. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  59897. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  59898. FATAL_ERROR);
  59899. ExpectIntEQ(wolfSSL_get_alert_history(ssl_c, &h), WOLFSSL_SUCCESS);
  59900. ExpectIntEQ(h.last_rx.code, illegal_parameter);
  59901. ExpectIntEQ(h.last_rx.level, alert_fatal);
  59902. wolfSSL_free(ssl_c);
  59903. wolfSSL_CTX_free(ctx_c);
  59904. wolfSSL_free(ssl_s);
  59905. wolfSSL_CTX_free(ctx_s);
  59906. #endif
  59907. return EXPECT_RESULT();
  59908. }
  59909. #if defined(WOLFSSL_HARDEN_TLS) && !defined(WOLFSSL_NO_TLS12) && \
  59910. defined(HAVE_IO_TESTS_DEPENDENCIES)
  59911. static int test_harden_no_secure_renegotiation_io_cb(WOLFSSL *ssl, char *buf,
  59912. int sz, void *ctx)
  59913. {
  59914. static int sentServerHello = FALSE;
  59915. if (!sentServerHello) {
  59916. byte renegExt[] = { 0xFF, 0x01, 0x00, 0x01, 0x00 };
  59917. size_t i;
  59918. if (sz < (int)sizeof(renegExt))
  59919. return WOLFSSL_CBIO_ERR_GENERAL;
  59920. /* Remove SCR from ServerHello */
  59921. for (i = 0; i < sz - sizeof(renegExt); i++) {
  59922. if (XMEMCMP(buf + i, renegExt, sizeof(renegExt)) == 0) {
  59923. /* Found the extension. Change it to something unrecognized. */
  59924. buf[i+1] = 0x11;
  59925. break;
  59926. }
  59927. }
  59928. sentServerHello = TRUE;
  59929. }
  59930. return EmbedSend(ssl, buf, sz, ctx);
  59931. }
  59932. static void test_harden_no_secure_renegotiation_ssl_ready(WOLFSSL* ssl)
  59933. {
  59934. wolfSSL_SSLSetIOSend(ssl, test_harden_no_secure_renegotiation_io_cb);
  59935. }
  59936. static void test_harden_no_secure_renegotiation_on_cleanup(WOLFSSL* ssl)
  59937. {
  59938. WOLFSSL_ALERT_HISTORY h;
  59939. AssertIntEQ(wolfSSL_get_alert_history(ssl, &h), WOLFSSL_SUCCESS);
  59940. AssertIntEQ(h.last_rx.code, handshake_failure);
  59941. AssertIntEQ(h.last_rx.level, alert_fatal);
  59942. }
  59943. static int test_harden_no_secure_renegotiation(void)
  59944. {
  59945. EXPECT_DECLS;
  59946. callback_functions client_cbs, server_cbs;
  59947. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  59948. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  59949. client_cbs.method = wolfTLSv1_2_client_method;
  59950. server_cbs.method = wolfTLSv1_2_server_method;
  59951. server_cbs.ssl_ready = test_harden_no_secure_renegotiation_ssl_ready;
  59952. server_cbs.on_cleanup = test_harden_no_secure_renegotiation_on_cleanup;
  59953. test_wolfSSL_client_server_nofail(&client_cbs, &server_cbs);
  59954. ExpectIntEQ(client_cbs.return_code, TEST_FAIL);
  59955. ExpectIntEQ(client_cbs.last_err, SECURE_RENEGOTIATION_E);
  59956. ExpectIntEQ(server_cbs.return_code, TEST_FAIL);
  59957. ExpectTrue(server_cbs.last_err == SOCKET_ERROR_E ||
  59958. server_cbs.last_err == FATAL_ERROR);
  59959. return EXPECT_RESULT();
  59960. }
  59961. #else
  59962. static int test_harden_no_secure_renegotiation(void)
  59963. {
  59964. return TEST_SKIPPED;
  59965. }
  59966. #endif
  59967. #if defined(HAVE_OCSP) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  59968. static int test_override_alt_cert_chain_cert_cb(int preverify,
  59969. WOLFSSL_X509_STORE_CTX* store)
  59970. {
  59971. fprintf(stderr, "preverify: %d\n", preverify);
  59972. fprintf(stderr, "store->error: %d\n", store->error);
  59973. fprintf(stderr, "error reason: %s\n", wolfSSL_ERR_reason_error_string(store->error));
  59974. if (store->error == OCSP_INVALID_STATUS) {
  59975. fprintf(stderr, "Overriding OCSP error\n");
  59976. return 1;
  59977. }
  59978. #ifndef WOLFSSL_ALT_CERT_CHAINS
  59979. else if ((store->error == ASN_NO_SIGNER_E ||
  59980. store->error == ASN_SELF_SIGNED_E
  59981. #if defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) || \
  59982. defined(HAVE_WEBSERVER)
  59983. || store->error == WOLFSSL_X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY
  59984. #endif
  59985. ) && store->error_depth == store->totalCerts - 1) {
  59986. fprintf(stderr, "Overriding no signer error only for root cert\n");
  59987. return 1;
  59988. }
  59989. #endif
  59990. else
  59991. return preverify;
  59992. }
  59993. static int test_override_alt_cert_chain_ocsp_cb(void* ioCtx, const char* url,
  59994. int urlSz, unsigned char* request, int requestSz,
  59995. unsigned char** response)
  59996. {
  59997. (void)ioCtx;
  59998. (void)url;
  59999. (void)urlSz;
  60000. (void)request;
  60001. (void)requestSz;
  60002. (void)response;
  60003. return -1;
  60004. }
  60005. static int test_override_alt_cert_chain_client_ctx_ready(WOLFSSL_CTX* ctx)
  60006. {
  60007. EXPECT_DECLS;
  60008. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER,
  60009. test_override_alt_cert_chain_cert_cb);
  60010. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  60011. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  60012. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  60013. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  60014. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  60015. WOLFSSL_SUCCESS);
  60016. return EXPECT_RESULT();
  60017. }
  60018. static int test_override_alt_cert_chain_client_ctx_ready2(WOLFSSL_CTX* ctx)
  60019. {
  60020. EXPECT_DECLS;
  60021. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, NULL);
  60022. ExpectIntEQ(wolfSSL_CTX_EnableOCSP(ctx, WOLFSSL_OCSP_CHECKALL |
  60023. WOLFSSL_OCSP_URL_OVERRIDE), WOLFSSL_SUCCESS);
  60024. ExpectIntEQ(wolfSSL_CTX_SetOCSP_Cb(ctx,
  60025. test_override_alt_cert_chain_ocsp_cb, NULL, NULL), WOLFSSL_SUCCESS);
  60026. ExpectIntEQ(wolfSSL_CTX_SetOCSP_OverrideURL(ctx, "not a url"),
  60027. WOLFSSL_SUCCESS);
  60028. return EXPECT_RESULT();
  60029. }
  60030. static int test_override_alt_cert_chain_server_ctx_ready(WOLFSSL_CTX* ctx)
  60031. {
  60032. EXPECT_DECLS;
  60033. ExpectIntEQ(wolfSSL_CTX_use_certificate_chain_file(ctx,
  60034. "./certs/intermediate/server-chain-alt.pem"), WOLFSSL_SUCCESS);
  60035. return EXPECT_RESULT();
  60036. }
  60037. static int test_override_alt_cert_chain(void)
  60038. {
  60039. EXPECT_DECLS;
  60040. size_t i;
  60041. struct test_params {
  60042. ctx_cb client_ctx_cb;
  60043. ctx_cb server_ctx_cb;
  60044. int result;
  60045. } params[] = {
  60046. {test_override_alt_cert_chain_client_ctx_ready,
  60047. test_override_alt_cert_chain_server_ctx_ready, TEST_SUCCESS},
  60048. {test_override_alt_cert_chain_client_ctx_ready2,
  60049. test_override_alt_cert_chain_server_ctx_ready, TEST_FAIL},
  60050. };
  60051. for (i = 0; i < sizeof(params)/sizeof(*params); i++) {
  60052. test_ssl_cbf client_cbs, server_cbs;
  60053. XMEMSET(&client_cbs, 0, sizeof(client_cbs));
  60054. XMEMSET(&server_cbs, 0, sizeof(server_cbs));
  60055. fprintf(stderr, "test config: %d\n", (int)i);
  60056. client_cbs.ctx_ready = params[i].client_ctx_cb;
  60057. server_cbs.ctx_ready = params[i].server_ctx_cb;
  60058. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbs,
  60059. &server_cbs, NULL), params[i].result);
  60060. ExpectIntEQ(client_cbs.return_code, params[i].result);
  60061. ExpectIntEQ(server_cbs.return_code, params[i].result);
  60062. }
  60063. return EXPECT_RESULT();
  60064. }
  60065. #else
  60066. static int test_override_alt_cert_chain(void)
  60067. {
  60068. return TEST_SKIPPED;
  60069. }
  60070. #endif
  60071. #if defined(HAVE_RPK)
  60072. #define svrRpkCertFile "./certs/rpk/server-cert-rpk.der"
  60073. #define clntRpkCertFile "./certs/rpk/client-cert-rpk.der"
  60074. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  60075. static int MyRpkVerifyCb(int mode, WOLFSSL_X509_STORE_CTX* strctx)
  60076. {
  60077. int ret = WOLFSSL_SUCCESS;
  60078. (void)mode;
  60079. (void)strctx;
  60080. WOLFSSL_ENTER("MyRpkVerifyCb");
  60081. return ret;
  60082. }
  60083. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  60084. static WC_INLINE int test_rpk_memio_setup(
  60085. struct test_memio_ctx *ctx,
  60086. WOLFSSL_CTX **ctx_c,
  60087. WOLFSSL_CTX **ctx_s,
  60088. WOLFSSL **ssl_c,
  60089. WOLFSSL **ssl_s,
  60090. method_provider method_c,
  60091. method_provider method_s,
  60092. const char* certfile_c, int fmt_cc, /* client cert file path and format */
  60093. const char* certfile_s, int fmt_cs, /* server cert file path and format */
  60094. const char* pkey_c, int fmt_kc, /* client private key and format */
  60095. const char* pkey_s, int fmt_ks /* server private key and format */
  60096. )
  60097. {
  60098. int ret;
  60099. if (ctx_c != NULL && *ctx_c == NULL) {
  60100. *ctx_c = wolfSSL_CTX_new(method_c());
  60101. if (*ctx_c == NULL) {
  60102. return -1;
  60103. }
  60104. wolfSSL_CTX_set_verify(*ctx_c, WOLFSSL_VERIFY_PEER, NULL);
  60105. ret = wolfSSL_CTX_load_verify_locations(*ctx_c, caCertFile, 0);
  60106. if (ret != WOLFSSL_SUCCESS) {
  60107. return -1;
  60108. }
  60109. wolfSSL_SetIORecv(*ctx_c, test_memio_read_cb);
  60110. wolfSSL_SetIOSend(*ctx_c, test_memio_write_cb);
  60111. ret = wolfSSL_CTX_use_certificate_file(*ctx_c, certfile_c, fmt_cc);
  60112. if (ret != WOLFSSL_SUCCESS) {
  60113. return -1;
  60114. }
  60115. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_c, pkey_c, fmt_kc);
  60116. if (ret != WOLFSSL_SUCCESS) {
  60117. return -1;
  60118. }
  60119. }
  60120. if (ctx_s != NULL && *ctx_s == NULL) {
  60121. *ctx_s = wolfSSL_CTX_new(method_s());
  60122. if (*ctx_s == NULL) {
  60123. return -1;
  60124. }
  60125. wolfSSL_CTX_set_verify(*ctx_s, WOLFSSL_VERIFY_PEER, NULL);
  60126. ret = wolfSSL_CTX_load_verify_locations(*ctx_s, cliCertFile, 0);
  60127. if (ret != WOLFSSL_SUCCESS) {
  60128. return -1;
  60129. }
  60130. ret = wolfSSL_CTX_use_PrivateKey_file(*ctx_s, pkey_s, fmt_ks);
  60131. if (ret != WOLFSSL_SUCCESS) {
  60132. return -1;
  60133. }
  60134. ret = wolfSSL_CTX_use_certificate_file(*ctx_s, certfile_s, fmt_cs);
  60135. if (ret != WOLFSSL_SUCCESS) {
  60136. return -1;
  60137. }
  60138. wolfSSL_SetIORecv(*ctx_s, test_memio_read_cb);
  60139. wolfSSL_SetIOSend(*ctx_s, test_memio_write_cb);
  60140. if (ctx->s_ciphers != NULL) {
  60141. ret = wolfSSL_CTX_set_cipher_list(*ctx_s, ctx->s_ciphers);
  60142. if (ret != WOLFSSL_SUCCESS) {
  60143. return -1;
  60144. }
  60145. }
  60146. }
  60147. if (ctx_c != NULL && ssl_c != NULL) {
  60148. *ssl_c = wolfSSL_new(*ctx_c);
  60149. if (*ssl_c == NULL) {
  60150. return -1;
  60151. }
  60152. wolfSSL_SetIOWriteCtx(*ssl_c, ctx);
  60153. wolfSSL_SetIOReadCtx(*ssl_c, ctx);
  60154. }
  60155. if (ctx_s != NULL && ssl_s != NULL) {
  60156. *ssl_s = wolfSSL_new(*ctx_s);
  60157. if (*ssl_s == NULL) {
  60158. return -1;
  60159. }
  60160. wolfSSL_SetIOWriteCtx(*ssl_s, ctx);
  60161. wolfSSL_SetIOReadCtx(*ssl_s, ctx);
  60162. #if !defined(NO_DH)
  60163. SetDH(*ssl_s);
  60164. #endif
  60165. }
  60166. return 0;
  60167. }
  60168. #endif /* HAVE_RPK */
  60169. static int test_rpk_set_xxx_cert_type(void)
  60170. {
  60171. EXPECT_DECLS;
  60172. #if defined(HAVE_RPK)
  60173. char ctype[MAX_CLIENT_CERT_TYPE_CNT + 1]; /* prepare bigger buffer */
  60174. WOLFSSL_CTX* ctx = NULL;
  60175. WOLFSSL* ssl = NULL;
  60176. int tp;
  60177. ctx = wolfSSL_CTX_new(wolfTLSv1_3_client_method());
  60178. ExpectNotNull(ctx);
  60179. ssl = wolfSSL_new(ctx);
  60180. ExpectNotNull(ssl);
  60181. /*--------------------------------------------*/
  60182. /* tests for wolfSSL_CTX_set_client_cert_type */
  60183. /*--------------------------------------------*/
  60184. /* illegal parameter test caces */
  60185. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(NULL, ctype,
  60186. MAX_CLIENT_CERT_TYPE_CNT),
  60187. BAD_FUNC_ARG);
  60188. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  60189. sizeof(ctype)),
  60190. BAD_FUNC_ARG);
  60191. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  60192. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  60193. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  60194. MAX_CLIENT_CERT_TYPE_CNT),
  60195. BAD_FUNC_ARG);
  60196. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  60197. ctype[1] = 10; /* set unknown cert type */
  60198. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  60199. MAX_CLIENT_CERT_TYPE_CNT),
  60200. BAD_FUNC_ARG);
  60201. /* pass larger type count */
  60202. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  60203. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  60204. ctype[2] = 1; /* pass unacceptable type count */
  60205. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  60206. MAX_CLIENT_CERT_TYPE_CNT + 1),
  60207. BAD_FUNC_ARG);
  60208. /* should accept NULL for type buffer */
  60209. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, NULL,
  60210. MAX_CLIENT_CERT_TYPE_CNT),
  60211. WOLFSSL_SUCCESS);
  60212. /* should accept zero for type count */
  60213. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  60214. 0),
  60215. WOLFSSL_SUCCESS);
  60216. ExpectIntEQ(wolfSSL_CTX_set_client_cert_type(ctx, ctype,
  60217. MAX_CLIENT_CERT_TYPE_CNT),
  60218. WOLFSSL_SUCCESS);
  60219. /*--------------------------------------------*/
  60220. /* tests for wolfSSL_CTX_set_server_cert_type */
  60221. /*--------------------------------------------*/
  60222. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(NULL, ctype,
  60223. MAX_SERVER_CERT_TYPE_CNT),
  60224. BAD_FUNC_ARG);
  60225. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  60226. sizeof(ctype)),
  60227. BAD_FUNC_ARG);
  60228. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  60229. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  60230. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  60231. MAX_SERVER_CERT_TYPE_CNT),
  60232. BAD_FUNC_ARG);
  60233. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  60234. ctype[1] = 10; /* set unknown cert type */
  60235. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  60236. MAX_SERVER_CERT_TYPE_CNT),
  60237. BAD_FUNC_ARG);
  60238. /* pass larger type count */
  60239. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  60240. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  60241. ctype[2] = 1; /* pass unacceptable type count */
  60242. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  60243. MAX_SERVER_CERT_TYPE_CNT + 1),
  60244. BAD_FUNC_ARG);
  60245. /* should accept NULL for type buffer */
  60246. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, NULL,
  60247. MAX_SERVER_CERT_TYPE_CNT),
  60248. WOLFSSL_SUCCESS);
  60249. /* should accept zero for type count */
  60250. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  60251. 0),
  60252. WOLFSSL_SUCCESS);
  60253. ExpectIntEQ(wolfSSL_CTX_set_server_cert_type(ctx, ctype,
  60254. MAX_CLIENT_CERT_TYPE_CNT),
  60255. WOLFSSL_SUCCESS);
  60256. /*--------------------------------------------*/
  60257. /* tests for wolfSSL_set_client_cert_type */
  60258. /*--------------------------------------------*/
  60259. ExpectIntEQ(wolfSSL_set_client_cert_type(NULL, ctype,
  60260. MAX_CLIENT_CERT_TYPE_CNT),
  60261. BAD_FUNC_ARG);
  60262. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  60263. sizeof(ctype)),
  60264. BAD_FUNC_ARG);
  60265. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  60266. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  60267. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  60268. MAX_CLIENT_CERT_TYPE_CNT),
  60269. BAD_FUNC_ARG);
  60270. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  60271. ctype[1] = 10; /* set unknown cert type */
  60272. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  60273. MAX_CLIENT_CERT_TYPE_CNT),
  60274. BAD_FUNC_ARG);
  60275. /* pass larger type count */
  60276. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  60277. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  60278. ctype[2] = 1; /* pass unacceptable type count */
  60279. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  60280. MAX_CLIENT_CERT_TYPE_CNT + 1),
  60281. BAD_FUNC_ARG);
  60282. /* should accept NULL for type buffer */
  60283. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, NULL,
  60284. MAX_CLIENT_CERT_TYPE_CNT),
  60285. WOLFSSL_SUCCESS);
  60286. /* should accept zero for type count */
  60287. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  60288. 0),
  60289. WOLFSSL_SUCCESS);
  60290. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl, ctype,
  60291. MAX_CLIENT_CERT_TYPE_CNT),
  60292. WOLFSSL_SUCCESS);
  60293. /*--------------------------------------------*/
  60294. /* tests for wolfSSL_CTX_set_server_cert_type */
  60295. /*--------------------------------------------*/
  60296. ExpectIntEQ(wolfSSL_set_server_cert_type(NULL, ctype,
  60297. MAX_SERVER_CERT_TYPE_CNT),
  60298. BAD_FUNC_ARG);
  60299. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  60300. sizeof(ctype)),
  60301. BAD_FUNC_ARG);
  60302. ctype[0] = WOLFSSL_CERT_TYPE_RPK; /* set an identical cert type */
  60303. ctype[1] = WOLFSSL_CERT_TYPE_RPK;
  60304. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  60305. MAX_SERVER_CERT_TYPE_CNT),
  60306. BAD_FUNC_ARG);
  60307. ctype[0] = WOLFSSL_CERT_TYPE_X509;
  60308. ctype[1] = 10; /* set unknown cert type */
  60309. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  60310. MAX_SERVER_CERT_TYPE_CNT),
  60311. BAD_FUNC_ARG);
  60312. /* pass larger type count */
  60313. ctype[0] = WOLFSSL_CERT_TYPE_RPK;
  60314. ctype[1] = WOLFSSL_CERT_TYPE_X509;
  60315. ctype[2] = 1; /* pass unacceptable type count */
  60316. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  60317. MAX_SERVER_CERT_TYPE_CNT + 1),
  60318. BAD_FUNC_ARG);
  60319. /* should accept NULL for type buffer */
  60320. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, NULL,
  60321. MAX_SERVER_CERT_TYPE_CNT),
  60322. WOLFSSL_SUCCESS);
  60323. /* should accept zero for type count */
  60324. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  60325. 0),
  60326. WOLFSSL_SUCCESS);
  60327. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl, ctype,
  60328. MAX_SERVER_CERT_TYPE_CNT),
  60329. WOLFSSL_SUCCESS);
  60330. /*------------------------------------------------*/
  60331. /* tests for wolfSSL_get_negotiated_xxx_cert_type */
  60332. /*------------------------------------------------*/
  60333. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(NULL, &tp),
  60334. BAD_FUNC_ARG);
  60335. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl, NULL),
  60336. BAD_FUNC_ARG);
  60337. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(NULL, &tp),
  60338. BAD_FUNC_ARG);
  60339. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl, NULL),
  60340. BAD_FUNC_ARG);
  60341. /* clean up */
  60342. wolfSSL_free(ssl);
  60343. wolfSSL_CTX_free(ctx);
  60344. #endif
  60345. return EXPECT_RESULT();
  60346. }
  60347. static int test_tls13_rpk_handshake(void)
  60348. {
  60349. EXPECT_DECLS;
  60350. #if defined(HAVE_RPK)
  60351. int ret = 0;
  60352. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  60353. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  60354. struct test_memio_ctx test_ctx;
  60355. int err;
  60356. char certType_c[MAX_CLIENT_CERT_TYPE_CNT];
  60357. char certType_s[MAX_CLIENT_CERT_TYPE_CNT];
  60358. int typeCnt_c;
  60359. int typeCnt_s;
  60360. int tp;
  60361. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  60362. int isServer;
  60363. #endif
  60364. (void)err;
  60365. (void)typeCnt_c;
  60366. (void)typeCnt_s;
  60367. (void)certType_c;
  60368. (void)certType_s;
  60369. /* TLS1.2
  60370. * Both client and server load x509 cert and start handshaking.
  60371. * Check no negotiation occurred.
  60372. */
  60373. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60374. ExpectIntEQ(
  60375. test_rpk_memio_setup(
  60376. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60377. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  60378. cliCertFile, WOLFSSL_FILETYPE_PEM,
  60379. svrCertFile, WOLFSSL_FILETYPE_PEM,
  60380. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60381. svrKeyFile, WOLFSSL_FILETYPE_PEM)
  60382. , 0);
  60383. /* set client certificate type in client end */
  60384. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60385. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60386. typeCnt_c = 2;
  60387. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  60388. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  60389. typeCnt_s = 2;
  60390. /* both client and server do not call client/server_cert_type APIs,
  60391. * expecting default settings works and no negotiation performed.
  60392. */
  60393. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  60394. return TEST_FAIL;
  60395. /* confirm no negotiation occurred */
  60396. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60397. WOLFSSL_SUCCESS);
  60398. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60399. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60400. WOLFSSL_SUCCESS);
  60401. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60402. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60403. WOLFSSL_SUCCESS);
  60404. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60405. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60406. WOLFSSL_SUCCESS);
  60407. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60408. (void)typeCnt_c;
  60409. (void)typeCnt_s;
  60410. wolfSSL_free(ssl_c);
  60411. wolfSSL_CTX_free(ctx_c);
  60412. wolfSSL_free(ssl_s);
  60413. wolfSSL_CTX_free(ctx_s);
  60414. ssl_c = ssl_s = NULL;
  60415. ctx_c = ctx_s = NULL;
  60416. /* Both client and server load x509 cert and start handshaking.
  60417. * Check no negotiation occurred.
  60418. */
  60419. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60420. ExpectIntEQ(
  60421. test_rpk_memio_setup(
  60422. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60423. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60424. cliCertFile, WOLFSSL_FILETYPE_PEM,
  60425. svrCertFile, WOLFSSL_FILETYPE_PEM,
  60426. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60427. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60428. , 0);
  60429. /* set client certificate type in client end */
  60430. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60431. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60432. typeCnt_c = 2;
  60433. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  60434. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  60435. typeCnt_s = 2;
  60436. /* both client and server do not call client/server_cert_type APIs,
  60437. * expecting default settings works and no negotiation performed.
  60438. */
  60439. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  60440. return TEST_FAIL;
  60441. /* confirm no negotiation occurred */
  60442. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60443. WOLFSSL_SUCCESS);
  60444. ExpectIntEQ((int)tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60445. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60446. WOLFSSL_SUCCESS);
  60447. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60448. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60449. WOLFSSL_SUCCESS);
  60450. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60451. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60452. WOLFSSL_SUCCESS);
  60453. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60454. (void)typeCnt_c;
  60455. (void)typeCnt_s;
  60456. wolfSSL_free(ssl_c);
  60457. wolfSSL_CTX_free(ctx_c);
  60458. wolfSSL_free(ssl_s);
  60459. wolfSSL_CTX_free(ctx_s);
  60460. ssl_c = ssl_s = NULL;
  60461. ctx_c = ctx_s = NULL;
  60462. /* Both client and server load RPK cert and start handshaking.
  60463. * Confirm negotiated cert types match as expected.
  60464. */
  60465. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60466. ExpectIntEQ(
  60467. test_rpk_memio_setup(
  60468. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60469. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60470. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60471. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60472. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60473. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60474. , 0);
  60475. /* set client certificate type in client end */
  60476. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60477. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60478. typeCnt_c = 2;
  60479. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  60480. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  60481. typeCnt_s = 2;
  60482. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60483. WOLFSSL_SUCCESS);
  60484. /* set server certificate type in client end */
  60485. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60486. WOLFSSL_SUCCESS);
  60487. /* set client certificate type in server end */
  60488. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60489. WOLFSSL_SUCCESS);
  60490. /* set server certificate type in server end */
  60491. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60492. WOLFSSL_SUCCESS);
  60493. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  60494. return TEST_FAIL;
  60495. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60496. WOLFSSL_SUCCESS);
  60497. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60498. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60499. WOLFSSL_SUCCESS);
  60500. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60501. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60502. WOLFSSL_SUCCESS);
  60503. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60504. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60505. WOLFSSL_SUCCESS);
  60506. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60507. wolfSSL_free(ssl_c);
  60508. wolfSSL_CTX_free(ctx_c);
  60509. wolfSSL_free(ssl_s);
  60510. wolfSSL_CTX_free(ctx_s);
  60511. ssl_c = ssl_s = NULL;
  60512. ctx_c = ctx_s = NULL;
  60513. /* TLS1.2
  60514. * Both client and server load RPK cert and start handshaking.
  60515. * Confirm negotiated cert types match as expected.
  60516. */
  60517. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60518. ExpectIntEQ(
  60519. test_rpk_memio_setup(
  60520. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60521. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  60522. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60523. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60524. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60525. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60526. , 0);
  60527. /* set client certificate type in client end */
  60528. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60529. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60530. typeCnt_c = 2;
  60531. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  60532. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  60533. typeCnt_s = 2;
  60534. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60535. WOLFSSL_SUCCESS);
  60536. /* set server certificate type in client end */
  60537. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60538. WOLFSSL_SUCCESS);
  60539. /* set client certificate type in server end */
  60540. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60541. WOLFSSL_SUCCESS);
  60542. /* set server certificate type in server end */
  60543. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60544. WOLFSSL_SUCCESS);
  60545. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  60546. return TEST_FAIL;
  60547. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60548. WOLFSSL_SUCCESS);
  60549. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60550. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60551. WOLFSSL_SUCCESS);
  60552. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60553. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60554. WOLFSSL_SUCCESS);
  60555. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60556. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60557. WOLFSSL_SUCCESS);
  60558. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60559. wolfSSL_free(ssl_c);
  60560. wolfSSL_CTX_free(ctx_c);
  60561. wolfSSL_free(ssl_s);
  60562. wolfSSL_CTX_free(ctx_s);
  60563. ssl_c = ssl_s = NULL;
  60564. ctx_c = ctx_s = NULL;
  60565. /* Both client and server load x509 cert.
  60566. * Have client call set_client_cert_type with both RPK and x509.
  60567. * This doesn't makes client add client cert type extension to ClientHello,
  60568. * since it does not load RPK cert actually.
  60569. */
  60570. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60571. ExpectIntEQ(
  60572. test_rpk_memio_setup(
  60573. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60574. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60575. cliCertFile, WOLFSSL_FILETYPE_PEM,
  60576. svrCertFile, WOLFSSL_FILETYPE_PEM,
  60577. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60578. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60579. , 0);
  60580. /* set client certificate type in client end
  60581. *
  60582. * client indicates both RPK and x509 certs are available but loaded RPK
  60583. * cert only. It does not have client add client-cert-type extension in CH.
  60584. */
  60585. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60586. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60587. typeCnt_c = 2;
  60588. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60589. WOLFSSL_SUCCESS);
  60590. /* client indicates both RPK and x509 certs are acceptable */
  60591. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  60592. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  60593. typeCnt_s = 2;
  60594. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60595. WOLFSSL_SUCCESS);
  60596. /* server indicates both RPK and x509 certs are acceptable */
  60597. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60598. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60599. typeCnt_c = 2;
  60600. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60601. WOLFSSL_SUCCESS);
  60602. /* server should indicate only RPK cert is available */
  60603. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60604. certType_s[1] = -1;
  60605. typeCnt_s = 1;
  60606. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60607. WOLFSSL_SUCCESS);
  60608. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  60609. return TEST_FAIL;
  60610. /* Negotiation for client-cert-type should NOT happen. Therefore -1 should
  60611. * be returned as cert type.
  60612. */
  60613. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60614. WOLFSSL_SUCCESS);
  60615. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60616. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60617. WOLFSSL_SUCCESS);
  60618. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60619. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60620. WOLFSSL_SUCCESS);
  60621. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60622. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60623. WOLFSSL_SUCCESS);
  60624. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60625. wolfSSL_free(ssl_c);
  60626. wolfSSL_CTX_free(ctx_c);
  60627. wolfSSL_free(ssl_s);
  60628. wolfSSL_CTX_free(ctx_s);
  60629. ssl_c = ssl_s = NULL;
  60630. ctx_c = ctx_s = NULL;
  60631. /* Have client load RPK cert and have server load x509 cert.
  60632. * Check the negotiation result from both ends.
  60633. */
  60634. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60635. ExpectIntEQ(
  60636. test_rpk_memio_setup(
  60637. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60638. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60639. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60640. svrCertFile, WOLFSSL_FILETYPE_PEM,
  60641. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60642. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60643. , 0);
  60644. /* have client tell to use RPK cert */
  60645. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60646. certType_c[1] = -1;
  60647. typeCnt_c = 1;
  60648. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60649. WOLFSSL_SUCCESS);
  60650. /* have client tell to accept both RPK and x509 cert */
  60651. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60652. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  60653. typeCnt_s = 2;
  60654. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60655. WOLFSSL_SUCCESS);
  60656. /* have server accept to both RPK and x509 cert */
  60657. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  60658. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  60659. typeCnt_c = 2;
  60660. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60661. WOLFSSL_SUCCESS);
  60662. /* does not call wolfSSL_set_server_cert_type intentionally in sesrver
  60663. * end, expecting the default setting works.
  60664. */
  60665. if (test_memio_do_handshake(ssl_c, ssl_s, 10, NULL) != 0)
  60666. return TEST_FAIL;
  60667. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60668. WOLFSSL_SUCCESS);
  60669. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60670. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60671. WOLFSSL_SUCCESS);
  60672. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60673. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60674. WOLFSSL_SUCCESS);
  60675. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60676. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60677. WOLFSSL_SUCCESS);
  60678. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60679. wolfSSL_free(ssl_c);
  60680. wolfSSL_CTX_free(ctx_c);
  60681. wolfSSL_free(ssl_s);
  60682. wolfSSL_CTX_free(ctx_s);
  60683. ssl_c = ssl_s = NULL;
  60684. ctx_c = ctx_s = NULL;
  60685. /* Have both client and server load RPK cert, however, have server
  60686. * indicate its cert type x509.
  60687. * Client is expected to detect the cert type mismatch then to send alert
  60688. * with "unsupported_certificate".
  60689. */
  60690. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60691. ExpectIntEQ(
  60692. test_rpk_memio_setup(
  60693. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60694. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60695. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60696. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1, /* server sends RPK cert */
  60697. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60698. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60699. , 0);
  60700. /* have client tell to use RPK cert */
  60701. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60702. certType_c[1] = -1;
  60703. typeCnt_c = 1;
  60704. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60705. WOLFSSL_SUCCESS);
  60706. /* have client tell to accept both RPK and x509 cert */
  60707. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60708. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  60709. typeCnt_s = 2;
  60710. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60711. WOLFSSL_SUCCESS);
  60712. /* have server accept to both RPK and x509 cert */
  60713. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  60714. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  60715. typeCnt_c = 2;
  60716. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60717. WOLFSSL_SUCCESS);
  60718. /* have server tell to use x509 cert intentionally. This will bring
  60719. * certificate type mismatch in client side.
  60720. */
  60721. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60722. certType_s[1] = -1;
  60723. typeCnt_s = 1;
  60724. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60725. WOLFSSL_SUCCESS);
  60726. /* expect client detect cert type mismatch then send Alert */
  60727. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  60728. if (ret != -1)
  60729. return TEST_FAIL;
  60730. ExpectIntEQ(wolfSSL_get_error(ssl_c, ret), UNSUPPORTED_CERTIFICATE);
  60731. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60732. WOLFSSL_SUCCESS);
  60733. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60734. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60735. WOLFSSL_SUCCESS);
  60736. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60737. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60738. WOLFSSL_SUCCESS);
  60739. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60740. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60741. WOLFSSL_SUCCESS);
  60742. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60743. wolfSSL_free(ssl_c);
  60744. wolfSSL_CTX_free(ctx_c);
  60745. wolfSSL_free(ssl_s);
  60746. wolfSSL_CTX_free(ctx_s);
  60747. ssl_c = ssl_s = NULL;
  60748. ctx_c = ctx_s = NULL;
  60749. /* Have client load x509 cert and server load RPK cert,
  60750. * however, have client indicate its cert type RPK.
  60751. * Server is expected to detect the cert type mismatch then to send alert
  60752. * with "unsupported_certificate".
  60753. */
  60754. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60755. ExpectIntEQ(
  60756. test_rpk_memio_setup(
  60757. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60758. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60759. cliCertFile, WOLFSSL_FILETYPE_PEM,
  60760. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60761. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60762. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60763. , 0);
  60764. /* have client tell to use RPK cert intentionally */
  60765. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60766. certType_c[1] = -1;
  60767. typeCnt_c = 1;
  60768. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60769. WOLFSSL_SUCCESS);
  60770. /* have client tell to accept both RPK and x509 cert */
  60771. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60772. certType_s[1] = WOLFSSL_CERT_TYPE_RPK;
  60773. typeCnt_s = 2;
  60774. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60775. WOLFSSL_SUCCESS);
  60776. /* have server accept to both RPK and x509 cert */
  60777. certType_c[0] = WOLFSSL_CERT_TYPE_X509;
  60778. certType_c[1] = WOLFSSL_CERT_TYPE_RPK;
  60779. typeCnt_c = 2;
  60780. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60781. WOLFSSL_SUCCESS);
  60782. /* have server tell to use x509 cert intentionally. This will bring
  60783. * certificate type mismatch in client side.
  60784. */
  60785. certType_s[0] = WOLFSSL_CERT_TYPE_X509;
  60786. certType_s[1] = -1;
  60787. typeCnt_s = 1;
  60788. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60789. WOLFSSL_SUCCESS);
  60790. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  60791. /* expect server detect cert type mismatch then send Alert */
  60792. ExpectIntNE(ret, 0);
  60793. err = wolfSSL_get_error(ssl_c, ret);
  60794. ExpectIntEQ(err, UNSUPPORTED_CERTIFICATE);
  60795. /* client did not load RPK cert actually, so negotiation did not happen */
  60796. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60797. WOLFSSL_SUCCESS);
  60798. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60799. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60800. WOLFSSL_SUCCESS);
  60801. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60802. /* client did not load RPK cert actually, so negotiation did not happen */
  60803. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60804. WOLFSSL_SUCCESS);
  60805. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_UNKNOWN);
  60806. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60807. WOLFSSL_SUCCESS);
  60808. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_X509);
  60809. wolfSSL_free(ssl_c);
  60810. wolfSSL_CTX_free(ctx_c);
  60811. wolfSSL_free(ssl_s);
  60812. wolfSSL_CTX_free(ctx_s);
  60813. ssl_c = ssl_s = NULL;
  60814. ctx_c = ctx_s = NULL;
  60815. #if defined(WOLFSSL_ALWAYS_VERIFY_CB)
  60816. /* Both client and server load RPK cert and set certificate verify
  60817. * callbacks then start handshaking.
  60818. * Confirm both side can refer the peer's cert.
  60819. */
  60820. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60821. ExpectIntEQ(
  60822. test_rpk_memio_setup(
  60823. &test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60824. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  60825. clntRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60826. svrRpkCertFile, WOLFSSL_FILETYPE_ASN1,
  60827. cliKeyFile, WOLFSSL_FILETYPE_PEM,
  60828. svrKeyFile, WOLFSSL_FILETYPE_PEM )
  60829. , 0);
  60830. /* set client certificate type in client end */
  60831. certType_c[0] = WOLFSSL_CERT_TYPE_RPK;
  60832. certType_c[1] = WOLFSSL_CERT_TYPE_X509;
  60833. typeCnt_c = 2;
  60834. certType_s[0] = WOLFSSL_CERT_TYPE_RPK;
  60835. certType_s[1] = WOLFSSL_CERT_TYPE_X509;
  60836. typeCnt_s = 2;
  60837. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_c, certType_c, typeCnt_c),
  60838. WOLFSSL_SUCCESS);
  60839. /* set server certificate type in client end */
  60840. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_c, certType_s, typeCnt_s),
  60841. WOLFSSL_SUCCESS);
  60842. /* set client certificate type in server end */
  60843. ExpectIntEQ(wolfSSL_set_client_cert_type(ssl_s, certType_c, typeCnt_c),
  60844. WOLFSSL_SUCCESS);
  60845. /* set server certificate type in server end */
  60846. ExpectIntEQ(wolfSSL_set_server_cert_type(ssl_s, certType_s, typeCnt_s),
  60847. WOLFSSL_SUCCESS);
  60848. /* set certificate verify callback to both client and server */
  60849. isServer = 0;
  60850. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  60851. wolfSSL_set_verify(ssl_c, SSL_VERIFY_PEER, MyRpkVerifyCb);
  60852. isServer = 1;
  60853. wolfSSL_SetCertCbCtx(ssl_c, &isServer);
  60854. wolfSSL_set_verify(ssl_s, SSL_VERIFY_PEER, MyRpkVerifyCb);
  60855. ret = test_memio_do_handshake(ssl_c, ssl_s, 10, NULL);
  60856. if (ret != 0)
  60857. return TEST_FAIL;
  60858. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_c, &tp),
  60859. WOLFSSL_SUCCESS);
  60860. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60861. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_c, &tp),
  60862. WOLFSSL_SUCCESS);
  60863. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60864. ExpectIntEQ(wolfSSL_get_negotiated_client_cert_type(ssl_s, &tp),
  60865. WOLFSSL_SUCCESS);
  60866. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60867. ExpectIntEQ(wolfSSL_get_negotiated_server_cert_type(ssl_s, &tp),
  60868. WOLFSSL_SUCCESS);
  60869. ExpectIntEQ(tp, WOLFSSL_CERT_TYPE_RPK);
  60870. wolfSSL_free(ssl_c);
  60871. wolfSSL_CTX_free(ctx_c);
  60872. wolfSSL_free(ssl_s);
  60873. wolfSSL_CTX_free(ctx_s);
  60874. ssl_c = ssl_s = NULL;
  60875. ctx_c = ctx_s = NULL;
  60876. #endif /* WOLFSSL_ALWAYS_VERIFY_CB */
  60877. #endif /* HAVE_RPK */
  60878. return EXPECT_RESULT();
  60879. }
  60880. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  60881. static int test_dtls13_bad_epoch_ch(void)
  60882. {
  60883. EXPECT_DECLS;
  60884. WOLFSSL_CTX *ctx_c = NULL;
  60885. WOLFSSL_CTX *ctx_s = NULL;
  60886. WOLFSSL *ssl_c = NULL;
  60887. WOLFSSL *ssl_s = NULL;
  60888. struct test_memio_ctx test_ctx;
  60889. const int EPOCH_OFF = 3;
  60890. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  60891. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  60892. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  60893. /* disable hrr cookie so we can later check msgsReceived.got_client_hello
  60894. * with just one message */
  60895. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  60896. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  60897. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  60898. WOLFSSL_ERROR_WANT_READ);
  60899. ExpectIntGE(test_ctx.s_len, EPOCH_OFF + 2);
  60900. /* first CH should use epoch 0x0 */
  60901. ExpectTrue((test_ctx.s_buff[EPOCH_OFF] == 0x0) &&
  60902. (test_ctx.s_buff[EPOCH_OFF + 1] == 0x0));
  60903. /* change epoch to 2 */
  60904. test_ctx.s_buff[EPOCH_OFF + 1] = 0x2;
  60905. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  60906. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  60907. WOLFSSL_ERROR_WANT_READ);
  60908. ExpectIntNE(ssl_s->msgsReceived.got_client_hello, 1);
  60909. /* resend the CH */
  60910. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  60911. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  60912. wolfSSL_free(ssl_c);
  60913. wolfSSL_CTX_free(ctx_c);
  60914. wolfSSL_free(ssl_s);
  60915. wolfSSL_CTX_free(ctx_s);
  60916. return EXPECT_RESULT();
  60917. }
  60918. #else
  60919. static int test_dtls13_bad_epoch_ch(void)
  60920. {
  60921. return TEST_SKIPPED;
  60922. }
  60923. #endif
  60924. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && !defined(NO_SESSION_CACHE)
  60925. static int test_short_session_id_ssl_ready(WOLFSSL* ssl)
  60926. {
  60927. EXPECT_DECLS;
  60928. WOLFSSL_SESSION *sess = NULL;
  60929. /* Setup the session to avoid errors */
  60930. ssl->session->timeout = -1;
  60931. ssl->session->side = WOLFSSL_CLIENT_END;
  60932. #if defined(SESSION_CERTS) || (defined(WOLFSSL_TLS13) && \
  60933. defined(HAVE_SESSION_TICKET))
  60934. ssl->session->version = ssl->version;
  60935. #endif
  60936. /* Force a short session ID to be sent */
  60937. ssl->session->sessionIDSz = 4;
  60938. #ifndef NO_SESSION_CACHE_REF
  60939. /* Allow the client cache to be used */
  60940. ssl->session->idLen = 4;
  60941. #endif
  60942. ssl->session->isSetup = 1;
  60943. ExpectNotNull(sess = wolfSSL_get_session(ssl));
  60944. ExpectIntEQ(wolfSSL_set_session(ssl, sess), WOLFSSL_SUCCESS);
  60945. return EXPECT_RESULT();
  60946. }
  60947. static int test_short_session_id(void)
  60948. {
  60949. EXPECT_DECLS;
  60950. test_ssl_cbf client_cbf;
  60951. test_ssl_cbf server_cbf;
  60952. size_t i;
  60953. struct {
  60954. method_provider client_meth;
  60955. method_provider server_meth;
  60956. const char* tls_version;
  60957. } params[] = {
  60958. #if defined(WOLFSSL_TLS13) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB) && \
  60959. defined(HAVE_SESSION_TICKET) && defined(WOLFSSL_TICKET_HAVE_ID) && \
  60960. !defined(WOLFSSL_TLS13_MIDDLEBOX_COMPAT)
  60961. /* With WOLFSSL_TLS13_MIDDLEBOX_COMPAT a short ID will result in an error */
  60962. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLSv1_3" },
  60963. #ifdef WOLFSSL_DTLS13
  60964. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method, "DTLSv1_3" },
  60965. #endif
  60966. #endif
  60967. #ifndef WOLFSSL_NO_TLS12
  60968. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLSv1_2" },
  60969. #ifdef WOLFSSL_DTLS
  60970. { wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method, "DTLSv1_2" },
  60971. #endif
  60972. #endif
  60973. #if !defined(NO_OLD_TLS) && ((!defined(NO_AES) && !defined(NO_AES_CBC)) || \
  60974. !defined(NO_DES3))
  60975. { wolfTLSv1_1_client_method, wolfTLSv1_1_server_method, "TLSv1_1" },
  60976. #ifdef WOLFSSL_DTLS
  60977. { wolfDTLSv1_client_method, wolfDTLSv1_server_method, "DTLSv1_0" },
  60978. #endif
  60979. #endif
  60980. };
  60981. fprintf(stderr, "\n");
  60982. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  60983. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  60984. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  60985. fprintf(stderr, "\tTesting short ID with %s\n", params[i].tls_version);
  60986. client_cbf.ssl_ready = test_short_session_id_ssl_ready;
  60987. client_cbf.method = params[i].client_meth;
  60988. server_cbf.method = params[i].server_meth;
  60989. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  60990. &server_cbf, NULL), TEST_SUCCESS);
  60991. }
  60992. return EXPECT_RESULT();
  60993. }
  60994. #else
  60995. static int test_short_session_id(void)
  60996. {
  60997. return TEST_SKIPPED;
  60998. }
  60999. #endif
  61000. #if defined(HAVE_NULL_CIPHER) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) \
  61001. && defined(WOLFSSL_DTLS13)
  61002. static byte* test_find_string(const char *string,
  61003. byte *buf, int buf_size)
  61004. {
  61005. int string_size, i;
  61006. string_size = (int)XSTRLEN(string);
  61007. for (i = 0; i < buf_size - string_size - 1; i++) {
  61008. if (XSTRCMP((char*)&buf[i], string) == 0)
  61009. return &buf[i];
  61010. }
  61011. return NULL;
  61012. }
  61013. static int test_wolfSSL_dtls13_null_cipher(void)
  61014. {
  61015. EXPECT_DECLS;
  61016. WOLFSSL_CTX *ctx_c = NULL;
  61017. WOLFSSL_CTX *ctx_s = NULL;
  61018. WOLFSSL *ssl_c = NULL;
  61019. WOLFSSL *ssl_s = NULL;
  61020. struct test_memio_ctx test_ctx;
  61021. const char *test_str = "test";
  61022. int test_str_size;
  61023. byte buf[255], *ptr = NULL;
  61024. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61025. test_ctx.c_ciphers = test_ctx.s_ciphers = "TLS13-SHA256-SHA256";
  61026. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61027. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  61028. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61029. test_str_size = XSTRLEN("test") + 1;
  61030. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  61031. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  61032. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  61033. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  61034. /* check that the packet was sent cleartext */
  61035. ExpectNotNull(ptr = test_find_string(test_str, test_ctx.s_buff,
  61036. test_ctx.s_len));
  61037. if (ptr != NULL) {
  61038. /* modify the message */
  61039. *ptr = 'H';
  61040. /* bad messages should be ignored in DTLS */
  61041. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), -1);
  61042. ExpectIntEQ(ssl_s->error, WANT_READ);
  61043. }
  61044. wolfSSL_free(ssl_c);
  61045. wolfSSL_free(ssl_s);
  61046. wolfSSL_CTX_free(ctx_c);
  61047. wolfSSL_CTX_free(ctx_s);
  61048. return TEST_SUCCESS;
  61049. }
  61050. #else
  61051. static int test_wolfSSL_dtls13_null_cipher(void)
  61052. {
  61053. return TEST_SKIPPED;
  61054. }
  61055. #endif
  61056. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  61057. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  61058. !defined(SINGLE_THREADED) && !defined(NO_RSA)
  61059. static int test_dtls_msg_get_connected_port(int fd, word16 *port)
  61060. {
  61061. SOCKADDR_S peer;
  61062. XSOCKLENT len;
  61063. int ret;
  61064. XMEMSET((byte*)&peer, 0, sizeof(peer));
  61065. len = sizeof(peer);
  61066. ret = getpeername(fd, (SOCKADDR*)&peer, &len);
  61067. if (ret != 0 || len > (XSOCKLENT)sizeof(peer))
  61068. return -1;
  61069. switch (peer.ss_family) {
  61070. #ifdef WOLFSSL_IPV6
  61071. case WOLFSSL_IP6: {
  61072. *port = ntohs(((SOCKADDR_IN6*)&peer)->sin6_port);
  61073. break;
  61074. }
  61075. #endif /* WOLFSSL_IPV6 */
  61076. case WOLFSSL_IP4:
  61077. *port = ntohs(((SOCKADDR_IN*)&peer)->sin_port);
  61078. break;
  61079. default:
  61080. return -1;
  61081. }
  61082. return 0;
  61083. }
  61084. static int test_dtls_msg_from_other_peer_cb(WOLFSSL_CTX *ctx, WOLFSSL *ssl)
  61085. {
  61086. char buf[1] = {'t'};
  61087. SOCKADDR_IN_T addr;
  61088. int sock_fd;
  61089. word16 port;
  61090. int err;
  61091. (void)ssl;
  61092. (void)ctx;
  61093. if (ssl == NULL)
  61094. return -1;
  61095. err = test_dtls_msg_get_connected_port(wolfSSL_get_fd(ssl), &port);
  61096. if (err != 0)
  61097. return -1;
  61098. sock_fd = socket(AF_INET_V, SOCK_DGRAM, 0);
  61099. if (sock_fd == -1)
  61100. return -1;
  61101. build_addr(&addr, wolfSSLIP, port, 1, 0);
  61102. /* send a packet to the server. Being another socket, the kernel will ensure
  61103. * the source port will be different. */
  61104. err = (int)sendto(sock_fd, buf, sizeof(buf), 0, (SOCKADDR*)&addr,
  61105. sizeof(addr));
  61106. close(sock_fd);
  61107. if (err == -1)
  61108. return -1;
  61109. return 0;
  61110. }
  61111. /* setup a SSL session but just after the handshake send a packet to the server
  61112. * with a source address different than the one of the connected client. The I/O
  61113. * callback EmbedRecvFrom should just ignore the packet. Sending of the packet
  61114. * is done in test_dtls_msg_from_other_peer_cb */
  61115. static int test_dtls_msg_from_other_peer(void)
  61116. {
  61117. EXPECT_DECLS;
  61118. callback_functions client_cbs;
  61119. callback_functions server_cbs;
  61120. XMEMSET((byte*)&client_cbs, 0, sizeof(client_cbs));
  61121. XMEMSET((byte*)&server_cbs, 0, sizeof(server_cbs));
  61122. client_cbs.method = wolfDTLSv1_2_client_method;
  61123. server_cbs.method = wolfDTLSv1_2_server_method;
  61124. client_cbs.doUdp = 1;
  61125. server_cbs.doUdp = 1;
  61126. test_wolfSSL_client_server_nofail_ex(&client_cbs, &server_cbs,
  61127. test_dtls_msg_from_other_peer_cb);
  61128. ExpectIntEQ(client_cbs.return_code, WOLFSSL_SUCCESS);
  61129. ExpectIntEQ(server_cbs.return_code, WOLFSSL_SUCCESS);
  61130. return EXPECT_RESULT();
  61131. }
  61132. #else
  61133. static int test_dtls_msg_from_other_peer(void)
  61134. {
  61135. return TEST_SKIPPED;
  61136. }
  61137. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  61138. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  61139. * !defined(SINGLE_THREADED) && !defined(NO_RSA) */
  61140. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_IPV6) && \
  61141. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  61142. defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  61143. static int test_dtls_ipv6_check(void)
  61144. {
  61145. EXPECT_DECLS;
  61146. WOLFSSL_CTX *ctx_c = NULL;
  61147. WOLFSSL_CTX *ctx_s = NULL;
  61148. WOLFSSL *ssl_c = NULL;
  61149. WOLFSSL *ssl_s = NULL;
  61150. SOCKADDR_IN fake_addr6;
  61151. int sockfd = -1;
  61152. ExpectNotNull(ctx_c = wolfSSL_CTX_new(wolfDTLSv1_2_client_method()));
  61153. ExpectNotNull(ssl_c = wolfSSL_new(ctx_c));
  61154. ExpectNotNull(ctx_s = wolfSSL_CTX_new(wolfDTLSv1_2_server_method()));
  61155. ExpectIntEQ(wolfSSL_CTX_use_PrivateKey_file(ctx_s, svrKeyFile,
  61156. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61157. ExpectIntEQ(wolfSSL_CTX_use_certificate_file(ctx_s, svrCertFile,
  61158. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  61159. ExpectNotNull(ssl_s = wolfSSL_new(ctx_s));
  61160. XMEMSET((byte*)&fake_addr6, 0, sizeof(fake_addr6));
  61161. /* mimic a sockaddr_in6 struct, this way we can't test without
  61162. * WOLFSSL_IPV6 */
  61163. fake_addr6.sin_family = WOLFSSL_IP6;
  61164. ExpectIntNE(sockfd = socket(AF_INET, SOCK_DGRAM, 0), -1);
  61165. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  61166. /* can't return error here, as the peer is opaque for wolfssl library at
  61167. * this point */
  61168. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_c, &fake_addr6, sizeof(fake_addr6)),
  61169. WOLFSSL_SUCCESS);
  61170. ExpectIntNE(fcntl(sockfd, F_SETFL, O_NONBLOCK), -1);
  61171. wolfSSL_dtls_set_using_nonblock(ssl_c, 1);
  61172. ExpectIntNE(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  61173. ExpectIntEQ(ssl_c->error, SOCKET_ERROR_E);
  61174. ExpectIntEQ(wolfSSL_dtls_set_peer(ssl_s, &fake_addr6, sizeof(fake_addr6)),
  61175. WOLFSSL_SUCCESS);
  61176. /* reuse the socket */
  61177. ExpectIntEQ(wolfSSL_set_fd(ssl_c, sockfd), WOLFSSL_SUCCESS);
  61178. wolfSSL_dtls_set_using_nonblock(ssl_s, 1);
  61179. ExpectIntNE(wolfSSL_accept(ssl_s), WOLFSSL_SUCCESS);
  61180. ExpectIntEQ(ssl_s->error, SOCKET_ERROR_E);
  61181. if (sockfd != -1)
  61182. close(sockfd);
  61183. wolfSSL_free(ssl_c);
  61184. wolfSSL_CTX_free(ctx_c);
  61185. wolfSSL_free(ssl_s);
  61186. wolfSSL_CTX_free(ctx_s);
  61187. return EXPECT_RESULT();
  61188. }
  61189. #else
  61190. static int test_dtls_ipv6_check(void)
  61191. {
  61192. return TEST_SKIPPED;
  61193. }
  61194. #endif
  61195. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  61196. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  61197. static WOLFSSL_SESSION* test_wolfSSL_SCR_after_resumption_session = NULL;
  61198. static void test_wolfSSL_SCR_after_resumption_ctx_ready(WOLFSSL_CTX* ctx)
  61199. {
  61200. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  61201. }
  61202. static void test_wolfSSL_SCR_after_resumption_on_result(WOLFSSL* ssl)
  61203. {
  61204. if (test_wolfSSL_SCR_after_resumption_session == NULL) {
  61205. test_wolfSSL_SCR_after_resumption_session = wolfSSL_get1_session(ssl);
  61206. AssertNotNull(test_wolfSSL_SCR_after_resumption_session);
  61207. }
  61208. else {
  61209. char testMsg[] = "Message after SCR";
  61210. char msgBuf[sizeof(testMsg)];
  61211. int ret;
  61212. if (!wolfSSL_is_server(ssl)) {
  61213. AssertIntEQ(WOLFSSL_SUCCESS,
  61214. wolfSSL_set_session(ssl,
  61215. test_wolfSSL_SCR_after_resumption_session));
  61216. }
  61217. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  61218. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  61219. sizeof(testMsg));
  61220. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  61221. if (ret != sizeof(msgBuf)) /* Possibly APP_DATA_READY error. Retry. */
  61222. ret = wolfSSL_read(ssl, msgBuf, sizeof(msgBuf));
  61223. AssertIntEQ(ret, sizeof(msgBuf));
  61224. }
  61225. }
  61226. static void test_wolfSSL_SCR_after_resumption_ssl_ready(WOLFSSL* ssl)
  61227. {
  61228. AssertIntEQ(WOLFSSL_SUCCESS,
  61229. wolfSSL_set_session(ssl, test_wolfSSL_SCR_after_resumption_session));
  61230. }
  61231. static int test_wolfSSL_SCR_after_resumption(void)
  61232. {
  61233. EXPECT_DECLS;
  61234. callback_functions func_cb_client;
  61235. callback_functions func_cb_server;
  61236. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  61237. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  61238. func_cb_client.method = wolfTLSv1_2_client_method;
  61239. func_cb_client.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  61240. func_cb_client.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  61241. func_cb_server.method = wolfTLSv1_2_server_method;
  61242. func_cb_server.ctx_ready = test_wolfSSL_SCR_after_resumption_ctx_ready;
  61243. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61244. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61245. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61246. func_cb_client.ssl_ready = test_wolfSSL_SCR_after_resumption_ssl_ready;
  61247. func_cb_server.on_result = test_wolfSSL_SCR_after_resumption_on_result;
  61248. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61249. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61250. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61251. wolfSSL_SESSION_free(test_wolfSSL_SCR_after_resumption_session);
  61252. return EXPECT_RESULT();
  61253. }
  61254. #else
  61255. static int test_wolfSSL_SCR_after_resumption(void)
  61256. {
  61257. return TEST_SKIPPED;
  61258. }
  61259. #endif
  61260. static int test_wolfSSL_configure_args(void)
  61261. {
  61262. EXPECT_DECLS;
  61263. #if defined(LIBWOLFSSL_CONFIGURE_ARGS) && defined(HAVE_WC_INTROSPECTION)
  61264. ExpectNotNull(wolfSSL_configure_args());
  61265. #endif
  61266. return EXPECT_RESULT();
  61267. }
  61268. static int test_dtls_no_extensions(void)
  61269. {
  61270. EXPECT_DECLS;
  61271. #if defined(WOLFSSL_DTLS) && defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  61272. !defined(WOLFSSL_NO_TLS12)
  61273. WOLFSSL *ssl_s = NULL;
  61274. WOLFSSL_CTX *ctx_s = NULL;
  61275. struct test_memio_ctx test_ctx;
  61276. const byte chNoExtensions[] = {
  61277. /* Handshake type */
  61278. 0x16,
  61279. /* Version */
  61280. 0xfe, 0xff,
  61281. /* Epoch */
  61282. 0x00, 0x00,
  61283. /* Seq number */
  61284. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  61285. /* Length */
  61286. 0x00, 0x40,
  61287. /* CH type */
  61288. 0x01,
  61289. /* Length */
  61290. 0x00, 0x00, 0x34,
  61291. /* Msg Seq */
  61292. 0x00, 0x00,
  61293. /* Frag offset */
  61294. 0x00, 0x00, 0x00,
  61295. /* Frag length */
  61296. 0x00, 0x00, 0x34,
  61297. /* Version */
  61298. 0xfe, 0xff,
  61299. /* Random */
  61300. 0x62, 0xfe, 0xbc, 0xfe, 0x2b, 0xfe, 0x3f, 0xeb, 0x03, 0xc4, 0xea, 0x37,
  61301. 0xe7, 0x47, 0x7e, 0x8a, 0xd9, 0xbf, 0x77, 0x0f, 0x6c, 0xb6, 0x77, 0x0b,
  61302. 0x03, 0x3f, 0x82, 0x2b, 0x21, 0x64, 0x57, 0x1d,
  61303. /* Session Length */
  61304. 0x00,
  61305. /* Cookie Length */
  61306. 0x00,
  61307. /* CS Length */
  61308. 0x00, 0x0c,
  61309. /* CS */
  61310. 0xc0, 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x39, 0x00, 0x33,
  61311. /* Comp Meths Length */
  61312. 0x01,
  61313. /* Comp Meths */
  61314. 0x00
  61315. /* And finally... no extensions */
  61316. };
  61317. int i;
  61318. #ifdef OPENSSL_EXTRA
  61319. int repeats = 2;
  61320. #else
  61321. int repeats = 1;
  61322. #endif
  61323. for (i = 0; i < repeats; i++) {
  61324. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61325. ssl_s = NULL;
  61326. ctx_s = NULL;
  61327. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  61328. NULL, wolfDTLS_server_method), 0);
  61329. XMEMCPY(test_ctx.s_buff, chNoExtensions, sizeof(chNoExtensions));
  61330. test_ctx.s_len = sizeof(chNoExtensions);
  61331. #ifdef OPENSSL_EXTRA
  61332. if (i > 0) {
  61333. ExpectIntEQ(wolfSSL_set_max_proto_version(ssl_s, DTLS1_2_VERSION),
  61334. WOLFSSL_SUCCESS);
  61335. }
  61336. #endif
  61337. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  61338. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61339. /* Expecting a handshake msg. Either HVR or SH. */
  61340. ExpectIntGT(test_ctx.c_len, 0);
  61341. ExpectIntEQ(test_ctx.c_buff[0], 0x16);
  61342. wolfSSL_free(ssl_s);
  61343. wolfSSL_CTX_free(ctx_s);
  61344. }
  61345. #endif
  61346. return EXPECT_RESULT();
  61347. }
  61348. static int test_TLSX_CA_NAMES_bad_extension(void)
  61349. {
  61350. EXPECT_DECLS;
  61351. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  61352. !defined(NO_CERTS) && !defined(WOLFSSL_NO_CA_NAMES) && \
  61353. defined(OPENSSL_EXTRA) && defined(WOLFSSL_SHA384) && \
  61354. defined(HAVE_NULL_CIPHER)
  61355. /* This test should only fail (with BUFFER_ERROR) when we actually try to
  61356. * parse the CA Names extension. Otherwise it will return other non-related
  61357. * errors. If CA Names will be parsed in more configurations, that should
  61358. * be reflected in the macro guard above. */
  61359. WOLFSSL *ssl_c = NULL;
  61360. WOLFSSL_CTX *ctx_c = NULL;
  61361. struct test_memio_ctx test_ctx;
  61362. /* HRR + SH using TLS_DHE_PSK_WITH_NULL_SHA384 */
  61363. const byte shBadCaNamesExt[] = {
  61364. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  61365. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  61366. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  61367. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  61368. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  61369. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  61370. 0x5c, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x03, 0xcf, 0x21, 0xad, 0x74,
  61371. 0x00, 0x00, 0x83, 0x3f, 0x3b, 0x80, 0x01, 0xac, 0x65, 0x8c, 0x19, 0x2a,
  61372. 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x02, 0x00, 0x9e, 0x09, 0x1c, 0xe8,
  61373. 0xa8, 0x09, 0x9c, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  61374. 0x03, 0x3f, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x13, 0x05,
  61375. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x00, 0x09, 0x00, 0x00,
  61376. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x01, 0xff,
  61377. 0xff, 0xff, 0xff, 0xfa, 0x0d, 0x00, 0x00, 0x00, 0xad, 0x02
  61378. };
  61379. const byte shBadCaNamesExt2[] = {
  61380. 0x16, 0x03, 0x04, 0x00, 0x3f, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0xcf,
  61381. 0x21, 0xad, 0x74, 0xe5, 0x9a, 0x61, 0x11, 0xbe, 0x1d, 0x8c, 0x02, 0x1e,
  61382. 0x65, 0xb8, 0x91, 0xc2, 0xa2, 0x11, 0x16, 0x7a, 0xbb, 0x8c, 0x5e, 0x07,
  61383. 0x9e, 0x09, 0xe2, 0xc8, 0xa8, 0x33, 0x9c, 0x00, 0x13, 0x03, 0x00, 0x00,
  61384. 0x13, 0x94, 0x7e, 0x00, 0x03, 0x0b, 0xf7, 0x03, 0x00, 0x2b, 0x00, 0x02,
  61385. 0x03, 0x04, 0x00, 0x33, 0x00, 0x02, 0x00, 0x19, 0x16, 0x03, 0x03, 0x00,
  61386. 0x5e, 0x02, 0x00, 0x00, 0x3b, 0x03, 0x03, 0x7f, 0xd0, 0x2d, 0xea, 0x6e,
  61387. 0x53, 0xa1, 0x6a, 0xc9, 0xc8, 0x54, 0xef, 0x75, 0xe4, 0xd9, 0xc6, 0x3e,
  61388. 0x74, 0xcb, 0x30, 0x80, 0xcc, 0x83, 0x3a, 0x00, 0x00, 0x00, 0x00, 0x00,
  61389. 0x00, 0xc0, 0x5a, 0x00, 0xc0, 0xb5, 0x00, 0x00, 0x11, 0x8f, 0x00, 0x00,
  61390. 0x03, 0x03, 0x00, 0x0c, 0x00, 0x2b, 0x00, 0x02, 0x03, 0x04, 0x53, 0x25,
  61391. 0x00, 0x00, 0x08, 0x00, 0x00, 0x06, 0x00, 0x04, 0x02, 0x05, 0x00, 0x00,
  61392. 0x0d, 0x00, 0x00, 0x11, 0x00, 0x00, 0x0d, 0x00, 0x2f, 0x00, 0x06, 0x00,
  61393. 0x04, 0x00, 0x03, 0x30, 0x00, 0x13, 0x94, 0x00, 0x06, 0x00, 0x04, 0x02
  61394. };
  61395. int i = 0;
  61396. for (i = 0; i < 2; i++) {
  61397. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61398. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  61399. wolfTLSv1_3_client_method, NULL), 0);
  61400. switch (i) {
  61401. case 0:
  61402. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt,
  61403. sizeof(shBadCaNamesExt));
  61404. test_ctx.c_len = sizeof(shBadCaNamesExt);
  61405. break;
  61406. case 1:
  61407. XMEMCPY(test_ctx.c_buff, shBadCaNamesExt2,
  61408. sizeof(shBadCaNamesExt2));
  61409. test_ctx.c_len = sizeof(shBadCaNamesExt2);
  61410. break;
  61411. }
  61412. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  61413. #ifndef WOLFSSL_DISABLE_EARLY_SANITY_CHECKS
  61414. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), OUT_OF_ORDER_E);
  61415. #else
  61416. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), BUFFER_ERROR);
  61417. #endif
  61418. wolfSSL_free(ssl_c);
  61419. ssl_c = NULL;
  61420. wolfSSL_CTX_free(ctx_c);
  61421. ctx_c = NULL;
  61422. }
  61423. #endif
  61424. return EXPECT_RESULT();
  61425. }
  61426. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  61427. defined(HAVE_IO_TESTS_DEPENDENCIES)
  61428. static void test_dtls_1_0_hvr_downgrade_ctx_ready(WOLFSSL_CTX* ctx)
  61429. {
  61430. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  61431. WOLFSSL_SUCCESS);
  61432. }
  61433. static int test_dtls_1_0_hvr_downgrade(void)
  61434. {
  61435. EXPECT_DECLS;
  61436. callback_functions func_cb_client;
  61437. callback_functions func_cb_server;
  61438. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  61439. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  61440. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  61441. func_cb_client.method = wolfDTLS_client_method;
  61442. func_cb_server.method = wolfDTLSv1_2_server_method;
  61443. func_cb_client.ctx_ready = test_dtls_1_0_hvr_downgrade_ctx_ready;
  61444. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61445. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61446. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61447. return EXPECT_RESULT();
  61448. }
  61449. #else
  61450. static int test_dtls_1_0_hvr_downgrade(void)
  61451. {
  61452. EXPECT_DECLS;
  61453. return EXPECT_RESULT();
  61454. }
  61455. #endif
  61456. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12) && \
  61457. defined(HAVE_SESSION_TICKET)
  61458. static WOLFSSL_SESSION* test_session_ticket_no_id_session = NULL;
  61459. static void test_session_ticket_no_id_on_result(WOLFSSL* ssl)
  61460. {
  61461. test_session_ticket_no_id_session = wolfSSL_get1_session(ssl);
  61462. AssertNotNull(test_session_ticket_no_id_session);
  61463. }
  61464. static void test_session_ticket_no_id_ctx_ready(WOLFSSL_CTX* ctx)
  61465. {
  61466. AssertIntEQ(wolfSSL_CTX_UseSessionTicket(ctx), WOLFSSL_SUCCESS);
  61467. }
  61468. static void test_session_ticket_no_id_ssl_ready(WOLFSSL* ssl)
  61469. {
  61470. test_session_ticket_no_id_session->sessionIDSz = 0;
  61471. AssertIntEQ(WOLFSSL_SUCCESS,
  61472. wolfSSL_set_session(ssl, test_session_ticket_no_id_session));
  61473. }
  61474. static int test_session_ticket_no_id(void)
  61475. {
  61476. /* We are testing an expired (invalid crypto context in out case since the
  61477. * ctx changes) session ticket being sent with the session ID being 0
  61478. * length. */
  61479. EXPECT_DECLS;
  61480. callback_functions func_cb_client;
  61481. callback_functions func_cb_server;
  61482. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  61483. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  61484. func_cb_client.method = wolfTLSv1_2_client_method;
  61485. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  61486. func_cb_client.on_result = test_session_ticket_no_id_on_result;
  61487. func_cb_server.method = wolfTLSv1_2_server_method;
  61488. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  61489. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61490. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61491. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61492. XMEMSET(&func_cb_client, 0, sizeof(func_cb_client));
  61493. XMEMSET(&func_cb_server, 0, sizeof(func_cb_server));
  61494. func_cb_client.method = wolfTLSv1_2_client_method;
  61495. func_cb_client.ctx_ready = test_session_ticket_no_id_ctx_ready;
  61496. func_cb_client.ssl_ready = test_session_ticket_no_id_ssl_ready;
  61497. func_cb_server.method = wolfTLSv1_2_server_method;
  61498. func_cb_server.ctx_ready = test_session_ticket_no_id_ctx_ready;
  61499. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61500. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61501. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61502. wolfSSL_SESSION_free(test_session_ticket_no_id_session);
  61503. return EXPECT_RESULT();
  61504. }
  61505. #else
  61506. static int test_session_ticket_no_id(void)
  61507. {
  61508. EXPECT_DECLS;
  61509. return EXPECT_RESULT();
  61510. }
  61511. #endif
  61512. static int test_session_ticket_hs_update(void)
  61513. {
  61514. EXPECT_DECLS;
  61515. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  61516. defined(HAVE_SESSION_TICKET) && !defined(WOLFSSL_NO_DEF_TICKET_ENC_CB)
  61517. struct test_memio_ctx test_ctx;
  61518. struct test_memio_ctx test_ctx2;
  61519. struct test_memio_ctx test_ctx3;
  61520. WOLFSSL_CTX *ctx_c = NULL;
  61521. WOLFSSL_CTX *ctx_s = NULL;
  61522. WOLFSSL *ssl_c = NULL;
  61523. WOLFSSL *ssl_c2 = NULL;
  61524. WOLFSSL *ssl_c3 = NULL;
  61525. WOLFSSL *ssl_s = NULL;
  61526. WOLFSSL *ssl_s2 = NULL;
  61527. WOLFSSL *ssl_s3 = NULL;
  61528. WOLFSSL_SESSION *sess = NULL;
  61529. byte read_data[1];
  61530. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61531. XMEMSET(&test_ctx2, 0, sizeof(test_ctx2));
  61532. XMEMSET(&test_ctx3, 0, sizeof(test_ctx3));
  61533. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61534. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  61535. /* Generate tickets */
  61536. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61537. wolfSSL_SetLoggingPrefix("client");
  61538. /* Read the ticket msg */
  61539. ExpectIntEQ(wolfSSL_read(ssl_c, read_data, sizeof(read_data)),
  61540. WOLFSSL_FATAL_ERROR);
  61541. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  61542. WOLFSSL_ERROR_WANT_READ);
  61543. wolfSSL_SetLoggingPrefix(NULL);
  61544. ExpectIntEQ(test_memio_setup(&test_ctx2, &ctx_c, &ctx_s, &ssl_c2, &ssl_s2,
  61545. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  61546. ExpectIntEQ(test_memio_setup(&test_ctx3, &ctx_c, &ctx_s, &ssl_c3, &ssl_s3,
  61547. wolfTLSv1_3_client_method, wolfTLSv1_3_server_method), 0);
  61548. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  61549. ExpectIntEQ(wolfSSL_set_session(ssl_c2, sess), WOLFSSL_SUCCESS);
  61550. ExpectIntEQ(wolfSSL_set_session(ssl_c3, sess), WOLFSSL_SUCCESS);
  61551. wolfSSL_SetLoggingPrefix("client");
  61552. /* Exchange initial flights for the second connection */
  61553. ExpectIntEQ(wolfSSL_connect(ssl_c2), WOLFSSL_FATAL_ERROR);
  61554. ExpectIntEQ(wolfSSL_get_error(ssl_c2, WOLFSSL_FATAL_ERROR),
  61555. WOLFSSL_ERROR_WANT_READ);
  61556. wolfSSL_SetLoggingPrefix(NULL);
  61557. wolfSSL_SetLoggingPrefix("server");
  61558. ExpectIntEQ(wolfSSL_accept(ssl_s2), WOLFSSL_FATAL_ERROR);
  61559. ExpectIntEQ(wolfSSL_get_error(ssl_s2, WOLFSSL_FATAL_ERROR),
  61560. WOLFSSL_ERROR_WANT_READ);
  61561. wolfSSL_SetLoggingPrefix(NULL);
  61562. /* Complete third connection so that new tickets are exchanged */
  61563. ExpectIntEQ(test_memio_do_handshake(ssl_c3, ssl_s3, 10, NULL), 0);
  61564. /* Read the ticket msg */
  61565. wolfSSL_SetLoggingPrefix("client");
  61566. ExpectIntEQ(wolfSSL_read(ssl_c3, read_data, sizeof(read_data)),
  61567. WOLFSSL_FATAL_ERROR);
  61568. ExpectIntEQ(wolfSSL_get_error(ssl_c3, WOLFSSL_FATAL_ERROR),
  61569. WOLFSSL_ERROR_WANT_READ);
  61570. wolfSSL_SetLoggingPrefix(NULL);
  61571. /* Complete second connection */
  61572. ExpectIntEQ(test_memio_do_handshake(ssl_c2, ssl_s2, 10, NULL), 0);
  61573. ExpectIntEQ(wolfSSL_session_reused(ssl_c2), 1);
  61574. ExpectIntEQ(wolfSSL_session_reused(ssl_c3), 1);
  61575. wolfSSL_free(ssl_c);
  61576. wolfSSL_free(ssl_c2);
  61577. wolfSSL_free(ssl_c3);
  61578. wolfSSL_free(ssl_s);
  61579. wolfSSL_free(ssl_s2);
  61580. wolfSSL_free(ssl_s3);
  61581. wolfSSL_CTX_free(ctx_c);
  61582. wolfSSL_CTX_free(ctx_s);
  61583. wolfSSL_SESSION_free(sess);
  61584. #endif
  61585. return EXPECT_RESULT();
  61586. }
  61587. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  61588. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  61589. static void test_dtls_downgrade_scr_server_ctx_ready_server(WOLFSSL_CTX* ctx)
  61590. {
  61591. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  61592. WOLFSSL_SUCCESS);
  61593. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  61594. }
  61595. static void test_dtls_downgrade_scr_server_ctx_ready(WOLFSSL_CTX* ctx)
  61596. {
  61597. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  61598. }
  61599. static void test_dtls_downgrade_scr_server_on_result(WOLFSSL* ssl)
  61600. {
  61601. char testMsg[] = "Message after SCR";
  61602. char msgBuf[sizeof(testMsg)];
  61603. if (wolfSSL_is_server(ssl)) {
  61604. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  61605. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  61606. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  61607. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  61608. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  61609. sizeof(testMsg));
  61610. }
  61611. else {
  61612. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  61613. sizeof(testMsg));
  61614. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  61615. }
  61616. }
  61617. static int test_dtls_downgrade_scr_server(void)
  61618. {
  61619. EXPECT_DECLS;
  61620. callback_functions func_cb_client;
  61621. callback_functions func_cb_server;
  61622. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  61623. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  61624. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  61625. func_cb_client.method = wolfDTLSv1_2_client_method;
  61626. func_cb_server.method = wolfDTLS_server_method;
  61627. func_cb_client.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready;
  61628. func_cb_server.ctx_ready = test_dtls_downgrade_scr_server_ctx_ready_server;
  61629. func_cb_client.on_result = test_dtls_downgrade_scr_server_on_result;
  61630. func_cb_server.on_result = test_dtls_downgrade_scr_server_on_result;
  61631. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61632. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61633. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61634. return EXPECT_RESULT();
  61635. }
  61636. #else
  61637. static int test_dtls_downgrade_scr_server(void)
  61638. {
  61639. EXPECT_DECLS;
  61640. return EXPECT_RESULT();
  61641. }
  61642. #endif
  61643. #if defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  61644. defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(HAVE_SECURE_RENEGOTIATION)
  61645. static void test_dtls_downgrade_scr_ctx_ready(WOLFSSL_CTX* ctx)
  61646. {
  61647. AssertIntEQ(wolfSSL_CTX_SetMinVersion(ctx, WOLFSSL_DTLSV1_2),
  61648. WOLFSSL_SUCCESS);
  61649. AssertIntEQ(wolfSSL_CTX_UseSecureRenegotiation(ctx), WOLFSSL_SUCCESS);
  61650. }
  61651. static void test_dtls_downgrade_scr_on_result(WOLFSSL* ssl)
  61652. {
  61653. char testMsg[] = "Message after SCR";
  61654. char msgBuf[sizeof(testMsg)];
  61655. if (wolfSSL_is_server(ssl)) {
  61656. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_FATAL_ERROR);
  61657. AssertIntEQ(wolfSSL_get_error(ssl, -1), APP_DATA_READY);
  61658. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  61659. AssertIntEQ(wolfSSL_Rehandshake(ssl), WOLFSSL_SUCCESS);
  61660. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  61661. sizeof(testMsg));
  61662. }
  61663. else {
  61664. AssertIntEQ(wolfSSL_write(ssl, testMsg, sizeof(testMsg)),
  61665. sizeof(testMsg));
  61666. AssertIntEQ(wolfSSL_read(ssl, msgBuf, sizeof(msgBuf)), sizeof(msgBuf));
  61667. }
  61668. }
  61669. static int test_dtls_downgrade_scr(void)
  61670. {
  61671. EXPECT_DECLS;
  61672. callback_functions func_cb_client;
  61673. callback_functions func_cb_server;
  61674. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  61675. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  61676. func_cb_client.doUdp = func_cb_server.doUdp = 1;
  61677. func_cb_client.method = wolfDTLS_client_method;
  61678. func_cb_server.method = wolfDTLSv1_2_server_method;
  61679. func_cb_client.ctx_ready = test_dtls_downgrade_scr_ctx_ready;
  61680. func_cb_client.on_result = test_dtls_downgrade_scr_on_result;
  61681. func_cb_server.on_result = test_dtls_downgrade_scr_on_result;
  61682. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  61683. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  61684. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  61685. return EXPECT_RESULT();
  61686. }
  61687. #else
  61688. static int test_dtls_downgrade_scr(void)
  61689. {
  61690. EXPECT_DECLS;
  61691. return EXPECT_RESULT();
  61692. }
  61693. #endif
  61694. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  61695. && !defined(WOLFSSL_NO_TLS12)
  61696. static int test_dtls_client_hello_timeout_downgrade_read_cb(WOLFSSL *ssl,
  61697. char *data, int sz, void *ctx)
  61698. {
  61699. static int call_counter = 0;
  61700. call_counter++;
  61701. (void)ssl;
  61702. (void)data;
  61703. (void)sz;
  61704. (void)ctx;
  61705. switch (call_counter) {
  61706. case 1:
  61707. case 2:
  61708. return WOLFSSL_CBIO_ERR_TIMEOUT;
  61709. case 3:
  61710. return WOLFSSL_CBIO_ERR_WANT_READ;
  61711. default:
  61712. AssertIntLE(call_counter, 3);
  61713. return -1;
  61714. }
  61715. }
  61716. #endif
  61717. /* Make sure we don't send acks before getting a server hello */
  61718. static int test_dtls_client_hello_timeout_downgrade(void)
  61719. {
  61720. EXPECT_DECLS;
  61721. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  61722. && !defined(WOLFSSL_NO_TLS12)
  61723. WOLFSSL_CTX *ctx_c = NULL;
  61724. WOLFSSL_CTX *ctx_s = NULL;
  61725. WOLFSSL *ssl_c = NULL;
  61726. WOLFSSL *ssl_s = NULL;
  61727. struct test_memio_ctx test_ctx;
  61728. DtlsRecordLayerHeader* dtlsRH;
  61729. size_t len;
  61730. byte sequence_number[8];
  61731. int i;
  61732. for (i = 0; i < 2; i++) {
  61733. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61734. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61735. wolfDTLS_client_method, wolfDTLSv1_2_server_method), 0);
  61736. if (i == 0) {
  61737. /* First time simulate timeout in IO layer */
  61738. /* CH1 */
  61739. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61740. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61741. /* HVR */
  61742. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61743. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61744. /* CH2 */
  61745. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61746. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61747. /* SH flight */
  61748. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61749. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61750. /* Drop the SH */
  61751. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  61752. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61753. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  61754. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  61755. (sizeof(DtlsRecordLayerHeader) + len));
  61756. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  61757. /* Read the remainder of the flight */
  61758. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61759. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61760. wolfSSL_SSLSetIORecv(ssl_c,
  61761. test_dtls_client_hello_timeout_downgrade_read_cb);
  61762. /* CH3 */
  61763. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61764. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61765. wolfSSL_SSLSetIORecv(ssl_c, test_memio_read_cb);
  61766. }
  61767. else {
  61768. /* Second time call wolfSSL_dtls_got_timeout */
  61769. /* CH1 */
  61770. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61771. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61772. /* HVR */
  61773. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61774. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61775. /* CH2 */
  61776. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61777. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61778. /* SH flight */
  61779. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61780. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61781. /* Drop the SH */
  61782. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.c_buff);
  61783. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61784. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  61785. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  61786. (sizeof(DtlsRecordLayerHeader) + len));
  61787. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  61788. /* Read the remainder of the flight */
  61789. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61790. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61791. /* Quick timeout should be set as we received at least one msg */
  61792. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 1);
  61793. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  61794. /* Quick timeout should be cleared after a quick timeout */
  61795. /* CH3 */
  61796. ExpectIntEQ(wolfSSL_dtls13_use_quick_timeout(ssl_c), 0);
  61797. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  61798. }
  61799. /* Parse out to make sure we got exactly one ClientHello message */
  61800. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  61801. /* Second ClientHello after HVR */
  61802. sequence_number[7] = 2;
  61803. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.s_buff;
  61804. ExpectIntEQ(dtlsRH->type, handshake);
  61805. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  61806. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  61807. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  61808. sizeof(sequence_number)), 0);
  61809. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61810. ExpectIntEQ(sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  61811. /* Connection should be able to continue */
  61812. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  61813. wolfSSL_free(ssl_c);
  61814. wolfSSL_free(ssl_s);
  61815. wolfSSL_CTX_free(ctx_c);
  61816. wolfSSL_CTX_free(ctx_s);
  61817. ssl_c = NULL;
  61818. ssl_s = NULL;
  61819. ctx_c = NULL;
  61820. ctx_s = NULL;
  61821. if (!EXPECT_SUCCESS())
  61822. break;
  61823. }
  61824. #endif
  61825. return EXPECT_RESULT();
  61826. }
  61827. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  61828. static int test_dtls_client_hello_timeout_read_cb(WOLFSSL *ssl, char *data,
  61829. int sz, void *ctx)
  61830. {
  61831. static int call_counter = 0;
  61832. call_counter++;
  61833. (void)ssl;
  61834. (void)data;
  61835. (void)sz;
  61836. (void)ctx;
  61837. switch (call_counter) {
  61838. case 1:
  61839. return WOLFSSL_CBIO_ERR_TIMEOUT;
  61840. case 2:
  61841. return WOLFSSL_CBIO_ERR_WANT_READ;
  61842. default:
  61843. AssertIntLE(call_counter, 2);
  61844. return -1;
  61845. }
  61846. }
  61847. #endif
  61848. /* Make sure we don't send acks before getting a server hello */
  61849. static int test_dtls_client_hello_timeout(void)
  61850. {
  61851. EXPECT_DECLS;
  61852. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  61853. WOLFSSL *ssl_c = NULL;
  61854. WOLFSSL_CTX *ctx_c = NULL;
  61855. struct test_memio_ctx test_ctx;
  61856. DtlsRecordLayerHeader* dtlsRH;
  61857. size_t idx;
  61858. size_t len;
  61859. byte sequence_number[8];
  61860. int i;
  61861. for (i = 0; i < 2; i++) {
  61862. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61863. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, NULL, &ssl_c, NULL,
  61864. wolfDTLSv1_3_client_method, NULL), 0);
  61865. if (i == 0) {
  61866. /* First time simulate timeout in IO layer */
  61867. wolfSSL_SSLSetIORecv(ssl_c, test_dtls_client_hello_timeout_read_cb);
  61868. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  61869. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61870. }
  61871. else {
  61872. /* Second time call wolfSSL_dtls_got_timeout */
  61873. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  61874. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61875. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  61876. }
  61877. /* Parse out to make sure we got exactly two ClientHello messages */
  61878. idx = 0;
  61879. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  61880. /* First ClientHello */
  61881. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  61882. ExpectIntEQ(dtlsRH->type, handshake);
  61883. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  61884. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  61885. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  61886. sizeof(sequence_number)), 0);
  61887. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61888. ExpectIntLT(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  61889. idx += sizeof(DtlsRecordLayerHeader) + len;
  61890. /* Second ClientHello */
  61891. sequence_number[7] = 1;
  61892. dtlsRH = (DtlsRecordLayerHeader*)(test_ctx.s_buff + idx);
  61893. ExpectIntEQ(dtlsRH->type, handshake);
  61894. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  61895. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  61896. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  61897. sizeof(sequence_number)), 0);
  61898. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61899. ExpectIntEQ(idx + sizeof(DtlsRecordLayerHeader) + len, test_ctx.s_len);
  61900. wolfSSL_free(ssl_c);
  61901. wolfSSL_CTX_free(ctx_c);
  61902. ssl_c = NULL;
  61903. ctx_c = NULL;
  61904. if (!EXPECT_SUCCESS())
  61905. break;
  61906. }
  61907. #endif
  61908. return EXPECT_RESULT();
  61909. }
  61910. /* DTLS test when dropping the changed cipher spec message */
  61911. static int test_dtls_dropped_ccs(void)
  61912. {
  61913. EXPECT_DECLS;
  61914. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  61915. && !defined(WOLFSSL_NO_TLS12)
  61916. WOLFSSL_CTX *ctx_c = NULL;
  61917. WOLFSSL_CTX *ctx_s = NULL;
  61918. WOLFSSL *ssl_c = NULL;
  61919. WOLFSSL *ssl_s = NULL;
  61920. struct test_memio_ctx test_ctx;
  61921. DtlsRecordLayerHeader* dtlsRH;
  61922. size_t len;
  61923. byte data[1];
  61924. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  61925. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  61926. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  61927. /* CH1 */
  61928. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61929. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61930. /* HVR */
  61931. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61932. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61933. /* CH2 */
  61934. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61935. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61936. /* Server first flight */
  61937. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  61938. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61939. /* Client flight */
  61940. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61941. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61942. /* Server ccs + finished */
  61943. ExpectIntEQ(wolfSSL_negotiate(ssl_s), 1);
  61944. /* Drop the ccs */
  61945. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  61946. len = (size_t)((dtlsRH->length[0] << 8) | dtlsRH->length[1]);
  61947. ExpectIntEQ(len, 1);
  61948. ExpectIntEQ(dtlsRH->type, change_cipher_spec);
  61949. if (EXPECT_SUCCESS()) {
  61950. XMEMMOVE(test_ctx.c_buff, test_ctx.c_buff +
  61951. sizeof(DtlsRecordLayerHeader) + len, test_ctx.c_len -
  61952. (sizeof(DtlsRecordLayerHeader) + len));
  61953. }
  61954. test_ctx.c_len -= sizeof(DtlsRecordLayerHeader) + len;
  61955. /* Client rtx flight */
  61956. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  61957. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  61958. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  61959. /* Server ccs + finished rtx */
  61960. ExpectIntEQ(wolfSSL_read(ssl_s, data, sizeof(data)), -1);
  61961. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  61962. /* Client processes finished */
  61963. ExpectIntEQ(wolfSSL_negotiate(ssl_c), 1);
  61964. wolfSSL_free(ssl_c);
  61965. wolfSSL_free(ssl_s);
  61966. wolfSSL_CTX_free(ctx_c);
  61967. wolfSSL_CTX_free(ctx_s);
  61968. #endif
  61969. return EXPECT_RESULT();
  61970. }
  61971. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  61972. && !defined(WOLFSSL_NO_TLS12)
  61973. static int test_dtls_seq_num_downgrade_check_num(byte* ioBuf, int ioBufLen,
  61974. byte seq_num)
  61975. {
  61976. EXPECT_DECLS;
  61977. DtlsRecordLayerHeader* dtlsRH;
  61978. byte sequence_number[8];
  61979. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  61980. ExpectIntGE(ioBufLen, sizeof(*dtlsRH));
  61981. dtlsRH = (DtlsRecordLayerHeader*)ioBuf;
  61982. ExpectIntEQ(dtlsRH->type, handshake);
  61983. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  61984. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  61985. sequence_number[7] = seq_num;
  61986. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  61987. sizeof(sequence_number)), 0);
  61988. return EXPECT_RESULT();
  61989. }
  61990. #endif
  61991. /*
  61992. * Make sure that we send the correct sequence number after a HelloVerifyRequest
  61993. * and after a HelloRetryRequest. This is testing the server side as it is
  61994. * operating statelessly and should copy the sequence number of the ClientHello.
  61995. */
  61996. static int test_dtls_seq_num_downgrade(void)
  61997. {
  61998. EXPECT_DECLS;
  61999. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  62000. && !defined(WOLFSSL_NO_TLS12)
  62001. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  62002. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  62003. struct test_memio_ctx test_ctx;
  62004. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62005. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62006. wolfDTLSv1_2_client_method, wolfDTLS_server_method), 0);
  62007. /* CH1 */
  62008. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62009. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62010. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.s_buff,
  62011. test_ctx.s_len, 0), TEST_SUCCESS);
  62012. /* HVR */
  62013. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62014. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62015. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.c_buff,
  62016. test_ctx.c_len, 0), TEST_SUCCESS);
  62017. /* CH2 */
  62018. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62019. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62020. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.s_buff,
  62021. test_ctx.s_len, 1), TEST_SUCCESS);
  62022. /* Server first flight */
  62023. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62024. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62025. ExpectIntEQ(test_dtls_seq_num_downgrade_check_num(test_ctx.c_buff,
  62026. test_ctx.c_len, 1), TEST_SUCCESS);
  62027. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62028. wolfSSL_free(ssl_c);
  62029. wolfSSL_CTX_free(ctx_c);
  62030. wolfSSL_free(ssl_s);
  62031. wolfSSL_CTX_free(ctx_s);
  62032. #endif
  62033. return EXPECT_RESULT();
  62034. }
  62035. /**
  62036. * Make sure we don't send RSA Signature Hash Algorithms in the
  62037. * CertificateRequest when we don't have any such ciphers set.
  62038. * @return EXPECT_RESULT()
  62039. */
  62040. static int test_certreq_sighash_algos(void)
  62041. {
  62042. EXPECT_DECLS;
  62043. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  62044. !defined(WOLFSSL_MAX_STRENGTH) && defined(HAVE_ECC) && \
  62045. defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256) && \
  62046. defined(HAVE_AES_CBC) && !defined(WOLFSSL_NO_TLS12)
  62047. WOLFSSL_CTX *ctx_c = NULL;
  62048. WOLFSSL_CTX *ctx_s = NULL;
  62049. WOLFSSL *ssl_c = NULL;
  62050. WOLFSSL *ssl_s = NULL;
  62051. struct test_memio_ctx test_ctx;
  62052. int idx = 0;
  62053. int maxIdx = 0;
  62054. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62055. test_ctx.c_ciphers = test_ctx.s_ciphers =
  62056. "ECDHE-ECDSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA384";
  62057. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62058. wolfTLSv1_2_client_method, wolfTLSv1_2_server_method), 0);
  62059. ExpectIntEQ(wolfSSL_CTX_load_verify_locations(ctx_c,
  62060. "./certs/ca-ecc-cert.pem", NULL), WOLFSSL_SUCCESS);
  62061. wolfSSL_set_verify(ssl_s, WOLFSSL_VERIFY_PEER, NULL);
  62062. ExpectIntEQ(wolfSSL_use_PrivateKey_file(ssl_s, "./certs/ecc-key.pem",
  62063. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  62064. ExpectIntEQ(wolfSSL_use_certificate_file(ssl_s, "./certs/server-ecc.pem",
  62065. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  62066. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_FATAL_ERROR);
  62067. ExpectIntEQ(wolfSSL_get_error(ssl_c, WOLFSSL_FATAL_ERROR),
  62068. WOLFSSL_ERROR_WANT_READ);
  62069. ExpectIntEQ(wolfSSL_accept(ssl_s), WOLFSSL_FATAL_ERROR);
  62070. ExpectIntEQ(wolfSSL_get_error(ssl_s, WOLFSSL_FATAL_ERROR),
  62071. WOLFSSL_ERROR_WANT_READ);
  62072. /* Find the CertificateRequest message */
  62073. for (idx = 0; idx < test_ctx.c_len && EXPECT_SUCCESS();) {
  62074. word16 len;
  62075. ExpectIntEQ(test_ctx.c_buff[idx++], handshake);
  62076. ExpectIntEQ(test_ctx.c_buff[idx++], SSLv3_MAJOR);
  62077. ExpectIntEQ(test_ctx.c_buff[idx++], TLSv1_2_MINOR);
  62078. ato16(test_ctx.c_buff + idx, &len);
  62079. idx += OPAQUE16_LEN;
  62080. if (test_ctx.c_buff[idx] == certificate_request) {
  62081. idx++;
  62082. /* length */
  62083. idx += OPAQUE24_LEN;
  62084. /* cert types */
  62085. idx += 1 + test_ctx.c_buff[idx];
  62086. /* Sig algos */
  62087. ato16(test_ctx.c_buff + idx, &len);
  62088. idx += OPAQUE16_LEN;
  62089. maxIdx = idx + (int)len;
  62090. for (; idx < maxIdx && EXPECT_SUCCESS(); idx += OPAQUE16_LEN) {
  62091. if (test_ctx.c_buff[idx+1] == ED25519_SA_MINOR ||
  62092. test_ctx.c_buff[idx+1] == ED448_SA_MINOR)
  62093. ExpectIntEQ(test_ctx.c_buff[idx], NEW_SA_MAJOR);
  62094. else
  62095. ExpectIntEQ(test_ctx.c_buff[idx+1], ecc_dsa_sa_algo);
  62096. }
  62097. break;
  62098. }
  62099. else {
  62100. idx += (int)len;
  62101. }
  62102. }
  62103. ExpectIntLT(idx, test_ctx.c_len);
  62104. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62105. wolfSSL_free(ssl_c);
  62106. wolfSSL_free(ssl_s);
  62107. wolfSSL_CTX_free(ctx_c);
  62108. wolfSSL_CTX_free(ctx_s);
  62109. #endif
  62110. return EXPECT_RESULT();
  62111. }
  62112. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  62113. static int test_revoked_loaded_int_cert_ctx_ready1(WOLFSSL_CTX* ctx)
  62114. {
  62115. EXPECT_DECLS;
  62116. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  62117. myVerifyAction = VERIFY_USE_PREVERFIY;
  62118. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  62119. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  62120. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  62121. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  62122. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  62123. WOLFSSL_SUCCESS);
  62124. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  62125. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  62126. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  62127. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  62128. "./certs/crl/ca-int.pem",
  62129. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  62130. return EXPECT_RESULT();
  62131. }
  62132. static int test_revoked_loaded_int_cert_ctx_ready2(WOLFSSL_CTX* ctx)
  62133. {
  62134. EXPECT_DECLS;
  62135. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER, myVerify);
  62136. myVerifyAction = VERIFY_USE_PREVERFIY;
  62137. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  62138. "./certs/ca-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  62139. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  62140. "./certs/intermediate/ca-int-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  62141. ExpectIntEQ(wolfSSL_CTX_load_verify_locations_ex(ctx,
  62142. "./certs/intermediate/ca-int2-cert.pem", NULL, 0), WOLFSSL_SUCCESS);
  62143. ExpectIntEQ(wolfSSL_CTX_EnableCRL(ctx, WOLFSSL_CRL_CHECKALL),
  62144. WOLFSSL_SUCCESS);
  62145. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  62146. "./certs/crl/ca-int2.pem",
  62147. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  62148. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  62149. "./certs/crl/extra-crls/ca-int-cert-revoked.pem",
  62150. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  62151. ExpectIntEQ(wolfSSL_CTX_LoadCRLFile(ctx,
  62152. "./certs/crl/ca-int.pem",
  62153. WOLFSSL_FILETYPE_PEM), WOLFSSL_SUCCESS);
  62154. return EXPECT_RESULT();
  62155. }
  62156. #endif
  62157. static int test_revoked_loaded_int_cert(void)
  62158. {
  62159. EXPECT_DECLS;
  62160. #if defined(HAVE_CRL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  62161. test_ssl_cbf client_cbf;
  62162. test_ssl_cbf server_cbf;
  62163. struct {
  62164. const char* certPemFile;
  62165. const char* keyPemFile;
  62166. ctx_cb client_ctx_ready;
  62167. } test_params[] = {
  62168. {"./certs/intermediate/ca-int2-cert.pem",
  62169. "./certs/intermediate/ca-int2-key.pem",
  62170. test_revoked_loaded_int_cert_ctx_ready1},
  62171. {"./certs/intermediate/server-chain.pem",
  62172. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  62173. {"./certs/intermediate/server-chain-short.pem",
  62174. "./certs/server-key.pem", test_revoked_loaded_int_cert_ctx_ready2},
  62175. };
  62176. size_t i;
  62177. printf("\n");
  62178. for (i = 0; i < XELEM_CNT(test_params); i++) {
  62179. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  62180. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  62181. printf("\tTesting with %s...\n", test_params[i].certPemFile);
  62182. server_cbf.certPemFile = test_params[i].certPemFile;
  62183. server_cbf.keyPemFile = test_params[i].keyPemFile;
  62184. client_cbf.ctx_ready = test_params[i].client_ctx_ready;
  62185. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  62186. &server_cbf, NULL), TEST_FAIL);
  62187. ExpectIntEQ(client_cbf.last_err, CRL_CERT_REVOKED);
  62188. ExpectIntEQ(server_cbf.last_err, FATAL_ERROR);
  62189. if (!EXPECT_SUCCESS())
  62190. break;
  62191. printf("\t%s passed\n", test_params[i].certPemFile);
  62192. }
  62193. #endif
  62194. return EXPECT_RESULT();
  62195. }
  62196. static int test_dtls13_frag_ch_pq(void)
  62197. {
  62198. EXPECT_DECLS;
  62199. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  62200. && defined(WOLFSSL_DTLS_CH_FRAG) && defined(HAVE_LIBOQS)
  62201. WOLFSSL_CTX *ctx_c = NULL;
  62202. WOLFSSL_CTX *ctx_s = NULL;
  62203. WOLFSSL *ssl_c = NULL;
  62204. WOLFSSL *ssl_s = NULL;
  62205. struct test_memio_ctx test_ctx;
  62206. const char *test_str = "test";
  62207. int test_str_size;
  62208. byte buf[255];
  62209. int group = WOLFSSL_KYBER_LEVEL5;
  62210. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62211. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62212. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  62213. /* Add in a large post-quantum key share to make the CH long. */
  62214. ExpectIntEQ(wolfSSL_set_groups(ssl_c, &group, 1), WOLFSSL_SUCCESS);
  62215. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, group), WOLFSSL_SUCCESS);
  62216. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  62217. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62218. ExpectStrEQ(wolfSSL_get_curve_name(ssl_c), "KYBER_LEVEL5");
  62219. ExpectStrEQ(wolfSSL_get_curve_name(ssl_s), "KYBER_LEVEL5");
  62220. test_str_size = XSTRLEN("test") + 1;
  62221. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  62222. ExpectIntEQ(wolfSSL_read(ssl_s, buf, sizeof(buf)), test_str_size);
  62223. ExpectIntEQ(XSTRCMP((char*)buf, test_str), 0);
  62224. ExpectIntEQ(wolfSSL_write(ssl_c, test_str, test_str_size), test_str_size);
  62225. wolfSSL_free(ssl_c);
  62226. wolfSSL_free(ssl_s);
  62227. wolfSSL_CTX_free(ctx_c);
  62228. wolfSSL_CTX_free(ctx_s);
  62229. #endif
  62230. return EXPECT_RESULT();
  62231. }
  62232. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS) \
  62233. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  62234. static int test_dtls_frag_ch_count_records(byte* b, int len)
  62235. {
  62236. DtlsRecordLayerHeader* dtlsRH;
  62237. int records = 0;
  62238. size_t recordLen;
  62239. while (len > 0) {
  62240. records++;
  62241. dtlsRH = (DtlsRecordLayerHeader*)b;
  62242. recordLen = (dtlsRH->length[0] << 8) | dtlsRH->length[1];
  62243. b += sizeof(DtlsRecordLayerHeader) + recordLen;
  62244. len -= sizeof(DtlsRecordLayerHeader) + recordLen;
  62245. }
  62246. return records;
  62247. }
  62248. #endif
  62249. static int test_dtls_frag_ch(void)
  62250. {
  62251. EXPECT_DECLS;
  62252. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13) \
  62253. && defined(WOLFSSL_DTLS_MTU) && defined(WOLFSSL_DTLS_CH_FRAG)
  62254. WOLFSSL_CTX *ctx_c = NULL;
  62255. WOLFSSL_CTX *ctx_s = NULL;
  62256. WOLFSSL *ssl_c = NULL;
  62257. WOLFSSL *ssl_s = NULL;
  62258. struct test_memio_ctx test_ctx;
  62259. static unsigned int DUMMY_MTU = 256;
  62260. unsigned char four_frag_CH[] = {
  62261. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  62262. 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  62263. 0xce, 0xfe, 0xfd, 0xf3, 0x94, 0x01, 0x33, 0x2c, 0xcf, 0x2c, 0x47, 0xb1,
  62264. 0xe5, 0xa1, 0x7b, 0x19, 0x3e, 0xac, 0x68, 0xdd, 0xe6, 0x17, 0x6b, 0x85,
  62265. 0xad, 0x5f, 0xfc, 0x7f, 0x6e, 0xf0, 0xb9, 0xe0, 0x2e, 0xca, 0x47, 0x00,
  62266. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  62267. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  62268. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  62269. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  62270. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x02,
  62271. 0x7c, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x0d, 0x00, 0x20,
  62272. 0x00, 0x1e, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02, 0x03, 0x08, 0x06,
  62273. 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06, 0x01,
  62274. 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00, 0x0c,
  62275. 0x00, 0x0a, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01, 0x00,
  62276. 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x02, 0x39, 0x02, 0x37, 0x00, 0x17,
  62277. 0x00, 0x41, 0x04, 0x94, 0xdf, 0x36, 0xd7, 0xb3, 0x90, 0x6d, 0x01, 0xa1,
  62278. 0xe6, 0xed, 0x67, 0xf4, 0xd9, 0x9d, 0x2c, 0xac, 0x57, 0x74, 0xff, 0x19,
  62279. 0xbe, 0x5a, 0xc9, 0x30, 0x11, 0xb7, 0x2b, 0x59, 0x47, 0x80, 0x7c, 0xa9,
  62280. 0xb7, 0x31, 0x8c, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  62281. 0x00, 0x01, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00, 0x00, 0x00, 0x00,
  62282. 0xce, 0x00, 0x00, 0xce, 0x9e, 0x13, 0x74, 0x3b, 0x86, 0xba, 0x69, 0x1f,
  62283. 0x12, 0xf7, 0xcd, 0x78, 0x53, 0xe8, 0x50, 0x4d, 0x71, 0x3f, 0x4b, 0x4e,
  62284. 0xeb, 0x3e, 0xe5, 0x43, 0x54, 0x78, 0x17, 0x6d, 0x00, 0x18, 0x00, 0x61,
  62285. 0x04, 0xd1, 0x99, 0x66, 0x4f, 0xda, 0xc7, 0x12, 0x3b, 0xff, 0xb2, 0xd6,
  62286. 0x2f, 0x35, 0xb6, 0x17, 0x1f, 0xb3, 0xd0, 0xb6, 0x52, 0xff, 0x97, 0x8b,
  62287. 0x01, 0xe8, 0xd9, 0x68, 0x71, 0x40, 0x02, 0xd5, 0x68, 0x3a, 0x58, 0xb2,
  62288. 0x5d, 0xee, 0xa4, 0xe9, 0x5f, 0xf4, 0xaf, 0x3e, 0x30, 0x9c, 0x3e, 0x2b,
  62289. 0xda, 0x61, 0x43, 0x99, 0x02, 0x35, 0x33, 0x9f, 0xcf, 0xb5, 0xd3, 0x28,
  62290. 0x19, 0x9d, 0x1c, 0xbe, 0x69, 0x07, 0x9e, 0xfc, 0xe4, 0x8e, 0xcd, 0x86,
  62291. 0x4a, 0x1b, 0xf0, 0xfc, 0x17, 0x94, 0x66, 0x53, 0xda, 0x24, 0x5e, 0xaf,
  62292. 0xce, 0xec, 0x62, 0x4c, 0x06, 0xb4, 0x52, 0x94, 0xb1, 0x4a, 0x7a, 0x8c,
  62293. 0x4f, 0x00, 0x19, 0x00, 0x85, 0x04, 0x00, 0x27, 0xeb, 0x99, 0x49, 0x7f,
  62294. 0xcb, 0x2c, 0x46, 0x54, 0x2d, 0x93, 0x5d, 0x25, 0x92, 0x58, 0x5e, 0x06,
  62295. 0xc3, 0x7c, 0xfb, 0x9a, 0xa7, 0xec, 0xcd, 0x9f, 0xe1, 0x6b, 0x2d, 0x78,
  62296. 0xf5, 0x16, 0xa9, 0x20, 0x52, 0x48, 0x19, 0x0f, 0x1a, 0xd0, 0xce, 0xd8,
  62297. 0x68, 0xb1, 0x4e, 0x7f, 0x33, 0x03, 0x7d, 0x0c, 0x39, 0xdb, 0x9c, 0x4b,
  62298. 0xf4, 0xe7, 0xc2, 0xf5, 0xdd, 0x51, 0x9b, 0x03, 0xa8, 0x53, 0x2b, 0xe6,
  62299. 0x00, 0x15, 0x4b, 0xff, 0xd2, 0xa0, 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00,
  62300. 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0xda, 0x01, 0x00, 0x02, 0xdc, 0x00,
  62301. 0x00, 0x00, 0x01, 0x9c, 0x00, 0x00, 0xce, 0x58, 0x30, 0x10, 0x3d, 0x46,
  62302. 0xcc, 0xca, 0x1a, 0x44, 0xc8, 0x58, 0x9b, 0x27, 0x17, 0x67, 0x31, 0x96,
  62303. 0x8a, 0x66, 0x39, 0xf4, 0xcc, 0xc1, 0x9f, 0x12, 0x1f, 0x01, 0x30, 0x50,
  62304. 0x16, 0xd6, 0x89, 0x97, 0xa3, 0x66, 0xd7, 0x99, 0x50, 0x09, 0x6e, 0x80,
  62305. 0x87, 0xe4, 0xa2, 0x88, 0xae, 0xb4, 0x23, 0x57, 0x2f, 0x12, 0x60, 0xe7,
  62306. 0x7d, 0x44, 0x2d, 0xad, 0xbe, 0xe9, 0x0d, 0x01, 0x00, 0x01, 0x00, 0xd5,
  62307. 0xdd, 0x62, 0xee, 0xf3, 0x0e, 0xd9, 0x30, 0x0e, 0x38, 0xf3, 0x48, 0xf4,
  62308. 0xc9, 0x8f, 0x8c, 0x20, 0xf7, 0xd3, 0xa8, 0xb3, 0x87, 0x3c, 0x98, 0x5d,
  62309. 0x70, 0xc5, 0x03, 0x76, 0xb7, 0xd5, 0x0b, 0x7b, 0x23, 0x97, 0x6b, 0xe3,
  62310. 0xb5, 0x18, 0xeb, 0x64, 0x55, 0x18, 0xb2, 0x8a, 0x90, 0x1a, 0x8f, 0x0e,
  62311. 0x15, 0xda, 0xb1, 0x8e, 0x7f, 0xee, 0x1f, 0xe0, 0x3b, 0xb9, 0xed, 0xfc,
  62312. 0x4e, 0x3f, 0x78, 0x16, 0x39, 0x95, 0x5f, 0xb7, 0xcb, 0x65, 0x55, 0x72,
  62313. 0x7b, 0x7d, 0x86, 0x2f, 0x8a, 0xe5, 0xee, 0xf7, 0x57, 0x40, 0xf3, 0xc4,
  62314. 0x96, 0x4f, 0x11, 0x4d, 0x85, 0xf9, 0x56, 0xfa, 0x3d, 0xf0, 0xc9, 0xa4,
  62315. 0xec, 0x1e, 0xaa, 0x47, 0x90, 0x53, 0xdf, 0xe1, 0xb7, 0x78, 0x18, 0xeb,
  62316. 0xdd, 0x0d, 0x89, 0xb7, 0xf6, 0x15, 0x0e, 0x55, 0x12, 0xb3, 0x23, 0x17,
  62317. 0x0b, 0x59, 0x6f, 0x83, 0x05, 0x6b, 0xa6, 0xf8, 0x6c, 0x3a, 0x9b, 0x1b,
  62318. 0x50, 0x93, 0x51, 0xea, 0x95, 0x2d, 0x99, 0x96, 0x38, 0x16, 0xfe, 0xfd,
  62319. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x7e, 0x01, 0x00,
  62320. 0x02, 0xdc, 0x00, 0x00, 0x00, 0x02, 0x6a, 0x00, 0x00, 0x72, 0x2d, 0x66,
  62321. 0x3e, 0xf2, 0x36, 0x5a, 0xf2, 0x23, 0x8f, 0x28, 0x09, 0xa9, 0x55, 0x8c,
  62322. 0x8f, 0xc0, 0x0d, 0x61, 0x98, 0x33, 0x56, 0x87, 0x7a, 0xfd, 0xa7, 0x50,
  62323. 0x71, 0x84, 0x2e, 0x41, 0x58, 0x00, 0x87, 0xd9, 0x27, 0xe5, 0x7b, 0xf4,
  62324. 0x6d, 0x84, 0x4e, 0x2e, 0x0c, 0x80, 0x0c, 0xf3, 0x8a, 0x02, 0x4b, 0x99,
  62325. 0x3a, 0x1f, 0x9f, 0x18, 0x7d, 0x1c, 0xec, 0xad, 0x60, 0x54, 0xa6, 0xa3,
  62326. 0x2c, 0x82, 0x5e, 0xf8, 0x8f, 0xae, 0xe1, 0xc4, 0x82, 0x7e, 0x43, 0x43,
  62327. 0xc5, 0x99, 0x49, 0x05, 0xd3, 0xf6, 0xdf, 0xa1, 0xb5, 0x2d, 0x0c, 0x13,
  62328. 0x2f, 0x1e, 0xb6, 0x28, 0x7c, 0x5c, 0xa1, 0x02, 0x6b, 0x8d, 0xa3, 0xeb,
  62329. 0xd4, 0x58, 0xe6, 0xa0, 0x7e, 0x6b, 0xaa, 0x09, 0x43, 0x67, 0x71, 0x87,
  62330. 0xa5, 0xcb, 0x68, 0xf3
  62331. };
  62332. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62333. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62334. wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method), 0);
  62335. /* Fragment msgs */
  62336. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_c, DUMMY_MTU), WOLFSSL_SUCCESS);
  62337. ExpectIntEQ(wolfSSL_dtls_set_mtu(ssl_s, DUMMY_MTU), WOLFSSL_SUCCESS);
  62338. /* Add in some key shares to make the CH long */
  62339. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP256R1),
  62340. WOLFSSL_SUCCESS);
  62341. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP384R1),
  62342. WOLFSSL_SUCCESS);
  62343. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_ECC_SECP521R1),
  62344. WOLFSSL_SUCCESS);
  62345. ExpectIntEQ(wolfSSL_UseKeyShare(ssl_c, WOLFSSL_FFDHE_2048),
  62346. WOLFSSL_SUCCESS);
  62347. ExpectIntEQ(wolfSSL_dtls13_allow_ch_frag(ssl_s, 1), WOLFSSL_SUCCESS);
  62348. /* Reject fragmented first CH */
  62349. ExpectIntEQ(test_dtls_frag_ch_count_records(four_frag_CH,
  62350. sizeof(four_frag_CH)), 4);
  62351. XMEMCPY(test_ctx.s_buff, four_frag_CH, sizeof(four_frag_CH));
  62352. test_ctx.s_len = sizeof(four_frag_CH);
  62353. while (test_ctx.s_len > 0 && EXPECT_SUCCESS()) {
  62354. int s_len = test_ctx.s_len;
  62355. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62356. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62357. /* Fail if we didn't advance the buffer to avoid infinite loops */
  62358. ExpectIntLT(test_ctx.s_len, s_len);
  62359. }
  62360. /* Expect all fragments read */
  62361. ExpectIntEQ(test_ctx.s_len, 0);
  62362. /* Expect quietly dropping fragmented first CH */
  62363. ExpectIntEQ(test_ctx.c_len, 0);
  62364. /* CH1 */
  62365. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62366. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62367. /* Count records. Expect 1 unfragmented CH */
  62368. ExpectIntEQ(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  62369. test_ctx.s_len), 1);
  62370. /* HRR */
  62371. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62372. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62373. /* CH2 */
  62374. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62375. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62376. /* Count records. Expect fragmented CH */
  62377. ExpectIntGT(test_dtls_frag_ch_count_records(test_ctx.s_buff,
  62378. test_ctx.s_len), 1);
  62379. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62380. wolfSSL_free(ssl_c);
  62381. wolfSSL_free(ssl_s);
  62382. wolfSSL_CTX_free(ctx_c);
  62383. wolfSSL_CTX_free(ctx_s);
  62384. ssl_c = ssl_s = NULL;
  62385. ctx_c = ctx_s = NULL;
  62386. #endif
  62387. return EXPECT_RESULT();
  62388. }
  62389. static int test_dtls_empty_keyshare_with_cookie(void)
  62390. {
  62391. EXPECT_DECLS;
  62392. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS13)
  62393. WOLFSSL_CTX *ctx_s = NULL;
  62394. WOLFSSL *ssl_s = NULL;
  62395. struct test_memio_ctx test_ctx;
  62396. unsigned char ch_empty_keyshare_with_cookie[] = {
  62397. 0x16, 0xfe, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01,
  62398. 0x12, 0x01, 0x00, 0x01, 0x06, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x01,
  62399. 0x06, 0xfe, 0xfd, 0xfb, 0x8c, 0x9b, 0x28, 0xae, 0x50, 0x1c, 0x4d, 0xf3,
  62400. 0xb8, 0xcf, 0x4d, 0xd8, 0x7e, 0x93, 0x13, 0x7b, 0x9e, 0xd9, 0xeb, 0xe9,
  62401. 0x13, 0x4b, 0x0d, 0x7f, 0x2e, 0x43, 0x62, 0x8c, 0xe4, 0x57, 0x79, 0x00,
  62402. 0x00, 0x00, 0x36, 0x13, 0x01, 0x13, 0x02, 0x13, 0x03, 0xc0, 0x2c, 0xc0,
  62403. 0x2b, 0xc0, 0x30, 0xc0, 0x2f, 0x00, 0x9f, 0x00, 0x9e, 0xcc, 0xa9, 0xcc,
  62404. 0xa8, 0xcc, 0xaa, 0xc0, 0x27, 0xc0, 0x23, 0xc0, 0x28, 0xc0, 0x24, 0xc0,
  62405. 0x0a, 0xc0, 0x09, 0xc0, 0x14, 0xc0, 0x13, 0x00, 0x6b, 0x00, 0x67, 0x00,
  62406. 0x39, 0x00, 0x33, 0xcc, 0x14, 0xcc, 0x13, 0xcc, 0x15, 0x01, 0x00, 0x00,
  62407. 0xa6, 0x00, 0x2b, 0x00, 0x03, 0x02, 0xfe, 0xfc, 0x00, 0x2c, 0x00, 0x47,
  62408. 0x00, 0x45, 0x20, 0xee, 0x4b, 0x17, 0x70, 0x63, 0xa0, 0x4c, 0x82, 0xbf,
  62409. 0x43, 0x01, 0x7d, 0x8d, 0xc1, 0x1b, 0x4e, 0x9b, 0xa0, 0x3c, 0x53, 0x1f,
  62410. 0xb7, 0xd1, 0x10, 0x81, 0xa8, 0xdf, 0xdf, 0x8c, 0x7f, 0xf3, 0x11, 0x13,
  62411. 0x01, 0x02, 0x3d, 0x3b, 0x7d, 0x14, 0x2c, 0x31, 0xb3, 0x60, 0x72, 0x4d,
  62412. 0xe5, 0x1a, 0xb2, 0xa3, 0x61, 0x77, 0x73, 0x03, 0x40, 0x0e, 0x5f, 0xc5,
  62413. 0x61, 0x38, 0x43, 0x56, 0x21, 0x4a, 0x95, 0xd5, 0x35, 0xa8, 0x0d, 0x00,
  62414. 0x0d, 0x00, 0x2a, 0x00, 0x28, 0x06, 0x03, 0x05, 0x03, 0x04, 0x03, 0x02,
  62415. 0x03, 0xfe, 0x0b, 0xfe, 0x0e, 0xfe, 0xa0, 0xfe, 0xa3, 0xfe, 0xa5, 0x08,
  62416. 0x06, 0x08, 0x0b, 0x08, 0x05, 0x08, 0x0a, 0x08, 0x04, 0x08, 0x09, 0x06,
  62417. 0x01, 0x05, 0x01, 0x04, 0x01, 0x03, 0x01, 0x02, 0x01, 0x00, 0x0a, 0x00,
  62418. 0x18, 0x00, 0x16, 0x00, 0x19, 0x00, 0x18, 0x00, 0x17, 0x00, 0x15, 0x01,
  62419. 0x00, 0x02, 0x3a, 0x02, 0x3c, 0x02, 0x3d, 0x2f, 0x3a, 0x2f, 0x3c, 0x2f,
  62420. 0x3d, 0x00, 0x16, 0x00, 0x00, 0x00, 0x33, 0x00, 0x02, 0x00, 0x00
  62421. };
  62422. DtlsRecordLayerHeader* dtlsRH;
  62423. byte sequence_number[8];
  62424. XMEMSET(&sequence_number, 0, sizeof(sequence_number));
  62425. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62426. XMEMCPY(test_ctx.s_buff, ch_empty_keyshare_with_cookie,
  62427. sizeof(ch_empty_keyshare_with_cookie));
  62428. test_ctx.s_len = sizeof(ch_empty_keyshare_with_cookie);
  62429. ExpectIntEQ(test_memio_setup(&test_ctx, NULL, &ctx_s, NULL, &ssl_s,
  62430. NULL, wolfDTLSv1_3_server_method), 0);
  62431. /* CH1 */
  62432. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62433. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62434. /* Expect an alert. A plaintext alert should be exactly 15 bytes. */
  62435. ExpectIntEQ(test_ctx.c_len, 15);
  62436. dtlsRH = (DtlsRecordLayerHeader*)test_ctx.c_buff;
  62437. ExpectIntEQ(dtlsRH->type, alert);
  62438. ExpectIntEQ(dtlsRH->pvMajor, DTLS_MAJOR);
  62439. ExpectIntEQ(dtlsRH->pvMinor, DTLSv1_2_MINOR);
  62440. sequence_number[7] = 1;
  62441. ExpectIntEQ(XMEMCMP(sequence_number, dtlsRH->sequence_number,
  62442. sizeof(sequence_number)), 0);
  62443. ExpectIntEQ(dtlsRH->length[0], 0);
  62444. ExpectIntEQ(dtlsRH->length[1], 2);
  62445. ExpectIntEQ(test_ctx.c_buff[13], alert_fatal);
  62446. ExpectIntEQ(test_ctx.c_buff[14], illegal_parameter);
  62447. wolfSSL_free(ssl_s);
  62448. wolfSSL_CTX_free(ctx_s);
  62449. #endif
  62450. return EXPECT_RESULT();
  62451. }
  62452. static int test_dtls_old_seq_number(void)
  62453. {
  62454. EXPECT_DECLS;
  62455. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(WOLFSSL_DTLS)
  62456. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  62457. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  62458. struct test_memio_ctx test_ctx;
  62459. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62460. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62461. wolfDTLSv1_2_client_method, wolfDTLSv1_2_server_method), 0);
  62462. /* CH1 */
  62463. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62464. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62465. /* HVR */
  62466. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62467. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62468. /* CH2 */
  62469. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62470. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62471. /* Server first flight */
  62472. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62473. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62474. /* Client second flight */
  62475. ExpectIntEQ(wolfSSL_negotiate(ssl_c), -1);
  62476. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62477. /* Modify the sequence number */
  62478. {
  62479. DtlsRecordLayerHeader* dtlsRH = (DtlsRecordLayerHeader*)test_ctx.s_buff;
  62480. XMEMSET(dtlsRH->sequence_number, 0, sizeof(dtlsRH->sequence_number));
  62481. }
  62482. /* Server second flight */
  62483. ExpectIntEQ(wolfSSL_negotiate(ssl_s), -1);
  62484. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62485. /* Server should not do anything as a pkt was dropped */
  62486. ExpectIntEQ(test_ctx.c_len, 0);
  62487. ExpectIntEQ(test_ctx.s_len, 0);
  62488. /* Trigger rtx */
  62489. ExpectIntEQ(wolfSSL_dtls_got_timeout(ssl_c), WOLFSSL_SUCCESS);
  62490. /* Complete connection */
  62491. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62492. wolfSSL_free(ssl_c);
  62493. wolfSSL_CTX_free(ctx_c);
  62494. wolfSSL_free(ssl_s);
  62495. wolfSSL_CTX_free(ctx_s);
  62496. #endif
  62497. return EXPECT_RESULT();
  62498. }
  62499. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  62500. defined(HAVE_LIBOQS)
  62501. static void test_tls13_pq_groups_ctx_ready(WOLFSSL_CTX* ctx)
  62502. {
  62503. int group = WOLFSSL_KYBER_LEVEL5;
  62504. AssertIntEQ(wolfSSL_CTX_set_groups(ctx, &group, 1), WOLFSSL_SUCCESS);
  62505. }
  62506. static void test_tls13_pq_groups_on_result(WOLFSSL* ssl)
  62507. {
  62508. AssertStrEQ(wolfSSL_get_curve_name(ssl), "KYBER_LEVEL5");
  62509. }
  62510. #endif
  62511. static int test_tls13_pq_groups(void)
  62512. {
  62513. EXPECT_DECLS;
  62514. #if defined(HAVE_IO_TESTS_DEPENDENCIES) && defined(WOLFSSL_TLS13) && \
  62515. defined(HAVE_LIBOQS)
  62516. callback_functions func_cb_client;
  62517. callback_functions func_cb_server;
  62518. XMEMSET(&func_cb_client, 0, sizeof(callback_functions));
  62519. XMEMSET(&func_cb_server, 0, sizeof(callback_functions));
  62520. func_cb_client.method = wolfTLSv1_3_client_method;
  62521. func_cb_server.method = wolfTLSv1_3_server_method;
  62522. func_cb_client.ctx_ready = test_tls13_pq_groups_ctx_ready;
  62523. func_cb_client.on_result = test_tls13_pq_groups_on_result;
  62524. func_cb_server.on_result = test_tls13_pq_groups_on_result;
  62525. test_wolfSSL_client_server_nofail(&func_cb_client, &func_cb_server);
  62526. ExpectIntEQ(func_cb_client.return_code, TEST_SUCCESS);
  62527. ExpectIntEQ(func_cb_server.return_code, TEST_SUCCESS);
  62528. #endif
  62529. return EXPECT_RESULT();
  62530. }
  62531. static int test_tls13_early_data(void)
  62532. {
  62533. EXPECT_DECLS;
  62534. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && \
  62535. defined(WOLFSSL_EARLY_DATA) && defined(HAVE_SESSION_TICKET)
  62536. int written = 0;
  62537. int read = 0;
  62538. size_t i;
  62539. int splitEarlyData;
  62540. char msg[] = "This is early data";
  62541. char msg2[] = "This is client data";
  62542. char msg3[] = "This is server data";
  62543. char msg4[] = "This is server immediate data";
  62544. char msgBuf[50];
  62545. struct {
  62546. method_provider client_meth;
  62547. method_provider server_meth;
  62548. const char* tls_version;
  62549. int isUdp;
  62550. } params[] = {
  62551. #ifdef WOLFSSL_TLS13
  62552. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  62553. "TLS 1.3", 0 },
  62554. #endif
  62555. #ifdef WOLFSSL_DTLS13
  62556. { wolfDTLSv1_3_client_method, wolfDTLSv1_3_server_method,
  62557. "DTLS 1.3", 1 },
  62558. #endif
  62559. };
  62560. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  62561. for (splitEarlyData = 0; splitEarlyData < 2; splitEarlyData++) {
  62562. struct test_memio_ctx test_ctx;
  62563. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  62564. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  62565. WOLFSSL_SESSION *sess = NULL;
  62566. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62567. fprintf(stderr, "\tEarly data with %s\n", params[i].tls_version);
  62568. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c,
  62569. &ssl_s, params[i].client_meth, params[i].server_meth), 0);
  62570. /* Get a ticket so that we can do 0-RTT on the next connection */
  62571. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62572. /* Make sure we read the ticket */
  62573. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), -1);
  62574. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62575. ExpectNotNull(sess = wolfSSL_get1_session(ssl_c));
  62576. wolfSSL_free(ssl_c);
  62577. ssl_c = NULL;
  62578. wolfSSL_free(ssl_s);
  62579. ssl_s = NULL;
  62580. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62581. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62582. params[i].client_meth, params[i].server_meth), 0);
  62583. ExpectIntEQ(wolfSSL_set_session(ssl_c, sess), WOLFSSL_SUCCESS);
  62584. #ifdef WOLFSSL_DTLS13
  62585. if (params[i].isUdp) {
  62586. #ifdef WOLFSSL_DTLS13_NO_HRR_ON_RESUME
  62587. ExpectIntEQ(wolfSSL_dtls13_no_hrr_on_resume(ssl_s, 1), WOLFSSL_SUCCESS);
  62588. #else
  62589. /* Let's test this but we generally don't recommend turning off the
  62590. * cookie exchange */
  62591. ExpectIntEQ(wolfSSL_disable_hrr_cookie(ssl_s), WOLFSSL_SUCCESS);
  62592. #endif
  62593. }
  62594. #endif
  62595. /* Test 0-RTT data */
  62596. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  62597. &written), sizeof(msg));
  62598. ExpectIntEQ(written, sizeof(msg));
  62599. if (splitEarlyData) {
  62600. ExpectIntEQ(wolfSSL_write_early_data(ssl_c, msg, sizeof(msg),
  62601. &written), sizeof(msg));
  62602. ExpectIntEQ(written, sizeof(msg));
  62603. }
  62604. /* Read first 0-RTT data (if split otherwise entire data) */
  62605. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  62606. &read), sizeof(msg));
  62607. ExpectIntEQ(read, sizeof(msg));
  62608. ExpectStrEQ(msg, msgBuf);
  62609. /* Test 0.5-RTT data */
  62610. ExpectIntEQ(wolfSSL_write(ssl_s, msg4, sizeof(msg4)), sizeof(msg4));
  62611. if (splitEarlyData) {
  62612. /* Read second 0-RTT data */
  62613. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  62614. &read), sizeof(msg));
  62615. ExpectIntEQ(read, sizeof(msg));
  62616. ExpectStrEQ(msg, msgBuf);
  62617. }
  62618. if (params[i].isUdp) {
  62619. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  62620. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), APP_DATA_READY);
  62621. /* Read server 0.5-RTT data */
  62622. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  62623. ExpectStrEQ(msg4, msgBuf);
  62624. /* Complete handshake */
  62625. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  62626. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62627. /* Use wolfSSL_is_init_finished to check if handshake is complete. Normally
  62628. * a user would loop until it is true but here we control both sides so we
  62629. * just assert the expected value. wolfSSL_read_early_data does not provide
  62630. * handshake status to us with non-blocking IO and we can't use
  62631. * wolfSSL_accept as TLS layer may return ZERO_RETURN due to early data
  62632. * parsing logic. */
  62633. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  62634. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  62635. &read), 0);
  62636. ExpectIntEQ(read, 0);
  62637. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  62638. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  62639. }
  62640. else {
  62641. ExpectIntEQ(wolfSSL_connect(ssl_c), WOLFSSL_SUCCESS);
  62642. ExpectFalse(wolfSSL_is_init_finished(ssl_s));
  62643. ExpectIntEQ(wolfSSL_read_early_data(ssl_s, msgBuf, sizeof(msgBuf),
  62644. &read), 0);
  62645. ExpectIntEQ(read, 0);
  62646. ExpectTrue(wolfSSL_is_init_finished(ssl_s));
  62647. /* Read server 0.5-RTT data */
  62648. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg4));
  62649. ExpectStrEQ(msg4, msgBuf);
  62650. }
  62651. /* Test bi-directional write */
  62652. ExpectIntEQ(wolfSSL_write(ssl_c, msg2, sizeof(msg2)), sizeof(msg2));
  62653. ExpectIntEQ(wolfSSL_read(ssl_s, msgBuf, sizeof(msgBuf)), sizeof(msg2));
  62654. ExpectStrEQ(msg2, msgBuf);
  62655. ExpectIntEQ(wolfSSL_write(ssl_s, msg3, sizeof(msg3)), sizeof(msg3));
  62656. ExpectIntEQ(wolfSSL_read(ssl_c, msgBuf, sizeof(msgBuf)), sizeof(msg3));
  62657. ExpectStrEQ(msg3, msgBuf);
  62658. ExpectTrue(wolfSSL_session_reused(ssl_c));
  62659. ExpectTrue(wolfSSL_session_reused(ssl_s));
  62660. wolfSSL_SESSION_free(sess);
  62661. wolfSSL_free(ssl_c);
  62662. wolfSSL_free(ssl_s);
  62663. wolfSSL_CTX_free(ctx_c);
  62664. wolfSSL_CTX_free(ctx_s);
  62665. }
  62666. }
  62667. #endif
  62668. return EXPECT_RESULT();
  62669. }
  62670. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  62671. static int test_self_signed_stapling_client_v1_ctx_ready(WOLFSSL_CTX* ctx)
  62672. {
  62673. EXPECT_DECLS;
  62674. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  62675. ExpectIntEQ(wolfSSL_CTX_UseOCSPStapling(ctx, WOLFSSL_CSR_OCSP,
  62676. WOLFSSL_CSR_OCSP_USE_NONCE), 1);
  62677. return EXPECT_RESULT();
  62678. }
  62679. #endif
  62680. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  62681. static int test_self_signed_stapling_client_v2_ctx_ready(WOLFSSL_CTX* ctx)
  62682. {
  62683. EXPECT_DECLS;
  62684. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  62685. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP,
  62686. WOLFSSL_CSR2_OCSP_USE_NONCE), 1);
  62687. return EXPECT_RESULT();
  62688. }
  62689. static int test_self_signed_stapling_client_v2_multi_ctx_ready(WOLFSSL_CTX* ctx)
  62690. {
  62691. EXPECT_DECLS;
  62692. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  62693. ExpectIntEQ(wolfSSL_CTX_UseOCSPStaplingV2(ctx, WOLFSSL_CSR2_OCSP_MULTI,
  62694. 0), 1);
  62695. return EXPECT_RESULT();
  62696. }
  62697. #endif
  62698. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  62699. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  62700. static int test_self_signed_stapling_server_ctx_ready(WOLFSSL_CTX* ctx)
  62701. {
  62702. EXPECT_DECLS;
  62703. ExpectIntEQ(wolfSSL_CTX_EnableOCSPStapling(ctx), 1);
  62704. return EXPECT_RESULT();
  62705. }
  62706. #endif
  62707. static int test_self_signed_stapling(void)
  62708. {
  62709. EXPECT_DECLS;
  62710. #if defined(HAVE_CERTIFICATE_STATUS_REQUEST) \
  62711. || defined(HAVE_CERTIFICATE_STATUS_REQUEST_V2)
  62712. test_ssl_cbf client_cbf;
  62713. test_ssl_cbf server_cbf;
  62714. size_t i;
  62715. struct {
  62716. method_provider client_meth;
  62717. method_provider server_meth;
  62718. ctx_cb client_ctx;
  62719. const char* tls_version;
  62720. } params[] = {
  62721. #if defined(WOLFSSL_TLS13) && defined(HAVE_CERTIFICATE_STATUS_REQUEST)
  62722. { wolfTLSv1_3_client_method, wolfTLSv1_3_server_method,
  62723. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_3 v1" },
  62724. #endif
  62725. #ifndef WOLFSSL_NO_TLS12
  62726. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST
  62727. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  62728. test_self_signed_stapling_client_v1_ctx_ready, "TLSv1_2 v1" },
  62729. #endif
  62730. #ifdef HAVE_CERTIFICATE_STATUS_REQUEST_V2
  62731. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  62732. test_self_signed_stapling_client_v2_ctx_ready, "TLSv1_2 v2" },
  62733. { wolfTLSv1_2_client_method, wolfTLSv1_2_server_method,
  62734. test_self_signed_stapling_client_v2_multi_ctx_ready,
  62735. "TLSv1_2 v2 multi" },
  62736. #endif
  62737. #endif
  62738. };
  62739. for (i = 0; i < sizeof(params)/sizeof(*params) && !EXPECT_FAIL(); i++) {
  62740. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  62741. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  62742. printf("\nTesting self-signed cert with status request: %s\n",
  62743. params[i].tls_version);
  62744. client_cbf.method = params[i].client_meth;
  62745. client_cbf.ctx_ready = params[i].client_ctx;
  62746. server_cbf.method = params[i].server_meth;
  62747. server_cbf.certPemFile = "certs/ca-cert.pem";
  62748. server_cbf.keyPemFile = "certs/ca-key.pem";
  62749. server_cbf.ctx_ready = test_self_signed_stapling_server_ctx_ready;
  62750. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  62751. &server_cbf, NULL), TEST_SUCCESS);
  62752. }
  62753. #endif
  62754. return EXPECT_RESULT();
  62755. }
  62756. static int test_tls_multi_handshakes_one_record(void)
  62757. {
  62758. EXPECT_DECLS;
  62759. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  62760. struct test_memio_ctx test_ctx;
  62761. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  62762. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  62763. RecordLayerHeader* rh = NULL;
  62764. byte *len ;
  62765. int newRecIdx = RECORD_HEADER_SZ;
  62766. int idx = 0;
  62767. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62768. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62769. wolfTLS_client_method, wolfTLSv1_2_server_method), 0);
  62770. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  62771. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62772. ExpectIntEQ(wolfSSL_accept(ssl_s), -1);
  62773. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62774. /* Combine server handshake msgs into one record */
  62775. while (idx < test_ctx.c_len) {
  62776. word16 recLen;
  62777. rh = (RecordLayerHeader*)(test_ctx.c_buff + idx);
  62778. len = &rh->length[0];
  62779. ato16((const byte*)len, &recLen);
  62780. idx += RECORD_HEADER_SZ;
  62781. XMEMMOVE(test_ctx.c_buff + newRecIdx, test_ctx.c_buff + idx,
  62782. (size_t)recLen);
  62783. newRecIdx += recLen;
  62784. idx += recLen;
  62785. }
  62786. rh = (RecordLayerHeader*)(test_ctx.c_buff);
  62787. len = &rh->length[0];
  62788. c16toa(newRecIdx - RECORD_HEADER_SZ, len);
  62789. test_ctx.c_len = newRecIdx;
  62790. ExpectIntEQ(wolfSSL_connect(ssl_c), -1);
  62791. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62792. wolfSSL_free(ssl_c);
  62793. wolfSSL_free(ssl_s);
  62794. wolfSSL_CTX_free(ctx_c);
  62795. wolfSSL_CTX_free(ctx_s);
  62796. #endif
  62797. return EXPECT_RESULT();
  62798. }
  62799. static int test_write_dup(void)
  62800. {
  62801. EXPECT_DECLS;
  62802. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && defined(HAVE_WRITE_DUP)
  62803. size_t i, j;
  62804. char hiWorld[] = "dup message";
  62805. char readData[sizeof(hiWorld) + 5];
  62806. struct {
  62807. method_provider client_meth;
  62808. method_provider server_meth;
  62809. const char* version_name;
  62810. int version;
  62811. } methods[] = {
  62812. #ifndef WOLFSSL_NO_TLS12
  62813. {wolfTLSv1_2_client_method, wolfTLSv1_2_server_method, "TLS 1.2", WOLFSSL_TLSV1_2},
  62814. #endif
  62815. #ifdef WOLFSSL_TLS13
  62816. {wolfTLSv1_3_client_method, wolfTLSv1_3_server_method, "TLS 1.3", WOLFSSL_TLSV1_3},
  62817. #endif
  62818. };
  62819. struct {
  62820. const char* cipher;
  62821. int version;
  62822. } ciphers[] = {
  62823. /* For simplicity the macros are copied from internal.h */
  62824. /* TLS 1.2 */
  62825. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305) && !defined(NO_SHA256)
  62826. #if defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)
  62827. #ifndef NO_RSA
  62828. {"ECDHE-RSA-CHACHA20-POLY1305", WOLFSSL_TLSV1_2},
  62829. #endif
  62830. #endif
  62831. #if !defined(NO_DH) && !defined(NO_RSA) && !defined(NO_TLS_DH)
  62832. {"DHE-RSA-CHACHA20-POLY1305", WOLFSSL_TLSV1_2},
  62833. #endif
  62834. #endif
  62835. #if !defined(NO_DH) && !defined(NO_AES) && !defined(NO_TLS) && \
  62836. !defined(NO_RSA) && defined(HAVE_AESGCM) && !defined(NO_TLS_DH)
  62837. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  62838. {"DHE-RSA-AES128-GCM-SHA256", WOLFSSL_TLSV1_2},
  62839. #endif
  62840. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  62841. {"DHE-RSA-AES256-GCM-SHA384", WOLFSSL_TLSV1_2},
  62842. #endif
  62843. #endif
  62844. #if (defined(HAVE_ECC) || defined(HAVE_CURVE25519) || defined(HAVE_CURVE448)) \
  62845. && !defined(NO_TLS) && !defined(NO_AES)
  62846. #ifdef HAVE_AESGCM
  62847. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  62848. #ifndef NO_RSA
  62849. {"ECDHE-RSA-AES128-GCM-SHA256", WOLFSSL_TLSV1_2},
  62850. #endif
  62851. #endif
  62852. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  62853. #ifndef NO_RSA
  62854. {"ECDHE-RSA-AES256-GCM-SHA384", WOLFSSL_TLSV1_2},
  62855. #endif
  62856. #endif
  62857. #endif
  62858. #endif
  62859. /* TLS 1.3 */
  62860. #ifdef WOLFSSL_TLS13
  62861. #ifdef HAVE_AESGCM
  62862. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  62863. {"TLS13-AES128-GCM-SHA256", WOLFSSL_TLSV1_3},
  62864. #endif
  62865. #if defined(WOLFSSL_SHA384) && defined(WOLFSSL_AES_256)
  62866. {"TLS13-AES256-GCM-SHA384", WOLFSSL_TLSV1_3},
  62867. #endif
  62868. #endif
  62869. #if defined(HAVE_CHACHA) && defined(HAVE_POLY1305)
  62870. #ifndef NO_SHA256
  62871. {"TLS13-CHACHA20-POLY1305-SHA256", WOLFSSL_TLSV1_3},
  62872. #endif
  62873. #endif
  62874. #ifdef HAVE_AESCCM
  62875. #if !defined(NO_SHA256) && defined(WOLFSSL_AES_128)
  62876. {"TLS13-AES128-CCM-SHA256", WOLFSSL_TLSV1_3},
  62877. #endif
  62878. #endif
  62879. #endif
  62880. };
  62881. for (i = 0; i < XELEM_CNT(methods); i++) {
  62882. for (j = 0; j < XELEM_CNT(ciphers) && !EXPECT_FAIL(); j++) {
  62883. struct test_memio_ctx test_ctx;
  62884. WOLFSSL_CTX *ctx_c = NULL, *ctx_s = NULL;
  62885. WOLFSSL *ssl_c = NULL, *ssl_s = NULL;
  62886. WOLFSSL *ssl_c2 = NULL;
  62887. if (methods[i].version != ciphers[j].version)
  62888. continue;
  62889. if (i == 0 && j == 0)
  62890. printf("\n");
  62891. printf("Testing %s with %s... ", methods[i].version_name,
  62892. ciphers[j].cipher);
  62893. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62894. test_ctx.c_ciphers = test_ctx.s_ciphers = ciphers[j].cipher;
  62895. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62896. methods[i].client_meth, methods[i].server_meth), 0);
  62897. ExpectIntEQ(test_memio_do_handshake(ssl_c, ssl_s, 10, NULL), 0);
  62898. ExpectNotNull(ssl_c2 = wolfSSL_write_dup(ssl_c));
  62899. ExpectIntEQ(wolfSSL_write(ssl_c, hiWorld, sizeof(hiWorld)),
  62900. WRITE_DUP_WRITE_E);
  62901. ExpectIntEQ(wolfSSL_write(ssl_c2, hiWorld, sizeof(hiWorld)),
  62902. sizeof(hiWorld));
  62903. ExpectIntEQ(wolfSSL_read(ssl_s, readData, sizeof(readData)),
  62904. sizeof(hiWorld));
  62905. ExpectIntEQ(wolfSSL_write(ssl_s, hiWorld, sizeof(hiWorld)),
  62906. sizeof(hiWorld));
  62907. ExpectIntEQ(wolfSSL_read(ssl_c2, readData, sizeof(readData)),
  62908. WRITE_DUP_READ_E);
  62909. ExpectIntEQ(wolfSSL_read(ssl_c, readData, sizeof(readData)),
  62910. sizeof(hiWorld));
  62911. if (EXPECT_SUCCESS())
  62912. printf("ok\n");
  62913. else
  62914. printf("failed\n");
  62915. wolfSSL_free(ssl_c);
  62916. wolfSSL_free(ssl_c2);
  62917. wolfSSL_free(ssl_s);
  62918. wolfSSL_CTX_free(ctx_c);
  62919. wolfSSL_CTX_free(ctx_s);
  62920. }
  62921. }
  62922. #endif
  62923. return EXPECT_RESULT();
  62924. }
  62925. static int test_read_write_hs(void)
  62926. {
  62927. EXPECT_DECLS;
  62928. #if defined(HAVE_MANUAL_MEMIO_TESTS_DEPENDENCIES) && !defined(WOLFSSL_NO_TLS12)
  62929. WOLFSSL_CTX *ctx_s = NULL, *ctx_c = NULL;
  62930. WOLFSSL *ssl_s = NULL, *ssl_c = NULL;
  62931. struct test_memio_ctx test_ctx;
  62932. uint8_t test_buffer[16];
  62933. unsigned int test;
  62934. /* test == 0 : client writes, server reads */
  62935. /* test == 1 : server writes, client reads */
  62936. for (test = 0; test < 2; test++) {
  62937. XMEMSET(&test_ctx, 0, sizeof(test_ctx));
  62938. ExpectIntEQ(test_memio_setup(&test_ctx, &ctx_c, &ctx_s, &ssl_c, &ssl_s,
  62939. wolfTLSv1_2_client_method,
  62940. wolfTLSv1_2_server_method), 0);
  62941. ExpectIntEQ(wolfSSL_set_group_messages(ssl_s), WOLFSSL_SUCCESS);
  62942. /* CH -> */
  62943. if (test == 0) {
  62944. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  62945. } else {
  62946. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  62947. sizeof(test_buffer)), -1);
  62948. }
  62949. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62950. /* <- SH + SKE + SHD */
  62951. if (test == 0) {
  62952. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  62953. sizeof(test_buffer)), -1);
  62954. } else {
  62955. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), -1);
  62956. }
  62957. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_READ);
  62958. /* -> CKE + CLIENT FINISHED */
  62959. if (test == 0) {
  62960. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  62961. } else {
  62962. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  62963. sizeof(test_buffer)), -1);
  62964. }
  62965. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62966. /* abide clang static analyzer */
  62967. if (ssl_s != NULL) {
  62968. /* disable group message to separate sending of ChangeCipherspec
  62969. * from Finished */
  62970. ssl_s->options.groupMessages = 0;
  62971. }
  62972. /* allow writing of CS, but not FINISHED */
  62973. test_ctx.c_len = TEST_MEMIO_BUF_SZ - 6;
  62974. /* <- CS */
  62975. if (test == 0) {
  62976. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  62977. sizeof(test_buffer)), -1);
  62978. } else {
  62979. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), -1);
  62980. }
  62981. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1), WOLFSSL_ERROR_WANT_WRITE);
  62982. /* move CS message where the client can read it */
  62983. memmove(test_ctx.c_buff,
  62984. (test_ctx.c_buff + TEST_MEMIO_BUF_SZ - 6), 6);
  62985. test_ctx.c_len = 6;
  62986. /* read CS */
  62987. if (test == 0) {
  62988. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), -1);
  62989. } else {
  62990. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  62991. sizeof(test_buffer)), -1);
  62992. }
  62993. ExpectIntEQ(wolfSSL_get_error(ssl_c, -1), WOLFSSL_ERROR_WANT_READ);
  62994. ExpectIntEQ(test_ctx.c_len, 0);
  62995. if (test == 0) {
  62996. /* send SERVER FINISHED */
  62997. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  62998. sizeof(test_buffer)), -1);
  62999. ExpectIntEQ(wolfSSL_get_error(ssl_s, -1),
  63000. WOLFSSL_ERROR_WANT_READ);
  63001. } else {
  63002. /* send SERVER FINISHED + App Data */
  63003. ExpectIntEQ(wolfSSL_write(ssl_s, "hello", 5), 5);
  63004. }
  63005. ExpectIntGT(test_ctx.c_len, 0);
  63006. /* Send and receive the data */
  63007. if (test == 0) {
  63008. ExpectIntEQ(wolfSSL_write(ssl_c, "hello", 5), 5);
  63009. ExpectIntEQ(wolfSSL_read(ssl_s, test_buffer,
  63010. sizeof(test_buffer)), 5);
  63011. } else {
  63012. ExpectIntEQ(wolfSSL_read(ssl_c, test_buffer,
  63013. sizeof(test_buffer)), 5);
  63014. }
  63015. ExpectBufEQ(test_buffer, "hello", 5);
  63016. wolfSSL_free(ssl_c);
  63017. wolfSSL_free(ssl_s);
  63018. wolfSSL_CTX_free(ctx_c);
  63019. wolfSSL_CTX_free(ctx_s);
  63020. ssl_c = ssl_s = NULL;
  63021. ctx_c = ctx_s = NULL;
  63022. }
  63023. #endif
  63024. return EXPECT_RESULT();
  63025. }
  63026. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA)
  63027. static const char* test_get_signature_nid_siglag;
  63028. static int test_get_signature_nid_sig;
  63029. static int test_get_signature_nid_hash;
  63030. static int test_get_signature_nid_ssl_ready(WOLFSSL* ssl)
  63031. {
  63032. EXPECT_DECLS;
  63033. ExpectIntEQ(wolfSSL_set_cipher_list(ssl, "ALL"), WOLFSSL_SUCCESS);
  63034. if (!wolfSSL_is_server(ssl)) {
  63035. ExpectIntEQ(wolfSSL_set1_sigalgs_list(ssl,
  63036. test_get_signature_nid_siglag), WOLFSSL_SUCCESS);
  63037. }
  63038. return EXPECT_RESULT();
  63039. }
  63040. static int test_get_signature_nid_on_hs_client(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  63041. {
  63042. EXPECT_DECLS;
  63043. int nid = 0;
  63044. (void)ctx;
  63045. if (XSTRSTR(wolfSSL_get_cipher(*ssl), "TLS_RSA_") == NULL) {
  63046. ExpectIntEQ(SSL_get_peer_signature_type_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  63047. ExpectIntEQ(nid, test_get_signature_nid_sig);
  63048. ExpectIntEQ(SSL_get_peer_signature_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  63049. ExpectIntEQ(nid, test_get_signature_nid_hash);
  63050. }
  63051. else /* No sigalg info on static ciphersuite */
  63052. return TEST_SUCCESS;
  63053. return EXPECT_RESULT();
  63054. }
  63055. static int test_get_signature_nid_on_hs_server(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  63056. {
  63057. EXPECT_DECLS;
  63058. int nid = 0;
  63059. (void)ctx;
  63060. ExpectIntEQ(SSL_get_signature_type_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  63061. ExpectIntEQ(nid, test_get_signature_nid_sig);
  63062. ExpectIntEQ(SSL_get_signature_nid(*ssl, &nid), WOLFSSL_SUCCESS);
  63063. ExpectIntEQ(nid, test_get_signature_nid_hash);
  63064. return EXPECT_RESULT();
  63065. }
  63066. #endif
  63067. static int test_get_signature_nid(void)
  63068. {
  63069. EXPECT_DECLS;
  63070. #if defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES) && defined(OPENSSL_EXTRA)
  63071. test_ssl_cbf client_cbf;
  63072. test_ssl_cbf server_cbf;
  63073. size_t i;
  63074. #define TGSN_TLS12_RSA(sigalg, sig_nid, hash_nid) \
  63075. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_2, svrCertFile, svrKeyFile, \
  63076. caCertFile }
  63077. #define TGSN_TLS12_ECDSA(sigalg, sig_nid, hash_nid) \
  63078. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_2, eccCertFile, eccKeyFile, \
  63079. caEccCertFile }
  63080. #define TGSN_TLS13_RSA(sigalg, sig_nid, hash_nid) \
  63081. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, svrCertFile, svrKeyFile, \
  63082. caCertFile }
  63083. #define TGSN_TLS13_ECDSA(sigalg, sig_nid, hash_nid) \
  63084. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, eccCertFile, eccKeyFile, \
  63085. caEccCertFile }
  63086. #define TGSN_TLS13_ED25519(sigalg, sig_nid, hash_nid) \
  63087. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, edCertFile, edKeyFile, \
  63088. caEdCertFile }
  63089. #define TGSN_TLS13_ED448(sigalg, sig_nid, hash_nid) \
  63090. { sigalg, sig_nid, hash_nid, WOLFSSL_TLSV1_3, ed448CertFile, ed448KeyFile, \
  63091. caEd448CertFile }
  63092. struct {
  63093. const char* siglag;
  63094. int sig_nid;
  63095. int hash_nid;
  63096. int tls_ver;
  63097. const char* server_cert;
  63098. const char* server_key;
  63099. const char* client_ca;
  63100. } params[] = {
  63101. #ifndef NO_RSA
  63102. #ifndef NO_SHA256
  63103. TGSN_TLS12_RSA("RSA+SHA256", NID_rsaEncryption, NID_sha256),
  63104. #ifdef WC_RSA_PSS
  63105. TGSN_TLS12_RSA("RSA-PSS+SHA256", NID_rsassaPss, NID_sha256),
  63106. TGSN_TLS13_RSA("RSA-PSS+SHA256", NID_rsassaPss, NID_sha256),
  63107. #endif
  63108. #endif
  63109. #ifdef WOLFSSL_SHA512
  63110. TGSN_TLS12_RSA("RSA+SHA512", NID_rsaEncryption, NID_sha512),
  63111. #ifdef WC_RSA_PSS
  63112. TGSN_TLS12_RSA("RSA-PSS+SHA512", NID_rsassaPss, NID_sha512),
  63113. TGSN_TLS13_RSA("RSA-PSS+SHA512", NID_rsassaPss, NID_sha512),
  63114. #endif
  63115. #endif
  63116. #endif
  63117. #ifdef HAVE_ECC
  63118. #ifndef NO_SHA256
  63119. TGSN_TLS12_ECDSA("ECDSA+SHA256", NID_X9_62_id_ecPublicKey, NID_sha256),
  63120. TGSN_TLS13_ECDSA("ECDSA+SHA256", NID_X9_62_id_ecPublicKey, NID_sha256),
  63121. #endif
  63122. #endif
  63123. #ifdef HAVE_ED25519
  63124. TGSN_TLS13_ED25519("ED25519", NID_ED25519, NID_sha512),
  63125. #endif
  63126. #ifdef HAVE_ED448
  63127. TGSN_TLS13_ED448("ED448", NID_ED448, NID_sha512),
  63128. #endif
  63129. };
  63130. printf("\n");
  63131. for (i = 0; i < XELEM_CNT(params) && !EXPECT_FAIL(); i++) {
  63132. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  63133. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  63134. printf("Testing %s with %s...", tls_desc[params[i].tls_ver],
  63135. params[i].siglag);
  63136. switch (params[i].tls_ver) {
  63137. #ifndef WOLFSSL_NO_TLS12
  63138. case WOLFSSL_TLSV1_2:
  63139. client_cbf.method = wolfTLSv1_2_client_method;
  63140. server_cbf.method = wolfTLSv1_2_server_method;
  63141. break;
  63142. #endif
  63143. #ifdef WOLFSSL_TLS13
  63144. case WOLFSSL_TLSV1_3:
  63145. client_cbf.method = wolfTLSv1_3_client_method;
  63146. server_cbf.method = wolfTLSv1_3_server_method;
  63147. break;
  63148. #endif
  63149. default:
  63150. printf("skipping\n");
  63151. continue;
  63152. }
  63153. test_get_signature_nid_siglag = params[i].siglag;
  63154. test_get_signature_nid_sig = params[i].sig_nid;
  63155. test_get_signature_nid_hash = params[i].hash_nid;
  63156. client_cbf.ssl_ready = test_get_signature_nid_ssl_ready;
  63157. server_cbf.ssl_ready = test_get_signature_nid_ssl_ready;
  63158. client_cbf.on_handshake = test_get_signature_nid_on_hs_client;
  63159. server_cbf.on_handshake = test_get_signature_nid_on_hs_server;
  63160. server_cbf.certPemFile = params[i].server_cert;
  63161. server_cbf.keyPemFile = params[i].server_key;
  63162. client_cbf.caPemFile = params[i].client_ca;
  63163. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  63164. &server_cbf, NULL), TEST_SUCCESS);
  63165. if (EXPECT_SUCCESS())
  63166. printf("passed\n");
  63167. }
  63168. #endif
  63169. return EXPECT_RESULT();
  63170. }
  63171. #if !defined(NO_CERTS) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  63172. static word32 test_tls_cert_store_unchanged_HashCaTable(Signer** caTable)
  63173. {
  63174. #ifndef NO_MD5
  63175. enum wc_HashType hashType = WC_HASH_TYPE_MD5;
  63176. #elif !defined(NO_SHA)
  63177. enum wc_HashType hashType = WC_HASH_TYPE_SHA;
  63178. #elif !defined(NO_SHA256)
  63179. enum wc_HashType hashType = WC_HASH_TYPE_SHA256;
  63180. #else
  63181. #error "We need a digest to hash the Signer object"
  63182. #endif
  63183. byte hashBuf[WC_MAX_DIGEST_SIZE];
  63184. wc_HashAlg hash;
  63185. size_t i;
  63186. AssertIntEQ(wc_HashInit(&hash, hashType), 0);
  63187. for (i = 0; i < CA_TABLE_SIZE; i++) {
  63188. Signer* cur;
  63189. for (cur = caTable[i]; cur != NULL; cur = cur->next)
  63190. AssertIntEQ(wc_HashUpdate(&hash, hashType, (byte*)cur,
  63191. sizeof(*cur)), 0);
  63192. }
  63193. AssertIntEQ(wc_HashFinal(&hash, hashType, hashBuf), 0);
  63194. AssertIntEQ(wc_HashFree(&hash, hashType), 0);
  63195. return MakeWordFromHash(hashBuf);
  63196. }
  63197. static word32 test_tls_cert_store_unchanged_before_hashes[2];
  63198. static size_t test_tls_cert_store_unchanged_before_hashes_idx;
  63199. static word32 test_tls_cert_store_unchanged_after_hashes[2];
  63200. static size_t test_tls_cert_store_unchanged_after_hashes_idx;
  63201. static int test_tls_cert_store_unchanged_ctx_ready(WOLFSSL_CTX* ctx)
  63202. {
  63203. EXPECT_DECLS;
  63204. ExpectIntNE(test_tls_cert_store_unchanged_before_hashes
  63205. [test_tls_cert_store_unchanged_before_hashes_idx++] =
  63206. test_tls_cert_store_unchanged_HashCaTable(ctx->cm->caTable), 0);
  63207. wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_PEER |
  63208. WOLFSSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0);
  63209. return EXPECT_RESULT();
  63210. }
  63211. static int test_tls_cert_store_unchanged_ctx_cleanup(WOLFSSL_CTX* ctx)
  63212. {
  63213. EXPECT_DECLS;
  63214. ExpectIntEQ(wolfSSL_CTX_UnloadIntermediateCerts(ctx), WOLFSSL_SUCCESS);
  63215. ExpectIntNE(test_tls_cert_store_unchanged_after_hashes
  63216. [test_tls_cert_store_unchanged_after_hashes_idx++] =
  63217. test_tls_cert_store_unchanged_HashCaTable(ctx->cm->caTable), 0);
  63218. return EXPECT_RESULT();
  63219. }
  63220. static int test_tls_cert_store_unchanged_on_hs(WOLFSSL_CTX **ctx, WOLFSSL **ssl)
  63221. {
  63222. EXPECT_DECLS;
  63223. WOLFSSL_CERT_MANAGER* cm;
  63224. (void)ssl;
  63225. /* WARNING: this approach bypasses the reference counter check in
  63226. * wolfSSL_CTX_UnloadIntermediateCerts. It is not recommended as it may
  63227. * cause unexpected behaviour when other active connections try accessing
  63228. * the caTable. */
  63229. ExpectNotNull(cm = wolfSSL_CTX_GetCertManager(*ctx));
  63230. ExpectIntEQ(wolfSSL_CertManagerUnloadIntermediateCerts(cm),
  63231. WOLFSSL_SUCCESS);
  63232. ExpectIntNE(test_tls_cert_store_unchanged_after_hashes
  63233. [test_tls_cert_store_unchanged_after_hashes_idx++] =
  63234. test_tls_cert_store_unchanged_HashCaTable((*ctx)->cm->caTable), 0);
  63235. return EXPECT_RESULT();
  63236. }
  63237. static int test_tls_cert_store_unchanged_ssl_ready(WOLFSSL* ssl)
  63238. {
  63239. EXPECT_DECLS;
  63240. WOLFSSL_CTX* ctx;
  63241. ExpectNotNull(ctx = wolfSSL_get_SSL_CTX(ssl));
  63242. return EXPECT_RESULT();
  63243. }
  63244. #endif
  63245. static int test_tls_cert_store_unchanged(void)
  63246. {
  63247. EXPECT_DECLS;
  63248. #if !defined(NO_CERTS) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  63249. test_ssl_cbf client_cbf;
  63250. test_ssl_cbf server_cbf;
  63251. int i;
  63252. for (i = 0; i < 2; i++) {
  63253. XMEMSET(&client_cbf, 0, sizeof(client_cbf));
  63254. XMEMSET(&server_cbf, 0, sizeof(server_cbf));
  63255. test_tls_cert_store_unchanged_before_hashes_idx = 0;
  63256. XMEMSET(test_tls_cert_store_unchanged_before_hashes, 0,
  63257. sizeof(test_tls_cert_store_unchanged_before_hashes));
  63258. test_tls_cert_store_unchanged_after_hashes_idx = 0;
  63259. XMEMSET(test_tls_cert_store_unchanged_after_hashes, 0,
  63260. sizeof(test_tls_cert_store_unchanged_after_hashes));
  63261. client_cbf.ctx_ready = test_tls_cert_store_unchanged_ctx_ready;
  63262. server_cbf.ctx_ready = test_tls_cert_store_unchanged_ctx_ready;
  63263. client_cbf.ssl_ready = test_tls_cert_store_unchanged_ssl_ready;
  63264. server_cbf.ssl_ready = test_tls_cert_store_unchanged_ssl_ready;
  63265. switch (i) {
  63266. case 0:
  63267. client_cbf.on_ctx_cleanup =
  63268. test_tls_cert_store_unchanged_ctx_cleanup;
  63269. server_cbf.on_ctx_cleanup =
  63270. test_tls_cert_store_unchanged_ctx_cleanup;
  63271. break;
  63272. case 1:
  63273. client_cbf.on_handshake = test_tls_cert_store_unchanged_on_hs;
  63274. server_cbf.on_handshake = test_tls_cert_store_unchanged_on_hs;
  63275. break;
  63276. default:
  63277. Fail(("Should not enter here"), ("Entered here"));
  63278. }
  63279. client_cbf.certPemFile = "certs/intermediate/client-chain.pem";
  63280. server_cbf.certPemFile = "certs/intermediate/server-chain.pem";
  63281. server_cbf.caPemFile = caCertFile;
  63282. ExpectIntEQ(test_wolfSSL_client_server_nofail_memio(&client_cbf,
  63283. &server_cbf, NULL), TEST_SUCCESS);
  63284. ExpectBufEQ(test_tls_cert_store_unchanged_before_hashes,
  63285. test_tls_cert_store_unchanged_after_hashes,
  63286. sizeof(test_tls_cert_store_unchanged_after_hashes));
  63287. }
  63288. #endif
  63289. return EXPECT_RESULT();
  63290. }
  63291. /*----------------------------------------------------------------------------*
  63292. | Main
  63293. *----------------------------------------------------------------------------*/
  63294. typedef int (*TEST_FUNC)(void);
  63295. typedef struct {
  63296. const char *name;
  63297. TEST_FUNC func;
  63298. byte run:1;
  63299. byte fail:1;
  63300. } TEST_CASE;
  63301. #define TEST_DECL(func) { #func, func, 0, 0 }
  63302. int testAll = 1;
  63303. TEST_CASE testCases[] = {
  63304. TEST_DECL(test_fileAccess),
  63305. /*********************************
  63306. * wolfcrypt
  63307. *********************************/
  63308. TEST_DECL(test_ForceZero),
  63309. TEST_DECL(test_wolfCrypt_Init),
  63310. TEST_DECL(test_wc_LoadStaticMemory_ex),
  63311. /* Locking with Compat Mutex */
  63312. TEST_DECL(test_wc_SetMutexCb),
  63313. TEST_DECL(test_wc_LockMutex_ex),
  63314. /* Digests */
  63315. TEST_DECL(test_wc_InitMd5),
  63316. TEST_DECL(test_wc_Md5Update),
  63317. TEST_DECL(test_wc_Md5Final),
  63318. TEST_DECL(test_wc_InitSha),
  63319. TEST_DECL(test_wc_ShaUpdate),
  63320. TEST_DECL(test_wc_ShaFinal),
  63321. TEST_DECL(test_wc_InitSha256),
  63322. TEST_DECL(test_wc_Sha256Update),
  63323. TEST_DECL(test_wc_Sha256Final),
  63324. TEST_DECL(test_wc_Sha256FinalRaw),
  63325. TEST_DECL(test_wc_Sha256GetFlags),
  63326. TEST_DECL(test_wc_Sha256Free),
  63327. TEST_DECL(test_wc_Sha256GetHash),
  63328. TEST_DECL(test_wc_Sha256Copy),
  63329. TEST_DECL(test_wc_InitSha224),
  63330. TEST_DECL(test_wc_Sha224Update),
  63331. TEST_DECL(test_wc_Sha224Final),
  63332. TEST_DECL(test_wc_Sha224SetFlags),
  63333. TEST_DECL(test_wc_Sha224GetFlags),
  63334. TEST_DECL(test_wc_Sha224Free),
  63335. TEST_DECL(test_wc_Sha224GetHash),
  63336. TEST_DECL(test_wc_Sha224Copy),
  63337. TEST_DECL(test_wc_InitSha512),
  63338. TEST_DECL(test_wc_Sha512Update),
  63339. TEST_DECL(test_wc_Sha512Final),
  63340. TEST_DECL(test_wc_Sha512GetFlags),
  63341. TEST_DECL(test_wc_Sha512FinalRaw),
  63342. TEST_DECL(test_wc_Sha512Free),
  63343. TEST_DECL(test_wc_Sha512GetHash),
  63344. TEST_DECL(test_wc_Sha512Copy),
  63345. TEST_DECL(test_wc_InitSha512_224),
  63346. TEST_DECL(test_wc_Sha512_224Update),
  63347. TEST_DECL(test_wc_Sha512_224Final),
  63348. TEST_DECL(test_wc_Sha512_224GetFlags),
  63349. TEST_DECL(test_wc_Sha512_224FinalRaw),
  63350. TEST_DECL(test_wc_Sha512_224Free),
  63351. TEST_DECL(test_wc_Sha512_224GetHash),
  63352. TEST_DECL(test_wc_Sha512_224Copy),
  63353. TEST_DECL(test_wc_InitSha512_256),
  63354. TEST_DECL(test_wc_Sha512_256Update),
  63355. TEST_DECL(test_wc_Sha512_256Final),
  63356. TEST_DECL(test_wc_Sha512_256GetFlags),
  63357. TEST_DECL(test_wc_Sha512_256FinalRaw),
  63358. TEST_DECL(test_wc_Sha512_256Free),
  63359. TEST_DECL(test_wc_Sha512_256GetHash),
  63360. TEST_DECL(test_wc_Sha512_256Copy),
  63361. TEST_DECL(test_wc_InitSha384),
  63362. TEST_DECL(test_wc_Sha384Update),
  63363. TEST_DECL(test_wc_Sha384Final),
  63364. TEST_DECL(test_wc_Sha384GetFlags),
  63365. TEST_DECL(test_wc_Sha384FinalRaw),
  63366. TEST_DECL(test_wc_Sha384Free),
  63367. TEST_DECL(test_wc_Sha384GetHash),
  63368. TEST_DECL(test_wc_Sha384Copy),
  63369. TEST_DECL(test_wc_InitBlake2b),
  63370. TEST_DECL(test_wc_InitBlake2b_WithKey),
  63371. TEST_DECL(test_wc_InitBlake2s_WithKey),
  63372. TEST_DECL(test_wc_InitRipeMd),
  63373. TEST_DECL(test_wc_RipeMdUpdate),
  63374. TEST_DECL(test_wc_RipeMdFinal),
  63375. TEST_DECL(test_wc_InitSha3),
  63376. TEST_DECL(testing_wc_Sha3_Update),
  63377. TEST_DECL(test_wc_Sha3_224_Final),
  63378. TEST_DECL(test_wc_Sha3_256_Final),
  63379. TEST_DECL(test_wc_Sha3_384_Final),
  63380. TEST_DECL(test_wc_Sha3_512_Final),
  63381. TEST_DECL(test_wc_Sha3_224_Copy),
  63382. TEST_DECL(test_wc_Sha3_256_Copy),
  63383. TEST_DECL(test_wc_Sha3_384_Copy),
  63384. TEST_DECL(test_wc_Sha3_512_Copy),
  63385. TEST_DECL(test_wc_Sha3_GetFlags),
  63386. TEST_DECL(test_wc_InitShake256),
  63387. TEST_DECL(testing_wc_Shake256_Update),
  63388. TEST_DECL(test_wc_Shake256_Final),
  63389. TEST_DECL(test_wc_Shake256_Copy),
  63390. TEST_DECL(test_wc_Shake256Hash),
  63391. /* SM3 Digest */
  63392. TEST_DECL(test_wc_InitSm3Free),
  63393. TEST_DECL(test_wc_Sm3UpdateFinal),
  63394. TEST_DECL(test_wc_Sm3GetHash),
  63395. TEST_DECL(test_wc_Sm3Copy),
  63396. TEST_DECL(test_wc_Sm3FinalRaw),
  63397. TEST_DECL(test_wc_Sm3GetSetFlags),
  63398. TEST_DECL(test_wc_Sm3Hash),
  63399. TEST_DECL(test_wc_HashInit),
  63400. TEST_DECL(test_wc_HashSetFlags),
  63401. TEST_DECL(test_wc_HashGetFlags),
  63402. /* HMAC */
  63403. TEST_DECL(test_wc_Md5HmacSetKey),
  63404. TEST_DECL(test_wc_Md5HmacUpdate),
  63405. TEST_DECL(test_wc_Md5HmacFinal),
  63406. TEST_DECL(test_wc_ShaHmacSetKey),
  63407. TEST_DECL(test_wc_ShaHmacUpdate),
  63408. TEST_DECL(test_wc_ShaHmacFinal),
  63409. TEST_DECL(test_wc_Sha224HmacSetKey),
  63410. TEST_DECL(test_wc_Sha224HmacUpdate),
  63411. TEST_DECL(test_wc_Sha224HmacFinal),
  63412. TEST_DECL(test_wc_Sha256HmacSetKey),
  63413. TEST_DECL(test_wc_Sha256HmacUpdate),
  63414. TEST_DECL(test_wc_Sha256HmacFinal),
  63415. TEST_DECL(test_wc_Sha384HmacSetKey),
  63416. TEST_DECL(test_wc_Sha384HmacUpdate),
  63417. TEST_DECL(test_wc_Sha384HmacFinal),
  63418. /* CMAC */
  63419. TEST_DECL(test_wc_InitCmac),
  63420. TEST_DECL(test_wc_CmacUpdate),
  63421. TEST_DECL(test_wc_CmacFinal),
  63422. TEST_DECL(test_wc_AesCmacGenerate),
  63423. /* Cipher */
  63424. TEST_DECL(test_wc_AesGcmStream),
  63425. TEST_DECL(test_wc_Des3_SetIV),
  63426. TEST_DECL(test_wc_Des3_SetKey),
  63427. TEST_DECL(test_wc_Des3_CbcEncryptDecrypt),
  63428. TEST_DECL(test_wc_Des3_CbcEncryptDecryptWithKey),
  63429. TEST_DECL(test_wc_Des3_EcbEncrypt),
  63430. TEST_DECL(test_wc_Chacha_SetKey),
  63431. TEST_DECL(test_wc_Chacha_Process),
  63432. TEST_DECL(test_wc_ChaCha20Poly1305_aead),
  63433. TEST_DECL(test_wc_Poly1305SetKey),
  63434. TEST_DECL(test_wc_CamelliaSetKey),
  63435. TEST_DECL(test_wc_CamelliaSetIV),
  63436. TEST_DECL(test_wc_CamelliaEncryptDecryptDirect),
  63437. TEST_DECL(test_wc_CamelliaCbcEncryptDecrypt),
  63438. TEST_DECL(test_wc_Arc4SetKey),
  63439. TEST_DECL(test_wc_Arc4Process),
  63440. TEST_DECL(test_wc_Rc2SetKey),
  63441. TEST_DECL(test_wc_Rc2SetIV),
  63442. TEST_DECL(test_wc_Rc2EcbEncryptDecrypt),
  63443. TEST_DECL(test_wc_Rc2CbcEncryptDecrypt),
  63444. /* AES cipher and GMAC. */
  63445. TEST_DECL(test_wc_AesSetKey),
  63446. TEST_DECL(test_wc_AesSetIV),
  63447. TEST_DECL(test_wc_AesCbcEncryptDecrypt),
  63448. TEST_DECL(test_wc_AesCtrEncryptDecrypt),
  63449. TEST_DECL(test_wc_AesGcmSetKey),
  63450. TEST_DECL(test_wc_AesGcmEncryptDecrypt),
  63451. TEST_DECL(test_wc_AesGcmMixedEncDecLongIV),
  63452. TEST_DECL(test_wc_GmacSetKey),
  63453. TEST_DECL(test_wc_GmacUpdate),
  63454. TEST_DECL(test_wc_AesCcmSetKey),
  63455. TEST_DECL(test_wc_AesCcmEncryptDecrypt),
  63456. #if defined(WOLFSSL_AES_EAX) && \
  63457. (!defined(HAVE_FIPS) || FIPS_VERSION_GE(5, 3)) && !defined(HAVE_SELFTEST)
  63458. TEST_DECL(test_wc_AesEaxVectors),
  63459. TEST_DECL(test_wc_AesEaxEncryptAuth),
  63460. TEST_DECL(test_wc_AesEaxDecryptAuth),
  63461. #endif /* WOLFSSL_AES_EAX */
  63462. /* SM4 cipher */
  63463. TEST_DECL(test_wc_Sm4),
  63464. TEST_DECL(test_wc_Sm4Ecb),
  63465. TEST_DECL(test_wc_Sm4Cbc),
  63466. TEST_DECL(test_wc_Sm4Ctr),
  63467. TEST_DECL(test_wc_Sm4Gcm),
  63468. TEST_DECL(test_wc_Sm4Ccm),
  63469. /* RNG tests */
  63470. #ifdef HAVE_HASHDRBG
  63471. #ifdef TEST_RESEED_INTERVAL
  63472. TEST_DECL(test_wc_RNG_GenerateBlock_Reseed),
  63473. #endif
  63474. TEST_DECL(test_wc_RNG_GenerateBlock),
  63475. #endif
  63476. TEST_DECL(test_get_rand_digit),
  63477. TEST_DECL(test_wc_InitRngNonce),
  63478. TEST_DECL(test_wc_InitRngNonce_ex),
  63479. /* MP API tests */
  63480. TEST_DECL(test_get_digit_count),
  63481. TEST_DECL(test_mp_cond_copy),
  63482. TEST_DECL(test_mp_rand),
  63483. TEST_DECL(test_get_digit),
  63484. TEST_DECL(test_wc_export_int),
  63485. /* RSA */
  63486. TEST_DECL(test_wc_InitRsaKey),
  63487. TEST_DECL(test_wc_RsaPrivateKeyDecode),
  63488. TEST_DECL(test_wc_RsaPublicKeyDecode),
  63489. TEST_DECL(test_wc_RsaPublicKeyDecodeRaw),
  63490. TEST_DECL(test_wc_MakeRsaKey),
  63491. TEST_DECL(test_wc_CheckProbablePrime),
  63492. TEST_DECL(test_wc_RsaPSS_Verify),
  63493. TEST_DECL(test_wc_RsaPSS_VerifyCheck),
  63494. TEST_DECL(test_wc_RsaPSS_VerifyCheckInline),
  63495. TEST_DECL(test_wc_RsaKeyToDer),
  63496. TEST_DECL(test_wc_RsaKeyToPublicDer),
  63497. TEST_DECL(test_wc_RsaPublicEncryptDecrypt),
  63498. TEST_DECL(test_wc_RsaPublicEncryptDecrypt_ex),
  63499. TEST_DECL(test_wc_RsaEncryptSize),
  63500. TEST_DECL(test_wc_RsaSSL_SignVerify),
  63501. TEST_DECL(test_wc_RsaFlattenPublicKey),
  63502. TEST_DECL(test_RsaDecryptBoundsCheck),
  63503. /* DSA */
  63504. TEST_DECL(test_wc_InitDsaKey),
  63505. TEST_DECL(test_wc_DsaSignVerify),
  63506. TEST_DECL(test_wc_DsaPublicPrivateKeyDecode),
  63507. TEST_DECL(test_wc_MakeDsaKey),
  63508. TEST_DECL(test_wc_DsaKeyToDer),
  63509. TEST_DECL(test_wc_DsaKeyToPublicDer),
  63510. TEST_DECL(test_wc_DsaImportParamsRaw),
  63511. TEST_DECL(test_wc_DsaImportParamsRawCheck),
  63512. TEST_DECL(test_wc_DsaExportParamsRaw),
  63513. TEST_DECL(test_wc_DsaExportKeyRaw),
  63514. /* DH */
  63515. TEST_DECL(test_wc_DhPublicKeyDecode),
  63516. /* wolfCrypt ECC tests */
  63517. TEST_DECL(test_wc_ecc_get_curve_size_from_name),
  63518. TEST_DECL(test_wc_ecc_get_curve_id_from_name),
  63519. TEST_DECL(test_wc_ecc_get_curve_id_from_params),
  63520. #if defined(OPENSSL_EXTRA) && defined(HAVE_ECC) && \
  63521. !defined(HAVE_SELFTEST) && \
  63522. !(defined(HAVE_FIPS) || defined(HAVE_FIPS_VERSION))
  63523. TEST_DECL(test_wc_ecc_get_curve_id_from_dp_params),
  63524. #endif
  63525. TEST_DECL(test_wc_ecc_make_key),
  63526. TEST_DECL(test_wc_ecc_init),
  63527. TEST_DECL(test_wc_ecc_check_key),
  63528. TEST_DECL(test_wc_ecc_get_generator),
  63529. TEST_DECL(test_wc_ecc_size),
  63530. TEST_DECL(test_wc_ecc_params),
  63531. TEST_DECL(test_wc_ecc_signVerify_hash),
  63532. TEST_DECL(test_wc_ecc_shared_secret),
  63533. TEST_DECL(test_wc_ecc_export_x963),
  63534. TEST_DECL(test_wc_ecc_export_x963_ex),
  63535. TEST_DECL(test_wc_ecc_import_x963),
  63536. TEST_DECL(test_wc_ecc_import_private_key),
  63537. TEST_DECL(test_wc_ecc_export_private_only),
  63538. TEST_DECL(test_wc_ecc_rs_to_sig),
  63539. TEST_DECL(test_wc_ecc_import_raw),
  63540. TEST_DECL(test_wc_ecc_import_unsigned),
  63541. TEST_DECL(test_wc_ecc_sig_size),
  63542. TEST_DECL(test_wc_ecc_ctx_new),
  63543. TEST_DECL(test_wc_ecc_ctx_reset),
  63544. TEST_DECL(test_wc_ecc_ctx_set_peer_salt),
  63545. TEST_DECL(test_wc_ecc_ctx_set_info),
  63546. TEST_DECL(test_wc_ecc_encryptDecrypt),
  63547. TEST_DECL(test_wc_ecc_del_point),
  63548. TEST_DECL(test_wc_ecc_pointFns),
  63549. TEST_DECL(test_wc_ecc_shared_secret_ssh),
  63550. TEST_DECL(test_wc_ecc_verify_hash_ex),
  63551. TEST_DECL(test_wc_ecc_mulmod),
  63552. TEST_DECL(test_wc_ecc_is_valid_idx),
  63553. TEST_DECL(test_wc_ecc_get_curve_id_from_oid),
  63554. TEST_DECL(test_wc_ecc_sig_size_calc),
  63555. TEST_DECL(test_wc_EccPrivateKeyToDer),
  63556. /* SM2 elliptic curve */
  63557. TEST_DECL(test_wc_ecc_sm2_make_key),
  63558. TEST_DECL(test_wc_ecc_sm2_shared_secret),
  63559. TEST_DECL(test_wc_ecc_sm2_create_digest),
  63560. TEST_DECL(test_wc_ecc_sm2_verify_hash_ex),
  63561. TEST_DECL(test_wc_ecc_sm2_verify_hash),
  63562. TEST_DECL(test_wc_ecc_sm2_sign_hash_ex),
  63563. TEST_DECL(test_wc_ecc_sm2_sign_hash),
  63564. /* Curve25519 */
  63565. TEST_DECL(test_wc_curve25519_init),
  63566. TEST_DECL(test_wc_curve25519_size),
  63567. TEST_DECL(test_wc_curve25519_export_key_raw),
  63568. TEST_DECL(test_wc_curve25519_export_key_raw_ex),
  63569. TEST_DECL(test_wc_curve25519_make_key),
  63570. TEST_DECL(test_wc_curve25519_shared_secret_ex),
  63571. TEST_DECL(test_wc_curve25519_make_pub),
  63572. TEST_DECL(test_wc_curve25519_export_public_ex),
  63573. TEST_DECL(test_wc_curve25519_export_private_raw_ex),
  63574. TEST_DECL(test_wc_curve25519_import_private_raw_ex),
  63575. TEST_DECL(test_wc_curve25519_import_private),
  63576. /* ED25519 */
  63577. TEST_DECL(test_wc_ed25519_make_key),
  63578. TEST_DECL(test_wc_ed25519_init),
  63579. TEST_DECL(test_wc_ed25519_sign_msg),
  63580. TEST_DECL(test_wc_ed25519_import_public),
  63581. TEST_DECL(test_wc_ed25519_import_private_key),
  63582. TEST_DECL(test_wc_ed25519_export),
  63583. TEST_DECL(test_wc_ed25519_size),
  63584. TEST_DECL(test_wc_ed25519_exportKey),
  63585. TEST_DECL(test_wc_Ed25519PublicKeyToDer),
  63586. TEST_DECL(test_wc_Ed25519KeyToDer),
  63587. TEST_DECL(test_wc_Ed25519PrivateKeyToDer),
  63588. /* Curve448 */
  63589. TEST_DECL(test_wc_curve448_make_key),
  63590. TEST_DECL(test_wc_curve448_shared_secret_ex),
  63591. TEST_DECL(test_wc_curve448_export_public_ex),
  63592. TEST_DECL(test_wc_curve448_export_private_raw_ex),
  63593. TEST_DECL(test_wc_curve448_export_key_raw),
  63594. TEST_DECL(test_wc_curve448_import_private_raw_ex),
  63595. TEST_DECL(test_wc_curve448_import_private),
  63596. TEST_DECL(test_wc_curve448_init),
  63597. TEST_DECL(test_wc_curve448_size),
  63598. /* Ed448 */
  63599. TEST_DECL(test_wc_ed448_make_key),
  63600. TEST_DECL(test_wc_ed448_init),
  63601. TEST_DECL(test_wc_ed448_sign_msg),
  63602. TEST_DECL(test_wc_ed448_import_public),
  63603. TEST_DECL(test_wc_ed448_import_private_key),
  63604. TEST_DECL(test_wc_ed448_export),
  63605. TEST_DECL(test_wc_ed448_size),
  63606. TEST_DECL(test_wc_ed448_exportKey),
  63607. TEST_DECL(test_wc_Ed448PublicKeyToDer),
  63608. TEST_DECL(test_wc_Ed448KeyToDer),
  63609. TEST_DECL(test_wc_Ed448PrivateKeyToDer),
  63610. TEST_DECL(test_wc_Curve448PrivateKeyToDer),
  63611. /* Signature API */
  63612. TEST_DECL(test_wc_SignatureGetSize_ecc),
  63613. TEST_DECL(test_wc_SignatureGetSize_rsa),
  63614. /* PEM and DER APIs. */
  63615. TEST_DECL(test_wc_PemToDer),
  63616. TEST_DECL(test_wc_AllocDer),
  63617. TEST_DECL(test_wc_CertPemToDer),
  63618. TEST_DECL(test_wc_KeyPemToDer),
  63619. TEST_DECL(test_wc_PubKeyPemToDer),
  63620. TEST_DECL(test_wc_PemPubKeyToDer),
  63621. TEST_DECL(test_wc_GetPubKeyDerFromCert),
  63622. TEST_DECL(test_wc_CheckCertSigPubKey),
  63623. /* wolfCrypt ASN tests */
  63624. TEST_DECL(test_ToTraditional),
  63625. TEST_DECL(test_wc_CreateEncryptedPKCS8Key),
  63626. TEST_DECL(test_wc_GetPkcs8TraditionalOffset),
  63627. /* Certificate */
  63628. TEST_DECL(test_wc_SetSubjectRaw),
  63629. TEST_DECL(test_wc_GetSubjectRaw),
  63630. TEST_DECL(test_wc_SetIssuerRaw),
  63631. TEST_DECL(test_wc_SetIssueBuffer),
  63632. TEST_DECL(test_wc_SetSubjectKeyId),
  63633. TEST_DECL(test_wc_SetSubject),
  63634. TEST_DECL(test_CheckCertSignature),
  63635. TEST_DECL(test_wc_ParseCert),
  63636. TEST_DECL(test_wc_ParseCert_Error),
  63637. TEST_DECL(test_MakeCertWithPathLen),
  63638. TEST_DECL(test_MakeCertWithCaFalse),
  63639. TEST_DECL(test_wc_SetKeyUsage),
  63640. TEST_DECL(test_wc_SetAuthKeyIdFromPublicKey_ex),
  63641. TEST_DECL(test_wc_SetSubjectBuffer),
  63642. TEST_DECL(test_wc_SetSubjectKeyIdFromPublicKey_ex),
  63643. /* wolfcrypt PKCS#7 */
  63644. TEST_DECL(test_wc_PKCS7_New),
  63645. TEST_DECL(test_wc_PKCS7_Init),
  63646. TEST_DECL(test_wc_PKCS7_InitWithCert),
  63647. TEST_DECL(test_wc_PKCS7_EncodeData),
  63648. TEST_DECL(test_wc_PKCS7_EncodeSignedData),
  63649. TEST_DECL(test_wc_PKCS7_EncodeSignedData_ex),
  63650. TEST_DECL(test_wc_PKCS7_VerifySignedData_RSA),
  63651. TEST_DECL(test_wc_PKCS7_VerifySignedData_ECC),
  63652. TEST_DECL(test_wc_PKCS7_EncodeDecodeEnvelopedData),
  63653. TEST_DECL(test_wc_PKCS7_EncodeEncryptedData),
  63654. TEST_DECL(test_wc_PKCS7_Degenerate),
  63655. TEST_DECL(test_wc_PKCS7_BER),
  63656. TEST_DECL(test_wc_PKCS7_signed_enveloped),
  63657. TEST_DECL(test_wc_PKCS7_NoDefaultSignedAttribs),
  63658. TEST_DECL(test_wc_PKCS7_SetOriEncryptCtx),
  63659. TEST_DECL(test_wc_PKCS7_SetOriDecryptCtx),
  63660. TEST_DECL(test_wc_PKCS7_DecodeCompressedData),
  63661. /* wolfCrypt PKCS#12 */
  63662. TEST_DECL(test_wc_i2d_PKCS12),
  63663. /*
  63664. * test_wolfCrypt_Cleanup needs to come after the above wolfCrypt tests to
  63665. * avoid memory leaks.
  63666. */
  63667. TEST_DECL(test_wolfCrypt_Cleanup),
  63668. TEST_DECL(test_wolfSSL_Init),
  63669. TEST_DECL(test_dual_alg_support),
  63670. /*********************************
  63671. * OpenSSL compatibility API tests
  63672. *********************************/
  63673. /* If at some point a stub get implemented this test should fail indicating
  63674. * a need to implement a new test case
  63675. */
  63676. TEST_DECL(test_stubs_are_stubs),
  63677. /* ASN.1 compatibility API tests */
  63678. TEST_DECL(test_wolfSSL_ASN1_BIT_STRING),
  63679. TEST_DECL(test_wolfSSL_ASN1_INTEGER),
  63680. TEST_DECL(test_wolfSSL_ASN1_INTEGER_cmp),
  63681. TEST_DECL(test_wolfSSL_ASN1_INTEGER_BN),
  63682. TEST_DECL(test_wolfSSL_ASN1_INTEGER_get_set),
  63683. TEST_DECL(test_wolfSSL_d2i_ASN1_INTEGER),
  63684. TEST_DECL(test_wolfSSL_a2i_ASN1_INTEGER),
  63685. TEST_DECL(test_wolfSSL_i2c_ASN1_INTEGER),
  63686. TEST_DECL(test_wolfSSL_ASN1_OBJECT),
  63687. TEST_DECL(test_wolfSSL_ASN1_get_object),
  63688. TEST_DECL(test_wolfSSL_i2a_ASN1_OBJECT),
  63689. TEST_DECL(test_wolfSSL_i2t_ASN1_OBJECT),
  63690. TEST_DECL(test_wolfSSL_sk_ASN1_OBJECT),
  63691. TEST_DECL(test_wolfSSL_ASN1_STRING),
  63692. TEST_DECL(test_wolfSSL_ASN1_STRING_to_UTF8),
  63693. TEST_DECL(test_wolfSSL_i2s_ASN1_STRING),
  63694. TEST_DECL(test_wolfSSL_ASN1_STRING_canon),
  63695. TEST_DECL(test_wolfSSL_ASN1_STRING_print),
  63696. TEST_DECL(test_wolfSSL_ASN1_STRING_print_ex),
  63697. TEST_DECL(test_wolfSSL_ASN1_UNIVERSALSTRING_to_string),
  63698. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_free),
  63699. TEST_DECL(test_wolfSSL_ASN1_GENERALIZEDTIME_print),
  63700. TEST_DECL(test_wolfSSL_ASN1_TIME),
  63701. TEST_DECL(test_wolfSSL_ASN1_TIME_to_string),
  63702. TEST_DECL(test_wolfSSL_ASN1_TIME_diff_compare),
  63703. TEST_DECL(test_wolfSSL_ASN1_TIME_adj),
  63704. TEST_DECL(test_wolfSSL_ASN1_TIME_to_tm),
  63705. TEST_DECL(test_wolfSSL_ASN1_TIME_to_generalizedtime),
  63706. TEST_DECL(test_wolfSSL_ASN1_TIME_print),
  63707. TEST_DECL(test_wolfSSL_ASN1_UTCTIME_print),
  63708. TEST_DECL(test_wolfSSL_ASN1_TYPE),
  63709. TEST_DECL(test_wolfSSL_IMPLEMENT_ASN1_FUNCTIONS),
  63710. TEST_DECL(test_wolfSSL_lhash),
  63711. TEST_DECL(test_wolfSSL_certs),
  63712. TEST_DECL(test_wolfSSL_private_keys),
  63713. TEST_DECL(test_wolfSSL_PEM_def_callback),
  63714. TEST_DECL(test_wolfSSL_PEM_read_PrivateKey),
  63715. TEST_DECL(test_wolfSSL_PEM_read_RSA_PUBKEY),
  63716. TEST_DECL(test_wolfSSL_PEM_read_PUBKEY),
  63717. TEST_DECL(test_wolfSSL_PEM_PrivateKey_rsa),
  63718. TEST_DECL(test_wolfSSL_PEM_PrivateKey_ecc),
  63719. TEST_DECL(test_wolfSSL_PEM_PrivateKey_dsa),
  63720. TEST_DECL(test_wolfSSL_PEM_PrivateKey_dh),
  63721. TEST_DECL(test_wolfSSL_PEM_PrivateKey),
  63722. TEST_DECL(test_wolfSSL_PEM_file_RSAKey),
  63723. TEST_DECL(test_wolfSSL_PEM_file_RSAPrivateKey),
  63724. #ifndef NO_BIO
  63725. TEST_DECL(test_wolfSSL_BIO),
  63726. TEST_DECL(test_wolfSSL_BIO_BIO_ring_read),
  63727. TEST_DECL(test_wolfSSL_PEM_read_bio),
  63728. TEST_DECL(test_wolfSSL_PEM_bio_RSAKey),
  63729. TEST_DECL(test_wolfSSL_PEM_bio_DSAKey),
  63730. TEST_DECL(test_wolfSSL_PEM_bio_ECKey),
  63731. TEST_DECL(test_wolfSSL_PEM_bio_RSAPrivateKey),
  63732. TEST_DECL(test_wolfSSL_PEM_PUBKEY),
  63733. #endif
  63734. /* EVP API testing */
  63735. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_new),
  63736. TEST_DECL(test_wolfSSL_EVP_ENCODE_CTX_free),
  63737. TEST_DECL(test_wolfSSL_EVP_EncodeInit),
  63738. TEST_DECL(test_wolfSSL_EVP_EncodeUpdate),
  63739. TEST_DECL(test_wolfSSL_EVP_EncodeFinal),
  63740. TEST_DECL(test_wolfSSL_EVP_DecodeInit),
  63741. TEST_DECL(test_wolfSSL_EVP_DecodeUpdate),
  63742. TEST_DECL(test_wolfSSL_EVP_DecodeFinal),
  63743. TEST_DECL(test_wolfSSL_EVP_shake128),
  63744. TEST_DECL(test_wolfSSL_EVP_shake256),
  63745. TEST_DECL(test_wolfSSL_EVP_sm3),
  63746. TEST_DECL(test_EVP_blake2),
  63747. #ifdef OPENSSL_ALL
  63748. TEST_DECL(test_wolfSSL_EVP_md4),
  63749. TEST_DECL(test_wolfSSL_EVP_ripemd160),
  63750. TEST_DECL(test_wolfSSL_EVP_get_digestbynid),
  63751. TEST_DECL(test_wolfSSL_EVP_MD_nid),
  63752. TEST_DECL(test_wolfSSL_EVP_DigestFinal_ex),
  63753. #endif
  63754. TEST_DECL(test_EVP_MD_do_all),
  63755. TEST_DECL(test_wolfSSL_EVP_MD_size),
  63756. TEST_DECL(test_wolfSSL_EVP_MD_pkey_type),
  63757. TEST_DECL(test_wolfSSL_EVP_Digest),
  63758. TEST_DECL(test_wolfSSL_EVP_Digest_all),
  63759. TEST_DECL(test_wolfSSL_EVP_MD_hmac_signing),
  63760. TEST_DECL(test_wolfSSL_EVP_MD_rsa_signing),
  63761. TEST_DECL(test_wolfSSL_EVP_MD_ecc_signing),
  63762. TEST_DECL(test_wolfssl_EVP_aes_gcm),
  63763. TEST_DECL(test_wolfssl_EVP_aes_gcm_AAD_2_parts),
  63764. TEST_DECL(test_wolfssl_EVP_aes_gcm_zeroLen),
  63765. TEST_DECL(test_wolfssl_EVP_aes_ccm),
  63766. TEST_DECL(test_wolfssl_EVP_aes_ccm_zeroLen),
  63767. TEST_DECL(test_wolfssl_EVP_chacha20),
  63768. TEST_DECL(test_wolfssl_EVP_chacha20_poly1305),
  63769. TEST_DECL(test_wolfssl_EVP_sm4_ecb),
  63770. TEST_DECL(test_wolfssl_EVP_sm4_cbc),
  63771. TEST_DECL(test_wolfssl_EVP_sm4_ctr),
  63772. TEST_DECL(test_wolfssl_EVP_sm4_gcm_zeroLen),
  63773. TEST_DECL(test_wolfssl_EVP_sm4_gcm),
  63774. TEST_DECL(test_wolfssl_EVP_sm4_ccm_zeroLen),
  63775. TEST_DECL(test_wolfssl_EVP_sm4_ccm),
  63776. #ifdef OPENSSL_ALL
  63777. TEST_DECL(test_wolfSSL_EVP_aes_256_gcm),
  63778. TEST_DECL(test_wolfSSL_EVP_aes_192_gcm),
  63779. TEST_DECL(test_wolfSSL_EVP_aes_256_ccm),
  63780. TEST_DECL(test_wolfSSL_EVP_aes_192_ccm),
  63781. TEST_DECL(test_wolfSSL_EVP_aes_128_ccm),
  63782. TEST_DECL(test_wolfSSL_EVP_rc4),
  63783. TEST_DECL(test_wolfSSL_EVP_enc_null),
  63784. TEST_DECL(test_wolfSSL_EVP_rc2_cbc),
  63785. TEST_DECL(test_wolfSSL_EVP_mdc2),
  63786. TEST_DECL(test_evp_cipher_aes_gcm),
  63787. #endif
  63788. TEST_DECL(test_wolfssl_EVP_aria_gcm),
  63789. TEST_DECL(test_wolfSSL_EVP_Cipher_extra),
  63790. #ifdef OPENSSL_EXTRA
  63791. TEST_DECL(test_wolfSSL_EVP_get_cipherbynid),
  63792. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX),
  63793. #endif
  63794. #ifdef OPENSSL_ALL
  63795. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_iv_length),
  63796. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_key_length),
  63797. TEST_DECL(test_wolfSSL_EVP_CIPHER_CTX_set_iv),
  63798. TEST_DECL(test_wolfSSL_EVP_CIPHER_block_size),
  63799. TEST_DECL(test_wolfSSL_EVP_CIPHER_iv_length),
  63800. TEST_DECL(test_wolfSSL_EVP_X_STATE),
  63801. TEST_DECL(test_wolfSSL_EVP_X_STATE_LEN),
  63802. TEST_DECL(test_wolfSSL_EVP_BytesToKey),
  63803. #endif
  63804. TEST_DECL(test_wolfSSL_EVP_PKEY_print_public),
  63805. TEST_DECL(test_wolfSSL_EVP_PKEY_new_mac_key),
  63806. TEST_DECL(test_wolfSSL_EVP_PKEY_new_CMAC_key),
  63807. TEST_DECL(test_wolfSSL_EVP_PKEY_up_ref),
  63808. TEST_DECL(test_wolfSSL_EVP_PKEY_hkdf),
  63809. TEST_DECL(test_wolfSSL_EVP_PKEY_derive),
  63810. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey),
  63811. TEST_DECL(test_wolfSSL_d2i_and_i2d_PublicKey_ecc),
  63812. #ifndef NO_BIO
  63813. TEST_DECL(test_wolfSSL_d2i_PUBKEY),
  63814. #endif
  63815. TEST_DECL(test_wolfSSL_d2i_and_i2d_DSAparams),
  63816. TEST_DECL(test_wolfSSL_i2d_PrivateKey),
  63817. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  63818. #ifndef NO_BIO
  63819. TEST_DECL(test_wolfSSL_d2i_PrivateKeys_bio),
  63820. #endif /* !NO_BIO */
  63821. #endif
  63822. #ifdef OPENSSL_ALL
  63823. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DSA),
  63824. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_EC_KEY),
  63825. TEST_DECL(test_wolfSSL_EVP_PKEY_set1_get1_DH),
  63826. TEST_DECL(test_wolfSSL_EVP_PKEY_assign),
  63827. TEST_DECL(test_wolfSSL_EVP_PKEY_assign_DH),
  63828. TEST_DECL(test_wolfSSL_EVP_PKEY_base_id),
  63829. TEST_DECL(test_wolfSSL_EVP_PKEY_id),
  63830. TEST_DECL(test_wolfSSL_EVP_PKEY_paramgen),
  63831. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen),
  63832. TEST_DECL(test_wolfSSL_EVP_PKEY_keygen_init),
  63833. TEST_DECL(test_wolfSSL_EVP_PKEY_missing_parameters),
  63834. TEST_DECL(test_wolfSSL_EVP_PKEY_copy_parameters),
  63835. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_set_rsa_keygen_bits),
  63836. TEST_DECL(test_wolfSSL_EVP_PKEY_CTX_new_id),
  63837. TEST_DECL(test_wolfSSL_EVP_PKEY_get0_EC_KEY),
  63838. #endif
  63839. TEST_DECL(test_EVP_PKEY_rsa),
  63840. TEST_DECL(test_EVP_PKEY_ec),
  63841. TEST_DECL(test_wolfSSL_EVP_PKEY_encrypt),
  63842. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_rsa),
  63843. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_dsa),
  63844. TEST_DECL(test_wolfSSL_EVP_PKEY_sign_verify_ec),
  63845. TEST_DECL(test_EVP_PKEY_cmp),
  63846. #ifdef OPENSSL_ALL
  63847. TEST_DECL(test_wolfSSL_EVP_SignInit_ex),
  63848. TEST_DECL(test_wolfSSL_EVP_PKEY_param_check),
  63849. TEST_DECL(test_wolfSSL_QT_EVP_PKEY_CTX_free),
  63850. #endif
  63851. TEST_DECL(test_wolfSSL_EVP_PBE_scrypt),
  63852. TEST_DECL(test_wolfSSL_CTX_add_extra_chain_cert),
  63853. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  63854. TEST_DECL(test_wolfSSL_ERR_peek_last_error_line),
  63855. #endif
  63856. #ifndef NO_BIO
  63857. TEST_DECL(test_wolfSSL_ERR_print_errors_cb),
  63858. TEST_DECL(test_wolfSSL_GetLoggingCb),
  63859. TEST_DECL(test_WOLFSSL_ERROR_MSG),
  63860. TEST_DECL(test_wc_ERR_remove_state),
  63861. TEST_DECL(test_wc_ERR_print_errors_fp),
  63862. #endif
  63863. TEST_DECL(test_wolfSSL_configure_args),
  63864. TEST_DECL(test_wolfSSL_sk_SSL_CIPHER),
  63865. TEST_DECL(test_wolfSSL_set1_curves_list),
  63866. TEST_DECL(test_wolfSSL_curves_mismatch),
  63867. TEST_DECL(test_wolfSSL_set1_sigalgs_list),
  63868. TEST_DECL(test_wolfSSL_OtherName),
  63869. TEST_DECL(test_wolfSSL_FPKI),
  63870. TEST_DECL(test_wolfSSL_URI),
  63871. TEST_DECL(test_wolfSSL_TBS),
  63872. TEST_DECL(test_wolfSSL_X509_STORE_CTX),
  63873. TEST_DECL(test_X509_STORE_untrusted),
  63874. TEST_DECL(test_wolfSSL_X509_STORE_CTX_trusted_stack_cleanup),
  63875. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_current_issuer),
  63876. TEST_DECL(test_wolfSSL_X509_STORE_set_flags),
  63877. TEST_DECL(test_wolfSSL_X509_LOOKUP_load_file),
  63878. TEST_DECL(test_wolfSSL_X509_Name_canon),
  63879. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_file),
  63880. TEST_DECL(test_wolfSSL_X509_LOOKUP_ctrl_hash_dir),
  63881. TEST_DECL(test_wolfSSL_X509_NID),
  63882. TEST_DECL(test_wolfSSL_X509_STORE_CTX_set_time),
  63883. TEST_DECL(test_wolfSSL_get0_param),
  63884. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_host),
  63885. TEST_DECL(test_wolfSSL_set1_host),
  63886. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM_set1_ip),
  63887. TEST_DECL(test_wolfSSL_X509_STORE_CTX_get0_store),
  63888. TEST_DECL(test_wolfSSL_X509_STORE),
  63889. TEST_DECL(test_wolfSSL_X509_STORE_load_locations),
  63890. TEST_DECL(test_X509_STORE_get0_objects),
  63891. TEST_DECL(test_wolfSSL_X509_load_crl_file),
  63892. TEST_DECL(test_wolfSSL_X509_STORE_get1_certs),
  63893. TEST_DECL(test_wolfSSL_X509_STORE_set_get_crl),
  63894. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY_get_object),
  63895. TEST_DECL(test_wolfSSL_X509_cmp_time),
  63896. TEST_DECL(test_wolfSSL_X509_time_adj),
  63897. /* X509 tests */
  63898. TEST_DECL(test_wolfSSL_X509_subject_name_hash),
  63899. TEST_DECL(test_wolfSSL_X509_issuer_name_hash),
  63900. TEST_DECL(test_wolfSSL_X509_check_host),
  63901. TEST_DECL(test_wolfSSL_X509_check_email),
  63902. TEST_DECL(test_wolfSSL_X509_check_private_key),
  63903. TEST_DECL(test_wolfSSL_X509),
  63904. TEST_DECL(test_wolfSSL_X509_VERIFY_PARAM),
  63905. TEST_DECL(test_wolfSSL_X509_sign),
  63906. TEST_DECL(test_wolfSSL_X509_sign2),
  63907. TEST_DECL(test_wolfSSL_X509_verify),
  63908. TEST_DECL(test_wolfSSL_X509_get0_tbs_sigalg),
  63909. TEST_DECL(test_wolfSSL_X509_ALGOR_get0),
  63910. TEST_DECL(test_wolfSSL_X509_get_X509_PUBKEY),
  63911. TEST_DECL(test_wolfSSL_X509_PUBKEY_RSA),
  63912. TEST_DECL(test_wolfSSL_X509_PUBKEY_EC),
  63913. TEST_DECL(test_wolfSSL_X509_PUBKEY_DSA),
  63914. TEST_DECL(test_wolfSSL_PEM_write_bio_X509),
  63915. TEST_DECL(test_wolfSSL_X509_NAME_get_entry),
  63916. TEST_DECL(test_wolfSSL_X509_NAME),
  63917. TEST_DECL(test_wolfSSL_X509_NAME_hash),
  63918. TEST_DECL(test_wolfSSL_X509_NAME_print_ex),
  63919. TEST_DECL(test_wolfSSL_X509_NAME_ENTRY),
  63920. TEST_DECL(test_wolfSSL_X509_set_name),
  63921. TEST_DECL(test_wolfSSL_X509_set_notAfter),
  63922. TEST_DECL(test_wolfSSL_X509_set_notBefore),
  63923. TEST_DECL(test_wolfSSL_X509_set_version),
  63924. TEST_DECL(test_wolfSSL_X509_get_serialNumber),
  63925. TEST_DECL(test_wolfSSL_X509_CRL),
  63926. TEST_DECL(test_wolfSSL_i2d_X509),
  63927. TEST_DECL(test_wolfSSL_d2i_X509_REQ),
  63928. TEST_DECL(test_wolfSSL_PEM_read_X509),
  63929. TEST_DECL(test_wolfSSL_X509_check_ca),
  63930. TEST_DECL(test_wolfSSL_X509_check_ip_asc),
  63931. TEST_DECL(test_wolfSSL_X509_bad_altname),
  63932. TEST_DECL(test_wolfSSL_make_cert),
  63933. #ifndef NO_BIO
  63934. TEST_DECL(test_wolfSSL_X509_INFO_multiple_info),
  63935. TEST_DECL(test_wolfSSL_X509_INFO),
  63936. TEST_DECL(test_wolfSSL_PEM_X509_INFO_read_bio),
  63937. #endif
  63938. #ifdef OPENSSL_ALL
  63939. TEST_DECL(test_wolfSSL_X509_PUBKEY_get),
  63940. #endif
  63941. TEST_DECL(test_wolfSSL_X509_CA_num),
  63942. TEST_DECL(test_wolfSSL_X509_get_version),
  63943. #ifndef NO_BIO
  63944. TEST_DECL(test_wolfSSL_X509_print),
  63945. TEST_DECL(test_wolfSSL_X509_CRL_print),
  63946. #endif
  63947. TEST_DECL(test_X509_get_signature_nid),
  63948. /* X509 extension testing. */
  63949. TEST_DECL(test_wolfSSL_X509_get_extension_flags),
  63950. TEST_DECL(test_wolfSSL_X509_get_ext),
  63951. TEST_DECL(test_wolfSSL_X509_get_ext_by_NID),
  63952. TEST_DECL(test_wolfSSL_X509_get_ext_subj_alt_name),
  63953. TEST_DECL(test_wolfSSL_X509_get_ext_count),
  63954. TEST_DECL(test_wolfSSL_X509_EXTENSION_new),
  63955. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_object),
  63956. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_data),
  63957. TEST_DECL(test_wolfSSL_X509_EXTENSION_get_critical),
  63958. TEST_DECL(test_wolfSSL_X509V3_EXT_get),
  63959. TEST_DECL(test_wolfSSL_X509V3_EXT_nconf),
  63960. TEST_DECL(test_wolfSSL_X509V3_EXT),
  63961. TEST_DECL(test_wolfSSL_X509V3_EXT_print),
  63962. TEST_DECL(test_wolfSSL_X509_cmp),
  63963. TEST_DECL(test_GENERAL_NAME_set0_othername),
  63964. TEST_DECL(test_othername_and_SID_ext),
  63965. TEST_DECL(test_wolfSSL_dup_CA_list),
  63966. /* OpenSSL sk_X509 API test */
  63967. TEST_DECL(test_sk_X509),
  63968. /* OpenSSL sk_X509_CRL API test */
  63969. TEST_DECL(test_sk_X509_CRL),
  63970. /* OpenSSL X509 REQ API test */
  63971. TEST_DECL(test_X509_REQ),
  63972. /* OpenSSL compatibility outside SSL context w/ CRL lookup directory */
  63973. TEST_DECL(test_X509_STORE_No_SSL_CTX),
  63974. TEST_DECL(test_X509_LOOKUP_add_dir),
  63975. /* RAND compatibility API */
  63976. TEST_DECL(test_wolfSSL_RAND_set_rand_method),
  63977. TEST_DECL(test_wolfSSL_RAND_bytes),
  63978. TEST_DECL(test_wolfSSL_RAND),
  63979. /* BN compatibility API */
  63980. TEST_DECL(test_wolfSSL_BN_CTX),
  63981. TEST_DECL(test_wolfSSL_BN),
  63982. TEST_DECL(test_wolfSSL_BN_init),
  63983. TEST_DECL(test_wolfSSL_BN_enc_dec),
  63984. TEST_DECL(test_wolfSSL_BN_word),
  63985. TEST_DECL(test_wolfSSL_BN_bits),
  63986. TEST_DECL(test_wolfSSL_BN_shift),
  63987. TEST_DECL(test_wolfSSL_BN_math),
  63988. TEST_DECL(test_wolfSSL_BN_math_mod),
  63989. TEST_DECL(test_wolfSSL_BN_math_other),
  63990. TEST_DECL(test_wolfSSL_BN_rand),
  63991. TEST_DECL(test_wolfSSL_BN_prime),
  63992. /* OpenSSL PKCS5 API test */
  63993. TEST_DECL(test_wolfSSL_PKCS5),
  63994. /* OpenSSL PKCS8 API test */
  63995. TEST_DECL(test_wolfSSL_PKCS8_Compat),
  63996. TEST_DECL(test_wolfSSL_PKCS8_d2i),
  63997. /* OpenSSL PKCS7 API test */
  63998. TEST_DECL(test_wolfssl_PKCS7),
  63999. TEST_DECL(test_wolfSSL_PKCS7_certs),
  64000. TEST_DECL(test_wolfSSL_PKCS7_sign),
  64001. TEST_DECL(test_wolfSSL_PKCS7_SIGNED_new),
  64002. #ifndef NO_BIO
  64003. TEST_DECL(test_wolfSSL_PEM_write_bio_PKCS7),
  64004. #ifdef HAVE_SMIME
  64005. TEST_DECL(test_wolfSSL_SMIME_read_PKCS7),
  64006. TEST_DECL(test_wolfSSL_SMIME_write_PKCS7),
  64007. #endif /* HAVE_SMIME */
  64008. #endif /* !NO_BIO */
  64009. /* OpenSSL PKCS12 API test */
  64010. TEST_DECL(test_wolfSSL_PKCS12),
  64011. /* Can't memory test as callbacks use Assert. */
  64012. TEST_DECL(test_error_queue_per_thread),
  64013. TEST_DECL(test_wolfSSL_ERR_put_error),
  64014. TEST_DECL(test_wolfSSL_ERR_get_error_order),
  64015. #ifndef NO_BIO
  64016. TEST_DECL(test_wolfSSL_ERR_print_errors),
  64017. #endif
  64018. TEST_DECL(test_OBJ_NAME_do_all),
  64019. TEST_DECL(test_wolfSSL_OBJ),
  64020. TEST_DECL(test_wolfSSL_OBJ_cmp),
  64021. TEST_DECL(test_wolfSSL_OBJ_txt2nid),
  64022. TEST_DECL(test_wolfSSL_OBJ_txt2obj),
  64023. #ifdef OPENSSL_ALL
  64024. TEST_DECL(test_wolfSSL_OBJ_ln),
  64025. TEST_DECL(test_wolfSSL_OBJ_sn),
  64026. #endif
  64027. #ifndef NO_BIO
  64028. TEST_DECL(test_wolfSSL_BIO_gets),
  64029. TEST_DECL(test_wolfSSL_BIO_puts),
  64030. TEST_DECL(test_wolfSSL_BIO_dump),
  64031. /* Can't memory test as server hangs. */
  64032. TEST_DECL(test_wolfSSL_BIO_should_retry),
  64033. TEST_DECL(test_wolfSSL_BIO_write),
  64034. TEST_DECL(test_wolfSSL_BIO_printf),
  64035. TEST_DECL(test_wolfSSL_BIO_f_md),
  64036. TEST_DECL(test_wolfSSL_BIO_up_ref),
  64037. TEST_DECL(test_wolfSSL_BIO_reset),
  64038. TEST_DECL(test_wolfSSL_BIO_get_len),
  64039. #endif
  64040. TEST_DECL(test_wolfSSL_check_domain),
  64041. TEST_DECL(test_wolfSSL_cert_cb),
  64042. TEST_DECL(test_wolfSSL_cert_cb_dyn_ciphers),
  64043. TEST_DECL(test_wolfSSL_ciphersuite_auth),
  64044. TEST_DECL(test_wolfSSL_sigalg_info),
  64045. /* Can't memory test as tcp_connect aborts. */
  64046. TEST_DECL(test_wolfSSL_SESSION),
  64047. TEST_DECL(test_wolfSSL_SESSION_expire_downgrade),
  64048. TEST_DECL(test_wolfSSL_CTX_sess_set_remove_cb),
  64049. TEST_DECL(test_wolfSSL_ticket_keys),
  64050. TEST_DECL(test_wolfSSL_sk_GENERAL_NAME),
  64051. TEST_DECL(test_wolfSSL_GENERAL_NAME_print),
  64052. TEST_DECL(test_wolfSSL_sk_DIST_POINT),
  64053. TEST_DECL(test_wolfSSL_verify_mode),
  64054. TEST_DECL(test_wolfSSL_verify_depth),
  64055. TEST_DECL(test_wolfSSL_verify_result),
  64056. TEST_DECL(test_wolfSSL_msg_callback),
  64057. TEST_DECL(test_wolfSSL_OCSP_id_get0_info),
  64058. TEST_DECL(test_wolfSSL_i2d_OCSP_CERTID),
  64059. TEST_DECL(test_wolfSSL_d2i_OCSP_CERTID),
  64060. TEST_DECL(test_wolfSSL_OCSP_id_cmp),
  64061. TEST_DECL(test_wolfSSL_OCSP_SINGLERESP_get0_id),
  64062. TEST_DECL(test_wolfSSL_OCSP_single_get0_status),
  64063. TEST_DECL(test_wolfSSL_OCSP_resp_count),
  64064. TEST_DECL(test_wolfSSL_OCSP_resp_get0),
  64065. TEST_DECL(test_wolfSSL_PEM_read),
  64066. TEST_DECL(test_wolfSSL_OpenSSL_version),
  64067. TEST_DECL(test_wolfSSL_OpenSSL_add_all_algorithms),
  64068. TEST_DECL(test_wolfSSL_OPENSSL_hexstr2buf),
  64069. TEST_DECL(test_CONF_modules_xxx),
  64070. #ifdef OPENSSL_ALL
  64071. TEST_DECL(test_wolfSSL_TXT_DB),
  64072. TEST_DECL(test_wolfSSL_NCONF),
  64073. #endif
  64074. TEST_DECL(test_wolfSSL_CRYPTO_memcmp),
  64075. TEST_DECL(test_wolfSSL_CRYPTO_get_ex_new_index),
  64076. TEST_DECL(test_wolfSSL_SESSION_get_ex_new_index),
  64077. TEST_DECL(test_CRYPTO_set_dynlock_xxx),
  64078. TEST_DECL(test_CRYPTO_THREADID_xxx),
  64079. TEST_DECL(test_ENGINE_cleanup),
  64080. /* test the no op functions for compatibility */
  64081. TEST_DECL(test_no_op_functions),
  64082. /* OpenSSL error API tests */
  64083. TEST_DECL(test_ERR_load_crypto_strings),
  64084. #ifdef OPENSSL_ALL
  64085. TEST_DECL(test_wolfSSL_sk_CIPHER_description),
  64086. TEST_DECL(test_wolfSSL_get_ciphers_compat),
  64087. TEST_DECL(test_wolfSSL_CTX_ctrl),
  64088. #endif /* OPENSSL_ALL */
  64089. #if (defined(OPENSSL_ALL) || defined(WOLFSSL_ASIO)) && !defined(NO_RSA)
  64090. TEST_DECL(test_wolfSSL_CTX_use_certificate_ASN1),
  64091. #endif /* (OPENSSL_ALL || WOLFSSL_ASIO) && !NO_RSA */
  64092. /*********************************
  64093. * Crypto API tests
  64094. *********************************/
  64095. TEST_DECL(test_wolfSSL_MD4),
  64096. TEST_DECL(test_wolfSSL_MD5),
  64097. TEST_DECL(test_wolfSSL_MD5_Transform),
  64098. TEST_DECL(test_wolfSSL_SHA),
  64099. TEST_DECL(test_wolfSSL_SHA_Transform),
  64100. TEST_DECL(test_wolfSSL_SHA224),
  64101. TEST_DECL(test_wolfSSL_SHA256),
  64102. TEST_DECL(test_wolfSSL_SHA256_Transform),
  64103. TEST_DECL(test_wolfSSL_SHA512_Transform),
  64104. TEST_DECL(test_wolfSSL_SHA512_224_Transform),
  64105. TEST_DECL(test_wolfSSL_SHA512_256_Transform),
  64106. TEST_DECL(test_wolfSSL_HMAC_CTX),
  64107. TEST_DECL(test_wolfSSL_HMAC),
  64108. TEST_DECL(test_wolfSSL_CMAC),
  64109. TEST_DECL(test_wolfSSL_DES),
  64110. TEST_DECL(test_wolfSSL_DES_ncbc),
  64111. TEST_DECL(test_wolfSSL_DES_ecb_encrypt),
  64112. TEST_DECL(test_wolfSSL_DES_ede3_cbc_encrypt),
  64113. TEST_DECL(test_wolfSSL_AES_encrypt),
  64114. TEST_DECL(test_wolfSSL_AES_ecb_encrypt),
  64115. TEST_DECL(test_wolfSSL_AES_cbc_encrypt),
  64116. TEST_DECL(test_wolfSSL_AES_cfb128_encrypt),
  64117. TEST_DECL(test_wolfSSL_CRYPTO_cts128),
  64118. TEST_DECL(test_wolfSSL_RC4),
  64119. TEST_DECL(test_wolfSSL_RSA),
  64120. TEST_DECL(test_wolfSSL_RSA_DER),
  64121. TEST_DECL(test_wolfSSL_RSA_print),
  64122. TEST_DECL(test_wolfSSL_RSA_padding_add_PKCS1_PSS),
  64123. TEST_DECL(test_wolfSSL_RSA_sign_sha3),
  64124. TEST_DECL(test_wolfSSL_RSA_get0_key),
  64125. TEST_DECL(test_wolfSSL_RSA_meth),
  64126. TEST_DECL(test_wolfSSL_RSA_verify),
  64127. TEST_DECL(test_wolfSSL_RSA_sign),
  64128. TEST_DECL(test_wolfSSL_RSA_sign_ex),
  64129. TEST_DECL(test_wolfSSL_RSA_public_decrypt),
  64130. TEST_DECL(test_wolfSSL_RSA_private_encrypt),
  64131. TEST_DECL(test_wolfSSL_RSA_public_encrypt),
  64132. TEST_DECL(test_wolfSSL_RSA_private_decrypt),
  64133. TEST_DECL(test_wolfSSL_RSA_GenAdd),
  64134. TEST_DECL(test_wolfSSL_RSA_blinding_on),
  64135. TEST_DECL(test_wolfSSL_RSA_ex_data),
  64136. TEST_DECL(test_wolfSSL_RSA_LoadDer),
  64137. TEST_DECL(test_wolfSSL_RSA_To_Der),
  64138. TEST_DECL(test_wolfSSL_PEM_read_RSAPublicKey),
  64139. TEST_DECL(test_wolfSSL_PEM_write_RSA_PUBKEY),
  64140. TEST_DECL(test_wolfSSL_PEM_write_RSAPrivateKey),
  64141. TEST_DECL(test_wolfSSL_PEM_write_mem_RSAPrivateKey),
  64142. TEST_DECL(test_wolfSSL_DH),
  64143. TEST_DECL(test_wolfSSL_DH_dup),
  64144. TEST_DECL(test_wolfSSL_DH_check),
  64145. TEST_DECL(test_wolfSSL_DH_prime),
  64146. TEST_DECL(test_wolfSSL_DH_1536_prime),
  64147. TEST_DECL(test_wolfSSL_DH_get_2048_256),
  64148. TEST_DECL(test_wolfSSL_PEM_write_DHparams),
  64149. TEST_DECL(test_wolfSSL_PEM_read_DHparams),
  64150. TEST_DECL(test_wolfSSL_d2i_DHparams),
  64151. TEST_DECL(test_wolfSSL_DH_LoadDer),
  64152. TEST_DECL(test_wolfSSL_i2d_DHparams),
  64153. #if defined(HAVE_ECC) && !defined(OPENSSL_NO_PK)
  64154. TEST_DECL(test_wolfSSL_EC_GROUP),
  64155. TEST_DECL(test_wolfSSL_PEM_read_bio_ECPKParameters),
  64156. TEST_DECL(test_wolfSSL_EC_POINT),
  64157. TEST_DECL(test_wolfSSL_SPAKE),
  64158. TEST_DECL(test_wolfSSL_EC_KEY_generate),
  64159. TEST_DECL(test_EC_i2d),
  64160. TEST_DECL(test_wolfSSL_EC_curve),
  64161. TEST_DECL(test_wolfSSL_EC_KEY_dup),
  64162. TEST_DECL(test_wolfSSL_EC_KEY_set_group),
  64163. TEST_DECL(test_wolfSSL_EC_KEY_set_conv_form),
  64164. TEST_DECL(test_wolfSSL_EC_KEY_private_key),
  64165. TEST_DECL(test_wolfSSL_EC_KEY_public_key),
  64166. TEST_DECL(test_wolfSSL_EC_KEY_print_fp),
  64167. TEST_DECL(test_wolfSSL_EC_get_builtin_curves),
  64168. TEST_DECL(test_wolfSSL_ECDSA_SIG),
  64169. TEST_DECL(test_ECDSA_size_sign),
  64170. TEST_DECL(test_ECDH_compute_key),
  64171. #endif
  64172. #ifdef OPENSSL_EXTRA
  64173. TEST_DECL(test_EC25519),
  64174. TEST_DECL(test_ED25519),
  64175. TEST_DECL(test_EC448),
  64176. TEST_DECL(test_ED448),
  64177. #endif
  64178. TEST_DECL(test_DSA_do_sign_verify),
  64179. #ifdef OPENSSL_ALL
  64180. TEST_DECL(test_wolfSSL_DSA_generate_parameters),
  64181. TEST_DECL(test_wolfSSL_DSA_SIG),
  64182. #endif
  64183. TEST_DECL(test_openssl_generate_key_and_cert),
  64184. TEST_DECL(test_wolfSSL_FIPS_mode),
  64185. TEST_DECL(test_openssl_FIPS_drbg),
  64186. /*********************************
  64187. * CertManager API tests
  64188. *********************************/
  64189. TEST_DECL(test_wolfSSL_CertManagerAPI),
  64190. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer),
  64191. TEST_DECL(test_wolfSSL_CertManagerLoadCABuffer_ex),
  64192. TEST_DECL(test_wolfSSL_CertManagerGetCerts),
  64193. TEST_DECL(test_wolfSSL_CertManagerSetVerify),
  64194. TEST_DECL(test_wolfSSL_CertManagerNameConstraint),
  64195. TEST_DECL(test_wolfSSL_CertManagerNameConstraint2),
  64196. TEST_DECL(test_wolfSSL_CertManagerNameConstraint3),
  64197. TEST_DECL(test_wolfSSL_CertManagerNameConstraint4),
  64198. TEST_DECL(test_wolfSSL_CertManagerNameConstraint5),
  64199. TEST_DECL(test_wolfSSL_CertManagerCRL),
  64200. TEST_DECL(test_wolfSSL_CertManagerCheckOCSPResponse),
  64201. TEST_DECL(test_wolfSSL_CheckOCSPResponse),
  64202. #if !defined(NO_RSA) && !defined(NO_SHA) && !defined(NO_FILESYSTEM) && \
  64203. !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  64204. !defined(WOLFSSL_NO_CLIENT_AUTH))
  64205. TEST_DECL(test_various_pathlen_chains),
  64206. #endif
  64207. /*********************************
  64208. * SSL/TLS API tests
  64209. *********************************/
  64210. TEST_DECL(test_wolfSSL_Method_Allocators),
  64211. #ifndef NO_WOLFSSL_SERVER
  64212. TEST_DECL(test_wolfSSL_CTX_new),
  64213. #endif
  64214. TEST_DECL(test_server_wolfSSL_new),
  64215. TEST_DECL(test_client_wolfSSL_new),
  64216. #if (!defined(NO_WOLFSSL_CLIENT) || !defined(NO_WOLFSSL_SERVER)) && \
  64217. (!defined(NO_RSA) || defined(HAVE_ECC)) && !defined(NO_FILESYSTEM)
  64218. TEST_DECL(test_for_double_Free),
  64219. #endif
  64220. TEST_DECL(test_wolfSSL_set_options),
  64221. #ifdef WOLFSSL_TLS13
  64222. /* TLS v1.3 API tests */
  64223. TEST_DECL(test_tls13_apis),
  64224. TEST_DECL(test_tls13_cipher_suites),
  64225. #endif
  64226. TEST_DECL(test_wolfSSL_tmp_dh),
  64227. TEST_DECL(test_wolfSSL_ctrl),
  64228. #if defined(OPENSSL_ALL) || (defined(OPENSSL_EXTRA) && \
  64229. (defined(HAVE_STUNNEL) || defined(WOLFSSL_NGINX) || \
  64230. defined(HAVE_LIGHTY) || defined(WOLFSSL_HAPROXY) || \
  64231. defined(WOLFSSL_OPENSSH) || defined(HAVE_SBLIM_SFCB)))
  64232. TEST_DECL(test_wolfSSL_set_SSL_CTX),
  64233. #endif
  64234. TEST_DECL(test_wolfSSL_CTX_get_min_proto_version),
  64235. TEST_DECL(test_wolfSSL_security_level),
  64236. TEST_DECL(test_wolfSSL_SSL_in_init),
  64237. TEST_DECL(test_wolfSSL_CTX_set_timeout),
  64238. TEST_DECL(test_wolfSSL_set_psk_use_session_callback),
  64239. TEST_DECL(test_CONF_CTX_FILE),
  64240. TEST_DECL(test_CONF_CTX_CMDLINE),
  64241. #if !defined(NO_CERTS) && (!defined(NO_WOLFSSL_CLIENT) || \
  64242. !defined(WOLFSSL_NO_CLIENT_AUTH)) && !defined(NO_FILESYSTEM)
  64243. /* Use the Cert Manager(CM) API to generate the error ASN_SIG_CONFIRM_E */
  64244. /* Bad certificate signature tests */
  64245. TEST_DECL(test_EccSigFailure_cm),
  64246. TEST_DECL(test_RsaSigFailure_cm),
  64247. #endif /* NO_CERTS */
  64248. /* PKCS8 testing */
  64249. TEST_DECL(test_wolfSSL_no_password_cb),
  64250. TEST_DECL(test_wolfSSL_PKCS8),
  64251. TEST_DECL(test_wolfSSL_PKCS8_ED25519),
  64252. TEST_DECL(test_wolfSSL_PKCS8_ED448),
  64253. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  64254. TEST_DECL(test_wolfSSL_get_finished),
  64255. /* Uses Assert in handshake callback. */
  64256. TEST_DECL(test_wolfSSL_CTX_add_session),
  64257. /* Large number of memory allocations. */
  64258. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls13),
  64259. /* Large number of memory allocations. */
  64260. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls13),
  64261. /* Large number of memory allocations. */
  64262. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls12),
  64263. /* Large number of memory allocations. */
  64264. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls12),
  64265. /* Large number of memory allocations. */
  64266. TEST_DECL(test_wolfSSL_CTX_add_session_ext_tls11),
  64267. /* Large number of memory allocations. */
  64268. TEST_DECL(test_wolfSSL_CTX_add_session_ext_dtls1),
  64269. #endif
  64270. TEST_DECL(test_SSL_CIPHER_get_xxx),
  64271. TEST_DECL(test_wolfSSL_ERR_strings),
  64272. TEST_DECL(test_wolfSSL_CTX_set_cipher_list_bytes),
  64273. TEST_DECL(test_wolfSSL_CTX_use_certificate_file),
  64274. TEST_DECL(test_wolfSSL_CTX_use_certificate_buffer),
  64275. TEST_DECL(test_wolfSSL_CTX_use_PrivateKey_file),
  64276. TEST_DECL(test_wolfSSL_CTX_load_verify_locations),
  64277. /* Large number of memory allocations. */
  64278. TEST_DECL(test_wolfSSL_CTX_load_system_CA_certs),
  64279. TEST_DECL(test_wolfSSL_CertRsaPss),
  64280. TEST_DECL(test_wolfSSL_CTX_load_verify_locations_ex),
  64281. TEST_DECL(test_wolfSSL_CTX_load_verify_buffer_ex),
  64282. TEST_DECL(test_wolfSSL_CTX_load_verify_chain_buffer_format),
  64283. TEST_DECL(test_wolfSSL_CTX_add1_chain_cert),
  64284. TEST_DECL(test_wolfSSL_CTX_use_certificate_chain_file_format),
  64285. TEST_DECL(test_wolfSSL_CTX_trust_peer_cert),
  64286. TEST_DECL(test_wolfSSL_CTX_LoadCRL),
  64287. TEST_DECL(test_multiple_crls_same_issuer),
  64288. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_file),
  64289. TEST_DECL(test_wolfSSL_CTX_SetTmpDH_buffer),
  64290. TEST_DECL(test_wolfSSL_CTX_SetMinMaxDhKey_Sz),
  64291. TEST_DECL(test_wolfSSL_CTX_der_load_verify_locations),
  64292. TEST_DECL(test_wolfSSL_CTX_enable_disable),
  64293. TEST_DECL(test_wolfSSL_CTX_ticket_API),
  64294. TEST_DECL(test_wolfSSL_SetTmpDH_file),
  64295. TEST_DECL(test_wolfSSL_SetTmpDH_buffer),
  64296. TEST_DECL(test_wolfSSL_SetMinMaxDhKey_Sz),
  64297. TEST_DECL(test_SetTmpEC_DHE_Sz),
  64298. TEST_DECL(test_wolfSSL_CTX_get0_privatekey),
  64299. #ifdef WOLFSSL_DTLS
  64300. TEST_DECL(test_wolfSSL_DtlsUpdateWindow),
  64301. TEST_DECL(test_wolfSSL_DTLS_fragment_buckets),
  64302. #endif
  64303. TEST_DECL(test_wolfSSL_dtls_set_mtu),
  64304. /* Uses Assert in handshake callback. */
  64305. TEST_DECL(test_wolfSSL_dtls_plaintext),
  64306. #if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) && \
  64307. defined(HAVE_IO_TESTS_DEPENDENCIES)
  64308. TEST_DECL(test_wolfSSL_read_write),
  64309. /* Can't memory test as server hangs if client fails before second connect.
  64310. */
  64311. TEST_DECL(test_wolfSSL_reuse_WOLFSSLobj),
  64312. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_1),
  64313. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_2),
  64314. TEST_DECL(test_wolfSSL_CTX_verifyDepth_ServerClient_3),
  64315. TEST_DECL(test_wolfSSL_CTX_set_cipher_list),
  64316. /* Can't memory test as server hangs. */
  64317. TEST_DECL(test_wolfSSL_dtls_export),
  64318. /* Uses Assert in handshake callback. */
  64319. TEST_DECL(test_wolfSSL_tls_export),
  64320. #endif
  64321. TEST_DECL(test_wolfSSL_dtls_export_peers),
  64322. TEST_DECL(test_wolfSSL_SetMinVersion),
  64323. TEST_DECL(test_wolfSSL_CTX_SetMinVersion),
  64324. /* wolfSSL handshake APIs. */
  64325. TEST_DECL(test_wolfSSL_CTX_get0_set1_param),
  64326. TEST_DECL(test_wolfSSL_a2i_IPADDRESS),
  64327. TEST_DECL(test_wolfSSL_BUF),
  64328. TEST_DECL(test_wolfSSL_set_tlsext_status_type),
  64329. /* Can't memory test as server hangs. */
  64330. TEST_DECL(test_wolfSSL_CTX_set_client_CA_list),
  64331. TEST_DECL(test_wolfSSL_CTX_add_client_CA),
  64332. TEST_DECL(test_wolfSSL_CTX_set_srp_username),
  64333. TEST_DECL(test_wolfSSL_CTX_set_srp_password),
  64334. TEST_DECL(test_wolfSSL_CTX_set_keylog_callback),
  64335. TEST_DECL(test_wolfSSL_CTX_get_keylog_callback),
  64336. TEST_DECL(test_wolfSSL_Tls12_Key_Logging_test),
  64337. /* Can't memory test as server hangs. */
  64338. TEST_DECL(test_wolfSSL_Tls13_Key_Logging_test),
  64339. TEST_DECL(test_wolfSSL_Tls13_postauth),
  64340. TEST_DECL(test_wolfSSL_set_ecdh_auto),
  64341. TEST_DECL(test_wolfSSL_CTX_set_ecdh_auto),
  64342. TEST_DECL(test_wolfSSL_set_minmax_proto_version),
  64343. TEST_DECL(test_wolfSSL_CTX_set_max_proto_version),
  64344. TEST_DECL(test_wolfSSL_THREADID_hash),
  64345. /* TLS extensions tests */
  64346. #ifdef HAVE_IO_TESTS_DEPENDENCIES
  64347. #ifdef HAVE_SNI
  64348. TEST_DECL(test_wolfSSL_UseSNI_params),
  64349. /* Uses Assert in handshake callback. */
  64350. TEST_DECL(test_wolfSSL_UseSNI_connection),
  64351. TEST_DECL(test_wolfSSL_SNI_GetFromBuffer),
  64352. #endif /* HAVE_SNI */
  64353. #endif
  64354. TEST_DECL(test_wolfSSL_UseTrustedCA),
  64355. TEST_DECL(test_wolfSSL_UseMaxFragment),
  64356. TEST_DECL(test_wolfSSL_UseTruncatedHMAC),
  64357. TEST_DECL(test_wolfSSL_UseSupportedCurve),
  64358. #if defined(HAVE_ALPN) && defined(HAVE_IO_TESTS_DEPENDENCIES)
  64359. /* Uses Assert in handshake callback. */
  64360. TEST_DECL(test_wolfSSL_UseALPN_connection),
  64361. TEST_DECL(test_wolfSSL_UseALPN_params),
  64362. #endif
  64363. #ifdef HAVE_ALPN_PROTOS_SUPPORT
  64364. /* Uses Assert in handshake callback. */
  64365. TEST_DECL(test_wolfSSL_set_alpn_protos),
  64366. #endif
  64367. TEST_DECL(test_wolfSSL_DisableExtendedMasterSecret),
  64368. TEST_DECL(test_wolfSSL_wolfSSL_UseSecureRenegotiation),
  64369. TEST_DECL(test_wolfSSL_SCR_Reconnect),
  64370. TEST_DECL(test_tls_ext_duplicate),
  64371. #if defined(WOLFSSL_TLS13) && defined(HAVE_ECH) && \
  64372. defined(HAVE_IO_TESTS_DEPENDENCIES)
  64373. TEST_DECL(test_wolfSSL_Tls13_ECH_params),
  64374. /* Uses Assert in handshake callback. */
  64375. TEST_DECL(test_wolfSSL_Tls13_ECH),
  64376. #endif
  64377. TEST_DECL(test_wolfSSL_X509_TLS_version_test_1),
  64378. TEST_DECL(test_wolfSSL_X509_TLS_version_test_2),
  64379. /* OCSP Stapling */
  64380. TEST_DECL(test_wolfSSL_UseOCSPStapling),
  64381. TEST_DECL(test_wolfSSL_UseOCSPStaplingV2),
  64382. TEST_DECL(test_self_signed_stapling),
  64383. /* Multicast */
  64384. TEST_DECL(test_wolfSSL_mcast),
  64385. TEST_DECL(test_wolfSSL_read_detect_TCP_disconnect),
  64386. TEST_DECL(test_wolfSSL_msgCb),
  64387. TEST_DECL(test_wolfSSL_either_side),
  64388. TEST_DECL(test_wolfSSL_DTLS_either_side),
  64389. /* Uses Assert in handshake callback. */
  64390. TEST_DECL(test_wolfSSL_dtls_fragments),
  64391. /* Uses Assert in handshake callback. */
  64392. TEST_DECL(test_wolfSSL_dtls_AEAD_limit),
  64393. /* Uses Assert in handshake callback. */
  64394. TEST_DECL(test_wolfSSL_ignore_alert_before_cookie),
  64395. /* Uses Assert in handshake callback. */
  64396. TEST_DECL(test_wolfSSL_dtls_bad_record),
  64397. /* Uses Assert in handshake callback. */
  64398. TEST_DECL(test_wolfSSL_dtls_stateless),
  64399. TEST_DECL(test_generate_cookie),
  64400. #ifndef NO_BIO
  64401. /* Can't memory test as server hangs. */
  64402. TEST_DECL(test_wolfSSL_BIO_connect),
  64403. /* Can't memory test as server Asserts in thread. */
  64404. TEST_DECL(test_wolfSSL_BIO_accept),
  64405. TEST_DECL(test_wolfSSL_BIO_tls),
  64406. #endif
  64407. #if defined(HAVE_PK_CALLBACKS) && !defined(WOLFSSL_NO_TLS12)
  64408. TEST_DECL(test_DhCallbacks),
  64409. #endif
  64410. #if defined(HAVE_KEYING_MATERIAL) && defined(HAVE_SSL_MEMIO_TESTS_DEPENDENCIES)
  64411. TEST_DECL(test_export_keying_material),
  64412. #endif
  64413. /* Can't memory test as client/server Asserts in thread. */
  64414. TEST_DECL(test_ticket_and_psk_mixing),
  64415. /* Can't memory test as client/server Asserts in thread. */
  64416. TEST_DECL(test_prioritize_psk),
  64417. /* Can't memory test as client/server hangs. */
  64418. TEST_DECL(test_wc_CryptoCb),
  64419. /* Can't memory test as client/server hangs. */
  64420. TEST_DECL(test_wolfSSL_CTX_StaticMemory),
  64421. #if !defined(NO_FILESYSTEM) && \
  64422. defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  64423. !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER)
  64424. #ifdef WOLFSSL_DTLS_NO_HVR_ON_RESUME
  64425. TEST_DECL(test_wolfSSL_dtls_stateless_resume),
  64426. #endif /* WOLFSSL_DTLS_NO_HVR_ON_RESUME */
  64427. #ifdef HAVE_MAX_FRAGMENT
  64428. TEST_DECL(test_wolfSSL_dtls_stateless_maxfrag),
  64429. #endif /* HAVE_MAX_FRAGMENT */
  64430. #ifndef NO_RSA
  64431. TEST_DECL(test_wolfSSL_dtls_stateless2),
  64432. #if !defined(NO_OLD_TLS)
  64433. TEST_DECL(test_wolfSSL_dtls_stateless_downgrade),
  64434. #endif /* !defined(NO_OLD_TLS) */
  64435. #endif /* ! NO_RSA */
  64436. #endif /* defined(WOLFSSL_DTLS) && !defined(WOLFSSL_NO_TLS12) && \
  64437. * !defined(NO_WOLFSSL_CLIENT) && !defined(NO_WOLFSSL_SERVER) */
  64438. TEST_DECL(test_wolfSSL_CTX_set_ciphersuites),
  64439. TEST_DECL(test_wolfSSL_CRL_CERT_REVOKED_alert),
  64440. TEST_DECL(test_TLS_13_ticket_different_ciphers),
  64441. TEST_DECL(test_WOLFSSL_dtls_version_alert),
  64442. #if defined(WOLFSSL_TICKET_NONCE_MALLOC) && defined(HAVE_SESSION_TICKET) \
  64443. && defined(WOLFSSL_TLS13) && \
  64444. (!defined(HAVE_FIPS) || (defined(FIPS_VERSION_GE) && FIPS_VERSION_GE(5,3)))
  64445. TEST_DECL(test_ticket_nonce_malloc),
  64446. #endif
  64447. TEST_DECL(test_ticket_ret_create),
  64448. TEST_DECL(test_extra_alerts_wrong_cs),
  64449. TEST_DECL(test_extra_alerts_skip_hs),
  64450. TEST_DECL(test_extra_alerts_bad_psk),
  64451. TEST_DECL(test_tls13_bad_psk_binder),
  64452. /* Can't memory test as client/server Asserts. */
  64453. TEST_DECL(test_harden_no_secure_renegotiation),
  64454. TEST_DECL(test_override_alt_cert_chain),
  64455. TEST_DECL(test_rpk_set_xxx_cert_type),
  64456. TEST_DECL(test_tls13_rpk_handshake),
  64457. TEST_DECL(test_dtls13_bad_epoch_ch),
  64458. TEST_DECL(test_short_session_id),
  64459. TEST_DECL(test_wolfSSL_dtls13_null_cipher),
  64460. /* Can't memory test as client/server hangs. */
  64461. TEST_DECL(test_dtls_msg_from_other_peer),
  64462. TEST_DECL(test_dtls_ipv6_check),
  64463. TEST_DECL(test_wolfSSL_SCR_after_resumption),
  64464. TEST_DECL(test_dtls_no_extensions),
  64465. TEST_DECL(test_TLSX_CA_NAMES_bad_extension),
  64466. TEST_DECL(test_dtls_1_0_hvr_downgrade),
  64467. TEST_DECL(test_session_ticket_no_id),
  64468. TEST_DECL(test_session_ticket_hs_update),
  64469. TEST_DECL(test_dtls_downgrade_scr_server),
  64470. TEST_DECL(test_dtls_downgrade_scr),
  64471. TEST_DECL(test_dtls_client_hello_timeout_downgrade),
  64472. TEST_DECL(test_dtls_client_hello_timeout),
  64473. TEST_DECL(test_dtls_dropped_ccs),
  64474. TEST_DECL(test_dtls_seq_num_downgrade),
  64475. TEST_DECL(test_certreq_sighash_algos),
  64476. TEST_DECL(test_revoked_loaded_int_cert),
  64477. TEST_DECL(test_dtls_frag_ch),
  64478. TEST_DECL(test_dtls13_frag_ch_pq),
  64479. TEST_DECL(test_dtls_empty_keyshare_with_cookie),
  64480. TEST_DECL(test_dtls_old_seq_number),
  64481. TEST_DECL(test_tls13_pq_groups),
  64482. TEST_DECL(test_tls13_early_data),
  64483. TEST_DECL(test_tls_multi_handshakes_one_record),
  64484. TEST_DECL(test_write_dup),
  64485. TEST_DECL(test_read_write_hs),
  64486. TEST_DECL(test_get_signature_nid),
  64487. TEST_DECL(test_tls_cert_store_unchanged),
  64488. /* This test needs to stay at the end to clean up any caches allocated. */
  64489. TEST_DECL(test_wolfSSL_Cleanup)
  64490. };
  64491. #define TEST_CASE_CNT (int)(sizeof(testCases) / sizeof(*testCases))
  64492. static void TestSetup(void)
  64493. {
  64494. /* Stub, for now. Add common test setup code here. */
  64495. }
  64496. static void TestCleanup(void)
  64497. {
  64498. #if defined(OPENSSL_EXTRA) || defined(DEBUG_WOLFSSL_VERBOSE)
  64499. /* Clear any errors added to the error queue during the test run. */
  64500. wolfSSL_ERR_clear_error();
  64501. #endif /* OPENSSL_EXTRA || DEBUG_WOLFSSL_VERBOSE */
  64502. }
  64503. /* Print out all API test cases with numeric identifier.
  64504. */
  64505. void ApiTest_PrintTestCases(void)
  64506. {
  64507. int i;
  64508. printf("All Test Cases:\n");
  64509. for (i = 0; i < TEST_CASE_CNT; i++) {
  64510. printf("%3d: %s\n", i + 1, testCases[i].name);
  64511. }
  64512. }
  64513. /* Add test case with index to the list to run.
  64514. *
  64515. * @param [in] idx Index of test case to run starting at 1.
  64516. * @return 0 on success.
  64517. * @return BAD_FUNC_ARG when index is out of range of test case identifiers.
  64518. */
  64519. int ApiTest_RunIdx(int idx)
  64520. {
  64521. if (idx < 1 || idx > TEST_CASE_CNT) {
  64522. printf("Index out of range (1 - %d): %d\n", TEST_CASE_CNT, idx);
  64523. return BAD_FUNC_ARG;
  64524. }
  64525. testAll = 0;
  64526. testCases[idx-1].run = 1;
  64527. return 0;
  64528. }
  64529. /* Add test case with name to the list to run.
  64530. *
  64531. * @param [in] name Name of test case to run.
  64532. * @return 0 on success.
  64533. * @return BAD_FUNC_ARG when name is not a known test case name.
  64534. */
  64535. int ApiTest_RunName(char* name)
  64536. {
  64537. int i;
  64538. for (i = 0; i < TEST_CASE_CNT; i++) {
  64539. if (XSTRCMP(testCases[i].name, name) == 0) {
  64540. testAll = 0;
  64541. testCases[i].run = 1;
  64542. return 0;
  64543. }
  64544. }
  64545. printf("Test case name not found: %s\n", name);
  64546. printf("Use --list to see all test case names.\n");
  64547. return BAD_FUNC_ARG;
  64548. }
  64549. /* Converts the result code to a string.
  64550. *
  64551. * @param [in] res Test result code.
  64552. * @return String describing test result.
  64553. */
  64554. static const char* apitest_res_string(int res)
  64555. {
  64556. const char* str = "invalid result";
  64557. switch (res) {
  64558. case TEST_SUCCESS:
  64559. str = "passed";
  64560. break;
  64561. case TEST_FAIL:
  64562. str = "failed";
  64563. break;
  64564. case TEST_SKIPPED:
  64565. str = "skipped";
  64566. break;
  64567. }
  64568. return str;
  64569. }
  64570. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  64571. static double gettime_secs(void)
  64572. #if defined(_MSC_VER) && defined(_WIN32)
  64573. {
  64574. /* there's no gettimeofday for Windows, so we'll use system time */
  64575. #define EPOCH_DIFF 11644473600LL
  64576. FILETIME currentFileTime;
  64577. GetSystemTimePreciseAsFileTime(&currentFileTime);
  64578. ULARGE_INTEGER uli = { 0, 0 };
  64579. uli.LowPart = currentFileTime.dwLowDateTime;
  64580. uli.HighPart = currentFileTime.dwHighDateTime;
  64581. /* Convert to seconds since Unix epoch */
  64582. return (double)((uli.QuadPart - (EPOCH_DIFF * 10000000)) / 10000000.0);
  64583. }
  64584. #else
  64585. {
  64586. struct timeval tv;
  64587. LIBCALL_CHECK_RET(gettimeofday(&tv, 0));
  64588. return (double)tv.tv_sec + (double)tv.tv_usec / 1000000.0;
  64589. }
  64590. #endif
  64591. #endif
  64592. int ApiTest(void)
  64593. {
  64594. int i;
  64595. int ret;
  64596. int res = 0;
  64597. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  64598. double timeDiff;
  64599. #endif
  64600. printf(" Begin API Tests\n");
  64601. fflush(stdout);
  64602. /* we must perform init and cleanup if not all tests are running */
  64603. if (!testAll) {
  64604. #ifdef WOLFCRYPT_ONLY
  64605. if (wolfCrypt_Init() != 0) {
  64606. printf("wolfCrypt Initialization failed\n");
  64607. res = 1;
  64608. }
  64609. #else
  64610. if (wolfSSL_Init() != WOLFSSL_SUCCESS) {
  64611. printf("wolfSSL Initialization failed\n");
  64612. res = 1;
  64613. }
  64614. #endif
  64615. }
  64616. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  64617. if (res == 0) {
  64618. if (create_tmp_dir(tmpDirName, sizeof(tmpDirName) - 1) == NULL) {
  64619. printf("failed to create tmp dir\n");
  64620. res = 1;
  64621. }
  64622. else {
  64623. tmpDirNameSet = 1;
  64624. }
  64625. }
  64626. #endif
  64627. if (res == 0) {
  64628. for (i = 0; i < TEST_CASE_CNT; ++i) {
  64629. EXPECT_DECLS;
  64630. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  64631. currentTestName = testCases[i].name;
  64632. #endif
  64633. /* When not testing all cases then skip if not marked for running.
  64634. */
  64635. if (!testAll && !testCases[i].run) {
  64636. continue;
  64637. }
  64638. TestSetup();
  64639. printf(" %3d: %-52s:", i + 1, testCases[i].name);
  64640. fflush(stdout);
  64641. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  64642. timeDiff = gettime_secs();
  64643. #endif
  64644. ret = testCases[i].func();
  64645. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  64646. timeDiff = gettime_secs() - timeDiff;
  64647. #endif
  64648. #ifndef WOLFSSL_UNIT_TEST_NO_TIMING
  64649. if (ret != TEST_SKIPPED) {
  64650. printf(" %s (%9.5lf)\n", apitest_res_string(ret), timeDiff);
  64651. }
  64652. else
  64653. #endif
  64654. {
  64655. printf(" %s\n", apitest_res_string(ret));
  64656. }
  64657. fflush(stdout);
  64658. /* if return code is < 0 and not skipped then assert error */
  64659. Expect((ret > 0 || ret == TEST_SKIPPED),
  64660. ("Test failed\n"),
  64661. ("ret %d", ret));
  64662. testCases[i].fail = ((ret <= 0) && (ret != TEST_SKIPPED));
  64663. res |= ((ret <= 0) && (ret != TEST_SKIPPED));
  64664. TestCleanup();
  64665. }
  64666. }
  64667. #if defined(HAVE_ECC) && defined(FP_ECC) && defined(HAVE_THREAD_LS) \
  64668. && (defined(NO_MAIN_DRIVER) || defined(HAVE_STACK_SIZE))
  64669. wc_ecc_fp_free(); /* free per thread cache */
  64670. #endif
  64671. if (!testAll) {
  64672. #ifdef WOLFCRYPT_ONLY
  64673. wolfCrypt_Cleanup();
  64674. #else
  64675. wolfSSL_Cleanup();
  64676. #endif
  64677. }
  64678. (void)testDevId;
  64679. if (res != 0) {
  64680. printf("\nFAILURES:\n");
  64681. for (i = 0; i < TEST_CASE_CNT; ++i) {
  64682. if (testCases[i].fail) {
  64683. printf(" %3d: %s\n", i + 1, testCases[i].name);
  64684. }
  64685. }
  64686. printf("\n");
  64687. fflush(stdout);
  64688. }
  64689. #ifdef WOLFSSL_DUMP_MEMIO_STREAM
  64690. if (tmpDirNameSet) {
  64691. printf("\nBinary dumps of the memio streams can be found in the\n"
  64692. "%s directory. This can be imported into\n"
  64693. "Wireshark by transforming the file with\n"
  64694. "\tod -Ax -tx1 -v stream.dump > stream.dump.hex\n"
  64695. "And then loading test_output.dump.hex into Wireshark using\n"
  64696. "the \"Import from Hex Dump...\" option and selecting the\n"
  64697. "TCP encapsulation option.\n", tmpDirName);
  64698. }
  64699. #endif
  64700. printf(" End API Tests\n");
  64701. fflush(stdout);
  64702. return res;
  64703. }